forked from Crablicious/libdwarf-addr2line
-
Notifications
You must be signed in to change notification settings - Fork 4
/
addr2line.c
762 lines (706 loc) · 20.8 KB
/
addr2line.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* addr2line.c
The code does not attempt to report details
when an unexpected DW_DLV_NO_ENTRY is received
from libdwarf. It just silently moves on.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "dwarf.h"
#include "libdwarf.h"
#define DW_PR_DUx "llx"
#define DW_PR_DUu "llu"
#define MAX_ADDR_LEN 20
#define BATCHMODE_HEURISTIC 100
#define DWARF5_VERSION 5
#define TRUE 1
#define FALSE 0
#define HIGHADDR (Dwarf_Unsigned)0xffffffffffffffff
static const char *objfile_name = "<none>";
typedef struct flags {
Dwarf_Bool addresses;
Dwarf_Bool batchmode;
Dwarf_Bool force_batchmode;
Dwarf_Bool force_nobatchmode;
} flagsT;
typedef struct lookup_table {
Dwarf_Line *table;
Dwarf_Line_Context *ctxts;
int cnt;
Dwarf_Addr low;
Dwarf_Addr high;
} lookup_tableT;
static void
fail_exit(const char *msg) {
printf("addr2line failure reading %s: %s\n",
objfile_name,
msg);
exit(1);
}
static void
err_handler(Dwarf_Error err, Dwarf_Ptr errarg)
{
printf("libdwarf error reading %s: %lu %s\n",
objfile_name,
(unsigned long)dwarf_errno(err),
dwarf_errmsg(err));
if (errarg) {
printf("Error: errarg is nonnull but it should be null\n");
}
printf("Giving up");
exit(1);
}
static int
dwarf5_ranges(Dwarf_Die cu_die,
Dwarf_Addr *lowest,
Dwarf_Addr *highest);
static int dwarf4_ranges( Dwarf_Debug dbg,
Dwarf_Die cu_die,
Dwarf_Addr cu_lowpc,
Dwarf_Addr *lowest,
Dwarf_Addr *highest);
static Dwarf_Bool
pc_in_die(Dwarf_Debug dbg, Dwarf_Die die,int version, Dwarf_Addr pc)
{
int ret;
Dwarf_Addr cu_lowpc = HIGHADDR;
Dwarf_Addr cu_highpc = 0;
enum Dwarf_Form_Class highpc_cls;
Dwarf_Addr lowest = HIGHADDR;
Dwarf_Addr highest = 0;
ret = dwarf_lowpc(die, &cu_lowpc, NULL);
if (ret == DW_DLV_OK) {
if (pc == cu_lowpc) {
return TRUE;
}
ret = dwarf_highpc_b(die, &cu_highpc,
NULL, &highpc_cls, NULL);
if (ret == DW_DLV_OK) {
if (highpc_cls == DW_FORM_CLASS_CONSTANT) {
cu_highpc += cu_lowpc;
}
if (pc >= cu_lowpc && pc < cu_highpc) {
return TRUE;
}
}
}
if (version >= DWARF5_VERSION) {
ret = dwarf5_ranges(die,
&lowest,&highest);
} else {
ret = dwarf4_ranges(dbg,die,cu_lowpc,
&lowest,&highest);
}
if (pc >= lowest && pc < highest) {
return TRUE;
}
return FALSE;
}
static void
print_line(Dwarf_Debug dbg,
flagsT *flags,
Dwarf_Line line,
Dwarf_Addr pc)
{
char * linesrc = "??";
Dwarf_Unsigned lineno = 0;
if (flags->addresses) {
printf("%#018" DW_PR_DUx "\n", pc);
}
if (line) {
/* These never return DW_DLV_NO_ENTRY */
dwarf_linesrc(line, &linesrc, NULL);
dwarf_lineno(line, &lineno, NULL);
}
printf("%s:%" DW_PR_DUu "\n", linesrc, lineno);
if (line) {
dwarf_dealloc(dbg, linesrc, DW_DLA_STRING);
}
}
static Dwarf_Bool
lookup_pc_cu(Dwarf_Debug dbg,
flagsT *flags,
Dwarf_Addr pc,
Dwarf_Die cu_die)
{
int ret;
Dwarf_Unsigned version;
Dwarf_Small table_count;
Dwarf_Line_Context ctxt;
Dwarf_Bool is_found = FALSE;
ret = dwarf_srclines_b(cu_die, &version, &table_count,
&ctxt, NULL);
if (ret == DW_DLV_NO_ENTRY) {
return FALSE;
}
if (table_count == 1) {
Dwarf_Line *linebuf = 0;
Dwarf_Signed linecount = 0;
Dwarf_Addr prev_lineaddr = 0;
dwarf_srclines_from_linecontext(ctxt, &linebuf,
&linecount, NULL);
Dwarf_Line prev_line = 0;
for (int i = 0; i < linecount; i++) {
Dwarf_Line line = linebuf[i];
Dwarf_Addr lineaddr = 0;
dwarf_lineaddr(line, &lineaddr, NULL);
if (pc == lineaddr) {
/* Print the last line entry containing current pc. */
Dwarf_Line last_pc_line = line;
for (int j = i + 1; j < linecount; j++) {
Dwarf_Line j_line = linebuf[j];
dwarf_lineaddr(j_line, &lineaddr, NULL);
if (pc == lineaddr) {
last_pc_line = j_line;
}
}
is_found = TRUE;
print_line(dbg, flags, last_pc_line, pc);
break;
} else if (prev_line && pc > prev_lineaddr &&
pc < lineaddr) {
is_found = TRUE;
print_line(dbg, flags, prev_line, pc);
break;
}
Dwarf_Bool is_lne;
dwarf_lineendsequence(line, &is_lne, NULL);
if (is_lne) {
prev_line = 0;
} else {
prev_lineaddr = lineaddr;
prev_line = line;
}
}
}
dwarf_srclines_dealloc_b(ctxt);
return is_found;
}
static Dwarf_Bool
lookup_pc(Dwarf_Debug dbg,
flagsT *flags,
Dwarf_Addr pc)
{
Dwarf_Bool is_info = TRUE;
Dwarf_Unsigned next_cu_header;
Dwarf_Half header_cu_type;
Dwarf_Half dwversion = 0;
Dwarf_Half offset_size = 0;
int ret;
for (int cu_i = 0;; cu_i++) {
ret = dwarf_next_cu_header_d(dbg, is_info, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, &next_cu_header, &header_cu_type, NULL);
if (ret == DW_DLV_NO_ENTRY) {
break;
}
Dwarf_Die cu_die = 0;
ret = dwarf_siblingof_b(dbg, 0, is_info, &cu_die, NULL);
if (ret == DW_DLV_OK) {
dwarf_get_version_of_die(cu_die,&dwversion,&offset_size);
if (pc_in_die(dbg, cu_die,dwversion, pc)) {
Dwarf_Bool lookup_ret = lookup_pc_cu(dbg, flags,
pc, cu_die);
dwarf_dealloc_die(cu_die);
while (dwarf_next_cu_header_d(dbg, is_info,
NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, &next_cu_header,
&header_cu_type, NULL)
!= DW_DLV_NO_ENTRY) {}
return lookup_ret;
} else {
dwarf_dealloc_die(cu_die);
cu_die = 0;
}
}
}
return FALSE;
}
#if 0
static Dwarf_Bool
get_pc_range_die(Dwarf_Die die,
Dwarf_Addr *low_out,
Dwarf_Addr *high_out)
{
int ret;
Dwarf_Addr lowpc, highpc;
Dwarf_Bool is_low_set = FALSE;
Dwarf_Bool is_high_set = FALSE;
enum Dwarf_Form_Class highpc_cls;
ret = dwarf_lowpc(die, &lowpc, NULL);
if (ret == DW_DLV_OK) {
is_low_set = TRUE;
*low_out = lowpc;
}
if (is_low_set) {
ret = dwarf_highpc_b(die, &highpc, NULL, &highpc_cls, NULL);
if (ret == DW_DLV_OK) {
if (highpc_cls == DW_FORM_CLASS_CONSTANT) {
*high_out = lowpc + highpc;
} else {
*high_out = highpc;
}
is_high_set = TRUE;
}
}
return is_low_set && is_high_set;
}
#endif
static int
get_rnglist_offset(Dwarf_Attribute attr, Dwarf_Unsigned* offset) {
Dwarf_Half attrform = 0;
dwarf_whatform(attr, &attrform, NULL);
if (attrform == DW_FORM_rnglistx) {
return dwarf_formudata(attr, offset, NULL);
} else {
return dwarf_global_formref(attr, offset, NULL);
}
}
static int
dwarf5_ranges(Dwarf_Die cu_die,
Dwarf_Addr *lowest,
Dwarf_Addr *highest)
{
Dwarf_Unsigned offset = 0;
Dwarf_Attribute attr = 0;
Dwarf_Half attrform = 0;
Dwarf_Unsigned i = 0;
int res = 0;
res = dwarf_attr(cu_die, DW_AT_ranges, &attr, NULL);
if (res != DW_DLV_OK) {
return res;
}
if (get_rnglist_offset(attr, &offset) == DW_DLV_OK) {
Dwarf_Unsigned rlesetoffset = 0;
Dwarf_Unsigned rnglists_count = 0;
Dwarf_Rnglists_Head head = 0;
dwarf_whatform(attr,&attrform,NULL);
/* offset either a global offset (FORM == DW_FORM_sec_offset)
or an index into .debug_rnglists
FORM = DW_FORM_rnglistx. The following call takes care
of the details */
res = dwarf_rnglists_get_rle_head(attr, attrform,offset,
&head,
&rnglists_count,&rlesetoffset,NULL);
if (res != DW_DLV_OK) {
/* ASSERT: is DW_DLV_NO_ENTRY */
dwarf_dealloc_attribute(attr);
return res;
}
for ( ; i < rnglists_count; ++i) {
unsigned entrylen = 0;
unsigned rle_val = 0;
Dwarf_Unsigned raw1 = 0;
Dwarf_Unsigned raw2 = 0;
Dwarf_Bool unavail = 0;
Dwarf_Unsigned cooked1 = 0;
Dwarf_Unsigned cooked2 = 0;
res = dwarf_get_rnglists_entry_fields_a(head,
i,&entrylen,&rle_val,&raw1,&raw2,
&unavail,&cooked1,&cooked2,NULL);
if (res != DW_DLV_OK) {
/* ASSERT: is DW_DLV_NO_ENTRY */
continue;
}
if (unavail) {
continue;
}
switch(rle_val) {
case DW_RLE_end_of_list:
case DW_RLE_base_address:
case DW_RLE_base_addressx:
/* These are accounted for already */
break;
case DW_RLE_offset_pair:
case DW_RLE_startx_endx:
case DW_RLE_start_end:
case DW_RLE_startx_length:
case DW_RLE_start_length:
if (cooked1 < *lowest) {
*lowest = cooked1;
}
if (cooked2 > *highest) {
*highest = cooked2;
}
default:
/* Something is wrong. */
break;
}
}
dwarf_dealloc_rnglists_head(head);
}
dwarf_dealloc_attribute(attr);
return DW_DLV_OK;
}
static int
dwarf4_ranges( Dwarf_Debug dbg,
Dwarf_Die cu_die,
Dwarf_Addr cu_lowpc,
Dwarf_Addr *lowest,
Dwarf_Addr *highest)
{
Dwarf_Unsigned offset;
Dwarf_Attribute attr = 0;
int res = 0;
res = dwarf_attr(cu_die, DW_AT_ranges, &attr, NULL);
if (res != DW_DLV_OK) {
return res;
}
if (dwarf_global_formref(attr, &offset, NULL) == DW_DLV_OK) {
Dwarf_Signed count = 0;
Dwarf_Ranges *ranges = 0;
Dwarf_Addr baseaddr = 0;
if (cu_lowpc != HIGHADDR) {
baseaddr = cu_lowpc;
}
res = dwarf_get_ranges_b(dbg, offset, cu_die,
NULL, &ranges, &count, NULL, NULL);
for (int i = 0; i < count; i++) {
Dwarf_Ranges *cur = ranges + i;
if (cur->dwr_type == DW_RANGES_ENTRY) {
Dwarf_Addr rng_lowpc, rng_highpc;
rng_lowpc = baseaddr + cur->dwr_addr1;
rng_highpc = baseaddr + cur->dwr_addr2;
if (rng_lowpc < *lowest) {
*lowest = rng_lowpc;
}
if (rng_highpc > *highest) {
*highest = rng_highpc;
}
} else if (cur->dwr_type ==
DW_RANGES_ADDRESS_SELECTION) {
baseaddr = cur->dwr_addr2;
} else { // DW_RANGES_END
baseaddr = cu_lowpc;
}
}
dwarf_dealloc_ranges(dbg, ranges, count);
}
dwarf_dealloc_attribute(attr);
return DW_DLV_OK;
}
static Dwarf_Bool
get_pc_range(Dwarf_Debug dbg,
Dwarf_Addr *lowest,
Dwarf_Addr *highest,
int *cu_cnt)
{
Dwarf_Bool is_info = TRUE;
Dwarf_Unsigned next_cu_header;
Dwarf_Half header_cu_type;
Dwarf_Half dwversion = 0;
Dwarf_Half offset_size = 0;
*lowest = HIGHADDR;
*highest = 0;
int ret, cu_i;
for (cu_i = 0;; cu_i++) {
ret = dwarf_next_cu_header_d(dbg, is_info, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, &next_cu_header, &header_cu_type, NULL);
if (ret == DW_DLV_NO_ENTRY) {
break;
}
Dwarf_Die cu_die = 0;
ret = dwarf_siblingof_b(dbg, 0, is_info, &cu_die, NULL);
if (ret == DW_DLV_OK) {
Dwarf_Addr cu_lowpc = HIGHADDR, cu_highpc;
enum Dwarf_Form_Class highpc_cls;
dwarf_get_version_of_die(cu_die,&dwversion,&offset_size);
ret = dwarf_lowpc(cu_die, &cu_lowpc, NULL);
if (ret == DW_DLV_OK) {
if (cu_lowpc < *lowest) {
*lowest = cu_lowpc;
}
ret = dwarf_highpc_b(cu_die, &cu_highpc,
NULL, &highpc_cls, NULL);
if (ret == DW_DLV_OK) {
if (highpc_cls == DW_FORM_CLASS_CONSTANT) {
cu_highpc += cu_lowpc;
}
if (cu_highpc > *highest) {
*highest = cu_highpc;
}
}
}
if (dwversion >= DWARF5_VERSION) {
ret = dwarf5_ranges(cu_die,
lowest,highest);
} else {
ret = dwarf4_ranges(dbg,cu_die,cu_lowpc,
lowest,highest);
}
if (ret == DW_DLV_ERROR) {
return 0;
}
dwarf_dealloc_die(cu_die);
cu_die = 0;
}
}
*cu_cnt = cu_i;
return (*lowest != HIGHADDR && *highest != 0);
}
static void
populate_lookup_table_die(lookup_tableT *lookup_table,
int cu_i,
Dwarf_Die cu_die)
{
int ret;
Dwarf_Unsigned version;
Dwarf_Small table_count;
ret = dwarf_srclines_b(cu_die, &version, &table_count,
&lookup_table->ctxts[cu_i], NULL);
if (ret == DW_DLV_NO_ENTRY) {
return;
}
if (table_count == 1) {
Dwarf_Line *linebuf = 0;
Dwarf_Signed linecount = 0;
ret = dwarf_srclines_from_linecontext(
lookup_table->ctxts[cu_i], &linebuf, &linecount, NULL);
if (ret == DW_DLV_NO_ENTRY) {
dwarf_srclines_dealloc_b(lookup_table->ctxts[cu_i]);
return;
}
Dwarf_Addr prev_lineaddr;
Dwarf_Line prev_line = 0;
for (int i = 0; i < linecount; i++) {
Dwarf_Line line = linebuf[i];
Dwarf_Addr lineaddr = 0;
ret = dwarf_lineaddr(line, &lineaddr, NULL);
if (ret == DW_DLV_NO_ENTRY) {
continue;
}
if (prev_line) {
for (Dwarf_Addr addr = prev_lineaddr;
addr < lineaddr; addr++) {
lookup_table->table[addr - lookup_table->low] =
linebuf[i - 1];
}
}
Dwarf_Bool is_lne = 0;
ret = dwarf_lineendsequence(line, &is_lne, NULL);
if (ret == DW_DLV_NO_ENTRY) {
continue;
}
if (is_lne) {
prev_line = 0;
} else {
prev_lineaddr = lineaddr;
prev_line = line;
}
}
}
}
static void
populate_lookup_table(Dwarf_Debug dbg,
lookup_tableT *lookup_table)
{
Dwarf_Bool is_info = TRUE;
Dwarf_Unsigned next_cu_header;
Dwarf_Half header_cu_type;
int ret;
for (int cu_i = 0;; cu_i++) {
ret = dwarf_next_cu_header_d(dbg, is_info, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL, &next_cu_header, &header_cu_type, NULL);
if (ret == DW_DLV_NO_ENTRY) {
break;
}
Dwarf_Die cu_die = 0;
ret = dwarf_siblingof_b(dbg, 0, is_info, &cu_die, NULL);
if (ret == DW_DLV_OK) {
populate_lookup_table_die(lookup_table,
cu_i, cu_die);
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
cu_die = 0;
}
}
}
static int
create_lookup_table(Dwarf_Debug dbg,
lookup_tableT *lookup_table)
{
Dwarf_Addr low, high;
int cu_cnt;
if (!get_pc_range(dbg, &low, &high, &cu_cnt)) {
goto exit;
}
lookup_table->table = malloc((high - low) * sizeof(Dwarf_Line));
if (! lookup_table->table) {
goto exit;
}
lookup_table->ctxts = malloc(cu_cnt * sizeof(Dwarf_Line_Context));
if (! lookup_table->ctxts) {
goto free_table_exit;
}
lookup_table->cnt = cu_cnt;
lookup_table->low = low;
lookup_table->high = high;
populate_lookup_table(dbg, lookup_table);
return 0;
free_table_exit:
free(lookup_table->table);
exit:
lookup_table->table = NULL;
lookup_table->ctxts = NULL;
return 1;
}
static void
delete_lookup_table(lookup_tableT *lookup_table)
{
free(lookup_table->table);
lookup_table->table = NULL;
for (int i = 0; i < lookup_table->cnt; i++) {
dwarf_srclines_dealloc_b(lookup_table->ctxts[i]);
}
free(lookup_table->ctxts);
lookup_table->ctxts = NULL;
}
static char *
get_pc_buf(int argc, char **argv, char *buf,
Dwarf_Bool do_read_stdin)
{
if (do_read_stdin) {
return fgets(buf, MAX_ADDR_LEN, stdin);
} else {
if (optind < argc) {
return argv[optind++];
} else {
return NULL;
}
}
}
char *usagestrings[] = {
"addr2line [ -h] [-a] [-e <objectpath>] [-b] [-n] [address] ...",
"where",
"-a --addresses Turns on printing of address before",
" the source line text",
"-e --exe <path> The the path to the object file to read.",
" Path defaults to \"a.out\"",
"-b --force-batch The CU address ranges will be looked",
" up once at the start and the generated table used.",
"-n --force-no-batch The addresses are looked up",
" independently for each address present.",
" In certain cases the no-batch will be overridden",
" and batching used.",
"-h --help Prints a help message and stops.",
0
};
static void
usage(void)
{
char **u = usagestrings;
for( ; *u ; u++) {
printf("%s\n",*u);
}
exit(0);
}
static void
populate_options(int argc, char *argv[], char **objfile,
flagsT *flags)
{
int c;
while (1) {
int option_index = 0;
static struct option longopts[] =
{
{"addresses", no_argument, 0, 'a'},
{"exe", required_argument, 0, 'e'},
{"help", no_argument, 0, 'h'},
{"force-batch", no_argument, 0, 'b'},
{"force-no-batch", no_argument, 0, 'n'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "ae:hbn", longopts,
&option_index);
if (c == -1) {
break;
}
switch (c) {
case 'a':
flags->addresses = TRUE;
break;
case 'e':
*objfile = optarg;
break;
case 'b':
flags->force_batchmode = TRUE;
break;
case 'h':
usage();
break;
case 'n':
flags->force_nobatchmode = TRUE;
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
break;
}
}
}
int
main(int argc, char *argv[])
{
int ret;
Dwarf_Debug dbg;
char *objfile = "a.out";
Dwarf_Bool do_read_stdin = FALSE;
char buf[MAX_ADDR_LEN];
char *pc_buf = 0;
char *endptr = 0;
lookup_tableT lookup_table;
Dwarf_Ptr errarg = 0;
flagsT flags = {0};
populate_options(argc, argv, &objfile, &flags);
objfile_name = objfile;
ret = dwarf_init_path(objfile, NULL, 0,
DW_GROUPNUMBER_ANY, err_handler, errarg, &dbg, NULL);
if (ret == DW_DLV_NO_ENTRY) {
fail_exit("Unable to open file");
}
do_read_stdin = (optind >= argc);
if (! flags.force_nobatchmode &&
(flags.force_batchmode || do_read_stdin ||
(argc + BATCHMODE_HEURISTIC) > optind)) {
flags.batchmode = TRUE;
}
if (flags.batchmode) {
ret = create_lookup_table(dbg, &lookup_table);
if (ret != DW_DLV_OK) {
fail_exit("Unable to create lookup table");
}
}
while ((pc_buf = get_pc_buf(argc, argv, buf, do_read_stdin))) {
Dwarf_Addr pc = strtoull(pc_buf, &endptr, 16);
Dwarf_Bool is_found = FALSE;
if (endptr != pc_buf) {
if (flags.batchmode && lookup_table.table) {
if (pc >= lookup_table.low &&
pc < lookup_table.high) {
Dwarf_Line line =
lookup_table.table[pc - lookup_table.low];
if (line) {
print_line(dbg, &flags, line, pc);
is_found = TRUE;
}
}
} else {
is_found = lookup_pc(dbg, &flags, pc);
}
}
if (! is_found) {
print_line(dbg, &flags, NULL, pc);
}
}
if (flags.batchmode && lookup_table.table) {
delete_lookup_table(&lookup_table);
}
dwarf_finish(dbg);
return 0;
}