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>
29 #include <capsicum_helpers.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;
64 Dwarf_Signed nsrcfiles;
65 STAILQ_HEAD(, Func) funclist;
69 static struct option longopts[] = {
70 {"addresses", no_argument, NULL, 'a'},
71 {"target" , required_argument, NULL, 'b'},
72 {"demangle", no_argument, NULL, 'C'},
73 {"exe", required_argument, NULL, 'e'},
74 {"functions", no_argument, NULL, 'f'},
75 {"inlines", no_argument, NULL, 'i'},
76 {"section", required_argument, NULL, 'j'},
77 {"pretty-print", no_argument, NULL, 'p'},
78 {"basename", no_argument, NULL, 's'},
79 {"help", no_argument, NULL, 'H'},
80 {"version", no_argument, NULL, 'V'},
83 static int demangle, func, base, inlines, print_addr, pretty_print;
84 static char unknown[] = { '?', '?', '\0' };
85 static Dwarf_Addr section_base;
86 static struct CU *culist;
88 #define USAGE_MESSAGE "\
89 Usage: %s [options] hexaddress...\n\
90 Map program addresses to source file names and line numbers.\n\n\
92 -a | --addresses Display address prior to line number info.\n\
93 -b TGT | --target=TGT (Accepted but ignored).\n\
94 -e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\
95 -f | --functions Display function names.\n\
96 -i | --inlines Display caller info for inlined functions.\n\
97 -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\
98 -p | --pretty-print Display line number info and function name\n\
99 in human readable manner.\n\
100 -s | --basename Only show the base name for each file name.\n\
101 -C | --demangle Demangle C++ names.\n\
102 -H | --help Print a help message.\n\
103 -V | --version Print a version identifier and exit.\n"
108 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
116 fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
121 * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't
122 * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1)
123 * generate DW_AT_high_pc as an offset from DW_AT_low_pc.
125 * "If the value of the DW_AT_high_pc is of class address, it is the
126 * relocated address of the first location past the last instruction
127 * associated with the entity; if it is of class constant, the value
128 * is an unsigned integer offset which when added to the low PC gives
129 * the address of the first location past the last instruction
130 * associated with the entity."
132 * DWARF4 spec, section 2.17.2.
135 handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc)
142 ret = dwarf_attr(die, DW_AT_high_pc, &at, &de);
143 if (ret == DW_DLV_ERROR) {
144 warnx("dwarf_attr failed: %s", dwarf_errmsg(de));
147 ret = dwarf_whatform(at, &form, &de);
148 if (ret == DW_DLV_ERROR) {
149 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de));
152 if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT)
159 search_func(struct CU *cu, Dwarf_Unsigned addr)
162 Dwarf_Unsigned lopc, hipc, addr_base;
167 STAILQ_FOREACH(f, &cu->funclist, next) {
168 if (f->ranges != NULL) {
170 for (i = 0; i < f->ranges_cnt; i++) {
171 if (f->ranges[i].dwr_type == DW_RANGES_END)
173 if (f->ranges[i].dwr_type ==
174 DW_RANGES_ADDRESS_SELECTION) {
175 addr_base = f->ranges[i].dwr_addr2;
179 /* DW_RANGES_ENTRY */
180 lopc = f->ranges[i].dwr_addr1 + addr_base;
181 hipc = f->ranges[i].dwr_addr2 + addr_base;
182 if (addr >= lopc && addr < hipc) {
193 } else if (addr >= f->lopc && addr < f->hipc) {
195 (f->lopc >= f0->lopc && f->hipc <= f0->hipc))
204 collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu)
206 Dwarf_Die ret_die, abst_die, spec_die;
209 Dwarf_Unsigned lopc, hipc, ranges_off;
210 Dwarf_Signed ranges_cnt;
212 Dwarf_Attribute abst_at, spec_at;
213 Dwarf_Ranges *ranges;
214 const char *funcname;
216 int found_ranges, ret;
219 abst_die = spec_die = NULL;
221 if (dwarf_tag(die, &tag, &de)) {
222 warnx("dwarf_tag: %s", dwarf_errmsg(de));
225 if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point ||
226 tag == DW_TAG_inlined_subroutine) {
228 * Function address range can be specified by either
229 * a DW_AT_ranges attribute which points to a range list or
230 * by a pair of DW_AT_low_pc and DW_AT_high_pc attributes.
235 if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off,
237 dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges,
238 &ranges_cnt, NULL, &de) == DW_DLV_OK) {
239 if (ranges != NULL && ranges_cnt > 0) {
246 * Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer
249 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ||
250 dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de))
252 if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK)
257 * Most common case the function name is stored in DW_AT_name
260 if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) ==
265 * For inlined function, the actual name is probably in the DIE
266 * referenced by DW_AT_abstract_origin. (if present)
268 if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) ==
270 dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK &&
271 dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK &&
272 dwarf_attrval_string(abst_die, DW_AT_name, &funcname,
277 * If DW_AT_name is not present, but DW_AT_specification is
278 * present, then probably the actual name is in the DIE
279 * referenced by DW_AT_specification.
281 if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) ==
283 dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK &&
284 dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK &&
285 dwarf_attrval_string(spec_die, DW_AT_name, &funcname,
289 /* Skip if no name associated with this DIE. */
293 if ((f = calloc(1, sizeof(*f))) == NULL)
294 err(EXIT_FAILURE, "calloc");
295 if ((f->name = strdup(funcname)) == NULL)
296 err(EXIT_FAILURE, "strdup");
299 f->ranges_cnt = ranges_cnt;
304 if (tag == DW_TAG_inlined_subroutine) {
305 f->inlined_caller = parent;
306 dwarf_attrval_unsigned(die, DW_AT_call_file,
308 dwarf_attrval_unsigned(die, DW_AT_call_line,
311 STAILQ_INSERT_TAIL(&cu->funclist, f, next);
316 /* Search children. */
317 ret = dwarf_child(die, &ret_die, &de);
318 if (ret == DW_DLV_ERROR)
319 warnx("dwarf_child: %s", dwarf_errmsg(de));
320 else if (ret == DW_DLV_OK) {
322 collect_func(dbg, ret_die, f, cu);
324 collect_func(dbg, ret_die, parent, cu);
327 /* Search sibling. */
328 ret = dwarf_siblingof(dbg, die, &ret_die, &de);
329 if (ret == DW_DLV_ERROR)
330 warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
331 else if (ret == DW_DLV_OK)
332 collect_func(dbg, ret_die, parent, cu);
335 dwarf_dealloc(dbg, die, DW_DLA_DIE);
337 if (abst_die != NULL)
338 dwarf_dealloc(dbg, abst_die, DW_DLA_DIE);
340 if (spec_die != NULL)
341 dwarf_dealloc(dbg, spec_die, DW_DLA_DIE);
345 print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file,
346 Dwarf_Unsigned call_line)
348 char demangled[1024];
351 if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles)
352 file = cu->srcfiles[call_file - 1];
357 printf(" (inlined by) ");
360 if (demangle && !elftc_demangle(f->name, demangled,
361 sizeof(demangled), 0)) {
363 printf("%s at ", demangled);
365 printf("%s\n", demangled);
368 printf("%s at ", f->name);
370 printf("%s\n", f->name);
373 (void) printf("%s:%ju\n", base ? basename(file) : file,
374 (uintmax_t) call_line);
376 if (f->inlined_caller != NULL)
377 print_inlines(cu, f->inlined_caller, f->call_file,
382 translate(Dwarf_Debug dbg, Elf *e, const char* addrstr)
384 Dwarf_Die die, ret_die;
388 Dwarf_Unsigned lopc, hipc, addr, lineno, plineno;
390 Dwarf_Addr lineaddr, plineaddr;
394 const char *funcname;
395 char *file, *file0, *pfile;
396 char demangled[1024];
399 addr = strtoull(addrstr, NULL, 16);
400 addr += section_base;
406 while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
407 &de)) == DW_DLV_OK) {
409 while (dwarf_siblingof(dbg, die, &ret_die, &de) == DW_DLV_OK) {
411 dwarf_dealloc(dbg, die, DW_DLA_DIE);
413 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
414 warnx("dwarf_tag failed: %s",
419 /* XXX: What about DW_TAG_partial_unit? */
420 if (tag == DW_TAG_compile_unit)
423 if (ret_die == NULL) {
424 warnx("could not find DW_TAG_compile_unit die");
427 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ==
429 if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc,
432 * Check if the address falls into the PC
435 if (handle_high_pc(die, lopc, &hipc) !=
439 /* Assume ~0ULL if DW_AT_high_pc not present */
444 * Record the CU in the hash table for faster lookup
447 if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) {
448 warnx("dwarf_dieoffset failed: %s",
452 HASH_FIND(hh, culist, &off, sizeof(off), cu);
454 if ((cu = calloc(1, sizeof(*cu))) == NULL)
455 err(EXIT_FAILURE, "calloc");
459 STAILQ_INIT(&cu->funclist);
460 HASH_ADD(hh, culist, off, sizeof(off), cu);
463 if (addr >= lopc && addr < hipc)
469 dwarf_dealloc(dbg, die, DW_DLA_DIE);
474 if (ret != DW_DLV_OK || die == NULL)
477 switch (dwarf_srclines(die, &lbuf, &lcount, &de)) {
480 case DW_DLV_NO_ENTRY:
481 /* If a CU lacks debug info, just skip it. */
484 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
491 for (i = 0; i < lcount; i++) {
492 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
493 warnx("dwarf_lineaddr: %s", dwarf_errmsg(de));
496 if (dwarf_lineno(lbuf[i], &lineno, &de)) {
497 warnx("dwarf_lineno: %s", dwarf_errmsg(de));
500 if (dwarf_linesrc(lbuf[i], &file0, &de)) {
501 warnx("dwarf_linesrc: %s", dwarf_errmsg(de));
504 if (addr == lineaddr)
506 else if (addr < lineaddr && addr > plineaddr) {
511 plineaddr = lineaddr;
519 if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) {
520 if (cu->srcfiles == NULL)
521 if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles,
523 warnx("dwarf_srcfiles: %s", dwarf_errmsg(de));
524 if (STAILQ_EMPTY(&cu->funclist)) {
525 collect_func(dbg, die, NULL, cu);
528 f = search_func(cu, addr);
534 if ((ec = gelf_getclass(e)) == ELFCLASSNONE) {
535 warnx("gelf_getclass failed: %s", elf_errmsg(-1));
538 if (ec == ELFCLASS32) {
540 printf("0x%08jx: ", (uintmax_t) addr);
542 printf("0x%08jx\n", (uintmax_t) addr);
545 printf("0x%016jx: ", (uintmax_t) addr);
547 printf("0x%016jx\n", (uintmax_t) addr);
552 if (funcname == NULL)
554 if (demangle && !elftc_demangle(funcname, demangled,
555 sizeof(demangled), 0)) {
557 printf("%s at ", demangled);
559 printf("%s\n", demangled);
562 printf("%s at ", funcname);
564 printf("%s\n", funcname);
568 (void) printf("%s:%ju\n", base ? basename(file) : file,
571 if (ret == DW_DLV_OK && inlines && cu != NULL &&
572 cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL)
573 print_inlines(cu, f->inlined_caller, f->call_file,
577 dwarf_dealloc(dbg, die, DW_DLA_DIE);
580 * Reset internal CU pointer, so we will start from the first CU
583 while (ret != DW_DLV_NO_ENTRY) {
584 if (ret == DW_DLV_ERROR)
585 errx(EXIT_FAILURE, "dwarf_next_cu_header: %s",
587 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
593 find_section_base(const char *exe, Elf *e, const char *section)
603 if (gelf_getehdr(e, &eh) != &eh) {
604 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
608 if (!elf_getshstrndx(e, &shstrndx)) {
609 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
616 while ((scn = elf_nextscn(e, scn)) != NULL) {
617 if (gelf_getshdr(scn, &sh) == NULL) {
618 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
621 if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL)
623 if (!strcmp(section, name)) {
624 if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) {
626 * For executables, section base is the virtual
627 * address of the specified section.
629 section_base = sh.sh_addr;
630 } else if (eh.e_type == ET_REL) {
632 * For relocatables, section base is the
633 * relative offset of the specified section
634 * to the start of the first section.
638 warnx("unknown e_type %u", eh.e_type);
644 elferr = elf_errno();
646 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
648 errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section);
652 main(int argc, char **argv)
658 const char *exe, *section;
664 while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts,
709 if ((fd = open(exe, O_RDONLY)) < 0)
710 err(EXIT_FAILURE, "%s", exe);
712 if (caph_rights_limit(fd, cap_rights_init(&rights, CAP_FSTAT,
714 errx(EXIT_FAILURE, "caph_rights_limit");
716 caph_cache_catpages();
717 if (caph_limit_stdio() < 0)
718 errx(EXIT_FAILURE, "failed to limit stdio rights");
719 if (caph_enter() < 0)
720 errx(EXIT_FAILURE, "failed to enter capability mode");
722 if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de))
723 errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de));
725 if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK)
726 errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de));
729 find_section_base(exe, e, section);
734 for (i = 0; i < argc; i++)
735 translate(dbg, e, argv[i]);
737 setvbuf(stdout, NULL, _IOLBF, 0);
738 while (fgets(line, sizeof(line), stdin) != NULL)
739 translate(dbg, e, line);
742 dwarf_finish(dbg, &de);