-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugger.c
More file actions
357 lines (300 loc) · 7.39 KB
/
debugger.c
File metadata and controls
357 lines (300 loc) · 7.39 KB
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
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <dwarf.h>
#include <libdwarf.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/user.h>
#include <libgen.h>
#define DEBUG 0
#ifdef ARM
unsigned int trap=0xe1200070;
#define PC_OFFSET 60
#else
#ifdef INTEL
unsigned char trap=0xcc;
#define PC_OFFSET 128
#endif
#endif
void die(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(EXIT_FAILURE);
}
#ifdef INTEL
typedef struct breakpoint
{
char name[10];
Dwarf_Addr addr;
unsigned char save;
int count;
struct breakpoint *nxt;
}bkpt;
#endif
#ifdef ARM
typedef struct breakpoint
{
char name[10];
Dwarf_Addr addr;
unsigned int save;
int count;
struct breakpoint *nxt;
}bkpt;
#endif
/* List a function if it's in the given DIE.
*/
void list_func_in_die(Dwarf_Debug dgb, Dwarf_Die the_die,bkpt *curr,int count)
{
char* die_name = 0;
const char* tag_name = 0;
Dwarf_Error err;
Dwarf_Half tag;
Dwarf_Attribute* attrs;
Dwarf_Addr lowpc, highpc;
Dwarf_Signed attrcount, i;
int rc = dwarf_diename(the_die, &die_name, &err);
if (rc == DW_DLV_ERROR)
die("Error in dwarf_diename\n");
else if (rc == DW_DLV_NO_ENTRY)
return;
if (dwarf_tag(the_die, &tag, &err) != DW_DLV_OK)
die("Error in dwarf_tag\n");
/* Only interested in subprogram DIEs here */
if (tag != DW_TAG_subprogram)
return;
/* Grab the DIEs attributes for display */
if (dwarf_attrlist(the_die, &attrs, &attrcount, &err) != DW_DLV_OK)
die("Error in dwarf_attlist\n");
for (i = 0; i < attrcount; ++i) {
Dwarf_Half attrcode;
if (dwarf_whatattr(attrs[i], &attrcode, &err) != DW_DLV_OK)
die("Error in dwarf_whatattr\n");
/* We only take some of the attributes for display here.
** More can be picked with appropriate tag constants.
*/
if (attrcode == DW_AT_low_pc)
dwarf_formaddr(attrs[i], &lowpc, 0);
}
// store function address in structure
memcpy(curr->name,die_name,strlen(die_name));
curr->addr=lowpc;
curr->count=count;
}
/* List all the functions from the file represented by the given descriptor.
*/
void list_funcs_in_file(Dwarf_Debug dbg,bkpt **curr)
{
Dwarf_Unsigned cu_header_length, abbrev_offset, next_cu_header;
Dwarf_Half version_stamp, address_size,tag;
Dwarf_Error err;
Dwarf_Die no_die = 0, cu_die, child_die;
bkpt *prev=NULL;
int count=1;
/* Find 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)
die("Error reading DWARF cu header\n");
/* Expect the CU to have a single sibling - a DIE */
if (dwarf_siblingof(dbg, no_die, &cu_die, &err) == DW_DLV_ERROR)
die("Error getting sibling of CU\n");
/* Expect the CU DIE to have children */
if (dwarf_child(cu_die, &child_die, &err) == DW_DLV_ERROR)
die("Error getting child of CU DIE\n");
/* Now go over all children DIEs */
while (1) {
int rc;
if (dwarf_tag(child_die, &tag, &err) != DW_DLV_OK)
die("Error in dwarf_tag\n");
if (tag==DW_TAG_subprogram)
{
*curr=malloc(sizeof(bkpt));
list_func_in_die(dbg, child_die,*curr,count);
(*curr)->nxt=prev;
prev=*curr;
count++;
}
rc = dwarf_siblingof(dbg, child_die, &child_die, &err);
if (rc == DW_DLV_ERROR)
die("Error getting sibling of DIE\n");
else if (rc == DW_DLV_NO_ENTRY)
break; /* done */
}
}
int set_bkpt(int fd,unsigned char trap,bkpt *curr)
{
pread(fd, &(curr->save), sizeof(curr->save), curr->addr);
int ret=pwrite(fd, &trap, sizeof(trap), curr->addr);
if (ret<0)
perror("error proc write ");
return ret;
}
int remove_bkpt(int fd,bkpt *bkpt_hit)
{
int ret=pwrite(fd, &(bkpt_hit->save), sizeof(bkpt_hit->save), bkpt_hit->addr);
if (ret<0)
perror("error proc write ");
}
int main(int argc, char** argv)
{
Dwarf_Debug dbg = 0;
Dwarf_Error err;
const char* progname;
int fd = -1;
bkpt *start;
char *filename;
unsigned char save;
bkpt *console;
int bkpt_count=0;
if (argc < 2) {
fprintf(stderr, "Expected a program name as argument\n");
return 1;
}
progname = argv[1];
if ((fd = open(progname, O_RDONLY)) < 0) {
perror("open");
return 1;
}
filename=basename(progname);
if (dwarf_init(fd, DW_DLC_READ, 0, 0, &dbg, &err) != DW_DLV_OK) {
fprintf(stderr, "Failed DWARF initialization\n");
return 1;
}
list_funcs_in_file(dbg,&start);
if (dwarf_finish(dbg, &err) != DW_DLV_OK) {
fprintf(stderr, "Failed DWARF finalization\n");
return 1;
}
close(fd);
pid_t child;
unsigned long r7,pc;
int status;
char opt[10];
// Debugging begins..
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl(progname, filename,"2\0", NULL);
}
else
{
char file[64];
int ret,fd;
long long eip;
sprintf(file, "/proc/%ld/mem", (long)child);
// wait for child to be stopped by SIGTRAP
wait(&status);
#if DEBUG==1
if (WIFSTOPPED(status))
{
printf("signal %d caused stop\n",WSTOPSIG(status));
}
#endif
fd = open(file, O_RDWR);
if (fd<0)
perror("error process open ");
while(1)
{
int num=1;
int bkpt_opt;
printf("Choose breakpoint locations\n");
console=start;
while(1)
{
printf("%d. %s\t",console->count,console->name);
printf("0x%llx\n",console->addr);
if(console->nxt==NULL)
break;
else
console=console->nxt;
}
scanf("%s",opt);
if (!strcmp("Q",opt) || !strcmp("q",opt))
break;
num=atoi(opt);
console=start;
while(1)
{
if (num==console->count)
{
printf("%s chosen\n",console->name);
set_bkpt(fd,trap,console);
bkpt_count++;
break;
}
if(console->nxt==NULL)
break;
else
console=console->nxt;
}
}
while(1)
{
ptrace(PTRACE_CONT, child, NULL, NULL);
if (bkpt_count>0)
{
wait(&status);
if (WIFSTOPPED(status))
{
printf("signal %d caused stop\n",WSTOPSIG(status));
}
eip = ptrace(PTRACE_PEEKUSER,child,PC_OFFSET,NULL);
if (eip < 0)
perror("error ");
printf("The child is executing %llx\n", eip);
console=start;
eip=eip-1;
while(1)
{
if (eip==console->addr)
{
printf("%s hit\n",console->name);
break;
}
if(console->nxt==NULL)
break;
else
console=console->nxt;
}
scanf("%s",opt);
// Q or q quits the breakpoint selection
if (!strcmp("Q",opt) || !strcmp("q",opt))
break;
else if (!strcmp("c",opt))
{
#if DEBUG==1
pread(fd, &ret, sizeof(ret), console->addr);
printf("%x\n",ret);
#endif
remove_bkpt(fd,console);
ret = ptrace(PTRACE_POKEUSER,child, PC_OFFSET,console->addr);
if (ret < 0)
perror("error poke");
bkpt_count--;
#if DEBUG==1
pread(fd, &ret, sizeof(ret), console->addr);
printf("%x\n",ret);
#endif
}
}
else
break;
}
}
return 0;
}