using _exit in fork() (Kamo Hiroyasu)
[qemu/mini2440.git] / monitor.c
blob70b473ddbff6ba7b3b3373485a05640675a88d2e
1 /*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
28 //#define DEBUG
29 //#define DEBUG_COMPLETION
31 #ifndef offsetof
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
33 #endif
36 * Supported types:
38 * 'F' filename
39 * 'B' block device name
40 * 's' string (accept optional quote)
41 * 'i' 32 bit integer
42 * 'l' target long (32 or 64 bit)
43 * '/' optional gdb-like print format (like "/10x")
45 * '?' optional type (for 'F', 's' and 'i')
49 typedef struct term_cmd_t {
50 const char *name;
51 const char *args_type;
52 void (*handler)();
53 const char *params;
54 const char *help;
55 } term_cmd_t;
57 static CharDriverState *monitor_hd;
59 static term_cmd_t term_cmds[];
60 static term_cmd_t info_cmds[];
62 static char term_outbuf[1024];
63 static int term_outbuf_index;
65 static void monitor_start_input(void);
67 CPUState *mon_cpu = NULL;
69 void term_flush(void)
71 if (term_outbuf_index > 0) {
72 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
73 term_outbuf_index = 0;
77 /* flush at every end of line or if the buffer is full */
78 void term_puts(const char *str)
80 int c;
81 for(;;) {
82 c = *str++;
83 if (c == '\0')
84 break;
85 term_outbuf[term_outbuf_index++] = c;
86 if (term_outbuf_index >= sizeof(term_outbuf) ||
87 c == '\n')
88 term_flush();
92 void term_vprintf(const char *fmt, va_list ap)
94 char buf[4096];
95 vsnprintf(buf, sizeof(buf), fmt, ap);
96 term_puts(buf);
99 void term_printf(const char *fmt, ...)
101 va_list ap;
102 va_start(ap, fmt);
103 term_vprintf(fmt, ap);
104 va_end(ap);
107 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
109 va_list ap;
110 va_start(ap, fmt);
111 term_vprintf(fmt, ap);
112 va_end(ap);
113 return 0;
116 static int compare_cmd(const char *name, const char *list)
118 const char *p, *pstart;
119 int len;
120 len = strlen(name);
121 p = list;
122 for(;;) {
123 pstart = p;
124 p = strchr(p, '|');
125 if (!p)
126 p = pstart + strlen(pstart);
127 if ((p - pstart) == len && !memcmp(pstart, name, len))
128 return 1;
129 if (*p == '\0')
130 break;
131 p++;
133 return 0;
136 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
138 term_cmd_t *cmd;
140 for(cmd = cmds; cmd->name != NULL; cmd++) {
141 if (!name || !strcmp(name, cmd->name))
142 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
146 static void help_cmd(const char *name)
148 if (name && !strcmp(name, "info")) {
149 help_cmd1(info_cmds, "info ", NULL);
150 } else {
151 help_cmd1(term_cmds, "", name);
152 if (name && !strcmp(name, "log")) {
153 CPULogItem *item;
154 term_printf("Log items (comma separated):\n");
155 term_printf("%-10s %s\n", "none", "remove all logs");
156 for(item = cpu_log_items; item->mask != 0; item++) {
157 term_printf("%-10s %s\n", item->name, item->help);
163 static void do_help(const char *name)
165 help_cmd(name);
168 static void do_commit(void)
170 int i;
172 for (i = 0; i < MAX_DISKS; i++) {
173 if (bs_table[i]) {
174 bdrv_commit(bs_table[i]);
179 static void do_info(const char *item)
181 term_cmd_t *cmd;
183 if (!item)
184 goto help;
185 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
186 if (compare_cmd(item, cmd->name))
187 goto found;
189 help:
190 help_cmd("info");
191 return;
192 found:
193 cmd->handler();
196 static void do_info_version(void)
198 term_printf("%s\n", QEMU_VERSION);
201 static void do_info_block(void)
203 bdrv_info();
206 /* get the current CPU defined by the user */
207 int mon_set_cpu(int cpu_index)
209 CPUState *env;
211 for(env = first_cpu; env != NULL; env = env->next_cpu) {
212 if (env->cpu_index == cpu_index) {
213 mon_cpu = env;
214 return 0;
217 return -1;
220 CPUState *mon_get_cpu(void)
222 if (!mon_cpu) {
223 mon_set_cpu(0);
225 return mon_cpu;
228 static void do_info_registers(void)
230 CPUState *env;
231 env = mon_get_cpu();
232 if (!env)
233 return;
234 #ifdef TARGET_I386
235 cpu_dump_state(env, NULL, monitor_fprintf,
236 X86_DUMP_FPU);
237 #else
238 cpu_dump_state(env, NULL, monitor_fprintf,
240 #endif
243 static void do_info_cpus(void)
245 CPUState *env;
247 /* just to set the default cpu if not already done */
248 mon_get_cpu();
250 for(env = first_cpu; env != NULL; env = env->next_cpu) {
251 term_printf("%c CPU #%d:",
252 (env == mon_cpu) ? '*' : ' ',
253 env->cpu_index);
254 #if defined(TARGET_I386)
255 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
256 if (env->hflags & HF_HALTED_MASK)
257 term_printf(" (halted)");
258 #elif defined(TARGET_PPC)
259 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
260 if (msr_pow)
261 term_printf(" (halted)");
262 #endif
263 term_printf("\n");
267 static void do_cpu_set(int index)
269 if (mon_set_cpu(index) < 0)
270 term_printf("Invalid CPU index\n");
273 static void do_info_jit(void)
275 dump_exec_info(NULL, monitor_fprintf);
278 static void do_info_history (void)
280 int i;
281 const char *str;
283 i = 0;
284 for(;;) {
285 str = readline_get_history(i);
286 if (!str)
287 break;
288 term_printf("%d: '%s'\n", i, str);
289 i++;
293 static void do_quit(void)
295 #ifdef USE_KQEMU
296 kqemu_record_dump();
297 #endif
298 exit(0);
301 static int eject_device(BlockDriverState *bs, int force)
303 if (bdrv_is_inserted(bs)) {
304 if (!force) {
305 if (!bdrv_is_removable(bs)) {
306 term_printf("device is not removable\n");
307 return -1;
309 if (bdrv_is_locked(bs)) {
310 term_printf("device is locked\n");
311 return -1;
314 bdrv_close(bs);
316 return 0;
319 static void do_eject(int force, const char *filename)
321 BlockDriverState *bs;
323 bs = bdrv_find(filename);
324 if (!bs) {
325 term_printf("device not found\n");
326 return;
328 eject_device(bs, force);
331 static void do_change(const char *device, const char *filename)
333 BlockDriverState *bs;
334 int i;
335 char password[256];
337 bs = bdrv_find(device);
338 if (!bs) {
339 term_printf("device not found\n");
340 return;
342 if (eject_device(bs, 0) < 0)
343 return;
344 bdrv_open(bs, filename, 0);
345 if (bdrv_is_encrypted(bs)) {
346 term_printf("%s is encrypted.\n", device);
347 for(i = 0; i < 3; i++) {
348 monitor_readline("Password: ", 1, password, sizeof(password));
349 if (bdrv_set_key(bs, password) == 0)
350 break;
351 term_printf("invalid password\n");
356 static void do_screen_dump(const char *filename)
358 vga_screen_dump(filename);
361 static void do_log(const char *items)
363 int mask;
365 if (!strcmp(items, "none")) {
366 mask = 0;
367 } else {
368 mask = cpu_str_to_log_mask(items);
369 if (!mask) {
370 help_cmd("log");
371 return;
374 cpu_set_log(mask);
377 static void do_savevm(const char *filename)
379 if (qemu_savevm(filename) < 0)
380 term_printf("I/O error when saving VM to '%s'\n", filename);
383 static void do_loadvm(const char *filename)
385 if (qemu_loadvm(filename) < 0)
386 term_printf("I/O error when loading VM from '%s'\n", filename);
389 static void do_stop(void)
391 vm_stop(EXCP_INTERRUPT);
394 static void do_cont(void)
396 vm_start();
399 #ifdef CONFIG_GDBSTUB
400 static void do_gdbserver(int has_port, int port)
402 if (!has_port)
403 port = DEFAULT_GDBSTUB_PORT;
404 if (gdbserver_start(port) < 0) {
405 qemu_printf("Could not open gdbserver socket on port %d\n", port);
406 } else {
407 qemu_printf("Waiting gdb connection on port %d\n", port);
410 #endif
412 static void term_printc(int c)
414 term_printf("'");
415 switch(c) {
416 case '\'':
417 term_printf("\\'");
418 break;
419 case '\\':
420 term_printf("\\\\");
421 break;
422 case '\n':
423 term_printf("\\n");
424 break;
425 case '\r':
426 term_printf("\\r");
427 break;
428 default:
429 if (c >= 32 && c <= 126) {
430 term_printf("%c", c);
431 } else {
432 term_printf("\\x%02x", c);
434 break;
436 term_printf("'");
439 static void memory_dump(int count, int format, int wsize,
440 target_ulong addr, int is_physical)
442 CPUState *env;
443 int nb_per_line, l, line_size, i, max_digits, len;
444 uint8_t buf[16];
445 uint64_t v;
447 if (format == 'i') {
448 int flags;
449 flags = 0;
450 env = mon_get_cpu();
451 if (!env && !is_physical)
452 return;
453 #ifdef TARGET_I386
454 if (wsize == 2) {
455 flags = 1;
456 } else if (wsize == 4) {
457 flags = 0;
458 } else {
459 /* as default we use the current CS size */
460 flags = 0;
461 if (env && !(env->segs[R_CS].flags & DESC_B_MASK))
462 flags = 1;
464 #endif
465 monitor_disas(env, addr, count, is_physical, flags);
466 return;
469 len = wsize * count;
470 if (wsize == 1)
471 line_size = 8;
472 else
473 line_size = 16;
474 nb_per_line = line_size / wsize;
475 max_digits = 0;
477 switch(format) {
478 case 'o':
479 max_digits = (wsize * 8 + 2) / 3;
480 break;
481 default:
482 case 'x':
483 max_digits = (wsize * 8) / 4;
484 break;
485 case 'u':
486 case 'd':
487 max_digits = (wsize * 8 * 10 + 32) / 33;
488 break;
489 case 'c':
490 wsize = 1;
491 break;
494 while (len > 0) {
495 term_printf(TARGET_FMT_lx ":", addr);
496 l = len;
497 if (l > line_size)
498 l = line_size;
499 if (is_physical) {
500 cpu_physical_memory_rw(addr, buf, l, 0);
501 } else {
502 env = mon_get_cpu();
503 if (!env)
504 break;
505 cpu_memory_rw_debug(env, addr, buf, l, 0);
507 i = 0;
508 while (i < l) {
509 switch(wsize) {
510 default:
511 case 1:
512 v = ldub_raw(buf + i);
513 break;
514 case 2:
515 v = lduw_raw(buf + i);
516 break;
517 case 4:
518 v = (uint32_t)ldl_raw(buf + i);
519 break;
520 case 8:
521 v = ldq_raw(buf + i);
522 break;
524 term_printf(" ");
525 switch(format) {
526 case 'o':
527 term_printf("%#*llo", max_digits, v);
528 break;
529 case 'x':
530 term_printf("0x%0*llx", max_digits, v);
531 break;
532 case 'u':
533 term_printf("%*llu", max_digits, v);
534 break;
535 case 'd':
536 term_printf("%*lld", max_digits, v);
537 break;
538 case 'c':
539 term_printc(v);
540 break;
542 i += wsize;
544 term_printf("\n");
545 addr += l;
546 len -= l;
550 #if TARGET_LONG_BITS == 64
551 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
552 #else
553 #define GET_TLONG(h, l) (l)
554 #endif
556 static void do_memory_dump(int count, int format, int size,
557 uint32_t addrh, uint32_t addrl)
559 target_long addr = GET_TLONG(addrh, addrl);
560 memory_dump(count, format, size, addr, 0);
563 static void do_physical_memory_dump(int count, int format, int size,
564 uint32_t addrh, uint32_t addrl)
567 target_long addr = GET_TLONG(addrh, addrl);
568 memory_dump(count, format, size, addr, 1);
571 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
573 target_long val = GET_TLONG(valh, vall);
574 #if TARGET_LONG_BITS == 32
575 switch(format) {
576 case 'o':
577 term_printf("%#o", val);
578 break;
579 case 'x':
580 term_printf("%#x", val);
581 break;
582 case 'u':
583 term_printf("%u", val);
584 break;
585 default:
586 case 'd':
587 term_printf("%d", val);
588 break;
589 case 'c':
590 term_printc(val);
591 break;
593 #else
594 switch(format) {
595 case 'o':
596 term_printf("%#llo", val);
597 break;
598 case 'x':
599 term_printf("%#llx", val);
600 break;
601 case 'u':
602 term_printf("%llu", val);
603 break;
604 default:
605 case 'd':
606 term_printf("%lld", val);
607 break;
608 case 'c':
609 term_printc(val);
610 break;
612 #endif
613 term_printf("\n");
616 static void do_sum(uint32_t start, uint32_t size)
618 uint32_t addr;
619 uint8_t buf[1];
620 uint16_t sum;
622 sum = 0;
623 for(addr = start; addr < (start + size); addr++) {
624 cpu_physical_memory_rw(addr, buf, 1, 0);
625 /* BSD sum algorithm ('sum' Unix command) */
626 sum = (sum >> 1) | (sum << 15);
627 sum += buf[0];
629 term_printf("%05d\n", sum);
632 typedef struct {
633 int keycode;
634 const char *name;
635 } KeyDef;
637 static const KeyDef key_defs[] = {
638 { 0x2a, "shift" },
639 { 0x36, "shift_r" },
641 { 0x38, "alt" },
642 { 0xb8, "alt_r" },
643 { 0x1d, "ctrl" },
644 { 0x9d, "ctrl_r" },
646 { 0xdd, "menu" },
648 { 0x01, "esc" },
650 { 0x02, "1" },
651 { 0x03, "2" },
652 { 0x04, "3" },
653 { 0x05, "4" },
654 { 0x06, "5" },
655 { 0x07, "6" },
656 { 0x08, "7" },
657 { 0x09, "8" },
658 { 0x0a, "9" },
659 { 0x0b, "0" },
660 { 0x0e, "backspace" },
662 { 0x0f, "tab" },
663 { 0x10, "q" },
664 { 0x11, "w" },
665 { 0x12, "e" },
666 { 0x13, "r" },
667 { 0x14, "t" },
668 { 0x15, "y" },
669 { 0x16, "u" },
670 { 0x17, "i" },
671 { 0x18, "o" },
672 { 0x19, "p" },
674 { 0x1c, "ret" },
676 { 0x1e, "a" },
677 { 0x1f, "s" },
678 { 0x20, "d" },
679 { 0x21, "f" },
680 { 0x22, "g" },
681 { 0x23, "h" },
682 { 0x24, "j" },
683 { 0x25, "k" },
684 { 0x26, "l" },
686 { 0x2c, "z" },
687 { 0x2d, "x" },
688 { 0x2e, "c" },
689 { 0x2f, "v" },
690 { 0x30, "b" },
691 { 0x31, "n" },
692 { 0x32, "m" },
694 { 0x39, "spc" },
695 { 0x3a, "caps_lock" },
696 { 0x3b, "f1" },
697 { 0x3c, "f2" },
698 { 0x3d, "f3" },
699 { 0x3e, "f4" },
700 { 0x3f, "f5" },
701 { 0x40, "f6" },
702 { 0x41, "f7" },
703 { 0x42, "f8" },
704 { 0x43, "f9" },
705 { 0x44, "f10" },
706 { 0x45, "num_lock" },
707 { 0x46, "scroll_lock" },
709 { 0x56, "<" },
711 { 0x57, "f11" },
712 { 0x58, "f12" },
714 { 0xb7, "print" },
716 { 0xc7, "home" },
717 { 0xc9, "pgup" },
718 { 0xd1, "pgdn" },
719 { 0xcf, "end" },
721 { 0xcb, "left" },
722 { 0xc8, "up" },
723 { 0xd0, "down" },
724 { 0xcd, "right" },
726 { 0xd2, "insert" },
727 { 0xd3, "delete" },
728 { 0, NULL },
731 static int get_keycode(const char *key)
733 const KeyDef *p;
735 for(p = key_defs; p->name != NULL; p++) {
736 if (!strcmp(key, p->name))
737 return p->keycode;
739 return -1;
742 static void do_send_key(const char *string)
744 char keybuf[16], *q;
745 uint8_t keycodes[16];
746 const char *p;
747 int nb_keycodes, keycode, i;
749 nb_keycodes = 0;
750 p = string;
751 while (*p != '\0') {
752 q = keybuf;
753 while (*p != '\0' && *p != '-') {
754 if ((q - keybuf) < sizeof(keybuf) - 1) {
755 *q++ = *p;
757 p++;
759 *q = '\0';
760 keycode = get_keycode(keybuf);
761 if (keycode < 0) {
762 term_printf("unknown key: '%s'\n", keybuf);
763 return;
765 keycodes[nb_keycodes++] = keycode;
766 if (*p == '\0')
767 break;
768 p++;
770 /* key down events */
771 for(i = 0; i < nb_keycodes; i++) {
772 keycode = keycodes[i];
773 if (keycode & 0x80)
774 kbd_put_keycode(0xe0);
775 kbd_put_keycode(keycode & 0x7f);
777 /* key up events */
778 for(i = nb_keycodes - 1; i >= 0; i--) {
779 keycode = keycodes[i];
780 if (keycode & 0x80)
781 kbd_put_keycode(0xe0);
782 kbd_put_keycode(keycode | 0x80);
786 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
788 uint32_t val;
789 int suffix;
791 if (has_index) {
792 cpu_outb(NULL, addr & 0xffff, index & 0xff);
793 addr++;
795 addr &= 0xffff;
797 switch(size) {
798 default:
799 case 1:
800 val = cpu_inb(NULL, addr);
801 suffix = 'b';
802 break;
803 case 2:
804 val = cpu_inw(NULL, addr);
805 suffix = 'w';
806 break;
807 case 4:
808 val = cpu_inl(NULL, addr);
809 suffix = 'l';
810 break;
812 term_printf("port%c[0x%04x] = %#0*x\n",
813 suffix, addr, size * 2, val);
816 static void do_system_reset(void)
818 qemu_system_reset_request();
821 static void do_system_powerdown(void)
823 qemu_system_powerdown_request();
826 #if defined(TARGET_I386)
827 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
829 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
830 addr,
831 pte & mask,
832 pte & PG_GLOBAL_MASK ? 'G' : '-',
833 pte & PG_PSE_MASK ? 'P' : '-',
834 pte & PG_DIRTY_MASK ? 'D' : '-',
835 pte & PG_ACCESSED_MASK ? 'A' : '-',
836 pte & PG_PCD_MASK ? 'C' : '-',
837 pte & PG_PWT_MASK ? 'T' : '-',
838 pte & PG_USER_MASK ? 'U' : '-',
839 pte & PG_RW_MASK ? 'W' : '-');
842 static void tlb_info(void)
844 CPUState *env;
845 int l1, l2;
846 uint32_t pgd, pde, pte;
848 env = mon_get_cpu();
849 if (!env)
850 return;
852 if (!(env->cr[0] & CR0_PG_MASK)) {
853 term_printf("PG disabled\n");
854 return;
856 pgd = env->cr[3] & ~0xfff;
857 for(l1 = 0; l1 < 1024; l1++) {
858 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
859 pde = le32_to_cpu(pde);
860 if (pde & PG_PRESENT_MASK) {
861 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
862 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
863 } else {
864 for(l2 = 0; l2 < 1024; l2++) {
865 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
866 (uint8_t *)&pte, 4);
867 pte = le32_to_cpu(pte);
868 if (pte & PG_PRESENT_MASK) {
869 print_pte((l1 << 22) + (l2 << 12),
870 pte & ~PG_PSE_MASK,
871 ~0xfff);
879 static void mem_print(uint32_t *pstart, int *plast_prot,
880 uint32_t end, int prot)
882 int prot1;
883 prot1 = *plast_prot;
884 if (prot != prot1) {
885 if (*pstart != -1) {
886 term_printf("%08x-%08x %08x %c%c%c\n",
887 *pstart, end, end - *pstart,
888 prot1 & PG_USER_MASK ? 'u' : '-',
889 'r',
890 prot1 & PG_RW_MASK ? 'w' : '-');
892 if (prot != 0)
893 *pstart = end;
894 else
895 *pstart = -1;
896 *plast_prot = prot;
900 static void mem_info(void)
902 CPUState *env;
903 int l1, l2, prot, last_prot;
904 uint32_t pgd, pde, pte, start, end;
906 env = mon_get_cpu();
907 if (!env)
908 return;
910 if (!(env->cr[0] & CR0_PG_MASK)) {
911 term_printf("PG disabled\n");
912 return;
914 pgd = env->cr[3] & ~0xfff;
915 last_prot = 0;
916 start = -1;
917 for(l1 = 0; l1 < 1024; l1++) {
918 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
919 pde = le32_to_cpu(pde);
920 end = l1 << 22;
921 if (pde & PG_PRESENT_MASK) {
922 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
923 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
924 mem_print(&start, &last_prot, end, prot);
925 } else {
926 for(l2 = 0; l2 < 1024; l2++) {
927 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
928 (uint8_t *)&pte, 4);
929 pte = le32_to_cpu(pte);
930 end = (l1 << 22) + (l2 << 12);
931 if (pte & PG_PRESENT_MASK) {
932 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
933 } else {
934 prot = 0;
936 mem_print(&start, &last_prot, end, prot);
939 } else {
940 prot = 0;
941 mem_print(&start, &last_prot, end, prot);
945 #endif
947 static void do_info_kqemu(void)
949 #ifdef USE_KQEMU
950 CPUState *env;
951 int val;
952 val = 0;
953 env = mon_get_cpu();
954 if (!env) {
955 term_printf("No cpu initialized yet");
956 return;
958 val = env->kqemu_enabled;
959 term_printf("kqemu is %s\n", val ? "enabled" : "disabled");
960 #else
961 term_printf("kqemu support is not compiled\n");
962 #endif
965 static term_cmd_t term_cmds[] = {
966 { "help|?", "s?", do_help,
967 "[cmd]", "show the help" },
968 { "commit", "", do_commit,
969 "", "commit changes to the disk images (if -snapshot is used)" },
970 { "info", "s?", do_info,
971 "subcommand", "show various information about the system state" },
972 { "q|quit", "", do_quit,
973 "", "quit the emulator" },
974 { "eject", "-fB", do_eject,
975 "[-f] device", "eject a removable media (use -f to force it)" },
976 { "change", "BF", do_change,
977 "device filename", "change a removable media" },
978 { "screendump", "F", do_screen_dump,
979 "filename", "save screen into PPM image 'filename'" },
980 { "log", "s", do_log,
981 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
982 { "savevm", "F", do_savevm,
983 "filename", "save the whole virtual machine state to 'filename'" },
984 { "loadvm", "F", do_loadvm,
985 "filename", "restore the whole virtual machine state from 'filename'" },
986 { "stop", "", do_stop,
987 "", "stop emulation", },
988 { "c|cont", "", do_cont,
989 "", "resume emulation", },
990 #ifdef CONFIG_GDBSTUB
991 { "gdbserver", "i?", do_gdbserver,
992 "[port]", "start gdbserver session (default port=1234)", },
993 #endif
994 { "x", "/l", do_memory_dump,
995 "/fmt addr", "virtual memory dump starting at 'addr'", },
996 { "xp", "/l", do_physical_memory_dump,
997 "/fmt addr", "physical memory dump starting at 'addr'", },
998 { "p|print", "/l", do_print,
999 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1000 { "i", "/ii.", do_ioport_read,
1001 "/fmt addr", "I/O port read" },
1003 { "sendkey", "s", do_send_key,
1004 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1005 { "system_reset", "", do_system_reset,
1006 "", "reset the system" },
1007 { "system_powerdown", "", do_system_powerdown,
1008 "", "send system power down event" },
1009 { "sum", "ii", do_sum,
1010 "addr size", "compute the checksum of a memory region" },
1011 { "usb_add", "s", do_usb_add,
1012 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1013 { "usb_del", "s", do_usb_del,
1014 "device", "remove USB device 'bus.addr'" },
1015 { "cpu", "i", do_cpu_set,
1016 "index", "set the default CPU" },
1017 { NULL, NULL, },
1020 static term_cmd_t info_cmds[] = {
1021 { "version", "", do_info_version,
1022 "", "show the version of qemu" },
1023 { "network", "", do_info_network,
1024 "", "show the network state" },
1025 { "block", "", do_info_block,
1026 "", "show the block devices" },
1027 { "registers", "", do_info_registers,
1028 "", "show the cpu registers" },
1029 { "cpus", "", do_info_cpus,
1030 "", "show infos for each CPU" },
1031 { "history", "", do_info_history,
1032 "", "show the command line history", },
1033 { "irq", "", irq_info,
1034 "", "show the interrupts statistics (if available)", },
1035 { "pic", "", pic_info,
1036 "", "show i8259 (PIC) state", },
1037 { "pci", "", pci_info,
1038 "", "show PCI info", },
1039 #if defined(TARGET_I386)
1040 { "tlb", "", tlb_info,
1041 "", "show virtual to physical memory mappings", },
1042 { "mem", "", mem_info,
1043 "", "show the active virtual memory mappings", },
1044 #endif
1045 { "jit", "", do_info_jit,
1046 "", "show dynamic compiler info", },
1047 { "kqemu", "", do_info_kqemu,
1048 "", "show kqemu information", },
1049 { "usb", "", usb_info,
1050 "", "show guest USB devices", },
1051 { "usbhost", "", usb_host_info,
1052 "", "show host USB devices", },
1053 { NULL, NULL, },
1056 /*******************************************************************/
1058 static const char *pch;
1059 static jmp_buf expr_env;
1061 #define MD_TLONG 0
1062 #define MD_I32 1
1064 typedef struct MonitorDef {
1065 const char *name;
1066 int offset;
1067 target_long (*get_value)(struct MonitorDef *md, int val);
1068 int type;
1069 } MonitorDef;
1071 #if defined(TARGET_I386)
1072 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1074 CPUState *env = mon_get_cpu();
1075 if (!env)
1076 return 0;
1077 return env->eip + env->segs[R_CS].base;
1079 #endif
1081 #if defined(TARGET_PPC)
1082 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1084 CPUState *env = mon_get_cpu();
1085 unsigned int u;
1086 int i;
1088 if (!env)
1089 return 0;
1091 u = 0;
1092 for (i = 0; i < 8; i++)
1093 u |= env->crf[i] << (32 - (4 * i));
1095 return u;
1098 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1100 CPUState *env = mon_get_cpu();
1101 if (!env)
1102 return 0;
1103 return (env->msr[MSR_POW] << MSR_POW) |
1104 (env->msr[MSR_ILE] << MSR_ILE) |
1105 (env->msr[MSR_EE] << MSR_EE) |
1106 (env->msr[MSR_PR] << MSR_PR) |
1107 (env->msr[MSR_FP] << MSR_FP) |
1108 (env->msr[MSR_ME] << MSR_ME) |
1109 (env->msr[MSR_FE0] << MSR_FE0) |
1110 (env->msr[MSR_SE] << MSR_SE) |
1111 (env->msr[MSR_BE] << MSR_BE) |
1112 (env->msr[MSR_FE1] << MSR_FE1) |
1113 (env->msr[MSR_IP] << MSR_IP) |
1114 (env->msr[MSR_IR] << MSR_IR) |
1115 (env->msr[MSR_DR] << MSR_DR) |
1116 (env->msr[MSR_RI] << MSR_RI) |
1117 (env->msr[MSR_LE] << MSR_LE);
1120 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1122 CPUState *env = mon_get_cpu();
1123 if (!env)
1124 return 0;
1125 return (env->xer[XER_SO] << XER_SO) |
1126 (env->xer[XER_OV] << XER_OV) |
1127 (env->xer[XER_CA] << XER_CA) |
1128 (env->xer[XER_BC] << XER_BC);
1131 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1133 CPUState *env = mon_get_cpu();
1134 if (!env)
1135 return 0;
1136 return cpu_ppc_load_decr(env);
1139 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1141 CPUState *env = mon_get_cpu();
1142 if (!env)
1143 return 0;
1144 return cpu_ppc_load_tbu(env);
1147 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1149 CPUState *env = mon_get_cpu();
1150 if (!env)
1151 return 0;
1152 return cpu_ppc_load_tbl(env);
1154 #endif
1156 #if defined(TARGET_SPARC)
1157 #ifndef TARGET_SPARC64
1158 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1160 CPUState *env = mon_get_cpu();
1161 if (!env)
1162 return 0;
1163 return GET_PSR(env);
1165 #endif
1167 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1169 CPUState *env = mon_get_cpu();
1170 if (!env)
1171 return 0;
1172 return env->regwptr[val];
1174 #endif
1176 static MonitorDef monitor_defs[] = {
1177 #ifdef TARGET_I386
1179 #define SEG(name, seg) \
1180 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1181 { name ".base", offsetof(CPUState, segs[seg].base) },\
1182 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1184 { "eax", offsetof(CPUState, regs[0]) },
1185 { "ecx", offsetof(CPUState, regs[1]) },
1186 { "edx", offsetof(CPUState, regs[2]) },
1187 { "ebx", offsetof(CPUState, regs[3]) },
1188 { "esp|sp", offsetof(CPUState, regs[4]) },
1189 { "ebp|fp", offsetof(CPUState, regs[5]) },
1190 { "esi", offsetof(CPUState, regs[6]) },
1191 { "edi", offsetof(CPUState, regs[7]) },
1192 #ifdef TARGET_X86_64
1193 { "r8", offsetof(CPUState, regs[8]) },
1194 { "r9", offsetof(CPUState, regs[9]) },
1195 { "r10", offsetof(CPUState, regs[10]) },
1196 { "r11", offsetof(CPUState, regs[11]) },
1197 { "r12", offsetof(CPUState, regs[12]) },
1198 { "r13", offsetof(CPUState, regs[13]) },
1199 { "r14", offsetof(CPUState, regs[14]) },
1200 { "r15", offsetof(CPUState, regs[15]) },
1201 #endif
1202 { "eflags", offsetof(CPUState, eflags) },
1203 { "eip", offsetof(CPUState, eip) },
1204 SEG("cs", R_CS)
1205 SEG("ds", R_DS)
1206 SEG("es", R_ES)
1207 SEG("ss", R_SS)
1208 SEG("fs", R_FS)
1209 SEG("gs", R_GS)
1210 { "pc", 0, monitor_get_pc, },
1211 #elif defined(TARGET_PPC)
1212 { "r0", offsetof(CPUState, gpr[0]) },
1213 { "r1", offsetof(CPUState, gpr[1]) },
1214 { "r2", offsetof(CPUState, gpr[2]) },
1215 { "r3", offsetof(CPUState, gpr[3]) },
1216 { "r4", offsetof(CPUState, gpr[4]) },
1217 { "r5", offsetof(CPUState, gpr[5]) },
1218 { "r6", offsetof(CPUState, gpr[6]) },
1219 { "r7", offsetof(CPUState, gpr[7]) },
1220 { "r8", offsetof(CPUState, gpr[8]) },
1221 { "r9", offsetof(CPUState, gpr[9]) },
1222 { "r10", offsetof(CPUState, gpr[10]) },
1223 { "r11", offsetof(CPUState, gpr[11]) },
1224 { "r12", offsetof(CPUState, gpr[12]) },
1225 { "r13", offsetof(CPUState, gpr[13]) },
1226 { "r14", offsetof(CPUState, gpr[14]) },
1227 { "r15", offsetof(CPUState, gpr[15]) },
1228 { "r16", offsetof(CPUState, gpr[16]) },
1229 { "r17", offsetof(CPUState, gpr[17]) },
1230 { "r18", offsetof(CPUState, gpr[18]) },
1231 { "r19", offsetof(CPUState, gpr[19]) },
1232 { "r20", offsetof(CPUState, gpr[20]) },
1233 { "r21", offsetof(CPUState, gpr[21]) },
1234 { "r22", offsetof(CPUState, gpr[22]) },
1235 { "r23", offsetof(CPUState, gpr[23]) },
1236 { "r24", offsetof(CPUState, gpr[24]) },
1237 { "r25", offsetof(CPUState, gpr[25]) },
1238 { "r26", offsetof(CPUState, gpr[26]) },
1239 { "r27", offsetof(CPUState, gpr[27]) },
1240 { "r28", offsetof(CPUState, gpr[28]) },
1241 { "r29", offsetof(CPUState, gpr[29]) },
1242 { "r30", offsetof(CPUState, gpr[30]) },
1243 { "r31", offsetof(CPUState, gpr[31]) },
1244 { "nip|pc", offsetof(CPUState, nip) },
1245 { "lr", offsetof(CPUState, lr) },
1246 { "ctr", offsetof(CPUState, ctr) },
1247 { "decr", 0, &monitor_get_decr, },
1248 { "ccr", 0, &monitor_get_ccr, },
1249 { "msr", 0, &monitor_get_msr, },
1250 { "xer", 0, &monitor_get_xer, },
1251 { "tbu", 0, &monitor_get_tbu, },
1252 { "tbl", 0, &monitor_get_tbl, },
1253 { "sdr1", offsetof(CPUState, sdr1) },
1254 { "sr0", offsetof(CPUState, sr[0]) },
1255 { "sr1", offsetof(CPUState, sr[1]) },
1256 { "sr2", offsetof(CPUState, sr[2]) },
1257 { "sr3", offsetof(CPUState, sr[3]) },
1258 { "sr4", offsetof(CPUState, sr[4]) },
1259 { "sr5", offsetof(CPUState, sr[5]) },
1260 { "sr6", offsetof(CPUState, sr[6]) },
1261 { "sr7", offsetof(CPUState, sr[7]) },
1262 { "sr8", offsetof(CPUState, sr[8]) },
1263 { "sr9", offsetof(CPUState, sr[9]) },
1264 { "sr10", offsetof(CPUState, sr[10]) },
1265 { "sr11", offsetof(CPUState, sr[11]) },
1266 { "sr12", offsetof(CPUState, sr[12]) },
1267 { "sr13", offsetof(CPUState, sr[13]) },
1268 { "sr14", offsetof(CPUState, sr[14]) },
1269 { "sr15", offsetof(CPUState, sr[15]) },
1270 /* Too lazy to put BATs and SPRs ... */
1271 #elif defined(TARGET_SPARC)
1272 { "g0", offsetof(CPUState, gregs[0]) },
1273 { "g1", offsetof(CPUState, gregs[1]) },
1274 { "g2", offsetof(CPUState, gregs[2]) },
1275 { "g3", offsetof(CPUState, gregs[3]) },
1276 { "g4", offsetof(CPUState, gregs[4]) },
1277 { "g5", offsetof(CPUState, gregs[5]) },
1278 { "g6", offsetof(CPUState, gregs[6]) },
1279 { "g7", offsetof(CPUState, gregs[7]) },
1280 { "o0", 0, monitor_get_reg },
1281 { "o1", 1, monitor_get_reg },
1282 { "o2", 2, monitor_get_reg },
1283 { "o3", 3, monitor_get_reg },
1284 { "o4", 4, monitor_get_reg },
1285 { "o5", 5, monitor_get_reg },
1286 { "o6", 6, monitor_get_reg },
1287 { "o7", 7, monitor_get_reg },
1288 { "l0", 8, monitor_get_reg },
1289 { "l1", 9, monitor_get_reg },
1290 { "l2", 10, monitor_get_reg },
1291 { "l3", 11, monitor_get_reg },
1292 { "l4", 12, monitor_get_reg },
1293 { "l5", 13, monitor_get_reg },
1294 { "l6", 14, monitor_get_reg },
1295 { "l7", 15, monitor_get_reg },
1296 { "i0", 16, monitor_get_reg },
1297 { "i1", 17, monitor_get_reg },
1298 { "i2", 18, monitor_get_reg },
1299 { "i3", 19, monitor_get_reg },
1300 { "i4", 20, monitor_get_reg },
1301 { "i5", 21, monitor_get_reg },
1302 { "i6", 22, monitor_get_reg },
1303 { "i7", 23, monitor_get_reg },
1304 { "pc", offsetof(CPUState, pc) },
1305 { "npc", offsetof(CPUState, npc) },
1306 { "y", offsetof(CPUState, y) },
1307 #ifndef TARGET_SPARC64
1308 { "psr", 0, &monitor_get_psr, },
1309 { "wim", offsetof(CPUState, wim) },
1310 #endif
1311 { "tbr", offsetof(CPUState, tbr) },
1312 { "fsr", offsetof(CPUState, fsr) },
1313 { "f0", offsetof(CPUState, fpr[0]) },
1314 { "f1", offsetof(CPUState, fpr[1]) },
1315 { "f2", offsetof(CPUState, fpr[2]) },
1316 { "f3", offsetof(CPUState, fpr[3]) },
1317 { "f4", offsetof(CPUState, fpr[4]) },
1318 { "f5", offsetof(CPUState, fpr[5]) },
1319 { "f6", offsetof(CPUState, fpr[6]) },
1320 { "f7", offsetof(CPUState, fpr[7]) },
1321 { "f8", offsetof(CPUState, fpr[8]) },
1322 { "f9", offsetof(CPUState, fpr[9]) },
1323 { "f10", offsetof(CPUState, fpr[10]) },
1324 { "f11", offsetof(CPUState, fpr[11]) },
1325 { "f12", offsetof(CPUState, fpr[12]) },
1326 { "f13", offsetof(CPUState, fpr[13]) },
1327 { "f14", offsetof(CPUState, fpr[14]) },
1328 { "f15", offsetof(CPUState, fpr[15]) },
1329 { "f16", offsetof(CPUState, fpr[16]) },
1330 { "f17", offsetof(CPUState, fpr[17]) },
1331 { "f18", offsetof(CPUState, fpr[18]) },
1332 { "f19", offsetof(CPUState, fpr[19]) },
1333 { "f20", offsetof(CPUState, fpr[20]) },
1334 { "f21", offsetof(CPUState, fpr[21]) },
1335 { "f22", offsetof(CPUState, fpr[22]) },
1336 { "f23", offsetof(CPUState, fpr[23]) },
1337 { "f24", offsetof(CPUState, fpr[24]) },
1338 { "f25", offsetof(CPUState, fpr[25]) },
1339 { "f26", offsetof(CPUState, fpr[26]) },
1340 { "f27", offsetof(CPUState, fpr[27]) },
1341 { "f28", offsetof(CPUState, fpr[28]) },
1342 { "f29", offsetof(CPUState, fpr[29]) },
1343 { "f30", offsetof(CPUState, fpr[30]) },
1344 { "f31", offsetof(CPUState, fpr[31]) },
1345 #ifdef TARGET_SPARC64
1346 { "f32", offsetof(CPUState, fpr[32]) },
1347 { "f34", offsetof(CPUState, fpr[34]) },
1348 { "f36", offsetof(CPUState, fpr[36]) },
1349 { "f38", offsetof(CPUState, fpr[38]) },
1350 { "f40", offsetof(CPUState, fpr[40]) },
1351 { "f42", offsetof(CPUState, fpr[42]) },
1352 { "f44", offsetof(CPUState, fpr[44]) },
1353 { "f46", offsetof(CPUState, fpr[46]) },
1354 { "f48", offsetof(CPUState, fpr[48]) },
1355 { "f50", offsetof(CPUState, fpr[50]) },
1356 { "f52", offsetof(CPUState, fpr[52]) },
1357 { "f54", offsetof(CPUState, fpr[54]) },
1358 { "f56", offsetof(CPUState, fpr[56]) },
1359 { "f58", offsetof(CPUState, fpr[58]) },
1360 { "f60", offsetof(CPUState, fpr[60]) },
1361 { "f62", offsetof(CPUState, fpr[62]) },
1362 { "asi", offsetof(CPUState, asi) },
1363 { "pstate", offsetof(CPUState, pstate) },
1364 { "cansave", offsetof(CPUState, cansave) },
1365 { "canrestore", offsetof(CPUState, canrestore) },
1366 { "otherwin", offsetof(CPUState, otherwin) },
1367 { "wstate", offsetof(CPUState, wstate) },
1368 { "cleanwin", offsetof(CPUState, cleanwin) },
1369 { "fprs", offsetof(CPUState, fprs) },
1370 #endif
1371 #endif
1372 { NULL },
1375 static void expr_error(const char *fmt)
1377 term_printf(fmt);
1378 term_printf("\n");
1379 longjmp(expr_env, 1);
1382 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1383 static int get_monitor_def(target_long *pval, const char *name)
1385 MonitorDef *md;
1386 void *ptr;
1388 for(md = monitor_defs; md->name != NULL; md++) {
1389 if (compare_cmd(name, md->name)) {
1390 if (md->get_value) {
1391 *pval = md->get_value(md, md->offset);
1392 } else {
1393 CPUState *env = mon_get_cpu();
1394 if (!env)
1395 return -2;
1396 ptr = (uint8_t *)env + md->offset;
1397 switch(md->type) {
1398 case MD_I32:
1399 *pval = *(int32_t *)ptr;
1400 break;
1401 case MD_TLONG:
1402 *pval = *(target_long *)ptr;
1403 break;
1404 default:
1405 *pval = 0;
1406 break;
1409 return 0;
1412 return -1;
1415 static void next(void)
1417 if (pch != '\0') {
1418 pch++;
1419 while (isspace(*pch))
1420 pch++;
1424 static target_long expr_sum(void);
1426 static target_long expr_unary(void)
1428 target_long n;
1429 char *p;
1430 int ret;
1432 switch(*pch) {
1433 case '+':
1434 next();
1435 n = expr_unary();
1436 break;
1437 case '-':
1438 next();
1439 n = -expr_unary();
1440 break;
1441 case '~':
1442 next();
1443 n = ~expr_unary();
1444 break;
1445 case '(':
1446 next();
1447 n = expr_sum();
1448 if (*pch != ')') {
1449 expr_error("')' expected");
1451 next();
1452 break;
1453 case '\'':
1454 pch++;
1455 if (*pch == '\0')
1456 expr_error("character constant expected");
1457 n = *pch;
1458 pch++;
1459 if (*pch != '\'')
1460 expr_error("missing terminating \' character");
1461 next();
1462 break;
1463 case '$':
1465 char buf[128], *q;
1467 pch++;
1468 q = buf;
1469 while ((*pch >= 'a' && *pch <= 'z') ||
1470 (*pch >= 'A' && *pch <= 'Z') ||
1471 (*pch >= '0' && *pch <= '9') ||
1472 *pch == '_' || *pch == '.') {
1473 if ((q - buf) < sizeof(buf) - 1)
1474 *q++ = *pch;
1475 pch++;
1477 while (isspace(*pch))
1478 pch++;
1479 *q = 0;
1480 ret = get_monitor_def(&n, buf);
1481 if (ret == -1)
1482 expr_error("unknown register");
1483 else if (ret == -2)
1484 expr_error("no cpu defined");
1486 break;
1487 case '\0':
1488 expr_error("unexpected end of expression");
1489 n = 0;
1490 break;
1491 default:
1492 n = strtoul(pch, &p, 0);
1493 if (pch == p) {
1494 expr_error("invalid char in expression");
1496 pch = p;
1497 while (isspace(*pch))
1498 pch++;
1499 break;
1501 return n;
1505 static target_long expr_prod(void)
1507 target_long val, val2;
1508 int op;
1510 val = expr_unary();
1511 for(;;) {
1512 op = *pch;
1513 if (op != '*' && op != '/' && op != '%')
1514 break;
1515 next();
1516 val2 = expr_unary();
1517 switch(op) {
1518 default:
1519 case '*':
1520 val *= val2;
1521 break;
1522 case '/':
1523 case '%':
1524 if (val2 == 0)
1525 expr_error("division by zero");
1526 if (op == '/')
1527 val /= val2;
1528 else
1529 val %= val2;
1530 break;
1533 return val;
1536 static target_long expr_logic(void)
1538 target_long val, val2;
1539 int op;
1541 val = expr_prod();
1542 for(;;) {
1543 op = *pch;
1544 if (op != '&' && op != '|' && op != '^')
1545 break;
1546 next();
1547 val2 = expr_prod();
1548 switch(op) {
1549 default:
1550 case '&':
1551 val &= val2;
1552 break;
1553 case '|':
1554 val |= val2;
1555 break;
1556 case '^':
1557 val ^= val2;
1558 break;
1561 return val;
1564 static target_long expr_sum(void)
1566 target_long val, val2;
1567 int op;
1569 val = expr_logic();
1570 for(;;) {
1571 op = *pch;
1572 if (op != '+' && op != '-')
1573 break;
1574 next();
1575 val2 = expr_logic();
1576 if (op == '+')
1577 val += val2;
1578 else
1579 val -= val2;
1581 return val;
1584 static int get_expr(target_long *pval, const char **pp)
1586 pch = *pp;
1587 if (setjmp(expr_env)) {
1588 *pp = pch;
1589 return -1;
1591 while (isspace(*pch))
1592 pch++;
1593 *pval = expr_sum();
1594 *pp = pch;
1595 return 0;
1598 static int get_str(char *buf, int buf_size, const char **pp)
1600 const char *p;
1601 char *q;
1602 int c;
1604 q = buf;
1605 p = *pp;
1606 while (isspace(*p))
1607 p++;
1608 if (*p == '\0') {
1609 fail:
1610 *q = '\0';
1611 *pp = p;
1612 return -1;
1614 if (*p == '\"') {
1615 p++;
1616 while (*p != '\0' && *p != '\"') {
1617 if (*p == '\\') {
1618 p++;
1619 c = *p++;
1620 switch(c) {
1621 case 'n':
1622 c = '\n';
1623 break;
1624 case 'r':
1625 c = '\r';
1626 break;
1627 case '\\':
1628 case '\'':
1629 case '\"':
1630 break;
1631 default:
1632 qemu_printf("unsupported escape code: '\\%c'\n", c);
1633 goto fail;
1635 if ((q - buf) < buf_size - 1) {
1636 *q++ = c;
1638 } else {
1639 if ((q - buf) < buf_size - 1) {
1640 *q++ = *p;
1642 p++;
1645 if (*p != '\"') {
1646 qemu_printf("unterminated string\n");
1647 goto fail;
1649 p++;
1650 } else {
1651 while (*p != '\0' && !isspace(*p)) {
1652 if ((q - buf) < buf_size - 1) {
1653 *q++ = *p;
1655 p++;
1658 *q = '\0';
1659 *pp = p;
1660 return 0;
1663 static int default_fmt_format = 'x';
1664 static int default_fmt_size = 4;
1666 #define MAX_ARGS 16
1668 static void monitor_handle_command(const char *cmdline)
1670 const char *p, *pstart, *typestr;
1671 char *q;
1672 int c, nb_args, len, i, has_arg;
1673 term_cmd_t *cmd;
1674 char cmdname[256];
1675 char buf[1024];
1676 void *str_allocated[MAX_ARGS];
1677 void *args[MAX_ARGS];
1679 #ifdef DEBUG
1680 term_printf("command='%s'\n", cmdline);
1681 #endif
1683 /* extract the command name */
1684 p = cmdline;
1685 q = cmdname;
1686 while (isspace(*p))
1687 p++;
1688 if (*p == '\0')
1689 return;
1690 pstart = p;
1691 while (*p != '\0' && *p != '/' && !isspace(*p))
1692 p++;
1693 len = p - pstart;
1694 if (len > sizeof(cmdname) - 1)
1695 len = sizeof(cmdname) - 1;
1696 memcpy(cmdname, pstart, len);
1697 cmdname[len] = '\0';
1699 /* find the command */
1700 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1701 if (compare_cmd(cmdname, cmd->name))
1702 goto found;
1704 term_printf("unknown command: '%s'\n", cmdname);
1705 return;
1706 found:
1708 for(i = 0; i < MAX_ARGS; i++)
1709 str_allocated[i] = NULL;
1711 /* parse the parameters */
1712 typestr = cmd->args_type;
1713 nb_args = 0;
1714 for(;;) {
1715 c = *typestr;
1716 if (c == '\0')
1717 break;
1718 typestr++;
1719 switch(c) {
1720 case 'F':
1721 case 'B':
1722 case 's':
1724 int ret;
1725 char *str;
1727 while (isspace(*p))
1728 p++;
1729 if (*typestr == '?') {
1730 typestr++;
1731 if (*p == '\0') {
1732 /* no optional string: NULL argument */
1733 str = NULL;
1734 goto add_str;
1737 ret = get_str(buf, sizeof(buf), &p);
1738 if (ret < 0) {
1739 switch(c) {
1740 case 'F':
1741 term_printf("%s: filename expected\n", cmdname);
1742 break;
1743 case 'B':
1744 term_printf("%s: block device name expected\n", cmdname);
1745 break;
1746 default:
1747 term_printf("%s: string expected\n", cmdname);
1748 break;
1750 goto fail;
1752 str = qemu_malloc(strlen(buf) + 1);
1753 strcpy(str, buf);
1754 str_allocated[nb_args] = str;
1755 add_str:
1756 if (nb_args >= MAX_ARGS) {
1757 error_args:
1758 term_printf("%s: too many arguments\n", cmdname);
1759 goto fail;
1761 args[nb_args++] = str;
1763 break;
1764 case '/':
1766 int count, format, size;
1768 while (isspace(*p))
1769 p++;
1770 if (*p == '/') {
1771 /* format found */
1772 p++;
1773 count = 1;
1774 if (isdigit(*p)) {
1775 count = 0;
1776 while (isdigit(*p)) {
1777 count = count * 10 + (*p - '0');
1778 p++;
1781 size = -1;
1782 format = -1;
1783 for(;;) {
1784 switch(*p) {
1785 case 'o':
1786 case 'd':
1787 case 'u':
1788 case 'x':
1789 case 'i':
1790 case 'c':
1791 format = *p++;
1792 break;
1793 case 'b':
1794 size = 1;
1795 p++;
1796 break;
1797 case 'h':
1798 size = 2;
1799 p++;
1800 break;
1801 case 'w':
1802 size = 4;
1803 p++;
1804 break;
1805 case 'g':
1806 case 'L':
1807 size = 8;
1808 p++;
1809 break;
1810 default:
1811 goto next;
1814 next:
1815 if (*p != '\0' && !isspace(*p)) {
1816 term_printf("invalid char in format: '%c'\n", *p);
1817 goto fail;
1819 if (format < 0)
1820 format = default_fmt_format;
1821 if (format != 'i') {
1822 /* for 'i', not specifying a size gives -1 as size */
1823 if (size < 0)
1824 size = default_fmt_size;
1826 default_fmt_size = size;
1827 default_fmt_format = format;
1828 } else {
1829 count = 1;
1830 format = default_fmt_format;
1831 if (format != 'i') {
1832 size = default_fmt_size;
1833 } else {
1834 size = -1;
1837 if (nb_args + 3 > MAX_ARGS)
1838 goto error_args;
1839 args[nb_args++] = (void*)count;
1840 args[nb_args++] = (void*)format;
1841 args[nb_args++] = (void*)size;
1843 break;
1844 case 'i':
1845 case 'l':
1847 target_long val;
1848 while (isspace(*p))
1849 p++;
1850 if (*typestr == '?' || *typestr == '.') {
1851 typestr++;
1852 if (*typestr == '?') {
1853 if (*p == '\0')
1854 has_arg = 0;
1855 else
1856 has_arg = 1;
1857 } else {
1858 if (*p == '.') {
1859 p++;
1860 while (isspace(*p))
1861 p++;
1862 has_arg = 1;
1863 } else {
1864 has_arg = 0;
1867 if (nb_args >= MAX_ARGS)
1868 goto error_args;
1869 args[nb_args++] = (void *)has_arg;
1870 if (!has_arg) {
1871 if (nb_args >= MAX_ARGS)
1872 goto error_args;
1873 val = -1;
1874 goto add_num;
1877 if (get_expr(&val, &p))
1878 goto fail;
1879 add_num:
1880 if (c == 'i') {
1881 if (nb_args >= MAX_ARGS)
1882 goto error_args;
1883 args[nb_args++] = (void *)(int)val;
1884 } else {
1885 if ((nb_args + 1) >= MAX_ARGS)
1886 goto error_args;
1887 #if TARGET_LONG_BITS == 64
1888 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1889 #else
1890 args[nb_args++] = (void *)0;
1891 #endif
1892 args[nb_args++] = (void *)(int)(val & 0xffffffff);
1895 break;
1896 case '-':
1898 int has_option;
1899 /* option */
1901 c = *typestr++;
1902 if (c == '\0')
1903 goto bad_type;
1904 while (isspace(*p))
1905 p++;
1906 has_option = 0;
1907 if (*p == '-') {
1908 p++;
1909 if (*p != c) {
1910 term_printf("%s: unsupported option -%c\n",
1911 cmdname, *p);
1912 goto fail;
1914 p++;
1915 has_option = 1;
1917 if (nb_args >= MAX_ARGS)
1918 goto error_args;
1919 args[nb_args++] = (void *)has_option;
1921 break;
1922 default:
1923 bad_type:
1924 term_printf("%s: unknown type '%c'\n", cmdname, c);
1925 goto fail;
1928 /* check that all arguments were parsed */
1929 while (isspace(*p))
1930 p++;
1931 if (*p != '\0') {
1932 term_printf("%s: extraneous characters at the end of line\n",
1933 cmdname);
1934 goto fail;
1937 switch(nb_args) {
1938 case 0:
1939 cmd->handler();
1940 break;
1941 case 1:
1942 cmd->handler(args[0]);
1943 break;
1944 case 2:
1945 cmd->handler(args[0], args[1]);
1946 break;
1947 case 3:
1948 cmd->handler(args[0], args[1], args[2]);
1949 break;
1950 case 4:
1951 cmd->handler(args[0], args[1], args[2], args[3]);
1952 break;
1953 case 5:
1954 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1955 break;
1956 case 6:
1957 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1958 break;
1959 default:
1960 term_printf("unsupported number of arguments: %d\n", nb_args);
1961 goto fail;
1963 fail:
1964 for(i = 0; i < MAX_ARGS; i++)
1965 qemu_free(str_allocated[i]);
1966 return;
1969 static void cmd_completion(const char *name, const char *list)
1971 const char *p, *pstart;
1972 char cmd[128];
1973 int len;
1975 p = list;
1976 for(;;) {
1977 pstart = p;
1978 p = strchr(p, '|');
1979 if (!p)
1980 p = pstart + strlen(pstart);
1981 len = p - pstart;
1982 if (len > sizeof(cmd) - 2)
1983 len = sizeof(cmd) - 2;
1984 memcpy(cmd, pstart, len);
1985 cmd[len] = '\0';
1986 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1987 add_completion(cmd);
1989 if (*p == '\0')
1990 break;
1991 p++;
1995 static void file_completion(const char *input)
1997 DIR *ffs;
1998 struct dirent *d;
1999 char path[1024];
2000 char file[1024], file_prefix[1024];
2001 int input_path_len;
2002 const char *p;
2004 p = strrchr(input, '/');
2005 if (!p) {
2006 input_path_len = 0;
2007 pstrcpy(file_prefix, sizeof(file_prefix), input);
2008 strcpy(path, ".");
2009 } else {
2010 input_path_len = p - input + 1;
2011 memcpy(path, input, input_path_len);
2012 if (input_path_len > sizeof(path) - 1)
2013 input_path_len = sizeof(path) - 1;
2014 path[input_path_len] = '\0';
2015 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2017 #ifdef DEBUG_COMPLETION
2018 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2019 #endif
2020 ffs = opendir(path);
2021 if (!ffs)
2022 return;
2023 for(;;) {
2024 struct stat sb;
2025 d = readdir(ffs);
2026 if (!d)
2027 break;
2028 if (strstart(d->d_name, file_prefix, NULL)) {
2029 memcpy(file, input, input_path_len);
2030 strcpy(file + input_path_len, d->d_name);
2031 /* stat the file to find out if it's a directory.
2032 * In that case add a slash to speed up typing long paths
2034 stat(file, &sb);
2035 if(S_ISDIR(sb.st_mode))
2036 strcat(file, "/");
2037 add_completion(file);
2040 closedir(ffs);
2043 static void block_completion_it(void *opaque, const char *name)
2045 const char *input = opaque;
2047 if (input[0] == '\0' ||
2048 !strncmp(name, (char *)input, strlen(input))) {
2049 add_completion(name);
2053 /* NOTE: this parser is an approximate form of the real command parser */
2054 static void parse_cmdline(const char *cmdline,
2055 int *pnb_args, char **args)
2057 const char *p;
2058 int nb_args, ret;
2059 char buf[1024];
2061 p = cmdline;
2062 nb_args = 0;
2063 for(;;) {
2064 while (isspace(*p))
2065 p++;
2066 if (*p == '\0')
2067 break;
2068 if (nb_args >= MAX_ARGS)
2069 break;
2070 ret = get_str(buf, sizeof(buf), &p);
2071 args[nb_args] = qemu_strdup(buf);
2072 nb_args++;
2073 if (ret < 0)
2074 break;
2076 *pnb_args = nb_args;
2079 void readline_find_completion(const char *cmdline)
2081 const char *cmdname;
2082 char *args[MAX_ARGS];
2083 int nb_args, i, len;
2084 const char *ptype, *str;
2085 term_cmd_t *cmd;
2087 parse_cmdline(cmdline, &nb_args, args);
2088 #ifdef DEBUG_COMPLETION
2089 for(i = 0; i < nb_args; i++) {
2090 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2092 #endif
2094 /* if the line ends with a space, it means we want to complete the
2095 next arg */
2096 len = strlen(cmdline);
2097 if (len > 0 && isspace(cmdline[len - 1])) {
2098 if (nb_args >= MAX_ARGS)
2099 return;
2100 args[nb_args++] = qemu_strdup("");
2102 if (nb_args <= 1) {
2103 /* command completion */
2104 if (nb_args == 0)
2105 cmdname = "";
2106 else
2107 cmdname = args[0];
2108 completion_index = strlen(cmdname);
2109 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2110 cmd_completion(cmdname, cmd->name);
2112 } else {
2113 /* find the command */
2114 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2115 if (compare_cmd(args[0], cmd->name))
2116 goto found;
2118 return;
2119 found:
2120 ptype = cmd->args_type;
2121 for(i = 0; i < nb_args - 2; i++) {
2122 if (*ptype != '\0') {
2123 ptype++;
2124 while (*ptype == '?')
2125 ptype++;
2128 str = args[nb_args - 1];
2129 switch(*ptype) {
2130 case 'F':
2131 /* file completion */
2132 completion_index = strlen(str);
2133 file_completion(str);
2134 break;
2135 case 'B':
2136 /* block device name completion */
2137 completion_index = strlen(str);
2138 bdrv_iterate(block_completion_it, (void *)str);
2139 break;
2140 case 's':
2141 /* XXX: more generic ? */
2142 if (!strcmp(cmd->name, "info")) {
2143 completion_index = strlen(str);
2144 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2145 cmd_completion(str, cmd->name);
2148 break;
2149 default:
2150 break;
2153 for(i = 0; i < nb_args; i++)
2154 qemu_free(args[i]);
2157 static int term_can_read(void *opaque)
2159 return 128;
2162 static void term_read(void *opaque, const uint8_t *buf, int size)
2164 int i;
2165 for(i = 0; i < size; i++)
2166 readline_handle_byte(buf[i]);
2169 static void monitor_start_input(void);
2171 static void monitor_handle_command1(void *opaque, const char *cmdline)
2173 monitor_handle_command(cmdline);
2174 monitor_start_input();
2177 static void monitor_start_input(void)
2179 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2182 void monitor_init(CharDriverState *hd, int show_banner)
2184 monitor_hd = hd;
2185 if (show_banner) {
2186 term_printf("QEMU %s monitor - type 'help' for more information\n",
2187 QEMU_VERSION);
2189 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2190 monitor_start_input();
2193 /* XXX: use threads ? */
2194 /* modal monitor readline */
2195 static int monitor_readline_started;
2196 static char *monitor_readline_buf;
2197 static int monitor_readline_buf_size;
2199 static void monitor_readline_cb(void *opaque, const char *input)
2201 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2202 monitor_readline_started = 0;
2205 void monitor_readline(const char *prompt, int is_password,
2206 char *buf, int buf_size)
2208 if (is_password) {
2209 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2211 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2212 monitor_readline_buf = buf;
2213 monitor_readline_buf_size = buf_size;
2214 monitor_readline_started = 1;
2215 while (monitor_readline_started) {
2216 main_loop_wait(10);