-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebugger.c
More file actions
586 lines (509 loc) · 19.1 KB
/
Copy pathdebugger.c
File metadata and controls
586 lines (509 loc) · 19.1 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
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
583
584
585
586
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include "headers/njvm.h"
#include "headers/stack.h"
#include "headers/instructions.h"
#include "headers/sda.h"
#include "headers/debugger.h"
#include "headers/heap.h"
#include "../lib/support.h"
#include "../lib/bigint.h"
unsigned int run;
unsigned int breakpoint;
unsigned int quit;
unsigned int verbose;
/**
* Dumps program memory, stack content and static data area
* content into the file specified by path.
* If the file does not exist, it will be created. If it does exist,
* its content will be DISCARDED!
*
* See man page for fopen with mode 'w+'
* $ man 3 fopen
*
* @param path - the path of the file to dump memory to
*/
void memoryDump(char* path) {
FILE* out;
unsigned int i;
printf("%s Performing memory dump...\n", DEBUG_DUMP);
if (path == NULL) {
changeTextColor(RED, TRANSPARENT, BRIGHT);
printf("%s ERROR: PATH IS NULL!\n", DEBUG_DUMP);
changeTextColor(WHITE, TRANSPARENT, RESET);
exit(E_ERR_IO_SHELL);
}
out = fopen(path, "w+");
if (out == NULL) {
changeTextColor(RED, TRANSPARENT, BRIGHT);
printf("%sERROR: Could not open %s: %s\n", DEBUG_DUMP, path, strerror(errno));
changeTextColor(WHITE, TRANSPARENT, RESET);
exit(E_ERR_IO_FILE);
}
fprintf(out, "NinjaVM Memory Dump\n");
fprintf(out, "VM Version: %u\n", VERSION);
fprintf(out, "The next instruction for execution:\n");
fprintf(out, "[%08d]: %s %d\n\n", pc-1, opcodes[programMemory[pc-1] >> 24], programMemory[pc-1] & 0x00FFFFFF);
fprintf(out, "The stack at dump time:\n\n");
printStackTo(out);
fprintf(out, "\n");
fprintf(out, "The static data area at dump time:\n");
printStaticDataAreaTo(out);
fprintf(out, "\n");
fprintf(out, "The current value of the return value register is: ");
if (returnValueRegister == NULL) { /* NULL-Pointer */
fprintf(out, "(NIL)\n");
}
else if (IS_PRIM(returnValueRegister)) { /* Primitive-Object (BigInt) */
fprintf(out, "\n\tAddress : %p\n\n", (void*)returnValueRegister);
fprintf(out, "\tType : Primitive\n");
}
else { /* Complex Object */
fprintf(out, "\n\tAddress : %p\n", (void*)returnValueRegister);
fprintf(out, "\tType : Complex\n");
fprintf(out, "\tReferencing: %d\n", GET_SIZE(returnValueRegister));
}
fprintf(out, "Content of program memory:\n\n");
for (i = 0; i < instructionCount; i++) {
fprintf(out, "[%08d]: %6s %d\n", i, opcodes[programMemory[i] >> 24], SIGN_EXTEND(IMMEDIATE(programMemory[i])));
}
fprintf(out, "** End of program memory **\n");
fprintf(out, "\n");
fprintf(out, "Content of heap:\n\n");
printHeap(out);
fprintf(out, "\n");
fprintf(out, "----- End of dump -----\n");
fclose(out);
}
/**
* Reads the next integer from stdin and truncates the rest.
*
* @return the first number found in stdin as signed int
*/
int getNumber(void) {
int number;
int cleanUp;
if (scanf("%d", &number) != 1) {
changeTextColor(RED, TRANSPARENT, BRIGHT);
printf("%s ", ERROR);
if (number == EOF) {
printf("Input stream closed!\n");
printf("%s\n", strerror(errno));
}
else {
printf("Could not read a number from input!\n");
}
changeTextColor(WHITE, TRANSPARENT, RESET);
exit(E_ERR_IO_SHELL);
}
while ((cleanUp = getchar()) != '\n' && cleanUp != EOF) { }
return number;
}
/**
* Reads one 'line' from stdin.
* NOTE: Only the first 32 characters are read, the rest is truncated.
*
* @return User input from stdin with a max length of 32 characters.
*/
char* getInput(void) {
int cleanUp;
char* input;
char* newLine;
cleanUp = 0;
newLine = NULL;
input = calloc(32, sizeof(unsigned char));
if (input == NULL) {
changeTextColor(RED, TRANSPARENT, BRIGHT);
printf("%s ", ERROR);
printf("System could not allocate 32 bytes of memory for debugger input.\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
exit(E_ERR_IO_SHELL);
}
if (fgets(input, 32, stdin) == NULL) {
changeTextColor(RED, TRANSPARENT, BRIGHT);
printf("Could not take user input!\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
exit(E_ERR_IO_SHELL);
}
if (strcmp("\n", input) == 0) {
return getInput();
}
newLine = strchr(input, 10);
if (newLine != NULL) {
*newLine = 0;
}
else {
/* flush stdin */
printf("%s ", DEBUGGER);
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("*WARNING* ");
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("Truncated input after 32 characters!\n");
while ((cleanUp = getchar()) != '\n' && cleanUp != EOF) { }
}
return input;
}
/**
* Prints the content of program memory to the console
*/
void listProgramMemory(void) {
int programCounter;
printf("%s Listing program memory:\n\n", DEBUG_INSPECT);
programCounter = 0;
while(programCounter < instructionCount) {
int instruction;
unsigned int opcode;
int operand;
instruction = programMemory[programCounter];
programCounter++;
opcode = instruction >> 24;
operand = SIGN_EXTEND(IMMEDIATE(instruction));
printf("[%08d]: ", programCounter-1);
changeTextColor(GREEN, TRANSPARENT, RESET);
printf("%6s ", opcodes[opcode]);
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%d\n", operand);
}
printf("%s End of program memory!\n\n", DEBUG_INSPECT);
}
/**
* Interprets and executes commands for the debugger.
*
* @param command - the command as a string
* @return 0 if the command advances the program counter, 1 if it doesn't
*/
int processCommand(char* command) {
if (strcmp("breakpoint", command) == 0){
int newBreakpoint;
printf("%s Enter the number of the instruction,\n", DEBUG_BREAKPOINT);
printf("%s 0 to clear breakpint or -1 to abort!\n", DEBUG_BREAKPOINT);
printf("%s Set breakpoint to: ", DEBUG_BREAKPOINT);
changeTextColor(MAGENTA, TRANSPARENT, RESET);
newBreakpoint = getNumber();
if (newBreakpoint < -1) {
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%s ", DEBUG_BREAKPOINT);
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("Malformed input! Aborting...\n");
}
else if (newBreakpoint == -1) {
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%s ", DEBUG_BREAKPOINT);
changeTextColor(MAGENTA, TRANSPARENT, RESET);
printf("Did not change breakpoint!\n");
}
else if (newBreakpoint == 0) {
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%s ", DEBUG_BREAKPOINT);
changeTextColor(MAGENTA, TRANSPARENT, RESET);
printf("Clearing breakpoint...\n");
breakpoint = 0;
}
else {
changeTextColor(WHITE, TRANSPARENT, RESET);
printf(
"%s Setting breakpoint on instruction ",
DEBUG_BREAKPOINT
);
changeTextColor(MAGENTA, TRANSPARENT, RESET);
printf("%d", newBreakpoint);
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("!\n");
breakpoint = newBreakpoint;
}
changeTextColor(WHITE, TRANSPARENT, RESET);
return FALSE;
}
else if (strcmp("dump", command) == 0) {
char* path;
int i;
printf("%s Please specify a file path to dump memory to.\n", DEBUG_DUMP);
printf("%s ", DEBUG_DUMP);
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("*WARNING* IF THE FILE EXISTS IT WILL BE OVERWRITTEN!\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%s ", DEBUG_DUMP);
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("*WARNING* ");
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("Input will be truncated after 128 characters!\n");
printf("%s ", DEBUG_DUMP);
path = (char*) calloc(128, sizeof(char));
fgets(path, 128, stdin);
for (i = 0; i < 128; i++) {
if (path[i] == '\n') {
path[i] = 0;
break;
}
}
memoryDump(path);
free(path);
return FALSE;
}
else if (strcmp("execute", command) == 0) {
char* asmInstr;
unsigned int opcode;
int operand;
printf("%s Enter the the instruction you would\n", DEBUG_EXEC);
printf("%s like to execute or 'ABORT' to abort.\n", DEBUG_EXEC);
printf("%s ", DEBUG_EXEC);
changeTextColor(GREEN, TRANSPARENT, RESET);
asmInstr = getInput();
changeTextColor(WHITE, TRANSPARENT, RESET);
if (strcasecmp(asmInstr, "ABORT") == 0) {
printf("%s Aborting...\n", DEBUG_EXEC);
return FALSE;
}
opcode = 0;
while(opcode < 42) {
if (strcasecmp(asmInstr, opcodes[opcode]) == 0) {
break;
}
opcode = opcode + 1;
}
if (opcode == 42) {
printf("%s ", DEBUG_EXEC);
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("*WARNING* ");
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%s is an invalid opcode! Aborting...\n", asmInstr);
return FALSE;
}
printf("%s Now enter the immediate value for this instruction:\n", DEBUG_EXEC);
printf("%s (In decimal, as a 24 bit signed integer)\n", DEBUG_EXEC);
printf("%s ", DEBUG_EXEC);
operand = getNumber();
printf("%s Executing instruction...\n", DEBUG_EXEC);
execute(opcode, operand);
printf("%s Done!\n", DEBUG_EXEC);
free(asmInstr);
return FALSE;
}
else if (strcmp("gc", command) == 0) {
changeTextColor(WHITE, TRANSPARENT, BRIGHT);
printf("[Debugger/GC]: Starting garbage collector...\n");
gc();
printf("[Debugger/GC]: Garbage collector finished!\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
gcRunning = FALSE;
return FALSE;
}
else if (strcmp("help", command) == 0) {
printf("\n");
changeTextColor(YELLOW, TRANSPARENT, RESET);
printf("********************* NinjaVM Debugger *********************\n\n");
printf("Available commands:\n");
printf(" help\t\t- Prints this info.\n");
printf(" breakpoint\t- Set a breakpoint in the program at a\n");
printf(" \t\t specified instruction.\n");
printf(" dump\t\t- Dumps memory to the specified file.\n");
printf(" exectue\t- Executes an instruction\n");
printf(" gc\t\t- Initiates the Garbage Collector.\n");
printf(" inspect\t- Print the content of one of the VMs many\n");
printf(" \t\t data-containing structures like the stack.\n");
printf(" quit\t\t- Stops the VM at the next instruction cycle.\n");
printf(" reset\t\t- Resets the VM into its pre program launch state.\n");
printf(" run\t\t- Starts continuous execution of instrcutions\n");
printf(" \t\t until a HALT is executed or the breakpoint\n");
printf(" \t\t is reached.\n");
printf(" skip\t\t- Skips the current instruction.\n");
printf(" step\t\t- Executes the current instrcution and\n");
printf(" \t\t advances the program counter by one.\n");
printf(" verbose\t- Toggles output mode for run command.\n");
printf("************************************************************\n\n");
printf("%s Consult the debugger documentation for further information.\n", DEBUGGER);
changeTextColor(WHITE, TRANSPARENT, RESET);
return FALSE;
}
else if (strcmp("inspect", command) == 0) {
unsigned int inspectNumber;
printf("%s What would you like to inspect:\n", DEBUG_INSPECT);
printf("%s 1: Stack\n", DEBUG_INSPECT);
printf("%s 2: Static data area\n", DEBUG_INSPECT);
printf("%s 3: Return value register\n", DEBUG_INSPECT);
printf("%s 4: Program Memory\n", DEBUG_INSPECT);
printf("%s 5: Object-Reference\n", DEBUG_INSPECT);
printf("%s Choose one of the aforementioned or 0 to abort!\n", DEBUG_INSPECT);
printf("%s ", DEBUG_INSPECT);
inspectNumber = getNumber();
switch(inspectNumber) {
case 0: { /*Abort*/
break;
}
case 1: { /*Stack*/
printf("%s Listing contents of stack:\n\n", DEBUG_INSPECT);
changeTextColor(CYAN, TRANSPARENT, BRIGHT);
printStack();
printf("\n");
break;
}
case 2: { /*SDA*/
printf("%s Listing contents of static data area:\n\n", DEBUG_INSPECT);
changeTextColor(CYAN, TRANSPARENT, BRIGHT);
printStaticDataArea();
printf("\n");
break;
}
case 3: { /*RET*/
printf("%s ", DEBUG_INSPECT);
changeTextColor(CYAN, TRANSPARENT, BRIGHT);
printf("The current value of the return value register is: ");
if (returnValueRegister == NULL) { /* NULL-Pointer */
printf("(NIL)\n");
}
else if (IS_PRIM(returnValueRegister)) { /* Primitive-Object (BigInt) */
printf("\n\tAddress : %p\n", (void*)returnValueRegister);
printf("\tType : Primitive\n");
printf("\tSize : %u Bytes\n", returnValueRegister->size);
}
else { /* Complex Object */
printf("\n\tAddress : %p\n", (void*)returnValueRegister);
printf("\tType : Complex\n");
printf("\tReferencing: %d\n", GET_SIZE(returnValueRegister));
}
changeTextColor(WHITE, TRANSPARENT, RESET);
break;
}
case 4: { /*PROG MEM*/
listProgramMemory();
break;
}
case 5: { /*ObjRef*/
printf("%s Enter the address of the object reference:\n", DEBUG_INSPECT);
printf("%s ", DEBUG_INSPECT);
changeTextColor(YELLOW, TRANSPARENT, RESET);
printf("*WARNING* ENTERING INVALID ADDRESS CAN CAUSE UNDEFINED BEHAVIOUR!\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%s 0x", DEBUG_INSPECT);
changeTextColor(CYAN, TRANSPARENT, BRIGHT);
inspectObject((ObjRef) strtol(getInput(), NULL, 16));
break;
}
default: {
printf("%s ", DEBUG_INSPECT);
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("There is no option with number '%u'...\n", inspectNumber);
}
}
changeTextColor(WHITE, TRANSPARENT, RESET);
return FALSE;
}
else if(strcmp("quit", command) == 0) {
printf("[Debugger/Quit]: ");
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("Halting NinjaVM...\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
quit = TRUE;
return FALSE;
}
else if (strcmp("reset", command) == 0) {
printf("[Debugger/Reset]: ");
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("RESETTING NINJA VIRTUAL MACHINE....\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
pc = 1;
purgeStack();
purgeSda();
purgeHeap();
returnValueRegister = NULL;
run = FALSE;
breakpoint = -1;
verbose = FALSE;
printf("\nNinja Virtual Machine started\n");
return FALSE;
}
else if (strcmp("run", command) == 0) {
run = TRUE;
return FALSE;
}
else if (strcmp("skip", command) == 0) {
pc = pc + 1;
return FALSE;
}
else if (strcmp("step", command) == 0) {
return TRUE;
}
else if (strcmp("verbose", command) == 0) {
verbose = verbose == TRUE ? FALSE : TRUE;
printf("%s ", DEBUGGER);
changeTextColor(GREEN, TRANSPARENT, BRIGHT);
printf("Toggled verbose run output: %s\n", verbose == TRUE ? "TRUE" : "FALSE");
changeTextColor(WHITE, TRANSPARENT, RESET);
return FALSE;
}
else {
printf("%s ", DEBUGGER);
changeTextColor(YELLOW, TRANSPARENT, BRIGHT);
printf("Unrecognized command: '%s'\n", command);
changeTextColor(WHITE, TRANSPARENT, RESET);
return FALSE;
}
return TRUE;
}
/**
* Main entry point for debugger.
*
* @param code - A FILE pointer pointing to an opened file for reading
*/
void debug(void) {
verbose = FALSE;
breakpoint = -1;
quit = FALSE;
run = FALSE;
changeTextColor(WHITE, TRANSPARENT, BRIGHT);
printf("%s Initialization routine completed! Launching program...\n\n", DEBUGGER);
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("Ninja Virtual Machine started\n");
while(quit != TRUE) {
int instruction;
int operand;
unsigned int doExecute;
unsigned int opcode;
char* inputCommand;
if (pc == breakpoint && breakpoint != 0) {
run = FALSE;
printf("%s ", DEBUG_BREAKPOINT);
changeTextColor(MAGENTA, TRANSPARENT, RESET);
printf("Reached breakpoint!\n");
changeTextColor(WHITE, TRANSPARENT, RESET);
}
doExecute = FALSE;
instruction = programMemory[pc];
pc = pc + 1;
opcode = instruction >> 24;
operand = SIGN_EXTEND(IMMEDIATE(instruction));
if (run == FALSE) {
printf("[%08d]: ", pc-1);
changeTextColor(GREEN, TRANSPARENT, RESET);
printf("%6s ", opcodes[opcode]);
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%d\n", operand);
printf("%s Enter a command or 'help' for a list of available commands.\n", DEBUGGER);
printf("%s ", DEBUGGER);
inputCommand = getInput();
doExecute = processCommand(inputCommand);
free(inputCommand);
}
else doExecute = TRUE;
if (doExecute) {
if (run == TRUE && verbose == TRUE) {
printf("[%08d]: ", pc-1);
changeTextColor(GREEN, TRANSPARENT, RESET);
printf("%6s ", opcodes[opcode]);
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("%d\n", operand);
}
execute(opcode, operand);
}
else
pc = pc - 1;
/* Check if the halt instruction has been executed or not */
if (halt == TRUE) quit = TRUE;
}
changeTextColor(WHITE, TRANSPARENT, RESET);
printf("Ninja Virtual Machine stopped\n");
exit(E_EXECOK);
}