1 /* Locate source files and line information for given addresses
2 Copyright (C) 2005-2010, 2012, 2013 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2005.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 elfutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
36 #include <stdio_ext.h>
44 /* Name and version of program. */
45 static void print_version (FILE *stream, struct argp_state *state);
46 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
48 /* Bug report address. */
49 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
52 /* Values for the parameters which have no short form. */
53 #define OPT_DEMANGLER 0x100
55 /* Definitions of arguments for argp functions. */
56 static const struct argp_option options[] =
58 { NULL, 0, NULL, 0, N_("Output selection options:"), 2 },
59 { "basenames", 's', NULL, 0, N_("Show only base names of source files"), 0 },
60 { "absolute", 'A', NULL, 0,
61 N_("Show absolute file names using compilation directory"), 0 },
62 { "functions", 'f', NULL, 0, N_("Also show function names"), 0 },
63 { "symbols", 'S', NULL, 0, N_("Also show symbol or section names"), 0 },
64 { "symbols-sections", 'x', NULL, 0, N_("Also show symbol and the section names"), 0 },
65 { "flags", 'F', NULL, 0, N_("Also show line table flags"), 0 },
66 { "section", 'j', "NAME", 0,
67 N_("Treat addresses as offsets relative to NAME section."), 0 },
68 { "inlines", 'i', NULL, 0,
69 N_("Show all source locations that caused inline expansion of subroutines at the address."),
72 { NULL, 0, NULL, 0, N_("Miscellaneous:"), 0 },
73 /* Unsupported options. */
74 { "target", 'b', "ARG", OPTION_HIDDEN, NULL, 0 },
75 { "demangle", 'C', "ARG", OPTION_HIDDEN | OPTION_ARG_OPTIONAL, NULL, 0 },
76 { "demangler", OPT_DEMANGLER, "ARG", OPTION_HIDDEN, NULL, 0 },
77 { NULL, 0, NULL, 0, NULL, 0 }
80 /* Short description of program. */
81 static const char doc[] = N_("\
82 Locate source files and line information for ADDRs (in a.out by default).");
84 /* Strings for arguments in help texts. */
85 static const char args_doc[] = N_("[ADDR...]");
87 /* Prototype for option handler. */
88 static error_t parse_opt (int key, char *arg, struct argp_state *state);
90 static struct argp_child argp_children[2]; /* [0] is set in main. */
92 /* Data structure to communicate with argp functions. */
93 static const struct argp argp =
95 options, parse_opt, args_doc, doc, argp_children, NULL, NULL
100 static int handle_address (const char *addr, Dwfl *dwfl);
103 /* True if only base names of files should be shown. */
104 static bool only_basenames;
106 /* True if absolute file names based on DW_AT_comp_dir should be shown. */
107 static bool use_comp_dir;
109 /* True if line flags should be shown. */
110 static bool show_flags;
112 /* True if function names should be shown. */
113 static bool show_functions;
115 /* True if ELF symbol or section info should be shown. */
116 static bool show_symbols;
118 /* True if section associated with a symbol address should be shown. */
119 static bool show_symbol_sections;
121 /* If non-null, take address parameters as relative to named section. */
122 static const char *just_section;
124 /* True if all inlined subroutines of the current address should be shown. */
125 static bool show_inlines;
129 main (int argc, char *argv[])
134 /* Make memory leak detection possible. */
137 /* We use no threads here which can interfere with handling a stream. */
138 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
141 (void) setlocale (LC_ALL, "");
143 /* Make sure the message catalog can be found. */
144 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
146 /* Initialize the message catalog. */
147 (void) textdomain (PACKAGE_TARNAME);
149 /* Parse and process arguments. This includes opening the modules. */
150 argp_children[0].argp = dwfl_standard_argp ();
151 argp_children[0].group = 1;
153 (void) argp_parse (&argp, argc, argv, 0, &remaining, &dwfl);
154 assert (dwfl != NULL);
156 /* Now handle the addresses. In case none are given on the command
157 line, read from stdin. */
158 if (remaining == argc)
160 /* We use no threads here which can interfere with handling a stream. */
161 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
166 while (!feof_unlocked (stdin))
168 if ((chars = getline (&buf, &len, stdin)) < 0)
171 if (buf[chars - 1] == '\n')
172 buf[chars - 1] = '\0';
174 result = handle_address (buf, dwfl);
182 result = handle_address (argv[remaining], dwfl);
183 while (++remaining < argc);
191 /* Print the version information. */
193 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
195 fprintf (stream, "addr2line (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
196 fprintf (stream, gettext ("\
197 Copyright (C) %s Red Hat, Inc.\n\
198 This is free software; see the source for copying conditions. There is NO\n\
199 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
201 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
205 /* Handle program arguments. */
207 parse_opt (int key, char *arg, struct argp_state *state)
212 state->child_inputs[0] = state->input;
218 /* Ignored for compatibility. */
222 only_basenames = true;
230 show_functions = true;
243 show_symbol_sections = true;
255 return ARGP_ERR_UNKNOWN;
262 print_dwarf_function (Dwfl_Module *mod, Dwarf_Addr addr)
265 Dwarf_Die *cudie = dwfl_module_addrdie (mod, addr, &bias);
268 int nscopes = dwarf_getscopes (cudie, addr - bias, &scopes);
272 for (int i = 0; i < nscopes; ++i)
273 switch (dwarf_tag (&scopes[i]))
275 case DW_TAG_subprogram:
277 const char *name = dwarf_diename (&scopes[i]);
284 case DW_TAG_inlined_subroutine:
286 const char *name = dwarf_diename (&scopes[i]);
289 printf ("%s inlined", name);
292 if (dwarf_getsrcfiles (cudie, &files, NULL) == 0)
294 Dwarf_Attribute attr_mem;
296 if (dwarf_formudata (dwarf_attr (&scopes[i],
298 &attr_mem), &val) == 0)
300 const char *file = dwarf_filesrc (files, val, NULL, NULL);
301 unsigned int lineno = 0;
302 unsigned int colno = 0;
303 if (dwarf_formudata (dwarf_attr (&scopes[i],
305 &attr_mem), &val) == 0)
307 if (dwarf_formudata (dwarf_attr (&scopes[i],
309 &attr_mem), &val) == 0)
312 const char *comp_dir = "";
313 const char *comp_dir_sep = "";
317 else if (only_basenames)
318 file = basename (file);
319 else if (use_comp_dir && file[0] != '/')
321 const char *const *dirs;
323 if (dwarf_getsrcdirs (files, &dirs, &ndirs) == 0
332 printf (" from %s%s%s",
333 comp_dir, comp_dir_sep, file);
335 printf (" at %s%s%s:%u",
336 comp_dir, comp_dir_sep, file, lineno);
338 printf (" at %s%s%s:%u:%u",
339 comp_dir, comp_dir_sep, file, lineno, colno);
351 print_addrsym (Dwfl_Module *mod, GElf_Addr addr)
355 const char *name = dwfl_module_addrinfo (mod, addr, &off, &s,
359 /* No symbol name. Get a section name instead. */
360 int i = dwfl_module_relocate_address (mod, &addr);
362 name = dwfl_module_relocation_info (mod, i, NULL);
366 printf ("(%s)+%#" PRIx64 "\n", name, addr);
373 printf ("%s+%#" PRIx64 "", name, off);
375 // Also show section name for address.
376 if (show_symbol_sections)
379 Elf_Scn *scn = dwfl_module_address_section (mod, &addr, &ebias);
383 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
386 Elf *elf = dwfl_module_getelf (mod, &ebias);
388 if (gelf_getehdr (elf, &ehdr) != NULL)
389 printf (" (%s)", elf_strptr (elf, ehdr.e_shstrndx,
399 print_diesym (Dwarf_Die *die)
401 Dwarf_Attribute attr;
404 name = dwarf_formstring (dwarf_attr_integrate (die, DW_AT_MIPS_linkage_name,
406 ?: dwarf_attr_integrate (die, DW_AT_linkage_name,
410 name = dwarf_diename (die) ?: "??";
416 see_one_module (Dwfl_Module *mod,
417 void **userdata __attribute__ ((unused)),
418 const char *name __attribute__ ((unused)),
419 Dwarf_Addr start __attribute__ ((unused)),
422 Dwfl_Module **result = arg;
424 return DWARF_CB_ABORT;
430 find_symbol (Dwfl_Module *mod,
431 void **userdata __attribute__ ((unused)),
432 const char *name __attribute__ ((unused)),
433 Dwarf_Addr start __attribute__ ((unused)),
436 const char *looking_for = ((void **) arg)[0];
437 GElf_Sym *symbol = ((void **) arg)[1];
438 GElf_Addr *value = ((void **) arg)[2];
440 int n = dwfl_module_getsymtab (mod);
441 for (int i = 1; i < n; ++i)
443 const char *symbol_name = dwfl_module_getsym_info (mod, i, symbol,
446 if (symbol_name == NULL || symbol_name[0] == '\0')
448 switch (GELF_ST_TYPE (symbol->st_info))
455 if (!strcmp (symbol_name, looking_for))
457 ((void **) arg)[0] = NULL;
458 return DWARF_CB_ABORT;
467 adjust_to_section (const char *name, uintmax_t *addr, Dwfl *dwfl)
469 /* It was (section)+offset. This makes sense if there is
470 only one module to look in for a section. */
471 Dwfl_Module *mod = NULL;
472 if (dwfl_getmodules (dwfl, &see_one_module, &mod, 0) != 0
474 error (EXIT_FAILURE, 0, gettext ("Section syntax requires"
475 " exactly one module"));
477 int nscn = dwfl_module_relocations (mod);
478 for (int i = 0; i < nscn; ++i)
481 const char *scn = dwfl_module_relocation_info (mod, i, &shndx);
482 if (unlikely (scn == NULL))
484 if (!strcmp (scn, name))
486 /* Found the section. */
489 GElf_Shdr *shdr = gelf_getshdr
490 (elf_getscn (dwfl_module_getelf (mod, &shdr_bias), shndx),
492 if (unlikely (shdr == NULL))
495 if (*addr >= shdr->sh_size)
497 gettext ("offset %#" PRIxMAX " lies outside"
501 *addr += shdr->sh_addr + shdr_bias;
510 print_src (const char *src, int lineno, int linecol, Dwarf_Die *cu)
512 const char *comp_dir = "";
513 const char *comp_dir_sep = "";
516 src = basename (src);
517 else if (use_comp_dir && src[0] != '/')
519 Dwarf_Attribute attr;
520 comp_dir = dwarf_formstring (dwarf_attr (cu, DW_AT_comp_dir, &attr));
521 if (comp_dir != NULL)
526 printf ("%s%s%s:%d:%d",
527 comp_dir, comp_dir_sep, src, lineno, linecol);
530 comp_dir, comp_dir_sep, src, lineno);
534 handle_address (const char *string, Dwfl *dwfl)
537 uintmax_t addr = strtoumax (string, &endp, 0);
543 if (sscanf (string, "(%m[^)])%" PRIiMAX "%n", &name, &addr, &i) == 2
544 && string[i] == '\0')
545 parsed = adjust_to_section (name, &addr, dwfl);
546 switch (sscanf (string, "%m[^-+]%n%" PRIiMAX "%n", &name, &i, &addr, &j))
554 if (string[j] != '\0')
557 /* It was symbol[+offset]. */
560 void *arg[3] = { name, &sym, &value };
561 (void) dwfl_getmodules (dwfl, &find_symbol, arg, 0);
563 error (0, 0, gettext ("cannot find symbol '%s'"), name);
566 if (sym.st_size != 0 && addr >= sym.st_size)
568 gettext ("offset %#" PRIxMAX " lies outside"
569 " contents of '%s'"),
581 else if (just_section != NULL
582 && !adjust_to_section (just_section, &addr, dwfl))
585 Dwfl_Module *mod = dwfl_addrmodule (dwfl, addr);
589 /* First determine the function name. Use the DWARF information if
591 if (! print_dwarf_function (mod, addr) && !show_symbols)
592 puts (dwfl_module_addrname (mod, addr) ?: "??");
596 print_addrsym (mod, addr);
598 Dwfl_Line *line = dwfl_module_getsrc (mod, addr);
603 if (line != NULL && (src = dwfl_lineinfo (line, &addr, &lineno, &linecol,
604 NULL, NULL)) != NULL)
606 print_src (src, lineno, linecol, dwfl_linecu (line));
610 Dwarf_Line *info = dwfl_dwarf_line (line, &bias);
611 assert (info != NULL);
613 inline void show (int (*get) (Dwarf_Line *, bool *),
617 if ((*get) (info, &flag) == 0 && flag)
618 fputs (note, stdout);
620 inline void show_int (int (*get) (Dwarf_Line *, unsigned int *),
624 if ((*get) (info, &val) == 0 && val != 0)
625 printf (" (%s %u)", name, val);
628 show (&dwarf_linebeginstatement, " (is_stmt)");
629 show (&dwarf_lineblock, " (basic_block)");
630 show (&dwarf_lineprologueend, " (prologue_end)");
631 show (&dwarf_lineepiloguebegin, " (epilogue_begin)");
632 show_int (&dwarf_lineisa, "isa");
633 show_int (&dwarf_linediscriminator, "discriminator");
643 Dwarf_Die *cudie = dwfl_module_addrdie (mod, addr, &bias);
645 Dwarf_Die *scopes = NULL;
646 int nscopes = dwarf_getscopes (cudie, addr - bias, &scopes);
652 Dwarf_Die subroutine;
653 Dwarf_Off dieoff = dwarf_dieoffset (&scopes[0]);
654 dwarf_offdie (dwfl_module_getdwarf (mod, &bias),
655 dieoff, &subroutine);
658 nscopes = dwarf_getscopes_die (&subroutine, &scopes);
663 if (dwarf_diecu (&scopes[0], &cu, NULL, NULL) != NULL
664 && dwarf_getsrcfiles (cudie, &files, NULL) == 0)
666 for (int i = 0; i < nscopes - 1; i++)
669 Dwarf_Attribute attr;
670 Dwarf_Die *die = &scopes[i];
671 if (dwarf_tag (die) != DW_TAG_inlined_subroutine)
675 print_diesym (&scopes[i + 1]);
680 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_file,
682 src = dwarf_filesrc (files, val, NULL, NULL);
684 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_line,
688 if (dwarf_formudata (dwarf_attr (die, DW_AT_call_column,
694 print_src (src, lineno, linecol, &cu);
710 #include "debugpred.h"