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> 28a85fe12eSEd Maste #include <dwarf.h> 29a85fe12eSEd Maste #include <err.h> 30a85fe12eSEd Maste #include <fcntl.h> 31a85fe12eSEd Maste #include <gelf.h> 32a85fe12eSEd Maste #include <getopt.h> 33a85fe12eSEd Maste #include <libdwarf.h> 34a85fe12eSEd Maste #include <libelftc.h> 35a85fe12eSEd Maste #include <libgen.h> 36a85fe12eSEd Maste #include <stdio.h> 37a85fe12eSEd Maste #include <stdlib.h> 38a85fe12eSEd Maste #include <string.h> 39a85fe12eSEd Maste 4095fd7f26SEd Maste #include "uthash.h" 41a85fe12eSEd Maste #include "_elftc.h" 42a85fe12eSEd Maste 43*839529caSEd Maste ELFTC_VCSID("$Id: addr2line.c 3273 2015-12-11 21:38:57Z kaiwang27 $"); 4495fd7f26SEd Maste 4595fd7f26SEd Maste struct Func { 4695fd7f26SEd Maste char *name; 4795fd7f26SEd Maste Dwarf_Unsigned lopc; 4895fd7f26SEd Maste Dwarf_Unsigned hipc; 4995fd7f26SEd Maste Dwarf_Unsigned call_file; 5095fd7f26SEd Maste Dwarf_Unsigned call_line; 5195fd7f26SEd Maste Dwarf_Ranges *ranges; 5295fd7f26SEd Maste Dwarf_Signed ranges_cnt; 5395fd7f26SEd Maste struct Func *inlined_caller; 5495fd7f26SEd Maste STAILQ_ENTRY(Func) next; 5595fd7f26SEd Maste }; 5695fd7f26SEd Maste 5795fd7f26SEd Maste struct CU { 5895fd7f26SEd Maste Dwarf_Off off; 5995fd7f26SEd Maste Dwarf_Unsigned lopc; 6095fd7f26SEd Maste Dwarf_Unsigned hipc; 6195fd7f26SEd Maste char **srcfiles; 6295fd7f26SEd Maste Dwarf_Signed nsrcfiles; 6395fd7f26SEd Maste STAILQ_HEAD(, Func) funclist; 6495fd7f26SEd Maste UT_hash_handle hh; 6595fd7f26SEd Maste }; 66a85fe12eSEd Maste 67a85fe12eSEd Maste static struct option longopts[] = { 6895fd7f26SEd Maste {"addresses", no_argument, NULL, 'a'}, 69a85fe12eSEd Maste {"target" , required_argument, NULL, 'b'}, 70a85fe12eSEd Maste {"demangle", no_argument, NULL, 'C'}, 71a85fe12eSEd Maste {"exe", required_argument, NULL, 'e'}, 72a85fe12eSEd Maste {"functions", no_argument, NULL, 'f'}, 7395fd7f26SEd Maste {"inlines", no_argument, NULL, 'i'}, 74a85fe12eSEd Maste {"section", required_argument, NULL, 'j'}, 7595fd7f26SEd Maste {"pretty-print", no_argument, NULL, 'p'}, 76a85fe12eSEd Maste {"basename", no_argument, NULL, 's'}, 77a85fe12eSEd Maste {"help", no_argument, NULL, 'H'}, 78a85fe12eSEd Maste {"version", no_argument, NULL, 'V'}, 79a85fe12eSEd Maste {NULL, 0, NULL, 0} 80a85fe12eSEd Maste }; 8195fd7f26SEd Maste static int demangle, func, base, inlines, print_addr, pretty_print; 82a85fe12eSEd Maste static char unknown[] = { '?', '?', '\0' }; 83a85fe12eSEd Maste static Dwarf_Addr section_base; 8495fd7f26SEd Maste static struct CU *culist; 85a85fe12eSEd Maste 86a85fe12eSEd Maste #define USAGE_MESSAGE "\ 87a85fe12eSEd Maste Usage: %s [options] hexaddress...\n\ 88a85fe12eSEd Maste Map program addresses to source file names and line numbers.\n\n\ 89a85fe12eSEd Maste Options:\n\ 9095fd7f26SEd Maste -a | --addresses Display address prior to line number info.\n\ 91a85fe12eSEd Maste -b TGT | --target=TGT (Accepted but ignored).\n\ 92656f49f8SEd Maste -e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\ 93a85fe12eSEd Maste -f | --functions Display function names.\n\ 9495fd7f26SEd Maste -i | --inlines Display caller info for inlined functions.\n\ 95a85fe12eSEd Maste -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\ 9695fd7f26SEd Maste -p | --pretty-print Display line number info and function name\n\ 9795fd7f26SEd Maste in human readable manner.\n\ 98a85fe12eSEd Maste -s | --basename Only show the base name for each file name.\n\ 99a85fe12eSEd Maste -C | --demangle Demangle C++ names.\n\ 100a85fe12eSEd Maste -H | --help Print a help message.\n\ 101a85fe12eSEd Maste -V | --version Print a version identifier and exit.\n" 102a85fe12eSEd Maste 103a85fe12eSEd Maste static void 104a85fe12eSEd Maste usage(void) 105a85fe12eSEd Maste { 106a85fe12eSEd Maste (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 107a85fe12eSEd Maste exit(1); 108a85fe12eSEd Maste } 109a85fe12eSEd Maste 110a85fe12eSEd Maste static void 111a85fe12eSEd Maste version(void) 112a85fe12eSEd Maste { 113a85fe12eSEd Maste 114a85fe12eSEd Maste fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); 115a85fe12eSEd Maste exit(0); 116a85fe12eSEd Maste } 117a85fe12eSEd Maste 118b00fe64fSEd Maste /* 119b00fe64fSEd Maste * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't 120b00fe64fSEd Maste * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1) 121b00fe64fSEd Maste * generate DW_AT_high_pc as an offset from DW_AT_low_pc. 122b00fe64fSEd Maste * 123b00fe64fSEd Maste * "If the value of the DW_AT_high_pc is of class address, it is the 124b00fe64fSEd Maste * relocated address of the first location past the last instruction 125b00fe64fSEd Maste * associated with the entity; if it is of class constant, the value 126b00fe64fSEd Maste * is an unsigned integer offset which when added to the low PC gives 127b00fe64fSEd Maste * the address of the first location past the last instruction 128b00fe64fSEd Maste * associated with the entity." 129b00fe64fSEd Maste * 130b00fe64fSEd Maste * DWARF4 spec, section 2.17.2. 131b00fe64fSEd Maste */ 132b00fe64fSEd Maste static int 133b00fe64fSEd Maste handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc) 134b00fe64fSEd Maste { 135b00fe64fSEd Maste Dwarf_Error de; 136b00fe64fSEd Maste Dwarf_Half form; 137b00fe64fSEd Maste Dwarf_Attribute at; 138b00fe64fSEd Maste int ret; 139b00fe64fSEd Maste 140b00fe64fSEd Maste ret = dwarf_attr(die, DW_AT_high_pc, &at, &de); 141b00fe64fSEd Maste if (ret == DW_DLV_ERROR) { 142b00fe64fSEd Maste warnx("dwarf_attr failed: %s", dwarf_errmsg(de)); 143b00fe64fSEd Maste return (ret); 144b00fe64fSEd Maste } 145b00fe64fSEd Maste ret = dwarf_whatform(at, &form, &de); 146b00fe64fSEd Maste if (ret == DW_DLV_ERROR) { 147b00fe64fSEd Maste warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 148b00fe64fSEd Maste return (ret); 149b00fe64fSEd Maste } 150b00fe64fSEd Maste if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT) 151b00fe64fSEd Maste *hipc += lopc; 152b00fe64fSEd Maste 153b00fe64fSEd Maste return (DW_DLV_OK); 154b00fe64fSEd Maste } 155b00fe64fSEd Maste 15695fd7f26SEd Maste static struct Func * 15795fd7f26SEd Maste search_func(struct CU *cu, Dwarf_Unsigned addr) 158a85fe12eSEd Maste { 15995fd7f26SEd Maste struct Func *f, *f0; 16095fd7f26SEd Maste Dwarf_Unsigned lopc, hipc, addr_base; 16195fd7f26SEd Maste int i; 16295fd7f26SEd Maste 16395fd7f26SEd Maste f0 = NULL; 16495fd7f26SEd Maste 16595fd7f26SEd Maste STAILQ_FOREACH(f, &cu->funclist, next) { 16695fd7f26SEd Maste if (f->ranges != NULL) { 16795fd7f26SEd Maste addr_base = 0; 16895fd7f26SEd Maste for (i = 0; i < f->ranges_cnt; i++) { 16995fd7f26SEd Maste if (f->ranges[i].dwr_type == DW_RANGES_END) 17095fd7f26SEd Maste break; 17195fd7f26SEd Maste if (f->ranges[i].dwr_type == 17295fd7f26SEd Maste DW_RANGES_ADDRESS_SELECTION) { 17395fd7f26SEd Maste addr_base = f->ranges[i].dwr_addr2; 17495fd7f26SEd Maste continue; 17595fd7f26SEd Maste } 17695fd7f26SEd Maste 17795fd7f26SEd Maste /* DW_RANGES_ENTRY */ 17895fd7f26SEd Maste lopc = f->ranges[i].dwr_addr1 + addr_base; 17995fd7f26SEd Maste hipc = f->ranges[i].dwr_addr2 + addr_base; 18095fd7f26SEd Maste if (addr >= lopc && addr < hipc) { 18195fd7f26SEd Maste if (f0 == NULL || 18295fd7f26SEd Maste (lopc >= f0->lopc && 18395fd7f26SEd Maste hipc <= f0->hipc)) { 18495fd7f26SEd Maste f0 = f; 18595fd7f26SEd Maste f0->lopc = lopc; 18695fd7f26SEd Maste f0->hipc = hipc; 18795fd7f26SEd Maste break; 18895fd7f26SEd Maste } 18995fd7f26SEd Maste } 19095fd7f26SEd Maste } 19195fd7f26SEd Maste } else if (addr >= f->lopc && addr < f->hipc) { 19295fd7f26SEd Maste if (f0 == NULL || 19395fd7f26SEd Maste (f->lopc >= f0->lopc && f->hipc <= f0->hipc)) 19495fd7f26SEd Maste f0 = f; 19595fd7f26SEd Maste } 19695fd7f26SEd Maste } 19795fd7f26SEd Maste 19895fd7f26SEd Maste return (f0); 19995fd7f26SEd Maste } 20095fd7f26SEd Maste 20195fd7f26SEd Maste static void 20295fd7f26SEd Maste collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu) 20395fd7f26SEd Maste { 20495fd7f26SEd Maste Dwarf_Die ret_die, abst_die, spec_die; 205a85fe12eSEd Maste Dwarf_Error de; 206a85fe12eSEd Maste Dwarf_Half tag; 20795fd7f26SEd Maste Dwarf_Unsigned lopc, hipc, ranges_off; 20895fd7f26SEd Maste Dwarf_Signed ranges_cnt; 209a85fe12eSEd Maste Dwarf_Off ref; 21095fd7f26SEd Maste Dwarf_Attribute abst_at, spec_at; 21195fd7f26SEd Maste Dwarf_Ranges *ranges; 21295fd7f26SEd Maste const char *funcname; 21395fd7f26SEd Maste struct Func *f; 21495fd7f26SEd Maste int found_ranges, ret; 215a85fe12eSEd Maste 21695fd7f26SEd Maste f = NULL; 21795fd7f26SEd Maste abst_die = spec_die = NULL; 218a85fe12eSEd Maste 219a85fe12eSEd Maste if (dwarf_tag(die, &tag, &de)) { 220a85fe12eSEd Maste warnx("dwarf_tag: %s", dwarf_errmsg(de)); 221a85fe12eSEd Maste goto cont_search; 222a85fe12eSEd Maste } 22395fd7f26SEd Maste if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point || 22495fd7f26SEd Maste tag == DW_TAG_inlined_subroutine) { 22595fd7f26SEd Maste /* 22695fd7f26SEd Maste * Function address range can be specified by either 22795fd7f26SEd Maste * a DW_AT_ranges attribute which points to a range list or 22895fd7f26SEd Maste * by a pair of DW_AT_low_pc and DW_AT_high_pc attributes. 22995fd7f26SEd Maste */ 23095fd7f26SEd Maste ranges = NULL; 23195fd7f26SEd Maste ranges_cnt = 0; 23295fd7f26SEd Maste found_ranges = 0; 23395fd7f26SEd Maste if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off, 23495fd7f26SEd Maste &de) == DW_DLV_OK && 23595fd7f26SEd Maste dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges, 23695fd7f26SEd Maste &ranges_cnt, NULL, &de) == DW_DLV_OK) { 23795fd7f26SEd Maste if (ranges != NULL && ranges_cnt > 0) { 23895fd7f26SEd Maste found_ranges = 1; 23995fd7f26SEd Maste goto get_func_name; 24095fd7f26SEd Maste } 24195fd7f26SEd Maste } 24295fd7f26SEd Maste 24395fd7f26SEd Maste /* 24495fd7f26SEd Maste * Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer 24595fd7f26SEd Maste * not found. 24695fd7f26SEd Maste */ 247a85fe12eSEd Maste if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) || 248a85fe12eSEd Maste dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de)) 249a85fe12eSEd Maste goto cont_search; 250b00fe64fSEd Maste if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK) 251b00fe64fSEd Maste goto cont_search; 252a85fe12eSEd Maste 25395fd7f26SEd Maste get_func_name: 25495fd7f26SEd Maste /* 25595fd7f26SEd Maste * Most common case the function name is stored in DW_AT_name 25695fd7f26SEd Maste * attribute. 25795fd7f26SEd Maste */ 25895fd7f26SEd Maste if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) == 25995fd7f26SEd Maste DW_DLV_OK) 26095fd7f26SEd Maste goto add_func; 261a85fe12eSEd Maste 26295fd7f26SEd Maste /* 26395fd7f26SEd Maste * For inlined function, the actual name is probably in the DIE 26495fd7f26SEd Maste * referenced by DW_AT_abstract_origin. (if present) 26595fd7f26SEd Maste */ 26695fd7f26SEd Maste if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) == 26795fd7f26SEd Maste DW_DLV_OK && 26895fd7f26SEd Maste dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK && 26995fd7f26SEd Maste dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK && 27095fd7f26SEd Maste dwarf_attrval_string(abst_die, DW_AT_name, &funcname, 27195fd7f26SEd Maste &de) == DW_DLV_OK) 27295fd7f26SEd Maste goto add_func; 273a85fe12eSEd Maste 274a85fe12eSEd Maste /* 275a85fe12eSEd Maste * If DW_AT_name is not present, but DW_AT_specification is 276a85fe12eSEd Maste * present, then probably the actual name is in the DIE 277a85fe12eSEd Maste * referenced by DW_AT_specification. 278a85fe12eSEd Maste */ 27995fd7f26SEd Maste if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) == 28095fd7f26SEd Maste DW_DLV_OK && 28195fd7f26SEd Maste dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK && 28295fd7f26SEd Maste dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK && 28395fd7f26SEd Maste dwarf_attrval_string(spec_die, DW_AT_name, &funcname, 28495fd7f26SEd Maste &de) == DW_DLV_OK) 28595fd7f26SEd Maste goto add_func; 286a85fe12eSEd Maste 28795fd7f26SEd Maste /* Skip if no name assoicated with this DIE. */ 28895fd7f26SEd Maste goto cont_search; 28995fd7f26SEd Maste 29095fd7f26SEd Maste add_func: 29195fd7f26SEd Maste if ((f = calloc(1, sizeof(*f))) == NULL) 29295fd7f26SEd Maste err(EXIT_FAILURE, "calloc"); 29395fd7f26SEd Maste if ((f->name = strdup(funcname)) == NULL) 29495fd7f26SEd Maste err(EXIT_FAILURE, "strdup"); 29595fd7f26SEd Maste if (found_ranges) { 29695fd7f26SEd Maste f->ranges = ranges; 29795fd7f26SEd Maste f->ranges_cnt = ranges_cnt; 29895fd7f26SEd Maste } else { 29995fd7f26SEd Maste f->lopc = lopc; 30095fd7f26SEd Maste f->hipc = hipc; 30195fd7f26SEd Maste } 30295fd7f26SEd Maste if (tag == DW_TAG_inlined_subroutine) { 30395fd7f26SEd Maste f->inlined_caller = parent; 30495fd7f26SEd Maste dwarf_attrval_unsigned(die, DW_AT_call_file, 30595fd7f26SEd Maste &f->call_file, &de); 30695fd7f26SEd Maste dwarf_attrval_unsigned(die, DW_AT_call_line, 30795fd7f26SEd Maste &f->call_line, &de); 30895fd7f26SEd Maste } 30995fd7f26SEd Maste STAILQ_INSERT_TAIL(&cu->funclist, f, next); 310a85fe12eSEd Maste } 311a85fe12eSEd Maste 312a85fe12eSEd Maste cont_search: 313a85fe12eSEd Maste 314a85fe12eSEd Maste /* Search children. */ 315a85fe12eSEd Maste ret = dwarf_child(die, &ret_die, &de); 316a85fe12eSEd Maste if (ret == DW_DLV_ERROR) 31795fd7f26SEd Maste warnx("dwarf_child: %s", dwarf_errmsg(de)); 31895fd7f26SEd Maste else if (ret == DW_DLV_OK) { 31995fd7f26SEd Maste if (f != NULL) 32095fd7f26SEd Maste collect_func(dbg, ret_die, f, cu); 32195fd7f26SEd Maste else 32295fd7f26SEd Maste collect_func(dbg, ret_die, parent, cu); 32395fd7f26SEd Maste } 324a85fe12eSEd Maste 325a85fe12eSEd Maste /* Search sibling. */ 326a85fe12eSEd Maste ret = dwarf_siblingof(dbg, die, &ret_die, &de); 327a85fe12eSEd Maste if (ret == DW_DLV_ERROR) 32895fd7f26SEd Maste warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 329a85fe12eSEd Maste else if (ret == DW_DLV_OK) 33095fd7f26SEd Maste collect_func(dbg, ret_die, parent, cu); 331656f49f8SEd Maste 33295fd7f26SEd Maste /* Cleanup */ 333656f49f8SEd Maste dwarf_dealloc(dbg, die, DW_DLA_DIE); 33495fd7f26SEd Maste 33595fd7f26SEd Maste if (abst_die != NULL) 33695fd7f26SEd Maste dwarf_dealloc(dbg, abst_die, DW_DLA_DIE); 33795fd7f26SEd Maste 33895fd7f26SEd Maste if (spec_die != NULL) 33995fd7f26SEd Maste dwarf_dealloc(dbg, spec_die, DW_DLA_DIE); 340a85fe12eSEd Maste } 341a85fe12eSEd Maste 342a85fe12eSEd Maste static void 34395fd7f26SEd Maste print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file, 34495fd7f26SEd Maste Dwarf_Unsigned call_line) 34595fd7f26SEd Maste { 34695fd7f26SEd Maste char demangled[1024]; 34795fd7f26SEd Maste char *file; 34895fd7f26SEd Maste 34995fd7f26SEd Maste if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles) 35095fd7f26SEd Maste file = cu->srcfiles[call_file - 1]; 35195fd7f26SEd Maste else 35295fd7f26SEd Maste file = unknown; 35395fd7f26SEd Maste 35495fd7f26SEd Maste if (pretty_print) 35595fd7f26SEd Maste printf(" (inlined by) "); 35695fd7f26SEd Maste 35795fd7f26SEd Maste if (func) { 35895fd7f26SEd Maste if (demangle && !elftc_demangle(f->name, demangled, 35995fd7f26SEd Maste sizeof(demangled), 0)) { 36095fd7f26SEd Maste if (pretty_print) 36195fd7f26SEd Maste printf("%s at ", demangled); 36295fd7f26SEd Maste else 36395fd7f26SEd Maste printf("%s\n", demangled); 36495fd7f26SEd Maste } else { 36595fd7f26SEd Maste if (pretty_print) 36695fd7f26SEd Maste printf("%s at ", f->name); 36795fd7f26SEd Maste else 36895fd7f26SEd Maste printf("%s\n", f->name); 36995fd7f26SEd Maste } 37095fd7f26SEd Maste } 371*839529caSEd Maste (void) printf("%s:%ju\n", base ? basename(file) : file, 372*839529caSEd Maste (uintmax_t) call_line); 37395fd7f26SEd Maste 37495fd7f26SEd Maste if (f->inlined_caller != NULL) 37595fd7f26SEd Maste print_inlines(cu, f->inlined_caller, f->call_file, 37695fd7f26SEd Maste f->call_line); 37795fd7f26SEd Maste } 37895fd7f26SEd Maste 37995fd7f26SEd Maste static void 38095fd7f26SEd Maste translate(Dwarf_Debug dbg, Elf *e, const char* addrstr) 381a85fe12eSEd Maste { 382656f49f8SEd Maste Dwarf_Die die, ret_die; 383a85fe12eSEd Maste Dwarf_Line *lbuf; 384a85fe12eSEd Maste Dwarf_Error de; 385a85fe12eSEd Maste Dwarf_Half tag; 386a85fe12eSEd Maste Dwarf_Unsigned lopc, hipc, addr, lineno, plineno; 387a85fe12eSEd Maste Dwarf_Signed lcount; 388a85fe12eSEd Maste Dwarf_Addr lineaddr, plineaddr; 38995fd7f26SEd Maste Dwarf_Off off; 39095fd7f26SEd Maste struct CU *cu; 39195fd7f26SEd Maste struct Func *f; 39295fd7f26SEd Maste const char *funcname; 393a85fe12eSEd Maste char *file, *file0, *pfile; 394a85fe12eSEd Maste char demangled[1024]; 39595fd7f26SEd Maste int ec, i, ret; 396a85fe12eSEd Maste 397a85fe12eSEd Maste addr = strtoull(addrstr, NULL, 16); 398a85fe12eSEd Maste addr += section_base; 399a85fe12eSEd Maste lineno = 0; 400a85fe12eSEd Maste file = unknown; 40195fd7f26SEd Maste cu = NULL; 4027a2e729bSEd Maste die = NULL; 403a85fe12eSEd Maste 404a85fe12eSEd Maste while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, 405a85fe12eSEd Maste &de)) == DW_DLV_OK) { 406a85fe12eSEd Maste die = NULL; 407656f49f8SEd Maste while (dwarf_siblingof(dbg, die, &ret_die, &de) == DW_DLV_OK) { 408656f49f8SEd Maste if (die != NULL) 409656f49f8SEd Maste dwarf_dealloc(dbg, die, DW_DLA_DIE); 410656f49f8SEd Maste die = ret_die; 411a85fe12eSEd Maste if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 412a85fe12eSEd Maste warnx("dwarf_tag failed: %s", 413a85fe12eSEd Maste dwarf_errmsg(de)); 414656f49f8SEd Maste goto next_cu; 415a85fe12eSEd Maste } 416656f49f8SEd Maste 417a85fe12eSEd Maste /* XXX: What about DW_TAG_partial_unit? */ 418a85fe12eSEd Maste if (tag == DW_TAG_compile_unit) 419a85fe12eSEd Maste break; 420a85fe12eSEd Maste } 421656f49f8SEd Maste if (ret_die == NULL) { 422a85fe12eSEd Maste warnx("could not find DW_TAG_compile_unit die"); 423656f49f8SEd Maste goto next_cu; 424a85fe12eSEd Maste } 42595fd7f26SEd Maste if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) == 42695fd7f26SEd Maste DW_DLV_OK) { 42795fd7f26SEd Maste if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, 42895fd7f26SEd Maste &de) == DW_DLV_OK) { 429a85fe12eSEd Maste /* 43095fd7f26SEd Maste * Check if the address falls into the PC 43195fd7f26SEd Maste * range of this CU. 432a85fe12eSEd Maste */ 43395fd7f26SEd Maste if (handle_high_pc(die, lopc, &hipc) != 43495fd7f26SEd Maste DW_DLV_OK) 43595fd7f26SEd Maste goto out; 43695fd7f26SEd Maste } else { 43795fd7f26SEd Maste /* Assume ~0ULL if DW_AT_high_pc not present */ 43895fd7f26SEd Maste hipc = ~0ULL; 439a85fe12eSEd Maste } 440a85fe12eSEd Maste 44195fd7f26SEd Maste /* 44295fd7f26SEd Maste * Record the CU in the hash table for faster lookup 44395fd7f26SEd Maste * later. 44495fd7f26SEd Maste */ 44595fd7f26SEd Maste if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) { 44695fd7f26SEd Maste warnx("dwarf_dieoffset failed: %s", 44795fd7f26SEd Maste dwarf_errmsg(de)); 44895fd7f26SEd Maste goto out; 44995fd7f26SEd Maste } 45095fd7f26SEd Maste HASH_FIND(hh, culist, &off, sizeof(off), cu); 45195fd7f26SEd Maste if (cu == NULL) { 45295fd7f26SEd Maste if ((cu = calloc(1, sizeof(*cu))) == NULL) 45395fd7f26SEd Maste err(EXIT_FAILURE, "calloc"); 45495fd7f26SEd Maste cu->off = off; 45595fd7f26SEd Maste cu->lopc = lopc; 45695fd7f26SEd Maste cu->hipc = hipc; 45795fd7f26SEd Maste STAILQ_INIT(&cu->funclist); 45895fd7f26SEd Maste HASH_ADD(hh, culist, off, sizeof(off), cu); 45995fd7f26SEd Maste } 46095fd7f26SEd Maste 46195fd7f26SEd Maste if (addr >= lopc && addr < hipc) 46295fd7f26SEd Maste break; 46395fd7f26SEd Maste } 46495fd7f26SEd Maste 46595fd7f26SEd Maste next_cu: 46695fd7f26SEd Maste if (die != NULL) { 46795fd7f26SEd Maste dwarf_dealloc(dbg, die, DW_DLA_DIE); 46895fd7f26SEd Maste die = NULL; 46995fd7f26SEd Maste } 47095fd7f26SEd Maste } 47195fd7f26SEd Maste 47295fd7f26SEd Maste if (ret != DW_DLV_OK || die == NULL) 47395fd7f26SEd Maste goto out; 47495fd7f26SEd Maste 475c9dbb1ccSEd Maste switch (dwarf_srclines(die, &lbuf, &lcount, &de)) { 476c9dbb1ccSEd Maste case DW_DLV_OK: 477c9dbb1ccSEd Maste break; 478c9dbb1ccSEd Maste case DW_DLV_NO_ENTRY: 479656f49f8SEd Maste /* If a CU lacks debug info, just skip it. */ 48095fd7f26SEd Maste goto out; 481c9dbb1ccSEd Maste default: 482a85fe12eSEd Maste warnx("dwarf_srclines: %s", dwarf_errmsg(de)); 483a85fe12eSEd Maste goto out; 484a85fe12eSEd Maste } 485a85fe12eSEd Maste 486a85fe12eSEd Maste plineaddr = ~0ULL; 487a85fe12eSEd Maste plineno = 0; 488a85fe12eSEd Maste pfile = unknown; 489a85fe12eSEd Maste for (i = 0; i < lcount; i++) { 490a85fe12eSEd Maste if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) { 49195fd7f26SEd Maste warnx("dwarf_lineaddr: %s", dwarf_errmsg(de)); 492a85fe12eSEd Maste goto out; 493a85fe12eSEd Maste } 494a85fe12eSEd Maste if (dwarf_lineno(lbuf[i], &lineno, &de)) { 49595fd7f26SEd Maste warnx("dwarf_lineno: %s", dwarf_errmsg(de)); 496a85fe12eSEd Maste goto out; 497a85fe12eSEd Maste } 498a85fe12eSEd Maste if (dwarf_linesrc(lbuf[i], &file0, &de)) { 49995fd7f26SEd Maste warnx("dwarf_linesrc: %s", dwarf_errmsg(de)); 500a85fe12eSEd Maste } else 501a85fe12eSEd Maste file = file0; 502a85fe12eSEd Maste if (addr == lineaddr) 503a85fe12eSEd Maste goto out; 504a85fe12eSEd Maste else if (addr < lineaddr && addr > plineaddr) { 505a85fe12eSEd Maste lineno = plineno; 506a85fe12eSEd Maste file = pfile; 507a85fe12eSEd Maste goto out; 508a85fe12eSEd Maste } 509a85fe12eSEd Maste plineaddr = lineaddr; 510a85fe12eSEd Maste plineno = lineno; 511a85fe12eSEd Maste pfile = file; 512a85fe12eSEd Maste } 513a85fe12eSEd Maste 514a85fe12eSEd Maste out: 51595fd7f26SEd Maste f = NULL; 516a85fe12eSEd Maste funcname = NULL; 51795fd7f26SEd Maste if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) { 51895fd7f26SEd Maste if (cu->srcfiles == NULL) 51995fd7f26SEd Maste if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles, 52095fd7f26SEd Maste &de)) 52195fd7f26SEd Maste warnx("dwarf_srcfiles: %s", dwarf_errmsg(de)); 52295fd7f26SEd Maste if (STAILQ_EMPTY(&cu->funclist)) { 52395fd7f26SEd Maste collect_func(dbg, die, NULL, cu); 524656f49f8SEd Maste die = NULL; 525656f49f8SEd Maste } 52695fd7f26SEd Maste f = search_func(cu, addr); 52795fd7f26SEd Maste if (f != NULL) 52895fd7f26SEd Maste funcname = f->name; 52995fd7f26SEd Maste } 53095fd7f26SEd Maste 53195fd7f26SEd Maste if (print_addr) { 53295fd7f26SEd Maste if ((ec = gelf_getclass(e)) == ELFCLASSNONE) { 53395fd7f26SEd Maste warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 53495fd7f26SEd Maste ec = ELFCLASS64; 53595fd7f26SEd Maste } 53695fd7f26SEd Maste if (ec == ELFCLASS32) { 53795fd7f26SEd Maste if (pretty_print) 53895fd7f26SEd Maste printf("0x%08jx: ", (uintmax_t) addr); 53995fd7f26SEd Maste else 54095fd7f26SEd Maste printf("0x%08jx\n", (uintmax_t) addr); 54195fd7f26SEd Maste } else { 54295fd7f26SEd Maste if (pretty_print) 54395fd7f26SEd Maste printf("0x%016jx: ", (uintmax_t) addr); 54495fd7f26SEd Maste else 54595fd7f26SEd Maste printf("0x%016jx\n", (uintmax_t) addr); 54695fd7f26SEd Maste } 54795fd7f26SEd Maste } 548a85fe12eSEd Maste 549a85fe12eSEd Maste if (func) { 550a85fe12eSEd Maste if (funcname == NULL) 55195fd7f26SEd Maste funcname = unknown; 55295fd7f26SEd Maste if (demangle && !elftc_demangle(funcname, demangled, 55395fd7f26SEd Maste sizeof(demangled), 0)) { 55495fd7f26SEd Maste if (pretty_print) 55595fd7f26SEd Maste printf("%s at ", demangled); 55695fd7f26SEd Maste else 557a85fe12eSEd Maste printf("%s\n", demangled); 55895fd7f26SEd Maste } else { 55995fd7f26SEd Maste if (pretty_print) 56095fd7f26SEd Maste printf("%s at ", funcname); 561a85fe12eSEd Maste else 562a85fe12eSEd Maste printf("%s\n", funcname); 56395fd7f26SEd Maste } 564a85fe12eSEd Maste } 565a85fe12eSEd Maste 566*839529caSEd Maste (void) printf("%s:%ju\n", base ? basename(file) : file, 567*839529caSEd Maste (uintmax_t) lineno); 568a85fe12eSEd Maste 56995fd7f26SEd Maste if (ret == DW_DLV_OK && inlines && cu != NULL && 57095fd7f26SEd Maste cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL) 57195fd7f26SEd Maste print_inlines(cu, f->inlined_caller, f->call_file, 57295fd7f26SEd Maste f->call_line); 57395fd7f26SEd Maste 574656f49f8SEd Maste if (die != NULL) 575656f49f8SEd Maste dwarf_dealloc(dbg, die, DW_DLA_DIE); 576656f49f8SEd Maste 577a85fe12eSEd Maste /* 578a85fe12eSEd Maste * Reset internal CU pointer, so we will start from the first CU 579a85fe12eSEd Maste * next round. 580a85fe12eSEd Maste */ 581a85fe12eSEd Maste while (ret != DW_DLV_NO_ENTRY) { 582a85fe12eSEd Maste if (ret == DW_DLV_ERROR) 583a85fe12eSEd Maste errx(EXIT_FAILURE, "dwarf_next_cu_header: %s", 584a85fe12eSEd Maste dwarf_errmsg(de)); 585a85fe12eSEd Maste ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, 586a85fe12eSEd Maste &de); 587a85fe12eSEd Maste } 588a85fe12eSEd Maste } 589a85fe12eSEd Maste 590a85fe12eSEd Maste static void 591a85fe12eSEd Maste find_section_base(const char *exe, Elf *e, const char *section) 592a85fe12eSEd Maste { 593a85fe12eSEd Maste Dwarf_Addr off; 594a85fe12eSEd Maste Elf_Scn *scn; 595a85fe12eSEd Maste GElf_Ehdr eh; 596a85fe12eSEd Maste GElf_Shdr sh; 597a85fe12eSEd Maste size_t shstrndx; 598a85fe12eSEd Maste int elferr; 599a85fe12eSEd Maste const char *name; 600a85fe12eSEd Maste 601a85fe12eSEd Maste if (gelf_getehdr(e, &eh) != &eh) { 602a85fe12eSEd Maste warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 603a85fe12eSEd Maste return; 604a85fe12eSEd Maste } 605a85fe12eSEd Maste 606a85fe12eSEd Maste if (!elf_getshstrndx(e, &shstrndx)) { 607a85fe12eSEd Maste warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 608a85fe12eSEd Maste return; 609a85fe12eSEd Maste } 610a85fe12eSEd Maste 611a85fe12eSEd Maste (void) elf_errno(); 612a85fe12eSEd Maste off = 0; 613a85fe12eSEd Maste scn = NULL; 614a85fe12eSEd Maste while ((scn = elf_nextscn(e, scn)) != NULL) { 615a85fe12eSEd Maste if (gelf_getshdr(scn, &sh) == NULL) { 616a85fe12eSEd Maste warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 617a85fe12eSEd Maste continue; 618a85fe12eSEd Maste } 619a85fe12eSEd Maste if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL) 620a85fe12eSEd Maste goto next; 621a85fe12eSEd Maste if (!strcmp(section, name)) { 622a85fe12eSEd Maste if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) { 623a85fe12eSEd Maste /* 624a85fe12eSEd Maste * For executables, section base is the virtual 625a85fe12eSEd Maste * address of the specified section. 626a85fe12eSEd Maste */ 627a85fe12eSEd Maste section_base = sh.sh_addr; 628a85fe12eSEd Maste } else if (eh.e_type == ET_REL) { 629a85fe12eSEd Maste /* 630a85fe12eSEd Maste * For relocatables, section base is the 631a85fe12eSEd Maste * relative offset of the specified section 632a85fe12eSEd Maste * to the start of the first section. 633a85fe12eSEd Maste */ 634a85fe12eSEd Maste section_base = off; 635a85fe12eSEd Maste } else 636a85fe12eSEd Maste warnx("unknown e_type %u", eh.e_type); 637a85fe12eSEd Maste return; 638a85fe12eSEd Maste } 639a85fe12eSEd Maste next: 640a85fe12eSEd Maste off += sh.sh_size; 641a85fe12eSEd Maste } 642a85fe12eSEd Maste elferr = elf_errno(); 643a85fe12eSEd Maste if (elferr != 0) 644a85fe12eSEd Maste warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 645a85fe12eSEd Maste 646a85fe12eSEd Maste errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section); 647a85fe12eSEd Maste } 648a85fe12eSEd Maste 649a85fe12eSEd Maste int 650a85fe12eSEd Maste main(int argc, char **argv) 651a85fe12eSEd Maste { 652a85fe12eSEd Maste Elf *e; 653a85fe12eSEd Maste Dwarf_Debug dbg; 654a85fe12eSEd Maste Dwarf_Error de; 655a85fe12eSEd Maste const char *exe, *section; 656a85fe12eSEd Maste char line[1024]; 657a85fe12eSEd Maste int fd, i, opt; 658a85fe12eSEd Maste 659a85fe12eSEd Maste exe = NULL; 660a85fe12eSEd Maste section = NULL; 66195fd7f26SEd Maste while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts, 66295fd7f26SEd Maste NULL)) != -1) { 663a85fe12eSEd Maste switch (opt) { 66495fd7f26SEd Maste case 'a': 66595fd7f26SEd Maste print_addr = 1; 66695fd7f26SEd Maste break; 667a85fe12eSEd Maste case 'b': 668a85fe12eSEd Maste /* ignored */ 669a85fe12eSEd Maste break; 670a85fe12eSEd Maste case 'C': 671a85fe12eSEd Maste demangle = 1; 672a85fe12eSEd Maste break; 673a85fe12eSEd Maste case 'e': 674a85fe12eSEd Maste exe = optarg; 675a85fe12eSEd Maste break; 676a85fe12eSEd Maste case 'f': 677a85fe12eSEd Maste func = 1; 678a85fe12eSEd Maste break; 67995fd7f26SEd Maste case 'i': 68095fd7f26SEd Maste inlines = 1; 68195fd7f26SEd Maste break; 682a85fe12eSEd Maste case 'j': 683a85fe12eSEd Maste section = optarg; 684a85fe12eSEd Maste break; 68595fd7f26SEd Maste case 'p': 68695fd7f26SEd Maste pretty_print = 1; 68795fd7f26SEd Maste break; 688a85fe12eSEd Maste case 's': 689a85fe12eSEd Maste base = 1; 690a85fe12eSEd Maste break; 691a85fe12eSEd Maste case 'H': 692a85fe12eSEd Maste usage(); 693a85fe12eSEd Maste case 'V': 694a85fe12eSEd Maste version(); 695a85fe12eSEd Maste default: 696a85fe12eSEd Maste usage(); 697a85fe12eSEd Maste } 698a85fe12eSEd Maste } 699a85fe12eSEd Maste 700a85fe12eSEd Maste argv += optind; 701a85fe12eSEd Maste argc -= optind; 702a85fe12eSEd Maste 703a85fe12eSEd Maste if (exe == NULL) 704a85fe12eSEd Maste exe = "a.out"; 705a85fe12eSEd Maste 706a85fe12eSEd Maste if ((fd = open(exe, O_RDONLY)) < 0) 707a85fe12eSEd Maste err(EXIT_FAILURE, "%s", exe); 708a85fe12eSEd Maste 709a85fe12eSEd Maste if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de)) 710a85fe12eSEd Maste errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de)); 711a85fe12eSEd Maste 712a85fe12eSEd Maste if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK) 713a85fe12eSEd Maste errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de)); 714a85fe12eSEd Maste 715a85fe12eSEd Maste if (section) 716a85fe12eSEd Maste find_section_base(exe, e, section); 717a85fe12eSEd Maste else 718a85fe12eSEd Maste section_base = 0; 719a85fe12eSEd Maste 720a85fe12eSEd Maste if (argc > 0) 721a85fe12eSEd Maste for (i = 0; i < argc; i++) 72295fd7f26SEd Maste translate(dbg, e, argv[i]); 723a85fe12eSEd Maste else 724a7265433SEd Maste while (fgets(line, sizeof(line), stdin) != NULL) { 72595fd7f26SEd Maste translate(dbg, e, line); 726a7265433SEd Maste fflush(stdout); 727a7265433SEd Maste } 728a85fe12eSEd Maste 729a85fe12eSEd Maste dwarf_finish(dbg, &de); 730a85fe12eSEd Maste 731a85fe12eSEd Maste (void) elf_end(e); 732a85fe12eSEd Maste 733a85fe12eSEd Maste exit(0); 734a85fe12eSEd Maste } 735