-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwarf_utils.c
411 lines (348 loc) · 10.9 KB
/
dwarf_utils.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
//
// Created by gnowacki on 21.03.18.
//
#include "dwarf_utils.h"
void show_function_address_and_line(Dwarf_Debug dgb, Dwarf_Die the_die)
{
char* die_name = 0;
const char* tag_name = 0;
Dwarf_Error err;
Dwarf_Half tag;
Dwarf_Attribute* attrs;
Dwarf_Addr lowpc;
Dwarf_Signed attrcount;
Dwarf_Signed i;
Dwarf_Unsigned line;
int rc = dwarf_diename(the_die, &die_name, &err);
if(rc == DW_DLV_ERROR)
{
printf("%s", "Error in dwarf_diename\n");
exit(EXIT_FAILURE);
}
else if(rc == DW_DLV_NO_ENTRY)
return;
if (dwarf_tag(the_die, &tag, &err) != DW_DLV_OK)
{
printf("%s", "Error in dwarf_tag\n");
exit(EXIT_FAILURE);
}
/* Only interested in subprogram DIEs here */
if (tag != DW_TAG_subprogram)
return;
if (dwarf_get_TAG_name(tag, &tag_name) != DW_DLV_OK)
{
printf("%s", "Error in dwarf_get_TAG_name\n");
exit(EXIT_FAILURE);
}
/* Grab the DIEs attributes for display */
if (dwarf_attrlist(the_die, &attrs, &attrcount, &err) != DW_DLV_OK)
{
printf("%s", "Error in dwarf_attlist\n");
exit(EXIT_FAILURE);
}
int counter = 0;
for (i = 0; i < attrcount; ++i)
{
Dwarf_Half attrcode;
if (dwarf_whatattr(attrs[i], &attrcode, &err) != DW_DLV_OK) {
printf("%s", "Error in dwarf_whatattr\n");
exit(EXIT_FAILURE);
}
if (attrcode == DW_AT_low_pc)
{
dwarf_formaddr(attrs[i], &lowpc, 0);
}
else
{
counter++;
}
if (attrcode == DW_AT_decl_line)
{
dwarf_formudata(attrs[i], &line, 0);
}
}
if(counter == attrcount - 1)
{
/* Print function name */
printf("<%s> ", die_name);
printf("%d ", line);
printf("0x%08llx\n", lowpc);
}
}
void list_functions_with_address(Dwarf_Debug dbg)
{
Dwarf_Unsigned cu_header_length;
Dwarf_Unsigned abbrev_offset;
Dwarf_Unsigned next_cu_header;
Dwarf_Half version_stamp;
Dwarf_Half address_size;
Dwarf_Error err;
Dwarf_Die no_die = 0;
Dwarf_Die cu_die;
Dwarf_Die child_die;
/* Find next compilation unit header */
if (dwarf_next_cu_header(dbg, &cu_header_length, &version_stamp, &abbrev_offset, &address_size, &next_cu_header, &err) == DW_DLV_ERROR)
{
printf("%s", "Error reading DWARF cu header\n");
exit(EXIT_FAILURE);
}
/* Expect the CU to have a single sibling - a DIE */
if (dwarf_siblingof(dbg, no_die, &cu_die, &err) == DW_DLV_ERROR)
{
printf("%s", "Error getting sibling of CU\n");
exit(EXIT_FAILURE);
}
/* Expect the CU DIE to have children */
if (dwarf_child(cu_die, &child_die, &err) == DW_DLV_ERROR)
{
printf("%s", "Error getting child of CU DIE\n");
exit(EXIT_FAILURE);
}
/* Go over all children DIEs */
while(1)
{
int ret;
show_function_address_and_line(dbg, child_die);
ret = dwarf_siblingof(dbg, child_die, &child_die, &err);
if(ret == DW_DLV_ERROR)
{
printf("%s", "Error getting sibling of DIE\n");
exit(EXIT_FAILURE);
}
else if(ret == DW_DLV_NO_ENTRY)
{
break;
}
}
}
Dwarf_Addr find_function_address(Dwarf_Debug dgb, Dwarf_Die the_die, char* function_name)
{
char* die_name = 0;
const char* tag_name = 0;
Dwarf_Error err;
Dwarf_Half tag;
Dwarf_Attribute* attrs;
Dwarf_Addr lowpc;
Dwarf_Signed attrcount;
Dwarf_Signed i;
Dwarf_Unsigned line;
int rc = dwarf_diename(the_die, &die_name, &err);
if(rc == DW_DLV_ERROR)
{
printf("%s", "Error in dwarf_diename\n");
exit(EXIT_FAILURE);
}
else if(rc == DW_DLV_NO_ENTRY)
return 0;
if(strcmp(die_name, function_name) != 0)
return 0;
if (dwarf_tag(the_die, &tag, &err) != DW_DLV_OK)
{
printf("%s", "Error in dwarf_tag\n");
exit(EXIT_FAILURE);
}
/* Only interested in subprogram DIEs here */
if (tag != DW_TAG_subprogram)
return 0;
if (dwarf_get_TAG_name(tag, &tag_name) != DW_DLV_OK)
{
printf("%s", "Error in dwarf_get_TAG_name\n");
exit(EXIT_FAILURE);
}
/* Grab the DIEs attributes for display */
if (dwarf_attrlist(the_die, &attrs, &attrcount, &err) != DW_DLV_OK)
{
printf("%s", "Error in dwarf_attlist\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < attrcount; ++i)
{
Dwarf_Half attrcode;
if (dwarf_whatattr(attrs[i], &attrcode, &err) != DW_DLV_OK) {
printf("%s", "Error in dwarf_whatattr\n");
exit(EXIT_FAILURE);
}
if (attrcode == DW_AT_low_pc)
{
dwarf_formaddr(attrs[i], &lowpc, 0);
return lowpc;
}
}
return 0;
}
void break_at_function(Dwarf_Debug dbg, pid_t child_pid, const char* command_name, breakpoint_struct** breakpoint_array, int* insert_elem)
{
char function_name[FUNCTION_NAME_LEN];
strncpy(function_name, command_name + 15, FUNCTION_NAME_LEN);
char* new_line_ptr = strchr(function_name, '\n');
*new_line_ptr = '\0';
Dwarf_Unsigned cu_header_length;
Dwarf_Unsigned abbrev_offset;
Dwarf_Unsigned next_cu_header;
Dwarf_Half version_stamp;
Dwarf_Half address_size;
Dwarf_Error err;
Dwarf_Die no_die = 0;
Dwarf_Die cu_die;
Dwarf_Die child_die;
/* Find next compilation unit header */
if (dwarf_next_cu_header(dbg, &cu_header_length, &version_stamp, &abbrev_offset, &address_size, &next_cu_header, &err) == DW_DLV_ERROR)
{
printf("%s", "Error reading DWARF cu header\n");
exit(EXIT_FAILURE);
}
/* Expect the CU to have a single sibling - a DIE */
if (dwarf_siblingof(dbg, no_die, &cu_die, &err) == DW_DLV_ERROR)
{
printf("%s", "Error getting sibling of CU\n");
exit(EXIT_FAILURE);
}
/* Expect the CU DIE to have children */
if (dwarf_child(cu_die, &child_die, &err) == DW_DLV_ERROR)
{
printf("%s", "Error getting child of CU DIE\n");
exit(EXIT_FAILURE);
}
Dwarf_Addr function_address = 0; //typedef unsigned long
/* Go over all children DIEs */
while(1)
{
int ret;
function_address = find_function_address(dbg, child_die, function_name);
if(function_address != 0)
break;
ret = dwarf_siblingof(dbg, child_die, &child_die, &err);
if(ret == DW_DLV_ERROR)
{
printf("%s", "Error getting sibling of DIE\n");
exit(EXIT_FAILURE);
}
else if(ret == DW_DLV_NO_ENTRY)
{
break;
}
}
breakpoint_struct* breakpoint = create_breakpoint(child_pid, (void*)function_address);
breakpoint_array[*insert_elem] = breakpoint;
(*insert_elem)++;
}
void line_address_mapping(Dwarf_Debug dbg)
{
Dwarf_Unsigned cu_header_length;
Dwarf_Unsigned abbrev_offset;
Dwarf_Unsigned next_cu_header;
Dwarf_Half version_stamp;
Dwarf_Half address_size;
Dwarf_Error err;
Dwarf_Die no_die = 0;
Dwarf_Die cu_die;
Dwarf_Die child_die;
int n;
Dwarf_Line *lines;
Dwarf_Signed nlines;
char *filename;
Dwarf_Addr lineaddr;
Dwarf_Unsigned lineno;
/* Find next compilation unit header */
if (dwarf_next_cu_header(dbg, &cu_header_length, &version_stamp, &abbrev_offset, &address_size, &next_cu_header, &err) == DW_DLV_ERROR)
{
printf("%s", "Error reading DWARF cu header\n");
exit(EXIT_FAILURE);
}
/* Expect the CU to have a single sibling - a DIE */
if (dwarf_siblingof(dbg, no_die, &cu_die, &err) == DW_DLV_ERROR)
{
printf("%s", "Error getting sibling of CU\n");
exit(EXIT_FAILURE);
}
if (dwarf_srclines(cu_die, &lines, &nlines, &err) != DW_DLV_OK)
{
printf("%s", "Error dwarf_srclines");
exit(EXIT_FAILURE);
}
for (n = 0; n < nlines; n++) {
/* Retrieve the file name for this descriptor. */
if (dwarf_linesrc(lines[n], &filename, &err))
{
printf("%s", "Error dwarf_linesrc");
exit(EXIT_FAILURE);
}
/* Retrieve the line number in the source file. */
if (dwarf_lineno(lines[n], &lineno, &err))
{
printf("%s", "Error dwarf_lineno");
exit(EXIT_FAILURE);
}
/* Retrieve the virtual address for this line. */
if (dwarf_lineaddr(lines[n], &lineaddr, &err))
{
printf("%s", "Error dwarf_lineaddr");
exit(EXIT_FAILURE);
}
printf("%s %d", filename, lineno);
printf(" 0x%08x\n", lineaddr);
}
}
void break_at_line(Dwarf_Debug dbg, pid_t child_pid, const char* command_name, breakpoint_struct** breakpoint_array, int* insert_elem)
{
char function_line_array[FUNCTION_LINE_LEN];
strncpy(function_line_array, command_name + 11, FUNCTION_LINE_LEN);
char* new_line_ptr = strchr(function_line_array, '\n');
*new_line_ptr = '\0';
unsigned long line_num = (unsigned long)strtol(function_line_array, NULL, 10);
Dwarf_Unsigned cu_header_length;
Dwarf_Unsigned abbrev_offset;
Dwarf_Unsigned next_cu_header;
Dwarf_Half version_stamp;
Dwarf_Half address_size;
Dwarf_Error err;
Dwarf_Die no_die = 0;
Dwarf_Die cu_die;
Dwarf_Die child_die;
int n;
Dwarf_Line *lines;
Dwarf_Signed nlines;
char *filename;
Dwarf_Addr lineaddr;
Dwarf_Unsigned lineno;
/* Find next compilation unit header */
if (dwarf_next_cu_header(dbg, &cu_header_length, &version_stamp, &abbrev_offset, &address_size, &next_cu_header, &err) == DW_DLV_ERROR)
{
printf("%s", "Error reading DWARF cu header\n");
exit(EXIT_FAILURE);
}
/* Expect the CU to have a single sibling - a DIE */
if (dwarf_siblingof(dbg, no_die, &cu_die, &err) == DW_DLV_ERROR)
{
printf("%s", "Error getting sibling of CU\n");
exit(EXIT_FAILURE);
}
if (dwarf_srclines(cu_die, &lines, &nlines, &err) != DW_DLV_OK)
{
printf("%s", "Error dwarf_srclines");
exit(EXIT_FAILURE);
}
for (n = 0; n < nlines; n++) {
/* Retrieve the file name for this descriptor. */
if (dwarf_linesrc(lines[n], &filename, &err))
{
printf("%s", "Error dwarf_linesrc");
exit(EXIT_FAILURE);
}
/* Retrieve the line number in the source file. */
if (dwarf_lineno(lines[n], &lineno, &err))
{
printf("%s", "Error dwarf_lineno");
exit(EXIT_FAILURE);
}
/* Retrieve the virtual address for this line. */
if (dwarf_lineaddr(lines[n], &lineaddr, &err))
{
printf("%s", "Error dwarf_lineaddr");
exit(EXIT_FAILURE);
}
if(lineno == line_num)
break;
}
break_at_address_dwarf(child_pid, lineaddr, breakpoint_array, insert_elem);
}