Add safety net against potential infinite loop
[qemu/mini2440.git] / monitor.c
blob61bd33543ac5b499b158dfc7a47454bfb1528bcf
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 <dirent.h>
38 #include "qemu-timer.h"
39 #include "migration.h"
41 //#define DEBUG
42 //#define DEBUG_COMPLETION
45 * Supported types:
47 * 'F' filename
48 * 'B' block device name
49 * 's' string (accept optional quote)
50 * 'i' 32 bit integer
51 * 'l' target long (32 or 64 bit)
52 * '/' optional gdb-like print format (like "/10x")
54 * '?' optional type (for 'F', 's' and 'i')
58 typedef struct term_cmd_t {
59 const char *name;
60 const char *args_type;
61 void *handler;
62 const char *params;
63 const char *help;
64 } term_cmd_t;
66 #define MAX_MON 4
67 static CharDriverState *monitor_hd[MAX_MON];
68 static int hide_banner;
70 static const term_cmd_t term_cmds[];
71 static const term_cmd_t info_cmds[];
73 static uint8_t term_outbuf[1024];
74 static int term_outbuf_index;
76 static void monitor_start_input(void);
78 static CPUState *mon_cpu = NULL;
80 void term_flush(void)
82 int i;
83 if (term_outbuf_index > 0) {
84 for (i = 0; i < MAX_MON; i++)
85 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
86 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
87 term_outbuf_index = 0;
91 /* flush at every end of line or if the buffer is full */
92 void term_puts(const char *str)
94 char c;
95 for(;;) {
96 c = *str++;
97 if (c == '\0')
98 break;
99 if (c == '\n')
100 term_outbuf[term_outbuf_index++] = '\r';
101 term_outbuf[term_outbuf_index++] = c;
102 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
103 c == '\n')
104 term_flush();
108 void term_vprintf(const char *fmt, va_list ap)
110 char buf[4096];
111 vsnprintf(buf, sizeof(buf), fmt, ap);
112 term_puts(buf);
115 void term_printf(const char *fmt, ...)
117 va_list ap;
118 va_start(ap, fmt);
119 term_vprintf(fmt, ap);
120 va_end(ap);
123 void term_print_filename(const char *filename)
125 int i;
127 for (i = 0; filename[i]; i++) {
128 switch (filename[i]) {
129 case ' ':
130 case '"':
131 case '\\':
132 term_printf("\\%c", filename[i]);
133 break;
134 case '\t':
135 term_printf("\\t");
136 break;
137 case '\r':
138 term_printf("\\r");
139 break;
140 case '\n':
141 term_printf("\\n");
142 break;
143 default:
144 term_printf("%c", filename[i]);
145 break;
150 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
152 va_list ap;
153 va_start(ap, fmt);
154 term_vprintf(fmt, ap);
155 va_end(ap);
156 return 0;
159 static int compare_cmd(const char *name, const char *list)
161 const char *p, *pstart;
162 int len;
163 len = strlen(name);
164 p = list;
165 for(;;) {
166 pstart = p;
167 p = strchr(p, '|');
168 if (!p)
169 p = pstart + strlen(pstart);
170 if ((p - pstart) == len && !memcmp(pstart, name, len))
171 return 1;
172 if (*p == '\0')
173 break;
174 p++;
176 return 0;
179 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
181 const term_cmd_t *cmd;
183 for(cmd = cmds; cmd->name != NULL; cmd++) {
184 if (!name || !strcmp(name, cmd->name))
185 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
189 static void help_cmd(const char *name)
191 if (name && !strcmp(name, "info")) {
192 help_cmd1(info_cmds, "info ", NULL);
193 } else {
194 help_cmd1(term_cmds, "", name);
195 if (name && !strcmp(name, "log")) {
196 const CPULogItem *item;
197 term_printf("Log items (comma separated):\n");
198 term_printf("%-10s %s\n", "none", "remove all logs");
199 for(item = cpu_log_items; item->mask != 0; item++) {
200 term_printf("%-10s %s\n", item->name, item->help);
206 static void do_help(const char *name)
208 help_cmd(name);
211 static void do_commit(const char *device)
213 int i, all_devices;
215 all_devices = !strcmp(device, "all");
216 for (i = 0; i < nb_drives; i++) {
217 if (all_devices ||
218 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
219 bdrv_commit(drives_table[i].bdrv);
223 static void do_info(const char *item)
225 const term_cmd_t *cmd;
226 void (*handler)(void);
228 if (!item)
229 goto help;
230 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
231 if (compare_cmd(item, cmd->name))
232 goto found;
234 help:
235 help_cmd("info");
236 return;
237 found:
238 handler = cmd->handler;
239 handler();
242 static void do_info_version(void)
244 term_printf("%s\n", QEMU_VERSION);
247 static void do_info_name(void)
249 if (qemu_name)
250 term_printf("%s\n", qemu_name);
253 static void do_info_uuid(void)
255 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
256 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
257 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
258 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
259 qemu_uuid[15]);
262 static void do_info_block(void)
264 bdrv_info();
267 static void do_info_blockstats(void)
269 bdrv_info_stats();
272 /* get the current CPU defined by the user */
273 static int mon_set_cpu(int cpu_index)
275 CPUState *env;
277 for(env = first_cpu; env != NULL; env = env->next_cpu) {
278 if (env->cpu_index == cpu_index) {
279 mon_cpu = env;
280 return 0;
283 return -1;
286 static CPUState *mon_get_cpu(void)
288 if (!mon_cpu) {
289 mon_set_cpu(0);
291 return mon_cpu;
294 static void do_info_registers(void)
296 CPUState *env;
297 env = mon_get_cpu();
298 if (!env)
299 return;
300 #ifdef TARGET_I386
301 cpu_dump_state(env, NULL, monitor_fprintf,
302 X86_DUMP_FPU);
303 #else
304 cpu_dump_state(env, NULL, monitor_fprintf,
306 #endif
309 static void do_info_cpus(void)
311 CPUState *env;
313 /* just to set the default cpu if not already done */
314 mon_get_cpu();
316 for(env = first_cpu; env != NULL; env = env->next_cpu) {
317 term_printf("%c CPU #%d:",
318 (env == mon_cpu) ? '*' : ' ',
319 env->cpu_index);
320 #if defined(TARGET_I386)
321 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
322 #elif defined(TARGET_PPC)
323 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
324 #elif defined(TARGET_SPARC)
325 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
326 #elif defined(TARGET_MIPS)
327 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
328 #endif
329 if (env->halted)
330 term_printf(" (halted)");
331 term_printf("\n");
335 static void do_cpu_set(int index)
337 if (mon_set_cpu(index) < 0)
338 term_printf("Invalid CPU index\n");
341 static void do_info_jit(void)
343 dump_exec_info(NULL, monitor_fprintf);
346 static void do_info_history (void)
348 int i;
349 const char *str;
351 i = 0;
352 for(;;) {
353 str = readline_get_history(i);
354 if (!str)
355 break;
356 term_printf("%d: '%s'\n", i, str);
357 i++;
361 #if defined(TARGET_PPC)
362 /* XXX: not implemented in other targets */
363 static void do_info_cpu_stats (void)
365 CPUState *env;
367 env = mon_get_cpu();
368 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
370 #endif
372 static void do_quit(void)
374 exit(0);
377 static int eject_device(BlockDriverState *bs, int force)
379 if (bdrv_is_inserted(bs)) {
380 if (!force) {
381 if (!bdrv_is_removable(bs)) {
382 term_printf("device is not removable\n");
383 return -1;
385 if (bdrv_is_locked(bs)) {
386 term_printf("device is locked\n");
387 return -1;
390 bdrv_close(bs);
392 return 0;
395 static void do_eject(int force, const char *filename)
397 BlockDriverState *bs;
399 bs = bdrv_find(filename);
400 if (!bs) {
401 term_printf("device not found\n");
402 return;
404 eject_device(bs, force);
407 static void do_change_block(const char *device, const char *filename, const char *fmt)
409 BlockDriverState *bs;
410 BlockDriver *drv = NULL;
412 bs = bdrv_find(device);
413 if (!bs) {
414 term_printf("device not found\n");
415 return;
417 if (fmt) {
418 drv = bdrv_find_format(fmt);
419 if (!drv) {
420 term_printf("invalid format %s\n", fmt);
421 return;
424 if (eject_device(bs, 0) < 0)
425 return;
426 bdrv_open2(bs, filename, 0, drv);
427 qemu_key_check(bs, filename);
430 static void do_change_vnc(const char *target)
432 if (strcmp(target, "passwd") == 0 ||
433 strcmp(target, "password") == 0) {
434 char password[9];
435 monitor_readline("Password: ", 1, password, sizeof(password)-1);
436 password[sizeof(password)-1] = '\0';
437 if (vnc_display_password(NULL, password) < 0)
438 term_printf("could not set VNC server password\n");
439 } else {
440 if (vnc_display_open(NULL, target) < 0)
441 term_printf("could not start VNC server on %s\n", target);
445 static void do_change(const char *device, const char *target, const char *fmt)
447 if (strcmp(device, "vnc") == 0) {
448 do_change_vnc(target);
449 } else {
450 do_change_block(device, target, fmt);
454 static void do_screen_dump(const char *filename)
456 vga_hw_screen_dump(filename);
459 static void do_logfile(const char *filename)
461 cpu_set_log_filename(filename);
464 static void do_log(const char *items)
466 int mask;
468 if (!strcmp(items, "none")) {
469 mask = 0;
470 } else {
471 mask = cpu_str_to_log_mask(items);
472 if (!mask) {
473 help_cmd("log");
474 return;
477 cpu_set_log(mask);
480 static void do_stop(void)
482 vm_stop(EXCP_INTERRUPT);
485 static void do_cont(void)
487 vm_start();
490 #ifdef CONFIG_GDBSTUB
491 static void do_gdbserver(const char *port)
493 if (!port)
494 port = DEFAULT_GDBSTUB_PORT;
495 if (gdbserver_start(port) < 0) {
496 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
497 } else {
498 qemu_printf("Waiting gdb connection on port '%s'\n", port);
501 #endif
503 static void term_printc(int c)
505 term_printf("'");
506 switch(c) {
507 case '\'':
508 term_printf("\\'");
509 break;
510 case '\\':
511 term_printf("\\\\");
512 break;
513 case '\n':
514 term_printf("\\n");
515 break;
516 case '\r':
517 term_printf("\\r");
518 break;
519 default:
520 if (c >= 32 && c <= 126) {
521 term_printf("%c", c);
522 } else {
523 term_printf("\\x%02x", c);
525 break;
527 term_printf("'");
530 static void memory_dump(int count, int format, int wsize,
531 target_phys_addr_t addr, int is_physical)
533 CPUState *env;
534 int nb_per_line, l, line_size, i, max_digits, len;
535 uint8_t buf[16];
536 uint64_t v;
538 if (format == 'i') {
539 int flags;
540 flags = 0;
541 env = mon_get_cpu();
542 if (!env && !is_physical)
543 return;
544 #ifdef TARGET_I386
545 if (wsize == 2) {
546 flags = 1;
547 } else if (wsize == 4) {
548 flags = 0;
549 } else {
550 /* as default we use the current CS size */
551 flags = 0;
552 if (env) {
553 #ifdef TARGET_X86_64
554 if ((env->efer & MSR_EFER_LMA) &&
555 (env->segs[R_CS].flags & DESC_L_MASK))
556 flags = 2;
557 else
558 #endif
559 if (!(env->segs[R_CS].flags & DESC_B_MASK))
560 flags = 1;
563 #endif
564 monitor_disas(env, addr, count, is_physical, flags);
565 return;
568 len = wsize * count;
569 if (wsize == 1)
570 line_size = 8;
571 else
572 line_size = 16;
573 nb_per_line = line_size / wsize;
574 max_digits = 0;
576 switch(format) {
577 case 'o':
578 max_digits = (wsize * 8 + 2) / 3;
579 break;
580 default:
581 case 'x':
582 max_digits = (wsize * 8) / 4;
583 break;
584 case 'u':
585 case 'd':
586 max_digits = (wsize * 8 * 10 + 32) / 33;
587 break;
588 case 'c':
589 wsize = 1;
590 break;
593 while (len > 0) {
594 if (is_physical)
595 term_printf(TARGET_FMT_plx ":", addr);
596 else
597 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
598 l = len;
599 if (l > line_size)
600 l = line_size;
601 if (is_physical) {
602 cpu_physical_memory_rw(addr, buf, l, 0);
603 } else {
604 env = mon_get_cpu();
605 if (!env)
606 break;
607 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
608 term_printf(" Cannot access memory\n");
609 break;
612 i = 0;
613 while (i < l) {
614 switch(wsize) {
615 default:
616 case 1:
617 v = ldub_raw(buf + i);
618 break;
619 case 2:
620 v = lduw_raw(buf + i);
621 break;
622 case 4:
623 v = (uint32_t)ldl_raw(buf + i);
624 break;
625 case 8:
626 v = ldq_raw(buf + i);
627 break;
629 term_printf(" ");
630 switch(format) {
631 case 'o':
632 term_printf("%#*" PRIo64, max_digits, v);
633 break;
634 case 'x':
635 term_printf("0x%0*" PRIx64, max_digits, v);
636 break;
637 case 'u':
638 term_printf("%*" PRIu64, max_digits, v);
639 break;
640 case 'd':
641 term_printf("%*" PRId64, max_digits, v);
642 break;
643 case 'c':
644 term_printc(v);
645 break;
647 i += wsize;
649 term_printf("\n");
650 addr += l;
651 len -= l;
655 #if TARGET_LONG_BITS == 64
656 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
657 #else
658 #define GET_TLONG(h, l) (l)
659 #endif
661 static void do_memory_dump(int count, int format, int size,
662 uint32_t addrh, uint32_t addrl)
664 target_long addr = GET_TLONG(addrh, addrl);
665 memory_dump(count, format, size, addr, 0);
668 #if TARGET_PHYS_ADDR_BITS > 32
669 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
670 #else
671 #define GET_TPHYSADDR(h, l) (l)
672 #endif
674 static void do_physical_memory_dump(int count, int format, int size,
675 uint32_t addrh, uint32_t addrl)
678 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
679 memory_dump(count, format, size, addr, 1);
682 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
684 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
685 #if TARGET_PHYS_ADDR_BITS == 32
686 switch(format) {
687 case 'o':
688 term_printf("%#o", val);
689 break;
690 case 'x':
691 term_printf("%#x", val);
692 break;
693 case 'u':
694 term_printf("%u", val);
695 break;
696 default:
697 case 'd':
698 term_printf("%d", val);
699 break;
700 case 'c':
701 term_printc(val);
702 break;
704 #else
705 switch(format) {
706 case 'o':
707 term_printf("%#" PRIo64, val);
708 break;
709 case 'x':
710 term_printf("%#" PRIx64, val);
711 break;
712 case 'u':
713 term_printf("%" PRIu64, val);
714 break;
715 default:
716 case 'd':
717 term_printf("%" PRId64, val);
718 break;
719 case 'c':
720 term_printc(val);
721 break;
723 #endif
724 term_printf("\n");
727 static void do_memory_save(unsigned int valh, unsigned int vall,
728 uint32_t size, const char *filename)
730 FILE *f;
731 target_long addr = GET_TLONG(valh, vall);
732 uint32_t l;
733 CPUState *env;
734 uint8_t buf[1024];
736 env = mon_get_cpu();
737 if (!env)
738 return;
740 f = fopen(filename, "wb");
741 if (!f) {
742 term_printf("could not open '%s'\n", filename);
743 return;
745 while (size != 0) {
746 l = sizeof(buf);
747 if (l > size)
748 l = size;
749 cpu_memory_rw_debug(env, addr, buf, l, 0);
750 fwrite(buf, 1, l, f);
751 addr += l;
752 size -= l;
754 fclose(f);
757 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
758 uint32_t size, const char *filename)
760 FILE *f;
761 uint32_t l;
762 uint8_t buf[1024];
763 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
765 f = fopen(filename, "wb");
766 if (!f) {
767 term_printf("could not open '%s'\n", filename);
768 return;
770 while (size != 0) {
771 l = sizeof(buf);
772 if (l > size)
773 l = size;
774 cpu_physical_memory_rw(addr, buf, l, 0);
775 fwrite(buf, 1, l, f);
776 fflush(f);
777 addr += l;
778 size -= l;
780 fclose(f);
783 static void do_sum(uint32_t start, uint32_t size)
785 uint32_t addr;
786 uint8_t buf[1];
787 uint16_t sum;
789 sum = 0;
790 for(addr = start; addr < (start + size); addr++) {
791 cpu_physical_memory_rw(addr, buf, 1, 0);
792 /* BSD sum algorithm ('sum' Unix command) */
793 sum = (sum >> 1) | (sum << 15);
794 sum += buf[0];
796 term_printf("%05d\n", sum);
799 typedef struct {
800 int keycode;
801 const char *name;
802 } KeyDef;
804 static const KeyDef key_defs[] = {
805 { 0x2a, "shift" },
806 { 0x36, "shift_r" },
808 { 0x38, "alt" },
809 { 0xb8, "alt_r" },
810 { 0x64, "altgr" },
811 { 0xe4, "altgr_r" },
812 { 0x1d, "ctrl" },
813 { 0x9d, "ctrl_r" },
815 { 0xdd, "menu" },
817 { 0x01, "esc" },
819 { 0x02, "1" },
820 { 0x03, "2" },
821 { 0x04, "3" },
822 { 0x05, "4" },
823 { 0x06, "5" },
824 { 0x07, "6" },
825 { 0x08, "7" },
826 { 0x09, "8" },
827 { 0x0a, "9" },
828 { 0x0b, "0" },
829 { 0x0c, "minus" },
830 { 0x0d, "equal" },
831 { 0x0e, "backspace" },
833 { 0x0f, "tab" },
834 { 0x10, "q" },
835 { 0x11, "w" },
836 { 0x12, "e" },
837 { 0x13, "r" },
838 { 0x14, "t" },
839 { 0x15, "y" },
840 { 0x16, "u" },
841 { 0x17, "i" },
842 { 0x18, "o" },
843 { 0x19, "p" },
845 { 0x1c, "ret" },
847 { 0x1e, "a" },
848 { 0x1f, "s" },
849 { 0x20, "d" },
850 { 0x21, "f" },
851 { 0x22, "g" },
852 { 0x23, "h" },
853 { 0x24, "j" },
854 { 0x25, "k" },
855 { 0x26, "l" },
857 { 0x2c, "z" },
858 { 0x2d, "x" },
859 { 0x2e, "c" },
860 { 0x2f, "v" },
861 { 0x30, "b" },
862 { 0x31, "n" },
863 { 0x32, "m" },
864 { 0x33, "comma" },
865 { 0x34, "dot" },
866 { 0x35, "slash" },
868 { 0x37, "asterisk" },
870 { 0x39, "spc" },
871 { 0x3a, "caps_lock" },
872 { 0x3b, "f1" },
873 { 0x3c, "f2" },
874 { 0x3d, "f3" },
875 { 0x3e, "f4" },
876 { 0x3f, "f5" },
877 { 0x40, "f6" },
878 { 0x41, "f7" },
879 { 0x42, "f8" },
880 { 0x43, "f9" },
881 { 0x44, "f10" },
882 { 0x45, "num_lock" },
883 { 0x46, "scroll_lock" },
885 { 0xb5, "kp_divide" },
886 { 0x37, "kp_multiply" },
887 { 0x4a, "kp_subtract" },
888 { 0x4e, "kp_add" },
889 { 0x9c, "kp_enter" },
890 { 0x53, "kp_decimal" },
891 { 0x54, "sysrq" },
893 { 0x52, "kp_0" },
894 { 0x4f, "kp_1" },
895 { 0x50, "kp_2" },
896 { 0x51, "kp_3" },
897 { 0x4b, "kp_4" },
898 { 0x4c, "kp_5" },
899 { 0x4d, "kp_6" },
900 { 0x47, "kp_7" },
901 { 0x48, "kp_8" },
902 { 0x49, "kp_9" },
904 { 0x56, "<" },
906 { 0x57, "f11" },
907 { 0x58, "f12" },
909 { 0xb7, "print" },
911 { 0xc7, "home" },
912 { 0xc9, "pgup" },
913 { 0xd1, "pgdn" },
914 { 0xcf, "end" },
916 { 0xcb, "left" },
917 { 0xc8, "up" },
918 { 0xd0, "down" },
919 { 0xcd, "right" },
921 { 0xd2, "insert" },
922 { 0xd3, "delete" },
923 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
924 { 0xf0, "stop" },
925 { 0xf1, "again" },
926 { 0xf2, "props" },
927 { 0xf3, "undo" },
928 { 0xf4, "front" },
929 { 0xf5, "copy" },
930 { 0xf6, "open" },
931 { 0xf7, "paste" },
932 { 0xf8, "find" },
933 { 0xf9, "cut" },
934 { 0xfa, "lf" },
935 { 0xfb, "help" },
936 { 0xfc, "meta_l" },
937 { 0xfd, "meta_r" },
938 { 0xfe, "compose" },
939 #endif
940 { 0, NULL },
943 static int get_keycode(const char *key)
945 const KeyDef *p;
946 char *endp;
947 int ret;
949 for(p = key_defs; p->name != NULL; p++) {
950 if (!strcmp(key, p->name))
951 return p->keycode;
953 if (strstart(key, "0x", NULL)) {
954 ret = strtoul(key, &endp, 0);
955 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
956 return ret;
958 return -1;
961 #define MAX_KEYCODES 16
962 static uint8_t keycodes[MAX_KEYCODES];
963 static int nb_pending_keycodes;
964 static QEMUTimer *key_timer;
966 static void release_keys(void *opaque)
968 int keycode;
970 while (nb_pending_keycodes > 0) {
971 nb_pending_keycodes--;
972 keycode = keycodes[nb_pending_keycodes];
973 if (keycode & 0x80)
974 kbd_put_keycode(0xe0);
975 kbd_put_keycode(keycode | 0x80);
979 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
981 char keyname_buf[16];
982 char *separator;
983 int keyname_len, keycode, i;
985 if (nb_pending_keycodes > 0) {
986 qemu_del_timer(key_timer);
987 release_keys(NULL);
989 if (!has_hold_time)
990 hold_time = 100;
991 i = 0;
992 while (1) {
993 separator = strchr(string, '-');
994 keyname_len = separator ? separator - string : strlen(string);
995 if (keyname_len > 0) {
996 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
997 if (keyname_len > sizeof(keyname_buf) - 1) {
998 term_printf("invalid key: '%s...'\n", keyname_buf);
999 return;
1001 if (i == MAX_KEYCODES) {
1002 term_printf("too many keys\n");
1003 return;
1005 keyname_buf[keyname_len] = 0;
1006 keycode = get_keycode(keyname_buf);
1007 if (keycode < 0) {
1008 term_printf("unknown key: '%s'\n", keyname_buf);
1009 return;
1011 keycodes[i++] = keycode;
1013 if (!separator)
1014 break;
1015 string = separator + 1;
1017 nb_pending_keycodes = i;
1018 /* key down events */
1019 for (i = 0; i < nb_pending_keycodes; i++) {
1020 keycode = keycodes[i];
1021 if (keycode & 0x80)
1022 kbd_put_keycode(0xe0);
1023 kbd_put_keycode(keycode & 0x7f);
1025 /* delayed key up events */
1026 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1027 muldiv64(ticks_per_sec, hold_time, 1000));
1030 static int mouse_button_state;
1032 static void do_mouse_move(const char *dx_str, const char *dy_str,
1033 const char *dz_str)
1035 int dx, dy, dz;
1036 dx = strtol(dx_str, NULL, 0);
1037 dy = strtol(dy_str, NULL, 0);
1038 dz = 0;
1039 if (dz_str)
1040 dz = strtol(dz_str, NULL, 0);
1041 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1044 static void do_mouse_button(int button_state)
1046 mouse_button_state = button_state;
1047 kbd_mouse_event(0, 0, 0, mouse_button_state);
1050 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1052 uint32_t val;
1053 int suffix;
1055 if (has_index) {
1056 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1057 addr++;
1059 addr &= 0xffff;
1061 switch(size) {
1062 default:
1063 case 1:
1064 val = cpu_inb(NULL, addr);
1065 suffix = 'b';
1066 break;
1067 case 2:
1068 val = cpu_inw(NULL, addr);
1069 suffix = 'w';
1070 break;
1071 case 4:
1072 val = cpu_inl(NULL, addr);
1073 suffix = 'l';
1074 break;
1076 term_printf("port%c[0x%04x] = %#0*x\n",
1077 suffix, addr, size * 2, val);
1080 /* boot_set handler */
1081 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1082 static void *boot_opaque;
1084 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1086 qemu_boot_set_handler = func;
1087 boot_opaque = opaque;
1090 static void do_boot_set(const char *bootdevice)
1092 int res;
1094 if (qemu_boot_set_handler) {
1095 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1096 if (res == 0)
1097 term_printf("boot device list now set to %s\n", bootdevice);
1098 else
1099 term_printf("setting boot device list failed with error %i\n", res);
1100 } else {
1101 term_printf("no function defined to set boot device list for this architecture\n");
1105 static void do_system_reset(void)
1107 qemu_system_reset_request();
1110 static void do_system_powerdown(void)
1112 qemu_system_powerdown_request();
1115 #if defined(TARGET_I386)
1116 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1118 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1119 addr,
1120 pte & mask,
1121 pte & PG_GLOBAL_MASK ? 'G' : '-',
1122 pte & PG_PSE_MASK ? 'P' : '-',
1123 pte & PG_DIRTY_MASK ? 'D' : '-',
1124 pte & PG_ACCESSED_MASK ? 'A' : '-',
1125 pte & PG_PCD_MASK ? 'C' : '-',
1126 pte & PG_PWT_MASK ? 'T' : '-',
1127 pte & PG_USER_MASK ? 'U' : '-',
1128 pte & PG_RW_MASK ? 'W' : '-');
1131 static void tlb_info(void)
1133 CPUState *env;
1134 int l1, l2;
1135 uint32_t pgd, pde, pte;
1137 env = mon_get_cpu();
1138 if (!env)
1139 return;
1141 if (!(env->cr[0] & CR0_PG_MASK)) {
1142 term_printf("PG disabled\n");
1143 return;
1145 pgd = env->cr[3] & ~0xfff;
1146 for(l1 = 0; l1 < 1024; l1++) {
1147 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1148 pde = le32_to_cpu(pde);
1149 if (pde & PG_PRESENT_MASK) {
1150 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1151 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1152 } else {
1153 for(l2 = 0; l2 < 1024; l2++) {
1154 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1155 (uint8_t *)&pte, 4);
1156 pte = le32_to_cpu(pte);
1157 if (pte & PG_PRESENT_MASK) {
1158 print_pte((l1 << 22) + (l2 << 12),
1159 pte & ~PG_PSE_MASK,
1160 ~0xfff);
1168 static void mem_print(uint32_t *pstart, int *plast_prot,
1169 uint32_t end, int prot)
1171 int prot1;
1172 prot1 = *plast_prot;
1173 if (prot != prot1) {
1174 if (*pstart != -1) {
1175 term_printf("%08x-%08x %08x %c%c%c\n",
1176 *pstart, end, end - *pstart,
1177 prot1 & PG_USER_MASK ? 'u' : '-',
1178 'r',
1179 prot1 & PG_RW_MASK ? 'w' : '-');
1181 if (prot != 0)
1182 *pstart = end;
1183 else
1184 *pstart = -1;
1185 *plast_prot = prot;
1189 static void mem_info(void)
1191 CPUState *env;
1192 int l1, l2, prot, last_prot;
1193 uint32_t pgd, pde, pte, start, end;
1195 env = mon_get_cpu();
1196 if (!env)
1197 return;
1199 if (!(env->cr[0] & CR0_PG_MASK)) {
1200 term_printf("PG disabled\n");
1201 return;
1203 pgd = env->cr[3] & ~0xfff;
1204 last_prot = 0;
1205 start = -1;
1206 for(l1 = 0; l1 < 1024; l1++) {
1207 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1208 pde = le32_to_cpu(pde);
1209 end = l1 << 22;
1210 if (pde & PG_PRESENT_MASK) {
1211 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1212 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1213 mem_print(&start, &last_prot, end, prot);
1214 } else {
1215 for(l2 = 0; l2 < 1024; l2++) {
1216 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1217 (uint8_t *)&pte, 4);
1218 pte = le32_to_cpu(pte);
1219 end = (l1 << 22) + (l2 << 12);
1220 if (pte & PG_PRESENT_MASK) {
1221 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1222 } else {
1223 prot = 0;
1225 mem_print(&start, &last_prot, end, prot);
1228 } else {
1229 prot = 0;
1230 mem_print(&start, &last_prot, end, prot);
1234 #endif
1236 static void do_info_kqemu(void)
1238 #ifdef USE_KQEMU
1239 CPUState *env;
1240 int val;
1241 val = 0;
1242 env = mon_get_cpu();
1243 if (!env) {
1244 term_printf("No cpu initialized yet");
1245 return;
1247 val = env->kqemu_enabled;
1248 term_printf("kqemu support: ");
1249 switch(val) {
1250 default:
1251 case 0:
1252 term_printf("disabled\n");
1253 break;
1254 case 1:
1255 term_printf("enabled for user code\n");
1256 break;
1257 case 2:
1258 term_printf("enabled for user and kernel code\n");
1259 break;
1261 #else
1262 term_printf("kqemu support: not compiled\n");
1263 #endif
1266 #ifdef CONFIG_PROFILER
1268 int64_t kqemu_time;
1269 int64_t qemu_time;
1270 int64_t kqemu_exec_count;
1271 int64_t dev_time;
1272 int64_t kqemu_ret_int_count;
1273 int64_t kqemu_ret_excp_count;
1274 int64_t kqemu_ret_intr_count;
1276 static void do_info_profile(void)
1278 int64_t total;
1279 total = qemu_time;
1280 if (total == 0)
1281 total = 1;
1282 term_printf("async time %" PRId64 " (%0.3f)\n",
1283 dev_time, dev_time / (double)ticks_per_sec);
1284 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1285 qemu_time, qemu_time / (double)ticks_per_sec);
1286 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1287 kqemu_time, kqemu_time / (double)ticks_per_sec,
1288 kqemu_time / (double)total * 100.0,
1289 kqemu_exec_count,
1290 kqemu_ret_int_count,
1291 kqemu_ret_excp_count,
1292 kqemu_ret_intr_count);
1293 qemu_time = 0;
1294 kqemu_time = 0;
1295 kqemu_exec_count = 0;
1296 dev_time = 0;
1297 kqemu_ret_int_count = 0;
1298 kqemu_ret_excp_count = 0;
1299 kqemu_ret_intr_count = 0;
1300 #ifdef USE_KQEMU
1301 kqemu_record_dump();
1302 #endif
1304 #else
1305 static void do_info_profile(void)
1307 term_printf("Internal profiler not compiled\n");
1309 #endif
1311 /* Capture support */
1312 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1314 static void do_info_capture (void)
1316 int i;
1317 CaptureState *s;
1319 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1320 term_printf ("[%d]: ", i);
1321 s->ops.info (s->opaque);
1325 static void do_stop_capture (int n)
1327 int i;
1328 CaptureState *s;
1330 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1331 if (i == n) {
1332 s->ops.destroy (s->opaque);
1333 LIST_REMOVE (s, entries);
1334 qemu_free (s);
1335 return;
1340 #ifdef HAS_AUDIO
1341 static void do_wav_capture (const char *path,
1342 int has_freq, int freq,
1343 int has_bits, int bits,
1344 int has_channels, int nchannels)
1346 CaptureState *s;
1348 s = qemu_mallocz (sizeof (*s));
1349 if (!s) {
1350 term_printf ("Not enough memory to add wave capture\n");
1351 return;
1354 freq = has_freq ? freq : 44100;
1355 bits = has_bits ? bits : 16;
1356 nchannels = has_channels ? nchannels : 2;
1358 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1359 term_printf ("Faied to add wave capture\n");
1360 qemu_free (s);
1362 LIST_INSERT_HEAD (&capture_head, s, entries);
1364 #endif
1366 #if defined(TARGET_I386)
1367 static void do_inject_nmi(int cpu_index)
1369 CPUState *env;
1371 for (env = first_cpu; env != NULL; env = env->next_cpu)
1372 if (env->cpu_index == cpu_index) {
1373 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1374 break;
1377 #endif
1379 static const term_cmd_t term_cmds[] = {
1380 { "help|?", "s?", do_help,
1381 "[cmd]", "show the help" },
1382 { "commit", "s", do_commit,
1383 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1384 { "info", "s?", do_info,
1385 "subcommand", "show various information about the system state" },
1386 { "q|quit", "", do_quit,
1387 "", "quit the emulator" },
1388 { "eject", "-fB", do_eject,
1389 "[-f] device", "eject a removable medium (use -f to force it)" },
1390 { "change", "BFs?", do_change,
1391 "device filename [format]", "change a removable medium, optional format" },
1392 { "screendump", "F", do_screen_dump,
1393 "filename", "save screen into PPM image 'filename'" },
1394 { "logfile", "F", do_logfile,
1395 "filename", "output logs to 'filename'" },
1396 { "log", "s", do_log,
1397 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1398 { "savevm", "s?", do_savevm,
1399 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1400 { "loadvm", "s", do_loadvm,
1401 "tag|id", "restore a VM snapshot from its tag or id" },
1402 { "delvm", "s", do_delvm,
1403 "tag|id", "delete a VM snapshot from its tag or id" },
1404 { "stop", "", do_stop,
1405 "", "stop emulation", },
1406 { "c|cont", "", do_cont,
1407 "", "resume emulation", },
1408 #ifdef CONFIG_GDBSTUB
1409 { "gdbserver", "s?", do_gdbserver,
1410 "[port]", "start gdbserver session (default port=1234)", },
1411 #endif
1412 { "x", "/l", do_memory_dump,
1413 "/fmt addr", "virtual memory dump starting at 'addr'", },
1414 { "xp", "/l", do_physical_memory_dump,
1415 "/fmt addr", "physical memory dump starting at 'addr'", },
1416 { "p|print", "/l", do_print,
1417 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1418 { "i", "/ii.", do_ioport_read,
1419 "/fmt addr", "I/O port read" },
1421 { "sendkey", "si?", do_sendkey,
1422 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1423 { "system_reset", "", do_system_reset,
1424 "", "reset the system" },
1425 { "system_powerdown", "", do_system_powerdown,
1426 "", "send system power down event" },
1427 { "sum", "ii", do_sum,
1428 "addr size", "compute the checksum of a memory region" },
1429 { "usb_add", "s", do_usb_add,
1430 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1431 { "usb_del", "s", do_usb_del,
1432 "device", "remove USB device 'bus.addr'" },
1433 { "cpu", "i", do_cpu_set,
1434 "index", "set the default CPU" },
1435 { "mouse_move", "sss?", do_mouse_move,
1436 "dx dy [dz]", "send mouse move events" },
1437 { "mouse_button", "i", do_mouse_button,
1438 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1439 { "mouse_set", "i", do_mouse_set,
1440 "index", "set which mouse device receives events" },
1441 #ifdef HAS_AUDIO
1442 { "wavcapture", "si?i?i?", do_wav_capture,
1443 "path [frequency bits channels]",
1444 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1445 #endif
1446 { "stopcapture", "i", do_stop_capture,
1447 "capture index", "stop capture" },
1448 { "memsave", "lis", do_memory_save,
1449 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1450 { "pmemsave", "lis", do_physical_memory_save,
1451 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1452 { "boot_set", "s", do_boot_set,
1453 "bootdevice", "define new values for the boot device list" },
1454 #if defined(TARGET_I386)
1455 { "nmi", "i", do_inject_nmi,
1456 "cpu", "inject an NMI on the given CPU", },
1457 #endif
1458 { "migrate", "-ds", do_migrate,
1459 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1460 { "migrate_cancel", "", do_migrate_cancel,
1461 "", "cancel the current VM migration" },
1462 { "migrate_set_speed", "s", do_migrate_set_speed,
1463 "value", "set maximum speed (in bytes) for migrations" },
1464 { NULL, NULL, },
1467 static const term_cmd_t info_cmds[] = {
1468 { "version", "", do_info_version,
1469 "", "show the version of qemu" },
1470 { "network", "", do_info_network,
1471 "", "show the network state" },
1472 { "chardev", "", qemu_chr_info,
1473 "", "show the character devices" },
1474 { "block", "", do_info_block,
1475 "", "show the block devices" },
1476 { "blockstats", "", do_info_blockstats,
1477 "", "show block device statistics" },
1478 { "registers", "", do_info_registers,
1479 "", "show the cpu registers" },
1480 { "cpus", "", do_info_cpus,
1481 "", "show infos for each CPU" },
1482 { "history", "", do_info_history,
1483 "", "show the command line history", },
1484 { "irq", "", irq_info,
1485 "", "show the interrupts statistics (if available)", },
1486 { "pic", "", pic_info,
1487 "", "show i8259 (PIC) state", },
1488 { "pci", "", pci_info,
1489 "", "show PCI info", },
1490 #if defined(TARGET_I386)
1491 { "tlb", "", tlb_info,
1492 "", "show virtual to physical memory mappings", },
1493 { "mem", "", mem_info,
1494 "", "show the active virtual memory mappings", },
1495 #endif
1496 { "jit", "", do_info_jit,
1497 "", "show dynamic compiler info", },
1498 { "kqemu", "", do_info_kqemu,
1499 "", "show kqemu information", },
1500 { "usb", "", usb_info,
1501 "", "show guest USB devices", },
1502 { "usbhost", "", usb_host_info,
1503 "", "show host USB devices", },
1504 { "profile", "", do_info_profile,
1505 "", "show profiling information", },
1506 { "capture", "", do_info_capture,
1507 "", "show capture information" },
1508 { "snapshots", "", do_info_snapshots,
1509 "", "show the currently saved VM snapshots" },
1510 { "pcmcia", "", pcmcia_info,
1511 "", "show guest PCMCIA status" },
1512 { "mice", "", do_info_mice,
1513 "", "show which guest mouse is receiving events" },
1514 { "vnc", "", do_info_vnc,
1515 "", "show the vnc server status"},
1516 { "name", "", do_info_name,
1517 "", "show the current VM name" },
1518 { "uuid", "", do_info_uuid,
1519 "", "show the current VM UUID" },
1520 #if defined(TARGET_PPC)
1521 { "cpustats", "", do_info_cpu_stats,
1522 "", "show CPU statistics", },
1523 #endif
1524 #if defined(CONFIG_SLIRP)
1525 { "slirp", "", do_info_slirp,
1526 "", "show SLIRP statistics", },
1527 #endif
1528 { "migrate", "", do_info_migrate, "", "show migration status" },
1529 { NULL, NULL, },
1532 /*******************************************************************/
1534 static const char *pch;
1535 static jmp_buf expr_env;
1537 #define MD_TLONG 0
1538 #define MD_I32 1
1540 typedef struct MonitorDef {
1541 const char *name;
1542 int offset;
1543 target_long (*get_value)(const struct MonitorDef *md, int val);
1544 int type;
1545 } MonitorDef;
1547 #if defined(TARGET_I386)
1548 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1550 CPUState *env = mon_get_cpu();
1551 if (!env)
1552 return 0;
1553 return env->eip + env->segs[R_CS].base;
1555 #endif
1557 #if defined(TARGET_PPC)
1558 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1560 CPUState *env = mon_get_cpu();
1561 unsigned int u;
1562 int i;
1564 if (!env)
1565 return 0;
1567 u = 0;
1568 for (i = 0; i < 8; i++)
1569 u |= env->crf[i] << (32 - (4 * i));
1571 return u;
1574 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1576 CPUState *env = mon_get_cpu();
1577 if (!env)
1578 return 0;
1579 return env->msr;
1582 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1584 CPUState *env = mon_get_cpu();
1585 if (!env)
1586 return 0;
1587 return env->xer;
1590 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1592 CPUState *env = mon_get_cpu();
1593 if (!env)
1594 return 0;
1595 return cpu_ppc_load_decr(env);
1598 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1600 CPUState *env = mon_get_cpu();
1601 if (!env)
1602 return 0;
1603 return cpu_ppc_load_tbu(env);
1606 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1608 CPUState *env = mon_get_cpu();
1609 if (!env)
1610 return 0;
1611 return cpu_ppc_load_tbl(env);
1613 #endif
1615 #if defined(TARGET_SPARC)
1616 #ifndef TARGET_SPARC64
1617 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1619 CPUState *env = mon_get_cpu();
1620 if (!env)
1621 return 0;
1622 return GET_PSR(env);
1624 #endif
1626 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1628 CPUState *env = mon_get_cpu();
1629 if (!env)
1630 return 0;
1631 return env->regwptr[val];
1633 #endif
1635 static const MonitorDef monitor_defs[] = {
1636 #ifdef TARGET_I386
1638 #define SEG(name, seg) \
1639 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1640 { name ".base", offsetof(CPUState, segs[seg].base) },\
1641 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1643 { "eax", offsetof(CPUState, regs[0]) },
1644 { "ecx", offsetof(CPUState, regs[1]) },
1645 { "edx", offsetof(CPUState, regs[2]) },
1646 { "ebx", offsetof(CPUState, regs[3]) },
1647 { "esp|sp", offsetof(CPUState, regs[4]) },
1648 { "ebp|fp", offsetof(CPUState, regs[5]) },
1649 { "esi", offsetof(CPUState, regs[6]) },
1650 { "edi", offsetof(CPUState, regs[7]) },
1651 #ifdef TARGET_X86_64
1652 { "r8", offsetof(CPUState, regs[8]) },
1653 { "r9", offsetof(CPUState, regs[9]) },
1654 { "r10", offsetof(CPUState, regs[10]) },
1655 { "r11", offsetof(CPUState, regs[11]) },
1656 { "r12", offsetof(CPUState, regs[12]) },
1657 { "r13", offsetof(CPUState, regs[13]) },
1658 { "r14", offsetof(CPUState, regs[14]) },
1659 { "r15", offsetof(CPUState, regs[15]) },
1660 #endif
1661 { "eflags", offsetof(CPUState, eflags) },
1662 { "eip", offsetof(CPUState, eip) },
1663 SEG("cs", R_CS)
1664 SEG("ds", R_DS)
1665 SEG("es", R_ES)
1666 SEG("ss", R_SS)
1667 SEG("fs", R_FS)
1668 SEG("gs", R_GS)
1669 { "pc", 0, monitor_get_pc, },
1670 #elif defined(TARGET_PPC)
1671 /* General purpose registers */
1672 { "r0", offsetof(CPUState, gpr[0]) },
1673 { "r1", offsetof(CPUState, gpr[1]) },
1674 { "r2", offsetof(CPUState, gpr[2]) },
1675 { "r3", offsetof(CPUState, gpr[3]) },
1676 { "r4", offsetof(CPUState, gpr[4]) },
1677 { "r5", offsetof(CPUState, gpr[5]) },
1678 { "r6", offsetof(CPUState, gpr[6]) },
1679 { "r7", offsetof(CPUState, gpr[7]) },
1680 { "r8", offsetof(CPUState, gpr[8]) },
1681 { "r9", offsetof(CPUState, gpr[9]) },
1682 { "r10", offsetof(CPUState, gpr[10]) },
1683 { "r11", offsetof(CPUState, gpr[11]) },
1684 { "r12", offsetof(CPUState, gpr[12]) },
1685 { "r13", offsetof(CPUState, gpr[13]) },
1686 { "r14", offsetof(CPUState, gpr[14]) },
1687 { "r15", offsetof(CPUState, gpr[15]) },
1688 { "r16", offsetof(CPUState, gpr[16]) },
1689 { "r17", offsetof(CPUState, gpr[17]) },
1690 { "r18", offsetof(CPUState, gpr[18]) },
1691 { "r19", offsetof(CPUState, gpr[19]) },
1692 { "r20", offsetof(CPUState, gpr[20]) },
1693 { "r21", offsetof(CPUState, gpr[21]) },
1694 { "r22", offsetof(CPUState, gpr[22]) },
1695 { "r23", offsetof(CPUState, gpr[23]) },
1696 { "r24", offsetof(CPUState, gpr[24]) },
1697 { "r25", offsetof(CPUState, gpr[25]) },
1698 { "r26", offsetof(CPUState, gpr[26]) },
1699 { "r27", offsetof(CPUState, gpr[27]) },
1700 { "r28", offsetof(CPUState, gpr[28]) },
1701 { "r29", offsetof(CPUState, gpr[29]) },
1702 { "r30", offsetof(CPUState, gpr[30]) },
1703 { "r31", offsetof(CPUState, gpr[31]) },
1704 /* Floating point registers */
1705 { "f0", offsetof(CPUState, fpr[0]) },
1706 { "f1", offsetof(CPUState, fpr[1]) },
1707 { "f2", offsetof(CPUState, fpr[2]) },
1708 { "f3", offsetof(CPUState, fpr[3]) },
1709 { "f4", offsetof(CPUState, fpr[4]) },
1710 { "f5", offsetof(CPUState, fpr[5]) },
1711 { "f6", offsetof(CPUState, fpr[6]) },
1712 { "f7", offsetof(CPUState, fpr[7]) },
1713 { "f8", offsetof(CPUState, fpr[8]) },
1714 { "f9", offsetof(CPUState, fpr[9]) },
1715 { "f10", offsetof(CPUState, fpr[10]) },
1716 { "f11", offsetof(CPUState, fpr[11]) },
1717 { "f12", offsetof(CPUState, fpr[12]) },
1718 { "f13", offsetof(CPUState, fpr[13]) },
1719 { "f14", offsetof(CPUState, fpr[14]) },
1720 { "f15", offsetof(CPUState, fpr[15]) },
1721 { "f16", offsetof(CPUState, fpr[16]) },
1722 { "f17", offsetof(CPUState, fpr[17]) },
1723 { "f18", offsetof(CPUState, fpr[18]) },
1724 { "f19", offsetof(CPUState, fpr[19]) },
1725 { "f20", offsetof(CPUState, fpr[20]) },
1726 { "f21", offsetof(CPUState, fpr[21]) },
1727 { "f22", offsetof(CPUState, fpr[22]) },
1728 { "f23", offsetof(CPUState, fpr[23]) },
1729 { "f24", offsetof(CPUState, fpr[24]) },
1730 { "f25", offsetof(CPUState, fpr[25]) },
1731 { "f26", offsetof(CPUState, fpr[26]) },
1732 { "f27", offsetof(CPUState, fpr[27]) },
1733 { "f28", offsetof(CPUState, fpr[28]) },
1734 { "f29", offsetof(CPUState, fpr[29]) },
1735 { "f30", offsetof(CPUState, fpr[30]) },
1736 { "f31", offsetof(CPUState, fpr[31]) },
1737 { "fpscr", offsetof(CPUState, fpscr) },
1738 /* Next instruction pointer */
1739 { "nip|pc", offsetof(CPUState, nip) },
1740 { "lr", offsetof(CPUState, lr) },
1741 { "ctr", offsetof(CPUState, ctr) },
1742 { "decr", 0, &monitor_get_decr, },
1743 { "ccr", 0, &monitor_get_ccr, },
1744 /* Machine state register */
1745 { "msr", 0, &monitor_get_msr, },
1746 { "xer", 0, &monitor_get_xer, },
1747 { "tbu", 0, &monitor_get_tbu, },
1748 { "tbl", 0, &monitor_get_tbl, },
1749 #if defined(TARGET_PPC64)
1750 /* Address space register */
1751 { "asr", offsetof(CPUState, asr) },
1752 #endif
1753 /* Segment registers */
1754 { "sdr1", offsetof(CPUState, sdr1) },
1755 { "sr0", offsetof(CPUState, sr[0]) },
1756 { "sr1", offsetof(CPUState, sr[1]) },
1757 { "sr2", offsetof(CPUState, sr[2]) },
1758 { "sr3", offsetof(CPUState, sr[3]) },
1759 { "sr4", offsetof(CPUState, sr[4]) },
1760 { "sr5", offsetof(CPUState, sr[5]) },
1761 { "sr6", offsetof(CPUState, sr[6]) },
1762 { "sr7", offsetof(CPUState, sr[7]) },
1763 { "sr8", offsetof(CPUState, sr[8]) },
1764 { "sr9", offsetof(CPUState, sr[9]) },
1765 { "sr10", offsetof(CPUState, sr[10]) },
1766 { "sr11", offsetof(CPUState, sr[11]) },
1767 { "sr12", offsetof(CPUState, sr[12]) },
1768 { "sr13", offsetof(CPUState, sr[13]) },
1769 { "sr14", offsetof(CPUState, sr[14]) },
1770 { "sr15", offsetof(CPUState, sr[15]) },
1771 /* Too lazy to put BATs and SPRs ... */
1772 #elif defined(TARGET_SPARC)
1773 { "g0", offsetof(CPUState, gregs[0]) },
1774 { "g1", offsetof(CPUState, gregs[1]) },
1775 { "g2", offsetof(CPUState, gregs[2]) },
1776 { "g3", offsetof(CPUState, gregs[3]) },
1777 { "g4", offsetof(CPUState, gregs[4]) },
1778 { "g5", offsetof(CPUState, gregs[5]) },
1779 { "g6", offsetof(CPUState, gregs[6]) },
1780 { "g7", offsetof(CPUState, gregs[7]) },
1781 { "o0", 0, monitor_get_reg },
1782 { "o1", 1, monitor_get_reg },
1783 { "o2", 2, monitor_get_reg },
1784 { "o3", 3, monitor_get_reg },
1785 { "o4", 4, monitor_get_reg },
1786 { "o5", 5, monitor_get_reg },
1787 { "o6", 6, monitor_get_reg },
1788 { "o7", 7, monitor_get_reg },
1789 { "l0", 8, monitor_get_reg },
1790 { "l1", 9, monitor_get_reg },
1791 { "l2", 10, monitor_get_reg },
1792 { "l3", 11, monitor_get_reg },
1793 { "l4", 12, monitor_get_reg },
1794 { "l5", 13, monitor_get_reg },
1795 { "l6", 14, monitor_get_reg },
1796 { "l7", 15, monitor_get_reg },
1797 { "i0", 16, monitor_get_reg },
1798 { "i1", 17, monitor_get_reg },
1799 { "i2", 18, monitor_get_reg },
1800 { "i3", 19, monitor_get_reg },
1801 { "i4", 20, monitor_get_reg },
1802 { "i5", 21, monitor_get_reg },
1803 { "i6", 22, monitor_get_reg },
1804 { "i7", 23, monitor_get_reg },
1805 { "pc", offsetof(CPUState, pc) },
1806 { "npc", offsetof(CPUState, npc) },
1807 { "y", offsetof(CPUState, y) },
1808 #ifndef TARGET_SPARC64
1809 { "psr", 0, &monitor_get_psr, },
1810 { "wim", offsetof(CPUState, wim) },
1811 #endif
1812 { "tbr", offsetof(CPUState, tbr) },
1813 { "fsr", offsetof(CPUState, fsr) },
1814 { "f0", offsetof(CPUState, fpr[0]) },
1815 { "f1", offsetof(CPUState, fpr[1]) },
1816 { "f2", offsetof(CPUState, fpr[2]) },
1817 { "f3", offsetof(CPUState, fpr[3]) },
1818 { "f4", offsetof(CPUState, fpr[4]) },
1819 { "f5", offsetof(CPUState, fpr[5]) },
1820 { "f6", offsetof(CPUState, fpr[6]) },
1821 { "f7", offsetof(CPUState, fpr[7]) },
1822 { "f8", offsetof(CPUState, fpr[8]) },
1823 { "f9", offsetof(CPUState, fpr[9]) },
1824 { "f10", offsetof(CPUState, fpr[10]) },
1825 { "f11", offsetof(CPUState, fpr[11]) },
1826 { "f12", offsetof(CPUState, fpr[12]) },
1827 { "f13", offsetof(CPUState, fpr[13]) },
1828 { "f14", offsetof(CPUState, fpr[14]) },
1829 { "f15", offsetof(CPUState, fpr[15]) },
1830 { "f16", offsetof(CPUState, fpr[16]) },
1831 { "f17", offsetof(CPUState, fpr[17]) },
1832 { "f18", offsetof(CPUState, fpr[18]) },
1833 { "f19", offsetof(CPUState, fpr[19]) },
1834 { "f20", offsetof(CPUState, fpr[20]) },
1835 { "f21", offsetof(CPUState, fpr[21]) },
1836 { "f22", offsetof(CPUState, fpr[22]) },
1837 { "f23", offsetof(CPUState, fpr[23]) },
1838 { "f24", offsetof(CPUState, fpr[24]) },
1839 { "f25", offsetof(CPUState, fpr[25]) },
1840 { "f26", offsetof(CPUState, fpr[26]) },
1841 { "f27", offsetof(CPUState, fpr[27]) },
1842 { "f28", offsetof(CPUState, fpr[28]) },
1843 { "f29", offsetof(CPUState, fpr[29]) },
1844 { "f30", offsetof(CPUState, fpr[30]) },
1845 { "f31", offsetof(CPUState, fpr[31]) },
1846 #ifdef TARGET_SPARC64
1847 { "f32", offsetof(CPUState, fpr[32]) },
1848 { "f34", offsetof(CPUState, fpr[34]) },
1849 { "f36", offsetof(CPUState, fpr[36]) },
1850 { "f38", offsetof(CPUState, fpr[38]) },
1851 { "f40", offsetof(CPUState, fpr[40]) },
1852 { "f42", offsetof(CPUState, fpr[42]) },
1853 { "f44", offsetof(CPUState, fpr[44]) },
1854 { "f46", offsetof(CPUState, fpr[46]) },
1855 { "f48", offsetof(CPUState, fpr[48]) },
1856 { "f50", offsetof(CPUState, fpr[50]) },
1857 { "f52", offsetof(CPUState, fpr[52]) },
1858 { "f54", offsetof(CPUState, fpr[54]) },
1859 { "f56", offsetof(CPUState, fpr[56]) },
1860 { "f58", offsetof(CPUState, fpr[58]) },
1861 { "f60", offsetof(CPUState, fpr[60]) },
1862 { "f62", offsetof(CPUState, fpr[62]) },
1863 { "asi", offsetof(CPUState, asi) },
1864 { "pstate", offsetof(CPUState, pstate) },
1865 { "cansave", offsetof(CPUState, cansave) },
1866 { "canrestore", offsetof(CPUState, canrestore) },
1867 { "otherwin", offsetof(CPUState, otherwin) },
1868 { "wstate", offsetof(CPUState, wstate) },
1869 { "cleanwin", offsetof(CPUState, cleanwin) },
1870 { "fprs", offsetof(CPUState, fprs) },
1871 #endif
1872 #endif
1873 { NULL },
1876 static void expr_error(const char *fmt)
1878 term_printf(fmt);
1879 term_printf("\n");
1880 longjmp(expr_env, 1);
1883 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1884 static int get_monitor_def(target_long *pval, const char *name)
1886 const MonitorDef *md;
1887 void *ptr;
1889 for(md = monitor_defs; md->name != NULL; md++) {
1890 if (compare_cmd(name, md->name)) {
1891 if (md->get_value) {
1892 *pval = md->get_value(md, md->offset);
1893 } else {
1894 CPUState *env = mon_get_cpu();
1895 if (!env)
1896 return -2;
1897 ptr = (uint8_t *)env + md->offset;
1898 switch(md->type) {
1899 case MD_I32:
1900 *pval = *(int32_t *)ptr;
1901 break;
1902 case MD_TLONG:
1903 *pval = *(target_long *)ptr;
1904 break;
1905 default:
1906 *pval = 0;
1907 break;
1910 return 0;
1913 return -1;
1916 static void next(void)
1918 if (pch != '\0') {
1919 pch++;
1920 while (isspace(*pch))
1921 pch++;
1925 static int64_t expr_sum(void);
1927 static int64_t expr_unary(void)
1929 int64_t n;
1930 char *p;
1931 int ret;
1933 switch(*pch) {
1934 case '+':
1935 next();
1936 n = expr_unary();
1937 break;
1938 case '-':
1939 next();
1940 n = -expr_unary();
1941 break;
1942 case '~':
1943 next();
1944 n = ~expr_unary();
1945 break;
1946 case '(':
1947 next();
1948 n = expr_sum();
1949 if (*pch != ')') {
1950 expr_error("')' expected");
1952 next();
1953 break;
1954 case '\'':
1955 pch++;
1956 if (*pch == '\0')
1957 expr_error("character constant expected");
1958 n = *pch;
1959 pch++;
1960 if (*pch != '\'')
1961 expr_error("missing terminating \' character");
1962 next();
1963 break;
1964 case '$':
1966 char buf[128], *q;
1967 target_long reg=0;
1969 pch++;
1970 q = buf;
1971 while ((*pch >= 'a' && *pch <= 'z') ||
1972 (*pch >= 'A' && *pch <= 'Z') ||
1973 (*pch >= '0' && *pch <= '9') ||
1974 *pch == '_' || *pch == '.') {
1975 if ((q - buf) < sizeof(buf) - 1)
1976 *q++ = *pch;
1977 pch++;
1979 while (isspace(*pch))
1980 pch++;
1981 *q = 0;
1982 ret = get_monitor_def(&reg, buf);
1983 if (ret == -1)
1984 expr_error("unknown register");
1985 else if (ret == -2)
1986 expr_error("no cpu defined");
1987 n = reg;
1989 break;
1990 case '\0':
1991 expr_error("unexpected end of expression");
1992 n = 0;
1993 break;
1994 default:
1995 #if TARGET_PHYS_ADDR_BITS > 32
1996 n = strtoull(pch, &p, 0);
1997 #else
1998 n = strtoul(pch, &p, 0);
1999 #endif
2000 if (pch == p) {
2001 expr_error("invalid char in expression");
2003 pch = p;
2004 while (isspace(*pch))
2005 pch++;
2006 break;
2008 return n;
2012 static int64_t expr_prod(void)
2014 int64_t val, val2;
2015 int op;
2017 val = expr_unary();
2018 for(;;) {
2019 op = *pch;
2020 if (op != '*' && op != '/' && op != '%')
2021 break;
2022 next();
2023 val2 = expr_unary();
2024 switch(op) {
2025 default:
2026 case '*':
2027 val *= val2;
2028 break;
2029 case '/':
2030 case '%':
2031 if (val2 == 0)
2032 expr_error("division by zero");
2033 if (op == '/')
2034 val /= val2;
2035 else
2036 val %= val2;
2037 break;
2040 return val;
2043 static int64_t expr_logic(void)
2045 int64_t val, val2;
2046 int op;
2048 val = expr_prod();
2049 for(;;) {
2050 op = *pch;
2051 if (op != '&' && op != '|' && op != '^')
2052 break;
2053 next();
2054 val2 = expr_prod();
2055 switch(op) {
2056 default:
2057 case '&':
2058 val &= val2;
2059 break;
2060 case '|':
2061 val |= val2;
2062 break;
2063 case '^':
2064 val ^= val2;
2065 break;
2068 return val;
2071 static int64_t expr_sum(void)
2073 int64_t val, val2;
2074 int op;
2076 val = expr_logic();
2077 for(;;) {
2078 op = *pch;
2079 if (op != '+' && op != '-')
2080 break;
2081 next();
2082 val2 = expr_logic();
2083 if (op == '+')
2084 val += val2;
2085 else
2086 val -= val2;
2088 return val;
2091 static int get_expr(int64_t *pval, const char **pp)
2093 pch = *pp;
2094 if (setjmp(expr_env)) {
2095 *pp = pch;
2096 return -1;
2098 while (isspace(*pch))
2099 pch++;
2100 *pval = expr_sum();
2101 *pp = pch;
2102 return 0;
2105 static int get_str(char *buf, int buf_size, const char **pp)
2107 const char *p;
2108 char *q;
2109 int c;
2111 q = buf;
2112 p = *pp;
2113 while (isspace(*p))
2114 p++;
2115 if (*p == '\0') {
2116 fail:
2117 *q = '\0';
2118 *pp = p;
2119 return -1;
2121 if (*p == '\"') {
2122 p++;
2123 while (*p != '\0' && *p != '\"') {
2124 if (*p == '\\') {
2125 p++;
2126 c = *p++;
2127 switch(c) {
2128 case 'n':
2129 c = '\n';
2130 break;
2131 case 'r':
2132 c = '\r';
2133 break;
2134 case '\\':
2135 case '\'':
2136 case '\"':
2137 break;
2138 default:
2139 qemu_printf("unsupported escape code: '\\%c'\n", c);
2140 goto fail;
2142 if ((q - buf) < buf_size - 1) {
2143 *q++ = c;
2145 } else {
2146 if ((q - buf) < buf_size - 1) {
2147 *q++ = *p;
2149 p++;
2152 if (*p != '\"') {
2153 qemu_printf("unterminated string\n");
2154 goto fail;
2156 p++;
2157 } else {
2158 while (*p != '\0' && !isspace(*p)) {
2159 if ((q - buf) < buf_size - 1) {
2160 *q++ = *p;
2162 p++;
2165 *q = '\0';
2166 *pp = p;
2167 return 0;
2170 static int default_fmt_format = 'x';
2171 static int default_fmt_size = 4;
2173 #define MAX_ARGS 16
2175 static void monitor_handle_command(const char *cmdline)
2177 const char *p, *pstart, *typestr;
2178 char *q;
2179 int c, nb_args, len, i, has_arg;
2180 const term_cmd_t *cmd;
2181 char cmdname[256];
2182 char buf[1024];
2183 void *str_allocated[MAX_ARGS];
2184 void *args[MAX_ARGS];
2185 void (*handler_0)(void);
2186 void (*handler_1)(void *arg0);
2187 void (*handler_2)(void *arg0, void *arg1);
2188 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2189 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2190 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2191 void *arg4);
2192 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2193 void *arg4, void *arg5);
2194 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2195 void *arg4, void *arg5, void *arg6);
2197 #ifdef DEBUG
2198 term_printf("command='%s'\n", cmdline);
2199 #endif
2201 /* extract the command name */
2202 p = cmdline;
2203 q = cmdname;
2204 while (isspace(*p))
2205 p++;
2206 if (*p == '\0')
2207 return;
2208 pstart = p;
2209 while (*p != '\0' && *p != '/' && !isspace(*p))
2210 p++;
2211 len = p - pstart;
2212 if (len > sizeof(cmdname) - 1)
2213 len = sizeof(cmdname) - 1;
2214 memcpy(cmdname, pstart, len);
2215 cmdname[len] = '\0';
2217 /* find the command */
2218 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2219 if (compare_cmd(cmdname, cmd->name))
2220 goto found;
2222 term_printf("unknown command: '%s'\n", cmdname);
2223 return;
2224 found:
2226 for(i = 0; i < MAX_ARGS; i++)
2227 str_allocated[i] = NULL;
2229 /* parse the parameters */
2230 typestr = cmd->args_type;
2231 nb_args = 0;
2232 for(;;) {
2233 c = *typestr;
2234 if (c == '\0')
2235 break;
2236 typestr++;
2237 switch(c) {
2238 case 'F':
2239 case 'B':
2240 case 's':
2242 int ret;
2243 char *str;
2245 while (isspace(*p))
2246 p++;
2247 if (*typestr == '?') {
2248 typestr++;
2249 if (*p == '\0') {
2250 /* no optional string: NULL argument */
2251 str = NULL;
2252 goto add_str;
2255 ret = get_str(buf, sizeof(buf), &p);
2256 if (ret < 0) {
2257 switch(c) {
2258 case 'F':
2259 term_printf("%s: filename expected\n", cmdname);
2260 break;
2261 case 'B':
2262 term_printf("%s: block device name expected\n", cmdname);
2263 break;
2264 default:
2265 term_printf("%s: string expected\n", cmdname);
2266 break;
2268 goto fail;
2270 str = qemu_malloc(strlen(buf) + 1);
2271 pstrcpy(str, sizeof(buf), buf);
2272 str_allocated[nb_args] = str;
2273 add_str:
2274 if (nb_args >= MAX_ARGS) {
2275 error_args:
2276 term_printf("%s: too many arguments\n", cmdname);
2277 goto fail;
2279 args[nb_args++] = str;
2281 break;
2282 case '/':
2284 int count, format, size;
2286 while (isspace(*p))
2287 p++;
2288 if (*p == '/') {
2289 /* format found */
2290 p++;
2291 count = 1;
2292 if (isdigit(*p)) {
2293 count = 0;
2294 while (isdigit(*p)) {
2295 count = count * 10 + (*p - '0');
2296 p++;
2299 size = -1;
2300 format = -1;
2301 for(;;) {
2302 switch(*p) {
2303 case 'o':
2304 case 'd':
2305 case 'u':
2306 case 'x':
2307 case 'i':
2308 case 'c':
2309 format = *p++;
2310 break;
2311 case 'b':
2312 size = 1;
2313 p++;
2314 break;
2315 case 'h':
2316 size = 2;
2317 p++;
2318 break;
2319 case 'w':
2320 size = 4;
2321 p++;
2322 break;
2323 case 'g':
2324 case 'L':
2325 size = 8;
2326 p++;
2327 break;
2328 default:
2329 goto next;
2332 next:
2333 if (*p != '\0' && !isspace(*p)) {
2334 term_printf("invalid char in format: '%c'\n", *p);
2335 goto fail;
2337 if (format < 0)
2338 format = default_fmt_format;
2339 if (format != 'i') {
2340 /* for 'i', not specifying a size gives -1 as size */
2341 if (size < 0)
2342 size = default_fmt_size;
2343 default_fmt_size = size;
2345 default_fmt_format = format;
2346 } else {
2347 count = 1;
2348 format = default_fmt_format;
2349 if (format != 'i') {
2350 size = default_fmt_size;
2351 } else {
2352 size = -1;
2355 if (nb_args + 3 > MAX_ARGS)
2356 goto error_args;
2357 args[nb_args++] = (void*)(long)count;
2358 args[nb_args++] = (void*)(long)format;
2359 args[nb_args++] = (void*)(long)size;
2361 break;
2362 case 'i':
2363 case 'l':
2365 int64_t val;
2367 while (isspace(*p))
2368 p++;
2369 if (*typestr == '?' || *typestr == '.') {
2370 if (*typestr == '?') {
2371 if (*p == '\0')
2372 has_arg = 0;
2373 else
2374 has_arg = 1;
2375 } else {
2376 if (*p == '.') {
2377 p++;
2378 while (isspace(*p))
2379 p++;
2380 has_arg = 1;
2381 } else {
2382 has_arg = 0;
2385 typestr++;
2386 if (nb_args >= MAX_ARGS)
2387 goto error_args;
2388 args[nb_args++] = (void *)(long)has_arg;
2389 if (!has_arg) {
2390 if (nb_args >= MAX_ARGS)
2391 goto error_args;
2392 val = -1;
2393 goto add_num;
2396 if (get_expr(&val, &p))
2397 goto fail;
2398 add_num:
2399 if (c == 'i') {
2400 if (nb_args >= MAX_ARGS)
2401 goto error_args;
2402 args[nb_args++] = (void *)(long)val;
2403 } else {
2404 if ((nb_args + 1) >= MAX_ARGS)
2405 goto error_args;
2406 #if TARGET_PHYS_ADDR_BITS > 32
2407 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2408 #else
2409 args[nb_args++] = (void *)0;
2410 #endif
2411 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2414 break;
2415 case '-':
2417 int has_option;
2418 /* option */
2420 c = *typestr++;
2421 if (c == '\0')
2422 goto bad_type;
2423 while (isspace(*p))
2424 p++;
2425 has_option = 0;
2426 if (*p == '-') {
2427 p++;
2428 if (*p != c) {
2429 term_printf("%s: unsupported option -%c\n",
2430 cmdname, *p);
2431 goto fail;
2433 p++;
2434 has_option = 1;
2436 if (nb_args >= MAX_ARGS)
2437 goto error_args;
2438 args[nb_args++] = (void *)(long)has_option;
2440 break;
2441 default:
2442 bad_type:
2443 term_printf("%s: unknown type '%c'\n", cmdname, c);
2444 goto fail;
2447 /* check that all arguments were parsed */
2448 while (isspace(*p))
2449 p++;
2450 if (*p != '\0') {
2451 term_printf("%s: extraneous characters at the end of line\n",
2452 cmdname);
2453 goto fail;
2456 switch(nb_args) {
2457 case 0:
2458 handler_0 = cmd->handler;
2459 handler_0();
2460 break;
2461 case 1:
2462 handler_1 = cmd->handler;
2463 handler_1(args[0]);
2464 break;
2465 case 2:
2466 handler_2 = cmd->handler;
2467 handler_2(args[0], args[1]);
2468 break;
2469 case 3:
2470 handler_3 = cmd->handler;
2471 handler_3(args[0], args[1], args[2]);
2472 break;
2473 case 4:
2474 handler_4 = cmd->handler;
2475 handler_4(args[0], args[1], args[2], args[3]);
2476 break;
2477 case 5:
2478 handler_5 = cmd->handler;
2479 handler_5(args[0], args[1], args[2], args[3], args[4]);
2480 break;
2481 case 6:
2482 handler_6 = cmd->handler;
2483 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2484 break;
2485 case 7:
2486 handler_7 = cmd->handler;
2487 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2488 break;
2489 default:
2490 term_printf("unsupported number of arguments: %d\n", nb_args);
2491 goto fail;
2493 fail:
2494 for(i = 0; i < MAX_ARGS; i++)
2495 qemu_free(str_allocated[i]);
2496 return;
2499 static void cmd_completion(const char *name, const char *list)
2501 const char *p, *pstart;
2502 char cmd[128];
2503 int len;
2505 p = list;
2506 for(;;) {
2507 pstart = p;
2508 p = strchr(p, '|');
2509 if (!p)
2510 p = pstart + strlen(pstart);
2511 len = p - pstart;
2512 if (len > sizeof(cmd) - 2)
2513 len = sizeof(cmd) - 2;
2514 memcpy(cmd, pstart, len);
2515 cmd[len] = '\0';
2516 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2517 add_completion(cmd);
2519 if (*p == '\0')
2520 break;
2521 p++;
2525 static void file_completion(const char *input)
2527 DIR *ffs;
2528 struct dirent *d;
2529 char path[1024];
2530 char file[1024], file_prefix[1024];
2531 int input_path_len;
2532 const char *p;
2534 p = strrchr(input, '/');
2535 if (!p) {
2536 input_path_len = 0;
2537 pstrcpy(file_prefix, sizeof(file_prefix), input);
2538 pstrcpy(path, sizeof(path), ".");
2539 } else {
2540 input_path_len = p - input + 1;
2541 memcpy(path, input, input_path_len);
2542 if (input_path_len > sizeof(path) - 1)
2543 input_path_len = sizeof(path) - 1;
2544 path[input_path_len] = '\0';
2545 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2547 #ifdef DEBUG_COMPLETION
2548 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2549 #endif
2550 ffs = opendir(path);
2551 if (!ffs)
2552 return;
2553 for(;;) {
2554 struct stat sb;
2555 d = readdir(ffs);
2556 if (!d)
2557 break;
2558 if (strstart(d->d_name, file_prefix, NULL)) {
2559 memcpy(file, input, input_path_len);
2560 if (input_path_len < sizeof(file))
2561 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2562 d->d_name);
2563 /* stat the file to find out if it's a directory.
2564 * In that case add a slash to speed up typing long paths
2566 stat(file, &sb);
2567 if(S_ISDIR(sb.st_mode))
2568 pstrcat(file, sizeof(file), "/");
2569 add_completion(file);
2572 closedir(ffs);
2575 static void block_completion_it(void *opaque, const char *name)
2577 const char *input = opaque;
2579 if (input[0] == '\0' ||
2580 !strncmp(name, (char *)input, strlen(input))) {
2581 add_completion(name);
2585 /* NOTE: this parser is an approximate form of the real command parser */
2586 static void parse_cmdline(const char *cmdline,
2587 int *pnb_args, char **args)
2589 const char *p;
2590 int nb_args, ret;
2591 char buf[1024];
2593 p = cmdline;
2594 nb_args = 0;
2595 for(;;) {
2596 while (isspace(*p))
2597 p++;
2598 if (*p == '\0')
2599 break;
2600 if (nb_args >= MAX_ARGS)
2601 break;
2602 ret = get_str(buf, sizeof(buf), &p);
2603 args[nb_args] = qemu_strdup(buf);
2604 nb_args++;
2605 if (ret < 0)
2606 break;
2608 *pnb_args = nb_args;
2611 void readline_find_completion(const char *cmdline)
2613 const char *cmdname;
2614 char *args[MAX_ARGS];
2615 int nb_args, i, len;
2616 const char *ptype, *str;
2617 const term_cmd_t *cmd;
2618 const KeyDef *key;
2620 parse_cmdline(cmdline, &nb_args, args);
2621 #ifdef DEBUG_COMPLETION
2622 for(i = 0; i < nb_args; i++) {
2623 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2625 #endif
2627 /* if the line ends with a space, it means we want to complete the
2628 next arg */
2629 len = strlen(cmdline);
2630 if (len > 0 && isspace(cmdline[len - 1])) {
2631 if (nb_args >= MAX_ARGS)
2632 return;
2633 args[nb_args++] = qemu_strdup("");
2635 if (nb_args <= 1) {
2636 /* command completion */
2637 if (nb_args == 0)
2638 cmdname = "";
2639 else
2640 cmdname = args[0];
2641 completion_index = strlen(cmdname);
2642 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2643 cmd_completion(cmdname, cmd->name);
2645 } else {
2646 /* find the command */
2647 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2648 if (compare_cmd(args[0], cmd->name))
2649 goto found;
2651 return;
2652 found:
2653 ptype = cmd->args_type;
2654 for(i = 0; i < nb_args - 2; i++) {
2655 if (*ptype != '\0') {
2656 ptype++;
2657 while (*ptype == '?')
2658 ptype++;
2661 str = args[nb_args - 1];
2662 switch(*ptype) {
2663 case 'F':
2664 /* file completion */
2665 completion_index = strlen(str);
2666 file_completion(str);
2667 break;
2668 case 'B':
2669 /* block device name completion */
2670 completion_index = strlen(str);
2671 bdrv_iterate(block_completion_it, (void *)str);
2672 break;
2673 case 's':
2674 /* XXX: more generic ? */
2675 if (!strcmp(cmd->name, "info")) {
2676 completion_index = strlen(str);
2677 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2678 cmd_completion(str, cmd->name);
2680 } else if (!strcmp(cmd->name, "sendkey")) {
2681 completion_index = strlen(str);
2682 for(key = key_defs; key->name != NULL; key++) {
2683 cmd_completion(str, key->name);
2686 break;
2687 default:
2688 break;
2691 for(i = 0; i < nb_args; i++)
2692 qemu_free(args[i]);
2695 static int term_can_read(void *opaque)
2697 return 128;
2700 static void term_read(void *opaque, const uint8_t *buf, int size)
2702 int i;
2703 for(i = 0; i < size; i++)
2704 readline_handle_byte(buf[i]);
2707 static int monitor_suspended;
2709 static void monitor_handle_command1(void *opaque, const char *cmdline)
2711 monitor_handle_command(cmdline);
2712 if (!monitor_suspended)
2713 monitor_start_input();
2714 else
2715 monitor_suspended = 2;
2718 void monitor_suspend(void)
2720 monitor_suspended = 1;
2723 void monitor_resume(void)
2725 if (monitor_suspended == 2)
2726 monitor_start_input();
2727 monitor_suspended = 0;
2730 static void monitor_start_input(void)
2732 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2735 static void term_event(void *opaque, int event)
2737 if (event != CHR_EVENT_RESET)
2738 return;
2740 if (!hide_banner)
2741 term_printf("QEMU %s monitor - type 'help' for more information\n",
2742 QEMU_VERSION);
2743 monitor_start_input();
2746 static int is_first_init = 1;
2748 void monitor_init(CharDriverState *hd, int show_banner)
2750 int i;
2752 if (is_first_init) {
2753 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2754 if (!key_timer)
2755 return;
2756 for (i = 0; i < MAX_MON; i++) {
2757 monitor_hd[i] = NULL;
2759 is_first_init = 0;
2761 for (i = 0; i < MAX_MON; i++) {
2762 if (monitor_hd[i] == NULL) {
2763 monitor_hd[i] = hd;
2764 break;
2768 hide_banner = !show_banner;
2770 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2772 readline_start("", 0, monitor_handle_command1, NULL);
2775 /* XXX: use threads ? */
2776 /* modal monitor readline */
2777 static int monitor_readline_started;
2778 static char *monitor_readline_buf;
2779 static int monitor_readline_buf_size;
2781 static void monitor_readline_cb(void *opaque, const char *input)
2783 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2784 monitor_readline_started = 0;
2787 void monitor_readline(const char *prompt, int is_password,
2788 char *buf, int buf_size)
2790 int i;
2791 int old_focus[MAX_MON];
2793 if (is_password) {
2794 for (i = 0; i < MAX_MON; i++) {
2795 old_focus[i] = 0;
2796 if (monitor_hd[i]) {
2797 old_focus[i] = monitor_hd[i]->focus;
2798 monitor_hd[i]->focus = 0;
2799 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2804 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2805 monitor_readline_buf = buf;
2806 monitor_readline_buf_size = buf_size;
2807 monitor_readline_started = 1;
2808 while (monitor_readline_started) {
2809 main_loop_wait(10);
2811 /* restore original focus */
2812 if (is_password) {
2813 for (i = 0; i < MAX_MON; i++)
2814 if (old_focus[i])
2815 monitor_hd[i]->focus = old_focus[i];