kvm: external module: add msr-index.h, now needed by kvm_host.h
[qemu-kvm/fedora.git] / monitor.c
blob3948aae3ef3ce28ec441710bc19a3a00cb31357b
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw/hw.h"
25 #include "hw/usb.h"
26 #include "hw/pcmcia.h"
27 #include "hw/pc.h"
28 #include "hw/pci.h"
29 #include "gdbstub.h"
30 #include "net.h"
31 #include "qemu-char.h"
32 #include "sysemu.h"
33 #include "console.h"
34 #include "block.h"
35 #include "audio/audio.h"
36 #include "disas.h"
37 #include "balloon.h"
38 #include <dirent.h>
39 #include "qemu-timer.h"
40 #include "migration.h"
41 #include "kvm.h"
43 #include "qemu-kvm.h"
45 //#define DEBUG
46 //#define DEBUG_COMPLETION
49 * Supported types:
51 * 'F' filename
52 * 'B' block device name
53 * 's' string (accept optional quote)
54 * 'i' 32 bit integer
55 * 'l' target long (32 or 64 bit)
56 * '/' optional gdb-like print format (like "/10x")
58 * '?' optional type (for 'F', 's' and 'i')
62 typedef struct term_cmd_t {
63 const char *name;
64 const char *args_type;
65 void *handler;
66 const char *params;
67 const char *help;
68 } term_cmd_t;
70 #define MAX_MON 4
71 static CharDriverState *monitor_hd[MAX_MON];
72 static int hide_banner;
74 static const term_cmd_t term_cmds[];
75 static const term_cmd_t info_cmds[];
77 static uint8_t term_outbuf[1024];
78 static int term_outbuf_index;
80 static void monitor_start_input(void);
82 static CPUState *mon_cpu = NULL;
84 void term_flush(void)
86 int i;
87 if (term_outbuf_index > 0) {
88 for (i = 0; i < MAX_MON; i++)
89 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
90 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
91 term_outbuf_index = 0;
95 /* flush at every end of line or if the buffer is full */
96 void term_puts(const char *str)
98 char c;
99 for(;;) {
100 c = *str++;
101 if (c == '\0')
102 break;
103 if (c == '\n')
104 term_outbuf[term_outbuf_index++] = '\r';
105 term_outbuf[term_outbuf_index++] = c;
106 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
107 c == '\n')
108 term_flush();
112 void term_vprintf(const char *fmt, va_list ap)
114 char buf[4096];
115 vsnprintf(buf, sizeof(buf), fmt, ap);
116 term_puts(buf);
119 void term_printf(const char *fmt, ...)
121 va_list ap;
122 va_start(ap, fmt);
123 term_vprintf(fmt, ap);
124 va_end(ap);
127 void term_print_filename(const char *filename)
129 int i;
131 for (i = 0; filename[i]; i++) {
132 switch (filename[i]) {
133 case ' ':
134 case '"':
135 case '\\':
136 term_printf("\\%c", filename[i]);
137 break;
138 case '\t':
139 term_printf("\\t");
140 break;
141 case '\r':
142 term_printf("\\r");
143 break;
144 case '\n':
145 term_printf("\\n");
146 break;
147 default:
148 term_printf("%c", filename[i]);
149 break;
154 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
156 va_list ap;
157 va_start(ap, fmt);
158 term_vprintf(fmt, ap);
159 va_end(ap);
160 return 0;
163 static int compare_cmd(const char *name, const char *list)
165 const char *p, *pstart;
166 int len;
167 len = strlen(name);
168 p = list;
169 for(;;) {
170 pstart = p;
171 p = strchr(p, '|');
172 if (!p)
173 p = pstart + strlen(pstart);
174 if ((p - pstart) == len && !memcmp(pstart, name, len))
175 return 1;
176 if (*p == '\0')
177 break;
178 p++;
180 return 0;
183 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
185 const term_cmd_t *cmd;
187 for(cmd = cmds; cmd->name != NULL; cmd++) {
188 if (!name || !strcmp(name, cmd->name))
189 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
193 static void help_cmd(const char *name)
195 if (name && !strcmp(name, "info")) {
196 help_cmd1(info_cmds, "info ", NULL);
197 } else {
198 help_cmd1(term_cmds, "", name);
199 if (name && !strcmp(name, "log")) {
200 const CPULogItem *item;
201 term_printf("Log items (comma separated):\n");
202 term_printf("%-10s %s\n", "none", "remove all logs");
203 for(item = cpu_log_items; item->mask != 0; item++) {
204 term_printf("%-10s %s\n", item->name, item->help);
210 static void do_help(const char *name)
212 help_cmd(name);
215 static void do_commit(const char *device)
217 int i, all_devices;
219 all_devices = !strcmp(device, "all");
220 for (i = 0; i < nb_drives; i++) {
221 if (all_devices ||
222 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
223 bdrv_commit(drives_table[i].bdrv);
227 static void do_info(const char *item)
229 const term_cmd_t *cmd;
230 void (*handler)(void);
232 if (!item)
233 goto help;
234 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
235 if (compare_cmd(item, cmd->name))
236 goto found;
238 help:
239 help_cmd("info");
240 return;
241 found:
242 handler = cmd->handler;
243 handler();
246 static void do_info_version(void)
248 term_printf("%s\n", QEMU_VERSION);
251 static void do_info_name(void)
253 if (qemu_name)
254 term_printf("%s\n", qemu_name);
257 static void do_info_uuid(void)
259 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
260 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
261 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
262 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
263 qemu_uuid[15]);
266 static void do_info_block(void)
268 bdrv_info();
271 static void do_info_blockstats(void)
273 bdrv_info_stats();
276 /* get the current CPU defined by the user */
277 static int mon_set_cpu(int cpu_index)
279 CPUState *env;
281 for(env = first_cpu; env != NULL; env = env->next_cpu) {
282 if (env->cpu_index == cpu_index) {
283 mon_cpu = env;
284 return 0;
287 return -1;
290 static CPUState *mon_get_cpu(void)
292 if (!mon_cpu) {
293 mon_set_cpu(0);
296 kvm_save_registers(mon_cpu);
298 return mon_cpu;
301 static void do_info_registers(void)
303 CPUState *env;
304 env = mon_get_cpu();
305 if (!env)
306 return;
307 #ifdef TARGET_I386
308 cpu_dump_state(env, NULL, monitor_fprintf,
309 X86_DUMP_FPU);
310 #else
311 cpu_dump_state(env, NULL, monitor_fprintf,
313 #endif
316 static void do_info_cpus(void)
318 CPUState *env;
320 /* just to set the default cpu if not already done */
321 mon_get_cpu();
323 for(env = first_cpu; env != NULL; env = env->next_cpu) {
324 kvm_save_registers(env);
325 term_printf("%c CPU #%d:",
326 (env == mon_cpu) ? '*' : ' ',
327 env->cpu_index);
328 #if defined(TARGET_I386)
329 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
330 #elif defined(TARGET_PPC)
331 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
332 #elif defined(TARGET_SPARC)
333 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
334 #elif defined(TARGET_MIPS)
335 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
336 #endif
337 if (env->halted)
338 term_printf(" (halted)");
339 term_printf(" thread_id=%d", env->thread_id);
340 term_printf("\n");
344 static void do_cpu_set(int index)
346 if (mon_set_cpu(index) < 0)
347 term_printf("Invalid CPU index\n");
350 static void do_cpu_set_nr(int value, const char *status)
352 int state;
354 if (!strcmp(status, "online"))
355 state = 1;
356 else if (!strcmp(status, "offline"))
357 state = 0;
358 else {
359 term_printf("invalid status: %s\n", status);
360 return;
362 #if defined(TARGET_I386) || defined(TARGET_X86_64)
363 qemu_system_cpu_hot_add(value, state);
364 #endif
367 static void do_info_jit(void)
369 dump_exec_info(NULL, monitor_fprintf);
372 static void do_info_history (void)
374 int i;
375 const char *str;
377 i = 0;
378 for(;;) {
379 str = readline_get_history(i);
380 if (!str)
381 break;
382 term_printf("%d: '%s'\n", i, str);
383 i++;
387 #if defined(TARGET_PPC)
388 /* XXX: not implemented in other targets */
389 static void do_info_cpu_stats (void)
391 CPUState *env;
393 env = mon_get_cpu();
394 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
396 #endif
398 static void do_quit(void)
400 exit(0);
403 static int eject_device(BlockDriverState *bs, int force)
405 if (bdrv_is_inserted(bs)) {
406 if (!force) {
407 if (!bdrv_is_removable(bs)) {
408 term_printf("device is not removable\n");
409 return -1;
411 if (bdrv_is_locked(bs)) {
412 term_printf("device is locked\n");
413 return -1;
416 bdrv_close(bs);
418 return 0;
421 static void do_eject(int force, const char *filename)
423 BlockDriverState *bs;
425 bs = bdrv_find(filename);
426 if (!bs) {
427 term_printf("device not found\n");
428 return;
430 eject_device(bs, force);
433 static void do_change_block(const char *device, const char *filename, const char *fmt)
435 BlockDriverState *bs;
436 BlockDriver *drv = NULL;
438 bs = bdrv_find(device);
439 if (!bs) {
440 term_printf("device not found\n");
441 return;
443 if (fmt) {
444 drv = bdrv_find_format(fmt);
445 if (!drv) {
446 term_printf("invalid format %s\n", fmt);
447 return;
450 if (eject_device(bs, 0) < 0)
451 return;
452 bdrv_open2(bs, filename, 0, drv);
453 qemu_key_check(bs, filename);
456 static void do_change_vnc(const char *target)
458 if (strcmp(target, "passwd") == 0 ||
459 strcmp(target, "password") == 0) {
460 char password[9];
461 monitor_readline("Password: ", 1, password, sizeof(password)-1);
462 password[sizeof(password)-1] = '\0';
463 if (vnc_display_password(NULL, password) < 0)
464 term_printf("could not set VNC server password\n");
465 } else {
466 if (vnc_display_open(NULL, target) < 0)
467 term_printf("could not start VNC server on %s\n", target);
471 static void do_change(const char *device, const char *target, const char *fmt)
473 if (strcmp(device, "vnc") == 0) {
474 do_change_vnc(target);
475 } else {
476 do_change_block(device, target, fmt);
480 static void do_screen_dump(const char *filename)
482 vga_hw_screen_dump(filename);
485 static void do_logfile(const char *filename)
487 cpu_set_log_filename(filename);
490 static void do_log(const char *items)
492 int mask;
494 if (!strcmp(items, "none")) {
495 mask = 0;
496 } else {
497 mask = cpu_str_to_log_mask(items);
498 if (!mask) {
499 help_cmd("log");
500 return;
503 cpu_set_log(mask);
506 static void do_stop(void)
508 vm_stop(EXCP_INTERRUPT);
511 static void do_cont(void)
513 vm_start();
516 #ifdef CONFIG_GDBSTUB
517 static void do_gdbserver(const char *port)
519 if (!port)
520 port = DEFAULT_GDBSTUB_PORT;
521 if (gdbserver_start(port) < 0) {
522 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
523 } else {
524 qemu_printf("Waiting gdb connection on port '%s'\n", port);
527 #endif
529 static void term_printc(int c)
531 term_printf("'");
532 switch(c) {
533 case '\'':
534 term_printf("\\'");
535 break;
536 case '\\':
537 term_printf("\\\\");
538 break;
539 case '\n':
540 term_printf("\\n");
541 break;
542 case '\r':
543 term_printf("\\r");
544 break;
545 default:
546 if (c >= 32 && c <= 126) {
547 term_printf("%c", c);
548 } else {
549 term_printf("\\x%02x", c);
551 break;
553 term_printf("'");
556 static void memory_dump(int count, int format, int wsize,
557 target_phys_addr_t addr, int is_physical)
559 CPUState *env;
560 int nb_per_line, l, line_size, i, max_digits, len;
561 uint8_t buf[16];
562 uint64_t v;
564 if (format == 'i') {
565 int flags;
566 flags = 0;
567 env = mon_get_cpu();
568 if (!env && !is_physical)
569 return;
570 #ifdef TARGET_I386
571 if (wsize == 2) {
572 flags = 1;
573 } else if (wsize == 4) {
574 flags = 0;
575 } else {
576 /* as default we use the current CS size */
577 flags = 0;
578 if (env) {
579 #ifdef TARGET_X86_64
580 if ((env->efer & MSR_EFER_LMA) &&
581 (env->segs[R_CS].flags & DESC_L_MASK))
582 flags = 2;
583 else
584 #endif
585 if (!(env->segs[R_CS].flags & DESC_B_MASK))
586 flags = 1;
589 #endif
590 monitor_disas(env, addr, count, is_physical, flags);
591 return;
594 len = wsize * count;
595 if (wsize == 1)
596 line_size = 8;
597 else
598 line_size = 16;
599 nb_per_line = line_size / wsize;
600 max_digits = 0;
602 switch(format) {
603 case 'o':
604 max_digits = (wsize * 8 + 2) / 3;
605 break;
606 default:
607 case 'x':
608 max_digits = (wsize * 8) / 4;
609 break;
610 case 'u':
611 case 'd':
612 max_digits = (wsize * 8 * 10 + 32) / 33;
613 break;
614 case 'c':
615 wsize = 1;
616 break;
619 while (len > 0) {
620 if (is_physical)
621 term_printf(TARGET_FMT_plx ":", addr);
622 else
623 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
624 l = len;
625 if (l > line_size)
626 l = line_size;
627 if (is_physical) {
628 cpu_physical_memory_rw(addr, buf, l, 0);
629 } else {
630 env = mon_get_cpu();
631 if (!env)
632 break;
633 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
634 term_printf(" Cannot access memory\n");
635 break;
638 i = 0;
639 while (i < l) {
640 switch(wsize) {
641 default:
642 case 1:
643 v = ldub_raw(buf + i);
644 break;
645 case 2:
646 v = lduw_raw(buf + i);
647 break;
648 case 4:
649 v = (uint32_t)ldl_raw(buf + i);
650 break;
651 case 8:
652 v = ldq_raw(buf + i);
653 break;
655 term_printf(" ");
656 switch(format) {
657 case 'o':
658 term_printf("%#*" PRIo64, max_digits, v);
659 break;
660 case 'x':
661 term_printf("0x%0*" PRIx64, max_digits, v);
662 break;
663 case 'u':
664 term_printf("%*" PRIu64, max_digits, v);
665 break;
666 case 'd':
667 term_printf("%*" PRId64, max_digits, v);
668 break;
669 case 'c':
670 term_printc(v);
671 break;
673 i += wsize;
675 term_printf("\n");
676 addr += l;
677 len -= l;
681 #if TARGET_LONG_BITS == 64
682 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
683 #else
684 #define GET_TLONG(h, l) (l)
685 #endif
687 static void do_memory_dump(int count, int format, int size,
688 uint32_t addrh, uint32_t addrl)
690 target_long addr = GET_TLONG(addrh, addrl);
691 memory_dump(count, format, size, addr, 0);
694 #if TARGET_PHYS_ADDR_BITS > 32
695 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
696 #else
697 #define GET_TPHYSADDR(h, l) (l)
698 #endif
700 static void do_physical_memory_dump(int count, int format, int size,
701 uint32_t addrh, uint32_t addrl)
704 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
705 memory_dump(count, format, size, addr, 1);
708 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
710 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
711 #if TARGET_PHYS_ADDR_BITS == 32
712 switch(format) {
713 case 'o':
714 term_printf("%#o", val);
715 break;
716 case 'x':
717 term_printf("%#x", val);
718 break;
719 case 'u':
720 term_printf("%u", val);
721 break;
722 default:
723 case 'd':
724 term_printf("%d", val);
725 break;
726 case 'c':
727 term_printc(val);
728 break;
730 #else
731 switch(format) {
732 case 'o':
733 term_printf("%#" PRIo64, val);
734 break;
735 case 'x':
736 term_printf("%#" PRIx64, val);
737 break;
738 case 'u':
739 term_printf("%" PRIu64, val);
740 break;
741 default:
742 case 'd':
743 term_printf("%" PRId64, val);
744 break;
745 case 'c':
746 term_printc(val);
747 break;
749 #endif
750 term_printf("\n");
753 static void do_memory_save(unsigned int valh, unsigned int vall,
754 uint32_t size, const char *filename)
756 FILE *f;
757 target_long addr = GET_TLONG(valh, vall);
758 uint32_t l;
759 CPUState *env;
760 uint8_t buf[1024];
762 env = mon_get_cpu();
763 if (!env)
764 return;
766 f = fopen(filename, "wb");
767 if (!f) {
768 term_printf("could not open '%s'\n", filename);
769 return;
771 while (size != 0) {
772 l = sizeof(buf);
773 if (l > size)
774 l = size;
775 cpu_memory_rw_debug(env, addr, buf, l, 0);
776 fwrite(buf, 1, l, f);
777 addr += l;
778 size -= l;
780 fclose(f);
783 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
784 uint32_t size, const char *filename)
786 FILE *f;
787 uint32_t l;
788 uint8_t buf[1024];
789 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
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_physical_memory_rw(addr, buf, l, 0);
801 fwrite(buf, 1, l, f);
802 fflush(f);
803 addr += l;
804 size -= l;
806 fclose(f);
809 static void do_sum(uint32_t start, uint32_t size)
811 uint32_t addr;
812 uint8_t buf[1];
813 uint16_t sum;
815 sum = 0;
816 for(addr = start; addr < (start + size); addr++) {
817 cpu_physical_memory_rw(addr, buf, 1, 0);
818 /* BSD sum algorithm ('sum' Unix command) */
819 sum = (sum >> 1) | (sum << 15);
820 sum += buf[0];
822 term_printf("%05d\n", sum);
825 typedef struct {
826 int keycode;
827 const char *name;
828 } KeyDef;
830 static const KeyDef key_defs[] = {
831 { 0x2a, "shift" },
832 { 0x36, "shift_r" },
834 { 0x38, "alt" },
835 { 0xb8, "alt_r" },
836 { 0x64, "altgr" },
837 { 0xe4, "altgr_r" },
838 { 0x1d, "ctrl" },
839 { 0x9d, "ctrl_r" },
841 { 0xdd, "menu" },
843 { 0x01, "esc" },
845 { 0x02, "1" },
846 { 0x03, "2" },
847 { 0x04, "3" },
848 { 0x05, "4" },
849 { 0x06, "5" },
850 { 0x07, "6" },
851 { 0x08, "7" },
852 { 0x09, "8" },
853 { 0x0a, "9" },
854 { 0x0b, "0" },
855 { 0x0c, "minus" },
856 { 0x0d, "equal" },
857 { 0x0e, "backspace" },
859 { 0x0f, "tab" },
860 { 0x10, "q" },
861 { 0x11, "w" },
862 { 0x12, "e" },
863 { 0x13, "r" },
864 { 0x14, "t" },
865 { 0x15, "y" },
866 { 0x16, "u" },
867 { 0x17, "i" },
868 { 0x18, "o" },
869 { 0x19, "p" },
871 { 0x1c, "ret" },
873 { 0x1e, "a" },
874 { 0x1f, "s" },
875 { 0x20, "d" },
876 { 0x21, "f" },
877 { 0x22, "g" },
878 { 0x23, "h" },
879 { 0x24, "j" },
880 { 0x25, "k" },
881 { 0x26, "l" },
883 { 0x2c, "z" },
884 { 0x2d, "x" },
885 { 0x2e, "c" },
886 { 0x2f, "v" },
887 { 0x30, "b" },
888 { 0x31, "n" },
889 { 0x32, "m" },
890 { 0x33, "comma" },
891 { 0x34, "dot" },
892 { 0x35, "slash" },
894 { 0x37, "asterisk" },
896 { 0x39, "spc" },
897 { 0x3a, "caps_lock" },
898 { 0x3b, "f1" },
899 { 0x3c, "f2" },
900 { 0x3d, "f3" },
901 { 0x3e, "f4" },
902 { 0x3f, "f5" },
903 { 0x40, "f6" },
904 { 0x41, "f7" },
905 { 0x42, "f8" },
906 { 0x43, "f9" },
907 { 0x44, "f10" },
908 { 0x45, "num_lock" },
909 { 0x46, "scroll_lock" },
911 { 0xb5, "kp_divide" },
912 { 0x37, "kp_multiply" },
913 { 0x4a, "kp_subtract" },
914 { 0x4e, "kp_add" },
915 { 0x9c, "kp_enter" },
916 { 0x53, "kp_decimal" },
917 { 0x54, "sysrq" },
919 { 0x52, "kp_0" },
920 { 0x4f, "kp_1" },
921 { 0x50, "kp_2" },
922 { 0x51, "kp_3" },
923 { 0x4b, "kp_4" },
924 { 0x4c, "kp_5" },
925 { 0x4d, "kp_6" },
926 { 0x47, "kp_7" },
927 { 0x48, "kp_8" },
928 { 0x49, "kp_9" },
930 { 0x56, "<" },
932 { 0x57, "f11" },
933 { 0x58, "f12" },
935 { 0xb7, "print" },
937 { 0xc7, "home" },
938 { 0xc9, "pgup" },
939 { 0xd1, "pgdn" },
940 { 0xcf, "end" },
942 { 0xcb, "left" },
943 { 0xc8, "up" },
944 { 0xd0, "down" },
945 { 0xcd, "right" },
947 { 0xd2, "insert" },
948 { 0xd3, "delete" },
949 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
950 { 0xf0, "stop" },
951 { 0xf1, "again" },
952 { 0xf2, "props" },
953 { 0xf3, "undo" },
954 { 0xf4, "front" },
955 { 0xf5, "copy" },
956 { 0xf6, "open" },
957 { 0xf7, "paste" },
958 { 0xf8, "find" },
959 { 0xf9, "cut" },
960 { 0xfa, "lf" },
961 { 0xfb, "help" },
962 { 0xfc, "meta_l" },
963 { 0xfd, "meta_r" },
964 { 0xfe, "compose" },
965 #endif
966 { 0, NULL },
969 static int get_keycode(const char *key)
971 const KeyDef *p;
972 char *endp;
973 int ret;
975 for(p = key_defs; p->name != NULL; p++) {
976 if (!strcmp(key, p->name))
977 return p->keycode;
979 if (strstart(key, "0x", NULL)) {
980 ret = strtoul(key, &endp, 0);
981 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
982 return ret;
984 return -1;
987 #define MAX_KEYCODES 16
988 static uint8_t keycodes[MAX_KEYCODES];
989 static int nb_pending_keycodes;
990 static QEMUTimer *key_timer;
992 static void release_keys(void *opaque)
994 int keycode;
996 while (nb_pending_keycodes > 0) {
997 nb_pending_keycodes--;
998 keycode = keycodes[nb_pending_keycodes];
999 if (keycode & 0x80)
1000 kbd_put_keycode(0xe0);
1001 kbd_put_keycode(keycode | 0x80);
1005 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1007 char keyname_buf[16];
1008 char *separator;
1009 int keyname_len, keycode, i;
1011 if (nb_pending_keycodes > 0) {
1012 qemu_del_timer(key_timer);
1013 release_keys(NULL);
1015 if (!has_hold_time)
1016 hold_time = 100;
1017 i = 0;
1018 while (1) {
1019 separator = strchr(string, '-');
1020 keyname_len = separator ? separator - string : strlen(string);
1021 if (keyname_len > 0) {
1022 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1023 if (keyname_len > sizeof(keyname_buf) - 1) {
1024 term_printf("invalid key: '%s...'\n", keyname_buf);
1025 return;
1027 if (i == MAX_KEYCODES) {
1028 term_printf("too many keys\n");
1029 return;
1031 keyname_buf[keyname_len] = 0;
1032 keycode = get_keycode(keyname_buf);
1033 if (keycode < 0) {
1034 term_printf("unknown key: '%s'\n", keyname_buf);
1035 return;
1037 keycodes[i++] = keycode;
1039 if (!separator)
1040 break;
1041 string = separator + 1;
1043 nb_pending_keycodes = i;
1044 /* key down events */
1045 for (i = 0; i < nb_pending_keycodes; i++) {
1046 keycode = keycodes[i];
1047 if (keycode & 0x80)
1048 kbd_put_keycode(0xe0);
1049 kbd_put_keycode(keycode & 0x7f);
1051 /* delayed key up events */
1052 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1053 muldiv64(ticks_per_sec, hold_time, 1000));
1056 static int mouse_button_state;
1058 static void do_mouse_move(const char *dx_str, const char *dy_str,
1059 const char *dz_str)
1061 int dx, dy, dz;
1062 dx = strtol(dx_str, NULL, 0);
1063 dy = strtol(dy_str, NULL, 0);
1064 dz = 0;
1065 if (dz_str)
1066 dz = strtol(dz_str, NULL, 0);
1067 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1070 static void do_mouse_button(int button_state)
1072 mouse_button_state = button_state;
1073 kbd_mouse_event(0, 0, 0, mouse_button_state);
1076 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1078 uint32_t val;
1079 int suffix;
1081 if (has_index) {
1082 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1083 addr++;
1085 addr &= 0xffff;
1087 switch(size) {
1088 default:
1089 case 1:
1090 val = cpu_inb(NULL, addr);
1091 suffix = 'b';
1092 break;
1093 case 2:
1094 val = cpu_inw(NULL, addr);
1095 suffix = 'w';
1096 break;
1097 case 4:
1098 val = cpu_inl(NULL, addr);
1099 suffix = 'l';
1100 break;
1102 term_printf("port%c[0x%04x] = %#0*x\n",
1103 suffix, addr, size * 2, val);
1106 /* boot_set handler */
1107 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1108 static void *boot_opaque;
1110 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1112 qemu_boot_set_handler = func;
1113 boot_opaque = opaque;
1116 static void do_boot_set(const char *bootdevice)
1118 int res;
1120 if (qemu_boot_set_handler) {
1121 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1122 if (res == 0)
1123 term_printf("boot device list now set to %s\n", bootdevice);
1124 else
1125 term_printf("setting boot device list failed with error %i\n", res);
1126 } else {
1127 term_printf("no function defined to set boot device list for this architecture\n");
1131 static void do_system_reset(void)
1133 qemu_system_reset_request();
1136 static void do_system_powerdown(void)
1138 qemu_system_powerdown_request();
1141 #if defined(TARGET_I386)
1142 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1144 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1145 addr,
1146 pte & mask,
1147 pte & PG_GLOBAL_MASK ? 'G' : '-',
1148 pte & PG_PSE_MASK ? 'P' : '-',
1149 pte & PG_DIRTY_MASK ? 'D' : '-',
1150 pte & PG_ACCESSED_MASK ? 'A' : '-',
1151 pte & PG_PCD_MASK ? 'C' : '-',
1152 pte & PG_PWT_MASK ? 'T' : '-',
1153 pte & PG_USER_MASK ? 'U' : '-',
1154 pte & PG_RW_MASK ? 'W' : '-');
1157 static void tlb_info(void)
1159 CPUState *env;
1160 int l1, l2;
1161 uint32_t pgd, pde, pte;
1163 env = mon_get_cpu();
1164 if (!env)
1165 return;
1167 if (!(env->cr[0] & CR0_PG_MASK)) {
1168 term_printf("PG disabled\n");
1169 return;
1171 pgd = env->cr[3] & ~0xfff;
1172 for(l1 = 0; l1 < 1024; l1++) {
1173 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1174 pde = le32_to_cpu(pde);
1175 if (pde & PG_PRESENT_MASK) {
1176 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1177 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1178 } else {
1179 for(l2 = 0; l2 < 1024; l2++) {
1180 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1181 (uint8_t *)&pte, 4);
1182 pte = le32_to_cpu(pte);
1183 if (pte & PG_PRESENT_MASK) {
1184 print_pte((l1 << 22) + (l2 << 12),
1185 pte & ~PG_PSE_MASK,
1186 ~0xfff);
1194 static void mem_print(uint32_t *pstart, int *plast_prot,
1195 uint32_t end, int prot)
1197 int prot1;
1198 prot1 = *plast_prot;
1199 if (prot != prot1) {
1200 if (*pstart != -1) {
1201 term_printf("%08x-%08x %08x %c%c%c\n",
1202 *pstart, end, end - *pstart,
1203 prot1 & PG_USER_MASK ? 'u' : '-',
1204 'r',
1205 prot1 & PG_RW_MASK ? 'w' : '-');
1207 if (prot != 0)
1208 *pstart = end;
1209 else
1210 *pstart = -1;
1211 *plast_prot = prot;
1215 static void mem_info(void)
1217 CPUState *env;
1218 int l1, l2, prot, last_prot;
1219 uint32_t pgd, pde, pte, start, end;
1221 env = mon_get_cpu();
1222 if (!env)
1223 return;
1225 if (!(env->cr[0] & CR0_PG_MASK)) {
1226 term_printf("PG disabled\n");
1227 return;
1229 pgd = env->cr[3] & ~0xfff;
1230 last_prot = 0;
1231 start = -1;
1232 for(l1 = 0; l1 < 1024; l1++) {
1233 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1234 pde = le32_to_cpu(pde);
1235 end = l1 << 22;
1236 if (pde & PG_PRESENT_MASK) {
1237 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1238 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1239 mem_print(&start, &last_prot, end, prot);
1240 } else {
1241 for(l2 = 0; l2 < 1024; l2++) {
1242 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1243 (uint8_t *)&pte, 4);
1244 pte = le32_to_cpu(pte);
1245 end = (l1 << 22) + (l2 << 12);
1246 if (pte & PG_PRESENT_MASK) {
1247 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1248 } else {
1249 prot = 0;
1251 mem_print(&start, &last_prot, end, prot);
1254 } else {
1255 prot = 0;
1256 mem_print(&start, &last_prot, end, prot);
1260 #endif
1262 static void do_info_kqemu(void)
1264 #ifdef USE_KQEMU
1265 CPUState *env;
1266 int val;
1267 val = 0;
1268 env = mon_get_cpu();
1269 if (!env) {
1270 term_printf("No cpu initialized yet");
1271 return;
1273 val = env->kqemu_enabled;
1274 term_printf("kqemu support: ");
1275 switch(val) {
1276 default:
1277 case 0:
1278 term_printf("disabled\n");
1279 break;
1280 case 1:
1281 term_printf("enabled for user code\n");
1282 break;
1283 case 2:
1284 term_printf("enabled for user and kernel code\n");
1285 break;
1287 #else
1288 term_printf("kqemu support: not compiled\n");
1289 #endif
1292 static void do_info_kvm(void)
1294 #if defined(USE_KVM) || defined(CONFIG_KVM)
1295 term_printf("kvm support: ");
1296 if (kvm_enabled())
1297 term_printf("enabled\n");
1298 else
1299 term_printf("disabled\n");
1300 #else
1301 term_printf("kvm support: not compiled\n");
1302 #endif
1305 #ifdef CONFIG_PROFILER
1307 int64_t kqemu_time;
1308 int64_t qemu_time;
1309 int64_t kqemu_exec_count;
1310 int64_t dev_time;
1311 int64_t kqemu_ret_int_count;
1312 int64_t kqemu_ret_excp_count;
1313 int64_t kqemu_ret_intr_count;
1315 static void do_info_profile(void)
1317 int64_t total;
1318 total = qemu_time;
1319 if (total == 0)
1320 total = 1;
1321 term_printf("async time %" PRId64 " (%0.3f)\n",
1322 dev_time, dev_time / (double)ticks_per_sec);
1323 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1324 qemu_time, qemu_time / (double)ticks_per_sec);
1325 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1326 kqemu_time, kqemu_time / (double)ticks_per_sec,
1327 kqemu_time / (double)total * 100.0,
1328 kqemu_exec_count,
1329 kqemu_ret_int_count,
1330 kqemu_ret_excp_count,
1331 kqemu_ret_intr_count);
1332 qemu_time = 0;
1333 kqemu_time = 0;
1334 kqemu_exec_count = 0;
1335 dev_time = 0;
1336 kqemu_ret_int_count = 0;
1337 kqemu_ret_excp_count = 0;
1338 kqemu_ret_intr_count = 0;
1339 #ifdef USE_KQEMU
1340 kqemu_record_dump();
1341 #endif
1343 #else
1344 static void do_info_profile(void)
1346 term_printf("Internal profiler not compiled\n");
1348 #endif
1350 /* Capture support */
1351 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1353 static void do_info_capture (void)
1355 int i;
1356 CaptureState *s;
1358 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1359 term_printf ("[%d]: ", i);
1360 s->ops.info (s->opaque);
1364 static void do_stop_capture (int n)
1366 int i;
1367 CaptureState *s;
1369 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1370 if (i == n) {
1371 s->ops.destroy (s->opaque);
1372 LIST_REMOVE (s, entries);
1373 qemu_free (s);
1374 return;
1379 #ifdef HAS_AUDIO
1380 static void do_wav_capture (const char *path,
1381 int has_freq, int freq,
1382 int has_bits, int bits,
1383 int has_channels, int nchannels)
1385 CaptureState *s;
1387 s = qemu_mallocz (sizeof (*s));
1388 if (!s) {
1389 term_printf ("Not enough memory to add wave capture\n");
1390 return;
1393 freq = has_freq ? freq : 44100;
1394 bits = has_bits ? bits : 16;
1395 nchannels = has_channels ? nchannels : 2;
1397 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1398 term_printf ("Faied to add wave capture\n");
1399 qemu_free (s);
1401 LIST_INSERT_HEAD (&capture_head, s, entries);
1403 #endif
1405 #if defined(TARGET_I386)
1406 static void do_inject_nmi(int cpu_index)
1408 CPUState *env;
1410 for (env = first_cpu; env != NULL; env = env->next_cpu)
1411 if (env->cpu_index == cpu_index) {
1412 if (kvm_enabled())
1413 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1414 else
1415 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1416 break;
1419 #endif
1421 static void do_balloon(int value)
1423 ram_addr_t target = value;
1424 qemu_balloon(target << 20);
1427 static void do_info_balloon(void)
1429 ram_addr_t actual;
1431 actual = qemu_balloon_status();
1432 if (kvm_enabled() && !kvm_has_sync_mmu())
1433 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1434 else if (actual == 0)
1435 term_printf("Ballooning not activated in VM\n");
1436 else
1437 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1440 static const term_cmd_t term_cmds[] = {
1441 { "help|?", "s?", do_help,
1442 "[cmd]", "show the help" },
1443 { "commit", "s", do_commit,
1444 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1445 { "info", "s?", do_info,
1446 "subcommand", "show various information about the system state" },
1447 { "q|quit", "", do_quit,
1448 "", "quit the emulator" },
1449 { "eject", "-fB", do_eject,
1450 "[-f] device", "eject a removable medium (use -f to force it)" },
1451 { "change", "BFs?", do_change,
1452 "device filename [format]", "change a removable medium, optional format" },
1453 { "screendump", "F", do_screen_dump,
1454 "filename", "save screen into PPM image 'filename'" },
1455 { "logfile", "F", do_logfile,
1456 "filename", "output logs to 'filename'" },
1457 { "log", "s", do_log,
1458 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1459 { "savevm", "s?", do_savevm,
1460 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1461 { "loadvm", "s", do_loadvm,
1462 "tag|id", "restore a VM snapshot from its tag or id" },
1463 { "delvm", "s", do_delvm,
1464 "tag|id", "delete a VM snapshot from its tag or id" },
1465 { "stop", "", do_stop,
1466 "", "stop emulation", },
1467 { "c|cont", "", do_cont,
1468 "", "resume emulation", },
1469 #ifdef CONFIG_GDBSTUB
1470 { "gdbserver", "s?", do_gdbserver,
1471 "[port]", "start gdbserver session (default port=1234)", },
1472 #endif
1473 { "x", "/l", do_memory_dump,
1474 "/fmt addr", "virtual memory dump starting at 'addr'", },
1475 { "xp", "/l", do_physical_memory_dump,
1476 "/fmt addr", "physical memory dump starting at 'addr'", },
1477 { "p|print", "/l", do_print,
1478 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1479 { "i", "/ii.", do_ioport_read,
1480 "/fmt addr", "I/O port read" },
1482 { "sendkey", "si?", do_sendkey,
1483 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1484 { "system_reset", "", do_system_reset,
1485 "", "reset the system" },
1486 { "system_powerdown", "", do_system_powerdown,
1487 "", "send system power down event" },
1488 { "sum", "ii", do_sum,
1489 "addr size", "compute the checksum of a memory region" },
1490 { "usb_add", "s", do_usb_add,
1491 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1492 { "usb_del", "s", do_usb_del,
1493 "device", "remove USB device 'bus.addr'" },
1494 { "cpu", "i", do_cpu_set,
1495 "index", "set the default CPU" },
1496 { "mouse_move", "sss?", do_mouse_move,
1497 "dx dy [dz]", "send mouse move events" },
1498 { "mouse_button", "i", do_mouse_button,
1499 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1500 { "mouse_set", "i", do_mouse_set,
1501 "index", "set which mouse device receives events" },
1502 #ifdef HAS_AUDIO
1503 { "wavcapture", "si?i?i?", do_wav_capture,
1504 "path [frequency bits channels]",
1505 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1506 #endif
1507 { "stopcapture", "i", do_stop_capture,
1508 "capture index", "stop capture" },
1509 { "memsave", "lis", do_memory_save,
1510 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1511 { "pmemsave", "lis", do_physical_memory_save,
1512 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1513 { "boot_set", "s", do_boot_set,
1514 "bootdevice", "define new values for the boot device list" },
1515 #if defined(TARGET_I386)
1516 { "nmi", "i", do_inject_nmi,
1517 "cpu", "inject an NMI on the given CPU", },
1518 #endif
1519 { "migrate", "-ds", do_migrate,
1520 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1521 { "migrate_cancel", "", do_migrate_cancel,
1522 "", "cancel the current VM migration" },
1523 { "migrate_set_speed", "s", do_migrate_set_speed,
1524 "value", "set maximum speed (in bytes) for migrations" },
1525 { "balloon", "i", do_balloon,
1526 "target", "request VM to change it's memory allocation (in MB)" },
1527 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1528 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1529 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1530 "[,unit=m][,media=d][index=i]\n"
1531 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1532 "[snapshot=on|off][,cache=on|off]",
1533 "add drive to PCI storage controller" },
1534 { "pci_add", "iss", device_hot_add, "bus 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" },
1535 { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" },
1536 #endif
1537 { NULL, NULL, },
1540 static const term_cmd_t info_cmds[] = {
1541 { "version", "", do_info_version,
1542 "", "show the version of qemu" },
1543 { "network", "", do_info_network,
1544 "", "show the network state" },
1545 { "chardev", "", qemu_chr_info,
1546 "", "show the character devices" },
1547 { "block", "", do_info_block,
1548 "", "show the block devices" },
1549 { "blockstats", "", do_info_blockstats,
1550 "", "show block device statistics" },
1551 { "registers", "", do_info_registers,
1552 "", "show the cpu registers" },
1553 { "cpus", "", do_info_cpus,
1554 "", "show infos for each CPU" },
1555 { "history", "", do_info_history,
1556 "", "show the command line history", },
1557 { "irq", "", irq_info,
1558 "", "show the interrupts statistics (if available)", },
1559 { "pic", "", pic_info,
1560 "", "show i8259 (PIC) state", },
1561 { "pci", "", pci_info,
1562 "", "show PCI info", },
1563 #if defined(TARGET_I386)
1564 { "tlb", "", tlb_info,
1565 "", "show virtual to physical memory mappings", },
1566 { "mem", "", mem_info,
1567 "", "show the active virtual memory mappings", },
1568 #endif
1569 { "jit", "", do_info_jit,
1570 "", "show dynamic compiler info", },
1571 { "kqemu", "", do_info_kqemu,
1572 "", "show kqemu information", },
1573 { "kvm", "", do_info_kvm,
1574 "", "show kvm information", },
1575 { "usb", "", usb_info,
1576 "", "show guest USB devices", },
1577 { "usbhost", "", usb_host_info,
1578 "", "show host USB devices", },
1579 { "profile", "", do_info_profile,
1580 "", "show profiling information", },
1581 { "capture", "", do_info_capture,
1582 "", "show capture information" },
1583 { "snapshots", "", do_info_snapshots,
1584 "", "show the currently saved VM snapshots" },
1585 { "pcmcia", "", pcmcia_info,
1586 "", "show guest PCMCIA status" },
1587 { "mice", "", do_info_mice,
1588 "", "show which guest mouse is receiving events" },
1589 { "vnc", "", do_info_vnc,
1590 "", "show the vnc server status"},
1591 { "name", "", do_info_name,
1592 "", "show the current VM name" },
1593 { "uuid", "", do_info_uuid,
1594 "", "show the current VM UUID" },
1595 #if defined(TARGET_PPC)
1596 { "cpustats", "", do_info_cpu_stats,
1597 "", "show CPU statistics", },
1598 #endif
1599 #if defined(CONFIG_SLIRP)
1600 { "slirp", "", do_info_slirp,
1601 "", "show SLIRP statistics", },
1602 #endif
1603 { "migrate", "", do_info_migrate, "", "show migration status" },
1604 { "balloon", "", do_info_balloon,
1605 "", "show balloon information" },
1606 { NULL, NULL, },
1609 /*******************************************************************/
1611 static const char *pch;
1612 static jmp_buf expr_env;
1614 #define MD_TLONG 0
1615 #define MD_I32 1
1617 typedef struct MonitorDef {
1618 const char *name;
1619 int offset;
1620 target_long (*get_value)(const struct MonitorDef *md, int val);
1621 int type;
1622 } MonitorDef;
1624 #if defined(TARGET_I386)
1625 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1627 CPUState *env = mon_get_cpu();
1628 if (!env)
1629 return 0;
1630 return env->eip + env->segs[R_CS].base;
1632 #endif
1634 #if defined(TARGET_PPC)
1635 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1637 CPUState *env = mon_get_cpu();
1638 unsigned int u;
1639 int i;
1641 if (!env)
1642 return 0;
1644 u = 0;
1645 for (i = 0; i < 8; i++)
1646 u |= env->crf[i] << (32 - (4 * i));
1648 return u;
1651 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1653 CPUState *env = mon_get_cpu();
1654 if (!env)
1655 return 0;
1656 return env->msr;
1659 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1661 CPUState *env = mon_get_cpu();
1662 if (!env)
1663 return 0;
1664 return env->xer;
1667 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1669 CPUState *env = mon_get_cpu();
1670 if (!env)
1671 return 0;
1672 return cpu_ppc_load_decr(env);
1675 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1677 CPUState *env = mon_get_cpu();
1678 if (!env)
1679 return 0;
1680 return cpu_ppc_load_tbu(env);
1683 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1685 CPUState *env = mon_get_cpu();
1686 if (!env)
1687 return 0;
1688 return cpu_ppc_load_tbl(env);
1690 #endif
1692 #if defined(TARGET_SPARC)
1693 #ifndef TARGET_SPARC64
1694 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1696 CPUState *env = mon_get_cpu();
1697 if (!env)
1698 return 0;
1699 return GET_PSR(env);
1701 #endif
1703 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1705 CPUState *env = mon_get_cpu();
1706 if (!env)
1707 return 0;
1708 return env->regwptr[val];
1710 #endif
1712 static const MonitorDef monitor_defs[] = {
1713 #ifdef TARGET_I386
1715 #define SEG(name, seg) \
1716 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1717 { name ".base", offsetof(CPUState, segs[seg].base) },\
1718 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1720 { "eax", offsetof(CPUState, regs[0]) },
1721 { "ecx", offsetof(CPUState, regs[1]) },
1722 { "edx", offsetof(CPUState, regs[2]) },
1723 { "ebx", offsetof(CPUState, regs[3]) },
1724 { "esp|sp", offsetof(CPUState, regs[4]) },
1725 { "ebp|fp", offsetof(CPUState, regs[5]) },
1726 { "esi", offsetof(CPUState, regs[6]) },
1727 { "edi", offsetof(CPUState, regs[7]) },
1728 #ifdef TARGET_X86_64
1729 { "r8", offsetof(CPUState, regs[8]) },
1730 { "r9", offsetof(CPUState, regs[9]) },
1731 { "r10", offsetof(CPUState, regs[10]) },
1732 { "r11", offsetof(CPUState, regs[11]) },
1733 { "r12", offsetof(CPUState, regs[12]) },
1734 { "r13", offsetof(CPUState, regs[13]) },
1735 { "r14", offsetof(CPUState, regs[14]) },
1736 { "r15", offsetof(CPUState, regs[15]) },
1737 #endif
1738 { "eflags", offsetof(CPUState, eflags) },
1739 { "eip", offsetof(CPUState, eip) },
1740 SEG("cs", R_CS)
1741 SEG("ds", R_DS)
1742 SEG("es", R_ES)
1743 SEG("ss", R_SS)
1744 SEG("fs", R_FS)
1745 SEG("gs", R_GS)
1746 { "pc", 0, monitor_get_pc, },
1747 #elif defined(TARGET_PPC)
1748 /* General purpose registers */
1749 { "r0", offsetof(CPUState, gpr[0]) },
1750 { "r1", offsetof(CPUState, gpr[1]) },
1751 { "r2", offsetof(CPUState, gpr[2]) },
1752 { "r3", offsetof(CPUState, gpr[3]) },
1753 { "r4", offsetof(CPUState, gpr[4]) },
1754 { "r5", offsetof(CPUState, gpr[5]) },
1755 { "r6", offsetof(CPUState, gpr[6]) },
1756 { "r7", offsetof(CPUState, gpr[7]) },
1757 { "r8", offsetof(CPUState, gpr[8]) },
1758 { "r9", offsetof(CPUState, gpr[9]) },
1759 { "r10", offsetof(CPUState, gpr[10]) },
1760 { "r11", offsetof(CPUState, gpr[11]) },
1761 { "r12", offsetof(CPUState, gpr[12]) },
1762 { "r13", offsetof(CPUState, gpr[13]) },
1763 { "r14", offsetof(CPUState, gpr[14]) },
1764 { "r15", offsetof(CPUState, gpr[15]) },
1765 { "r16", offsetof(CPUState, gpr[16]) },
1766 { "r17", offsetof(CPUState, gpr[17]) },
1767 { "r18", offsetof(CPUState, gpr[18]) },
1768 { "r19", offsetof(CPUState, gpr[19]) },
1769 { "r20", offsetof(CPUState, gpr[20]) },
1770 { "r21", offsetof(CPUState, gpr[21]) },
1771 { "r22", offsetof(CPUState, gpr[22]) },
1772 { "r23", offsetof(CPUState, gpr[23]) },
1773 { "r24", offsetof(CPUState, gpr[24]) },
1774 { "r25", offsetof(CPUState, gpr[25]) },
1775 { "r26", offsetof(CPUState, gpr[26]) },
1776 { "r27", offsetof(CPUState, gpr[27]) },
1777 { "r28", offsetof(CPUState, gpr[28]) },
1778 { "r29", offsetof(CPUState, gpr[29]) },
1779 { "r30", offsetof(CPUState, gpr[30]) },
1780 { "r31", offsetof(CPUState, gpr[31]) },
1781 /* Floating point registers */
1782 { "f0", offsetof(CPUState, fpr[0]) },
1783 { "f1", offsetof(CPUState, fpr[1]) },
1784 { "f2", offsetof(CPUState, fpr[2]) },
1785 { "f3", offsetof(CPUState, fpr[3]) },
1786 { "f4", offsetof(CPUState, fpr[4]) },
1787 { "f5", offsetof(CPUState, fpr[5]) },
1788 { "f6", offsetof(CPUState, fpr[6]) },
1789 { "f7", offsetof(CPUState, fpr[7]) },
1790 { "f8", offsetof(CPUState, fpr[8]) },
1791 { "f9", offsetof(CPUState, fpr[9]) },
1792 { "f10", offsetof(CPUState, fpr[10]) },
1793 { "f11", offsetof(CPUState, fpr[11]) },
1794 { "f12", offsetof(CPUState, fpr[12]) },
1795 { "f13", offsetof(CPUState, fpr[13]) },
1796 { "f14", offsetof(CPUState, fpr[14]) },
1797 { "f15", offsetof(CPUState, fpr[15]) },
1798 { "f16", offsetof(CPUState, fpr[16]) },
1799 { "f17", offsetof(CPUState, fpr[17]) },
1800 { "f18", offsetof(CPUState, fpr[18]) },
1801 { "f19", offsetof(CPUState, fpr[19]) },
1802 { "f20", offsetof(CPUState, fpr[20]) },
1803 { "f21", offsetof(CPUState, fpr[21]) },
1804 { "f22", offsetof(CPUState, fpr[22]) },
1805 { "f23", offsetof(CPUState, fpr[23]) },
1806 { "f24", offsetof(CPUState, fpr[24]) },
1807 { "f25", offsetof(CPUState, fpr[25]) },
1808 { "f26", offsetof(CPUState, fpr[26]) },
1809 { "f27", offsetof(CPUState, fpr[27]) },
1810 { "f28", offsetof(CPUState, fpr[28]) },
1811 { "f29", offsetof(CPUState, fpr[29]) },
1812 { "f30", offsetof(CPUState, fpr[30]) },
1813 { "f31", offsetof(CPUState, fpr[31]) },
1814 { "fpscr", offsetof(CPUState, fpscr) },
1815 /* Next instruction pointer */
1816 { "nip|pc", offsetof(CPUState, nip) },
1817 { "lr", offsetof(CPUState, lr) },
1818 { "ctr", offsetof(CPUState, ctr) },
1819 { "decr", 0, &monitor_get_decr, },
1820 { "ccr", 0, &monitor_get_ccr, },
1821 /* Machine state register */
1822 { "msr", 0, &monitor_get_msr, },
1823 { "xer", 0, &monitor_get_xer, },
1824 { "tbu", 0, &monitor_get_tbu, },
1825 { "tbl", 0, &monitor_get_tbl, },
1826 #if defined(TARGET_PPC64)
1827 /* Address space register */
1828 { "asr", offsetof(CPUState, asr) },
1829 #endif
1830 /* Segment registers */
1831 { "sdr1", offsetof(CPUState, sdr1) },
1832 { "sr0", offsetof(CPUState, sr[0]) },
1833 { "sr1", offsetof(CPUState, sr[1]) },
1834 { "sr2", offsetof(CPUState, sr[2]) },
1835 { "sr3", offsetof(CPUState, sr[3]) },
1836 { "sr4", offsetof(CPUState, sr[4]) },
1837 { "sr5", offsetof(CPUState, sr[5]) },
1838 { "sr6", offsetof(CPUState, sr[6]) },
1839 { "sr7", offsetof(CPUState, sr[7]) },
1840 { "sr8", offsetof(CPUState, sr[8]) },
1841 { "sr9", offsetof(CPUState, sr[9]) },
1842 { "sr10", offsetof(CPUState, sr[10]) },
1843 { "sr11", offsetof(CPUState, sr[11]) },
1844 { "sr12", offsetof(CPUState, sr[12]) },
1845 { "sr13", offsetof(CPUState, sr[13]) },
1846 { "sr14", offsetof(CPUState, sr[14]) },
1847 { "sr15", offsetof(CPUState, sr[15]) },
1848 /* Too lazy to put BATs and SPRs ... */
1849 #elif defined(TARGET_SPARC)
1850 { "g0", offsetof(CPUState, gregs[0]) },
1851 { "g1", offsetof(CPUState, gregs[1]) },
1852 { "g2", offsetof(CPUState, gregs[2]) },
1853 { "g3", offsetof(CPUState, gregs[3]) },
1854 { "g4", offsetof(CPUState, gregs[4]) },
1855 { "g5", offsetof(CPUState, gregs[5]) },
1856 { "g6", offsetof(CPUState, gregs[6]) },
1857 { "g7", offsetof(CPUState, gregs[7]) },
1858 { "o0", 0, monitor_get_reg },
1859 { "o1", 1, monitor_get_reg },
1860 { "o2", 2, monitor_get_reg },
1861 { "o3", 3, monitor_get_reg },
1862 { "o4", 4, monitor_get_reg },
1863 { "o5", 5, monitor_get_reg },
1864 { "o6", 6, monitor_get_reg },
1865 { "o7", 7, monitor_get_reg },
1866 { "l0", 8, monitor_get_reg },
1867 { "l1", 9, monitor_get_reg },
1868 { "l2", 10, monitor_get_reg },
1869 { "l3", 11, monitor_get_reg },
1870 { "l4", 12, monitor_get_reg },
1871 { "l5", 13, monitor_get_reg },
1872 { "l6", 14, monitor_get_reg },
1873 { "l7", 15, monitor_get_reg },
1874 { "i0", 16, monitor_get_reg },
1875 { "i1", 17, monitor_get_reg },
1876 { "i2", 18, monitor_get_reg },
1877 { "i3", 19, monitor_get_reg },
1878 { "i4", 20, monitor_get_reg },
1879 { "i5", 21, monitor_get_reg },
1880 { "i6", 22, monitor_get_reg },
1881 { "i7", 23, monitor_get_reg },
1882 { "pc", offsetof(CPUState, pc) },
1883 { "npc", offsetof(CPUState, npc) },
1884 { "y", offsetof(CPUState, y) },
1885 #ifndef TARGET_SPARC64
1886 { "psr", 0, &monitor_get_psr, },
1887 { "wim", offsetof(CPUState, wim) },
1888 #endif
1889 { "tbr", offsetof(CPUState, tbr) },
1890 { "fsr", offsetof(CPUState, fsr) },
1891 { "f0", offsetof(CPUState, fpr[0]) },
1892 { "f1", offsetof(CPUState, fpr[1]) },
1893 { "f2", offsetof(CPUState, fpr[2]) },
1894 { "f3", offsetof(CPUState, fpr[3]) },
1895 { "f4", offsetof(CPUState, fpr[4]) },
1896 { "f5", offsetof(CPUState, fpr[5]) },
1897 { "f6", offsetof(CPUState, fpr[6]) },
1898 { "f7", offsetof(CPUState, fpr[7]) },
1899 { "f8", offsetof(CPUState, fpr[8]) },
1900 { "f9", offsetof(CPUState, fpr[9]) },
1901 { "f10", offsetof(CPUState, fpr[10]) },
1902 { "f11", offsetof(CPUState, fpr[11]) },
1903 { "f12", offsetof(CPUState, fpr[12]) },
1904 { "f13", offsetof(CPUState, fpr[13]) },
1905 { "f14", offsetof(CPUState, fpr[14]) },
1906 { "f15", offsetof(CPUState, fpr[15]) },
1907 { "f16", offsetof(CPUState, fpr[16]) },
1908 { "f17", offsetof(CPUState, fpr[17]) },
1909 { "f18", offsetof(CPUState, fpr[18]) },
1910 { "f19", offsetof(CPUState, fpr[19]) },
1911 { "f20", offsetof(CPUState, fpr[20]) },
1912 { "f21", offsetof(CPUState, fpr[21]) },
1913 { "f22", offsetof(CPUState, fpr[22]) },
1914 { "f23", offsetof(CPUState, fpr[23]) },
1915 { "f24", offsetof(CPUState, fpr[24]) },
1916 { "f25", offsetof(CPUState, fpr[25]) },
1917 { "f26", offsetof(CPUState, fpr[26]) },
1918 { "f27", offsetof(CPUState, fpr[27]) },
1919 { "f28", offsetof(CPUState, fpr[28]) },
1920 { "f29", offsetof(CPUState, fpr[29]) },
1921 { "f30", offsetof(CPUState, fpr[30]) },
1922 { "f31", offsetof(CPUState, fpr[31]) },
1923 #ifdef TARGET_SPARC64
1924 { "f32", offsetof(CPUState, fpr[32]) },
1925 { "f34", offsetof(CPUState, fpr[34]) },
1926 { "f36", offsetof(CPUState, fpr[36]) },
1927 { "f38", offsetof(CPUState, fpr[38]) },
1928 { "f40", offsetof(CPUState, fpr[40]) },
1929 { "f42", offsetof(CPUState, fpr[42]) },
1930 { "f44", offsetof(CPUState, fpr[44]) },
1931 { "f46", offsetof(CPUState, fpr[46]) },
1932 { "f48", offsetof(CPUState, fpr[48]) },
1933 { "f50", offsetof(CPUState, fpr[50]) },
1934 { "f52", offsetof(CPUState, fpr[52]) },
1935 { "f54", offsetof(CPUState, fpr[54]) },
1936 { "f56", offsetof(CPUState, fpr[56]) },
1937 { "f58", offsetof(CPUState, fpr[58]) },
1938 { "f60", offsetof(CPUState, fpr[60]) },
1939 { "f62", offsetof(CPUState, fpr[62]) },
1940 { "asi", offsetof(CPUState, asi) },
1941 { "pstate", offsetof(CPUState, pstate) },
1942 { "cansave", offsetof(CPUState, cansave) },
1943 { "canrestore", offsetof(CPUState, canrestore) },
1944 { "otherwin", offsetof(CPUState, otherwin) },
1945 { "wstate", offsetof(CPUState, wstate) },
1946 { "cleanwin", offsetof(CPUState, cleanwin) },
1947 { "fprs", offsetof(CPUState, fprs) },
1948 #endif
1949 #endif
1950 { NULL },
1953 static void expr_error(const char *fmt)
1955 term_printf(fmt);
1956 term_printf("\n");
1957 longjmp(expr_env, 1);
1960 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1961 static int get_monitor_def(target_long *pval, const char *name)
1963 const MonitorDef *md;
1964 void *ptr;
1966 for(md = monitor_defs; md->name != NULL; md++) {
1967 if (compare_cmd(name, md->name)) {
1968 if (md->get_value) {
1969 *pval = md->get_value(md, md->offset);
1970 } else {
1971 CPUState *env = mon_get_cpu();
1972 if (!env)
1973 return -2;
1974 ptr = (uint8_t *)env + md->offset;
1975 switch(md->type) {
1976 case MD_I32:
1977 *pval = *(int32_t *)ptr;
1978 break;
1979 case MD_TLONG:
1980 *pval = *(target_long *)ptr;
1981 break;
1982 default:
1983 *pval = 0;
1984 break;
1987 return 0;
1990 return -1;
1993 static void next(void)
1995 if (pch != '\0') {
1996 pch++;
1997 while (qemu_isspace(*pch))
1998 pch++;
2002 static int64_t expr_sum(void);
2004 static int64_t expr_unary(void)
2006 int64_t n;
2007 char *p;
2008 int ret;
2010 switch(*pch) {
2011 case '+':
2012 next();
2013 n = expr_unary();
2014 break;
2015 case '-':
2016 next();
2017 n = -expr_unary();
2018 break;
2019 case '~':
2020 next();
2021 n = ~expr_unary();
2022 break;
2023 case '(':
2024 next();
2025 n = expr_sum();
2026 if (*pch != ')') {
2027 expr_error("')' expected");
2029 next();
2030 break;
2031 case '\'':
2032 pch++;
2033 if (*pch == '\0')
2034 expr_error("character constant expected");
2035 n = *pch;
2036 pch++;
2037 if (*pch != '\'')
2038 expr_error("missing terminating \' character");
2039 next();
2040 break;
2041 case '$':
2043 char buf[128], *q;
2044 target_long reg=0;
2046 pch++;
2047 q = buf;
2048 while ((*pch >= 'a' && *pch <= 'z') ||
2049 (*pch >= 'A' && *pch <= 'Z') ||
2050 (*pch >= '0' && *pch <= '9') ||
2051 *pch == '_' || *pch == '.') {
2052 if ((q - buf) < sizeof(buf) - 1)
2053 *q++ = *pch;
2054 pch++;
2056 while (qemu_isspace(*pch))
2057 pch++;
2058 *q = 0;
2059 ret = get_monitor_def(&reg, buf);
2060 if (ret == -1)
2061 expr_error("unknown register");
2062 else if (ret == -2)
2063 expr_error("no cpu defined");
2064 n = reg;
2066 break;
2067 case '\0':
2068 expr_error("unexpected end of expression");
2069 n = 0;
2070 break;
2071 default:
2072 #if TARGET_PHYS_ADDR_BITS > 32
2073 n = strtoull(pch, &p, 0);
2074 #else
2075 n = strtoul(pch, &p, 0);
2076 #endif
2077 if (pch == p) {
2078 expr_error("invalid char in expression");
2080 pch = p;
2081 while (qemu_isspace(*pch))
2082 pch++;
2083 break;
2085 return n;
2089 static int64_t expr_prod(void)
2091 int64_t val, val2;
2092 int op;
2094 val = expr_unary();
2095 for(;;) {
2096 op = *pch;
2097 if (op != '*' && op != '/' && op != '%')
2098 break;
2099 next();
2100 val2 = expr_unary();
2101 switch(op) {
2102 default:
2103 case '*':
2104 val *= val2;
2105 break;
2106 case '/':
2107 case '%':
2108 if (val2 == 0)
2109 expr_error("division by zero");
2110 if (op == '/')
2111 val /= val2;
2112 else
2113 val %= val2;
2114 break;
2117 return val;
2120 static int64_t expr_logic(void)
2122 int64_t val, val2;
2123 int op;
2125 val = expr_prod();
2126 for(;;) {
2127 op = *pch;
2128 if (op != '&' && op != '|' && op != '^')
2129 break;
2130 next();
2131 val2 = expr_prod();
2132 switch(op) {
2133 default:
2134 case '&':
2135 val &= val2;
2136 break;
2137 case '|':
2138 val |= val2;
2139 break;
2140 case '^':
2141 val ^= val2;
2142 break;
2145 return val;
2148 static int64_t expr_sum(void)
2150 int64_t val, val2;
2151 int op;
2153 val = expr_logic();
2154 for(;;) {
2155 op = *pch;
2156 if (op != '+' && op != '-')
2157 break;
2158 next();
2159 val2 = expr_logic();
2160 if (op == '+')
2161 val += val2;
2162 else
2163 val -= val2;
2165 return val;
2168 static int get_expr(int64_t *pval, const char **pp)
2170 pch = *pp;
2171 if (setjmp(expr_env)) {
2172 *pp = pch;
2173 return -1;
2175 while (qemu_isspace(*pch))
2176 pch++;
2177 *pval = expr_sum();
2178 *pp = pch;
2179 return 0;
2182 static int get_str(char *buf, int buf_size, const char **pp)
2184 const char *p;
2185 char *q;
2186 int c;
2188 q = buf;
2189 p = *pp;
2190 while (qemu_isspace(*p))
2191 p++;
2192 if (*p == '\0') {
2193 fail:
2194 *q = '\0';
2195 *pp = p;
2196 return -1;
2198 if (*p == '\"') {
2199 p++;
2200 while (*p != '\0' && *p != '\"') {
2201 if (*p == '\\') {
2202 p++;
2203 c = *p++;
2204 switch(c) {
2205 case 'n':
2206 c = '\n';
2207 break;
2208 case 'r':
2209 c = '\r';
2210 break;
2211 case '\\':
2212 case '\'':
2213 case '\"':
2214 break;
2215 default:
2216 qemu_printf("unsupported escape code: '\\%c'\n", c);
2217 goto fail;
2219 if ((q - buf) < buf_size - 1) {
2220 *q++ = c;
2222 } else {
2223 if ((q - buf) < buf_size - 1) {
2224 *q++ = *p;
2226 p++;
2229 if (*p != '\"') {
2230 qemu_printf("unterminated string\n");
2231 goto fail;
2233 p++;
2234 } else {
2235 while (*p != '\0' && !qemu_isspace(*p)) {
2236 if ((q - buf) < buf_size - 1) {
2237 *q++ = *p;
2239 p++;
2242 *q = '\0';
2243 *pp = p;
2244 return 0;
2247 static int default_fmt_format = 'x';
2248 static int default_fmt_size = 4;
2250 #define MAX_ARGS 16
2252 static void monitor_handle_command(const char *cmdline)
2254 const char *p, *pstart, *typestr;
2255 char *q;
2256 int c, nb_args, len, i, has_arg;
2257 const term_cmd_t *cmd;
2258 char cmdname[256];
2259 char buf[1024];
2260 void *str_allocated[MAX_ARGS];
2261 void *args[MAX_ARGS];
2262 void (*handler_0)(void);
2263 void (*handler_1)(void *arg0);
2264 void (*handler_2)(void *arg0, void *arg1);
2265 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2266 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2267 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2268 void *arg4);
2269 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2270 void *arg4, void *arg5);
2271 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2272 void *arg4, void *arg5, void *arg6);
2274 #ifdef DEBUG
2275 term_printf("command='%s'\n", cmdline);
2276 #endif
2278 /* extract the command name */
2279 p = cmdline;
2280 q = cmdname;
2281 while (qemu_isspace(*p))
2282 p++;
2283 if (*p == '\0')
2284 return;
2285 pstart = p;
2286 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2287 p++;
2288 len = p - pstart;
2289 if (len > sizeof(cmdname) - 1)
2290 len = sizeof(cmdname) - 1;
2291 memcpy(cmdname, pstart, len);
2292 cmdname[len] = '\0';
2294 /* find the command */
2295 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2296 if (compare_cmd(cmdname, cmd->name))
2297 goto found;
2299 term_printf("unknown command: '%s'\n", cmdname);
2300 return;
2301 found:
2303 for(i = 0; i < MAX_ARGS; i++)
2304 str_allocated[i] = NULL;
2306 /* parse the parameters */
2307 typestr = cmd->args_type;
2308 nb_args = 0;
2309 for(;;) {
2310 c = *typestr;
2311 if (c == '\0')
2312 break;
2313 typestr++;
2314 switch(c) {
2315 case 'F':
2316 case 'B':
2317 case 's':
2319 int ret;
2320 char *str;
2322 while (qemu_isspace(*p))
2323 p++;
2324 if (*typestr == '?') {
2325 typestr++;
2326 if (*p == '\0') {
2327 /* no optional string: NULL argument */
2328 str = NULL;
2329 goto add_str;
2332 ret = get_str(buf, sizeof(buf), &p);
2333 if (ret < 0) {
2334 switch(c) {
2335 case 'F':
2336 term_printf("%s: filename expected\n", cmdname);
2337 break;
2338 case 'B':
2339 term_printf("%s: block device name expected\n", cmdname);
2340 break;
2341 default:
2342 term_printf("%s: string expected\n", cmdname);
2343 break;
2345 goto fail;
2347 str = qemu_malloc(strlen(buf) + 1);
2348 pstrcpy(str, sizeof(buf), buf);
2349 str_allocated[nb_args] = str;
2350 add_str:
2351 if (nb_args >= MAX_ARGS) {
2352 error_args:
2353 term_printf("%s: too many arguments\n", cmdname);
2354 goto fail;
2356 args[nb_args++] = str;
2358 break;
2359 case '/':
2361 int count, format, size;
2363 while (qemu_isspace(*p))
2364 p++;
2365 if (*p == '/') {
2366 /* format found */
2367 p++;
2368 count = 1;
2369 if (qemu_isdigit(*p)) {
2370 count = 0;
2371 while (qemu_isdigit(*p)) {
2372 count = count * 10 + (*p - '0');
2373 p++;
2376 size = -1;
2377 format = -1;
2378 for(;;) {
2379 switch(*p) {
2380 case 'o':
2381 case 'd':
2382 case 'u':
2383 case 'x':
2384 case 'i':
2385 case 'c':
2386 format = *p++;
2387 break;
2388 case 'b':
2389 size = 1;
2390 p++;
2391 break;
2392 case 'h':
2393 size = 2;
2394 p++;
2395 break;
2396 case 'w':
2397 size = 4;
2398 p++;
2399 break;
2400 case 'g':
2401 case 'L':
2402 size = 8;
2403 p++;
2404 break;
2405 default:
2406 goto next;
2409 next:
2410 if (*p != '\0' && !qemu_isspace(*p)) {
2411 term_printf("invalid char in format: '%c'\n", *p);
2412 goto fail;
2414 if (format < 0)
2415 format = default_fmt_format;
2416 if (format != 'i') {
2417 /* for 'i', not specifying a size gives -1 as size */
2418 if (size < 0)
2419 size = default_fmt_size;
2420 default_fmt_size = size;
2422 default_fmt_format = format;
2423 } else {
2424 count = 1;
2425 format = default_fmt_format;
2426 if (format != 'i') {
2427 size = default_fmt_size;
2428 } else {
2429 size = -1;
2432 if (nb_args + 3 > MAX_ARGS)
2433 goto error_args;
2434 args[nb_args++] = (void*)(long)count;
2435 args[nb_args++] = (void*)(long)format;
2436 args[nb_args++] = (void*)(long)size;
2438 break;
2439 case 'i':
2440 case 'l':
2442 int64_t val;
2444 while (qemu_isspace(*p))
2445 p++;
2446 if (*typestr == '?' || *typestr == '.') {
2447 if (*typestr == '?') {
2448 if (*p == '\0')
2449 has_arg = 0;
2450 else
2451 has_arg = 1;
2452 } else {
2453 if (*p == '.') {
2454 p++;
2455 while (qemu_isspace(*p))
2456 p++;
2457 has_arg = 1;
2458 } else {
2459 has_arg = 0;
2462 typestr++;
2463 if (nb_args >= MAX_ARGS)
2464 goto error_args;
2465 args[nb_args++] = (void *)(long)has_arg;
2466 if (!has_arg) {
2467 if (nb_args >= MAX_ARGS)
2468 goto error_args;
2469 val = -1;
2470 goto add_num;
2473 if (get_expr(&val, &p))
2474 goto fail;
2475 add_num:
2476 if (c == 'i') {
2477 if (nb_args >= MAX_ARGS)
2478 goto error_args;
2479 args[nb_args++] = (void *)(long)val;
2480 } else {
2481 if ((nb_args + 1) >= MAX_ARGS)
2482 goto error_args;
2483 #if TARGET_PHYS_ADDR_BITS > 32
2484 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2485 #else
2486 args[nb_args++] = (void *)0;
2487 #endif
2488 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2491 break;
2492 case '-':
2494 int has_option;
2495 /* option */
2497 c = *typestr++;
2498 if (c == '\0')
2499 goto bad_type;
2500 while (qemu_isspace(*p))
2501 p++;
2502 has_option = 0;
2503 if (*p == '-') {
2504 p++;
2505 if (*p != c) {
2506 term_printf("%s: unsupported option -%c\n",
2507 cmdname, *p);
2508 goto fail;
2510 p++;
2511 has_option = 1;
2513 if (nb_args >= MAX_ARGS)
2514 goto error_args;
2515 args[nb_args++] = (void *)(long)has_option;
2517 break;
2518 default:
2519 bad_type:
2520 term_printf("%s: unknown type '%c'\n", cmdname, c);
2521 goto fail;
2524 /* check that all arguments were parsed */
2525 while (qemu_isspace(*p))
2526 p++;
2527 if (*p != '\0') {
2528 term_printf("%s: extraneous characters at the end of line\n",
2529 cmdname);
2530 goto fail;
2533 switch(nb_args) {
2534 case 0:
2535 handler_0 = cmd->handler;
2536 handler_0();
2537 break;
2538 case 1:
2539 handler_1 = cmd->handler;
2540 handler_1(args[0]);
2541 break;
2542 case 2:
2543 handler_2 = cmd->handler;
2544 handler_2(args[0], args[1]);
2545 break;
2546 case 3:
2547 handler_3 = cmd->handler;
2548 handler_3(args[0], args[1], args[2]);
2549 break;
2550 case 4:
2551 handler_4 = cmd->handler;
2552 handler_4(args[0], args[1], args[2], args[3]);
2553 break;
2554 case 5:
2555 handler_5 = cmd->handler;
2556 handler_5(args[0], args[1], args[2], args[3], args[4]);
2557 break;
2558 case 6:
2559 handler_6 = cmd->handler;
2560 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2561 break;
2562 case 7:
2563 handler_7 = cmd->handler;
2564 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2565 break;
2566 default:
2567 term_printf("unsupported number of arguments: %d\n", nb_args);
2568 goto fail;
2570 fail:
2571 for(i = 0; i < MAX_ARGS; i++)
2572 qemu_free(str_allocated[i]);
2573 return;
2576 static void cmd_completion(const char *name, const char *list)
2578 const char *p, *pstart;
2579 char cmd[128];
2580 int len;
2582 p = list;
2583 for(;;) {
2584 pstart = p;
2585 p = strchr(p, '|');
2586 if (!p)
2587 p = pstart + strlen(pstart);
2588 len = p - pstart;
2589 if (len > sizeof(cmd) - 2)
2590 len = sizeof(cmd) - 2;
2591 memcpy(cmd, pstart, len);
2592 cmd[len] = '\0';
2593 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2594 add_completion(cmd);
2596 if (*p == '\0')
2597 break;
2598 p++;
2602 static void file_completion(const char *input)
2604 DIR *ffs;
2605 struct dirent *d;
2606 char path[1024];
2607 char file[1024], file_prefix[1024];
2608 int input_path_len;
2609 const char *p;
2611 p = strrchr(input, '/');
2612 if (!p) {
2613 input_path_len = 0;
2614 pstrcpy(file_prefix, sizeof(file_prefix), input);
2615 pstrcpy(path, sizeof(path), ".");
2616 } else {
2617 input_path_len = p - input + 1;
2618 memcpy(path, input, input_path_len);
2619 if (input_path_len > sizeof(path) - 1)
2620 input_path_len = sizeof(path) - 1;
2621 path[input_path_len] = '\0';
2622 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2624 #ifdef DEBUG_COMPLETION
2625 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2626 #endif
2627 ffs = opendir(path);
2628 if (!ffs)
2629 return;
2630 for(;;) {
2631 struct stat sb;
2632 d = readdir(ffs);
2633 if (!d)
2634 break;
2635 if (strstart(d->d_name, file_prefix, NULL)) {
2636 memcpy(file, input, input_path_len);
2637 if (input_path_len < sizeof(file))
2638 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2639 d->d_name);
2640 /* stat the file to find out if it's a directory.
2641 * In that case add a slash to speed up typing long paths
2643 stat(file, &sb);
2644 if(S_ISDIR(sb.st_mode))
2645 pstrcat(file, sizeof(file), "/");
2646 add_completion(file);
2649 closedir(ffs);
2652 static void block_completion_it(void *opaque, const char *name)
2654 const char *input = opaque;
2656 if (input[0] == '\0' ||
2657 !strncmp(name, (char *)input, strlen(input))) {
2658 add_completion(name);
2662 /* NOTE: this parser is an approximate form of the real command parser */
2663 static void parse_cmdline(const char *cmdline,
2664 int *pnb_args, char **args)
2666 const char *p;
2667 int nb_args, ret;
2668 char buf[1024];
2670 p = cmdline;
2671 nb_args = 0;
2672 for(;;) {
2673 while (qemu_isspace(*p))
2674 p++;
2675 if (*p == '\0')
2676 break;
2677 if (nb_args >= MAX_ARGS)
2678 break;
2679 ret = get_str(buf, sizeof(buf), &p);
2680 args[nb_args] = qemu_strdup(buf);
2681 nb_args++;
2682 if (ret < 0)
2683 break;
2685 *pnb_args = nb_args;
2688 void readline_find_completion(const char *cmdline)
2690 const char *cmdname;
2691 char *args[MAX_ARGS];
2692 int nb_args, i, len;
2693 const char *ptype, *str;
2694 const term_cmd_t *cmd;
2695 const KeyDef *key;
2697 parse_cmdline(cmdline, &nb_args, args);
2698 #ifdef DEBUG_COMPLETION
2699 for(i = 0; i < nb_args; i++) {
2700 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2702 #endif
2704 /* if the line ends with a space, it means we want to complete the
2705 next arg */
2706 len = strlen(cmdline);
2707 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2708 if (nb_args >= MAX_ARGS)
2709 return;
2710 args[nb_args++] = qemu_strdup("");
2712 if (nb_args <= 1) {
2713 /* command completion */
2714 if (nb_args == 0)
2715 cmdname = "";
2716 else
2717 cmdname = args[0];
2718 completion_index = strlen(cmdname);
2719 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2720 cmd_completion(cmdname, cmd->name);
2722 } else {
2723 /* find the command */
2724 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2725 if (compare_cmd(args[0], cmd->name))
2726 goto found;
2728 return;
2729 found:
2730 ptype = cmd->args_type;
2731 for(i = 0; i < nb_args - 2; i++) {
2732 if (*ptype != '\0') {
2733 ptype++;
2734 while (*ptype == '?')
2735 ptype++;
2738 str = args[nb_args - 1];
2739 switch(*ptype) {
2740 case 'F':
2741 /* file completion */
2742 completion_index = strlen(str);
2743 file_completion(str);
2744 break;
2745 case 'B':
2746 /* block device name completion */
2747 completion_index = strlen(str);
2748 bdrv_iterate(block_completion_it, (void *)str);
2749 break;
2750 case 's':
2751 /* XXX: more generic ? */
2752 if (!strcmp(cmd->name, "info")) {
2753 completion_index = strlen(str);
2754 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2755 cmd_completion(str, cmd->name);
2757 } else if (!strcmp(cmd->name, "sendkey")) {
2758 completion_index = strlen(str);
2759 for(key = key_defs; key->name != NULL; key++) {
2760 cmd_completion(str, key->name);
2763 break;
2764 default:
2765 break;
2768 for(i = 0; i < nb_args; i++)
2769 qemu_free(args[i]);
2772 static int term_can_read(void *opaque)
2774 return 128;
2777 static void term_read(void *opaque, const uint8_t *buf, int size)
2779 int i;
2780 for(i = 0; i < size; i++)
2781 readline_handle_byte(buf[i]);
2784 static int monitor_suspended;
2786 static void monitor_handle_command1(void *opaque, const char *cmdline)
2788 monitor_handle_command(cmdline);
2789 if (!monitor_suspended)
2790 monitor_start_input();
2791 else
2792 monitor_suspended = 2;
2795 void monitor_suspend(void)
2797 monitor_suspended = 1;
2800 void monitor_resume(void)
2802 if (monitor_suspended == 2)
2803 monitor_start_input();
2804 monitor_suspended = 0;
2807 static void monitor_start_input(void)
2809 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2812 static void term_event(void *opaque, int event)
2814 if (event != CHR_EVENT_RESET)
2815 return;
2817 if (!hide_banner)
2818 term_printf("QEMU %s monitor - type 'help' for more information\n",
2819 QEMU_VERSION);
2820 monitor_start_input();
2823 static int is_first_init = 1;
2825 void monitor_init(CharDriverState *hd, int show_banner)
2827 int i;
2829 if (is_first_init) {
2830 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2831 if (!key_timer)
2832 return;
2833 for (i = 0; i < MAX_MON; i++) {
2834 monitor_hd[i] = NULL;
2836 is_first_init = 0;
2838 for (i = 0; i < MAX_MON; i++) {
2839 if (monitor_hd[i] == NULL) {
2840 monitor_hd[i] = hd;
2841 break;
2845 hide_banner = !show_banner;
2847 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2849 readline_start("", 0, monitor_handle_command1, NULL);
2852 /* XXX: use threads ? */
2853 /* modal monitor readline */
2854 static int monitor_readline_started;
2855 static char *monitor_readline_buf;
2856 static int monitor_readline_buf_size;
2858 static void monitor_readline_cb(void *opaque, const char *input)
2860 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2861 monitor_readline_started = 0;
2864 void monitor_readline(const char *prompt, int is_password,
2865 char *buf, int buf_size)
2867 int i;
2868 int old_focus[MAX_MON];
2870 if (is_password) {
2871 for (i = 0; i < MAX_MON; i++) {
2872 old_focus[i] = 0;
2873 if (monitor_hd[i]) {
2874 old_focus[i] = monitor_hd[i]->focus;
2875 monitor_hd[i]->focus = 0;
2876 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2881 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2882 monitor_readline_buf = buf;
2883 monitor_readline_buf_size = buf_size;
2884 monitor_readline_started = 1;
2885 while (monitor_readline_started) {
2886 main_loop_wait(10);
2888 /* restore original focus */
2889 if (is_password) {
2890 for (i = 0; i < MAX_MON; i++)
2891 if (old_focus[i])
2892 monitor_hd[i]->focus = old_focus[i];