kvm: testsuite: test 'pop mem' instruction
[qemu-kvm/fedora.git] / monitor.c
blob87ad3a75c25b5628fb565ab38c51732e4f0f6a8f
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 "migration.h"
38 #include "balloon.h"
39 #include <dirent.h>
40 #include "qemu-timer.h"
41 #include "migration.h"
42 #include "kvm.h"
44 #include "qemu-kvm.h"
46 //#define DEBUG
47 //#define DEBUG_COMPLETION
50 * Supported types:
52 * 'F' filename
53 * 'B' block device name
54 * 's' string (accept optional quote)
55 * 'i' 32 bit integer
56 * 'l' target long (32 or 64 bit)
57 * '/' optional gdb-like print format (like "/10x")
59 * '?' optional type (for 'F', 's' and 'i')
63 typedef struct term_cmd_t {
64 const char *name;
65 const char *args_type;
66 void *handler;
67 const char *params;
68 const char *help;
69 } term_cmd_t;
71 #define MAX_MON 4
72 static CharDriverState *monitor_hd[MAX_MON];
73 static int hide_banner;
75 static const term_cmd_t term_cmds[];
76 static const term_cmd_t info_cmds[];
78 static uint8_t term_outbuf[1024];
79 static int term_outbuf_index;
81 static void monitor_start_input(void);
83 static CPUState *mon_cpu = NULL;
85 void term_flush(void)
87 int i;
88 if (term_outbuf_index > 0) {
89 for (i = 0; i < MAX_MON; i++)
90 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
91 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
92 term_outbuf_index = 0;
96 /* flush at every end of line or if the buffer is full */
97 void term_puts(const char *str)
99 char c;
100 for(;;) {
101 c = *str++;
102 if (c == '\0')
103 break;
104 if (c == '\n')
105 term_outbuf[term_outbuf_index++] = '\r';
106 term_outbuf[term_outbuf_index++] = c;
107 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
108 c == '\n')
109 term_flush();
113 void term_vprintf(const char *fmt, va_list ap)
115 char buf[4096];
116 vsnprintf(buf, sizeof(buf), fmt, ap);
117 term_puts(buf);
120 void term_printf(const char *fmt, ...)
122 va_list ap;
123 va_start(ap, fmt);
124 term_vprintf(fmt, ap);
125 va_end(ap);
128 void term_print_filename(const char *filename)
130 int i;
132 for (i = 0; filename[i]; i++) {
133 switch (filename[i]) {
134 case ' ':
135 case '"':
136 case '\\':
137 term_printf("\\%c", filename[i]);
138 break;
139 case '\t':
140 term_printf("\\t");
141 break;
142 case '\r':
143 term_printf("\\r");
144 break;
145 case '\n':
146 term_printf("\\n");
147 break;
148 default:
149 term_printf("%c", filename[i]);
150 break;
155 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
157 va_list ap;
158 va_start(ap, fmt);
159 term_vprintf(fmt, ap);
160 va_end(ap);
161 return 0;
164 static int compare_cmd(const char *name, const char *list)
166 const char *p, *pstart;
167 int len;
168 len = strlen(name);
169 p = list;
170 for(;;) {
171 pstart = p;
172 p = strchr(p, '|');
173 if (!p)
174 p = pstart + strlen(pstart);
175 if ((p - pstart) == len && !memcmp(pstart, name, len))
176 return 1;
177 if (*p == '\0')
178 break;
179 p++;
181 return 0;
184 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
186 const term_cmd_t *cmd;
188 for(cmd = cmds; cmd->name != NULL; cmd++) {
189 if (!name || !strcmp(name, cmd->name))
190 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
194 static void help_cmd(const char *name)
196 if (name && !strcmp(name, "info")) {
197 help_cmd1(info_cmds, "info ", NULL);
198 } else {
199 help_cmd1(term_cmds, "", name);
200 if (name && !strcmp(name, "log")) {
201 const CPULogItem *item;
202 term_printf("Log items (comma separated):\n");
203 term_printf("%-10s %s\n", "none", "remove all logs");
204 for(item = cpu_log_items; item->mask != 0; item++) {
205 term_printf("%-10s %s\n", item->name, item->help);
211 static void do_help(const char *name)
213 help_cmd(name);
216 static void do_commit(const char *device)
218 int i, all_devices;
220 all_devices = !strcmp(device, "all");
221 for (i = 0; i < nb_drives; i++) {
222 if (all_devices ||
223 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
224 bdrv_commit(drives_table[i].bdrv);
228 static void do_info(const char *item)
230 const term_cmd_t *cmd;
231 void (*handler)(void);
233 if (!item)
234 goto help;
235 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
236 if (compare_cmd(item, cmd->name))
237 goto found;
239 help:
240 help_cmd("info");
241 return;
242 found:
243 handler = cmd->handler;
244 handler();
247 static void do_info_version(void)
249 term_printf("%s\n", QEMU_VERSION);
252 static void do_info_name(void)
254 if (qemu_name)
255 term_printf("%s\n", qemu_name);
258 static void do_info_uuid(void)
260 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
261 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
262 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
263 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
264 qemu_uuid[15]);
267 static void do_info_block(void)
269 bdrv_info();
272 static void do_info_blockstats(void)
274 bdrv_info_stats();
277 /* get the current CPU defined by the user */
278 static int mon_set_cpu(int cpu_index)
280 CPUState *env;
282 for(env = first_cpu; env != NULL; env = env->next_cpu) {
283 if (env->cpu_index == cpu_index) {
284 mon_cpu = env;
285 return 0;
288 return -1;
291 static CPUState *mon_get_cpu(void)
293 if (!mon_cpu) {
294 mon_set_cpu(0);
297 kvm_save_registers(mon_cpu);
299 return mon_cpu;
302 static void do_info_registers(void)
304 CPUState *env;
305 env = mon_get_cpu();
306 if (!env)
307 return;
308 #ifdef TARGET_I386
309 cpu_dump_state(env, NULL, monitor_fprintf,
310 X86_DUMP_FPU);
311 #else
312 cpu_dump_state(env, NULL, monitor_fprintf,
314 #endif
317 static void do_info_cpus(void)
319 CPUState *env;
321 /* just to set the default cpu if not already done */
322 mon_get_cpu();
324 for(env = first_cpu; env != NULL; env = env->next_cpu) {
325 kvm_save_registers(env);
326 term_printf("%c CPU #%d:",
327 (env == mon_cpu) ? '*' : ' ',
328 env->cpu_index);
329 #if defined(TARGET_I386)
330 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
331 #elif defined(TARGET_PPC)
332 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
333 #elif defined(TARGET_SPARC)
334 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
335 #elif defined(TARGET_MIPS)
336 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
337 #endif
338 if (env->halted)
339 term_printf(" (halted)");
340 term_printf(" thread_id=%d", env->thread_id);
341 term_printf("\n");
345 static void do_cpu_set(int index)
347 if (mon_set_cpu(index) < 0)
348 term_printf("Invalid CPU index\n");
351 static void do_cpu_set_nr(int value, const char *status)
353 int state;
355 if (!strcmp(status, "online"))
356 state = 1;
357 else if (!strcmp(status, "offline"))
358 state = 0;
359 else {
360 term_printf("invalid status: %s\n", status);
361 return;
363 #if defined(TARGET_I386) || defined(TARGET_X86_64)
364 qemu_system_cpu_hot_add(value, state);
365 #endif
368 static void do_info_jit(void)
370 dump_exec_info(NULL, monitor_fprintf);
373 static void do_info_history (void)
375 int i;
376 const char *str;
378 i = 0;
379 for(;;) {
380 str = readline_get_history(i);
381 if (!str)
382 break;
383 term_printf("%d: '%s'\n", i, str);
384 i++;
388 #if defined(TARGET_PPC)
389 /* XXX: not implemented in other targets */
390 static void do_info_cpu_stats (void)
392 CPUState *env;
394 env = mon_get_cpu();
395 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
397 #endif
399 static void do_quit(void)
401 exit(0);
404 static int eject_device(BlockDriverState *bs, int force)
406 if (bdrv_is_inserted(bs)) {
407 if (!force) {
408 if (!bdrv_is_removable(bs)) {
409 term_printf("device is not removable\n");
410 return -1;
412 if (bdrv_is_locked(bs)) {
413 term_printf("device is locked\n");
414 return -1;
417 bdrv_close(bs);
419 return 0;
422 static void do_eject(int force, const char *filename)
424 BlockDriverState *bs;
426 bs = bdrv_find(filename);
427 if (!bs) {
428 term_printf("device not found\n");
429 return;
431 eject_device(bs, force);
434 static void do_change_block(const char *device, const char *filename, const char *fmt)
436 BlockDriverState *bs;
437 BlockDriver *drv = NULL;
439 bs = bdrv_find(device);
440 if (!bs) {
441 term_printf("device not found\n");
442 return;
444 if (fmt) {
445 drv = bdrv_find_format(fmt);
446 if (!drv) {
447 term_printf("invalid format %s\n", fmt);
448 return;
451 if (eject_device(bs, 0) < 0)
452 return;
453 bdrv_open2(bs, filename, 0, drv);
454 qemu_key_check(bs, filename);
457 static void do_change_vnc(const char *target)
459 if (strcmp(target, "passwd") == 0 ||
460 strcmp(target, "password") == 0) {
461 char password[9];
462 monitor_readline("Password: ", 1, password, sizeof(password)-1);
463 password[sizeof(password)-1] = '\0';
464 if (vnc_display_password(NULL, password) < 0)
465 term_printf("could not set VNC server password\n");
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 *fmt)
474 if (strcmp(device, "vnc") == 0) {
475 do_change_vnc(target);
476 } else {
477 do_change_block(device, target, fmt);
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 do_cont(void)
514 vm_start();
517 #ifdef CONFIG_GDBSTUB
518 static void do_gdbserver(const char *port)
520 if (!port)
521 port = DEFAULT_GDBSTUB_PORT;
522 if (gdbserver_start(port) < 0) {
523 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
524 } else {
525 qemu_printf("Waiting gdb connection on port '%s'\n", port);
528 #endif
530 static void term_printc(int c)
532 term_printf("'");
533 switch(c) {
534 case '\'':
535 term_printf("\\'");
536 break;
537 case '\\':
538 term_printf("\\\\");
539 break;
540 case '\n':
541 term_printf("\\n");
542 break;
543 case '\r':
544 term_printf("\\r");
545 break;
546 default:
547 if (c >= 32 && c <= 126) {
548 term_printf("%c", c);
549 } else {
550 term_printf("\\x%02x", c);
552 break;
554 term_printf("'");
557 static void memory_dump(int count, int format, int wsize,
558 target_phys_addr_t addr, int is_physical)
560 CPUState *env;
561 int nb_per_line, l, line_size, i, max_digits, len;
562 uint8_t buf[16];
563 uint64_t v;
565 if (format == 'i') {
566 int flags;
567 flags = 0;
568 env = mon_get_cpu();
569 if (!env && !is_physical)
570 return;
571 #ifdef TARGET_I386
572 if (wsize == 2) {
573 flags = 1;
574 } else if (wsize == 4) {
575 flags = 0;
576 } else {
577 /* as default we use the current CS size */
578 flags = 0;
579 if (env) {
580 #ifdef TARGET_X86_64
581 if ((env->efer & MSR_EFER_LMA) &&
582 (env->segs[R_CS].flags & DESC_L_MASK))
583 flags = 2;
584 else
585 #endif
586 if (!(env->segs[R_CS].flags & DESC_B_MASK))
587 flags = 1;
590 #endif
591 monitor_disas(env, addr, count, is_physical, flags);
592 return;
595 len = wsize * count;
596 if (wsize == 1)
597 line_size = 8;
598 else
599 line_size = 16;
600 nb_per_line = line_size / wsize;
601 max_digits = 0;
603 switch(format) {
604 case 'o':
605 max_digits = (wsize * 8 + 2) / 3;
606 break;
607 default:
608 case 'x':
609 max_digits = (wsize * 8) / 4;
610 break;
611 case 'u':
612 case 'd':
613 max_digits = (wsize * 8 * 10 + 32) / 33;
614 break;
615 case 'c':
616 wsize = 1;
617 break;
620 while (len > 0) {
621 if (is_physical)
622 term_printf(TARGET_FMT_plx ":", addr);
623 else
624 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
625 l = len;
626 if (l > line_size)
627 l = line_size;
628 if (is_physical) {
629 cpu_physical_memory_rw(addr, buf, l, 0);
630 } else {
631 env = mon_get_cpu();
632 if (!env)
633 break;
634 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
635 term_printf(" Cannot access memory\n");
636 break;
639 i = 0;
640 while (i < l) {
641 switch(wsize) {
642 default:
643 case 1:
644 v = ldub_raw(buf + i);
645 break;
646 case 2:
647 v = lduw_raw(buf + i);
648 break;
649 case 4:
650 v = (uint32_t)ldl_raw(buf + i);
651 break;
652 case 8:
653 v = ldq_raw(buf + i);
654 break;
656 term_printf(" ");
657 switch(format) {
658 case 'o':
659 term_printf("%#*" PRIo64, max_digits, v);
660 break;
661 case 'x':
662 term_printf("0x%0*" PRIx64, max_digits, v);
663 break;
664 case 'u':
665 term_printf("%*" PRIu64, max_digits, v);
666 break;
667 case 'd':
668 term_printf("%*" PRId64, max_digits, v);
669 break;
670 case 'c':
671 term_printc(v);
672 break;
674 i += wsize;
676 term_printf("\n");
677 addr += l;
678 len -= l;
682 #if TARGET_LONG_BITS == 64
683 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
684 #else
685 #define GET_TLONG(h, l) (l)
686 #endif
688 static void do_memory_dump(int count, int format, int size,
689 uint32_t addrh, uint32_t addrl)
691 target_long addr = GET_TLONG(addrh, addrl);
692 memory_dump(count, format, size, addr, 0);
695 #if TARGET_PHYS_ADDR_BITS > 32
696 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
697 #else
698 #define GET_TPHYSADDR(h, l) (l)
699 #endif
701 static void do_physical_memory_dump(int count, int format, int size,
702 uint32_t addrh, uint32_t addrl)
705 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
706 memory_dump(count, format, size, addr, 1);
709 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
711 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
712 #if TARGET_PHYS_ADDR_BITS == 32
713 switch(format) {
714 case 'o':
715 term_printf("%#o", val);
716 break;
717 case 'x':
718 term_printf("%#x", val);
719 break;
720 case 'u':
721 term_printf("%u", val);
722 break;
723 default:
724 case 'd':
725 term_printf("%d", val);
726 break;
727 case 'c':
728 term_printc(val);
729 break;
731 #else
732 switch(format) {
733 case 'o':
734 term_printf("%#" PRIo64, val);
735 break;
736 case 'x':
737 term_printf("%#" PRIx64, val);
738 break;
739 case 'u':
740 term_printf("%" PRIu64, val);
741 break;
742 default:
743 case 'd':
744 term_printf("%" PRId64, val);
745 break;
746 case 'c':
747 term_printc(val);
748 break;
750 #endif
751 term_printf("\n");
754 static void do_memory_save(unsigned int valh, unsigned int vall,
755 uint32_t size, const char *filename)
757 FILE *f;
758 target_long addr = GET_TLONG(valh, vall);
759 uint32_t l;
760 CPUState *env;
761 uint8_t buf[1024];
763 env = mon_get_cpu();
764 if (!env)
765 return;
767 f = fopen(filename, "wb");
768 if (!f) {
769 term_printf("could not open '%s'\n", filename);
770 return;
772 while (size != 0) {
773 l = sizeof(buf);
774 if (l > size)
775 l = size;
776 cpu_memory_rw_debug(env, addr, buf, l, 0);
777 fwrite(buf, 1, l, f);
778 addr += l;
779 size -= l;
781 fclose(f);
784 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
785 uint32_t size, const char *filename)
787 FILE *f;
788 uint32_t l;
789 uint8_t buf[1024];
790 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
792 f = fopen(filename, "wb");
793 if (!f) {
794 term_printf("could not open '%s'\n", filename);
795 return;
797 while (size != 0) {
798 l = sizeof(buf);
799 if (l > size)
800 l = size;
801 cpu_physical_memory_rw(addr, buf, l, 0);
802 fwrite(buf, 1, l, f);
803 fflush(f);
804 addr += l;
805 size -= l;
807 fclose(f);
810 static void do_sum(uint32_t start, uint32_t size)
812 uint32_t addr;
813 uint8_t buf[1];
814 uint16_t sum;
816 sum = 0;
817 for(addr = start; addr < (start + size); addr++) {
818 cpu_physical_memory_rw(addr, buf, 1, 0);
819 /* BSD sum algorithm ('sum' Unix command) */
820 sum = (sum >> 1) | (sum << 15);
821 sum += buf[0];
823 term_printf("%05d\n", sum);
826 typedef struct {
827 int keycode;
828 const char *name;
829 } KeyDef;
831 static const KeyDef key_defs[] = {
832 { 0x2a, "shift" },
833 { 0x36, "shift_r" },
835 { 0x38, "alt" },
836 { 0xb8, "alt_r" },
837 { 0x64, "altgr" },
838 { 0xe4, "altgr_r" },
839 { 0x1d, "ctrl" },
840 { 0x9d, "ctrl_r" },
842 { 0xdd, "menu" },
844 { 0x01, "esc" },
846 { 0x02, "1" },
847 { 0x03, "2" },
848 { 0x04, "3" },
849 { 0x05, "4" },
850 { 0x06, "5" },
851 { 0x07, "6" },
852 { 0x08, "7" },
853 { 0x09, "8" },
854 { 0x0a, "9" },
855 { 0x0b, "0" },
856 { 0x0c, "minus" },
857 { 0x0d, "equal" },
858 { 0x0e, "backspace" },
860 { 0x0f, "tab" },
861 { 0x10, "q" },
862 { 0x11, "w" },
863 { 0x12, "e" },
864 { 0x13, "r" },
865 { 0x14, "t" },
866 { 0x15, "y" },
867 { 0x16, "u" },
868 { 0x17, "i" },
869 { 0x18, "o" },
870 { 0x19, "p" },
872 { 0x1c, "ret" },
874 { 0x1e, "a" },
875 { 0x1f, "s" },
876 { 0x20, "d" },
877 { 0x21, "f" },
878 { 0x22, "g" },
879 { 0x23, "h" },
880 { 0x24, "j" },
881 { 0x25, "k" },
882 { 0x26, "l" },
884 { 0x2c, "z" },
885 { 0x2d, "x" },
886 { 0x2e, "c" },
887 { 0x2f, "v" },
888 { 0x30, "b" },
889 { 0x31, "n" },
890 { 0x32, "m" },
891 { 0x33, "comma" },
892 { 0x34, "dot" },
893 { 0x35, "slash" },
895 { 0x37, "asterisk" },
897 { 0x39, "spc" },
898 { 0x3a, "caps_lock" },
899 { 0x3b, "f1" },
900 { 0x3c, "f2" },
901 { 0x3d, "f3" },
902 { 0x3e, "f4" },
903 { 0x3f, "f5" },
904 { 0x40, "f6" },
905 { 0x41, "f7" },
906 { 0x42, "f8" },
907 { 0x43, "f9" },
908 { 0x44, "f10" },
909 { 0x45, "num_lock" },
910 { 0x46, "scroll_lock" },
912 { 0xb5, "kp_divide" },
913 { 0x37, "kp_multiply" },
914 { 0x4a, "kp_subtract" },
915 { 0x4e, "kp_add" },
916 { 0x9c, "kp_enter" },
917 { 0x53, "kp_decimal" },
918 { 0x54, "sysrq" },
920 { 0x52, "kp_0" },
921 { 0x4f, "kp_1" },
922 { 0x50, "kp_2" },
923 { 0x51, "kp_3" },
924 { 0x4b, "kp_4" },
925 { 0x4c, "kp_5" },
926 { 0x4d, "kp_6" },
927 { 0x47, "kp_7" },
928 { 0x48, "kp_8" },
929 { 0x49, "kp_9" },
931 { 0x56, "<" },
933 { 0x57, "f11" },
934 { 0x58, "f12" },
936 { 0xb7, "print" },
938 { 0xc7, "home" },
939 { 0xc9, "pgup" },
940 { 0xd1, "pgdn" },
941 { 0xcf, "end" },
943 { 0xcb, "left" },
944 { 0xc8, "up" },
945 { 0xd0, "down" },
946 { 0xcd, "right" },
948 { 0xd2, "insert" },
949 { 0xd3, "delete" },
950 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
951 { 0xf0, "stop" },
952 { 0xf1, "again" },
953 { 0xf2, "props" },
954 { 0xf3, "undo" },
955 { 0xf4, "front" },
956 { 0xf5, "copy" },
957 { 0xf6, "open" },
958 { 0xf7, "paste" },
959 { 0xf8, "find" },
960 { 0xf9, "cut" },
961 { 0xfa, "lf" },
962 { 0xfb, "help" },
963 { 0xfc, "meta_l" },
964 { 0xfd, "meta_r" },
965 { 0xfe, "compose" },
966 #endif
967 { 0, NULL },
970 static int get_keycode(const char *key)
972 const KeyDef *p;
973 char *endp;
974 int ret;
976 for(p = key_defs; p->name != NULL; p++) {
977 if (!strcmp(key, p->name))
978 return p->keycode;
980 if (strstart(key, "0x", NULL)) {
981 ret = strtoul(key, &endp, 0);
982 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
983 return ret;
985 return -1;
988 #define MAX_KEYCODES 16
989 static uint8_t keycodes[MAX_KEYCODES];
990 static int nb_pending_keycodes;
991 static QEMUTimer *key_timer;
993 static void release_keys(void *opaque)
995 int keycode;
997 while (nb_pending_keycodes > 0) {
998 nb_pending_keycodes--;
999 keycode = keycodes[nb_pending_keycodes];
1000 if (keycode & 0x80)
1001 kbd_put_keycode(0xe0);
1002 kbd_put_keycode(keycode | 0x80);
1006 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1008 char keyname_buf[16];
1009 char *separator;
1010 int keyname_len, keycode, i;
1012 if (nb_pending_keycodes > 0) {
1013 qemu_del_timer(key_timer);
1014 release_keys(NULL);
1016 if (!has_hold_time)
1017 hold_time = 100;
1018 i = 0;
1019 while (1) {
1020 separator = strchr(string, '-');
1021 keyname_len = separator ? separator - string : strlen(string);
1022 if (keyname_len > 0) {
1023 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1024 if (keyname_len > sizeof(keyname_buf) - 1) {
1025 term_printf("invalid key: '%s...'\n", keyname_buf);
1026 return;
1028 if (i == MAX_KEYCODES) {
1029 term_printf("too many keys\n");
1030 return;
1032 keyname_buf[keyname_len] = 0;
1033 keycode = get_keycode(keyname_buf);
1034 if (keycode < 0) {
1035 term_printf("unknown key: '%s'\n", keyname_buf);
1036 return;
1038 keycodes[i++] = keycode;
1040 if (!separator)
1041 break;
1042 string = separator + 1;
1044 nb_pending_keycodes = i;
1045 /* key down events */
1046 for (i = 0; i < nb_pending_keycodes; i++) {
1047 keycode = keycodes[i];
1048 if (keycode & 0x80)
1049 kbd_put_keycode(0xe0);
1050 kbd_put_keycode(keycode & 0x7f);
1052 /* delayed key up events */
1053 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1054 muldiv64(ticks_per_sec, hold_time, 1000));
1057 static int mouse_button_state;
1059 static void do_mouse_move(const char *dx_str, const char *dy_str,
1060 const char *dz_str)
1062 int dx, dy, dz;
1063 dx = strtol(dx_str, NULL, 0);
1064 dy = strtol(dy_str, NULL, 0);
1065 dz = 0;
1066 if (dz_str)
1067 dz = strtol(dz_str, NULL, 0);
1068 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1071 static void do_mouse_button(int button_state)
1073 mouse_button_state = button_state;
1074 kbd_mouse_event(0, 0, 0, mouse_button_state);
1077 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1079 uint32_t val;
1080 int suffix;
1082 if (has_index) {
1083 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1084 addr++;
1086 addr &= 0xffff;
1088 switch(size) {
1089 default:
1090 case 1:
1091 val = cpu_inb(NULL, addr);
1092 suffix = 'b';
1093 break;
1094 case 2:
1095 val = cpu_inw(NULL, addr);
1096 suffix = 'w';
1097 break;
1098 case 4:
1099 val = cpu_inl(NULL, addr);
1100 suffix = 'l';
1101 break;
1103 term_printf("port%c[0x%04x] = %#0*x\n",
1104 suffix, addr, size * 2, val);
1107 /* boot_set handler */
1108 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1109 static void *boot_opaque;
1111 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1113 qemu_boot_set_handler = func;
1114 boot_opaque = opaque;
1117 static void do_boot_set(const char *bootdevice)
1119 int res;
1121 if (qemu_boot_set_handler) {
1122 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1123 if (res == 0)
1124 term_printf("boot device list now set to %s\n", bootdevice);
1125 else
1126 term_printf("setting boot device list failed with error %i\n", res);
1127 } else {
1128 term_printf("no function defined to set boot device list for this architecture\n");
1132 static void do_system_reset(void)
1134 qemu_system_reset_request();
1137 static void do_system_powerdown(void)
1139 qemu_system_powerdown_request();
1142 #if defined(TARGET_I386)
1143 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1145 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1146 addr,
1147 pte & mask,
1148 pte & PG_GLOBAL_MASK ? 'G' : '-',
1149 pte & PG_PSE_MASK ? 'P' : '-',
1150 pte & PG_DIRTY_MASK ? 'D' : '-',
1151 pte & PG_ACCESSED_MASK ? 'A' : '-',
1152 pte & PG_PCD_MASK ? 'C' : '-',
1153 pte & PG_PWT_MASK ? 'T' : '-',
1154 pte & PG_USER_MASK ? 'U' : '-',
1155 pte & PG_RW_MASK ? 'W' : '-');
1158 static void tlb_info(void)
1160 CPUState *env;
1161 int l1, l2;
1162 uint32_t pgd, pde, pte;
1164 env = mon_get_cpu();
1165 if (!env)
1166 return;
1168 if (!(env->cr[0] & CR0_PG_MASK)) {
1169 term_printf("PG disabled\n");
1170 return;
1172 pgd = env->cr[3] & ~0xfff;
1173 for(l1 = 0; l1 < 1024; l1++) {
1174 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1175 pde = le32_to_cpu(pde);
1176 if (pde & PG_PRESENT_MASK) {
1177 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1178 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1179 } else {
1180 for(l2 = 0; l2 < 1024; l2++) {
1181 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1182 (uint8_t *)&pte, 4);
1183 pte = le32_to_cpu(pte);
1184 if (pte & PG_PRESENT_MASK) {
1185 print_pte((l1 << 22) + (l2 << 12),
1186 pte & ~PG_PSE_MASK,
1187 ~0xfff);
1195 static void mem_print(uint32_t *pstart, int *plast_prot,
1196 uint32_t end, int prot)
1198 int prot1;
1199 prot1 = *plast_prot;
1200 if (prot != prot1) {
1201 if (*pstart != -1) {
1202 term_printf("%08x-%08x %08x %c%c%c\n",
1203 *pstart, end, end - *pstart,
1204 prot1 & PG_USER_MASK ? 'u' : '-',
1205 'r',
1206 prot1 & PG_RW_MASK ? 'w' : '-');
1208 if (prot != 0)
1209 *pstart = end;
1210 else
1211 *pstart = -1;
1212 *plast_prot = prot;
1216 static void mem_info(void)
1218 CPUState *env;
1219 int l1, l2, prot, last_prot;
1220 uint32_t pgd, pde, pte, start, end;
1222 env = mon_get_cpu();
1223 if (!env)
1224 return;
1226 if (!(env->cr[0] & CR0_PG_MASK)) {
1227 term_printf("PG disabled\n");
1228 return;
1230 pgd = env->cr[3] & ~0xfff;
1231 last_prot = 0;
1232 start = -1;
1233 for(l1 = 0; l1 < 1024; l1++) {
1234 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1235 pde = le32_to_cpu(pde);
1236 end = l1 << 22;
1237 if (pde & PG_PRESENT_MASK) {
1238 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1239 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1240 mem_print(&start, &last_prot, end, prot);
1241 } else {
1242 for(l2 = 0; l2 < 1024; l2++) {
1243 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1244 (uint8_t *)&pte, 4);
1245 pte = le32_to_cpu(pte);
1246 end = (l1 << 22) + (l2 << 12);
1247 if (pte & PG_PRESENT_MASK) {
1248 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1249 } else {
1250 prot = 0;
1252 mem_print(&start, &last_prot, end, prot);
1255 } else {
1256 prot = 0;
1257 mem_print(&start, &last_prot, end, prot);
1261 #endif
1263 static void do_info_kqemu(void)
1265 #ifdef USE_KQEMU
1266 CPUState *env;
1267 int val;
1268 val = 0;
1269 env = mon_get_cpu();
1270 if (!env) {
1271 term_printf("No cpu initialized yet");
1272 return;
1274 val = env->kqemu_enabled;
1275 term_printf("kqemu support: ");
1276 switch(val) {
1277 default:
1278 case 0:
1279 term_printf("disabled\n");
1280 break;
1281 case 1:
1282 term_printf("enabled for user code\n");
1283 break;
1284 case 2:
1285 term_printf("enabled for user and kernel code\n");
1286 break;
1288 #else
1289 term_printf("kqemu support: not compiled\n");
1290 #endif
1293 static void do_info_kvm(void)
1295 #if defined(USE_KVM) || defined(CONFIG_KVM)
1296 term_printf("kvm support: ");
1297 if (kvm_enabled())
1298 term_printf("enabled\n");
1299 else
1300 term_printf("disabled\n");
1301 #else
1302 term_printf("kvm support: not compiled\n");
1303 #endif
1306 #ifdef CONFIG_PROFILER
1308 int64_t kqemu_time;
1309 int64_t qemu_time;
1310 int64_t kqemu_exec_count;
1311 int64_t dev_time;
1312 int64_t kqemu_ret_int_count;
1313 int64_t kqemu_ret_excp_count;
1314 int64_t kqemu_ret_intr_count;
1316 static void do_info_profile(void)
1318 int64_t total;
1319 total = qemu_time;
1320 if (total == 0)
1321 total = 1;
1322 term_printf("async time %" PRId64 " (%0.3f)\n",
1323 dev_time, dev_time / (double)ticks_per_sec);
1324 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1325 qemu_time, qemu_time / (double)ticks_per_sec);
1326 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1327 kqemu_time, kqemu_time / (double)ticks_per_sec,
1328 kqemu_time / (double)total * 100.0,
1329 kqemu_exec_count,
1330 kqemu_ret_int_count,
1331 kqemu_ret_excp_count,
1332 kqemu_ret_intr_count);
1333 qemu_time = 0;
1334 kqemu_time = 0;
1335 kqemu_exec_count = 0;
1336 dev_time = 0;
1337 kqemu_ret_int_count = 0;
1338 kqemu_ret_excp_count = 0;
1339 kqemu_ret_intr_count = 0;
1340 #ifdef USE_KQEMU
1341 kqemu_record_dump();
1342 #endif
1344 #else
1345 static void do_info_profile(void)
1347 term_printf("Internal profiler not compiled\n");
1349 #endif
1351 /* Capture support */
1352 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1354 static void do_info_capture (void)
1356 int i;
1357 CaptureState *s;
1359 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1360 term_printf ("[%d]: ", i);
1361 s->ops.info (s->opaque);
1365 static void do_stop_capture (int n)
1367 int i;
1368 CaptureState *s;
1370 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1371 if (i == n) {
1372 s->ops.destroy (s->opaque);
1373 LIST_REMOVE (s, entries);
1374 qemu_free (s);
1375 return;
1380 #ifdef HAS_AUDIO
1381 static void do_wav_capture (const char *path,
1382 int has_freq, int freq,
1383 int has_bits, int bits,
1384 int has_channels, int nchannels)
1386 CaptureState *s;
1388 s = qemu_mallocz (sizeof (*s));
1389 if (!s) {
1390 term_printf ("Not enough memory to add wave capture\n");
1391 return;
1394 freq = has_freq ? freq : 44100;
1395 bits = has_bits ? bits : 16;
1396 nchannels = has_channels ? nchannels : 2;
1398 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1399 term_printf ("Faied to add wave capture\n");
1400 qemu_free (s);
1402 LIST_INSERT_HEAD (&capture_head, s, entries);
1404 #endif
1406 #if defined(TARGET_I386)
1407 static void do_inject_nmi(int cpu_index)
1409 CPUState *env;
1411 for (env = first_cpu; env != NULL; env = env->next_cpu)
1412 if (env->cpu_index == cpu_index) {
1413 if (kvm_enabled())
1414 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1415 else
1416 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1417 break;
1420 #endif
1422 static void do_balloon(int value)
1424 ram_addr_t target = value;
1425 qemu_balloon(target << 20);
1428 static void do_info_balloon(void)
1430 ram_addr_t actual;
1432 actual = qemu_balloon_status();
1433 if (kvm_enabled() && !qemu_kvm_has_sync_mmu())
1434 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1435 else if (actual == 0)
1436 term_printf("Ballooning not activated in VM\n");
1437 else
1438 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1441 static const term_cmd_t term_cmds[] = {
1442 { "help|?", "s?", do_help,
1443 "[cmd]", "show the help" },
1444 { "commit", "s", do_commit,
1445 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1446 { "info", "s?", do_info,
1447 "subcommand", "show various information about the system state" },
1448 { "q|quit", "", do_quit,
1449 "", "quit the emulator" },
1450 { "eject", "-fB", do_eject,
1451 "[-f] device", "eject a removable medium (use -f to force it)" },
1452 { "change", "BFs?", do_change,
1453 "device filename [format]", "change a removable medium, optional format" },
1454 { "screendump", "F", do_screen_dump,
1455 "filename", "save screen into PPM image 'filename'" },
1456 { "logfile", "F", do_logfile,
1457 "filename", "output logs to 'filename'" },
1458 { "log", "s", do_log,
1459 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1460 { "savevm", "s?", do_savevm,
1461 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1462 { "loadvm", "s", do_loadvm,
1463 "tag|id", "restore a VM snapshot from its tag or id" },
1464 { "delvm", "s", do_delvm,
1465 "tag|id", "delete a VM snapshot from its tag or id" },
1466 { "stop", "", do_stop,
1467 "", "stop emulation", },
1468 { "c|cont", "", do_cont,
1469 "", "resume emulation", },
1470 #ifdef CONFIG_GDBSTUB
1471 { "gdbserver", "s?", do_gdbserver,
1472 "[port]", "start gdbserver session (default port=1234)", },
1473 #endif
1474 { "x", "/l", do_memory_dump,
1475 "/fmt addr", "virtual memory dump starting at 'addr'", },
1476 { "xp", "/l", do_physical_memory_dump,
1477 "/fmt addr", "physical memory dump starting at 'addr'", },
1478 { "p|print", "/l", do_print,
1479 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1480 { "i", "/ii.", do_ioport_read,
1481 "/fmt addr", "I/O port read" },
1483 { "sendkey", "si?", do_sendkey,
1484 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1485 { "system_reset", "", do_system_reset,
1486 "", "reset the system" },
1487 { "system_powerdown", "", do_system_powerdown,
1488 "", "send system power down event" },
1489 { "sum", "ii", do_sum,
1490 "addr size", "compute the checksum of a memory region" },
1491 { "usb_add", "s", do_usb_add,
1492 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1493 { "usb_del", "s", do_usb_del,
1494 "device", "remove USB device 'bus.addr'" },
1495 { "cpu", "i", do_cpu_set,
1496 "index", "set the default CPU" },
1497 { "mouse_move", "sss?", do_mouse_move,
1498 "dx dy [dz]", "send mouse move events" },
1499 { "mouse_button", "i", do_mouse_button,
1500 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1501 { "mouse_set", "i", do_mouse_set,
1502 "index", "set which mouse device receives events" },
1503 #ifdef HAS_AUDIO
1504 { "wavcapture", "si?i?i?", do_wav_capture,
1505 "path [frequency bits channels]",
1506 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1507 #endif
1508 { "stopcapture", "i", do_stop_capture,
1509 "capture index", "stop capture" },
1510 { "memsave", "lis", do_memory_save,
1511 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1512 { "pmemsave", "lis", do_physical_memory_save,
1513 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1514 { "boot_set", "s", do_boot_set,
1515 "bootdevice", "define new values for the boot device list" },
1516 #if defined(TARGET_I386)
1517 { "nmi", "i", do_inject_nmi,
1518 "cpu", "inject an NMI on the given CPU", },
1519 #endif
1520 { "migrate", "-ds", do_migrate,
1521 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1522 { "migrate_cancel", "", do_migrate_cancel,
1523 "", "cancel the current VM migration" },
1524 { "migrate_set_speed", "s", do_migrate_set_speed,
1525 "value", "set maximum speed (in bytes) for migrations" },
1526 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1527 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1528 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1529 "[,unit=m][,media=d][index=i]\n"
1530 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1531 "[snapshot=on|off][,cache=on|off]",
1532 "add drive to PCI storage controller" },
1533 { "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" },
1534 { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" },
1535 #endif
1536 { "balloon", "i", do_balloon,
1537 "target", "request VM to change it's memory allocation (in MB)" },
1538 { NULL, NULL, },
1541 static const term_cmd_t info_cmds[] = {
1542 { "version", "", do_info_version,
1543 "", "show the version of qemu" },
1544 { "network", "", do_info_network,
1545 "", "show the network state" },
1546 { "chardev", "", qemu_chr_info,
1547 "", "show the character devices" },
1548 { "block", "", do_info_block,
1549 "", "show the block devices" },
1550 { "blockstats", "", do_info_blockstats,
1551 "", "show block device statistics" },
1552 { "registers", "", do_info_registers,
1553 "", "show the cpu registers" },
1554 { "cpus", "", do_info_cpus,
1555 "", "show infos for each CPU" },
1556 { "history", "", do_info_history,
1557 "", "show the command line history", },
1558 { "irq", "", irq_info,
1559 "", "show the interrupts statistics (if available)", },
1560 { "pic", "", pic_info,
1561 "", "show i8259 (PIC) state", },
1562 { "pci", "", pci_info,
1563 "", "show PCI info", },
1564 #if defined(TARGET_I386)
1565 { "tlb", "", tlb_info,
1566 "", "show virtual to physical memory mappings", },
1567 { "mem", "", mem_info,
1568 "", "show the active virtual memory mappings", },
1569 #endif
1570 { "jit", "", do_info_jit,
1571 "", "show dynamic compiler info", },
1572 { "kqemu", "", do_info_kqemu,
1573 "", "show kqemu information", },
1574 { "kvm", "", do_info_kvm,
1575 "", "show kvm information", },
1576 { "usb", "", usb_info,
1577 "", "show guest USB devices", },
1578 { "usbhost", "", usb_host_info,
1579 "", "show host USB devices", },
1580 { "profile", "", do_info_profile,
1581 "", "show profiling information", },
1582 { "capture", "", do_info_capture,
1583 "", "show capture information" },
1584 { "snapshots", "", do_info_snapshots,
1585 "", "show the currently saved VM snapshots" },
1586 { "pcmcia", "", pcmcia_info,
1587 "", "show guest PCMCIA status" },
1588 { "mice", "", do_info_mice,
1589 "", "show which guest mouse is receiving events" },
1590 { "vnc", "", do_info_vnc,
1591 "", "show the vnc server status"},
1592 { "name", "", do_info_name,
1593 "", "show the current VM name" },
1594 { "uuid", "", do_info_uuid,
1595 "", "show the current VM UUID" },
1596 #if defined(TARGET_PPC)
1597 { "cpustats", "", do_info_cpu_stats,
1598 "", "show CPU statistics", },
1599 #endif
1600 #if defined(CONFIG_SLIRP)
1601 { "slirp", "", do_info_slirp,
1602 "", "show SLIRP statistics", },
1603 #endif
1604 { "migrate", "", do_info_migrate, "", "show migration status" },
1605 { "balloon", "", do_info_balloon,
1606 "", "show balloon information" },
1607 { NULL, NULL, },
1610 /*******************************************************************/
1612 static const char *pch;
1613 static jmp_buf expr_env;
1615 #define MD_TLONG 0
1616 #define MD_I32 1
1618 typedef struct MonitorDef {
1619 const char *name;
1620 int offset;
1621 target_long (*get_value)(const struct MonitorDef *md, int val);
1622 int type;
1623 } MonitorDef;
1625 #if defined(TARGET_I386)
1626 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1628 CPUState *env = mon_get_cpu();
1629 if (!env)
1630 return 0;
1631 return env->eip + env->segs[R_CS].base;
1633 #endif
1635 #if defined(TARGET_PPC)
1636 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1638 CPUState *env = mon_get_cpu();
1639 unsigned int u;
1640 int i;
1642 if (!env)
1643 return 0;
1645 u = 0;
1646 for (i = 0; i < 8; i++)
1647 u |= env->crf[i] << (32 - (4 * i));
1649 return u;
1652 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1654 CPUState *env = mon_get_cpu();
1655 if (!env)
1656 return 0;
1657 return env->msr;
1660 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1662 CPUState *env = mon_get_cpu();
1663 if (!env)
1664 return 0;
1665 return env->xer;
1668 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1670 CPUState *env = mon_get_cpu();
1671 if (!env)
1672 return 0;
1673 return cpu_ppc_load_decr(env);
1676 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1678 CPUState *env = mon_get_cpu();
1679 if (!env)
1680 return 0;
1681 return cpu_ppc_load_tbu(env);
1684 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1686 CPUState *env = mon_get_cpu();
1687 if (!env)
1688 return 0;
1689 return cpu_ppc_load_tbl(env);
1691 #endif
1693 #if defined(TARGET_SPARC)
1694 #ifndef TARGET_SPARC64
1695 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1697 CPUState *env = mon_get_cpu();
1698 if (!env)
1699 return 0;
1700 return GET_PSR(env);
1702 #endif
1704 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1706 CPUState *env = mon_get_cpu();
1707 if (!env)
1708 return 0;
1709 return env->regwptr[val];
1711 #endif
1713 static const MonitorDef monitor_defs[] = {
1714 #ifdef TARGET_I386
1716 #define SEG(name, seg) \
1717 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1718 { name ".base", offsetof(CPUState, segs[seg].base) },\
1719 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1721 { "eax", offsetof(CPUState, regs[0]) },
1722 { "ecx", offsetof(CPUState, regs[1]) },
1723 { "edx", offsetof(CPUState, regs[2]) },
1724 { "ebx", offsetof(CPUState, regs[3]) },
1725 { "esp|sp", offsetof(CPUState, regs[4]) },
1726 { "ebp|fp", offsetof(CPUState, regs[5]) },
1727 { "esi", offsetof(CPUState, regs[6]) },
1728 { "edi", offsetof(CPUState, regs[7]) },
1729 #ifdef TARGET_X86_64
1730 { "r8", offsetof(CPUState, regs[8]) },
1731 { "r9", offsetof(CPUState, regs[9]) },
1732 { "r10", offsetof(CPUState, regs[10]) },
1733 { "r11", offsetof(CPUState, regs[11]) },
1734 { "r12", offsetof(CPUState, regs[12]) },
1735 { "r13", offsetof(CPUState, regs[13]) },
1736 { "r14", offsetof(CPUState, regs[14]) },
1737 { "r15", offsetof(CPUState, regs[15]) },
1738 #endif
1739 { "eflags", offsetof(CPUState, eflags) },
1740 { "eip", offsetof(CPUState, eip) },
1741 SEG("cs", R_CS)
1742 SEG("ds", R_DS)
1743 SEG("es", R_ES)
1744 SEG("ss", R_SS)
1745 SEG("fs", R_FS)
1746 SEG("gs", R_GS)
1747 { "pc", 0, monitor_get_pc, },
1748 #elif defined(TARGET_PPC)
1749 /* General purpose registers */
1750 { "r0", offsetof(CPUState, gpr[0]) },
1751 { "r1", offsetof(CPUState, gpr[1]) },
1752 { "r2", offsetof(CPUState, gpr[2]) },
1753 { "r3", offsetof(CPUState, gpr[3]) },
1754 { "r4", offsetof(CPUState, gpr[4]) },
1755 { "r5", offsetof(CPUState, gpr[5]) },
1756 { "r6", offsetof(CPUState, gpr[6]) },
1757 { "r7", offsetof(CPUState, gpr[7]) },
1758 { "r8", offsetof(CPUState, gpr[8]) },
1759 { "r9", offsetof(CPUState, gpr[9]) },
1760 { "r10", offsetof(CPUState, gpr[10]) },
1761 { "r11", offsetof(CPUState, gpr[11]) },
1762 { "r12", offsetof(CPUState, gpr[12]) },
1763 { "r13", offsetof(CPUState, gpr[13]) },
1764 { "r14", offsetof(CPUState, gpr[14]) },
1765 { "r15", offsetof(CPUState, gpr[15]) },
1766 { "r16", offsetof(CPUState, gpr[16]) },
1767 { "r17", offsetof(CPUState, gpr[17]) },
1768 { "r18", offsetof(CPUState, gpr[18]) },
1769 { "r19", offsetof(CPUState, gpr[19]) },
1770 { "r20", offsetof(CPUState, gpr[20]) },
1771 { "r21", offsetof(CPUState, gpr[21]) },
1772 { "r22", offsetof(CPUState, gpr[22]) },
1773 { "r23", offsetof(CPUState, gpr[23]) },
1774 { "r24", offsetof(CPUState, gpr[24]) },
1775 { "r25", offsetof(CPUState, gpr[25]) },
1776 { "r26", offsetof(CPUState, gpr[26]) },
1777 { "r27", offsetof(CPUState, gpr[27]) },
1778 { "r28", offsetof(CPUState, gpr[28]) },
1779 { "r29", offsetof(CPUState, gpr[29]) },
1780 { "r30", offsetof(CPUState, gpr[30]) },
1781 { "r31", offsetof(CPUState, gpr[31]) },
1782 /* Floating point registers */
1783 { "f0", offsetof(CPUState, fpr[0]) },
1784 { "f1", offsetof(CPUState, fpr[1]) },
1785 { "f2", offsetof(CPUState, fpr[2]) },
1786 { "f3", offsetof(CPUState, fpr[3]) },
1787 { "f4", offsetof(CPUState, fpr[4]) },
1788 { "f5", offsetof(CPUState, fpr[5]) },
1789 { "f6", offsetof(CPUState, fpr[6]) },
1790 { "f7", offsetof(CPUState, fpr[7]) },
1791 { "f8", offsetof(CPUState, fpr[8]) },
1792 { "f9", offsetof(CPUState, fpr[9]) },
1793 { "f10", offsetof(CPUState, fpr[10]) },
1794 { "f11", offsetof(CPUState, fpr[11]) },
1795 { "f12", offsetof(CPUState, fpr[12]) },
1796 { "f13", offsetof(CPUState, fpr[13]) },
1797 { "f14", offsetof(CPUState, fpr[14]) },
1798 { "f15", offsetof(CPUState, fpr[15]) },
1799 { "f16", offsetof(CPUState, fpr[16]) },
1800 { "f17", offsetof(CPUState, fpr[17]) },
1801 { "f18", offsetof(CPUState, fpr[18]) },
1802 { "f19", offsetof(CPUState, fpr[19]) },
1803 { "f20", offsetof(CPUState, fpr[20]) },
1804 { "f21", offsetof(CPUState, fpr[21]) },
1805 { "f22", offsetof(CPUState, fpr[22]) },
1806 { "f23", offsetof(CPUState, fpr[23]) },
1807 { "f24", offsetof(CPUState, fpr[24]) },
1808 { "f25", offsetof(CPUState, fpr[25]) },
1809 { "f26", offsetof(CPUState, fpr[26]) },
1810 { "f27", offsetof(CPUState, fpr[27]) },
1811 { "f28", offsetof(CPUState, fpr[28]) },
1812 { "f29", offsetof(CPUState, fpr[29]) },
1813 { "f30", offsetof(CPUState, fpr[30]) },
1814 { "f31", offsetof(CPUState, fpr[31]) },
1815 { "fpscr", offsetof(CPUState, fpscr) },
1816 /* Next instruction pointer */
1817 { "nip|pc", offsetof(CPUState, nip) },
1818 { "lr", offsetof(CPUState, lr) },
1819 { "ctr", offsetof(CPUState, ctr) },
1820 { "decr", 0, &monitor_get_decr, },
1821 { "ccr", 0, &monitor_get_ccr, },
1822 /* Machine state register */
1823 { "msr", 0, &monitor_get_msr, },
1824 { "xer", 0, &monitor_get_xer, },
1825 { "tbu", 0, &monitor_get_tbu, },
1826 { "tbl", 0, &monitor_get_tbl, },
1827 #if defined(TARGET_PPC64)
1828 /* Address space register */
1829 { "asr", offsetof(CPUState, asr) },
1830 #endif
1831 /* Segment registers */
1832 { "sdr1", offsetof(CPUState, sdr1) },
1833 { "sr0", offsetof(CPUState, sr[0]) },
1834 { "sr1", offsetof(CPUState, sr[1]) },
1835 { "sr2", offsetof(CPUState, sr[2]) },
1836 { "sr3", offsetof(CPUState, sr[3]) },
1837 { "sr4", offsetof(CPUState, sr[4]) },
1838 { "sr5", offsetof(CPUState, sr[5]) },
1839 { "sr6", offsetof(CPUState, sr[6]) },
1840 { "sr7", offsetof(CPUState, sr[7]) },
1841 { "sr8", offsetof(CPUState, sr[8]) },
1842 { "sr9", offsetof(CPUState, sr[9]) },
1843 { "sr10", offsetof(CPUState, sr[10]) },
1844 { "sr11", offsetof(CPUState, sr[11]) },
1845 { "sr12", offsetof(CPUState, sr[12]) },
1846 { "sr13", offsetof(CPUState, sr[13]) },
1847 { "sr14", offsetof(CPUState, sr[14]) },
1848 { "sr15", offsetof(CPUState, sr[15]) },
1849 /* Too lazy to put BATs and SPRs ... */
1850 #elif defined(TARGET_SPARC)
1851 { "g0", offsetof(CPUState, gregs[0]) },
1852 { "g1", offsetof(CPUState, gregs[1]) },
1853 { "g2", offsetof(CPUState, gregs[2]) },
1854 { "g3", offsetof(CPUState, gregs[3]) },
1855 { "g4", offsetof(CPUState, gregs[4]) },
1856 { "g5", offsetof(CPUState, gregs[5]) },
1857 { "g6", offsetof(CPUState, gregs[6]) },
1858 { "g7", offsetof(CPUState, gregs[7]) },
1859 { "o0", 0, monitor_get_reg },
1860 { "o1", 1, monitor_get_reg },
1861 { "o2", 2, monitor_get_reg },
1862 { "o3", 3, monitor_get_reg },
1863 { "o4", 4, monitor_get_reg },
1864 { "o5", 5, monitor_get_reg },
1865 { "o6", 6, monitor_get_reg },
1866 { "o7", 7, monitor_get_reg },
1867 { "l0", 8, monitor_get_reg },
1868 { "l1", 9, monitor_get_reg },
1869 { "l2", 10, monitor_get_reg },
1870 { "l3", 11, monitor_get_reg },
1871 { "l4", 12, monitor_get_reg },
1872 { "l5", 13, monitor_get_reg },
1873 { "l6", 14, monitor_get_reg },
1874 { "l7", 15, monitor_get_reg },
1875 { "i0", 16, monitor_get_reg },
1876 { "i1", 17, monitor_get_reg },
1877 { "i2", 18, monitor_get_reg },
1878 { "i3", 19, monitor_get_reg },
1879 { "i4", 20, monitor_get_reg },
1880 { "i5", 21, monitor_get_reg },
1881 { "i6", 22, monitor_get_reg },
1882 { "i7", 23, monitor_get_reg },
1883 { "pc", offsetof(CPUState, pc) },
1884 { "npc", offsetof(CPUState, npc) },
1885 { "y", offsetof(CPUState, y) },
1886 #ifndef TARGET_SPARC64
1887 { "psr", 0, &monitor_get_psr, },
1888 { "wim", offsetof(CPUState, wim) },
1889 #endif
1890 { "tbr", offsetof(CPUState, tbr) },
1891 { "fsr", offsetof(CPUState, fsr) },
1892 { "f0", offsetof(CPUState, fpr[0]) },
1893 { "f1", offsetof(CPUState, fpr[1]) },
1894 { "f2", offsetof(CPUState, fpr[2]) },
1895 { "f3", offsetof(CPUState, fpr[3]) },
1896 { "f4", offsetof(CPUState, fpr[4]) },
1897 { "f5", offsetof(CPUState, fpr[5]) },
1898 { "f6", offsetof(CPUState, fpr[6]) },
1899 { "f7", offsetof(CPUState, fpr[7]) },
1900 { "f8", offsetof(CPUState, fpr[8]) },
1901 { "f9", offsetof(CPUState, fpr[9]) },
1902 { "f10", offsetof(CPUState, fpr[10]) },
1903 { "f11", offsetof(CPUState, fpr[11]) },
1904 { "f12", offsetof(CPUState, fpr[12]) },
1905 { "f13", offsetof(CPUState, fpr[13]) },
1906 { "f14", offsetof(CPUState, fpr[14]) },
1907 { "f15", offsetof(CPUState, fpr[15]) },
1908 { "f16", offsetof(CPUState, fpr[16]) },
1909 { "f17", offsetof(CPUState, fpr[17]) },
1910 { "f18", offsetof(CPUState, fpr[18]) },
1911 { "f19", offsetof(CPUState, fpr[19]) },
1912 { "f20", offsetof(CPUState, fpr[20]) },
1913 { "f21", offsetof(CPUState, fpr[21]) },
1914 { "f22", offsetof(CPUState, fpr[22]) },
1915 { "f23", offsetof(CPUState, fpr[23]) },
1916 { "f24", offsetof(CPUState, fpr[24]) },
1917 { "f25", offsetof(CPUState, fpr[25]) },
1918 { "f26", offsetof(CPUState, fpr[26]) },
1919 { "f27", offsetof(CPUState, fpr[27]) },
1920 { "f28", offsetof(CPUState, fpr[28]) },
1921 { "f29", offsetof(CPUState, fpr[29]) },
1922 { "f30", offsetof(CPUState, fpr[30]) },
1923 { "f31", offsetof(CPUState, fpr[31]) },
1924 #ifdef TARGET_SPARC64
1925 { "f32", offsetof(CPUState, fpr[32]) },
1926 { "f34", offsetof(CPUState, fpr[34]) },
1927 { "f36", offsetof(CPUState, fpr[36]) },
1928 { "f38", offsetof(CPUState, fpr[38]) },
1929 { "f40", offsetof(CPUState, fpr[40]) },
1930 { "f42", offsetof(CPUState, fpr[42]) },
1931 { "f44", offsetof(CPUState, fpr[44]) },
1932 { "f46", offsetof(CPUState, fpr[46]) },
1933 { "f48", offsetof(CPUState, fpr[48]) },
1934 { "f50", offsetof(CPUState, fpr[50]) },
1935 { "f52", offsetof(CPUState, fpr[52]) },
1936 { "f54", offsetof(CPUState, fpr[54]) },
1937 { "f56", offsetof(CPUState, fpr[56]) },
1938 { "f58", offsetof(CPUState, fpr[58]) },
1939 { "f60", offsetof(CPUState, fpr[60]) },
1940 { "f62", offsetof(CPUState, fpr[62]) },
1941 { "asi", offsetof(CPUState, asi) },
1942 { "pstate", offsetof(CPUState, pstate) },
1943 { "cansave", offsetof(CPUState, cansave) },
1944 { "canrestore", offsetof(CPUState, canrestore) },
1945 { "otherwin", offsetof(CPUState, otherwin) },
1946 { "wstate", offsetof(CPUState, wstate) },
1947 { "cleanwin", offsetof(CPUState, cleanwin) },
1948 { "fprs", offsetof(CPUState, fprs) },
1949 #endif
1950 #endif
1951 { NULL },
1954 static void expr_error(const char *fmt)
1956 term_printf(fmt);
1957 term_printf("\n");
1958 longjmp(expr_env, 1);
1961 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1962 static int get_monitor_def(target_long *pval, const char *name)
1964 const MonitorDef *md;
1965 void *ptr;
1967 for(md = monitor_defs; md->name != NULL; md++) {
1968 if (compare_cmd(name, md->name)) {
1969 if (md->get_value) {
1970 *pval = md->get_value(md, md->offset);
1971 } else {
1972 CPUState *env = mon_get_cpu();
1973 if (!env)
1974 return -2;
1975 ptr = (uint8_t *)env + md->offset;
1976 switch(md->type) {
1977 case MD_I32:
1978 *pval = *(int32_t *)ptr;
1979 break;
1980 case MD_TLONG:
1981 *pval = *(target_long *)ptr;
1982 break;
1983 default:
1984 *pval = 0;
1985 break;
1988 return 0;
1991 return -1;
1994 static void next(void)
1996 if (pch != '\0') {
1997 pch++;
1998 while (qemu_isspace(*pch))
1999 pch++;
2003 static int64_t expr_sum(void);
2005 static int64_t expr_unary(void)
2007 int64_t n;
2008 char *p;
2009 int ret;
2011 switch(*pch) {
2012 case '+':
2013 next();
2014 n = expr_unary();
2015 break;
2016 case '-':
2017 next();
2018 n = -expr_unary();
2019 break;
2020 case '~':
2021 next();
2022 n = ~expr_unary();
2023 break;
2024 case '(':
2025 next();
2026 n = expr_sum();
2027 if (*pch != ')') {
2028 expr_error("')' expected");
2030 next();
2031 break;
2032 case '\'':
2033 pch++;
2034 if (*pch == '\0')
2035 expr_error("character constant expected");
2036 n = *pch;
2037 pch++;
2038 if (*pch != '\'')
2039 expr_error("missing terminating \' character");
2040 next();
2041 break;
2042 case '$':
2044 char buf[128], *q;
2045 target_long reg=0;
2047 pch++;
2048 q = buf;
2049 while ((*pch >= 'a' && *pch <= 'z') ||
2050 (*pch >= 'A' && *pch <= 'Z') ||
2051 (*pch >= '0' && *pch <= '9') ||
2052 *pch == '_' || *pch == '.') {
2053 if ((q - buf) < sizeof(buf) - 1)
2054 *q++ = *pch;
2055 pch++;
2057 while (qemu_isspace(*pch))
2058 pch++;
2059 *q = 0;
2060 ret = get_monitor_def(&reg, buf);
2061 if (ret == -1)
2062 expr_error("unknown register");
2063 else if (ret == -2)
2064 expr_error("no cpu defined");
2065 n = reg;
2067 break;
2068 case '\0':
2069 expr_error("unexpected end of expression");
2070 n = 0;
2071 break;
2072 default:
2073 #if TARGET_PHYS_ADDR_BITS > 32
2074 n = strtoull(pch, &p, 0);
2075 #else
2076 n = strtoul(pch, &p, 0);
2077 #endif
2078 if (pch == p) {
2079 expr_error("invalid char in expression");
2081 pch = p;
2082 while (qemu_isspace(*pch))
2083 pch++;
2084 break;
2086 return n;
2090 static int64_t expr_prod(void)
2092 int64_t val, val2;
2093 int op;
2095 val = expr_unary();
2096 for(;;) {
2097 op = *pch;
2098 if (op != '*' && op != '/' && op != '%')
2099 break;
2100 next();
2101 val2 = expr_unary();
2102 switch(op) {
2103 default:
2104 case '*':
2105 val *= val2;
2106 break;
2107 case '/':
2108 case '%':
2109 if (val2 == 0)
2110 expr_error("division by zero");
2111 if (op == '/')
2112 val /= val2;
2113 else
2114 val %= val2;
2115 break;
2118 return val;
2121 static int64_t expr_logic(void)
2123 int64_t val, val2;
2124 int op;
2126 val = expr_prod();
2127 for(;;) {
2128 op = *pch;
2129 if (op != '&' && op != '|' && op != '^')
2130 break;
2131 next();
2132 val2 = expr_prod();
2133 switch(op) {
2134 default:
2135 case '&':
2136 val &= val2;
2137 break;
2138 case '|':
2139 val |= val2;
2140 break;
2141 case '^':
2142 val ^= val2;
2143 break;
2146 return val;
2149 static int64_t expr_sum(void)
2151 int64_t val, val2;
2152 int op;
2154 val = expr_logic();
2155 for(;;) {
2156 op = *pch;
2157 if (op != '+' && op != '-')
2158 break;
2159 next();
2160 val2 = expr_logic();
2161 if (op == '+')
2162 val += val2;
2163 else
2164 val -= val2;
2166 return val;
2169 static int get_expr(int64_t *pval, const char **pp)
2171 pch = *pp;
2172 if (setjmp(expr_env)) {
2173 *pp = pch;
2174 return -1;
2176 while (qemu_isspace(*pch))
2177 pch++;
2178 *pval = expr_sum();
2179 *pp = pch;
2180 return 0;
2183 static int get_str(char *buf, int buf_size, const char **pp)
2185 const char *p;
2186 char *q;
2187 int c;
2189 q = buf;
2190 p = *pp;
2191 while (qemu_isspace(*p))
2192 p++;
2193 if (*p == '\0') {
2194 fail:
2195 *q = '\0';
2196 *pp = p;
2197 return -1;
2199 if (*p == '\"') {
2200 p++;
2201 while (*p != '\0' && *p != '\"') {
2202 if (*p == '\\') {
2203 p++;
2204 c = *p++;
2205 switch(c) {
2206 case 'n':
2207 c = '\n';
2208 break;
2209 case 'r':
2210 c = '\r';
2211 break;
2212 case '\\':
2213 case '\'':
2214 case '\"':
2215 break;
2216 default:
2217 qemu_printf("unsupported escape code: '\\%c'\n", c);
2218 goto fail;
2220 if ((q - buf) < buf_size - 1) {
2221 *q++ = c;
2223 } else {
2224 if ((q - buf) < buf_size - 1) {
2225 *q++ = *p;
2227 p++;
2230 if (*p != '\"') {
2231 qemu_printf("unterminated string\n");
2232 goto fail;
2234 p++;
2235 } else {
2236 while (*p != '\0' && !qemu_isspace(*p)) {
2237 if ((q - buf) < buf_size - 1) {
2238 *q++ = *p;
2240 p++;
2243 *q = '\0';
2244 *pp = p;
2245 return 0;
2248 static int default_fmt_format = 'x';
2249 static int default_fmt_size = 4;
2251 #define MAX_ARGS 16
2253 static void monitor_handle_command(const char *cmdline)
2255 const char *p, *pstart, *typestr;
2256 char *q;
2257 int c, nb_args, len, i, has_arg;
2258 const term_cmd_t *cmd;
2259 char cmdname[256];
2260 char buf[1024];
2261 void *str_allocated[MAX_ARGS];
2262 void *args[MAX_ARGS];
2263 void (*handler_0)(void);
2264 void (*handler_1)(void *arg0);
2265 void (*handler_2)(void *arg0, void *arg1);
2266 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2267 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2268 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2269 void *arg4);
2270 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2271 void *arg4, void *arg5);
2272 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2273 void *arg4, void *arg5, void *arg6);
2275 #ifdef DEBUG
2276 term_printf("command='%s'\n", cmdline);
2277 #endif
2279 /* extract the command name */
2280 p = cmdline;
2281 q = cmdname;
2282 while (qemu_isspace(*p))
2283 p++;
2284 if (*p == '\0')
2285 return;
2286 pstart = p;
2287 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2288 p++;
2289 len = p - pstart;
2290 if (len > sizeof(cmdname) - 1)
2291 len = sizeof(cmdname) - 1;
2292 memcpy(cmdname, pstart, len);
2293 cmdname[len] = '\0';
2295 /* find the command */
2296 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2297 if (compare_cmd(cmdname, cmd->name))
2298 goto found;
2300 term_printf("unknown command: '%s'\n", cmdname);
2301 return;
2302 found:
2304 for(i = 0; i < MAX_ARGS; i++)
2305 str_allocated[i] = NULL;
2307 /* parse the parameters */
2308 typestr = cmd->args_type;
2309 nb_args = 0;
2310 for(;;) {
2311 c = *typestr;
2312 if (c == '\0')
2313 break;
2314 typestr++;
2315 switch(c) {
2316 case 'F':
2317 case 'B':
2318 case 's':
2320 int ret;
2321 char *str;
2323 while (qemu_isspace(*p))
2324 p++;
2325 if (*typestr == '?') {
2326 typestr++;
2327 if (*p == '\0') {
2328 /* no optional string: NULL argument */
2329 str = NULL;
2330 goto add_str;
2333 ret = get_str(buf, sizeof(buf), &p);
2334 if (ret < 0) {
2335 switch(c) {
2336 case 'F':
2337 term_printf("%s: filename expected\n", cmdname);
2338 break;
2339 case 'B':
2340 term_printf("%s: block device name expected\n", cmdname);
2341 break;
2342 default:
2343 term_printf("%s: string expected\n", cmdname);
2344 break;
2346 goto fail;
2348 str = qemu_malloc(strlen(buf) + 1);
2349 pstrcpy(str, sizeof(buf), buf);
2350 str_allocated[nb_args] = str;
2351 add_str:
2352 if (nb_args >= MAX_ARGS) {
2353 error_args:
2354 term_printf("%s: too many arguments\n", cmdname);
2355 goto fail;
2357 args[nb_args++] = str;
2359 break;
2360 case '/':
2362 int count, format, size;
2364 while (qemu_isspace(*p))
2365 p++;
2366 if (*p == '/') {
2367 /* format found */
2368 p++;
2369 count = 1;
2370 if (qemu_isdigit(*p)) {
2371 count = 0;
2372 while (qemu_isdigit(*p)) {
2373 count = count * 10 + (*p - '0');
2374 p++;
2377 size = -1;
2378 format = -1;
2379 for(;;) {
2380 switch(*p) {
2381 case 'o':
2382 case 'd':
2383 case 'u':
2384 case 'x':
2385 case 'i':
2386 case 'c':
2387 format = *p++;
2388 break;
2389 case 'b':
2390 size = 1;
2391 p++;
2392 break;
2393 case 'h':
2394 size = 2;
2395 p++;
2396 break;
2397 case 'w':
2398 size = 4;
2399 p++;
2400 break;
2401 case 'g':
2402 case 'L':
2403 size = 8;
2404 p++;
2405 break;
2406 default:
2407 goto next;
2410 next:
2411 if (*p != '\0' && !qemu_isspace(*p)) {
2412 term_printf("invalid char in format: '%c'\n", *p);
2413 goto fail;
2415 if (format < 0)
2416 format = default_fmt_format;
2417 if (format != 'i') {
2418 /* for 'i', not specifying a size gives -1 as size */
2419 if (size < 0)
2420 size = default_fmt_size;
2421 default_fmt_size = size;
2423 default_fmt_format = format;
2424 } else {
2425 count = 1;
2426 format = default_fmt_format;
2427 if (format != 'i') {
2428 size = default_fmt_size;
2429 } else {
2430 size = -1;
2433 if (nb_args + 3 > MAX_ARGS)
2434 goto error_args;
2435 args[nb_args++] = (void*)(long)count;
2436 args[nb_args++] = (void*)(long)format;
2437 args[nb_args++] = (void*)(long)size;
2439 break;
2440 case 'i':
2441 case 'l':
2443 int64_t val;
2445 while (qemu_isspace(*p))
2446 p++;
2447 if (*typestr == '?' || *typestr == '.') {
2448 if (*typestr == '?') {
2449 if (*p == '\0')
2450 has_arg = 0;
2451 else
2452 has_arg = 1;
2453 } else {
2454 if (*p == '.') {
2455 p++;
2456 while (qemu_isspace(*p))
2457 p++;
2458 has_arg = 1;
2459 } else {
2460 has_arg = 0;
2463 typestr++;
2464 if (nb_args >= MAX_ARGS)
2465 goto error_args;
2466 args[nb_args++] = (void *)(long)has_arg;
2467 if (!has_arg) {
2468 if (nb_args >= MAX_ARGS)
2469 goto error_args;
2470 val = -1;
2471 goto add_num;
2474 if (get_expr(&val, &p))
2475 goto fail;
2476 add_num:
2477 if (c == 'i') {
2478 if (nb_args >= MAX_ARGS)
2479 goto error_args;
2480 args[nb_args++] = (void *)(long)val;
2481 } else {
2482 if ((nb_args + 1) >= MAX_ARGS)
2483 goto error_args;
2484 #if TARGET_PHYS_ADDR_BITS > 32
2485 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2486 #else
2487 args[nb_args++] = (void *)0;
2488 #endif
2489 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2492 break;
2493 case '-':
2495 int has_option;
2496 /* option */
2498 c = *typestr++;
2499 if (c == '\0')
2500 goto bad_type;
2501 while (qemu_isspace(*p))
2502 p++;
2503 has_option = 0;
2504 if (*p == '-') {
2505 p++;
2506 if (*p != c) {
2507 term_printf("%s: unsupported option -%c\n",
2508 cmdname, *p);
2509 goto fail;
2511 p++;
2512 has_option = 1;
2514 if (nb_args >= MAX_ARGS)
2515 goto error_args;
2516 args[nb_args++] = (void *)(long)has_option;
2518 break;
2519 default:
2520 bad_type:
2521 term_printf("%s: unknown type '%c'\n", cmdname, c);
2522 goto fail;
2525 /* check that all arguments were parsed */
2526 while (qemu_isspace(*p))
2527 p++;
2528 if (*p != '\0') {
2529 term_printf("%s: extraneous characters at the end of line\n",
2530 cmdname);
2531 goto fail;
2534 switch(nb_args) {
2535 case 0:
2536 handler_0 = cmd->handler;
2537 handler_0();
2538 break;
2539 case 1:
2540 handler_1 = cmd->handler;
2541 handler_1(args[0]);
2542 break;
2543 case 2:
2544 handler_2 = cmd->handler;
2545 handler_2(args[0], args[1]);
2546 break;
2547 case 3:
2548 handler_3 = cmd->handler;
2549 handler_3(args[0], args[1], args[2]);
2550 break;
2551 case 4:
2552 handler_4 = cmd->handler;
2553 handler_4(args[0], args[1], args[2], args[3]);
2554 break;
2555 case 5:
2556 handler_5 = cmd->handler;
2557 handler_5(args[0], args[1], args[2], args[3], args[4]);
2558 break;
2559 case 6:
2560 handler_6 = cmd->handler;
2561 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2562 break;
2563 case 7:
2564 handler_7 = cmd->handler;
2565 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2566 break;
2567 default:
2568 term_printf("unsupported number of arguments: %d\n", nb_args);
2569 goto fail;
2571 fail:
2572 for(i = 0; i < MAX_ARGS; i++)
2573 qemu_free(str_allocated[i]);
2574 return;
2577 static void cmd_completion(const char *name, const char *list)
2579 const char *p, *pstart;
2580 char cmd[128];
2581 int len;
2583 p = list;
2584 for(;;) {
2585 pstart = p;
2586 p = strchr(p, '|');
2587 if (!p)
2588 p = pstart + strlen(pstart);
2589 len = p - pstart;
2590 if (len > sizeof(cmd) - 2)
2591 len = sizeof(cmd) - 2;
2592 memcpy(cmd, pstart, len);
2593 cmd[len] = '\0';
2594 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2595 add_completion(cmd);
2597 if (*p == '\0')
2598 break;
2599 p++;
2603 static void file_completion(const char *input)
2605 DIR *ffs;
2606 struct dirent *d;
2607 char path[1024];
2608 char file[1024], file_prefix[1024];
2609 int input_path_len;
2610 const char *p;
2612 p = strrchr(input, '/');
2613 if (!p) {
2614 input_path_len = 0;
2615 pstrcpy(file_prefix, sizeof(file_prefix), input);
2616 pstrcpy(path, sizeof(path), ".");
2617 } else {
2618 input_path_len = p - input + 1;
2619 memcpy(path, input, input_path_len);
2620 if (input_path_len > sizeof(path) - 1)
2621 input_path_len = sizeof(path) - 1;
2622 path[input_path_len] = '\0';
2623 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2625 #ifdef DEBUG_COMPLETION
2626 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2627 #endif
2628 ffs = opendir(path);
2629 if (!ffs)
2630 return;
2631 for(;;) {
2632 struct stat sb;
2633 d = readdir(ffs);
2634 if (!d)
2635 break;
2636 if (strstart(d->d_name, file_prefix, NULL)) {
2637 memcpy(file, input, input_path_len);
2638 if (input_path_len < sizeof(file))
2639 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2640 d->d_name);
2641 /* stat the file to find out if it's a directory.
2642 * In that case add a slash to speed up typing long paths
2644 stat(file, &sb);
2645 if(S_ISDIR(sb.st_mode))
2646 pstrcat(file, sizeof(file), "/");
2647 add_completion(file);
2650 closedir(ffs);
2653 static void block_completion_it(void *opaque, const char *name)
2655 const char *input = opaque;
2657 if (input[0] == '\0' ||
2658 !strncmp(name, (char *)input, strlen(input))) {
2659 add_completion(name);
2663 /* NOTE: this parser is an approximate form of the real command parser */
2664 static void parse_cmdline(const char *cmdline,
2665 int *pnb_args, char **args)
2667 const char *p;
2668 int nb_args, ret;
2669 char buf[1024];
2671 p = cmdline;
2672 nb_args = 0;
2673 for(;;) {
2674 while (qemu_isspace(*p))
2675 p++;
2676 if (*p == '\0')
2677 break;
2678 if (nb_args >= MAX_ARGS)
2679 break;
2680 ret = get_str(buf, sizeof(buf), &p);
2681 args[nb_args] = qemu_strdup(buf);
2682 nb_args++;
2683 if (ret < 0)
2684 break;
2686 *pnb_args = nb_args;
2689 void readline_find_completion(const char *cmdline)
2691 const char *cmdname;
2692 char *args[MAX_ARGS];
2693 int nb_args, i, len;
2694 const char *ptype, *str;
2695 const term_cmd_t *cmd;
2696 const KeyDef *key;
2698 parse_cmdline(cmdline, &nb_args, args);
2699 #ifdef DEBUG_COMPLETION
2700 for(i = 0; i < nb_args; i++) {
2701 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2703 #endif
2705 /* if the line ends with a space, it means we want to complete the
2706 next arg */
2707 len = strlen(cmdline);
2708 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2709 if (nb_args >= MAX_ARGS)
2710 return;
2711 args[nb_args++] = qemu_strdup("");
2713 if (nb_args <= 1) {
2714 /* command completion */
2715 if (nb_args == 0)
2716 cmdname = "";
2717 else
2718 cmdname = args[0];
2719 completion_index = strlen(cmdname);
2720 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2721 cmd_completion(cmdname, cmd->name);
2723 } else {
2724 /* find the command */
2725 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2726 if (compare_cmd(args[0], cmd->name))
2727 goto found;
2729 return;
2730 found:
2731 ptype = cmd->args_type;
2732 for(i = 0; i < nb_args - 2; i++) {
2733 if (*ptype != '\0') {
2734 ptype++;
2735 while (*ptype == '?')
2736 ptype++;
2739 str = args[nb_args - 1];
2740 switch(*ptype) {
2741 case 'F':
2742 /* file completion */
2743 completion_index = strlen(str);
2744 file_completion(str);
2745 break;
2746 case 'B':
2747 /* block device name completion */
2748 completion_index = strlen(str);
2749 bdrv_iterate(block_completion_it, (void *)str);
2750 break;
2751 case 's':
2752 /* XXX: more generic ? */
2753 if (!strcmp(cmd->name, "info")) {
2754 completion_index = strlen(str);
2755 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2756 cmd_completion(str, cmd->name);
2758 } else if (!strcmp(cmd->name, "sendkey")) {
2759 completion_index = strlen(str);
2760 for(key = key_defs; key->name != NULL; key++) {
2761 cmd_completion(str, key->name);
2764 break;
2765 default:
2766 break;
2769 for(i = 0; i < nb_args; i++)
2770 qemu_free(args[i]);
2773 static int term_can_read(void *opaque)
2775 return 128;
2778 static void term_read(void *opaque, const uint8_t *buf, int size)
2780 int i;
2781 for(i = 0; i < size; i++)
2782 readline_handle_byte(buf[i]);
2785 static int monitor_suspended;
2787 static void monitor_handle_command1(void *opaque, const char *cmdline)
2789 monitor_handle_command(cmdline);
2790 if (!monitor_suspended)
2791 monitor_start_input();
2792 else
2793 monitor_suspended = 2;
2796 void monitor_suspend(void)
2798 monitor_suspended = 1;
2801 void monitor_resume(void)
2803 if (monitor_suspended == 2)
2804 monitor_start_input();
2805 monitor_suspended = 0;
2808 static void monitor_start_input(void)
2810 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2813 static void term_event(void *opaque, int event)
2815 if (event != CHR_EVENT_RESET)
2816 return;
2818 if (!hide_banner)
2819 term_printf("QEMU %s monitor - type 'help' for more information\n",
2820 QEMU_VERSION);
2821 monitor_start_input();
2824 static int is_first_init = 1;
2826 void monitor_init(CharDriverState *hd, int show_banner)
2828 int i;
2830 if (is_first_init) {
2831 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2832 if (!key_timer)
2833 return;
2834 for (i = 0; i < MAX_MON; i++) {
2835 monitor_hd[i] = NULL;
2837 is_first_init = 0;
2839 for (i = 0; i < MAX_MON; i++) {
2840 if (monitor_hd[i] == NULL) {
2841 monitor_hd[i] = hd;
2842 break;
2846 hide_banner = !show_banner;
2848 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2850 readline_start("", 0, monitor_handle_command1, NULL);
2853 /* XXX: use threads ? */
2854 /* modal monitor readline */
2855 static int monitor_readline_started;
2856 static char *monitor_readline_buf;
2857 static int monitor_readline_buf_size;
2859 static void monitor_readline_cb(void *opaque, const char *input)
2861 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2862 monitor_readline_started = 0;
2865 void monitor_readline(const char *prompt, int is_password,
2866 char *buf, int buf_size)
2868 int i;
2869 int old_focus[MAX_MON];
2871 if (is_password) {
2872 for (i = 0; i < MAX_MON; i++) {
2873 old_focus[i] = 0;
2874 if (monitor_hd[i]) {
2875 old_focus[i] = monitor_hd[i]->focus;
2876 monitor_hd[i]->focus = 0;
2877 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2882 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2883 monitor_readline_buf = buf;
2884 monitor_readline_buf_size = buf_size;
2885 monitor_readline_started = 1;
2886 while (monitor_readline_started) {
2887 main_loop_wait(10);
2889 /* restore original focus */
2890 if (is_password) {
2891 for (i = 0; i < MAX_MON; i++)
2892 if (old_focus[i])
2893 monitor_hd[i]->focus = old_focus[i];