trace.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. #ifdef WPA_TRACE_BFD
  9. #define _GNU_SOURCE
  10. #include <link.h>
  11. #endif /* WPA_TRACE_BCD */
  12. #include "includes.h"
  13. #include "common.h"
  14. #include "trace.h"
  15. #ifdef WPA_TRACE
  16. static struct dl_list active_references =
  17. { &active_references, &active_references };
  18. #ifdef WPA_TRACE_BFD
  19. #include <bfd.h>
  20. #define DMGL_PARAMS (1 << 0)
  21. #define DMGL_ANSI (1 << 1)
  22. static char *prg_fname = NULL;
  23. static bfd *cached_abfd = NULL;
  24. static asymbol **syms = NULL;
  25. static unsigned long start_offset;
  26. static int start_offset_looked_up;
  27. static int callback(struct dl_phdr_info *info, size_t size, void *data)
  28. {
  29. /*
  30. * dl_iterate_phdr(3):
  31. * "The first object visited by callback is the main program."
  32. */
  33. start_offset = info->dlpi_addr;
  34. /*
  35. * dl_iterate_phdr(3):
  36. * "The dl_iterate_phdr() function walks through the list of an
  37. * application's shared objects and calls the function callback
  38. * once for each object, until either all shared objects have
  39. * been processed or callback returns a nonzero value."
  40. */
  41. return 1;
  42. }
  43. static void get_prg_fname(void)
  44. {
  45. char exe[50], fname[512];
  46. int len;
  47. os_snprintf(exe, sizeof(exe) - 1, "/proc/%u/exe", getpid());
  48. len = readlink(exe, fname, sizeof(fname) - 1);
  49. if (len < 0 || len >= (int) sizeof(fname)) {
  50. wpa_printf(MSG_ERROR, "readlink: %s", strerror(errno));
  51. return;
  52. }
  53. fname[len] = '\0';
  54. prg_fname = strdup(fname);
  55. }
  56. static bfd * open_bfd(const char *fname)
  57. {
  58. bfd *abfd;
  59. char **matching;
  60. abfd = bfd_openr(prg_fname, NULL);
  61. if (abfd == NULL) {
  62. wpa_printf(MSG_INFO, "bfd_openr failed");
  63. return NULL;
  64. }
  65. if (bfd_check_format(abfd, bfd_archive)) {
  66. wpa_printf(MSG_INFO, "bfd_check_format failed");
  67. bfd_close(abfd);
  68. return NULL;
  69. }
  70. if (!bfd_check_format_matches(abfd, bfd_object, &matching)) {
  71. wpa_printf(MSG_INFO, "bfd_check_format_matches failed");
  72. free(matching);
  73. bfd_close(abfd);
  74. return NULL;
  75. }
  76. return abfd;
  77. }
  78. static void read_syms(bfd *abfd)
  79. {
  80. long storage, symcount;
  81. bfd_boolean dynamic = FALSE;
  82. if (syms)
  83. return;
  84. if (!(bfd_get_file_flags(abfd) & HAS_SYMS)) {
  85. wpa_printf(MSG_INFO, "No symbols");
  86. return;
  87. }
  88. storage = bfd_get_symtab_upper_bound(abfd);
  89. if (storage == 0) {
  90. storage = bfd_get_dynamic_symtab_upper_bound(abfd);
  91. dynamic = TRUE;
  92. }
  93. if (storage < 0) {
  94. wpa_printf(MSG_INFO, "Unknown symtab upper bound");
  95. return;
  96. }
  97. syms = malloc(storage);
  98. if (syms == NULL) {
  99. wpa_printf(MSG_INFO, "Failed to allocate memory for symtab "
  100. "(%ld bytes)", storage);
  101. return;
  102. }
  103. if (dynamic)
  104. symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
  105. else
  106. symcount = bfd_canonicalize_symtab(abfd, syms);
  107. if (symcount < 0) {
  108. wpa_printf(MSG_INFO, "Failed to canonicalize %ssymtab",
  109. dynamic ? "dynamic " : "");
  110. free(syms);
  111. syms = NULL;
  112. return;
  113. }
  114. }
  115. struct bfd_data {
  116. bfd_vma pc;
  117. bfd_boolean found;
  118. const char *filename;
  119. const char *function;
  120. unsigned int line;
  121. };
  122. static void find_addr_sect(bfd *abfd, asection *section, void *obj)
  123. {
  124. struct bfd_data *data = obj;
  125. bfd_vma vma;
  126. bfd_size_type size;
  127. if (data->found)
  128. return;
  129. if (!(bfd_get_section_vma(abfd, section)))
  130. return;
  131. vma = bfd_get_section_vma(abfd, section);
  132. if (data->pc < vma)
  133. return;
  134. size = bfd_get_section_size(section);
  135. if (data->pc >= vma + size)
  136. return;
  137. data->found = bfd_find_nearest_line(abfd, section, syms,
  138. data->pc - vma,
  139. &data->filename,
  140. &data->function,
  141. &data->line);
  142. }
  143. static void wpa_trace_bfd_addr(void *pc)
  144. {
  145. bfd *abfd = cached_abfd;
  146. struct bfd_data data;
  147. const char *name;
  148. char *aname = NULL;
  149. const char *filename;
  150. if (abfd == NULL)
  151. return;
  152. data.pc = (bfd_hostptr_t) (pc - start_offset);
  153. data.found = FALSE;
  154. bfd_map_over_sections(abfd, find_addr_sect, &data);
  155. if (!data.found)
  156. return;
  157. do {
  158. if (data.function)
  159. aname = bfd_demangle(abfd, data.function,
  160. DMGL_ANSI | DMGL_PARAMS);
  161. name = aname ? aname : data.function;
  162. filename = data.filename;
  163. if (filename) {
  164. char *end = os_strrchr(filename, '/');
  165. int i = 0;
  166. while (*filename && *filename == prg_fname[i] &&
  167. filename <= end) {
  168. filename++;
  169. i++;
  170. }
  171. }
  172. wpa_printf(MSG_INFO, " %s() %s:%u",
  173. name, filename, data.line);
  174. free(aname);
  175. aname = NULL;
  176. data.found = bfd_find_inliner_info(abfd, &data.filename,
  177. &data.function, &data.line);
  178. } while (data.found);
  179. }
  180. static const char * wpa_trace_bfd_addr2func(void *pc)
  181. {
  182. bfd *abfd = cached_abfd;
  183. struct bfd_data data;
  184. if (abfd == NULL)
  185. return NULL;
  186. data.pc = (bfd_hostptr_t) (pc - start_offset);
  187. data.found = FALSE;
  188. bfd_map_over_sections(abfd, find_addr_sect, &data);
  189. if (!data.found)
  190. return NULL;
  191. return data.function;
  192. }
  193. static void wpa_trace_bfd_init(void)
  194. {
  195. if (!prg_fname) {
  196. get_prg_fname();
  197. if (!prg_fname)
  198. return;
  199. }
  200. if (!cached_abfd) {
  201. cached_abfd = open_bfd(prg_fname);
  202. if (!cached_abfd) {
  203. wpa_printf(MSG_INFO, "Failed to open bfd");
  204. return;
  205. }
  206. }
  207. read_syms(cached_abfd);
  208. if (!syms) {
  209. wpa_printf(MSG_INFO, "Failed to read symbols");
  210. return;
  211. }
  212. if (!start_offset_looked_up) {
  213. dl_iterate_phdr(callback, NULL);
  214. start_offset_looked_up = 1;
  215. }
  216. }
  217. void wpa_trace_dump_funcname(const char *title, void *pc)
  218. {
  219. wpa_printf(MSG_INFO, "WPA_TRACE: %s: %p", title, pc);
  220. wpa_trace_bfd_init();
  221. wpa_trace_bfd_addr(pc);
  222. }
  223. size_t wpa_trace_calling_func(const char *buf[], size_t len)
  224. {
  225. bfd *abfd;
  226. void *btrace_res[WPA_TRACE_LEN];
  227. int i, btrace_num;
  228. size_t pos = 0;
  229. if (len == 0)
  230. return 0;
  231. if (len > WPA_TRACE_LEN)
  232. len = WPA_TRACE_LEN;
  233. wpa_trace_bfd_init();
  234. abfd = cached_abfd;
  235. if (!abfd)
  236. return 0;
  237. btrace_num = backtrace(btrace_res, len);
  238. if (btrace_num < 1)
  239. return 0;
  240. for (i = 0; i < btrace_num; i++) {
  241. struct bfd_data data;
  242. data.pc = (bfd_hostptr_t) (btrace_res[i] - start_offset);
  243. data.found = FALSE;
  244. bfd_map_over_sections(abfd, find_addr_sect, &data);
  245. while (data.found) {
  246. if (data.function &&
  247. (pos > 0 ||
  248. os_strcmp(data.function, __func__) != 0)) {
  249. buf[pos++] = data.function;
  250. if (pos == len)
  251. return pos;
  252. }
  253. data.found = bfd_find_inliner_info(abfd, &data.filename,
  254. &data.function,
  255. &data.line);
  256. }
  257. }
  258. return pos;
  259. }
  260. #else /* WPA_TRACE_BFD */
  261. #define wpa_trace_bfd_init() do { } while (0)
  262. #define wpa_trace_bfd_addr(pc) do { } while (0)
  263. #define wpa_trace_bfd_addr2func(pc) NULL
  264. #endif /* WPA_TRACE_BFD */
  265. void wpa_trace_dump_func(const char *title, void **btrace, int btrace_num)
  266. {
  267. char **sym;
  268. int i;
  269. enum { TRACE_HEAD, TRACE_RELEVANT, TRACE_TAIL } state;
  270. wpa_trace_bfd_init();
  271. wpa_printf(MSG_INFO, "WPA_TRACE: %s - START", title);
  272. sym = backtrace_symbols(btrace, btrace_num);
  273. state = TRACE_HEAD;
  274. for (i = 0; i < btrace_num; i++) {
  275. const char *func = wpa_trace_bfd_addr2func(btrace[i]);
  276. if (state == TRACE_HEAD && func &&
  277. (os_strcmp(func, "wpa_trace_add_ref_func") == 0 ||
  278. os_strcmp(func, "wpa_trace_check_ref") == 0 ||
  279. os_strcmp(func, "wpa_trace_show") == 0))
  280. continue;
  281. if (state == TRACE_TAIL && sym && sym[i] &&
  282. os_strstr(sym[i], "__libc_start_main"))
  283. break;
  284. if (state == TRACE_HEAD)
  285. state = TRACE_RELEVANT;
  286. if (sym)
  287. wpa_printf(MSG_INFO, "[%d]: %s", i, sym[i]);
  288. else
  289. wpa_printf(MSG_INFO, "[%d]: ?? [%p]", i, btrace[i]);
  290. wpa_trace_bfd_addr(btrace[i]);
  291. if (state == TRACE_RELEVANT && func &&
  292. os_strcmp(func, "main") == 0)
  293. state = TRACE_TAIL;
  294. }
  295. free(sym);
  296. wpa_printf(MSG_INFO, "WPA_TRACE: %s - END", title);
  297. }
  298. void wpa_trace_show(const char *title)
  299. {
  300. struct info {
  301. WPA_TRACE_INFO
  302. } info;
  303. wpa_trace_record(&info);
  304. wpa_trace_dump(title, &info);
  305. }
  306. void wpa_trace_add_ref_func(struct wpa_trace_ref *ref, const void *addr)
  307. {
  308. if (addr == NULL)
  309. return;
  310. ref->addr = addr;
  311. wpa_trace_record(ref);
  312. dl_list_add(&active_references, &ref->list);
  313. }
  314. void wpa_trace_check_ref(const void *addr)
  315. {
  316. struct wpa_trace_ref *ref;
  317. dl_list_for_each(ref, &active_references, struct wpa_trace_ref, list) {
  318. if (addr != ref->addr)
  319. continue;
  320. wpa_trace_show("Freeing referenced memory");
  321. wpa_trace_dump("Reference registration", ref);
  322. abort();
  323. }
  324. }
  325. void wpa_trace_deinit(void)
  326. {
  327. #ifdef WPA_TRACE_BFD
  328. free(syms);
  329. syms = NULL;
  330. #endif /* WPA_TRACE_BFD */
  331. }
  332. #endif /* WPA_TRACE */