ia64/xen-unstable

view tools/ioemu/monitor.c @ 15841:c5f735271e22

[IA64] Foreign p2m: Fix vti domain builder.

It should set arch_domain::convmem_end.

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