]> git.proxmox.com Git - qemu.git/blob - monitor.c
Fix dumping of arm registers (Paul Brook)
[qemu.git] / monitor.c
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24 #include "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
27
28 //#define DEBUG
29 //#define DEBUG_COMPLETION
30
31 #ifndef offsetof
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
33 #endif
34
35 /*
36 * Supported types:
37 *
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")
44 *
45 * '?' optional type (for 'F', 's' and 'i')
46 *
47 */
48
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;
56
57 static CharDriverState *monitor_hd;
58
59 static term_cmd_t term_cmds[];
60 static term_cmd_t info_cmds[];
61
62 static char term_outbuf[1024];
63 static int term_outbuf_index;
64
65 static void monitor_start_input(void);
66
67 void term_flush(void)
68 {
69 if (term_outbuf_index > 0) {
70 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
71 term_outbuf_index = 0;
72 }
73 }
74
75 /* flush at every end of line or if the buffer is full */
76 void term_puts(const char *str)
77 {
78 int c;
79 for(;;) {
80 c = *str++;
81 if (c == '\0')
82 break;
83 term_outbuf[term_outbuf_index++] = c;
84 if (term_outbuf_index >= sizeof(term_outbuf) ||
85 c == '\n')
86 term_flush();
87 }
88 }
89
90 void term_vprintf(const char *fmt, va_list ap)
91 {
92 char buf[4096];
93 vsnprintf(buf, sizeof(buf), fmt, ap);
94 term_puts(buf);
95 }
96
97 void term_printf(const char *fmt, ...)
98 {
99 va_list ap;
100 va_start(ap, fmt);
101 term_vprintf(fmt, ap);
102 va_end(ap);
103 }
104
105 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
106 {
107 va_list ap;
108 va_start(ap, fmt);
109 term_vprintf(fmt, ap);
110 va_end(ap);
111 return 0;
112 }
113
114 static int compare_cmd(const char *name, const char *list)
115 {
116 const char *p, *pstart;
117 int len;
118 len = strlen(name);
119 p = list;
120 for(;;) {
121 pstart = p;
122 p = strchr(p, '|');
123 if (!p)
124 p = pstart + strlen(pstart);
125 if ((p - pstart) == len && !memcmp(pstart, name, len))
126 return 1;
127 if (*p == '\0')
128 break;
129 p++;
130 }
131 return 0;
132 }
133
134 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
135 {
136 term_cmd_t *cmd;
137
138 for(cmd = cmds; cmd->name != NULL; cmd++) {
139 if (!name || !strcmp(name, cmd->name))
140 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
141 }
142 }
143
144 static void help_cmd(const char *name)
145 {
146 if (name && !strcmp(name, "info")) {
147 help_cmd1(info_cmds, "info ", NULL);
148 } else {
149 help_cmd1(term_cmds, "", name);
150 if (name && !strcmp(name, "log")) {
151 CPULogItem *item;
152 term_printf("Log items (comma separated):\n");
153 term_printf("%-10s %s\n", "none", "remove all logs");
154 for(item = cpu_log_items; item->mask != 0; item++) {
155 term_printf("%-10s %s\n", item->name, item->help);
156 }
157 }
158 }
159 }
160
161 static void do_help(const char *name)
162 {
163 help_cmd(name);
164 }
165
166 static void do_commit(void)
167 {
168 int i;
169
170 for (i = 0; i < MAX_DISKS; i++) {
171 if (bs_table[i]) {
172 bdrv_commit(bs_table[i]);
173 }
174 }
175 }
176
177 static void do_info(const char *item)
178 {
179 term_cmd_t *cmd;
180
181 if (!item)
182 goto help;
183 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
184 if (compare_cmd(item, cmd->name))
185 goto found;
186 }
187 help:
188 help_cmd("info");
189 return;
190 found:
191 cmd->handler();
192 }
193
194 static void do_info_version(void)
195 {
196 term_printf("%s\n", QEMU_VERSION);
197 }
198
199 static void do_info_network(void)
200 {
201 int i, j;
202 NetDriverState *nd;
203
204 for(i = 0; i < nb_nics; i++) {
205 nd = &nd_table[i];
206 term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
207 for(j = 0; j < 6; j++) {
208 if (j > 0)
209 term_printf(":");
210 term_printf("%02x", nd->macaddr[j]);
211 }
212 term_printf("\n");
213 }
214 }
215
216 static void do_info_block(void)
217 {
218 bdrv_info();
219 }
220
221 static void do_info_registers(void)
222 {
223 #ifdef TARGET_I386
224 cpu_dump_state(cpu_single_env, NULL, monitor_fprintf,
225 X86_DUMP_FPU | X86_DUMP_CCOP);
226 #else
227 cpu_dump_state(cpu_single_env, NULL, monitor_fprintf,
228 0);
229 #endif
230 }
231
232 static void do_info_jit(void)
233 {
234 dump_exec_info(NULL, monitor_fprintf);
235 }
236
237 static void do_info_history (void)
238 {
239 int i;
240 const char *str;
241
242 i = 0;
243 for(;;) {
244 str = readline_get_history(i);
245 if (!str)
246 break;
247 term_printf("%d: '%s'\n", i, str);
248 i++;
249 }
250 }
251
252 static void do_quit(void)
253 {
254 exit(0);
255 }
256
257 static int eject_device(BlockDriverState *bs, int force)
258 {
259 if (bdrv_is_inserted(bs)) {
260 if (!force) {
261 if (!bdrv_is_removable(bs)) {
262 term_printf("device is not removable\n");
263 return -1;
264 }
265 if (bdrv_is_locked(bs)) {
266 term_printf("device is locked\n");
267 return -1;
268 }
269 }
270 bdrv_close(bs);
271 }
272 return 0;
273 }
274
275 static void do_eject(int force, const char *filename)
276 {
277 BlockDriverState *bs;
278
279 bs = bdrv_find(filename);
280 if (!bs) {
281 term_printf("device not found\n");
282 return;
283 }
284 eject_device(bs, force);
285 }
286
287 static void do_change(const char *device, const char *filename)
288 {
289 BlockDriverState *bs;
290 int i;
291 char password[256];
292
293 bs = bdrv_find(device);
294 if (!bs) {
295 term_printf("device not found\n");
296 return;
297 }
298 if (eject_device(bs, 0) < 0)
299 return;
300 bdrv_open(bs, filename, 0);
301 if (bdrv_is_encrypted(bs)) {
302 term_printf("%s is encrypted.\n", device);
303 for(i = 0; i < 3; i++) {
304 monitor_readline("Password: ", 1, password, sizeof(password));
305 if (bdrv_set_key(bs, password) == 0)
306 break;
307 term_printf("invalid password\n");
308 }
309 }
310 }
311
312 static void do_screen_dump(const char *filename)
313 {
314 vga_screen_dump(filename);
315 }
316
317 static void do_log(const char *items)
318 {
319 int mask;
320
321 if (!strcmp(items, "none")) {
322 mask = 0;
323 } else {
324 mask = cpu_str_to_log_mask(items);
325 if (!mask) {
326 help_cmd("log");
327 return;
328 }
329 }
330 cpu_set_log(mask);
331 }
332
333 static void do_savevm(const char *filename)
334 {
335 if (qemu_savevm(filename) < 0)
336 term_printf("I/O error when saving VM to '%s'\n", filename);
337 }
338
339 static void do_loadvm(const char *filename)
340 {
341 if (qemu_loadvm(filename) < 0)
342 term_printf("I/O error when loading VM from '%s'\n", filename);
343 }
344
345 static void do_stop(void)
346 {
347 vm_stop(EXCP_INTERRUPT);
348 }
349
350 static void do_cont(void)
351 {
352 vm_start();
353 }
354
355 #ifdef CONFIG_GDBSTUB
356 static void do_gdbserver(int has_port, int port)
357 {
358 if (!has_port)
359 port = DEFAULT_GDBSTUB_PORT;
360 if (gdbserver_start(port) < 0) {
361 qemu_printf("Could not open gdbserver socket on port %d\n", port);
362 } else {
363 qemu_printf("Waiting gdb connection on port %d\n", port);
364 }
365 }
366 #endif
367
368 static void term_printc(int c)
369 {
370 term_printf("'");
371 switch(c) {
372 case '\'':
373 term_printf("\\'");
374 break;
375 case '\\':
376 term_printf("\\\\");
377 break;
378 case '\n':
379 term_printf("\\n");
380 break;
381 case '\r':
382 term_printf("\\r");
383 break;
384 default:
385 if (c >= 32 && c <= 126) {
386 term_printf("%c", c);
387 } else {
388 term_printf("\\x%02x", c);
389 }
390 break;
391 }
392 term_printf("'");
393 }
394
395 static void memory_dump(int count, int format, int wsize,
396 target_ulong addr, int is_physical)
397 {
398 int nb_per_line, l, line_size, i, max_digits, len;
399 uint8_t buf[16];
400 uint64_t v;
401
402 if (format == 'i') {
403 int flags;
404 flags = 0;
405 #ifdef TARGET_I386
406 if (wsize == 2) {
407 flags = 1;
408 } else if (wsize == 4) {
409 flags = 0;
410 } else {
411 /* as default we use the current CS size */
412 flags = 0;
413 if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
414 flags = 1;
415 }
416 #endif
417 monitor_disas(addr, count, is_physical, flags);
418 return;
419 }
420
421 len = wsize * count;
422 if (wsize == 1)
423 line_size = 8;
424 else
425 line_size = 16;
426 nb_per_line = line_size / wsize;
427 max_digits = 0;
428
429 switch(format) {
430 case 'o':
431 max_digits = (wsize * 8 + 2) / 3;
432 break;
433 default:
434 case 'x':
435 max_digits = (wsize * 8) / 4;
436 break;
437 case 'u':
438 case 'd':
439 max_digits = (wsize * 8 * 10 + 32) / 33;
440 break;
441 case 'c':
442 wsize = 1;
443 break;
444 }
445
446 while (len > 0) {
447 term_printf(TARGET_FMT_lx ":", addr);
448 l = len;
449 if (l > line_size)
450 l = line_size;
451 if (is_physical) {
452 cpu_physical_memory_rw(addr, buf, l, 0);
453 } else {
454 cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
455 }
456 i = 0;
457 while (i < l) {
458 switch(wsize) {
459 default:
460 case 1:
461 v = ldub_raw(buf + i);
462 break;
463 case 2:
464 v = lduw_raw(buf + i);
465 break;
466 case 4:
467 v = (uint32_t)ldl_raw(buf + i);
468 break;
469 case 8:
470 v = ldq_raw(buf + i);
471 break;
472 }
473 term_printf(" ");
474 switch(format) {
475 case 'o':
476 term_printf("%#*llo", max_digits, v);
477 break;
478 case 'x':
479 term_printf("0x%0*llx", max_digits, v);
480 break;
481 case 'u':
482 term_printf("%*llu", max_digits, v);
483 break;
484 case 'd':
485 term_printf("%*lld", max_digits, v);
486 break;
487 case 'c':
488 term_printc(v);
489 break;
490 }
491 i += wsize;
492 }
493 term_printf("\n");
494 addr += l;
495 len -= l;
496 }
497 }
498
499 #if TARGET_LONG_BITS == 64
500 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
501 #else
502 #define GET_TLONG(h, l) (l)
503 #endif
504
505 static void do_memory_dump(int count, int format, int size,
506 uint32_t addrh, uint32_t addrl)
507 {
508 target_long addr = GET_TLONG(addrh, addrl);
509 memory_dump(count, format, size, addr, 0);
510 }
511
512 static void do_physical_memory_dump(int count, int format, int size,
513 uint32_t addrh, uint32_t addrl)
514
515 {
516 target_long addr = GET_TLONG(addrh, addrl);
517 memory_dump(count, format, size, addr, 1);
518 }
519
520 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
521 {
522 target_long val = GET_TLONG(valh, vall);
523 #if TARGET_LONG_BITS == 32
524 switch(format) {
525 case 'o':
526 term_printf("%#o", val);
527 break;
528 case 'x':
529 term_printf("%#x", val);
530 break;
531 case 'u':
532 term_printf("%u", val);
533 break;
534 default:
535 case 'd':
536 term_printf("%d", val);
537 break;
538 case 'c':
539 term_printc(val);
540 break;
541 }
542 #else
543 switch(format) {
544 case 'o':
545 term_printf("%#llo", val);
546 break;
547 case 'x':
548 term_printf("%#llx", val);
549 break;
550 case 'u':
551 term_printf("%llu", val);
552 break;
553 default:
554 case 'd':
555 term_printf("%lld", val);
556 break;
557 case 'c':
558 term_printc(val);
559 break;
560 }
561 #endif
562 term_printf("\n");
563 }
564
565 typedef struct {
566 int keycode;
567 const char *name;
568 } KeyDef;
569
570 static const KeyDef key_defs[] = {
571 { 0x2a, "shift" },
572 { 0x36, "shift_r" },
573
574 { 0x38, "alt" },
575 { 0xb8, "alt_r" },
576 { 0x1d, "ctrl" },
577 { 0x9d, "ctrl_r" },
578
579 { 0xdd, "menu" },
580
581 { 0x01, "esc" },
582
583 { 0x02, "1" },
584 { 0x03, "2" },
585 { 0x04, "3" },
586 { 0x05, "4" },
587 { 0x06, "5" },
588 { 0x07, "6" },
589 { 0x08, "7" },
590 { 0x09, "8" },
591 { 0x0a, "9" },
592 { 0x0b, "0" },
593 { 0x0e, "backspace" },
594
595 { 0x0f, "tab" },
596 { 0x10, "q" },
597 { 0x11, "w" },
598 { 0x12, "e" },
599 { 0x13, "r" },
600 { 0x14, "t" },
601 { 0x15, "y" },
602 { 0x16, "u" },
603 { 0x17, "i" },
604 { 0x18, "o" },
605 { 0x19, "p" },
606
607 { 0x1c, "ret" },
608
609 { 0x1e, "a" },
610 { 0x1f, "s" },
611 { 0x20, "d" },
612 { 0x21, "f" },
613 { 0x22, "g" },
614 { 0x23, "h" },
615 { 0x24, "j" },
616 { 0x25, "k" },
617 { 0x26, "l" },
618
619 { 0x2c, "z" },
620 { 0x2d, "x" },
621 { 0x2e, "c" },
622 { 0x2f, "v" },
623 { 0x30, "b" },
624 { 0x31, "n" },
625 { 0x32, "m" },
626
627 { 0x39, "spc" },
628 { 0x3a, "caps_lock" },
629 { 0x3b, "f1" },
630 { 0x3c, "f2" },
631 { 0x3d, "f3" },
632 { 0x3e, "f4" },
633 { 0x3f, "f5" },
634 { 0x40, "f6" },
635 { 0x41, "f7" },
636 { 0x42, "f8" },
637 { 0x43, "f9" },
638 { 0x44, "f10" },
639 { 0x45, "num_lock" },
640 { 0x46, "scroll_lock" },
641
642 { 0x56, "<" },
643
644 { 0x57, "f11" },
645 { 0x58, "f12" },
646
647 { 0xb7, "print" },
648
649 { 0xc7, "home" },
650 { 0xc9, "pgup" },
651 { 0xd1, "pgdn" },
652 { 0xcf, "end" },
653
654 { 0xcb, "left" },
655 { 0xc8, "up" },
656 { 0xd0, "down" },
657 { 0xcd, "right" },
658
659 { 0xd2, "insert" },
660 { 0xd3, "delete" },
661 { 0, NULL },
662 };
663
664 static int get_keycode(const char *key)
665 {
666 const KeyDef *p;
667
668 for(p = key_defs; p->name != NULL; p++) {
669 if (!strcmp(key, p->name))
670 return p->keycode;
671 }
672 return -1;
673 }
674
675 static void do_send_key(const char *string)
676 {
677 char keybuf[16], *q;
678 uint8_t keycodes[16];
679 const char *p;
680 int nb_keycodes, keycode, i;
681
682 nb_keycodes = 0;
683 p = string;
684 while (*p != '\0') {
685 q = keybuf;
686 while (*p != '\0' && *p != '-') {
687 if ((q - keybuf) < sizeof(keybuf) - 1) {
688 *q++ = *p;
689 }
690 p++;
691 }
692 *q = '\0';
693 keycode = get_keycode(keybuf);
694 if (keycode < 0) {
695 term_printf("unknown key: '%s'\n", keybuf);
696 return;
697 }
698 keycodes[nb_keycodes++] = keycode;
699 if (*p == '\0')
700 break;
701 p++;
702 }
703 /* key down events */
704 for(i = 0; i < nb_keycodes; i++) {
705 keycode = keycodes[i];
706 if (keycode & 0x80)
707 kbd_put_keycode(0xe0);
708 kbd_put_keycode(keycode & 0x7f);
709 }
710 /* key up events */
711 for(i = nb_keycodes - 1; i >= 0; i--) {
712 keycode = keycodes[i];
713 if (keycode & 0x80)
714 kbd_put_keycode(0xe0);
715 kbd_put_keycode(keycode | 0x80);
716 }
717 }
718
719 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
720 {
721 uint32_t val;
722 int suffix;
723
724 if (has_index) {
725 cpu_outb(NULL, addr & 0xffff, index & 0xff);
726 addr++;
727 }
728 addr &= 0xffff;
729
730 switch(size) {
731 default:
732 case 1:
733 val = cpu_inb(NULL, addr);
734 suffix = 'b';
735 break;
736 case 2:
737 val = cpu_inw(NULL, addr);
738 suffix = 'w';
739 break;
740 case 4:
741 val = cpu_inl(NULL, addr);
742 suffix = 'l';
743 break;
744 }
745 term_printf("port%c[0x%04x] = %#0*x\n",
746 suffix, addr, size * 2, val);
747 }
748
749 static void do_system_reset(void)
750 {
751 qemu_system_reset_request();
752 }
753
754 #if defined(TARGET_I386)
755 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
756 {
757 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
758 addr,
759 pte & mask,
760 pte & PG_GLOBAL_MASK ? 'G' : '-',
761 pte & PG_PSE_MASK ? 'P' : '-',
762 pte & PG_DIRTY_MASK ? 'D' : '-',
763 pte & PG_ACCESSED_MASK ? 'A' : '-',
764 pte & PG_PCD_MASK ? 'C' : '-',
765 pte & PG_PWT_MASK ? 'T' : '-',
766 pte & PG_USER_MASK ? 'U' : '-',
767 pte & PG_RW_MASK ? 'W' : '-');
768 }
769
770 static void tlb_info(void)
771 {
772 CPUState *env = cpu_single_env;
773 int l1, l2;
774 uint32_t pgd, pde, pte;
775
776 if (!(env->cr[0] & CR0_PG_MASK)) {
777 term_printf("PG disabled\n");
778 return;
779 }
780 pgd = env->cr[3] & ~0xfff;
781 for(l1 = 0; l1 < 1024; l1++) {
782 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
783 pde = le32_to_cpu(pde);
784 if (pde & PG_PRESENT_MASK) {
785 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
786 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
787 } else {
788 for(l2 = 0; l2 < 1024; l2++) {
789 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
790 (uint8_t *)&pte, 4);
791 pte = le32_to_cpu(pte);
792 if (pte & PG_PRESENT_MASK) {
793 print_pte((l1 << 22) + (l2 << 12),
794 pte & ~PG_PSE_MASK,
795 ~0xfff);
796 }
797 }
798 }
799 }
800 }
801 }
802
803 static void mem_print(uint32_t *pstart, int *plast_prot,
804 uint32_t end, int prot)
805 {
806 int prot1;
807 prot1 = *plast_prot;
808 if (prot != prot1) {
809 if (*pstart != -1) {
810 term_printf("%08x-%08x %08x %c%c%c\n",
811 *pstart, end, end - *pstart,
812 prot1 & PG_USER_MASK ? 'u' : '-',
813 'r',
814 prot1 & PG_RW_MASK ? 'w' : '-');
815 }
816 if (prot != 0)
817 *pstart = end;
818 else
819 *pstart = -1;
820 *plast_prot = prot;
821 }
822 }
823
824 static void mem_info(void)
825 {
826 CPUState *env = cpu_single_env;
827 int l1, l2, prot, last_prot;
828 uint32_t pgd, pde, pte, start, end;
829
830 if (!(env->cr[0] & CR0_PG_MASK)) {
831 term_printf("PG disabled\n");
832 return;
833 }
834 pgd = env->cr[3] & ~0xfff;
835 last_prot = 0;
836 start = -1;
837 for(l1 = 0; l1 < 1024; l1++) {
838 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
839 pde = le32_to_cpu(pde);
840 end = l1 << 22;
841 if (pde & PG_PRESENT_MASK) {
842 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
843 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
844 mem_print(&start, &last_prot, end, prot);
845 } else {
846 for(l2 = 0; l2 < 1024; l2++) {
847 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
848 (uint8_t *)&pte, 4);
849 pte = le32_to_cpu(pte);
850 end = (l1 << 22) + (l2 << 12);
851 if (pte & PG_PRESENT_MASK) {
852 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
853 } else {
854 prot = 0;
855 }
856 mem_print(&start, &last_prot, end, prot);
857 }
858 }
859 } else {
860 prot = 0;
861 mem_print(&start, &last_prot, end, prot);
862 }
863 }
864 }
865 #endif
866
867 static term_cmd_t term_cmds[] = {
868 { "help|?", "s?", do_help,
869 "[cmd]", "show the help" },
870 { "commit", "", do_commit,
871 "", "commit changes to the disk images (if -snapshot is used)" },
872 { "info", "s?", do_info,
873 "subcommand", "show various information about the system state" },
874 { "q|quit", "", do_quit,
875 "", "quit the emulator" },
876 { "eject", "-fB", do_eject,
877 "[-f] device", "eject a removable media (use -f to force it)" },
878 { "change", "BF", do_change,
879 "device filename", "change a removable media" },
880 { "screendump", "F", do_screen_dump,
881 "filename", "save screen into PPM image 'filename'" },
882 { "log", "s", do_log,
883 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
884 { "savevm", "F", do_savevm,
885 "filename", "save the whole virtual machine state to 'filename'" },
886 { "loadvm", "F", do_loadvm,
887 "filename", "restore the whole virtual machine state from 'filename'" },
888 { "stop", "", do_stop,
889 "", "stop emulation", },
890 { "c|cont", "", do_cont,
891 "", "resume emulation", },
892 #ifdef CONFIG_GDBSTUB
893 { "gdbserver", "i?", do_gdbserver,
894 "[port]", "start gdbserver session (default port=1234)", },
895 #endif
896 { "x", "/l", do_memory_dump,
897 "/fmt addr", "virtual memory dump starting at 'addr'", },
898 { "xp", "/l", do_physical_memory_dump,
899 "/fmt addr", "physical memory dump starting at 'addr'", },
900 { "p|print", "/l", do_print,
901 "/fmt expr", "print expression value (use $reg for CPU register access)", },
902 { "i", "/ii.", do_ioport_read,
903 "/fmt addr", "I/O port read" },
904
905 { "sendkey", "s", do_send_key,
906 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
907 { "system_reset", "", do_system_reset,
908 "", "reset the system" },
909 { NULL, NULL, },
910 };
911
912 static term_cmd_t info_cmds[] = {
913 { "version", "", do_info_version,
914 "", "show the version of qemu" },
915 { "network", "", do_info_network,
916 "", "show the network state" },
917 { "block", "", do_info_block,
918 "", "show the block devices" },
919 { "registers", "", do_info_registers,
920 "", "show the cpu registers" },
921 { "history", "", do_info_history,
922 "", "show the command line history", },
923 { "irq", "", irq_info,
924 "", "show the interrupts statistics (if available)", },
925 { "pic", "", pic_info,
926 "", "show i8259 (PIC) state", },
927 { "pci", "", pci_info,
928 "", "show PCI info", },
929 #if defined(TARGET_I386)
930 { "tlb", "", tlb_info,
931 "", "show virtual to physical memory mappings", },
932 { "mem", "", mem_info,
933 "", "show the active virtual memory mappings", },
934 #endif
935 { "jit", "", do_info_jit,
936 "", "show dynamic compiler info", },
937 { NULL, NULL, },
938 };
939
940 /*******************************************************************/
941
942 static const char *pch;
943 static jmp_buf expr_env;
944
945 #define MD_TLONG 0
946 #define MD_I32 1
947
948 typedef struct MonitorDef {
949 const char *name;
950 int offset;
951 target_long (*get_value)(struct MonitorDef *md, int val);
952 int type;
953 } MonitorDef;
954
955 #if defined(TARGET_I386)
956 static target_long monitor_get_pc (struct MonitorDef *md, int val)
957 {
958 return cpu_single_env->eip + cpu_single_env->segs[R_CS].base;
959 }
960 #endif
961
962 #if defined(TARGET_PPC)
963 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
964 {
965 unsigned int u;
966 int i;
967
968 u = 0;
969 for (i = 0; i < 8; i++)
970 u |= cpu_single_env->crf[i] << (32 - (4 * i));
971
972 return u;
973 }
974
975 static target_long monitor_get_msr (struct MonitorDef *md, int val)
976 {
977 return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
978 (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
979 (cpu_single_env->msr[MSR_EE] << MSR_EE) |
980 (cpu_single_env->msr[MSR_PR] << MSR_PR) |
981 (cpu_single_env->msr[MSR_FP] << MSR_FP) |
982 (cpu_single_env->msr[MSR_ME] << MSR_ME) |
983 (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
984 (cpu_single_env->msr[MSR_SE] << MSR_SE) |
985 (cpu_single_env->msr[MSR_BE] << MSR_BE) |
986 (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
987 (cpu_single_env->msr[MSR_IP] << MSR_IP) |
988 (cpu_single_env->msr[MSR_IR] << MSR_IR) |
989 (cpu_single_env->msr[MSR_DR] << MSR_DR) |
990 (cpu_single_env->msr[MSR_RI] << MSR_RI) |
991 (cpu_single_env->msr[MSR_LE] << MSR_LE);
992 }
993
994 static target_long monitor_get_xer (struct MonitorDef *md, int val)
995 {
996 return (cpu_single_env->xer[XER_SO] << XER_SO) |
997 (cpu_single_env->xer[XER_OV] << XER_OV) |
998 (cpu_single_env->xer[XER_CA] << XER_CA) |
999 (cpu_single_env->xer[XER_BC] << XER_BC);
1000 }
1001
1002 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1003 {
1004 return cpu_ppc_load_decr(cpu_single_env);
1005 }
1006
1007 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1008 {
1009 return cpu_ppc_load_tbu(cpu_single_env);
1010 }
1011
1012 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1013 {
1014 return cpu_ppc_load_tbl(cpu_single_env);
1015 }
1016 #endif
1017
1018 #if defined(TARGET_SPARC)
1019 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1020 {
1021 return GET_PSR(cpu_single_env);
1022 }
1023
1024 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1025 {
1026 return cpu_single_env->regwptr[val];
1027 }
1028 #endif
1029
1030 static MonitorDef monitor_defs[] = {
1031 #ifdef TARGET_I386
1032
1033 #define SEG(name, seg) \
1034 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1035 { name ".base", offsetof(CPUState, segs[seg].base) },\
1036 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1037
1038 { "eax", offsetof(CPUState, regs[0]) },
1039 { "ecx", offsetof(CPUState, regs[1]) },
1040 { "edx", offsetof(CPUState, regs[2]) },
1041 { "ebx", offsetof(CPUState, regs[3]) },
1042 { "esp|sp", offsetof(CPUState, regs[4]) },
1043 { "ebp|fp", offsetof(CPUState, regs[5]) },
1044 { "esi", offsetof(CPUState, regs[6]) },
1045 { "edi", offsetof(CPUState, regs[7]) },
1046 #ifdef TARGET_X86_64
1047 { "r8", offsetof(CPUState, regs[8]) },
1048 { "r9", offsetof(CPUState, regs[9]) },
1049 { "r10", offsetof(CPUState, regs[10]) },
1050 { "r11", offsetof(CPUState, regs[11]) },
1051 { "r12", offsetof(CPUState, regs[12]) },
1052 { "r13", offsetof(CPUState, regs[13]) },
1053 { "r14", offsetof(CPUState, regs[14]) },
1054 { "r15", offsetof(CPUState, regs[15]) },
1055 #endif
1056 { "eflags", offsetof(CPUState, eflags) },
1057 { "eip", offsetof(CPUState, eip) },
1058 SEG("cs", R_CS)
1059 SEG("ds", R_DS)
1060 SEG("es", R_ES)
1061 SEG("ss", R_SS)
1062 SEG("fs", R_FS)
1063 SEG("gs", R_GS)
1064 { "pc", 0, monitor_get_pc, },
1065 #elif defined(TARGET_PPC)
1066 { "r0", offsetof(CPUState, gpr[0]) },
1067 { "r1", offsetof(CPUState, gpr[1]) },
1068 { "r2", offsetof(CPUState, gpr[2]) },
1069 { "r3", offsetof(CPUState, gpr[3]) },
1070 { "r4", offsetof(CPUState, gpr[4]) },
1071 { "r5", offsetof(CPUState, gpr[5]) },
1072 { "r6", offsetof(CPUState, gpr[6]) },
1073 { "r7", offsetof(CPUState, gpr[7]) },
1074 { "r8", offsetof(CPUState, gpr[8]) },
1075 { "r9", offsetof(CPUState, gpr[9]) },
1076 { "r10", offsetof(CPUState, gpr[10]) },
1077 { "r11", offsetof(CPUState, gpr[11]) },
1078 { "r12", offsetof(CPUState, gpr[12]) },
1079 { "r13", offsetof(CPUState, gpr[13]) },
1080 { "r14", offsetof(CPUState, gpr[14]) },
1081 { "r15", offsetof(CPUState, gpr[15]) },
1082 { "r16", offsetof(CPUState, gpr[16]) },
1083 { "r17", offsetof(CPUState, gpr[17]) },
1084 { "r18", offsetof(CPUState, gpr[18]) },
1085 { "r19", offsetof(CPUState, gpr[19]) },
1086 { "r20", offsetof(CPUState, gpr[20]) },
1087 { "r21", offsetof(CPUState, gpr[21]) },
1088 { "r22", offsetof(CPUState, gpr[22]) },
1089 { "r23", offsetof(CPUState, gpr[23]) },
1090 { "r24", offsetof(CPUState, gpr[24]) },
1091 { "r25", offsetof(CPUState, gpr[25]) },
1092 { "r26", offsetof(CPUState, gpr[26]) },
1093 { "r27", offsetof(CPUState, gpr[27]) },
1094 { "r28", offsetof(CPUState, gpr[28]) },
1095 { "r29", offsetof(CPUState, gpr[29]) },
1096 { "r30", offsetof(CPUState, gpr[30]) },
1097 { "r31", offsetof(CPUState, gpr[31]) },
1098 { "nip|pc", offsetof(CPUState, nip) },
1099 { "lr", offsetof(CPUState, lr) },
1100 { "ctr", offsetof(CPUState, ctr) },
1101 { "decr", 0, &monitor_get_decr, },
1102 { "ccr", 0, &monitor_get_ccr, },
1103 { "msr", 0, &monitor_get_msr, },
1104 { "xer", 0, &monitor_get_xer, },
1105 { "tbu", 0, &monitor_get_tbu, },
1106 { "tbl", 0, &monitor_get_tbl, },
1107 { "sdr1", offsetof(CPUState, sdr1) },
1108 { "sr0", offsetof(CPUState, sr[0]) },
1109 { "sr1", offsetof(CPUState, sr[1]) },
1110 { "sr2", offsetof(CPUState, sr[2]) },
1111 { "sr3", offsetof(CPUState, sr[3]) },
1112 { "sr4", offsetof(CPUState, sr[4]) },
1113 { "sr5", offsetof(CPUState, sr[5]) },
1114 { "sr6", offsetof(CPUState, sr[6]) },
1115 { "sr7", offsetof(CPUState, sr[7]) },
1116 { "sr8", offsetof(CPUState, sr[8]) },
1117 { "sr9", offsetof(CPUState, sr[9]) },
1118 { "sr10", offsetof(CPUState, sr[10]) },
1119 { "sr11", offsetof(CPUState, sr[11]) },
1120 { "sr12", offsetof(CPUState, sr[12]) },
1121 { "sr13", offsetof(CPUState, sr[13]) },
1122 { "sr14", offsetof(CPUState, sr[14]) },
1123 { "sr15", offsetof(CPUState, sr[15]) },
1124 /* Too lazy to put BATs and SPRs ... */
1125 #elif defined(TARGET_SPARC)
1126 { "g0", offsetof(CPUState, gregs[0]) },
1127 { "g1", offsetof(CPUState, gregs[1]) },
1128 { "g2", offsetof(CPUState, gregs[2]) },
1129 { "g3", offsetof(CPUState, gregs[3]) },
1130 { "g4", offsetof(CPUState, gregs[4]) },
1131 { "g5", offsetof(CPUState, gregs[5]) },
1132 { "g6", offsetof(CPUState, gregs[6]) },
1133 { "g7", offsetof(CPUState, gregs[7]) },
1134 { "o0", 0, monitor_get_reg },
1135 { "o1", 1, monitor_get_reg },
1136 { "o2", 2, monitor_get_reg },
1137 { "o3", 3, monitor_get_reg },
1138 { "o4", 4, monitor_get_reg },
1139 { "o5", 5, monitor_get_reg },
1140 { "o6", 6, monitor_get_reg },
1141 { "o7", 7, monitor_get_reg },
1142 { "l0", 8, monitor_get_reg },
1143 { "l1", 9, monitor_get_reg },
1144 { "l2", 10, monitor_get_reg },
1145 { "l3", 11, monitor_get_reg },
1146 { "l4", 12, monitor_get_reg },
1147 { "l5", 13, monitor_get_reg },
1148 { "l6", 14, monitor_get_reg },
1149 { "l7", 15, monitor_get_reg },
1150 { "i0", 16, monitor_get_reg },
1151 { "i1", 17, monitor_get_reg },
1152 { "i2", 18, monitor_get_reg },
1153 { "i3", 19, monitor_get_reg },
1154 { "i4", 20, monitor_get_reg },
1155 { "i5", 21, monitor_get_reg },
1156 { "i6", 22, monitor_get_reg },
1157 { "i7", 23, monitor_get_reg },
1158 { "pc", offsetof(CPUState, pc) },
1159 { "npc", offsetof(CPUState, npc) },
1160 { "y", offsetof(CPUState, y) },
1161 { "psr", 0, &monitor_get_psr, },
1162 { "wim", offsetof(CPUState, wim) },
1163 { "tbr", offsetof(CPUState, tbr) },
1164 { "fsr", offsetof(CPUState, fsr) },
1165 { "f0", offsetof(CPUState, fpr[0]) },
1166 { "f1", offsetof(CPUState, fpr[1]) },
1167 { "f2", offsetof(CPUState, fpr[2]) },
1168 { "f3", offsetof(CPUState, fpr[3]) },
1169 { "f4", offsetof(CPUState, fpr[4]) },
1170 { "f5", offsetof(CPUState, fpr[5]) },
1171 { "f6", offsetof(CPUState, fpr[6]) },
1172 { "f7", offsetof(CPUState, fpr[7]) },
1173 { "f8", offsetof(CPUState, fpr[8]) },
1174 { "f9", offsetof(CPUState, fpr[9]) },
1175 { "f10", offsetof(CPUState, fpr[10]) },
1176 { "f11", offsetof(CPUState, fpr[11]) },
1177 { "f12", offsetof(CPUState, fpr[12]) },
1178 { "f13", offsetof(CPUState, fpr[13]) },
1179 { "f14", offsetof(CPUState, fpr[14]) },
1180 { "f15", offsetof(CPUState, fpr[15]) },
1181 { "f16", offsetof(CPUState, fpr[16]) },
1182 { "f17", offsetof(CPUState, fpr[17]) },
1183 { "f18", offsetof(CPUState, fpr[18]) },
1184 { "f19", offsetof(CPUState, fpr[19]) },
1185 { "f20", offsetof(CPUState, fpr[20]) },
1186 { "f21", offsetof(CPUState, fpr[21]) },
1187 { "f22", offsetof(CPUState, fpr[22]) },
1188 { "f23", offsetof(CPUState, fpr[23]) },
1189 { "f24", offsetof(CPUState, fpr[24]) },
1190 { "f25", offsetof(CPUState, fpr[25]) },
1191 { "f26", offsetof(CPUState, fpr[26]) },
1192 { "f27", offsetof(CPUState, fpr[27]) },
1193 { "f28", offsetof(CPUState, fpr[28]) },
1194 { "f29", offsetof(CPUState, fpr[29]) },
1195 { "f30", offsetof(CPUState, fpr[30]) },
1196 { "f31", offsetof(CPUState, fpr[31]) },
1197 #endif
1198 { NULL },
1199 };
1200
1201 static void expr_error(const char *fmt)
1202 {
1203 term_printf(fmt);
1204 term_printf("\n");
1205 longjmp(expr_env, 1);
1206 }
1207
1208 static int get_monitor_def(target_long *pval, const char *name)
1209 {
1210 MonitorDef *md;
1211 void *ptr;
1212
1213 for(md = monitor_defs; md->name != NULL; md++) {
1214 if (compare_cmd(name, md->name)) {
1215 if (md->get_value) {
1216 *pval = md->get_value(md, md->offset);
1217 } else {
1218 ptr = (uint8_t *)cpu_single_env + md->offset;
1219 switch(md->type) {
1220 case MD_I32:
1221 *pval = *(int32_t *)ptr;
1222 break;
1223 case MD_TLONG:
1224 *pval = *(target_long *)ptr;
1225 break;
1226 default:
1227 *pval = 0;
1228 break;
1229 }
1230 }
1231 return 0;
1232 }
1233 }
1234 return -1;
1235 }
1236
1237 static void next(void)
1238 {
1239 if (pch != '\0') {
1240 pch++;
1241 while (isspace(*pch))
1242 pch++;
1243 }
1244 }
1245
1246 static target_long expr_sum(void);
1247
1248 static target_long expr_unary(void)
1249 {
1250 target_long n;
1251 char *p;
1252
1253 switch(*pch) {
1254 case '+':
1255 next();
1256 n = expr_unary();
1257 break;
1258 case '-':
1259 next();
1260 n = -expr_unary();
1261 break;
1262 case '~':
1263 next();
1264 n = ~expr_unary();
1265 break;
1266 case '(':
1267 next();
1268 n = expr_sum();
1269 if (*pch != ')') {
1270 expr_error("')' expected");
1271 }
1272 next();
1273 break;
1274 case '\'':
1275 pch++;
1276 if (*pch == '\0')
1277 expr_error("character constant expected");
1278 n = *pch;
1279 pch++;
1280 if (*pch != '\'')
1281 expr_error("missing terminating \' character");
1282 next();
1283 break;
1284 case '$':
1285 {
1286 char buf[128], *q;
1287
1288 pch++;
1289 q = buf;
1290 while ((*pch >= 'a' && *pch <= 'z') ||
1291 (*pch >= 'A' && *pch <= 'Z') ||
1292 (*pch >= '0' && *pch <= '9') ||
1293 *pch == '_' || *pch == '.') {
1294 if ((q - buf) < sizeof(buf) - 1)
1295 *q++ = *pch;
1296 pch++;
1297 }
1298 while (isspace(*pch))
1299 pch++;
1300 *q = 0;
1301 if (get_monitor_def(&n, buf))
1302 expr_error("unknown register");
1303 }
1304 break;
1305 case '\0':
1306 expr_error("unexpected end of expression");
1307 n = 0;
1308 break;
1309 default:
1310 n = strtoul(pch, &p, 0);
1311 if (pch == p) {
1312 expr_error("invalid char in expression");
1313 }
1314 pch = p;
1315 while (isspace(*pch))
1316 pch++;
1317 break;
1318 }
1319 return n;
1320 }
1321
1322
1323 static target_long expr_prod(void)
1324 {
1325 target_long val, val2;
1326 int op;
1327
1328 val = expr_unary();
1329 for(;;) {
1330 op = *pch;
1331 if (op != '*' && op != '/' && op != '%')
1332 break;
1333 next();
1334 val2 = expr_unary();
1335 switch(op) {
1336 default:
1337 case '*':
1338 val *= val2;
1339 break;
1340 case '/':
1341 case '%':
1342 if (val2 == 0)
1343 expr_error("division by zero");
1344 if (op == '/')
1345 val /= val2;
1346 else
1347 val %= val2;
1348 break;
1349 }
1350 }
1351 return val;
1352 }
1353
1354 static target_long expr_logic(void)
1355 {
1356 target_long val, val2;
1357 int op;
1358
1359 val = expr_prod();
1360 for(;;) {
1361 op = *pch;
1362 if (op != '&' && op != '|' && op != '^')
1363 break;
1364 next();
1365 val2 = expr_prod();
1366 switch(op) {
1367 default:
1368 case '&':
1369 val &= val2;
1370 break;
1371 case '|':
1372 val |= val2;
1373 break;
1374 case '^':
1375 val ^= val2;
1376 break;
1377 }
1378 }
1379 return val;
1380 }
1381
1382 static target_long expr_sum(void)
1383 {
1384 target_long val, val2;
1385 int op;
1386
1387 val = expr_logic();
1388 for(;;) {
1389 op = *pch;
1390 if (op != '+' && op != '-')
1391 break;
1392 next();
1393 val2 = expr_logic();
1394 if (op == '+')
1395 val += val2;
1396 else
1397 val -= val2;
1398 }
1399 return val;
1400 }
1401
1402 static int get_expr(target_long *pval, const char **pp)
1403 {
1404 pch = *pp;
1405 if (setjmp(expr_env)) {
1406 *pp = pch;
1407 return -1;
1408 }
1409 while (isspace(*pch))
1410 pch++;
1411 *pval = expr_sum();
1412 *pp = pch;
1413 return 0;
1414 }
1415
1416 static int get_str(char *buf, int buf_size, const char **pp)
1417 {
1418 const char *p;
1419 char *q;
1420 int c;
1421
1422 q = buf;
1423 p = *pp;
1424 while (isspace(*p))
1425 p++;
1426 if (*p == '\0') {
1427 fail:
1428 *q = '\0';
1429 *pp = p;
1430 return -1;
1431 }
1432 if (*p == '\"') {
1433 p++;
1434 while (*p != '\0' && *p != '\"') {
1435 if (*p == '\\') {
1436 p++;
1437 c = *p++;
1438 switch(c) {
1439 case 'n':
1440 c = '\n';
1441 break;
1442 case 'r':
1443 c = '\r';
1444 break;
1445 case '\\':
1446 case '\'':
1447 case '\"':
1448 break;
1449 default:
1450 qemu_printf("unsupported escape code: '\\%c'\n", c);
1451 goto fail;
1452 }
1453 if ((q - buf) < buf_size - 1) {
1454 *q++ = c;
1455 }
1456 } else {
1457 if ((q - buf) < buf_size - 1) {
1458 *q++ = *p;
1459 }
1460 p++;
1461 }
1462 }
1463 if (*p != '\"') {
1464 qemu_printf("unterminated string\n");
1465 goto fail;
1466 }
1467 p++;
1468 } else {
1469 while (*p != '\0' && !isspace(*p)) {
1470 if ((q - buf) < buf_size - 1) {
1471 *q++ = *p;
1472 }
1473 p++;
1474 }
1475 }
1476 *q = '\0';
1477 *pp = p;
1478 return 0;
1479 }
1480
1481 static int default_fmt_format = 'x';
1482 static int default_fmt_size = 4;
1483
1484 #define MAX_ARGS 16
1485
1486 static void monitor_handle_command(const char *cmdline)
1487 {
1488 const char *p, *pstart, *typestr;
1489 char *q;
1490 int c, nb_args, len, i, has_arg;
1491 term_cmd_t *cmd;
1492 char cmdname[256];
1493 char buf[1024];
1494 void *str_allocated[MAX_ARGS];
1495 void *args[MAX_ARGS];
1496
1497 #ifdef DEBUG
1498 term_printf("command='%s'\n", cmdline);
1499 #endif
1500
1501 /* extract the command name */
1502 p = cmdline;
1503 q = cmdname;
1504 while (isspace(*p))
1505 p++;
1506 if (*p == '\0')
1507 return;
1508 pstart = p;
1509 while (*p != '\0' && *p != '/' && !isspace(*p))
1510 p++;
1511 len = p - pstart;
1512 if (len > sizeof(cmdname) - 1)
1513 len = sizeof(cmdname) - 1;
1514 memcpy(cmdname, pstart, len);
1515 cmdname[len] = '\0';
1516
1517 /* find the command */
1518 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1519 if (compare_cmd(cmdname, cmd->name))
1520 goto found;
1521 }
1522 term_printf("unknown command: '%s'\n", cmdname);
1523 return;
1524 found:
1525
1526 for(i = 0; i < MAX_ARGS; i++)
1527 str_allocated[i] = NULL;
1528
1529 /* parse the parameters */
1530 typestr = cmd->args_type;
1531 nb_args = 0;
1532 for(;;) {
1533 c = *typestr;
1534 if (c == '\0')
1535 break;
1536 typestr++;
1537 switch(c) {
1538 case 'F':
1539 case 'B':
1540 case 's':
1541 {
1542 int ret;
1543 char *str;
1544
1545 while (isspace(*p))
1546 p++;
1547 if (*typestr == '?') {
1548 typestr++;
1549 if (*p == '\0') {
1550 /* no optional string: NULL argument */
1551 str = NULL;
1552 goto add_str;
1553 }
1554 }
1555 ret = get_str(buf, sizeof(buf), &p);
1556 if (ret < 0) {
1557 switch(c) {
1558 case 'F':
1559 term_printf("%s: filename expected\n", cmdname);
1560 break;
1561 case 'B':
1562 term_printf("%s: block device name expected\n", cmdname);
1563 break;
1564 default:
1565 term_printf("%s: string expected\n", cmdname);
1566 break;
1567 }
1568 goto fail;
1569 }
1570 str = qemu_malloc(strlen(buf) + 1);
1571 strcpy(str, buf);
1572 str_allocated[nb_args] = str;
1573 add_str:
1574 if (nb_args >= MAX_ARGS) {
1575 error_args:
1576 term_printf("%s: too many arguments\n", cmdname);
1577 goto fail;
1578 }
1579 args[nb_args++] = str;
1580 }
1581 break;
1582 case '/':
1583 {
1584 int count, format, size;
1585
1586 while (isspace(*p))
1587 p++;
1588 if (*p == '/') {
1589 /* format found */
1590 p++;
1591 count = 1;
1592 if (isdigit(*p)) {
1593 count = 0;
1594 while (isdigit(*p)) {
1595 count = count * 10 + (*p - '0');
1596 p++;
1597 }
1598 }
1599 size = -1;
1600 format = -1;
1601 for(;;) {
1602 switch(*p) {
1603 case 'o':
1604 case 'd':
1605 case 'u':
1606 case 'x':
1607 case 'i':
1608 case 'c':
1609 format = *p++;
1610 break;
1611 case 'b':
1612 size = 1;
1613 p++;
1614 break;
1615 case 'h':
1616 size = 2;
1617 p++;
1618 break;
1619 case 'w':
1620 size = 4;
1621 p++;
1622 break;
1623 case 'g':
1624 case 'L':
1625 size = 8;
1626 p++;
1627 break;
1628 default:
1629 goto next;
1630 }
1631 }
1632 next:
1633 if (*p != '\0' && !isspace(*p)) {
1634 term_printf("invalid char in format: '%c'\n", *p);
1635 goto fail;
1636 }
1637 if (format < 0)
1638 format = default_fmt_format;
1639 if (format != 'i') {
1640 /* for 'i', not specifying a size gives -1 as size */
1641 if (size < 0)
1642 size = default_fmt_size;
1643 }
1644 default_fmt_size = size;
1645 default_fmt_format = format;
1646 } else {
1647 count = 1;
1648 format = default_fmt_format;
1649 if (format != 'i') {
1650 size = default_fmt_size;
1651 } else {
1652 size = -1;
1653 }
1654 }
1655 if (nb_args + 3 > MAX_ARGS)
1656 goto error_args;
1657 args[nb_args++] = (void*)count;
1658 args[nb_args++] = (void*)format;
1659 args[nb_args++] = (void*)size;
1660 }
1661 break;
1662 case 'i':
1663 case 'l':
1664 {
1665 target_long val;
1666 while (isspace(*p))
1667 p++;
1668 if (*typestr == '?' || *typestr == '.') {
1669 typestr++;
1670 if (*typestr == '?') {
1671 if (*p == '\0')
1672 has_arg = 0;
1673 else
1674 has_arg = 1;
1675 } else {
1676 if (*p == '.') {
1677 p++;
1678 while (isspace(*p))
1679 p++;
1680 has_arg = 1;
1681 } else {
1682 has_arg = 0;
1683 }
1684 }
1685 if (nb_args >= MAX_ARGS)
1686 goto error_args;
1687 args[nb_args++] = (void *)has_arg;
1688 if (!has_arg) {
1689 if (nb_args >= MAX_ARGS)
1690 goto error_args;
1691 val = -1;
1692 goto add_num;
1693 }
1694 }
1695 if (get_expr(&val, &p))
1696 goto fail;
1697 add_num:
1698 if (c == 'i') {
1699 if (nb_args >= MAX_ARGS)
1700 goto error_args;
1701 args[nb_args++] = (void *)(int)val;
1702 } else {
1703 if ((nb_args + 1) >= MAX_ARGS)
1704 goto error_args;
1705 #if TARGET_LONG_BITS == 64
1706 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1707 #else
1708 args[nb_args++] = (void *)0;
1709 #endif
1710 args[nb_args++] = (void *)(int)(val & 0xffffffff);
1711 }
1712 }
1713 break;
1714 case '-':
1715 {
1716 int has_option;
1717 /* option */
1718
1719 c = *typestr++;
1720 if (c == '\0')
1721 goto bad_type;
1722 while (isspace(*p))
1723 p++;
1724 has_option = 0;
1725 if (*p == '-') {
1726 p++;
1727 if (*p != c) {
1728 term_printf("%s: unsupported option -%c\n",
1729 cmdname, *p);
1730 goto fail;
1731 }
1732 p++;
1733 has_option = 1;
1734 }
1735 if (nb_args >= MAX_ARGS)
1736 goto error_args;
1737 args[nb_args++] = (void *)has_option;
1738 }
1739 break;
1740 default:
1741 bad_type:
1742 term_printf("%s: unknown type '%c'\n", cmdname, c);
1743 goto fail;
1744 }
1745 }
1746 /* check that all arguments were parsed */
1747 while (isspace(*p))
1748 p++;
1749 if (*p != '\0') {
1750 term_printf("%s: extraneous characters at the end of line\n",
1751 cmdname);
1752 goto fail;
1753 }
1754
1755 switch(nb_args) {
1756 case 0:
1757 cmd->handler();
1758 break;
1759 case 1:
1760 cmd->handler(args[0]);
1761 break;
1762 case 2:
1763 cmd->handler(args[0], args[1]);
1764 break;
1765 case 3:
1766 cmd->handler(args[0], args[1], args[2]);
1767 break;
1768 case 4:
1769 cmd->handler(args[0], args[1], args[2], args[3]);
1770 break;
1771 case 5:
1772 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1773 break;
1774 case 6:
1775 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1776 break;
1777 default:
1778 term_printf("unsupported number of arguments: %d\n", nb_args);
1779 goto fail;
1780 }
1781 fail:
1782 for(i = 0; i < MAX_ARGS; i++)
1783 qemu_free(str_allocated[i]);
1784 return;
1785 }
1786
1787 static void cmd_completion(const char *name, const char *list)
1788 {
1789 const char *p, *pstart;
1790 char cmd[128];
1791 int len;
1792
1793 p = list;
1794 for(;;) {
1795 pstart = p;
1796 p = strchr(p, '|');
1797 if (!p)
1798 p = pstart + strlen(pstart);
1799 len = p - pstart;
1800 if (len > sizeof(cmd) - 2)
1801 len = sizeof(cmd) - 2;
1802 memcpy(cmd, pstart, len);
1803 cmd[len] = '\0';
1804 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1805 add_completion(cmd);
1806 }
1807 if (*p == '\0')
1808 break;
1809 p++;
1810 }
1811 }
1812
1813 static void file_completion(const char *input)
1814 {
1815 DIR *ffs;
1816 struct dirent *d;
1817 char path[1024];
1818 char file[1024], file_prefix[1024];
1819 int input_path_len;
1820 const char *p;
1821
1822 p = strrchr(input, '/');
1823 if (!p) {
1824 input_path_len = 0;
1825 pstrcpy(file_prefix, sizeof(file_prefix), input);
1826 strcpy(path, ".");
1827 } else {
1828 input_path_len = p - input + 1;
1829 memcpy(path, input, input_path_len);
1830 if (input_path_len > sizeof(path) - 1)
1831 input_path_len = sizeof(path) - 1;
1832 path[input_path_len] = '\0';
1833 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1834 }
1835 #ifdef DEBUG_COMPLETION
1836 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1837 #endif
1838 ffs = opendir(path);
1839 if (!ffs)
1840 return;
1841 for(;;) {
1842 struct stat sb;
1843 d = readdir(ffs);
1844 if (!d)
1845 break;
1846 if (strstart(d->d_name, file_prefix, NULL)) {
1847 memcpy(file, input, input_path_len);
1848 strcpy(file + input_path_len, d->d_name);
1849 /* stat the file to find out if it's a directory.
1850 * In that case add a slash to speed up typing long paths
1851 */
1852 stat(file, &sb);
1853 if(S_ISDIR(sb.st_mode))
1854 strcat(file, "/");
1855 add_completion(file);
1856 }
1857 }
1858 closedir(ffs);
1859 }
1860
1861 static void block_completion_it(void *opaque, const char *name)
1862 {
1863 const char *input = opaque;
1864
1865 if (input[0] == '\0' ||
1866 !strncmp(name, (char *)input, strlen(input))) {
1867 add_completion(name);
1868 }
1869 }
1870
1871 /* NOTE: this parser is an approximate form of the real command parser */
1872 static void parse_cmdline(const char *cmdline,
1873 int *pnb_args, char **args)
1874 {
1875 const char *p;
1876 int nb_args, ret;
1877 char buf[1024];
1878
1879 p = cmdline;
1880 nb_args = 0;
1881 for(;;) {
1882 while (isspace(*p))
1883 p++;
1884 if (*p == '\0')
1885 break;
1886 if (nb_args >= MAX_ARGS)
1887 break;
1888 ret = get_str(buf, sizeof(buf), &p);
1889 args[nb_args] = qemu_strdup(buf);
1890 nb_args++;
1891 if (ret < 0)
1892 break;
1893 }
1894 *pnb_args = nb_args;
1895 }
1896
1897 void readline_find_completion(const char *cmdline)
1898 {
1899 const char *cmdname;
1900 char *args[MAX_ARGS];
1901 int nb_args, i, len;
1902 const char *ptype, *str;
1903 term_cmd_t *cmd;
1904
1905 parse_cmdline(cmdline, &nb_args, args);
1906 #ifdef DEBUG_COMPLETION
1907 for(i = 0; i < nb_args; i++) {
1908 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1909 }
1910 #endif
1911
1912 /* if the line ends with a space, it means we want to complete the
1913 next arg */
1914 len = strlen(cmdline);
1915 if (len > 0 && isspace(cmdline[len - 1])) {
1916 if (nb_args >= MAX_ARGS)
1917 return;
1918 args[nb_args++] = qemu_strdup("");
1919 }
1920 if (nb_args <= 1) {
1921 /* command completion */
1922 if (nb_args == 0)
1923 cmdname = "";
1924 else
1925 cmdname = args[0];
1926 completion_index = strlen(cmdname);
1927 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1928 cmd_completion(cmdname, cmd->name);
1929 }
1930 } else {
1931 /* find the command */
1932 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1933 if (compare_cmd(args[0], cmd->name))
1934 goto found;
1935 }
1936 return;
1937 found:
1938 ptype = cmd->args_type;
1939 for(i = 0; i < nb_args - 2; i++) {
1940 if (*ptype != '\0') {
1941 ptype++;
1942 while (*ptype == '?')
1943 ptype++;
1944 }
1945 }
1946 str = args[nb_args - 1];
1947 switch(*ptype) {
1948 case 'F':
1949 /* file completion */
1950 completion_index = strlen(str);
1951 file_completion(str);
1952 break;
1953 case 'B':
1954 /* block device name completion */
1955 completion_index = strlen(str);
1956 bdrv_iterate(block_completion_it, (void *)str);
1957 break;
1958 case 's':
1959 /* XXX: more generic ? */
1960 if (!strcmp(cmd->name, "info")) {
1961 completion_index = strlen(str);
1962 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
1963 cmd_completion(str, cmd->name);
1964 }
1965 }
1966 break;
1967 default:
1968 break;
1969 }
1970 }
1971 for(i = 0; i < nb_args; i++)
1972 qemu_free(args[i]);
1973 }
1974
1975 static int term_can_read(void *opaque)
1976 {
1977 return 128;
1978 }
1979
1980 static void term_read(void *opaque, const uint8_t *buf, int size)
1981 {
1982 int i;
1983 for(i = 0; i < size; i++)
1984 readline_handle_byte(buf[i]);
1985 }
1986
1987 static void monitor_start_input(void);
1988
1989 static void monitor_handle_command1(void *opaque, const char *cmdline)
1990 {
1991 monitor_handle_command(cmdline);
1992 monitor_start_input();
1993 }
1994
1995 static void monitor_start_input(void)
1996 {
1997 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
1998 }
1999
2000 void monitor_init(CharDriverState *hd, int show_banner)
2001 {
2002 monitor_hd = hd;
2003 if (show_banner) {
2004 term_printf("QEMU %s monitor - type 'help' for more information\n",
2005 QEMU_VERSION);
2006 }
2007 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2008 monitor_start_input();
2009 }
2010
2011 /* XXX: use threads ? */
2012 /* modal monitor readline */
2013 static int monitor_readline_started;
2014 static char *monitor_readline_buf;
2015 static int monitor_readline_buf_size;
2016
2017 static void monitor_readline_cb(void *opaque, const char *input)
2018 {
2019 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2020 monitor_readline_started = 0;
2021 }
2022
2023 void monitor_readline(const char *prompt, int is_password,
2024 char *buf, int buf_size)
2025 {
2026 if (is_password) {
2027 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2028 }
2029 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2030 monitor_readline_buf = buf;
2031 monitor_readline_buf_size = buf_size;
2032 monitor_readline_started = 1;
2033 while (monitor_readline_started) {
2034 main_loop_wait(10);
2035 }
2036 }