summaryrefslogtreecommitdiff
path: root/src/shared-objects.c
blob: caad562ecf7d8e15c1028a70b08098b845161515 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
   This file is part of odin, a memory profiler with fragmentation analysis.

   Copyright (C) 2007 Chris Wilson <chris@chris-wilson.co.uk>

   odin is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   odin 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with odin. If not, see <http://www.gnu.org/licenses/>/

   The GNU General Public License is contained in the file COPYING.
*/

#define _GNU_SOURCE
#include <stdlib.h>
#include <link.h>
#include <string.h>

#include "odin.h"
#include "client.h"
#include "shared-objects.h"

//#define ODIN_USE_BFD

#ifdef ODIN_USE_BFD
#include <bfd.h>
#else
#include "minibfd/binfile.h"
#endif


struct _symbol_offset {
    SymbolOffset *next;
    gulong eip;
    gulong offset;
};

struct _shared_object {
    SharedObjects *objects;
    SharedObject *ht_next;

    SharedObject *pending_next;

    guint key;
    const char *name;

#ifdef ODIN_USE_BFD
    bfd *abfd;
    asymbol **syms;
#else
    BinFile bin_file;
#endif

    SymbolOffset *pending_lookups;
};

#ifdef ODIN_USE_BFD
struct find_address {
    asymbol **syms;
    gulong pc;
    const char *path;
    const char *function;
    unsigned int line;
    gboolean found;
};

static gboolean
slurp_symtab (bfd *abfd, asymbol ***syms)
{
    long symcount;
    unsigned int size;

    if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
	return FALSE;

    symcount = bfd_read_minisymbols (abfd, FALSE, (void *) syms, &size);
    if (symcount == 0)
	symcount = bfd_read_minisymbols (abfd, TRUE, (void *) syms, &size);

    return symcount > 0;
}

static void
find_address_in_section (bfd *abfd, asection *section, void *data)
{
    struct find_address *fa = data;
    bfd_vma vma;
    bfd_size_type size;

    if (fa->found)
	return;

    if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
	return;

    vma = bfd_get_section_vma (abfd, section);
    if (fa->pc < vma)
	return;

    size = bfd_get_section_size (section);
    if (fa->pc >= vma + size)
	return;

    fa->found = bfd_find_nearest_line (abfd, section, fa->syms, fa->pc - vma,
	                               &fa->path,
				       &fa->function,
				       &fa->line);
}

static void
_shared_object_open (SharedObject *so)
{
    if (so->abfd == NULL) {
	so->abfd = bfd_openr (so->name, NULL);
	if (so->abfd == NULL)
	    return;

	if (bfd_check_format (so->abfd, bfd_archive) ||
	    ! bfd_check_format_matches (so->abfd, bfd_object, NULL) ||
	    ! slurp_symtab (so->abfd, &so->syms))
	 {
	    bfd_close (so->abfd);
	    so->abfd = NULL;
	    return;
	}
    }
}


static gboolean
_shared_object_has_symbols (SharedObject *so)
{
    return so->abfd != NULL;
}

static void
_shared_object_lookup_symbol (SharedObject *so,
	                      gulong offset,
			      const char **function,
			      const char **filename,
			      const char **path,
			      guint *line)
{
    struct find_address fa;

    fa.syms = so->syms;
    fa.pc = offset;
    fa.found = FALSE;
    fa.path = NULL;
    fa.function = NULL;
    fa.line = 0;

    bfd_map_over_sections (so->abfd, find_address_in_section, &fa);

    *path = fa.path;
    if (fa.function != NULL && *fa.function != '\0') {
	char *demangled = bfd_demangle (so->abfd, fa.function, 3);
	if (demangled != NULL) {
	    *function = client_add_string (so->objects->client, demangled);
	    g_free (demangled);
	} else
	    *function = fa.function;
    }
    *line = fa.line;
}

static void
_shared_object_close (SharedObject *so)
{
    if (so->abfd != NULL) {
	bfd_close (so->abfd);
	so->abfd = NULL;
    }

    if (so->syms != NULL) {
	free (so->syms);
	so->syms = NULL;
    }
}

#else
extern char *minibfd_demangle (const char *mangled, int options);

static void
_shared_object_open (SharedObject *so)
{
    bin_file_open (&so->bin_file);
}

static gboolean
_shared_object_has_symbols (SharedObject *so)
{
    return so->bin_file.real_filename != NULL;
}

static void
_shared_object_lookup_symbol (SharedObject *so,
	                      gulong offset,
	                      const char **function,
	                      const char **filename,
	                      const char **path,
			      guint *line)
{
    bin_file_lookup_symbol (&so->bin_file, offset,
	                    function,
			    filename,
			    path,
			    line);
    if (*function != NULL) {
	char *demangled = minibfd_demangle (*function, 3);
	if (demangled != NULL) {
	    *function = client_add_string (so->objects->client, demangled);
	    g_free (demangled);
	}
    }
}

static void
_shared_object_close (SharedObject *so)
{
    bin_file_close (&so->bin_file);
}
#endif

void
shared_object_queue_lookup_symbol (SharedObject *so, gulong eip, gulong offset)
{
    SymbolOffset *symbol;

    if (so->pending_lookups == NULL) {
	so->pending_next = so->objects->pending_lookups;
	so->objects->pending_lookups = so;
    }

    symbol = so->objects->symbol_offset_free_list;
    if (symbol != NULL)
	so->objects->symbol_offset_free_list = symbol->next;
    else
	symbol = client_perm_alloc (so->objects->client, sizeof (SymbolOffset));

    symbol->eip = eip;
    symbol->offset = offset;
    symbol->next = so->pending_lookups;
    so->pending_lookups = symbol;

}

void
shared_objects_lookup_symbols (SharedObjects *objects)
{
    Frames *frames = &objects->client->frames;
    SharedObject *so;

    for (so = objects->pending_lookups; so != NULL; so = so->pending_next) {
	SymbolOffset *symbol = so->pending_lookups, *next;

	_shared_object_open (so);

	do {
	    const gchar *function, *path, *filename;
	    guint lineno;

	    g_assert (frames_get (frames, symbol->eip) == NULL);

	    function = NULL;
	    filename = NULL;
	    path = NULL;
	    lineno = 0;

	    if (_shared_object_has_symbols (so)) {
		_shared_object_lookup_symbol (so, symbol->offset,
					      &function,
					      &filename,
					      &path,
					      &lineno);
		if (filename == NULL && (path != NULL && *path != '\0')) {
		    const char *slash = strrchr (path, '/');
		    if (slash)
			filename = slash + 1;
		    else
			filename = path;
		}
	    }

	    frames_add (frames, symbol->eip, so->name,
		        function, filename, path, lineno);

	    next = symbol->next;
	    symbol->next = objects->symbol_offset_free_list;
	    objects->symbol_offset_free_list = symbol;
	} while ((symbol = next));

	so->pending_lookups = NULL;
	_shared_object_close (so);
    }
    objects->pending_lookups = NULL;
}

SharedObject *
shared_object_add (SharedObjects *objects, guint key, const char *name)
{
    SharedObject *so;
    guint index;

    g_assert (shared_object_get (objects, key) == NULL);

    if (++objects->ht.nnodes > 3 * objects->ht.size) {
	SharedObject **new_nodes;
	SharedObject *node, *next;
	guint n, new_size;

	new_size = g_spaced_primes_closest (objects->ht.nnodes);
	new_nodes = g_new0 (SharedObject *, new_size);
	for (n = 0; n < objects->ht.size ; n++) {
	    for (node = objects->ht.nodes[n];
		 node != NULL;
		 node = next)
	    {
		next = node->ht_next;
		index = node->key % new_size;
		node->ht_next = new_nodes[index];
		new_nodes[index] = node;
	    }
	}
	g_free (objects->ht.nodes);
	objects->ht.nodes = new_nodes;
	objects->ht.size  = new_size;
    }

    so = client_perm_alloc (objects->client, sizeof (SharedObject));
    so->objects = objects;
    so->pending_next = NULL;
    so->key = key;
    so->name = client_add_string (objects->client, name);
#ifdef ODIN_USE_BFD
    so->abfd = NULL;
    so->syms = NULL;
#else
    bin_file_init (&so->bin_file, so->name);
#endif
    so->pending_lookups = NULL;

    index = key % objects->ht.size;
    so->ht_next = objects->ht.nodes[index];
    objects->ht.nodes[index] = so;
    g_assert (shared_object_get (objects, key) == so);

    return so;
}

SharedObject *
shared_object_get (SharedObjects *objects,
		   guint key)
{
    guint index = key % objects->ht.size;
    SharedObject *so = objects->ht.nodes[index];
    while (so != NULL) {
	if (so->key == key)
	    return so;

	so = so->ht_next;
    }

    return NULL;
}

void
shared_objects_init (SharedObjects *objects, Client *client)
{
    objects->client = client;

    objects->ht.size = 673;
    objects->ht.nnodes = 0;
    objects->ht.nodes = g_new0 (SharedObject *, objects->ht.size);

    objects->pending_lookups = NULL;

    objects->symbol_offset_free_list = NULL;
}

void
shared_objects_fini (SharedObjects *objects)
{
    guint index;
    SharedObject *so;

    for (index = 0; index < objects->ht.size; index++) {
	for (so = objects->ht.nodes[index]; so != NULL; so = so->ht_next)
	    _shared_object_close (so);
    }
    g_free (objects->ht.nodes);
}