summaryrefslogtreecommitdiff
path: root/src/hgspy_helper.c
blob: 7c64386e178490123df3016df0015c85a5f36293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* 
 * hgspy_helper.c
 * Copyright (C) 2006 Akira TAGOH
 * 
 * Authors:
 *   Akira TAGOH  <at@gclab.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <gmodule.h>
#include <hieroglyph/hgtypes.h>
#include <hieroglyph/hgmem.h>
#include "../hieroglyph/hgallocator-private.h"
#include "hgspy_helper.h"
#include "visualizer.h"


static gpointer __hg_mem_pool_destroy = NULL;
static gpointer __hg_mem_pool_add_heap = NULL;
static gpointer __hg_mem_alloc_with_flags = NULL;
static gpointer __hg_mem_free = NULL;
static gpointer __hg_mem_resize = NULL;
static gpointer __hg_mem_garbage_collection = NULL;
static GModule *__handle = NULL;
static GtkWidget *visual = NULL;

/*
 * Private Functions
 */
static void  __attribute__((constructor))
helper_init(void)
{
	unsetenv("LD_PRELOAD");
	__handle = g_module_open("libhieroglyph.so", 0);
	if (__handle == NULL) {
		g_warning("Failed g_module_open: %s", g_module_error());
		exit(1);
	}
	if (!g_module_symbol(__handle, "hg_mem_pool_destroy", &__hg_mem_pool_destroy)) {
		g_warning("Failed g_module_symbol: %s", g_module_error());
		exit(1);
	}
	if (!g_module_symbol(__handle, "hg_mem_pool_add_heap", &__hg_mem_pool_add_heap)) {
		g_warning("Failed g_module_symbol: %s", g_module_error());
		exit(1);
	}
	if (!g_module_symbol(__handle, "hg_mem_alloc_with_flags", &__hg_mem_alloc_with_flags)) {
		g_warning("Failed g_module_symbol: %s", g_module_error());
		exit(1);
	}
	if (!g_module_symbol(__handle, "hg_mem_free", &__hg_mem_free)) {
		g_warning("Failed g_module_symbol: %s", g_module_error());
		exit(1);
	}
	if (!g_module_symbol(__handle, "hg_mem_resize", &__hg_mem_resize)) {
		g_warning("Failed g_module_symbol: %s", g_module_error());
		exit(1);
	}
	if (!g_module_symbol(__handle, "hg_mem_garbage_collection", &__hg_mem_garbage_collection)) {
		g_warning("Failed g_module_symbol: %s", g_module_error());
		exit(1);
	}
	g_type_init();
	visual = hg_memory_visualizer_new();
}

void  __attribute__((destructor))
helper_finalize(void)
{
	if (__handle)
		g_module_close(__handle);
}

/*
 * preload functions
 */
void
hg_mem_pool_destroy(HgMemPool *pool)
{
	gchar *pool_name = g_strdup(hg_mem_pool_get_name(pool));

	((void (*) (HgMemPool *))__hg_mem_pool_destroy) (pool);

	hg_memory_visualizer_remove_pool(HG_MEMORY_VISUALIZER (visual),
					 pool_name);
	g_free(pool_name);
}

void
hg_mem_pool_add_heap(HgMemPool *pool,
		     HgHeap    *heap)
{
	((void (*) (HgMemPool *, HgHeap *))__hg_mem_pool_add_heap) (pool, heap);
	hg_memory_visualizer_set_heap_state(HG_MEMORY_VISUALIZER (visual),
					    hg_mem_pool_get_name(pool),
					    heap);
}

gpointer
hg_mem_alloc_with_flags(HgMemPool *pool,
			gsize      size,
			guint      flags)
{
	gpointer retval = ((gpointer (*) (HgMemPool *, gsize, guint))
			   __hg_mem_alloc_with_flags) (pool, size, flags);
	HgMemObject *obj;

	if (retval) {
		hg_mem_get_object__inline(retval, obj);
		if (obj) {
			gint heap_id = HG_MEMOBJ_GET_HEAP_ID (obj);
			GPtrArray *a = obj->pool->heap_list;
			HgHeap *h;

			if (a && a->len > 0) {
				h = g_ptr_array_index(a, heap_id);
				hg_memory_visualizer_set_chunk_state(HG_MEMORY_VISUALIZER (visual),
								     hg_mem_pool_get_name(obj->pool),
								     heap_id,
								     (gsize)obj - (gsize)h->heaps,
								     hg_mem_get_object_size(retval),
								     HG_CHUNK_USED);
			}
		}
	}

	return retval;
}

gboolean
hg_mem_free(gpointer data)
{
	HgMemObject *obj;

	hg_mem_get_object__inline(data, obj);
	if (obj) {
		gint heap_id = HG_MEMOBJ_GET_HEAP_ID (obj);
		HgHeap *h = g_ptr_array_index(obj->pool->heap_list, heap_id);

		hg_memory_visualizer_set_chunk_state(HG_MEMORY_VISUALIZER (visual),
						     hg_mem_pool_get_name(obj->pool),
						     heap_id,
						     (gsize)obj - (gsize)h->heaps,
						     hg_mem_get_object_size(data),
						     HG_CHUNK_FREE);
	}

	return ((gboolean (*) (gpointer))__hg_mem_free) (data);
}

gpointer
hg_mem_resize(gpointer data,
	      gsize    size)
{
	gpointer retval;
	HgMemObject *obj;

	hg_mem_get_object__inline(data, obj);
	if (obj) {
		gint heap_id = HG_MEMOBJ_GET_HEAP_ID (obj);
		HgHeap *h = g_ptr_array_index(obj->pool->heap_list, heap_id);

		hg_memory_visualizer_set_chunk_state(HG_MEMORY_VISUALIZER (visual),
						     hg_mem_pool_get_name(obj->pool),
						     heap_id,
						     (gsize)obj - (gsize)h->heaps,
						     hg_mem_get_object_size(data),
						     HG_CHUNK_FREE);
	}

	retval = ((gpointer (*) (gpointer, gsize))__hg_mem_resize) (data, size);

	if (retval) {
		hg_mem_get_object__inline(retval, obj);
		if (obj) {
			gint heap_id = HG_MEMOBJ_GET_HEAP_ID (obj);
			HgHeap *h = g_ptr_array_index(obj->pool->heap_list, heap_id);

			hg_memory_visualizer_set_chunk_state(HG_MEMORY_VISUALIZER (visual),
							     hg_mem_pool_get_name(obj->pool),
							     heap_id,
							     (gsize)obj - (gsize)h->heaps,
							     hg_mem_get_object_size(retval),
							     HG_CHUNK_USED);
		}
	}

	return retval;
}

gboolean
hg_mem_garbage_collection(HgMemPool *pool)
{
	gboolean retval;

	hg_memory_visualizer_notify_gc_state(HG_MEMORY_VISUALIZER (visual), FALSE);
	retval = ((gboolean (*) (HgMemPool *))__hg_mem_garbage_collection) (pool);
	hg_memory_visualizer_notify_gc_state(HG_MEMORY_VISUALIZER (visual), TRUE);

	return retval;
}

/*
 * Public Functions
 */
GtkWidget *
hg_spy_helper_get_widget(void)
{
	return visual;
}