kvm: bios: read UUID from firmware interface
[qemu-kvm/fedora.git] / monitor.c
blob79b6b4c10691d30ca85a7ce06d6618d0f1b35b00
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 "balloon.h"
39 #include <dirent.h>
40 #include "qemu-timer.h"
41 #include "migration.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 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)
458 if (strcmp(target, "passwd") == 0 ||
459 strcmp(target, "password") == 0) {
460 char password[9];
461 monitor_readline("Password: ", 1, password, sizeof(password)-1);
462 password[sizeof(password)-1] = '\0';
463 if (vnc_display_password(NULL, password) < 0)
464 term_printf("could not set VNC server password\n");
465 } else {
466 if (vnc_display_open(NULL, target) < 0)
467 term_printf("could not start VNC server on %s\n", target);
471 static void do_change(const char *device, const char *target, const char *fmt)
473 if (strcmp(device, "vnc") == 0) {
474 do_change_vnc(target);
475 } else {
476 do_change_block(device, target, fmt);
480 static void do_screen_dump(const char *filename)
482 vga_hw_screen_dump(filename);
485 static void do_logfile(const char *filename)
487 cpu_set_log_filename(filename);
490 static void do_log(const char *items)
492 int mask;
494 if (!strcmp(items, "none")) {
495 mask = 0;
496 } else {
497 mask = cpu_str_to_log_mask(items);
498 if (!mask) {
499 help_cmd("log");
500 return;
503 cpu_set_log(mask);
506 static void do_stop(void)
508 vm_stop(EXCP_INTERRUPT);
511 static void do_cont(void)
513 vm_start();
516 #ifdef CONFIG_GDBSTUB
517 static void do_gdbserver(const char *port)
519 if (!port)
520 port = DEFAULT_GDBSTUB_PORT;
521 if (gdbserver_start(port) < 0) {
522 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
523 } else {
524 qemu_printf("Waiting gdb connection on port '%s'\n", port);
527 #endif
529 static void term_printc(int c)
531 term_printf("'");
532 switch(c) {
533 case '\'':
534 term_printf("\\'");
535 break;
536 case '\\':
537 term_printf("\\\\");
538 break;
539 case '\n':
540 term_printf("\\n");
541 break;
542 case '\r':
543 term_printf("\\r");
544 break;
545 default:
546 if (c >= 32 && c <= 126) {
547 term_printf("%c", c);
548 } else {
549 term_printf("\\x%02x", c);
551 break;
553 term_printf("'");
556 static void memory_dump(int count, int format, int wsize,
557 target_phys_addr_t addr, int is_physical)
559 CPUState *env;
560 int nb_per_line, l, line_size, i, max_digits, len;
561 uint8_t buf[16];
562 uint64_t v;
564 if (format == 'i') {
565 int flags;
566 flags = 0;
567 env = mon_get_cpu();
568 if (!env && !is_physical)
569 return;
570 #ifdef TARGET_I386
571 if (wsize == 2) {
572 flags = 1;
573 } else if (wsize == 4) {
574 flags = 0;
575 } else {
576 /* as default we use the current CS size */
577 flags = 0;
578 if (env) {
579 #ifdef TARGET_X86_64
580 if ((env->efer & MSR_EFER_LMA) &&
581 (env->segs[R_CS].flags & DESC_L_MASK))
582 flags = 2;
583 else
584 #endif
585 if (!(env->segs[R_CS].flags & DESC_B_MASK))
586 flags = 1;
589 #endif
590 monitor_disas(env, addr, count, is_physical, flags);
591 return;
594 len = wsize * count;
595 if (wsize == 1)
596 line_size = 8;
597 else
598 line_size = 16;
599 nb_per_line = line_size / wsize;
600 max_digits = 0;
602 switch(format) {
603 case 'o':
604 max_digits = (wsize * 8 + 2) / 3;
605 break;
606 default:
607 case 'x':
608 max_digits = (wsize * 8) / 4;
609 break;
610 case 'u':
611 case 'd':
612 max_digits = (wsize * 8 * 10 + 32) / 33;
613 break;
614 case 'c':
615 wsize = 1;
616 break;
619 while (len > 0) {
620 if (is_physical)
621 term_printf(TARGET_FMT_plx ":", addr);
622 else
623 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
624 l = len;
625 if (l > line_size)
626 l = line_size;
627 if (is_physical) {
628 cpu_physical_memory_rw(addr, buf, l, 0);
629 } else {
630 env = mon_get_cpu();
631 if (!env)
632 break;
633 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
634 term_printf(" Cannot access memory\n");
635 break;
638 i = 0;
639 while (i < l) {
640 switch(wsize) {
641 default:
642 case 1:
643 v = ldub_raw(buf + i);
644 break;
645 case 2:
646 v = lduw_raw(buf + i);
647 break;
648 case 4:
649 v = (uint32_t)ldl_raw(buf + i);
650 break;
651 case 8:
652 v = ldq_raw(buf + i);
653 break;
655 term_printf(" ");
656 switch(format) {
657 case 'o':
658 term_printf("%#*" PRIo64, max_digits, v);
659 break;
660 case 'x':
661 term_printf("0x%0*" PRIx64, max_digits, v);
662 break;
663 case 'u':
664 term_printf("%*" PRIu64, max_digits, v);
665 break;
666 case 'd':
667 term_printf("%*" PRId64, max_digits, v);
668 break;
669 case 'c':
670 term_printc(v);
671 break;
673 i += wsize;
675 term_printf("\n");
676 addr += l;
677 len -= l;
681 #if TARGET_LONG_BITS == 64
682 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
683 #else
684 #define GET_TLONG(h, l) (l)
685 #endif
687 static void do_memory_dump(int count, int format, int size,
688 uint32_t addrh, uint32_t addrl)
690 target_long addr = GET_TLONG(addrh, addrl);
691 memory_dump(count, format, size, addr, 0);
694 #if TARGET_PHYS_ADDR_BITS > 32
695 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
696 #else
697 #define GET_TPHYSADDR(h, l) (l)
698 #endif
700 static void do_physical_memory_dump(int count, int format, int size,
701 uint32_t addrh, uint32_t addrl)
704 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
705 memory_dump(count, format, size, addr, 1);
708 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
710 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
711 #if TARGET_PHYS_ADDR_BITS == 32
712 switch(format) {
713 case 'o':
714 term_printf("%#o", val);
715 break;
716 case 'x':
717 term_printf("%#x", val);
718 break;
719 case 'u':
720 term_printf("%u", val);
721 break;
722 default:
723 case 'd':
724 term_printf("%d", val);
725 break;
726 case 'c':
727 term_printc(val);
728 break;
730 #else
731 switch(format) {
732 case 'o':
733 term_printf("%#" PRIo64, val);
734 break;
735 case 'x':
736 term_printf("%#" PRIx64, val);
737 break;
738 case 'u':
739 term_printf("%" PRIu64, val);
740 break;
741 default:
742 case 'd':
743 term_printf("%" PRId64, val);
744 break;
745 case 'c':
746 term_printc(val);
747 break;
749 #endif
750 term_printf("\n");
753 static void do_memory_save(unsigned int valh, unsigned int vall,
754 uint32_t size, const char *filename)
756 FILE *f;
757 target_long addr = GET_TLONG(valh, vall);
758 uint32_t l;
759 CPUState *env;
760 uint8_t buf[1024];
762 env = mon_get_cpu();
763 if (!env)
764 return;
766 f = fopen(filename, "wb");
767 if (!f) {
768 term_printf("could not open '%s'\n", filename);
769 return;
771 while (size != 0) {
772 l = sizeof(buf);
773 if (l > size)
774 l = size;
775 cpu_memory_rw_debug(env, addr, buf, l, 0);
776 fwrite(buf, 1, l, f);
777 addr += l;
778 size -= l;
780 fclose(f);
783 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
784 uint32_t size, const char *filename)
786 FILE *f;
787 uint32_t l;
788 uint8_t buf[1024];
789 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
791 f = fopen(filename, "wb");
792 if (!f) {
793 term_printf("could not open '%s'\n", filename);
794 return;
796 while (size != 0) {
797 l = sizeof(buf);
798 if (l > size)
799 l = size;
800 cpu_physical_memory_rw(addr, buf, l, 0);
801 fwrite(buf, 1, l, f);
802 fflush(f);
803 addr += l;
804 size -= l;
806 fclose(f);
809 static void do_sum(uint32_t start, uint32_t size)
811 uint32_t addr;
812 uint8_t buf[1];
813 uint16_t sum;
815 sum = 0;
816 for(addr = start; addr < (start + size); addr++) {
817 cpu_physical_memory_rw(addr, buf, 1, 0);
818 /* BSD sum algorithm ('sum' Unix command) */
819 sum = (sum >> 1) | (sum << 15);
820 sum += buf[0];
822 term_printf("%05d\n", sum);
825 typedef struct {
826 int keycode;
827 const char *name;
828 } KeyDef;
830 static const KeyDef key_defs[] = {
831 { 0x2a, "shift" },
832 { 0x36, "shift_r" },
834 { 0x38, "alt" },
835 { 0xb8, "alt_r" },
836 { 0x64, "altgr" },
837 { 0xe4, "altgr_r" },
838 { 0x1d, "ctrl" },
839 { 0x9d, "ctrl_r" },
841 { 0xdd, "menu" },
843 { 0x01, "esc" },
845 { 0x02, "1" },
846 { 0x03, "2" },
847 { 0x04, "3" },
848 { 0x05, "4" },
849 { 0x06, "5" },
850 { 0x07, "6" },
851 { 0x08, "7" },
852 { 0x09, "8" },
853 { 0x0a, "9" },
854 { 0x0b, "0" },
855 { 0x0c, "minus" },
856 { 0x0d, "equal" },
857 { 0x0e, "backspace" },
859 { 0x0f, "tab" },
860 { 0x10, "q" },
861 { 0x11, "w" },
862 { 0x12, "e" },
863 { 0x13, "r" },
864 { 0x14, "t" },
865 { 0x15, "y" },
866 { 0x16, "u" },
867 { 0x17, "i" },
868 { 0x18, "o" },
869 { 0x19, "p" },
871 { 0x1c, "ret" },
873 { 0x1e, "a" },
874 { 0x1f, "s" },
875 { 0x20, "d" },
876 { 0x21, "f" },
877 { 0x22, "g" },
878 { 0x23, "h" },
879 { 0x24, "j" },
880 { 0x25, "k" },
881 { 0x26, "l" },
883 { 0x2c, "z" },
884 { 0x2d, "x" },
885 { 0x2e, "c" },
886 { 0x2f, "v" },
887 { 0x30, "b" },
888 { 0x31, "n" },
889 { 0x32, "m" },
890 { 0x33, "comma" },
891 { 0x34, "dot" },
892 { 0x35, "slash" },
894 { 0x37, "asterisk" },
896 { 0x39, "spc" },
897 { 0x3a, "caps_lock" },
898 { 0x3b, "f1" },
899 { 0x3c, "f2" },
900 { 0x3d, "f3" },
901 { 0x3e, "f4" },
902 { 0x3f, "f5" },
903 { 0x40, "f6" },
904 { 0x41, "f7" },
905 { 0x42, "f8" },
906 { 0x43, "f9" },
907 { 0x44, "f10" },
908 { 0x45, "num_lock" },
909 { 0x46, "scroll_lock" },
911 { 0xb5, "kp_divide" },
912 { 0x37, "kp_multiply" },
913 { 0x4a, "kp_subtract" },
914 { 0x4e, "kp_add" },
915 { 0x9c, "kp_enter" },
916 { 0x53, "kp_decimal" },
917 { 0x54, "sysrq" },
919 { 0x52, "kp_0" },
920 { 0x4f, "kp_1" },
921 { 0x50, "kp_2" },
922 { 0x51, "kp_3" },
923 { 0x4b, "kp_4" },
924 { 0x4c, "kp_5" },
925 { 0x4d, "kp_6" },
926 { 0x47, "kp_7" },
927 { 0x48, "kp_8" },
928 { 0x49, "kp_9" },
930 { 0x56, "<" },
932 { 0x57, "f11" },
933 { 0x58, "f12" },
935 { 0xb7, "print" },
937 { 0xc7, "home" },
938 { 0xc9, "pgup" },
939 { 0xd1, "pgdn" },
940 { 0xcf, "end" },
942 { 0xcb, "left" },
943 { 0xc8, "up" },
944 { 0xd0, "down" },
945 { 0xcd, "right" },
947 { 0xd2, "insert" },
948 { 0xd3, "delete" },
949 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
950 { 0xf0, "stop" },
951 { 0xf1, "again" },
952 { 0xf2, "props" },
953 { 0xf3, "undo" },
954 { 0xf4, "front" },
955 { 0xf5, "copy" },
956 { 0xf6, "open" },
957 { 0xf7, "paste" },
958 { 0xf8, "find" },
959 { 0xf9, "cut" },
960 { 0xfa, "lf" },
961 { 0xfb, "help" },
962 { 0xfc, "meta_l" },
963 { 0xfd, "meta_r" },
964 { 0xfe, "compose" },
965 #endif
966 { 0, NULL },
969 static int get_keycode(const char *key)
971 const KeyDef *p;
972 char *endp;
973 int ret;
975 for(p = key_defs; p->name != NULL; p++) {
976 if (!strcmp(key, p->name))
977 return p->keycode;
979 if (strstart(key, "0x", NULL)) {
980 ret = strtoul(key, &endp, 0);
981 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
982 return ret;
984 return -1;
987 #define MAX_KEYCODES 16
988 static uint8_t keycodes[MAX_KEYCODES];
989 static int nb_pending_keycodes;
990 static QEMUTimer *key_timer;
992 static void release_keys(void *opaque)
994 int keycode;
996 while (nb_pending_keycodes > 0) {
997 nb_pending_keycodes--;
998 keycode = keycodes[nb_pending_keycodes];
999 if (keycode & 0x80)
1000 kbd_put_keycode(0xe0);
1001 kbd_put_keycode(keycode | 0x80);
1005 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1007 char keyname_buf[16];
1008 char *separator;
1009 int keyname_len, keycode, i;
1011 if (nb_pending_keycodes > 0) {
1012 qemu_del_timer(key_timer);
1013 release_keys(NULL);
1015 if (!has_hold_time)
1016 hold_time = 100;
1017 i = 0;
1018 while (1) {
1019 separator = strchr(string, '-');
1020 keyname_len = separator ? separator - string : strlen(string);
1021 if (keyname_len > 0) {
1022 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1023 if (keyname_len > sizeof(keyname_buf) - 1) {
1024 term_printf("invalid key: '%s...'\n", keyname_buf);
1025 return;
1027 if (i == MAX_KEYCODES) {
1028 term_printf("too many keys\n");
1029 return;
1031 keyname_buf[keyname_len] = 0;
1032 keycode = get_keycode(keyname_buf);
1033 if (keycode < 0) {
1034 term_printf("unknown key: '%s'\n", keyname_buf);
1035 return;
1037 keycodes[i++] = keycode;
1039 if (!separator)
1040 break;
1041 string = separator + 1;
1043 nb_pending_keycodes = i;
1044 /* key down events */
1045 for (i = 0; i < nb_pending_keycodes; i++) {
1046 keycode = keycodes[i];
1047 if (keycode & 0x80)
1048 kbd_put_keycode(0xe0);
1049 kbd_put_keycode(keycode & 0x7f);
1051 /* delayed key up events */
1052 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1053 muldiv64(ticks_per_sec, hold_time, 1000));
1056 static int mouse_button_state;
1058 static void do_mouse_move(const char *dx_str, const char *dy_str,
1059 const char *dz_str)
1061 int dx, dy, dz;
1062 dx = strtol(dx_str, NULL, 0);
1063 dy = strtol(dy_str, NULL, 0);
1064 dz = 0;
1065 if (dz_str)
1066 dz = strtol(dz_str, NULL, 0);
1067 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1070 static void do_mouse_button(int button_state)
1072 mouse_button_state = button_state;
1073 kbd_mouse_event(0, 0, 0, mouse_button_state);
1076 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1078 uint32_t val;
1079 int suffix;
1081 if (has_index) {
1082 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1083 addr++;
1085 addr &= 0xffff;
1087 switch(size) {
1088 default:
1089 case 1:
1090 val = cpu_inb(NULL, addr);
1091 suffix = 'b';
1092 break;
1093 case 2:
1094 val = cpu_inw(NULL, addr);
1095 suffix = 'w';
1096 break;
1097 case 4:
1098 val = cpu_inl(NULL, addr);
1099 suffix = 'l';
1100 break;
1102 term_printf("port%c[0x%04x] = %#0*x\n",
1103 suffix, addr, size * 2, val);
1106 /* boot_set handler */
1107 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1108 static void *boot_opaque;
1110 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1112 qemu_boot_set_handler = func;
1113 boot_opaque = opaque;
1116 static void do_boot_set(const char *bootdevice)
1118 int res;
1120 if (qemu_boot_set_handler) {
1121 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1122 if (res == 0)
1123 term_printf("boot device list now set to %s\n", bootdevice);
1124 else
1125 term_printf("setting boot device list failed with error %i\n", res);
1126 } else {
1127 term_printf("no function defined to set boot device list for this architecture\n");
1131 static void do_system_reset(void)
1133 qemu_system_reset_request();
1136 static void do_system_powerdown(void)
1138 qemu_system_powerdown_request();
1141 #if defined(TARGET_I386)
1142 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1144 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1145 addr,
1146 pte & mask,
1147 pte & PG_GLOBAL_MASK ? 'G' : '-',
1148 pte & PG_PSE_MASK ? 'P' : '-',
1149 pte & PG_DIRTY_MASK ? 'D' : '-',
1150 pte & PG_ACCESSED_MASK ? 'A' : '-',
1151 pte & PG_PCD_MASK ? 'C' : '-',
1152 pte & PG_PWT_MASK ? 'T' : '-',
1153 pte & PG_USER_MASK ? 'U' : '-',
1154 pte & PG_RW_MASK ? 'W' : '-');
1157 static void tlb_info(void)
1159 CPUState *env;
1160 int l1, l2;
1161 uint32_t pgd, pde, pte;
1163 env = mon_get_cpu();
1164 if (!env)
1165 return;
1167 if (!(env->cr[0] & CR0_PG_MASK)) {
1168 term_printf("PG disabled\n");
1169 return;
1171 pgd = env->cr[3] & ~0xfff;
1172 for(l1 = 0; l1 < 1024; l1++) {
1173 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1174 pde = le32_to_cpu(pde);
1175 if (pde & PG_PRESENT_MASK) {
1176 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1177 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1178 } else {
1179 for(l2 = 0; l2 < 1024; l2++) {
1180 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1181 (uint8_t *)&pte, 4);
1182 pte = le32_to_cpu(pte);
1183 if (pte & PG_PRESENT_MASK) {
1184 print_pte((l1 << 22) + (l2 << 12),
1185 pte & ~PG_PSE_MASK,
1186 ~0xfff);
1194 static void mem_print(uint32_t *pstart, int *plast_prot,
1195 uint32_t end, int prot)
1197 int prot1;
1198 prot1 = *plast_prot;
1199 if (prot != prot1) {
1200 if (*pstart != -1) {
1201 term_printf("%08x-%08x %08x %c%c%c\n",
1202 *pstart, end, end - *pstart,
1203 prot1 & PG_USER_MASK ? 'u' : '-',
1204 'r',
1205 prot1 & PG_RW_MASK ? 'w' : '-');
1207 if (prot != 0)
1208 *pstart = end;
1209 else
1210 *pstart = -1;
1211 *plast_prot = prot;
1215 static void mem_info(void)
1217 CPUState *env;
1218 int l1, l2, prot, last_prot;
1219 uint32_t pgd, pde, pte, start, end;
1221 env = mon_get_cpu();
1222 if (!env)
1223 return;
1225 if (!(env->cr[0] & CR0_PG_MASK)) {
1226 term_printf("PG disabled\n");
1227 return;
1229 pgd = env->cr[3] & ~0xfff;
1230 last_prot = 0;
1231 start = -1;
1232 for(l1 = 0; l1 < 1024; l1++) {
1233 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1234 pde = le32_to_cpu(pde);
1235 end = l1 << 22;
1236 if (pde & PG_PRESENT_MASK) {
1237 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1238 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1239 mem_print(&start, &last_prot, end, prot);
1240 } else {
1241 for(l2 = 0; l2 < 1024; l2++) {
1242 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1243 (uint8_t *)&pte, 4);
1244 pte = le32_to_cpu(pte);
1245 end = (l1 << 22) + (l2 << 12);
1246 if (pte & PG_PRESENT_MASK) {
1247 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1248 } else {
1249 prot = 0;
1251 mem_print(&start, &last_prot, end, prot);
1254 } else {
1255 prot = 0;
1256 mem_print(&start, &last_prot, end, prot);
1260 #endif
1262 static void do_info_kqemu(void)
1264 #ifdef USE_KQEMU
1265 CPUState *env;
1266 int val;
1267 val = 0;
1268 env = mon_get_cpu();
1269 if (!env) {
1270 term_printf("No cpu initialized yet");
1271 return;
1273 val = env->kqemu_enabled;
1274 term_printf("kqemu support: ");
1275 switch(val) {
1276 default:
1277 case 0:
1278 term_printf("disabled\n");
1279 break;
1280 case 1:
1281 term_printf("enabled for user code\n");
1282 break;
1283 case 2:
1284 term_printf("enabled for user and kernel code\n");
1285 break;
1287 #else
1288 term_printf("kqemu support: not compiled\n");
1289 #endif
1292 static void do_info_kvm(void)
1294 #ifdef USE_KVM
1295 term_printf("kvm support: ");
1296 if (kvm_enabled())
1297 term_printf("enabled\n");
1298 else
1299 term_printf("disabled\n");
1300 #else
1301 term_printf("kvm support: not compiled\n");
1302 #endif
1305 #ifdef CONFIG_PROFILER
1307 int64_t kqemu_time;
1308 int64_t qemu_time;
1309 int64_t kqemu_exec_count;
1310 int64_t dev_time;
1311 int64_t kqemu_ret_int_count;
1312 int64_t kqemu_ret_excp_count;
1313 int64_t kqemu_ret_intr_count;
1315 static void do_info_profile(void)
1317 int64_t total;
1318 total = qemu_time;
1319 if (total == 0)
1320 total = 1;
1321 term_printf("async time %" PRId64 " (%0.3f)\n",
1322 dev_time, dev_time / (double)ticks_per_sec);
1323 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1324 qemu_time, qemu_time / (double)ticks_per_sec);
1325 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1326 kqemu_time, kqemu_time / (double)ticks_per_sec,
1327 kqemu_time / (double)total * 100.0,
1328 kqemu_exec_count,
1329 kqemu_ret_int_count,
1330 kqemu_ret_excp_count,
1331 kqemu_ret_intr_count);
1332 qemu_time = 0;
1333 kqemu_time = 0;
1334 kqemu_exec_count = 0;
1335 dev_time = 0;
1336 kqemu_ret_int_count = 0;
1337 kqemu_ret_excp_count = 0;
1338 kqemu_ret_intr_count = 0;
1339 #ifdef USE_KQEMU
1340 kqemu_record_dump();
1341 #endif
1343 #else
1344 static void do_info_profile(void)
1346 term_printf("Internal profiler not compiled\n");
1348 #endif
1350 /* Capture support */
1351 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1353 static void do_info_capture (void)
1355 int i;
1356 CaptureState *s;
1358 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1359 term_printf ("[%d]: ", i);
1360 s->ops.info (s->opaque);
1364 static void do_stop_capture (int n)
1366 int i;
1367 CaptureState *s;
1369 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1370 if (i == n) {
1371 s->ops.destroy (s->opaque);
1372 LIST_REMOVE (s, entries);
1373 qemu_free (s);
1374 return;
1379 #ifdef HAS_AUDIO
1380 static void do_wav_capture (const char *path,
1381 int has_freq, int freq,
1382 int has_bits, int bits,
1383 int has_channels, int nchannels)
1385 CaptureState *s;
1387 s = qemu_mallocz (sizeof (*s));
1388 if (!s) {
1389 term_printf ("Not enough memory to add wave capture\n");
1390 return;
1393 freq = has_freq ? freq : 44100;
1394 bits = has_bits ? bits : 16;
1395 nchannels = has_channels ? nchannels : 2;
1397 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1398 term_printf ("Faied to add wave capture\n");
1399 qemu_free (s);
1401 LIST_INSERT_HEAD (&capture_head, s, entries);
1403 #endif
1405 #if defined(TARGET_I386)
1406 static void do_inject_nmi(int cpu_index)
1408 CPUState *env;
1410 for (env = first_cpu; env != NULL; env = env->next_cpu)
1411 if (env->cpu_index == cpu_index) {
1412 if (kvm_enabled())
1413 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1414 else
1415 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1416 break;
1419 #endif
1421 static void do_balloon(int value)
1423 ram_addr_t target = value;
1424 qemu_balloon(target << 20);
1427 static void do_info_balloon(void)
1429 ram_addr_t actual;
1431 actual = qemu_balloon_status();
1432 if (kvm_enabled() && !qemu_kvm_has_sync_mmu())
1433 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1434 else if (actual == 0)
1435 term_printf("Ballooning not activated in VM\n");
1436 else
1437 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1440 static const term_cmd_t term_cmds[] = {
1441 { "help|?", "s?", do_help,
1442 "[cmd]", "show the help" },
1443 { "commit", "s", do_commit,
1444 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1445 { "info", "s?", do_info,
1446 "subcommand", "show various information about the system state" },
1447 { "q|quit", "", do_quit,
1448 "", "quit the emulator" },
1449 { "eject", "-fB", do_eject,
1450 "[-f] device", "eject a removable medium (use -f to force it)" },
1451 { "change", "BFs?", do_change,
1452 "device filename [format]", "change a removable medium, optional format" },
1453 { "screendump", "F", do_screen_dump,
1454 "filename", "save screen into PPM image 'filename'" },
1455 { "logfile", "F", do_logfile,
1456 "filename", "output logs to 'filename'" },
1457 { "log", "s", do_log,
1458 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1459 { "savevm", "s?", do_savevm,
1460 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1461 { "loadvm", "s", do_loadvm,
1462 "tag|id", "restore a VM snapshot from its tag or id" },
1463 { "delvm", "s", do_delvm,
1464 "tag|id", "delete a VM snapshot from its tag or id" },
1465 { "stop", "", do_stop,
1466 "", "stop emulation", },
1467 { "c|cont", "", do_cont,
1468 "", "resume emulation", },
1469 #ifdef CONFIG_GDBSTUB
1470 { "gdbserver", "s?", do_gdbserver,
1471 "[port]", "start gdbserver session (default port=1234)", },
1472 #endif
1473 { "x", "/l", do_memory_dump,
1474 "/fmt addr", "virtual memory dump starting at 'addr'", },
1475 { "xp", "/l", do_physical_memory_dump,
1476 "/fmt addr", "physical memory dump starting at 'addr'", },
1477 { "p|print", "/l", do_print,
1478 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1479 { "i", "/ii.", do_ioport_read,
1480 "/fmt addr", "I/O port read" },
1482 { "sendkey", "si?", do_sendkey,
1483 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1484 { "system_reset", "", do_system_reset,
1485 "", "reset the system" },
1486 { "system_powerdown", "", do_system_powerdown,
1487 "", "send system power down event" },
1488 { "sum", "ii", do_sum,
1489 "addr size", "compute the checksum of a memory region" },
1490 { "usb_add", "s", do_usb_add,
1491 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1492 { "usb_del", "s", do_usb_del,
1493 "device", "remove USB device 'bus.addr'" },
1494 { "cpu", "i", do_cpu_set,
1495 "index", "set the default CPU" },
1496 { "mouse_move", "sss?", do_mouse_move,
1497 "dx dy [dz]", "send mouse move events" },
1498 { "mouse_button", "i", do_mouse_button,
1499 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1500 { "mouse_set", "i", do_mouse_set,
1501 "index", "set which mouse device receives events" },
1502 #ifdef HAS_AUDIO
1503 { "wavcapture", "si?i?i?", do_wav_capture,
1504 "path [frequency bits channels]",
1505 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1506 #endif
1507 { "stopcapture", "i", do_stop_capture,
1508 "capture index", "stop capture" },
1509 { "memsave", "lis", do_memory_save,
1510 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1511 { "pmemsave", "lis", do_physical_memory_save,
1512 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1513 { "boot_set", "s", do_boot_set,
1514 "bootdevice", "define new values for the boot device list" },
1515 #if defined(TARGET_I386)
1516 { "nmi", "i", do_inject_nmi,
1517 "cpu", "inject an NMI on the given CPU", },
1518 #endif
1519 { "migrate", "-ds", do_migrate,
1520 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1521 { "migrate_cancel", "", do_migrate_cancel,
1522 "", "cancel the current VM migration" },
1523 { "migrate_set_speed", "s", do_migrate_set_speed,
1524 "value", "set maximum speed (in bytes) for migrations" },
1525 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1526 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1527 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1528 "[,unit=m][,media=d][index=i]\n"
1529 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1530 "[snapshot=on|off][,cache=on|off]",
1531 "add drive to PCI storage controller" },
1532 { "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" },
1533 { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" },
1534 #endif
1535 { "balloon", "i", do_balloon,
1536 "target", "request VM to change it's memory allocation (in MB)" },
1537 { NULL, NULL, },
1540 static const term_cmd_t info_cmds[] = {
1541 { "version", "", do_info_version,
1542 "", "show the version of qemu" },
1543 { "network", "", do_info_network,
1544 "", "show the network state" },
1545 { "block", "", do_info_block,
1546 "", "show the block devices" },
1547 { "blockstats", "", do_info_blockstats,
1548 "", "show block device statistics" },
1549 { "registers", "", do_info_registers,
1550 "", "show the cpu registers" },
1551 { "cpus", "", do_info_cpus,
1552 "", "show infos for each CPU" },
1553 { "history", "", do_info_history,
1554 "", "show the command line history", },
1555 { "irq", "", irq_info,
1556 "", "show the interrupts statistics (if available)", },
1557 { "pic", "", pic_info,
1558 "", "show i8259 (PIC) state", },
1559 { "pci", "", pci_info,
1560 "", "show PCI info", },
1561 #if defined(TARGET_I386)
1562 { "tlb", "", tlb_info,
1563 "", "show virtual to physical memory mappings", },
1564 { "mem", "", mem_info,
1565 "", "show the active virtual memory mappings", },
1566 #endif
1567 { "jit", "", do_info_jit,
1568 "", "show dynamic compiler info", },
1569 { "kqemu", "", do_info_kqemu,
1570 "", "show kqemu information", },
1571 { "kvm", "", do_info_kvm,
1572 "", "show kvm information", },
1573 { "usb", "", usb_info,
1574 "", "show guest USB devices", },
1575 { "usbhost", "", usb_host_info,
1576 "", "show host USB devices", },
1577 { "profile", "", do_info_profile,
1578 "", "show profiling information", },
1579 { "capture", "", do_info_capture,
1580 "", "show capture information" },
1581 { "snapshots", "", do_info_snapshots,
1582 "", "show the currently saved VM snapshots" },
1583 { "pcmcia", "", pcmcia_info,
1584 "", "show guest PCMCIA status" },
1585 { "mice", "", do_info_mice,
1586 "", "show which guest mouse is receiving events" },
1587 { "vnc", "", do_info_vnc,
1588 "", "show the vnc server status"},
1589 { "name", "", do_info_name,
1590 "", "show the current VM name" },
1591 { "uuid", "", do_info_uuid,
1592 "", "show the current VM UUID" },
1593 #if defined(TARGET_PPC)
1594 { "cpustats", "", do_info_cpu_stats,
1595 "", "show CPU statistics", },
1596 #endif
1597 #if defined(CONFIG_SLIRP)
1598 { "slirp", "", do_info_slirp,
1599 "", "show SLIRP statistics", },
1600 #endif
1601 { "migrate", "", do_info_migrate, "", "show migration status" },
1602 { "balloon", "", do_info_balloon,
1603 "", "show balloon information" },
1604 { NULL, NULL, },
1607 /*******************************************************************/
1609 static const char *pch;
1610 static jmp_buf expr_env;
1612 #define MD_TLONG 0
1613 #define MD_I32 1
1615 typedef struct MonitorDef {
1616 const char *name;
1617 int offset;
1618 target_long (*get_value)(const struct MonitorDef *md, int val);
1619 int type;
1620 } MonitorDef;
1622 #if defined(TARGET_I386)
1623 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1625 CPUState *env = mon_get_cpu();
1626 if (!env)
1627 return 0;
1628 return env->eip + env->segs[R_CS].base;
1630 #endif
1632 #if defined(TARGET_PPC)
1633 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1635 CPUState *env = mon_get_cpu();
1636 unsigned int u;
1637 int i;
1639 if (!env)
1640 return 0;
1642 u = 0;
1643 for (i = 0; i < 8; i++)
1644 u |= env->crf[i] << (32 - (4 * i));
1646 return u;
1649 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1651 CPUState *env = mon_get_cpu();
1652 if (!env)
1653 return 0;
1654 return env->msr;
1657 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1659 CPUState *env = mon_get_cpu();
1660 if (!env)
1661 return 0;
1662 return ppc_load_xer(env);
1665 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1667 CPUState *env = mon_get_cpu();
1668 if (!env)
1669 return 0;
1670 return cpu_ppc_load_decr(env);
1673 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1675 CPUState *env = mon_get_cpu();
1676 if (!env)
1677 return 0;
1678 return cpu_ppc_load_tbu(env);
1681 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1683 CPUState *env = mon_get_cpu();
1684 if (!env)
1685 return 0;
1686 return cpu_ppc_load_tbl(env);
1688 #endif
1690 #if defined(TARGET_SPARC)
1691 #ifndef TARGET_SPARC64
1692 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1694 CPUState *env = mon_get_cpu();
1695 if (!env)
1696 return 0;
1697 return GET_PSR(env);
1699 #endif
1701 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1703 CPUState *env = mon_get_cpu();
1704 if (!env)
1705 return 0;
1706 return env->regwptr[val];
1708 #endif
1710 static const MonitorDef monitor_defs[] = {
1711 #ifdef TARGET_I386
1713 #define SEG(name, seg) \
1714 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1715 { name ".base", offsetof(CPUState, segs[seg].base) },\
1716 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1718 { "eax", offsetof(CPUState, regs[0]) },
1719 { "ecx", offsetof(CPUState, regs[1]) },
1720 { "edx", offsetof(CPUState, regs[2]) },
1721 { "ebx", offsetof(CPUState, regs[3]) },
1722 { "esp|sp", offsetof(CPUState, regs[4]) },
1723 { "ebp|fp", offsetof(CPUState, regs[5]) },
1724 { "esi", offsetof(CPUState, regs[6]) },
1725 { "edi", offsetof(CPUState, regs[7]) },
1726 #ifdef TARGET_X86_64
1727 { "r8", offsetof(CPUState, regs[8]) },
1728 { "r9", offsetof(CPUState, regs[9]) },
1729 { "r10", offsetof(CPUState, regs[10]) },
1730 { "r11", offsetof(CPUState, regs[11]) },
1731 { "r12", offsetof(CPUState, regs[12]) },
1732 { "r13", offsetof(CPUState, regs[13]) },
1733 { "r14", offsetof(CPUState, regs[14]) },
1734 { "r15", offsetof(CPUState, regs[15]) },
1735 #endif
1736 { "eflags", offsetof(CPUState, eflags) },
1737 { "eip", offsetof(CPUState, eip) },
1738 SEG("cs", R_CS)
1739 SEG("ds", R_DS)
1740 SEG("es", R_ES)
1741 SEG("ss", R_SS)
1742 SEG("fs", R_FS)
1743 SEG("gs", R_GS)
1744 { "pc", 0, monitor_get_pc, },
1745 #elif defined(TARGET_PPC)
1746 /* General purpose registers */
1747 { "r0", offsetof(CPUState, gpr[0]) },
1748 { "r1", offsetof(CPUState, gpr[1]) },
1749 { "r2", offsetof(CPUState, gpr[2]) },
1750 { "r3", offsetof(CPUState, gpr[3]) },
1751 { "r4", offsetof(CPUState, gpr[4]) },
1752 { "r5", offsetof(CPUState, gpr[5]) },
1753 { "r6", offsetof(CPUState, gpr[6]) },
1754 { "r7", offsetof(CPUState, gpr[7]) },
1755 { "r8", offsetof(CPUState, gpr[8]) },
1756 { "r9", offsetof(CPUState, gpr[9]) },
1757 { "r10", offsetof(CPUState, gpr[10]) },
1758 { "r11", offsetof(CPUState, gpr[11]) },
1759 { "r12", offsetof(CPUState, gpr[12]) },
1760 { "r13", offsetof(CPUState, gpr[13]) },
1761 { "r14", offsetof(CPUState, gpr[14]) },
1762 { "r15", offsetof(CPUState, gpr[15]) },
1763 { "r16", offsetof(CPUState, gpr[16]) },
1764 { "r17", offsetof(CPUState, gpr[17]) },
1765 { "r18", offsetof(CPUState, gpr[18]) },
1766 { "r19", offsetof(CPUState, gpr[19]) },
1767 { "r20", offsetof(CPUState, gpr[20]) },
1768 { "r21", offsetof(CPUState, gpr[21]) },
1769 { "r22", offsetof(CPUState, gpr[22]) },
1770 { "r23", offsetof(CPUState, gpr[23]) },
1771 { "r24", offsetof(CPUState, gpr[24]) },
1772 { "r25", offsetof(CPUState, gpr[25]) },
1773 { "r26", offsetof(CPUState, gpr[26]) },
1774 { "r27", offsetof(CPUState, gpr[27]) },
1775 { "r28", offsetof(CPUState, gpr[28]) },
1776 { "r29", offsetof(CPUState, gpr[29]) },
1777 { "r30", offsetof(CPUState, gpr[30]) },
1778 { "r31", offsetof(CPUState, gpr[31]) },
1779 /* Floating point registers */
1780 { "f0", offsetof(CPUState, fpr[0]) },
1781 { "f1", offsetof(CPUState, fpr[1]) },
1782 { "f2", offsetof(CPUState, fpr[2]) },
1783 { "f3", offsetof(CPUState, fpr[3]) },
1784 { "f4", offsetof(CPUState, fpr[4]) },
1785 { "f5", offsetof(CPUState, fpr[5]) },
1786 { "f6", offsetof(CPUState, fpr[6]) },
1787 { "f7", offsetof(CPUState, fpr[7]) },
1788 { "f8", offsetof(CPUState, fpr[8]) },
1789 { "f9", offsetof(CPUState, fpr[9]) },
1790 { "f10", offsetof(CPUState, fpr[10]) },
1791 { "f11", offsetof(CPUState, fpr[11]) },
1792 { "f12", offsetof(CPUState, fpr[12]) },
1793 { "f13", offsetof(CPUState, fpr[13]) },
1794 { "f14", offsetof(CPUState, fpr[14]) },
1795 { "f15", offsetof(CPUState, fpr[15]) },
1796 { "f16", offsetof(CPUState, fpr[16]) },
1797 { "f17", offsetof(CPUState, fpr[17]) },
1798 { "f18", offsetof(CPUState, fpr[18]) },
1799 { "f19", offsetof(CPUState, fpr[19]) },
1800 { "f20", offsetof(CPUState, fpr[20]) },
1801 { "f21", offsetof(CPUState, fpr[21]) },
1802 { "f22", offsetof(CPUState, fpr[22]) },
1803 { "f23", offsetof(CPUState, fpr[23]) },
1804 { "f24", offsetof(CPUState, fpr[24]) },
1805 { "f25", offsetof(CPUState, fpr[25]) },
1806 { "f26", offsetof(CPUState, fpr[26]) },
1807 { "f27", offsetof(CPUState, fpr[27]) },
1808 { "f28", offsetof(CPUState, fpr[28]) },
1809 { "f29", offsetof(CPUState, fpr[29]) },
1810 { "f30", offsetof(CPUState, fpr[30]) },
1811 { "f31", offsetof(CPUState, fpr[31]) },
1812 { "fpscr", offsetof(CPUState, fpscr) },
1813 /* Next instruction pointer */
1814 { "nip|pc", offsetof(CPUState, nip) },
1815 { "lr", offsetof(CPUState, lr) },
1816 { "ctr", offsetof(CPUState, ctr) },
1817 { "decr", 0, &monitor_get_decr, },
1818 { "ccr", 0, &monitor_get_ccr, },
1819 /* Machine state register */
1820 { "msr", 0, &monitor_get_msr, },
1821 { "xer", 0, &monitor_get_xer, },
1822 { "tbu", 0, &monitor_get_tbu, },
1823 { "tbl", 0, &monitor_get_tbl, },
1824 #if defined(TARGET_PPC64)
1825 /* Address space register */
1826 { "asr", offsetof(CPUState, asr) },
1827 #endif
1828 /* Segment registers */
1829 { "sdr1", offsetof(CPUState, sdr1) },
1830 { "sr0", offsetof(CPUState, sr[0]) },
1831 { "sr1", offsetof(CPUState, sr[1]) },
1832 { "sr2", offsetof(CPUState, sr[2]) },
1833 { "sr3", offsetof(CPUState, sr[3]) },
1834 { "sr4", offsetof(CPUState, sr[4]) },
1835 { "sr5", offsetof(CPUState, sr[5]) },
1836 { "sr6", offsetof(CPUState, sr[6]) },
1837 { "sr7", offsetof(CPUState, sr[7]) },
1838 { "sr8", offsetof(CPUState, sr[8]) },
1839 { "sr9", offsetof(CPUState, sr[9]) },
1840 { "sr10", offsetof(CPUState, sr[10]) },
1841 { "sr11", offsetof(CPUState, sr[11]) },
1842 { "sr12", offsetof(CPUState, sr[12]) },
1843 { "sr13", offsetof(CPUState, sr[13]) },
1844 { "sr14", offsetof(CPUState, sr[14]) },
1845 { "sr15", offsetof(CPUState, sr[15]) },
1846 /* Too lazy to put BATs and SPRs ... */
1847 #elif defined(TARGET_SPARC)
1848 { "g0", offsetof(CPUState, gregs[0]) },
1849 { "g1", offsetof(CPUState, gregs[1]) },
1850 { "g2", offsetof(CPUState, gregs[2]) },
1851 { "g3", offsetof(CPUState, gregs[3]) },
1852 { "g4", offsetof(CPUState, gregs[4]) },
1853 { "g5", offsetof(CPUState, gregs[5]) },
1854 { "g6", offsetof(CPUState, gregs[6]) },
1855 { "g7", offsetof(CPUState, gregs[7]) },
1856 { "o0", 0, monitor_get_reg },
1857 { "o1", 1, monitor_get_reg },
1858 { "o2", 2, monitor_get_reg },
1859 { "o3", 3, monitor_get_reg },
1860 { "o4", 4, monitor_get_reg },
1861 { "o5", 5, monitor_get_reg },
1862 { "o6", 6, monitor_get_reg },
1863 { "o7", 7, monitor_get_reg },
1864 { "l0", 8, monitor_get_reg },
1865 { "l1", 9, monitor_get_reg },
1866 { "l2", 10, monitor_get_reg },
1867 { "l3", 11, monitor_get_reg },
1868 { "l4", 12, monitor_get_reg },
1869 { "l5", 13, monitor_get_reg },
1870 { "l6", 14, monitor_get_reg },
1871 { "l7", 15, monitor_get_reg },
1872 { "i0", 16, monitor_get_reg },
1873 { "i1", 17, monitor_get_reg },
1874 { "i2", 18, monitor_get_reg },
1875 { "i3", 19, monitor_get_reg },
1876 { "i4", 20, monitor_get_reg },
1877 { "i5", 21, monitor_get_reg },
1878 { "i6", 22, monitor_get_reg },
1879 { "i7", 23, monitor_get_reg },
1880 { "pc", offsetof(CPUState, pc) },
1881 { "npc", offsetof(CPUState, npc) },
1882 { "y", offsetof(CPUState, y) },
1883 #ifndef TARGET_SPARC64
1884 { "psr", 0, &monitor_get_psr, },
1885 { "wim", offsetof(CPUState, wim) },
1886 #endif
1887 { "tbr", offsetof(CPUState, tbr) },
1888 { "fsr", offsetof(CPUState, fsr) },
1889 { "f0", offsetof(CPUState, fpr[0]) },
1890 { "f1", offsetof(CPUState, fpr[1]) },
1891 { "f2", offsetof(CPUState, fpr[2]) },
1892 { "f3", offsetof(CPUState, fpr[3]) },
1893 { "f4", offsetof(CPUState, fpr[4]) },
1894 { "f5", offsetof(CPUState, fpr[5]) },
1895 { "f6", offsetof(CPUState, fpr[6]) },
1896 { "f7", offsetof(CPUState, fpr[7]) },
1897 { "f8", offsetof(CPUState, fpr[8]) },
1898 { "f9", offsetof(CPUState, fpr[9]) },
1899 { "f10", offsetof(CPUState, fpr[10]) },
1900 { "f11", offsetof(CPUState, fpr[11]) },
1901 { "f12", offsetof(CPUState, fpr[12]) },
1902 { "f13", offsetof(CPUState, fpr[13]) },
1903 { "f14", offsetof(CPUState, fpr[14]) },
1904 { "f15", offsetof(CPUState, fpr[15]) },
1905 { "f16", offsetof(CPUState, fpr[16]) },
1906 { "f17", offsetof(CPUState, fpr[17]) },
1907 { "f18", offsetof(CPUState, fpr[18]) },
1908 { "f19", offsetof(CPUState, fpr[19]) },
1909 { "f20", offsetof(CPUState, fpr[20]) },
1910 { "f21", offsetof(CPUState, fpr[21]) },
1911 { "f22", offsetof(CPUState, fpr[22]) },
1912 { "f23", offsetof(CPUState, fpr[23]) },
1913 { "f24", offsetof(CPUState, fpr[24]) },
1914 { "f25", offsetof(CPUState, fpr[25]) },
1915 { "f26", offsetof(CPUState, fpr[26]) },
1916 { "f27", offsetof(CPUState, fpr[27]) },
1917 { "f28", offsetof(CPUState, fpr[28]) },
1918 { "f29", offsetof(CPUState, fpr[29]) },
1919 { "f30", offsetof(CPUState, fpr[30]) },
1920 { "f31", offsetof(CPUState, fpr[31]) },
1921 #ifdef TARGET_SPARC64
1922 { "f32", offsetof(CPUState, fpr[32]) },
1923 { "f34", offsetof(CPUState, fpr[34]) },
1924 { "f36", offsetof(CPUState, fpr[36]) },
1925 { "f38", offsetof(CPUState, fpr[38]) },
1926 { "f40", offsetof(CPUState, fpr[40]) },
1927 { "f42", offsetof(CPUState, fpr[42]) },
1928 { "f44", offsetof(CPUState, fpr[44]) },
1929 { "f46", offsetof(CPUState, fpr[46]) },
1930 { "f48", offsetof(CPUState, fpr[48]) },
1931 { "f50", offsetof(CPUState, fpr[50]) },
1932 { "f52", offsetof(CPUState, fpr[52]) },
1933 { "f54", offsetof(CPUState, fpr[54]) },
1934 { "f56", offsetof(CPUState, fpr[56]) },
1935 { "f58", offsetof(CPUState, fpr[58]) },
1936 { "f60", offsetof(CPUState, fpr[60]) },
1937 { "f62", offsetof(CPUState, fpr[62]) },
1938 { "asi", offsetof(CPUState, asi) },
1939 { "pstate", offsetof(CPUState, pstate) },
1940 { "cansave", offsetof(CPUState, cansave) },
1941 { "canrestore", offsetof(CPUState, canrestore) },
1942 { "otherwin", offsetof(CPUState, otherwin) },
1943 { "wstate", offsetof(CPUState, wstate) },
1944 { "cleanwin", offsetof(CPUState, cleanwin) },
1945 { "fprs", offsetof(CPUState, fprs) },
1946 #endif
1947 #endif
1948 { NULL },
1951 static void expr_error(const char *fmt)
1953 term_printf(fmt);
1954 term_printf("\n");
1955 longjmp(expr_env, 1);
1958 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1959 static int get_monitor_def(target_long *pval, const char *name)
1961 const MonitorDef *md;
1962 void *ptr;
1964 for(md = monitor_defs; md->name != NULL; md++) {
1965 if (compare_cmd(name, md->name)) {
1966 if (md->get_value) {
1967 *pval = md->get_value(md, md->offset);
1968 } else {
1969 CPUState *env = mon_get_cpu();
1970 if (!env)
1971 return -2;
1972 ptr = (uint8_t *)env + md->offset;
1973 switch(md->type) {
1974 case MD_I32:
1975 *pval = *(int32_t *)ptr;
1976 break;
1977 case MD_TLONG:
1978 *pval = *(target_long *)ptr;
1979 break;
1980 default:
1981 *pval = 0;
1982 break;
1985 return 0;
1988 return -1;
1991 static void next(void)
1993 if (pch != '\0') {
1994 pch++;
1995 while (isspace(*pch))
1996 pch++;
2000 static int64_t expr_sum(void);
2002 static int64_t expr_unary(void)
2004 int64_t n;
2005 char *p;
2006 int ret;
2008 switch(*pch) {
2009 case '+':
2010 next();
2011 n = expr_unary();
2012 break;
2013 case '-':
2014 next();
2015 n = -expr_unary();
2016 break;
2017 case '~':
2018 next();
2019 n = ~expr_unary();
2020 break;
2021 case '(':
2022 next();
2023 n = expr_sum();
2024 if (*pch != ')') {
2025 expr_error("')' expected");
2027 next();
2028 break;
2029 case '\'':
2030 pch++;
2031 if (*pch == '\0')
2032 expr_error("character constant expected");
2033 n = *pch;
2034 pch++;
2035 if (*pch != '\'')
2036 expr_error("missing terminating \' character");
2037 next();
2038 break;
2039 case '$':
2041 char buf[128], *q;
2042 target_long reg=0;
2044 pch++;
2045 q = buf;
2046 while ((*pch >= 'a' && *pch <= 'z') ||
2047 (*pch >= 'A' && *pch <= 'Z') ||
2048 (*pch >= '0' && *pch <= '9') ||
2049 *pch == '_' || *pch == '.') {
2050 if ((q - buf) < sizeof(buf) - 1)
2051 *q++ = *pch;
2052 pch++;
2054 while (isspace(*pch))
2055 pch++;
2056 *q = 0;
2057 ret = get_monitor_def(&reg, buf);
2058 if (ret == -1)
2059 expr_error("unknown register");
2060 else if (ret == -2)
2061 expr_error("no cpu defined");
2062 n = reg;
2064 break;
2065 case '\0':
2066 expr_error("unexpected end of expression");
2067 n = 0;
2068 break;
2069 default:
2070 #if TARGET_PHYS_ADDR_BITS > 32
2071 n = strtoull(pch, &p, 0);
2072 #else
2073 n = strtoul(pch, &p, 0);
2074 #endif
2075 if (pch == p) {
2076 expr_error("invalid char in expression");
2078 pch = p;
2079 while (isspace(*pch))
2080 pch++;
2081 break;
2083 return n;
2087 static int64_t expr_prod(void)
2089 int64_t val, val2;
2090 int op;
2092 val = expr_unary();
2093 for(;;) {
2094 op = *pch;
2095 if (op != '*' && op != '/' && op != '%')
2096 break;
2097 next();
2098 val2 = expr_unary();
2099 switch(op) {
2100 default:
2101 case '*':
2102 val *= val2;
2103 break;
2104 case '/':
2105 case '%':
2106 if (val2 == 0)
2107 expr_error("division by zero");
2108 if (op == '/')
2109 val /= val2;
2110 else
2111 val %= val2;
2112 break;
2115 return val;
2118 static int64_t expr_logic(void)
2120 int64_t val, val2;
2121 int op;
2123 val = expr_prod();
2124 for(;;) {
2125 op = *pch;
2126 if (op != '&' && op != '|' && op != '^')
2127 break;
2128 next();
2129 val2 = expr_prod();
2130 switch(op) {
2131 default:
2132 case '&':
2133 val &= val2;
2134 break;
2135 case '|':
2136 val |= val2;
2137 break;
2138 case '^':
2139 val ^= val2;
2140 break;
2143 return val;
2146 static int64_t expr_sum(void)
2148 int64_t val, val2;
2149 int op;
2151 val = expr_logic();
2152 for(;;) {
2153 op = *pch;
2154 if (op != '+' && op != '-')
2155 break;
2156 next();
2157 val2 = expr_logic();
2158 if (op == '+')
2159 val += val2;
2160 else
2161 val -= val2;
2163 return val;
2166 static int get_expr(int64_t *pval, const char **pp)
2168 pch = *pp;
2169 if (setjmp(expr_env)) {
2170 *pp = pch;
2171 return -1;
2173 while (isspace(*pch))
2174 pch++;
2175 *pval = expr_sum();
2176 *pp = pch;
2177 return 0;
2180 static int get_str(char *buf, int buf_size, const char **pp)
2182 const char *p;
2183 char *q;
2184 int c;
2186 q = buf;
2187 p = *pp;
2188 while (isspace(*p))
2189 p++;
2190 if (*p == '\0') {
2191 fail:
2192 *q = '\0';
2193 *pp = p;
2194 return -1;
2196 if (*p == '\"') {
2197 p++;
2198 while (*p != '\0' && *p != '\"') {
2199 if (*p == '\\') {
2200 p++;
2201 c = *p++;
2202 switch(c) {
2203 case 'n':
2204 c = '\n';
2205 break;
2206 case 'r':
2207 c = '\r';
2208 break;
2209 case '\\':
2210 case '\'':
2211 case '\"':
2212 break;
2213 default:
2214 qemu_printf("unsupported escape code: '\\%c'\n", c);
2215 goto fail;
2217 if ((q - buf) < buf_size - 1) {
2218 *q++ = c;
2220 } else {
2221 if ((q - buf) < buf_size - 1) {
2222 *q++ = *p;
2224 p++;
2227 if (*p != '\"') {
2228 qemu_printf("unterminated string\n");
2229 goto fail;
2231 p++;
2232 } else {
2233 while (*p != '\0' && !isspace(*p)) {
2234 if ((q - buf) < buf_size - 1) {
2235 *q++ = *p;
2237 p++;
2240 *q = '\0';
2241 *pp = p;
2242 return 0;
2245 static int default_fmt_format = 'x';
2246 static int default_fmt_size = 4;
2248 #define MAX_ARGS 16
2250 static void monitor_handle_command(const char *cmdline)
2252 const char *p, *pstart, *typestr;
2253 char *q;
2254 int c, nb_args, len, i, has_arg;
2255 const term_cmd_t *cmd;
2256 char cmdname[256];
2257 char buf[1024];
2258 void *str_allocated[MAX_ARGS];
2259 void *args[MAX_ARGS];
2260 void (*handler_0)(void);
2261 void (*handler_1)(void *arg0);
2262 void (*handler_2)(void *arg0, void *arg1);
2263 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2264 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2265 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2266 void *arg4);
2267 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2268 void *arg4, void *arg5);
2269 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2270 void *arg4, void *arg5, void *arg6);
2272 #ifdef DEBUG
2273 term_printf("command='%s'\n", cmdline);
2274 #endif
2276 /* extract the command name */
2277 p = cmdline;
2278 q = cmdname;
2279 while (isspace(*p))
2280 p++;
2281 if (*p == '\0')
2282 return;
2283 pstart = p;
2284 while (*p != '\0' && *p != '/' && !isspace(*p))
2285 p++;
2286 len = p - pstart;
2287 if (len > sizeof(cmdname) - 1)
2288 len = sizeof(cmdname) - 1;
2289 memcpy(cmdname, pstart, len);
2290 cmdname[len] = '\0';
2292 /* find the command */
2293 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2294 if (compare_cmd(cmdname, cmd->name))
2295 goto found;
2297 term_printf("unknown command: '%s'\n", cmdname);
2298 return;
2299 found:
2301 for(i = 0; i < MAX_ARGS; i++)
2302 str_allocated[i] = NULL;
2304 /* parse the parameters */
2305 typestr = cmd->args_type;
2306 nb_args = 0;
2307 for(;;) {
2308 c = *typestr;
2309 if (c == '\0')
2310 break;
2311 typestr++;
2312 switch(c) {
2313 case 'F':
2314 case 'B':
2315 case 's':
2317 int ret;
2318 char *str;
2320 while (isspace(*p))
2321 p++;
2322 if (*typestr == '?') {
2323 typestr++;
2324 if (*p == '\0') {
2325 /* no optional string: NULL argument */
2326 str = NULL;
2327 goto add_str;
2330 ret = get_str(buf, sizeof(buf), &p);
2331 if (ret < 0) {
2332 switch(c) {
2333 case 'F':
2334 term_printf("%s: filename expected\n", cmdname);
2335 break;
2336 case 'B':
2337 term_printf("%s: block device name expected\n", cmdname);
2338 break;
2339 default:
2340 term_printf("%s: string expected\n", cmdname);
2341 break;
2343 goto fail;
2345 str = qemu_malloc(strlen(buf) + 1);
2346 pstrcpy(str, sizeof(buf), buf);
2347 str_allocated[nb_args] = str;
2348 add_str:
2349 if (nb_args >= MAX_ARGS) {
2350 error_args:
2351 term_printf("%s: too many arguments\n", cmdname);
2352 goto fail;
2354 args[nb_args++] = str;
2356 break;
2357 case '/':
2359 int count, format, size;
2361 while (isspace(*p))
2362 p++;
2363 if (*p == '/') {
2364 /* format found */
2365 p++;
2366 count = 1;
2367 if (isdigit(*p)) {
2368 count = 0;
2369 while (isdigit(*p)) {
2370 count = count * 10 + (*p - '0');
2371 p++;
2374 size = -1;
2375 format = -1;
2376 for(;;) {
2377 switch(*p) {
2378 case 'o':
2379 case 'd':
2380 case 'u':
2381 case 'x':
2382 case 'i':
2383 case 'c':
2384 format = *p++;
2385 break;
2386 case 'b':
2387 size = 1;
2388 p++;
2389 break;
2390 case 'h':
2391 size = 2;
2392 p++;
2393 break;
2394 case 'w':
2395 size = 4;
2396 p++;
2397 break;
2398 case 'g':
2399 case 'L':
2400 size = 8;
2401 p++;
2402 break;
2403 default:
2404 goto next;
2407 next:
2408 if (*p != '\0' && !isspace(*p)) {
2409 term_printf("invalid char in format: '%c'\n", *p);
2410 goto fail;
2412 if (format < 0)
2413 format = default_fmt_format;
2414 if (format != 'i') {
2415 /* for 'i', not specifying a size gives -1 as size */
2416 if (size < 0)
2417 size = default_fmt_size;
2418 default_fmt_size = size;
2420 default_fmt_format = format;
2421 } else {
2422 count = 1;
2423 format = default_fmt_format;
2424 if (format != 'i') {
2425 size = default_fmt_size;
2426 } else {
2427 size = -1;
2430 if (nb_args + 3 > MAX_ARGS)
2431 goto error_args;
2432 args[nb_args++] = (void*)(long)count;
2433 args[nb_args++] = (void*)(long)format;
2434 args[nb_args++] = (void*)(long)size;
2436 break;
2437 case 'i':
2438 case 'l':
2440 int64_t val;
2442 while (isspace(*p))
2443 p++;
2444 if (*typestr == '?' || *typestr == '.') {
2445 if (*typestr == '?') {
2446 if (*p == '\0')
2447 has_arg = 0;
2448 else
2449 has_arg = 1;
2450 } else {
2451 if (*p == '.') {
2452 p++;
2453 while (isspace(*p))
2454 p++;
2455 has_arg = 1;
2456 } else {
2457 has_arg = 0;
2460 typestr++;
2461 if (nb_args >= MAX_ARGS)
2462 goto error_args;
2463 args[nb_args++] = (void *)(long)has_arg;
2464 if (!has_arg) {
2465 if (nb_args >= MAX_ARGS)
2466 goto error_args;
2467 val = -1;
2468 goto add_num;
2471 if (get_expr(&val, &p))
2472 goto fail;
2473 add_num:
2474 if (c == 'i') {
2475 if (nb_args >= MAX_ARGS)
2476 goto error_args;
2477 args[nb_args++] = (void *)(long)val;
2478 } else {
2479 if ((nb_args + 1) >= MAX_ARGS)
2480 goto error_args;
2481 #if TARGET_PHYS_ADDR_BITS > 32
2482 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2483 #else
2484 args[nb_args++] = (void *)0;
2485 #endif
2486 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2489 break;
2490 case '-':
2492 int has_option;
2493 /* option */
2495 c = *typestr++;
2496 if (c == '\0')
2497 goto bad_type;
2498 while (isspace(*p))
2499 p++;
2500 has_option = 0;
2501 if (*p == '-') {
2502 p++;
2503 if (*p != c) {
2504 term_printf("%s: unsupported option -%c\n",
2505 cmdname, *p);
2506 goto fail;
2508 p++;
2509 has_option = 1;
2511 if (nb_args >= MAX_ARGS)
2512 goto error_args;
2513 args[nb_args++] = (void *)(long)has_option;
2515 break;
2516 default:
2517 bad_type:
2518 term_printf("%s: unknown type '%c'\n", cmdname, c);
2519 goto fail;
2522 /* check that all arguments were parsed */
2523 while (isspace(*p))
2524 p++;
2525 if (*p != '\0') {
2526 term_printf("%s: extraneous characters at the end of line\n",
2527 cmdname);
2528 goto fail;
2531 switch(nb_args) {
2532 case 0:
2533 handler_0 = cmd->handler;
2534 handler_0();
2535 break;
2536 case 1:
2537 handler_1 = cmd->handler;
2538 handler_1(args[0]);
2539 break;
2540 case 2:
2541 handler_2 = cmd->handler;
2542 handler_2(args[0], args[1]);
2543 break;
2544 case 3:
2545 handler_3 = cmd->handler;
2546 handler_3(args[0], args[1], args[2]);
2547 break;
2548 case 4:
2549 handler_4 = cmd->handler;
2550 handler_4(args[0], args[1], args[2], args[3]);
2551 break;
2552 case 5:
2553 handler_5 = cmd->handler;
2554 handler_5(args[0], args[1], args[2], args[3], args[4]);
2555 break;
2556 case 6:
2557 handler_6 = cmd->handler;
2558 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2559 break;
2560 case 7:
2561 handler_7 = cmd->handler;
2562 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2563 break;
2564 default:
2565 term_printf("unsupported number of arguments: %d\n", nb_args);
2566 goto fail;
2568 fail:
2569 for(i = 0; i < MAX_ARGS; i++)
2570 qemu_free(str_allocated[i]);
2571 return;
2574 static void cmd_completion(const char *name, const char *list)
2576 const char *p, *pstart;
2577 char cmd[128];
2578 int len;
2580 p = list;
2581 for(;;) {
2582 pstart = p;
2583 p = strchr(p, '|');
2584 if (!p)
2585 p = pstart + strlen(pstart);
2586 len = p - pstart;
2587 if (len > sizeof(cmd) - 2)
2588 len = sizeof(cmd) - 2;
2589 memcpy(cmd, pstart, len);
2590 cmd[len] = '\0';
2591 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2592 add_completion(cmd);
2594 if (*p == '\0')
2595 break;
2596 p++;
2600 static void file_completion(const char *input)
2602 DIR *ffs;
2603 struct dirent *d;
2604 char path[1024];
2605 char file[1024], file_prefix[1024];
2606 int input_path_len;
2607 const char *p;
2609 p = strrchr(input, '/');
2610 if (!p) {
2611 input_path_len = 0;
2612 pstrcpy(file_prefix, sizeof(file_prefix), input);
2613 pstrcpy(path, sizeof(path), ".");
2614 } else {
2615 input_path_len = p - input + 1;
2616 memcpy(path, input, input_path_len);
2617 if (input_path_len > sizeof(path) - 1)
2618 input_path_len = sizeof(path) - 1;
2619 path[input_path_len] = '\0';
2620 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2622 #ifdef DEBUG_COMPLETION
2623 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2624 #endif
2625 ffs = opendir(path);
2626 if (!ffs)
2627 return;
2628 for(;;) {
2629 struct stat sb;
2630 d = readdir(ffs);
2631 if (!d)
2632 break;
2633 if (strstart(d->d_name, file_prefix, NULL)) {
2634 memcpy(file, input, input_path_len);
2635 if (input_path_len < sizeof(file))
2636 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2637 d->d_name);
2638 /* stat the file to find out if it's a directory.
2639 * In that case add a slash to speed up typing long paths
2641 stat(file, &sb);
2642 if(S_ISDIR(sb.st_mode))
2643 pstrcat(file, sizeof(file), "/");
2644 add_completion(file);
2647 closedir(ffs);
2650 static void block_completion_it(void *opaque, const char *name)
2652 const char *input = opaque;
2654 if (input[0] == '\0' ||
2655 !strncmp(name, (char *)input, strlen(input))) {
2656 add_completion(name);
2660 /* NOTE: this parser is an approximate form of the real command parser */
2661 static void parse_cmdline(const char *cmdline,
2662 int *pnb_args, char **args)
2664 const char *p;
2665 int nb_args, ret;
2666 char buf[1024];
2668 p = cmdline;
2669 nb_args = 0;
2670 for(;;) {
2671 while (isspace(*p))
2672 p++;
2673 if (*p == '\0')
2674 break;
2675 if (nb_args >= MAX_ARGS)
2676 break;
2677 ret = get_str(buf, sizeof(buf), &p);
2678 args[nb_args] = qemu_strdup(buf);
2679 nb_args++;
2680 if (ret < 0)
2681 break;
2683 *pnb_args = nb_args;
2686 void readline_find_completion(const char *cmdline)
2688 const char *cmdname;
2689 char *args[MAX_ARGS];
2690 int nb_args, i, len;
2691 const char *ptype, *str;
2692 const term_cmd_t *cmd;
2693 const KeyDef *key;
2695 parse_cmdline(cmdline, &nb_args, args);
2696 #ifdef DEBUG_COMPLETION
2697 for(i = 0; i < nb_args; i++) {
2698 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2700 #endif
2702 /* if the line ends with a space, it means we want to complete the
2703 next arg */
2704 len = strlen(cmdline);
2705 if (len > 0 && isspace(cmdline[len - 1])) {
2706 if (nb_args >= MAX_ARGS)
2707 return;
2708 args[nb_args++] = qemu_strdup("");
2710 if (nb_args <= 1) {
2711 /* command completion */
2712 if (nb_args == 0)
2713 cmdname = "";
2714 else
2715 cmdname = args[0];
2716 completion_index = strlen(cmdname);
2717 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2718 cmd_completion(cmdname, cmd->name);
2720 } else {
2721 /* find the command */
2722 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2723 if (compare_cmd(args[0], cmd->name))
2724 goto found;
2726 return;
2727 found:
2728 ptype = cmd->args_type;
2729 for(i = 0; i < nb_args - 2; i++) {
2730 if (*ptype != '\0') {
2731 ptype++;
2732 while (*ptype == '?')
2733 ptype++;
2736 str = args[nb_args - 1];
2737 switch(*ptype) {
2738 case 'F':
2739 /* file completion */
2740 completion_index = strlen(str);
2741 file_completion(str);
2742 break;
2743 case 'B':
2744 /* block device name completion */
2745 completion_index = strlen(str);
2746 bdrv_iterate(block_completion_it, (void *)str);
2747 break;
2748 case 's':
2749 /* XXX: more generic ? */
2750 if (!strcmp(cmd->name, "info")) {
2751 completion_index = strlen(str);
2752 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2753 cmd_completion(str, cmd->name);
2755 } else if (!strcmp(cmd->name, "sendkey")) {
2756 completion_index = strlen(str);
2757 for(key = key_defs; key->name != NULL; key++) {
2758 cmd_completion(str, key->name);
2761 break;
2762 default:
2763 break;
2766 for(i = 0; i < nb_args; i++)
2767 qemu_free(args[i]);
2770 static int term_can_read(void *opaque)
2772 return 128;
2775 static void term_read(void *opaque, const uint8_t *buf, int size)
2777 int i;
2778 for(i = 0; i < size; i++)
2779 readline_handle_byte(buf[i]);
2782 static int monitor_suspended;
2784 static void monitor_handle_command1(void *opaque, const char *cmdline)
2786 monitor_handle_command(cmdline);
2787 if (!monitor_suspended)
2788 monitor_start_input();
2789 else
2790 monitor_suspended = 2;
2793 void monitor_suspend(void)
2795 monitor_suspended = 1;
2798 void monitor_resume(void)
2800 if (monitor_suspended == 2)
2801 monitor_start_input();
2802 monitor_suspended = 0;
2805 static void monitor_start_input(void)
2807 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2810 static void term_event(void *opaque, int event)
2812 if (event != CHR_EVENT_RESET)
2813 return;
2815 if (!hide_banner)
2816 term_printf("QEMU %s monitor - type 'help' for more information\n",
2817 QEMU_VERSION);
2818 monitor_start_input();
2821 static int is_first_init = 1;
2823 void monitor_init(CharDriverState *hd, int show_banner)
2825 int i;
2827 if (is_first_init) {
2828 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2829 if (!key_timer)
2830 return;
2831 for (i = 0; i < MAX_MON; i++) {
2832 monitor_hd[i] = NULL;
2834 is_first_init = 0;
2836 for (i = 0; i < MAX_MON; i++) {
2837 if (monitor_hd[i] == NULL) {
2838 monitor_hd[i] = hd;
2839 break;
2843 hide_banner = !show_banner;
2845 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2847 readline_start("", 0, monitor_handle_command1, NULL);
2850 /* XXX: use threads ? */
2851 /* modal monitor readline */
2852 static int monitor_readline_started;
2853 static char *monitor_readline_buf;
2854 static int monitor_readline_buf_size;
2856 static void monitor_readline_cb(void *opaque, const char *input)
2858 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2859 monitor_readline_started = 0;
2862 void monitor_readline(const char *prompt, int is_password,
2863 char *buf, int buf_size)
2865 int i;
2866 int old_focus[MAX_MON];
2868 if (is_password) {
2869 for (i = 0; i < MAX_MON; i++) {
2870 old_focus[i] = 0;
2871 if (monitor_hd[i]) {
2872 old_focus[i] = monitor_hd[i]->focus;
2873 monitor_hd[i]->focus = 0;
2874 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2879 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2880 monitor_readline_buf = buf;
2881 monitor_readline_buf_size = buf_size;
2882 monitor_readline_started = 1;
2883 while (monitor_readline_started) {
2884 main_loop_wait(10);
2886 /* restore original focus */
2887 if (is_password) {
2888 for (i = 0; i < MAX_MON; i++)
2889 if (old_focus[i])
2890 monitor_hd[i]->focus = old_focus[i];