addr2line.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* addr2line.c -- convert addresses to line number and function name
  2. Copyright (C) 1997-2015 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. static bfd_boolean unwind_inlines; /* -i, unwind inlined functions. */
  32. static bfd_boolean with_addresses; /* -a, show addresses. */
  33. static bfd_boolean with_functions; /* -f, show function names. */
  34. static bfd_boolean do_demangle; /* -C, demangle names. */
  35. static bfd_boolean pretty_print; /* -p, print on one line. */
  36. static bfd_boolean base_names; /* -s, strip directory names. */
  37. static int naddr; /* Number of addresses to process. */
  38. static char **addr; /* Hex addresses to process. */
  39. static asymbol **syms; /* Symbol table. */
  40. static struct option long_options[] =
  41. {
  42. {"addresses", no_argument, NULL, 'a'},
  43. {"basenames", no_argument, NULL, 's'},
  44. {"demangle", optional_argument, NULL, 'C'},
  45. {"exe", required_argument, NULL, 'e'},
  46. {"functions", no_argument, NULL, 'f'},
  47. {"inlines", no_argument, NULL, 'i'},
  48. {"pretty-print", no_argument, NULL, 'p'},
  49. {"section", required_argument, NULL, 'j'},
  50. {"target", required_argument, NULL, 'b'},
  51. {"help", no_argument, NULL, 'H'},
  52. {"version", no_argument, NULL, 'V'},
  53. {0, no_argument, 0, 0}
  54. };
  55. static void usage (FILE *, int);
  56. static void slurp_symtab (bfd *);
  57. static void find_address_in_section (bfd *, asection *, void *);
  58. static void find_offset_in_section (bfd *, asection *);
  59. static void translate_addresses (bfd *, asection *);
  60. /* Print a usage message to STREAM and exit with STATUS. */
  61. static void
  62. usage (FILE *stream, int status)
  63. {
  64. fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name);
  65. fprintf (stream, _(" Convert addresses into line number/file name pairs.\n"));
  66. fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
  67. fprintf (stream, _(" The options are:\n\
  68. @<file> Read options from <file>\n\
  69. -a --addresses Show addresses\n\
  70. -b --target=<bfdname> Set the binary file format\n\
  71. -e --exe=<executable> Set the input file name (default is a.out)\n\
  72. -i --inlines Unwind inlined functions\n\
  73. -j --section=<name> Read section-relative offsets instead of addresses\n\
  74. -p --pretty-print Make the output easier to read for humans\n\
  75. -s --basenames Strip directory names\n\
  76. -f --functions Show function names\n\
  77. -C --demangle[=style] Demangle function names\n\
  78. -h --help Display this information\n\
  79. -v --version Display the program's version\n\
  80. \n"));
  81. list_supported_targets (program_name, stream);
  82. if (REPORT_BUGS_TO[0] && status == 0)
  83. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  84. exit (status);
  85. }
  86. /* Read in the symbol table. */
  87. static void
  88. slurp_symtab (bfd *abfd)
  89. {
  90. long storage;
  91. long symcount;
  92. bfd_boolean dynamic = FALSE;
  93. if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
  94. return;
  95. storage = bfd_get_symtab_upper_bound (abfd);
  96. if (storage == 0)
  97. {
  98. storage = bfd_get_dynamic_symtab_upper_bound (abfd);
  99. dynamic = TRUE;
  100. }
  101. if (storage < 0)
  102. bfd_fatal (bfd_get_filename (abfd));
  103. syms = (asymbol **) xmalloc (storage);
  104. if (dynamic)
  105. symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
  106. else
  107. symcount = bfd_canonicalize_symtab (abfd, syms);
  108. if (symcount < 0)
  109. bfd_fatal (bfd_get_filename (abfd));
  110. /* If there are no symbols left after canonicalization and
  111. we have not tried the dynamic symbols then give them a go. */
  112. if (symcount == 0
  113. && ! dynamic
  114. && (storage = bfd_get_dynamic_symtab_upper_bound (abfd)) > 0)
  115. {
  116. free (syms);
  117. syms = xmalloc (storage);
  118. symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
  119. }
  120. /* PR 17512: file: 2a1d3b5b.
  121. Do not pretend that we have some symbols when we don't. */
  122. if (symcount <= 0)
  123. {
  124. free (syms);
  125. syms = NULL;
  126. }
  127. }
  128. /* These global variables are used to pass information between
  129. translate_addresses and find_address_in_section. */
  130. static bfd_vma pc;
  131. static const char *filename;
  132. static const char *functionname;
  133. static unsigned int line;
  134. static unsigned int discriminator;
  135. static bfd_boolean found;
  136. /* Look for an address in a section. This is called via
  137. bfd_map_over_sections. */
  138. static void
  139. find_address_in_section (bfd *abfd, asection *section,
  140. void *data ATTRIBUTE_UNUSED)
  141. {
  142. bfd_vma vma;
  143. bfd_size_type size;
  144. if (found)
  145. return;
  146. if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
  147. return;
  148. vma = bfd_get_section_vma (abfd, section);
  149. if (pc < vma)
  150. return;
  151. size = bfd_get_section_size (section);
  152. if (pc >= vma + size)
  153. return;
  154. found = bfd_find_nearest_line_discriminator (abfd, section, syms, pc - vma,
  155. &filename, &functionname,
  156. &line, &discriminator);
  157. }
  158. /* Look for an offset in a section. This is directly called. */
  159. static void
  160. find_offset_in_section (bfd *abfd, asection *section)
  161. {
  162. bfd_size_type size;
  163. if (found)
  164. return;
  165. if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
  166. return;
  167. size = bfd_get_section_size (section);
  168. if (pc >= size)
  169. return;
  170. found = bfd_find_nearest_line_discriminator (abfd, section, syms, pc,
  171. &filename, &functionname,
  172. &line, &discriminator);
  173. }
  174. /* Read hexadecimal addresses from stdin, translate into
  175. file_name:line_number and optionally function name. */
  176. static void
  177. translate_addresses (bfd *abfd, asection *section)
  178. {
  179. int read_stdin = (naddr == 0);
  180. for (;;)
  181. {
  182. if (read_stdin)
  183. {
  184. char addr_hex[100];
  185. if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
  186. break;
  187. pc = bfd_scan_vma (addr_hex, NULL, 16);
  188. }
  189. else
  190. {
  191. if (naddr <= 0)
  192. break;
  193. --naddr;
  194. pc = bfd_scan_vma (*addr++, NULL, 16);
  195. }
  196. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  197. {
  198. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  199. bfd_vma sign = (bfd_vma) 1 << (bed->s->arch_size - 1);
  200. pc &= (sign << 1) - 1;
  201. if (bed->sign_extend_vma)
  202. pc = (pc ^ sign) - sign;
  203. }
  204. if (with_addresses)
  205. {
  206. printf ("0x");
  207. bfd_printf_vma (abfd, pc);
  208. if (pretty_print)
  209. printf (": ");
  210. else
  211. printf ("\n");
  212. }
  213. found = FALSE;
  214. if (section)
  215. find_offset_in_section (abfd, section);
  216. else
  217. bfd_map_over_sections (abfd, find_address_in_section, NULL);
  218. if (! found)
  219. {
  220. if (with_functions)
  221. {
  222. if (pretty_print)
  223. printf ("?? ");
  224. else
  225. printf ("??\n");
  226. }
  227. printf ("??:0\n");
  228. }
  229. else
  230. {
  231. while (1)
  232. {
  233. if (with_functions)
  234. {
  235. const char *name;
  236. char *alloc = NULL;
  237. name = functionname;
  238. if (name == NULL || *name == '\0')
  239. name = "??";
  240. else if (do_demangle)
  241. {
  242. alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
  243. if (alloc != NULL)
  244. name = alloc;
  245. }
  246. printf ("%s", name);
  247. if (pretty_print)
  248. /* Note for translators: This printf is used to join the
  249. function name just printed above to the line number/
  250. file name pair that is about to be printed below. Eg:
  251. foo at 123:bar.c */
  252. printf (_(" at "));
  253. else
  254. printf ("\n");
  255. if (alloc != NULL)
  256. free (alloc);
  257. }
  258. if (base_names && filename != NULL)
  259. {
  260. char *h;
  261. h = strrchr (filename, '/');
  262. if (h != NULL)
  263. filename = h + 1;
  264. }
  265. printf ("%s:", filename ? filename : "??");
  266. if (line != 0)
  267. {
  268. if (discriminator != 0)
  269. printf ("%u (discriminator %u)\n", line, discriminator);
  270. else
  271. printf ("%u\n", line);
  272. }
  273. else
  274. printf ("?\n");
  275. if (!unwind_inlines)
  276. found = FALSE;
  277. else
  278. found = bfd_find_inliner_info (abfd, &filename, &functionname,
  279. &line);
  280. if (! found)
  281. break;
  282. if (pretty_print)
  283. /* Note for translators: This printf is used to join the
  284. line number/file name pair that has just been printed with
  285. the line number/file name pair that is going to be printed
  286. by the next iteration of the while loop. Eg:
  287. 123:bar.c (inlined by) 456:main.c */
  288. printf (_(" (inlined by) "));
  289. }
  290. }
  291. /* fflush() is essential for using this command as a server
  292. child process that reads addresses from a pipe and responds
  293. with line number information, processing one address at a
  294. time. */
  295. fflush (stdout);
  296. }
  297. }
  298. /* Process a file. Returns an exit value for main(). */
  299. static int
  300. process_file (const char *file_name, const char *section_name,
  301. const char *target)
  302. {
  303. bfd *abfd;
  304. asection *section;
  305. char **matching;
  306. if (get_file_size (file_name) < 1)
  307. return 1;
  308. abfd = bfd_openr (file_name, target);
  309. if (abfd == NULL)
  310. bfd_fatal (file_name);
  311. /* Decompress sections. */
  312. abfd->flags |= BFD_DECOMPRESS;
  313. if (bfd_check_format (abfd, bfd_archive))
  314. fatal (_("%s: cannot get addresses from archive"), file_name);
  315. if (! bfd_check_format_matches (abfd, bfd_object, &matching))
  316. {
  317. bfd_nonfatal (bfd_get_filename (abfd));
  318. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  319. {
  320. list_matching_formats (matching);
  321. free (matching);
  322. }
  323. xexit (1);
  324. }
  325. if (section_name != NULL)
  326. {
  327. section = bfd_get_section_by_name (abfd, section_name);
  328. if (section == NULL)
  329. fatal (_("%s: cannot find section %s"), file_name, section_name);
  330. }
  331. else
  332. section = NULL;
  333. slurp_symtab (abfd);
  334. translate_addresses (abfd, section);
  335. if (syms != NULL)
  336. {
  337. free (syms);
  338. syms = NULL;
  339. }
  340. bfd_close (abfd);
  341. return 0;
  342. }
  343. int
  344. main (int argc, char **argv)
  345. {
  346. const char *file_name;
  347. const char *section_name;
  348. char *target;
  349. int c;
  350. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  351. setlocale (LC_MESSAGES, "");
  352. #endif
  353. #if defined (HAVE_SETLOCALE)
  354. setlocale (LC_CTYPE, "");
  355. #endif
  356. bindtextdomain (PACKAGE, LOCALEDIR);
  357. textdomain (PACKAGE);
  358. program_name = *argv;
  359. xmalloc_set_program_name (program_name);
  360. bfd_set_error_program_name (program_name);
  361. expandargv (&argc, &argv);
  362. bfd_init ();
  363. set_default_bfd_target ();
  364. file_name = NULL;
  365. section_name = NULL;
  366. target = NULL;
  367. while ((c = getopt_long (argc, argv, "ab:Ce:sfHhij:pVv", long_options, (int *) 0))
  368. != EOF)
  369. {
  370. switch (c)
  371. {
  372. case 0:
  373. break; /* We've been given a long option. */
  374. case 'a':
  375. with_addresses = TRUE;
  376. break;
  377. case 'b':
  378. target = optarg;
  379. break;
  380. case 'C':
  381. do_demangle = TRUE;
  382. if (optarg != NULL)
  383. {
  384. enum demangling_styles style;
  385. style = cplus_demangle_name_to_style (optarg);
  386. if (style == unknown_demangling)
  387. fatal (_("unknown demangling style `%s'"),
  388. optarg);
  389. cplus_demangle_set_style (style);
  390. }
  391. break;
  392. case 'e':
  393. file_name = optarg;
  394. break;
  395. case 's':
  396. base_names = TRUE;
  397. break;
  398. case 'f':
  399. with_functions = TRUE;
  400. break;
  401. case 'p':
  402. pretty_print = TRUE;
  403. break;
  404. case 'v':
  405. case 'V':
  406. print_version ("addr2line");
  407. break;
  408. case 'h':
  409. case 'H':
  410. usage (stdout, 0);
  411. break;
  412. case 'i':
  413. unwind_inlines = TRUE;
  414. break;
  415. case 'j':
  416. section_name = optarg;
  417. break;
  418. default:
  419. usage (stderr, 1);
  420. break;
  421. }
  422. }
  423. if (file_name == NULL)
  424. file_name = "a.out";
  425. addr = argv + optind;
  426. naddr = argc - optind;
  427. return process_file (file_name, section_name, target);
  428. }