Cleanup mmio coalescing functions
[qemu-kvm/fedora.git] / monitor.c
blobcfca4661a97904cb25b436e78172792422e0e8aa
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"
47 #include "exec-all.h"
49 #include "qemu-kvm.h"
51 //#define DEBUG
52 //#define DEBUG_COMPLETION
55 * Supported types:
57 * 'F' filename
58 * 'B' block device name
59 * 's' string (accept optional quote)
60 * 'i' 32 bit integer
61 * 'l' target long (32 or 64 bit)
62 * '/' optional gdb-like print format (like "/10x")
64 * '?' optional type (for 'F', 's' and 'i')
68 typedef struct mon_cmd_t {
69 const char *name;
70 const char *args_type;
71 void *handler;
72 const char *params;
73 const char *help;
74 } mon_cmd_t;
76 struct Monitor {
77 CharDriverState *chr;
78 int flags;
79 int suspend_cnt;
80 uint8_t outbuf[1024];
81 int outbuf_index;
82 ReadLineState *rs;
83 CPUState *mon_cpu;
84 BlockDriverCompletionFunc *password_completion_cb;
85 void *password_opaque;
86 LIST_ENTRY(Monitor) entry;
89 static LIST_HEAD(mon_list, Monitor) mon_list;
91 static const mon_cmd_t mon_cmds[];
92 static const mon_cmd_t info_cmds[];
94 Monitor *cur_mon = NULL;
96 static void monitor_command_cb(Monitor *mon, const char *cmdline,
97 void *opaque);
99 static void monitor_read_command(Monitor *mon, int show_prompt)
101 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
102 if (show_prompt)
103 readline_show_prompt(mon->rs);
106 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
107 void *opaque)
109 if (mon->rs) {
110 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
111 /* prompt is printed on return from the command handler */
112 return 0;
113 } else {
114 monitor_printf(mon, "terminal does not support password prompting\n");
115 return -ENOTTY;
119 void monitor_flush(Monitor *mon)
121 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
122 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
123 mon->outbuf_index = 0;
127 /* flush at every end of line or if the buffer is full */
128 static void monitor_puts(Monitor *mon, const char *str)
130 char c;
132 if (!mon)
133 return;
135 for(;;) {
136 c = *str++;
137 if (c == '\0')
138 break;
139 if (c == '\n')
140 mon->outbuf[mon->outbuf_index++] = '\r';
141 mon->outbuf[mon->outbuf_index++] = c;
142 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
143 || c == '\n')
144 monitor_flush(mon);
148 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
150 char buf[4096];
151 vsnprintf(buf, sizeof(buf), fmt, ap);
152 monitor_puts(mon, buf);
155 void monitor_printf(Monitor *mon, const char *fmt, ...)
157 va_list ap;
158 va_start(ap, fmt);
159 monitor_vprintf(mon, fmt, ap);
160 va_end(ap);
163 void monitor_print_filename(Monitor *mon, const char *filename)
165 int i;
167 for (i = 0; filename[i]; i++) {
168 switch (filename[i]) {
169 case ' ':
170 case '"':
171 case '\\':
172 monitor_printf(mon, "\\%c", filename[i]);
173 break;
174 case '\t':
175 monitor_printf(mon, "\\t");
176 break;
177 case '\r':
178 monitor_printf(mon, "\\r");
179 break;
180 case '\n':
181 monitor_printf(mon, "\\n");
182 break;
183 default:
184 monitor_printf(mon, "%c", filename[i]);
185 break;
190 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
192 va_list ap;
193 va_start(ap, fmt);
194 monitor_vprintf((Monitor *)stream, fmt, ap);
195 va_end(ap);
196 return 0;
199 static int compare_cmd(const char *name, const char *list)
201 const char *p, *pstart;
202 int len;
203 len = strlen(name);
204 p = list;
205 for(;;) {
206 pstart = p;
207 p = strchr(p, '|');
208 if (!p)
209 p = pstart + strlen(pstart);
210 if ((p - pstart) == len && !memcmp(pstart, name, len))
211 return 1;
212 if (*p == '\0')
213 break;
214 p++;
216 return 0;
219 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
220 const char *prefix, const char *name)
222 const mon_cmd_t *cmd;
224 for(cmd = cmds; cmd->name != NULL; cmd++) {
225 if (!name || !strcmp(name, cmd->name))
226 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
227 cmd->params, cmd->help);
231 static void help_cmd(Monitor *mon, const char *name)
233 if (name && !strcmp(name, "info")) {
234 help_cmd_dump(mon, info_cmds, "info ", NULL);
235 } else {
236 help_cmd_dump(mon, mon_cmds, "", name);
237 if (name && !strcmp(name, "log")) {
238 const CPULogItem *item;
239 monitor_printf(mon, "Log items (comma separated):\n");
240 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
241 for(item = cpu_log_items; item->mask != 0; item++) {
242 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
248 static void do_commit(Monitor *mon, const char *device)
250 int i, all_devices;
252 all_devices = !strcmp(device, "all");
253 for (i = 0; i < nb_drives; i++) {
254 if (all_devices ||
255 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
256 bdrv_commit(drives_table[i].bdrv);
260 static void do_info(Monitor *mon, const char *item)
262 const mon_cmd_t *cmd;
263 void (*handler)(Monitor *);
265 if (!item)
266 goto help;
267 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
268 if (compare_cmd(item, cmd->name))
269 goto found;
271 help:
272 help_cmd(mon, "info");
273 return;
274 found:
275 handler = cmd->handler;
276 handler(mon);
279 static void do_info_version(Monitor *mon)
281 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
284 static void do_info_name(Monitor *mon)
286 if (qemu_name)
287 monitor_printf(mon, "%s\n", qemu_name);
290 #if defined(TARGET_I386)
291 static void do_info_hpet(Monitor *mon)
293 monitor_printf(mon, "HPET is %s by QEMU\n",
294 (no_hpet) ? "disabled" : "enabled");
296 #endif
298 static void do_info_uuid(Monitor *mon)
300 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
301 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
302 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
303 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
304 qemu_uuid[14], qemu_uuid[15]);
307 /* get the current CPU defined by the user */
308 static int mon_set_cpu(int cpu_index)
310 CPUState *env;
312 for(env = first_cpu; env != NULL; env = env->next_cpu) {
313 if (env->cpu_index == cpu_index) {
314 cur_mon->mon_cpu = env;
315 return 0;
318 return -1;
321 static CPUState *mon_get_cpu(void)
323 if (!cur_mon->mon_cpu) {
324 mon_set_cpu(0);
326 cpu_synchronize_state(cur_mon->mon_cpu, 0);
327 return cur_mon->mon_cpu;
330 static void do_info_registers(Monitor *mon)
332 CPUState *env;
333 env = mon_get_cpu();
334 if (!env)
335 return;
336 #ifdef TARGET_I386
337 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
338 X86_DUMP_FPU);
339 #else
340 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
342 #endif
345 static void do_info_cpus(Monitor *mon)
347 CPUState *env;
349 /* just to set the default cpu if not already done */
350 mon_get_cpu();
352 for(env = first_cpu; env != NULL; env = env->next_cpu) {
353 cpu_synchronize_state(env, 0);
354 monitor_printf(mon, "%c CPU #%d:",
355 (env == mon->mon_cpu) ? '*' : ' ',
356 env->cpu_index);
357 #if defined(TARGET_I386)
358 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
359 env->eip + env->segs[R_CS].base);
360 #elif defined(TARGET_PPC)
361 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
362 #elif defined(TARGET_SPARC)
363 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
364 env->pc, env->npc);
365 #elif defined(TARGET_MIPS)
366 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
367 #endif
368 if (env->halted)
369 monitor_printf(mon, " (halted)");
370 monitor_printf(mon," thread_id=%d", env->thread_id);
371 monitor_printf(mon, "\n");
375 static void do_cpu_set(Monitor *mon, int index)
377 if (mon_set_cpu(index) < 0)
378 monitor_printf(mon, "Invalid CPU index\n");
381 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
383 int state;
385 if (!strcmp(status, "online"))
386 state = 1;
387 else if (!strcmp(status, "offline"))
388 state = 0;
389 else {
390 monitor_printf(mon, "invalid status: %s\n", status);
391 return;
393 #if defined(TARGET_I386) || defined(TARGET_X86_64)
394 qemu_system_cpu_hot_add(value, state);
395 #endif
398 static void do_info_jit(Monitor *mon)
400 dump_exec_info((FILE *)mon, monitor_fprintf);
403 static void do_info_history(Monitor *mon)
405 int i;
406 const char *str;
408 if (!mon->rs)
409 return;
410 i = 0;
411 for(;;) {
412 str = readline_get_history(mon->rs, i);
413 if (!str)
414 break;
415 monitor_printf(mon, "%d: '%s'\n", i, str);
416 i++;
420 #if defined(TARGET_PPC)
421 /* XXX: not implemented in other targets */
422 static void do_info_cpu_stats(Monitor *mon)
424 CPUState *env;
426 env = mon_get_cpu();
427 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
429 #endif
431 static void do_quit(Monitor *mon)
433 exit(0);
436 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
438 if (bdrv_is_inserted(bs)) {
439 if (!force) {
440 if (!bdrv_is_removable(bs)) {
441 monitor_printf(mon, "device is not removable\n");
442 return -1;
444 if (bdrv_is_locked(bs)) {
445 monitor_printf(mon, "device is locked\n");
446 return -1;
449 bdrv_close(bs);
451 return 0;
454 static void do_eject(Monitor *mon, int force, const char *filename)
456 BlockDriverState *bs;
458 bs = bdrv_find(filename);
459 if (!bs) {
460 monitor_printf(mon, "device not found\n");
461 return;
463 eject_device(mon, bs, force);
466 static void do_change_block(Monitor *mon, const char *device,
467 const char *filename, const char *fmt)
469 BlockDriverState *bs;
470 BlockDriver *drv = NULL;
472 bs = bdrv_find(device);
473 if (!bs) {
474 monitor_printf(mon, "device not found\n");
475 return;
477 if (fmt) {
478 drv = bdrv_find_format(fmt);
479 if (!drv) {
480 monitor_printf(mon, "invalid format %s\n", fmt);
481 return;
484 if (eject_device(mon, bs, 0) < 0)
485 return;
486 bdrv_open2(bs, filename, 0, drv);
487 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
490 static void change_vnc_password_cb(Monitor *mon, const char *password,
491 void *opaque)
493 if (vnc_display_password(NULL, password) < 0)
494 monitor_printf(mon, "could not set VNC server password\n");
496 monitor_read_command(mon, 1);
499 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
501 if (strcmp(target, "passwd") == 0 ||
502 strcmp(target, "password") == 0) {
503 if (arg) {
504 char password[9];
505 strncpy(password, arg, sizeof(password));
506 password[sizeof(password) - 1] = '\0';
507 change_vnc_password_cb(mon, password, NULL);
508 } else {
509 monitor_read_password(mon, change_vnc_password_cb, NULL);
511 } else {
512 if (vnc_display_open(NULL, target) < 0)
513 monitor_printf(mon, "could not start VNC server on %s\n", target);
517 static void do_change(Monitor *mon, const char *device, const char *target,
518 const char *arg)
520 if (strcmp(device, "vnc") == 0) {
521 do_change_vnc(mon, target, arg);
522 } else {
523 do_change_block(mon, device, target, arg);
527 static void do_screen_dump(Monitor *mon, const char *filename)
529 vga_hw_screen_dump(filename);
532 static void do_logfile(Monitor *mon, const char *filename)
534 cpu_set_log_filename(filename);
537 static void do_log(Monitor *mon, const char *items)
539 int mask;
541 if (!strcmp(items, "none")) {
542 mask = 0;
543 } else {
544 mask = cpu_str_to_log_mask(items);
545 if (!mask) {
546 help_cmd(mon, "log");
547 return;
550 cpu_set_log(mask);
553 static void do_singlestep(Monitor *mon, const char *option)
555 if (!option || !strcmp(option, "on")) {
556 singlestep = 1;
557 } else if (!strcmp(option, "off")) {
558 singlestep = 0;
559 } else {
560 monitor_printf(mon, "unexpected option %s\n", option);
564 static void do_stop(Monitor *mon)
566 vm_stop(EXCP_INTERRUPT);
569 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
571 struct bdrv_iterate_context {
572 Monitor *mon;
573 int err;
576 static void do_cont(Monitor *mon)
578 struct bdrv_iterate_context context = { mon, 0 };
580 bdrv_iterate(encrypted_bdrv_it, &context);
581 /* only resume the vm if all keys are set and valid */
582 if (!context.err)
583 vm_start();
586 static void bdrv_key_cb(void *opaque, int err)
588 Monitor *mon = opaque;
590 /* another key was set successfully, retry to continue */
591 if (!err)
592 do_cont(mon);
595 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
597 struct bdrv_iterate_context *context = opaque;
599 if (!context->err && bdrv_key_required(bs)) {
600 context->err = -EBUSY;
601 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
602 context->mon);
606 static void do_gdbserver(Monitor *mon, const char *device)
608 if (!device)
609 device = "tcp::" DEFAULT_GDBSTUB_PORT;
610 if (gdbserver_start(device) < 0) {
611 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
612 device);
613 } else if (strcmp(device, "none") == 0) {
614 monitor_printf(mon, "Disabled gdbserver\n");
615 } else {
616 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
617 device);
621 static void do_watchdog_action(Monitor *mon, const char *action)
623 if (select_watchdog_action(action) == -1) {
624 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
628 static void monitor_printc(Monitor *mon, int c)
630 monitor_printf(mon, "'");
631 switch(c) {
632 case '\'':
633 monitor_printf(mon, "\\'");
634 break;
635 case '\\':
636 monitor_printf(mon, "\\\\");
637 break;
638 case '\n':
639 monitor_printf(mon, "\\n");
640 break;
641 case '\r':
642 monitor_printf(mon, "\\r");
643 break;
644 default:
645 if (c >= 32 && c <= 126) {
646 monitor_printf(mon, "%c", c);
647 } else {
648 monitor_printf(mon, "\\x%02x", c);
650 break;
652 monitor_printf(mon, "'");
655 static void memory_dump(Monitor *mon, int count, int format, int wsize,
656 target_phys_addr_t addr, int is_physical)
658 CPUState *env;
659 int nb_per_line, l, line_size, i, max_digits, len;
660 uint8_t buf[16];
661 uint64_t v;
663 if (format == 'i') {
664 int flags;
665 flags = 0;
666 env = mon_get_cpu();
667 if (!env && !is_physical)
668 return;
669 #ifdef TARGET_I386
670 if (wsize == 2) {
671 flags = 1;
672 } else if (wsize == 4) {
673 flags = 0;
674 } else {
675 /* as default we use the current CS size */
676 flags = 0;
677 if (env) {
678 #ifdef TARGET_X86_64
679 if ((env->efer & MSR_EFER_LMA) &&
680 (env->segs[R_CS].flags & DESC_L_MASK))
681 flags = 2;
682 else
683 #endif
684 if (!(env->segs[R_CS].flags & DESC_B_MASK))
685 flags = 1;
688 #endif
689 monitor_disas(mon, env, addr, count, is_physical, flags);
690 return;
693 len = wsize * count;
694 if (wsize == 1)
695 line_size = 8;
696 else
697 line_size = 16;
698 nb_per_line = line_size / wsize;
699 max_digits = 0;
701 switch(format) {
702 case 'o':
703 max_digits = (wsize * 8 + 2) / 3;
704 break;
705 default:
706 case 'x':
707 max_digits = (wsize * 8) / 4;
708 break;
709 case 'u':
710 case 'd':
711 max_digits = (wsize * 8 * 10 + 32) / 33;
712 break;
713 case 'c':
714 wsize = 1;
715 break;
718 while (len > 0) {
719 if (is_physical)
720 monitor_printf(mon, TARGET_FMT_plx ":", addr);
721 else
722 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
723 l = len;
724 if (l > line_size)
725 l = line_size;
726 if (is_physical) {
727 cpu_physical_memory_rw(addr, buf, l, 0);
728 } else {
729 env = mon_get_cpu();
730 if (!env)
731 break;
732 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
733 monitor_printf(mon, " Cannot access memory\n");
734 break;
737 i = 0;
738 while (i < l) {
739 switch(wsize) {
740 default:
741 case 1:
742 v = ldub_raw(buf + i);
743 break;
744 case 2:
745 v = lduw_raw(buf + i);
746 break;
747 case 4:
748 v = (uint32_t)ldl_raw(buf + i);
749 break;
750 case 8:
751 v = ldq_raw(buf + i);
752 break;
754 monitor_printf(mon, " ");
755 switch(format) {
756 case 'o':
757 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
758 break;
759 case 'x':
760 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
761 break;
762 case 'u':
763 monitor_printf(mon, "%*" PRIu64, max_digits, v);
764 break;
765 case 'd':
766 monitor_printf(mon, "%*" PRId64, max_digits, v);
767 break;
768 case 'c':
769 monitor_printc(mon, v);
770 break;
772 i += wsize;
774 monitor_printf(mon, "\n");
775 addr += l;
776 len -= l;
780 #if TARGET_LONG_BITS == 64
781 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
782 #else
783 #define GET_TLONG(h, l) (l)
784 #endif
786 static void do_memory_dump(Monitor *mon, int count, int format, int size,
787 uint32_t addrh, uint32_t addrl)
789 target_long addr = GET_TLONG(addrh, addrl);
790 memory_dump(mon, count, format, size, addr, 0);
793 #if TARGET_PHYS_ADDR_BITS > 32
794 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
795 #else
796 #define GET_TPHYSADDR(h, l) (l)
797 #endif
799 static void do_physical_memory_dump(Monitor *mon, int count, int format,
800 int size, uint32_t addrh, uint32_t addrl)
803 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
804 memory_dump(mon, count, format, size, addr, 1);
807 static void do_print(Monitor *mon, int count, int format, int size,
808 unsigned int valh, unsigned int vall)
810 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
811 #if TARGET_PHYS_ADDR_BITS == 32
812 switch(format) {
813 case 'o':
814 monitor_printf(mon, "%#o", val);
815 break;
816 case 'x':
817 monitor_printf(mon, "%#x", val);
818 break;
819 case 'u':
820 monitor_printf(mon, "%u", val);
821 break;
822 default:
823 case 'd':
824 monitor_printf(mon, "%d", val);
825 break;
826 case 'c':
827 monitor_printc(mon, val);
828 break;
830 #else
831 switch(format) {
832 case 'o':
833 monitor_printf(mon, "%#" PRIo64, val);
834 break;
835 case 'x':
836 monitor_printf(mon, "%#" PRIx64, val);
837 break;
838 case 'u':
839 monitor_printf(mon, "%" PRIu64, val);
840 break;
841 default:
842 case 'd':
843 monitor_printf(mon, "%" PRId64, val);
844 break;
845 case 'c':
846 monitor_printc(mon, val);
847 break;
849 #endif
850 monitor_printf(mon, "\n");
853 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
854 uint32_t size, const char *filename)
856 FILE *f;
857 target_long addr = GET_TLONG(valh, vall);
858 uint32_t l;
859 CPUState *env;
860 uint8_t buf[1024];
862 env = mon_get_cpu();
863 if (!env)
864 return;
866 f = fopen(filename, "wb");
867 if (!f) {
868 monitor_printf(mon, "could not open '%s'\n", filename);
869 return;
871 while (size != 0) {
872 l = sizeof(buf);
873 if (l > size)
874 l = size;
875 cpu_memory_rw_debug(env, addr, buf, l, 0);
876 fwrite(buf, 1, l, f);
877 addr += l;
878 size -= l;
880 fclose(f);
883 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
884 unsigned int vall, uint32_t size,
885 const char *filename)
887 FILE *f;
888 uint32_t l;
889 uint8_t buf[1024];
890 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
892 f = fopen(filename, "wb");
893 if (!f) {
894 monitor_printf(mon, "could not open '%s'\n", filename);
895 return;
897 while (size != 0) {
898 l = sizeof(buf);
899 if (l > size)
900 l = size;
901 cpu_physical_memory_rw(addr, buf, l, 0);
902 fwrite(buf, 1, l, f);
903 fflush(f);
904 addr += l;
905 size -= l;
907 fclose(f);
910 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
912 uint32_t addr;
913 uint8_t buf[1];
914 uint16_t sum;
916 sum = 0;
917 for(addr = start; addr < (start + size); addr++) {
918 cpu_physical_memory_rw(addr, buf, 1, 0);
919 /* BSD sum algorithm ('sum' Unix command) */
920 sum = (sum >> 1) | (sum << 15);
921 sum += buf[0];
923 monitor_printf(mon, "%05d\n", sum);
926 typedef struct {
927 int keycode;
928 const char *name;
929 } KeyDef;
931 static const KeyDef key_defs[] = {
932 { 0x2a, "shift" },
933 { 0x36, "shift_r" },
935 { 0x38, "alt" },
936 { 0xb8, "alt_r" },
937 { 0x64, "altgr" },
938 { 0xe4, "altgr_r" },
939 { 0x1d, "ctrl" },
940 { 0x9d, "ctrl_r" },
942 { 0xdd, "menu" },
944 { 0x01, "esc" },
946 { 0x02, "1" },
947 { 0x03, "2" },
948 { 0x04, "3" },
949 { 0x05, "4" },
950 { 0x06, "5" },
951 { 0x07, "6" },
952 { 0x08, "7" },
953 { 0x09, "8" },
954 { 0x0a, "9" },
955 { 0x0b, "0" },
956 { 0x0c, "minus" },
957 { 0x0d, "equal" },
958 { 0x0e, "backspace" },
960 { 0x0f, "tab" },
961 { 0x10, "q" },
962 { 0x11, "w" },
963 { 0x12, "e" },
964 { 0x13, "r" },
965 { 0x14, "t" },
966 { 0x15, "y" },
967 { 0x16, "u" },
968 { 0x17, "i" },
969 { 0x18, "o" },
970 { 0x19, "p" },
972 { 0x1c, "ret" },
974 { 0x1e, "a" },
975 { 0x1f, "s" },
976 { 0x20, "d" },
977 { 0x21, "f" },
978 { 0x22, "g" },
979 { 0x23, "h" },
980 { 0x24, "j" },
981 { 0x25, "k" },
982 { 0x26, "l" },
984 { 0x2c, "z" },
985 { 0x2d, "x" },
986 { 0x2e, "c" },
987 { 0x2f, "v" },
988 { 0x30, "b" },
989 { 0x31, "n" },
990 { 0x32, "m" },
991 { 0x33, "comma" },
992 { 0x34, "dot" },
993 { 0x35, "slash" },
995 { 0x37, "asterisk" },
997 { 0x39, "spc" },
998 { 0x3a, "caps_lock" },
999 { 0x3b, "f1" },
1000 { 0x3c, "f2" },
1001 { 0x3d, "f3" },
1002 { 0x3e, "f4" },
1003 { 0x3f, "f5" },
1004 { 0x40, "f6" },
1005 { 0x41, "f7" },
1006 { 0x42, "f8" },
1007 { 0x43, "f9" },
1008 { 0x44, "f10" },
1009 { 0x45, "num_lock" },
1010 { 0x46, "scroll_lock" },
1012 { 0xb5, "kp_divide" },
1013 { 0x37, "kp_multiply" },
1014 { 0x4a, "kp_subtract" },
1015 { 0x4e, "kp_add" },
1016 { 0x9c, "kp_enter" },
1017 { 0x53, "kp_decimal" },
1018 { 0x54, "sysrq" },
1020 { 0x52, "kp_0" },
1021 { 0x4f, "kp_1" },
1022 { 0x50, "kp_2" },
1023 { 0x51, "kp_3" },
1024 { 0x4b, "kp_4" },
1025 { 0x4c, "kp_5" },
1026 { 0x4d, "kp_6" },
1027 { 0x47, "kp_7" },
1028 { 0x48, "kp_8" },
1029 { 0x49, "kp_9" },
1031 { 0x56, "<" },
1033 { 0x57, "f11" },
1034 { 0x58, "f12" },
1036 { 0xb7, "print" },
1038 { 0xc7, "home" },
1039 { 0xc9, "pgup" },
1040 { 0xd1, "pgdn" },
1041 { 0xcf, "end" },
1043 { 0xcb, "left" },
1044 { 0xc8, "up" },
1045 { 0xd0, "down" },
1046 { 0xcd, "right" },
1048 { 0xd2, "insert" },
1049 { 0xd3, "delete" },
1050 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1051 { 0xf0, "stop" },
1052 { 0xf1, "again" },
1053 { 0xf2, "props" },
1054 { 0xf3, "undo" },
1055 { 0xf4, "front" },
1056 { 0xf5, "copy" },
1057 { 0xf6, "open" },
1058 { 0xf7, "paste" },
1059 { 0xf8, "find" },
1060 { 0xf9, "cut" },
1061 { 0xfa, "lf" },
1062 { 0xfb, "help" },
1063 { 0xfc, "meta_l" },
1064 { 0xfd, "meta_r" },
1065 { 0xfe, "compose" },
1066 #endif
1067 { 0, NULL },
1070 static int get_keycode(const char *key)
1072 const KeyDef *p;
1073 char *endp;
1074 int ret;
1076 for(p = key_defs; p->name != NULL; p++) {
1077 if (!strcmp(key, p->name))
1078 return p->keycode;
1080 if (strstart(key, "0x", NULL)) {
1081 ret = strtoul(key, &endp, 0);
1082 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1083 return ret;
1085 return -1;
1088 #define MAX_KEYCODES 16
1089 static uint8_t keycodes[MAX_KEYCODES];
1090 static int nb_pending_keycodes;
1091 static QEMUTimer *key_timer;
1093 static void release_keys(void *opaque)
1095 int keycode;
1097 while (nb_pending_keycodes > 0) {
1098 nb_pending_keycodes--;
1099 keycode = keycodes[nb_pending_keycodes];
1100 if (keycode & 0x80)
1101 kbd_put_keycode(0xe0);
1102 kbd_put_keycode(keycode | 0x80);
1106 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1107 int hold_time)
1109 char keyname_buf[16];
1110 char *separator;
1111 int keyname_len, keycode, i;
1113 if (nb_pending_keycodes > 0) {
1114 qemu_del_timer(key_timer);
1115 release_keys(NULL);
1117 if (!has_hold_time)
1118 hold_time = 100;
1119 i = 0;
1120 while (1) {
1121 separator = strchr(string, '-');
1122 keyname_len = separator ? separator - string : strlen(string);
1123 if (keyname_len > 0) {
1124 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1125 if (keyname_len > sizeof(keyname_buf) - 1) {
1126 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1127 return;
1129 if (i == MAX_KEYCODES) {
1130 monitor_printf(mon, "too many keys\n");
1131 return;
1133 keyname_buf[keyname_len] = 0;
1134 keycode = get_keycode(keyname_buf);
1135 if (keycode < 0) {
1136 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1137 return;
1139 keycodes[i++] = keycode;
1141 if (!separator)
1142 break;
1143 string = separator + 1;
1145 nb_pending_keycodes = i;
1146 /* key down events */
1147 for (i = 0; i < nb_pending_keycodes; i++) {
1148 keycode = keycodes[i];
1149 if (keycode & 0x80)
1150 kbd_put_keycode(0xe0);
1151 kbd_put_keycode(keycode & 0x7f);
1153 /* delayed key up events */
1154 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1155 muldiv64(ticks_per_sec, hold_time, 1000));
1158 static int mouse_button_state;
1160 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1161 const char *dz_str)
1163 int dx, dy, dz;
1164 dx = strtol(dx_str, NULL, 0);
1165 dy = strtol(dy_str, NULL, 0);
1166 dz = 0;
1167 if (dz_str)
1168 dz = strtol(dz_str, NULL, 0);
1169 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1172 static void do_mouse_button(Monitor *mon, int button_state)
1174 mouse_button_state = button_state;
1175 kbd_mouse_event(0, 0, 0, mouse_button_state);
1178 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1179 int addr, int has_index, int index)
1181 uint32_t val;
1182 int suffix;
1184 if (has_index) {
1185 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1186 addr++;
1188 addr &= 0xffff;
1190 switch(size) {
1191 default:
1192 case 1:
1193 val = cpu_inb(NULL, addr);
1194 suffix = 'b';
1195 break;
1196 case 2:
1197 val = cpu_inw(NULL, addr);
1198 suffix = 'w';
1199 break;
1200 case 4:
1201 val = cpu_inl(NULL, addr);
1202 suffix = 'l';
1203 break;
1205 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1206 suffix, addr, size * 2, val);
1209 /* boot_set handler */
1210 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1211 static void *boot_opaque;
1213 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1215 qemu_boot_set_handler = func;
1216 boot_opaque = opaque;
1219 static void do_boot_set(Monitor *mon, const char *bootdevice)
1221 int res;
1223 if (qemu_boot_set_handler) {
1224 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1225 if (res == 0)
1226 monitor_printf(mon, "boot device list now set to %s\n",
1227 bootdevice);
1228 else
1229 monitor_printf(mon, "setting boot device list failed with "
1230 "error %i\n", res);
1231 } else {
1232 monitor_printf(mon, "no function defined to set boot device list for "
1233 "this architecture\n");
1237 static void do_system_reset(Monitor *mon)
1239 qemu_system_reset_request();
1242 static void do_system_powerdown(Monitor *mon)
1244 qemu_system_powerdown_request();
1247 #if defined(TARGET_I386)
1248 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1250 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1251 addr,
1252 pte & mask,
1253 pte & PG_GLOBAL_MASK ? 'G' : '-',
1254 pte & PG_PSE_MASK ? 'P' : '-',
1255 pte & PG_DIRTY_MASK ? 'D' : '-',
1256 pte & PG_ACCESSED_MASK ? 'A' : '-',
1257 pte & PG_PCD_MASK ? 'C' : '-',
1258 pte & PG_PWT_MASK ? 'T' : '-',
1259 pte & PG_USER_MASK ? 'U' : '-',
1260 pte & PG_RW_MASK ? 'W' : '-');
1263 static void tlb_info(Monitor *mon)
1265 CPUState *env;
1266 int l1, l2;
1267 uint32_t pgd, pde, pte;
1269 env = mon_get_cpu();
1270 if (!env)
1271 return;
1273 if (!(env->cr[0] & CR0_PG_MASK)) {
1274 monitor_printf(mon, "PG disabled\n");
1275 return;
1277 pgd = env->cr[3] & ~0xfff;
1278 for(l1 = 0; l1 < 1024; l1++) {
1279 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1280 pde = le32_to_cpu(pde);
1281 if (pde & PG_PRESENT_MASK) {
1282 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1283 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1284 } else {
1285 for(l2 = 0; l2 < 1024; l2++) {
1286 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1287 (uint8_t *)&pte, 4);
1288 pte = le32_to_cpu(pte);
1289 if (pte & PG_PRESENT_MASK) {
1290 print_pte(mon, (l1 << 22) + (l2 << 12),
1291 pte & ~PG_PSE_MASK,
1292 ~0xfff);
1300 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1301 uint32_t end, int prot)
1303 int prot1;
1304 prot1 = *plast_prot;
1305 if (prot != prot1) {
1306 if (*pstart != -1) {
1307 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1308 *pstart, end, end - *pstart,
1309 prot1 & PG_USER_MASK ? 'u' : '-',
1310 'r',
1311 prot1 & PG_RW_MASK ? 'w' : '-');
1313 if (prot != 0)
1314 *pstart = end;
1315 else
1316 *pstart = -1;
1317 *plast_prot = prot;
1321 static void mem_info(Monitor *mon)
1323 CPUState *env;
1324 int l1, l2, prot, last_prot;
1325 uint32_t pgd, pde, pte, start, end;
1327 env = mon_get_cpu();
1328 if (!env)
1329 return;
1331 if (!(env->cr[0] & CR0_PG_MASK)) {
1332 monitor_printf(mon, "PG disabled\n");
1333 return;
1335 pgd = env->cr[3] & ~0xfff;
1336 last_prot = 0;
1337 start = -1;
1338 for(l1 = 0; l1 < 1024; l1++) {
1339 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1340 pde = le32_to_cpu(pde);
1341 end = l1 << 22;
1342 if (pde & PG_PRESENT_MASK) {
1343 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1344 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1345 mem_print(mon, &start, &last_prot, end, prot);
1346 } else {
1347 for(l2 = 0; l2 < 1024; l2++) {
1348 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1349 (uint8_t *)&pte, 4);
1350 pte = le32_to_cpu(pte);
1351 end = (l1 << 22) + (l2 << 12);
1352 if (pte & PG_PRESENT_MASK) {
1353 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1354 } else {
1355 prot = 0;
1357 mem_print(mon, &start, &last_prot, end, prot);
1360 } else {
1361 prot = 0;
1362 mem_print(mon, &start, &last_prot, end, prot);
1366 #endif
1368 #if defined(TARGET_SH4)
1370 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1372 monitor_printf(mon, " tlb%i:\t"
1373 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1374 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1375 "dirty=%hhu writethrough=%hhu\n",
1376 idx,
1377 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1378 tlb->v, tlb->sh, tlb->c, tlb->pr,
1379 tlb->d, tlb->wt);
1382 static void tlb_info(Monitor *mon)
1384 CPUState *env = mon_get_cpu();
1385 int i;
1387 monitor_printf (mon, "ITLB:\n");
1388 for (i = 0 ; i < ITLB_SIZE ; i++)
1389 print_tlb (mon, i, &env->itlb[i]);
1390 monitor_printf (mon, "UTLB:\n");
1391 for (i = 0 ; i < UTLB_SIZE ; i++)
1392 print_tlb (mon, i, &env->utlb[i]);
1395 #endif
1397 static void do_info_kqemu(Monitor *mon)
1399 #ifdef CONFIG_KQEMU
1400 CPUState *env;
1401 int val;
1402 val = 0;
1403 env = mon_get_cpu();
1404 if (!env) {
1405 monitor_printf(mon, "No cpu initialized yet");
1406 return;
1408 val = env->kqemu_enabled;
1409 monitor_printf(mon, "kqemu support: ");
1410 switch(val) {
1411 default:
1412 case 0:
1413 monitor_printf(mon, "disabled\n");
1414 break;
1415 case 1:
1416 monitor_printf(mon, "enabled for user code\n");
1417 break;
1418 case 2:
1419 monitor_printf(mon, "enabled for user and kernel code\n");
1420 break;
1422 #else
1423 monitor_printf(mon, "kqemu support: not compiled\n");
1424 #endif
1427 static void do_info_kvm(Monitor *mon)
1429 #if defined(USE_KVM) || defined(CONFIG_KVM)
1430 monitor_printf(mon, "kvm support: ");
1431 if (kvm_enabled())
1432 monitor_printf(mon, "enabled\n");
1433 else
1434 monitor_printf(mon, "disabled\n");
1435 #else
1436 monitor_printf(mon, "kvm support: not compiled\n");
1437 #endif
1440 static void do_info_numa(Monitor *mon)
1442 int i;
1443 CPUState *env;
1445 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1446 for (i = 0; i < nb_numa_nodes; i++) {
1447 monitor_printf(mon, "node %d cpus:", i);
1448 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1449 if (env->numa_node == i) {
1450 monitor_printf(mon, " %d", env->cpu_index);
1453 monitor_printf(mon, "\n");
1454 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1455 node_mem[i] >> 20);
1459 #ifdef CONFIG_PROFILER
1461 int64_t kqemu_time;
1462 int64_t qemu_time;
1463 int64_t kqemu_exec_count;
1464 int64_t dev_time;
1465 int64_t kqemu_ret_int_count;
1466 int64_t kqemu_ret_excp_count;
1467 int64_t kqemu_ret_intr_count;
1469 static void do_info_profile(Monitor *mon)
1471 int64_t total;
1472 total = qemu_time;
1473 if (total == 0)
1474 total = 1;
1475 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1476 dev_time, dev_time / (double)ticks_per_sec);
1477 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1478 qemu_time, qemu_time / (double)ticks_per_sec);
1479 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1480 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1481 PRId64 "\n",
1482 kqemu_time, kqemu_time / (double)ticks_per_sec,
1483 kqemu_time / (double)total * 100.0,
1484 kqemu_exec_count,
1485 kqemu_ret_int_count,
1486 kqemu_ret_excp_count,
1487 kqemu_ret_intr_count);
1488 qemu_time = 0;
1489 kqemu_time = 0;
1490 kqemu_exec_count = 0;
1491 dev_time = 0;
1492 kqemu_ret_int_count = 0;
1493 kqemu_ret_excp_count = 0;
1494 kqemu_ret_intr_count = 0;
1495 #ifdef CONFIG_KQEMU
1496 kqemu_record_dump();
1497 #endif
1499 #else
1500 static void do_info_profile(Monitor *mon)
1502 monitor_printf(mon, "Internal profiler not compiled\n");
1504 #endif
1506 /* Capture support */
1507 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1509 static void do_info_capture(Monitor *mon)
1511 int i;
1512 CaptureState *s;
1514 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1515 monitor_printf(mon, "[%d]: ", i);
1516 s->ops.info (s->opaque);
1520 #ifdef HAS_AUDIO
1521 static void do_stop_capture(Monitor *mon, int n)
1523 int i;
1524 CaptureState *s;
1526 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1527 if (i == n) {
1528 s->ops.destroy (s->opaque);
1529 LIST_REMOVE (s, entries);
1530 qemu_free (s);
1531 return;
1536 static void do_wav_capture(Monitor *mon, const char *path,
1537 int has_freq, int freq,
1538 int has_bits, int bits,
1539 int has_channels, int nchannels)
1541 CaptureState *s;
1543 s = qemu_mallocz (sizeof (*s));
1545 freq = has_freq ? freq : 44100;
1546 bits = has_bits ? bits : 16;
1547 nchannels = has_channels ? nchannels : 2;
1549 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1550 monitor_printf(mon, "Faied to add wave capture\n");
1551 qemu_free (s);
1553 LIST_INSERT_HEAD (&capture_head, s, entries);
1555 #endif
1557 #if defined(TARGET_I386)
1558 static void do_inject_nmi(Monitor *mon, int cpu_index)
1560 CPUState *env;
1562 for (env = first_cpu; env != NULL; env = env->next_cpu)
1563 if (env->cpu_index == cpu_index) {
1564 if (kvm_enabled())
1565 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1566 else
1567 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1568 break;
1571 #endif
1573 static void do_info_status(Monitor *mon)
1575 if (vm_running) {
1576 if (singlestep) {
1577 monitor_printf(mon, "VM status: running (single step mode)\n");
1578 } else {
1579 monitor_printf(mon, "VM status: running\n");
1581 } else
1582 monitor_printf(mon, "VM status: paused\n");
1586 static void do_balloon(Monitor *mon, int value)
1588 ram_addr_t target = value;
1589 qemu_balloon(target << 20);
1592 static void do_info_balloon(Monitor *mon)
1594 ram_addr_t actual;
1596 actual = qemu_balloon_status();
1597 if (kvm_enabled() && !kvm_has_sync_mmu())
1598 monitor_printf(mon, "Using KVM without synchronous MMU, "
1599 "ballooning disabled\n");
1600 else if (actual == 0)
1601 monitor_printf(mon, "Ballooning not activated in VM\n");
1602 else
1603 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1606 static qemu_acl *find_acl(Monitor *mon, const char *name)
1608 qemu_acl *acl = qemu_acl_find(name);
1610 if (!acl) {
1611 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1613 return acl;
1616 static void do_acl_show(Monitor *mon, const char *aclname)
1618 qemu_acl *acl = find_acl(mon, aclname);
1619 qemu_acl_entry *entry;
1620 int i = 0;
1622 if (acl) {
1623 monitor_printf(mon, "policy: %s\n",
1624 acl->defaultDeny ? "deny" : "allow");
1625 TAILQ_FOREACH(entry, &acl->entries, next) {
1626 i++;
1627 monitor_printf(mon, "%d: %s %s\n", i,
1628 entry->deny ? "deny" : "allow", entry->match);
1633 static void do_acl_reset(Monitor *mon, const char *aclname)
1635 qemu_acl *acl = find_acl(mon, aclname);
1637 if (acl) {
1638 qemu_acl_reset(acl);
1639 monitor_printf(mon, "acl: removed all rules\n");
1643 static void do_acl_policy(Monitor *mon, const char *aclname,
1644 const char *policy)
1646 qemu_acl *acl = find_acl(mon, aclname);
1648 if (acl) {
1649 if (strcmp(policy, "allow") == 0) {
1650 acl->defaultDeny = 0;
1651 monitor_printf(mon, "acl: policy set to 'allow'\n");
1652 } else if (strcmp(policy, "deny") == 0) {
1653 acl->defaultDeny = 1;
1654 monitor_printf(mon, "acl: policy set to 'deny'\n");
1655 } else {
1656 monitor_printf(mon, "acl: unknown policy '%s', "
1657 "expected 'deny' or 'allow'\n", policy);
1662 static void do_acl_add(Monitor *mon, const char *aclname,
1663 const char *match, const char *policy,
1664 int has_index, int index)
1666 qemu_acl *acl = find_acl(mon, aclname);
1667 int deny, ret;
1669 if (acl) {
1670 if (strcmp(policy, "allow") == 0) {
1671 deny = 0;
1672 } else if (strcmp(policy, "deny") == 0) {
1673 deny = 1;
1674 } else {
1675 monitor_printf(mon, "acl: unknown policy '%s', "
1676 "expected 'deny' or 'allow'\n", policy);
1677 return;
1679 if (has_index)
1680 ret = qemu_acl_insert(acl, deny, match, index);
1681 else
1682 ret = qemu_acl_append(acl, deny, match);
1683 if (ret < 0)
1684 monitor_printf(mon, "acl: unable to add acl entry\n");
1685 else
1686 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1690 static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1692 qemu_acl *acl = find_acl(mon, aclname);
1693 int ret;
1695 if (acl) {
1696 ret = qemu_acl_remove(acl, match);
1697 if (ret < 0)
1698 monitor_printf(mon, "acl: no matching acl entry\n");
1699 else
1700 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1704 static const mon_cmd_t mon_cmds[] = {
1705 #include "qemu-monitor.h"
1706 { NULL, NULL, },
1709 /* Please update qemu-monitor.hx when adding or changing commands */
1710 static const mon_cmd_t info_cmds[] = {
1711 { "version", "", do_info_version,
1712 "", "show the version of QEMU" },
1713 { "network", "", do_info_network,
1714 "", "show the network state" },
1715 { "chardev", "", qemu_chr_info,
1716 "", "show the character devices" },
1717 { "block", "", bdrv_info,
1718 "", "show the block devices" },
1719 { "blockstats", "", bdrv_info_stats,
1720 "", "show block device statistics" },
1721 { "registers", "", do_info_registers,
1722 "", "show the cpu registers" },
1723 { "cpus", "", do_info_cpus,
1724 "", "show infos for each CPU" },
1725 { "history", "", do_info_history,
1726 "", "show the command line history", },
1727 { "irq", "", irq_info,
1728 "", "show the interrupts statistics (if available)", },
1729 { "pic", "", pic_info,
1730 "", "show i8259 (PIC) state", },
1731 { "pci", "", pci_info,
1732 "", "show PCI info", },
1733 #if defined(TARGET_I386) || defined(TARGET_SH4)
1734 { "tlb", "", tlb_info,
1735 "", "show virtual to physical memory mappings", },
1736 #endif
1737 #if defined(TARGET_I386)
1738 { "mem", "", mem_info,
1739 "", "show the active virtual memory mappings", },
1740 { "hpet", "", do_info_hpet,
1741 "", "show state of HPET", },
1742 #endif
1743 { "jit", "", do_info_jit,
1744 "", "show dynamic compiler info", },
1745 { "kqemu", "", do_info_kqemu,
1746 "", "show KQEMU information", },
1747 { "kvm", "", do_info_kvm,
1748 "", "show KVM information", },
1749 { "numa", "", do_info_numa,
1750 "", "show NUMA information", },
1751 { "usb", "", usb_info,
1752 "", "show guest USB devices", },
1753 { "usbhost", "", usb_host_info,
1754 "", "show host USB devices", },
1755 { "profile", "", do_info_profile,
1756 "", "show profiling information", },
1757 { "capture", "", do_info_capture,
1758 "", "show capture information" },
1759 { "snapshots", "", do_info_snapshots,
1760 "", "show the currently saved VM snapshots" },
1761 { "status", "", do_info_status,
1762 "", "show the current VM status (running|paused)" },
1763 { "pcmcia", "", pcmcia_info,
1764 "", "show guest PCMCIA status" },
1765 { "mice", "", do_info_mice,
1766 "", "show which guest mouse is receiving events" },
1767 { "vnc", "", do_info_vnc,
1768 "", "show the vnc server status"},
1769 { "name", "", do_info_name,
1770 "", "show the current VM name" },
1771 { "uuid", "", do_info_uuid,
1772 "", "show the current VM UUID" },
1773 #if defined(TARGET_PPC)
1774 { "cpustats", "", do_info_cpu_stats,
1775 "", "show CPU statistics", },
1776 #endif
1777 #if defined(CONFIG_SLIRP)
1778 { "usernet", "", do_info_usernet,
1779 "", "show user network stack connection states", },
1780 #endif
1781 { "migrate", "", do_info_migrate, "", "show migration status" },
1782 { "balloon", "", do_info_balloon,
1783 "", "show balloon information" },
1784 { "qtree", "", do_info_qtree,
1785 "", "show device tree" },
1786 { NULL, NULL, },
1789 /*******************************************************************/
1791 static const char *pch;
1792 static jmp_buf expr_env;
1794 #define MD_TLONG 0
1795 #define MD_I32 1
1797 typedef struct MonitorDef {
1798 const char *name;
1799 int offset;
1800 target_long (*get_value)(const struct MonitorDef *md, int val);
1801 int type;
1802 } MonitorDef;
1804 #if defined(TARGET_I386)
1805 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1807 CPUState *env = mon_get_cpu();
1808 if (!env)
1809 return 0;
1810 return env->eip + env->segs[R_CS].base;
1812 #endif
1814 #if defined(TARGET_PPC)
1815 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1817 CPUState *env = mon_get_cpu();
1818 unsigned int u;
1819 int i;
1821 if (!env)
1822 return 0;
1824 u = 0;
1825 for (i = 0; i < 8; i++)
1826 u |= env->crf[i] << (32 - (4 * i));
1828 return u;
1831 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1833 CPUState *env = mon_get_cpu();
1834 if (!env)
1835 return 0;
1836 return env->msr;
1839 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1841 CPUState *env = mon_get_cpu();
1842 if (!env)
1843 return 0;
1844 return env->xer;
1847 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1849 CPUState *env = mon_get_cpu();
1850 if (!env)
1851 return 0;
1852 return cpu_ppc_load_decr(env);
1855 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1857 CPUState *env = mon_get_cpu();
1858 if (!env)
1859 return 0;
1860 return cpu_ppc_load_tbu(env);
1863 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1865 CPUState *env = mon_get_cpu();
1866 if (!env)
1867 return 0;
1868 return cpu_ppc_load_tbl(env);
1870 #endif
1872 #if defined(TARGET_SPARC)
1873 #ifndef TARGET_SPARC64
1874 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1876 CPUState *env = mon_get_cpu();
1877 if (!env)
1878 return 0;
1879 return GET_PSR(env);
1881 #endif
1883 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1885 CPUState *env = mon_get_cpu();
1886 if (!env)
1887 return 0;
1888 return env->regwptr[val];
1890 #endif
1892 static const MonitorDef monitor_defs[] = {
1893 #ifdef TARGET_I386
1895 #define SEG(name, seg) \
1896 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1897 { name ".base", offsetof(CPUState, segs[seg].base) },\
1898 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1900 { "eax", offsetof(CPUState, regs[0]) },
1901 { "ecx", offsetof(CPUState, regs[1]) },
1902 { "edx", offsetof(CPUState, regs[2]) },
1903 { "ebx", offsetof(CPUState, regs[3]) },
1904 { "esp|sp", offsetof(CPUState, regs[4]) },
1905 { "ebp|fp", offsetof(CPUState, regs[5]) },
1906 { "esi", offsetof(CPUState, regs[6]) },
1907 { "edi", offsetof(CPUState, regs[7]) },
1908 #ifdef TARGET_X86_64
1909 { "r8", offsetof(CPUState, regs[8]) },
1910 { "r9", offsetof(CPUState, regs[9]) },
1911 { "r10", offsetof(CPUState, regs[10]) },
1912 { "r11", offsetof(CPUState, regs[11]) },
1913 { "r12", offsetof(CPUState, regs[12]) },
1914 { "r13", offsetof(CPUState, regs[13]) },
1915 { "r14", offsetof(CPUState, regs[14]) },
1916 { "r15", offsetof(CPUState, regs[15]) },
1917 #endif
1918 { "eflags", offsetof(CPUState, eflags) },
1919 { "eip", offsetof(CPUState, eip) },
1920 SEG("cs", R_CS)
1921 SEG("ds", R_DS)
1922 SEG("es", R_ES)
1923 SEG("ss", R_SS)
1924 SEG("fs", R_FS)
1925 SEG("gs", R_GS)
1926 { "pc", 0, monitor_get_pc, },
1927 #elif defined(TARGET_PPC)
1928 /* General purpose registers */
1929 { "r0", offsetof(CPUState, gpr[0]) },
1930 { "r1", offsetof(CPUState, gpr[1]) },
1931 { "r2", offsetof(CPUState, gpr[2]) },
1932 { "r3", offsetof(CPUState, gpr[3]) },
1933 { "r4", offsetof(CPUState, gpr[4]) },
1934 { "r5", offsetof(CPUState, gpr[5]) },
1935 { "r6", offsetof(CPUState, gpr[6]) },
1936 { "r7", offsetof(CPUState, gpr[7]) },
1937 { "r8", offsetof(CPUState, gpr[8]) },
1938 { "r9", offsetof(CPUState, gpr[9]) },
1939 { "r10", offsetof(CPUState, gpr[10]) },
1940 { "r11", offsetof(CPUState, gpr[11]) },
1941 { "r12", offsetof(CPUState, gpr[12]) },
1942 { "r13", offsetof(CPUState, gpr[13]) },
1943 { "r14", offsetof(CPUState, gpr[14]) },
1944 { "r15", offsetof(CPUState, gpr[15]) },
1945 { "r16", offsetof(CPUState, gpr[16]) },
1946 { "r17", offsetof(CPUState, gpr[17]) },
1947 { "r18", offsetof(CPUState, gpr[18]) },
1948 { "r19", offsetof(CPUState, gpr[19]) },
1949 { "r20", offsetof(CPUState, gpr[20]) },
1950 { "r21", offsetof(CPUState, gpr[21]) },
1951 { "r22", offsetof(CPUState, gpr[22]) },
1952 { "r23", offsetof(CPUState, gpr[23]) },
1953 { "r24", offsetof(CPUState, gpr[24]) },
1954 { "r25", offsetof(CPUState, gpr[25]) },
1955 { "r26", offsetof(CPUState, gpr[26]) },
1956 { "r27", offsetof(CPUState, gpr[27]) },
1957 { "r28", offsetof(CPUState, gpr[28]) },
1958 { "r29", offsetof(CPUState, gpr[29]) },
1959 { "r30", offsetof(CPUState, gpr[30]) },
1960 { "r31", offsetof(CPUState, gpr[31]) },
1961 /* Floating point registers */
1962 { "f0", offsetof(CPUState, fpr[0]) },
1963 { "f1", offsetof(CPUState, fpr[1]) },
1964 { "f2", offsetof(CPUState, fpr[2]) },
1965 { "f3", offsetof(CPUState, fpr[3]) },
1966 { "f4", offsetof(CPUState, fpr[4]) },
1967 { "f5", offsetof(CPUState, fpr[5]) },
1968 { "f6", offsetof(CPUState, fpr[6]) },
1969 { "f7", offsetof(CPUState, fpr[7]) },
1970 { "f8", offsetof(CPUState, fpr[8]) },
1971 { "f9", offsetof(CPUState, fpr[9]) },
1972 { "f10", offsetof(CPUState, fpr[10]) },
1973 { "f11", offsetof(CPUState, fpr[11]) },
1974 { "f12", offsetof(CPUState, fpr[12]) },
1975 { "f13", offsetof(CPUState, fpr[13]) },
1976 { "f14", offsetof(CPUState, fpr[14]) },
1977 { "f15", offsetof(CPUState, fpr[15]) },
1978 { "f16", offsetof(CPUState, fpr[16]) },
1979 { "f17", offsetof(CPUState, fpr[17]) },
1980 { "f18", offsetof(CPUState, fpr[18]) },
1981 { "f19", offsetof(CPUState, fpr[19]) },
1982 { "f20", offsetof(CPUState, fpr[20]) },
1983 { "f21", offsetof(CPUState, fpr[21]) },
1984 { "f22", offsetof(CPUState, fpr[22]) },
1985 { "f23", offsetof(CPUState, fpr[23]) },
1986 { "f24", offsetof(CPUState, fpr[24]) },
1987 { "f25", offsetof(CPUState, fpr[25]) },
1988 { "f26", offsetof(CPUState, fpr[26]) },
1989 { "f27", offsetof(CPUState, fpr[27]) },
1990 { "f28", offsetof(CPUState, fpr[28]) },
1991 { "f29", offsetof(CPUState, fpr[29]) },
1992 { "f30", offsetof(CPUState, fpr[30]) },
1993 { "f31", offsetof(CPUState, fpr[31]) },
1994 { "fpscr", offsetof(CPUState, fpscr) },
1995 /* Next instruction pointer */
1996 { "nip|pc", offsetof(CPUState, nip) },
1997 { "lr", offsetof(CPUState, lr) },
1998 { "ctr", offsetof(CPUState, ctr) },
1999 { "decr", 0, &monitor_get_decr, },
2000 { "ccr", 0, &monitor_get_ccr, },
2001 /* Machine state register */
2002 { "msr", 0, &monitor_get_msr, },
2003 { "xer", 0, &monitor_get_xer, },
2004 { "tbu", 0, &monitor_get_tbu, },
2005 { "tbl", 0, &monitor_get_tbl, },
2006 #if defined(TARGET_PPC64)
2007 /* Address space register */
2008 { "asr", offsetof(CPUState, asr) },
2009 #endif
2010 /* Segment registers */
2011 { "sdr1", offsetof(CPUState, sdr1) },
2012 { "sr0", offsetof(CPUState, sr[0]) },
2013 { "sr1", offsetof(CPUState, sr[1]) },
2014 { "sr2", offsetof(CPUState, sr[2]) },
2015 { "sr3", offsetof(CPUState, sr[3]) },
2016 { "sr4", offsetof(CPUState, sr[4]) },
2017 { "sr5", offsetof(CPUState, sr[5]) },
2018 { "sr6", offsetof(CPUState, sr[6]) },
2019 { "sr7", offsetof(CPUState, sr[7]) },
2020 { "sr8", offsetof(CPUState, sr[8]) },
2021 { "sr9", offsetof(CPUState, sr[9]) },
2022 { "sr10", offsetof(CPUState, sr[10]) },
2023 { "sr11", offsetof(CPUState, sr[11]) },
2024 { "sr12", offsetof(CPUState, sr[12]) },
2025 { "sr13", offsetof(CPUState, sr[13]) },
2026 { "sr14", offsetof(CPUState, sr[14]) },
2027 { "sr15", offsetof(CPUState, sr[15]) },
2028 /* Too lazy to put BATs and SPRs ... */
2029 #elif defined(TARGET_SPARC)
2030 { "g0", offsetof(CPUState, gregs[0]) },
2031 { "g1", offsetof(CPUState, gregs[1]) },
2032 { "g2", offsetof(CPUState, gregs[2]) },
2033 { "g3", offsetof(CPUState, gregs[3]) },
2034 { "g4", offsetof(CPUState, gregs[4]) },
2035 { "g5", offsetof(CPUState, gregs[5]) },
2036 { "g6", offsetof(CPUState, gregs[6]) },
2037 { "g7", offsetof(CPUState, gregs[7]) },
2038 { "o0", 0, monitor_get_reg },
2039 { "o1", 1, monitor_get_reg },
2040 { "o2", 2, monitor_get_reg },
2041 { "o3", 3, monitor_get_reg },
2042 { "o4", 4, monitor_get_reg },
2043 { "o5", 5, monitor_get_reg },
2044 { "o6", 6, monitor_get_reg },
2045 { "o7", 7, monitor_get_reg },
2046 { "l0", 8, monitor_get_reg },
2047 { "l1", 9, monitor_get_reg },
2048 { "l2", 10, monitor_get_reg },
2049 { "l3", 11, monitor_get_reg },
2050 { "l4", 12, monitor_get_reg },
2051 { "l5", 13, monitor_get_reg },
2052 { "l6", 14, monitor_get_reg },
2053 { "l7", 15, monitor_get_reg },
2054 { "i0", 16, monitor_get_reg },
2055 { "i1", 17, monitor_get_reg },
2056 { "i2", 18, monitor_get_reg },
2057 { "i3", 19, monitor_get_reg },
2058 { "i4", 20, monitor_get_reg },
2059 { "i5", 21, monitor_get_reg },
2060 { "i6", 22, monitor_get_reg },
2061 { "i7", 23, monitor_get_reg },
2062 { "pc", offsetof(CPUState, pc) },
2063 { "npc", offsetof(CPUState, npc) },
2064 { "y", offsetof(CPUState, y) },
2065 #ifndef TARGET_SPARC64
2066 { "psr", 0, &monitor_get_psr, },
2067 { "wim", offsetof(CPUState, wim) },
2068 #endif
2069 { "tbr", offsetof(CPUState, tbr) },
2070 { "fsr", offsetof(CPUState, fsr) },
2071 { "f0", offsetof(CPUState, fpr[0]) },
2072 { "f1", offsetof(CPUState, fpr[1]) },
2073 { "f2", offsetof(CPUState, fpr[2]) },
2074 { "f3", offsetof(CPUState, fpr[3]) },
2075 { "f4", offsetof(CPUState, fpr[4]) },
2076 { "f5", offsetof(CPUState, fpr[5]) },
2077 { "f6", offsetof(CPUState, fpr[6]) },
2078 { "f7", offsetof(CPUState, fpr[7]) },
2079 { "f8", offsetof(CPUState, fpr[8]) },
2080 { "f9", offsetof(CPUState, fpr[9]) },
2081 { "f10", offsetof(CPUState, fpr[10]) },
2082 { "f11", offsetof(CPUState, fpr[11]) },
2083 { "f12", offsetof(CPUState, fpr[12]) },
2084 { "f13", offsetof(CPUState, fpr[13]) },
2085 { "f14", offsetof(CPUState, fpr[14]) },
2086 { "f15", offsetof(CPUState, fpr[15]) },
2087 { "f16", offsetof(CPUState, fpr[16]) },
2088 { "f17", offsetof(CPUState, fpr[17]) },
2089 { "f18", offsetof(CPUState, fpr[18]) },
2090 { "f19", offsetof(CPUState, fpr[19]) },
2091 { "f20", offsetof(CPUState, fpr[20]) },
2092 { "f21", offsetof(CPUState, fpr[21]) },
2093 { "f22", offsetof(CPUState, fpr[22]) },
2094 { "f23", offsetof(CPUState, fpr[23]) },
2095 { "f24", offsetof(CPUState, fpr[24]) },
2096 { "f25", offsetof(CPUState, fpr[25]) },
2097 { "f26", offsetof(CPUState, fpr[26]) },
2098 { "f27", offsetof(CPUState, fpr[27]) },
2099 { "f28", offsetof(CPUState, fpr[28]) },
2100 { "f29", offsetof(CPUState, fpr[29]) },
2101 { "f30", offsetof(CPUState, fpr[30]) },
2102 { "f31", offsetof(CPUState, fpr[31]) },
2103 #ifdef TARGET_SPARC64
2104 { "f32", offsetof(CPUState, fpr[32]) },
2105 { "f34", offsetof(CPUState, fpr[34]) },
2106 { "f36", offsetof(CPUState, fpr[36]) },
2107 { "f38", offsetof(CPUState, fpr[38]) },
2108 { "f40", offsetof(CPUState, fpr[40]) },
2109 { "f42", offsetof(CPUState, fpr[42]) },
2110 { "f44", offsetof(CPUState, fpr[44]) },
2111 { "f46", offsetof(CPUState, fpr[46]) },
2112 { "f48", offsetof(CPUState, fpr[48]) },
2113 { "f50", offsetof(CPUState, fpr[50]) },
2114 { "f52", offsetof(CPUState, fpr[52]) },
2115 { "f54", offsetof(CPUState, fpr[54]) },
2116 { "f56", offsetof(CPUState, fpr[56]) },
2117 { "f58", offsetof(CPUState, fpr[58]) },
2118 { "f60", offsetof(CPUState, fpr[60]) },
2119 { "f62", offsetof(CPUState, fpr[62]) },
2120 { "asi", offsetof(CPUState, asi) },
2121 { "pstate", offsetof(CPUState, pstate) },
2122 { "cansave", offsetof(CPUState, cansave) },
2123 { "canrestore", offsetof(CPUState, canrestore) },
2124 { "otherwin", offsetof(CPUState, otherwin) },
2125 { "wstate", offsetof(CPUState, wstate) },
2126 { "cleanwin", offsetof(CPUState, cleanwin) },
2127 { "fprs", offsetof(CPUState, fprs) },
2128 #endif
2129 #endif
2130 { NULL },
2133 static void expr_error(Monitor *mon, const char *msg)
2135 monitor_printf(mon, "%s\n", msg);
2136 longjmp(expr_env, 1);
2139 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2140 static int get_monitor_def(target_long *pval, const char *name)
2142 const MonitorDef *md;
2143 void *ptr;
2145 for(md = monitor_defs; md->name != NULL; md++) {
2146 if (compare_cmd(name, md->name)) {
2147 if (md->get_value) {
2148 *pval = md->get_value(md, md->offset);
2149 } else {
2150 CPUState *env = mon_get_cpu();
2151 if (!env)
2152 return -2;
2153 ptr = (uint8_t *)env + md->offset;
2154 switch(md->type) {
2155 case MD_I32:
2156 *pval = *(int32_t *)ptr;
2157 break;
2158 case MD_TLONG:
2159 *pval = *(target_long *)ptr;
2160 break;
2161 default:
2162 *pval = 0;
2163 break;
2166 return 0;
2169 return -1;
2172 static void next(void)
2174 if (pch != '\0') {
2175 pch++;
2176 while (qemu_isspace(*pch))
2177 pch++;
2181 static int64_t expr_sum(Monitor *mon);
2183 static int64_t expr_unary(Monitor *mon)
2185 int64_t n;
2186 char *p;
2187 int ret;
2189 switch(*pch) {
2190 case '+':
2191 next();
2192 n = expr_unary(mon);
2193 break;
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_sum(mon);
2205 if (*pch != ')') {
2206 expr_error(mon, "')' expected");
2208 next();
2209 break;
2210 case '\'':
2211 pch++;
2212 if (*pch == '\0')
2213 expr_error(mon, "character constant expected");
2214 n = *pch;
2215 pch++;
2216 if (*pch != '\'')
2217 expr_error(mon, "missing terminating \' character");
2218 next();
2219 break;
2220 case '$':
2222 char buf[128], *q;
2223 target_long reg=0;
2225 pch++;
2226 q = buf;
2227 while ((*pch >= 'a' && *pch <= 'z') ||
2228 (*pch >= 'A' && *pch <= 'Z') ||
2229 (*pch >= '0' && *pch <= '9') ||
2230 *pch == '_' || *pch == '.') {
2231 if ((q - buf) < sizeof(buf) - 1)
2232 *q++ = *pch;
2233 pch++;
2235 while (qemu_isspace(*pch))
2236 pch++;
2237 *q = 0;
2238 ret = get_monitor_def(&reg, buf);
2239 if (ret == -1)
2240 expr_error(mon, "unknown register");
2241 else if (ret == -2)
2242 expr_error(mon, "no cpu defined");
2243 n = reg;
2245 break;
2246 case '\0':
2247 expr_error(mon, "unexpected end of expression");
2248 n = 0;
2249 break;
2250 default:
2251 #if TARGET_PHYS_ADDR_BITS > 32
2252 n = strtoull(pch, &p, 0);
2253 #else
2254 n = strtoul(pch, &p, 0);
2255 #endif
2256 if (pch == p) {
2257 expr_error(mon, "invalid char in expression");
2259 pch = p;
2260 while (qemu_isspace(*pch))
2261 pch++;
2262 break;
2264 return n;
2268 static int64_t expr_prod(Monitor *mon)
2270 int64_t val, val2;
2271 int op;
2273 val = expr_unary(mon);
2274 for(;;) {
2275 op = *pch;
2276 if (op != '*' && op != '/' && op != '%')
2277 break;
2278 next();
2279 val2 = expr_unary(mon);
2280 switch(op) {
2281 default:
2282 case '*':
2283 val *= val2;
2284 break;
2285 case '/':
2286 case '%':
2287 if (val2 == 0)
2288 expr_error(mon, "division by zero");
2289 if (op == '/')
2290 val /= val2;
2291 else
2292 val %= val2;
2293 break;
2296 return val;
2299 static int64_t expr_logic(Monitor *mon)
2301 int64_t val, val2;
2302 int op;
2304 val = expr_prod(mon);
2305 for(;;) {
2306 op = *pch;
2307 if (op != '&' && op != '|' && op != '^')
2308 break;
2309 next();
2310 val2 = expr_prod(mon);
2311 switch(op) {
2312 default:
2313 case '&':
2314 val &= val2;
2315 break;
2316 case '|':
2317 val |= val2;
2318 break;
2319 case '^':
2320 val ^= val2;
2321 break;
2324 return val;
2327 static int64_t expr_sum(Monitor *mon)
2329 int64_t val, val2;
2330 int op;
2332 val = expr_logic(mon);
2333 for(;;) {
2334 op = *pch;
2335 if (op != '+' && op != '-')
2336 break;
2337 next();
2338 val2 = expr_logic(mon);
2339 if (op == '+')
2340 val += val2;
2341 else
2342 val -= val2;
2344 return val;
2347 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2349 pch = *pp;
2350 if (setjmp(expr_env)) {
2351 *pp = pch;
2352 return -1;
2354 while (qemu_isspace(*pch))
2355 pch++;
2356 *pval = expr_sum(mon);
2357 *pp = pch;
2358 return 0;
2361 static int get_str(char *buf, int buf_size, const char **pp)
2363 const char *p;
2364 char *q;
2365 int c;
2367 q = buf;
2368 p = *pp;
2369 while (qemu_isspace(*p))
2370 p++;
2371 if (*p == '\0') {
2372 fail:
2373 *q = '\0';
2374 *pp = p;
2375 return -1;
2377 if (*p == '\"') {
2378 p++;
2379 while (*p != '\0' && *p != '\"') {
2380 if (*p == '\\') {
2381 p++;
2382 c = *p++;
2383 switch(c) {
2384 case 'n':
2385 c = '\n';
2386 break;
2387 case 'r':
2388 c = '\r';
2389 break;
2390 case '\\':
2391 case '\'':
2392 case '\"':
2393 break;
2394 default:
2395 qemu_printf("unsupported escape code: '\\%c'\n", c);
2396 goto fail;
2398 if ((q - buf) < buf_size - 1) {
2399 *q++ = c;
2401 } else {
2402 if ((q - buf) < buf_size - 1) {
2403 *q++ = *p;
2405 p++;
2408 if (*p != '\"') {
2409 qemu_printf("unterminated string\n");
2410 goto fail;
2412 p++;
2413 } else {
2414 while (*p != '\0' && !qemu_isspace(*p)) {
2415 if ((q - buf) < buf_size - 1) {
2416 *q++ = *p;
2418 p++;
2421 *q = '\0';
2422 *pp = p;
2423 return 0;
2427 * Store the command-name in cmdname, and return a pointer to
2428 * the remaining of the command string.
2430 static const char *get_command_name(const char *cmdline,
2431 char *cmdname, size_t nlen)
2433 size_t len;
2434 const char *p, *pstart;
2436 p = cmdline;
2437 while (qemu_isspace(*p))
2438 p++;
2439 if (*p == '\0')
2440 return NULL;
2441 pstart = p;
2442 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2443 p++;
2444 len = p - pstart;
2445 if (len > nlen - 1)
2446 len = nlen - 1;
2447 memcpy(cmdname, pstart, len);
2448 cmdname[len] = '\0';
2449 return p;
2452 static int default_fmt_format = 'x';
2453 static int default_fmt_size = 4;
2455 #define MAX_ARGS 16
2457 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2459 const char *p, *typestr;
2460 int c, nb_args, i, has_arg;
2461 const mon_cmd_t *cmd;
2462 char cmdname[256];
2463 char buf[1024];
2464 void *str_allocated[MAX_ARGS];
2465 void *args[MAX_ARGS];
2466 void (*handler_0)(Monitor *mon);
2467 void (*handler_1)(Monitor *mon, void *arg0);
2468 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2469 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2470 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2471 void *arg3);
2472 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2473 void *arg3, void *arg4);
2474 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2475 void *arg3, void *arg4, void *arg5);
2476 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2477 void *arg3, void *arg4, void *arg5, void *arg6);
2479 #ifdef DEBUG
2480 monitor_printf(mon, "command='%s'\n", cmdline);
2481 #endif
2483 /* extract the command name */
2484 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2485 if (!p)
2486 return;
2488 /* find the command */
2489 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2490 if (compare_cmd(cmdname, cmd->name))
2491 break;
2494 if (cmd->name == NULL) {
2495 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2496 return;
2499 for(i = 0; i < MAX_ARGS; i++)
2500 str_allocated[i] = NULL;
2502 /* parse the parameters */
2503 typestr = cmd->args_type;
2504 nb_args = 0;
2505 for(;;) {
2506 c = *typestr;
2507 if (c == '\0')
2508 break;
2509 typestr++;
2510 switch(c) {
2511 case 'F':
2512 case 'B':
2513 case 's':
2515 int ret;
2516 char *str;
2518 while (qemu_isspace(*p))
2519 p++;
2520 if (*typestr == '?') {
2521 typestr++;
2522 if (*p == '\0') {
2523 /* no optional string: NULL argument */
2524 str = NULL;
2525 goto add_str;
2528 ret = get_str(buf, sizeof(buf), &p);
2529 if (ret < 0) {
2530 switch(c) {
2531 case 'F':
2532 monitor_printf(mon, "%s: filename expected\n",
2533 cmdname);
2534 break;
2535 case 'B':
2536 monitor_printf(mon, "%s: block device name expected\n",
2537 cmdname);
2538 break;
2539 default:
2540 monitor_printf(mon, "%s: string expected\n", cmdname);
2541 break;
2543 goto fail;
2545 str = qemu_malloc(strlen(buf) + 1);
2546 pstrcpy(str, sizeof(buf), buf);
2547 str_allocated[nb_args] = str;
2548 add_str:
2549 if (nb_args >= MAX_ARGS) {
2550 error_args:
2551 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2552 goto fail;
2554 args[nb_args++] = str;
2556 break;
2557 case '/':
2559 int count, format, size;
2561 while (qemu_isspace(*p))
2562 p++;
2563 if (*p == '/') {
2564 /* format found */
2565 p++;
2566 count = 1;
2567 if (qemu_isdigit(*p)) {
2568 count = 0;
2569 while (qemu_isdigit(*p)) {
2570 count = count * 10 + (*p - '0');
2571 p++;
2574 size = -1;
2575 format = -1;
2576 for(;;) {
2577 switch(*p) {
2578 case 'o':
2579 case 'd':
2580 case 'u':
2581 case 'x':
2582 case 'i':
2583 case 'c':
2584 format = *p++;
2585 break;
2586 case 'b':
2587 size = 1;
2588 p++;
2589 break;
2590 case 'h':
2591 size = 2;
2592 p++;
2593 break;
2594 case 'w':
2595 size = 4;
2596 p++;
2597 break;
2598 case 'g':
2599 case 'L':
2600 size = 8;
2601 p++;
2602 break;
2603 default:
2604 goto next;
2607 next:
2608 if (*p != '\0' && !qemu_isspace(*p)) {
2609 monitor_printf(mon, "invalid char in format: '%c'\n",
2610 *p);
2611 goto fail;
2613 if (format < 0)
2614 format = default_fmt_format;
2615 if (format != 'i') {
2616 /* for 'i', not specifying a size gives -1 as size */
2617 if (size < 0)
2618 size = default_fmt_size;
2619 default_fmt_size = size;
2621 default_fmt_format = format;
2622 } else {
2623 count = 1;
2624 format = default_fmt_format;
2625 if (format != 'i') {
2626 size = default_fmt_size;
2627 } else {
2628 size = -1;
2631 if (nb_args + 3 > MAX_ARGS)
2632 goto error_args;
2633 args[nb_args++] = (void*)(long)count;
2634 args[nb_args++] = (void*)(long)format;
2635 args[nb_args++] = (void*)(long)size;
2637 break;
2638 case 'i':
2639 case 'l':
2641 int64_t val;
2643 while (qemu_isspace(*p))
2644 p++;
2645 if (*typestr == '?' || *typestr == '.') {
2646 if (*typestr == '?') {
2647 if (*p == '\0')
2648 has_arg = 0;
2649 else
2650 has_arg = 1;
2651 } else {
2652 if (*p == '.') {
2653 p++;
2654 while (qemu_isspace(*p))
2655 p++;
2656 has_arg = 1;
2657 } else {
2658 has_arg = 0;
2661 typestr++;
2662 if (nb_args >= MAX_ARGS)
2663 goto error_args;
2664 args[nb_args++] = (void *)(long)has_arg;
2665 if (!has_arg) {
2666 if (nb_args >= MAX_ARGS)
2667 goto error_args;
2668 val = -1;
2669 goto add_num;
2672 if (get_expr(mon, &val, &p))
2673 goto fail;
2674 add_num:
2675 if (c == 'i') {
2676 if (nb_args >= MAX_ARGS)
2677 goto error_args;
2678 args[nb_args++] = (void *)(long)val;
2679 } else {
2680 if ((nb_args + 1) >= MAX_ARGS)
2681 goto error_args;
2682 #if TARGET_PHYS_ADDR_BITS > 32
2683 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2684 #else
2685 args[nb_args++] = (void *)0;
2686 #endif
2687 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2690 break;
2691 case '-':
2693 int has_option;
2694 /* option */
2696 c = *typestr++;
2697 if (c == '\0')
2698 goto bad_type;
2699 while (qemu_isspace(*p))
2700 p++;
2701 has_option = 0;
2702 if (*p == '-') {
2703 p++;
2704 if (*p != c) {
2705 monitor_printf(mon, "%s: unsupported option -%c\n",
2706 cmdname, *p);
2707 goto fail;
2709 p++;
2710 has_option = 1;
2712 if (nb_args >= MAX_ARGS)
2713 goto error_args;
2714 args[nb_args++] = (void *)(long)has_option;
2716 break;
2717 default:
2718 bad_type:
2719 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2720 goto fail;
2723 /* check that all arguments were parsed */
2724 while (qemu_isspace(*p))
2725 p++;
2726 if (*p != '\0') {
2727 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2728 cmdname);
2729 goto fail;
2732 switch(nb_args) {
2733 case 0:
2734 handler_0 = cmd->handler;
2735 handler_0(mon);
2736 break;
2737 case 1:
2738 handler_1 = cmd->handler;
2739 handler_1(mon, args[0]);
2740 break;
2741 case 2:
2742 handler_2 = cmd->handler;
2743 handler_2(mon, args[0], args[1]);
2744 break;
2745 case 3:
2746 handler_3 = cmd->handler;
2747 handler_3(mon, args[0], args[1], args[2]);
2748 break;
2749 case 4:
2750 handler_4 = cmd->handler;
2751 handler_4(mon, args[0], args[1], args[2], args[3]);
2752 break;
2753 case 5:
2754 handler_5 = cmd->handler;
2755 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2756 break;
2757 case 6:
2758 handler_6 = cmd->handler;
2759 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2760 break;
2761 case 7:
2762 handler_7 = cmd->handler;
2763 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2764 args[6]);
2765 break;
2766 default:
2767 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2768 goto fail;
2770 fail:
2771 for(i = 0; i < MAX_ARGS; i++)
2772 qemu_free(str_allocated[i]);
2775 static void cmd_completion(const char *name, const char *list)
2777 const char *p, *pstart;
2778 char cmd[128];
2779 int len;
2781 p = list;
2782 for(;;) {
2783 pstart = p;
2784 p = strchr(p, '|');
2785 if (!p)
2786 p = pstart + strlen(pstart);
2787 len = p - pstart;
2788 if (len > sizeof(cmd) - 2)
2789 len = sizeof(cmd) - 2;
2790 memcpy(cmd, pstart, len);
2791 cmd[len] = '\0';
2792 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2793 readline_add_completion(cur_mon->rs, cmd);
2795 if (*p == '\0')
2796 break;
2797 p++;
2801 static void file_completion(const char *input)
2803 DIR *ffs;
2804 struct dirent *d;
2805 char path[1024];
2806 char file[1024], file_prefix[1024];
2807 int input_path_len;
2808 const char *p;
2810 p = strrchr(input, '/');
2811 if (!p) {
2812 input_path_len = 0;
2813 pstrcpy(file_prefix, sizeof(file_prefix), input);
2814 pstrcpy(path, sizeof(path), ".");
2815 } else {
2816 input_path_len = p - input + 1;
2817 memcpy(path, input, input_path_len);
2818 if (input_path_len > sizeof(path) - 1)
2819 input_path_len = sizeof(path) - 1;
2820 path[input_path_len] = '\0';
2821 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2823 #ifdef DEBUG_COMPLETION
2824 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2825 input, path, file_prefix);
2826 #endif
2827 ffs = opendir(path);
2828 if (!ffs)
2829 return;
2830 for(;;) {
2831 struct stat sb;
2832 d = readdir(ffs);
2833 if (!d)
2834 break;
2835 if (strstart(d->d_name, file_prefix, NULL)) {
2836 memcpy(file, input, input_path_len);
2837 if (input_path_len < sizeof(file))
2838 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2839 d->d_name);
2840 /* stat the file to find out if it's a directory.
2841 * In that case add a slash to speed up typing long paths
2843 stat(file, &sb);
2844 if(S_ISDIR(sb.st_mode))
2845 pstrcat(file, sizeof(file), "/");
2846 readline_add_completion(cur_mon->rs, file);
2849 closedir(ffs);
2852 static void block_completion_it(void *opaque, BlockDriverState *bs)
2854 const char *name = bdrv_get_device_name(bs);
2855 const char *input = opaque;
2857 if (input[0] == '\0' ||
2858 !strncmp(name, (char *)input, strlen(input))) {
2859 readline_add_completion(cur_mon->rs, name);
2863 /* NOTE: this parser is an approximate form of the real command parser */
2864 static void parse_cmdline(const char *cmdline,
2865 int *pnb_args, char **args)
2867 const char *p;
2868 int nb_args, ret;
2869 char buf[1024];
2871 p = cmdline;
2872 nb_args = 0;
2873 for(;;) {
2874 while (qemu_isspace(*p))
2875 p++;
2876 if (*p == '\0')
2877 break;
2878 if (nb_args >= MAX_ARGS)
2879 break;
2880 ret = get_str(buf, sizeof(buf), &p);
2881 args[nb_args] = qemu_strdup(buf);
2882 nb_args++;
2883 if (ret < 0)
2884 break;
2886 *pnb_args = nb_args;
2889 static void monitor_find_completion(const char *cmdline)
2891 const char *cmdname;
2892 char *args[MAX_ARGS];
2893 int nb_args, i, len;
2894 const char *ptype, *str;
2895 const mon_cmd_t *cmd;
2896 const KeyDef *key;
2898 parse_cmdline(cmdline, &nb_args, args);
2899 #ifdef DEBUG_COMPLETION
2900 for(i = 0; i < nb_args; i++) {
2901 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2903 #endif
2905 /* if the line ends with a space, it means we want to complete the
2906 next arg */
2907 len = strlen(cmdline);
2908 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2909 if (nb_args >= MAX_ARGS)
2910 return;
2911 args[nb_args++] = qemu_strdup("");
2913 if (nb_args <= 1) {
2914 /* command completion */
2915 if (nb_args == 0)
2916 cmdname = "";
2917 else
2918 cmdname = args[0];
2919 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2920 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2921 cmd_completion(cmdname, cmd->name);
2923 } else {
2924 /* find the command */
2925 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2926 if (compare_cmd(args[0], cmd->name))
2927 goto found;
2929 return;
2930 found:
2931 ptype = cmd->args_type;
2932 for(i = 0; i < nb_args - 2; i++) {
2933 if (*ptype != '\0') {
2934 ptype++;
2935 while (*ptype == '?')
2936 ptype++;
2939 str = args[nb_args - 1];
2940 switch(*ptype) {
2941 case 'F':
2942 /* file completion */
2943 readline_set_completion_index(cur_mon->rs, strlen(str));
2944 file_completion(str);
2945 break;
2946 case 'B':
2947 /* block device name completion */
2948 readline_set_completion_index(cur_mon->rs, strlen(str));
2949 bdrv_iterate(block_completion_it, (void *)str);
2950 break;
2951 case 's':
2952 /* XXX: more generic ? */
2953 if (!strcmp(cmd->name, "info")) {
2954 readline_set_completion_index(cur_mon->rs, strlen(str));
2955 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2956 cmd_completion(str, cmd->name);
2958 } else if (!strcmp(cmd->name, "sendkey")) {
2959 char *sep = strrchr(str, '-');
2960 if (sep)
2961 str = sep + 1;
2962 readline_set_completion_index(cur_mon->rs, strlen(str));
2963 for(key = key_defs; key->name != NULL; key++) {
2964 cmd_completion(str, key->name);
2966 } else if (!strcmp(cmd->name, "help|?")) {
2967 readline_set_completion_index(cur_mon->rs, strlen(str));
2968 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
2969 cmd_completion(str, cmd->name);
2972 break;
2973 default:
2974 break;
2977 for(i = 0; i < nb_args; i++)
2978 qemu_free(args[i]);
2981 static int monitor_can_read(void *opaque)
2983 Monitor *mon = opaque;
2985 return (mon->suspend_cnt == 0) ? 128 : 0;
2988 static void monitor_read(void *opaque, const uint8_t *buf, int size)
2990 Monitor *old_mon = cur_mon;
2991 int i;
2993 cur_mon = opaque;
2995 if (cur_mon->rs) {
2996 for (i = 0; i < size; i++)
2997 readline_handle_byte(cur_mon->rs, buf[i]);
2998 } else {
2999 if (size == 0 || buf[size - 1] != 0)
3000 monitor_printf(cur_mon, "corrupted command\n");
3001 else
3002 monitor_handle_command(cur_mon, (char *)buf);
3005 cur_mon = old_mon;
3008 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3010 monitor_suspend(mon);
3011 monitor_handle_command(mon, cmdline);
3012 monitor_resume(mon);
3015 int monitor_suspend(Monitor *mon)
3017 if (!mon->rs)
3018 return -ENOTTY;
3019 mon->suspend_cnt++;
3020 return 0;
3023 void monitor_resume(Monitor *mon)
3025 if (!mon->rs)
3026 return;
3027 if (--mon->suspend_cnt == 0)
3028 readline_show_prompt(mon->rs);
3031 static void monitor_event(void *opaque, int event)
3033 Monitor *mon = opaque;
3035 switch (event) {
3036 case CHR_EVENT_MUX_IN:
3037 readline_restart(mon->rs);
3038 monitor_resume(mon);
3039 monitor_flush(mon);
3040 break;
3042 case CHR_EVENT_MUX_OUT:
3043 if (mon->suspend_cnt == 0)
3044 monitor_printf(mon, "\n");
3045 monitor_flush(mon);
3046 monitor_suspend(mon);
3047 break;
3049 case CHR_EVENT_RESET:
3050 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3051 "information\n", QEMU_VERSION);
3052 if (mon->chr->focus == 0)
3053 readline_show_prompt(mon->rs);
3054 break;
3060 * Local variables:
3061 * c-indent-level: 4
3062 * c-basic-offset: 4
3063 * tab-width: 8
3064 * End:
3067 void monitor_init(CharDriverState *chr, int flags)
3069 static int is_first_init = 1;
3070 Monitor *mon;
3072 if (is_first_init) {
3073 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3074 is_first_init = 0;
3077 mon = qemu_mallocz(sizeof(*mon));
3079 mon->chr = chr;
3080 mon->flags = flags;
3081 if (mon->chr->focus != 0)
3082 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3083 if (flags & MONITOR_USE_READLINE) {
3084 mon->rs = readline_init(mon, monitor_find_completion);
3085 monitor_read_command(mon, 0);
3088 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3089 mon);
3091 LIST_INSERT_HEAD(&mon_list, mon, entry);
3092 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3093 cur_mon = mon;
3096 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3098 BlockDriverState *bs = opaque;
3099 int ret = 0;
3101 if (bdrv_set_key(bs, password) != 0) {
3102 monitor_printf(mon, "invalid password\n");
3103 ret = -EPERM;
3105 if (mon->password_completion_cb)
3106 mon->password_completion_cb(mon->password_opaque, ret);
3108 monitor_read_command(mon, 1);
3111 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3112 BlockDriverCompletionFunc *completion_cb,
3113 void *opaque)
3115 int err;
3117 if (!bdrv_key_required(bs)) {
3118 if (completion_cb)
3119 completion_cb(opaque, 0);
3120 return;
3123 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3124 bdrv_get_encrypted_filename(bs));
3126 mon->password_completion_cb = completion_cb;
3127 mon->password_opaque = opaque;
3129 err = monitor_read_password(mon, bdrv_password_cb, bs);
3131 if (err && completion_cb)
3132 completion_cb(opaque, err);