Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
401
402
403
404
405
406
407
408
409
/*
 * Backtrace debugging
 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */

#ifdef WPA_TRACE_BFD
#define _GNU_SOURCE
#include <link.h>
#endif /* WPA_TRACE_BCD */
#include "includes.h"

#include "common.h"
#include "trace.h"

#ifdef WPA_TRACE

static struct dl_list active_references =
{ &active_references, &active_references };

#ifdef WPA_TRACE_BFD
#include <bfd.h>

#define DMGL_PARAMS      (1 << 0)
#define DMGL_ANSI        (1 << 1)

static char *prg_fname = NULL;
static bfd *cached_abfd = NULL;
static asymbol **syms = NULL;
static unsigned long start_offset;
static int start_offset_looked_up;


static int callback(struct dl_phdr_info *info, size_t size, void *data)
{
	/*
	 * dl_iterate_phdr(3):
	 * "The first object visited by callback is the main program."
	 */
	start_offset = info->dlpi_addr;

	/*
	 * dl_iterate_phdr(3):
	 * "The dl_iterate_phdr() function walks through the list of an
	 *  application's shared objects and calls the function callback
	 *  once for each object, until either all shared objects have
	 *  been processed or callback returns a nonzero value."
	 */
	return 1;
}


static void get_prg_fname(void)
{
	char exe[50], fname[512];
	int len;
	os_snprintf(exe, sizeof(exe) - 1, "/proc/%u/exe", getpid());
	len = readlink(exe, fname, sizeof(fname) - 1);
	if (len < 0 || len >= (int) sizeof(fname)) {
		wpa_printf(MSG_ERROR, "readlink: %s", strerror(errno));
		return;
	}
	fname[len] = '\0';
	prg_fname = strdup(fname);
}


static bfd * open_bfd(const char *fname)
{
	bfd *abfd;
	char **matching;

	abfd = bfd_openr(prg_fname, NULL);
	if (abfd == NULL) {
		wpa_printf(MSG_INFO, "bfd_openr failed");
		return NULL;
	}

	if (bfd_check_format(abfd, bfd_archive)) {
		wpa_printf(MSG_INFO, "bfd_check_format failed");
		bfd_close(abfd);
		return NULL;
	}

	if (!bfd_check_format_matches(abfd, bfd_object, &matching)) {
		wpa_printf(MSG_INFO, "bfd_check_format_matches failed");
		free(matching);
		bfd_close(abfd);
		return NULL;
	}

	return abfd;
}


static void read_syms(bfd *abfd)
{
	long storage, symcount;
	bfd_boolean dynamic = FALSE;

	if (syms)
		return;

	if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) {
		wpa_printf(MSG_INFO, "No symbols");
		return;
	}

	storage = bfd_get_symtab_upper_bound(abfd);
	if (storage == 0) {
		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
		dynamic = TRUE;
	}
	if (storage < 0) {
		wpa_printf(MSG_INFO, "Unknown symtab upper bound");
		return;
	}

	syms = malloc(storage);
	if (syms == NULL) {
		wpa_printf(MSG_INFO, "Failed to allocate memory for symtab "
			   "(%ld bytes)", storage);
		return;
	}
	if (dynamic)
		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
	else
		symcount = bfd_canonicalize_symtab(abfd, syms);
	if (symcount < 0) {
		wpa_printf(MSG_INFO, "Failed to canonicalize %ssymtab",
			   dynamic ? "dynamic " : "");
		free(syms);
		syms = NULL;
		return;
	}
}


struct bfd_data {
	bfd_vma pc;
	bfd_boolean found;
	const char *filename;
	const char *function;
	unsigned int line;
};


static void find_addr_sect(bfd *abfd, asection *section, void *obj)
{
	struct bfd_data *data = obj;
	bfd_vma vma;
	bfd_size_type size;

	if (data->found)
		return;

	if (!(bfd_get_section_vma(abfd, section)))
		return;

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

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

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


static void wpa_trace_bfd_addr(void *pc)
{
	bfd *abfd = cached_abfd;
	struct bfd_data data;
	const char *name;
	char *aname = NULL;
	const char *filename;

	if (abfd == NULL)
		return;

	data.pc = (bfd_hostptr_t) (pc - start_offset);
	data.found = FALSE;
	bfd_map_over_sections(abfd, find_addr_sect, &data);

	if (!data.found)
		return;

	do {
		if (data.function)
			aname = bfd_demangle(abfd, data.function,
					     DMGL_ANSI | DMGL_PARAMS);
		name = aname ? aname : data.function;
		filename = data.filename;
		if (filename) {
			char *end = os_strrchr(filename, '/');
			int i = 0;
			while (*filename && *filename == prg_fname[i] &&
			       filename <= end) {
				filename++;
				i++;
			}
		}
		wpa_printf(MSG_INFO, "     %s() %s:%u",
			   name, filename, data.line);
		free(aname);
		aname = NULL;

		data.found = bfd_find_inliner_info(abfd, &data.filename,
						   &data.function, &data.line);
	} while (data.found);
}


static const char * wpa_trace_bfd_addr2func(void *pc)
{
	bfd *abfd = cached_abfd;
	struct bfd_data data;

	if (abfd == NULL)
		return NULL;

	data.pc = (bfd_hostptr_t) (pc - start_offset);
	data.found = FALSE;
	bfd_map_over_sections(abfd, find_addr_sect, &data);

	if (!data.found)
		return NULL;

	return data.function;
}


static void wpa_trace_bfd_init(void)
{
	if (!prg_fname) {
		get_prg_fname();
		if (!prg_fname)
			return;
	}

	if (!cached_abfd) {
		cached_abfd = open_bfd(prg_fname);
		if (!cached_abfd) {
			wpa_printf(MSG_INFO, "Failed to open bfd");
			return;
		}
	}

	read_syms(cached_abfd);
	if (!syms) {
		wpa_printf(MSG_INFO, "Failed to read symbols");
		return;
	}

	if (!start_offset_looked_up) {
		dl_iterate_phdr(callback, NULL);
		start_offset_looked_up = 1;
	}
}


void wpa_trace_dump_funcname(const char *title, void *pc)
{
	wpa_printf(MSG_INFO, "WPA_TRACE: %s: %p", title, pc);
	wpa_trace_bfd_init();
	wpa_trace_bfd_addr(pc);
}


size_t wpa_trace_calling_func(const char *buf[], size_t len)
{
	bfd *abfd;
	void *btrace_res[WPA_TRACE_LEN];
	int i, btrace_num;
	size_t pos = 0;

	if (len == 0)
		return 0;
	if (len > WPA_TRACE_LEN)
		len = WPA_TRACE_LEN;

	wpa_trace_bfd_init();
	abfd = cached_abfd;
	if (!abfd)
		return 0;

	btrace_num = backtrace(btrace_res, len);
	if (btrace_num < 1)
		return 0;

	for (i = 0; i < btrace_num; i++) {
		struct bfd_data data;

		data.pc = (bfd_hostptr_t) (btrace_res[i] - start_offset);
		data.found = FALSE;
		bfd_map_over_sections(abfd, find_addr_sect, &data);

		while (data.found) {
			if (data.function &&
			    (pos > 0 ||
			     os_strcmp(data.function, __func__) != 0)) {
				buf[pos++] = data.function;
				if (pos == len)
					return pos;
			}

			data.found = bfd_find_inliner_info(abfd, &data.filename,
							   &data.function,
							   &data.line);
		}
	}

	return pos;
}

#else /* WPA_TRACE_BFD */

#define wpa_trace_bfd_init() do { } while (0)
#define wpa_trace_bfd_addr(pc) do { } while (0)
#define wpa_trace_bfd_addr2func(pc) NULL

#endif /* WPA_TRACE_BFD */

void wpa_trace_dump_func(const char *title, void **btrace, int btrace_num)
{
	char **sym;
	int i;
	enum { TRACE_HEAD, TRACE_RELEVANT, TRACE_TAIL } state;

	wpa_trace_bfd_init();
	wpa_printf(MSG_INFO, "WPA_TRACE: %s - START", title);
	sym = backtrace_symbols(btrace, btrace_num);
	state = TRACE_HEAD;
	for (i = 0; i < btrace_num; i++) {
		const char *func = wpa_trace_bfd_addr2func(btrace[i]);
		if (state == TRACE_HEAD && func &&
		    (os_strcmp(func, "wpa_trace_add_ref_func") == 0 ||
		     os_strcmp(func, "wpa_trace_check_ref") == 0 ||
		     os_strcmp(func, "wpa_trace_show") == 0))
			continue;
		if (state == TRACE_TAIL && sym && sym[i] &&
		    os_strstr(sym[i], "__libc_start_main"))
			break;
		if (state == TRACE_HEAD)
			state = TRACE_RELEVANT;
		if (sym)
			wpa_printf(MSG_INFO, "[%d]: %s", i, sym[i]);
		else
			wpa_printf(MSG_INFO, "[%d]: ?? [%p]", i, btrace[i]);
		wpa_trace_bfd_addr(btrace[i]);
		if (state == TRACE_RELEVANT && func &&
		    os_strcmp(func, "main") == 0)
			state = TRACE_TAIL;
	}
	free(sym);
	wpa_printf(MSG_INFO, "WPA_TRACE: %s - END", title);
}


void wpa_trace_show(const char *title)
{
	struct info {
		WPA_TRACE_INFO
	} info;
	wpa_trace_record(&info);
	wpa_trace_dump(title, &info);
}


void wpa_trace_add_ref_func(struct wpa_trace_ref *ref, const void *addr)
{
	if (addr == NULL)
		return;
	ref->addr = addr;
	wpa_trace_record(ref);
	dl_list_add(&active_references, &ref->list);
}


void wpa_trace_check_ref(const void *addr)
{
	struct wpa_trace_ref *ref;
	dl_list_for_each(ref, &active_references, struct wpa_trace_ref, list) {
		if (addr != ref->addr)
			continue;
		wpa_trace_show("Freeing referenced memory");
		wpa_trace_dump("Reference registration", ref);
		abort();
	}
}


void wpa_trace_deinit(void)
{
#ifdef WPA_TRACE_BFD
	free(syms);
	syms = NULL;
#endif /* WPA_TRACE_BFD */
}

#endif /* WPA_TRACE */