]>
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 | ||
a3a91a35 FB |
479 | typedef struct { |
480 | int keycode; | |
481 | const char *name; | |
482 | } KeyDef; | |
483 | ||
484 | static const KeyDef key_defs[] = { | |
485 | { 0x2a, "shift" }, | |
486 | { 0x36, "shift_r" }, | |
487 | ||
488 | { 0x38, "alt" }, | |
489 | { 0xb8, "alt_r" }, | |
490 | { 0x1d, "ctrl" }, | |
491 | { 0x9d, "ctrl_r" }, | |
492 | ||
493 | { 0xdd, "menu" }, | |
494 | ||
495 | { 0x01, "esc" }, | |
496 | ||
497 | { 0x02, "1" }, | |
498 | { 0x03, "2" }, | |
499 | { 0x04, "3" }, | |
500 | { 0x05, "4" }, | |
501 | { 0x06, "5" }, | |
502 | { 0x07, "6" }, | |
503 | { 0x08, "7" }, | |
504 | { 0x09, "8" }, | |
505 | { 0x0a, "9" }, | |
506 | { 0x0b, "0" }, | |
507 | { 0x0e, "backspace" }, | |
508 | ||
509 | { 0x0f, "tab" }, | |
510 | { 0x10, "q" }, | |
511 | { 0x11, "w" }, | |
512 | { 0x12, "e" }, | |
513 | { 0x13, "r" }, | |
514 | { 0x14, "t" }, | |
515 | { 0x15, "y" }, | |
516 | { 0x16, "u" }, | |
517 | { 0x17, "i" }, | |
518 | { 0x18, "o" }, | |
519 | { 0x19, "p" }, | |
520 | ||
521 | { 0x1c, "ret" }, | |
522 | ||
523 | { 0x1e, "a" }, | |
524 | { 0x1f, "s" }, | |
525 | { 0x20, "d" }, | |
526 | { 0x21, "f" }, | |
527 | { 0x22, "g" }, | |
528 | { 0x23, "h" }, | |
529 | { 0x24, "j" }, | |
530 | { 0x25, "k" }, | |
531 | { 0x26, "l" }, | |
532 | ||
533 | { 0x2c, "z" }, | |
534 | { 0x2d, "x" }, | |
535 | { 0x2e, "c" }, | |
536 | { 0x2f, "v" }, | |
537 | { 0x30, "b" }, | |
538 | { 0x31, "n" }, | |
539 | { 0x32, "m" }, | |
540 | ||
541 | { 0x39, "spc" }, | |
00ffa62a | 542 | { 0x3a, "caps_lock" }, |
a3a91a35 FB |
543 | { 0x3b, "f1" }, |
544 | { 0x3c, "f2" }, | |
545 | { 0x3d, "f3" }, | |
546 | { 0x3e, "f4" }, | |
547 | { 0x3f, "f5" }, | |
548 | { 0x40, "f6" }, | |
549 | { 0x41, "f7" }, | |
550 | { 0x42, "f8" }, | |
551 | { 0x43, "f9" }, | |
552 | { 0x44, "f10" }, | |
00ffa62a | 553 | { 0x45, "num_lock" }, |
a3a91a35 FB |
554 | { 0x46, "scroll_lock" }, |
555 | ||
556 | { 0x56, "<" }, | |
557 | ||
558 | { 0x57, "f11" }, | |
559 | { 0x58, "f12" }, | |
560 | ||
561 | { 0xb7, "print" }, | |
562 | ||
563 | { 0xc7, "home" }, | |
564 | { 0xc9, "pgup" }, | |
565 | { 0xd1, "pgdn" }, | |
566 | { 0xcf, "end" }, | |
567 | ||
568 | { 0xcb, "left" }, | |
569 | { 0xc8, "up" }, | |
570 | { 0xd0, "down" }, | |
571 | { 0xcd, "right" }, | |
572 | ||
573 | { 0xd2, "insert" }, | |
574 | { 0xd3, "delete" }, | |
575 | { 0, NULL }, | |
576 | }; | |
577 | ||
578 | static int get_keycode(const char *key) | |
579 | { | |
580 | const KeyDef *p; | |
581 | ||
582 | for(p = key_defs; p->name != NULL; p++) { | |
583 | if (!strcmp(key, p->name)) | |
584 | return p->keycode; | |
585 | } | |
586 | return -1; | |
587 | } | |
588 | ||
589 | static void do_send_key(const char *string) | |
590 | { | |
591 | char keybuf[16], *q; | |
592 | uint8_t keycodes[16]; | |
593 | const char *p; | |
594 | int nb_keycodes, keycode, i; | |
595 | ||
596 | nb_keycodes = 0; | |
597 | p = string; | |
598 | while (*p != '\0') { | |
599 | q = keybuf; | |
600 | while (*p != '\0' && *p != '-') { | |
601 | if ((q - keybuf) < sizeof(keybuf) - 1) { | |
602 | *q++ = *p; | |
603 | } | |
604 | p++; | |
605 | } | |
606 | *q = '\0'; | |
607 | keycode = get_keycode(keybuf); | |
608 | if (keycode < 0) { | |
609 | term_printf("unknown key: '%s'\n", keybuf); | |
610 | return; | |
611 | } | |
612 | keycodes[nb_keycodes++] = keycode; | |
613 | if (*p == '\0') | |
614 | break; | |
615 | p++; | |
616 | } | |
617 | /* key down events */ | |
618 | for(i = 0; i < nb_keycodes; i++) { | |
619 | keycode = keycodes[i]; | |
620 | if (keycode & 0x80) | |
621 | kbd_put_keycode(0xe0); | |
622 | kbd_put_keycode(keycode & 0x7f); | |
623 | } | |
624 | /* key up events */ | |
625 | for(i = nb_keycodes - 1; i >= 0; i--) { | |
626 | keycode = keycodes[i]; | |
627 | if (keycode & 0x80) | |
628 | kbd_put_keycode(0xe0); | |
629 | kbd_put_keycode(keycode | 0x80); | |
630 | } | |
631 | } | |
632 | ||
3440557b FB |
633 | static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index) |
634 | { | |
635 | uint32_t val; | |
636 | int suffix; | |
637 | ||
638 | if (has_index) { | |
639 | cpu_outb(NULL, addr & 0xffff, index & 0xff); | |
640 | addr++; | |
641 | } | |
642 | addr &= 0xffff; | |
643 | ||
644 | switch(size) { | |
645 | default: | |
646 | case 1: | |
647 | val = cpu_inb(NULL, addr); | |
648 | suffix = 'b'; | |
649 | break; | |
650 | case 2: | |
651 | val = cpu_inw(NULL, addr); | |
652 | suffix = 'w'; | |
653 | break; | |
654 | case 4: | |
655 | val = cpu_inl(NULL, addr); | |
656 | suffix = 'l'; | |
657 | break; | |
658 | } | |
659 | term_printf("port%c[0x%04x] = %#0*x\n", | |
660 | suffix, addr, size * 2, val); | |
661 | } | |
a3a91a35 | 662 | |
9dc39cba | 663 | static term_cmd_t term_cmds[] = { |
9307c4c1 | 664 | { "help|?", "s?", do_help, |
9dc39cba | 665 | "[cmd]", "show the help" }, |
9307c4c1 | 666 | { "commit", "", do_commit, |
9dc39cba | 667 | "", "commit changes to the disk images (if -snapshot is used)" }, |
9307c4c1 | 668 | { "info", "s?", do_info, |
9dc39cba | 669 | "subcommand", "show various information about the system state" }, |
9307c4c1 | 670 | { "q|quit", "", do_quit, |
9dc39cba | 671 | "", "quit the emulator" }, |
9307c4c1 | 672 | { "eject", "-fs", do_eject, |
9dc39cba | 673 | "[-f] device", "eject a removable media (use -f to force it)" }, |
9307c4c1 | 674 | { "change", "sF", do_change, |
9dc39cba | 675 | "device filename", "change a removable media" }, |
9307c4c1 | 676 | { "screendump", "F", do_screen_dump, |
59a983b9 | 677 | "filename", "save screen into PPM image 'filename'" }, |
9307c4c1 | 678 | { "log", "s", do_log, |
f193c797 | 679 | "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, |
9307c4c1 | 680 | { "savevm", "F", do_savevm, |
8a7ddc38 | 681 | "filename", "save the whole virtual machine state to 'filename'" }, |
9307c4c1 | 682 | { "loadvm", "F", do_loadvm, |
8a7ddc38 | 683 | "filename", "restore the whole virtual machine state from 'filename'" }, |
9307c4c1 FB |
684 | { "stop", "", do_stop, |
685 | "", "stop emulation", }, | |
686 | { "c|cont", "", do_cont, | |
687 | "", "resume emulation", }, | |
67b915a5 | 688 | #ifdef CONFIG_GDBSTUB |
9307c4c1 FB |
689 | { "gdbserver", "i?", do_gdbserver, |
690 | "[port]", "start gdbserver session (default port=1234)", }, | |
67b915a5 | 691 | #endif |
9307c4c1 FB |
692 | { "x", "/i", do_memory_dump, |
693 | "/fmt addr", "virtual memory dump starting at 'addr'", }, | |
694 | { "xp", "/i", do_physical_memory_dump, | |
695 | "/fmt addr", "physical memory dump starting at 'addr'", }, | |
696 | { "p|print", "/i", do_print, | |
697 | "/fmt expr", "print expression value (use $reg for CPU register access)", }, | |
3440557b FB |
698 | { "i", "/ii.", do_ioport_read, |
699 | "/fmt addr", "I/O port read" }, | |
700 | ||
a3a91a35 FB |
701 | { "sendkey", "s", do_send_key, |
702 | "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" }, | |
f193c797 | 703 | { NULL, NULL, }, |
9dc39cba FB |
704 | }; |
705 | ||
706 | static term_cmd_t info_cmds[] = { | |
9307c4c1 | 707 | { "network", "", do_info_network, |
9dc39cba | 708 | "", "show the network state" }, |
9307c4c1 | 709 | { "block", "", do_info_block, |
9dc39cba | 710 | "", "show the block devices" }, |
9307c4c1 FB |
711 | { "registers", "", do_info_registers, |
712 | "", "show the cpu registers" }, | |
aa455485 FB |
713 | { "history", "", do_info_history, |
714 | "", "show the command line history", }, | |
4a0fb71e FB |
715 | { "irq", "", irq_info, |
716 | "", "show the interrupts statistics (if available)", }, | |
4c27ba27 FB |
717 | { "pic", "", pic_info, |
718 | "", "show i8259 (PIC) state", }, | |
86e0c048 FB |
719 | { "pci", "", pci_info, |
720 | "", "show PCI info", }, | |
9dc39cba FB |
721 | { NULL, NULL, }, |
722 | }; | |
723 | ||
9307c4c1 FB |
724 | /*******************************************************************/ |
725 | ||
726 | static const char *pch; | |
727 | static jmp_buf expr_env; | |
728 | ||
729 | typedef struct MonitorDef { | |
730 | const char *name; | |
731 | int offset; | |
732 | int (*get_value)(struct MonitorDef *md); | |
733 | } MonitorDef; | |
734 | ||
57206fd4 FB |
735 | #if defined(TARGET_I386) |
736 | static int monitor_get_pc (struct MonitorDef *md) | |
737 | { | |
738 | return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base; | |
739 | } | |
740 | #endif | |
741 | ||
a541f297 FB |
742 | #if defined(TARGET_PPC) |
743 | static int monitor_get_ccr (struct MonitorDef *md) | |
744 | { | |
745 | unsigned int u; | |
746 | int i; | |
747 | ||
748 | u = 0; | |
749 | for (i = 0; i < 8; i++) | |
750 | u |= cpu_single_env->crf[i] << (32 - (4 * i)); | |
751 | ||
752 | return u; | |
753 | } | |
754 | ||
755 | static int monitor_get_msr (struct MonitorDef *md) | |
756 | { | |
757 | return (cpu_single_env->msr[MSR_POW] << MSR_POW) | | |
758 | (cpu_single_env->msr[MSR_ILE] << MSR_ILE) | | |
759 | (cpu_single_env->msr[MSR_EE] << MSR_EE) | | |
760 | (cpu_single_env->msr[MSR_PR] << MSR_PR) | | |
761 | (cpu_single_env->msr[MSR_FP] << MSR_FP) | | |
762 | (cpu_single_env->msr[MSR_ME] << MSR_ME) | | |
763 | (cpu_single_env->msr[MSR_FE0] << MSR_FE0) | | |
764 | (cpu_single_env->msr[MSR_SE] << MSR_SE) | | |
765 | (cpu_single_env->msr[MSR_BE] << MSR_BE) | | |
766 | (cpu_single_env->msr[MSR_FE1] << MSR_FE1) | | |
767 | (cpu_single_env->msr[MSR_IP] << MSR_IP) | | |
768 | (cpu_single_env->msr[MSR_IR] << MSR_IR) | | |
769 | (cpu_single_env->msr[MSR_DR] << MSR_DR) | | |
770 | (cpu_single_env->msr[MSR_RI] << MSR_RI) | | |
771 | (cpu_single_env->msr[MSR_LE] << MSR_LE); | |
772 | } | |
773 | ||
774 | static int monitor_get_xer (struct MonitorDef *md) | |
775 | { | |
776 | return (cpu_single_env->xer[XER_SO] << XER_SO) | | |
777 | (cpu_single_env->xer[XER_OV] << XER_OV) | | |
778 | (cpu_single_env->xer[XER_CA] << XER_CA) | | |
779 | (cpu_single_env->xer[XER_BC] << XER_BC); | |
780 | } | |
9fddaa0c FB |
781 | |
782 | uint32_t cpu_ppc_load_decr (CPUState *env); | |
783 | static int monitor_get_decr (struct MonitorDef *md) | |
784 | { | |
785 | return cpu_ppc_load_decr(cpu_single_env); | |
786 | } | |
787 | ||
788 | uint32_t cpu_ppc_load_tbu (CPUState *env); | |
789 | static int monitor_get_tbu (struct MonitorDef *md) | |
790 | { | |
791 | return cpu_ppc_load_tbu(cpu_single_env); | |
792 | } | |
793 | ||
794 | uint32_t cpu_ppc_load_tbl (CPUState *env); | |
795 | static int monitor_get_tbl (struct MonitorDef *md) | |
796 | { | |
797 | return cpu_ppc_load_tbl(cpu_single_env); | |
798 | } | |
a541f297 FB |
799 | #endif |
800 | ||
9307c4c1 FB |
801 | static MonitorDef monitor_defs[] = { |
802 | #ifdef TARGET_I386 | |
57206fd4 FB |
803 | |
804 | #define SEG(name, seg) \ | |
805 | { name, offsetof(CPUState, segs[seg].selector) },\ | |
806 | { name ".base", offsetof(CPUState, segs[seg].base) },\ | |
807 | { name ".limit", offsetof(CPUState, segs[seg].limit) }, | |
808 | ||
9307c4c1 FB |
809 | { "eax", offsetof(CPUState, regs[0]) }, |
810 | { "ecx", offsetof(CPUState, regs[1]) }, | |
811 | { "edx", offsetof(CPUState, regs[2]) }, | |
812 | { "ebx", offsetof(CPUState, regs[3]) }, | |
813 | { "esp|sp", offsetof(CPUState, regs[4]) }, | |
814 | { "ebp|fp", offsetof(CPUState, regs[5]) }, | |
815 | { "esi", offsetof(CPUState, regs[6]) }, | |
816 | { "esi", offsetof(CPUState, regs[7]) }, | |
817 | { "eflags", offsetof(CPUState, eflags) }, | |
57206fd4 FB |
818 | { "eip", offsetof(CPUState, eip) }, |
819 | SEG("cs", R_CS) | |
820 | SEG("ds", R_DS) | |
821 | SEG("es", R_ES) | |
822 | SEG("fs", R_FS) | |
823 | SEG("gs", R_GS) | |
824 | { "pc", 0, monitor_get_pc, }, | |
a541f297 FB |
825 | #elif defined(TARGET_PPC) |
826 | { "r0", offsetof(CPUState, gpr[0]) }, | |
827 | { "r1", offsetof(CPUState, gpr[1]) }, | |
828 | { "r2", offsetof(CPUState, gpr[2]) }, | |
829 | { "r3", offsetof(CPUState, gpr[3]) }, | |
830 | { "r4", offsetof(CPUState, gpr[4]) }, | |
831 | { "r5", offsetof(CPUState, gpr[5]) }, | |
832 | { "r6", offsetof(CPUState, gpr[6]) }, | |
833 | { "r7", offsetof(CPUState, gpr[7]) }, | |
834 | { "r8", offsetof(CPUState, gpr[8]) }, | |
835 | { "r9", offsetof(CPUState, gpr[9]) }, | |
836 | { "r10", offsetof(CPUState, gpr[10]) }, | |
837 | { "r11", offsetof(CPUState, gpr[11]) }, | |
838 | { "r12", offsetof(CPUState, gpr[12]) }, | |
839 | { "r13", offsetof(CPUState, gpr[13]) }, | |
840 | { "r14", offsetof(CPUState, gpr[14]) }, | |
841 | { "r15", offsetof(CPUState, gpr[15]) }, | |
842 | { "r16", offsetof(CPUState, gpr[16]) }, | |
843 | { "r17", offsetof(CPUState, gpr[17]) }, | |
844 | { "r18", offsetof(CPUState, gpr[18]) }, | |
845 | { "r19", offsetof(CPUState, gpr[19]) }, | |
846 | { "r20", offsetof(CPUState, gpr[20]) }, | |
847 | { "r21", offsetof(CPUState, gpr[21]) }, | |
848 | { "r22", offsetof(CPUState, gpr[22]) }, | |
849 | { "r23", offsetof(CPUState, gpr[23]) }, | |
850 | { "r24", offsetof(CPUState, gpr[24]) }, | |
851 | { "r25", offsetof(CPUState, gpr[25]) }, | |
852 | { "r26", offsetof(CPUState, gpr[26]) }, | |
853 | { "r27", offsetof(CPUState, gpr[27]) }, | |
854 | { "r28", offsetof(CPUState, gpr[28]) }, | |
855 | { "r29", offsetof(CPUState, gpr[29]) }, | |
856 | { "r30", offsetof(CPUState, gpr[30]) }, | |
857 | { "r31", offsetof(CPUState, gpr[31]) }, | |
57206fd4 | 858 | { "nip|pc", offsetof(CPUState, nip) }, |
a541f297 FB |
859 | { "lr", offsetof(CPUState, lr) }, |
860 | { "ctr", offsetof(CPUState, ctr) }, | |
9fddaa0c | 861 | { "decr", 0, &monitor_get_decr, }, |
a541f297 FB |
862 | { "ccr", 0, &monitor_get_ccr, }, |
863 | { "msr", 0, &monitor_get_msr, }, | |
864 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
865 | { "tbu", 0, &monitor_get_tbu, }, |
866 | { "tbl", 0, &monitor_get_tbl, }, | |
a541f297 FB |
867 | { "sdr1", offsetof(CPUState, sdr1) }, |
868 | { "sr0", offsetof(CPUState, sr[0]) }, | |
869 | { "sr1", offsetof(CPUState, sr[1]) }, | |
870 | { "sr2", offsetof(CPUState, sr[2]) }, | |
871 | { "sr3", offsetof(CPUState, sr[3]) }, | |
872 | { "sr4", offsetof(CPUState, sr[4]) }, | |
873 | { "sr5", offsetof(CPUState, sr[5]) }, | |
874 | { "sr6", offsetof(CPUState, sr[6]) }, | |
875 | { "sr7", offsetof(CPUState, sr[7]) }, | |
876 | { "sr8", offsetof(CPUState, sr[8]) }, | |
877 | { "sr9", offsetof(CPUState, sr[9]) }, | |
878 | { "sr10", offsetof(CPUState, sr[10]) }, | |
879 | { "sr11", offsetof(CPUState, sr[11]) }, | |
880 | { "sr12", offsetof(CPUState, sr[12]) }, | |
881 | { "sr13", offsetof(CPUState, sr[13]) }, | |
882 | { "sr14", offsetof(CPUState, sr[14]) }, | |
883 | { "sr15", offsetof(CPUState, sr[15]) }, | |
884 | /* Too lazy to put BATs and SPRs ... */ | |
9307c4c1 FB |
885 | #endif |
886 | { NULL }, | |
887 | }; | |
888 | ||
889 | static void expr_error(const char *fmt) | |
9dc39cba | 890 | { |
9307c4c1 FB |
891 | term_printf(fmt); |
892 | term_printf("\n"); | |
893 | longjmp(expr_env, 1); | |
894 | } | |
895 | ||
896 | static int get_monitor_def(int *pval, const char *name) | |
897 | { | |
898 | MonitorDef *md; | |
899 | for(md = monitor_defs; md->name != NULL; md++) { | |
900 | if (compare_cmd(name, md->name)) { | |
901 | if (md->get_value) { | |
902 | *pval = md->get_value(md); | |
903 | } else { | |
904 | *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset); | |
905 | } | |
906 | return 0; | |
907 | } | |
908 | } | |
909 | return -1; | |
910 | } | |
911 | ||
912 | static void next(void) | |
913 | { | |
914 | if (pch != '\0') { | |
915 | pch++; | |
916 | while (isspace(*pch)) | |
917 | pch++; | |
918 | } | |
919 | } | |
920 | ||
921 | static int expr_sum(void); | |
922 | ||
923 | static int expr_unary(void) | |
924 | { | |
925 | int n; | |
926 | char *p; | |
927 | ||
928 | switch(*pch) { | |
929 | case '+': | |
930 | next(); | |
931 | n = expr_unary(); | |
932 | break; | |
933 | case '-': | |
934 | next(); | |
935 | n = -expr_unary(); | |
936 | break; | |
937 | case '~': | |
938 | next(); | |
939 | n = ~expr_unary(); | |
940 | break; | |
941 | case '(': | |
942 | next(); | |
943 | n = expr_sum(); | |
944 | if (*pch != ')') { | |
945 | expr_error("')' expected"); | |
946 | } | |
947 | next(); | |
948 | break; | |
949 | case '$': | |
950 | { | |
951 | char buf[128], *q; | |
952 | ||
953 | pch++; | |
954 | q = buf; | |
955 | while ((*pch >= 'a' && *pch <= 'z') || | |
956 | (*pch >= 'A' && *pch <= 'Z') || | |
957 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 958 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
959 | if ((q - buf) < sizeof(buf) - 1) |
960 | *q++ = *pch; | |
961 | pch++; | |
962 | } | |
963 | while (isspace(*pch)) | |
964 | pch++; | |
965 | *q = 0; | |
966 | if (get_monitor_def(&n, buf)) | |
967 | expr_error("unknown register"); | |
968 | } | |
969 | break; | |
970 | case '\0': | |
971 | expr_error("unexpected end of expression"); | |
972 | n = 0; | |
973 | break; | |
974 | default: | |
975 | n = strtoul(pch, &p, 0); | |
976 | if (pch == p) { | |
977 | expr_error("invalid char in expression"); | |
978 | } | |
979 | pch = p; | |
980 | while (isspace(*pch)) | |
981 | pch++; | |
982 | break; | |
983 | } | |
984 | return n; | |
985 | } | |
986 | ||
987 | ||
988 | static int expr_prod(void) | |
989 | { | |
990 | int val, val2, op; | |
991 | ||
992 | val = expr_unary(); | |
993 | for(;;) { | |
994 | op = *pch; | |
995 | if (op != '*' && op != '/' && op != '%') | |
996 | break; | |
997 | next(); | |
998 | val2 = expr_unary(); | |
999 | switch(op) { | |
1000 | default: | |
1001 | case '*': | |
1002 | val *= val2; | |
1003 | break; | |
1004 | case '/': | |
1005 | case '%': | |
1006 | if (val2 == 0) | |
5b60212f | 1007 | expr_error("division by zero"); |
9307c4c1 FB |
1008 | if (op == '/') |
1009 | val /= val2; | |
1010 | else | |
1011 | val %= val2; | |
1012 | break; | |
1013 | } | |
1014 | } | |
1015 | return val; | |
1016 | } | |
1017 | ||
1018 | static int expr_logic(void) | |
1019 | { | |
1020 | int val, val2, op; | |
1021 | ||
1022 | val = expr_prod(); | |
1023 | for(;;) { | |
1024 | op = *pch; | |
1025 | if (op != '&' && op != '|' && op != '^') | |
1026 | break; | |
1027 | next(); | |
1028 | val2 = expr_prod(); | |
1029 | switch(op) { | |
1030 | default: | |
1031 | case '&': | |
1032 | val &= val2; | |
1033 | break; | |
1034 | case '|': | |
1035 | val |= val2; | |
1036 | break; | |
1037 | case '^': | |
1038 | val ^= val2; | |
1039 | break; | |
1040 | } | |
1041 | } | |
1042 | return val; | |
1043 | } | |
1044 | ||
1045 | static int expr_sum(void) | |
1046 | { | |
1047 | int val, val2, op; | |
1048 | ||
1049 | val = expr_logic(); | |
1050 | for(;;) { | |
1051 | op = *pch; | |
1052 | if (op != '+' && op != '-') | |
1053 | break; | |
1054 | next(); | |
1055 | val2 = expr_logic(); | |
1056 | if (op == '+') | |
1057 | val += val2; | |
1058 | else | |
1059 | val -= val2; | |
1060 | } | |
1061 | return val; | |
1062 | } | |
1063 | ||
1064 | static int get_expr(int *pval, const char **pp) | |
1065 | { | |
1066 | pch = *pp; | |
1067 | if (setjmp(expr_env)) { | |
1068 | *pp = pch; | |
1069 | return -1; | |
1070 | } | |
1071 | while (isspace(*pch)) | |
1072 | pch++; | |
1073 | *pval = expr_sum(); | |
1074 | *pp = pch; | |
1075 | return 0; | |
1076 | } | |
1077 | ||
1078 | static int get_str(char *buf, int buf_size, const char **pp) | |
1079 | { | |
1080 | const char *p; | |
1081 | char *q; | |
1082 | int c; | |
1083 | ||
1084 | p = *pp; | |
1085 | while (isspace(*p)) | |
1086 | p++; | |
1087 | if (*p == '\0') { | |
1088 | fail: | |
1089 | *pp = p; | |
1090 | return -1; | |
1091 | } | |
1092 | q = buf; | |
1093 | if (*p == '\"') { | |
1094 | p++; | |
1095 | while (*p != '\0' && *p != '\"') { | |
1096 | if (*p == '\\') { | |
1097 | p++; | |
1098 | c = *p++; | |
1099 | switch(c) { | |
1100 | case 'n': | |
1101 | c = '\n'; | |
1102 | break; | |
1103 | case 'r': | |
1104 | c = '\r'; | |
1105 | break; | |
1106 | case '\\': | |
1107 | case '\'': | |
1108 | case '\"': | |
1109 | break; | |
1110 | default: | |
1111 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
1112 | goto fail; | |
1113 | } | |
1114 | if ((q - buf) < buf_size - 1) { | |
1115 | *q++ = c; | |
1116 | } | |
1117 | } else { | |
1118 | if ((q - buf) < buf_size - 1) { | |
1119 | *q++ = *p; | |
1120 | } | |
1121 | p++; | |
1122 | } | |
1123 | } | |
1124 | if (*p != '\"') { | |
5b60212f | 1125 | qemu_printf("unterminated string\n"); |
9307c4c1 FB |
1126 | goto fail; |
1127 | } | |
1128 | p++; | |
1129 | } else { | |
1130 | while (*p != '\0' && !isspace(*p)) { | |
1131 | if ((q - buf) < buf_size - 1) { | |
1132 | *q++ = *p; | |
1133 | } | |
1134 | p++; | |
1135 | } | |
1136 | *q = '\0'; | |
1137 | } | |
1138 | *pp = p; | |
1139 | return 0; | |
1140 | } | |
1141 | ||
1142 | static int default_fmt_format = 'x'; | |
1143 | static int default_fmt_size = 4; | |
1144 | ||
1145 | #define MAX_ARGS 16 | |
1146 | ||
1147 | static void term_handle_command(const char *cmdline) | |
1148 | { | |
1149 | const char *p, *pstart, *typestr; | |
1150 | char *q; | |
1151 | int c, nb_args, len, i, has_arg; | |
9dc39cba | 1152 | term_cmd_t *cmd; |
9307c4c1 FB |
1153 | char cmdname[256]; |
1154 | char buf[1024]; | |
1155 | void *str_allocated[MAX_ARGS]; | |
1156 | void *args[MAX_ARGS]; | |
9dc39cba FB |
1157 | |
1158 | #ifdef DEBUG | |
1159 | term_printf("command='%s'\n", cmdline); | |
1160 | #endif | |
1161 | ||
9307c4c1 | 1162 | /* extract the command name */ |
9dc39cba | 1163 | p = cmdline; |
9307c4c1 FB |
1164 | q = cmdname; |
1165 | while (isspace(*p)) | |
1166 | p++; | |
1167 | if (*p == '\0') | |
1168 | return; | |
1169 | pstart = p; | |
1170 | while (*p != '\0' && *p != '/' && !isspace(*p)) | |
1171 | p++; | |
1172 | len = p - pstart; | |
1173 | if (len > sizeof(cmdname) - 1) | |
1174 | len = sizeof(cmdname) - 1; | |
1175 | memcpy(cmdname, pstart, len); | |
1176 | cmdname[len] = '\0'; | |
1177 | ||
1178 | /* find the command */ | |
1179 | for(cmd = term_cmds; cmd->name != NULL; cmd++) { | |
1180 | if (compare_cmd(cmdname, cmd->name)) | |
1181 | goto found; | |
1182 | } | |
1183 | term_printf("unknown command: '%s'\n", cmdname); | |
1184 | return; | |
1185 | found: | |
1186 | ||
1187 | for(i = 0; i < MAX_ARGS; i++) | |
1188 | str_allocated[i] = NULL; | |
1189 | ||
1190 | /* parse the parameters */ | |
1191 | typestr = cmd->args_type; | |
1192 | nb_args = 0; | |
9dc39cba | 1193 | for(;;) { |
9307c4c1 FB |
1194 | c = *typestr; |
1195 | if (c == '\0') | |
9dc39cba | 1196 | break; |
9307c4c1 FB |
1197 | typestr++; |
1198 | switch(c) { | |
1199 | case 'F': | |
1200 | case 's': | |
1201 | { | |
1202 | int ret; | |
1203 | char *str; | |
1204 | ||
1205 | while (isspace(*p)) | |
1206 | p++; | |
1207 | if (*typestr == '?') { | |
1208 | typestr++; | |
1209 | if (*p == '\0') { | |
1210 | /* no optional string: NULL argument */ | |
1211 | str = NULL; | |
1212 | goto add_str; | |
1213 | } | |
1214 | } | |
1215 | ret = get_str(buf, sizeof(buf), &p); | |
1216 | if (ret < 0) { | |
1217 | if (c == 'F') | |
1218 | term_printf("%s: filename expected\n", cmdname); | |
1219 | else | |
1220 | term_printf("%s: string expected\n", cmdname); | |
1221 | goto fail; | |
1222 | } | |
1223 | str = qemu_malloc(strlen(buf) + 1); | |
1224 | strcpy(str, buf); | |
1225 | str_allocated[nb_args] = str; | |
1226 | add_str: | |
1227 | if (nb_args >= MAX_ARGS) { | |
1228 | error_args: | |
1229 | term_printf("%s: too many arguments\n", cmdname); | |
1230 | goto fail; | |
1231 | } | |
1232 | args[nb_args++] = str; | |
1233 | } | |
9dc39cba | 1234 | break; |
9307c4c1 FB |
1235 | case '/': |
1236 | { | |
1237 | int count, format, size; | |
1238 | ||
1239 | while (isspace(*p)) | |
1240 | p++; | |
1241 | if (*p == '/') { | |
1242 | /* format found */ | |
1243 | p++; | |
1244 | count = 1; | |
1245 | if (isdigit(*p)) { | |
1246 | count = 0; | |
1247 | while (isdigit(*p)) { | |
1248 | count = count * 10 + (*p - '0'); | |
1249 | p++; | |
1250 | } | |
1251 | } | |
1252 | size = -1; | |
1253 | format = -1; | |
1254 | for(;;) { | |
1255 | switch(*p) { | |
1256 | case 'o': | |
1257 | case 'd': | |
1258 | case 'u': | |
1259 | case 'x': | |
1260 | case 'i': | |
1261 | case 'c': | |
1262 | format = *p++; | |
1263 | break; | |
1264 | case 'b': | |
1265 | size = 1; | |
1266 | p++; | |
1267 | break; | |
1268 | case 'h': | |
1269 | size = 2; | |
1270 | p++; | |
1271 | break; | |
1272 | case 'w': | |
1273 | size = 4; | |
1274 | p++; | |
1275 | break; | |
1276 | case 'g': | |
1277 | case 'L': | |
1278 | size = 8; | |
1279 | p++; | |
1280 | break; | |
1281 | default: | |
1282 | goto next; | |
1283 | } | |
1284 | } | |
1285 | next: | |
1286 | if (*p != '\0' && !isspace(*p)) { | |
1287 | term_printf("invalid char in format: '%c'\n", *p); | |
1288 | goto fail; | |
1289 | } | |
9307c4c1 FB |
1290 | if (format < 0) |
1291 | format = default_fmt_format; | |
4c27ba27 FB |
1292 | if (format != 'i') { |
1293 | /* for 'i', not specifying a size gives -1 as size */ | |
1294 | if (size < 0) | |
1295 | size = default_fmt_size; | |
1296 | } | |
9307c4c1 FB |
1297 | default_fmt_size = size; |
1298 | default_fmt_format = format; | |
1299 | } else { | |
1300 | count = 1; | |
1301 | format = default_fmt_format; | |
4c27ba27 FB |
1302 | if (format != 'i') { |
1303 | size = default_fmt_size; | |
1304 | } else { | |
1305 | size = -1; | |
1306 | } | |
9307c4c1 FB |
1307 | } |
1308 | if (nb_args + 3 > MAX_ARGS) | |
1309 | goto error_args; | |
1310 | args[nb_args++] = (void*)count; | |
1311 | args[nb_args++] = (void*)format; | |
1312 | args[nb_args++] = (void*)size; | |
1313 | } | |
9dc39cba | 1314 | break; |
9307c4c1 FB |
1315 | case 'i': |
1316 | { | |
1317 | int val; | |
1318 | while (isspace(*p)) | |
1319 | p++; | |
3440557b | 1320 | if (*typestr == '?' || *typestr == '.') { |
9307c4c1 | 1321 | typestr++; |
3440557b FB |
1322 | if (*typestr == '?') { |
1323 | if (*p == '\0') | |
1324 | has_arg = 0; | |
1325 | else | |
1326 | has_arg = 1; | |
1327 | } else { | |
1328 | if (*p == '.') { | |
1329 | p++; | |
1330 | while (isspace(*p)) | |
1331 | p++; | |
1332 | has_arg = 1; | |
1333 | } else { | |
1334 | has_arg = 0; | |
1335 | } | |
1336 | } | |
9307c4c1 FB |
1337 | if (nb_args >= MAX_ARGS) |
1338 | goto error_args; | |
1339 | args[nb_args++] = (void *)has_arg; | |
1340 | if (!has_arg) { | |
1341 | if (nb_args >= MAX_ARGS) | |
1342 | goto error_args; | |
1343 | val = -1; | |
1344 | goto add_num; | |
1345 | } | |
1346 | } | |
1347 | if (get_expr(&val, &p)) | |
1348 | goto fail; | |
1349 | add_num: | |
1350 | if (nb_args >= MAX_ARGS) | |
1351 | goto error_args; | |
1352 | args[nb_args++] = (void *)val; | |
1353 | } | |
1354 | break; | |
1355 | case '-': | |
1356 | { | |
1357 | int has_option; | |
1358 | /* option */ | |
1359 | ||
1360 | c = *typestr++; | |
1361 | if (c == '\0') | |
1362 | goto bad_type; | |
1363 | while (isspace(*p)) | |
1364 | p++; | |
1365 | has_option = 0; | |
1366 | if (*p == '-') { | |
1367 | p++; | |
1368 | if (*p != c) { | |
1369 | term_printf("%s: unsupported option -%c\n", | |
1370 | cmdname, *p); | |
1371 | goto fail; | |
1372 | } | |
1373 | p++; | |
1374 | has_option = 1; | |
1375 | } | |
1376 | if (nb_args >= MAX_ARGS) | |
1377 | goto error_args; | |
1378 | args[nb_args++] = (void *)has_option; | |
1379 | } | |
1380 | break; | |
1381 | default: | |
1382 | bad_type: | |
1383 | term_printf("%s: unknown type '%c'\n", cmdname, c); | |
1384 | goto fail; | |
1385 | } | |
9dc39cba | 1386 | } |
9307c4c1 FB |
1387 | /* check that all arguments were parsed */ |
1388 | while (isspace(*p)) | |
1389 | p++; | |
1390 | if (*p != '\0') { | |
1391 | term_printf("%s: extraneous characters at the end of line\n", | |
1392 | cmdname); | |
1393 | goto fail; | |
9dc39cba | 1394 | } |
9307c4c1 FB |
1395 | |
1396 | switch(nb_args) { | |
1397 | case 0: | |
1398 | cmd->handler(); | |
1399 | break; | |
1400 | case 1: | |
1401 | cmd->handler(args[0]); | |
1402 | break; | |
1403 | case 2: | |
1404 | cmd->handler(args[0], args[1]); | |
1405 | break; | |
1406 | case 3: | |
1407 | cmd->handler(args[0], args[1], args[2]); | |
1408 | break; | |
1409 | case 4: | |
1410 | cmd->handler(args[0], args[1], args[2], args[3]); | |
1411 | break; | |
1412 | case 5: | |
1413 | cmd->handler(args[0], args[1], args[2], args[3], args[4]); | |
1414 | break; | |
3440557b FB |
1415 | case 6: |
1416 | cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]); | |
1417 | break; | |
9307c4c1 FB |
1418 | default: |
1419 | term_printf("unsupported number of arguments: %d\n", nb_args); | |
1420 | goto fail; | |
9dc39cba | 1421 | } |
9307c4c1 FB |
1422 | fail: |
1423 | for(i = 0; i < MAX_ARGS; i++) | |
1424 | qemu_free(str_allocated[i]); | |
9dc39cba | 1425 | return; |
9dc39cba FB |
1426 | } |
1427 | ||
1428 | static void term_show_prompt(void) | |
1429 | { | |
1430 | term_printf("(qemu) "); | |
1431 | fflush(stdout); | |
1432 | term_cmd_buf_index = 0; | |
1433 | term_cmd_buf_size = 0; | |
1434 | term_esc_state = IS_NORM; | |
1435 | } | |
1436 | ||
aa455485 FB |
1437 | static void term_print_cmdline (const char *cmdline) |
1438 | { | |
1439 | term_show_prompt(); | |
1440 | term_printf(cmdline); | |
1441 | term_flush(); | |
1442 | } | |
1443 | ||
9dc39cba FB |
1444 | static void term_insert_char(int ch) |
1445 | { | |
1446 | if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) { | |
1447 | memmove(term_cmd_buf + term_cmd_buf_index + 1, | |
1448 | term_cmd_buf + term_cmd_buf_index, | |
1449 | term_cmd_buf_size - term_cmd_buf_index); | |
1450 | term_cmd_buf[term_cmd_buf_index] = ch; | |
1451 | term_cmd_buf_size++; | |
1452 | term_printf("\033[@%c", ch); | |
1453 | term_cmd_buf_index++; | |
1454 | term_flush(); | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | static void term_backward_char(void) | |
1459 | { | |
1460 | if (term_cmd_buf_index > 0) { | |
1461 | term_cmd_buf_index--; | |
1462 | term_printf("\033[D"); | |
1463 | term_flush(); | |
1464 | } | |
1465 | } | |
1466 | ||
1467 | static void term_forward_char(void) | |
1468 | { | |
1469 | if (term_cmd_buf_index < term_cmd_buf_size) { | |
1470 | term_cmd_buf_index++; | |
1471 | term_printf("\033[C"); | |
1472 | term_flush(); | |
1473 | } | |
1474 | } | |
1475 | ||
1476 | static void term_delete_char(void) | |
1477 | { | |
1478 | if (term_cmd_buf_index < term_cmd_buf_size) { | |
1479 | memmove(term_cmd_buf + term_cmd_buf_index, | |
1480 | term_cmd_buf + term_cmd_buf_index + 1, | |
1481 | term_cmd_buf_size - term_cmd_buf_index - 1); | |
1482 | term_printf("\033[P"); | |
1483 | term_cmd_buf_size--; | |
1484 | term_flush(); | |
1485 | } | |
1486 | } | |
1487 | ||
1488 | static void term_backspace(void) | |
1489 | { | |
1490 | if (term_cmd_buf_index > 0) { | |
1491 | term_backward_char(); | |
1492 | term_delete_char(); | |
1493 | } | |
1494 | } | |
1495 | ||
1496 | static void term_bol(void) | |
1497 | { | |
1498 | while (term_cmd_buf_index > 0) | |
1499 | term_backward_char(); | |
1500 | } | |
1501 | ||
1502 | static void term_eol(void) | |
1503 | { | |
1504 | while (term_cmd_buf_index < term_cmd_buf_size) | |
1505 | term_forward_char(); | |
1506 | } | |
1507 | ||
aa455485 FB |
1508 | static void term_up_char(void) |
1509 | { | |
1510 | int idx; | |
1511 | ||
1512 | if (term_hist_entry == 0) | |
1513 | return; | |
1514 | if (term_hist_entry == -1) { | |
1515 | /* Find latest entry */ | |
1516 | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { | |
1517 | if (term_history[idx] == NULL) | |
1518 | break; | |
1519 | } | |
1520 | term_hist_entry = idx; | |
1521 | } | |
1522 | term_hist_entry--; | |
1523 | if (term_hist_entry >= 0) { | |
1524 | strcpy(term_cmd_buf, term_history[term_hist_entry]); | |
1525 | term_printf("\n"); | |
1526 | term_print_cmdline(term_cmd_buf); | |
1527 | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); | |
1528 | } | |
1529 | } | |
1530 | ||
1531 | static void term_down_char(void) | |
1532 | { | |
1533 | if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1) | |
1534 | return; | |
1535 | if (term_history[++term_hist_entry] != NULL) { | |
1536 | strcpy(term_cmd_buf, term_history[term_hist_entry]); | |
1537 | } else { | |
1538 | term_hist_entry = -1; | |
1539 | } | |
1540 | term_printf("\n"); | |
1541 | term_print_cmdline(term_cmd_buf); | |
1542 | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); | |
1543 | } | |
1544 | ||
1545 | static void term_hist_add(const char *cmdline) | |
1546 | { | |
1547 | char *hist_entry, *new_entry; | |
1548 | int idx; | |
1549 | ||
1550 | if (cmdline[0] == '\0') | |
1551 | return; | |
1552 | new_entry = NULL; | |
1553 | if (term_hist_entry != -1) { | |
1554 | /* We were editing an existing history entry: replace it */ | |
1555 | hist_entry = term_history[term_hist_entry]; | |
1556 | idx = term_hist_entry; | |
1557 | if (strcmp(hist_entry, cmdline) == 0) { | |
1558 | goto same_entry; | |
1559 | } | |
1560 | } | |
1561 | /* Search cmdline in history buffers */ | |
1562 | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { | |
1563 | hist_entry = term_history[idx]; | |
1564 | if (hist_entry == NULL) | |
1565 | break; | |
1566 | if (strcmp(hist_entry, cmdline) == 0) { | |
1567 | same_entry: | |
1568 | new_entry = hist_entry; | |
1569 | /* Put this entry at the end of history */ | |
1570 | memmove(&term_history[idx], &term_history[idx + 1], | |
1571 | &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]); | |
1572 | term_history[TERM_MAX_CMDS - 1] = NULL; | |
1573 | for (; idx < TERM_MAX_CMDS; idx++) { | |
1574 | if (term_history[idx] == NULL) | |
1575 | break; | |
1576 | } | |
1577 | break; | |
1578 | } | |
1579 | } | |
1580 | if (idx == TERM_MAX_CMDS) { | |
1581 | /* Need to get one free slot */ | |
1582 | free(term_history[0]); | |
1583 | memcpy(term_history, &term_history[1], | |
1584 | &term_history[TERM_MAX_CMDS] - &term_history[1]); | |
1585 | term_history[TERM_MAX_CMDS - 1] = NULL; | |
1586 | idx = TERM_MAX_CMDS - 1; | |
1587 | } | |
1588 | if (new_entry == NULL) | |
1589 | new_entry = strdup(cmdline); | |
1590 | term_history[idx] = new_entry; | |
1591 | term_hist_entry = -1; | |
1592 | } | |
1593 | ||
9dc39cba FB |
1594 | /* return true if command handled */ |
1595 | static void term_handle_byte(int ch) | |
1596 | { | |
1597 | switch(term_esc_state) { | |
1598 | case IS_NORM: | |
1599 | switch(ch) { | |
1600 | case 1: | |
1601 | term_bol(); | |
1602 | break; | |
1603 | case 5: | |
1604 | term_eol(); | |
1605 | break; | |
1606 | case 10: | |
1607 | case 13: | |
1608 | term_cmd_buf[term_cmd_buf_size] = '\0'; | |
aa455485 | 1609 | term_hist_add(term_cmd_buf); |
9dc39cba FB |
1610 | term_printf("\n"); |
1611 | term_handle_command(term_cmd_buf); | |
1612 | term_show_prompt(); | |
1613 | break; | |
1614 | case 27: | |
1615 | term_esc_state = IS_ESC; | |
1616 | break; | |
1617 | case 127: | |
1618 | case 8: | |
1619 | term_backspace(); | |
1620 | break; | |
aa455485 FB |
1621 | case 155: |
1622 | term_esc_state = IS_CSI; | |
1623 | break; | |
9dc39cba FB |
1624 | default: |
1625 | if (ch >= 32) { | |
1626 | term_insert_char(ch); | |
1627 | } | |
1628 | break; | |
1629 | } | |
1630 | break; | |
1631 | case IS_ESC: | |
1632 | if (ch == '[') { | |
1633 | term_esc_state = IS_CSI; | |
1634 | term_esc_param = 0; | |
1635 | } else { | |
1636 | term_esc_state = IS_NORM; | |
1637 | } | |
1638 | break; | |
1639 | case IS_CSI: | |
1640 | switch(ch) { | |
aa455485 FB |
1641 | case 'A': |
1642 | case 'F': | |
1643 | term_up_char(); | |
1644 | break; | |
1645 | case 'B': | |
1646 | case 'E': | |
1647 | term_down_char(); | |
1648 | break; | |
9dc39cba FB |
1649 | case 'D': |
1650 | term_backward_char(); | |
1651 | break; | |
1652 | case 'C': | |
1653 | term_forward_char(); | |
1654 | break; | |
1655 | case '0' ... '9': | |
1656 | term_esc_param = term_esc_param * 10 + (ch - '0'); | |
1657 | goto the_end; | |
1658 | case '~': | |
1659 | switch(term_esc_param) { | |
1660 | case 1: | |
1661 | term_bol(); | |
1662 | break; | |
1663 | case 3: | |
1664 | term_delete_char(); | |
1665 | break; | |
1666 | case 4: | |
1667 | term_eol(); | |
1668 | break; | |
1669 | } | |
1670 | break; | |
1671 | default: | |
1672 | break; | |
1673 | } | |
1674 | term_esc_state = IS_NORM; | |
1675 | the_end: | |
1676 | break; | |
1677 | } | |
1678 | } | |
1679 | ||
1680 | /*************************************************************/ | |
1681 | /* serial console support */ | |
1682 | ||
1683 | #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ | |
1684 | ||
1685 | static int term_got_escape, term_command; | |
1686 | ||
1687 | void term_print_help(void) | |
1688 | { | |
1689 | term_printf("\n" | |
1690 | "C-a h print this help\n" | |
1691 | "C-a x exit emulatior\n" | |
9dc39cba FB |
1692 | "C-a s save disk data back to file (if -snapshot)\n" |
1693 | "C-a b send break (magic sysrq)\n" | |
1694 | "C-a c switch between console and monitor\n" | |
1695 | "C-a C-a send C-a\n" | |
1696 | ); | |
1697 | } | |
1698 | ||
1699 | /* called when a char is received */ | |
1700 | static void term_received_byte(int ch) | |
1701 | { | |
1702 | if (!serial_console) { | |
1703 | /* if no serial console, handle every command */ | |
1704 | term_handle_byte(ch); | |
1705 | } else { | |
1706 | if (term_got_escape) { | |
1707 | term_got_escape = 0; | |
1708 | switch(ch) { | |
1709 | case 'h': | |
1710 | term_print_help(); | |
1711 | break; | |
1712 | case 'x': | |
1713 | exit(0); | |
1714 | break; | |
1715 | case 's': | |
1716 | { | |
1717 | int i; | |
1718 | for (i = 0; i < MAX_DISKS; i++) { | |
1719 | if (bs_table[i]) | |
1720 | bdrv_commit(bs_table[i]); | |
1721 | } | |
1722 | } | |
1723 | break; | |
1724 | case 'b': | |
1725 | if (serial_console) | |
1726 | serial_receive_break(serial_console); | |
1727 | break; | |
1728 | case 'c': | |
1729 | if (!term_command) { | |
1730 | term_show_prompt(); | |
1731 | term_command = 1; | |
1732 | } else { | |
1733 | term_command = 0; | |
1734 | } | |
1735 | break; | |
9dc39cba FB |
1736 | case TERM_ESCAPE: |
1737 | goto send_char; | |
1738 | } | |
1739 | } else if (ch == TERM_ESCAPE) { | |
1740 | term_got_escape = 1; | |
1741 | } else { | |
1742 | send_char: | |
1743 | if (term_command) { | |
1744 | term_handle_byte(ch); | |
1745 | } else { | |
1746 | if (serial_console) | |
1747 | serial_receive_byte(serial_console, ch); | |
1748 | } | |
1749 | } | |
1750 | } | |
1751 | } | |
1752 | ||
1753 | static int term_can_read(void *opaque) | |
1754 | { | |
1755 | if (serial_console) { | |
1756 | return serial_can_receive(serial_console); | |
1757 | } else { | |
f193c797 | 1758 | return 128; |
9dc39cba FB |
1759 | } |
1760 | } | |
1761 | ||
1762 | static void term_read(void *opaque, const uint8_t *buf, int size) | |
1763 | { | |
1764 | int i; | |
1765 | for(i = 0; i < size; i++) | |
1766 | term_received_byte(buf[i]); | |
1767 | } | |
1768 | ||
1769 | void monitor_init(void) | |
1770 | { | |
1771 | if (!serial_console) { | |
1772 | term_printf("QEMU %s monitor - type 'help' for more information\n", | |
1773 | QEMU_VERSION); | |
1774 | term_show_prompt(); | |
1775 | } | |
aa455485 | 1776 | term_hist_entry = -1; |
8a7ddc38 | 1777 | qemu_add_fd_read_handler(0, term_can_read, term_read, NULL); |
9dc39cba | 1778 | } |