1a85fe12eSEd Maste /*- 2a85fe12eSEd Maste * Copyright (c) 2009 Kai Wang 3a85fe12eSEd Maste * All rights reserved. 4a85fe12eSEd Maste * 5a85fe12eSEd Maste * Redistribution and use in source and binary forms, with or without 6a85fe12eSEd Maste * modification, are permitted provided that the following conditions 7a85fe12eSEd Maste * are met: 8a85fe12eSEd Maste * 1. Redistributions of source code must retain the above copyright 9a85fe12eSEd Maste * notice, this list of conditions and the following disclaimer. 10a85fe12eSEd Maste * 2. Redistributions in binary form must reproduce the above copyright 11a85fe12eSEd Maste * notice, this list of conditions and the following disclaimer in the 12a85fe12eSEd Maste * documentation and/or other materials provided with the distribution. 13a85fe12eSEd Maste * 14a85fe12eSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15a85fe12eSEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16a85fe12eSEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17a85fe12eSEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18a85fe12eSEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19a85fe12eSEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20a85fe12eSEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21a85fe12eSEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22a85fe12eSEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23a85fe12eSEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24a85fe12eSEd Maste * SUCH DAMAGE. 25a85fe12eSEd Maste */ 26a85fe12eSEd Maste 27a85fe12eSEd Maste #include <sys/param.h> 28af843291SMark Johnston #include <sys/tree.h> 291d954fedSMark Johnston 301d954fedSMark Johnston #include <capsicum_helpers.h> 31a85fe12eSEd Maste #include <dwarf.h> 32a85fe12eSEd Maste #include <err.h> 33a85fe12eSEd Maste #include <fcntl.h> 34a85fe12eSEd Maste #include <gelf.h> 35a85fe12eSEd Maste #include <getopt.h> 36a85fe12eSEd Maste #include <libdwarf.h> 37a85fe12eSEd Maste #include <libelftc.h> 38a85fe12eSEd Maste #include <libgen.h> 39a85fe12eSEd Maste #include <stdio.h> 40a85fe12eSEd Maste #include <stdlib.h> 41a85fe12eSEd Maste #include <string.h> 42a85fe12eSEd Maste 43a85fe12eSEd Maste #include "_elftc.h" 44a85fe12eSEd Maste 45d003e0d7SEd Maste ELFTC_VCSID("$Id: addr2line.c 3499 2016-11-25 16:06:29Z emaste $"); 4695fd7f26SEd Maste 4795fd7f26SEd Maste struct Func { 4895fd7f26SEd Maste char *name; 4995fd7f26SEd Maste Dwarf_Unsigned lopc; 5095fd7f26SEd Maste Dwarf_Unsigned hipc; 5195fd7f26SEd Maste Dwarf_Unsigned call_file; 5295fd7f26SEd Maste Dwarf_Unsigned call_line; 5395fd7f26SEd Maste Dwarf_Ranges *ranges; 5495fd7f26SEd Maste Dwarf_Signed ranges_cnt; 5595fd7f26SEd Maste struct Func *inlined_caller; 5695fd7f26SEd Maste STAILQ_ENTRY(Func) next; 5795fd7f26SEd Maste }; 5895fd7f26SEd Maste 5995fd7f26SEd Maste struct CU { 60af843291SMark Johnston RB_ENTRY(CU) entry; 6195fd7f26SEd Maste Dwarf_Off off; 6295fd7f26SEd Maste Dwarf_Unsigned lopc; 6395fd7f26SEd Maste Dwarf_Unsigned hipc; 6495fd7f26SEd Maste char **srcfiles; 6595fd7f26SEd Maste Dwarf_Signed nsrcfiles; 6695fd7f26SEd Maste STAILQ_HEAD(, Func) funclist; 67af843291SMark Johnston Dwarf_Die die; 6895fd7f26SEd Maste }; 69a85fe12eSEd Maste 70a85fe12eSEd Maste static struct option longopts[] = { 7195fd7f26SEd Maste {"addresses", no_argument, NULL, 'a'}, 72a85fe12eSEd Maste {"target" , required_argument, NULL, 'b'}, 73a85fe12eSEd Maste {"demangle", no_argument, NULL, 'C'}, 74a85fe12eSEd Maste {"exe", required_argument, NULL, 'e'}, 75a85fe12eSEd Maste {"functions", no_argument, NULL, 'f'}, 7695fd7f26SEd Maste {"inlines", no_argument, NULL, 'i'}, 77a85fe12eSEd Maste {"section", required_argument, NULL, 'j'}, 7895fd7f26SEd Maste {"pretty-print", no_argument, NULL, 'p'}, 79a85fe12eSEd Maste {"basename", no_argument, NULL, 's'}, 80a85fe12eSEd Maste {"help", no_argument, NULL, 'H'}, 81a85fe12eSEd Maste {"version", no_argument, NULL, 'V'}, 82a85fe12eSEd Maste {NULL, 0, NULL, 0} 83a85fe12eSEd Maste }; 84af843291SMark Johnston 8595fd7f26SEd Maste static int demangle, func, base, inlines, print_addr, pretty_print; 86a85fe12eSEd Maste static char unknown[] = { '?', '?', '\0' }; 87a85fe12eSEd Maste static Dwarf_Addr section_base; 88af843291SMark Johnston /* Need a new curlopc that stores last lopc value. */ 89af843291SMark Johnston static Dwarf_Unsigned curlopc = ~0ULL; 90*35beedaeSMark Johnston static RB_HEAD(cutree, CU) cuhead = RB_INITIALIZER(&cuhead); 91af843291SMark Johnston 92af843291SMark Johnston static int 93af843291SMark Johnston lopccmp(struct CU *e1, struct CU *e2) 94af843291SMark Johnston { 95af843291SMark Johnston return (e1->lopc < e2->lopc ? -1 : e1->lopc > e2->lopc); 96af843291SMark Johnston } 97af843291SMark Johnston 98af843291SMark Johnston RB_PROTOTYPE(cutree, CU, entry, lopccmp); 99af843291SMark Johnston RB_GENERATE(cutree, CU, entry, lopccmp) 100a85fe12eSEd Maste 101a85fe12eSEd Maste #define USAGE_MESSAGE "\ 102a85fe12eSEd Maste Usage: %s [options] hexaddress...\n\ 103a85fe12eSEd Maste Map program addresses to source file names and line numbers.\n\n\ 104a85fe12eSEd Maste Options:\n\ 10595fd7f26SEd Maste -a | --addresses Display address prior to line number info.\n\ 106a85fe12eSEd Maste -b TGT | --target=TGT (Accepted but ignored).\n\ 107656f49f8SEd Maste -e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\ 108a85fe12eSEd Maste -f | --functions Display function names.\n\ 10995fd7f26SEd Maste -i | --inlines Display caller info for inlined functions.\n\ 110a85fe12eSEd Maste -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\ 11195fd7f26SEd Maste -p | --pretty-print Display line number info and function name\n\ 11295fd7f26SEd Maste in human readable manner.\n\ 113a85fe12eSEd Maste -s | --basename Only show the base name for each file name.\n\ 114a85fe12eSEd Maste -C | --demangle Demangle C++ names.\n\ 115a85fe12eSEd Maste -H | --help Print a help message.\n\ 116a85fe12eSEd Maste -V | --version Print a version identifier and exit.\n" 117a85fe12eSEd Maste 118a85fe12eSEd Maste static void 119a85fe12eSEd Maste usage(void) 120a85fe12eSEd Maste { 121a85fe12eSEd Maste (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 122a85fe12eSEd Maste exit(1); 123a85fe12eSEd Maste } 124a85fe12eSEd Maste 125a85fe12eSEd Maste static void 126a85fe12eSEd Maste version(void) 127a85fe12eSEd Maste { 128a85fe12eSEd Maste 129a85fe12eSEd Maste fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); 130a85fe12eSEd Maste exit(0); 131a85fe12eSEd Maste } 132a85fe12eSEd Maste 133b00fe64fSEd Maste /* 134b00fe64fSEd Maste * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't 135b00fe64fSEd Maste * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1) 136b00fe64fSEd Maste * generate DW_AT_high_pc as an offset from DW_AT_low_pc. 137b00fe64fSEd Maste * 138b00fe64fSEd Maste * "If the value of the DW_AT_high_pc is of class address, it is the 139b00fe64fSEd Maste * relocated address of the first location past the last instruction 140b00fe64fSEd Maste * associated with the entity; if it is of class constant, the value 141b00fe64fSEd Maste * is an unsigned integer offset which when added to the low PC gives 142b00fe64fSEd Maste * the address of the first location past the last instruction 143b00fe64fSEd Maste * associated with the entity." 144b00fe64fSEd Maste * 145b00fe64fSEd Maste * DWARF4 spec, section 2.17.2. 146b00fe64fSEd Maste */ 147b00fe64fSEd Maste static int 148b00fe64fSEd Maste handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc) 149b00fe64fSEd Maste { 150b00fe64fSEd Maste Dwarf_Error de; 151b00fe64fSEd Maste Dwarf_Half form; 152b00fe64fSEd Maste Dwarf_Attribute at; 153b00fe64fSEd Maste int ret; 154b00fe64fSEd Maste 155b00fe64fSEd Maste ret = dwarf_attr(die, DW_AT_high_pc, &at, &de); 156b00fe64fSEd Maste if (ret == DW_DLV_ERROR) { 157b00fe64fSEd Maste warnx("dwarf_attr failed: %s", dwarf_errmsg(de)); 158b00fe64fSEd Maste return (ret); 159b00fe64fSEd Maste } 160b00fe64fSEd Maste ret = dwarf_whatform(at, &form, &de); 161b00fe64fSEd Maste if (ret == DW_DLV_ERROR) { 162b00fe64fSEd Maste warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 163b00fe64fSEd Maste return (ret); 164b00fe64fSEd Maste } 165b00fe64fSEd Maste if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT) 166b00fe64fSEd Maste *hipc += lopc; 167b00fe64fSEd Maste 168b00fe64fSEd Maste return (DW_DLV_OK); 169b00fe64fSEd Maste } 170b00fe64fSEd Maste 17195fd7f26SEd Maste static struct Func * 17295fd7f26SEd Maste search_func(struct CU *cu, Dwarf_Unsigned addr) 173a85fe12eSEd Maste { 17495fd7f26SEd Maste struct Func *f, *f0; 17595fd7f26SEd Maste Dwarf_Unsigned lopc, hipc, addr_base; 17695fd7f26SEd Maste int i; 17795fd7f26SEd Maste 17895fd7f26SEd Maste f0 = NULL; 17995fd7f26SEd Maste 18095fd7f26SEd Maste STAILQ_FOREACH(f, &cu->funclist, next) { 18195fd7f26SEd Maste if (f->ranges != NULL) { 18295fd7f26SEd Maste addr_base = 0; 18395fd7f26SEd Maste for (i = 0; i < f->ranges_cnt; i++) { 18495fd7f26SEd Maste if (f->ranges[i].dwr_type == DW_RANGES_END) 18595fd7f26SEd Maste break; 18695fd7f26SEd Maste if (f->ranges[i].dwr_type == 18795fd7f26SEd Maste DW_RANGES_ADDRESS_SELECTION) { 18895fd7f26SEd Maste addr_base = f->ranges[i].dwr_addr2; 18995fd7f26SEd Maste continue; 19095fd7f26SEd Maste } 19195fd7f26SEd Maste 19295fd7f26SEd Maste /* DW_RANGES_ENTRY */ 19395fd7f26SEd Maste lopc = f->ranges[i].dwr_addr1 + addr_base; 19495fd7f26SEd Maste hipc = f->ranges[i].dwr_addr2 + addr_base; 19595fd7f26SEd Maste if (addr >= lopc && addr < hipc) { 19695fd7f26SEd Maste if (f0 == NULL || 19795fd7f26SEd Maste (lopc >= f0->lopc && 19895fd7f26SEd Maste hipc <= f0->hipc)) { 19995fd7f26SEd Maste f0 = f; 20095fd7f26SEd Maste f0->lopc = lopc; 20195fd7f26SEd Maste f0->hipc = hipc; 20295fd7f26SEd Maste break; 20395fd7f26SEd Maste } 20495fd7f26SEd Maste } 20595fd7f26SEd Maste } 20695fd7f26SEd Maste } else if (addr >= f->lopc && addr < f->hipc) { 20795fd7f26SEd Maste if (f0 == NULL || 20895fd7f26SEd Maste (f->lopc >= f0->lopc && f->hipc <= f0->hipc)) 20995fd7f26SEd Maste f0 = f; 21095fd7f26SEd Maste } 21195fd7f26SEd Maste } 21295fd7f26SEd Maste 21395fd7f26SEd Maste return (f0); 21495fd7f26SEd Maste } 21595fd7f26SEd Maste 21695fd7f26SEd Maste static void 21795fd7f26SEd Maste collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu) 21895fd7f26SEd Maste { 21995fd7f26SEd Maste Dwarf_Die ret_die, abst_die, spec_die; 220a85fe12eSEd Maste Dwarf_Error de; 221a85fe12eSEd Maste Dwarf_Half tag; 22295fd7f26SEd Maste Dwarf_Unsigned lopc, hipc, ranges_off; 22395fd7f26SEd Maste Dwarf_Signed ranges_cnt; 224a85fe12eSEd Maste Dwarf_Off ref; 22595fd7f26SEd Maste Dwarf_Attribute abst_at, spec_at; 22695fd7f26SEd Maste Dwarf_Ranges *ranges; 22795fd7f26SEd Maste const char *funcname; 22895fd7f26SEd Maste struct Func *f; 22995fd7f26SEd Maste int found_ranges, ret; 230a85fe12eSEd Maste 23195fd7f26SEd Maste f = NULL; 23295fd7f26SEd Maste abst_die = spec_die = NULL; 233a85fe12eSEd Maste 234a85fe12eSEd Maste if (dwarf_tag(die, &tag, &de)) { 235a85fe12eSEd Maste warnx("dwarf_tag: %s", dwarf_errmsg(de)); 236a85fe12eSEd Maste goto cont_search; 237a85fe12eSEd Maste } 23895fd7f26SEd Maste if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point || 23995fd7f26SEd Maste tag == DW_TAG_inlined_subroutine) { 24095fd7f26SEd Maste /* 24195fd7f26SEd Maste * Function address range can be specified by either 24295fd7f26SEd Maste * a DW_AT_ranges attribute which points to a range list or 24395fd7f26SEd Maste * by a pair of DW_AT_low_pc and DW_AT_high_pc attributes. 24495fd7f26SEd Maste */ 24595fd7f26SEd Maste ranges = NULL; 24695fd7f26SEd Maste ranges_cnt = 0; 24795fd7f26SEd Maste found_ranges = 0; 24895fd7f26SEd Maste if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off, 24995fd7f26SEd Maste &de) == DW_DLV_OK && 25095fd7f26SEd Maste dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges, 25195fd7f26SEd Maste &ranges_cnt, NULL, &de) == DW_DLV_OK) { 25295fd7f26SEd Maste if (ranges != NULL && ranges_cnt > 0) { 25395fd7f26SEd Maste found_ranges = 1; 25495fd7f26SEd Maste goto get_func_name; 25595fd7f26SEd Maste } 25695fd7f26SEd Maste } 25795fd7f26SEd Maste 25895fd7f26SEd Maste /* 25995fd7f26SEd Maste * Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer 26095fd7f26SEd Maste * not found. 26195fd7f26SEd Maste */ 262a85fe12eSEd Maste if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) || 263a85fe12eSEd Maste dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de)) 264a85fe12eSEd Maste goto cont_search; 265b00fe64fSEd Maste if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK) 266b00fe64fSEd Maste goto cont_search; 267a85fe12eSEd Maste 26895fd7f26SEd Maste get_func_name: 26995fd7f26SEd Maste /* 27095fd7f26SEd Maste * Most common case the function name is stored in DW_AT_name 27195fd7f26SEd Maste * attribute. 27295fd7f26SEd Maste */ 27395fd7f26SEd Maste if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) == 27495fd7f26SEd Maste DW_DLV_OK) 27595fd7f26SEd Maste goto add_func; 276a85fe12eSEd Maste 27795fd7f26SEd Maste /* 27895fd7f26SEd Maste * For inlined function, the actual name is probably in the DIE 27995fd7f26SEd Maste * referenced by DW_AT_abstract_origin. (if present) 28095fd7f26SEd Maste */ 28195fd7f26SEd Maste if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) == 28295fd7f26SEd Maste DW_DLV_OK && 28395fd7f26SEd Maste dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK && 28495fd7f26SEd Maste dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK && 28595fd7f26SEd Maste dwarf_attrval_string(abst_die, DW_AT_name, &funcname, 28695fd7f26SEd Maste &de) == DW_DLV_OK) 28795fd7f26SEd Maste goto add_func; 288a85fe12eSEd Maste 289a85fe12eSEd Maste /* 290a85fe12eSEd Maste * If DW_AT_name is not present, but DW_AT_specification is 291a85fe12eSEd Maste * present, then probably the actual name is in the DIE 292a85fe12eSEd Maste * referenced by DW_AT_specification. 293a85fe12eSEd Maste */ 29495fd7f26SEd Maste if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) == 29595fd7f26SEd Maste DW_DLV_OK && 29695fd7f26SEd Maste dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK && 29795fd7f26SEd Maste dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK && 29895fd7f26SEd Maste dwarf_attrval_string(spec_die, DW_AT_name, &funcname, 29995fd7f26SEd Maste &de) == DW_DLV_OK) 30095fd7f26SEd Maste goto add_func; 301a85fe12eSEd Maste 302b6b6f9ccSEd Maste /* Skip if no name associated with this DIE. */ 30395fd7f26SEd Maste goto cont_search; 30495fd7f26SEd Maste 30595fd7f26SEd Maste add_func: 30695fd7f26SEd Maste if ((f = calloc(1, sizeof(*f))) == NULL) 30795fd7f26SEd Maste err(EXIT_FAILURE, "calloc"); 30895fd7f26SEd Maste if ((f->name = strdup(funcname)) == NULL) 30995fd7f26SEd Maste err(EXIT_FAILURE, "strdup"); 31095fd7f26SEd Maste if (found_ranges) { 31195fd7f26SEd Maste f->ranges = ranges; 31295fd7f26SEd Maste f->ranges_cnt = ranges_cnt; 31395fd7f26SEd Maste } else { 31495fd7f26SEd Maste f->lopc = lopc; 31595fd7f26SEd Maste f->hipc = hipc; 31695fd7f26SEd Maste } 31795fd7f26SEd Maste if (tag == DW_TAG_inlined_subroutine) { 31895fd7f26SEd Maste f->inlined_caller = parent; 31995fd7f26SEd Maste dwarf_attrval_unsigned(die, DW_AT_call_file, 32095fd7f26SEd Maste &f->call_file, &de); 32195fd7f26SEd Maste dwarf_attrval_unsigned(die, DW_AT_call_line, 32295fd7f26SEd Maste &f->call_line, &de); 32395fd7f26SEd Maste } 32495fd7f26SEd Maste STAILQ_INSERT_TAIL(&cu->funclist, f, next); 325a85fe12eSEd Maste } 326a85fe12eSEd Maste 327a85fe12eSEd Maste cont_search: 328a85fe12eSEd Maste 329a85fe12eSEd Maste /* Search children. */ 330a85fe12eSEd Maste ret = dwarf_child(die, &ret_die, &de); 331a85fe12eSEd Maste if (ret == DW_DLV_ERROR) 33295fd7f26SEd Maste warnx("dwarf_child: %s", dwarf_errmsg(de)); 33395fd7f26SEd Maste else if (ret == DW_DLV_OK) { 33495fd7f26SEd Maste if (f != NULL) 33595fd7f26SEd Maste collect_func(dbg, ret_die, f, cu); 33695fd7f26SEd Maste else 33795fd7f26SEd Maste collect_func(dbg, ret_die, parent, cu); 33895fd7f26SEd Maste } 339a85fe12eSEd Maste 340a85fe12eSEd Maste /* Search sibling. */ 341a85fe12eSEd Maste ret = dwarf_siblingof(dbg, die, &ret_die, &de); 342a85fe12eSEd Maste if (ret == DW_DLV_ERROR) 34395fd7f26SEd Maste warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 344a85fe12eSEd Maste else if (ret == DW_DLV_OK) 34595fd7f26SEd Maste collect_func(dbg, ret_die, parent, cu); 346656f49f8SEd Maste 34795fd7f26SEd Maste /* Cleanup */ 348656f49f8SEd Maste dwarf_dealloc(dbg, die, DW_DLA_DIE); 34995fd7f26SEd Maste 35095fd7f26SEd Maste if (abst_die != NULL) 35195fd7f26SEd Maste dwarf_dealloc(dbg, abst_die, DW_DLA_DIE); 35295fd7f26SEd Maste 35395fd7f26SEd Maste if (spec_die != NULL) 35495fd7f26SEd Maste dwarf_dealloc(dbg, spec_die, DW_DLA_DIE); 355a85fe12eSEd Maste } 356a85fe12eSEd Maste 357a85fe12eSEd Maste static void 35895fd7f26SEd Maste print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file, 35995fd7f26SEd Maste Dwarf_Unsigned call_line) 36095fd7f26SEd Maste { 36195fd7f26SEd Maste char demangled[1024]; 36295fd7f26SEd Maste char *file; 36395fd7f26SEd Maste 36495fd7f26SEd Maste if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles) 36595fd7f26SEd Maste file = cu->srcfiles[call_file - 1]; 36695fd7f26SEd Maste else 36795fd7f26SEd Maste file = unknown; 36895fd7f26SEd Maste 36995fd7f26SEd Maste if (pretty_print) 37095fd7f26SEd Maste printf(" (inlined by) "); 37195fd7f26SEd Maste 37295fd7f26SEd Maste if (func) { 37395fd7f26SEd Maste if (demangle && !elftc_demangle(f->name, demangled, 37495fd7f26SEd Maste sizeof(demangled), 0)) { 37595fd7f26SEd Maste if (pretty_print) 37695fd7f26SEd Maste printf("%s at ", demangled); 37795fd7f26SEd Maste else 37895fd7f26SEd Maste printf("%s\n", demangled); 37995fd7f26SEd Maste } else { 38095fd7f26SEd Maste if (pretty_print) 38195fd7f26SEd Maste printf("%s at ", f->name); 38295fd7f26SEd Maste else 38395fd7f26SEd Maste printf("%s\n", f->name); 38495fd7f26SEd Maste } 38595fd7f26SEd Maste } 386839529caSEd Maste (void) printf("%s:%ju\n", base ? basename(file) : file, 387839529caSEd Maste (uintmax_t) call_line); 38895fd7f26SEd Maste 38995fd7f26SEd Maste if (f->inlined_caller != NULL) 39095fd7f26SEd Maste print_inlines(cu, f->inlined_caller, f->call_file, 39195fd7f26SEd Maste f->call_line); 39295fd7f26SEd Maste } 39395fd7f26SEd Maste 394af843291SMark Johnston static struct CU * 395af843291SMark Johnston culookup(Dwarf_Unsigned addr) 396af843291SMark Johnston { 397af843291SMark Johnston struct CU find, *res; 398af843291SMark Johnston 399af843291SMark Johnston find.lopc = addr; 400*35beedaeSMark Johnston res = RB_NFIND(cutree, &cuhead, &find); 401af843291SMark Johnston if (res != NULL) { 402af843291SMark Johnston if (res->lopc != addr) 403*35beedaeSMark Johnston res = RB_PREV(cutree, &cuhead, res); 404af843291SMark Johnston if (res != NULL && addr >= res->lopc && addr < res->hipc) 405af843291SMark Johnston return (res); 406af843291SMark Johnston } else { 407*35beedaeSMark Johnston res = RB_MAX(cutree, &cuhead); 408af843291SMark Johnston if (res != NULL && addr >= res->lopc && addr < res->hipc) 409af843291SMark Johnston return (res); 410af843291SMark Johnston } 411af843291SMark Johnston return (NULL); 412af843291SMark Johnston } 413af843291SMark Johnston 41495fd7f26SEd Maste static void 41595fd7f26SEd Maste translate(Dwarf_Debug dbg, Elf *e, const char* addrstr) 416a85fe12eSEd Maste { 417656f49f8SEd Maste Dwarf_Die die, ret_die; 418a85fe12eSEd Maste Dwarf_Line *lbuf; 419a85fe12eSEd Maste Dwarf_Error de; 420a85fe12eSEd Maste Dwarf_Half tag; 421a85fe12eSEd Maste Dwarf_Unsigned lopc, hipc, addr, lineno, plineno; 422a85fe12eSEd Maste Dwarf_Signed lcount; 423a85fe12eSEd Maste Dwarf_Addr lineaddr, plineaddr; 42495fd7f26SEd Maste Dwarf_Off off; 42595fd7f26SEd Maste struct CU *cu; 42695fd7f26SEd Maste struct Func *f; 42795fd7f26SEd Maste const char *funcname; 428a85fe12eSEd Maste char *file, *file0, *pfile; 429a85fe12eSEd Maste char demangled[1024]; 43095fd7f26SEd Maste int ec, i, ret; 431a85fe12eSEd Maste 432a85fe12eSEd Maste addr = strtoull(addrstr, NULL, 16); 433a85fe12eSEd Maste addr += section_base; 434a85fe12eSEd Maste lineno = 0; 435a85fe12eSEd Maste file = unknown; 4367a2e729bSEd Maste die = NULL; 437af843291SMark Johnston ret = DW_DLV_OK; 438a85fe12eSEd Maste 439af843291SMark Johnston cu = culookup(addr); 440af843291SMark Johnston if (cu != NULL) { 441af843291SMark Johnston die = cu->die; 442af843291SMark Johnston goto status_ok; 443af843291SMark Johnston } 444af843291SMark Johnston 445af843291SMark Johnston while (true) { 446af843291SMark Johnston /* 447af843291SMark Johnston * We resume the CU scan from the last place we found a match. 448af843291SMark Johnston * Because when we have 2 sequential addresses, and the second 449af843291SMark Johnston * one is of the next CU, it is faster to just go to the next CU 450af843291SMark Johnston * instead of starting from the beginning. 451af843291SMark Johnston */ 452af843291SMark Johnston ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, 453af843291SMark Johnston &de); 454af843291SMark Johnston if (ret == DW_DLV_NO_ENTRY) { 455af843291SMark Johnston if (curlopc == ~0ULL) 456af843291SMark Johnston goto out; 457af843291SMark Johnston ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, 458af843291SMark Johnston NULL, &de); 459af843291SMark Johnston } 460a85fe12eSEd Maste die = NULL; 461656f49f8SEd Maste while (dwarf_siblingof(dbg, die, &ret_die, &de) == DW_DLV_OK) { 462656f49f8SEd Maste if (die != NULL) 463656f49f8SEd Maste dwarf_dealloc(dbg, die, DW_DLA_DIE); 464656f49f8SEd Maste die = ret_die; 465a85fe12eSEd Maste if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 466a85fe12eSEd Maste warnx("dwarf_tag failed: %s", 467a85fe12eSEd Maste dwarf_errmsg(de)); 468656f49f8SEd Maste goto next_cu; 469a85fe12eSEd Maste } 470656f49f8SEd Maste 471a85fe12eSEd Maste /* XXX: What about DW_TAG_partial_unit? */ 472a85fe12eSEd Maste if (tag == DW_TAG_compile_unit) 473a85fe12eSEd Maste break; 474a85fe12eSEd Maste } 475af843291SMark Johnston 476656f49f8SEd Maste if (ret_die == NULL) { 477a85fe12eSEd Maste warnx("could not find DW_TAG_compile_unit die"); 478656f49f8SEd Maste goto next_cu; 479a85fe12eSEd Maste } 48095fd7f26SEd Maste if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) == 48195fd7f26SEd Maste DW_DLV_OK) { 482af843291SMark Johnston if (lopc == curlopc) 483af843291SMark Johnston goto out; 48495fd7f26SEd Maste if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, 48595fd7f26SEd Maste &de) == DW_DLV_OK) { 486a85fe12eSEd Maste /* 48795fd7f26SEd Maste * Check if the address falls into the PC 48895fd7f26SEd Maste * range of this CU. 489a85fe12eSEd Maste */ 49095fd7f26SEd Maste if (handle_high_pc(die, lopc, &hipc) != 49195fd7f26SEd Maste DW_DLV_OK) 49295fd7f26SEd Maste goto out; 49395fd7f26SEd Maste } else { 49495fd7f26SEd Maste /* Assume ~0ULL if DW_AT_high_pc not present */ 49595fd7f26SEd Maste hipc = ~0ULL; 496a85fe12eSEd Maste } 497a85fe12eSEd Maste 49895fd7f26SEd Maste if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) { 49995fd7f26SEd Maste warnx("dwarf_dieoffset failed: %s", 50095fd7f26SEd Maste dwarf_errmsg(de)); 50195fd7f26SEd Maste goto out; 50295fd7f26SEd Maste } 503af843291SMark Johnston 504af843291SMark Johnston if (addr >= lopc && addr < hipc) { 50595fd7f26SEd Maste if ((cu = calloc(1, sizeof(*cu))) == NULL) 50695fd7f26SEd Maste err(EXIT_FAILURE, "calloc"); 50795fd7f26SEd Maste cu->off = off; 50895fd7f26SEd Maste cu->lopc = lopc; 50995fd7f26SEd Maste cu->hipc = hipc; 510af843291SMark Johnston cu->die = die; 51195fd7f26SEd Maste STAILQ_INIT(&cu->funclist); 512*35beedaeSMark Johnston RB_INSERT(cutree, &cuhead, cu); 51395fd7f26SEd Maste 514af843291SMark Johnston curlopc = lopc; 51595fd7f26SEd Maste break; 51695fd7f26SEd Maste } 517af843291SMark Johnston } 51895fd7f26SEd Maste next_cu: 51995fd7f26SEd Maste if (die != NULL) { 52095fd7f26SEd Maste dwarf_dealloc(dbg, die, DW_DLA_DIE); 52195fd7f26SEd Maste die = NULL; 52295fd7f26SEd Maste } 52395fd7f26SEd Maste } 52495fd7f26SEd Maste 52595fd7f26SEd Maste if (ret != DW_DLV_OK || die == NULL) 52695fd7f26SEd Maste goto out; 52795fd7f26SEd Maste 528af843291SMark Johnston status_ok: 529c9dbb1ccSEd Maste switch (dwarf_srclines(die, &lbuf, &lcount, &de)) { 530c9dbb1ccSEd Maste case DW_DLV_OK: 531c9dbb1ccSEd Maste break; 532c9dbb1ccSEd Maste case DW_DLV_NO_ENTRY: 533656f49f8SEd Maste /* If a CU lacks debug info, just skip it. */ 53495fd7f26SEd Maste goto out; 535c9dbb1ccSEd Maste default: 536a85fe12eSEd Maste warnx("dwarf_srclines: %s", dwarf_errmsg(de)); 537a85fe12eSEd Maste goto out; 538a85fe12eSEd Maste } 539a85fe12eSEd Maste 540a85fe12eSEd Maste plineaddr = ~0ULL; 541a85fe12eSEd Maste plineno = 0; 542a85fe12eSEd Maste pfile = unknown; 543a85fe12eSEd Maste for (i = 0; i < lcount; i++) { 544a85fe12eSEd Maste if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) { 54595fd7f26SEd Maste warnx("dwarf_lineaddr: %s", dwarf_errmsg(de)); 546a85fe12eSEd Maste goto out; 547a85fe12eSEd Maste } 548a85fe12eSEd Maste if (dwarf_lineno(lbuf[i], &lineno, &de)) { 54995fd7f26SEd Maste warnx("dwarf_lineno: %s", dwarf_errmsg(de)); 550a85fe12eSEd Maste goto out; 551a85fe12eSEd Maste } 552a85fe12eSEd Maste if (dwarf_linesrc(lbuf[i], &file0, &de)) { 55395fd7f26SEd Maste warnx("dwarf_linesrc: %s", dwarf_errmsg(de)); 554a85fe12eSEd Maste } else 555a85fe12eSEd Maste file = file0; 556a85fe12eSEd Maste if (addr == lineaddr) 557a85fe12eSEd Maste goto out; 558a85fe12eSEd Maste else if (addr < lineaddr && addr > plineaddr) { 559a85fe12eSEd Maste lineno = plineno; 560a85fe12eSEd Maste file = pfile; 561a85fe12eSEd Maste goto out; 562a85fe12eSEd Maste } 563a85fe12eSEd Maste plineaddr = lineaddr; 564a85fe12eSEd Maste plineno = lineno; 565a85fe12eSEd Maste pfile = file; 566a85fe12eSEd Maste } 567a85fe12eSEd Maste 568a85fe12eSEd Maste out: 56995fd7f26SEd Maste f = NULL; 570a85fe12eSEd Maste funcname = NULL; 57195fd7f26SEd Maste if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) { 57295fd7f26SEd Maste if (cu->srcfiles == NULL) 57395fd7f26SEd Maste if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles, 57495fd7f26SEd Maste &de)) 57595fd7f26SEd Maste warnx("dwarf_srcfiles: %s", dwarf_errmsg(de)); 57695fd7f26SEd Maste if (STAILQ_EMPTY(&cu->funclist)) { 57795fd7f26SEd Maste collect_func(dbg, die, NULL, cu); 578656f49f8SEd Maste die = NULL; 579656f49f8SEd Maste } 58095fd7f26SEd Maste f = search_func(cu, addr); 58195fd7f26SEd Maste if (f != NULL) 58295fd7f26SEd Maste funcname = f->name; 58395fd7f26SEd Maste } 58495fd7f26SEd Maste 58595fd7f26SEd Maste if (print_addr) { 58695fd7f26SEd Maste if ((ec = gelf_getclass(e)) == ELFCLASSNONE) { 58795fd7f26SEd Maste warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 58895fd7f26SEd Maste ec = ELFCLASS64; 58995fd7f26SEd Maste } 59095fd7f26SEd Maste if (ec == ELFCLASS32) { 59195fd7f26SEd Maste if (pretty_print) 59295fd7f26SEd Maste printf("0x%08jx: ", (uintmax_t) addr); 59395fd7f26SEd Maste else 59495fd7f26SEd Maste printf("0x%08jx\n", (uintmax_t) addr); 59595fd7f26SEd Maste } else { 59695fd7f26SEd Maste if (pretty_print) 59795fd7f26SEd Maste printf("0x%016jx: ", (uintmax_t) addr); 59895fd7f26SEd Maste else 59995fd7f26SEd Maste printf("0x%016jx\n", (uintmax_t) addr); 60095fd7f26SEd Maste } 60195fd7f26SEd Maste } 602a85fe12eSEd Maste 603a85fe12eSEd Maste if (func) { 604a85fe12eSEd Maste if (funcname == NULL) 60595fd7f26SEd Maste funcname = unknown; 60695fd7f26SEd Maste if (demangle && !elftc_demangle(funcname, demangled, 60795fd7f26SEd Maste sizeof(demangled), 0)) { 60895fd7f26SEd Maste if (pretty_print) 60995fd7f26SEd Maste printf("%s at ", demangled); 61095fd7f26SEd Maste else 611a85fe12eSEd Maste printf("%s\n", demangled); 61295fd7f26SEd Maste } else { 61395fd7f26SEd Maste if (pretty_print) 61495fd7f26SEd Maste printf("%s at ", funcname); 615a85fe12eSEd Maste else 616a85fe12eSEd Maste printf("%s\n", funcname); 61795fd7f26SEd Maste } 618a85fe12eSEd Maste } 619a85fe12eSEd Maste 620839529caSEd Maste (void) printf("%s:%ju\n", base ? basename(file) : file, 621839529caSEd Maste (uintmax_t) lineno); 622a85fe12eSEd Maste 62395fd7f26SEd Maste if (ret == DW_DLV_OK && inlines && cu != NULL && 62495fd7f26SEd Maste cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL) 62595fd7f26SEd Maste print_inlines(cu, f->inlined_caller, f->call_file, 62695fd7f26SEd Maste f->call_line); 627a85fe12eSEd Maste } 628a85fe12eSEd Maste 629a85fe12eSEd Maste static void 630a85fe12eSEd Maste find_section_base(const char *exe, Elf *e, const char *section) 631a85fe12eSEd Maste { 632a85fe12eSEd Maste Dwarf_Addr off; 633a85fe12eSEd Maste Elf_Scn *scn; 634a85fe12eSEd Maste GElf_Ehdr eh; 635a85fe12eSEd Maste GElf_Shdr sh; 636a85fe12eSEd Maste size_t shstrndx; 637a85fe12eSEd Maste int elferr; 638a85fe12eSEd Maste const char *name; 639a85fe12eSEd Maste 640a85fe12eSEd Maste if (gelf_getehdr(e, &eh) != &eh) { 641a85fe12eSEd Maste warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 642a85fe12eSEd Maste return; 643a85fe12eSEd Maste } 644a85fe12eSEd Maste 645a85fe12eSEd Maste if (!elf_getshstrndx(e, &shstrndx)) { 646a85fe12eSEd Maste warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 647a85fe12eSEd Maste return; 648a85fe12eSEd Maste } 649a85fe12eSEd Maste 650a85fe12eSEd Maste (void) elf_errno(); 651a85fe12eSEd Maste off = 0; 652a85fe12eSEd Maste scn = NULL; 653a85fe12eSEd Maste while ((scn = elf_nextscn(e, scn)) != NULL) { 654a85fe12eSEd Maste if (gelf_getshdr(scn, &sh) == NULL) { 655a85fe12eSEd Maste warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 656a85fe12eSEd Maste continue; 657a85fe12eSEd Maste } 658a85fe12eSEd Maste if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL) 659a85fe12eSEd Maste goto next; 660a85fe12eSEd Maste if (!strcmp(section, name)) { 661a85fe12eSEd Maste if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) { 662a85fe12eSEd Maste /* 663a85fe12eSEd Maste * For executables, section base is the virtual 664a85fe12eSEd Maste * address of the specified section. 665a85fe12eSEd Maste */ 666a85fe12eSEd Maste section_base = sh.sh_addr; 667a85fe12eSEd Maste } else if (eh.e_type == ET_REL) { 668a85fe12eSEd Maste /* 669a85fe12eSEd Maste * For relocatables, section base is the 670a85fe12eSEd Maste * relative offset of the specified section 671a85fe12eSEd Maste * to the start of the first section. 672a85fe12eSEd Maste */ 673a85fe12eSEd Maste section_base = off; 674a85fe12eSEd Maste } else 675a85fe12eSEd Maste warnx("unknown e_type %u", eh.e_type); 676a85fe12eSEd Maste return; 677a85fe12eSEd Maste } 678a85fe12eSEd Maste next: 679a85fe12eSEd Maste off += sh.sh_size; 680a85fe12eSEd Maste } 681a85fe12eSEd Maste elferr = elf_errno(); 682a85fe12eSEd Maste if (elferr != 0) 683a85fe12eSEd Maste warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 684a85fe12eSEd Maste 685a85fe12eSEd Maste errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section); 686a85fe12eSEd Maste } 687a85fe12eSEd Maste 688a85fe12eSEd Maste int 689a85fe12eSEd Maste main(int argc, char **argv) 690a85fe12eSEd Maste { 6911d954fedSMark Johnston cap_rights_t rights; 692a85fe12eSEd Maste Elf *e; 693a85fe12eSEd Maste Dwarf_Debug dbg; 694a85fe12eSEd Maste Dwarf_Error de; 695a85fe12eSEd Maste const char *exe, *section; 696a85fe12eSEd Maste char line[1024]; 697a85fe12eSEd Maste int fd, i, opt; 698a85fe12eSEd Maste 699a85fe12eSEd Maste exe = NULL; 700a85fe12eSEd Maste section = NULL; 70195fd7f26SEd Maste while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts, 70295fd7f26SEd Maste NULL)) != -1) { 703a85fe12eSEd Maste switch (opt) { 70495fd7f26SEd Maste case 'a': 70595fd7f26SEd Maste print_addr = 1; 70695fd7f26SEd Maste break; 707a85fe12eSEd Maste case 'b': 708a85fe12eSEd Maste /* ignored */ 709a85fe12eSEd Maste break; 710a85fe12eSEd Maste case 'C': 711a85fe12eSEd Maste demangle = 1; 712a85fe12eSEd Maste break; 713a85fe12eSEd Maste case 'e': 714a85fe12eSEd Maste exe = optarg; 715a85fe12eSEd Maste break; 716a85fe12eSEd Maste case 'f': 717a85fe12eSEd Maste func = 1; 718a85fe12eSEd Maste break; 71995fd7f26SEd Maste case 'i': 72095fd7f26SEd Maste inlines = 1; 72195fd7f26SEd Maste break; 722a85fe12eSEd Maste case 'j': 723a85fe12eSEd Maste section = optarg; 724a85fe12eSEd Maste break; 72595fd7f26SEd Maste case 'p': 72695fd7f26SEd Maste pretty_print = 1; 72795fd7f26SEd Maste break; 728a85fe12eSEd Maste case 's': 729a85fe12eSEd Maste base = 1; 730a85fe12eSEd Maste break; 731a85fe12eSEd Maste case 'H': 732a85fe12eSEd Maste usage(); 733a85fe12eSEd Maste case 'V': 734a85fe12eSEd Maste version(); 735a85fe12eSEd Maste default: 736a85fe12eSEd Maste usage(); 737a85fe12eSEd Maste } 738a85fe12eSEd Maste } 739a85fe12eSEd Maste 740a85fe12eSEd Maste argv += optind; 741a85fe12eSEd Maste argc -= optind; 742a85fe12eSEd Maste 743a85fe12eSEd Maste if (exe == NULL) 744a85fe12eSEd Maste exe = "a.out"; 745a85fe12eSEd Maste 746a85fe12eSEd Maste if ((fd = open(exe, O_RDONLY)) < 0) 747a85fe12eSEd Maste err(EXIT_FAILURE, "%s", exe); 748a85fe12eSEd Maste 7491d954fedSMark Johnston if (caph_rights_limit(fd, cap_rights_init(&rights, CAP_FSTAT, 7501d954fedSMark Johnston CAP_MMAP_R)) < 0) 7511d954fedSMark Johnston errx(EXIT_FAILURE, "caph_rights_limit"); 7521d954fedSMark Johnston 7531d954fedSMark Johnston caph_cache_catpages(); 7541d954fedSMark Johnston if (caph_limit_stdio() < 0) 7551d954fedSMark Johnston errx(EXIT_FAILURE, "failed to limit stdio rights"); 7561d954fedSMark Johnston if (caph_enter() < 0) 7571d954fedSMark Johnston errx(EXIT_FAILURE, "failed to enter capability mode"); 7581d954fedSMark Johnston 759a85fe12eSEd Maste if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de)) 760a85fe12eSEd Maste errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de)); 761a85fe12eSEd Maste 762a85fe12eSEd Maste if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK) 763a85fe12eSEd Maste errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de)); 764a85fe12eSEd Maste 765a85fe12eSEd Maste if (section) 766a85fe12eSEd Maste find_section_base(exe, e, section); 767a85fe12eSEd Maste else 768a85fe12eSEd Maste section_base = 0; 769a85fe12eSEd Maste 770a85fe12eSEd Maste if (argc > 0) 771a85fe12eSEd Maste for (i = 0; i < argc; i++) 77295fd7f26SEd Maste translate(dbg, e, argv[i]); 773bee2765cSEd Maste else { 774bee2765cSEd Maste setvbuf(stdout, NULL, _IOLBF, 0); 775bee2765cSEd Maste while (fgets(line, sizeof(line), stdin) != NULL) 77695fd7f26SEd Maste translate(dbg, e, line); 777a7265433SEd Maste } 778a85fe12eSEd Maste 779a85fe12eSEd Maste dwarf_finish(dbg, &de); 780a85fe12eSEd Maste 781a85fe12eSEd Maste (void) elf_end(e); 782a85fe12eSEd Maste 783a85fe12eSEd Maste exit(0); 784a85fe12eSEd Maste } 785