Work around dhclient brokenness
[qemu-kvm/fedora.git] / monitor.c
blob32d39d6580e3085dc61aaef32c5bbcfd8fc774a2
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>
39 #include "qemu-timer.h"
41 #include "qemu-kvm.h"
43 //#define DEBUG
44 //#define DEBUG_COMPLETION
46 #ifndef offsetof
47 #define offsetof(type, field) ((size_t) &((type *)0)->field)
48 #endif
51 * Supported types:
53 * 'F' filename
54 * 'B' block device name
55 * 's' string (accept optional quote)
56 * 'i' 32 bit integer
57 * 'l' target long (32 or 64 bit)
58 * '/' optional gdb-like print format (like "/10x")
60 * '?' optional type (for 'F', 's' and 'i')
64 typedef struct term_cmd_t {
65 const char *name;
66 const char *args_type;
67 void (*handler)();
68 const char *params;
69 const char *help;
70 } term_cmd_t;
72 #define MAX_MON 4
73 static CharDriverState *monitor_hd[MAX_MON];
74 static int hide_banner;
76 static term_cmd_t term_cmds[];
77 static term_cmd_t info_cmds[];
79 static uint8_t term_outbuf[1024];
80 static int term_outbuf_index;
82 CPUState *mon_cpu = NULL;
84 void term_flush(void)
86 int i;
87 if (term_outbuf_index > 0) {
88 for (i = 0; i < MAX_MON; i++)
89 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
90 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
91 term_outbuf_index = 0;
95 /* flush at every end of line or if the buffer is full */
96 void term_puts(const char *str)
98 char c;
99 for(;;) {
100 c = *str++;
101 if (c == '\0')
102 break;
103 if (c == '\n')
104 term_outbuf[term_outbuf_index++] = '\r';
105 term_outbuf[term_outbuf_index++] = c;
106 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
107 c == '\n')
108 term_flush();
112 void term_vprintf(const char *fmt, va_list ap)
114 char buf[4096];
115 vsnprintf(buf, sizeof(buf), fmt, ap);
116 term_puts(buf);
119 void term_printf(const char *fmt, ...)
121 va_list ap;
122 va_start(ap, fmt);
123 term_vprintf(fmt, ap);
124 va_end(ap);
127 void term_print_filename(const char *filename)
129 int i;
131 for (i = 0; filename[i]; i++) {
132 switch (filename[i]) {
133 case ' ':
134 case '"':
135 case '\\':
136 term_printf("\\%c", filename[i]);
137 break;
138 case '\t':
139 term_printf("\\t");
140 break;
141 case '\r':
142 term_printf("\\r");
143 break;
144 case '\n':
145 term_printf("\\n");
146 break;
147 default:
148 term_printf("%c", filename[i]);
149 break;
154 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
156 va_list ap;
157 va_start(ap, fmt);
158 term_vprintf(fmt, ap);
159 va_end(ap);
160 return 0;
163 static int compare_cmd(const char *name, const char *list)
165 const char *p, *pstart;
166 int len;
167 len = strlen(name);
168 p = list;
169 for(;;) {
170 pstart = p;
171 p = strchr(p, '|');
172 if (!p)
173 p = pstart + strlen(pstart);
174 if ((p - pstart) == len && !memcmp(pstart, name, len))
175 return 1;
176 if (*p == '\0')
177 break;
178 p++;
180 return 0;
183 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
185 term_cmd_t *cmd;
187 for(cmd = cmds; cmd->name != NULL; cmd++) {
188 if (!name || !strcmp(name, cmd->name))
189 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
193 static void help_cmd(const char *name)
195 if (name && !strcmp(name, "info")) {
196 help_cmd1(info_cmds, "info ", NULL);
197 } else {
198 help_cmd1(term_cmds, "", name);
199 if (name && !strcmp(name, "log")) {
200 CPULogItem *item;
201 term_printf("Log items (comma separated):\n");
202 term_printf("%-10s %s\n", "none", "remove all logs");
203 for(item = cpu_log_items; item->mask != 0; item++) {
204 term_printf("%-10s %s\n", item->name, item->help);
210 static void do_help(const char *name)
212 help_cmd(name);
215 static void do_commit(const char *device)
217 int i, all_devices;
219 all_devices = !strcmp(device, "all");
220 for (i = 0; i < nb_drives; i++) {
221 if (all_devices ||
222 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
223 bdrv_commit(drives_table[i].bdrv);
227 static void do_info(const char *item)
229 term_cmd_t *cmd;
231 if (!item)
232 goto help;
233 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
234 if (compare_cmd(item, cmd->name))
235 goto found;
237 help:
238 help_cmd("info");
239 return;
240 found:
241 cmd->handler();
244 static void do_info_version(void)
246 term_printf("%s\n", QEMU_VERSION);
249 static void do_info_name(void)
251 if (qemu_name)
252 term_printf("%s\n", qemu_name);
255 static void do_info_block(void)
257 bdrv_info();
260 static void do_info_blockstats(void)
262 bdrv_info_stats();
265 /* get the current CPU defined by the user */
266 static int mon_set_cpu(int cpu_index)
268 CPUState *env;
270 for(env = first_cpu; env != NULL; env = env->next_cpu) {
271 if (env->cpu_index == cpu_index) {
272 mon_cpu = env;
273 return 0;
276 return -1;
279 static CPUState *mon_get_cpu(void)
281 if (!mon_cpu) {
282 mon_set_cpu(0);
285 kvm_save_registers(mon_cpu);
287 return mon_cpu;
290 static void do_info_registers(void)
292 CPUState *env;
293 env = mon_get_cpu();
294 if (!env)
295 return;
296 #ifdef TARGET_I386
297 cpu_dump_state(env, NULL, monitor_fprintf,
298 X86_DUMP_FPU);
299 #else
300 cpu_dump_state(env, NULL, monitor_fprintf,
302 #endif
305 static void do_info_cpus(void)
307 CPUState *env;
309 /* just to set the default cpu if not already done */
310 mon_get_cpu();
312 for(env = first_cpu; env != NULL; env = env->next_cpu) {
313 kvm_save_registers(env);
314 term_printf("%c CPU #%d:",
315 (env == mon_cpu) ? '*' : ' ',
316 env->cpu_index);
317 #if defined(TARGET_I386)
318 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
319 #elif defined(TARGET_PPC)
320 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
321 #elif defined(TARGET_SPARC)
322 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
323 #elif defined(TARGET_MIPS)
324 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
325 #endif
326 if (env->halted)
327 term_printf(" (halted)");
328 term_printf(" thread_id=%d", env->thread_id);
329 term_printf("\n");
333 static void do_cpu_set(int index)
335 if (mon_set_cpu(index) < 0)
336 term_printf("Invalid CPU index\n");
339 static void do_cpu_set_nr(int value, const char *status)
341 int state;
343 if (!strcmp(status, "online"))
344 state = 1;
345 else if (!strcmp(status, "offline"))
346 state = 0;
347 else {
348 term_printf("invalid status: %s\n", status);
349 return;
351 #if defined(TARGET_I386) || defined(TARGET_X86_64)
352 qemu_system_cpu_hot_add(value, state);
353 #endif
356 static void do_info_jit(void)
358 dump_exec_info(NULL, monitor_fprintf);
361 static void do_info_history (void)
363 int i;
364 const char *str;
366 i = 0;
367 for(;;) {
368 str = readline_get_history(i);
369 if (!str)
370 break;
371 term_printf("%d: '%s'\n", i, str);
372 i++;
376 #if defined(TARGET_PPC)
377 /* XXX: not implemented in other targets */
378 static void do_info_cpu_stats (void)
380 CPUState *env;
382 env = mon_get_cpu();
383 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
385 #endif
387 static void do_quit(void)
389 exit(0);
392 static int eject_device(BlockDriverState *bs, int force)
394 if (bdrv_is_inserted(bs)) {
395 if (!force) {
396 if (!bdrv_is_removable(bs)) {
397 term_printf("device is not removable\n");
398 return -1;
400 if (bdrv_is_locked(bs)) {
401 term_printf("device is locked\n");
402 return -1;
405 bdrv_close(bs);
407 return 0;
410 static void do_eject(int force, const char *filename)
412 BlockDriverState *bs;
414 bs = bdrv_find(filename);
415 if (!bs) {
416 term_printf("device not found\n");
417 return;
419 eject_device(bs, force);
422 static void do_change_block(const char *device, const char *filename, const char *fmt)
424 BlockDriverState *bs;
425 BlockDriver *drv = NULL;
427 bs = bdrv_find(device);
428 if (!bs) {
429 term_printf("device not found\n");
430 return;
432 if (fmt) {
433 drv = bdrv_find_format(fmt);
434 if (!drv) {
435 term_printf("invalid format %s\n", fmt);
436 return;
439 if (eject_device(bs, 0) < 0)
440 return;
441 bdrv_open2(bs, filename, 0, drv);
442 qemu_key_check(bs, filename);
445 static void do_change_vnc(const char *target)
447 if (strcmp(target, "passwd") == 0 ||
448 strcmp(target, "password") == 0) {
449 char password[9];
450 monitor_readline("Password: ", 1, password, sizeof(password)-1);
451 password[sizeof(password)-1] = '\0';
452 if (vnc_display_password(NULL, password) < 0)
453 term_printf("could not set VNC server password\n");
454 } else {
455 if (vnc_display_open(NULL, target) < 0)
456 term_printf("could not start VNC server on %s\n", target);
460 static void do_change(const char *device, const char *target, const char *fmt)
462 if (strcmp(device, "vnc") == 0) {
463 do_change_vnc(target);
464 } else {
465 do_change_block(device, target, fmt);
469 static void do_screen_dump(const char *filename)
471 vga_hw_screen_dump(filename);
474 static void do_logfile(const char *filename)
476 cpu_set_log_filename(filename);
479 static void do_log(const char *items)
481 int mask;
483 if (!strcmp(items, "none")) {
484 mask = 0;
485 } else {
486 mask = cpu_str_to_log_mask(items);
487 if (!mask) {
488 help_cmd("log");
489 return;
492 cpu_set_log(mask);
495 static void do_stop(void)
497 vm_stop(EXCP_INTERRUPT);
500 static void do_cont(void)
502 vm_start();
505 #ifdef CONFIG_GDBSTUB
506 static void do_gdbserver(const char *port)
508 if (!port)
509 port = DEFAULT_GDBSTUB_PORT;
510 if (gdbserver_start(port) < 0) {
511 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
512 } else {
513 qemu_printf("Waiting gdb connection on port '%s'\n", port);
516 #endif
518 static void term_printc(int c)
520 term_printf("'");
521 switch(c) {
522 case '\'':
523 term_printf("\\'");
524 break;
525 case '\\':
526 term_printf("\\\\");
527 break;
528 case '\n':
529 term_printf("\\n");
530 break;
531 case '\r':
532 term_printf("\\r");
533 break;
534 default:
535 if (c >= 32 && c <= 126) {
536 term_printf("%c", c);
537 } else {
538 term_printf("\\x%02x", c);
540 break;
542 term_printf("'");
545 static void memory_dump(int count, int format, int wsize,
546 target_phys_addr_t addr, int is_physical)
548 CPUState *env;
549 int nb_per_line, l, line_size, i, max_digits, len;
550 uint8_t buf[16];
551 uint64_t v;
553 if (format == 'i') {
554 int flags;
555 flags = 0;
556 env = mon_get_cpu();
557 if (!env && !is_physical)
558 return;
559 #ifdef TARGET_I386
560 if (wsize == 2) {
561 flags = 1;
562 } else if (wsize == 4) {
563 flags = 0;
564 } else {
565 /* as default we use the current CS size */
566 flags = 0;
567 if (env) {
568 #ifdef TARGET_X86_64
569 if ((env->efer & MSR_EFER_LMA) &&
570 (env->segs[R_CS].flags & DESC_L_MASK))
571 flags = 2;
572 else
573 #endif
574 if (!(env->segs[R_CS].flags & DESC_B_MASK))
575 flags = 1;
578 #endif
579 monitor_disas(env, addr, count, is_physical, flags);
580 return;
583 len = wsize * count;
584 if (wsize == 1)
585 line_size = 8;
586 else
587 line_size = 16;
588 nb_per_line = line_size / wsize;
589 max_digits = 0;
591 switch(format) {
592 case 'o':
593 max_digits = (wsize * 8 + 2) / 3;
594 break;
595 default:
596 case 'x':
597 max_digits = (wsize * 8) / 4;
598 break;
599 case 'u':
600 case 'd':
601 max_digits = (wsize * 8 * 10 + 32) / 33;
602 break;
603 case 'c':
604 wsize = 1;
605 break;
608 while (len > 0) {
609 if (is_physical)
610 term_printf(TARGET_FMT_plx ":", addr);
611 else
612 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
613 l = len;
614 if (l > line_size)
615 l = line_size;
616 if (is_physical) {
617 cpu_physical_memory_rw(addr, buf, l, 0);
618 } else {
619 env = mon_get_cpu();
620 if (!env)
621 break;
622 cpu_memory_rw_debug(env, addr, buf, l, 0);
624 i = 0;
625 while (i < l) {
626 switch(wsize) {
627 default:
628 case 1:
629 v = ldub_raw(buf + i);
630 break;
631 case 2:
632 v = lduw_raw(buf + i);
633 break;
634 case 4:
635 v = (uint32_t)ldl_raw(buf + i);
636 break;
637 case 8:
638 v = ldq_raw(buf + i);
639 break;
641 term_printf(" ");
642 switch(format) {
643 case 'o':
644 term_printf("%#*" PRIo64, max_digits, v);
645 break;
646 case 'x':
647 term_printf("0x%0*" PRIx64, max_digits, v);
648 break;
649 case 'u':
650 term_printf("%*" PRIu64, max_digits, v);
651 break;
652 case 'd':
653 term_printf("%*" PRId64, max_digits, v);
654 break;
655 case 'c':
656 term_printc(v);
657 break;
659 i += wsize;
661 term_printf("\n");
662 addr += l;
663 len -= l;
667 #if TARGET_LONG_BITS == 64
668 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
669 #else
670 #define GET_TLONG(h, l) (l)
671 #endif
673 static void do_memory_dump(int count, int format, int size,
674 uint32_t addrh, uint32_t addrl)
676 target_long addr = GET_TLONG(addrh, addrl);
677 memory_dump(count, format, size, addr, 0);
680 #if TARGET_PHYS_ADDR_BITS > 32
681 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
682 #else
683 #define GET_TPHYSADDR(h, l) (l)
684 #endif
686 static void do_physical_memory_dump(int count, int format, int size,
687 uint32_t addrh, uint32_t addrl)
690 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
691 memory_dump(count, format, size, addr, 1);
694 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
696 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
697 #if TARGET_PHYS_ADDR_BITS == 32
698 switch(format) {
699 case 'o':
700 term_printf("%#o", val);
701 break;
702 case 'x':
703 term_printf("%#x", val);
704 break;
705 case 'u':
706 term_printf("%u", val);
707 break;
708 default:
709 case 'd':
710 term_printf("%d", val);
711 break;
712 case 'c':
713 term_printc(val);
714 break;
716 #else
717 switch(format) {
718 case 'o':
719 term_printf("%#" PRIo64, val);
720 break;
721 case 'x':
722 term_printf("%#" PRIx64, val);
723 break;
724 case 'u':
725 term_printf("%" PRIu64, val);
726 break;
727 default:
728 case 'd':
729 term_printf("%" PRId64, val);
730 break;
731 case 'c':
732 term_printc(val);
733 break;
735 #endif
736 term_printf("\n");
739 static void do_memory_save(unsigned int valh, unsigned int vall,
740 uint32_t size, const char *filename)
742 FILE *f;
743 target_long addr = GET_TLONG(valh, vall);
744 uint32_t l;
745 CPUState *env;
746 uint8_t buf[1024];
748 env = mon_get_cpu();
749 if (!env)
750 return;
752 f = fopen(filename, "wb");
753 if (!f) {
754 term_printf("could not open '%s'\n", filename);
755 return;
757 while (size != 0) {
758 l = sizeof(buf);
759 if (l > size)
760 l = size;
761 cpu_memory_rw_debug(env, addr, buf, l, 0);
762 fwrite(buf, 1, l, f);
763 addr += l;
764 size -= l;
766 fclose(f);
769 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
770 uint32_t size, const char *filename)
772 FILE *f;
773 uint32_t l;
774 uint8_t buf[1024];
775 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
777 f = fopen(filename, "wb");
778 if (!f) {
779 term_printf("could not open '%s'\n", filename);
780 return;
782 while (size != 0) {
783 l = sizeof(buf);
784 if (l > size)
785 l = size;
786 cpu_physical_memory_rw(addr, buf, l, 0);
787 fwrite(buf, 1, l, f);
788 fflush(f);
789 addr += l;
790 size -= l;
792 fclose(f);
795 static void do_sum(uint32_t start, uint32_t size)
797 uint32_t addr;
798 uint8_t buf[1];
799 uint16_t sum;
801 sum = 0;
802 for(addr = start; addr < (start + size); addr++) {
803 cpu_physical_memory_rw(addr, buf, 1, 0);
804 /* BSD sum algorithm ('sum' Unix command) */
805 sum = (sum >> 1) | (sum << 15);
806 sum += buf[0];
808 term_printf("%05d\n", sum);
811 typedef struct {
812 int keycode;
813 const char *name;
814 } KeyDef;
816 static const KeyDef key_defs[] = {
817 { 0x2a, "shift" },
818 { 0x36, "shift_r" },
820 { 0x38, "alt" },
821 { 0xb8, "alt_r" },
822 { 0x64, "altgr" },
823 { 0xe4, "altgr_r" },
824 { 0x1d, "ctrl" },
825 { 0x9d, "ctrl_r" },
827 { 0xdd, "menu" },
829 { 0x01, "esc" },
831 { 0x02, "1" },
832 { 0x03, "2" },
833 { 0x04, "3" },
834 { 0x05, "4" },
835 { 0x06, "5" },
836 { 0x07, "6" },
837 { 0x08, "7" },
838 { 0x09, "8" },
839 { 0x0a, "9" },
840 { 0x0b, "0" },
841 { 0x0c, "minus" },
842 { 0x0d, "equal" },
843 { 0x0e, "backspace" },
845 { 0x0f, "tab" },
846 { 0x10, "q" },
847 { 0x11, "w" },
848 { 0x12, "e" },
849 { 0x13, "r" },
850 { 0x14, "t" },
851 { 0x15, "y" },
852 { 0x16, "u" },
853 { 0x17, "i" },
854 { 0x18, "o" },
855 { 0x19, "p" },
857 { 0x1c, "ret" },
859 { 0x1e, "a" },
860 { 0x1f, "s" },
861 { 0x20, "d" },
862 { 0x21, "f" },
863 { 0x22, "g" },
864 { 0x23, "h" },
865 { 0x24, "j" },
866 { 0x25, "k" },
867 { 0x26, "l" },
869 { 0x2c, "z" },
870 { 0x2d, "x" },
871 { 0x2e, "c" },
872 { 0x2f, "v" },
873 { 0x30, "b" },
874 { 0x31, "n" },
875 { 0x32, "m" },
877 { 0x37, "asterisk" },
879 { 0x39, "spc" },
880 { 0x3a, "caps_lock" },
881 { 0x3b, "f1" },
882 { 0x3c, "f2" },
883 { 0x3d, "f3" },
884 { 0x3e, "f4" },
885 { 0x3f, "f5" },
886 { 0x40, "f6" },
887 { 0x41, "f7" },
888 { 0x42, "f8" },
889 { 0x43, "f9" },
890 { 0x44, "f10" },
891 { 0x45, "num_lock" },
892 { 0x46, "scroll_lock" },
894 { 0xb5, "kp_divide" },
895 { 0x37, "kp_multiply" },
896 { 0x4a, "kp_subtract" },
897 { 0x4e, "kp_add" },
898 { 0x9c, "kp_enter" },
899 { 0x53, "kp_decimal" },
900 { 0x54, "sysrq" },
902 { 0x52, "kp_0" },
903 { 0x4f, "kp_1" },
904 { 0x50, "kp_2" },
905 { 0x51, "kp_3" },
906 { 0x4b, "kp_4" },
907 { 0x4c, "kp_5" },
908 { 0x4d, "kp_6" },
909 { 0x47, "kp_7" },
910 { 0x48, "kp_8" },
911 { 0x49, "kp_9" },
913 { 0x56, "<" },
915 { 0x57, "f11" },
916 { 0x58, "f12" },
918 { 0xb7, "print" },
920 { 0xc7, "home" },
921 { 0xc9, "pgup" },
922 { 0xd1, "pgdn" },
923 { 0xcf, "end" },
925 { 0xcb, "left" },
926 { 0xc8, "up" },
927 { 0xd0, "down" },
928 { 0xcd, "right" },
930 { 0xd2, "insert" },
931 { 0xd3, "delete" },
932 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
933 { 0xf0, "stop" },
934 { 0xf1, "again" },
935 { 0xf2, "props" },
936 { 0xf3, "undo" },
937 { 0xf4, "front" },
938 { 0xf5, "copy" },
939 { 0xf6, "open" },
940 { 0xf7, "paste" },
941 { 0xf8, "find" },
942 { 0xf9, "cut" },
943 { 0xfa, "lf" },
944 { 0xfb, "help" },
945 { 0xfc, "meta_l" },
946 { 0xfd, "meta_r" },
947 { 0xfe, "compose" },
948 #endif
949 { 0, NULL },
952 static int get_keycode(const char *key)
954 const KeyDef *p;
955 char *endp;
956 int ret;
958 for(p = key_defs; p->name != NULL; p++) {
959 if (!strcmp(key, p->name))
960 return p->keycode;
962 if (strstart(key, "0x", NULL)) {
963 ret = strtoul(key, &endp, 0);
964 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
965 return ret;
967 return -1;
970 #define MAX_KEYCODES 16
971 static uint8_t keycodes[MAX_KEYCODES];
972 static int nb_pending_keycodes;
973 static QEMUTimer *key_timer;
975 static void release_keys(void *opaque)
977 int keycode;
979 while (nb_pending_keycodes > 0) {
980 nb_pending_keycodes--;
981 keycode = keycodes[nb_pending_keycodes];
982 if (keycode & 0x80)
983 kbd_put_keycode(0xe0);
984 kbd_put_keycode(keycode | 0x80);
988 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
990 char keyname_buf[16];
991 char *separator;
992 int keyname_len, keycode, i;
994 if (nb_pending_keycodes > 0) {
995 qemu_del_timer(key_timer);
996 release_keys(NULL);
998 if (!has_hold_time)
999 hold_time = 100;
1000 i = 0;
1001 while (1) {
1002 separator = strchr(string, '-');
1003 keyname_len = separator ? separator - string : strlen(string);
1004 if (keyname_len > 0) {
1005 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1006 if (keyname_len > sizeof(keyname_buf) - 1) {
1007 term_printf("invalid key: '%s...'\n", keyname_buf);
1008 return;
1010 if (i == MAX_KEYCODES) {
1011 term_printf("too many keys\n");
1012 return;
1014 keyname_buf[keyname_len] = 0;
1015 keycode = get_keycode(keyname_buf);
1016 if (keycode < 0) {
1017 term_printf("unknown key: '%s'\n", keyname_buf);
1018 return;
1020 keycodes[i++] = keycode;
1022 if (!separator)
1023 break;
1024 string = separator + 1;
1026 nb_pending_keycodes = i;
1027 /* key down events */
1028 for (i = 0; i < nb_pending_keycodes; i++) {
1029 keycode = keycodes[i];
1030 if (keycode & 0x80)
1031 kbd_put_keycode(0xe0);
1032 kbd_put_keycode(keycode & 0x7f);
1034 /* delayed key up events */
1035 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1036 muldiv64(ticks_per_sec, hold_time, 1000));
1039 static int mouse_button_state;
1041 static void do_mouse_move(const char *dx_str, const char *dy_str,
1042 const char *dz_str)
1044 int dx, dy, dz;
1045 dx = strtol(dx_str, NULL, 0);
1046 dy = strtol(dy_str, NULL, 0);
1047 dz = 0;
1048 if (dz_str)
1049 dz = strtol(dz_str, NULL, 0);
1050 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1053 static void do_mouse_button(int button_state)
1055 mouse_button_state = button_state;
1056 kbd_mouse_event(0, 0, 0, mouse_button_state);
1059 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1061 uint32_t val;
1062 int suffix;
1064 if (has_index) {
1065 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1066 addr++;
1068 addr &= 0xffff;
1070 switch(size) {
1071 default:
1072 case 1:
1073 val = cpu_inb(NULL, addr);
1074 suffix = 'b';
1075 break;
1076 case 2:
1077 val = cpu_inw(NULL, addr);
1078 suffix = 'w';
1079 break;
1080 case 4:
1081 val = cpu_inl(NULL, addr);
1082 suffix = 'l';
1083 break;
1085 term_printf("port%c[0x%04x] = %#0*x\n",
1086 suffix, addr, size * 2, val);
1089 /* boot_set handler */
1090 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1091 static void *boot_opaque;
1093 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1095 qemu_boot_set_handler = func;
1096 boot_opaque = opaque;
1099 static void do_boot_set(const char *bootdevice)
1101 int res;
1103 if (qemu_boot_set_handler) {
1104 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1105 if (res == 0)
1106 term_printf("boot device list now set to %s\n", bootdevice);
1107 else
1108 term_printf("setting boot device list failed with error %i\n", res);
1109 } else {
1110 term_printf("no function defined to set boot device list for this architecture\n");
1114 static void do_system_reset(void)
1116 qemu_system_reset_request();
1119 static void do_system_powerdown(void)
1121 qemu_system_powerdown_request();
1124 #if defined(TARGET_I386)
1125 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1127 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1128 addr,
1129 pte & mask,
1130 pte & PG_GLOBAL_MASK ? 'G' : '-',
1131 pte & PG_PSE_MASK ? 'P' : '-',
1132 pte & PG_DIRTY_MASK ? 'D' : '-',
1133 pte & PG_ACCESSED_MASK ? 'A' : '-',
1134 pte & PG_PCD_MASK ? 'C' : '-',
1135 pte & PG_PWT_MASK ? 'T' : '-',
1136 pte & PG_USER_MASK ? 'U' : '-',
1137 pte & PG_RW_MASK ? 'W' : '-');
1140 static void tlb_info(void)
1142 CPUState *env;
1143 int l1, l2;
1144 uint32_t pgd, pde, pte;
1146 env = mon_get_cpu();
1147 if (!env)
1148 return;
1150 if (!(env->cr[0] & CR0_PG_MASK)) {
1151 term_printf("PG disabled\n");
1152 return;
1154 pgd = env->cr[3] & ~0xfff;
1155 for(l1 = 0; l1 < 1024; l1++) {
1156 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1157 pde = le32_to_cpu(pde);
1158 if (pde & PG_PRESENT_MASK) {
1159 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1160 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1161 } else {
1162 for(l2 = 0; l2 < 1024; l2++) {
1163 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1164 (uint8_t *)&pte, 4);
1165 pte = le32_to_cpu(pte);
1166 if (pte & PG_PRESENT_MASK) {
1167 print_pte((l1 << 22) + (l2 << 12),
1168 pte & ~PG_PSE_MASK,
1169 ~0xfff);
1177 static void mem_print(uint32_t *pstart, int *plast_prot,
1178 uint32_t end, int prot)
1180 int prot1;
1181 prot1 = *plast_prot;
1182 if (prot != prot1) {
1183 if (*pstart != -1) {
1184 term_printf("%08x-%08x %08x %c%c%c\n",
1185 *pstart, end, end - *pstart,
1186 prot1 & PG_USER_MASK ? 'u' : '-',
1187 'r',
1188 prot1 & PG_RW_MASK ? 'w' : '-');
1190 if (prot != 0)
1191 *pstart = end;
1192 else
1193 *pstart = -1;
1194 *plast_prot = prot;
1198 static void mem_info(void)
1200 CPUState *env;
1201 int l1, l2, prot, last_prot;
1202 uint32_t pgd, pde, pte, start, end;
1204 env = mon_get_cpu();
1205 if (!env)
1206 return;
1208 if (!(env->cr[0] & CR0_PG_MASK)) {
1209 term_printf("PG disabled\n");
1210 return;
1212 pgd = env->cr[3] & ~0xfff;
1213 last_prot = 0;
1214 start = -1;
1215 for(l1 = 0; l1 < 1024; l1++) {
1216 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1217 pde = le32_to_cpu(pde);
1218 end = l1 << 22;
1219 if (pde & PG_PRESENT_MASK) {
1220 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1221 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1222 mem_print(&start, &last_prot, end, prot);
1223 } else {
1224 for(l2 = 0; l2 < 1024; l2++) {
1225 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1226 (uint8_t *)&pte, 4);
1227 pte = le32_to_cpu(pte);
1228 end = (l1 << 22) + (l2 << 12);
1229 if (pte & PG_PRESENT_MASK) {
1230 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1231 } else {
1232 prot = 0;
1234 mem_print(&start, &last_prot, end, prot);
1237 } else {
1238 prot = 0;
1239 mem_print(&start, &last_prot, end, prot);
1243 #endif
1245 static void do_info_kqemu(void)
1247 #ifdef USE_KQEMU
1248 CPUState *env;
1249 int val;
1250 val = 0;
1251 env = mon_get_cpu();
1252 if (!env) {
1253 term_printf("No cpu initialized yet");
1254 return;
1256 val = env->kqemu_enabled;
1257 term_printf("kqemu support: ");
1258 switch(val) {
1259 default:
1260 case 0:
1261 term_printf("disabled\n");
1262 break;
1263 case 1:
1264 term_printf("enabled for user code\n");
1265 break;
1266 case 2:
1267 term_printf("enabled for user and kernel code\n");
1268 break;
1270 #else
1271 term_printf("kqemu support: not compiled\n");
1272 #endif
1275 static void do_info_kvm(void)
1277 #ifdef USE_KVM
1278 term_printf("kvm support: ");
1279 if (kvm_enabled())
1280 term_printf("enabled\n");
1281 else
1282 term_printf("disabled\n");
1283 #else
1284 term_printf("kvm support: not compiled\n");
1285 #endif
1288 #ifdef CONFIG_PROFILER
1290 int64_t kqemu_time;
1291 int64_t qemu_time;
1292 int64_t kqemu_exec_count;
1293 int64_t dev_time;
1294 int64_t kqemu_ret_int_count;
1295 int64_t kqemu_ret_excp_count;
1296 int64_t kqemu_ret_intr_count;
1298 static void do_info_profile(void)
1300 int64_t total;
1301 total = qemu_time;
1302 if (total == 0)
1303 total = 1;
1304 term_printf("async time %" PRId64 " (%0.3f)\n",
1305 dev_time, dev_time / (double)ticks_per_sec);
1306 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1307 qemu_time, qemu_time / (double)ticks_per_sec);
1308 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1309 kqemu_time, kqemu_time / (double)ticks_per_sec,
1310 kqemu_time / (double)total * 100.0,
1311 kqemu_exec_count,
1312 kqemu_ret_int_count,
1313 kqemu_ret_excp_count,
1314 kqemu_ret_intr_count);
1315 qemu_time = 0;
1316 kqemu_time = 0;
1317 kqemu_exec_count = 0;
1318 dev_time = 0;
1319 kqemu_ret_int_count = 0;
1320 kqemu_ret_excp_count = 0;
1321 kqemu_ret_intr_count = 0;
1322 #ifdef USE_KQEMU
1323 kqemu_record_dump();
1324 #endif
1326 #else
1327 static void do_info_profile(void)
1329 term_printf("Internal profiler not compiled\n");
1331 #endif
1333 /* Capture support */
1334 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1336 static void do_info_capture (void)
1338 int i;
1339 CaptureState *s;
1341 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1342 term_printf ("[%d]: ", i);
1343 s->ops.info (s->opaque);
1347 static void do_stop_capture (int n)
1349 int i;
1350 CaptureState *s;
1352 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1353 if (i == n) {
1354 s->ops.destroy (s->opaque);
1355 LIST_REMOVE (s, entries);
1356 qemu_free (s);
1357 return;
1362 #ifdef HAS_AUDIO
1363 int wav_start_capture (CaptureState *s, const char *path, int freq,
1364 int bits, int nchannels);
1366 static void do_wav_capture (const char *path,
1367 int has_freq, int freq,
1368 int has_bits, int bits,
1369 int has_channels, int nchannels)
1371 CaptureState *s;
1373 s = qemu_mallocz (sizeof (*s));
1374 if (!s) {
1375 term_printf ("Not enough memory to add wave capture\n");
1376 return;
1379 freq = has_freq ? freq : 44100;
1380 bits = has_bits ? bits : 16;
1381 nchannels = has_channels ? nchannels : 2;
1383 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1384 term_printf ("Faied to add wave capture\n");
1385 qemu_free (s);
1387 LIST_INSERT_HEAD (&capture_head, s, entries);
1389 #endif
1391 #if defined(TARGET_I386)
1392 static void do_inject_nmi(int cpu_index)
1394 CPUState *env;
1396 for (env = first_cpu; env != NULL; env = env->next_cpu)
1397 if (env->cpu_index == cpu_index) {
1398 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1399 break;
1402 #endif
1404 static term_cmd_t term_cmds[] = {
1405 { "help|?", "s?", do_help,
1406 "[cmd]", "show the help" },
1407 { "commit", "s", do_commit,
1408 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1409 { "info", "s?", do_info,
1410 "subcommand", "show various information about the system state" },
1411 { "q|quit", "", do_quit,
1412 "", "quit the emulator" },
1413 { "eject", "-fB", do_eject,
1414 "[-f] device", "eject a removable medium (use -f to force it)" },
1415 { "change", "BFs?", do_change,
1416 "device filename [format]", "change a removable medium, optional format" },
1417 { "screendump", "F", do_screen_dump,
1418 "filename", "save screen into PPM image 'filename'" },
1419 { "logfile", "F", do_logfile,
1420 "filename", "output logs to 'filename'" },
1421 { "log", "s", do_log,
1422 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1423 { "savevm", "s?", do_savevm,
1424 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1425 { "loadvm", "s", do_loadvm,
1426 "tag|id", "restore a VM snapshot from its tag or id" },
1427 { "delvm", "s", do_delvm,
1428 "tag|id", "delete a VM snapshot from its tag or id" },
1429 { "stop", "", do_stop,
1430 "", "stop emulation", },
1431 { "c|cont", "", do_cont,
1432 "", "resume emulation", },
1433 #ifdef CONFIG_GDBSTUB
1434 { "gdbserver", "s?", do_gdbserver,
1435 "[port]", "start gdbserver session (default port=1234)", },
1436 #endif
1437 { "x", "/l", do_memory_dump,
1438 "/fmt addr", "virtual memory dump starting at 'addr'", },
1439 { "xp", "/l", do_physical_memory_dump,
1440 "/fmt addr", "physical memory dump starting at 'addr'", },
1441 { "p|print", "/l", do_print,
1442 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1443 { "i", "/ii.", do_ioport_read,
1444 "/fmt addr", "I/O port read" },
1446 { "sendkey", "si?", do_sendkey,
1447 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1448 { "system_reset", "", do_system_reset,
1449 "", "reset the system" },
1450 { "system_powerdown", "", do_system_powerdown,
1451 "", "send system power down event" },
1452 { "sum", "ii", do_sum,
1453 "addr size", "compute the checksum of a memory region" },
1454 { "usb_add", "s", do_usb_add,
1455 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1456 { "usb_del", "s", do_usb_del,
1457 "device", "remove USB device 'bus.addr'" },
1458 { "cpu", "i", do_cpu_set,
1459 "index", "set the default CPU" },
1460 { "mouse_move", "sss?", do_mouse_move,
1461 "dx dy [dz]", "send mouse move events" },
1462 { "mouse_button", "i", do_mouse_button,
1463 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1464 { "mouse_set", "i", do_mouse_set,
1465 "index", "set which mouse device receives events" },
1466 #ifdef HAS_AUDIO
1467 { "wavcapture", "si?i?i?", do_wav_capture,
1468 "path [frequency bits channels]",
1469 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1470 #endif
1471 { "stopcapture", "i", do_stop_capture,
1472 "capture index", "stop capture" },
1473 { "memsave", "lis", do_memory_save,
1474 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1475 { "pmemsave", "lis", do_physical_memory_save,
1476 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1477 { "boot_set", "s", do_boot_set,
1478 "bootdevice", "define new values for the boot device list" },
1479 #if defined(TARGET_I386)
1480 { "nmi", "i", do_inject_nmi,
1481 "cpu", "inject an NMI on the given CPU", },
1482 #endif
1483 { "migrate", "-ds", do_migrate,
1484 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1485 { "migrate_cancel", "", do_migrate_cancel,
1486 "", "cancel the current VM migration" },
1487 { "migrate_set_speed", "s", do_migrate_set_speed,
1488 "value", "set maximum speed (in bytes) for migrations" },
1489 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1490 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1491 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1492 "[,unit=m][,media=d][index=i]\n"
1493 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1494 "[snapshot=on|off][,cache=on|off]",
1495 "add drive to PCI storage controller" },
1496 { "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" },
1497 { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" },
1498 #endif
1499 { NULL, NULL, },
1502 static term_cmd_t info_cmds[] = {
1503 { "version", "", do_info_version,
1504 "", "show the version of qemu" },
1505 { "network", "", do_info_network,
1506 "", "show the network state" },
1507 { "block", "", do_info_block,
1508 "", "show the block devices" },
1509 { "blockstats", "", do_info_blockstats,
1510 "", "show block device statistics" },
1511 { "registers", "", do_info_registers,
1512 "", "show the cpu registers" },
1513 { "cpus", "", do_info_cpus,
1514 "", "show infos for each CPU" },
1515 { "history", "", do_info_history,
1516 "", "show the command line history", },
1517 { "irq", "", irq_info,
1518 "", "show the interrupts statistics (if available)", },
1519 { "pic", "", pic_info,
1520 "", "show i8259 (PIC) state", },
1521 { "pci", "", pci_info,
1522 "", "show PCI info", },
1523 #if defined(TARGET_I386)
1524 { "tlb", "", tlb_info,
1525 "", "show virtual to physical memory mappings", },
1526 { "mem", "", mem_info,
1527 "", "show the active virtual memory mappings", },
1528 #endif
1529 { "jit", "", do_info_jit,
1530 "", "show dynamic compiler info", },
1531 { "kqemu", "", do_info_kqemu,
1532 "", "show kqemu information", },
1533 { "kvm", "", do_info_kvm,
1534 "", "show kvm information", },
1535 { "usb", "", usb_info,
1536 "", "show guest USB devices", },
1537 { "usbhost", "", usb_host_info,
1538 "", "show host USB devices", },
1539 { "profile", "", do_info_profile,
1540 "", "show profiling information", },
1541 { "capture", "", do_info_capture,
1542 "", "show capture information" },
1543 { "snapshots", "", do_info_snapshots,
1544 "", "show the currently saved VM snapshots" },
1545 { "pcmcia", "", pcmcia_info,
1546 "", "show guest PCMCIA status" },
1547 { "mice", "", do_info_mice,
1548 "", "show which guest mouse is receiving events" },
1549 { "vnc", "", do_info_vnc,
1550 "", "show the vnc server status"},
1551 { "name", "", do_info_name,
1552 "", "show the current VM name" },
1553 #if defined(TARGET_PPC)
1554 { "cpustats", "", do_info_cpu_stats,
1555 "", "show CPU statistics", },
1556 #endif
1557 #if defined(CONFIG_SLIRP)
1558 { "slirp", "", do_info_slirp,
1559 "", "show SLIRP statistics", },
1560 #endif
1561 { "migration", "", do_info_migration,
1562 "", "show migration information" },
1563 { NULL, NULL, },
1566 /*******************************************************************/
1568 static const char *pch;
1569 static jmp_buf expr_env;
1571 #define MD_TLONG 0
1572 #define MD_I32 1
1574 typedef struct MonitorDef {
1575 const char *name;
1576 int offset;
1577 target_long (*get_value)(struct MonitorDef *md, int val);
1578 int type;
1579 } MonitorDef;
1581 #if defined(TARGET_I386)
1582 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1584 CPUState *env = mon_get_cpu();
1585 if (!env)
1586 return 0;
1587 return env->eip + env->segs[R_CS].base;
1589 #endif
1591 #if defined(TARGET_PPC)
1592 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1594 CPUState *env = mon_get_cpu();
1595 unsigned int u;
1596 int i;
1598 if (!env)
1599 return 0;
1601 u = 0;
1602 for (i = 0; i < 8; i++)
1603 u |= env->crf[i] << (32 - (4 * i));
1605 return u;
1608 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1610 CPUState *env = mon_get_cpu();
1611 if (!env)
1612 return 0;
1613 return env->msr;
1616 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1618 CPUState *env = mon_get_cpu();
1619 if (!env)
1620 return 0;
1621 return ppc_load_xer(env);
1624 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1626 CPUState *env = mon_get_cpu();
1627 if (!env)
1628 return 0;
1629 return cpu_ppc_load_decr(env);
1632 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1634 CPUState *env = mon_get_cpu();
1635 if (!env)
1636 return 0;
1637 return cpu_ppc_load_tbu(env);
1640 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1642 CPUState *env = mon_get_cpu();
1643 if (!env)
1644 return 0;
1645 return cpu_ppc_load_tbl(env);
1647 #endif
1649 #if defined(TARGET_SPARC)
1650 #ifndef TARGET_SPARC64
1651 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1653 CPUState *env = mon_get_cpu();
1654 if (!env)
1655 return 0;
1656 return GET_PSR(env);
1658 #endif
1660 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1662 CPUState *env = mon_get_cpu();
1663 if (!env)
1664 return 0;
1665 return env->regwptr[val];
1667 #endif
1669 static MonitorDef monitor_defs[] = {
1670 #ifdef TARGET_I386
1672 #define SEG(name, seg) \
1673 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1674 { name ".base", offsetof(CPUState, segs[seg].base) },\
1675 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1677 { "eax", offsetof(CPUState, regs[0]) },
1678 { "ecx", offsetof(CPUState, regs[1]) },
1679 { "edx", offsetof(CPUState, regs[2]) },
1680 { "ebx", offsetof(CPUState, regs[3]) },
1681 { "esp|sp", offsetof(CPUState, regs[4]) },
1682 { "ebp|fp", offsetof(CPUState, regs[5]) },
1683 { "esi", offsetof(CPUState, regs[6]) },
1684 { "edi", offsetof(CPUState, regs[7]) },
1685 #ifdef TARGET_X86_64
1686 { "r8", offsetof(CPUState, regs[8]) },
1687 { "r9", offsetof(CPUState, regs[9]) },
1688 { "r10", offsetof(CPUState, regs[10]) },
1689 { "r11", offsetof(CPUState, regs[11]) },
1690 { "r12", offsetof(CPUState, regs[12]) },
1691 { "r13", offsetof(CPUState, regs[13]) },
1692 { "r14", offsetof(CPUState, regs[14]) },
1693 { "r15", offsetof(CPUState, regs[15]) },
1694 #endif
1695 { "eflags", offsetof(CPUState, eflags) },
1696 { "eip", offsetof(CPUState, eip) },
1697 SEG("cs", R_CS)
1698 SEG("ds", R_DS)
1699 SEG("es", R_ES)
1700 SEG("ss", R_SS)
1701 SEG("fs", R_FS)
1702 SEG("gs", R_GS)
1703 { "pc", 0, monitor_get_pc, },
1704 #elif defined(TARGET_PPC)
1705 /* General purpose registers */
1706 { "r0", offsetof(CPUState, gpr[0]) },
1707 { "r1", offsetof(CPUState, gpr[1]) },
1708 { "r2", offsetof(CPUState, gpr[2]) },
1709 { "r3", offsetof(CPUState, gpr[3]) },
1710 { "r4", offsetof(CPUState, gpr[4]) },
1711 { "r5", offsetof(CPUState, gpr[5]) },
1712 { "r6", offsetof(CPUState, gpr[6]) },
1713 { "r7", offsetof(CPUState, gpr[7]) },
1714 { "r8", offsetof(CPUState, gpr[8]) },
1715 { "r9", offsetof(CPUState, gpr[9]) },
1716 { "r10", offsetof(CPUState, gpr[10]) },
1717 { "r11", offsetof(CPUState, gpr[11]) },
1718 { "r12", offsetof(CPUState, gpr[12]) },
1719 { "r13", offsetof(CPUState, gpr[13]) },
1720 { "r14", offsetof(CPUState, gpr[14]) },
1721 { "r15", offsetof(CPUState, gpr[15]) },
1722 { "r16", offsetof(CPUState, gpr[16]) },
1723 { "r17", offsetof(CPUState, gpr[17]) },
1724 { "r18", offsetof(CPUState, gpr[18]) },
1725 { "r19", offsetof(CPUState, gpr[19]) },
1726 { "r20", offsetof(CPUState, gpr[20]) },
1727 { "r21", offsetof(CPUState, gpr[21]) },
1728 { "r22", offsetof(CPUState, gpr[22]) },
1729 { "r23", offsetof(CPUState, gpr[23]) },
1730 { "r24", offsetof(CPUState, gpr[24]) },
1731 { "r25", offsetof(CPUState, gpr[25]) },
1732 { "r26", offsetof(CPUState, gpr[26]) },
1733 { "r27", offsetof(CPUState, gpr[27]) },
1734 { "r28", offsetof(CPUState, gpr[28]) },
1735 { "r29", offsetof(CPUState, gpr[29]) },
1736 { "r30", offsetof(CPUState, gpr[30]) },
1737 { "r31", offsetof(CPUState, gpr[31]) },
1738 /* Floating point registers */
1739 { "f0", offsetof(CPUState, fpr[0]) },
1740 { "f1", offsetof(CPUState, fpr[1]) },
1741 { "f2", offsetof(CPUState, fpr[2]) },
1742 { "f3", offsetof(CPUState, fpr[3]) },
1743 { "f4", offsetof(CPUState, fpr[4]) },
1744 { "f5", offsetof(CPUState, fpr[5]) },
1745 { "f6", offsetof(CPUState, fpr[6]) },
1746 { "f7", offsetof(CPUState, fpr[7]) },
1747 { "f8", offsetof(CPUState, fpr[8]) },
1748 { "f9", offsetof(CPUState, fpr[9]) },
1749 { "f10", offsetof(CPUState, fpr[10]) },
1750 { "f11", offsetof(CPUState, fpr[11]) },
1751 { "f12", offsetof(CPUState, fpr[12]) },
1752 { "f13", offsetof(CPUState, fpr[13]) },
1753 { "f14", offsetof(CPUState, fpr[14]) },
1754 { "f15", offsetof(CPUState, fpr[15]) },
1755 { "f16", offsetof(CPUState, fpr[16]) },
1756 { "f17", offsetof(CPUState, fpr[17]) },
1757 { "f18", offsetof(CPUState, fpr[18]) },
1758 { "f19", offsetof(CPUState, fpr[19]) },
1759 { "f20", offsetof(CPUState, fpr[20]) },
1760 { "f21", offsetof(CPUState, fpr[21]) },
1761 { "f22", offsetof(CPUState, fpr[22]) },
1762 { "f23", offsetof(CPUState, fpr[23]) },
1763 { "f24", offsetof(CPUState, fpr[24]) },
1764 { "f25", offsetof(CPUState, fpr[25]) },
1765 { "f26", offsetof(CPUState, fpr[26]) },
1766 { "f27", offsetof(CPUState, fpr[27]) },
1767 { "f28", offsetof(CPUState, fpr[28]) },
1768 { "f29", offsetof(CPUState, fpr[29]) },
1769 { "f30", offsetof(CPUState, fpr[30]) },
1770 { "f31", offsetof(CPUState, fpr[31]) },
1771 { "fpscr", offsetof(CPUState, fpscr) },
1772 /* Next instruction pointer */
1773 { "nip|pc", offsetof(CPUState, nip) },
1774 { "lr", offsetof(CPUState, lr) },
1775 { "ctr", offsetof(CPUState, ctr) },
1776 { "decr", 0, &monitor_get_decr, },
1777 { "ccr", 0, &monitor_get_ccr, },
1778 /* Machine state register */
1779 { "msr", 0, &monitor_get_msr, },
1780 { "xer", 0, &monitor_get_xer, },
1781 { "tbu", 0, &monitor_get_tbu, },
1782 { "tbl", 0, &monitor_get_tbl, },
1783 #if defined(TARGET_PPC64)
1784 /* Address space register */
1785 { "asr", offsetof(CPUState, asr) },
1786 #endif
1787 /* Segment registers */
1788 { "sdr1", offsetof(CPUState, sdr1) },
1789 { "sr0", offsetof(CPUState, sr[0]) },
1790 { "sr1", offsetof(CPUState, sr[1]) },
1791 { "sr2", offsetof(CPUState, sr[2]) },
1792 { "sr3", offsetof(CPUState, sr[3]) },
1793 { "sr4", offsetof(CPUState, sr[4]) },
1794 { "sr5", offsetof(CPUState, sr[5]) },
1795 { "sr6", offsetof(CPUState, sr[6]) },
1796 { "sr7", offsetof(CPUState, sr[7]) },
1797 { "sr8", offsetof(CPUState, sr[8]) },
1798 { "sr9", offsetof(CPUState, sr[9]) },
1799 { "sr10", offsetof(CPUState, sr[10]) },
1800 { "sr11", offsetof(CPUState, sr[11]) },
1801 { "sr12", offsetof(CPUState, sr[12]) },
1802 { "sr13", offsetof(CPUState, sr[13]) },
1803 { "sr14", offsetof(CPUState, sr[14]) },
1804 { "sr15", offsetof(CPUState, sr[15]) },
1805 /* Too lazy to put BATs and SPRs ... */
1806 #elif defined(TARGET_SPARC)
1807 { "g0", offsetof(CPUState, gregs[0]) },
1808 { "g1", offsetof(CPUState, gregs[1]) },
1809 { "g2", offsetof(CPUState, gregs[2]) },
1810 { "g3", offsetof(CPUState, gregs[3]) },
1811 { "g4", offsetof(CPUState, gregs[4]) },
1812 { "g5", offsetof(CPUState, gregs[5]) },
1813 { "g6", offsetof(CPUState, gregs[6]) },
1814 { "g7", offsetof(CPUState, gregs[7]) },
1815 { "o0", 0, monitor_get_reg },
1816 { "o1", 1, monitor_get_reg },
1817 { "o2", 2, monitor_get_reg },
1818 { "o3", 3, monitor_get_reg },
1819 { "o4", 4, monitor_get_reg },
1820 { "o5", 5, monitor_get_reg },
1821 { "o6", 6, monitor_get_reg },
1822 { "o7", 7, monitor_get_reg },
1823 { "l0", 8, monitor_get_reg },
1824 { "l1", 9, monitor_get_reg },
1825 { "l2", 10, monitor_get_reg },
1826 { "l3", 11, monitor_get_reg },
1827 { "l4", 12, monitor_get_reg },
1828 { "l5", 13, monitor_get_reg },
1829 { "l6", 14, monitor_get_reg },
1830 { "l7", 15, monitor_get_reg },
1831 { "i0", 16, monitor_get_reg },
1832 { "i1", 17, monitor_get_reg },
1833 { "i2", 18, monitor_get_reg },
1834 { "i3", 19, monitor_get_reg },
1835 { "i4", 20, monitor_get_reg },
1836 { "i5", 21, monitor_get_reg },
1837 { "i6", 22, monitor_get_reg },
1838 { "i7", 23, monitor_get_reg },
1839 { "pc", offsetof(CPUState, pc) },
1840 { "npc", offsetof(CPUState, npc) },
1841 { "y", offsetof(CPUState, y) },
1842 #ifndef TARGET_SPARC64
1843 { "psr", 0, &monitor_get_psr, },
1844 { "wim", offsetof(CPUState, wim) },
1845 #endif
1846 { "tbr", offsetof(CPUState, tbr) },
1847 { "fsr", offsetof(CPUState, fsr) },
1848 { "f0", offsetof(CPUState, fpr[0]) },
1849 { "f1", offsetof(CPUState, fpr[1]) },
1850 { "f2", offsetof(CPUState, fpr[2]) },
1851 { "f3", offsetof(CPUState, fpr[3]) },
1852 { "f4", offsetof(CPUState, fpr[4]) },
1853 { "f5", offsetof(CPUState, fpr[5]) },
1854 { "f6", offsetof(CPUState, fpr[6]) },
1855 { "f7", offsetof(CPUState, fpr[7]) },
1856 { "f8", offsetof(CPUState, fpr[8]) },
1857 { "f9", offsetof(CPUState, fpr[9]) },
1858 { "f10", offsetof(CPUState, fpr[10]) },
1859 { "f11", offsetof(CPUState, fpr[11]) },
1860 { "f12", offsetof(CPUState, fpr[12]) },
1861 { "f13", offsetof(CPUState, fpr[13]) },
1862 { "f14", offsetof(CPUState, fpr[14]) },
1863 { "f15", offsetof(CPUState, fpr[15]) },
1864 { "f16", offsetof(CPUState, fpr[16]) },
1865 { "f17", offsetof(CPUState, fpr[17]) },
1866 { "f18", offsetof(CPUState, fpr[18]) },
1867 { "f19", offsetof(CPUState, fpr[19]) },
1868 { "f20", offsetof(CPUState, fpr[20]) },
1869 { "f21", offsetof(CPUState, fpr[21]) },
1870 { "f22", offsetof(CPUState, fpr[22]) },
1871 { "f23", offsetof(CPUState, fpr[23]) },
1872 { "f24", offsetof(CPUState, fpr[24]) },
1873 { "f25", offsetof(CPUState, fpr[25]) },
1874 { "f26", offsetof(CPUState, fpr[26]) },
1875 { "f27", offsetof(CPUState, fpr[27]) },
1876 { "f28", offsetof(CPUState, fpr[28]) },
1877 { "f29", offsetof(CPUState, fpr[29]) },
1878 { "f30", offsetof(CPUState, fpr[30]) },
1879 { "f31", offsetof(CPUState, fpr[31]) },
1880 #ifdef TARGET_SPARC64
1881 { "f32", offsetof(CPUState, fpr[32]) },
1882 { "f34", offsetof(CPUState, fpr[34]) },
1883 { "f36", offsetof(CPUState, fpr[36]) },
1884 { "f38", offsetof(CPUState, fpr[38]) },
1885 { "f40", offsetof(CPUState, fpr[40]) },
1886 { "f42", offsetof(CPUState, fpr[42]) },
1887 { "f44", offsetof(CPUState, fpr[44]) },
1888 { "f46", offsetof(CPUState, fpr[46]) },
1889 { "f48", offsetof(CPUState, fpr[48]) },
1890 { "f50", offsetof(CPUState, fpr[50]) },
1891 { "f52", offsetof(CPUState, fpr[52]) },
1892 { "f54", offsetof(CPUState, fpr[54]) },
1893 { "f56", offsetof(CPUState, fpr[56]) },
1894 { "f58", offsetof(CPUState, fpr[58]) },
1895 { "f60", offsetof(CPUState, fpr[60]) },
1896 { "f62", offsetof(CPUState, fpr[62]) },
1897 { "asi", offsetof(CPUState, asi) },
1898 { "pstate", offsetof(CPUState, pstate) },
1899 { "cansave", offsetof(CPUState, cansave) },
1900 { "canrestore", offsetof(CPUState, canrestore) },
1901 { "otherwin", offsetof(CPUState, otherwin) },
1902 { "wstate", offsetof(CPUState, wstate) },
1903 { "cleanwin", offsetof(CPUState, cleanwin) },
1904 { "fprs", offsetof(CPUState, fprs) },
1905 #endif
1906 #endif
1907 { NULL },
1910 static void expr_error(const char *fmt)
1912 term_printf(fmt);
1913 term_printf("\n");
1914 longjmp(expr_env, 1);
1917 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1918 static int get_monitor_def(target_long *pval, const char *name)
1920 MonitorDef *md;
1921 void *ptr;
1923 for(md = monitor_defs; md->name != NULL; md++) {
1924 if (compare_cmd(name, md->name)) {
1925 if (md->get_value) {
1926 *pval = md->get_value(md, md->offset);
1927 } else {
1928 CPUState *env = mon_get_cpu();
1929 if (!env)
1930 return -2;
1931 ptr = (uint8_t *)env + md->offset;
1932 switch(md->type) {
1933 case MD_I32:
1934 *pval = *(int32_t *)ptr;
1935 break;
1936 case MD_TLONG:
1937 *pval = *(target_long *)ptr;
1938 break;
1939 default:
1940 *pval = 0;
1941 break;
1944 return 0;
1947 return -1;
1950 static void next(void)
1952 if (pch != '\0') {
1953 pch++;
1954 while (isspace(*pch))
1955 pch++;
1959 static int64_t expr_sum(void);
1961 static int64_t expr_unary(void)
1963 int64_t n;
1964 char *p;
1965 int ret;
1967 switch(*pch) {
1968 case '+':
1969 next();
1970 n = expr_unary();
1971 break;
1972 case '-':
1973 next();
1974 n = -expr_unary();
1975 break;
1976 case '~':
1977 next();
1978 n = ~expr_unary();
1979 break;
1980 case '(':
1981 next();
1982 n = expr_sum();
1983 if (*pch != ')') {
1984 expr_error("')' expected");
1986 next();
1987 break;
1988 case '\'':
1989 pch++;
1990 if (*pch == '\0')
1991 expr_error("character constant expected");
1992 n = *pch;
1993 pch++;
1994 if (*pch != '\'')
1995 expr_error("missing terminating \' character");
1996 next();
1997 break;
1998 case '$':
2000 char buf[128], *q;
2001 target_long reg=0;
2003 pch++;
2004 q = buf;
2005 while ((*pch >= 'a' && *pch <= 'z') ||
2006 (*pch >= 'A' && *pch <= 'Z') ||
2007 (*pch >= '0' && *pch <= '9') ||
2008 *pch == '_' || *pch == '.') {
2009 if ((q - buf) < sizeof(buf) - 1)
2010 *q++ = *pch;
2011 pch++;
2013 while (isspace(*pch))
2014 pch++;
2015 *q = 0;
2016 ret = get_monitor_def(&reg, buf);
2017 if (ret == -1)
2018 expr_error("unknown register");
2019 else if (ret == -2)
2020 expr_error("no cpu defined");
2021 n = reg;
2023 break;
2024 case '\0':
2025 expr_error("unexpected end of expression");
2026 n = 0;
2027 break;
2028 default:
2029 #if TARGET_PHYS_ADDR_BITS > 32
2030 n = strtoull(pch, &p, 0);
2031 #else
2032 n = strtoul(pch, &p, 0);
2033 #endif
2034 if (pch == p) {
2035 expr_error("invalid char in expression");
2037 pch = p;
2038 while (isspace(*pch))
2039 pch++;
2040 break;
2042 return n;
2046 static int64_t expr_prod(void)
2048 int64_t val, val2;
2049 int op;
2051 val = expr_unary();
2052 for(;;) {
2053 op = *pch;
2054 if (op != '*' && op != '/' && op != '%')
2055 break;
2056 next();
2057 val2 = expr_unary();
2058 switch(op) {
2059 default:
2060 case '*':
2061 val *= val2;
2062 break;
2063 case '/':
2064 case '%':
2065 if (val2 == 0)
2066 expr_error("division by zero");
2067 if (op == '/')
2068 val /= val2;
2069 else
2070 val %= val2;
2071 break;
2074 return val;
2077 static int64_t expr_logic(void)
2079 int64_t val, val2;
2080 int op;
2082 val = expr_prod();
2083 for(;;) {
2084 op = *pch;
2085 if (op != '&' && op != '|' && op != '^')
2086 break;
2087 next();
2088 val2 = expr_prod();
2089 switch(op) {
2090 default:
2091 case '&':
2092 val &= val2;
2093 break;
2094 case '|':
2095 val |= val2;
2096 break;
2097 case '^':
2098 val ^= val2;
2099 break;
2102 return val;
2105 static int64_t expr_sum(void)
2107 int64_t val, val2;
2108 int op;
2110 val = expr_logic();
2111 for(;;) {
2112 op = *pch;
2113 if (op != '+' && op != '-')
2114 break;
2115 next();
2116 val2 = expr_logic();
2117 if (op == '+')
2118 val += val2;
2119 else
2120 val -= val2;
2122 return val;
2125 static int get_expr(int64_t *pval, const char **pp)
2127 pch = *pp;
2128 if (setjmp(expr_env)) {
2129 *pp = pch;
2130 return -1;
2132 while (isspace(*pch))
2133 pch++;
2134 *pval = expr_sum();
2135 *pp = pch;
2136 return 0;
2139 static int get_str(char *buf, int buf_size, const char **pp)
2141 const char *p;
2142 char *q;
2143 int c;
2145 q = buf;
2146 p = *pp;
2147 while (isspace(*p))
2148 p++;
2149 if (*p == '\0') {
2150 fail:
2151 *q = '\0';
2152 *pp = p;
2153 return -1;
2155 if (*p == '\"') {
2156 p++;
2157 while (*p != '\0' && *p != '\"') {
2158 if (*p == '\\') {
2159 p++;
2160 c = *p++;
2161 switch(c) {
2162 case 'n':
2163 c = '\n';
2164 break;
2165 case 'r':
2166 c = '\r';
2167 break;
2168 case '\\':
2169 case '\'':
2170 case '\"':
2171 break;
2172 default:
2173 qemu_printf("unsupported escape code: '\\%c'\n", c);
2174 goto fail;
2176 if ((q - buf) < buf_size - 1) {
2177 *q++ = c;
2179 } else {
2180 if ((q - buf) < buf_size - 1) {
2181 *q++ = *p;
2183 p++;
2186 if (*p != '\"') {
2187 qemu_printf("unterminated string\n");
2188 goto fail;
2190 p++;
2191 } else {
2192 while (*p != '\0' && !isspace(*p)) {
2193 if ((q - buf) < buf_size - 1) {
2194 *q++ = *p;
2196 p++;
2199 *q = '\0';
2200 *pp = p;
2201 return 0;
2204 static int default_fmt_format = 'x';
2205 static int default_fmt_size = 4;
2207 #define MAX_ARGS 16
2209 static void monitor_handle_command(const char *cmdline)
2211 const char *p, *pstart, *typestr;
2212 char *q;
2213 int c, nb_args, len, i, has_arg;
2214 term_cmd_t *cmd;
2215 char cmdname[256];
2216 char buf[1024];
2217 void *str_allocated[MAX_ARGS];
2218 void *args[MAX_ARGS];
2220 #ifdef DEBUG
2221 term_printf("command='%s'\n", cmdline);
2222 #endif
2224 /* extract the command name */
2225 p = cmdline;
2226 q = cmdname;
2227 while (isspace(*p))
2228 p++;
2229 if (*p == '\0')
2230 return;
2231 pstart = p;
2232 while (*p != '\0' && *p != '/' && !isspace(*p))
2233 p++;
2234 len = p - pstart;
2235 if (len > sizeof(cmdname) - 1)
2236 len = sizeof(cmdname) - 1;
2237 memcpy(cmdname, pstart, len);
2238 cmdname[len] = '\0';
2240 /* find the command */
2241 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2242 if (compare_cmd(cmdname, cmd->name))
2243 goto found;
2245 term_printf("unknown command: '%s'\n", cmdname);
2246 return;
2247 found:
2249 for(i = 0; i < MAX_ARGS; i++)
2250 str_allocated[i] = NULL;
2252 /* parse the parameters */
2253 typestr = cmd->args_type;
2254 nb_args = 0;
2255 for(;;) {
2256 c = *typestr;
2257 if (c == '\0')
2258 break;
2259 typestr++;
2260 switch(c) {
2261 case 'F':
2262 case 'B':
2263 case 's':
2265 int ret;
2266 char *str;
2268 while (isspace(*p))
2269 p++;
2270 if (*typestr == '?') {
2271 typestr++;
2272 if (*p == '\0') {
2273 /* no optional string: NULL argument */
2274 str = NULL;
2275 goto add_str;
2278 ret = get_str(buf, sizeof(buf), &p);
2279 if (ret < 0) {
2280 switch(c) {
2281 case 'F':
2282 term_printf("%s: filename expected\n", cmdname);
2283 break;
2284 case 'B':
2285 term_printf("%s: block device name expected\n", cmdname);
2286 break;
2287 default:
2288 term_printf("%s: string expected\n", cmdname);
2289 break;
2291 goto fail;
2293 str = qemu_malloc(strlen(buf) + 1);
2294 strcpy(str, buf);
2295 str_allocated[nb_args] = str;
2296 add_str:
2297 if (nb_args >= MAX_ARGS) {
2298 error_args:
2299 term_printf("%s: too many arguments\n", cmdname);
2300 goto fail;
2302 args[nb_args++] = str;
2304 break;
2305 case '/':
2307 int count, format, size;
2309 while (isspace(*p))
2310 p++;
2311 if (*p == '/') {
2312 /* format found */
2313 p++;
2314 count = 1;
2315 if (isdigit(*p)) {
2316 count = 0;
2317 while (isdigit(*p)) {
2318 count = count * 10 + (*p - '0');
2319 p++;
2322 size = -1;
2323 format = -1;
2324 for(;;) {
2325 switch(*p) {
2326 case 'o':
2327 case 'd':
2328 case 'u':
2329 case 'x':
2330 case 'i':
2331 case 'c':
2332 format = *p++;
2333 break;
2334 case 'b':
2335 size = 1;
2336 p++;
2337 break;
2338 case 'h':
2339 size = 2;
2340 p++;
2341 break;
2342 case 'w':
2343 size = 4;
2344 p++;
2345 break;
2346 case 'g':
2347 case 'L':
2348 size = 8;
2349 p++;
2350 break;
2351 default:
2352 goto next;
2355 next:
2356 if (*p != '\0' && !isspace(*p)) {
2357 term_printf("invalid char in format: '%c'\n", *p);
2358 goto fail;
2360 if (format < 0)
2361 format = default_fmt_format;
2362 if (format != 'i') {
2363 /* for 'i', not specifying a size gives -1 as size */
2364 if (size < 0)
2365 size = default_fmt_size;
2367 default_fmt_size = size;
2368 default_fmt_format = format;
2369 } else {
2370 count = 1;
2371 format = default_fmt_format;
2372 if (format != 'i') {
2373 size = default_fmt_size;
2374 } else {
2375 size = -1;
2378 if (nb_args + 3 > MAX_ARGS)
2379 goto error_args;
2380 args[nb_args++] = (void*)(long)count;
2381 args[nb_args++] = (void*)(long)format;
2382 args[nb_args++] = (void*)(long)size;
2384 break;
2385 case 'i':
2386 case 'l':
2388 int64_t val;
2390 while (isspace(*p))
2391 p++;
2392 if (*typestr == '?' || *typestr == '.') {
2393 if (*typestr == '?') {
2394 if (*p == '\0')
2395 has_arg = 0;
2396 else
2397 has_arg = 1;
2398 } else {
2399 if (*p == '.') {
2400 p++;
2401 while (isspace(*p))
2402 p++;
2403 has_arg = 1;
2404 } else {
2405 has_arg = 0;
2408 typestr++;
2409 if (nb_args >= MAX_ARGS)
2410 goto error_args;
2411 args[nb_args++] = (void *)(long)has_arg;
2412 if (!has_arg) {
2413 if (nb_args >= MAX_ARGS)
2414 goto error_args;
2415 val = -1;
2416 goto add_num;
2419 if (get_expr(&val, &p))
2420 goto fail;
2421 add_num:
2422 if (c == 'i') {
2423 if (nb_args >= MAX_ARGS)
2424 goto error_args;
2425 args[nb_args++] = (void *)(long)val;
2426 } else {
2427 if ((nb_args + 1) >= MAX_ARGS)
2428 goto error_args;
2429 #if TARGET_PHYS_ADDR_BITS > 32
2430 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2431 #else
2432 args[nb_args++] = (void *)0;
2433 #endif
2434 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2437 break;
2438 case '-':
2440 int has_option;
2441 /* option */
2443 c = *typestr++;
2444 if (c == '\0')
2445 goto bad_type;
2446 while (isspace(*p))
2447 p++;
2448 has_option = 0;
2449 if (*p == '-') {
2450 p++;
2451 if (*p != c) {
2452 term_printf("%s: unsupported option -%c\n",
2453 cmdname, *p);
2454 goto fail;
2456 p++;
2457 has_option = 1;
2459 if (nb_args >= MAX_ARGS)
2460 goto error_args;
2461 args[nb_args++] = (void *)(long)has_option;
2463 break;
2464 default:
2465 bad_type:
2466 term_printf("%s: unknown type '%c'\n", cmdname, c);
2467 goto fail;
2470 /* check that all arguments were parsed */
2471 while (isspace(*p))
2472 p++;
2473 if (*p != '\0') {
2474 term_printf("%s: extraneous characters at the end of line\n",
2475 cmdname);
2476 goto fail;
2479 switch(nb_args) {
2480 case 0:
2481 cmd->handler();
2482 break;
2483 case 1:
2484 cmd->handler(args[0]);
2485 break;
2486 case 2:
2487 cmd->handler(args[0], args[1]);
2488 break;
2489 case 3:
2490 cmd->handler(args[0], args[1], args[2]);
2491 break;
2492 case 4:
2493 cmd->handler(args[0], args[1], args[2], args[3]);
2494 break;
2495 case 5:
2496 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2497 break;
2498 case 6:
2499 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2500 break;
2501 case 7:
2502 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2503 break;
2504 default:
2505 term_printf("unsupported number of arguments: %d\n", nb_args);
2506 goto fail;
2508 fail:
2509 for(i = 0; i < MAX_ARGS; i++)
2510 qemu_free(str_allocated[i]);
2511 return;
2514 static void cmd_completion(const char *name, const char *list)
2516 const char *p, *pstart;
2517 char cmd[128];
2518 int len;
2520 p = list;
2521 for(;;) {
2522 pstart = p;
2523 p = strchr(p, '|');
2524 if (!p)
2525 p = pstart + strlen(pstart);
2526 len = p - pstart;
2527 if (len > sizeof(cmd) - 2)
2528 len = sizeof(cmd) - 2;
2529 memcpy(cmd, pstart, len);
2530 cmd[len] = '\0';
2531 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2532 add_completion(cmd);
2534 if (*p == '\0')
2535 break;
2536 p++;
2540 static void file_completion(const char *input)
2542 DIR *ffs;
2543 struct dirent *d;
2544 char path[1024];
2545 char file[1024], file_prefix[1024];
2546 int input_path_len;
2547 const char *p;
2549 p = strrchr(input, '/');
2550 if (!p) {
2551 input_path_len = 0;
2552 pstrcpy(file_prefix, sizeof(file_prefix), input);
2553 strcpy(path, ".");
2554 } else {
2555 input_path_len = p - input + 1;
2556 memcpy(path, input, input_path_len);
2557 if (input_path_len > sizeof(path) - 1)
2558 input_path_len = sizeof(path) - 1;
2559 path[input_path_len] = '\0';
2560 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2562 #ifdef DEBUG_COMPLETION
2563 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2564 #endif
2565 ffs = opendir(path);
2566 if (!ffs)
2567 return;
2568 for(;;) {
2569 struct stat sb;
2570 d = readdir(ffs);
2571 if (!d)
2572 break;
2573 if (strstart(d->d_name, file_prefix, NULL)) {
2574 memcpy(file, input, input_path_len);
2575 strcpy(file + input_path_len, d->d_name);
2576 /* stat the file to find out if it's a directory.
2577 * In that case add a slash to speed up typing long paths
2579 stat(file, &sb);
2580 if(S_ISDIR(sb.st_mode))
2581 strcat(file, "/");
2582 add_completion(file);
2585 closedir(ffs);
2588 static void block_completion_it(void *opaque, const char *name)
2590 const char *input = opaque;
2592 if (input[0] == '\0' ||
2593 !strncmp(name, (char *)input, strlen(input))) {
2594 add_completion(name);
2598 /* NOTE: this parser is an approximate form of the real command parser */
2599 static void parse_cmdline(const char *cmdline,
2600 int *pnb_args, char **args)
2602 const char *p;
2603 int nb_args, ret;
2604 char buf[1024];
2606 p = cmdline;
2607 nb_args = 0;
2608 for(;;) {
2609 while (isspace(*p))
2610 p++;
2611 if (*p == '\0')
2612 break;
2613 if (nb_args >= MAX_ARGS)
2614 break;
2615 ret = get_str(buf, sizeof(buf), &p);
2616 args[nb_args] = qemu_strdup(buf);
2617 nb_args++;
2618 if (ret < 0)
2619 break;
2621 *pnb_args = nb_args;
2624 void readline_find_completion(const char *cmdline)
2626 const char *cmdname;
2627 char *args[MAX_ARGS];
2628 int nb_args, i, len;
2629 const char *ptype, *str;
2630 term_cmd_t *cmd;
2631 const KeyDef *key;
2633 parse_cmdline(cmdline, &nb_args, args);
2634 #ifdef DEBUG_COMPLETION
2635 for(i = 0; i < nb_args; i++) {
2636 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2638 #endif
2640 /* if the line ends with a space, it means we want to complete the
2641 next arg */
2642 len = strlen(cmdline);
2643 if (len > 0 && isspace(cmdline[len - 1])) {
2644 if (nb_args >= MAX_ARGS)
2645 return;
2646 args[nb_args++] = qemu_strdup("");
2648 if (nb_args <= 1) {
2649 /* command completion */
2650 if (nb_args == 0)
2651 cmdname = "";
2652 else
2653 cmdname = args[0];
2654 completion_index = strlen(cmdname);
2655 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2656 cmd_completion(cmdname, cmd->name);
2658 } else {
2659 /* find the command */
2660 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2661 if (compare_cmd(args[0], cmd->name))
2662 goto found;
2664 return;
2665 found:
2666 ptype = cmd->args_type;
2667 for(i = 0; i < nb_args - 2; i++) {
2668 if (*ptype != '\0') {
2669 ptype++;
2670 while (*ptype == '?')
2671 ptype++;
2674 str = args[nb_args - 1];
2675 switch(*ptype) {
2676 case 'F':
2677 /* file completion */
2678 completion_index = strlen(str);
2679 file_completion(str);
2680 break;
2681 case 'B':
2682 /* block device name completion */
2683 completion_index = strlen(str);
2684 bdrv_iterate(block_completion_it, (void *)str);
2685 break;
2686 case 's':
2687 /* XXX: more generic ? */
2688 if (!strcmp(cmd->name, "info")) {
2689 completion_index = strlen(str);
2690 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2691 cmd_completion(str, cmd->name);
2693 } else if (!strcmp(cmd->name, "sendkey")) {
2694 completion_index = strlen(str);
2695 for(key = key_defs; key->name != NULL; key++) {
2696 cmd_completion(str, key->name);
2699 break;
2700 default:
2701 break;
2704 for(i = 0; i < nb_args; i++)
2705 qemu_free(args[i]);
2708 static int term_can_read(void *opaque)
2710 return 128;
2713 static void term_read(void *opaque, const uint8_t *buf, int size)
2715 int i;
2716 for(i = 0; i < size; i++)
2717 readline_handle_byte(buf[i]);
2720 static int monitor_suspended;
2722 void monitor_suspend(void)
2724 monitor_suspended = 1;
2727 void monitor_resume(void)
2729 monitor_suspended = 0;
2730 monitor_start_input();
2733 static void monitor_handle_command1(void *opaque, const char *cmdline)
2735 monitor_handle_command(cmdline);
2736 if (!monitor_suspended)
2737 monitor_start_input();
2740 void monitor_start_input(void)
2742 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2745 static void term_event(void *opaque, int event)
2747 if (event != CHR_EVENT_RESET)
2748 return;
2750 if (!hide_banner)
2751 term_printf("QEMU %s monitor - type 'help' for more information\n",
2752 QEMU_VERSION);
2753 monitor_start_input();
2756 static int is_first_init = 1;
2758 void monitor_init(CharDriverState *hd, int show_banner)
2760 int i;
2762 if (is_first_init) {
2763 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2764 if (!key_timer)
2765 return;
2766 for (i = 0; i < MAX_MON; i++) {
2767 monitor_hd[i] = NULL;
2769 is_first_init = 0;
2771 for (i = 0; i < MAX_MON; i++) {
2772 if (monitor_hd[i] == NULL) {
2773 monitor_hd[i] = hd;
2774 break;
2778 hide_banner = !show_banner;
2780 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2783 /* XXX: use threads ? */
2784 /* modal monitor readline */
2785 static int monitor_readline_started;
2786 static char *monitor_readline_buf;
2787 static int monitor_readline_buf_size;
2789 static void monitor_readline_cb(void *opaque, const char *input)
2791 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2792 monitor_readline_started = 0;
2795 void monitor_readline(const char *prompt, int is_password,
2796 char *buf, int buf_size)
2798 int i;
2799 int old_focus[MAX_MON];
2801 if (is_password) {
2802 for (i = 0; i < MAX_MON; i++) {
2803 old_focus[i] = 0;
2804 if (monitor_hd[i]) {
2805 old_focus[i] = monitor_hd[i]->focus;
2806 monitor_hd[i]->focus = 0;
2807 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2812 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2813 monitor_readline_buf = buf;
2814 monitor_readline_buf_size = buf_size;
2815 monitor_readline_started = 1;
2816 while (monitor_readline_started) {
2817 main_loop_wait(10);
2819 /* restore original focus */
2820 if (is_password) {
2821 for (i = 0; i < MAX_MON; i++)
2822 if (old_focus[i])
2823 monitor_hd[i]->focus = old_focus[i];