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 void _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 dv->intValue = value;
437 return;
438 }
439 dv->type = CLIDV_INT_TYPE;
440 value = cliDebugger->system->lookupIdentifier(cliDebugger->system, name, dv);
441 if (dv->type != CLIDV_ERROR_TYPE) {
442 dv->intValue = value;
443 return;
444 }
445 }
446 dv->type = CLIDV_ERROR_TYPE;
447}
448
449static void _evaluateParseTree(struct mDebugger* debugger, struct ParseTree* tree, struct CLIDebugVector* dv) {
450 int32_t lhs, rhs;
451 switch (tree->token.type) {
452 case TOKEN_UINT_TYPE:
453 dv->intValue = tree->token.uintValue;
454 break;
455 case TOKEN_SEGMENT_TYPE:
456 _evaluateParseTree(debugger, tree->lhs, dv);
457 dv->segmentValue = dv->intValue;
458 _evaluateParseTree(debugger, tree->rhs, dv);
459 break;
460 case TOKEN_OPERATOR_TYPE:
461 _evaluateParseTree(debugger, tree->lhs, dv);
462 lhs = dv->intValue;
463 _evaluateParseTree(debugger, tree->rhs, dv);
464 rhs = dv->intValue;
465 dv->intValue = _performOperation(tree->token.operatorValue, lhs, rhs, dv);
466 break;
467 case TOKEN_IDENTIFIER_TYPE:
468 _lookupIdentifier(debugger, tree->token.identifierValue, dv);
469 break;
470 case TOKEN_ERROR_TYPE:
471 default:
472 dv->type = CLIDV_ERROR_TYPE;
473 }
474}
475
476struct CLIDebugVector* CLIDVParse(struct CLIDebugger* debugger, const char* string, size_t length) {
477 if (!string || length < 1) {
478 return 0;
479 }
480
481 struct CLIDebugVector dvTemp = { .type = CLIDV_INT_TYPE, .segmentValue = -1 };
482
483 struct LexVector lv = { .next = 0 };
484 size_t adjusted = lexExpression(&lv, string, length);
485 if (adjusted > length) {
486 dvTemp.type = CLIDV_ERROR_TYPE;
487 lexFree(lv.next);
488 }
489
490 struct ParseTree tree;
491 parseLexedExpression(&tree, &lv);
492 if (tree.token.type == TOKEN_ERROR_TYPE) {
493 dvTemp.type = CLIDV_ERROR_TYPE;
494 } else {
495 _evaluateParseTree(&debugger->d, &tree, &dvTemp);
496 }
497
498 parseFree(tree.lhs);
499 parseFree(tree.rhs);
500
501 length -= adjusted;
502 string += adjusted;
503
504 struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
505 if (dvTemp.type == CLIDV_ERROR_TYPE) {
506 dv->type = CLIDV_ERROR_TYPE;
507 dv->next = 0;
508 } else {
509 *dv = dvTemp;
510 if (string[0] == ' ') {
511 dv->next = CLIDVParse(debugger, string + 1, length - 1);
512 if (dv->next && dv->next->type == CLIDV_ERROR_TYPE) {
513 dv->type = CLIDV_ERROR_TYPE;
514 }
515 }
516 }
517 return dv;
518}
519
520struct CLIDebugVector* CLIDVStringParse(struct CLIDebugger* debugger, const char* string, size_t length) {
521 if (!string || length < 1) {
522 return 0;
523 }
524
525 struct CLIDebugVector dvTemp = { .type = CLIDV_CHAR_TYPE };
526
527 size_t adjusted;
528 const char* next = strchr(string, ' ');
529 if (next) {
530 adjusted = next - string;
531 } else {
532 adjusted = length;
533 }
534 dvTemp.charValue = malloc(adjusted);
535 strncpy(dvTemp.charValue, string, adjusted);
536
537 length -= adjusted;
538 string += adjusted;
539
540 struct CLIDebugVector* dv = malloc(sizeof(struct CLIDebugVector));
541 *dv = dvTemp;
542 if (string[0] == ' ') {
543 dv->next = CLIDVStringParse(debugger, string + 1, length - 1);
544 if (dv->next && dv->next->type == CLIDV_ERROR_TYPE) {
545 dv->type = CLIDV_ERROR_TYPE;
546 }
547 }
548 return dv;
549}
550
551static void _DVFree(struct CLIDebugVector* dv) {
552 struct CLIDebugVector* next;
553 while (dv) {
554 next = dv->next;
555 if (dv->type == CLIDV_CHAR_TYPE) {
556 free(dv->charValue);
557 }
558 free(dv);
559 dv = next;
560 }
561}
562
563static int _tryCommands(struct CLIDebugger* debugger, struct CLIDebuggerCommandSummary* commands, const char* command, size_t commandLen, const char* args, size_t argsLen) {
564 struct CLIDebugVector* dv = 0;
565 int i;
566 const char* name;
567 for (i = 0; (name = commands[i].name); ++i) {
568 if (strlen(name) != commandLen) {
569 continue;
570 }
571 if (strncasecmp(name, command, commandLen) == 0) {
572 if (commands[i].parser) {
573 if (args) {
574 dv = commands[i].parser(debugger, args, argsLen);
575 if (dv && dv->type == CLIDV_ERROR_TYPE) {
576 printf("Parse error\n");
577 _DVFree(dv);
578 return false;
579 }
580 }
581 } else if (args) {
582 printf("Wrong number of arguments\n");
583 return false;
584 }
585 commands[i].command(debugger, dv);
586 _DVFree(dv);
587 return true;
588 }
589 }
590 return -1;
591}
592
593static bool _parse(struct CLIDebugger* debugger, const char* line, size_t count) {
594 const char* firstSpace = strchr(line, ' ');
595 size_t cmdLength;
596 if (firstSpace) {
597 cmdLength = firstSpace - line;
598 } else {
599 cmdLength = count;
600 }
601
602 const char* args = 0;
603 if (firstSpace) {
604 args = firstSpace + 1;
605 }
606 int result = _tryCommands(debugger, _debuggerCommands, line, cmdLength, args, count - cmdLength - 1);
607 if (result < 0 && debugger->system) {
608 result = _tryCommands(debugger, debugger->system->commands, line, cmdLength, args, count - cmdLength - 1);
609 if (result < 0) {
610 result = _tryCommands(debugger, debugger->system->platformCommands, line, cmdLength, args, count - cmdLength - 1);
611 }
612 }
613 if (result < 0) {
614 printf("Command not found\n");
615 }
616 return false;
617}
618
619static char* _prompt(EditLine* el) {
620 UNUSED(el);
621 return "> ";
622}
623
624static void _commandLine(struct mDebugger* debugger) {
625 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
626 const char* line;
627 _printStatus(cliDebugger, 0);
628 int count = 0;
629 HistEvent ev;
630 while (debugger->state == DEBUGGER_PAUSED) {
631 line = el_gets(cliDebugger->elstate, &count);
632 if (!line) {
633 debugger->state = DEBUGGER_SHUTDOWN;
634 return;
635 }
636 if (line[0] == '\n') {
637 if (history(cliDebugger->histate, &ev, H_FIRST) >= 0) {
638 _parse(cliDebugger, ev.str, strlen(ev.str) - 1);
639 }
640 } else {
641 _parse(cliDebugger, line, count - 1);
642 history(cliDebugger->histate, &ev, H_ENTER, line);
643 }
644 }
645}
646
647static void _reportEntry(struct mDebugger* debugger, enum mDebuggerEntryReason reason, struct mDebuggerEntryInfo* info) {
648 UNUSED(debugger);
649 switch (reason) {
650 case DEBUGGER_ENTER_MANUAL:
651 case DEBUGGER_ENTER_ATTACHED:
652 break;
653 case DEBUGGER_ENTER_BREAKPOINT:
654 if (info) {
655 printf("Hit breakpoint at 0x%08X\n", info->address);
656 } else {
657 printf("Hit breakpoint\n");
658 }
659 break;
660 case DEBUGGER_ENTER_WATCHPOINT:
661 if (info) {
662 if (info->accessType & WATCHPOINT_WRITE) {
663 printf("Hit watchpoint at 0x%08X: (new value = 0x%08x, old value = 0x%08X)\n", info->address, info->newValue, info->oldValue);
664 } else {
665 printf("Hit watchpoint at 0x%08X: (value = 0x%08x)\n", info->address, info->oldValue);
666 }
667 } else {
668 printf("Hit watchpoint\n");
669 }
670 break;
671 case DEBUGGER_ENTER_ILLEGAL_OP:
672 if (info) {
673 printf("Hit illegal opcode at 0x%08X: 0x%08X\n", info->address, info->opcode);
674 } else {
675 printf("Hit illegal opcode\n");
676 }
677 break;
678 }
679}
680
681static unsigned char _tabComplete(EditLine* elstate, int ch) {
682 UNUSED(ch);
683 const LineInfo* li = el_line(elstate);
684 if (!li->buffer[0]) {
685 return CC_ERROR;
686 }
687
688 const char* commandPtr;
689 size_t cmd = 0, len = 0;
690 const char* name = 0;
691 for (commandPtr = li->buffer; commandPtr <= li->cursor; ++commandPtr, ++len) {
692 for (; (name = _debuggerCommands[cmd].name); ++cmd) {
693 int cmp = strncasecmp(name, li->buffer, len);
694 if (cmp > 0) {
695 return CC_ERROR;
696 }
697 if (cmp == 0) {
698 break;
699 }
700 }
701 }
702 if (!name) {
703 return CC_ERROR;
704 }
705 if (_debuggerCommands[cmd + 1].name && strlen(_debuggerCommands[cmd + 1].name) >= len - 1 && name[len - 2] == _debuggerCommands[cmd + 1].name[len - 2]) {
706 --len;
707 const char* next = 0;
708 int i;
709 for (i = cmd + 1; _debuggerCommands[i].name; ++i) {
710 if (strncasecmp(name, _debuggerCommands[i].name, len)) {
711 break;
712 }
713 next = _debuggerCommands[i].name;
714 }
715 if (!next) {
716 return CC_ERROR;
717 }
718
719 for (; name[len]; ++len) {
720 if (name[len] != next[len]) {
721 break;
722 }
723 char out[2] = { name[len], '\0' };
724 el_insertstr(elstate, out);
725 }
726 return CC_REDISPLAY;
727 }
728 name += len - 1;
729 el_insertstr(elstate, name);
730 el_insertstr(elstate, " ");
731 return CC_REDISPLAY;
732}
733
734static void _cliDebuggerInit(struct mDebugger* debugger) {
735 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
736 // TODO: get argv[0]
737 cliDebugger->elstate = el_init(binaryName, stdin, stdout, stderr);
738 el_set(cliDebugger->elstate, EL_PROMPT, _prompt);
739 el_set(cliDebugger->elstate, EL_EDITOR, "emacs");
740
741 el_set(cliDebugger->elstate, EL_CLIENTDATA, cliDebugger);
742 el_set(cliDebugger->elstate, EL_ADDFN, "tab-complete", "Tab completion", _tabComplete);
743 el_set(cliDebugger->elstate, EL_BIND, "\t", "tab-complete", 0);
744 cliDebugger->histate = history_init();
745 HistEvent ev;
746 history(cliDebugger->histate, &ev, H_SETSIZE, 200);
747 el_set(cliDebugger->elstate, EL_HIST, history, cliDebugger->histate);
748 _activeDebugger = cliDebugger;
749 signal(SIGINT, _breakIntoDefault);
750}
751
752static void _cliDebuggerDeinit(struct mDebugger* debugger) {
753 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
754 history_end(cliDebugger->histate);
755 el_end(cliDebugger->elstate);
756
757 if (cliDebugger->system) {
758 if (cliDebugger->system->deinit) {
759 cliDebugger->system->deinit(cliDebugger->system);
760 }
761 free(cliDebugger->system);
762 cliDebugger->system = 0;
763 }
764}
765
766static void _cliDebuggerCustom(struct mDebugger* debugger) {
767 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
768 bool retain = false;
769 if (cliDebugger->system) {
770 retain = cliDebugger->system->custom(cliDebugger->system);
771 }
772 if (!retain && debugger->state == DEBUGGER_CUSTOM) {
773 debugger->state = DEBUGGER_RUNNING;
774 }
775}
776
777void CLIDebuggerCreate(struct CLIDebugger* debugger) {
778 debugger->d.init = _cliDebuggerInit;
779 debugger->d.deinit = _cliDebuggerDeinit;
780 debugger->d.custom = _cliDebuggerCustom;
781 debugger->d.paused = _commandLine;
782 debugger->d.entered = _reportEntry;
783
784 debugger->system = 0;
785}
786
787void CLIDebuggerAttachSystem(struct CLIDebugger* debugger, struct CLIDebuggerSystem* system) {
788 if (debugger->system) {
789 if (debugger->system->deinit) {
790 debugger->system->deinit(debugger->system);
791 }
792 free(debugger->system);
793 }
794
795 debugger->system = system;
796 system->p = debugger;
797}
798
799#endif