kvm: vgabios: include <string.h> in biossums.c for memset declaration
[qemu-kvm/fedora.git] / monitor.c
blobe03c473096c066051b89f78d1885d485cfddfe75
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 #ifdef USE_KVM
290 kvm_save_registers(mon_cpu);
291 #endif
293 return mon_cpu;
296 static void do_info_registers(void)
298 CPUState *env;
299 env = mon_get_cpu();
300 if (!env)
301 return;
302 #ifdef TARGET_I386
303 cpu_dump_state(env, NULL, monitor_fprintf,
304 X86_DUMP_FPU);
305 #else
306 cpu_dump_state(env, NULL, monitor_fprintf,
308 #endif
311 static void do_info_cpus(void)
313 CPUState *env;
315 /* just to set the default cpu if not already done */
316 mon_get_cpu();
318 for(env = first_cpu; env != NULL; env = env->next_cpu) {
319 term_printf("%c CPU #%d:",
320 (env == mon_cpu) ? '*' : ' ',
321 env->cpu_index);
322 #if defined(TARGET_I386)
323 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
324 if (env->hflags & HF_HALTED_MASK)
325 term_printf(" (halted)");
326 #elif defined(TARGET_PPC)
327 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
328 if (env->halted)
329 term_printf(" (halted)");
330 #elif defined(TARGET_SPARC)
331 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
332 if (env->halted)
333 term_printf(" (halted)");
334 #elif defined(TARGET_MIPS)
335 term_printf(" PC=0x" TARGET_FMT_lx, env->PC[env->current_tc]);
336 if (env->halted)
337 term_printf(" (halted)");
338 #endif
339 term_printf("\n");
343 static void do_cpu_set(int index)
345 if (mon_set_cpu(index) < 0)
346 term_printf("Invalid CPU index\n");
349 static void do_info_jit(void)
351 dump_exec_info(NULL, monitor_fprintf);
354 static void do_info_history (void)
356 int i;
357 const char *str;
359 i = 0;
360 for(;;) {
361 str = readline_get_history(i);
362 if (!str)
363 break;
364 term_printf("%d: '%s'\n", i, str);
365 i++;
369 #if defined(TARGET_PPC)
370 /* XXX: not implemented in other targets */
371 static void do_info_cpu_stats (void)
373 CPUState *env;
375 env = mon_get_cpu();
376 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
378 #endif
380 static void do_quit(void)
382 exit(0);
385 static int eject_device(BlockDriverState *bs, int force)
387 if (bdrv_is_inserted(bs)) {
388 if (!force) {
389 if (!bdrv_is_removable(bs)) {
390 term_printf("device is not removable\n");
391 return -1;
393 if (bdrv_is_locked(bs)) {
394 term_printf("device is locked\n");
395 return -1;
398 bdrv_close(bs);
400 return 0;
403 static void do_eject(int force, const char *filename)
405 BlockDriverState *bs;
407 bs = bdrv_find(filename);
408 if (!bs) {
409 term_printf("device not found\n");
410 return;
412 eject_device(bs, force);
415 static void do_change_block(const char *device, const char *filename)
417 BlockDriverState *bs;
419 bs = bdrv_find(device);
420 if (!bs) {
421 term_printf("device not found\n");
422 return;
424 if (eject_device(bs, 0) < 0)
425 return;
426 bdrv_open(bs, filename, 0);
427 qemu_key_check(bs, filename);
430 static void do_change_vnc(const char *target)
432 if (strcmp(target, "passwd") == 0 ||
433 strcmp(target, "password") == 0) {
434 char password[9];
435 monitor_readline("Password: ", 1, password, sizeof(password)-1);
436 password[sizeof(password)-1] = '\0';
437 if (vnc_display_password(NULL, password) < 0)
438 term_printf("could not set VNC server password\n");
439 } else {
440 if (vnc_display_open(NULL, target) < 0)
441 term_printf("could not start VNC server on %s\n", target);
445 static void do_change(const char *device, const char *target)
447 if (strcmp(device, "vnc") == 0) {
448 do_change_vnc(target);
449 } else {
450 do_change_block(device, target);
454 static void do_screen_dump(const char *filename)
456 vga_hw_screen_dump(filename);
459 static void do_logfile(const char *filename)
461 cpu_set_log_filename(filename);
464 static void do_log(const char *items)
466 int mask;
468 if (!strcmp(items, "none")) {
469 mask = 0;
470 } else {
471 mask = cpu_str_to_log_mask(items);
472 if (!mask) {
473 help_cmd("log");
474 return;
477 cpu_set_log(mask);
480 static void do_stop(void)
482 vm_stop(EXCP_INTERRUPT);
485 static void do_cont(void)
487 vm_start();
490 #ifdef CONFIG_GDBSTUB
491 static void do_gdbserver(const char *port)
493 if (!port)
494 port = DEFAULT_GDBSTUB_PORT;
495 if (gdbserver_start(port) < 0) {
496 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
497 } else {
498 qemu_printf("Waiting gdb connection on port '%s'\n", port);
501 #endif
503 static void term_printc(int c)
505 term_printf("'");
506 switch(c) {
507 case '\'':
508 term_printf("\\'");
509 break;
510 case '\\':
511 term_printf("\\\\");
512 break;
513 case '\n':
514 term_printf("\\n");
515 break;
516 case '\r':
517 term_printf("\\r");
518 break;
519 default:
520 if (c >= 32 && c <= 126) {
521 term_printf("%c", c);
522 } else {
523 term_printf("\\x%02x", c);
525 break;
527 term_printf("'");
530 static void memory_dump(int count, int format, int wsize,
531 target_phys_addr_t addr, int is_physical)
533 CPUState *env;
534 int nb_per_line, l, line_size, i, max_digits, len;
535 uint8_t buf[16];
536 uint64_t v;
538 if (format == 'i') {
539 int flags;
540 flags = 0;
541 env = mon_get_cpu();
542 if (!env && !is_physical)
543 return;
544 #ifdef TARGET_I386
545 if (wsize == 2) {
546 flags = 1;
547 } else if (wsize == 4) {
548 flags = 0;
549 } else {
550 /* as default we use the current CS size */
551 flags = 0;
552 if (env) {
553 #ifdef TARGET_X86_64
554 if ((env->efer & MSR_EFER_LMA) &&
555 (env->segs[R_CS].flags & DESC_L_MASK))
556 flags = 2;
557 else
558 #endif
559 if (!(env->segs[R_CS].flags & DESC_B_MASK))
560 flags = 1;
563 #endif
564 monitor_disas(env, addr, count, is_physical, flags);
565 return;
568 len = wsize * count;
569 if (wsize == 1)
570 line_size = 8;
571 else
572 line_size = 16;
573 nb_per_line = line_size / wsize;
574 max_digits = 0;
576 switch(format) {
577 case 'o':
578 max_digits = (wsize * 8 + 2) / 3;
579 break;
580 default:
581 case 'x':
582 max_digits = (wsize * 8) / 4;
583 break;
584 case 'u':
585 case 'd':
586 max_digits = (wsize * 8 * 10 + 32) / 33;
587 break;
588 case 'c':
589 wsize = 1;
590 break;
593 while (len > 0) {
594 if (is_physical)
595 term_printf(TARGET_FMT_plx ":", addr);
596 else
597 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
598 l = len;
599 if (l > line_size)
600 l = line_size;
601 if (is_physical) {
602 cpu_physical_memory_rw(addr, buf, l, 0);
603 } else {
604 env = mon_get_cpu();
605 if (!env)
606 break;
607 cpu_memory_rw_debug(env, addr, buf, l, 0);
609 i = 0;
610 while (i < l) {
611 switch(wsize) {
612 default:
613 case 1:
614 v = ldub_raw(buf + i);
615 break;
616 case 2:
617 v = lduw_raw(buf + i);
618 break;
619 case 4:
620 v = (uint32_t)ldl_raw(buf + i);
621 break;
622 case 8:
623 v = ldq_raw(buf + i);
624 break;
626 term_printf(" ");
627 switch(format) {
628 case 'o':
629 term_printf("%#*" PRIo64, max_digits, v);
630 break;
631 case 'x':
632 term_printf("0x%0*" PRIx64, max_digits, v);
633 break;
634 case 'u':
635 term_printf("%*" PRIu64, max_digits, v);
636 break;
637 case 'd':
638 term_printf("%*" PRId64, max_digits, v);
639 break;
640 case 'c':
641 term_printc(v);
642 break;
644 i += wsize;
646 term_printf("\n");
647 addr += l;
648 len -= l;
652 #if TARGET_LONG_BITS == 64
653 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
654 #else
655 #define GET_TLONG(h, l) (l)
656 #endif
658 static void do_memory_dump(int count, int format, int size,
659 uint32_t addrh, uint32_t addrl)
661 target_long addr = GET_TLONG(addrh, addrl);
662 memory_dump(count, format, size, addr, 0);
665 #if TARGET_PHYS_ADDR_BITS > 32
666 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
667 #else
668 #define GET_TPHYSADDR(h, l) (l)
669 #endif
671 static void do_physical_memory_dump(int count, int format, int size,
672 uint32_t addrh, uint32_t addrl)
675 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
676 memory_dump(count, format, size, addr, 1);
679 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
681 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
682 #if TARGET_PHYS_ADDR_BITS == 32
683 switch(format) {
684 case 'o':
685 term_printf("%#o", val);
686 break;
687 case 'x':
688 term_printf("%#x", val);
689 break;
690 case 'u':
691 term_printf("%u", val);
692 break;
693 default:
694 case 'd':
695 term_printf("%d", val);
696 break;
697 case 'c':
698 term_printc(val);
699 break;
701 #else
702 switch(format) {
703 case 'o':
704 term_printf("%#" PRIo64, val);
705 break;
706 case 'x':
707 term_printf("%#" PRIx64, val);
708 break;
709 case 'u':
710 term_printf("%" PRIu64, val);
711 break;
712 default:
713 case 'd':
714 term_printf("%" PRId64, val);
715 break;
716 case 'c':
717 term_printc(val);
718 break;
720 #endif
721 term_printf("\n");
724 static void do_memory_save(unsigned int valh, unsigned int vall,
725 uint32_t size, const char *filename)
727 FILE *f;
728 target_long addr = GET_TLONG(valh, vall);
729 uint32_t l;
730 CPUState *env;
731 uint8_t buf[1024];
733 env = mon_get_cpu();
734 if (!env)
735 return;
737 f = fopen(filename, "wb");
738 if (!f) {
739 term_printf("could not open '%s'\n", filename);
740 return;
742 while (size != 0) {
743 l = sizeof(buf);
744 if (l > size)
745 l = size;
746 cpu_memory_rw_debug(env, addr, buf, l, 0);
747 fwrite(buf, 1, l, f);
748 addr += l;
749 size -= l;
751 fclose(f);
754 static void do_sum(uint32_t start, uint32_t size)
756 uint32_t addr;
757 uint8_t buf[1];
758 uint16_t sum;
760 sum = 0;
761 for(addr = start; addr < (start + size); addr++) {
762 cpu_physical_memory_rw(addr, buf, 1, 0);
763 /* BSD sum algorithm ('sum' Unix command) */
764 sum = (sum >> 1) | (sum << 15);
765 sum += buf[0];
767 term_printf("%05d\n", sum);
770 typedef struct {
771 int keycode;
772 const char *name;
773 } KeyDef;
775 static const KeyDef key_defs[] = {
776 { 0x2a, "shift" },
777 { 0x36, "shift_r" },
779 { 0x38, "alt" },
780 { 0xb8, "alt_r" },
781 { 0x1d, "ctrl" },
782 { 0x9d, "ctrl_r" },
784 { 0xdd, "menu" },
786 { 0x01, "esc" },
788 { 0x02, "1" },
789 { 0x03, "2" },
790 { 0x04, "3" },
791 { 0x05, "4" },
792 { 0x06, "5" },
793 { 0x07, "6" },
794 { 0x08, "7" },
795 { 0x09, "8" },
796 { 0x0a, "9" },
797 { 0x0b, "0" },
798 { 0x0c, "minus" },
799 { 0x0d, "equal" },
800 { 0x0e, "backspace" },
802 { 0x0f, "tab" },
803 { 0x10, "q" },
804 { 0x11, "w" },
805 { 0x12, "e" },
806 { 0x13, "r" },
807 { 0x14, "t" },
808 { 0x15, "y" },
809 { 0x16, "u" },
810 { 0x17, "i" },
811 { 0x18, "o" },
812 { 0x19, "p" },
814 { 0x1c, "ret" },
816 { 0x1e, "a" },
817 { 0x1f, "s" },
818 { 0x20, "d" },
819 { 0x21, "f" },
820 { 0x22, "g" },
821 { 0x23, "h" },
822 { 0x24, "j" },
823 { 0x25, "k" },
824 { 0x26, "l" },
826 { 0x2c, "z" },
827 { 0x2d, "x" },
828 { 0x2e, "c" },
829 { 0x2f, "v" },
830 { 0x30, "b" },
831 { 0x31, "n" },
832 { 0x32, "m" },
834 { 0x39, "spc" },
835 { 0x3a, "caps_lock" },
836 { 0x3b, "f1" },
837 { 0x3c, "f2" },
838 { 0x3d, "f3" },
839 { 0x3e, "f4" },
840 { 0x3f, "f5" },
841 { 0x40, "f6" },
842 { 0x41, "f7" },
843 { 0x42, "f8" },
844 { 0x43, "f9" },
845 { 0x44, "f10" },
846 { 0x45, "num_lock" },
847 { 0x46, "scroll_lock" },
849 { 0xb5, "kp_divide" },
850 { 0x37, "kp_multiply" },
851 { 0x4a, "kp_subtract" },
852 { 0x4e, "kp_add" },
853 { 0x9c, "kp_enter" },
854 { 0x53, "kp_decimal" },
856 { 0x52, "kp_0" },
857 { 0x4f, "kp_1" },
858 { 0x50, "kp_2" },
859 { 0x51, "kp_3" },
860 { 0x4b, "kp_4" },
861 { 0x4c, "kp_5" },
862 { 0x4d, "kp_6" },
863 { 0x47, "kp_7" },
864 { 0x48, "kp_8" },
865 { 0x49, "kp_9" },
867 { 0x56, "<" },
869 { 0x57, "f11" },
870 { 0x58, "f12" },
872 { 0xb7, "print" },
874 { 0xc7, "home" },
875 { 0xc9, "pgup" },
876 { 0xd1, "pgdn" },
877 { 0xcf, "end" },
879 { 0xcb, "left" },
880 { 0xc8, "up" },
881 { 0xd0, "down" },
882 { 0xcd, "right" },
884 { 0xd2, "insert" },
885 { 0xd3, "delete" },
886 { 0, NULL },
889 static int get_keycode(const char *key)
891 const KeyDef *p;
892 char *endp;
893 int ret;
895 for(p = key_defs; p->name != NULL; p++) {
896 if (!strcmp(key, p->name))
897 return p->keycode;
899 if (strstart(key, "0x", NULL)) {
900 ret = strtoul(key, &endp, 0);
901 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
902 return ret;
904 return -1;
907 static void do_send_key(const char *string)
909 char keybuf[16], *q;
910 uint8_t keycodes[16];
911 const char *p;
912 int nb_keycodes, keycode, i;
914 nb_keycodes = 0;
915 p = string;
916 while (*p != '\0') {
917 q = keybuf;
918 while (*p != '\0' && *p != '-') {
919 if ((q - keybuf) < sizeof(keybuf) - 1) {
920 *q++ = *p;
922 p++;
924 *q = '\0';
925 keycode = get_keycode(keybuf);
926 if (keycode < 0) {
927 term_printf("unknown key: '%s'\n", keybuf);
928 return;
930 keycodes[nb_keycodes++] = keycode;
931 if (*p == '\0')
932 break;
933 p++;
935 /* key down events */
936 for(i = 0; i < nb_keycodes; i++) {
937 keycode = keycodes[i];
938 if (keycode & 0x80)
939 kbd_put_keycode(0xe0);
940 kbd_put_keycode(keycode & 0x7f);
942 /* key up events */
943 for(i = nb_keycodes - 1; i >= 0; i--) {
944 keycode = keycodes[i];
945 if (keycode & 0x80)
946 kbd_put_keycode(0xe0);
947 kbd_put_keycode(keycode | 0x80);
951 static int mouse_button_state;
953 static void do_mouse_move(const char *dx_str, const char *dy_str,
954 const char *dz_str)
956 int dx, dy, dz;
957 dx = strtol(dx_str, NULL, 0);
958 dy = strtol(dy_str, NULL, 0);
959 dz = 0;
960 if (dz_str)
961 dz = strtol(dz_str, NULL, 0);
962 kbd_mouse_event(dx, dy, dz, mouse_button_state);
965 static void do_mouse_button(int button_state)
967 mouse_button_state = button_state;
968 kbd_mouse_event(0, 0, 0, mouse_button_state);
971 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
973 uint32_t val;
974 int suffix;
976 if (has_index) {
977 cpu_outb(NULL, addr & 0xffff, index & 0xff);
978 addr++;
980 addr &= 0xffff;
982 switch(size) {
983 default:
984 case 1:
985 val = cpu_inb(NULL, addr);
986 suffix = 'b';
987 break;
988 case 2:
989 val = cpu_inw(NULL, addr);
990 suffix = 'w';
991 break;
992 case 4:
993 val = cpu_inl(NULL, addr);
994 suffix = 'l';
995 break;
997 term_printf("port%c[0x%04x] = %#0*x\n",
998 suffix, addr, size * 2, val);
1001 static void do_system_reset(void)
1003 qemu_system_reset_request();
1006 static void do_system_powerdown(void)
1008 qemu_system_powerdown_request();
1011 #if defined(TARGET_I386)
1012 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1014 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1015 addr,
1016 pte & mask,
1017 pte & PG_GLOBAL_MASK ? 'G' : '-',
1018 pte & PG_PSE_MASK ? 'P' : '-',
1019 pte & PG_DIRTY_MASK ? 'D' : '-',
1020 pte & PG_ACCESSED_MASK ? 'A' : '-',
1021 pte & PG_PCD_MASK ? 'C' : '-',
1022 pte & PG_PWT_MASK ? 'T' : '-',
1023 pte & PG_USER_MASK ? 'U' : '-',
1024 pte & PG_RW_MASK ? 'W' : '-');
1027 static void tlb_info(void)
1029 CPUState *env;
1030 int l1, l2;
1031 uint32_t pgd, pde, pte;
1033 env = mon_get_cpu();
1034 if (!env)
1035 return;
1037 if (!(env->cr[0] & CR0_PG_MASK)) {
1038 term_printf("PG disabled\n");
1039 return;
1041 pgd = env->cr[3] & ~0xfff;
1042 for(l1 = 0; l1 < 1024; l1++) {
1043 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1044 pde = le32_to_cpu(pde);
1045 if (pde & PG_PRESENT_MASK) {
1046 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1047 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1048 } else {
1049 for(l2 = 0; l2 < 1024; l2++) {
1050 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1051 (uint8_t *)&pte, 4);
1052 pte = le32_to_cpu(pte);
1053 if (pte & PG_PRESENT_MASK) {
1054 print_pte((l1 << 22) + (l2 << 12),
1055 pte & ~PG_PSE_MASK,
1056 ~0xfff);
1064 static void mem_print(uint32_t *pstart, int *plast_prot,
1065 uint32_t end, int prot)
1067 int prot1;
1068 prot1 = *plast_prot;
1069 if (prot != prot1) {
1070 if (*pstart != -1) {
1071 term_printf("%08x-%08x %08x %c%c%c\n",
1072 *pstart, end, end - *pstart,
1073 prot1 & PG_USER_MASK ? 'u' : '-',
1074 'r',
1075 prot1 & PG_RW_MASK ? 'w' : '-');
1077 if (prot != 0)
1078 *pstart = end;
1079 else
1080 *pstart = -1;
1081 *plast_prot = prot;
1085 static void mem_info(void)
1087 CPUState *env;
1088 int l1, l2, prot, last_prot;
1089 uint32_t pgd, pde, pte, start, end;
1091 env = mon_get_cpu();
1092 if (!env)
1093 return;
1095 if (!(env->cr[0] & CR0_PG_MASK)) {
1096 term_printf("PG disabled\n");
1097 return;
1099 pgd = env->cr[3] & ~0xfff;
1100 last_prot = 0;
1101 start = -1;
1102 for(l1 = 0; l1 < 1024; l1++) {
1103 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1104 pde = le32_to_cpu(pde);
1105 end = l1 << 22;
1106 if (pde & PG_PRESENT_MASK) {
1107 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1108 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1109 mem_print(&start, &last_prot, end, prot);
1110 } else {
1111 for(l2 = 0; l2 < 1024; l2++) {
1112 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1113 (uint8_t *)&pte, 4);
1114 pte = le32_to_cpu(pte);
1115 end = (l1 << 22) + (l2 << 12);
1116 if (pte & PG_PRESENT_MASK) {
1117 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1118 } else {
1119 prot = 0;
1121 mem_print(&start, &last_prot, end, prot);
1124 } else {
1125 prot = 0;
1126 mem_print(&start, &last_prot, end, prot);
1130 #endif
1132 static void do_info_kqemu(void)
1134 #ifdef USE_KQEMU
1135 CPUState *env;
1136 int val;
1137 val = 0;
1138 env = mon_get_cpu();
1139 if (!env) {
1140 term_printf("No cpu initialized yet");
1141 return;
1143 val = env->kqemu_enabled;
1144 term_printf("kqemu support: ");
1145 switch(val) {
1146 default:
1147 case 0:
1148 term_printf("disabled\n");
1149 break;
1150 case 1:
1151 term_printf("enabled for user code\n");
1152 break;
1153 case 2:
1154 term_printf("enabled for user and kernel code\n");
1155 break;
1157 #else
1158 term_printf("kqemu support: not compiled\n");
1159 #endif
1162 #ifdef CONFIG_PROFILER
1164 int64_t kqemu_time;
1165 int64_t qemu_time;
1166 int64_t kqemu_exec_count;
1167 int64_t dev_time;
1168 int64_t kqemu_ret_int_count;
1169 int64_t kqemu_ret_excp_count;
1170 int64_t kqemu_ret_intr_count;
1172 static void do_info_profile(void)
1174 int64_t total;
1175 total = qemu_time;
1176 if (total == 0)
1177 total = 1;
1178 term_printf("async time %" PRId64 " (%0.3f)\n",
1179 dev_time, dev_time / (double)ticks_per_sec);
1180 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1181 qemu_time, qemu_time / (double)ticks_per_sec);
1182 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1183 kqemu_time, kqemu_time / (double)ticks_per_sec,
1184 kqemu_time / (double)total * 100.0,
1185 kqemu_exec_count,
1186 kqemu_ret_int_count,
1187 kqemu_ret_excp_count,
1188 kqemu_ret_intr_count);
1189 qemu_time = 0;
1190 kqemu_time = 0;
1191 kqemu_exec_count = 0;
1192 dev_time = 0;
1193 kqemu_ret_int_count = 0;
1194 kqemu_ret_excp_count = 0;
1195 kqemu_ret_intr_count = 0;
1196 #ifdef USE_KQEMU
1197 kqemu_record_dump();
1198 #endif
1200 #else
1201 static void do_info_profile(void)
1203 term_printf("Internal profiler not compiled\n");
1205 #endif
1207 /* Capture support */
1208 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1210 static void do_info_capture (void)
1212 int i;
1213 CaptureState *s;
1215 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1216 term_printf ("[%d]: ", i);
1217 s->ops.info (s->opaque);
1221 static void do_stop_capture (int n)
1223 int i;
1224 CaptureState *s;
1226 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1227 if (i == n) {
1228 s->ops.destroy (s->opaque);
1229 LIST_REMOVE (s, entries);
1230 qemu_free (s);
1231 return;
1236 #ifdef HAS_AUDIO
1237 int wav_start_capture (CaptureState *s, const char *path, int freq,
1238 int bits, int nchannels);
1240 static void do_wav_capture (const char *path,
1241 int has_freq, int freq,
1242 int has_bits, int bits,
1243 int has_channels, int nchannels)
1245 CaptureState *s;
1247 s = qemu_mallocz (sizeof (*s));
1248 if (!s) {
1249 term_printf ("Not enough memory to add wave capture\n");
1250 return;
1253 freq = has_freq ? freq : 44100;
1254 bits = has_bits ? bits : 16;
1255 nchannels = has_channels ? nchannels : 2;
1257 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1258 term_printf ("Faied to add wave capture\n");
1259 qemu_free (s);
1261 LIST_INSERT_HEAD (&capture_head, s, entries);
1263 #endif
1265 static term_cmd_t term_cmds[] = {
1266 { "help|?", "s?", do_help,
1267 "[cmd]", "show the help" },
1268 { "commit", "s", do_commit,
1269 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1270 { "info", "s?", do_info,
1271 "subcommand", "show various information about the system state" },
1272 { "q|quit", "", do_quit,
1273 "", "quit the emulator" },
1274 { "eject", "-fB", do_eject,
1275 "[-f] device", "eject a removable medium (use -f to force it)" },
1276 { "change", "BF", do_change,
1277 "device filename", "change a removable medium" },
1278 { "screendump", "F", do_screen_dump,
1279 "filename", "save screen into PPM image 'filename'" },
1280 { "logfile", "s", do_logfile,
1281 "filename", "output logs to 'filename'" },
1282 { "log", "s", do_log,
1283 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1284 { "savevm", "s?", do_savevm,
1285 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1286 { "loadvm", "s", do_loadvm,
1287 "tag|id", "restore a VM snapshot from its tag or id" },
1288 { "delvm", "s", do_delvm,
1289 "tag|id", "delete a VM snapshot from its tag or id" },
1290 { "stop", "", do_stop,
1291 "", "stop emulation", },
1292 { "c|cont", "", do_cont,
1293 "", "resume emulation", },
1294 #ifdef CONFIG_GDBSTUB
1295 { "gdbserver", "s?", do_gdbserver,
1296 "[port]", "start gdbserver session (default port=1234)", },
1297 #endif
1298 { "x", "/l", do_memory_dump,
1299 "/fmt addr", "virtual memory dump starting at 'addr'", },
1300 { "xp", "/l", do_physical_memory_dump,
1301 "/fmt addr", "physical memory dump starting at 'addr'", },
1302 { "p|print", "/l", do_print,
1303 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1304 { "i", "/ii.", do_ioport_read,
1305 "/fmt addr", "I/O port read" },
1307 { "sendkey", "s", do_send_key,
1308 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1309 { "system_reset", "", do_system_reset,
1310 "", "reset the system" },
1311 { "system_powerdown", "", do_system_powerdown,
1312 "", "send system power down event" },
1313 { "sum", "ii", do_sum,
1314 "addr size", "compute the checksum of a memory region" },
1315 { "usb_add", "s", do_usb_add,
1316 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1317 { "usb_del", "s", do_usb_del,
1318 "device", "remove USB device 'bus.addr'" },
1319 { "cpu", "i", do_cpu_set,
1320 "index", "set the default CPU" },
1321 { "mouse_move", "sss?", do_mouse_move,
1322 "dx dy [dz]", "send mouse move events" },
1323 { "mouse_button", "i", do_mouse_button,
1324 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1325 { "mouse_set", "i", do_mouse_set,
1326 "index", "set which mouse device receives events" },
1327 #ifdef HAS_AUDIO
1328 { "wavcapture", "si?i?i?", do_wav_capture,
1329 "path [frequency bits channels]",
1330 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1331 #endif
1332 { "stopcapture", "i", do_stop_capture,
1333 "capture index", "stop capture" },
1334 { "memsave", "lis", do_memory_save,
1335 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1336 { "migrate", "-ds", do_migrate,
1337 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1338 { "migrate_cancel", "", do_migrate_cancel,
1339 "", "cancel the current VM migration" },
1340 { "migrate_set_speed", "s", do_migrate_set_speed,
1341 "value", "set maximum speed (in bytes) for migrations" },
1342 { NULL, NULL, },
1345 static term_cmd_t info_cmds[] = {
1346 { "version", "", do_info_version,
1347 "", "show the version of qemu" },
1348 { "network", "", do_info_network,
1349 "", "show the network state" },
1350 { "block", "", do_info_block,
1351 "", "show the block devices" },
1352 { "blockstats", "", do_info_blockstats,
1353 "", "show block device statistics" },
1354 { "registers", "", do_info_registers,
1355 "", "show the cpu registers" },
1356 { "cpus", "", do_info_cpus,
1357 "", "show infos for each CPU" },
1358 { "history", "", do_info_history,
1359 "", "show the command line history", },
1360 { "irq", "", irq_info,
1361 "", "show the interrupts statistics (if available)", },
1362 { "pic", "", pic_info,
1363 "", "show i8259 (PIC) state", },
1364 { "pci", "", pci_info,
1365 "", "show PCI info", },
1366 #if defined(TARGET_I386)
1367 { "tlb", "", tlb_info,
1368 "", "show virtual to physical memory mappings", },
1369 { "mem", "", mem_info,
1370 "", "show the active virtual memory mappings", },
1371 #endif
1372 { "jit", "", do_info_jit,
1373 "", "show dynamic compiler info", },
1374 { "kqemu", "", do_info_kqemu,
1375 "", "show kqemu information", },
1376 { "usb", "", usb_info,
1377 "", "show guest USB devices", },
1378 { "usbhost", "", usb_host_info,
1379 "", "show host USB devices", },
1380 { "profile", "", do_info_profile,
1381 "", "show profiling information", },
1382 { "capture", "", do_info_capture,
1383 "", "show capture information" },
1384 { "snapshots", "", do_info_snapshots,
1385 "", "show the currently saved VM snapshots" },
1386 { "pcmcia", "", pcmcia_info,
1387 "", "show guest PCMCIA status" },
1388 { "mice", "", do_info_mice,
1389 "", "show which guest mouse is receiving events" },
1390 { "vnc", "", do_info_vnc,
1391 "", "show the vnc server status"},
1392 { "name", "", do_info_name,
1393 "", "show the current VM name" },
1394 #if defined(TARGET_PPC)
1395 { "cpustats", "", do_info_cpu_stats,
1396 "", "show CPU statistics", },
1397 #endif
1398 #if defined(CONFIG_SLIRP)
1399 { "slirp", "", do_info_slirp,
1400 "", "show SLIRP statistics", },
1401 #endif
1402 { "migration", "", do_info_migration,
1403 "", "show migration information" },
1404 { NULL, NULL, },
1407 /*******************************************************************/
1409 static const char *pch;
1410 static jmp_buf expr_env;
1412 #define MD_TLONG 0
1413 #define MD_I32 1
1415 typedef struct MonitorDef {
1416 const char *name;
1417 int offset;
1418 target_long (*get_value)(struct MonitorDef *md, int val);
1419 int type;
1420 } MonitorDef;
1422 #if defined(TARGET_I386)
1423 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1425 CPUState *env = mon_get_cpu();
1426 if (!env)
1427 return 0;
1428 return env->eip + env->segs[R_CS].base;
1430 #endif
1432 #if defined(TARGET_PPC)
1433 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1435 CPUState *env = mon_get_cpu();
1436 unsigned int u;
1437 int i;
1439 if (!env)
1440 return 0;
1442 u = 0;
1443 for (i = 0; i < 8; i++)
1444 u |= env->crf[i] << (32 - (4 * i));
1446 return u;
1449 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1451 CPUState *env = mon_get_cpu();
1452 if (!env)
1453 return 0;
1454 return env->msr;
1457 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1459 CPUState *env = mon_get_cpu();
1460 if (!env)
1461 return 0;
1462 return ppc_load_xer(env);
1465 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1467 CPUState *env = mon_get_cpu();
1468 if (!env)
1469 return 0;
1470 return cpu_ppc_load_decr(env);
1473 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1475 CPUState *env = mon_get_cpu();
1476 if (!env)
1477 return 0;
1478 return cpu_ppc_load_tbu(env);
1481 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1483 CPUState *env = mon_get_cpu();
1484 if (!env)
1485 return 0;
1486 return cpu_ppc_load_tbl(env);
1488 #endif
1490 #if defined(TARGET_SPARC)
1491 #ifndef TARGET_SPARC64
1492 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1494 CPUState *env = mon_get_cpu();
1495 if (!env)
1496 return 0;
1497 return GET_PSR(env);
1499 #endif
1501 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1503 CPUState *env = mon_get_cpu();
1504 if (!env)
1505 return 0;
1506 return env->regwptr[val];
1508 #endif
1510 static MonitorDef monitor_defs[] = {
1511 #ifdef TARGET_I386
1513 #define SEG(name, seg) \
1514 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1515 { name ".base", offsetof(CPUState, segs[seg].base) },\
1516 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1518 { "eax", offsetof(CPUState, regs[0]) },
1519 { "ecx", offsetof(CPUState, regs[1]) },
1520 { "edx", offsetof(CPUState, regs[2]) },
1521 { "ebx", offsetof(CPUState, regs[3]) },
1522 { "esp|sp", offsetof(CPUState, regs[4]) },
1523 { "ebp|fp", offsetof(CPUState, regs[5]) },
1524 { "esi", offsetof(CPUState, regs[6]) },
1525 { "edi", offsetof(CPUState, regs[7]) },
1526 #ifdef TARGET_X86_64
1527 { "r8", offsetof(CPUState, regs[8]) },
1528 { "r9", offsetof(CPUState, regs[9]) },
1529 { "r10", offsetof(CPUState, regs[10]) },
1530 { "r11", offsetof(CPUState, regs[11]) },
1531 { "r12", offsetof(CPUState, regs[12]) },
1532 { "r13", offsetof(CPUState, regs[13]) },
1533 { "r14", offsetof(CPUState, regs[14]) },
1534 { "r15", offsetof(CPUState, regs[15]) },
1535 #endif
1536 { "eflags", offsetof(CPUState, eflags) },
1537 { "eip", offsetof(CPUState, eip) },
1538 SEG("cs", R_CS)
1539 SEG("ds", R_DS)
1540 SEG("es", R_ES)
1541 SEG("ss", R_SS)
1542 SEG("fs", R_FS)
1543 SEG("gs", R_GS)
1544 { "pc", 0, monitor_get_pc, },
1545 #elif defined(TARGET_PPC)
1546 /* General purpose registers */
1547 { "r0", offsetof(CPUState, gpr[0]) },
1548 { "r1", offsetof(CPUState, gpr[1]) },
1549 { "r2", offsetof(CPUState, gpr[2]) },
1550 { "r3", offsetof(CPUState, gpr[3]) },
1551 { "r4", offsetof(CPUState, gpr[4]) },
1552 { "r5", offsetof(CPUState, gpr[5]) },
1553 { "r6", offsetof(CPUState, gpr[6]) },
1554 { "r7", offsetof(CPUState, gpr[7]) },
1555 { "r8", offsetof(CPUState, gpr[8]) },
1556 { "r9", offsetof(CPUState, gpr[9]) },
1557 { "r10", offsetof(CPUState, gpr[10]) },
1558 { "r11", offsetof(CPUState, gpr[11]) },
1559 { "r12", offsetof(CPUState, gpr[12]) },
1560 { "r13", offsetof(CPUState, gpr[13]) },
1561 { "r14", offsetof(CPUState, gpr[14]) },
1562 { "r15", offsetof(CPUState, gpr[15]) },
1563 { "r16", offsetof(CPUState, gpr[16]) },
1564 { "r17", offsetof(CPUState, gpr[17]) },
1565 { "r18", offsetof(CPUState, gpr[18]) },
1566 { "r19", offsetof(CPUState, gpr[19]) },
1567 { "r20", offsetof(CPUState, gpr[20]) },
1568 { "r21", offsetof(CPUState, gpr[21]) },
1569 { "r22", offsetof(CPUState, gpr[22]) },
1570 { "r23", offsetof(CPUState, gpr[23]) },
1571 { "r24", offsetof(CPUState, gpr[24]) },
1572 { "r25", offsetof(CPUState, gpr[25]) },
1573 { "r26", offsetof(CPUState, gpr[26]) },
1574 { "r27", offsetof(CPUState, gpr[27]) },
1575 { "r28", offsetof(CPUState, gpr[28]) },
1576 { "r29", offsetof(CPUState, gpr[29]) },
1577 { "r30", offsetof(CPUState, gpr[30]) },
1578 { "r31", offsetof(CPUState, gpr[31]) },
1579 /* Floating point registers */
1580 { "f0", offsetof(CPUState, fpr[0]) },
1581 { "f1", offsetof(CPUState, fpr[1]) },
1582 { "f2", offsetof(CPUState, fpr[2]) },
1583 { "f3", offsetof(CPUState, fpr[3]) },
1584 { "f4", offsetof(CPUState, fpr[4]) },
1585 { "f5", offsetof(CPUState, fpr[5]) },
1586 { "f6", offsetof(CPUState, fpr[6]) },
1587 { "f7", offsetof(CPUState, fpr[7]) },
1588 { "f8", offsetof(CPUState, fpr[8]) },
1589 { "f9", offsetof(CPUState, fpr[9]) },
1590 { "f10", offsetof(CPUState, fpr[10]) },
1591 { "f11", offsetof(CPUState, fpr[11]) },
1592 { "f12", offsetof(CPUState, fpr[12]) },
1593 { "f13", offsetof(CPUState, fpr[13]) },
1594 { "f14", offsetof(CPUState, fpr[14]) },
1595 { "f15", offsetof(CPUState, fpr[15]) },
1596 { "f16", offsetof(CPUState, fpr[16]) },
1597 { "f17", offsetof(CPUState, fpr[17]) },
1598 { "f18", offsetof(CPUState, fpr[18]) },
1599 { "f19", offsetof(CPUState, fpr[19]) },
1600 { "f20", offsetof(CPUState, fpr[20]) },
1601 { "f21", offsetof(CPUState, fpr[21]) },
1602 { "f22", offsetof(CPUState, fpr[22]) },
1603 { "f23", offsetof(CPUState, fpr[23]) },
1604 { "f24", offsetof(CPUState, fpr[24]) },
1605 { "f25", offsetof(CPUState, fpr[25]) },
1606 { "f26", offsetof(CPUState, fpr[26]) },
1607 { "f27", offsetof(CPUState, fpr[27]) },
1608 { "f28", offsetof(CPUState, fpr[28]) },
1609 { "f29", offsetof(CPUState, fpr[29]) },
1610 { "f30", offsetof(CPUState, fpr[30]) },
1611 { "f31", offsetof(CPUState, fpr[31]) },
1612 { "fpscr", offsetof(CPUState, fpscr) },
1613 /* Next instruction pointer */
1614 { "nip|pc", offsetof(CPUState, nip) },
1615 { "lr", offsetof(CPUState, lr) },
1616 { "ctr", offsetof(CPUState, ctr) },
1617 { "decr", 0, &monitor_get_decr, },
1618 { "ccr", 0, &monitor_get_ccr, },
1619 /* Machine state register */
1620 { "msr", 0, &monitor_get_msr, },
1621 { "xer", 0, &monitor_get_xer, },
1622 { "tbu", 0, &monitor_get_tbu, },
1623 { "tbl", 0, &monitor_get_tbl, },
1624 #if defined(TARGET_PPC64)
1625 /* Address space register */
1626 { "asr", offsetof(CPUState, asr) },
1627 #endif
1628 /* Segment registers */
1629 { "sdr1", offsetof(CPUState, sdr1) },
1630 { "sr0", offsetof(CPUState, sr[0]) },
1631 { "sr1", offsetof(CPUState, sr[1]) },
1632 { "sr2", offsetof(CPUState, sr[2]) },
1633 { "sr3", offsetof(CPUState, sr[3]) },
1634 { "sr4", offsetof(CPUState, sr[4]) },
1635 { "sr5", offsetof(CPUState, sr[5]) },
1636 { "sr6", offsetof(CPUState, sr[6]) },
1637 { "sr7", offsetof(CPUState, sr[7]) },
1638 { "sr8", offsetof(CPUState, sr[8]) },
1639 { "sr9", offsetof(CPUState, sr[9]) },
1640 { "sr10", offsetof(CPUState, sr[10]) },
1641 { "sr11", offsetof(CPUState, sr[11]) },
1642 { "sr12", offsetof(CPUState, sr[12]) },
1643 { "sr13", offsetof(CPUState, sr[13]) },
1644 { "sr14", offsetof(CPUState, sr[14]) },
1645 { "sr15", offsetof(CPUState, sr[15]) },
1646 /* Too lazy to put BATs and SPRs ... */
1647 #elif defined(TARGET_SPARC)
1648 { "g0", offsetof(CPUState, gregs[0]) },
1649 { "g1", offsetof(CPUState, gregs[1]) },
1650 { "g2", offsetof(CPUState, gregs[2]) },
1651 { "g3", offsetof(CPUState, gregs[3]) },
1652 { "g4", offsetof(CPUState, gregs[4]) },
1653 { "g5", offsetof(CPUState, gregs[5]) },
1654 { "g6", offsetof(CPUState, gregs[6]) },
1655 { "g7", offsetof(CPUState, gregs[7]) },
1656 { "o0", 0, monitor_get_reg },
1657 { "o1", 1, monitor_get_reg },
1658 { "o2", 2, monitor_get_reg },
1659 { "o3", 3, monitor_get_reg },
1660 { "o4", 4, monitor_get_reg },
1661 { "o5", 5, monitor_get_reg },
1662 { "o6", 6, monitor_get_reg },
1663 { "o7", 7, monitor_get_reg },
1664 { "l0", 8, monitor_get_reg },
1665 { "l1", 9, monitor_get_reg },
1666 { "l2", 10, monitor_get_reg },
1667 { "l3", 11, monitor_get_reg },
1668 { "l4", 12, monitor_get_reg },
1669 { "l5", 13, monitor_get_reg },
1670 { "l6", 14, monitor_get_reg },
1671 { "l7", 15, monitor_get_reg },
1672 { "i0", 16, monitor_get_reg },
1673 { "i1", 17, monitor_get_reg },
1674 { "i2", 18, monitor_get_reg },
1675 { "i3", 19, monitor_get_reg },
1676 { "i4", 20, monitor_get_reg },
1677 { "i5", 21, monitor_get_reg },
1678 { "i6", 22, monitor_get_reg },
1679 { "i7", 23, monitor_get_reg },
1680 { "pc", offsetof(CPUState, pc) },
1681 { "npc", offsetof(CPUState, npc) },
1682 { "y", offsetof(CPUState, y) },
1683 #ifndef TARGET_SPARC64
1684 { "psr", 0, &monitor_get_psr, },
1685 { "wim", offsetof(CPUState, wim) },
1686 #endif
1687 { "tbr", offsetof(CPUState, tbr) },
1688 { "fsr", offsetof(CPUState, fsr) },
1689 { "f0", offsetof(CPUState, fpr[0]) },
1690 { "f1", offsetof(CPUState, fpr[1]) },
1691 { "f2", offsetof(CPUState, fpr[2]) },
1692 { "f3", offsetof(CPUState, fpr[3]) },
1693 { "f4", offsetof(CPUState, fpr[4]) },
1694 { "f5", offsetof(CPUState, fpr[5]) },
1695 { "f6", offsetof(CPUState, fpr[6]) },
1696 { "f7", offsetof(CPUState, fpr[7]) },
1697 { "f8", offsetof(CPUState, fpr[8]) },
1698 { "f9", offsetof(CPUState, fpr[9]) },
1699 { "f10", offsetof(CPUState, fpr[10]) },
1700 { "f11", offsetof(CPUState, fpr[11]) },
1701 { "f12", offsetof(CPUState, fpr[12]) },
1702 { "f13", offsetof(CPUState, fpr[13]) },
1703 { "f14", offsetof(CPUState, fpr[14]) },
1704 { "f15", offsetof(CPUState, fpr[15]) },
1705 { "f16", offsetof(CPUState, fpr[16]) },
1706 { "f17", offsetof(CPUState, fpr[17]) },
1707 { "f18", offsetof(CPUState, fpr[18]) },
1708 { "f19", offsetof(CPUState, fpr[19]) },
1709 { "f20", offsetof(CPUState, fpr[20]) },
1710 { "f21", offsetof(CPUState, fpr[21]) },
1711 { "f22", offsetof(CPUState, fpr[22]) },
1712 { "f23", offsetof(CPUState, fpr[23]) },
1713 { "f24", offsetof(CPUState, fpr[24]) },
1714 { "f25", offsetof(CPUState, fpr[25]) },
1715 { "f26", offsetof(CPUState, fpr[26]) },
1716 { "f27", offsetof(CPUState, fpr[27]) },
1717 { "f28", offsetof(CPUState, fpr[28]) },
1718 { "f29", offsetof(CPUState, fpr[29]) },
1719 { "f30", offsetof(CPUState, fpr[30]) },
1720 { "f31", offsetof(CPUState, fpr[31]) },
1721 #ifdef TARGET_SPARC64
1722 { "f32", offsetof(CPUState, fpr[32]) },
1723 { "f34", offsetof(CPUState, fpr[34]) },
1724 { "f36", offsetof(CPUState, fpr[36]) },
1725 { "f38", offsetof(CPUState, fpr[38]) },
1726 { "f40", offsetof(CPUState, fpr[40]) },
1727 { "f42", offsetof(CPUState, fpr[42]) },
1728 { "f44", offsetof(CPUState, fpr[44]) },
1729 { "f46", offsetof(CPUState, fpr[46]) },
1730 { "f48", offsetof(CPUState, fpr[48]) },
1731 { "f50", offsetof(CPUState, fpr[50]) },
1732 { "f52", offsetof(CPUState, fpr[52]) },
1733 { "f54", offsetof(CPUState, fpr[54]) },
1734 { "f56", offsetof(CPUState, fpr[56]) },
1735 { "f58", offsetof(CPUState, fpr[58]) },
1736 { "f60", offsetof(CPUState, fpr[60]) },
1737 { "f62", offsetof(CPUState, fpr[62]) },
1738 { "asi", offsetof(CPUState, asi) },
1739 { "pstate", offsetof(CPUState, pstate) },
1740 { "cansave", offsetof(CPUState, cansave) },
1741 { "canrestore", offsetof(CPUState, canrestore) },
1742 { "otherwin", offsetof(CPUState, otherwin) },
1743 { "wstate", offsetof(CPUState, wstate) },
1744 { "cleanwin", offsetof(CPUState, cleanwin) },
1745 { "fprs", offsetof(CPUState, fprs) },
1746 #endif
1747 #endif
1748 { NULL },
1751 static void expr_error(const char *fmt)
1753 term_printf(fmt);
1754 term_printf("\n");
1755 longjmp(expr_env, 1);
1758 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1759 static int get_monitor_def(target_long *pval, const char *name)
1761 MonitorDef *md;
1762 void *ptr;
1764 for(md = monitor_defs; md->name != NULL; md++) {
1765 if (compare_cmd(name, md->name)) {
1766 if (md->get_value) {
1767 *pval = md->get_value(md, md->offset);
1768 } else {
1769 CPUState *env = mon_get_cpu();
1770 if (!env)
1771 return -2;
1772 ptr = (uint8_t *)env + md->offset;
1773 switch(md->type) {
1774 case MD_I32:
1775 *pval = *(int32_t *)ptr;
1776 break;
1777 case MD_TLONG:
1778 *pval = *(target_long *)ptr;
1779 break;
1780 default:
1781 *pval = 0;
1782 break;
1785 return 0;
1788 return -1;
1791 static void next(void)
1793 if (pch != '\0') {
1794 pch++;
1795 while (isspace(*pch))
1796 pch++;
1800 static int64_t expr_sum(void);
1802 static int64_t expr_unary(void)
1804 int64_t n;
1805 char *p;
1806 int ret;
1808 switch(*pch) {
1809 case '+':
1810 next();
1811 n = expr_unary();
1812 break;
1813 case '-':
1814 next();
1815 n = -expr_unary();
1816 break;
1817 case '~':
1818 next();
1819 n = ~expr_unary();
1820 break;
1821 case '(':
1822 next();
1823 n = expr_sum();
1824 if (*pch != ')') {
1825 expr_error("')' expected");
1827 next();
1828 break;
1829 case '\'':
1830 pch++;
1831 if (*pch == '\0')
1832 expr_error("character constant expected");
1833 n = *pch;
1834 pch++;
1835 if (*pch != '\'')
1836 expr_error("missing terminating \' character");
1837 next();
1838 break;
1839 case '$':
1841 char buf[128], *q;
1842 target_long reg=0;
1844 pch++;
1845 q = buf;
1846 while ((*pch >= 'a' && *pch <= 'z') ||
1847 (*pch >= 'A' && *pch <= 'Z') ||
1848 (*pch >= '0' && *pch <= '9') ||
1849 *pch == '_' || *pch == '.') {
1850 if ((q - buf) < sizeof(buf) - 1)
1851 *q++ = *pch;
1852 pch++;
1854 while (isspace(*pch))
1855 pch++;
1856 *q = 0;
1857 ret = get_monitor_def(&reg, buf);
1858 if (ret == -1)
1859 expr_error("unknown register");
1860 else if (ret == -2)
1861 expr_error("no cpu defined");
1862 n = reg;
1864 break;
1865 case '\0':
1866 expr_error("unexpected end of expression");
1867 n = 0;
1868 break;
1869 default:
1870 #if TARGET_PHYS_ADDR_BITS > 32
1871 n = strtoull(pch, &p, 0);
1872 #else
1873 n = strtoul(pch, &p, 0);
1874 #endif
1875 if (pch == p) {
1876 expr_error("invalid char in expression");
1878 pch = p;
1879 while (isspace(*pch))
1880 pch++;
1881 break;
1883 return n;
1887 static int64_t expr_prod(void)
1889 int64_t val, val2;
1890 int op;
1892 val = expr_unary();
1893 for(;;) {
1894 op = *pch;
1895 if (op != '*' && op != '/' && op != '%')
1896 break;
1897 next();
1898 val2 = expr_unary();
1899 switch(op) {
1900 default:
1901 case '*':
1902 val *= val2;
1903 break;
1904 case '/':
1905 case '%':
1906 if (val2 == 0)
1907 expr_error("division by zero");
1908 if (op == '/')
1909 val /= val2;
1910 else
1911 val %= val2;
1912 break;
1915 return val;
1918 static int64_t expr_logic(void)
1920 int64_t val, val2;
1921 int op;
1923 val = expr_prod();
1924 for(;;) {
1925 op = *pch;
1926 if (op != '&' && op != '|' && op != '^')
1927 break;
1928 next();
1929 val2 = expr_prod();
1930 switch(op) {
1931 default:
1932 case '&':
1933 val &= val2;
1934 break;
1935 case '|':
1936 val |= val2;
1937 break;
1938 case '^':
1939 val ^= val2;
1940 break;
1943 return val;
1946 static int64_t expr_sum(void)
1948 int64_t val, val2;
1949 int op;
1951 val = expr_logic();
1952 for(;;) {
1953 op = *pch;
1954 if (op != '+' && op != '-')
1955 break;
1956 next();
1957 val2 = expr_logic();
1958 if (op == '+')
1959 val += val2;
1960 else
1961 val -= val2;
1963 return val;
1966 static int get_expr(int64_t *pval, const char **pp)
1968 pch = *pp;
1969 if (setjmp(expr_env)) {
1970 *pp = pch;
1971 return -1;
1973 while (isspace(*pch))
1974 pch++;
1975 *pval = expr_sum();
1976 *pp = pch;
1977 return 0;
1980 static int get_str(char *buf, int buf_size, const char **pp)
1982 const char *p;
1983 char *q;
1984 int c;
1986 q = buf;
1987 p = *pp;
1988 while (isspace(*p))
1989 p++;
1990 if (*p == '\0') {
1991 fail:
1992 *q = '\0';
1993 *pp = p;
1994 return -1;
1996 if (*p == '\"') {
1997 p++;
1998 while (*p != '\0' && *p != '\"') {
1999 if (*p == '\\') {
2000 p++;
2001 c = *p++;
2002 switch(c) {
2003 case 'n':
2004 c = '\n';
2005 break;
2006 case 'r':
2007 c = '\r';
2008 break;
2009 case '\\':
2010 case '\'':
2011 case '\"':
2012 break;
2013 default:
2014 qemu_printf("unsupported escape code: '\\%c'\n", c);
2015 goto fail;
2017 if ((q - buf) < buf_size - 1) {
2018 *q++ = c;
2020 } else {
2021 if ((q - buf) < buf_size - 1) {
2022 *q++ = *p;
2024 p++;
2027 if (*p != '\"') {
2028 qemu_printf("unterminated string\n");
2029 goto fail;
2031 p++;
2032 } else {
2033 while (*p != '\0' && !isspace(*p)) {
2034 if ((q - buf) < buf_size - 1) {
2035 *q++ = *p;
2037 p++;
2040 *q = '\0';
2041 *pp = p;
2042 return 0;
2045 static int default_fmt_format = 'x';
2046 static int default_fmt_size = 4;
2048 #define MAX_ARGS 16
2050 static void monitor_handle_command(const char *cmdline)
2052 const char *p, *pstart, *typestr;
2053 char *q;
2054 int c, nb_args, len, i, has_arg;
2055 term_cmd_t *cmd;
2056 char cmdname[256];
2057 char buf[1024];
2058 void *str_allocated[MAX_ARGS];
2059 void *args[MAX_ARGS];
2061 #ifdef DEBUG
2062 term_printf("command='%s'\n", cmdline);
2063 #endif
2065 /* extract the command name */
2066 p = cmdline;
2067 q = cmdname;
2068 while (isspace(*p))
2069 p++;
2070 if (*p == '\0')
2071 return;
2072 pstart = p;
2073 while (*p != '\0' && *p != '/' && !isspace(*p))
2074 p++;
2075 len = p - pstart;
2076 if (len > sizeof(cmdname) - 1)
2077 len = sizeof(cmdname) - 1;
2078 memcpy(cmdname, pstart, len);
2079 cmdname[len] = '\0';
2081 /* find the command */
2082 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2083 if (compare_cmd(cmdname, cmd->name))
2084 goto found;
2086 term_printf("unknown command: '%s'\n", cmdname);
2087 return;
2088 found:
2090 for(i = 0; i < MAX_ARGS; i++)
2091 str_allocated[i] = NULL;
2093 /* parse the parameters */
2094 typestr = cmd->args_type;
2095 nb_args = 0;
2096 for(;;) {
2097 c = *typestr;
2098 if (c == '\0')
2099 break;
2100 typestr++;
2101 switch(c) {
2102 case 'F':
2103 case 'B':
2104 case 's':
2106 int ret;
2107 char *str;
2109 while (isspace(*p))
2110 p++;
2111 if (*typestr == '?') {
2112 typestr++;
2113 if (*p == '\0') {
2114 /* no optional string: NULL argument */
2115 str = NULL;
2116 goto add_str;
2119 ret = get_str(buf, sizeof(buf), &p);
2120 if (ret < 0) {
2121 switch(c) {
2122 case 'F':
2123 term_printf("%s: filename expected\n", cmdname);
2124 break;
2125 case 'B':
2126 term_printf("%s: block device name expected\n", cmdname);
2127 break;
2128 default:
2129 term_printf("%s: string expected\n", cmdname);
2130 break;
2132 goto fail;
2134 str = qemu_malloc(strlen(buf) + 1);
2135 strcpy(str, buf);
2136 str_allocated[nb_args] = str;
2137 add_str:
2138 if (nb_args >= MAX_ARGS) {
2139 error_args:
2140 term_printf("%s: too many arguments\n", cmdname);
2141 goto fail;
2143 args[nb_args++] = str;
2145 break;
2146 case '/':
2148 int count, format, size;
2150 while (isspace(*p))
2151 p++;
2152 if (*p == '/') {
2153 /* format found */
2154 p++;
2155 count = 1;
2156 if (isdigit(*p)) {
2157 count = 0;
2158 while (isdigit(*p)) {
2159 count = count * 10 + (*p - '0');
2160 p++;
2163 size = -1;
2164 format = -1;
2165 for(;;) {
2166 switch(*p) {
2167 case 'o':
2168 case 'd':
2169 case 'u':
2170 case 'x':
2171 case 'i':
2172 case 'c':
2173 format = *p++;
2174 break;
2175 case 'b':
2176 size = 1;
2177 p++;
2178 break;
2179 case 'h':
2180 size = 2;
2181 p++;
2182 break;
2183 case 'w':
2184 size = 4;
2185 p++;
2186 break;
2187 case 'g':
2188 case 'L':
2189 size = 8;
2190 p++;
2191 break;
2192 default:
2193 goto next;
2196 next:
2197 if (*p != '\0' && !isspace(*p)) {
2198 term_printf("invalid char in format: '%c'\n", *p);
2199 goto fail;
2201 if (format < 0)
2202 format = default_fmt_format;
2203 if (format != 'i') {
2204 /* for 'i', not specifying a size gives -1 as size */
2205 if (size < 0)
2206 size = default_fmt_size;
2208 default_fmt_size = size;
2209 default_fmt_format = format;
2210 } else {
2211 count = 1;
2212 format = default_fmt_format;
2213 if (format != 'i') {
2214 size = default_fmt_size;
2215 } else {
2216 size = -1;
2219 if (nb_args + 3 > MAX_ARGS)
2220 goto error_args;
2221 args[nb_args++] = (void*)(long)count;
2222 args[nb_args++] = (void*)(long)format;
2223 args[nb_args++] = (void*)(long)size;
2225 break;
2226 case 'i':
2227 case 'l':
2229 int64_t val;
2231 while (isspace(*p))
2232 p++;
2233 if (*typestr == '?' || *typestr == '.') {
2234 if (*typestr == '?') {
2235 if (*p == '\0')
2236 has_arg = 0;
2237 else
2238 has_arg = 1;
2239 } else {
2240 if (*p == '.') {
2241 p++;
2242 while (isspace(*p))
2243 p++;
2244 has_arg = 1;
2245 } else {
2246 has_arg = 0;
2249 typestr++;
2250 if (nb_args >= MAX_ARGS)
2251 goto error_args;
2252 args[nb_args++] = (void *)(long)has_arg;
2253 if (!has_arg) {
2254 if (nb_args >= MAX_ARGS)
2255 goto error_args;
2256 val = -1;
2257 goto add_num;
2260 if (get_expr(&val, &p))
2261 goto fail;
2262 add_num:
2263 if (c == 'i') {
2264 if (nb_args >= MAX_ARGS)
2265 goto error_args;
2266 args[nb_args++] = (void *)(long)val;
2267 } else {
2268 if ((nb_args + 1) >= MAX_ARGS)
2269 goto error_args;
2270 #if TARGET_PHYS_ADDR_BITS > 32
2271 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2272 #else
2273 args[nb_args++] = (void *)0;
2274 #endif
2275 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2278 break;
2279 case '-':
2281 int has_option;
2282 /* option */
2284 c = *typestr++;
2285 if (c == '\0')
2286 goto bad_type;
2287 while (isspace(*p))
2288 p++;
2289 has_option = 0;
2290 if (*p == '-') {
2291 p++;
2292 if (*p != c) {
2293 term_printf("%s: unsupported option -%c\n",
2294 cmdname, *p);
2295 goto fail;
2297 p++;
2298 has_option = 1;
2300 if (nb_args >= MAX_ARGS)
2301 goto error_args;
2302 args[nb_args++] = (void *)(long)has_option;
2304 break;
2305 default:
2306 bad_type:
2307 term_printf("%s: unknown type '%c'\n", cmdname, c);
2308 goto fail;
2311 /* check that all arguments were parsed */
2312 while (isspace(*p))
2313 p++;
2314 if (*p != '\0') {
2315 term_printf("%s: extraneous characters at the end of line\n",
2316 cmdname);
2317 goto fail;
2320 switch(nb_args) {
2321 case 0:
2322 cmd->handler();
2323 break;
2324 case 1:
2325 cmd->handler(args[0]);
2326 break;
2327 case 2:
2328 cmd->handler(args[0], args[1]);
2329 break;
2330 case 3:
2331 cmd->handler(args[0], args[1], args[2]);
2332 break;
2333 case 4:
2334 cmd->handler(args[0], args[1], args[2], args[3]);
2335 break;
2336 case 5:
2337 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2338 break;
2339 case 6:
2340 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2341 break;
2342 case 7:
2343 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2344 break;
2345 default:
2346 term_printf("unsupported number of arguments: %d\n", nb_args);
2347 goto fail;
2349 fail:
2350 for(i = 0; i < MAX_ARGS; i++)
2351 qemu_free(str_allocated[i]);
2352 return;
2355 static void cmd_completion(const char *name, const char *list)
2357 const char *p, *pstart;
2358 char cmd[128];
2359 int len;
2361 p = list;
2362 for(;;) {
2363 pstart = p;
2364 p = strchr(p, '|');
2365 if (!p)
2366 p = pstart + strlen(pstart);
2367 len = p - pstart;
2368 if (len > sizeof(cmd) - 2)
2369 len = sizeof(cmd) - 2;
2370 memcpy(cmd, pstart, len);
2371 cmd[len] = '\0';
2372 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2373 add_completion(cmd);
2375 if (*p == '\0')
2376 break;
2377 p++;
2381 static void file_completion(const char *input)
2383 DIR *ffs;
2384 struct dirent *d;
2385 char path[1024];
2386 char file[1024], file_prefix[1024];
2387 int input_path_len;
2388 const char *p;
2390 p = strrchr(input, '/');
2391 if (!p) {
2392 input_path_len = 0;
2393 pstrcpy(file_prefix, sizeof(file_prefix), input);
2394 strcpy(path, ".");
2395 } else {
2396 input_path_len = p - input + 1;
2397 memcpy(path, input, input_path_len);
2398 if (input_path_len > sizeof(path) - 1)
2399 input_path_len = sizeof(path) - 1;
2400 path[input_path_len] = '\0';
2401 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2403 #ifdef DEBUG_COMPLETION
2404 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2405 #endif
2406 ffs = opendir(path);
2407 if (!ffs)
2408 return;
2409 for(;;) {
2410 struct stat sb;
2411 d = readdir(ffs);
2412 if (!d)
2413 break;
2414 if (strstart(d->d_name, file_prefix, NULL)) {
2415 memcpy(file, input, input_path_len);
2416 strcpy(file + input_path_len, d->d_name);
2417 /* stat the file to find out if it's a directory.
2418 * In that case add a slash to speed up typing long paths
2420 stat(file, &sb);
2421 if(S_ISDIR(sb.st_mode))
2422 strcat(file, "/");
2423 add_completion(file);
2426 closedir(ffs);
2429 static void block_completion_it(void *opaque, const char *name)
2431 const char *input = opaque;
2433 if (input[0] == '\0' ||
2434 !strncmp(name, (char *)input, strlen(input))) {
2435 add_completion(name);
2439 /* NOTE: this parser is an approximate form of the real command parser */
2440 static void parse_cmdline(const char *cmdline,
2441 int *pnb_args, char **args)
2443 const char *p;
2444 int nb_args, ret;
2445 char buf[1024];
2447 p = cmdline;
2448 nb_args = 0;
2449 for(;;) {
2450 while (isspace(*p))
2451 p++;
2452 if (*p == '\0')
2453 break;
2454 if (nb_args >= MAX_ARGS)
2455 break;
2456 ret = get_str(buf, sizeof(buf), &p);
2457 args[nb_args] = qemu_strdup(buf);
2458 nb_args++;
2459 if (ret < 0)
2460 break;
2462 *pnb_args = nb_args;
2465 void readline_find_completion(const char *cmdline)
2467 const char *cmdname;
2468 char *args[MAX_ARGS];
2469 int nb_args, i, len;
2470 const char *ptype, *str;
2471 term_cmd_t *cmd;
2472 const KeyDef *key;
2474 parse_cmdline(cmdline, &nb_args, args);
2475 #ifdef DEBUG_COMPLETION
2476 for(i = 0; i < nb_args; i++) {
2477 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2479 #endif
2481 /* if the line ends with a space, it means we want to complete the
2482 next arg */
2483 len = strlen(cmdline);
2484 if (len > 0 && isspace(cmdline[len - 1])) {
2485 if (nb_args >= MAX_ARGS)
2486 return;
2487 args[nb_args++] = qemu_strdup("");
2489 if (nb_args <= 1) {
2490 /* command completion */
2491 if (nb_args == 0)
2492 cmdname = "";
2493 else
2494 cmdname = args[0];
2495 completion_index = strlen(cmdname);
2496 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2497 cmd_completion(cmdname, cmd->name);
2499 } else {
2500 /* find the command */
2501 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2502 if (compare_cmd(args[0], cmd->name))
2503 goto found;
2505 return;
2506 found:
2507 ptype = cmd->args_type;
2508 for(i = 0; i < nb_args - 2; i++) {
2509 if (*ptype != '\0') {
2510 ptype++;
2511 while (*ptype == '?')
2512 ptype++;
2515 str = args[nb_args - 1];
2516 switch(*ptype) {
2517 case 'F':
2518 /* file completion */
2519 completion_index = strlen(str);
2520 file_completion(str);
2521 break;
2522 case 'B':
2523 /* block device name completion */
2524 completion_index = strlen(str);
2525 bdrv_iterate(block_completion_it, (void *)str);
2526 break;
2527 case 's':
2528 /* XXX: more generic ? */
2529 if (!strcmp(cmd->name, "info")) {
2530 completion_index = strlen(str);
2531 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2532 cmd_completion(str, cmd->name);
2534 } else if (!strcmp(cmd->name, "sendkey")) {
2535 completion_index = strlen(str);
2536 for(key = key_defs; key->name != NULL; key++) {
2537 cmd_completion(str, key->name);
2540 break;
2541 default:
2542 break;
2545 for(i = 0; i < nb_args; i++)
2546 qemu_free(args[i]);
2549 static int term_can_read(void *opaque)
2551 return 128;
2554 static void term_read(void *opaque, const uint8_t *buf, int size)
2556 int i;
2557 for(i = 0; i < size; i++)
2558 readline_handle_byte(buf[i]);
2561 static int monitor_suspended;
2563 void monitor_suspend(void)
2565 monitor_suspended = 1;
2568 void monitor_resume(void)
2570 monitor_suspended = 0;
2571 monitor_start_input();
2574 static void monitor_start_input(void);
2576 static void monitor_handle_command1(void *opaque, const char *cmdline)
2578 monitor_handle_command(cmdline);
2579 if (!monitor_suspended)
2580 monitor_start_input();
2583 static void monitor_start_input(void)
2585 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2588 static void term_event(void *opaque, int event)
2590 if (event != CHR_EVENT_RESET)
2591 return;
2593 if (!hide_banner)
2594 term_printf("QEMU %s monitor - type 'help' for more information\n",
2595 QEMU_VERSION);
2596 monitor_start_input();
2599 static int is_first_init = 1;
2601 void monitor_init(CharDriverState *hd, int show_banner)
2603 int i;
2605 if (is_first_init) {
2606 for (i = 0; i < MAX_MON; i++) {
2607 monitor_hd[i] = NULL;
2609 is_first_init = 0;
2611 for (i = 0; i < MAX_MON; i++) {
2612 if (monitor_hd[i] == NULL) {
2613 monitor_hd[i] = hd;
2614 break;
2618 hide_banner = !show_banner;
2620 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2622 readline_start("", 0, monitor_handle_command1, NULL);
2625 /* XXX: use threads ? */
2626 /* modal monitor readline */
2627 static int monitor_readline_started;
2628 static char *monitor_readline_buf;
2629 static int monitor_readline_buf_size;
2631 static void monitor_readline_cb(void *opaque, const char *input)
2633 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2634 monitor_readline_started = 0;
2637 void monitor_readline(const char *prompt, int is_password,
2638 char *buf, int buf_size)
2640 int i;
2642 if (is_password) {
2643 for (i = 0; i < MAX_MON; i++)
2644 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2645 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2647 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2648 monitor_readline_buf = buf;
2649 monitor_readline_buf_size = buf_size;
2650 monitor_readline_started = 1;
2651 while (monitor_readline_started) {
2652 main_loop_wait(10);