Solaris SMBD hacks (Ben Taylor).
[qemu/mini2440.git] / monitor.c
blobd553ce60770f07a555c270560490fabaef0b8d50
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 void term_print_filename(const char *filename)
111 int i;
113 for (i = 0; filename[i]; i++) {
114 switch (filename[i]) {
115 case ' ':
116 case '"':
117 case '\\':
118 term_printf("\\%c", filename[i]);
119 break;
120 case '\t':
121 term_printf("\\t");
122 break;
123 case '\r':
124 term_printf("\\r");
125 break;
126 case '\n':
127 term_printf("\\n");
128 break;
129 default:
130 term_printf("%c", filename[i]);
131 break;
136 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
138 va_list ap;
139 va_start(ap, fmt);
140 term_vprintf(fmt, ap);
141 va_end(ap);
142 return 0;
145 static int compare_cmd(const char *name, const char *list)
147 const char *p, *pstart;
148 int len;
149 len = strlen(name);
150 p = list;
151 for(;;) {
152 pstart = p;
153 p = strchr(p, '|');
154 if (!p)
155 p = pstart + strlen(pstart);
156 if ((p - pstart) == len && !memcmp(pstart, name, len))
157 return 1;
158 if (*p == '\0')
159 break;
160 p++;
162 return 0;
165 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
167 term_cmd_t *cmd;
169 for(cmd = cmds; cmd->name != NULL; cmd++) {
170 if (!name || !strcmp(name, cmd->name))
171 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
175 static void help_cmd(const char *name)
177 if (name && !strcmp(name, "info")) {
178 help_cmd1(info_cmds, "info ", NULL);
179 } else {
180 help_cmd1(term_cmds, "", name);
181 if (name && !strcmp(name, "log")) {
182 CPULogItem *item;
183 term_printf("Log items (comma separated):\n");
184 term_printf("%-10s %s\n", "none", "remove all logs");
185 for(item = cpu_log_items; item->mask != 0; item++) {
186 term_printf("%-10s %s\n", item->name, item->help);
192 static void do_help(const char *name)
194 help_cmd(name);
197 static void do_commit(const char *device)
199 int i, all_devices;
201 all_devices = !strcmp(device, "all");
202 for (i = 0; i < MAX_DISKS; i++) {
203 if (bs_table[i]) {
204 if (all_devices ||
205 !strcmp(bdrv_get_device_name(bs_table[i]), device))
206 bdrv_commit(bs_table[i]);
211 static void do_info(const char *item)
213 term_cmd_t *cmd;
215 if (!item)
216 goto help;
217 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
218 if (compare_cmd(item, cmd->name))
219 goto found;
221 help:
222 help_cmd("info");
223 return;
224 found:
225 cmd->handler();
228 static void do_info_version(void)
230 term_printf("%s\n", QEMU_VERSION);
233 static void do_info_block(void)
235 bdrv_info();
238 /* get the current CPU defined by the user */
239 int mon_set_cpu(int cpu_index)
241 CPUState *env;
243 for(env = first_cpu; env != NULL; env = env->next_cpu) {
244 if (env->cpu_index == cpu_index) {
245 mon_cpu = env;
246 return 0;
249 return -1;
252 CPUState *mon_get_cpu(void)
254 if (!mon_cpu) {
255 mon_set_cpu(0);
257 return mon_cpu;
260 static void do_info_registers(void)
262 CPUState *env;
263 env = mon_get_cpu();
264 if (!env)
265 return;
266 #ifdef TARGET_I386
267 cpu_dump_state(env, NULL, monitor_fprintf,
268 X86_DUMP_FPU);
269 #else
270 cpu_dump_state(env, NULL, monitor_fprintf,
272 #endif
275 static void do_info_cpus(void)
277 CPUState *env;
279 /* just to set the default cpu if not already done */
280 mon_get_cpu();
282 for(env = first_cpu; env != NULL; env = env->next_cpu) {
283 term_printf("%c CPU #%d:",
284 (env == mon_cpu) ? '*' : ' ',
285 env->cpu_index);
286 #if defined(TARGET_I386)
287 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
288 if (env->hflags & HF_HALTED_MASK)
289 term_printf(" (halted)");
290 #elif defined(TARGET_PPC)
291 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
292 if (env->halted)
293 term_printf(" (halted)");
294 #elif defined(TARGET_SPARC)
295 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
296 if (env->halted)
297 term_printf(" (halted)");
298 #endif
299 term_printf("\n");
303 static void do_cpu_set(int index)
305 if (mon_set_cpu(index) < 0)
306 term_printf("Invalid CPU index\n");
309 static void do_info_jit(void)
311 dump_exec_info(NULL, monitor_fprintf);
314 static void do_info_history (void)
316 int i;
317 const char *str;
319 i = 0;
320 for(;;) {
321 str = readline_get_history(i);
322 if (!str)
323 break;
324 term_printf("%d: '%s'\n", i, str);
325 i++;
329 static void do_quit(void)
331 exit(0);
334 static int eject_device(BlockDriverState *bs, int force)
336 if (bdrv_is_inserted(bs)) {
337 if (!force) {
338 if (!bdrv_is_removable(bs)) {
339 term_printf("device is not removable\n");
340 return -1;
342 if (bdrv_is_locked(bs)) {
343 term_printf("device is locked\n");
344 return -1;
347 bdrv_close(bs);
349 return 0;
352 static void do_eject(int force, const char *filename)
354 BlockDriverState *bs;
356 bs = bdrv_find(filename);
357 if (!bs) {
358 term_printf("device not found\n");
359 return;
361 eject_device(bs, force);
364 static void do_change(const char *device, const char *filename)
366 BlockDriverState *bs;
367 int i;
368 char password[256];
370 bs = bdrv_find(device);
371 if (!bs) {
372 term_printf("device not found\n");
373 return;
375 if (eject_device(bs, 0) < 0)
376 return;
377 bdrv_open(bs, filename, 0);
378 if (bdrv_is_encrypted(bs)) {
379 term_printf("%s is encrypted.\n", device);
380 for(i = 0; i < 3; i++) {
381 monitor_readline("Password: ", 1, password, sizeof(password));
382 if (bdrv_set_key(bs, password) == 0)
383 break;
384 term_printf("invalid password\n");
389 static void do_screen_dump(const char *filename)
391 vga_hw_screen_dump(filename);
394 static void do_log(const char *items)
396 int mask;
398 if (!strcmp(items, "none")) {
399 mask = 0;
400 } else {
401 mask = cpu_str_to_log_mask(items);
402 if (!mask) {
403 help_cmd("log");
404 return;
407 cpu_set_log(mask);
410 static void do_stop(void)
412 vm_stop(EXCP_INTERRUPT);
415 static void do_cont(void)
417 vm_start();
420 #ifdef CONFIG_GDBSTUB
421 static void do_gdbserver(int has_port, int port)
423 if (!has_port)
424 port = DEFAULT_GDBSTUB_PORT;
425 if (gdbserver_start(port) < 0) {
426 qemu_printf("Could not open gdbserver socket on port %d\n", port);
427 } else {
428 qemu_printf("Waiting gdb connection on port %d\n", port);
431 #endif
433 static void term_printc(int c)
435 term_printf("'");
436 switch(c) {
437 case '\'':
438 term_printf("\\'");
439 break;
440 case '\\':
441 term_printf("\\\\");
442 break;
443 case '\n':
444 term_printf("\\n");
445 break;
446 case '\r':
447 term_printf("\\r");
448 break;
449 default:
450 if (c >= 32 && c <= 126) {
451 term_printf("%c", c);
452 } else {
453 term_printf("\\x%02x", c);
455 break;
457 term_printf("'");
460 static void memory_dump(int count, int format, int wsize,
461 target_ulong addr, int is_physical)
463 CPUState *env;
464 int nb_per_line, l, line_size, i, max_digits, len;
465 uint8_t buf[16];
466 uint64_t v;
468 if (format == 'i') {
469 int flags;
470 flags = 0;
471 env = mon_get_cpu();
472 if (!env && !is_physical)
473 return;
474 #ifdef TARGET_I386
475 if (wsize == 2) {
476 flags = 1;
477 } else if (wsize == 4) {
478 flags = 0;
479 } else {
480 /* as default we use the current CS size */
481 flags = 0;
482 if (env) {
483 #ifdef TARGET_X86_64
484 if ((env->efer & MSR_EFER_LMA) &&
485 (env->segs[R_CS].flags & DESC_L_MASK))
486 flags = 2;
487 else
488 #endif
489 if (!(env->segs[R_CS].flags & DESC_B_MASK))
490 flags = 1;
493 #endif
494 monitor_disas(env, addr, count, is_physical, flags);
495 return;
498 len = wsize * count;
499 if (wsize == 1)
500 line_size = 8;
501 else
502 line_size = 16;
503 nb_per_line = line_size / wsize;
504 max_digits = 0;
506 switch(format) {
507 case 'o':
508 max_digits = (wsize * 8 + 2) / 3;
509 break;
510 default:
511 case 'x':
512 max_digits = (wsize * 8) / 4;
513 break;
514 case 'u':
515 case 'd':
516 max_digits = (wsize * 8 * 10 + 32) / 33;
517 break;
518 case 'c':
519 wsize = 1;
520 break;
523 while (len > 0) {
524 term_printf(TARGET_FMT_lx ":", addr);
525 l = len;
526 if (l > line_size)
527 l = line_size;
528 if (is_physical) {
529 cpu_physical_memory_rw(addr, buf, l, 0);
530 } else {
531 env = mon_get_cpu();
532 if (!env)
533 break;
534 cpu_memory_rw_debug(env, addr, buf, l, 0);
536 i = 0;
537 while (i < l) {
538 switch(wsize) {
539 default:
540 case 1:
541 v = ldub_raw(buf + i);
542 break;
543 case 2:
544 v = lduw_raw(buf + i);
545 break;
546 case 4:
547 v = (uint32_t)ldl_raw(buf + i);
548 break;
549 case 8:
550 v = ldq_raw(buf + i);
551 break;
553 term_printf(" ");
554 switch(format) {
555 case 'o':
556 term_printf("%#*" PRIo64, max_digits, v);
557 break;
558 case 'x':
559 term_printf("0x%0*" PRIx64, max_digits, v);
560 break;
561 case 'u':
562 term_printf("%*" PRIu64, max_digits, v);
563 break;
564 case 'd':
565 term_printf("%*" PRId64, max_digits, v);
566 break;
567 case 'c':
568 term_printc(v);
569 break;
571 i += wsize;
573 term_printf("\n");
574 addr += l;
575 len -= l;
579 #if TARGET_LONG_BITS == 64
580 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
581 #else
582 #define GET_TLONG(h, l) (l)
583 #endif
585 static void do_memory_dump(int count, int format, int size,
586 uint32_t addrh, uint32_t addrl)
588 target_long addr = GET_TLONG(addrh, addrl);
589 memory_dump(count, format, size, addr, 0);
592 static void do_physical_memory_dump(int count, int format, int size,
593 uint32_t addrh, uint32_t addrl)
596 target_long addr = GET_TLONG(addrh, addrl);
597 memory_dump(count, format, size, addr, 1);
600 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
602 target_long val = GET_TLONG(valh, vall);
603 #if TARGET_LONG_BITS == 32
604 switch(format) {
605 case 'o':
606 term_printf("%#o", val);
607 break;
608 case 'x':
609 term_printf("%#x", val);
610 break;
611 case 'u':
612 term_printf("%u", val);
613 break;
614 default:
615 case 'd':
616 term_printf("%d", val);
617 break;
618 case 'c':
619 term_printc(val);
620 break;
622 #else
623 switch(format) {
624 case 'o':
625 term_printf("%#" PRIo64, val);
626 break;
627 case 'x':
628 term_printf("%#" PRIx64, val);
629 break;
630 case 'u':
631 term_printf("%" PRIu64, val);
632 break;
633 default:
634 case 'd':
635 term_printf("%" PRId64, val);
636 break;
637 case 'c':
638 term_printc(val);
639 break;
641 #endif
642 term_printf("\n");
645 static void do_sum(uint32_t start, uint32_t size)
647 uint32_t addr;
648 uint8_t buf[1];
649 uint16_t sum;
651 sum = 0;
652 for(addr = start; addr < (start + size); addr++) {
653 cpu_physical_memory_rw(addr, buf, 1, 0);
654 /* BSD sum algorithm ('sum' Unix command) */
655 sum = (sum >> 1) | (sum << 15);
656 sum += buf[0];
658 term_printf("%05d\n", sum);
661 typedef struct {
662 int keycode;
663 const char *name;
664 } KeyDef;
666 static const KeyDef key_defs[] = {
667 { 0x2a, "shift" },
668 { 0x36, "shift_r" },
670 { 0x38, "alt" },
671 { 0xb8, "alt_r" },
672 { 0x1d, "ctrl" },
673 { 0x9d, "ctrl_r" },
675 { 0xdd, "menu" },
677 { 0x01, "esc" },
679 { 0x02, "1" },
680 { 0x03, "2" },
681 { 0x04, "3" },
682 { 0x05, "4" },
683 { 0x06, "5" },
684 { 0x07, "6" },
685 { 0x08, "7" },
686 { 0x09, "8" },
687 { 0x0a, "9" },
688 { 0x0b, "0" },
689 { 0x0c, "minus" },
690 { 0x0d, "equal" },
691 { 0x0e, "backspace" },
693 { 0x0f, "tab" },
694 { 0x10, "q" },
695 { 0x11, "w" },
696 { 0x12, "e" },
697 { 0x13, "r" },
698 { 0x14, "t" },
699 { 0x15, "y" },
700 { 0x16, "u" },
701 { 0x17, "i" },
702 { 0x18, "o" },
703 { 0x19, "p" },
705 { 0x1c, "ret" },
707 { 0x1e, "a" },
708 { 0x1f, "s" },
709 { 0x20, "d" },
710 { 0x21, "f" },
711 { 0x22, "g" },
712 { 0x23, "h" },
713 { 0x24, "j" },
714 { 0x25, "k" },
715 { 0x26, "l" },
717 { 0x2c, "z" },
718 { 0x2d, "x" },
719 { 0x2e, "c" },
720 { 0x2f, "v" },
721 { 0x30, "b" },
722 { 0x31, "n" },
723 { 0x32, "m" },
725 { 0x39, "spc" },
726 { 0x3a, "caps_lock" },
727 { 0x3b, "f1" },
728 { 0x3c, "f2" },
729 { 0x3d, "f3" },
730 { 0x3e, "f4" },
731 { 0x3f, "f5" },
732 { 0x40, "f6" },
733 { 0x41, "f7" },
734 { 0x42, "f8" },
735 { 0x43, "f9" },
736 { 0x44, "f10" },
737 { 0x45, "num_lock" },
738 { 0x46, "scroll_lock" },
740 { 0xb5, "kp_divide" },
741 { 0x37, "kp_multiply" },
742 { 0x4a, "kp_substract" },
743 { 0x4e, "kp_add" },
744 { 0x9c, "kp_enter" },
745 { 0x53, "kp_decimal" },
747 { 0x52, "kp_0" },
748 { 0x4f, "kp_1" },
749 { 0x50, "kp_2" },
750 { 0x51, "kp_3" },
751 { 0x4b, "kp_4" },
752 { 0x4c, "kp_5" },
753 { 0x4d, "kp_6" },
754 { 0x47, "kp_7" },
755 { 0x48, "kp_8" },
756 { 0x49, "kp_9" },
758 { 0x56, "<" },
760 { 0x57, "f11" },
761 { 0x58, "f12" },
763 { 0xb7, "print" },
765 { 0xc7, "home" },
766 { 0xc9, "pgup" },
767 { 0xd1, "pgdn" },
768 { 0xcf, "end" },
770 { 0xcb, "left" },
771 { 0xc8, "up" },
772 { 0xd0, "down" },
773 { 0xcd, "right" },
775 { 0xd2, "insert" },
776 { 0xd3, "delete" },
777 { 0, NULL },
780 static int get_keycode(const char *key)
782 const KeyDef *p;
783 char *endp;
784 int ret;
786 for(p = key_defs; p->name != NULL; p++) {
787 if (!strcmp(key, p->name))
788 return p->keycode;
790 if (strstart(key, "0x", NULL)) {
791 ret = strtoul(key, &endp, 0);
792 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
793 return ret;
795 return -1;
798 static void do_send_key(const char *string)
800 char keybuf[16], *q;
801 uint8_t keycodes[16];
802 const char *p;
803 int nb_keycodes, keycode, i;
805 nb_keycodes = 0;
806 p = string;
807 while (*p != '\0') {
808 q = keybuf;
809 while (*p != '\0' && *p != '-') {
810 if ((q - keybuf) < sizeof(keybuf) - 1) {
811 *q++ = *p;
813 p++;
815 *q = '\0';
816 keycode = get_keycode(keybuf);
817 if (keycode < 0) {
818 term_printf("unknown key: '%s'\n", keybuf);
819 return;
821 keycodes[nb_keycodes++] = keycode;
822 if (*p == '\0')
823 break;
824 p++;
826 /* key down events */
827 for(i = 0; i < nb_keycodes; i++) {
828 keycode = keycodes[i];
829 if (keycode & 0x80)
830 kbd_put_keycode(0xe0);
831 kbd_put_keycode(keycode & 0x7f);
833 /* key up events */
834 for(i = nb_keycodes - 1; i >= 0; i--) {
835 keycode = keycodes[i];
836 if (keycode & 0x80)
837 kbd_put_keycode(0xe0);
838 kbd_put_keycode(keycode | 0x80);
842 static int mouse_button_state;
844 static void do_mouse_move(const char *dx_str, const char *dy_str,
845 const char *dz_str)
847 int dx, dy, dz;
848 dx = strtol(dx_str, NULL, 0);
849 dy = strtol(dy_str, NULL, 0);
850 dz = 0;
851 if (dz_str)
852 dz = strtol(dz_str, NULL, 0);
853 kbd_mouse_event(dx, dy, dz, mouse_button_state);
856 static void do_mouse_button(int button_state)
858 mouse_button_state = button_state;
859 kbd_mouse_event(0, 0, 0, mouse_button_state);
862 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
864 uint32_t val;
865 int suffix;
867 if (has_index) {
868 cpu_outb(NULL, addr & 0xffff, index & 0xff);
869 addr++;
871 addr &= 0xffff;
873 switch(size) {
874 default:
875 case 1:
876 val = cpu_inb(NULL, addr);
877 suffix = 'b';
878 break;
879 case 2:
880 val = cpu_inw(NULL, addr);
881 suffix = 'w';
882 break;
883 case 4:
884 val = cpu_inl(NULL, addr);
885 suffix = 'l';
886 break;
888 term_printf("port%c[0x%04x] = %#0*x\n",
889 suffix, addr, size * 2, val);
892 static void do_system_reset(void)
894 qemu_system_reset_request();
897 static void do_system_powerdown(void)
899 qemu_system_powerdown_request();
902 #if defined(TARGET_I386)
903 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
905 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
906 addr,
907 pte & mask,
908 pte & PG_GLOBAL_MASK ? 'G' : '-',
909 pte & PG_PSE_MASK ? 'P' : '-',
910 pte & PG_DIRTY_MASK ? 'D' : '-',
911 pte & PG_ACCESSED_MASK ? 'A' : '-',
912 pte & PG_PCD_MASK ? 'C' : '-',
913 pte & PG_PWT_MASK ? 'T' : '-',
914 pte & PG_USER_MASK ? 'U' : '-',
915 pte & PG_RW_MASK ? 'W' : '-');
918 static void tlb_info(void)
920 CPUState *env;
921 int l1, l2;
922 uint32_t pgd, pde, pte;
924 env = mon_get_cpu();
925 if (!env)
926 return;
928 if (!(env->cr[0] & CR0_PG_MASK)) {
929 term_printf("PG disabled\n");
930 return;
932 pgd = env->cr[3] & ~0xfff;
933 for(l1 = 0; l1 < 1024; l1++) {
934 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
935 pde = le32_to_cpu(pde);
936 if (pde & PG_PRESENT_MASK) {
937 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
938 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
939 } else {
940 for(l2 = 0; l2 < 1024; l2++) {
941 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
942 (uint8_t *)&pte, 4);
943 pte = le32_to_cpu(pte);
944 if (pte & PG_PRESENT_MASK) {
945 print_pte((l1 << 22) + (l2 << 12),
946 pte & ~PG_PSE_MASK,
947 ~0xfff);
955 static void mem_print(uint32_t *pstart, int *plast_prot,
956 uint32_t end, int prot)
958 int prot1;
959 prot1 = *plast_prot;
960 if (prot != prot1) {
961 if (*pstart != -1) {
962 term_printf("%08x-%08x %08x %c%c%c\n",
963 *pstart, end, end - *pstart,
964 prot1 & PG_USER_MASK ? 'u' : '-',
965 'r',
966 prot1 & PG_RW_MASK ? 'w' : '-');
968 if (prot != 0)
969 *pstart = end;
970 else
971 *pstart = -1;
972 *plast_prot = prot;
976 static void mem_info(void)
978 CPUState *env;
979 int l1, l2, prot, last_prot;
980 uint32_t pgd, pde, pte, start, end;
982 env = mon_get_cpu();
983 if (!env)
984 return;
986 if (!(env->cr[0] & CR0_PG_MASK)) {
987 term_printf("PG disabled\n");
988 return;
990 pgd = env->cr[3] & ~0xfff;
991 last_prot = 0;
992 start = -1;
993 for(l1 = 0; l1 < 1024; l1++) {
994 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
995 pde = le32_to_cpu(pde);
996 end = l1 << 22;
997 if (pde & PG_PRESENT_MASK) {
998 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
999 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1000 mem_print(&start, &last_prot, end, prot);
1001 } else {
1002 for(l2 = 0; l2 < 1024; l2++) {
1003 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1004 (uint8_t *)&pte, 4);
1005 pte = le32_to_cpu(pte);
1006 end = (l1 << 22) + (l2 << 12);
1007 if (pte & PG_PRESENT_MASK) {
1008 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1009 } else {
1010 prot = 0;
1012 mem_print(&start, &last_prot, end, prot);
1015 } else {
1016 prot = 0;
1017 mem_print(&start, &last_prot, end, prot);
1021 #endif
1023 static void do_info_kqemu(void)
1025 #ifdef USE_KQEMU
1026 CPUState *env;
1027 int val;
1028 val = 0;
1029 env = mon_get_cpu();
1030 if (!env) {
1031 term_printf("No cpu initialized yet");
1032 return;
1034 val = env->kqemu_enabled;
1035 term_printf("kqemu support: ");
1036 switch(val) {
1037 default:
1038 case 0:
1039 term_printf("disabled\n");
1040 break;
1041 case 1:
1042 term_printf("enabled for user code\n");
1043 break;
1044 case 2:
1045 term_printf("enabled for user and kernel code\n");
1046 break;
1048 #else
1049 term_printf("kqemu support: not compiled\n");
1050 #endif
1053 #ifdef CONFIG_PROFILER
1055 int64_t kqemu_time;
1056 int64_t qemu_time;
1057 int64_t kqemu_exec_count;
1058 int64_t dev_time;
1059 int64_t kqemu_ret_int_count;
1060 int64_t kqemu_ret_excp_count;
1061 int64_t kqemu_ret_intr_count;
1063 static void do_info_profile(void)
1065 int64_t total;
1066 total = qemu_time;
1067 if (total == 0)
1068 total = 1;
1069 term_printf("async time %" PRId64 " (%0.3f)\n",
1070 dev_time, dev_time / (double)ticks_per_sec);
1071 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1072 qemu_time, qemu_time / (double)ticks_per_sec);
1073 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1074 kqemu_time, kqemu_time / (double)ticks_per_sec,
1075 kqemu_time / (double)total * 100.0,
1076 kqemu_exec_count,
1077 kqemu_ret_int_count,
1078 kqemu_ret_excp_count,
1079 kqemu_ret_intr_count);
1080 qemu_time = 0;
1081 kqemu_time = 0;
1082 kqemu_exec_count = 0;
1083 dev_time = 0;
1084 kqemu_ret_int_count = 0;
1085 kqemu_ret_excp_count = 0;
1086 kqemu_ret_intr_count = 0;
1087 #ifdef USE_KQEMU
1088 kqemu_record_dump();
1089 #endif
1091 #else
1092 static void do_info_profile(void)
1094 term_printf("Internal profiler not compiled\n");
1096 #endif
1098 /* Capture support */
1099 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1101 static void do_info_capture (void)
1103 int i;
1104 CaptureState *s;
1106 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1107 term_printf ("[%d]: ", i);
1108 s->ops.info (s->opaque);
1112 static void do_stop_capture (int n)
1114 int i;
1115 CaptureState *s;
1117 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1118 if (i == n) {
1119 s->ops.destroy (s->opaque);
1120 LIST_REMOVE (s, entries);
1121 qemu_free (s);
1122 return;
1127 #ifdef HAS_AUDIO
1128 int wav_start_capture (CaptureState *s, const char *path, int freq,
1129 int bits, int nchannels);
1131 static void do_wav_capture (const char *path,
1132 int has_freq, int freq,
1133 int has_bits, int bits,
1134 int has_channels, int nchannels)
1136 CaptureState *s;
1138 s = qemu_mallocz (sizeof (*s));
1139 if (!s) {
1140 term_printf ("Not enough memory to add wave capture\n");
1141 return;
1144 freq = has_freq ? freq : 44100;
1145 bits = has_bits ? bits : 16;
1146 nchannels = has_channels ? nchannels : 2;
1148 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1149 term_printf ("Faied to add wave capture\n");
1150 qemu_free (s);
1152 LIST_INSERT_HEAD (&capture_head, s, entries);
1154 #endif
1156 static term_cmd_t term_cmds[] = {
1157 { "help|?", "s?", do_help,
1158 "[cmd]", "show the help" },
1159 { "commit", "s", do_commit,
1160 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1161 { "info", "s?", do_info,
1162 "subcommand", "show various information about the system state" },
1163 { "q|quit", "", do_quit,
1164 "", "quit the emulator" },
1165 { "eject", "-fB", do_eject,
1166 "[-f] device", "eject a removable media (use -f to force it)" },
1167 { "change", "BF", do_change,
1168 "device filename", "change a removable media" },
1169 { "screendump", "F", do_screen_dump,
1170 "filename", "save screen into PPM image 'filename'" },
1171 { "log", "s", do_log,
1172 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1173 { "savevm", "s?", do_savevm,
1174 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1175 { "loadvm", "s", do_loadvm,
1176 "tag|id", "restore a VM snapshot from its tag or id" },
1177 { "delvm", "s", do_delvm,
1178 "tag|id", "delete a VM snapshot from its tag or id" },
1179 { "stop", "", do_stop,
1180 "", "stop emulation", },
1181 { "c|cont", "", do_cont,
1182 "", "resume emulation", },
1183 #ifdef CONFIG_GDBSTUB
1184 { "gdbserver", "i?", do_gdbserver,
1185 "[port]", "start gdbserver session (default port=1234)", },
1186 #endif
1187 { "x", "/l", do_memory_dump,
1188 "/fmt addr", "virtual memory dump starting at 'addr'", },
1189 { "xp", "/l", do_physical_memory_dump,
1190 "/fmt addr", "physical memory dump starting at 'addr'", },
1191 { "p|print", "/l", do_print,
1192 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1193 { "i", "/ii.", do_ioport_read,
1194 "/fmt addr", "I/O port read" },
1196 { "sendkey", "s", do_send_key,
1197 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1198 { "system_reset", "", do_system_reset,
1199 "", "reset the system" },
1200 { "system_powerdown", "", do_system_powerdown,
1201 "", "send system power down event" },
1202 { "sum", "ii", do_sum,
1203 "addr size", "compute the checksum of a memory region" },
1204 { "usb_add", "s", do_usb_add,
1205 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1206 { "usb_del", "s", do_usb_del,
1207 "device", "remove USB device 'bus.addr'" },
1208 { "cpu", "i", do_cpu_set,
1209 "index", "set the default CPU" },
1210 { "mouse_move", "sss?", do_mouse_move,
1211 "dx dy [dz]", "send mouse move events" },
1212 { "mouse_button", "i", do_mouse_button,
1213 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1214 #ifdef HAS_AUDIO
1215 { "wavcapture", "si?i?i?", do_wav_capture,
1216 "path [frequency bits channels]",
1217 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1218 #endif
1219 { "stopcapture", "i", do_stop_capture,
1220 "capture index", "stop capture" },
1221 { NULL, NULL, },
1224 static term_cmd_t info_cmds[] = {
1225 { "version", "", do_info_version,
1226 "", "show the version of qemu" },
1227 { "network", "", do_info_network,
1228 "", "show the network state" },
1229 { "block", "", do_info_block,
1230 "", "show the block devices" },
1231 { "registers", "", do_info_registers,
1232 "", "show the cpu registers" },
1233 { "cpus", "", do_info_cpus,
1234 "", "show infos for each CPU" },
1235 { "history", "", do_info_history,
1236 "", "show the command line history", },
1237 { "irq", "", irq_info,
1238 "", "show the interrupts statistics (if available)", },
1239 { "pic", "", pic_info,
1240 "", "show i8259 (PIC) state", },
1241 { "pci", "", pci_info,
1242 "", "show PCI info", },
1243 #if defined(TARGET_I386)
1244 { "tlb", "", tlb_info,
1245 "", "show virtual to physical memory mappings", },
1246 { "mem", "", mem_info,
1247 "", "show the active virtual memory mappings", },
1248 #endif
1249 { "jit", "", do_info_jit,
1250 "", "show dynamic compiler info", },
1251 { "kqemu", "", do_info_kqemu,
1252 "", "show kqemu information", },
1253 { "usb", "", usb_info,
1254 "", "show guest USB devices", },
1255 { "usbhost", "", usb_host_info,
1256 "", "show host USB devices", },
1257 { "profile", "", do_info_profile,
1258 "", "show profiling information", },
1259 { "capture", "", do_info_capture,
1260 "", "show capture information" },
1261 { "snapshots", "", do_info_snapshots,
1262 "", "show the currently saved VM snapshots" },
1263 { NULL, NULL, },
1266 /*******************************************************************/
1268 static const char *pch;
1269 static jmp_buf expr_env;
1271 #define MD_TLONG 0
1272 #define MD_I32 1
1274 typedef struct MonitorDef {
1275 const char *name;
1276 int offset;
1277 target_long (*get_value)(struct MonitorDef *md, int val);
1278 int type;
1279 } MonitorDef;
1281 #if defined(TARGET_I386)
1282 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1284 CPUState *env = mon_get_cpu();
1285 if (!env)
1286 return 0;
1287 return env->eip + env->segs[R_CS].base;
1289 #endif
1291 #if defined(TARGET_PPC)
1292 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1294 CPUState *env = mon_get_cpu();
1295 unsigned int u;
1296 int i;
1298 if (!env)
1299 return 0;
1301 u = 0;
1302 for (i = 0; i < 8; i++)
1303 u |= env->crf[i] << (32 - (4 * i));
1305 return u;
1308 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1310 CPUState *env = mon_get_cpu();
1311 if (!env)
1312 return 0;
1313 return (env->msr[MSR_POW] << MSR_POW) |
1314 (env->msr[MSR_ILE] << MSR_ILE) |
1315 (env->msr[MSR_EE] << MSR_EE) |
1316 (env->msr[MSR_PR] << MSR_PR) |
1317 (env->msr[MSR_FP] << MSR_FP) |
1318 (env->msr[MSR_ME] << MSR_ME) |
1319 (env->msr[MSR_FE0] << MSR_FE0) |
1320 (env->msr[MSR_SE] << MSR_SE) |
1321 (env->msr[MSR_BE] << MSR_BE) |
1322 (env->msr[MSR_FE1] << MSR_FE1) |
1323 (env->msr[MSR_IP] << MSR_IP) |
1324 (env->msr[MSR_IR] << MSR_IR) |
1325 (env->msr[MSR_DR] << MSR_DR) |
1326 (env->msr[MSR_RI] << MSR_RI) |
1327 (env->msr[MSR_LE] << MSR_LE);
1330 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1332 CPUState *env = mon_get_cpu();
1333 if (!env)
1334 return 0;
1335 return (env->xer[XER_SO] << XER_SO) |
1336 (env->xer[XER_OV] << XER_OV) |
1337 (env->xer[XER_CA] << XER_CA) |
1338 (env->xer[XER_BC] << XER_BC);
1341 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1343 CPUState *env = mon_get_cpu();
1344 if (!env)
1345 return 0;
1346 return cpu_ppc_load_decr(env);
1349 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1351 CPUState *env = mon_get_cpu();
1352 if (!env)
1353 return 0;
1354 return cpu_ppc_load_tbu(env);
1357 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1359 CPUState *env = mon_get_cpu();
1360 if (!env)
1361 return 0;
1362 return cpu_ppc_load_tbl(env);
1364 #endif
1366 #if defined(TARGET_SPARC)
1367 #ifndef TARGET_SPARC64
1368 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1370 CPUState *env = mon_get_cpu();
1371 if (!env)
1372 return 0;
1373 return GET_PSR(env);
1375 #endif
1377 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1379 CPUState *env = mon_get_cpu();
1380 if (!env)
1381 return 0;
1382 return env->regwptr[val];
1384 #endif
1386 static MonitorDef monitor_defs[] = {
1387 #ifdef TARGET_I386
1389 #define SEG(name, seg) \
1390 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1391 { name ".base", offsetof(CPUState, segs[seg].base) },\
1392 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1394 { "eax", offsetof(CPUState, regs[0]) },
1395 { "ecx", offsetof(CPUState, regs[1]) },
1396 { "edx", offsetof(CPUState, regs[2]) },
1397 { "ebx", offsetof(CPUState, regs[3]) },
1398 { "esp|sp", offsetof(CPUState, regs[4]) },
1399 { "ebp|fp", offsetof(CPUState, regs[5]) },
1400 { "esi", offsetof(CPUState, regs[6]) },
1401 { "edi", offsetof(CPUState, regs[7]) },
1402 #ifdef TARGET_X86_64
1403 { "r8", offsetof(CPUState, regs[8]) },
1404 { "r9", offsetof(CPUState, regs[9]) },
1405 { "r10", offsetof(CPUState, regs[10]) },
1406 { "r11", offsetof(CPUState, regs[11]) },
1407 { "r12", offsetof(CPUState, regs[12]) },
1408 { "r13", offsetof(CPUState, regs[13]) },
1409 { "r14", offsetof(CPUState, regs[14]) },
1410 { "r15", offsetof(CPUState, regs[15]) },
1411 #endif
1412 { "eflags", offsetof(CPUState, eflags) },
1413 { "eip", offsetof(CPUState, eip) },
1414 SEG("cs", R_CS)
1415 SEG("ds", R_DS)
1416 SEG("es", R_ES)
1417 SEG("ss", R_SS)
1418 SEG("fs", R_FS)
1419 SEG("gs", R_GS)
1420 { "pc", 0, monitor_get_pc, },
1421 #elif defined(TARGET_PPC)
1422 { "r0", offsetof(CPUState, gpr[0]) },
1423 { "r1", offsetof(CPUState, gpr[1]) },
1424 { "r2", offsetof(CPUState, gpr[2]) },
1425 { "r3", offsetof(CPUState, gpr[3]) },
1426 { "r4", offsetof(CPUState, gpr[4]) },
1427 { "r5", offsetof(CPUState, gpr[5]) },
1428 { "r6", offsetof(CPUState, gpr[6]) },
1429 { "r7", offsetof(CPUState, gpr[7]) },
1430 { "r8", offsetof(CPUState, gpr[8]) },
1431 { "r9", offsetof(CPUState, gpr[9]) },
1432 { "r10", offsetof(CPUState, gpr[10]) },
1433 { "r11", offsetof(CPUState, gpr[11]) },
1434 { "r12", offsetof(CPUState, gpr[12]) },
1435 { "r13", offsetof(CPUState, gpr[13]) },
1436 { "r14", offsetof(CPUState, gpr[14]) },
1437 { "r15", offsetof(CPUState, gpr[15]) },
1438 { "r16", offsetof(CPUState, gpr[16]) },
1439 { "r17", offsetof(CPUState, gpr[17]) },
1440 { "r18", offsetof(CPUState, gpr[18]) },
1441 { "r19", offsetof(CPUState, gpr[19]) },
1442 { "r20", offsetof(CPUState, gpr[20]) },
1443 { "r21", offsetof(CPUState, gpr[21]) },
1444 { "r22", offsetof(CPUState, gpr[22]) },
1445 { "r23", offsetof(CPUState, gpr[23]) },
1446 { "r24", offsetof(CPUState, gpr[24]) },
1447 { "r25", offsetof(CPUState, gpr[25]) },
1448 { "r26", offsetof(CPUState, gpr[26]) },
1449 { "r27", offsetof(CPUState, gpr[27]) },
1450 { "r28", offsetof(CPUState, gpr[28]) },
1451 { "r29", offsetof(CPUState, gpr[29]) },
1452 { "r30", offsetof(CPUState, gpr[30]) },
1453 { "r31", offsetof(CPUState, gpr[31]) },
1454 { "nip|pc", offsetof(CPUState, nip) },
1455 { "lr", offsetof(CPUState, lr) },
1456 { "ctr", offsetof(CPUState, ctr) },
1457 { "decr", 0, &monitor_get_decr, },
1458 { "ccr", 0, &monitor_get_ccr, },
1459 { "msr", 0, &monitor_get_msr, },
1460 { "xer", 0, &monitor_get_xer, },
1461 { "tbu", 0, &monitor_get_tbu, },
1462 { "tbl", 0, &monitor_get_tbl, },
1463 { "sdr1", offsetof(CPUState, sdr1) },
1464 { "sr0", offsetof(CPUState, sr[0]) },
1465 { "sr1", offsetof(CPUState, sr[1]) },
1466 { "sr2", offsetof(CPUState, sr[2]) },
1467 { "sr3", offsetof(CPUState, sr[3]) },
1468 { "sr4", offsetof(CPUState, sr[4]) },
1469 { "sr5", offsetof(CPUState, sr[5]) },
1470 { "sr6", offsetof(CPUState, sr[6]) },
1471 { "sr7", offsetof(CPUState, sr[7]) },
1472 { "sr8", offsetof(CPUState, sr[8]) },
1473 { "sr9", offsetof(CPUState, sr[9]) },
1474 { "sr10", offsetof(CPUState, sr[10]) },
1475 { "sr11", offsetof(CPUState, sr[11]) },
1476 { "sr12", offsetof(CPUState, sr[12]) },
1477 { "sr13", offsetof(CPUState, sr[13]) },
1478 { "sr14", offsetof(CPUState, sr[14]) },
1479 { "sr15", offsetof(CPUState, sr[15]) },
1480 /* Too lazy to put BATs and SPRs ... */
1481 #elif defined(TARGET_SPARC)
1482 { "g0", offsetof(CPUState, gregs[0]) },
1483 { "g1", offsetof(CPUState, gregs[1]) },
1484 { "g2", offsetof(CPUState, gregs[2]) },
1485 { "g3", offsetof(CPUState, gregs[3]) },
1486 { "g4", offsetof(CPUState, gregs[4]) },
1487 { "g5", offsetof(CPUState, gregs[5]) },
1488 { "g6", offsetof(CPUState, gregs[6]) },
1489 { "g7", offsetof(CPUState, gregs[7]) },
1490 { "o0", 0, monitor_get_reg },
1491 { "o1", 1, monitor_get_reg },
1492 { "o2", 2, monitor_get_reg },
1493 { "o3", 3, monitor_get_reg },
1494 { "o4", 4, monitor_get_reg },
1495 { "o5", 5, monitor_get_reg },
1496 { "o6", 6, monitor_get_reg },
1497 { "o7", 7, monitor_get_reg },
1498 { "l0", 8, monitor_get_reg },
1499 { "l1", 9, monitor_get_reg },
1500 { "l2", 10, monitor_get_reg },
1501 { "l3", 11, monitor_get_reg },
1502 { "l4", 12, monitor_get_reg },
1503 { "l5", 13, monitor_get_reg },
1504 { "l6", 14, monitor_get_reg },
1505 { "l7", 15, monitor_get_reg },
1506 { "i0", 16, monitor_get_reg },
1507 { "i1", 17, monitor_get_reg },
1508 { "i2", 18, monitor_get_reg },
1509 { "i3", 19, monitor_get_reg },
1510 { "i4", 20, monitor_get_reg },
1511 { "i5", 21, monitor_get_reg },
1512 { "i6", 22, monitor_get_reg },
1513 { "i7", 23, monitor_get_reg },
1514 { "pc", offsetof(CPUState, pc) },
1515 { "npc", offsetof(CPUState, npc) },
1516 { "y", offsetof(CPUState, y) },
1517 #ifndef TARGET_SPARC64
1518 { "psr", 0, &monitor_get_psr, },
1519 { "wim", offsetof(CPUState, wim) },
1520 #endif
1521 { "tbr", offsetof(CPUState, tbr) },
1522 { "fsr", offsetof(CPUState, fsr) },
1523 { "f0", offsetof(CPUState, fpr[0]) },
1524 { "f1", offsetof(CPUState, fpr[1]) },
1525 { "f2", offsetof(CPUState, fpr[2]) },
1526 { "f3", offsetof(CPUState, fpr[3]) },
1527 { "f4", offsetof(CPUState, fpr[4]) },
1528 { "f5", offsetof(CPUState, fpr[5]) },
1529 { "f6", offsetof(CPUState, fpr[6]) },
1530 { "f7", offsetof(CPUState, fpr[7]) },
1531 { "f8", offsetof(CPUState, fpr[8]) },
1532 { "f9", offsetof(CPUState, fpr[9]) },
1533 { "f10", offsetof(CPUState, fpr[10]) },
1534 { "f11", offsetof(CPUState, fpr[11]) },
1535 { "f12", offsetof(CPUState, fpr[12]) },
1536 { "f13", offsetof(CPUState, fpr[13]) },
1537 { "f14", offsetof(CPUState, fpr[14]) },
1538 { "f15", offsetof(CPUState, fpr[15]) },
1539 { "f16", offsetof(CPUState, fpr[16]) },
1540 { "f17", offsetof(CPUState, fpr[17]) },
1541 { "f18", offsetof(CPUState, fpr[18]) },
1542 { "f19", offsetof(CPUState, fpr[19]) },
1543 { "f20", offsetof(CPUState, fpr[20]) },
1544 { "f21", offsetof(CPUState, fpr[21]) },
1545 { "f22", offsetof(CPUState, fpr[22]) },
1546 { "f23", offsetof(CPUState, fpr[23]) },
1547 { "f24", offsetof(CPUState, fpr[24]) },
1548 { "f25", offsetof(CPUState, fpr[25]) },
1549 { "f26", offsetof(CPUState, fpr[26]) },
1550 { "f27", offsetof(CPUState, fpr[27]) },
1551 { "f28", offsetof(CPUState, fpr[28]) },
1552 { "f29", offsetof(CPUState, fpr[29]) },
1553 { "f30", offsetof(CPUState, fpr[30]) },
1554 { "f31", offsetof(CPUState, fpr[31]) },
1555 #ifdef TARGET_SPARC64
1556 { "f32", offsetof(CPUState, fpr[32]) },
1557 { "f34", offsetof(CPUState, fpr[34]) },
1558 { "f36", offsetof(CPUState, fpr[36]) },
1559 { "f38", offsetof(CPUState, fpr[38]) },
1560 { "f40", offsetof(CPUState, fpr[40]) },
1561 { "f42", offsetof(CPUState, fpr[42]) },
1562 { "f44", offsetof(CPUState, fpr[44]) },
1563 { "f46", offsetof(CPUState, fpr[46]) },
1564 { "f48", offsetof(CPUState, fpr[48]) },
1565 { "f50", offsetof(CPUState, fpr[50]) },
1566 { "f52", offsetof(CPUState, fpr[52]) },
1567 { "f54", offsetof(CPUState, fpr[54]) },
1568 { "f56", offsetof(CPUState, fpr[56]) },
1569 { "f58", offsetof(CPUState, fpr[58]) },
1570 { "f60", offsetof(CPUState, fpr[60]) },
1571 { "f62", offsetof(CPUState, fpr[62]) },
1572 { "asi", offsetof(CPUState, asi) },
1573 { "pstate", offsetof(CPUState, pstate) },
1574 { "cansave", offsetof(CPUState, cansave) },
1575 { "canrestore", offsetof(CPUState, canrestore) },
1576 { "otherwin", offsetof(CPUState, otherwin) },
1577 { "wstate", offsetof(CPUState, wstate) },
1578 { "cleanwin", offsetof(CPUState, cleanwin) },
1579 { "fprs", offsetof(CPUState, fprs) },
1580 #endif
1581 #endif
1582 { NULL },
1585 static void expr_error(const char *fmt)
1587 term_printf(fmt);
1588 term_printf("\n");
1589 longjmp(expr_env, 1);
1592 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1593 static int get_monitor_def(target_long *pval, const char *name)
1595 MonitorDef *md;
1596 void *ptr;
1598 for(md = monitor_defs; md->name != NULL; md++) {
1599 if (compare_cmd(name, md->name)) {
1600 if (md->get_value) {
1601 *pval = md->get_value(md, md->offset);
1602 } else {
1603 CPUState *env = mon_get_cpu();
1604 if (!env)
1605 return -2;
1606 ptr = (uint8_t *)env + md->offset;
1607 switch(md->type) {
1608 case MD_I32:
1609 *pval = *(int32_t *)ptr;
1610 break;
1611 case MD_TLONG:
1612 *pval = *(target_long *)ptr;
1613 break;
1614 default:
1615 *pval = 0;
1616 break;
1619 return 0;
1622 return -1;
1625 static void next(void)
1627 if (pch != '\0') {
1628 pch++;
1629 while (isspace(*pch))
1630 pch++;
1634 static target_long expr_sum(void);
1636 static target_long expr_unary(void)
1638 target_long n;
1639 char *p;
1640 int ret;
1642 switch(*pch) {
1643 case '+':
1644 next();
1645 n = expr_unary();
1646 break;
1647 case '-':
1648 next();
1649 n = -expr_unary();
1650 break;
1651 case '~':
1652 next();
1653 n = ~expr_unary();
1654 break;
1655 case '(':
1656 next();
1657 n = expr_sum();
1658 if (*pch != ')') {
1659 expr_error("')' expected");
1661 next();
1662 break;
1663 case '\'':
1664 pch++;
1665 if (*pch == '\0')
1666 expr_error("character constant expected");
1667 n = *pch;
1668 pch++;
1669 if (*pch != '\'')
1670 expr_error("missing terminating \' character");
1671 next();
1672 break;
1673 case '$':
1675 char buf[128], *q;
1677 pch++;
1678 q = buf;
1679 while ((*pch >= 'a' && *pch <= 'z') ||
1680 (*pch >= 'A' && *pch <= 'Z') ||
1681 (*pch >= '0' && *pch <= '9') ||
1682 *pch == '_' || *pch == '.') {
1683 if ((q - buf) < sizeof(buf) - 1)
1684 *q++ = *pch;
1685 pch++;
1687 while (isspace(*pch))
1688 pch++;
1689 *q = 0;
1690 ret = get_monitor_def(&n, buf);
1691 if (ret == -1)
1692 expr_error("unknown register");
1693 else if (ret == -2)
1694 expr_error("no cpu defined");
1696 break;
1697 case '\0':
1698 expr_error("unexpected end of expression");
1699 n = 0;
1700 break;
1701 default:
1702 #if TARGET_LONG_BITS == 64
1703 n = strtoull(pch, &p, 0);
1704 #else
1705 n = strtoul(pch, &p, 0);
1706 #endif
1707 if (pch == p) {
1708 expr_error("invalid char in expression");
1710 pch = p;
1711 while (isspace(*pch))
1712 pch++;
1713 break;
1715 return n;
1719 static target_long expr_prod(void)
1721 target_long val, val2;
1722 int op;
1724 val = expr_unary();
1725 for(;;) {
1726 op = *pch;
1727 if (op != '*' && op != '/' && op != '%')
1728 break;
1729 next();
1730 val2 = expr_unary();
1731 switch(op) {
1732 default:
1733 case '*':
1734 val *= val2;
1735 break;
1736 case '/':
1737 case '%':
1738 if (val2 == 0)
1739 expr_error("division by zero");
1740 if (op == '/')
1741 val /= val2;
1742 else
1743 val %= val2;
1744 break;
1747 return val;
1750 static target_long expr_logic(void)
1752 target_long val, val2;
1753 int op;
1755 val = expr_prod();
1756 for(;;) {
1757 op = *pch;
1758 if (op != '&' && op != '|' && op != '^')
1759 break;
1760 next();
1761 val2 = expr_prod();
1762 switch(op) {
1763 default:
1764 case '&':
1765 val &= val2;
1766 break;
1767 case '|':
1768 val |= val2;
1769 break;
1770 case '^':
1771 val ^= val2;
1772 break;
1775 return val;
1778 static target_long expr_sum(void)
1780 target_long val, val2;
1781 int op;
1783 val = expr_logic();
1784 for(;;) {
1785 op = *pch;
1786 if (op != '+' && op != '-')
1787 break;
1788 next();
1789 val2 = expr_logic();
1790 if (op == '+')
1791 val += val2;
1792 else
1793 val -= val2;
1795 return val;
1798 static int get_expr(target_long *pval, const char **pp)
1800 pch = *pp;
1801 if (setjmp(expr_env)) {
1802 *pp = pch;
1803 return -1;
1805 while (isspace(*pch))
1806 pch++;
1807 *pval = expr_sum();
1808 *pp = pch;
1809 return 0;
1812 static int get_str(char *buf, int buf_size, const char **pp)
1814 const char *p;
1815 char *q;
1816 int c;
1818 q = buf;
1819 p = *pp;
1820 while (isspace(*p))
1821 p++;
1822 if (*p == '\0') {
1823 fail:
1824 *q = '\0';
1825 *pp = p;
1826 return -1;
1828 if (*p == '\"') {
1829 p++;
1830 while (*p != '\0' && *p != '\"') {
1831 if (*p == '\\') {
1832 p++;
1833 c = *p++;
1834 switch(c) {
1835 case 'n':
1836 c = '\n';
1837 break;
1838 case 'r':
1839 c = '\r';
1840 break;
1841 case '\\':
1842 case '\'':
1843 case '\"':
1844 break;
1845 default:
1846 qemu_printf("unsupported escape code: '\\%c'\n", c);
1847 goto fail;
1849 if ((q - buf) < buf_size - 1) {
1850 *q++ = c;
1852 } else {
1853 if ((q - buf) < buf_size - 1) {
1854 *q++ = *p;
1856 p++;
1859 if (*p != '\"') {
1860 qemu_printf("unterminated string\n");
1861 goto fail;
1863 p++;
1864 } else {
1865 while (*p != '\0' && !isspace(*p)) {
1866 if ((q - buf) < buf_size - 1) {
1867 *q++ = *p;
1869 p++;
1872 *q = '\0';
1873 *pp = p;
1874 return 0;
1877 static int default_fmt_format = 'x';
1878 static int default_fmt_size = 4;
1880 #define MAX_ARGS 16
1882 static void monitor_handle_command(const char *cmdline)
1884 const char *p, *pstart, *typestr;
1885 char *q;
1886 int c, nb_args, len, i, has_arg;
1887 term_cmd_t *cmd;
1888 char cmdname[256];
1889 char buf[1024];
1890 void *str_allocated[MAX_ARGS];
1891 void *args[MAX_ARGS];
1893 #ifdef DEBUG
1894 term_printf("command='%s'\n", cmdline);
1895 #endif
1897 /* extract the command name */
1898 p = cmdline;
1899 q = cmdname;
1900 while (isspace(*p))
1901 p++;
1902 if (*p == '\0')
1903 return;
1904 pstart = p;
1905 while (*p != '\0' && *p != '/' && !isspace(*p))
1906 p++;
1907 len = p - pstart;
1908 if (len > sizeof(cmdname) - 1)
1909 len = sizeof(cmdname) - 1;
1910 memcpy(cmdname, pstart, len);
1911 cmdname[len] = '\0';
1913 /* find the command */
1914 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1915 if (compare_cmd(cmdname, cmd->name))
1916 goto found;
1918 term_printf("unknown command: '%s'\n", cmdname);
1919 return;
1920 found:
1922 for(i = 0; i < MAX_ARGS; i++)
1923 str_allocated[i] = NULL;
1925 /* parse the parameters */
1926 typestr = cmd->args_type;
1927 nb_args = 0;
1928 for(;;) {
1929 c = *typestr;
1930 if (c == '\0')
1931 break;
1932 typestr++;
1933 switch(c) {
1934 case 'F':
1935 case 'B':
1936 case 's':
1938 int ret;
1939 char *str;
1941 while (isspace(*p))
1942 p++;
1943 if (*typestr == '?') {
1944 typestr++;
1945 if (*p == '\0') {
1946 /* no optional string: NULL argument */
1947 str = NULL;
1948 goto add_str;
1951 ret = get_str(buf, sizeof(buf), &p);
1952 if (ret < 0) {
1953 switch(c) {
1954 case 'F':
1955 term_printf("%s: filename expected\n", cmdname);
1956 break;
1957 case 'B':
1958 term_printf("%s: block device name expected\n", cmdname);
1959 break;
1960 default:
1961 term_printf("%s: string expected\n", cmdname);
1962 break;
1964 goto fail;
1966 str = qemu_malloc(strlen(buf) + 1);
1967 strcpy(str, buf);
1968 str_allocated[nb_args] = str;
1969 add_str:
1970 if (nb_args >= MAX_ARGS) {
1971 error_args:
1972 term_printf("%s: too many arguments\n", cmdname);
1973 goto fail;
1975 args[nb_args++] = str;
1977 break;
1978 case '/':
1980 int count, format, size;
1982 while (isspace(*p))
1983 p++;
1984 if (*p == '/') {
1985 /* format found */
1986 p++;
1987 count = 1;
1988 if (isdigit(*p)) {
1989 count = 0;
1990 while (isdigit(*p)) {
1991 count = count * 10 + (*p - '0');
1992 p++;
1995 size = -1;
1996 format = -1;
1997 for(;;) {
1998 switch(*p) {
1999 case 'o':
2000 case 'd':
2001 case 'u':
2002 case 'x':
2003 case 'i':
2004 case 'c':
2005 format = *p++;
2006 break;
2007 case 'b':
2008 size = 1;
2009 p++;
2010 break;
2011 case 'h':
2012 size = 2;
2013 p++;
2014 break;
2015 case 'w':
2016 size = 4;
2017 p++;
2018 break;
2019 case 'g':
2020 case 'L':
2021 size = 8;
2022 p++;
2023 break;
2024 default:
2025 goto next;
2028 next:
2029 if (*p != '\0' && !isspace(*p)) {
2030 term_printf("invalid char in format: '%c'\n", *p);
2031 goto fail;
2033 if (format < 0)
2034 format = default_fmt_format;
2035 if (format != 'i') {
2036 /* for 'i', not specifying a size gives -1 as size */
2037 if (size < 0)
2038 size = default_fmt_size;
2040 default_fmt_size = size;
2041 default_fmt_format = format;
2042 } else {
2043 count = 1;
2044 format = default_fmt_format;
2045 if (format != 'i') {
2046 size = default_fmt_size;
2047 } else {
2048 size = -1;
2051 if (nb_args + 3 > MAX_ARGS)
2052 goto error_args;
2053 args[nb_args++] = (void*)count;
2054 args[nb_args++] = (void*)format;
2055 args[nb_args++] = (void*)size;
2057 break;
2058 case 'i':
2059 case 'l':
2061 target_long val;
2062 while (isspace(*p))
2063 p++;
2064 if (*typestr == '?' || *typestr == '.') {
2065 if (*typestr == '?') {
2066 if (*p == '\0')
2067 has_arg = 0;
2068 else
2069 has_arg = 1;
2070 } else {
2071 if (*p == '.') {
2072 p++;
2073 while (isspace(*p))
2074 p++;
2075 has_arg = 1;
2076 } else {
2077 has_arg = 0;
2080 typestr++;
2081 if (nb_args >= MAX_ARGS)
2082 goto error_args;
2083 args[nb_args++] = (void *)has_arg;
2084 if (!has_arg) {
2085 if (nb_args >= MAX_ARGS)
2086 goto error_args;
2087 val = -1;
2088 goto add_num;
2091 if (get_expr(&val, &p))
2092 goto fail;
2093 add_num:
2094 if (c == 'i') {
2095 if (nb_args >= MAX_ARGS)
2096 goto error_args;
2097 args[nb_args++] = (void *)(int)val;
2098 } else {
2099 if ((nb_args + 1) >= MAX_ARGS)
2100 goto error_args;
2101 #if TARGET_LONG_BITS == 64
2102 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2103 #else
2104 args[nb_args++] = (void *)0;
2105 #endif
2106 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2109 break;
2110 case '-':
2112 int has_option;
2113 /* option */
2115 c = *typestr++;
2116 if (c == '\0')
2117 goto bad_type;
2118 while (isspace(*p))
2119 p++;
2120 has_option = 0;
2121 if (*p == '-') {
2122 p++;
2123 if (*p != c) {
2124 term_printf("%s: unsupported option -%c\n",
2125 cmdname, *p);
2126 goto fail;
2128 p++;
2129 has_option = 1;
2131 if (nb_args >= MAX_ARGS)
2132 goto error_args;
2133 args[nb_args++] = (void *)has_option;
2135 break;
2136 default:
2137 bad_type:
2138 term_printf("%s: unknown type '%c'\n", cmdname, c);
2139 goto fail;
2142 /* check that all arguments were parsed */
2143 while (isspace(*p))
2144 p++;
2145 if (*p != '\0') {
2146 term_printf("%s: extraneous characters at the end of line\n",
2147 cmdname);
2148 goto fail;
2151 switch(nb_args) {
2152 case 0:
2153 cmd->handler();
2154 break;
2155 case 1:
2156 cmd->handler(args[0]);
2157 break;
2158 case 2:
2159 cmd->handler(args[0], args[1]);
2160 break;
2161 case 3:
2162 cmd->handler(args[0], args[1], args[2]);
2163 break;
2164 case 4:
2165 cmd->handler(args[0], args[1], args[2], args[3]);
2166 break;
2167 case 5:
2168 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2169 break;
2170 case 6:
2171 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2172 break;
2173 case 7:
2174 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2175 break;
2176 default:
2177 term_printf("unsupported number of arguments: %d\n", nb_args);
2178 goto fail;
2180 fail:
2181 for(i = 0; i < MAX_ARGS; i++)
2182 qemu_free(str_allocated[i]);
2183 return;
2186 static void cmd_completion(const char *name, const char *list)
2188 const char *p, *pstart;
2189 char cmd[128];
2190 int len;
2192 p = list;
2193 for(;;) {
2194 pstart = p;
2195 p = strchr(p, '|');
2196 if (!p)
2197 p = pstart + strlen(pstart);
2198 len = p - pstart;
2199 if (len > sizeof(cmd) - 2)
2200 len = sizeof(cmd) - 2;
2201 memcpy(cmd, pstart, len);
2202 cmd[len] = '\0';
2203 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2204 add_completion(cmd);
2206 if (*p == '\0')
2207 break;
2208 p++;
2212 static void file_completion(const char *input)
2214 DIR *ffs;
2215 struct dirent *d;
2216 char path[1024];
2217 char file[1024], file_prefix[1024];
2218 int input_path_len;
2219 const char *p;
2221 p = strrchr(input, '/');
2222 if (!p) {
2223 input_path_len = 0;
2224 pstrcpy(file_prefix, sizeof(file_prefix), input);
2225 strcpy(path, ".");
2226 } else {
2227 input_path_len = p - input + 1;
2228 memcpy(path, input, input_path_len);
2229 if (input_path_len > sizeof(path) - 1)
2230 input_path_len = sizeof(path) - 1;
2231 path[input_path_len] = '\0';
2232 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2234 #ifdef DEBUG_COMPLETION
2235 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2236 #endif
2237 ffs = opendir(path);
2238 if (!ffs)
2239 return;
2240 for(;;) {
2241 struct stat sb;
2242 d = readdir(ffs);
2243 if (!d)
2244 break;
2245 if (strstart(d->d_name, file_prefix, NULL)) {
2246 memcpy(file, input, input_path_len);
2247 strcpy(file + input_path_len, d->d_name);
2248 /* stat the file to find out if it's a directory.
2249 * In that case add a slash to speed up typing long paths
2251 stat(file, &sb);
2252 if(S_ISDIR(sb.st_mode))
2253 strcat(file, "/");
2254 add_completion(file);
2257 closedir(ffs);
2260 static void block_completion_it(void *opaque, const char *name)
2262 const char *input = opaque;
2264 if (input[0] == '\0' ||
2265 !strncmp(name, (char *)input, strlen(input))) {
2266 add_completion(name);
2270 /* NOTE: this parser is an approximate form of the real command parser */
2271 static void parse_cmdline(const char *cmdline,
2272 int *pnb_args, char **args)
2274 const char *p;
2275 int nb_args, ret;
2276 char buf[1024];
2278 p = cmdline;
2279 nb_args = 0;
2280 for(;;) {
2281 while (isspace(*p))
2282 p++;
2283 if (*p == '\0')
2284 break;
2285 if (nb_args >= MAX_ARGS)
2286 break;
2287 ret = get_str(buf, sizeof(buf), &p);
2288 args[nb_args] = qemu_strdup(buf);
2289 nb_args++;
2290 if (ret < 0)
2291 break;
2293 *pnb_args = nb_args;
2296 void readline_find_completion(const char *cmdline)
2298 const char *cmdname;
2299 char *args[MAX_ARGS];
2300 int nb_args, i, len;
2301 const char *ptype, *str;
2302 term_cmd_t *cmd;
2303 const KeyDef *key;
2305 parse_cmdline(cmdline, &nb_args, args);
2306 #ifdef DEBUG_COMPLETION
2307 for(i = 0; i < nb_args; i++) {
2308 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2310 #endif
2312 /* if the line ends with a space, it means we want to complete the
2313 next arg */
2314 len = strlen(cmdline);
2315 if (len > 0 && isspace(cmdline[len - 1])) {
2316 if (nb_args >= MAX_ARGS)
2317 return;
2318 args[nb_args++] = qemu_strdup("");
2320 if (nb_args <= 1) {
2321 /* command completion */
2322 if (nb_args == 0)
2323 cmdname = "";
2324 else
2325 cmdname = args[0];
2326 completion_index = strlen(cmdname);
2327 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2328 cmd_completion(cmdname, cmd->name);
2330 } else {
2331 /* find the command */
2332 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2333 if (compare_cmd(args[0], cmd->name))
2334 goto found;
2336 return;
2337 found:
2338 ptype = cmd->args_type;
2339 for(i = 0; i < nb_args - 2; i++) {
2340 if (*ptype != '\0') {
2341 ptype++;
2342 while (*ptype == '?')
2343 ptype++;
2346 str = args[nb_args - 1];
2347 switch(*ptype) {
2348 case 'F':
2349 /* file completion */
2350 completion_index = strlen(str);
2351 file_completion(str);
2352 break;
2353 case 'B':
2354 /* block device name completion */
2355 completion_index = strlen(str);
2356 bdrv_iterate(block_completion_it, (void *)str);
2357 break;
2358 case 's':
2359 /* XXX: more generic ? */
2360 if (!strcmp(cmd->name, "info")) {
2361 completion_index = strlen(str);
2362 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2363 cmd_completion(str, cmd->name);
2365 } else if (!strcmp(cmd->name, "sendkey")) {
2366 completion_index = strlen(str);
2367 for(key = key_defs; key->name != NULL; key++) {
2368 cmd_completion(str, key->name);
2371 break;
2372 default:
2373 break;
2376 for(i = 0; i < nb_args; i++)
2377 qemu_free(args[i]);
2380 static int term_can_read(void *opaque)
2382 return 128;
2385 static void term_read(void *opaque, const uint8_t *buf, int size)
2387 int i;
2388 for(i = 0; i < size; i++)
2389 readline_handle_byte(buf[i]);
2392 static void monitor_start_input(void);
2394 static void monitor_handle_command1(void *opaque, const char *cmdline)
2396 monitor_handle_command(cmdline);
2397 monitor_start_input();
2400 static void monitor_start_input(void)
2402 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2405 void monitor_init(CharDriverState *hd, int show_banner)
2407 monitor_hd = hd;
2408 if (show_banner) {
2409 term_printf("QEMU %s monitor - type 'help' for more information\n",
2410 QEMU_VERSION);
2412 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2413 monitor_start_input();
2416 /* XXX: use threads ? */
2417 /* modal monitor readline */
2418 static int monitor_readline_started;
2419 static char *monitor_readline_buf;
2420 static int monitor_readline_buf_size;
2422 static void monitor_readline_cb(void *opaque, const char *input)
2424 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2425 monitor_readline_started = 0;
2428 void monitor_readline(const char *prompt, int is_password,
2429 char *buf, int buf_size)
2431 if (is_password) {
2432 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2434 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2435 monitor_readline_buf = buf;
2436 monitor_readline_buf_size = buf_size;
2437 monitor_readline_started = 1;
2438 while (monitor_readline_started) {
2439 main_loop_wait(10);