Wrap assign_device and assign_irq
[qemu-kvm/fedora.git] / monitor.c
bloba4a6eb4b99bad6ca9495ee2f5f2bcd6c9b22eefa
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
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 "hw/hw.h"
25 #include "hw/usb.h"
26 #include "hw/pcmcia.h"
27 #include "hw/pc.h"
28 #include "hw/pci.h"
29 #include "gdbstub.h"
30 #include "net.h"
31 #include "qemu-char.h"
32 #include "sysemu.h"
33 #include "console.h"
34 #include "block.h"
35 #include "audio/audio.h"
36 #include "disas.h"
37 #include "balloon.h"
38 #include <dirent.h>
39 #include "qemu-timer.h"
40 #include "migration.h"
41 #include "kvm.h"
43 #include "qemu-kvm.h"
45 //#define DEBUG
46 //#define DEBUG_COMPLETION
49 * Supported types:
51 * 'F' filename
52 * 'B' block device name
53 * 's' string (accept optional quote)
54 * 'i' 32 bit integer
55 * 'l' target long (32 or 64 bit)
56 * '/' optional gdb-like print format (like "/10x")
58 * '?' optional type (for 'F', 's' and 'i')
62 typedef struct term_cmd_t {
63 const char *name;
64 const char *args_type;
65 void *handler;
66 const char *params;
67 const char *help;
68 } term_cmd_t;
70 #define MAX_MON 4
71 static CharDriverState *monitor_hd[MAX_MON];
72 static int hide_banner;
74 static const term_cmd_t term_cmds[];
75 static const term_cmd_t info_cmds[];
77 static uint8_t term_outbuf[1024];
78 static int term_outbuf_index;
80 static void monitor_start_input(void);
82 static CPUState *mon_cpu = NULL;
84 void term_flush(void)
86 int i;
87 if (term_outbuf_index > 0) {
88 for (i = 0; i < MAX_MON; i++)
89 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
90 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
91 term_outbuf_index = 0;
95 /* flush at every end of line or if the buffer is full */
96 void term_puts(const char *str)
98 char c;
99 for(;;) {
100 c = *str++;
101 if (c == '\0')
102 break;
103 if (c == '\n')
104 term_outbuf[term_outbuf_index++] = '\r';
105 term_outbuf[term_outbuf_index++] = c;
106 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
107 c == '\n')
108 term_flush();
112 void term_vprintf(const char *fmt, va_list ap)
114 char buf[4096];
115 vsnprintf(buf, sizeof(buf), fmt, ap);
116 term_puts(buf);
119 void term_printf(const char *fmt, ...)
121 va_list ap;
122 va_start(ap, fmt);
123 term_vprintf(fmt, ap);
124 va_end(ap);
127 void term_print_filename(const char *filename)
129 int i;
131 for (i = 0; filename[i]; i++) {
132 switch (filename[i]) {
133 case ' ':
134 case '"':
135 case '\\':
136 term_printf("\\%c", filename[i]);
137 break;
138 case '\t':
139 term_printf("\\t");
140 break;
141 case '\r':
142 term_printf("\\r");
143 break;
144 case '\n':
145 term_printf("\\n");
146 break;
147 default:
148 term_printf("%c", filename[i]);
149 break;
154 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
156 va_list ap;
157 va_start(ap, fmt);
158 term_vprintf(fmt, ap);
159 va_end(ap);
160 return 0;
163 static int compare_cmd(const char *name, const char *list)
165 const char *p, *pstart;
166 int len;
167 len = strlen(name);
168 p = list;
169 for(;;) {
170 pstart = p;
171 p = strchr(p, '|');
172 if (!p)
173 p = pstart + strlen(pstart);
174 if ((p - pstart) == len && !memcmp(pstart, name, len))
175 return 1;
176 if (*p == '\0')
177 break;
178 p++;
180 return 0;
183 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
185 const term_cmd_t *cmd;
187 for(cmd = cmds; cmd->name != NULL; cmd++) {
188 if (!name || !strcmp(name, cmd->name))
189 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
193 static void help_cmd(const char *name)
195 if (name && !strcmp(name, "info")) {
196 help_cmd1(info_cmds, "info ", NULL);
197 } else {
198 help_cmd1(term_cmds, "", name);
199 if (name && !strcmp(name, "log")) {
200 const CPULogItem *item;
201 term_printf("Log items (comma separated):\n");
202 term_printf("%-10s %s\n", "none", "remove all logs");
203 for(item = cpu_log_items; item->mask != 0; item++) {
204 term_printf("%-10s %s\n", item->name, item->help);
210 static void do_help(const char *name)
212 help_cmd(name);
215 static void do_commit(const char *device)
217 int i, all_devices;
219 all_devices = !strcmp(device, "all");
220 for (i = 0; i < nb_drives; i++) {
221 if (all_devices ||
222 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
223 bdrv_commit(drives_table[i].bdrv);
227 static void do_info(const char *item)
229 const term_cmd_t *cmd;
230 void (*handler)(void);
232 if (!item)
233 goto help;
234 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
235 if (compare_cmd(item, cmd->name))
236 goto found;
238 help:
239 help_cmd("info");
240 return;
241 found:
242 handler = cmd->handler;
243 handler();
246 static void do_info_version(void)
248 term_printf("%s\n", QEMU_VERSION);
251 static void do_info_name(void)
253 if (qemu_name)
254 term_printf("%s\n", qemu_name);
257 #if defined(TARGET_I386)
258 static void do_info_hpet(void)
260 term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
262 #endif
264 static void do_info_uuid(void)
266 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
267 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
268 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
269 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
270 qemu_uuid[15]);
273 static void do_info_block(void)
275 bdrv_info();
278 static void do_info_blockstats(void)
280 bdrv_info_stats();
283 /* get the current CPU defined by the user */
284 static int mon_set_cpu(int cpu_index)
286 CPUState *env;
288 for(env = first_cpu; env != NULL; env = env->next_cpu) {
289 if (env->cpu_index == cpu_index) {
290 mon_cpu = env;
291 return 0;
294 return -1;
297 static CPUState *mon_get_cpu(void)
299 if (!mon_cpu) {
300 mon_set_cpu(0);
303 kvm_save_registers(mon_cpu);
305 return mon_cpu;
308 static void do_info_registers(void)
310 CPUState *env;
311 env = mon_get_cpu();
312 if (!env)
313 return;
314 #ifdef TARGET_I386
315 cpu_dump_state(env, NULL, monitor_fprintf,
316 X86_DUMP_FPU);
317 #else
318 cpu_dump_state(env, NULL, monitor_fprintf,
320 #endif
323 static void do_info_cpus(void)
325 CPUState *env;
327 /* just to set the default cpu if not already done */
328 mon_get_cpu();
330 for(env = first_cpu; env != NULL; env = env->next_cpu) {
331 kvm_save_registers(env);
332 term_printf("%c CPU #%d:",
333 (env == mon_cpu) ? '*' : ' ',
334 env->cpu_index);
335 #if defined(TARGET_I386)
336 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
337 #elif defined(TARGET_PPC)
338 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
339 #elif defined(TARGET_SPARC)
340 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
341 #elif defined(TARGET_MIPS)
342 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
343 #endif
344 if (env->halted)
345 term_printf(" (halted)");
346 term_printf(" thread_id=%d", env->thread_id);
347 term_printf("\n");
351 static void do_cpu_set(int index)
353 if (mon_set_cpu(index) < 0)
354 term_printf("Invalid CPU index\n");
357 static void do_cpu_set_nr(int value, const char *status)
359 int state;
361 if (!strcmp(status, "online"))
362 state = 1;
363 else if (!strcmp(status, "offline"))
364 state = 0;
365 else {
366 term_printf("invalid status: %s\n", status);
367 return;
369 #if defined(TARGET_I386) || defined(TARGET_X86_64)
370 qemu_system_cpu_hot_add(value, state);
371 #endif
374 static void do_info_jit(void)
376 dump_exec_info(NULL, monitor_fprintf);
379 static void do_info_history (void)
381 int i;
382 const char *str;
384 i = 0;
385 for(;;) {
386 str = readline_get_history(i);
387 if (!str)
388 break;
389 term_printf("%d: '%s'\n", i, str);
390 i++;
394 #if defined(TARGET_PPC)
395 /* XXX: not implemented in other targets */
396 static void do_info_cpu_stats (void)
398 CPUState *env;
400 env = mon_get_cpu();
401 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
403 #endif
405 static void do_quit(void)
407 exit(0);
410 static int eject_device(BlockDriverState *bs, int force)
412 if (bdrv_is_inserted(bs)) {
413 if (!force) {
414 if (!bdrv_is_removable(bs)) {
415 term_printf("device is not removable\n");
416 return -1;
418 if (bdrv_is_locked(bs)) {
419 term_printf("device is locked\n");
420 return -1;
423 bdrv_close(bs);
425 return 0;
428 static void do_eject(int force, const char *filename)
430 BlockDriverState *bs;
432 bs = bdrv_find(filename);
433 if (!bs) {
434 term_printf("device not found\n");
435 return;
437 eject_device(bs, force);
440 static void do_change_block(const char *device, const char *filename, const char *fmt)
442 BlockDriverState *bs;
443 BlockDriver *drv = NULL;
445 bs = bdrv_find(device);
446 if (!bs) {
447 term_printf("device not found\n");
448 return;
450 if (fmt) {
451 drv = bdrv_find_format(fmt);
452 if (!drv) {
453 term_printf("invalid format %s\n", fmt);
454 return;
457 if (eject_device(bs, 0) < 0)
458 return;
459 bdrv_open2(bs, filename, 0, drv);
460 qemu_key_check(bs, filename);
463 static void do_change_vnc(const char *target, const char *arg)
465 if (strcmp(target, "passwd") == 0 ||
466 strcmp(target, "password") == 0) {
467 char password[9];
468 if (arg) {
469 strncpy(password, arg, sizeof(password));
470 password[sizeof(password) - 1] = '\0';
471 } else
472 monitor_readline("Password: ", 1, password, sizeof(password));
473 if (vnc_display_password(NULL, password) < 0)
474 term_printf("could not set VNC server password\n");
475 } else {
476 if (vnc_display_open(NULL, target) < 0)
477 term_printf("could not start VNC server on %s\n", target);
481 static void do_change(const char *device, const char *target, const char *arg)
483 if (strcmp(device, "vnc") == 0) {
484 do_change_vnc(target, arg);
485 } else {
486 do_change_block(device, target, arg);
490 static void do_screen_dump(const char *filename)
492 vga_hw_screen_dump(filename);
495 static void do_logfile(const char *filename)
497 cpu_set_log_filename(filename);
500 static void do_log(const char *items)
502 int mask;
504 if (!strcmp(items, "none")) {
505 mask = 0;
506 } else {
507 mask = cpu_str_to_log_mask(items);
508 if (!mask) {
509 help_cmd("log");
510 return;
513 cpu_set_log(mask);
516 static void do_stop(void)
518 vm_stop(EXCP_INTERRUPT);
521 static void do_cont(void)
523 vm_start();
526 #ifdef CONFIG_GDBSTUB
527 static void do_gdbserver(const char *port)
529 if (!port)
530 port = DEFAULT_GDBSTUB_PORT;
531 if (gdbserver_start(port) < 0) {
532 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
533 } else {
534 qemu_printf("Waiting gdb connection on port '%s'\n", port);
537 #endif
539 static void term_printc(int c)
541 term_printf("'");
542 switch(c) {
543 case '\'':
544 term_printf("\\'");
545 break;
546 case '\\':
547 term_printf("\\\\");
548 break;
549 case '\n':
550 term_printf("\\n");
551 break;
552 case '\r':
553 term_printf("\\r");
554 break;
555 default:
556 if (c >= 32 && c <= 126) {
557 term_printf("%c", c);
558 } else {
559 term_printf("\\x%02x", c);
561 break;
563 term_printf("'");
566 static void memory_dump(int count, int format, int wsize,
567 target_phys_addr_t addr, int is_physical)
569 CPUState *env;
570 int nb_per_line, l, line_size, i, max_digits, len;
571 uint8_t buf[16];
572 uint64_t v;
574 if (format == 'i') {
575 int flags;
576 flags = 0;
577 env = mon_get_cpu();
578 if (!env && !is_physical)
579 return;
580 #ifdef TARGET_I386
581 if (wsize == 2) {
582 flags = 1;
583 } else if (wsize == 4) {
584 flags = 0;
585 } else {
586 /* as default we use the current CS size */
587 flags = 0;
588 if (env) {
589 #ifdef TARGET_X86_64
590 if ((env->efer & MSR_EFER_LMA) &&
591 (env->segs[R_CS].flags & DESC_L_MASK))
592 flags = 2;
593 else
594 #endif
595 if (!(env->segs[R_CS].flags & DESC_B_MASK))
596 flags = 1;
599 #endif
600 monitor_disas(env, addr, count, is_physical, flags);
601 return;
604 len = wsize * count;
605 if (wsize == 1)
606 line_size = 8;
607 else
608 line_size = 16;
609 nb_per_line = line_size / wsize;
610 max_digits = 0;
612 switch(format) {
613 case 'o':
614 max_digits = (wsize * 8 + 2) / 3;
615 break;
616 default:
617 case 'x':
618 max_digits = (wsize * 8) / 4;
619 break;
620 case 'u':
621 case 'd':
622 max_digits = (wsize * 8 * 10 + 32) / 33;
623 break;
624 case 'c':
625 wsize = 1;
626 break;
629 while (len > 0) {
630 if (is_physical)
631 term_printf(TARGET_FMT_plx ":", addr);
632 else
633 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
634 l = len;
635 if (l > line_size)
636 l = line_size;
637 if (is_physical) {
638 cpu_physical_memory_rw(addr, buf, l, 0);
639 } else {
640 env = mon_get_cpu();
641 if (!env)
642 break;
643 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
644 term_printf(" Cannot access memory\n");
645 break;
648 i = 0;
649 while (i < l) {
650 switch(wsize) {
651 default:
652 case 1:
653 v = ldub_raw(buf + i);
654 break;
655 case 2:
656 v = lduw_raw(buf + i);
657 break;
658 case 4:
659 v = (uint32_t)ldl_raw(buf + i);
660 break;
661 case 8:
662 v = ldq_raw(buf + i);
663 break;
665 term_printf(" ");
666 switch(format) {
667 case 'o':
668 term_printf("%#*" PRIo64, max_digits, v);
669 break;
670 case 'x':
671 term_printf("0x%0*" PRIx64, max_digits, v);
672 break;
673 case 'u':
674 term_printf("%*" PRIu64, max_digits, v);
675 break;
676 case 'd':
677 term_printf("%*" PRId64, max_digits, v);
678 break;
679 case 'c':
680 term_printc(v);
681 break;
683 i += wsize;
685 term_printf("\n");
686 addr += l;
687 len -= l;
691 #if TARGET_LONG_BITS == 64
692 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
693 #else
694 #define GET_TLONG(h, l) (l)
695 #endif
697 static void do_memory_dump(int count, int format, int size,
698 uint32_t addrh, uint32_t addrl)
700 target_long addr = GET_TLONG(addrh, addrl);
701 memory_dump(count, format, size, addr, 0);
704 #if TARGET_PHYS_ADDR_BITS > 32
705 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
706 #else
707 #define GET_TPHYSADDR(h, l) (l)
708 #endif
710 static void do_physical_memory_dump(int count, int format, int size,
711 uint32_t addrh, uint32_t addrl)
714 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
715 memory_dump(count, format, size, addr, 1);
718 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
720 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
721 #if TARGET_PHYS_ADDR_BITS == 32
722 switch(format) {
723 case 'o':
724 term_printf("%#o", val);
725 break;
726 case 'x':
727 term_printf("%#x", val);
728 break;
729 case 'u':
730 term_printf("%u", val);
731 break;
732 default:
733 case 'd':
734 term_printf("%d", val);
735 break;
736 case 'c':
737 term_printc(val);
738 break;
740 #else
741 switch(format) {
742 case 'o':
743 term_printf("%#" PRIo64, val);
744 break;
745 case 'x':
746 term_printf("%#" PRIx64, val);
747 break;
748 case 'u':
749 term_printf("%" PRIu64, val);
750 break;
751 default:
752 case 'd':
753 term_printf("%" PRId64, val);
754 break;
755 case 'c':
756 term_printc(val);
757 break;
759 #endif
760 term_printf("\n");
763 static void do_memory_save(unsigned int valh, unsigned int vall,
764 uint32_t size, const char *filename)
766 FILE *f;
767 target_long addr = GET_TLONG(valh, vall);
768 uint32_t l;
769 CPUState *env;
770 uint8_t buf[1024];
772 env = mon_get_cpu();
773 if (!env)
774 return;
776 f = fopen(filename, "wb");
777 if (!f) {
778 term_printf("could not open '%s'\n", filename);
779 return;
781 while (size != 0) {
782 l = sizeof(buf);
783 if (l > size)
784 l = size;
785 cpu_memory_rw_debug(env, addr, buf, l, 0);
786 fwrite(buf, 1, l, f);
787 addr += l;
788 size -= l;
790 fclose(f);
793 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
794 uint32_t size, const char *filename)
796 FILE *f;
797 uint32_t l;
798 uint8_t buf[1024];
799 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
801 f = fopen(filename, "wb");
802 if (!f) {
803 term_printf("could not open '%s'\n", filename);
804 return;
806 while (size != 0) {
807 l = sizeof(buf);
808 if (l > size)
809 l = size;
810 cpu_physical_memory_rw(addr, buf, l, 0);
811 fwrite(buf, 1, l, f);
812 fflush(f);
813 addr += l;
814 size -= l;
816 fclose(f);
819 static void do_sum(uint32_t start, uint32_t size)
821 uint32_t addr;
822 uint8_t buf[1];
823 uint16_t sum;
825 sum = 0;
826 for(addr = start; addr < (start + size); addr++) {
827 cpu_physical_memory_rw(addr, buf, 1, 0);
828 /* BSD sum algorithm ('sum' Unix command) */
829 sum = (sum >> 1) | (sum << 15);
830 sum += buf[0];
832 term_printf("%05d\n", sum);
835 typedef struct {
836 int keycode;
837 const char *name;
838 } KeyDef;
840 static const KeyDef key_defs[] = {
841 { 0x2a, "shift" },
842 { 0x36, "shift_r" },
844 { 0x38, "alt" },
845 { 0xb8, "alt_r" },
846 { 0x64, "altgr" },
847 { 0xe4, "altgr_r" },
848 { 0x1d, "ctrl" },
849 { 0x9d, "ctrl_r" },
851 { 0xdd, "menu" },
853 { 0x01, "esc" },
855 { 0x02, "1" },
856 { 0x03, "2" },
857 { 0x04, "3" },
858 { 0x05, "4" },
859 { 0x06, "5" },
860 { 0x07, "6" },
861 { 0x08, "7" },
862 { 0x09, "8" },
863 { 0x0a, "9" },
864 { 0x0b, "0" },
865 { 0x0c, "minus" },
866 { 0x0d, "equal" },
867 { 0x0e, "backspace" },
869 { 0x0f, "tab" },
870 { 0x10, "q" },
871 { 0x11, "w" },
872 { 0x12, "e" },
873 { 0x13, "r" },
874 { 0x14, "t" },
875 { 0x15, "y" },
876 { 0x16, "u" },
877 { 0x17, "i" },
878 { 0x18, "o" },
879 { 0x19, "p" },
881 { 0x1c, "ret" },
883 { 0x1e, "a" },
884 { 0x1f, "s" },
885 { 0x20, "d" },
886 { 0x21, "f" },
887 { 0x22, "g" },
888 { 0x23, "h" },
889 { 0x24, "j" },
890 { 0x25, "k" },
891 { 0x26, "l" },
893 { 0x2c, "z" },
894 { 0x2d, "x" },
895 { 0x2e, "c" },
896 { 0x2f, "v" },
897 { 0x30, "b" },
898 { 0x31, "n" },
899 { 0x32, "m" },
900 { 0x33, "comma" },
901 { 0x34, "dot" },
902 { 0x35, "slash" },
904 { 0x37, "asterisk" },
906 { 0x39, "spc" },
907 { 0x3a, "caps_lock" },
908 { 0x3b, "f1" },
909 { 0x3c, "f2" },
910 { 0x3d, "f3" },
911 { 0x3e, "f4" },
912 { 0x3f, "f5" },
913 { 0x40, "f6" },
914 { 0x41, "f7" },
915 { 0x42, "f8" },
916 { 0x43, "f9" },
917 { 0x44, "f10" },
918 { 0x45, "num_lock" },
919 { 0x46, "scroll_lock" },
921 { 0xb5, "kp_divide" },
922 { 0x37, "kp_multiply" },
923 { 0x4a, "kp_subtract" },
924 { 0x4e, "kp_add" },
925 { 0x9c, "kp_enter" },
926 { 0x53, "kp_decimal" },
927 { 0x54, "sysrq" },
929 { 0x52, "kp_0" },
930 { 0x4f, "kp_1" },
931 { 0x50, "kp_2" },
932 { 0x51, "kp_3" },
933 { 0x4b, "kp_4" },
934 { 0x4c, "kp_5" },
935 { 0x4d, "kp_6" },
936 { 0x47, "kp_7" },
937 { 0x48, "kp_8" },
938 { 0x49, "kp_9" },
940 { 0x56, "<" },
942 { 0x57, "f11" },
943 { 0x58, "f12" },
945 { 0xb7, "print" },
947 { 0xc7, "home" },
948 { 0xc9, "pgup" },
949 { 0xd1, "pgdn" },
950 { 0xcf, "end" },
952 { 0xcb, "left" },
953 { 0xc8, "up" },
954 { 0xd0, "down" },
955 { 0xcd, "right" },
957 { 0xd2, "insert" },
958 { 0xd3, "delete" },
959 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
960 { 0xf0, "stop" },
961 { 0xf1, "again" },
962 { 0xf2, "props" },
963 { 0xf3, "undo" },
964 { 0xf4, "front" },
965 { 0xf5, "copy" },
966 { 0xf6, "open" },
967 { 0xf7, "paste" },
968 { 0xf8, "find" },
969 { 0xf9, "cut" },
970 { 0xfa, "lf" },
971 { 0xfb, "help" },
972 { 0xfc, "meta_l" },
973 { 0xfd, "meta_r" },
974 { 0xfe, "compose" },
975 #endif
976 { 0, NULL },
979 static int get_keycode(const char *key)
981 const KeyDef *p;
982 char *endp;
983 int ret;
985 for(p = key_defs; p->name != NULL; p++) {
986 if (!strcmp(key, p->name))
987 return p->keycode;
989 if (strstart(key, "0x", NULL)) {
990 ret = strtoul(key, &endp, 0);
991 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
992 return ret;
994 return -1;
997 #define MAX_KEYCODES 16
998 static uint8_t keycodes[MAX_KEYCODES];
999 static int nb_pending_keycodes;
1000 static QEMUTimer *key_timer;
1002 static void release_keys(void *opaque)
1004 int keycode;
1006 while (nb_pending_keycodes > 0) {
1007 nb_pending_keycodes--;
1008 keycode = keycodes[nb_pending_keycodes];
1009 if (keycode & 0x80)
1010 kbd_put_keycode(0xe0);
1011 kbd_put_keycode(keycode | 0x80);
1015 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1017 char keyname_buf[16];
1018 char *separator;
1019 int keyname_len, keycode, i;
1021 if (nb_pending_keycodes > 0) {
1022 qemu_del_timer(key_timer);
1023 release_keys(NULL);
1025 if (!has_hold_time)
1026 hold_time = 100;
1027 i = 0;
1028 while (1) {
1029 separator = strchr(string, '-');
1030 keyname_len = separator ? separator - string : strlen(string);
1031 if (keyname_len > 0) {
1032 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1033 if (keyname_len > sizeof(keyname_buf) - 1) {
1034 term_printf("invalid key: '%s...'\n", keyname_buf);
1035 return;
1037 if (i == MAX_KEYCODES) {
1038 term_printf("too many keys\n");
1039 return;
1041 keyname_buf[keyname_len] = 0;
1042 keycode = get_keycode(keyname_buf);
1043 if (keycode < 0) {
1044 term_printf("unknown key: '%s'\n", keyname_buf);
1045 return;
1047 keycodes[i++] = keycode;
1049 if (!separator)
1050 break;
1051 string = separator + 1;
1053 nb_pending_keycodes = i;
1054 /* key down events */
1055 for (i = 0; i < nb_pending_keycodes; i++) {
1056 keycode = keycodes[i];
1057 if (keycode & 0x80)
1058 kbd_put_keycode(0xe0);
1059 kbd_put_keycode(keycode & 0x7f);
1061 /* delayed key up events */
1062 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1063 muldiv64(ticks_per_sec, hold_time, 1000));
1066 static int mouse_button_state;
1068 static void do_mouse_move(const char *dx_str, const char *dy_str,
1069 const char *dz_str)
1071 int dx, dy, dz;
1072 dx = strtol(dx_str, NULL, 0);
1073 dy = strtol(dy_str, NULL, 0);
1074 dz = 0;
1075 if (dz_str)
1076 dz = strtol(dz_str, NULL, 0);
1077 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1080 static void do_mouse_button(int button_state)
1082 mouse_button_state = button_state;
1083 kbd_mouse_event(0, 0, 0, mouse_button_state);
1086 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1088 uint32_t val;
1089 int suffix;
1091 if (has_index) {
1092 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1093 addr++;
1095 addr &= 0xffff;
1097 switch(size) {
1098 default:
1099 case 1:
1100 val = cpu_inb(NULL, addr);
1101 suffix = 'b';
1102 break;
1103 case 2:
1104 val = cpu_inw(NULL, addr);
1105 suffix = 'w';
1106 break;
1107 case 4:
1108 val = cpu_inl(NULL, addr);
1109 suffix = 'l';
1110 break;
1112 term_printf("port%c[0x%04x] = %#0*x\n",
1113 suffix, addr, size * 2, val);
1116 /* boot_set handler */
1117 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1118 static void *boot_opaque;
1120 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1122 qemu_boot_set_handler = func;
1123 boot_opaque = opaque;
1126 static void do_boot_set(const char *bootdevice)
1128 int res;
1130 if (qemu_boot_set_handler) {
1131 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1132 if (res == 0)
1133 term_printf("boot device list now set to %s\n", bootdevice);
1134 else
1135 term_printf("setting boot device list failed with error %i\n", res);
1136 } else {
1137 term_printf("no function defined to set boot device list for this architecture\n");
1141 static void do_system_reset(void)
1143 qemu_system_reset_request();
1146 static void do_system_powerdown(void)
1148 qemu_system_powerdown_request();
1151 #if defined(TARGET_I386)
1152 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1154 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1155 addr,
1156 pte & mask,
1157 pte & PG_GLOBAL_MASK ? 'G' : '-',
1158 pte & PG_PSE_MASK ? 'P' : '-',
1159 pte & PG_DIRTY_MASK ? 'D' : '-',
1160 pte & PG_ACCESSED_MASK ? 'A' : '-',
1161 pte & PG_PCD_MASK ? 'C' : '-',
1162 pte & PG_PWT_MASK ? 'T' : '-',
1163 pte & PG_USER_MASK ? 'U' : '-',
1164 pte & PG_RW_MASK ? 'W' : '-');
1167 static void tlb_info(void)
1169 CPUState *env;
1170 int l1, l2;
1171 uint32_t pgd, pde, pte;
1173 env = mon_get_cpu();
1174 if (!env)
1175 return;
1177 if (!(env->cr[0] & CR0_PG_MASK)) {
1178 term_printf("PG disabled\n");
1179 return;
1181 pgd = env->cr[3] & ~0xfff;
1182 for(l1 = 0; l1 < 1024; l1++) {
1183 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1184 pde = le32_to_cpu(pde);
1185 if (pde & PG_PRESENT_MASK) {
1186 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1187 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1188 } else {
1189 for(l2 = 0; l2 < 1024; l2++) {
1190 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1191 (uint8_t *)&pte, 4);
1192 pte = le32_to_cpu(pte);
1193 if (pte & PG_PRESENT_MASK) {
1194 print_pte((l1 << 22) + (l2 << 12),
1195 pte & ~PG_PSE_MASK,
1196 ~0xfff);
1204 static void mem_print(uint32_t *pstart, int *plast_prot,
1205 uint32_t end, int prot)
1207 int prot1;
1208 prot1 = *plast_prot;
1209 if (prot != prot1) {
1210 if (*pstart != -1) {
1211 term_printf("%08x-%08x %08x %c%c%c\n",
1212 *pstart, end, end - *pstart,
1213 prot1 & PG_USER_MASK ? 'u' : '-',
1214 'r',
1215 prot1 & PG_RW_MASK ? 'w' : '-');
1217 if (prot != 0)
1218 *pstart = end;
1219 else
1220 *pstart = -1;
1221 *plast_prot = prot;
1225 static void mem_info(void)
1227 CPUState *env;
1228 int l1, l2, prot, last_prot;
1229 uint32_t pgd, pde, pte, start, end;
1231 env = mon_get_cpu();
1232 if (!env)
1233 return;
1235 if (!(env->cr[0] & CR0_PG_MASK)) {
1236 term_printf("PG disabled\n");
1237 return;
1239 pgd = env->cr[3] & ~0xfff;
1240 last_prot = 0;
1241 start = -1;
1242 for(l1 = 0; l1 < 1024; l1++) {
1243 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1244 pde = le32_to_cpu(pde);
1245 end = l1 << 22;
1246 if (pde & PG_PRESENT_MASK) {
1247 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1248 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1249 mem_print(&start, &last_prot, end, prot);
1250 } else {
1251 for(l2 = 0; l2 < 1024; l2++) {
1252 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1253 (uint8_t *)&pte, 4);
1254 pte = le32_to_cpu(pte);
1255 end = (l1 << 22) + (l2 << 12);
1256 if (pte & PG_PRESENT_MASK) {
1257 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1258 } else {
1259 prot = 0;
1261 mem_print(&start, &last_prot, end, prot);
1264 } else {
1265 prot = 0;
1266 mem_print(&start, &last_prot, end, prot);
1270 #endif
1272 static void do_info_kqemu(void)
1274 #ifdef USE_KQEMU
1275 CPUState *env;
1276 int val;
1277 val = 0;
1278 env = mon_get_cpu();
1279 if (!env) {
1280 term_printf("No cpu initialized yet");
1281 return;
1283 val = env->kqemu_enabled;
1284 term_printf("kqemu support: ");
1285 switch(val) {
1286 default:
1287 case 0:
1288 term_printf("disabled\n");
1289 break;
1290 case 1:
1291 term_printf("enabled for user code\n");
1292 break;
1293 case 2:
1294 term_printf("enabled for user and kernel code\n");
1295 break;
1297 #else
1298 term_printf("kqemu support: not compiled\n");
1299 #endif
1302 static void do_info_kvm(void)
1304 #if defined(USE_KVM) || defined(CONFIG_KVM)
1305 term_printf("kvm support: ");
1306 if (kvm_enabled())
1307 term_printf("enabled\n");
1308 else
1309 term_printf("disabled\n");
1310 #else
1311 term_printf("kvm support: not compiled\n");
1312 #endif
1315 #ifdef CONFIG_PROFILER
1317 int64_t kqemu_time;
1318 int64_t qemu_time;
1319 int64_t kqemu_exec_count;
1320 int64_t dev_time;
1321 int64_t kqemu_ret_int_count;
1322 int64_t kqemu_ret_excp_count;
1323 int64_t kqemu_ret_intr_count;
1325 static void do_info_profile(void)
1327 int64_t total;
1328 total = qemu_time;
1329 if (total == 0)
1330 total = 1;
1331 term_printf("async time %" PRId64 " (%0.3f)\n",
1332 dev_time, dev_time / (double)ticks_per_sec);
1333 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1334 qemu_time, qemu_time / (double)ticks_per_sec);
1335 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1336 kqemu_time, kqemu_time / (double)ticks_per_sec,
1337 kqemu_time / (double)total * 100.0,
1338 kqemu_exec_count,
1339 kqemu_ret_int_count,
1340 kqemu_ret_excp_count,
1341 kqemu_ret_intr_count);
1342 qemu_time = 0;
1343 kqemu_time = 0;
1344 kqemu_exec_count = 0;
1345 dev_time = 0;
1346 kqemu_ret_int_count = 0;
1347 kqemu_ret_excp_count = 0;
1348 kqemu_ret_intr_count = 0;
1349 #ifdef USE_KQEMU
1350 kqemu_record_dump();
1351 #endif
1353 #else
1354 static void do_info_profile(void)
1356 term_printf("Internal profiler not compiled\n");
1358 #endif
1360 /* Capture support */
1361 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1363 static void do_info_capture (void)
1365 int i;
1366 CaptureState *s;
1368 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1369 term_printf ("[%d]: ", i);
1370 s->ops.info (s->opaque);
1374 static void do_stop_capture (int n)
1376 int i;
1377 CaptureState *s;
1379 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1380 if (i == n) {
1381 s->ops.destroy (s->opaque);
1382 LIST_REMOVE (s, entries);
1383 qemu_free (s);
1384 return;
1389 #ifdef HAS_AUDIO
1390 static void do_wav_capture (const char *path,
1391 int has_freq, int freq,
1392 int has_bits, int bits,
1393 int has_channels, int nchannels)
1395 CaptureState *s;
1397 s = qemu_mallocz (sizeof (*s));
1399 freq = has_freq ? freq : 44100;
1400 bits = has_bits ? bits : 16;
1401 nchannels = has_channels ? nchannels : 2;
1403 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1404 term_printf ("Faied to add wave capture\n");
1405 qemu_free (s);
1407 LIST_INSERT_HEAD (&capture_head, s, entries);
1409 #endif
1411 #if defined(TARGET_I386)
1412 static void do_inject_nmi(int cpu_index)
1414 CPUState *env;
1416 for (env = first_cpu; env != NULL; env = env->next_cpu)
1417 if (env->cpu_index == cpu_index) {
1418 if (kvm_enabled())
1419 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1420 else
1421 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1422 break;
1425 #endif
1427 static void do_info_status(void)
1429 if (vm_running)
1430 term_printf("VM status: running\n");
1431 else
1432 term_printf("VM status: paused\n");
1436 static void do_balloon(int value)
1438 ram_addr_t target = value;
1439 qemu_balloon(target << 20);
1442 static void do_info_balloon(void)
1444 ram_addr_t actual;
1446 actual = qemu_balloon_status();
1447 if (kvm_enabled() && !kvm_has_sync_mmu())
1448 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1449 else if (actual == 0)
1450 term_printf("Ballooning not activated in VM\n");
1451 else
1452 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1455 /* Please update qemu-doc.texi when adding or changing commands */
1456 static const term_cmd_t term_cmds[] = {
1457 { "help|?", "s?", do_help,
1458 "[cmd]", "show the help" },
1459 { "commit", "s", do_commit,
1460 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1461 { "info", "s?", do_info,
1462 "subcommand", "show various information about the system state" },
1463 { "q|quit", "", do_quit,
1464 "", "quit the emulator" },
1465 { "eject", "-fB", do_eject,
1466 "[-f] device", "eject a removable medium (use -f to force it)" },
1467 { "change", "BFs?", do_change,
1468 "device filename [format]", "change a removable medium, optional format" },
1469 { "screendump", "F", do_screen_dump,
1470 "filename", "save screen into PPM image 'filename'" },
1471 { "logfile", "F", do_logfile,
1472 "filename", "output logs to 'filename'" },
1473 { "log", "s", do_log,
1474 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1475 { "savevm", "s?", do_savevm,
1476 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1477 { "loadvm", "s", do_loadvm,
1478 "tag|id", "restore a VM snapshot from its tag or id" },
1479 { "delvm", "s", do_delvm,
1480 "tag|id", "delete a VM snapshot from its tag or id" },
1481 { "stop", "", do_stop,
1482 "", "stop emulation", },
1483 { "c|cont", "", do_cont,
1484 "", "resume emulation", },
1485 #ifdef CONFIG_GDBSTUB
1486 { "gdbserver", "s?", do_gdbserver,
1487 "[port]", "start gdbserver session (default port=1234)", },
1488 #endif
1489 { "x", "/l", do_memory_dump,
1490 "/fmt addr", "virtual memory dump starting at 'addr'", },
1491 { "xp", "/l", do_physical_memory_dump,
1492 "/fmt addr", "physical memory dump starting at 'addr'", },
1493 { "p|print", "/l", do_print,
1494 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1495 { "i", "/ii.", do_ioport_read,
1496 "/fmt addr", "I/O port read" },
1498 { "sendkey", "si?", do_sendkey,
1499 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1500 { "system_reset", "", do_system_reset,
1501 "", "reset the system" },
1502 { "system_powerdown", "", do_system_powerdown,
1503 "", "send system power down event" },
1504 { "sum", "ii", do_sum,
1505 "addr size", "compute the checksum of a memory region" },
1506 { "usb_add", "s", do_usb_add,
1507 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1508 { "usb_del", "s", do_usb_del,
1509 "device", "remove USB device 'bus.addr'" },
1510 { "cpu", "i", do_cpu_set,
1511 "index", "set the default CPU" },
1512 { "mouse_move", "sss?", do_mouse_move,
1513 "dx dy [dz]", "send mouse move events" },
1514 { "mouse_button", "i", do_mouse_button,
1515 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1516 { "mouse_set", "i", do_mouse_set,
1517 "index", "set which mouse device receives events" },
1518 #ifdef HAS_AUDIO
1519 { "wavcapture", "si?i?i?", do_wav_capture,
1520 "path [frequency bits channels]",
1521 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1522 #endif
1523 { "stopcapture", "i", do_stop_capture,
1524 "capture index", "stop capture" },
1525 { "memsave", "lis", do_memory_save,
1526 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1527 { "pmemsave", "lis", do_physical_memory_save,
1528 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1529 { "boot_set", "s", do_boot_set,
1530 "bootdevice", "define new values for the boot device list" },
1531 #if defined(TARGET_I386)
1532 { "nmi", "i", do_inject_nmi,
1533 "cpu", "inject an NMI on the given CPU", },
1534 #endif
1535 { "migrate", "-ds", do_migrate,
1536 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1537 { "migrate_cancel", "", do_migrate_cancel,
1538 "", "cancel the current VM migration" },
1539 { "migrate_set_speed", "s", do_migrate_set_speed,
1540 "value", "set maximum speed (in bytes) for migrations" },
1541 #if defined(TARGET_I386)
1542 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1543 "[file=file][,if=type][,bus=n]\n"
1544 "[,unit=m][,media=d][index=i]\n"
1545 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1546 "[snapshot=on|off][,cache=on|off]",
1547 "add drive to PCI storage controller" },
1548 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]", "hot-add PCI device" },
1549 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1550 { "host_net_add", "ss", net_host_device_add,
1551 "[tap,user,socket,vde] options", "add host VLAN client" },
1552 { "host_net_remove", "is", net_host_device_remove,
1553 "vlan_id name", "remove host VLAN client" },
1554 #endif
1555 { "balloon", "i", do_balloon,
1556 "target", "request VM to change it's memory allocation (in MB)" },
1557 { "set_link", "ss", do_set_link,
1558 "name [up|down]", "change the link status of a network adapter" },
1559 { "set_link", "ss", do_set_link, "name [up|down]" },
1560 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1561 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1562 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1563 "[,unit=m][,media=d][index=i]\n"
1564 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1565 "[snapshot=on|off][,cache=on|off]",
1566 "add drive to PCI storage controller" },
1567 #endif
1568 { NULL, NULL, },
1571 /* Please update qemu-doc.texi when adding or changing commands */
1572 static const term_cmd_t info_cmds[] = {
1573 { "version", "", do_info_version,
1574 "", "show the version of QEMU" },
1575 { "network", "", do_info_network,
1576 "", "show the network state" },
1577 { "chardev", "", qemu_chr_info,
1578 "", "show the character devices" },
1579 { "block", "", do_info_block,
1580 "", "show the block devices" },
1581 { "blockstats", "", do_info_blockstats,
1582 "", "show block device statistics" },
1583 { "registers", "", do_info_registers,
1584 "", "show the cpu registers" },
1585 { "cpus", "", do_info_cpus,
1586 "", "show infos for each CPU" },
1587 { "history", "", do_info_history,
1588 "", "show the command line history", },
1589 { "irq", "", irq_info,
1590 "", "show the interrupts statistics (if available)", },
1591 { "pic", "", pic_info,
1592 "", "show i8259 (PIC) state", },
1593 { "pci", "", pci_info,
1594 "", "show PCI info", },
1595 #if defined(TARGET_I386)
1596 { "tlb", "", tlb_info,
1597 "", "show virtual to physical memory mappings", },
1598 { "mem", "", mem_info,
1599 "", "show the active virtual memory mappings", },
1600 { "hpet", "", do_info_hpet,
1601 "", "show state of HPET", },
1602 #endif
1603 { "jit", "", do_info_jit,
1604 "", "show dynamic compiler info", },
1605 { "kqemu", "", do_info_kqemu,
1606 "", "show KQEMU information", },
1607 { "kvm", "", do_info_kvm,
1608 "", "show KVM information", },
1609 { "usb", "", usb_info,
1610 "", "show guest USB devices", },
1611 { "usbhost", "", usb_host_info,
1612 "", "show host USB devices", },
1613 { "profile", "", do_info_profile,
1614 "", "show profiling information", },
1615 { "capture", "", do_info_capture,
1616 "", "show capture information" },
1617 { "snapshots", "", do_info_snapshots,
1618 "", "show the currently saved VM snapshots" },
1619 { "status", "", do_info_status,
1620 "", "show the current VM status (running|paused)" },
1621 { "pcmcia", "", pcmcia_info,
1622 "", "show guest PCMCIA status" },
1623 { "mice", "", do_info_mice,
1624 "", "show which guest mouse is receiving events" },
1625 { "vnc", "", do_info_vnc,
1626 "", "show the vnc server status"},
1627 { "name", "", do_info_name,
1628 "", "show the current VM name" },
1629 { "uuid", "", do_info_uuid,
1630 "", "show the current VM UUID" },
1631 #if defined(TARGET_PPC)
1632 { "cpustats", "", do_info_cpu_stats,
1633 "", "show CPU statistics", },
1634 #endif
1635 #if defined(CONFIG_SLIRP)
1636 { "slirp", "", do_info_slirp,
1637 "", "show SLIRP statistics", },
1638 #endif
1639 { "migrate", "", do_info_migrate, "", "show migration status" },
1640 { "balloon", "", do_info_balloon,
1641 "", "show balloon information" },
1642 { NULL, NULL, },
1645 /*******************************************************************/
1647 static const char *pch;
1648 static jmp_buf expr_env;
1650 #define MD_TLONG 0
1651 #define MD_I32 1
1653 typedef struct MonitorDef {
1654 const char *name;
1655 int offset;
1656 target_long (*get_value)(const struct MonitorDef *md, int val);
1657 int type;
1658 } MonitorDef;
1660 #if defined(TARGET_I386)
1661 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1663 CPUState *env = mon_get_cpu();
1664 if (!env)
1665 return 0;
1666 return env->eip + env->segs[R_CS].base;
1668 #endif
1670 #if defined(TARGET_PPC)
1671 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1673 CPUState *env = mon_get_cpu();
1674 unsigned int u;
1675 int i;
1677 if (!env)
1678 return 0;
1680 u = 0;
1681 for (i = 0; i < 8; i++)
1682 u |= env->crf[i] << (32 - (4 * i));
1684 return u;
1687 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1689 CPUState *env = mon_get_cpu();
1690 if (!env)
1691 return 0;
1692 return env->msr;
1695 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1697 CPUState *env = mon_get_cpu();
1698 if (!env)
1699 return 0;
1700 return env->xer;
1703 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1705 CPUState *env = mon_get_cpu();
1706 if (!env)
1707 return 0;
1708 return cpu_ppc_load_decr(env);
1711 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1713 CPUState *env = mon_get_cpu();
1714 if (!env)
1715 return 0;
1716 return cpu_ppc_load_tbu(env);
1719 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1721 CPUState *env = mon_get_cpu();
1722 if (!env)
1723 return 0;
1724 return cpu_ppc_load_tbl(env);
1726 #endif
1728 #if defined(TARGET_SPARC)
1729 #ifndef TARGET_SPARC64
1730 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1732 CPUState *env = mon_get_cpu();
1733 if (!env)
1734 return 0;
1735 return GET_PSR(env);
1737 #endif
1739 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1741 CPUState *env = mon_get_cpu();
1742 if (!env)
1743 return 0;
1744 return env->regwptr[val];
1746 #endif
1748 static const MonitorDef monitor_defs[] = {
1749 #ifdef TARGET_I386
1751 #define SEG(name, seg) \
1752 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1753 { name ".base", offsetof(CPUState, segs[seg].base) },\
1754 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1756 { "eax", offsetof(CPUState, regs[0]) },
1757 { "ecx", offsetof(CPUState, regs[1]) },
1758 { "edx", offsetof(CPUState, regs[2]) },
1759 { "ebx", offsetof(CPUState, regs[3]) },
1760 { "esp|sp", offsetof(CPUState, regs[4]) },
1761 { "ebp|fp", offsetof(CPUState, regs[5]) },
1762 { "esi", offsetof(CPUState, regs[6]) },
1763 { "edi", offsetof(CPUState, regs[7]) },
1764 #ifdef TARGET_X86_64
1765 { "r8", offsetof(CPUState, regs[8]) },
1766 { "r9", offsetof(CPUState, regs[9]) },
1767 { "r10", offsetof(CPUState, regs[10]) },
1768 { "r11", offsetof(CPUState, regs[11]) },
1769 { "r12", offsetof(CPUState, regs[12]) },
1770 { "r13", offsetof(CPUState, regs[13]) },
1771 { "r14", offsetof(CPUState, regs[14]) },
1772 { "r15", offsetof(CPUState, regs[15]) },
1773 #endif
1774 { "eflags", offsetof(CPUState, eflags) },
1775 { "eip", offsetof(CPUState, eip) },
1776 SEG("cs", R_CS)
1777 SEG("ds", R_DS)
1778 SEG("es", R_ES)
1779 SEG("ss", R_SS)
1780 SEG("fs", R_FS)
1781 SEG("gs", R_GS)
1782 { "pc", 0, monitor_get_pc, },
1783 #elif defined(TARGET_PPC)
1784 /* General purpose registers */
1785 { "r0", offsetof(CPUState, gpr[0]) },
1786 { "r1", offsetof(CPUState, gpr[1]) },
1787 { "r2", offsetof(CPUState, gpr[2]) },
1788 { "r3", offsetof(CPUState, gpr[3]) },
1789 { "r4", offsetof(CPUState, gpr[4]) },
1790 { "r5", offsetof(CPUState, gpr[5]) },
1791 { "r6", offsetof(CPUState, gpr[6]) },
1792 { "r7", offsetof(CPUState, gpr[7]) },
1793 { "r8", offsetof(CPUState, gpr[8]) },
1794 { "r9", offsetof(CPUState, gpr[9]) },
1795 { "r10", offsetof(CPUState, gpr[10]) },
1796 { "r11", offsetof(CPUState, gpr[11]) },
1797 { "r12", offsetof(CPUState, gpr[12]) },
1798 { "r13", offsetof(CPUState, gpr[13]) },
1799 { "r14", offsetof(CPUState, gpr[14]) },
1800 { "r15", offsetof(CPUState, gpr[15]) },
1801 { "r16", offsetof(CPUState, gpr[16]) },
1802 { "r17", offsetof(CPUState, gpr[17]) },
1803 { "r18", offsetof(CPUState, gpr[18]) },
1804 { "r19", offsetof(CPUState, gpr[19]) },
1805 { "r20", offsetof(CPUState, gpr[20]) },
1806 { "r21", offsetof(CPUState, gpr[21]) },
1807 { "r22", offsetof(CPUState, gpr[22]) },
1808 { "r23", offsetof(CPUState, gpr[23]) },
1809 { "r24", offsetof(CPUState, gpr[24]) },
1810 { "r25", offsetof(CPUState, gpr[25]) },
1811 { "r26", offsetof(CPUState, gpr[26]) },
1812 { "r27", offsetof(CPUState, gpr[27]) },
1813 { "r28", offsetof(CPUState, gpr[28]) },
1814 { "r29", offsetof(CPUState, gpr[29]) },
1815 { "r30", offsetof(CPUState, gpr[30]) },
1816 { "r31", offsetof(CPUState, gpr[31]) },
1817 /* Floating point registers */
1818 { "f0", offsetof(CPUState, fpr[0]) },
1819 { "f1", offsetof(CPUState, fpr[1]) },
1820 { "f2", offsetof(CPUState, fpr[2]) },
1821 { "f3", offsetof(CPUState, fpr[3]) },
1822 { "f4", offsetof(CPUState, fpr[4]) },
1823 { "f5", offsetof(CPUState, fpr[5]) },
1824 { "f6", offsetof(CPUState, fpr[6]) },
1825 { "f7", offsetof(CPUState, fpr[7]) },
1826 { "f8", offsetof(CPUState, fpr[8]) },
1827 { "f9", offsetof(CPUState, fpr[9]) },
1828 { "f10", offsetof(CPUState, fpr[10]) },
1829 { "f11", offsetof(CPUState, fpr[11]) },
1830 { "f12", offsetof(CPUState, fpr[12]) },
1831 { "f13", offsetof(CPUState, fpr[13]) },
1832 { "f14", offsetof(CPUState, fpr[14]) },
1833 { "f15", offsetof(CPUState, fpr[15]) },
1834 { "f16", offsetof(CPUState, fpr[16]) },
1835 { "f17", offsetof(CPUState, fpr[17]) },
1836 { "f18", offsetof(CPUState, fpr[18]) },
1837 { "f19", offsetof(CPUState, fpr[19]) },
1838 { "f20", offsetof(CPUState, fpr[20]) },
1839 { "f21", offsetof(CPUState, fpr[21]) },
1840 { "f22", offsetof(CPUState, fpr[22]) },
1841 { "f23", offsetof(CPUState, fpr[23]) },
1842 { "f24", offsetof(CPUState, fpr[24]) },
1843 { "f25", offsetof(CPUState, fpr[25]) },
1844 { "f26", offsetof(CPUState, fpr[26]) },
1845 { "f27", offsetof(CPUState, fpr[27]) },
1846 { "f28", offsetof(CPUState, fpr[28]) },
1847 { "f29", offsetof(CPUState, fpr[29]) },
1848 { "f30", offsetof(CPUState, fpr[30]) },
1849 { "f31", offsetof(CPUState, fpr[31]) },
1850 { "fpscr", offsetof(CPUState, fpscr) },
1851 /* Next instruction pointer */
1852 { "nip|pc", offsetof(CPUState, nip) },
1853 { "lr", offsetof(CPUState, lr) },
1854 { "ctr", offsetof(CPUState, ctr) },
1855 { "decr", 0, &monitor_get_decr, },
1856 { "ccr", 0, &monitor_get_ccr, },
1857 /* Machine state register */
1858 { "msr", 0, &monitor_get_msr, },
1859 { "xer", 0, &monitor_get_xer, },
1860 { "tbu", 0, &monitor_get_tbu, },
1861 { "tbl", 0, &monitor_get_tbl, },
1862 #if defined(TARGET_PPC64)
1863 /* Address space register */
1864 { "asr", offsetof(CPUState, asr) },
1865 #endif
1866 /* Segment registers */
1867 { "sdr1", offsetof(CPUState, sdr1) },
1868 { "sr0", offsetof(CPUState, sr[0]) },
1869 { "sr1", offsetof(CPUState, sr[1]) },
1870 { "sr2", offsetof(CPUState, sr[2]) },
1871 { "sr3", offsetof(CPUState, sr[3]) },
1872 { "sr4", offsetof(CPUState, sr[4]) },
1873 { "sr5", offsetof(CPUState, sr[5]) },
1874 { "sr6", offsetof(CPUState, sr[6]) },
1875 { "sr7", offsetof(CPUState, sr[7]) },
1876 { "sr8", offsetof(CPUState, sr[8]) },
1877 { "sr9", offsetof(CPUState, sr[9]) },
1878 { "sr10", offsetof(CPUState, sr[10]) },
1879 { "sr11", offsetof(CPUState, sr[11]) },
1880 { "sr12", offsetof(CPUState, sr[12]) },
1881 { "sr13", offsetof(CPUState, sr[13]) },
1882 { "sr14", offsetof(CPUState, sr[14]) },
1883 { "sr15", offsetof(CPUState, sr[15]) },
1884 /* Too lazy to put BATs and SPRs ... */
1885 #elif defined(TARGET_SPARC)
1886 { "g0", offsetof(CPUState, gregs[0]) },
1887 { "g1", offsetof(CPUState, gregs[1]) },
1888 { "g2", offsetof(CPUState, gregs[2]) },
1889 { "g3", offsetof(CPUState, gregs[3]) },
1890 { "g4", offsetof(CPUState, gregs[4]) },
1891 { "g5", offsetof(CPUState, gregs[5]) },
1892 { "g6", offsetof(CPUState, gregs[6]) },
1893 { "g7", offsetof(CPUState, gregs[7]) },
1894 { "o0", 0, monitor_get_reg },
1895 { "o1", 1, monitor_get_reg },
1896 { "o2", 2, monitor_get_reg },
1897 { "o3", 3, monitor_get_reg },
1898 { "o4", 4, monitor_get_reg },
1899 { "o5", 5, monitor_get_reg },
1900 { "o6", 6, monitor_get_reg },
1901 { "o7", 7, monitor_get_reg },
1902 { "l0", 8, monitor_get_reg },
1903 { "l1", 9, monitor_get_reg },
1904 { "l2", 10, monitor_get_reg },
1905 { "l3", 11, monitor_get_reg },
1906 { "l4", 12, monitor_get_reg },
1907 { "l5", 13, monitor_get_reg },
1908 { "l6", 14, monitor_get_reg },
1909 { "l7", 15, monitor_get_reg },
1910 { "i0", 16, monitor_get_reg },
1911 { "i1", 17, monitor_get_reg },
1912 { "i2", 18, monitor_get_reg },
1913 { "i3", 19, monitor_get_reg },
1914 { "i4", 20, monitor_get_reg },
1915 { "i5", 21, monitor_get_reg },
1916 { "i6", 22, monitor_get_reg },
1917 { "i7", 23, monitor_get_reg },
1918 { "pc", offsetof(CPUState, pc) },
1919 { "npc", offsetof(CPUState, npc) },
1920 { "y", offsetof(CPUState, y) },
1921 #ifndef TARGET_SPARC64
1922 { "psr", 0, &monitor_get_psr, },
1923 { "wim", offsetof(CPUState, wim) },
1924 #endif
1925 { "tbr", offsetof(CPUState, tbr) },
1926 { "fsr", offsetof(CPUState, fsr) },
1927 { "f0", offsetof(CPUState, fpr[0]) },
1928 { "f1", offsetof(CPUState, fpr[1]) },
1929 { "f2", offsetof(CPUState, fpr[2]) },
1930 { "f3", offsetof(CPUState, fpr[3]) },
1931 { "f4", offsetof(CPUState, fpr[4]) },
1932 { "f5", offsetof(CPUState, fpr[5]) },
1933 { "f6", offsetof(CPUState, fpr[6]) },
1934 { "f7", offsetof(CPUState, fpr[7]) },
1935 { "f8", offsetof(CPUState, fpr[8]) },
1936 { "f9", offsetof(CPUState, fpr[9]) },
1937 { "f10", offsetof(CPUState, fpr[10]) },
1938 { "f11", offsetof(CPUState, fpr[11]) },
1939 { "f12", offsetof(CPUState, fpr[12]) },
1940 { "f13", offsetof(CPUState, fpr[13]) },
1941 { "f14", offsetof(CPUState, fpr[14]) },
1942 { "f15", offsetof(CPUState, fpr[15]) },
1943 { "f16", offsetof(CPUState, fpr[16]) },
1944 { "f17", offsetof(CPUState, fpr[17]) },
1945 { "f18", offsetof(CPUState, fpr[18]) },
1946 { "f19", offsetof(CPUState, fpr[19]) },
1947 { "f20", offsetof(CPUState, fpr[20]) },
1948 { "f21", offsetof(CPUState, fpr[21]) },
1949 { "f22", offsetof(CPUState, fpr[22]) },
1950 { "f23", offsetof(CPUState, fpr[23]) },
1951 { "f24", offsetof(CPUState, fpr[24]) },
1952 { "f25", offsetof(CPUState, fpr[25]) },
1953 { "f26", offsetof(CPUState, fpr[26]) },
1954 { "f27", offsetof(CPUState, fpr[27]) },
1955 { "f28", offsetof(CPUState, fpr[28]) },
1956 { "f29", offsetof(CPUState, fpr[29]) },
1957 { "f30", offsetof(CPUState, fpr[30]) },
1958 { "f31", offsetof(CPUState, fpr[31]) },
1959 #ifdef TARGET_SPARC64
1960 { "f32", offsetof(CPUState, fpr[32]) },
1961 { "f34", offsetof(CPUState, fpr[34]) },
1962 { "f36", offsetof(CPUState, fpr[36]) },
1963 { "f38", offsetof(CPUState, fpr[38]) },
1964 { "f40", offsetof(CPUState, fpr[40]) },
1965 { "f42", offsetof(CPUState, fpr[42]) },
1966 { "f44", offsetof(CPUState, fpr[44]) },
1967 { "f46", offsetof(CPUState, fpr[46]) },
1968 { "f48", offsetof(CPUState, fpr[48]) },
1969 { "f50", offsetof(CPUState, fpr[50]) },
1970 { "f52", offsetof(CPUState, fpr[52]) },
1971 { "f54", offsetof(CPUState, fpr[54]) },
1972 { "f56", offsetof(CPUState, fpr[56]) },
1973 { "f58", offsetof(CPUState, fpr[58]) },
1974 { "f60", offsetof(CPUState, fpr[60]) },
1975 { "f62", offsetof(CPUState, fpr[62]) },
1976 { "asi", offsetof(CPUState, asi) },
1977 { "pstate", offsetof(CPUState, pstate) },
1978 { "cansave", offsetof(CPUState, cansave) },
1979 { "canrestore", offsetof(CPUState, canrestore) },
1980 { "otherwin", offsetof(CPUState, otherwin) },
1981 { "wstate", offsetof(CPUState, wstate) },
1982 { "cleanwin", offsetof(CPUState, cleanwin) },
1983 { "fprs", offsetof(CPUState, fprs) },
1984 #endif
1985 #endif
1986 { NULL },
1989 static void expr_error(const char *msg)
1991 term_printf("%s\n", msg);
1992 longjmp(expr_env, 1);
1995 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1996 static int get_monitor_def(target_long *pval, const char *name)
1998 const MonitorDef *md;
1999 void *ptr;
2001 for(md = monitor_defs; md->name != NULL; md++) {
2002 if (compare_cmd(name, md->name)) {
2003 if (md->get_value) {
2004 *pval = md->get_value(md, md->offset);
2005 } else {
2006 CPUState *env = mon_get_cpu();
2007 if (!env)
2008 return -2;
2009 ptr = (uint8_t *)env + md->offset;
2010 switch(md->type) {
2011 case MD_I32:
2012 *pval = *(int32_t *)ptr;
2013 break;
2014 case MD_TLONG:
2015 *pval = *(target_long *)ptr;
2016 break;
2017 default:
2018 *pval = 0;
2019 break;
2022 return 0;
2025 return -1;
2028 static void next(void)
2030 if (pch != '\0') {
2031 pch++;
2032 while (qemu_isspace(*pch))
2033 pch++;
2037 static int64_t expr_sum(void);
2039 static int64_t expr_unary(void)
2041 int64_t n;
2042 char *p;
2043 int ret;
2045 switch(*pch) {
2046 case '+':
2047 next();
2048 n = expr_unary();
2049 break;
2050 case '-':
2051 next();
2052 n = -expr_unary();
2053 break;
2054 case '~':
2055 next();
2056 n = ~expr_unary();
2057 break;
2058 case '(':
2059 next();
2060 n = expr_sum();
2061 if (*pch != ')') {
2062 expr_error("')' expected");
2064 next();
2065 break;
2066 case '\'':
2067 pch++;
2068 if (*pch == '\0')
2069 expr_error("character constant expected");
2070 n = *pch;
2071 pch++;
2072 if (*pch != '\'')
2073 expr_error("missing terminating \' character");
2074 next();
2075 break;
2076 case '$':
2078 char buf[128], *q;
2079 target_long reg=0;
2081 pch++;
2082 q = buf;
2083 while ((*pch >= 'a' && *pch <= 'z') ||
2084 (*pch >= 'A' && *pch <= 'Z') ||
2085 (*pch >= '0' && *pch <= '9') ||
2086 *pch == '_' || *pch == '.') {
2087 if ((q - buf) < sizeof(buf) - 1)
2088 *q++ = *pch;
2089 pch++;
2091 while (qemu_isspace(*pch))
2092 pch++;
2093 *q = 0;
2094 ret = get_monitor_def(&reg, buf);
2095 if (ret == -1)
2096 expr_error("unknown register");
2097 else if (ret == -2)
2098 expr_error("no cpu defined");
2099 n = reg;
2101 break;
2102 case '\0':
2103 expr_error("unexpected end of expression");
2104 n = 0;
2105 break;
2106 default:
2107 #if TARGET_PHYS_ADDR_BITS > 32
2108 n = strtoull(pch, &p, 0);
2109 #else
2110 n = strtoul(pch, &p, 0);
2111 #endif
2112 if (pch == p) {
2113 expr_error("invalid char in expression");
2115 pch = p;
2116 while (qemu_isspace(*pch))
2117 pch++;
2118 break;
2120 return n;
2124 static int64_t expr_prod(void)
2126 int64_t val, val2;
2127 int op;
2129 val = expr_unary();
2130 for(;;) {
2131 op = *pch;
2132 if (op != '*' && op != '/' && op != '%')
2133 break;
2134 next();
2135 val2 = expr_unary();
2136 switch(op) {
2137 default:
2138 case '*':
2139 val *= val2;
2140 break;
2141 case '/':
2142 case '%':
2143 if (val2 == 0)
2144 expr_error("division by zero");
2145 if (op == '/')
2146 val /= val2;
2147 else
2148 val %= val2;
2149 break;
2152 return val;
2155 static int64_t expr_logic(void)
2157 int64_t val, val2;
2158 int op;
2160 val = expr_prod();
2161 for(;;) {
2162 op = *pch;
2163 if (op != '&' && op != '|' && op != '^')
2164 break;
2165 next();
2166 val2 = expr_prod();
2167 switch(op) {
2168 default:
2169 case '&':
2170 val &= val2;
2171 break;
2172 case '|':
2173 val |= val2;
2174 break;
2175 case '^':
2176 val ^= val2;
2177 break;
2180 return val;
2183 static int64_t expr_sum(void)
2185 int64_t val, val2;
2186 int op;
2188 val = expr_logic();
2189 for(;;) {
2190 op = *pch;
2191 if (op != '+' && op != '-')
2192 break;
2193 next();
2194 val2 = expr_logic();
2195 if (op == '+')
2196 val += val2;
2197 else
2198 val -= val2;
2200 return val;
2203 static int get_expr(int64_t *pval, const char **pp)
2205 pch = *pp;
2206 if (setjmp(expr_env)) {
2207 *pp = pch;
2208 return -1;
2210 while (qemu_isspace(*pch))
2211 pch++;
2212 *pval = expr_sum();
2213 *pp = pch;
2214 return 0;
2217 static int get_str(char *buf, int buf_size, const char **pp)
2219 const char *p;
2220 char *q;
2221 int c;
2223 q = buf;
2224 p = *pp;
2225 while (qemu_isspace(*p))
2226 p++;
2227 if (*p == '\0') {
2228 fail:
2229 *q = '\0';
2230 *pp = p;
2231 return -1;
2233 if (*p == '\"') {
2234 p++;
2235 while (*p != '\0' && *p != '\"') {
2236 if (*p == '\\') {
2237 p++;
2238 c = *p++;
2239 switch(c) {
2240 case 'n':
2241 c = '\n';
2242 break;
2243 case 'r':
2244 c = '\r';
2245 break;
2246 case '\\':
2247 case '\'':
2248 case '\"':
2249 break;
2250 default:
2251 qemu_printf("unsupported escape code: '\\%c'\n", c);
2252 goto fail;
2254 if ((q - buf) < buf_size - 1) {
2255 *q++ = c;
2257 } else {
2258 if ((q - buf) < buf_size - 1) {
2259 *q++ = *p;
2261 p++;
2264 if (*p != '\"') {
2265 qemu_printf("unterminated string\n");
2266 goto fail;
2268 p++;
2269 } else {
2270 while (*p != '\0' && !qemu_isspace(*p)) {
2271 if ((q - buf) < buf_size - 1) {
2272 *q++ = *p;
2274 p++;
2277 *q = '\0';
2278 *pp = p;
2279 return 0;
2282 static int default_fmt_format = 'x';
2283 static int default_fmt_size = 4;
2285 #define MAX_ARGS 16
2287 static void monitor_handle_command(const char *cmdline)
2289 const char *p, *pstart, *typestr;
2290 char *q;
2291 int c, nb_args, len, i, has_arg;
2292 const term_cmd_t *cmd;
2293 char cmdname[256];
2294 char buf[1024];
2295 void *str_allocated[MAX_ARGS];
2296 void *args[MAX_ARGS];
2297 void (*handler_0)(void);
2298 void (*handler_1)(void *arg0);
2299 void (*handler_2)(void *arg0, void *arg1);
2300 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2301 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2302 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2303 void *arg4);
2304 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2305 void *arg4, void *arg5);
2306 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2307 void *arg4, void *arg5, void *arg6);
2309 #ifdef DEBUG
2310 term_printf("command='%s'\n", cmdline);
2311 #endif
2313 /* extract the command name */
2314 p = cmdline;
2315 q = cmdname;
2316 while (qemu_isspace(*p))
2317 p++;
2318 if (*p == '\0')
2319 return;
2320 pstart = p;
2321 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2322 p++;
2323 len = p - pstart;
2324 if (len > sizeof(cmdname) - 1)
2325 len = sizeof(cmdname) - 1;
2326 memcpy(cmdname, pstart, len);
2327 cmdname[len] = '\0';
2329 /* find the command */
2330 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2331 if (compare_cmd(cmdname, cmd->name))
2332 goto found;
2334 term_printf("unknown command: '%s'\n", cmdname);
2335 return;
2336 found:
2338 for(i = 0; i < MAX_ARGS; i++)
2339 str_allocated[i] = NULL;
2341 /* parse the parameters */
2342 typestr = cmd->args_type;
2343 nb_args = 0;
2344 for(;;) {
2345 c = *typestr;
2346 if (c == '\0')
2347 break;
2348 typestr++;
2349 switch(c) {
2350 case 'F':
2351 case 'B':
2352 case 's':
2354 int ret;
2355 char *str;
2357 while (qemu_isspace(*p))
2358 p++;
2359 if (*typestr == '?') {
2360 typestr++;
2361 if (*p == '\0') {
2362 /* no optional string: NULL argument */
2363 str = NULL;
2364 goto add_str;
2367 ret = get_str(buf, sizeof(buf), &p);
2368 if (ret < 0) {
2369 switch(c) {
2370 case 'F':
2371 term_printf("%s: filename expected\n", cmdname);
2372 break;
2373 case 'B':
2374 term_printf("%s: block device name expected\n", cmdname);
2375 break;
2376 default:
2377 term_printf("%s: string expected\n", cmdname);
2378 break;
2380 goto fail;
2382 str = qemu_malloc(strlen(buf) + 1);
2383 pstrcpy(str, sizeof(buf), buf);
2384 str_allocated[nb_args] = str;
2385 add_str:
2386 if (nb_args >= MAX_ARGS) {
2387 error_args:
2388 term_printf("%s: too many arguments\n", cmdname);
2389 goto fail;
2391 args[nb_args++] = str;
2393 break;
2394 case '/':
2396 int count, format, size;
2398 while (qemu_isspace(*p))
2399 p++;
2400 if (*p == '/') {
2401 /* format found */
2402 p++;
2403 count = 1;
2404 if (qemu_isdigit(*p)) {
2405 count = 0;
2406 while (qemu_isdigit(*p)) {
2407 count = count * 10 + (*p - '0');
2408 p++;
2411 size = -1;
2412 format = -1;
2413 for(;;) {
2414 switch(*p) {
2415 case 'o':
2416 case 'd':
2417 case 'u':
2418 case 'x':
2419 case 'i':
2420 case 'c':
2421 format = *p++;
2422 break;
2423 case 'b':
2424 size = 1;
2425 p++;
2426 break;
2427 case 'h':
2428 size = 2;
2429 p++;
2430 break;
2431 case 'w':
2432 size = 4;
2433 p++;
2434 break;
2435 case 'g':
2436 case 'L':
2437 size = 8;
2438 p++;
2439 break;
2440 default:
2441 goto next;
2444 next:
2445 if (*p != '\0' && !qemu_isspace(*p)) {
2446 term_printf("invalid char in format: '%c'\n", *p);
2447 goto fail;
2449 if (format < 0)
2450 format = default_fmt_format;
2451 if (format != 'i') {
2452 /* for 'i', not specifying a size gives -1 as size */
2453 if (size < 0)
2454 size = default_fmt_size;
2455 default_fmt_size = size;
2457 default_fmt_format = format;
2458 } else {
2459 count = 1;
2460 format = default_fmt_format;
2461 if (format != 'i') {
2462 size = default_fmt_size;
2463 } else {
2464 size = -1;
2467 if (nb_args + 3 > MAX_ARGS)
2468 goto error_args;
2469 args[nb_args++] = (void*)(long)count;
2470 args[nb_args++] = (void*)(long)format;
2471 args[nb_args++] = (void*)(long)size;
2473 break;
2474 case 'i':
2475 case 'l':
2477 int64_t val;
2479 while (qemu_isspace(*p))
2480 p++;
2481 if (*typestr == '?' || *typestr == '.') {
2482 if (*typestr == '?') {
2483 if (*p == '\0')
2484 has_arg = 0;
2485 else
2486 has_arg = 1;
2487 } else {
2488 if (*p == '.') {
2489 p++;
2490 while (qemu_isspace(*p))
2491 p++;
2492 has_arg = 1;
2493 } else {
2494 has_arg = 0;
2497 typestr++;
2498 if (nb_args >= MAX_ARGS)
2499 goto error_args;
2500 args[nb_args++] = (void *)(long)has_arg;
2501 if (!has_arg) {
2502 if (nb_args >= MAX_ARGS)
2503 goto error_args;
2504 val = -1;
2505 goto add_num;
2508 if (get_expr(&val, &p))
2509 goto fail;
2510 add_num:
2511 if (c == 'i') {
2512 if (nb_args >= MAX_ARGS)
2513 goto error_args;
2514 args[nb_args++] = (void *)(long)val;
2515 } else {
2516 if ((nb_args + 1) >= MAX_ARGS)
2517 goto error_args;
2518 #if TARGET_PHYS_ADDR_BITS > 32
2519 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2520 #else
2521 args[nb_args++] = (void *)0;
2522 #endif
2523 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2526 break;
2527 case '-':
2529 int has_option;
2530 /* option */
2532 c = *typestr++;
2533 if (c == '\0')
2534 goto bad_type;
2535 while (qemu_isspace(*p))
2536 p++;
2537 has_option = 0;
2538 if (*p == '-') {
2539 p++;
2540 if (*p != c) {
2541 term_printf("%s: unsupported option -%c\n",
2542 cmdname, *p);
2543 goto fail;
2545 p++;
2546 has_option = 1;
2548 if (nb_args >= MAX_ARGS)
2549 goto error_args;
2550 args[nb_args++] = (void *)(long)has_option;
2552 break;
2553 default:
2554 bad_type:
2555 term_printf("%s: unknown type '%c'\n", cmdname, c);
2556 goto fail;
2559 /* check that all arguments were parsed */
2560 while (qemu_isspace(*p))
2561 p++;
2562 if (*p != '\0') {
2563 term_printf("%s: extraneous characters at the end of line\n",
2564 cmdname);
2565 goto fail;
2568 switch(nb_args) {
2569 case 0:
2570 handler_0 = cmd->handler;
2571 handler_0();
2572 break;
2573 case 1:
2574 handler_1 = cmd->handler;
2575 handler_1(args[0]);
2576 break;
2577 case 2:
2578 handler_2 = cmd->handler;
2579 handler_2(args[0], args[1]);
2580 break;
2581 case 3:
2582 handler_3 = cmd->handler;
2583 handler_3(args[0], args[1], args[2]);
2584 break;
2585 case 4:
2586 handler_4 = cmd->handler;
2587 handler_4(args[0], args[1], args[2], args[3]);
2588 break;
2589 case 5:
2590 handler_5 = cmd->handler;
2591 handler_5(args[0], args[1], args[2], args[3], args[4]);
2592 break;
2593 case 6:
2594 handler_6 = cmd->handler;
2595 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2596 break;
2597 case 7:
2598 handler_7 = cmd->handler;
2599 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2600 break;
2601 default:
2602 term_printf("unsupported number of arguments: %d\n", nb_args);
2603 goto fail;
2605 fail:
2606 for(i = 0; i < MAX_ARGS; i++)
2607 qemu_free(str_allocated[i]);
2608 return;
2611 static void cmd_completion(const char *name, const char *list)
2613 const char *p, *pstart;
2614 char cmd[128];
2615 int len;
2617 p = list;
2618 for(;;) {
2619 pstart = p;
2620 p = strchr(p, '|');
2621 if (!p)
2622 p = pstart + strlen(pstart);
2623 len = p - pstart;
2624 if (len > sizeof(cmd) - 2)
2625 len = sizeof(cmd) - 2;
2626 memcpy(cmd, pstart, len);
2627 cmd[len] = '\0';
2628 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2629 add_completion(cmd);
2631 if (*p == '\0')
2632 break;
2633 p++;
2637 static void file_completion(const char *input)
2639 DIR *ffs;
2640 struct dirent *d;
2641 char path[1024];
2642 char file[1024], file_prefix[1024];
2643 int input_path_len;
2644 const char *p;
2646 p = strrchr(input, '/');
2647 if (!p) {
2648 input_path_len = 0;
2649 pstrcpy(file_prefix, sizeof(file_prefix), input);
2650 pstrcpy(path, sizeof(path), ".");
2651 } else {
2652 input_path_len = p - input + 1;
2653 memcpy(path, input, input_path_len);
2654 if (input_path_len > sizeof(path) - 1)
2655 input_path_len = sizeof(path) - 1;
2656 path[input_path_len] = '\0';
2657 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2659 #ifdef DEBUG_COMPLETION
2660 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2661 #endif
2662 ffs = opendir(path);
2663 if (!ffs)
2664 return;
2665 for(;;) {
2666 struct stat sb;
2667 d = readdir(ffs);
2668 if (!d)
2669 break;
2670 if (strstart(d->d_name, file_prefix, NULL)) {
2671 memcpy(file, input, input_path_len);
2672 if (input_path_len < sizeof(file))
2673 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2674 d->d_name);
2675 /* stat the file to find out if it's a directory.
2676 * In that case add a slash to speed up typing long paths
2678 stat(file, &sb);
2679 if(S_ISDIR(sb.st_mode))
2680 pstrcat(file, sizeof(file), "/");
2681 add_completion(file);
2684 closedir(ffs);
2687 static void block_completion_it(void *opaque, const char *name)
2689 const char *input = opaque;
2691 if (input[0] == '\0' ||
2692 !strncmp(name, (char *)input, strlen(input))) {
2693 add_completion(name);
2697 /* NOTE: this parser is an approximate form of the real command parser */
2698 static void parse_cmdline(const char *cmdline,
2699 int *pnb_args, char **args)
2701 const char *p;
2702 int nb_args, ret;
2703 char buf[1024];
2705 p = cmdline;
2706 nb_args = 0;
2707 for(;;) {
2708 while (qemu_isspace(*p))
2709 p++;
2710 if (*p == '\0')
2711 break;
2712 if (nb_args >= MAX_ARGS)
2713 break;
2714 ret = get_str(buf, sizeof(buf), &p);
2715 args[nb_args] = qemu_strdup(buf);
2716 nb_args++;
2717 if (ret < 0)
2718 break;
2720 *pnb_args = nb_args;
2723 void readline_find_completion(const char *cmdline)
2725 const char *cmdname;
2726 char *args[MAX_ARGS];
2727 int nb_args, i, len;
2728 const char *ptype, *str;
2729 const term_cmd_t *cmd;
2730 const KeyDef *key;
2732 parse_cmdline(cmdline, &nb_args, args);
2733 #ifdef DEBUG_COMPLETION
2734 for(i = 0; i < nb_args; i++) {
2735 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2737 #endif
2739 /* if the line ends with a space, it means we want to complete the
2740 next arg */
2741 len = strlen(cmdline);
2742 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2743 if (nb_args >= MAX_ARGS)
2744 return;
2745 args[nb_args++] = qemu_strdup("");
2747 if (nb_args <= 1) {
2748 /* command completion */
2749 if (nb_args == 0)
2750 cmdname = "";
2751 else
2752 cmdname = args[0];
2753 completion_index = strlen(cmdname);
2754 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2755 cmd_completion(cmdname, cmd->name);
2757 } else {
2758 /* find the command */
2759 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2760 if (compare_cmd(args[0], cmd->name))
2761 goto found;
2763 return;
2764 found:
2765 ptype = cmd->args_type;
2766 for(i = 0; i < nb_args - 2; i++) {
2767 if (*ptype != '\0') {
2768 ptype++;
2769 while (*ptype == '?')
2770 ptype++;
2773 str = args[nb_args - 1];
2774 switch(*ptype) {
2775 case 'F':
2776 /* file completion */
2777 completion_index = strlen(str);
2778 file_completion(str);
2779 break;
2780 case 'B':
2781 /* block device name completion */
2782 completion_index = strlen(str);
2783 bdrv_iterate(block_completion_it, (void *)str);
2784 break;
2785 case 's':
2786 /* XXX: more generic ? */
2787 if (!strcmp(cmd->name, "info")) {
2788 completion_index = strlen(str);
2789 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2790 cmd_completion(str, cmd->name);
2792 } else if (!strcmp(cmd->name, "sendkey")) {
2793 completion_index = strlen(str);
2794 for(key = key_defs; key->name != NULL; key++) {
2795 cmd_completion(str, key->name);
2798 break;
2799 default:
2800 break;
2803 for(i = 0; i < nb_args; i++)
2804 qemu_free(args[i]);
2807 static int term_can_read(void *opaque)
2809 return 128;
2812 static void term_read(void *opaque, const uint8_t *buf, int size)
2814 int i;
2815 for(i = 0; i < size; i++)
2816 readline_handle_byte(buf[i]);
2819 static int monitor_suspended;
2821 static void monitor_handle_command1(void *opaque, const char *cmdline)
2823 monitor_handle_command(cmdline);
2824 if (!monitor_suspended)
2825 monitor_start_input();
2826 else
2827 monitor_suspended = 2;
2830 void monitor_suspend(void)
2832 monitor_suspended = 1;
2835 void monitor_resume(void)
2837 if (monitor_suspended == 2)
2838 monitor_start_input();
2839 monitor_suspended = 0;
2842 static void monitor_start_input(void)
2844 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2847 static void term_event(void *opaque, int event)
2849 if (event != CHR_EVENT_RESET)
2850 return;
2852 if (!hide_banner)
2853 term_printf("QEMU %s monitor - type 'help' for more information\n",
2854 QEMU_VERSION);
2855 monitor_start_input();
2858 static int is_first_init = 1;
2860 void monitor_init(CharDriverState *hd, int show_banner)
2862 int i;
2864 if (is_first_init) {
2865 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2866 if (!key_timer)
2867 return;
2868 for (i = 0; i < MAX_MON; i++) {
2869 monitor_hd[i] = NULL;
2871 is_first_init = 0;
2873 for (i = 0; i < MAX_MON; i++) {
2874 if (monitor_hd[i] == NULL) {
2875 monitor_hd[i] = hd;
2876 break;
2880 hide_banner = !show_banner;
2882 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2884 readline_start("", 0, monitor_handle_command1, NULL);
2887 /* XXX: use threads ? */
2888 /* modal monitor readline */
2889 static int monitor_readline_started;
2890 static char *monitor_readline_buf;
2891 static int monitor_readline_buf_size;
2893 static void monitor_readline_cb(void *opaque, const char *input)
2895 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2896 monitor_readline_started = 0;
2899 void monitor_readline(const char *prompt, int is_password,
2900 char *buf, int buf_size)
2902 int i;
2903 int old_focus[MAX_MON];
2905 if (is_password) {
2906 for (i = 0; i < MAX_MON; i++) {
2907 old_focus[i] = 0;
2908 if (monitor_hd[i]) {
2909 old_focus[i] = monitor_hd[i]->focus;
2910 monitor_hd[i]->focus = 0;
2911 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2916 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2917 monitor_readline_buf = buf;
2918 monitor_readline_buf_size = buf_size;
2919 monitor_readline_started = 1;
2920 while (monitor_readline_started) {
2921 main_loop_wait(10);
2923 /* restore original focus */
2924 if (is_password) {
2925 for (i = 0; i < MAX_MON; i++)
2926 if (old_focus[i])
2927 monitor_hd[i]->focus = old_focus[i];