Merge branch 'qemu-cvs'
[qemu-kvm/fedora.git] / monitor.c
blob46d59981e3718ba23f07531da8dcb87f1beed510
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>
39 #include "qemu-timer.h"
41 #include "qemu-kvm.h"
43 //#define DEBUG
44 //#define DEBUG_COMPLETION
46 #ifndef offsetof
47 #define offsetof(type, field) ((size_t) &((type *)0)->field)
48 #endif
51 * Supported types:
53 * 'F' filename
54 * 'B' block device name
55 * 's' string (accept optional quote)
56 * 'i' 32 bit integer
57 * 'l' target long (32 or 64 bit)
58 * '/' optional gdb-like print format (like "/10x")
60 * '?' optional type (for 'F', 's' and 'i')
64 typedef struct term_cmd_t {
65 const char *name;
66 const char *args_type;
67 void (*handler)();
68 const char *params;
69 const char *help;
70 } term_cmd_t;
72 #define MAX_MON 4
73 static CharDriverState *monitor_hd[MAX_MON];
74 static int hide_banner;
76 static term_cmd_t term_cmds[];
77 static term_cmd_t info_cmds[];
79 static uint8_t term_outbuf[1024];
80 static int term_outbuf_index;
82 static void monitor_start_input(void);
84 CPUState *mon_cpu = NULL;
86 void term_flush(void)
88 int i;
89 if (term_outbuf_index > 0) {
90 for (i = 0; i < MAX_MON; i++)
91 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
92 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
93 term_outbuf_index = 0;
97 /* flush at every end of line or if the buffer is full */
98 void term_puts(const char *str)
100 char c;
101 for(;;) {
102 c = *str++;
103 if (c == '\0')
104 break;
105 if (c == '\n')
106 term_outbuf[term_outbuf_index++] = '\r';
107 term_outbuf[term_outbuf_index++] = c;
108 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
109 c == '\n')
110 term_flush();
114 void term_vprintf(const char *fmt, va_list ap)
116 char buf[4096];
117 vsnprintf(buf, sizeof(buf), fmt, ap);
118 term_puts(buf);
121 void term_printf(const char *fmt, ...)
123 va_list ap;
124 va_start(ap, fmt);
125 term_vprintf(fmt, ap);
126 va_end(ap);
129 void term_print_filename(const char *filename)
131 int i;
133 for (i = 0; filename[i]; i++) {
134 switch (filename[i]) {
135 case ' ':
136 case '"':
137 case '\\':
138 term_printf("\\%c", filename[i]);
139 break;
140 case '\t':
141 term_printf("\\t");
142 break;
143 case '\r':
144 term_printf("\\r");
145 break;
146 case '\n':
147 term_printf("\\n");
148 break;
149 default:
150 term_printf("%c", filename[i]);
151 break;
156 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
158 va_list ap;
159 va_start(ap, fmt);
160 term_vprintf(fmt, ap);
161 va_end(ap);
162 return 0;
165 static int compare_cmd(const char *name, const char *list)
167 const char *p, *pstart;
168 int len;
169 len = strlen(name);
170 p = list;
171 for(;;) {
172 pstart = p;
173 p = strchr(p, '|');
174 if (!p)
175 p = pstart + strlen(pstart);
176 if ((p - pstart) == len && !memcmp(pstart, name, len))
177 return 1;
178 if (*p == '\0')
179 break;
180 p++;
182 return 0;
185 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
187 term_cmd_t *cmd;
189 for(cmd = cmds; cmd->name != NULL; cmd++) {
190 if (!name || !strcmp(name, cmd->name))
191 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
195 static void help_cmd(const char *name)
197 if (name && !strcmp(name, "info")) {
198 help_cmd1(info_cmds, "info ", NULL);
199 } else {
200 help_cmd1(term_cmds, "", name);
201 if (name && !strcmp(name, "log")) {
202 CPULogItem *item;
203 term_printf("Log items (comma separated):\n");
204 term_printf("%-10s %s\n", "none", "remove all logs");
205 for(item = cpu_log_items; item->mask != 0; item++) {
206 term_printf("%-10s %s\n", item->name, item->help);
212 static void do_help(const char *name)
214 help_cmd(name);
217 static void do_commit(const char *device)
219 int i, all_devices;
221 all_devices = !strcmp(device, "all");
222 for (i = 0; i < nb_drives; i++) {
223 if (all_devices ||
224 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
225 bdrv_commit(drives_table[i].bdrv);
229 static void do_info(const char *item)
231 term_cmd_t *cmd;
233 if (!item)
234 goto help;
235 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
236 if (compare_cmd(item, cmd->name))
237 goto found;
239 help:
240 help_cmd("info");
241 return;
242 found:
243 cmd->handler();
246 static void do_info_version(void)
248 term_printf("%s\n", QEMU_VERSION);
251 static void do_info_name(void)
253 if (qemu_name)
254 term_printf("%s\n", qemu_name);
257 static void do_info_block(void)
259 bdrv_info();
262 static void do_info_blockstats(void)
264 bdrv_info_stats();
267 /* get the current CPU defined by the user */
268 static int mon_set_cpu(int cpu_index)
270 CPUState *env;
272 for(env = first_cpu; env != NULL; env = env->next_cpu) {
273 if (env->cpu_index == cpu_index) {
274 mon_cpu = env;
275 return 0;
278 return -1;
281 static CPUState *mon_get_cpu(void)
283 if (!mon_cpu) {
284 mon_set_cpu(0);
287 kvm_save_registers(mon_cpu);
289 return mon_cpu;
292 static void do_info_registers(void)
294 CPUState *env;
295 env = mon_get_cpu();
296 if (!env)
297 return;
298 #ifdef TARGET_I386
299 cpu_dump_state(env, NULL, monitor_fprintf,
300 X86_DUMP_FPU);
301 #else
302 cpu_dump_state(env, NULL, monitor_fprintf,
304 #endif
307 static void do_info_cpus(void)
309 CPUState *env;
311 /* just to set the default cpu if not already done */
312 mon_get_cpu();
314 for(env = first_cpu; env != NULL; env = env->next_cpu) {
315 kvm_save_registers(env);
316 term_printf("%c CPU #%d:",
317 (env == mon_cpu) ? '*' : ' ',
318 env->cpu_index);
319 #if defined(TARGET_I386)
320 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
321 #elif defined(TARGET_PPC)
322 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
323 #elif defined(TARGET_SPARC)
324 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
325 #elif defined(TARGET_MIPS)
326 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
327 #endif
328 if (env->halted)
329 term_printf(" (halted)");
330 term_printf(" thread_id=%d", env->thread_id);
331 term_printf("\n");
335 static void do_cpu_set(int index)
337 if (mon_set_cpu(index) < 0)
338 term_printf("Invalid CPU index\n");
341 static void do_cpu_set_nr(int value, const char *status)
343 int state;
345 if (!strcmp(status, "online"))
346 state = 1;
347 else if (!strcmp(status, "offline"))
348 state = 0;
349 else {
350 term_printf("invalid status: %s\n", status);
351 return;
353 #if defined(TARGET_I386) || defined(TARGET_X86_64)
354 qemu_system_cpu_hot_add(value, state);
355 #endif
358 static void do_info_jit(void)
360 dump_exec_info(NULL, monitor_fprintf);
363 static void do_info_history (void)
365 int i;
366 const char *str;
368 i = 0;
369 for(;;) {
370 str = readline_get_history(i);
371 if (!str)
372 break;
373 term_printf("%d: '%s'\n", i, str);
374 i++;
378 #if defined(TARGET_PPC)
379 /* XXX: not implemented in other targets */
380 static void do_info_cpu_stats (void)
382 CPUState *env;
384 env = mon_get_cpu();
385 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
387 #endif
389 static void do_quit(void)
391 exit(0);
394 static int eject_device(BlockDriverState *bs, int force)
396 if (bdrv_is_inserted(bs)) {
397 if (!force) {
398 if (!bdrv_is_removable(bs)) {
399 term_printf("device is not removable\n");
400 return -1;
402 if (bdrv_is_locked(bs)) {
403 term_printf("device is locked\n");
404 return -1;
407 bdrv_close(bs);
409 return 0;
412 static void do_eject(int force, const char *filename)
414 BlockDriverState *bs;
416 bs = bdrv_find(filename);
417 if (!bs) {
418 term_printf("device not found\n");
419 return;
421 eject_device(bs, force);
424 static void do_change_block(const char *device, const char *filename, const char *fmt)
426 BlockDriverState *bs;
427 BlockDriver *drv = NULL;
429 bs = bdrv_find(device);
430 if (!bs) {
431 term_printf("device not found\n");
432 return;
434 if (fmt) {
435 drv = bdrv_find_format(fmt);
436 if (!drv) {
437 term_printf("invalid format %s\n", fmt);
438 return;
441 if (eject_device(bs, 0) < 0)
442 return;
443 bdrv_open2(bs, filename, 0, drv);
444 qemu_key_check(bs, filename);
447 static void do_change_vnc(const char *target)
449 if (strcmp(target, "passwd") == 0 ||
450 strcmp(target, "password") == 0) {
451 char password[9];
452 monitor_readline("Password: ", 1, password, sizeof(password)-1);
453 password[sizeof(password)-1] = '\0';
454 if (vnc_display_password(NULL, password) < 0)
455 term_printf("could not set VNC server password\n");
456 } else {
457 if (vnc_display_open(NULL, target) < 0)
458 term_printf("could not start VNC server on %s\n", target);
462 static void do_change(const char *device, const char *target, const char *fmt)
464 if (strcmp(device, "vnc") == 0) {
465 do_change_vnc(target);
466 } else {
467 do_change_block(device, target, fmt);
471 static void do_screen_dump(const char *filename)
473 vga_hw_screen_dump(filename);
476 static void do_logfile(const char *filename)
478 cpu_set_log_filename(filename);
481 static void do_log(const char *items)
483 int mask;
485 if (!strcmp(items, "none")) {
486 mask = 0;
487 } else {
488 mask = cpu_str_to_log_mask(items);
489 if (!mask) {
490 help_cmd("log");
491 return;
494 cpu_set_log(mask);
497 static void do_stop(void)
499 vm_stop(EXCP_INTERRUPT);
502 static void do_cont(void)
504 vm_start();
507 #ifdef CONFIG_GDBSTUB
508 static void do_gdbserver(const char *port)
510 if (!port)
511 port = DEFAULT_GDBSTUB_PORT;
512 if (gdbserver_start(port) < 0) {
513 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
514 } else {
515 qemu_printf("Waiting gdb connection on port '%s'\n", port);
518 #endif
520 static void term_printc(int c)
522 term_printf("'");
523 switch(c) {
524 case '\'':
525 term_printf("\\'");
526 break;
527 case '\\':
528 term_printf("\\\\");
529 break;
530 case '\n':
531 term_printf("\\n");
532 break;
533 case '\r':
534 term_printf("\\r");
535 break;
536 default:
537 if (c >= 32 && c <= 126) {
538 term_printf("%c", c);
539 } else {
540 term_printf("\\x%02x", c);
542 break;
544 term_printf("'");
547 static void memory_dump(int count, int format, int wsize,
548 target_phys_addr_t addr, int is_physical)
550 CPUState *env;
551 int nb_per_line, l, line_size, i, max_digits, len;
552 uint8_t buf[16];
553 uint64_t v;
555 if (format == 'i') {
556 int flags;
557 flags = 0;
558 env = mon_get_cpu();
559 if (!env && !is_physical)
560 return;
561 #ifdef TARGET_I386
562 if (wsize == 2) {
563 flags = 1;
564 } else if (wsize == 4) {
565 flags = 0;
566 } else {
567 /* as default we use the current CS size */
568 flags = 0;
569 if (env) {
570 #ifdef TARGET_X86_64
571 if ((env->efer & MSR_EFER_LMA) &&
572 (env->segs[R_CS].flags & DESC_L_MASK))
573 flags = 2;
574 else
575 #endif
576 if (!(env->segs[R_CS].flags & DESC_B_MASK))
577 flags = 1;
580 #endif
581 monitor_disas(env, addr, count, is_physical, flags);
582 return;
585 len = wsize * count;
586 if (wsize == 1)
587 line_size = 8;
588 else
589 line_size = 16;
590 nb_per_line = line_size / wsize;
591 max_digits = 0;
593 switch(format) {
594 case 'o':
595 max_digits = (wsize * 8 + 2) / 3;
596 break;
597 default:
598 case 'x':
599 max_digits = (wsize * 8) / 4;
600 break;
601 case 'u':
602 case 'd':
603 max_digits = (wsize * 8 * 10 + 32) / 33;
604 break;
605 case 'c':
606 wsize = 1;
607 break;
610 while (len > 0) {
611 if (is_physical)
612 term_printf(TARGET_FMT_plx ":", addr);
613 else
614 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
615 l = len;
616 if (l > line_size)
617 l = line_size;
618 if (is_physical) {
619 cpu_physical_memory_rw(addr, buf, l, 0);
620 } else {
621 env = mon_get_cpu();
622 if (!env)
623 break;
624 cpu_memory_rw_debug(env, addr, buf, l, 0);
626 i = 0;
627 while (i < l) {
628 switch(wsize) {
629 default:
630 case 1:
631 v = ldub_raw(buf + i);
632 break;
633 case 2:
634 v = lduw_raw(buf + i);
635 break;
636 case 4:
637 v = (uint32_t)ldl_raw(buf + i);
638 break;
639 case 8:
640 v = ldq_raw(buf + i);
641 break;
643 term_printf(" ");
644 switch(format) {
645 case 'o':
646 term_printf("%#*" PRIo64, max_digits, v);
647 break;
648 case 'x':
649 term_printf("0x%0*" PRIx64, max_digits, v);
650 break;
651 case 'u':
652 term_printf("%*" PRIu64, max_digits, v);
653 break;
654 case 'd':
655 term_printf("%*" PRId64, max_digits, v);
656 break;
657 case 'c':
658 term_printc(v);
659 break;
661 i += wsize;
663 term_printf("\n");
664 addr += l;
665 len -= l;
669 #if TARGET_LONG_BITS == 64
670 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
671 #else
672 #define GET_TLONG(h, l) (l)
673 #endif
675 static void do_memory_dump(int count, int format, int size,
676 uint32_t addrh, uint32_t addrl)
678 target_long addr = GET_TLONG(addrh, addrl);
679 memory_dump(count, format, size, addr, 0);
682 #if TARGET_PHYS_ADDR_BITS > 32
683 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
684 #else
685 #define GET_TPHYSADDR(h, l) (l)
686 #endif
688 static void do_physical_memory_dump(int count, int format, int size,
689 uint32_t addrh, uint32_t addrl)
692 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
693 memory_dump(count, format, size, addr, 1);
696 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
698 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
699 #if TARGET_PHYS_ADDR_BITS == 32
700 switch(format) {
701 case 'o':
702 term_printf("%#o", val);
703 break;
704 case 'x':
705 term_printf("%#x", val);
706 break;
707 case 'u':
708 term_printf("%u", val);
709 break;
710 default:
711 case 'd':
712 term_printf("%d", val);
713 break;
714 case 'c':
715 term_printc(val);
716 break;
718 #else
719 switch(format) {
720 case 'o':
721 term_printf("%#" PRIo64, val);
722 break;
723 case 'x':
724 term_printf("%#" PRIx64, val);
725 break;
726 case 'u':
727 term_printf("%" PRIu64, val);
728 break;
729 default:
730 case 'd':
731 term_printf("%" PRId64, val);
732 break;
733 case 'c':
734 term_printc(val);
735 break;
737 #endif
738 term_printf("\n");
741 static void do_memory_save(unsigned int valh, unsigned int vall,
742 uint32_t size, const char *filename)
744 FILE *f;
745 target_long addr = GET_TLONG(valh, vall);
746 uint32_t l;
747 CPUState *env;
748 uint8_t buf[1024];
750 env = mon_get_cpu();
751 if (!env)
752 return;
754 f = fopen(filename, "wb");
755 if (!f) {
756 term_printf("could not open '%s'\n", filename);
757 return;
759 while (size != 0) {
760 l = sizeof(buf);
761 if (l > size)
762 l = size;
763 cpu_memory_rw_debug(env, addr, buf, l, 0);
764 fwrite(buf, 1, l, f);
765 addr += l;
766 size -= l;
768 fclose(f);
771 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
772 uint32_t size, const char *filename)
774 FILE *f;
775 uint32_t l;
776 uint8_t buf[1024];
777 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
779 f = fopen(filename, "wb");
780 if (!f) {
781 term_printf("could not open '%s'\n", filename);
782 return;
784 while (size != 0) {
785 l = sizeof(buf);
786 if (l > size)
787 l = size;
788 cpu_physical_memory_rw(addr, buf, l, 0);
789 fwrite(buf, 1, l, f);
790 fflush(f);
791 addr += l;
792 size -= l;
794 fclose(f);
797 static void do_sum(uint32_t start, uint32_t size)
799 uint32_t addr;
800 uint8_t buf[1];
801 uint16_t sum;
803 sum = 0;
804 for(addr = start; addr < (start + size); addr++) {
805 cpu_physical_memory_rw(addr, buf, 1, 0);
806 /* BSD sum algorithm ('sum' Unix command) */
807 sum = (sum >> 1) | (sum << 15);
808 sum += buf[0];
810 term_printf("%05d\n", sum);
813 typedef struct {
814 int keycode;
815 const char *name;
816 } KeyDef;
818 static const KeyDef key_defs[] = {
819 { 0x2a, "shift" },
820 { 0x36, "shift_r" },
822 { 0x38, "alt" },
823 { 0xb8, "alt_r" },
824 { 0x1d, "ctrl" },
825 { 0x9d, "ctrl_r" },
827 { 0xdd, "menu" },
829 { 0x01, "esc" },
831 { 0x02, "1" },
832 { 0x03, "2" },
833 { 0x04, "3" },
834 { 0x05, "4" },
835 { 0x06, "5" },
836 { 0x07, "6" },
837 { 0x08, "7" },
838 { 0x09, "8" },
839 { 0x0a, "9" },
840 { 0x0b, "0" },
841 { 0x0c, "minus" },
842 { 0x0d, "equal" },
843 { 0x0e, "backspace" },
845 { 0x0f, "tab" },
846 { 0x10, "q" },
847 { 0x11, "w" },
848 { 0x12, "e" },
849 { 0x13, "r" },
850 { 0x14, "t" },
851 { 0x15, "y" },
852 { 0x16, "u" },
853 { 0x17, "i" },
854 { 0x18, "o" },
855 { 0x19, "p" },
857 { 0x1c, "ret" },
859 { 0x1e, "a" },
860 { 0x1f, "s" },
861 { 0x20, "d" },
862 { 0x21, "f" },
863 { 0x22, "g" },
864 { 0x23, "h" },
865 { 0x24, "j" },
866 { 0x25, "k" },
867 { 0x26, "l" },
869 { 0x2c, "z" },
870 { 0x2d, "x" },
871 { 0x2e, "c" },
872 { 0x2f, "v" },
873 { 0x30, "b" },
874 { 0x31, "n" },
875 { 0x32, "m" },
877 { 0x37, "asterisk" },
879 { 0x39, "spc" },
880 { 0x3a, "caps_lock" },
881 { 0x3b, "f1" },
882 { 0x3c, "f2" },
883 { 0x3d, "f3" },
884 { 0x3e, "f4" },
885 { 0x3f, "f5" },
886 { 0x40, "f6" },
887 { 0x41, "f7" },
888 { 0x42, "f8" },
889 { 0x43, "f9" },
890 { 0x44, "f10" },
891 { 0x45, "num_lock" },
892 { 0x46, "scroll_lock" },
894 { 0xb5, "kp_divide" },
895 { 0x37, "kp_multiply" },
896 { 0x4a, "kp_subtract" },
897 { 0x4e, "kp_add" },
898 { 0x9c, "kp_enter" },
899 { 0x53, "kp_decimal" },
900 { 0x54, "sysrq" },
902 { 0x52, "kp_0" },
903 { 0x4f, "kp_1" },
904 { 0x50, "kp_2" },
905 { 0x51, "kp_3" },
906 { 0x4b, "kp_4" },
907 { 0x4c, "kp_5" },
908 { 0x4d, "kp_6" },
909 { 0x47, "kp_7" },
910 { 0x48, "kp_8" },
911 { 0x49, "kp_9" },
913 { 0x56, "<" },
915 { 0x57, "f11" },
916 { 0x58, "f12" },
918 { 0xb7, "print" },
920 { 0xc7, "home" },
921 { 0xc9, "pgup" },
922 { 0xd1, "pgdn" },
923 { 0xcf, "end" },
925 { 0xcb, "left" },
926 { 0xc8, "up" },
927 { 0xd0, "down" },
928 { 0xcd, "right" },
930 { 0xd2, "insert" },
931 { 0xd3, "delete" },
932 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
933 { 0xf0, "stop" },
934 { 0xf1, "again" },
935 { 0xf2, "props" },
936 { 0xf3, "undo" },
937 { 0xf4, "front" },
938 { 0xf5, "copy" },
939 { 0xf6, "open" },
940 { 0xf7, "paste" },
941 { 0xf8, "find" },
942 { 0xf9, "cut" },
943 { 0xfa, "lf" },
944 { 0xfb, "help" },
945 { 0xfc, "meta_l" },
946 { 0xfd, "meta_r" },
947 { 0xfe, "compose" },
948 #endif
949 { 0, NULL },
952 static int get_keycode(const char *key)
954 const KeyDef *p;
955 char *endp;
956 int ret;
958 for(p = key_defs; p->name != NULL; p++) {
959 if (!strcmp(key, p->name))
960 return p->keycode;
962 if (strstart(key, "0x", NULL)) {
963 ret = strtoul(key, &endp, 0);
964 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
965 return ret;
967 return -1;
970 #define MAX_KEYCODES 16
971 static uint8_t keycodes[MAX_KEYCODES];
972 static int nb_pending_keycodes;
973 static QEMUTimer *key_timer;
975 static void release_keys(void *opaque)
977 int keycode;
979 while (nb_pending_keycodes > 0) {
980 nb_pending_keycodes--;
981 keycode = keycodes[nb_pending_keycodes];
982 if (keycode & 0x80)
983 kbd_put_keycode(0xe0);
984 kbd_put_keycode(keycode | 0x80);
988 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
990 char keyname_buf[16];
991 char *separator;
992 int keyname_len, keycode, i;
994 if (nb_pending_keycodes > 0) {
995 qemu_del_timer(key_timer);
996 release_keys(NULL);
998 if (!has_hold_time)
999 hold_time = 100;
1000 i = 0;
1001 while (1) {
1002 separator = strchr(string, '-');
1003 keyname_len = separator ? separator - string : strlen(string);
1004 if (keyname_len > 0) {
1005 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1006 if (keyname_len > sizeof(keyname_buf) - 1) {
1007 term_printf("invalid key: '%s...'\n", keyname_buf);
1008 return;
1010 if (i == MAX_KEYCODES) {
1011 term_printf("too many keys\n");
1012 return;
1014 keyname_buf[keyname_len] = 0;
1015 keycode = get_keycode(keyname_buf);
1016 if (keycode < 0) {
1017 term_printf("unknown key: '%s'\n", keyname_buf);
1018 return;
1020 keycodes[i++] = keycode;
1022 if (!separator)
1023 break;
1024 string = separator + 1;
1026 nb_pending_keycodes = i;
1027 /* key down events */
1028 for (i = 0; i < nb_pending_keycodes; i++) {
1029 keycode = keycodes[i];
1030 if (keycode & 0x80)
1031 kbd_put_keycode(0xe0);
1032 kbd_put_keycode(keycode & 0x7f);
1034 /* delayed key up events */
1035 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1036 muldiv64(ticks_per_sec, hold_time, 1000));
1039 static int mouse_button_state;
1041 static void do_mouse_move(const char *dx_str, const char *dy_str,
1042 const char *dz_str)
1044 int dx, dy, dz;
1045 dx = strtol(dx_str, NULL, 0);
1046 dy = strtol(dy_str, NULL, 0);
1047 dz = 0;
1048 if (dz_str)
1049 dz = strtol(dz_str, NULL, 0);
1050 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1053 static void do_mouse_button(int button_state)
1055 mouse_button_state = button_state;
1056 kbd_mouse_event(0, 0, 0, mouse_button_state);
1059 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1061 uint32_t val;
1062 int suffix;
1064 if (has_index) {
1065 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1066 addr++;
1068 addr &= 0xffff;
1070 switch(size) {
1071 default:
1072 case 1:
1073 val = cpu_inb(NULL, addr);
1074 suffix = 'b';
1075 break;
1076 case 2:
1077 val = cpu_inw(NULL, addr);
1078 suffix = 'w';
1079 break;
1080 case 4:
1081 val = cpu_inl(NULL, addr);
1082 suffix = 'l';
1083 break;
1085 term_printf("port%c[0x%04x] = %#0*x\n",
1086 suffix, addr, size * 2, val);
1089 /* boot_set handler */
1090 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1091 static void *boot_opaque;
1093 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1095 qemu_boot_set_handler = func;
1096 boot_opaque = opaque;
1099 static void do_boot_set(const char *bootdevice)
1101 int res;
1103 if (qemu_boot_set_handler) {
1104 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1105 if (res == 0)
1106 term_printf("boot device list now set to %s\n", bootdevice);
1107 else
1108 term_printf("setting boot device list failed with error %i\n", res);
1109 } else {
1110 term_printf("no function defined to set boot device list for this architecture\n");
1114 static void do_system_reset(void)
1116 qemu_system_reset_request();
1119 static void do_system_powerdown(void)
1121 qemu_system_powerdown_request();
1124 #if defined(TARGET_I386)
1125 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1127 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1128 addr,
1129 pte & mask,
1130 pte & PG_GLOBAL_MASK ? 'G' : '-',
1131 pte & PG_PSE_MASK ? 'P' : '-',
1132 pte & PG_DIRTY_MASK ? 'D' : '-',
1133 pte & PG_ACCESSED_MASK ? 'A' : '-',
1134 pte & PG_PCD_MASK ? 'C' : '-',
1135 pte & PG_PWT_MASK ? 'T' : '-',
1136 pte & PG_USER_MASK ? 'U' : '-',
1137 pte & PG_RW_MASK ? 'W' : '-');
1140 static void tlb_info(void)
1142 CPUState *env;
1143 int l1, l2;
1144 uint32_t pgd, pde, pte;
1146 env = mon_get_cpu();
1147 if (!env)
1148 return;
1150 if (!(env->cr[0] & CR0_PG_MASK)) {
1151 term_printf("PG disabled\n");
1152 return;
1154 pgd = env->cr[3] & ~0xfff;
1155 for(l1 = 0; l1 < 1024; l1++) {
1156 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1157 pde = le32_to_cpu(pde);
1158 if (pde & PG_PRESENT_MASK) {
1159 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1160 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1161 } else {
1162 for(l2 = 0; l2 < 1024; l2++) {
1163 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1164 (uint8_t *)&pte, 4);
1165 pte = le32_to_cpu(pte);
1166 if (pte & PG_PRESENT_MASK) {
1167 print_pte((l1 << 22) + (l2 << 12),
1168 pte & ~PG_PSE_MASK,
1169 ~0xfff);
1177 static void mem_print(uint32_t *pstart, int *plast_prot,
1178 uint32_t end, int prot)
1180 int prot1;
1181 prot1 = *plast_prot;
1182 if (prot != prot1) {
1183 if (*pstart != -1) {
1184 term_printf("%08x-%08x %08x %c%c%c\n",
1185 *pstart, end, end - *pstart,
1186 prot1 & PG_USER_MASK ? 'u' : '-',
1187 'r',
1188 prot1 & PG_RW_MASK ? 'w' : '-');
1190 if (prot != 0)
1191 *pstart = end;
1192 else
1193 *pstart = -1;
1194 *plast_prot = prot;
1198 static void mem_info(void)
1200 CPUState *env;
1201 int l1, l2, prot, last_prot;
1202 uint32_t pgd, pde, pte, start, end;
1204 env = mon_get_cpu();
1205 if (!env)
1206 return;
1208 if (!(env->cr[0] & CR0_PG_MASK)) {
1209 term_printf("PG disabled\n");
1210 return;
1212 pgd = env->cr[3] & ~0xfff;
1213 last_prot = 0;
1214 start = -1;
1215 for(l1 = 0; l1 < 1024; l1++) {
1216 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1217 pde = le32_to_cpu(pde);
1218 end = l1 << 22;
1219 if (pde & PG_PRESENT_MASK) {
1220 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1221 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1222 mem_print(&start, &last_prot, end, prot);
1223 } else {
1224 for(l2 = 0; l2 < 1024; l2++) {
1225 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1226 (uint8_t *)&pte, 4);
1227 pte = le32_to_cpu(pte);
1228 end = (l1 << 22) + (l2 << 12);
1229 if (pte & PG_PRESENT_MASK) {
1230 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1231 } else {
1232 prot = 0;
1234 mem_print(&start, &last_prot, end, prot);
1237 } else {
1238 prot = 0;
1239 mem_print(&start, &last_prot, end, prot);
1243 #endif
1245 static void do_info_kqemu(void)
1247 #ifdef USE_KQEMU
1248 CPUState *env;
1249 int val;
1250 val = 0;
1251 env = mon_get_cpu();
1252 if (!env) {
1253 term_printf("No cpu initialized yet");
1254 return;
1256 val = env->kqemu_enabled;
1257 term_printf("kqemu support: ");
1258 switch(val) {
1259 default:
1260 case 0:
1261 term_printf("disabled\n");
1262 break;
1263 case 1:
1264 term_printf("enabled for user code\n");
1265 break;
1266 case 2:
1267 term_printf("enabled for user and kernel code\n");
1268 break;
1270 #else
1271 term_printf("kqemu support: not compiled\n");
1272 #endif
1275 #ifdef CONFIG_PROFILER
1277 int64_t kqemu_time;
1278 int64_t qemu_time;
1279 int64_t kqemu_exec_count;
1280 int64_t dev_time;
1281 int64_t kqemu_ret_int_count;
1282 int64_t kqemu_ret_excp_count;
1283 int64_t kqemu_ret_intr_count;
1285 static void do_info_profile(void)
1287 int64_t total;
1288 total = qemu_time;
1289 if (total == 0)
1290 total = 1;
1291 term_printf("async time %" PRId64 " (%0.3f)\n",
1292 dev_time, dev_time / (double)ticks_per_sec);
1293 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1294 qemu_time, qemu_time / (double)ticks_per_sec);
1295 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1296 kqemu_time, kqemu_time / (double)ticks_per_sec,
1297 kqemu_time / (double)total * 100.0,
1298 kqemu_exec_count,
1299 kqemu_ret_int_count,
1300 kqemu_ret_excp_count,
1301 kqemu_ret_intr_count);
1302 qemu_time = 0;
1303 kqemu_time = 0;
1304 kqemu_exec_count = 0;
1305 dev_time = 0;
1306 kqemu_ret_int_count = 0;
1307 kqemu_ret_excp_count = 0;
1308 kqemu_ret_intr_count = 0;
1309 #ifdef USE_KQEMU
1310 kqemu_record_dump();
1311 #endif
1313 #else
1314 static void do_info_profile(void)
1316 term_printf("Internal profiler not compiled\n");
1318 #endif
1320 /* Capture support */
1321 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1323 static void do_info_capture (void)
1325 int i;
1326 CaptureState *s;
1328 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1329 term_printf ("[%d]: ", i);
1330 s->ops.info (s->opaque);
1334 static void do_stop_capture (int n)
1336 int i;
1337 CaptureState *s;
1339 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1340 if (i == n) {
1341 s->ops.destroy (s->opaque);
1342 LIST_REMOVE (s, entries);
1343 qemu_free (s);
1344 return;
1349 #ifdef HAS_AUDIO
1350 int wav_start_capture (CaptureState *s, const char *path, int freq,
1351 int bits, int nchannels);
1353 static void do_wav_capture (const char *path,
1354 int has_freq, int freq,
1355 int has_bits, int bits,
1356 int has_channels, int nchannels)
1358 CaptureState *s;
1360 s = qemu_mallocz (sizeof (*s));
1361 if (!s) {
1362 term_printf ("Not enough memory to add wave capture\n");
1363 return;
1366 freq = has_freq ? freq : 44100;
1367 bits = has_bits ? bits : 16;
1368 nchannels = has_channels ? nchannels : 2;
1370 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1371 term_printf ("Faied to add wave capture\n");
1372 qemu_free (s);
1374 LIST_INSERT_HEAD (&capture_head, s, entries);
1376 #endif
1378 #if defined(TARGET_I386)
1379 static void do_inject_nmi(int cpu_index)
1381 CPUState *env;
1383 for (env = first_cpu; env != NULL; env = env->next_cpu)
1384 if (env->cpu_index == cpu_index) {
1385 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1386 break;
1389 #endif
1391 static term_cmd_t term_cmds[] = {
1392 { "help|?", "s?", do_help,
1393 "[cmd]", "show the help" },
1394 { "commit", "s", do_commit,
1395 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1396 { "info", "s?", do_info,
1397 "subcommand", "show various information about the system state" },
1398 { "q|quit", "", do_quit,
1399 "", "quit the emulator" },
1400 { "eject", "-fB", do_eject,
1401 "[-f] device", "eject a removable medium (use -f to force it)" },
1402 { "change", "BFs?", do_change,
1403 "device filename [format]", "change a removable medium, optional format" },
1404 { "screendump", "F", do_screen_dump,
1405 "filename", "save screen into PPM image 'filename'" },
1406 { "logfile", "F", do_logfile,
1407 "filename", "output logs to 'filename'" },
1408 { "log", "s", do_log,
1409 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1410 { "savevm", "s?", do_savevm,
1411 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1412 { "loadvm", "s", do_loadvm,
1413 "tag|id", "restore a VM snapshot from its tag or id" },
1414 { "delvm", "s", do_delvm,
1415 "tag|id", "delete a VM snapshot from its tag or id" },
1416 { "stop", "", do_stop,
1417 "", "stop emulation", },
1418 { "c|cont", "", do_cont,
1419 "", "resume emulation", },
1420 #ifdef CONFIG_GDBSTUB
1421 { "gdbserver", "s?", do_gdbserver,
1422 "[port]", "start gdbserver session (default port=1234)", },
1423 #endif
1424 { "x", "/l", do_memory_dump,
1425 "/fmt addr", "virtual memory dump starting at 'addr'", },
1426 { "xp", "/l", do_physical_memory_dump,
1427 "/fmt addr", "physical memory dump starting at 'addr'", },
1428 { "p|print", "/l", do_print,
1429 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1430 { "i", "/ii.", do_ioport_read,
1431 "/fmt addr", "I/O port read" },
1433 { "sendkey", "si?", do_sendkey,
1434 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1435 { "system_reset", "", do_system_reset,
1436 "", "reset the system" },
1437 { "system_powerdown", "", do_system_powerdown,
1438 "", "send system power down event" },
1439 { "sum", "ii", do_sum,
1440 "addr size", "compute the checksum of a memory region" },
1441 { "usb_add", "s", do_usb_add,
1442 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1443 { "usb_del", "s", do_usb_del,
1444 "device", "remove USB device 'bus.addr'" },
1445 { "cpu", "i", do_cpu_set,
1446 "index", "set the default CPU" },
1447 { "mouse_move", "sss?", do_mouse_move,
1448 "dx dy [dz]", "send mouse move events" },
1449 { "mouse_button", "i", do_mouse_button,
1450 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1451 { "mouse_set", "i", do_mouse_set,
1452 "index", "set which mouse device receives events" },
1453 #ifdef HAS_AUDIO
1454 { "wavcapture", "si?i?i?", do_wav_capture,
1455 "path [frequency bits channels]",
1456 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1457 #endif
1458 { "stopcapture", "i", do_stop_capture,
1459 "capture index", "stop capture" },
1460 { "memsave", "lis", do_memory_save,
1461 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1462 { "pmemsave", "lis", do_physical_memory_save,
1463 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1464 { "boot_set", "s", do_boot_set,
1465 "bootdevice", "define new values for the boot device list" },
1466 #if defined(TARGET_I386)
1467 { "nmi", "i", do_inject_nmi,
1468 "cpu", "inject an NMI on the given CPU", },
1469 #endif
1470 { "migrate", "-ds", do_migrate,
1471 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1472 { "migrate_cancel", "", do_migrate_cancel,
1473 "", "cancel the current VM migration" },
1474 { "migrate_set_speed", "s", do_migrate_set_speed,
1475 "value", "set maximum speed (in bytes) for migrations" },
1476 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1477 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1478 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1479 "[,unit=m][,media=d][index=i]\n"
1480 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1481 "[snapshot=on|off][,cache=on|off]",
1482 "add drive to PCI storage controller" },
1483 { "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" },
1484 { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" },
1485 #endif
1486 { NULL, NULL, },
1489 static term_cmd_t info_cmds[] = {
1490 { "version", "", do_info_version,
1491 "", "show the version of qemu" },
1492 { "network", "", do_info_network,
1493 "", "show the network state" },
1494 { "block", "", do_info_block,
1495 "", "show the block devices" },
1496 { "blockstats", "", do_info_blockstats,
1497 "", "show block device statistics" },
1498 { "registers", "", do_info_registers,
1499 "", "show the cpu registers" },
1500 { "cpus", "", do_info_cpus,
1501 "", "show infos for each CPU" },
1502 { "history", "", do_info_history,
1503 "", "show the command line history", },
1504 { "irq", "", irq_info,
1505 "", "show the interrupts statistics (if available)", },
1506 { "pic", "", pic_info,
1507 "", "show i8259 (PIC) state", },
1508 { "pci", "", pci_info,
1509 "", "show PCI info", },
1510 #if defined(TARGET_I386)
1511 { "tlb", "", tlb_info,
1512 "", "show virtual to physical memory mappings", },
1513 { "mem", "", mem_info,
1514 "", "show the active virtual memory mappings", },
1515 #endif
1516 { "jit", "", do_info_jit,
1517 "", "show dynamic compiler info", },
1518 { "kqemu", "", do_info_kqemu,
1519 "", "show kqemu information", },
1520 { "usb", "", usb_info,
1521 "", "show guest USB devices", },
1522 { "usbhost", "", usb_host_info,
1523 "", "show host USB devices", },
1524 { "profile", "", do_info_profile,
1525 "", "show profiling information", },
1526 { "capture", "", do_info_capture,
1527 "", "show capture information" },
1528 { "snapshots", "", do_info_snapshots,
1529 "", "show the currently saved VM snapshots" },
1530 { "pcmcia", "", pcmcia_info,
1531 "", "show guest PCMCIA status" },
1532 { "mice", "", do_info_mice,
1533 "", "show which guest mouse is receiving events" },
1534 { "vnc", "", do_info_vnc,
1535 "", "show the vnc server status"},
1536 { "name", "", do_info_name,
1537 "", "show the current VM name" },
1538 #if defined(TARGET_PPC)
1539 { "cpustats", "", do_info_cpu_stats,
1540 "", "show CPU statistics", },
1541 #endif
1542 #if defined(CONFIG_SLIRP)
1543 { "slirp", "", do_info_slirp,
1544 "", "show SLIRP statistics", },
1545 #endif
1546 { "migration", "", do_info_migration,
1547 "", "show migration information" },
1548 { NULL, NULL, },
1551 /*******************************************************************/
1553 static const char *pch;
1554 static jmp_buf expr_env;
1556 #define MD_TLONG 0
1557 #define MD_I32 1
1559 typedef struct MonitorDef {
1560 const char *name;
1561 int offset;
1562 target_long (*get_value)(struct MonitorDef *md, int val);
1563 int type;
1564 } MonitorDef;
1566 #if defined(TARGET_I386)
1567 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1569 CPUState *env = mon_get_cpu();
1570 if (!env)
1571 return 0;
1572 return env->eip + env->segs[R_CS].base;
1574 #endif
1576 #if defined(TARGET_PPC)
1577 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1579 CPUState *env = mon_get_cpu();
1580 unsigned int u;
1581 int i;
1583 if (!env)
1584 return 0;
1586 u = 0;
1587 for (i = 0; i < 8; i++)
1588 u |= env->crf[i] << (32 - (4 * i));
1590 return u;
1593 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1595 CPUState *env = mon_get_cpu();
1596 if (!env)
1597 return 0;
1598 return env->msr;
1601 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1603 CPUState *env = mon_get_cpu();
1604 if (!env)
1605 return 0;
1606 return ppc_load_xer(env);
1609 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1611 CPUState *env = mon_get_cpu();
1612 if (!env)
1613 return 0;
1614 return cpu_ppc_load_decr(env);
1617 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1619 CPUState *env = mon_get_cpu();
1620 if (!env)
1621 return 0;
1622 return cpu_ppc_load_tbu(env);
1625 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1627 CPUState *env = mon_get_cpu();
1628 if (!env)
1629 return 0;
1630 return cpu_ppc_load_tbl(env);
1632 #endif
1634 #if defined(TARGET_SPARC)
1635 #ifndef TARGET_SPARC64
1636 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1638 CPUState *env = mon_get_cpu();
1639 if (!env)
1640 return 0;
1641 return GET_PSR(env);
1643 #endif
1645 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1647 CPUState *env = mon_get_cpu();
1648 if (!env)
1649 return 0;
1650 return env->regwptr[val];
1652 #endif
1654 static MonitorDef monitor_defs[] = {
1655 #ifdef TARGET_I386
1657 #define SEG(name, seg) \
1658 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1659 { name ".base", offsetof(CPUState, segs[seg].base) },\
1660 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1662 { "eax", offsetof(CPUState, regs[0]) },
1663 { "ecx", offsetof(CPUState, regs[1]) },
1664 { "edx", offsetof(CPUState, regs[2]) },
1665 { "ebx", offsetof(CPUState, regs[3]) },
1666 { "esp|sp", offsetof(CPUState, regs[4]) },
1667 { "ebp|fp", offsetof(CPUState, regs[5]) },
1668 { "esi", offsetof(CPUState, regs[6]) },
1669 { "edi", offsetof(CPUState, regs[7]) },
1670 #ifdef TARGET_X86_64
1671 { "r8", offsetof(CPUState, regs[8]) },
1672 { "r9", offsetof(CPUState, regs[9]) },
1673 { "r10", offsetof(CPUState, regs[10]) },
1674 { "r11", offsetof(CPUState, regs[11]) },
1675 { "r12", offsetof(CPUState, regs[12]) },
1676 { "r13", offsetof(CPUState, regs[13]) },
1677 { "r14", offsetof(CPUState, regs[14]) },
1678 { "r15", offsetof(CPUState, regs[15]) },
1679 #endif
1680 { "eflags", offsetof(CPUState, eflags) },
1681 { "eip", offsetof(CPUState, eip) },
1682 SEG("cs", R_CS)
1683 SEG("ds", R_DS)
1684 SEG("es", R_ES)
1685 SEG("ss", R_SS)
1686 SEG("fs", R_FS)
1687 SEG("gs", R_GS)
1688 { "pc", 0, monitor_get_pc, },
1689 #elif defined(TARGET_PPC)
1690 /* General purpose registers */
1691 { "r0", offsetof(CPUState, gpr[0]) },
1692 { "r1", offsetof(CPUState, gpr[1]) },
1693 { "r2", offsetof(CPUState, gpr[2]) },
1694 { "r3", offsetof(CPUState, gpr[3]) },
1695 { "r4", offsetof(CPUState, gpr[4]) },
1696 { "r5", offsetof(CPUState, gpr[5]) },
1697 { "r6", offsetof(CPUState, gpr[6]) },
1698 { "r7", offsetof(CPUState, gpr[7]) },
1699 { "r8", offsetof(CPUState, gpr[8]) },
1700 { "r9", offsetof(CPUState, gpr[9]) },
1701 { "r10", offsetof(CPUState, gpr[10]) },
1702 { "r11", offsetof(CPUState, gpr[11]) },
1703 { "r12", offsetof(CPUState, gpr[12]) },
1704 { "r13", offsetof(CPUState, gpr[13]) },
1705 { "r14", offsetof(CPUState, gpr[14]) },
1706 { "r15", offsetof(CPUState, gpr[15]) },
1707 { "r16", offsetof(CPUState, gpr[16]) },
1708 { "r17", offsetof(CPUState, gpr[17]) },
1709 { "r18", offsetof(CPUState, gpr[18]) },
1710 { "r19", offsetof(CPUState, gpr[19]) },
1711 { "r20", offsetof(CPUState, gpr[20]) },
1712 { "r21", offsetof(CPUState, gpr[21]) },
1713 { "r22", offsetof(CPUState, gpr[22]) },
1714 { "r23", offsetof(CPUState, gpr[23]) },
1715 { "r24", offsetof(CPUState, gpr[24]) },
1716 { "r25", offsetof(CPUState, gpr[25]) },
1717 { "r26", offsetof(CPUState, gpr[26]) },
1718 { "r27", offsetof(CPUState, gpr[27]) },
1719 { "r28", offsetof(CPUState, gpr[28]) },
1720 { "r29", offsetof(CPUState, gpr[29]) },
1721 { "r30", offsetof(CPUState, gpr[30]) },
1722 { "r31", offsetof(CPUState, gpr[31]) },
1723 /* Floating point registers */
1724 { "f0", offsetof(CPUState, fpr[0]) },
1725 { "f1", offsetof(CPUState, fpr[1]) },
1726 { "f2", offsetof(CPUState, fpr[2]) },
1727 { "f3", offsetof(CPUState, fpr[3]) },
1728 { "f4", offsetof(CPUState, fpr[4]) },
1729 { "f5", offsetof(CPUState, fpr[5]) },
1730 { "f6", offsetof(CPUState, fpr[6]) },
1731 { "f7", offsetof(CPUState, fpr[7]) },
1732 { "f8", offsetof(CPUState, fpr[8]) },
1733 { "f9", offsetof(CPUState, fpr[9]) },
1734 { "f10", offsetof(CPUState, fpr[10]) },
1735 { "f11", offsetof(CPUState, fpr[11]) },
1736 { "f12", offsetof(CPUState, fpr[12]) },
1737 { "f13", offsetof(CPUState, fpr[13]) },
1738 { "f14", offsetof(CPUState, fpr[14]) },
1739 { "f15", offsetof(CPUState, fpr[15]) },
1740 { "f16", offsetof(CPUState, fpr[16]) },
1741 { "f17", offsetof(CPUState, fpr[17]) },
1742 { "f18", offsetof(CPUState, fpr[18]) },
1743 { "f19", offsetof(CPUState, fpr[19]) },
1744 { "f20", offsetof(CPUState, fpr[20]) },
1745 { "f21", offsetof(CPUState, fpr[21]) },
1746 { "f22", offsetof(CPUState, fpr[22]) },
1747 { "f23", offsetof(CPUState, fpr[23]) },
1748 { "f24", offsetof(CPUState, fpr[24]) },
1749 { "f25", offsetof(CPUState, fpr[25]) },
1750 { "f26", offsetof(CPUState, fpr[26]) },
1751 { "f27", offsetof(CPUState, fpr[27]) },
1752 { "f28", offsetof(CPUState, fpr[28]) },
1753 { "f29", offsetof(CPUState, fpr[29]) },
1754 { "f30", offsetof(CPUState, fpr[30]) },
1755 { "f31", offsetof(CPUState, fpr[31]) },
1756 { "fpscr", offsetof(CPUState, fpscr) },
1757 /* Next instruction pointer */
1758 { "nip|pc", offsetof(CPUState, nip) },
1759 { "lr", offsetof(CPUState, lr) },
1760 { "ctr", offsetof(CPUState, ctr) },
1761 { "decr", 0, &monitor_get_decr, },
1762 { "ccr", 0, &monitor_get_ccr, },
1763 /* Machine state register */
1764 { "msr", 0, &monitor_get_msr, },
1765 { "xer", 0, &monitor_get_xer, },
1766 { "tbu", 0, &monitor_get_tbu, },
1767 { "tbl", 0, &monitor_get_tbl, },
1768 #if defined(TARGET_PPC64)
1769 /* Address space register */
1770 { "asr", offsetof(CPUState, asr) },
1771 #endif
1772 /* Segment registers */
1773 { "sdr1", offsetof(CPUState, sdr1) },
1774 { "sr0", offsetof(CPUState, sr[0]) },
1775 { "sr1", offsetof(CPUState, sr[1]) },
1776 { "sr2", offsetof(CPUState, sr[2]) },
1777 { "sr3", offsetof(CPUState, sr[3]) },
1778 { "sr4", offsetof(CPUState, sr[4]) },
1779 { "sr5", offsetof(CPUState, sr[5]) },
1780 { "sr6", offsetof(CPUState, sr[6]) },
1781 { "sr7", offsetof(CPUState, sr[7]) },
1782 { "sr8", offsetof(CPUState, sr[8]) },
1783 { "sr9", offsetof(CPUState, sr[9]) },
1784 { "sr10", offsetof(CPUState, sr[10]) },
1785 { "sr11", offsetof(CPUState, sr[11]) },
1786 { "sr12", offsetof(CPUState, sr[12]) },
1787 { "sr13", offsetof(CPUState, sr[13]) },
1788 { "sr14", offsetof(CPUState, sr[14]) },
1789 { "sr15", offsetof(CPUState, sr[15]) },
1790 /* Too lazy to put BATs and SPRs ... */
1791 #elif defined(TARGET_SPARC)
1792 { "g0", offsetof(CPUState, gregs[0]) },
1793 { "g1", offsetof(CPUState, gregs[1]) },
1794 { "g2", offsetof(CPUState, gregs[2]) },
1795 { "g3", offsetof(CPUState, gregs[3]) },
1796 { "g4", offsetof(CPUState, gregs[4]) },
1797 { "g5", offsetof(CPUState, gregs[5]) },
1798 { "g6", offsetof(CPUState, gregs[6]) },
1799 { "g7", offsetof(CPUState, gregs[7]) },
1800 { "o0", 0, monitor_get_reg },
1801 { "o1", 1, monitor_get_reg },
1802 { "o2", 2, monitor_get_reg },
1803 { "o3", 3, monitor_get_reg },
1804 { "o4", 4, monitor_get_reg },
1805 { "o5", 5, monitor_get_reg },
1806 { "o6", 6, monitor_get_reg },
1807 { "o7", 7, monitor_get_reg },
1808 { "l0", 8, monitor_get_reg },
1809 { "l1", 9, monitor_get_reg },
1810 { "l2", 10, monitor_get_reg },
1811 { "l3", 11, monitor_get_reg },
1812 { "l4", 12, monitor_get_reg },
1813 { "l5", 13, monitor_get_reg },
1814 { "l6", 14, monitor_get_reg },
1815 { "l7", 15, monitor_get_reg },
1816 { "i0", 16, monitor_get_reg },
1817 { "i1", 17, monitor_get_reg },
1818 { "i2", 18, monitor_get_reg },
1819 { "i3", 19, monitor_get_reg },
1820 { "i4", 20, monitor_get_reg },
1821 { "i5", 21, monitor_get_reg },
1822 { "i6", 22, monitor_get_reg },
1823 { "i7", 23, monitor_get_reg },
1824 { "pc", offsetof(CPUState, pc) },
1825 { "npc", offsetof(CPUState, npc) },
1826 { "y", offsetof(CPUState, y) },
1827 #ifndef TARGET_SPARC64
1828 { "psr", 0, &monitor_get_psr, },
1829 { "wim", offsetof(CPUState, wim) },
1830 #endif
1831 { "tbr", offsetof(CPUState, tbr) },
1832 { "fsr", offsetof(CPUState, fsr) },
1833 { "f0", offsetof(CPUState, fpr[0]) },
1834 { "f1", offsetof(CPUState, fpr[1]) },
1835 { "f2", offsetof(CPUState, fpr[2]) },
1836 { "f3", offsetof(CPUState, fpr[3]) },
1837 { "f4", offsetof(CPUState, fpr[4]) },
1838 { "f5", offsetof(CPUState, fpr[5]) },
1839 { "f6", offsetof(CPUState, fpr[6]) },
1840 { "f7", offsetof(CPUState, fpr[7]) },
1841 { "f8", offsetof(CPUState, fpr[8]) },
1842 { "f9", offsetof(CPUState, fpr[9]) },
1843 { "f10", offsetof(CPUState, fpr[10]) },
1844 { "f11", offsetof(CPUState, fpr[11]) },
1845 { "f12", offsetof(CPUState, fpr[12]) },
1846 { "f13", offsetof(CPUState, fpr[13]) },
1847 { "f14", offsetof(CPUState, fpr[14]) },
1848 { "f15", offsetof(CPUState, fpr[15]) },
1849 { "f16", offsetof(CPUState, fpr[16]) },
1850 { "f17", offsetof(CPUState, fpr[17]) },
1851 { "f18", offsetof(CPUState, fpr[18]) },
1852 { "f19", offsetof(CPUState, fpr[19]) },
1853 { "f20", offsetof(CPUState, fpr[20]) },
1854 { "f21", offsetof(CPUState, fpr[21]) },
1855 { "f22", offsetof(CPUState, fpr[22]) },
1856 { "f23", offsetof(CPUState, fpr[23]) },
1857 { "f24", offsetof(CPUState, fpr[24]) },
1858 { "f25", offsetof(CPUState, fpr[25]) },
1859 { "f26", offsetof(CPUState, fpr[26]) },
1860 { "f27", offsetof(CPUState, fpr[27]) },
1861 { "f28", offsetof(CPUState, fpr[28]) },
1862 { "f29", offsetof(CPUState, fpr[29]) },
1863 { "f30", offsetof(CPUState, fpr[30]) },
1864 { "f31", offsetof(CPUState, fpr[31]) },
1865 #ifdef TARGET_SPARC64
1866 { "f32", offsetof(CPUState, fpr[32]) },
1867 { "f34", offsetof(CPUState, fpr[34]) },
1868 { "f36", offsetof(CPUState, fpr[36]) },
1869 { "f38", offsetof(CPUState, fpr[38]) },
1870 { "f40", offsetof(CPUState, fpr[40]) },
1871 { "f42", offsetof(CPUState, fpr[42]) },
1872 { "f44", offsetof(CPUState, fpr[44]) },
1873 { "f46", offsetof(CPUState, fpr[46]) },
1874 { "f48", offsetof(CPUState, fpr[48]) },
1875 { "f50", offsetof(CPUState, fpr[50]) },
1876 { "f52", offsetof(CPUState, fpr[52]) },
1877 { "f54", offsetof(CPUState, fpr[54]) },
1878 { "f56", offsetof(CPUState, fpr[56]) },
1879 { "f58", offsetof(CPUState, fpr[58]) },
1880 { "f60", offsetof(CPUState, fpr[60]) },
1881 { "f62", offsetof(CPUState, fpr[62]) },
1882 { "asi", offsetof(CPUState, asi) },
1883 { "pstate", offsetof(CPUState, pstate) },
1884 { "cansave", offsetof(CPUState, cansave) },
1885 { "canrestore", offsetof(CPUState, canrestore) },
1886 { "otherwin", offsetof(CPUState, otherwin) },
1887 { "wstate", offsetof(CPUState, wstate) },
1888 { "cleanwin", offsetof(CPUState, cleanwin) },
1889 { "fprs", offsetof(CPUState, fprs) },
1890 #endif
1891 #endif
1892 { NULL },
1895 static void expr_error(const char *fmt)
1897 term_printf(fmt);
1898 term_printf("\n");
1899 longjmp(expr_env, 1);
1902 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1903 static int get_monitor_def(target_long *pval, const char *name)
1905 MonitorDef *md;
1906 void *ptr;
1908 for(md = monitor_defs; md->name != NULL; md++) {
1909 if (compare_cmd(name, md->name)) {
1910 if (md->get_value) {
1911 *pval = md->get_value(md, md->offset);
1912 } else {
1913 CPUState *env = mon_get_cpu();
1914 if (!env)
1915 return -2;
1916 ptr = (uint8_t *)env + md->offset;
1917 switch(md->type) {
1918 case MD_I32:
1919 *pval = *(int32_t *)ptr;
1920 break;
1921 case MD_TLONG:
1922 *pval = *(target_long *)ptr;
1923 break;
1924 default:
1925 *pval = 0;
1926 break;
1929 return 0;
1932 return -1;
1935 static void next(void)
1937 if (pch != '\0') {
1938 pch++;
1939 while (isspace(*pch))
1940 pch++;
1944 static int64_t expr_sum(void);
1946 static int64_t expr_unary(void)
1948 int64_t n;
1949 char *p;
1950 int ret;
1952 switch(*pch) {
1953 case '+':
1954 next();
1955 n = expr_unary();
1956 break;
1957 case '-':
1958 next();
1959 n = -expr_unary();
1960 break;
1961 case '~':
1962 next();
1963 n = ~expr_unary();
1964 break;
1965 case '(':
1966 next();
1967 n = expr_sum();
1968 if (*pch != ')') {
1969 expr_error("')' expected");
1971 next();
1972 break;
1973 case '\'':
1974 pch++;
1975 if (*pch == '\0')
1976 expr_error("character constant expected");
1977 n = *pch;
1978 pch++;
1979 if (*pch != '\'')
1980 expr_error("missing terminating \' character");
1981 next();
1982 break;
1983 case '$':
1985 char buf[128], *q;
1986 target_long reg=0;
1988 pch++;
1989 q = buf;
1990 while ((*pch >= 'a' && *pch <= 'z') ||
1991 (*pch >= 'A' && *pch <= 'Z') ||
1992 (*pch >= '0' && *pch <= '9') ||
1993 *pch == '_' || *pch == '.') {
1994 if ((q - buf) < sizeof(buf) - 1)
1995 *q++ = *pch;
1996 pch++;
1998 while (isspace(*pch))
1999 pch++;
2000 *q = 0;
2001 ret = get_monitor_def(&reg, buf);
2002 if (ret == -1)
2003 expr_error("unknown register");
2004 else if (ret == -2)
2005 expr_error("no cpu defined");
2006 n = reg;
2008 break;
2009 case '\0':
2010 expr_error("unexpected end of expression");
2011 n = 0;
2012 break;
2013 default:
2014 #if TARGET_PHYS_ADDR_BITS > 32
2015 n = strtoull(pch, &p, 0);
2016 #else
2017 n = strtoul(pch, &p, 0);
2018 #endif
2019 if (pch == p) {
2020 expr_error("invalid char in expression");
2022 pch = p;
2023 while (isspace(*pch))
2024 pch++;
2025 break;
2027 return n;
2031 static int64_t expr_prod(void)
2033 int64_t val, val2;
2034 int op;
2036 val = expr_unary();
2037 for(;;) {
2038 op = *pch;
2039 if (op != '*' && op != '/' && op != '%')
2040 break;
2041 next();
2042 val2 = expr_unary();
2043 switch(op) {
2044 default:
2045 case '*':
2046 val *= val2;
2047 break;
2048 case '/':
2049 case '%':
2050 if (val2 == 0)
2051 expr_error("division by zero");
2052 if (op == '/')
2053 val /= val2;
2054 else
2055 val %= val2;
2056 break;
2059 return val;
2062 static int64_t expr_logic(void)
2064 int64_t val, val2;
2065 int op;
2067 val = expr_prod();
2068 for(;;) {
2069 op = *pch;
2070 if (op != '&' && op != '|' && op != '^')
2071 break;
2072 next();
2073 val2 = expr_prod();
2074 switch(op) {
2075 default:
2076 case '&':
2077 val &= val2;
2078 break;
2079 case '|':
2080 val |= val2;
2081 break;
2082 case '^':
2083 val ^= val2;
2084 break;
2087 return val;
2090 static int64_t expr_sum(void)
2092 int64_t val, val2;
2093 int op;
2095 val = expr_logic();
2096 for(;;) {
2097 op = *pch;
2098 if (op != '+' && op != '-')
2099 break;
2100 next();
2101 val2 = expr_logic();
2102 if (op == '+')
2103 val += val2;
2104 else
2105 val -= val2;
2107 return val;
2110 static int get_expr(int64_t *pval, const char **pp)
2112 pch = *pp;
2113 if (setjmp(expr_env)) {
2114 *pp = pch;
2115 return -1;
2117 while (isspace(*pch))
2118 pch++;
2119 *pval = expr_sum();
2120 *pp = pch;
2121 return 0;
2124 static int get_str(char *buf, int buf_size, const char **pp)
2126 const char *p;
2127 char *q;
2128 int c;
2130 q = buf;
2131 p = *pp;
2132 while (isspace(*p))
2133 p++;
2134 if (*p == '\0') {
2135 fail:
2136 *q = '\0';
2137 *pp = p;
2138 return -1;
2140 if (*p == '\"') {
2141 p++;
2142 while (*p != '\0' && *p != '\"') {
2143 if (*p == '\\') {
2144 p++;
2145 c = *p++;
2146 switch(c) {
2147 case 'n':
2148 c = '\n';
2149 break;
2150 case 'r':
2151 c = '\r';
2152 break;
2153 case '\\':
2154 case '\'':
2155 case '\"':
2156 break;
2157 default:
2158 qemu_printf("unsupported escape code: '\\%c'\n", c);
2159 goto fail;
2161 if ((q - buf) < buf_size - 1) {
2162 *q++ = c;
2164 } else {
2165 if ((q - buf) < buf_size - 1) {
2166 *q++ = *p;
2168 p++;
2171 if (*p != '\"') {
2172 qemu_printf("unterminated string\n");
2173 goto fail;
2175 p++;
2176 } else {
2177 while (*p != '\0' && !isspace(*p)) {
2178 if ((q - buf) < buf_size - 1) {
2179 *q++ = *p;
2181 p++;
2184 *q = '\0';
2185 *pp = p;
2186 return 0;
2189 static int default_fmt_format = 'x';
2190 static int default_fmt_size = 4;
2192 #define MAX_ARGS 16
2194 static void monitor_handle_command(const char *cmdline)
2196 const char *p, *pstart, *typestr;
2197 char *q;
2198 int c, nb_args, len, i, has_arg;
2199 term_cmd_t *cmd;
2200 char cmdname[256];
2201 char buf[1024];
2202 void *str_allocated[MAX_ARGS];
2203 void *args[MAX_ARGS];
2205 #ifdef DEBUG
2206 term_printf("command='%s'\n", cmdline);
2207 #endif
2209 /* extract the command name */
2210 p = cmdline;
2211 q = cmdname;
2212 while (isspace(*p))
2213 p++;
2214 if (*p == '\0')
2215 return;
2216 pstart = p;
2217 while (*p != '\0' && *p != '/' && !isspace(*p))
2218 p++;
2219 len = p - pstart;
2220 if (len > sizeof(cmdname) - 1)
2221 len = sizeof(cmdname) - 1;
2222 memcpy(cmdname, pstart, len);
2223 cmdname[len] = '\0';
2225 /* find the command */
2226 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2227 if (compare_cmd(cmdname, cmd->name))
2228 goto found;
2230 term_printf("unknown command: '%s'\n", cmdname);
2231 return;
2232 found:
2234 for(i = 0; i < MAX_ARGS; i++)
2235 str_allocated[i] = NULL;
2237 /* parse the parameters */
2238 typestr = cmd->args_type;
2239 nb_args = 0;
2240 for(;;) {
2241 c = *typestr;
2242 if (c == '\0')
2243 break;
2244 typestr++;
2245 switch(c) {
2246 case 'F':
2247 case 'B':
2248 case 's':
2250 int ret;
2251 char *str;
2253 while (isspace(*p))
2254 p++;
2255 if (*typestr == '?') {
2256 typestr++;
2257 if (*p == '\0') {
2258 /* no optional string: NULL argument */
2259 str = NULL;
2260 goto add_str;
2263 ret = get_str(buf, sizeof(buf), &p);
2264 if (ret < 0) {
2265 switch(c) {
2266 case 'F':
2267 term_printf("%s: filename expected\n", cmdname);
2268 break;
2269 case 'B':
2270 term_printf("%s: block device name expected\n", cmdname);
2271 break;
2272 default:
2273 term_printf("%s: string expected\n", cmdname);
2274 break;
2276 goto fail;
2278 str = qemu_malloc(strlen(buf) + 1);
2279 strcpy(str, buf);
2280 str_allocated[nb_args] = str;
2281 add_str:
2282 if (nb_args >= MAX_ARGS) {
2283 error_args:
2284 term_printf("%s: too many arguments\n", cmdname);
2285 goto fail;
2287 args[nb_args++] = str;
2289 break;
2290 case '/':
2292 int count, format, size;
2294 while (isspace(*p))
2295 p++;
2296 if (*p == '/') {
2297 /* format found */
2298 p++;
2299 count = 1;
2300 if (isdigit(*p)) {
2301 count = 0;
2302 while (isdigit(*p)) {
2303 count = count * 10 + (*p - '0');
2304 p++;
2307 size = -1;
2308 format = -1;
2309 for(;;) {
2310 switch(*p) {
2311 case 'o':
2312 case 'd':
2313 case 'u':
2314 case 'x':
2315 case 'i':
2316 case 'c':
2317 format = *p++;
2318 break;
2319 case 'b':
2320 size = 1;
2321 p++;
2322 break;
2323 case 'h':
2324 size = 2;
2325 p++;
2326 break;
2327 case 'w':
2328 size = 4;
2329 p++;
2330 break;
2331 case 'g':
2332 case 'L':
2333 size = 8;
2334 p++;
2335 break;
2336 default:
2337 goto next;
2340 next:
2341 if (*p != '\0' && !isspace(*p)) {
2342 term_printf("invalid char in format: '%c'\n", *p);
2343 goto fail;
2345 if (format < 0)
2346 format = default_fmt_format;
2347 if (format != 'i') {
2348 /* for 'i', not specifying a size gives -1 as size */
2349 if (size < 0)
2350 size = default_fmt_size;
2352 default_fmt_size = size;
2353 default_fmt_format = format;
2354 } else {
2355 count = 1;
2356 format = default_fmt_format;
2357 if (format != 'i') {
2358 size = default_fmt_size;
2359 } else {
2360 size = -1;
2363 if (nb_args + 3 > MAX_ARGS)
2364 goto error_args;
2365 args[nb_args++] = (void*)(long)count;
2366 args[nb_args++] = (void*)(long)format;
2367 args[nb_args++] = (void*)(long)size;
2369 break;
2370 case 'i':
2371 case 'l':
2373 int64_t val;
2375 while (isspace(*p))
2376 p++;
2377 if (*typestr == '?' || *typestr == '.') {
2378 if (*typestr == '?') {
2379 if (*p == '\0')
2380 has_arg = 0;
2381 else
2382 has_arg = 1;
2383 } else {
2384 if (*p == '.') {
2385 p++;
2386 while (isspace(*p))
2387 p++;
2388 has_arg = 1;
2389 } else {
2390 has_arg = 0;
2393 typestr++;
2394 if (nb_args >= MAX_ARGS)
2395 goto error_args;
2396 args[nb_args++] = (void *)(long)has_arg;
2397 if (!has_arg) {
2398 if (nb_args >= MAX_ARGS)
2399 goto error_args;
2400 val = -1;
2401 goto add_num;
2404 if (get_expr(&val, &p))
2405 goto fail;
2406 add_num:
2407 if (c == 'i') {
2408 if (nb_args >= MAX_ARGS)
2409 goto error_args;
2410 args[nb_args++] = (void *)(long)val;
2411 } else {
2412 if ((nb_args + 1) >= MAX_ARGS)
2413 goto error_args;
2414 #if TARGET_PHYS_ADDR_BITS > 32
2415 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2416 #else
2417 args[nb_args++] = (void *)0;
2418 #endif
2419 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2422 break;
2423 case '-':
2425 int has_option;
2426 /* option */
2428 c = *typestr++;
2429 if (c == '\0')
2430 goto bad_type;
2431 while (isspace(*p))
2432 p++;
2433 has_option = 0;
2434 if (*p == '-') {
2435 p++;
2436 if (*p != c) {
2437 term_printf("%s: unsupported option -%c\n",
2438 cmdname, *p);
2439 goto fail;
2441 p++;
2442 has_option = 1;
2444 if (nb_args >= MAX_ARGS)
2445 goto error_args;
2446 args[nb_args++] = (void *)(long)has_option;
2448 break;
2449 default:
2450 bad_type:
2451 term_printf("%s: unknown type '%c'\n", cmdname, c);
2452 goto fail;
2455 /* check that all arguments were parsed */
2456 while (isspace(*p))
2457 p++;
2458 if (*p != '\0') {
2459 term_printf("%s: extraneous characters at the end of line\n",
2460 cmdname);
2461 goto fail;
2464 switch(nb_args) {
2465 case 0:
2466 cmd->handler();
2467 break;
2468 case 1:
2469 cmd->handler(args[0]);
2470 break;
2471 case 2:
2472 cmd->handler(args[0], args[1]);
2473 break;
2474 case 3:
2475 cmd->handler(args[0], args[1], args[2]);
2476 break;
2477 case 4:
2478 cmd->handler(args[0], args[1], args[2], args[3]);
2479 break;
2480 case 5:
2481 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2482 break;
2483 case 6:
2484 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2485 break;
2486 case 7:
2487 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2488 break;
2489 default:
2490 term_printf("unsupported number of arguments: %d\n", nb_args);
2491 goto fail;
2493 fail:
2494 for(i = 0; i < MAX_ARGS; i++)
2495 qemu_free(str_allocated[i]);
2496 return;
2499 static void cmd_completion(const char *name, const char *list)
2501 const char *p, *pstart;
2502 char cmd[128];
2503 int len;
2505 p = list;
2506 for(;;) {
2507 pstart = p;
2508 p = strchr(p, '|');
2509 if (!p)
2510 p = pstart + strlen(pstart);
2511 len = p - pstart;
2512 if (len > sizeof(cmd) - 2)
2513 len = sizeof(cmd) - 2;
2514 memcpy(cmd, pstart, len);
2515 cmd[len] = '\0';
2516 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2517 add_completion(cmd);
2519 if (*p == '\0')
2520 break;
2521 p++;
2525 static void file_completion(const char *input)
2527 DIR *ffs;
2528 struct dirent *d;
2529 char path[1024];
2530 char file[1024], file_prefix[1024];
2531 int input_path_len;
2532 const char *p;
2534 p = strrchr(input, '/');
2535 if (!p) {
2536 input_path_len = 0;
2537 pstrcpy(file_prefix, sizeof(file_prefix), input);
2538 strcpy(path, ".");
2539 } else {
2540 input_path_len = p - input + 1;
2541 memcpy(path, input, input_path_len);
2542 if (input_path_len > sizeof(path) - 1)
2543 input_path_len = sizeof(path) - 1;
2544 path[input_path_len] = '\0';
2545 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2547 #ifdef DEBUG_COMPLETION
2548 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2549 #endif
2550 ffs = opendir(path);
2551 if (!ffs)
2552 return;
2553 for(;;) {
2554 struct stat sb;
2555 d = readdir(ffs);
2556 if (!d)
2557 break;
2558 if (strstart(d->d_name, file_prefix, NULL)) {
2559 memcpy(file, input, input_path_len);
2560 strcpy(file + input_path_len, d->d_name);
2561 /* stat the file to find out if it's a directory.
2562 * In that case add a slash to speed up typing long paths
2564 stat(file, &sb);
2565 if(S_ISDIR(sb.st_mode))
2566 strcat(file, "/");
2567 add_completion(file);
2570 closedir(ffs);
2573 static void block_completion_it(void *opaque, const char *name)
2575 const char *input = opaque;
2577 if (input[0] == '\0' ||
2578 !strncmp(name, (char *)input, strlen(input))) {
2579 add_completion(name);
2583 /* NOTE: this parser is an approximate form of the real command parser */
2584 static void parse_cmdline(const char *cmdline,
2585 int *pnb_args, char **args)
2587 const char *p;
2588 int nb_args, ret;
2589 char buf[1024];
2591 p = cmdline;
2592 nb_args = 0;
2593 for(;;) {
2594 while (isspace(*p))
2595 p++;
2596 if (*p == '\0')
2597 break;
2598 if (nb_args >= MAX_ARGS)
2599 break;
2600 ret = get_str(buf, sizeof(buf), &p);
2601 args[nb_args] = qemu_strdup(buf);
2602 nb_args++;
2603 if (ret < 0)
2604 break;
2606 *pnb_args = nb_args;
2609 void readline_find_completion(const char *cmdline)
2611 const char *cmdname;
2612 char *args[MAX_ARGS];
2613 int nb_args, i, len;
2614 const char *ptype, *str;
2615 term_cmd_t *cmd;
2616 const KeyDef *key;
2618 parse_cmdline(cmdline, &nb_args, args);
2619 #ifdef DEBUG_COMPLETION
2620 for(i = 0; i < nb_args; i++) {
2621 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2623 #endif
2625 /* if the line ends with a space, it means we want to complete the
2626 next arg */
2627 len = strlen(cmdline);
2628 if (len > 0 && isspace(cmdline[len - 1])) {
2629 if (nb_args >= MAX_ARGS)
2630 return;
2631 args[nb_args++] = qemu_strdup("");
2633 if (nb_args <= 1) {
2634 /* command completion */
2635 if (nb_args == 0)
2636 cmdname = "";
2637 else
2638 cmdname = args[0];
2639 completion_index = strlen(cmdname);
2640 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2641 cmd_completion(cmdname, cmd->name);
2643 } else {
2644 /* find the command */
2645 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2646 if (compare_cmd(args[0], cmd->name))
2647 goto found;
2649 return;
2650 found:
2651 ptype = cmd->args_type;
2652 for(i = 0; i < nb_args - 2; i++) {
2653 if (*ptype != '\0') {
2654 ptype++;
2655 while (*ptype == '?')
2656 ptype++;
2659 str = args[nb_args - 1];
2660 switch(*ptype) {
2661 case 'F':
2662 /* file completion */
2663 completion_index = strlen(str);
2664 file_completion(str);
2665 break;
2666 case 'B':
2667 /* block device name completion */
2668 completion_index = strlen(str);
2669 bdrv_iterate(block_completion_it, (void *)str);
2670 break;
2671 case 's':
2672 /* XXX: more generic ? */
2673 if (!strcmp(cmd->name, "info")) {
2674 completion_index = strlen(str);
2675 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2676 cmd_completion(str, cmd->name);
2678 } else if (!strcmp(cmd->name, "sendkey")) {
2679 completion_index = strlen(str);
2680 for(key = key_defs; key->name != NULL; key++) {
2681 cmd_completion(str, key->name);
2684 break;
2685 default:
2686 break;
2689 for(i = 0; i < nb_args; i++)
2690 qemu_free(args[i]);
2693 static int term_can_read(void *opaque)
2695 return 128;
2698 static void term_read(void *opaque, const uint8_t *buf, int size)
2700 int i;
2701 for(i = 0; i < size; i++)
2702 readline_handle_byte(buf[i]);
2705 static int monitor_suspended;
2707 void monitor_suspend(void)
2709 monitor_suspended = 1;
2712 void monitor_resume(void)
2714 monitor_suspended = 0;
2715 monitor_start_input();
2718 static void monitor_start_input(void);
2720 static void monitor_handle_command1(void *opaque, const char *cmdline)
2722 monitor_handle_command(cmdline);
2723 if (!monitor_suspended)
2724 monitor_start_input();
2727 static void monitor_start_input(void)
2729 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2732 static void term_event(void *opaque, int event)
2734 if (event != CHR_EVENT_RESET)
2735 return;
2737 if (!hide_banner)
2738 term_printf("QEMU %s monitor - type 'help' for more information\n",
2739 QEMU_VERSION);
2740 monitor_start_input();
2743 static int is_first_init = 1;
2745 void monitor_init(CharDriverState *hd, int show_banner)
2747 int i;
2749 if (is_first_init) {
2750 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2751 if (!key_timer)
2752 return;
2753 for (i = 0; i < MAX_MON; i++) {
2754 monitor_hd[i] = NULL;
2756 is_first_init = 0;
2758 for (i = 0; i < MAX_MON; i++) {
2759 if (monitor_hd[i] == NULL) {
2760 monitor_hd[i] = hd;
2761 break;
2765 hide_banner = !show_banner;
2767 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2769 readline_start("", 0, monitor_handle_command1, NULL);
2772 /* XXX: use threads ? */
2773 /* modal monitor readline */
2774 static int monitor_readline_started;
2775 static char *monitor_readline_buf;
2776 static int monitor_readline_buf_size;
2778 static void monitor_readline_cb(void *opaque, const char *input)
2780 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2781 monitor_readline_started = 0;
2784 void monitor_readline(const char *prompt, int is_password,
2785 char *buf, int buf_size)
2787 int i;
2789 if (is_password) {
2790 for (i = 0; i < MAX_MON; i++)
2791 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2792 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2794 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2795 monitor_readline_buf = buf;
2796 monitor_readline_buf_size = buf_size;
2797 monitor_readline_started = 1;
2798 while (monitor_readline_started) {
2799 main_loop_wait(10);