src/debugger/cli-debugger.c (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "cli-debugger.h"
7
8#ifdef USE_CLI_DEBUGGER
9
10#include "core/core.h"
11#include "core/version.h"
12#include "parser.h"
13
14#include <signal.h>
15
16#ifdef USE_PTHREADS
17#include <pthread.h>
18#endif
19
20const char* ERROR_MISSING_ARGS = "Arguments missing"; // TODO: share
21const char* ERROR_OVERFLOW = "Arguments overflow";
22
23static struct CLIDebugger* _activeDebugger;
24
25#ifndef NDEBUG
26static void _breakInto(struct CLIDebugger*, struct CLIDebugVector*);
27#endif
28static void _continue(struct CLIDebugger*, struct CLIDebugVector*);
29static void _disassemble(struct CLIDebugger*, struct CLIDebugVector*);
30static void _next(struct CLIDebugger*, struct CLIDebugVector*);
31static void _print(struct CLIDebugger*, struct CLIDebugVector*);
32static void _printBin(struct CLIDebugger*, struct CLIDebugVector*);
33static void _printHex(struct CLIDebugger*, struct CLIDebugVector*);
34static void _printStatus(struct CLIDebugger*, struct CLIDebugVector*);
35static void _printHelp(struct CLIDebugger*, struct CLIDebugVector*);
36static void _quit(struct CLIDebugger*, struct CLIDebugVector*);
37static void _readByte(struct CLIDebugger*, struct CLIDebugVector*);
38static void _reset(struct CLIDebugger*, struct CLIDebugVector*);
39static void _readHalfword(struct CLIDebugger*, struct CLIDebugVector*);
40static void _readWord(struct CLIDebugger*, struct CLIDebugVector*);
41static void _setBreakpoint(struct CLIDebugger*, struct CLIDebugVector*);
42static void _clearBreakpoint(struct CLIDebugger*, struct CLIDebugVector*);
43static void _setWatchpoint(struct CLIDebugger*, struct CLIDebugVector*);
44static void _writeByte(struct CLIDebugger*, struct CLIDebugVector*);
45static void _writeHalfword(struct CLIDebugger*, struct CLIDebugVector*);
46static void _writeWord(struct CLIDebugger*, struct CLIDebugVector*);
47static void _dumpByte(struct CLIDebugger*, struct CLIDebugVector*);
48static void _dumpHalfword(struct CLIDebugger*, struct CLIDebugVector*);
49static void _dumpWord(struct CLIDebugger*, struct CLIDebugVector*);
50
51static void _breakIntoDefault(int signal);
52
53static struct CLIDebuggerCommandSummary _debuggerCommands[] = {
54 { "b", _setBreakpoint, CLIDVParse, "Set a breakpoint" },
55 { "break", _setBreakpoint, CLIDVParse, "Set a breakpoint" },
56 { "c", _continue, 0, "Continue execution" },
57 { "continue", _continue, 0, "Continue execution" },
58 { "d", _clearBreakpoint, CLIDVParse, "Delete a breakpoint" },
59 { "delete", _clearBreakpoint, CLIDVParse, "Delete a breakpoint" },
60 { "dis", _disassemble, CLIDVParse, "Disassemble instructions" },
61 { "disasm", _disassemble, CLIDVParse, "Disassemble instructions" },
62 { "disassemble", _disassemble, CLIDVParse, "Disassemble instructions" },
63 { "h", _printHelp, CLIDVStringParse, "Print help" },
64 { "help", _printHelp, CLIDVStringParse, "Print help" },
65 { "i", _printStatus, 0, "Print the current status" },
66 { "info", _printStatus, 0, "Print the current status" },
67 { "n", _next, 0, "Execute next instruction" },
68 { "next", _next, 0, "Execute next instruction" },
69 { "p", _print, CLIDVParse, "Print a value" },
70 { "p/t", _printBin, CLIDVParse, "Print a value as binary" },
71 { "p/x", _printHex, CLIDVParse, "Print a value as hexadecimal" },
72 { "print", _print, CLIDVParse, "Print a value" },
73 { "print/t", _printBin, CLIDVParse, "Print a value as binary" },
74 { "print/x", _printHex, CLIDVParse, "Print a value as hexadecimal" },
75 { "q", _quit, 0, "Quit the emulator" },
76 { "quit", _quit, 0, "Quit the emulator" },
77 { "reset", _reset, 0, "Reset the emulation" },
78 { "r/1", _readByte, CLIDVParse, "Read a byte from a specified offset" },
79 { "r/2", _readHalfword, CLIDVParse, "Read a halfword from a specified offset" },
80 { "r/4", _readWord, CLIDVParse, "Read a word from a specified offset" },
81 { "status", _printStatus, 0, "Print the current status" },
82 { "w", _setWatchpoint, CLIDVParse, "Set a watchpoint" },
83 { "w/1", _writeByte, CLIDVParse, "Write a byte at a specified offset" },
84 { "w/2", _writeHalfword, CLIDVParse, "Write a halfword at a specified offset" },
85 { "w/4", _writeWord, CLIDVParse, "Write a word at a specified offset" },
86 { "watch", _setWatchpoint, CLIDVParse, "Set a watchpoint" },
87 { "x/1", _dumpByte, CLIDVParse, "Examine bytes at a specified offset" },
88 { "x/2", _dumpHalfword, CLIDVParse, "Examine halfwords at a specified offset" },
89 { "x/4", _dumpWord, CLIDVParse, "Examine words at a specified offset" },
90#ifndef NDEBUG
91 { "!", _breakInto, 0, "Break into attached debugger (for developers)" },
92#endif
93 { 0, 0, 0, 0 }
94};
95
96#ifndef NDEBUG
97static void _handleDeath(int sig) {
98 UNUSED(sig);
99 printf("No debugger attached!\n");
100}
101
102static void _breakInto(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
103 UNUSED(debugger);
104 UNUSED(dv);
105 struct sigaction sa, osa;
106 sa.sa_handler = _handleDeath;
107 sigemptyset(&sa.sa_mask);
108 sigaddset(&sa.sa_mask, SIGTRAP);
109 sa.sa_flags = SA_RESTART;
110 sigaction(SIGTRAP, &sa, &osa);
111#ifdef USE_PTHREADS
112 pthread_kill(pthread_self(), SIGTRAP);
113#else
114 kill(getpid(), SIGTRAP);
115#endif
116 sigaction(SIGTRAP, &osa, 0);
117}
118#endif
119
120static void _continue(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
121 UNUSED(dv);
122 debugger->d.state = DEBUGGER_RUNNING;
123}
124
125static void _next(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
126 UNUSED(dv);
127 debugger->d.core->step(debugger->d.core);
128 _printStatus(debugger, 0);
129}
130
131static void _disassemble(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
132 debugger->system->disassemble(debugger->system, dv);
133}
134
135static void _print(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
136 UNUSED(debugger);
137 for (; dv; dv = dv->next) {
138 printf(" %u", dv->intValue);
139 }
140 printf("\n");
141}
142
143static void _printBin(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
144 UNUSED(debugger);
145 for (; dv; dv = dv->next) {
146 printf(" 0b");
147 int i = 32;
148 while (i--) {
149 printf("%u", (dv->intValue >> i) & 1);
150 }
151 }
152 printf("\n");
153}
154
155static void _printHex(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
156 UNUSED(debugger);
157 for (; dv; dv = dv->next) {
158 printf(" 0x%08X", dv->intValue);
159 }
160 printf("\n");
161}
162
163static void _printHelp(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
164 UNUSED(debugger);
165 UNUSED(dv);
166 if (!dv) {
167 puts("Generic commands:");
168 int i;
169 for (i = 0; _debuggerCommands[i].name; ++i) {
170 printf("%-10s %s\n", _debuggerCommands[i].name, _debuggerCommands[i].summary);
171 }
172 if (debugger->system) {
173 printf("%s commands:\n", debugger->system->platformName);
174 for (i = 0; debugger->system->platformCommands[i].name; ++i) {
175 printf("%-10s %s\n", debugger->system->platformCommands[i].name, debugger->system->platformCommands[i].summary);
176 }
177 printf("%s commands:\n", debugger->system->name);
178 for (i = 0; debugger->system->commands[i].name; ++i) {
179 printf("%-10s %s\n", debugger->system->commands[i].name, debugger->system->commands[i].summary);
180 }
181 }
182 } else {
183 int i;
184 for (i = 0; _debuggerCommands[i].name; ++i) {
185 if (strcmp(_debuggerCommands[i].name, dv->charValue) == 0) {
186 printf(" %s\n", _debuggerCommands[i].summary);
187 }
188 }
189 if (debugger->system) {
190 for (i = 0; debugger->system->platformCommands[i].name; ++i) {
191 if (strcmp(debugger->system->platformCommands[i].name, dv->charValue) == 0) {
192 printf(" %s\n", debugger->system->platformCommands[i].summary);
193 }
194 }
195 for (i = 0; debugger->system->commands[i].name; ++i) {
196 if (strcmp(debugger->system->commands[i].name, dv->charValue) == 0) {
197 printf(" %s\n", debugger->system->commands[i].summary);
198 }
199 }
200 }
201 }
202}
203
204static void _quit(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
205 UNUSED(dv);
206 debugger->d.state = DEBUGGER_SHUTDOWN;
207}
208
209static void _readByte(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
210 if (!dv || dv->type != CLIDV_INT_TYPE) {
211 printf("%s\n", ERROR_MISSING_ARGS);
212 return;
213 }
214 uint32_t address = dv->intValue;
215 uint8_t value = debugger->d.core->busRead8(debugger->d.core, address);
216 printf(" 0x%02X\n", value);
217}
218
219static void _reset(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
220 UNUSED(dv);
221 debugger->d.core->reset(debugger->d.core);
222 _printStatus(debugger, 0);
223}
224
225static void _readHalfword(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
226 if (!dv || dv->type != CLIDV_INT_TYPE) {
227 printf("%s\n", ERROR_MISSING_ARGS);
228 return;
229 }
230 uint32_t address = dv->intValue;
231 uint16_t value = debugger->d.core->busRead16(debugger->d.core, address & ~1);
232 printf(" 0x%04X\n", value);
233}
234
235static void _readWord(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
236 if (!dv || dv->type != CLIDV_INT_TYPE) {
237 printf("%s\n", ERROR_MISSING_ARGS);
238 return;
239 }
240 uint32_t address = dv->intValue;
241 uint32_t value = debugger->d.core->busRead32(debugger->d.core, address & ~3);
242 printf(" 0x%08X\n", value);
243}
244
245static void _writeByte(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
246 if (!dv || dv->type != CLIDV_INT_TYPE) {
247 printf("%s\n", ERROR_MISSING_ARGS);
248 return;
249 }
250 if (!dv->next || dv->next->type != CLIDV_INT_TYPE) {
251 printf("%s\n", ERROR_MISSING_ARGS);
252 return;
253 }
254 uint32_t address = dv->intValue;
255 uint32_t value = dv->next->intValue;
256 if (value > 0xFF) {
257 printf("%s\n", ERROR_OVERFLOW);
258 return;
259 }
260 debugger->d.core->busWrite8(debugger->d.core, address, value);
261}
262
263static void _writeHalfword(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
264 if (!dv || dv->type != CLIDV_INT_TYPE) {
265 printf("%s\n", ERROR_MISSING_ARGS);
266 return;
267 }
268 if (!dv->next || dv->next->type != CLIDV_INT_TYPE) {
269 printf("%s\n", ERROR_MISSING_ARGS);
270 return;
271 }
272 uint32_t address = dv->intValue;
273 uint32_t value = dv->next->intValue;
274 if (value > 0xFFFF) {
275 printf("%s\n", ERROR_OVERFLOW);
276 return;
277 }
278 debugger->d.core->busWrite16(debugger->d.core, address, value);
279}
280
281static void _writeWord(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
282 if (!dv || dv->type != CLIDV_INT_TYPE) {
283 printf("%s\n", ERROR_MISSING_ARGS);
284 return;
285 }
286 if (!dv->next || dv->next->type != CLIDV_INT_TYPE) {
287 printf("%s\n", ERROR_MISSING_ARGS);
288 return;
289 }
290 uint32_t address = dv->intValue;
291 uint32_t value = dv->next->intValue;
292 debugger->d.core->busWrite32(debugger->d.core, address, value);
293}
294
295static void _dumpByte(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
296 if (!dv || dv->type != CLIDV_INT_TYPE) {
297 printf("%s\n", ERROR_MISSING_ARGS);
298 return;
299 }
300 uint32_t address = dv->intValue;
301 uint32_t words = 16;
302 if (dv->next && dv->next->type == CLIDV_INT_TYPE) {
303 words = dv->next->intValue;
304 }
305 while (words) {
306 uint32_t line = 16;
307 if (line > words) {
308 line = words;
309 }
310 printf("0x%08X:", address);
311 for (; line > 0; --line, ++address, --words) {
312 uint32_t value = debugger->d.core->busRead8(debugger->d.core, address);
313 printf(" %02X", value);
314 }
315 printf("\n");
316 }
317}
318
319static void _dumpHalfword(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
320 if (!dv || dv->type != CLIDV_INT_TYPE) {
321 printf("%s\n", ERROR_MISSING_ARGS);
322 return;
323 }
324 uint32_t address = dv->intValue;
325 uint32_t words = 8;
326 if (dv->next && dv->next->type == CLIDV_INT_TYPE) {
327 words = dv->next->intValue;
328 }
329 while (words) {
330 uint32_t line = 8;
331 if (line > words) {
332 line = words;
333 }
334 printf("0x%08X:", address);
335 for (; line > 0; --line, address += 2, --words) {
336 uint32_t value = debugger->d.core->busRead16(debugger->d.core, address);
337 printf(" %04X", value);
338 }
339 printf("\n");
340 }
341}
342
343static void _dumpWord(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
344 if (!dv || dv->type != CLIDV_INT_TYPE) {
345 printf("%s\n", ERROR_MISSING_ARGS);
346 return;
347 }
348 uint32_t address = dv->intValue;
349 uint32_t words = 4;
350 if (dv->next && dv->next->type == CLIDV_INT_TYPE) {
351 words = dv->next->intValue;
352 }
353 while (words) {
354 uint32_t line = 4;
355 if (line > words) {
356 line = words;
357 }
358 printf("0x%08X:", address);
359 for (; line > 0; --line, address += 4, --words) {
360 uint32_t value = debugger->d.core->busRead32(debugger->d.core, address);
361 printf(" %08X", value);
362 }
363 printf("\n");
364 }
365}
366
367static void _setBreakpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
368 if (!dv || dv->type != CLIDV_INT_TYPE) {
369 printf("%s\n", ERROR_MISSING_ARGS);
370 return;
371 }
372 uint32_t address = dv->intValue;
373 debugger->d.platform->setBreakpoint(debugger->d.platform, address);
374}
375
376static void _setWatchpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
377 if (!dv || dv->type != CLIDV_INT_TYPE) {
378 printf("%s\n", ERROR_MISSING_ARGS);
379 return;
380 }
381 uint32_t address = dv->intValue;
382 debugger->d.platform->setWatchpoint(debugger->d.platform, address, WATCHPOINT_RW); // TODO: ro/wo
383}
384
385static void _clearBreakpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
386 if (!dv || dv->type != CLIDV_INT_TYPE) {
387 printf("%s\n", ERROR_MISSING_ARGS);
388 return;
389 }
390 uint32_t address = dv->intValue;
391 debugger->d.platform->clearBreakpoint(debugger->d.platform, address);
392 debugger->d.platform->clearWatchpoint(debugger->d.platform, address);
393}
394
395static void _breakIntoDefault(int signal) {
396 UNUSED(signal);
397 mDebuggerEnter(&_activeDebugger->d, DEBUGGER_ENTER_MANUAL, 0);
398}
399
400static void _printStatus(struct CLIDebugger* debugger, struct CLIDebugVector* dv) {
401 UNUSED(dv);
402 debugger->system->printStatus(debugger->system);
403}
404
405static uint32_t _performOperation(enum Operation operation, uint32_t current, uint32_t next, struct CLIDebugVector* dv) {
406 switch (operation) {
407 case OP_ASSIGN:
408 current = next;
409 break;
410 case OP_ADD:
411 current += next;
412 break;
413 case OP_SUBTRACT:
414 current -= next;
415 break;
416 case OP_MULTIPLY:
417 current *= next;
418 break;
419 case OP_DIVIDE:
420 if (next != 0) {
421 current /= next;
422 } else {
423 dv->type = CLIDV_ERROR_TYPE;
424 return 0;
425 }
426 break;
427 }
428 return current;
429}
430
431static uint32_t _lookupIdentifier(struct mDebugger* debugger, const char* name, struct CLIDebugVector* dv) {
432 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
433 if (cliDebugger->system) {
434 uint32_t value = cliDebugger->system->lookupPlatformIdentifier(cliDebugger->system, name, dv);
435 if (dv->type != CLIDV_ERROR_TYPE) {
436 return value;
437 }
438 dv->type = CLIDV_INT_TYPE;
439 value = cliDebugger->system->lookupIdentifier(cliDebugger->system, name, dv);
440 if (dv->type != CLIDV_ERROR_TYPE) {
441 return value;
442 }
443 }
444 dv->type = CLIDV_ERROR_TYPE;
445 return 0;
446}
447
448static uint32_t _evaluateParseTree(struct mDebugger* debugger, struct ParseTree* tree, struct CLIDebugVector* dv) {
449 switch (tree->token.type) {
450 case TOKEN_UINT_TYPE:
451 return tree->token.uintValue;
452 case TOKEN_OPERATOR_TYPE:
453 return _performOperation(tree->token.operatorValue, _evaluateParseTree(debugger, tree->lhs, dv), _evaluateParseTree(debugger, tree->rhs, dv), dv);
454 case TOKEN_IDENTIFIER_TYPE:
455 return _lookupIdentifier(debugger, tree->token.identifierValue, dv);
456 case TOKEN_ERROR_TYPE:
457 default:
458 dv->type = CLIDV_ERROR_TYPE;
459 }
460 return 0;
461}
462
463struct CLIDebugVector* CLIDVParse(struct CLIDebugger* debugger, const char* string, size_t length) {
464 if (!string || length < 1) {
465 return 0;
466 }
467
468 struct CLIDebugVector dvTemp = { .type = CLIDV_INT_TYPE };
469
470 struct LexVector lv = { .next = 0 };
471 size_t adjusted = lexExpression(&lv, string, length);
472 if (adjusted > length) {
473 dvTemp.type = CLIDV_ERROR_TYPE;
474 lexFree(lv.next);
475 }
476
477 struct ParseTree tree;
478 parseLexedExpression(&tree, &lv);
479 if (tree.token.type == TOKEN_ERROR_TYPE) {
480 dvTemp.type = CLIDV_ERROR_TYPE;
481 } else {
482 dvTemp.intValue = _evaluateParseTree(&debugger->d, &tree, &dvTemp);
483 }
484
485 parseFree(tree.lhs);
486 parseFree(tree.rhs);
487
488 length -= adjusted;
489 string += adjusted;
490
491 struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
492 if (dvTemp.type == CLIDV_ERROR_TYPE) {
493 dv->type = CLIDV_ERROR_TYPE;
494 dv->next = 0;
495 } else {
496 *dv = dvTemp;
497 if (string[0] == ' ') {
498 dv->next = CLIDVParse(debugger, string + 1, length - 1);
499 if (dv->next && dv->next->type == CLIDV_ERROR_TYPE) {
500 dv->type = CLIDV_ERROR_TYPE;
501 }
502 }
503 }
504 return dv;
505}
506
507struct CLIDebugVector* CLIDVStringParse(struct CLIDebugger* debugger, const char* string, size_t length) {
508 if (!string || length < 1) {
509 return 0;
510 }
511
512 struct CLIDebugVector dvTemp = { .type = CLIDV_CHAR_TYPE };
513
514 size_t adjusted;
515 const char* next = strchr(string, ' ');
516 if (next) {
517 adjusted = next - string;
518 } else {
519 adjusted = length;
520 }
521 dvTemp.charValue = malloc(adjusted);
522 strncpy(dvTemp.charValue, string, adjusted);
523
524 length -= adjusted;
525 string += adjusted;
526
527 struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
528 *dv = dvTemp;
529 if (string[0] == ' ') {
530 dv->next = CLIDVStringParse(debugger, string + 1, length - 1);
531 if (dv->next && dv->next->type == CLIDV_ERROR_TYPE) {
532 dv->type = CLIDV_ERROR_TYPE;
533 }
534 }
535 return dv;
536}
537
538static void _DVFree(struct CLIDebugVector* dv) {
539 struct CLIDebugVector* next;
540 while (dv) {
541 next = dv->next;
542 if (dv->type == CLIDV_CHAR_TYPE) {
543 free(dv->charValue);
544 }
545 free(dv);
546 dv = next;
547 }
548}
549
550static int _tryCommands(struct CLIDebugger* debugger, struct CLIDebuggerCommandSummary* commands, const char* command, size_t commandLen, const char* args, size_t argsLen) {
551 struct CLIDebugVector* dv = 0;
552 int i;
553 const char* name;
554 for (i = 0; (name = commands[i].name); ++i) {
555 if (strlen(name) != commandLen) {
556 continue;
557 }
558 if (strncasecmp(name, command, commandLen) == 0) {
559 if (commands[i].parser) {
560 if (args) {
561 dv = commands[i].parser(debugger, args, argsLen);
562 if (dv && dv->type == CLIDV_ERROR_TYPE) {
563 printf("Parse error\n");
564 _DVFree(dv);
565 return false;
566 }
567 }
568 } else if (args) {
569 printf("Wrong number of arguments\n");
570 return false;
571 }
572 commands[i].command(debugger, dv);
573 _DVFree(dv);
574 return true;
575 }
576 }
577 return -1;
578}
579
580static bool _parse(struct CLIDebugger* debugger, const char* line, size_t count) {
581 const char* firstSpace = strchr(line, ' ');
582 size_t cmdLength;
583 if (firstSpace) {
584 cmdLength = firstSpace - line;
585 } else {
586 cmdLength = count;
587 }
588
589 const char* args = 0;
590 if (firstSpace) {
591 args = firstSpace + 1;
592 }
593 int result = _tryCommands(debugger, _debuggerCommands, line, cmdLength, args, count - cmdLength - 1);
594 if (result < 0 && debugger->system) {
595 result = _tryCommands(debugger, debugger->system->commands, line, cmdLength, args, count - cmdLength - 1);
596 if (result < 0) {
597 result = _tryCommands(debugger, debugger->system->platformCommands, line, cmdLength, args, count - cmdLength - 1);
598 }
599 }
600 if (result < 0) {
601 printf("Command not found\n");
602 }
603 return false;
604}
605
606static char* _prompt(EditLine* el) {
607 UNUSED(el);
608 return "> ";
609}
610
611static void _commandLine(struct mDebugger* debugger) {
612 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
613 const char* line;
614 _printStatus(cliDebugger, 0);
615 int count = 0;
616 HistEvent ev;
617 while (debugger->state == DEBUGGER_PAUSED) {
618 line = el_gets(cliDebugger->elstate, &count);
619 if (!line) {
620 return;
621 }
622 if (line[0] == '\n') {
623 if (history(cliDebugger->histate, &ev, H_FIRST) >= 0) {
624 _parse(cliDebugger, ev.str, strlen(ev.str) - 1);
625 }
626 } else {
627 _parse(cliDebugger, line, count - 1);
628 history(cliDebugger->histate, &ev, H_ENTER, line);
629 }
630 }
631}
632
633static void _reportEntry(struct mDebugger* debugger, enum mDebuggerEntryReason reason, struct mDebuggerEntryInfo* info) {
634 UNUSED(debugger);
635 switch (reason) {
636 case DEBUGGER_ENTER_MANUAL:
637 case DEBUGGER_ENTER_ATTACHED:
638 break;
639 case DEBUGGER_ENTER_BREAKPOINT:
640 if (info) {
641 printf("Hit breakpoint at 0x%08X\n", info->address);
642 } else {
643 printf("Hit breakpoint\n");
644 }
645 break;
646 case DEBUGGER_ENTER_WATCHPOINT:
647 if (info) {
648 if (info->accessType & WATCHPOINT_WRITE) {
649 printf("Hit watchpoint at 0x%08X: (new value = 0x%08x, old value = 0x%08X)\n", info->address, info->newValue, info->oldValue);
650 } else {
651 printf("Hit watchpoint at 0x%08X: (value = 0x%08x)\n", info->address, info->oldValue);
652 }
653 } else {
654 printf("Hit watchpoint\n");
655 }
656 break;
657 case DEBUGGER_ENTER_ILLEGAL_OP:
658 if (info) {
659 printf("Hit illegal opcode at 0x%08X: 0x%08X\n", info->address, info->opcode);
660 } else {
661 printf("Hit illegal opcode\n");
662 }
663 break;
664 }
665}
666
667static unsigned char _tabComplete(EditLine* elstate, int ch) {
668 UNUSED(ch);
669 const LineInfo* li = el_line(elstate);
670 if (!li->buffer[0]) {
671 return CC_ERROR;
672 }
673
674 const char* commandPtr;
675 size_t cmd = 0, len = 0;
676 const char* name = 0;
677 for (commandPtr = li->buffer; commandPtr <= li->cursor; ++commandPtr, ++len) {
678 for (; (name = _debuggerCommands[cmd].name); ++cmd) {
679 int cmp = strncasecmp(name, li->buffer, len);
680 if (cmp > 0) {
681 return CC_ERROR;
682 }
683 if (cmp == 0) {
684 break;
685 }
686 }
687 }
688 if (!name) {
689 return CC_ERROR;
690 }
691 if (_debuggerCommands[cmd + 1].name && strlen(_debuggerCommands[cmd + 1].name) >= len - 1 && name[len - 2] == _debuggerCommands[cmd + 1].name[len - 2]) {
692 --len;
693 const char* next = 0;
694 int i;
695 for (i = cmd + 1; _debuggerCommands[i].name; ++i) {
696 if (strncasecmp(name, _debuggerCommands[i].name, len)) {
697 break;
698 }
699 next = _debuggerCommands[i].name;
700 }
701 if (!next) {
702 return CC_ERROR;
703 }
704
705 for (; name[len]; ++len) {
706 if (name[len] != next[len]) {
707 break;
708 }
709 char out[2] = { name[len], '\0' };
710 el_insertstr(elstate, out);
711 }
712 return CC_REDISPLAY;
713 }
714 name += len - 1;
715 el_insertstr(elstate, name);
716 el_insertstr(elstate, " ");
717 return CC_REDISPLAY;
718}
719
720static void _cliDebuggerInit(struct mDebugger* debugger) {
721 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
722 // TODO: get argv[0]
723 cliDebugger->elstate = el_init(binaryName, stdin, stdout, stderr);
724 el_set(cliDebugger->elstate, EL_PROMPT, _prompt);
725 el_set(cliDebugger->elstate, EL_EDITOR, "emacs");
726
727 el_set(cliDebugger->elstate, EL_CLIENTDATA, cliDebugger);
728 el_set(cliDebugger->elstate, EL_ADDFN, "tab-complete", "Tab completion", _tabComplete);
729 el_set(cliDebugger->elstate, EL_BIND, "\t", "tab-complete", 0);
730 cliDebugger->histate = history_init();
731 HistEvent ev;
732 history(cliDebugger->histate, &ev, H_SETSIZE, 200);
733 el_set(cliDebugger->elstate, EL_HIST, history, cliDebugger->histate);
734 _activeDebugger = cliDebugger;
735 signal(SIGINT, _breakIntoDefault);
736}
737
738static void _cliDebuggerDeinit(struct mDebugger* debugger) {
739 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
740 history_end(cliDebugger->histate);
741 el_end(cliDebugger->elstate);
742
743 if (cliDebugger->system) {
744 cliDebugger->system->deinit(cliDebugger->system);
745 free(cliDebugger->system);
746 cliDebugger->system = 0;
747 }
748}
749
750static void _cliDebuggerCustom(struct mDebugger* debugger) {
751 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
752 bool retain = false;
753 if (cliDebugger->system) {
754 retain = cliDebugger->system->custom(cliDebugger->system);
755 }
756 if (!retain && debugger->state == DEBUGGER_CUSTOM) {
757 debugger->state = DEBUGGER_RUNNING;
758 }
759}
760
761void CLIDebuggerCreate(struct CLIDebugger* debugger) {
762 debugger->d.init = _cliDebuggerInit;
763 debugger->d.deinit = _cliDebuggerDeinit;
764 debugger->d.custom = _cliDebuggerCustom;
765 debugger->d.paused = _commandLine;
766 debugger->d.entered = _reportEntry;
767
768 debugger->system = 0;
769}
770
771void CLIDebuggerAttachSystem(struct CLIDebugger* debugger, struct CLIDebuggerSystem* system) {
772 if (debugger->system) {
773 debugger->system->deinit(debugger->system);
774 free(debugger->system);
775 }
776
777 debugger->system = system;
778 system->p = debugger;
779}
780
781#endif