-
Notifications
You must be signed in to change notification settings - Fork 2
/
dm_dwarf.c
326 lines (264 loc) · 7.69 KB
/
dm_dwarf.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
/*
* Copyright (c) 2011 Edd Barrett <[email protected]>
*
* This code was based upon the simplereader.c example distrubuted with
* dwarfutils. The original copyright and license follows.
*/
/*
Copyright (c) 2009-2010 David Anderson. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the example nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL David Anderson BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <libdwarf.h>
#include <dwarf.h>
#include "tree.h"
#include "dm_dwarf.h"
#include "common.h"
extern FILE *f;
RB_HEAD(dm_dwarf_sym_cache_, dm_dwarf_sym_cache_entry)
dm_dwarf_sym_cache = RB_INITIALIZER(&dm_dwarf_sym_cache);
RB_GENERATE(dm_dwarf_sym_cache_,
dm_dwarf_sym_cache_entry, entry, dm_dwarf_sym_rb_cmp);
int
dm_dwarf_sym_rb_cmp(struct dm_dwarf_sym_cache_entry *s1,
struct dm_dwarf_sym_cache_entry *s2)
{
return (strcmp(s1->name, s2->name));
}
int
dm_cmd_dwarf_funcs(char **args)
{
struct dm_dwarf_sym_cache_entry *sym;
int n = 0;
(void) args;
printf("\n");
RB_FOREACH(sym, dm_dwarf_sym_cache_, &dm_dwarf_sym_cache) {
/* reprint headers evert 20 lines */
if (n % 20 == 0) {
printf("%s\n", DM_RULE);
printf("%-40s | %-10s | %-10s\n", "Function", "Virtual", "Offset");
printf("%s\n", DM_RULE);
}
if (!sym->offset_err)
printf("%-40s | " ADDR_FMT_64 " | " ADDR_FMT_64 "\n",
sym->name, sym->vaddr, sym->offset);
else
printf("%-40s | " ADDR_FMT_64 " | %s\n",
sym->name, sym->vaddr, "???");
n++;
}
printf("%s\n\n", DM_RULE);
return (DM_OK);
}
int
dm_parse_dwarf()
{
Dwarf_Debug dbg = 0;
Dwarf_Error error;
Dwarf_Handler errhand = 0;
Dwarf_Ptr errarg = 0;
int ret = DM_FAIL;
if (dwarf_init(fileno(file_info.fptr), DW_DLC_READ, errhand,
errarg, &dbg, &error) != DW_DLV_OK) {
DPRINTF(DM_D_DEBUG, "Can't parse dwarf info. Probably none.");
goto error;
}
file_info.dwarf = 1;
if (dm_dwarf_recurse_cu(dbg) != DM_OK)
goto error;
if (dwarf_finish(dbg,&error) != DW_DLV_OK) {
DPRINTF(DM_D_WARN, "Failed to clean up");
goto error;
}
ret = DM_OK;
error:
return (ret);
}
int
dm_dwarf_recurse_cu(Dwarf_Debug dbg)
{
Dwarf_Unsigned cu_header_length = 0;
Dwarf_Half version_stamp = 0;
Dwarf_Unsigned abbrev_offset = 0;
Dwarf_Half address_size = 0;
Dwarf_Unsigned next_cu_header = 0;
Dwarf_Error error;
int cu_number = 0, res;
Dwarf_Die no_die = 0;
Dwarf_Die cu_die = 0;
for(;;++cu_number) {
no_die = cu_die = 0;
res = DW_DLV_ERROR;
res = dwarf_next_cu_header(dbg,&cu_header_length,
&version_stamp, &abbrev_offset, &address_size,
&next_cu_header, &error);
if (res == DW_DLV_ERROR) {
DPRINTF(DM_D_DEBUG, "dwarf_next_cu_header failed");
return (DM_FAIL);
}
if (res == DW_DLV_NO_ENTRY)
return (DM_OK); /* done */
/* The CU will have a single sibling, a cu_die. */
res = dwarf_siblingof(dbg, no_die, &cu_die, &error);
if (res == DW_DLV_ERROR) {
DPRINTF(DM_D_DEBUG, "siblingof failed");
return (DM_FAIL);
}
if (res == DW_DLV_NO_ENTRY) { /* Impossible case. */
DPRINTF(DM_D_WARN, "impossible case");
return (DM_FAIL);
}
dm_dwarf_recurse_die(dbg, cu_die);
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
}
}
int
dm_dwarf_recurse_die(Dwarf_Debug dbg, Dwarf_Die in_die)
{
int res = DW_DLV_ERROR;
Dwarf_Die cur_die = in_die, child = 0, sib_die = 0;
Dwarf_Error error;
dm_dwarf_inspect_die(dbg, in_die);
for (;;) {
sib_die = 0;
res = dwarf_child(cur_die,&child,&error);
if (res == DW_DLV_ERROR) {
DPRINTF(DM_D_DEBUG, "dwarf_child");
return (DM_FAIL);
}
if (res == DW_DLV_OK)
dm_dwarf_recurse_die(dbg, child);
res = dwarf_siblingof(dbg, cur_die, &sib_die, &error);
if (res == DW_DLV_ERROR) {
DPRINTF(DM_D_DEBUG, "siblingof");
return (DM_FAIL);
}
if (res == DW_DLV_NO_ENTRY)
break; /* Done at this level. */
if (cur_die != in_die)
dwarf_dealloc(dbg, cur_die, DW_DLA_DIE);
cur_die = sib_die;
dm_dwarf_inspect_die(dbg, cur_die);
}
return (DM_OK);
}
int
dm_dwarf_inspect_die(Dwarf_Debug dbg, Dwarf_Die print_me)
{
char *name = 0;
Dwarf_Error error = 0;
Dwarf_Half tag = 0;
const char *tagname = 0;
int res;
Dwarf_Addr lo;
ADDR64 offset;
int offset_err = 0, ret = DM_FAIL;
struct dm_dwarf_sym_cache_entry *sym_rec;
res = dwarf_diename(print_me, &name, &error);
if (res == DW_DLV_ERROR) {
DPRINTF(DM_D_DEBUG, "diename");
goto clean;
}
if (res == DW_DLV_NO_ENTRY)
goto clean;
res = dwarf_tag(print_me, &tag, &error);
if (res != DW_DLV_OK) {
DPRINTF(DM_D_DEBUG, "dwarf_tag");
goto clean;
}
if (tag != DW_TAG_subprogram) {
ret = DM_OK;
goto clean; /* we only extract funcs for now */
}
/* get virtual addr */
res = dwarf_lowpc(print_me, &lo, &error);
if (res != DW_DLV_OK) {
DPRINTF(DM_D_DEBUG, "lopc");
if (res == DW_DLV_NO_ENTRY)
DPRINTF(DM_D_DEBUG, "no_entry");
goto clean;
}
/* get the function name */
res = dwarf_get_TAG_name(tag, &tagname);
if (res != DW_DLV_OK) {
DPRINTF(DM_D_DEBUG, "TAG_name");
goto clean;
}
offset_err = 0;
if ((dm_offset_from_vaddr(lo, &offset)) != DM_OK)
offset_err = 1;
sym_rec = calloc(1, sizeof(struct dm_dwarf_sym_cache_entry));
sym_rec->name = strdup(name);
sym_rec->vaddr = lo;
sym_rec->offset = offset;
sym_rec->sym_type = DW_TAG_subprogram;
sym_rec->offset_err = offset_err;
RB_INSERT(dm_dwarf_sym_cache_, &dm_dwarf_sym_cache, sym_rec);
clean:
if (name)
dwarf_dealloc(dbg,name,DW_DLA_STRING);
return (DM_OK);
}
int
dm_clean_dwarf()
{
struct dm_dwarf_sym_cache_entry *var, *nxt;
for (var = RB_MIN(dm_dwarf_sym_cache_,
&dm_dwarf_sym_cache); var != NULL; var = nxt) {
nxt = RB_NEXT(dm_dwarf_sym_cache_, &dm_dwarf_sym_cache, var);
RB_REMOVE(dm_dwarf_sym_cache_, &dm_dwarf_sym_cache, var);
free(var->name);
free(var);
}
return (DM_OK);
}
int
dm_dwarf_find_sym(char *name, struct dm_dwarf_sym_cache_entry **s)
{
struct dm_dwarf_sym_cache_entry sym, *res;
sym.name = name;
res = RB_FIND(dm_dwarf_sym_cache_, &dm_dwarf_sym_cache, &sym);
if (!res)
return (DM_FAIL);
/* found */
*s = res;
return (DM_OK);
}
int
dm_dwarf_find_sym_at_offset(ADDR64 off, struct dm_dwarf_sym_cache_entry **ent)
{
struct dm_dwarf_sym_cache_entry *e;
/* rbtree is keyed by symname, so we must iterate */
RB_FOREACH(e, dm_dwarf_sym_cache_, &dm_dwarf_sym_cache) {
if (e->offset == off) {
*ent = e;
return (DM_OK);
}
}
return (DM_FAIL);
}