fixed refresh logic (initial patch by Igor Kovalenko)
[qemu/mini2440.git] / monitor.c
blobb48e8cc702e9356b21d3f3cbdced9b27857abd9f
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(const char *device)
172 int i, all_devices;
174 all_devices = !strcmp(device, "all");
175 for (i = 0; i < MAX_DISKS; i++) {
176 if (bs_table[i]) {
177 if (all_devices ||
178 !strcmp(bdrv_get_device_name(bs_table[i]), device))
179 bdrv_commit(bs_table[i]);
184 static void do_info(const char *item)
186 term_cmd_t *cmd;
188 if (!item)
189 goto help;
190 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
191 if (compare_cmd(item, cmd->name))
192 goto found;
194 help:
195 help_cmd("info");
196 return;
197 found:
198 cmd->handler();
201 static void do_info_version(void)
203 term_printf("%s\n", QEMU_VERSION);
206 static void do_info_block(void)
208 bdrv_info();
211 /* get the current CPU defined by the user */
212 int mon_set_cpu(int cpu_index)
214 CPUState *env;
216 for(env = first_cpu; env != NULL; env = env->next_cpu) {
217 if (env->cpu_index == cpu_index) {
218 mon_cpu = env;
219 return 0;
222 return -1;
225 CPUState *mon_get_cpu(void)
227 if (!mon_cpu) {
228 mon_set_cpu(0);
230 return mon_cpu;
233 static void do_info_registers(void)
235 CPUState *env;
236 env = mon_get_cpu();
237 if (!env)
238 return;
239 #ifdef TARGET_I386
240 cpu_dump_state(env, NULL, monitor_fprintf,
241 X86_DUMP_FPU);
242 #else
243 cpu_dump_state(env, NULL, monitor_fprintf,
245 #endif
248 static void do_info_cpus(void)
250 CPUState *env;
252 /* just to set the default cpu if not already done */
253 mon_get_cpu();
255 for(env = first_cpu; env != NULL; env = env->next_cpu) {
256 term_printf("%c CPU #%d:",
257 (env == mon_cpu) ? '*' : ' ',
258 env->cpu_index);
259 #if defined(TARGET_I386)
260 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
261 if (env->hflags & HF_HALTED_MASK)
262 term_printf(" (halted)");
263 #elif defined(TARGET_PPC)
264 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
265 if (env->halted)
266 term_printf(" (halted)");
267 #elif defined(TARGET_SPARC)
268 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
269 if (env->halted)
270 term_printf(" (halted)");
271 #endif
272 term_printf("\n");
276 static void do_cpu_set(int index)
278 if (mon_set_cpu(index) < 0)
279 term_printf("Invalid CPU index\n");
282 static void do_info_jit(void)
284 dump_exec_info(NULL, monitor_fprintf);
287 static void do_info_history (void)
289 int i;
290 const char *str;
292 i = 0;
293 for(;;) {
294 str = readline_get_history(i);
295 if (!str)
296 break;
297 term_printf("%d: '%s'\n", i, str);
298 i++;
302 static void do_quit(void)
304 exit(0);
307 static int eject_device(BlockDriverState *bs, int force)
309 if (bdrv_is_inserted(bs)) {
310 if (!force) {
311 if (!bdrv_is_removable(bs)) {
312 term_printf("device is not removable\n");
313 return -1;
315 if (bdrv_is_locked(bs)) {
316 term_printf("device is locked\n");
317 return -1;
320 bdrv_close(bs);
322 return 0;
325 static void do_eject(int force, const char *filename)
327 BlockDriverState *bs;
329 bs = bdrv_find(filename);
330 if (!bs) {
331 term_printf("device not found\n");
332 return;
334 eject_device(bs, force);
337 static void do_change(const char *device, const char *filename)
339 BlockDriverState *bs;
340 int i;
341 char password[256];
343 bs = bdrv_find(device);
344 if (!bs) {
345 term_printf("device not found\n");
346 return;
348 if (eject_device(bs, 0) < 0)
349 return;
350 bdrv_open(bs, filename, 0);
351 if (bdrv_is_encrypted(bs)) {
352 term_printf("%s is encrypted.\n", device);
353 for(i = 0; i < 3; i++) {
354 monitor_readline("Password: ", 1, password, sizeof(password));
355 if (bdrv_set_key(bs, password) == 0)
356 break;
357 term_printf("invalid password\n");
362 static void do_screen_dump(const char *filename)
364 vga_hw_screen_dump(filename);
367 static void do_log(const char *items)
369 int mask;
371 if (!strcmp(items, "none")) {
372 mask = 0;
373 } else {
374 mask = cpu_str_to_log_mask(items);
375 if (!mask) {
376 help_cmd("log");
377 return;
380 cpu_set_log(mask);
383 static void do_savevm(const char *filename)
385 if (qemu_savevm(filename) < 0)
386 term_printf("I/O error when saving VM to '%s'\n", filename);
389 static void do_loadvm(const char *filename)
391 if (qemu_loadvm(filename) < 0)
392 term_printf("I/O error when loading VM from '%s'\n", filename);
395 static void do_stop(void)
397 vm_stop(EXCP_INTERRUPT);
400 static void do_cont(void)
402 vm_start();
405 #ifdef CONFIG_GDBSTUB
406 static void do_gdbserver(int has_port, int port)
408 if (!has_port)
409 port = DEFAULT_GDBSTUB_PORT;
410 if (gdbserver_start(port) < 0) {
411 qemu_printf("Could not open gdbserver socket on port %d\n", port);
412 } else {
413 qemu_printf("Waiting gdb connection on port %d\n", port);
416 #endif
418 static void term_printc(int c)
420 term_printf("'");
421 switch(c) {
422 case '\'':
423 term_printf("\\'");
424 break;
425 case '\\':
426 term_printf("\\\\");
427 break;
428 case '\n':
429 term_printf("\\n");
430 break;
431 case '\r':
432 term_printf("\\r");
433 break;
434 default:
435 if (c >= 32 && c <= 126) {
436 term_printf("%c", c);
437 } else {
438 term_printf("\\x%02x", c);
440 break;
442 term_printf("'");
445 static void memory_dump(int count, int format, int wsize,
446 target_ulong addr, int is_physical)
448 CPUState *env;
449 int nb_per_line, l, line_size, i, max_digits, len;
450 uint8_t buf[16];
451 uint64_t v;
453 if (format == 'i') {
454 int flags;
455 flags = 0;
456 env = mon_get_cpu();
457 if (!env && !is_physical)
458 return;
459 #ifdef TARGET_I386
460 if (wsize == 2) {
461 flags = 1;
462 } else if (wsize == 4) {
463 flags = 0;
464 } else {
465 /* as default we use the current CS size */
466 flags = 0;
467 if (env) {
468 #ifdef TARGET_X86_64
469 if ((env->efer & MSR_EFER_LMA) &&
470 (env->segs[R_CS].flags & DESC_L_MASK))
471 flags = 2;
472 else
473 #endif
474 if (!(env->segs[R_CS].flags & DESC_B_MASK))
475 flags = 1;
478 #endif
479 monitor_disas(env, addr, count, is_physical, flags);
480 return;
483 len = wsize * count;
484 if (wsize == 1)
485 line_size = 8;
486 else
487 line_size = 16;
488 nb_per_line = line_size / wsize;
489 max_digits = 0;
491 switch(format) {
492 case 'o':
493 max_digits = (wsize * 8 + 2) / 3;
494 break;
495 default:
496 case 'x':
497 max_digits = (wsize * 8) / 4;
498 break;
499 case 'u':
500 case 'd':
501 max_digits = (wsize * 8 * 10 + 32) / 33;
502 break;
503 case 'c':
504 wsize = 1;
505 break;
508 while (len > 0) {
509 term_printf(TARGET_FMT_lx ":", addr);
510 l = len;
511 if (l > line_size)
512 l = line_size;
513 if (is_physical) {
514 cpu_physical_memory_rw(addr, buf, l, 0);
515 } else {
516 env = mon_get_cpu();
517 if (!env)
518 break;
519 cpu_memory_rw_debug(env, addr, buf, l, 0);
521 i = 0;
522 while (i < l) {
523 switch(wsize) {
524 default:
525 case 1:
526 v = ldub_raw(buf + i);
527 break;
528 case 2:
529 v = lduw_raw(buf + i);
530 break;
531 case 4:
532 v = (uint32_t)ldl_raw(buf + i);
533 break;
534 case 8:
535 v = ldq_raw(buf + i);
536 break;
538 term_printf(" ");
539 switch(format) {
540 case 'o':
541 term_printf("%#*" PRIo64, max_digits, v);
542 break;
543 case 'x':
544 term_printf("0x%0*" PRIx64, max_digits, v);
545 break;
546 case 'u':
547 term_printf("%*" PRIu64, max_digits, v);
548 break;
549 case 'd':
550 term_printf("%*" PRId64, max_digits, v);
551 break;
552 case 'c':
553 term_printc(v);
554 break;
556 i += wsize;
558 term_printf("\n");
559 addr += l;
560 len -= l;
564 #if TARGET_LONG_BITS == 64
565 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
566 #else
567 #define GET_TLONG(h, l) (l)
568 #endif
570 static void do_memory_dump(int count, int format, int size,
571 uint32_t addrh, uint32_t addrl)
573 target_long addr = GET_TLONG(addrh, addrl);
574 memory_dump(count, format, size, addr, 0);
577 static void do_physical_memory_dump(int count, int format, int size,
578 uint32_t addrh, uint32_t addrl)
581 target_long addr = GET_TLONG(addrh, addrl);
582 memory_dump(count, format, size, addr, 1);
585 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
587 target_long val = GET_TLONG(valh, vall);
588 #if TARGET_LONG_BITS == 32
589 switch(format) {
590 case 'o':
591 term_printf("%#o", val);
592 break;
593 case 'x':
594 term_printf("%#x", val);
595 break;
596 case 'u':
597 term_printf("%u", val);
598 break;
599 default:
600 case 'd':
601 term_printf("%d", val);
602 break;
603 case 'c':
604 term_printc(val);
605 break;
607 #else
608 switch(format) {
609 case 'o':
610 term_printf("%#" PRIo64, val);
611 break;
612 case 'x':
613 term_printf("%#" PRIx64, val);
614 break;
615 case 'u':
616 term_printf("%" PRIu64, val);
617 break;
618 default:
619 case 'd':
620 term_printf("%" PRId64, val);
621 break;
622 case 'c':
623 term_printc(val);
624 break;
626 #endif
627 term_printf("\n");
630 static void do_sum(uint32_t start, uint32_t size)
632 uint32_t addr;
633 uint8_t buf[1];
634 uint16_t sum;
636 sum = 0;
637 for(addr = start; addr < (start + size); addr++) {
638 cpu_physical_memory_rw(addr, buf, 1, 0);
639 /* BSD sum algorithm ('sum' Unix command) */
640 sum = (sum >> 1) | (sum << 15);
641 sum += buf[0];
643 term_printf("%05d\n", sum);
646 typedef struct {
647 int keycode;
648 const char *name;
649 } KeyDef;
651 static const KeyDef key_defs[] = {
652 { 0x2a, "shift" },
653 { 0x36, "shift_r" },
655 { 0x38, "alt" },
656 { 0xb8, "alt_r" },
657 { 0x1d, "ctrl" },
658 { 0x9d, "ctrl_r" },
660 { 0xdd, "menu" },
662 { 0x01, "esc" },
664 { 0x02, "1" },
665 { 0x03, "2" },
666 { 0x04, "3" },
667 { 0x05, "4" },
668 { 0x06, "5" },
669 { 0x07, "6" },
670 { 0x08, "7" },
671 { 0x09, "8" },
672 { 0x0a, "9" },
673 { 0x0b, "0" },
674 { 0x0c, "minus" },
675 { 0x0d, "equal" },
676 { 0x0e, "backspace" },
678 { 0x0f, "tab" },
679 { 0x10, "q" },
680 { 0x11, "w" },
681 { 0x12, "e" },
682 { 0x13, "r" },
683 { 0x14, "t" },
684 { 0x15, "y" },
685 { 0x16, "u" },
686 { 0x17, "i" },
687 { 0x18, "o" },
688 { 0x19, "p" },
690 { 0x1c, "ret" },
692 { 0x1e, "a" },
693 { 0x1f, "s" },
694 { 0x20, "d" },
695 { 0x21, "f" },
696 { 0x22, "g" },
697 { 0x23, "h" },
698 { 0x24, "j" },
699 { 0x25, "k" },
700 { 0x26, "l" },
702 { 0x2c, "z" },
703 { 0x2d, "x" },
704 { 0x2e, "c" },
705 { 0x2f, "v" },
706 { 0x30, "b" },
707 { 0x31, "n" },
708 { 0x32, "m" },
710 { 0x39, "spc" },
711 { 0x3a, "caps_lock" },
712 { 0x3b, "f1" },
713 { 0x3c, "f2" },
714 { 0x3d, "f3" },
715 { 0x3e, "f4" },
716 { 0x3f, "f5" },
717 { 0x40, "f6" },
718 { 0x41, "f7" },
719 { 0x42, "f8" },
720 { 0x43, "f9" },
721 { 0x44, "f10" },
722 { 0x45, "num_lock" },
723 { 0x46, "scroll_lock" },
725 { 0xb5, "kp_divide" },
726 { 0x37, "kp_multiply" },
727 { 0x4a, "kp_substract" },
728 { 0x4e, "kp_add" },
729 { 0x9c, "kp_enter" },
730 { 0x53, "kp_decimal" },
732 { 0x52, "kp_0" },
733 { 0x4f, "kp_1" },
734 { 0x50, "kp_2" },
735 { 0x51, "kp_3" },
736 { 0x4b, "kp_4" },
737 { 0x4c, "kp_5" },
738 { 0x4d, "kp_6" },
739 { 0x47, "kp_7" },
740 { 0x48, "kp_8" },
741 { 0x49, "kp_9" },
743 { 0x56, "<" },
745 { 0x57, "f11" },
746 { 0x58, "f12" },
748 { 0xb7, "print" },
750 { 0xc7, "home" },
751 { 0xc9, "pgup" },
752 { 0xd1, "pgdn" },
753 { 0xcf, "end" },
755 { 0xcb, "left" },
756 { 0xc8, "up" },
757 { 0xd0, "down" },
758 { 0xcd, "right" },
760 { 0xd2, "insert" },
761 { 0xd3, "delete" },
762 { 0, NULL },
765 static int get_keycode(const char *key)
767 const KeyDef *p;
768 char *endp;
769 int ret;
771 for(p = key_defs; p->name != NULL; p++) {
772 if (!strcmp(key, p->name))
773 return p->keycode;
775 if (strstart(key, "0x", NULL)) {
776 ret = strtoul(key, &endp, 0);
777 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
778 return ret;
780 return -1;
783 static void do_send_key(const char *string)
785 char keybuf[16], *q;
786 uint8_t keycodes[16];
787 const char *p;
788 int nb_keycodes, keycode, i;
790 nb_keycodes = 0;
791 p = string;
792 while (*p != '\0') {
793 q = keybuf;
794 while (*p != '\0' && *p != '-') {
795 if ((q - keybuf) < sizeof(keybuf) - 1) {
796 *q++ = *p;
798 p++;
800 *q = '\0';
801 keycode = get_keycode(keybuf);
802 if (keycode < 0) {
803 term_printf("unknown key: '%s'\n", keybuf);
804 return;
806 keycodes[nb_keycodes++] = keycode;
807 if (*p == '\0')
808 break;
809 p++;
811 /* key down events */
812 for(i = 0; i < nb_keycodes; i++) {
813 keycode = keycodes[i];
814 if (keycode & 0x80)
815 kbd_put_keycode(0xe0);
816 kbd_put_keycode(keycode & 0x7f);
818 /* key up events */
819 for(i = nb_keycodes - 1; i >= 0; i--) {
820 keycode = keycodes[i];
821 if (keycode & 0x80)
822 kbd_put_keycode(0xe0);
823 kbd_put_keycode(keycode | 0x80);
827 static int mouse_button_state;
829 static void do_mouse_move(const char *dx_str, const char *dy_str,
830 const char *dz_str)
832 int dx, dy, dz;
833 dx = strtol(dx_str, NULL, 0);
834 dy = strtol(dy_str, NULL, 0);
835 dz = 0;
836 if (dz_str)
837 dz = strtol(dz_str, NULL, 0);
838 kbd_mouse_event(dx, dy, dz, mouse_button_state);
841 static void do_mouse_button(int button_state)
843 mouse_button_state = button_state;
844 kbd_mouse_event(0, 0, 0, mouse_button_state);
847 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
849 uint32_t val;
850 int suffix;
852 if (has_index) {
853 cpu_outb(NULL, addr & 0xffff, index & 0xff);
854 addr++;
856 addr &= 0xffff;
858 switch(size) {
859 default:
860 case 1:
861 val = cpu_inb(NULL, addr);
862 suffix = 'b';
863 break;
864 case 2:
865 val = cpu_inw(NULL, addr);
866 suffix = 'w';
867 break;
868 case 4:
869 val = cpu_inl(NULL, addr);
870 suffix = 'l';
871 break;
873 term_printf("port%c[0x%04x] = %#0*x\n",
874 suffix, addr, size * 2, val);
877 static void do_system_reset(void)
879 qemu_system_reset_request();
882 static void do_system_powerdown(void)
884 qemu_system_powerdown_request();
887 #if defined(TARGET_I386)
888 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
890 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
891 addr,
892 pte & mask,
893 pte & PG_GLOBAL_MASK ? 'G' : '-',
894 pte & PG_PSE_MASK ? 'P' : '-',
895 pte & PG_DIRTY_MASK ? 'D' : '-',
896 pte & PG_ACCESSED_MASK ? 'A' : '-',
897 pte & PG_PCD_MASK ? 'C' : '-',
898 pte & PG_PWT_MASK ? 'T' : '-',
899 pte & PG_USER_MASK ? 'U' : '-',
900 pte & PG_RW_MASK ? 'W' : '-');
903 static void tlb_info(void)
905 CPUState *env;
906 int l1, l2;
907 uint32_t pgd, pde, pte;
909 env = mon_get_cpu();
910 if (!env)
911 return;
913 if (!(env->cr[0] & CR0_PG_MASK)) {
914 term_printf("PG disabled\n");
915 return;
917 pgd = env->cr[3] & ~0xfff;
918 for(l1 = 0; l1 < 1024; l1++) {
919 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
920 pde = le32_to_cpu(pde);
921 if (pde & PG_PRESENT_MASK) {
922 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
923 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
924 } else {
925 for(l2 = 0; l2 < 1024; l2++) {
926 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
927 (uint8_t *)&pte, 4);
928 pte = le32_to_cpu(pte);
929 if (pte & PG_PRESENT_MASK) {
930 print_pte((l1 << 22) + (l2 << 12),
931 pte & ~PG_PSE_MASK,
932 ~0xfff);
940 static void mem_print(uint32_t *pstart, int *plast_prot,
941 uint32_t end, int prot)
943 int prot1;
944 prot1 = *plast_prot;
945 if (prot != prot1) {
946 if (*pstart != -1) {
947 term_printf("%08x-%08x %08x %c%c%c\n",
948 *pstart, end, end - *pstart,
949 prot1 & PG_USER_MASK ? 'u' : '-',
950 'r',
951 prot1 & PG_RW_MASK ? 'w' : '-');
953 if (prot != 0)
954 *pstart = end;
955 else
956 *pstart = -1;
957 *plast_prot = prot;
961 static void mem_info(void)
963 CPUState *env;
964 int l1, l2, prot, last_prot;
965 uint32_t pgd, pde, pte, start, end;
967 env = mon_get_cpu();
968 if (!env)
969 return;
971 if (!(env->cr[0] & CR0_PG_MASK)) {
972 term_printf("PG disabled\n");
973 return;
975 pgd = env->cr[3] & ~0xfff;
976 last_prot = 0;
977 start = -1;
978 for(l1 = 0; l1 < 1024; l1++) {
979 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
980 pde = le32_to_cpu(pde);
981 end = l1 << 22;
982 if (pde & PG_PRESENT_MASK) {
983 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
984 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
985 mem_print(&start, &last_prot, end, prot);
986 } else {
987 for(l2 = 0; l2 < 1024; l2++) {
988 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
989 (uint8_t *)&pte, 4);
990 pte = le32_to_cpu(pte);
991 end = (l1 << 22) + (l2 << 12);
992 if (pte & PG_PRESENT_MASK) {
993 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
994 } else {
995 prot = 0;
997 mem_print(&start, &last_prot, end, prot);
1000 } else {
1001 prot = 0;
1002 mem_print(&start, &last_prot, end, prot);
1006 #endif
1008 static void do_info_kqemu(void)
1010 #ifdef USE_KQEMU
1011 CPUState *env;
1012 int val;
1013 val = 0;
1014 env = mon_get_cpu();
1015 if (!env) {
1016 term_printf("No cpu initialized yet");
1017 return;
1019 val = env->kqemu_enabled;
1020 term_printf("kqemu support: ");
1021 switch(val) {
1022 default:
1023 case 0:
1024 term_printf("disabled\n");
1025 break;
1026 case 1:
1027 term_printf("enabled for user code\n");
1028 break;
1029 case 2:
1030 term_printf("enabled for user and kernel code\n");
1031 break;
1033 #else
1034 term_printf("kqemu support: not compiled\n");
1035 #endif
1038 #ifdef CONFIG_PROFILER
1040 int64_t kqemu_time;
1041 int64_t qemu_time;
1042 int64_t kqemu_exec_count;
1043 int64_t dev_time;
1044 int64_t kqemu_ret_int_count;
1045 int64_t kqemu_ret_excp_count;
1046 int64_t kqemu_ret_intr_count;
1048 static void do_info_profile(void)
1050 int64_t total;
1051 total = qemu_time;
1052 if (total == 0)
1053 total = 1;
1054 term_printf("async time %" PRId64 " (%0.3f)\n",
1055 dev_time, dev_time / (double)ticks_per_sec);
1056 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1057 qemu_time, qemu_time / (double)ticks_per_sec);
1058 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1059 kqemu_time, kqemu_time / (double)ticks_per_sec,
1060 kqemu_time / (double)total * 100.0,
1061 kqemu_exec_count,
1062 kqemu_ret_int_count,
1063 kqemu_ret_excp_count,
1064 kqemu_ret_intr_count);
1065 qemu_time = 0;
1066 kqemu_time = 0;
1067 kqemu_exec_count = 0;
1068 dev_time = 0;
1069 kqemu_ret_int_count = 0;
1070 kqemu_ret_excp_count = 0;
1071 kqemu_ret_intr_count = 0;
1072 #ifdef USE_KQEMU
1073 kqemu_record_dump();
1074 #endif
1076 #else
1077 static void do_info_profile(void)
1079 term_printf("Internal profiler not compiled\n");
1081 #endif
1083 /* Capture support */
1084 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1086 static void do_info_capture (void)
1088 int i;
1089 CaptureState *s;
1091 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1092 term_printf ("[%d]: ", i);
1093 s->ops.info (s->opaque);
1097 static void do_stop_capture (int n)
1099 int i;
1100 CaptureState *s;
1102 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1103 if (i == n) {
1104 s->ops.destroy (s->opaque);
1105 LIST_REMOVE (s, entries);
1106 qemu_free (s);
1107 return;
1112 #ifdef HAS_AUDIO
1113 int wav_start_capture (CaptureState *s, const char *path, int freq,
1114 int bits, int nchannels);
1116 static void do_wav_capture (const char *path,
1117 int has_freq, int freq,
1118 int has_bits, int bits,
1119 int has_channels, int nchannels)
1121 CaptureState *s;
1123 s = qemu_mallocz (sizeof (*s));
1124 if (!s) {
1125 term_printf ("Not enough memory to add wave capture\n");
1126 return;
1129 freq = has_freq ? freq : 44100;
1130 bits = has_bits ? bits : 16;
1131 nchannels = has_channels ? nchannels : 2;
1133 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1134 term_printf ("Faied to add wave capture\n");
1135 qemu_free (s);
1137 LIST_INSERT_HEAD (&capture_head, s, entries);
1139 #endif
1141 static term_cmd_t term_cmds[] = {
1142 { "help|?", "s?", do_help,
1143 "[cmd]", "show the help" },
1144 { "commit", "s", do_commit,
1145 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1146 { "info", "s?", do_info,
1147 "subcommand", "show various information about the system state" },
1148 { "q|quit", "", do_quit,
1149 "", "quit the emulator" },
1150 { "eject", "-fB", do_eject,
1151 "[-f] device", "eject a removable media (use -f to force it)" },
1152 { "change", "BF", do_change,
1153 "device filename", "change a removable media" },
1154 { "screendump", "F", do_screen_dump,
1155 "filename", "save screen into PPM image 'filename'" },
1156 { "log", "s", do_log,
1157 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1158 { "savevm", "F", do_savevm,
1159 "filename", "save the whole virtual machine state to 'filename'" },
1160 { "loadvm", "F", do_loadvm,
1161 "filename", "restore the whole virtual machine state from 'filename'" },
1162 { "stop", "", do_stop,
1163 "", "stop emulation", },
1164 { "c|cont", "", do_cont,
1165 "", "resume emulation", },
1166 #ifdef CONFIG_GDBSTUB
1167 { "gdbserver", "i?", do_gdbserver,
1168 "[port]", "start gdbserver session (default port=1234)", },
1169 #endif
1170 { "x", "/l", do_memory_dump,
1171 "/fmt addr", "virtual memory dump starting at 'addr'", },
1172 { "xp", "/l", do_physical_memory_dump,
1173 "/fmt addr", "physical memory dump starting at 'addr'", },
1174 { "p|print", "/l", do_print,
1175 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1176 { "i", "/ii.", do_ioport_read,
1177 "/fmt addr", "I/O port read" },
1179 { "sendkey", "s", do_send_key,
1180 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1181 { "system_reset", "", do_system_reset,
1182 "", "reset the system" },
1183 { "system_powerdown", "", do_system_powerdown,
1184 "", "send system power down event" },
1185 { "sum", "ii", do_sum,
1186 "addr size", "compute the checksum of a memory region" },
1187 { "usb_add", "s", do_usb_add,
1188 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1189 { "usb_del", "s", do_usb_del,
1190 "device", "remove USB device 'bus.addr'" },
1191 { "cpu", "i", do_cpu_set,
1192 "index", "set the default CPU" },
1193 { "mouse_move", "sss?", do_mouse_move,
1194 "dx dy [dz]", "send mouse move events" },
1195 { "mouse_button", "i", do_mouse_button,
1196 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1197 #ifdef HAS_AUDIO
1198 { "wavcapture", "si?i?i?", do_wav_capture,
1199 "path [frequency bits channels]",
1200 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1201 #endif
1202 { "stopcapture", "i", do_stop_capture,
1203 "capture index", "stop capture" },
1204 { NULL, NULL, },
1207 static term_cmd_t info_cmds[] = {
1208 { "version", "", do_info_version,
1209 "", "show the version of qemu" },
1210 { "network", "", do_info_network,
1211 "", "show the network state" },
1212 { "block", "", do_info_block,
1213 "", "show the block devices" },
1214 { "registers", "", do_info_registers,
1215 "", "show the cpu registers" },
1216 { "cpus", "", do_info_cpus,
1217 "", "show infos for each CPU" },
1218 { "history", "", do_info_history,
1219 "", "show the command line history", },
1220 { "irq", "", irq_info,
1221 "", "show the interrupts statistics (if available)", },
1222 { "pic", "", pic_info,
1223 "", "show i8259 (PIC) state", },
1224 { "pci", "", pci_info,
1225 "", "show PCI info", },
1226 #if defined(TARGET_I386)
1227 { "tlb", "", tlb_info,
1228 "", "show virtual to physical memory mappings", },
1229 { "mem", "", mem_info,
1230 "", "show the active virtual memory mappings", },
1231 #endif
1232 { "jit", "", do_info_jit,
1233 "", "show dynamic compiler info", },
1234 { "kqemu", "", do_info_kqemu,
1235 "", "show kqemu information", },
1236 { "usb", "", usb_info,
1237 "", "show guest USB devices", },
1238 { "usbhost", "", usb_host_info,
1239 "", "show host USB devices", },
1240 { "profile", "", do_info_profile,
1241 "", "show profiling information", },
1242 { "capture", "", do_info_capture,
1243 "show capture information" },
1244 { NULL, NULL, },
1247 /*******************************************************************/
1249 static const char *pch;
1250 static jmp_buf expr_env;
1252 #define MD_TLONG 0
1253 #define MD_I32 1
1255 typedef struct MonitorDef {
1256 const char *name;
1257 int offset;
1258 target_long (*get_value)(struct MonitorDef *md, int val);
1259 int type;
1260 } MonitorDef;
1262 #if defined(TARGET_I386)
1263 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1265 CPUState *env = mon_get_cpu();
1266 if (!env)
1267 return 0;
1268 return env->eip + env->segs[R_CS].base;
1270 #endif
1272 #if defined(TARGET_PPC)
1273 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1275 CPUState *env = mon_get_cpu();
1276 unsigned int u;
1277 int i;
1279 if (!env)
1280 return 0;
1282 u = 0;
1283 for (i = 0; i < 8; i++)
1284 u |= env->crf[i] << (32 - (4 * i));
1286 return u;
1289 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1291 CPUState *env = mon_get_cpu();
1292 if (!env)
1293 return 0;
1294 return (env->msr[MSR_POW] << MSR_POW) |
1295 (env->msr[MSR_ILE] << MSR_ILE) |
1296 (env->msr[MSR_EE] << MSR_EE) |
1297 (env->msr[MSR_PR] << MSR_PR) |
1298 (env->msr[MSR_FP] << MSR_FP) |
1299 (env->msr[MSR_ME] << MSR_ME) |
1300 (env->msr[MSR_FE0] << MSR_FE0) |
1301 (env->msr[MSR_SE] << MSR_SE) |
1302 (env->msr[MSR_BE] << MSR_BE) |
1303 (env->msr[MSR_FE1] << MSR_FE1) |
1304 (env->msr[MSR_IP] << MSR_IP) |
1305 (env->msr[MSR_IR] << MSR_IR) |
1306 (env->msr[MSR_DR] << MSR_DR) |
1307 (env->msr[MSR_RI] << MSR_RI) |
1308 (env->msr[MSR_LE] << MSR_LE);
1311 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1313 CPUState *env = mon_get_cpu();
1314 if (!env)
1315 return 0;
1316 return (env->xer[XER_SO] << XER_SO) |
1317 (env->xer[XER_OV] << XER_OV) |
1318 (env->xer[XER_CA] << XER_CA) |
1319 (env->xer[XER_BC] << XER_BC);
1322 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1324 CPUState *env = mon_get_cpu();
1325 if (!env)
1326 return 0;
1327 return cpu_ppc_load_decr(env);
1330 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1332 CPUState *env = mon_get_cpu();
1333 if (!env)
1334 return 0;
1335 return cpu_ppc_load_tbu(env);
1338 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1340 CPUState *env = mon_get_cpu();
1341 if (!env)
1342 return 0;
1343 return cpu_ppc_load_tbl(env);
1345 #endif
1347 #if defined(TARGET_SPARC)
1348 #ifndef TARGET_SPARC64
1349 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1351 CPUState *env = mon_get_cpu();
1352 if (!env)
1353 return 0;
1354 return GET_PSR(env);
1356 #endif
1358 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1360 CPUState *env = mon_get_cpu();
1361 if (!env)
1362 return 0;
1363 return env->regwptr[val];
1365 #endif
1367 static MonitorDef monitor_defs[] = {
1368 #ifdef TARGET_I386
1370 #define SEG(name, seg) \
1371 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1372 { name ".base", offsetof(CPUState, segs[seg].base) },\
1373 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1375 { "eax", offsetof(CPUState, regs[0]) },
1376 { "ecx", offsetof(CPUState, regs[1]) },
1377 { "edx", offsetof(CPUState, regs[2]) },
1378 { "ebx", offsetof(CPUState, regs[3]) },
1379 { "esp|sp", offsetof(CPUState, regs[4]) },
1380 { "ebp|fp", offsetof(CPUState, regs[5]) },
1381 { "esi", offsetof(CPUState, regs[6]) },
1382 { "edi", offsetof(CPUState, regs[7]) },
1383 #ifdef TARGET_X86_64
1384 { "r8", offsetof(CPUState, regs[8]) },
1385 { "r9", offsetof(CPUState, regs[9]) },
1386 { "r10", offsetof(CPUState, regs[10]) },
1387 { "r11", offsetof(CPUState, regs[11]) },
1388 { "r12", offsetof(CPUState, regs[12]) },
1389 { "r13", offsetof(CPUState, regs[13]) },
1390 { "r14", offsetof(CPUState, regs[14]) },
1391 { "r15", offsetof(CPUState, regs[15]) },
1392 #endif
1393 { "eflags", offsetof(CPUState, eflags) },
1394 { "eip", offsetof(CPUState, eip) },
1395 SEG("cs", R_CS)
1396 SEG("ds", R_DS)
1397 SEG("es", R_ES)
1398 SEG("ss", R_SS)
1399 SEG("fs", R_FS)
1400 SEG("gs", R_GS)
1401 { "pc", 0, monitor_get_pc, },
1402 #elif defined(TARGET_PPC)
1403 { "r0", offsetof(CPUState, gpr[0]) },
1404 { "r1", offsetof(CPUState, gpr[1]) },
1405 { "r2", offsetof(CPUState, gpr[2]) },
1406 { "r3", offsetof(CPUState, gpr[3]) },
1407 { "r4", offsetof(CPUState, gpr[4]) },
1408 { "r5", offsetof(CPUState, gpr[5]) },
1409 { "r6", offsetof(CPUState, gpr[6]) },
1410 { "r7", offsetof(CPUState, gpr[7]) },
1411 { "r8", offsetof(CPUState, gpr[8]) },
1412 { "r9", offsetof(CPUState, gpr[9]) },
1413 { "r10", offsetof(CPUState, gpr[10]) },
1414 { "r11", offsetof(CPUState, gpr[11]) },
1415 { "r12", offsetof(CPUState, gpr[12]) },
1416 { "r13", offsetof(CPUState, gpr[13]) },
1417 { "r14", offsetof(CPUState, gpr[14]) },
1418 { "r15", offsetof(CPUState, gpr[15]) },
1419 { "r16", offsetof(CPUState, gpr[16]) },
1420 { "r17", offsetof(CPUState, gpr[17]) },
1421 { "r18", offsetof(CPUState, gpr[18]) },
1422 { "r19", offsetof(CPUState, gpr[19]) },
1423 { "r20", offsetof(CPUState, gpr[20]) },
1424 { "r21", offsetof(CPUState, gpr[21]) },
1425 { "r22", offsetof(CPUState, gpr[22]) },
1426 { "r23", offsetof(CPUState, gpr[23]) },
1427 { "r24", offsetof(CPUState, gpr[24]) },
1428 { "r25", offsetof(CPUState, gpr[25]) },
1429 { "r26", offsetof(CPUState, gpr[26]) },
1430 { "r27", offsetof(CPUState, gpr[27]) },
1431 { "r28", offsetof(CPUState, gpr[28]) },
1432 { "r29", offsetof(CPUState, gpr[29]) },
1433 { "r30", offsetof(CPUState, gpr[30]) },
1434 { "r31", offsetof(CPUState, gpr[31]) },
1435 { "nip|pc", offsetof(CPUState, nip) },
1436 { "lr", offsetof(CPUState, lr) },
1437 { "ctr", offsetof(CPUState, ctr) },
1438 { "decr", 0, &monitor_get_decr, },
1439 { "ccr", 0, &monitor_get_ccr, },
1440 { "msr", 0, &monitor_get_msr, },
1441 { "xer", 0, &monitor_get_xer, },
1442 { "tbu", 0, &monitor_get_tbu, },
1443 { "tbl", 0, &monitor_get_tbl, },
1444 { "sdr1", offsetof(CPUState, sdr1) },
1445 { "sr0", offsetof(CPUState, sr[0]) },
1446 { "sr1", offsetof(CPUState, sr[1]) },
1447 { "sr2", offsetof(CPUState, sr[2]) },
1448 { "sr3", offsetof(CPUState, sr[3]) },
1449 { "sr4", offsetof(CPUState, sr[4]) },
1450 { "sr5", offsetof(CPUState, sr[5]) },
1451 { "sr6", offsetof(CPUState, sr[6]) },
1452 { "sr7", offsetof(CPUState, sr[7]) },
1453 { "sr8", offsetof(CPUState, sr[8]) },
1454 { "sr9", offsetof(CPUState, sr[9]) },
1455 { "sr10", offsetof(CPUState, sr[10]) },
1456 { "sr11", offsetof(CPUState, sr[11]) },
1457 { "sr12", offsetof(CPUState, sr[12]) },
1458 { "sr13", offsetof(CPUState, sr[13]) },
1459 { "sr14", offsetof(CPUState, sr[14]) },
1460 { "sr15", offsetof(CPUState, sr[15]) },
1461 /* Too lazy to put BATs and SPRs ... */
1462 #elif defined(TARGET_SPARC)
1463 { "g0", offsetof(CPUState, gregs[0]) },
1464 { "g1", offsetof(CPUState, gregs[1]) },
1465 { "g2", offsetof(CPUState, gregs[2]) },
1466 { "g3", offsetof(CPUState, gregs[3]) },
1467 { "g4", offsetof(CPUState, gregs[4]) },
1468 { "g5", offsetof(CPUState, gregs[5]) },
1469 { "g6", offsetof(CPUState, gregs[6]) },
1470 { "g7", offsetof(CPUState, gregs[7]) },
1471 { "o0", 0, monitor_get_reg },
1472 { "o1", 1, monitor_get_reg },
1473 { "o2", 2, monitor_get_reg },
1474 { "o3", 3, monitor_get_reg },
1475 { "o4", 4, monitor_get_reg },
1476 { "o5", 5, monitor_get_reg },
1477 { "o6", 6, monitor_get_reg },
1478 { "o7", 7, monitor_get_reg },
1479 { "l0", 8, monitor_get_reg },
1480 { "l1", 9, monitor_get_reg },
1481 { "l2", 10, monitor_get_reg },
1482 { "l3", 11, monitor_get_reg },
1483 { "l4", 12, monitor_get_reg },
1484 { "l5", 13, monitor_get_reg },
1485 { "l6", 14, monitor_get_reg },
1486 { "l7", 15, monitor_get_reg },
1487 { "i0", 16, monitor_get_reg },
1488 { "i1", 17, monitor_get_reg },
1489 { "i2", 18, monitor_get_reg },
1490 { "i3", 19, monitor_get_reg },
1491 { "i4", 20, monitor_get_reg },
1492 { "i5", 21, monitor_get_reg },
1493 { "i6", 22, monitor_get_reg },
1494 { "i7", 23, monitor_get_reg },
1495 { "pc", offsetof(CPUState, pc) },
1496 { "npc", offsetof(CPUState, npc) },
1497 { "y", offsetof(CPUState, y) },
1498 #ifndef TARGET_SPARC64
1499 { "psr", 0, &monitor_get_psr, },
1500 { "wim", offsetof(CPUState, wim) },
1501 #endif
1502 { "tbr", offsetof(CPUState, tbr) },
1503 { "fsr", offsetof(CPUState, fsr) },
1504 { "f0", offsetof(CPUState, fpr[0]) },
1505 { "f1", offsetof(CPUState, fpr[1]) },
1506 { "f2", offsetof(CPUState, fpr[2]) },
1507 { "f3", offsetof(CPUState, fpr[3]) },
1508 { "f4", offsetof(CPUState, fpr[4]) },
1509 { "f5", offsetof(CPUState, fpr[5]) },
1510 { "f6", offsetof(CPUState, fpr[6]) },
1511 { "f7", offsetof(CPUState, fpr[7]) },
1512 { "f8", offsetof(CPUState, fpr[8]) },
1513 { "f9", offsetof(CPUState, fpr[9]) },
1514 { "f10", offsetof(CPUState, fpr[10]) },
1515 { "f11", offsetof(CPUState, fpr[11]) },
1516 { "f12", offsetof(CPUState, fpr[12]) },
1517 { "f13", offsetof(CPUState, fpr[13]) },
1518 { "f14", offsetof(CPUState, fpr[14]) },
1519 { "f15", offsetof(CPUState, fpr[15]) },
1520 { "f16", offsetof(CPUState, fpr[16]) },
1521 { "f17", offsetof(CPUState, fpr[17]) },
1522 { "f18", offsetof(CPUState, fpr[18]) },
1523 { "f19", offsetof(CPUState, fpr[19]) },
1524 { "f20", offsetof(CPUState, fpr[20]) },
1525 { "f21", offsetof(CPUState, fpr[21]) },
1526 { "f22", offsetof(CPUState, fpr[22]) },
1527 { "f23", offsetof(CPUState, fpr[23]) },
1528 { "f24", offsetof(CPUState, fpr[24]) },
1529 { "f25", offsetof(CPUState, fpr[25]) },
1530 { "f26", offsetof(CPUState, fpr[26]) },
1531 { "f27", offsetof(CPUState, fpr[27]) },
1532 { "f28", offsetof(CPUState, fpr[28]) },
1533 { "f29", offsetof(CPUState, fpr[29]) },
1534 { "f30", offsetof(CPUState, fpr[30]) },
1535 { "f31", offsetof(CPUState, fpr[31]) },
1536 #ifdef TARGET_SPARC64
1537 { "f32", offsetof(CPUState, fpr[32]) },
1538 { "f34", offsetof(CPUState, fpr[34]) },
1539 { "f36", offsetof(CPUState, fpr[36]) },
1540 { "f38", offsetof(CPUState, fpr[38]) },
1541 { "f40", offsetof(CPUState, fpr[40]) },
1542 { "f42", offsetof(CPUState, fpr[42]) },
1543 { "f44", offsetof(CPUState, fpr[44]) },
1544 { "f46", offsetof(CPUState, fpr[46]) },
1545 { "f48", offsetof(CPUState, fpr[48]) },
1546 { "f50", offsetof(CPUState, fpr[50]) },
1547 { "f52", offsetof(CPUState, fpr[52]) },
1548 { "f54", offsetof(CPUState, fpr[54]) },
1549 { "f56", offsetof(CPUState, fpr[56]) },
1550 { "f58", offsetof(CPUState, fpr[58]) },
1551 { "f60", offsetof(CPUState, fpr[60]) },
1552 { "f62", offsetof(CPUState, fpr[62]) },
1553 { "asi", offsetof(CPUState, asi) },
1554 { "pstate", offsetof(CPUState, pstate) },
1555 { "cansave", offsetof(CPUState, cansave) },
1556 { "canrestore", offsetof(CPUState, canrestore) },
1557 { "otherwin", offsetof(CPUState, otherwin) },
1558 { "wstate", offsetof(CPUState, wstate) },
1559 { "cleanwin", offsetof(CPUState, cleanwin) },
1560 { "fprs", offsetof(CPUState, fprs) },
1561 #endif
1562 #endif
1563 { NULL },
1566 static void expr_error(const char *fmt)
1568 term_printf(fmt);
1569 term_printf("\n");
1570 longjmp(expr_env, 1);
1573 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1574 static int get_monitor_def(target_long *pval, const char *name)
1576 MonitorDef *md;
1577 void *ptr;
1579 for(md = monitor_defs; md->name != NULL; md++) {
1580 if (compare_cmd(name, md->name)) {
1581 if (md->get_value) {
1582 *pval = md->get_value(md, md->offset);
1583 } else {
1584 CPUState *env = mon_get_cpu();
1585 if (!env)
1586 return -2;
1587 ptr = (uint8_t *)env + md->offset;
1588 switch(md->type) {
1589 case MD_I32:
1590 *pval = *(int32_t *)ptr;
1591 break;
1592 case MD_TLONG:
1593 *pval = *(target_long *)ptr;
1594 break;
1595 default:
1596 *pval = 0;
1597 break;
1600 return 0;
1603 return -1;
1606 static void next(void)
1608 if (pch != '\0') {
1609 pch++;
1610 while (isspace(*pch))
1611 pch++;
1615 static target_long expr_sum(void);
1617 static target_long expr_unary(void)
1619 target_long n;
1620 char *p;
1621 int ret;
1623 switch(*pch) {
1624 case '+':
1625 next();
1626 n = expr_unary();
1627 break;
1628 case '-':
1629 next();
1630 n = -expr_unary();
1631 break;
1632 case '~':
1633 next();
1634 n = ~expr_unary();
1635 break;
1636 case '(':
1637 next();
1638 n = expr_sum();
1639 if (*pch != ')') {
1640 expr_error("')' expected");
1642 next();
1643 break;
1644 case '\'':
1645 pch++;
1646 if (*pch == '\0')
1647 expr_error("character constant expected");
1648 n = *pch;
1649 pch++;
1650 if (*pch != '\'')
1651 expr_error("missing terminating \' character");
1652 next();
1653 break;
1654 case '$':
1656 char buf[128], *q;
1658 pch++;
1659 q = buf;
1660 while ((*pch >= 'a' && *pch <= 'z') ||
1661 (*pch >= 'A' && *pch <= 'Z') ||
1662 (*pch >= '0' && *pch <= '9') ||
1663 *pch == '_' || *pch == '.') {
1664 if ((q - buf) < sizeof(buf) - 1)
1665 *q++ = *pch;
1666 pch++;
1668 while (isspace(*pch))
1669 pch++;
1670 *q = 0;
1671 ret = get_monitor_def(&n, buf);
1672 if (ret == -1)
1673 expr_error("unknown register");
1674 else if (ret == -2)
1675 expr_error("no cpu defined");
1677 break;
1678 case '\0':
1679 expr_error("unexpected end of expression");
1680 n = 0;
1681 break;
1682 default:
1683 #if TARGET_LONG_BITS == 64
1684 n = strtoull(pch, &p, 0);
1685 #else
1686 n = strtoul(pch, &p, 0);
1687 #endif
1688 if (pch == p) {
1689 expr_error("invalid char in expression");
1691 pch = p;
1692 while (isspace(*pch))
1693 pch++;
1694 break;
1696 return n;
1700 static target_long expr_prod(void)
1702 target_long val, val2;
1703 int op;
1705 val = expr_unary();
1706 for(;;) {
1707 op = *pch;
1708 if (op != '*' && op != '/' && op != '%')
1709 break;
1710 next();
1711 val2 = expr_unary();
1712 switch(op) {
1713 default:
1714 case '*':
1715 val *= val2;
1716 break;
1717 case '/':
1718 case '%':
1719 if (val2 == 0)
1720 expr_error("division by zero");
1721 if (op == '/')
1722 val /= val2;
1723 else
1724 val %= val2;
1725 break;
1728 return val;
1731 static target_long expr_logic(void)
1733 target_long val, val2;
1734 int op;
1736 val = expr_prod();
1737 for(;;) {
1738 op = *pch;
1739 if (op != '&' && op != '|' && op != '^')
1740 break;
1741 next();
1742 val2 = expr_prod();
1743 switch(op) {
1744 default:
1745 case '&':
1746 val &= val2;
1747 break;
1748 case '|':
1749 val |= val2;
1750 break;
1751 case '^':
1752 val ^= val2;
1753 break;
1756 return val;
1759 static target_long expr_sum(void)
1761 target_long val, val2;
1762 int op;
1764 val = expr_logic();
1765 for(;;) {
1766 op = *pch;
1767 if (op != '+' && op != '-')
1768 break;
1769 next();
1770 val2 = expr_logic();
1771 if (op == '+')
1772 val += val2;
1773 else
1774 val -= val2;
1776 return val;
1779 static int get_expr(target_long *pval, const char **pp)
1781 pch = *pp;
1782 if (setjmp(expr_env)) {
1783 *pp = pch;
1784 return -1;
1786 while (isspace(*pch))
1787 pch++;
1788 *pval = expr_sum();
1789 *pp = pch;
1790 return 0;
1793 static int get_str(char *buf, int buf_size, const char **pp)
1795 const char *p;
1796 char *q;
1797 int c;
1799 q = buf;
1800 p = *pp;
1801 while (isspace(*p))
1802 p++;
1803 if (*p == '\0') {
1804 fail:
1805 *q = '\0';
1806 *pp = p;
1807 return -1;
1809 if (*p == '\"') {
1810 p++;
1811 while (*p != '\0' && *p != '\"') {
1812 if (*p == '\\') {
1813 p++;
1814 c = *p++;
1815 switch(c) {
1816 case 'n':
1817 c = '\n';
1818 break;
1819 case 'r':
1820 c = '\r';
1821 break;
1822 case '\\':
1823 case '\'':
1824 case '\"':
1825 break;
1826 default:
1827 qemu_printf("unsupported escape code: '\\%c'\n", c);
1828 goto fail;
1830 if ((q - buf) < buf_size - 1) {
1831 *q++ = c;
1833 } else {
1834 if ((q - buf) < buf_size - 1) {
1835 *q++ = *p;
1837 p++;
1840 if (*p != '\"') {
1841 qemu_printf("unterminated string\n");
1842 goto fail;
1844 p++;
1845 } else {
1846 while (*p != '\0' && !isspace(*p)) {
1847 if ((q - buf) < buf_size - 1) {
1848 *q++ = *p;
1850 p++;
1853 *q = '\0';
1854 *pp = p;
1855 return 0;
1858 static int default_fmt_format = 'x';
1859 static int default_fmt_size = 4;
1861 #define MAX_ARGS 16
1863 static void monitor_handle_command(const char *cmdline)
1865 const char *p, *pstart, *typestr;
1866 char *q;
1867 int c, nb_args, len, i, has_arg;
1868 term_cmd_t *cmd;
1869 char cmdname[256];
1870 char buf[1024];
1871 void *str_allocated[MAX_ARGS];
1872 void *args[MAX_ARGS];
1874 #ifdef DEBUG
1875 term_printf("command='%s'\n", cmdline);
1876 #endif
1878 /* extract the command name */
1879 p = cmdline;
1880 q = cmdname;
1881 while (isspace(*p))
1882 p++;
1883 if (*p == '\0')
1884 return;
1885 pstart = p;
1886 while (*p != '\0' && *p != '/' && !isspace(*p))
1887 p++;
1888 len = p - pstart;
1889 if (len > sizeof(cmdname) - 1)
1890 len = sizeof(cmdname) - 1;
1891 memcpy(cmdname, pstart, len);
1892 cmdname[len] = '\0';
1894 /* find the command */
1895 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1896 if (compare_cmd(cmdname, cmd->name))
1897 goto found;
1899 term_printf("unknown command: '%s'\n", cmdname);
1900 return;
1901 found:
1903 for(i = 0; i < MAX_ARGS; i++)
1904 str_allocated[i] = NULL;
1906 /* parse the parameters */
1907 typestr = cmd->args_type;
1908 nb_args = 0;
1909 for(;;) {
1910 c = *typestr;
1911 if (c == '\0')
1912 break;
1913 typestr++;
1914 switch(c) {
1915 case 'F':
1916 case 'B':
1917 case 's':
1919 int ret;
1920 char *str;
1922 while (isspace(*p))
1923 p++;
1924 if (*typestr == '?') {
1925 typestr++;
1926 if (*p == '\0') {
1927 /* no optional string: NULL argument */
1928 str = NULL;
1929 goto add_str;
1932 ret = get_str(buf, sizeof(buf), &p);
1933 if (ret < 0) {
1934 switch(c) {
1935 case 'F':
1936 term_printf("%s: filename expected\n", cmdname);
1937 break;
1938 case 'B':
1939 term_printf("%s: block device name expected\n", cmdname);
1940 break;
1941 default:
1942 term_printf("%s: string expected\n", cmdname);
1943 break;
1945 goto fail;
1947 str = qemu_malloc(strlen(buf) + 1);
1948 strcpy(str, buf);
1949 str_allocated[nb_args] = str;
1950 add_str:
1951 if (nb_args >= MAX_ARGS) {
1952 error_args:
1953 term_printf("%s: too many arguments\n", cmdname);
1954 goto fail;
1956 args[nb_args++] = str;
1958 break;
1959 case '/':
1961 int count, format, size;
1963 while (isspace(*p))
1964 p++;
1965 if (*p == '/') {
1966 /* format found */
1967 p++;
1968 count = 1;
1969 if (isdigit(*p)) {
1970 count = 0;
1971 while (isdigit(*p)) {
1972 count = count * 10 + (*p - '0');
1973 p++;
1976 size = -1;
1977 format = -1;
1978 for(;;) {
1979 switch(*p) {
1980 case 'o':
1981 case 'd':
1982 case 'u':
1983 case 'x':
1984 case 'i':
1985 case 'c':
1986 format = *p++;
1987 break;
1988 case 'b':
1989 size = 1;
1990 p++;
1991 break;
1992 case 'h':
1993 size = 2;
1994 p++;
1995 break;
1996 case 'w':
1997 size = 4;
1998 p++;
1999 break;
2000 case 'g':
2001 case 'L':
2002 size = 8;
2003 p++;
2004 break;
2005 default:
2006 goto next;
2009 next:
2010 if (*p != '\0' && !isspace(*p)) {
2011 term_printf("invalid char in format: '%c'\n", *p);
2012 goto fail;
2014 if (format < 0)
2015 format = default_fmt_format;
2016 if (format != 'i') {
2017 /* for 'i', not specifying a size gives -1 as size */
2018 if (size < 0)
2019 size = default_fmt_size;
2021 default_fmt_size = size;
2022 default_fmt_format = format;
2023 } else {
2024 count = 1;
2025 format = default_fmt_format;
2026 if (format != 'i') {
2027 size = default_fmt_size;
2028 } else {
2029 size = -1;
2032 if (nb_args + 3 > MAX_ARGS)
2033 goto error_args;
2034 args[nb_args++] = (void*)count;
2035 args[nb_args++] = (void*)format;
2036 args[nb_args++] = (void*)size;
2038 break;
2039 case 'i':
2040 case 'l':
2042 target_long val;
2043 while (isspace(*p))
2044 p++;
2045 if (*typestr == '?' || *typestr == '.') {
2046 if (*typestr == '?') {
2047 if (*p == '\0')
2048 has_arg = 0;
2049 else
2050 has_arg = 1;
2051 } else {
2052 if (*p == '.') {
2053 p++;
2054 while (isspace(*p))
2055 p++;
2056 has_arg = 1;
2057 } else {
2058 has_arg = 0;
2061 typestr++;
2062 if (nb_args >= MAX_ARGS)
2063 goto error_args;
2064 args[nb_args++] = (void *)has_arg;
2065 if (!has_arg) {
2066 if (nb_args >= MAX_ARGS)
2067 goto error_args;
2068 val = -1;
2069 goto add_num;
2072 if (get_expr(&val, &p))
2073 goto fail;
2074 add_num:
2075 if (c == 'i') {
2076 if (nb_args >= MAX_ARGS)
2077 goto error_args;
2078 args[nb_args++] = (void *)(int)val;
2079 } else {
2080 if ((nb_args + 1) >= MAX_ARGS)
2081 goto error_args;
2082 #if TARGET_LONG_BITS == 64
2083 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2084 #else
2085 args[nb_args++] = (void *)0;
2086 #endif
2087 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2090 break;
2091 case '-':
2093 int has_option;
2094 /* option */
2096 c = *typestr++;
2097 if (c == '\0')
2098 goto bad_type;
2099 while (isspace(*p))
2100 p++;
2101 has_option = 0;
2102 if (*p == '-') {
2103 p++;
2104 if (*p != c) {
2105 term_printf("%s: unsupported option -%c\n",
2106 cmdname, *p);
2107 goto fail;
2109 p++;
2110 has_option = 1;
2112 if (nb_args >= MAX_ARGS)
2113 goto error_args;
2114 args[nb_args++] = (void *)has_option;
2116 break;
2117 default:
2118 bad_type:
2119 term_printf("%s: unknown type '%c'\n", cmdname, c);
2120 goto fail;
2123 /* check that all arguments were parsed */
2124 while (isspace(*p))
2125 p++;
2126 if (*p != '\0') {
2127 term_printf("%s: extraneous characters at the end of line\n",
2128 cmdname);
2129 goto fail;
2132 switch(nb_args) {
2133 case 0:
2134 cmd->handler();
2135 break;
2136 case 1:
2137 cmd->handler(args[0]);
2138 break;
2139 case 2:
2140 cmd->handler(args[0], args[1]);
2141 break;
2142 case 3:
2143 cmd->handler(args[0], args[1], args[2]);
2144 break;
2145 case 4:
2146 cmd->handler(args[0], args[1], args[2], args[3]);
2147 break;
2148 case 5:
2149 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2150 break;
2151 case 6:
2152 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2153 break;
2154 case 7:
2155 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2156 break;
2157 default:
2158 term_printf("unsupported number of arguments: %d\n", nb_args);
2159 goto fail;
2161 fail:
2162 for(i = 0; i < MAX_ARGS; i++)
2163 qemu_free(str_allocated[i]);
2164 return;
2167 static void cmd_completion(const char *name, const char *list)
2169 const char *p, *pstart;
2170 char cmd[128];
2171 int len;
2173 p = list;
2174 for(;;) {
2175 pstart = p;
2176 p = strchr(p, '|');
2177 if (!p)
2178 p = pstart + strlen(pstart);
2179 len = p - pstart;
2180 if (len > sizeof(cmd) - 2)
2181 len = sizeof(cmd) - 2;
2182 memcpy(cmd, pstart, len);
2183 cmd[len] = '\0';
2184 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2185 add_completion(cmd);
2187 if (*p == '\0')
2188 break;
2189 p++;
2193 static void file_completion(const char *input)
2195 DIR *ffs;
2196 struct dirent *d;
2197 char path[1024];
2198 char file[1024], file_prefix[1024];
2199 int input_path_len;
2200 const char *p;
2202 p = strrchr(input, '/');
2203 if (!p) {
2204 input_path_len = 0;
2205 pstrcpy(file_prefix, sizeof(file_prefix), input);
2206 strcpy(path, ".");
2207 } else {
2208 input_path_len = p - input + 1;
2209 memcpy(path, input, input_path_len);
2210 if (input_path_len > sizeof(path) - 1)
2211 input_path_len = sizeof(path) - 1;
2212 path[input_path_len] = '\0';
2213 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2215 #ifdef DEBUG_COMPLETION
2216 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2217 #endif
2218 ffs = opendir(path);
2219 if (!ffs)
2220 return;
2221 for(;;) {
2222 struct stat sb;
2223 d = readdir(ffs);
2224 if (!d)
2225 break;
2226 if (strstart(d->d_name, file_prefix, NULL)) {
2227 memcpy(file, input, input_path_len);
2228 strcpy(file + input_path_len, d->d_name);
2229 /* stat the file to find out if it's a directory.
2230 * In that case add a slash to speed up typing long paths
2232 stat(file, &sb);
2233 if(S_ISDIR(sb.st_mode))
2234 strcat(file, "/");
2235 add_completion(file);
2238 closedir(ffs);
2241 static void block_completion_it(void *opaque, const char *name)
2243 const char *input = opaque;
2245 if (input[0] == '\0' ||
2246 !strncmp(name, (char *)input, strlen(input))) {
2247 add_completion(name);
2251 /* NOTE: this parser is an approximate form of the real command parser */
2252 static void parse_cmdline(const char *cmdline,
2253 int *pnb_args, char **args)
2255 const char *p;
2256 int nb_args, ret;
2257 char buf[1024];
2259 p = cmdline;
2260 nb_args = 0;
2261 for(;;) {
2262 while (isspace(*p))
2263 p++;
2264 if (*p == '\0')
2265 break;
2266 if (nb_args >= MAX_ARGS)
2267 break;
2268 ret = get_str(buf, sizeof(buf), &p);
2269 args[nb_args] = qemu_strdup(buf);
2270 nb_args++;
2271 if (ret < 0)
2272 break;
2274 *pnb_args = nb_args;
2277 void readline_find_completion(const char *cmdline)
2279 const char *cmdname;
2280 char *args[MAX_ARGS];
2281 int nb_args, i, len;
2282 const char *ptype, *str;
2283 term_cmd_t *cmd;
2284 const KeyDef *key;
2286 parse_cmdline(cmdline, &nb_args, args);
2287 #ifdef DEBUG_COMPLETION
2288 for(i = 0; i < nb_args; i++) {
2289 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2291 #endif
2293 /* if the line ends with a space, it means we want to complete the
2294 next arg */
2295 len = strlen(cmdline);
2296 if (len > 0 && isspace(cmdline[len - 1])) {
2297 if (nb_args >= MAX_ARGS)
2298 return;
2299 args[nb_args++] = qemu_strdup("");
2301 if (nb_args <= 1) {
2302 /* command completion */
2303 if (nb_args == 0)
2304 cmdname = "";
2305 else
2306 cmdname = args[0];
2307 completion_index = strlen(cmdname);
2308 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2309 cmd_completion(cmdname, cmd->name);
2311 } else {
2312 /* find the command */
2313 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2314 if (compare_cmd(args[0], cmd->name))
2315 goto found;
2317 return;
2318 found:
2319 ptype = cmd->args_type;
2320 for(i = 0; i < nb_args - 2; i++) {
2321 if (*ptype != '\0') {
2322 ptype++;
2323 while (*ptype == '?')
2324 ptype++;
2327 str = args[nb_args - 1];
2328 switch(*ptype) {
2329 case 'F':
2330 /* file completion */
2331 completion_index = strlen(str);
2332 file_completion(str);
2333 break;
2334 case 'B':
2335 /* block device name completion */
2336 completion_index = strlen(str);
2337 bdrv_iterate(block_completion_it, (void *)str);
2338 break;
2339 case 's':
2340 /* XXX: more generic ? */
2341 if (!strcmp(cmd->name, "info")) {
2342 completion_index = strlen(str);
2343 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2344 cmd_completion(str, cmd->name);
2346 } else if (!strcmp(cmd->name, "sendkey")) {
2347 completion_index = strlen(str);
2348 for(key = key_defs; key->name != NULL; key++) {
2349 cmd_completion(str, key->name);
2352 break;
2353 default:
2354 break;
2357 for(i = 0; i < nb_args; i++)
2358 qemu_free(args[i]);
2361 static int term_can_read(void *opaque)
2363 return 128;
2366 static void term_read(void *opaque, const uint8_t *buf, int size)
2368 int i;
2369 for(i = 0; i < size; i++)
2370 readline_handle_byte(buf[i]);
2373 static void monitor_start_input(void);
2375 static void monitor_handle_command1(void *opaque, const char *cmdline)
2377 monitor_handle_command(cmdline);
2378 monitor_start_input();
2381 static void monitor_start_input(void)
2383 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2386 void monitor_init(CharDriverState *hd, int show_banner)
2388 monitor_hd = hd;
2389 if (show_banner) {
2390 term_printf("QEMU %s monitor - type 'help' for more information\n",
2391 QEMU_VERSION);
2393 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2394 monitor_start_input();
2397 /* XXX: use threads ? */
2398 /* modal monitor readline */
2399 static int monitor_readline_started;
2400 static char *monitor_readline_buf;
2401 static int monitor_readline_buf_size;
2403 static void monitor_readline_cb(void *opaque, const char *input)
2405 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2406 monitor_readline_started = 0;
2409 void monitor_readline(const char *prompt, int is_password,
2410 char *buf, int buf_size)
2412 if (is_password) {
2413 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2415 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2416 monitor_readline_buf = buf;
2417 monitor_readline_buf_size = buf_size;
2418 monitor_readline_started = 1;
2419 while (monitor_readline_started) {
2420 main_loop_wait(10);