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/cdefs.h>
28 #include <sys/param.h>
43 ELFTC_VCSID("$Id: addr2line.c 2185 2011-11-19 16:07:16Z jkoshy $");
45 static struct option longopts[] = {
46 {"target" , required_argument, NULL, 'b'},
47 {"demangle", no_argument, NULL, 'C'},
48 {"exe", required_argument, NULL, 'e'},
49 {"functions", no_argument, NULL, 'f'},
50 {"section", required_argument, NULL, 'j'},
51 {"basename", no_argument, NULL, 's'},
52 {"help", no_argument, NULL, 'H'},
53 {"version", no_argument, NULL, 'V'},
56 static int demangle, func, base;
57 static char unknown[] = { '?', '?', '\0' };
58 static Dwarf_Addr section_base;
60 #define USAGE_MESSAGE "\
61 Usage: %s [options] hexaddress...\n\
62 Map program addresses to source file names and line numbers.\n\n\
64 -b TGT | --target=TGT (Accepted but ignored).\n\
65 -e EXE | --exec=EXE Use program \"EXE\" to translate addresses.\n\
66 -f | --functions Display function names.\n\
67 -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\
68 -s | --basename Only show the base name for each file name.\n\
69 -C | --demangle Demangle C++ names.\n\
70 -H | --help Print a help message.\n\
71 -V | --version Print a version identifier and exit.\n"
76 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
84 fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
89 search_func(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Addr addr,
90 const char **rlt_func)
92 Dwarf_Die ret_die, spec_die;
95 Dwarf_Unsigned lopc, hipc;
97 Dwarf_Attribute sub_at, spec_at;
101 if (*rlt_func != NULL)
104 if (dwarf_tag(die, &tag, &de)) {
105 warnx("dwarf_tag: %s", dwarf_errmsg(de));
108 if (tag == DW_TAG_subprogram) {
109 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ||
110 dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de))
112 if (addr < lopc || addr >= hipc)
118 ret = dwarf_attr(die, DW_AT_name, &sub_at, &de);
119 if (ret == DW_DLV_ERROR)
121 if (ret == DW_DLV_OK) {
122 if (dwarf_formstring(sub_at, &func0, &de))
130 * If DW_AT_name is not present, but DW_AT_specification is
131 * present, then probably the actual name is in the DIE
132 * referenced by DW_AT_specification.
134 if (dwarf_attr(die, DW_AT_specification, &spec_at, &de))
136 if (dwarf_global_formref(spec_at, &ref, &de))
138 if (dwarf_offdie(dbg, ref, &spec_die, &de))
140 if (dwarf_attrval_string(spec_die, DW_AT_name, rlt_func, &de))
148 /* Search children. */
149 ret = dwarf_child(die, &ret_die, &de);
150 if (ret == DW_DLV_ERROR)
151 errx(EXIT_FAILURE, "dwarf_child: %s", dwarf_errmsg(de));
152 else if (ret == DW_DLV_OK)
153 search_func(dbg, ret_die, addr, rlt_func);
155 /* Search sibling. */
156 ret = dwarf_siblingof(dbg, die, &ret_die, &de);
157 if (ret == DW_DLV_ERROR)
158 errx(EXIT_FAILURE, "dwarf_siblingof: %s", dwarf_errmsg(de));
159 else if (ret == DW_DLV_OK)
160 search_func(dbg, ret_die, addr, rlt_func);
164 translate(Dwarf_Debug dbg, const char* addrstr)
170 Dwarf_Unsigned lopc, hipc, addr, lineno, plineno;
172 Dwarf_Addr lineaddr, plineaddr;
173 const char *funcname;
174 char *file, *file0, *pfile;
175 char demangled[1024];
178 addr = strtoull(addrstr, NULL, 16);
179 addr += section_base;
183 while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
184 &de)) == DW_DLV_OK) {
186 while (dwarf_siblingof(dbg, die, &die, &de) == DW_DLV_OK) {
187 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) {
188 warnx("dwarf_tag failed: %s",
192 /* XXX: What about DW_TAG_partial_unit? */
193 if (tag == DW_TAG_compile_unit)
197 warnx("could not find DW_TAG_compile_unit die");
200 if (!dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) &&
201 !dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de)) {
203 * Check if the address falls into the PC range of
206 if (addr < lopc || addr >= hipc)
210 if (dwarf_srclines(die, &lbuf, &lcount, &de) != DW_DLV_OK) {
211 warnx("dwarf_srclines: %s", dwarf_errmsg(de));
218 for (i = 0; i < lcount; i++) {
219 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
220 warnx("dwarf_lineaddr: %s",
224 if (dwarf_lineno(lbuf[i], &lineno, &de)) {
225 warnx("dwarf_lineno: %s",
229 if (dwarf_linesrc(lbuf[i], &file0, &de)) {
230 warnx("dwarf_linesrc: %s",
234 if (addr == lineaddr)
236 else if (addr < lineaddr && addr > plineaddr) {
241 plineaddr = lineaddr;
249 if (ret == DW_DLV_OK && func)
250 search_func(dbg, die, addr, &funcname);
253 if (funcname == NULL)
256 !elftc_demangle(funcname, demangled, sizeof(demangled), 0))
257 printf("%s\n", demangled);
259 printf("%s\n", funcname);
262 (void) printf("%s:%ju\n", base ? basename(file) : file, lineno);
265 * Reset internal CU pointer, so we will start from the first CU
268 while (ret != DW_DLV_NO_ENTRY) {
269 if (ret == DW_DLV_ERROR)
270 errx(EXIT_FAILURE, "dwarf_next_cu_header: %s",
272 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
278 find_section_base(const char *exe, Elf *e, const char *section)
288 if (gelf_getehdr(e, &eh) != &eh) {
289 warnx("gelf_getehdr failed: %s", elf_errmsg(-1));
293 if (!elf_getshstrndx(e, &shstrndx)) {
294 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1));
301 while ((scn = elf_nextscn(e, scn)) != NULL) {
302 if (gelf_getshdr(scn, &sh) == NULL) {
303 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
306 if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL)
308 if (!strcmp(section, name)) {
309 if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) {
311 * For executables, section base is the virtual
312 * address of the specified section.
314 section_base = sh.sh_addr;
315 } else if (eh.e_type == ET_REL) {
317 * For relocatables, section base is the
318 * relative offset of the specified section
319 * to the start of the first section.
323 warnx("unknown e_type %u", eh.e_type);
329 elferr = elf_errno();
331 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
333 errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section);
337 main(int argc, char **argv)
342 const char *exe, *section;
348 while ((opt = getopt_long(argc, argv, "b:Ce:fj:sHV", longopts, NULL)) !=
384 if ((fd = open(exe, O_RDONLY)) < 0)
385 err(EXIT_FAILURE, "%s", exe);
387 if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de))
388 errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de));
390 if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK)
391 errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de));
394 find_section_base(exe, e, section);
399 for (i = 0; i < argc; i++)
400 translate(dbg, argv[i]);
402 while (fgets(line, sizeof(line), stdin) != NULL) {
403 translate(dbg, line);
407 dwarf_finish(dbg, &de);