kvm: testsuite: compile fix - avoid raw string literal
[qemu-kvm/fedora.git] / monitor.c
blob49091e4216d3c3211ff7868bd0b2faf95e8430fd
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);
81 static void monitor_readline(const char *prompt, int is_password,
82 char *buf, int buf_size);
84 static CPUState *mon_cpu = NULL;
86 void term_flush(void)
88 int i;
89 if (term_outbuf_index > 0) {
90 for (i = 0; i < MAX_MON; i++)
91 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
92 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
93 term_outbuf_index = 0;
97 /* flush at every end of line or if the buffer is full */
98 void term_puts(const char *str)
100 char c;
101 for(;;) {
102 c = *str++;
103 if (c == '\0')
104 break;
105 if (c == '\n')
106 term_outbuf[term_outbuf_index++] = '\r';
107 term_outbuf[term_outbuf_index++] = c;
108 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
109 c == '\n')
110 term_flush();
114 void term_vprintf(const char *fmt, va_list ap)
116 char buf[4096];
117 vsnprintf(buf, sizeof(buf), fmt, ap);
118 term_puts(buf);
121 void term_printf(const char *fmt, ...)
123 va_list ap;
124 va_start(ap, fmt);
125 term_vprintf(fmt, ap);
126 va_end(ap);
129 void term_print_filename(const char *filename)
131 int i;
133 for (i = 0; filename[i]; i++) {
134 switch (filename[i]) {
135 case ' ':
136 case '"':
137 case '\\':
138 term_printf("\\%c", filename[i]);
139 break;
140 case '\t':
141 term_printf("\\t");
142 break;
143 case '\r':
144 term_printf("\\r");
145 break;
146 case '\n':
147 term_printf("\\n");
148 break;
149 default:
150 term_printf("%c", filename[i]);
151 break;
156 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
158 va_list ap;
159 va_start(ap, fmt);
160 term_vprintf(fmt, ap);
161 va_end(ap);
162 return 0;
165 static int compare_cmd(const char *name, const char *list)
167 const char *p, *pstart;
168 int len;
169 len = strlen(name);
170 p = list;
171 for(;;) {
172 pstart = p;
173 p = strchr(p, '|');
174 if (!p)
175 p = pstart + strlen(pstart);
176 if ((p - pstart) == len && !memcmp(pstart, name, len))
177 return 1;
178 if (*p == '\0')
179 break;
180 p++;
182 return 0;
185 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
187 const term_cmd_t *cmd;
189 for(cmd = cmds; cmd->name != NULL; cmd++) {
190 if (!name || !strcmp(name, cmd->name))
191 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
195 static void help_cmd(const char *name)
197 if (name && !strcmp(name, "info")) {
198 help_cmd1(info_cmds, "info ", NULL);
199 } else {
200 help_cmd1(term_cmds, "", name);
201 if (name && !strcmp(name, "log")) {
202 const CPULogItem *item;
203 term_printf("Log items (comma separated):\n");
204 term_printf("%-10s %s\n", "none", "remove all logs");
205 for(item = cpu_log_items; item->mask != 0; item++) {
206 term_printf("%-10s %s\n", item->name, item->help);
212 static void do_help(const char *name)
214 help_cmd(name);
217 static void do_commit(const char *device)
219 int i, all_devices;
221 all_devices = !strcmp(device, "all");
222 for (i = 0; i < nb_drives; i++) {
223 if (all_devices ||
224 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
225 bdrv_commit(drives_table[i].bdrv);
229 static void do_info(const char *item)
231 const term_cmd_t *cmd;
232 void (*handler)(void);
234 if (!item)
235 goto help;
236 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
237 if (compare_cmd(item, cmd->name))
238 goto found;
240 help:
241 help_cmd("info");
242 return;
243 found:
244 handler = cmd->handler;
245 handler();
248 static void do_info_version(void)
250 term_printf("%s\n", QEMU_VERSION);
253 static void do_info_name(void)
255 if (qemu_name)
256 term_printf("%s\n", qemu_name);
259 #if defined(TARGET_I386)
260 static void do_info_hpet(void)
262 term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
264 #endif
266 static void do_info_uuid(void)
268 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
269 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
270 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
271 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
272 qemu_uuid[15]);
275 static void do_info_block(void)
277 bdrv_info();
280 static void do_info_blockstats(void)
282 bdrv_info_stats();
285 /* get the current CPU defined by the user */
286 static int mon_set_cpu(int cpu_index)
288 CPUState *env;
290 for(env = first_cpu; env != NULL; env = env->next_cpu) {
291 if (env->cpu_index == cpu_index) {
292 mon_cpu = env;
293 return 0;
296 return -1;
299 static CPUState *mon_get_cpu(void)
301 if (!mon_cpu) {
302 mon_set_cpu(0);
305 kvm_save_registers(mon_cpu);
307 return mon_cpu;
310 static void do_info_registers(void)
312 CPUState *env;
313 env = mon_get_cpu();
314 if (!env)
315 return;
316 #ifdef TARGET_I386
317 cpu_dump_state(env, NULL, monitor_fprintf,
318 X86_DUMP_FPU);
319 #else
320 cpu_dump_state(env, NULL, monitor_fprintf,
322 #endif
325 static void do_info_cpus(void)
327 CPUState *env;
329 /* just to set the default cpu if not already done */
330 mon_get_cpu();
332 for(env = first_cpu; env != NULL; env = env->next_cpu) {
333 kvm_save_registers(env);
334 term_printf("%c CPU #%d:",
335 (env == mon_cpu) ? '*' : ' ',
336 env->cpu_index);
337 #if defined(TARGET_I386)
338 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
339 #elif defined(TARGET_PPC)
340 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
341 #elif defined(TARGET_SPARC)
342 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
343 #elif defined(TARGET_MIPS)
344 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
345 #endif
346 if (env->halted)
347 term_printf(" (halted)");
348 term_printf(" thread_id=%d", env->thread_id);
349 term_printf("\n");
353 static void do_cpu_set(int index)
355 if (mon_set_cpu(index) < 0)
356 term_printf("Invalid CPU index\n");
359 static void do_cpu_set_nr(int value, const char *status)
361 int state;
363 if (!strcmp(status, "online"))
364 state = 1;
365 else if (!strcmp(status, "offline"))
366 state = 0;
367 else {
368 term_printf("invalid status: %s\n", status);
369 return;
371 #if defined(TARGET_I386) || defined(TARGET_X86_64)
372 qemu_system_cpu_hot_add(value, state);
373 #endif
376 static void do_info_jit(void)
378 dump_exec_info(NULL, monitor_fprintf);
381 static void do_info_history (void)
383 int i;
384 const char *str;
386 i = 0;
387 for(;;) {
388 str = readline_get_history(i);
389 if (!str)
390 break;
391 term_printf("%d: '%s'\n", i, str);
392 i++;
396 #if defined(TARGET_PPC)
397 /* XXX: not implemented in other targets */
398 static void do_info_cpu_stats (void)
400 CPUState *env;
402 env = mon_get_cpu();
403 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
405 #endif
407 static void do_quit(void)
409 exit(0);
412 static int eject_device(BlockDriverState *bs, int force)
414 if (bdrv_is_inserted(bs)) {
415 if (!force) {
416 if (!bdrv_is_removable(bs)) {
417 term_printf("device is not removable\n");
418 return -1;
420 if (bdrv_is_locked(bs)) {
421 term_printf("device is locked\n");
422 return -1;
425 bdrv_close(bs);
427 return 0;
430 static void do_eject(int force, const char *filename)
432 BlockDriverState *bs;
434 bs = bdrv_find(filename);
435 if (!bs) {
436 term_printf("device not found\n");
437 return;
439 eject_device(bs, force);
442 static void do_change_block(const char *device, const char *filename, const char *fmt)
444 BlockDriverState *bs;
445 BlockDriver *drv = NULL;
447 bs = bdrv_find(device);
448 if (!bs) {
449 term_printf("device not found\n");
450 return;
452 if (fmt) {
453 drv = bdrv_find_format(fmt);
454 if (!drv) {
455 term_printf("invalid format %s\n", fmt);
456 return;
459 if (eject_device(bs, 0) < 0)
460 return;
461 bdrv_open2(bs, filename, 0, drv);
462 monitor_read_bdrv_key(bs);
465 static void do_change_vnc(const char *target, const char *arg)
467 if (strcmp(target, "passwd") == 0 ||
468 strcmp(target, "password") == 0) {
469 char password[9];
470 if (arg) {
471 strncpy(password, arg, sizeof(password));
472 password[sizeof(password) - 1] = '\0';
473 } else
474 monitor_readline("Password: ", 1, password, sizeof(password));
475 if (vnc_display_password(NULL, password) < 0)
476 term_printf("could not set VNC server password\n");
477 } else {
478 if (vnc_display_open(NULL, target) < 0)
479 term_printf("could not start VNC server on %s\n", target);
483 static void do_change(const char *device, const char *target, const char *arg)
485 if (strcmp(device, "vnc") == 0) {
486 do_change_vnc(target, arg);
487 } else {
488 do_change_block(device, target, arg);
492 static void do_screen_dump(const char *filename)
494 vga_hw_screen_dump(filename);
497 static void do_logfile(const char *filename)
499 cpu_set_log_filename(filename);
502 static void do_log(const char *items)
504 int mask;
506 if (!strcmp(items, "none")) {
507 mask = 0;
508 } else {
509 mask = cpu_str_to_log_mask(items);
510 if (!mask) {
511 help_cmd("log");
512 return;
515 cpu_set_log(mask);
518 static void do_stop(void)
520 vm_stop(EXCP_INTERRUPT);
523 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
525 int *err = opaque;
527 if (bdrv_key_required(bs))
528 *err = monitor_read_bdrv_key(bs);
529 else
530 *err = 0;
533 static void do_cont(void)
535 int err = 0;
537 bdrv_iterate(encrypted_bdrv_it, &err);
538 /* only resume the vm if all keys are set and valid */
539 if (!err)
540 vm_start();
543 #ifdef CONFIG_GDBSTUB
544 static void do_gdbserver(const char *port)
546 if (!port)
547 port = DEFAULT_GDBSTUB_PORT;
548 if (gdbserver_start(port) < 0) {
549 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
550 } else {
551 qemu_printf("Waiting gdb connection on port '%s'\n", port);
554 #endif
556 static void term_printc(int c)
558 term_printf("'");
559 switch(c) {
560 case '\'':
561 term_printf("\\'");
562 break;
563 case '\\':
564 term_printf("\\\\");
565 break;
566 case '\n':
567 term_printf("\\n");
568 break;
569 case '\r':
570 term_printf("\\r");
571 break;
572 default:
573 if (c >= 32 && c <= 126) {
574 term_printf("%c", c);
575 } else {
576 term_printf("\\x%02x", c);
578 break;
580 term_printf("'");
583 static void memory_dump(int count, int format, int wsize,
584 target_phys_addr_t addr, int is_physical)
586 CPUState *env;
587 int nb_per_line, l, line_size, i, max_digits, len;
588 uint8_t buf[16];
589 uint64_t v;
591 if (format == 'i') {
592 int flags;
593 flags = 0;
594 env = mon_get_cpu();
595 if (!env && !is_physical)
596 return;
597 #ifdef TARGET_I386
598 if (wsize == 2) {
599 flags = 1;
600 } else if (wsize == 4) {
601 flags = 0;
602 } else {
603 /* as default we use the current CS size */
604 flags = 0;
605 if (env) {
606 #ifdef TARGET_X86_64
607 if ((env->efer & MSR_EFER_LMA) &&
608 (env->segs[R_CS].flags & DESC_L_MASK))
609 flags = 2;
610 else
611 #endif
612 if (!(env->segs[R_CS].flags & DESC_B_MASK))
613 flags = 1;
616 #endif
617 monitor_disas(env, addr, count, is_physical, flags);
618 return;
621 len = wsize * count;
622 if (wsize == 1)
623 line_size = 8;
624 else
625 line_size = 16;
626 nb_per_line = line_size / wsize;
627 max_digits = 0;
629 switch(format) {
630 case 'o':
631 max_digits = (wsize * 8 + 2) / 3;
632 break;
633 default:
634 case 'x':
635 max_digits = (wsize * 8) / 4;
636 break;
637 case 'u':
638 case 'd':
639 max_digits = (wsize * 8 * 10 + 32) / 33;
640 break;
641 case 'c':
642 wsize = 1;
643 break;
646 while (len > 0) {
647 if (is_physical)
648 term_printf(TARGET_FMT_plx ":", addr);
649 else
650 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
651 l = len;
652 if (l > line_size)
653 l = line_size;
654 if (is_physical) {
655 cpu_physical_memory_rw(addr, buf, l, 0);
656 } else {
657 env = mon_get_cpu();
658 if (!env)
659 break;
660 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
661 term_printf(" Cannot access memory\n");
662 break;
665 i = 0;
666 while (i < l) {
667 switch(wsize) {
668 default:
669 case 1:
670 v = ldub_raw(buf + i);
671 break;
672 case 2:
673 v = lduw_raw(buf + i);
674 break;
675 case 4:
676 v = (uint32_t)ldl_raw(buf + i);
677 break;
678 case 8:
679 v = ldq_raw(buf + i);
680 break;
682 term_printf(" ");
683 switch(format) {
684 case 'o':
685 term_printf("%#*" PRIo64, max_digits, v);
686 break;
687 case 'x':
688 term_printf("0x%0*" PRIx64, max_digits, v);
689 break;
690 case 'u':
691 term_printf("%*" PRIu64, max_digits, v);
692 break;
693 case 'd':
694 term_printf("%*" PRId64, max_digits, v);
695 break;
696 case 'c':
697 term_printc(v);
698 break;
700 i += wsize;
702 term_printf("\n");
703 addr += l;
704 len -= l;
708 #if TARGET_LONG_BITS == 64
709 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
710 #else
711 #define GET_TLONG(h, l) (l)
712 #endif
714 static void do_memory_dump(int count, int format, int size,
715 uint32_t addrh, uint32_t addrl)
717 target_long addr = GET_TLONG(addrh, addrl);
718 memory_dump(count, format, size, addr, 0);
721 #if TARGET_PHYS_ADDR_BITS > 32
722 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
723 #else
724 #define GET_TPHYSADDR(h, l) (l)
725 #endif
727 static void do_physical_memory_dump(int count, int format, int size,
728 uint32_t addrh, uint32_t addrl)
731 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
732 memory_dump(count, format, size, addr, 1);
735 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
737 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
738 #if TARGET_PHYS_ADDR_BITS == 32
739 switch(format) {
740 case 'o':
741 term_printf("%#o", val);
742 break;
743 case 'x':
744 term_printf("%#x", val);
745 break;
746 case 'u':
747 term_printf("%u", val);
748 break;
749 default:
750 case 'd':
751 term_printf("%d", val);
752 break;
753 case 'c':
754 term_printc(val);
755 break;
757 #else
758 switch(format) {
759 case 'o':
760 term_printf("%#" PRIo64, val);
761 break;
762 case 'x':
763 term_printf("%#" PRIx64, val);
764 break;
765 case 'u':
766 term_printf("%" PRIu64, val);
767 break;
768 default:
769 case 'd':
770 term_printf("%" PRId64, val);
771 break;
772 case 'c':
773 term_printc(val);
774 break;
776 #endif
777 term_printf("\n");
780 static void do_memory_save(unsigned int valh, unsigned int vall,
781 uint32_t size, const char *filename)
783 FILE *f;
784 target_long addr = GET_TLONG(valh, vall);
785 uint32_t l;
786 CPUState *env;
787 uint8_t buf[1024];
789 env = mon_get_cpu();
790 if (!env)
791 return;
793 f = fopen(filename, "wb");
794 if (!f) {
795 term_printf("could not open '%s'\n", filename);
796 return;
798 while (size != 0) {
799 l = sizeof(buf);
800 if (l > size)
801 l = size;
802 cpu_memory_rw_debug(env, addr, buf, l, 0);
803 fwrite(buf, 1, l, f);
804 addr += l;
805 size -= l;
807 fclose(f);
810 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
811 uint32_t size, const char *filename)
813 FILE *f;
814 uint32_t l;
815 uint8_t buf[1024];
816 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
818 f = fopen(filename, "wb");
819 if (!f) {
820 term_printf("could not open '%s'\n", filename);
821 return;
823 while (size != 0) {
824 l = sizeof(buf);
825 if (l > size)
826 l = size;
827 cpu_physical_memory_rw(addr, buf, l, 0);
828 fwrite(buf, 1, l, f);
829 fflush(f);
830 addr += l;
831 size -= l;
833 fclose(f);
836 static void do_sum(uint32_t start, uint32_t size)
838 uint32_t addr;
839 uint8_t buf[1];
840 uint16_t sum;
842 sum = 0;
843 for(addr = start; addr < (start + size); addr++) {
844 cpu_physical_memory_rw(addr, buf, 1, 0);
845 /* BSD sum algorithm ('sum' Unix command) */
846 sum = (sum >> 1) | (sum << 15);
847 sum += buf[0];
849 term_printf("%05d\n", sum);
852 typedef struct {
853 int keycode;
854 const char *name;
855 } KeyDef;
857 static const KeyDef key_defs[] = {
858 { 0x2a, "shift" },
859 { 0x36, "shift_r" },
861 { 0x38, "alt" },
862 { 0xb8, "alt_r" },
863 { 0x64, "altgr" },
864 { 0xe4, "altgr_r" },
865 { 0x1d, "ctrl" },
866 { 0x9d, "ctrl_r" },
868 { 0xdd, "menu" },
870 { 0x01, "esc" },
872 { 0x02, "1" },
873 { 0x03, "2" },
874 { 0x04, "3" },
875 { 0x05, "4" },
876 { 0x06, "5" },
877 { 0x07, "6" },
878 { 0x08, "7" },
879 { 0x09, "8" },
880 { 0x0a, "9" },
881 { 0x0b, "0" },
882 { 0x0c, "minus" },
883 { 0x0d, "equal" },
884 { 0x0e, "backspace" },
886 { 0x0f, "tab" },
887 { 0x10, "q" },
888 { 0x11, "w" },
889 { 0x12, "e" },
890 { 0x13, "r" },
891 { 0x14, "t" },
892 { 0x15, "y" },
893 { 0x16, "u" },
894 { 0x17, "i" },
895 { 0x18, "o" },
896 { 0x19, "p" },
898 { 0x1c, "ret" },
900 { 0x1e, "a" },
901 { 0x1f, "s" },
902 { 0x20, "d" },
903 { 0x21, "f" },
904 { 0x22, "g" },
905 { 0x23, "h" },
906 { 0x24, "j" },
907 { 0x25, "k" },
908 { 0x26, "l" },
910 { 0x2c, "z" },
911 { 0x2d, "x" },
912 { 0x2e, "c" },
913 { 0x2f, "v" },
914 { 0x30, "b" },
915 { 0x31, "n" },
916 { 0x32, "m" },
917 { 0x33, "comma" },
918 { 0x34, "dot" },
919 { 0x35, "slash" },
921 { 0x37, "asterisk" },
923 { 0x39, "spc" },
924 { 0x3a, "caps_lock" },
925 { 0x3b, "f1" },
926 { 0x3c, "f2" },
927 { 0x3d, "f3" },
928 { 0x3e, "f4" },
929 { 0x3f, "f5" },
930 { 0x40, "f6" },
931 { 0x41, "f7" },
932 { 0x42, "f8" },
933 { 0x43, "f9" },
934 { 0x44, "f10" },
935 { 0x45, "num_lock" },
936 { 0x46, "scroll_lock" },
938 { 0xb5, "kp_divide" },
939 { 0x37, "kp_multiply" },
940 { 0x4a, "kp_subtract" },
941 { 0x4e, "kp_add" },
942 { 0x9c, "kp_enter" },
943 { 0x53, "kp_decimal" },
944 { 0x54, "sysrq" },
946 { 0x52, "kp_0" },
947 { 0x4f, "kp_1" },
948 { 0x50, "kp_2" },
949 { 0x51, "kp_3" },
950 { 0x4b, "kp_4" },
951 { 0x4c, "kp_5" },
952 { 0x4d, "kp_6" },
953 { 0x47, "kp_7" },
954 { 0x48, "kp_8" },
955 { 0x49, "kp_9" },
957 { 0x56, "<" },
959 { 0x57, "f11" },
960 { 0x58, "f12" },
962 { 0xb7, "print" },
964 { 0xc7, "home" },
965 { 0xc9, "pgup" },
966 { 0xd1, "pgdn" },
967 { 0xcf, "end" },
969 { 0xcb, "left" },
970 { 0xc8, "up" },
971 { 0xd0, "down" },
972 { 0xcd, "right" },
974 { 0xd2, "insert" },
975 { 0xd3, "delete" },
976 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
977 { 0xf0, "stop" },
978 { 0xf1, "again" },
979 { 0xf2, "props" },
980 { 0xf3, "undo" },
981 { 0xf4, "front" },
982 { 0xf5, "copy" },
983 { 0xf6, "open" },
984 { 0xf7, "paste" },
985 { 0xf8, "find" },
986 { 0xf9, "cut" },
987 { 0xfa, "lf" },
988 { 0xfb, "help" },
989 { 0xfc, "meta_l" },
990 { 0xfd, "meta_r" },
991 { 0xfe, "compose" },
992 #endif
993 { 0, NULL },
996 static int get_keycode(const char *key)
998 const KeyDef *p;
999 char *endp;
1000 int ret;
1002 for(p = key_defs; p->name != NULL; p++) {
1003 if (!strcmp(key, p->name))
1004 return p->keycode;
1006 if (strstart(key, "0x", NULL)) {
1007 ret = strtoul(key, &endp, 0);
1008 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1009 return ret;
1011 return -1;
1014 #define MAX_KEYCODES 16
1015 static uint8_t keycodes[MAX_KEYCODES];
1016 static int nb_pending_keycodes;
1017 static QEMUTimer *key_timer;
1019 static void release_keys(void *opaque)
1021 int keycode;
1023 while (nb_pending_keycodes > 0) {
1024 nb_pending_keycodes--;
1025 keycode = keycodes[nb_pending_keycodes];
1026 if (keycode & 0x80)
1027 kbd_put_keycode(0xe0);
1028 kbd_put_keycode(keycode | 0x80);
1032 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1034 char keyname_buf[16];
1035 char *separator;
1036 int keyname_len, keycode, i;
1038 if (nb_pending_keycodes > 0) {
1039 qemu_del_timer(key_timer);
1040 release_keys(NULL);
1042 if (!has_hold_time)
1043 hold_time = 100;
1044 i = 0;
1045 while (1) {
1046 separator = strchr(string, '-');
1047 keyname_len = separator ? separator - string : strlen(string);
1048 if (keyname_len > 0) {
1049 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1050 if (keyname_len > sizeof(keyname_buf) - 1) {
1051 term_printf("invalid key: '%s...'\n", keyname_buf);
1052 return;
1054 if (i == MAX_KEYCODES) {
1055 term_printf("too many keys\n");
1056 return;
1058 keyname_buf[keyname_len] = 0;
1059 keycode = get_keycode(keyname_buf);
1060 if (keycode < 0) {
1061 term_printf("unknown key: '%s'\n", keyname_buf);
1062 return;
1064 keycodes[i++] = keycode;
1066 if (!separator)
1067 break;
1068 string = separator + 1;
1070 nb_pending_keycodes = i;
1071 /* key down events */
1072 for (i = 0; i < nb_pending_keycodes; i++) {
1073 keycode = keycodes[i];
1074 if (keycode & 0x80)
1075 kbd_put_keycode(0xe0);
1076 kbd_put_keycode(keycode & 0x7f);
1078 /* delayed key up events */
1079 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1080 muldiv64(ticks_per_sec, hold_time, 1000));
1083 static int mouse_button_state;
1085 static void do_mouse_move(const char *dx_str, const char *dy_str,
1086 const char *dz_str)
1088 int dx, dy, dz;
1089 dx = strtol(dx_str, NULL, 0);
1090 dy = strtol(dy_str, NULL, 0);
1091 dz = 0;
1092 if (dz_str)
1093 dz = strtol(dz_str, NULL, 0);
1094 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1097 static void do_mouse_button(int button_state)
1099 mouse_button_state = button_state;
1100 kbd_mouse_event(0, 0, 0, mouse_button_state);
1103 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1105 uint32_t val;
1106 int suffix;
1108 if (has_index) {
1109 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1110 addr++;
1112 addr &= 0xffff;
1114 switch(size) {
1115 default:
1116 case 1:
1117 val = cpu_inb(NULL, addr);
1118 suffix = 'b';
1119 break;
1120 case 2:
1121 val = cpu_inw(NULL, addr);
1122 suffix = 'w';
1123 break;
1124 case 4:
1125 val = cpu_inl(NULL, addr);
1126 suffix = 'l';
1127 break;
1129 term_printf("port%c[0x%04x] = %#0*x\n",
1130 suffix, addr, size * 2, val);
1133 /* boot_set handler */
1134 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1135 static void *boot_opaque;
1137 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1139 qemu_boot_set_handler = func;
1140 boot_opaque = opaque;
1143 static void do_boot_set(const char *bootdevice)
1145 int res;
1147 if (qemu_boot_set_handler) {
1148 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1149 if (res == 0)
1150 term_printf("boot device list now set to %s\n", bootdevice);
1151 else
1152 term_printf("setting boot device list failed with error %i\n", res);
1153 } else {
1154 term_printf("no function defined to set boot device list for this architecture\n");
1158 static void do_system_reset(void)
1160 qemu_system_reset_request();
1163 static void do_system_powerdown(void)
1165 qemu_system_powerdown_request();
1168 #if defined(TARGET_I386)
1169 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1171 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1172 addr,
1173 pte & mask,
1174 pte & PG_GLOBAL_MASK ? 'G' : '-',
1175 pte & PG_PSE_MASK ? 'P' : '-',
1176 pte & PG_DIRTY_MASK ? 'D' : '-',
1177 pte & PG_ACCESSED_MASK ? 'A' : '-',
1178 pte & PG_PCD_MASK ? 'C' : '-',
1179 pte & PG_PWT_MASK ? 'T' : '-',
1180 pte & PG_USER_MASK ? 'U' : '-',
1181 pte & PG_RW_MASK ? 'W' : '-');
1184 static void tlb_info(void)
1186 CPUState *env;
1187 int l1, l2;
1188 uint32_t pgd, pde, pte;
1190 env = mon_get_cpu();
1191 if (!env)
1192 return;
1194 if (!(env->cr[0] & CR0_PG_MASK)) {
1195 term_printf("PG disabled\n");
1196 return;
1198 pgd = env->cr[3] & ~0xfff;
1199 for(l1 = 0; l1 < 1024; l1++) {
1200 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1201 pde = le32_to_cpu(pde);
1202 if (pde & PG_PRESENT_MASK) {
1203 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1204 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1205 } else {
1206 for(l2 = 0; l2 < 1024; l2++) {
1207 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1208 (uint8_t *)&pte, 4);
1209 pte = le32_to_cpu(pte);
1210 if (pte & PG_PRESENT_MASK) {
1211 print_pte((l1 << 22) + (l2 << 12),
1212 pte & ~PG_PSE_MASK,
1213 ~0xfff);
1221 static void mem_print(uint32_t *pstart, int *plast_prot,
1222 uint32_t end, int prot)
1224 int prot1;
1225 prot1 = *plast_prot;
1226 if (prot != prot1) {
1227 if (*pstart != -1) {
1228 term_printf("%08x-%08x %08x %c%c%c\n",
1229 *pstart, end, end - *pstart,
1230 prot1 & PG_USER_MASK ? 'u' : '-',
1231 'r',
1232 prot1 & PG_RW_MASK ? 'w' : '-');
1234 if (prot != 0)
1235 *pstart = end;
1236 else
1237 *pstart = -1;
1238 *plast_prot = prot;
1242 static void mem_info(void)
1244 CPUState *env;
1245 int l1, l2, prot, last_prot;
1246 uint32_t pgd, pde, pte, start, end;
1248 env = mon_get_cpu();
1249 if (!env)
1250 return;
1252 if (!(env->cr[0] & CR0_PG_MASK)) {
1253 term_printf("PG disabled\n");
1254 return;
1256 pgd = env->cr[3] & ~0xfff;
1257 last_prot = 0;
1258 start = -1;
1259 for(l1 = 0; l1 < 1024; l1++) {
1260 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1261 pde = le32_to_cpu(pde);
1262 end = l1 << 22;
1263 if (pde & PG_PRESENT_MASK) {
1264 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1265 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1266 mem_print(&start, &last_prot, end, prot);
1267 } else {
1268 for(l2 = 0; l2 < 1024; l2++) {
1269 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1270 (uint8_t *)&pte, 4);
1271 pte = le32_to_cpu(pte);
1272 end = (l1 << 22) + (l2 << 12);
1273 if (pte & PG_PRESENT_MASK) {
1274 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1275 } else {
1276 prot = 0;
1278 mem_print(&start, &last_prot, end, prot);
1281 } else {
1282 prot = 0;
1283 mem_print(&start, &last_prot, end, prot);
1287 #endif
1289 #if defined(TARGET_SH4)
1291 static void print_tlb(int idx, tlb_t *tlb)
1293 term_printf(" tlb%i:\t"
1294 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1295 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1296 "dirty=%hhu writethrough=%hhu\n",
1297 idx,
1298 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1299 tlb->v, tlb->sh, tlb->c, tlb->pr,
1300 tlb->d, tlb->wt);
1303 static void tlb_info(void)
1305 CPUState *env = mon_get_cpu();
1306 int i;
1308 term_printf ("ITLB:\n");
1309 for (i = 0 ; i < ITLB_SIZE ; i++)
1310 print_tlb (i, &env->itlb[i]);
1311 term_printf ("UTLB:\n");
1312 for (i = 0 ; i < UTLB_SIZE ; i++)
1313 print_tlb (i, &env->utlb[i]);
1316 #endif
1318 static void do_info_kqemu(void)
1320 #ifdef USE_KQEMU
1321 CPUState *env;
1322 int val;
1323 val = 0;
1324 env = mon_get_cpu();
1325 if (!env) {
1326 term_printf("No cpu initialized yet");
1327 return;
1329 val = env->kqemu_enabled;
1330 term_printf("kqemu support: ");
1331 switch(val) {
1332 default:
1333 case 0:
1334 term_printf("disabled\n");
1335 break;
1336 case 1:
1337 term_printf("enabled for user code\n");
1338 break;
1339 case 2:
1340 term_printf("enabled for user and kernel code\n");
1341 break;
1343 #else
1344 term_printf("kqemu support: not compiled\n");
1345 #endif
1348 static void do_info_kvm(void)
1350 #if defined(USE_KVM) || defined(CONFIG_KVM)
1351 term_printf("kvm support: ");
1352 if (kvm_enabled())
1353 term_printf("enabled\n");
1354 else
1355 term_printf("disabled\n");
1356 #else
1357 term_printf("kvm support: not compiled\n");
1358 #endif
1361 #ifdef CONFIG_PROFILER
1363 int64_t kqemu_time;
1364 int64_t qemu_time;
1365 int64_t kqemu_exec_count;
1366 int64_t dev_time;
1367 int64_t kqemu_ret_int_count;
1368 int64_t kqemu_ret_excp_count;
1369 int64_t kqemu_ret_intr_count;
1371 static void do_info_profile(void)
1373 int64_t total;
1374 total = qemu_time;
1375 if (total == 0)
1376 total = 1;
1377 term_printf("async time %" PRId64 " (%0.3f)\n",
1378 dev_time, dev_time / (double)ticks_per_sec);
1379 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1380 qemu_time, qemu_time / (double)ticks_per_sec);
1381 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1382 kqemu_time, kqemu_time / (double)ticks_per_sec,
1383 kqemu_time / (double)total * 100.0,
1384 kqemu_exec_count,
1385 kqemu_ret_int_count,
1386 kqemu_ret_excp_count,
1387 kqemu_ret_intr_count);
1388 qemu_time = 0;
1389 kqemu_time = 0;
1390 kqemu_exec_count = 0;
1391 dev_time = 0;
1392 kqemu_ret_int_count = 0;
1393 kqemu_ret_excp_count = 0;
1394 kqemu_ret_intr_count = 0;
1395 #ifdef USE_KQEMU
1396 kqemu_record_dump();
1397 #endif
1399 #else
1400 static void do_info_profile(void)
1402 term_printf("Internal profiler not compiled\n");
1404 #endif
1406 /* Capture support */
1407 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1409 static void do_info_capture (void)
1411 int i;
1412 CaptureState *s;
1414 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1415 term_printf ("[%d]: ", i);
1416 s->ops.info (s->opaque);
1420 static void do_stop_capture (int n)
1422 int i;
1423 CaptureState *s;
1425 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1426 if (i == n) {
1427 s->ops.destroy (s->opaque);
1428 LIST_REMOVE (s, entries);
1429 qemu_free (s);
1430 return;
1435 #ifdef HAS_AUDIO
1436 static void do_wav_capture (const char *path,
1437 int has_freq, int freq,
1438 int has_bits, int bits,
1439 int has_channels, int nchannels)
1441 CaptureState *s;
1443 s = qemu_mallocz (sizeof (*s));
1445 freq = has_freq ? freq : 44100;
1446 bits = has_bits ? bits : 16;
1447 nchannels = has_channels ? nchannels : 2;
1449 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1450 term_printf ("Faied to add wave capture\n");
1451 qemu_free (s);
1453 LIST_INSERT_HEAD (&capture_head, s, entries);
1455 #endif
1457 #if defined(TARGET_I386)
1458 static void do_inject_nmi(int cpu_index)
1460 CPUState *env;
1462 for (env = first_cpu; env != NULL; env = env->next_cpu)
1463 if (env->cpu_index == cpu_index) {
1464 if (kvm_enabled())
1465 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1466 else
1467 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1468 break;
1471 #endif
1473 static void do_info_status(void)
1475 if (vm_running)
1476 term_printf("VM status: running\n");
1477 else
1478 term_printf("VM status: paused\n");
1482 static void do_balloon(int value)
1484 ram_addr_t target = value;
1485 qemu_balloon(target << 20);
1488 static void do_info_balloon(void)
1490 ram_addr_t actual;
1492 actual = qemu_balloon_status();
1493 if (kvm_enabled() && !kvm_has_sync_mmu())
1494 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1495 else if (actual == 0)
1496 term_printf("Ballooning not activated in VM\n");
1497 else
1498 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1501 /* Please update qemu-doc.texi when adding or changing commands */
1502 static const term_cmd_t term_cmds[] = {
1503 { "help|?", "s?", do_help,
1504 "[cmd]", "show the help" },
1505 { "commit", "s", do_commit,
1506 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1507 { "info", "s?", do_info,
1508 "subcommand", "show various information about the system state" },
1509 { "q|quit", "", do_quit,
1510 "", "quit the emulator" },
1511 { "eject", "-fB", do_eject,
1512 "[-f] device", "eject a removable medium (use -f to force it)" },
1513 { "change", "BFs?", do_change,
1514 "device filename [format]", "change a removable medium, optional format" },
1515 { "screendump", "F", do_screen_dump,
1516 "filename", "save screen into PPM image 'filename'" },
1517 { "logfile", "F", do_logfile,
1518 "filename", "output logs to 'filename'" },
1519 { "log", "s", do_log,
1520 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1521 { "savevm", "s?", do_savevm,
1522 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1523 { "loadvm", "s", do_loadvm,
1524 "tag|id", "restore a VM snapshot from its tag or id" },
1525 { "delvm", "s", do_delvm,
1526 "tag|id", "delete a VM snapshot from its tag or id" },
1527 { "stop", "", do_stop,
1528 "", "stop emulation", },
1529 { "c|cont", "", do_cont,
1530 "", "resume emulation", },
1531 #ifdef CONFIG_GDBSTUB
1532 { "gdbserver", "s?", do_gdbserver,
1533 "[port]", "start gdbserver session (default port=1234)", },
1534 #endif
1535 { "x", "/l", do_memory_dump,
1536 "/fmt addr", "virtual memory dump starting at 'addr'", },
1537 { "xp", "/l", do_physical_memory_dump,
1538 "/fmt addr", "physical memory dump starting at 'addr'", },
1539 { "p|print", "/l", do_print,
1540 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1541 { "i", "/ii.", do_ioport_read,
1542 "/fmt addr", "I/O port read" },
1544 { "sendkey", "si?", do_sendkey,
1545 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1546 { "system_reset", "", do_system_reset,
1547 "", "reset the system" },
1548 { "system_powerdown", "", do_system_powerdown,
1549 "", "send system power down event" },
1550 { "sum", "ii", do_sum,
1551 "addr size", "compute the checksum of a memory region" },
1552 { "usb_add", "s", do_usb_add,
1553 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1554 { "usb_del", "s", do_usb_del,
1555 "device", "remove USB device 'bus.addr'" },
1556 { "cpu", "i", do_cpu_set,
1557 "index", "set the default CPU" },
1558 { "mouse_move", "sss?", do_mouse_move,
1559 "dx dy [dz]", "send mouse move events" },
1560 { "mouse_button", "i", do_mouse_button,
1561 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1562 { "mouse_set", "i", do_mouse_set,
1563 "index", "set which mouse device receives events" },
1564 #ifdef HAS_AUDIO
1565 { "wavcapture", "si?i?i?", do_wav_capture,
1566 "path [frequency bits channels]",
1567 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1568 #endif
1569 { "stopcapture", "i", do_stop_capture,
1570 "capture index", "stop capture" },
1571 { "memsave", "lis", do_memory_save,
1572 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1573 { "pmemsave", "lis", do_physical_memory_save,
1574 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1575 { "boot_set", "s", do_boot_set,
1576 "bootdevice", "define new values for the boot device list" },
1577 #if defined(TARGET_I386)
1578 { "nmi", "i", do_inject_nmi,
1579 "cpu", "inject an NMI on the given CPU", },
1580 #endif
1581 { "migrate", "-ds", do_migrate,
1582 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1583 { "migrate_cancel", "", do_migrate_cancel,
1584 "", "cancel the current VM migration" },
1585 { "migrate_set_speed", "s", do_migrate_set_speed,
1586 "value", "set maximum speed (in bytes) for migrations" },
1587 #if defined(TARGET_I386)
1588 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1589 "[file=file][,if=type][,bus=n]\n"
1590 "[,unit=m][,media=d][index=i]\n"
1591 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1592 "[snapshot=on|off][,cache=on|off]",
1593 "add drive to PCI storage controller" },
1594 { "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" },
1595 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1596 { "host_net_add", "ss", net_host_device_add,
1597 "[tap,user,socket,vde] options", "add host VLAN client" },
1598 { "host_net_remove", "is", net_host_device_remove,
1599 "vlan_id name", "remove host VLAN client" },
1600 #endif
1601 { "balloon", "i", do_balloon,
1602 "target", "request VM to change it's memory allocation (in MB)" },
1603 { "set_link", "ss", do_set_link,
1604 "name [up|down]", "change the link status of a network adapter" },
1605 { "set_link", "ss", do_set_link, "name [up|down]" },
1606 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1607 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1608 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1609 "[,unit=m][,media=d][index=i]\n"
1610 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1611 "[snapshot=on|off][,cache=on|off]",
1612 "add drive to PCI storage controller" },
1613 #endif
1614 { NULL, NULL, },
1617 /* Please update qemu-doc.texi when adding or changing commands */
1618 static const term_cmd_t info_cmds[] = {
1619 { "version", "", do_info_version,
1620 "", "show the version of QEMU" },
1621 { "network", "", do_info_network,
1622 "", "show the network state" },
1623 { "chardev", "", qemu_chr_info,
1624 "", "show the character devices" },
1625 { "block", "", do_info_block,
1626 "", "show the block devices" },
1627 { "blockstats", "", do_info_blockstats,
1628 "", "show block device statistics" },
1629 { "registers", "", do_info_registers,
1630 "", "show the cpu registers" },
1631 { "cpus", "", do_info_cpus,
1632 "", "show infos for each CPU" },
1633 { "history", "", do_info_history,
1634 "", "show the command line history", },
1635 { "irq", "", irq_info,
1636 "", "show the interrupts statistics (if available)", },
1637 { "pic", "", pic_info,
1638 "", "show i8259 (PIC) state", },
1639 { "pci", "", pci_info,
1640 "", "show PCI info", },
1641 #if defined(TARGET_I386) || defined(TARGET_SH4)
1642 { "tlb", "", tlb_info,
1643 "", "show virtual to physical memory mappings", },
1644 #endif
1645 #if defined(TARGET_I386)
1646 { "mem", "", mem_info,
1647 "", "show the active virtual memory mappings", },
1648 { "hpet", "", do_info_hpet,
1649 "", "show state of HPET", },
1650 #endif
1651 { "jit", "", do_info_jit,
1652 "", "show dynamic compiler info", },
1653 { "kqemu", "", do_info_kqemu,
1654 "", "show KQEMU information", },
1655 { "kvm", "", do_info_kvm,
1656 "", "show KVM information", },
1657 { "usb", "", usb_info,
1658 "", "show guest USB devices", },
1659 { "usbhost", "", usb_host_info,
1660 "", "show host USB devices", },
1661 { "profile", "", do_info_profile,
1662 "", "show profiling information", },
1663 { "capture", "", do_info_capture,
1664 "", "show capture information" },
1665 { "snapshots", "", do_info_snapshots,
1666 "", "show the currently saved VM snapshots" },
1667 { "status", "", do_info_status,
1668 "", "show the current VM status (running|paused)" },
1669 { "pcmcia", "", pcmcia_info,
1670 "", "show guest PCMCIA status" },
1671 { "mice", "", do_info_mice,
1672 "", "show which guest mouse is receiving events" },
1673 { "vnc", "", do_info_vnc,
1674 "", "show the vnc server status"},
1675 { "name", "", do_info_name,
1676 "", "show the current VM name" },
1677 { "uuid", "", do_info_uuid,
1678 "", "show the current VM UUID" },
1679 #if defined(TARGET_PPC)
1680 { "cpustats", "", do_info_cpu_stats,
1681 "", "show CPU statistics", },
1682 #endif
1683 #if defined(CONFIG_SLIRP)
1684 { "slirp", "", do_info_slirp,
1685 "", "show SLIRP statistics", },
1686 #endif
1687 { "migrate", "", do_info_migrate, "", "show migration status" },
1688 { "balloon", "", do_info_balloon,
1689 "", "show balloon information" },
1690 { NULL, NULL, },
1693 /*******************************************************************/
1695 static const char *pch;
1696 static jmp_buf expr_env;
1698 #define MD_TLONG 0
1699 #define MD_I32 1
1701 typedef struct MonitorDef {
1702 const char *name;
1703 int offset;
1704 target_long (*get_value)(const struct MonitorDef *md, int val);
1705 int type;
1706 } MonitorDef;
1708 #if defined(TARGET_I386)
1709 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1711 CPUState *env = mon_get_cpu();
1712 if (!env)
1713 return 0;
1714 return env->eip + env->segs[R_CS].base;
1716 #endif
1718 #if defined(TARGET_PPC)
1719 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1721 CPUState *env = mon_get_cpu();
1722 unsigned int u;
1723 int i;
1725 if (!env)
1726 return 0;
1728 u = 0;
1729 for (i = 0; i < 8; i++)
1730 u |= env->crf[i] << (32 - (4 * i));
1732 return u;
1735 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1737 CPUState *env = mon_get_cpu();
1738 if (!env)
1739 return 0;
1740 return env->msr;
1743 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1745 CPUState *env = mon_get_cpu();
1746 if (!env)
1747 return 0;
1748 return env->xer;
1751 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1753 CPUState *env = mon_get_cpu();
1754 if (!env)
1755 return 0;
1756 return cpu_ppc_load_decr(env);
1759 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1761 CPUState *env = mon_get_cpu();
1762 if (!env)
1763 return 0;
1764 return cpu_ppc_load_tbu(env);
1767 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1769 CPUState *env = mon_get_cpu();
1770 if (!env)
1771 return 0;
1772 return cpu_ppc_load_tbl(env);
1774 #endif
1776 #if defined(TARGET_SPARC)
1777 #ifndef TARGET_SPARC64
1778 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1780 CPUState *env = mon_get_cpu();
1781 if (!env)
1782 return 0;
1783 return GET_PSR(env);
1785 #endif
1787 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1789 CPUState *env = mon_get_cpu();
1790 if (!env)
1791 return 0;
1792 return env->regwptr[val];
1794 #endif
1796 static const MonitorDef monitor_defs[] = {
1797 #ifdef TARGET_I386
1799 #define SEG(name, seg) \
1800 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1801 { name ".base", offsetof(CPUState, segs[seg].base) },\
1802 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1804 { "eax", offsetof(CPUState, regs[0]) },
1805 { "ecx", offsetof(CPUState, regs[1]) },
1806 { "edx", offsetof(CPUState, regs[2]) },
1807 { "ebx", offsetof(CPUState, regs[3]) },
1808 { "esp|sp", offsetof(CPUState, regs[4]) },
1809 { "ebp|fp", offsetof(CPUState, regs[5]) },
1810 { "esi", offsetof(CPUState, regs[6]) },
1811 { "edi", offsetof(CPUState, regs[7]) },
1812 #ifdef TARGET_X86_64
1813 { "r8", offsetof(CPUState, regs[8]) },
1814 { "r9", offsetof(CPUState, regs[9]) },
1815 { "r10", offsetof(CPUState, regs[10]) },
1816 { "r11", offsetof(CPUState, regs[11]) },
1817 { "r12", offsetof(CPUState, regs[12]) },
1818 { "r13", offsetof(CPUState, regs[13]) },
1819 { "r14", offsetof(CPUState, regs[14]) },
1820 { "r15", offsetof(CPUState, regs[15]) },
1821 #endif
1822 { "eflags", offsetof(CPUState, eflags) },
1823 { "eip", offsetof(CPUState, eip) },
1824 SEG("cs", R_CS)
1825 SEG("ds", R_DS)
1826 SEG("es", R_ES)
1827 SEG("ss", R_SS)
1828 SEG("fs", R_FS)
1829 SEG("gs", R_GS)
1830 { "pc", 0, monitor_get_pc, },
1831 #elif defined(TARGET_PPC)
1832 /* General purpose registers */
1833 { "r0", offsetof(CPUState, gpr[0]) },
1834 { "r1", offsetof(CPUState, gpr[1]) },
1835 { "r2", offsetof(CPUState, gpr[2]) },
1836 { "r3", offsetof(CPUState, gpr[3]) },
1837 { "r4", offsetof(CPUState, gpr[4]) },
1838 { "r5", offsetof(CPUState, gpr[5]) },
1839 { "r6", offsetof(CPUState, gpr[6]) },
1840 { "r7", offsetof(CPUState, gpr[7]) },
1841 { "r8", offsetof(CPUState, gpr[8]) },
1842 { "r9", offsetof(CPUState, gpr[9]) },
1843 { "r10", offsetof(CPUState, gpr[10]) },
1844 { "r11", offsetof(CPUState, gpr[11]) },
1845 { "r12", offsetof(CPUState, gpr[12]) },
1846 { "r13", offsetof(CPUState, gpr[13]) },
1847 { "r14", offsetof(CPUState, gpr[14]) },
1848 { "r15", offsetof(CPUState, gpr[15]) },
1849 { "r16", offsetof(CPUState, gpr[16]) },
1850 { "r17", offsetof(CPUState, gpr[17]) },
1851 { "r18", offsetof(CPUState, gpr[18]) },
1852 { "r19", offsetof(CPUState, gpr[19]) },
1853 { "r20", offsetof(CPUState, gpr[20]) },
1854 { "r21", offsetof(CPUState, gpr[21]) },
1855 { "r22", offsetof(CPUState, gpr[22]) },
1856 { "r23", offsetof(CPUState, gpr[23]) },
1857 { "r24", offsetof(CPUState, gpr[24]) },
1858 { "r25", offsetof(CPUState, gpr[25]) },
1859 { "r26", offsetof(CPUState, gpr[26]) },
1860 { "r27", offsetof(CPUState, gpr[27]) },
1861 { "r28", offsetof(CPUState, gpr[28]) },
1862 { "r29", offsetof(CPUState, gpr[29]) },
1863 { "r30", offsetof(CPUState, gpr[30]) },
1864 { "r31", offsetof(CPUState, gpr[31]) },
1865 /* Floating point registers */
1866 { "f0", offsetof(CPUState, fpr[0]) },
1867 { "f1", offsetof(CPUState, fpr[1]) },
1868 { "f2", offsetof(CPUState, fpr[2]) },
1869 { "f3", offsetof(CPUState, fpr[3]) },
1870 { "f4", offsetof(CPUState, fpr[4]) },
1871 { "f5", offsetof(CPUState, fpr[5]) },
1872 { "f6", offsetof(CPUState, fpr[6]) },
1873 { "f7", offsetof(CPUState, fpr[7]) },
1874 { "f8", offsetof(CPUState, fpr[8]) },
1875 { "f9", offsetof(CPUState, fpr[9]) },
1876 { "f10", offsetof(CPUState, fpr[10]) },
1877 { "f11", offsetof(CPUState, fpr[11]) },
1878 { "f12", offsetof(CPUState, fpr[12]) },
1879 { "f13", offsetof(CPUState, fpr[13]) },
1880 { "f14", offsetof(CPUState, fpr[14]) },
1881 { "f15", offsetof(CPUState, fpr[15]) },
1882 { "f16", offsetof(CPUState, fpr[16]) },
1883 { "f17", offsetof(CPUState, fpr[17]) },
1884 { "f18", offsetof(CPUState, fpr[18]) },
1885 { "f19", offsetof(CPUState, fpr[19]) },
1886 { "f20", offsetof(CPUState, fpr[20]) },
1887 { "f21", offsetof(CPUState, fpr[21]) },
1888 { "f22", offsetof(CPUState, fpr[22]) },
1889 { "f23", offsetof(CPUState, fpr[23]) },
1890 { "f24", offsetof(CPUState, fpr[24]) },
1891 { "f25", offsetof(CPUState, fpr[25]) },
1892 { "f26", offsetof(CPUState, fpr[26]) },
1893 { "f27", offsetof(CPUState, fpr[27]) },
1894 { "f28", offsetof(CPUState, fpr[28]) },
1895 { "f29", offsetof(CPUState, fpr[29]) },
1896 { "f30", offsetof(CPUState, fpr[30]) },
1897 { "f31", offsetof(CPUState, fpr[31]) },
1898 { "fpscr", offsetof(CPUState, fpscr) },
1899 /* Next instruction pointer */
1900 { "nip|pc", offsetof(CPUState, nip) },
1901 { "lr", offsetof(CPUState, lr) },
1902 { "ctr", offsetof(CPUState, ctr) },
1903 { "decr", 0, &monitor_get_decr, },
1904 { "ccr", 0, &monitor_get_ccr, },
1905 /* Machine state register */
1906 { "msr", 0, &monitor_get_msr, },
1907 { "xer", 0, &monitor_get_xer, },
1908 { "tbu", 0, &monitor_get_tbu, },
1909 { "tbl", 0, &monitor_get_tbl, },
1910 #if defined(TARGET_PPC64)
1911 /* Address space register */
1912 { "asr", offsetof(CPUState, asr) },
1913 #endif
1914 /* Segment registers */
1915 { "sdr1", offsetof(CPUState, sdr1) },
1916 { "sr0", offsetof(CPUState, sr[0]) },
1917 { "sr1", offsetof(CPUState, sr[1]) },
1918 { "sr2", offsetof(CPUState, sr[2]) },
1919 { "sr3", offsetof(CPUState, sr[3]) },
1920 { "sr4", offsetof(CPUState, sr[4]) },
1921 { "sr5", offsetof(CPUState, sr[5]) },
1922 { "sr6", offsetof(CPUState, sr[6]) },
1923 { "sr7", offsetof(CPUState, sr[7]) },
1924 { "sr8", offsetof(CPUState, sr[8]) },
1925 { "sr9", offsetof(CPUState, sr[9]) },
1926 { "sr10", offsetof(CPUState, sr[10]) },
1927 { "sr11", offsetof(CPUState, sr[11]) },
1928 { "sr12", offsetof(CPUState, sr[12]) },
1929 { "sr13", offsetof(CPUState, sr[13]) },
1930 { "sr14", offsetof(CPUState, sr[14]) },
1931 { "sr15", offsetof(CPUState, sr[15]) },
1932 /* Too lazy to put BATs and SPRs ... */
1933 #elif defined(TARGET_SPARC)
1934 { "g0", offsetof(CPUState, gregs[0]) },
1935 { "g1", offsetof(CPUState, gregs[1]) },
1936 { "g2", offsetof(CPUState, gregs[2]) },
1937 { "g3", offsetof(CPUState, gregs[3]) },
1938 { "g4", offsetof(CPUState, gregs[4]) },
1939 { "g5", offsetof(CPUState, gregs[5]) },
1940 { "g6", offsetof(CPUState, gregs[6]) },
1941 { "g7", offsetof(CPUState, gregs[7]) },
1942 { "o0", 0, monitor_get_reg },
1943 { "o1", 1, monitor_get_reg },
1944 { "o2", 2, monitor_get_reg },
1945 { "o3", 3, monitor_get_reg },
1946 { "o4", 4, monitor_get_reg },
1947 { "o5", 5, monitor_get_reg },
1948 { "o6", 6, monitor_get_reg },
1949 { "o7", 7, monitor_get_reg },
1950 { "l0", 8, monitor_get_reg },
1951 { "l1", 9, monitor_get_reg },
1952 { "l2", 10, monitor_get_reg },
1953 { "l3", 11, monitor_get_reg },
1954 { "l4", 12, monitor_get_reg },
1955 { "l5", 13, monitor_get_reg },
1956 { "l6", 14, monitor_get_reg },
1957 { "l7", 15, monitor_get_reg },
1958 { "i0", 16, monitor_get_reg },
1959 { "i1", 17, monitor_get_reg },
1960 { "i2", 18, monitor_get_reg },
1961 { "i3", 19, monitor_get_reg },
1962 { "i4", 20, monitor_get_reg },
1963 { "i5", 21, monitor_get_reg },
1964 { "i6", 22, monitor_get_reg },
1965 { "i7", 23, monitor_get_reg },
1966 { "pc", offsetof(CPUState, pc) },
1967 { "npc", offsetof(CPUState, npc) },
1968 { "y", offsetof(CPUState, y) },
1969 #ifndef TARGET_SPARC64
1970 { "psr", 0, &monitor_get_psr, },
1971 { "wim", offsetof(CPUState, wim) },
1972 #endif
1973 { "tbr", offsetof(CPUState, tbr) },
1974 { "fsr", offsetof(CPUState, fsr) },
1975 { "f0", offsetof(CPUState, fpr[0]) },
1976 { "f1", offsetof(CPUState, fpr[1]) },
1977 { "f2", offsetof(CPUState, fpr[2]) },
1978 { "f3", offsetof(CPUState, fpr[3]) },
1979 { "f4", offsetof(CPUState, fpr[4]) },
1980 { "f5", offsetof(CPUState, fpr[5]) },
1981 { "f6", offsetof(CPUState, fpr[6]) },
1982 { "f7", offsetof(CPUState, fpr[7]) },
1983 { "f8", offsetof(CPUState, fpr[8]) },
1984 { "f9", offsetof(CPUState, fpr[9]) },
1985 { "f10", offsetof(CPUState, fpr[10]) },
1986 { "f11", offsetof(CPUState, fpr[11]) },
1987 { "f12", offsetof(CPUState, fpr[12]) },
1988 { "f13", offsetof(CPUState, fpr[13]) },
1989 { "f14", offsetof(CPUState, fpr[14]) },
1990 { "f15", offsetof(CPUState, fpr[15]) },
1991 { "f16", offsetof(CPUState, fpr[16]) },
1992 { "f17", offsetof(CPUState, fpr[17]) },
1993 { "f18", offsetof(CPUState, fpr[18]) },
1994 { "f19", offsetof(CPUState, fpr[19]) },
1995 { "f20", offsetof(CPUState, fpr[20]) },
1996 { "f21", offsetof(CPUState, fpr[21]) },
1997 { "f22", offsetof(CPUState, fpr[22]) },
1998 { "f23", offsetof(CPUState, fpr[23]) },
1999 { "f24", offsetof(CPUState, fpr[24]) },
2000 { "f25", offsetof(CPUState, fpr[25]) },
2001 { "f26", offsetof(CPUState, fpr[26]) },
2002 { "f27", offsetof(CPUState, fpr[27]) },
2003 { "f28", offsetof(CPUState, fpr[28]) },
2004 { "f29", offsetof(CPUState, fpr[29]) },
2005 { "f30", offsetof(CPUState, fpr[30]) },
2006 { "f31", offsetof(CPUState, fpr[31]) },
2007 #ifdef TARGET_SPARC64
2008 { "f32", offsetof(CPUState, fpr[32]) },
2009 { "f34", offsetof(CPUState, fpr[34]) },
2010 { "f36", offsetof(CPUState, fpr[36]) },
2011 { "f38", offsetof(CPUState, fpr[38]) },
2012 { "f40", offsetof(CPUState, fpr[40]) },
2013 { "f42", offsetof(CPUState, fpr[42]) },
2014 { "f44", offsetof(CPUState, fpr[44]) },
2015 { "f46", offsetof(CPUState, fpr[46]) },
2016 { "f48", offsetof(CPUState, fpr[48]) },
2017 { "f50", offsetof(CPUState, fpr[50]) },
2018 { "f52", offsetof(CPUState, fpr[52]) },
2019 { "f54", offsetof(CPUState, fpr[54]) },
2020 { "f56", offsetof(CPUState, fpr[56]) },
2021 { "f58", offsetof(CPUState, fpr[58]) },
2022 { "f60", offsetof(CPUState, fpr[60]) },
2023 { "f62", offsetof(CPUState, fpr[62]) },
2024 { "asi", offsetof(CPUState, asi) },
2025 { "pstate", offsetof(CPUState, pstate) },
2026 { "cansave", offsetof(CPUState, cansave) },
2027 { "canrestore", offsetof(CPUState, canrestore) },
2028 { "otherwin", offsetof(CPUState, otherwin) },
2029 { "wstate", offsetof(CPUState, wstate) },
2030 { "cleanwin", offsetof(CPUState, cleanwin) },
2031 { "fprs", offsetof(CPUState, fprs) },
2032 #endif
2033 #endif
2034 { NULL },
2037 static void expr_error(const char *msg)
2039 term_printf("%s\n", msg);
2040 longjmp(expr_env, 1);
2043 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2044 static int get_monitor_def(target_long *pval, const char *name)
2046 const MonitorDef *md;
2047 void *ptr;
2049 for(md = monitor_defs; md->name != NULL; md++) {
2050 if (compare_cmd(name, md->name)) {
2051 if (md->get_value) {
2052 *pval = md->get_value(md, md->offset);
2053 } else {
2054 CPUState *env = mon_get_cpu();
2055 if (!env)
2056 return -2;
2057 ptr = (uint8_t *)env + md->offset;
2058 switch(md->type) {
2059 case MD_I32:
2060 *pval = *(int32_t *)ptr;
2061 break;
2062 case MD_TLONG:
2063 *pval = *(target_long *)ptr;
2064 break;
2065 default:
2066 *pval = 0;
2067 break;
2070 return 0;
2073 return -1;
2076 static void next(void)
2078 if (pch != '\0') {
2079 pch++;
2080 while (qemu_isspace(*pch))
2081 pch++;
2085 static int64_t expr_sum(void);
2087 static int64_t expr_unary(void)
2089 int64_t n;
2090 char *p;
2091 int ret;
2093 switch(*pch) {
2094 case '+':
2095 next();
2096 n = expr_unary();
2097 break;
2098 case '-':
2099 next();
2100 n = -expr_unary();
2101 break;
2102 case '~':
2103 next();
2104 n = ~expr_unary();
2105 break;
2106 case '(':
2107 next();
2108 n = expr_sum();
2109 if (*pch != ')') {
2110 expr_error("')' expected");
2112 next();
2113 break;
2114 case '\'':
2115 pch++;
2116 if (*pch == '\0')
2117 expr_error("character constant expected");
2118 n = *pch;
2119 pch++;
2120 if (*pch != '\'')
2121 expr_error("missing terminating \' character");
2122 next();
2123 break;
2124 case '$':
2126 char buf[128], *q;
2127 target_long reg=0;
2129 pch++;
2130 q = buf;
2131 while ((*pch >= 'a' && *pch <= 'z') ||
2132 (*pch >= 'A' && *pch <= 'Z') ||
2133 (*pch >= '0' && *pch <= '9') ||
2134 *pch == '_' || *pch == '.') {
2135 if ((q - buf) < sizeof(buf) - 1)
2136 *q++ = *pch;
2137 pch++;
2139 while (qemu_isspace(*pch))
2140 pch++;
2141 *q = 0;
2142 ret = get_monitor_def(&reg, buf);
2143 if (ret == -1)
2144 expr_error("unknown register");
2145 else if (ret == -2)
2146 expr_error("no cpu defined");
2147 n = reg;
2149 break;
2150 case '\0':
2151 expr_error("unexpected end of expression");
2152 n = 0;
2153 break;
2154 default:
2155 #if TARGET_PHYS_ADDR_BITS > 32
2156 n = strtoull(pch, &p, 0);
2157 #else
2158 n = strtoul(pch, &p, 0);
2159 #endif
2160 if (pch == p) {
2161 expr_error("invalid char in expression");
2163 pch = p;
2164 while (qemu_isspace(*pch))
2165 pch++;
2166 break;
2168 return n;
2172 static int64_t expr_prod(void)
2174 int64_t val, val2;
2175 int op;
2177 val = expr_unary();
2178 for(;;) {
2179 op = *pch;
2180 if (op != '*' && op != '/' && op != '%')
2181 break;
2182 next();
2183 val2 = expr_unary();
2184 switch(op) {
2185 default:
2186 case '*':
2187 val *= val2;
2188 break;
2189 case '/':
2190 case '%':
2191 if (val2 == 0)
2192 expr_error("division by zero");
2193 if (op == '/')
2194 val /= val2;
2195 else
2196 val %= val2;
2197 break;
2200 return val;
2203 static int64_t expr_logic(void)
2205 int64_t val, val2;
2206 int op;
2208 val = expr_prod();
2209 for(;;) {
2210 op = *pch;
2211 if (op != '&' && op != '|' && op != '^')
2212 break;
2213 next();
2214 val2 = expr_prod();
2215 switch(op) {
2216 default:
2217 case '&':
2218 val &= val2;
2219 break;
2220 case '|':
2221 val |= val2;
2222 break;
2223 case '^':
2224 val ^= val2;
2225 break;
2228 return val;
2231 static int64_t expr_sum(void)
2233 int64_t val, val2;
2234 int op;
2236 val = expr_logic();
2237 for(;;) {
2238 op = *pch;
2239 if (op != '+' && op != '-')
2240 break;
2241 next();
2242 val2 = expr_logic();
2243 if (op == '+')
2244 val += val2;
2245 else
2246 val -= val2;
2248 return val;
2251 static int get_expr(int64_t *pval, const char **pp)
2253 pch = *pp;
2254 if (setjmp(expr_env)) {
2255 *pp = pch;
2256 return -1;
2258 while (qemu_isspace(*pch))
2259 pch++;
2260 *pval = expr_sum();
2261 *pp = pch;
2262 return 0;
2265 static int get_str(char *buf, int buf_size, const char **pp)
2267 const char *p;
2268 char *q;
2269 int c;
2271 q = buf;
2272 p = *pp;
2273 while (qemu_isspace(*p))
2274 p++;
2275 if (*p == '\0') {
2276 fail:
2277 *q = '\0';
2278 *pp = p;
2279 return -1;
2281 if (*p == '\"') {
2282 p++;
2283 while (*p != '\0' && *p != '\"') {
2284 if (*p == '\\') {
2285 p++;
2286 c = *p++;
2287 switch(c) {
2288 case 'n':
2289 c = '\n';
2290 break;
2291 case 'r':
2292 c = '\r';
2293 break;
2294 case '\\':
2295 case '\'':
2296 case '\"':
2297 break;
2298 default:
2299 qemu_printf("unsupported escape code: '\\%c'\n", c);
2300 goto fail;
2302 if ((q - buf) < buf_size - 1) {
2303 *q++ = c;
2305 } else {
2306 if ((q - buf) < buf_size - 1) {
2307 *q++ = *p;
2309 p++;
2312 if (*p != '\"') {
2313 qemu_printf("unterminated string\n");
2314 goto fail;
2316 p++;
2317 } else {
2318 while (*p != '\0' && !qemu_isspace(*p)) {
2319 if ((q - buf) < buf_size - 1) {
2320 *q++ = *p;
2322 p++;
2325 *q = '\0';
2326 *pp = p;
2327 return 0;
2330 static int default_fmt_format = 'x';
2331 static int default_fmt_size = 4;
2333 #define MAX_ARGS 16
2335 static void monitor_handle_command(const char *cmdline)
2337 const char *p, *pstart, *typestr;
2338 char *q;
2339 int c, nb_args, len, i, has_arg;
2340 const term_cmd_t *cmd;
2341 char cmdname[256];
2342 char buf[1024];
2343 void *str_allocated[MAX_ARGS];
2344 void *args[MAX_ARGS];
2345 void (*handler_0)(void);
2346 void (*handler_1)(void *arg0);
2347 void (*handler_2)(void *arg0, void *arg1);
2348 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2349 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2350 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2351 void *arg4);
2352 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2353 void *arg4, void *arg5);
2354 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2355 void *arg4, void *arg5, void *arg6);
2357 #ifdef DEBUG
2358 term_printf("command='%s'\n", cmdline);
2359 #endif
2361 /* extract the command name */
2362 p = cmdline;
2363 q = cmdname;
2364 while (qemu_isspace(*p))
2365 p++;
2366 if (*p == '\0')
2367 return;
2368 pstart = p;
2369 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2370 p++;
2371 len = p - pstart;
2372 if (len > sizeof(cmdname) - 1)
2373 len = sizeof(cmdname) - 1;
2374 memcpy(cmdname, pstart, len);
2375 cmdname[len] = '\0';
2377 /* find the command */
2378 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2379 if (compare_cmd(cmdname, cmd->name))
2380 goto found;
2382 term_printf("unknown command: '%s'\n", cmdname);
2383 return;
2384 found:
2386 for(i = 0; i < MAX_ARGS; i++)
2387 str_allocated[i] = NULL;
2389 /* parse the parameters */
2390 typestr = cmd->args_type;
2391 nb_args = 0;
2392 for(;;) {
2393 c = *typestr;
2394 if (c == '\0')
2395 break;
2396 typestr++;
2397 switch(c) {
2398 case 'F':
2399 case 'B':
2400 case 's':
2402 int ret;
2403 char *str;
2405 while (qemu_isspace(*p))
2406 p++;
2407 if (*typestr == '?') {
2408 typestr++;
2409 if (*p == '\0') {
2410 /* no optional string: NULL argument */
2411 str = NULL;
2412 goto add_str;
2415 ret = get_str(buf, sizeof(buf), &p);
2416 if (ret < 0) {
2417 switch(c) {
2418 case 'F':
2419 term_printf("%s: filename expected\n", cmdname);
2420 break;
2421 case 'B':
2422 term_printf("%s: block device name expected\n", cmdname);
2423 break;
2424 default:
2425 term_printf("%s: string expected\n", cmdname);
2426 break;
2428 goto fail;
2430 str = qemu_malloc(strlen(buf) + 1);
2431 pstrcpy(str, sizeof(buf), buf);
2432 str_allocated[nb_args] = str;
2433 add_str:
2434 if (nb_args >= MAX_ARGS) {
2435 error_args:
2436 term_printf("%s: too many arguments\n", cmdname);
2437 goto fail;
2439 args[nb_args++] = str;
2441 break;
2442 case '/':
2444 int count, format, size;
2446 while (qemu_isspace(*p))
2447 p++;
2448 if (*p == '/') {
2449 /* format found */
2450 p++;
2451 count = 1;
2452 if (qemu_isdigit(*p)) {
2453 count = 0;
2454 while (qemu_isdigit(*p)) {
2455 count = count * 10 + (*p - '0');
2456 p++;
2459 size = -1;
2460 format = -1;
2461 for(;;) {
2462 switch(*p) {
2463 case 'o':
2464 case 'd':
2465 case 'u':
2466 case 'x':
2467 case 'i':
2468 case 'c':
2469 format = *p++;
2470 break;
2471 case 'b':
2472 size = 1;
2473 p++;
2474 break;
2475 case 'h':
2476 size = 2;
2477 p++;
2478 break;
2479 case 'w':
2480 size = 4;
2481 p++;
2482 break;
2483 case 'g':
2484 case 'L':
2485 size = 8;
2486 p++;
2487 break;
2488 default:
2489 goto next;
2492 next:
2493 if (*p != '\0' && !qemu_isspace(*p)) {
2494 term_printf("invalid char in format: '%c'\n", *p);
2495 goto fail;
2497 if (format < 0)
2498 format = default_fmt_format;
2499 if (format != 'i') {
2500 /* for 'i', not specifying a size gives -1 as size */
2501 if (size < 0)
2502 size = default_fmt_size;
2503 default_fmt_size = size;
2505 default_fmt_format = format;
2506 } else {
2507 count = 1;
2508 format = default_fmt_format;
2509 if (format != 'i') {
2510 size = default_fmt_size;
2511 } else {
2512 size = -1;
2515 if (nb_args + 3 > MAX_ARGS)
2516 goto error_args;
2517 args[nb_args++] = (void*)(long)count;
2518 args[nb_args++] = (void*)(long)format;
2519 args[nb_args++] = (void*)(long)size;
2521 break;
2522 case 'i':
2523 case 'l':
2525 int64_t val;
2527 while (qemu_isspace(*p))
2528 p++;
2529 if (*typestr == '?' || *typestr == '.') {
2530 if (*typestr == '?') {
2531 if (*p == '\0')
2532 has_arg = 0;
2533 else
2534 has_arg = 1;
2535 } else {
2536 if (*p == '.') {
2537 p++;
2538 while (qemu_isspace(*p))
2539 p++;
2540 has_arg = 1;
2541 } else {
2542 has_arg = 0;
2545 typestr++;
2546 if (nb_args >= MAX_ARGS)
2547 goto error_args;
2548 args[nb_args++] = (void *)(long)has_arg;
2549 if (!has_arg) {
2550 if (nb_args >= MAX_ARGS)
2551 goto error_args;
2552 val = -1;
2553 goto add_num;
2556 if (get_expr(&val, &p))
2557 goto fail;
2558 add_num:
2559 if (c == 'i') {
2560 if (nb_args >= MAX_ARGS)
2561 goto error_args;
2562 args[nb_args++] = (void *)(long)val;
2563 } else {
2564 if ((nb_args + 1) >= MAX_ARGS)
2565 goto error_args;
2566 #if TARGET_PHYS_ADDR_BITS > 32
2567 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2568 #else
2569 args[nb_args++] = (void *)0;
2570 #endif
2571 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2574 break;
2575 case '-':
2577 int has_option;
2578 /* option */
2580 c = *typestr++;
2581 if (c == '\0')
2582 goto bad_type;
2583 while (qemu_isspace(*p))
2584 p++;
2585 has_option = 0;
2586 if (*p == '-') {
2587 p++;
2588 if (*p != c) {
2589 term_printf("%s: unsupported option -%c\n",
2590 cmdname, *p);
2591 goto fail;
2593 p++;
2594 has_option = 1;
2596 if (nb_args >= MAX_ARGS)
2597 goto error_args;
2598 args[nb_args++] = (void *)(long)has_option;
2600 break;
2601 default:
2602 bad_type:
2603 term_printf("%s: unknown type '%c'\n", cmdname, c);
2604 goto fail;
2607 /* check that all arguments were parsed */
2608 while (qemu_isspace(*p))
2609 p++;
2610 if (*p != '\0') {
2611 term_printf("%s: extraneous characters at the end of line\n",
2612 cmdname);
2613 goto fail;
2616 switch(nb_args) {
2617 case 0:
2618 handler_0 = cmd->handler;
2619 handler_0();
2620 break;
2621 case 1:
2622 handler_1 = cmd->handler;
2623 handler_1(args[0]);
2624 break;
2625 case 2:
2626 handler_2 = cmd->handler;
2627 handler_2(args[0], args[1]);
2628 break;
2629 case 3:
2630 handler_3 = cmd->handler;
2631 handler_3(args[0], args[1], args[2]);
2632 break;
2633 case 4:
2634 handler_4 = cmd->handler;
2635 handler_4(args[0], args[1], args[2], args[3]);
2636 break;
2637 case 5:
2638 handler_5 = cmd->handler;
2639 handler_5(args[0], args[1], args[2], args[3], args[4]);
2640 break;
2641 case 6:
2642 handler_6 = cmd->handler;
2643 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2644 break;
2645 case 7:
2646 handler_7 = cmd->handler;
2647 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2648 break;
2649 default:
2650 term_printf("unsupported number of arguments: %d\n", nb_args);
2651 goto fail;
2653 fail:
2654 for(i = 0; i < MAX_ARGS; i++)
2655 qemu_free(str_allocated[i]);
2656 return;
2659 static void cmd_completion(const char *name, const char *list)
2661 const char *p, *pstart;
2662 char cmd[128];
2663 int len;
2665 p = list;
2666 for(;;) {
2667 pstart = p;
2668 p = strchr(p, '|');
2669 if (!p)
2670 p = pstart + strlen(pstart);
2671 len = p - pstart;
2672 if (len > sizeof(cmd) - 2)
2673 len = sizeof(cmd) - 2;
2674 memcpy(cmd, pstart, len);
2675 cmd[len] = '\0';
2676 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2677 add_completion(cmd);
2679 if (*p == '\0')
2680 break;
2681 p++;
2685 static void file_completion(const char *input)
2687 DIR *ffs;
2688 struct dirent *d;
2689 char path[1024];
2690 char file[1024], file_prefix[1024];
2691 int input_path_len;
2692 const char *p;
2694 p = strrchr(input, '/');
2695 if (!p) {
2696 input_path_len = 0;
2697 pstrcpy(file_prefix, sizeof(file_prefix), input);
2698 pstrcpy(path, sizeof(path), ".");
2699 } else {
2700 input_path_len = p - input + 1;
2701 memcpy(path, input, input_path_len);
2702 if (input_path_len > sizeof(path) - 1)
2703 input_path_len = sizeof(path) - 1;
2704 path[input_path_len] = '\0';
2705 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2707 #ifdef DEBUG_COMPLETION
2708 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2709 #endif
2710 ffs = opendir(path);
2711 if (!ffs)
2712 return;
2713 for(;;) {
2714 struct stat sb;
2715 d = readdir(ffs);
2716 if (!d)
2717 break;
2718 if (strstart(d->d_name, file_prefix, NULL)) {
2719 memcpy(file, input, input_path_len);
2720 if (input_path_len < sizeof(file))
2721 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2722 d->d_name);
2723 /* stat the file to find out if it's a directory.
2724 * In that case add a slash to speed up typing long paths
2726 stat(file, &sb);
2727 if(S_ISDIR(sb.st_mode))
2728 pstrcat(file, sizeof(file), "/");
2729 add_completion(file);
2732 closedir(ffs);
2735 static void block_completion_it(void *opaque, BlockDriverState *bs)
2737 const char *name = bdrv_get_device_name(bs);
2738 const char *input = opaque;
2740 if (input[0] == '\0' ||
2741 !strncmp(name, (char *)input, strlen(input))) {
2742 add_completion(name);
2746 /* NOTE: this parser is an approximate form of the real command parser */
2747 static void parse_cmdline(const char *cmdline,
2748 int *pnb_args, char **args)
2750 const char *p;
2751 int nb_args, ret;
2752 char buf[1024];
2754 p = cmdline;
2755 nb_args = 0;
2756 for(;;) {
2757 while (qemu_isspace(*p))
2758 p++;
2759 if (*p == '\0')
2760 break;
2761 if (nb_args >= MAX_ARGS)
2762 break;
2763 ret = get_str(buf, sizeof(buf), &p);
2764 args[nb_args] = qemu_strdup(buf);
2765 nb_args++;
2766 if (ret < 0)
2767 break;
2769 *pnb_args = nb_args;
2772 void readline_find_completion(const char *cmdline)
2774 const char *cmdname;
2775 char *args[MAX_ARGS];
2776 int nb_args, i, len;
2777 const char *ptype, *str;
2778 const term_cmd_t *cmd;
2779 const KeyDef *key;
2781 parse_cmdline(cmdline, &nb_args, args);
2782 #ifdef DEBUG_COMPLETION
2783 for(i = 0; i < nb_args; i++) {
2784 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2786 #endif
2788 /* if the line ends with a space, it means we want to complete the
2789 next arg */
2790 len = strlen(cmdline);
2791 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2792 if (nb_args >= MAX_ARGS)
2793 return;
2794 args[nb_args++] = qemu_strdup("");
2796 if (nb_args <= 1) {
2797 /* command completion */
2798 if (nb_args == 0)
2799 cmdname = "";
2800 else
2801 cmdname = args[0];
2802 completion_index = strlen(cmdname);
2803 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2804 cmd_completion(cmdname, cmd->name);
2806 } else {
2807 /* find the command */
2808 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2809 if (compare_cmd(args[0], cmd->name))
2810 goto found;
2812 return;
2813 found:
2814 ptype = cmd->args_type;
2815 for(i = 0; i < nb_args - 2; i++) {
2816 if (*ptype != '\0') {
2817 ptype++;
2818 while (*ptype == '?')
2819 ptype++;
2822 str = args[nb_args - 1];
2823 switch(*ptype) {
2824 case 'F':
2825 /* file completion */
2826 completion_index = strlen(str);
2827 file_completion(str);
2828 break;
2829 case 'B':
2830 /* block device name completion */
2831 completion_index = strlen(str);
2832 bdrv_iterate(block_completion_it, (void *)str);
2833 break;
2834 case 's':
2835 /* XXX: more generic ? */
2836 if (!strcmp(cmd->name, "info")) {
2837 completion_index = strlen(str);
2838 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2839 cmd_completion(str, cmd->name);
2841 } else if (!strcmp(cmd->name, "sendkey")) {
2842 completion_index = strlen(str);
2843 for(key = key_defs; key->name != NULL; key++) {
2844 cmd_completion(str, key->name);
2847 break;
2848 default:
2849 break;
2852 for(i = 0; i < nb_args; i++)
2853 qemu_free(args[i]);
2856 static int term_can_read(void *opaque)
2858 return 128;
2861 static void term_read(void *opaque, const uint8_t *buf, int size)
2863 int i;
2864 for(i = 0; i < size; i++)
2865 readline_handle_byte(buf[i]);
2868 static int monitor_suspended;
2870 static void monitor_handle_command1(void *opaque, const char *cmdline)
2872 monitor_handle_command(cmdline);
2873 if (!monitor_suspended)
2874 monitor_start_input();
2875 else
2876 monitor_suspended = 2;
2879 void monitor_suspend(void)
2881 monitor_suspended = 1;
2884 void monitor_resume(void)
2886 if (monitor_suspended == 2)
2887 monitor_start_input();
2888 monitor_suspended = 0;
2891 static void monitor_start_input(void)
2893 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2896 static void term_event(void *opaque, int event)
2898 if (event != CHR_EVENT_RESET)
2899 return;
2901 if (!hide_banner)
2902 term_printf("QEMU %s monitor - type 'help' for more information\n",
2903 QEMU_VERSION);
2904 monitor_start_input();
2907 static int is_first_init = 1;
2909 void monitor_init(CharDriverState *hd, int show_banner)
2911 int i;
2913 if (is_first_init) {
2914 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2915 if (!key_timer)
2916 return;
2917 for (i = 0; i < MAX_MON; i++) {
2918 monitor_hd[i] = NULL;
2920 is_first_init = 0;
2922 for (i = 0; i < MAX_MON; i++) {
2923 if (monitor_hd[i] == NULL) {
2924 monitor_hd[i] = hd;
2925 break;
2929 hide_banner = !show_banner;
2931 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2933 readline_start("", 0, monitor_handle_command1, NULL);
2936 /* XXX: use threads ? */
2937 /* modal monitor readline */
2938 static int monitor_readline_started;
2939 static char *monitor_readline_buf;
2940 static int monitor_readline_buf_size;
2942 static void monitor_readline_cb(void *opaque, const char *input)
2944 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2945 monitor_readline_started = 0;
2948 static void monitor_readline(const char *prompt, int is_password,
2949 char *buf, int buf_size)
2951 int i;
2952 int old_focus[MAX_MON];
2954 if (is_password) {
2955 for (i = 0; i < MAX_MON; i++) {
2956 old_focus[i] = 0;
2957 if (monitor_hd[i]) {
2958 old_focus[i] = monitor_hd[i]->focus;
2959 monitor_hd[i]->focus = 0;
2960 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2965 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2966 monitor_readline_buf = buf;
2967 monitor_readline_buf_size = buf_size;
2968 monitor_readline_started = 1;
2969 while (monitor_readline_started) {
2970 main_loop_wait(10);
2972 /* restore original focus */
2973 if (is_password) {
2974 for (i = 0; i < MAX_MON; i++)
2975 if (old_focus[i])
2976 monitor_hd[i]->focus = old_focus[i];
2980 int monitor_read_bdrv_key(BlockDriverState *bs)
2982 char password[256];
2983 int i;
2985 if (!bdrv_is_encrypted(bs))
2986 return 0;
2988 term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2989 bdrv_get_encrypted_filename(bs));
2990 for(i = 0; i < 3; i++) {
2991 monitor_readline("Password: ", 1, password, sizeof(password));
2992 if (bdrv_set_key(bs, password) == 0)
2993 return 0;
2994 term_printf("invalid password\n");
2996 return -EPERM;