Merge commit '9637443f852a3abb61c0c8f33c567ff87928e845' into upstream-merge
[qemu-kvm/fedora.git] / monitor.c
blobfec9a19a8e5126cdd1def066b42bb899eea82fb1
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 & IOPORTS_MASK, 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 static void do_ioport_write(Monitor *mon, int count, int format, int size,
1210 int addr, int val)
1212 addr &= IOPORTS_MASK;
1214 switch (size) {
1215 default:
1216 case 1:
1217 cpu_outb(NULL, addr, val);
1218 break;
1219 case 2:
1220 cpu_outw(NULL, addr, val);
1221 break;
1222 case 4:
1223 cpu_outl(NULL, addr, val);
1224 break;
1228 static void do_boot_set(Monitor *mon, const char *bootdevice)
1230 int res;
1232 res = qemu_boot_set(bootdevice);
1233 if (res == 0) {
1234 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1235 } else if (res > 0) {
1236 monitor_printf(mon, "setting boot device list failed\n");
1237 } else {
1238 monitor_printf(mon, "no function defined to set boot device list for "
1239 "this architecture\n");
1243 static void do_system_reset(Monitor *mon)
1245 qemu_system_reset_request();
1248 static void do_system_powerdown(Monitor *mon)
1250 qemu_system_powerdown_request();
1253 #if defined(TARGET_I386)
1254 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1256 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1257 addr,
1258 pte & mask,
1259 pte & PG_GLOBAL_MASK ? 'G' : '-',
1260 pte & PG_PSE_MASK ? 'P' : '-',
1261 pte & PG_DIRTY_MASK ? 'D' : '-',
1262 pte & PG_ACCESSED_MASK ? 'A' : '-',
1263 pte & PG_PCD_MASK ? 'C' : '-',
1264 pte & PG_PWT_MASK ? 'T' : '-',
1265 pte & PG_USER_MASK ? 'U' : '-',
1266 pte & PG_RW_MASK ? 'W' : '-');
1269 static void tlb_info(Monitor *mon)
1271 CPUState *env;
1272 int l1, l2;
1273 uint32_t pgd, pde, pte;
1275 env = mon_get_cpu();
1276 if (!env)
1277 return;
1279 if (!(env->cr[0] & CR0_PG_MASK)) {
1280 monitor_printf(mon, "PG disabled\n");
1281 return;
1283 pgd = env->cr[3] & ~0xfff;
1284 for(l1 = 0; l1 < 1024; l1++) {
1285 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1286 pde = le32_to_cpu(pde);
1287 if (pde & PG_PRESENT_MASK) {
1288 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1289 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1290 } else {
1291 for(l2 = 0; l2 < 1024; l2++) {
1292 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1293 (uint8_t *)&pte, 4);
1294 pte = le32_to_cpu(pte);
1295 if (pte & PG_PRESENT_MASK) {
1296 print_pte(mon, (l1 << 22) + (l2 << 12),
1297 pte & ~PG_PSE_MASK,
1298 ~0xfff);
1306 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1307 uint32_t end, int prot)
1309 int prot1;
1310 prot1 = *plast_prot;
1311 if (prot != prot1) {
1312 if (*pstart != -1) {
1313 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1314 *pstart, end, end - *pstart,
1315 prot1 & PG_USER_MASK ? 'u' : '-',
1316 'r',
1317 prot1 & PG_RW_MASK ? 'w' : '-');
1319 if (prot != 0)
1320 *pstart = end;
1321 else
1322 *pstart = -1;
1323 *plast_prot = prot;
1327 static void mem_info(Monitor *mon)
1329 CPUState *env;
1330 int l1, l2, prot, last_prot;
1331 uint32_t pgd, pde, pte, start, end;
1333 env = mon_get_cpu();
1334 if (!env)
1335 return;
1337 if (!(env->cr[0] & CR0_PG_MASK)) {
1338 monitor_printf(mon, "PG disabled\n");
1339 return;
1341 pgd = env->cr[3] & ~0xfff;
1342 last_prot = 0;
1343 start = -1;
1344 for(l1 = 0; l1 < 1024; l1++) {
1345 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1346 pde = le32_to_cpu(pde);
1347 end = l1 << 22;
1348 if (pde & PG_PRESENT_MASK) {
1349 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1350 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1351 mem_print(mon, &start, &last_prot, end, prot);
1352 } else {
1353 for(l2 = 0; l2 < 1024; l2++) {
1354 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1355 (uint8_t *)&pte, 4);
1356 pte = le32_to_cpu(pte);
1357 end = (l1 << 22) + (l2 << 12);
1358 if (pte & PG_PRESENT_MASK) {
1359 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1360 } else {
1361 prot = 0;
1363 mem_print(mon, &start, &last_prot, end, prot);
1366 } else {
1367 prot = 0;
1368 mem_print(mon, &start, &last_prot, end, prot);
1372 #endif
1374 #if defined(TARGET_SH4)
1376 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1378 monitor_printf(mon, " tlb%i:\t"
1379 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1380 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1381 "dirty=%hhu writethrough=%hhu\n",
1382 idx,
1383 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1384 tlb->v, tlb->sh, tlb->c, tlb->pr,
1385 tlb->d, tlb->wt);
1388 static void tlb_info(Monitor *mon)
1390 CPUState *env = mon_get_cpu();
1391 int i;
1393 monitor_printf (mon, "ITLB:\n");
1394 for (i = 0 ; i < ITLB_SIZE ; i++)
1395 print_tlb (mon, i, &env->itlb[i]);
1396 monitor_printf (mon, "UTLB:\n");
1397 for (i = 0 ; i < UTLB_SIZE ; i++)
1398 print_tlb (mon, i, &env->utlb[i]);
1401 #endif
1403 static void do_info_kqemu(Monitor *mon)
1405 #ifdef CONFIG_KQEMU
1406 CPUState *env;
1407 int val;
1408 val = 0;
1409 env = mon_get_cpu();
1410 if (!env) {
1411 monitor_printf(mon, "No cpu initialized yet");
1412 return;
1414 val = env->kqemu_enabled;
1415 monitor_printf(mon, "kqemu support: ");
1416 switch(val) {
1417 default:
1418 case 0:
1419 monitor_printf(mon, "disabled\n");
1420 break;
1421 case 1:
1422 monitor_printf(mon, "enabled for user code\n");
1423 break;
1424 case 2:
1425 monitor_printf(mon, "enabled for user and kernel code\n");
1426 break;
1428 #else
1429 monitor_printf(mon, "kqemu support: not compiled\n");
1430 #endif
1433 static void do_info_kvm(Monitor *mon)
1435 #if defined(USE_KVM) || defined(CONFIG_KVM)
1436 monitor_printf(mon, "kvm support: ");
1437 if (kvm_enabled())
1438 monitor_printf(mon, "enabled\n");
1439 else
1440 monitor_printf(mon, "disabled\n");
1441 #else
1442 monitor_printf(mon, "kvm support: not compiled\n");
1443 #endif
1446 static void do_info_numa(Monitor *mon)
1448 int i;
1449 CPUState *env;
1451 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1452 for (i = 0; i < nb_numa_nodes; i++) {
1453 monitor_printf(mon, "node %d cpus:", i);
1454 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1455 if (env->numa_node == i) {
1456 monitor_printf(mon, " %d", env->cpu_index);
1459 monitor_printf(mon, "\n");
1460 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1461 node_mem[i] >> 20);
1465 #ifdef CONFIG_PROFILER
1467 int64_t kqemu_time;
1468 int64_t qemu_time;
1469 int64_t kqemu_exec_count;
1470 int64_t dev_time;
1471 int64_t kqemu_ret_int_count;
1472 int64_t kqemu_ret_excp_count;
1473 int64_t kqemu_ret_intr_count;
1475 static void do_info_profile(Monitor *mon)
1477 int64_t total;
1478 total = qemu_time;
1479 if (total == 0)
1480 total = 1;
1481 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1482 dev_time, dev_time / (double)ticks_per_sec);
1483 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1484 qemu_time, qemu_time / (double)ticks_per_sec);
1485 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1486 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1487 PRId64 "\n",
1488 kqemu_time, kqemu_time / (double)ticks_per_sec,
1489 kqemu_time / (double)total * 100.0,
1490 kqemu_exec_count,
1491 kqemu_ret_int_count,
1492 kqemu_ret_excp_count,
1493 kqemu_ret_intr_count);
1494 qemu_time = 0;
1495 kqemu_time = 0;
1496 kqemu_exec_count = 0;
1497 dev_time = 0;
1498 kqemu_ret_int_count = 0;
1499 kqemu_ret_excp_count = 0;
1500 kqemu_ret_intr_count = 0;
1501 #ifdef CONFIG_KQEMU
1502 kqemu_record_dump();
1503 #endif
1505 #else
1506 static void do_info_profile(Monitor *mon)
1508 monitor_printf(mon, "Internal profiler not compiled\n");
1510 #endif
1512 /* Capture support */
1513 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1515 static void do_info_capture(Monitor *mon)
1517 int i;
1518 CaptureState *s;
1520 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1521 monitor_printf(mon, "[%d]: ", i);
1522 s->ops.info (s->opaque);
1526 #ifdef HAS_AUDIO
1527 static void do_stop_capture(Monitor *mon, int n)
1529 int i;
1530 CaptureState *s;
1532 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1533 if (i == n) {
1534 s->ops.destroy (s->opaque);
1535 LIST_REMOVE (s, entries);
1536 qemu_free (s);
1537 return;
1542 static void do_wav_capture(Monitor *mon, const char *path,
1543 int has_freq, int freq,
1544 int has_bits, int bits,
1545 int has_channels, int nchannels)
1547 CaptureState *s;
1549 s = qemu_mallocz (sizeof (*s));
1551 freq = has_freq ? freq : 44100;
1552 bits = has_bits ? bits : 16;
1553 nchannels = has_channels ? nchannels : 2;
1555 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1556 monitor_printf(mon, "Faied to add wave capture\n");
1557 qemu_free (s);
1559 LIST_INSERT_HEAD (&capture_head, s, entries);
1561 #endif
1563 #if defined(TARGET_I386)
1564 static void do_inject_nmi(Monitor *mon, int cpu_index)
1566 CPUState *env;
1568 for (env = first_cpu; env != NULL; env = env->next_cpu)
1569 if (env->cpu_index == cpu_index) {
1570 if (kvm_enabled())
1571 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1572 else
1573 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1574 break;
1577 #endif
1579 static void do_info_status(Monitor *mon)
1581 if (vm_running) {
1582 if (singlestep) {
1583 monitor_printf(mon, "VM status: running (single step mode)\n");
1584 } else {
1585 monitor_printf(mon, "VM status: running\n");
1587 } else
1588 monitor_printf(mon, "VM status: paused\n");
1592 static void do_balloon(Monitor *mon, int value)
1594 ram_addr_t target = value;
1595 qemu_balloon(target << 20);
1598 static void do_info_balloon(Monitor *mon)
1600 ram_addr_t actual;
1602 actual = qemu_balloon_status();
1603 if (kvm_enabled() && !kvm_has_sync_mmu())
1604 monitor_printf(mon, "Using KVM without synchronous MMU, "
1605 "ballooning disabled\n");
1606 else if (actual == 0)
1607 monitor_printf(mon, "Ballooning not activated in VM\n");
1608 else
1609 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1612 static qemu_acl *find_acl(Monitor *mon, const char *name)
1614 qemu_acl *acl = qemu_acl_find(name);
1616 if (!acl) {
1617 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1619 return acl;
1622 static void do_acl_show(Monitor *mon, const char *aclname)
1624 qemu_acl *acl = find_acl(mon, aclname);
1625 qemu_acl_entry *entry;
1626 int i = 0;
1628 if (acl) {
1629 monitor_printf(mon, "policy: %s\n",
1630 acl->defaultDeny ? "deny" : "allow");
1631 TAILQ_FOREACH(entry, &acl->entries, next) {
1632 i++;
1633 monitor_printf(mon, "%d: %s %s\n", i,
1634 entry->deny ? "deny" : "allow", entry->match);
1639 static void do_acl_reset(Monitor *mon, const char *aclname)
1641 qemu_acl *acl = find_acl(mon, aclname);
1643 if (acl) {
1644 qemu_acl_reset(acl);
1645 monitor_printf(mon, "acl: removed all rules\n");
1649 static void do_acl_policy(Monitor *mon, const char *aclname,
1650 const char *policy)
1652 qemu_acl *acl = find_acl(mon, aclname);
1654 if (acl) {
1655 if (strcmp(policy, "allow") == 0) {
1656 acl->defaultDeny = 0;
1657 monitor_printf(mon, "acl: policy set to 'allow'\n");
1658 } else if (strcmp(policy, "deny") == 0) {
1659 acl->defaultDeny = 1;
1660 monitor_printf(mon, "acl: policy set to 'deny'\n");
1661 } else {
1662 monitor_printf(mon, "acl: unknown policy '%s', "
1663 "expected 'deny' or 'allow'\n", policy);
1668 static void do_acl_add(Monitor *mon, const char *aclname,
1669 const char *match, const char *policy,
1670 int has_index, int index)
1672 qemu_acl *acl = find_acl(mon, aclname);
1673 int deny, ret;
1675 if (acl) {
1676 if (strcmp(policy, "allow") == 0) {
1677 deny = 0;
1678 } else if (strcmp(policy, "deny") == 0) {
1679 deny = 1;
1680 } else {
1681 monitor_printf(mon, "acl: unknown policy '%s', "
1682 "expected 'deny' or 'allow'\n", policy);
1683 return;
1685 if (has_index)
1686 ret = qemu_acl_insert(acl, deny, match, index);
1687 else
1688 ret = qemu_acl_append(acl, deny, match);
1689 if (ret < 0)
1690 monitor_printf(mon, "acl: unable to add acl entry\n");
1691 else
1692 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1696 static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1698 qemu_acl *acl = find_acl(mon, aclname);
1699 int ret;
1701 if (acl) {
1702 ret = qemu_acl_remove(acl, match);
1703 if (ret < 0)
1704 monitor_printf(mon, "acl: no matching acl entry\n");
1705 else
1706 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1710 #if defined(TARGET_I386)
1711 static void do_inject_mce(Monitor *mon,
1712 int cpu_index, int bank,
1713 unsigned status_hi, unsigned status_lo,
1714 unsigned mcg_status_hi, unsigned mcg_status_lo,
1715 unsigned addr_hi, unsigned addr_lo,
1716 unsigned misc_hi, unsigned misc_lo)
1718 CPUState *cenv;
1719 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1720 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1721 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1722 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1724 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1725 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1726 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1727 break;
1730 #endif
1732 static const mon_cmd_t mon_cmds[] = {
1733 #include "qemu-monitor.h"
1734 { NULL, NULL, },
1737 /* Please update qemu-monitor.hx when adding or changing commands */
1738 static const mon_cmd_t info_cmds[] = {
1739 { "version", "", do_info_version,
1740 "", "show the version of QEMU" },
1741 { "network", "", do_info_network,
1742 "", "show the network state" },
1743 { "chardev", "", qemu_chr_info,
1744 "", "show the character devices" },
1745 { "block", "", bdrv_info,
1746 "", "show the block devices" },
1747 { "blockstats", "", bdrv_info_stats,
1748 "", "show block device statistics" },
1749 { "registers", "", do_info_registers,
1750 "", "show the cpu registers" },
1751 { "cpus", "", do_info_cpus,
1752 "", "show infos for each CPU" },
1753 { "history", "", do_info_history,
1754 "", "show the command line history", },
1755 { "irq", "", irq_info,
1756 "", "show the interrupts statistics (if available)", },
1757 { "pic", "", pic_info,
1758 "", "show i8259 (PIC) state", },
1759 { "pci", "", pci_info,
1760 "", "show PCI info", },
1761 #if defined(TARGET_I386) || defined(TARGET_SH4)
1762 { "tlb", "", tlb_info,
1763 "", "show virtual to physical memory mappings", },
1764 #endif
1765 #if defined(TARGET_I386)
1766 { "mem", "", mem_info,
1767 "", "show the active virtual memory mappings", },
1768 { "hpet", "", do_info_hpet,
1769 "", "show state of HPET", },
1770 #endif
1771 { "jit", "", do_info_jit,
1772 "", "show dynamic compiler info", },
1773 { "kqemu", "", do_info_kqemu,
1774 "", "show KQEMU information", },
1775 { "kvm", "", do_info_kvm,
1776 "", "show KVM information", },
1777 { "numa", "", do_info_numa,
1778 "", "show NUMA information", },
1779 { "usb", "", usb_info,
1780 "", "show guest USB devices", },
1781 { "usbhost", "", usb_host_info,
1782 "", "show host USB devices", },
1783 { "profile", "", do_info_profile,
1784 "", "show profiling information", },
1785 { "capture", "", do_info_capture,
1786 "", "show capture information" },
1787 { "snapshots", "", do_info_snapshots,
1788 "", "show the currently saved VM snapshots" },
1789 { "status", "", do_info_status,
1790 "", "show the current VM status (running|paused)" },
1791 { "pcmcia", "", pcmcia_info,
1792 "", "show guest PCMCIA status" },
1793 { "mice", "", do_info_mice,
1794 "", "show which guest mouse is receiving events" },
1795 { "vnc", "", do_info_vnc,
1796 "", "show the vnc server status"},
1797 { "name", "", do_info_name,
1798 "", "show the current VM name" },
1799 { "uuid", "", do_info_uuid,
1800 "", "show the current VM UUID" },
1801 #if defined(TARGET_PPC)
1802 { "cpustats", "", do_info_cpu_stats,
1803 "", "show CPU statistics", },
1804 #endif
1805 #if defined(CONFIG_SLIRP)
1806 { "usernet", "", do_info_usernet,
1807 "", "show user network stack connection states", },
1808 #endif
1809 { "migrate", "", do_info_migrate, "", "show migration status" },
1810 { "balloon", "", do_info_balloon,
1811 "", "show balloon information" },
1812 { "qtree", "", do_info_qtree,
1813 "", "show device tree" },
1814 { NULL, NULL, },
1817 /*******************************************************************/
1819 static const char *pch;
1820 static jmp_buf expr_env;
1822 #define MD_TLONG 0
1823 #define MD_I32 1
1825 typedef struct MonitorDef {
1826 const char *name;
1827 int offset;
1828 target_long (*get_value)(const struct MonitorDef *md, int val);
1829 int type;
1830 } MonitorDef;
1832 #if defined(TARGET_I386)
1833 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1835 CPUState *env = mon_get_cpu();
1836 if (!env)
1837 return 0;
1838 return env->eip + env->segs[R_CS].base;
1840 #endif
1842 #if defined(TARGET_PPC)
1843 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1845 CPUState *env = mon_get_cpu();
1846 unsigned int u;
1847 int i;
1849 if (!env)
1850 return 0;
1852 u = 0;
1853 for (i = 0; i < 8; i++)
1854 u |= env->crf[i] << (32 - (4 * i));
1856 return u;
1859 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1861 CPUState *env = mon_get_cpu();
1862 if (!env)
1863 return 0;
1864 return env->msr;
1867 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1869 CPUState *env = mon_get_cpu();
1870 if (!env)
1871 return 0;
1872 return env->xer;
1875 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1877 CPUState *env = mon_get_cpu();
1878 if (!env)
1879 return 0;
1880 return cpu_ppc_load_decr(env);
1883 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1885 CPUState *env = mon_get_cpu();
1886 if (!env)
1887 return 0;
1888 return cpu_ppc_load_tbu(env);
1891 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1893 CPUState *env = mon_get_cpu();
1894 if (!env)
1895 return 0;
1896 return cpu_ppc_load_tbl(env);
1898 #endif
1900 #if defined(TARGET_SPARC)
1901 #ifndef TARGET_SPARC64
1902 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1904 CPUState *env = mon_get_cpu();
1905 if (!env)
1906 return 0;
1907 return GET_PSR(env);
1909 #endif
1911 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1913 CPUState *env = mon_get_cpu();
1914 if (!env)
1915 return 0;
1916 return env->regwptr[val];
1918 #endif
1920 static const MonitorDef monitor_defs[] = {
1921 #ifdef TARGET_I386
1923 #define SEG(name, seg) \
1924 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1925 { name ".base", offsetof(CPUState, segs[seg].base) },\
1926 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1928 { "eax", offsetof(CPUState, regs[0]) },
1929 { "ecx", offsetof(CPUState, regs[1]) },
1930 { "edx", offsetof(CPUState, regs[2]) },
1931 { "ebx", offsetof(CPUState, regs[3]) },
1932 { "esp|sp", offsetof(CPUState, regs[4]) },
1933 { "ebp|fp", offsetof(CPUState, regs[5]) },
1934 { "esi", offsetof(CPUState, regs[6]) },
1935 { "edi", offsetof(CPUState, regs[7]) },
1936 #ifdef TARGET_X86_64
1937 { "r8", offsetof(CPUState, regs[8]) },
1938 { "r9", offsetof(CPUState, regs[9]) },
1939 { "r10", offsetof(CPUState, regs[10]) },
1940 { "r11", offsetof(CPUState, regs[11]) },
1941 { "r12", offsetof(CPUState, regs[12]) },
1942 { "r13", offsetof(CPUState, regs[13]) },
1943 { "r14", offsetof(CPUState, regs[14]) },
1944 { "r15", offsetof(CPUState, regs[15]) },
1945 #endif
1946 { "eflags", offsetof(CPUState, eflags) },
1947 { "eip", offsetof(CPUState, eip) },
1948 SEG("cs", R_CS)
1949 SEG("ds", R_DS)
1950 SEG("es", R_ES)
1951 SEG("ss", R_SS)
1952 SEG("fs", R_FS)
1953 SEG("gs", R_GS)
1954 { "pc", 0, monitor_get_pc, },
1955 #elif defined(TARGET_PPC)
1956 /* General purpose registers */
1957 { "r0", offsetof(CPUState, gpr[0]) },
1958 { "r1", offsetof(CPUState, gpr[1]) },
1959 { "r2", offsetof(CPUState, gpr[2]) },
1960 { "r3", offsetof(CPUState, gpr[3]) },
1961 { "r4", offsetof(CPUState, gpr[4]) },
1962 { "r5", offsetof(CPUState, gpr[5]) },
1963 { "r6", offsetof(CPUState, gpr[6]) },
1964 { "r7", offsetof(CPUState, gpr[7]) },
1965 { "r8", offsetof(CPUState, gpr[8]) },
1966 { "r9", offsetof(CPUState, gpr[9]) },
1967 { "r10", offsetof(CPUState, gpr[10]) },
1968 { "r11", offsetof(CPUState, gpr[11]) },
1969 { "r12", offsetof(CPUState, gpr[12]) },
1970 { "r13", offsetof(CPUState, gpr[13]) },
1971 { "r14", offsetof(CPUState, gpr[14]) },
1972 { "r15", offsetof(CPUState, gpr[15]) },
1973 { "r16", offsetof(CPUState, gpr[16]) },
1974 { "r17", offsetof(CPUState, gpr[17]) },
1975 { "r18", offsetof(CPUState, gpr[18]) },
1976 { "r19", offsetof(CPUState, gpr[19]) },
1977 { "r20", offsetof(CPUState, gpr[20]) },
1978 { "r21", offsetof(CPUState, gpr[21]) },
1979 { "r22", offsetof(CPUState, gpr[22]) },
1980 { "r23", offsetof(CPUState, gpr[23]) },
1981 { "r24", offsetof(CPUState, gpr[24]) },
1982 { "r25", offsetof(CPUState, gpr[25]) },
1983 { "r26", offsetof(CPUState, gpr[26]) },
1984 { "r27", offsetof(CPUState, gpr[27]) },
1985 { "r28", offsetof(CPUState, gpr[28]) },
1986 { "r29", offsetof(CPUState, gpr[29]) },
1987 { "r30", offsetof(CPUState, gpr[30]) },
1988 { "r31", offsetof(CPUState, gpr[31]) },
1989 /* Floating point registers */
1990 { "f0", offsetof(CPUState, fpr[0]) },
1991 { "f1", offsetof(CPUState, fpr[1]) },
1992 { "f2", offsetof(CPUState, fpr[2]) },
1993 { "f3", offsetof(CPUState, fpr[3]) },
1994 { "f4", offsetof(CPUState, fpr[4]) },
1995 { "f5", offsetof(CPUState, fpr[5]) },
1996 { "f6", offsetof(CPUState, fpr[6]) },
1997 { "f7", offsetof(CPUState, fpr[7]) },
1998 { "f8", offsetof(CPUState, fpr[8]) },
1999 { "f9", offsetof(CPUState, fpr[9]) },
2000 { "f10", offsetof(CPUState, fpr[10]) },
2001 { "f11", offsetof(CPUState, fpr[11]) },
2002 { "f12", offsetof(CPUState, fpr[12]) },
2003 { "f13", offsetof(CPUState, fpr[13]) },
2004 { "f14", offsetof(CPUState, fpr[14]) },
2005 { "f15", offsetof(CPUState, fpr[15]) },
2006 { "f16", offsetof(CPUState, fpr[16]) },
2007 { "f17", offsetof(CPUState, fpr[17]) },
2008 { "f18", offsetof(CPUState, fpr[18]) },
2009 { "f19", offsetof(CPUState, fpr[19]) },
2010 { "f20", offsetof(CPUState, fpr[20]) },
2011 { "f21", offsetof(CPUState, fpr[21]) },
2012 { "f22", offsetof(CPUState, fpr[22]) },
2013 { "f23", offsetof(CPUState, fpr[23]) },
2014 { "f24", offsetof(CPUState, fpr[24]) },
2015 { "f25", offsetof(CPUState, fpr[25]) },
2016 { "f26", offsetof(CPUState, fpr[26]) },
2017 { "f27", offsetof(CPUState, fpr[27]) },
2018 { "f28", offsetof(CPUState, fpr[28]) },
2019 { "f29", offsetof(CPUState, fpr[29]) },
2020 { "f30", offsetof(CPUState, fpr[30]) },
2021 { "f31", offsetof(CPUState, fpr[31]) },
2022 { "fpscr", offsetof(CPUState, fpscr) },
2023 /* Next instruction pointer */
2024 { "nip|pc", offsetof(CPUState, nip) },
2025 { "lr", offsetof(CPUState, lr) },
2026 { "ctr", offsetof(CPUState, ctr) },
2027 { "decr", 0, &monitor_get_decr, },
2028 { "ccr", 0, &monitor_get_ccr, },
2029 /* Machine state register */
2030 { "msr", 0, &monitor_get_msr, },
2031 { "xer", 0, &monitor_get_xer, },
2032 { "tbu", 0, &monitor_get_tbu, },
2033 { "tbl", 0, &monitor_get_tbl, },
2034 #if defined(TARGET_PPC64)
2035 /* Address space register */
2036 { "asr", offsetof(CPUState, asr) },
2037 #endif
2038 /* Segment registers */
2039 { "sdr1", offsetof(CPUState, sdr1) },
2040 { "sr0", offsetof(CPUState, sr[0]) },
2041 { "sr1", offsetof(CPUState, sr[1]) },
2042 { "sr2", offsetof(CPUState, sr[2]) },
2043 { "sr3", offsetof(CPUState, sr[3]) },
2044 { "sr4", offsetof(CPUState, sr[4]) },
2045 { "sr5", offsetof(CPUState, sr[5]) },
2046 { "sr6", offsetof(CPUState, sr[6]) },
2047 { "sr7", offsetof(CPUState, sr[7]) },
2048 { "sr8", offsetof(CPUState, sr[8]) },
2049 { "sr9", offsetof(CPUState, sr[9]) },
2050 { "sr10", offsetof(CPUState, sr[10]) },
2051 { "sr11", offsetof(CPUState, sr[11]) },
2052 { "sr12", offsetof(CPUState, sr[12]) },
2053 { "sr13", offsetof(CPUState, sr[13]) },
2054 { "sr14", offsetof(CPUState, sr[14]) },
2055 { "sr15", offsetof(CPUState, sr[15]) },
2056 /* Too lazy to put BATs and SPRs ... */
2057 #elif defined(TARGET_SPARC)
2058 { "g0", offsetof(CPUState, gregs[0]) },
2059 { "g1", offsetof(CPUState, gregs[1]) },
2060 { "g2", offsetof(CPUState, gregs[2]) },
2061 { "g3", offsetof(CPUState, gregs[3]) },
2062 { "g4", offsetof(CPUState, gregs[4]) },
2063 { "g5", offsetof(CPUState, gregs[5]) },
2064 { "g6", offsetof(CPUState, gregs[6]) },
2065 { "g7", offsetof(CPUState, gregs[7]) },
2066 { "o0", 0, monitor_get_reg },
2067 { "o1", 1, monitor_get_reg },
2068 { "o2", 2, monitor_get_reg },
2069 { "o3", 3, monitor_get_reg },
2070 { "o4", 4, monitor_get_reg },
2071 { "o5", 5, monitor_get_reg },
2072 { "o6", 6, monitor_get_reg },
2073 { "o7", 7, monitor_get_reg },
2074 { "l0", 8, monitor_get_reg },
2075 { "l1", 9, monitor_get_reg },
2076 { "l2", 10, monitor_get_reg },
2077 { "l3", 11, monitor_get_reg },
2078 { "l4", 12, monitor_get_reg },
2079 { "l5", 13, monitor_get_reg },
2080 { "l6", 14, monitor_get_reg },
2081 { "l7", 15, monitor_get_reg },
2082 { "i0", 16, monitor_get_reg },
2083 { "i1", 17, monitor_get_reg },
2084 { "i2", 18, monitor_get_reg },
2085 { "i3", 19, monitor_get_reg },
2086 { "i4", 20, monitor_get_reg },
2087 { "i5", 21, monitor_get_reg },
2088 { "i6", 22, monitor_get_reg },
2089 { "i7", 23, monitor_get_reg },
2090 { "pc", offsetof(CPUState, pc) },
2091 { "npc", offsetof(CPUState, npc) },
2092 { "y", offsetof(CPUState, y) },
2093 #ifndef TARGET_SPARC64
2094 { "psr", 0, &monitor_get_psr, },
2095 { "wim", offsetof(CPUState, wim) },
2096 #endif
2097 { "tbr", offsetof(CPUState, tbr) },
2098 { "fsr", offsetof(CPUState, fsr) },
2099 { "f0", offsetof(CPUState, fpr[0]) },
2100 { "f1", offsetof(CPUState, fpr[1]) },
2101 { "f2", offsetof(CPUState, fpr[2]) },
2102 { "f3", offsetof(CPUState, fpr[3]) },
2103 { "f4", offsetof(CPUState, fpr[4]) },
2104 { "f5", offsetof(CPUState, fpr[5]) },
2105 { "f6", offsetof(CPUState, fpr[6]) },
2106 { "f7", offsetof(CPUState, fpr[7]) },
2107 { "f8", offsetof(CPUState, fpr[8]) },
2108 { "f9", offsetof(CPUState, fpr[9]) },
2109 { "f10", offsetof(CPUState, fpr[10]) },
2110 { "f11", offsetof(CPUState, fpr[11]) },
2111 { "f12", offsetof(CPUState, fpr[12]) },
2112 { "f13", offsetof(CPUState, fpr[13]) },
2113 { "f14", offsetof(CPUState, fpr[14]) },
2114 { "f15", offsetof(CPUState, fpr[15]) },
2115 { "f16", offsetof(CPUState, fpr[16]) },
2116 { "f17", offsetof(CPUState, fpr[17]) },
2117 { "f18", offsetof(CPUState, fpr[18]) },
2118 { "f19", offsetof(CPUState, fpr[19]) },
2119 { "f20", offsetof(CPUState, fpr[20]) },
2120 { "f21", offsetof(CPUState, fpr[21]) },
2121 { "f22", offsetof(CPUState, fpr[22]) },
2122 { "f23", offsetof(CPUState, fpr[23]) },
2123 { "f24", offsetof(CPUState, fpr[24]) },
2124 { "f25", offsetof(CPUState, fpr[25]) },
2125 { "f26", offsetof(CPUState, fpr[26]) },
2126 { "f27", offsetof(CPUState, fpr[27]) },
2127 { "f28", offsetof(CPUState, fpr[28]) },
2128 { "f29", offsetof(CPUState, fpr[29]) },
2129 { "f30", offsetof(CPUState, fpr[30]) },
2130 { "f31", offsetof(CPUState, fpr[31]) },
2131 #ifdef TARGET_SPARC64
2132 { "f32", offsetof(CPUState, fpr[32]) },
2133 { "f34", offsetof(CPUState, fpr[34]) },
2134 { "f36", offsetof(CPUState, fpr[36]) },
2135 { "f38", offsetof(CPUState, fpr[38]) },
2136 { "f40", offsetof(CPUState, fpr[40]) },
2137 { "f42", offsetof(CPUState, fpr[42]) },
2138 { "f44", offsetof(CPUState, fpr[44]) },
2139 { "f46", offsetof(CPUState, fpr[46]) },
2140 { "f48", offsetof(CPUState, fpr[48]) },
2141 { "f50", offsetof(CPUState, fpr[50]) },
2142 { "f52", offsetof(CPUState, fpr[52]) },
2143 { "f54", offsetof(CPUState, fpr[54]) },
2144 { "f56", offsetof(CPUState, fpr[56]) },
2145 { "f58", offsetof(CPUState, fpr[58]) },
2146 { "f60", offsetof(CPUState, fpr[60]) },
2147 { "f62", offsetof(CPUState, fpr[62]) },
2148 { "asi", offsetof(CPUState, asi) },
2149 { "pstate", offsetof(CPUState, pstate) },
2150 { "cansave", offsetof(CPUState, cansave) },
2151 { "canrestore", offsetof(CPUState, canrestore) },
2152 { "otherwin", offsetof(CPUState, otherwin) },
2153 { "wstate", offsetof(CPUState, wstate) },
2154 { "cleanwin", offsetof(CPUState, cleanwin) },
2155 { "fprs", offsetof(CPUState, fprs) },
2156 #endif
2157 #endif
2158 { NULL },
2161 static void expr_error(Monitor *mon, const char *msg)
2163 monitor_printf(mon, "%s\n", msg);
2164 longjmp(expr_env, 1);
2167 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2168 static int get_monitor_def(target_long *pval, const char *name)
2170 const MonitorDef *md;
2171 void *ptr;
2173 for(md = monitor_defs; md->name != NULL; md++) {
2174 if (compare_cmd(name, md->name)) {
2175 if (md->get_value) {
2176 *pval = md->get_value(md, md->offset);
2177 } else {
2178 CPUState *env = mon_get_cpu();
2179 if (!env)
2180 return -2;
2181 ptr = (uint8_t *)env + md->offset;
2182 switch(md->type) {
2183 case MD_I32:
2184 *pval = *(int32_t *)ptr;
2185 break;
2186 case MD_TLONG:
2187 *pval = *(target_long *)ptr;
2188 break;
2189 default:
2190 *pval = 0;
2191 break;
2194 return 0;
2197 return -1;
2200 static void next(void)
2202 if (pch != '\0') {
2203 pch++;
2204 while (qemu_isspace(*pch))
2205 pch++;
2209 static int64_t expr_sum(Monitor *mon);
2211 static int64_t expr_unary(Monitor *mon)
2213 int64_t n;
2214 char *p;
2215 int ret;
2217 switch(*pch) {
2218 case '+':
2219 next();
2220 n = expr_unary(mon);
2221 break;
2222 case '-':
2223 next();
2224 n = -expr_unary(mon);
2225 break;
2226 case '~':
2227 next();
2228 n = ~expr_unary(mon);
2229 break;
2230 case '(':
2231 next();
2232 n = expr_sum(mon);
2233 if (*pch != ')') {
2234 expr_error(mon, "')' expected");
2236 next();
2237 break;
2238 case '\'':
2239 pch++;
2240 if (*pch == '\0')
2241 expr_error(mon, "character constant expected");
2242 n = *pch;
2243 pch++;
2244 if (*pch != '\'')
2245 expr_error(mon, "missing terminating \' character");
2246 next();
2247 break;
2248 case '$':
2250 char buf[128], *q;
2251 target_long reg=0;
2253 pch++;
2254 q = buf;
2255 while ((*pch >= 'a' && *pch <= 'z') ||
2256 (*pch >= 'A' && *pch <= 'Z') ||
2257 (*pch >= '0' && *pch <= '9') ||
2258 *pch == '_' || *pch == '.') {
2259 if ((q - buf) < sizeof(buf) - 1)
2260 *q++ = *pch;
2261 pch++;
2263 while (qemu_isspace(*pch))
2264 pch++;
2265 *q = 0;
2266 ret = get_monitor_def(&reg, buf);
2267 if (ret == -1)
2268 expr_error(mon, "unknown register");
2269 else if (ret == -2)
2270 expr_error(mon, "no cpu defined");
2271 n = reg;
2273 break;
2274 case '\0':
2275 expr_error(mon, "unexpected end of expression");
2276 n = 0;
2277 break;
2278 default:
2279 #if TARGET_PHYS_ADDR_BITS > 32
2280 n = strtoull(pch, &p, 0);
2281 #else
2282 n = strtoul(pch, &p, 0);
2283 #endif
2284 if (pch == p) {
2285 expr_error(mon, "invalid char in expression");
2287 pch = p;
2288 while (qemu_isspace(*pch))
2289 pch++;
2290 break;
2292 return n;
2296 static int64_t expr_prod(Monitor *mon)
2298 int64_t val, val2;
2299 int op;
2301 val = expr_unary(mon);
2302 for(;;) {
2303 op = *pch;
2304 if (op != '*' && op != '/' && op != '%')
2305 break;
2306 next();
2307 val2 = expr_unary(mon);
2308 switch(op) {
2309 default:
2310 case '*':
2311 val *= val2;
2312 break;
2313 case '/':
2314 case '%':
2315 if (val2 == 0)
2316 expr_error(mon, "division by zero");
2317 if (op == '/')
2318 val /= val2;
2319 else
2320 val %= val2;
2321 break;
2324 return val;
2327 static int64_t expr_logic(Monitor *mon)
2329 int64_t val, val2;
2330 int op;
2332 val = expr_prod(mon);
2333 for(;;) {
2334 op = *pch;
2335 if (op != '&' && op != '|' && op != '^')
2336 break;
2337 next();
2338 val2 = expr_prod(mon);
2339 switch(op) {
2340 default:
2341 case '&':
2342 val &= val2;
2343 break;
2344 case '|':
2345 val |= val2;
2346 break;
2347 case '^':
2348 val ^= val2;
2349 break;
2352 return val;
2355 static int64_t expr_sum(Monitor *mon)
2357 int64_t val, val2;
2358 int op;
2360 val = expr_logic(mon);
2361 for(;;) {
2362 op = *pch;
2363 if (op != '+' && op != '-')
2364 break;
2365 next();
2366 val2 = expr_logic(mon);
2367 if (op == '+')
2368 val += val2;
2369 else
2370 val -= val2;
2372 return val;
2375 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2377 pch = *pp;
2378 if (setjmp(expr_env)) {
2379 *pp = pch;
2380 return -1;
2382 while (qemu_isspace(*pch))
2383 pch++;
2384 *pval = expr_sum(mon);
2385 *pp = pch;
2386 return 0;
2389 static int get_str(char *buf, int buf_size, const char **pp)
2391 const char *p;
2392 char *q;
2393 int c;
2395 q = buf;
2396 p = *pp;
2397 while (qemu_isspace(*p))
2398 p++;
2399 if (*p == '\0') {
2400 fail:
2401 *q = '\0';
2402 *pp = p;
2403 return -1;
2405 if (*p == '\"') {
2406 p++;
2407 while (*p != '\0' && *p != '\"') {
2408 if (*p == '\\') {
2409 p++;
2410 c = *p++;
2411 switch(c) {
2412 case 'n':
2413 c = '\n';
2414 break;
2415 case 'r':
2416 c = '\r';
2417 break;
2418 case '\\':
2419 case '\'':
2420 case '\"':
2421 break;
2422 default:
2423 qemu_printf("unsupported escape code: '\\%c'\n", c);
2424 goto fail;
2426 if ((q - buf) < buf_size - 1) {
2427 *q++ = c;
2429 } else {
2430 if ((q - buf) < buf_size - 1) {
2431 *q++ = *p;
2433 p++;
2436 if (*p != '\"') {
2437 qemu_printf("unterminated string\n");
2438 goto fail;
2440 p++;
2441 } else {
2442 while (*p != '\0' && !qemu_isspace(*p)) {
2443 if ((q - buf) < buf_size - 1) {
2444 *q++ = *p;
2446 p++;
2449 *q = '\0';
2450 *pp = p;
2451 return 0;
2455 * Store the command-name in cmdname, and return a pointer to
2456 * the remaining of the command string.
2458 static const char *get_command_name(const char *cmdline,
2459 char *cmdname, size_t nlen)
2461 size_t len;
2462 const char *p, *pstart;
2464 p = cmdline;
2465 while (qemu_isspace(*p))
2466 p++;
2467 if (*p == '\0')
2468 return NULL;
2469 pstart = p;
2470 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2471 p++;
2472 len = p - pstart;
2473 if (len > nlen - 1)
2474 len = nlen - 1;
2475 memcpy(cmdname, pstart, len);
2476 cmdname[len] = '\0';
2477 return p;
2480 static int default_fmt_format = 'x';
2481 static int default_fmt_size = 4;
2483 #define MAX_ARGS 16
2485 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2487 const char *p, *typestr;
2488 int c, nb_args, i, has_arg;
2489 const mon_cmd_t *cmd;
2490 char cmdname[256];
2491 char buf[1024];
2492 void *str_allocated[MAX_ARGS];
2493 void *args[MAX_ARGS];
2494 void (*handler_0)(Monitor *mon);
2495 void (*handler_1)(Monitor *mon, void *arg0);
2496 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2497 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2498 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2499 void *arg3);
2500 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2501 void *arg3, void *arg4);
2502 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2503 void *arg3, void *arg4, void *arg5);
2504 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2505 void *arg3, void *arg4, void *arg5, void *arg6);
2506 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2507 void *arg3, void *arg4, void *arg5, void *arg6,
2508 void *arg7);
2509 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2510 void *arg3, void *arg4, void *arg5, void *arg6,
2511 void *arg7, void *arg8);
2512 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2513 void *arg3, void *arg4, void *arg5, void *arg6,
2514 void *arg7, void *arg8, void *arg9);
2516 #ifdef DEBUG
2517 monitor_printf(mon, "command='%s'\n", cmdline);
2518 #endif
2520 /* extract the command name */
2521 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2522 if (!p)
2523 return;
2525 /* find the command */
2526 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2527 if (compare_cmd(cmdname, cmd->name))
2528 break;
2531 if (cmd->name == NULL) {
2532 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2533 return;
2536 for(i = 0; i < MAX_ARGS; i++)
2537 str_allocated[i] = NULL;
2539 /* parse the parameters */
2540 typestr = cmd->args_type;
2541 nb_args = 0;
2542 for(;;) {
2543 c = *typestr;
2544 if (c == '\0')
2545 break;
2546 typestr++;
2547 switch(c) {
2548 case 'F':
2549 case 'B':
2550 case 's':
2552 int ret;
2553 char *str;
2555 while (qemu_isspace(*p))
2556 p++;
2557 if (*typestr == '?') {
2558 typestr++;
2559 if (*p == '\0') {
2560 /* no optional string: NULL argument */
2561 str = NULL;
2562 goto add_str;
2565 ret = get_str(buf, sizeof(buf), &p);
2566 if (ret < 0) {
2567 switch(c) {
2568 case 'F':
2569 monitor_printf(mon, "%s: filename expected\n",
2570 cmdname);
2571 break;
2572 case 'B':
2573 monitor_printf(mon, "%s: block device name expected\n",
2574 cmdname);
2575 break;
2576 default:
2577 monitor_printf(mon, "%s: string expected\n", cmdname);
2578 break;
2580 goto fail;
2582 str = qemu_malloc(strlen(buf) + 1);
2583 pstrcpy(str, sizeof(buf), buf);
2584 str_allocated[nb_args] = str;
2585 add_str:
2586 if (nb_args >= MAX_ARGS) {
2587 error_args:
2588 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2589 goto fail;
2591 args[nb_args++] = str;
2593 break;
2594 case '/':
2596 int count, format, size;
2598 while (qemu_isspace(*p))
2599 p++;
2600 if (*p == '/') {
2601 /* format found */
2602 p++;
2603 count = 1;
2604 if (qemu_isdigit(*p)) {
2605 count = 0;
2606 while (qemu_isdigit(*p)) {
2607 count = count * 10 + (*p - '0');
2608 p++;
2611 size = -1;
2612 format = -1;
2613 for(;;) {
2614 switch(*p) {
2615 case 'o':
2616 case 'd':
2617 case 'u':
2618 case 'x':
2619 case 'i':
2620 case 'c':
2621 format = *p++;
2622 break;
2623 case 'b':
2624 size = 1;
2625 p++;
2626 break;
2627 case 'h':
2628 size = 2;
2629 p++;
2630 break;
2631 case 'w':
2632 size = 4;
2633 p++;
2634 break;
2635 case 'g':
2636 case 'L':
2637 size = 8;
2638 p++;
2639 break;
2640 default:
2641 goto next;
2644 next:
2645 if (*p != '\0' && !qemu_isspace(*p)) {
2646 monitor_printf(mon, "invalid char in format: '%c'\n",
2647 *p);
2648 goto fail;
2650 if (format < 0)
2651 format = default_fmt_format;
2652 if (format != 'i') {
2653 /* for 'i', not specifying a size gives -1 as size */
2654 if (size < 0)
2655 size = default_fmt_size;
2656 default_fmt_size = size;
2658 default_fmt_format = format;
2659 } else {
2660 count = 1;
2661 format = default_fmt_format;
2662 if (format != 'i') {
2663 size = default_fmt_size;
2664 } else {
2665 size = -1;
2668 if (nb_args + 3 > MAX_ARGS)
2669 goto error_args;
2670 args[nb_args++] = (void*)(long)count;
2671 args[nb_args++] = (void*)(long)format;
2672 args[nb_args++] = (void*)(long)size;
2674 break;
2675 case 'i':
2676 case 'l':
2678 int64_t val;
2680 while (qemu_isspace(*p))
2681 p++;
2682 if (*typestr == '?' || *typestr == '.') {
2683 if (*typestr == '?') {
2684 if (*p == '\0')
2685 has_arg = 0;
2686 else
2687 has_arg = 1;
2688 } else {
2689 if (*p == '.') {
2690 p++;
2691 while (qemu_isspace(*p))
2692 p++;
2693 has_arg = 1;
2694 } else {
2695 has_arg = 0;
2698 typestr++;
2699 if (nb_args >= MAX_ARGS)
2700 goto error_args;
2701 args[nb_args++] = (void *)(long)has_arg;
2702 if (!has_arg) {
2703 if (nb_args >= MAX_ARGS)
2704 goto error_args;
2705 val = -1;
2706 goto add_num;
2709 if (get_expr(mon, &val, &p))
2710 goto fail;
2711 add_num:
2712 if (c == 'i') {
2713 if (nb_args >= MAX_ARGS)
2714 goto error_args;
2715 args[nb_args++] = (void *)(long)val;
2716 } else {
2717 if ((nb_args + 1) >= MAX_ARGS)
2718 goto error_args;
2719 #if TARGET_PHYS_ADDR_BITS > 32
2720 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2721 #else
2722 args[nb_args++] = (void *)0;
2723 #endif
2724 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2727 break;
2728 case '-':
2730 int has_option;
2731 /* option */
2733 c = *typestr++;
2734 if (c == '\0')
2735 goto bad_type;
2736 while (qemu_isspace(*p))
2737 p++;
2738 has_option = 0;
2739 if (*p == '-') {
2740 p++;
2741 if (*p != c) {
2742 monitor_printf(mon, "%s: unsupported option -%c\n",
2743 cmdname, *p);
2744 goto fail;
2746 p++;
2747 has_option = 1;
2749 if (nb_args >= MAX_ARGS)
2750 goto error_args;
2751 args[nb_args++] = (void *)(long)has_option;
2753 break;
2754 default:
2755 bad_type:
2756 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2757 goto fail;
2760 /* check that all arguments were parsed */
2761 while (qemu_isspace(*p))
2762 p++;
2763 if (*p != '\0') {
2764 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2765 cmdname);
2766 goto fail;
2769 switch(nb_args) {
2770 case 0:
2771 handler_0 = cmd->handler;
2772 handler_0(mon);
2773 break;
2774 case 1:
2775 handler_1 = cmd->handler;
2776 handler_1(mon, args[0]);
2777 break;
2778 case 2:
2779 handler_2 = cmd->handler;
2780 handler_2(mon, args[0], args[1]);
2781 break;
2782 case 3:
2783 handler_3 = cmd->handler;
2784 handler_3(mon, args[0], args[1], args[2]);
2785 break;
2786 case 4:
2787 handler_4 = cmd->handler;
2788 handler_4(mon, args[0], args[1], args[2], args[3]);
2789 break;
2790 case 5:
2791 handler_5 = cmd->handler;
2792 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2793 break;
2794 case 6:
2795 handler_6 = cmd->handler;
2796 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2797 break;
2798 case 7:
2799 handler_7 = cmd->handler;
2800 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2801 args[6]);
2802 break;
2803 case 8:
2804 handler_8 = cmd->handler;
2805 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2806 args[6], args[7]);
2807 break;
2808 case 9:
2809 handler_9 = cmd->handler;
2810 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2811 args[6], args[7], args[8]);
2812 break;
2813 case 10:
2814 handler_10 = cmd->handler;
2815 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2816 args[6], args[7], args[8], args[9]);
2817 break;
2818 default:
2819 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2820 goto fail;
2822 fail:
2823 for(i = 0; i < MAX_ARGS; i++)
2824 qemu_free(str_allocated[i]);
2827 static void cmd_completion(const char *name, const char *list)
2829 const char *p, *pstart;
2830 char cmd[128];
2831 int len;
2833 p = list;
2834 for(;;) {
2835 pstart = p;
2836 p = strchr(p, '|');
2837 if (!p)
2838 p = pstart + strlen(pstart);
2839 len = p - pstart;
2840 if (len > sizeof(cmd) - 2)
2841 len = sizeof(cmd) - 2;
2842 memcpy(cmd, pstart, len);
2843 cmd[len] = '\0';
2844 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2845 readline_add_completion(cur_mon->rs, cmd);
2847 if (*p == '\0')
2848 break;
2849 p++;
2853 static void file_completion(const char *input)
2855 DIR *ffs;
2856 struct dirent *d;
2857 char path[1024];
2858 char file[1024], file_prefix[1024];
2859 int input_path_len;
2860 const char *p;
2862 p = strrchr(input, '/');
2863 if (!p) {
2864 input_path_len = 0;
2865 pstrcpy(file_prefix, sizeof(file_prefix), input);
2866 pstrcpy(path, sizeof(path), ".");
2867 } else {
2868 input_path_len = p - input + 1;
2869 memcpy(path, input, input_path_len);
2870 if (input_path_len > sizeof(path) - 1)
2871 input_path_len = sizeof(path) - 1;
2872 path[input_path_len] = '\0';
2873 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2875 #ifdef DEBUG_COMPLETION
2876 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2877 input, path, file_prefix);
2878 #endif
2879 ffs = opendir(path);
2880 if (!ffs)
2881 return;
2882 for(;;) {
2883 struct stat sb;
2884 d = readdir(ffs);
2885 if (!d)
2886 break;
2887 if (strstart(d->d_name, file_prefix, NULL)) {
2888 memcpy(file, input, input_path_len);
2889 if (input_path_len < sizeof(file))
2890 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2891 d->d_name);
2892 /* stat the file to find out if it's a directory.
2893 * In that case add a slash to speed up typing long paths
2895 stat(file, &sb);
2896 if(S_ISDIR(sb.st_mode))
2897 pstrcat(file, sizeof(file), "/");
2898 readline_add_completion(cur_mon->rs, file);
2901 closedir(ffs);
2904 static void block_completion_it(void *opaque, BlockDriverState *bs)
2906 const char *name = bdrv_get_device_name(bs);
2907 const char *input = opaque;
2909 if (input[0] == '\0' ||
2910 !strncmp(name, (char *)input, strlen(input))) {
2911 readline_add_completion(cur_mon->rs, name);
2915 /* NOTE: this parser is an approximate form of the real command parser */
2916 static void parse_cmdline(const char *cmdline,
2917 int *pnb_args, char **args)
2919 const char *p;
2920 int nb_args, ret;
2921 char buf[1024];
2923 p = cmdline;
2924 nb_args = 0;
2925 for(;;) {
2926 while (qemu_isspace(*p))
2927 p++;
2928 if (*p == '\0')
2929 break;
2930 if (nb_args >= MAX_ARGS)
2931 break;
2932 ret = get_str(buf, sizeof(buf), &p);
2933 args[nb_args] = qemu_strdup(buf);
2934 nb_args++;
2935 if (ret < 0)
2936 break;
2938 *pnb_args = nb_args;
2941 static void monitor_find_completion(const char *cmdline)
2943 const char *cmdname;
2944 char *args[MAX_ARGS];
2945 int nb_args, i, len;
2946 const char *ptype, *str;
2947 const mon_cmd_t *cmd;
2948 const KeyDef *key;
2950 parse_cmdline(cmdline, &nb_args, args);
2951 #ifdef DEBUG_COMPLETION
2952 for(i = 0; i < nb_args; i++) {
2953 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2955 #endif
2957 /* if the line ends with a space, it means we want to complete the
2958 next arg */
2959 len = strlen(cmdline);
2960 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2961 if (nb_args >= MAX_ARGS)
2962 return;
2963 args[nb_args++] = qemu_strdup("");
2965 if (nb_args <= 1) {
2966 /* command completion */
2967 if (nb_args == 0)
2968 cmdname = "";
2969 else
2970 cmdname = args[0];
2971 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2972 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2973 cmd_completion(cmdname, cmd->name);
2975 } else {
2976 /* find the command */
2977 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2978 if (compare_cmd(args[0], cmd->name))
2979 goto found;
2981 return;
2982 found:
2983 ptype = cmd->args_type;
2984 for(i = 0; i < nb_args - 2; i++) {
2985 if (*ptype != '\0') {
2986 ptype++;
2987 while (*ptype == '?')
2988 ptype++;
2991 str = args[nb_args - 1];
2992 switch(*ptype) {
2993 case 'F':
2994 /* file completion */
2995 readline_set_completion_index(cur_mon->rs, strlen(str));
2996 file_completion(str);
2997 break;
2998 case 'B':
2999 /* block device name completion */
3000 readline_set_completion_index(cur_mon->rs, strlen(str));
3001 bdrv_iterate(block_completion_it, (void *)str);
3002 break;
3003 case 's':
3004 /* XXX: more generic ? */
3005 if (!strcmp(cmd->name, "info")) {
3006 readline_set_completion_index(cur_mon->rs, strlen(str));
3007 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3008 cmd_completion(str, cmd->name);
3010 } else if (!strcmp(cmd->name, "sendkey")) {
3011 char *sep = strrchr(str, '-');
3012 if (sep)
3013 str = sep + 1;
3014 readline_set_completion_index(cur_mon->rs, strlen(str));
3015 for(key = key_defs; key->name != NULL; key++) {
3016 cmd_completion(str, key->name);
3018 } else if (!strcmp(cmd->name, "help|?")) {
3019 readline_set_completion_index(cur_mon->rs, strlen(str));
3020 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3021 cmd_completion(str, cmd->name);
3024 break;
3025 default:
3026 break;
3029 for(i = 0; i < nb_args; i++)
3030 qemu_free(args[i]);
3033 static int monitor_can_read(void *opaque)
3035 Monitor *mon = opaque;
3037 return (mon->suspend_cnt == 0) ? 128 : 0;
3040 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3042 Monitor *old_mon = cur_mon;
3043 int i;
3045 cur_mon = opaque;
3047 if (cur_mon->rs) {
3048 for (i = 0; i < size; i++)
3049 readline_handle_byte(cur_mon->rs, buf[i]);
3050 } else {
3051 if (size == 0 || buf[size - 1] != 0)
3052 monitor_printf(cur_mon, "corrupted command\n");
3053 else
3054 monitor_handle_command(cur_mon, (char *)buf);
3057 cur_mon = old_mon;
3060 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3062 monitor_suspend(mon);
3063 monitor_handle_command(mon, cmdline);
3064 monitor_resume(mon);
3067 int monitor_suspend(Monitor *mon)
3069 if (!mon->rs)
3070 return -ENOTTY;
3071 mon->suspend_cnt++;
3072 return 0;
3075 void monitor_resume(Monitor *mon)
3077 if (!mon->rs)
3078 return;
3079 if (--mon->suspend_cnt == 0)
3080 readline_show_prompt(mon->rs);
3083 static void monitor_event(void *opaque, int event)
3085 Monitor *mon = opaque;
3087 switch (event) {
3088 case CHR_EVENT_MUX_IN:
3089 readline_restart(mon->rs);
3090 monitor_resume(mon);
3091 monitor_flush(mon);
3092 break;
3094 case CHR_EVENT_MUX_OUT:
3095 if (mon->suspend_cnt == 0)
3096 monitor_printf(mon, "\n");
3097 monitor_flush(mon);
3098 monitor_suspend(mon);
3099 break;
3101 case CHR_EVENT_RESET:
3102 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3103 "information\n", QEMU_VERSION);
3104 if (mon->chr->focus == 0)
3105 readline_show_prompt(mon->rs);
3106 break;
3112 * Local variables:
3113 * c-indent-level: 4
3114 * c-basic-offset: 4
3115 * tab-width: 8
3116 * End:
3119 void monitor_init(CharDriverState *chr, int flags)
3121 static int is_first_init = 1;
3122 Monitor *mon;
3124 if (is_first_init) {
3125 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3126 is_first_init = 0;
3129 mon = qemu_mallocz(sizeof(*mon));
3131 mon->chr = chr;
3132 mon->flags = flags;
3133 if (mon->chr->focus != 0)
3134 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3135 if (flags & MONITOR_USE_READLINE) {
3136 mon->rs = readline_init(mon, monitor_find_completion);
3137 monitor_read_command(mon, 0);
3140 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3141 mon);
3143 LIST_INSERT_HEAD(&mon_list, mon, entry);
3144 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3145 cur_mon = mon;
3148 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3150 BlockDriverState *bs = opaque;
3151 int ret = 0;
3153 if (bdrv_set_key(bs, password) != 0) {
3154 monitor_printf(mon, "invalid password\n");
3155 ret = -EPERM;
3157 if (mon->password_completion_cb)
3158 mon->password_completion_cb(mon->password_opaque, ret);
3160 monitor_read_command(mon, 1);
3163 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3164 BlockDriverCompletionFunc *completion_cb,
3165 void *opaque)
3167 int err;
3169 if (!bdrv_key_required(bs)) {
3170 if (completion_cb)
3171 completion_cb(opaque, 0);
3172 return;
3175 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3176 bdrv_get_encrypted_filename(bs));
3178 mon->password_completion_cb = completion_cb;
3179 mon->password_opaque = opaque;
3181 err = monitor_read_password(mon, bdrv_password_cb, bs);
3183 if (err && completion_cb)
3184 completion_cb(opaque, err);