addr2line.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /* addr2line.c -- convert addresses to line number and function name
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
  18. Usage:
  19. addr2line [options] addr addr ...
  20. or
  21. addr2line [options]
  22. both forms write results to stdout, the second form reads addresses
  23. to be converted from stdin. */
  24. #include "sysdep.h"
  25. #include "bfd.h"
  26. #include "getopt.h"
  27. #include "libiberty.h"
  28. #include "demangle.h"
  29. #include "bucomm.h"
  30. #include "elf-bfd.h"
  31. #include "safe-ctype.h"
  32. static bool unwind_inlines; /* -i, unwind inlined functions. */
  33. static bool with_addresses; /* -a, show addresses. */
  34. static bool with_functions; /* -f, show function names. */
  35. static bool do_demangle; /* -C, demangle names. */
  36. static bool pretty_print; /* -p, print on one line. */
  37. static bool base_names; /* -s, strip directory names. */
  38. /* Flags passed to the name demangler. */
  39. static int demangle_flags = DMGL_PARAMS | DMGL_ANSI;
  40. static int naddr; /* Number of addresses to process. */
  41. static char **addr; /* Hex addresses to process. */
  42. static long symcount;
  43. static asymbol **syms; /* Symbol table. */
  44. static struct option long_options[] =
  45. {
  46. {"addresses", no_argument, NULL, 'a'},
  47. {"basenames", no_argument, NULL, 's'},
  48. {"demangle", optional_argument, NULL, 'C'},
  49. {"exe", required_argument, NULL, 'e'},
  50. {"functions", no_argument, NULL, 'f'},
  51. {"inlines", no_argument, NULL, 'i'},
  52. {"pretty-print", no_argument, NULL, 'p'},
  53. {"recurse-limit", no_argument, NULL, 'R'},
  54. {"recursion-limit", no_argument, NULL, 'R'},
  55. {"no-recurse-limit", no_argument, NULL, 'r'},
  56. {"no-recursion-limit", no_argument, NULL, 'r'},
  57. {"section", required_argument, NULL, 'j'},
  58. {"target", required_argument, NULL, 'b'},
  59. {"help", no_argument, NULL, 'H'},
  60. {"version", no_argument, NULL, 'V'},
  61. {0, no_argument, 0, 0}
  62. };
  63. static void usage (FILE *, int);
  64. static void slurp_symtab (bfd *);
  65. static void find_address_in_section (bfd *, asection *, void *);
  66. static void find_offset_in_section (bfd *, asection *);
  67. static void translate_addresses (bfd *, asection *);
  68. /* Print a usage message to STREAM and exit with STATUS. */
  69. static void
  70. usage (FILE *stream, int status)
  71. {
  72. fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name);
  73. fprintf (stream, _(" Convert addresses into line number/file name pairs.\n"));
  74. fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
  75. fprintf (stream, _(" The options are:\n\
  76. @<file> Read options from <file>\n\
  77. -a --addresses Show addresses\n\
  78. -b --target=<bfdname> Set the binary file format\n\
  79. -e --exe=<executable> Set the input file name (default is a.out)\n\
  80. -i --inlines Unwind inlined functions\n\
  81. -j --section=<name> Read section-relative offsets instead of addresses\n\
  82. -p --pretty-print Make the output easier to read for humans\n\
  83. -s --basenames Strip directory names\n\
  84. -f --functions Show function names\n\
  85. -C --demangle[=style] Demangle function names\n\
  86. -R --recurse-limit Enable a limit on recursion whilst demangling. [Default]\n\
  87. -r --no-recurse-limit Disable a limit on recursion whilst demangling\n\
  88. -h --help Display this information\n\
  89. -v --version Display the program's version\n\
  90. \n"));
  91. list_supported_targets (program_name, stream);
  92. if (REPORT_BUGS_TO[0] && status == 0)
  93. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  94. exit (status);
  95. }
  96. /* Read in the symbol table. */
  97. static void
  98. slurp_symtab (bfd *abfd)
  99. {
  100. long storage;
  101. bool dynamic = false;
  102. if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
  103. return;
  104. storage = bfd_get_symtab_upper_bound (abfd);
  105. if (storage == 0)
  106. {
  107. storage = bfd_get_dynamic_symtab_upper_bound (abfd);
  108. dynamic = true;
  109. }
  110. if (storage < 0)
  111. bfd_fatal (bfd_get_filename (abfd));
  112. syms = (asymbol **) xmalloc (storage);
  113. if (dynamic)
  114. symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
  115. else
  116. symcount = bfd_canonicalize_symtab (abfd, syms);
  117. if (symcount < 0)
  118. bfd_fatal (bfd_get_filename (abfd));
  119. /* If there are no symbols left after canonicalization and
  120. we have not tried the dynamic symbols then give them a go. */
  121. if (symcount == 0
  122. && ! dynamic
  123. && (storage = bfd_get_dynamic_symtab_upper_bound (abfd)) > 0)
  124. {
  125. free (syms);
  126. syms = xmalloc (storage);
  127. symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
  128. }
  129. /* PR 17512: file: 2a1d3b5b.
  130. Do not pretend that we have some symbols when we don't. */
  131. if (symcount <= 0)
  132. {
  133. free (syms);
  134. syms = NULL;
  135. }
  136. }
  137. /* These global variables are used to pass information between
  138. translate_addresses and find_address_in_section. */
  139. static bfd_vma pc;
  140. static const char *filename;
  141. static const char *functionname;
  142. static unsigned int line;
  143. static unsigned int discriminator;
  144. static bool found;
  145. /* Look for an address in a section. This is called via
  146. bfd_map_over_sections. */
  147. static void
  148. find_address_in_section (bfd *abfd, asection *section,
  149. void *data ATTRIBUTE_UNUSED)
  150. {
  151. bfd_vma vma;
  152. bfd_size_type size;
  153. if (found)
  154. return;
  155. if ((bfd_section_flags (section) & SEC_ALLOC) == 0)
  156. return;
  157. vma = bfd_section_vma (section);
  158. if (pc < vma)
  159. return;
  160. size = bfd_section_size (section);
  161. if (pc >= vma + size)
  162. return;
  163. found = bfd_find_nearest_line_discriminator (abfd, section, syms, pc - vma,
  164. &filename, &functionname,
  165. &line, &discriminator);
  166. }
  167. /* Look for an offset in a section. This is directly called. */
  168. static void
  169. find_offset_in_section (bfd *abfd, asection *section)
  170. {
  171. bfd_size_type size;
  172. if (found)
  173. return;
  174. if ((bfd_section_flags (section) & SEC_ALLOC) == 0)
  175. return;
  176. size = bfd_section_size (section);
  177. if (pc >= size)
  178. return;
  179. found = bfd_find_nearest_line_discriminator (abfd, section, syms, pc,
  180. &filename, &functionname,
  181. &line, &discriminator);
  182. }
  183. /* Lookup a symbol with offset in symbol table. */
  184. static bfd_vma
  185. lookup_symbol (bfd *abfd, char *sym, size_t offset)
  186. {
  187. long i;
  188. for (i = 0; i < symcount; i++)
  189. {
  190. if (!strcmp (syms[i]->name, sym))
  191. return syms[i]->value + offset + bfd_asymbol_section (syms[i])->vma;
  192. }
  193. /* Try again mangled */
  194. for (i = 0; i < symcount; i++)
  195. {
  196. char *d = bfd_demangle (abfd, syms[i]->name, demangle_flags);
  197. bool match = d && !strcmp (d, sym);
  198. free (d);
  199. if (match)
  200. return syms[i]->value + offset + bfd_asymbol_section (syms[i])->vma;
  201. }
  202. return 0;
  203. }
  204. /* Split an symbol+offset expression. adr is modified. */
  205. static bool
  206. is_symbol (char *adr, char **symp, size_t *offset)
  207. {
  208. char *end;
  209. while (ISSPACE (*adr))
  210. adr++;
  211. if (ISDIGIT (*adr) || *adr == 0)
  212. return false;
  213. /* Could be either symbol or hex number. Check if it has +. */
  214. if (TOUPPER(*adr) >= 'A' && TOUPPER(*adr) <= 'F' && !strchr (adr, '+'))
  215. return false;
  216. *symp = adr;
  217. while (*adr && !ISSPACE (*adr) && *adr != '+')
  218. adr++;
  219. end = adr;
  220. while (ISSPACE (*adr))
  221. adr++;
  222. *offset = 0;
  223. if (*adr == '+')
  224. {
  225. adr++;
  226. *offset = strtoul(adr, NULL, 0);
  227. }
  228. *end = 0;
  229. return true;
  230. }
  231. /* Read hexadecimal or symbolic with offset addresses from stdin, translate into
  232. file_name:line_number and optionally function name. */
  233. static void
  234. translate_addresses (bfd *abfd, asection *section)
  235. {
  236. int read_stdin = (naddr == 0);
  237. char *adr;
  238. char addr_hex[100];
  239. char *symp;
  240. size_t offset;
  241. for (;;)
  242. {
  243. if (read_stdin)
  244. {
  245. if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
  246. break;
  247. adr = addr_hex;
  248. }
  249. else
  250. {
  251. if (naddr <= 0)
  252. break;
  253. --naddr;
  254. adr = *addr++;
  255. }
  256. if (is_symbol (adr, &symp, &offset))
  257. pc = lookup_symbol (abfd, symp, offset);
  258. else
  259. pc = bfd_scan_vma (adr, NULL, 16);
  260. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  261. {
  262. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  263. bfd_vma sign = (bfd_vma) 1 << (bed->s->arch_size - 1);
  264. pc &= (sign << 1) - 1;
  265. if (bed->sign_extend_vma)
  266. pc = (pc ^ sign) - sign;
  267. }
  268. if (with_addresses)
  269. {
  270. printf ("0x");
  271. bfd_printf_vma (abfd, pc);
  272. if (pretty_print)
  273. printf (": ");
  274. else
  275. printf ("\n");
  276. }
  277. found = false;
  278. if (section)
  279. find_offset_in_section (abfd, section);
  280. else
  281. bfd_map_over_sections (abfd, find_address_in_section, NULL);
  282. if (! found)
  283. {
  284. if (with_functions)
  285. {
  286. if (pretty_print)
  287. printf ("?? ");
  288. else
  289. printf ("??\n");
  290. }
  291. printf ("??:0\n");
  292. }
  293. else
  294. {
  295. while (1)
  296. {
  297. if (with_functions)
  298. {
  299. const char *name;
  300. char *alloc = NULL;
  301. name = functionname;
  302. if (name == NULL || *name == '\0')
  303. name = "??";
  304. else if (do_demangle)
  305. {
  306. alloc = bfd_demangle (abfd, name, demangle_flags);
  307. if (alloc != NULL)
  308. name = alloc;
  309. }
  310. printf ("%s", name);
  311. if (pretty_print)
  312. /* Note for translators: This printf is used to join the
  313. function name just printed above to the line number/
  314. file name pair that is about to be printed below. Eg:
  315. foo at 123:bar.c */
  316. printf (_(" at "));
  317. else
  318. printf ("\n");
  319. free (alloc);
  320. }
  321. if (base_names && filename != NULL)
  322. {
  323. char *h;
  324. h = strrchr (filename, '/');
  325. if (h != NULL)
  326. filename = h + 1;
  327. }
  328. printf ("%s:", filename ? filename : "??");
  329. if (line != 0)
  330. {
  331. if (discriminator != 0)
  332. printf ("%u (discriminator %u)\n", line, discriminator);
  333. else
  334. printf ("%u\n", line);
  335. }
  336. else
  337. printf ("?\n");
  338. if (!unwind_inlines)
  339. found = false;
  340. else
  341. found = bfd_find_inliner_info (abfd, &filename, &functionname,
  342. &line);
  343. if (! found)
  344. break;
  345. if (pretty_print)
  346. /* Note for translators: This printf is used to join the
  347. line number/file name pair that has just been printed with
  348. the line number/file name pair that is going to be printed
  349. by the next iteration of the while loop. Eg:
  350. 123:bar.c (inlined by) 456:main.c */
  351. printf (_(" (inlined by) "));
  352. }
  353. }
  354. /* fflush() is essential for using this command as a server
  355. child process that reads addresses from a pipe and responds
  356. with line number information, processing one address at a
  357. time. */
  358. fflush (stdout);
  359. }
  360. }
  361. /* Process a file. Returns an exit value for main(). */
  362. static int
  363. process_file (const char *file_name, const char *section_name,
  364. const char *target)
  365. {
  366. bfd *abfd;
  367. asection *section;
  368. char **matching;
  369. if (get_file_size (file_name) < 1)
  370. return 1;
  371. abfd = bfd_openr (file_name, target);
  372. if (abfd == NULL)
  373. bfd_fatal (file_name);
  374. /* Decompress sections. */
  375. abfd->flags |= BFD_DECOMPRESS;
  376. if (bfd_check_format (abfd, bfd_archive))
  377. fatal (_("%s: cannot get addresses from archive"), file_name);
  378. if (! bfd_check_format_matches (abfd, bfd_object, &matching))
  379. {
  380. bfd_nonfatal (bfd_get_filename (abfd));
  381. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  382. {
  383. list_matching_formats (matching);
  384. free (matching);
  385. }
  386. xexit (1);
  387. }
  388. if (section_name != NULL)
  389. {
  390. section = bfd_get_section_by_name (abfd, section_name);
  391. if (section == NULL)
  392. fatal (_("%s: cannot find section %s"), file_name, section_name);
  393. }
  394. else
  395. section = NULL;
  396. slurp_symtab (abfd);
  397. translate_addresses (abfd, section);
  398. free (syms);
  399. syms = NULL;
  400. bfd_close (abfd);
  401. return 0;
  402. }
  403. int
  404. main (int argc, char **argv)
  405. {
  406. const char *file_name;
  407. const char *section_name;
  408. char *target;
  409. int c;
  410. #ifdef HAVE_LC_MESSAGES
  411. setlocale (LC_MESSAGES, "");
  412. #endif
  413. setlocale (LC_CTYPE, "");
  414. bindtextdomain (PACKAGE, LOCALEDIR);
  415. textdomain (PACKAGE);
  416. program_name = *argv;
  417. xmalloc_set_program_name (program_name);
  418. bfd_set_error_program_name (program_name);
  419. expandargv (&argc, &argv);
  420. if (bfd_init () != BFD_INIT_MAGIC)
  421. fatal (_("fatal error: libbfd ABI mismatch"));
  422. set_default_bfd_target ();
  423. file_name = NULL;
  424. section_name = NULL;
  425. target = NULL;
  426. while ((c = getopt_long (argc, argv, "ab:Ce:rRsfHhij:pVv", long_options, (int *) 0))
  427. != EOF)
  428. {
  429. switch (c)
  430. {
  431. case 0:
  432. break; /* We've been given a long option. */
  433. case 'a':
  434. with_addresses = true;
  435. break;
  436. case 'b':
  437. target = optarg;
  438. break;
  439. case 'C':
  440. do_demangle = true;
  441. if (optarg != NULL)
  442. {
  443. enum demangling_styles style;
  444. style = cplus_demangle_name_to_style (optarg);
  445. if (style == unknown_demangling)
  446. fatal (_("unknown demangling style `%s'"),
  447. optarg);
  448. cplus_demangle_set_style (style);
  449. }
  450. break;
  451. case 'r':
  452. demangle_flags |= DMGL_NO_RECURSE_LIMIT;
  453. break;
  454. case 'R':
  455. demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
  456. break;
  457. case 'e':
  458. file_name = optarg;
  459. break;
  460. case 's':
  461. base_names = true;
  462. break;
  463. case 'f':
  464. with_functions = true;
  465. break;
  466. case 'p':
  467. pretty_print = true;
  468. break;
  469. case 'v':
  470. case 'V':
  471. print_version ("addr2line");
  472. break;
  473. case 'h':
  474. case 'H':
  475. usage (stdout, 0);
  476. break;
  477. case 'i':
  478. unwind_inlines = true;
  479. break;
  480. case 'j':
  481. section_name = optarg;
  482. break;
  483. default:
  484. usage (stderr, 1);
  485. break;
  486. }
  487. }
  488. if (file_name == NULL)
  489. file_name = "a.out";
  490. addr = argv + optind;
  491. naddr = argc - optind;
  492. return process_file (file_name, section_name, target);
  493. }