replace qemu_kvm_cpu_env
[qemu-kvm/fedora.git] / monitor.c
blob193f0b95ab0ca816b62bec1dccddcbd44bb34b0e
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 /* 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 #if defined(TARGET_I386)
1705 static void do_inject_mce(Monitor *mon,
1706 int cpu_index, int bank,
1707 unsigned status_hi, unsigned status_lo,
1708 unsigned mcg_status_hi, unsigned mcg_status_lo,
1709 unsigned addr_hi, unsigned addr_lo,
1710 unsigned misc_hi, unsigned misc_lo)
1712 CPUState *cenv;
1713 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1714 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1715 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1716 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1718 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1719 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1720 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1721 break;
1724 #endif
1726 static const mon_cmd_t mon_cmds[] = {
1727 #include "qemu-monitor.h"
1728 { NULL, NULL, },
1731 /* Please update qemu-monitor.hx when adding or changing commands */
1732 static const mon_cmd_t info_cmds[] = {
1733 { "version", "", do_info_version,
1734 "", "show the version of QEMU" },
1735 { "network", "", do_info_network,
1736 "", "show the network state" },
1737 { "chardev", "", qemu_chr_info,
1738 "", "show the character devices" },
1739 { "block", "", bdrv_info,
1740 "", "show the block devices" },
1741 { "blockstats", "", bdrv_info_stats,
1742 "", "show block device statistics" },
1743 { "registers", "", do_info_registers,
1744 "", "show the cpu registers" },
1745 { "cpus", "", do_info_cpus,
1746 "", "show infos for each CPU" },
1747 { "history", "", do_info_history,
1748 "", "show the command line history", },
1749 { "irq", "", irq_info,
1750 "", "show the interrupts statistics (if available)", },
1751 { "pic", "", pic_info,
1752 "", "show i8259 (PIC) state", },
1753 { "pci", "", pci_info,
1754 "", "show PCI info", },
1755 #if defined(TARGET_I386) || defined(TARGET_SH4)
1756 { "tlb", "", tlb_info,
1757 "", "show virtual to physical memory mappings", },
1758 #endif
1759 #if defined(TARGET_I386)
1760 { "mem", "", mem_info,
1761 "", "show the active virtual memory mappings", },
1762 { "hpet", "", do_info_hpet,
1763 "", "show state of HPET", },
1764 #endif
1765 { "jit", "", do_info_jit,
1766 "", "show dynamic compiler info", },
1767 { "kqemu", "", do_info_kqemu,
1768 "", "show KQEMU information", },
1769 { "kvm", "", do_info_kvm,
1770 "", "show KVM information", },
1771 { "numa", "", do_info_numa,
1772 "", "show NUMA information", },
1773 { "usb", "", usb_info,
1774 "", "show guest USB devices", },
1775 { "usbhost", "", usb_host_info,
1776 "", "show host USB devices", },
1777 { "profile", "", do_info_profile,
1778 "", "show profiling information", },
1779 { "capture", "", do_info_capture,
1780 "", "show capture information" },
1781 { "snapshots", "", do_info_snapshots,
1782 "", "show the currently saved VM snapshots" },
1783 { "status", "", do_info_status,
1784 "", "show the current VM status (running|paused)" },
1785 { "pcmcia", "", pcmcia_info,
1786 "", "show guest PCMCIA status" },
1787 { "mice", "", do_info_mice,
1788 "", "show which guest mouse is receiving events" },
1789 { "vnc", "", do_info_vnc,
1790 "", "show the vnc server status"},
1791 { "name", "", do_info_name,
1792 "", "show the current VM name" },
1793 { "uuid", "", do_info_uuid,
1794 "", "show the current VM UUID" },
1795 #if defined(TARGET_PPC)
1796 { "cpustats", "", do_info_cpu_stats,
1797 "", "show CPU statistics", },
1798 #endif
1799 #if defined(CONFIG_SLIRP)
1800 { "usernet", "", do_info_usernet,
1801 "", "show user network stack connection states", },
1802 #endif
1803 { "migrate", "", do_info_migrate, "", "show migration status" },
1804 { "balloon", "", do_info_balloon,
1805 "", "show balloon information" },
1806 { "qtree", "", do_info_qtree,
1807 "", "show device tree" },
1808 { NULL, NULL, },
1811 /*******************************************************************/
1813 static const char *pch;
1814 static jmp_buf expr_env;
1816 #define MD_TLONG 0
1817 #define MD_I32 1
1819 typedef struct MonitorDef {
1820 const char *name;
1821 int offset;
1822 target_long (*get_value)(const struct MonitorDef *md, int val);
1823 int type;
1824 } MonitorDef;
1826 #if defined(TARGET_I386)
1827 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1829 CPUState *env = mon_get_cpu();
1830 if (!env)
1831 return 0;
1832 return env->eip + env->segs[R_CS].base;
1834 #endif
1836 #if defined(TARGET_PPC)
1837 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1839 CPUState *env = mon_get_cpu();
1840 unsigned int u;
1841 int i;
1843 if (!env)
1844 return 0;
1846 u = 0;
1847 for (i = 0; i < 8; i++)
1848 u |= env->crf[i] << (32 - (4 * i));
1850 return u;
1853 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1855 CPUState *env = mon_get_cpu();
1856 if (!env)
1857 return 0;
1858 return env->msr;
1861 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1863 CPUState *env = mon_get_cpu();
1864 if (!env)
1865 return 0;
1866 return env->xer;
1869 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1871 CPUState *env = mon_get_cpu();
1872 if (!env)
1873 return 0;
1874 return cpu_ppc_load_decr(env);
1877 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1879 CPUState *env = mon_get_cpu();
1880 if (!env)
1881 return 0;
1882 return cpu_ppc_load_tbu(env);
1885 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1887 CPUState *env = mon_get_cpu();
1888 if (!env)
1889 return 0;
1890 return cpu_ppc_load_tbl(env);
1892 #endif
1894 #if defined(TARGET_SPARC)
1895 #ifndef TARGET_SPARC64
1896 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1898 CPUState *env = mon_get_cpu();
1899 if (!env)
1900 return 0;
1901 return GET_PSR(env);
1903 #endif
1905 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1907 CPUState *env = mon_get_cpu();
1908 if (!env)
1909 return 0;
1910 return env->regwptr[val];
1912 #endif
1914 static const MonitorDef monitor_defs[] = {
1915 #ifdef TARGET_I386
1917 #define SEG(name, seg) \
1918 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1919 { name ".base", offsetof(CPUState, segs[seg].base) },\
1920 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1922 { "eax", offsetof(CPUState, regs[0]) },
1923 { "ecx", offsetof(CPUState, regs[1]) },
1924 { "edx", offsetof(CPUState, regs[2]) },
1925 { "ebx", offsetof(CPUState, regs[3]) },
1926 { "esp|sp", offsetof(CPUState, regs[4]) },
1927 { "ebp|fp", offsetof(CPUState, regs[5]) },
1928 { "esi", offsetof(CPUState, regs[6]) },
1929 { "edi", offsetof(CPUState, regs[7]) },
1930 #ifdef TARGET_X86_64
1931 { "r8", offsetof(CPUState, regs[8]) },
1932 { "r9", offsetof(CPUState, regs[9]) },
1933 { "r10", offsetof(CPUState, regs[10]) },
1934 { "r11", offsetof(CPUState, regs[11]) },
1935 { "r12", offsetof(CPUState, regs[12]) },
1936 { "r13", offsetof(CPUState, regs[13]) },
1937 { "r14", offsetof(CPUState, regs[14]) },
1938 { "r15", offsetof(CPUState, regs[15]) },
1939 #endif
1940 { "eflags", offsetof(CPUState, eflags) },
1941 { "eip", offsetof(CPUState, eip) },
1942 SEG("cs", R_CS)
1943 SEG("ds", R_DS)
1944 SEG("es", R_ES)
1945 SEG("ss", R_SS)
1946 SEG("fs", R_FS)
1947 SEG("gs", R_GS)
1948 { "pc", 0, monitor_get_pc, },
1949 #elif defined(TARGET_PPC)
1950 /* General purpose registers */
1951 { "r0", offsetof(CPUState, gpr[0]) },
1952 { "r1", offsetof(CPUState, gpr[1]) },
1953 { "r2", offsetof(CPUState, gpr[2]) },
1954 { "r3", offsetof(CPUState, gpr[3]) },
1955 { "r4", offsetof(CPUState, gpr[4]) },
1956 { "r5", offsetof(CPUState, gpr[5]) },
1957 { "r6", offsetof(CPUState, gpr[6]) },
1958 { "r7", offsetof(CPUState, gpr[7]) },
1959 { "r8", offsetof(CPUState, gpr[8]) },
1960 { "r9", offsetof(CPUState, gpr[9]) },
1961 { "r10", offsetof(CPUState, gpr[10]) },
1962 { "r11", offsetof(CPUState, gpr[11]) },
1963 { "r12", offsetof(CPUState, gpr[12]) },
1964 { "r13", offsetof(CPUState, gpr[13]) },
1965 { "r14", offsetof(CPUState, gpr[14]) },
1966 { "r15", offsetof(CPUState, gpr[15]) },
1967 { "r16", offsetof(CPUState, gpr[16]) },
1968 { "r17", offsetof(CPUState, gpr[17]) },
1969 { "r18", offsetof(CPUState, gpr[18]) },
1970 { "r19", offsetof(CPUState, gpr[19]) },
1971 { "r20", offsetof(CPUState, gpr[20]) },
1972 { "r21", offsetof(CPUState, gpr[21]) },
1973 { "r22", offsetof(CPUState, gpr[22]) },
1974 { "r23", offsetof(CPUState, gpr[23]) },
1975 { "r24", offsetof(CPUState, gpr[24]) },
1976 { "r25", offsetof(CPUState, gpr[25]) },
1977 { "r26", offsetof(CPUState, gpr[26]) },
1978 { "r27", offsetof(CPUState, gpr[27]) },
1979 { "r28", offsetof(CPUState, gpr[28]) },
1980 { "r29", offsetof(CPUState, gpr[29]) },
1981 { "r30", offsetof(CPUState, gpr[30]) },
1982 { "r31", offsetof(CPUState, gpr[31]) },
1983 /* Floating point registers */
1984 { "f0", offsetof(CPUState, fpr[0]) },
1985 { "f1", offsetof(CPUState, fpr[1]) },
1986 { "f2", offsetof(CPUState, fpr[2]) },
1987 { "f3", offsetof(CPUState, fpr[3]) },
1988 { "f4", offsetof(CPUState, fpr[4]) },
1989 { "f5", offsetof(CPUState, fpr[5]) },
1990 { "f6", offsetof(CPUState, fpr[6]) },
1991 { "f7", offsetof(CPUState, fpr[7]) },
1992 { "f8", offsetof(CPUState, fpr[8]) },
1993 { "f9", offsetof(CPUState, fpr[9]) },
1994 { "f10", offsetof(CPUState, fpr[10]) },
1995 { "f11", offsetof(CPUState, fpr[11]) },
1996 { "f12", offsetof(CPUState, fpr[12]) },
1997 { "f13", offsetof(CPUState, fpr[13]) },
1998 { "f14", offsetof(CPUState, fpr[14]) },
1999 { "f15", offsetof(CPUState, fpr[15]) },
2000 { "f16", offsetof(CPUState, fpr[16]) },
2001 { "f17", offsetof(CPUState, fpr[17]) },
2002 { "f18", offsetof(CPUState, fpr[18]) },
2003 { "f19", offsetof(CPUState, fpr[19]) },
2004 { "f20", offsetof(CPUState, fpr[20]) },
2005 { "f21", offsetof(CPUState, fpr[21]) },
2006 { "f22", offsetof(CPUState, fpr[22]) },
2007 { "f23", offsetof(CPUState, fpr[23]) },
2008 { "f24", offsetof(CPUState, fpr[24]) },
2009 { "f25", offsetof(CPUState, fpr[25]) },
2010 { "f26", offsetof(CPUState, fpr[26]) },
2011 { "f27", offsetof(CPUState, fpr[27]) },
2012 { "f28", offsetof(CPUState, fpr[28]) },
2013 { "f29", offsetof(CPUState, fpr[29]) },
2014 { "f30", offsetof(CPUState, fpr[30]) },
2015 { "f31", offsetof(CPUState, fpr[31]) },
2016 { "fpscr", offsetof(CPUState, fpscr) },
2017 /* Next instruction pointer */
2018 { "nip|pc", offsetof(CPUState, nip) },
2019 { "lr", offsetof(CPUState, lr) },
2020 { "ctr", offsetof(CPUState, ctr) },
2021 { "decr", 0, &monitor_get_decr, },
2022 { "ccr", 0, &monitor_get_ccr, },
2023 /* Machine state register */
2024 { "msr", 0, &monitor_get_msr, },
2025 { "xer", 0, &monitor_get_xer, },
2026 { "tbu", 0, &monitor_get_tbu, },
2027 { "tbl", 0, &monitor_get_tbl, },
2028 #if defined(TARGET_PPC64)
2029 /* Address space register */
2030 { "asr", offsetof(CPUState, asr) },
2031 #endif
2032 /* Segment registers */
2033 { "sdr1", offsetof(CPUState, sdr1) },
2034 { "sr0", offsetof(CPUState, sr[0]) },
2035 { "sr1", offsetof(CPUState, sr[1]) },
2036 { "sr2", offsetof(CPUState, sr[2]) },
2037 { "sr3", offsetof(CPUState, sr[3]) },
2038 { "sr4", offsetof(CPUState, sr[4]) },
2039 { "sr5", offsetof(CPUState, sr[5]) },
2040 { "sr6", offsetof(CPUState, sr[6]) },
2041 { "sr7", offsetof(CPUState, sr[7]) },
2042 { "sr8", offsetof(CPUState, sr[8]) },
2043 { "sr9", offsetof(CPUState, sr[9]) },
2044 { "sr10", offsetof(CPUState, sr[10]) },
2045 { "sr11", offsetof(CPUState, sr[11]) },
2046 { "sr12", offsetof(CPUState, sr[12]) },
2047 { "sr13", offsetof(CPUState, sr[13]) },
2048 { "sr14", offsetof(CPUState, sr[14]) },
2049 { "sr15", offsetof(CPUState, sr[15]) },
2050 /* Too lazy to put BATs and SPRs ... */
2051 #elif defined(TARGET_SPARC)
2052 { "g0", offsetof(CPUState, gregs[0]) },
2053 { "g1", offsetof(CPUState, gregs[1]) },
2054 { "g2", offsetof(CPUState, gregs[2]) },
2055 { "g3", offsetof(CPUState, gregs[3]) },
2056 { "g4", offsetof(CPUState, gregs[4]) },
2057 { "g5", offsetof(CPUState, gregs[5]) },
2058 { "g6", offsetof(CPUState, gregs[6]) },
2059 { "g7", offsetof(CPUState, gregs[7]) },
2060 { "o0", 0, monitor_get_reg },
2061 { "o1", 1, monitor_get_reg },
2062 { "o2", 2, monitor_get_reg },
2063 { "o3", 3, monitor_get_reg },
2064 { "o4", 4, monitor_get_reg },
2065 { "o5", 5, monitor_get_reg },
2066 { "o6", 6, monitor_get_reg },
2067 { "o7", 7, monitor_get_reg },
2068 { "l0", 8, monitor_get_reg },
2069 { "l1", 9, monitor_get_reg },
2070 { "l2", 10, monitor_get_reg },
2071 { "l3", 11, monitor_get_reg },
2072 { "l4", 12, monitor_get_reg },
2073 { "l5", 13, monitor_get_reg },
2074 { "l6", 14, monitor_get_reg },
2075 { "l7", 15, monitor_get_reg },
2076 { "i0", 16, monitor_get_reg },
2077 { "i1", 17, monitor_get_reg },
2078 { "i2", 18, monitor_get_reg },
2079 { "i3", 19, monitor_get_reg },
2080 { "i4", 20, monitor_get_reg },
2081 { "i5", 21, monitor_get_reg },
2082 { "i6", 22, monitor_get_reg },
2083 { "i7", 23, monitor_get_reg },
2084 { "pc", offsetof(CPUState, pc) },
2085 { "npc", offsetof(CPUState, npc) },
2086 { "y", offsetof(CPUState, y) },
2087 #ifndef TARGET_SPARC64
2088 { "psr", 0, &monitor_get_psr, },
2089 { "wim", offsetof(CPUState, wim) },
2090 #endif
2091 { "tbr", offsetof(CPUState, tbr) },
2092 { "fsr", offsetof(CPUState, fsr) },
2093 { "f0", offsetof(CPUState, fpr[0]) },
2094 { "f1", offsetof(CPUState, fpr[1]) },
2095 { "f2", offsetof(CPUState, fpr[2]) },
2096 { "f3", offsetof(CPUState, fpr[3]) },
2097 { "f4", offsetof(CPUState, fpr[4]) },
2098 { "f5", offsetof(CPUState, fpr[5]) },
2099 { "f6", offsetof(CPUState, fpr[6]) },
2100 { "f7", offsetof(CPUState, fpr[7]) },
2101 { "f8", offsetof(CPUState, fpr[8]) },
2102 { "f9", offsetof(CPUState, fpr[9]) },
2103 { "f10", offsetof(CPUState, fpr[10]) },
2104 { "f11", offsetof(CPUState, fpr[11]) },
2105 { "f12", offsetof(CPUState, fpr[12]) },
2106 { "f13", offsetof(CPUState, fpr[13]) },
2107 { "f14", offsetof(CPUState, fpr[14]) },
2108 { "f15", offsetof(CPUState, fpr[15]) },
2109 { "f16", offsetof(CPUState, fpr[16]) },
2110 { "f17", offsetof(CPUState, fpr[17]) },
2111 { "f18", offsetof(CPUState, fpr[18]) },
2112 { "f19", offsetof(CPUState, fpr[19]) },
2113 { "f20", offsetof(CPUState, fpr[20]) },
2114 { "f21", offsetof(CPUState, fpr[21]) },
2115 { "f22", offsetof(CPUState, fpr[22]) },
2116 { "f23", offsetof(CPUState, fpr[23]) },
2117 { "f24", offsetof(CPUState, fpr[24]) },
2118 { "f25", offsetof(CPUState, fpr[25]) },
2119 { "f26", offsetof(CPUState, fpr[26]) },
2120 { "f27", offsetof(CPUState, fpr[27]) },
2121 { "f28", offsetof(CPUState, fpr[28]) },
2122 { "f29", offsetof(CPUState, fpr[29]) },
2123 { "f30", offsetof(CPUState, fpr[30]) },
2124 { "f31", offsetof(CPUState, fpr[31]) },
2125 #ifdef TARGET_SPARC64
2126 { "f32", offsetof(CPUState, fpr[32]) },
2127 { "f34", offsetof(CPUState, fpr[34]) },
2128 { "f36", offsetof(CPUState, fpr[36]) },
2129 { "f38", offsetof(CPUState, fpr[38]) },
2130 { "f40", offsetof(CPUState, fpr[40]) },
2131 { "f42", offsetof(CPUState, fpr[42]) },
2132 { "f44", offsetof(CPUState, fpr[44]) },
2133 { "f46", offsetof(CPUState, fpr[46]) },
2134 { "f48", offsetof(CPUState, fpr[48]) },
2135 { "f50", offsetof(CPUState, fpr[50]) },
2136 { "f52", offsetof(CPUState, fpr[52]) },
2137 { "f54", offsetof(CPUState, fpr[54]) },
2138 { "f56", offsetof(CPUState, fpr[56]) },
2139 { "f58", offsetof(CPUState, fpr[58]) },
2140 { "f60", offsetof(CPUState, fpr[60]) },
2141 { "f62", offsetof(CPUState, fpr[62]) },
2142 { "asi", offsetof(CPUState, asi) },
2143 { "pstate", offsetof(CPUState, pstate) },
2144 { "cansave", offsetof(CPUState, cansave) },
2145 { "canrestore", offsetof(CPUState, canrestore) },
2146 { "otherwin", offsetof(CPUState, otherwin) },
2147 { "wstate", offsetof(CPUState, wstate) },
2148 { "cleanwin", offsetof(CPUState, cleanwin) },
2149 { "fprs", offsetof(CPUState, fprs) },
2150 #endif
2151 #endif
2152 { NULL },
2155 static void expr_error(Monitor *mon, const char *msg)
2157 monitor_printf(mon, "%s\n", msg);
2158 longjmp(expr_env, 1);
2161 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2162 static int get_monitor_def(target_long *pval, const char *name)
2164 const MonitorDef *md;
2165 void *ptr;
2167 for(md = monitor_defs; md->name != NULL; md++) {
2168 if (compare_cmd(name, md->name)) {
2169 if (md->get_value) {
2170 *pval = md->get_value(md, md->offset);
2171 } else {
2172 CPUState *env = mon_get_cpu();
2173 if (!env)
2174 return -2;
2175 ptr = (uint8_t *)env + md->offset;
2176 switch(md->type) {
2177 case MD_I32:
2178 *pval = *(int32_t *)ptr;
2179 break;
2180 case MD_TLONG:
2181 *pval = *(target_long *)ptr;
2182 break;
2183 default:
2184 *pval = 0;
2185 break;
2188 return 0;
2191 return -1;
2194 static void next(void)
2196 if (pch != '\0') {
2197 pch++;
2198 while (qemu_isspace(*pch))
2199 pch++;
2203 static int64_t expr_sum(Monitor *mon);
2205 static int64_t expr_unary(Monitor *mon)
2207 int64_t n;
2208 char *p;
2209 int ret;
2211 switch(*pch) {
2212 case '+':
2213 next();
2214 n = expr_unary(mon);
2215 break;
2216 case '-':
2217 next();
2218 n = -expr_unary(mon);
2219 break;
2220 case '~':
2221 next();
2222 n = ~expr_unary(mon);
2223 break;
2224 case '(':
2225 next();
2226 n = expr_sum(mon);
2227 if (*pch != ')') {
2228 expr_error(mon, "')' expected");
2230 next();
2231 break;
2232 case '\'':
2233 pch++;
2234 if (*pch == '\0')
2235 expr_error(mon, "character constant expected");
2236 n = *pch;
2237 pch++;
2238 if (*pch != '\'')
2239 expr_error(mon, "missing terminating \' character");
2240 next();
2241 break;
2242 case '$':
2244 char buf[128], *q;
2245 target_long reg=0;
2247 pch++;
2248 q = buf;
2249 while ((*pch >= 'a' && *pch <= 'z') ||
2250 (*pch >= 'A' && *pch <= 'Z') ||
2251 (*pch >= '0' && *pch <= '9') ||
2252 *pch == '_' || *pch == '.') {
2253 if ((q - buf) < sizeof(buf) - 1)
2254 *q++ = *pch;
2255 pch++;
2257 while (qemu_isspace(*pch))
2258 pch++;
2259 *q = 0;
2260 ret = get_monitor_def(&reg, buf);
2261 if (ret == -1)
2262 expr_error(mon, "unknown register");
2263 else if (ret == -2)
2264 expr_error(mon, "no cpu defined");
2265 n = reg;
2267 break;
2268 case '\0':
2269 expr_error(mon, "unexpected end of expression");
2270 n = 0;
2271 break;
2272 default:
2273 #if TARGET_PHYS_ADDR_BITS > 32
2274 n = strtoull(pch, &p, 0);
2275 #else
2276 n = strtoul(pch, &p, 0);
2277 #endif
2278 if (pch == p) {
2279 expr_error(mon, "invalid char in expression");
2281 pch = p;
2282 while (qemu_isspace(*pch))
2283 pch++;
2284 break;
2286 return n;
2290 static int64_t expr_prod(Monitor *mon)
2292 int64_t val, val2;
2293 int op;
2295 val = expr_unary(mon);
2296 for(;;) {
2297 op = *pch;
2298 if (op != '*' && op != '/' && op != '%')
2299 break;
2300 next();
2301 val2 = expr_unary(mon);
2302 switch(op) {
2303 default:
2304 case '*':
2305 val *= val2;
2306 break;
2307 case '/':
2308 case '%':
2309 if (val2 == 0)
2310 expr_error(mon, "division by zero");
2311 if (op == '/')
2312 val /= val2;
2313 else
2314 val %= val2;
2315 break;
2318 return val;
2321 static int64_t expr_logic(Monitor *mon)
2323 int64_t val, val2;
2324 int op;
2326 val = expr_prod(mon);
2327 for(;;) {
2328 op = *pch;
2329 if (op != '&' && op != '|' && op != '^')
2330 break;
2331 next();
2332 val2 = expr_prod(mon);
2333 switch(op) {
2334 default:
2335 case '&':
2336 val &= val2;
2337 break;
2338 case '|':
2339 val |= val2;
2340 break;
2341 case '^':
2342 val ^= val2;
2343 break;
2346 return val;
2349 static int64_t expr_sum(Monitor *mon)
2351 int64_t val, val2;
2352 int op;
2354 val = expr_logic(mon);
2355 for(;;) {
2356 op = *pch;
2357 if (op != '+' && op != '-')
2358 break;
2359 next();
2360 val2 = expr_logic(mon);
2361 if (op == '+')
2362 val += val2;
2363 else
2364 val -= val2;
2366 return val;
2369 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2371 pch = *pp;
2372 if (setjmp(expr_env)) {
2373 *pp = pch;
2374 return -1;
2376 while (qemu_isspace(*pch))
2377 pch++;
2378 *pval = expr_sum(mon);
2379 *pp = pch;
2380 return 0;
2383 static int get_str(char *buf, int buf_size, const char **pp)
2385 const char *p;
2386 char *q;
2387 int c;
2389 q = buf;
2390 p = *pp;
2391 while (qemu_isspace(*p))
2392 p++;
2393 if (*p == '\0') {
2394 fail:
2395 *q = '\0';
2396 *pp = p;
2397 return -1;
2399 if (*p == '\"') {
2400 p++;
2401 while (*p != '\0' && *p != '\"') {
2402 if (*p == '\\') {
2403 p++;
2404 c = *p++;
2405 switch(c) {
2406 case 'n':
2407 c = '\n';
2408 break;
2409 case 'r':
2410 c = '\r';
2411 break;
2412 case '\\':
2413 case '\'':
2414 case '\"':
2415 break;
2416 default:
2417 qemu_printf("unsupported escape code: '\\%c'\n", c);
2418 goto fail;
2420 if ((q - buf) < buf_size - 1) {
2421 *q++ = c;
2423 } else {
2424 if ((q - buf) < buf_size - 1) {
2425 *q++ = *p;
2427 p++;
2430 if (*p != '\"') {
2431 qemu_printf("unterminated string\n");
2432 goto fail;
2434 p++;
2435 } else {
2436 while (*p != '\0' && !qemu_isspace(*p)) {
2437 if ((q - buf) < buf_size - 1) {
2438 *q++ = *p;
2440 p++;
2443 *q = '\0';
2444 *pp = p;
2445 return 0;
2449 * Store the command-name in cmdname, and return a pointer to
2450 * the remaining of the command string.
2452 static const char *get_command_name(const char *cmdline,
2453 char *cmdname, size_t nlen)
2455 size_t len;
2456 const char *p, *pstart;
2458 p = cmdline;
2459 while (qemu_isspace(*p))
2460 p++;
2461 if (*p == '\0')
2462 return NULL;
2463 pstart = p;
2464 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2465 p++;
2466 len = p - pstart;
2467 if (len > nlen - 1)
2468 len = nlen - 1;
2469 memcpy(cmdname, pstart, len);
2470 cmdname[len] = '\0';
2471 return p;
2474 static int default_fmt_format = 'x';
2475 static int default_fmt_size = 4;
2477 #define MAX_ARGS 16
2479 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2481 const char *p, *typestr;
2482 int c, nb_args, i, has_arg;
2483 const mon_cmd_t *cmd;
2484 char cmdname[256];
2485 char buf[1024];
2486 void *str_allocated[MAX_ARGS];
2487 void *args[MAX_ARGS];
2488 void (*handler_0)(Monitor *mon);
2489 void (*handler_1)(Monitor *mon, void *arg0);
2490 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2491 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2492 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2493 void *arg3);
2494 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2495 void *arg3, void *arg4);
2496 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2497 void *arg3, void *arg4, void *arg5);
2498 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2499 void *arg3, void *arg4, void *arg5, void *arg6);
2500 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2501 void *arg3, void *arg4, void *arg5, void *arg6,
2502 void *arg7);
2503 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2504 void *arg3, void *arg4, void *arg5, void *arg6,
2505 void *arg7, void *arg8);
2506 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2507 void *arg3, void *arg4, void *arg5, void *arg6,
2508 void *arg7, void *arg8, void *arg9);
2510 #ifdef DEBUG
2511 monitor_printf(mon, "command='%s'\n", cmdline);
2512 #endif
2514 /* extract the command name */
2515 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2516 if (!p)
2517 return;
2519 /* find the command */
2520 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2521 if (compare_cmd(cmdname, cmd->name))
2522 break;
2525 if (cmd->name == NULL) {
2526 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2527 return;
2530 for(i = 0; i < MAX_ARGS; i++)
2531 str_allocated[i] = NULL;
2533 /* parse the parameters */
2534 typestr = cmd->args_type;
2535 nb_args = 0;
2536 for(;;) {
2537 c = *typestr;
2538 if (c == '\0')
2539 break;
2540 typestr++;
2541 switch(c) {
2542 case 'F':
2543 case 'B':
2544 case 's':
2546 int ret;
2547 char *str;
2549 while (qemu_isspace(*p))
2550 p++;
2551 if (*typestr == '?') {
2552 typestr++;
2553 if (*p == '\0') {
2554 /* no optional string: NULL argument */
2555 str = NULL;
2556 goto add_str;
2559 ret = get_str(buf, sizeof(buf), &p);
2560 if (ret < 0) {
2561 switch(c) {
2562 case 'F':
2563 monitor_printf(mon, "%s: filename expected\n",
2564 cmdname);
2565 break;
2566 case 'B':
2567 monitor_printf(mon, "%s: block device name expected\n",
2568 cmdname);
2569 break;
2570 default:
2571 monitor_printf(mon, "%s: string expected\n", cmdname);
2572 break;
2574 goto fail;
2576 str = qemu_malloc(strlen(buf) + 1);
2577 pstrcpy(str, sizeof(buf), buf);
2578 str_allocated[nb_args] = str;
2579 add_str:
2580 if (nb_args >= MAX_ARGS) {
2581 error_args:
2582 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2583 goto fail;
2585 args[nb_args++] = str;
2587 break;
2588 case '/':
2590 int count, format, size;
2592 while (qemu_isspace(*p))
2593 p++;
2594 if (*p == '/') {
2595 /* format found */
2596 p++;
2597 count = 1;
2598 if (qemu_isdigit(*p)) {
2599 count = 0;
2600 while (qemu_isdigit(*p)) {
2601 count = count * 10 + (*p - '0');
2602 p++;
2605 size = -1;
2606 format = -1;
2607 for(;;) {
2608 switch(*p) {
2609 case 'o':
2610 case 'd':
2611 case 'u':
2612 case 'x':
2613 case 'i':
2614 case 'c':
2615 format = *p++;
2616 break;
2617 case 'b':
2618 size = 1;
2619 p++;
2620 break;
2621 case 'h':
2622 size = 2;
2623 p++;
2624 break;
2625 case 'w':
2626 size = 4;
2627 p++;
2628 break;
2629 case 'g':
2630 case 'L':
2631 size = 8;
2632 p++;
2633 break;
2634 default:
2635 goto next;
2638 next:
2639 if (*p != '\0' && !qemu_isspace(*p)) {
2640 monitor_printf(mon, "invalid char in format: '%c'\n",
2641 *p);
2642 goto fail;
2644 if (format < 0)
2645 format = default_fmt_format;
2646 if (format != 'i') {
2647 /* for 'i', not specifying a size gives -1 as size */
2648 if (size < 0)
2649 size = default_fmt_size;
2650 default_fmt_size = size;
2652 default_fmt_format = format;
2653 } else {
2654 count = 1;
2655 format = default_fmt_format;
2656 if (format != 'i') {
2657 size = default_fmt_size;
2658 } else {
2659 size = -1;
2662 if (nb_args + 3 > MAX_ARGS)
2663 goto error_args;
2664 args[nb_args++] = (void*)(long)count;
2665 args[nb_args++] = (void*)(long)format;
2666 args[nb_args++] = (void*)(long)size;
2668 break;
2669 case 'i':
2670 case 'l':
2672 int64_t val;
2674 while (qemu_isspace(*p))
2675 p++;
2676 if (*typestr == '?' || *typestr == '.') {
2677 if (*typestr == '?') {
2678 if (*p == '\0')
2679 has_arg = 0;
2680 else
2681 has_arg = 1;
2682 } else {
2683 if (*p == '.') {
2684 p++;
2685 while (qemu_isspace(*p))
2686 p++;
2687 has_arg = 1;
2688 } else {
2689 has_arg = 0;
2692 typestr++;
2693 if (nb_args >= MAX_ARGS)
2694 goto error_args;
2695 args[nb_args++] = (void *)(long)has_arg;
2696 if (!has_arg) {
2697 if (nb_args >= MAX_ARGS)
2698 goto error_args;
2699 val = -1;
2700 goto add_num;
2703 if (get_expr(mon, &val, &p))
2704 goto fail;
2705 add_num:
2706 if (c == 'i') {
2707 if (nb_args >= MAX_ARGS)
2708 goto error_args;
2709 args[nb_args++] = (void *)(long)val;
2710 } else {
2711 if ((nb_args + 1) >= MAX_ARGS)
2712 goto error_args;
2713 #if TARGET_PHYS_ADDR_BITS > 32
2714 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2715 #else
2716 args[nb_args++] = (void *)0;
2717 #endif
2718 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2721 break;
2722 case '-':
2724 int has_option;
2725 /* option */
2727 c = *typestr++;
2728 if (c == '\0')
2729 goto bad_type;
2730 while (qemu_isspace(*p))
2731 p++;
2732 has_option = 0;
2733 if (*p == '-') {
2734 p++;
2735 if (*p != c) {
2736 monitor_printf(mon, "%s: unsupported option -%c\n",
2737 cmdname, *p);
2738 goto fail;
2740 p++;
2741 has_option = 1;
2743 if (nb_args >= MAX_ARGS)
2744 goto error_args;
2745 args[nb_args++] = (void *)(long)has_option;
2747 break;
2748 default:
2749 bad_type:
2750 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2751 goto fail;
2754 /* check that all arguments were parsed */
2755 while (qemu_isspace(*p))
2756 p++;
2757 if (*p != '\0') {
2758 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2759 cmdname);
2760 goto fail;
2763 switch(nb_args) {
2764 case 0:
2765 handler_0 = cmd->handler;
2766 handler_0(mon);
2767 break;
2768 case 1:
2769 handler_1 = cmd->handler;
2770 handler_1(mon, args[0]);
2771 break;
2772 case 2:
2773 handler_2 = cmd->handler;
2774 handler_2(mon, args[0], args[1]);
2775 break;
2776 case 3:
2777 handler_3 = cmd->handler;
2778 handler_3(mon, args[0], args[1], args[2]);
2779 break;
2780 case 4:
2781 handler_4 = cmd->handler;
2782 handler_4(mon, args[0], args[1], args[2], args[3]);
2783 break;
2784 case 5:
2785 handler_5 = cmd->handler;
2786 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2787 break;
2788 case 6:
2789 handler_6 = cmd->handler;
2790 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2791 break;
2792 case 7:
2793 handler_7 = cmd->handler;
2794 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2795 args[6]);
2796 break;
2797 case 8:
2798 handler_8 = cmd->handler;
2799 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2800 args[6], args[7]);
2801 break;
2802 case 9:
2803 handler_9 = cmd->handler;
2804 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2805 args[6], args[7], args[8]);
2806 break;
2807 case 10:
2808 handler_10 = cmd->handler;
2809 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2810 args[6], args[7], args[8], args[9]);
2811 break;
2812 default:
2813 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2814 goto fail;
2816 fail:
2817 for(i = 0; i < MAX_ARGS; i++)
2818 qemu_free(str_allocated[i]);
2821 static void cmd_completion(const char *name, const char *list)
2823 const char *p, *pstart;
2824 char cmd[128];
2825 int len;
2827 p = list;
2828 for(;;) {
2829 pstart = p;
2830 p = strchr(p, '|');
2831 if (!p)
2832 p = pstart + strlen(pstart);
2833 len = p - pstart;
2834 if (len > sizeof(cmd) - 2)
2835 len = sizeof(cmd) - 2;
2836 memcpy(cmd, pstart, len);
2837 cmd[len] = '\0';
2838 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2839 readline_add_completion(cur_mon->rs, cmd);
2841 if (*p == '\0')
2842 break;
2843 p++;
2847 static void file_completion(const char *input)
2849 DIR *ffs;
2850 struct dirent *d;
2851 char path[1024];
2852 char file[1024], file_prefix[1024];
2853 int input_path_len;
2854 const char *p;
2856 p = strrchr(input, '/');
2857 if (!p) {
2858 input_path_len = 0;
2859 pstrcpy(file_prefix, sizeof(file_prefix), input);
2860 pstrcpy(path, sizeof(path), ".");
2861 } else {
2862 input_path_len = p - input + 1;
2863 memcpy(path, input, input_path_len);
2864 if (input_path_len > sizeof(path) - 1)
2865 input_path_len = sizeof(path) - 1;
2866 path[input_path_len] = '\0';
2867 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2869 #ifdef DEBUG_COMPLETION
2870 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2871 input, path, file_prefix);
2872 #endif
2873 ffs = opendir(path);
2874 if (!ffs)
2875 return;
2876 for(;;) {
2877 struct stat sb;
2878 d = readdir(ffs);
2879 if (!d)
2880 break;
2881 if (strstart(d->d_name, file_prefix, NULL)) {
2882 memcpy(file, input, input_path_len);
2883 if (input_path_len < sizeof(file))
2884 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2885 d->d_name);
2886 /* stat the file to find out if it's a directory.
2887 * In that case add a slash to speed up typing long paths
2889 stat(file, &sb);
2890 if(S_ISDIR(sb.st_mode))
2891 pstrcat(file, sizeof(file), "/");
2892 readline_add_completion(cur_mon->rs, file);
2895 closedir(ffs);
2898 static void block_completion_it(void *opaque, BlockDriverState *bs)
2900 const char *name = bdrv_get_device_name(bs);
2901 const char *input = opaque;
2903 if (input[0] == '\0' ||
2904 !strncmp(name, (char *)input, strlen(input))) {
2905 readline_add_completion(cur_mon->rs, name);
2909 /* NOTE: this parser is an approximate form of the real command parser */
2910 static void parse_cmdline(const char *cmdline,
2911 int *pnb_args, char **args)
2913 const char *p;
2914 int nb_args, ret;
2915 char buf[1024];
2917 p = cmdline;
2918 nb_args = 0;
2919 for(;;) {
2920 while (qemu_isspace(*p))
2921 p++;
2922 if (*p == '\0')
2923 break;
2924 if (nb_args >= MAX_ARGS)
2925 break;
2926 ret = get_str(buf, sizeof(buf), &p);
2927 args[nb_args] = qemu_strdup(buf);
2928 nb_args++;
2929 if (ret < 0)
2930 break;
2932 *pnb_args = nb_args;
2935 static void monitor_find_completion(const char *cmdline)
2937 const char *cmdname;
2938 char *args[MAX_ARGS];
2939 int nb_args, i, len;
2940 const char *ptype, *str;
2941 const mon_cmd_t *cmd;
2942 const KeyDef *key;
2944 parse_cmdline(cmdline, &nb_args, args);
2945 #ifdef DEBUG_COMPLETION
2946 for(i = 0; i < nb_args; i++) {
2947 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2949 #endif
2951 /* if the line ends with a space, it means we want to complete the
2952 next arg */
2953 len = strlen(cmdline);
2954 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2955 if (nb_args >= MAX_ARGS)
2956 return;
2957 args[nb_args++] = qemu_strdup("");
2959 if (nb_args <= 1) {
2960 /* command completion */
2961 if (nb_args == 0)
2962 cmdname = "";
2963 else
2964 cmdname = args[0];
2965 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2966 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2967 cmd_completion(cmdname, cmd->name);
2969 } else {
2970 /* find the command */
2971 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2972 if (compare_cmd(args[0], cmd->name))
2973 goto found;
2975 return;
2976 found:
2977 ptype = cmd->args_type;
2978 for(i = 0; i < nb_args - 2; i++) {
2979 if (*ptype != '\0') {
2980 ptype++;
2981 while (*ptype == '?')
2982 ptype++;
2985 str = args[nb_args - 1];
2986 switch(*ptype) {
2987 case 'F':
2988 /* file completion */
2989 readline_set_completion_index(cur_mon->rs, strlen(str));
2990 file_completion(str);
2991 break;
2992 case 'B':
2993 /* block device name completion */
2994 readline_set_completion_index(cur_mon->rs, strlen(str));
2995 bdrv_iterate(block_completion_it, (void *)str);
2996 break;
2997 case 's':
2998 /* XXX: more generic ? */
2999 if (!strcmp(cmd->name, "info")) {
3000 readline_set_completion_index(cur_mon->rs, strlen(str));
3001 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3002 cmd_completion(str, cmd->name);
3004 } else if (!strcmp(cmd->name, "sendkey")) {
3005 char *sep = strrchr(str, '-');
3006 if (sep)
3007 str = sep + 1;
3008 readline_set_completion_index(cur_mon->rs, strlen(str));
3009 for(key = key_defs; key->name != NULL; key++) {
3010 cmd_completion(str, key->name);
3012 } else if (!strcmp(cmd->name, "help|?")) {
3013 readline_set_completion_index(cur_mon->rs, strlen(str));
3014 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3015 cmd_completion(str, cmd->name);
3018 break;
3019 default:
3020 break;
3023 for(i = 0; i < nb_args; i++)
3024 qemu_free(args[i]);
3027 static int monitor_can_read(void *opaque)
3029 Monitor *mon = opaque;
3031 return (mon->suspend_cnt == 0) ? 128 : 0;
3034 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3036 Monitor *old_mon = cur_mon;
3037 int i;
3039 cur_mon = opaque;
3041 if (cur_mon->rs) {
3042 for (i = 0; i < size; i++)
3043 readline_handle_byte(cur_mon->rs, buf[i]);
3044 } else {
3045 if (size == 0 || buf[size - 1] != 0)
3046 monitor_printf(cur_mon, "corrupted command\n");
3047 else
3048 monitor_handle_command(cur_mon, (char *)buf);
3051 cur_mon = old_mon;
3054 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3056 monitor_suspend(mon);
3057 monitor_handle_command(mon, cmdline);
3058 monitor_resume(mon);
3061 int monitor_suspend(Monitor *mon)
3063 if (!mon->rs)
3064 return -ENOTTY;
3065 mon->suspend_cnt++;
3066 return 0;
3069 void monitor_resume(Monitor *mon)
3071 if (!mon->rs)
3072 return;
3073 if (--mon->suspend_cnt == 0)
3074 readline_show_prompt(mon->rs);
3077 static void monitor_event(void *opaque, int event)
3079 Monitor *mon = opaque;
3081 switch (event) {
3082 case CHR_EVENT_MUX_IN:
3083 readline_restart(mon->rs);
3084 monitor_resume(mon);
3085 monitor_flush(mon);
3086 break;
3088 case CHR_EVENT_MUX_OUT:
3089 if (mon->suspend_cnt == 0)
3090 monitor_printf(mon, "\n");
3091 monitor_flush(mon);
3092 monitor_suspend(mon);
3093 break;
3095 case CHR_EVENT_RESET:
3096 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3097 "information\n", QEMU_VERSION);
3098 if (mon->chr->focus == 0)
3099 readline_show_prompt(mon->rs);
3100 break;
3106 * Local variables:
3107 * c-indent-level: 4
3108 * c-basic-offset: 4
3109 * tab-width: 8
3110 * End:
3113 void monitor_init(CharDriverState *chr, int flags)
3115 static int is_first_init = 1;
3116 Monitor *mon;
3118 if (is_first_init) {
3119 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3120 is_first_init = 0;
3123 mon = qemu_mallocz(sizeof(*mon));
3125 mon->chr = chr;
3126 mon->flags = flags;
3127 if (mon->chr->focus != 0)
3128 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3129 if (flags & MONITOR_USE_READLINE) {
3130 mon->rs = readline_init(mon, monitor_find_completion);
3131 monitor_read_command(mon, 0);
3134 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3135 mon);
3137 LIST_INSERT_HEAD(&mon_list, mon, entry);
3138 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3139 cur_mon = mon;
3142 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3144 BlockDriverState *bs = opaque;
3145 int ret = 0;
3147 if (bdrv_set_key(bs, password) != 0) {
3148 monitor_printf(mon, "invalid password\n");
3149 ret = -EPERM;
3151 if (mon->password_completion_cb)
3152 mon->password_completion_cb(mon->password_opaque, ret);
3154 monitor_read_command(mon, 1);
3157 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3158 BlockDriverCompletionFunc *completion_cb,
3159 void *opaque)
3161 int err;
3163 if (!bdrv_key_required(bs)) {
3164 if (completion_cb)
3165 completion_cb(opaque, 0);
3166 return;
3169 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3170 bdrv_get_encrypted_filename(bs));
3172 mon->password_completion_cb = completion_cb;
3173 mon->password_opaque = opaque;
3175 err = monitor_read_password(mon, bdrv_password_cb, bs);
3177 if (err && completion_cb)
3178 completion_cb(opaque, err);