2 * Copyright (c) 2009 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
43 ELFTC_VCSID("$Id: addr2line.c 3499 2016-11-25 16:06:29Z emaste $");
49 Dwarf_Unsigned call_file;
50 Dwarf_Unsigned call_line;
52 Dwarf_Signed ranges_cnt;
53 struct Func *inlined_caller;
54 STAILQ_ENTRY(Func) next;
62 Dwarf_Signed nsrcfiles;
63 STAILQ_HEAD(, Func) funclist;
67 static struct option longopts[] = {
68 {"addresses", no_argument, NULL, 'a'},
69 {"target" , required_argument, NULL, 'b'},
70 {"demangle", no_argument, NULL, 'C'},
71 {"exe", required_argument, NULL, 'e'},
72 {"functions", no_argument, NULL, 'f'},
73 {"inlines", no_argument, NULL, 'i'},
74 {"section", required_argument, NULL, 'j'},
75 {"pretty-print", no_argument, NULL, 'p'},
76 {"basename", no_argument, NULL, 's'},
77 {"help", no_argument, NULL, 'H'},
78 {"version", no_argument, NULL, 'V'},
81 static int demangle, func, base, inlines, print_addr, pretty_print;
82 static char unknown[] = { '?', '?', '\0' };
83 static Dwarf_Addr section_base;
84 static struct CU *culist;
86 #define USAGE_MESSAGE "\
87 Usage: %s [options] hexaddress...\n\
88 Map program addresses to source file names and line numbers.\n\n\
90 -a | --addresses Display address prior to line number info.\n\
91 -b TGT | --target=TGT (Accepted but ignored).\n\
92 -e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\
93 -f | --functions Display function names.\n\
94 -i | --inlines Display caller info for inlined functions.\n\
95 -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\
96 -p | --pretty-print Display line number info and function name\n\
97 in human readable manner.\n\
98 -s | --basename Only show the base name for each file name.\n\
99 -C | --demangle Demangle C++ names.\n\
100 -H | --help Print a help message.\n\
101 -V | --version Print a version identifier and exit.\n"
106 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
114 fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
119 * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't
120 * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1)
121 * generate DW_AT_high_pc as an offset from DW_AT_low_pc.
123 * "If the value of the DW_AT_high_pc is of class address, it is the
124 * relocated address of the first location past the last instruction
125 * associated with the entity; if it is of class constant, the value
126 * is an unsigned integer offset which when added to the low PC gives
127 * the address of the first location past the last instruction
128 * associated with the entity."
130 * DWARF4 spec, section 2.17.2.
133 handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc)
140 ret = dwarf_attr(die, DW_AT_high_pc, &at, &de);
141 if (ret == DW_DLV_ERROR) {
142 warnx("dwarf_attr failed: %s", dwarf_errmsg(de));
145 ret = dwarf_whatform(at, &form, &de);
146 if (ret == DW_DLV_ERROR) {
147 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
150 if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT)
157 search_func(struct CU *cu, Dwarf_Unsigned addr)
160 Dwarf_Unsigned lopc, hipc, addr_base;
165 STAILQ_FOREACH(f, &cu->funclist, next) {
166 if (f->ranges != NULL) {
168 for (i = 0; i < f->ranges_cnt; i++) {
169 if (f->ranges[i].dwr_type == DW_RANGES_END)
171 if (f->ranges[i].dwr_type ==
172 DW_RANGES_ADDRESS_SELECTION) {
173 addr_base = f->ranges[i].dwr_addr2;
177 /* DW_RANGES_ENTRY */
178 lopc = f->ranges[i].dwr_addr1 + addr_base;
179 hipc = f->ranges[i].dwr_addr2 + addr_base;
180 if (addr >= lopc && addr < hipc) {
191 } else if (addr >= f->lopc && addr < f->hipc) {
193 (f->lopc >= f0->lopc && f->hipc <= f0->hipc))
202 collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu)
204 Dwarf_Die ret_die, abst_die, spec_die;
207 Dwarf_Unsigned lopc, hipc, ranges_off;
208 Dwarf_Signed ranges_cnt;
210 Dwarf_Attribute abst_at, spec_at;
211 Dwarf_Ranges *ranges;
212 const char *funcname;
214 int found_ranges, ret;
217 abst_die = spec_die = NULL;
219 if (dwarf_tag(die, &tag, &de)) {
220 warnx("dwarf_tag: %s", dwarf_errmsg(de));
223 if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point ||
224 tag == DW_TAG_inlined_subroutine) {
226 * Function address range can be specified by either
227 * a DW_AT_ranges attribute which points to a range list or
228 * by a pair of DW_AT_low_pc and DW_AT_high_pc attributes.
233 if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off,
235 dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges,
236 &ranges_cnt, NULL, &de) == DW_DLV_OK) {
237 if (ranges != NULL && ranges_cnt > 0) {
244 * Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer
247 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ||
248 dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de))
250 if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK)
255 * Most common case the function name is stored in DW_AT_name
258 if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) ==
263 * For inlined function, the actual name is probably in the DIE
264 * referenced by DW_AT_abstract_origin. (if present)
266 if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) ==
268 dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK &&
269 dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK &&
270 dwarf_attrval_string(abst_die, DW_AT_name, &funcname,
275 * If DW_AT_name is not present, but DW_AT_specification is
276 * present, then probably the actual name is in the DIE
277 * referenced by DW_AT_specification.
279 if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) ==
281 dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK &&
282 dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK &&
283 dwarf_attrval_string(spec_die, DW_AT_name, &funcname,
287 /* Skip if no name associated with this DIE. */
291 if ((f = calloc(1, sizeof(*f))) == NULL)
292 err(EXIT_FAILURE, "calloc");
293 if ((f->name = strdup(funcname)) == NULL)
294 err(EXIT_FAILURE, "strdup");
297 f->ranges_cnt = ranges_cnt;
302 if (tag == DW_TAG_inlined_subroutine) {
303 f->inlined_caller = parent;
304 dwarf_attrval_unsigned(die, DW_AT_call_file,
306 dwarf_attrval_unsigned(die, DW_AT_call_line,
309 STAILQ_INSERT_TAIL(&cu->funclist, f, next);
314 /* Search children. */
315 ret = dwarf_child(die, &ret_die, &de);
316 if (ret == DW_DLV_ERROR)
317 warnx("dwarf_child: %s", dwarf_errmsg(de));
318 else if (ret == DW_DLV_OK) {
320 collect_func(dbg, ret_die, f, cu);
322 collect_func(dbg, ret_die, parent, cu);
325 /* Search sibling. */
326 ret = dwarf_siblingof(dbg, die, &ret_die, &de);
327 if (ret == DW_DLV_ERROR)
328 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
329 else if (ret == DW_DLV_OK)
330 collect_func(dbg, ret_die, parent, cu);
333 dwarf_dealloc(dbg, die, DW_DLA_DIE);
335 if (abst_die != NULL)
336 dwarf_dealloc(dbg, abst_die, DW_DLA_DIE);
338 if (spec_die != NULL)
339 dwarf_dealloc(dbg, spec_die, DW_DLA_DIE);
343 print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file,
344 Dwarf_Unsigned call_line)
346 char demangled[1024];
349 if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles)
350 file = cu->srcfiles[call_file - 1];
355 printf(" (inlined by) ");
358 if (demangle && !elftc_demangle(f->name, demangled,
359 sizeof(demangled), 0)) {
361 printf("%s at ", demangled);
363 printf("%s\n", demangled);
366 printf("%s at ", f->name);
368 printf("%s\n", f->name);
371 (void) printf("%s:%ju\n", base ? basename(file) : file,
372 (uintmax_t) call_line);
374 if (f->inlined_caller != NULL)
375 print_inlines(cu, f->inlined_caller, f->call_file,
380 translate(Dwarf_Debug dbg, Elf *e, const char* addrstr)
382 Dwarf_Die die, ret_die;
386 Dwarf_Unsigned lopc, hipc, addr, lineno, plineno;
388 Dwarf_Addr lineaddr, plineaddr;
392 const char *funcname;
393 char *file, *file0, *pfile;
394 char demangled[1024];
397 addr = strtoull(addrstr, NULL, 16);
398 addr += section_base;
404 while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
405 &de)) == DW_DLV_OK) {
407 while (dwarf_siblingof(dbg, die, &ret_die, &de) == DW_DLV_OK) {
409 dwarf_dealloc(dbg, die, DW_DLA_DIE);
411 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
412 warnx("dwarf_tag failed: %s",
417 /* XXX: What about DW_TAG_partial_unit? */
418 if (tag == DW_TAG_compile_unit)
421 if (ret_die == NULL) {
422 warnx("could not find DW_TAG_compile_unit die");
425 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ==
427 if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc,
430 * Check if the address falls into the PC
433 if (handle_high_pc(die, lopc, &hipc) !=
437 /* Assume ~0ULL if DW_AT_high_pc not present */
442 * Record the CU in the hash table for faster lookup
445 if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) {
446 warnx("dwarf_dieoffset failed: %s",
450 HASH_FIND(hh, culist, &off, sizeof(off), cu);
452 if ((cu = calloc(1, sizeof(*cu))) == NULL)
453 err(EXIT_FAILURE, "calloc");
457 STAILQ_INIT(&cu->funclist);
458 HASH_ADD(hh, culist, off, sizeof(off), cu);
461 if (addr >= lopc && addr < hipc)
467 dwarf_dealloc(dbg, die, DW_DLA_DIE);
472 if (ret != DW_DLV_OK || die == NULL)
475 switch (dwarf_srclines(die, &lbuf, &lcount, &de)) {
478 case DW_DLV_NO_ENTRY:
479 /* If a CU lacks debug info, just skip it. */
482 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
489 for (i = 0; i < lcount; i++) {
490 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
491 warnx("dwarf_lineaddr: %s", dwarf_errmsg(de));
494 if (dwarf_lineno(lbuf[i], &lineno, &de)) {
495 warnx("dwarf_lineno: %s", dwarf_errmsg(de));
498 if (dwarf_linesrc(lbuf[i], &file0, &de)) {
499 warnx("dwarf_linesrc: %s", dwarf_errmsg(de));
502 if (addr == lineaddr)
504 else if (addr < lineaddr && addr > plineaddr) {
509 plineaddr = lineaddr;
517 if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) {
518 if (cu->srcfiles == NULL)
519 if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles,
521 warnx("dwarf_srcfiles: %s", dwarf_errmsg(de));
522 if (STAILQ_EMPTY(&cu->funclist)) {
523 collect_func(dbg, die, NULL, cu);
526 f = search_func(cu, addr);
532 if ((ec = gelf_getclass(e)) == ELFCLASSNONE) {
533 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
536 if (ec == ELFCLASS32) {
538 printf("0x%08jx: ", (uintmax_t) addr);
540 printf("0x%08jx\n", (uintmax_t) addr);
543 printf("0x%016jx: ", (uintmax_t) addr);
545 printf("0x%016jx\n", (uintmax_t) addr);
550 if (funcname == NULL)
552 if (demangle && !elftc_demangle(funcname, demangled,
553 sizeof(demangled), 0)) {
555 printf("%s at ", demangled);
557 printf("%s\n", demangled);
560 printf("%s at ", funcname);
562 printf("%s\n", funcname);
566 (void) printf("%s:%ju\n", base ? basename(file) : file,
569 if (ret == DW_DLV_OK && inlines && cu != NULL &&
570 cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL)
571 print_inlines(cu, f->inlined_caller, f->call_file,
575 dwarf_dealloc(dbg, die, DW_DLA_DIE);
578 * Reset internal CU pointer, so we will start from the first CU
581 while (ret != DW_DLV_NO_ENTRY) {
582 if (ret == DW_DLV_ERROR)
583 errx(EXIT_FAILURE, "dwarf_next_cu_header: %s",
585 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
591 find_section_base(const char *exe, Elf *e, const char *section)
601 if (gelf_getehdr(e, &eh) != &eh) {
602 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
606 if (!elf_getshstrndx(e, &shstrndx)) {
607 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
614 while ((scn = elf_nextscn(e, scn)) != NULL) {
615 if (gelf_getshdr(scn, &sh) == NULL) {
616 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
619 if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL)
621 if (!strcmp(section, name)) {
622 if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) {
624 * For executables, section base is the virtual
625 * address of the specified section.
627 section_base = sh.sh_addr;
628 } else if (eh.e_type == ET_REL) {
630 * For relocatables, section base is the
631 * relative offset of the specified section
632 * to the start of the first section.
636 warnx("unknown e_type %u", eh.e_type);
642 elferr = elf_errno();
644 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
646 errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section);
650 main(int argc, char **argv)
655 const char *exe, *section;
661 while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts,
706 if ((fd = open(exe, O_RDONLY)) < 0)
707 err(EXIT_FAILURE, "%s", exe);
709 if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de))
710 errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de));
712 if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK)
713 errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de));
716 find_section_base(exe, e, section);
721 for (i = 0; i < argc; i++)
722 translate(dbg, e, argv[i]);
724 setvbuf(stdout, NULL, _IOLBF, 0);
725 while (fgets(line, sizeof(line), stdin) != NULL)
726 translate(dbg, e, line);
729 dwarf_finish(dbg, &de);