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