]> git.proxmox.com Git - mirror_qemu.git/blob - monitor.c
memsave monitor command
[mirror_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 CPUState *mon_cpu = NULL;
68
69 void term_flush(void)
70 {
71 if (term_outbuf_index > 0) {
72 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
73 term_outbuf_index = 0;
74 }
75 }
76
77 /* flush at every end of line or if the buffer is full */
78 void term_puts(const char *str)
79 {
80 int c;
81 for(;;) {
82 c = *str++;
83 if (c == '\0')
84 break;
85 if (c == '\n')
86 term_outbuf[term_outbuf_index++] = '\r';
87 term_outbuf[term_outbuf_index++] = c;
88 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
89 c == '\n')
90 term_flush();
91 }
92 }
93
94 void term_vprintf(const char *fmt, va_list ap)
95 {
96 char buf[4096];
97 vsnprintf(buf, sizeof(buf), fmt, ap);
98 term_puts(buf);
99 }
100
101 void term_printf(const char *fmt, ...)
102 {
103 va_list ap;
104 va_start(ap, fmt);
105 term_vprintf(fmt, ap);
106 va_end(ap);
107 }
108
109 void term_print_filename(const char *filename)
110 {
111 int i;
112
113 for (i = 0; filename[i]; i++) {
114 switch (filename[i]) {
115 case ' ':
116 case '"':
117 case '\\':
118 term_printf("\\%c", filename[i]);
119 break;
120 case '\t':
121 term_printf("\\t");
122 break;
123 case '\r':
124 term_printf("\\r");
125 break;
126 case '\n':
127 term_printf("\\n");
128 break;
129 default:
130 term_printf("%c", filename[i]);
131 break;
132 }
133 }
134 }
135
136 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
137 {
138 va_list ap;
139 va_start(ap, fmt);
140 term_vprintf(fmt, ap);
141 va_end(ap);
142 return 0;
143 }
144
145 static int compare_cmd(const char *name, const char *list)
146 {
147 const char *p, *pstart;
148 int len;
149 len = strlen(name);
150 p = list;
151 for(;;) {
152 pstart = p;
153 p = strchr(p, '|');
154 if (!p)
155 p = pstart + strlen(pstart);
156 if ((p - pstart) == len && !memcmp(pstart, name, len))
157 return 1;
158 if (*p == '\0')
159 break;
160 p++;
161 }
162 return 0;
163 }
164
165 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
166 {
167 term_cmd_t *cmd;
168
169 for(cmd = cmds; cmd->name != NULL; cmd++) {
170 if (!name || !strcmp(name, cmd->name))
171 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
172 }
173 }
174
175 static void help_cmd(const char *name)
176 {
177 if (name && !strcmp(name, "info")) {
178 help_cmd1(info_cmds, "info ", NULL);
179 } else {
180 help_cmd1(term_cmds, "", name);
181 if (name && !strcmp(name, "log")) {
182 CPULogItem *item;
183 term_printf("Log items (comma separated):\n");
184 term_printf("%-10s %s\n", "none", "remove all logs");
185 for(item = cpu_log_items; item->mask != 0; item++) {
186 term_printf("%-10s %s\n", item->name, item->help);
187 }
188 }
189 }
190 }
191
192 static void do_help(const char *name)
193 {
194 help_cmd(name);
195 }
196
197 static void do_commit(const char *device)
198 {
199 int i, all_devices;
200
201 all_devices = !strcmp(device, "all");
202 for (i = 0; i < MAX_DISKS; i++) {
203 if (bs_table[i]) {
204 if (all_devices ||
205 !strcmp(bdrv_get_device_name(bs_table[i]), device))
206 bdrv_commit(bs_table[i]);
207 }
208 }
209 }
210
211 static void do_info(const char *item)
212 {
213 term_cmd_t *cmd;
214
215 if (!item)
216 goto help;
217 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
218 if (compare_cmd(item, cmd->name))
219 goto found;
220 }
221 help:
222 help_cmd("info");
223 return;
224 found:
225 cmd->handler();
226 }
227
228 static void do_info_version(void)
229 {
230 term_printf("%s\n", QEMU_VERSION);
231 }
232
233 static void do_info_block(void)
234 {
235 bdrv_info();
236 }
237
238 /* get the current CPU defined by the user */
239 int mon_set_cpu(int cpu_index)
240 {
241 CPUState *env;
242
243 for(env = first_cpu; env != NULL; env = env->next_cpu) {
244 if (env->cpu_index == cpu_index) {
245 mon_cpu = env;
246 return 0;
247 }
248 }
249 return -1;
250 }
251
252 CPUState *mon_get_cpu(void)
253 {
254 if (!mon_cpu) {
255 mon_set_cpu(0);
256 }
257 return mon_cpu;
258 }
259
260 static void do_info_registers(void)
261 {
262 CPUState *env;
263 env = mon_get_cpu();
264 if (!env)
265 return;
266 #ifdef TARGET_I386
267 cpu_dump_state(env, NULL, monitor_fprintf,
268 X86_DUMP_FPU);
269 #else
270 cpu_dump_state(env, NULL, monitor_fprintf,
271 0);
272 #endif
273 }
274
275 static void do_info_cpus(void)
276 {
277 CPUState *env;
278
279 /* just to set the default cpu if not already done */
280 mon_get_cpu();
281
282 for(env = first_cpu; env != NULL; env = env->next_cpu) {
283 term_printf("%c CPU #%d:",
284 (env == mon_cpu) ? '*' : ' ',
285 env->cpu_index);
286 #if defined(TARGET_I386)
287 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
288 if (env->hflags & HF_HALTED_MASK)
289 term_printf(" (halted)");
290 #elif defined(TARGET_PPC)
291 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
292 if (env->halted)
293 term_printf(" (halted)");
294 #elif defined(TARGET_SPARC)
295 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
296 if (env->halted)
297 term_printf(" (halted)");
298 #endif
299 term_printf("\n");
300 }
301 }
302
303 static void do_cpu_set(int index)
304 {
305 if (mon_set_cpu(index) < 0)
306 term_printf("Invalid CPU index\n");
307 }
308
309 static void do_info_jit(void)
310 {
311 dump_exec_info(NULL, monitor_fprintf);
312 }
313
314 static void do_info_history (void)
315 {
316 int i;
317 const char *str;
318
319 i = 0;
320 for(;;) {
321 str = readline_get_history(i);
322 if (!str)
323 break;
324 term_printf("%d: '%s'\n", i, str);
325 i++;
326 }
327 }
328
329 static void do_quit(void)
330 {
331 exit(0);
332 }
333
334 static int eject_device(BlockDriverState *bs, int force)
335 {
336 if (bdrv_is_inserted(bs)) {
337 if (!force) {
338 if (!bdrv_is_removable(bs)) {
339 term_printf("device is not removable\n");
340 return -1;
341 }
342 if (bdrv_is_locked(bs)) {
343 term_printf("device is locked\n");
344 return -1;
345 }
346 }
347 bdrv_close(bs);
348 }
349 return 0;
350 }
351
352 static void do_eject(int force, const char *filename)
353 {
354 BlockDriverState *bs;
355
356 bs = bdrv_find(filename);
357 if (!bs) {
358 term_printf("device not found\n");
359 return;
360 }
361 eject_device(bs, force);
362 }
363
364 static void do_change(const char *device, const char *filename)
365 {
366 BlockDriverState *bs;
367 int i;
368 char password[256];
369
370 bs = bdrv_find(device);
371 if (!bs) {
372 term_printf("device not found\n");
373 return;
374 }
375 if (eject_device(bs, 0) < 0)
376 return;
377 bdrv_open(bs, filename, 0);
378 if (bdrv_is_encrypted(bs)) {
379 term_printf("%s is encrypted.\n", device);
380 for(i = 0; i < 3; i++) {
381 monitor_readline("Password: ", 1, password, sizeof(password));
382 if (bdrv_set_key(bs, password) == 0)
383 break;
384 term_printf("invalid password\n");
385 }
386 }
387 }
388
389 static void do_screen_dump(const char *filename)
390 {
391 vga_hw_screen_dump(filename);
392 }
393
394 static void do_log(const char *items)
395 {
396 int mask;
397
398 if (!strcmp(items, "none")) {
399 mask = 0;
400 } else {
401 mask = cpu_str_to_log_mask(items);
402 if (!mask) {
403 help_cmd("log");
404 return;
405 }
406 }
407 cpu_set_log(mask);
408 }
409
410 static void do_stop(void)
411 {
412 vm_stop(EXCP_INTERRUPT);
413 }
414
415 static void do_cont(void)
416 {
417 vm_start();
418 }
419
420 #ifdef CONFIG_GDBSTUB
421 static void do_gdbserver(int has_port, int port)
422 {
423 if (!has_port)
424 port = DEFAULT_GDBSTUB_PORT;
425 if (gdbserver_start(port) < 0) {
426 qemu_printf("Could not open gdbserver socket on port %d\n", port);
427 } else {
428 qemu_printf("Waiting gdb connection on port %d\n", port);
429 }
430 }
431 #endif
432
433 static void term_printc(int c)
434 {
435 term_printf("'");
436 switch(c) {
437 case '\'':
438 term_printf("\\'");
439 break;
440 case '\\':
441 term_printf("\\\\");
442 break;
443 case '\n':
444 term_printf("\\n");
445 break;
446 case '\r':
447 term_printf("\\r");
448 break;
449 default:
450 if (c >= 32 && c <= 126) {
451 term_printf("%c", c);
452 } else {
453 term_printf("\\x%02x", c);
454 }
455 break;
456 }
457 term_printf("'");
458 }
459
460 static void memory_dump(int count, int format, int wsize,
461 target_ulong addr, int is_physical)
462 {
463 CPUState *env;
464 int nb_per_line, l, line_size, i, max_digits, len;
465 uint8_t buf[16];
466 uint64_t v;
467
468 if (format == 'i') {
469 int flags;
470 flags = 0;
471 env = mon_get_cpu();
472 if (!env && !is_physical)
473 return;
474 #ifdef TARGET_I386
475 if (wsize == 2) {
476 flags = 1;
477 } else if (wsize == 4) {
478 flags = 0;
479 } else {
480 /* as default we use the current CS size */
481 flags = 0;
482 if (env) {
483 #ifdef TARGET_X86_64
484 if ((env->efer & MSR_EFER_LMA) &&
485 (env->segs[R_CS].flags & DESC_L_MASK))
486 flags = 2;
487 else
488 #endif
489 if (!(env->segs[R_CS].flags & DESC_B_MASK))
490 flags = 1;
491 }
492 }
493 #endif
494 monitor_disas(env, addr, count, is_physical, flags);
495 return;
496 }
497
498 len = wsize * count;
499 if (wsize == 1)
500 line_size = 8;
501 else
502 line_size = 16;
503 nb_per_line = line_size / wsize;
504 max_digits = 0;
505
506 switch(format) {
507 case 'o':
508 max_digits = (wsize * 8 + 2) / 3;
509 break;
510 default:
511 case 'x':
512 max_digits = (wsize * 8) / 4;
513 break;
514 case 'u':
515 case 'd':
516 max_digits = (wsize * 8 * 10 + 32) / 33;
517 break;
518 case 'c':
519 wsize = 1;
520 break;
521 }
522
523 while (len > 0) {
524 term_printf(TARGET_FMT_lx ":", addr);
525 l = len;
526 if (l > line_size)
527 l = line_size;
528 if (is_physical) {
529 cpu_physical_memory_rw(addr, buf, l, 0);
530 } else {
531 env = mon_get_cpu();
532 if (!env)
533 break;
534 cpu_memory_rw_debug(env, addr, buf, l, 0);
535 }
536 i = 0;
537 while (i < l) {
538 switch(wsize) {
539 default:
540 case 1:
541 v = ldub_raw(buf + i);
542 break;
543 case 2:
544 v = lduw_raw(buf + i);
545 break;
546 case 4:
547 v = (uint32_t)ldl_raw(buf + i);
548 break;
549 case 8:
550 v = ldq_raw(buf + i);
551 break;
552 }
553 term_printf(" ");
554 switch(format) {
555 case 'o':
556 term_printf("%#*" PRIo64, max_digits, v);
557 break;
558 case 'x':
559 term_printf("0x%0*" PRIx64, max_digits, v);
560 break;
561 case 'u':
562 term_printf("%*" PRIu64, max_digits, v);
563 break;
564 case 'd':
565 term_printf("%*" PRId64, max_digits, v);
566 break;
567 case 'c':
568 term_printc(v);
569 break;
570 }
571 i += wsize;
572 }
573 term_printf("\n");
574 addr += l;
575 len -= l;
576 }
577 }
578
579 #if TARGET_LONG_BITS == 64
580 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
581 #else
582 #define GET_TLONG(h, l) (l)
583 #endif
584
585 static void do_memory_dump(int count, int format, int size,
586 uint32_t addrh, uint32_t addrl)
587 {
588 target_long addr = GET_TLONG(addrh, addrl);
589 memory_dump(count, format, size, addr, 0);
590 }
591
592 static void do_physical_memory_dump(int count, int format, int size,
593 uint32_t addrh, uint32_t addrl)
594
595 {
596 target_long addr = GET_TLONG(addrh, addrl);
597 memory_dump(count, format, size, addr, 1);
598 }
599
600 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
601 {
602 target_long val = GET_TLONG(valh, vall);
603 #if TARGET_LONG_BITS == 32
604 switch(format) {
605 case 'o':
606 term_printf("%#o", val);
607 break;
608 case 'x':
609 term_printf("%#x", val);
610 break;
611 case 'u':
612 term_printf("%u", val);
613 break;
614 default:
615 case 'd':
616 term_printf("%d", val);
617 break;
618 case 'c':
619 term_printc(val);
620 break;
621 }
622 #else
623 switch(format) {
624 case 'o':
625 term_printf("%#" PRIo64, val);
626 break;
627 case 'x':
628 term_printf("%#" PRIx64, val);
629 break;
630 case 'u':
631 term_printf("%" PRIu64, val);
632 break;
633 default:
634 case 'd':
635 term_printf("%" PRId64, val);
636 break;
637 case 'c':
638 term_printc(val);
639 break;
640 }
641 #endif
642 term_printf("\n");
643 }
644
645 static void do_memory_save(unsigned int valh, unsigned int vall,
646 uint32_t size, const char *filename)
647 {
648 FILE *f;
649 target_long addr = GET_TLONG(valh, vall);
650 uint32_t l;
651 CPUState *env;
652 uint8_t buf[1024];
653
654 env = mon_get_cpu();
655 if (!env)
656 return;
657
658 f = fopen(filename, "wb");
659 if (!f) {
660 term_printf("could not open '%s'\n", filename);
661 return;
662 }
663 while (size != 0) {
664 l = sizeof(buf);
665 if (l > size)
666 l = size;
667 cpu_memory_rw_debug(env, addr, buf, l, 0);
668 fwrite(buf, 1, l, f);
669 addr += l;
670 size -= l;
671 }
672 fclose(f);
673 }
674
675 static void do_sum(uint32_t start, uint32_t size)
676 {
677 uint32_t addr;
678 uint8_t buf[1];
679 uint16_t sum;
680
681 sum = 0;
682 for(addr = start; addr < (start + size); addr++) {
683 cpu_physical_memory_rw(addr, buf, 1, 0);
684 /* BSD sum algorithm ('sum' Unix command) */
685 sum = (sum >> 1) | (sum << 15);
686 sum += buf[0];
687 }
688 term_printf("%05d\n", sum);
689 }
690
691 typedef struct {
692 int keycode;
693 const char *name;
694 } KeyDef;
695
696 static const KeyDef key_defs[] = {
697 { 0x2a, "shift" },
698 { 0x36, "shift_r" },
699
700 { 0x38, "alt" },
701 { 0xb8, "alt_r" },
702 { 0x1d, "ctrl" },
703 { 0x9d, "ctrl_r" },
704
705 { 0xdd, "menu" },
706
707 { 0x01, "esc" },
708
709 { 0x02, "1" },
710 { 0x03, "2" },
711 { 0x04, "3" },
712 { 0x05, "4" },
713 { 0x06, "5" },
714 { 0x07, "6" },
715 { 0x08, "7" },
716 { 0x09, "8" },
717 { 0x0a, "9" },
718 { 0x0b, "0" },
719 { 0x0c, "minus" },
720 { 0x0d, "equal" },
721 { 0x0e, "backspace" },
722
723 { 0x0f, "tab" },
724 { 0x10, "q" },
725 { 0x11, "w" },
726 { 0x12, "e" },
727 { 0x13, "r" },
728 { 0x14, "t" },
729 { 0x15, "y" },
730 { 0x16, "u" },
731 { 0x17, "i" },
732 { 0x18, "o" },
733 { 0x19, "p" },
734
735 { 0x1c, "ret" },
736
737 { 0x1e, "a" },
738 { 0x1f, "s" },
739 { 0x20, "d" },
740 { 0x21, "f" },
741 { 0x22, "g" },
742 { 0x23, "h" },
743 { 0x24, "j" },
744 { 0x25, "k" },
745 { 0x26, "l" },
746
747 { 0x2c, "z" },
748 { 0x2d, "x" },
749 { 0x2e, "c" },
750 { 0x2f, "v" },
751 { 0x30, "b" },
752 { 0x31, "n" },
753 { 0x32, "m" },
754
755 { 0x39, "spc" },
756 { 0x3a, "caps_lock" },
757 { 0x3b, "f1" },
758 { 0x3c, "f2" },
759 { 0x3d, "f3" },
760 { 0x3e, "f4" },
761 { 0x3f, "f5" },
762 { 0x40, "f6" },
763 { 0x41, "f7" },
764 { 0x42, "f8" },
765 { 0x43, "f9" },
766 { 0x44, "f10" },
767 { 0x45, "num_lock" },
768 { 0x46, "scroll_lock" },
769
770 { 0xb5, "kp_divide" },
771 { 0x37, "kp_multiply" },
772 { 0x4a, "kp_substract" },
773 { 0x4e, "kp_add" },
774 { 0x9c, "kp_enter" },
775 { 0x53, "kp_decimal" },
776
777 { 0x52, "kp_0" },
778 { 0x4f, "kp_1" },
779 { 0x50, "kp_2" },
780 { 0x51, "kp_3" },
781 { 0x4b, "kp_4" },
782 { 0x4c, "kp_5" },
783 { 0x4d, "kp_6" },
784 { 0x47, "kp_7" },
785 { 0x48, "kp_8" },
786 { 0x49, "kp_9" },
787
788 { 0x56, "<" },
789
790 { 0x57, "f11" },
791 { 0x58, "f12" },
792
793 { 0xb7, "print" },
794
795 { 0xc7, "home" },
796 { 0xc9, "pgup" },
797 { 0xd1, "pgdn" },
798 { 0xcf, "end" },
799
800 { 0xcb, "left" },
801 { 0xc8, "up" },
802 { 0xd0, "down" },
803 { 0xcd, "right" },
804
805 { 0xd2, "insert" },
806 { 0xd3, "delete" },
807 { 0, NULL },
808 };
809
810 static int get_keycode(const char *key)
811 {
812 const KeyDef *p;
813 char *endp;
814 int ret;
815
816 for(p = key_defs; p->name != NULL; p++) {
817 if (!strcmp(key, p->name))
818 return p->keycode;
819 }
820 if (strstart(key, "0x", NULL)) {
821 ret = strtoul(key, &endp, 0);
822 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
823 return ret;
824 }
825 return -1;
826 }
827
828 static void do_send_key(const char *string)
829 {
830 char keybuf[16], *q;
831 uint8_t keycodes[16];
832 const char *p;
833 int nb_keycodes, keycode, i;
834
835 nb_keycodes = 0;
836 p = string;
837 while (*p != '\0') {
838 q = keybuf;
839 while (*p != '\0' && *p != '-') {
840 if ((q - keybuf) < sizeof(keybuf) - 1) {
841 *q++ = *p;
842 }
843 p++;
844 }
845 *q = '\0';
846 keycode = get_keycode(keybuf);
847 if (keycode < 0) {
848 term_printf("unknown key: '%s'\n", keybuf);
849 return;
850 }
851 keycodes[nb_keycodes++] = keycode;
852 if (*p == '\0')
853 break;
854 p++;
855 }
856 /* key down events */
857 for(i = 0; i < nb_keycodes; i++) {
858 keycode = keycodes[i];
859 if (keycode & 0x80)
860 kbd_put_keycode(0xe0);
861 kbd_put_keycode(keycode & 0x7f);
862 }
863 /* key up events */
864 for(i = nb_keycodes - 1; i >= 0; i--) {
865 keycode = keycodes[i];
866 if (keycode & 0x80)
867 kbd_put_keycode(0xe0);
868 kbd_put_keycode(keycode | 0x80);
869 }
870 }
871
872 static int mouse_button_state;
873
874 static void do_mouse_move(const char *dx_str, const char *dy_str,
875 const char *dz_str)
876 {
877 int dx, dy, dz;
878 dx = strtol(dx_str, NULL, 0);
879 dy = strtol(dy_str, NULL, 0);
880 dz = 0;
881 if (dz_str)
882 dz = strtol(dz_str, NULL, 0);
883 kbd_mouse_event(dx, dy, dz, mouse_button_state);
884 }
885
886 static void do_mouse_button(int button_state)
887 {
888 mouse_button_state = button_state;
889 kbd_mouse_event(0, 0, 0, mouse_button_state);
890 }
891
892 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
893 {
894 uint32_t val;
895 int suffix;
896
897 if (has_index) {
898 cpu_outb(NULL, addr & 0xffff, index & 0xff);
899 addr++;
900 }
901 addr &= 0xffff;
902
903 switch(size) {
904 default:
905 case 1:
906 val = cpu_inb(NULL, addr);
907 suffix = 'b';
908 break;
909 case 2:
910 val = cpu_inw(NULL, addr);
911 suffix = 'w';
912 break;
913 case 4:
914 val = cpu_inl(NULL, addr);
915 suffix = 'l';
916 break;
917 }
918 term_printf("port%c[0x%04x] = %#0*x\n",
919 suffix, addr, size * 2, val);
920 }
921
922 static void do_system_reset(void)
923 {
924 qemu_system_reset_request();
925 }
926
927 static void do_system_powerdown(void)
928 {
929 qemu_system_powerdown_request();
930 }
931
932 #if defined(TARGET_I386)
933 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
934 {
935 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
936 addr,
937 pte & mask,
938 pte & PG_GLOBAL_MASK ? 'G' : '-',
939 pte & PG_PSE_MASK ? 'P' : '-',
940 pte & PG_DIRTY_MASK ? 'D' : '-',
941 pte & PG_ACCESSED_MASK ? 'A' : '-',
942 pte & PG_PCD_MASK ? 'C' : '-',
943 pte & PG_PWT_MASK ? 'T' : '-',
944 pte & PG_USER_MASK ? 'U' : '-',
945 pte & PG_RW_MASK ? 'W' : '-');
946 }
947
948 static void tlb_info(void)
949 {
950 CPUState *env;
951 int l1, l2;
952 uint32_t pgd, pde, pte;
953
954 env = mon_get_cpu();
955 if (!env)
956 return;
957
958 if (!(env->cr[0] & CR0_PG_MASK)) {
959 term_printf("PG disabled\n");
960 return;
961 }
962 pgd = env->cr[3] & ~0xfff;
963 for(l1 = 0; l1 < 1024; l1++) {
964 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
965 pde = le32_to_cpu(pde);
966 if (pde & PG_PRESENT_MASK) {
967 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
968 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
969 } else {
970 for(l2 = 0; l2 < 1024; l2++) {
971 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
972 (uint8_t *)&pte, 4);
973 pte = le32_to_cpu(pte);
974 if (pte & PG_PRESENT_MASK) {
975 print_pte((l1 << 22) + (l2 << 12),
976 pte & ~PG_PSE_MASK,
977 ~0xfff);
978 }
979 }
980 }
981 }
982 }
983 }
984
985 static void mem_print(uint32_t *pstart, int *plast_prot,
986 uint32_t end, int prot)
987 {
988 int prot1;
989 prot1 = *plast_prot;
990 if (prot != prot1) {
991 if (*pstart != -1) {
992 term_printf("%08x-%08x %08x %c%c%c\n",
993 *pstart, end, end - *pstart,
994 prot1 & PG_USER_MASK ? 'u' : '-',
995 'r',
996 prot1 & PG_RW_MASK ? 'w' : '-');
997 }
998 if (prot != 0)
999 *pstart = end;
1000 else
1001 *pstart = -1;
1002 *plast_prot = prot;
1003 }
1004 }
1005
1006 static void mem_info(void)
1007 {
1008 CPUState *env;
1009 int l1, l2, prot, last_prot;
1010 uint32_t pgd, pde, pte, start, end;
1011
1012 env = mon_get_cpu();
1013 if (!env)
1014 return;
1015
1016 if (!(env->cr[0] & CR0_PG_MASK)) {
1017 term_printf("PG disabled\n");
1018 return;
1019 }
1020 pgd = env->cr[3] & ~0xfff;
1021 last_prot = 0;
1022 start = -1;
1023 for(l1 = 0; l1 < 1024; l1++) {
1024 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1025 pde = le32_to_cpu(pde);
1026 end = l1 << 22;
1027 if (pde & PG_PRESENT_MASK) {
1028 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1029 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1030 mem_print(&start, &last_prot, end, prot);
1031 } else {
1032 for(l2 = 0; l2 < 1024; l2++) {
1033 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1034 (uint8_t *)&pte, 4);
1035 pte = le32_to_cpu(pte);
1036 end = (l1 << 22) + (l2 << 12);
1037 if (pte & PG_PRESENT_MASK) {
1038 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1039 } else {
1040 prot = 0;
1041 }
1042 mem_print(&start, &last_prot, end, prot);
1043 }
1044 }
1045 } else {
1046 prot = 0;
1047 mem_print(&start, &last_prot, end, prot);
1048 }
1049 }
1050 }
1051 #endif
1052
1053 static void do_info_kqemu(void)
1054 {
1055 #ifdef USE_KQEMU
1056 CPUState *env;
1057 int val;
1058 val = 0;
1059 env = mon_get_cpu();
1060 if (!env) {
1061 term_printf("No cpu initialized yet");
1062 return;
1063 }
1064 val = env->kqemu_enabled;
1065 term_printf("kqemu support: ");
1066 switch(val) {
1067 default:
1068 case 0:
1069 term_printf("disabled\n");
1070 break;
1071 case 1:
1072 term_printf("enabled for user code\n");
1073 break;
1074 case 2:
1075 term_printf("enabled for user and kernel code\n");
1076 break;
1077 }
1078 #else
1079 term_printf("kqemu support: not compiled\n");
1080 #endif
1081 }
1082
1083 #ifdef CONFIG_PROFILER
1084
1085 int64_t kqemu_time;
1086 int64_t qemu_time;
1087 int64_t kqemu_exec_count;
1088 int64_t dev_time;
1089 int64_t kqemu_ret_int_count;
1090 int64_t kqemu_ret_excp_count;
1091 int64_t kqemu_ret_intr_count;
1092
1093 static void do_info_profile(void)
1094 {
1095 int64_t total;
1096 total = qemu_time;
1097 if (total == 0)
1098 total = 1;
1099 term_printf("async time %" PRId64 " (%0.3f)\n",
1100 dev_time, dev_time / (double)ticks_per_sec);
1101 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1102 qemu_time, qemu_time / (double)ticks_per_sec);
1103 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1104 kqemu_time, kqemu_time / (double)ticks_per_sec,
1105 kqemu_time / (double)total * 100.0,
1106 kqemu_exec_count,
1107 kqemu_ret_int_count,
1108 kqemu_ret_excp_count,
1109 kqemu_ret_intr_count);
1110 qemu_time = 0;
1111 kqemu_time = 0;
1112 kqemu_exec_count = 0;
1113 dev_time = 0;
1114 kqemu_ret_int_count = 0;
1115 kqemu_ret_excp_count = 0;
1116 kqemu_ret_intr_count = 0;
1117 #ifdef USE_KQEMU
1118 kqemu_record_dump();
1119 #endif
1120 }
1121 #else
1122 static void do_info_profile(void)
1123 {
1124 term_printf("Internal profiler not compiled\n");
1125 }
1126 #endif
1127
1128 /* Capture support */
1129 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1130
1131 static void do_info_capture (void)
1132 {
1133 int i;
1134 CaptureState *s;
1135
1136 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1137 term_printf ("[%d]: ", i);
1138 s->ops.info (s->opaque);
1139 }
1140 }
1141
1142 static void do_stop_capture (int n)
1143 {
1144 int i;
1145 CaptureState *s;
1146
1147 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1148 if (i == n) {
1149 s->ops.destroy (s->opaque);
1150 LIST_REMOVE (s, entries);
1151 qemu_free (s);
1152 return;
1153 }
1154 }
1155 }
1156
1157 #ifdef HAS_AUDIO
1158 int wav_start_capture (CaptureState *s, const char *path, int freq,
1159 int bits, int nchannels);
1160
1161 static void do_wav_capture (const char *path,
1162 int has_freq, int freq,
1163 int has_bits, int bits,
1164 int has_channels, int nchannels)
1165 {
1166 CaptureState *s;
1167
1168 s = qemu_mallocz (sizeof (*s));
1169 if (!s) {
1170 term_printf ("Not enough memory to add wave capture\n");
1171 return;
1172 }
1173
1174 freq = has_freq ? freq : 44100;
1175 bits = has_bits ? bits : 16;
1176 nchannels = has_channels ? nchannels : 2;
1177
1178 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1179 term_printf ("Faied to add wave capture\n");
1180 qemu_free (s);
1181 }
1182 LIST_INSERT_HEAD (&capture_head, s, entries);
1183 }
1184 #endif
1185
1186 static term_cmd_t term_cmds[] = {
1187 { "help|?", "s?", do_help,
1188 "[cmd]", "show the help" },
1189 { "commit", "s", do_commit,
1190 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1191 { "info", "s?", do_info,
1192 "subcommand", "show various information about the system state" },
1193 { "q|quit", "", do_quit,
1194 "", "quit the emulator" },
1195 { "eject", "-fB", do_eject,
1196 "[-f] device", "eject a removable media (use -f to force it)" },
1197 { "change", "BF", do_change,
1198 "device filename", "change a removable media" },
1199 { "screendump", "F", do_screen_dump,
1200 "filename", "save screen into PPM image 'filename'" },
1201 { "log", "s", do_log,
1202 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1203 { "savevm", "s?", do_savevm,
1204 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1205 { "loadvm", "s", do_loadvm,
1206 "tag|id", "restore a VM snapshot from its tag or id" },
1207 { "delvm", "s", do_delvm,
1208 "tag|id", "delete a VM snapshot from its tag or id" },
1209 { "stop", "", do_stop,
1210 "", "stop emulation", },
1211 { "c|cont", "", do_cont,
1212 "", "resume emulation", },
1213 #ifdef CONFIG_GDBSTUB
1214 { "gdbserver", "i?", do_gdbserver,
1215 "[port]", "start gdbserver session (default port=1234)", },
1216 #endif
1217 { "x", "/l", do_memory_dump,
1218 "/fmt addr", "virtual memory dump starting at 'addr'", },
1219 { "xp", "/l", do_physical_memory_dump,
1220 "/fmt addr", "physical memory dump starting at 'addr'", },
1221 { "p|print", "/l", do_print,
1222 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1223 { "i", "/ii.", do_ioport_read,
1224 "/fmt addr", "I/O port read" },
1225
1226 { "sendkey", "s", do_send_key,
1227 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1228 { "system_reset", "", do_system_reset,
1229 "", "reset the system" },
1230 { "system_powerdown", "", do_system_powerdown,
1231 "", "send system power down event" },
1232 { "sum", "ii", do_sum,
1233 "addr size", "compute the checksum of a memory region" },
1234 { "usb_add", "s", do_usb_add,
1235 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1236 { "usb_del", "s", do_usb_del,
1237 "device", "remove USB device 'bus.addr'" },
1238 { "cpu", "i", do_cpu_set,
1239 "index", "set the default CPU" },
1240 { "mouse_move", "sss?", do_mouse_move,
1241 "dx dy [dz]", "send mouse move events" },
1242 { "mouse_button", "i", do_mouse_button,
1243 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1244 #ifdef HAS_AUDIO
1245 { "wavcapture", "si?i?i?", do_wav_capture,
1246 "path [frequency bits channels]",
1247 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1248 #endif
1249 { "stopcapture", "i", do_stop_capture,
1250 "capture index", "stop capture" },
1251 { "memsave", "lis", do_memory_save,
1252 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1253 { NULL, NULL, },
1254 };
1255
1256 static term_cmd_t info_cmds[] = {
1257 { "version", "", do_info_version,
1258 "", "show the version of qemu" },
1259 { "network", "", do_info_network,
1260 "", "show the network state" },
1261 { "block", "", do_info_block,
1262 "", "show the block devices" },
1263 { "registers", "", do_info_registers,
1264 "", "show the cpu registers" },
1265 { "cpus", "", do_info_cpus,
1266 "", "show infos for each CPU" },
1267 { "history", "", do_info_history,
1268 "", "show the command line history", },
1269 { "irq", "", irq_info,
1270 "", "show the interrupts statistics (if available)", },
1271 { "pic", "", pic_info,
1272 "", "show i8259 (PIC) state", },
1273 { "pci", "", pci_info,
1274 "", "show PCI info", },
1275 #if defined(TARGET_I386)
1276 { "tlb", "", tlb_info,
1277 "", "show virtual to physical memory mappings", },
1278 { "mem", "", mem_info,
1279 "", "show the active virtual memory mappings", },
1280 #endif
1281 { "jit", "", do_info_jit,
1282 "", "show dynamic compiler info", },
1283 { "kqemu", "", do_info_kqemu,
1284 "", "show kqemu information", },
1285 { "usb", "", usb_info,
1286 "", "show guest USB devices", },
1287 { "usbhost", "", usb_host_info,
1288 "", "show host USB devices", },
1289 { "profile", "", do_info_profile,
1290 "", "show profiling information", },
1291 { "capture", "", do_info_capture,
1292 "", "show capture information" },
1293 { "snapshots", "", do_info_snapshots,
1294 "", "show the currently saved VM snapshots" },
1295 { NULL, NULL, },
1296 };
1297
1298 /*******************************************************************/
1299
1300 static const char *pch;
1301 static jmp_buf expr_env;
1302
1303 #define MD_TLONG 0
1304 #define MD_I32 1
1305
1306 typedef struct MonitorDef {
1307 const char *name;
1308 int offset;
1309 target_long (*get_value)(struct MonitorDef *md, int val);
1310 int type;
1311 } MonitorDef;
1312
1313 #if defined(TARGET_I386)
1314 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1315 {
1316 CPUState *env = mon_get_cpu();
1317 if (!env)
1318 return 0;
1319 return env->eip + env->segs[R_CS].base;
1320 }
1321 #endif
1322
1323 #if defined(TARGET_PPC)
1324 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1325 {
1326 CPUState *env = mon_get_cpu();
1327 unsigned int u;
1328 int i;
1329
1330 if (!env)
1331 return 0;
1332
1333 u = 0;
1334 for (i = 0; i < 8; i++)
1335 u |= env->crf[i] << (32 - (4 * i));
1336
1337 return u;
1338 }
1339
1340 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1341 {
1342 CPUState *env = mon_get_cpu();
1343 if (!env)
1344 return 0;
1345 return (env->msr[MSR_POW] << MSR_POW) |
1346 (env->msr[MSR_ILE] << MSR_ILE) |
1347 (env->msr[MSR_EE] << MSR_EE) |
1348 (env->msr[MSR_PR] << MSR_PR) |
1349 (env->msr[MSR_FP] << MSR_FP) |
1350 (env->msr[MSR_ME] << MSR_ME) |
1351 (env->msr[MSR_FE0] << MSR_FE0) |
1352 (env->msr[MSR_SE] << MSR_SE) |
1353 (env->msr[MSR_BE] << MSR_BE) |
1354 (env->msr[MSR_FE1] << MSR_FE1) |
1355 (env->msr[MSR_IP] << MSR_IP) |
1356 (env->msr[MSR_IR] << MSR_IR) |
1357 (env->msr[MSR_DR] << MSR_DR) |
1358 (env->msr[MSR_RI] << MSR_RI) |
1359 (env->msr[MSR_LE] << MSR_LE);
1360 }
1361
1362 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1363 {
1364 CPUState *env = mon_get_cpu();
1365 if (!env)
1366 return 0;
1367 return (env->xer[XER_SO] << XER_SO) |
1368 (env->xer[XER_OV] << XER_OV) |
1369 (env->xer[XER_CA] << XER_CA) |
1370 (env->xer[XER_BC] << XER_BC);
1371 }
1372
1373 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1374 {
1375 CPUState *env = mon_get_cpu();
1376 if (!env)
1377 return 0;
1378 return cpu_ppc_load_decr(env);
1379 }
1380
1381 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1382 {
1383 CPUState *env = mon_get_cpu();
1384 if (!env)
1385 return 0;
1386 return cpu_ppc_load_tbu(env);
1387 }
1388
1389 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1390 {
1391 CPUState *env = mon_get_cpu();
1392 if (!env)
1393 return 0;
1394 return cpu_ppc_load_tbl(env);
1395 }
1396 #endif
1397
1398 #if defined(TARGET_SPARC)
1399 #ifndef TARGET_SPARC64
1400 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1401 {
1402 CPUState *env = mon_get_cpu();
1403 if (!env)
1404 return 0;
1405 return GET_PSR(env);
1406 }
1407 #endif
1408
1409 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1410 {
1411 CPUState *env = mon_get_cpu();
1412 if (!env)
1413 return 0;
1414 return env->regwptr[val];
1415 }
1416 #endif
1417
1418 static MonitorDef monitor_defs[] = {
1419 #ifdef TARGET_I386
1420
1421 #define SEG(name, seg) \
1422 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1423 { name ".base", offsetof(CPUState, segs[seg].base) },\
1424 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1425
1426 { "eax", offsetof(CPUState, regs[0]) },
1427 { "ecx", offsetof(CPUState, regs[1]) },
1428 { "edx", offsetof(CPUState, regs[2]) },
1429 { "ebx", offsetof(CPUState, regs[3]) },
1430 { "esp|sp", offsetof(CPUState, regs[4]) },
1431 { "ebp|fp", offsetof(CPUState, regs[5]) },
1432 { "esi", offsetof(CPUState, regs[6]) },
1433 { "edi", offsetof(CPUState, regs[7]) },
1434 #ifdef TARGET_X86_64
1435 { "r8", offsetof(CPUState, regs[8]) },
1436 { "r9", offsetof(CPUState, regs[9]) },
1437 { "r10", offsetof(CPUState, regs[10]) },
1438 { "r11", offsetof(CPUState, regs[11]) },
1439 { "r12", offsetof(CPUState, regs[12]) },
1440 { "r13", offsetof(CPUState, regs[13]) },
1441 { "r14", offsetof(CPUState, regs[14]) },
1442 { "r15", offsetof(CPUState, regs[15]) },
1443 #endif
1444 { "eflags", offsetof(CPUState, eflags) },
1445 { "eip", offsetof(CPUState, eip) },
1446 SEG("cs", R_CS)
1447 SEG("ds", R_DS)
1448 SEG("es", R_ES)
1449 SEG("ss", R_SS)
1450 SEG("fs", R_FS)
1451 SEG("gs", R_GS)
1452 { "pc", 0, monitor_get_pc, },
1453 #elif defined(TARGET_PPC)
1454 { "r0", offsetof(CPUState, gpr[0]) },
1455 { "r1", offsetof(CPUState, gpr[1]) },
1456 { "r2", offsetof(CPUState, gpr[2]) },
1457 { "r3", offsetof(CPUState, gpr[3]) },
1458 { "r4", offsetof(CPUState, gpr[4]) },
1459 { "r5", offsetof(CPUState, gpr[5]) },
1460 { "r6", offsetof(CPUState, gpr[6]) },
1461 { "r7", offsetof(CPUState, gpr[7]) },
1462 { "r8", offsetof(CPUState, gpr[8]) },
1463 { "r9", offsetof(CPUState, gpr[9]) },
1464 { "r10", offsetof(CPUState, gpr[10]) },
1465 { "r11", offsetof(CPUState, gpr[11]) },
1466 { "r12", offsetof(CPUState, gpr[12]) },
1467 { "r13", offsetof(CPUState, gpr[13]) },
1468 { "r14", offsetof(CPUState, gpr[14]) },
1469 { "r15", offsetof(CPUState, gpr[15]) },
1470 { "r16", offsetof(CPUState, gpr[16]) },
1471 { "r17", offsetof(CPUState, gpr[17]) },
1472 { "r18", offsetof(CPUState, gpr[18]) },
1473 { "r19", offsetof(CPUState, gpr[19]) },
1474 { "r20", offsetof(CPUState, gpr[20]) },
1475 { "r21", offsetof(CPUState, gpr[21]) },
1476 { "r22", offsetof(CPUState, gpr[22]) },
1477 { "r23", offsetof(CPUState, gpr[23]) },
1478 { "r24", offsetof(CPUState, gpr[24]) },
1479 { "r25", offsetof(CPUState, gpr[25]) },
1480 { "r26", offsetof(CPUState, gpr[26]) },
1481 { "r27", offsetof(CPUState, gpr[27]) },
1482 { "r28", offsetof(CPUState, gpr[28]) },
1483 { "r29", offsetof(CPUState, gpr[29]) },
1484 { "r30", offsetof(CPUState, gpr[30]) },
1485 { "r31", offsetof(CPUState, gpr[31]) },
1486 { "nip|pc", offsetof(CPUState, nip) },
1487 { "lr", offsetof(CPUState, lr) },
1488 { "ctr", offsetof(CPUState, ctr) },
1489 { "decr", 0, &monitor_get_decr, },
1490 { "ccr", 0, &monitor_get_ccr, },
1491 { "msr", 0, &monitor_get_msr, },
1492 { "xer", 0, &monitor_get_xer, },
1493 { "tbu", 0, &monitor_get_tbu, },
1494 { "tbl", 0, &monitor_get_tbl, },
1495 { "sdr1", offsetof(CPUState, sdr1) },
1496 { "sr0", offsetof(CPUState, sr[0]) },
1497 { "sr1", offsetof(CPUState, sr[1]) },
1498 { "sr2", offsetof(CPUState, sr[2]) },
1499 { "sr3", offsetof(CPUState, sr[3]) },
1500 { "sr4", offsetof(CPUState, sr[4]) },
1501 { "sr5", offsetof(CPUState, sr[5]) },
1502 { "sr6", offsetof(CPUState, sr[6]) },
1503 { "sr7", offsetof(CPUState, sr[7]) },
1504 { "sr8", offsetof(CPUState, sr[8]) },
1505 { "sr9", offsetof(CPUState, sr[9]) },
1506 { "sr10", offsetof(CPUState, sr[10]) },
1507 { "sr11", offsetof(CPUState, sr[11]) },
1508 { "sr12", offsetof(CPUState, sr[12]) },
1509 { "sr13", offsetof(CPUState, sr[13]) },
1510 { "sr14", offsetof(CPUState, sr[14]) },
1511 { "sr15", offsetof(CPUState, sr[15]) },
1512 /* Too lazy to put BATs and SPRs ... */
1513 #elif defined(TARGET_SPARC)
1514 { "g0", offsetof(CPUState, gregs[0]) },
1515 { "g1", offsetof(CPUState, gregs[1]) },
1516 { "g2", offsetof(CPUState, gregs[2]) },
1517 { "g3", offsetof(CPUState, gregs[3]) },
1518 { "g4", offsetof(CPUState, gregs[4]) },
1519 { "g5", offsetof(CPUState, gregs[5]) },
1520 { "g6", offsetof(CPUState, gregs[6]) },
1521 { "g7", offsetof(CPUState, gregs[7]) },
1522 { "o0", 0, monitor_get_reg },
1523 { "o1", 1, monitor_get_reg },
1524 { "o2", 2, monitor_get_reg },
1525 { "o3", 3, monitor_get_reg },
1526 { "o4", 4, monitor_get_reg },
1527 { "o5", 5, monitor_get_reg },
1528 { "o6", 6, monitor_get_reg },
1529 { "o7", 7, monitor_get_reg },
1530 { "l0", 8, monitor_get_reg },
1531 { "l1", 9, monitor_get_reg },
1532 { "l2", 10, monitor_get_reg },
1533 { "l3", 11, monitor_get_reg },
1534 { "l4", 12, monitor_get_reg },
1535 { "l5", 13, monitor_get_reg },
1536 { "l6", 14, monitor_get_reg },
1537 { "l7", 15, monitor_get_reg },
1538 { "i0", 16, monitor_get_reg },
1539 { "i1", 17, monitor_get_reg },
1540 { "i2", 18, monitor_get_reg },
1541 { "i3", 19, monitor_get_reg },
1542 { "i4", 20, monitor_get_reg },
1543 { "i5", 21, monitor_get_reg },
1544 { "i6", 22, monitor_get_reg },
1545 { "i7", 23, monitor_get_reg },
1546 { "pc", offsetof(CPUState, pc) },
1547 { "npc", offsetof(CPUState, npc) },
1548 { "y", offsetof(CPUState, y) },
1549 #ifndef TARGET_SPARC64
1550 { "psr", 0, &monitor_get_psr, },
1551 { "wim", offsetof(CPUState, wim) },
1552 #endif
1553 { "tbr", offsetof(CPUState, tbr) },
1554 { "fsr", offsetof(CPUState, fsr) },
1555 { "f0", offsetof(CPUState, fpr[0]) },
1556 { "f1", offsetof(CPUState, fpr[1]) },
1557 { "f2", offsetof(CPUState, fpr[2]) },
1558 { "f3", offsetof(CPUState, fpr[3]) },
1559 { "f4", offsetof(CPUState, fpr[4]) },
1560 { "f5", offsetof(CPUState, fpr[5]) },
1561 { "f6", offsetof(CPUState, fpr[6]) },
1562 { "f7", offsetof(CPUState, fpr[7]) },
1563 { "f8", offsetof(CPUState, fpr[8]) },
1564 { "f9", offsetof(CPUState, fpr[9]) },
1565 { "f10", offsetof(CPUState, fpr[10]) },
1566 { "f11", offsetof(CPUState, fpr[11]) },
1567 { "f12", offsetof(CPUState, fpr[12]) },
1568 { "f13", offsetof(CPUState, fpr[13]) },
1569 { "f14", offsetof(CPUState, fpr[14]) },
1570 { "f15", offsetof(CPUState, fpr[15]) },
1571 { "f16", offsetof(CPUState, fpr[16]) },
1572 { "f17", offsetof(CPUState, fpr[17]) },
1573 { "f18", offsetof(CPUState, fpr[18]) },
1574 { "f19", offsetof(CPUState, fpr[19]) },
1575 { "f20", offsetof(CPUState, fpr[20]) },
1576 { "f21", offsetof(CPUState, fpr[21]) },
1577 { "f22", offsetof(CPUState, fpr[22]) },
1578 { "f23", offsetof(CPUState, fpr[23]) },
1579 { "f24", offsetof(CPUState, fpr[24]) },
1580 { "f25", offsetof(CPUState, fpr[25]) },
1581 { "f26", offsetof(CPUState, fpr[26]) },
1582 { "f27", offsetof(CPUState, fpr[27]) },
1583 { "f28", offsetof(CPUState, fpr[28]) },
1584 { "f29", offsetof(CPUState, fpr[29]) },
1585 { "f30", offsetof(CPUState, fpr[30]) },
1586 { "f31", offsetof(CPUState, fpr[31]) },
1587 #ifdef TARGET_SPARC64
1588 { "f32", offsetof(CPUState, fpr[32]) },
1589 { "f34", offsetof(CPUState, fpr[34]) },
1590 { "f36", offsetof(CPUState, fpr[36]) },
1591 { "f38", offsetof(CPUState, fpr[38]) },
1592 { "f40", offsetof(CPUState, fpr[40]) },
1593 { "f42", offsetof(CPUState, fpr[42]) },
1594 { "f44", offsetof(CPUState, fpr[44]) },
1595 { "f46", offsetof(CPUState, fpr[46]) },
1596 { "f48", offsetof(CPUState, fpr[48]) },
1597 { "f50", offsetof(CPUState, fpr[50]) },
1598 { "f52", offsetof(CPUState, fpr[52]) },
1599 { "f54", offsetof(CPUState, fpr[54]) },
1600 { "f56", offsetof(CPUState, fpr[56]) },
1601 { "f58", offsetof(CPUState, fpr[58]) },
1602 { "f60", offsetof(CPUState, fpr[60]) },
1603 { "f62", offsetof(CPUState, fpr[62]) },
1604 { "asi", offsetof(CPUState, asi) },
1605 { "pstate", offsetof(CPUState, pstate) },
1606 { "cansave", offsetof(CPUState, cansave) },
1607 { "canrestore", offsetof(CPUState, canrestore) },
1608 { "otherwin", offsetof(CPUState, otherwin) },
1609 { "wstate", offsetof(CPUState, wstate) },
1610 { "cleanwin", offsetof(CPUState, cleanwin) },
1611 { "fprs", offsetof(CPUState, fprs) },
1612 #endif
1613 #endif
1614 { NULL },
1615 };
1616
1617 static void expr_error(const char *fmt)
1618 {
1619 term_printf(fmt);
1620 term_printf("\n");
1621 longjmp(expr_env, 1);
1622 }
1623
1624 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1625 static int get_monitor_def(target_long *pval, const char *name)
1626 {
1627 MonitorDef *md;
1628 void *ptr;
1629
1630 for(md = monitor_defs; md->name != NULL; md++) {
1631 if (compare_cmd(name, md->name)) {
1632 if (md->get_value) {
1633 *pval = md->get_value(md, md->offset);
1634 } else {
1635 CPUState *env = mon_get_cpu();
1636 if (!env)
1637 return -2;
1638 ptr = (uint8_t *)env + md->offset;
1639 switch(md->type) {
1640 case MD_I32:
1641 *pval = *(int32_t *)ptr;
1642 break;
1643 case MD_TLONG:
1644 *pval = *(target_long *)ptr;
1645 break;
1646 default:
1647 *pval = 0;
1648 break;
1649 }
1650 }
1651 return 0;
1652 }
1653 }
1654 return -1;
1655 }
1656
1657 static void next(void)
1658 {
1659 if (pch != '\0') {
1660 pch++;
1661 while (isspace(*pch))
1662 pch++;
1663 }
1664 }
1665
1666 static target_long expr_sum(void);
1667
1668 static target_long expr_unary(void)
1669 {
1670 target_long n;
1671 char *p;
1672 int ret;
1673
1674 switch(*pch) {
1675 case '+':
1676 next();
1677 n = expr_unary();
1678 break;
1679 case '-':
1680 next();
1681 n = -expr_unary();
1682 break;
1683 case '~':
1684 next();
1685 n = ~expr_unary();
1686 break;
1687 case '(':
1688 next();
1689 n = expr_sum();
1690 if (*pch != ')') {
1691 expr_error("')' expected");
1692 }
1693 next();
1694 break;
1695 case '\'':
1696 pch++;
1697 if (*pch == '\0')
1698 expr_error("character constant expected");
1699 n = *pch;
1700 pch++;
1701 if (*pch != '\'')
1702 expr_error("missing terminating \' character");
1703 next();
1704 break;
1705 case '$':
1706 {
1707 char buf[128], *q;
1708
1709 pch++;
1710 q = buf;
1711 while ((*pch >= 'a' && *pch <= 'z') ||
1712 (*pch >= 'A' && *pch <= 'Z') ||
1713 (*pch >= '0' && *pch <= '9') ||
1714 *pch == '_' || *pch == '.') {
1715 if ((q - buf) < sizeof(buf) - 1)
1716 *q++ = *pch;
1717 pch++;
1718 }
1719 while (isspace(*pch))
1720 pch++;
1721 *q = 0;
1722 ret = get_monitor_def(&n, buf);
1723 if (ret == -1)
1724 expr_error("unknown register");
1725 else if (ret == -2)
1726 expr_error("no cpu defined");
1727 }
1728 break;
1729 case '\0':
1730 expr_error("unexpected end of expression");
1731 n = 0;
1732 break;
1733 default:
1734 #if TARGET_LONG_BITS == 64
1735 n = strtoull(pch, &p, 0);
1736 #else
1737 n = strtoul(pch, &p, 0);
1738 #endif
1739 if (pch == p) {
1740 expr_error("invalid char in expression");
1741 }
1742 pch = p;
1743 while (isspace(*pch))
1744 pch++;
1745 break;
1746 }
1747 return n;
1748 }
1749
1750
1751 static target_long expr_prod(void)
1752 {
1753 target_long val, val2;
1754 int op;
1755
1756 val = expr_unary();
1757 for(;;) {
1758 op = *pch;
1759 if (op != '*' && op != '/' && op != '%')
1760 break;
1761 next();
1762 val2 = expr_unary();
1763 switch(op) {
1764 default:
1765 case '*':
1766 val *= val2;
1767 break;
1768 case '/':
1769 case '%':
1770 if (val2 == 0)
1771 expr_error("division by zero");
1772 if (op == '/')
1773 val /= val2;
1774 else
1775 val %= val2;
1776 break;
1777 }
1778 }
1779 return val;
1780 }
1781
1782 static target_long expr_logic(void)
1783 {
1784 target_long val, val2;
1785 int op;
1786
1787 val = expr_prod();
1788 for(;;) {
1789 op = *pch;
1790 if (op != '&' && op != '|' && op != '^')
1791 break;
1792 next();
1793 val2 = expr_prod();
1794 switch(op) {
1795 default:
1796 case '&':
1797 val &= val2;
1798 break;
1799 case '|':
1800 val |= val2;
1801 break;
1802 case '^':
1803 val ^= val2;
1804 break;
1805 }
1806 }
1807 return val;
1808 }
1809
1810 static target_long expr_sum(void)
1811 {
1812 target_long val, val2;
1813 int op;
1814
1815 val = expr_logic();
1816 for(;;) {
1817 op = *pch;
1818 if (op != '+' && op != '-')
1819 break;
1820 next();
1821 val2 = expr_logic();
1822 if (op == '+')
1823 val += val2;
1824 else
1825 val -= val2;
1826 }
1827 return val;
1828 }
1829
1830 static int get_expr(target_long *pval, const char **pp)
1831 {
1832 pch = *pp;
1833 if (setjmp(expr_env)) {
1834 *pp = pch;
1835 return -1;
1836 }
1837 while (isspace(*pch))
1838 pch++;
1839 *pval = expr_sum();
1840 *pp = pch;
1841 return 0;
1842 }
1843
1844 static int get_str(char *buf, int buf_size, const char **pp)
1845 {
1846 const char *p;
1847 char *q;
1848 int c;
1849
1850 q = buf;
1851 p = *pp;
1852 while (isspace(*p))
1853 p++;
1854 if (*p == '\0') {
1855 fail:
1856 *q = '\0';
1857 *pp = p;
1858 return -1;
1859 }
1860 if (*p == '\"') {
1861 p++;
1862 while (*p != '\0' && *p != '\"') {
1863 if (*p == '\\') {
1864 p++;
1865 c = *p++;
1866 switch(c) {
1867 case 'n':
1868 c = '\n';
1869 break;
1870 case 'r':
1871 c = '\r';
1872 break;
1873 case '\\':
1874 case '\'':
1875 case '\"':
1876 break;
1877 default:
1878 qemu_printf("unsupported escape code: '\\%c'\n", c);
1879 goto fail;
1880 }
1881 if ((q - buf) < buf_size - 1) {
1882 *q++ = c;
1883 }
1884 } else {
1885 if ((q - buf) < buf_size - 1) {
1886 *q++ = *p;
1887 }
1888 p++;
1889 }
1890 }
1891 if (*p != '\"') {
1892 qemu_printf("unterminated string\n");
1893 goto fail;
1894 }
1895 p++;
1896 } else {
1897 while (*p != '\0' && !isspace(*p)) {
1898 if ((q - buf) < buf_size - 1) {
1899 *q++ = *p;
1900 }
1901 p++;
1902 }
1903 }
1904 *q = '\0';
1905 *pp = p;
1906 return 0;
1907 }
1908
1909 static int default_fmt_format = 'x';
1910 static int default_fmt_size = 4;
1911
1912 #define MAX_ARGS 16
1913
1914 static void monitor_handle_command(const char *cmdline)
1915 {
1916 const char *p, *pstart, *typestr;
1917 char *q;
1918 int c, nb_args, len, i, has_arg;
1919 term_cmd_t *cmd;
1920 char cmdname[256];
1921 char buf[1024];
1922 void *str_allocated[MAX_ARGS];
1923 void *args[MAX_ARGS];
1924
1925 #ifdef DEBUG
1926 term_printf("command='%s'\n", cmdline);
1927 #endif
1928
1929 /* extract the command name */
1930 p = cmdline;
1931 q = cmdname;
1932 while (isspace(*p))
1933 p++;
1934 if (*p == '\0')
1935 return;
1936 pstart = p;
1937 while (*p != '\0' && *p != '/' && !isspace(*p))
1938 p++;
1939 len = p - pstart;
1940 if (len > sizeof(cmdname) - 1)
1941 len = sizeof(cmdname) - 1;
1942 memcpy(cmdname, pstart, len);
1943 cmdname[len] = '\0';
1944
1945 /* find the command */
1946 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1947 if (compare_cmd(cmdname, cmd->name))
1948 goto found;
1949 }
1950 term_printf("unknown command: '%s'\n", cmdname);
1951 return;
1952 found:
1953
1954 for(i = 0; i < MAX_ARGS; i++)
1955 str_allocated[i] = NULL;
1956
1957 /* parse the parameters */
1958 typestr = cmd->args_type;
1959 nb_args = 0;
1960 for(;;) {
1961 c = *typestr;
1962 if (c == '\0')
1963 break;
1964 typestr++;
1965 switch(c) {
1966 case 'F':
1967 case 'B':
1968 case 's':
1969 {
1970 int ret;
1971 char *str;
1972
1973 while (isspace(*p))
1974 p++;
1975 if (*typestr == '?') {
1976 typestr++;
1977 if (*p == '\0') {
1978 /* no optional string: NULL argument */
1979 str = NULL;
1980 goto add_str;
1981 }
1982 }
1983 ret = get_str(buf, sizeof(buf), &p);
1984 if (ret < 0) {
1985 switch(c) {
1986 case 'F':
1987 term_printf("%s: filename expected\n", cmdname);
1988 break;
1989 case 'B':
1990 term_printf("%s: block device name expected\n", cmdname);
1991 break;
1992 default:
1993 term_printf("%s: string expected\n", cmdname);
1994 break;
1995 }
1996 goto fail;
1997 }
1998 str = qemu_malloc(strlen(buf) + 1);
1999 strcpy(str, buf);
2000 str_allocated[nb_args] = str;
2001 add_str:
2002 if (nb_args >= MAX_ARGS) {
2003 error_args:
2004 term_printf("%s: too many arguments\n", cmdname);
2005 goto fail;
2006 }
2007 args[nb_args++] = str;
2008 }
2009 break;
2010 case '/':
2011 {
2012 int count, format, size;
2013
2014 while (isspace(*p))
2015 p++;
2016 if (*p == '/') {
2017 /* format found */
2018 p++;
2019 count = 1;
2020 if (isdigit(*p)) {
2021 count = 0;
2022 while (isdigit(*p)) {
2023 count = count * 10 + (*p - '0');
2024 p++;
2025 }
2026 }
2027 size = -1;
2028 format = -1;
2029 for(;;) {
2030 switch(*p) {
2031 case 'o':
2032 case 'd':
2033 case 'u':
2034 case 'x':
2035 case 'i':
2036 case 'c':
2037 format = *p++;
2038 break;
2039 case 'b':
2040 size = 1;
2041 p++;
2042 break;
2043 case 'h':
2044 size = 2;
2045 p++;
2046 break;
2047 case 'w':
2048 size = 4;
2049 p++;
2050 break;
2051 case 'g':
2052 case 'L':
2053 size = 8;
2054 p++;
2055 break;
2056 default:
2057 goto next;
2058 }
2059 }
2060 next:
2061 if (*p != '\0' && !isspace(*p)) {
2062 term_printf("invalid char in format: '%c'\n", *p);
2063 goto fail;
2064 }
2065 if (format < 0)
2066 format = default_fmt_format;
2067 if (format != 'i') {
2068 /* for 'i', not specifying a size gives -1 as size */
2069 if (size < 0)
2070 size = default_fmt_size;
2071 }
2072 default_fmt_size = size;
2073 default_fmt_format = format;
2074 } else {
2075 count = 1;
2076 format = default_fmt_format;
2077 if (format != 'i') {
2078 size = default_fmt_size;
2079 } else {
2080 size = -1;
2081 }
2082 }
2083 if (nb_args + 3 > MAX_ARGS)
2084 goto error_args;
2085 args[nb_args++] = (void*)count;
2086 args[nb_args++] = (void*)format;
2087 args[nb_args++] = (void*)size;
2088 }
2089 break;
2090 case 'i':
2091 case 'l':
2092 {
2093 target_long val;
2094 while (isspace(*p))
2095 p++;
2096 if (*typestr == '?' || *typestr == '.') {
2097 if (*typestr == '?') {
2098 if (*p == '\0')
2099 has_arg = 0;
2100 else
2101 has_arg = 1;
2102 } else {
2103 if (*p == '.') {
2104 p++;
2105 while (isspace(*p))
2106 p++;
2107 has_arg = 1;
2108 } else {
2109 has_arg = 0;
2110 }
2111 }
2112 typestr++;
2113 if (nb_args >= MAX_ARGS)
2114 goto error_args;
2115 args[nb_args++] = (void *)has_arg;
2116 if (!has_arg) {
2117 if (nb_args >= MAX_ARGS)
2118 goto error_args;
2119 val = -1;
2120 goto add_num;
2121 }
2122 }
2123 if (get_expr(&val, &p))
2124 goto fail;
2125 add_num:
2126 if (c == 'i') {
2127 if (nb_args >= MAX_ARGS)
2128 goto error_args;
2129 args[nb_args++] = (void *)(int)val;
2130 } else {
2131 if ((nb_args + 1) >= MAX_ARGS)
2132 goto error_args;
2133 #if TARGET_LONG_BITS == 64
2134 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2135 #else
2136 args[nb_args++] = (void *)0;
2137 #endif
2138 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2139 }
2140 }
2141 break;
2142 case '-':
2143 {
2144 int has_option;
2145 /* option */
2146
2147 c = *typestr++;
2148 if (c == '\0')
2149 goto bad_type;
2150 while (isspace(*p))
2151 p++;
2152 has_option = 0;
2153 if (*p == '-') {
2154 p++;
2155 if (*p != c) {
2156 term_printf("%s: unsupported option -%c\n",
2157 cmdname, *p);
2158 goto fail;
2159 }
2160 p++;
2161 has_option = 1;
2162 }
2163 if (nb_args >= MAX_ARGS)
2164 goto error_args;
2165 args[nb_args++] = (void *)has_option;
2166 }
2167 break;
2168 default:
2169 bad_type:
2170 term_printf("%s: unknown type '%c'\n", cmdname, c);
2171 goto fail;
2172 }
2173 }
2174 /* check that all arguments were parsed */
2175 while (isspace(*p))
2176 p++;
2177 if (*p != '\0') {
2178 term_printf("%s: extraneous characters at the end of line\n",
2179 cmdname);
2180 goto fail;
2181 }
2182
2183 switch(nb_args) {
2184 case 0:
2185 cmd->handler();
2186 break;
2187 case 1:
2188 cmd->handler(args[0]);
2189 break;
2190 case 2:
2191 cmd->handler(args[0], args[1]);
2192 break;
2193 case 3:
2194 cmd->handler(args[0], args[1], args[2]);
2195 break;
2196 case 4:
2197 cmd->handler(args[0], args[1], args[2], args[3]);
2198 break;
2199 case 5:
2200 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2201 break;
2202 case 6:
2203 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2204 break;
2205 case 7:
2206 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2207 break;
2208 default:
2209 term_printf("unsupported number of arguments: %d\n", nb_args);
2210 goto fail;
2211 }
2212 fail:
2213 for(i = 0; i < MAX_ARGS; i++)
2214 qemu_free(str_allocated[i]);
2215 return;
2216 }
2217
2218 static void cmd_completion(const char *name, const char *list)
2219 {
2220 const char *p, *pstart;
2221 char cmd[128];
2222 int len;
2223
2224 p = list;
2225 for(;;) {
2226 pstart = p;
2227 p = strchr(p, '|');
2228 if (!p)
2229 p = pstart + strlen(pstart);
2230 len = p - pstart;
2231 if (len > sizeof(cmd) - 2)
2232 len = sizeof(cmd) - 2;
2233 memcpy(cmd, pstart, len);
2234 cmd[len] = '\0';
2235 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2236 add_completion(cmd);
2237 }
2238 if (*p == '\0')
2239 break;
2240 p++;
2241 }
2242 }
2243
2244 static void file_completion(const char *input)
2245 {
2246 DIR *ffs;
2247 struct dirent *d;
2248 char path[1024];
2249 char file[1024], file_prefix[1024];
2250 int input_path_len;
2251 const char *p;
2252
2253 p = strrchr(input, '/');
2254 if (!p) {
2255 input_path_len = 0;
2256 pstrcpy(file_prefix, sizeof(file_prefix), input);
2257 strcpy(path, ".");
2258 } else {
2259 input_path_len = p - input + 1;
2260 memcpy(path, input, input_path_len);
2261 if (input_path_len > sizeof(path) - 1)
2262 input_path_len = sizeof(path) - 1;
2263 path[input_path_len] = '\0';
2264 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2265 }
2266 #ifdef DEBUG_COMPLETION
2267 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2268 #endif
2269 ffs = opendir(path);
2270 if (!ffs)
2271 return;
2272 for(;;) {
2273 struct stat sb;
2274 d = readdir(ffs);
2275 if (!d)
2276 break;
2277 if (strstart(d->d_name, file_prefix, NULL)) {
2278 memcpy(file, input, input_path_len);
2279 strcpy(file + input_path_len, d->d_name);
2280 /* stat the file to find out if it's a directory.
2281 * In that case add a slash to speed up typing long paths
2282 */
2283 stat(file, &sb);
2284 if(S_ISDIR(sb.st_mode))
2285 strcat(file, "/");
2286 add_completion(file);
2287 }
2288 }
2289 closedir(ffs);
2290 }
2291
2292 static void block_completion_it(void *opaque, const char *name)
2293 {
2294 const char *input = opaque;
2295
2296 if (input[0] == '\0' ||
2297 !strncmp(name, (char *)input, strlen(input))) {
2298 add_completion(name);
2299 }
2300 }
2301
2302 /* NOTE: this parser is an approximate form of the real command parser */
2303 static void parse_cmdline(const char *cmdline,
2304 int *pnb_args, char **args)
2305 {
2306 const char *p;
2307 int nb_args, ret;
2308 char buf[1024];
2309
2310 p = cmdline;
2311 nb_args = 0;
2312 for(;;) {
2313 while (isspace(*p))
2314 p++;
2315 if (*p == '\0')
2316 break;
2317 if (nb_args >= MAX_ARGS)
2318 break;
2319 ret = get_str(buf, sizeof(buf), &p);
2320 args[nb_args] = qemu_strdup(buf);
2321 nb_args++;
2322 if (ret < 0)
2323 break;
2324 }
2325 *pnb_args = nb_args;
2326 }
2327
2328 void readline_find_completion(const char *cmdline)
2329 {
2330 const char *cmdname;
2331 char *args[MAX_ARGS];
2332 int nb_args, i, len;
2333 const char *ptype, *str;
2334 term_cmd_t *cmd;
2335 const KeyDef *key;
2336
2337 parse_cmdline(cmdline, &nb_args, args);
2338 #ifdef DEBUG_COMPLETION
2339 for(i = 0; i < nb_args; i++) {
2340 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2341 }
2342 #endif
2343
2344 /* if the line ends with a space, it means we want to complete the
2345 next arg */
2346 len = strlen(cmdline);
2347 if (len > 0 && isspace(cmdline[len - 1])) {
2348 if (nb_args >= MAX_ARGS)
2349 return;
2350 args[nb_args++] = qemu_strdup("");
2351 }
2352 if (nb_args <= 1) {
2353 /* command completion */
2354 if (nb_args == 0)
2355 cmdname = "";
2356 else
2357 cmdname = args[0];
2358 completion_index = strlen(cmdname);
2359 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2360 cmd_completion(cmdname, cmd->name);
2361 }
2362 } else {
2363 /* find the command */
2364 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2365 if (compare_cmd(args[0], cmd->name))
2366 goto found;
2367 }
2368 return;
2369 found:
2370 ptype = cmd->args_type;
2371 for(i = 0; i < nb_args - 2; i++) {
2372 if (*ptype != '\0') {
2373 ptype++;
2374 while (*ptype == '?')
2375 ptype++;
2376 }
2377 }
2378 str = args[nb_args - 1];
2379 switch(*ptype) {
2380 case 'F':
2381 /* file completion */
2382 completion_index = strlen(str);
2383 file_completion(str);
2384 break;
2385 case 'B':
2386 /* block device name completion */
2387 completion_index = strlen(str);
2388 bdrv_iterate(block_completion_it, (void *)str);
2389 break;
2390 case 's':
2391 /* XXX: more generic ? */
2392 if (!strcmp(cmd->name, "info")) {
2393 completion_index = strlen(str);
2394 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2395 cmd_completion(str, cmd->name);
2396 }
2397 } else if (!strcmp(cmd->name, "sendkey")) {
2398 completion_index = strlen(str);
2399 for(key = key_defs; key->name != NULL; key++) {
2400 cmd_completion(str, key->name);
2401 }
2402 }
2403 break;
2404 default:
2405 break;
2406 }
2407 }
2408 for(i = 0; i < nb_args; i++)
2409 qemu_free(args[i]);
2410 }
2411
2412 static int term_can_read(void *opaque)
2413 {
2414 return 128;
2415 }
2416
2417 static void term_read(void *opaque, const uint8_t *buf, int size)
2418 {
2419 int i;
2420 for(i = 0; i < size; i++)
2421 readline_handle_byte(buf[i]);
2422 }
2423
2424 static void monitor_start_input(void);
2425
2426 static void monitor_handle_command1(void *opaque, const char *cmdline)
2427 {
2428 monitor_handle_command(cmdline);
2429 monitor_start_input();
2430 }
2431
2432 static void monitor_start_input(void)
2433 {
2434 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2435 }
2436
2437 void monitor_init(CharDriverState *hd, int show_banner)
2438 {
2439 monitor_hd = hd;
2440 if (show_banner) {
2441 term_printf("QEMU %s monitor - type 'help' for more information\n",
2442 QEMU_VERSION);
2443 }
2444 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2445 monitor_start_input();
2446 }
2447
2448 /* XXX: use threads ? */
2449 /* modal monitor readline */
2450 static int monitor_readline_started;
2451 static char *monitor_readline_buf;
2452 static int monitor_readline_buf_size;
2453
2454 static void monitor_readline_cb(void *opaque, const char *input)
2455 {
2456 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2457 monitor_readline_started = 0;
2458 }
2459
2460 void monitor_readline(const char *prompt, int is_password,
2461 char *buf, int buf_size)
2462 {
2463 if (is_password) {
2464 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2465 }
2466 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2467 monitor_readline_buf = buf;
2468 monitor_readline_buf_size = buf_size;
2469 monitor_readline_started = 1;
2470 while (monitor_readline_started) {
2471 main_loop_wait(10);
2472 }
2473 }