monitor: Rework modal password input (Jan Kiszka)
[qemu/mmix.git] / monitor.c
blob183c846847d3d09dba78bc9cfedda793faa2d823
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 //#define DEBUG
44 //#define DEBUG_COMPLETION
47 * Supported types:
49 * 'F' filename
50 * 'B' block device name
51 * 's' string (accept optional quote)
52 * 'i' 32 bit integer
53 * 'l' target long (32 or 64 bit)
54 * '/' optional gdb-like print format (like "/10x")
56 * '?' optional type (for 'F', 's' and 'i')
60 typedef struct term_cmd_t {
61 const char *name;
62 const char *args_type;
63 void *handler;
64 const char *params;
65 const char *help;
66 } term_cmd_t;
68 #define MAX_MON 4
69 static CharDriverState *monitor_hd[MAX_MON];
70 static int hide_banner;
72 static const term_cmd_t term_cmds[];
73 static const term_cmd_t info_cmds[];
75 static uint8_t term_outbuf[1024];
76 static int term_outbuf_index;
77 static BlockDriverCompletionFunc *password_completion_cb;
78 static void *password_opaque;
80 static void monitor_start_input(void);
82 static CPUState *mon_cpu = NULL;
84 static void monitor_read_password(ReadLineFunc *readline_func, void *opaque)
86 readline_start("Password: ", 1, readline_func, opaque);
89 void term_flush(void)
91 int i;
92 if (term_outbuf_index > 0) {
93 for (i = 0; i < MAX_MON; i++)
94 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
95 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
96 term_outbuf_index = 0;
100 /* flush at every end of line or if the buffer is full */
101 void term_puts(const char *str)
103 char c;
104 for(;;) {
105 c = *str++;
106 if (c == '\0')
107 break;
108 if (c == '\n')
109 term_outbuf[term_outbuf_index++] = '\r';
110 term_outbuf[term_outbuf_index++] = c;
111 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
112 c == '\n')
113 term_flush();
117 void term_vprintf(const char *fmt, va_list ap)
119 char buf[4096];
120 vsnprintf(buf, sizeof(buf), fmt, ap);
121 term_puts(buf);
124 void term_printf(const char *fmt, ...)
126 va_list ap;
127 va_start(ap, fmt);
128 term_vprintf(fmt, ap);
129 va_end(ap);
132 void term_print_filename(const char *filename)
134 int i;
136 for (i = 0; filename[i]; i++) {
137 switch (filename[i]) {
138 case ' ':
139 case '"':
140 case '\\':
141 term_printf("\\%c", filename[i]);
142 break;
143 case '\t':
144 term_printf("\\t");
145 break;
146 case '\r':
147 term_printf("\\r");
148 break;
149 case '\n':
150 term_printf("\\n");
151 break;
152 default:
153 term_printf("%c", filename[i]);
154 break;
159 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
161 va_list ap;
162 va_start(ap, fmt);
163 term_vprintf(fmt, ap);
164 va_end(ap);
165 return 0;
168 static int compare_cmd(const char *name, const char *list)
170 const char *p, *pstart;
171 int len;
172 len = strlen(name);
173 p = list;
174 for(;;) {
175 pstart = p;
176 p = strchr(p, '|');
177 if (!p)
178 p = pstart + strlen(pstart);
179 if ((p - pstart) == len && !memcmp(pstart, name, len))
180 return 1;
181 if (*p == '\0')
182 break;
183 p++;
185 return 0;
188 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
190 const term_cmd_t *cmd;
192 for(cmd = cmds; cmd->name != NULL; cmd++) {
193 if (!name || !strcmp(name, cmd->name))
194 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
198 static void help_cmd(const char *name)
200 if (name && !strcmp(name, "info")) {
201 help_cmd1(info_cmds, "info ", NULL);
202 } else {
203 help_cmd1(term_cmds, "", name);
204 if (name && !strcmp(name, "log")) {
205 const CPULogItem *item;
206 term_printf("Log items (comma separated):\n");
207 term_printf("%-10s %s\n", "none", "remove all logs");
208 for(item = cpu_log_items; item->mask != 0; item++) {
209 term_printf("%-10s %s\n", item->name, item->help);
215 static void do_help(const char *name)
217 help_cmd(name);
220 static void do_commit(const char *device)
222 int i, all_devices;
224 all_devices = !strcmp(device, "all");
225 for (i = 0; i < nb_drives; i++) {
226 if (all_devices ||
227 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
228 bdrv_commit(drives_table[i].bdrv);
232 static void do_info(const char *item)
234 const term_cmd_t *cmd;
235 void (*handler)(void);
237 if (!item)
238 goto help;
239 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
240 if (compare_cmd(item, cmd->name))
241 goto found;
243 help:
244 help_cmd("info");
245 return;
246 found:
247 handler = cmd->handler;
248 handler();
251 static void do_info_version(void)
253 term_printf("%s\n", QEMU_VERSION);
256 static void do_info_name(void)
258 if (qemu_name)
259 term_printf("%s\n", qemu_name);
262 #if defined(TARGET_I386)
263 static void do_info_hpet(void)
265 term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
267 #endif
269 static void do_info_uuid(void)
271 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
272 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
273 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
274 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
275 qemu_uuid[15]);
278 static void do_info_block(void)
280 bdrv_info();
283 static void do_info_blockstats(void)
285 bdrv_info_stats();
288 /* get the current CPU defined by the user */
289 static int mon_set_cpu(int cpu_index)
291 CPUState *env;
293 for(env = first_cpu; env != NULL; env = env->next_cpu) {
294 if (env->cpu_index == cpu_index) {
295 mon_cpu = env;
296 return 0;
299 return -1;
302 static CPUState *mon_get_cpu(void)
304 if (!mon_cpu) {
305 mon_set_cpu(0);
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 term_printf("%c CPU #%d:",
334 (env == mon_cpu) ? '*' : ' ',
335 env->cpu_index);
336 #if defined(TARGET_I386)
337 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
338 #elif defined(TARGET_PPC)
339 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
340 #elif defined(TARGET_SPARC)
341 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
342 #elif defined(TARGET_MIPS)
343 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
344 #endif
345 if (env->halted)
346 term_printf(" (halted)");
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_info_jit(void)
359 dump_exec_info(NULL, monitor_fprintf);
362 static void do_info_history (void)
364 int i;
365 const char *str;
367 i = 0;
368 for(;;) {
369 str = readline_get_history(i);
370 if (!str)
371 break;
372 term_printf("%d: '%s'\n", i, str);
373 i++;
377 #if defined(TARGET_PPC)
378 /* XXX: not implemented in other targets */
379 static void do_info_cpu_stats (void)
381 CPUState *env;
383 env = mon_get_cpu();
384 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
386 #endif
388 static void do_quit(void)
390 exit(0);
393 static int eject_device(BlockDriverState *bs, int force)
395 if (bdrv_is_inserted(bs)) {
396 if (!force) {
397 if (!bdrv_is_removable(bs)) {
398 term_printf("device is not removable\n");
399 return -1;
401 if (bdrv_is_locked(bs)) {
402 term_printf("device is locked\n");
403 return -1;
406 bdrv_close(bs);
408 return 0;
411 static void do_eject(int force, const char *filename)
413 BlockDriverState *bs;
415 bs = bdrv_find(filename);
416 if (!bs) {
417 term_printf("device not found\n");
418 return;
420 eject_device(bs, force);
423 static void do_change_block(const char *device, const char *filename, const char *fmt)
425 BlockDriverState *bs;
426 BlockDriver *drv = NULL;
428 bs = bdrv_find(device);
429 if (!bs) {
430 term_printf("device not found\n");
431 return;
433 if (fmt) {
434 drv = bdrv_find_format(fmt);
435 if (!drv) {
436 term_printf("invalid format %s\n", fmt);
437 return;
440 if (eject_device(bs, 0) < 0)
441 return;
442 bdrv_open2(bs, filename, 0, drv);
443 monitor_read_bdrv_key_start(bs, NULL, NULL);
446 static void change_vnc_password_cb(void *opaque, const char *password)
448 if (vnc_display_password(NULL, password) < 0)
449 term_printf("could not set VNC server password\n");
451 monitor_start_input();
454 static void do_change_vnc(const char *target, const char *arg)
456 if (strcmp(target, "passwd") == 0 ||
457 strcmp(target, "password") == 0) {
458 if (arg) {
459 char password[9];
460 strncpy(password, arg, sizeof(password));
461 password[sizeof(password) - 1] = '\0';
462 change_vnc_password_cb(NULL, password);
463 } else {
464 monitor_read_password(change_vnc_password_cb, NULL);
466 } else {
467 if (vnc_display_open(NULL, target) < 0)
468 term_printf("could not start VNC server on %s\n", target);
472 static void do_change(const char *device, const char *target, const char *arg)
474 if (strcmp(device, "vnc") == 0) {
475 do_change_vnc(target, arg);
476 } else {
477 do_change_block(device, target, arg);
481 static void do_screen_dump(const char *filename)
483 vga_hw_screen_dump(filename);
486 static void do_logfile(const char *filename)
488 cpu_set_log_filename(filename);
491 static void do_log(const char *items)
493 int mask;
495 if (!strcmp(items, "none")) {
496 mask = 0;
497 } else {
498 mask = cpu_str_to_log_mask(items);
499 if (!mask) {
500 help_cmd("log");
501 return;
504 cpu_set_log(mask);
507 static void do_stop(void)
509 vm_stop(EXCP_INTERRUPT);
512 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
514 static void do_cont(void)
516 int err = 0;
518 bdrv_iterate(encrypted_bdrv_it, &err);
519 /* only resume the vm if all keys are set and valid */
520 if (!err)
521 vm_start();
524 static void bdrv_key_cb(void *opaque, int err)
526 /* another key was set successfully, retry to continue */
527 if (!err)
528 do_cont();
531 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
533 int *err = opaque;
535 if (!*err && bdrv_key_required(bs)) {
536 *err = -EBUSY;
537 monitor_read_bdrv_key_start(bs, bdrv_key_cb, NULL);
541 #ifdef CONFIG_GDBSTUB
542 static void do_gdbserver(const char *port)
544 if (!port)
545 port = DEFAULT_GDBSTUB_PORT;
546 if (gdbserver_start(port) < 0) {
547 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
548 } else {
549 qemu_printf("Waiting gdb connection on port '%s'\n", port);
552 #endif
554 static void term_printc(int c)
556 term_printf("'");
557 switch(c) {
558 case '\'':
559 term_printf("\\'");
560 break;
561 case '\\':
562 term_printf("\\\\");
563 break;
564 case '\n':
565 term_printf("\\n");
566 break;
567 case '\r':
568 term_printf("\\r");
569 break;
570 default:
571 if (c >= 32 && c <= 126) {
572 term_printf("%c", c);
573 } else {
574 term_printf("\\x%02x", c);
576 break;
578 term_printf("'");
581 static void memory_dump(int count, int format, int wsize,
582 target_phys_addr_t addr, int is_physical)
584 CPUState *env;
585 int nb_per_line, l, line_size, i, max_digits, len;
586 uint8_t buf[16];
587 uint64_t v;
589 if (format == 'i') {
590 int flags;
591 flags = 0;
592 env = mon_get_cpu();
593 if (!env && !is_physical)
594 return;
595 #ifdef TARGET_I386
596 if (wsize == 2) {
597 flags = 1;
598 } else if (wsize == 4) {
599 flags = 0;
600 } else {
601 /* as default we use the current CS size */
602 flags = 0;
603 if (env) {
604 #ifdef TARGET_X86_64
605 if ((env->efer & MSR_EFER_LMA) &&
606 (env->segs[R_CS].flags & DESC_L_MASK))
607 flags = 2;
608 else
609 #endif
610 if (!(env->segs[R_CS].flags & DESC_B_MASK))
611 flags = 1;
614 #endif
615 monitor_disas(env, addr, count, is_physical, flags);
616 return;
619 len = wsize * count;
620 if (wsize == 1)
621 line_size = 8;
622 else
623 line_size = 16;
624 nb_per_line = line_size / wsize;
625 max_digits = 0;
627 switch(format) {
628 case 'o':
629 max_digits = (wsize * 8 + 2) / 3;
630 break;
631 default:
632 case 'x':
633 max_digits = (wsize * 8) / 4;
634 break;
635 case 'u':
636 case 'd':
637 max_digits = (wsize * 8 * 10 + 32) / 33;
638 break;
639 case 'c':
640 wsize = 1;
641 break;
644 while (len > 0) {
645 if (is_physical)
646 term_printf(TARGET_FMT_plx ":", addr);
647 else
648 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
649 l = len;
650 if (l > line_size)
651 l = line_size;
652 if (is_physical) {
653 cpu_physical_memory_rw(addr, buf, l, 0);
654 } else {
655 env = mon_get_cpu();
656 if (!env)
657 break;
658 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
659 term_printf(" Cannot access memory\n");
660 break;
663 i = 0;
664 while (i < l) {
665 switch(wsize) {
666 default:
667 case 1:
668 v = ldub_raw(buf + i);
669 break;
670 case 2:
671 v = lduw_raw(buf + i);
672 break;
673 case 4:
674 v = (uint32_t)ldl_raw(buf + i);
675 break;
676 case 8:
677 v = ldq_raw(buf + i);
678 break;
680 term_printf(" ");
681 switch(format) {
682 case 'o':
683 term_printf("%#*" PRIo64, max_digits, v);
684 break;
685 case 'x':
686 term_printf("0x%0*" PRIx64, max_digits, v);
687 break;
688 case 'u':
689 term_printf("%*" PRIu64, max_digits, v);
690 break;
691 case 'd':
692 term_printf("%*" PRId64, max_digits, v);
693 break;
694 case 'c':
695 term_printc(v);
696 break;
698 i += wsize;
700 term_printf("\n");
701 addr += l;
702 len -= l;
706 #if TARGET_LONG_BITS == 64
707 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
708 #else
709 #define GET_TLONG(h, l) (l)
710 #endif
712 static void do_memory_dump(int count, int format, int size,
713 uint32_t addrh, uint32_t addrl)
715 target_long addr = GET_TLONG(addrh, addrl);
716 memory_dump(count, format, size, addr, 0);
719 #if TARGET_PHYS_ADDR_BITS > 32
720 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
721 #else
722 #define GET_TPHYSADDR(h, l) (l)
723 #endif
725 static void do_physical_memory_dump(int count, int format, int size,
726 uint32_t addrh, uint32_t addrl)
729 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
730 memory_dump(count, format, size, addr, 1);
733 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
735 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
736 #if TARGET_PHYS_ADDR_BITS == 32
737 switch(format) {
738 case 'o':
739 term_printf("%#o", val);
740 break;
741 case 'x':
742 term_printf("%#x", val);
743 break;
744 case 'u':
745 term_printf("%u", val);
746 break;
747 default:
748 case 'd':
749 term_printf("%d", val);
750 break;
751 case 'c':
752 term_printc(val);
753 break;
755 #else
756 switch(format) {
757 case 'o':
758 term_printf("%#" PRIo64, val);
759 break;
760 case 'x':
761 term_printf("%#" PRIx64, val);
762 break;
763 case 'u':
764 term_printf("%" PRIu64, val);
765 break;
766 default:
767 case 'd':
768 term_printf("%" PRId64, val);
769 break;
770 case 'c':
771 term_printc(val);
772 break;
774 #endif
775 term_printf("\n");
778 static void do_memory_save(unsigned int valh, unsigned int vall,
779 uint32_t size, const char *filename)
781 FILE *f;
782 target_long addr = GET_TLONG(valh, vall);
783 uint32_t l;
784 CPUState *env;
785 uint8_t buf[1024];
787 env = mon_get_cpu();
788 if (!env)
789 return;
791 f = fopen(filename, "wb");
792 if (!f) {
793 term_printf("could not open '%s'\n", filename);
794 return;
796 while (size != 0) {
797 l = sizeof(buf);
798 if (l > size)
799 l = size;
800 cpu_memory_rw_debug(env, addr, buf, l, 0);
801 fwrite(buf, 1, l, f);
802 addr += l;
803 size -= l;
805 fclose(f);
808 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
809 uint32_t size, const char *filename)
811 FILE *f;
812 uint32_t l;
813 uint8_t buf[1024];
814 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
816 f = fopen(filename, "wb");
817 if (!f) {
818 term_printf("could not open '%s'\n", filename);
819 return;
821 while (size != 0) {
822 l = sizeof(buf);
823 if (l > size)
824 l = size;
825 cpu_physical_memory_rw(addr, buf, l, 0);
826 fwrite(buf, 1, l, f);
827 fflush(f);
828 addr += l;
829 size -= l;
831 fclose(f);
834 static void do_sum(uint32_t start, uint32_t size)
836 uint32_t addr;
837 uint8_t buf[1];
838 uint16_t sum;
840 sum = 0;
841 for(addr = start; addr < (start + size); addr++) {
842 cpu_physical_memory_rw(addr, buf, 1, 0);
843 /* BSD sum algorithm ('sum' Unix command) */
844 sum = (sum >> 1) | (sum << 15);
845 sum += buf[0];
847 term_printf("%05d\n", sum);
850 typedef struct {
851 int keycode;
852 const char *name;
853 } KeyDef;
855 static const KeyDef key_defs[] = {
856 { 0x2a, "shift" },
857 { 0x36, "shift_r" },
859 { 0x38, "alt" },
860 { 0xb8, "alt_r" },
861 { 0x64, "altgr" },
862 { 0xe4, "altgr_r" },
863 { 0x1d, "ctrl" },
864 { 0x9d, "ctrl_r" },
866 { 0xdd, "menu" },
868 { 0x01, "esc" },
870 { 0x02, "1" },
871 { 0x03, "2" },
872 { 0x04, "3" },
873 { 0x05, "4" },
874 { 0x06, "5" },
875 { 0x07, "6" },
876 { 0x08, "7" },
877 { 0x09, "8" },
878 { 0x0a, "9" },
879 { 0x0b, "0" },
880 { 0x0c, "minus" },
881 { 0x0d, "equal" },
882 { 0x0e, "backspace" },
884 { 0x0f, "tab" },
885 { 0x10, "q" },
886 { 0x11, "w" },
887 { 0x12, "e" },
888 { 0x13, "r" },
889 { 0x14, "t" },
890 { 0x15, "y" },
891 { 0x16, "u" },
892 { 0x17, "i" },
893 { 0x18, "o" },
894 { 0x19, "p" },
896 { 0x1c, "ret" },
898 { 0x1e, "a" },
899 { 0x1f, "s" },
900 { 0x20, "d" },
901 { 0x21, "f" },
902 { 0x22, "g" },
903 { 0x23, "h" },
904 { 0x24, "j" },
905 { 0x25, "k" },
906 { 0x26, "l" },
908 { 0x2c, "z" },
909 { 0x2d, "x" },
910 { 0x2e, "c" },
911 { 0x2f, "v" },
912 { 0x30, "b" },
913 { 0x31, "n" },
914 { 0x32, "m" },
915 { 0x33, "comma" },
916 { 0x34, "dot" },
917 { 0x35, "slash" },
919 { 0x37, "asterisk" },
921 { 0x39, "spc" },
922 { 0x3a, "caps_lock" },
923 { 0x3b, "f1" },
924 { 0x3c, "f2" },
925 { 0x3d, "f3" },
926 { 0x3e, "f4" },
927 { 0x3f, "f5" },
928 { 0x40, "f6" },
929 { 0x41, "f7" },
930 { 0x42, "f8" },
931 { 0x43, "f9" },
932 { 0x44, "f10" },
933 { 0x45, "num_lock" },
934 { 0x46, "scroll_lock" },
936 { 0xb5, "kp_divide" },
937 { 0x37, "kp_multiply" },
938 { 0x4a, "kp_subtract" },
939 { 0x4e, "kp_add" },
940 { 0x9c, "kp_enter" },
941 { 0x53, "kp_decimal" },
942 { 0x54, "sysrq" },
944 { 0x52, "kp_0" },
945 { 0x4f, "kp_1" },
946 { 0x50, "kp_2" },
947 { 0x51, "kp_3" },
948 { 0x4b, "kp_4" },
949 { 0x4c, "kp_5" },
950 { 0x4d, "kp_6" },
951 { 0x47, "kp_7" },
952 { 0x48, "kp_8" },
953 { 0x49, "kp_9" },
955 { 0x56, "<" },
957 { 0x57, "f11" },
958 { 0x58, "f12" },
960 { 0xb7, "print" },
962 { 0xc7, "home" },
963 { 0xc9, "pgup" },
964 { 0xd1, "pgdn" },
965 { 0xcf, "end" },
967 { 0xcb, "left" },
968 { 0xc8, "up" },
969 { 0xd0, "down" },
970 { 0xcd, "right" },
972 { 0xd2, "insert" },
973 { 0xd3, "delete" },
974 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
975 { 0xf0, "stop" },
976 { 0xf1, "again" },
977 { 0xf2, "props" },
978 { 0xf3, "undo" },
979 { 0xf4, "front" },
980 { 0xf5, "copy" },
981 { 0xf6, "open" },
982 { 0xf7, "paste" },
983 { 0xf8, "find" },
984 { 0xf9, "cut" },
985 { 0xfa, "lf" },
986 { 0xfb, "help" },
987 { 0xfc, "meta_l" },
988 { 0xfd, "meta_r" },
989 { 0xfe, "compose" },
990 #endif
991 { 0, NULL },
994 static int get_keycode(const char *key)
996 const KeyDef *p;
997 char *endp;
998 int ret;
1000 for(p = key_defs; p->name != NULL; p++) {
1001 if (!strcmp(key, p->name))
1002 return p->keycode;
1004 if (strstart(key, "0x", NULL)) {
1005 ret = strtoul(key, &endp, 0);
1006 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1007 return ret;
1009 return -1;
1012 #define MAX_KEYCODES 16
1013 static uint8_t keycodes[MAX_KEYCODES];
1014 static int nb_pending_keycodes;
1015 static QEMUTimer *key_timer;
1017 static void release_keys(void *opaque)
1019 int keycode;
1021 while (nb_pending_keycodes > 0) {
1022 nb_pending_keycodes--;
1023 keycode = keycodes[nb_pending_keycodes];
1024 if (keycode & 0x80)
1025 kbd_put_keycode(0xe0);
1026 kbd_put_keycode(keycode | 0x80);
1030 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1032 char keyname_buf[16];
1033 char *separator;
1034 int keyname_len, keycode, i;
1036 if (nb_pending_keycodes > 0) {
1037 qemu_del_timer(key_timer);
1038 release_keys(NULL);
1040 if (!has_hold_time)
1041 hold_time = 100;
1042 i = 0;
1043 while (1) {
1044 separator = strchr(string, '-');
1045 keyname_len = separator ? separator - string : strlen(string);
1046 if (keyname_len > 0) {
1047 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1048 if (keyname_len > sizeof(keyname_buf) - 1) {
1049 term_printf("invalid key: '%s...'\n", keyname_buf);
1050 return;
1052 if (i == MAX_KEYCODES) {
1053 term_printf("too many keys\n");
1054 return;
1056 keyname_buf[keyname_len] = 0;
1057 keycode = get_keycode(keyname_buf);
1058 if (keycode < 0) {
1059 term_printf("unknown key: '%s'\n", keyname_buf);
1060 return;
1062 keycodes[i++] = keycode;
1064 if (!separator)
1065 break;
1066 string = separator + 1;
1068 nb_pending_keycodes = i;
1069 /* key down events */
1070 for (i = 0; i < nb_pending_keycodes; i++) {
1071 keycode = keycodes[i];
1072 if (keycode & 0x80)
1073 kbd_put_keycode(0xe0);
1074 kbd_put_keycode(keycode & 0x7f);
1076 /* delayed key up events */
1077 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1078 muldiv64(ticks_per_sec, hold_time, 1000));
1081 static int mouse_button_state;
1083 static void do_mouse_move(const char *dx_str, const char *dy_str,
1084 const char *dz_str)
1086 int dx, dy, dz;
1087 dx = strtol(dx_str, NULL, 0);
1088 dy = strtol(dy_str, NULL, 0);
1089 dz = 0;
1090 if (dz_str)
1091 dz = strtol(dz_str, NULL, 0);
1092 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1095 static void do_mouse_button(int button_state)
1097 mouse_button_state = button_state;
1098 kbd_mouse_event(0, 0, 0, mouse_button_state);
1101 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1103 uint32_t val;
1104 int suffix;
1106 if (has_index) {
1107 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1108 addr++;
1110 addr &= 0xffff;
1112 switch(size) {
1113 default:
1114 case 1:
1115 val = cpu_inb(NULL, addr);
1116 suffix = 'b';
1117 break;
1118 case 2:
1119 val = cpu_inw(NULL, addr);
1120 suffix = 'w';
1121 break;
1122 case 4:
1123 val = cpu_inl(NULL, addr);
1124 suffix = 'l';
1125 break;
1127 term_printf("port%c[0x%04x] = %#0*x\n",
1128 suffix, addr, size * 2, val);
1131 /* boot_set handler */
1132 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1133 static void *boot_opaque;
1135 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1137 qemu_boot_set_handler = func;
1138 boot_opaque = opaque;
1141 static void do_boot_set(const char *bootdevice)
1143 int res;
1145 if (qemu_boot_set_handler) {
1146 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1147 if (res == 0)
1148 term_printf("boot device list now set to %s\n", bootdevice);
1149 else
1150 term_printf("setting boot device list failed with error %i\n", res);
1151 } else {
1152 term_printf("no function defined to set boot device list for this architecture\n");
1156 static void do_system_reset(void)
1158 qemu_system_reset_request();
1161 static void do_system_powerdown(void)
1163 qemu_system_powerdown_request();
1166 #if defined(TARGET_I386)
1167 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1169 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1170 addr,
1171 pte & mask,
1172 pte & PG_GLOBAL_MASK ? 'G' : '-',
1173 pte & PG_PSE_MASK ? 'P' : '-',
1174 pte & PG_DIRTY_MASK ? 'D' : '-',
1175 pte & PG_ACCESSED_MASK ? 'A' : '-',
1176 pte & PG_PCD_MASK ? 'C' : '-',
1177 pte & PG_PWT_MASK ? 'T' : '-',
1178 pte & PG_USER_MASK ? 'U' : '-',
1179 pte & PG_RW_MASK ? 'W' : '-');
1182 static void tlb_info(void)
1184 CPUState *env;
1185 int l1, l2;
1186 uint32_t pgd, pde, pte;
1188 env = mon_get_cpu();
1189 if (!env)
1190 return;
1192 if (!(env->cr[0] & CR0_PG_MASK)) {
1193 term_printf("PG disabled\n");
1194 return;
1196 pgd = env->cr[3] & ~0xfff;
1197 for(l1 = 0; l1 < 1024; l1++) {
1198 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1199 pde = le32_to_cpu(pde);
1200 if (pde & PG_PRESENT_MASK) {
1201 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1202 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1203 } else {
1204 for(l2 = 0; l2 < 1024; l2++) {
1205 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1206 (uint8_t *)&pte, 4);
1207 pte = le32_to_cpu(pte);
1208 if (pte & PG_PRESENT_MASK) {
1209 print_pte((l1 << 22) + (l2 << 12),
1210 pte & ~PG_PSE_MASK,
1211 ~0xfff);
1219 static void mem_print(uint32_t *pstart, int *plast_prot,
1220 uint32_t end, int prot)
1222 int prot1;
1223 prot1 = *plast_prot;
1224 if (prot != prot1) {
1225 if (*pstart != -1) {
1226 term_printf("%08x-%08x %08x %c%c%c\n",
1227 *pstart, end, end - *pstart,
1228 prot1 & PG_USER_MASK ? 'u' : '-',
1229 'r',
1230 prot1 & PG_RW_MASK ? 'w' : '-');
1232 if (prot != 0)
1233 *pstart = end;
1234 else
1235 *pstart = -1;
1236 *plast_prot = prot;
1240 static void mem_info(void)
1242 CPUState *env;
1243 int l1, l2, prot, last_prot;
1244 uint32_t pgd, pde, pte, start, end;
1246 env = mon_get_cpu();
1247 if (!env)
1248 return;
1250 if (!(env->cr[0] & CR0_PG_MASK)) {
1251 term_printf("PG disabled\n");
1252 return;
1254 pgd = env->cr[3] & ~0xfff;
1255 last_prot = 0;
1256 start = -1;
1257 for(l1 = 0; l1 < 1024; l1++) {
1258 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1259 pde = le32_to_cpu(pde);
1260 end = l1 << 22;
1261 if (pde & PG_PRESENT_MASK) {
1262 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1263 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1264 mem_print(&start, &last_prot, end, prot);
1265 } else {
1266 for(l2 = 0; l2 < 1024; l2++) {
1267 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1268 (uint8_t *)&pte, 4);
1269 pte = le32_to_cpu(pte);
1270 end = (l1 << 22) + (l2 << 12);
1271 if (pte & PG_PRESENT_MASK) {
1272 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1273 } else {
1274 prot = 0;
1276 mem_print(&start, &last_prot, end, prot);
1279 } else {
1280 prot = 0;
1281 mem_print(&start, &last_prot, end, prot);
1285 #endif
1287 #if defined(TARGET_SH4)
1289 static void print_tlb(int idx, tlb_t *tlb)
1291 term_printf(" tlb%i:\t"
1292 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1293 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1294 "dirty=%hhu writethrough=%hhu\n",
1295 idx,
1296 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1297 tlb->v, tlb->sh, tlb->c, tlb->pr,
1298 tlb->d, tlb->wt);
1301 static void tlb_info(void)
1303 CPUState *env = mon_get_cpu();
1304 int i;
1306 term_printf ("ITLB:\n");
1307 for (i = 0 ; i < ITLB_SIZE ; i++)
1308 print_tlb (i, &env->itlb[i]);
1309 term_printf ("UTLB:\n");
1310 for (i = 0 ; i < UTLB_SIZE ; i++)
1311 print_tlb (i, &env->utlb[i]);
1314 #endif
1316 static void do_info_kqemu(void)
1318 #ifdef USE_KQEMU
1319 CPUState *env;
1320 int val;
1321 val = 0;
1322 env = mon_get_cpu();
1323 if (!env) {
1324 term_printf("No cpu initialized yet");
1325 return;
1327 val = env->kqemu_enabled;
1328 term_printf("kqemu support: ");
1329 switch(val) {
1330 default:
1331 case 0:
1332 term_printf("disabled\n");
1333 break;
1334 case 1:
1335 term_printf("enabled for user code\n");
1336 break;
1337 case 2:
1338 term_printf("enabled for user and kernel code\n");
1339 break;
1341 #else
1342 term_printf("kqemu support: not compiled\n");
1343 #endif
1346 static void do_info_kvm(void)
1348 #ifdef CONFIG_KVM
1349 term_printf("kvm support: ");
1350 if (kvm_enabled())
1351 term_printf("enabled\n");
1352 else
1353 term_printf("disabled\n");
1354 #else
1355 term_printf("kvm support: not compiled\n");
1356 #endif
1359 #ifdef CONFIG_PROFILER
1361 int64_t kqemu_time;
1362 int64_t qemu_time;
1363 int64_t kqemu_exec_count;
1364 int64_t dev_time;
1365 int64_t kqemu_ret_int_count;
1366 int64_t kqemu_ret_excp_count;
1367 int64_t kqemu_ret_intr_count;
1369 static void do_info_profile(void)
1371 int64_t total;
1372 total = qemu_time;
1373 if (total == 0)
1374 total = 1;
1375 term_printf("async time %" PRId64 " (%0.3f)\n",
1376 dev_time, dev_time / (double)ticks_per_sec);
1377 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1378 qemu_time, qemu_time / (double)ticks_per_sec);
1379 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1380 kqemu_time, kqemu_time / (double)ticks_per_sec,
1381 kqemu_time / (double)total * 100.0,
1382 kqemu_exec_count,
1383 kqemu_ret_int_count,
1384 kqemu_ret_excp_count,
1385 kqemu_ret_intr_count);
1386 qemu_time = 0;
1387 kqemu_time = 0;
1388 kqemu_exec_count = 0;
1389 dev_time = 0;
1390 kqemu_ret_int_count = 0;
1391 kqemu_ret_excp_count = 0;
1392 kqemu_ret_intr_count = 0;
1393 #ifdef USE_KQEMU
1394 kqemu_record_dump();
1395 #endif
1397 #else
1398 static void do_info_profile(void)
1400 term_printf("Internal profiler not compiled\n");
1402 #endif
1404 /* Capture support */
1405 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1407 static void do_info_capture (void)
1409 int i;
1410 CaptureState *s;
1412 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1413 term_printf ("[%d]: ", i);
1414 s->ops.info (s->opaque);
1418 static void do_stop_capture (int n)
1420 int i;
1421 CaptureState *s;
1423 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1424 if (i == n) {
1425 s->ops.destroy (s->opaque);
1426 LIST_REMOVE (s, entries);
1427 qemu_free (s);
1428 return;
1433 #ifdef HAS_AUDIO
1434 static void do_wav_capture (const char *path,
1435 int has_freq, int freq,
1436 int has_bits, int bits,
1437 int has_channels, int nchannels)
1439 CaptureState *s;
1441 s = qemu_mallocz (sizeof (*s));
1443 freq = has_freq ? freq : 44100;
1444 bits = has_bits ? bits : 16;
1445 nchannels = has_channels ? nchannels : 2;
1447 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1448 term_printf ("Faied to add wave capture\n");
1449 qemu_free (s);
1451 LIST_INSERT_HEAD (&capture_head, s, entries);
1453 #endif
1455 #if defined(TARGET_I386)
1456 static void do_inject_nmi(int cpu_index)
1458 CPUState *env;
1460 for (env = first_cpu; env != NULL; env = env->next_cpu)
1461 if (env->cpu_index == cpu_index) {
1462 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1463 break;
1466 #endif
1468 static void do_info_status(void)
1470 if (vm_running)
1471 term_printf("VM status: running\n");
1472 else
1473 term_printf("VM status: paused\n");
1477 static void do_balloon(int value)
1479 ram_addr_t target = value;
1480 qemu_balloon(target << 20);
1483 static void do_info_balloon(void)
1485 ram_addr_t actual;
1487 actual = qemu_balloon_status();
1488 if (kvm_enabled() && !kvm_has_sync_mmu())
1489 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1490 else if (actual == 0)
1491 term_printf("Ballooning not activated in VM\n");
1492 else
1493 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1496 /* Please update qemu-doc.texi when adding or changing commands */
1497 static const term_cmd_t term_cmds[] = {
1498 { "help|?", "s?", do_help,
1499 "[cmd]", "show the help" },
1500 { "commit", "s", do_commit,
1501 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1502 { "info", "s?", do_info,
1503 "subcommand", "show various information about the system state" },
1504 { "q|quit", "", do_quit,
1505 "", "quit the emulator" },
1506 { "eject", "-fB", do_eject,
1507 "[-f] device", "eject a removable medium (use -f to force it)" },
1508 { "change", "BFs?", do_change,
1509 "device filename [format]", "change a removable medium, optional format" },
1510 { "screendump", "F", do_screen_dump,
1511 "filename", "save screen into PPM image 'filename'" },
1512 { "logfile", "F", do_logfile,
1513 "filename", "output logs to 'filename'" },
1514 { "log", "s", do_log,
1515 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1516 { "savevm", "s?", do_savevm,
1517 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1518 { "loadvm", "s", do_loadvm,
1519 "tag|id", "restore a VM snapshot from its tag or id" },
1520 { "delvm", "s", do_delvm,
1521 "tag|id", "delete a VM snapshot from its tag or id" },
1522 { "stop", "", do_stop,
1523 "", "stop emulation", },
1524 { "c|cont", "", do_cont,
1525 "", "resume emulation", },
1526 #ifdef CONFIG_GDBSTUB
1527 { "gdbserver", "s?", do_gdbserver,
1528 "[port]", "start gdbserver session (default port=1234)", },
1529 #endif
1530 { "x", "/l", do_memory_dump,
1531 "/fmt addr", "virtual memory dump starting at 'addr'", },
1532 { "xp", "/l", do_physical_memory_dump,
1533 "/fmt addr", "physical memory dump starting at 'addr'", },
1534 { "p|print", "/l", do_print,
1535 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1536 { "i", "/ii.", do_ioport_read,
1537 "/fmt addr", "I/O port read" },
1539 { "sendkey", "si?", do_sendkey,
1540 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1541 { "system_reset", "", do_system_reset,
1542 "", "reset the system" },
1543 { "system_powerdown", "", do_system_powerdown,
1544 "", "send system power down event" },
1545 { "sum", "ii", do_sum,
1546 "addr size", "compute the checksum of a memory region" },
1547 { "usb_add", "s", do_usb_add,
1548 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1549 { "usb_del", "s", do_usb_del,
1550 "device", "remove USB device 'bus.addr'" },
1551 { "cpu", "i", do_cpu_set,
1552 "index", "set the default CPU" },
1553 { "mouse_move", "sss?", do_mouse_move,
1554 "dx dy [dz]", "send mouse move events" },
1555 { "mouse_button", "i", do_mouse_button,
1556 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1557 { "mouse_set", "i", do_mouse_set,
1558 "index", "set which mouse device receives events" },
1559 #ifdef HAS_AUDIO
1560 { "wavcapture", "si?i?i?", do_wav_capture,
1561 "path [frequency bits channels]",
1562 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1563 #endif
1564 { "stopcapture", "i", do_stop_capture,
1565 "capture index", "stop capture" },
1566 { "memsave", "lis", do_memory_save,
1567 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1568 { "pmemsave", "lis", do_physical_memory_save,
1569 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1570 { "boot_set", "s", do_boot_set,
1571 "bootdevice", "define new values for the boot device list" },
1572 #if defined(TARGET_I386)
1573 { "nmi", "i", do_inject_nmi,
1574 "cpu", "inject an NMI on the given CPU", },
1575 #endif
1576 { "migrate", "-ds", do_migrate,
1577 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1578 { "migrate_cancel", "", do_migrate_cancel,
1579 "", "cancel the current VM migration" },
1580 { "migrate_set_speed", "s", do_migrate_set_speed,
1581 "value", "set maximum speed (in bytes) for migrations" },
1582 #if defined(TARGET_I386)
1583 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1584 "[file=file][,if=type][,bus=n]\n"
1585 "[,unit=m][,media=d][index=i]\n"
1586 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1587 "[snapshot=on|off][,cache=on|off]",
1588 "add drive to PCI storage controller" },
1589 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1590 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1591 { "host_net_add", "ss", net_host_device_add,
1592 "[tap,user,socket,vde] options", "add host VLAN client" },
1593 { "host_net_remove", "is", net_host_device_remove,
1594 "vlan_id name", "remove host VLAN client" },
1595 #endif
1596 { "balloon", "i", do_balloon,
1597 "target", "request VM to change it's memory allocation (in MB)" },
1598 { "set_link", "ss", do_set_link,
1599 "name [up|down]", "change the link status of a network adapter" },
1600 { NULL, NULL, },
1603 /* Please update qemu-doc.texi when adding or changing commands */
1604 static const term_cmd_t info_cmds[] = {
1605 { "version", "", do_info_version,
1606 "", "show the version of QEMU" },
1607 { "network", "", do_info_network,
1608 "", "show the network state" },
1609 { "chardev", "", qemu_chr_info,
1610 "", "show the character devices" },
1611 { "block", "", do_info_block,
1612 "", "show the block devices" },
1613 { "blockstats", "", do_info_blockstats,
1614 "", "show block device statistics" },
1615 { "registers", "", do_info_registers,
1616 "", "show the cpu registers" },
1617 { "cpus", "", do_info_cpus,
1618 "", "show infos for each CPU" },
1619 { "history", "", do_info_history,
1620 "", "show the command line history", },
1621 { "irq", "", irq_info,
1622 "", "show the interrupts statistics (if available)", },
1623 { "pic", "", pic_info,
1624 "", "show i8259 (PIC) state", },
1625 { "pci", "", pci_info,
1626 "", "show PCI info", },
1627 #if defined(TARGET_I386) || defined(TARGET_SH4)
1628 { "tlb", "", tlb_info,
1629 "", "show virtual to physical memory mappings", },
1630 #endif
1631 #if defined(TARGET_I386)
1632 { "mem", "", mem_info,
1633 "", "show the active virtual memory mappings", },
1634 { "hpet", "", do_info_hpet,
1635 "", "show state of HPET", },
1636 #endif
1637 { "jit", "", do_info_jit,
1638 "", "show dynamic compiler info", },
1639 { "kqemu", "", do_info_kqemu,
1640 "", "show KQEMU information", },
1641 { "kvm", "", do_info_kvm,
1642 "", "show KVM information", },
1643 { "usb", "", usb_info,
1644 "", "show guest USB devices", },
1645 { "usbhost", "", usb_host_info,
1646 "", "show host USB devices", },
1647 { "profile", "", do_info_profile,
1648 "", "show profiling information", },
1649 { "capture", "", do_info_capture,
1650 "", "show capture information" },
1651 { "snapshots", "", do_info_snapshots,
1652 "", "show the currently saved VM snapshots" },
1653 { "status", "", do_info_status,
1654 "", "show the current VM status (running|paused)" },
1655 { "pcmcia", "", pcmcia_info,
1656 "", "show guest PCMCIA status" },
1657 { "mice", "", do_info_mice,
1658 "", "show which guest mouse is receiving events" },
1659 { "vnc", "", do_info_vnc,
1660 "", "show the vnc server status"},
1661 { "name", "", do_info_name,
1662 "", "show the current VM name" },
1663 { "uuid", "", do_info_uuid,
1664 "", "show the current VM UUID" },
1665 #if defined(TARGET_PPC)
1666 { "cpustats", "", do_info_cpu_stats,
1667 "", "show CPU statistics", },
1668 #endif
1669 #if defined(CONFIG_SLIRP)
1670 { "slirp", "", do_info_slirp,
1671 "", "show SLIRP statistics", },
1672 #endif
1673 { "migrate", "", do_info_migrate, "", "show migration status" },
1674 { "balloon", "", do_info_balloon,
1675 "", "show balloon information" },
1676 { NULL, NULL, },
1679 /*******************************************************************/
1681 static const char *pch;
1682 static jmp_buf expr_env;
1684 #define MD_TLONG 0
1685 #define MD_I32 1
1687 typedef struct MonitorDef {
1688 const char *name;
1689 int offset;
1690 target_long (*get_value)(const struct MonitorDef *md, int val);
1691 int type;
1692 } MonitorDef;
1694 #if defined(TARGET_I386)
1695 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1697 CPUState *env = mon_get_cpu();
1698 if (!env)
1699 return 0;
1700 return env->eip + env->segs[R_CS].base;
1702 #endif
1704 #if defined(TARGET_PPC)
1705 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1707 CPUState *env = mon_get_cpu();
1708 unsigned int u;
1709 int i;
1711 if (!env)
1712 return 0;
1714 u = 0;
1715 for (i = 0; i < 8; i++)
1716 u |= env->crf[i] << (32 - (4 * i));
1718 return u;
1721 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1723 CPUState *env = mon_get_cpu();
1724 if (!env)
1725 return 0;
1726 return env->msr;
1729 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1731 CPUState *env = mon_get_cpu();
1732 if (!env)
1733 return 0;
1734 return env->xer;
1737 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1739 CPUState *env = mon_get_cpu();
1740 if (!env)
1741 return 0;
1742 return cpu_ppc_load_decr(env);
1745 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1747 CPUState *env = mon_get_cpu();
1748 if (!env)
1749 return 0;
1750 return cpu_ppc_load_tbu(env);
1753 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1755 CPUState *env = mon_get_cpu();
1756 if (!env)
1757 return 0;
1758 return cpu_ppc_load_tbl(env);
1760 #endif
1762 #if defined(TARGET_SPARC)
1763 #ifndef TARGET_SPARC64
1764 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1766 CPUState *env = mon_get_cpu();
1767 if (!env)
1768 return 0;
1769 return GET_PSR(env);
1771 #endif
1773 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1775 CPUState *env = mon_get_cpu();
1776 if (!env)
1777 return 0;
1778 return env->regwptr[val];
1780 #endif
1782 static const MonitorDef monitor_defs[] = {
1783 #ifdef TARGET_I386
1785 #define SEG(name, seg) \
1786 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1787 { name ".base", offsetof(CPUState, segs[seg].base) },\
1788 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1790 { "eax", offsetof(CPUState, regs[0]) },
1791 { "ecx", offsetof(CPUState, regs[1]) },
1792 { "edx", offsetof(CPUState, regs[2]) },
1793 { "ebx", offsetof(CPUState, regs[3]) },
1794 { "esp|sp", offsetof(CPUState, regs[4]) },
1795 { "ebp|fp", offsetof(CPUState, regs[5]) },
1796 { "esi", offsetof(CPUState, regs[6]) },
1797 { "edi", offsetof(CPUState, regs[7]) },
1798 #ifdef TARGET_X86_64
1799 { "r8", offsetof(CPUState, regs[8]) },
1800 { "r9", offsetof(CPUState, regs[9]) },
1801 { "r10", offsetof(CPUState, regs[10]) },
1802 { "r11", offsetof(CPUState, regs[11]) },
1803 { "r12", offsetof(CPUState, regs[12]) },
1804 { "r13", offsetof(CPUState, regs[13]) },
1805 { "r14", offsetof(CPUState, regs[14]) },
1806 { "r15", offsetof(CPUState, regs[15]) },
1807 #endif
1808 { "eflags", offsetof(CPUState, eflags) },
1809 { "eip", offsetof(CPUState, eip) },
1810 SEG("cs", R_CS)
1811 SEG("ds", R_DS)
1812 SEG("es", R_ES)
1813 SEG("ss", R_SS)
1814 SEG("fs", R_FS)
1815 SEG("gs", R_GS)
1816 { "pc", 0, monitor_get_pc, },
1817 #elif defined(TARGET_PPC)
1818 /* General purpose registers */
1819 { "r0", offsetof(CPUState, gpr[0]) },
1820 { "r1", offsetof(CPUState, gpr[1]) },
1821 { "r2", offsetof(CPUState, gpr[2]) },
1822 { "r3", offsetof(CPUState, gpr[3]) },
1823 { "r4", offsetof(CPUState, gpr[4]) },
1824 { "r5", offsetof(CPUState, gpr[5]) },
1825 { "r6", offsetof(CPUState, gpr[6]) },
1826 { "r7", offsetof(CPUState, gpr[7]) },
1827 { "r8", offsetof(CPUState, gpr[8]) },
1828 { "r9", offsetof(CPUState, gpr[9]) },
1829 { "r10", offsetof(CPUState, gpr[10]) },
1830 { "r11", offsetof(CPUState, gpr[11]) },
1831 { "r12", offsetof(CPUState, gpr[12]) },
1832 { "r13", offsetof(CPUState, gpr[13]) },
1833 { "r14", offsetof(CPUState, gpr[14]) },
1834 { "r15", offsetof(CPUState, gpr[15]) },
1835 { "r16", offsetof(CPUState, gpr[16]) },
1836 { "r17", offsetof(CPUState, gpr[17]) },
1837 { "r18", offsetof(CPUState, gpr[18]) },
1838 { "r19", offsetof(CPUState, gpr[19]) },
1839 { "r20", offsetof(CPUState, gpr[20]) },
1840 { "r21", offsetof(CPUState, gpr[21]) },
1841 { "r22", offsetof(CPUState, gpr[22]) },
1842 { "r23", offsetof(CPUState, gpr[23]) },
1843 { "r24", offsetof(CPUState, gpr[24]) },
1844 { "r25", offsetof(CPUState, gpr[25]) },
1845 { "r26", offsetof(CPUState, gpr[26]) },
1846 { "r27", offsetof(CPUState, gpr[27]) },
1847 { "r28", offsetof(CPUState, gpr[28]) },
1848 { "r29", offsetof(CPUState, gpr[29]) },
1849 { "r30", offsetof(CPUState, gpr[30]) },
1850 { "r31", offsetof(CPUState, gpr[31]) },
1851 /* Floating point registers */
1852 { "f0", offsetof(CPUState, fpr[0]) },
1853 { "f1", offsetof(CPUState, fpr[1]) },
1854 { "f2", offsetof(CPUState, fpr[2]) },
1855 { "f3", offsetof(CPUState, fpr[3]) },
1856 { "f4", offsetof(CPUState, fpr[4]) },
1857 { "f5", offsetof(CPUState, fpr[5]) },
1858 { "f6", offsetof(CPUState, fpr[6]) },
1859 { "f7", offsetof(CPUState, fpr[7]) },
1860 { "f8", offsetof(CPUState, fpr[8]) },
1861 { "f9", offsetof(CPUState, fpr[9]) },
1862 { "f10", offsetof(CPUState, fpr[10]) },
1863 { "f11", offsetof(CPUState, fpr[11]) },
1864 { "f12", offsetof(CPUState, fpr[12]) },
1865 { "f13", offsetof(CPUState, fpr[13]) },
1866 { "f14", offsetof(CPUState, fpr[14]) },
1867 { "f15", offsetof(CPUState, fpr[15]) },
1868 { "f16", offsetof(CPUState, fpr[16]) },
1869 { "f17", offsetof(CPUState, fpr[17]) },
1870 { "f18", offsetof(CPUState, fpr[18]) },
1871 { "f19", offsetof(CPUState, fpr[19]) },
1872 { "f20", offsetof(CPUState, fpr[20]) },
1873 { "f21", offsetof(CPUState, fpr[21]) },
1874 { "f22", offsetof(CPUState, fpr[22]) },
1875 { "f23", offsetof(CPUState, fpr[23]) },
1876 { "f24", offsetof(CPUState, fpr[24]) },
1877 { "f25", offsetof(CPUState, fpr[25]) },
1878 { "f26", offsetof(CPUState, fpr[26]) },
1879 { "f27", offsetof(CPUState, fpr[27]) },
1880 { "f28", offsetof(CPUState, fpr[28]) },
1881 { "f29", offsetof(CPUState, fpr[29]) },
1882 { "f30", offsetof(CPUState, fpr[30]) },
1883 { "f31", offsetof(CPUState, fpr[31]) },
1884 { "fpscr", offsetof(CPUState, fpscr) },
1885 /* Next instruction pointer */
1886 { "nip|pc", offsetof(CPUState, nip) },
1887 { "lr", offsetof(CPUState, lr) },
1888 { "ctr", offsetof(CPUState, ctr) },
1889 { "decr", 0, &monitor_get_decr, },
1890 { "ccr", 0, &monitor_get_ccr, },
1891 /* Machine state register */
1892 { "msr", 0, &monitor_get_msr, },
1893 { "xer", 0, &monitor_get_xer, },
1894 { "tbu", 0, &monitor_get_tbu, },
1895 { "tbl", 0, &monitor_get_tbl, },
1896 #if defined(TARGET_PPC64)
1897 /* Address space register */
1898 { "asr", offsetof(CPUState, asr) },
1899 #endif
1900 /* Segment registers */
1901 { "sdr1", offsetof(CPUState, sdr1) },
1902 { "sr0", offsetof(CPUState, sr[0]) },
1903 { "sr1", offsetof(CPUState, sr[1]) },
1904 { "sr2", offsetof(CPUState, sr[2]) },
1905 { "sr3", offsetof(CPUState, sr[3]) },
1906 { "sr4", offsetof(CPUState, sr[4]) },
1907 { "sr5", offsetof(CPUState, sr[5]) },
1908 { "sr6", offsetof(CPUState, sr[6]) },
1909 { "sr7", offsetof(CPUState, sr[7]) },
1910 { "sr8", offsetof(CPUState, sr[8]) },
1911 { "sr9", offsetof(CPUState, sr[9]) },
1912 { "sr10", offsetof(CPUState, sr[10]) },
1913 { "sr11", offsetof(CPUState, sr[11]) },
1914 { "sr12", offsetof(CPUState, sr[12]) },
1915 { "sr13", offsetof(CPUState, sr[13]) },
1916 { "sr14", offsetof(CPUState, sr[14]) },
1917 { "sr15", offsetof(CPUState, sr[15]) },
1918 /* Too lazy to put BATs and SPRs ... */
1919 #elif defined(TARGET_SPARC)
1920 { "g0", offsetof(CPUState, gregs[0]) },
1921 { "g1", offsetof(CPUState, gregs[1]) },
1922 { "g2", offsetof(CPUState, gregs[2]) },
1923 { "g3", offsetof(CPUState, gregs[3]) },
1924 { "g4", offsetof(CPUState, gregs[4]) },
1925 { "g5", offsetof(CPUState, gregs[5]) },
1926 { "g6", offsetof(CPUState, gregs[6]) },
1927 { "g7", offsetof(CPUState, gregs[7]) },
1928 { "o0", 0, monitor_get_reg },
1929 { "o1", 1, monitor_get_reg },
1930 { "o2", 2, monitor_get_reg },
1931 { "o3", 3, monitor_get_reg },
1932 { "o4", 4, monitor_get_reg },
1933 { "o5", 5, monitor_get_reg },
1934 { "o6", 6, monitor_get_reg },
1935 { "o7", 7, monitor_get_reg },
1936 { "l0", 8, monitor_get_reg },
1937 { "l1", 9, monitor_get_reg },
1938 { "l2", 10, monitor_get_reg },
1939 { "l3", 11, monitor_get_reg },
1940 { "l4", 12, monitor_get_reg },
1941 { "l5", 13, monitor_get_reg },
1942 { "l6", 14, monitor_get_reg },
1943 { "l7", 15, monitor_get_reg },
1944 { "i0", 16, monitor_get_reg },
1945 { "i1", 17, monitor_get_reg },
1946 { "i2", 18, monitor_get_reg },
1947 { "i3", 19, monitor_get_reg },
1948 { "i4", 20, monitor_get_reg },
1949 { "i5", 21, monitor_get_reg },
1950 { "i6", 22, monitor_get_reg },
1951 { "i7", 23, monitor_get_reg },
1952 { "pc", offsetof(CPUState, pc) },
1953 { "npc", offsetof(CPUState, npc) },
1954 { "y", offsetof(CPUState, y) },
1955 #ifndef TARGET_SPARC64
1956 { "psr", 0, &monitor_get_psr, },
1957 { "wim", offsetof(CPUState, wim) },
1958 #endif
1959 { "tbr", offsetof(CPUState, tbr) },
1960 { "fsr", offsetof(CPUState, fsr) },
1961 { "f0", offsetof(CPUState, fpr[0]) },
1962 { "f1", offsetof(CPUState, fpr[1]) },
1963 { "f2", offsetof(CPUState, fpr[2]) },
1964 { "f3", offsetof(CPUState, fpr[3]) },
1965 { "f4", offsetof(CPUState, fpr[4]) },
1966 { "f5", offsetof(CPUState, fpr[5]) },
1967 { "f6", offsetof(CPUState, fpr[6]) },
1968 { "f7", offsetof(CPUState, fpr[7]) },
1969 { "f8", offsetof(CPUState, fpr[8]) },
1970 { "f9", offsetof(CPUState, fpr[9]) },
1971 { "f10", offsetof(CPUState, fpr[10]) },
1972 { "f11", offsetof(CPUState, fpr[11]) },
1973 { "f12", offsetof(CPUState, fpr[12]) },
1974 { "f13", offsetof(CPUState, fpr[13]) },
1975 { "f14", offsetof(CPUState, fpr[14]) },
1976 { "f15", offsetof(CPUState, fpr[15]) },
1977 { "f16", offsetof(CPUState, fpr[16]) },
1978 { "f17", offsetof(CPUState, fpr[17]) },
1979 { "f18", offsetof(CPUState, fpr[18]) },
1980 { "f19", offsetof(CPUState, fpr[19]) },
1981 { "f20", offsetof(CPUState, fpr[20]) },
1982 { "f21", offsetof(CPUState, fpr[21]) },
1983 { "f22", offsetof(CPUState, fpr[22]) },
1984 { "f23", offsetof(CPUState, fpr[23]) },
1985 { "f24", offsetof(CPUState, fpr[24]) },
1986 { "f25", offsetof(CPUState, fpr[25]) },
1987 { "f26", offsetof(CPUState, fpr[26]) },
1988 { "f27", offsetof(CPUState, fpr[27]) },
1989 { "f28", offsetof(CPUState, fpr[28]) },
1990 { "f29", offsetof(CPUState, fpr[29]) },
1991 { "f30", offsetof(CPUState, fpr[30]) },
1992 { "f31", offsetof(CPUState, fpr[31]) },
1993 #ifdef TARGET_SPARC64
1994 { "f32", offsetof(CPUState, fpr[32]) },
1995 { "f34", offsetof(CPUState, fpr[34]) },
1996 { "f36", offsetof(CPUState, fpr[36]) },
1997 { "f38", offsetof(CPUState, fpr[38]) },
1998 { "f40", offsetof(CPUState, fpr[40]) },
1999 { "f42", offsetof(CPUState, fpr[42]) },
2000 { "f44", offsetof(CPUState, fpr[44]) },
2001 { "f46", offsetof(CPUState, fpr[46]) },
2002 { "f48", offsetof(CPUState, fpr[48]) },
2003 { "f50", offsetof(CPUState, fpr[50]) },
2004 { "f52", offsetof(CPUState, fpr[52]) },
2005 { "f54", offsetof(CPUState, fpr[54]) },
2006 { "f56", offsetof(CPUState, fpr[56]) },
2007 { "f58", offsetof(CPUState, fpr[58]) },
2008 { "f60", offsetof(CPUState, fpr[60]) },
2009 { "f62", offsetof(CPUState, fpr[62]) },
2010 { "asi", offsetof(CPUState, asi) },
2011 { "pstate", offsetof(CPUState, pstate) },
2012 { "cansave", offsetof(CPUState, cansave) },
2013 { "canrestore", offsetof(CPUState, canrestore) },
2014 { "otherwin", offsetof(CPUState, otherwin) },
2015 { "wstate", offsetof(CPUState, wstate) },
2016 { "cleanwin", offsetof(CPUState, cleanwin) },
2017 { "fprs", offsetof(CPUState, fprs) },
2018 #endif
2019 #endif
2020 { NULL },
2023 static void expr_error(const char *msg)
2025 term_printf("%s\n", msg);
2026 longjmp(expr_env, 1);
2029 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2030 static int get_monitor_def(target_long *pval, const char *name)
2032 const MonitorDef *md;
2033 void *ptr;
2035 for(md = monitor_defs; md->name != NULL; md++) {
2036 if (compare_cmd(name, md->name)) {
2037 if (md->get_value) {
2038 *pval = md->get_value(md, md->offset);
2039 } else {
2040 CPUState *env = mon_get_cpu();
2041 if (!env)
2042 return -2;
2043 ptr = (uint8_t *)env + md->offset;
2044 switch(md->type) {
2045 case MD_I32:
2046 *pval = *(int32_t *)ptr;
2047 break;
2048 case MD_TLONG:
2049 *pval = *(target_long *)ptr;
2050 break;
2051 default:
2052 *pval = 0;
2053 break;
2056 return 0;
2059 return -1;
2062 static void next(void)
2064 if (pch != '\0') {
2065 pch++;
2066 while (qemu_isspace(*pch))
2067 pch++;
2071 static int64_t expr_sum(void);
2073 static int64_t expr_unary(void)
2075 int64_t n;
2076 char *p;
2077 int ret;
2079 switch(*pch) {
2080 case '+':
2081 next();
2082 n = expr_unary();
2083 break;
2084 case '-':
2085 next();
2086 n = -expr_unary();
2087 break;
2088 case '~':
2089 next();
2090 n = ~expr_unary();
2091 break;
2092 case '(':
2093 next();
2094 n = expr_sum();
2095 if (*pch != ')') {
2096 expr_error("')' expected");
2098 next();
2099 break;
2100 case '\'':
2101 pch++;
2102 if (*pch == '\0')
2103 expr_error("character constant expected");
2104 n = *pch;
2105 pch++;
2106 if (*pch != '\'')
2107 expr_error("missing terminating \' character");
2108 next();
2109 break;
2110 case '$':
2112 char buf[128], *q;
2113 target_long reg=0;
2115 pch++;
2116 q = buf;
2117 while ((*pch >= 'a' && *pch <= 'z') ||
2118 (*pch >= 'A' && *pch <= 'Z') ||
2119 (*pch >= '0' && *pch <= '9') ||
2120 *pch == '_' || *pch == '.') {
2121 if ((q - buf) < sizeof(buf) - 1)
2122 *q++ = *pch;
2123 pch++;
2125 while (qemu_isspace(*pch))
2126 pch++;
2127 *q = 0;
2128 ret = get_monitor_def(&reg, buf);
2129 if (ret == -1)
2130 expr_error("unknown register");
2131 else if (ret == -2)
2132 expr_error("no cpu defined");
2133 n = reg;
2135 break;
2136 case '\0':
2137 expr_error("unexpected end of expression");
2138 n = 0;
2139 break;
2140 default:
2141 #if TARGET_PHYS_ADDR_BITS > 32
2142 n = strtoull(pch, &p, 0);
2143 #else
2144 n = strtoul(pch, &p, 0);
2145 #endif
2146 if (pch == p) {
2147 expr_error("invalid char in expression");
2149 pch = p;
2150 while (qemu_isspace(*pch))
2151 pch++;
2152 break;
2154 return n;
2158 static int64_t expr_prod(void)
2160 int64_t val, val2;
2161 int op;
2163 val = expr_unary();
2164 for(;;) {
2165 op = *pch;
2166 if (op != '*' && op != '/' && op != '%')
2167 break;
2168 next();
2169 val2 = expr_unary();
2170 switch(op) {
2171 default:
2172 case '*':
2173 val *= val2;
2174 break;
2175 case '/':
2176 case '%':
2177 if (val2 == 0)
2178 expr_error("division by zero");
2179 if (op == '/')
2180 val /= val2;
2181 else
2182 val %= val2;
2183 break;
2186 return val;
2189 static int64_t expr_logic(void)
2191 int64_t val, val2;
2192 int op;
2194 val = expr_prod();
2195 for(;;) {
2196 op = *pch;
2197 if (op != '&' && op != '|' && op != '^')
2198 break;
2199 next();
2200 val2 = expr_prod();
2201 switch(op) {
2202 default:
2203 case '&':
2204 val &= val2;
2205 break;
2206 case '|':
2207 val |= val2;
2208 break;
2209 case '^':
2210 val ^= val2;
2211 break;
2214 return val;
2217 static int64_t expr_sum(void)
2219 int64_t val, val2;
2220 int op;
2222 val = expr_logic();
2223 for(;;) {
2224 op = *pch;
2225 if (op != '+' && op != '-')
2226 break;
2227 next();
2228 val2 = expr_logic();
2229 if (op == '+')
2230 val += val2;
2231 else
2232 val -= val2;
2234 return val;
2237 static int get_expr(int64_t *pval, const char **pp)
2239 pch = *pp;
2240 if (setjmp(expr_env)) {
2241 *pp = pch;
2242 return -1;
2244 while (qemu_isspace(*pch))
2245 pch++;
2246 *pval = expr_sum();
2247 *pp = pch;
2248 return 0;
2251 static int get_str(char *buf, int buf_size, const char **pp)
2253 const char *p;
2254 char *q;
2255 int c;
2257 q = buf;
2258 p = *pp;
2259 while (qemu_isspace(*p))
2260 p++;
2261 if (*p == '\0') {
2262 fail:
2263 *q = '\0';
2264 *pp = p;
2265 return -1;
2267 if (*p == '\"') {
2268 p++;
2269 while (*p != '\0' && *p != '\"') {
2270 if (*p == '\\') {
2271 p++;
2272 c = *p++;
2273 switch(c) {
2274 case 'n':
2275 c = '\n';
2276 break;
2277 case 'r':
2278 c = '\r';
2279 break;
2280 case '\\':
2281 case '\'':
2282 case '\"':
2283 break;
2284 default:
2285 qemu_printf("unsupported escape code: '\\%c'\n", c);
2286 goto fail;
2288 if ((q - buf) < buf_size - 1) {
2289 *q++ = c;
2291 } else {
2292 if ((q - buf) < buf_size - 1) {
2293 *q++ = *p;
2295 p++;
2298 if (*p != '\"') {
2299 qemu_printf("unterminated string\n");
2300 goto fail;
2302 p++;
2303 } else {
2304 while (*p != '\0' && !qemu_isspace(*p)) {
2305 if ((q - buf) < buf_size - 1) {
2306 *q++ = *p;
2308 p++;
2311 *q = '\0';
2312 *pp = p;
2313 return 0;
2316 static int default_fmt_format = 'x';
2317 static int default_fmt_size = 4;
2319 #define MAX_ARGS 16
2321 static void monitor_handle_command(const char *cmdline)
2323 const char *p, *pstart, *typestr;
2324 char *q;
2325 int c, nb_args, len, i, has_arg;
2326 const term_cmd_t *cmd;
2327 char cmdname[256];
2328 char buf[1024];
2329 void *str_allocated[MAX_ARGS];
2330 void *args[MAX_ARGS];
2331 void (*handler_0)(void);
2332 void (*handler_1)(void *arg0);
2333 void (*handler_2)(void *arg0, void *arg1);
2334 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2335 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2336 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2337 void *arg4);
2338 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2339 void *arg4, void *arg5);
2340 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2341 void *arg4, void *arg5, void *arg6);
2343 #ifdef DEBUG
2344 term_printf("command='%s'\n", cmdline);
2345 #endif
2347 /* extract the command name */
2348 p = cmdline;
2349 q = cmdname;
2350 while (qemu_isspace(*p))
2351 p++;
2352 if (*p == '\0')
2353 return;
2354 pstart = p;
2355 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2356 p++;
2357 len = p - pstart;
2358 if (len > sizeof(cmdname) - 1)
2359 len = sizeof(cmdname) - 1;
2360 memcpy(cmdname, pstart, len);
2361 cmdname[len] = '\0';
2363 /* find the command */
2364 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2365 if (compare_cmd(cmdname, cmd->name))
2366 goto found;
2368 term_printf("unknown command: '%s'\n", cmdname);
2369 return;
2370 found:
2372 for(i = 0; i < MAX_ARGS; i++)
2373 str_allocated[i] = NULL;
2375 /* parse the parameters */
2376 typestr = cmd->args_type;
2377 nb_args = 0;
2378 for(;;) {
2379 c = *typestr;
2380 if (c == '\0')
2381 break;
2382 typestr++;
2383 switch(c) {
2384 case 'F':
2385 case 'B':
2386 case 's':
2388 int ret;
2389 char *str;
2391 while (qemu_isspace(*p))
2392 p++;
2393 if (*typestr == '?') {
2394 typestr++;
2395 if (*p == '\0') {
2396 /* no optional string: NULL argument */
2397 str = NULL;
2398 goto add_str;
2401 ret = get_str(buf, sizeof(buf), &p);
2402 if (ret < 0) {
2403 switch(c) {
2404 case 'F':
2405 term_printf("%s: filename expected\n", cmdname);
2406 break;
2407 case 'B':
2408 term_printf("%s: block device name expected\n", cmdname);
2409 break;
2410 default:
2411 term_printf("%s: string expected\n", cmdname);
2412 break;
2414 goto fail;
2416 str = qemu_malloc(strlen(buf) + 1);
2417 pstrcpy(str, sizeof(buf), buf);
2418 str_allocated[nb_args] = str;
2419 add_str:
2420 if (nb_args >= MAX_ARGS) {
2421 error_args:
2422 term_printf("%s: too many arguments\n", cmdname);
2423 goto fail;
2425 args[nb_args++] = str;
2427 break;
2428 case '/':
2430 int count, format, size;
2432 while (qemu_isspace(*p))
2433 p++;
2434 if (*p == '/') {
2435 /* format found */
2436 p++;
2437 count = 1;
2438 if (qemu_isdigit(*p)) {
2439 count = 0;
2440 while (qemu_isdigit(*p)) {
2441 count = count * 10 + (*p - '0');
2442 p++;
2445 size = -1;
2446 format = -1;
2447 for(;;) {
2448 switch(*p) {
2449 case 'o':
2450 case 'd':
2451 case 'u':
2452 case 'x':
2453 case 'i':
2454 case 'c':
2455 format = *p++;
2456 break;
2457 case 'b':
2458 size = 1;
2459 p++;
2460 break;
2461 case 'h':
2462 size = 2;
2463 p++;
2464 break;
2465 case 'w':
2466 size = 4;
2467 p++;
2468 break;
2469 case 'g':
2470 case 'L':
2471 size = 8;
2472 p++;
2473 break;
2474 default:
2475 goto next;
2478 next:
2479 if (*p != '\0' && !qemu_isspace(*p)) {
2480 term_printf("invalid char in format: '%c'\n", *p);
2481 goto fail;
2483 if (format < 0)
2484 format = default_fmt_format;
2485 if (format != 'i') {
2486 /* for 'i', not specifying a size gives -1 as size */
2487 if (size < 0)
2488 size = default_fmt_size;
2489 default_fmt_size = size;
2491 default_fmt_format = format;
2492 } else {
2493 count = 1;
2494 format = default_fmt_format;
2495 if (format != 'i') {
2496 size = default_fmt_size;
2497 } else {
2498 size = -1;
2501 if (nb_args + 3 > MAX_ARGS)
2502 goto error_args;
2503 args[nb_args++] = (void*)(long)count;
2504 args[nb_args++] = (void*)(long)format;
2505 args[nb_args++] = (void*)(long)size;
2507 break;
2508 case 'i':
2509 case 'l':
2511 int64_t val;
2513 while (qemu_isspace(*p))
2514 p++;
2515 if (*typestr == '?' || *typestr == '.') {
2516 if (*typestr == '?') {
2517 if (*p == '\0')
2518 has_arg = 0;
2519 else
2520 has_arg = 1;
2521 } else {
2522 if (*p == '.') {
2523 p++;
2524 while (qemu_isspace(*p))
2525 p++;
2526 has_arg = 1;
2527 } else {
2528 has_arg = 0;
2531 typestr++;
2532 if (nb_args >= MAX_ARGS)
2533 goto error_args;
2534 args[nb_args++] = (void *)(long)has_arg;
2535 if (!has_arg) {
2536 if (nb_args >= MAX_ARGS)
2537 goto error_args;
2538 val = -1;
2539 goto add_num;
2542 if (get_expr(&val, &p))
2543 goto fail;
2544 add_num:
2545 if (c == 'i') {
2546 if (nb_args >= MAX_ARGS)
2547 goto error_args;
2548 args[nb_args++] = (void *)(long)val;
2549 } else {
2550 if ((nb_args + 1) >= MAX_ARGS)
2551 goto error_args;
2552 #if TARGET_PHYS_ADDR_BITS > 32
2553 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2554 #else
2555 args[nb_args++] = (void *)0;
2556 #endif
2557 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2560 break;
2561 case '-':
2563 int has_option;
2564 /* option */
2566 c = *typestr++;
2567 if (c == '\0')
2568 goto bad_type;
2569 while (qemu_isspace(*p))
2570 p++;
2571 has_option = 0;
2572 if (*p == '-') {
2573 p++;
2574 if (*p != c) {
2575 term_printf("%s: unsupported option -%c\n",
2576 cmdname, *p);
2577 goto fail;
2579 p++;
2580 has_option = 1;
2582 if (nb_args >= MAX_ARGS)
2583 goto error_args;
2584 args[nb_args++] = (void *)(long)has_option;
2586 break;
2587 default:
2588 bad_type:
2589 term_printf("%s: unknown type '%c'\n", cmdname, c);
2590 goto fail;
2593 /* check that all arguments were parsed */
2594 while (qemu_isspace(*p))
2595 p++;
2596 if (*p != '\0') {
2597 term_printf("%s: extraneous characters at the end of line\n",
2598 cmdname);
2599 goto fail;
2602 switch(nb_args) {
2603 case 0:
2604 handler_0 = cmd->handler;
2605 handler_0();
2606 break;
2607 case 1:
2608 handler_1 = cmd->handler;
2609 handler_1(args[0]);
2610 break;
2611 case 2:
2612 handler_2 = cmd->handler;
2613 handler_2(args[0], args[1]);
2614 break;
2615 case 3:
2616 handler_3 = cmd->handler;
2617 handler_3(args[0], args[1], args[2]);
2618 break;
2619 case 4:
2620 handler_4 = cmd->handler;
2621 handler_4(args[0], args[1], args[2], args[3]);
2622 break;
2623 case 5:
2624 handler_5 = cmd->handler;
2625 handler_5(args[0], args[1], args[2], args[3], args[4]);
2626 break;
2627 case 6:
2628 handler_6 = cmd->handler;
2629 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2630 break;
2631 case 7:
2632 handler_7 = cmd->handler;
2633 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2634 break;
2635 default:
2636 term_printf("unsupported number of arguments: %d\n", nb_args);
2637 goto fail;
2639 fail:
2640 for(i = 0; i < MAX_ARGS; i++)
2641 qemu_free(str_allocated[i]);
2642 return;
2645 static void cmd_completion(const char *name, const char *list)
2647 const char *p, *pstart;
2648 char cmd[128];
2649 int len;
2651 p = list;
2652 for(;;) {
2653 pstart = p;
2654 p = strchr(p, '|');
2655 if (!p)
2656 p = pstart + strlen(pstart);
2657 len = p - pstart;
2658 if (len > sizeof(cmd) - 2)
2659 len = sizeof(cmd) - 2;
2660 memcpy(cmd, pstart, len);
2661 cmd[len] = '\0';
2662 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2663 add_completion(cmd);
2665 if (*p == '\0')
2666 break;
2667 p++;
2671 static void file_completion(const char *input)
2673 DIR *ffs;
2674 struct dirent *d;
2675 char path[1024];
2676 char file[1024], file_prefix[1024];
2677 int input_path_len;
2678 const char *p;
2680 p = strrchr(input, '/');
2681 if (!p) {
2682 input_path_len = 0;
2683 pstrcpy(file_prefix, sizeof(file_prefix), input);
2684 pstrcpy(path, sizeof(path), ".");
2685 } else {
2686 input_path_len = p - input + 1;
2687 memcpy(path, input, input_path_len);
2688 if (input_path_len > sizeof(path) - 1)
2689 input_path_len = sizeof(path) - 1;
2690 path[input_path_len] = '\0';
2691 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2693 #ifdef DEBUG_COMPLETION
2694 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2695 #endif
2696 ffs = opendir(path);
2697 if (!ffs)
2698 return;
2699 for(;;) {
2700 struct stat sb;
2701 d = readdir(ffs);
2702 if (!d)
2703 break;
2704 if (strstart(d->d_name, file_prefix, NULL)) {
2705 memcpy(file, input, input_path_len);
2706 if (input_path_len < sizeof(file))
2707 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2708 d->d_name);
2709 /* stat the file to find out if it's a directory.
2710 * In that case add a slash to speed up typing long paths
2712 stat(file, &sb);
2713 if(S_ISDIR(sb.st_mode))
2714 pstrcat(file, sizeof(file), "/");
2715 add_completion(file);
2718 closedir(ffs);
2721 static void block_completion_it(void *opaque, BlockDriverState *bs)
2723 const char *name = bdrv_get_device_name(bs);
2724 const char *input = opaque;
2726 if (input[0] == '\0' ||
2727 !strncmp(name, (char *)input, strlen(input))) {
2728 add_completion(name);
2732 /* NOTE: this parser is an approximate form of the real command parser */
2733 static void parse_cmdline(const char *cmdline,
2734 int *pnb_args, char **args)
2736 const char *p;
2737 int nb_args, ret;
2738 char buf[1024];
2740 p = cmdline;
2741 nb_args = 0;
2742 for(;;) {
2743 while (qemu_isspace(*p))
2744 p++;
2745 if (*p == '\0')
2746 break;
2747 if (nb_args >= MAX_ARGS)
2748 break;
2749 ret = get_str(buf, sizeof(buf), &p);
2750 args[nb_args] = qemu_strdup(buf);
2751 nb_args++;
2752 if (ret < 0)
2753 break;
2755 *pnb_args = nb_args;
2758 void readline_find_completion(const char *cmdline)
2760 const char *cmdname;
2761 char *args[MAX_ARGS];
2762 int nb_args, i, len;
2763 const char *ptype, *str;
2764 const term_cmd_t *cmd;
2765 const KeyDef *key;
2767 parse_cmdline(cmdline, &nb_args, args);
2768 #ifdef DEBUG_COMPLETION
2769 for(i = 0; i < nb_args; i++) {
2770 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2772 #endif
2774 /* if the line ends with a space, it means we want to complete the
2775 next arg */
2776 len = strlen(cmdline);
2777 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2778 if (nb_args >= MAX_ARGS)
2779 return;
2780 args[nb_args++] = qemu_strdup("");
2782 if (nb_args <= 1) {
2783 /* command completion */
2784 if (nb_args == 0)
2785 cmdname = "";
2786 else
2787 cmdname = args[0];
2788 completion_index = strlen(cmdname);
2789 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2790 cmd_completion(cmdname, cmd->name);
2792 } else {
2793 /* find the command */
2794 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2795 if (compare_cmd(args[0], cmd->name))
2796 goto found;
2798 return;
2799 found:
2800 ptype = cmd->args_type;
2801 for(i = 0; i < nb_args - 2; i++) {
2802 if (*ptype != '\0') {
2803 ptype++;
2804 while (*ptype == '?')
2805 ptype++;
2808 str = args[nb_args - 1];
2809 switch(*ptype) {
2810 case 'F':
2811 /* file completion */
2812 completion_index = strlen(str);
2813 file_completion(str);
2814 break;
2815 case 'B':
2816 /* block device name completion */
2817 completion_index = strlen(str);
2818 bdrv_iterate(block_completion_it, (void *)str);
2819 break;
2820 case 's':
2821 /* XXX: more generic ? */
2822 if (!strcmp(cmd->name, "info")) {
2823 completion_index = strlen(str);
2824 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2825 cmd_completion(str, cmd->name);
2827 } else if (!strcmp(cmd->name, "sendkey")) {
2828 completion_index = strlen(str);
2829 for(key = key_defs; key->name != NULL; key++) {
2830 cmd_completion(str, key->name);
2833 break;
2834 default:
2835 break;
2838 for(i = 0; i < nb_args; i++)
2839 qemu_free(args[i]);
2842 static int term_can_read(void *opaque)
2844 return 128;
2847 static void term_read(void *opaque, const uint8_t *buf, int size)
2849 int i;
2850 for(i = 0; i < size; i++)
2851 readline_handle_byte(buf[i]);
2854 static int monitor_suspended;
2856 static void monitor_handle_command1(void *opaque, const char *cmdline)
2858 monitor_handle_command(cmdline);
2859 if (!monitor_suspended)
2860 readline_show_prompt();
2861 else
2862 monitor_suspended = 2;
2865 void monitor_suspend(void)
2867 monitor_suspended = 1;
2870 void monitor_resume(void)
2872 if (monitor_suspended == 2)
2873 monitor_start_input();
2874 monitor_suspended = 0;
2877 static void monitor_start_input(void)
2879 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2880 readline_show_prompt();
2883 static void term_event(void *opaque, int event)
2885 if (event != CHR_EVENT_RESET)
2886 return;
2888 if (!hide_banner)
2889 term_printf("QEMU %s monitor - type 'help' for more information\n",
2890 QEMU_VERSION);
2891 monitor_start_input();
2894 static int is_first_init = 1;
2896 void monitor_init(CharDriverState *hd, int show_banner)
2898 int i;
2900 if (is_first_init) {
2901 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2902 if (!key_timer)
2903 return;
2904 for (i = 0; i < MAX_MON; i++) {
2905 monitor_hd[i] = NULL;
2907 is_first_init = 0;
2909 for (i = 0; i < MAX_MON; i++) {
2910 if (monitor_hd[i] == NULL) {
2911 monitor_hd[i] = hd;
2912 break;
2916 hide_banner = !show_banner;
2918 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2920 readline_start("", 0, monitor_handle_command1, NULL);
2923 static void bdrv_password_cb(void *opaque, const char *password)
2925 BlockDriverState *bs = opaque;
2926 int ret = 0;
2928 if (bdrv_set_key(bs, password) != 0) {
2929 term_printf("invalid password\n");
2930 ret = -EPERM;
2932 if (password_completion_cb)
2933 password_completion_cb(password_opaque, ret);
2935 monitor_start_input();
2938 void monitor_read_bdrv_key_start(BlockDriverState *bs,
2939 BlockDriverCompletionFunc *completion_cb,
2940 void *opaque)
2942 if (!bdrv_key_required(bs)) {
2943 if (completion_cb)
2944 completion_cb(opaque, 0);
2945 return;
2948 term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2949 bdrv_get_encrypted_filename(bs));
2951 password_completion_cb = completion_cb;
2952 password_opaque = opaque;
2954 monitor_read_password(bdrv_password_cb, bs);