1 /*
2  * Backtrace debugging
3  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifdef WPA_TRACE_BFD
10 #define _GNU_SOURCE
11 #include <link.h>
12 #endif /* WPA_TRACE_BCD */
13 #include "includes.h"
14 
15 #include "common.h"
16 #include "trace.h"
17 
18 #ifdef WPA_TRACE
19 
20 static struct dl_list active_references =
21 { &active_references, &active_references };
22 
23 #ifdef WPA_TRACE_BFD
24 #include <bfd.h>
25 
26 #define DMGL_PARAMS      (1 << 0)
27 #define DMGL_ANSI        (1 << 1)
28 
29 static char *prg_fname = NULL;
30 static bfd *cached_abfd = NULL;
31 static asymbol **syms = NULL;
32 static unsigned long start_offset;
33 static int start_offset_looked_up;
34 
35 
callback(struct dl_phdr_info * info,size_t size,void * data)36 static int callback(struct dl_phdr_info *info, size_t size, void *data)
37 {
38 	/*
39 	 * dl_iterate_phdr(3):
40 	 * "The first object visited by callback is the main program."
41 	 */
42 	start_offset = info->dlpi_addr;
43 
44 	/*
45 	 * dl_iterate_phdr(3):
46 	 * "The dl_iterate_phdr() function walks through the list of an
47 	 *  application's shared objects and calls the function callback
48 	 *  once for each object, until either all shared objects have
49 	 *  been processed or callback returns a nonzero value."
50 	 */
51 	return 1;
52 }
53 
54 
get_prg_fname(void)55 static void get_prg_fname(void)
56 {
57 	char exe[50], fname[512];
58 	int len;
59 	os_snprintf(exe, sizeof(exe) - 1, "/proc/%u/exe", getpid());
60 	len = readlink(exe, fname, sizeof(fname) - 1);
61 	if (len < 0 || len >= (int) sizeof(fname)) {
62 		wpa_printf(MSG_ERROR, "readlink: %s", strerror(errno));
63 		return;
64 	}
65 	fname[len] = '\0';
66 	prg_fname = strdup(fname);
67 }
68 
69 
open_bfd(const char * fname)70 static bfd * open_bfd(const char *fname)
71 {
72 	bfd *abfd;
73 	char **matching;
74 
75 	abfd = bfd_openr(prg_fname, NULL);
76 	if (abfd == NULL) {
77 		wpa_printf(MSG_INFO, "bfd_openr failed");
78 		return NULL;
79 	}
80 
81 	if (bfd_check_format(abfd, bfd_archive)) {
82 		wpa_printf(MSG_INFO, "bfd_check_format failed");
83 		bfd_close(abfd);
84 		return NULL;
85 	}
86 
87 	if (!bfd_check_format_matches(abfd, bfd_object, &matching)) {
88 		wpa_printf(MSG_INFO, "bfd_check_format_matches failed");
89 		free(matching);
90 		bfd_close(abfd);
91 		return NULL;
92 	}
93 
94 	return abfd;
95 }
96 
97 
read_syms(bfd * abfd)98 static void read_syms(bfd *abfd)
99 {
100 	long storage, symcount;
101 	bfd_boolean dynamic = FALSE;
102 
103 	if (syms)
104 		return;
105 
106 	if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) {
107 		wpa_printf(MSG_INFO, "No symbols");
108 		return;
109 	}
110 
111 	storage = bfd_get_symtab_upper_bound(abfd);
112 	if (storage == 0) {
113 		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
114 		dynamic = TRUE;
115 	}
116 	if (storage < 0) {
117 		wpa_printf(MSG_INFO, "Unknown symtab upper bound");
118 		return;
119 	}
120 
121 	syms = malloc(storage);
122 	if (syms == NULL) {
123 		wpa_printf(MSG_INFO, "Failed to allocate memory for symtab "
124 			   "(%ld bytes)", storage);
125 		return;
126 	}
127 	if (dynamic)
128 		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
129 	else
130 		symcount = bfd_canonicalize_symtab(abfd, syms);
131 	if (symcount < 0) {
132 		wpa_printf(MSG_INFO, "Failed to canonicalize %ssymtab",
133 			   dynamic ? "dynamic " : "");
134 		free(syms);
135 		syms = NULL;
136 		return;
137 	}
138 }
139 
140 
141 struct bfd_data {
142 	bfd_vma pc;
143 	bfd_boolean found;
144 	const char *filename;
145 	const char *function;
146 	unsigned int line;
147 };
148 
149 /*
150  * binutils removed the bfd parameter and renamed things but
151  * those were macros so we can detect their absence.
152  * Cf. https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commitdiff;h=fd3619828e94a24a92cddec42cbc0ab33352eeb4;hp=5dfda3562a69686c43aad4fb0269cc9d5ec010d5
153  */
154 #ifndef bfd_get_section_vma
155 #define bfd_get_section_vma(bfd, section) bfd_section_vma(section)
156 #endif
157 #ifndef bfd_get_section_size
158 #define bfd_get_section_size bfd_section_size
159 #endif
160 
find_addr_sect(bfd * abfd,asection * section,void * obj)161 static void find_addr_sect(bfd *abfd, asection *section, void *obj)
162 {
163 	struct bfd_data *data = obj;
164 	bfd_vma vma;
165 	bfd_size_type size;
166 
167 	if (data->found)
168 		return;
169 
170 	if (!(bfd_get_section_vma(abfd, section)))
171 		return;
172 
173 	vma = bfd_get_section_vma(abfd, section);
174 	if (data->pc < vma)
175 		return;
176 
177 	size = bfd_get_section_size(section);
178 	if (data->pc >= vma + size)
179 		return;
180 
181 	data->found = bfd_find_nearest_line(abfd, section, syms,
182 					    data->pc - vma,
183 					    &data->filename,
184 					    &data->function,
185 					    &data->line);
186 }
187 
188 
wpa_trace_bfd_addr(void * pc)189 static void wpa_trace_bfd_addr(void *pc)
190 {
191 	bfd *abfd = cached_abfd;
192 	struct bfd_data data;
193 	const char *name;
194 	char *aname = NULL;
195 	const char *filename;
196 
197 	if (abfd == NULL)
198 		return;
199 
200 	if (start_offset > (uintptr_t) pc)
201 		return;
202 	data.pc = (uintptr_t) ((u8 *) pc - start_offset);
203 	data.found = FALSE;
204 	bfd_map_over_sections(abfd, find_addr_sect, &data);
205 
206 	if (!data.found)
207 		return;
208 
209 	do {
210 		if (data.function)
211 			aname = bfd_demangle(abfd, data.function,
212 					     DMGL_ANSI | DMGL_PARAMS);
213 		name = aname ? aname : data.function;
214 		filename = data.filename;
215 		if (filename) {
216 			char *end = os_strrchr(filename, '/');
217 			int i = 0;
218 			while (*filename && *filename == prg_fname[i] &&
219 			       filename <= end) {
220 				filename++;
221 				i++;
222 			}
223 		}
224 		wpa_printf(MSG_INFO, "     %s() %s:%u",
225 			   name, filename, data.line);
226 		free(aname);
227 		aname = NULL;
228 
229 		data.found = bfd_find_inliner_info(abfd, &data.filename,
230 						   &data.function, &data.line);
231 	} while (data.found);
232 }
233 
234 
wpa_trace_bfd_addr2func(void * pc)235 static const char * wpa_trace_bfd_addr2func(void *pc)
236 {
237 	bfd *abfd = cached_abfd;
238 	struct bfd_data data;
239 
240 	if (abfd == NULL)
241 		return NULL;
242 
243 	if (start_offset > (uintptr_t) pc)
244 		return NULL;
245 	data.pc = (uintptr_t) ((u8 *) pc - start_offset);
246 	data.found = FALSE;
247 	bfd_map_over_sections(abfd, find_addr_sect, &data);
248 
249 	if (!data.found)
250 		return NULL;
251 
252 	return data.function;
253 }
254 
255 
wpa_trace_bfd_init(void)256 static void wpa_trace_bfd_init(void)
257 {
258 	if (!prg_fname) {
259 		get_prg_fname();
260 		if (!prg_fname)
261 			return;
262 	}
263 
264 	if (!cached_abfd) {
265 		cached_abfd = open_bfd(prg_fname);
266 		if (!cached_abfd) {
267 			wpa_printf(MSG_INFO, "Failed to open bfd");
268 			return;
269 		}
270 	}
271 
272 	read_syms(cached_abfd);
273 	if (!syms) {
274 		wpa_printf(MSG_INFO, "Failed to read symbols");
275 		return;
276 	}
277 
278 	if (!start_offset_looked_up) {
279 		dl_iterate_phdr(callback, NULL);
280 		start_offset_looked_up = 1;
281 	}
282 }
283 
284 
wpa_trace_dump_funcname(const char * title,void * pc)285 void wpa_trace_dump_funcname(const char *title, void *pc)
286 {
287 	wpa_printf(MSG_INFO, "WPA_TRACE: %s: %p", title, pc);
288 	wpa_trace_bfd_init();
289 	wpa_trace_bfd_addr(pc);
290 }
291 
292 
wpa_trace_calling_func(const char * buf[],size_t len)293 size_t wpa_trace_calling_func(const char *buf[], size_t len)
294 {
295 	bfd *abfd;
296 	void *btrace_res[WPA_TRACE_LEN];
297 	int i, btrace_num;
298 	size_t pos = 0;
299 
300 	if (len == 0)
301 		return 0;
302 	if (len > WPA_TRACE_LEN)
303 		len = WPA_TRACE_LEN;
304 
305 	wpa_trace_bfd_init();
306 	abfd = cached_abfd;
307 	if (!abfd)
308 		return 0;
309 
310 	btrace_num = backtrace(btrace_res, len);
311 	if (btrace_num < 1)
312 		return 0;
313 
314 	for (i = 0; i < btrace_num; i++) {
315 		struct bfd_data data;
316 
317 		if (start_offset > (uintptr_t) btrace_res[i])
318 			continue;
319 		data.pc = (uintptr_t) ((u8 *) btrace_res[i] - start_offset);
320 		data.found = FALSE;
321 		bfd_map_over_sections(abfd, find_addr_sect, &data);
322 
323 		while (data.found) {
324 			if (data.function &&
325 			    (pos > 0 ||
326 			     os_strcmp(data.function, __func__) != 0)) {
327 				buf[pos++] = data.function;
328 				if (pos == len)
329 					return pos;
330 			}
331 
332 			data.found = bfd_find_inliner_info(abfd, &data.filename,
333 							   &data.function,
334 							   &data.line);
335 		}
336 	}
337 
338 	return pos;
339 }
340 
341 #else /* WPA_TRACE_BFD */
342 
343 #define wpa_trace_bfd_init() do { } while (0)
344 #define wpa_trace_bfd_addr(pc) do { } while (0)
345 #define wpa_trace_bfd_addr2func(pc) NULL
346 
347 #endif /* WPA_TRACE_BFD */
348 
wpa_trace_dump_func(const char * title,void ** btrace,int btrace_num)349 void wpa_trace_dump_func(const char *title, void **btrace, int btrace_num)
350 {
351 	char **sym;
352 	int i;
353 	enum { TRACE_HEAD, TRACE_RELEVANT, TRACE_TAIL } state;
354 
355 	wpa_trace_bfd_init();
356 	wpa_printf(MSG_INFO, "WPA_TRACE: %s - START", title);
357 	sym = backtrace_symbols(btrace, btrace_num);
358 	state = TRACE_HEAD;
359 	for (i = 0; i < btrace_num; i++) {
360 		const char *func = wpa_trace_bfd_addr2func(btrace[i]);
361 		if (state == TRACE_HEAD && func &&
362 		    (os_strcmp(func, "wpa_trace_add_ref_func") == 0 ||
363 		     os_strcmp(func, "wpa_trace_check_ref") == 0 ||
364 		     os_strcmp(func, "wpa_trace_show") == 0))
365 			continue;
366 		if (state == TRACE_TAIL && sym && sym[i] &&
367 		    os_strstr(sym[i], "__libc_start_main"))
368 			break;
369 		if (state == TRACE_HEAD)
370 			state = TRACE_RELEVANT;
371 		if (sym)
372 			wpa_printf(MSG_INFO, "[%d]: %s", i, sym[i]);
373 		else
374 			wpa_printf(MSG_INFO, "[%d]: ?? [%p]", i, btrace[i]);
375 		wpa_trace_bfd_addr(btrace[i]);
376 		if (state == TRACE_RELEVANT && func &&
377 		    os_strcmp(func, "main") == 0)
378 			state = TRACE_TAIL;
379 	}
380 	free(sym);
381 	wpa_printf(MSG_INFO, "WPA_TRACE: %s - END", title);
382 }
383 
384 
wpa_trace_show(const char * title)385 void wpa_trace_show(const char *title)
386 {
387 	struct info {
388 		WPA_TRACE_INFO
389 	} info;
390 	wpa_trace_record(&info);
391 	wpa_trace_dump(title, &info);
392 }
393 
394 
wpa_trace_add_ref_func(struct wpa_trace_ref * ref,const void * addr)395 void wpa_trace_add_ref_func(struct wpa_trace_ref *ref, const void *addr)
396 {
397 	if (addr == NULL)
398 		return;
399 	ref->addr = addr;
400 	wpa_trace_record(ref);
401 	dl_list_add(&active_references, &ref->list);
402 }
403 
404 
wpa_trace_check_ref(const void * addr)405 void wpa_trace_check_ref(const void *addr)
406 {
407 	struct wpa_trace_ref *ref;
408 	dl_list_for_each(ref, &active_references, struct wpa_trace_ref, list) {
409 		if (addr != ref->addr)
410 			continue;
411 		wpa_trace_show("Freeing referenced memory");
412 		wpa_trace_dump("Reference registration", ref);
413 		abort();
414 	}
415 }
416 
417 
wpa_trace_deinit(void)418 void wpa_trace_deinit(void)
419 {
420 #ifdef WPA_TRACE_BFD
421 	free(syms);
422 	syms = NULL;
423 #endif /* WPA_TRACE_BFD */
424 }
425 
426 #endif /* WPA_TRACE */
427