Export qemu_system_reset() for qemu-kvm.c
[qemu-kvm/fedora.git] / monitor.c
blobde8d1ea896c5185e700f8b5567fdae8cf486b0f6
1 /*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
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 static CharDriverState *monitor_hd;
60 static int hide_banner;
62 static term_cmd_t term_cmds[];
63 static term_cmd_t info_cmds[];
65 static char term_outbuf[1024];
66 static int term_outbuf_index;
68 static void monitor_start_input(void);
70 CPUState *mon_cpu = NULL;
72 void term_flush(void)
74 if (term_outbuf_index > 0) {
75 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
76 term_outbuf_index = 0;
80 /* flush at every end of line or if the buffer is full */
81 void term_puts(const char *str)
83 int c;
84 for(;;) {
85 c = *str++;
86 if (c == '\0')
87 break;
88 if (c == '\n')
89 term_outbuf[term_outbuf_index++] = '\r';
90 term_outbuf[term_outbuf_index++] = c;
91 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
92 c == '\n')
93 term_flush();
97 void term_vprintf(const char *fmt, va_list ap)
99 char buf[4096];
100 vsnprintf(buf, sizeof(buf), fmt, ap);
101 term_puts(buf);
104 void term_printf(const char *fmt, ...)
106 va_list ap;
107 va_start(ap, fmt);
108 term_vprintf(fmt, ap);
109 va_end(ap);
112 void term_print_filename(const char *filename)
114 int i;
116 for (i = 0; filename[i]; i++) {
117 switch (filename[i]) {
118 case ' ':
119 case '"':
120 case '\\':
121 term_printf("\\%c", filename[i]);
122 break;
123 case '\t':
124 term_printf("\\t");
125 break;
126 case '\r':
127 term_printf("\\r");
128 break;
129 case '\n':
130 term_printf("\\n");
131 break;
132 default:
133 term_printf("%c", filename[i]);
134 break;
139 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
141 va_list ap;
142 va_start(ap, fmt);
143 term_vprintf(fmt, ap);
144 va_end(ap);
145 return 0;
148 static int compare_cmd(const char *name, const char *list)
150 const char *p, *pstart;
151 int len;
152 len = strlen(name);
153 p = list;
154 for(;;) {
155 pstart = p;
156 p = strchr(p, '|');
157 if (!p)
158 p = pstart + strlen(pstart);
159 if ((p - pstart) == len && !memcmp(pstart, name, len))
160 return 1;
161 if (*p == '\0')
162 break;
163 p++;
165 return 0;
168 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
170 term_cmd_t *cmd;
172 for(cmd = cmds; cmd->name != NULL; cmd++) {
173 if (!name || !strcmp(name, cmd->name))
174 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
178 static void help_cmd(const char *name)
180 if (name && !strcmp(name, "info")) {
181 help_cmd1(info_cmds, "info ", NULL);
182 } else {
183 help_cmd1(term_cmds, "", name);
184 if (name && !strcmp(name, "log")) {
185 CPULogItem *item;
186 term_printf("Log items (comma separated):\n");
187 term_printf("%-10s %s\n", "none", "remove all logs");
188 for(item = cpu_log_items; item->mask != 0; item++) {
189 term_printf("%-10s %s\n", item->name, item->help);
195 static void do_help(const char *name)
197 help_cmd(name);
200 static void do_commit(const char *device)
202 int i, all_devices;
204 all_devices = !strcmp(device, "all");
205 for (i = 0; i < MAX_DISKS; i++) {
206 if (bs_table[i]) {
207 if (all_devices ||
208 !strcmp(bdrv_get_device_name(bs_table[i]), device))
209 bdrv_commit(bs_table[i]);
214 static void do_info(const char *item)
216 term_cmd_t *cmd;
218 if (!item)
219 goto help;
220 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
221 if (compare_cmd(item, cmd->name))
222 goto found;
224 help:
225 help_cmd("info");
226 return;
227 found:
228 cmd->handler();
231 static void do_info_version(void)
233 term_printf("%s\n", QEMU_VERSION);
236 static void do_info_block(void)
238 bdrv_info();
241 /* get the current CPU defined by the user */
242 int mon_set_cpu(int cpu_index)
244 CPUState *env;
246 for(env = first_cpu; env != NULL; env = env->next_cpu) {
247 if (env->cpu_index == cpu_index) {
248 mon_cpu = env;
249 return 0;
252 return -1;
255 CPUState *mon_get_cpu(void)
257 if (!mon_cpu) {
258 mon_set_cpu(0);
261 #ifdef USE_KVM
262 kvm_save_registers(mon_cpu);
263 #endif
265 return mon_cpu;
268 static void do_info_registers(void)
270 CPUState *env;
271 env = mon_get_cpu();
272 if (!env)
273 return;
274 #ifdef TARGET_I386
275 cpu_dump_state(env, NULL, monitor_fprintf,
276 X86_DUMP_FPU);
277 #else
278 cpu_dump_state(env, NULL, monitor_fprintf,
280 #endif
283 static void do_info_cpus(void)
285 CPUState *env;
287 /* just to set the default cpu if not already done */
288 mon_get_cpu();
290 for(env = first_cpu; env != NULL; env = env->next_cpu) {
291 term_printf("%c CPU #%d:",
292 (env == mon_cpu) ? '*' : ' ',
293 env->cpu_index);
294 #if defined(TARGET_I386)
295 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
296 if (env->hflags & HF_HALTED_MASK)
297 term_printf(" (halted)");
298 #elif defined(TARGET_PPC)
299 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
300 if (env->halted)
301 term_printf(" (halted)");
302 #elif defined(TARGET_SPARC)
303 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
304 if (env->halted)
305 term_printf(" (halted)");
306 #endif
307 term_printf("\n");
311 static void do_cpu_set(int index)
313 if (mon_set_cpu(index) < 0)
314 term_printf("Invalid CPU index\n");
317 static void do_info_jit(void)
319 dump_exec_info(NULL, monitor_fprintf);
322 static void do_info_history (void)
324 int i;
325 const char *str;
327 i = 0;
328 for(;;) {
329 str = readline_get_history(i);
330 if (!str)
331 break;
332 term_printf("%d: '%s'\n", i, str);
333 i++;
337 static void do_quit(void)
339 exit(0);
342 static int eject_device(BlockDriverState *bs, int force)
344 if (bdrv_is_inserted(bs)) {
345 if (!force) {
346 if (!bdrv_is_removable(bs)) {
347 term_printf("device is not removable\n");
348 return -1;
350 if (bdrv_is_locked(bs)) {
351 term_printf("device is locked\n");
352 return -1;
355 bdrv_close(bs);
357 return 0;
360 static void do_eject(int force, const char *filename)
362 BlockDriverState *bs;
364 bs = bdrv_find(filename);
365 if (!bs) {
366 term_printf("device not found\n");
367 return;
369 eject_device(bs, force);
372 #define USAGE_STR "Usage: read_disk_io hd[a/b/c/d]\n"
373 DiskIOStatistics vmdk_io_statistics(BlockDriverState *bs);
375 static void do_io_statistics(const char *hdx)
377 DiskIOStatistics io[4];
379 if ((strcmp(hdx,"hda") != 0) && (strcmp(hdx,"hdb") != 0) &&
380 (strcmp(hdx,"hdc") != 0) && (strcmp(hdx,"hdd") != 0)) {
381 term_printf(USAGE_STR);
382 return;
385 switch (hdx[2]) {
386 case 'a':
387 term_printf("---------- hda io statistics ----------\n");
388 io[0] = vmdk_io_statistics(bs_table[0]);
389 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
390 io[0].read_byte_counter, io[0].write_byte_counter);
391 break;
392 case 'b':
393 term_printf("---------- hdb io statistics ----------\n");
394 if (bs_table[1]) {
395 io[1] = vmdk_io_statistics(bs_table[1]);
396 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
397 io[1].read_byte_counter, io[1].write_byte_counter);
398 }else {
399 term_printf("hdb not exist\n");
401 break;
402 case 'c':
403 term_printf("---------- hdc io statistics ----------\n");
404 if (bs_table[2]) {
405 io[2] = vmdk_io_statistics(bs_table[2]);
406 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
407 io[2].read_byte_counter, io[2].write_byte_counter);
408 }else {
409 term_printf("hdc not exist\n");
411 break;
412 case 'd':
413 term_printf("---------- hdd io statistics ----------\n");
414 if (bs_table[3]) {
415 io[3] = vmdk_io_statistics(bs_table[3]);
416 term_printf("read: %" PRIu64 " \nwrite: %" PRIu64 " \n",
417 io[3].read_byte_counter, io[3].write_byte_counter);
418 }else {
419 term_printf("hdd not exist\n");
421 break;
425 static void do_change(const char *device, const char *filename)
427 BlockDriverState *bs;
428 int i;
429 char password[256];
431 bs = bdrv_find(device);
432 if (!bs) {
433 term_printf("device not found\n");
434 return;
436 if (eject_device(bs, 0) < 0)
437 return;
438 bdrv_open(bs, filename, 0);
439 if (bdrv_is_encrypted(bs)) {
440 term_printf("%s is encrypted.\n", device);
441 for(i = 0; i < 3; i++) {
442 monitor_readline("Password: ", 1, password, sizeof(password));
443 if (bdrv_set_key(bs, password) == 0)
444 break;
445 term_printf("invalid password\n");
450 static void do_screen_dump(const char *filename)
452 vga_hw_screen_dump(filename);
455 static void do_log(const char *items)
457 int mask;
459 if (!strcmp(items, "none")) {
460 mask = 0;
461 } else {
462 mask = cpu_str_to_log_mask(items);
463 if (!mask) {
464 help_cmd("log");
465 return;
468 cpu_set_log(mask);
471 static void do_stop(void)
473 vm_stop(EXCP_INTERRUPT);
476 static void do_cont(void)
478 vm_start();
481 #ifdef CONFIG_GDBSTUB
482 static void do_gdbserver(int has_port, int port)
484 if (!has_port)
485 port = DEFAULT_GDBSTUB_PORT;
486 if (gdbserver_start_port(port) < 0) {
487 qemu_printf("Could not open gdbserver socket on port %d\n", port);
488 } else {
489 qemu_printf("Waiting gdb connection on port %d\n", port);
492 #endif
494 static void term_printc(int c)
496 term_printf("'");
497 switch(c) {
498 case '\'':
499 term_printf("\\'");
500 break;
501 case '\\':
502 term_printf("\\\\");
503 break;
504 case '\n':
505 term_printf("\\n");
506 break;
507 case '\r':
508 term_printf("\\r");
509 break;
510 default:
511 if (c >= 32 && c <= 126) {
512 term_printf("%c", c);
513 } else {
514 term_printf("\\x%02x", c);
516 break;
518 term_printf("'");
521 static void memory_dump(int count, int format, int wsize,
522 target_ulong addr, int is_physical)
524 CPUState *env;
525 int nb_per_line, l, line_size, i, max_digits, len;
526 uint8_t buf[16];
527 uint64_t v;
529 if (format == 'i') {
530 int flags;
531 flags = 0;
532 env = mon_get_cpu();
533 if (!env && !is_physical)
534 return;
535 #ifdef TARGET_I386
536 if (wsize == 2) {
537 flags = 1;
538 } else if (wsize == 4) {
539 flags = 0;
540 } else {
541 /* as default we use the current CS size */
542 flags = 0;
543 if (env) {
544 #ifdef TARGET_X86_64
545 if ((env->efer & MSR_EFER_LMA) &&
546 (env->segs[R_CS].flags & DESC_L_MASK))
547 flags = 2;
548 else
549 #endif
550 if (!(env->segs[R_CS].flags & DESC_B_MASK))
551 flags = 1;
554 #endif
555 monitor_disas(env, addr, count, is_physical, flags);
556 return;
559 len = wsize * count;
560 if (wsize == 1)
561 line_size = 8;
562 else
563 line_size = 16;
564 nb_per_line = line_size / wsize;
565 max_digits = 0;
567 switch(format) {
568 case 'o':
569 max_digits = (wsize * 8 + 2) / 3;
570 break;
571 default:
572 case 'x':
573 max_digits = (wsize * 8) / 4;
574 break;
575 case 'u':
576 case 'd':
577 max_digits = (wsize * 8 * 10 + 32) / 33;
578 break;
579 case 'c':
580 wsize = 1;
581 break;
584 while (len > 0) {
585 term_printf(TARGET_FMT_lx ":", addr);
586 l = len;
587 if (l > line_size)
588 l = line_size;
589 if (is_physical) {
590 cpu_physical_memory_rw(addr, buf, l, 0);
591 } else {
592 env = mon_get_cpu();
593 if (!env)
594 break;
595 cpu_memory_rw_debug(env, addr, buf, l, 0);
597 i = 0;
598 while (i < l) {
599 switch(wsize) {
600 default:
601 case 1:
602 v = ldub_raw(buf + i);
603 break;
604 case 2:
605 v = lduw_raw(buf + i);
606 break;
607 case 4:
608 v = (uint32_t)ldl_raw(buf + i);
609 break;
610 case 8:
611 v = ldq_raw(buf + i);
612 break;
614 term_printf(" ");
615 switch(format) {
616 case 'o':
617 term_printf("%#*" PRIo64, max_digits, v);
618 break;
619 case 'x':
620 term_printf("0x%0*" PRIx64, max_digits, v);
621 break;
622 case 'u':
623 term_printf("%*" PRIu64, max_digits, v);
624 break;
625 case 'd':
626 term_printf("%*" PRId64, max_digits, v);
627 break;
628 case 'c':
629 term_printc(v);
630 break;
632 i += wsize;
634 term_printf("\n");
635 addr += l;
636 len -= l;
640 #if TARGET_LONG_BITS == 64
641 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
642 #else
643 #define GET_TLONG(h, l) (l)
644 #endif
646 static void do_memory_dump(int count, int format, int size,
647 uint32_t addrh, uint32_t addrl)
649 target_long addr = GET_TLONG(addrh, addrl);
650 memory_dump(count, format, size, addr, 0);
653 static void do_physical_memory_dump(int count, int format, int size,
654 uint32_t addrh, uint32_t addrl)
657 target_long addr = GET_TLONG(addrh, addrl);
658 memory_dump(count, format, size, addr, 1);
661 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
663 target_long val = GET_TLONG(valh, vall);
664 #if TARGET_LONG_BITS == 32
665 switch(format) {
666 case 'o':
667 term_printf("%#o", val);
668 break;
669 case 'x':
670 term_printf("%#x", val);
671 break;
672 case 'u':
673 term_printf("%u", val);
674 break;
675 default:
676 case 'd':
677 term_printf("%d", val);
678 break;
679 case 'c':
680 term_printc(val);
681 break;
683 #else
684 switch(format) {
685 case 'o':
686 term_printf("%#" PRIo64, val);
687 break;
688 case 'x':
689 term_printf("%#" PRIx64, val);
690 break;
691 case 'u':
692 term_printf("%" PRIu64, val);
693 break;
694 default:
695 case 'd':
696 term_printf("%" PRId64, val);
697 break;
698 case 'c':
699 term_printc(val);
700 break;
702 #endif
703 term_printf("\n");
706 static void do_memory_save(unsigned int valh, unsigned int vall,
707 uint32_t size, const char *filename)
709 FILE *f;
710 target_long addr = GET_TLONG(valh, vall);
711 uint32_t l;
712 CPUState *env;
713 uint8_t buf[1024];
715 env = mon_get_cpu();
716 if (!env)
717 return;
719 f = fopen(filename, "wb");
720 if (!f) {
721 term_printf("could not open '%s'\n", filename);
722 return;
724 while (size != 0) {
725 l = sizeof(buf);
726 if (l > size)
727 l = size;
728 cpu_memory_rw_debug(env, addr, buf, l, 0);
729 fwrite(buf, 1, l, f);
730 addr += l;
731 size -= l;
733 fclose(f);
736 static void do_sum(uint32_t start, uint32_t size)
738 uint32_t addr;
739 uint8_t buf[1];
740 uint16_t sum;
742 sum = 0;
743 for(addr = start; addr < (start + size); addr++) {
744 cpu_physical_memory_rw(addr, buf, 1, 0);
745 /* BSD sum algorithm ('sum' Unix command) */
746 sum = (sum >> 1) | (sum << 15);
747 sum += buf[0];
749 term_printf("%05d\n", sum);
752 typedef struct {
753 int keycode;
754 const char *name;
755 } KeyDef;
757 static const KeyDef key_defs[] = {
758 { 0x2a, "shift" },
759 { 0x36, "shift_r" },
761 { 0x38, "alt" },
762 { 0xb8, "alt_r" },
763 { 0x1d, "ctrl" },
764 { 0x9d, "ctrl_r" },
766 { 0xdd, "menu" },
768 { 0x01, "esc" },
770 { 0x02, "1" },
771 { 0x03, "2" },
772 { 0x04, "3" },
773 { 0x05, "4" },
774 { 0x06, "5" },
775 { 0x07, "6" },
776 { 0x08, "7" },
777 { 0x09, "8" },
778 { 0x0a, "9" },
779 { 0x0b, "0" },
780 { 0x0c, "minus" },
781 { 0x0d, "equal" },
782 { 0x0e, "backspace" },
784 { 0x0f, "tab" },
785 { 0x10, "q" },
786 { 0x11, "w" },
787 { 0x12, "e" },
788 { 0x13, "r" },
789 { 0x14, "t" },
790 { 0x15, "y" },
791 { 0x16, "u" },
792 { 0x17, "i" },
793 { 0x18, "o" },
794 { 0x19, "p" },
796 { 0x1c, "ret" },
798 { 0x1e, "a" },
799 { 0x1f, "s" },
800 { 0x20, "d" },
801 { 0x21, "f" },
802 { 0x22, "g" },
803 { 0x23, "h" },
804 { 0x24, "j" },
805 { 0x25, "k" },
806 { 0x26, "l" },
808 { 0x2c, "z" },
809 { 0x2d, "x" },
810 { 0x2e, "c" },
811 { 0x2f, "v" },
812 { 0x30, "b" },
813 { 0x31, "n" },
814 { 0x32, "m" },
816 { 0x39, "spc" },
817 { 0x3a, "caps_lock" },
818 { 0x3b, "f1" },
819 { 0x3c, "f2" },
820 { 0x3d, "f3" },
821 { 0x3e, "f4" },
822 { 0x3f, "f5" },
823 { 0x40, "f6" },
824 { 0x41, "f7" },
825 { 0x42, "f8" },
826 { 0x43, "f9" },
827 { 0x44, "f10" },
828 { 0x45, "num_lock" },
829 { 0x46, "scroll_lock" },
831 { 0xb5, "kp_divide" },
832 { 0x37, "kp_multiply" },
833 { 0x4a, "kp_substract" },
834 { 0x4e, "kp_add" },
835 { 0x9c, "kp_enter" },
836 { 0x53, "kp_decimal" },
838 { 0x52, "kp_0" },
839 { 0x4f, "kp_1" },
840 { 0x50, "kp_2" },
841 { 0x51, "kp_3" },
842 { 0x4b, "kp_4" },
843 { 0x4c, "kp_5" },
844 { 0x4d, "kp_6" },
845 { 0x47, "kp_7" },
846 { 0x48, "kp_8" },
847 { 0x49, "kp_9" },
849 { 0x56, "<" },
851 { 0x57, "f11" },
852 { 0x58, "f12" },
854 { 0xb7, "print" },
856 { 0xc7, "home" },
857 { 0xc9, "pgup" },
858 { 0xd1, "pgdn" },
859 { 0xcf, "end" },
861 { 0xcb, "left" },
862 { 0xc8, "up" },
863 { 0xd0, "down" },
864 { 0xcd, "right" },
866 { 0xd2, "insert" },
867 { 0xd3, "delete" },
868 { 0, NULL },
871 static int get_keycode(const char *key)
873 const KeyDef *p;
874 char *endp;
875 int ret;
877 for(p = key_defs; p->name != NULL; p++) {
878 if (!strcmp(key, p->name))
879 return p->keycode;
881 if (strstart(key, "0x", NULL)) {
882 ret = strtoul(key, &endp, 0);
883 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
884 return ret;
886 return -1;
889 static void do_send_key(const char *string)
891 char keybuf[16], *q;
892 uint8_t keycodes[16];
893 const char *p;
894 int nb_keycodes, keycode, i;
896 nb_keycodes = 0;
897 p = string;
898 while (*p != '\0') {
899 q = keybuf;
900 while (*p != '\0' && *p != '-') {
901 if ((q - keybuf) < sizeof(keybuf) - 1) {
902 *q++ = *p;
904 p++;
906 *q = '\0';
907 keycode = get_keycode(keybuf);
908 if (keycode < 0) {
909 term_printf("unknown key: '%s'\n", keybuf);
910 return;
912 keycodes[nb_keycodes++] = keycode;
913 if (*p == '\0')
914 break;
915 p++;
917 /* key down events */
918 for(i = 0; i < nb_keycodes; i++) {
919 keycode = keycodes[i];
920 if (keycode & 0x80)
921 kbd_put_keycode(0xe0);
922 kbd_put_keycode(keycode & 0x7f);
924 /* key up events */
925 for(i = nb_keycodes - 1; i >= 0; i--) {
926 keycode = keycodes[i];
927 if (keycode & 0x80)
928 kbd_put_keycode(0xe0);
929 kbd_put_keycode(keycode | 0x80);
933 static int mouse_button_state;
935 static void do_mouse_move(const char *dx_str, const char *dy_str,
936 const char *dz_str)
938 int dx, dy, dz;
939 dx = strtol(dx_str, NULL, 0);
940 dy = strtol(dy_str, NULL, 0);
941 dz = 0;
942 if (dz_str)
943 dz = strtol(dz_str, NULL, 0);
944 kbd_mouse_event(dx, dy, dz, mouse_button_state);
947 static void do_mouse_button(int button_state)
949 mouse_button_state = button_state;
950 kbd_mouse_event(0, 0, 0, mouse_button_state);
953 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
955 uint32_t val;
956 int suffix;
958 if (has_index) {
959 cpu_outb(NULL, addr & 0xffff, index & 0xff);
960 addr++;
962 addr &= 0xffff;
964 switch(size) {
965 default:
966 case 1:
967 val = cpu_inb(NULL, addr);
968 suffix = 'b';
969 break;
970 case 2:
971 val = cpu_inw(NULL, addr);
972 suffix = 'w';
973 break;
974 case 4:
975 val = cpu_inl(NULL, addr);
976 suffix = 'l';
977 break;
979 term_printf("port%c[0x%04x] = %#0*x\n",
980 suffix, addr, size * 2, val);
983 static void do_system_reset(void)
985 qemu_system_reset_request();
988 static void do_system_powerdown(void)
990 qemu_system_powerdown_request();
993 #if defined(TARGET_I386)
994 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
996 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
997 addr,
998 pte & mask,
999 pte & PG_GLOBAL_MASK ? 'G' : '-',
1000 pte & PG_PSE_MASK ? 'P' : '-',
1001 pte & PG_DIRTY_MASK ? 'D' : '-',
1002 pte & PG_ACCESSED_MASK ? 'A' : '-',
1003 pte & PG_PCD_MASK ? 'C' : '-',
1004 pte & PG_PWT_MASK ? 'T' : '-',
1005 pte & PG_USER_MASK ? 'U' : '-',
1006 pte & PG_RW_MASK ? 'W' : '-');
1009 static void tlb_info(void)
1011 CPUState *env;
1012 int l1, l2;
1013 uint32_t pgd, pde, pte;
1015 env = mon_get_cpu();
1016 if (!env)
1017 return;
1019 if (!(env->cr[0] & CR0_PG_MASK)) {
1020 term_printf("PG disabled\n");
1021 return;
1023 pgd = env->cr[3] & ~0xfff;
1024 for(l1 = 0; l1 < 1024; l1++) {
1025 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1026 pde = le32_to_cpu(pde);
1027 if (pde & PG_PRESENT_MASK) {
1028 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1029 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1030 } else {
1031 for(l2 = 0; l2 < 1024; l2++) {
1032 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1033 (uint8_t *)&pte, 4);
1034 pte = le32_to_cpu(pte);
1035 if (pte & PG_PRESENT_MASK) {
1036 print_pte((l1 << 22) + (l2 << 12),
1037 pte & ~PG_PSE_MASK,
1038 ~0xfff);
1046 static void mem_print(uint32_t *pstart, int *plast_prot,
1047 uint32_t end, int prot)
1049 int prot1;
1050 prot1 = *plast_prot;
1051 if (prot != prot1) {
1052 if (*pstart != -1) {
1053 term_printf("%08x-%08x %08x %c%c%c\n",
1054 *pstart, end, end - *pstart,
1055 prot1 & PG_USER_MASK ? 'u' : '-',
1056 'r',
1057 prot1 & PG_RW_MASK ? 'w' : '-');
1059 if (prot != 0)
1060 *pstart = end;
1061 else
1062 *pstart = -1;
1063 *plast_prot = prot;
1067 static void mem_info(void)
1069 CPUState *env;
1070 int l1, l2, prot, last_prot;
1071 uint32_t pgd, pde, pte, start, end;
1073 env = mon_get_cpu();
1074 if (!env)
1075 return;
1077 if (!(env->cr[0] & CR0_PG_MASK)) {
1078 term_printf("PG disabled\n");
1079 return;
1081 pgd = env->cr[3] & ~0xfff;
1082 last_prot = 0;
1083 start = -1;
1084 for(l1 = 0; l1 < 1024; l1++) {
1085 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1086 pde = le32_to_cpu(pde);
1087 end = l1 << 22;
1088 if (pde & PG_PRESENT_MASK) {
1089 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1090 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1091 mem_print(&start, &last_prot, end, prot);
1092 } else {
1093 for(l2 = 0; l2 < 1024; l2++) {
1094 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1095 (uint8_t *)&pte, 4);
1096 pte = le32_to_cpu(pte);
1097 end = (l1 << 22) + (l2 << 12);
1098 if (pte & PG_PRESENT_MASK) {
1099 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1100 } else {
1101 prot = 0;
1103 mem_print(&start, &last_prot, end, prot);
1106 } else {
1107 prot = 0;
1108 mem_print(&start, &last_prot, end, prot);
1112 #endif
1114 static void do_info_kqemu(void)
1116 #ifdef USE_KQEMU
1117 CPUState *env;
1118 int val;
1119 val = 0;
1120 env = mon_get_cpu();
1121 if (!env) {
1122 term_printf("No cpu initialized yet");
1123 return;
1125 val = env->kqemu_enabled;
1126 term_printf("kqemu support: ");
1127 switch(val) {
1128 default:
1129 case 0:
1130 term_printf("disabled\n");
1131 break;
1132 case 1:
1133 term_printf("enabled for user code\n");
1134 break;
1135 case 2:
1136 term_printf("enabled for user and kernel code\n");
1137 break;
1139 #else
1140 term_printf("kqemu support: not compiled\n");
1141 #endif
1144 #ifdef CONFIG_PROFILER
1146 int64_t kqemu_time;
1147 int64_t qemu_time;
1148 int64_t kqemu_exec_count;
1149 int64_t dev_time;
1150 int64_t kqemu_ret_int_count;
1151 int64_t kqemu_ret_excp_count;
1152 int64_t kqemu_ret_intr_count;
1154 static void do_info_profile(void)
1156 int64_t total;
1157 total = qemu_time;
1158 if (total == 0)
1159 total = 1;
1160 term_printf("async time %" PRId64 " (%0.3f)\n",
1161 dev_time, dev_time / (double)ticks_per_sec);
1162 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1163 qemu_time, qemu_time / (double)ticks_per_sec);
1164 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1165 kqemu_time, kqemu_time / (double)ticks_per_sec,
1166 kqemu_time / (double)total * 100.0,
1167 kqemu_exec_count,
1168 kqemu_ret_int_count,
1169 kqemu_ret_excp_count,
1170 kqemu_ret_intr_count);
1171 qemu_time = 0;
1172 kqemu_time = 0;
1173 kqemu_exec_count = 0;
1174 dev_time = 0;
1175 kqemu_ret_int_count = 0;
1176 kqemu_ret_excp_count = 0;
1177 kqemu_ret_intr_count = 0;
1178 #ifdef USE_KQEMU
1179 kqemu_record_dump();
1180 #endif
1182 #else
1183 static void do_info_profile(void)
1185 term_printf("Internal profiler not compiled\n");
1187 #endif
1189 /* Capture support */
1190 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1192 static void do_info_capture (void)
1194 int i;
1195 CaptureState *s;
1197 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1198 term_printf ("[%d]: ", i);
1199 s->ops.info (s->opaque);
1203 static void do_stop_capture (int n)
1205 int i;
1206 CaptureState *s;
1208 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1209 if (i == n) {
1210 s->ops.destroy (s->opaque);
1211 LIST_REMOVE (s, entries);
1212 qemu_free (s);
1213 return;
1218 #ifdef HAS_AUDIO
1219 int wav_start_capture (CaptureState *s, const char *path, int freq,
1220 int bits, int nchannels);
1222 static void do_wav_capture (const char *path,
1223 int has_freq, int freq,
1224 int has_bits, int bits,
1225 int has_channels, int nchannels)
1227 CaptureState *s;
1229 s = qemu_mallocz (sizeof (*s));
1230 if (!s) {
1231 term_printf ("Not enough memory to add wave capture\n");
1232 return;
1235 freq = has_freq ? freq : 44100;
1236 bits = has_bits ? bits : 16;
1237 nchannels = has_channels ? nchannels : 2;
1239 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1240 term_printf ("Faied to add wave capture\n");
1241 qemu_free (s);
1243 LIST_INSERT_HEAD (&capture_head, s, entries);
1245 #endif
1247 static term_cmd_t term_cmds[] = {
1248 { "help|?", "s?", do_help,
1249 "[cmd]", "show the help" },
1250 { "commit", "s", do_commit,
1251 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1252 { "info", "s?", do_info,
1253 "subcommand", "show various information about the system state" },
1254 { "q|quit", "", do_quit,
1255 "", "quit the emulator" },
1256 { "eject", "-fB", do_eject,
1257 "[-f] device", "eject a removable media (use -f to force it)" },
1258 { "change", "BF", do_change,
1259 "device filename", "change a removable media" },
1260 { "screendump", "F", do_screen_dump,
1261 "filename", "save screen into PPM image 'filename'" },
1262 { "log", "s", do_log,
1263 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1264 { "savevm", "s?", do_savevm,
1265 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1266 { "loadvm", "s", do_loadvm,
1267 "tag|id", "restore a VM snapshot from its tag or id" },
1268 { "delvm", "s", do_delvm,
1269 "tag|id", "delete a VM snapshot from its tag or id" },
1270 { "stop", "", do_stop,
1271 "", "stop emulation", },
1272 { "c|cont", "", do_cont,
1273 "", "resume emulation", },
1274 #ifdef CONFIG_GDBSTUB
1275 { "gdbserver", "i?", do_gdbserver,
1276 "[port]", "start gdbserver session (default port=1234)", },
1277 #endif
1278 { "x", "/l", do_memory_dump,
1279 "/fmt addr", "virtual memory dump starting at 'addr'", },
1280 { "xp", "/l", do_physical_memory_dump,
1281 "/fmt addr", "physical memory dump starting at 'addr'", },
1282 { "p|print", "/l", do_print,
1283 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1284 { "i", "/ii.", do_ioport_read,
1285 "/fmt addr", "I/O port read" },
1287 { "sendkey", "s", do_send_key,
1288 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1289 { "system_reset", "", do_system_reset,
1290 "", "reset the system" },
1291 { "system_powerdown", "", do_system_powerdown,
1292 "", "send system power down event" },
1293 { "sum", "ii", do_sum,
1294 "addr size", "compute the checksum of a memory region" },
1295 { "usb_add", "s", do_usb_add,
1296 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1297 { "usb_del", "s", do_usb_del,
1298 "device", "remove USB device 'bus.addr'" },
1299 { "cpu", "i", do_cpu_set,
1300 "index", "set the default CPU" },
1301 { "mouse_move", "sss?", do_mouse_move,
1302 "dx dy [dz]", "send mouse move events" },
1303 { "mouse_button", "i", do_mouse_button,
1304 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1305 { "mouse_set", "i", do_mouse_set,
1306 "index", "set which mouse device receives events" },
1307 #ifdef HAS_AUDIO
1308 { "wavcapture", "si?i?i?", do_wav_capture,
1309 "path [frequency bits channels]",
1310 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1311 #endif
1312 { "stopcapture", "i", do_stop_capture,
1313 "capture index", "stop capture" },
1314 { "memsave", "lis", do_memory_save,
1315 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'" },
1316 { "read_disk_io", "s", do_io_statistics,
1317 "hdx", "read disk I/O statistics (VMDK format)" },
1318 { "migrate", "-ds", do_migrate,
1319 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1320 { "migrate_cancel", "", do_migrate_cancel,
1321 "", "cancel the current VM migration" },
1322 { "migrate_set_speed", "s", do_migrate_set_speed,
1323 "value", "set maximum speed (in bytes) for migrations" },
1324 { NULL, NULL, },
1327 static term_cmd_t info_cmds[] = {
1328 { "version", "", do_info_version,
1329 "", "show the version of qemu" },
1330 { "network", "", do_info_network,
1331 "", "show the network state" },
1332 { "block", "", do_info_block,
1333 "", "show the block devices" },
1334 { "registers", "", do_info_registers,
1335 "", "show the cpu registers" },
1336 { "cpus", "", do_info_cpus,
1337 "", "show infos for each CPU" },
1338 { "history", "", do_info_history,
1339 "", "show the command line history", },
1340 { "irq", "", irq_info,
1341 "", "show the interrupts statistics (if available)", },
1342 { "pic", "", pic_info,
1343 "", "show i8259 (PIC) state", },
1344 { "pci", "", pci_info,
1345 "", "show PCI info", },
1346 #if defined(TARGET_I386)
1347 { "tlb", "", tlb_info,
1348 "", "show virtual to physical memory mappings", },
1349 { "mem", "", mem_info,
1350 "", "show the active virtual memory mappings", },
1351 #endif
1352 { "jit", "", do_info_jit,
1353 "", "show dynamic compiler info", },
1354 { "kqemu", "", do_info_kqemu,
1355 "", "show kqemu information", },
1356 { "usb", "", usb_info,
1357 "", "show guest USB devices", },
1358 { "usbhost", "", usb_host_info,
1359 "", "show host USB devices", },
1360 { "profile", "", do_info_profile,
1361 "", "show profiling information", },
1362 { "capture", "", do_info_capture,
1363 "", "show capture information" },
1364 { "snapshots", "", do_info_snapshots,
1365 "", "show the currently saved VM snapshots" },
1366 { "mice", "", do_info_mice,
1367 "", "show which guest mouse is receiving events" },
1368 { "vnc", "", do_info_vnc,
1369 "", "show the vnc server status"},
1370 { "migration", "", do_info_migration,
1371 "", "show migration information" },
1372 { NULL, NULL, },
1375 /*******************************************************************/
1377 static const char *pch;
1378 static jmp_buf expr_env;
1380 #define MD_TLONG 0
1381 #define MD_I32 1
1383 typedef struct MonitorDef {
1384 const char *name;
1385 int offset;
1386 target_long (*get_value)(struct MonitorDef *md, int val);
1387 int type;
1388 } MonitorDef;
1390 #if defined(TARGET_I386)
1391 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1393 CPUState *env = mon_get_cpu();
1394 if (!env)
1395 return 0;
1396 return env->eip + env->segs[R_CS].base;
1398 #endif
1400 #if defined(TARGET_PPC)
1401 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1403 CPUState *env = mon_get_cpu();
1404 unsigned int u;
1405 int i;
1407 if (!env)
1408 return 0;
1410 u = 0;
1411 for (i = 0; i < 8; i++)
1412 u |= env->crf[i] << (32 - (4 * i));
1414 return u;
1417 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1419 CPUState *env = mon_get_cpu();
1420 if (!env)
1421 return 0;
1422 return (env->msr[MSR_POW] << MSR_POW) |
1423 (env->msr[MSR_ILE] << MSR_ILE) |
1424 (env->msr[MSR_EE] << MSR_EE) |
1425 (env->msr[MSR_PR] << MSR_PR) |
1426 (env->msr[MSR_FP] << MSR_FP) |
1427 (env->msr[MSR_ME] << MSR_ME) |
1428 (env->msr[MSR_FE0] << MSR_FE0) |
1429 (env->msr[MSR_SE] << MSR_SE) |
1430 (env->msr[MSR_BE] << MSR_BE) |
1431 (env->msr[MSR_FE1] << MSR_FE1) |
1432 (env->msr[MSR_IP] << MSR_IP) |
1433 (env->msr[MSR_IR] << MSR_IR) |
1434 (env->msr[MSR_DR] << MSR_DR) |
1435 (env->msr[MSR_RI] << MSR_RI) |
1436 (env->msr[MSR_LE] << MSR_LE);
1439 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1441 CPUState *env = mon_get_cpu();
1442 if (!env)
1443 return 0;
1444 return (env->xer[XER_SO] << XER_SO) |
1445 (env->xer[XER_OV] << XER_OV) |
1446 (env->xer[XER_CA] << XER_CA) |
1447 (env->xer[XER_BC] << XER_BC);
1450 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1452 CPUState *env = mon_get_cpu();
1453 if (!env)
1454 return 0;
1455 return cpu_ppc_load_decr(env);
1458 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1460 CPUState *env = mon_get_cpu();
1461 if (!env)
1462 return 0;
1463 return cpu_ppc_load_tbu(env);
1466 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1468 CPUState *env = mon_get_cpu();
1469 if (!env)
1470 return 0;
1471 return cpu_ppc_load_tbl(env);
1473 #endif
1475 #if defined(TARGET_SPARC)
1476 #ifndef TARGET_SPARC64
1477 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1479 CPUState *env = mon_get_cpu();
1480 if (!env)
1481 return 0;
1482 return GET_PSR(env);
1484 #endif
1486 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1488 CPUState *env = mon_get_cpu();
1489 if (!env)
1490 return 0;
1491 return env->regwptr[val];
1493 #endif
1495 static MonitorDef monitor_defs[] = {
1496 #ifdef TARGET_I386
1498 #define SEG(name, seg) \
1499 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1500 { name ".base", offsetof(CPUState, segs[seg].base) },\
1501 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1503 { "eax", offsetof(CPUState, regs[0]) },
1504 { "ecx", offsetof(CPUState, regs[1]) },
1505 { "edx", offsetof(CPUState, regs[2]) },
1506 { "ebx", offsetof(CPUState, regs[3]) },
1507 { "esp|sp", offsetof(CPUState, regs[4]) },
1508 { "ebp|fp", offsetof(CPUState, regs[5]) },
1509 { "esi", offsetof(CPUState, regs[6]) },
1510 { "edi", offsetof(CPUState, regs[7]) },
1511 #ifdef TARGET_X86_64
1512 { "r8", offsetof(CPUState, regs[8]) },
1513 { "r9", offsetof(CPUState, regs[9]) },
1514 { "r10", offsetof(CPUState, regs[10]) },
1515 { "r11", offsetof(CPUState, regs[11]) },
1516 { "r12", offsetof(CPUState, regs[12]) },
1517 { "r13", offsetof(CPUState, regs[13]) },
1518 { "r14", offsetof(CPUState, regs[14]) },
1519 { "r15", offsetof(CPUState, regs[15]) },
1520 #endif
1521 { "eflags", offsetof(CPUState, eflags) },
1522 { "eip", offsetof(CPUState, eip) },
1523 SEG("cs", R_CS)
1524 SEG("ds", R_DS)
1525 SEG("es", R_ES)
1526 SEG("ss", R_SS)
1527 SEG("fs", R_FS)
1528 SEG("gs", R_GS)
1529 { "pc", 0, monitor_get_pc, },
1530 #elif defined(TARGET_PPC)
1531 { "r0", offsetof(CPUState, gpr[0]) },
1532 { "r1", offsetof(CPUState, gpr[1]) },
1533 { "r2", offsetof(CPUState, gpr[2]) },
1534 { "r3", offsetof(CPUState, gpr[3]) },
1535 { "r4", offsetof(CPUState, gpr[4]) },
1536 { "r5", offsetof(CPUState, gpr[5]) },
1537 { "r6", offsetof(CPUState, gpr[6]) },
1538 { "r7", offsetof(CPUState, gpr[7]) },
1539 { "r8", offsetof(CPUState, gpr[8]) },
1540 { "r9", offsetof(CPUState, gpr[9]) },
1541 { "r10", offsetof(CPUState, gpr[10]) },
1542 { "r11", offsetof(CPUState, gpr[11]) },
1543 { "r12", offsetof(CPUState, gpr[12]) },
1544 { "r13", offsetof(CPUState, gpr[13]) },
1545 { "r14", offsetof(CPUState, gpr[14]) },
1546 { "r15", offsetof(CPUState, gpr[15]) },
1547 { "r16", offsetof(CPUState, gpr[16]) },
1548 { "r17", offsetof(CPUState, gpr[17]) },
1549 { "r18", offsetof(CPUState, gpr[18]) },
1550 { "r19", offsetof(CPUState, gpr[19]) },
1551 { "r20", offsetof(CPUState, gpr[20]) },
1552 { "r21", offsetof(CPUState, gpr[21]) },
1553 { "r22", offsetof(CPUState, gpr[22]) },
1554 { "r23", offsetof(CPUState, gpr[23]) },
1555 { "r24", offsetof(CPUState, gpr[24]) },
1556 { "r25", offsetof(CPUState, gpr[25]) },
1557 { "r26", offsetof(CPUState, gpr[26]) },
1558 { "r27", offsetof(CPUState, gpr[27]) },
1559 { "r28", offsetof(CPUState, gpr[28]) },
1560 { "r29", offsetof(CPUState, gpr[29]) },
1561 { "r30", offsetof(CPUState, gpr[30]) },
1562 { "r31", offsetof(CPUState, gpr[31]) },
1563 { "nip|pc", offsetof(CPUState, nip) },
1564 { "lr", offsetof(CPUState, lr) },
1565 { "ctr", offsetof(CPUState, ctr) },
1566 { "decr", 0, &monitor_get_decr, },
1567 { "ccr", 0, &monitor_get_ccr, },
1568 { "msr", 0, &monitor_get_msr, },
1569 { "xer", 0, &monitor_get_xer, },
1570 { "tbu", 0, &monitor_get_tbu, },
1571 { "tbl", 0, &monitor_get_tbl, },
1572 { "sdr1", offsetof(CPUState, sdr1) },
1573 { "sr0", offsetof(CPUState, sr[0]) },
1574 { "sr1", offsetof(CPUState, sr[1]) },
1575 { "sr2", offsetof(CPUState, sr[2]) },
1576 { "sr3", offsetof(CPUState, sr[3]) },
1577 { "sr4", offsetof(CPUState, sr[4]) },
1578 { "sr5", offsetof(CPUState, sr[5]) },
1579 { "sr6", offsetof(CPUState, sr[6]) },
1580 { "sr7", offsetof(CPUState, sr[7]) },
1581 { "sr8", offsetof(CPUState, sr[8]) },
1582 { "sr9", offsetof(CPUState, sr[9]) },
1583 { "sr10", offsetof(CPUState, sr[10]) },
1584 { "sr11", offsetof(CPUState, sr[11]) },
1585 { "sr12", offsetof(CPUState, sr[12]) },
1586 { "sr13", offsetof(CPUState, sr[13]) },
1587 { "sr14", offsetof(CPUState, sr[14]) },
1588 { "sr15", offsetof(CPUState, sr[15]) },
1589 /* Too lazy to put BATs and SPRs ... */
1590 #elif defined(TARGET_SPARC)
1591 { "g0", offsetof(CPUState, gregs[0]) },
1592 { "g1", offsetof(CPUState, gregs[1]) },
1593 { "g2", offsetof(CPUState, gregs[2]) },
1594 { "g3", offsetof(CPUState, gregs[3]) },
1595 { "g4", offsetof(CPUState, gregs[4]) },
1596 { "g5", offsetof(CPUState, gregs[5]) },
1597 { "g6", offsetof(CPUState, gregs[6]) },
1598 { "g7", offsetof(CPUState, gregs[7]) },
1599 { "o0", 0, monitor_get_reg },
1600 { "o1", 1, monitor_get_reg },
1601 { "o2", 2, monitor_get_reg },
1602 { "o3", 3, monitor_get_reg },
1603 { "o4", 4, monitor_get_reg },
1604 { "o5", 5, monitor_get_reg },
1605 { "o6", 6, monitor_get_reg },
1606 { "o7", 7, monitor_get_reg },
1607 { "l0", 8, monitor_get_reg },
1608 { "l1", 9, monitor_get_reg },
1609 { "l2", 10, monitor_get_reg },
1610 { "l3", 11, monitor_get_reg },
1611 { "l4", 12, monitor_get_reg },
1612 { "l5", 13, monitor_get_reg },
1613 { "l6", 14, monitor_get_reg },
1614 { "l7", 15, monitor_get_reg },
1615 { "i0", 16, monitor_get_reg },
1616 { "i1", 17, monitor_get_reg },
1617 { "i2", 18, monitor_get_reg },
1618 { "i3", 19, monitor_get_reg },
1619 { "i4", 20, monitor_get_reg },
1620 { "i5", 21, monitor_get_reg },
1621 { "i6", 22, monitor_get_reg },
1622 { "i7", 23, monitor_get_reg },
1623 { "pc", offsetof(CPUState, pc) },
1624 { "npc", offsetof(CPUState, npc) },
1625 { "y", offsetof(CPUState, y) },
1626 #ifndef TARGET_SPARC64
1627 { "psr", 0, &monitor_get_psr, },
1628 { "wim", offsetof(CPUState, wim) },
1629 #endif
1630 { "tbr", offsetof(CPUState, tbr) },
1631 { "fsr", offsetof(CPUState, fsr) },
1632 { "f0", offsetof(CPUState, fpr[0]) },
1633 { "f1", offsetof(CPUState, fpr[1]) },
1634 { "f2", offsetof(CPUState, fpr[2]) },
1635 { "f3", offsetof(CPUState, fpr[3]) },
1636 { "f4", offsetof(CPUState, fpr[4]) },
1637 { "f5", offsetof(CPUState, fpr[5]) },
1638 { "f6", offsetof(CPUState, fpr[6]) },
1639 { "f7", offsetof(CPUState, fpr[7]) },
1640 { "f8", offsetof(CPUState, fpr[8]) },
1641 { "f9", offsetof(CPUState, fpr[9]) },
1642 { "f10", offsetof(CPUState, fpr[10]) },
1643 { "f11", offsetof(CPUState, fpr[11]) },
1644 { "f12", offsetof(CPUState, fpr[12]) },
1645 { "f13", offsetof(CPUState, fpr[13]) },
1646 { "f14", offsetof(CPUState, fpr[14]) },
1647 { "f15", offsetof(CPUState, fpr[15]) },
1648 { "f16", offsetof(CPUState, fpr[16]) },
1649 { "f17", offsetof(CPUState, fpr[17]) },
1650 { "f18", offsetof(CPUState, fpr[18]) },
1651 { "f19", offsetof(CPUState, fpr[19]) },
1652 { "f20", offsetof(CPUState, fpr[20]) },
1653 { "f21", offsetof(CPUState, fpr[21]) },
1654 { "f22", offsetof(CPUState, fpr[22]) },
1655 { "f23", offsetof(CPUState, fpr[23]) },
1656 { "f24", offsetof(CPUState, fpr[24]) },
1657 { "f25", offsetof(CPUState, fpr[25]) },
1658 { "f26", offsetof(CPUState, fpr[26]) },
1659 { "f27", offsetof(CPUState, fpr[27]) },
1660 { "f28", offsetof(CPUState, fpr[28]) },
1661 { "f29", offsetof(CPUState, fpr[29]) },
1662 { "f30", offsetof(CPUState, fpr[30]) },
1663 { "f31", offsetof(CPUState, fpr[31]) },
1664 #ifdef TARGET_SPARC64
1665 { "f32", offsetof(CPUState, fpr[32]) },
1666 { "f34", offsetof(CPUState, fpr[34]) },
1667 { "f36", offsetof(CPUState, fpr[36]) },
1668 { "f38", offsetof(CPUState, fpr[38]) },
1669 { "f40", offsetof(CPUState, fpr[40]) },
1670 { "f42", offsetof(CPUState, fpr[42]) },
1671 { "f44", offsetof(CPUState, fpr[44]) },
1672 { "f46", offsetof(CPUState, fpr[46]) },
1673 { "f48", offsetof(CPUState, fpr[48]) },
1674 { "f50", offsetof(CPUState, fpr[50]) },
1675 { "f52", offsetof(CPUState, fpr[52]) },
1676 { "f54", offsetof(CPUState, fpr[54]) },
1677 { "f56", offsetof(CPUState, fpr[56]) },
1678 { "f58", offsetof(CPUState, fpr[58]) },
1679 { "f60", offsetof(CPUState, fpr[60]) },
1680 { "f62", offsetof(CPUState, fpr[62]) },
1681 { "asi", offsetof(CPUState, asi) },
1682 { "pstate", offsetof(CPUState, pstate) },
1683 { "cansave", offsetof(CPUState, cansave) },
1684 { "canrestore", offsetof(CPUState, canrestore) },
1685 { "otherwin", offsetof(CPUState, otherwin) },
1686 { "wstate", offsetof(CPUState, wstate) },
1687 { "cleanwin", offsetof(CPUState, cleanwin) },
1688 { "fprs", offsetof(CPUState, fprs) },
1689 #endif
1690 #endif
1691 { NULL },
1694 static void expr_error(const char *fmt)
1696 term_printf(fmt);
1697 term_printf("\n");
1698 longjmp(expr_env, 1);
1701 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1702 static int get_monitor_def(target_long *pval, const char *name)
1704 MonitorDef *md;
1705 void *ptr;
1707 for(md = monitor_defs; md->name != NULL; md++) {
1708 if (compare_cmd(name, md->name)) {
1709 if (md->get_value) {
1710 *pval = md->get_value(md, md->offset);
1711 } else {
1712 CPUState *env = mon_get_cpu();
1713 if (!env)
1714 return -2;
1715 ptr = (uint8_t *)env + md->offset;
1716 switch(md->type) {
1717 case MD_I32:
1718 *pval = *(int32_t *)ptr;
1719 break;
1720 case MD_TLONG:
1721 *pval = *(target_long *)ptr;
1722 break;
1723 default:
1724 *pval = 0;
1725 break;
1728 return 0;
1731 return -1;
1734 static void next(void)
1736 if (pch != '\0') {
1737 pch++;
1738 while (isspace(*pch))
1739 pch++;
1743 static target_long expr_sum(void);
1745 static target_long expr_unary(void)
1747 target_long n;
1748 char *p;
1749 int ret;
1751 switch(*pch) {
1752 case '+':
1753 next();
1754 n = expr_unary();
1755 break;
1756 case '-':
1757 next();
1758 n = -expr_unary();
1759 break;
1760 case '~':
1761 next();
1762 n = ~expr_unary();
1763 break;
1764 case '(':
1765 next();
1766 n = expr_sum();
1767 if (*pch != ')') {
1768 expr_error("')' expected");
1770 next();
1771 break;
1772 case '\'':
1773 pch++;
1774 if (*pch == '\0')
1775 expr_error("character constant expected");
1776 n = *pch;
1777 pch++;
1778 if (*pch != '\'')
1779 expr_error("missing terminating \' character");
1780 next();
1781 break;
1782 case '$':
1784 char buf[128], *q;
1786 pch++;
1787 q = buf;
1788 while ((*pch >= 'a' && *pch <= 'z') ||
1789 (*pch >= 'A' && *pch <= 'Z') ||
1790 (*pch >= '0' && *pch <= '9') ||
1791 *pch == '_' || *pch == '.') {
1792 if ((q - buf) < sizeof(buf) - 1)
1793 *q++ = *pch;
1794 pch++;
1796 while (isspace(*pch))
1797 pch++;
1798 *q = 0;
1799 ret = get_monitor_def(&n, buf);
1800 if (ret == -1)
1801 expr_error("unknown register");
1802 else if (ret == -2)
1803 expr_error("no cpu defined");
1805 break;
1806 case '\0':
1807 expr_error("unexpected end of expression");
1808 n = 0;
1809 break;
1810 default:
1811 #if TARGET_LONG_BITS == 64
1812 n = strtoull(pch, &p, 0);
1813 #else
1814 n = strtoul(pch, &p, 0);
1815 #endif
1816 if (pch == p) {
1817 expr_error("invalid char in expression");
1819 pch = p;
1820 while (isspace(*pch))
1821 pch++;
1822 break;
1824 return n;
1828 static target_long expr_prod(void)
1830 target_long val, val2;
1831 int op;
1833 val = expr_unary();
1834 for(;;) {
1835 op = *pch;
1836 if (op != '*' && op != '/' && op != '%')
1837 break;
1838 next();
1839 val2 = expr_unary();
1840 switch(op) {
1841 default:
1842 case '*':
1843 val *= val2;
1844 break;
1845 case '/':
1846 case '%':
1847 if (val2 == 0)
1848 expr_error("division by zero");
1849 if (op == '/')
1850 val /= val2;
1851 else
1852 val %= val2;
1853 break;
1856 return val;
1859 static target_long expr_logic(void)
1861 target_long val, val2;
1862 int op;
1864 val = expr_prod();
1865 for(;;) {
1866 op = *pch;
1867 if (op != '&' && op != '|' && op != '^')
1868 break;
1869 next();
1870 val2 = expr_prod();
1871 switch(op) {
1872 default:
1873 case '&':
1874 val &= val2;
1875 break;
1876 case '|':
1877 val |= val2;
1878 break;
1879 case '^':
1880 val ^= val2;
1881 break;
1884 return val;
1887 static target_long expr_sum(void)
1889 target_long val, val2;
1890 int op;
1892 val = expr_logic();
1893 for(;;) {
1894 op = *pch;
1895 if (op != '+' && op != '-')
1896 break;
1897 next();
1898 val2 = expr_logic();
1899 if (op == '+')
1900 val += val2;
1901 else
1902 val -= val2;
1904 return val;
1907 static int get_expr(target_long *pval, const char **pp)
1909 pch = *pp;
1910 if (setjmp(expr_env)) {
1911 *pp = pch;
1912 return -1;
1914 while (isspace(*pch))
1915 pch++;
1916 *pval = expr_sum();
1917 *pp = pch;
1918 return 0;
1921 static int get_str(char *buf, int buf_size, const char **pp)
1923 const char *p;
1924 char *q;
1925 int c;
1927 q = buf;
1928 p = *pp;
1929 while (isspace(*p))
1930 p++;
1931 if (*p == '\0') {
1932 fail:
1933 *q = '\0';
1934 *pp = p;
1935 return -1;
1937 if (*p == '\"') {
1938 p++;
1939 while (*p != '\0' && *p != '\"') {
1940 if (*p == '\\') {
1941 p++;
1942 c = *p++;
1943 switch(c) {
1944 case 'n':
1945 c = '\n';
1946 break;
1947 case 'r':
1948 c = '\r';
1949 break;
1950 case '\\':
1951 case '\'':
1952 case '\"':
1953 break;
1954 default:
1955 qemu_printf("unsupported escape code: '\\%c'\n", c);
1956 goto fail;
1958 if ((q - buf) < buf_size - 1) {
1959 *q++ = c;
1961 } else {
1962 if ((q - buf) < buf_size - 1) {
1963 *q++ = *p;
1965 p++;
1968 if (*p != '\"') {
1969 qemu_printf("unterminated string\n");
1970 goto fail;
1972 p++;
1973 } else {
1974 while (*p != '\0' && !isspace(*p)) {
1975 if ((q - buf) < buf_size - 1) {
1976 *q++ = *p;
1978 p++;
1981 *q = '\0';
1982 *pp = p;
1983 return 0;
1986 static int default_fmt_format = 'x';
1987 static int default_fmt_size = 4;
1989 #define MAX_ARGS 16
1991 static void monitor_handle_command(const char *cmdline)
1993 const char *p, *pstart, *typestr;
1994 char *q;
1995 int c, nb_args, len, i, has_arg;
1996 term_cmd_t *cmd;
1997 char cmdname[256];
1998 char buf[1024];
1999 void *str_allocated[MAX_ARGS];
2000 void *args[MAX_ARGS];
2002 #ifdef DEBUG
2003 term_printf("command='%s'\n", cmdline);
2004 #endif
2006 /* extract the command name */
2007 p = cmdline;
2008 q = cmdname;
2009 while (isspace(*p))
2010 p++;
2011 if (*p == '\0')
2012 return;
2013 pstart = p;
2014 while (*p != '\0' && *p != '/' && !isspace(*p))
2015 p++;
2016 len = p - pstart;
2017 if (len > sizeof(cmdname) - 1)
2018 len = sizeof(cmdname) - 1;
2019 memcpy(cmdname, pstart, len);
2020 cmdname[len] = '\0';
2022 /* find the command */
2023 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2024 if (compare_cmd(cmdname, cmd->name))
2025 goto found;
2027 term_printf("unknown command: '%s'\n", cmdname);
2028 return;
2029 found:
2031 for(i = 0; i < MAX_ARGS; i++)
2032 str_allocated[i] = NULL;
2034 /* parse the parameters */
2035 typestr = cmd->args_type;
2036 nb_args = 0;
2037 for(;;) {
2038 c = *typestr;
2039 if (c == '\0')
2040 break;
2041 typestr++;
2042 switch(c) {
2043 case 'F':
2044 case 'B':
2045 case 's':
2047 int ret;
2048 char *str;
2050 while (isspace(*p))
2051 p++;
2052 if (*typestr == '?') {
2053 typestr++;
2054 if (*p == '\0') {
2055 /* no optional string: NULL argument */
2056 str = NULL;
2057 goto add_str;
2060 ret = get_str(buf, sizeof(buf), &p);
2061 if (ret < 0) {
2062 switch(c) {
2063 case 'F':
2064 term_printf("%s: filename expected\n", cmdname);
2065 break;
2066 case 'B':
2067 term_printf("%s: block device name expected\n", cmdname);
2068 break;
2069 default:
2070 term_printf("%s: string expected\n", cmdname);
2071 break;
2073 goto fail;
2075 str = qemu_malloc(strlen(buf) + 1);
2076 strcpy(str, buf);
2077 str_allocated[nb_args] = str;
2078 add_str:
2079 if (nb_args >= MAX_ARGS) {
2080 error_args:
2081 term_printf("%s: too many arguments\n", cmdname);
2082 goto fail;
2084 args[nb_args++] = str;
2086 break;
2087 case '/':
2089 int count, format, size;
2091 while (isspace(*p))
2092 p++;
2093 if (*p == '/') {
2094 /* format found */
2095 p++;
2096 count = 1;
2097 if (isdigit(*p)) {
2098 count = 0;
2099 while (isdigit(*p)) {
2100 count = count * 10 + (*p - '0');
2101 p++;
2104 size = -1;
2105 format = -1;
2106 for(;;) {
2107 switch(*p) {
2108 case 'o':
2109 case 'd':
2110 case 'u':
2111 case 'x':
2112 case 'i':
2113 case 'c':
2114 format = *p++;
2115 break;
2116 case 'b':
2117 size = 1;
2118 p++;
2119 break;
2120 case 'h':
2121 size = 2;
2122 p++;
2123 break;
2124 case 'w':
2125 size = 4;
2126 p++;
2127 break;
2128 case 'g':
2129 case 'L':
2130 size = 8;
2131 p++;
2132 break;
2133 default:
2134 goto next;
2137 next:
2138 if (*p != '\0' && !isspace(*p)) {
2139 term_printf("invalid char in format: '%c'\n", *p);
2140 goto fail;
2142 if (format < 0)
2143 format = default_fmt_format;
2144 if (format != 'i') {
2145 /* for 'i', not specifying a size gives -1 as size */
2146 if (size < 0)
2147 size = default_fmt_size;
2149 default_fmt_size = size;
2150 default_fmt_format = format;
2151 } else {
2152 count = 1;
2153 format = default_fmt_format;
2154 if (format != 'i') {
2155 size = default_fmt_size;
2156 } else {
2157 size = -1;
2160 if (nb_args + 3 > MAX_ARGS)
2161 goto error_args;
2162 args[nb_args++] = (void*)count;
2163 args[nb_args++] = (void*)format;
2164 args[nb_args++] = (void*)size;
2166 break;
2167 case 'i':
2168 case 'l':
2170 target_long val;
2171 while (isspace(*p))
2172 p++;
2173 if (*typestr == '?' || *typestr == '.') {
2174 if (*typestr == '?') {
2175 if (*p == '\0')
2176 has_arg = 0;
2177 else
2178 has_arg = 1;
2179 } else {
2180 if (*p == '.') {
2181 p++;
2182 while (isspace(*p))
2183 p++;
2184 has_arg = 1;
2185 } else {
2186 has_arg = 0;
2189 typestr++;
2190 if (nb_args >= MAX_ARGS)
2191 goto error_args;
2192 args[nb_args++] = (void *)has_arg;
2193 if (!has_arg) {
2194 if (nb_args >= MAX_ARGS)
2195 goto error_args;
2196 val = -1;
2197 goto add_num;
2200 if (get_expr(&val, &p))
2201 goto fail;
2202 add_num:
2203 if (c == 'i') {
2204 if (nb_args >= MAX_ARGS)
2205 goto error_args;
2206 args[nb_args++] = (void *)(int)val;
2207 } else {
2208 if ((nb_args + 1) >= MAX_ARGS)
2209 goto error_args;
2210 #if TARGET_LONG_BITS == 64
2211 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2212 #else
2213 args[nb_args++] = (void *)0;
2214 #endif
2215 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2218 break;
2219 case '-':
2221 int has_option;
2222 /* option */
2224 c = *typestr++;
2225 if (c == '\0')
2226 goto bad_type;
2227 while (isspace(*p))
2228 p++;
2229 has_option = 0;
2230 if (*p == '-') {
2231 p++;
2232 if (*p != c) {
2233 term_printf("%s: unsupported option -%c\n",
2234 cmdname, *p);
2235 goto fail;
2237 p++;
2238 has_option = 1;
2240 if (nb_args >= MAX_ARGS)
2241 goto error_args;
2242 args[nb_args++] = (void *)has_option;
2244 break;
2245 default:
2246 bad_type:
2247 term_printf("%s: unknown type '%c'\n", cmdname, c);
2248 goto fail;
2251 /* check that all arguments were parsed */
2252 while (isspace(*p))
2253 p++;
2254 if (*p != '\0') {
2255 term_printf("%s: extraneous characters at the end of line\n",
2256 cmdname);
2257 goto fail;
2260 switch(nb_args) {
2261 case 0:
2262 cmd->handler();
2263 break;
2264 case 1:
2265 cmd->handler(args[0]);
2266 break;
2267 case 2:
2268 cmd->handler(args[0], args[1]);
2269 break;
2270 case 3:
2271 cmd->handler(args[0], args[1], args[2]);
2272 break;
2273 case 4:
2274 cmd->handler(args[0], args[1], args[2], args[3]);
2275 break;
2276 case 5:
2277 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2278 break;
2279 case 6:
2280 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2281 break;
2282 case 7:
2283 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2284 break;
2285 default:
2286 term_printf("unsupported number of arguments: %d\n", nb_args);
2287 goto fail;
2289 fail:
2290 for(i = 0; i < MAX_ARGS; i++)
2291 qemu_free(str_allocated[i]);
2292 return;
2295 static void cmd_completion(const char *name, const char *list)
2297 const char *p, *pstart;
2298 char cmd[128];
2299 int len;
2301 p = list;
2302 for(;;) {
2303 pstart = p;
2304 p = strchr(p, '|');
2305 if (!p)
2306 p = pstart + strlen(pstart);
2307 len = p - pstart;
2308 if (len > sizeof(cmd) - 2)
2309 len = sizeof(cmd) - 2;
2310 memcpy(cmd, pstart, len);
2311 cmd[len] = '\0';
2312 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2313 add_completion(cmd);
2315 if (*p == '\0')
2316 break;
2317 p++;
2321 static void file_completion(const char *input)
2323 DIR *ffs;
2324 struct dirent *d;
2325 char path[1024];
2326 char file[1024], file_prefix[1024];
2327 int input_path_len;
2328 const char *p;
2330 p = strrchr(input, '/');
2331 if (!p) {
2332 input_path_len = 0;
2333 pstrcpy(file_prefix, sizeof(file_prefix), input);
2334 strcpy(path, ".");
2335 } else {
2336 input_path_len = p - input + 1;
2337 memcpy(path, input, input_path_len);
2338 if (input_path_len > sizeof(path) - 1)
2339 input_path_len = sizeof(path) - 1;
2340 path[input_path_len] = '\0';
2341 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2343 #ifdef DEBUG_COMPLETION
2344 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2345 #endif
2346 ffs = opendir(path);
2347 if (!ffs)
2348 return;
2349 for(;;) {
2350 struct stat sb;
2351 d = readdir(ffs);
2352 if (!d)
2353 break;
2354 if (strstart(d->d_name, file_prefix, NULL)) {
2355 memcpy(file, input, input_path_len);
2356 strcpy(file + input_path_len, d->d_name);
2357 /* stat the file to find out if it's a directory.
2358 * In that case add a slash to speed up typing long paths
2360 stat(file, &sb);
2361 if(S_ISDIR(sb.st_mode))
2362 strcat(file, "/");
2363 add_completion(file);
2366 closedir(ffs);
2369 static void block_completion_it(void *opaque, const char *name)
2371 const char *input = opaque;
2373 if (input[0] == '\0' ||
2374 !strncmp(name, (char *)input, strlen(input))) {
2375 add_completion(name);
2379 /* NOTE: this parser is an approximate form of the real command parser */
2380 static void parse_cmdline(const char *cmdline,
2381 int *pnb_args, char **args)
2383 const char *p;
2384 int nb_args, ret;
2385 char buf[1024];
2387 p = cmdline;
2388 nb_args = 0;
2389 for(;;) {
2390 while (isspace(*p))
2391 p++;
2392 if (*p == '\0')
2393 break;
2394 if (nb_args >= MAX_ARGS)
2395 break;
2396 ret = get_str(buf, sizeof(buf), &p);
2397 args[nb_args] = qemu_strdup(buf);
2398 nb_args++;
2399 if (ret < 0)
2400 break;
2402 *pnb_args = nb_args;
2405 void readline_find_completion(const char *cmdline)
2407 const char *cmdname;
2408 char *args[MAX_ARGS];
2409 int nb_args, i, len;
2410 const char *ptype, *str;
2411 term_cmd_t *cmd;
2412 const KeyDef *key;
2414 parse_cmdline(cmdline, &nb_args, args);
2415 #ifdef DEBUG_COMPLETION
2416 for(i = 0; i < nb_args; i++) {
2417 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2419 #endif
2421 /* if the line ends with a space, it means we want to complete the
2422 next arg */
2423 len = strlen(cmdline);
2424 if (len > 0 && isspace(cmdline[len - 1])) {
2425 if (nb_args >= MAX_ARGS)
2426 return;
2427 args[nb_args++] = qemu_strdup("");
2429 if (nb_args <= 1) {
2430 /* command completion */
2431 if (nb_args == 0)
2432 cmdname = "";
2433 else
2434 cmdname = args[0];
2435 completion_index = strlen(cmdname);
2436 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2437 cmd_completion(cmdname, cmd->name);
2439 } else {
2440 /* find the command */
2441 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2442 if (compare_cmd(args[0], cmd->name))
2443 goto found;
2445 return;
2446 found:
2447 ptype = cmd->args_type;
2448 for(i = 0; i < nb_args - 2; i++) {
2449 if (*ptype != '\0') {
2450 ptype++;
2451 while (*ptype == '?')
2452 ptype++;
2455 str = args[nb_args - 1];
2456 switch(*ptype) {
2457 case 'F':
2458 /* file completion */
2459 completion_index = strlen(str);
2460 file_completion(str);
2461 break;
2462 case 'B':
2463 /* block device name completion */
2464 completion_index = strlen(str);
2465 bdrv_iterate(block_completion_it, (void *)str);
2466 break;
2467 case 's':
2468 /* XXX: more generic ? */
2469 if (!strcmp(cmd->name, "info")) {
2470 completion_index = strlen(str);
2471 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2472 cmd_completion(str, cmd->name);
2474 } else if (!strcmp(cmd->name, "sendkey")) {
2475 completion_index = strlen(str);
2476 for(key = key_defs; key->name != NULL; key++) {
2477 cmd_completion(str, key->name);
2480 break;
2481 default:
2482 break;
2485 for(i = 0; i < nb_args; i++)
2486 qemu_free(args[i]);
2489 static int term_can_read(void *opaque)
2491 return 128;
2494 static void term_read(void *opaque, const uint8_t *buf, int size)
2496 int i;
2497 for(i = 0; i < size; i++)
2498 readline_handle_byte(buf[i]);
2501 static int monitor_suspended;
2503 void monitor_suspend(void)
2505 monitor_suspended = 1;
2508 void monitor_resume(void)
2510 monitor_suspended = 0;
2511 monitor_start_input();
2514 static void monitor_start_input(void);
2516 static void monitor_handle_command1(void *opaque, const char *cmdline)
2518 monitor_handle_command(cmdline);
2519 if (!monitor_suspended)
2520 monitor_start_input();
2523 static void monitor_start_input(void)
2525 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2528 static void term_event(void *opaque, int event)
2530 if (event != CHR_EVENT_RESET)
2531 return;
2533 if (!hide_banner)
2534 term_printf("QEMU %s monitor - type 'help' for more information\n",
2535 QEMU_VERSION);
2536 monitor_start_input();
2539 void monitor_init(CharDriverState *hd, int show_banner)
2541 monitor_hd = hd;
2542 hide_banner = !show_banner;
2544 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2547 /* XXX: use threads ? */
2548 /* modal monitor readline */
2549 static int monitor_readline_started;
2550 static char *monitor_readline_buf;
2551 static int monitor_readline_buf_size;
2553 static void monitor_readline_cb(void *opaque, const char *input)
2555 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2556 monitor_readline_started = 0;
2559 void monitor_readline(const char *prompt, int is_password,
2560 char *buf, int buf_size)
2562 if (is_password) {
2563 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2565 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2566 monitor_readline_buf = buf;
2567 monitor_readline_buf_size = buf_size;
2568 monitor_readline_started = 1;
2569 while (monitor_readline_started) {
2570 main_loop_wait(10);