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>
45 ELFTC_VCSID("$Id: addr2line.c 3499 2016-11-25 16:06:29Z emaste $");
51 Dwarf_Unsigned call_file;
52 Dwarf_Unsigned call_line;
54 Dwarf_Signed ranges_cnt;
55 struct Func *inlined_caller;
56 STAILQ_ENTRY(Func) next;
65 Dwarf_Signed nsrcfiles;
66 STAILQ_HEAD(, Func) funclist;
71 static struct option longopts[] = {
72 {"addresses", no_argument, NULL, 'a'},
73 {"target" , required_argument, NULL, 'b'},
74 {"demangle", no_argument, NULL, 'C'},
75 {"exe", required_argument, NULL, 'e'},
76 {"functions", no_argument, NULL, 'f'},
77 {"inlines", no_argument, NULL, 'i'},
78 {"section", required_argument, NULL, 'j'},
79 {"pretty-print", no_argument, NULL, 'p'},
80 {"basename", no_argument, NULL, 's'},
81 {"help", no_argument, NULL, 'H'},
82 {"version", no_argument, NULL, 'V'},
86 static int demangle, func, base, inlines, print_addr, pretty_print;
87 static char unknown[] = { '?', '?', '\0' };
88 static Dwarf_Addr section_base;
89 /* Need a new curlopc that stores last lopc value. */
90 static Dwarf_Unsigned curlopc = ~0ULL;
91 static RB_HEAD(cutree, CU) cuhead = RB_INITIALIZER(&cuhead);
94 lopccmp(struct CU *e1, struct CU *e2)
96 return (e1->lopc < e2->lopc ? -1 : e1->lopc > e2->lopc);
99 RB_PROTOTYPE(cutree, CU, entry, lopccmp);
100 RB_GENERATE(cutree, CU, entry, lopccmp)
102 #define USAGE_MESSAGE "\
103 Usage: %s [options] hexaddress...\n\
104 Map program addresses to source file names and line numbers.\n\n\
106 -a | --addresses Display address prior to line number info.\n\
107 -b TGT | --target=TGT (Accepted but ignored).\n\
108 -e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\
109 -f | --functions Display function names.\n\
110 -i | --inlines Display caller info for inlined functions.\n\
111 -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\
112 -p | --pretty-print Display line number info and function name\n\
113 in human readable manner.\n\
114 -s | --basename Only show the base name for each file name.\n\
115 -C | --demangle Demangle C++ names.\n\
116 -H | --help Print a help message.\n\
117 -V | --version Print a version identifier and exit.\n"
122 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
130 fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
135 * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't
136 * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1)
137 * generate DW_AT_high_pc as an offset from DW_AT_low_pc.
139 * "If the value of the DW_AT_high_pc is of class address, it is the
140 * relocated address of the first location past the last instruction
141 * associated with the entity; if it is of class constant, the value
142 * is an unsigned integer offset which when added to the low PC gives
143 * the address of the first location past the last instruction
144 * associated with the entity."
146 * DWARF4 spec, section 2.17.2.
149 handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc)
156 ret = dwarf_attr(die, DW_AT_high_pc, &at, &de);
157 if (ret == DW_DLV_ERROR) {
158 warnx("dwarf_attr failed: %s", dwarf_errmsg(de));
161 ret = dwarf_whatform(at, &form, &de);
162 if (ret == DW_DLV_ERROR) {
163 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
166 if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT)
173 search_func(struct CU *cu, Dwarf_Unsigned addr)
176 Dwarf_Unsigned lopc, hipc, addr_base;
181 STAILQ_FOREACH(f, &cu->funclist, next) {
182 if (f->ranges != NULL) {
184 for (i = 0; i < f->ranges_cnt; i++) {
185 if (f->ranges[i].dwr_type == DW_RANGES_END)
187 if (f->ranges[i].dwr_type ==
188 DW_RANGES_ADDRESS_SELECTION) {
189 addr_base = f->ranges[i].dwr_addr2;
193 /* DW_RANGES_ENTRY */
194 lopc = f->ranges[i].dwr_addr1 + addr_base;
195 hipc = f->ranges[i].dwr_addr2 + addr_base;
196 if (addr >= lopc && addr < hipc) {
207 } else if (addr >= f->lopc && addr < f->hipc) {
209 (f->lopc >= f0->lopc && f->hipc <= f0->hipc))
218 collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu)
220 Dwarf_Die ret_die, abst_die, spec_die;
223 Dwarf_Unsigned lopc, hipc, ranges_off;
224 Dwarf_Signed ranges_cnt;
226 Dwarf_Attribute abst_at, spec_at;
227 Dwarf_Ranges *ranges;
228 const char *funcname;
230 int found_ranges, ret;
233 abst_die = spec_die = NULL;
235 if (dwarf_tag(die, &tag, &de)) {
236 warnx("dwarf_tag: %s", dwarf_errmsg(de));
239 if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point ||
240 tag == DW_TAG_inlined_subroutine) {
242 * Function address range can be specified by either
243 * a DW_AT_ranges attribute which points to a range list or
244 * by a pair of DW_AT_low_pc and DW_AT_high_pc attributes.
249 if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off,
251 dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges,
252 &ranges_cnt, NULL, &de) == DW_DLV_OK) {
253 if (ranges != NULL && ranges_cnt > 0) {
260 * Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer
263 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ||
264 dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de))
266 if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK)
271 * Most common case the function name is stored in DW_AT_name
274 if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) ==
279 * For inlined function, the actual name is probably in the DIE
280 * referenced by DW_AT_abstract_origin. (if present)
282 if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) ==
284 dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK &&
285 dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK &&
286 dwarf_attrval_string(abst_die, DW_AT_name, &funcname,
291 * If DW_AT_name is not present, but DW_AT_specification is
292 * present, then probably the actual name is in the DIE
293 * referenced by DW_AT_specification.
295 if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) ==
297 dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK &&
298 dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK &&
299 dwarf_attrval_string(spec_die, DW_AT_name, &funcname,
303 /* Skip if no name associated with this DIE. */
307 if ((f = calloc(1, sizeof(*f))) == NULL)
308 err(EXIT_FAILURE, "calloc");
309 if ((f->name = strdup(funcname)) == NULL)
310 err(EXIT_FAILURE, "strdup");
313 f->ranges_cnt = ranges_cnt;
318 if (tag == DW_TAG_inlined_subroutine) {
319 f->inlined_caller = parent;
320 dwarf_attrval_unsigned(die, DW_AT_call_file,
322 dwarf_attrval_unsigned(die, DW_AT_call_line,
325 STAILQ_INSERT_TAIL(&cu->funclist, f, next);
330 /* Search children. */
331 ret = dwarf_child(die, &ret_die, &de);
332 if (ret == DW_DLV_ERROR)
333 warnx("dwarf_child: %s", dwarf_errmsg(de));
334 else if (ret == DW_DLV_OK) {
336 collect_func(dbg, ret_die, f, cu);
338 collect_func(dbg, ret_die, parent, cu);
341 /* Search sibling. */
342 ret = dwarf_siblingof(dbg, die, &ret_die, &de);
343 if (ret == DW_DLV_ERROR)
344 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
345 else if (ret == DW_DLV_OK)
346 collect_func(dbg, ret_die, parent, cu);
350 dwarf_dealloc(dbg, die, DW_DLA_DIE);
352 if (abst_die != NULL)
353 dwarf_dealloc(dbg, abst_die, DW_DLA_DIE);
355 if (spec_die != NULL)
356 dwarf_dealloc(dbg, spec_die, DW_DLA_DIE);
360 print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file,
361 Dwarf_Unsigned call_line)
363 char demangled[1024];
366 if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles)
367 file = cu->srcfiles[call_file - 1];
372 printf(" (inlined by) ");
375 if (demangle && !elftc_demangle(f->name, demangled,
376 sizeof(demangled), 0)) {
378 printf("%s at ", demangled);
380 printf("%s\n", demangled);
383 printf("%s at ", f->name);
385 printf("%s\n", f->name);
388 (void) printf("%s:%ju\n", base ? basename(file) : file,
389 (uintmax_t) call_line);
391 if (f->inlined_caller != NULL)
392 print_inlines(cu, f->inlined_caller, f->call_file,
397 culookup(Dwarf_Unsigned addr)
399 struct CU find, *res;
402 res = RB_NFIND(cutree, &cuhead, &find);
404 if (res->lopc != addr)
405 res = RB_PREV(cutree, &cuhead, res);
406 if (res != NULL && addr >= res->lopc && addr < res->hipc)
409 res = RB_MAX(cutree, &cuhead);
410 if (res != NULL && addr >= res->lopc && addr < res->hipc)
417 * Check whether addr falls into range(s) of current CU, and save current CU
418 * to lookup tree if so.
421 check_range(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Unsigned addr,
425 Dwarf_Unsigned addr_base, lopc, hipc;
426 Dwarf_Off ranges_off;
427 Dwarf_Signed ranges_cnt;
428 Dwarf_Ranges *ranges;
437 ret = dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off, &de);
438 if (ret == DW_DLV_NO_ENTRY) {
439 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ==
442 return (DW_DLV_ERROR);
443 if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc,
446 * Check if the address falls into the PC
449 if (handle_high_pc(die, lopc, &hipc) !=
451 return (DW_DLV_ERROR);
453 /* Assume ~0ULL if DW_AT_high_pc not present */
457 if (addr >= lopc && addr < hipc) {
461 } else if (ret == DW_DLV_OK) {
462 ret = dwarf_get_ranges(dbg, ranges_off, &ranges,
463 &ranges_cnt, NULL, &de);
464 if (ret != DW_DLV_OK)
467 if (!ranges || ranges_cnt <= 0)
468 return (DW_DLV_ERROR);
470 for (i = 0; i < ranges_cnt; i++) {
471 if (ranges[i].dwr_type == DW_RANGES_END)
472 return (DW_DLV_NO_ENTRY);
474 if (ranges[i].dwr_type ==
475 DW_RANGES_ADDRESS_SELECTION) {
476 addr_base = ranges[i].dwr_addr2;
480 /* DW_RANGES_ENTRY */
481 lopc = ranges[i].dwr_addr1 + addr_base;
482 hipc = ranges[i].dwr_addr2 + addr_base;
485 return (DW_DLV_ERROR);
487 if (addr >= lopc && addr < hipc){
493 return (DW_DLV_ERROR);
497 if ((*cu = calloc(1, sizeof(struct CU))) == NULL)
498 err(EXIT_FAILURE, "calloc");
503 STAILQ_INIT(&(*cu)->funclist);
504 RB_INSERT(cutree, &cuhead, *cu);
508 return (DW_DLV_NO_ENTRY);
513 translate(Dwarf_Debug dbg, Elf *e, const char* addrstr)
515 Dwarf_Die die, ret_die;
519 Dwarf_Unsigned addr, lineno, plineno;
521 Dwarf_Addr lineaddr, plineaddr;
524 const char *funcname;
525 char *file, *file0, *pfile;
526 char demangled[1024];
529 addr = strtoull(addrstr, NULL, 16);
530 addr += section_base;
545 * We resume the CU scan from the last place we found a match.
546 * Because when we have 2 sequential addresses, and the second
547 * one is of the next CU, it is faster to just go to the next CU
548 * instead of starting from the beginning.
550 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
552 if (ret == DW_DLV_NO_ENTRY) {
553 if (curlopc == ~0ULL)
555 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL,
559 while (dwarf_siblingof(dbg, die, &ret_die, &de) == DW_DLV_OK) {
561 dwarf_dealloc(dbg, die, DW_DLA_DIE);
563 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
564 warnx("dwarf_tag failed: %s",
569 /* XXX: What about DW_TAG_partial_unit? */
570 if (tag == DW_TAG_compile_unit)
574 if (ret_die == NULL) {
575 warnx("could not find DW_TAG_compile_unit die");
578 ret = check_range(dbg, die, addr, &cu);
579 if (ret == DW_DLV_OK)
581 if (ret == DW_DLV_ERROR)
585 dwarf_dealloc(dbg, die, DW_DLA_DIE);
590 if (ret != DW_DLV_OK || die == NULL)
594 switch (dwarf_srclines(die, &lbuf, &lcount, &de)) {
597 case DW_DLV_NO_ENTRY:
598 /* If a CU lacks debug info, just skip it. */
601 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
608 for (i = 0; i < lcount; i++) {
609 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
610 warnx("dwarf_lineaddr: %s", dwarf_errmsg(de));
613 if (dwarf_lineno(lbuf[i], &lineno, &de)) {
614 warnx("dwarf_lineno: %s", dwarf_errmsg(de));
617 if (dwarf_linesrc(lbuf[i], &file0, &de)) {
618 warnx("dwarf_linesrc: %s", dwarf_errmsg(de));
621 if (addr == lineaddr)
623 else if (addr < lineaddr && addr > plineaddr) {
628 plineaddr = lineaddr;
636 if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) {
637 if (cu->srcfiles == NULL)
638 if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles,
640 warnx("dwarf_srcfiles: %s", dwarf_errmsg(de));
641 if (STAILQ_EMPTY(&cu->funclist)) {
642 collect_func(dbg, die, NULL, cu);
645 f = search_func(cu, addr);
651 if ((ec = gelf_getclass(e)) == ELFCLASSNONE) {
652 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
655 if (ec == ELFCLASS32) {
657 printf("0x%08jx: ", (uintmax_t) addr);
659 printf("0x%08jx\n", (uintmax_t) addr);
662 printf("0x%016jx: ", (uintmax_t) addr);
664 printf("0x%016jx\n", (uintmax_t) addr);
669 if (funcname == NULL)
671 if (demangle && !elftc_demangle(funcname, demangled,
672 sizeof(demangled), 0)) {
674 printf("%s at ", demangled);
676 printf("%s\n", demangled);
679 printf("%s at ", funcname);
681 printf("%s\n", funcname);
685 (void) printf("%s:%ju\n", base ? basename(file) : file,
688 if (ret == DW_DLV_OK && inlines && cu != NULL &&
689 cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL)
690 print_inlines(cu, f->inlined_caller, f->call_file,
695 find_section_base(const char *exe, Elf *e, const char *section)
705 if (gelf_getehdr(e, &eh) != &eh) {
706 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
710 if (!elf_getshstrndx(e, &shstrndx)) {
711 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
718 while ((scn = elf_nextscn(e, scn)) != NULL) {
719 if (gelf_getshdr(scn, &sh) == NULL) {
720 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
723 if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL)
725 if (!strcmp(section, name)) {
726 if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) {
728 * For executables, section base is the virtual
729 * address of the specified section.
731 section_base = sh.sh_addr;
732 } else if (eh.e_type == ET_REL) {
734 * For relocatables, section base is the
735 * relative offset of the specified section
736 * to the start of the first section.
740 warnx("unknown e_type %u", eh.e_type);
746 elferr = elf_errno();
748 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
750 errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section);
754 main(int argc, char **argv)
759 const char *exe, *section;
765 while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts,
810 if ((fd = open(exe, O_RDONLY)) < 0)
811 err(EXIT_FAILURE, "%s", exe);
813 if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de))
814 errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de));
816 if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK)
817 errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de));
820 find_section_base(exe, e, section);
825 for (i = 0; i < argc; i++)
826 translate(dbg, e, argv[i]);
828 setvbuf(stdout, NULL, _IOLBF, 0);
829 while (fgets(line, sizeof(line), stdin) != NULL)
830 translate(dbg, e, line);
833 dwarf_finish(dbg, &de);