1 /*- 2 * Copyright (c) 2009 Kai Wang 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <dwarf.h> 29 #include <err.h> 30 #include <fcntl.h> 31 #include <gelf.h> 32 #include <getopt.h> 33 #include <libdwarf.h> 34 #include <libelftc.h> 35 #include <libgen.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 40 #include "uthash.h" 41 #include "_elftc.h" 42 43 ELFTC_VCSID("$Id: addr2line.c 3264 2015-11-30 05:38:14Z kaiwang27 $"); 44 45 struct Func { 46 char *name; 47 Dwarf_Unsigned lopc; 48 Dwarf_Unsigned hipc; 49 Dwarf_Unsigned call_file; 50 Dwarf_Unsigned call_line; 51 Dwarf_Ranges *ranges; 52 Dwarf_Signed ranges_cnt; 53 struct Func *inlined_caller; 54 STAILQ_ENTRY(Func) next; 55 }; 56 57 struct CU { 58 Dwarf_Off off; 59 Dwarf_Unsigned lopc; 60 Dwarf_Unsigned hipc; 61 char **srcfiles; 62 Dwarf_Signed nsrcfiles; 63 STAILQ_HEAD(, Func) funclist; 64 UT_hash_handle hh; 65 }; 66 67 static struct option longopts[] = { 68 {"addresses", no_argument, NULL, 'a'}, 69 {"target" , required_argument, NULL, 'b'}, 70 {"demangle", no_argument, NULL, 'C'}, 71 {"exe", required_argument, NULL, 'e'}, 72 {"functions", no_argument, NULL, 'f'}, 73 {"inlines", no_argument, NULL, 'i'}, 74 {"section", required_argument, NULL, 'j'}, 75 {"pretty-print", no_argument, NULL, 'p'}, 76 {"basename", no_argument, NULL, 's'}, 77 {"help", no_argument, NULL, 'H'}, 78 {"version", no_argument, NULL, 'V'}, 79 {NULL, 0, NULL, 0} 80 }; 81 static int demangle, func, base, inlines, print_addr, pretty_print; 82 static char unknown[] = { '?', '?', '\0' }; 83 static Dwarf_Addr section_base; 84 static struct CU *culist; 85 86 #define USAGE_MESSAGE "\ 87 Usage: %s [options] hexaddress...\n\ 88 Map program addresses to source file names and line numbers.\n\n\ 89 Options:\n\ 90 -a | --addresses Display address prior to line number info.\n\ 91 -b TGT | --target=TGT (Accepted but ignored).\n\ 92 -e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\ 93 -f | --functions Display function names.\n\ 94 -i | --inlines Display caller info for inlined functions.\n\ 95 -j NAME | --section=NAME Values are offsets into section \"NAME\".\n\ 96 -p | --pretty-print Display line number info and function name\n\ 97 in human readable manner.\n\ 98 -s | --basename Only show the base name for each file name.\n\ 99 -C | --demangle Demangle C++ names.\n\ 100 -H | --help Print a help message.\n\ 101 -V | --version Print a version identifier and exit.\n" 102 103 static void 104 usage(void) 105 { 106 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); 107 exit(1); 108 } 109 110 static void 111 version(void) 112 { 113 114 fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); 115 exit(0); 116 } 117 118 /* 119 * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't 120 * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1) 121 * generate DW_AT_high_pc as an offset from DW_AT_low_pc. 122 * 123 * "If the value of the DW_AT_high_pc is of class address, it is the 124 * relocated address of the first location past the last instruction 125 * associated with the entity; if it is of class constant, the value 126 * is an unsigned integer offset which when added to the low PC gives 127 * the address of the first location past the last instruction 128 * associated with the entity." 129 * 130 * DWARF4 spec, section 2.17.2. 131 */ 132 static int 133 handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc) 134 { 135 Dwarf_Error de; 136 Dwarf_Half form; 137 Dwarf_Attribute at; 138 int ret; 139 140 ret = dwarf_attr(die, DW_AT_high_pc, &at, &de); 141 if (ret == DW_DLV_ERROR) { 142 warnx("dwarf_attr failed: %s", dwarf_errmsg(de)); 143 return (ret); 144 } 145 ret = dwarf_whatform(at, &form, &de); 146 if (ret == DW_DLV_ERROR) { 147 warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); 148 return (ret); 149 } 150 if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT) 151 *hipc += lopc; 152 153 return (DW_DLV_OK); 154 } 155 156 static struct Func * 157 search_func(struct CU *cu, Dwarf_Unsigned addr) 158 { 159 struct Func *f, *f0; 160 Dwarf_Unsigned lopc, hipc, addr_base; 161 int i; 162 163 f0 = NULL; 164 165 STAILQ_FOREACH(f, &cu->funclist, next) { 166 if (f->ranges != NULL) { 167 addr_base = 0; 168 for (i = 0; i < f->ranges_cnt; i++) { 169 if (f->ranges[i].dwr_type == DW_RANGES_END) 170 break; 171 if (f->ranges[i].dwr_type == 172 DW_RANGES_ADDRESS_SELECTION) { 173 addr_base = f->ranges[i].dwr_addr2; 174 continue; 175 } 176 177 /* DW_RANGES_ENTRY */ 178 lopc = f->ranges[i].dwr_addr1 + addr_base; 179 hipc = f->ranges[i].dwr_addr2 + addr_base; 180 if (addr >= lopc && addr < hipc) { 181 if (f0 == NULL || 182 (lopc >= f0->lopc && 183 hipc <= f0->hipc)) { 184 f0 = f; 185 f0->lopc = lopc; 186 f0->hipc = hipc; 187 break; 188 } 189 } 190 } 191 } else if (addr >= f->lopc && addr < f->hipc) { 192 if (f0 == NULL || 193 (f->lopc >= f0->lopc && f->hipc <= f0->hipc)) 194 f0 = f; 195 } 196 } 197 198 return (f0); 199 } 200 201 static void 202 collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu) 203 { 204 Dwarf_Die ret_die, abst_die, spec_die; 205 Dwarf_Error de; 206 Dwarf_Half tag; 207 Dwarf_Unsigned lopc, hipc, ranges_off; 208 Dwarf_Signed ranges_cnt; 209 Dwarf_Off ref; 210 Dwarf_Attribute abst_at, spec_at; 211 Dwarf_Ranges *ranges; 212 const char *funcname; 213 struct Func *f; 214 int found_ranges, ret; 215 216 f = NULL; 217 abst_die = spec_die = NULL; 218 219 if (dwarf_tag(die, &tag, &de)) { 220 warnx("dwarf_tag: %s", dwarf_errmsg(de)); 221 goto cont_search; 222 } 223 if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point || 224 tag == DW_TAG_inlined_subroutine) { 225 /* 226 * Function address range can be specified by either 227 * a DW_AT_ranges attribute which points to a range list or 228 * by a pair of DW_AT_low_pc and DW_AT_high_pc attributes. 229 */ 230 ranges = NULL; 231 ranges_cnt = 0; 232 found_ranges = 0; 233 if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off, 234 &de) == DW_DLV_OK && 235 dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges, 236 &ranges_cnt, NULL, &de) == DW_DLV_OK) { 237 if (ranges != NULL && ranges_cnt > 0) { 238 found_ranges = 1; 239 goto get_func_name; 240 } 241 } 242 243 /* 244 * Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer 245 * not found. 246 */ 247 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) || 248 dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de)) 249 goto cont_search; 250 if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK) 251 goto cont_search; 252 253 get_func_name: 254 /* 255 * Most common case the function name is stored in DW_AT_name 256 * attribute. 257 */ 258 if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) == 259 DW_DLV_OK) 260 goto add_func; 261 262 /* 263 * For inlined function, the actual name is probably in the DIE 264 * referenced by DW_AT_abstract_origin. (if present) 265 */ 266 if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) == 267 DW_DLV_OK && 268 dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK && 269 dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK && 270 dwarf_attrval_string(abst_die, DW_AT_name, &funcname, 271 &de) == DW_DLV_OK) 272 goto add_func; 273 274 /* 275 * If DW_AT_name is not present, but DW_AT_specification is 276 * present, then probably the actual name is in the DIE 277 * referenced by DW_AT_specification. 278 */ 279 if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) == 280 DW_DLV_OK && 281 dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK && 282 dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK && 283 dwarf_attrval_string(spec_die, DW_AT_name, &funcname, 284 &de) == DW_DLV_OK) 285 goto add_func; 286 287 /* Skip if no name assoicated with this DIE. */ 288 goto cont_search; 289 290 add_func: 291 if ((f = calloc(1, sizeof(*f))) == NULL) 292 err(EXIT_FAILURE, "calloc"); 293 if ((f->name = strdup(funcname)) == NULL) 294 err(EXIT_FAILURE, "strdup"); 295 if (found_ranges) { 296 f->ranges = ranges; 297 f->ranges_cnt = ranges_cnt; 298 } else { 299 f->lopc = lopc; 300 f->hipc = hipc; 301 } 302 if (tag == DW_TAG_inlined_subroutine) { 303 f->inlined_caller = parent; 304 dwarf_attrval_unsigned(die, DW_AT_call_file, 305 &f->call_file, &de); 306 dwarf_attrval_unsigned(die, DW_AT_call_line, 307 &f->call_line, &de); 308 } 309 STAILQ_INSERT_TAIL(&cu->funclist, f, next); 310 } 311 312 cont_search: 313 314 /* Search children. */ 315 ret = dwarf_child(die, &ret_die, &de); 316 if (ret == DW_DLV_ERROR) 317 warnx("dwarf_child: %s", dwarf_errmsg(de)); 318 else if (ret == DW_DLV_OK) { 319 if (f != NULL) 320 collect_func(dbg, ret_die, f, cu); 321 else 322 collect_func(dbg, ret_die, parent, cu); 323 } 324 325 /* Search sibling. */ 326 ret = dwarf_siblingof(dbg, die, &ret_die, &de); 327 if (ret == DW_DLV_ERROR) 328 warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); 329 else if (ret == DW_DLV_OK) 330 collect_func(dbg, ret_die, parent, cu); 331 332 /* Cleanup */ 333 dwarf_dealloc(dbg, die, DW_DLA_DIE); 334 335 if (abst_die != NULL) 336 dwarf_dealloc(dbg, abst_die, DW_DLA_DIE); 337 338 if (spec_die != NULL) 339 dwarf_dealloc(dbg, spec_die, DW_DLA_DIE); 340 } 341 342 static void 343 print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file, 344 Dwarf_Unsigned call_line) 345 { 346 char demangled[1024]; 347 char *file; 348 349 if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles) 350 file = cu->srcfiles[call_file - 1]; 351 else 352 file = unknown; 353 354 if (pretty_print) 355 printf(" (inlined by) "); 356 357 if (func) { 358 if (demangle && !elftc_demangle(f->name, demangled, 359 sizeof(demangled), 0)) { 360 if (pretty_print) 361 printf("%s at ", demangled); 362 else 363 printf("%s\n", demangled); 364 } else { 365 if (pretty_print) 366 printf("%s at ", f->name); 367 else 368 printf("%s\n", f->name); 369 } 370 } 371 (void) printf("%s:%ju\n", base ? basename(file) : file, call_line); 372 373 if (f->inlined_caller != NULL) 374 print_inlines(cu, f->inlined_caller, f->call_file, 375 f->call_line); 376 } 377 378 static void 379 translate(Dwarf_Debug dbg, Elf *e, const char* addrstr) 380 { 381 Dwarf_Die die, ret_die; 382 Dwarf_Line *lbuf; 383 Dwarf_Error de; 384 Dwarf_Half tag; 385 Dwarf_Unsigned lopc, hipc, addr, lineno, plineno; 386 Dwarf_Signed lcount; 387 Dwarf_Addr lineaddr, plineaddr; 388 Dwarf_Off off; 389 struct CU *cu; 390 struct Func *f; 391 const char *funcname; 392 char *file, *file0, *pfile; 393 char demangled[1024]; 394 int ec, i, ret; 395 396 addr = strtoull(addrstr, NULL, 16); 397 addr += section_base; 398 lineno = 0; 399 file = unknown; 400 cu = NULL; 401 die = NULL; 402 403 while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, 404 &de)) == DW_DLV_OK) { 405 die = NULL; 406 while (dwarf_siblingof(dbg, die, &ret_die, &de) == DW_DLV_OK) { 407 if (die != NULL) 408 dwarf_dealloc(dbg, die, DW_DLA_DIE); 409 die = ret_die; 410 if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { 411 warnx("dwarf_tag failed: %s", 412 dwarf_errmsg(de)); 413 goto next_cu; 414 } 415 416 /* XXX: What about DW_TAG_partial_unit? */ 417 if (tag == DW_TAG_compile_unit) 418 break; 419 } 420 if (ret_die == NULL) { 421 warnx("could not find DW_TAG_compile_unit die"); 422 goto next_cu; 423 } 424 if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) == 425 DW_DLV_OK) { 426 if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, 427 &de) == DW_DLV_OK) { 428 /* 429 * Check if the address falls into the PC 430 * range of this CU. 431 */ 432 if (handle_high_pc(die, lopc, &hipc) != 433 DW_DLV_OK) 434 goto out; 435 } else { 436 /* Assume ~0ULL if DW_AT_high_pc not present */ 437 hipc = ~0ULL; 438 } 439 440 /* 441 * Record the CU in the hash table for faster lookup 442 * later. 443 */ 444 if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) { 445 warnx("dwarf_dieoffset failed: %s", 446 dwarf_errmsg(de)); 447 goto out; 448 } 449 HASH_FIND(hh, culist, &off, sizeof(off), cu); 450 if (cu == NULL) { 451 if ((cu = calloc(1, sizeof(*cu))) == NULL) 452 err(EXIT_FAILURE, "calloc"); 453 cu->off = off; 454 cu->lopc = lopc; 455 cu->hipc = hipc; 456 STAILQ_INIT(&cu->funclist); 457 HASH_ADD(hh, culist, off, sizeof(off), cu); 458 } 459 460 if (addr >= lopc && addr < hipc) 461 break; 462 } 463 464 next_cu: 465 if (die != NULL) { 466 dwarf_dealloc(dbg, die, DW_DLA_DIE); 467 die = NULL; 468 } 469 } 470 471 if (ret != DW_DLV_OK || die == NULL) 472 goto out; 473 474 switch (dwarf_srclines(die, &lbuf, &lcount, &de)) { 475 case DW_DLV_OK: 476 break; 477 case DW_DLV_NO_ENTRY: 478 /* If a CU lacks debug info, just skip it. */ 479 goto out; 480 default: 481 warnx("dwarf_srclines: %s", dwarf_errmsg(de)); 482 goto out; 483 } 484 485 plineaddr = ~0ULL; 486 plineno = 0; 487 pfile = unknown; 488 for (i = 0; i < lcount; i++) { 489 if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) { 490 warnx("dwarf_lineaddr: %s", dwarf_errmsg(de)); 491 goto out; 492 } 493 if (dwarf_lineno(lbuf[i], &lineno, &de)) { 494 warnx("dwarf_lineno: %s", dwarf_errmsg(de)); 495 goto out; 496 } 497 if (dwarf_linesrc(lbuf[i], &file0, &de)) { 498 warnx("dwarf_linesrc: %s", dwarf_errmsg(de)); 499 } else 500 file = file0; 501 if (addr == lineaddr) 502 goto out; 503 else if (addr < lineaddr && addr > plineaddr) { 504 lineno = plineno; 505 file = pfile; 506 goto out; 507 } 508 plineaddr = lineaddr; 509 plineno = lineno; 510 pfile = file; 511 } 512 513 out: 514 f = NULL; 515 funcname = NULL; 516 if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) { 517 if (cu->srcfiles == NULL) 518 if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles, 519 &de)) 520 warnx("dwarf_srcfiles: %s", dwarf_errmsg(de)); 521 if (STAILQ_EMPTY(&cu->funclist)) { 522 collect_func(dbg, die, NULL, cu); 523 die = NULL; 524 } 525 f = search_func(cu, addr); 526 if (f != NULL) 527 funcname = f->name; 528 } 529 530 if (print_addr) { 531 if ((ec = gelf_getclass(e)) == ELFCLASSNONE) { 532 warnx("gelf_getclass failed: %s", elf_errmsg(-1)); 533 ec = ELFCLASS64; 534 } 535 if (ec == ELFCLASS32) { 536 if (pretty_print) 537 printf("0x%08jx: ", (uintmax_t) addr); 538 else 539 printf("0x%08jx\n", (uintmax_t) addr); 540 } else { 541 if (pretty_print) 542 printf("0x%016jx: ", (uintmax_t) addr); 543 else 544 printf("0x%016jx\n", (uintmax_t) addr); 545 } 546 } 547 548 if (func) { 549 if (funcname == NULL) 550 funcname = unknown; 551 if (demangle && !elftc_demangle(funcname, demangled, 552 sizeof(demangled), 0)) { 553 if (pretty_print) 554 printf("%s at ", demangled); 555 else 556 printf("%s\n", demangled); 557 } else { 558 if (pretty_print) 559 printf("%s at ", funcname); 560 else 561 printf("%s\n", funcname); 562 } 563 } 564 565 (void) printf("%s:%ju\n", base ? basename(file) : file, lineno); 566 567 if (ret == DW_DLV_OK && inlines && cu != NULL && 568 cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL) 569 print_inlines(cu, f->inlined_caller, f->call_file, 570 f->call_line); 571 572 if (die != NULL) 573 dwarf_dealloc(dbg, die, DW_DLA_DIE); 574 575 /* 576 * Reset internal CU pointer, so we will start from the first CU 577 * next round. 578 */ 579 while (ret != DW_DLV_NO_ENTRY) { 580 if (ret == DW_DLV_ERROR) 581 errx(EXIT_FAILURE, "dwarf_next_cu_header: %s", 582 dwarf_errmsg(de)); 583 ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, 584 &de); 585 } 586 } 587 588 static void 589 find_section_base(const char *exe, Elf *e, const char *section) 590 { 591 Dwarf_Addr off; 592 Elf_Scn *scn; 593 GElf_Ehdr eh; 594 GElf_Shdr sh; 595 size_t shstrndx; 596 int elferr; 597 const char *name; 598 599 if (gelf_getehdr(e, &eh) != &eh) { 600 warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); 601 return; 602 } 603 604 if (!elf_getshstrndx(e, &shstrndx)) { 605 warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); 606 return; 607 } 608 609 (void) elf_errno(); 610 off = 0; 611 scn = NULL; 612 while ((scn = elf_nextscn(e, scn)) != NULL) { 613 if (gelf_getshdr(scn, &sh) == NULL) { 614 warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); 615 continue; 616 } 617 if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL) 618 goto next; 619 if (!strcmp(section, name)) { 620 if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) { 621 /* 622 * For executables, section base is the virtual 623 * address of the specified section. 624 */ 625 section_base = sh.sh_addr; 626 } else if (eh.e_type == ET_REL) { 627 /* 628 * For relocatables, section base is the 629 * relative offset of the specified section 630 * to the start of the first section. 631 */ 632 section_base = off; 633 } else 634 warnx("unknown e_type %u", eh.e_type); 635 return; 636 } 637 next: 638 off += sh.sh_size; 639 } 640 elferr = elf_errno(); 641 if (elferr != 0) 642 warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); 643 644 errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section); 645 } 646 647 int 648 main(int argc, char **argv) 649 { 650 Elf *e; 651 Dwarf_Debug dbg; 652 Dwarf_Error de; 653 const char *exe, *section; 654 char line[1024]; 655 int fd, i, opt; 656 657 exe = NULL; 658 section = NULL; 659 while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts, 660 NULL)) != -1) { 661 switch (opt) { 662 case 'a': 663 print_addr = 1; 664 break; 665 case 'b': 666 /* ignored */ 667 break; 668 case 'C': 669 demangle = 1; 670 break; 671 case 'e': 672 exe = optarg; 673 break; 674 case 'f': 675 func = 1; 676 break; 677 case 'i': 678 inlines = 1; 679 break; 680 case 'j': 681 section = optarg; 682 break; 683 case 'p': 684 pretty_print = 1; 685 break; 686 case 's': 687 base = 1; 688 break; 689 case 'H': 690 usage(); 691 case 'V': 692 version(); 693 default: 694 usage(); 695 } 696 } 697 698 argv += optind; 699 argc -= optind; 700 701 if (exe == NULL) 702 exe = "a.out"; 703 704 if ((fd = open(exe, O_RDONLY)) < 0) 705 err(EXIT_FAILURE, "%s", exe); 706 707 if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de)) 708 errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de)); 709 710 if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK) 711 errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de)); 712 713 if (section) 714 find_section_base(exe, e, section); 715 else 716 section_base = 0; 717 718 if (argc > 0) 719 for (i = 0; i < argc; i++) 720 translate(dbg, e, argv[i]); 721 else 722 while (fgets(line, sizeof(line), stdin) != NULL) { 723 translate(dbg, e, line); 724 fflush(stdout); 725 } 726 727 dwarf_finish(dbg, &de); 728 729 (void) elf_end(e); 730 731 exit(0); 732 } 733