-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaddr2line.c
511 lines (489 loc) · 16.5 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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <err.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libdwarf.h>
#include <dwarf.h>
#include <libelf.h>
#define MAX_ADDR_LEN 20
#define BATCHMODE_HEURISTIC 100
typedef struct flags {
bool addresses;
bool batchmode;
bool force_batchmode;
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 err_handler(Dwarf_Error err, Dwarf_Ptr errarg)
{
errx(EXIT_FAILURE, "libdwarf error: %d %s0", dwarf_errno(err), dwarf_errmsg(err));
}
static bool pc_in_die(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Addr pc)
{
int ret;
Dwarf_Addr cu_lowpc = DW_DLV_BADADDR, cu_highpc;
enum Dwarf_Form_Class highpc_cls;
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;
}
}
}
Dwarf_Attribute attr;
if (dwarf_attr(die, DW_AT_ranges, &attr, NULL) == DW_DLV_OK) {
Dwarf_Unsigned offset;
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 != DW_DLV_BADADDR) {
baseaddr = cu_lowpc;
}
ret = dwarf_get_ranges_a(dbg, offset, die,
&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 (pc >= rng_lowpc && pc < rng_highpc) {
dwarf_ranges_dealloc(dbg, ranges, count);
dwarf_dealloc(dbg, attr, DW_DLA_ATTR);
return true;
}
} else if (cur->dwr_type == DW_RANGES_ADDRESS_SELECTION) {
baseaddr = cur->dwr_addr2;
} else { // DW_RANGES_END
baseaddr = cu_lowpc;
}
}
dwarf_ranges_dealloc(dbg, ranges, count);
}
dwarf_dealloc(dbg, attr, DW_DLA_ATTR);
}
return false;
}
static void print_line(Dwarf_Debug dbg, flagsT *flags, Dwarf_Line line, Dwarf_Addr pc)
{
char *linesrc;
Dwarf_Unsigned lineno;
if (flags->addresses) {
printf("%#018" DW_PR_DUx "\n", pc);
}
if (line) {
dwarf_linesrc(line, &linesrc, NULL);
dwarf_lineno(line, &lineno, NULL);
} else {
linesrc = "??";
lineno = 0;
}
printf("%s:%" DW_PR_DUu "\n", linesrc, lineno);
if (line) {
dwarf_dealloc(dbg, linesrc, DW_DLA_STRING);
}
}
static 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;
ret = dwarf_srclines_b(cu_die, &version, &table_count, &ctxt, NULL);
if (ret == DW_DLV_NO_ENTRY) {
return false;
}
bool is_found = false;
if (table_count == 1) {
Dwarf_Line *linebuf = 0;
Dwarf_Signed linecount = 0;
Dwarf_Error err;
ret = dwarf_srclines_from_linecontext(ctxt, &linebuf, &linecount, &err);
if (ret == DW_DLV_ERROR) {
dwarf_srclines_dealloc_b(ctxt);
err_handler(err, NULL);
}
Dwarf_Addr prev_lineaddr;
Dwarf_Line prev_line = 0;
for (int i = 0; i < linecount; i++) {
Dwarf_Line line = linebuf[i];
Dwarf_Addr lineaddr;
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 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;
int ret;
int cu_i;
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) {
if (pc_in_die(dbg, cu_die, pc)) {
bool lookup_ret = lookup_pc_cu(dbg, flags, pc, cu_die);
dwarf_dealloc(dbg, cu_die, DW_DLA_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(dbg, cu_die, DW_DLA_DIE);
cu_die = 0;
}
}
}
return false;
}
static bool get_pc_range_die(Dwarf_Die die, Dwarf_Addr *low_out, Dwarf_Addr *high_out)
{
int ret;
Dwarf_Addr lowpc, highpc;
bool is_low_set = false, 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;
}
static 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;
*lowest = DW_DLV_BADADDR;
*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 = DW_DLV_BADADDR, cu_highpc;
enum Dwarf_Form_Class highpc_cls;
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;
}
}
}
Dwarf_Attribute attr;
if (dwarf_attr(cu_die, DW_AT_ranges, &attr, NULL) == DW_DLV_OK) {
Dwarf_Unsigned offset;
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 != DW_DLV_BADADDR) {
baseaddr = cu_lowpc;
}
ret = dwarf_get_ranges_a(dbg, offset, cu_die,
&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_ranges_dealloc(dbg, ranges, count);
}
dwarf_dealloc(dbg, attr, DW_DLA_ATTR);
}
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
cu_die = 0;
}
}
*cu_cnt = cu_i;
return (*lowest != DW_DLV_BADADDR && *highest != 0);
}
static void populate_lookup_table_die(Dwarf_Debug dbg, 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;
}
bool is_found = false;
if (table_count == 1) {
Dwarf_Line *linebuf = 0;
Dwarf_Signed linecount = 0;
Dwarf_Error err;
ret = dwarf_srclines_from_linecontext(lookup_table->ctxts[cu_i], &linebuf, &linecount, &err);
if (ret == DW_DLV_ERROR) {
dwarf_srclines_dealloc_b(lookup_table->ctxts[cu_i]);
err_handler(err, NULL);
}
Dwarf_Addr prev_lineaddr;
Dwarf_Line prev_line = 0;
for (int i = 0; i < linecount; i++) {
Dwarf_Line line = linebuf[i];
Dwarf_Addr lineaddr;
dwarf_lineaddr(line, &lineaddr, NULL);
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;
dwarf_lineendsequence(line, &is_lne, NULL);
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(dbg, 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;
int result = 0;
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, 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;
}
}
}
static void populate_options(int argc, char *argv[], char **objfile, flagsT *flags)
{
int c;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option longopts[] =
{
{"addresses", no_argument, 0, 'a'},
{"exe", required_argument, 0, 'e'},
{"force-batch", no_argument, 0, 'b'},
{"force-no-batch", no_argument, 0, 'n'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "ae:bn", 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 '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[])
{
flagsT flags = {0};
char *objfile = "a.out";
populate_options(argc, argv, &objfile, &flags);
int ret;
Dwarf_Debug dbg;
ret = dwarf_init_path(objfile, NULL, 0, DW_DLC_READ, DW_GROUPNUMBER_ANY, err_handler, NULL, &dbg, 0, 0, 0, NULL);
if (ret == DW_DLV_NO_ENTRY) {
errx(EXIT_FAILURE, "%s not found", objfile);
}
bool do_read_stdin = (optind >= argc);
if (! flags.force_nobatchmode && (flags.force_batchmode || do_read_stdin || (argc + BATCHMODE_HEURISTIC) > optind)) {
flags.batchmode = true;
}
lookup_tableT lookup_table;
if (flags.batchmode) {
create_lookup_table(dbg, &lookup_table);
}
char buf[MAX_ADDR_LEN], *pc_buf, *endptr;
while ((pc_buf = get_pc_buf(argc, argv, buf, do_read_stdin))) {
Dwarf_Addr pc = strtoull(pc_buf, &endptr, 16);
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, NULL);
return 0;
}