summaryrefslogtreecommitdiff
path: root/msim/debug.c
blob: 1613c7689a5fcc40d39b4625dc2ee5a7ac150734 (plain)
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
#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;
}