kvm: testsuite: add emulator tests
[qemu-kvm/amd-iommu.git] / monitor.c
blob10e0af954f79fc6c4b7a2896ee6c1073d05c9aa1
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 "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
28 #include "qemu-kvm.h"
30 //#define DEBUG
31 //#define DEBUG_COMPLETION
33 #ifndef offsetof
34 #define offsetof(type, field) ((size_t) &((type *)0)->field)
35 #endif
38 * Supported types:
40 * 'F' filename
41 * 'B' block device name
42 * 's' string (accept optional quote)
43 * 'i' 32 bit integer
44 * 'l' target long (32 or 64 bit)
45 * '/' optional gdb-like print format (like "/10x")
47 * '?' optional type (for 'F', 's' and 'i')
51 typedef struct term_cmd_t {
52 const char *name;
53 const char *args_type;
54 void (*handler)();
55 const char *params;
56 const char *help;
57 } term_cmd_t;
59 #define MAX_MON 4
60 static CharDriverState *monitor_hd[MAX_MON];
61 static int hide_banner;
63 static term_cmd_t term_cmds[];
64 static term_cmd_t info_cmds[];
66 static char term_outbuf[1024];
67 static int term_outbuf_index;
69 static void monitor_start_input(void);
71 CPUState *mon_cpu = NULL;
73 void term_flush(void)
75 int i;
76 if (term_outbuf_index > 0) {
77 for (i = 0; i < MAX_MON; i++)
78 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
79 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
80 term_outbuf_index = 0;
84 /* flush at every end of line or if the buffer is full */
85 void term_puts(const char *str)
87 int c;
88 for(;;) {
89 c = *str++;
90 if (c == '\0')
91 break;
92 if (c == '\n')
93 term_outbuf[term_outbuf_index++] = '\r';
94 term_outbuf[term_outbuf_index++] = c;
95 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
96 c == '\n')
97 term_flush();
101 void term_vprintf(const char *fmt, va_list ap)
103 char buf[4096];
104 vsnprintf(buf, sizeof(buf), fmt, ap);
105 term_puts(buf);
108 void term_printf(const char *fmt, ...)
110 va_list ap;
111 va_start(ap, fmt);
112 term_vprintf(fmt, ap);
113 va_end(ap);
116 void term_print_filename(const char *filename)
118 int i;
120 for (i = 0; filename[i]; i++) {
121 switch (filename[i]) {
122 case ' ':
123 case '"':
124 case '\\':
125 term_printf("\\%c", filename[i]);
126 break;
127 case '\t':
128 term_printf("\\t");
129 break;
130 case '\r':
131 term_printf("\\r");
132 break;
133 case '\n':
134 term_printf("\\n");
135 break;
136 default:
137 term_printf("%c", filename[i]);
138 break;
143 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
145 va_list ap;
146 va_start(ap, fmt);
147 term_vprintf(fmt, ap);
148 va_end(ap);
149 return 0;
152 static int compare_cmd(const char *name, const char *list)
154 const char *p, *pstart;
155 int len;
156 len = strlen(name);
157 p = list;
158 for(;;) {
159 pstart = p;
160 p = strchr(p, '|');
161 if (!p)
162 p = pstart + strlen(pstart);
163 if ((p - pstart) == len && !memcmp(pstart, name, len))
164 return 1;
165 if (*p == '\0')
166 break;
167 p++;
169 return 0;
172 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
174 term_cmd_t *cmd;
176 for(cmd = cmds; cmd->name != NULL; cmd++) {
177 if (!name || !strcmp(name, cmd->name))
178 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
182 static void help_cmd(const char *name)
184 if (name && !strcmp(name, "info")) {
185 help_cmd1(info_cmds, "info ", NULL);
186 } else {
187 help_cmd1(term_cmds, "", name);
188 if (name && !strcmp(name, "log")) {
189 CPULogItem *item;
190 term_printf("Log items (comma separated):\n");
191 term_printf("%-10s %s\n", "none", "remove all logs");
192 for(item = cpu_log_items; item->mask != 0; item++) {
193 term_printf("%-10s %s\n", item->name, item->help);
199 static void do_help(const char *name)
201 help_cmd(name);
204 static void do_commit(const char *device)
206 int i, all_devices;
208 all_devices = !strcmp(device, "all");
209 for (i = 0; i < MAX_DISKS; i++) {
210 if (bs_table[i]) {
211 if (all_devices ||
212 !strcmp(bdrv_get_device_name(bs_table[i]), device))
213 bdrv_commit(bs_table[i]);
216 if (mtd_bdrv)
217 if (all_devices || !strcmp(bdrv_get_device_name(mtd_bdrv), device))
218 bdrv_commit(mtd_bdrv);
221 static void do_info(const char *item)
223 term_cmd_t *cmd;
225 if (!item)
226 goto help;
227 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
228 if (compare_cmd(item, cmd->name))
229 goto found;
231 help:
232 help_cmd("info");
233 return;
234 found:
235 cmd->handler();
238 static void do_info_version(void)
240 term_printf("%s\n", QEMU_VERSION);
243 static void do_info_name(void)
245 if (qemu_name)
246 term_printf("%s\n", qemu_name);
249 static void do_info_block(void)
251 bdrv_info();
254 /* get the current CPU defined by the user */
255 int mon_set_cpu(int cpu_index)
257 CPUState *env;
259 for(env = first_cpu; env != NULL; env = env->next_cpu) {
260 if (env->cpu_index == cpu_index) {
261 mon_cpu = env;
262 return 0;
265 return -1;
268 CPUState *mon_get_cpu(void)
270 if (!mon_cpu) {
271 mon_set_cpu(0);
274 #ifdef USE_KVM
275 kvm_save_registers(mon_cpu);
276 #endif
278 return mon_cpu;
281 static void do_info_registers(void)
283 CPUState *env;
284 env = mon_get_cpu();
285 if (!env)
286 return;
287 #ifdef TARGET_I386
288 cpu_dump_state(env, NULL, monitor_fprintf,
289 X86_DUMP_FPU);
290 #else
291 cpu_dump_state(env, NULL, monitor_fprintf,
293 #endif
296 static void do_info_cpus(void)
298 CPUState *env;
300 /* just to set the default cpu if not already done */
301 mon_get_cpu();
303 for(env = first_cpu; env != NULL; env = env->next_cpu) {
304 term_printf("%c CPU #%d:",
305 (env == mon_cpu) ? '*' : ' ',
306 env->cpu_index);
307 #if defined(TARGET_I386)
308 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
309 if (env->hflags & HF_HALTED_MASK)
310 term_printf(" (halted)");
311 #elif defined(TARGET_PPC)
312 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
313 if (env->halted)
314 term_printf(" (halted)");
315 #elif defined(TARGET_SPARC)
316 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
317 if (env->halted)
318 term_printf(" (halted)");
319 #elif defined(TARGET_MIPS)
320 term_printf(" PC=0x" TARGET_FMT_lx, env->PC[env->current_tc]);
321 if (env->halted)
322 term_printf(" (halted)");
323 #endif
324 term_printf("\n");
328 static void do_cpu_set(int index)
330 if (mon_set_cpu(index) < 0)
331 term_printf("Invalid CPU index\n");
334 static void do_info_jit(void)
336 dump_exec_info(NULL, monitor_fprintf);
339 static void do_info_history (void)
341 int i;
342 const char *str;
344 i = 0;
345 for(;;) {
346 str = readline_get_history(i);
347 if (!str)
348 break;
349 term_printf("%d: '%s'\n", i, str);
350 i++;
354 #if defined(TARGET_PPC)
355 /* XXX: not implemented in other targets */
356 static void do_info_cpu_stats (void)
358 CPUState *env;
360 env = mon_get_cpu();
361 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
363 #endif
365 static void do_quit(void)
367 exit(0);
370 static int eject_device(BlockDriverState *bs, int force)
372 if (bdrv_is_inserted(bs)) {
373 if (!force) {
374 if (!bdrv_is_removable(bs)) {
375 term_printf("device is not removable\n");
376 return -1;
378 if (bdrv_is_locked(bs)) {
379 term_printf("device is locked\n");
380 return -1;
383 bdrv_close(bs);
385 return 0;
388 static void do_eject(int force, const char *filename)
390 BlockDriverState *bs;
392 bs = bdrv_find(filename);
393 if (!bs) {
394 term_printf("device not found\n");
395 return;
397 eject_device(bs, force);
400 #define USAGE_STR "Usage: read_disk_io hd[a/b/c/d]\n"
401 DiskIOStatistics vmdk_io_statistics(BlockDriverState *bs);
403 static void do_io_statistics(const char *hdx)
405 DiskIOStatistics io[4];
407 if ((strcmp(hdx,"hda") != 0) && (strcmp(hdx,"hdb") != 0) &&
408 (strcmp(hdx,"hdc") != 0) && (strcmp(hdx,"hdd") != 0)) {
409 term_printf(USAGE_STR);
410 return;
413 switch (hdx[2]) {
414 case 'a':
415 term_printf("---------- hda io statistics ----------\n");
416 io[0] = vmdk_io_statistics(bs_table[0]);
417 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
418 io[0].read_byte_counter, io[0].write_byte_counter);
419 break;
420 case 'b':
421 term_printf("---------- hdb io statistics ----------\n");
422 if (bs_table[1]) {
423 io[1] = vmdk_io_statistics(bs_table[1]);
424 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
425 io[1].read_byte_counter, io[1].write_byte_counter);
426 }else {
427 term_printf("hdb not exist\n");
429 break;
430 case 'c':
431 term_printf("---------- hdc io statistics ----------\n");
432 if (bs_table[2]) {
433 io[2] = vmdk_io_statistics(bs_table[2]);
434 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
435 io[2].read_byte_counter, io[2].write_byte_counter);
436 }else {
437 term_printf("hdc not exist\n");
439 break;
440 case 'd':
441 term_printf("---------- hdd io statistics ----------\n");
442 if (bs_table[3]) {
443 io[3] = vmdk_io_statistics(bs_table[3]);
444 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
445 io[3].read_byte_counter, io[3].write_byte_counter);
446 }else {
447 term_printf("hdd not exist\n");
449 break;
453 static void do_change_block(const char *device, const char *filename)
455 BlockDriverState *bs;
457 bs = bdrv_find(device);
458 if (!bs) {
459 term_printf("device not found\n");
460 return;
462 if (eject_device(bs, 0) < 0)
463 return;
464 bdrv_open(bs, filename, 0);
465 qemu_key_check(bs, filename);
468 static void do_change_vnc(const char *target)
470 if (strcmp(target, "passwd") == 0 ||
471 strcmp(target, "password") == 0) {
472 char password[9];
473 monitor_readline("Password: ", 1, password, sizeof(password)-1);
474 password[sizeof(password)-1] = '\0';
475 if (vnc_display_password(NULL, password) < 0)
476 term_printf("could not set VNC server password\n");
477 } else {
478 if (vnc_display_open(NULL, target) < 0)
479 term_printf("could not start VNC server on %s\n", target);
483 static void do_change(const char *device, const char *target)
485 if (strcmp(device, "vnc") == 0) {
486 do_change_vnc(target);
487 } else {
488 do_change_block(device, target);
492 static void do_screen_dump(const char *filename)
494 vga_hw_screen_dump(filename);
497 static void do_logfile(const char *filename)
499 cpu_set_log_filename(filename);
502 static void do_log(const char *items)
504 int mask;
506 if (!strcmp(items, "none")) {
507 mask = 0;
508 } else {
509 mask = cpu_str_to_log_mask(items);
510 if (!mask) {
511 help_cmd("log");
512 return;
515 cpu_set_log(mask);
518 static void do_stop(void)
520 vm_stop(EXCP_INTERRUPT);
523 static void do_cont(void)
525 vm_start();
528 #ifdef CONFIG_GDBSTUB
529 static void do_gdbserver(const char *port)
531 if (!port)
532 port = DEFAULT_GDBSTUB_PORT;
533 if (gdbserver_start(port) < 0) {
534 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
535 } else {
536 qemu_printf("Waiting gdb connection on port '%s'\n", port);
539 #endif
541 static void term_printc(int c)
543 term_printf("'");
544 switch(c) {
545 case '\'':
546 term_printf("\\'");
547 break;
548 case '\\':
549 term_printf("\\\\");
550 break;
551 case '\n':
552 term_printf("\\n");
553 break;
554 case '\r':
555 term_printf("\\r");
556 break;
557 default:
558 if (c >= 32 && c <= 126) {
559 term_printf("%c", c);
560 } else {
561 term_printf("\\x%02x", c);
563 break;
565 term_printf("'");
568 static void memory_dump(int count, int format, int wsize,
569 target_phys_addr_t addr, int is_physical)
571 CPUState *env;
572 int nb_per_line, l, line_size, i, max_digits, len;
573 uint8_t buf[16];
574 uint64_t v;
576 if (format == 'i') {
577 int flags;
578 flags = 0;
579 env = mon_get_cpu();
580 if (!env && !is_physical)
581 return;
582 #ifdef TARGET_I386
583 if (wsize == 2) {
584 flags = 1;
585 } else if (wsize == 4) {
586 flags = 0;
587 } else {
588 /* as default we use the current CS size */
589 flags = 0;
590 if (env) {
591 #ifdef TARGET_X86_64
592 if ((env->efer & MSR_EFER_LMA) &&
593 (env->segs[R_CS].flags & DESC_L_MASK))
594 flags = 2;
595 else
596 #endif
597 if (!(env->segs[R_CS].flags & DESC_B_MASK))
598 flags = 1;
601 #endif
602 monitor_disas(env, addr, count, is_physical, flags);
603 return;
606 len = wsize * count;
607 if (wsize == 1)
608 line_size = 8;
609 else
610 line_size = 16;
611 nb_per_line = line_size / wsize;
612 max_digits = 0;
614 switch(format) {
615 case 'o':
616 max_digits = (wsize * 8 + 2) / 3;
617 break;
618 default:
619 case 'x':
620 max_digits = (wsize * 8) / 4;
621 break;
622 case 'u':
623 case 'd':
624 max_digits = (wsize * 8 * 10 + 32) / 33;
625 break;
626 case 'c':
627 wsize = 1;
628 break;
631 while (len > 0) {
632 if (is_physical)
633 term_printf(TARGET_FMT_plx ":", addr);
634 else
635 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
636 l = len;
637 if (l > line_size)
638 l = line_size;
639 if (is_physical) {
640 cpu_physical_memory_rw(addr, buf, l, 0);
641 } else {
642 env = mon_get_cpu();
643 if (!env)
644 break;
645 cpu_memory_rw_debug(env, addr, buf, l, 0);
647 i = 0;
648 while (i < l) {
649 switch(wsize) {
650 default:
651 case 1:
652 v = ldub_raw(buf + i);
653 break;
654 case 2:
655 v = lduw_raw(buf + i);
656 break;
657 case 4:
658 v = (uint32_t)ldl_raw(buf + i);
659 break;
660 case 8:
661 v = ldq_raw(buf + i);
662 break;
664 term_printf(" ");
665 switch(format) {
666 case 'o':
667 term_printf("%#*" PRIo64, max_digits, v);
668 break;
669 case 'x':
670 term_printf("0x%0*" PRIx64, max_digits, v);
671 break;
672 case 'u':
673 term_printf("%*" PRIu64, max_digits, v);
674 break;
675 case 'd':
676 term_printf("%*" PRId64, max_digits, v);
677 break;
678 case 'c':
679 term_printc(v);
680 break;
682 i += wsize;
684 term_printf("\n");
685 addr += l;
686 len -= l;
690 #if TARGET_LONG_BITS == 64
691 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
692 #else
693 #define GET_TLONG(h, l) (l)
694 #endif
696 static void do_memory_dump(int count, int format, int size,
697 uint32_t addrh, uint32_t addrl)
699 target_long addr = GET_TLONG(addrh, addrl);
700 memory_dump(count, format, size, addr, 0);
703 #if TARGET_PHYS_ADDR_BITS > 32
704 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
705 #else
706 #define GET_TPHYSADDR(h, l) (l)
707 #endif
709 static void do_physical_memory_dump(int count, int format, int size,
710 uint32_t addrh, uint32_t addrl)
713 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
714 memory_dump(count, format, size, addr, 1);
717 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
719 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
720 #if TARGET_PHYS_ADDR_BITS == 32
721 switch(format) {
722 case 'o':
723 term_printf("%#o", val);
724 break;
725 case 'x':
726 term_printf("%#x", val);
727 break;
728 case 'u':
729 term_printf("%u", val);
730 break;
731 default:
732 case 'd':
733 term_printf("%d", val);
734 break;
735 case 'c':
736 term_printc(val);
737 break;
739 #else
740 switch(format) {
741 case 'o':
742 term_printf("%#" PRIo64, val);
743 break;
744 case 'x':
745 term_printf("%#" PRIx64, val);
746 break;
747 case 'u':
748 term_printf("%" PRIu64, val);
749 break;
750 default:
751 case 'd':
752 term_printf("%" PRId64, val);
753 break;
754 case 'c':
755 term_printc(val);
756 break;
758 #endif
759 term_printf("\n");
762 static void do_memory_save(unsigned int valh, unsigned int vall,
763 uint32_t size, const char *filename)
765 FILE *f;
766 target_long addr = GET_TLONG(valh, vall);
767 uint32_t l;
768 CPUState *env;
769 uint8_t buf[1024];
771 env = mon_get_cpu();
772 if (!env)
773 return;
775 f = fopen(filename, "wb");
776 if (!f) {
777 term_printf("could not open '%s'\n", filename);
778 return;
780 while (size != 0) {
781 l = sizeof(buf);
782 if (l > size)
783 l = size;
784 cpu_memory_rw_debug(env, addr, buf, l, 0);
785 fwrite(buf, 1, l, f);
786 addr += l;
787 size -= l;
789 fclose(f);
792 static void do_sum(uint32_t start, uint32_t size)
794 uint32_t addr;
795 uint8_t buf[1];
796 uint16_t sum;
798 sum = 0;
799 for(addr = start; addr < (start + size); addr++) {
800 cpu_physical_memory_rw(addr, buf, 1, 0);
801 /* BSD sum algorithm ('sum' Unix command) */
802 sum = (sum >> 1) | (sum << 15);
803 sum += buf[0];
805 term_printf("%05d\n", sum);
808 typedef struct {
809 int keycode;
810 const char *name;
811 } KeyDef;
813 static const KeyDef key_defs[] = {
814 { 0x2a, "shift" },
815 { 0x36, "shift_r" },
817 { 0x38, "alt" },
818 { 0xb8, "alt_r" },
819 { 0x1d, "ctrl" },
820 { 0x9d, "ctrl_r" },
822 { 0xdd, "menu" },
824 { 0x01, "esc" },
826 { 0x02, "1" },
827 { 0x03, "2" },
828 { 0x04, "3" },
829 { 0x05, "4" },
830 { 0x06, "5" },
831 { 0x07, "6" },
832 { 0x08, "7" },
833 { 0x09, "8" },
834 { 0x0a, "9" },
835 { 0x0b, "0" },
836 { 0x0c, "minus" },
837 { 0x0d, "equal" },
838 { 0x0e, "backspace" },
840 { 0x0f, "tab" },
841 { 0x10, "q" },
842 { 0x11, "w" },
843 { 0x12, "e" },
844 { 0x13, "r" },
845 { 0x14, "t" },
846 { 0x15, "y" },
847 { 0x16, "u" },
848 { 0x17, "i" },
849 { 0x18, "o" },
850 { 0x19, "p" },
852 { 0x1c, "ret" },
854 { 0x1e, "a" },
855 { 0x1f, "s" },
856 { 0x20, "d" },
857 { 0x21, "f" },
858 { 0x22, "g" },
859 { 0x23, "h" },
860 { 0x24, "j" },
861 { 0x25, "k" },
862 { 0x26, "l" },
864 { 0x2c, "z" },
865 { 0x2d, "x" },
866 { 0x2e, "c" },
867 { 0x2f, "v" },
868 { 0x30, "b" },
869 { 0x31, "n" },
870 { 0x32, "m" },
872 { 0x39, "spc" },
873 { 0x3a, "caps_lock" },
874 { 0x3b, "f1" },
875 { 0x3c, "f2" },
876 { 0x3d, "f3" },
877 { 0x3e, "f4" },
878 { 0x3f, "f5" },
879 { 0x40, "f6" },
880 { 0x41, "f7" },
881 { 0x42, "f8" },
882 { 0x43, "f9" },
883 { 0x44, "f10" },
884 { 0x45, "num_lock" },
885 { 0x46, "scroll_lock" },
887 { 0xb5, "kp_divide" },
888 { 0x37, "kp_multiply" },
889 { 0x4a, "kp_subtract" },
890 { 0x4e, "kp_add" },
891 { 0x9c, "kp_enter" },
892 { 0x53, "kp_decimal" },
894 { 0x52, "kp_0" },
895 { 0x4f, "kp_1" },
896 { 0x50, "kp_2" },
897 { 0x51, "kp_3" },
898 { 0x4b, "kp_4" },
899 { 0x4c, "kp_5" },
900 { 0x4d, "kp_6" },
901 { 0x47, "kp_7" },
902 { 0x48, "kp_8" },
903 { 0x49, "kp_9" },
905 { 0x56, "<" },
907 { 0x57, "f11" },
908 { 0x58, "f12" },
910 { 0xb7, "print" },
912 { 0xc7, "home" },
913 { 0xc9, "pgup" },
914 { 0xd1, "pgdn" },
915 { 0xcf, "end" },
917 { 0xcb, "left" },
918 { 0xc8, "up" },
919 { 0xd0, "down" },
920 { 0xcd, "right" },
922 { 0xd2, "insert" },
923 { 0xd3, "delete" },
924 { 0, NULL },
927 static int get_keycode(const char *key)
929 const KeyDef *p;
930 char *endp;
931 int ret;
933 for(p = key_defs; p->name != NULL; p++) {
934 if (!strcmp(key, p->name))
935 return p->keycode;
937 if (strstart(key, "0x", NULL)) {
938 ret = strtoul(key, &endp, 0);
939 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
940 return ret;
942 return -1;
945 static void do_send_key(const char *string)
947 char keybuf[16], *q;
948 uint8_t keycodes[16];
949 const char *p;
950 int nb_keycodes, keycode, i;
952 nb_keycodes = 0;
953 p = string;
954 while (*p != '\0') {
955 q = keybuf;
956 while (*p != '\0' && *p != '-') {
957 if ((q - keybuf) < sizeof(keybuf) - 1) {
958 *q++ = *p;
960 p++;
962 *q = '\0';
963 keycode = get_keycode(keybuf);
964 if (keycode < 0) {
965 term_printf("unknown key: '%s'\n", keybuf);
966 return;
968 keycodes[nb_keycodes++] = keycode;
969 if (*p == '\0')
970 break;
971 p++;
973 /* key down events */
974 for(i = 0; i < nb_keycodes; i++) {
975 keycode = keycodes[i];
976 if (keycode & 0x80)
977 kbd_put_keycode(0xe0);
978 kbd_put_keycode(keycode & 0x7f);
980 /* key up events */
981 for(i = nb_keycodes - 1; i >= 0; i--) {
982 keycode = keycodes[i];
983 if (keycode & 0x80)
984 kbd_put_keycode(0xe0);
985 kbd_put_keycode(keycode | 0x80);
989 static int mouse_button_state;
991 static void do_mouse_move(const char *dx_str, const char *dy_str,
992 const char *dz_str)
994 int dx, dy, dz;
995 dx = strtol(dx_str, NULL, 0);
996 dy = strtol(dy_str, NULL, 0);
997 dz = 0;
998 if (dz_str)
999 dz = strtol(dz_str, NULL, 0);
1000 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1003 static void do_mouse_button(int button_state)
1005 mouse_button_state = button_state;
1006 kbd_mouse_event(0, 0, 0, mouse_button_state);
1009 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1011 uint32_t val;
1012 int suffix;
1014 if (has_index) {
1015 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1016 addr++;
1018 addr &= 0xffff;
1020 switch(size) {
1021 default:
1022 case 1:
1023 val = cpu_inb(NULL, addr);
1024 suffix = 'b';
1025 break;
1026 case 2:
1027 val = cpu_inw(NULL, addr);
1028 suffix = 'w';
1029 break;
1030 case 4:
1031 val = cpu_inl(NULL, addr);
1032 suffix = 'l';
1033 break;
1035 term_printf("port%c[0x%04x] = %#0*x\n",
1036 suffix, addr, size * 2, val);
1039 static void do_system_reset(void)
1041 qemu_system_reset_request();
1044 static void do_system_powerdown(void)
1046 qemu_system_powerdown_request();
1049 #if defined(TARGET_I386)
1050 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1052 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1053 addr,
1054 pte & mask,
1055 pte & PG_GLOBAL_MASK ? 'G' : '-',
1056 pte & PG_PSE_MASK ? 'P' : '-',
1057 pte & PG_DIRTY_MASK ? 'D' : '-',
1058 pte & PG_ACCESSED_MASK ? 'A' : '-',
1059 pte & PG_PCD_MASK ? 'C' : '-',
1060 pte & PG_PWT_MASK ? 'T' : '-',
1061 pte & PG_USER_MASK ? 'U' : '-',
1062 pte & PG_RW_MASK ? 'W' : '-');
1065 static void tlb_info(void)
1067 CPUState *env;
1068 int l1, l2;
1069 uint32_t pgd, pde, pte;
1071 env = mon_get_cpu();
1072 if (!env)
1073 return;
1075 if (!(env->cr[0] & CR0_PG_MASK)) {
1076 term_printf("PG disabled\n");
1077 return;
1079 pgd = env->cr[3] & ~0xfff;
1080 for(l1 = 0; l1 < 1024; l1++) {
1081 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1082 pde = le32_to_cpu(pde);
1083 if (pde & PG_PRESENT_MASK) {
1084 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1085 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1086 } else {
1087 for(l2 = 0; l2 < 1024; l2++) {
1088 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1089 (uint8_t *)&pte, 4);
1090 pte = le32_to_cpu(pte);
1091 if (pte & PG_PRESENT_MASK) {
1092 print_pte((l1 << 22) + (l2 << 12),
1093 pte & ~PG_PSE_MASK,
1094 ~0xfff);
1102 static void mem_print(uint32_t *pstart, int *plast_prot,
1103 uint32_t end, int prot)
1105 int prot1;
1106 prot1 = *plast_prot;
1107 if (prot != prot1) {
1108 if (*pstart != -1) {
1109 term_printf("%08x-%08x %08x %c%c%c\n",
1110 *pstart, end, end - *pstart,
1111 prot1 & PG_USER_MASK ? 'u' : '-',
1112 'r',
1113 prot1 & PG_RW_MASK ? 'w' : '-');
1115 if (prot != 0)
1116 *pstart = end;
1117 else
1118 *pstart = -1;
1119 *plast_prot = prot;
1123 static void mem_info(void)
1125 CPUState *env;
1126 int l1, l2, prot, last_prot;
1127 uint32_t pgd, pde, pte, start, end;
1129 env = mon_get_cpu();
1130 if (!env)
1131 return;
1133 if (!(env->cr[0] & CR0_PG_MASK)) {
1134 term_printf("PG disabled\n");
1135 return;
1137 pgd = env->cr[3] & ~0xfff;
1138 last_prot = 0;
1139 start = -1;
1140 for(l1 = 0; l1 < 1024; l1++) {
1141 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1142 pde = le32_to_cpu(pde);
1143 end = l1 << 22;
1144 if (pde & PG_PRESENT_MASK) {
1145 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1146 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1147 mem_print(&start, &last_prot, end, prot);
1148 } else {
1149 for(l2 = 0; l2 < 1024; l2++) {
1150 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1151 (uint8_t *)&pte, 4);
1152 pte = le32_to_cpu(pte);
1153 end = (l1 << 22) + (l2 << 12);
1154 if (pte & PG_PRESENT_MASK) {
1155 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1156 } else {
1157 prot = 0;
1159 mem_print(&start, &last_prot, end, prot);
1162 } else {
1163 prot = 0;
1164 mem_print(&start, &last_prot, end, prot);
1168 #endif
1170 static void do_info_kqemu(void)
1172 #ifdef USE_KQEMU
1173 CPUState *env;
1174 int val;
1175 val = 0;
1176 env = mon_get_cpu();
1177 if (!env) {
1178 term_printf("No cpu initialized yet");
1179 return;
1181 val = env->kqemu_enabled;
1182 term_printf("kqemu support: ");
1183 switch(val) {
1184 default:
1185 case 0:
1186 term_printf("disabled\n");
1187 break;
1188 case 1:
1189 term_printf("enabled for user code\n");
1190 break;
1191 case 2:
1192 term_printf("enabled for user and kernel code\n");
1193 break;
1195 #else
1196 term_printf("kqemu support: not compiled\n");
1197 #endif
1200 #ifdef CONFIG_PROFILER
1202 int64_t kqemu_time;
1203 int64_t qemu_time;
1204 int64_t kqemu_exec_count;
1205 int64_t dev_time;
1206 int64_t kqemu_ret_int_count;
1207 int64_t kqemu_ret_excp_count;
1208 int64_t kqemu_ret_intr_count;
1210 static void do_info_profile(void)
1212 int64_t total;
1213 total = qemu_time;
1214 if (total == 0)
1215 total = 1;
1216 term_printf("async time %" PRId64 " (%0.3f)\n",
1217 dev_time, dev_time / (double)ticks_per_sec);
1218 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1219 qemu_time, qemu_time / (double)ticks_per_sec);
1220 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1221 kqemu_time, kqemu_time / (double)ticks_per_sec,
1222 kqemu_time / (double)total * 100.0,
1223 kqemu_exec_count,
1224 kqemu_ret_int_count,
1225 kqemu_ret_excp_count,
1226 kqemu_ret_intr_count);
1227 qemu_time = 0;
1228 kqemu_time = 0;
1229 kqemu_exec_count = 0;
1230 dev_time = 0;
1231 kqemu_ret_int_count = 0;
1232 kqemu_ret_excp_count = 0;
1233 kqemu_ret_intr_count = 0;
1234 #ifdef USE_KQEMU
1235 kqemu_record_dump();
1236 #endif
1238 #else
1239 static void do_info_profile(void)
1241 term_printf("Internal profiler not compiled\n");
1243 #endif
1245 /* Capture support */
1246 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1248 static void do_info_capture (void)
1250 int i;
1251 CaptureState *s;
1253 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1254 term_printf ("[%d]: ", i);
1255 s->ops.info (s->opaque);
1259 static void do_stop_capture (int n)
1261 int i;
1262 CaptureState *s;
1264 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1265 if (i == n) {
1266 s->ops.destroy (s->opaque);
1267 LIST_REMOVE (s, entries);
1268 qemu_free (s);
1269 return;
1274 #ifdef HAS_AUDIO
1275 int wav_start_capture (CaptureState *s, const char *path, int freq,
1276 int bits, int nchannels);
1278 static void do_wav_capture (const char *path,
1279 int has_freq, int freq,
1280 int has_bits, int bits,
1281 int has_channels, int nchannels)
1283 CaptureState *s;
1285 s = qemu_mallocz (sizeof (*s));
1286 if (!s) {
1287 term_printf ("Not enough memory to add wave capture\n");
1288 return;
1291 freq = has_freq ? freq : 44100;
1292 bits = has_bits ? bits : 16;
1293 nchannels = has_channels ? nchannels : 2;
1295 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1296 term_printf ("Faied to add wave capture\n");
1297 qemu_free (s);
1299 LIST_INSERT_HEAD (&capture_head, s, entries);
1301 #endif
1303 static term_cmd_t term_cmds[] = {
1304 { "help|?", "s?", do_help,
1305 "[cmd]", "show the help" },
1306 { "commit", "s", do_commit,
1307 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1308 { "info", "s?", do_info,
1309 "subcommand", "show various information about the system state" },
1310 { "q|quit", "", do_quit,
1311 "", "quit the emulator" },
1312 { "eject", "-fB", do_eject,
1313 "[-f] device", "eject a removable medium (use -f to force it)" },
1314 { "change", "BF", do_change,
1315 "device filename", "change a removable medium" },
1316 { "screendump", "F", do_screen_dump,
1317 "filename", "save screen into PPM image 'filename'" },
1318 { "logfile", "s", do_logfile,
1319 "filename", "output logs to 'filename'" },
1320 { "log", "s", do_log,
1321 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1322 { "savevm", "s?", do_savevm,
1323 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1324 { "loadvm", "s", do_loadvm,
1325 "tag|id", "restore a VM snapshot from its tag or id" },
1326 { "delvm", "s", do_delvm,
1327 "tag|id", "delete a VM snapshot from its tag or id" },
1328 { "stop", "", do_stop,
1329 "", "stop emulation", },
1330 { "c|cont", "", do_cont,
1331 "", "resume emulation", },
1332 #ifdef CONFIG_GDBSTUB
1333 { "gdbserver", "s?", do_gdbserver,
1334 "[port]", "start gdbserver session (default port=1234)", },
1335 #endif
1336 { "x", "/l", do_memory_dump,
1337 "/fmt addr", "virtual memory dump starting at 'addr'", },
1338 { "xp", "/l", do_physical_memory_dump,
1339 "/fmt addr", "physical memory dump starting at 'addr'", },
1340 { "p|print", "/l", do_print,
1341 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1342 { "i", "/ii.", do_ioport_read,
1343 "/fmt addr", "I/O port read" },
1345 { "sendkey", "s", do_send_key,
1346 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1347 { "system_reset", "", do_system_reset,
1348 "", "reset the system" },
1349 { "system_powerdown", "", do_system_powerdown,
1350 "", "send system power down event" },
1351 { "sum", "ii", do_sum,
1352 "addr size", "compute the checksum of a memory region" },
1353 { "usb_add", "s", do_usb_add,
1354 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1355 { "usb_del", "s", do_usb_del,
1356 "device", "remove USB device 'bus.addr'" },
1357 { "cpu", "i", do_cpu_set,
1358 "index", "set the default CPU" },
1359 { "mouse_move", "sss?", do_mouse_move,
1360 "dx dy [dz]", "send mouse move events" },
1361 { "mouse_button", "i", do_mouse_button,
1362 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1363 { "mouse_set", "i", do_mouse_set,
1364 "index", "set which mouse device receives events" },
1365 #ifdef HAS_AUDIO
1366 { "wavcapture", "si?i?i?", do_wav_capture,
1367 "path [frequency bits channels]",
1368 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1369 #endif
1370 { "stopcapture", "i", do_stop_capture,
1371 "capture index", "stop capture" },
1372 { "memsave", "lis", do_memory_save,
1373 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1374 { "read_disk_io", "s", do_io_statistics,
1375 "hdx", "read disk I/O statistics (VMDK format)" },
1376 { "migrate", "-ds", do_migrate,
1377 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1378 { "migrate_cancel", "", do_migrate_cancel,
1379 "", "cancel the current VM migration" },
1380 { "migrate_set_speed", "s", do_migrate_set_speed,
1381 "value", "set maximum speed (in bytes) for migrations" },
1382 { NULL, NULL, },
1385 static term_cmd_t info_cmds[] = {
1386 { "version", "", do_info_version,
1387 "", "show the version of qemu" },
1388 { "network", "", do_info_network,
1389 "", "show the network state" },
1390 { "block", "", do_info_block,
1391 "", "show the block devices" },
1392 { "registers", "", do_info_registers,
1393 "", "show the cpu registers" },
1394 { "cpus", "", do_info_cpus,
1395 "", "show infos for each CPU" },
1396 { "history", "", do_info_history,
1397 "", "show the command line history", },
1398 { "irq", "", irq_info,
1399 "", "show the interrupts statistics (if available)", },
1400 { "pic", "", pic_info,
1401 "", "show i8259 (PIC) state", },
1402 { "pci", "", pci_info,
1403 "", "show PCI info", },
1404 #if defined(TARGET_I386)
1405 { "tlb", "", tlb_info,
1406 "", "show virtual to physical memory mappings", },
1407 { "mem", "", mem_info,
1408 "", "show the active virtual memory mappings", },
1409 #endif
1410 { "jit", "", do_info_jit,
1411 "", "show dynamic compiler info", },
1412 { "kqemu", "", do_info_kqemu,
1413 "", "show kqemu information", },
1414 { "usb", "", usb_info,
1415 "", "show guest USB devices", },
1416 { "usbhost", "", usb_host_info,
1417 "", "show host USB devices", },
1418 { "profile", "", do_info_profile,
1419 "", "show profiling information", },
1420 { "capture", "", do_info_capture,
1421 "", "show capture information" },
1422 { "snapshots", "", do_info_snapshots,
1423 "", "show the currently saved VM snapshots" },
1424 { "pcmcia", "", pcmcia_info,
1425 "", "show guest PCMCIA status" },
1426 { "mice", "", do_info_mice,
1427 "", "show which guest mouse is receiving events" },
1428 { "vnc", "", do_info_vnc,
1429 "", "show the vnc server status"},
1430 { "name", "", do_info_name,
1431 "", "show the current VM name" },
1432 #if defined(TARGET_PPC)
1433 { "cpustats", "", do_info_cpu_stats,
1434 "", "show CPU statistics", },
1435 #endif
1436 { "migration", "", do_info_migration,
1437 "", "show migration information" },
1438 { NULL, NULL, },
1441 /*******************************************************************/
1443 static const char *pch;
1444 static jmp_buf expr_env;
1446 #define MD_TLONG 0
1447 #define MD_I32 1
1449 typedef struct MonitorDef {
1450 const char *name;
1451 int offset;
1452 target_long (*get_value)(struct MonitorDef *md, int val);
1453 int type;
1454 } MonitorDef;
1456 #if defined(TARGET_I386)
1457 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1459 CPUState *env = mon_get_cpu();
1460 if (!env)
1461 return 0;
1462 return env->eip + env->segs[R_CS].base;
1464 #endif
1466 #if defined(TARGET_PPC)
1467 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1469 CPUState *env = mon_get_cpu();
1470 unsigned int u;
1471 int i;
1473 if (!env)
1474 return 0;
1476 u = 0;
1477 for (i = 0; i < 8; i++)
1478 u |= env->crf[i] << (32 - (4 * i));
1480 return u;
1483 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1485 CPUState *env = mon_get_cpu();
1486 if (!env)
1487 return 0;
1488 return do_load_msr(env);
1491 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1493 CPUState *env = mon_get_cpu();
1494 if (!env)
1495 return 0;
1496 return ppc_load_xer(env);
1499 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1501 CPUState *env = mon_get_cpu();
1502 if (!env)
1503 return 0;
1504 return cpu_ppc_load_decr(env);
1507 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1509 CPUState *env = mon_get_cpu();
1510 if (!env)
1511 return 0;
1512 return cpu_ppc_load_tbu(env);
1515 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1517 CPUState *env = mon_get_cpu();
1518 if (!env)
1519 return 0;
1520 return cpu_ppc_load_tbl(env);
1522 #endif
1524 #if defined(TARGET_SPARC)
1525 #ifndef TARGET_SPARC64
1526 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1528 CPUState *env = mon_get_cpu();
1529 if (!env)
1530 return 0;
1531 return GET_PSR(env);
1533 #endif
1535 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1537 CPUState *env = mon_get_cpu();
1538 if (!env)
1539 return 0;
1540 return env->regwptr[val];
1542 #endif
1544 static MonitorDef monitor_defs[] = {
1545 #ifdef TARGET_I386
1547 #define SEG(name, seg) \
1548 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1549 { name ".base", offsetof(CPUState, segs[seg].base) },\
1550 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1552 { "eax", offsetof(CPUState, regs[0]) },
1553 { "ecx", offsetof(CPUState, regs[1]) },
1554 { "edx", offsetof(CPUState, regs[2]) },
1555 { "ebx", offsetof(CPUState, regs[3]) },
1556 { "esp|sp", offsetof(CPUState, regs[4]) },
1557 { "ebp|fp", offsetof(CPUState, regs[5]) },
1558 { "esi", offsetof(CPUState, regs[6]) },
1559 { "edi", offsetof(CPUState, regs[7]) },
1560 #ifdef TARGET_X86_64
1561 { "r8", offsetof(CPUState, regs[8]) },
1562 { "r9", offsetof(CPUState, regs[9]) },
1563 { "r10", offsetof(CPUState, regs[10]) },
1564 { "r11", offsetof(CPUState, regs[11]) },
1565 { "r12", offsetof(CPUState, regs[12]) },
1566 { "r13", offsetof(CPUState, regs[13]) },
1567 { "r14", offsetof(CPUState, regs[14]) },
1568 { "r15", offsetof(CPUState, regs[15]) },
1569 #endif
1570 { "eflags", offsetof(CPUState, eflags) },
1571 { "eip", offsetof(CPUState, eip) },
1572 SEG("cs", R_CS)
1573 SEG("ds", R_DS)
1574 SEG("es", R_ES)
1575 SEG("ss", R_SS)
1576 SEG("fs", R_FS)
1577 SEG("gs", R_GS)
1578 { "pc", 0, monitor_get_pc, },
1579 #elif defined(TARGET_PPC)
1580 /* General purpose registers */
1581 { "r0", offsetof(CPUState, gpr[0]) },
1582 { "r1", offsetof(CPUState, gpr[1]) },
1583 { "r2", offsetof(CPUState, gpr[2]) },
1584 { "r3", offsetof(CPUState, gpr[3]) },
1585 { "r4", offsetof(CPUState, gpr[4]) },
1586 { "r5", offsetof(CPUState, gpr[5]) },
1587 { "r6", offsetof(CPUState, gpr[6]) },
1588 { "r7", offsetof(CPUState, gpr[7]) },
1589 { "r8", offsetof(CPUState, gpr[8]) },
1590 { "r9", offsetof(CPUState, gpr[9]) },
1591 { "r10", offsetof(CPUState, gpr[10]) },
1592 { "r11", offsetof(CPUState, gpr[11]) },
1593 { "r12", offsetof(CPUState, gpr[12]) },
1594 { "r13", offsetof(CPUState, gpr[13]) },
1595 { "r14", offsetof(CPUState, gpr[14]) },
1596 { "r15", offsetof(CPUState, gpr[15]) },
1597 { "r16", offsetof(CPUState, gpr[16]) },
1598 { "r17", offsetof(CPUState, gpr[17]) },
1599 { "r18", offsetof(CPUState, gpr[18]) },
1600 { "r19", offsetof(CPUState, gpr[19]) },
1601 { "r20", offsetof(CPUState, gpr[20]) },
1602 { "r21", offsetof(CPUState, gpr[21]) },
1603 { "r22", offsetof(CPUState, gpr[22]) },
1604 { "r23", offsetof(CPUState, gpr[23]) },
1605 { "r24", offsetof(CPUState, gpr[24]) },
1606 { "r25", offsetof(CPUState, gpr[25]) },
1607 { "r26", offsetof(CPUState, gpr[26]) },
1608 { "r27", offsetof(CPUState, gpr[27]) },
1609 { "r28", offsetof(CPUState, gpr[28]) },
1610 { "r29", offsetof(CPUState, gpr[29]) },
1611 { "r30", offsetof(CPUState, gpr[30]) },
1612 { "r31", offsetof(CPUState, gpr[31]) },
1613 /* Floating point registers */
1614 { "f0", offsetof(CPUState, fpr[0]) },
1615 { "f1", offsetof(CPUState, fpr[1]) },
1616 { "f2", offsetof(CPUState, fpr[2]) },
1617 { "f3", offsetof(CPUState, fpr[3]) },
1618 { "f4", offsetof(CPUState, fpr[4]) },
1619 { "f5", offsetof(CPUState, fpr[5]) },
1620 { "f6", offsetof(CPUState, fpr[6]) },
1621 { "f7", offsetof(CPUState, fpr[7]) },
1622 { "f8", offsetof(CPUState, fpr[8]) },
1623 { "f9", offsetof(CPUState, fpr[9]) },
1624 { "f10", offsetof(CPUState, fpr[10]) },
1625 { "f11", offsetof(CPUState, fpr[11]) },
1626 { "f12", offsetof(CPUState, fpr[12]) },
1627 { "f13", offsetof(CPUState, fpr[13]) },
1628 { "f14", offsetof(CPUState, fpr[14]) },
1629 { "f15", offsetof(CPUState, fpr[15]) },
1630 { "f16", offsetof(CPUState, fpr[16]) },
1631 { "f17", offsetof(CPUState, fpr[17]) },
1632 { "f18", offsetof(CPUState, fpr[18]) },
1633 { "f19", offsetof(CPUState, fpr[19]) },
1634 { "f20", offsetof(CPUState, fpr[20]) },
1635 { "f21", offsetof(CPUState, fpr[21]) },
1636 { "f22", offsetof(CPUState, fpr[22]) },
1637 { "f23", offsetof(CPUState, fpr[23]) },
1638 { "f24", offsetof(CPUState, fpr[24]) },
1639 { "f25", offsetof(CPUState, fpr[25]) },
1640 { "f26", offsetof(CPUState, fpr[26]) },
1641 { "f27", offsetof(CPUState, fpr[27]) },
1642 { "f28", offsetof(CPUState, fpr[28]) },
1643 { "f29", offsetof(CPUState, fpr[29]) },
1644 { "f30", offsetof(CPUState, fpr[30]) },
1645 { "f31", offsetof(CPUState, fpr[31]) },
1646 { "fpscr", offsetof(CPUState, fpscr) },
1647 /* Next instruction pointer */
1648 { "nip|pc", offsetof(CPUState, nip) },
1649 { "lr", offsetof(CPUState, lr) },
1650 { "ctr", offsetof(CPUState, ctr) },
1651 { "decr", 0, &monitor_get_decr, },
1652 { "ccr", 0, &monitor_get_ccr, },
1653 /* Machine state register */
1654 { "msr", 0, &monitor_get_msr, },
1655 { "xer", 0, &monitor_get_xer, },
1656 { "tbu", 0, &monitor_get_tbu, },
1657 { "tbl", 0, &monitor_get_tbl, },
1658 #if defined(TARGET_PPC64)
1659 /* Address space register */
1660 { "asr", offsetof(CPUState, asr) },
1661 #endif
1662 /* Segment registers */
1663 { "sdr1", offsetof(CPUState, sdr1) },
1664 { "sr0", offsetof(CPUState, sr[0]) },
1665 { "sr1", offsetof(CPUState, sr[1]) },
1666 { "sr2", offsetof(CPUState, sr[2]) },
1667 { "sr3", offsetof(CPUState, sr[3]) },
1668 { "sr4", offsetof(CPUState, sr[4]) },
1669 { "sr5", offsetof(CPUState, sr[5]) },
1670 { "sr6", offsetof(CPUState, sr[6]) },
1671 { "sr7", offsetof(CPUState, sr[7]) },
1672 { "sr8", offsetof(CPUState, sr[8]) },
1673 { "sr9", offsetof(CPUState, sr[9]) },
1674 { "sr10", offsetof(CPUState, sr[10]) },
1675 { "sr11", offsetof(CPUState, sr[11]) },
1676 { "sr12", offsetof(CPUState, sr[12]) },
1677 { "sr13", offsetof(CPUState, sr[13]) },
1678 { "sr14", offsetof(CPUState, sr[14]) },
1679 { "sr15", offsetof(CPUState, sr[15]) },
1680 /* Too lazy to put BATs and SPRs ... */
1681 #elif defined(TARGET_SPARC)
1682 { "g0", offsetof(CPUState, gregs[0]) },
1683 { "g1", offsetof(CPUState, gregs[1]) },
1684 { "g2", offsetof(CPUState, gregs[2]) },
1685 { "g3", offsetof(CPUState, gregs[3]) },
1686 { "g4", offsetof(CPUState, gregs[4]) },
1687 { "g5", offsetof(CPUState, gregs[5]) },
1688 { "g6", offsetof(CPUState, gregs[6]) },
1689 { "g7", offsetof(CPUState, gregs[7]) },
1690 { "o0", 0, monitor_get_reg },
1691 { "o1", 1, monitor_get_reg },
1692 { "o2", 2, monitor_get_reg },
1693 { "o3", 3, monitor_get_reg },
1694 { "o4", 4, monitor_get_reg },
1695 { "o5", 5, monitor_get_reg },
1696 { "o6", 6, monitor_get_reg },
1697 { "o7", 7, monitor_get_reg },
1698 { "l0", 8, monitor_get_reg },
1699 { "l1", 9, monitor_get_reg },
1700 { "l2", 10, monitor_get_reg },
1701 { "l3", 11, monitor_get_reg },
1702 { "l4", 12, monitor_get_reg },
1703 { "l5", 13, monitor_get_reg },
1704 { "l6", 14, monitor_get_reg },
1705 { "l7", 15, monitor_get_reg },
1706 { "i0", 16, monitor_get_reg },
1707 { "i1", 17, monitor_get_reg },
1708 { "i2", 18, monitor_get_reg },
1709 { "i3", 19, monitor_get_reg },
1710 { "i4", 20, monitor_get_reg },
1711 { "i5", 21, monitor_get_reg },
1712 { "i6", 22, monitor_get_reg },
1713 { "i7", 23, monitor_get_reg },
1714 { "pc", offsetof(CPUState, pc) },
1715 { "npc", offsetof(CPUState, npc) },
1716 { "y", offsetof(CPUState, y) },
1717 #ifndef TARGET_SPARC64
1718 { "psr", 0, &monitor_get_psr, },
1719 { "wim", offsetof(CPUState, wim) },
1720 #endif
1721 { "tbr", offsetof(CPUState, tbr) },
1722 { "fsr", offsetof(CPUState, fsr) },
1723 { "f0", offsetof(CPUState, fpr[0]) },
1724 { "f1", offsetof(CPUState, fpr[1]) },
1725 { "f2", offsetof(CPUState, fpr[2]) },
1726 { "f3", offsetof(CPUState, fpr[3]) },
1727 { "f4", offsetof(CPUState, fpr[4]) },
1728 { "f5", offsetof(CPUState, fpr[5]) },
1729 { "f6", offsetof(CPUState, fpr[6]) },
1730 { "f7", offsetof(CPUState, fpr[7]) },
1731 { "f8", offsetof(CPUState, fpr[8]) },
1732 { "f9", offsetof(CPUState, fpr[9]) },
1733 { "f10", offsetof(CPUState, fpr[10]) },
1734 { "f11", offsetof(CPUState, fpr[11]) },
1735 { "f12", offsetof(CPUState, fpr[12]) },
1736 { "f13", offsetof(CPUState, fpr[13]) },
1737 { "f14", offsetof(CPUState, fpr[14]) },
1738 { "f15", offsetof(CPUState, fpr[15]) },
1739 { "f16", offsetof(CPUState, fpr[16]) },
1740 { "f17", offsetof(CPUState, fpr[17]) },
1741 { "f18", offsetof(CPUState, fpr[18]) },
1742 { "f19", offsetof(CPUState, fpr[19]) },
1743 { "f20", offsetof(CPUState, fpr[20]) },
1744 { "f21", offsetof(CPUState, fpr[21]) },
1745 { "f22", offsetof(CPUState, fpr[22]) },
1746 { "f23", offsetof(CPUState, fpr[23]) },
1747 { "f24", offsetof(CPUState, fpr[24]) },
1748 { "f25", offsetof(CPUState, fpr[25]) },
1749 { "f26", offsetof(CPUState, fpr[26]) },
1750 { "f27", offsetof(CPUState, fpr[27]) },
1751 { "f28", offsetof(CPUState, fpr[28]) },
1752 { "f29", offsetof(CPUState, fpr[29]) },
1753 { "f30", offsetof(CPUState, fpr[30]) },
1754 { "f31", offsetof(CPUState, fpr[31]) },
1755 #ifdef TARGET_SPARC64
1756 { "f32", offsetof(CPUState, fpr[32]) },
1757 { "f34", offsetof(CPUState, fpr[34]) },
1758 { "f36", offsetof(CPUState, fpr[36]) },
1759 { "f38", offsetof(CPUState, fpr[38]) },
1760 { "f40", offsetof(CPUState, fpr[40]) },
1761 { "f42", offsetof(CPUState, fpr[42]) },
1762 { "f44", offsetof(CPUState, fpr[44]) },
1763 { "f46", offsetof(CPUState, fpr[46]) },
1764 { "f48", offsetof(CPUState, fpr[48]) },
1765 { "f50", offsetof(CPUState, fpr[50]) },
1766 { "f52", offsetof(CPUState, fpr[52]) },
1767 { "f54", offsetof(CPUState, fpr[54]) },
1768 { "f56", offsetof(CPUState, fpr[56]) },
1769 { "f58", offsetof(CPUState, fpr[58]) },
1770 { "f60", offsetof(CPUState, fpr[60]) },
1771 { "f62", offsetof(CPUState, fpr[62]) },
1772 { "asi", offsetof(CPUState, asi) },
1773 { "pstate", offsetof(CPUState, pstate) },
1774 { "cansave", offsetof(CPUState, cansave) },
1775 { "canrestore", offsetof(CPUState, canrestore) },
1776 { "otherwin", offsetof(CPUState, otherwin) },
1777 { "wstate", offsetof(CPUState, wstate) },
1778 { "cleanwin", offsetof(CPUState, cleanwin) },
1779 { "fprs", offsetof(CPUState, fprs) },
1780 #endif
1781 #endif
1782 { NULL },
1785 static void expr_error(const char *fmt)
1787 term_printf(fmt);
1788 term_printf("\n");
1789 longjmp(expr_env, 1);
1792 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1793 static int get_monitor_def(target_long *pval, const char *name)
1795 MonitorDef *md;
1796 void *ptr;
1798 for(md = monitor_defs; md->name != NULL; md++) {
1799 if (compare_cmd(name, md->name)) {
1800 if (md->get_value) {
1801 *pval = md->get_value(md, md->offset);
1802 } else {
1803 CPUState *env = mon_get_cpu();
1804 if (!env)
1805 return -2;
1806 ptr = (uint8_t *)env + md->offset;
1807 switch(md->type) {
1808 case MD_I32:
1809 *pval = *(int32_t *)ptr;
1810 break;
1811 case MD_TLONG:
1812 *pval = *(target_long *)ptr;
1813 break;
1814 default:
1815 *pval = 0;
1816 break;
1819 return 0;
1822 return -1;
1825 static void next(void)
1827 if (pch != '\0') {
1828 pch++;
1829 while (isspace(*pch))
1830 pch++;
1834 static int64_t expr_sum(void);
1836 static int64_t expr_unary(void)
1838 int64_t n;
1839 char *p;
1840 int ret;
1842 switch(*pch) {
1843 case '+':
1844 next();
1845 n = expr_unary();
1846 break;
1847 case '-':
1848 next();
1849 n = -expr_unary();
1850 break;
1851 case '~':
1852 next();
1853 n = ~expr_unary();
1854 break;
1855 case '(':
1856 next();
1857 n = expr_sum();
1858 if (*pch != ')') {
1859 expr_error("')' expected");
1861 next();
1862 break;
1863 case '\'':
1864 pch++;
1865 if (*pch == '\0')
1866 expr_error("character constant expected");
1867 n = *pch;
1868 pch++;
1869 if (*pch != '\'')
1870 expr_error("missing terminating \' character");
1871 next();
1872 break;
1873 case '$':
1875 char buf[128], *q;
1876 target_long reg;
1878 pch++;
1879 q = buf;
1880 while ((*pch >= 'a' && *pch <= 'z') ||
1881 (*pch >= 'A' && *pch <= 'Z') ||
1882 (*pch >= '0' && *pch <= '9') ||
1883 *pch == '_' || *pch == '.') {
1884 if ((q - buf) < sizeof(buf) - 1)
1885 *q++ = *pch;
1886 pch++;
1888 while (isspace(*pch))
1889 pch++;
1890 *q = 0;
1891 ret = get_monitor_def(&reg, buf);
1892 if (ret == -1)
1893 expr_error("unknown register");
1894 else if (ret == -2)
1895 expr_error("no cpu defined");
1896 n = reg;
1898 break;
1899 case '\0':
1900 expr_error("unexpected end of expression");
1901 n = 0;
1902 break;
1903 default:
1904 #if TARGET_PHYS_ADDR_BITS > 32
1905 n = strtoull(pch, &p, 0);
1906 #else
1907 n = strtoul(pch, &p, 0);
1908 #endif
1909 if (pch == p) {
1910 expr_error("invalid char in expression");
1912 pch = p;
1913 while (isspace(*pch))
1914 pch++;
1915 break;
1917 return n;
1921 static int64_t expr_prod(void)
1923 int64_t val, val2;
1924 int op;
1926 val = expr_unary();
1927 for(;;) {
1928 op = *pch;
1929 if (op != '*' && op != '/' && op != '%')
1930 break;
1931 next();
1932 val2 = expr_unary();
1933 switch(op) {
1934 default:
1935 case '*':
1936 val *= val2;
1937 break;
1938 case '/':
1939 case '%':
1940 if (val2 == 0)
1941 expr_error("division by zero");
1942 if (op == '/')
1943 val /= val2;
1944 else
1945 val %= val2;
1946 break;
1949 return val;
1952 static int64_t expr_logic(void)
1954 int64_t val, val2;
1955 int op;
1957 val = expr_prod();
1958 for(;;) {
1959 op = *pch;
1960 if (op != '&' && op != '|' && op != '^')
1961 break;
1962 next();
1963 val2 = expr_prod();
1964 switch(op) {
1965 default:
1966 case '&':
1967 val &= val2;
1968 break;
1969 case '|':
1970 val |= val2;
1971 break;
1972 case '^':
1973 val ^= val2;
1974 break;
1977 return val;
1980 static int64_t expr_sum(void)
1982 int64_t val, val2;
1983 int op;
1985 val = expr_logic();
1986 for(;;) {
1987 op = *pch;
1988 if (op != '+' && op != '-')
1989 break;
1990 next();
1991 val2 = expr_logic();
1992 if (op == '+')
1993 val += val2;
1994 else
1995 val -= val2;
1997 return val;
2000 static int get_expr(int64_t *pval, const char **pp)
2002 pch = *pp;
2003 if (setjmp(expr_env)) {
2004 *pp = pch;
2005 return -1;
2007 while (isspace(*pch))
2008 pch++;
2009 *pval = expr_sum();
2010 *pp = pch;
2011 return 0;
2014 static int get_str(char *buf, int buf_size, const char **pp)
2016 const char *p;
2017 char *q;
2018 int c;
2020 q = buf;
2021 p = *pp;
2022 while (isspace(*p))
2023 p++;
2024 if (*p == '\0') {
2025 fail:
2026 *q = '\0';
2027 *pp = p;
2028 return -1;
2030 if (*p == '\"') {
2031 p++;
2032 while (*p != '\0' && *p != '\"') {
2033 if (*p == '\\') {
2034 p++;
2035 c = *p++;
2036 switch(c) {
2037 case 'n':
2038 c = '\n';
2039 break;
2040 case 'r':
2041 c = '\r';
2042 break;
2043 case '\\':
2044 case '\'':
2045 case '\"':
2046 break;
2047 default:
2048 qemu_printf("unsupported escape code: '\\%c'\n", c);
2049 goto fail;
2051 if ((q - buf) < buf_size - 1) {
2052 *q++ = c;
2054 } else {
2055 if ((q - buf) < buf_size - 1) {
2056 *q++ = *p;
2058 p++;
2061 if (*p != '\"') {
2062 qemu_printf("unterminated string\n");
2063 goto fail;
2065 p++;
2066 } else {
2067 while (*p != '\0' && !isspace(*p)) {
2068 if ((q - buf) < buf_size - 1) {
2069 *q++ = *p;
2071 p++;
2074 *q = '\0';
2075 *pp = p;
2076 return 0;
2079 static int default_fmt_format = 'x';
2080 static int default_fmt_size = 4;
2082 #define MAX_ARGS 16
2084 static void monitor_handle_command(const char *cmdline)
2086 const char *p, *pstart, *typestr;
2087 char *q;
2088 int c, nb_args, len, i, has_arg;
2089 term_cmd_t *cmd;
2090 char cmdname[256];
2091 char buf[1024];
2092 void *str_allocated[MAX_ARGS];
2093 void *args[MAX_ARGS];
2095 #ifdef DEBUG
2096 term_printf("command='%s'\n", cmdline);
2097 #endif
2099 /* extract the command name */
2100 p = cmdline;
2101 q = cmdname;
2102 while (isspace(*p))
2103 p++;
2104 if (*p == '\0')
2105 return;
2106 pstart = p;
2107 while (*p != '\0' && *p != '/' && !isspace(*p))
2108 p++;
2109 len = p - pstart;
2110 if (len > sizeof(cmdname) - 1)
2111 len = sizeof(cmdname) - 1;
2112 memcpy(cmdname, pstart, len);
2113 cmdname[len] = '\0';
2115 /* find the command */
2116 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2117 if (compare_cmd(cmdname, cmd->name))
2118 goto found;
2120 term_printf("unknown command: '%s'\n", cmdname);
2121 return;
2122 found:
2124 for(i = 0; i < MAX_ARGS; i++)
2125 str_allocated[i] = NULL;
2127 /* parse the parameters */
2128 typestr = cmd->args_type;
2129 nb_args = 0;
2130 for(;;) {
2131 c = *typestr;
2132 if (c == '\0')
2133 break;
2134 typestr++;
2135 switch(c) {
2136 case 'F':
2137 case 'B':
2138 case 's':
2140 int ret;
2141 char *str;
2143 while (isspace(*p))
2144 p++;
2145 if (*typestr == '?') {
2146 typestr++;
2147 if (*p == '\0') {
2148 /* no optional string: NULL argument */
2149 str = NULL;
2150 goto add_str;
2153 ret = get_str(buf, sizeof(buf), &p);
2154 if (ret < 0) {
2155 switch(c) {
2156 case 'F':
2157 term_printf("%s: filename expected\n", cmdname);
2158 break;
2159 case 'B':
2160 term_printf("%s: block device name expected\n", cmdname);
2161 break;
2162 default:
2163 term_printf("%s: string expected\n", cmdname);
2164 break;
2166 goto fail;
2168 str = qemu_malloc(strlen(buf) + 1);
2169 strcpy(str, buf);
2170 str_allocated[nb_args] = str;
2171 add_str:
2172 if (nb_args >= MAX_ARGS) {
2173 error_args:
2174 term_printf("%s: too many arguments\n", cmdname);
2175 goto fail;
2177 args[nb_args++] = str;
2179 break;
2180 case '/':
2182 int count, format, size;
2184 while (isspace(*p))
2185 p++;
2186 if (*p == '/') {
2187 /* format found */
2188 p++;
2189 count = 1;
2190 if (isdigit(*p)) {
2191 count = 0;
2192 while (isdigit(*p)) {
2193 count = count * 10 + (*p - '0');
2194 p++;
2197 size = -1;
2198 format = -1;
2199 for(;;) {
2200 switch(*p) {
2201 case 'o':
2202 case 'd':
2203 case 'u':
2204 case 'x':
2205 case 'i':
2206 case 'c':
2207 format = *p++;
2208 break;
2209 case 'b':
2210 size = 1;
2211 p++;
2212 break;
2213 case 'h':
2214 size = 2;
2215 p++;
2216 break;
2217 case 'w':
2218 size = 4;
2219 p++;
2220 break;
2221 case 'g':
2222 case 'L':
2223 size = 8;
2224 p++;
2225 break;
2226 default:
2227 goto next;
2230 next:
2231 if (*p != '\0' && !isspace(*p)) {
2232 term_printf("invalid char in format: '%c'\n", *p);
2233 goto fail;
2235 if (format < 0)
2236 format = default_fmt_format;
2237 if (format != 'i') {
2238 /* for 'i', not specifying a size gives -1 as size */
2239 if (size < 0)
2240 size = default_fmt_size;
2242 default_fmt_size = size;
2243 default_fmt_format = format;
2244 } else {
2245 count = 1;
2246 format = default_fmt_format;
2247 if (format != 'i') {
2248 size = default_fmt_size;
2249 } else {
2250 size = -1;
2253 if (nb_args + 3 > MAX_ARGS)
2254 goto error_args;
2255 args[nb_args++] = (void*)(long)count;
2256 args[nb_args++] = (void*)(long)format;
2257 args[nb_args++] = (void*)(long)size;
2259 break;
2260 case 'i':
2261 case 'l':
2263 int64_t val;
2265 while (isspace(*p))
2266 p++;
2267 if (*typestr == '?' || *typestr == '.') {
2268 if (*typestr == '?') {
2269 if (*p == '\0')
2270 has_arg = 0;
2271 else
2272 has_arg = 1;
2273 } else {
2274 if (*p == '.') {
2275 p++;
2276 while (isspace(*p))
2277 p++;
2278 has_arg = 1;
2279 } else {
2280 has_arg = 0;
2283 typestr++;
2284 if (nb_args >= MAX_ARGS)
2285 goto error_args;
2286 args[nb_args++] = (void *)(long)has_arg;
2287 if (!has_arg) {
2288 if (nb_args >= MAX_ARGS)
2289 goto error_args;
2290 val = -1;
2291 goto add_num;
2294 if (get_expr(&val, &p))
2295 goto fail;
2296 add_num:
2297 if (c == 'i') {
2298 if (nb_args >= MAX_ARGS)
2299 goto error_args;
2300 args[nb_args++] = (void *)(long)val;
2301 } else {
2302 if ((nb_args + 1) >= MAX_ARGS)
2303 goto error_args;
2304 #if TARGET_PHYS_ADDR_BITS > 32
2305 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2306 #else
2307 args[nb_args++] = (void *)0;
2308 #endif
2309 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2312 break;
2313 case '-':
2315 int has_option;
2316 /* option */
2318 c = *typestr++;
2319 if (c == '\0')
2320 goto bad_type;
2321 while (isspace(*p))
2322 p++;
2323 has_option = 0;
2324 if (*p == '-') {
2325 p++;
2326 if (*p != c) {
2327 term_printf("%s: unsupported option -%c\n",
2328 cmdname, *p);
2329 goto fail;
2331 p++;
2332 has_option = 1;
2334 if (nb_args >= MAX_ARGS)
2335 goto error_args;
2336 args[nb_args++] = (void *)(long)has_option;
2338 break;
2339 default:
2340 bad_type:
2341 term_printf("%s: unknown type '%c'\n", cmdname, c);
2342 goto fail;
2345 /* check that all arguments were parsed */
2346 while (isspace(*p))
2347 p++;
2348 if (*p != '\0') {
2349 term_printf("%s: extraneous characters at the end of line\n",
2350 cmdname);
2351 goto fail;
2354 switch(nb_args) {
2355 case 0:
2356 cmd->handler();
2357 break;
2358 case 1:
2359 cmd->handler(args[0]);
2360 break;
2361 case 2:
2362 cmd->handler(args[0], args[1]);
2363 break;
2364 case 3:
2365 cmd->handler(args[0], args[1], args[2]);
2366 break;
2367 case 4:
2368 cmd->handler(args[0], args[1], args[2], args[3]);
2369 break;
2370 case 5:
2371 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2372 break;
2373 case 6:
2374 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2375 break;
2376 case 7:
2377 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2378 break;
2379 default:
2380 term_printf("unsupported number of arguments: %d\n", nb_args);
2381 goto fail;
2383 fail:
2384 for(i = 0; i < MAX_ARGS; i++)
2385 qemu_free(str_allocated[i]);
2386 return;
2389 static void cmd_completion(const char *name, const char *list)
2391 const char *p, *pstart;
2392 char cmd[128];
2393 int len;
2395 p = list;
2396 for(;;) {
2397 pstart = p;
2398 p = strchr(p, '|');
2399 if (!p)
2400 p = pstart + strlen(pstart);
2401 len = p - pstart;
2402 if (len > sizeof(cmd) - 2)
2403 len = sizeof(cmd) - 2;
2404 memcpy(cmd, pstart, len);
2405 cmd[len] = '\0';
2406 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2407 add_completion(cmd);
2409 if (*p == '\0')
2410 break;
2411 p++;
2415 static void file_completion(const char *input)
2417 DIR *ffs;
2418 struct dirent *d;
2419 char path[1024];
2420 char file[1024], file_prefix[1024];
2421 int input_path_len;
2422 const char *p;
2424 p = strrchr(input, '/');
2425 if (!p) {
2426 input_path_len = 0;
2427 pstrcpy(file_prefix, sizeof(file_prefix), input);
2428 strcpy(path, ".");
2429 } else {
2430 input_path_len = p - input + 1;
2431 memcpy(path, input, input_path_len);
2432 if (input_path_len > sizeof(path) - 1)
2433 input_path_len = sizeof(path) - 1;
2434 path[input_path_len] = '\0';
2435 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2437 #ifdef DEBUG_COMPLETION
2438 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2439 #endif
2440 ffs = opendir(path);
2441 if (!ffs)
2442 return;
2443 for(;;) {
2444 struct stat sb;
2445 d = readdir(ffs);
2446 if (!d)
2447 break;
2448 if (strstart(d->d_name, file_prefix, NULL)) {
2449 memcpy(file, input, input_path_len);
2450 strcpy(file + input_path_len, d->d_name);
2451 /* stat the file to find out if it's a directory.
2452 * In that case add a slash to speed up typing long paths
2454 stat(file, &sb);
2455 if(S_ISDIR(sb.st_mode))
2456 strcat(file, "/");
2457 add_completion(file);
2460 closedir(ffs);
2463 static void block_completion_it(void *opaque, const char *name)
2465 const char *input = opaque;
2467 if (input[0] == '\0' ||
2468 !strncmp(name, (char *)input, strlen(input))) {
2469 add_completion(name);
2473 /* NOTE: this parser is an approximate form of the real command parser */
2474 static void parse_cmdline(const char *cmdline,
2475 int *pnb_args, char **args)
2477 const char *p;
2478 int nb_args, ret;
2479 char buf[1024];
2481 p = cmdline;
2482 nb_args = 0;
2483 for(;;) {
2484 while (isspace(*p))
2485 p++;
2486 if (*p == '\0')
2487 break;
2488 if (nb_args >= MAX_ARGS)
2489 break;
2490 ret = get_str(buf, sizeof(buf), &p);
2491 args[nb_args] = qemu_strdup(buf);
2492 nb_args++;
2493 if (ret < 0)
2494 break;
2496 *pnb_args = nb_args;
2499 void readline_find_completion(const char *cmdline)
2501 const char *cmdname;
2502 char *args[MAX_ARGS];
2503 int nb_args, i, len;
2504 const char *ptype, *str;
2505 term_cmd_t *cmd;
2506 const KeyDef *key;
2508 parse_cmdline(cmdline, &nb_args, args);
2509 #ifdef DEBUG_COMPLETION
2510 for(i = 0; i < nb_args; i++) {
2511 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2513 #endif
2515 /* if the line ends with a space, it means we want to complete the
2516 next arg */
2517 len = strlen(cmdline);
2518 if (len > 0 && isspace(cmdline[len - 1])) {
2519 if (nb_args >= MAX_ARGS)
2520 return;
2521 args[nb_args++] = qemu_strdup("");
2523 if (nb_args <= 1) {
2524 /* command completion */
2525 if (nb_args == 0)
2526 cmdname = "";
2527 else
2528 cmdname = args[0];
2529 completion_index = strlen(cmdname);
2530 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2531 cmd_completion(cmdname, cmd->name);
2533 } else {
2534 /* find the command */
2535 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2536 if (compare_cmd(args[0], cmd->name))
2537 goto found;
2539 return;
2540 found:
2541 ptype = cmd->args_type;
2542 for(i = 0; i < nb_args - 2; i++) {
2543 if (*ptype != '\0') {
2544 ptype++;
2545 while (*ptype == '?')
2546 ptype++;
2549 str = args[nb_args - 1];
2550 switch(*ptype) {
2551 case 'F':
2552 /* file completion */
2553 completion_index = strlen(str);
2554 file_completion(str);
2555 break;
2556 case 'B':
2557 /* block device name completion */
2558 completion_index = strlen(str);
2559 bdrv_iterate(block_completion_it, (void *)str);
2560 break;
2561 case 's':
2562 /* XXX: more generic ? */
2563 if (!strcmp(cmd->name, "info")) {
2564 completion_index = strlen(str);
2565 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2566 cmd_completion(str, cmd->name);
2568 } else if (!strcmp(cmd->name, "sendkey")) {
2569 completion_index = strlen(str);
2570 for(key = key_defs; key->name != NULL; key++) {
2571 cmd_completion(str, key->name);
2574 break;
2575 default:
2576 break;
2579 for(i = 0; i < nb_args; i++)
2580 qemu_free(args[i]);
2583 static int term_can_read(void *opaque)
2585 return 128;
2588 static void term_read(void *opaque, const uint8_t *buf, int size)
2590 int i;
2591 for(i = 0; i < size; i++)
2592 readline_handle_byte(buf[i]);
2595 static int monitor_suspended;
2597 void monitor_suspend(void)
2599 monitor_suspended = 1;
2602 void monitor_resume(void)
2604 monitor_suspended = 0;
2605 monitor_start_input();
2608 static void monitor_start_input(void);
2610 static void monitor_handle_command1(void *opaque, const char *cmdline)
2612 monitor_handle_command(cmdline);
2613 if (!monitor_suspended)
2614 monitor_start_input();
2617 static void monitor_start_input(void)
2619 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2622 static void term_event(void *opaque, int event)
2624 if (event != CHR_EVENT_RESET)
2625 return;
2627 if (!hide_banner)
2628 term_printf("QEMU %s monitor - type 'help' for more information\n",
2629 QEMU_VERSION);
2630 monitor_start_input();
2633 static int is_first_init = 1;
2635 void monitor_init(CharDriverState *hd, int show_banner)
2637 int i;
2639 if (is_first_init) {
2640 for (i = 0; i < MAX_MON; i++) {
2641 monitor_hd[i] = NULL;
2643 is_first_init = 0;
2645 for (i = 0; i < MAX_MON; i++) {
2646 if (monitor_hd[i] == NULL) {
2647 monitor_hd[i] = hd;
2648 break;
2652 hide_banner = !show_banner;
2654 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2657 /* XXX: use threads ? */
2658 /* modal monitor readline */
2659 static int monitor_readline_started;
2660 static char *monitor_readline_buf;
2661 static int monitor_readline_buf_size;
2663 static void monitor_readline_cb(void *opaque, const char *input)
2665 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2666 monitor_readline_started = 0;
2669 void monitor_readline(const char *prompt, int is_password,
2670 char *buf, int buf_size)
2672 int i;
2674 if (is_password) {
2675 for (i = 0; i < MAX_MON; i++)
2676 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2677 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2679 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2680 monitor_readline_buf = buf;
2681 monitor_readline_buf_size = buf_size;
2682 monitor_readline_started = 1;
2683 while (monitor_readline_started) {
2684 main_loop_wait(10);