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
12 * See README and COPYING for more details.
22 static struct dl_list active_references =
23 { &active_references, &active_references };
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];
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)) {
44 prg_fname = strdup(fname);
48 static bfd * open_bfd(const char *fname)
53 abfd = bfd_openr(prg_fname, NULL);
55 wpa_printf(MSG_INFO, "bfd_openr failed");
59 if (bfd_check_format(abfd, bfd_archive)) {
60 wpa_printf(MSG_INFO, "bfd_check_format failed");
65 if (!bfd_check_format_matches(abfd, bfd_object, &matching)) {
66 wpa_printf(MSG_INFO, "bfd_check_format_matches failed");
76 static void read_syms(bfd *abfd)
78 long storage, symcount;
79 bfd_boolean dynamic = FALSE;
84 if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) {
85 wpa_printf(MSG_INFO, "No symbols");
89 storage = bfd_get_symtab_upper_bound(abfd);
91 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
95 wpa_printf(MSG_INFO, "Unknown symtab upper bound");
99 syms = malloc(storage);
101 wpa_printf(MSG_INFO, "Failed to allocate memory for symtab "
102 "(%ld bytes)", storage);
106 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
108 symcount = bfd_canonicalize_symtab(abfd, syms);
110 wpa_printf(MSG_INFO, "Failed to canonicalize %ssymtab",
111 dynamic ? "dynamic " : "");
116 wpa_printf(MSG_INFO, "BFD: Read %ld symbols (%ld bytes)",
124 const char *filename;
125 const char *function;
130 static void find_addr_sect(bfd *abfd, asection *section, void *obj)
132 struct bfd_data *data = obj;
139 if (!(bfd_get_section_vma(abfd, section)))
142 vma = bfd_get_section_vma(abfd, section);
146 size = bfd_get_section_size(section);
147 if (data->pc >= vma + size)
150 data->found = bfd_find_nearest_line(abfd, section, syms,
158 static void wpa_trace_bfd_addr(void *pc)
160 bfd *abfd = cached_abfd;
161 struct bfd_data data;
164 const char *filename;
169 data.pc = (bfd_vma) pc;
171 bfd_map_over_sections(abfd, find_addr_sect, &data);
178 aname = bfd_demangle(abfd, data.function,
179 DMGL_ANSI | DMGL_PARAMS);
180 name = aname ? aname : data.function;
181 filename = data.filename;
183 char *end = os_strrchr(filename, '/');
185 while (*filename && *filename == prg_fname[i] &&
191 wpa_printf(MSG_INFO, " %s() %s:%u",
192 name, filename, data.line);
195 data.found = bfd_find_inliner_info(abfd, &data.filename,
196 &data.function, &data.line);
197 } while (data.found);
201 static void wpa_trace_bfd_init(void)
207 wpa_printf(MSG_INFO, "BFD[%s]", prg_fname);
211 cached_abfd = open_bfd(prg_fname);
213 wpa_printf(MSG_INFO, "Failed to open bfd");
218 read_syms(cached_abfd);
220 wpa_printf(MSG_INFO, "Failed to read symbols");
225 #else /* WPA_TRACE_BFD */
227 #define wpa_trace_bfd_init() do { } while (0)
228 #define wpa_trace_bfd_addr(pc) do { } while (0)
230 #endif /* WPA_TRACE_BFD */
232 void wpa_trace_dump_func(const char *title, void **btrace, int btrace_num)
237 wpa_trace_bfd_init();
238 wpa_printf(MSG_INFO, "WPA_TRACE: %s - START", title);
239 sym = backtrace_symbols(btrace, btrace_num);
240 for (i = 0; i < btrace_num; i++) {
242 wpa_printf(MSG_INFO, "[%d]: %s", i, sym[i]);
244 wpa_printf(MSG_INFO, "[%d]: ?? [%p]", i, btrace[i]);
245 wpa_trace_bfd_addr(btrace[i]);
248 wpa_printf(MSG_INFO, "WPA_TRACE: %s - END", title);
252 void wpa_trace_show(const char *title)
257 wpa_trace_record(&info);
258 wpa_trace_dump(title, &info);
262 void wpa_trace_add_ref_func(struct wpa_trace_ref *ref, const void *addr)
267 wpa_trace_record(ref);
268 dl_list_add(&active_references, &ref->list);
272 void wpa_trace_check_ref(const void *addr)
274 struct wpa_trace_ref *ref;
275 dl_list_for_each(ref, &active_references, struct wpa_trace_ref, list) {
276 if (addr != ref->addr)
278 wpa_trace_show("Freeing referenced memory");
279 wpa_trace_dump("Reference registration", ref);
284 #endif /* WPA_TRACE */