-
Notifications
You must be signed in to change notification settings - Fork 1
/
legerdemain.c
582 lines (455 loc) · 13.6 KB
/
legerdemain.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
/*Included System Headers*/
/*for backtrace_symbols*/
#include <execinfo.h>
/*for size_t*/
#include <sys/types.h>
/*for getenv*/
#include <stdlib.h>
/*for fprintf*/
#include <stdio.h>
/*for readlink*/
#include <unistd.h>
/*for SIG_ constants, sigaction, etc*/
#include <signal.h>
/*for readline() support*/
#include <readline/readline.h>
/*Defined to ensure RTLD_NEXT is defined in dlfcn.h*/
#define __USE_GNU
/*for dlopen, dlsym, etc*/
#include <dlfcn.h>
/*for get_info()*/
#include "addr2line.h"
/*for applyToTokens*/
#include "Applier.h"
/*for dwarf manipulation support*/
#include "dwarfclient.h"
/*for various project-specific macros and typedefs*/
#include "legerdemain.h"
/*
* These will be used to preserve the program's
* original signal handlers when LDM replaces them
*/
struct sigaction sigABRTSaver;
struct sigaction sigSEGVSaver;
struct sigaction sigTERMSaver;
struct sigaction sigINTSaver;
static void *curDebugInstr;
static void *curDebugFramePtr;
/*LDM's constructor and destructor*/
void __attribute__ ((constructor)) LDM_init();
void __attribute__ ((destructor)) LDM_deinit();
/*The executable name*/
static char *LDM_ProgramName;
/*Records whether the program is running or not*/
static bool LDM_runstate;
/*
* The following functions wrap the dwarf information
* access support provided by libdwarfclient. See
* libdwarfclient for more information about what
* each of these does.
*/
/*
* Print the instruction pointer of the first instruction
* on line 'line' of the file 'file'
*/
void LDM_line_to_addr(char *file, int line){
void * a = (void*)get_iaddr_of_file_line(file,line);
fprintf(stderr,"%p\n",a);
}
long LDM_locate_var(void *addr,char *var){
long loc = get_location_of_scoped_variable(addr,var);
return loc;
}
DC_type *LDM_gettype_var(void *addr,char *var){
return get_type_of_scoped_variable(addr,var);
}
void LDM_vars_a(void *addr){
show_vars_by_scope_addr(addr);
}
/*
* Print the sequence of scopes and the variables
* in each for the provided file and line. The scopes
* will be descended into, starting at the top-most
* scope of the compilation unit that the file and line
* is part of
*/
void LDM_scope_f(char *file, int line){
show_scopes_by_file_line(file,line);
}
/*
* Same functionality as LDM_scope_f(), except searches based
* on an instruction address, instead of a file:line pair
*/
void LDM_scope_a(void *addr){
show_scopes_by_addr(addr);
}
/*
* Gets the dwarf information for a variable 'varname',
* given an instruction address in a scope in
* which to search for a variable named 'varname'.
*/
void LDM_inspect(void *addr,const char *varname){
//show_info_for_scoped_variable(addr,varname);
//fprintf(stderr,"done showing the info\n");
long loc = LDM_locate_var(addr,(char *)varname);
DC_type *t = LDM_gettype_var(addr,(char*)varname);
if( loc < 0 ){
loc += 16;
}
fprintf(stderr,"%s ",t->name);
int i;
for(i = 0; i < t->indirectionLevel; i++){
fprintf(stderr,"*");
}
fprintf(stderr,"%s",varname);
for(i = 0; i < t->arrayLevel; i++){
fprintf(stderr,"[]");
}
fprintf(stderr," = ");
for(i = 1; i <= t->byteSize; i++){
fprintf(stderr,"%hhx",*((unsigned char*)(curDebugFramePtr + loc + (t->byteSize-i))));
}
fprintf(stderr,"\n");
return;
}
/*Show line 'line' of file 'fname'*/
void LDM_showsource(char *fname, unsigned int line){
int lines = 0;
FILE *f = fopen(fname,"r");
char buf[8192];
while( fgets(buf, 8191, f) != NULL && ++lines < line);
ldmmsg(stderr,"%s",buf);
fclose(f);
}
/*
* Print all libraries that were LD_PRELOAD-ed
* before the program started
*/
void LDM_printlibs(char *c, void *d){
if( strstr(c,"libldm.so") ){
return;
}
ldmmsg(stderr,"%s ",c);
}
/*
* Print a stack trace -- this
* is intended to be used when
* the program is stopped, during debugging
*/
void print_trace(){
void * array[10];
int size;
char **strings;
int i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
for (i = 2; i < size; i++){
fprintf (stderr," %s\n", strings[i]);
/*TODO: get_info is currently broken, and I don't know why*/
/*
if( strstr(strings[i],"/libc.") ){
fprintf(stderr,"\n");
continue;
}
int len = strlen(strings[i]);
char *temp = (char *)malloc(len);
strncpy(temp,strings[i],len);
char *c;
if( (c = strrchr(temp,'(')) ){
while( *c ){
*c=0;
c++;
}
fprintf(stderr," %s",get_info(array[i],temp));
}else if( (c = strrchr(temp,' ')) ){
while( *c ){
*c=0;
c++;
}
fprintf(stderr," %s",get_info(array[i],temp));
}
fprintf(stderr,"\n");
free( temp );*/
}
free (strings);
}
/*
* Get the program's name, by looking at /proc/self/exe
*/
char *getProgramName(){
char *buf = (char*)malloc(1024);
memset(buf,0,1024);
readlink("/proc/self/exe",buf,1024);
return buf;
}
void print_help(){
fprintf(stderr,"\
help - this message\n \
quit - exit the program\n \
cont - continue executing. Leaves the LDM debugger.\n \
run - run the program.\n \
source - show <file> <line>. Shows the source code at <line> in <file>.\n \
inspect - shows the value of a memory location [currently unsafe!].\n \
dumpdwarf - shows all debug info in the exeuctable.\n \
ascope - ascope <instruction address>. Prints a chain of scopes leading down to the innermost one enclosing address <instruction address>.\n \
fscope - fscope <file> <line>. Prints a chain of scopes leading down to the innermost one enclosing the first instruction of the code on line <line> in <file>\n \
linetoaddr - linetoaddr <file> <line>. Prints the instruction address of the first instruction of he code on line <line> in <file>.\n \
bt/backtrace/where - Prints the call stack.\n \
\n");
}
/*
* The main debug command loop.
*/
void LDM_debug(ucontext_t *ctx){
int curDebugFrame = 0;
if(ctx == NULL){
getcontext(ctx);
}
print_trace();
if(ctx != (ucontext_t*)INIT_CTX){
fprintf(stderr,"Instruction: %p\n",ctx->uc_mcontext.gregs[REG_RIP]);
fprintf(stderr,"Frame Pointer: %p\n",ctx->uc_mcontext.gregs[REG_RBP]);
fprintf(stderr,"Stack Pointer: %p\n",ctx->uc_mcontext.gregs[REG_RSP]);
curDebugInstr = (void*)ctx->uc_mcontext.gregs[REG_RIP];
curDebugFramePtr = (void*)ctx->uc_mcontext.gregs[REG_RBP];
}else{
curDebugInstr = (void*)0x0;
curDebugFramePtr = (void*)0x0;
}
while(1){
char *line = readline("LDM>");
/*Decide what to do based on the command!*/
if(line && !strncmp(line,"",1)){
continue;
}
add_history(line);
if(line && strstr(line,"help")){
print_help();
}
if(line && strstr(line,"fptr")){
if(ctx && ctx != (ucontext_t*)INIT_CTX){
fprintf(stderr,"%lx\n",ctx->uc_mcontext.gregs[REG_RBP]);
}else{
fprintf(stderr,"No Frame Pointer Available\n");
}
continue;
}
if(line && strstr(line,"quit")){
exit(0);
}
if(line && strstr(line,"cont")){
if( LDM_runstate == true ){
ldmmsg(stderr,"Continuing.\n");
return;
}else{
ldmmsg(stderr,"Can't continue - Not Running!\n");
}
}
if(line && strstr(line,"run")){
if( LDM_runstate == false ){
return;
}else{
ldmmsg(stderr,"Can't run - Already running!\n");
}
}
if(line && strstr(line,"source")){
char fname[2048];
char show[5];
int lineno;
sscanf(line,"%s %s %d\n",show,fname,&lineno);
LDM_showsource(fname,lineno);
continue;
}
if(line && strstr(line,"deref")){
unsigned long ad;
char insp[8];
int num = sscanf(line,"%s %lx\n",insp,&ad);
fprintf(stderr,"%lx\n",*((unsigned long *)ad));
continue;
}
if(line && strstr(line,"showvars")){
LDM_vars_a(curDebugInstr);
continue;
}
if(line && strstr(line,"up")){
if(ctx && ctx != (ucontext_t*)INIT_CTX){
int i;
/*TODO: Bounds check this somehow*/
curDebugFrame++;
curDebugFramePtr = (void*)ctx->uc_mcontext.gregs[REG_RBP];
for(i = 0; i < curDebugFrame; i++){
fprintf(stderr,"Next Frame Pointer: %p.\n",curDebugFramePtr);
curDebugFramePtr = ((void**)curDebugFramePtr)[0];
}
curDebugInstr = ((void**)curDebugFramePtr)[1];
fprintf(stderr,"Frame Pointer: %p. Instruction: %p\n",curDebugFramePtr,curDebugInstr);
}else{
fprintf(stderr,"No Frame Pointer Available\n");
}
continue;
}
if(line && strstr(line,"down")){
if(ctx && ctx != (ucontext_t*)INIT_CTX){
int i;
if(curDebugFrame > 0){
curDebugFrame--;
}
curDebugFramePtr = (void*)ctx->uc_mcontext.gregs[REG_RBP];
for(i = 0; i < curDebugFrame; i++){
curDebugFramePtr = ((void**)curDebugFramePtr)[0];
}
if( curDebugFrame > 0 ){
curDebugInstr = ((void**)curDebugFramePtr)[1];
}else{
curDebugInstr = (void*)ctx->uc_mcontext.gregs[REG_RIP];
}
fprintf(stderr,"Frame Pointer: %p. Instruction: %p\n",curDebugFramePtr,curDebugInstr);
}else{
fprintf(stderr,"No Frame Pointer Available\n");
}
continue;
}
if(line && strstr(line,"string")){
unsigned long ad;
char insp[8];
sscanf(line,"%s %lx\n",insp,&ad);
int len = 0;
char *c = (char *)ad;
while(*c++ != 0){len++; }
char str[len+1];
strncpy(str,c,len);
fprintf(stderr,"%s\n",str);
continue;
}
if(line && strstr(line,"inspect")){
unsigned long ad;
char varname[512];
char insp[8];
int num = sscanf(line,"%s %s %lx\n",insp,varname,&ad);
if(num == 2){
ad = (unsigned long)curDebugInstr;
}
LDM_inspect((void*)ad,(const char *)varname);
continue;
}
if(line && strstr(line,"dumpdwarf")){
dump_dwarf_info();
continue;
}
if(line && strstr(line,"ascope")){
char sc_str[6];
void *ad;
int num = sscanf(line,"%s 0x%p\n",sc_str,&ad);
if( num == 1 ){
LDM_scope_a(curDebugInstr);
}
LDM_scope_a(ad);
continue;
}
if(line && strstr(line,"fscope")){
char sc_str[6];
char fn[1024];
unsigned ln;
sscanf(line,"%s %s %u\n",sc_str,fn,&ln);
fprintf(stderr,"%s %s %u\n",sc_str,fn,ln);
LDM_scope_f(fn,ln);
continue;
}
if(line && strstr(line,"linetoaddr")){
char sc_str[6];
char fn[1024];
unsigned ln;
sscanf(line,"%s %s %u\n",sc_str,fn,&ln);
fprintf(stderr,"%s %s %u\n",sc_str,fn,ln);
LDM_line_to_addr(fn,ln);
continue;
}
if(line && strstr(line,"bt") || strstr(line,"backtrace") || strstr(line,"where")){
print_trace();
continue;
}
ldmmsg(stderr,"Unsupported Command %s!\n", line);
}
exit(1);
}
/*
* This function is called when any catchable signal is caught.
* The first thing it does is go into the debugger loop.
* When the user exits the debugger loop, the program continues
* by executing the program's original signal handler, or the
* default handler, if the program has registered no handler.
*/
void terminationHandler(int signum, siginfo_t *sinf, void *uctx){
ucontext_t *ctx = (ucontext_t *)uctx;
ldmmsg(stderr,"Process %d got signal %d. Entering LDM Debugger\n",getpid(),signum);
LDM_debug(ctx);
if( signum == SIGSEGV){
if( sigSEGVSaver.sa_handler != SIG_DFL &&
sigSEGVSaver.sa_handler != SIG_IGN){
(*sigSEGVSaver.sa_handler)(signum);
}
}else if( signum == SIGTERM){
if( sigTERMSaver.sa_handler != SIG_DFL &&
sigTERMSaver.sa_handler != SIG_IGN){
(*sigTERMSaver.sa_handler)(signum);
}
}else if( signum == SIGABRT){
if( sigABRTSaver.sa_handler != SIG_DFL &&
sigABRTSaver.sa_handler != SIG_IGN){
(*sigABRTSaver.sa_handler)(signum);
}
}else if ( signum == SIGINT ){
if( sigINTSaver.sa_handler != SIG_DFL &&
sigINTSaver.sa_handler != SIG_IGN){
(*sigINTSaver.sa_handler)(signum);
}
}
signal(signum, SIG_DFL);
kill(getpid(), signum);
}
/*
* Set up the signal handling support
*/
void setupSignals(){
memset(&sigABRTSaver, 0, sizeof(struct sigaction));
memset(&sigSEGVSaver, 0, sizeof(struct sigaction));
//memset(&sigTERMSaver, 0, sizeof(struct sigaction));
//memset(&sigINTSaver, 0, sizeof(struct sigaction));
/*Register Signal handler for SEGV and TERM*/
struct sigaction segv_sa;
segv_sa.sa_sigaction = terminationHandler;
sigemptyset(&segv_sa.sa_mask);
segv_sa.sa_flags = SA_ONSTACK | SA_SIGINFO;
sigaction(SIGSEGV,&segv_sa,&sigSEGVSaver);
//sigaction(SIGTERM,&segv_sa,&sigTERMSaver);
sigaction(SIGABRT,&segv_sa,&sigABRTSaver);
//sigaction(SIGINT,&segv_sa,&sigINTSaver);
}
/*
* The initializer implementation.
* 1)Print welcome
* 2)Show preloaded libraries
* 3)Setup signal handling
* 4)Register replacement pthread_create
* 5)Get the program's name
* 6)Enter the debugger loop
*/
void LDM_init(){
ldmmsg(stderr,"Legerdemain - Brandon Lucia - 2011\n");
ldmmsg(stderr,"Loading function wrappers via LD_PRELOAD:\n");
char *preloaded = getenv("LD_PRELOAD");
applyToTokens(preloaded,": ,",LDM_printlibs,NULL);
ldmmsg(stderr,"\n");
setupSignals();
char *dbg = getenv("LDM_DEBUG");
LDM_ProgramName = getProgramName();
opendwarf(LDM_ProgramName);
if( dbg != NULL ){
LDM_debug((ucontext_t*)INIT_CTX);
}
}
/*
* No tasks need to be performed on shutdown currently.
*/
void LDM_deinit(){
}