Remove env->ready_for_interrupt_injection
[qemu-kvm/fedora.git] / monitor.c
blob9cfee4c9e893201ad503242b91dbb0316a4a3140
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw/hw.h"
25 #include "hw/usb.h"
26 #include "hw/pcmcia.h"
27 #include "hw/pc.h"
28 #include "hw/pci.h"
29 #include "gdbstub.h"
30 #include "net.h"
31 #include "qemu-char.h"
32 #include "sysemu.h"
33 #include "console.h"
34 #include "block.h"
35 #include "audio/audio.h"
36 #include "disas.h"
37 #include "migration.h"
38 #include <dirent.h>
40 #include "qemu-kvm.h"
41 #ifdef CONFIG_PROFILER
42 #include "qemu-timer.h" /* for ticks_per_sec */
43 #endif
45 //#define DEBUG
46 //#define DEBUG_COMPLETION
48 #ifndef offsetof
49 #define offsetof(type, field) ((size_t) &((type *)0)->field)
50 #endif
53 * Supported types:
55 * 'F' filename
56 * 'B' block device name
57 * 's' string (accept optional quote)
58 * 'i' 32 bit integer
59 * 'l' target long (32 or 64 bit)
60 * '/' optional gdb-like print format (like "/10x")
62 * '?' optional type (for 'F', 's' and 'i')
66 typedef struct term_cmd_t {
67 const char *name;
68 const char *args_type;
69 void (*handler)();
70 const char *params;
71 const char *help;
72 } term_cmd_t;
74 #define MAX_MON 4
75 static CharDriverState *monitor_hd[MAX_MON];
76 static int hide_banner;
78 static term_cmd_t term_cmds[];
79 static term_cmd_t info_cmds[];
81 static uint8_t term_outbuf[1024];
82 static int term_outbuf_index;
84 static void monitor_start_input(void);
86 CPUState *mon_cpu = NULL;
88 void term_flush(void)
90 int i;
91 if (term_outbuf_index > 0) {
92 for (i = 0; i < MAX_MON; i++)
93 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
94 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
95 term_outbuf_index = 0;
99 /* flush at every end of line or if the buffer is full */
100 void term_puts(const char *str)
102 char c;
103 for(;;) {
104 c = *str++;
105 if (c == '\0')
106 break;
107 if (c == '\n')
108 term_outbuf[term_outbuf_index++] = '\r';
109 term_outbuf[term_outbuf_index++] = c;
110 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
111 c == '\n')
112 term_flush();
116 void term_vprintf(const char *fmt, va_list ap)
118 char buf[4096];
119 vsnprintf(buf, sizeof(buf), fmt, ap);
120 term_puts(buf);
123 void term_printf(const char *fmt, ...)
125 va_list ap;
126 va_start(ap, fmt);
127 term_vprintf(fmt, ap);
128 va_end(ap);
131 void term_print_filename(const char *filename)
133 int i;
135 for (i = 0; filename[i]; i++) {
136 switch (filename[i]) {
137 case ' ':
138 case '"':
139 case '\\':
140 term_printf("\\%c", filename[i]);
141 break;
142 case '\t':
143 term_printf("\\t");
144 break;
145 case '\r':
146 term_printf("\\r");
147 break;
148 case '\n':
149 term_printf("\\n");
150 break;
151 default:
152 term_printf("%c", filename[i]);
153 break;
158 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
160 va_list ap;
161 va_start(ap, fmt);
162 term_vprintf(fmt, ap);
163 va_end(ap);
164 return 0;
167 static int compare_cmd(const char *name, const char *list)
169 const char *p, *pstart;
170 int len;
171 len = strlen(name);
172 p = list;
173 for(;;) {
174 pstart = p;
175 p = strchr(p, '|');
176 if (!p)
177 p = pstart + strlen(pstart);
178 if ((p - pstart) == len && !memcmp(pstart, name, len))
179 return 1;
180 if (*p == '\0')
181 break;
182 p++;
184 return 0;
187 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
189 term_cmd_t *cmd;
191 for(cmd = cmds; cmd->name != NULL; cmd++) {
192 if (!name || !strcmp(name, cmd->name))
193 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
197 static void help_cmd(const char *name)
199 if (name && !strcmp(name, "info")) {
200 help_cmd1(info_cmds, "info ", NULL);
201 } else {
202 help_cmd1(term_cmds, "", name);
203 if (name && !strcmp(name, "log")) {
204 CPULogItem *item;
205 term_printf("Log items (comma separated):\n");
206 term_printf("%-10s %s\n", "none", "remove all logs");
207 for(item = cpu_log_items; item->mask != 0; item++) {
208 term_printf("%-10s %s\n", item->name, item->help);
214 static void do_help(const char *name)
216 help_cmd(name);
219 static void do_commit(const char *device)
221 int i, all_devices;
223 all_devices = !strcmp(device, "all");
224 for (i = 0; i < nb_drives; i++) {
225 if (all_devices ||
226 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
227 bdrv_commit(drives_table[i].bdrv);
231 static void do_info(const char *item)
233 term_cmd_t *cmd;
235 if (!item)
236 goto help;
237 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
238 if (compare_cmd(item, cmd->name))
239 goto found;
241 help:
242 help_cmd("info");
243 return;
244 found:
245 cmd->handler();
248 static void do_info_version(void)
250 term_printf("%s\n", QEMU_VERSION);
253 static void do_info_name(void)
255 if (qemu_name)
256 term_printf("%s\n", qemu_name);
259 static void do_info_block(void)
261 bdrv_info();
264 static void do_info_blockstats(void)
266 bdrv_info_stats();
269 /* get the current CPU defined by the user */
270 static int mon_set_cpu(int cpu_index)
272 CPUState *env;
274 for(env = first_cpu; env != NULL; env = env->next_cpu) {
275 if (env->cpu_index == cpu_index) {
276 mon_cpu = env;
277 return 0;
280 return -1;
283 static CPUState *mon_get_cpu(void)
285 if (!mon_cpu) {
286 mon_set_cpu(0);
289 kvm_save_registers(mon_cpu);
291 return mon_cpu;
294 static void do_info_registers(void)
296 CPUState *env;
297 env = mon_get_cpu();
298 if (!env)
299 return;
300 #ifdef TARGET_I386
301 cpu_dump_state(env, NULL, monitor_fprintf,
302 X86_DUMP_FPU);
303 #else
304 cpu_dump_state(env, NULL, monitor_fprintf,
306 #endif
309 static void do_info_cpus(void)
311 CPUState *env;
313 /* just to set the default cpu if not already done */
314 mon_get_cpu();
316 for(env = first_cpu; env != NULL; env = env->next_cpu) {
317 kvm_save_registers(env);
318 term_printf("%c CPU #%d:",
319 (env == mon_cpu) ? '*' : ' ',
320 env->cpu_index);
321 #if defined(TARGET_I386)
322 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
323 #elif defined(TARGET_PPC)
324 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
325 #elif defined(TARGET_SPARC)
326 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
327 #elif defined(TARGET_MIPS)
328 term_printf(" PC=0x" TARGET_FMT_lx, env->PC[env->current_tc]);
329 #endif
330 if (env->halted)
331 term_printf(" (halted)");
332 term_printf(" thread_id=%d", env->thread_id);
333 term_printf("\n");
337 static void do_cpu_set(int index)
339 if (mon_set_cpu(index) < 0)
340 term_printf("Invalid CPU index\n");
343 static void do_cpu_set_nr(int value, const char *status)
345 int state;
347 if (!strcmp(status, "online"))
348 state = 1;
349 else if (!strcmp(status, "offline"))
350 state = 0;
351 else {
352 term_printf("invalid status: %s\n", status);
353 return;
355 #if defined(TARGET_I386) || defined(TARGET_X86_64)
356 qemu_system_cpu_hot_add(value, state);
357 #endif
360 static void do_info_jit(void)
362 dump_exec_info(NULL, monitor_fprintf);
365 static void do_info_history (void)
367 int i;
368 const char *str;
370 i = 0;
371 for(;;) {
372 str = readline_get_history(i);
373 if (!str)
374 break;
375 term_printf("%d: '%s'\n", i, str);
376 i++;
380 #if defined(TARGET_PPC)
381 /* XXX: not implemented in other targets */
382 static void do_info_cpu_stats (void)
384 CPUState *env;
386 env = mon_get_cpu();
387 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
389 #endif
391 static void do_quit(void)
393 exit(0);
396 static int eject_device(BlockDriverState *bs, int force)
398 if (bdrv_is_inserted(bs)) {
399 if (!force) {
400 if (!bdrv_is_removable(bs)) {
401 term_printf("device is not removable\n");
402 return -1;
404 if (bdrv_is_locked(bs)) {
405 term_printf("device is locked\n");
406 return -1;
409 bdrv_close(bs);
411 return 0;
414 static void do_eject(int force, const char *filename)
416 BlockDriverState *bs;
418 bs = bdrv_find(filename);
419 if (!bs) {
420 term_printf("device not found\n");
421 return;
423 eject_device(bs, force);
426 static void do_change_block(const char *device, const char *filename)
428 BlockDriverState *bs;
430 bs = bdrv_find(device);
431 if (!bs) {
432 term_printf("device not found\n");
433 return;
435 if (eject_device(bs, 0) < 0)
436 return;
437 bdrv_open(bs, filename, 0);
438 qemu_key_check(bs, filename);
441 static void do_change_vnc(const char *target)
443 if (strcmp(target, "passwd") == 0 ||
444 strcmp(target, "password") == 0) {
445 char password[9];
446 monitor_readline("Password: ", 1, password, sizeof(password)-1);
447 password[sizeof(password)-1] = '\0';
448 if (vnc_display_password(NULL, password) < 0)
449 term_printf("could not set VNC server password\n");
450 } else {
451 if (vnc_display_open(NULL, target) < 0)
452 term_printf("could not start VNC server on %s\n", target);
456 static void do_change(const char *device, const char *target)
458 if (strcmp(device, "vnc") == 0) {
459 do_change_vnc(target);
460 } else {
461 do_change_block(device, target);
465 static void do_screen_dump(const char *filename)
467 vga_hw_screen_dump(filename);
470 static void do_logfile(const char *filename)
472 cpu_set_log_filename(filename);
475 static void do_log(const char *items)
477 int mask;
479 if (!strcmp(items, "none")) {
480 mask = 0;
481 } else {
482 mask = cpu_str_to_log_mask(items);
483 if (!mask) {
484 help_cmd("log");
485 return;
488 cpu_set_log(mask);
491 static void do_stop(void)
493 vm_stop(EXCP_INTERRUPT);
496 static void do_cont(void)
498 vm_start();
501 #ifdef CONFIG_GDBSTUB
502 static void do_gdbserver(const char *port)
504 if (!port)
505 port = DEFAULT_GDBSTUB_PORT;
506 if (gdbserver_start(port) < 0) {
507 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
508 } else {
509 qemu_printf("Waiting gdb connection on port '%s'\n", port);
512 #endif
514 static void term_printc(int c)
516 term_printf("'");
517 switch(c) {
518 case '\'':
519 term_printf("\\'");
520 break;
521 case '\\':
522 term_printf("\\\\");
523 break;
524 case '\n':
525 term_printf("\\n");
526 break;
527 case '\r':
528 term_printf("\\r");
529 break;
530 default:
531 if (c >= 32 && c <= 126) {
532 term_printf("%c", c);
533 } else {
534 term_printf("\\x%02x", c);
536 break;
538 term_printf("'");
541 static void memory_dump(int count, int format, int wsize,
542 target_phys_addr_t addr, int is_physical)
544 CPUState *env;
545 int nb_per_line, l, line_size, i, max_digits, len;
546 uint8_t buf[16];
547 uint64_t v;
549 if (format == 'i') {
550 int flags;
551 flags = 0;
552 env = mon_get_cpu();
553 if (!env && !is_physical)
554 return;
555 #ifdef TARGET_I386
556 if (wsize == 2) {
557 flags = 1;
558 } else if (wsize == 4) {
559 flags = 0;
560 } else {
561 /* as default we use the current CS size */
562 flags = 0;
563 if (env) {
564 #ifdef TARGET_X86_64
565 if ((env->efer & MSR_EFER_LMA) &&
566 (env->segs[R_CS].flags & DESC_L_MASK))
567 flags = 2;
568 else
569 #endif
570 if (!(env->segs[R_CS].flags & DESC_B_MASK))
571 flags = 1;
574 #endif
575 monitor_disas(env, addr, count, is_physical, flags);
576 return;
579 len = wsize * count;
580 if (wsize == 1)
581 line_size = 8;
582 else
583 line_size = 16;
584 nb_per_line = line_size / wsize;
585 max_digits = 0;
587 switch(format) {
588 case 'o':
589 max_digits = (wsize * 8 + 2) / 3;
590 break;
591 default:
592 case 'x':
593 max_digits = (wsize * 8) / 4;
594 break;
595 case 'u':
596 case 'd':
597 max_digits = (wsize * 8 * 10 + 32) / 33;
598 break;
599 case 'c':
600 wsize = 1;
601 break;
604 while (len > 0) {
605 if (is_physical)
606 term_printf(TARGET_FMT_plx ":", addr);
607 else
608 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
609 l = len;
610 if (l > line_size)
611 l = line_size;
612 if (is_physical) {
613 cpu_physical_memory_rw(addr, buf, l, 0);
614 } else {
615 env = mon_get_cpu();
616 if (!env)
617 break;
618 cpu_memory_rw_debug(env, addr, buf, l, 0);
620 i = 0;
621 while (i < l) {
622 switch(wsize) {
623 default:
624 case 1:
625 v = ldub_raw(buf + i);
626 break;
627 case 2:
628 v = lduw_raw(buf + i);
629 break;
630 case 4:
631 v = (uint32_t)ldl_raw(buf + i);
632 break;
633 case 8:
634 v = ldq_raw(buf + i);
635 break;
637 term_printf(" ");
638 switch(format) {
639 case 'o':
640 term_printf("%#*" PRIo64, max_digits, v);
641 break;
642 case 'x':
643 term_printf("0x%0*" PRIx64, max_digits, v);
644 break;
645 case 'u':
646 term_printf("%*" PRIu64, max_digits, v);
647 break;
648 case 'd':
649 term_printf("%*" PRId64, max_digits, v);
650 break;
651 case 'c':
652 term_printc(v);
653 break;
655 i += wsize;
657 term_printf("\n");
658 addr += l;
659 len -= l;
663 #if TARGET_LONG_BITS == 64
664 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
665 #else
666 #define GET_TLONG(h, l) (l)
667 #endif
669 static void do_memory_dump(int count, int format, int size,
670 uint32_t addrh, uint32_t addrl)
672 target_long addr = GET_TLONG(addrh, addrl);
673 memory_dump(count, format, size, addr, 0);
676 #if TARGET_PHYS_ADDR_BITS > 32
677 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
678 #else
679 #define GET_TPHYSADDR(h, l) (l)
680 #endif
682 static void do_physical_memory_dump(int count, int format, int size,
683 uint32_t addrh, uint32_t addrl)
686 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
687 memory_dump(count, format, size, addr, 1);
690 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
692 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
693 #if TARGET_PHYS_ADDR_BITS == 32
694 switch(format) {
695 case 'o':
696 term_printf("%#o", val);
697 break;
698 case 'x':
699 term_printf("%#x", val);
700 break;
701 case 'u':
702 term_printf("%u", val);
703 break;
704 default:
705 case 'd':
706 term_printf("%d", val);
707 break;
708 case 'c':
709 term_printc(val);
710 break;
712 #else
713 switch(format) {
714 case 'o':
715 term_printf("%#" PRIo64, val);
716 break;
717 case 'x':
718 term_printf("%#" PRIx64, val);
719 break;
720 case 'u':
721 term_printf("%" PRIu64, val);
722 break;
723 default:
724 case 'd':
725 term_printf("%" PRId64, val);
726 break;
727 case 'c':
728 term_printc(val);
729 break;
731 #endif
732 term_printf("\n");
735 static void do_memory_save(unsigned int valh, unsigned int vall,
736 uint32_t size, const char *filename)
738 FILE *f;
739 target_long addr = GET_TLONG(valh, vall);
740 uint32_t l;
741 CPUState *env;
742 uint8_t buf[1024];
744 env = mon_get_cpu();
745 if (!env)
746 return;
748 f = fopen(filename, "wb");
749 if (!f) {
750 term_printf("could not open '%s'\n", filename);
751 return;
753 while (size != 0) {
754 l = sizeof(buf);
755 if (l > size)
756 l = size;
757 cpu_memory_rw_debug(env, addr, buf, l, 0);
758 fwrite(buf, 1, l, f);
759 addr += l;
760 size -= l;
762 fclose(f);
765 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
766 uint32_t size, const char *filename)
768 FILE *f;
769 uint32_t l;
770 uint8_t buf[1024];
771 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
773 f = fopen(filename, "wb");
774 if (!f) {
775 term_printf("could not open '%s'\n", filename);
776 return;
778 while (size != 0) {
779 l = sizeof(buf);
780 if (l > size)
781 l = size;
782 cpu_physical_memory_rw(addr, buf, l, 0);
783 fwrite(buf, 1, l, f);
784 fflush(f);
785 addr += l;
786 size -= l;
788 fclose(f);
791 static void do_sum(uint32_t start, uint32_t size)
793 uint32_t addr;
794 uint8_t buf[1];
795 uint16_t sum;
797 sum = 0;
798 for(addr = start; addr < (start + size); addr++) {
799 cpu_physical_memory_rw(addr, buf, 1, 0);
800 /* BSD sum algorithm ('sum' Unix command) */
801 sum = (sum >> 1) | (sum << 15);
802 sum += buf[0];
804 term_printf("%05d\n", sum);
807 typedef struct {
808 int keycode;
809 const char *name;
810 } KeyDef;
812 static const KeyDef key_defs[] = {
813 { 0x2a, "shift" },
814 { 0x36, "shift_r" },
816 { 0x38, "alt" },
817 { 0xb8, "alt_r" },
818 { 0x1d, "ctrl" },
819 { 0x9d, "ctrl_r" },
821 { 0xdd, "menu" },
823 { 0x01, "esc" },
825 { 0x02, "1" },
826 { 0x03, "2" },
827 { 0x04, "3" },
828 { 0x05, "4" },
829 { 0x06, "5" },
830 { 0x07, "6" },
831 { 0x08, "7" },
832 { 0x09, "8" },
833 { 0x0a, "9" },
834 { 0x0b, "0" },
835 { 0x0c, "minus" },
836 { 0x0d, "equal" },
837 { 0x0e, "backspace" },
839 { 0x0f, "tab" },
840 { 0x10, "q" },
841 { 0x11, "w" },
842 { 0x12, "e" },
843 { 0x13, "r" },
844 { 0x14, "t" },
845 { 0x15, "y" },
846 { 0x16, "u" },
847 { 0x17, "i" },
848 { 0x18, "o" },
849 { 0x19, "p" },
851 { 0x1c, "ret" },
853 { 0x1e, "a" },
854 { 0x1f, "s" },
855 { 0x20, "d" },
856 { 0x21, "f" },
857 { 0x22, "g" },
858 { 0x23, "h" },
859 { 0x24, "j" },
860 { 0x25, "k" },
861 { 0x26, "l" },
863 { 0x2c, "z" },
864 { 0x2d, "x" },
865 { 0x2e, "c" },
866 { 0x2f, "v" },
867 { 0x30, "b" },
868 { 0x31, "n" },
869 { 0x32, "m" },
871 { 0x37, "asterisk" },
873 { 0x39, "spc" },
874 { 0x3a, "caps_lock" },
875 { 0x3b, "f1" },
876 { 0x3c, "f2" },
877 { 0x3d, "f3" },
878 { 0x3e, "f4" },
879 { 0x3f, "f5" },
880 { 0x40, "f6" },
881 { 0x41, "f7" },
882 { 0x42, "f8" },
883 { 0x43, "f9" },
884 { 0x44, "f10" },
885 { 0x45, "num_lock" },
886 { 0x46, "scroll_lock" },
888 { 0xb5, "kp_divide" },
889 { 0x37, "kp_multiply" },
890 { 0x4a, "kp_subtract" },
891 { 0x4e, "kp_add" },
892 { 0x9c, "kp_enter" },
893 { 0x53, "kp_decimal" },
894 { 0x54, "sysrq" },
896 { 0x52, "kp_0" },
897 { 0x4f, "kp_1" },
898 { 0x50, "kp_2" },
899 { 0x51, "kp_3" },
900 { 0x4b, "kp_4" },
901 { 0x4c, "kp_5" },
902 { 0x4d, "kp_6" },
903 { 0x47, "kp_7" },
904 { 0x48, "kp_8" },
905 { 0x49, "kp_9" },
907 { 0x56, "<" },
909 { 0x57, "f11" },
910 { 0x58, "f12" },
912 { 0xb7, "print" },
914 { 0xc7, "home" },
915 { 0xc9, "pgup" },
916 { 0xd1, "pgdn" },
917 { 0xcf, "end" },
919 { 0xcb, "left" },
920 { 0xc8, "up" },
921 { 0xd0, "down" },
922 { 0xcd, "right" },
924 { 0xd2, "insert" },
925 { 0xd3, "delete" },
926 { 0, NULL },
929 static int get_keycode(const char *key)
931 const KeyDef *p;
932 char *endp;
933 int ret;
935 for(p = key_defs; p->name != NULL; p++) {
936 if (!strcmp(key, p->name))
937 return p->keycode;
939 if (strstart(key, "0x", NULL)) {
940 ret = strtoul(key, &endp, 0);
941 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
942 return ret;
944 return -1;
947 static void do_sendkey(const char *string)
949 uint8_t keycodes[16];
950 int nb_keycodes = 0;
951 char keyname_buf[16];
952 char *separator;
953 int keyname_len, keycode, i;
955 while (1) {
956 separator = strchr(string, '-');
957 keyname_len = separator ? separator - string : strlen(string);
958 if (keyname_len > 0) {
959 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
960 if (keyname_len > sizeof(keyname_buf) - 1) {
961 term_printf("invalid key: '%s...'\n", keyname_buf);
962 return;
964 if (nb_keycodes == sizeof(keycodes)) {
965 term_printf("too many keys\n");
966 return;
968 keyname_buf[keyname_len] = 0;
969 keycode = get_keycode(keyname_buf);
970 if (keycode < 0) {
971 term_printf("unknown key: '%s'\n", keyname_buf);
972 return;
974 keycodes[nb_keycodes++] = keycode;
976 if (!separator)
977 break;
978 string = separator + 1;
980 /* key down events */
981 for(i = 0; i < nb_keycodes; i++) {
982 keycode = keycodes[i];
983 if (keycode & 0x80)
984 kbd_put_keycode(0xe0);
985 kbd_put_keycode(keycode & 0x7f);
987 /* key up events */
988 for(i = nb_keycodes - 1; i >= 0; i--) {
989 keycode = keycodes[i];
990 if (keycode & 0x80)
991 kbd_put_keycode(0xe0);
992 kbd_put_keycode(keycode | 0x80);
996 static int mouse_button_state;
998 static void do_mouse_move(const char *dx_str, const char *dy_str,
999 const char *dz_str)
1001 int dx, dy, dz;
1002 dx = strtol(dx_str, NULL, 0);
1003 dy = strtol(dy_str, NULL, 0);
1004 dz = 0;
1005 if (dz_str)
1006 dz = strtol(dz_str, NULL, 0);
1007 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1010 static void do_mouse_button(int button_state)
1012 mouse_button_state = button_state;
1013 kbd_mouse_event(0, 0, 0, mouse_button_state);
1016 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1018 uint32_t val;
1019 int suffix;
1021 if (has_index) {
1022 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1023 addr++;
1025 addr &= 0xffff;
1027 switch(size) {
1028 default:
1029 case 1:
1030 val = cpu_inb(NULL, addr);
1031 suffix = 'b';
1032 break;
1033 case 2:
1034 val = cpu_inw(NULL, addr);
1035 suffix = 'w';
1036 break;
1037 case 4:
1038 val = cpu_inl(NULL, addr);
1039 suffix = 'l';
1040 break;
1042 term_printf("port%c[0x%04x] = %#0*x\n",
1043 suffix, addr, size * 2, val);
1046 static void do_boot_set(const char *bootdevice)
1048 int res;
1050 if (qemu_boot_set_handler) {
1051 res = qemu_boot_set_handler(bootdevice);
1052 if (res == 0)
1053 term_printf("boot device list now set to %s\n", bootdevice);
1054 else
1055 term_printf("setting boot device list failed with error %i\n", res);
1056 } else {
1057 term_printf("no function defined to set boot device list for this architecture\n");
1061 static void do_system_reset(void)
1063 qemu_system_reset_request();
1066 static void do_system_powerdown(void)
1068 qemu_system_powerdown_request();
1071 #if defined(TARGET_I386)
1072 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1074 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1075 addr,
1076 pte & mask,
1077 pte & PG_GLOBAL_MASK ? 'G' : '-',
1078 pte & PG_PSE_MASK ? 'P' : '-',
1079 pte & PG_DIRTY_MASK ? 'D' : '-',
1080 pte & PG_ACCESSED_MASK ? 'A' : '-',
1081 pte & PG_PCD_MASK ? 'C' : '-',
1082 pte & PG_PWT_MASK ? 'T' : '-',
1083 pte & PG_USER_MASK ? 'U' : '-',
1084 pte & PG_RW_MASK ? 'W' : '-');
1087 static void tlb_info(void)
1089 CPUState *env;
1090 int l1, l2;
1091 uint32_t pgd, pde, pte;
1093 env = mon_get_cpu();
1094 if (!env)
1095 return;
1097 if (!(env->cr[0] & CR0_PG_MASK)) {
1098 term_printf("PG disabled\n");
1099 return;
1101 pgd = env->cr[3] & ~0xfff;
1102 for(l1 = 0; l1 < 1024; l1++) {
1103 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1104 pde = le32_to_cpu(pde);
1105 if (pde & PG_PRESENT_MASK) {
1106 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1107 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1108 } else {
1109 for(l2 = 0; l2 < 1024; l2++) {
1110 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1111 (uint8_t *)&pte, 4);
1112 pte = le32_to_cpu(pte);
1113 if (pte & PG_PRESENT_MASK) {
1114 print_pte((l1 << 22) + (l2 << 12),
1115 pte & ~PG_PSE_MASK,
1116 ~0xfff);
1124 static void mem_print(uint32_t *pstart, int *plast_prot,
1125 uint32_t end, int prot)
1127 int prot1;
1128 prot1 = *plast_prot;
1129 if (prot != prot1) {
1130 if (*pstart != -1) {
1131 term_printf("%08x-%08x %08x %c%c%c\n",
1132 *pstart, end, end - *pstart,
1133 prot1 & PG_USER_MASK ? 'u' : '-',
1134 'r',
1135 prot1 & PG_RW_MASK ? 'w' : '-');
1137 if (prot != 0)
1138 *pstart = end;
1139 else
1140 *pstart = -1;
1141 *plast_prot = prot;
1145 static void mem_info(void)
1147 CPUState *env;
1148 int l1, l2, prot, last_prot;
1149 uint32_t pgd, pde, pte, start, end;
1151 env = mon_get_cpu();
1152 if (!env)
1153 return;
1155 if (!(env->cr[0] & CR0_PG_MASK)) {
1156 term_printf("PG disabled\n");
1157 return;
1159 pgd = env->cr[3] & ~0xfff;
1160 last_prot = 0;
1161 start = -1;
1162 for(l1 = 0; l1 < 1024; l1++) {
1163 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1164 pde = le32_to_cpu(pde);
1165 end = l1 << 22;
1166 if (pde & PG_PRESENT_MASK) {
1167 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1168 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1169 mem_print(&start, &last_prot, end, prot);
1170 } else {
1171 for(l2 = 0; l2 < 1024; l2++) {
1172 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1173 (uint8_t *)&pte, 4);
1174 pte = le32_to_cpu(pte);
1175 end = (l1 << 22) + (l2 << 12);
1176 if (pte & PG_PRESENT_MASK) {
1177 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1178 } else {
1179 prot = 0;
1181 mem_print(&start, &last_prot, end, prot);
1184 } else {
1185 prot = 0;
1186 mem_print(&start, &last_prot, end, prot);
1190 #endif
1192 static void do_info_kqemu(void)
1194 #ifdef USE_KQEMU
1195 CPUState *env;
1196 int val;
1197 val = 0;
1198 env = mon_get_cpu();
1199 if (!env) {
1200 term_printf("No cpu initialized yet");
1201 return;
1203 val = env->kqemu_enabled;
1204 term_printf("kqemu support: ");
1205 switch(val) {
1206 default:
1207 case 0:
1208 term_printf("disabled\n");
1209 break;
1210 case 1:
1211 term_printf("enabled for user code\n");
1212 break;
1213 case 2:
1214 term_printf("enabled for user and kernel code\n");
1215 break;
1217 #else
1218 term_printf("kqemu support: not compiled\n");
1219 #endif
1222 #ifdef CONFIG_PROFILER
1224 int64_t kqemu_time;
1225 int64_t qemu_time;
1226 int64_t kqemu_exec_count;
1227 int64_t dev_time;
1228 int64_t kqemu_ret_int_count;
1229 int64_t kqemu_ret_excp_count;
1230 int64_t kqemu_ret_intr_count;
1232 static void do_info_profile(void)
1234 int64_t total;
1235 total = qemu_time;
1236 if (total == 0)
1237 total = 1;
1238 term_printf("async time %" PRId64 " (%0.3f)\n",
1239 dev_time, dev_time / (double)ticks_per_sec);
1240 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1241 qemu_time, qemu_time / (double)ticks_per_sec);
1242 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1243 kqemu_time, kqemu_time / (double)ticks_per_sec,
1244 kqemu_time / (double)total * 100.0,
1245 kqemu_exec_count,
1246 kqemu_ret_int_count,
1247 kqemu_ret_excp_count,
1248 kqemu_ret_intr_count);
1249 qemu_time = 0;
1250 kqemu_time = 0;
1251 kqemu_exec_count = 0;
1252 dev_time = 0;
1253 kqemu_ret_int_count = 0;
1254 kqemu_ret_excp_count = 0;
1255 kqemu_ret_intr_count = 0;
1256 #ifdef USE_KQEMU
1257 kqemu_record_dump();
1258 #endif
1260 #else
1261 static void do_info_profile(void)
1263 term_printf("Internal profiler not compiled\n");
1265 #endif
1267 /* Capture support */
1268 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1270 static void do_info_capture (void)
1272 int i;
1273 CaptureState *s;
1275 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1276 term_printf ("[%d]: ", i);
1277 s->ops.info (s->opaque);
1281 static void do_stop_capture (int n)
1283 int i;
1284 CaptureState *s;
1286 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1287 if (i == n) {
1288 s->ops.destroy (s->opaque);
1289 LIST_REMOVE (s, entries);
1290 qemu_free (s);
1291 return;
1296 #ifdef HAS_AUDIO
1297 int wav_start_capture (CaptureState *s, const char *path, int freq,
1298 int bits, int nchannels);
1300 static void do_wav_capture (const char *path,
1301 int has_freq, int freq,
1302 int has_bits, int bits,
1303 int has_channels, int nchannels)
1305 CaptureState *s;
1307 s = qemu_mallocz (sizeof (*s));
1308 if (!s) {
1309 term_printf ("Not enough memory to add wave capture\n");
1310 return;
1313 freq = has_freq ? freq : 44100;
1314 bits = has_bits ? bits : 16;
1315 nchannels = has_channels ? nchannels : 2;
1317 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1318 term_printf ("Faied to add wave capture\n");
1319 qemu_free (s);
1321 LIST_INSERT_HEAD (&capture_head, s, entries);
1323 #endif
1325 #if defined(TARGET_I386)
1326 static void do_inject_nmi(int cpu_index)
1328 CPUState *env;
1330 for (env = first_cpu; env != NULL; env = env->next_cpu)
1331 if (env->cpu_index == cpu_index) {
1332 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1333 break;
1336 #endif
1338 static term_cmd_t term_cmds[] = {
1339 { "help|?", "s?", do_help,
1340 "[cmd]", "show the help" },
1341 { "commit", "s", do_commit,
1342 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1343 { "info", "s?", do_info,
1344 "subcommand", "show various information about the system state" },
1345 { "q|quit", "", do_quit,
1346 "", "quit the emulator" },
1347 { "eject", "-fB", do_eject,
1348 "[-f] device", "eject a removable medium (use -f to force it)" },
1349 { "change", "BF", do_change,
1350 "device filename", "change a removable medium" },
1351 { "screendump", "F", do_screen_dump,
1352 "filename", "save screen into PPM image 'filename'" },
1353 { "logfile", "F", do_logfile,
1354 "filename", "output logs to 'filename'" },
1355 { "log", "s", do_log,
1356 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1357 { "savevm", "s?", do_savevm,
1358 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1359 { "loadvm", "s", do_loadvm,
1360 "tag|id", "restore a VM snapshot from its tag or id" },
1361 { "delvm", "s", do_delvm,
1362 "tag|id", "delete a VM snapshot from its tag or id" },
1363 { "stop", "", do_stop,
1364 "", "stop emulation", },
1365 { "c|cont", "", do_cont,
1366 "", "resume emulation", },
1367 #ifdef CONFIG_GDBSTUB
1368 { "gdbserver", "s?", do_gdbserver,
1369 "[port]", "start gdbserver session (default port=1234)", },
1370 #endif
1371 { "x", "/l", do_memory_dump,
1372 "/fmt addr", "virtual memory dump starting at 'addr'", },
1373 { "xp", "/l", do_physical_memory_dump,
1374 "/fmt addr", "physical memory dump starting at 'addr'", },
1375 { "p|print", "/l", do_print,
1376 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1377 { "i", "/ii.", do_ioport_read,
1378 "/fmt addr", "I/O port read" },
1380 { "sendkey", "s", do_sendkey,
1381 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1382 { "system_reset", "", do_system_reset,
1383 "", "reset the system" },
1384 { "system_powerdown", "", do_system_powerdown,
1385 "", "send system power down event" },
1386 { "sum", "ii", do_sum,
1387 "addr size", "compute the checksum of a memory region" },
1388 { "usb_add", "s", do_usb_add,
1389 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1390 { "usb_del", "s", do_usb_del,
1391 "device", "remove USB device 'bus.addr'" },
1392 { "cpu", "i", do_cpu_set,
1393 "index", "set the default CPU" },
1394 { "mouse_move", "sss?", do_mouse_move,
1395 "dx dy [dz]", "send mouse move events" },
1396 { "mouse_button", "i", do_mouse_button,
1397 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1398 { "mouse_set", "i", do_mouse_set,
1399 "index", "set which mouse device receives events" },
1400 #ifdef HAS_AUDIO
1401 { "wavcapture", "si?i?i?", do_wav_capture,
1402 "path [frequency bits channels]",
1403 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1404 #endif
1405 { "stopcapture", "i", do_stop_capture,
1406 "capture index", "stop capture" },
1407 { "memsave", "lis", do_memory_save,
1408 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1409 { "pmemsave", "lis", do_physical_memory_save,
1410 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1411 { "boot_set", "s", do_boot_set,
1412 "bootdevice", "define new values for the boot device list" },
1413 #if defined(TARGET_I386)
1414 { "nmi", "i", do_inject_nmi,
1415 "cpu", "inject an NMI on the given CPU", },
1416 #endif
1417 { "migrate", "-ds", do_migrate,
1418 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1419 { "migrate_cancel", "", do_migrate_cancel,
1420 "", "cancel the current VM migration" },
1421 { "migrate_set_speed", "s", do_migrate_set_speed,
1422 "value", "set maximum speed (in bytes) for migrations" },
1423 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1424 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1425 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1426 "[,unit=m][,media=d][index=i]\n"
1427 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1428 "[snapshot=on|off][,cache=on|off]",
1429 "add drive to PCI storage controller" },
1430 { "pci_add", "iss", device_hot_add, "bus nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1431 { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" },
1432 #endif
1433 { NULL, NULL, },
1436 static term_cmd_t info_cmds[] = {
1437 { "version", "", do_info_version,
1438 "", "show the version of qemu" },
1439 { "network", "", do_info_network,
1440 "", "show the network state" },
1441 { "block", "", do_info_block,
1442 "", "show the block devices" },
1443 { "blockstats", "", do_info_blockstats,
1444 "", "show block device statistics" },
1445 { "registers", "", do_info_registers,
1446 "", "show the cpu registers" },
1447 { "cpus", "", do_info_cpus,
1448 "", "show infos for each CPU" },
1449 { "history", "", do_info_history,
1450 "", "show the command line history", },
1451 { "irq", "", irq_info,
1452 "", "show the interrupts statistics (if available)", },
1453 { "pic", "", pic_info,
1454 "", "show i8259 (PIC) state", },
1455 { "pci", "", pci_info,
1456 "", "show PCI info", },
1457 #if defined(TARGET_I386)
1458 { "tlb", "", tlb_info,
1459 "", "show virtual to physical memory mappings", },
1460 { "mem", "", mem_info,
1461 "", "show the active virtual memory mappings", },
1462 #endif
1463 { "jit", "", do_info_jit,
1464 "", "show dynamic compiler info", },
1465 { "kqemu", "", do_info_kqemu,
1466 "", "show kqemu information", },
1467 { "usb", "", usb_info,
1468 "", "show guest USB devices", },
1469 { "usbhost", "", usb_host_info,
1470 "", "show host USB devices", },
1471 { "profile", "", do_info_profile,
1472 "", "show profiling information", },
1473 { "capture", "", do_info_capture,
1474 "", "show capture information" },
1475 { "snapshots", "", do_info_snapshots,
1476 "", "show the currently saved VM snapshots" },
1477 { "pcmcia", "", pcmcia_info,
1478 "", "show guest PCMCIA status" },
1479 { "mice", "", do_info_mice,
1480 "", "show which guest mouse is receiving events" },
1481 { "vnc", "", do_info_vnc,
1482 "", "show the vnc server status"},
1483 { "name", "", do_info_name,
1484 "", "show the current VM name" },
1485 #if defined(TARGET_PPC)
1486 { "cpustats", "", do_info_cpu_stats,
1487 "", "show CPU statistics", },
1488 #endif
1489 #if defined(CONFIG_SLIRP)
1490 { "slirp", "", do_info_slirp,
1491 "", "show SLIRP statistics", },
1492 #endif
1493 { "migration", "", do_info_migration,
1494 "", "show migration information" },
1495 { NULL, NULL, },
1498 /*******************************************************************/
1500 static const char *pch;
1501 static jmp_buf expr_env;
1503 #define MD_TLONG 0
1504 #define MD_I32 1
1506 typedef struct MonitorDef {
1507 const char *name;
1508 int offset;
1509 target_long (*get_value)(struct MonitorDef *md, int val);
1510 int type;
1511 } MonitorDef;
1513 #if defined(TARGET_I386)
1514 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1516 CPUState *env = mon_get_cpu();
1517 if (!env)
1518 return 0;
1519 return env->eip + env->segs[R_CS].base;
1521 #endif
1523 #if defined(TARGET_PPC)
1524 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1526 CPUState *env = mon_get_cpu();
1527 unsigned int u;
1528 int i;
1530 if (!env)
1531 return 0;
1533 u = 0;
1534 for (i = 0; i < 8; i++)
1535 u |= env->crf[i] << (32 - (4 * i));
1537 return u;
1540 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1542 CPUState *env = mon_get_cpu();
1543 if (!env)
1544 return 0;
1545 return env->msr;
1548 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1550 CPUState *env = mon_get_cpu();
1551 if (!env)
1552 return 0;
1553 return ppc_load_xer(env);
1556 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1558 CPUState *env = mon_get_cpu();
1559 if (!env)
1560 return 0;
1561 return cpu_ppc_load_decr(env);
1564 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1566 CPUState *env = mon_get_cpu();
1567 if (!env)
1568 return 0;
1569 return cpu_ppc_load_tbu(env);
1572 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1574 CPUState *env = mon_get_cpu();
1575 if (!env)
1576 return 0;
1577 return cpu_ppc_load_tbl(env);
1579 #endif
1581 #if defined(TARGET_SPARC)
1582 #ifndef TARGET_SPARC64
1583 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1585 CPUState *env = mon_get_cpu();
1586 if (!env)
1587 return 0;
1588 return GET_PSR(env);
1590 #endif
1592 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1594 CPUState *env = mon_get_cpu();
1595 if (!env)
1596 return 0;
1597 return env->regwptr[val];
1599 #endif
1601 static MonitorDef monitor_defs[] = {
1602 #ifdef TARGET_I386
1604 #define SEG(name, seg) \
1605 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1606 { name ".base", offsetof(CPUState, segs[seg].base) },\
1607 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1609 { "eax", offsetof(CPUState, regs[0]) },
1610 { "ecx", offsetof(CPUState, regs[1]) },
1611 { "edx", offsetof(CPUState, regs[2]) },
1612 { "ebx", offsetof(CPUState, regs[3]) },
1613 { "esp|sp", offsetof(CPUState, regs[4]) },
1614 { "ebp|fp", offsetof(CPUState, regs[5]) },
1615 { "esi", offsetof(CPUState, regs[6]) },
1616 { "edi", offsetof(CPUState, regs[7]) },
1617 #ifdef TARGET_X86_64
1618 { "r8", offsetof(CPUState, regs[8]) },
1619 { "r9", offsetof(CPUState, regs[9]) },
1620 { "r10", offsetof(CPUState, regs[10]) },
1621 { "r11", offsetof(CPUState, regs[11]) },
1622 { "r12", offsetof(CPUState, regs[12]) },
1623 { "r13", offsetof(CPUState, regs[13]) },
1624 { "r14", offsetof(CPUState, regs[14]) },
1625 { "r15", offsetof(CPUState, regs[15]) },
1626 #endif
1627 { "eflags", offsetof(CPUState, eflags) },
1628 { "eip", offsetof(CPUState, eip) },
1629 SEG("cs", R_CS)
1630 SEG("ds", R_DS)
1631 SEG("es", R_ES)
1632 SEG("ss", R_SS)
1633 SEG("fs", R_FS)
1634 SEG("gs", R_GS)
1635 { "pc", 0, monitor_get_pc, },
1636 #elif defined(TARGET_PPC)
1637 /* General purpose registers */
1638 { "r0", offsetof(CPUState, gpr[0]) },
1639 { "r1", offsetof(CPUState, gpr[1]) },
1640 { "r2", offsetof(CPUState, gpr[2]) },
1641 { "r3", offsetof(CPUState, gpr[3]) },
1642 { "r4", offsetof(CPUState, gpr[4]) },
1643 { "r5", offsetof(CPUState, gpr[5]) },
1644 { "r6", offsetof(CPUState, gpr[6]) },
1645 { "r7", offsetof(CPUState, gpr[7]) },
1646 { "r8", offsetof(CPUState, gpr[8]) },
1647 { "r9", offsetof(CPUState, gpr[9]) },
1648 { "r10", offsetof(CPUState, gpr[10]) },
1649 { "r11", offsetof(CPUState, gpr[11]) },
1650 { "r12", offsetof(CPUState, gpr[12]) },
1651 { "r13", offsetof(CPUState, gpr[13]) },
1652 { "r14", offsetof(CPUState, gpr[14]) },
1653 { "r15", offsetof(CPUState, gpr[15]) },
1654 { "r16", offsetof(CPUState, gpr[16]) },
1655 { "r17", offsetof(CPUState, gpr[17]) },
1656 { "r18", offsetof(CPUState, gpr[18]) },
1657 { "r19", offsetof(CPUState, gpr[19]) },
1658 { "r20", offsetof(CPUState, gpr[20]) },
1659 { "r21", offsetof(CPUState, gpr[21]) },
1660 { "r22", offsetof(CPUState, gpr[22]) },
1661 { "r23", offsetof(CPUState, gpr[23]) },
1662 { "r24", offsetof(CPUState, gpr[24]) },
1663 { "r25", offsetof(CPUState, gpr[25]) },
1664 { "r26", offsetof(CPUState, gpr[26]) },
1665 { "r27", offsetof(CPUState, gpr[27]) },
1666 { "r28", offsetof(CPUState, gpr[28]) },
1667 { "r29", offsetof(CPUState, gpr[29]) },
1668 { "r30", offsetof(CPUState, gpr[30]) },
1669 { "r31", offsetof(CPUState, gpr[31]) },
1670 /* Floating point registers */
1671 { "f0", offsetof(CPUState, fpr[0]) },
1672 { "f1", offsetof(CPUState, fpr[1]) },
1673 { "f2", offsetof(CPUState, fpr[2]) },
1674 { "f3", offsetof(CPUState, fpr[3]) },
1675 { "f4", offsetof(CPUState, fpr[4]) },
1676 { "f5", offsetof(CPUState, fpr[5]) },
1677 { "f6", offsetof(CPUState, fpr[6]) },
1678 { "f7", offsetof(CPUState, fpr[7]) },
1679 { "f8", offsetof(CPUState, fpr[8]) },
1680 { "f9", offsetof(CPUState, fpr[9]) },
1681 { "f10", offsetof(CPUState, fpr[10]) },
1682 { "f11", offsetof(CPUState, fpr[11]) },
1683 { "f12", offsetof(CPUState, fpr[12]) },
1684 { "f13", offsetof(CPUState, fpr[13]) },
1685 { "f14", offsetof(CPUState, fpr[14]) },
1686 { "f15", offsetof(CPUState, fpr[15]) },
1687 { "f16", offsetof(CPUState, fpr[16]) },
1688 { "f17", offsetof(CPUState, fpr[17]) },
1689 { "f18", offsetof(CPUState, fpr[18]) },
1690 { "f19", offsetof(CPUState, fpr[19]) },
1691 { "f20", offsetof(CPUState, fpr[20]) },
1692 { "f21", offsetof(CPUState, fpr[21]) },
1693 { "f22", offsetof(CPUState, fpr[22]) },
1694 { "f23", offsetof(CPUState, fpr[23]) },
1695 { "f24", offsetof(CPUState, fpr[24]) },
1696 { "f25", offsetof(CPUState, fpr[25]) },
1697 { "f26", offsetof(CPUState, fpr[26]) },
1698 { "f27", offsetof(CPUState, fpr[27]) },
1699 { "f28", offsetof(CPUState, fpr[28]) },
1700 { "f29", offsetof(CPUState, fpr[29]) },
1701 { "f30", offsetof(CPUState, fpr[30]) },
1702 { "f31", offsetof(CPUState, fpr[31]) },
1703 { "fpscr", offsetof(CPUState, fpscr) },
1704 /* Next instruction pointer */
1705 { "nip|pc", offsetof(CPUState, nip) },
1706 { "lr", offsetof(CPUState, lr) },
1707 { "ctr", offsetof(CPUState, ctr) },
1708 { "decr", 0, &monitor_get_decr, },
1709 { "ccr", 0, &monitor_get_ccr, },
1710 /* Machine state register */
1711 { "msr", 0, &monitor_get_msr, },
1712 { "xer", 0, &monitor_get_xer, },
1713 { "tbu", 0, &monitor_get_tbu, },
1714 { "tbl", 0, &monitor_get_tbl, },
1715 #if defined(TARGET_PPC64)
1716 /* Address space register */
1717 { "asr", offsetof(CPUState, asr) },
1718 #endif
1719 /* Segment registers */
1720 { "sdr1", offsetof(CPUState, sdr1) },
1721 { "sr0", offsetof(CPUState, sr[0]) },
1722 { "sr1", offsetof(CPUState, sr[1]) },
1723 { "sr2", offsetof(CPUState, sr[2]) },
1724 { "sr3", offsetof(CPUState, sr[3]) },
1725 { "sr4", offsetof(CPUState, sr[4]) },
1726 { "sr5", offsetof(CPUState, sr[5]) },
1727 { "sr6", offsetof(CPUState, sr[6]) },
1728 { "sr7", offsetof(CPUState, sr[7]) },
1729 { "sr8", offsetof(CPUState, sr[8]) },
1730 { "sr9", offsetof(CPUState, sr[9]) },
1731 { "sr10", offsetof(CPUState, sr[10]) },
1732 { "sr11", offsetof(CPUState, sr[11]) },
1733 { "sr12", offsetof(CPUState, sr[12]) },
1734 { "sr13", offsetof(CPUState, sr[13]) },
1735 { "sr14", offsetof(CPUState, sr[14]) },
1736 { "sr15", offsetof(CPUState, sr[15]) },
1737 /* Too lazy to put BATs and SPRs ... */
1738 #elif defined(TARGET_SPARC)
1739 { "g0", offsetof(CPUState, gregs[0]) },
1740 { "g1", offsetof(CPUState, gregs[1]) },
1741 { "g2", offsetof(CPUState, gregs[2]) },
1742 { "g3", offsetof(CPUState, gregs[3]) },
1743 { "g4", offsetof(CPUState, gregs[4]) },
1744 { "g5", offsetof(CPUState, gregs[5]) },
1745 { "g6", offsetof(CPUState, gregs[6]) },
1746 { "g7", offsetof(CPUState, gregs[7]) },
1747 { "o0", 0, monitor_get_reg },
1748 { "o1", 1, monitor_get_reg },
1749 { "o2", 2, monitor_get_reg },
1750 { "o3", 3, monitor_get_reg },
1751 { "o4", 4, monitor_get_reg },
1752 { "o5", 5, monitor_get_reg },
1753 { "o6", 6, monitor_get_reg },
1754 { "o7", 7, monitor_get_reg },
1755 { "l0", 8, monitor_get_reg },
1756 { "l1", 9, monitor_get_reg },
1757 { "l2", 10, monitor_get_reg },
1758 { "l3", 11, monitor_get_reg },
1759 { "l4", 12, monitor_get_reg },
1760 { "l5", 13, monitor_get_reg },
1761 { "l6", 14, monitor_get_reg },
1762 { "l7", 15, monitor_get_reg },
1763 { "i0", 16, monitor_get_reg },
1764 { "i1", 17, monitor_get_reg },
1765 { "i2", 18, monitor_get_reg },
1766 { "i3", 19, monitor_get_reg },
1767 { "i4", 20, monitor_get_reg },
1768 { "i5", 21, monitor_get_reg },
1769 { "i6", 22, monitor_get_reg },
1770 { "i7", 23, monitor_get_reg },
1771 { "pc", offsetof(CPUState, pc) },
1772 { "npc", offsetof(CPUState, npc) },
1773 { "y", offsetof(CPUState, y) },
1774 #ifndef TARGET_SPARC64
1775 { "psr", 0, &monitor_get_psr, },
1776 { "wim", offsetof(CPUState, wim) },
1777 #endif
1778 { "tbr", offsetof(CPUState, tbr) },
1779 { "fsr", offsetof(CPUState, fsr) },
1780 { "f0", offsetof(CPUState, fpr[0]) },
1781 { "f1", offsetof(CPUState, fpr[1]) },
1782 { "f2", offsetof(CPUState, fpr[2]) },
1783 { "f3", offsetof(CPUState, fpr[3]) },
1784 { "f4", offsetof(CPUState, fpr[4]) },
1785 { "f5", offsetof(CPUState, fpr[5]) },
1786 { "f6", offsetof(CPUState, fpr[6]) },
1787 { "f7", offsetof(CPUState, fpr[7]) },
1788 { "f8", offsetof(CPUState, fpr[8]) },
1789 { "f9", offsetof(CPUState, fpr[9]) },
1790 { "f10", offsetof(CPUState, fpr[10]) },
1791 { "f11", offsetof(CPUState, fpr[11]) },
1792 { "f12", offsetof(CPUState, fpr[12]) },
1793 { "f13", offsetof(CPUState, fpr[13]) },
1794 { "f14", offsetof(CPUState, fpr[14]) },
1795 { "f15", offsetof(CPUState, fpr[15]) },
1796 { "f16", offsetof(CPUState, fpr[16]) },
1797 { "f17", offsetof(CPUState, fpr[17]) },
1798 { "f18", offsetof(CPUState, fpr[18]) },
1799 { "f19", offsetof(CPUState, fpr[19]) },
1800 { "f20", offsetof(CPUState, fpr[20]) },
1801 { "f21", offsetof(CPUState, fpr[21]) },
1802 { "f22", offsetof(CPUState, fpr[22]) },
1803 { "f23", offsetof(CPUState, fpr[23]) },
1804 { "f24", offsetof(CPUState, fpr[24]) },
1805 { "f25", offsetof(CPUState, fpr[25]) },
1806 { "f26", offsetof(CPUState, fpr[26]) },
1807 { "f27", offsetof(CPUState, fpr[27]) },
1808 { "f28", offsetof(CPUState, fpr[28]) },
1809 { "f29", offsetof(CPUState, fpr[29]) },
1810 { "f30", offsetof(CPUState, fpr[30]) },
1811 { "f31", offsetof(CPUState, fpr[31]) },
1812 #ifdef TARGET_SPARC64
1813 { "f32", offsetof(CPUState, fpr[32]) },
1814 { "f34", offsetof(CPUState, fpr[34]) },
1815 { "f36", offsetof(CPUState, fpr[36]) },
1816 { "f38", offsetof(CPUState, fpr[38]) },
1817 { "f40", offsetof(CPUState, fpr[40]) },
1818 { "f42", offsetof(CPUState, fpr[42]) },
1819 { "f44", offsetof(CPUState, fpr[44]) },
1820 { "f46", offsetof(CPUState, fpr[46]) },
1821 { "f48", offsetof(CPUState, fpr[48]) },
1822 { "f50", offsetof(CPUState, fpr[50]) },
1823 { "f52", offsetof(CPUState, fpr[52]) },
1824 { "f54", offsetof(CPUState, fpr[54]) },
1825 { "f56", offsetof(CPUState, fpr[56]) },
1826 { "f58", offsetof(CPUState, fpr[58]) },
1827 { "f60", offsetof(CPUState, fpr[60]) },
1828 { "f62", offsetof(CPUState, fpr[62]) },
1829 { "asi", offsetof(CPUState, asi) },
1830 { "pstate", offsetof(CPUState, pstate) },
1831 { "cansave", offsetof(CPUState, cansave) },
1832 { "canrestore", offsetof(CPUState, canrestore) },
1833 { "otherwin", offsetof(CPUState, otherwin) },
1834 { "wstate", offsetof(CPUState, wstate) },
1835 { "cleanwin", offsetof(CPUState, cleanwin) },
1836 { "fprs", offsetof(CPUState, fprs) },
1837 #endif
1838 #endif
1839 { NULL },
1842 static void expr_error(const char *fmt)
1844 term_printf(fmt);
1845 term_printf("\n");
1846 longjmp(expr_env, 1);
1849 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1850 static int get_monitor_def(target_long *pval, const char *name)
1852 MonitorDef *md;
1853 void *ptr;
1855 for(md = monitor_defs; md->name != NULL; md++) {
1856 if (compare_cmd(name, md->name)) {
1857 if (md->get_value) {
1858 *pval = md->get_value(md, md->offset);
1859 } else {
1860 CPUState *env = mon_get_cpu();
1861 if (!env)
1862 return -2;
1863 ptr = (uint8_t *)env + md->offset;
1864 switch(md->type) {
1865 case MD_I32:
1866 *pval = *(int32_t *)ptr;
1867 break;
1868 case MD_TLONG:
1869 *pval = *(target_long *)ptr;
1870 break;
1871 default:
1872 *pval = 0;
1873 break;
1876 return 0;
1879 return -1;
1882 static void next(void)
1884 if (pch != '\0') {
1885 pch++;
1886 while (isspace(*pch))
1887 pch++;
1891 static int64_t expr_sum(void);
1893 static int64_t expr_unary(void)
1895 int64_t n;
1896 char *p;
1897 int ret;
1899 switch(*pch) {
1900 case '+':
1901 next();
1902 n = expr_unary();
1903 break;
1904 case '-':
1905 next();
1906 n = -expr_unary();
1907 break;
1908 case '~':
1909 next();
1910 n = ~expr_unary();
1911 break;
1912 case '(':
1913 next();
1914 n = expr_sum();
1915 if (*pch != ')') {
1916 expr_error("')' expected");
1918 next();
1919 break;
1920 case '\'':
1921 pch++;
1922 if (*pch == '\0')
1923 expr_error("character constant expected");
1924 n = *pch;
1925 pch++;
1926 if (*pch != '\'')
1927 expr_error("missing terminating \' character");
1928 next();
1929 break;
1930 case '$':
1932 char buf[128], *q;
1933 target_long reg=0;
1935 pch++;
1936 q = buf;
1937 while ((*pch >= 'a' && *pch <= 'z') ||
1938 (*pch >= 'A' && *pch <= 'Z') ||
1939 (*pch >= '0' && *pch <= '9') ||
1940 *pch == '_' || *pch == '.') {
1941 if ((q - buf) < sizeof(buf) - 1)
1942 *q++ = *pch;
1943 pch++;
1945 while (isspace(*pch))
1946 pch++;
1947 *q = 0;
1948 ret = get_monitor_def(&reg, buf);
1949 if (ret == -1)
1950 expr_error("unknown register");
1951 else if (ret == -2)
1952 expr_error("no cpu defined");
1953 n = reg;
1955 break;
1956 case '\0':
1957 expr_error("unexpected end of expression");
1958 n = 0;
1959 break;
1960 default:
1961 #if TARGET_PHYS_ADDR_BITS > 32
1962 n = strtoull(pch, &p, 0);
1963 #else
1964 n = strtoul(pch, &p, 0);
1965 #endif
1966 if (pch == p) {
1967 expr_error("invalid char in expression");
1969 pch = p;
1970 while (isspace(*pch))
1971 pch++;
1972 break;
1974 return n;
1978 static int64_t expr_prod(void)
1980 int64_t val, val2;
1981 int op;
1983 val = expr_unary();
1984 for(;;) {
1985 op = *pch;
1986 if (op != '*' && op != '/' && op != '%')
1987 break;
1988 next();
1989 val2 = expr_unary();
1990 switch(op) {
1991 default:
1992 case '*':
1993 val *= val2;
1994 break;
1995 case '/':
1996 case '%':
1997 if (val2 == 0)
1998 expr_error("division by zero");
1999 if (op == '/')
2000 val /= val2;
2001 else
2002 val %= val2;
2003 break;
2006 return val;
2009 static int64_t expr_logic(void)
2011 int64_t val, val2;
2012 int op;
2014 val = expr_prod();
2015 for(;;) {
2016 op = *pch;
2017 if (op != '&' && op != '|' && op != '^')
2018 break;
2019 next();
2020 val2 = expr_prod();
2021 switch(op) {
2022 default:
2023 case '&':
2024 val &= val2;
2025 break;
2026 case '|':
2027 val |= val2;
2028 break;
2029 case '^':
2030 val ^= val2;
2031 break;
2034 return val;
2037 static int64_t expr_sum(void)
2039 int64_t val, val2;
2040 int op;
2042 val = expr_logic();
2043 for(;;) {
2044 op = *pch;
2045 if (op != '+' && op != '-')
2046 break;
2047 next();
2048 val2 = expr_logic();
2049 if (op == '+')
2050 val += val2;
2051 else
2052 val -= val2;
2054 return val;
2057 static int get_expr(int64_t *pval, const char **pp)
2059 pch = *pp;
2060 if (setjmp(expr_env)) {
2061 *pp = pch;
2062 return -1;
2064 while (isspace(*pch))
2065 pch++;
2066 *pval = expr_sum();
2067 *pp = pch;
2068 return 0;
2071 static int get_str(char *buf, int buf_size, const char **pp)
2073 const char *p;
2074 char *q;
2075 int c;
2077 q = buf;
2078 p = *pp;
2079 while (isspace(*p))
2080 p++;
2081 if (*p == '\0') {
2082 fail:
2083 *q = '\0';
2084 *pp = p;
2085 return -1;
2087 if (*p == '\"') {
2088 p++;
2089 while (*p != '\0' && *p != '\"') {
2090 if (*p == '\\') {
2091 p++;
2092 c = *p++;
2093 switch(c) {
2094 case 'n':
2095 c = '\n';
2096 break;
2097 case 'r':
2098 c = '\r';
2099 break;
2100 case '\\':
2101 case '\'':
2102 case '\"':
2103 break;
2104 default:
2105 qemu_printf("unsupported escape code: '\\%c'\n", c);
2106 goto fail;
2108 if ((q - buf) < buf_size - 1) {
2109 *q++ = c;
2111 } else {
2112 if ((q - buf) < buf_size - 1) {
2113 *q++ = *p;
2115 p++;
2118 if (*p != '\"') {
2119 qemu_printf("unterminated string\n");
2120 goto fail;
2122 p++;
2123 } else {
2124 while (*p != '\0' && !isspace(*p)) {
2125 if ((q - buf) < buf_size - 1) {
2126 *q++ = *p;
2128 p++;
2131 *q = '\0';
2132 *pp = p;
2133 return 0;
2136 static int default_fmt_format = 'x';
2137 static int default_fmt_size = 4;
2139 #define MAX_ARGS 16
2141 static void monitor_handle_command(const char *cmdline)
2143 const char *p, *pstart, *typestr;
2144 char *q;
2145 int c, nb_args, len, i, has_arg;
2146 term_cmd_t *cmd;
2147 char cmdname[256];
2148 char buf[1024];
2149 void *str_allocated[MAX_ARGS];
2150 void *args[MAX_ARGS];
2152 #ifdef DEBUG
2153 term_printf("command='%s'\n", cmdline);
2154 #endif
2156 /* extract the command name */
2157 p = cmdline;
2158 q = cmdname;
2159 while (isspace(*p))
2160 p++;
2161 if (*p == '\0')
2162 return;
2163 pstart = p;
2164 while (*p != '\0' && *p != '/' && !isspace(*p))
2165 p++;
2166 len = p - pstart;
2167 if (len > sizeof(cmdname) - 1)
2168 len = sizeof(cmdname) - 1;
2169 memcpy(cmdname, pstart, len);
2170 cmdname[len] = '\0';
2172 /* find the command */
2173 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2174 if (compare_cmd(cmdname, cmd->name))
2175 goto found;
2177 term_printf("unknown command: '%s'\n", cmdname);
2178 return;
2179 found:
2181 for(i = 0; i < MAX_ARGS; i++)
2182 str_allocated[i] = NULL;
2184 /* parse the parameters */
2185 typestr = cmd->args_type;
2186 nb_args = 0;
2187 for(;;) {
2188 c = *typestr;
2189 if (c == '\0')
2190 break;
2191 typestr++;
2192 switch(c) {
2193 case 'F':
2194 case 'B':
2195 case 's':
2197 int ret;
2198 char *str;
2200 while (isspace(*p))
2201 p++;
2202 if (*typestr == '?') {
2203 typestr++;
2204 if (*p == '\0') {
2205 /* no optional string: NULL argument */
2206 str = NULL;
2207 goto add_str;
2210 ret = get_str(buf, sizeof(buf), &p);
2211 if (ret < 0) {
2212 switch(c) {
2213 case 'F':
2214 term_printf("%s: filename expected\n", cmdname);
2215 break;
2216 case 'B':
2217 term_printf("%s: block device name expected\n", cmdname);
2218 break;
2219 default:
2220 term_printf("%s: string expected\n", cmdname);
2221 break;
2223 goto fail;
2225 str = qemu_malloc(strlen(buf) + 1);
2226 strcpy(str, buf);
2227 str_allocated[nb_args] = str;
2228 add_str:
2229 if (nb_args >= MAX_ARGS) {
2230 error_args:
2231 term_printf("%s: too many arguments\n", cmdname);
2232 goto fail;
2234 args[nb_args++] = str;
2236 break;
2237 case '/':
2239 int count, format, size;
2241 while (isspace(*p))
2242 p++;
2243 if (*p == '/') {
2244 /* format found */
2245 p++;
2246 count = 1;
2247 if (isdigit(*p)) {
2248 count = 0;
2249 while (isdigit(*p)) {
2250 count = count * 10 + (*p - '0');
2251 p++;
2254 size = -1;
2255 format = -1;
2256 for(;;) {
2257 switch(*p) {
2258 case 'o':
2259 case 'd':
2260 case 'u':
2261 case 'x':
2262 case 'i':
2263 case 'c':
2264 format = *p++;
2265 break;
2266 case 'b':
2267 size = 1;
2268 p++;
2269 break;
2270 case 'h':
2271 size = 2;
2272 p++;
2273 break;
2274 case 'w':
2275 size = 4;
2276 p++;
2277 break;
2278 case 'g':
2279 case 'L':
2280 size = 8;
2281 p++;
2282 break;
2283 default:
2284 goto next;
2287 next:
2288 if (*p != '\0' && !isspace(*p)) {
2289 term_printf("invalid char in format: '%c'\n", *p);
2290 goto fail;
2292 if (format < 0)
2293 format = default_fmt_format;
2294 if (format != 'i') {
2295 /* for 'i', not specifying a size gives -1 as size */
2296 if (size < 0)
2297 size = default_fmt_size;
2299 default_fmt_size = size;
2300 default_fmt_format = format;
2301 } else {
2302 count = 1;
2303 format = default_fmt_format;
2304 if (format != 'i') {
2305 size = default_fmt_size;
2306 } else {
2307 size = -1;
2310 if (nb_args + 3 > MAX_ARGS)
2311 goto error_args;
2312 args[nb_args++] = (void*)(long)count;
2313 args[nb_args++] = (void*)(long)format;
2314 args[nb_args++] = (void*)(long)size;
2316 break;
2317 case 'i':
2318 case 'l':
2320 int64_t val;
2322 while (isspace(*p))
2323 p++;
2324 if (*typestr == '?' || *typestr == '.') {
2325 if (*typestr == '?') {
2326 if (*p == '\0')
2327 has_arg = 0;
2328 else
2329 has_arg = 1;
2330 } else {
2331 if (*p == '.') {
2332 p++;
2333 while (isspace(*p))
2334 p++;
2335 has_arg = 1;
2336 } else {
2337 has_arg = 0;
2340 typestr++;
2341 if (nb_args >= MAX_ARGS)
2342 goto error_args;
2343 args[nb_args++] = (void *)(long)has_arg;
2344 if (!has_arg) {
2345 if (nb_args >= MAX_ARGS)
2346 goto error_args;
2347 val = -1;
2348 goto add_num;
2351 if (get_expr(&val, &p))
2352 goto fail;
2353 add_num:
2354 if (c == 'i') {
2355 if (nb_args >= MAX_ARGS)
2356 goto error_args;
2357 args[nb_args++] = (void *)(long)val;
2358 } else {
2359 if ((nb_args + 1) >= MAX_ARGS)
2360 goto error_args;
2361 #if TARGET_PHYS_ADDR_BITS > 32
2362 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2363 #else
2364 args[nb_args++] = (void *)0;
2365 #endif
2366 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2369 break;
2370 case '-':
2372 int has_option;
2373 /* option */
2375 c = *typestr++;
2376 if (c == '\0')
2377 goto bad_type;
2378 while (isspace(*p))
2379 p++;
2380 has_option = 0;
2381 if (*p == '-') {
2382 p++;
2383 if (*p != c) {
2384 term_printf("%s: unsupported option -%c\n",
2385 cmdname, *p);
2386 goto fail;
2388 p++;
2389 has_option = 1;
2391 if (nb_args >= MAX_ARGS)
2392 goto error_args;
2393 args[nb_args++] = (void *)(long)has_option;
2395 break;
2396 default:
2397 bad_type:
2398 term_printf("%s: unknown type '%c'\n", cmdname, c);
2399 goto fail;
2402 /* check that all arguments were parsed */
2403 while (isspace(*p))
2404 p++;
2405 if (*p != '\0') {
2406 term_printf("%s: extraneous characters at the end of line\n",
2407 cmdname);
2408 goto fail;
2411 switch(nb_args) {
2412 case 0:
2413 cmd->handler();
2414 break;
2415 case 1:
2416 cmd->handler(args[0]);
2417 break;
2418 case 2:
2419 cmd->handler(args[0], args[1]);
2420 break;
2421 case 3:
2422 cmd->handler(args[0], args[1], args[2]);
2423 break;
2424 case 4:
2425 cmd->handler(args[0], args[1], args[2], args[3]);
2426 break;
2427 case 5:
2428 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2429 break;
2430 case 6:
2431 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2432 break;
2433 case 7:
2434 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2435 break;
2436 default:
2437 term_printf("unsupported number of arguments: %d\n", nb_args);
2438 goto fail;
2440 fail:
2441 for(i = 0; i < MAX_ARGS; i++)
2442 qemu_free(str_allocated[i]);
2443 return;
2446 static void cmd_completion(const char *name, const char *list)
2448 const char *p, *pstart;
2449 char cmd[128];
2450 int len;
2452 p = list;
2453 for(;;) {
2454 pstart = p;
2455 p = strchr(p, '|');
2456 if (!p)
2457 p = pstart + strlen(pstart);
2458 len = p - pstart;
2459 if (len > sizeof(cmd) - 2)
2460 len = sizeof(cmd) - 2;
2461 memcpy(cmd, pstart, len);
2462 cmd[len] = '\0';
2463 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2464 add_completion(cmd);
2466 if (*p == '\0')
2467 break;
2468 p++;
2472 static void file_completion(const char *input)
2474 DIR *ffs;
2475 struct dirent *d;
2476 char path[1024];
2477 char file[1024], file_prefix[1024];
2478 int input_path_len;
2479 const char *p;
2481 p = strrchr(input, '/');
2482 if (!p) {
2483 input_path_len = 0;
2484 pstrcpy(file_prefix, sizeof(file_prefix), input);
2485 strcpy(path, ".");
2486 } else {
2487 input_path_len = p - input + 1;
2488 memcpy(path, input, input_path_len);
2489 if (input_path_len > sizeof(path) - 1)
2490 input_path_len = sizeof(path) - 1;
2491 path[input_path_len] = '\0';
2492 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2494 #ifdef DEBUG_COMPLETION
2495 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2496 #endif
2497 ffs = opendir(path);
2498 if (!ffs)
2499 return;
2500 for(;;) {
2501 struct stat sb;
2502 d = readdir(ffs);
2503 if (!d)
2504 break;
2505 if (strstart(d->d_name, file_prefix, NULL)) {
2506 memcpy(file, input, input_path_len);
2507 strcpy(file + input_path_len, d->d_name);
2508 /* stat the file to find out if it's a directory.
2509 * In that case add a slash to speed up typing long paths
2511 stat(file, &sb);
2512 if(S_ISDIR(sb.st_mode))
2513 strcat(file, "/");
2514 add_completion(file);
2517 closedir(ffs);
2520 static void block_completion_it(void *opaque, const char *name)
2522 const char *input = opaque;
2524 if (input[0] == '\0' ||
2525 !strncmp(name, (char *)input, strlen(input))) {
2526 add_completion(name);
2530 /* NOTE: this parser is an approximate form of the real command parser */
2531 static void parse_cmdline(const char *cmdline,
2532 int *pnb_args, char **args)
2534 const char *p;
2535 int nb_args, ret;
2536 char buf[1024];
2538 p = cmdline;
2539 nb_args = 0;
2540 for(;;) {
2541 while (isspace(*p))
2542 p++;
2543 if (*p == '\0')
2544 break;
2545 if (nb_args >= MAX_ARGS)
2546 break;
2547 ret = get_str(buf, sizeof(buf), &p);
2548 args[nb_args] = qemu_strdup(buf);
2549 nb_args++;
2550 if (ret < 0)
2551 break;
2553 *pnb_args = nb_args;
2556 void readline_find_completion(const char *cmdline)
2558 const char *cmdname;
2559 char *args[MAX_ARGS];
2560 int nb_args, i, len;
2561 const char *ptype, *str;
2562 term_cmd_t *cmd;
2563 const KeyDef *key;
2565 parse_cmdline(cmdline, &nb_args, args);
2566 #ifdef DEBUG_COMPLETION
2567 for(i = 0; i < nb_args; i++) {
2568 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2570 #endif
2572 /* if the line ends with a space, it means we want to complete the
2573 next arg */
2574 len = strlen(cmdline);
2575 if (len > 0 && isspace(cmdline[len - 1])) {
2576 if (nb_args >= MAX_ARGS)
2577 return;
2578 args[nb_args++] = qemu_strdup("");
2580 if (nb_args <= 1) {
2581 /* command completion */
2582 if (nb_args == 0)
2583 cmdname = "";
2584 else
2585 cmdname = args[0];
2586 completion_index = strlen(cmdname);
2587 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2588 cmd_completion(cmdname, cmd->name);
2590 } else {
2591 /* find the command */
2592 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2593 if (compare_cmd(args[0], cmd->name))
2594 goto found;
2596 return;
2597 found:
2598 ptype = cmd->args_type;
2599 for(i = 0; i < nb_args - 2; i++) {
2600 if (*ptype != '\0') {
2601 ptype++;
2602 while (*ptype == '?')
2603 ptype++;
2606 str = args[nb_args - 1];
2607 switch(*ptype) {
2608 case 'F':
2609 /* file completion */
2610 completion_index = strlen(str);
2611 file_completion(str);
2612 break;
2613 case 'B':
2614 /* block device name completion */
2615 completion_index = strlen(str);
2616 bdrv_iterate(block_completion_it, (void *)str);
2617 break;
2618 case 's':
2619 /* XXX: more generic ? */
2620 if (!strcmp(cmd->name, "info")) {
2621 completion_index = strlen(str);
2622 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2623 cmd_completion(str, cmd->name);
2625 } else if (!strcmp(cmd->name, "sendkey")) {
2626 completion_index = strlen(str);
2627 for(key = key_defs; key->name != NULL; key++) {
2628 cmd_completion(str, key->name);
2631 break;
2632 default:
2633 break;
2636 for(i = 0; i < nb_args; i++)
2637 qemu_free(args[i]);
2640 static int term_can_read(void *opaque)
2642 return 128;
2645 static void term_read(void *opaque, const uint8_t *buf, int size)
2647 int i;
2648 for(i = 0; i < size; i++)
2649 readline_handle_byte(buf[i]);
2652 static int monitor_suspended;
2654 void monitor_suspend(void)
2656 monitor_suspended = 1;
2659 void monitor_resume(void)
2661 monitor_suspended = 0;
2662 monitor_start_input();
2665 static void monitor_start_input(void);
2667 static void monitor_handle_command1(void *opaque, const char *cmdline)
2669 monitor_handle_command(cmdline);
2670 if (!monitor_suspended)
2671 monitor_start_input();
2674 static void monitor_start_input(void)
2676 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2679 static void term_event(void *opaque, int event)
2681 if (event != CHR_EVENT_RESET)
2682 return;
2684 if (!hide_banner)
2685 term_printf("QEMU %s monitor - type 'help' for more information\n",
2686 QEMU_VERSION);
2687 monitor_start_input();
2690 static int is_first_init = 1;
2692 void monitor_init(CharDriverState *hd, int show_banner)
2694 int i;
2696 if (is_first_init) {
2697 for (i = 0; i < MAX_MON; i++) {
2698 monitor_hd[i] = NULL;
2700 is_first_init = 0;
2702 for (i = 0; i < MAX_MON; i++) {
2703 if (monitor_hd[i] == NULL) {
2704 monitor_hd[i] = hd;
2705 break;
2709 hide_banner = !show_banner;
2711 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2713 readline_start("", 0, monitor_handle_command1, NULL);
2716 /* XXX: use threads ? */
2717 /* modal monitor readline */
2718 static int monitor_readline_started;
2719 static char *monitor_readline_buf;
2720 static int monitor_readline_buf_size;
2722 static void monitor_readline_cb(void *opaque, const char *input)
2724 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2725 monitor_readline_started = 0;
2728 void monitor_readline(const char *prompt, int is_password,
2729 char *buf, int buf_size)
2731 int i;
2733 if (is_password) {
2734 for (i = 0; i < MAX_MON; i++)
2735 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2736 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2738 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2739 monitor_readline_buf = buf;
2740 monitor_readline_buf_size = buf_size;
2741 monitor_readline_started = 1;
2742 while (monitor_readline_started) {
2743 main_loop_wait(10);