mips/msim/debug.c

80 lines
1 KiB
C
Raw Normal View History

2024-09-30 22:52:25 +00:00
#include <merror.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <melf.h>
#include "sim.h"
static uint32_t read_num() {
int i = 0;
while (1) {
int c = getc(stdin);
if (c < '0' || c > '9') {
ungetc(c, stdin);
break;
}
i *= 10;
i += c - '0';
}
if (i)
return i;
return 1;
}
_Noreturn void sim_debug(struct simulator *sim) {
int c;
sim->pc = sim->entry;
sim->current_pc = sim->pc;
prompt:
printf(">> ");
fflush(stdout);
next:
switch ((c = getchar())) {
/* skip whitespace */
case ' ':
case '\t':
goto next;
/* ignore nl */
case '\n':
goto prompt;
/* exit on eof */
case EOF:
exit(0);
break;
/* dump registers */
case 'r':
sim_dump_reg(sim);
break;
/* step ins */
case 's': {
for (uint32_t i = read_num(); i > 0; i--)
sim_step(sim);
break;
}
/* print curr ins */
case 'i':
printf("0x%08x\n", B32(* (uint32_t *) (uintptr_t) sim->pc));
break;
case 'a':
printf("0x%08x\n", sim->pc);
break;
default:
ERROR("unknown command '%c'", c);
break;
}
goto next;
}