Merge branch 'vgabios-cvs'
[qemu-kvm/amd-iommu.git] / monitor.c
blob0fa8547cd580e754f0e4a57387837e9f21e45389
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 "balloon.h"
38 #include <dirent.h>
39 #include "qemu-timer.h"
40 #include "migration.h"
41 #include "kvm.h"
43 #include "qemu-kvm.h"
45 //#define DEBUG
46 //#define DEBUG_COMPLETION
49 * Supported types:
51 * 'F' filename
52 * 'B' block device name
53 * 's' string (accept optional quote)
54 * 'i' 32 bit integer
55 * 'l' target long (32 or 64 bit)
56 * '/' optional gdb-like print format (like "/10x")
58 * '?' optional type (for 'F', 's' and 'i')
62 typedef struct term_cmd_t {
63 const char *name;
64 const char *args_type;
65 void *handler;
66 const char *params;
67 const char *help;
68 } term_cmd_t;
70 #define MAX_MON 4
71 static CharDriverState *monitor_hd[MAX_MON];
72 static int hide_banner;
74 static const term_cmd_t term_cmds[];
75 static const term_cmd_t info_cmds[];
77 static uint8_t term_outbuf[1024];
78 static int term_outbuf_index;
80 static void monitor_start_input(void);
82 static CPUState *mon_cpu = NULL;
84 void term_flush(void)
86 int i;
87 if (term_outbuf_index > 0) {
88 for (i = 0; i < MAX_MON; i++)
89 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
90 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
91 term_outbuf_index = 0;
95 /* flush at every end of line or if the buffer is full */
96 void term_puts(const char *str)
98 char c;
99 for(;;) {
100 c = *str++;
101 if (c == '\0')
102 break;
103 if (c == '\n')
104 term_outbuf[term_outbuf_index++] = '\r';
105 term_outbuf[term_outbuf_index++] = c;
106 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
107 c == '\n')
108 term_flush();
112 void term_vprintf(const char *fmt, va_list ap)
114 char buf[4096];
115 vsnprintf(buf, sizeof(buf), fmt, ap);
116 term_puts(buf);
119 void term_printf(const char *fmt, ...)
121 va_list ap;
122 va_start(ap, fmt);
123 term_vprintf(fmt, ap);
124 va_end(ap);
127 void term_print_filename(const char *filename)
129 int i;
131 for (i = 0; filename[i]; i++) {
132 switch (filename[i]) {
133 case ' ':
134 case '"':
135 case '\\':
136 term_printf("\\%c", filename[i]);
137 break;
138 case '\t':
139 term_printf("\\t");
140 break;
141 case '\r':
142 term_printf("\\r");
143 break;
144 case '\n':
145 term_printf("\\n");
146 break;
147 default:
148 term_printf("%c", filename[i]);
149 break;
154 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
156 va_list ap;
157 va_start(ap, fmt);
158 term_vprintf(fmt, ap);
159 va_end(ap);
160 return 0;
163 static int compare_cmd(const char *name, const char *list)
165 const char *p, *pstart;
166 int len;
167 len = strlen(name);
168 p = list;
169 for(;;) {
170 pstart = p;
171 p = strchr(p, '|');
172 if (!p)
173 p = pstart + strlen(pstart);
174 if ((p - pstart) == len && !memcmp(pstart, name, len))
175 return 1;
176 if (*p == '\0')
177 break;
178 p++;
180 return 0;
183 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
185 const term_cmd_t *cmd;
187 for(cmd = cmds; cmd->name != NULL; cmd++) {
188 if (!name || !strcmp(name, cmd->name))
189 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
193 static void help_cmd(const char *name)
195 if (name && !strcmp(name, "info")) {
196 help_cmd1(info_cmds, "info ", NULL);
197 } else {
198 help_cmd1(term_cmds, "", name);
199 if (name && !strcmp(name, "log")) {
200 const CPULogItem *item;
201 term_printf("Log items (comma separated):\n");
202 term_printf("%-10s %s\n", "none", "remove all logs");
203 for(item = cpu_log_items; item->mask != 0; item++) {
204 term_printf("%-10s %s\n", item->name, item->help);
210 static void do_help(const char *name)
212 help_cmd(name);
215 static void do_commit(const char *device)
217 int i, all_devices;
219 all_devices = !strcmp(device, "all");
220 for (i = 0; i < nb_drives; i++) {
221 if (all_devices ||
222 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
223 bdrv_commit(drives_table[i].bdrv);
227 static void do_info(const char *item)
229 const term_cmd_t *cmd;
230 void (*handler)(void);
232 if (!item)
233 goto help;
234 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
235 if (compare_cmd(item, cmd->name))
236 goto found;
238 help:
239 help_cmd("info");
240 return;
241 found:
242 handler = cmd->handler;
243 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_uuid(void)
259 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
260 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
261 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
262 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
263 qemu_uuid[15]);
266 static void do_info_block(void)
268 bdrv_info();
271 static void do_info_blockstats(void)
273 bdrv_info_stats();
276 /* get the current CPU defined by the user */
277 static int mon_set_cpu(int cpu_index)
279 CPUState *env;
281 for(env = first_cpu; env != NULL; env = env->next_cpu) {
282 if (env->cpu_index == cpu_index) {
283 mon_cpu = env;
284 return 0;
287 return -1;
290 static CPUState *mon_get_cpu(void)
292 if (!mon_cpu) {
293 mon_set_cpu(0);
296 kvm_save_registers(mon_cpu);
298 return mon_cpu;
301 static void do_info_registers(void)
303 CPUState *env;
304 env = mon_get_cpu();
305 if (!env)
306 return;
307 #ifdef TARGET_I386
308 cpu_dump_state(env, NULL, monitor_fprintf,
309 X86_DUMP_FPU);
310 #else
311 cpu_dump_state(env, NULL, monitor_fprintf,
313 #endif
316 static void do_info_cpus(void)
318 CPUState *env;
320 /* just to set the default cpu if not already done */
321 mon_get_cpu();
323 for(env = first_cpu; env != NULL; env = env->next_cpu) {
324 kvm_save_registers(env);
325 term_printf("%c CPU #%d:",
326 (env == mon_cpu) ? '*' : ' ',
327 env->cpu_index);
328 #if defined(TARGET_I386)
329 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
330 #elif defined(TARGET_PPC)
331 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
332 #elif defined(TARGET_SPARC)
333 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
334 #elif defined(TARGET_MIPS)
335 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
336 #endif
337 if (env->halted)
338 term_printf(" (halted)");
339 term_printf(" thread_id=%d", env->thread_id);
340 term_printf("\n");
344 static void do_cpu_set(int index)
346 if (mon_set_cpu(index) < 0)
347 term_printf("Invalid CPU index\n");
350 static void do_cpu_set_nr(int value, const char *status)
352 int state;
354 if (!strcmp(status, "online"))
355 state = 1;
356 else if (!strcmp(status, "offline"))
357 state = 0;
358 else {
359 term_printf("invalid status: %s\n", status);
360 return;
362 #if defined(TARGET_I386) || defined(TARGET_X86_64)
363 qemu_system_cpu_hot_add(value, state);
364 #endif
367 static void do_info_jit(void)
369 dump_exec_info(NULL, monitor_fprintf);
372 static void do_info_history (void)
374 int i;
375 const char *str;
377 i = 0;
378 for(;;) {
379 str = readline_get_history(i);
380 if (!str)
381 break;
382 term_printf("%d: '%s'\n", i, str);
383 i++;
387 #if defined(TARGET_PPC)
388 /* XXX: not implemented in other targets */
389 static void do_info_cpu_stats (void)
391 CPUState *env;
393 env = mon_get_cpu();
394 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
396 #endif
398 static void do_quit(void)
400 exit(0);
403 static int eject_device(BlockDriverState *bs, int force)
405 if (bdrv_is_inserted(bs)) {
406 if (!force) {
407 if (!bdrv_is_removable(bs)) {
408 term_printf("device is not removable\n");
409 return -1;
411 if (bdrv_is_locked(bs)) {
412 term_printf("device is locked\n");
413 return -1;
416 bdrv_close(bs);
418 return 0;
421 static void do_eject(int force, const char *filename)
423 BlockDriverState *bs;
425 bs = bdrv_find(filename);
426 if (!bs) {
427 term_printf("device not found\n");
428 return;
430 eject_device(bs, force);
433 static void do_change_block(const char *device, const char *filename, const char *fmt)
435 BlockDriverState *bs;
436 BlockDriver *drv = NULL;
438 bs = bdrv_find(device);
439 if (!bs) {
440 term_printf("device not found\n");
441 return;
443 if (fmt) {
444 drv = bdrv_find_format(fmt);
445 if (!drv) {
446 term_printf("invalid format %s\n", fmt);
447 return;
450 if (eject_device(bs, 0) < 0)
451 return;
452 bdrv_open2(bs, filename, 0, drv);
453 qemu_key_check(bs, filename);
456 static void do_change_vnc(const char *target, const char *arg)
458 if (strcmp(target, "passwd") == 0 ||
459 strcmp(target, "password") == 0) {
460 char password[9];
461 if (arg) {
462 strncpy(password, arg, sizeof(password));
463 password[sizeof(password) - 1] = '\0';
464 } else
465 monitor_readline("Password: ", 1, password, sizeof(password));
466 if (vnc_display_password(NULL, password) < 0)
467 term_printf("could not set VNC server password\n");
468 } else {
469 if (vnc_display_open(NULL, target) < 0)
470 term_printf("could not start VNC server on %s\n", target);
474 static void do_change(const char *device, const char *target, const char *arg)
476 if (strcmp(device, "vnc") == 0) {
477 do_change_vnc(target, arg);
478 } else {
479 do_change_block(device, target, arg);
483 static void do_screen_dump(const char *filename)
485 vga_hw_screen_dump(filename);
488 static void do_logfile(const char *filename)
490 cpu_set_log_filename(filename);
493 static void do_log(const char *items)
495 int mask;
497 if (!strcmp(items, "none")) {
498 mask = 0;
499 } else {
500 mask = cpu_str_to_log_mask(items);
501 if (!mask) {
502 help_cmd("log");
503 return;
506 cpu_set_log(mask);
509 static void do_stop(void)
511 vm_stop(EXCP_INTERRUPT);
514 static void do_cont(void)
516 vm_start();
519 #ifdef CONFIG_GDBSTUB
520 static void do_gdbserver(const char *port)
522 if (!port)
523 port = DEFAULT_GDBSTUB_PORT;
524 if (gdbserver_start(port) < 0) {
525 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
526 } else {
527 qemu_printf("Waiting gdb connection on port '%s'\n", port);
530 #endif
532 static void term_printc(int c)
534 term_printf("'");
535 switch(c) {
536 case '\'':
537 term_printf("\\'");
538 break;
539 case '\\':
540 term_printf("\\\\");
541 break;
542 case '\n':
543 term_printf("\\n");
544 break;
545 case '\r':
546 term_printf("\\r");
547 break;
548 default:
549 if (c >= 32 && c <= 126) {
550 term_printf("%c", c);
551 } else {
552 term_printf("\\x%02x", c);
554 break;
556 term_printf("'");
559 static void memory_dump(int count, int format, int wsize,
560 target_phys_addr_t addr, int is_physical)
562 CPUState *env;
563 int nb_per_line, l, line_size, i, max_digits, len;
564 uint8_t buf[16];
565 uint64_t v;
567 if (format == 'i') {
568 int flags;
569 flags = 0;
570 env = mon_get_cpu();
571 if (!env && !is_physical)
572 return;
573 #ifdef TARGET_I386
574 if (wsize == 2) {
575 flags = 1;
576 } else if (wsize == 4) {
577 flags = 0;
578 } else {
579 /* as default we use the current CS size */
580 flags = 0;
581 if (env) {
582 #ifdef TARGET_X86_64
583 if ((env->efer & MSR_EFER_LMA) &&
584 (env->segs[R_CS].flags & DESC_L_MASK))
585 flags = 2;
586 else
587 #endif
588 if (!(env->segs[R_CS].flags & DESC_B_MASK))
589 flags = 1;
592 #endif
593 monitor_disas(env, addr, count, is_physical, flags);
594 return;
597 len = wsize * count;
598 if (wsize == 1)
599 line_size = 8;
600 else
601 line_size = 16;
602 nb_per_line = line_size / wsize;
603 max_digits = 0;
605 switch(format) {
606 case 'o':
607 max_digits = (wsize * 8 + 2) / 3;
608 break;
609 default:
610 case 'x':
611 max_digits = (wsize * 8) / 4;
612 break;
613 case 'u':
614 case 'd':
615 max_digits = (wsize * 8 * 10 + 32) / 33;
616 break;
617 case 'c':
618 wsize = 1;
619 break;
622 while (len > 0) {
623 if (is_physical)
624 term_printf(TARGET_FMT_plx ":", addr);
625 else
626 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
627 l = len;
628 if (l > line_size)
629 l = line_size;
630 if (is_physical) {
631 cpu_physical_memory_rw(addr, buf, l, 0);
632 } else {
633 env = mon_get_cpu();
634 if (!env)
635 break;
636 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
637 term_printf(" Cannot access memory\n");
638 break;
641 i = 0;
642 while (i < l) {
643 switch(wsize) {
644 default:
645 case 1:
646 v = ldub_raw(buf + i);
647 break;
648 case 2:
649 v = lduw_raw(buf + i);
650 break;
651 case 4:
652 v = (uint32_t)ldl_raw(buf + i);
653 break;
654 case 8:
655 v = ldq_raw(buf + i);
656 break;
658 term_printf(" ");
659 switch(format) {
660 case 'o':
661 term_printf("%#*" PRIo64, max_digits, v);
662 break;
663 case 'x':
664 term_printf("0x%0*" PRIx64, max_digits, v);
665 break;
666 case 'u':
667 term_printf("%*" PRIu64, max_digits, v);
668 break;
669 case 'd':
670 term_printf("%*" PRId64, max_digits, v);
671 break;
672 case 'c':
673 term_printc(v);
674 break;
676 i += wsize;
678 term_printf("\n");
679 addr += l;
680 len -= l;
684 #if TARGET_LONG_BITS == 64
685 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
686 #else
687 #define GET_TLONG(h, l) (l)
688 #endif
690 static void do_memory_dump(int count, int format, int size,
691 uint32_t addrh, uint32_t addrl)
693 target_long addr = GET_TLONG(addrh, addrl);
694 memory_dump(count, format, size, addr, 0);
697 #if TARGET_PHYS_ADDR_BITS > 32
698 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
699 #else
700 #define GET_TPHYSADDR(h, l) (l)
701 #endif
703 static void do_physical_memory_dump(int count, int format, int size,
704 uint32_t addrh, uint32_t addrl)
707 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
708 memory_dump(count, format, size, addr, 1);
711 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
713 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
714 #if TARGET_PHYS_ADDR_BITS == 32
715 switch(format) {
716 case 'o':
717 term_printf("%#o", val);
718 break;
719 case 'x':
720 term_printf("%#x", val);
721 break;
722 case 'u':
723 term_printf("%u", val);
724 break;
725 default:
726 case 'd':
727 term_printf("%d", val);
728 break;
729 case 'c':
730 term_printc(val);
731 break;
733 #else
734 switch(format) {
735 case 'o':
736 term_printf("%#" PRIo64, val);
737 break;
738 case 'x':
739 term_printf("%#" PRIx64, val);
740 break;
741 case 'u':
742 term_printf("%" PRIu64, val);
743 break;
744 default:
745 case 'd':
746 term_printf("%" PRId64, val);
747 break;
748 case 'c':
749 term_printc(val);
750 break;
752 #endif
753 term_printf("\n");
756 static void do_memory_save(unsigned int valh, unsigned int vall,
757 uint32_t size, const char *filename)
759 FILE *f;
760 target_long addr = GET_TLONG(valh, vall);
761 uint32_t l;
762 CPUState *env;
763 uint8_t buf[1024];
765 env = mon_get_cpu();
766 if (!env)
767 return;
769 f = fopen(filename, "wb");
770 if (!f) {
771 term_printf("could not open '%s'\n", filename);
772 return;
774 while (size != 0) {
775 l = sizeof(buf);
776 if (l > size)
777 l = size;
778 cpu_memory_rw_debug(env, addr, buf, l, 0);
779 fwrite(buf, 1, l, f);
780 addr += l;
781 size -= l;
783 fclose(f);
786 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
787 uint32_t size, const char *filename)
789 FILE *f;
790 uint32_t l;
791 uint8_t buf[1024];
792 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
794 f = fopen(filename, "wb");
795 if (!f) {
796 term_printf("could not open '%s'\n", filename);
797 return;
799 while (size != 0) {
800 l = sizeof(buf);
801 if (l > size)
802 l = size;
803 cpu_physical_memory_rw(addr, buf, l, 0);
804 fwrite(buf, 1, l, f);
805 fflush(f);
806 addr += l;
807 size -= l;
809 fclose(f);
812 static void do_sum(uint32_t start, uint32_t size)
814 uint32_t addr;
815 uint8_t buf[1];
816 uint16_t sum;
818 sum = 0;
819 for(addr = start; addr < (start + size); addr++) {
820 cpu_physical_memory_rw(addr, buf, 1, 0);
821 /* BSD sum algorithm ('sum' Unix command) */
822 sum = (sum >> 1) | (sum << 15);
823 sum += buf[0];
825 term_printf("%05d\n", sum);
828 typedef struct {
829 int keycode;
830 const char *name;
831 } KeyDef;
833 static const KeyDef key_defs[] = {
834 { 0x2a, "shift" },
835 { 0x36, "shift_r" },
837 { 0x38, "alt" },
838 { 0xb8, "alt_r" },
839 { 0x64, "altgr" },
840 { 0xe4, "altgr_r" },
841 { 0x1d, "ctrl" },
842 { 0x9d, "ctrl_r" },
844 { 0xdd, "menu" },
846 { 0x01, "esc" },
848 { 0x02, "1" },
849 { 0x03, "2" },
850 { 0x04, "3" },
851 { 0x05, "4" },
852 { 0x06, "5" },
853 { 0x07, "6" },
854 { 0x08, "7" },
855 { 0x09, "8" },
856 { 0x0a, "9" },
857 { 0x0b, "0" },
858 { 0x0c, "minus" },
859 { 0x0d, "equal" },
860 { 0x0e, "backspace" },
862 { 0x0f, "tab" },
863 { 0x10, "q" },
864 { 0x11, "w" },
865 { 0x12, "e" },
866 { 0x13, "r" },
867 { 0x14, "t" },
868 { 0x15, "y" },
869 { 0x16, "u" },
870 { 0x17, "i" },
871 { 0x18, "o" },
872 { 0x19, "p" },
874 { 0x1c, "ret" },
876 { 0x1e, "a" },
877 { 0x1f, "s" },
878 { 0x20, "d" },
879 { 0x21, "f" },
880 { 0x22, "g" },
881 { 0x23, "h" },
882 { 0x24, "j" },
883 { 0x25, "k" },
884 { 0x26, "l" },
886 { 0x2c, "z" },
887 { 0x2d, "x" },
888 { 0x2e, "c" },
889 { 0x2f, "v" },
890 { 0x30, "b" },
891 { 0x31, "n" },
892 { 0x32, "m" },
893 { 0x33, "comma" },
894 { 0x34, "dot" },
895 { 0x35, "slash" },
897 { 0x37, "asterisk" },
899 { 0x39, "spc" },
900 { 0x3a, "caps_lock" },
901 { 0x3b, "f1" },
902 { 0x3c, "f2" },
903 { 0x3d, "f3" },
904 { 0x3e, "f4" },
905 { 0x3f, "f5" },
906 { 0x40, "f6" },
907 { 0x41, "f7" },
908 { 0x42, "f8" },
909 { 0x43, "f9" },
910 { 0x44, "f10" },
911 { 0x45, "num_lock" },
912 { 0x46, "scroll_lock" },
914 { 0xb5, "kp_divide" },
915 { 0x37, "kp_multiply" },
916 { 0x4a, "kp_subtract" },
917 { 0x4e, "kp_add" },
918 { 0x9c, "kp_enter" },
919 { 0x53, "kp_decimal" },
920 { 0x54, "sysrq" },
922 { 0x52, "kp_0" },
923 { 0x4f, "kp_1" },
924 { 0x50, "kp_2" },
925 { 0x51, "kp_3" },
926 { 0x4b, "kp_4" },
927 { 0x4c, "kp_5" },
928 { 0x4d, "kp_6" },
929 { 0x47, "kp_7" },
930 { 0x48, "kp_8" },
931 { 0x49, "kp_9" },
933 { 0x56, "<" },
935 { 0x57, "f11" },
936 { 0x58, "f12" },
938 { 0xb7, "print" },
940 { 0xc7, "home" },
941 { 0xc9, "pgup" },
942 { 0xd1, "pgdn" },
943 { 0xcf, "end" },
945 { 0xcb, "left" },
946 { 0xc8, "up" },
947 { 0xd0, "down" },
948 { 0xcd, "right" },
950 { 0xd2, "insert" },
951 { 0xd3, "delete" },
952 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
953 { 0xf0, "stop" },
954 { 0xf1, "again" },
955 { 0xf2, "props" },
956 { 0xf3, "undo" },
957 { 0xf4, "front" },
958 { 0xf5, "copy" },
959 { 0xf6, "open" },
960 { 0xf7, "paste" },
961 { 0xf8, "find" },
962 { 0xf9, "cut" },
963 { 0xfa, "lf" },
964 { 0xfb, "help" },
965 { 0xfc, "meta_l" },
966 { 0xfd, "meta_r" },
967 { 0xfe, "compose" },
968 #endif
969 { 0, NULL },
972 static int get_keycode(const char *key)
974 const KeyDef *p;
975 char *endp;
976 int ret;
978 for(p = key_defs; p->name != NULL; p++) {
979 if (!strcmp(key, p->name))
980 return p->keycode;
982 if (strstart(key, "0x", NULL)) {
983 ret = strtoul(key, &endp, 0);
984 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
985 return ret;
987 return -1;
990 #define MAX_KEYCODES 16
991 static uint8_t keycodes[MAX_KEYCODES];
992 static int nb_pending_keycodes;
993 static QEMUTimer *key_timer;
995 static void release_keys(void *opaque)
997 int keycode;
999 while (nb_pending_keycodes > 0) {
1000 nb_pending_keycodes--;
1001 keycode = keycodes[nb_pending_keycodes];
1002 if (keycode & 0x80)
1003 kbd_put_keycode(0xe0);
1004 kbd_put_keycode(keycode | 0x80);
1008 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1010 char keyname_buf[16];
1011 char *separator;
1012 int keyname_len, keycode, i;
1014 if (nb_pending_keycodes > 0) {
1015 qemu_del_timer(key_timer);
1016 release_keys(NULL);
1018 if (!has_hold_time)
1019 hold_time = 100;
1020 i = 0;
1021 while (1) {
1022 separator = strchr(string, '-');
1023 keyname_len = separator ? separator - string : strlen(string);
1024 if (keyname_len > 0) {
1025 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1026 if (keyname_len > sizeof(keyname_buf) - 1) {
1027 term_printf("invalid key: '%s...'\n", keyname_buf);
1028 return;
1030 if (i == MAX_KEYCODES) {
1031 term_printf("too many keys\n");
1032 return;
1034 keyname_buf[keyname_len] = 0;
1035 keycode = get_keycode(keyname_buf);
1036 if (keycode < 0) {
1037 term_printf("unknown key: '%s'\n", keyname_buf);
1038 return;
1040 keycodes[i++] = keycode;
1042 if (!separator)
1043 break;
1044 string = separator + 1;
1046 nb_pending_keycodes = i;
1047 /* key down events */
1048 for (i = 0; i < nb_pending_keycodes; i++) {
1049 keycode = keycodes[i];
1050 if (keycode & 0x80)
1051 kbd_put_keycode(0xe0);
1052 kbd_put_keycode(keycode & 0x7f);
1054 /* delayed key up events */
1055 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1056 muldiv64(ticks_per_sec, hold_time, 1000));
1059 static int mouse_button_state;
1061 static void do_mouse_move(const char *dx_str, const char *dy_str,
1062 const char *dz_str)
1064 int dx, dy, dz;
1065 dx = strtol(dx_str, NULL, 0);
1066 dy = strtol(dy_str, NULL, 0);
1067 dz = 0;
1068 if (dz_str)
1069 dz = strtol(dz_str, NULL, 0);
1070 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1073 static void do_mouse_button(int button_state)
1075 mouse_button_state = button_state;
1076 kbd_mouse_event(0, 0, 0, mouse_button_state);
1079 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1081 uint32_t val;
1082 int suffix;
1084 if (has_index) {
1085 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1086 addr++;
1088 addr &= 0xffff;
1090 switch(size) {
1091 default:
1092 case 1:
1093 val = cpu_inb(NULL, addr);
1094 suffix = 'b';
1095 break;
1096 case 2:
1097 val = cpu_inw(NULL, addr);
1098 suffix = 'w';
1099 break;
1100 case 4:
1101 val = cpu_inl(NULL, addr);
1102 suffix = 'l';
1103 break;
1105 term_printf("port%c[0x%04x] = %#0*x\n",
1106 suffix, addr, size * 2, val);
1109 /* boot_set handler */
1110 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1111 static void *boot_opaque;
1113 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1115 qemu_boot_set_handler = func;
1116 boot_opaque = opaque;
1119 static void do_boot_set(const char *bootdevice)
1121 int res;
1123 if (qemu_boot_set_handler) {
1124 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1125 if (res == 0)
1126 term_printf("boot device list now set to %s\n", bootdevice);
1127 else
1128 term_printf("setting boot device list failed with error %i\n", res);
1129 } else {
1130 term_printf("no function defined to set boot device list for this architecture\n");
1134 static void do_system_reset(void)
1136 qemu_system_reset_request();
1139 static void do_system_powerdown(void)
1141 qemu_system_powerdown_request();
1144 #if defined(TARGET_I386)
1145 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1147 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1148 addr,
1149 pte & mask,
1150 pte & PG_GLOBAL_MASK ? 'G' : '-',
1151 pte & PG_PSE_MASK ? 'P' : '-',
1152 pte & PG_DIRTY_MASK ? 'D' : '-',
1153 pte & PG_ACCESSED_MASK ? 'A' : '-',
1154 pte & PG_PCD_MASK ? 'C' : '-',
1155 pte & PG_PWT_MASK ? 'T' : '-',
1156 pte & PG_USER_MASK ? 'U' : '-',
1157 pte & PG_RW_MASK ? 'W' : '-');
1160 static void tlb_info(void)
1162 CPUState *env;
1163 int l1, l2;
1164 uint32_t pgd, pde, pte;
1166 env = mon_get_cpu();
1167 if (!env)
1168 return;
1170 if (!(env->cr[0] & CR0_PG_MASK)) {
1171 term_printf("PG disabled\n");
1172 return;
1174 pgd = env->cr[3] & ~0xfff;
1175 for(l1 = 0; l1 < 1024; l1++) {
1176 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1177 pde = le32_to_cpu(pde);
1178 if (pde & PG_PRESENT_MASK) {
1179 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1180 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1181 } else {
1182 for(l2 = 0; l2 < 1024; l2++) {
1183 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1184 (uint8_t *)&pte, 4);
1185 pte = le32_to_cpu(pte);
1186 if (pte & PG_PRESENT_MASK) {
1187 print_pte((l1 << 22) + (l2 << 12),
1188 pte & ~PG_PSE_MASK,
1189 ~0xfff);
1197 static void mem_print(uint32_t *pstart, int *plast_prot,
1198 uint32_t end, int prot)
1200 int prot1;
1201 prot1 = *plast_prot;
1202 if (prot != prot1) {
1203 if (*pstart != -1) {
1204 term_printf("%08x-%08x %08x %c%c%c\n",
1205 *pstart, end, end - *pstart,
1206 prot1 & PG_USER_MASK ? 'u' : '-',
1207 'r',
1208 prot1 & PG_RW_MASK ? 'w' : '-');
1210 if (prot != 0)
1211 *pstart = end;
1212 else
1213 *pstart = -1;
1214 *plast_prot = prot;
1218 static void mem_info(void)
1220 CPUState *env;
1221 int l1, l2, prot, last_prot;
1222 uint32_t pgd, pde, pte, start, end;
1224 env = mon_get_cpu();
1225 if (!env)
1226 return;
1228 if (!(env->cr[0] & CR0_PG_MASK)) {
1229 term_printf("PG disabled\n");
1230 return;
1232 pgd = env->cr[3] & ~0xfff;
1233 last_prot = 0;
1234 start = -1;
1235 for(l1 = 0; l1 < 1024; l1++) {
1236 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1237 pde = le32_to_cpu(pde);
1238 end = l1 << 22;
1239 if (pde & PG_PRESENT_MASK) {
1240 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1241 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1242 mem_print(&start, &last_prot, end, prot);
1243 } else {
1244 for(l2 = 0; l2 < 1024; l2++) {
1245 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1246 (uint8_t *)&pte, 4);
1247 pte = le32_to_cpu(pte);
1248 end = (l1 << 22) + (l2 << 12);
1249 if (pte & PG_PRESENT_MASK) {
1250 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1251 } else {
1252 prot = 0;
1254 mem_print(&start, &last_prot, end, prot);
1257 } else {
1258 prot = 0;
1259 mem_print(&start, &last_prot, end, prot);
1263 #endif
1265 static void do_info_kqemu(void)
1267 #ifdef USE_KQEMU
1268 CPUState *env;
1269 int val;
1270 val = 0;
1271 env = mon_get_cpu();
1272 if (!env) {
1273 term_printf("No cpu initialized yet");
1274 return;
1276 val = env->kqemu_enabled;
1277 term_printf("kqemu support: ");
1278 switch(val) {
1279 default:
1280 case 0:
1281 term_printf("disabled\n");
1282 break;
1283 case 1:
1284 term_printf("enabled for user code\n");
1285 break;
1286 case 2:
1287 term_printf("enabled for user and kernel code\n");
1288 break;
1290 #else
1291 term_printf("kqemu support: not compiled\n");
1292 #endif
1295 static void do_info_kvm(void)
1297 #if defined(USE_KVM) || defined(CONFIG_KVM)
1298 term_printf("kvm support: ");
1299 if (kvm_enabled())
1300 term_printf("enabled\n");
1301 else
1302 term_printf("disabled\n");
1303 #else
1304 term_printf("kvm support: not compiled\n");
1305 #endif
1308 #ifdef CONFIG_PROFILER
1310 int64_t kqemu_time;
1311 int64_t qemu_time;
1312 int64_t kqemu_exec_count;
1313 int64_t dev_time;
1314 int64_t kqemu_ret_int_count;
1315 int64_t kqemu_ret_excp_count;
1316 int64_t kqemu_ret_intr_count;
1318 static void do_info_profile(void)
1320 int64_t total;
1321 total = qemu_time;
1322 if (total == 0)
1323 total = 1;
1324 term_printf("async time %" PRId64 " (%0.3f)\n",
1325 dev_time, dev_time / (double)ticks_per_sec);
1326 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1327 qemu_time, qemu_time / (double)ticks_per_sec);
1328 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1329 kqemu_time, kqemu_time / (double)ticks_per_sec,
1330 kqemu_time / (double)total * 100.0,
1331 kqemu_exec_count,
1332 kqemu_ret_int_count,
1333 kqemu_ret_excp_count,
1334 kqemu_ret_intr_count);
1335 qemu_time = 0;
1336 kqemu_time = 0;
1337 kqemu_exec_count = 0;
1338 dev_time = 0;
1339 kqemu_ret_int_count = 0;
1340 kqemu_ret_excp_count = 0;
1341 kqemu_ret_intr_count = 0;
1342 #ifdef USE_KQEMU
1343 kqemu_record_dump();
1344 #endif
1346 #else
1347 static void do_info_profile(void)
1349 term_printf("Internal profiler not compiled\n");
1351 #endif
1353 /* Capture support */
1354 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1356 static void do_info_capture (void)
1358 int i;
1359 CaptureState *s;
1361 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1362 term_printf ("[%d]: ", i);
1363 s->ops.info (s->opaque);
1367 static void do_stop_capture (int n)
1369 int i;
1370 CaptureState *s;
1372 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1373 if (i == n) {
1374 s->ops.destroy (s->opaque);
1375 LIST_REMOVE (s, entries);
1376 qemu_free (s);
1377 return;
1382 #ifdef HAS_AUDIO
1383 static void do_wav_capture (const char *path,
1384 int has_freq, int freq,
1385 int has_bits, int bits,
1386 int has_channels, int nchannels)
1388 CaptureState *s;
1390 s = qemu_mallocz (sizeof (*s));
1391 if (!s) {
1392 term_printf ("Not enough memory to add wave capture\n");
1393 return;
1396 freq = has_freq ? freq : 44100;
1397 bits = has_bits ? bits : 16;
1398 nchannels = has_channels ? nchannels : 2;
1400 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1401 term_printf ("Faied to add wave capture\n");
1402 qemu_free (s);
1404 LIST_INSERT_HEAD (&capture_head, s, entries);
1406 #endif
1408 #if defined(TARGET_I386)
1409 static void do_inject_nmi(int cpu_index)
1411 CPUState *env;
1413 for (env = first_cpu; env != NULL; env = env->next_cpu)
1414 if (env->cpu_index == cpu_index) {
1415 if (kvm_enabled())
1416 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1417 else
1418 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1419 break;
1422 #endif
1424 static void do_balloon(int value)
1426 ram_addr_t target = value;
1427 qemu_balloon(target << 20);
1430 static void do_info_balloon(void)
1432 ram_addr_t actual;
1434 actual = qemu_balloon_status();
1435 if (kvm_enabled() && !kvm_has_sync_mmu())
1436 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1437 else if (actual == 0)
1438 term_printf("Ballooning not activated in VM\n");
1439 else
1440 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1443 static const term_cmd_t term_cmds[] = {
1444 { "help|?", "s?", do_help,
1445 "[cmd]", "show the help" },
1446 { "commit", "s", do_commit,
1447 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1448 { "info", "s?", do_info,
1449 "subcommand", "show various information about the system state" },
1450 { "q|quit", "", do_quit,
1451 "", "quit the emulator" },
1452 { "eject", "-fB", do_eject,
1453 "[-f] device", "eject a removable medium (use -f to force it)" },
1454 { "change", "BFs?", do_change,
1455 "device filename [format]", "change a removable medium, optional format" },
1456 { "screendump", "F", do_screen_dump,
1457 "filename", "save screen into PPM image 'filename'" },
1458 { "logfile", "F", do_logfile,
1459 "filename", "output logs to 'filename'" },
1460 { "log", "s", do_log,
1461 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1462 { "savevm", "s?", do_savevm,
1463 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1464 { "loadvm", "s", do_loadvm,
1465 "tag|id", "restore a VM snapshot from its tag or id" },
1466 { "delvm", "s", do_delvm,
1467 "tag|id", "delete a VM snapshot from its tag or id" },
1468 { "stop", "", do_stop,
1469 "", "stop emulation", },
1470 { "c|cont", "", do_cont,
1471 "", "resume emulation", },
1472 #ifdef CONFIG_GDBSTUB
1473 { "gdbserver", "s?", do_gdbserver,
1474 "[port]", "start gdbserver session (default port=1234)", },
1475 #endif
1476 { "x", "/l", do_memory_dump,
1477 "/fmt addr", "virtual memory dump starting at 'addr'", },
1478 { "xp", "/l", do_physical_memory_dump,
1479 "/fmt addr", "physical memory dump starting at 'addr'", },
1480 { "p|print", "/l", do_print,
1481 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1482 { "i", "/ii.", do_ioport_read,
1483 "/fmt addr", "I/O port read" },
1485 { "sendkey", "si?", do_sendkey,
1486 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1487 { "system_reset", "", do_system_reset,
1488 "", "reset the system" },
1489 { "system_powerdown", "", do_system_powerdown,
1490 "", "send system power down event" },
1491 { "sum", "ii", do_sum,
1492 "addr size", "compute the checksum of a memory region" },
1493 { "usb_add", "s", do_usb_add,
1494 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1495 { "usb_del", "s", do_usb_del,
1496 "device", "remove USB device 'bus.addr'" },
1497 { "cpu", "i", do_cpu_set,
1498 "index", "set the default CPU" },
1499 { "mouse_move", "sss?", do_mouse_move,
1500 "dx dy [dz]", "send mouse move events" },
1501 { "mouse_button", "i", do_mouse_button,
1502 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1503 { "mouse_set", "i", do_mouse_set,
1504 "index", "set which mouse device receives events" },
1505 #ifdef HAS_AUDIO
1506 { "wavcapture", "si?i?i?", do_wav_capture,
1507 "path [frequency bits channels]",
1508 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1509 #endif
1510 { "stopcapture", "i", do_stop_capture,
1511 "capture index", "stop capture" },
1512 { "memsave", "lis", do_memory_save,
1513 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1514 { "pmemsave", "lis", do_physical_memory_save,
1515 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1516 { "boot_set", "s", do_boot_set,
1517 "bootdevice", "define new values for the boot device list" },
1518 #if defined(TARGET_I386)
1519 { "nmi", "i", do_inject_nmi,
1520 "cpu", "inject an NMI on the given CPU", },
1521 #endif
1522 { "migrate", "-ds", do_migrate,
1523 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1524 { "migrate_cancel", "", do_migrate_cancel,
1525 "", "cancel the current VM migration" },
1526 { "migrate_set_speed", "s", do_migrate_set_speed,
1527 "value", "set maximum speed (in bytes) for migrations" },
1528 { "balloon", "i", do_balloon,
1529 "target", "request VM to change it's memory allocation (in MB)" },
1530 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1531 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1532 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1533 "[,unit=m][,media=d][index=i]\n"
1534 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1535 "[snapshot=on|off][,cache=on|off]",
1536 "add drive to PCI storage controller" },
1537 { "pci_add", "iss", device_hot_add, "bus nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]", "hot-add PCI device" },
1538 { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" },
1539 #endif
1540 { NULL, NULL, },
1543 static const term_cmd_t info_cmds[] = {
1544 { "version", "", do_info_version,
1545 "", "show the version of qemu" },
1546 { "network", "", do_info_network,
1547 "", "show the network state" },
1548 { "chardev", "", qemu_chr_info,
1549 "", "show the character devices" },
1550 { "block", "", do_info_block,
1551 "", "show the block devices" },
1552 { "blockstats", "", do_info_blockstats,
1553 "", "show block device statistics" },
1554 { "registers", "", do_info_registers,
1555 "", "show the cpu registers" },
1556 { "cpus", "", do_info_cpus,
1557 "", "show infos for each CPU" },
1558 { "history", "", do_info_history,
1559 "", "show the command line history", },
1560 { "irq", "", irq_info,
1561 "", "show the interrupts statistics (if available)", },
1562 { "pic", "", pic_info,
1563 "", "show i8259 (PIC) state", },
1564 { "pci", "", pci_info,
1565 "", "show PCI info", },
1566 #if defined(TARGET_I386)
1567 { "tlb", "", tlb_info,
1568 "", "show virtual to physical memory mappings", },
1569 { "mem", "", mem_info,
1570 "", "show the active virtual memory mappings", },
1571 #endif
1572 { "jit", "", do_info_jit,
1573 "", "show dynamic compiler info", },
1574 { "kqemu", "", do_info_kqemu,
1575 "", "show kqemu information", },
1576 { "kvm", "", do_info_kvm,
1577 "", "show kvm information", },
1578 { "usb", "", usb_info,
1579 "", "show guest USB devices", },
1580 { "usbhost", "", usb_host_info,
1581 "", "show host USB devices", },
1582 { "profile", "", do_info_profile,
1583 "", "show profiling information", },
1584 { "capture", "", do_info_capture,
1585 "", "show capture information" },
1586 { "snapshots", "", do_info_snapshots,
1587 "", "show the currently saved VM snapshots" },
1588 { "pcmcia", "", pcmcia_info,
1589 "", "show guest PCMCIA status" },
1590 { "mice", "", do_info_mice,
1591 "", "show which guest mouse is receiving events" },
1592 { "vnc", "", do_info_vnc,
1593 "", "show the vnc server status"},
1594 { "name", "", do_info_name,
1595 "", "show the current VM name" },
1596 { "uuid", "", do_info_uuid,
1597 "", "show the current VM UUID" },
1598 #if defined(TARGET_PPC)
1599 { "cpustats", "", do_info_cpu_stats,
1600 "", "show CPU statistics", },
1601 #endif
1602 #if defined(CONFIG_SLIRP)
1603 { "slirp", "", do_info_slirp,
1604 "", "show SLIRP statistics", },
1605 #endif
1606 { "migrate", "", do_info_migrate, "", "show migration status" },
1607 { "balloon", "", do_info_balloon,
1608 "", "show balloon information" },
1609 { NULL, NULL, },
1612 /*******************************************************************/
1614 static const char *pch;
1615 static jmp_buf expr_env;
1617 #define MD_TLONG 0
1618 #define MD_I32 1
1620 typedef struct MonitorDef {
1621 const char *name;
1622 int offset;
1623 target_long (*get_value)(const struct MonitorDef *md, int val);
1624 int type;
1625 } MonitorDef;
1627 #if defined(TARGET_I386)
1628 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1630 CPUState *env = mon_get_cpu();
1631 if (!env)
1632 return 0;
1633 return env->eip + env->segs[R_CS].base;
1635 #endif
1637 #if defined(TARGET_PPC)
1638 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1640 CPUState *env = mon_get_cpu();
1641 unsigned int u;
1642 int i;
1644 if (!env)
1645 return 0;
1647 u = 0;
1648 for (i = 0; i < 8; i++)
1649 u |= env->crf[i] << (32 - (4 * i));
1651 return u;
1654 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1656 CPUState *env = mon_get_cpu();
1657 if (!env)
1658 return 0;
1659 return env->msr;
1662 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1664 CPUState *env = mon_get_cpu();
1665 if (!env)
1666 return 0;
1667 return env->xer;
1670 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1672 CPUState *env = mon_get_cpu();
1673 if (!env)
1674 return 0;
1675 return cpu_ppc_load_decr(env);
1678 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1680 CPUState *env = mon_get_cpu();
1681 if (!env)
1682 return 0;
1683 return cpu_ppc_load_tbu(env);
1686 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1688 CPUState *env = mon_get_cpu();
1689 if (!env)
1690 return 0;
1691 return cpu_ppc_load_tbl(env);
1693 #endif
1695 #if defined(TARGET_SPARC)
1696 #ifndef TARGET_SPARC64
1697 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1699 CPUState *env = mon_get_cpu();
1700 if (!env)
1701 return 0;
1702 return GET_PSR(env);
1704 #endif
1706 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1708 CPUState *env = mon_get_cpu();
1709 if (!env)
1710 return 0;
1711 return env->regwptr[val];
1713 #endif
1715 static const MonitorDef monitor_defs[] = {
1716 #ifdef TARGET_I386
1718 #define SEG(name, seg) \
1719 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1720 { name ".base", offsetof(CPUState, segs[seg].base) },\
1721 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1723 { "eax", offsetof(CPUState, regs[0]) },
1724 { "ecx", offsetof(CPUState, regs[1]) },
1725 { "edx", offsetof(CPUState, regs[2]) },
1726 { "ebx", offsetof(CPUState, regs[3]) },
1727 { "esp|sp", offsetof(CPUState, regs[4]) },
1728 { "ebp|fp", offsetof(CPUState, regs[5]) },
1729 { "esi", offsetof(CPUState, regs[6]) },
1730 { "edi", offsetof(CPUState, regs[7]) },
1731 #ifdef TARGET_X86_64
1732 { "r8", offsetof(CPUState, regs[8]) },
1733 { "r9", offsetof(CPUState, regs[9]) },
1734 { "r10", offsetof(CPUState, regs[10]) },
1735 { "r11", offsetof(CPUState, regs[11]) },
1736 { "r12", offsetof(CPUState, regs[12]) },
1737 { "r13", offsetof(CPUState, regs[13]) },
1738 { "r14", offsetof(CPUState, regs[14]) },
1739 { "r15", offsetof(CPUState, regs[15]) },
1740 #endif
1741 { "eflags", offsetof(CPUState, eflags) },
1742 { "eip", offsetof(CPUState, eip) },
1743 SEG("cs", R_CS)
1744 SEG("ds", R_DS)
1745 SEG("es", R_ES)
1746 SEG("ss", R_SS)
1747 SEG("fs", R_FS)
1748 SEG("gs", R_GS)
1749 { "pc", 0, monitor_get_pc, },
1750 #elif defined(TARGET_PPC)
1751 /* General purpose registers */
1752 { "r0", offsetof(CPUState, gpr[0]) },
1753 { "r1", offsetof(CPUState, gpr[1]) },
1754 { "r2", offsetof(CPUState, gpr[2]) },
1755 { "r3", offsetof(CPUState, gpr[3]) },
1756 { "r4", offsetof(CPUState, gpr[4]) },
1757 { "r5", offsetof(CPUState, gpr[5]) },
1758 { "r6", offsetof(CPUState, gpr[6]) },
1759 { "r7", offsetof(CPUState, gpr[7]) },
1760 { "r8", offsetof(CPUState, gpr[8]) },
1761 { "r9", offsetof(CPUState, gpr[9]) },
1762 { "r10", offsetof(CPUState, gpr[10]) },
1763 { "r11", offsetof(CPUState, gpr[11]) },
1764 { "r12", offsetof(CPUState, gpr[12]) },
1765 { "r13", offsetof(CPUState, gpr[13]) },
1766 { "r14", offsetof(CPUState, gpr[14]) },
1767 { "r15", offsetof(CPUState, gpr[15]) },
1768 { "r16", offsetof(CPUState, gpr[16]) },
1769 { "r17", offsetof(CPUState, gpr[17]) },
1770 { "r18", offsetof(CPUState, gpr[18]) },
1771 { "r19", offsetof(CPUState, gpr[19]) },
1772 { "r20", offsetof(CPUState, gpr[20]) },
1773 { "r21", offsetof(CPUState, gpr[21]) },
1774 { "r22", offsetof(CPUState, gpr[22]) },
1775 { "r23", offsetof(CPUState, gpr[23]) },
1776 { "r24", offsetof(CPUState, gpr[24]) },
1777 { "r25", offsetof(CPUState, gpr[25]) },
1778 { "r26", offsetof(CPUState, gpr[26]) },
1779 { "r27", offsetof(CPUState, gpr[27]) },
1780 { "r28", offsetof(CPUState, gpr[28]) },
1781 { "r29", offsetof(CPUState, gpr[29]) },
1782 { "r30", offsetof(CPUState, gpr[30]) },
1783 { "r31", offsetof(CPUState, gpr[31]) },
1784 /* Floating point registers */
1785 { "f0", offsetof(CPUState, fpr[0]) },
1786 { "f1", offsetof(CPUState, fpr[1]) },
1787 { "f2", offsetof(CPUState, fpr[2]) },
1788 { "f3", offsetof(CPUState, fpr[3]) },
1789 { "f4", offsetof(CPUState, fpr[4]) },
1790 { "f5", offsetof(CPUState, fpr[5]) },
1791 { "f6", offsetof(CPUState, fpr[6]) },
1792 { "f7", offsetof(CPUState, fpr[7]) },
1793 { "f8", offsetof(CPUState, fpr[8]) },
1794 { "f9", offsetof(CPUState, fpr[9]) },
1795 { "f10", offsetof(CPUState, fpr[10]) },
1796 { "f11", offsetof(CPUState, fpr[11]) },
1797 { "f12", offsetof(CPUState, fpr[12]) },
1798 { "f13", offsetof(CPUState, fpr[13]) },
1799 { "f14", offsetof(CPUState, fpr[14]) },
1800 { "f15", offsetof(CPUState, fpr[15]) },
1801 { "f16", offsetof(CPUState, fpr[16]) },
1802 { "f17", offsetof(CPUState, fpr[17]) },
1803 { "f18", offsetof(CPUState, fpr[18]) },
1804 { "f19", offsetof(CPUState, fpr[19]) },
1805 { "f20", offsetof(CPUState, fpr[20]) },
1806 { "f21", offsetof(CPUState, fpr[21]) },
1807 { "f22", offsetof(CPUState, fpr[22]) },
1808 { "f23", offsetof(CPUState, fpr[23]) },
1809 { "f24", offsetof(CPUState, fpr[24]) },
1810 { "f25", offsetof(CPUState, fpr[25]) },
1811 { "f26", offsetof(CPUState, fpr[26]) },
1812 { "f27", offsetof(CPUState, fpr[27]) },
1813 { "f28", offsetof(CPUState, fpr[28]) },
1814 { "f29", offsetof(CPUState, fpr[29]) },
1815 { "f30", offsetof(CPUState, fpr[30]) },
1816 { "f31", offsetof(CPUState, fpr[31]) },
1817 { "fpscr", offsetof(CPUState, fpscr) },
1818 /* Next instruction pointer */
1819 { "nip|pc", offsetof(CPUState, nip) },
1820 { "lr", offsetof(CPUState, lr) },
1821 { "ctr", offsetof(CPUState, ctr) },
1822 { "decr", 0, &monitor_get_decr, },
1823 { "ccr", 0, &monitor_get_ccr, },
1824 /* Machine state register */
1825 { "msr", 0, &monitor_get_msr, },
1826 { "xer", 0, &monitor_get_xer, },
1827 { "tbu", 0, &monitor_get_tbu, },
1828 { "tbl", 0, &monitor_get_tbl, },
1829 #if defined(TARGET_PPC64)
1830 /* Address space register */
1831 { "asr", offsetof(CPUState, asr) },
1832 #endif
1833 /* Segment registers */
1834 { "sdr1", offsetof(CPUState, sdr1) },
1835 { "sr0", offsetof(CPUState, sr[0]) },
1836 { "sr1", offsetof(CPUState, sr[1]) },
1837 { "sr2", offsetof(CPUState, sr[2]) },
1838 { "sr3", offsetof(CPUState, sr[3]) },
1839 { "sr4", offsetof(CPUState, sr[4]) },
1840 { "sr5", offsetof(CPUState, sr[5]) },
1841 { "sr6", offsetof(CPUState, sr[6]) },
1842 { "sr7", offsetof(CPUState, sr[7]) },
1843 { "sr8", offsetof(CPUState, sr[8]) },
1844 { "sr9", offsetof(CPUState, sr[9]) },
1845 { "sr10", offsetof(CPUState, sr[10]) },
1846 { "sr11", offsetof(CPUState, sr[11]) },
1847 { "sr12", offsetof(CPUState, sr[12]) },
1848 { "sr13", offsetof(CPUState, sr[13]) },
1849 { "sr14", offsetof(CPUState, sr[14]) },
1850 { "sr15", offsetof(CPUState, sr[15]) },
1851 /* Too lazy to put BATs and SPRs ... */
1852 #elif defined(TARGET_SPARC)
1853 { "g0", offsetof(CPUState, gregs[0]) },
1854 { "g1", offsetof(CPUState, gregs[1]) },
1855 { "g2", offsetof(CPUState, gregs[2]) },
1856 { "g3", offsetof(CPUState, gregs[3]) },
1857 { "g4", offsetof(CPUState, gregs[4]) },
1858 { "g5", offsetof(CPUState, gregs[5]) },
1859 { "g6", offsetof(CPUState, gregs[6]) },
1860 { "g7", offsetof(CPUState, gregs[7]) },
1861 { "o0", 0, monitor_get_reg },
1862 { "o1", 1, monitor_get_reg },
1863 { "o2", 2, monitor_get_reg },
1864 { "o3", 3, monitor_get_reg },
1865 { "o4", 4, monitor_get_reg },
1866 { "o5", 5, monitor_get_reg },
1867 { "o6", 6, monitor_get_reg },
1868 { "o7", 7, monitor_get_reg },
1869 { "l0", 8, monitor_get_reg },
1870 { "l1", 9, monitor_get_reg },
1871 { "l2", 10, monitor_get_reg },
1872 { "l3", 11, monitor_get_reg },
1873 { "l4", 12, monitor_get_reg },
1874 { "l5", 13, monitor_get_reg },
1875 { "l6", 14, monitor_get_reg },
1876 { "l7", 15, monitor_get_reg },
1877 { "i0", 16, monitor_get_reg },
1878 { "i1", 17, monitor_get_reg },
1879 { "i2", 18, monitor_get_reg },
1880 { "i3", 19, monitor_get_reg },
1881 { "i4", 20, monitor_get_reg },
1882 { "i5", 21, monitor_get_reg },
1883 { "i6", 22, monitor_get_reg },
1884 { "i7", 23, monitor_get_reg },
1885 { "pc", offsetof(CPUState, pc) },
1886 { "npc", offsetof(CPUState, npc) },
1887 { "y", offsetof(CPUState, y) },
1888 #ifndef TARGET_SPARC64
1889 { "psr", 0, &monitor_get_psr, },
1890 { "wim", offsetof(CPUState, wim) },
1891 #endif
1892 { "tbr", offsetof(CPUState, tbr) },
1893 { "fsr", offsetof(CPUState, fsr) },
1894 { "f0", offsetof(CPUState, fpr[0]) },
1895 { "f1", offsetof(CPUState, fpr[1]) },
1896 { "f2", offsetof(CPUState, fpr[2]) },
1897 { "f3", offsetof(CPUState, fpr[3]) },
1898 { "f4", offsetof(CPUState, fpr[4]) },
1899 { "f5", offsetof(CPUState, fpr[5]) },
1900 { "f6", offsetof(CPUState, fpr[6]) },
1901 { "f7", offsetof(CPUState, fpr[7]) },
1902 { "f8", offsetof(CPUState, fpr[8]) },
1903 { "f9", offsetof(CPUState, fpr[9]) },
1904 { "f10", offsetof(CPUState, fpr[10]) },
1905 { "f11", offsetof(CPUState, fpr[11]) },
1906 { "f12", offsetof(CPUState, fpr[12]) },
1907 { "f13", offsetof(CPUState, fpr[13]) },
1908 { "f14", offsetof(CPUState, fpr[14]) },
1909 { "f15", offsetof(CPUState, fpr[15]) },
1910 { "f16", offsetof(CPUState, fpr[16]) },
1911 { "f17", offsetof(CPUState, fpr[17]) },
1912 { "f18", offsetof(CPUState, fpr[18]) },
1913 { "f19", offsetof(CPUState, fpr[19]) },
1914 { "f20", offsetof(CPUState, fpr[20]) },
1915 { "f21", offsetof(CPUState, fpr[21]) },
1916 { "f22", offsetof(CPUState, fpr[22]) },
1917 { "f23", offsetof(CPUState, fpr[23]) },
1918 { "f24", offsetof(CPUState, fpr[24]) },
1919 { "f25", offsetof(CPUState, fpr[25]) },
1920 { "f26", offsetof(CPUState, fpr[26]) },
1921 { "f27", offsetof(CPUState, fpr[27]) },
1922 { "f28", offsetof(CPUState, fpr[28]) },
1923 { "f29", offsetof(CPUState, fpr[29]) },
1924 { "f30", offsetof(CPUState, fpr[30]) },
1925 { "f31", offsetof(CPUState, fpr[31]) },
1926 #ifdef TARGET_SPARC64
1927 { "f32", offsetof(CPUState, fpr[32]) },
1928 { "f34", offsetof(CPUState, fpr[34]) },
1929 { "f36", offsetof(CPUState, fpr[36]) },
1930 { "f38", offsetof(CPUState, fpr[38]) },
1931 { "f40", offsetof(CPUState, fpr[40]) },
1932 { "f42", offsetof(CPUState, fpr[42]) },
1933 { "f44", offsetof(CPUState, fpr[44]) },
1934 { "f46", offsetof(CPUState, fpr[46]) },
1935 { "f48", offsetof(CPUState, fpr[48]) },
1936 { "f50", offsetof(CPUState, fpr[50]) },
1937 { "f52", offsetof(CPUState, fpr[52]) },
1938 { "f54", offsetof(CPUState, fpr[54]) },
1939 { "f56", offsetof(CPUState, fpr[56]) },
1940 { "f58", offsetof(CPUState, fpr[58]) },
1941 { "f60", offsetof(CPUState, fpr[60]) },
1942 { "f62", offsetof(CPUState, fpr[62]) },
1943 { "asi", offsetof(CPUState, asi) },
1944 { "pstate", offsetof(CPUState, pstate) },
1945 { "cansave", offsetof(CPUState, cansave) },
1946 { "canrestore", offsetof(CPUState, canrestore) },
1947 { "otherwin", offsetof(CPUState, otherwin) },
1948 { "wstate", offsetof(CPUState, wstate) },
1949 { "cleanwin", offsetof(CPUState, cleanwin) },
1950 { "fprs", offsetof(CPUState, fprs) },
1951 #endif
1952 #endif
1953 { NULL },
1956 static void expr_error(const char *fmt)
1958 term_printf(fmt);
1959 term_printf("\n");
1960 longjmp(expr_env, 1);
1963 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1964 static int get_monitor_def(target_long *pval, const char *name)
1966 const MonitorDef *md;
1967 void *ptr;
1969 for(md = monitor_defs; md->name != NULL; md++) {
1970 if (compare_cmd(name, md->name)) {
1971 if (md->get_value) {
1972 *pval = md->get_value(md, md->offset);
1973 } else {
1974 CPUState *env = mon_get_cpu();
1975 if (!env)
1976 return -2;
1977 ptr = (uint8_t *)env + md->offset;
1978 switch(md->type) {
1979 case MD_I32:
1980 *pval = *(int32_t *)ptr;
1981 break;
1982 case MD_TLONG:
1983 *pval = *(target_long *)ptr;
1984 break;
1985 default:
1986 *pval = 0;
1987 break;
1990 return 0;
1993 return -1;
1996 static void next(void)
1998 if (pch != '\0') {
1999 pch++;
2000 while (qemu_isspace(*pch))
2001 pch++;
2005 static int64_t expr_sum(void);
2007 static int64_t expr_unary(void)
2009 int64_t n;
2010 char *p;
2011 int ret;
2013 switch(*pch) {
2014 case '+':
2015 next();
2016 n = expr_unary();
2017 break;
2018 case '-':
2019 next();
2020 n = -expr_unary();
2021 break;
2022 case '~':
2023 next();
2024 n = ~expr_unary();
2025 break;
2026 case '(':
2027 next();
2028 n = expr_sum();
2029 if (*pch != ')') {
2030 expr_error("')' expected");
2032 next();
2033 break;
2034 case '\'':
2035 pch++;
2036 if (*pch == '\0')
2037 expr_error("character constant expected");
2038 n = *pch;
2039 pch++;
2040 if (*pch != '\'')
2041 expr_error("missing terminating \' character");
2042 next();
2043 break;
2044 case '$':
2046 char buf[128], *q;
2047 target_long reg=0;
2049 pch++;
2050 q = buf;
2051 while ((*pch >= 'a' && *pch <= 'z') ||
2052 (*pch >= 'A' && *pch <= 'Z') ||
2053 (*pch >= '0' && *pch <= '9') ||
2054 *pch == '_' || *pch == '.') {
2055 if ((q - buf) < sizeof(buf) - 1)
2056 *q++ = *pch;
2057 pch++;
2059 while (qemu_isspace(*pch))
2060 pch++;
2061 *q = 0;
2062 ret = get_monitor_def(&reg, buf);
2063 if (ret == -1)
2064 expr_error("unknown register");
2065 else if (ret == -2)
2066 expr_error("no cpu defined");
2067 n = reg;
2069 break;
2070 case '\0':
2071 expr_error("unexpected end of expression");
2072 n = 0;
2073 break;
2074 default:
2075 #if TARGET_PHYS_ADDR_BITS > 32
2076 n = strtoull(pch, &p, 0);
2077 #else
2078 n = strtoul(pch, &p, 0);
2079 #endif
2080 if (pch == p) {
2081 expr_error("invalid char in expression");
2083 pch = p;
2084 while (qemu_isspace(*pch))
2085 pch++;
2086 break;
2088 return n;
2092 static int64_t expr_prod(void)
2094 int64_t val, val2;
2095 int op;
2097 val = expr_unary();
2098 for(;;) {
2099 op = *pch;
2100 if (op != '*' && op != '/' && op != '%')
2101 break;
2102 next();
2103 val2 = expr_unary();
2104 switch(op) {
2105 default:
2106 case '*':
2107 val *= val2;
2108 break;
2109 case '/':
2110 case '%':
2111 if (val2 == 0)
2112 expr_error("division by zero");
2113 if (op == '/')
2114 val /= val2;
2115 else
2116 val %= val2;
2117 break;
2120 return val;
2123 static int64_t expr_logic(void)
2125 int64_t val, val2;
2126 int op;
2128 val = expr_prod();
2129 for(;;) {
2130 op = *pch;
2131 if (op != '&' && op != '|' && op != '^')
2132 break;
2133 next();
2134 val2 = expr_prod();
2135 switch(op) {
2136 default:
2137 case '&':
2138 val &= val2;
2139 break;
2140 case '|':
2141 val |= val2;
2142 break;
2143 case '^':
2144 val ^= val2;
2145 break;
2148 return val;
2151 static int64_t expr_sum(void)
2153 int64_t val, val2;
2154 int op;
2156 val = expr_logic();
2157 for(;;) {
2158 op = *pch;
2159 if (op != '+' && op != '-')
2160 break;
2161 next();
2162 val2 = expr_logic();
2163 if (op == '+')
2164 val += val2;
2165 else
2166 val -= val2;
2168 return val;
2171 static int get_expr(int64_t *pval, const char **pp)
2173 pch = *pp;
2174 if (setjmp(expr_env)) {
2175 *pp = pch;
2176 return -1;
2178 while (qemu_isspace(*pch))
2179 pch++;
2180 *pval = expr_sum();
2181 *pp = pch;
2182 return 0;
2185 static int get_str(char *buf, int buf_size, const char **pp)
2187 const char *p;
2188 char *q;
2189 int c;
2191 q = buf;
2192 p = *pp;
2193 while (qemu_isspace(*p))
2194 p++;
2195 if (*p == '\0') {
2196 fail:
2197 *q = '\0';
2198 *pp = p;
2199 return -1;
2201 if (*p == '\"') {
2202 p++;
2203 while (*p != '\0' && *p != '\"') {
2204 if (*p == '\\') {
2205 p++;
2206 c = *p++;
2207 switch(c) {
2208 case 'n':
2209 c = '\n';
2210 break;
2211 case 'r':
2212 c = '\r';
2213 break;
2214 case '\\':
2215 case '\'':
2216 case '\"':
2217 break;
2218 default:
2219 qemu_printf("unsupported escape code: '\\%c'\n", c);
2220 goto fail;
2222 if ((q - buf) < buf_size - 1) {
2223 *q++ = c;
2225 } else {
2226 if ((q - buf) < buf_size - 1) {
2227 *q++ = *p;
2229 p++;
2232 if (*p != '\"') {
2233 qemu_printf("unterminated string\n");
2234 goto fail;
2236 p++;
2237 } else {
2238 while (*p != '\0' && !qemu_isspace(*p)) {
2239 if ((q - buf) < buf_size - 1) {
2240 *q++ = *p;
2242 p++;
2245 *q = '\0';
2246 *pp = p;
2247 return 0;
2250 static int default_fmt_format = 'x';
2251 static int default_fmt_size = 4;
2253 #define MAX_ARGS 16
2255 static void monitor_handle_command(const char *cmdline)
2257 const char *p, *pstart, *typestr;
2258 char *q;
2259 int c, nb_args, len, i, has_arg;
2260 const term_cmd_t *cmd;
2261 char cmdname[256];
2262 char buf[1024];
2263 void *str_allocated[MAX_ARGS];
2264 void *args[MAX_ARGS];
2265 void (*handler_0)(void);
2266 void (*handler_1)(void *arg0);
2267 void (*handler_2)(void *arg0, void *arg1);
2268 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2269 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2270 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2271 void *arg4);
2272 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2273 void *arg4, void *arg5);
2274 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2275 void *arg4, void *arg5, void *arg6);
2277 #ifdef DEBUG
2278 term_printf("command='%s'\n", cmdline);
2279 #endif
2281 /* extract the command name */
2282 p = cmdline;
2283 q = cmdname;
2284 while (qemu_isspace(*p))
2285 p++;
2286 if (*p == '\0')
2287 return;
2288 pstart = p;
2289 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2290 p++;
2291 len = p - pstart;
2292 if (len > sizeof(cmdname) - 1)
2293 len = sizeof(cmdname) - 1;
2294 memcpy(cmdname, pstart, len);
2295 cmdname[len] = '\0';
2297 /* find the command */
2298 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2299 if (compare_cmd(cmdname, cmd->name))
2300 goto found;
2302 term_printf("unknown command: '%s'\n", cmdname);
2303 return;
2304 found:
2306 for(i = 0; i < MAX_ARGS; i++)
2307 str_allocated[i] = NULL;
2309 /* parse the parameters */
2310 typestr = cmd->args_type;
2311 nb_args = 0;
2312 for(;;) {
2313 c = *typestr;
2314 if (c == '\0')
2315 break;
2316 typestr++;
2317 switch(c) {
2318 case 'F':
2319 case 'B':
2320 case 's':
2322 int ret;
2323 char *str;
2325 while (qemu_isspace(*p))
2326 p++;
2327 if (*typestr == '?') {
2328 typestr++;
2329 if (*p == '\0') {
2330 /* no optional string: NULL argument */
2331 str = NULL;
2332 goto add_str;
2335 ret = get_str(buf, sizeof(buf), &p);
2336 if (ret < 0) {
2337 switch(c) {
2338 case 'F':
2339 term_printf("%s: filename expected\n", cmdname);
2340 break;
2341 case 'B':
2342 term_printf("%s: block device name expected\n", cmdname);
2343 break;
2344 default:
2345 term_printf("%s: string expected\n", cmdname);
2346 break;
2348 goto fail;
2350 str = qemu_malloc(strlen(buf) + 1);
2351 pstrcpy(str, sizeof(buf), buf);
2352 str_allocated[nb_args] = str;
2353 add_str:
2354 if (nb_args >= MAX_ARGS) {
2355 error_args:
2356 term_printf("%s: too many arguments\n", cmdname);
2357 goto fail;
2359 args[nb_args++] = str;
2361 break;
2362 case '/':
2364 int count, format, size;
2366 while (qemu_isspace(*p))
2367 p++;
2368 if (*p == '/') {
2369 /* format found */
2370 p++;
2371 count = 1;
2372 if (qemu_isdigit(*p)) {
2373 count = 0;
2374 while (qemu_isdigit(*p)) {
2375 count = count * 10 + (*p - '0');
2376 p++;
2379 size = -1;
2380 format = -1;
2381 for(;;) {
2382 switch(*p) {
2383 case 'o':
2384 case 'd':
2385 case 'u':
2386 case 'x':
2387 case 'i':
2388 case 'c':
2389 format = *p++;
2390 break;
2391 case 'b':
2392 size = 1;
2393 p++;
2394 break;
2395 case 'h':
2396 size = 2;
2397 p++;
2398 break;
2399 case 'w':
2400 size = 4;
2401 p++;
2402 break;
2403 case 'g':
2404 case 'L':
2405 size = 8;
2406 p++;
2407 break;
2408 default:
2409 goto next;
2412 next:
2413 if (*p != '\0' && !qemu_isspace(*p)) {
2414 term_printf("invalid char in format: '%c'\n", *p);
2415 goto fail;
2417 if (format < 0)
2418 format = default_fmt_format;
2419 if (format != 'i') {
2420 /* for 'i', not specifying a size gives -1 as size */
2421 if (size < 0)
2422 size = default_fmt_size;
2423 default_fmt_size = size;
2425 default_fmt_format = format;
2426 } else {
2427 count = 1;
2428 format = default_fmt_format;
2429 if (format != 'i') {
2430 size = default_fmt_size;
2431 } else {
2432 size = -1;
2435 if (nb_args + 3 > MAX_ARGS)
2436 goto error_args;
2437 args[nb_args++] = (void*)(long)count;
2438 args[nb_args++] = (void*)(long)format;
2439 args[nb_args++] = (void*)(long)size;
2441 break;
2442 case 'i':
2443 case 'l':
2445 int64_t val;
2447 while (qemu_isspace(*p))
2448 p++;
2449 if (*typestr == '?' || *typestr == '.') {
2450 if (*typestr == '?') {
2451 if (*p == '\0')
2452 has_arg = 0;
2453 else
2454 has_arg = 1;
2455 } else {
2456 if (*p == '.') {
2457 p++;
2458 while (qemu_isspace(*p))
2459 p++;
2460 has_arg = 1;
2461 } else {
2462 has_arg = 0;
2465 typestr++;
2466 if (nb_args >= MAX_ARGS)
2467 goto error_args;
2468 args[nb_args++] = (void *)(long)has_arg;
2469 if (!has_arg) {
2470 if (nb_args >= MAX_ARGS)
2471 goto error_args;
2472 val = -1;
2473 goto add_num;
2476 if (get_expr(&val, &p))
2477 goto fail;
2478 add_num:
2479 if (c == 'i') {
2480 if (nb_args >= MAX_ARGS)
2481 goto error_args;
2482 args[nb_args++] = (void *)(long)val;
2483 } else {
2484 if ((nb_args + 1) >= MAX_ARGS)
2485 goto error_args;
2486 #if TARGET_PHYS_ADDR_BITS > 32
2487 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2488 #else
2489 args[nb_args++] = (void *)0;
2490 #endif
2491 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2494 break;
2495 case '-':
2497 int has_option;
2498 /* option */
2500 c = *typestr++;
2501 if (c == '\0')
2502 goto bad_type;
2503 while (qemu_isspace(*p))
2504 p++;
2505 has_option = 0;
2506 if (*p == '-') {
2507 p++;
2508 if (*p != c) {
2509 term_printf("%s: unsupported option -%c\n",
2510 cmdname, *p);
2511 goto fail;
2513 p++;
2514 has_option = 1;
2516 if (nb_args >= MAX_ARGS)
2517 goto error_args;
2518 args[nb_args++] = (void *)(long)has_option;
2520 break;
2521 default:
2522 bad_type:
2523 term_printf("%s: unknown type '%c'\n", cmdname, c);
2524 goto fail;
2527 /* check that all arguments were parsed */
2528 while (qemu_isspace(*p))
2529 p++;
2530 if (*p != '\0') {
2531 term_printf("%s: extraneous characters at the end of line\n",
2532 cmdname);
2533 goto fail;
2536 switch(nb_args) {
2537 case 0:
2538 handler_0 = cmd->handler;
2539 handler_0();
2540 break;
2541 case 1:
2542 handler_1 = cmd->handler;
2543 handler_1(args[0]);
2544 break;
2545 case 2:
2546 handler_2 = cmd->handler;
2547 handler_2(args[0], args[1]);
2548 break;
2549 case 3:
2550 handler_3 = cmd->handler;
2551 handler_3(args[0], args[1], args[2]);
2552 break;
2553 case 4:
2554 handler_4 = cmd->handler;
2555 handler_4(args[0], args[1], args[2], args[3]);
2556 break;
2557 case 5:
2558 handler_5 = cmd->handler;
2559 handler_5(args[0], args[1], args[2], args[3], args[4]);
2560 break;
2561 case 6:
2562 handler_6 = cmd->handler;
2563 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2564 break;
2565 case 7:
2566 handler_7 = cmd->handler;
2567 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2568 break;
2569 default:
2570 term_printf("unsupported number of arguments: %d\n", nb_args);
2571 goto fail;
2573 fail:
2574 for(i = 0; i < MAX_ARGS; i++)
2575 qemu_free(str_allocated[i]);
2576 return;
2579 static void cmd_completion(const char *name, const char *list)
2581 const char *p, *pstart;
2582 char cmd[128];
2583 int len;
2585 p = list;
2586 for(;;) {
2587 pstart = p;
2588 p = strchr(p, '|');
2589 if (!p)
2590 p = pstart + strlen(pstart);
2591 len = p - pstart;
2592 if (len > sizeof(cmd) - 2)
2593 len = sizeof(cmd) - 2;
2594 memcpy(cmd, pstart, len);
2595 cmd[len] = '\0';
2596 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2597 add_completion(cmd);
2599 if (*p == '\0')
2600 break;
2601 p++;
2605 static void file_completion(const char *input)
2607 DIR *ffs;
2608 struct dirent *d;
2609 char path[1024];
2610 char file[1024], file_prefix[1024];
2611 int input_path_len;
2612 const char *p;
2614 p = strrchr(input, '/');
2615 if (!p) {
2616 input_path_len = 0;
2617 pstrcpy(file_prefix, sizeof(file_prefix), input);
2618 pstrcpy(path, sizeof(path), ".");
2619 } else {
2620 input_path_len = p - input + 1;
2621 memcpy(path, input, input_path_len);
2622 if (input_path_len > sizeof(path) - 1)
2623 input_path_len = sizeof(path) - 1;
2624 path[input_path_len] = '\0';
2625 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2627 #ifdef DEBUG_COMPLETION
2628 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2629 #endif
2630 ffs = opendir(path);
2631 if (!ffs)
2632 return;
2633 for(;;) {
2634 struct stat sb;
2635 d = readdir(ffs);
2636 if (!d)
2637 break;
2638 if (strstart(d->d_name, file_prefix, NULL)) {
2639 memcpy(file, input, input_path_len);
2640 if (input_path_len < sizeof(file))
2641 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2642 d->d_name);
2643 /* stat the file to find out if it's a directory.
2644 * In that case add a slash to speed up typing long paths
2646 stat(file, &sb);
2647 if(S_ISDIR(sb.st_mode))
2648 pstrcat(file, sizeof(file), "/");
2649 add_completion(file);
2652 closedir(ffs);
2655 static void block_completion_it(void *opaque, const char *name)
2657 const char *input = opaque;
2659 if (input[0] == '\0' ||
2660 !strncmp(name, (char *)input, strlen(input))) {
2661 add_completion(name);
2665 /* NOTE: this parser is an approximate form of the real command parser */
2666 static void parse_cmdline(const char *cmdline,
2667 int *pnb_args, char **args)
2669 const char *p;
2670 int nb_args, ret;
2671 char buf[1024];
2673 p = cmdline;
2674 nb_args = 0;
2675 for(;;) {
2676 while (qemu_isspace(*p))
2677 p++;
2678 if (*p == '\0')
2679 break;
2680 if (nb_args >= MAX_ARGS)
2681 break;
2682 ret = get_str(buf, sizeof(buf), &p);
2683 args[nb_args] = qemu_strdup(buf);
2684 nb_args++;
2685 if (ret < 0)
2686 break;
2688 *pnb_args = nb_args;
2691 void readline_find_completion(const char *cmdline)
2693 const char *cmdname;
2694 char *args[MAX_ARGS];
2695 int nb_args, i, len;
2696 const char *ptype, *str;
2697 const term_cmd_t *cmd;
2698 const KeyDef *key;
2700 parse_cmdline(cmdline, &nb_args, args);
2701 #ifdef DEBUG_COMPLETION
2702 for(i = 0; i < nb_args; i++) {
2703 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2705 #endif
2707 /* if the line ends with a space, it means we want to complete the
2708 next arg */
2709 len = strlen(cmdline);
2710 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2711 if (nb_args >= MAX_ARGS)
2712 return;
2713 args[nb_args++] = qemu_strdup("");
2715 if (nb_args <= 1) {
2716 /* command completion */
2717 if (nb_args == 0)
2718 cmdname = "";
2719 else
2720 cmdname = args[0];
2721 completion_index = strlen(cmdname);
2722 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2723 cmd_completion(cmdname, cmd->name);
2725 } else {
2726 /* find the command */
2727 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2728 if (compare_cmd(args[0], cmd->name))
2729 goto found;
2731 return;
2732 found:
2733 ptype = cmd->args_type;
2734 for(i = 0; i < nb_args - 2; i++) {
2735 if (*ptype != '\0') {
2736 ptype++;
2737 while (*ptype == '?')
2738 ptype++;
2741 str = args[nb_args - 1];
2742 switch(*ptype) {
2743 case 'F':
2744 /* file completion */
2745 completion_index = strlen(str);
2746 file_completion(str);
2747 break;
2748 case 'B':
2749 /* block device name completion */
2750 completion_index = strlen(str);
2751 bdrv_iterate(block_completion_it, (void *)str);
2752 break;
2753 case 's':
2754 /* XXX: more generic ? */
2755 if (!strcmp(cmd->name, "info")) {
2756 completion_index = strlen(str);
2757 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2758 cmd_completion(str, cmd->name);
2760 } else if (!strcmp(cmd->name, "sendkey")) {
2761 completion_index = strlen(str);
2762 for(key = key_defs; key->name != NULL; key++) {
2763 cmd_completion(str, key->name);
2766 break;
2767 default:
2768 break;
2771 for(i = 0; i < nb_args; i++)
2772 qemu_free(args[i]);
2775 static int term_can_read(void *opaque)
2777 return 128;
2780 static void term_read(void *opaque, const uint8_t *buf, int size)
2782 int i;
2783 for(i = 0; i < size; i++)
2784 readline_handle_byte(buf[i]);
2787 static int monitor_suspended;
2789 static void monitor_handle_command1(void *opaque, const char *cmdline)
2791 monitor_handle_command(cmdline);
2792 if (!monitor_suspended)
2793 monitor_start_input();
2794 else
2795 monitor_suspended = 2;
2798 void monitor_suspend(void)
2800 monitor_suspended = 1;
2803 void monitor_resume(void)
2805 if (monitor_suspended == 2)
2806 monitor_start_input();
2807 monitor_suspended = 0;
2810 static void monitor_start_input(void)
2812 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2815 static void term_event(void *opaque, int event)
2817 if (event != CHR_EVENT_RESET)
2818 return;
2820 if (!hide_banner)
2821 term_printf("QEMU %s monitor - type 'help' for more information\n",
2822 QEMU_VERSION);
2823 monitor_start_input();
2826 static int is_first_init = 1;
2828 void monitor_init(CharDriverState *hd, int show_banner)
2830 int i;
2832 if (is_first_init) {
2833 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2834 if (!key_timer)
2835 return;
2836 for (i = 0; i < MAX_MON; i++) {
2837 monitor_hd[i] = NULL;
2839 is_first_init = 0;
2841 for (i = 0; i < MAX_MON; i++) {
2842 if (monitor_hd[i] == NULL) {
2843 monitor_hd[i] = hd;
2844 break;
2848 hide_banner = !show_banner;
2850 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2852 readline_start("", 0, monitor_handle_command1, NULL);
2855 /* XXX: use threads ? */
2856 /* modal monitor readline */
2857 static int monitor_readline_started;
2858 static char *monitor_readline_buf;
2859 static int monitor_readline_buf_size;
2861 static void monitor_readline_cb(void *opaque, const char *input)
2863 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2864 monitor_readline_started = 0;
2867 void monitor_readline(const char *prompt, int is_password,
2868 char *buf, int buf_size)
2870 int i;
2871 int old_focus[MAX_MON];
2873 if (is_password) {
2874 for (i = 0; i < MAX_MON; i++) {
2875 old_focus[i] = 0;
2876 if (monitor_hd[i]) {
2877 old_focus[i] = monitor_hd[i]->focus;
2878 monitor_hd[i]->focus = 0;
2879 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2884 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2885 monitor_readline_buf = buf;
2886 monitor_readline_buf_size = buf_size;
2887 monitor_readline_started = 1;
2888 while (monitor_readline_started) {
2889 main_loop_wait(10);
2891 /* restore original focus */
2892 if (is_password) {
2893 for (i = 0; i < MAX_MON; i++)
2894 if (old_focus[i])
2895 monitor_hd[i]->focus = old_focus[i];