Add the kvm utility script to the rpm so developers can easily work with it
[qemu-kvm/fedora.git] / monitor.c
blob09a0299fc77d11b2901609d3bb055bc479324989
1 /*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
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 "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
28 //#define DEBUG
29 //#define DEBUG_COMPLETION
31 #ifndef offsetof
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
33 #endif
36 * Supported types:
38 * 'F' filename
39 * 'B' block device name
40 * 's' string (accept optional quote)
41 * 'i' 32 bit integer
42 * 'l' target long (32 or 64 bit)
43 * '/' optional gdb-like print format (like "/10x")
45 * '?' optional type (for 'F', 's' and 'i')
49 typedef struct term_cmd_t {
50 const char *name;
51 const char *args_type;
52 void (*handler)();
53 const char *params;
54 const char *help;
55 } term_cmd_t;
57 static CharDriverState *monitor_hd;
59 static term_cmd_t term_cmds[];
60 static term_cmd_t info_cmds[];
62 static char term_outbuf[1024];
63 static int term_outbuf_index;
65 static void monitor_start_input(void);
67 CPUState *mon_cpu = NULL;
69 void term_flush(void)
71 if (term_outbuf_index > 0) {
72 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
73 term_outbuf_index = 0;
77 /* flush at every end of line or if the buffer is full */
78 void term_puts(const char *str)
80 int c;
81 for(;;) {
82 c = *str++;
83 if (c == '\0')
84 break;
85 if (c == '\n')
86 term_outbuf[term_outbuf_index++] = '\r';
87 term_outbuf[term_outbuf_index++] = c;
88 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
89 c == '\n')
90 term_flush();
94 void term_vprintf(const char *fmt, va_list ap)
96 char buf[4096];
97 vsnprintf(buf, sizeof(buf), fmt, ap);
98 term_puts(buf);
101 void term_printf(const char *fmt, ...)
103 va_list ap;
104 va_start(ap, fmt);
105 term_vprintf(fmt, ap);
106 va_end(ap);
109 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
111 va_list ap;
112 va_start(ap, fmt);
113 term_vprintf(fmt, ap);
114 va_end(ap);
115 return 0;
118 static int compare_cmd(const char *name, const char *list)
120 const char *p, *pstart;
121 int len;
122 len = strlen(name);
123 p = list;
124 for(;;) {
125 pstart = p;
126 p = strchr(p, '|');
127 if (!p)
128 p = pstart + strlen(pstart);
129 if ((p - pstart) == len && !memcmp(pstart, name, len))
130 return 1;
131 if (*p == '\0')
132 break;
133 p++;
135 return 0;
138 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
140 term_cmd_t *cmd;
142 for(cmd = cmds; cmd->name != NULL; cmd++) {
143 if (!name || !strcmp(name, cmd->name))
144 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
148 static void help_cmd(const char *name)
150 if (name && !strcmp(name, "info")) {
151 help_cmd1(info_cmds, "info ", NULL);
152 } else {
153 help_cmd1(term_cmds, "", name);
154 if (name && !strcmp(name, "log")) {
155 CPULogItem *item;
156 term_printf("Log items (comma separated):\n");
157 term_printf("%-10s %s\n", "none", "remove all logs");
158 for(item = cpu_log_items; item->mask != 0; item++) {
159 term_printf("%-10s %s\n", item->name, item->help);
165 static void do_help(const char *name)
167 help_cmd(name);
170 static void do_commit(void)
172 int i;
174 for (i = 0; i < MAX_DISKS; i++) {
175 if (bs_table[i]) {
176 bdrv_commit(bs_table[i]);
181 static void do_info(const char *item)
183 term_cmd_t *cmd;
185 if (!item)
186 goto help;
187 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
188 if (compare_cmd(item, cmd->name))
189 goto found;
191 help:
192 help_cmd("info");
193 return;
194 found:
195 cmd->handler();
198 static void do_info_version(void)
200 term_printf("%s\n", QEMU_VERSION);
203 static void do_info_block(void)
205 bdrv_info();
208 /* get the current CPU defined by the user */
209 int mon_set_cpu(int cpu_index)
211 CPUState *env;
213 for(env = first_cpu; env != NULL; env = env->next_cpu) {
214 if (env->cpu_index == cpu_index) {
215 mon_cpu = env;
216 return 0;
219 return -1;
222 CPUState *mon_get_cpu(void)
224 if (!mon_cpu) {
225 mon_set_cpu(0);
227 return mon_cpu;
230 static void do_info_registers(void)
232 CPUState *env;
233 env = mon_get_cpu();
234 if (!env)
235 return;
236 #ifdef TARGET_I386
237 cpu_dump_state(env, NULL, monitor_fprintf,
238 X86_DUMP_FPU);
239 #else
240 cpu_dump_state(env, NULL, monitor_fprintf,
242 #endif
245 static void do_info_cpus(void)
247 CPUState *env;
249 /* just to set the default cpu if not already done */
250 mon_get_cpu();
252 for(env = first_cpu; env != NULL; env = env->next_cpu) {
253 term_printf("%c CPU #%d:",
254 (env == mon_cpu) ? '*' : ' ',
255 env->cpu_index);
256 #if defined(TARGET_I386)
257 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
258 if (env->hflags & HF_HALTED_MASK)
259 term_printf(" (halted)");
260 #elif defined(TARGET_PPC)
261 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
262 if (env->halted)
263 term_printf(" (halted)");
264 #elif defined(TARGET_SPARC)
265 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
266 if (env->halted)
267 term_printf(" (halted)");
268 #endif
269 term_printf("\n");
273 static void do_cpu_set(int index)
275 if (mon_set_cpu(index) < 0)
276 term_printf("Invalid CPU index\n");
279 static void do_info_jit(void)
281 dump_exec_info(NULL, monitor_fprintf);
284 static void do_info_history (void)
286 int i;
287 const char *str;
289 i = 0;
290 for(;;) {
291 str = readline_get_history(i);
292 if (!str)
293 break;
294 term_printf("%d: '%s'\n", i, str);
295 i++;
299 static void do_quit(void)
301 exit(0);
304 static int eject_device(BlockDriverState *bs, int force)
306 if (bdrv_is_inserted(bs)) {
307 if (!force) {
308 if (!bdrv_is_removable(bs)) {
309 term_printf("device is not removable\n");
310 return -1;
312 if (bdrv_is_locked(bs)) {
313 term_printf("device is locked\n");
314 return -1;
317 bdrv_close(bs);
319 return 0;
322 static void do_eject(int force, const char *filename)
324 BlockDriverState *bs;
326 bs = bdrv_find(filename);
327 if (!bs) {
328 term_printf("device not found\n");
329 return;
331 eject_device(bs, force);
334 static void do_change(const char *device, const char *filename)
336 BlockDriverState *bs;
337 int i;
338 char password[256];
340 bs = bdrv_find(device);
341 if (!bs) {
342 term_printf("device not found\n");
343 return;
345 if (eject_device(bs, 0) < 0)
346 return;
347 bdrv_open(bs, filename, 0);
348 if (bdrv_is_encrypted(bs)) {
349 term_printf("%s is encrypted.\n", device);
350 for(i = 0; i < 3; i++) {
351 monitor_readline("Password: ", 1, password, sizeof(password));
352 if (bdrv_set_key(bs, password) == 0)
353 break;
354 term_printf("invalid password\n");
359 static void do_screen_dump(const char *filename)
361 vga_hw_screen_dump(filename);
364 static void do_log(const char *items)
366 int mask;
368 if (!strcmp(items, "none")) {
369 mask = 0;
370 } else {
371 mask = cpu_str_to_log_mask(items);
372 if (!mask) {
373 help_cmd("log");
374 return;
377 cpu_set_log(mask);
380 static void do_savevm(const char *filename)
382 if (qemu_savevm(filename) < 0)
383 term_printf("I/O error when saving VM to '%s'\n", filename);
386 static void do_loadvm(const char *filename)
388 if (qemu_loadvm(filename) < 0)
389 term_printf("I/O error when loading VM from '%s'\n", filename);
392 static void do_stop(void)
394 vm_stop(EXCP_INTERRUPT);
397 static void do_cont(void)
399 vm_start();
402 #ifdef CONFIG_GDBSTUB
403 static void do_gdbserver(int has_port, int port)
405 if (!has_port)
406 port = DEFAULT_GDBSTUB_PORT;
407 if (gdbserver_start(port) < 0) {
408 qemu_printf("Could not open gdbserver socket on port %d\n", port);
409 } else {
410 qemu_printf("Waiting gdb connection on port %d\n", port);
413 #endif
415 static void term_printc(int c)
417 term_printf("'");
418 switch(c) {
419 case '\'':
420 term_printf("\\'");
421 break;
422 case '\\':
423 term_printf("\\\\");
424 break;
425 case '\n':
426 term_printf("\\n");
427 break;
428 case '\r':
429 term_printf("\\r");
430 break;
431 default:
432 if (c >= 32 && c <= 126) {
433 term_printf("%c", c);
434 } else {
435 term_printf("\\x%02x", c);
437 break;
439 term_printf("'");
442 static void memory_dump(int count, int format, int wsize,
443 target_ulong addr, int is_physical)
445 CPUState *env;
446 int nb_per_line, l, line_size, i, max_digits, len;
447 uint8_t buf[16];
448 uint64_t v;
450 if (format == 'i') {
451 int flags;
452 flags = 0;
453 env = mon_get_cpu();
454 if (!env && !is_physical)
455 return;
456 #ifdef TARGET_I386
457 if (wsize == 2) {
458 flags = 1;
459 } else if (wsize == 4) {
460 flags = 0;
461 } else {
462 /* as default we use the current CS size */
463 flags = 0;
464 if (env) {
465 #ifdef TARGET_X86_64
466 if ((env->efer & MSR_EFER_LMA) &&
467 (env->segs[R_CS].flags & DESC_L_MASK))
468 flags = 2;
469 else
470 #endif
471 if (!(env->segs[R_CS].flags & DESC_B_MASK))
472 flags = 1;
475 #endif
476 monitor_disas(env, addr, count, is_physical, flags);
477 return;
480 len = wsize * count;
481 if (wsize == 1)
482 line_size = 8;
483 else
484 line_size = 16;
485 nb_per_line = line_size / wsize;
486 max_digits = 0;
488 switch(format) {
489 case 'o':
490 max_digits = (wsize * 8 + 2) / 3;
491 break;
492 default:
493 case 'x':
494 max_digits = (wsize * 8) / 4;
495 break;
496 case 'u':
497 case 'd':
498 max_digits = (wsize * 8 * 10 + 32) / 33;
499 break;
500 case 'c':
501 wsize = 1;
502 break;
505 while (len > 0) {
506 term_printf(TARGET_FMT_lx ":", addr);
507 l = len;
508 if (l > line_size)
509 l = line_size;
510 if (is_physical) {
511 cpu_physical_memory_rw(addr, buf, l, 0);
512 } else {
513 env = mon_get_cpu();
514 if (!env)
515 break;
516 cpu_memory_rw_debug(env, addr, buf, l, 0);
518 i = 0;
519 while (i < l) {
520 switch(wsize) {
521 default:
522 case 1:
523 v = ldub_raw(buf + i);
524 break;
525 case 2:
526 v = lduw_raw(buf + i);
527 break;
528 case 4:
529 v = (uint32_t)ldl_raw(buf + i);
530 break;
531 case 8:
532 v = ldq_raw(buf + i);
533 break;
535 term_printf(" ");
536 switch(format) {
537 case 'o':
538 term_printf("%#*" PRIo64, max_digits, v);
539 break;
540 case 'x':
541 term_printf("0x%0*" PRIx64, max_digits, v);
542 break;
543 case 'u':
544 term_printf("%*" PRIu64, max_digits, v);
545 break;
546 case 'd':
547 term_printf("%*" PRId64, max_digits, v);
548 break;
549 case 'c':
550 term_printc(v);
551 break;
553 i += wsize;
555 term_printf("\n");
556 addr += l;
557 len -= l;
561 #if TARGET_LONG_BITS == 64
562 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
563 #else
564 #define GET_TLONG(h, l) (l)
565 #endif
567 static void do_memory_dump(int count, int format, int size,
568 uint32_t addrh, uint32_t addrl)
570 target_long addr = GET_TLONG(addrh, addrl);
571 memory_dump(count, format, size, addr, 0);
574 static void do_physical_memory_dump(int count, int format, int size,
575 uint32_t addrh, uint32_t addrl)
578 target_long addr = GET_TLONG(addrh, addrl);
579 memory_dump(count, format, size, addr, 1);
582 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
584 target_long val = GET_TLONG(valh, vall);
585 #if TARGET_LONG_BITS == 32
586 switch(format) {
587 case 'o':
588 term_printf("%#o", val);
589 break;
590 case 'x':
591 term_printf("%#x", val);
592 break;
593 case 'u':
594 term_printf("%u", val);
595 break;
596 default:
597 case 'd':
598 term_printf("%d", val);
599 break;
600 case 'c':
601 term_printc(val);
602 break;
604 #else
605 switch(format) {
606 case 'o':
607 term_printf("%#" PRIo64, val);
608 break;
609 case 'x':
610 term_printf("%#" PRIx64, val);
611 break;
612 case 'u':
613 term_printf("%" PRIu64, val);
614 break;
615 default:
616 case 'd':
617 term_printf("%" PRId64, val);
618 break;
619 case 'c':
620 term_printc(val);
621 break;
623 #endif
624 term_printf("\n");
627 static void do_sum(uint32_t start, uint32_t size)
629 uint32_t addr;
630 uint8_t buf[1];
631 uint16_t sum;
633 sum = 0;
634 for(addr = start; addr < (start + size); addr++) {
635 cpu_physical_memory_rw(addr, buf, 1, 0);
636 /* BSD sum algorithm ('sum' Unix command) */
637 sum = (sum >> 1) | (sum << 15);
638 sum += buf[0];
640 term_printf("%05d\n", sum);
643 typedef struct {
644 int keycode;
645 const char *name;
646 } KeyDef;
648 static const KeyDef key_defs[] = {
649 { 0x2a, "shift" },
650 { 0x36, "shift_r" },
652 { 0x38, "alt" },
653 { 0xb8, "alt_r" },
654 { 0x1d, "ctrl" },
655 { 0x9d, "ctrl_r" },
657 { 0xdd, "menu" },
659 { 0x01, "esc" },
661 { 0x02, "1" },
662 { 0x03, "2" },
663 { 0x04, "3" },
664 { 0x05, "4" },
665 { 0x06, "5" },
666 { 0x07, "6" },
667 { 0x08, "7" },
668 { 0x09, "8" },
669 { 0x0a, "9" },
670 { 0x0b, "0" },
671 { 0x0c, "minus" },
672 { 0x0d, "equal" },
673 { 0x0e, "backspace" },
675 { 0x0f, "tab" },
676 { 0x10, "q" },
677 { 0x11, "w" },
678 { 0x12, "e" },
679 { 0x13, "r" },
680 { 0x14, "t" },
681 { 0x15, "y" },
682 { 0x16, "u" },
683 { 0x17, "i" },
684 { 0x18, "o" },
685 { 0x19, "p" },
687 { 0x1c, "ret" },
689 { 0x1e, "a" },
690 { 0x1f, "s" },
691 { 0x20, "d" },
692 { 0x21, "f" },
693 { 0x22, "g" },
694 { 0x23, "h" },
695 { 0x24, "j" },
696 { 0x25, "k" },
697 { 0x26, "l" },
699 { 0x2c, "z" },
700 { 0x2d, "x" },
701 { 0x2e, "c" },
702 { 0x2f, "v" },
703 { 0x30, "b" },
704 { 0x31, "n" },
705 { 0x32, "m" },
707 { 0x39, "spc" },
708 { 0x3a, "caps_lock" },
709 { 0x3b, "f1" },
710 { 0x3c, "f2" },
711 { 0x3d, "f3" },
712 { 0x3e, "f4" },
713 { 0x3f, "f5" },
714 { 0x40, "f6" },
715 { 0x41, "f7" },
716 { 0x42, "f8" },
717 { 0x43, "f9" },
718 { 0x44, "f10" },
719 { 0x45, "num_lock" },
720 { 0x46, "scroll_lock" },
722 { 0xb5, "kp_divide" },
723 { 0x37, "kp_multiply" },
724 { 0x4a, "kp_substract" },
725 { 0x4e, "kp_add" },
726 { 0x9c, "kp_enter" },
727 { 0x53, "kp_decimal" },
729 { 0x52, "kp_0" },
730 { 0x4f, "kp_1" },
731 { 0x50, "kp_2" },
732 { 0x51, "kp_3" },
733 { 0x4b, "kp_4" },
734 { 0x4c, "kp_5" },
735 { 0x4d, "kp_6" },
736 { 0x47, "kp_7" },
737 { 0x48, "kp_8" },
738 { 0x49, "kp_9" },
740 { 0x56, "<" },
742 { 0x57, "f11" },
743 { 0x58, "f12" },
745 { 0xb7, "print" },
747 { 0xc7, "home" },
748 { 0xc9, "pgup" },
749 { 0xd1, "pgdn" },
750 { 0xcf, "end" },
752 { 0xcb, "left" },
753 { 0xc8, "up" },
754 { 0xd0, "down" },
755 { 0xcd, "right" },
757 { 0xd2, "insert" },
758 { 0xd3, "delete" },
759 { 0, NULL },
762 static int get_keycode(const char *key)
764 const KeyDef *p;
765 char *endp;
766 int ret;
768 for(p = key_defs; p->name != NULL; p++) {
769 if (!strcmp(key, p->name))
770 return p->keycode;
772 if (strstart(key, "0x", NULL)) {
773 ret = strtoul(key, &endp, 0);
774 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
775 return ret;
777 return -1;
780 static void do_send_key(const char *string)
782 char keybuf[16], *q;
783 uint8_t keycodes[16];
784 const char *p;
785 int nb_keycodes, keycode, i;
787 nb_keycodes = 0;
788 p = string;
789 while (*p != '\0') {
790 q = keybuf;
791 while (*p != '\0' && *p != '-') {
792 if ((q - keybuf) < sizeof(keybuf) - 1) {
793 *q++ = *p;
795 p++;
797 *q = '\0';
798 keycode = get_keycode(keybuf);
799 if (keycode < 0) {
800 term_printf("unknown key: '%s'\n", keybuf);
801 return;
803 keycodes[nb_keycodes++] = keycode;
804 if (*p == '\0')
805 break;
806 p++;
808 /* key down events */
809 for(i = 0; i < nb_keycodes; i++) {
810 keycode = keycodes[i];
811 if (keycode & 0x80)
812 kbd_put_keycode(0xe0);
813 kbd_put_keycode(keycode & 0x7f);
815 /* key up events */
816 for(i = nb_keycodes - 1; i >= 0; i--) {
817 keycode = keycodes[i];
818 if (keycode & 0x80)
819 kbd_put_keycode(0xe0);
820 kbd_put_keycode(keycode | 0x80);
824 static int mouse_button_state;
826 static void do_mouse_move(const char *dx_str, const char *dy_str,
827 const char *dz_str)
829 int dx, dy, dz;
830 dx = strtol(dx_str, NULL, 0);
831 dy = strtol(dy_str, NULL, 0);
832 dz = 0;
833 if (dz_str)
834 dz = strtol(dz_str, NULL, 0);
835 kbd_mouse_event(dx, dy, dz, mouse_button_state);
838 static void do_mouse_button(int button_state)
840 mouse_button_state = button_state;
841 kbd_mouse_event(0, 0, 0, mouse_button_state);
844 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
846 uint32_t val;
847 int suffix;
849 if (has_index) {
850 cpu_outb(NULL, addr & 0xffff, index & 0xff);
851 addr++;
853 addr &= 0xffff;
855 switch(size) {
856 default:
857 case 1:
858 val = cpu_inb(NULL, addr);
859 suffix = 'b';
860 break;
861 case 2:
862 val = cpu_inw(NULL, addr);
863 suffix = 'w';
864 break;
865 case 4:
866 val = cpu_inl(NULL, addr);
867 suffix = 'l';
868 break;
870 term_printf("port%c[0x%04x] = %#0*x\n",
871 suffix, addr, size * 2, val);
874 static void do_system_reset(void)
876 qemu_system_reset_request();
879 static void do_system_powerdown(void)
881 qemu_system_powerdown_request();
884 #if defined(TARGET_I386)
885 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
887 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
888 addr,
889 pte & mask,
890 pte & PG_GLOBAL_MASK ? 'G' : '-',
891 pte & PG_PSE_MASK ? 'P' : '-',
892 pte & PG_DIRTY_MASK ? 'D' : '-',
893 pte & PG_ACCESSED_MASK ? 'A' : '-',
894 pte & PG_PCD_MASK ? 'C' : '-',
895 pte & PG_PWT_MASK ? 'T' : '-',
896 pte & PG_USER_MASK ? 'U' : '-',
897 pte & PG_RW_MASK ? 'W' : '-');
900 static void tlb_info(void)
902 CPUState *env;
903 int l1, l2;
904 uint32_t pgd, pde, pte;
906 env = mon_get_cpu();
907 if (!env)
908 return;
910 if (!(env->cr[0] & CR0_PG_MASK)) {
911 term_printf("PG disabled\n");
912 return;
914 pgd = env->cr[3] & ~0xfff;
915 for(l1 = 0; l1 < 1024; l1++) {
916 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
917 pde = le32_to_cpu(pde);
918 if (pde & PG_PRESENT_MASK) {
919 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
920 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
921 } else {
922 for(l2 = 0; l2 < 1024; l2++) {
923 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
924 (uint8_t *)&pte, 4);
925 pte = le32_to_cpu(pte);
926 if (pte & PG_PRESENT_MASK) {
927 print_pte((l1 << 22) + (l2 << 12),
928 pte & ~PG_PSE_MASK,
929 ~0xfff);
937 static void mem_print(uint32_t *pstart, int *plast_prot,
938 uint32_t end, int prot)
940 int prot1;
941 prot1 = *plast_prot;
942 if (prot != prot1) {
943 if (*pstart != -1) {
944 term_printf("%08x-%08x %08x %c%c%c\n",
945 *pstart, end, end - *pstart,
946 prot1 & PG_USER_MASK ? 'u' : '-',
947 'r',
948 prot1 & PG_RW_MASK ? 'w' : '-');
950 if (prot != 0)
951 *pstart = end;
952 else
953 *pstart = -1;
954 *plast_prot = prot;
958 static void mem_info(void)
960 CPUState *env;
961 int l1, l2, prot, last_prot;
962 uint32_t pgd, pde, pte, start, end;
964 env = mon_get_cpu();
965 if (!env)
966 return;
968 if (!(env->cr[0] & CR0_PG_MASK)) {
969 term_printf("PG disabled\n");
970 return;
972 pgd = env->cr[3] & ~0xfff;
973 last_prot = 0;
974 start = -1;
975 for(l1 = 0; l1 < 1024; l1++) {
976 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
977 pde = le32_to_cpu(pde);
978 end = l1 << 22;
979 if (pde & PG_PRESENT_MASK) {
980 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
981 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
982 mem_print(&start, &last_prot, end, prot);
983 } else {
984 for(l2 = 0; l2 < 1024; l2++) {
985 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
986 (uint8_t *)&pte, 4);
987 pte = le32_to_cpu(pte);
988 end = (l1 << 22) + (l2 << 12);
989 if (pte & PG_PRESENT_MASK) {
990 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
991 } else {
992 prot = 0;
994 mem_print(&start, &last_prot, end, prot);
997 } else {
998 prot = 0;
999 mem_print(&start, &last_prot, end, prot);
1003 #endif
1005 static void do_info_kqemu(void)
1007 #ifdef USE_KQEMU
1008 CPUState *env;
1009 int val;
1010 val = 0;
1011 env = mon_get_cpu();
1012 if (!env) {
1013 term_printf("No cpu initialized yet");
1014 return;
1016 val = env->kqemu_enabled;
1017 term_printf("kqemu support: ");
1018 switch(val) {
1019 default:
1020 case 0:
1021 term_printf("disabled\n");
1022 break;
1023 case 1:
1024 term_printf("enabled for user code\n");
1025 break;
1026 case 2:
1027 term_printf("enabled for user and kernel code\n");
1028 break;
1030 #else
1031 term_printf("kqemu support: not compiled\n");
1032 #endif
1035 #ifdef CONFIG_PROFILER
1037 int64_t kqemu_time;
1038 int64_t qemu_time;
1039 int64_t kqemu_exec_count;
1040 int64_t dev_time;
1041 int64_t kqemu_ret_int_count;
1042 int64_t kqemu_ret_excp_count;
1043 int64_t kqemu_ret_intr_count;
1045 static void do_info_profile(void)
1047 int64_t total;
1048 total = qemu_time;
1049 if (total == 0)
1050 total = 1;
1051 term_printf("async time %" PRId64 " (%0.3f)\n",
1052 dev_time, dev_time / (double)ticks_per_sec);
1053 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1054 qemu_time, qemu_time / (double)ticks_per_sec);
1055 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1056 kqemu_time, kqemu_time / (double)ticks_per_sec,
1057 kqemu_time / (double)total * 100.0,
1058 kqemu_exec_count,
1059 kqemu_ret_int_count,
1060 kqemu_ret_excp_count,
1061 kqemu_ret_intr_count);
1062 qemu_time = 0;
1063 kqemu_time = 0;
1064 kqemu_exec_count = 0;
1065 dev_time = 0;
1066 kqemu_ret_int_count = 0;
1067 kqemu_ret_excp_count = 0;
1068 kqemu_ret_intr_count = 0;
1069 #ifdef USE_KQEMU
1070 kqemu_record_dump();
1071 #endif
1073 #else
1074 static void do_info_profile(void)
1076 term_printf("Internal profiler not compiled\n");
1078 #endif
1080 /* Capture support */
1081 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1083 static void do_info_capture (void)
1085 int i;
1086 CaptureState *s;
1088 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1089 term_printf ("[%d]: ", i);
1090 s->ops.info (s->opaque);
1094 static void do_stop_capture (int n)
1096 int i;
1097 CaptureState *s;
1099 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1100 if (i == n) {
1101 s->ops.destroy (s->opaque);
1102 LIST_REMOVE (s, entries);
1103 qemu_free (s);
1104 return;
1109 #ifdef HAS_AUDIO
1110 int wav_start_capture (CaptureState *s, const char *path, int freq,
1111 int bits, int nchannels);
1113 static void do_wav_capture (const char *path,
1114 int has_freq, int freq,
1115 int has_bits, int bits,
1116 int has_channels, int nchannels)
1118 CaptureState *s;
1120 s = qemu_mallocz (sizeof (*s));
1121 if (!s) {
1122 term_printf ("Not enough memory to add wave capture\n");
1123 return;
1126 freq = has_freq ? freq : 44100;
1127 bits = has_bits ? bits : 16;
1128 nchannels = has_channels ? nchannels : 2;
1130 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1131 term_printf ("Faied to add wave capture\n");
1132 qemu_free (s);
1134 LIST_INSERT_HEAD (&capture_head, s, entries);
1136 #endif
1138 static term_cmd_t term_cmds[] = {
1139 { "help|?", "s?", do_help,
1140 "[cmd]", "show the help" },
1141 { "commit", "", do_commit,
1142 "", "commit changes to the disk images (if -snapshot is used)" },
1143 { "info", "s?", do_info,
1144 "subcommand", "show various information about the system state" },
1145 { "q|quit", "", do_quit,
1146 "", "quit the emulator" },
1147 { "eject", "-fB", do_eject,
1148 "[-f] device", "eject a removable media (use -f to force it)" },
1149 { "change", "BF", do_change,
1150 "device filename", "change a removable media" },
1151 { "screendump", "F", do_screen_dump,
1152 "filename", "save screen into PPM image 'filename'" },
1153 { "log", "s", do_log,
1154 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1155 { "savevm", "F", do_savevm,
1156 "filename", "save the whole virtual machine state to 'filename'" },
1157 { "loadvm", "F", do_loadvm,
1158 "filename", "restore the whole virtual machine state from 'filename'" },
1159 { "stop", "", do_stop,
1160 "", "stop emulation", },
1161 { "c|cont", "", do_cont,
1162 "", "resume emulation", },
1163 #ifdef CONFIG_GDBSTUB
1164 { "gdbserver", "i?", do_gdbserver,
1165 "[port]", "start gdbserver session (default port=1234)", },
1166 #endif
1167 { "x", "/l", do_memory_dump,
1168 "/fmt addr", "virtual memory dump starting at 'addr'", },
1169 { "xp", "/l", do_physical_memory_dump,
1170 "/fmt addr", "physical memory dump starting at 'addr'", },
1171 { "p|print", "/l", do_print,
1172 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1173 { "i", "/ii.", do_ioport_read,
1174 "/fmt addr", "I/O port read" },
1176 { "sendkey", "s", do_send_key,
1177 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1178 { "system_reset", "", do_system_reset,
1179 "", "reset the system" },
1180 { "system_powerdown", "", do_system_powerdown,
1181 "", "send system power down event" },
1182 { "sum", "ii", do_sum,
1183 "addr size", "compute the checksum of a memory region" },
1184 { "usb_add", "s", do_usb_add,
1185 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1186 { "usb_del", "s", do_usb_del,
1187 "device", "remove USB device 'bus.addr'" },
1188 { "cpu", "i", do_cpu_set,
1189 "index", "set the default CPU" },
1190 { "mouse_move", "sss?", do_mouse_move,
1191 "dx dy [dz]", "send mouse move events" },
1192 { "mouse_button", "i", do_mouse_button,
1193 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1194 #ifdef HAS_AUDIO
1195 { "wavcapture", "si?i?i?", do_wav_capture,
1196 "path [frequency bits channels]",
1197 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1198 #endif
1199 { "stopcapture", "i", do_stop_capture,
1200 "capture index", "stop capture" },
1201 { NULL, NULL, },
1204 static term_cmd_t info_cmds[] = {
1205 { "version", "", do_info_version,
1206 "", "show the version of qemu" },
1207 { "network", "", do_info_network,
1208 "", "show the network state" },
1209 { "block", "", do_info_block,
1210 "", "show the block devices" },
1211 { "registers", "", do_info_registers,
1212 "", "show the cpu registers" },
1213 { "cpus", "", do_info_cpus,
1214 "", "show infos for each CPU" },
1215 { "history", "", do_info_history,
1216 "", "show the command line history", },
1217 { "irq", "", irq_info,
1218 "", "show the interrupts statistics (if available)", },
1219 { "pic", "", pic_info,
1220 "", "show i8259 (PIC) state", },
1221 { "pci", "", pci_info,
1222 "", "show PCI info", },
1223 #if defined(TARGET_I386)
1224 { "tlb", "", tlb_info,
1225 "", "show virtual to physical memory mappings", },
1226 { "mem", "", mem_info,
1227 "", "show the active virtual memory mappings", },
1228 #endif
1229 { "jit", "", do_info_jit,
1230 "", "show dynamic compiler info", },
1231 { "kqemu", "", do_info_kqemu,
1232 "", "show kqemu information", },
1233 { "usb", "", usb_info,
1234 "", "show guest USB devices", },
1235 { "usbhost", "", usb_host_info,
1236 "", "show host USB devices", },
1237 { "profile", "", do_info_profile,
1238 "", "show profiling information", },
1239 { "capture", "", do_info_capture,
1240 "show capture information" },
1241 { NULL, NULL, },
1244 /*******************************************************************/
1246 static const char *pch;
1247 static jmp_buf expr_env;
1249 #define MD_TLONG 0
1250 #define MD_I32 1
1252 typedef struct MonitorDef {
1253 const char *name;
1254 int offset;
1255 target_long (*get_value)(struct MonitorDef *md, int val);
1256 int type;
1257 } MonitorDef;
1259 #if defined(TARGET_I386)
1260 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1262 CPUState *env = mon_get_cpu();
1263 if (!env)
1264 return 0;
1265 return env->eip + env->segs[R_CS].base;
1267 #endif
1269 #if defined(TARGET_PPC)
1270 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1272 CPUState *env = mon_get_cpu();
1273 unsigned int u;
1274 int i;
1276 if (!env)
1277 return 0;
1279 u = 0;
1280 for (i = 0; i < 8; i++)
1281 u |= env->crf[i] << (32 - (4 * i));
1283 return u;
1286 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1288 CPUState *env = mon_get_cpu();
1289 if (!env)
1290 return 0;
1291 return (env->msr[MSR_POW] << MSR_POW) |
1292 (env->msr[MSR_ILE] << MSR_ILE) |
1293 (env->msr[MSR_EE] << MSR_EE) |
1294 (env->msr[MSR_PR] << MSR_PR) |
1295 (env->msr[MSR_FP] << MSR_FP) |
1296 (env->msr[MSR_ME] << MSR_ME) |
1297 (env->msr[MSR_FE0] << MSR_FE0) |
1298 (env->msr[MSR_SE] << MSR_SE) |
1299 (env->msr[MSR_BE] << MSR_BE) |
1300 (env->msr[MSR_FE1] << MSR_FE1) |
1301 (env->msr[MSR_IP] << MSR_IP) |
1302 (env->msr[MSR_IR] << MSR_IR) |
1303 (env->msr[MSR_DR] << MSR_DR) |
1304 (env->msr[MSR_RI] << MSR_RI) |
1305 (env->msr[MSR_LE] << MSR_LE);
1308 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1310 CPUState *env = mon_get_cpu();
1311 if (!env)
1312 return 0;
1313 return (env->xer[XER_SO] << XER_SO) |
1314 (env->xer[XER_OV] << XER_OV) |
1315 (env->xer[XER_CA] << XER_CA) |
1316 (env->xer[XER_BC] << XER_BC);
1319 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1321 CPUState *env = mon_get_cpu();
1322 if (!env)
1323 return 0;
1324 return cpu_ppc_load_decr(env);
1327 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1329 CPUState *env = mon_get_cpu();
1330 if (!env)
1331 return 0;
1332 return cpu_ppc_load_tbu(env);
1335 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1337 CPUState *env = mon_get_cpu();
1338 if (!env)
1339 return 0;
1340 return cpu_ppc_load_tbl(env);
1342 #endif
1344 #if defined(TARGET_SPARC)
1345 #ifndef TARGET_SPARC64
1346 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1348 CPUState *env = mon_get_cpu();
1349 if (!env)
1350 return 0;
1351 return GET_PSR(env);
1353 #endif
1355 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1357 CPUState *env = mon_get_cpu();
1358 if (!env)
1359 return 0;
1360 return env->regwptr[val];
1362 #endif
1364 static MonitorDef monitor_defs[] = {
1365 #ifdef TARGET_I386
1367 #define SEG(name, seg) \
1368 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1369 { name ".base", offsetof(CPUState, segs[seg].base) },\
1370 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1372 { "eax", offsetof(CPUState, regs[0]) },
1373 { "ecx", offsetof(CPUState, regs[1]) },
1374 { "edx", offsetof(CPUState, regs[2]) },
1375 { "ebx", offsetof(CPUState, regs[3]) },
1376 { "esp|sp", offsetof(CPUState, regs[4]) },
1377 { "ebp|fp", offsetof(CPUState, regs[5]) },
1378 { "esi", offsetof(CPUState, regs[6]) },
1379 { "edi", offsetof(CPUState, regs[7]) },
1380 #ifdef TARGET_X86_64
1381 { "r8", offsetof(CPUState, regs[8]) },
1382 { "r9", offsetof(CPUState, regs[9]) },
1383 { "r10", offsetof(CPUState, regs[10]) },
1384 { "r11", offsetof(CPUState, regs[11]) },
1385 { "r12", offsetof(CPUState, regs[12]) },
1386 { "r13", offsetof(CPUState, regs[13]) },
1387 { "r14", offsetof(CPUState, regs[14]) },
1388 { "r15", offsetof(CPUState, regs[15]) },
1389 #endif
1390 { "eflags", offsetof(CPUState, eflags) },
1391 { "eip", offsetof(CPUState, eip) },
1392 SEG("cs", R_CS)
1393 SEG("ds", R_DS)
1394 SEG("es", R_ES)
1395 SEG("ss", R_SS)
1396 SEG("fs", R_FS)
1397 SEG("gs", R_GS)
1398 { "pc", 0, monitor_get_pc, },
1399 #elif defined(TARGET_PPC)
1400 { "r0", offsetof(CPUState, gpr[0]) },
1401 { "r1", offsetof(CPUState, gpr[1]) },
1402 { "r2", offsetof(CPUState, gpr[2]) },
1403 { "r3", offsetof(CPUState, gpr[3]) },
1404 { "r4", offsetof(CPUState, gpr[4]) },
1405 { "r5", offsetof(CPUState, gpr[5]) },
1406 { "r6", offsetof(CPUState, gpr[6]) },
1407 { "r7", offsetof(CPUState, gpr[7]) },
1408 { "r8", offsetof(CPUState, gpr[8]) },
1409 { "r9", offsetof(CPUState, gpr[9]) },
1410 { "r10", offsetof(CPUState, gpr[10]) },
1411 { "r11", offsetof(CPUState, gpr[11]) },
1412 { "r12", offsetof(CPUState, gpr[12]) },
1413 { "r13", offsetof(CPUState, gpr[13]) },
1414 { "r14", offsetof(CPUState, gpr[14]) },
1415 { "r15", offsetof(CPUState, gpr[15]) },
1416 { "r16", offsetof(CPUState, gpr[16]) },
1417 { "r17", offsetof(CPUState, gpr[17]) },
1418 { "r18", offsetof(CPUState, gpr[18]) },
1419 { "r19", offsetof(CPUState, gpr[19]) },
1420 { "r20", offsetof(CPUState, gpr[20]) },
1421 { "r21", offsetof(CPUState, gpr[21]) },
1422 { "r22", offsetof(CPUState, gpr[22]) },
1423 { "r23", offsetof(CPUState, gpr[23]) },
1424 { "r24", offsetof(CPUState, gpr[24]) },
1425 { "r25", offsetof(CPUState, gpr[25]) },
1426 { "r26", offsetof(CPUState, gpr[26]) },
1427 { "r27", offsetof(CPUState, gpr[27]) },
1428 { "r28", offsetof(CPUState, gpr[28]) },
1429 { "r29", offsetof(CPUState, gpr[29]) },
1430 { "r30", offsetof(CPUState, gpr[30]) },
1431 { "r31", offsetof(CPUState, gpr[31]) },
1432 { "nip|pc", offsetof(CPUState, nip) },
1433 { "lr", offsetof(CPUState, lr) },
1434 { "ctr", offsetof(CPUState, ctr) },
1435 { "decr", 0, &monitor_get_decr, },
1436 { "ccr", 0, &monitor_get_ccr, },
1437 { "msr", 0, &monitor_get_msr, },
1438 { "xer", 0, &monitor_get_xer, },
1439 { "tbu", 0, &monitor_get_tbu, },
1440 { "tbl", 0, &monitor_get_tbl, },
1441 { "sdr1", offsetof(CPUState, sdr1) },
1442 { "sr0", offsetof(CPUState, sr[0]) },
1443 { "sr1", offsetof(CPUState, sr[1]) },
1444 { "sr2", offsetof(CPUState, sr[2]) },
1445 { "sr3", offsetof(CPUState, sr[3]) },
1446 { "sr4", offsetof(CPUState, sr[4]) },
1447 { "sr5", offsetof(CPUState, sr[5]) },
1448 { "sr6", offsetof(CPUState, sr[6]) },
1449 { "sr7", offsetof(CPUState, sr[7]) },
1450 { "sr8", offsetof(CPUState, sr[8]) },
1451 { "sr9", offsetof(CPUState, sr[9]) },
1452 { "sr10", offsetof(CPUState, sr[10]) },
1453 { "sr11", offsetof(CPUState, sr[11]) },
1454 { "sr12", offsetof(CPUState, sr[12]) },
1455 { "sr13", offsetof(CPUState, sr[13]) },
1456 { "sr14", offsetof(CPUState, sr[14]) },
1457 { "sr15", offsetof(CPUState, sr[15]) },
1458 /* Too lazy to put BATs and SPRs ... */
1459 #elif defined(TARGET_SPARC)
1460 { "g0", offsetof(CPUState, gregs[0]) },
1461 { "g1", offsetof(CPUState, gregs[1]) },
1462 { "g2", offsetof(CPUState, gregs[2]) },
1463 { "g3", offsetof(CPUState, gregs[3]) },
1464 { "g4", offsetof(CPUState, gregs[4]) },
1465 { "g5", offsetof(CPUState, gregs[5]) },
1466 { "g6", offsetof(CPUState, gregs[6]) },
1467 { "g7", offsetof(CPUState, gregs[7]) },
1468 { "o0", 0, monitor_get_reg },
1469 { "o1", 1, monitor_get_reg },
1470 { "o2", 2, monitor_get_reg },
1471 { "o3", 3, monitor_get_reg },
1472 { "o4", 4, monitor_get_reg },
1473 { "o5", 5, monitor_get_reg },
1474 { "o6", 6, monitor_get_reg },
1475 { "o7", 7, monitor_get_reg },
1476 { "l0", 8, monitor_get_reg },
1477 { "l1", 9, monitor_get_reg },
1478 { "l2", 10, monitor_get_reg },
1479 { "l3", 11, monitor_get_reg },
1480 { "l4", 12, monitor_get_reg },
1481 { "l5", 13, monitor_get_reg },
1482 { "l6", 14, monitor_get_reg },
1483 { "l7", 15, monitor_get_reg },
1484 { "i0", 16, monitor_get_reg },
1485 { "i1", 17, monitor_get_reg },
1486 { "i2", 18, monitor_get_reg },
1487 { "i3", 19, monitor_get_reg },
1488 { "i4", 20, monitor_get_reg },
1489 { "i5", 21, monitor_get_reg },
1490 { "i6", 22, monitor_get_reg },
1491 { "i7", 23, monitor_get_reg },
1492 { "pc", offsetof(CPUState, pc) },
1493 { "npc", offsetof(CPUState, npc) },
1494 { "y", offsetof(CPUState, y) },
1495 #ifndef TARGET_SPARC64
1496 { "psr", 0, &monitor_get_psr, },
1497 { "wim", offsetof(CPUState, wim) },
1498 #endif
1499 { "tbr", offsetof(CPUState, tbr) },
1500 { "fsr", offsetof(CPUState, fsr) },
1501 { "f0", offsetof(CPUState, fpr[0]) },
1502 { "f1", offsetof(CPUState, fpr[1]) },
1503 { "f2", offsetof(CPUState, fpr[2]) },
1504 { "f3", offsetof(CPUState, fpr[3]) },
1505 { "f4", offsetof(CPUState, fpr[4]) },
1506 { "f5", offsetof(CPUState, fpr[5]) },
1507 { "f6", offsetof(CPUState, fpr[6]) },
1508 { "f7", offsetof(CPUState, fpr[7]) },
1509 { "f8", offsetof(CPUState, fpr[8]) },
1510 { "f9", offsetof(CPUState, fpr[9]) },
1511 { "f10", offsetof(CPUState, fpr[10]) },
1512 { "f11", offsetof(CPUState, fpr[11]) },
1513 { "f12", offsetof(CPUState, fpr[12]) },
1514 { "f13", offsetof(CPUState, fpr[13]) },
1515 { "f14", offsetof(CPUState, fpr[14]) },
1516 { "f15", offsetof(CPUState, fpr[15]) },
1517 { "f16", offsetof(CPUState, fpr[16]) },
1518 { "f17", offsetof(CPUState, fpr[17]) },
1519 { "f18", offsetof(CPUState, fpr[18]) },
1520 { "f19", offsetof(CPUState, fpr[19]) },
1521 { "f20", offsetof(CPUState, fpr[20]) },
1522 { "f21", offsetof(CPUState, fpr[21]) },
1523 { "f22", offsetof(CPUState, fpr[22]) },
1524 { "f23", offsetof(CPUState, fpr[23]) },
1525 { "f24", offsetof(CPUState, fpr[24]) },
1526 { "f25", offsetof(CPUState, fpr[25]) },
1527 { "f26", offsetof(CPUState, fpr[26]) },
1528 { "f27", offsetof(CPUState, fpr[27]) },
1529 { "f28", offsetof(CPUState, fpr[28]) },
1530 { "f29", offsetof(CPUState, fpr[29]) },
1531 { "f30", offsetof(CPUState, fpr[30]) },
1532 { "f31", offsetof(CPUState, fpr[31]) },
1533 #ifdef TARGET_SPARC64
1534 { "f32", offsetof(CPUState, fpr[32]) },
1535 { "f34", offsetof(CPUState, fpr[34]) },
1536 { "f36", offsetof(CPUState, fpr[36]) },
1537 { "f38", offsetof(CPUState, fpr[38]) },
1538 { "f40", offsetof(CPUState, fpr[40]) },
1539 { "f42", offsetof(CPUState, fpr[42]) },
1540 { "f44", offsetof(CPUState, fpr[44]) },
1541 { "f46", offsetof(CPUState, fpr[46]) },
1542 { "f48", offsetof(CPUState, fpr[48]) },
1543 { "f50", offsetof(CPUState, fpr[50]) },
1544 { "f52", offsetof(CPUState, fpr[52]) },
1545 { "f54", offsetof(CPUState, fpr[54]) },
1546 { "f56", offsetof(CPUState, fpr[56]) },
1547 { "f58", offsetof(CPUState, fpr[58]) },
1548 { "f60", offsetof(CPUState, fpr[60]) },
1549 { "f62", offsetof(CPUState, fpr[62]) },
1550 { "asi", offsetof(CPUState, asi) },
1551 { "pstate", offsetof(CPUState, pstate) },
1552 { "cansave", offsetof(CPUState, cansave) },
1553 { "canrestore", offsetof(CPUState, canrestore) },
1554 { "otherwin", offsetof(CPUState, otherwin) },
1555 { "wstate", offsetof(CPUState, wstate) },
1556 { "cleanwin", offsetof(CPUState, cleanwin) },
1557 { "fprs", offsetof(CPUState, fprs) },
1558 #endif
1559 #endif
1560 { NULL },
1563 static void expr_error(const char *fmt)
1565 term_printf(fmt);
1566 term_printf("\n");
1567 longjmp(expr_env, 1);
1570 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1571 static int get_monitor_def(target_long *pval, const char *name)
1573 MonitorDef *md;
1574 void *ptr;
1576 for(md = monitor_defs; md->name != NULL; md++) {
1577 if (compare_cmd(name, md->name)) {
1578 if (md->get_value) {
1579 *pval = md->get_value(md, md->offset);
1580 } else {
1581 CPUState *env = mon_get_cpu();
1582 if (!env)
1583 return -2;
1584 ptr = (uint8_t *)env + md->offset;
1585 switch(md->type) {
1586 case MD_I32:
1587 *pval = *(int32_t *)ptr;
1588 break;
1589 case MD_TLONG:
1590 *pval = *(target_long *)ptr;
1591 break;
1592 default:
1593 *pval = 0;
1594 break;
1597 return 0;
1600 return -1;
1603 static void next(void)
1605 if (pch != '\0') {
1606 pch++;
1607 while (isspace(*pch))
1608 pch++;
1612 static target_long expr_sum(void);
1614 static target_long expr_unary(void)
1616 target_long n;
1617 char *p;
1618 int ret;
1620 switch(*pch) {
1621 case '+':
1622 next();
1623 n = expr_unary();
1624 break;
1625 case '-':
1626 next();
1627 n = -expr_unary();
1628 break;
1629 case '~':
1630 next();
1631 n = ~expr_unary();
1632 break;
1633 case '(':
1634 next();
1635 n = expr_sum();
1636 if (*pch != ')') {
1637 expr_error("')' expected");
1639 next();
1640 break;
1641 case '\'':
1642 pch++;
1643 if (*pch == '\0')
1644 expr_error("character constant expected");
1645 n = *pch;
1646 pch++;
1647 if (*pch != '\'')
1648 expr_error("missing terminating \' character");
1649 next();
1650 break;
1651 case '$':
1653 char buf[128], *q;
1655 pch++;
1656 q = buf;
1657 while ((*pch >= 'a' && *pch <= 'z') ||
1658 (*pch >= 'A' && *pch <= 'Z') ||
1659 (*pch >= '0' && *pch <= '9') ||
1660 *pch == '_' || *pch == '.') {
1661 if ((q - buf) < sizeof(buf) - 1)
1662 *q++ = *pch;
1663 pch++;
1665 while (isspace(*pch))
1666 pch++;
1667 *q = 0;
1668 ret = get_monitor_def(&n, buf);
1669 if (ret == -1)
1670 expr_error("unknown register");
1671 else if (ret == -2)
1672 expr_error("no cpu defined");
1674 break;
1675 case '\0':
1676 expr_error("unexpected end of expression");
1677 n = 0;
1678 break;
1679 default:
1680 #if TARGET_LONG_BITS == 64
1681 n = strtoull(pch, &p, 0);
1682 #else
1683 n = strtoul(pch, &p, 0);
1684 #endif
1685 if (pch == p) {
1686 expr_error("invalid char in expression");
1688 pch = p;
1689 while (isspace(*pch))
1690 pch++;
1691 break;
1693 return n;
1697 static target_long expr_prod(void)
1699 target_long val, val2;
1700 int op;
1702 val = expr_unary();
1703 for(;;) {
1704 op = *pch;
1705 if (op != '*' && op != '/' && op != '%')
1706 break;
1707 next();
1708 val2 = expr_unary();
1709 switch(op) {
1710 default:
1711 case '*':
1712 val *= val2;
1713 break;
1714 case '/':
1715 case '%':
1716 if (val2 == 0)
1717 expr_error("division by zero");
1718 if (op == '/')
1719 val /= val2;
1720 else
1721 val %= val2;
1722 break;
1725 return val;
1728 static target_long expr_logic(void)
1730 target_long val, val2;
1731 int op;
1733 val = expr_prod();
1734 for(;;) {
1735 op = *pch;
1736 if (op != '&' && op != '|' && op != '^')
1737 break;
1738 next();
1739 val2 = expr_prod();
1740 switch(op) {
1741 default:
1742 case '&':
1743 val &= val2;
1744 break;
1745 case '|':
1746 val |= val2;
1747 break;
1748 case '^':
1749 val ^= val2;
1750 break;
1753 return val;
1756 static target_long expr_sum(void)
1758 target_long val, val2;
1759 int op;
1761 val = expr_logic();
1762 for(;;) {
1763 op = *pch;
1764 if (op != '+' && op != '-')
1765 break;
1766 next();
1767 val2 = expr_logic();
1768 if (op == '+')
1769 val += val2;
1770 else
1771 val -= val2;
1773 return val;
1776 static int get_expr(target_long *pval, const char **pp)
1778 pch = *pp;
1779 if (setjmp(expr_env)) {
1780 *pp = pch;
1781 return -1;
1783 while (isspace(*pch))
1784 pch++;
1785 *pval = expr_sum();
1786 *pp = pch;
1787 return 0;
1790 static int get_str(char *buf, int buf_size, const char **pp)
1792 const char *p;
1793 char *q;
1794 int c;
1796 q = buf;
1797 p = *pp;
1798 while (isspace(*p))
1799 p++;
1800 if (*p == '\0') {
1801 fail:
1802 *q = '\0';
1803 *pp = p;
1804 return -1;
1806 if (*p == '\"') {
1807 p++;
1808 while (*p != '\0' && *p != '\"') {
1809 if (*p == '\\') {
1810 p++;
1811 c = *p++;
1812 switch(c) {
1813 case 'n':
1814 c = '\n';
1815 break;
1816 case 'r':
1817 c = '\r';
1818 break;
1819 case '\\':
1820 case '\'':
1821 case '\"':
1822 break;
1823 default:
1824 qemu_printf("unsupported escape code: '\\%c'\n", c);
1825 goto fail;
1827 if ((q - buf) < buf_size - 1) {
1828 *q++ = c;
1830 } else {
1831 if ((q - buf) < buf_size - 1) {
1832 *q++ = *p;
1834 p++;
1837 if (*p != '\"') {
1838 qemu_printf("unterminated string\n");
1839 goto fail;
1841 p++;
1842 } else {
1843 while (*p != '\0' && !isspace(*p)) {
1844 if ((q - buf) < buf_size - 1) {
1845 *q++ = *p;
1847 p++;
1850 *q = '\0';
1851 *pp = p;
1852 return 0;
1855 static int default_fmt_format = 'x';
1856 static int default_fmt_size = 4;
1858 #define MAX_ARGS 16
1860 static void monitor_handle_command(const char *cmdline)
1862 const char *p, *pstart, *typestr;
1863 char *q;
1864 int c, nb_args, len, i, has_arg;
1865 term_cmd_t *cmd;
1866 char cmdname[256];
1867 char buf[1024];
1868 void *str_allocated[MAX_ARGS];
1869 void *args[MAX_ARGS];
1871 #ifdef DEBUG
1872 term_printf("command='%s'\n", cmdline);
1873 #endif
1875 /* extract the command name */
1876 p = cmdline;
1877 q = cmdname;
1878 while (isspace(*p))
1879 p++;
1880 if (*p == '\0')
1881 return;
1882 pstart = p;
1883 while (*p != '\0' && *p != '/' && !isspace(*p))
1884 p++;
1885 len = p - pstart;
1886 if (len > sizeof(cmdname) - 1)
1887 len = sizeof(cmdname) - 1;
1888 memcpy(cmdname, pstart, len);
1889 cmdname[len] = '\0';
1891 /* find the command */
1892 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1893 if (compare_cmd(cmdname, cmd->name))
1894 goto found;
1896 term_printf("unknown command: '%s'\n", cmdname);
1897 return;
1898 found:
1900 for(i = 0; i < MAX_ARGS; i++)
1901 str_allocated[i] = NULL;
1903 /* parse the parameters */
1904 typestr = cmd->args_type;
1905 nb_args = 0;
1906 for(;;) {
1907 c = *typestr;
1908 if (c == '\0')
1909 break;
1910 typestr++;
1911 switch(c) {
1912 case 'F':
1913 case 'B':
1914 case 's':
1916 int ret;
1917 char *str;
1919 while (isspace(*p))
1920 p++;
1921 if (*typestr == '?') {
1922 typestr++;
1923 if (*p == '\0') {
1924 /* no optional string: NULL argument */
1925 str = NULL;
1926 goto add_str;
1929 ret = get_str(buf, sizeof(buf), &p);
1930 if (ret < 0) {
1931 switch(c) {
1932 case 'F':
1933 term_printf("%s: filename expected\n", cmdname);
1934 break;
1935 case 'B':
1936 term_printf("%s: block device name expected\n", cmdname);
1937 break;
1938 default:
1939 term_printf("%s: string expected\n", cmdname);
1940 break;
1942 goto fail;
1944 str = qemu_malloc(strlen(buf) + 1);
1945 strcpy(str, buf);
1946 str_allocated[nb_args] = str;
1947 add_str:
1948 if (nb_args >= MAX_ARGS) {
1949 error_args:
1950 term_printf("%s: too many arguments\n", cmdname);
1951 goto fail;
1953 args[nb_args++] = str;
1955 break;
1956 case '/':
1958 int count, format, size;
1960 while (isspace(*p))
1961 p++;
1962 if (*p == '/') {
1963 /* format found */
1964 p++;
1965 count = 1;
1966 if (isdigit(*p)) {
1967 count = 0;
1968 while (isdigit(*p)) {
1969 count = count * 10 + (*p - '0');
1970 p++;
1973 size = -1;
1974 format = -1;
1975 for(;;) {
1976 switch(*p) {
1977 case 'o':
1978 case 'd':
1979 case 'u':
1980 case 'x':
1981 case 'i':
1982 case 'c':
1983 format = *p++;
1984 break;
1985 case 'b':
1986 size = 1;
1987 p++;
1988 break;
1989 case 'h':
1990 size = 2;
1991 p++;
1992 break;
1993 case 'w':
1994 size = 4;
1995 p++;
1996 break;
1997 case 'g':
1998 case 'L':
1999 size = 8;
2000 p++;
2001 break;
2002 default:
2003 goto next;
2006 next:
2007 if (*p != '\0' && !isspace(*p)) {
2008 term_printf("invalid char in format: '%c'\n", *p);
2009 goto fail;
2011 if (format < 0)
2012 format = default_fmt_format;
2013 if (format != 'i') {
2014 /* for 'i', not specifying a size gives -1 as size */
2015 if (size < 0)
2016 size = default_fmt_size;
2018 default_fmt_size = size;
2019 default_fmt_format = format;
2020 } else {
2021 count = 1;
2022 format = default_fmt_format;
2023 if (format != 'i') {
2024 size = default_fmt_size;
2025 } else {
2026 size = -1;
2029 if (nb_args + 3 > MAX_ARGS)
2030 goto error_args;
2031 args[nb_args++] = (void*)count;
2032 args[nb_args++] = (void*)format;
2033 args[nb_args++] = (void*)size;
2035 break;
2036 case 'i':
2037 case 'l':
2039 target_long val;
2040 while (isspace(*p))
2041 p++;
2042 if (*typestr == '?' || *typestr == '.') {
2043 if (*typestr == '?') {
2044 if (*p == '\0')
2045 has_arg = 0;
2046 else
2047 has_arg = 1;
2048 } else {
2049 if (*p == '.') {
2050 p++;
2051 while (isspace(*p))
2052 p++;
2053 has_arg = 1;
2054 } else {
2055 has_arg = 0;
2058 typestr++;
2059 if (nb_args >= MAX_ARGS)
2060 goto error_args;
2061 args[nb_args++] = (void *)has_arg;
2062 if (!has_arg) {
2063 if (nb_args >= MAX_ARGS)
2064 goto error_args;
2065 val = -1;
2066 goto add_num;
2069 if (get_expr(&val, &p))
2070 goto fail;
2071 add_num:
2072 if (c == 'i') {
2073 if (nb_args >= MAX_ARGS)
2074 goto error_args;
2075 args[nb_args++] = (void *)(int)val;
2076 } else {
2077 if ((nb_args + 1) >= MAX_ARGS)
2078 goto error_args;
2079 #if TARGET_LONG_BITS == 64
2080 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2081 #else
2082 args[nb_args++] = (void *)0;
2083 #endif
2084 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2087 break;
2088 case '-':
2090 int has_option;
2091 /* option */
2093 c = *typestr++;
2094 if (c == '\0')
2095 goto bad_type;
2096 while (isspace(*p))
2097 p++;
2098 has_option = 0;
2099 if (*p == '-') {
2100 p++;
2101 if (*p != c) {
2102 term_printf("%s: unsupported option -%c\n",
2103 cmdname, *p);
2104 goto fail;
2106 p++;
2107 has_option = 1;
2109 if (nb_args >= MAX_ARGS)
2110 goto error_args;
2111 args[nb_args++] = (void *)has_option;
2113 break;
2114 default:
2115 bad_type:
2116 term_printf("%s: unknown type '%c'\n", cmdname, c);
2117 goto fail;
2120 /* check that all arguments were parsed */
2121 while (isspace(*p))
2122 p++;
2123 if (*p != '\0') {
2124 term_printf("%s: extraneous characters at the end of line\n",
2125 cmdname);
2126 goto fail;
2129 switch(nb_args) {
2130 case 0:
2131 cmd->handler();
2132 break;
2133 case 1:
2134 cmd->handler(args[0]);
2135 break;
2136 case 2:
2137 cmd->handler(args[0], args[1]);
2138 break;
2139 case 3:
2140 cmd->handler(args[0], args[1], args[2]);
2141 break;
2142 case 4:
2143 cmd->handler(args[0], args[1], args[2], args[3]);
2144 break;
2145 case 5:
2146 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2147 break;
2148 case 6:
2149 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2150 break;
2151 case 7:
2152 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2153 break;
2154 default:
2155 term_printf("unsupported number of arguments: %d\n", nb_args);
2156 goto fail;
2158 fail:
2159 for(i = 0; i < MAX_ARGS; i++)
2160 qemu_free(str_allocated[i]);
2161 return;
2164 static void cmd_completion(const char *name, const char *list)
2166 const char *p, *pstart;
2167 char cmd[128];
2168 int len;
2170 p = list;
2171 for(;;) {
2172 pstart = p;
2173 p = strchr(p, '|');
2174 if (!p)
2175 p = pstart + strlen(pstart);
2176 len = p - pstart;
2177 if (len > sizeof(cmd) - 2)
2178 len = sizeof(cmd) - 2;
2179 memcpy(cmd, pstart, len);
2180 cmd[len] = '\0';
2181 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2182 add_completion(cmd);
2184 if (*p == '\0')
2185 break;
2186 p++;
2190 static void file_completion(const char *input)
2192 DIR *ffs;
2193 struct dirent *d;
2194 char path[1024];
2195 char file[1024], file_prefix[1024];
2196 int input_path_len;
2197 const char *p;
2199 p = strrchr(input, '/');
2200 if (!p) {
2201 input_path_len = 0;
2202 pstrcpy(file_prefix, sizeof(file_prefix), input);
2203 strcpy(path, ".");
2204 } else {
2205 input_path_len = p - input + 1;
2206 memcpy(path, input, input_path_len);
2207 if (input_path_len > sizeof(path) - 1)
2208 input_path_len = sizeof(path) - 1;
2209 path[input_path_len] = '\0';
2210 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2212 #ifdef DEBUG_COMPLETION
2213 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2214 #endif
2215 ffs = opendir(path);
2216 if (!ffs)
2217 return;
2218 for(;;) {
2219 struct stat sb;
2220 d = readdir(ffs);
2221 if (!d)
2222 break;
2223 if (strstart(d->d_name, file_prefix, NULL)) {
2224 memcpy(file, input, input_path_len);
2225 strcpy(file + input_path_len, d->d_name);
2226 /* stat the file to find out if it's a directory.
2227 * In that case add a slash to speed up typing long paths
2229 stat(file, &sb);
2230 if(S_ISDIR(sb.st_mode))
2231 strcat(file, "/");
2232 add_completion(file);
2235 closedir(ffs);
2238 static void block_completion_it(void *opaque, const char *name)
2240 const char *input = opaque;
2242 if (input[0] == '\0' ||
2243 !strncmp(name, (char *)input, strlen(input))) {
2244 add_completion(name);
2248 /* NOTE: this parser is an approximate form of the real command parser */
2249 static void parse_cmdline(const char *cmdline,
2250 int *pnb_args, char **args)
2252 const char *p;
2253 int nb_args, ret;
2254 char buf[1024];
2256 p = cmdline;
2257 nb_args = 0;
2258 for(;;) {
2259 while (isspace(*p))
2260 p++;
2261 if (*p == '\0')
2262 break;
2263 if (nb_args >= MAX_ARGS)
2264 break;
2265 ret = get_str(buf, sizeof(buf), &p);
2266 args[nb_args] = qemu_strdup(buf);
2267 nb_args++;
2268 if (ret < 0)
2269 break;
2271 *pnb_args = nb_args;
2274 void readline_find_completion(const char *cmdline)
2276 const char *cmdname;
2277 char *args[MAX_ARGS];
2278 int nb_args, i, len;
2279 const char *ptype, *str;
2280 term_cmd_t *cmd;
2281 const KeyDef *key;
2283 parse_cmdline(cmdline, &nb_args, args);
2284 #ifdef DEBUG_COMPLETION
2285 for(i = 0; i < nb_args; i++) {
2286 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2288 #endif
2290 /* if the line ends with a space, it means we want to complete the
2291 next arg */
2292 len = strlen(cmdline);
2293 if (len > 0 && isspace(cmdline[len - 1])) {
2294 if (nb_args >= MAX_ARGS)
2295 return;
2296 args[nb_args++] = qemu_strdup("");
2298 if (nb_args <= 1) {
2299 /* command completion */
2300 if (nb_args == 0)
2301 cmdname = "";
2302 else
2303 cmdname = args[0];
2304 completion_index = strlen(cmdname);
2305 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2306 cmd_completion(cmdname, cmd->name);
2308 } else {
2309 /* find the command */
2310 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2311 if (compare_cmd(args[0], cmd->name))
2312 goto found;
2314 return;
2315 found:
2316 ptype = cmd->args_type;
2317 for(i = 0; i < nb_args - 2; i++) {
2318 if (*ptype != '\0') {
2319 ptype++;
2320 while (*ptype == '?')
2321 ptype++;
2324 str = args[nb_args - 1];
2325 switch(*ptype) {
2326 case 'F':
2327 /* file completion */
2328 completion_index = strlen(str);
2329 file_completion(str);
2330 break;
2331 case 'B':
2332 /* block device name completion */
2333 completion_index = strlen(str);
2334 bdrv_iterate(block_completion_it, (void *)str);
2335 break;
2336 case 's':
2337 /* XXX: more generic ? */
2338 if (!strcmp(cmd->name, "info")) {
2339 completion_index = strlen(str);
2340 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2341 cmd_completion(str, cmd->name);
2343 } else if (!strcmp(cmd->name, "sendkey")) {
2344 completion_index = strlen(str);
2345 for(key = key_defs; key->name != NULL; key++) {
2346 cmd_completion(str, key->name);
2349 break;
2350 default:
2351 break;
2354 for(i = 0; i < nb_args; i++)
2355 qemu_free(args[i]);
2358 static int term_can_read(void *opaque)
2360 return 128;
2363 static void term_read(void *opaque, const uint8_t *buf, int size)
2365 int i;
2366 for(i = 0; i < size; i++)
2367 readline_handle_byte(buf[i]);
2370 static void monitor_start_input(void);
2372 static void monitor_handle_command1(void *opaque, const char *cmdline)
2374 monitor_handle_command(cmdline);
2375 monitor_start_input();
2378 static void monitor_start_input(void)
2380 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2383 void monitor_init(CharDriverState *hd, int show_banner)
2385 monitor_hd = hd;
2386 if (show_banner) {
2387 term_printf("QEMU %s monitor - type 'help' for more information\n",
2388 QEMU_VERSION);
2390 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2391 monitor_start_input();
2394 /* XXX: use threads ? */
2395 /* modal monitor readline */
2396 static int monitor_readline_started;
2397 static char *monitor_readline_buf;
2398 static int monitor_readline_buf_size;
2400 static void monitor_readline_cb(void *opaque, const char *input)
2402 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2403 monitor_readline_started = 0;
2406 void monitor_readline(const char *prompt, int is_password,
2407 char *buf, int buf_size)
2409 if (is_password) {
2410 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2412 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2413 monitor_readline_buf = buf;
2414 monitor_readline_buf_size = buf_size;
2415 monitor_readline_started = 1;
2416 while (monitor_readline_started) {
2417 main_loop_wait(10);