]>
Commit | Line | Data |
---|---|---|
9dc39cba FB |
1 | /* |
2 | * QEMU monitor | |
3 | * | |
4 | * Copyright (c) 2003-2004 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
9dc39cba | 24 | #include "vl.h" |
9307c4c1 | 25 | #include "disas.h" |
9dc39cba FB |
26 | |
27 | //#define DEBUG | |
28 | ||
9307c4c1 FB |
29 | #ifndef offsetof |
30 | #define offsetof(type, field) ((size_t) &((type *)0)->field) | |
31 | #endif | |
32 | ||
9dc39cba | 33 | #define TERM_CMD_BUF_SIZE 4095 |
aa455485 | 34 | #define TERM_MAX_CMDS 64 |
9dc39cba FB |
35 | |
36 | #define IS_NORM 0 | |
37 | #define IS_ESC 1 | |
38 | #define IS_CSI 2 | |
39 | ||
40 | #define printf do_not_use_printf | |
41 | ||
42 | static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1]; | |
43 | static int term_cmd_buf_index; | |
44 | static int term_cmd_buf_size; | |
45 | static int term_esc_state; | |
46 | static int term_esc_param; | |
47 | ||
aa455485 FB |
48 | static char *term_history[TERM_MAX_CMDS]; |
49 | static int term_hist_entry; | |
50 | ||
9307c4c1 FB |
51 | /* |
52 | * Supported types: | |
53 | * | |
54 | * 'F' filename | |
55 | * 's' string (accept optional quote) | |
56 | * 'i' integer | |
57 | * '/' optional gdb-like print format (like "/10x") | |
58 | * | |
59 | * '?' optional type (for 'F', 's' and 'i') | |
60 | * | |
61 | */ | |
62 | ||
9dc39cba FB |
63 | typedef struct term_cmd_t { |
64 | const char *name; | |
9307c4c1 FB |
65 | const char *args_type; |
66 | void (*handler)(); | |
9dc39cba FB |
67 | const char *params; |
68 | const char *help; | |
69 | } term_cmd_t; | |
70 | ||
71 | static term_cmd_t term_cmds[]; | |
72 | static term_cmd_t info_cmds[]; | |
73 | ||
74 | void term_printf(const char *fmt, ...) | |
75 | { | |
76 | va_list ap; | |
77 | va_start(ap, fmt); | |
78 | vprintf(fmt, ap); | |
79 | va_end(ap); | |
80 | } | |
81 | ||
82 | void term_flush(void) | |
83 | { | |
84 | fflush(stdout); | |
85 | } | |
86 | ||
87 | static int compare_cmd(const char *name, const char *list) | |
88 | { | |
89 | const char *p, *pstart; | |
90 | int len; | |
91 | len = strlen(name); | |
92 | p = list; | |
93 | for(;;) { | |
94 | pstart = p; | |
95 | p = strchr(p, '|'); | |
96 | if (!p) | |
97 | p = pstart + strlen(pstart); | |
98 | if ((p - pstart) == len && !memcmp(pstart, name, len)) | |
99 | return 1; | |
100 | if (*p == '\0') | |
101 | break; | |
102 | p++; | |
103 | } | |
104 | return 0; | |
105 | } | |
106 | ||
107 | static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name) | |
108 | { | |
109 | term_cmd_t *cmd; | |
110 | ||
111 | for(cmd = cmds; cmd->name != NULL; cmd++) { | |
112 | if (!name || !strcmp(name, cmd->name)) | |
113 | term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help); | |
114 | } | |
115 | } | |
116 | ||
117 | static void help_cmd(const char *name) | |
118 | { | |
119 | if (name && !strcmp(name, "info")) { | |
120 | help_cmd1(info_cmds, "info ", NULL); | |
121 | } else { | |
122 | help_cmd1(term_cmds, "", name); | |
f193c797 FB |
123 | if (name && !strcmp(name, "log")) { |
124 | CPULogItem *item; | |
125 | term_printf("Log items (comma separated):\n"); | |
126 | term_printf("%-10s %s\n", "none", "remove all logs"); | |
127 | for(item = cpu_log_items; item->mask != 0; item++) { | |
128 | term_printf("%-10s %s\n", item->name, item->help); | |
129 | } | |
130 | } | |
9dc39cba FB |
131 | } |
132 | } | |
133 | ||
9307c4c1 | 134 | static void do_help(const char *name) |
9dc39cba | 135 | { |
9307c4c1 | 136 | help_cmd(name); |
9dc39cba FB |
137 | } |
138 | ||
9307c4c1 | 139 | static void do_commit(void) |
9dc39cba FB |
140 | { |
141 | int i; | |
142 | ||
143 | for (i = 0; i < MAX_DISKS; i++) { | |
144 | if (bs_table[i]) | |
145 | bdrv_commit(bs_table[i]); | |
146 | } | |
147 | } | |
148 | ||
9307c4c1 | 149 | static void do_info(const char *item) |
9dc39cba FB |
150 | { |
151 | term_cmd_t *cmd; | |
9dc39cba | 152 | |
9307c4c1 | 153 | if (!item) |
9dc39cba | 154 | goto help; |
9dc39cba | 155 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
9307c4c1 | 156 | if (compare_cmd(item, cmd->name)) |
9dc39cba FB |
157 | goto found; |
158 | } | |
159 | help: | |
9307c4c1 | 160 | help_cmd("info"); |
9dc39cba FB |
161 | return; |
162 | found: | |
9307c4c1 | 163 | cmd->handler(); |
9dc39cba FB |
164 | } |
165 | ||
9307c4c1 | 166 | static void do_info_network(void) |
9dc39cba FB |
167 | { |
168 | int i, j; | |
169 | NetDriverState *nd; | |
170 | ||
171 | for(i = 0; i < nb_nics; i++) { | |
172 | nd = &nd_table[i]; | |
173 | term_printf("%d: ifname=%s macaddr=", i, nd->ifname); | |
174 | for(j = 0; j < 6; j++) { | |
175 | if (j > 0) | |
176 | term_printf(":"); | |
177 | term_printf("%02x", nd->macaddr[j]); | |
178 | } | |
179 | term_printf("\n"); | |
180 | } | |
181 | } | |
182 | ||
9307c4c1 | 183 | static void do_info_block(void) |
9dc39cba FB |
184 | { |
185 | bdrv_info(); | |
186 | } | |
187 | ||
9307c4c1 FB |
188 | static void do_info_registers(void) |
189 | { | |
190 | #ifdef TARGET_I386 | |
191 | cpu_dump_state(cpu_single_env, stdout, X86_DUMP_FPU | X86_DUMP_CCOP); | |
192 | #else | |
193 | cpu_dump_state(cpu_single_env, stdout, 0); | |
194 | #endif | |
195 | } | |
196 | ||
aa455485 FB |
197 | static void do_info_history (void) |
198 | { | |
199 | int i; | |
200 | ||
201 | for (i = 0; i < TERM_MAX_CMDS; i++) { | |
202 | if (term_history[i] == NULL) | |
203 | break; | |
204 | term_printf("%d: '%s'\n", i, term_history[i]); | |
205 | } | |
206 | } | |
207 | ||
9307c4c1 | 208 | static void do_quit(void) |
9dc39cba FB |
209 | { |
210 | exit(0); | |
211 | } | |
212 | ||
213 | static int eject_device(BlockDriverState *bs, int force) | |
214 | { | |
215 | if (bdrv_is_inserted(bs)) { | |
216 | if (!force) { | |
217 | if (!bdrv_is_removable(bs)) { | |
218 | term_printf("device is not removable\n"); | |
219 | return -1; | |
220 | } | |
221 | if (bdrv_is_locked(bs)) { | |
222 | term_printf("device is locked\n"); | |
223 | return -1; | |
224 | } | |
225 | } | |
226 | bdrv_close(bs); | |
227 | } | |
228 | return 0; | |
229 | } | |
230 | ||
9307c4c1 | 231 | static void do_eject(int force, const char *filename) |
9dc39cba FB |
232 | { |
233 | BlockDriverState *bs; | |
9dc39cba | 234 | |
9307c4c1 FB |
235 | term_printf("%d %s\n", force, filename); |
236 | ||
237 | bs = bdrv_find(filename); | |
9dc39cba FB |
238 | if (!bs) { |
239 | term_printf("device not found\n"); | |
240 | return; | |
241 | } | |
242 | eject_device(bs, force); | |
243 | } | |
244 | ||
9307c4c1 | 245 | static void do_change(const char *device, const char *filename) |
9dc39cba FB |
246 | { |
247 | BlockDriverState *bs; | |
248 | ||
9307c4c1 | 249 | bs = bdrv_find(device); |
9dc39cba FB |
250 | if (!bs) { |
251 | term_printf("device not found\n"); | |
252 | return; | |
253 | } | |
254 | if (eject_device(bs, 0) < 0) | |
255 | return; | |
9307c4c1 | 256 | bdrv_open(bs, filename, 0); |
9dc39cba FB |
257 | } |
258 | ||
9307c4c1 | 259 | static void do_screen_dump(const char *filename) |
59a983b9 | 260 | { |
9307c4c1 | 261 | vga_screen_dump(filename); |
59a983b9 FB |
262 | } |
263 | ||
9307c4c1 | 264 | static void do_log(const char *items) |
f193c797 FB |
265 | { |
266 | int mask; | |
267 | ||
9307c4c1 | 268 | if (!strcmp(items, "none")) { |
f193c797 FB |
269 | mask = 0; |
270 | } else { | |
9307c4c1 | 271 | mask = cpu_str_to_log_mask(items); |
f193c797 | 272 | if (!mask) { |
9307c4c1 | 273 | help_cmd("log"); |
f193c797 FB |
274 | return; |
275 | } | |
276 | } | |
277 | cpu_set_log(mask); | |
278 | } | |
279 | ||
9307c4c1 | 280 | static void do_savevm(const char *filename) |
8a7ddc38 | 281 | { |
9307c4c1 FB |
282 | if (qemu_savevm(filename) < 0) |
283 | term_printf("I/O error when saving VM to '%s'\n", filename); | |
8a7ddc38 FB |
284 | } |
285 | ||
9307c4c1 | 286 | static void do_loadvm(const char *filename) |
8a7ddc38 | 287 | { |
9307c4c1 FB |
288 | if (qemu_loadvm(filename) < 0) |
289 | term_printf("I/O error when loading VM from '%s'\n", filename); | |
8a7ddc38 FB |
290 | } |
291 | ||
9307c4c1 | 292 | static void do_stop(void) |
8a7ddc38 FB |
293 | { |
294 | vm_stop(EXCP_INTERRUPT); | |
295 | } | |
296 | ||
9307c4c1 | 297 | static void do_cont(void) |
8a7ddc38 FB |
298 | { |
299 | vm_start(); | |
300 | } | |
301 | ||
67b915a5 | 302 | #ifdef CONFIG_GDBSTUB |
9307c4c1 | 303 | static void do_gdbserver(int has_port, int port) |
8a7ddc38 | 304 | { |
9307c4c1 FB |
305 | if (!has_port) |
306 | port = DEFAULT_GDBSTUB_PORT; | |
8a7ddc38 FB |
307 | if (gdbserver_start(port) < 0) { |
308 | qemu_printf("Could not open gdbserver socket on port %d\n", port); | |
309 | } else { | |
310 | qemu_printf("Waiting gdb connection on port %d\n", port); | |
311 | } | |
312 | } | |
67b915a5 | 313 | #endif |
8a7ddc38 | 314 | |
9307c4c1 FB |
315 | static void term_printc(int c) |
316 | { | |
317 | term_printf("'"); | |
318 | switch(c) { | |
319 | case '\'': | |
320 | term_printf("\\'"); | |
321 | break; | |
322 | case '\\': | |
323 | term_printf("\\\\"); | |
324 | break; | |
325 | case '\n': | |
326 | term_printf("\\n"); | |
327 | break; | |
328 | case '\r': | |
329 | term_printf("\\r"); | |
330 | break; | |
331 | default: | |
332 | if (c >= 32 && c <= 126) { | |
333 | term_printf("%c", c); | |
334 | } else { | |
335 | term_printf("\\x%02x", c); | |
336 | } | |
337 | break; | |
338 | } | |
339 | term_printf("'"); | |
340 | } | |
341 | ||
342 | static void memory_dump(int count, int format, int wsize, | |
343 | target_ulong addr, int is_physical) | |
344 | { | |
345 | int nb_per_line, l, line_size, i, max_digits, len; | |
346 | uint8_t buf[16]; | |
347 | uint64_t v; | |
348 | ||
349 | if (format == 'i') { | |
350 | int flags; | |
351 | flags = 0; | |
352 | #ifdef TARGET_I386 | |
4c27ba27 | 353 | if (wsize == 2) { |
9307c4c1 | 354 | flags = 1; |
4c27ba27 FB |
355 | } else if (wsize == 4) { |
356 | flags = 0; | |
357 | } else { | |
358 | /* as default we use the current CS size */ | |
359 | flags = 0; | |
360 | if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK)) | |
361 | flags = 1; | |
362 | } | |
363 | #endif | |
9307c4c1 FB |
364 | monitor_disas(addr, count, is_physical, flags); |
365 | return; | |
366 | } | |
367 | ||
368 | len = wsize * count; | |
369 | if (wsize == 1) | |
370 | line_size = 8; | |
371 | else | |
372 | line_size = 16; | |
373 | nb_per_line = line_size / wsize; | |
374 | max_digits = 0; | |
375 | ||
376 | switch(format) { | |
377 | case 'o': | |
378 | max_digits = (wsize * 8 + 2) / 3; | |
379 | break; | |
380 | default: | |
381 | case 'x': | |
382 | max_digits = (wsize * 8) / 4; | |
383 | break; | |
384 | case 'u': | |
385 | case 'd': | |
386 | max_digits = (wsize * 8 * 10 + 32) / 33; | |
387 | break; | |
388 | case 'c': | |
389 | wsize = 1; | |
390 | break; | |
391 | } | |
392 | ||
393 | while (len > 0) { | |
394 | term_printf("0x%08x:", addr); | |
395 | l = len; | |
396 | if (l > line_size) | |
397 | l = line_size; | |
398 | if (is_physical) { | |
399 | cpu_physical_memory_rw(addr, buf, l, 0); | |
400 | } else { | |
401 | cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0); | |
402 | } | |
403 | i = 0; | |
404 | while (i < l) { | |
405 | switch(wsize) { | |
406 | default: | |
407 | case 1: | |
408 | v = ldub_raw(buf + i); | |
409 | break; | |
410 | case 2: | |
411 | v = lduw_raw(buf + i); | |
412 | break; | |
413 | case 4: | |
414 | v = ldl_raw(buf + i); | |
415 | break; | |
416 | case 8: | |
417 | v = ldq_raw(buf + i); | |
418 | break; | |
419 | } | |
420 | term_printf(" "); | |
421 | switch(format) { | |
422 | case 'o': | |
423 | term_printf("%#*llo", max_digits, v); | |
424 | break; | |
425 | case 'x': | |
426 | term_printf("0x%0*llx", max_digits, v); | |
427 | break; | |
428 | case 'u': | |
429 | term_printf("%*llu", max_digits, v); | |
430 | break; | |
431 | case 'd': | |
432 | term_printf("%*lld", max_digits, v); | |
433 | break; | |
434 | case 'c': | |
435 | term_printc(v); | |
436 | break; | |
437 | } | |
438 | i += wsize; | |
439 | } | |
440 | term_printf("\n"); | |
441 | addr += l; | |
442 | len -= l; | |
443 | } | |
444 | } | |
445 | ||
446 | static void do_memory_dump(int count, int format, int size, int addr) | |
447 | { | |
448 | memory_dump(count, format, size, addr, 0); | |
449 | } | |
450 | ||
451 | static void do_physical_memory_dump(int count, int format, int size, int addr) | |
452 | { | |
453 | memory_dump(count, format, size, addr, 1); | |
454 | } | |
455 | ||
456 | static void do_print(int count, int format, int size, int val) | |
457 | { | |
458 | switch(format) { | |
459 | case 'o': | |
460 | term_printf("%#o", val); | |
461 | break; | |
462 | case 'x': | |
463 | term_printf("%#x", val); | |
464 | break; | |
465 | case 'u': | |
466 | term_printf("%u", val); | |
467 | break; | |
468 | default: | |
469 | case 'd': | |
470 | term_printf("%d", val); | |
471 | break; | |
472 | case 'c': | |
473 | term_printc(val); | |
474 | break; | |
475 | } | |
476 | term_printf("\n"); | |
477 | } | |
478 | ||
9dc39cba | 479 | static term_cmd_t term_cmds[] = { |
9307c4c1 | 480 | { "help|?", "s?", do_help, |
9dc39cba | 481 | "[cmd]", "show the help" }, |
9307c4c1 | 482 | { "commit", "", do_commit, |
9dc39cba | 483 | "", "commit changes to the disk images (if -snapshot is used)" }, |
9307c4c1 | 484 | { "info", "s?", do_info, |
9dc39cba | 485 | "subcommand", "show various information about the system state" }, |
9307c4c1 | 486 | { "q|quit", "", do_quit, |
9dc39cba | 487 | "", "quit the emulator" }, |
9307c4c1 | 488 | { "eject", "-fs", do_eject, |
9dc39cba | 489 | "[-f] device", "eject a removable media (use -f to force it)" }, |
9307c4c1 | 490 | { "change", "sF", do_change, |
9dc39cba | 491 | "device filename", "change a removable media" }, |
9307c4c1 | 492 | { "screendump", "F", do_screen_dump, |
59a983b9 | 493 | "filename", "save screen into PPM image 'filename'" }, |
9307c4c1 | 494 | { "log", "s", do_log, |
f193c797 | 495 | "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, |
9307c4c1 | 496 | { "savevm", "F", do_savevm, |
8a7ddc38 | 497 | "filename", "save the whole virtual machine state to 'filename'" }, |
9307c4c1 | 498 | { "loadvm", "F", do_loadvm, |
8a7ddc38 | 499 | "filename", "restore the whole virtual machine state from 'filename'" }, |
9307c4c1 FB |
500 | { "stop", "", do_stop, |
501 | "", "stop emulation", }, | |
502 | { "c|cont", "", do_cont, | |
503 | "", "resume emulation", }, | |
67b915a5 | 504 | #ifdef CONFIG_GDBSTUB |
9307c4c1 FB |
505 | { "gdbserver", "i?", do_gdbserver, |
506 | "[port]", "start gdbserver session (default port=1234)", }, | |
67b915a5 | 507 | #endif |
9307c4c1 FB |
508 | { "x", "/i", do_memory_dump, |
509 | "/fmt addr", "virtual memory dump starting at 'addr'", }, | |
510 | { "xp", "/i", do_physical_memory_dump, | |
511 | "/fmt addr", "physical memory dump starting at 'addr'", }, | |
512 | { "p|print", "/i", do_print, | |
513 | "/fmt expr", "print expression value (use $reg for CPU register access)", }, | |
f193c797 | 514 | { NULL, NULL, }, |
9dc39cba FB |
515 | }; |
516 | ||
517 | static term_cmd_t info_cmds[] = { | |
9307c4c1 | 518 | { "network", "", do_info_network, |
9dc39cba | 519 | "", "show the network state" }, |
9307c4c1 | 520 | { "block", "", do_info_block, |
9dc39cba | 521 | "", "show the block devices" }, |
9307c4c1 FB |
522 | { "registers", "", do_info_registers, |
523 | "", "show the cpu registers" }, | |
aa455485 FB |
524 | { "history", "", do_info_history, |
525 | "", "show the command line history", }, | |
4c27ba27 FB |
526 | { "pic", "", pic_info, |
527 | "", "show i8259 (PIC) state", }, | |
9dc39cba FB |
528 | { NULL, NULL, }, |
529 | }; | |
530 | ||
9307c4c1 FB |
531 | /*******************************************************************/ |
532 | ||
533 | static const char *pch; | |
534 | static jmp_buf expr_env; | |
535 | ||
536 | typedef struct MonitorDef { | |
537 | const char *name; | |
538 | int offset; | |
539 | int (*get_value)(struct MonitorDef *md); | |
540 | } MonitorDef; | |
541 | ||
57206fd4 FB |
542 | #if defined(TARGET_I386) |
543 | static int monitor_get_pc (struct MonitorDef *md) | |
544 | { | |
545 | return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base; | |
546 | } | |
547 | #endif | |
548 | ||
a541f297 FB |
549 | #if defined(TARGET_PPC) |
550 | static int monitor_get_ccr (struct MonitorDef *md) | |
551 | { | |
552 | unsigned int u; | |
553 | int i; | |
554 | ||
555 | u = 0; | |
556 | for (i = 0; i < 8; i++) | |
557 | u |= cpu_single_env->crf[i] << (32 - (4 * i)); | |
558 | ||
559 | return u; | |
560 | } | |
561 | ||
562 | static int monitor_get_msr (struct MonitorDef *md) | |
563 | { | |
564 | return (cpu_single_env->msr[MSR_POW] << MSR_POW) | | |
565 | (cpu_single_env->msr[MSR_ILE] << MSR_ILE) | | |
566 | (cpu_single_env->msr[MSR_EE] << MSR_EE) | | |
567 | (cpu_single_env->msr[MSR_PR] << MSR_PR) | | |
568 | (cpu_single_env->msr[MSR_FP] << MSR_FP) | | |
569 | (cpu_single_env->msr[MSR_ME] << MSR_ME) | | |
570 | (cpu_single_env->msr[MSR_FE0] << MSR_FE0) | | |
571 | (cpu_single_env->msr[MSR_SE] << MSR_SE) | | |
572 | (cpu_single_env->msr[MSR_BE] << MSR_BE) | | |
573 | (cpu_single_env->msr[MSR_FE1] << MSR_FE1) | | |
574 | (cpu_single_env->msr[MSR_IP] << MSR_IP) | | |
575 | (cpu_single_env->msr[MSR_IR] << MSR_IR) | | |
576 | (cpu_single_env->msr[MSR_DR] << MSR_DR) | | |
577 | (cpu_single_env->msr[MSR_RI] << MSR_RI) | | |
578 | (cpu_single_env->msr[MSR_LE] << MSR_LE); | |
579 | } | |
580 | ||
581 | static int monitor_get_xer (struct MonitorDef *md) | |
582 | { | |
583 | return (cpu_single_env->xer[XER_SO] << XER_SO) | | |
584 | (cpu_single_env->xer[XER_OV] << XER_OV) | | |
585 | (cpu_single_env->xer[XER_CA] << XER_CA) | | |
586 | (cpu_single_env->xer[XER_BC] << XER_BC); | |
587 | } | |
588 | #endif | |
589 | ||
9307c4c1 FB |
590 | static MonitorDef monitor_defs[] = { |
591 | #ifdef TARGET_I386 | |
57206fd4 FB |
592 | |
593 | #define SEG(name, seg) \ | |
594 | { name, offsetof(CPUState, segs[seg].selector) },\ | |
595 | { name ".base", offsetof(CPUState, segs[seg].base) },\ | |
596 | { name ".limit", offsetof(CPUState, segs[seg].limit) }, | |
597 | ||
9307c4c1 FB |
598 | { "eax", offsetof(CPUState, regs[0]) }, |
599 | { "ecx", offsetof(CPUState, regs[1]) }, | |
600 | { "edx", offsetof(CPUState, regs[2]) }, | |
601 | { "ebx", offsetof(CPUState, regs[3]) }, | |
602 | { "esp|sp", offsetof(CPUState, regs[4]) }, | |
603 | { "ebp|fp", offsetof(CPUState, regs[5]) }, | |
604 | { "esi", offsetof(CPUState, regs[6]) }, | |
605 | { "esi", offsetof(CPUState, regs[7]) }, | |
606 | { "eflags", offsetof(CPUState, eflags) }, | |
57206fd4 FB |
607 | { "eip", offsetof(CPUState, eip) }, |
608 | SEG("cs", R_CS) | |
609 | SEG("ds", R_DS) | |
610 | SEG("es", R_ES) | |
611 | SEG("fs", R_FS) | |
612 | SEG("gs", R_GS) | |
613 | { "pc", 0, monitor_get_pc, }, | |
a541f297 FB |
614 | #elif defined(TARGET_PPC) |
615 | { "r0", offsetof(CPUState, gpr[0]) }, | |
616 | { "r1", offsetof(CPUState, gpr[1]) }, | |
617 | { "r2", offsetof(CPUState, gpr[2]) }, | |
618 | { "r3", offsetof(CPUState, gpr[3]) }, | |
619 | { "r4", offsetof(CPUState, gpr[4]) }, | |
620 | { "r5", offsetof(CPUState, gpr[5]) }, | |
621 | { "r6", offsetof(CPUState, gpr[6]) }, | |
622 | { "r7", offsetof(CPUState, gpr[7]) }, | |
623 | { "r8", offsetof(CPUState, gpr[8]) }, | |
624 | { "r9", offsetof(CPUState, gpr[9]) }, | |
625 | { "r10", offsetof(CPUState, gpr[10]) }, | |
626 | { "r11", offsetof(CPUState, gpr[11]) }, | |
627 | { "r12", offsetof(CPUState, gpr[12]) }, | |
628 | { "r13", offsetof(CPUState, gpr[13]) }, | |
629 | { "r14", offsetof(CPUState, gpr[14]) }, | |
630 | { "r15", offsetof(CPUState, gpr[15]) }, | |
631 | { "r16", offsetof(CPUState, gpr[16]) }, | |
632 | { "r17", offsetof(CPUState, gpr[17]) }, | |
633 | { "r18", offsetof(CPUState, gpr[18]) }, | |
634 | { "r19", offsetof(CPUState, gpr[19]) }, | |
635 | { "r20", offsetof(CPUState, gpr[20]) }, | |
636 | { "r21", offsetof(CPUState, gpr[21]) }, | |
637 | { "r22", offsetof(CPUState, gpr[22]) }, | |
638 | { "r23", offsetof(CPUState, gpr[23]) }, | |
639 | { "r24", offsetof(CPUState, gpr[24]) }, | |
640 | { "r25", offsetof(CPUState, gpr[25]) }, | |
641 | { "r26", offsetof(CPUState, gpr[26]) }, | |
642 | { "r27", offsetof(CPUState, gpr[27]) }, | |
643 | { "r28", offsetof(CPUState, gpr[28]) }, | |
644 | { "r29", offsetof(CPUState, gpr[29]) }, | |
645 | { "r30", offsetof(CPUState, gpr[30]) }, | |
646 | { "r31", offsetof(CPUState, gpr[31]) }, | |
57206fd4 | 647 | { "nip|pc", offsetof(CPUState, nip) }, |
a541f297 FB |
648 | { "lr", offsetof(CPUState, lr) }, |
649 | { "ctr", offsetof(CPUState, ctr) }, | |
650 | { "decr", offsetof(CPUState, decr) }, | |
651 | { "ccr", 0, &monitor_get_ccr, }, | |
652 | { "msr", 0, &monitor_get_msr, }, | |
653 | { "xer", 0, &monitor_get_xer, }, | |
654 | { "tbu", offsetof(CPUState, tb[0]) }, | |
655 | { "tbl", offsetof(CPUState, tb[1]) }, | |
656 | { "sdr1", offsetof(CPUState, sdr1) }, | |
657 | { "sr0", offsetof(CPUState, sr[0]) }, | |
658 | { "sr1", offsetof(CPUState, sr[1]) }, | |
659 | { "sr2", offsetof(CPUState, sr[2]) }, | |
660 | { "sr3", offsetof(CPUState, sr[3]) }, | |
661 | { "sr4", offsetof(CPUState, sr[4]) }, | |
662 | { "sr5", offsetof(CPUState, sr[5]) }, | |
663 | { "sr6", offsetof(CPUState, sr[6]) }, | |
664 | { "sr7", offsetof(CPUState, sr[7]) }, | |
665 | { "sr8", offsetof(CPUState, sr[8]) }, | |
666 | { "sr9", offsetof(CPUState, sr[9]) }, | |
667 | { "sr10", offsetof(CPUState, sr[10]) }, | |
668 | { "sr11", offsetof(CPUState, sr[11]) }, | |
669 | { "sr12", offsetof(CPUState, sr[12]) }, | |
670 | { "sr13", offsetof(CPUState, sr[13]) }, | |
671 | { "sr14", offsetof(CPUState, sr[14]) }, | |
672 | { "sr15", offsetof(CPUState, sr[15]) }, | |
673 | /* Too lazy to put BATs and SPRs ... */ | |
9307c4c1 FB |
674 | #endif |
675 | { NULL }, | |
676 | }; | |
677 | ||
678 | static void expr_error(const char *fmt) | |
9dc39cba | 679 | { |
9307c4c1 FB |
680 | term_printf(fmt); |
681 | term_printf("\n"); | |
682 | longjmp(expr_env, 1); | |
683 | } | |
684 | ||
685 | static int get_monitor_def(int *pval, const char *name) | |
686 | { | |
687 | MonitorDef *md; | |
688 | for(md = monitor_defs; md->name != NULL; md++) { | |
689 | if (compare_cmd(name, md->name)) { | |
690 | if (md->get_value) { | |
691 | *pval = md->get_value(md); | |
692 | } else { | |
693 | *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset); | |
694 | } | |
695 | return 0; | |
696 | } | |
697 | } | |
698 | return -1; | |
699 | } | |
700 | ||
701 | static void next(void) | |
702 | { | |
703 | if (pch != '\0') { | |
704 | pch++; | |
705 | while (isspace(*pch)) | |
706 | pch++; | |
707 | } | |
708 | } | |
709 | ||
710 | static int expr_sum(void); | |
711 | ||
712 | static int expr_unary(void) | |
713 | { | |
714 | int n; | |
715 | char *p; | |
716 | ||
717 | switch(*pch) { | |
718 | case '+': | |
719 | next(); | |
720 | n = expr_unary(); | |
721 | break; | |
722 | case '-': | |
723 | next(); | |
724 | n = -expr_unary(); | |
725 | break; | |
726 | case '~': | |
727 | next(); | |
728 | n = ~expr_unary(); | |
729 | break; | |
730 | case '(': | |
731 | next(); | |
732 | n = expr_sum(); | |
733 | if (*pch != ')') { | |
734 | expr_error("')' expected"); | |
735 | } | |
736 | next(); | |
737 | break; | |
738 | case '$': | |
739 | { | |
740 | char buf[128], *q; | |
741 | ||
742 | pch++; | |
743 | q = buf; | |
744 | while ((*pch >= 'a' && *pch <= 'z') || | |
745 | (*pch >= 'A' && *pch <= 'Z') || | |
746 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 747 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
748 | if ((q - buf) < sizeof(buf) - 1) |
749 | *q++ = *pch; | |
750 | pch++; | |
751 | } | |
752 | while (isspace(*pch)) | |
753 | pch++; | |
754 | *q = 0; | |
755 | if (get_monitor_def(&n, buf)) | |
756 | expr_error("unknown register"); | |
757 | } | |
758 | break; | |
759 | case '\0': | |
760 | expr_error("unexpected end of expression"); | |
761 | n = 0; | |
762 | break; | |
763 | default: | |
764 | n = strtoul(pch, &p, 0); | |
765 | if (pch == p) { | |
766 | expr_error("invalid char in expression"); | |
767 | } | |
768 | pch = p; | |
769 | while (isspace(*pch)) | |
770 | pch++; | |
771 | break; | |
772 | } | |
773 | return n; | |
774 | } | |
775 | ||
776 | ||
777 | static int expr_prod(void) | |
778 | { | |
779 | int val, val2, op; | |
780 | ||
781 | val = expr_unary(); | |
782 | for(;;) { | |
783 | op = *pch; | |
784 | if (op != '*' && op != '/' && op != '%') | |
785 | break; | |
786 | next(); | |
787 | val2 = expr_unary(); | |
788 | switch(op) { | |
789 | default: | |
790 | case '*': | |
791 | val *= val2; | |
792 | break; | |
793 | case '/': | |
794 | case '%': | |
795 | if (val2 == 0) | |
796 | expr_error("divison by zero"); | |
797 | if (op == '/') | |
798 | val /= val2; | |
799 | else | |
800 | val %= val2; | |
801 | break; | |
802 | } | |
803 | } | |
804 | return val; | |
805 | } | |
806 | ||
807 | static int expr_logic(void) | |
808 | { | |
809 | int val, val2, op; | |
810 | ||
811 | val = expr_prod(); | |
812 | for(;;) { | |
813 | op = *pch; | |
814 | if (op != '&' && op != '|' && op != '^') | |
815 | break; | |
816 | next(); | |
817 | val2 = expr_prod(); | |
818 | switch(op) { | |
819 | default: | |
820 | case '&': | |
821 | val &= val2; | |
822 | break; | |
823 | case '|': | |
824 | val |= val2; | |
825 | break; | |
826 | case '^': | |
827 | val ^= val2; | |
828 | break; | |
829 | } | |
830 | } | |
831 | return val; | |
832 | } | |
833 | ||
834 | static int expr_sum(void) | |
835 | { | |
836 | int val, val2, op; | |
837 | ||
838 | val = expr_logic(); | |
839 | for(;;) { | |
840 | op = *pch; | |
841 | if (op != '+' && op != '-') | |
842 | break; | |
843 | next(); | |
844 | val2 = expr_logic(); | |
845 | if (op == '+') | |
846 | val += val2; | |
847 | else | |
848 | val -= val2; | |
849 | } | |
850 | return val; | |
851 | } | |
852 | ||
853 | static int get_expr(int *pval, const char **pp) | |
854 | { | |
855 | pch = *pp; | |
856 | if (setjmp(expr_env)) { | |
857 | *pp = pch; | |
858 | return -1; | |
859 | } | |
860 | while (isspace(*pch)) | |
861 | pch++; | |
862 | *pval = expr_sum(); | |
863 | *pp = pch; | |
864 | return 0; | |
865 | } | |
866 | ||
867 | static int get_str(char *buf, int buf_size, const char **pp) | |
868 | { | |
869 | const char *p; | |
870 | char *q; | |
871 | int c; | |
872 | ||
873 | p = *pp; | |
874 | while (isspace(*p)) | |
875 | p++; | |
876 | if (*p == '\0') { | |
877 | fail: | |
878 | *pp = p; | |
879 | return -1; | |
880 | } | |
881 | q = buf; | |
882 | if (*p == '\"') { | |
883 | p++; | |
884 | while (*p != '\0' && *p != '\"') { | |
885 | if (*p == '\\') { | |
886 | p++; | |
887 | c = *p++; | |
888 | switch(c) { | |
889 | case 'n': | |
890 | c = '\n'; | |
891 | break; | |
892 | case 'r': | |
893 | c = '\r'; | |
894 | break; | |
895 | case '\\': | |
896 | case '\'': | |
897 | case '\"': | |
898 | break; | |
899 | default: | |
900 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
901 | goto fail; | |
902 | } | |
903 | if ((q - buf) < buf_size - 1) { | |
904 | *q++ = c; | |
905 | } | |
906 | } else { | |
907 | if ((q - buf) < buf_size - 1) { | |
908 | *q++ = *p; | |
909 | } | |
910 | p++; | |
911 | } | |
912 | } | |
913 | if (*p != '\"') { | |
914 | qemu_printf("untermintated string\n"); | |
915 | goto fail; | |
916 | } | |
917 | p++; | |
918 | } else { | |
919 | while (*p != '\0' && !isspace(*p)) { | |
920 | if ((q - buf) < buf_size - 1) { | |
921 | *q++ = *p; | |
922 | } | |
923 | p++; | |
924 | } | |
925 | *q = '\0'; | |
926 | } | |
927 | *pp = p; | |
928 | return 0; | |
929 | } | |
930 | ||
931 | static int default_fmt_format = 'x'; | |
932 | static int default_fmt_size = 4; | |
933 | ||
934 | #define MAX_ARGS 16 | |
935 | ||
936 | static void term_handle_command(const char *cmdline) | |
937 | { | |
938 | const char *p, *pstart, *typestr; | |
939 | char *q; | |
940 | int c, nb_args, len, i, has_arg; | |
9dc39cba | 941 | term_cmd_t *cmd; |
9307c4c1 FB |
942 | char cmdname[256]; |
943 | char buf[1024]; | |
944 | void *str_allocated[MAX_ARGS]; | |
945 | void *args[MAX_ARGS]; | |
9dc39cba FB |
946 | |
947 | #ifdef DEBUG | |
948 | term_printf("command='%s'\n", cmdline); | |
949 | #endif | |
950 | ||
9307c4c1 | 951 | /* extract the command name */ |
9dc39cba | 952 | p = cmdline; |
9307c4c1 FB |
953 | q = cmdname; |
954 | while (isspace(*p)) | |
955 | p++; | |
956 | if (*p == '\0') | |
957 | return; | |
958 | pstart = p; | |
959 | while (*p != '\0' && *p != '/' && !isspace(*p)) | |
960 | p++; | |
961 | len = p - pstart; | |
962 | if (len > sizeof(cmdname) - 1) | |
963 | len = sizeof(cmdname) - 1; | |
964 | memcpy(cmdname, pstart, len); | |
965 | cmdname[len] = '\0'; | |
966 | ||
967 | /* find the command */ | |
968 | for(cmd = term_cmds; cmd->name != NULL; cmd++) { | |
969 | if (compare_cmd(cmdname, cmd->name)) | |
970 | goto found; | |
971 | } | |
972 | term_printf("unknown command: '%s'\n", cmdname); | |
973 | return; | |
974 | found: | |
975 | ||
976 | for(i = 0; i < MAX_ARGS; i++) | |
977 | str_allocated[i] = NULL; | |
978 | ||
979 | /* parse the parameters */ | |
980 | typestr = cmd->args_type; | |
981 | nb_args = 0; | |
9dc39cba | 982 | for(;;) { |
9307c4c1 FB |
983 | c = *typestr; |
984 | if (c == '\0') | |
9dc39cba | 985 | break; |
9307c4c1 FB |
986 | typestr++; |
987 | switch(c) { | |
988 | case 'F': | |
989 | case 's': | |
990 | { | |
991 | int ret; | |
992 | char *str; | |
993 | ||
994 | while (isspace(*p)) | |
995 | p++; | |
996 | if (*typestr == '?') { | |
997 | typestr++; | |
998 | if (*p == '\0') { | |
999 | /* no optional string: NULL argument */ | |
1000 | str = NULL; | |
1001 | goto add_str; | |
1002 | } | |
1003 | } | |
1004 | ret = get_str(buf, sizeof(buf), &p); | |
1005 | if (ret < 0) { | |
1006 | if (c == 'F') | |
1007 | term_printf("%s: filename expected\n", cmdname); | |
1008 | else | |
1009 | term_printf("%s: string expected\n", cmdname); | |
1010 | goto fail; | |
1011 | } | |
1012 | str = qemu_malloc(strlen(buf) + 1); | |
1013 | strcpy(str, buf); | |
1014 | str_allocated[nb_args] = str; | |
1015 | add_str: | |
1016 | if (nb_args >= MAX_ARGS) { | |
1017 | error_args: | |
1018 | term_printf("%s: too many arguments\n", cmdname); | |
1019 | goto fail; | |
1020 | } | |
1021 | args[nb_args++] = str; | |
1022 | } | |
9dc39cba | 1023 | break; |
9307c4c1 FB |
1024 | case '/': |
1025 | { | |
1026 | int count, format, size; | |
1027 | ||
1028 | while (isspace(*p)) | |
1029 | p++; | |
1030 | if (*p == '/') { | |
1031 | /* format found */ | |
1032 | p++; | |
1033 | count = 1; | |
1034 | if (isdigit(*p)) { | |
1035 | count = 0; | |
1036 | while (isdigit(*p)) { | |
1037 | count = count * 10 + (*p - '0'); | |
1038 | p++; | |
1039 | } | |
1040 | } | |
1041 | size = -1; | |
1042 | format = -1; | |
1043 | for(;;) { | |
1044 | switch(*p) { | |
1045 | case 'o': | |
1046 | case 'd': | |
1047 | case 'u': | |
1048 | case 'x': | |
1049 | case 'i': | |
1050 | case 'c': | |
1051 | format = *p++; | |
1052 | break; | |
1053 | case 'b': | |
1054 | size = 1; | |
1055 | p++; | |
1056 | break; | |
1057 | case 'h': | |
1058 | size = 2; | |
1059 | p++; | |
1060 | break; | |
1061 | case 'w': | |
1062 | size = 4; | |
1063 | p++; | |
1064 | break; | |
1065 | case 'g': | |
1066 | case 'L': | |
1067 | size = 8; | |
1068 | p++; | |
1069 | break; | |
1070 | default: | |
1071 | goto next; | |
1072 | } | |
1073 | } | |
1074 | next: | |
1075 | if (*p != '\0' && !isspace(*p)) { | |
1076 | term_printf("invalid char in format: '%c'\n", *p); | |
1077 | goto fail; | |
1078 | } | |
9307c4c1 FB |
1079 | if (format < 0) |
1080 | format = default_fmt_format; | |
4c27ba27 FB |
1081 | if (format != 'i') { |
1082 | /* for 'i', not specifying a size gives -1 as size */ | |
1083 | if (size < 0) | |
1084 | size = default_fmt_size; | |
1085 | } | |
9307c4c1 FB |
1086 | default_fmt_size = size; |
1087 | default_fmt_format = format; | |
1088 | } else { | |
1089 | count = 1; | |
1090 | format = default_fmt_format; | |
4c27ba27 FB |
1091 | if (format != 'i') { |
1092 | size = default_fmt_size; | |
1093 | } else { | |
1094 | size = -1; | |
1095 | } | |
9307c4c1 FB |
1096 | } |
1097 | if (nb_args + 3 > MAX_ARGS) | |
1098 | goto error_args; | |
1099 | args[nb_args++] = (void*)count; | |
1100 | args[nb_args++] = (void*)format; | |
1101 | args[nb_args++] = (void*)size; | |
1102 | } | |
9dc39cba | 1103 | break; |
9307c4c1 FB |
1104 | case 'i': |
1105 | { | |
1106 | int val; | |
1107 | while (isspace(*p)) | |
1108 | p++; | |
1109 | if (*typestr == '?') { | |
1110 | typestr++; | |
1111 | if (*p == '\0') | |
1112 | has_arg = 0; | |
1113 | else | |
1114 | has_arg = 1; | |
1115 | if (nb_args >= MAX_ARGS) | |
1116 | goto error_args; | |
1117 | args[nb_args++] = (void *)has_arg; | |
1118 | if (!has_arg) { | |
1119 | if (nb_args >= MAX_ARGS) | |
1120 | goto error_args; | |
1121 | val = -1; | |
1122 | goto add_num; | |
1123 | } | |
1124 | } | |
1125 | if (get_expr(&val, &p)) | |
1126 | goto fail; | |
1127 | add_num: | |
1128 | if (nb_args >= MAX_ARGS) | |
1129 | goto error_args; | |
1130 | args[nb_args++] = (void *)val; | |
1131 | } | |
1132 | break; | |
1133 | case '-': | |
1134 | { | |
1135 | int has_option; | |
1136 | /* option */ | |
1137 | ||
1138 | c = *typestr++; | |
1139 | if (c == '\0') | |
1140 | goto bad_type; | |
1141 | while (isspace(*p)) | |
1142 | p++; | |
1143 | has_option = 0; | |
1144 | if (*p == '-') { | |
1145 | p++; | |
1146 | if (*p != c) { | |
1147 | term_printf("%s: unsupported option -%c\n", | |
1148 | cmdname, *p); | |
1149 | goto fail; | |
1150 | } | |
1151 | p++; | |
1152 | has_option = 1; | |
1153 | } | |
1154 | if (nb_args >= MAX_ARGS) | |
1155 | goto error_args; | |
1156 | args[nb_args++] = (void *)has_option; | |
1157 | } | |
1158 | break; | |
1159 | default: | |
1160 | bad_type: | |
1161 | term_printf("%s: unknown type '%c'\n", cmdname, c); | |
1162 | goto fail; | |
1163 | } | |
9dc39cba | 1164 | } |
9307c4c1 FB |
1165 | /* check that all arguments were parsed */ |
1166 | while (isspace(*p)) | |
1167 | p++; | |
1168 | if (*p != '\0') { | |
1169 | term_printf("%s: extraneous characters at the end of line\n", | |
1170 | cmdname); | |
1171 | goto fail; | |
9dc39cba | 1172 | } |
9307c4c1 FB |
1173 | |
1174 | switch(nb_args) { | |
1175 | case 0: | |
1176 | cmd->handler(); | |
1177 | break; | |
1178 | case 1: | |
1179 | cmd->handler(args[0]); | |
1180 | break; | |
1181 | case 2: | |
1182 | cmd->handler(args[0], args[1]); | |
1183 | break; | |
1184 | case 3: | |
1185 | cmd->handler(args[0], args[1], args[2]); | |
1186 | break; | |
1187 | case 4: | |
1188 | cmd->handler(args[0], args[1], args[2], args[3]); | |
1189 | break; | |
1190 | case 5: | |
1191 | cmd->handler(args[0], args[1], args[2], args[3], args[4]); | |
1192 | break; | |
1193 | default: | |
1194 | term_printf("unsupported number of arguments: %d\n", nb_args); | |
1195 | goto fail; | |
9dc39cba | 1196 | } |
9307c4c1 FB |
1197 | fail: |
1198 | for(i = 0; i < MAX_ARGS; i++) | |
1199 | qemu_free(str_allocated[i]); | |
9dc39cba | 1200 | return; |
9dc39cba FB |
1201 | } |
1202 | ||
1203 | static void term_show_prompt(void) | |
1204 | { | |
1205 | term_printf("(qemu) "); | |
1206 | fflush(stdout); | |
1207 | term_cmd_buf_index = 0; | |
1208 | term_cmd_buf_size = 0; | |
1209 | term_esc_state = IS_NORM; | |
1210 | } | |
1211 | ||
aa455485 FB |
1212 | static void term_print_cmdline (const char *cmdline) |
1213 | { | |
1214 | term_show_prompt(); | |
1215 | term_printf(cmdline); | |
1216 | term_flush(); | |
1217 | } | |
1218 | ||
9dc39cba FB |
1219 | static void term_insert_char(int ch) |
1220 | { | |
1221 | if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) { | |
1222 | memmove(term_cmd_buf + term_cmd_buf_index + 1, | |
1223 | term_cmd_buf + term_cmd_buf_index, | |
1224 | term_cmd_buf_size - term_cmd_buf_index); | |
1225 | term_cmd_buf[term_cmd_buf_index] = ch; | |
1226 | term_cmd_buf_size++; | |
1227 | term_printf("\033[@%c", ch); | |
1228 | term_cmd_buf_index++; | |
1229 | term_flush(); | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | static void term_backward_char(void) | |
1234 | { | |
1235 | if (term_cmd_buf_index > 0) { | |
1236 | term_cmd_buf_index--; | |
1237 | term_printf("\033[D"); | |
1238 | term_flush(); | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | static void term_forward_char(void) | |
1243 | { | |
1244 | if (term_cmd_buf_index < term_cmd_buf_size) { | |
1245 | term_cmd_buf_index++; | |
1246 | term_printf("\033[C"); | |
1247 | term_flush(); | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | static void term_delete_char(void) | |
1252 | { | |
1253 | if (term_cmd_buf_index < term_cmd_buf_size) { | |
1254 | memmove(term_cmd_buf + term_cmd_buf_index, | |
1255 | term_cmd_buf + term_cmd_buf_index + 1, | |
1256 | term_cmd_buf_size - term_cmd_buf_index - 1); | |
1257 | term_printf("\033[P"); | |
1258 | term_cmd_buf_size--; | |
1259 | term_flush(); | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | static void term_backspace(void) | |
1264 | { | |
1265 | if (term_cmd_buf_index > 0) { | |
1266 | term_backward_char(); | |
1267 | term_delete_char(); | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | static void term_bol(void) | |
1272 | { | |
1273 | while (term_cmd_buf_index > 0) | |
1274 | term_backward_char(); | |
1275 | } | |
1276 | ||
1277 | static void term_eol(void) | |
1278 | { | |
1279 | while (term_cmd_buf_index < term_cmd_buf_size) | |
1280 | term_forward_char(); | |
1281 | } | |
1282 | ||
aa455485 FB |
1283 | static void term_up_char(void) |
1284 | { | |
1285 | int idx; | |
1286 | ||
1287 | if (term_hist_entry == 0) | |
1288 | return; | |
1289 | if (term_hist_entry == -1) { | |
1290 | /* Find latest entry */ | |
1291 | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { | |
1292 | if (term_history[idx] == NULL) | |
1293 | break; | |
1294 | } | |
1295 | term_hist_entry = idx; | |
1296 | } | |
1297 | term_hist_entry--; | |
1298 | if (term_hist_entry >= 0) { | |
1299 | strcpy(term_cmd_buf, term_history[term_hist_entry]); | |
1300 | term_printf("\n"); | |
1301 | term_print_cmdline(term_cmd_buf); | |
1302 | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | static void term_down_char(void) | |
1307 | { | |
1308 | if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1) | |
1309 | return; | |
1310 | if (term_history[++term_hist_entry] != NULL) { | |
1311 | strcpy(term_cmd_buf, term_history[term_hist_entry]); | |
1312 | } else { | |
1313 | term_hist_entry = -1; | |
1314 | } | |
1315 | term_printf("\n"); | |
1316 | term_print_cmdline(term_cmd_buf); | |
1317 | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); | |
1318 | } | |
1319 | ||
1320 | static void term_hist_add(const char *cmdline) | |
1321 | { | |
1322 | char *hist_entry, *new_entry; | |
1323 | int idx; | |
1324 | ||
1325 | if (cmdline[0] == '\0') | |
1326 | return; | |
1327 | new_entry = NULL; | |
1328 | if (term_hist_entry != -1) { | |
1329 | /* We were editing an existing history entry: replace it */ | |
1330 | hist_entry = term_history[term_hist_entry]; | |
1331 | idx = term_hist_entry; | |
1332 | if (strcmp(hist_entry, cmdline) == 0) { | |
1333 | goto same_entry; | |
1334 | } | |
1335 | } | |
1336 | /* Search cmdline in history buffers */ | |
1337 | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { | |
1338 | hist_entry = term_history[idx]; | |
1339 | if (hist_entry == NULL) | |
1340 | break; | |
1341 | if (strcmp(hist_entry, cmdline) == 0) { | |
1342 | same_entry: | |
1343 | new_entry = hist_entry; | |
1344 | /* Put this entry at the end of history */ | |
1345 | memmove(&term_history[idx], &term_history[idx + 1], | |
1346 | &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]); | |
1347 | term_history[TERM_MAX_CMDS - 1] = NULL; | |
1348 | for (; idx < TERM_MAX_CMDS; idx++) { | |
1349 | if (term_history[idx] == NULL) | |
1350 | break; | |
1351 | } | |
1352 | break; | |
1353 | } | |
1354 | } | |
1355 | if (idx == TERM_MAX_CMDS) { | |
1356 | /* Need to get one free slot */ | |
1357 | free(term_history[0]); | |
1358 | memcpy(term_history, &term_history[1], | |
1359 | &term_history[TERM_MAX_CMDS] - &term_history[1]); | |
1360 | term_history[TERM_MAX_CMDS - 1] = NULL; | |
1361 | idx = TERM_MAX_CMDS - 1; | |
1362 | } | |
1363 | if (new_entry == NULL) | |
1364 | new_entry = strdup(cmdline); | |
1365 | term_history[idx] = new_entry; | |
1366 | term_hist_entry = -1; | |
1367 | } | |
1368 | ||
9dc39cba FB |
1369 | /* return true if command handled */ |
1370 | static void term_handle_byte(int ch) | |
1371 | { | |
1372 | switch(term_esc_state) { | |
1373 | case IS_NORM: | |
1374 | switch(ch) { | |
1375 | case 1: | |
1376 | term_bol(); | |
1377 | break; | |
1378 | case 5: | |
1379 | term_eol(); | |
1380 | break; | |
1381 | case 10: | |
1382 | case 13: | |
1383 | term_cmd_buf[term_cmd_buf_size] = '\0'; | |
aa455485 | 1384 | term_hist_add(term_cmd_buf); |
9dc39cba FB |
1385 | term_printf("\n"); |
1386 | term_handle_command(term_cmd_buf); | |
1387 | term_show_prompt(); | |
1388 | break; | |
1389 | case 27: | |
1390 | term_esc_state = IS_ESC; | |
1391 | break; | |
1392 | case 127: | |
1393 | case 8: | |
1394 | term_backspace(); | |
1395 | break; | |
aa455485 FB |
1396 | case 155: |
1397 | term_esc_state = IS_CSI; | |
1398 | break; | |
9dc39cba FB |
1399 | default: |
1400 | if (ch >= 32) { | |
1401 | term_insert_char(ch); | |
1402 | } | |
1403 | break; | |
1404 | } | |
1405 | break; | |
1406 | case IS_ESC: | |
1407 | if (ch == '[') { | |
1408 | term_esc_state = IS_CSI; | |
1409 | term_esc_param = 0; | |
1410 | } else { | |
1411 | term_esc_state = IS_NORM; | |
1412 | } | |
1413 | break; | |
1414 | case IS_CSI: | |
1415 | switch(ch) { | |
aa455485 FB |
1416 | case 'A': |
1417 | case 'F': | |
1418 | term_up_char(); | |
1419 | break; | |
1420 | case 'B': | |
1421 | case 'E': | |
1422 | term_down_char(); | |
1423 | break; | |
9dc39cba FB |
1424 | case 'D': |
1425 | term_backward_char(); | |
1426 | break; | |
1427 | case 'C': | |
1428 | term_forward_char(); | |
1429 | break; | |
1430 | case '0' ... '9': | |
1431 | term_esc_param = term_esc_param * 10 + (ch - '0'); | |
1432 | goto the_end; | |
1433 | case '~': | |
1434 | switch(term_esc_param) { | |
1435 | case 1: | |
1436 | term_bol(); | |
1437 | break; | |
1438 | case 3: | |
1439 | term_delete_char(); | |
1440 | break; | |
1441 | case 4: | |
1442 | term_eol(); | |
1443 | break; | |
1444 | } | |
1445 | break; | |
1446 | default: | |
1447 | break; | |
1448 | } | |
1449 | term_esc_state = IS_NORM; | |
1450 | the_end: | |
1451 | break; | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | /*************************************************************/ | |
1456 | /* serial console support */ | |
1457 | ||
1458 | #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ | |
1459 | ||
1460 | static int term_got_escape, term_command; | |
1461 | ||
1462 | void term_print_help(void) | |
1463 | { | |
1464 | term_printf("\n" | |
1465 | "C-a h print this help\n" | |
1466 | "C-a x exit emulatior\n" | |
9dc39cba FB |
1467 | "C-a s save disk data back to file (if -snapshot)\n" |
1468 | "C-a b send break (magic sysrq)\n" | |
1469 | "C-a c switch between console and monitor\n" | |
1470 | "C-a C-a send C-a\n" | |
1471 | ); | |
1472 | } | |
1473 | ||
1474 | /* called when a char is received */ | |
1475 | static void term_received_byte(int ch) | |
1476 | { | |
1477 | if (!serial_console) { | |
1478 | /* if no serial console, handle every command */ | |
1479 | term_handle_byte(ch); | |
1480 | } else { | |
1481 | if (term_got_escape) { | |
1482 | term_got_escape = 0; | |
1483 | switch(ch) { | |
1484 | case 'h': | |
1485 | term_print_help(); | |
1486 | break; | |
1487 | case 'x': | |
1488 | exit(0); | |
1489 | break; | |
1490 | case 's': | |
1491 | { | |
1492 | int i; | |
1493 | for (i = 0; i < MAX_DISKS; i++) { | |
1494 | if (bs_table[i]) | |
1495 | bdrv_commit(bs_table[i]); | |
1496 | } | |
1497 | } | |
1498 | break; | |
1499 | case 'b': | |
1500 | if (serial_console) | |
1501 | serial_receive_break(serial_console); | |
1502 | break; | |
1503 | case 'c': | |
1504 | if (!term_command) { | |
1505 | term_show_prompt(); | |
1506 | term_command = 1; | |
1507 | } else { | |
1508 | term_command = 0; | |
1509 | } | |
1510 | break; | |
9dc39cba FB |
1511 | case TERM_ESCAPE: |
1512 | goto send_char; | |
1513 | } | |
1514 | } else if (ch == TERM_ESCAPE) { | |
1515 | term_got_escape = 1; | |
1516 | } else { | |
1517 | send_char: | |
1518 | if (term_command) { | |
1519 | term_handle_byte(ch); | |
1520 | } else { | |
1521 | if (serial_console) | |
1522 | serial_receive_byte(serial_console, ch); | |
1523 | } | |
1524 | } | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | static int term_can_read(void *opaque) | |
1529 | { | |
1530 | if (serial_console) { | |
1531 | return serial_can_receive(serial_console); | |
1532 | } else { | |
f193c797 | 1533 | return 128; |
9dc39cba FB |
1534 | } |
1535 | } | |
1536 | ||
1537 | static void term_read(void *opaque, const uint8_t *buf, int size) | |
1538 | { | |
1539 | int i; | |
1540 | for(i = 0; i < size; i++) | |
1541 | term_received_byte(buf[i]); | |
1542 | } | |
1543 | ||
1544 | void monitor_init(void) | |
1545 | { | |
1546 | if (!serial_console) { | |
1547 | term_printf("QEMU %s monitor - type 'help' for more information\n", | |
1548 | QEMU_VERSION); | |
1549 | term_show_prompt(); | |
1550 | } | |
aa455485 | 1551 | term_hist_entry = -1; |
8a7ddc38 | 1552 | qemu_add_fd_read_handler(0, term_can_read, term_read, NULL); |
9dc39cba | 1553 | } |