kvm: bios: use smp_cpus as lapic id
[qemu-kvm/amd-iommu.git] / monitor.c
blob8874e3ec7224f20e9d0041eb5441a7a24a2cee54
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw/hw.h"
25 #include "hw/usb.h"
26 #include "hw/pcmcia.h"
27 #include "hw/pc.h"
28 #include "hw/pci.h"
29 #include "gdbstub.h"
30 #include "net.h"
31 #include "qemu-char.h"
32 #include "sysemu.h"
33 #include "console.h"
34 #include "block.h"
35 #include "audio/audio.h"
36 #include "disas.h"
37 #include "migration.h"
38 #include <dirent.h>
40 #include "qemu-kvm.h"
41 #ifdef CONFIG_PROFILER
42 #include "qemu-timer.h" /* for ticks_per_sec */
43 #endif
45 //#define DEBUG
46 //#define DEBUG_COMPLETION
48 #ifndef offsetof
49 #define offsetof(type, field) ((size_t) &((type *)0)->field)
50 #endif
53 * Supported types:
55 * 'F' filename
56 * 'B' block device name
57 * 's' string (accept optional quote)
58 * 'i' 32 bit integer
59 * 'l' target long (32 or 64 bit)
60 * '/' optional gdb-like print format (like "/10x")
62 * '?' optional type (for 'F', 's' and 'i')
66 typedef struct term_cmd_t {
67 const char *name;
68 const char *args_type;
69 void (*handler)();
70 const char *params;
71 const char *help;
72 } term_cmd_t;
74 #define MAX_MON 4
75 static CharDriverState *monitor_hd[MAX_MON];
76 static int hide_banner;
78 static term_cmd_t term_cmds[];
79 static term_cmd_t info_cmds[];
81 static uint8_t term_outbuf[1024];
82 static int term_outbuf_index;
84 static void monitor_start_input(void);
86 CPUState *mon_cpu = NULL;
88 void term_flush(void)
90 int i;
91 if (term_outbuf_index > 0) {
92 for (i = 0; i < MAX_MON; i++)
93 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
94 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
95 term_outbuf_index = 0;
99 /* flush at every end of line or if the buffer is full */
100 void term_puts(const char *str)
102 char c;
103 for(;;) {
104 c = *str++;
105 if (c == '\0')
106 break;
107 if (c == '\n')
108 term_outbuf[term_outbuf_index++] = '\r';
109 term_outbuf[term_outbuf_index++] = c;
110 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
111 c == '\n')
112 term_flush();
116 void term_vprintf(const char *fmt, va_list ap)
118 char buf[4096];
119 vsnprintf(buf, sizeof(buf), fmt, ap);
120 term_puts(buf);
123 void term_printf(const char *fmt, ...)
125 va_list ap;
126 va_start(ap, fmt);
127 term_vprintf(fmt, ap);
128 va_end(ap);
131 void term_print_filename(const char *filename)
133 int i;
135 for (i = 0; filename[i]; i++) {
136 switch (filename[i]) {
137 case ' ':
138 case '"':
139 case '\\':
140 term_printf("\\%c", filename[i]);
141 break;
142 case '\t':
143 term_printf("\\t");
144 break;
145 case '\r':
146 term_printf("\\r");
147 break;
148 case '\n':
149 term_printf("\\n");
150 break;
151 default:
152 term_printf("%c", filename[i]);
153 break;
158 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
160 va_list ap;
161 va_start(ap, fmt);
162 term_vprintf(fmt, ap);
163 va_end(ap);
164 return 0;
167 static int compare_cmd(const char *name, const char *list)
169 const char *p, *pstart;
170 int len;
171 len = strlen(name);
172 p = list;
173 for(;;) {
174 pstart = p;
175 p = strchr(p, '|');
176 if (!p)
177 p = pstart + strlen(pstart);
178 if ((p - pstart) == len && !memcmp(pstart, name, len))
179 return 1;
180 if (*p == '\0')
181 break;
182 p++;
184 return 0;
187 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
189 term_cmd_t *cmd;
191 for(cmd = cmds; cmd->name != NULL; cmd++) {
192 if (!name || !strcmp(name, cmd->name))
193 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
197 static void help_cmd(const char *name)
199 if (name && !strcmp(name, "info")) {
200 help_cmd1(info_cmds, "info ", NULL);
201 } else {
202 help_cmd1(term_cmds, "", name);
203 if (name && !strcmp(name, "log")) {
204 CPULogItem *item;
205 term_printf("Log items (comma separated):\n");
206 term_printf("%-10s %s\n", "none", "remove all logs");
207 for(item = cpu_log_items; item->mask != 0; item++) {
208 term_printf("%-10s %s\n", item->name, item->help);
214 static void do_help(const char *name)
216 help_cmd(name);
219 static void do_commit(const char *device)
221 int i, all_devices;
223 all_devices = !strcmp(device, "all");
224 for (i = 0; i < nb_drives; i++) {
225 if (all_devices ||
226 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
227 bdrv_commit(drives_table[i].bdrv);
231 static void do_info(const char *item)
233 term_cmd_t *cmd;
235 if (!item)
236 goto help;
237 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
238 if (compare_cmd(item, cmd->name))
239 goto found;
241 help:
242 help_cmd("info");
243 return;
244 found:
245 cmd->handler();
248 static void do_info_version(void)
250 term_printf("%s\n", QEMU_VERSION);
253 static void do_info_name(void)
255 if (qemu_name)
256 term_printf("%s\n", qemu_name);
259 static void do_info_block(void)
261 bdrv_info();
264 static void do_info_blockstats(void)
266 bdrv_info_stats();
269 /* get the current CPU defined by the user */
270 static int mon_set_cpu(int cpu_index)
272 CPUState *env;
274 for(env = first_cpu; env != NULL; env = env->next_cpu) {
275 if (env->cpu_index == cpu_index) {
276 mon_cpu = env;
277 return 0;
280 return -1;
283 static CPUState *mon_get_cpu(void)
285 if (!mon_cpu) {
286 mon_set_cpu(0);
289 if (kvm_enabled())
290 kvm_save_registers(mon_cpu);
292 return mon_cpu;
295 static void do_info_registers(void)
297 CPUState *env;
298 env = mon_get_cpu();
299 if (!env)
300 return;
301 #ifdef TARGET_I386
302 cpu_dump_state(env, NULL, monitor_fprintf,
303 X86_DUMP_FPU);
304 #else
305 cpu_dump_state(env, NULL, monitor_fprintf,
307 #endif
310 static void do_info_cpus(void)
312 CPUState *env;
314 /* just to set the default cpu if not already done */
315 mon_get_cpu();
317 for(env = first_cpu; env != NULL; env = env->next_cpu) {
318 term_printf("%c CPU #%d:",
319 (env == mon_cpu) ? '*' : ' ',
320 env->cpu_index);
321 #if defined(TARGET_I386)
322 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
323 if (env->hflags & HF_HALTED_MASK)
324 term_printf(" (halted)");
325 #elif defined(TARGET_PPC)
326 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
327 if (env->halted)
328 term_printf(" (halted)");
329 #elif defined(TARGET_SPARC)
330 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
331 if (env->halted)
332 term_printf(" (halted)");
333 #elif defined(TARGET_MIPS)
334 term_printf(" PC=0x" TARGET_FMT_lx, env->PC[env->current_tc]);
335 if (env->halted)
336 term_printf(" (halted)");
337 #endif
338 term_printf("\n");
342 static void do_cpu_set(int index)
344 if (mon_set_cpu(index) < 0)
345 term_printf("Invalid CPU index\n");
348 static void do_cpu_set_nr(int value, const char *status)
350 int state;
352 if (!strcmp(status, "online"))
353 state = 1;
354 else if (!strcmp(status, "offline"))
355 state = 0;
356 else {
357 term_printf("invalid status: %s\n", status);
358 return;
360 #if defined(TARGET_I386) || defined(TARGET_X86_64)
361 qemu_system_cpu_hot_add(value, state);
362 #endif
365 static void do_info_jit(void)
367 dump_exec_info(NULL, monitor_fprintf);
370 static void do_info_history (void)
372 int i;
373 const char *str;
375 i = 0;
376 for(;;) {
377 str = readline_get_history(i);
378 if (!str)
379 break;
380 term_printf("%d: '%s'\n", i, str);
381 i++;
385 #if defined(TARGET_PPC)
386 /* XXX: not implemented in other targets */
387 static void do_info_cpu_stats (void)
389 CPUState *env;
391 env = mon_get_cpu();
392 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
394 #endif
396 static void do_quit(void)
398 exit(0);
401 static int eject_device(BlockDriverState *bs, int force)
403 if (bdrv_is_inserted(bs)) {
404 if (!force) {
405 if (!bdrv_is_removable(bs)) {
406 term_printf("device is not removable\n");
407 return -1;
409 if (bdrv_is_locked(bs)) {
410 term_printf("device is locked\n");
411 return -1;
414 bdrv_close(bs);
416 return 0;
419 static void do_eject(int force, const char *filename)
421 BlockDriverState *bs;
423 bs = bdrv_find(filename);
424 if (!bs) {
425 term_printf("device not found\n");
426 return;
428 eject_device(bs, force);
431 static void do_change_block(const char *device, const char *filename)
433 BlockDriverState *bs;
435 bs = bdrv_find(device);
436 if (!bs) {
437 term_printf("device not found\n");
438 return;
440 if (eject_device(bs, 0) < 0)
441 return;
442 bdrv_open(bs, filename, 0);
443 qemu_key_check(bs, filename);
446 static void do_change_vnc(const char *target)
448 if (strcmp(target, "passwd") == 0 ||
449 strcmp(target, "password") == 0) {
450 char password[9];
451 monitor_readline("Password: ", 1, password, sizeof(password)-1);
452 password[sizeof(password)-1] = '\0';
453 if (vnc_display_password(NULL, password) < 0)
454 term_printf("could not set VNC server password\n");
455 } else {
456 if (vnc_display_open(NULL, target) < 0)
457 term_printf("could not start VNC server on %s\n", target);
461 static void do_change(const char *device, const char *target)
463 if (strcmp(device, "vnc") == 0) {
464 do_change_vnc(target);
465 } else {
466 do_change_block(device, target);
470 static void do_screen_dump(const char *filename)
472 vga_hw_screen_dump(filename);
475 static void do_logfile(const char *filename)
477 cpu_set_log_filename(filename);
480 static void do_log(const char *items)
482 int mask;
484 if (!strcmp(items, "none")) {
485 mask = 0;
486 } else {
487 mask = cpu_str_to_log_mask(items);
488 if (!mask) {
489 help_cmd("log");
490 return;
493 cpu_set_log(mask);
496 static void do_stop(void)
498 vm_stop(EXCP_INTERRUPT);
501 static void do_cont(void)
503 vm_start();
506 #ifdef CONFIG_GDBSTUB
507 static void do_gdbserver(const char *port)
509 if (!port)
510 port = DEFAULT_GDBSTUB_PORT;
511 if (gdbserver_start(port) < 0) {
512 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
513 } else {
514 qemu_printf("Waiting gdb connection on port '%s'\n", port);
517 #endif
519 static void term_printc(int c)
521 term_printf("'");
522 switch(c) {
523 case '\'':
524 term_printf("\\'");
525 break;
526 case '\\':
527 term_printf("\\\\");
528 break;
529 case '\n':
530 term_printf("\\n");
531 break;
532 case '\r':
533 term_printf("\\r");
534 break;
535 default:
536 if (c >= 32 && c <= 126) {
537 term_printf("%c", c);
538 } else {
539 term_printf("\\x%02x", c);
541 break;
543 term_printf("'");
546 static void memory_dump(int count, int format, int wsize,
547 target_phys_addr_t addr, int is_physical)
549 CPUState *env;
550 int nb_per_line, l, line_size, i, max_digits, len;
551 uint8_t buf[16];
552 uint64_t v;
554 if (format == 'i') {
555 int flags;
556 flags = 0;
557 env = mon_get_cpu();
558 if (!env && !is_physical)
559 return;
560 #ifdef TARGET_I386
561 if (wsize == 2) {
562 flags = 1;
563 } else if (wsize == 4) {
564 flags = 0;
565 } else {
566 /* as default we use the current CS size */
567 flags = 0;
568 if (env) {
569 #ifdef TARGET_X86_64
570 if ((env->efer & MSR_EFER_LMA) &&
571 (env->segs[R_CS].flags & DESC_L_MASK))
572 flags = 2;
573 else
574 #endif
575 if (!(env->segs[R_CS].flags & DESC_B_MASK))
576 flags = 1;
579 #endif
580 monitor_disas(env, addr, count, is_physical, flags);
581 return;
584 len = wsize * count;
585 if (wsize == 1)
586 line_size = 8;
587 else
588 line_size = 16;
589 nb_per_line = line_size / wsize;
590 max_digits = 0;
592 switch(format) {
593 case 'o':
594 max_digits = (wsize * 8 + 2) / 3;
595 break;
596 default:
597 case 'x':
598 max_digits = (wsize * 8) / 4;
599 break;
600 case 'u':
601 case 'd':
602 max_digits = (wsize * 8 * 10 + 32) / 33;
603 break;
604 case 'c':
605 wsize = 1;
606 break;
609 while (len > 0) {
610 if (is_physical)
611 term_printf(TARGET_FMT_plx ":", addr);
612 else
613 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
614 l = len;
615 if (l > line_size)
616 l = line_size;
617 if (is_physical) {
618 cpu_physical_memory_rw(addr, buf, l, 0);
619 } else {
620 env = mon_get_cpu();
621 if (!env)
622 break;
623 cpu_memory_rw_debug(env, addr, buf, l, 0);
625 i = 0;
626 while (i < l) {
627 switch(wsize) {
628 default:
629 case 1:
630 v = ldub_raw(buf + i);
631 break;
632 case 2:
633 v = lduw_raw(buf + i);
634 break;
635 case 4:
636 v = (uint32_t)ldl_raw(buf + i);
637 break;
638 case 8:
639 v = ldq_raw(buf + i);
640 break;
642 term_printf(" ");
643 switch(format) {
644 case 'o':
645 term_printf("%#*" PRIo64, max_digits, v);
646 break;
647 case 'x':
648 term_printf("0x%0*" PRIx64, max_digits, v);
649 break;
650 case 'u':
651 term_printf("%*" PRIu64, max_digits, v);
652 break;
653 case 'd':
654 term_printf("%*" PRId64, max_digits, v);
655 break;
656 case 'c':
657 term_printc(v);
658 break;
660 i += wsize;
662 term_printf("\n");
663 addr += l;
664 len -= l;
668 #if TARGET_LONG_BITS == 64
669 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
670 #else
671 #define GET_TLONG(h, l) (l)
672 #endif
674 static void do_memory_dump(int count, int format, int size,
675 uint32_t addrh, uint32_t addrl)
677 target_long addr = GET_TLONG(addrh, addrl);
678 memory_dump(count, format, size, addr, 0);
681 #if TARGET_PHYS_ADDR_BITS > 32
682 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
683 #else
684 #define GET_TPHYSADDR(h, l) (l)
685 #endif
687 static void do_physical_memory_dump(int count, int format, int size,
688 uint32_t addrh, uint32_t addrl)
691 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
692 memory_dump(count, format, size, addr, 1);
695 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
697 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
698 #if TARGET_PHYS_ADDR_BITS == 32
699 switch(format) {
700 case 'o':
701 term_printf("%#o", val);
702 break;
703 case 'x':
704 term_printf("%#x", val);
705 break;
706 case 'u':
707 term_printf("%u", val);
708 break;
709 default:
710 case 'd':
711 term_printf("%d", val);
712 break;
713 case 'c':
714 term_printc(val);
715 break;
717 #else
718 switch(format) {
719 case 'o':
720 term_printf("%#" PRIo64, val);
721 break;
722 case 'x':
723 term_printf("%#" PRIx64, val);
724 break;
725 case 'u':
726 term_printf("%" PRIu64, val);
727 break;
728 default:
729 case 'd':
730 term_printf("%" PRId64, val);
731 break;
732 case 'c':
733 term_printc(val);
734 break;
736 #endif
737 term_printf("\n");
740 static void do_memory_save(unsigned int valh, unsigned int vall,
741 uint32_t size, const char *filename)
743 FILE *f;
744 target_long addr = GET_TLONG(valh, vall);
745 uint32_t l;
746 CPUState *env;
747 uint8_t buf[1024];
749 env = mon_get_cpu();
750 if (!env)
751 return;
753 f = fopen(filename, "wb");
754 if (!f) {
755 term_printf("could not open '%s'\n", filename);
756 return;
758 while (size != 0) {
759 l = sizeof(buf);
760 if (l > size)
761 l = size;
762 cpu_memory_rw_debug(env, addr, buf, l, 0);
763 fwrite(buf, 1, l, f);
764 addr += l;
765 size -= l;
767 fclose(f);
770 static void do_sum(uint32_t start, uint32_t size)
772 uint32_t addr;
773 uint8_t buf[1];
774 uint16_t sum;
776 sum = 0;
777 for(addr = start; addr < (start + size); addr++) {
778 cpu_physical_memory_rw(addr, buf, 1, 0);
779 /* BSD sum algorithm ('sum' Unix command) */
780 sum = (sum >> 1) | (sum << 15);
781 sum += buf[0];
783 term_printf("%05d\n", sum);
786 typedef struct {
787 int keycode;
788 const char *name;
789 } KeyDef;
791 static const KeyDef key_defs[] = {
792 { 0x2a, "shift" },
793 { 0x36, "shift_r" },
795 { 0x38, "alt" },
796 { 0xb8, "alt_r" },
797 { 0x1d, "ctrl" },
798 { 0x9d, "ctrl_r" },
800 { 0xdd, "menu" },
802 { 0x01, "esc" },
804 { 0x02, "1" },
805 { 0x03, "2" },
806 { 0x04, "3" },
807 { 0x05, "4" },
808 { 0x06, "5" },
809 { 0x07, "6" },
810 { 0x08, "7" },
811 { 0x09, "8" },
812 { 0x0a, "9" },
813 { 0x0b, "0" },
814 { 0x0c, "minus" },
815 { 0x0d, "equal" },
816 { 0x0e, "backspace" },
818 { 0x0f, "tab" },
819 { 0x10, "q" },
820 { 0x11, "w" },
821 { 0x12, "e" },
822 { 0x13, "r" },
823 { 0x14, "t" },
824 { 0x15, "y" },
825 { 0x16, "u" },
826 { 0x17, "i" },
827 { 0x18, "o" },
828 { 0x19, "p" },
830 { 0x1c, "ret" },
832 { 0x1e, "a" },
833 { 0x1f, "s" },
834 { 0x20, "d" },
835 { 0x21, "f" },
836 { 0x22, "g" },
837 { 0x23, "h" },
838 { 0x24, "j" },
839 { 0x25, "k" },
840 { 0x26, "l" },
842 { 0x2c, "z" },
843 { 0x2d, "x" },
844 { 0x2e, "c" },
845 { 0x2f, "v" },
846 { 0x30, "b" },
847 { 0x31, "n" },
848 { 0x32, "m" },
850 { 0x37, "asterisk" },
852 { 0x39, "spc" },
853 { 0x3a, "caps_lock" },
854 { 0x3b, "f1" },
855 { 0x3c, "f2" },
856 { 0x3d, "f3" },
857 { 0x3e, "f4" },
858 { 0x3f, "f5" },
859 { 0x40, "f6" },
860 { 0x41, "f7" },
861 { 0x42, "f8" },
862 { 0x43, "f9" },
863 { 0x44, "f10" },
864 { 0x45, "num_lock" },
865 { 0x46, "scroll_lock" },
867 { 0xb5, "kp_divide" },
868 { 0x37, "kp_multiply" },
869 { 0x4a, "kp_subtract" },
870 { 0x4e, "kp_add" },
871 { 0x9c, "kp_enter" },
872 { 0x53, "kp_decimal" },
874 { 0x52, "kp_0" },
875 { 0x4f, "kp_1" },
876 { 0x50, "kp_2" },
877 { 0x51, "kp_3" },
878 { 0x4b, "kp_4" },
879 { 0x4c, "kp_5" },
880 { 0x4d, "kp_6" },
881 { 0x47, "kp_7" },
882 { 0x48, "kp_8" },
883 { 0x49, "kp_9" },
885 { 0x56, "<" },
887 { 0x57, "f11" },
888 { 0x58, "f12" },
890 { 0xb7, "print" },
892 { 0xc7, "home" },
893 { 0xc9, "pgup" },
894 { 0xd1, "pgdn" },
895 { 0xcf, "end" },
897 { 0xcb, "left" },
898 { 0xc8, "up" },
899 { 0xd0, "down" },
900 { 0xcd, "right" },
902 { 0xd2, "insert" },
903 { 0xd3, "delete" },
904 { 0, NULL },
907 static int get_keycode(const char *key)
909 const KeyDef *p;
910 char *endp;
911 int ret;
913 for(p = key_defs; p->name != NULL; p++) {
914 if (!strcmp(key, p->name))
915 return p->keycode;
917 if (strstart(key, "0x", NULL)) {
918 ret = strtoul(key, &endp, 0);
919 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
920 return ret;
922 return -1;
925 static void do_send_key(const char *string)
927 char keybuf[16], *q;
928 uint8_t keycodes[16];
929 const char *p;
930 int nb_keycodes, keycode, i;
932 nb_keycodes = 0;
933 p = string;
934 while (*p != '\0') {
935 q = keybuf;
936 while (*p != '\0' && *p != '-') {
937 if ((q - keybuf) < sizeof(keybuf) - 1) {
938 *q++ = *p;
940 p++;
942 *q = '\0';
943 keycode = get_keycode(keybuf);
944 if (keycode < 0) {
945 term_printf("unknown key: '%s'\n", keybuf);
946 return;
948 keycodes[nb_keycodes++] = keycode;
949 if (*p == '\0')
950 break;
951 p++;
953 /* key down events */
954 for(i = 0; i < nb_keycodes; i++) {
955 keycode = keycodes[i];
956 if (keycode & 0x80)
957 kbd_put_keycode(0xe0);
958 kbd_put_keycode(keycode & 0x7f);
960 /* key up events */
961 for(i = nb_keycodes - 1; i >= 0; i--) {
962 keycode = keycodes[i];
963 if (keycode & 0x80)
964 kbd_put_keycode(0xe0);
965 kbd_put_keycode(keycode | 0x80);
969 static int mouse_button_state;
971 static void do_mouse_move(const char *dx_str, const char *dy_str,
972 const char *dz_str)
974 int dx, dy, dz;
975 dx = strtol(dx_str, NULL, 0);
976 dy = strtol(dy_str, NULL, 0);
977 dz = 0;
978 if (dz_str)
979 dz = strtol(dz_str, NULL, 0);
980 kbd_mouse_event(dx, dy, dz, mouse_button_state);
983 static void do_mouse_button(int button_state)
985 mouse_button_state = button_state;
986 kbd_mouse_event(0, 0, 0, mouse_button_state);
989 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
991 uint32_t val;
992 int suffix;
994 if (has_index) {
995 cpu_outb(NULL, addr & 0xffff, index & 0xff);
996 addr++;
998 addr &= 0xffff;
1000 switch(size) {
1001 default:
1002 case 1:
1003 val = cpu_inb(NULL, addr);
1004 suffix = 'b';
1005 break;
1006 case 2:
1007 val = cpu_inw(NULL, addr);
1008 suffix = 'w';
1009 break;
1010 case 4:
1011 val = cpu_inl(NULL, addr);
1012 suffix = 'l';
1013 break;
1015 term_printf("port%c[0x%04x] = %#0*x\n",
1016 suffix, addr, size * 2, val);
1019 static void do_system_reset(void)
1021 qemu_system_reset_request();
1024 static void do_system_powerdown(void)
1026 qemu_system_powerdown_request();
1029 #if defined(TARGET_I386)
1030 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1032 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1033 addr,
1034 pte & mask,
1035 pte & PG_GLOBAL_MASK ? 'G' : '-',
1036 pte & PG_PSE_MASK ? 'P' : '-',
1037 pte & PG_DIRTY_MASK ? 'D' : '-',
1038 pte & PG_ACCESSED_MASK ? 'A' : '-',
1039 pte & PG_PCD_MASK ? 'C' : '-',
1040 pte & PG_PWT_MASK ? 'T' : '-',
1041 pte & PG_USER_MASK ? 'U' : '-',
1042 pte & PG_RW_MASK ? 'W' : '-');
1045 static void tlb_info(void)
1047 CPUState *env;
1048 int l1, l2;
1049 uint32_t pgd, pde, pte;
1051 env = mon_get_cpu();
1052 if (!env)
1053 return;
1055 if (!(env->cr[0] & CR0_PG_MASK)) {
1056 term_printf("PG disabled\n");
1057 return;
1059 pgd = env->cr[3] & ~0xfff;
1060 for(l1 = 0; l1 < 1024; l1++) {
1061 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1062 pde = le32_to_cpu(pde);
1063 if (pde & PG_PRESENT_MASK) {
1064 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1065 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1066 } else {
1067 for(l2 = 0; l2 < 1024; l2++) {
1068 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1069 (uint8_t *)&pte, 4);
1070 pte = le32_to_cpu(pte);
1071 if (pte & PG_PRESENT_MASK) {
1072 print_pte((l1 << 22) + (l2 << 12),
1073 pte & ~PG_PSE_MASK,
1074 ~0xfff);
1082 static void mem_print(uint32_t *pstart, int *plast_prot,
1083 uint32_t end, int prot)
1085 int prot1;
1086 prot1 = *plast_prot;
1087 if (prot != prot1) {
1088 if (*pstart != -1) {
1089 term_printf("%08x-%08x %08x %c%c%c\n",
1090 *pstart, end, end - *pstart,
1091 prot1 & PG_USER_MASK ? 'u' : '-',
1092 'r',
1093 prot1 & PG_RW_MASK ? 'w' : '-');
1095 if (prot != 0)
1096 *pstart = end;
1097 else
1098 *pstart = -1;
1099 *plast_prot = prot;
1103 static void mem_info(void)
1105 CPUState *env;
1106 int l1, l2, prot, last_prot;
1107 uint32_t pgd, pde, pte, start, end;
1109 env = mon_get_cpu();
1110 if (!env)
1111 return;
1113 if (!(env->cr[0] & CR0_PG_MASK)) {
1114 term_printf("PG disabled\n");
1115 return;
1117 pgd = env->cr[3] & ~0xfff;
1118 last_prot = 0;
1119 start = -1;
1120 for(l1 = 0; l1 < 1024; l1++) {
1121 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1122 pde = le32_to_cpu(pde);
1123 end = l1 << 22;
1124 if (pde & PG_PRESENT_MASK) {
1125 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1126 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1127 mem_print(&start, &last_prot, end, prot);
1128 } else {
1129 for(l2 = 0; l2 < 1024; l2++) {
1130 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1131 (uint8_t *)&pte, 4);
1132 pte = le32_to_cpu(pte);
1133 end = (l1 << 22) + (l2 << 12);
1134 if (pte & PG_PRESENT_MASK) {
1135 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1136 } else {
1137 prot = 0;
1139 mem_print(&start, &last_prot, end, prot);
1142 } else {
1143 prot = 0;
1144 mem_print(&start, &last_prot, end, prot);
1148 #endif
1150 static void do_info_kqemu(void)
1152 #ifdef USE_KQEMU
1153 CPUState *env;
1154 int val;
1155 val = 0;
1156 env = mon_get_cpu();
1157 if (!env) {
1158 term_printf("No cpu initialized yet");
1159 return;
1161 val = env->kqemu_enabled;
1162 term_printf("kqemu support: ");
1163 switch(val) {
1164 default:
1165 case 0:
1166 term_printf("disabled\n");
1167 break;
1168 case 1:
1169 term_printf("enabled for user code\n");
1170 break;
1171 case 2:
1172 term_printf("enabled for user and kernel code\n");
1173 break;
1175 #else
1176 term_printf("kqemu support: not compiled\n");
1177 #endif
1180 #ifdef CONFIG_PROFILER
1182 int64_t kqemu_time;
1183 int64_t qemu_time;
1184 int64_t kqemu_exec_count;
1185 int64_t dev_time;
1186 int64_t kqemu_ret_int_count;
1187 int64_t kqemu_ret_excp_count;
1188 int64_t kqemu_ret_intr_count;
1190 static void do_info_profile(void)
1192 int64_t total;
1193 total = qemu_time;
1194 if (total == 0)
1195 total = 1;
1196 term_printf("async time %" PRId64 " (%0.3f)\n",
1197 dev_time, dev_time / (double)ticks_per_sec);
1198 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1199 qemu_time, qemu_time / (double)ticks_per_sec);
1200 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1201 kqemu_time, kqemu_time / (double)ticks_per_sec,
1202 kqemu_time / (double)total * 100.0,
1203 kqemu_exec_count,
1204 kqemu_ret_int_count,
1205 kqemu_ret_excp_count,
1206 kqemu_ret_intr_count);
1207 qemu_time = 0;
1208 kqemu_time = 0;
1209 kqemu_exec_count = 0;
1210 dev_time = 0;
1211 kqemu_ret_int_count = 0;
1212 kqemu_ret_excp_count = 0;
1213 kqemu_ret_intr_count = 0;
1214 #ifdef USE_KQEMU
1215 kqemu_record_dump();
1216 #endif
1218 #else
1219 static void do_info_profile(void)
1221 term_printf("Internal profiler not compiled\n");
1223 #endif
1225 /* Capture support */
1226 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1228 static void do_info_capture (void)
1230 int i;
1231 CaptureState *s;
1233 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1234 term_printf ("[%d]: ", i);
1235 s->ops.info (s->opaque);
1239 static void do_stop_capture (int n)
1241 int i;
1242 CaptureState *s;
1244 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1245 if (i == n) {
1246 s->ops.destroy (s->opaque);
1247 LIST_REMOVE (s, entries);
1248 qemu_free (s);
1249 return;
1254 #ifdef HAS_AUDIO
1255 int wav_start_capture (CaptureState *s, const char *path, int freq,
1256 int bits, int nchannels);
1258 static void do_wav_capture (const char *path,
1259 int has_freq, int freq,
1260 int has_bits, int bits,
1261 int has_channels, int nchannels)
1263 CaptureState *s;
1265 s = qemu_mallocz (sizeof (*s));
1266 if (!s) {
1267 term_printf ("Not enough memory to add wave capture\n");
1268 return;
1271 freq = has_freq ? freq : 44100;
1272 bits = has_bits ? bits : 16;
1273 nchannels = has_channels ? nchannels : 2;
1275 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1276 term_printf ("Faied to add wave capture\n");
1277 qemu_free (s);
1279 LIST_INSERT_HEAD (&capture_head, s, entries);
1281 #endif
1283 static term_cmd_t term_cmds[] = {
1284 { "help|?", "s?", do_help,
1285 "[cmd]", "show the help" },
1286 { "commit", "s", do_commit,
1287 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1288 { "info", "s?", do_info,
1289 "subcommand", "show various information about the system state" },
1290 { "q|quit", "", do_quit,
1291 "", "quit the emulator" },
1292 { "eject", "-fB", do_eject,
1293 "[-f] device", "eject a removable medium (use -f to force it)" },
1294 { "change", "BF", do_change,
1295 "device filename", "change a removable medium" },
1296 { "screendump", "F", do_screen_dump,
1297 "filename", "save screen into PPM image 'filename'" },
1298 { "logfile", "s", do_logfile,
1299 "filename", "output logs to 'filename'" },
1300 { "log", "s", do_log,
1301 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1302 { "savevm", "s?", do_savevm,
1303 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1304 { "loadvm", "s", do_loadvm,
1305 "tag|id", "restore a VM snapshot from its tag or id" },
1306 { "delvm", "s", do_delvm,
1307 "tag|id", "delete a VM snapshot from its tag or id" },
1308 { "stop", "", do_stop,
1309 "", "stop emulation", },
1310 { "c|cont", "", do_cont,
1311 "", "resume emulation", },
1312 #ifdef CONFIG_GDBSTUB
1313 { "gdbserver", "s?", do_gdbserver,
1314 "[port]", "start gdbserver session (default port=1234)", },
1315 #endif
1316 { "x", "/l", do_memory_dump,
1317 "/fmt addr", "virtual memory dump starting at 'addr'", },
1318 { "xp", "/l", do_physical_memory_dump,
1319 "/fmt addr", "physical memory dump starting at 'addr'", },
1320 { "p|print", "/l", do_print,
1321 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1322 { "i", "/ii.", do_ioport_read,
1323 "/fmt addr", "I/O port read" },
1325 { "sendkey", "s", do_send_key,
1326 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1327 { "system_reset", "", do_system_reset,
1328 "", "reset the system" },
1329 { "system_powerdown", "", do_system_powerdown,
1330 "", "send system power down event" },
1331 { "sum", "ii", do_sum,
1332 "addr size", "compute the checksum of a memory region" },
1333 { "usb_add", "s", do_usb_add,
1334 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1335 { "usb_del", "s", do_usb_del,
1336 "device", "remove USB device 'bus.addr'" },
1337 { "cpu", "i", do_cpu_set,
1338 "index", "set the default CPU" },
1339 { "mouse_move", "sss?", do_mouse_move,
1340 "dx dy [dz]", "send mouse move events" },
1341 { "mouse_button", "i", do_mouse_button,
1342 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1343 { "mouse_set", "i", do_mouse_set,
1344 "index", "set which mouse device receives events" },
1345 #ifdef HAS_AUDIO
1346 { "wavcapture", "si?i?i?", do_wav_capture,
1347 "path [frequency bits channels]",
1348 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1349 #endif
1350 { "stopcapture", "i", do_stop_capture,
1351 "capture index", "stop capture" },
1352 { "memsave", "lis", do_memory_save,
1353 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1354 { "migrate", "-ds", do_migrate,
1355 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1356 { "migrate_cancel", "", do_migrate_cancel,
1357 "", "cancel the current VM migration" },
1358 { "migrate_set_speed", "s", do_migrate_set_speed,
1359 "value", "set maximum speed (in bytes) for migrations" },
1360 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1361 { NULL, NULL, },
1364 static term_cmd_t info_cmds[] = {
1365 { "version", "", do_info_version,
1366 "", "show the version of qemu" },
1367 { "network", "", do_info_network,
1368 "", "show the network state" },
1369 { "block", "", do_info_block,
1370 "", "show the block devices" },
1371 { "blockstats", "", do_info_blockstats,
1372 "", "show block device statistics" },
1373 { "registers", "", do_info_registers,
1374 "", "show the cpu registers" },
1375 { "cpus", "", do_info_cpus,
1376 "", "show infos for each CPU" },
1377 { "history", "", do_info_history,
1378 "", "show the command line history", },
1379 { "irq", "", irq_info,
1380 "", "show the interrupts statistics (if available)", },
1381 { "pic", "", pic_info,
1382 "", "show i8259 (PIC) state", },
1383 { "pci", "", pci_info,
1384 "", "show PCI info", },
1385 #if defined(TARGET_I386)
1386 { "tlb", "", tlb_info,
1387 "", "show virtual to physical memory mappings", },
1388 { "mem", "", mem_info,
1389 "", "show the active virtual memory mappings", },
1390 #endif
1391 { "jit", "", do_info_jit,
1392 "", "show dynamic compiler info", },
1393 { "kqemu", "", do_info_kqemu,
1394 "", "show kqemu information", },
1395 { "usb", "", usb_info,
1396 "", "show guest USB devices", },
1397 { "usbhost", "", usb_host_info,
1398 "", "show host USB devices", },
1399 { "profile", "", do_info_profile,
1400 "", "show profiling information", },
1401 { "capture", "", do_info_capture,
1402 "", "show capture information" },
1403 { "snapshots", "", do_info_snapshots,
1404 "", "show the currently saved VM snapshots" },
1405 { "pcmcia", "", pcmcia_info,
1406 "", "show guest PCMCIA status" },
1407 { "mice", "", do_info_mice,
1408 "", "show which guest mouse is receiving events" },
1409 { "vnc", "", do_info_vnc,
1410 "", "show the vnc server status"},
1411 { "name", "", do_info_name,
1412 "", "show the current VM name" },
1413 #if defined(TARGET_PPC)
1414 { "cpustats", "", do_info_cpu_stats,
1415 "", "show CPU statistics", },
1416 #endif
1417 #if defined(CONFIG_SLIRP)
1418 { "slirp", "", do_info_slirp,
1419 "", "show SLIRP statistics", },
1420 #endif
1421 { "migration", "", do_info_migration,
1422 "", "show migration information" },
1423 { NULL, NULL, },
1426 /*******************************************************************/
1428 static const char *pch;
1429 static jmp_buf expr_env;
1431 #define MD_TLONG 0
1432 #define MD_I32 1
1434 typedef struct MonitorDef {
1435 const char *name;
1436 int offset;
1437 target_long (*get_value)(struct MonitorDef *md, int val);
1438 int type;
1439 } MonitorDef;
1441 #if defined(TARGET_I386)
1442 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1444 CPUState *env = mon_get_cpu();
1445 if (!env)
1446 return 0;
1447 return env->eip + env->segs[R_CS].base;
1449 #endif
1451 #if defined(TARGET_PPC)
1452 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1454 CPUState *env = mon_get_cpu();
1455 unsigned int u;
1456 int i;
1458 if (!env)
1459 return 0;
1461 u = 0;
1462 for (i = 0; i < 8; i++)
1463 u |= env->crf[i] << (32 - (4 * i));
1465 return u;
1468 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1470 CPUState *env = mon_get_cpu();
1471 if (!env)
1472 return 0;
1473 return env->msr;
1476 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1478 CPUState *env = mon_get_cpu();
1479 if (!env)
1480 return 0;
1481 return ppc_load_xer(env);
1484 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1486 CPUState *env = mon_get_cpu();
1487 if (!env)
1488 return 0;
1489 return cpu_ppc_load_decr(env);
1492 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1494 CPUState *env = mon_get_cpu();
1495 if (!env)
1496 return 0;
1497 return cpu_ppc_load_tbu(env);
1500 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1502 CPUState *env = mon_get_cpu();
1503 if (!env)
1504 return 0;
1505 return cpu_ppc_load_tbl(env);
1507 #endif
1509 #if defined(TARGET_SPARC)
1510 #ifndef TARGET_SPARC64
1511 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1513 CPUState *env = mon_get_cpu();
1514 if (!env)
1515 return 0;
1516 return GET_PSR(env);
1518 #endif
1520 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1522 CPUState *env = mon_get_cpu();
1523 if (!env)
1524 return 0;
1525 return env->regwptr[val];
1527 #endif
1529 static MonitorDef monitor_defs[] = {
1530 #ifdef TARGET_I386
1532 #define SEG(name, seg) \
1533 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1534 { name ".base", offsetof(CPUState, segs[seg].base) },\
1535 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1537 { "eax", offsetof(CPUState, regs[0]) },
1538 { "ecx", offsetof(CPUState, regs[1]) },
1539 { "edx", offsetof(CPUState, regs[2]) },
1540 { "ebx", offsetof(CPUState, regs[3]) },
1541 { "esp|sp", offsetof(CPUState, regs[4]) },
1542 { "ebp|fp", offsetof(CPUState, regs[5]) },
1543 { "esi", offsetof(CPUState, regs[6]) },
1544 { "edi", offsetof(CPUState, regs[7]) },
1545 #ifdef TARGET_X86_64
1546 { "r8", offsetof(CPUState, regs[8]) },
1547 { "r9", offsetof(CPUState, regs[9]) },
1548 { "r10", offsetof(CPUState, regs[10]) },
1549 { "r11", offsetof(CPUState, regs[11]) },
1550 { "r12", offsetof(CPUState, regs[12]) },
1551 { "r13", offsetof(CPUState, regs[13]) },
1552 { "r14", offsetof(CPUState, regs[14]) },
1553 { "r15", offsetof(CPUState, regs[15]) },
1554 #endif
1555 { "eflags", offsetof(CPUState, eflags) },
1556 { "eip", offsetof(CPUState, eip) },
1557 SEG("cs", R_CS)
1558 SEG("ds", R_DS)
1559 SEG("es", R_ES)
1560 SEG("ss", R_SS)
1561 SEG("fs", R_FS)
1562 SEG("gs", R_GS)
1563 { "pc", 0, monitor_get_pc, },
1564 #elif defined(TARGET_PPC)
1565 /* General purpose registers */
1566 { "r0", offsetof(CPUState, gpr[0]) },
1567 { "r1", offsetof(CPUState, gpr[1]) },
1568 { "r2", offsetof(CPUState, gpr[2]) },
1569 { "r3", offsetof(CPUState, gpr[3]) },
1570 { "r4", offsetof(CPUState, gpr[4]) },
1571 { "r5", offsetof(CPUState, gpr[5]) },
1572 { "r6", offsetof(CPUState, gpr[6]) },
1573 { "r7", offsetof(CPUState, gpr[7]) },
1574 { "r8", offsetof(CPUState, gpr[8]) },
1575 { "r9", offsetof(CPUState, gpr[9]) },
1576 { "r10", offsetof(CPUState, gpr[10]) },
1577 { "r11", offsetof(CPUState, gpr[11]) },
1578 { "r12", offsetof(CPUState, gpr[12]) },
1579 { "r13", offsetof(CPUState, gpr[13]) },
1580 { "r14", offsetof(CPUState, gpr[14]) },
1581 { "r15", offsetof(CPUState, gpr[15]) },
1582 { "r16", offsetof(CPUState, gpr[16]) },
1583 { "r17", offsetof(CPUState, gpr[17]) },
1584 { "r18", offsetof(CPUState, gpr[18]) },
1585 { "r19", offsetof(CPUState, gpr[19]) },
1586 { "r20", offsetof(CPUState, gpr[20]) },
1587 { "r21", offsetof(CPUState, gpr[21]) },
1588 { "r22", offsetof(CPUState, gpr[22]) },
1589 { "r23", offsetof(CPUState, gpr[23]) },
1590 { "r24", offsetof(CPUState, gpr[24]) },
1591 { "r25", offsetof(CPUState, gpr[25]) },
1592 { "r26", offsetof(CPUState, gpr[26]) },
1593 { "r27", offsetof(CPUState, gpr[27]) },
1594 { "r28", offsetof(CPUState, gpr[28]) },
1595 { "r29", offsetof(CPUState, gpr[29]) },
1596 { "r30", offsetof(CPUState, gpr[30]) },
1597 { "r31", offsetof(CPUState, gpr[31]) },
1598 /* Floating point registers */
1599 { "f0", offsetof(CPUState, fpr[0]) },
1600 { "f1", offsetof(CPUState, fpr[1]) },
1601 { "f2", offsetof(CPUState, fpr[2]) },
1602 { "f3", offsetof(CPUState, fpr[3]) },
1603 { "f4", offsetof(CPUState, fpr[4]) },
1604 { "f5", offsetof(CPUState, fpr[5]) },
1605 { "f6", offsetof(CPUState, fpr[6]) },
1606 { "f7", offsetof(CPUState, fpr[7]) },
1607 { "f8", offsetof(CPUState, fpr[8]) },
1608 { "f9", offsetof(CPUState, fpr[9]) },
1609 { "f10", offsetof(CPUState, fpr[10]) },
1610 { "f11", offsetof(CPUState, fpr[11]) },
1611 { "f12", offsetof(CPUState, fpr[12]) },
1612 { "f13", offsetof(CPUState, fpr[13]) },
1613 { "f14", offsetof(CPUState, fpr[14]) },
1614 { "f15", offsetof(CPUState, fpr[15]) },
1615 { "f16", offsetof(CPUState, fpr[16]) },
1616 { "f17", offsetof(CPUState, fpr[17]) },
1617 { "f18", offsetof(CPUState, fpr[18]) },
1618 { "f19", offsetof(CPUState, fpr[19]) },
1619 { "f20", offsetof(CPUState, fpr[20]) },
1620 { "f21", offsetof(CPUState, fpr[21]) },
1621 { "f22", offsetof(CPUState, fpr[22]) },
1622 { "f23", offsetof(CPUState, fpr[23]) },
1623 { "f24", offsetof(CPUState, fpr[24]) },
1624 { "f25", offsetof(CPUState, fpr[25]) },
1625 { "f26", offsetof(CPUState, fpr[26]) },
1626 { "f27", offsetof(CPUState, fpr[27]) },
1627 { "f28", offsetof(CPUState, fpr[28]) },
1628 { "f29", offsetof(CPUState, fpr[29]) },
1629 { "f30", offsetof(CPUState, fpr[30]) },
1630 { "f31", offsetof(CPUState, fpr[31]) },
1631 { "fpscr", offsetof(CPUState, fpscr) },
1632 /* Next instruction pointer */
1633 { "nip|pc", offsetof(CPUState, nip) },
1634 { "lr", offsetof(CPUState, lr) },
1635 { "ctr", offsetof(CPUState, ctr) },
1636 { "decr", 0, &monitor_get_decr, },
1637 { "ccr", 0, &monitor_get_ccr, },
1638 /* Machine state register */
1639 { "msr", 0, &monitor_get_msr, },
1640 { "xer", 0, &monitor_get_xer, },
1641 { "tbu", 0, &monitor_get_tbu, },
1642 { "tbl", 0, &monitor_get_tbl, },
1643 #if defined(TARGET_PPC64)
1644 /* Address space register */
1645 { "asr", offsetof(CPUState, asr) },
1646 #endif
1647 /* Segment registers */
1648 { "sdr1", offsetof(CPUState, sdr1) },
1649 { "sr0", offsetof(CPUState, sr[0]) },
1650 { "sr1", offsetof(CPUState, sr[1]) },
1651 { "sr2", offsetof(CPUState, sr[2]) },
1652 { "sr3", offsetof(CPUState, sr[3]) },
1653 { "sr4", offsetof(CPUState, sr[4]) },
1654 { "sr5", offsetof(CPUState, sr[5]) },
1655 { "sr6", offsetof(CPUState, sr[6]) },
1656 { "sr7", offsetof(CPUState, sr[7]) },
1657 { "sr8", offsetof(CPUState, sr[8]) },
1658 { "sr9", offsetof(CPUState, sr[9]) },
1659 { "sr10", offsetof(CPUState, sr[10]) },
1660 { "sr11", offsetof(CPUState, sr[11]) },
1661 { "sr12", offsetof(CPUState, sr[12]) },
1662 { "sr13", offsetof(CPUState, sr[13]) },
1663 { "sr14", offsetof(CPUState, sr[14]) },
1664 { "sr15", offsetof(CPUState, sr[15]) },
1665 /* Too lazy to put BATs and SPRs ... */
1666 #elif defined(TARGET_SPARC)
1667 { "g0", offsetof(CPUState, gregs[0]) },
1668 { "g1", offsetof(CPUState, gregs[1]) },
1669 { "g2", offsetof(CPUState, gregs[2]) },
1670 { "g3", offsetof(CPUState, gregs[3]) },
1671 { "g4", offsetof(CPUState, gregs[4]) },
1672 { "g5", offsetof(CPUState, gregs[5]) },
1673 { "g6", offsetof(CPUState, gregs[6]) },
1674 { "g7", offsetof(CPUState, gregs[7]) },
1675 { "o0", 0, monitor_get_reg },
1676 { "o1", 1, monitor_get_reg },
1677 { "o2", 2, monitor_get_reg },
1678 { "o3", 3, monitor_get_reg },
1679 { "o4", 4, monitor_get_reg },
1680 { "o5", 5, monitor_get_reg },
1681 { "o6", 6, monitor_get_reg },
1682 { "o7", 7, monitor_get_reg },
1683 { "l0", 8, monitor_get_reg },
1684 { "l1", 9, monitor_get_reg },
1685 { "l2", 10, monitor_get_reg },
1686 { "l3", 11, monitor_get_reg },
1687 { "l4", 12, monitor_get_reg },
1688 { "l5", 13, monitor_get_reg },
1689 { "l6", 14, monitor_get_reg },
1690 { "l7", 15, monitor_get_reg },
1691 { "i0", 16, monitor_get_reg },
1692 { "i1", 17, monitor_get_reg },
1693 { "i2", 18, monitor_get_reg },
1694 { "i3", 19, monitor_get_reg },
1695 { "i4", 20, monitor_get_reg },
1696 { "i5", 21, monitor_get_reg },
1697 { "i6", 22, monitor_get_reg },
1698 { "i7", 23, monitor_get_reg },
1699 { "pc", offsetof(CPUState, pc) },
1700 { "npc", offsetof(CPUState, npc) },
1701 { "y", offsetof(CPUState, y) },
1702 #ifndef TARGET_SPARC64
1703 { "psr", 0, &monitor_get_psr, },
1704 { "wim", offsetof(CPUState, wim) },
1705 #endif
1706 { "tbr", offsetof(CPUState, tbr) },
1707 { "fsr", offsetof(CPUState, fsr) },
1708 { "f0", offsetof(CPUState, fpr[0]) },
1709 { "f1", offsetof(CPUState, fpr[1]) },
1710 { "f2", offsetof(CPUState, fpr[2]) },
1711 { "f3", offsetof(CPUState, fpr[3]) },
1712 { "f4", offsetof(CPUState, fpr[4]) },
1713 { "f5", offsetof(CPUState, fpr[5]) },
1714 { "f6", offsetof(CPUState, fpr[6]) },
1715 { "f7", offsetof(CPUState, fpr[7]) },
1716 { "f8", offsetof(CPUState, fpr[8]) },
1717 { "f9", offsetof(CPUState, fpr[9]) },
1718 { "f10", offsetof(CPUState, fpr[10]) },
1719 { "f11", offsetof(CPUState, fpr[11]) },
1720 { "f12", offsetof(CPUState, fpr[12]) },
1721 { "f13", offsetof(CPUState, fpr[13]) },
1722 { "f14", offsetof(CPUState, fpr[14]) },
1723 { "f15", offsetof(CPUState, fpr[15]) },
1724 { "f16", offsetof(CPUState, fpr[16]) },
1725 { "f17", offsetof(CPUState, fpr[17]) },
1726 { "f18", offsetof(CPUState, fpr[18]) },
1727 { "f19", offsetof(CPUState, fpr[19]) },
1728 { "f20", offsetof(CPUState, fpr[20]) },
1729 { "f21", offsetof(CPUState, fpr[21]) },
1730 { "f22", offsetof(CPUState, fpr[22]) },
1731 { "f23", offsetof(CPUState, fpr[23]) },
1732 { "f24", offsetof(CPUState, fpr[24]) },
1733 { "f25", offsetof(CPUState, fpr[25]) },
1734 { "f26", offsetof(CPUState, fpr[26]) },
1735 { "f27", offsetof(CPUState, fpr[27]) },
1736 { "f28", offsetof(CPUState, fpr[28]) },
1737 { "f29", offsetof(CPUState, fpr[29]) },
1738 { "f30", offsetof(CPUState, fpr[30]) },
1739 { "f31", offsetof(CPUState, fpr[31]) },
1740 #ifdef TARGET_SPARC64
1741 { "f32", offsetof(CPUState, fpr[32]) },
1742 { "f34", offsetof(CPUState, fpr[34]) },
1743 { "f36", offsetof(CPUState, fpr[36]) },
1744 { "f38", offsetof(CPUState, fpr[38]) },
1745 { "f40", offsetof(CPUState, fpr[40]) },
1746 { "f42", offsetof(CPUState, fpr[42]) },
1747 { "f44", offsetof(CPUState, fpr[44]) },
1748 { "f46", offsetof(CPUState, fpr[46]) },
1749 { "f48", offsetof(CPUState, fpr[48]) },
1750 { "f50", offsetof(CPUState, fpr[50]) },
1751 { "f52", offsetof(CPUState, fpr[52]) },
1752 { "f54", offsetof(CPUState, fpr[54]) },
1753 { "f56", offsetof(CPUState, fpr[56]) },
1754 { "f58", offsetof(CPUState, fpr[58]) },
1755 { "f60", offsetof(CPUState, fpr[60]) },
1756 { "f62", offsetof(CPUState, fpr[62]) },
1757 { "asi", offsetof(CPUState, asi) },
1758 { "pstate", offsetof(CPUState, pstate) },
1759 { "cansave", offsetof(CPUState, cansave) },
1760 { "canrestore", offsetof(CPUState, canrestore) },
1761 { "otherwin", offsetof(CPUState, otherwin) },
1762 { "wstate", offsetof(CPUState, wstate) },
1763 { "cleanwin", offsetof(CPUState, cleanwin) },
1764 { "fprs", offsetof(CPUState, fprs) },
1765 #endif
1766 #endif
1767 { NULL },
1770 static void expr_error(const char *fmt)
1772 term_printf(fmt);
1773 term_printf("\n");
1774 longjmp(expr_env, 1);
1777 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1778 static int get_monitor_def(target_long *pval, const char *name)
1780 MonitorDef *md;
1781 void *ptr;
1783 for(md = monitor_defs; md->name != NULL; md++) {
1784 if (compare_cmd(name, md->name)) {
1785 if (md->get_value) {
1786 *pval = md->get_value(md, md->offset);
1787 } else {
1788 CPUState *env = mon_get_cpu();
1789 if (!env)
1790 return -2;
1791 ptr = (uint8_t *)env + md->offset;
1792 switch(md->type) {
1793 case MD_I32:
1794 *pval = *(int32_t *)ptr;
1795 break;
1796 case MD_TLONG:
1797 *pval = *(target_long *)ptr;
1798 break;
1799 default:
1800 *pval = 0;
1801 break;
1804 return 0;
1807 return -1;
1810 static void next(void)
1812 if (pch != '\0') {
1813 pch++;
1814 while (isspace(*pch))
1815 pch++;
1819 static int64_t expr_sum(void);
1821 static int64_t expr_unary(void)
1823 int64_t n;
1824 char *p;
1825 int ret;
1827 switch(*pch) {
1828 case '+':
1829 next();
1830 n = expr_unary();
1831 break;
1832 case '-':
1833 next();
1834 n = -expr_unary();
1835 break;
1836 case '~':
1837 next();
1838 n = ~expr_unary();
1839 break;
1840 case '(':
1841 next();
1842 n = expr_sum();
1843 if (*pch != ')') {
1844 expr_error("')' expected");
1846 next();
1847 break;
1848 case '\'':
1849 pch++;
1850 if (*pch == '\0')
1851 expr_error("character constant expected");
1852 n = *pch;
1853 pch++;
1854 if (*pch != '\'')
1855 expr_error("missing terminating \' character");
1856 next();
1857 break;
1858 case '$':
1860 char buf[128], *q;
1861 target_long reg=0;
1863 pch++;
1864 q = buf;
1865 while ((*pch >= 'a' && *pch <= 'z') ||
1866 (*pch >= 'A' && *pch <= 'Z') ||
1867 (*pch >= '0' && *pch <= '9') ||
1868 *pch == '_' || *pch == '.') {
1869 if ((q - buf) < sizeof(buf) - 1)
1870 *q++ = *pch;
1871 pch++;
1873 while (isspace(*pch))
1874 pch++;
1875 *q = 0;
1876 ret = get_monitor_def(&reg, buf);
1877 if (ret == -1)
1878 expr_error("unknown register");
1879 else if (ret == -2)
1880 expr_error("no cpu defined");
1881 n = reg;
1883 break;
1884 case '\0':
1885 expr_error("unexpected end of expression");
1886 n = 0;
1887 break;
1888 default:
1889 #if TARGET_PHYS_ADDR_BITS > 32
1890 n = strtoull(pch, &p, 0);
1891 #else
1892 n = strtoul(pch, &p, 0);
1893 #endif
1894 if (pch == p) {
1895 expr_error("invalid char in expression");
1897 pch = p;
1898 while (isspace(*pch))
1899 pch++;
1900 break;
1902 return n;
1906 static int64_t expr_prod(void)
1908 int64_t val, val2;
1909 int op;
1911 val = expr_unary();
1912 for(;;) {
1913 op = *pch;
1914 if (op != '*' && op != '/' && op != '%')
1915 break;
1916 next();
1917 val2 = expr_unary();
1918 switch(op) {
1919 default:
1920 case '*':
1921 val *= val2;
1922 break;
1923 case '/':
1924 case '%':
1925 if (val2 == 0)
1926 expr_error("division by zero");
1927 if (op == '/')
1928 val /= val2;
1929 else
1930 val %= val2;
1931 break;
1934 return val;
1937 static int64_t expr_logic(void)
1939 int64_t val, val2;
1940 int op;
1942 val = expr_prod();
1943 for(;;) {
1944 op = *pch;
1945 if (op != '&' && op != '|' && op != '^')
1946 break;
1947 next();
1948 val2 = expr_prod();
1949 switch(op) {
1950 default:
1951 case '&':
1952 val &= val2;
1953 break;
1954 case '|':
1955 val |= val2;
1956 break;
1957 case '^':
1958 val ^= val2;
1959 break;
1962 return val;
1965 static int64_t expr_sum(void)
1967 int64_t val, val2;
1968 int op;
1970 val = expr_logic();
1971 for(;;) {
1972 op = *pch;
1973 if (op != '+' && op != '-')
1974 break;
1975 next();
1976 val2 = expr_logic();
1977 if (op == '+')
1978 val += val2;
1979 else
1980 val -= val2;
1982 return val;
1985 static int get_expr(int64_t *pval, const char **pp)
1987 pch = *pp;
1988 if (setjmp(expr_env)) {
1989 *pp = pch;
1990 return -1;
1992 while (isspace(*pch))
1993 pch++;
1994 *pval = expr_sum();
1995 *pp = pch;
1996 return 0;
1999 static int get_str(char *buf, int buf_size, const char **pp)
2001 const char *p;
2002 char *q;
2003 int c;
2005 q = buf;
2006 p = *pp;
2007 while (isspace(*p))
2008 p++;
2009 if (*p == '\0') {
2010 fail:
2011 *q = '\0';
2012 *pp = p;
2013 return -1;
2015 if (*p == '\"') {
2016 p++;
2017 while (*p != '\0' && *p != '\"') {
2018 if (*p == '\\') {
2019 p++;
2020 c = *p++;
2021 switch(c) {
2022 case 'n':
2023 c = '\n';
2024 break;
2025 case 'r':
2026 c = '\r';
2027 break;
2028 case '\\':
2029 case '\'':
2030 case '\"':
2031 break;
2032 default:
2033 qemu_printf("unsupported escape code: '\\%c'\n", c);
2034 goto fail;
2036 if ((q - buf) < buf_size - 1) {
2037 *q++ = c;
2039 } else {
2040 if ((q - buf) < buf_size - 1) {
2041 *q++ = *p;
2043 p++;
2046 if (*p != '\"') {
2047 qemu_printf("unterminated string\n");
2048 goto fail;
2050 p++;
2051 } else {
2052 while (*p != '\0' && !isspace(*p)) {
2053 if ((q - buf) < buf_size - 1) {
2054 *q++ = *p;
2056 p++;
2059 *q = '\0';
2060 *pp = p;
2061 return 0;
2064 static int default_fmt_format = 'x';
2065 static int default_fmt_size = 4;
2067 #define MAX_ARGS 16
2069 static void monitor_handle_command(const char *cmdline)
2071 const char *p, *pstart, *typestr;
2072 char *q;
2073 int c, nb_args, len, i, has_arg;
2074 term_cmd_t *cmd;
2075 char cmdname[256];
2076 char buf[1024];
2077 void *str_allocated[MAX_ARGS];
2078 void *args[MAX_ARGS];
2080 #ifdef DEBUG
2081 term_printf("command='%s'\n", cmdline);
2082 #endif
2084 /* extract the command name */
2085 p = cmdline;
2086 q = cmdname;
2087 while (isspace(*p))
2088 p++;
2089 if (*p == '\0')
2090 return;
2091 pstart = p;
2092 while (*p != '\0' && *p != '/' && !isspace(*p))
2093 p++;
2094 len = p - pstart;
2095 if (len > sizeof(cmdname) - 1)
2096 len = sizeof(cmdname) - 1;
2097 memcpy(cmdname, pstart, len);
2098 cmdname[len] = '\0';
2100 /* find the command */
2101 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2102 if (compare_cmd(cmdname, cmd->name))
2103 goto found;
2105 term_printf("unknown command: '%s'\n", cmdname);
2106 return;
2107 found:
2109 for(i = 0; i < MAX_ARGS; i++)
2110 str_allocated[i] = NULL;
2112 /* parse the parameters */
2113 typestr = cmd->args_type;
2114 nb_args = 0;
2115 for(;;) {
2116 c = *typestr;
2117 if (c == '\0')
2118 break;
2119 typestr++;
2120 switch(c) {
2121 case 'F':
2122 case 'B':
2123 case 's':
2125 int ret;
2126 char *str;
2128 while (isspace(*p))
2129 p++;
2130 if (*typestr == '?') {
2131 typestr++;
2132 if (*p == '\0') {
2133 /* no optional string: NULL argument */
2134 str = NULL;
2135 goto add_str;
2138 ret = get_str(buf, sizeof(buf), &p);
2139 if (ret < 0) {
2140 switch(c) {
2141 case 'F':
2142 term_printf("%s: filename expected\n", cmdname);
2143 break;
2144 case 'B':
2145 term_printf("%s: block device name expected\n", cmdname);
2146 break;
2147 default:
2148 term_printf("%s: string expected\n", cmdname);
2149 break;
2151 goto fail;
2153 str = qemu_malloc(strlen(buf) + 1);
2154 strcpy(str, buf);
2155 str_allocated[nb_args] = str;
2156 add_str:
2157 if (nb_args >= MAX_ARGS) {
2158 error_args:
2159 term_printf("%s: too many arguments\n", cmdname);
2160 goto fail;
2162 args[nb_args++] = str;
2164 break;
2165 case '/':
2167 int count, format, size;
2169 while (isspace(*p))
2170 p++;
2171 if (*p == '/') {
2172 /* format found */
2173 p++;
2174 count = 1;
2175 if (isdigit(*p)) {
2176 count = 0;
2177 while (isdigit(*p)) {
2178 count = count * 10 + (*p - '0');
2179 p++;
2182 size = -1;
2183 format = -1;
2184 for(;;) {
2185 switch(*p) {
2186 case 'o':
2187 case 'd':
2188 case 'u':
2189 case 'x':
2190 case 'i':
2191 case 'c':
2192 format = *p++;
2193 break;
2194 case 'b':
2195 size = 1;
2196 p++;
2197 break;
2198 case 'h':
2199 size = 2;
2200 p++;
2201 break;
2202 case 'w':
2203 size = 4;
2204 p++;
2205 break;
2206 case 'g':
2207 case 'L':
2208 size = 8;
2209 p++;
2210 break;
2211 default:
2212 goto next;
2215 next:
2216 if (*p != '\0' && !isspace(*p)) {
2217 term_printf("invalid char in format: '%c'\n", *p);
2218 goto fail;
2220 if (format < 0)
2221 format = default_fmt_format;
2222 if (format != 'i') {
2223 /* for 'i', not specifying a size gives -1 as size */
2224 if (size < 0)
2225 size = default_fmt_size;
2227 default_fmt_size = size;
2228 default_fmt_format = format;
2229 } else {
2230 count = 1;
2231 format = default_fmt_format;
2232 if (format != 'i') {
2233 size = default_fmt_size;
2234 } else {
2235 size = -1;
2238 if (nb_args + 3 > MAX_ARGS)
2239 goto error_args;
2240 args[nb_args++] = (void*)(long)count;
2241 args[nb_args++] = (void*)(long)format;
2242 args[nb_args++] = (void*)(long)size;
2244 break;
2245 case 'i':
2246 case 'l':
2248 int64_t val;
2250 while (isspace(*p))
2251 p++;
2252 if (*typestr == '?' || *typestr == '.') {
2253 if (*typestr == '?') {
2254 if (*p == '\0')
2255 has_arg = 0;
2256 else
2257 has_arg = 1;
2258 } else {
2259 if (*p == '.') {
2260 p++;
2261 while (isspace(*p))
2262 p++;
2263 has_arg = 1;
2264 } else {
2265 has_arg = 0;
2268 typestr++;
2269 if (nb_args >= MAX_ARGS)
2270 goto error_args;
2271 args[nb_args++] = (void *)(long)has_arg;
2272 if (!has_arg) {
2273 if (nb_args >= MAX_ARGS)
2274 goto error_args;
2275 val = -1;
2276 goto add_num;
2279 if (get_expr(&val, &p))
2280 goto fail;
2281 add_num:
2282 if (c == 'i') {
2283 if (nb_args >= MAX_ARGS)
2284 goto error_args;
2285 args[nb_args++] = (void *)(long)val;
2286 } else {
2287 if ((nb_args + 1) >= MAX_ARGS)
2288 goto error_args;
2289 #if TARGET_PHYS_ADDR_BITS > 32
2290 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2291 #else
2292 args[nb_args++] = (void *)0;
2293 #endif
2294 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2297 break;
2298 case '-':
2300 int has_option;
2301 /* option */
2303 c = *typestr++;
2304 if (c == '\0')
2305 goto bad_type;
2306 while (isspace(*p))
2307 p++;
2308 has_option = 0;
2309 if (*p == '-') {
2310 p++;
2311 if (*p != c) {
2312 term_printf("%s: unsupported option -%c\n",
2313 cmdname, *p);
2314 goto fail;
2316 p++;
2317 has_option = 1;
2319 if (nb_args >= MAX_ARGS)
2320 goto error_args;
2321 args[nb_args++] = (void *)(long)has_option;
2323 break;
2324 default:
2325 bad_type:
2326 term_printf("%s: unknown type '%c'\n", cmdname, c);
2327 goto fail;
2330 /* check that all arguments were parsed */
2331 while (isspace(*p))
2332 p++;
2333 if (*p != '\0') {
2334 term_printf("%s: extraneous characters at the end of line\n",
2335 cmdname);
2336 goto fail;
2339 switch(nb_args) {
2340 case 0:
2341 cmd->handler();
2342 break;
2343 case 1:
2344 cmd->handler(args[0]);
2345 break;
2346 case 2:
2347 cmd->handler(args[0], args[1]);
2348 break;
2349 case 3:
2350 cmd->handler(args[0], args[1], args[2]);
2351 break;
2352 case 4:
2353 cmd->handler(args[0], args[1], args[2], args[3]);
2354 break;
2355 case 5:
2356 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2357 break;
2358 case 6:
2359 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2360 break;
2361 case 7:
2362 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2363 break;
2364 default:
2365 term_printf("unsupported number of arguments: %d\n", nb_args);
2366 goto fail;
2368 fail:
2369 for(i = 0; i < MAX_ARGS; i++)
2370 qemu_free(str_allocated[i]);
2371 return;
2374 static void cmd_completion(const char *name, const char *list)
2376 const char *p, *pstart;
2377 char cmd[128];
2378 int len;
2380 p = list;
2381 for(;;) {
2382 pstart = p;
2383 p = strchr(p, '|');
2384 if (!p)
2385 p = pstart + strlen(pstart);
2386 len = p - pstart;
2387 if (len > sizeof(cmd) - 2)
2388 len = sizeof(cmd) - 2;
2389 memcpy(cmd, pstart, len);
2390 cmd[len] = '\0';
2391 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2392 add_completion(cmd);
2394 if (*p == '\0')
2395 break;
2396 p++;
2400 static void file_completion(const char *input)
2402 DIR *ffs;
2403 struct dirent *d;
2404 char path[1024];
2405 char file[1024], file_prefix[1024];
2406 int input_path_len;
2407 const char *p;
2409 p = strrchr(input, '/');
2410 if (!p) {
2411 input_path_len = 0;
2412 pstrcpy(file_prefix, sizeof(file_prefix), input);
2413 strcpy(path, ".");
2414 } else {
2415 input_path_len = p - input + 1;
2416 memcpy(path, input, input_path_len);
2417 if (input_path_len > sizeof(path) - 1)
2418 input_path_len = sizeof(path) - 1;
2419 path[input_path_len] = '\0';
2420 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2422 #ifdef DEBUG_COMPLETION
2423 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2424 #endif
2425 ffs = opendir(path);
2426 if (!ffs)
2427 return;
2428 for(;;) {
2429 struct stat sb;
2430 d = readdir(ffs);
2431 if (!d)
2432 break;
2433 if (strstart(d->d_name, file_prefix, NULL)) {
2434 memcpy(file, input, input_path_len);
2435 strcpy(file + input_path_len, d->d_name);
2436 /* stat the file to find out if it's a directory.
2437 * In that case add a slash to speed up typing long paths
2439 stat(file, &sb);
2440 if(S_ISDIR(sb.st_mode))
2441 strcat(file, "/");
2442 add_completion(file);
2445 closedir(ffs);
2448 static void block_completion_it(void *opaque, const char *name)
2450 const char *input = opaque;
2452 if (input[0] == '\0' ||
2453 !strncmp(name, (char *)input, strlen(input))) {
2454 add_completion(name);
2458 /* NOTE: this parser is an approximate form of the real command parser */
2459 static void parse_cmdline(const char *cmdline,
2460 int *pnb_args, char **args)
2462 const char *p;
2463 int nb_args, ret;
2464 char buf[1024];
2466 p = cmdline;
2467 nb_args = 0;
2468 for(;;) {
2469 while (isspace(*p))
2470 p++;
2471 if (*p == '\0')
2472 break;
2473 if (nb_args >= MAX_ARGS)
2474 break;
2475 ret = get_str(buf, sizeof(buf), &p);
2476 args[nb_args] = qemu_strdup(buf);
2477 nb_args++;
2478 if (ret < 0)
2479 break;
2481 *pnb_args = nb_args;
2484 void readline_find_completion(const char *cmdline)
2486 const char *cmdname;
2487 char *args[MAX_ARGS];
2488 int nb_args, i, len;
2489 const char *ptype, *str;
2490 term_cmd_t *cmd;
2491 const KeyDef *key;
2493 parse_cmdline(cmdline, &nb_args, args);
2494 #ifdef DEBUG_COMPLETION
2495 for(i = 0; i < nb_args; i++) {
2496 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2498 #endif
2500 /* if the line ends with a space, it means we want to complete the
2501 next arg */
2502 len = strlen(cmdline);
2503 if (len > 0 && isspace(cmdline[len - 1])) {
2504 if (nb_args >= MAX_ARGS)
2505 return;
2506 args[nb_args++] = qemu_strdup("");
2508 if (nb_args <= 1) {
2509 /* command completion */
2510 if (nb_args == 0)
2511 cmdname = "";
2512 else
2513 cmdname = args[0];
2514 completion_index = strlen(cmdname);
2515 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2516 cmd_completion(cmdname, cmd->name);
2518 } else {
2519 /* find the command */
2520 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2521 if (compare_cmd(args[0], cmd->name))
2522 goto found;
2524 return;
2525 found:
2526 ptype = cmd->args_type;
2527 for(i = 0; i < nb_args - 2; i++) {
2528 if (*ptype != '\0') {
2529 ptype++;
2530 while (*ptype == '?')
2531 ptype++;
2534 str = args[nb_args - 1];
2535 switch(*ptype) {
2536 case 'F':
2537 /* file completion */
2538 completion_index = strlen(str);
2539 file_completion(str);
2540 break;
2541 case 'B':
2542 /* block device name completion */
2543 completion_index = strlen(str);
2544 bdrv_iterate(block_completion_it, (void *)str);
2545 break;
2546 case 's':
2547 /* XXX: more generic ? */
2548 if (!strcmp(cmd->name, "info")) {
2549 completion_index = strlen(str);
2550 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2551 cmd_completion(str, cmd->name);
2553 } else if (!strcmp(cmd->name, "sendkey")) {
2554 completion_index = strlen(str);
2555 for(key = key_defs; key->name != NULL; key++) {
2556 cmd_completion(str, key->name);
2559 break;
2560 default:
2561 break;
2564 for(i = 0; i < nb_args; i++)
2565 qemu_free(args[i]);
2568 static int term_can_read(void *opaque)
2570 return 128;
2573 static void term_read(void *opaque, const uint8_t *buf, int size)
2575 int i;
2576 for(i = 0; i < size; i++)
2577 readline_handle_byte(buf[i]);
2580 static int monitor_suspended;
2582 void monitor_suspend(void)
2584 monitor_suspended = 1;
2587 void monitor_resume(void)
2589 monitor_suspended = 0;
2590 monitor_start_input();
2593 static void monitor_start_input(void);
2595 static void monitor_handle_command1(void *opaque, const char *cmdline)
2597 monitor_handle_command(cmdline);
2598 if (!monitor_suspended)
2599 monitor_start_input();
2602 static void monitor_start_input(void)
2604 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2607 static void term_event(void *opaque, int event)
2609 if (event != CHR_EVENT_RESET)
2610 return;
2612 if (!hide_banner)
2613 term_printf("QEMU %s monitor - type 'help' for more information\n",
2614 QEMU_VERSION);
2615 monitor_start_input();
2618 static int is_first_init = 1;
2620 void monitor_init(CharDriverState *hd, int show_banner)
2622 int i;
2624 if (is_first_init) {
2625 for (i = 0; i < MAX_MON; i++) {
2626 monitor_hd[i] = NULL;
2628 is_first_init = 0;
2630 for (i = 0; i < MAX_MON; i++) {
2631 if (monitor_hd[i] == NULL) {
2632 monitor_hd[i] = hd;
2633 break;
2637 hide_banner = !show_banner;
2639 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2641 readline_start("", 0, monitor_handle_command1, NULL);
2644 /* XXX: use threads ? */
2645 /* modal monitor readline */
2646 static int monitor_readline_started;
2647 static char *monitor_readline_buf;
2648 static int monitor_readline_buf_size;
2650 static void monitor_readline_cb(void *opaque, const char *input)
2652 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2653 monitor_readline_started = 0;
2656 void monitor_readline(const char *prompt, int is_password,
2657 char *buf, int buf_size)
2659 int i;
2661 if (is_password) {
2662 for (i = 0; i < MAX_MON; i++)
2663 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2664 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2666 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2667 monitor_readline_buf = buf;
2668 monitor_readline_buf_size = buf_size;
2669 monitor_readline_started = 1;
2670 while (monitor_readline_started) {
2671 main_loop_wait(10);