Replace tabs by 8 spaces. No code change, by Herve Poussineau.
[qemu/dscho.git] / monitor.c
blob3b9408b557e055a69e20383e5dcf99841a2c4333
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 #define MAX_MON 4
58 static CharDriverState *monitor_hd[MAX_MON];
59 static int hide_banner;
61 static term_cmd_t term_cmds[];
62 static term_cmd_t info_cmds[];
64 static char term_outbuf[1024];
65 static int term_outbuf_index;
67 static void monitor_start_input(void);
69 CPUState *mon_cpu = NULL;
71 void term_flush(void)
73 int i;
74 if (term_outbuf_index > 0) {
75 for (i = 0; i < MAX_MON; i++)
76 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
77 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
78 term_outbuf_index = 0;
82 /* flush at every end of line or if the buffer is full */
83 void term_puts(const char *str)
85 int c;
86 for(;;) {
87 c = *str++;
88 if (c == '\0')
89 break;
90 if (c == '\n')
91 term_outbuf[term_outbuf_index++] = '\r';
92 term_outbuf[term_outbuf_index++] = c;
93 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
94 c == '\n')
95 term_flush();
99 void term_vprintf(const char *fmt, va_list ap)
101 char buf[4096];
102 vsnprintf(buf, sizeof(buf), fmt, ap);
103 term_puts(buf);
106 void term_printf(const char *fmt, ...)
108 va_list ap;
109 va_start(ap, fmt);
110 term_vprintf(fmt, ap);
111 va_end(ap);
114 void term_print_filename(const char *filename)
116 int i;
118 for (i = 0; filename[i]; i++) {
119 switch (filename[i]) {
120 case ' ':
121 case '"':
122 case '\\':
123 term_printf("\\%c", filename[i]);
124 break;
125 case '\t':
126 term_printf("\\t");
127 break;
128 case '\r':
129 term_printf("\\r");
130 break;
131 case '\n':
132 term_printf("\\n");
133 break;
134 default:
135 term_printf("%c", filename[i]);
136 break;
141 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
143 va_list ap;
144 va_start(ap, fmt);
145 term_vprintf(fmt, ap);
146 va_end(ap);
147 return 0;
150 static int compare_cmd(const char *name, const char *list)
152 const char *p, *pstart;
153 int len;
154 len = strlen(name);
155 p = list;
156 for(;;) {
157 pstart = p;
158 p = strchr(p, '|');
159 if (!p)
160 p = pstart + strlen(pstart);
161 if ((p - pstart) == len && !memcmp(pstart, name, len))
162 return 1;
163 if (*p == '\0')
164 break;
165 p++;
167 return 0;
170 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
172 term_cmd_t *cmd;
174 for(cmd = cmds; cmd->name != NULL; cmd++) {
175 if (!name || !strcmp(name, cmd->name))
176 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
180 static void help_cmd(const char *name)
182 if (name && !strcmp(name, "info")) {
183 help_cmd1(info_cmds, "info ", NULL);
184 } else {
185 help_cmd1(term_cmds, "", name);
186 if (name && !strcmp(name, "log")) {
187 CPULogItem *item;
188 term_printf("Log items (comma separated):\n");
189 term_printf("%-10s %s\n", "none", "remove all logs");
190 for(item = cpu_log_items; item->mask != 0; item++) {
191 term_printf("%-10s %s\n", item->name, item->help);
197 static void do_help(const char *name)
199 help_cmd(name);
202 static void do_commit(const char *device)
204 int i, all_devices;
206 all_devices = !strcmp(device, "all");
207 for (i = 0; i < MAX_DISKS; i++) {
208 if (bs_table[i]) {
209 if (all_devices ||
210 !strcmp(bdrv_get_device_name(bs_table[i]), device))
211 bdrv_commit(bs_table[i]);
214 if (mtd_bdrv)
215 if (all_devices || !strcmp(bdrv_get_device_name(mtd_bdrv), device))
216 bdrv_commit(mtd_bdrv);
219 static void do_info(const char *item)
221 term_cmd_t *cmd;
223 if (!item)
224 goto help;
225 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
226 if (compare_cmd(item, cmd->name))
227 goto found;
229 help:
230 help_cmd("info");
231 return;
232 found:
233 cmd->handler();
236 static void do_info_version(void)
238 term_printf("%s\n", QEMU_VERSION);
241 static void do_info_name(void)
243 if (qemu_name)
244 term_printf("%s\n", qemu_name);
247 static void do_info_block(void)
249 bdrv_info();
252 /* get the current CPU defined by the user */
253 int mon_set_cpu(int cpu_index)
255 CPUState *env;
257 for(env = first_cpu; env != NULL; env = env->next_cpu) {
258 if (env->cpu_index == cpu_index) {
259 mon_cpu = env;
260 return 0;
263 return -1;
266 CPUState *mon_get_cpu(void)
268 if (!mon_cpu) {
269 mon_set_cpu(0);
271 return mon_cpu;
274 static void do_info_registers(void)
276 CPUState *env;
277 env = mon_get_cpu();
278 if (!env)
279 return;
280 #ifdef TARGET_I386
281 cpu_dump_state(env, NULL, monitor_fprintf,
282 X86_DUMP_FPU);
283 #else
284 cpu_dump_state(env, NULL, monitor_fprintf,
286 #endif
289 static void do_info_cpus(void)
291 CPUState *env;
293 /* just to set the default cpu if not already done */
294 mon_get_cpu();
296 for(env = first_cpu; env != NULL; env = env->next_cpu) {
297 term_printf("%c CPU #%d:",
298 (env == mon_cpu) ? '*' : ' ',
299 env->cpu_index);
300 #if defined(TARGET_I386)
301 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
302 if (env->hflags & HF_HALTED_MASK)
303 term_printf(" (halted)");
304 #elif defined(TARGET_PPC)
305 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
306 if (env->halted)
307 term_printf(" (halted)");
308 #elif defined(TARGET_SPARC)
309 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
310 if (env->halted)
311 term_printf(" (halted)");
312 #endif
313 term_printf("\n");
317 static void do_cpu_set(int index)
319 if (mon_set_cpu(index) < 0)
320 term_printf("Invalid CPU index\n");
323 static void do_info_jit(void)
325 dump_exec_info(NULL, monitor_fprintf);
328 static void do_info_history (void)
330 int i;
331 const char *str;
333 i = 0;
334 for(;;) {
335 str = readline_get_history(i);
336 if (!str)
337 break;
338 term_printf("%d: '%s'\n", i, str);
339 i++;
343 #if defined(TARGET_PPC)
344 /* XXX: not implemented in other targets */
345 static void do_info_cpu_stats (void)
347 CPUState *env;
349 env = mon_get_cpu();
350 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
352 #endif
354 static void do_quit(void)
356 exit(0);
359 static int eject_device(BlockDriverState *bs, int force)
361 if (bdrv_is_inserted(bs)) {
362 if (!force) {
363 if (!bdrv_is_removable(bs)) {
364 term_printf("device is not removable\n");
365 return -1;
367 if (bdrv_is_locked(bs)) {
368 term_printf("device is locked\n");
369 return -1;
372 bdrv_close(bs);
374 return 0;
377 static void do_eject(int force, const char *filename)
379 BlockDriverState *bs;
381 bs = bdrv_find(filename);
382 if (!bs) {
383 term_printf("device not found\n");
384 return;
386 eject_device(bs, force);
389 static void do_change_block(const char *device, const char *filename)
391 BlockDriverState *bs;
393 bs = bdrv_find(device);
394 if (!bs) {
395 term_printf("device not found\n");
396 return;
398 if (eject_device(bs, 0) < 0)
399 return;
400 bdrv_open(bs, filename, 0);
401 qemu_key_check(bs, filename);
404 static void do_change_vnc(const char *target)
406 if (strcmp(target, "passwd") == 0 ||
407 strcmp(target, "password") == 0) {
408 char password[9];
409 monitor_readline("Password: ", 1, password, sizeof(password)-1);
410 password[sizeof(password)-1] = '\0';
411 if (vnc_display_password(NULL, password) < 0)
412 term_printf("could not set VNC server password\n");
413 } else {
414 if (vnc_display_open(NULL, target) < 0)
415 term_printf("could not start VNC server on %s\n", target);
419 static void do_change(const char *device, const char *target)
421 if (strcmp(device, "vnc") == 0) {
422 do_change_vnc(target);
423 } else {
424 do_change_block(device, target);
428 static void do_screen_dump(const char *filename)
430 vga_hw_screen_dump(filename);
433 static void do_logfile(const char *filename)
435 cpu_set_log_filename(filename);
438 static void do_log(const char *items)
440 int mask;
442 if (!strcmp(items, "none")) {
443 mask = 0;
444 } else {
445 mask = cpu_str_to_log_mask(items);
446 if (!mask) {
447 help_cmd("log");
448 return;
451 cpu_set_log(mask);
454 static void do_stop(void)
456 vm_stop(EXCP_INTERRUPT);
459 static void do_cont(void)
461 vm_start();
464 #ifdef CONFIG_GDBSTUB
465 static void do_gdbserver(const char *port)
467 if (!port)
468 port = DEFAULT_GDBSTUB_PORT;
469 if (gdbserver_start(port) < 0) {
470 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
471 } else {
472 qemu_printf("Waiting gdb connection on port '%s'\n", port);
475 #endif
477 static void term_printc(int c)
479 term_printf("'");
480 switch(c) {
481 case '\'':
482 term_printf("\\'");
483 break;
484 case '\\':
485 term_printf("\\\\");
486 break;
487 case '\n':
488 term_printf("\\n");
489 break;
490 case '\r':
491 term_printf("\\r");
492 break;
493 default:
494 if (c >= 32 && c <= 126) {
495 term_printf("%c", c);
496 } else {
497 term_printf("\\x%02x", c);
499 break;
501 term_printf("'");
504 static void memory_dump(int count, int format, int wsize,
505 target_ulong addr, int is_physical)
507 CPUState *env;
508 int nb_per_line, l, line_size, i, max_digits, len;
509 uint8_t buf[16];
510 uint64_t v;
512 if (format == 'i') {
513 int flags;
514 flags = 0;
515 env = mon_get_cpu();
516 if (!env && !is_physical)
517 return;
518 #ifdef TARGET_I386
519 if (wsize == 2) {
520 flags = 1;
521 } else if (wsize == 4) {
522 flags = 0;
523 } else {
524 /* as default we use the current CS size */
525 flags = 0;
526 if (env) {
527 #ifdef TARGET_X86_64
528 if ((env->efer & MSR_EFER_LMA) &&
529 (env->segs[R_CS].flags & DESC_L_MASK))
530 flags = 2;
531 else
532 #endif
533 if (!(env->segs[R_CS].flags & DESC_B_MASK))
534 flags = 1;
537 #endif
538 monitor_disas(env, addr, count, is_physical, flags);
539 return;
542 len = wsize * count;
543 if (wsize == 1)
544 line_size = 8;
545 else
546 line_size = 16;
547 nb_per_line = line_size / wsize;
548 max_digits = 0;
550 switch(format) {
551 case 'o':
552 max_digits = (wsize * 8 + 2) / 3;
553 break;
554 default:
555 case 'x':
556 max_digits = (wsize * 8) / 4;
557 break;
558 case 'u':
559 case 'd':
560 max_digits = (wsize * 8 * 10 + 32) / 33;
561 break;
562 case 'c':
563 wsize = 1;
564 break;
567 while (len > 0) {
568 term_printf(TARGET_FMT_lx ":", addr);
569 l = len;
570 if (l > line_size)
571 l = line_size;
572 if (is_physical) {
573 cpu_physical_memory_rw(addr, buf, l, 0);
574 } else {
575 env = mon_get_cpu();
576 if (!env)
577 break;
578 cpu_memory_rw_debug(env, addr, buf, l, 0);
580 i = 0;
581 while (i < l) {
582 switch(wsize) {
583 default:
584 case 1:
585 v = ldub_raw(buf + i);
586 break;
587 case 2:
588 v = lduw_raw(buf + i);
589 break;
590 case 4:
591 v = (uint32_t)ldl_raw(buf + i);
592 break;
593 case 8:
594 v = ldq_raw(buf + i);
595 break;
597 term_printf(" ");
598 switch(format) {
599 case 'o':
600 term_printf("%#*" PRIo64, max_digits, v);
601 break;
602 case 'x':
603 term_printf("0x%0*" PRIx64, max_digits, v);
604 break;
605 case 'u':
606 term_printf("%*" PRIu64, max_digits, v);
607 break;
608 case 'd':
609 term_printf("%*" PRId64, max_digits, v);
610 break;
611 case 'c':
612 term_printc(v);
613 break;
615 i += wsize;
617 term_printf("\n");
618 addr += l;
619 len -= l;
623 #if TARGET_LONG_BITS == 64
624 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
625 #else
626 #define GET_TLONG(h, l) (l)
627 #endif
629 static void do_memory_dump(int count, int format, int size,
630 uint32_t addrh, uint32_t addrl)
632 target_long addr = GET_TLONG(addrh, addrl);
633 memory_dump(count, format, size, addr, 0);
636 static void do_physical_memory_dump(int count, int format, int size,
637 uint32_t addrh, uint32_t addrl)
640 target_long addr = GET_TLONG(addrh, addrl);
641 memory_dump(count, format, size, addr, 1);
644 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
646 target_long val = GET_TLONG(valh, vall);
647 #if TARGET_LONG_BITS == 32
648 switch(format) {
649 case 'o':
650 term_printf("%#o", val);
651 break;
652 case 'x':
653 term_printf("%#x", val);
654 break;
655 case 'u':
656 term_printf("%u", val);
657 break;
658 default:
659 case 'd':
660 term_printf("%d", val);
661 break;
662 case 'c':
663 term_printc(val);
664 break;
666 #else
667 switch(format) {
668 case 'o':
669 term_printf("%#" PRIo64, val);
670 break;
671 case 'x':
672 term_printf("%#" PRIx64, val);
673 break;
674 case 'u':
675 term_printf("%" PRIu64, val);
676 break;
677 default:
678 case 'd':
679 term_printf("%" PRId64, val);
680 break;
681 case 'c':
682 term_printc(val);
683 break;
685 #endif
686 term_printf("\n");
689 static void do_memory_save(unsigned int valh, unsigned int vall,
690 uint32_t size, const char *filename)
692 FILE *f;
693 target_long addr = GET_TLONG(valh, vall);
694 uint32_t l;
695 CPUState *env;
696 uint8_t buf[1024];
698 env = mon_get_cpu();
699 if (!env)
700 return;
702 f = fopen(filename, "wb");
703 if (!f) {
704 term_printf("could not open '%s'\n", filename);
705 return;
707 while (size != 0) {
708 l = sizeof(buf);
709 if (l > size)
710 l = size;
711 cpu_memory_rw_debug(env, addr, buf, l, 0);
712 fwrite(buf, 1, l, f);
713 addr += l;
714 size -= l;
716 fclose(f);
719 static void do_sum(uint32_t start, uint32_t size)
721 uint32_t addr;
722 uint8_t buf[1];
723 uint16_t sum;
725 sum = 0;
726 for(addr = start; addr < (start + size); addr++) {
727 cpu_physical_memory_rw(addr, buf, 1, 0);
728 /* BSD sum algorithm ('sum' Unix command) */
729 sum = (sum >> 1) | (sum << 15);
730 sum += buf[0];
732 term_printf("%05d\n", sum);
735 typedef struct {
736 int keycode;
737 const char *name;
738 } KeyDef;
740 static const KeyDef key_defs[] = {
741 { 0x2a, "shift" },
742 { 0x36, "shift_r" },
744 { 0x38, "alt" },
745 { 0xb8, "alt_r" },
746 { 0x1d, "ctrl" },
747 { 0x9d, "ctrl_r" },
749 { 0xdd, "menu" },
751 { 0x01, "esc" },
753 { 0x02, "1" },
754 { 0x03, "2" },
755 { 0x04, "3" },
756 { 0x05, "4" },
757 { 0x06, "5" },
758 { 0x07, "6" },
759 { 0x08, "7" },
760 { 0x09, "8" },
761 { 0x0a, "9" },
762 { 0x0b, "0" },
763 { 0x0c, "minus" },
764 { 0x0d, "equal" },
765 { 0x0e, "backspace" },
767 { 0x0f, "tab" },
768 { 0x10, "q" },
769 { 0x11, "w" },
770 { 0x12, "e" },
771 { 0x13, "r" },
772 { 0x14, "t" },
773 { 0x15, "y" },
774 { 0x16, "u" },
775 { 0x17, "i" },
776 { 0x18, "o" },
777 { 0x19, "p" },
779 { 0x1c, "ret" },
781 { 0x1e, "a" },
782 { 0x1f, "s" },
783 { 0x20, "d" },
784 { 0x21, "f" },
785 { 0x22, "g" },
786 { 0x23, "h" },
787 { 0x24, "j" },
788 { 0x25, "k" },
789 { 0x26, "l" },
791 { 0x2c, "z" },
792 { 0x2d, "x" },
793 { 0x2e, "c" },
794 { 0x2f, "v" },
795 { 0x30, "b" },
796 { 0x31, "n" },
797 { 0x32, "m" },
799 { 0x39, "spc" },
800 { 0x3a, "caps_lock" },
801 { 0x3b, "f1" },
802 { 0x3c, "f2" },
803 { 0x3d, "f3" },
804 { 0x3e, "f4" },
805 { 0x3f, "f5" },
806 { 0x40, "f6" },
807 { 0x41, "f7" },
808 { 0x42, "f8" },
809 { 0x43, "f9" },
810 { 0x44, "f10" },
811 { 0x45, "num_lock" },
812 { 0x46, "scroll_lock" },
814 { 0xb5, "kp_divide" },
815 { 0x37, "kp_multiply" },
816 { 0x4a, "kp_subtract" },
817 { 0x4e, "kp_add" },
818 { 0x9c, "kp_enter" },
819 { 0x53, "kp_decimal" },
821 { 0x52, "kp_0" },
822 { 0x4f, "kp_1" },
823 { 0x50, "kp_2" },
824 { 0x51, "kp_3" },
825 { 0x4b, "kp_4" },
826 { 0x4c, "kp_5" },
827 { 0x4d, "kp_6" },
828 { 0x47, "kp_7" },
829 { 0x48, "kp_8" },
830 { 0x49, "kp_9" },
832 { 0x56, "<" },
834 { 0x57, "f11" },
835 { 0x58, "f12" },
837 { 0xb7, "print" },
839 { 0xc7, "home" },
840 { 0xc9, "pgup" },
841 { 0xd1, "pgdn" },
842 { 0xcf, "end" },
844 { 0xcb, "left" },
845 { 0xc8, "up" },
846 { 0xd0, "down" },
847 { 0xcd, "right" },
849 { 0xd2, "insert" },
850 { 0xd3, "delete" },
851 { 0, NULL },
854 static int get_keycode(const char *key)
856 const KeyDef *p;
857 char *endp;
858 int ret;
860 for(p = key_defs; p->name != NULL; p++) {
861 if (!strcmp(key, p->name))
862 return p->keycode;
864 if (strstart(key, "0x", NULL)) {
865 ret = strtoul(key, &endp, 0);
866 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
867 return ret;
869 return -1;
872 static void do_send_key(const char *string)
874 char keybuf[16], *q;
875 uint8_t keycodes[16];
876 const char *p;
877 int nb_keycodes, keycode, i;
879 nb_keycodes = 0;
880 p = string;
881 while (*p != '\0') {
882 q = keybuf;
883 while (*p != '\0' && *p != '-') {
884 if ((q - keybuf) < sizeof(keybuf) - 1) {
885 *q++ = *p;
887 p++;
889 *q = '\0';
890 keycode = get_keycode(keybuf);
891 if (keycode < 0) {
892 term_printf("unknown key: '%s'\n", keybuf);
893 return;
895 keycodes[nb_keycodes++] = keycode;
896 if (*p == '\0')
897 break;
898 p++;
900 /* key down events */
901 for(i = 0; i < nb_keycodes; i++) {
902 keycode = keycodes[i];
903 if (keycode & 0x80)
904 kbd_put_keycode(0xe0);
905 kbd_put_keycode(keycode & 0x7f);
907 /* key up events */
908 for(i = nb_keycodes - 1; i >= 0; i--) {
909 keycode = keycodes[i];
910 if (keycode & 0x80)
911 kbd_put_keycode(0xe0);
912 kbd_put_keycode(keycode | 0x80);
916 static int mouse_button_state;
918 static void do_mouse_move(const char *dx_str, const char *dy_str,
919 const char *dz_str)
921 int dx, dy, dz;
922 dx = strtol(dx_str, NULL, 0);
923 dy = strtol(dy_str, NULL, 0);
924 dz = 0;
925 if (dz_str)
926 dz = strtol(dz_str, NULL, 0);
927 kbd_mouse_event(dx, dy, dz, mouse_button_state);
930 static void do_mouse_button(int button_state)
932 mouse_button_state = button_state;
933 kbd_mouse_event(0, 0, 0, mouse_button_state);
936 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
938 uint32_t val;
939 int suffix;
941 if (has_index) {
942 cpu_outb(NULL, addr & 0xffff, index & 0xff);
943 addr++;
945 addr &= 0xffff;
947 switch(size) {
948 default:
949 case 1:
950 val = cpu_inb(NULL, addr);
951 suffix = 'b';
952 break;
953 case 2:
954 val = cpu_inw(NULL, addr);
955 suffix = 'w';
956 break;
957 case 4:
958 val = cpu_inl(NULL, addr);
959 suffix = 'l';
960 break;
962 term_printf("port%c[0x%04x] = %#0*x\n",
963 suffix, addr, size * 2, val);
966 static void do_system_reset(void)
968 qemu_system_reset_request();
971 static void do_system_powerdown(void)
973 qemu_system_powerdown_request();
976 #if defined(TARGET_I386)
977 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
979 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
980 addr,
981 pte & mask,
982 pte & PG_GLOBAL_MASK ? 'G' : '-',
983 pte & PG_PSE_MASK ? 'P' : '-',
984 pte & PG_DIRTY_MASK ? 'D' : '-',
985 pte & PG_ACCESSED_MASK ? 'A' : '-',
986 pte & PG_PCD_MASK ? 'C' : '-',
987 pte & PG_PWT_MASK ? 'T' : '-',
988 pte & PG_USER_MASK ? 'U' : '-',
989 pte & PG_RW_MASK ? 'W' : '-');
992 static void tlb_info(void)
994 CPUState *env;
995 int l1, l2;
996 uint32_t pgd, pde, pte;
998 env = mon_get_cpu();
999 if (!env)
1000 return;
1002 if (!(env->cr[0] & CR0_PG_MASK)) {
1003 term_printf("PG disabled\n");
1004 return;
1006 pgd = env->cr[3] & ~0xfff;
1007 for(l1 = 0; l1 < 1024; l1++) {
1008 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1009 pde = le32_to_cpu(pde);
1010 if (pde & PG_PRESENT_MASK) {
1011 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1012 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1013 } else {
1014 for(l2 = 0; l2 < 1024; l2++) {
1015 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1016 (uint8_t *)&pte, 4);
1017 pte = le32_to_cpu(pte);
1018 if (pte & PG_PRESENT_MASK) {
1019 print_pte((l1 << 22) + (l2 << 12),
1020 pte & ~PG_PSE_MASK,
1021 ~0xfff);
1029 static void mem_print(uint32_t *pstart, int *plast_prot,
1030 uint32_t end, int prot)
1032 int prot1;
1033 prot1 = *plast_prot;
1034 if (prot != prot1) {
1035 if (*pstart != -1) {
1036 term_printf("%08x-%08x %08x %c%c%c\n",
1037 *pstart, end, end - *pstart,
1038 prot1 & PG_USER_MASK ? 'u' : '-',
1039 'r',
1040 prot1 & PG_RW_MASK ? 'w' : '-');
1042 if (prot != 0)
1043 *pstart = end;
1044 else
1045 *pstart = -1;
1046 *plast_prot = prot;
1050 static void mem_info(void)
1052 CPUState *env;
1053 int l1, l2, prot, last_prot;
1054 uint32_t pgd, pde, pte, start, end;
1056 env = mon_get_cpu();
1057 if (!env)
1058 return;
1060 if (!(env->cr[0] & CR0_PG_MASK)) {
1061 term_printf("PG disabled\n");
1062 return;
1064 pgd = env->cr[3] & ~0xfff;
1065 last_prot = 0;
1066 start = -1;
1067 for(l1 = 0; l1 < 1024; l1++) {
1068 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1069 pde = le32_to_cpu(pde);
1070 end = l1 << 22;
1071 if (pde & PG_PRESENT_MASK) {
1072 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1073 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1074 mem_print(&start, &last_prot, end, prot);
1075 } else {
1076 for(l2 = 0; l2 < 1024; l2++) {
1077 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1078 (uint8_t *)&pte, 4);
1079 pte = le32_to_cpu(pte);
1080 end = (l1 << 22) + (l2 << 12);
1081 if (pte & PG_PRESENT_MASK) {
1082 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1083 } else {
1084 prot = 0;
1086 mem_print(&start, &last_prot, end, prot);
1089 } else {
1090 prot = 0;
1091 mem_print(&start, &last_prot, end, prot);
1095 #endif
1097 static void do_info_kqemu(void)
1099 #ifdef USE_KQEMU
1100 CPUState *env;
1101 int val;
1102 val = 0;
1103 env = mon_get_cpu();
1104 if (!env) {
1105 term_printf("No cpu initialized yet");
1106 return;
1108 val = env->kqemu_enabled;
1109 term_printf("kqemu support: ");
1110 switch(val) {
1111 default:
1112 case 0:
1113 term_printf("disabled\n");
1114 break;
1115 case 1:
1116 term_printf("enabled for user code\n");
1117 break;
1118 case 2:
1119 term_printf("enabled for user and kernel code\n");
1120 break;
1122 #else
1123 term_printf("kqemu support: not compiled\n");
1124 #endif
1127 #ifdef CONFIG_PROFILER
1129 int64_t kqemu_time;
1130 int64_t qemu_time;
1131 int64_t kqemu_exec_count;
1132 int64_t dev_time;
1133 int64_t kqemu_ret_int_count;
1134 int64_t kqemu_ret_excp_count;
1135 int64_t kqemu_ret_intr_count;
1137 static void do_info_profile(void)
1139 int64_t total;
1140 total = qemu_time;
1141 if (total == 0)
1142 total = 1;
1143 term_printf("async time %" PRId64 " (%0.3f)\n",
1144 dev_time, dev_time / (double)ticks_per_sec);
1145 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1146 qemu_time, qemu_time / (double)ticks_per_sec);
1147 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1148 kqemu_time, kqemu_time / (double)ticks_per_sec,
1149 kqemu_time / (double)total * 100.0,
1150 kqemu_exec_count,
1151 kqemu_ret_int_count,
1152 kqemu_ret_excp_count,
1153 kqemu_ret_intr_count);
1154 qemu_time = 0;
1155 kqemu_time = 0;
1156 kqemu_exec_count = 0;
1157 dev_time = 0;
1158 kqemu_ret_int_count = 0;
1159 kqemu_ret_excp_count = 0;
1160 kqemu_ret_intr_count = 0;
1161 #ifdef USE_KQEMU
1162 kqemu_record_dump();
1163 #endif
1165 #else
1166 static void do_info_profile(void)
1168 term_printf("Internal profiler not compiled\n");
1170 #endif
1172 /* Capture support */
1173 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1175 static void do_info_capture (void)
1177 int i;
1178 CaptureState *s;
1180 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1181 term_printf ("[%d]: ", i);
1182 s->ops.info (s->opaque);
1186 static void do_stop_capture (int n)
1188 int i;
1189 CaptureState *s;
1191 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1192 if (i == n) {
1193 s->ops.destroy (s->opaque);
1194 LIST_REMOVE (s, entries);
1195 qemu_free (s);
1196 return;
1201 #ifdef HAS_AUDIO
1202 int wav_start_capture (CaptureState *s, const char *path, int freq,
1203 int bits, int nchannels);
1205 static void do_wav_capture (const char *path,
1206 int has_freq, int freq,
1207 int has_bits, int bits,
1208 int has_channels, int nchannels)
1210 CaptureState *s;
1212 s = qemu_mallocz (sizeof (*s));
1213 if (!s) {
1214 term_printf ("Not enough memory to add wave capture\n");
1215 return;
1218 freq = has_freq ? freq : 44100;
1219 bits = has_bits ? bits : 16;
1220 nchannels = has_channels ? nchannels : 2;
1222 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1223 term_printf ("Faied to add wave capture\n");
1224 qemu_free (s);
1226 LIST_INSERT_HEAD (&capture_head, s, entries);
1228 #endif
1230 static term_cmd_t term_cmds[] = {
1231 { "help|?", "s?", do_help,
1232 "[cmd]", "show the help" },
1233 { "commit", "s", do_commit,
1234 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1235 { "info", "s?", do_info,
1236 "subcommand", "show various information about the system state" },
1237 { "q|quit", "", do_quit,
1238 "", "quit the emulator" },
1239 { "eject", "-fB", do_eject,
1240 "[-f] device", "eject a removable medium (use -f to force it)" },
1241 { "change", "BF", do_change,
1242 "device filename", "change a removable medium" },
1243 { "screendump", "F", do_screen_dump,
1244 "filename", "save screen into PPM image 'filename'" },
1245 { "logfile", "s", do_logfile,
1246 "filename", "output logs to 'filename'" },
1247 { "log", "s", do_log,
1248 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1249 { "savevm", "s?", do_savevm,
1250 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1251 { "loadvm", "s", do_loadvm,
1252 "tag|id", "restore a VM snapshot from its tag or id" },
1253 { "delvm", "s", do_delvm,
1254 "tag|id", "delete a VM snapshot from its tag or id" },
1255 { "stop", "", do_stop,
1256 "", "stop emulation", },
1257 { "c|cont", "", do_cont,
1258 "", "resume emulation", },
1259 #ifdef CONFIG_GDBSTUB
1260 { "gdbserver", "s?", do_gdbserver,
1261 "[port]", "start gdbserver session (default port=1234)", },
1262 #endif
1263 { "x", "/l", do_memory_dump,
1264 "/fmt addr", "virtual memory dump starting at 'addr'", },
1265 { "xp", "/l", do_physical_memory_dump,
1266 "/fmt addr", "physical memory dump starting at 'addr'", },
1267 { "p|print", "/l", do_print,
1268 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1269 { "i", "/ii.", do_ioport_read,
1270 "/fmt addr", "I/O port read" },
1272 { "sendkey", "s", do_send_key,
1273 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1274 { "system_reset", "", do_system_reset,
1275 "", "reset the system" },
1276 { "system_powerdown", "", do_system_powerdown,
1277 "", "send system power down event" },
1278 { "sum", "ii", do_sum,
1279 "addr size", "compute the checksum of a memory region" },
1280 { "usb_add", "s", do_usb_add,
1281 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1282 { "usb_del", "s", do_usb_del,
1283 "device", "remove USB device 'bus.addr'" },
1284 { "cpu", "i", do_cpu_set,
1285 "index", "set the default CPU" },
1286 { "mouse_move", "sss?", do_mouse_move,
1287 "dx dy [dz]", "send mouse move events" },
1288 { "mouse_button", "i", do_mouse_button,
1289 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1290 { "mouse_set", "i", do_mouse_set,
1291 "index", "set which mouse device receives events" },
1292 #ifdef HAS_AUDIO
1293 { "wavcapture", "si?i?i?", do_wav_capture,
1294 "path [frequency bits channels]",
1295 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1296 #endif
1297 { "stopcapture", "i", do_stop_capture,
1298 "capture index", "stop capture" },
1299 { "memsave", "lis", do_memory_save,
1300 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1301 { NULL, NULL, },
1304 static term_cmd_t info_cmds[] = {
1305 { "version", "", do_info_version,
1306 "", "show the version of qemu" },
1307 { "network", "", do_info_network,
1308 "", "show the network state" },
1309 { "block", "", do_info_block,
1310 "", "show the block devices" },
1311 { "registers", "", do_info_registers,
1312 "", "show the cpu registers" },
1313 { "cpus", "", do_info_cpus,
1314 "", "show infos for each CPU" },
1315 { "history", "", do_info_history,
1316 "", "show the command line history", },
1317 { "irq", "", irq_info,
1318 "", "show the interrupts statistics (if available)", },
1319 { "pic", "", pic_info,
1320 "", "show i8259 (PIC) state", },
1321 { "pci", "", pci_info,
1322 "", "show PCI info", },
1323 #if defined(TARGET_I386)
1324 { "tlb", "", tlb_info,
1325 "", "show virtual to physical memory mappings", },
1326 { "mem", "", mem_info,
1327 "", "show the active virtual memory mappings", },
1328 #endif
1329 { "jit", "", do_info_jit,
1330 "", "show dynamic compiler info", },
1331 { "kqemu", "", do_info_kqemu,
1332 "", "show kqemu information", },
1333 { "usb", "", usb_info,
1334 "", "show guest USB devices", },
1335 { "usbhost", "", usb_host_info,
1336 "", "show host USB devices", },
1337 { "profile", "", do_info_profile,
1338 "", "show profiling information", },
1339 { "capture", "", do_info_capture,
1340 "", "show capture information" },
1341 { "snapshots", "", do_info_snapshots,
1342 "", "show the currently saved VM snapshots" },
1343 { "pcmcia", "", pcmcia_info,
1344 "", "show guest PCMCIA status" },
1345 { "mice", "", do_info_mice,
1346 "", "show which guest mouse is receiving events" },
1347 { "vnc", "", do_info_vnc,
1348 "", "show the vnc server status"},
1349 { "name", "", do_info_name,
1350 "", "show the current VM name" },
1351 #if defined(TARGET_PPC)
1352 { "cpustats", "", do_info_cpu_stats,
1353 "", "show CPU statistics", },
1354 #endif
1355 { NULL, NULL, },
1358 /*******************************************************************/
1360 static const char *pch;
1361 static jmp_buf expr_env;
1363 #define MD_TLONG 0
1364 #define MD_I32 1
1366 typedef struct MonitorDef {
1367 const char *name;
1368 int offset;
1369 target_long (*get_value)(struct MonitorDef *md, int val);
1370 int type;
1371 } MonitorDef;
1373 #if defined(TARGET_I386)
1374 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1376 CPUState *env = mon_get_cpu();
1377 if (!env)
1378 return 0;
1379 return env->eip + env->segs[R_CS].base;
1381 #endif
1383 #if defined(TARGET_PPC)
1384 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1386 CPUState *env = mon_get_cpu();
1387 unsigned int u;
1388 int i;
1390 if (!env)
1391 return 0;
1393 u = 0;
1394 for (i = 0; i < 8; i++)
1395 u |= env->crf[i] << (32 - (4 * i));
1397 return u;
1400 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1402 CPUState *env = mon_get_cpu();
1403 if (!env)
1404 return 0;
1405 return (env->msr[MSR_POW] << MSR_POW) |
1406 (env->msr[MSR_ILE] << MSR_ILE) |
1407 (env->msr[MSR_EE] << MSR_EE) |
1408 (env->msr[MSR_PR] << MSR_PR) |
1409 (env->msr[MSR_FP] << MSR_FP) |
1410 (env->msr[MSR_ME] << MSR_ME) |
1411 (env->msr[MSR_FE0] << MSR_FE0) |
1412 (env->msr[MSR_SE] << MSR_SE) |
1413 (env->msr[MSR_BE] << MSR_BE) |
1414 (env->msr[MSR_FE1] << MSR_FE1) |
1415 (env->msr[MSR_IP] << MSR_IP) |
1416 (env->msr[MSR_IR] << MSR_IR) |
1417 (env->msr[MSR_DR] << MSR_DR) |
1418 (env->msr[MSR_RI] << MSR_RI) |
1419 (env->msr[MSR_LE] << MSR_LE);
1422 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1424 CPUState *env = mon_get_cpu();
1425 if (!env)
1426 return 0;
1427 return (env->xer[XER_SO] << XER_SO) |
1428 (env->xer[XER_OV] << XER_OV) |
1429 (env->xer[XER_CA] << XER_CA) |
1430 (env->xer[XER_BC] << XER_BC);
1433 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1435 CPUState *env = mon_get_cpu();
1436 if (!env)
1437 return 0;
1438 return cpu_ppc_load_decr(env);
1441 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1443 CPUState *env = mon_get_cpu();
1444 if (!env)
1445 return 0;
1446 return cpu_ppc_load_tbu(env);
1449 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1451 CPUState *env = mon_get_cpu();
1452 if (!env)
1453 return 0;
1454 return cpu_ppc_load_tbl(env);
1456 #endif
1458 #if defined(TARGET_SPARC)
1459 #ifndef TARGET_SPARC64
1460 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1462 CPUState *env = mon_get_cpu();
1463 if (!env)
1464 return 0;
1465 return GET_PSR(env);
1467 #endif
1469 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1471 CPUState *env = mon_get_cpu();
1472 if (!env)
1473 return 0;
1474 return env->regwptr[val];
1476 #endif
1478 static MonitorDef monitor_defs[] = {
1479 #ifdef TARGET_I386
1481 #define SEG(name, seg) \
1482 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1483 { name ".base", offsetof(CPUState, segs[seg].base) },\
1484 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1486 { "eax", offsetof(CPUState, regs[0]) },
1487 { "ecx", offsetof(CPUState, regs[1]) },
1488 { "edx", offsetof(CPUState, regs[2]) },
1489 { "ebx", offsetof(CPUState, regs[3]) },
1490 { "esp|sp", offsetof(CPUState, regs[4]) },
1491 { "ebp|fp", offsetof(CPUState, regs[5]) },
1492 { "esi", offsetof(CPUState, regs[6]) },
1493 { "edi", offsetof(CPUState, regs[7]) },
1494 #ifdef TARGET_X86_64
1495 { "r8", offsetof(CPUState, regs[8]) },
1496 { "r9", offsetof(CPUState, regs[9]) },
1497 { "r10", offsetof(CPUState, regs[10]) },
1498 { "r11", offsetof(CPUState, regs[11]) },
1499 { "r12", offsetof(CPUState, regs[12]) },
1500 { "r13", offsetof(CPUState, regs[13]) },
1501 { "r14", offsetof(CPUState, regs[14]) },
1502 { "r15", offsetof(CPUState, regs[15]) },
1503 #endif
1504 { "eflags", offsetof(CPUState, eflags) },
1505 { "eip", offsetof(CPUState, eip) },
1506 SEG("cs", R_CS)
1507 SEG("ds", R_DS)
1508 SEG("es", R_ES)
1509 SEG("ss", R_SS)
1510 SEG("fs", R_FS)
1511 SEG("gs", R_GS)
1512 { "pc", 0, monitor_get_pc, },
1513 #elif defined(TARGET_PPC)
1514 { "r0", offsetof(CPUState, gpr[0]) },
1515 { "r1", offsetof(CPUState, gpr[1]) },
1516 { "r2", offsetof(CPUState, gpr[2]) },
1517 { "r3", offsetof(CPUState, gpr[3]) },
1518 { "r4", offsetof(CPUState, gpr[4]) },
1519 { "r5", offsetof(CPUState, gpr[5]) },
1520 { "r6", offsetof(CPUState, gpr[6]) },
1521 { "r7", offsetof(CPUState, gpr[7]) },
1522 { "r8", offsetof(CPUState, gpr[8]) },
1523 { "r9", offsetof(CPUState, gpr[9]) },
1524 { "r10", offsetof(CPUState, gpr[10]) },
1525 { "r11", offsetof(CPUState, gpr[11]) },
1526 { "r12", offsetof(CPUState, gpr[12]) },
1527 { "r13", offsetof(CPUState, gpr[13]) },
1528 { "r14", offsetof(CPUState, gpr[14]) },
1529 { "r15", offsetof(CPUState, gpr[15]) },
1530 { "r16", offsetof(CPUState, gpr[16]) },
1531 { "r17", offsetof(CPUState, gpr[17]) },
1532 { "r18", offsetof(CPUState, gpr[18]) },
1533 { "r19", offsetof(CPUState, gpr[19]) },
1534 { "r20", offsetof(CPUState, gpr[20]) },
1535 { "r21", offsetof(CPUState, gpr[21]) },
1536 { "r22", offsetof(CPUState, gpr[22]) },
1537 { "r23", offsetof(CPUState, gpr[23]) },
1538 { "r24", offsetof(CPUState, gpr[24]) },
1539 { "r25", offsetof(CPUState, gpr[25]) },
1540 { "r26", offsetof(CPUState, gpr[26]) },
1541 { "r27", offsetof(CPUState, gpr[27]) },
1542 { "r28", offsetof(CPUState, gpr[28]) },
1543 { "r29", offsetof(CPUState, gpr[29]) },
1544 { "r30", offsetof(CPUState, gpr[30]) },
1545 { "r31", offsetof(CPUState, gpr[31]) },
1546 { "nip|pc", offsetof(CPUState, nip) },
1547 { "lr", offsetof(CPUState, lr) },
1548 { "ctr", offsetof(CPUState, ctr) },
1549 { "decr", 0, &monitor_get_decr, },
1550 { "ccr", 0, &monitor_get_ccr, },
1551 { "msr", 0, &monitor_get_msr, },
1552 { "xer", 0, &monitor_get_xer, },
1553 { "tbu", 0, &monitor_get_tbu, },
1554 { "tbl", 0, &monitor_get_tbl, },
1555 { "sdr1", offsetof(CPUState, sdr1) },
1556 { "sr0", offsetof(CPUState, sr[0]) },
1557 { "sr1", offsetof(CPUState, sr[1]) },
1558 { "sr2", offsetof(CPUState, sr[2]) },
1559 { "sr3", offsetof(CPUState, sr[3]) },
1560 { "sr4", offsetof(CPUState, sr[4]) },
1561 { "sr5", offsetof(CPUState, sr[5]) },
1562 { "sr6", offsetof(CPUState, sr[6]) },
1563 { "sr7", offsetof(CPUState, sr[7]) },
1564 { "sr8", offsetof(CPUState, sr[8]) },
1565 { "sr9", offsetof(CPUState, sr[9]) },
1566 { "sr10", offsetof(CPUState, sr[10]) },
1567 { "sr11", offsetof(CPUState, sr[11]) },
1568 { "sr12", offsetof(CPUState, sr[12]) },
1569 { "sr13", offsetof(CPUState, sr[13]) },
1570 { "sr14", offsetof(CPUState, sr[14]) },
1571 { "sr15", offsetof(CPUState, sr[15]) },
1572 /* Too lazy to put BATs and SPRs ... */
1573 #elif defined(TARGET_SPARC)
1574 { "g0", offsetof(CPUState, gregs[0]) },
1575 { "g1", offsetof(CPUState, gregs[1]) },
1576 { "g2", offsetof(CPUState, gregs[2]) },
1577 { "g3", offsetof(CPUState, gregs[3]) },
1578 { "g4", offsetof(CPUState, gregs[4]) },
1579 { "g5", offsetof(CPUState, gregs[5]) },
1580 { "g6", offsetof(CPUState, gregs[6]) },
1581 { "g7", offsetof(CPUState, gregs[7]) },
1582 { "o0", 0, monitor_get_reg },
1583 { "o1", 1, monitor_get_reg },
1584 { "o2", 2, monitor_get_reg },
1585 { "o3", 3, monitor_get_reg },
1586 { "o4", 4, monitor_get_reg },
1587 { "o5", 5, monitor_get_reg },
1588 { "o6", 6, monitor_get_reg },
1589 { "o7", 7, monitor_get_reg },
1590 { "l0", 8, monitor_get_reg },
1591 { "l1", 9, monitor_get_reg },
1592 { "l2", 10, monitor_get_reg },
1593 { "l3", 11, monitor_get_reg },
1594 { "l4", 12, monitor_get_reg },
1595 { "l5", 13, monitor_get_reg },
1596 { "l6", 14, monitor_get_reg },
1597 { "l7", 15, monitor_get_reg },
1598 { "i0", 16, monitor_get_reg },
1599 { "i1", 17, monitor_get_reg },
1600 { "i2", 18, monitor_get_reg },
1601 { "i3", 19, monitor_get_reg },
1602 { "i4", 20, monitor_get_reg },
1603 { "i5", 21, monitor_get_reg },
1604 { "i6", 22, monitor_get_reg },
1605 { "i7", 23, monitor_get_reg },
1606 { "pc", offsetof(CPUState, pc) },
1607 { "npc", offsetof(CPUState, npc) },
1608 { "y", offsetof(CPUState, y) },
1609 #ifndef TARGET_SPARC64
1610 { "psr", 0, &monitor_get_psr, },
1611 { "wim", offsetof(CPUState, wim) },
1612 #endif
1613 { "tbr", offsetof(CPUState, tbr) },
1614 { "fsr", offsetof(CPUState, fsr) },
1615 { "f0", offsetof(CPUState, fpr[0]) },
1616 { "f1", offsetof(CPUState, fpr[1]) },
1617 { "f2", offsetof(CPUState, fpr[2]) },
1618 { "f3", offsetof(CPUState, fpr[3]) },
1619 { "f4", offsetof(CPUState, fpr[4]) },
1620 { "f5", offsetof(CPUState, fpr[5]) },
1621 { "f6", offsetof(CPUState, fpr[6]) },
1622 { "f7", offsetof(CPUState, fpr[7]) },
1623 { "f8", offsetof(CPUState, fpr[8]) },
1624 { "f9", offsetof(CPUState, fpr[9]) },
1625 { "f10", offsetof(CPUState, fpr[10]) },
1626 { "f11", offsetof(CPUState, fpr[11]) },
1627 { "f12", offsetof(CPUState, fpr[12]) },
1628 { "f13", offsetof(CPUState, fpr[13]) },
1629 { "f14", offsetof(CPUState, fpr[14]) },
1630 { "f15", offsetof(CPUState, fpr[15]) },
1631 { "f16", offsetof(CPUState, fpr[16]) },
1632 { "f17", offsetof(CPUState, fpr[17]) },
1633 { "f18", offsetof(CPUState, fpr[18]) },
1634 { "f19", offsetof(CPUState, fpr[19]) },
1635 { "f20", offsetof(CPUState, fpr[20]) },
1636 { "f21", offsetof(CPUState, fpr[21]) },
1637 { "f22", offsetof(CPUState, fpr[22]) },
1638 { "f23", offsetof(CPUState, fpr[23]) },
1639 { "f24", offsetof(CPUState, fpr[24]) },
1640 { "f25", offsetof(CPUState, fpr[25]) },
1641 { "f26", offsetof(CPUState, fpr[26]) },
1642 { "f27", offsetof(CPUState, fpr[27]) },
1643 { "f28", offsetof(CPUState, fpr[28]) },
1644 { "f29", offsetof(CPUState, fpr[29]) },
1645 { "f30", offsetof(CPUState, fpr[30]) },
1646 { "f31", offsetof(CPUState, fpr[31]) },
1647 #ifdef TARGET_SPARC64
1648 { "f32", offsetof(CPUState, fpr[32]) },
1649 { "f34", offsetof(CPUState, fpr[34]) },
1650 { "f36", offsetof(CPUState, fpr[36]) },
1651 { "f38", offsetof(CPUState, fpr[38]) },
1652 { "f40", offsetof(CPUState, fpr[40]) },
1653 { "f42", offsetof(CPUState, fpr[42]) },
1654 { "f44", offsetof(CPUState, fpr[44]) },
1655 { "f46", offsetof(CPUState, fpr[46]) },
1656 { "f48", offsetof(CPUState, fpr[48]) },
1657 { "f50", offsetof(CPUState, fpr[50]) },
1658 { "f52", offsetof(CPUState, fpr[52]) },
1659 { "f54", offsetof(CPUState, fpr[54]) },
1660 { "f56", offsetof(CPUState, fpr[56]) },
1661 { "f58", offsetof(CPUState, fpr[58]) },
1662 { "f60", offsetof(CPUState, fpr[60]) },
1663 { "f62", offsetof(CPUState, fpr[62]) },
1664 { "asi", offsetof(CPUState, asi) },
1665 { "pstate", offsetof(CPUState, pstate) },
1666 { "cansave", offsetof(CPUState, cansave) },
1667 { "canrestore", offsetof(CPUState, canrestore) },
1668 { "otherwin", offsetof(CPUState, otherwin) },
1669 { "wstate", offsetof(CPUState, wstate) },
1670 { "cleanwin", offsetof(CPUState, cleanwin) },
1671 { "fprs", offsetof(CPUState, fprs) },
1672 #endif
1673 #endif
1674 { NULL },
1677 static void expr_error(const char *fmt)
1679 term_printf(fmt);
1680 term_printf("\n");
1681 longjmp(expr_env, 1);
1684 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1685 static int get_monitor_def(target_long *pval, const char *name)
1687 MonitorDef *md;
1688 void *ptr;
1690 for(md = monitor_defs; md->name != NULL; md++) {
1691 if (compare_cmd(name, md->name)) {
1692 if (md->get_value) {
1693 *pval = md->get_value(md, md->offset);
1694 } else {
1695 CPUState *env = mon_get_cpu();
1696 if (!env)
1697 return -2;
1698 ptr = (uint8_t *)env + md->offset;
1699 switch(md->type) {
1700 case MD_I32:
1701 *pval = *(int32_t *)ptr;
1702 break;
1703 case MD_TLONG:
1704 *pval = *(target_long *)ptr;
1705 break;
1706 default:
1707 *pval = 0;
1708 break;
1711 return 0;
1714 return -1;
1717 static void next(void)
1719 if (pch != '\0') {
1720 pch++;
1721 while (isspace(*pch))
1722 pch++;
1726 static target_long expr_sum(void);
1728 static target_long expr_unary(void)
1730 target_long n;
1731 char *p;
1732 int ret;
1734 switch(*pch) {
1735 case '+':
1736 next();
1737 n = expr_unary();
1738 break;
1739 case '-':
1740 next();
1741 n = -expr_unary();
1742 break;
1743 case '~':
1744 next();
1745 n = ~expr_unary();
1746 break;
1747 case '(':
1748 next();
1749 n = expr_sum();
1750 if (*pch != ')') {
1751 expr_error("')' expected");
1753 next();
1754 break;
1755 case '\'':
1756 pch++;
1757 if (*pch == '\0')
1758 expr_error("character constant expected");
1759 n = *pch;
1760 pch++;
1761 if (*pch != '\'')
1762 expr_error("missing terminating \' character");
1763 next();
1764 break;
1765 case '$':
1767 char buf[128], *q;
1769 pch++;
1770 q = buf;
1771 while ((*pch >= 'a' && *pch <= 'z') ||
1772 (*pch >= 'A' && *pch <= 'Z') ||
1773 (*pch >= '0' && *pch <= '9') ||
1774 *pch == '_' || *pch == '.') {
1775 if ((q - buf) < sizeof(buf) - 1)
1776 *q++ = *pch;
1777 pch++;
1779 while (isspace(*pch))
1780 pch++;
1781 *q = 0;
1782 ret = get_monitor_def(&n, buf);
1783 if (ret == -1)
1784 expr_error("unknown register");
1785 else if (ret == -2)
1786 expr_error("no cpu defined");
1788 break;
1789 case '\0':
1790 expr_error("unexpected end of expression");
1791 n = 0;
1792 break;
1793 default:
1794 #if TARGET_LONG_BITS == 64
1795 n = strtoull(pch, &p, 0);
1796 #else
1797 n = strtoul(pch, &p, 0);
1798 #endif
1799 if (pch == p) {
1800 expr_error("invalid char in expression");
1802 pch = p;
1803 while (isspace(*pch))
1804 pch++;
1805 break;
1807 return n;
1811 static target_long expr_prod(void)
1813 target_long val, val2;
1814 int op;
1816 val = expr_unary();
1817 for(;;) {
1818 op = *pch;
1819 if (op != '*' && op != '/' && op != '%')
1820 break;
1821 next();
1822 val2 = expr_unary();
1823 switch(op) {
1824 default:
1825 case '*':
1826 val *= val2;
1827 break;
1828 case '/':
1829 case '%':
1830 if (val2 == 0)
1831 expr_error("division by zero");
1832 if (op == '/')
1833 val /= val2;
1834 else
1835 val %= val2;
1836 break;
1839 return val;
1842 static target_long expr_logic(void)
1844 target_long val, val2;
1845 int op;
1847 val = expr_prod();
1848 for(;;) {
1849 op = *pch;
1850 if (op != '&' && op != '|' && op != '^')
1851 break;
1852 next();
1853 val2 = expr_prod();
1854 switch(op) {
1855 default:
1856 case '&':
1857 val &= val2;
1858 break;
1859 case '|':
1860 val |= val2;
1861 break;
1862 case '^':
1863 val ^= val2;
1864 break;
1867 return val;
1870 static target_long expr_sum(void)
1872 target_long val, val2;
1873 int op;
1875 val = expr_logic();
1876 for(;;) {
1877 op = *pch;
1878 if (op != '+' && op != '-')
1879 break;
1880 next();
1881 val2 = expr_logic();
1882 if (op == '+')
1883 val += val2;
1884 else
1885 val -= val2;
1887 return val;
1890 static int get_expr(target_long *pval, const char **pp)
1892 pch = *pp;
1893 if (setjmp(expr_env)) {
1894 *pp = pch;
1895 return -1;
1897 while (isspace(*pch))
1898 pch++;
1899 *pval = expr_sum();
1900 *pp = pch;
1901 return 0;
1904 static int get_str(char *buf, int buf_size, const char **pp)
1906 const char *p;
1907 char *q;
1908 int c;
1910 q = buf;
1911 p = *pp;
1912 while (isspace(*p))
1913 p++;
1914 if (*p == '\0') {
1915 fail:
1916 *q = '\0';
1917 *pp = p;
1918 return -1;
1920 if (*p == '\"') {
1921 p++;
1922 while (*p != '\0' && *p != '\"') {
1923 if (*p == '\\') {
1924 p++;
1925 c = *p++;
1926 switch(c) {
1927 case 'n':
1928 c = '\n';
1929 break;
1930 case 'r':
1931 c = '\r';
1932 break;
1933 case '\\':
1934 case '\'':
1935 case '\"':
1936 break;
1937 default:
1938 qemu_printf("unsupported escape code: '\\%c'\n", c);
1939 goto fail;
1941 if ((q - buf) < buf_size - 1) {
1942 *q++ = c;
1944 } else {
1945 if ((q - buf) < buf_size - 1) {
1946 *q++ = *p;
1948 p++;
1951 if (*p != '\"') {
1952 qemu_printf("unterminated string\n");
1953 goto fail;
1955 p++;
1956 } else {
1957 while (*p != '\0' && !isspace(*p)) {
1958 if ((q - buf) < buf_size - 1) {
1959 *q++ = *p;
1961 p++;
1964 *q = '\0';
1965 *pp = p;
1966 return 0;
1969 static int default_fmt_format = 'x';
1970 static int default_fmt_size = 4;
1972 #define MAX_ARGS 16
1974 static void monitor_handle_command(const char *cmdline)
1976 const char *p, *pstart, *typestr;
1977 char *q;
1978 int c, nb_args, len, i, has_arg;
1979 term_cmd_t *cmd;
1980 char cmdname[256];
1981 char buf[1024];
1982 void *str_allocated[MAX_ARGS];
1983 void *args[MAX_ARGS];
1985 #ifdef DEBUG
1986 term_printf("command='%s'\n", cmdline);
1987 #endif
1989 /* extract the command name */
1990 p = cmdline;
1991 q = cmdname;
1992 while (isspace(*p))
1993 p++;
1994 if (*p == '\0')
1995 return;
1996 pstart = p;
1997 while (*p != '\0' && *p != '/' && !isspace(*p))
1998 p++;
1999 len = p - pstart;
2000 if (len > sizeof(cmdname) - 1)
2001 len = sizeof(cmdname) - 1;
2002 memcpy(cmdname, pstart, len);
2003 cmdname[len] = '\0';
2005 /* find the command */
2006 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2007 if (compare_cmd(cmdname, cmd->name))
2008 goto found;
2010 term_printf("unknown command: '%s'\n", cmdname);
2011 return;
2012 found:
2014 for(i = 0; i < MAX_ARGS; i++)
2015 str_allocated[i] = NULL;
2017 /* parse the parameters */
2018 typestr = cmd->args_type;
2019 nb_args = 0;
2020 for(;;) {
2021 c = *typestr;
2022 if (c == '\0')
2023 break;
2024 typestr++;
2025 switch(c) {
2026 case 'F':
2027 case 'B':
2028 case 's':
2030 int ret;
2031 char *str;
2033 while (isspace(*p))
2034 p++;
2035 if (*typestr == '?') {
2036 typestr++;
2037 if (*p == '\0') {
2038 /* no optional string: NULL argument */
2039 str = NULL;
2040 goto add_str;
2043 ret = get_str(buf, sizeof(buf), &p);
2044 if (ret < 0) {
2045 switch(c) {
2046 case 'F':
2047 term_printf("%s: filename expected\n", cmdname);
2048 break;
2049 case 'B':
2050 term_printf("%s: block device name expected\n", cmdname);
2051 break;
2052 default:
2053 term_printf("%s: string expected\n", cmdname);
2054 break;
2056 goto fail;
2058 str = qemu_malloc(strlen(buf) + 1);
2059 strcpy(str, buf);
2060 str_allocated[nb_args] = str;
2061 add_str:
2062 if (nb_args >= MAX_ARGS) {
2063 error_args:
2064 term_printf("%s: too many arguments\n", cmdname);
2065 goto fail;
2067 args[nb_args++] = str;
2069 break;
2070 case '/':
2072 int count, format, size;
2074 while (isspace(*p))
2075 p++;
2076 if (*p == '/') {
2077 /* format found */
2078 p++;
2079 count = 1;
2080 if (isdigit(*p)) {
2081 count = 0;
2082 while (isdigit(*p)) {
2083 count = count * 10 + (*p - '0');
2084 p++;
2087 size = -1;
2088 format = -1;
2089 for(;;) {
2090 switch(*p) {
2091 case 'o':
2092 case 'd':
2093 case 'u':
2094 case 'x':
2095 case 'i':
2096 case 'c':
2097 format = *p++;
2098 break;
2099 case 'b':
2100 size = 1;
2101 p++;
2102 break;
2103 case 'h':
2104 size = 2;
2105 p++;
2106 break;
2107 case 'w':
2108 size = 4;
2109 p++;
2110 break;
2111 case 'g':
2112 case 'L':
2113 size = 8;
2114 p++;
2115 break;
2116 default:
2117 goto next;
2120 next:
2121 if (*p != '\0' && !isspace(*p)) {
2122 term_printf("invalid char in format: '%c'\n", *p);
2123 goto fail;
2125 if (format < 0)
2126 format = default_fmt_format;
2127 if (format != 'i') {
2128 /* for 'i', not specifying a size gives -1 as size */
2129 if (size < 0)
2130 size = default_fmt_size;
2132 default_fmt_size = size;
2133 default_fmt_format = format;
2134 } else {
2135 count = 1;
2136 format = default_fmt_format;
2137 if (format != 'i') {
2138 size = default_fmt_size;
2139 } else {
2140 size = -1;
2143 if (nb_args + 3 > MAX_ARGS)
2144 goto error_args;
2145 args[nb_args++] = (void*)(long)count;
2146 args[nb_args++] = (void*)(long)format;
2147 args[nb_args++] = (void*)(long)size;
2149 break;
2150 case 'i':
2151 case 'l':
2153 target_long val;
2154 while (isspace(*p))
2155 p++;
2156 if (*typestr == '?' || *typestr == '.') {
2157 if (*typestr == '?') {
2158 if (*p == '\0')
2159 has_arg = 0;
2160 else
2161 has_arg = 1;
2162 } else {
2163 if (*p == '.') {
2164 p++;
2165 while (isspace(*p))
2166 p++;
2167 has_arg = 1;
2168 } else {
2169 has_arg = 0;
2172 typestr++;
2173 if (nb_args >= MAX_ARGS)
2174 goto error_args;
2175 args[nb_args++] = (void *)(long)has_arg;
2176 if (!has_arg) {
2177 if (nb_args >= MAX_ARGS)
2178 goto error_args;
2179 val = -1;
2180 goto add_num;
2183 if (get_expr(&val, &p))
2184 goto fail;
2185 add_num:
2186 if (c == 'i') {
2187 if (nb_args >= MAX_ARGS)
2188 goto error_args;
2189 args[nb_args++] = (void *)(long)val;
2190 } else {
2191 if ((nb_args + 1) >= MAX_ARGS)
2192 goto error_args;
2193 #if TARGET_LONG_BITS == 64
2194 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2195 #else
2196 args[nb_args++] = (void *)0;
2197 #endif
2198 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2201 break;
2202 case '-':
2204 int has_option;
2205 /* option */
2207 c = *typestr++;
2208 if (c == '\0')
2209 goto bad_type;
2210 while (isspace(*p))
2211 p++;
2212 has_option = 0;
2213 if (*p == '-') {
2214 p++;
2215 if (*p != c) {
2216 term_printf("%s: unsupported option -%c\n",
2217 cmdname, *p);
2218 goto fail;
2220 p++;
2221 has_option = 1;
2223 if (nb_args >= MAX_ARGS)
2224 goto error_args;
2225 args[nb_args++] = (void *)(long)has_option;
2227 break;
2228 default:
2229 bad_type:
2230 term_printf("%s: unknown type '%c'\n", cmdname, c);
2231 goto fail;
2234 /* check that all arguments were parsed */
2235 while (isspace(*p))
2236 p++;
2237 if (*p != '\0') {
2238 term_printf("%s: extraneous characters at the end of line\n",
2239 cmdname);
2240 goto fail;
2243 switch(nb_args) {
2244 case 0:
2245 cmd->handler();
2246 break;
2247 case 1:
2248 cmd->handler(args[0]);
2249 break;
2250 case 2:
2251 cmd->handler(args[0], args[1]);
2252 break;
2253 case 3:
2254 cmd->handler(args[0], args[1], args[2]);
2255 break;
2256 case 4:
2257 cmd->handler(args[0], args[1], args[2], args[3]);
2258 break;
2259 case 5:
2260 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2261 break;
2262 case 6:
2263 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2264 break;
2265 case 7:
2266 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2267 break;
2268 default:
2269 term_printf("unsupported number of arguments: %d\n", nb_args);
2270 goto fail;
2272 fail:
2273 for(i = 0; i < MAX_ARGS; i++)
2274 qemu_free(str_allocated[i]);
2275 return;
2278 static void cmd_completion(const char *name, const char *list)
2280 const char *p, *pstart;
2281 char cmd[128];
2282 int len;
2284 p = list;
2285 for(;;) {
2286 pstart = p;
2287 p = strchr(p, '|');
2288 if (!p)
2289 p = pstart + strlen(pstart);
2290 len = p - pstart;
2291 if (len > sizeof(cmd) - 2)
2292 len = sizeof(cmd) - 2;
2293 memcpy(cmd, pstart, len);
2294 cmd[len] = '\0';
2295 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2296 add_completion(cmd);
2298 if (*p == '\0')
2299 break;
2300 p++;
2304 static void file_completion(const char *input)
2306 DIR *ffs;
2307 struct dirent *d;
2308 char path[1024];
2309 char file[1024], file_prefix[1024];
2310 int input_path_len;
2311 const char *p;
2313 p = strrchr(input, '/');
2314 if (!p) {
2315 input_path_len = 0;
2316 pstrcpy(file_prefix, sizeof(file_prefix), input);
2317 strcpy(path, ".");
2318 } else {
2319 input_path_len = p - input + 1;
2320 memcpy(path, input, input_path_len);
2321 if (input_path_len > sizeof(path) - 1)
2322 input_path_len = sizeof(path) - 1;
2323 path[input_path_len] = '\0';
2324 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2326 #ifdef DEBUG_COMPLETION
2327 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2328 #endif
2329 ffs = opendir(path);
2330 if (!ffs)
2331 return;
2332 for(;;) {
2333 struct stat sb;
2334 d = readdir(ffs);
2335 if (!d)
2336 break;
2337 if (strstart(d->d_name, file_prefix, NULL)) {
2338 memcpy(file, input, input_path_len);
2339 strcpy(file + input_path_len, d->d_name);
2340 /* stat the file to find out if it's a directory.
2341 * In that case add a slash to speed up typing long paths
2343 stat(file, &sb);
2344 if(S_ISDIR(sb.st_mode))
2345 strcat(file, "/");
2346 add_completion(file);
2349 closedir(ffs);
2352 static void block_completion_it(void *opaque, const char *name)
2354 const char *input = opaque;
2356 if (input[0] == '\0' ||
2357 !strncmp(name, (char *)input, strlen(input))) {
2358 add_completion(name);
2362 /* NOTE: this parser is an approximate form of the real command parser */
2363 static void parse_cmdline(const char *cmdline,
2364 int *pnb_args, char **args)
2366 const char *p;
2367 int nb_args, ret;
2368 char buf[1024];
2370 p = cmdline;
2371 nb_args = 0;
2372 for(;;) {
2373 while (isspace(*p))
2374 p++;
2375 if (*p == '\0')
2376 break;
2377 if (nb_args >= MAX_ARGS)
2378 break;
2379 ret = get_str(buf, sizeof(buf), &p);
2380 args[nb_args] = qemu_strdup(buf);
2381 nb_args++;
2382 if (ret < 0)
2383 break;
2385 *pnb_args = nb_args;
2388 void readline_find_completion(const char *cmdline)
2390 const char *cmdname;
2391 char *args[MAX_ARGS];
2392 int nb_args, i, len;
2393 const char *ptype, *str;
2394 term_cmd_t *cmd;
2395 const KeyDef *key;
2397 parse_cmdline(cmdline, &nb_args, args);
2398 #ifdef DEBUG_COMPLETION
2399 for(i = 0; i < nb_args; i++) {
2400 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2402 #endif
2404 /* if the line ends with a space, it means we want to complete the
2405 next arg */
2406 len = strlen(cmdline);
2407 if (len > 0 && isspace(cmdline[len - 1])) {
2408 if (nb_args >= MAX_ARGS)
2409 return;
2410 args[nb_args++] = qemu_strdup("");
2412 if (nb_args <= 1) {
2413 /* command completion */
2414 if (nb_args == 0)
2415 cmdname = "";
2416 else
2417 cmdname = args[0];
2418 completion_index = strlen(cmdname);
2419 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2420 cmd_completion(cmdname, cmd->name);
2422 } else {
2423 /* find the command */
2424 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2425 if (compare_cmd(args[0], cmd->name))
2426 goto found;
2428 return;
2429 found:
2430 ptype = cmd->args_type;
2431 for(i = 0; i < nb_args - 2; i++) {
2432 if (*ptype != '\0') {
2433 ptype++;
2434 while (*ptype == '?')
2435 ptype++;
2438 str = args[nb_args - 1];
2439 switch(*ptype) {
2440 case 'F':
2441 /* file completion */
2442 completion_index = strlen(str);
2443 file_completion(str);
2444 break;
2445 case 'B':
2446 /* block device name completion */
2447 completion_index = strlen(str);
2448 bdrv_iterate(block_completion_it, (void *)str);
2449 break;
2450 case 's':
2451 /* XXX: more generic ? */
2452 if (!strcmp(cmd->name, "info")) {
2453 completion_index = strlen(str);
2454 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2455 cmd_completion(str, cmd->name);
2457 } else if (!strcmp(cmd->name, "sendkey")) {
2458 completion_index = strlen(str);
2459 for(key = key_defs; key->name != NULL; key++) {
2460 cmd_completion(str, key->name);
2463 break;
2464 default:
2465 break;
2468 for(i = 0; i < nb_args; i++)
2469 qemu_free(args[i]);
2472 static int term_can_read(void *opaque)
2474 return 128;
2477 static void term_read(void *opaque, const uint8_t *buf, int size)
2479 int i;
2480 for(i = 0; i < size; i++)
2481 readline_handle_byte(buf[i]);
2484 static void monitor_start_input(void);
2486 static void monitor_handle_command1(void *opaque, const char *cmdline)
2488 monitor_handle_command(cmdline);
2489 monitor_start_input();
2492 static void monitor_start_input(void)
2494 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2497 static void term_event(void *opaque, int event)
2499 if (event != CHR_EVENT_RESET)
2500 return;
2502 if (!hide_banner)
2503 term_printf("QEMU %s monitor - type 'help' for more information\n",
2504 QEMU_VERSION);
2505 monitor_start_input();
2508 static int is_first_init = 1;
2510 void monitor_init(CharDriverState *hd, int show_banner)
2512 int i;
2514 if (is_first_init) {
2515 for (i = 0; i < MAX_MON; i++) {
2516 monitor_hd[i] = NULL;
2518 is_first_init = 0;
2520 for (i = 0; i < MAX_MON; i++) {
2521 if (monitor_hd[i] == NULL) {
2522 monitor_hd[i] = hd;
2523 break;
2527 hide_banner = !show_banner;
2529 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2532 /* XXX: use threads ? */
2533 /* modal monitor readline */
2534 static int monitor_readline_started;
2535 static char *monitor_readline_buf;
2536 static int monitor_readline_buf_size;
2538 static void monitor_readline_cb(void *opaque, const char *input)
2540 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2541 monitor_readline_started = 0;
2544 void monitor_readline(const char *prompt, int is_password,
2545 char *buf, int buf_size)
2547 int i;
2549 if (is_password) {
2550 for (i = 0; i < MAX_MON; i++)
2551 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2552 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2554 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2555 monitor_readline_buf = buf;
2556 monitor_readline_buf_size = buf_size;
2557 monitor_readline_started = 1;
2558 while (monitor_readline_started) {
2559 main_loop_wait(10);