-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
366 lines (304 loc) · 11.9 KB
/
cli.c
File metadata and controls
366 lines (304 loc) · 11.9 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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
#include "debug-state.h"
#include "define.h"
#include "inst2text.h"
#include "state.h"
#include "util.h"
#define MAX_COMMAND_TOKENS 10
const int MAX_COMMAND_LENGTH = 512;
const char* PROMPT = "(jonsp430) ";
typedef void (* cli_operation_T) (struct state*, struct debug_state*, char **);
struct command_type {
char *name;
cli_operation_T op;
char *help_string;
};
struct command_instance {
struct command_type type;
char *args[MAX_COMMAND_TOKENS];
};
// dummy placeholder
void cli_quit(struct state *context, struct debug_state *dbg_state, char **args) { }
void cli_start(struct state *context) {
struct debug_state *dbg_context = init_debug_state();
printf("===================================\n");
printf("= jonsp430 =\n");
printf("= =\n");
printf("= custom msp430 emulator/debugger =\n");
printf("===================================\n");
printf("\n");
printf("legend: %s, %s, %s\n", "debugger text", FROM_CONSOLE("device stdout"), FROM_ERROR("device stderr"));
printf("the random seed for this session is: %d\n", context->seed);
printf("\ntype \"help\" for a list of available commands\n");
printf("\n");
char line[MAX_COMMAND_LENGTH];
int keep_going = 1;
while (keep_going) {
printf ("%s", PROMPT);
struct command_instance command;
if (!fgets (line, MAX_COMMAND_LENGTH, stdin)) {
command.type.op = cli_quit;
} else {
command = cli_parse(line);
}
if (command.type.op == cli_quit) {
keep_going = 0;
printf("\n");
} else {
command.type.op(context, dbg_context, command.args);
}
if (context == NULL) {
keep_going = 0;
printf("\n");
}
}
destroy_debug_state(dbg_context);
}
// no args
void cli_unrecognized(struct state *context, struct debug_state *dbg_state, char **args) {
printf ("unrecognized instruction: %s\n", args[0]);
}
// no args
void cli_ambiguous(struct state *context, struct debug_state *dbg_state, char **args) {
printf ("ambiguous instruction: %s\n", args[0]);
}
// no args
void cli_none (struct state *context, struct debug_state *dbg_state, char **args) { }
//////
// NO MODIFY STATE
//////
// [*] arg 1: address to read
// arg 2: number of bytes to read
void cli_read(struct state *context, struct debug_state *dbg_context, char **args) {
int start, nbytes, i;
if (args[1] == NULL) {
printf ("insufficient arguments for read command\n");
} else {
start = strtol(args[1], NULL, 0);
}
if (args[2] == NULL) {
nbytes = 16;
} else {
nbytes = strtol(args[2], NULL, 0);
}
if (start + nbytes > 0x10000) {
printf ("command reads out-of-bounds memory\n");
} else {
for (i=0; i < nbytes; i += 2) {
if (i % 16 == 0) {
if (i) {
printf("\n");
}
printf ("0x%04x: ", start + i);
}
char byteval[2];
byte2ascii(get_byte_at(context, NULL, start+i), byteval);
printf ("%c%c", byteval[0], byteval[1]);
byte2ascii(get_byte_at(context, NULL, start+i+1), byteval);
printf("%c%c ", byteval[0], byteval[1]);
}
printf ("\n");
}
}
// no args
void cli_regs(struct state *context, struct debug_state *dbg_context, char **args) {
int i, j;
// printf changes x to unsigned int; we only want the last 4 hex digits
printf ("pc %04x ", context->reg[REG_PC] & 0x0000ffff);
printf ("sp %04x ", context->reg[REG_SP] & 0x0000ffff);
printf ("sr %04x ", context->reg[REG_SR] & 0x0000ffff);
printf ("cg %04x\n", context->reg[REG_CG] & 0x0000ffff);
for (i=1; i < 4; ++i) {
for (j=0; j < 4; ++j) {
printf ("r%02d %04x ", 4*i + j, context->reg[4*i + j] & 0x0000ffff);
}
printf ("\n");
}
}
// [*] arg1: file to disassemble to
void cli_disassemble(struct state *context, struct debug_state *dbg_context, char **args) {
word_T start = 0x4400;
int num_insts = 5;
FILE *outfile = stdout;
if (args[1]) {
start = strtol(args[1], NULL, 0) & 0xffff;
}
if (args[2]) {
int tmp_num_insts = strtol(args[2], NULL, 0);
if (tmp_num_insts > 0) {
num_insts = tmp_num_insts;
}
}
// if (args[3]) {
// TODO: disassemble to file
// }
disassemble(context, start, num_insts, outfile);
}
void cli_log(struct state *context, struct debug_state *dbg_context, char **args) {
int fd;
FILE *fp = NULL;
if (args[1]) {
fd = open(args[1], O_WRONLY | O_CREAT | O_EXCL);
if (fd == -1) {
perror("open");
// TODO: return fail
}
fp = fdopen(fd, "w");
if (fp == NULL) {
perror("fopen");
// TODO: return fail
}
dbg_context->logfile = fp;
} else {
dbg_context->logfile = stdout;
}
if (dbg_context->logfile) {
fprintf(dbg_context->logfile, "random seed: %d\n", context->seed);
}
}
void cli_unlog(struct state *context, struct debug_state *dbg_context, char **args) {
fclose(dbg_context->logfile);
dbg_context->logfile = NULL;
}
///////
// MODIFY STATE
///////
// no args
void cli_next (struct state *context, struct debug_state *dbg_context, char **args) {
trigger_start(dbg_context);
untrigger_stop(dbg_context);
do_next_state(context, dbg_context);
untrigger_start(dbg_context);
cli_regs(context, dbg_context, NULL);
disassemble(context, get_reg(context, dbg_context, REG_PC), 1, stdout);
}
// no args
void cli_continue (struct state *context, struct debug_state *dbg_context, char **args) {
trigger_start(dbg_context);
untrigger_stop(dbg_context);
while (!(get_flag(context, FLAG_CPUOFF) || stop_triggered(dbg_context) || context->unlocked || context->crashed)) {
do_next_state(context, dbg_context);
untrigger_start(dbg_context);
}
cli_regs(context, dbg_context, NULL);
disassemble(context, get_reg(context, dbg_context, REG_PC), 1, stdout);
}
// no args
void cli_reset(struct state *context, struct debug_state *dbg_context, char **args) {
reset_state(context);
}
//////
// MODIFY DEBUG STATE
/////
// [*] arg1: address to set breakpoint at
void cli_break (struct state *context, struct debug_state *dbg_context, char **args) {
if (args[1] == NULL) {
printf ("break requires an argument\n");
} else {
word_T loc = strtol(args[1], NULL, 0) & 0xffff;
add_breakpoint(dbg_context, loc);
printf ("breakpoint set at 0x%04x\n", loc & 0xffff);
}
}
// [*] arg1: address to unset breakpoint at
void cli_unbreak (struct state *context, struct debug_state *dbg_context, char **args) {
printf ("NOT IMPLEMENTED YET\n");
}
void cli_help(struct state *context, struct debug_state *dbg_state, char **args);
struct command_type command_function_table[] = {
{"break", cli_break,
"set a breakpoint at <ARG1>; this includes either memory access at <ARG1>, or when executing the instruction located at <ARG1>"},
{"continue", cli_continue,
"continues execution until one of the following occurs: (1) the door is unlocked; (2) the CPUOFF flag is set; (3) a breakpoint is hit; (4) a \"gets\" interrupt; (5) a crash"},
{"disassemble", cli_disassemble,
"prints disassembly of <ARG2=5> instructions starting at <ARG1=0x4400>"},
{"help", cli_help,
"get help on function named <ARG1>; key: <ARGi> is 'i'th argument provided to function; <ARGi=d> is 'i'th argument provided to function, which takes a default value of 'd'"},
{"log", cli_log,
"log execution data to file named <ARG1=stdout>"},
{"next", cli_next,
"execute one instruction"},
{"quit", cli_quit,
"quit jonsp430"},
{"read", cli_read,
"read <ARG2=16> bytes starting at memory location <ARG1>"},
{"registers", cli_regs,
"view value of registers"},
{"reset", cli_reset,
"reset cpu to initial state at beginning of program"},
{"unbreak", cli_unbreak,
"not implemented yet"},
{"unlog", cli_unlog,
"stop logging instructions"},
};
#define NUM_COMMANDS (sizeof (command_function_table) / sizeof(struct command_type))
struct command_instance cli_parse (char *line) {
struct command_instance ret;
struct command_type ret_type;
int i = 0;
int num_toks = 0;
int num_matches = 0;
for (i=0; i < MAX_COMMAND_TOKENS; ++i) {
ret.args[i] = NULL;
}
if (line[0] == '\n' || line[0] == '\r') {
ret_type.name = "none";
ret_type.op = cli_none;
ret.type = ret_type;
return ret;
}
char *tok = strtok(line, " \t\r\n");
while (num_toks < MAX_COMMAND_TOKENS && tok != NULL) {
ret.args[num_toks] = tok;
++num_toks;
tok = strtok(NULL, " \t\r\n");
}
if (ret.args[0][0] == '\00') {
ret_type.name = "none";
ret_type.op = cli_none;
ret.type = ret_type;
return ret;
}
// for (i=0; i < sizeof(command_function_table) / sizeof(struct command_type); ++i) {
for (i=0; i < NUM_COMMANDS; ++i) {
if (strstr(command_function_table[i].name, ret.args[0]) == command_function_table[i].name) {
++num_matches;
ret_type.name = command_function_table[i].name;
ret_type.op = command_function_table[i].op;
}
}
if (num_matches == 0) {
ret_type.name = "unrecognized";
ret_type.op = cli_unrecognized;
} else if (num_matches > 1) {
ret_type.name = "ambiguous";
ret_type.op = cli_ambiguous;
}
ret.type = ret_type;
return ret;
}
void cli_help(struct state *context, struct debug_state *dbg_state, char **args) {
int i;
int found = 0;
if (args[1] == NULL) {
printf("\navailable commands:\n");
for (i = 0; i < NUM_COMMANDS; ++i) {
printf("\t%s\n", command_function_table[i].name);
}
printf("\ntype \"help <command>\" to learn more about an individual command\n\n");
} else {
for (i = 0; i < NUM_COMMANDS; ++i) {
if (!strcmp(command_function_table[i].name, args[1])) {
printf("\n\t%s: %s\n\n", command_function_table[i].name, command_function_table[i].help_string);
found = 1;
}
}
if (!found) {
printf("unrecognized command; type \"help\" to see list of available commands.\n");
}
}
}