kvm: cosmetics
[qemu-kvm/fedora.git] / monitor.c
blob63908c9bc479406d6804868935ed1f5569a29309
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
27 #include "migration.h"
28 #if USE_KVM
29 #include "qemu-kvm.h"
30 #endif
31 //#define DEBUG
32 //#define DEBUG_COMPLETION
34 #ifndef offsetof
35 #define offsetof(type, field) ((size_t) &((type *)0)->field)
36 #endif
39 * Supported types:
41 * 'F' filename
42 * 'B' block device name
43 * 's' string (accept optional quote)
44 * 'i' 32 bit integer
45 * 'l' target long (32 or 64 bit)
46 * '/' optional gdb-like print format (like "/10x")
47 * 'A' pass the rest of cmdline as one argument (for subcommands).
49 * '?' optional type (for 'F', 's' and 'i')
53 typedef struct term_cmd_t {
54 const char *name;
55 const char *args_type;
56 void (*handler)();
57 const char *params;
58 const char *help;
59 } term_cmd_t;
61 static CharDriverState *monitor_hd;
63 static term_cmd_t term_cmds[];
64 static term_cmd_t info_cmds[];
65 static term_cmd_t migration_cmds[];
66 static term_cmd_t migration_set_cmds[];
68 static char term_outbuf[1024];
69 static int term_outbuf_index;
71 static void monitor_start_input(void);
72 static void monitor_handle_command(const term_cmd_t *cmds,
73 const char *cmdline);
76 CPUState *mon_cpu = NULL;
78 void term_flush(void)
80 if (term_outbuf_index > 0) {
81 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
82 term_outbuf_index = 0;
86 /* flush at every end of line or if the buffer is full */
87 void term_puts(const char *str)
89 int c;
90 for(;;) {
91 c = *str++;
92 if (c == '\0')
93 break;
94 if (c == '\n')
95 term_outbuf[term_outbuf_index++] = '\r';
96 term_outbuf[term_outbuf_index++] = c;
97 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
98 c == '\n')
99 term_flush();
103 void term_vprintf(const char *fmt, va_list ap)
105 char buf[4096];
106 vsnprintf(buf, sizeof(buf), fmt, ap);
107 term_puts(buf);
110 void term_printf(const char *fmt, ...)
112 va_list ap;
113 va_start(ap, fmt);
114 term_vprintf(fmt, ap);
115 va_end(ap);
118 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
120 va_list ap;
121 va_start(ap, fmt);
122 term_vprintf(fmt, ap);
123 va_end(ap);
124 return 0;
127 static int compare_cmd(const char *name, const char *list)
129 const char *p, *pstart;
130 int len;
131 len = strlen(name);
132 p = list;
133 for(;;) {
134 pstart = p;
135 p = strchr(p, '|');
136 if (!p)
137 p = pstart + strlen(pstart);
138 if ((p - pstart) == len && !memcmp(pstart, name, len))
139 return 1;
140 if (*p == '\0')
141 break;
142 p++;
144 return 0;
147 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
149 term_cmd_t *cmd;
151 for(cmd = cmds; cmd->name != NULL; cmd++) {
152 if (!name || !strcmp(name, cmd->name))
153 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
157 static void help_cmd(const char *name)
159 if (name && !strcmp(name, "info")) {
160 help_cmd1(info_cmds, "info ", NULL);
161 } else {
162 help_cmd1(term_cmds, "", name);
163 if (name && !strcmp(name, "log")) {
164 CPULogItem *item;
165 term_printf("Log items (comma separated):\n");
166 term_printf("%-10s %s\n", "none", "remove all logs");
167 for(item = cpu_log_items; item->mask != 0; item++) {
168 term_printf("%-10s %s\n", item->name, item->help);
174 static void do_help(const char *name)
176 help_cmd(name);
179 static void do_commit(void)
181 int i;
183 for (i = 0; i < MAX_DISKS; i++) {
184 if (bs_table[i]) {
185 bdrv_commit(bs_table[i]);
190 static void do_info(const char *item)
192 term_cmd_t *cmd;
194 if (!item)
195 goto help;
196 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
197 if (compare_cmd(item, cmd->name))
198 goto found;
200 help:
201 help_cmd("info");
202 return;
203 found:
204 cmd->handler();
207 static void do_info_version(void)
209 term_printf("%s\n", QEMU_VERSION);
212 static void do_info_block(void)
214 bdrv_info();
217 /* get the current CPU defined by the user */
218 int mon_set_cpu(int cpu_index)
220 CPUState *env;
222 for(env = first_cpu; env != NULL; env = env->next_cpu) {
223 if (env->cpu_index == cpu_index) {
224 mon_cpu = env;
225 return 0;
228 return -1;
231 CPUState *mon_get_cpu(void)
233 if (!mon_cpu) {
234 mon_set_cpu(0);
236 return mon_cpu;
239 static void do_info_registers(void)
241 CPUState *env;
242 env = mon_get_cpu();
243 if (!env)
244 return;
245 #ifdef TARGET_I386
246 cpu_dump_state(env, NULL, monitor_fprintf,
247 X86_DUMP_FPU);
248 #else
249 cpu_dump_state(env, NULL, monitor_fprintf,
251 #endif
254 static void do_info_cpus(void)
256 CPUState *env;
258 /* just to set the default cpu if not already done */
259 mon_get_cpu();
261 for(env = first_cpu; env != NULL; env = env->next_cpu) {
262 term_printf("%c CPU #%d:",
263 (env == mon_cpu) ? '*' : ' ',
264 env->cpu_index);
265 #if defined(TARGET_I386)
266 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
267 if (env->hflags & HF_HALTED_MASK)
268 term_printf(" (halted)");
269 #elif defined(TARGET_PPC)
270 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
271 if (env->halted)
272 term_printf(" (halted)");
273 #elif defined(TARGET_SPARC)
274 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
275 if (env->halted)
276 term_printf(" (halted)");
277 #endif
278 term_printf("\n");
282 static void do_cpu_set(int index)
284 if (mon_set_cpu(index) < 0)
285 term_printf("Invalid CPU index\n");
288 static void do_info_jit(void)
290 dump_exec_info(NULL, monitor_fprintf);
293 static void do_info_history (void)
295 int i;
296 const char *str;
298 i = 0;
299 for(;;) {
300 str = readline_get_history(i);
301 if (!str)
302 break;
303 term_printf("%d: '%s'\n", i, str);
304 i++;
308 static void do_quit(void)
310 exit(0);
313 static int eject_device(BlockDriverState *bs, int force)
315 if (bdrv_is_inserted(bs)) {
316 if (!force) {
317 if (!bdrv_is_removable(bs)) {
318 term_printf("device is not removable\n");
319 return -1;
321 if (bdrv_is_locked(bs)) {
322 term_printf("device is locked\n");
323 return -1;
326 bdrv_close(bs);
328 return 0;
331 static void do_eject(int force, const char *filename)
333 BlockDriverState *bs;
335 bs = bdrv_find(filename);
336 if (!bs) {
337 term_printf("device not found\n");
338 return;
340 eject_device(bs, force);
344 int vmdk_snapshot_create(BlockDriverState *bs);
346 static void do_snapshot(const char *hda, const char *hdb,
347 const char *hdc, const char *hdd)
350 if (!strcmp(hda,"hda")) {
351 if (vmdk_snapshot_create(bs_table[0]) == -1)
352 term_printf("hda snapshot fail\n");
355 if (hdb) {
356 if (!strcmp(hdb,"hdb")) {
357 if (bs_table[1]) {
358 if (vmdk_snapshot_create(bs_table[1]) == -1)
359 term_printf("hdb snapshot fail\n");
360 }else {
361 term_printf("hdb not exist\n");
366 if (hdc) {
367 if (!strcmp(hdc,"hdc")) {
368 if (bs_table[2]) {
369 if (vmdk_snapshot_create(bs_table[2]) == -1)
370 term_printf("hdc snapshot fail\n");
371 }else {
372 term_printf("hdc not exist\n");
377 if (hdd) {
378 if (!strcmp(hdd,"hdd")) {
379 if (bs_table[3]) {
380 if (vmdk_snapshot_create(bs_table[3]) == -1)
381 term_printf("hdd snapshot fail\n");
382 }else {
383 term_printf("hdd not exist\n");
389 static void do_change(const char *device, const char *filename)
391 BlockDriverState *bs;
392 int i;
393 char password[256];
395 bs = bdrv_find(device);
396 if (!bs) {
397 term_printf("device not found\n");
398 return;
400 if (eject_device(bs, 0) < 0)
401 return;
402 bdrv_open(bs, filename, 0);
403 if (bdrv_is_encrypted(bs)) {
404 term_printf("%s is encrypted.\n", device);
405 for(i = 0; i < 3; i++) {
406 monitor_readline("Password: ", 1, password, sizeof(password));
407 if (bdrv_set_key(bs, password) == 0)
408 break;
409 term_printf("invalid password\n");
414 static void do_screen_dump(const char *filename)
416 vga_hw_screen_dump(filename);
419 static void do_log(const char *items)
421 int mask;
423 if (!strcmp(items, "none")) {
424 mask = 0;
425 } else {
426 mask = cpu_str_to_log_mask(items);
427 if (!mask) {
428 help_cmd("log");
429 return;
432 cpu_set_log(mask);
435 static void do_savevm(const char *filename)
437 if (qemu_savevm(filename, &qemu_savevm_method_file) < 0)
438 term_printf("I/O error when saving VM to '%s'\n", filename);
441 static void do_loadvm(const char *filename)
443 if (qemu_loadvm(filename, &qemu_savevm_method_file) < 0)
444 term_printf("I/O error when loading VM from '%s'\n", filename);
447 static void do_stop(void)
449 vm_stop(EXCP_INTERRUPT);
452 static void do_cont(void)
454 vm_start();
457 #ifdef CONFIG_GDBSTUB
458 static void do_gdbserver(int has_port, int port)
460 if (!has_port)
461 port = DEFAULT_GDBSTUB_PORT;
462 if (gdbserver_start(port) < 0) {
463 qemu_printf("Could not open gdbserver socket on port %d\n", port);
464 } else {
465 qemu_printf("Waiting gdb connection on port %d\n", port);
468 #endif
470 static void term_printc(int c)
472 term_printf("'");
473 switch(c) {
474 case '\'':
475 term_printf("\\'");
476 break;
477 case '\\':
478 term_printf("\\\\");
479 break;
480 case '\n':
481 term_printf("\\n");
482 break;
483 case '\r':
484 term_printf("\\r");
485 break;
486 default:
487 if (c >= 32 && c <= 126) {
488 term_printf("%c", c);
489 } else {
490 term_printf("\\x%02x", c);
492 break;
494 term_printf("'");
497 static void memory_dump(int count, int format, int wsize,
498 target_ulong addr, int is_physical)
500 CPUState *env;
501 int nb_per_line, l, line_size, i, max_digits, len;
502 uint8_t buf[16];
503 uint64_t v;
505 if (format == 'i') {
506 int flags;
507 flags = 0;
508 env = mon_get_cpu();
509 if (!env && !is_physical)
510 return;
511 #ifdef TARGET_I386
512 if (wsize == 2) {
513 flags = 1;
514 } else if (wsize == 4) {
515 flags = 0;
516 } else {
517 /* as default we use the current CS size */
518 flags = 0;
519 if (env) {
520 #ifdef TARGET_X86_64
521 if ((env->efer & MSR_EFER_LMA) &&
522 (env->segs[R_CS].flags & DESC_L_MASK))
523 flags = 2;
524 else
525 #endif
526 if (!(env->segs[R_CS].flags & DESC_B_MASK))
527 flags = 1;
530 #endif
531 monitor_disas(env, addr, count, is_physical, flags);
532 return;
535 len = wsize * count;
536 if (wsize == 1)
537 line_size = 8;
538 else
539 line_size = 16;
540 nb_per_line = line_size / wsize;
541 max_digits = 0;
543 switch(format) {
544 case 'o':
545 max_digits = (wsize * 8 + 2) / 3;
546 break;
547 default:
548 case 'x':
549 max_digits = (wsize * 8) / 4;
550 break;
551 case 'u':
552 case 'd':
553 max_digits = (wsize * 8 * 10 + 32) / 33;
554 break;
555 case 'c':
556 wsize = 1;
557 break;
560 while (len > 0) {
561 term_printf(TARGET_FMT_lx ":", addr);
562 l = len;
563 if (l > line_size)
564 l = line_size;
565 if (is_physical) {
566 cpu_physical_memory_rw(addr, buf, l, 0);
567 } else {
568 env = mon_get_cpu();
569 if (!env)
570 break;
571 cpu_memory_rw_debug(env, addr, buf, l, 0);
573 i = 0;
574 while (i < l) {
575 switch(wsize) {
576 default:
577 case 1:
578 v = ldub_raw(buf + i);
579 break;
580 case 2:
581 v = lduw_raw(buf + i);
582 break;
583 case 4:
584 v = (uint32_t)ldl_raw(buf + i);
585 break;
586 case 8:
587 v = ldq_raw(buf + i);
588 break;
590 term_printf(" ");
591 switch(format) {
592 case 'o':
593 term_printf("%#*" PRIo64, max_digits, v);
594 break;
595 case 'x':
596 term_printf("0x%0*" PRIx64, max_digits, v);
597 break;
598 case 'u':
599 term_printf("%*" PRIu64, max_digits, v);
600 break;
601 case 'd':
602 term_printf("%*" PRId64, max_digits, v);
603 break;
604 case 'c':
605 term_printc(v);
606 break;
608 i += wsize;
610 term_printf("\n");
611 addr += l;
612 len -= l;
616 #if TARGET_LONG_BITS == 64
617 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
618 #else
619 #define GET_TLONG(h, l) (l)
620 #endif
622 static void do_memory_dump(int count, int format, int size,
623 uint32_t addrh, uint32_t addrl)
625 target_long addr = GET_TLONG(addrh, addrl);
626 memory_dump(count, format, size, addr, 0);
629 static void do_physical_memory_dump(int count, int format, int size,
630 uint32_t addrh, uint32_t addrl)
633 target_long addr = GET_TLONG(addrh, addrl);
634 memory_dump(count, format, size, addr, 1);
637 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
639 target_long val = GET_TLONG(valh, vall);
640 #if TARGET_LONG_BITS == 32
641 switch(format) {
642 case 'o':
643 term_printf("%#o", val);
644 break;
645 case 'x':
646 term_printf("%#x", val);
647 break;
648 case 'u':
649 term_printf("%u", val);
650 break;
651 default:
652 case 'd':
653 term_printf("%d", val);
654 break;
655 case 'c':
656 term_printc(val);
657 break;
659 #else
660 switch(format) {
661 case 'o':
662 term_printf("%#" PRIo64, val);
663 break;
664 case 'x':
665 term_printf("%#" PRIx64, val);
666 break;
667 case 'u':
668 term_printf("%" PRIu64, val);
669 break;
670 default:
671 case 'd':
672 term_printf("%" PRId64, val);
673 break;
674 case 'c':
675 term_printc(val);
676 break;
678 #endif
679 term_printf("\n");
682 static void do_sum(uint32_t start, uint32_t size)
684 uint32_t addr;
685 uint8_t buf[1];
686 uint16_t sum;
688 sum = 0;
689 for(addr = start; addr < (start + size); addr++) {
690 cpu_physical_memory_rw(addr, buf, 1, 0);
691 /* BSD sum algorithm ('sum' Unix command) */
692 sum = (sum >> 1) | (sum << 15);
693 sum += buf[0];
695 term_printf("%05d\n", sum);
698 typedef struct {
699 int keycode;
700 const char *name;
701 } KeyDef;
703 static const KeyDef key_defs[] = {
704 { 0x2a, "shift" },
705 { 0x36, "shift_r" },
707 { 0x38, "alt" },
708 { 0xb8, "alt_r" },
709 { 0x1d, "ctrl" },
710 { 0x9d, "ctrl_r" },
712 { 0xdd, "menu" },
714 { 0x01, "esc" },
716 { 0x02, "1" },
717 { 0x03, "2" },
718 { 0x04, "3" },
719 { 0x05, "4" },
720 { 0x06, "5" },
721 { 0x07, "6" },
722 { 0x08, "7" },
723 { 0x09, "8" },
724 { 0x0a, "9" },
725 { 0x0b, "0" },
726 { 0x0c, "minus" },
727 { 0x0d, "equal" },
728 { 0x0e, "backspace" },
730 { 0x0f, "tab" },
731 { 0x10, "q" },
732 { 0x11, "w" },
733 { 0x12, "e" },
734 { 0x13, "r" },
735 { 0x14, "t" },
736 { 0x15, "y" },
737 { 0x16, "u" },
738 { 0x17, "i" },
739 { 0x18, "o" },
740 { 0x19, "p" },
742 { 0x1c, "ret" },
744 { 0x1e, "a" },
745 { 0x1f, "s" },
746 { 0x20, "d" },
747 { 0x21, "f" },
748 { 0x22, "g" },
749 { 0x23, "h" },
750 { 0x24, "j" },
751 { 0x25, "k" },
752 { 0x26, "l" },
754 { 0x2c, "z" },
755 { 0x2d, "x" },
756 { 0x2e, "c" },
757 { 0x2f, "v" },
758 { 0x30, "b" },
759 { 0x31, "n" },
760 { 0x32, "m" },
762 { 0x39, "spc" },
763 { 0x3a, "caps_lock" },
764 { 0x3b, "f1" },
765 { 0x3c, "f2" },
766 { 0x3d, "f3" },
767 { 0x3e, "f4" },
768 { 0x3f, "f5" },
769 { 0x40, "f6" },
770 { 0x41, "f7" },
771 { 0x42, "f8" },
772 { 0x43, "f9" },
773 { 0x44, "f10" },
774 { 0x45, "num_lock" },
775 { 0x46, "scroll_lock" },
777 { 0xb5, "kp_divide" },
778 { 0x37, "kp_multiply" },
779 { 0x4a, "kp_substract" },
780 { 0x4e, "kp_add" },
781 { 0x9c, "kp_enter" },
782 { 0x53, "kp_decimal" },
784 { 0x52, "kp_0" },
785 { 0x4f, "kp_1" },
786 { 0x50, "kp_2" },
787 { 0x51, "kp_3" },
788 { 0x4b, "kp_4" },
789 { 0x4c, "kp_5" },
790 { 0x4d, "kp_6" },
791 { 0x47, "kp_7" },
792 { 0x48, "kp_8" },
793 { 0x49, "kp_9" },
795 { 0x56, "<" },
797 { 0x57, "f11" },
798 { 0x58, "f12" },
800 { 0xb7, "print" },
802 { 0xc7, "home" },
803 { 0xc9, "pgup" },
804 { 0xd1, "pgdn" },
805 { 0xcf, "end" },
807 { 0xcb, "left" },
808 { 0xc8, "up" },
809 { 0xd0, "down" },
810 { 0xcd, "right" },
812 { 0xd2, "insert" },
813 { 0xd3, "delete" },
814 { 0, NULL },
817 static int get_keycode(const char *key)
819 const KeyDef *p;
820 char *endp;
821 int ret;
823 for(p = key_defs; p->name != NULL; p++) {
824 if (!strcmp(key, p->name))
825 return p->keycode;
827 if (strstart(key, "0x", NULL)) {
828 ret = strtoul(key, &endp, 0);
829 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
830 return ret;
832 return -1;
835 static void do_send_key(const char *string)
837 char keybuf[16], *q;
838 uint8_t keycodes[16];
839 const char *p;
840 int nb_keycodes, keycode, i;
842 nb_keycodes = 0;
843 p = string;
844 while (*p != '\0') {
845 q = keybuf;
846 while (*p != '\0' && *p != '-') {
847 if ((q - keybuf) < sizeof(keybuf) - 1) {
848 *q++ = *p;
850 p++;
852 *q = '\0';
853 keycode = get_keycode(keybuf);
854 if (keycode < 0) {
855 term_printf("unknown key: '%s'\n", keybuf);
856 return;
858 keycodes[nb_keycodes++] = keycode;
859 if (*p == '\0')
860 break;
861 p++;
863 /* key down events */
864 for(i = 0; i < nb_keycodes; i++) {
865 keycode = keycodes[i];
866 if (keycode & 0x80)
867 kbd_put_keycode(0xe0);
868 kbd_put_keycode(keycode & 0x7f);
870 /* key up events */
871 for(i = nb_keycodes - 1; i >= 0; i--) {
872 keycode = keycodes[i];
873 if (keycode & 0x80)
874 kbd_put_keycode(0xe0);
875 kbd_put_keycode(keycode | 0x80);
879 static int mouse_button_state;
881 static void do_mouse_move(const char *dx_str, const char *dy_str,
882 const char *dz_str)
884 int dx, dy, dz;
885 dx = strtol(dx_str, NULL, 0);
886 dy = strtol(dy_str, NULL, 0);
887 dz = 0;
888 if (dz_str)
889 dz = strtol(dz_str, NULL, 0);
890 kbd_mouse_event(dx, dy, dz, mouse_button_state);
893 static void do_mouse_button(int button_state)
895 mouse_button_state = button_state;
896 kbd_mouse_event(0, 0, 0, mouse_button_state);
899 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
901 uint32_t val;
902 int suffix;
904 if (has_index) {
905 cpu_outb(NULL, addr & 0xffff, index & 0xff);
906 addr++;
908 addr &= 0xffff;
910 switch(size) {
911 default:
912 case 1:
913 val = cpu_inb(NULL, addr);
914 suffix = 'b';
915 break;
916 case 2:
917 val = cpu_inw(NULL, addr);
918 suffix = 'w';
919 break;
920 case 4:
921 val = cpu_inl(NULL, addr);
922 suffix = 'l';
923 break;
925 term_printf("port%c[0x%04x] = %#0*x\n",
926 suffix, addr, size * 2, val);
929 static void do_system_reset(void)
931 qemu_system_reset_request();
934 static void do_system_powerdown(void)
936 qemu_system_powerdown_request();
939 #if defined(TARGET_I386)
940 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
942 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
943 addr,
944 pte & mask,
945 pte & PG_GLOBAL_MASK ? 'G' : '-',
946 pte & PG_PSE_MASK ? 'P' : '-',
947 pte & PG_DIRTY_MASK ? 'D' : '-',
948 pte & PG_ACCESSED_MASK ? 'A' : '-',
949 pte & PG_PCD_MASK ? 'C' : '-',
950 pte & PG_PWT_MASK ? 'T' : '-',
951 pte & PG_USER_MASK ? 'U' : '-',
952 pte & PG_RW_MASK ? 'W' : '-');
955 static void tlb_info(void)
957 CPUState *env;
958 int l1, l2;
959 uint32_t pgd, pde, pte;
961 env = mon_get_cpu();
962 if (!env)
963 return;
965 if (!(env->cr[0] & CR0_PG_MASK)) {
966 term_printf("PG disabled\n");
967 return;
969 pgd = env->cr[3] & ~0xfff;
970 for(l1 = 0; l1 < 1024; l1++) {
971 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
972 pde = le32_to_cpu(pde);
973 if (pde & PG_PRESENT_MASK) {
974 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
975 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
976 } else {
977 for(l2 = 0; l2 < 1024; l2++) {
978 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
979 (uint8_t *)&pte, 4);
980 pte = le32_to_cpu(pte);
981 if (pte & PG_PRESENT_MASK) {
982 print_pte((l1 << 22) + (l2 << 12),
983 pte & ~PG_PSE_MASK,
984 ~0xfff);
992 static void mem_print(uint32_t *pstart, int *plast_prot,
993 uint32_t end, int prot)
995 int prot1;
996 prot1 = *plast_prot;
997 if (prot != prot1) {
998 if (*pstart != -1) {
999 term_printf("%08x-%08x %08x %c%c%c\n",
1000 *pstart, end, end - *pstart,
1001 prot1 & PG_USER_MASK ? 'u' : '-',
1002 'r',
1003 prot1 & PG_RW_MASK ? 'w' : '-');
1005 if (prot != 0)
1006 *pstart = end;
1007 else
1008 *pstart = -1;
1009 *plast_prot = prot;
1013 static void mem_info(void)
1015 CPUState *env;
1016 int l1, l2, prot, last_prot;
1017 uint32_t pgd, pde, pte, start, end;
1019 env = mon_get_cpu();
1020 if (!env)
1021 return;
1023 if (!(env->cr[0] & CR0_PG_MASK)) {
1024 term_printf("PG disabled\n");
1025 return;
1027 pgd = env->cr[3] & ~0xfff;
1028 last_prot = 0;
1029 start = -1;
1030 for(l1 = 0; l1 < 1024; l1++) {
1031 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1032 pde = le32_to_cpu(pde);
1033 end = l1 << 22;
1034 if (pde & PG_PRESENT_MASK) {
1035 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1036 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1037 mem_print(&start, &last_prot, end, prot);
1038 } else {
1039 for(l2 = 0; l2 < 1024; l2++) {
1040 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1041 (uint8_t *)&pte, 4);
1042 pte = le32_to_cpu(pte);
1043 end = (l1 << 22) + (l2 << 12);
1044 if (pte & PG_PRESENT_MASK) {
1045 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1046 } else {
1047 prot = 0;
1049 mem_print(&start, &last_prot, end, prot);
1052 } else {
1053 prot = 0;
1054 mem_print(&start, &last_prot, end, prot);
1058 #endif
1060 static void do_info_kqemu(void)
1062 #ifdef USE_KQEMU
1063 CPUState *env;
1064 int val;
1065 val = 0;
1066 env = mon_get_cpu();
1067 if (!env) {
1068 term_printf("No cpu initialized yet");
1069 return;
1071 val = env->kqemu_enabled;
1072 term_printf("kqemu support: ");
1073 switch(val) {
1074 default:
1075 case 0:
1076 term_printf("disabled\n");
1077 break;
1078 case 1:
1079 term_printf("enabled for user code\n");
1080 break;
1081 case 2:
1082 term_printf("enabled for user and kernel code\n");
1083 break;
1085 #else
1086 term_printf("kqemu support: not compiled\n");
1087 #endif
1090 #ifdef CONFIG_PROFILER
1092 int64_t kqemu_time;
1093 int64_t qemu_time;
1094 int64_t kqemu_exec_count;
1095 int64_t dev_time;
1096 int64_t kqemu_ret_int_count;
1097 int64_t kqemu_ret_excp_count;
1098 int64_t kqemu_ret_intr_count;
1100 static void do_info_profile(void)
1102 int64_t total;
1103 total = qemu_time;
1104 if (total == 0)
1105 total = 1;
1106 term_printf("async time %" PRId64 " (%0.3f)\n",
1107 dev_time, dev_time / (double)ticks_per_sec);
1108 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1109 qemu_time, qemu_time / (double)ticks_per_sec);
1110 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1111 kqemu_time, kqemu_time / (double)ticks_per_sec,
1112 kqemu_time / (double)total * 100.0,
1113 kqemu_exec_count,
1114 kqemu_ret_int_count,
1115 kqemu_ret_excp_count,
1116 kqemu_ret_intr_count);
1117 qemu_time = 0;
1118 kqemu_time = 0;
1119 kqemu_exec_count = 0;
1120 dev_time = 0;
1121 kqemu_ret_int_count = 0;
1122 kqemu_ret_excp_count = 0;
1123 kqemu_ret_intr_count = 0;
1124 #ifdef USE_KQEMU
1125 kqemu_record_dump();
1126 #endif
1128 #else
1129 static void do_info_profile(void)
1131 term_printf("Internal profiler not compiled\n");
1133 #endif
1135 /* Capture support */
1136 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1138 static void do_info_capture (void)
1140 int i;
1141 CaptureState *s;
1143 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1144 term_printf ("[%d]: ", i);
1145 s->ops.info (s->opaque);
1149 static void do_stop_capture (int n)
1151 int i;
1152 CaptureState *s;
1154 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1155 if (i == n) {
1156 s->ops.destroy (s->opaque);
1157 LIST_REMOVE (s, entries);
1158 qemu_free (s);
1159 return;
1164 static void do_migration(const char *subcmdline)
1166 if (subcmdline[0] == '\0')
1167 subcmdline = "help";
1168 monitor_handle_command(migration_cmds, subcmdline);
1171 static void do_migration_help(const char *name)
1173 help_cmd1(migration_cmds, "migration ", name);
1176 static void do_migration_set(const char *subcmdline)
1178 if (subcmdline[0] == '\0')
1179 subcmdline = "help";
1180 monitor_handle_command(migration_set_cmds, subcmdline);
1183 static void do_migration_set_help(const char *name)
1185 help_cmd1(migration_set_cmds, "migration set ", name);
1188 #ifdef HAS_AUDIO
1189 int wav_start_capture (CaptureState *s, const char *path, int freq,
1190 int bits, int nchannels);
1192 static void do_wav_capture (const char *path,
1193 int has_freq, int freq,
1194 int has_bits, int bits,
1195 int has_channels, int nchannels)
1197 CaptureState *s;
1199 s = qemu_mallocz (sizeof (*s));
1200 if (!s) {
1201 term_printf ("Not enough memory to add wave capture\n");
1202 return;
1205 freq = has_freq ? freq : 44100;
1206 bits = has_bits ? bits : 16;
1207 nchannels = has_channels ? nchannels : 2;
1209 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1210 term_printf ("Faied to add wave capture\n");
1211 qemu_free (s);
1213 LIST_INSERT_HEAD (&capture_head, s, entries);
1215 #endif
1217 static term_cmd_t term_cmds[] = {
1218 { "help|?", "s?", do_help,
1219 "[cmd]", "show the help" },
1220 { "commit", "", do_commit,
1221 "", "commit changes to the disk images (if -snapshot is used)" },
1222 { "info", "s?", do_info,
1223 "subcommand", "show various information about the system state" },
1224 { "q|quit", "", do_quit,
1225 "", "quit the emulator" },
1226 { "eject", "-fB", do_eject,
1227 "[-f] device", "eject a removable media (use -f to force it)" },
1228 { "change", "BF", do_change,
1229 "device filename", "change a removable media" },
1230 { "screendump", "F", do_screen_dump,
1231 "filename", "save screen into PPM image 'filename'" },
1232 { "log", "s", do_log,
1233 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1234 { "savevm", "F", do_savevm,
1235 "filename", "save the whole virtual machine state to 'filename'" },
1236 { "loadvm", "F", do_loadvm,
1237 "filename", "restore the whole virtual machine state from 'filename'" },
1238 { "stop", "", do_stop,
1239 "", "stop emulation", },
1240 { "c|cont", "", do_cont,
1241 "", "resume emulation", },
1242 #ifdef CONFIG_GDBSTUB
1243 { "gdbserver", "i?", do_gdbserver,
1244 "[port]", "start gdbserver session (default port=1234)", },
1245 #endif
1246 { "x", "/l", do_memory_dump,
1247 "/fmt addr", "virtual memory dump starting at 'addr'", },
1248 { "xp", "/l", do_physical_memory_dump,
1249 "/fmt addr", "physical memory dump starting at 'addr'", },
1250 { "p|print", "/l", do_print,
1251 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1252 { "i", "/ii.", do_ioport_read,
1253 "/fmt addr", "I/O port read" },
1255 { "sendkey", "s", do_send_key,
1256 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1257 { "system_reset", "", do_system_reset,
1258 "", "reset the system" },
1259 { "system_powerdown", "", do_system_powerdown,
1260 "", "send system power down event" },
1261 { "sum", "ii", do_sum,
1262 "addr size", "compute the checksum of a memory region" },
1263 { "usb_add", "s", do_usb_add,
1264 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1265 { "usb_del", "s", do_usb_del,
1266 "device", "remove USB device 'bus.addr'" },
1267 { "cpu", "i", do_cpu_set,
1268 "index", "set the default CPU" },
1269 { "mouse_move", "sss?", do_mouse_move,
1270 "dx dy [dz]", "send mouse move events" },
1271 { "mouse_button", "i", do_mouse_button,
1272 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1273 #ifdef HAS_AUDIO
1274 { "wavcapture", "si?i?i?", do_wav_capture,
1275 "path [frequency bits channels]",
1276 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1277 #endif
1278 { "stopcapture", "i", do_stop_capture,
1279 "capture index", "stop capture" },
1280 { "create_snapshot", "ss?s?s?", do_snapshot,
1281 "hda [hdb] [hdc] [hdd]", "create snapshot of one or more images (VMDK format)" },
1282 { "migration", "A", do_migration, "subcommand|help",
1283 "start/stop/manage migrations"},
1284 { NULL, NULL, },
1287 static term_cmd_t info_cmds[] = {
1288 { "version", "", do_info_version,
1289 "", "show the version of qemu" },
1290 { "network", "", do_info_network,
1291 "", "show the network state" },
1292 { "block", "", do_info_block,
1293 "", "show the block devices" },
1294 { "registers", "", do_info_registers,
1295 "", "show the cpu registers" },
1296 { "cpus", "", do_info_cpus,
1297 "", "show infos for each CPU" },
1298 { "history", "", do_info_history,
1299 "", "show the command line history", },
1300 { "irq", "", irq_info,
1301 "", "show the interrupts statistics (if available)", },
1302 { "pic", "", pic_info,
1303 "", "show i8259 (PIC) state", },
1304 { "pci", "", pci_info,
1305 "", "show PCI info", },
1306 #if defined(TARGET_I386)
1307 { "tlb", "", tlb_info,
1308 "", "show virtual to physical memory mappings", },
1309 { "mem", "", mem_info,
1310 "", "show the active virtual memory mappings", },
1311 #endif
1312 { "jit", "", do_info_jit,
1313 "", "show dynamic compiler info", },
1314 { "kqemu", "", do_info_kqemu,
1315 "", "show kqemu information", },
1316 { "usb", "", usb_info,
1317 "", "show guest USB devices", },
1318 { "usbhost", "", usb_host_info,
1319 "", "show host USB devices", },
1320 { "profile", "", do_info_profile,
1321 "", "show profiling information", },
1322 { "capture", "", do_info_capture,
1323 "show capture information" },
1324 { NULL, NULL, },
1328 static term_cmd_t migration_cmds[] = {
1329 { "listen", "s?s?", do_migration_listen,
1330 "[local_host:port [remote_host:port]]", "listen to a port" },
1331 { "connect", "s?s?", do_migration_connect,
1332 "[local_host:port [remote_host:port]]", "connect to a port"},
1333 { "getfd", "i", do_migration_getfd, "fd (socket)",
1334 "get established connection"},
1335 { "start", "s", do_migration_start, "online|offline" ,
1336 "start the migration proccess"},
1337 { "cancel", "", do_migration_cancel, "",
1338 "cancel an ongoing migration procces"},
1339 { "status", "", do_migration_status, "", "get migration status/progress"},
1340 { "set", "A", do_migration_set, "params", "set migration parameters"},
1341 { "show", "", do_migration_show, "", "show migration parameters"},
1342 { "help", "s?", do_migration_help, "[subcommand]", "show help message"},
1343 { NULL, NULL, },
1346 static term_cmd_t migration_set_cmds[] = {
1347 { "rate", "iii", do_migration_set_rate, "min max offline", "bandwidth params" },
1348 { "total_time", "i", do_migration_set_total_time, "seconds", "max migration time"},
1349 { "help", "s?", do_migration_set_help, "[subcommand]", "show help message"},
1350 { NULL, NULL, }
1353 /*******************************************************************/
1355 static const char *pch;
1356 static jmp_buf expr_env;
1358 #define MD_TLONG 0
1359 #define MD_I32 1
1361 typedef struct MonitorDef {
1362 const char *name;
1363 int offset;
1364 target_long (*get_value)(struct MonitorDef *md, int val);
1365 int type;
1366 } MonitorDef;
1368 #if defined(TARGET_I386)
1369 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1371 CPUState *env = mon_get_cpu();
1372 if (!env)
1373 return 0;
1374 return env->eip + env->segs[R_CS].base;
1376 #endif
1378 #if defined(TARGET_PPC)
1379 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1381 CPUState *env = mon_get_cpu();
1382 unsigned int u;
1383 int i;
1385 if (!env)
1386 return 0;
1388 u = 0;
1389 for (i = 0; i < 8; i++)
1390 u |= env->crf[i] << (32 - (4 * i));
1392 return u;
1395 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1397 CPUState *env = mon_get_cpu();
1398 if (!env)
1399 return 0;
1400 return (env->msr[MSR_POW] << MSR_POW) |
1401 (env->msr[MSR_ILE] << MSR_ILE) |
1402 (env->msr[MSR_EE] << MSR_EE) |
1403 (env->msr[MSR_PR] << MSR_PR) |
1404 (env->msr[MSR_FP] << MSR_FP) |
1405 (env->msr[MSR_ME] << MSR_ME) |
1406 (env->msr[MSR_FE0] << MSR_FE0) |
1407 (env->msr[MSR_SE] << MSR_SE) |
1408 (env->msr[MSR_BE] << MSR_BE) |
1409 (env->msr[MSR_FE1] << MSR_FE1) |
1410 (env->msr[MSR_IP] << MSR_IP) |
1411 (env->msr[MSR_IR] << MSR_IR) |
1412 (env->msr[MSR_DR] << MSR_DR) |
1413 (env->msr[MSR_RI] << MSR_RI) |
1414 (env->msr[MSR_LE] << MSR_LE);
1417 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1419 CPUState *env = mon_get_cpu();
1420 if (!env)
1421 return 0;
1422 return (env->xer[XER_SO] << XER_SO) |
1423 (env->xer[XER_OV] << XER_OV) |
1424 (env->xer[XER_CA] << XER_CA) |
1425 (env->xer[XER_BC] << XER_BC);
1428 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1430 CPUState *env = mon_get_cpu();
1431 if (!env)
1432 return 0;
1433 return cpu_ppc_load_decr(env);
1436 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1438 CPUState *env = mon_get_cpu();
1439 if (!env)
1440 return 0;
1441 return cpu_ppc_load_tbu(env);
1444 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1446 CPUState *env = mon_get_cpu();
1447 if (!env)
1448 return 0;
1449 return cpu_ppc_load_tbl(env);
1451 #endif
1453 #if defined(TARGET_SPARC)
1454 #ifndef TARGET_SPARC64
1455 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1457 CPUState *env = mon_get_cpu();
1458 if (!env)
1459 return 0;
1460 return GET_PSR(env);
1462 #endif
1464 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1466 CPUState *env = mon_get_cpu();
1467 if (!env)
1468 return 0;
1469 return env->regwptr[val];
1471 #endif
1473 static MonitorDef monitor_defs[] = {
1474 #ifdef TARGET_I386
1476 #define SEG(name, seg) \
1477 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1478 { name ".base", offsetof(CPUState, segs[seg].base) },\
1479 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1481 { "eax", offsetof(CPUState, regs[0]) },
1482 { "ecx", offsetof(CPUState, regs[1]) },
1483 { "edx", offsetof(CPUState, regs[2]) },
1484 { "ebx", offsetof(CPUState, regs[3]) },
1485 { "esp|sp", offsetof(CPUState, regs[4]) },
1486 { "ebp|fp", offsetof(CPUState, regs[5]) },
1487 { "esi", offsetof(CPUState, regs[6]) },
1488 { "edi", offsetof(CPUState, regs[7]) },
1489 #ifdef TARGET_X86_64
1490 { "r8", offsetof(CPUState, regs[8]) },
1491 { "r9", offsetof(CPUState, regs[9]) },
1492 { "r10", offsetof(CPUState, regs[10]) },
1493 { "r11", offsetof(CPUState, regs[11]) },
1494 { "r12", offsetof(CPUState, regs[12]) },
1495 { "r13", offsetof(CPUState, regs[13]) },
1496 { "r14", offsetof(CPUState, regs[14]) },
1497 { "r15", offsetof(CPUState, regs[15]) },
1498 #endif
1499 { "eflags", offsetof(CPUState, eflags) },
1500 { "eip", offsetof(CPUState, eip) },
1501 SEG("cs", R_CS)
1502 SEG("ds", R_DS)
1503 SEG("es", R_ES)
1504 SEG("ss", R_SS)
1505 SEG("fs", R_FS)
1506 SEG("gs", R_GS)
1507 { "pc", 0, monitor_get_pc, },
1508 #elif defined(TARGET_PPC)
1509 { "r0", offsetof(CPUState, gpr[0]) },
1510 { "r1", offsetof(CPUState, gpr[1]) },
1511 { "r2", offsetof(CPUState, gpr[2]) },
1512 { "r3", offsetof(CPUState, gpr[3]) },
1513 { "r4", offsetof(CPUState, gpr[4]) },
1514 { "r5", offsetof(CPUState, gpr[5]) },
1515 { "r6", offsetof(CPUState, gpr[6]) },
1516 { "r7", offsetof(CPUState, gpr[7]) },
1517 { "r8", offsetof(CPUState, gpr[8]) },
1518 { "r9", offsetof(CPUState, gpr[9]) },
1519 { "r10", offsetof(CPUState, gpr[10]) },
1520 { "r11", offsetof(CPUState, gpr[11]) },
1521 { "r12", offsetof(CPUState, gpr[12]) },
1522 { "r13", offsetof(CPUState, gpr[13]) },
1523 { "r14", offsetof(CPUState, gpr[14]) },
1524 { "r15", offsetof(CPUState, gpr[15]) },
1525 { "r16", offsetof(CPUState, gpr[16]) },
1526 { "r17", offsetof(CPUState, gpr[17]) },
1527 { "r18", offsetof(CPUState, gpr[18]) },
1528 { "r19", offsetof(CPUState, gpr[19]) },
1529 { "r20", offsetof(CPUState, gpr[20]) },
1530 { "r21", offsetof(CPUState, gpr[21]) },
1531 { "r22", offsetof(CPUState, gpr[22]) },
1532 { "r23", offsetof(CPUState, gpr[23]) },
1533 { "r24", offsetof(CPUState, gpr[24]) },
1534 { "r25", offsetof(CPUState, gpr[25]) },
1535 { "r26", offsetof(CPUState, gpr[26]) },
1536 { "r27", offsetof(CPUState, gpr[27]) },
1537 { "r28", offsetof(CPUState, gpr[28]) },
1538 { "r29", offsetof(CPUState, gpr[29]) },
1539 { "r30", offsetof(CPUState, gpr[30]) },
1540 { "r31", offsetof(CPUState, gpr[31]) },
1541 { "nip|pc", offsetof(CPUState, nip) },
1542 { "lr", offsetof(CPUState, lr) },
1543 { "ctr", offsetof(CPUState, ctr) },
1544 { "decr", 0, &monitor_get_decr, },
1545 { "ccr", 0, &monitor_get_ccr, },
1546 { "msr", 0, &monitor_get_msr, },
1547 { "xer", 0, &monitor_get_xer, },
1548 { "tbu", 0, &monitor_get_tbu, },
1549 { "tbl", 0, &monitor_get_tbl, },
1550 { "sdr1", offsetof(CPUState, sdr1) },
1551 { "sr0", offsetof(CPUState, sr[0]) },
1552 { "sr1", offsetof(CPUState, sr[1]) },
1553 { "sr2", offsetof(CPUState, sr[2]) },
1554 { "sr3", offsetof(CPUState, sr[3]) },
1555 { "sr4", offsetof(CPUState, sr[4]) },
1556 { "sr5", offsetof(CPUState, sr[5]) },
1557 { "sr6", offsetof(CPUState, sr[6]) },
1558 { "sr7", offsetof(CPUState, sr[7]) },
1559 { "sr8", offsetof(CPUState, sr[8]) },
1560 { "sr9", offsetof(CPUState, sr[9]) },
1561 { "sr10", offsetof(CPUState, sr[10]) },
1562 { "sr11", offsetof(CPUState, sr[11]) },
1563 { "sr12", offsetof(CPUState, sr[12]) },
1564 { "sr13", offsetof(CPUState, sr[13]) },
1565 { "sr14", offsetof(CPUState, sr[14]) },
1566 { "sr15", offsetof(CPUState, sr[15]) },
1567 /* Too lazy to put BATs and SPRs ... */
1568 #elif defined(TARGET_SPARC)
1569 { "g0", offsetof(CPUState, gregs[0]) },
1570 { "g1", offsetof(CPUState, gregs[1]) },
1571 { "g2", offsetof(CPUState, gregs[2]) },
1572 { "g3", offsetof(CPUState, gregs[3]) },
1573 { "g4", offsetof(CPUState, gregs[4]) },
1574 { "g5", offsetof(CPUState, gregs[5]) },
1575 { "g6", offsetof(CPUState, gregs[6]) },
1576 { "g7", offsetof(CPUState, gregs[7]) },
1577 { "o0", 0, monitor_get_reg },
1578 { "o1", 1, monitor_get_reg },
1579 { "o2", 2, monitor_get_reg },
1580 { "o3", 3, monitor_get_reg },
1581 { "o4", 4, monitor_get_reg },
1582 { "o5", 5, monitor_get_reg },
1583 { "o6", 6, monitor_get_reg },
1584 { "o7", 7, monitor_get_reg },
1585 { "l0", 8, monitor_get_reg },
1586 { "l1", 9, monitor_get_reg },
1587 { "l2", 10, monitor_get_reg },
1588 { "l3", 11, monitor_get_reg },
1589 { "l4", 12, monitor_get_reg },
1590 { "l5", 13, monitor_get_reg },
1591 { "l6", 14, monitor_get_reg },
1592 { "l7", 15, monitor_get_reg },
1593 { "i0", 16, monitor_get_reg },
1594 { "i1", 17, monitor_get_reg },
1595 { "i2", 18, monitor_get_reg },
1596 { "i3", 19, monitor_get_reg },
1597 { "i4", 20, monitor_get_reg },
1598 { "i5", 21, monitor_get_reg },
1599 { "i6", 22, monitor_get_reg },
1600 { "i7", 23, monitor_get_reg },
1601 { "pc", offsetof(CPUState, pc) },
1602 { "npc", offsetof(CPUState, npc) },
1603 { "y", offsetof(CPUState, y) },
1604 #ifndef TARGET_SPARC64
1605 { "psr", 0, &monitor_get_psr, },
1606 { "wim", offsetof(CPUState, wim) },
1607 #endif
1608 { "tbr", offsetof(CPUState, tbr) },
1609 { "fsr", offsetof(CPUState, fsr) },
1610 { "f0", offsetof(CPUState, fpr[0]) },
1611 { "f1", offsetof(CPUState, fpr[1]) },
1612 { "f2", offsetof(CPUState, fpr[2]) },
1613 { "f3", offsetof(CPUState, fpr[3]) },
1614 { "f4", offsetof(CPUState, fpr[4]) },
1615 { "f5", offsetof(CPUState, fpr[5]) },
1616 { "f6", offsetof(CPUState, fpr[6]) },
1617 { "f7", offsetof(CPUState, fpr[7]) },
1618 { "f8", offsetof(CPUState, fpr[8]) },
1619 { "f9", offsetof(CPUState, fpr[9]) },
1620 { "f10", offsetof(CPUState, fpr[10]) },
1621 { "f11", offsetof(CPUState, fpr[11]) },
1622 { "f12", offsetof(CPUState, fpr[12]) },
1623 { "f13", offsetof(CPUState, fpr[13]) },
1624 { "f14", offsetof(CPUState, fpr[14]) },
1625 { "f15", offsetof(CPUState, fpr[15]) },
1626 { "f16", offsetof(CPUState, fpr[16]) },
1627 { "f17", offsetof(CPUState, fpr[17]) },
1628 { "f18", offsetof(CPUState, fpr[18]) },
1629 { "f19", offsetof(CPUState, fpr[19]) },
1630 { "f20", offsetof(CPUState, fpr[20]) },
1631 { "f21", offsetof(CPUState, fpr[21]) },
1632 { "f22", offsetof(CPUState, fpr[22]) },
1633 { "f23", offsetof(CPUState, fpr[23]) },
1634 { "f24", offsetof(CPUState, fpr[24]) },
1635 { "f25", offsetof(CPUState, fpr[25]) },
1636 { "f26", offsetof(CPUState, fpr[26]) },
1637 { "f27", offsetof(CPUState, fpr[27]) },
1638 { "f28", offsetof(CPUState, fpr[28]) },
1639 { "f29", offsetof(CPUState, fpr[29]) },
1640 { "f30", offsetof(CPUState, fpr[30]) },
1641 { "f31", offsetof(CPUState, fpr[31]) },
1642 #ifdef TARGET_SPARC64
1643 { "f32", offsetof(CPUState, fpr[32]) },
1644 { "f34", offsetof(CPUState, fpr[34]) },
1645 { "f36", offsetof(CPUState, fpr[36]) },
1646 { "f38", offsetof(CPUState, fpr[38]) },
1647 { "f40", offsetof(CPUState, fpr[40]) },
1648 { "f42", offsetof(CPUState, fpr[42]) },
1649 { "f44", offsetof(CPUState, fpr[44]) },
1650 { "f46", offsetof(CPUState, fpr[46]) },
1651 { "f48", offsetof(CPUState, fpr[48]) },
1652 { "f50", offsetof(CPUState, fpr[50]) },
1653 { "f52", offsetof(CPUState, fpr[52]) },
1654 { "f54", offsetof(CPUState, fpr[54]) },
1655 { "f56", offsetof(CPUState, fpr[56]) },
1656 { "f58", offsetof(CPUState, fpr[58]) },
1657 { "f60", offsetof(CPUState, fpr[60]) },
1658 { "f62", offsetof(CPUState, fpr[62]) },
1659 { "asi", offsetof(CPUState, asi) },
1660 { "pstate", offsetof(CPUState, pstate) },
1661 { "cansave", offsetof(CPUState, cansave) },
1662 { "canrestore", offsetof(CPUState, canrestore) },
1663 { "otherwin", offsetof(CPUState, otherwin) },
1664 { "wstate", offsetof(CPUState, wstate) },
1665 { "cleanwin", offsetof(CPUState, cleanwin) },
1666 { "fprs", offsetof(CPUState, fprs) },
1667 #endif
1668 #endif
1669 { NULL },
1672 static void expr_error(const char *fmt)
1674 term_printf(fmt);
1675 term_printf("\n");
1676 longjmp(expr_env, 1);
1679 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1680 static int get_monitor_def(target_long *pval, const char *name)
1682 MonitorDef *md;
1683 void *ptr;
1685 for(md = monitor_defs; md->name != NULL; md++) {
1686 if (compare_cmd(name, md->name)) {
1687 if (md->get_value) {
1688 *pval = md->get_value(md, md->offset);
1689 } else {
1690 CPUState *env = mon_get_cpu();
1691 if (!env)
1692 return -2;
1693 ptr = (uint8_t *)env + md->offset;
1694 switch(md->type) {
1695 case MD_I32:
1696 *pval = *(int32_t *)ptr;
1697 break;
1698 case MD_TLONG:
1699 *pval = *(target_long *)ptr;
1700 break;
1701 default:
1702 *pval = 0;
1703 break;
1706 return 0;
1709 return -1;
1712 static void next(void)
1714 if (pch != '\0') {
1715 pch++;
1716 while (isspace(*pch))
1717 pch++;
1721 static target_long expr_sum(void);
1723 static target_long expr_unary(void)
1725 target_long n;
1726 char *p;
1727 int ret;
1729 switch(*pch) {
1730 case '+':
1731 next();
1732 n = expr_unary();
1733 break;
1734 case '-':
1735 next();
1736 n = -expr_unary();
1737 break;
1738 case '~':
1739 next();
1740 n = ~expr_unary();
1741 break;
1742 case '(':
1743 next();
1744 n = expr_sum();
1745 if (*pch != ')') {
1746 expr_error("')' expected");
1748 next();
1749 break;
1750 case '\'':
1751 pch++;
1752 if (*pch == '\0')
1753 expr_error("character constant expected");
1754 n = *pch;
1755 pch++;
1756 if (*pch != '\'')
1757 expr_error("missing terminating \' character");
1758 next();
1759 break;
1760 case '$':
1762 char buf[128], *q;
1764 pch++;
1765 q = buf;
1766 while ((*pch >= 'a' && *pch <= 'z') ||
1767 (*pch >= 'A' && *pch <= 'Z') ||
1768 (*pch >= '0' && *pch <= '9') ||
1769 *pch == '_' || *pch == '.') {
1770 if ((q - buf) < sizeof(buf) - 1)
1771 *q++ = *pch;
1772 pch++;
1774 while (isspace(*pch))
1775 pch++;
1776 *q = 0;
1777 ret = get_monitor_def(&n, buf);
1778 if (ret == -1)
1779 expr_error("unknown register");
1780 else if (ret == -2)
1781 expr_error("no cpu defined");
1783 break;
1784 case '\0':
1785 expr_error("unexpected end of expression");
1786 n = 0;
1787 break;
1788 default:
1789 #if TARGET_LONG_BITS == 64
1790 n = strtoull(pch, &p, 0);
1791 #else
1792 n = strtoul(pch, &p, 0);
1793 #endif
1794 if (pch == p) {
1795 expr_error("invalid char in expression");
1797 pch = p;
1798 while (isspace(*pch))
1799 pch++;
1800 break;
1802 return n;
1806 static target_long expr_prod(void)
1808 target_long val, val2;
1809 int op;
1811 val = expr_unary();
1812 for(;;) {
1813 op = *pch;
1814 if (op != '*' && op != '/' && op != '%')
1815 break;
1816 next();
1817 val2 = expr_unary();
1818 switch(op) {
1819 default:
1820 case '*':
1821 val *= val2;
1822 break;
1823 case '/':
1824 case '%':
1825 if (val2 == 0)
1826 expr_error("division by zero");
1827 if (op == '/')
1828 val /= val2;
1829 else
1830 val %= val2;
1831 break;
1834 return val;
1837 static target_long expr_logic(void)
1839 target_long val, val2;
1840 int op;
1842 val = expr_prod();
1843 for(;;) {
1844 op = *pch;
1845 if (op != '&' && op != '|' && op != '^')
1846 break;
1847 next();
1848 val2 = expr_prod();
1849 switch(op) {
1850 default:
1851 case '&':
1852 val &= val2;
1853 break;
1854 case '|':
1855 val |= val2;
1856 break;
1857 case '^':
1858 val ^= val2;
1859 break;
1862 return val;
1865 static target_long expr_sum(void)
1867 target_long val, val2;
1868 int op;
1870 val = expr_logic();
1871 for(;;) {
1872 op = *pch;
1873 if (op != '+' && op != '-')
1874 break;
1875 next();
1876 val2 = expr_logic();
1877 if (op == '+')
1878 val += val2;
1879 else
1880 val -= val2;
1882 return val;
1885 static int get_expr(target_long *pval, const char **pp)
1887 pch = *pp;
1888 if (setjmp(expr_env)) {
1889 *pp = pch;
1890 return -1;
1892 while (isspace(*pch))
1893 pch++;
1894 *pval = expr_sum();
1895 *pp = pch;
1896 return 0;
1899 static int get_str(char *buf, int buf_size, const char **pp)
1901 const char *p;
1902 char *q;
1903 int c;
1905 q = buf;
1906 p = *pp;
1907 while (isspace(*p))
1908 p++;
1909 if (*p == '\0') {
1910 fail:
1911 *q = '\0';
1912 *pp = p;
1913 return -1;
1915 if (*p == '\"') {
1916 p++;
1917 while (*p != '\0' && *p != '\"') {
1918 if (*p == '\\') {
1919 p++;
1920 c = *p++;
1921 switch(c) {
1922 case 'n':
1923 c = '\n';
1924 break;
1925 case 'r':
1926 c = '\r';
1927 break;
1928 case '\\':
1929 case '\'':
1930 case '\"':
1931 break;
1932 default:
1933 qemu_printf("unsupported escape code: '\\%c'\n", c);
1934 goto fail;
1936 if ((q - buf) < buf_size - 1) {
1937 *q++ = c;
1939 } else {
1940 if ((q - buf) < buf_size - 1) {
1941 *q++ = *p;
1943 p++;
1946 if (*p != '\"') {
1947 qemu_printf("unterminated string\n");
1948 goto fail;
1950 p++;
1951 } else {
1952 while (*p != '\0' && !isspace(*p)) {
1953 if ((q - buf) < buf_size - 1) {
1954 *q++ = *p;
1956 p++;
1959 *q = '\0';
1960 *pp = p;
1961 return 0;
1964 static int default_fmt_format = 'x';
1965 static int default_fmt_size = 4;
1967 #define MAX_ARGS 16
1969 static void monitor_handle_command(const term_cmd_t *cmds, const char *cmdline)
1971 const char *p, *pstart, *typestr;
1972 char *q;
1973 int c, nb_args, len, i, has_arg;
1974 term_cmd_t *cmd;
1975 char cmdname[256];
1976 char buf[1024];
1977 void *str_allocated[MAX_ARGS];
1978 void *args[MAX_ARGS];
1980 #ifdef DEBUG
1981 term_printf("command='%s'\n", cmdline);
1982 #endif
1984 /* extract the command name */
1985 p = cmdline;
1986 q = cmdname;
1987 while (isspace(*p))
1988 p++;
1989 if (*p == '\0')
1990 return;
1991 pstart = p;
1992 while (*p != '\0' && *p != '/' && !isspace(*p))
1993 p++;
1994 len = p - pstart;
1995 if (len > sizeof(cmdname) - 1)
1996 len = sizeof(cmdname) - 1;
1997 memcpy(cmdname, pstart, len);
1998 cmdname[len] = '\0';
2000 /* find the command */
2001 for(cmd = cmds; cmd->name != NULL; cmd++) {
2002 if (compare_cmd(cmdname, cmd->name))
2003 goto found;
2005 term_printf("unknown command: '%s'\n", cmdname);
2006 return;
2007 found:
2009 for(i = 0; i < MAX_ARGS; i++)
2010 str_allocated[i] = NULL;
2012 /* parse the parameters */
2013 typestr = cmd->args_type;
2014 nb_args = 0;
2015 for(;;) {
2016 while (isspace(*p)) /* eat whitespaces */
2017 p++;
2018 c = *typestr;
2019 if (c == '\0')
2020 break;
2021 typestr++;
2022 switch(c) {
2023 case 'F':
2024 case 'B':
2025 case 's':
2027 int ret;
2028 char *str;
2030 if (*typestr == '?') {
2031 typestr++;
2032 if (*p == '\0') {
2033 /* no optional string: NULL argument */
2034 str = NULL;
2035 goto add_str;
2038 ret = get_str(buf, sizeof(buf), &p);
2039 if (ret < 0) {
2040 switch(c) {
2041 case 'F':
2042 term_printf("%s: filename expected\n", cmdname);
2043 break;
2044 case 'B':
2045 term_printf("%s: block device name expected\n", cmdname);
2046 break;
2047 default:
2048 term_printf("%s: string expected\n", cmdname);
2049 break;
2051 goto fail;
2053 str = qemu_malloc(strlen(buf) + 1);
2054 strcpy(str, buf);
2055 str_allocated[nb_args] = str;
2056 add_str:
2057 if (nb_args >= MAX_ARGS) {
2058 error_args:
2059 term_printf("%s: too many arguments\n", cmdname);
2060 goto fail;
2062 args[nb_args++] = str;
2064 break;
2065 case '/':
2067 int count, format, size;
2069 if (*p == '/') {
2070 /* format found */
2071 p++;
2072 count = 1;
2073 if (isdigit(*p)) {
2074 count = 0;
2075 while (isdigit(*p)) {
2076 count = count * 10 + (*p - '0');
2077 p++;
2080 size = -1;
2081 format = -1;
2082 for(;;) {
2083 switch(*p) {
2084 case 'o':
2085 case 'd':
2086 case 'u':
2087 case 'x':
2088 case 'i':
2089 case 'c':
2090 format = *p++;
2091 break;
2092 case 'b':
2093 size = 1;
2094 p++;
2095 break;
2096 case 'h':
2097 size = 2;
2098 p++;
2099 break;
2100 case 'w':
2101 size = 4;
2102 p++;
2103 break;
2104 case 'g':
2105 case 'L':
2106 size = 8;
2107 p++;
2108 break;
2109 default:
2110 goto next;
2113 next:
2114 if (*p != '\0' && !isspace(*p)) {
2115 term_printf("invalid char in format: '%c'\n", *p);
2116 goto fail;
2118 if (format < 0)
2119 format = default_fmt_format;
2120 if (format != 'i') {
2121 /* for 'i', not specifying a size gives -1 as size */
2122 if (size < 0)
2123 size = default_fmt_size;
2125 default_fmt_size = size;
2126 default_fmt_format = format;
2127 } else {
2128 count = 1;
2129 format = default_fmt_format;
2130 if (format != 'i') {
2131 size = default_fmt_size;
2132 } else {
2133 size = -1;
2136 if (nb_args + 3 > MAX_ARGS)
2137 goto error_args;
2138 args[nb_args++] = (void*)count;
2139 args[nb_args++] = (void*)format;
2140 args[nb_args++] = (void*)size;
2142 break;
2143 case 'i':
2144 case 'l':
2146 target_long val;
2147 if (*typestr == '?' || *typestr == '.') {
2148 if (*typestr == '?') {
2149 if (*p == '\0')
2150 has_arg = 0;
2151 else
2152 has_arg = 1;
2153 } else {
2154 if (*p == '.') {
2155 p++;
2156 while (isspace(*p))
2157 p++;
2158 has_arg = 1;
2159 } else {
2160 has_arg = 0;
2163 typestr++;
2164 if (nb_args >= MAX_ARGS)
2165 goto error_args;
2166 args[nb_args++] = (void *)has_arg;
2167 if (!has_arg) {
2168 if (nb_args >= MAX_ARGS)
2169 goto error_args;
2170 val = -1;
2171 goto add_num;
2174 if (get_expr(&val, &p))
2175 goto fail;
2176 add_num:
2177 if (c == 'i') {
2178 if (nb_args >= MAX_ARGS)
2179 goto error_args;
2180 args[nb_args++] = (void *)(int)val;
2181 } else {
2182 if ((nb_args + 1) >= MAX_ARGS)
2183 goto error_args;
2184 #if TARGET_LONG_BITS == 64
2185 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2186 #else
2187 args[nb_args++] = (void *)0;
2188 #endif
2189 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2192 break;
2193 case 'A':
2194 args[nb_args++] = p;
2195 while (*p) /* goto end of cmdline */
2196 p++;
2197 break;
2198 case '-':
2200 int has_option;
2201 /* option */
2203 c = *typestr++;
2204 if (c == '\0')
2205 goto bad_type;
2206 has_option = 0;
2207 if (*p == '-') {
2208 p++;
2209 if (*p != c) {
2210 term_printf("%s: unsupported option -%c\n",
2211 cmdname, *p);
2212 goto fail;
2214 p++;
2215 has_option = 1;
2217 if (nb_args >= MAX_ARGS)
2218 goto error_args;
2219 args[nb_args++] = (void *)has_option;
2221 break;
2222 default:
2223 bad_type:
2224 term_printf("%s: unknown type '%c'\n", cmdname, c);
2225 goto fail;
2228 /* check that all arguments were parsed */
2229 while (isspace(*p))
2230 p++;
2231 if (*p != '\0') {
2232 term_printf("%s: extraneous characters at the end of line\n",
2233 cmdname);
2234 goto fail;
2237 #ifdef USE_KVM
2238 if(1)
2240 CPUState *env=mon_get_cpu();
2241 if (kvm_allowed)
2242 kvm_save_registers(env);
2244 #endif
2246 switch(nb_args) {
2247 case 0:
2248 cmd->handler();
2249 break;
2250 case 1:
2251 cmd->handler(args[0]);
2252 break;
2253 case 2:
2254 cmd->handler(args[0], args[1]);
2255 break;
2256 case 3:
2257 cmd->handler(args[0], args[1], args[2]);
2258 break;
2259 case 4:
2260 cmd->handler(args[0], args[1], args[2], args[3]);
2261 break;
2262 case 5:
2263 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2264 break;
2265 case 6:
2266 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2267 break;
2268 case 7:
2269 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2270 break;
2271 default:
2272 term_printf("unsupported number of arguments: %d\n", nb_args);
2273 goto fail;
2275 fail:
2276 for(i = 0; i < MAX_ARGS; i++)
2277 qemu_free(str_allocated[i]);
2278 return;
2281 static void cmd_completion(const char *name, const char *list)
2283 const char *p, *pstart;
2284 char cmd[128];
2285 int len;
2287 p = list;
2288 for(;;) {
2289 pstart = p;
2290 p = strchr(p, '|');
2291 if (!p)
2292 p = pstart + strlen(pstart);
2293 len = p - pstart;
2294 if (len > sizeof(cmd) - 2)
2295 len = sizeof(cmd) - 2;
2296 memcpy(cmd, pstart, len);
2297 cmd[len] = '\0';
2298 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2299 add_completion(cmd);
2301 if (*p == '\0')
2302 break;
2303 p++;
2307 static void file_completion(const char *input)
2309 DIR *ffs;
2310 struct dirent *d;
2311 char path[1024];
2312 char file[1024], file_prefix[1024];
2313 int input_path_len;
2314 const char *p;
2316 p = strrchr(input, '/');
2317 if (!p) {
2318 input_path_len = 0;
2319 pstrcpy(file_prefix, sizeof(file_prefix), input);
2320 strcpy(path, ".");
2321 } else {
2322 input_path_len = p - input + 1;
2323 memcpy(path, input, input_path_len);
2324 if (input_path_len > sizeof(path) - 1)
2325 input_path_len = sizeof(path) - 1;
2326 path[input_path_len] = '\0';
2327 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2329 #ifdef DEBUG_COMPLETION
2330 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2331 #endif
2332 ffs = opendir(path);
2333 if (!ffs)
2334 return;
2335 for(;;) {
2336 struct stat sb;
2337 d = readdir(ffs);
2338 if (!d)
2339 break;
2340 if (strstart(d->d_name, file_prefix, NULL)) {
2341 memcpy(file, input, input_path_len);
2342 strcpy(file + input_path_len, d->d_name);
2343 /* stat the file to find out if it's a directory.
2344 * In that case add a slash to speed up typing long paths
2346 stat(file, &sb);
2347 if(S_ISDIR(sb.st_mode))
2348 strcat(file, "/");
2349 add_completion(file);
2352 closedir(ffs);
2355 static void block_completion_it(void *opaque, const char *name)
2357 const char *input = opaque;
2359 if (input[0] == '\0' ||
2360 !strncmp(name, (char *)input, strlen(input))) {
2361 add_completion(name);
2365 /* NOTE: this parser is an approximate form of the real command parser */
2366 static void parse_cmdline(const char *cmdline,
2367 int *pnb_args, char **args)
2369 const char *p;
2370 int nb_args, ret;
2371 char buf[1024];
2373 p = cmdline;
2374 nb_args = 0;
2375 for(;;) {
2376 while (isspace(*p))
2377 p++;
2378 if (*p == '\0')
2379 break;
2380 if (nb_args >= MAX_ARGS)
2381 break;
2382 ret = get_str(buf, sizeof(buf), &p);
2383 args[nb_args] = qemu_strdup(buf);
2384 nb_args++;
2385 if (ret < 0)
2386 break;
2388 *pnb_args = nb_args;
2391 void readline_find_completion(const char *cmdline)
2393 const char *cmdname;
2394 char *args[MAX_ARGS];
2395 int nb_args, i, len;
2396 const char *ptype, *str;
2397 term_cmd_t *cmd;
2398 const KeyDef *key;
2400 parse_cmdline(cmdline, &nb_args, args);
2401 #ifdef DEBUG_COMPLETION
2402 for(i = 0; i < nb_args; i++) {
2403 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2405 #endif
2407 /* if the line ends with a space, it means we want to complete the
2408 next arg */
2409 len = strlen(cmdline);
2410 if (len > 0 && isspace(cmdline[len - 1])) {
2411 if (nb_args >= MAX_ARGS)
2412 return;
2413 args[nb_args++] = qemu_strdup("");
2415 if (nb_args <= 1) {
2416 /* command completion */
2417 if (nb_args == 0)
2418 cmdname = "";
2419 else
2420 cmdname = args[0];
2421 completion_index = strlen(cmdname);
2422 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2423 cmd_completion(cmdname, cmd->name);
2425 } else {
2426 /* find the command */
2427 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2428 if (compare_cmd(args[0], cmd->name))
2429 goto found;
2431 return;
2432 found:
2433 ptype = cmd->args_type;
2434 for(i = 0; i < nb_args - 2; i++) {
2435 if (*ptype != '\0') {
2436 ptype++;
2437 while (*ptype == '?')
2438 ptype++;
2441 str = args[nb_args - 1];
2442 switch(*ptype) {
2443 case 'F':
2444 /* file completion */
2445 completion_index = strlen(str);
2446 file_completion(str);
2447 break;
2448 case 'B':
2449 /* block device name completion */
2450 completion_index = strlen(str);
2451 bdrv_iterate(block_completion_it, (void *)str);
2452 break;
2453 case 's':
2454 case 'A':
2455 /* XXX: more generic ? */
2456 if (!strcmp(cmd->name, "info")) {
2457 completion_index = strlen(str);
2458 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2459 cmd_completion(str, cmd->name);
2461 } else if (!strcmp(cmd->name, "sendkey")) {
2462 completion_index = strlen(str);
2463 for(key = key_defs; key->name != NULL; key++) {
2464 cmd_completion(str, key->name);
2466 } else if (!strcmp(cmd->name, "migration")) {
2467 completion_index = strlen(str);
2468 for(cmd = migration_cmds; cmd->name != NULL; cmd++) {
2469 cmd_completion(str, cmd->name);
2472 break;
2473 default:
2474 break;
2477 for(i = 0; i < nb_args; i++)
2478 qemu_free(args[i]);
2481 static int term_can_read(void *opaque)
2483 return 128;
2486 static void term_read(void *opaque, const uint8_t *buf, int size)
2488 int i;
2489 for(i = 0; i < size; i++)
2490 readline_handle_byte(buf[i]);
2493 static void monitor_start_input(void);
2495 static void monitor_handle_command1(void *opaque, const char *cmdline)
2497 monitor_handle_command(term_cmds, cmdline);
2498 monitor_start_input();
2501 static void monitor_start_input(void)
2503 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2506 void monitor_init(CharDriverState *hd, int show_banner)
2508 monitor_hd = hd;
2509 if (show_banner) {
2510 term_printf("QEMU %s monitor - type 'help' for more information\n",
2511 QEMU_VERSION);
2513 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2514 monitor_start_input();
2517 /* XXX: use threads ? */
2518 /* modal monitor readline */
2519 static int monitor_readline_started;
2520 static char *monitor_readline_buf;
2521 static int monitor_readline_buf_size;
2523 static void monitor_readline_cb(void *opaque, const char *input)
2525 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2526 monitor_readline_started = 0;
2529 void monitor_readline(const char *prompt, int is_password,
2530 char *buf, int buf_size)
2532 if (is_password) {
2533 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2535 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2536 monitor_readline_buf = buf;
2537 monitor_readline_buf_size = buf_size;
2538 monitor_readline_started = 1;
2539 while (monitor_readline_started) {
2540 main_loop_wait(10);