kvm: configure: run kernel configure even with --with-patched-kernel
[qemu-kvm/fedora.git] / monitor.c
blobab5de15a9ac359ab46b78b5f33bfb8ebd28083b1
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <dirent.h>
25 #include "hw/hw.h"
26 #include "hw/usb.h"
27 #include "hw/pcmcia.h"
28 #include "hw/pc.h"
29 #include "hw/pci.h"
30 #include "gdbstub.h"
31 #include "net.h"
32 #include "qemu-char.h"
33 #include "sysemu.h"
34 #include "monitor.h"
35 #include "readline.h"
36 #include "console.h"
37 #include "block.h"
38 #include "audio/audio.h"
39 #include "disas.h"
40 #include "balloon.h"
41 #include "qemu-timer.h"
42 #include "migration.h"
43 #include "kvm.h"
44 #include "acl.h"
46 #include "qemu-kvm.h"
48 //#define DEBUG
49 //#define DEBUG_COMPLETION
52 * Supported types:
54 * 'F' filename
55 * 'B' block device name
56 * 's' string (accept optional quote)
57 * 'i' 32 bit integer
58 * 'l' target long (32 or 64 bit)
59 * '/' optional gdb-like print format (like "/10x")
61 * '?' optional type (for 'F', 's' and 'i')
65 typedef struct mon_cmd_t {
66 const char *name;
67 const char *args_type;
68 void *handler;
69 const char *params;
70 const char *help;
71 } mon_cmd_t;
73 struct Monitor {
74 CharDriverState *chr;
75 int flags;
76 int suspend_cnt;
77 uint8_t outbuf[1024];
78 int outbuf_index;
79 ReadLineState *rs;
80 CPUState *mon_cpu;
81 BlockDriverCompletionFunc *password_completion_cb;
82 void *password_opaque;
83 LIST_ENTRY(Monitor) entry;
86 static LIST_HEAD(mon_list, Monitor) mon_list;
88 static const mon_cmd_t mon_cmds[];
89 static const mon_cmd_t info_cmds[];
91 Monitor *cur_mon = NULL;
93 static void monitor_command_cb(Monitor *mon, const char *cmdline,
94 void *opaque);
96 static void monitor_read_command(Monitor *mon, int show_prompt)
98 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
99 if (show_prompt)
100 readline_show_prompt(mon->rs);
103 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
104 void *opaque)
106 if (mon->rs) {
107 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
108 /* prompt is printed on return from the command handler */
109 return 0;
110 } else {
111 monitor_printf(mon, "terminal does not support password prompting\n");
112 return -ENOTTY;
116 void monitor_flush(Monitor *mon)
118 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
119 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
120 mon->outbuf_index = 0;
124 /* flush at every end of line or if the buffer is full */
125 static void monitor_puts(Monitor *mon, const char *str)
127 char c;
129 if (!mon)
130 return;
132 for(;;) {
133 c = *str++;
134 if (c == '\0')
135 break;
136 if (c == '\n')
137 mon->outbuf[mon->outbuf_index++] = '\r';
138 mon->outbuf[mon->outbuf_index++] = c;
139 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
140 || c == '\n')
141 monitor_flush(mon);
145 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
147 char buf[4096];
148 vsnprintf(buf, sizeof(buf), fmt, ap);
149 monitor_puts(mon, buf);
152 void monitor_printf(Monitor *mon, const char *fmt, ...)
154 va_list ap;
155 va_start(ap, fmt);
156 monitor_vprintf(mon, fmt, ap);
157 va_end(ap);
160 void monitor_print_filename(Monitor *mon, const char *filename)
162 int i;
164 for (i = 0; filename[i]; i++) {
165 switch (filename[i]) {
166 case ' ':
167 case '"':
168 case '\\':
169 monitor_printf(mon, "\\%c", filename[i]);
170 break;
171 case '\t':
172 monitor_printf(mon, "\\t");
173 break;
174 case '\r':
175 monitor_printf(mon, "\\r");
176 break;
177 case '\n':
178 monitor_printf(mon, "\\n");
179 break;
180 default:
181 monitor_printf(mon, "%c", filename[i]);
182 break;
187 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
189 va_list ap;
190 va_start(ap, fmt);
191 monitor_vprintf((Monitor *)stream, fmt, ap);
192 va_end(ap);
193 return 0;
196 static int compare_cmd(const char *name, const char *list)
198 const char *p, *pstart;
199 int len;
200 len = strlen(name);
201 p = list;
202 for(;;) {
203 pstart = p;
204 p = strchr(p, '|');
205 if (!p)
206 p = pstart + strlen(pstart);
207 if ((p - pstart) == len && !memcmp(pstart, name, len))
208 return 1;
209 if (*p == '\0')
210 break;
211 p++;
213 return 0;
216 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
217 const char *prefix, const char *name)
219 const mon_cmd_t *cmd;
221 for(cmd = cmds; cmd->name != NULL; cmd++) {
222 if (!name || !strcmp(name, cmd->name))
223 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
224 cmd->params, cmd->help);
228 static void help_cmd(Monitor *mon, const char *name)
230 if (name && !strcmp(name, "info")) {
231 help_cmd_dump(mon, info_cmds, "info ", NULL);
232 } else {
233 help_cmd_dump(mon, mon_cmds, "", name);
234 if (name && !strcmp(name, "log")) {
235 const CPULogItem *item;
236 monitor_printf(mon, "Log items (comma separated):\n");
237 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
238 for(item = cpu_log_items; item->mask != 0; item++) {
239 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
245 static void do_commit(Monitor *mon, const char *device)
247 int i, all_devices;
249 all_devices = !strcmp(device, "all");
250 for (i = 0; i < nb_drives; i++) {
251 if (all_devices ||
252 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
253 bdrv_commit(drives_table[i].bdrv);
257 static void do_info(Monitor *mon, const char *item)
259 const mon_cmd_t *cmd;
260 void (*handler)(Monitor *);
262 if (!item)
263 goto help;
264 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
265 if (compare_cmd(item, cmd->name))
266 goto found;
268 help:
269 help_cmd(mon, "info");
270 return;
271 found:
272 handler = cmd->handler;
273 handler(mon);
276 static void do_info_version(Monitor *mon)
278 monitor_printf(mon, "%s\n", QEMU_VERSION);
281 static void do_info_name(Monitor *mon)
283 if (qemu_name)
284 monitor_printf(mon, "%s\n", qemu_name);
287 #if defined(TARGET_I386)
288 static void do_info_hpet(Monitor *mon)
290 monitor_printf(mon, "HPET is %s by QEMU\n",
291 (no_hpet) ? "disabled" : "enabled");
293 #endif
295 static void do_info_uuid(Monitor *mon)
297 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
298 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
299 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
300 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
301 qemu_uuid[14], qemu_uuid[15]);
304 /* get the current CPU defined by the user */
305 static int mon_set_cpu(int cpu_index)
307 CPUState *env;
309 for(env = first_cpu; env != NULL; env = env->next_cpu) {
310 if (env->cpu_index == cpu_index) {
311 cur_mon->mon_cpu = env;
312 return 0;
315 return -1;
318 static CPUState *mon_get_cpu(void)
320 if (!cur_mon->mon_cpu) {
321 mon_set_cpu(0);
323 cpu_synchronize_state(cur_mon->mon_cpu, 0);
324 return cur_mon->mon_cpu;
327 static void do_info_registers(Monitor *mon)
329 CPUState *env;
330 env = mon_get_cpu();
331 if (!env)
332 return;
333 #ifdef TARGET_I386
334 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
335 X86_DUMP_FPU);
336 #else
337 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
339 #endif
342 static void do_info_cpus(Monitor *mon)
344 CPUState *env;
346 /* just to set the default cpu if not already done */
347 mon_get_cpu();
349 for(env = first_cpu; env != NULL; env = env->next_cpu) {
350 cpu_synchronize_state(env, 0);
351 monitor_printf(mon, "%c CPU #%d:",
352 (env == mon->mon_cpu) ? '*' : ' ',
353 env->cpu_index);
354 #if defined(TARGET_I386)
355 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
356 env->eip + env->segs[R_CS].base);
357 #elif defined(TARGET_PPC)
358 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
359 #elif defined(TARGET_SPARC)
360 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
361 env->pc, env->npc);
362 #elif defined(TARGET_MIPS)
363 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
364 #endif
365 if (env->halted)
366 monitor_printf(mon, " (halted)");
367 monitor_printf(mon," thread_id=%d", env->thread_id);
368 monitor_printf(mon, "\n");
372 static void do_cpu_set(Monitor *mon, int index)
374 if (mon_set_cpu(index) < 0)
375 monitor_printf(mon, "Invalid CPU index\n");
378 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
380 int state;
382 if (!strcmp(status, "online"))
383 state = 1;
384 else if (!strcmp(status, "offline"))
385 state = 0;
386 else {
387 monitor_printf(mon, "invalid status: %s\n", status);
388 return;
390 #if defined(TARGET_I386) || defined(TARGET_X86_64)
391 qemu_system_cpu_hot_add(value, state);
392 #endif
395 static void do_info_jit(Monitor *mon)
397 dump_exec_info((FILE *)mon, monitor_fprintf);
400 static void do_info_history(Monitor *mon)
402 int i;
403 const char *str;
405 if (!mon->rs)
406 return;
407 i = 0;
408 for(;;) {
409 str = readline_get_history(mon->rs, i);
410 if (!str)
411 break;
412 monitor_printf(mon, "%d: '%s'\n", i, str);
413 i++;
417 #if defined(TARGET_PPC)
418 /* XXX: not implemented in other targets */
419 static void do_info_cpu_stats(Monitor *mon)
421 CPUState *env;
423 env = mon_get_cpu();
424 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
426 #endif
428 static void do_quit(Monitor *mon)
430 exit(0);
433 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
435 if (bdrv_is_inserted(bs)) {
436 if (!force) {
437 if (!bdrv_is_removable(bs)) {
438 monitor_printf(mon, "device is not removable\n");
439 return -1;
441 if (bdrv_is_locked(bs)) {
442 monitor_printf(mon, "device is locked\n");
443 return -1;
446 bdrv_close(bs);
448 return 0;
451 static void do_eject(Monitor *mon, int force, const char *filename)
453 BlockDriverState *bs;
455 bs = bdrv_find(filename);
456 if (!bs) {
457 monitor_printf(mon, "device not found\n");
458 return;
460 eject_device(mon, bs, force);
463 static void do_change_block(Monitor *mon, const char *device,
464 const char *filename, const char *fmt)
466 BlockDriverState *bs;
467 BlockDriver *drv = NULL;
469 bs = bdrv_find(device);
470 if (!bs) {
471 monitor_printf(mon, "device not found\n");
472 return;
474 if (fmt) {
475 drv = bdrv_find_format(fmt);
476 if (!drv) {
477 monitor_printf(mon, "invalid format %s\n", fmt);
478 return;
481 if (eject_device(mon, bs, 0) < 0)
482 return;
483 bdrv_open2(bs, filename, 0, drv);
484 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
487 static void change_vnc_password_cb(Monitor *mon, const char *password,
488 void *opaque)
490 if (vnc_display_password(NULL, password) < 0)
491 monitor_printf(mon, "could not set VNC server password\n");
493 monitor_read_command(mon, 1);
496 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
498 if (strcmp(target, "passwd") == 0 ||
499 strcmp(target, "password") == 0) {
500 if (arg) {
501 char password[9];
502 strncpy(password, arg, sizeof(password));
503 password[sizeof(password) - 1] = '\0';
504 change_vnc_password_cb(mon, password, NULL);
505 } else {
506 monitor_read_password(mon, change_vnc_password_cb, NULL);
508 } else {
509 if (vnc_display_open(NULL, target) < 0)
510 monitor_printf(mon, "could not start VNC server on %s\n", target);
514 static void do_change(Monitor *mon, const char *device, const char *target,
515 const char *arg)
517 if (strcmp(device, "vnc") == 0) {
518 do_change_vnc(mon, target, arg);
519 } else {
520 do_change_block(mon, device, target, arg);
524 static void do_screen_dump(Monitor *mon, const char *filename)
526 vga_hw_screen_dump(filename);
529 static void do_logfile(Monitor *mon, const char *filename)
531 cpu_set_log_filename(filename);
534 static void do_log(Monitor *mon, const char *items)
536 int mask;
538 if (!strcmp(items, "none")) {
539 mask = 0;
540 } else {
541 mask = cpu_str_to_log_mask(items);
542 if (!mask) {
543 help_cmd(mon, "log");
544 return;
547 cpu_set_log(mask);
550 static void do_stop(Monitor *mon)
552 vm_stop(EXCP_INTERRUPT);
555 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
557 struct bdrv_iterate_context {
558 Monitor *mon;
559 int err;
562 static void do_cont(Monitor *mon)
564 struct bdrv_iterate_context context = { mon, 0 };
566 bdrv_iterate(encrypted_bdrv_it, &context);
567 /* only resume the vm if all keys are set and valid */
568 if (!context.err)
569 vm_start();
572 static void bdrv_key_cb(void *opaque, int err)
574 Monitor *mon = opaque;
576 /* another key was set successfully, retry to continue */
577 if (!err)
578 do_cont(mon);
581 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
583 struct bdrv_iterate_context *context = opaque;
585 if (!context->err && bdrv_key_required(bs)) {
586 context->err = -EBUSY;
587 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
588 context->mon);
592 #ifdef CONFIG_GDBSTUB
593 static void do_gdbserver(Monitor *mon, const char *port)
595 if (!port)
596 port = DEFAULT_GDBSTUB_PORT;
597 if (gdbserver_start(port) < 0) {
598 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
599 port);
600 } else if (strcmp(port, "none") == 0) {
601 monitor_printf(mon, "Disabled gdbserver\n");
602 } else {
603 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
606 #endif
608 static void monitor_printc(Monitor *mon, int c)
610 monitor_printf(mon, "'");
611 switch(c) {
612 case '\'':
613 monitor_printf(mon, "\\'");
614 break;
615 case '\\':
616 monitor_printf(mon, "\\\\");
617 break;
618 case '\n':
619 monitor_printf(mon, "\\n");
620 break;
621 case '\r':
622 monitor_printf(mon, "\\r");
623 break;
624 default:
625 if (c >= 32 && c <= 126) {
626 monitor_printf(mon, "%c", c);
627 } else {
628 monitor_printf(mon, "\\x%02x", c);
630 break;
632 monitor_printf(mon, "'");
635 static void memory_dump(Monitor *mon, int count, int format, int wsize,
636 target_phys_addr_t addr, int is_physical)
638 CPUState *env;
639 int nb_per_line, l, line_size, i, max_digits, len;
640 uint8_t buf[16];
641 uint64_t v;
643 if (format == 'i') {
644 int flags;
645 flags = 0;
646 env = mon_get_cpu();
647 if (!env && !is_physical)
648 return;
649 #ifdef TARGET_I386
650 if (wsize == 2) {
651 flags = 1;
652 } else if (wsize == 4) {
653 flags = 0;
654 } else {
655 /* as default we use the current CS size */
656 flags = 0;
657 if (env) {
658 #ifdef TARGET_X86_64
659 if ((env->efer & MSR_EFER_LMA) &&
660 (env->segs[R_CS].flags & DESC_L_MASK))
661 flags = 2;
662 else
663 #endif
664 if (!(env->segs[R_CS].flags & DESC_B_MASK))
665 flags = 1;
668 #endif
669 monitor_disas(mon, env, addr, count, is_physical, flags);
670 return;
673 len = wsize * count;
674 if (wsize == 1)
675 line_size = 8;
676 else
677 line_size = 16;
678 nb_per_line = line_size / wsize;
679 max_digits = 0;
681 switch(format) {
682 case 'o':
683 max_digits = (wsize * 8 + 2) / 3;
684 break;
685 default:
686 case 'x':
687 max_digits = (wsize * 8) / 4;
688 break;
689 case 'u':
690 case 'd':
691 max_digits = (wsize * 8 * 10 + 32) / 33;
692 break;
693 case 'c':
694 wsize = 1;
695 break;
698 while (len > 0) {
699 if (is_physical)
700 monitor_printf(mon, TARGET_FMT_plx ":", addr);
701 else
702 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
703 l = len;
704 if (l > line_size)
705 l = line_size;
706 if (is_physical) {
707 cpu_physical_memory_rw(addr, buf, l, 0);
708 } else {
709 env = mon_get_cpu();
710 if (!env)
711 break;
712 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
713 monitor_printf(mon, " Cannot access memory\n");
714 break;
717 i = 0;
718 while (i < l) {
719 switch(wsize) {
720 default:
721 case 1:
722 v = ldub_raw(buf + i);
723 break;
724 case 2:
725 v = lduw_raw(buf + i);
726 break;
727 case 4:
728 v = (uint32_t)ldl_raw(buf + i);
729 break;
730 case 8:
731 v = ldq_raw(buf + i);
732 break;
734 monitor_printf(mon, " ");
735 switch(format) {
736 case 'o':
737 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
738 break;
739 case 'x':
740 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
741 break;
742 case 'u':
743 monitor_printf(mon, "%*" PRIu64, max_digits, v);
744 break;
745 case 'd':
746 monitor_printf(mon, "%*" PRId64, max_digits, v);
747 break;
748 case 'c':
749 monitor_printc(mon, v);
750 break;
752 i += wsize;
754 monitor_printf(mon, "\n");
755 addr += l;
756 len -= l;
760 #if TARGET_LONG_BITS == 64
761 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
762 #else
763 #define GET_TLONG(h, l) (l)
764 #endif
766 static void do_memory_dump(Monitor *mon, int count, int format, int size,
767 uint32_t addrh, uint32_t addrl)
769 target_long addr = GET_TLONG(addrh, addrl);
770 memory_dump(mon, count, format, size, addr, 0);
773 #if TARGET_PHYS_ADDR_BITS > 32
774 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
775 #else
776 #define GET_TPHYSADDR(h, l) (l)
777 #endif
779 static void do_physical_memory_dump(Monitor *mon, int count, int format,
780 int size, uint32_t addrh, uint32_t addrl)
783 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
784 memory_dump(mon, count, format, size, addr, 1);
787 static void do_print(Monitor *mon, int count, int format, int size,
788 unsigned int valh, unsigned int vall)
790 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
791 #if TARGET_PHYS_ADDR_BITS == 32
792 switch(format) {
793 case 'o':
794 monitor_printf(mon, "%#o", val);
795 break;
796 case 'x':
797 monitor_printf(mon, "%#x", val);
798 break;
799 case 'u':
800 monitor_printf(mon, "%u", val);
801 break;
802 default:
803 case 'd':
804 monitor_printf(mon, "%d", val);
805 break;
806 case 'c':
807 monitor_printc(mon, val);
808 break;
810 #else
811 switch(format) {
812 case 'o':
813 monitor_printf(mon, "%#" PRIo64, val);
814 break;
815 case 'x':
816 monitor_printf(mon, "%#" PRIx64, val);
817 break;
818 case 'u':
819 monitor_printf(mon, "%" PRIu64, val);
820 break;
821 default:
822 case 'd':
823 monitor_printf(mon, "%" PRId64, val);
824 break;
825 case 'c':
826 monitor_printc(mon, val);
827 break;
829 #endif
830 monitor_printf(mon, "\n");
833 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
834 uint32_t size, const char *filename)
836 FILE *f;
837 target_long addr = GET_TLONG(valh, vall);
838 uint32_t l;
839 CPUState *env;
840 uint8_t buf[1024];
842 env = mon_get_cpu();
843 if (!env)
844 return;
846 f = fopen(filename, "wb");
847 if (!f) {
848 monitor_printf(mon, "could not open '%s'\n", filename);
849 return;
851 while (size != 0) {
852 l = sizeof(buf);
853 if (l > size)
854 l = size;
855 cpu_memory_rw_debug(env, addr, buf, l, 0);
856 fwrite(buf, 1, l, f);
857 addr += l;
858 size -= l;
860 fclose(f);
863 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
864 unsigned int vall, uint32_t size,
865 const char *filename)
867 FILE *f;
868 uint32_t l;
869 uint8_t buf[1024];
870 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
872 f = fopen(filename, "wb");
873 if (!f) {
874 monitor_printf(mon, "could not open '%s'\n", filename);
875 return;
877 while (size != 0) {
878 l = sizeof(buf);
879 if (l > size)
880 l = size;
881 cpu_physical_memory_rw(addr, buf, l, 0);
882 fwrite(buf, 1, l, f);
883 fflush(f);
884 addr += l;
885 size -= l;
887 fclose(f);
890 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
892 uint32_t addr;
893 uint8_t buf[1];
894 uint16_t sum;
896 sum = 0;
897 for(addr = start; addr < (start + size); addr++) {
898 cpu_physical_memory_rw(addr, buf, 1, 0);
899 /* BSD sum algorithm ('sum' Unix command) */
900 sum = (sum >> 1) | (sum << 15);
901 sum += buf[0];
903 monitor_printf(mon, "%05d\n", sum);
906 typedef struct {
907 int keycode;
908 const char *name;
909 } KeyDef;
911 static const KeyDef key_defs[] = {
912 { 0x2a, "shift" },
913 { 0x36, "shift_r" },
915 { 0x38, "alt" },
916 { 0xb8, "alt_r" },
917 { 0x64, "altgr" },
918 { 0xe4, "altgr_r" },
919 { 0x1d, "ctrl" },
920 { 0x9d, "ctrl_r" },
922 { 0xdd, "menu" },
924 { 0x01, "esc" },
926 { 0x02, "1" },
927 { 0x03, "2" },
928 { 0x04, "3" },
929 { 0x05, "4" },
930 { 0x06, "5" },
931 { 0x07, "6" },
932 { 0x08, "7" },
933 { 0x09, "8" },
934 { 0x0a, "9" },
935 { 0x0b, "0" },
936 { 0x0c, "minus" },
937 { 0x0d, "equal" },
938 { 0x0e, "backspace" },
940 { 0x0f, "tab" },
941 { 0x10, "q" },
942 { 0x11, "w" },
943 { 0x12, "e" },
944 { 0x13, "r" },
945 { 0x14, "t" },
946 { 0x15, "y" },
947 { 0x16, "u" },
948 { 0x17, "i" },
949 { 0x18, "o" },
950 { 0x19, "p" },
952 { 0x1c, "ret" },
954 { 0x1e, "a" },
955 { 0x1f, "s" },
956 { 0x20, "d" },
957 { 0x21, "f" },
958 { 0x22, "g" },
959 { 0x23, "h" },
960 { 0x24, "j" },
961 { 0x25, "k" },
962 { 0x26, "l" },
964 { 0x2c, "z" },
965 { 0x2d, "x" },
966 { 0x2e, "c" },
967 { 0x2f, "v" },
968 { 0x30, "b" },
969 { 0x31, "n" },
970 { 0x32, "m" },
971 { 0x33, "comma" },
972 { 0x34, "dot" },
973 { 0x35, "slash" },
975 { 0x37, "asterisk" },
977 { 0x39, "spc" },
978 { 0x3a, "caps_lock" },
979 { 0x3b, "f1" },
980 { 0x3c, "f2" },
981 { 0x3d, "f3" },
982 { 0x3e, "f4" },
983 { 0x3f, "f5" },
984 { 0x40, "f6" },
985 { 0x41, "f7" },
986 { 0x42, "f8" },
987 { 0x43, "f9" },
988 { 0x44, "f10" },
989 { 0x45, "num_lock" },
990 { 0x46, "scroll_lock" },
992 { 0xb5, "kp_divide" },
993 { 0x37, "kp_multiply" },
994 { 0x4a, "kp_subtract" },
995 { 0x4e, "kp_add" },
996 { 0x9c, "kp_enter" },
997 { 0x53, "kp_decimal" },
998 { 0x54, "sysrq" },
1000 { 0x52, "kp_0" },
1001 { 0x4f, "kp_1" },
1002 { 0x50, "kp_2" },
1003 { 0x51, "kp_3" },
1004 { 0x4b, "kp_4" },
1005 { 0x4c, "kp_5" },
1006 { 0x4d, "kp_6" },
1007 { 0x47, "kp_7" },
1008 { 0x48, "kp_8" },
1009 { 0x49, "kp_9" },
1011 { 0x56, "<" },
1013 { 0x57, "f11" },
1014 { 0x58, "f12" },
1016 { 0xb7, "print" },
1018 { 0xc7, "home" },
1019 { 0xc9, "pgup" },
1020 { 0xd1, "pgdn" },
1021 { 0xcf, "end" },
1023 { 0xcb, "left" },
1024 { 0xc8, "up" },
1025 { 0xd0, "down" },
1026 { 0xcd, "right" },
1028 { 0xd2, "insert" },
1029 { 0xd3, "delete" },
1030 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1031 { 0xf0, "stop" },
1032 { 0xf1, "again" },
1033 { 0xf2, "props" },
1034 { 0xf3, "undo" },
1035 { 0xf4, "front" },
1036 { 0xf5, "copy" },
1037 { 0xf6, "open" },
1038 { 0xf7, "paste" },
1039 { 0xf8, "find" },
1040 { 0xf9, "cut" },
1041 { 0xfa, "lf" },
1042 { 0xfb, "help" },
1043 { 0xfc, "meta_l" },
1044 { 0xfd, "meta_r" },
1045 { 0xfe, "compose" },
1046 #endif
1047 { 0, NULL },
1050 static int get_keycode(const char *key)
1052 const KeyDef *p;
1053 char *endp;
1054 int ret;
1056 for(p = key_defs; p->name != NULL; p++) {
1057 if (!strcmp(key, p->name))
1058 return p->keycode;
1060 if (strstart(key, "0x", NULL)) {
1061 ret = strtoul(key, &endp, 0);
1062 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1063 return ret;
1065 return -1;
1068 #define MAX_KEYCODES 16
1069 static uint8_t keycodes[MAX_KEYCODES];
1070 static int nb_pending_keycodes;
1071 static QEMUTimer *key_timer;
1073 static void release_keys(void *opaque)
1075 int keycode;
1077 while (nb_pending_keycodes > 0) {
1078 nb_pending_keycodes--;
1079 keycode = keycodes[nb_pending_keycodes];
1080 if (keycode & 0x80)
1081 kbd_put_keycode(0xe0);
1082 kbd_put_keycode(keycode | 0x80);
1086 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1087 int hold_time)
1089 char keyname_buf[16];
1090 char *separator;
1091 int keyname_len, keycode, i;
1093 if (nb_pending_keycodes > 0) {
1094 qemu_del_timer(key_timer);
1095 release_keys(NULL);
1097 if (!has_hold_time)
1098 hold_time = 100;
1099 i = 0;
1100 while (1) {
1101 separator = strchr(string, '-');
1102 keyname_len = separator ? separator - string : strlen(string);
1103 if (keyname_len > 0) {
1104 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1105 if (keyname_len > sizeof(keyname_buf) - 1) {
1106 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1107 return;
1109 if (i == MAX_KEYCODES) {
1110 monitor_printf(mon, "too many keys\n");
1111 return;
1113 keyname_buf[keyname_len] = 0;
1114 keycode = get_keycode(keyname_buf);
1115 if (keycode < 0) {
1116 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1117 return;
1119 keycodes[i++] = keycode;
1121 if (!separator)
1122 break;
1123 string = separator + 1;
1125 nb_pending_keycodes = i;
1126 /* key down events */
1127 for (i = 0; i < nb_pending_keycodes; i++) {
1128 keycode = keycodes[i];
1129 if (keycode & 0x80)
1130 kbd_put_keycode(0xe0);
1131 kbd_put_keycode(keycode & 0x7f);
1133 /* delayed key up events */
1134 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1135 muldiv64(ticks_per_sec, hold_time, 1000));
1138 static int mouse_button_state;
1140 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1141 const char *dz_str)
1143 int dx, dy, dz;
1144 dx = strtol(dx_str, NULL, 0);
1145 dy = strtol(dy_str, NULL, 0);
1146 dz = 0;
1147 if (dz_str)
1148 dz = strtol(dz_str, NULL, 0);
1149 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1152 static void do_mouse_button(Monitor *mon, int button_state)
1154 mouse_button_state = button_state;
1155 kbd_mouse_event(0, 0, 0, mouse_button_state);
1158 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1159 int addr, int has_index, int index)
1161 uint32_t val;
1162 int suffix;
1164 if (has_index) {
1165 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1166 addr++;
1168 addr &= 0xffff;
1170 switch(size) {
1171 default:
1172 case 1:
1173 val = cpu_inb(NULL, addr);
1174 suffix = 'b';
1175 break;
1176 case 2:
1177 val = cpu_inw(NULL, addr);
1178 suffix = 'w';
1179 break;
1180 case 4:
1181 val = cpu_inl(NULL, addr);
1182 suffix = 'l';
1183 break;
1185 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1186 suffix, addr, size * 2, val);
1189 /* boot_set handler */
1190 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1191 static void *boot_opaque;
1193 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1195 qemu_boot_set_handler = func;
1196 boot_opaque = opaque;
1199 static void do_boot_set(Monitor *mon, const char *bootdevice)
1201 int res;
1203 if (qemu_boot_set_handler) {
1204 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1205 if (res == 0)
1206 monitor_printf(mon, "boot device list now set to %s\n",
1207 bootdevice);
1208 else
1209 monitor_printf(mon, "setting boot device list failed with "
1210 "error %i\n", res);
1211 } else {
1212 monitor_printf(mon, "no function defined to set boot device list for "
1213 "this architecture\n");
1217 static void do_system_reset(Monitor *mon)
1219 qemu_system_reset_request();
1222 static void do_system_powerdown(Monitor *mon)
1224 qemu_system_powerdown_request();
1227 #if defined(TARGET_I386)
1228 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1230 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1231 addr,
1232 pte & mask,
1233 pte & PG_GLOBAL_MASK ? 'G' : '-',
1234 pte & PG_PSE_MASK ? 'P' : '-',
1235 pte & PG_DIRTY_MASK ? 'D' : '-',
1236 pte & PG_ACCESSED_MASK ? 'A' : '-',
1237 pte & PG_PCD_MASK ? 'C' : '-',
1238 pte & PG_PWT_MASK ? 'T' : '-',
1239 pte & PG_USER_MASK ? 'U' : '-',
1240 pte & PG_RW_MASK ? 'W' : '-');
1243 static void tlb_info(Monitor *mon)
1245 CPUState *env;
1246 int l1, l2;
1247 uint32_t pgd, pde, pte;
1249 env = mon_get_cpu();
1250 if (!env)
1251 return;
1253 if (!(env->cr[0] & CR0_PG_MASK)) {
1254 monitor_printf(mon, "PG disabled\n");
1255 return;
1257 pgd = env->cr[3] & ~0xfff;
1258 for(l1 = 0; l1 < 1024; l1++) {
1259 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1260 pde = le32_to_cpu(pde);
1261 if (pde & PG_PRESENT_MASK) {
1262 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1263 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1264 } else {
1265 for(l2 = 0; l2 < 1024; l2++) {
1266 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1267 (uint8_t *)&pte, 4);
1268 pte = le32_to_cpu(pte);
1269 if (pte & PG_PRESENT_MASK) {
1270 print_pte(mon, (l1 << 22) + (l2 << 12),
1271 pte & ~PG_PSE_MASK,
1272 ~0xfff);
1280 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1281 uint32_t end, int prot)
1283 int prot1;
1284 prot1 = *plast_prot;
1285 if (prot != prot1) {
1286 if (*pstart != -1) {
1287 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1288 *pstart, end, end - *pstart,
1289 prot1 & PG_USER_MASK ? 'u' : '-',
1290 'r',
1291 prot1 & PG_RW_MASK ? 'w' : '-');
1293 if (prot != 0)
1294 *pstart = end;
1295 else
1296 *pstart = -1;
1297 *plast_prot = prot;
1301 static void mem_info(Monitor *mon)
1303 CPUState *env;
1304 int l1, l2, prot, last_prot;
1305 uint32_t pgd, pde, pte, start, end;
1307 env = mon_get_cpu();
1308 if (!env)
1309 return;
1311 if (!(env->cr[0] & CR0_PG_MASK)) {
1312 monitor_printf(mon, "PG disabled\n");
1313 return;
1315 pgd = env->cr[3] & ~0xfff;
1316 last_prot = 0;
1317 start = -1;
1318 for(l1 = 0; l1 < 1024; l1++) {
1319 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1320 pde = le32_to_cpu(pde);
1321 end = l1 << 22;
1322 if (pde & PG_PRESENT_MASK) {
1323 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1324 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1325 mem_print(mon, &start, &last_prot, end, prot);
1326 } else {
1327 for(l2 = 0; l2 < 1024; l2++) {
1328 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1329 (uint8_t *)&pte, 4);
1330 pte = le32_to_cpu(pte);
1331 end = (l1 << 22) + (l2 << 12);
1332 if (pte & PG_PRESENT_MASK) {
1333 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1334 } else {
1335 prot = 0;
1337 mem_print(mon, &start, &last_prot, end, prot);
1340 } else {
1341 prot = 0;
1342 mem_print(mon, &start, &last_prot, end, prot);
1346 #endif
1348 #if defined(TARGET_SH4)
1350 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1352 monitor_printf(mon, " tlb%i:\t"
1353 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1354 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1355 "dirty=%hhu writethrough=%hhu\n",
1356 idx,
1357 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1358 tlb->v, tlb->sh, tlb->c, tlb->pr,
1359 tlb->d, tlb->wt);
1362 static void tlb_info(Monitor *mon)
1364 CPUState *env = mon_get_cpu();
1365 int i;
1367 monitor_printf (mon, "ITLB:\n");
1368 for (i = 0 ; i < ITLB_SIZE ; i++)
1369 print_tlb (mon, i, &env->itlb[i]);
1370 monitor_printf (mon, "UTLB:\n");
1371 for (i = 0 ; i < UTLB_SIZE ; i++)
1372 print_tlb (mon, i, &env->utlb[i]);
1375 #endif
1377 static void do_info_kqemu(Monitor *mon)
1379 #ifdef USE_KQEMU
1380 CPUState *env;
1381 int val;
1382 val = 0;
1383 env = mon_get_cpu();
1384 if (!env) {
1385 monitor_printf(mon, "No cpu initialized yet");
1386 return;
1388 val = env->kqemu_enabled;
1389 monitor_printf(mon, "kqemu support: ");
1390 switch(val) {
1391 default:
1392 case 0:
1393 monitor_printf(mon, "disabled\n");
1394 break;
1395 case 1:
1396 monitor_printf(mon, "enabled for user code\n");
1397 break;
1398 case 2:
1399 monitor_printf(mon, "enabled for user and kernel code\n");
1400 break;
1402 #else
1403 monitor_printf(mon, "kqemu support: not compiled\n");
1404 #endif
1407 static void do_info_kvm(Monitor *mon)
1409 #if defined(USE_KVM) || defined(CONFIG_KVM)
1410 monitor_printf(mon, "kvm support: ");
1411 if (kvm_enabled())
1412 monitor_printf(mon, "enabled\n");
1413 else
1414 monitor_printf(mon, "disabled\n");
1415 #else
1416 monitor_printf(mon, "kvm support: not compiled\n");
1417 #endif
1420 #ifdef CONFIG_PROFILER
1422 int64_t kqemu_time;
1423 int64_t qemu_time;
1424 int64_t kqemu_exec_count;
1425 int64_t dev_time;
1426 int64_t kqemu_ret_int_count;
1427 int64_t kqemu_ret_excp_count;
1428 int64_t kqemu_ret_intr_count;
1430 static void do_info_profile(Monitor *mon)
1432 int64_t total;
1433 total = qemu_time;
1434 if (total == 0)
1435 total = 1;
1436 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1437 dev_time, dev_time / (double)ticks_per_sec);
1438 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1439 qemu_time, qemu_time / (double)ticks_per_sec);
1440 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1441 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1442 PRId64 "\n",
1443 kqemu_time, kqemu_time / (double)ticks_per_sec,
1444 kqemu_time / (double)total * 100.0,
1445 kqemu_exec_count,
1446 kqemu_ret_int_count,
1447 kqemu_ret_excp_count,
1448 kqemu_ret_intr_count);
1449 qemu_time = 0;
1450 kqemu_time = 0;
1451 kqemu_exec_count = 0;
1452 dev_time = 0;
1453 kqemu_ret_int_count = 0;
1454 kqemu_ret_excp_count = 0;
1455 kqemu_ret_intr_count = 0;
1456 #ifdef USE_KQEMU
1457 kqemu_record_dump();
1458 #endif
1460 #else
1461 static void do_info_profile(Monitor *mon)
1463 monitor_printf(mon, "Internal profiler not compiled\n");
1465 #endif
1467 /* Capture support */
1468 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1470 static void do_info_capture(Monitor *mon)
1472 int i;
1473 CaptureState *s;
1475 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1476 monitor_printf(mon, "[%d]: ", i);
1477 s->ops.info (s->opaque);
1481 static void do_stop_capture(Monitor *mon, int n)
1483 int i;
1484 CaptureState *s;
1486 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1487 if (i == n) {
1488 s->ops.destroy (s->opaque);
1489 LIST_REMOVE (s, entries);
1490 qemu_free (s);
1491 return;
1496 #ifdef HAS_AUDIO
1497 static void do_wav_capture(Monitor *mon, const char *path,
1498 int has_freq, int freq,
1499 int has_bits, int bits,
1500 int has_channels, int nchannels)
1502 CaptureState *s;
1504 s = qemu_mallocz (sizeof (*s));
1506 freq = has_freq ? freq : 44100;
1507 bits = has_bits ? bits : 16;
1508 nchannels = has_channels ? nchannels : 2;
1510 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1511 monitor_printf(mon, "Faied to add wave capture\n");
1512 qemu_free (s);
1514 LIST_INSERT_HEAD (&capture_head, s, entries);
1516 #endif
1518 #if defined(TARGET_I386)
1519 static void do_inject_nmi(Monitor *mon, int cpu_index)
1521 CPUState *env;
1523 for (env = first_cpu; env != NULL; env = env->next_cpu)
1524 if (env->cpu_index == cpu_index) {
1525 if (kvm_enabled())
1526 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1527 else
1528 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1529 break;
1532 #endif
1534 static void do_info_status(Monitor *mon)
1536 if (vm_running)
1537 monitor_printf(mon, "VM status: running\n");
1538 else
1539 monitor_printf(mon, "VM status: paused\n");
1543 static void do_balloon(Monitor *mon, int value)
1545 ram_addr_t target = value;
1546 qemu_balloon(target << 20);
1549 static void do_info_balloon(Monitor *mon)
1551 ram_addr_t actual;
1553 actual = qemu_balloon_status();
1554 if (kvm_enabled() && !kvm_has_sync_mmu())
1555 monitor_printf(mon, "Using KVM without synchronous MMU, "
1556 "ballooning disabled\n");
1557 else if (actual == 0)
1558 monitor_printf(mon, "Ballooning not activated in VM\n");
1559 else
1560 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1563 static void do_acl(Monitor *mon,
1564 const char *command,
1565 const char *aclname,
1566 const char *match,
1567 int has_index,
1568 int index)
1570 qemu_acl *acl;
1572 acl = qemu_acl_find(aclname);
1573 if (!acl) {
1574 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1575 return;
1578 if (strcmp(command, "show") == 0) {
1579 int i = 0;
1580 qemu_acl_entry *entry;
1581 monitor_printf(mon, "policy: %s\n",
1582 acl->defaultDeny ? "deny" : "allow");
1583 TAILQ_FOREACH(entry, &acl->entries, next) {
1584 i++;
1585 monitor_printf(mon, "%d: %s %s\n", i,
1586 entry->deny ? "deny" : "allow",
1587 entry->match);
1589 } else if (strcmp(command, "reset") == 0) {
1590 qemu_acl_reset(acl);
1591 monitor_printf(mon, "acl: removed all rules\n");
1592 } else if (strcmp(command, "policy") == 0) {
1593 if (!match) {
1594 monitor_printf(mon, "acl: missing policy parameter\n");
1595 return;
1598 if (strcmp(match, "allow") == 0) {
1599 acl->defaultDeny = 0;
1600 monitor_printf(mon, "acl: policy set to 'allow'\n");
1601 } else if (strcmp(match, "deny") == 0) {
1602 acl->defaultDeny = 1;
1603 monitor_printf(mon, "acl: policy set to 'deny'\n");
1604 } else {
1605 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1607 } else if ((strcmp(command, "allow") == 0) ||
1608 (strcmp(command, "deny") == 0)) {
1609 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1610 int ret;
1612 if (!match) {
1613 monitor_printf(mon, "acl: missing match parameter\n");
1614 return;
1617 if (has_index)
1618 ret = qemu_acl_insert(acl, deny, match, index);
1619 else
1620 ret = qemu_acl_append(acl, deny, match);
1621 if (ret < 0)
1622 monitor_printf(mon, "acl: unable to add acl entry\n");
1623 else
1624 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1625 } else if (strcmp(command, "remove") == 0) {
1626 int ret;
1628 if (!match) {
1629 monitor_printf(mon, "acl: missing match parameter\n");
1630 return;
1633 ret = qemu_acl_remove(acl, match);
1634 if (ret < 0)
1635 monitor_printf(mon, "acl: no matching acl entry\n");
1636 else
1637 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1638 } else {
1639 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1643 /* Please update qemu-doc.texi when adding or changing commands */
1644 static const mon_cmd_t mon_cmds[] = {
1645 { "help|?", "s?", help_cmd,
1646 "[cmd]", "show the help" },
1647 { "commit", "s", do_commit,
1648 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1649 { "info", "s?", do_info,
1650 "subcommand", "show various information about the system state" },
1651 { "q|quit", "", do_quit,
1652 "", "quit the emulator" },
1653 { "eject", "-fB", do_eject,
1654 "[-f] device", "eject a removable medium (use -f to force it)" },
1655 { "change", "BFs?", do_change,
1656 "device filename [format]", "change a removable medium, optional format" },
1657 { "screendump", "F", do_screen_dump,
1658 "filename", "save screen into PPM image 'filename'" },
1659 { "logfile", "F", do_logfile,
1660 "filename", "output logs to 'filename'" },
1661 { "log", "s", do_log,
1662 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1663 { "savevm", "s?", do_savevm,
1664 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1665 { "loadvm", "s", do_loadvm,
1666 "tag|id", "restore a VM snapshot from its tag or id" },
1667 { "delvm", "s", do_delvm,
1668 "tag|id", "delete a VM snapshot from its tag or id" },
1669 { "stop", "", do_stop,
1670 "", "stop emulation", },
1671 { "c|cont", "", do_cont,
1672 "", "resume emulation", },
1673 #ifdef CONFIG_GDBSTUB
1674 { "gdbserver", "s?", do_gdbserver,
1675 "[port]", "start gdbserver session (default port=1234)", },
1676 #endif
1677 { "x", "/l", do_memory_dump,
1678 "/fmt addr", "virtual memory dump starting at 'addr'", },
1679 { "xp", "/l", do_physical_memory_dump,
1680 "/fmt addr", "physical memory dump starting at 'addr'", },
1681 { "p|print", "/l", do_print,
1682 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1683 { "i", "/ii.", do_ioport_read,
1684 "/fmt addr", "I/O port read" },
1686 { "sendkey", "si?", do_sendkey,
1687 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1688 { "system_reset", "", do_system_reset,
1689 "", "reset the system" },
1690 { "system_powerdown", "", do_system_powerdown,
1691 "", "send system power down event" },
1692 { "sum", "ii", do_sum,
1693 "addr size", "compute the checksum of a memory region" },
1694 { "usb_add", "s", do_usb_add,
1695 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1696 { "usb_del", "s", do_usb_del,
1697 "device", "remove USB device 'bus.addr'" },
1698 { "cpu", "i", do_cpu_set,
1699 "index", "set the default CPU" },
1700 { "mouse_move", "sss?", do_mouse_move,
1701 "dx dy [dz]", "send mouse move events" },
1702 { "mouse_button", "i", do_mouse_button,
1703 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1704 { "mouse_set", "i", do_mouse_set,
1705 "index", "set which mouse device receives events" },
1706 #ifdef HAS_AUDIO
1707 { "wavcapture", "si?i?i?", do_wav_capture,
1708 "path [frequency bits channels]",
1709 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1710 #endif
1711 { "stopcapture", "i", do_stop_capture,
1712 "capture index", "stop capture" },
1713 { "memsave", "lis", do_memory_save,
1714 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1715 { "pmemsave", "lis", do_physical_memory_save,
1716 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1717 { "boot_set", "s", do_boot_set,
1718 "bootdevice", "define new values for the boot device list" },
1719 #if defined(TARGET_I386)
1720 { "nmi", "i", do_inject_nmi,
1721 "cpu", "inject an NMI on the given CPU", },
1722 #endif
1723 { "migrate", "-ds", do_migrate,
1724 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1725 { "migrate_cancel", "", do_migrate_cancel,
1726 "", "cancel the current VM migration" },
1727 { "migrate_set_speed", "s", do_migrate_set_speed,
1728 "value", "set maximum speed (in bytes) for migrations" },
1729 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1730 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1731 "[file=file][,if=type][,bus=n]\n"
1732 "[,unit=m][,media=d][index=i]\n"
1733 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1734 "[snapshot=on|off][,cache=on|off]",
1735 "add drive to PCI storage controller" },
1736 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]", "hot-add PCI device" },
1737 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1738 { "host_net_add", "ss", net_host_device_add,
1739 "[tap,user,socket,vde] options", "add host VLAN client" },
1740 { "host_net_remove", "is", net_host_device_remove,
1741 "vlan_id name", "remove host VLAN client" },
1742 #endif
1743 { "balloon", "i", do_balloon,
1744 "target", "request VM to change it's memory allocation (in MB)" },
1745 { "set_link", "ss", do_set_link,
1746 "name [up|down]", "change the link status of a network adapter" },
1747 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1748 "acl show vnc.username\n"
1749 "acl policy vnc.username deny\n"
1750 "acl allow vnc.username fred\n"
1751 "acl deny vnc.username bob\n"
1752 "acl reset vnc.username\n" },
1753 { "set_link", "ss", do_set_link, "name [up|down]" },
1754 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1755 { NULL, NULL, },
1758 /* Please update qemu-doc.texi when adding or changing commands */
1759 static const mon_cmd_t info_cmds[] = {
1760 { "version", "", do_info_version,
1761 "", "show the version of QEMU" },
1762 { "network", "", do_info_network,
1763 "", "show the network state" },
1764 { "chardev", "", qemu_chr_info,
1765 "", "show the character devices" },
1766 { "block", "", bdrv_info,
1767 "", "show the block devices" },
1768 { "blockstats", "", bdrv_info_stats,
1769 "", "show block device statistics" },
1770 { "registers", "", do_info_registers,
1771 "", "show the cpu registers" },
1772 { "cpus", "", do_info_cpus,
1773 "", "show infos for each CPU" },
1774 { "history", "", do_info_history,
1775 "", "show the command line history", },
1776 { "irq", "", irq_info,
1777 "", "show the interrupts statistics (if available)", },
1778 { "pic", "", pic_info,
1779 "", "show i8259 (PIC) state", },
1780 { "pci", "", pci_info,
1781 "", "show PCI info", },
1782 #if defined(TARGET_I386) || defined(TARGET_SH4)
1783 { "tlb", "", tlb_info,
1784 "", "show virtual to physical memory mappings", },
1785 #endif
1786 #if defined(TARGET_I386)
1787 { "mem", "", mem_info,
1788 "", "show the active virtual memory mappings", },
1789 { "hpet", "", do_info_hpet,
1790 "", "show state of HPET", },
1791 #endif
1792 { "jit", "", do_info_jit,
1793 "", "show dynamic compiler info", },
1794 { "kqemu", "", do_info_kqemu,
1795 "", "show KQEMU information", },
1796 { "kvm", "", do_info_kvm,
1797 "", "show KVM information", },
1798 { "usb", "", usb_info,
1799 "", "show guest USB devices", },
1800 { "usbhost", "", usb_host_info,
1801 "", "show host USB devices", },
1802 { "profile", "", do_info_profile,
1803 "", "show profiling information", },
1804 { "capture", "", do_info_capture,
1805 "", "show capture information" },
1806 { "snapshots", "", do_info_snapshots,
1807 "", "show the currently saved VM snapshots" },
1808 { "status", "", do_info_status,
1809 "", "show the current VM status (running|paused)" },
1810 { "pcmcia", "", pcmcia_info,
1811 "", "show guest PCMCIA status" },
1812 { "mice", "", do_info_mice,
1813 "", "show which guest mouse is receiving events" },
1814 { "vnc", "", do_info_vnc,
1815 "", "show the vnc server status"},
1816 { "name", "", do_info_name,
1817 "", "show the current VM name" },
1818 { "uuid", "", do_info_uuid,
1819 "", "show the current VM UUID" },
1820 #if defined(TARGET_PPC)
1821 { "cpustats", "", do_info_cpu_stats,
1822 "", "show CPU statistics", },
1823 #endif
1824 #if defined(CONFIG_SLIRP)
1825 { "slirp", "", do_info_slirp,
1826 "", "show SLIRP statistics", },
1827 #endif
1828 { "migrate", "", do_info_migrate, "", "show migration status" },
1829 { "balloon", "", do_info_balloon,
1830 "", "show balloon information" },
1831 { NULL, NULL, },
1834 /*******************************************************************/
1836 static const char *pch;
1837 static jmp_buf expr_env;
1839 #define MD_TLONG 0
1840 #define MD_I32 1
1842 typedef struct MonitorDef {
1843 const char *name;
1844 int offset;
1845 target_long (*get_value)(const struct MonitorDef *md, int val);
1846 int type;
1847 } MonitorDef;
1849 #if defined(TARGET_I386)
1850 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1852 CPUState *env = mon_get_cpu();
1853 if (!env)
1854 return 0;
1855 return env->eip + env->segs[R_CS].base;
1857 #endif
1859 #if defined(TARGET_PPC)
1860 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1862 CPUState *env = mon_get_cpu();
1863 unsigned int u;
1864 int i;
1866 if (!env)
1867 return 0;
1869 u = 0;
1870 for (i = 0; i < 8; i++)
1871 u |= env->crf[i] << (32 - (4 * i));
1873 return u;
1876 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1878 CPUState *env = mon_get_cpu();
1879 if (!env)
1880 return 0;
1881 return env->msr;
1884 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1886 CPUState *env = mon_get_cpu();
1887 if (!env)
1888 return 0;
1889 return env->xer;
1892 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1894 CPUState *env = mon_get_cpu();
1895 if (!env)
1896 return 0;
1897 return cpu_ppc_load_decr(env);
1900 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1902 CPUState *env = mon_get_cpu();
1903 if (!env)
1904 return 0;
1905 return cpu_ppc_load_tbu(env);
1908 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1910 CPUState *env = mon_get_cpu();
1911 if (!env)
1912 return 0;
1913 return cpu_ppc_load_tbl(env);
1915 #endif
1917 #if defined(TARGET_SPARC)
1918 #ifndef TARGET_SPARC64
1919 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1921 CPUState *env = mon_get_cpu();
1922 if (!env)
1923 return 0;
1924 return GET_PSR(env);
1926 #endif
1928 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1930 CPUState *env = mon_get_cpu();
1931 if (!env)
1932 return 0;
1933 return env->regwptr[val];
1935 #endif
1937 static const MonitorDef monitor_defs[] = {
1938 #ifdef TARGET_I386
1940 #define SEG(name, seg) \
1941 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1942 { name ".base", offsetof(CPUState, segs[seg].base) },\
1943 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1945 { "eax", offsetof(CPUState, regs[0]) },
1946 { "ecx", offsetof(CPUState, regs[1]) },
1947 { "edx", offsetof(CPUState, regs[2]) },
1948 { "ebx", offsetof(CPUState, regs[3]) },
1949 { "esp|sp", offsetof(CPUState, regs[4]) },
1950 { "ebp|fp", offsetof(CPUState, regs[5]) },
1951 { "esi", offsetof(CPUState, regs[6]) },
1952 { "edi", offsetof(CPUState, regs[7]) },
1953 #ifdef TARGET_X86_64
1954 { "r8", offsetof(CPUState, regs[8]) },
1955 { "r9", offsetof(CPUState, regs[9]) },
1956 { "r10", offsetof(CPUState, regs[10]) },
1957 { "r11", offsetof(CPUState, regs[11]) },
1958 { "r12", offsetof(CPUState, regs[12]) },
1959 { "r13", offsetof(CPUState, regs[13]) },
1960 { "r14", offsetof(CPUState, regs[14]) },
1961 { "r15", offsetof(CPUState, regs[15]) },
1962 #endif
1963 { "eflags", offsetof(CPUState, eflags) },
1964 { "eip", offsetof(CPUState, eip) },
1965 SEG("cs", R_CS)
1966 SEG("ds", R_DS)
1967 SEG("es", R_ES)
1968 SEG("ss", R_SS)
1969 SEG("fs", R_FS)
1970 SEG("gs", R_GS)
1971 { "pc", 0, monitor_get_pc, },
1972 #elif defined(TARGET_PPC)
1973 /* General purpose registers */
1974 { "r0", offsetof(CPUState, gpr[0]) },
1975 { "r1", offsetof(CPUState, gpr[1]) },
1976 { "r2", offsetof(CPUState, gpr[2]) },
1977 { "r3", offsetof(CPUState, gpr[3]) },
1978 { "r4", offsetof(CPUState, gpr[4]) },
1979 { "r5", offsetof(CPUState, gpr[5]) },
1980 { "r6", offsetof(CPUState, gpr[6]) },
1981 { "r7", offsetof(CPUState, gpr[7]) },
1982 { "r8", offsetof(CPUState, gpr[8]) },
1983 { "r9", offsetof(CPUState, gpr[9]) },
1984 { "r10", offsetof(CPUState, gpr[10]) },
1985 { "r11", offsetof(CPUState, gpr[11]) },
1986 { "r12", offsetof(CPUState, gpr[12]) },
1987 { "r13", offsetof(CPUState, gpr[13]) },
1988 { "r14", offsetof(CPUState, gpr[14]) },
1989 { "r15", offsetof(CPUState, gpr[15]) },
1990 { "r16", offsetof(CPUState, gpr[16]) },
1991 { "r17", offsetof(CPUState, gpr[17]) },
1992 { "r18", offsetof(CPUState, gpr[18]) },
1993 { "r19", offsetof(CPUState, gpr[19]) },
1994 { "r20", offsetof(CPUState, gpr[20]) },
1995 { "r21", offsetof(CPUState, gpr[21]) },
1996 { "r22", offsetof(CPUState, gpr[22]) },
1997 { "r23", offsetof(CPUState, gpr[23]) },
1998 { "r24", offsetof(CPUState, gpr[24]) },
1999 { "r25", offsetof(CPUState, gpr[25]) },
2000 { "r26", offsetof(CPUState, gpr[26]) },
2001 { "r27", offsetof(CPUState, gpr[27]) },
2002 { "r28", offsetof(CPUState, gpr[28]) },
2003 { "r29", offsetof(CPUState, gpr[29]) },
2004 { "r30", offsetof(CPUState, gpr[30]) },
2005 { "r31", offsetof(CPUState, gpr[31]) },
2006 /* Floating point registers */
2007 { "f0", offsetof(CPUState, fpr[0]) },
2008 { "f1", offsetof(CPUState, fpr[1]) },
2009 { "f2", offsetof(CPUState, fpr[2]) },
2010 { "f3", offsetof(CPUState, fpr[3]) },
2011 { "f4", offsetof(CPUState, fpr[4]) },
2012 { "f5", offsetof(CPUState, fpr[5]) },
2013 { "f6", offsetof(CPUState, fpr[6]) },
2014 { "f7", offsetof(CPUState, fpr[7]) },
2015 { "f8", offsetof(CPUState, fpr[8]) },
2016 { "f9", offsetof(CPUState, fpr[9]) },
2017 { "f10", offsetof(CPUState, fpr[10]) },
2018 { "f11", offsetof(CPUState, fpr[11]) },
2019 { "f12", offsetof(CPUState, fpr[12]) },
2020 { "f13", offsetof(CPUState, fpr[13]) },
2021 { "f14", offsetof(CPUState, fpr[14]) },
2022 { "f15", offsetof(CPUState, fpr[15]) },
2023 { "f16", offsetof(CPUState, fpr[16]) },
2024 { "f17", offsetof(CPUState, fpr[17]) },
2025 { "f18", offsetof(CPUState, fpr[18]) },
2026 { "f19", offsetof(CPUState, fpr[19]) },
2027 { "f20", offsetof(CPUState, fpr[20]) },
2028 { "f21", offsetof(CPUState, fpr[21]) },
2029 { "f22", offsetof(CPUState, fpr[22]) },
2030 { "f23", offsetof(CPUState, fpr[23]) },
2031 { "f24", offsetof(CPUState, fpr[24]) },
2032 { "f25", offsetof(CPUState, fpr[25]) },
2033 { "f26", offsetof(CPUState, fpr[26]) },
2034 { "f27", offsetof(CPUState, fpr[27]) },
2035 { "f28", offsetof(CPUState, fpr[28]) },
2036 { "f29", offsetof(CPUState, fpr[29]) },
2037 { "f30", offsetof(CPUState, fpr[30]) },
2038 { "f31", offsetof(CPUState, fpr[31]) },
2039 { "fpscr", offsetof(CPUState, fpscr) },
2040 /* Next instruction pointer */
2041 { "nip|pc", offsetof(CPUState, nip) },
2042 { "lr", offsetof(CPUState, lr) },
2043 { "ctr", offsetof(CPUState, ctr) },
2044 { "decr", 0, &monitor_get_decr, },
2045 { "ccr", 0, &monitor_get_ccr, },
2046 /* Machine state register */
2047 { "msr", 0, &monitor_get_msr, },
2048 { "xer", 0, &monitor_get_xer, },
2049 { "tbu", 0, &monitor_get_tbu, },
2050 { "tbl", 0, &monitor_get_tbl, },
2051 #if defined(TARGET_PPC64)
2052 /* Address space register */
2053 { "asr", offsetof(CPUState, asr) },
2054 #endif
2055 /* Segment registers */
2056 { "sdr1", offsetof(CPUState, sdr1) },
2057 { "sr0", offsetof(CPUState, sr[0]) },
2058 { "sr1", offsetof(CPUState, sr[1]) },
2059 { "sr2", offsetof(CPUState, sr[2]) },
2060 { "sr3", offsetof(CPUState, sr[3]) },
2061 { "sr4", offsetof(CPUState, sr[4]) },
2062 { "sr5", offsetof(CPUState, sr[5]) },
2063 { "sr6", offsetof(CPUState, sr[6]) },
2064 { "sr7", offsetof(CPUState, sr[7]) },
2065 { "sr8", offsetof(CPUState, sr[8]) },
2066 { "sr9", offsetof(CPUState, sr[9]) },
2067 { "sr10", offsetof(CPUState, sr[10]) },
2068 { "sr11", offsetof(CPUState, sr[11]) },
2069 { "sr12", offsetof(CPUState, sr[12]) },
2070 { "sr13", offsetof(CPUState, sr[13]) },
2071 { "sr14", offsetof(CPUState, sr[14]) },
2072 { "sr15", offsetof(CPUState, sr[15]) },
2073 /* Too lazy to put BATs and SPRs ... */
2074 #elif defined(TARGET_SPARC)
2075 { "g0", offsetof(CPUState, gregs[0]) },
2076 { "g1", offsetof(CPUState, gregs[1]) },
2077 { "g2", offsetof(CPUState, gregs[2]) },
2078 { "g3", offsetof(CPUState, gregs[3]) },
2079 { "g4", offsetof(CPUState, gregs[4]) },
2080 { "g5", offsetof(CPUState, gregs[5]) },
2081 { "g6", offsetof(CPUState, gregs[6]) },
2082 { "g7", offsetof(CPUState, gregs[7]) },
2083 { "o0", 0, monitor_get_reg },
2084 { "o1", 1, monitor_get_reg },
2085 { "o2", 2, monitor_get_reg },
2086 { "o3", 3, monitor_get_reg },
2087 { "o4", 4, monitor_get_reg },
2088 { "o5", 5, monitor_get_reg },
2089 { "o6", 6, monitor_get_reg },
2090 { "o7", 7, monitor_get_reg },
2091 { "l0", 8, monitor_get_reg },
2092 { "l1", 9, monitor_get_reg },
2093 { "l2", 10, monitor_get_reg },
2094 { "l3", 11, monitor_get_reg },
2095 { "l4", 12, monitor_get_reg },
2096 { "l5", 13, monitor_get_reg },
2097 { "l6", 14, monitor_get_reg },
2098 { "l7", 15, monitor_get_reg },
2099 { "i0", 16, monitor_get_reg },
2100 { "i1", 17, monitor_get_reg },
2101 { "i2", 18, monitor_get_reg },
2102 { "i3", 19, monitor_get_reg },
2103 { "i4", 20, monitor_get_reg },
2104 { "i5", 21, monitor_get_reg },
2105 { "i6", 22, monitor_get_reg },
2106 { "i7", 23, monitor_get_reg },
2107 { "pc", offsetof(CPUState, pc) },
2108 { "npc", offsetof(CPUState, npc) },
2109 { "y", offsetof(CPUState, y) },
2110 #ifndef TARGET_SPARC64
2111 { "psr", 0, &monitor_get_psr, },
2112 { "wim", offsetof(CPUState, wim) },
2113 #endif
2114 { "tbr", offsetof(CPUState, tbr) },
2115 { "fsr", offsetof(CPUState, fsr) },
2116 { "f0", offsetof(CPUState, fpr[0]) },
2117 { "f1", offsetof(CPUState, fpr[1]) },
2118 { "f2", offsetof(CPUState, fpr[2]) },
2119 { "f3", offsetof(CPUState, fpr[3]) },
2120 { "f4", offsetof(CPUState, fpr[4]) },
2121 { "f5", offsetof(CPUState, fpr[5]) },
2122 { "f6", offsetof(CPUState, fpr[6]) },
2123 { "f7", offsetof(CPUState, fpr[7]) },
2124 { "f8", offsetof(CPUState, fpr[8]) },
2125 { "f9", offsetof(CPUState, fpr[9]) },
2126 { "f10", offsetof(CPUState, fpr[10]) },
2127 { "f11", offsetof(CPUState, fpr[11]) },
2128 { "f12", offsetof(CPUState, fpr[12]) },
2129 { "f13", offsetof(CPUState, fpr[13]) },
2130 { "f14", offsetof(CPUState, fpr[14]) },
2131 { "f15", offsetof(CPUState, fpr[15]) },
2132 { "f16", offsetof(CPUState, fpr[16]) },
2133 { "f17", offsetof(CPUState, fpr[17]) },
2134 { "f18", offsetof(CPUState, fpr[18]) },
2135 { "f19", offsetof(CPUState, fpr[19]) },
2136 { "f20", offsetof(CPUState, fpr[20]) },
2137 { "f21", offsetof(CPUState, fpr[21]) },
2138 { "f22", offsetof(CPUState, fpr[22]) },
2139 { "f23", offsetof(CPUState, fpr[23]) },
2140 { "f24", offsetof(CPUState, fpr[24]) },
2141 { "f25", offsetof(CPUState, fpr[25]) },
2142 { "f26", offsetof(CPUState, fpr[26]) },
2143 { "f27", offsetof(CPUState, fpr[27]) },
2144 { "f28", offsetof(CPUState, fpr[28]) },
2145 { "f29", offsetof(CPUState, fpr[29]) },
2146 { "f30", offsetof(CPUState, fpr[30]) },
2147 { "f31", offsetof(CPUState, fpr[31]) },
2148 #ifdef TARGET_SPARC64
2149 { "f32", offsetof(CPUState, fpr[32]) },
2150 { "f34", offsetof(CPUState, fpr[34]) },
2151 { "f36", offsetof(CPUState, fpr[36]) },
2152 { "f38", offsetof(CPUState, fpr[38]) },
2153 { "f40", offsetof(CPUState, fpr[40]) },
2154 { "f42", offsetof(CPUState, fpr[42]) },
2155 { "f44", offsetof(CPUState, fpr[44]) },
2156 { "f46", offsetof(CPUState, fpr[46]) },
2157 { "f48", offsetof(CPUState, fpr[48]) },
2158 { "f50", offsetof(CPUState, fpr[50]) },
2159 { "f52", offsetof(CPUState, fpr[52]) },
2160 { "f54", offsetof(CPUState, fpr[54]) },
2161 { "f56", offsetof(CPUState, fpr[56]) },
2162 { "f58", offsetof(CPUState, fpr[58]) },
2163 { "f60", offsetof(CPUState, fpr[60]) },
2164 { "f62", offsetof(CPUState, fpr[62]) },
2165 { "asi", offsetof(CPUState, asi) },
2166 { "pstate", offsetof(CPUState, pstate) },
2167 { "cansave", offsetof(CPUState, cansave) },
2168 { "canrestore", offsetof(CPUState, canrestore) },
2169 { "otherwin", offsetof(CPUState, otherwin) },
2170 { "wstate", offsetof(CPUState, wstate) },
2171 { "cleanwin", offsetof(CPUState, cleanwin) },
2172 { "fprs", offsetof(CPUState, fprs) },
2173 #endif
2174 #endif
2175 { NULL },
2178 static void expr_error(Monitor *mon, const char *msg)
2180 monitor_printf(mon, "%s\n", msg);
2181 longjmp(expr_env, 1);
2184 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2185 static int get_monitor_def(target_long *pval, const char *name)
2187 const MonitorDef *md;
2188 void *ptr;
2190 for(md = monitor_defs; md->name != NULL; md++) {
2191 if (compare_cmd(name, md->name)) {
2192 if (md->get_value) {
2193 *pval = md->get_value(md, md->offset);
2194 } else {
2195 CPUState *env = mon_get_cpu();
2196 if (!env)
2197 return -2;
2198 ptr = (uint8_t *)env + md->offset;
2199 switch(md->type) {
2200 case MD_I32:
2201 *pval = *(int32_t *)ptr;
2202 break;
2203 case MD_TLONG:
2204 *pval = *(target_long *)ptr;
2205 break;
2206 default:
2207 *pval = 0;
2208 break;
2211 return 0;
2214 return -1;
2217 static void next(void)
2219 if (pch != '\0') {
2220 pch++;
2221 while (qemu_isspace(*pch))
2222 pch++;
2226 static int64_t expr_sum(Monitor *mon);
2228 static int64_t expr_unary(Monitor *mon)
2230 int64_t n;
2231 char *p;
2232 int ret;
2234 switch(*pch) {
2235 case '+':
2236 next();
2237 n = expr_unary(mon);
2238 break;
2239 case '-':
2240 next();
2241 n = -expr_unary(mon);
2242 break;
2243 case '~':
2244 next();
2245 n = ~expr_unary(mon);
2246 break;
2247 case '(':
2248 next();
2249 n = expr_sum(mon);
2250 if (*pch != ')') {
2251 expr_error(mon, "')' expected");
2253 next();
2254 break;
2255 case '\'':
2256 pch++;
2257 if (*pch == '\0')
2258 expr_error(mon, "character constant expected");
2259 n = *pch;
2260 pch++;
2261 if (*pch != '\'')
2262 expr_error(mon, "missing terminating \' character");
2263 next();
2264 break;
2265 case '$':
2267 char buf[128], *q;
2268 target_long reg=0;
2270 pch++;
2271 q = buf;
2272 while ((*pch >= 'a' && *pch <= 'z') ||
2273 (*pch >= 'A' && *pch <= 'Z') ||
2274 (*pch >= '0' && *pch <= '9') ||
2275 *pch == '_' || *pch == '.') {
2276 if ((q - buf) < sizeof(buf) - 1)
2277 *q++ = *pch;
2278 pch++;
2280 while (qemu_isspace(*pch))
2281 pch++;
2282 *q = 0;
2283 ret = get_monitor_def(&reg, buf);
2284 if (ret == -1)
2285 expr_error(mon, "unknown register");
2286 else if (ret == -2)
2287 expr_error(mon, "no cpu defined");
2288 n = reg;
2290 break;
2291 case '\0':
2292 expr_error(mon, "unexpected end of expression");
2293 n = 0;
2294 break;
2295 default:
2296 #if TARGET_PHYS_ADDR_BITS > 32
2297 n = strtoull(pch, &p, 0);
2298 #else
2299 n = strtoul(pch, &p, 0);
2300 #endif
2301 if (pch == p) {
2302 expr_error(mon, "invalid char in expression");
2304 pch = p;
2305 while (qemu_isspace(*pch))
2306 pch++;
2307 break;
2309 return n;
2313 static int64_t expr_prod(Monitor *mon)
2315 int64_t val, val2;
2316 int op;
2318 val = expr_unary(mon);
2319 for(;;) {
2320 op = *pch;
2321 if (op != '*' && op != '/' && op != '%')
2322 break;
2323 next();
2324 val2 = expr_unary(mon);
2325 switch(op) {
2326 default:
2327 case '*':
2328 val *= val2;
2329 break;
2330 case '/':
2331 case '%':
2332 if (val2 == 0)
2333 expr_error(mon, "division by zero");
2334 if (op == '/')
2335 val /= val2;
2336 else
2337 val %= val2;
2338 break;
2341 return val;
2344 static int64_t expr_logic(Monitor *mon)
2346 int64_t val, val2;
2347 int op;
2349 val = expr_prod(mon);
2350 for(;;) {
2351 op = *pch;
2352 if (op != '&' && op != '|' && op != '^')
2353 break;
2354 next();
2355 val2 = expr_prod(mon);
2356 switch(op) {
2357 default:
2358 case '&':
2359 val &= val2;
2360 break;
2361 case '|':
2362 val |= val2;
2363 break;
2364 case '^':
2365 val ^= val2;
2366 break;
2369 return val;
2372 static int64_t expr_sum(Monitor *mon)
2374 int64_t val, val2;
2375 int op;
2377 val = expr_logic(mon);
2378 for(;;) {
2379 op = *pch;
2380 if (op != '+' && op != '-')
2381 break;
2382 next();
2383 val2 = expr_logic(mon);
2384 if (op == '+')
2385 val += val2;
2386 else
2387 val -= val2;
2389 return val;
2392 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2394 pch = *pp;
2395 if (setjmp(expr_env)) {
2396 *pp = pch;
2397 return -1;
2399 while (qemu_isspace(*pch))
2400 pch++;
2401 *pval = expr_sum(mon);
2402 *pp = pch;
2403 return 0;
2406 static int get_str(char *buf, int buf_size, const char **pp)
2408 const char *p;
2409 char *q;
2410 int c;
2412 q = buf;
2413 p = *pp;
2414 while (qemu_isspace(*p))
2415 p++;
2416 if (*p == '\0') {
2417 fail:
2418 *q = '\0';
2419 *pp = p;
2420 return -1;
2422 if (*p == '\"') {
2423 p++;
2424 while (*p != '\0' && *p != '\"') {
2425 if (*p == '\\') {
2426 p++;
2427 c = *p++;
2428 switch(c) {
2429 case 'n':
2430 c = '\n';
2431 break;
2432 case 'r':
2433 c = '\r';
2434 break;
2435 case '\\':
2436 case '\'':
2437 case '\"':
2438 break;
2439 default:
2440 qemu_printf("unsupported escape code: '\\%c'\n", c);
2441 goto fail;
2443 if ((q - buf) < buf_size - 1) {
2444 *q++ = c;
2446 } else {
2447 if ((q - buf) < buf_size - 1) {
2448 *q++ = *p;
2450 p++;
2453 if (*p != '\"') {
2454 qemu_printf("unterminated string\n");
2455 goto fail;
2457 p++;
2458 } else {
2459 while (*p != '\0' && !qemu_isspace(*p)) {
2460 if ((q - buf) < buf_size - 1) {
2461 *q++ = *p;
2463 p++;
2466 *q = '\0';
2467 *pp = p;
2468 return 0;
2471 static int default_fmt_format = 'x';
2472 static int default_fmt_size = 4;
2474 #define MAX_ARGS 16
2476 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2478 const char *p, *pstart, *typestr;
2479 char *q;
2480 int c, nb_args, len, i, has_arg;
2481 const mon_cmd_t *cmd;
2482 char cmdname[256];
2483 char buf[1024];
2484 void *str_allocated[MAX_ARGS];
2485 void *args[MAX_ARGS];
2486 void (*handler_0)(Monitor *mon);
2487 void (*handler_1)(Monitor *mon, void *arg0);
2488 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2489 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2490 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2491 void *arg3);
2492 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2493 void *arg3, void *arg4);
2494 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2495 void *arg3, void *arg4, void *arg5);
2496 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2497 void *arg3, void *arg4, void *arg5, void *arg6);
2499 #ifdef DEBUG
2500 monitor_printf(mon, "command='%s'\n", cmdline);
2501 #endif
2503 /* extract the command name */
2504 p = cmdline;
2505 q = cmdname;
2506 while (qemu_isspace(*p))
2507 p++;
2508 if (*p == '\0')
2509 return;
2510 pstart = p;
2511 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2512 p++;
2513 len = p - pstart;
2514 if (len > sizeof(cmdname) - 1)
2515 len = sizeof(cmdname) - 1;
2516 memcpy(cmdname, pstart, len);
2517 cmdname[len] = '\0';
2519 /* find the command */
2520 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2521 if (compare_cmd(cmdname, cmd->name))
2522 goto found;
2524 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2525 return;
2526 found:
2528 for(i = 0; i < MAX_ARGS; i++)
2529 str_allocated[i] = NULL;
2531 /* parse the parameters */
2532 typestr = cmd->args_type;
2533 nb_args = 0;
2534 for(;;) {
2535 c = *typestr;
2536 if (c == '\0')
2537 break;
2538 typestr++;
2539 switch(c) {
2540 case 'F':
2541 case 'B':
2542 case 's':
2544 int ret;
2545 char *str;
2547 while (qemu_isspace(*p))
2548 p++;
2549 if (*typestr == '?') {
2550 typestr++;
2551 if (*p == '\0') {
2552 /* no optional string: NULL argument */
2553 str = NULL;
2554 goto add_str;
2557 ret = get_str(buf, sizeof(buf), &p);
2558 if (ret < 0) {
2559 switch(c) {
2560 case 'F':
2561 monitor_printf(mon, "%s: filename expected\n",
2562 cmdname);
2563 break;
2564 case 'B':
2565 monitor_printf(mon, "%s: block device name expected\n",
2566 cmdname);
2567 break;
2568 default:
2569 monitor_printf(mon, "%s: string expected\n", cmdname);
2570 break;
2572 goto fail;
2574 str = qemu_malloc(strlen(buf) + 1);
2575 pstrcpy(str, sizeof(buf), buf);
2576 str_allocated[nb_args] = str;
2577 add_str:
2578 if (nb_args >= MAX_ARGS) {
2579 error_args:
2580 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2581 goto fail;
2583 args[nb_args++] = str;
2585 break;
2586 case '/':
2588 int count, format, size;
2590 while (qemu_isspace(*p))
2591 p++;
2592 if (*p == '/') {
2593 /* format found */
2594 p++;
2595 count = 1;
2596 if (qemu_isdigit(*p)) {
2597 count = 0;
2598 while (qemu_isdigit(*p)) {
2599 count = count * 10 + (*p - '0');
2600 p++;
2603 size = -1;
2604 format = -1;
2605 for(;;) {
2606 switch(*p) {
2607 case 'o':
2608 case 'd':
2609 case 'u':
2610 case 'x':
2611 case 'i':
2612 case 'c':
2613 format = *p++;
2614 break;
2615 case 'b':
2616 size = 1;
2617 p++;
2618 break;
2619 case 'h':
2620 size = 2;
2621 p++;
2622 break;
2623 case 'w':
2624 size = 4;
2625 p++;
2626 break;
2627 case 'g':
2628 case 'L':
2629 size = 8;
2630 p++;
2631 break;
2632 default:
2633 goto next;
2636 next:
2637 if (*p != '\0' && !qemu_isspace(*p)) {
2638 monitor_printf(mon, "invalid char in format: '%c'\n",
2639 *p);
2640 goto fail;
2642 if (format < 0)
2643 format = default_fmt_format;
2644 if (format != 'i') {
2645 /* for 'i', not specifying a size gives -1 as size */
2646 if (size < 0)
2647 size = default_fmt_size;
2648 default_fmt_size = size;
2650 default_fmt_format = format;
2651 } else {
2652 count = 1;
2653 format = default_fmt_format;
2654 if (format != 'i') {
2655 size = default_fmt_size;
2656 } else {
2657 size = -1;
2660 if (nb_args + 3 > MAX_ARGS)
2661 goto error_args;
2662 args[nb_args++] = (void*)(long)count;
2663 args[nb_args++] = (void*)(long)format;
2664 args[nb_args++] = (void*)(long)size;
2666 break;
2667 case 'i':
2668 case 'l':
2670 int64_t val;
2672 while (qemu_isspace(*p))
2673 p++;
2674 if (*typestr == '?' || *typestr == '.') {
2675 if (*typestr == '?') {
2676 if (*p == '\0')
2677 has_arg = 0;
2678 else
2679 has_arg = 1;
2680 } else {
2681 if (*p == '.') {
2682 p++;
2683 while (qemu_isspace(*p))
2684 p++;
2685 has_arg = 1;
2686 } else {
2687 has_arg = 0;
2690 typestr++;
2691 if (nb_args >= MAX_ARGS)
2692 goto error_args;
2693 args[nb_args++] = (void *)(long)has_arg;
2694 if (!has_arg) {
2695 if (nb_args >= MAX_ARGS)
2696 goto error_args;
2697 val = -1;
2698 goto add_num;
2701 if (get_expr(mon, &val, &p))
2702 goto fail;
2703 add_num:
2704 if (c == 'i') {
2705 if (nb_args >= MAX_ARGS)
2706 goto error_args;
2707 args[nb_args++] = (void *)(long)val;
2708 } else {
2709 if ((nb_args + 1) >= MAX_ARGS)
2710 goto error_args;
2711 #if TARGET_PHYS_ADDR_BITS > 32
2712 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2713 #else
2714 args[nb_args++] = (void *)0;
2715 #endif
2716 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2719 break;
2720 case '-':
2722 int has_option;
2723 /* option */
2725 c = *typestr++;
2726 if (c == '\0')
2727 goto bad_type;
2728 while (qemu_isspace(*p))
2729 p++;
2730 has_option = 0;
2731 if (*p == '-') {
2732 p++;
2733 if (*p != c) {
2734 monitor_printf(mon, "%s: unsupported option -%c\n",
2735 cmdname, *p);
2736 goto fail;
2738 p++;
2739 has_option = 1;
2741 if (nb_args >= MAX_ARGS)
2742 goto error_args;
2743 args[nb_args++] = (void *)(long)has_option;
2745 break;
2746 default:
2747 bad_type:
2748 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2749 goto fail;
2752 /* check that all arguments were parsed */
2753 while (qemu_isspace(*p))
2754 p++;
2755 if (*p != '\0') {
2756 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2757 cmdname);
2758 goto fail;
2761 switch(nb_args) {
2762 case 0:
2763 handler_0 = cmd->handler;
2764 handler_0(mon);
2765 break;
2766 case 1:
2767 handler_1 = cmd->handler;
2768 handler_1(mon, args[0]);
2769 break;
2770 case 2:
2771 handler_2 = cmd->handler;
2772 handler_2(mon, args[0], args[1]);
2773 break;
2774 case 3:
2775 handler_3 = cmd->handler;
2776 handler_3(mon, args[0], args[1], args[2]);
2777 break;
2778 case 4:
2779 handler_4 = cmd->handler;
2780 handler_4(mon, args[0], args[1], args[2], args[3]);
2781 break;
2782 case 5:
2783 handler_5 = cmd->handler;
2784 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2785 break;
2786 case 6:
2787 handler_6 = cmd->handler;
2788 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2789 break;
2790 case 7:
2791 handler_7 = cmd->handler;
2792 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2793 args[6]);
2794 break;
2795 default:
2796 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2797 goto fail;
2799 fail:
2800 for(i = 0; i < MAX_ARGS; i++)
2801 qemu_free(str_allocated[i]);
2802 return;
2805 static void cmd_completion(const char *name, const char *list)
2807 const char *p, *pstart;
2808 char cmd[128];
2809 int len;
2811 p = list;
2812 for(;;) {
2813 pstart = p;
2814 p = strchr(p, '|');
2815 if (!p)
2816 p = pstart + strlen(pstart);
2817 len = p - pstart;
2818 if (len > sizeof(cmd) - 2)
2819 len = sizeof(cmd) - 2;
2820 memcpy(cmd, pstart, len);
2821 cmd[len] = '\0';
2822 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2823 readline_add_completion(cur_mon->rs, cmd);
2825 if (*p == '\0')
2826 break;
2827 p++;
2831 static void file_completion(const char *input)
2833 DIR *ffs;
2834 struct dirent *d;
2835 char path[1024];
2836 char file[1024], file_prefix[1024];
2837 int input_path_len;
2838 const char *p;
2840 p = strrchr(input, '/');
2841 if (!p) {
2842 input_path_len = 0;
2843 pstrcpy(file_prefix, sizeof(file_prefix), input);
2844 pstrcpy(path, sizeof(path), ".");
2845 } else {
2846 input_path_len = p - input + 1;
2847 memcpy(path, input, input_path_len);
2848 if (input_path_len > sizeof(path) - 1)
2849 input_path_len = sizeof(path) - 1;
2850 path[input_path_len] = '\0';
2851 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2853 #ifdef DEBUG_COMPLETION
2854 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2855 input, path, file_prefix);
2856 #endif
2857 ffs = opendir(path);
2858 if (!ffs)
2859 return;
2860 for(;;) {
2861 struct stat sb;
2862 d = readdir(ffs);
2863 if (!d)
2864 break;
2865 if (strstart(d->d_name, file_prefix, NULL)) {
2866 memcpy(file, input, input_path_len);
2867 if (input_path_len < sizeof(file))
2868 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2869 d->d_name);
2870 /* stat the file to find out if it's a directory.
2871 * In that case add a slash to speed up typing long paths
2873 stat(file, &sb);
2874 if(S_ISDIR(sb.st_mode))
2875 pstrcat(file, sizeof(file), "/");
2876 readline_add_completion(cur_mon->rs, file);
2879 closedir(ffs);
2882 static void block_completion_it(void *opaque, BlockDriverState *bs)
2884 const char *name = bdrv_get_device_name(bs);
2885 const char *input = opaque;
2887 if (input[0] == '\0' ||
2888 !strncmp(name, (char *)input, strlen(input))) {
2889 readline_add_completion(cur_mon->rs, name);
2893 /* NOTE: this parser is an approximate form of the real command parser */
2894 static void parse_cmdline(const char *cmdline,
2895 int *pnb_args, char **args)
2897 const char *p;
2898 int nb_args, ret;
2899 char buf[1024];
2901 p = cmdline;
2902 nb_args = 0;
2903 for(;;) {
2904 while (qemu_isspace(*p))
2905 p++;
2906 if (*p == '\0')
2907 break;
2908 if (nb_args >= MAX_ARGS)
2909 break;
2910 ret = get_str(buf, sizeof(buf), &p);
2911 args[nb_args] = qemu_strdup(buf);
2912 nb_args++;
2913 if (ret < 0)
2914 break;
2916 *pnb_args = nb_args;
2919 static void monitor_find_completion(const char *cmdline)
2921 const char *cmdname;
2922 char *args[MAX_ARGS];
2923 int nb_args, i, len;
2924 const char *ptype, *str;
2925 const mon_cmd_t *cmd;
2926 const KeyDef *key;
2928 parse_cmdline(cmdline, &nb_args, args);
2929 #ifdef DEBUG_COMPLETION
2930 for(i = 0; i < nb_args; i++) {
2931 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2933 #endif
2935 /* if the line ends with a space, it means we want to complete the
2936 next arg */
2937 len = strlen(cmdline);
2938 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2939 if (nb_args >= MAX_ARGS)
2940 return;
2941 args[nb_args++] = qemu_strdup("");
2943 if (nb_args <= 1) {
2944 /* command completion */
2945 if (nb_args == 0)
2946 cmdname = "";
2947 else
2948 cmdname = args[0];
2949 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2950 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2951 cmd_completion(cmdname, cmd->name);
2953 } else {
2954 /* find the command */
2955 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2956 if (compare_cmd(args[0], cmd->name))
2957 goto found;
2959 return;
2960 found:
2961 ptype = cmd->args_type;
2962 for(i = 0; i < nb_args - 2; i++) {
2963 if (*ptype != '\0') {
2964 ptype++;
2965 while (*ptype == '?')
2966 ptype++;
2969 str = args[nb_args - 1];
2970 switch(*ptype) {
2971 case 'F':
2972 /* file completion */
2973 readline_set_completion_index(cur_mon->rs, strlen(str));
2974 file_completion(str);
2975 break;
2976 case 'B':
2977 /* block device name completion */
2978 readline_set_completion_index(cur_mon->rs, strlen(str));
2979 bdrv_iterate(block_completion_it, (void *)str);
2980 break;
2981 case 's':
2982 /* XXX: more generic ? */
2983 if (!strcmp(cmd->name, "info")) {
2984 readline_set_completion_index(cur_mon->rs, strlen(str));
2985 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2986 cmd_completion(str, cmd->name);
2988 } else if (!strcmp(cmd->name, "sendkey")) {
2989 char *sep = strrchr(str, '-');
2990 if (sep)
2991 str = sep + 1;
2992 readline_set_completion_index(cur_mon->rs, strlen(str));
2993 for(key = key_defs; key->name != NULL; key++) {
2994 cmd_completion(str, key->name);
2997 break;
2998 default:
2999 break;
3002 for(i = 0; i < nb_args; i++)
3003 qemu_free(args[i]);
3006 static int monitor_can_read(void *opaque)
3008 Monitor *mon = opaque;
3010 return (mon->suspend_cnt == 0) ? 128 : 0;
3013 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3015 Monitor *old_mon = cur_mon;
3016 int i;
3018 cur_mon = opaque;
3020 if (cur_mon->rs) {
3021 for (i = 0; i < size; i++)
3022 readline_handle_byte(cur_mon->rs, buf[i]);
3023 } else {
3024 if (size == 0 || buf[size - 1] != 0)
3025 monitor_printf(cur_mon, "corrupted command\n");
3026 else
3027 monitor_handle_command(cur_mon, (char *)buf);
3030 cur_mon = old_mon;
3033 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3035 monitor_suspend(mon);
3036 monitor_handle_command(mon, cmdline);
3037 monitor_resume(mon);
3040 int monitor_suspend(Monitor *mon)
3042 if (!mon->rs)
3043 return -ENOTTY;
3044 mon->suspend_cnt++;
3045 return 0;
3048 void monitor_resume(Monitor *mon)
3050 if (!mon->rs)
3051 return;
3052 if (--mon->suspend_cnt == 0)
3053 readline_show_prompt(mon->rs);
3056 static void monitor_event(void *opaque, int event)
3058 Monitor *mon = opaque;
3060 switch (event) {
3061 case CHR_EVENT_MUX_IN:
3062 readline_restart(mon->rs);
3063 monitor_resume(mon);
3064 monitor_flush(mon);
3065 break;
3067 case CHR_EVENT_MUX_OUT:
3068 if (mon->suspend_cnt == 0)
3069 monitor_printf(mon, "\n");
3070 monitor_flush(mon);
3071 monitor_suspend(mon);
3072 break;
3074 case CHR_EVENT_RESET:
3075 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3076 "information\n", QEMU_VERSION);
3077 if (mon->chr->focus == 0)
3078 readline_show_prompt(mon->rs);
3079 break;
3085 * Local variables:
3086 * c-indent-level: 4
3087 * c-basic-offset: 4
3088 * tab-width: 8
3089 * End:
3092 void monitor_init(CharDriverState *chr, int flags)
3094 static int is_first_init = 1;
3095 Monitor *mon;
3097 if (is_first_init) {
3098 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3099 is_first_init = 0;
3102 mon = qemu_mallocz(sizeof(*mon));
3104 mon->chr = chr;
3105 mon->flags = flags;
3106 if (mon->chr->focus != 0)
3107 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3108 if (flags & MONITOR_USE_READLINE) {
3109 mon->rs = readline_init(mon, monitor_find_completion);
3110 monitor_read_command(mon, 0);
3113 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3114 mon);
3116 LIST_INSERT_HEAD(&mon_list, mon, entry);
3117 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3118 cur_mon = mon;
3121 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3123 BlockDriverState *bs = opaque;
3124 int ret = 0;
3126 if (bdrv_set_key(bs, password) != 0) {
3127 monitor_printf(mon, "invalid password\n");
3128 ret = -EPERM;
3130 if (mon->password_completion_cb)
3131 mon->password_completion_cb(mon->password_opaque, ret);
3133 monitor_read_command(mon, 1);
3136 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3137 BlockDriverCompletionFunc *completion_cb,
3138 void *opaque)
3140 int err;
3142 if (!bdrv_key_required(bs)) {
3143 if (completion_cb)
3144 completion_cb(opaque, 0);
3145 return;
3148 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3149 bdrv_get_encrypted_filename(bs));
3151 mon->password_completion_cb = completion_cb;
3152 mon->password_opaque = opaque;
3154 err = monitor_read_password(mon, bdrv_password_cb, bs);
3156 if (err && completion_cb)
3157 completion_cb(opaque, err);