nl80211: Fix a typo
[hostap-gosc2009.git] / src / utils / trace.c
blob17ce22442a4c89e959db63cd02cdc57e44e41a31
1 /*
2 * Backtrace debugging
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "trace.h"
20 #ifdef WPA_TRACE
22 static struct dl_list active_references =
23 { &active_references, &active_references };
25 #ifdef WPA_TRACE_BFD
26 #include <bfd.h>
27 #include <demangle.h>
29 static char *prg_fname = NULL;
30 static bfd *cached_abfd = NULL;
31 static asymbol **syms = NULL;
33 static void get_prg_fname(void)
35 char exe[50], fname[512];
36 int len;
37 os_snprintf(exe, sizeof(exe) - 1, "/proc/%u/exe", getpid());
38 len = readlink(exe, fname, sizeof(fname) - 1);
39 if (len < 0 || len >= (int) sizeof(fname)) {
40 perror("readlink");
41 return;
43 fname[len] = '\0';
44 prg_fname = strdup(fname);
48 static bfd * open_bfd(const char *fname)
50 bfd *abfd;
51 char **matching;
53 abfd = bfd_openr(prg_fname, NULL);
54 if (abfd == NULL) {
55 wpa_printf(MSG_INFO, "bfd_openr failed");
56 return NULL;
59 if (bfd_check_format(abfd, bfd_archive)) {
60 wpa_printf(MSG_INFO, "bfd_check_format failed");
61 bfd_close(abfd);
62 return NULL;
65 if (!bfd_check_format_matches(abfd, bfd_object, &matching)) {
66 wpa_printf(MSG_INFO, "bfd_check_format_matches failed");
67 free(matching);
68 bfd_close(abfd);
69 return NULL;
72 return abfd;
76 static void read_syms(bfd *abfd)
78 long storage, symcount;
79 bfd_boolean dynamic = FALSE;
81 if (syms)
82 return;
84 if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) {
85 wpa_printf(MSG_INFO, "No symbols");
86 return;
89 storage = bfd_get_symtab_upper_bound(abfd);
90 if (storage == 0) {
91 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
92 dynamic = TRUE;
94 if (storage < 0) {
95 wpa_printf(MSG_INFO, "Unknown symtab upper bound");
96 return;
99 syms = malloc(storage);
100 if (syms == NULL) {
101 wpa_printf(MSG_INFO, "Failed to allocate memory for symtab "
102 "(%ld bytes)", storage);
103 return;
105 if (dynamic)
106 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
107 else
108 symcount = bfd_canonicalize_symtab(abfd, syms);
109 if (symcount < 0) {
110 wpa_printf(MSG_INFO, "Failed to canonicalize %ssymtab",
111 dynamic ? "dynamic " : "");
112 free(syms);
113 syms = NULL;
114 return;
119 struct bfd_data {
120 bfd_vma pc;
121 bfd_boolean found;
122 const char *filename;
123 const char *function;
124 unsigned int line;
128 static void find_addr_sect(bfd *abfd, asection *section, void *obj)
130 struct bfd_data *data = obj;
131 bfd_vma vma;
132 bfd_size_type size;
134 if (data->found)
135 return;
137 if (!(bfd_get_section_vma(abfd, section)))
138 return;
140 vma = bfd_get_section_vma(abfd, section);
141 if (data->pc < vma)
142 return;
144 size = bfd_get_section_size(section);
145 if (data->pc >= vma + size)
146 return;
148 data->found = bfd_find_nearest_line(abfd, section, syms,
149 data->pc - vma,
150 &data->filename,
151 &data->function,
152 &data->line);
156 static void wpa_trace_bfd_addr(void *pc)
158 bfd *abfd = cached_abfd;
159 struct bfd_data data;
160 const char *name;
161 char *aname = NULL;
162 const char *filename;
164 if (abfd == NULL)
165 return;
167 data.pc = (bfd_vma) pc;
168 data.found = FALSE;
169 bfd_map_over_sections(abfd, find_addr_sect, &data);
171 if (!data.found)
172 return;
174 do {
175 if (data.function)
176 aname = bfd_demangle(abfd, data.function,
177 DMGL_ANSI | DMGL_PARAMS);
178 name = aname ? aname : data.function;
179 filename = data.filename;
180 if (filename) {
181 char *end = os_strrchr(filename, '/');
182 int i = 0;
183 while (*filename && *filename == prg_fname[i] &&
184 filename <= end) {
185 filename++;
186 i++;
189 wpa_printf(MSG_INFO, " %s() %s:%u",
190 name, filename, data.line);
191 free(aname);
193 data.found = bfd_find_inliner_info(abfd, &data.filename,
194 &data.function, &data.line);
195 } while (data.found);
199 static const char * wpa_trace_bfd_addr2func(void *pc)
201 bfd *abfd = cached_abfd;
202 struct bfd_data data;
204 if (abfd == NULL)
205 return NULL;
207 data.pc = (bfd_vma) pc;
208 data.found = FALSE;
209 bfd_map_over_sections(abfd, find_addr_sect, &data);
211 if (!data.found)
212 return NULL;
214 return data.function;
218 static void wpa_trace_bfd_init(void)
220 if (!prg_fname) {
221 get_prg_fname();
222 if (!prg_fname)
223 return;
226 if (!cached_abfd) {
227 cached_abfd = open_bfd(prg_fname);
228 if (!cached_abfd) {
229 wpa_printf(MSG_INFO, "Failed to open bfd");
230 return;
234 read_syms(cached_abfd);
235 if (!syms) {
236 wpa_printf(MSG_INFO, "Failed to read symbols");
237 return;
242 void wpa_trace_dump_funcname(const char *title, void *pc)
244 wpa_printf(MSG_INFO, "WPA_TRACE: %s: %p", title, pc);
245 wpa_trace_bfd_init();
246 wpa_trace_bfd_addr(pc);
249 #else /* WPA_TRACE_BFD */
251 #define wpa_trace_bfd_init() do { } while (0)
252 #define wpa_trace_bfd_addr(pc) do { } while (0)
253 #define wpa_trace_bfd_addr2func(pc) NULL
255 #endif /* WPA_TRACE_BFD */
257 void wpa_trace_dump_func(const char *title, void **btrace, int btrace_num)
259 char **sym;
260 int i;
261 enum { TRACE_HEAD, TRACE_RELEVANT, TRACE_TAIL } state;
263 wpa_trace_bfd_init();
264 wpa_printf(MSG_INFO, "WPA_TRACE: %s - START", title);
265 sym = backtrace_symbols(btrace, btrace_num);
266 state = TRACE_HEAD;
267 for (i = 0; i < btrace_num; i++) {
268 const char *func = wpa_trace_bfd_addr2func(btrace[i]);
269 if (state == TRACE_HEAD && func &&
270 (os_strcmp(func, "wpa_trace_add_ref_func") == 0 ||
271 os_strcmp(func, "wpa_trace_check_ref") == 0 ||
272 os_strcmp(func, "wpa_trace_show") == 0))
273 continue;
274 if (state == TRACE_TAIL && sym && sym[i] &&
275 os_strstr(sym[i], "__libc_start_main"))
276 break;
277 if (state == TRACE_HEAD)
278 state = TRACE_RELEVANT;
279 if (sym)
280 wpa_printf(MSG_INFO, "[%d]: %s", i, sym[i]);
281 else
282 wpa_printf(MSG_INFO, "[%d]: ?? [%p]", i, btrace[i]);
283 wpa_trace_bfd_addr(btrace[i]);
284 if (state == TRACE_RELEVANT && func &&
285 os_strcmp(func, "main") == 0)
286 state = TRACE_TAIL;
288 free(sym);
289 wpa_printf(MSG_INFO, "WPA_TRACE: %s - END", title);
293 void wpa_trace_show(const char *title)
295 struct info {
296 WPA_TRACE_INFO
297 } info;
298 wpa_trace_record(&info);
299 wpa_trace_dump(title, &info);
303 void wpa_trace_add_ref_func(struct wpa_trace_ref *ref, const void *addr)
305 if (addr == NULL)
306 return;
307 ref->addr = addr;
308 wpa_trace_record(ref);
309 dl_list_add(&active_references, &ref->list);
313 void wpa_trace_check_ref(const void *addr)
315 struct wpa_trace_ref *ref;
316 dl_list_for_each(ref, &active_references, struct wpa_trace_ref, list) {
317 if (addr != ref->addr)
318 continue;
319 wpa_trace_show("Freeing referenced memory");
320 wpa_trace_dump("Reference registration", ref);
321 abort();
325 #endif /* WPA_TRACE */