configure xen in a single place
[qemu/aliguori-queue.git] / monitor.c
blobe38c86e1a3316b485f8f24209c6431d024b4469d
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/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/pcmcia.h"
29 #include "hw/pc.h"
30 #include "hw/pci.h"
31 #include "hw/watchdog.h"
32 #include "gdbstub.h"
33 #include "net.h"
34 #include "qemu-char.h"
35 #include "sysemu.h"
36 #include "monitor.h"
37 #include "readline.h"
38 #include "console.h"
39 #include "block.h"
40 #include "audio/audio.h"
41 #include "disas.h"
42 #include "balloon.h"
43 #include "qemu-timer.h"
44 #include "migration.h"
45 #include "kvm.h"
46 #include "acl.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 QEMU_PKGVERSION);
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, "\n");
371 static void do_cpu_set(Monitor *mon, int index)
373 if (mon_set_cpu(index) < 0)
374 monitor_printf(mon, "Invalid CPU index\n");
377 static void do_info_jit(Monitor *mon)
379 dump_exec_info((FILE *)mon, monitor_fprintf);
382 static void do_info_history(Monitor *mon)
384 int i;
385 const char *str;
387 if (!mon->rs)
388 return;
389 i = 0;
390 for(;;) {
391 str = readline_get_history(mon->rs, i);
392 if (!str)
393 break;
394 monitor_printf(mon, "%d: '%s'\n", i, str);
395 i++;
399 #if defined(TARGET_PPC)
400 /* XXX: not implemented in other targets */
401 static void do_info_cpu_stats(Monitor *mon)
403 CPUState *env;
405 env = mon_get_cpu();
406 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
408 #endif
410 static void do_quit(Monitor *mon)
412 exit(0);
415 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
417 if (bdrv_is_inserted(bs)) {
418 if (!force) {
419 if (!bdrv_is_removable(bs)) {
420 monitor_printf(mon, "device is not removable\n");
421 return -1;
423 if (bdrv_is_locked(bs)) {
424 monitor_printf(mon, "device is locked\n");
425 return -1;
428 bdrv_close(bs);
430 return 0;
433 static void do_eject(Monitor *mon, int force, const char *filename)
435 BlockDriverState *bs;
437 bs = bdrv_find(filename);
438 if (!bs) {
439 monitor_printf(mon, "device not found\n");
440 return;
442 eject_device(mon, bs, force);
445 static void do_change_block(Monitor *mon, const char *device,
446 const char *filename, const char *fmt)
448 BlockDriverState *bs;
449 BlockDriver *drv = NULL;
451 bs = bdrv_find(device);
452 if (!bs) {
453 monitor_printf(mon, "device not found\n");
454 return;
456 if (fmt) {
457 drv = bdrv_find_format(fmt);
458 if (!drv) {
459 monitor_printf(mon, "invalid format %s\n", fmt);
460 return;
463 if (eject_device(mon, bs, 0) < 0)
464 return;
465 bdrv_open2(bs, filename, 0, drv);
466 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
469 static void change_vnc_password_cb(Monitor *mon, const char *password,
470 void *opaque)
472 if (vnc_display_password(NULL, password) < 0)
473 monitor_printf(mon, "could not set VNC server password\n");
475 monitor_read_command(mon, 1);
478 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
480 if (strcmp(target, "passwd") == 0 ||
481 strcmp(target, "password") == 0) {
482 if (arg) {
483 char password[9];
484 strncpy(password, arg, sizeof(password));
485 password[sizeof(password) - 1] = '\0';
486 change_vnc_password_cb(mon, password, NULL);
487 } else {
488 monitor_read_password(mon, change_vnc_password_cb, NULL);
490 } else {
491 if (vnc_display_open(NULL, target) < 0)
492 monitor_printf(mon, "could not start VNC server on %s\n", target);
496 static void do_change(Monitor *mon, const char *device, const char *target,
497 const char *arg)
499 if (strcmp(device, "vnc") == 0) {
500 do_change_vnc(mon, target, arg);
501 } else {
502 do_change_block(mon, device, target, arg);
506 static void do_screen_dump(Monitor *mon, const char *filename)
508 vga_hw_screen_dump(filename);
511 static void do_logfile(Monitor *mon, const char *filename)
513 cpu_set_log_filename(filename);
516 static void do_log(Monitor *mon, const char *items)
518 int mask;
520 if (!strcmp(items, "none")) {
521 mask = 0;
522 } else {
523 mask = cpu_str_to_log_mask(items);
524 if (!mask) {
525 help_cmd(mon, "log");
526 return;
529 cpu_set_log(mask);
532 static void do_singlestep(Monitor *mon, const char *option)
534 if (!option || !strcmp(option, "on")) {
535 singlestep = 1;
536 } else if (!strcmp(option, "off")) {
537 singlestep = 0;
538 } else {
539 monitor_printf(mon, "unexpected option %s\n", option);
543 static void do_stop(Monitor *mon)
545 vm_stop(EXCP_INTERRUPT);
548 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
550 struct bdrv_iterate_context {
551 Monitor *mon;
552 int err;
555 static void do_cont(Monitor *mon)
557 struct bdrv_iterate_context context = { mon, 0 };
559 bdrv_iterate(encrypted_bdrv_it, &context);
560 /* only resume the vm if all keys are set and valid */
561 if (!context.err)
562 vm_start();
565 static void bdrv_key_cb(void *opaque, int err)
567 Monitor *mon = opaque;
569 /* another key was set successfully, retry to continue */
570 if (!err)
571 do_cont(mon);
574 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
576 struct bdrv_iterate_context *context = opaque;
578 if (!context->err && bdrv_key_required(bs)) {
579 context->err = -EBUSY;
580 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
581 context->mon);
585 static void do_gdbserver(Monitor *mon, const char *device)
587 if (!device)
588 device = "tcp::" DEFAULT_GDBSTUB_PORT;
589 if (gdbserver_start(device) < 0) {
590 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
591 device);
592 } else if (strcmp(device, "none") == 0) {
593 monitor_printf(mon, "Disabled gdbserver\n");
594 } else {
595 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
596 device);
600 static void do_watchdog_action(Monitor *mon, const char *action)
602 if (select_watchdog_action(action) == -1) {
603 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
607 static void monitor_printc(Monitor *mon, int c)
609 monitor_printf(mon, "'");
610 switch(c) {
611 case '\'':
612 monitor_printf(mon, "\\'");
613 break;
614 case '\\':
615 monitor_printf(mon, "\\\\");
616 break;
617 case '\n':
618 monitor_printf(mon, "\\n");
619 break;
620 case '\r':
621 monitor_printf(mon, "\\r");
622 break;
623 default:
624 if (c >= 32 && c <= 126) {
625 monitor_printf(mon, "%c", c);
626 } else {
627 monitor_printf(mon, "\\x%02x", c);
629 break;
631 monitor_printf(mon, "'");
634 static void memory_dump(Monitor *mon, int count, int format, int wsize,
635 target_phys_addr_t addr, int is_physical)
637 CPUState *env;
638 int nb_per_line, l, line_size, i, max_digits, len;
639 uint8_t buf[16];
640 uint64_t v;
642 if (format == 'i') {
643 int flags;
644 flags = 0;
645 env = mon_get_cpu();
646 if (!env && !is_physical)
647 return;
648 #ifdef TARGET_I386
649 if (wsize == 2) {
650 flags = 1;
651 } else if (wsize == 4) {
652 flags = 0;
653 } else {
654 /* as default we use the current CS size */
655 flags = 0;
656 if (env) {
657 #ifdef TARGET_X86_64
658 if ((env->efer & MSR_EFER_LMA) &&
659 (env->segs[R_CS].flags & DESC_L_MASK))
660 flags = 2;
661 else
662 #endif
663 if (!(env->segs[R_CS].flags & DESC_B_MASK))
664 flags = 1;
667 #endif
668 monitor_disas(mon, env, addr, count, is_physical, flags);
669 return;
672 len = wsize * count;
673 if (wsize == 1)
674 line_size = 8;
675 else
676 line_size = 16;
677 nb_per_line = line_size / wsize;
678 max_digits = 0;
680 switch(format) {
681 case 'o':
682 max_digits = (wsize * 8 + 2) / 3;
683 break;
684 default:
685 case 'x':
686 max_digits = (wsize * 8) / 4;
687 break;
688 case 'u':
689 case 'd':
690 max_digits = (wsize * 8 * 10 + 32) / 33;
691 break;
692 case 'c':
693 wsize = 1;
694 break;
697 while (len > 0) {
698 if (is_physical)
699 monitor_printf(mon, TARGET_FMT_plx ":", addr);
700 else
701 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
702 l = len;
703 if (l > line_size)
704 l = line_size;
705 if (is_physical) {
706 cpu_physical_memory_rw(addr, buf, l, 0);
707 } else {
708 env = mon_get_cpu();
709 if (!env)
710 break;
711 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
712 monitor_printf(mon, " Cannot access memory\n");
713 break;
716 i = 0;
717 while (i < l) {
718 switch(wsize) {
719 default:
720 case 1:
721 v = ldub_raw(buf + i);
722 break;
723 case 2:
724 v = lduw_raw(buf + i);
725 break;
726 case 4:
727 v = (uint32_t)ldl_raw(buf + i);
728 break;
729 case 8:
730 v = ldq_raw(buf + i);
731 break;
733 monitor_printf(mon, " ");
734 switch(format) {
735 case 'o':
736 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
737 break;
738 case 'x':
739 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
740 break;
741 case 'u':
742 monitor_printf(mon, "%*" PRIu64, max_digits, v);
743 break;
744 case 'd':
745 monitor_printf(mon, "%*" PRId64, max_digits, v);
746 break;
747 case 'c':
748 monitor_printc(mon, v);
749 break;
751 i += wsize;
753 monitor_printf(mon, "\n");
754 addr += l;
755 len -= l;
759 #if TARGET_LONG_BITS == 64
760 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
761 #else
762 #define GET_TLONG(h, l) (l)
763 #endif
765 static void do_memory_dump(Monitor *mon, int count, int format, int size,
766 uint32_t addrh, uint32_t addrl)
768 target_long addr = GET_TLONG(addrh, addrl);
769 memory_dump(mon, count, format, size, addr, 0);
772 #if TARGET_PHYS_ADDR_BITS > 32
773 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
774 #else
775 #define GET_TPHYSADDR(h, l) (l)
776 #endif
778 static void do_physical_memory_dump(Monitor *mon, int count, int format,
779 int size, uint32_t addrh, uint32_t addrl)
782 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
783 memory_dump(mon, count, format, size, addr, 1);
786 static void do_print(Monitor *mon, int count, int format, int size,
787 unsigned int valh, unsigned int vall)
789 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
790 #if TARGET_PHYS_ADDR_BITS == 32
791 switch(format) {
792 case 'o':
793 monitor_printf(mon, "%#o", val);
794 break;
795 case 'x':
796 monitor_printf(mon, "%#x", val);
797 break;
798 case 'u':
799 monitor_printf(mon, "%u", val);
800 break;
801 default:
802 case 'd':
803 monitor_printf(mon, "%d", val);
804 break;
805 case 'c':
806 monitor_printc(mon, val);
807 break;
809 #else
810 switch(format) {
811 case 'o':
812 monitor_printf(mon, "%#" PRIo64, val);
813 break;
814 case 'x':
815 monitor_printf(mon, "%#" PRIx64, val);
816 break;
817 case 'u':
818 monitor_printf(mon, "%" PRIu64, val);
819 break;
820 default:
821 case 'd':
822 monitor_printf(mon, "%" PRId64, val);
823 break;
824 case 'c':
825 monitor_printc(mon, val);
826 break;
828 #endif
829 monitor_printf(mon, "\n");
832 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
833 uint32_t size, const char *filename)
835 FILE *f;
836 target_long addr = GET_TLONG(valh, vall);
837 uint32_t l;
838 CPUState *env;
839 uint8_t buf[1024];
841 env = mon_get_cpu();
842 if (!env)
843 return;
845 f = fopen(filename, "wb");
846 if (!f) {
847 monitor_printf(mon, "could not open '%s'\n", filename);
848 return;
850 while (size != 0) {
851 l = sizeof(buf);
852 if (l > size)
853 l = size;
854 cpu_memory_rw_debug(env, addr, buf, l, 0);
855 fwrite(buf, 1, l, f);
856 addr += l;
857 size -= l;
859 fclose(f);
862 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
863 unsigned int vall, uint32_t size,
864 const char *filename)
866 FILE *f;
867 uint32_t l;
868 uint8_t buf[1024];
869 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
871 f = fopen(filename, "wb");
872 if (!f) {
873 monitor_printf(mon, "could not open '%s'\n", filename);
874 return;
876 while (size != 0) {
877 l = sizeof(buf);
878 if (l > size)
879 l = size;
880 cpu_physical_memory_rw(addr, buf, l, 0);
881 fwrite(buf, 1, l, f);
882 fflush(f);
883 addr += l;
884 size -= l;
886 fclose(f);
889 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
891 uint32_t addr;
892 uint8_t buf[1];
893 uint16_t sum;
895 sum = 0;
896 for(addr = start; addr < (start + size); addr++) {
897 cpu_physical_memory_rw(addr, buf, 1, 0);
898 /* BSD sum algorithm ('sum' Unix command) */
899 sum = (sum >> 1) | (sum << 15);
900 sum += buf[0];
902 monitor_printf(mon, "%05d\n", sum);
905 typedef struct {
906 int keycode;
907 const char *name;
908 } KeyDef;
910 static const KeyDef key_defs[] = {
911 { 0x2a, "shift" },
912 { 0x36, "shift_r" },
914 { 0x38, "alt" },
915 { 0xb8, "alt_r" },
916 { 0x64, "altgr" },
917 { 0xe4, "altgr_r" },
918 { 0x1d, "ctrl" },
919 { 0x9d, "ctrl_r" },
921 { 0xdd, "menu" },
923 { 0x01, "esc" },
925 { 0x02, "1" },
926 { 0x03, "2" },
927 { 0x04, "3" },
928 { 0x05, "4" },
929 { 0x06, "5" },
930 { 0x07, "6" },
931 { 0x08, "7" },
932 { 0x09, "8" },
933 { 0x0a, "9" },
934 { 0x0b, "0" },
935 { 0x0c, "minus" },
936 { 0x0d, "equal" },
937 { 0x0e, "backspace" },
939 { 0x0f, "tab" },
940 { 0x10, "q" },
941 { 0x11, "w" },
942 { 0x12, "e" },
943 { 0x13, "r" },
944 { 0x14, "t" },
945 { 0x15, "y" },
946 { 0x16, "u" },
947 { 0x17, "i" },
948 { 0x18, "o" },
949 { 0x19, "p" },
951 { 0x1c, "ret" },
953 { 0x1e, "a" },
954 { 0x1f, "s" },
955 { 0x20, "d" },
956 { 0x21, "f" },
957 { 0x22, "g" },
958 { 0x23, "h" },
959 { 0x24, "j" },
960 { 0x25, "k" },
961 { 0x26, "l" },
963 { 0x2c, "z" },
964 { 0x2d, "x" },
965 { 0x2e, "c" },
966 { 0x2f, "v" },
967 { 0x30, "b" },
968 { 0x31, "n" },
969 { 0x32, "m" },
970 { 0x33, "comma" },
971 { 0x34, "dot" },
972 { 0x35, "slash" },
974 { 0x37, "asterisk" },
976 { 0x39, "spc" },
977 { 0x3a, "caps_lock" },
978 { 0x3b, "f1" },
979 { 0x3c, "f2" },
980 { 0x3d, "f3" },
981 { 0x3e, "f4" },
982 { 0x3f, "f5" },
983 { 0x40, "f6" },
984 { 0x41, "f7" },
985 { 0x42, "f8" },
986 { 0x43, "f9" },
987 { 0x44, "f10" },
988 { 0x45, "num_lock" },
989 { 0x46, "scroll_lock" },
991 { 0xb5, "kp_divide" },
992 { 0x37, "kp_multiply" },
993 { 0x4a, "kp_subtract" },
994 { 0x4e, "kp_add" },
995 { 0x9c, "kp_enter" },
996 { 0x53, "kp_decimal" },
997 { 0x54, "sysrq" },
999 { 0x52, "kp_0" },
1000 { 0x4f, "kp_1" },
1001 { 0x50, "kp_2" },
1002 { 0x51, "kp_3" },
1003 { 0x4b, "kp_4" },
1004 { 0x4c, "kp_5" },
1005 { 0x4d, "kp_6" },
1006 { 0x47, "kp_7" },
1007 { 0x48, "kp_8" },
1008 { 0x49, "kp_9" },
1010 { 0x56, "<" },
1012 { 0x57, "f11" },
1013 { 0x58, "f12" },
1015 { 0xb7, "print" },
1017 { 0xc7, "home" },
1018 { 0xc9, "pgup" },
1019 { 0xd1, "pgdn" },
1020 { 0xcf, "end" },
1022 { 0xcb, "left" },
1023 { 0xc8, "up" },
1024 { 0xd0, "down" },
1025 { 0xcd, "right" },
1027 { 0xd2, "insert" },
1028 { 0xd3, "delete" },
1029 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1030 { 0xf0, "stop" },
1031 { 0xf1, "again" },
1032 { 0xf2, "props" },
1033 { 0xf3, "undo" },
1034 { 0xf4, "front" },
1035 { 0xf5, "copy" },
1036 { 0xf6, "open" },
1037 { 0xf7, "paste" },
1038 { 0xf8, "find" },
1039 { 0xf9, "cut" },
1040 { 0xfa, "lf" },
1041 { 0xfb, "help" },
1042 { 0xfc, "meta_l" },
1043 { 0xfd, "meta_r" },
1044 { 0xfe, "compose" },
1045 #endif
1046 { 0, NULL },
1049 static int get_keycode(const char *key)
1051 const KeyDef *p;
1052 char *endp;
1053 int ret;
1055 for(p = key_defs; p->name != NULL; p++) {
1056 if (!strcmp(key, p->name))
1057 return p->keycode;
1059 if (strstart(key, "0x", NULL)) {
1060 ret = strtoul(key, &endp, 0);
1061 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1062 return ret;
1064 return -1;
1067 #define MAX_KEYCODES 16
1068 static uint8_t keycodes[MAX_KEYCODES];
1069 static int nb_pending_keycodes;
1070 static QEMUTimer *key_timer;
1072 static void release_keys(void *opaque)
1074 int keycode;
1076 while (nb_pending_keycodes > 0) {
1077 nb_pending_keycodes--;
1078 keycode = keycodes[nb_pending_keycodes];
1079 if (keycode & 0x80)
1080 kbd_put_keycode(0xe0);
1081 kbd_put_keycode(keycode | 0x80);
1085 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1086 int hold_time)
1088 char keyname_buf[16];
1089 char *separator;
1090 int keyname_len, keycode, i;
1092 if (nb_pending_keycodes > 0) {
1093 qemu_del_timer(key_timer);
1094 release_keys(NULL);
1096 if (!has_hold_time)
1097 hold_time = 100;
1098 i = 0;
1099 while (1) {
1100 separator = strchr(string, '-');
1101 keyname_len = separator ? separator - string : strlen(string);
1102 if (keyname_len > 0) {
1103 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1104 if (keyname_len > sizeof(keyname_buf) - 1) {
1105 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1106 return;
1108 if (i == MAX_KEYCODES) {
1109 monitor_printf(mon, "too many keys\n");
1110 return;
1112 keyname_buf[keyname_len] = 0;
1113 keycode = get_keycode(keyname_buf);
1114 if (keycode < 0) {
1115 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1116 return;
1118 keycodes[i++] = keycode;
1120 if (!separator)
1121 break;
1122 string = separator + 1;
1124 nb_pending_keycodes = i;
1125 /* key down events */
1126 for (i = 0; i < nb_pending_keycodes; i++) {
1127 keycode = keycodes[i];
1128 if (keycode & 0x80)
1129 kbd_put_keycode(0xe0);
1130 kbd_put_keycode(keycode & 0x7f);
1132 /* delayed key up events */
1133 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1134 muldiv64(ticks_per_sec, hold_time, 1000));
1137 static int mouse_button_state;
1139 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1140 const char *dz_str)
1142 int dx, dy, dz;
1143 dx = strtol(dx_str, NULL, 0);
1144 dy = strtol(dy_str, NULL, 0);
1145 dz = 0;
1146 if (dz_str)
1147 dz = strtol(dz_str, NULL, 0);
1148 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1151 static void do_mouse_button(Monitor *mon, int button_state)
1153 mouse_button_state = button_state;
1154 kbd_mouse_event(0, 0, 0, mouse_button_state);
1157 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1158 int addr, int has_index, int index)
1160 uint32_t val;
1161 int suffix;
1163 if (has_index) {
1164 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1165 addr++;
1167 addr &= 0xffff;
1169 switch(size) {
1170 default:
1171 case 1:
1172 val = cpu_inb(NULL, addr);
1173 suffix = 'b';
1174 break;
1175 case 2:
1176 val = cpu_inw(NULL, addr);
1177 suffix = 'w';
1178 break;
1179 case 4:
1180 val = cpu_inl(NULL, addr);
1181 suffix = 'l';
1182 break;
1184 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1185 suffix, addr, size * 2, val);
1188 static void do_ioport_write(Monitor *mon, int count, int format, int size,
1189 int addr, int val)
1191 addr &= IOPORTS_MASK;
1193 switch (size) {
1194 default:
1195 case 1:
1196 cpu_outb(NULL, addr, val);
1197 break;
1198 case 2:
1199 cpu_outw(NULL, addr, val);
1200 break;
1201 case 4:
1202 cpu_outl(NULL, addr, val);
1203 break;
1207 static void do_boot_set(Monitor *mon, const char *bootdevice)
1209 int res;
1211 res = qemu_boot_set(bootdevice);
1212 if (res == 0) {
1213 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1214 } else if (res > 0) {
1215 monitor_printf(mon, "setting boot device list failed\n");
1216 } else {
1217 monitor_printf(mon, "no function defined to set boot device list for "
1218 "this architecture\n");
1222 static void do_system_reset(Monitor *mon)
1224 qemu_system_reset_request();
1227 static void do_system_powerdown(Monitor *mon)
1229 qemu_system_powerdown_request();
1232 #if defined(TARGET_I386)
1233 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1235 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1236 addr,
1237 pte & mask,
1238 pte & PG_GLOBAL_MASK ? 'G' : '-',
1239 pte & PG_PSE_MASK ? 'P' : '-',
1240 pte & PG_DIRTY_MASK ? 'D' : '-',
1241 pte & PG_ACCESSED_MASK ? 'A' : '-',
1242 pte & PG_PCD_MASK ? 'C' : '-',
1243 pte & PG_PWT_MASK ? 'T' : '-',
1244 pte & PG_USER_MASK ? 'U' : '-',
1245 pte & PG_RW_MASK ? 'W' : '-');
1248 static void tlb_info(Monitor *mon)
1250 CPUState *env;
1251 int l1, l2;
1252 uint32_t pgd, pde, pte;
1254 env = mon_get_cpu();
1255 if (!env)
1256 return;
1258 if (!(env->cr[0] & CR0_PG_MASK)) {
1259 monitor_printf(mon, "PG disabled\n");
1260 return;
1262 pgd = env->cr[3] & ~0xfff;
1263 for(l1 = 0; l1 < 1024; l1++) {
1264 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1265 pde = le32_to_cpu(pde);
1266 if (pde & PG_PRESENT_MASK) {
1267 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1268 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1269 } else {
1270 for(l2 = 0; l2 < 1024; l2++) {
1271 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1272 (uint8_t *)&pte, 4);
1273 pte = le32_to_cpu(pte);
1274 if (pte & PG_PRESENT_MASK) {
1275 print_pte(mon, (l1 << 22) + (l2 << 12),
1276 pte & ~PG_PSE_MASK,
1277 ~0xfff);
1285 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1286 uint32_t end, int prot)
1288 int prot1;
1289 prot1 = *plast_prot;
1290 if (prot != prot1) {
1291 if (*pstart != -1) {
1292 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1293 *pstart, end, end - *pstart,
1294 prot1 & PG_USER_MASK ? 'u' : '-',
1295 'r',
1296 prot1 & PG_RW_MASK ? 'w' : '-');
1298 if (prot != 0)
1299 *pstart = end;
1300 else
1301 *pstart = -1;
1302 *plast_prot = prot;
1306 static void mem_info(Monitor *mon)
1308 CPUState *env;
1309 int l1, l2, prot, last_prot;
1310 uint32_t pgd, pde, pte, start, end;
1312 env = mon_get_cpu();
1313 if (!env)
1314 return;
1316 if (!(env->cr[0] & CR0_PG_MASK)) {
1317 monitor_printf(mon, "PG disabled\n");
1318 return;
1320 pgd = env->cr[3] & ~0xfff;
1321 last_prot = 0;
1322 start = -1;
1323 for(l1 = 0; l1 < 1024; l1++) {
1324 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1325 pde = le32_to_cpu(pde);
1326 end = l1 << 22;
1327 if (pde & PG_PRESENT_MASK) {
1328 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1329 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1330 mem_print(mon, &start, &last_prot, end, prot);
1331 } else {
1332 for(l2 = 0; l2 < 1024; l2++) {
1333 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1334 (uint8_t *)&pte, 4);
1335 pte = le32_to_cpu(pte);
1336 end = (l1 << 22) + (l2 << 12);
1337 if (pte & PG_PRESENT_MASK) {
1338 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1339 } else {
1340 prot = 0;
1342 mem_print(mon, &start, &last_prot, end, prot);
1345 } else {
1346 prot = 0;
1347 mem_print(mon, &start, &last_prot, end, prot);
1351 #endif
1353 #if defined(TARGET_SH4)
1355 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1357 monitor_printf(mon, " tlb%i:\t"
1358 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1359 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1360 "dirty=%hhu writethrough=%hhu\n",
1361 idx,
1362 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1363 tlb->v, tlb->sh, tlb->c, tlb->pr,
1364 tlb->d, tlb->wt);
1367 static void tlb_info(Monitor *mon)
1369 CPUState *env = mon_get_cpu();
1370 int i;
1372 monitor_printf (mon, "ITLB:\n");
1373 for (i = 0 ; i < ITLB_SIZE ; i++)
1374 print_tlb (mon, i, &env->itlb[i]);
1375 monitor_printf (mon, "UTLB:\n");
1376 for (i = 0 ; i < UTLB_SIZE ; i++)
1377 print_tlb (mon, i, &env->utlb[i]);
1380 #endif
1382 static void do_info_kqemu(Monitor *mon)
1384 #ifdef CONFIG_KQEMU
1385 CPUState *env;
1386 int val;
1387 val = 0;
1388 env = mon_get_cpu();
1389 if (!env) {
1390 monitor_printf(mon, "No cpu initialized yet");
1391 return;
1393 val = env->kqemu_enabled;
1394 monitor_printf(mon, "kqemu support: ");
1395 switch(val) {
1396 default:
1397 case 0:
1398 monitor_printf(mon, "disabled\n");
1399 break;
1400 case 1:
1401 monitor_printf(mon, "enabled for user code\n");
1402 break;
1403 case 2:
1404 monitor_printf(mon, "enabled for user and kernel code\n");
1405 break;
1407 #else
1408 monitor_printf(mon, "kqemu support: not compiled\n");
1409 #endif
1412 static void do_info_kvm(Monitor *mon)
1414 #ifdef CONFIG_KVM
1415 monitor_printf(mon, "kvm support: ");
1416 if (kvm_enabled())
1417 monitor_printf(mon, "enabled\n");
1418 else
1419 monitor_printf(mon, "disabled\n");
1420 #else
1421 monitor_printf(mon, "kvm support: not compiled\n");
1422 #endif
1425 static void do_info_numa(Monitor *mon)
1427 int i;
1428 CPUState *env;
1430 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1431 for (i = 0; i < nb_numa_nodes; i++) {
1432 monitor_printf(mon, "node %d cpus:", i);
1433 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1434 if (env->numa_node == i) {
1435 monitor_printf(mon, " %d", env->cpu_index);
1438 monitor_printf(mon, "\n");
1439 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1440 node_mem[i] >> 20);
1444 #ifdef CONFIG_PROFILER
1446 int64_t kqemu_time;
1447 int64_t qemu_time;
1448 int64_t kqemu_exec_count;
1449 int64_t dev_time;
1450 int64_t kqemu_ret_int_count;
1451 int64_t kqemu_ret_excp_count;
1452 int64_t kqemu_ret_intr_count;
1454 static void do_info_profile(Monitor *mon)
1456 int64_t total;
1457 total = qemu_time;
1458 if (total == 0)
1459 total = 1;
1460 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1461 dev_time, dev_time / (double)ticks_per_sec);
1462 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1463 qemu_time, qemu_time / (double)ticks_per_sec);
1464 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1465 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1466 PRId64 "\n",
1467 kqemu_time, kqemu_time / (double)ticks_per_sec,
1468 kqemu_time / (double)total * 100.0,
1469 kqemu_exec_count,
1470 kqemu_ret_int_count,
1471 kqemu_ret_excp_count,
1472 kqemu_ret_intr_count);
1473 qemu_time = 0;
1474 kqemu_time = 0;
1475 kqemu_exec_count = 0;
1476 dev_time = 0;
1477 kqemu_ret_int_count = 0;
1478 kqemu_ret_excp_count = 0;
1479 kqemu_ret_intr_count = 0;
1480 #ifdef CONFIG_KQEMU
1481 kqemu_record_dump();
1482 #endif
1484 #else
1485 static void do_info_profile(Monitor *mon)
1487 monitor_printf(mon, "Internal profiler not compiled\n");
1489 #endif
1491 /* Capture support */
1492 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1494 static void do_info_capture(Monitor *mon)
1496 int i;
1497 CaptureState *s;
1499 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1500 monitor_printf(mon, "[%d]: ", i);
1501 s->ops.info (s->opaque);
1505 #ifdef HAS_AUDIO
1506 static void do_stop_capture(Monitor *mon, int n)
1508 int i;
1509 CaptureState *s;
1511 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1512 if (i == n) {
1513 s->ops.destroy (s->opaque);
1514 LIST_REMOVE (s, entries);
1515 qemu_free (s);
1516 return;
1521 static void do_wav_capture(Monitor *mon, const char *path,
1522 int has_freq, int freq,
1523 int has_bits, int bits,
1524 int has_channels, int nchannels)
1526 CaptureState *s;
1528 s = qemu_mallocz (sizeof (*s));
1530 freq = has_freq ? freq : 44100;
1531 bits = has_bits ? bits : 16;
1532 nchannels = has_channels ? nchannels : 2;
1534 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1535 monitor_printf(mon, "Faied to add wave capture\n");
1536 qemu_free (s);
1538 LIST_INSERT_HEAD (&capture_head, s, entries);
1540 #endif
1542 #if defined(TARGET_I386)
1543 static void do_inject_nmi(Monitor *mon, int cpu_index)
1545 CPUState *env;
1547 for (env = first_cpu; env != NULL; env = env->next_cpu)
1548 if (env->cpu_index == cpu_index) {
1549 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1550 break;
1553 #endif
1555 static void do_info_status(Monitor *mon)
1557 if (vm_running) {
1558 if (singlestep) {
1559 monitor_printf(mon, "VM status: running (single step mode)\n");
1560 } else {
1561 monitor_printf(mon, "VM status: running\n");
1563 } else
1564 monitor_printf(mon, "VM status: paused\n");
1568 static void do_balloon(Monitor *mon, int value)
1570 ram_addr_t target = value;
1571 qemu_balloon(target << 20);
1574 static void do_info_balloon(Monitor *mon)
1576 ram_addr_t actual;
1578 actual = qemu_balloon_status();
1579 if (kvm_enabled() && !kvm_has_sync_mmu())
1580 monitor_printf(mon, "Using KVM without synchronous MMU, "
1581 "ballooning disabled\n");
1582 else if (actual == 0)
1583 monitor_printf(mon, "Ballooning not activated in VM\n");
1584 else
1585 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1588 static qemu_acl *find_acl(Monitor *mon, const char *name)
1590 qemu_acl *acl = qemu_acl_find(name);
1592 if (!acl) {
1593 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1595 return acl;
1598 static void do_acl_show(Monitor *mon, const char *aclname)
1600 qemu_acl *acl = find_acl(mon, aclname);
1601 qemu_acl_entry *entry;
1602 int i = 0;
1604 if (acl) {
1605 monitor_printf(mon, "policy: %s\n",
1606 acl->defaultDeny ? "deny" : "allow");
1607 TAILQ_FOREACH(entry, &acl->entries, next) {
1608 i++;
1609 monitor_printf(mon, "%d: %s %s\n", i,
1610 entry->deny ? "deny" : "allow", entry->match);
1615 static void do_acl_reset(Monitor *mon, const char *aclname)
1617 qemu_acl *acl = find_acl(mon, aclname);
1619 if (acl) {
1620 qemu_acl_reset(acl);
1621 monitor_printf(mon, "acl: removed all rules\n");
1625 static void do_acl_policy(Monitor *mon, const char *aclname,
1626 const char *policy)
1628 qemu_acl *acl = find_acl(mon, aclname);
1630 if (acl) {
1631 if (strcmp(policy, "allow") == 0) {
1632 acl->defaultDeny = 0;
1633 monitor_printf(mon, "acl: policy set to 'allow'\n");
1634 } else if (strcmp(policy, "deny") == 0) {
1635 acl->defaultDeny = 1;
1636 monitor_printf(mon, "acl: policy set to 'deny'\n");
1637 } else {
1638 monitor_printf(mon, "acl: unknown policy '%s', "
1639 "expected 'deny' or 'allow'\n", policy);
1644 static void do_acl_add(Monitor *mon, const char *aclname,
1645 const char *match, const char *policy,
1646 int has_index, int index)
1648 qemu_acl *acl = find_acl(mon, aclname);
1649 int deny, ret;
1651 if (acl) {
1652 if (strcmp(policy, "allow") == 0) {
1653 deny = 0;
1654 } else if (strcmp(policy, "deny") == 0) {
1655 deny = 1;
1656 } else {
1657 monitor_printf(mon, "acl: unknown policy '%s', "
1658 "expected 'deny' or 'allow'\n", policy);
1659 return;
1661 if (has_index)
1662 ret = qemu_acl_insert(acl, deny, match, index);
1663 else
1664 ret = qemu_acl_append(acl, deny, match);
1665 if (ret < 0)
1666 monitor_printf(mon, "acl: unable to add acl entry\n");
1667 else
1668 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1672 static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1674 qemu_acl *acl = find_acl(mon, aclname);
1675 int ret;
1677 if (acl) {
1678 ret = qemu_acl_remove(acl, match);
1679 if (ret < 0)
1680 monitor_printf(mon, "acl: no matching acl entry\n");
1681 else
1682 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1686 #if defined(TARGET_I386)
1687 static void do_inject_mce(Monitor *mon,
1688 int cpu_index, int bank,
1689 unsigned status_hi, unsigned status_lo,
1690 unsigned mcg_status_hi, unsigned mcg_status_lo,
1691 unsigned addr_hi, unsigned addr_lo,
1692 unsigned misc_hi, unsigned misc_lo)
1694 CPUState *cenv;
1695 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1696 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1697 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1698 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1700 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1701 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1702 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1703 break;
1706 #endif
1708 static const mon_cmd_t mon_cmds[] = {
1709 #include "qemu-monitor.h"
1710 { NULL, NULL, },
1713 /* Please update qemu-monitor.hx when adding or changing commands */
1714 static const mon_cmd_t info_cmds[] = {
1715 { "version", "", do_info_version,
1716 "", "show the version of QEMU" },
1717 { "network", "", do_info_network,
1718 "", "show the network state" },
1719 { "chardev", "", qemu_chr_info,
1720 "", "show the character devices" },
1721 { "block", "", bdrv_info,
1722 "", "show the block devices" },
1723 { "blockstats", "", bdrv_info_stats,
1724 "", "show block device statistics" },
1725 { "registers", "", do_info_registers,
1726 "", "show the cpu registers" },
1727 { "cpus", "", do_info_cpus,
1728 "", "show infos for each CPU" },
1729 { "history", "", do_info_history,
1730 "", "show the command line history", },
1731 { "irq", "", irq_info,
1732 "", "show the interrupts statistics (if available)", },
1733 { "pic", "", pic_info,
1734 "", "show i8259 (PIC) state", },
1735 { "pci", "", pci_info,
1736 "", "show PCI info", },
1737 #if defined(TARGET_I386) || defined(TARGET_SH4)
1738 { "tlb", "", tlb_info,
1739 "", "show virtual to physical memory mappings", },
1740 #endif
1741 #if defined(TARGET_I386)
1742 { "mem", "", mem_info,
1743 "", "show the active virtual memory mappings", },
1744 { "hpet", "", do_info_hpet,
1745 "", "show state of HPET", },
1746 #endif
1747 { "jit", "", do_info_jit,
1748 "", "show dynamic compiler info", },
1749 { "kqemu", "", do_info_kqemu,
1750 "", "show KQEMU information", },
1751 { "kvm", "", do_info_kvm,
1752 "", "show KVM information", },
1753 { "numa", "", do_info_numa,
1754 "", "show NUMA information", },
1755 { "usb", "", usb_info,
1756 "", "show guest USB devices", },
1757 { "usbhost", "", usb_host_info,
1758 "", "show host USB devices", },
1759 { "profile", "", do_info_profile,
1760 "", "show profiling information", },
1761 { "capture", "", do_info_capture,
1762 "", "show capture information" },
1763 { "snapshots", "", do_info_snapshots,
1764 "", "show the currently saved VM snapshots" },
1765 { "status", "", do_info_status,
1766 "", "show the current VM status (running|paused)" },
1767 { "pcmcia", "", pcmcia_info,
1768 "", "show guest PCMCIA status" },
1769 { "mice", "", do_info_mice,
1770 "", "show which guest mouse is receiving events" },
1771 { "vnc", "", do_info_vnc,
1772 "", "show the vnc server status"},
1773 { "name", "", do_info_name,
1774 "", "show the current VM name" },
1775 { "uuid", "", do_info_uuid,
1776 "", "show the current VM UUID" },
1777 #if defined(TARGET_PPC)
1778 { "cpustats", "", do_info_cpu_stats,
1779 "", "show CPU statistics", },
1780 #endif
1781 #if defined(CONFIG_SLIRP)
1782 { "usernet", "", do_info_usernet,
1783 "", "show user network stack connection states", },
1784 #endif
1785 { "migrate", "", do_info_migrate, "", "show migration status" },
1786 { "balloon", "", do_info_balloon,
1787 "", "show balloon information" },
1788 { "qtree", "", do_info_qtree,
1789 "", "show device tree" },
1790 { NULL, NULL, },
1793 /*******************************************************************/
1795 static const char *pch;
1796 static jmp_buf expr_env;
1798 #define MD_TLONG 0
1799 #define MD_I32 1
1801 typedef struct MonitorDef {
1802 const char *name;
1803 int offset;
1804 target_long (*get_value)(const struct MonitorDef *md, int val);
1805 int type;
1806 } MonitorDef;
1808 #if defined(TARGET_I386)
1809 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1811 CPUState *env = mon_get_cpu();
1812 if (!env)
1813 return 0;
1814 return env->eip + env->segs[R_CS].base;
1816 #endif
1818 #if defined(TARGET_PPC)
1819 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1821 CPUState *env = mon_get_cpu();
1822 unsigned int u;
1823 int i;
1825 if (!env)
1826 return 0;
1828 u = 0;
1829 for (i = 0; i < 8; i++)
1830 u |= env->crf[i] << (32 - (4 * i));
1832 return u;
1835 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1837 CPUState *env = mon_get_cpu();
1838 if (!env)
1839 return 0;
1840 return env->msr;
1843 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1845 CPUState *env = mon_get_cpu();
1846 if (!env)
1847 return 0;
1848 return env->xer;
1851 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1853 CPUState *env = mon_get_cpu();
1854 if (!env)
1855 return 0;
1856 return cpu_ppc_load_decr(env);
1859 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1861 CPUState *env = mon_get_cpu();
1862 if (!env)
1863 return 0;
1864 return cpu_ppc_load_tbu(env);
1867 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1869 CPUState *env = mon_get_cpu();
1870 if (!env)
1871 return 0;
1872 return cpu_ppc_load_tbl(env);
1874 #endif
1876 #if defined(TARGET_SPARC)
1877 #ifndef TARGET_SPARC64
1878 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1880 CPUState *env = mon_get_cpu();
1881 if (!env)
1882 return 0;
1883 return GET_PSR(env);
1885 #endif
1887 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1889 CPUState *env = mon_get_cpu();
1890 if (!env)
1891 return 0;
1892 return env->regwptr[val];
1894 #endif
1896 static const MonitorDef monitor_defs[] = {
1897 #ifdef TARGET_I386
1899 #define SEG(name, seg) \
1900 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1901 { name ".base", offsetof(CPUState, segs[seg].base) },\
1902 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1904 { "eax", offsetof(CPUState, regs[0]) },
1905 { "ecx", offsetof(CPUState, regs[1]) },
1906 { "edx", offsetof(CPUState, regs[2]) },
1907 { "ebx", offsetof(CPUState, regs[3]) },
1908 { "esp|sp", offsetof(CPUState, regs[4]) },
1909 { "ebp|fp", offsetof(CPUState, regs[5]) },
1910 { "esi", offsetof(CPUState, regs[6]) },
1911 { "edi", offsetof(CPUState, regs[7]) },
1912 #ifdef TARGET_X86_64
1913 { "r8", offsetof(CPUState, regs[8]) },
1914 { "r9", offsetof(CPUState, regs[9]) },
1915 { "r10", offsetof(CPUState, regs[10]) },
1916 { "r11", offsetof(CPUState, regs[11]) },
1917 { "r12", offsetof(CPUState, regs[12]) },
1918 { "r13", offsetof(CPUState, regs[13]) },
1919 { "r14", offsetof(CPUState, regs[14]) },
1920 { "r15", offsetof(CPUState, regs[15]) },
1921 #endif
1922 { "eflags", offsetof(CPUState, eflags) },
1923 { "eip", offsetof(CPUState, eip) },
1924 SEG("cs", R_CS)
1925 SEG("ds", R_DS)
1926 SEG("es", R_ES)
1927 SEG("ss", R_SS)
1928 SEG("fs", R_FS)
1929 SEG("gs", R_GS)
1930 { "pc", 0, monitor_get_pc, },
1931 #elif defined(TARGET_PPC)
1932 /* General purpose registers */
1933 { "r0", offsetof(CPUState, gpr[0]) },
1934 { "r1", offsetof(CPUState, gpr[1]) },
1935 { "r2", offsetof(CPUState, gpr[2]) },
1936 { "r3", offsetof(CPUState, gpr[3]) },
1937 { "r4", offsetof(CPUState, gpr[4]) },
1938 { "r5", offsetof(CPUState, gpr[5]) },
1939 { "r6", offsetof(CPUState, gpr[6]) },
1940 { "r7", offsetof(CPUState, gpr[7]) },
1941 { "r8", offsetof(CPUState, gpr[8]) },
1942 { "r9", offsetof(CPUState, gpr[9]) },
1943 { "r10", offsetof(CPUState, gpr[10]) },
1944 { "r11", offsetof(CPUState, gpr[11]) },
1945 { "r12", offsetof(CPUState, gpr[12]) },
1946 { "r13", offsetof(CPUState, gpr[13]) },
1947 { "r14", offsetof(CPUState, gpr[14]) },
1948 { "r15", offsetof(CPUState, gpr[15]) },
1949 { "r16", offsetof(CPUState, gpr[16]) },
1950 { "r17", offsetof(CPUState, gpr[17]) },
1951 { "r18", offsetof(CPUState, gpr[18]) },
1952 { "r19", offsetof(CPUState, gpr[19]) },
1953 { "r20", offsetof(CPUState, gpr[20]) },
1954 { "r21", offsetof(CPUState, gpr[21]) },
1955 { "r22", offsetof(CPUState, gpr[22]) },
1956 { "r23", offsetof(CPUState, gpr[23]) },
1957 { "r24", offsetof(CPUState, gpr[24]) },
1958 { "r25", offsetof(CPUState, gpr[25]) },
1959 { "r26", offsetof(CPUState, gpr[26]) },
1960 { "r27", offsetof(CPUState, gpr[27]) },
1961 { "r28", offsetof(CPUState, gpr[28]) },
1962 { "r29", offsetof(CPUState, gpr[29]) },
1963 { "r30", offsetof(CPUState, gpr[30]) },
1964 { "r31", offsetof(CPUState, gpr[31]) },
1965 /* Floating point registers */
1966 { "f0", offsetof(CPUState, fpr[0]) },
1967 { "f1", offsetof(CPUState, fpr[1]) },
1968 { "f2", offsetof(CPUState, fpr[2]) },
1969 { "f3", offsetof(CPUState, fpr[3]) },
1970 { "f4", offsetof(CPUState, fpr[4]) },
1971 { "f5", offsetof(CPUState, fpr[5]) },
1972 { "f6", offsetof(CPUState, fpr[6]) },
1973 { "f7", offsetof(CPUState, fpr[7]) },
1974 { "f8", offsetof(CPUState, fpr[8]) },
1975 { "f9", offsetof(CPUState, fpr[9]) },
1976 { "f10", offsetof(CPUState, fpr[10]) },
1977 { "f11", offsetof(CPUState, fpr[11]) },
1978 { "f12", offsetof(CPUState, fpr[12]) },
1979 { "f13", offsetof(CPUState, fpr[13]) },
1980 { "f14", offsetof(CPUState, fpr[14]) },
1981 { "f15", offsetof(CPUState, fpr[15]) },
1982 { "f16", offsetof(CPUState, fpr[16]) },
1983 { "f17", offsetof(CPUState, fpr[17]) },
1984 { "f18", offsetof(CPUState, fpr[18]) },
1985 { "f19", offsetof(CPUState, fpr[19]) },
1986 { "f20", offsetof(CPUState, fpr[20]) },
1987 { "f21", offsetof(CPUState, fpr[21]) },
1988 { "f22", offsetof(CPUState, fpr[22]) },
1989 { "f23", offsetof(CPUState, fpr[23]) },
1990 { "f24", offsetof(CPUState, fpr[24]) },
1991 { "f25", offsetof(CPUState, fpr[25]) },
1992 { "f26", offsetof(CPUState, fpr[26]) },
1993 { "f27", offsetof(CPUState, fpr[27]) },
1994 { "f28", offsetof(CPUState, fpr[28]) },
1995 { "f29", offsetof(CPUState, fpr[29]) },
1996 { "f30", offsetof(CPUState, fpr[30]) },
1997 { "f31", offsetof(CPUState, fpr[31]) },
1998 { "fpscr", offsetof(CPUState, fpscr) },
1999 /* Next instruction pointer */
2000 { "nip|pc", offsetof(CPUState, nip) },
2001 { "lr", offsetof(CPUState, lr) },
2002 { "ctr", offsetof(CPUState, ctr) },
2003 { "decr", 0, &monitor_get_decr, },
2004 { "ccr", 0, &monitor_get_ccr, },
2005 /* Machine state register */
2006 { "msr", 0, &monitor_get_msr, },
2007 { "xer", 0, &monitor_get_xer, },
2008 { "tbu", 0, &monitor_get_tbu, },
2009 { "tbl", 0, &monitor_get_tbl, },
2010 #if defined(TARGET_PPC64)
2011 /* Address space register */
2012 { "asr", offsetof(CPUState, asr) },
2013 #endif
2014 /* Segment registers */
2015 { "sdr1", offsetof(CPUState, sdr1) },
2016 { "sr0", offsetof(CPUState, sr[0]) },
2017 { "sr1", offsetof(CPUState, sr[1]) },
2018 { "sr2", offsetof(CPUState, sr[2]) },
2019 { "sr3", offsetof(CPUState, sr[3]) },
2020 { "sr4", offsetof(CPUState, sr[4]) },
2021 { "sr5", offsetof(CPUState, sr[5]) },
2022 { "sr6", offsetof(CPUState, sr[6]) },
2023 { "sr7", offsetof(CPUState, sr[7]) },
2024 { "sr8", offsetof(CPUState, sr[8]) },
2025 { "sr9", offsetof(CPUState, sr[9]) },
2026 { "sr10", offsetof(CPUState, sr[10]) },
2027 { "sr11", offsetof(CPUState, sr[11]) },
2028 { "sr12", offsetof(CPUState, sr[12]) },
2029 { "sr13", offsetof(CPUState, sr[13]) },
2030 { "sr14", offsetof(CPUState, sr[14]) },
2031 { "sr15", offsetof(CPUState, sr[15]) },
2032 /* Too lazy to put BATs and SPRs ... */
2033 #elif defined(TARGET_SPARC)
2034 { "g0", offsetof(CPUState, gregs[0]) },
2035 { "g1", offsetof(CPUState, gregs[1]) },
2036 { "g2", offsetof(CPUState, gregs[2]) },
2037 { "g3", offsetof(CPUState, gregs[3]) },
2038 { "g4", offsetof(CPUState, gregs[4]) },
2039 { "g5", offsetof(CPUState, gregs[5]) },
2040 { "g6", offsetof(CPUState, gregs[6]) },
2041 { "g7", offsetof(CPUState, gregs[7]) },
2042 { "o0", 0, monitor_get_reg },
2043 { "o1", 1, monitor_get_reg },
2044 { "o2", 2, monitor_get_reg },
2045 { "o3", 3, monitor_get_reg },
2046 { "o4", 4, monitor_get_reg },
2047 { "o5", 5, monitor_get_reg },
2048 { "o6", 6, monitor_get_reg },
2049 { "o7", 7, monitor_get_reg },
2050 { "l0", 8, monitor_get_reg },
2051 { "l1", 9, monitor_get_reg },
2052 { "l2", 10, monitor_get_reg },
2053 { "l3", 11, monitor_get_reg },
2054 { "l4", 12, monitor_get_reg },
2055 { "l5", 13, monitor_get_reg },
2056 { "l6", 14, monitor_get_reg },
2057 { "l7", 15, monitor_get_reg },
2058 { "i0", 16, monitor_get_reg },
2059 { "i1", 17, monitor_get_reg },
2060 { "i2", 18, monitor_get_reg },
2061 { "i3", 19, monitor_get_reg },
2062 { "i4", 20, monitor_get_reg },
2063 { "i5", 21, monitor_get_reg },
2064 { "i6", 22, monitor_get_reg },
2065 { "i7", 23, monitor_get_reg },
2066 { "pc", offsetof(CPUState, pc) },
2067 { "npc", offsetof(CPUState, npc) },
2068 { "y", offsetof(CPUState, y) },
2069 #ifndef TARGET_SPARC64
2070 { "psr", 0, &monitor_get_psr, },
2071 { "wim", offsetof(CPUState, wim) },
2072 #endif
2073 { "tbr", offsetof(CPUState, tbr) },
2074 { "fsr", offsetof(CPUState, fsr) },
2075 { "f0", offsetof(CPUState, fpr[0]) },
2076 { "f1", offsetof(CPUState, fpr[1]) },
2077 { "f2", offsetof(CPUState, fpr[2]) },
2078 { "f3", offsetof(CPUState, fpr[3]) },
2079 { "f4", offsetof(CPUState, fpr[4]) },
2080 { "f5", offsetof(CPUState, fpr[5]) },
2081 { "f6", offsetof(CPUState, fpr[6]) },
2082 { "f7", offsetof(CPUState, fpr[7]) },
2083 { "f8", offsetof(CPUState, fpr[8]) },
2084 { "f9", offsetof(CPUState, fpr[9]) },
2085 { "f10", offsetof(CPUState, fpr[10]) },
2086 { "f11", offsetof(CPUState, fpr[11]) },
2087 { "f12", offsetof(CPUState, fpr[12]) },
2088 { "f13", offsetof(CPUState, fpr[13]) },
2089 { "f14", offsetof(CPUState, fpr[14]) },
2090 { "f15", offsetof(CPUState, fpr[15]) },
2091 { "f16", offsetof(CPUState, fpr[16]) },
2092 { "f17", offsetof(CPUState, fpr[17]) },
2093 { "f18", offsetof(CPUState, fpr[18]) },
2094 { "f19", offsetof(CPUState, fpr[19]) },
2095 { "f20", offsetof(CPUState, fpr[20]) },
2096 { "f21", offsetof(CPUState, fpr[21]) },
2097 { "f22", offsetof(CPUState, fpr[22]) },
2098 { "f23", offsetof(CPUState, fpr[23]) },
2099 { "f24", offsetof(CPUState, fpr[24]) },
2100 { "f25", offsetof(CPUState, fpr[25]) },
2101 { "f26", offsetof(CPUState, fpr[26]) },
2102 { "f27", offsetof(CPUState, fpr[27]) },
2103 { "f28", offsetof(CPUState, fpr[28]) },
2104 { "f29", offsetof(CPUState, fpr[29]) },
2105 { "f30", offsetof(CPUState, fpr[30]) },
2106 { "f31", offsetof(CPUState, fpr[31]) },
2107 #ifdef TARGET_SPARC64
2108 { "f32", offsetof(CPUState, fpr[32]) },
2109 { "f34", offsetof(CPUState, fpr[34]) },
2110 { "f36", offsetof(CPUState, fpr[36]) },
2111 { "f38", offsetof(CPUState, fpr[38]) },
2112 { "f40", offsetof(CPUState, fpr[40]) },
2113 { "f42", offsetof(CPUState, fpr[42]) },
2114 { "f44", offsetof(CPUState, fpr[44]) },
2115 { "f46", offsetof(CPUState, fpr[46]) },
2116 { "f48", offsetof(CPUState, fpr[48]) },
2117 { "f50", offsetof(CPUState, fpr[50]) },
2118 { "f52", offsetof(CPUState, fpr[52]) },
2119 { "f54", offsetof(CPUState, fpr[54]) },
2120 { "f56", offsetof(CPUState, fpr[56]) },
2121 { "f58", offsetof(CPUState, fpr[58]) },
2122 { "f60", offsetof(CPUState, fpr[60]) },
2123 { "f62", offsetof(CPUState, fpr[62]) },
2124 { "asi", offsetof(CPUState, asi) },
2125 { "pstate", offsetof(CPUState, pstate) },
2126 { "cansave", offsetof(CPUState, cansave) },
2127 { "canrestore", offsetof(CPUState, canrestore) },
2128 { "otherwin", offsetof(CPUState, otherwin) },
2129 { "wstate", offsetof(CPUState, wstate) },
2130 { "cleanwin", offsetof(CPUState, cleanwin) },
2131 { "fprs", offsetof(CPUState, fprs) },
2132 #endif
2133 #endif
2134 { NULL },
2137 static void expr_error(Monitor *mon, const char *msg)
2139 monitor_printf(mon, "%s\n", msg);
2140 longjmp(expr_env, 1);
2143 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2144 static int get_monitor_def(target_long *pval, const char *name)
2146 const MonitorDef *md;
2147 void *ptr;
2149 for(md = monitor_defs; md->name != NULL; md++) {
2150 if (compare_cmd(name, md->name)) {
2151 if (md->get_value) {
2152 *pval = md->get_value(md, md->offset);
2153 } else {
2154 CPUState *env = mon_get_cpu();
2155 if (!env)
2156 return -2;
2157 ptr = (uint8_t *)env + md->offset;
2158 switch(md->type) {
2159 case MD_I32:
2160 *pval = *(int32_t *)ptr;
2161 break;
2162 case MD_TLONG:
2163 *pval = *(target_long *)ptr;
2164 break;
2165 default:
2166 *pval = 0;
2167 break;
2170 return 0;
2173 return -1;
2176 static void next(void)
2178 if (pch != '\0') {
2179 pch++;
2180 while (qemu_isspace(*pch))
2181 pch++;
2185 static int64_t expr_sum(Monitor *mon);
2187 static int64_t expr_unary(Monitor *mon)
2189 int64_t n;
2190 char *p;
2191 int ret;
2193 switch(*pch) {
2194 case '+':
2195 next();
2196 n = expr_unary(mon);
2197 break;
2198 case '-':
2199 next();
2200 n = -expr_unary(mon);
2201 break;
2202 case '~':
2203 next();
2204 n = ~expr_unary(mon);
2205 break;
2206 case '(':
2207 next();
2208 n = expr_sum(mon);
2209 if (*pch != ')') {
2210 expr_error(mon, "')' expected");
2212 next();
2213 break;
2214 case '\'':
2215 pch++;
2216 if (*pch == '\0')
2217 expr_error(mon, "character constant expected");
2218 n = *pch;
2219 pch++;
2220 if (*pch != '\'')
2221 expr_error(mon, "missing terminating \' character");
2222 next();
2223 break;
2224 case '$':
2226 char buf[128], *q;
2227 target_long reg=0;
2229 pch++;
2230 q = buf;
2231 while ((*pch >= 'a' && *pch <= 'z') ||
2232 (*pch >= 'A' && *pch <= 'Z') ||
2233 (*pch >= '0' && *pch <= '9') ||
2234 *pch == '_' || *pch == '.') {
2235 if ((q - buf) < sizeof(buf) - 1)
2236 *q++ = *pch;
2237 pch++;
2239 while (qemu_isspace(*pch))
2240 pch++;
2241 *q = 0;
2242 ret = get_monitor_def(&reg, buf);
2243 if (ret == -1)
2244 expr_error(mon, "unknown register");
2245 else if (ret == -2)
2246 expr_error(mon, "no cpu defined");
2247 n = reg;
2249 break;
2250 case '\0':
2251 expr_error(mon, "unexpected end of expression");
2252 n = 0;
2253 break;
2254 default:
2255 #if TARGET_PHYS_ADDR_BITS > 32
2256 n = strtoull(pch, &p, 0);
2257 #else
2258 n = strtoul(pch, &p, 0);
2259 #endif
2260 if (pch == p) {
2261 expr_error(mon, "invalid char in expression");
2263 pch = p;
2264 while (qemu_isspace(*pch))
2265 pch++;
2266 break;
2268 return n;
2272 static int64_t expr_prod(Monitor *mon)
2274 int64_t val, val2;
2275 int op;
2277 val = expr_unary(mon);
2278 for(;;) {
2279 op = *pch;
2280 if (op != '*' && op != '/' && op != '%')
2281 break;
2282 next();
2283 val2 = expr_unary(mon);
2284 switch(op) {
2285 default:
2286 case '*':
2287 val *= val2;
2288 break;
2289 case '/':
2290 case '%':
2291 if (val2 == 0)
2292 expr_error(mon, "division by zero");
2293 if (op == '/')
2294 val /= val2;
2295 else
2296 val %= val2;
2297 break;
2300 return val;
2303 static int64_t expr_logic(Monitor *mon)
2305 int64_t val, val2;
2306 int op;
2308 val = expr_prod(mon);
2309 for(;;) {
2310 op = *pch;
2311 if (op != '&' && op != '|' && op != '^')
2312 break;
2313 next();
2314 val2 = expr_prod(mon);
2315 switch(op) {
2316 default:
2317 case '&':
2318 val &= val2;
2319 break;
2320 case '|':
2321 val |= val2;
2322 break;
2323 case '^':
2324 val ^= val2;
2325 break;
2328 return val;
2331 static int64_t expr_sum(Monitor *mon)
2333 int64_t val, val2;
2334 int op;
2336 val = expr_logic(mon);
2337 for(;;) {
2338 op = *pch;
2339 if (op != '+' && op != '-')
2340 break;
2341 next();
2342 val2 = expr_logic(mon);
2343 if (op == '+')
2344 val += val2;
2345 else
2346 val -= val2;
2348 return val;
2351 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2353 pch = *pp;
2354 if (setjmp(expr_env)) {
2355 *pp = pch;
2356 return -1;
2358 while (qemu_isspace(*pch))
2359 pch++;
2360 *pval = expr_sum(mon);
2361 *pp = pch;
2362 return 0;
2365 static int get_str(char *buf, int buf_size, const char **pp)
2367 const char *p;
2368 char *q;
2369 int c;
2371 q = buf;
2372 p = *pp;
2373 while (qemu_isspace(*p))
2374 p++;
2375 if (*p == '\0') {
2376 fail:
2377 *q = '\0';
2378 *pp = p;
2379 return -1;
2381 if (*p == '\"') {
2382 p++;
2383 while (*p != '\0' && *p != '\"') {
2384 if (*p == '\\') {
2385 p++;
2386 c = *p++;
2387 switch(c) {
2388 case 'n':
2389 c = '\n';
2390 break;
2391 case 'r':
2392 c = '\r';
2393 break;
2394 case '\\':
2395 case '\'':
2396 case '\"':
2397 break;
2398 default:
2399 qemu_printf("unsupported escape code: '\\%c'\n", c);
2400 goto fail;
2402 if ((q - buf) < buf_size - 1) {
2403 *q++ = c;
2405 } else {
2406 if ((q - buf) < buf_size - 1) {
2407 *q++ = *p;
2409 p++;
2412 if (*p != '\"') {
2413 qemu_printf("unterminated string\n");
2414 goto fail;
2416 p++;
2417 } else {
2418 while (*p != '\0' && !qemu_isspace(*p)) {
2419 if ((q - buf) < buf_size - 1) {
2420 *q++ = *p;
2422 p++;
2425 *q = '\0';
2426 *pp = p;
2427 return 0;
2431 * Store the command-name in cmdname, and return a pointer to
2432 * the remaining of the command string.
2434 static const char *get_command_name(const char *cmdline,
2435 char *cmdname, size_t nlen)
2437 size_t len;
2438 const char *p, *pstart;
2440 p = cmdline;
2441 while (qemu_isspace(*p))
2442 p++;
2443 if (*p == '\0')
2444 return NULL;
2445 pstart = p;
2446 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2447 p++;
2448 len = p - pstart;
2449 if (len > nlen - 1)
2450 len = nlen - 1;
2451 memcpy(cmdname, pstart, len);
2452 cmdname[len] = '\0';
2453 return p;
2456 static int default_fmt_format = 'x';
2457 static int default_fmt_size = 4;
2459 #define MAX_ARGS 16
2461 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2463 const char *p, *typestr;
2464 int c, nb_args, i, has_arg;
2465 const mon_cmd_t *cmd;
2466 char cmdname[256];
2467 char buf[1024];
2468 void *str_allocated[MAX_ARGS];
2469 void *args[MAX_ARGS];
2470 void (*handler_0)(Monitor *mon);
2471 void (*handler_1)(Monitor *mon, void *arg0);
2472 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2473 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2474 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2475 void *arg3);
2476 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2477 void *arg3, void *arg4);
2478 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2479 void *arg3, void *arg4, void *arg5);
2480 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2481 void *arg3, void *arg4, void *arg5, void *arg6);
2482 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2483 void *arg3, void *arg4, void *arg5, void *arg6,
2484 void *arg7);
2485 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2486 void *arg3, void *arg4, void *arg5, void *arg6,
2487 void *arg7, void *arg8);
2488 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2489 void *arg3, void *arg4, void *arg5, void *arg6,
2490 void *arg7, void *arg8, void *arg9);
2492 #ifdef DEBUG
2493 monitor_printf(mon, "command='%s'\n", cmdline);
2494 #endif
2496 /* extract the command name */
2497 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2498 if (!p)
2499 return;
2501 /* find the command */
2502 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2503 if (compare_cmd(cmdname, cmd->name))
2504 break;
2507 if (cmd->name == NULL) {
2508 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2509 return;
2512 for(i = 0; i < MAX_ARGS; i++)
2513 str_allocated[i] = NULL;
2515 /* parse the parameters */
2516 typestr = cmd->args_type;
2517 nb_args = 0;
2518 for(;;) {
2519 c = *typestr;
2520 if (c == '\0')
2521 break;
2522 typestr++;
2523 switch(c) {
2524 case 'F':
2525 case 'B':
2526 case 's':
2528 int ret;
2529 char *str;
2531 while (qemu_isspace(*p))
2532 p++;
2533 if (*typestr == '?') {
2534 typestr++;
2535 if (*p == '\0') {
2536 /* no optional string: NULL argument */
2537 str = NULL;
2538 goto add_str;
2541 ret = get_str(buf, sizeof(buf), &p);
2542 if (ret < 0) {
2543 switch(c) {
2544 case 'F':
2545 monitor_printf(mon, "%s: filename expected\n",
2546 cmdname);
2547 break;
2548 case 'B':
2549 monitor_printf(mon, "%s: block device name expected\n",
2550 cmdname);
2551 break;
2552 default:
2553 monitor_printf(mon, "%s: string expected\n", cmdname);
2554 break;
2556 goto fail;
2558 str = qemu_malloc(strlen(buf) + 1);
2559 pstrcpy(str, sizeof(buf), buf);
2560 str_allocated[nb_args] = str;
2561 add_str:
2562 if (nb_args >= MAX_ARGS) {
2563 error_args:
2564 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2565 goto fail;
2567 args[nb_args++] = str;
2569 break;
2570 case '/':
2572 int count, format, size;
2574 while (qemu_isspace(*p))
2575 p++;
2576 if (*p == '/') {
2577 /* format found */
2578 p++;
2579 count = 1;
2580 if (qemu_isdigit(*p)) {
2581 count = 0;
2582 while (qemu_isdigit(*p)) {
2583 count = count * 10 + (*p - '0');
2584 p++;
2587 size = -1;
2588 format = -1;
2589 for(;;) {
2590 switch(*p) {
2591 case 'o':
2592 case 'd':
2593 case 'u':
2594 case 'x':
2595 case 'i':
2596 case 'c':
2597 format = *p++;
2598 break;
2599 case 'b':
2600 size = 1;
2601 p++;
2602 break;
2603 case 'h':
2604 size = 2;
2605 p++;
2606 break;
2607 case 'w':
2608 size = 4;
2609 p++;
2610 break;
2611 case 'g':
2612 case 'L':
2613 size = 8;
2614 p++;
2615 break;
2616 default:
2617 goto next;
2620 next:
2621 if (*p != '\0' && !qemu_isspace(*p)) {
2622 monitor_printf(mon, "invalid char in format: '%c'\n",
2623 *p);
2624 goto fail;
2626 if (format < 0)
2627 format = default_fmt_format;
2628 if (format != 'i') {
2629 /* for 'i', not specifying a size gives -1 as size */
2630 if (size < 0)
2631 size = default_fmt_size;
2632 default_fmt_size = size;
2634 default_fmt_format = format;
2635 } else {
2636 count = 1;
2637 format = default_fmt_format;
2638 if (format != 'i') {
2639 size = default_fmt_size;
2640 } else {
2641 size = -1;
2644 if (nb_args + 3 > MAX_ARGS)
2645 goto error_args;
2646 args[nb_args++] = (void*)(long)count;
2647 args[nb_args++] = (void*)(long)format;
2648 args[nb_args++] = (void*)(long)size;
2650 break;
2651 case 'i':
2652 case 'l':
2654 int64_t val;
2656 while (qemu_isspace(*p))
2657 p++;
2658 if (*typestr == '?' || *typestr == '.') {
2659 if (*typestr == '?') {
2660 if (*p == '\0')
2661 has_arg = 0;
2662 else
2663 has_arg = 1;
2664 } else {
2665 if (*p == '.') {
2666 p++;
2667 while (qemu_isspace(*p))
2668 p++;
2669 has_arg = 1;
2670 } else {
2671 has_arg = 0;
2674 typestr++;
2675 if (nb_args >= MAX_ARGS)
2676 goto error_args;
2677 args[nb_args++] = (void *)(long)has_arg;
2678 if (!has_arg) {
2679 if (nb_args >= MAX_ARGS)
2680 goto error_args;
2681 val = -1;
2682 goto add_num;
2685 if (get_expr(mon, &val, &p))
2686 goto fail;
2687 add_num:
2688 if (c == 'i') {
2689 if (nb_args >= MAX_ARGS)
2690 goto error_args;
2691 args[nb_args++] = (void *)(long)val;
2692 } else {
2693 if ((nb_args + 1) >= MAX_ARGS)
2694 goto error_args;
2695 #if TARGET_PHYS_ADDR_BITS > 32
2696 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2697 #else
2698 args[nb_args++] = (void *)0;
2699 #endif
2700 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2703 break;
2704 case '-':
2706 int has_option;
2707 /* option */
2709 c = *typestr++;
2710 if (c == '\0')
2711 goto bad_type;
2712 while (qemu_isspace(*p))
2713 p++;
2714 has_option = 0;
2715 if (*p == '-') {
2716 p++;
2717 if (*p != c) {
2718 monitor_printf(mon, "%s: unsupported option -%c\n",
2719 cmdname, *p);
2720 goto fail;
2722 p++;
2723 has_option = 1;
2725 if (nb_args >= MAX_ARGS)
2726 goto error_args;
2727 args[nb_args++] = (void *)(long)has_option;
2729 break;
2730 default:
2731 bad_type:
2732 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2733 goto fail;
2736 /* check that all arguments were parsed */
2737 while (qemu_isspace(*p))
2738 p++;
2739 if (*p != '\0') {
2740 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2741 cmdname);
2742 goto fail;
2745 switch(nb_args) {
2746 case 0:
2747 handler_0 = cmd->handler;
2748 handler_0(mon);
2749 break;
2750 case 1:
2751 handler_1 = cmd->handler;
2752 handler_1(mon, args[0]);
2753 break;
2754 case 2:
2755 handler_2 = cmd->handler;
2756 handler_2(mon, args[0], args[1]);
2757 break;
2758 case 3:
2759 handler_3 = cmd->handler;
2760 handler_3(mon, args[0], args[1], args[2]);
2761 break;
2762 case 4:
2763 handler_4 = cmd->handler;
2764 handler_4(mon, args[0], args[1], args[2], args[3]);
2765 break;
2766 case 5:
2767 handler_5 = cmd->handler;
2768 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2769 break;
2770 case 6:
2771 handler_6 = cmd->handler;
2772 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2773 break;
2774 case 7:
2775 handler_7 = cmd->handler;
2776 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2777 args[6]);
2778 break;
2779 case 8:
2780 handler_8 = cmd->handler;
2781 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2782 args[6], args[7]);
2783 break;
2784 case 9:
2785 handler_9 = cmd->handler;
2786 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2787 args[6], args[7], args[8]);
2788 break;
2789 case 10:
2790 handler_10 = cmd->handler;
2791 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2792 args[6], args[7], args[8], args[9]);
2793 break;
2794 default:
2795 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2796 goto fail;
2798 fail:
2799 for(i = 0; i < MAX_ARGS; i++)
2800 qemu_free(str_allocated[i]);
2803 static void cmd_completion(const char *name, const char *list)
2805 const char *p, *pstart;
2806 char cmd[128];
2807 int len;
2809 p = list;
2810 for(;;) {
2811 pstart = p;
2812 p = strchr(p, '|');
2813 if (!p)
2814 p = pstart + strlen(pstart);
2815 len = p - pstart;
2816 if (len > sizeof(cmd) - 2)
2817 len = sizeof(cmd) - 2;
2818 memcpy(cmd, pstart, len);
2819 cmd[len] = '\0';
2820 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2821 readline_add_completion(cur_mon->rs, cmd);
2823 if (*p == '\0')
2824 break;
2825 p++;
2829 static void file_completion(const char *input)
2831 DIR *ffs;
2832 struct dirent *d;
2833 char path[1024];
2834 char file[1024], file_prefix[1024];
2835 int input_path_len;
2836 const char *p;
2838 p = strrchr(input, '/');
2839 if (!p) {
2840 input_path_len = 0;
2841 pstrcpy(file_prefix, sizeof(file_prefix), input);
2842 pstrcpy(path, sizeof(path), ".");
2843 } else {
2844 input_path_len = p - input + 1;
2845 memcpy(path, input, input_path_len);
2846 if (input_path_len > sizeof(path) - 1)
2847 input_path_len = sizeof(path) - 1;
2848 path[input_path_len] = '\0';
2849 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2851 #ifdef DEBUG_COMPLETION
2852 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2853 input, path, file_prefix);
2854 #endif
2855 ffs = opendir(path);
2856 if (!ffs)
2857 return;
2858 for(;;) {
2859 struct stat sb;
2860 d = readdir(ffs);
2861 if (!d)
2862 break;
2863 if (strstart(d->d_name, file_prefix, NULL)) {
2864 memcpy(file, input, input_path_len);
2865 if (input_path_len < sizeof(file))
2866 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2867 d->d_name);
2868 /* stat the file to find out if it's a directory.
2869 * In that case add a slash to speed up typing long paths
2871 stat(file, &sb);
2872 if(S_ISDIR(sb.st_mode))
2873 pstrcat(file, sizeof(file), "/");
2874 readline_add_completion(cur_mon->rs, file);
2877 closedir(ffs);
2880 static void block_completion_it(void *opaque, BlockDriverState *bs)
2882 const char *name = bdrv_get_device_name(bs);
2883 const char *input = opaque;
2885 if (input[0] == '\0' ||
2886 !strncmp(name, (char *)input, strlen(input))) {
2887 readline_add_completion(cur_mon->rs, name);
2891 /* NOTE: this parser is an approximate form of the real command parser */
2892 static void parse_cmdline(const char *cmdline,
2893 int *pnb_args, char **args)
2895 const char *p;
2896 int nb_args, ret;
2897 char buf[1024];
2899 p = cmdline;
2900 nb_args = 0;
2901 for(;;) {
2902 while (qemu_isspace(*p))
2903 p++;
2904 if (*p == '\0')
2905 break;
2906 if (nb_args >= MAX_ARGS)
2907 break;
2908 ret = get_str(buf, sizeof(buf), &p);
2909 args[nb_args] = qemu_strdup(buf);
2910 nb_args++;
2911 if (ret < 0)
2912 break;
2914 *pnb_args = nb_args;
2917 static void monitor_find_completion(const char *cmdline)
2919 const char *cmdname;
2920 char *args[MAX_ARGS];
2921 int nb_args, i, len;
2922 const char *ptype, *str;
2923 const mon_cmd_t *cmd;
2924 const KeyDef *key;
2926 parse_cmdline(cmdline, &nb_args, args);
2927 #ifdef DEBUG_COMPLETION
2928 for(i = 0; i < nb_args; i++) {
2929 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2931 #endif
2933 /* if the line ends with a space, it means we want to complete the
2934 next arg */
2935 len = strlen(cmdline);
2936 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2937 if (nb_args >= MAX_ARGS)
2938 return;
2939 args[nb_args++] = qemu_strdup("");
2941 if (nb_args <= 1) {
2942 /* command completion */
2943 if (nb_args == 0)
2944 cmdname = "";
2945 else
2946 cmdname = args[0];
2947 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2948 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2949 cmd_completion(cmdname, cmd->name);
2951 } else {
2952 /* find the command */
2953 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2954 if (compare_cmd(args[0], cmd->name))
2955 goto found;
2957 return;
2958 found:
2959 ptype = cmd->args_type;
2960 for(i = 0; i < nb_args - 2; i++) {
2961 if (*ptype != '\0') {
2962 ptype++;
2963 while (*ptype == '?')
2964 ptype++;
2967 str = args[nb_args - 1];
2968 switch(*ptype) {
2969 case 'F':
2970 /* file completion */
2971 readline_set_completion_index(cur_mon->rs, strlen(str));
2972 file_completion(str);
2973 break;
2974 case 'B':
2975 /* block device name completion */
2976 readline_set_completion_index(cur_mon->rs, strlen(str));
2977 bdrv_iterate(block_completion_it, (void *)str);
2978 break;
2979 case 's':
2980 /* XXX: more generic ? */
2981 if (!strcmp(cmd->name, "info")) {
2982 readline_set_completion_index(cur_mon->rs, strlen(str));
2983 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2984 cmd_completion(str, cmd->name);
2986 } else if (!strcmp(cmd->name, "sendkey")) {
2987 char *sep = strrchr(str, '-');
2988 if (sep)
2989 str = sep + 1;
2990 readline_set_completion_index(cur_mon->rs, strlen(str));
2991 for(key = key_defs; key->name != NULL; key++) {
2992 cmd_completion(str, key->name);
2994 } else if (!strcmp(cmd->name, "help|?")) {
2995 readline_set_completion_index(cur_mon->rs, strlen(str));
2996 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
2997 cmd_completion(str, cmd->name);
3000 break;
3001 default:
3002 break;
3005 for(i = 0; i < nb_args; i++)
3006 qemu_free(args[i]);
3009 static int monitor_can_read(void *opaque)
3011 Monitor *mon = opaque;
3013 return (mon->suspend_cnt == 0) ? 128 : 0;
3016 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3018 Monitor *old_mon = cur_mon;
3019 int i;
3021 cur_mon = opaque;
3023 if (cur_mon->rs) {
3024 for (i = 0; i < size; i++)
3025 readline_handle_byte(cur_mon->rs, buf[i]);
3026 } else {
3027 if (size == 0 || buf[size - 1] != 0)
3028 monitor_printf(cur_mon, "corrupted command\n");
3029 else
3030 monitor_handle_command(cur_mon, (char *)buf);
3033 cur_mon = old_mon;
3036 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3038 monitor_suspend(mon);
3039 monitor_handle_command(mon, cmdline);
3040 monitor_resume(mon);
3043 int monitor_suspend(Monitor *mon)
3045 if (!mon->rs)
3046 return -ENOTTY;
3047 mon->suspend_cnt++;
3048 return 0;
3051 void monitor_resume(Monitor *mon)
3053 if (!mon->rs)
3054 return;
3055 if (--mon->suspend_cnt == 0)
3056 readline_show_prompt(mon->rs);
3059 static void monitor_event(void *opaque, int event)
3061 Monitor *mon = opaque;
3063 switch (event) {
3064 case CHR_EVENT_MUX_IN:
3065 readline_restart(mon->rs);
3066 monitor_resume(mon);
3067 monitor_flush(mon);
3068 break;
3070 case CHR_EVENT_MUX_OUT:
3071 if (mon->suspend_cnt == 0)
3072 monitor_printf(mon, "\n");
3073 monitor_flush(mon);
3074 monitor_suspend(mon);
3075 break;
3077 case CHR_EVENT_RESET:
3078 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3079 "information\n", QEMU_VERSION);
3080 if (mon->chr->focus == 0)
3081 readline_show_prompt(mon->rs);
3082 break;
3088 * Local variables:
3089 * c-indent-level: 4
3090 * c-basic-offset: 4
3091 * tab-width: 8
3092 * End:
3095 void monitor_init(CharDriverState *chr, int flags)
3097 static int is_first_init = 1;
3098 Monitor *mon;
3100 if (is_first_init) {
3101 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3102 is_first_init = 0;
3105 mon = qemu_mallocz(sizeof(*mon));
3107 mon->chr = chr;
3108 mon->flags = flags;
3109 if (mon->chr->focus != 0)
3110 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3111 if (flags & MONITOR_USE_READLINE) {
3112 mon->rs = readline_init(mon, monitor_find_completion);
3113 monitor_read_command(mon, 0);
3116 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3117 mon);
3119 LIST_INSERT_HEAD(&mon_list, mon, entry);
3120 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3121 cur_mon = mon;
3124 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3126 BlockDriverState *bs = opaque;
3127 int ret = 0;
3129 if (bdrv_set_key(bs, password) != 0) {
3130 monitor_printf(mon, "invalid password\n");
3131 ret = -EPERM;
3133 if (mon->password_completion_cb)
3134 mon->password_completion_cb(mon->password_opaque, ret);
3136 monitor_read_command(mon, 1);
3139 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3140 BlockDriverCompletionFunc *completion_cb,
3141 void *opaque)
3143 int err;
3145 if (!bdrv_key_required(bs)) {
3146 if (completion_cb)
3147 completion_cb(opaque, 0);
3148 return;
3151 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3152 bdrv_get_encrypted_filename(bs));
3154 mon->password_completion_cb = completion_cb;
3155 mon->password_opaque = opaque;
3157 err = monitor_read_password(mon, bdrv_password_cb, bs);
3159 if (err && completion_cb)
3160 completion_cb(opaque, err);