remove unneded opaque.
[qemu-kvm/amd-iommu.git] / monitor.c
blob7f0f5a952fe5f2d873962b43e5f8a6650ec564a2
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 "hw/loader.h"
33 #include "gdbstub.h"
34 #include "net.h"
35 #include "qemu-char.h"
36 #include "sysemu.h"
37 #include "monitor.h"
38 #include "readline.h"
39 #include "console.h"
40 #include "block.h"
41 #include "audio/audio.h"
42 #include "disas.h"
43 #include "balloon.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
46 #include "kvm.h"
47 #include "acl.h"
48 #include "qint.h"
49 #include "qdict.h"
50 #include "qstring.h"
51 #include "exec-all.h"
53 #include "qemu-kvm.h"
55 //#define DEBUG
56 //#define DEBUG_COMPLETION
59 * Supported types:
61 * 'F' filename
62 * 'B' block device name
63 * 's' string (accept optional quote)
64 * 'i' 32 bit integer
65 * 'l' target long (32 or 64 bit)
66 * '/' optional gdb-like print format (like "/10x")
68 * '?' optional type (for all types, except '/')
69 * '.' other form of optional type (for 'i' and 'l')
70 * '-' optional parameter (eg. '-f')
74 typedef struct mon_cmd_t {
75 const char *name;
76 const char *args_type;
77 void *handler;
78 const char *params;
79 const char *help;
80 } mon_cmd_t;
82 /* file descriptors passed via SCM_RIGHTS */
83 typedef struct mon_fd_t mon_fd_t;
84 struct mon_fd_t {
85 char *name;
86 int fd;
87 QLIST_ENTRY(mon_fd_t) next;
90 struct Monitor {
91 CharDriverState *chr;
92 int mux_out;
93 int reset_seen;
94 int flags;
95 int suspend_cnt;
96 uint8_t outbuf[1024];
97 int outbuf_index;
98 ReadLineState *rs;
99 CPUState *mon_cpu;
100 BlockDriverCompletionFunc *password_completion_cb;
101 void *password_opaque;
102 QLIST_HEAD(,mon_fd_t) fds;
103 QLIST_ENTRY(Monitor) entry;
106 static QLIST_HEAD(mon_list, Monitor) mon_list;
108 static const mon_cmd_t mon_cmds[];
109 static const mon_cmd_t info_cmds[];
111 Monitor *cur_mon = NULL;
113 static void monitor_command_cb(Monitor *mon, const char *cmdline,
114 void *opaque);
116 static void monitor_read_command(Monitor *mon, int show_prompt)
118 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
119 if (show_prompt)
120 readline_show_prompt(mon->rs);
123 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
124 void *opaque)
126 if (mon->rs) {
127 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
128 /* prompt is printed on return from the command handler */
129 return 0;
130 } else {
131 monitor_printf(mon, "terminal does not support password prompting\n");
132 return -ENOTTY;
136 void monitor_flush(Monitor *mon)
138 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
139 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
140 mon->outbuf_index = 0;
144 /* flush at every end of line or if the buffer is full */
145 static void monitor_puts(Monitor *mon, const char *str)
147 char c;
149 if (!mon)
150 return;
152 for(;;) {
153 c = *str++;
154 if (c == '\0')
155 break;
156 if (c == '\n')
157 mon->outbuf[mon->outbuf_index++] = '\r';
158 mon->outbuf[mon->outbuf_index++] = c;
159 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
160 || c == '\n')
161 monitor_flush(mon);
165 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
167 char buf[4096];
168 vsnprintf(buf, sizeof(buf), fmt, ap);
169 monitor_puts(mon, buf);
172 void monitor_printf(Monitor *mon, const char *fmt, ...)
174 va_list ap;
175 va_start(ap, fmt);
176 monitor_vprintf(mon, fmt, ap);
177 va_end(ap);
180 void monitor_print_filename(Monitor *mon, const char *filename)
182 int i;
184 for (i = 0; filename[i]; i++) {
185 switch (filename[i]) {
186 case ' ':
187 case '"':
188 case '\\':
189 monitor_printf(mon, "\\%c", filename[i]);
190 break;
191 case '\t':
192 monitor_printf(mon, "\\t");
193 break;
194 case '\r':
195 monitor_printf(mon, "\\r");
196 break;
197 case '\n':
198 monitor_printf(mon, "\\n");
199 break;
200 default:
201 monitor_printf(mon, "%c", filename[i]);
202 break;
207 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
209 va_list ap;
210 va_start(ap, fmt);
211 monitor_vprintf((Monitor *)stream, fmt, ap);
212 va_end(ap);
213 return 0;
216 static int compare_cmd(const char *name, const char *list)
218 const char *p, *pstart;
219 int len;
220 len = strlen(name);
221 p = list;
222 for(;;) {
223 pstart = p;
224 p = strchr(p, '|');
225 if (!p)
226 p = pstart + strlen(pstart);
227 if ((p - pstart) == len && !memcmp(pstart, name, len))
228 return 1;
229 if (*p == '\0')
230 break;
231 p++;
233 return 0;
236 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
237 const char *prefix, const char *name)
239 const mon_cmd_t *cmd;
241 for(cmd = cmds; cmd->name != NULL; cmd++) {
242 if (!name || !strcmp(name, cmd->name))
243 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
244 cmd->params, cmd->help);
248 static void help_cmd(Monitor *mon, const char *name)
250 if (name && !strcmp(name, "info")) {
251 help_cmd_dump(mon, info_cmds, "info ", NULL);
252 } else {
253 help_cmd_dump(mon, mon_cmds, "", name);
254 if (name && !strcmp(name, "log")) {
255 const CPULogItem *item;
256 monitor_printf(mon, "Log items (comma separated):\n");
257 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
258 for(item = cpu_log_items; item->mask != 0; item++) {
259 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
265 static void do_help_cmd(Monitor *mon, const QDict *qdict)
267 help_cmd(mon, qdict_get_try_str(qdict, "name"));
270 static void do_commit(Monitor *mon, const QDict *qdict)
272 int all_devices;
273 DriveInfo *dinfo;
274 const char *device = qdict_get_str(qdict, "device");
276 all_devices = !strcmp(device, "all");
277 QTAILQ_FOREACH(dinfo, &drives, next) {
278 if (!all_devices)
279 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
280 continue;
281 bdrv_commit(dinfo->bdrv);
285 static void do_info(Monitor *mon, const QDict *qdict)
287 const mon_cmd_t *cmd;
288 const char *item = qdict_get_try_str(qdict, "item");
289 void (*handler)(Monitor *);
291 if (!item)
292 goto help;
293 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
294 if (compare_cmd(item, cmd->name))
295 goto found;
297 help:
298 help_cmd(mon, "info");
299 return;
300 found:
301 handler = cmd->handler;
302 handler(mon);
305 static void do_info_version(Monitor *mon)
307 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
310 static void do_info_name(Monitor *mon)
312 if (qemu_name)
313 monitor_printf(mon, "%s\n", qemu_name);
316 #if defined(TARGET_I386)
317 static void do_info_hpet(Monitor *mon)
319 monitor_printf(mon, "HPET is %s by QEMU\n",
320 (no_hpet) ? "disabled" : "enabled");
322 #endif
324 static void do_info_uuid(Monitor *mon)
326 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
327 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
328 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
329 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
330 qemu_uuid[14], qemu_uuid[15]);
333 /* get the current CPU defined by the user */
334 static int mon_set_cpu(int cpu_index)
336 CPUState *env;
338 for(env = first_cpu; env != NULL; env = env->next_cpu) {
339 if (env->cpu_index == cpu_index) {
340 cur_mon->mon_cpu = env;
341 return 0;
344 return -1;
347 static CPUState *mon_get_cpu(void)
349 if (!cur_mon->mon_cpu) {
350 mon_set_cpu(0);
352 cpu_synchronize_state(cur_mon->mon_cpu);
353 return cur_mon->mon_cpu;
356 static void do_info_registers(Monitor *mon)
358 CPUState *env;
359 env = mon_get_cpu();
360 if (!env)
361 return;
362 #ifdef TARGET_I386
363 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
364 X86_DUMP_FPU);
365 #else
366 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
368 #endif
371 static void do_info_cpus(Monitor *mon)
373 CPUState *env;
375 /* just to set the default cpu if not already done */
376 mon_get_cpu();
378 for(env = first_cpu; env != NULL; env = env->next_cpu) {
379 cpu_synchronize_state(env);
380 monitor_printf(mon, "%c CPU #%d:",
381 (env == mon->mon_cpu) ? '*' : ' ',
382 env->cpu_index);
383 #if defined(TARGET_I386)
384 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
385 env->eip + env->segs[R_CS].base);
386 #elif defined(TARGET_PPC)
387 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
388 #elif defined(TARGET_SPARC)
389 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
390 env->pc, env->npc);
391 #elif defined(TARGET_MIPS)
392 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
393 #endif
394 if (env->halted)
395 monitor_printf(mon, " (halted)");
396 monitor_printf(mon," thread_id=%d", env->thread_id);
397 monitor_printf(mon, "\n");
401 static void do_cpu_set(Monitor *mon, const QDict *qdict)
403 int index = qdict_get_int(qdict, "index");
404 if (mon_set_cpu(index) < 0)
405 monitor_printf(mon, "Invalid CPU index\n");
408 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
410 int state;
412 if (!strcmp(status, "online"))
413 state = 1;
414 else if (!strcmp(status, "offline"))
415 state = 0;
416 else {
417 monitor_printf(mon, "invalid status: %s\n", status);
418 return;
420 #if defined(TARGET_I386) || defined(TARGET_X86_64)
421 qemu_system_cpu_hot_add(value, state);
422 #endif
425 static void do_info_jit(Monitor *mon)
427 dump_exec_info((FILE *)mon, monitor_fprintf);
430 static void do_info_history(Monitor *mon)
432 int i;
433 const char *str;
435 if (!mon->rs)
436 return;
437 i = 0;
438 for(;;) {
439 str = readline_get_history(mon->rs, i);
440 if (!str)
441 break;
442 monitor_printf(mon, "%d: '%s'\n", i, str);
443 i++;
447 #if defined(TARGET_PPC)
448 /* XXX: not implemented in other targets */
449 static void do_info_cpu_stats(Monitor *mon)
451 CPUState *env;
453 env = mon_get_cpu();
454 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
456 #endif
458 static void do_quit(Monitor *mon, const QDict *qdict)
460 exit(0);
463 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
465 if (bdrv_is_inserted(bs)) {
466 if (!force) {
467 if (!bdrv_is_removable(bs)) {
468 monitor_printf(mon, "device is not removable\n");
469 return -1;
471 if (bdrv_is_locked(bs)) {
472 monitor_printf(mon, "device is locked\n");
473 return -1;
476 bdrv_close(bs);
478 return 0;
481 static void do_eject(Monitor *mon, const QDict *qdict)
483 BlockDriverState *bs;
484 int force = qdict_get_int(qdict, "force");
485 const char *filename = qdict_get_str(qdict, "filename");
487 bs = bdrv_find(filename);
488 if (!bs) {
489 monitor_printf(mon, "device not found\n");
490 return;
492 eject_device(mon, bs, force);
495 static void do_change_block(Monitor *mon, const char *device,
496 const char *filename, const char *fmt)
498 BlockDriverState *bs;
499 BlockDriver *drv = NULL;
501 bs = bdrv_find(device);
502 if (!bs) {
503 monitor_printf(mon, "device not found\n");
504 return;
506 if (fmt) {
507 drv = bdrv_find_format(fmt);
508 if (!drv) {
509 monitor_printf(mon, "invalid format %s\n", fmt);
510 return;
513 if (eject_device(mon, bs, 0) < 0)
514 return;
515 bdrv_open2(bs, filename, 0, drv);
516 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
519 static void change_vnc_password_cb(Monitor *mon, const char *password,
520 void *opaque)
522 if (vnc_display_password(NULL, password) < 0)
523 monitor_printf(mon, "could not set VNC server password\n");
525 monitor_read_command(mon, 1);
528 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
530 if (strcmp(target, "passwd") == 0 ||
531 strcmp(target, "password") == 0) {
532 if (arg) {
533 char password[9];
534 strncpy(password, arg, sizeof(password));
535 password[sizeof(password) - 1] = '\0';
536 change_vnc_password_cb(mon, password, NULL);
537 } else {
538 monitor_read_password(mon, change_vnc_password_cb, NULL);
540 } else {
541 if (vnc_display_open(NULL, target) < 0)
542 monitor_printf(mon, "could not start VNC server on %s\n", target);
546 static void do_change(Monitor *mon, const QDict *qdict)
548 const char *device = qdict_get_str(qdict, "device");
549 const char *target = qdict_get_str(qdict, "target");
550 const char *arg = qdict_get_try_str(qdict, "arg");
551 if (strcmp(device, "vnc") == 0) {
552 do_change_vnc(mon, target, arg);
553 } else {
554 do_change_block(mon, device, target, arg);
558 static void do_screen_dump(Monitor *mon, const QDict *qdict)
560 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
563 static void do_logfile(Monitor *mon, const QDict *qdict)
565 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
568 static void do_log(Monitor *mon, const QDict *qdict)
570 int mask;
571 const char *items = qdict_get_str(qdict, "items");
573 if (!strcmp(items, "none")) {
574 mask = 0;
575 } else {
576 mask = cpu_str_to_log_mask(items);
577 if (!mask) {
578 help_cmd(mon, "log");
579 return;
582 cpu_set_log(mask);
585 static void do_singlestep(Monitor *mon, const QDict *qdict)
587 const char *option = qdict_get_try_str(qdict, "option");
588 if (!option || !strcmp(option, "on")) {
589 singlestep = 1;
590 } else if (!strcmp(option, "off")) {
591 singlestep = 0;
592 } else {
593 monitor_printf(mon, "unexpected option %s\n", option);
597 static void do_stop(Monitor *mon, const QDict *qdict)
599 vm_stop(EXCP_INTERRUPT);
602 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
604 struct bdrv_iterate_context {
605 Monitor *mon;
606 int err;
609 static void do_cont(Monitor *mon, const QDict *qdict)
611 struct bdrv_iterate_context context = { mon, 0 };
613 bdrv_iterate(encrypted_bdrv_it, &context);
614 /* only resume the vm if all keys are set and valid */
615 if (!context.err)
616 vm_start();
619 static void bdrv_key_cb(void *opaque, int err)
621 Monitor *mon = opaque;
623 /* another key was set successfully, retry to continue */
624 if (!err)
625 do_cont(mon, NULL);
628 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
630 struct bdrv_iterate_context *context = opaque;
632 if (!context->err && bdrv_key_required(bs)) {
633 context->err = -EBUSY;
634 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
635 context->mon);
639 static void do_gdbserver(Monitor *mon, const QDict *qdict)
641 const char *device = qdict_get_try_str(qdict, "device");
642 if (!device)
643 device = "tcp::" DEFAULT_GDBSTUB_PORT;
644 if (gdbserver_start(device) < 0) {
645 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
646 device);
647 } else if (strcmp(device, "none") == 0) {
648 monitor_printf(mon, "Disabled gdbserver\n");
649 } else {
650 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
651 device);
655 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
657 const char *action = qdict_get_str(qdict, "action");
658 if (select_watchdog_action(action) == -1) {
659 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
663 static void monitor_printc(Monitor *mon, int c)
665 monitor_printf(mon, "'");
666 switch(c) {
667 case '\'':
668 monitor_printf(mon, "\\'");
669 break;
670 case '\\':
671 monitor_printf(mon, "\\\\");
672 break;
673 case '\n':
674 monitor_printf(mon, "\\n");
675 break;
676 case '\r':
677 monitor_printf(mon, "\\r");
678 break;
679 default:
680 if (c >= 32 && c <= 126) {
681 monitor_printf(mon, "%c", c);
682 } else {
683 monitor_printf(mon, "\\x%02x", c);
685 break;
687 monitor_printf(mon, "'");
690 static void memory_dump(Monitor *mon, int count, int format, int wsize,
691 target_phys_addr_t addr, int is_physical)
693 CPUState *env;
694 int nb_per_line, l, line_size, i, max_digits, len;
695 uint8_t buf[16];
696 uint64_t v;
698 if (format == 'i') {
699 int flags;
700 flags = 0;
701 env = mon_get_cpu();
702 if (!env && !is_physical)
703 return;
704 #ifdef TARGET_I386
705 if (wsize == 2) {
706 flags = 1;
707 } else if (wsize == 4) {
708 flags = 0;
709 } else {
710 /* as default we use the current CS size */
711 flags = 0;
712 if (env) {
713 #ifdef TARGET_X86_64
714 if ((env->efer & MSR_EFER_LMA) &&
715 (env->segs[R_CS].flags & DESC_L_MASK))
716 flags = 2;
717 else
718 #endif
719 if (!(env->segs[R_CS].flags & DESC_B_MASK))
720 flags = 1;
723 #endif
724 monitor_disas(mon, env, addr, count, is_physical, flags);
725 return;
728 len = wsize * count;
729 if (wsize == 1)
730 line_size = 8;
731 else
732 line_size = 16;
733 nb_per_line = line_size / wsize;
734 max_digits = 0;
736 switch(format) {
737 case 'o':
738 max_digits = (wsize * 8 + 2) / 3;
739 break;
740 default:
741 case 'x':
742 max_digits = (wsize * 8) / 4;
743 break;
744 case 'u':
745 case 'd':
746 max_digits = (wsize * 8 * 10 + 32) / 33;
747 break;
748 case 'c':
749 wsize = 1;
750 break;
753 while (len > 0) {
754 if (is_physical)
755 monitor_printf(mon, TARGET_FMT_plx ":", addr);
756 else
757 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
758 l = len;
759 if (l > line_size)
760 l = line_size;
761 if (is_physical) {
762 cpu_physical_memory_rw(addr, buf, l, 0);
763 } else {
764 env = mon_get_cpu();
765 if (!env)
766 break;
767 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
768 monitor_printf(mon, " Cannot access memory\n");
769 break;
772 i = 0;
773 while (i < l) {
774 switch(wsize) {
775 default:
776 case 1:
777 v = ldub_raw(buf + i);
778 break;
779 case 2:
780 v = lduw_raw(buf + i);
781 break;
782 case 4:
783 v = (uint32_t)ldl_raw(buf + i);
784 break;
785 case 8:
786 v = ldq_raw(buf + i);
787 break;
789 monitor_printf(mon, " ");
790 switch(format) {
791 case 'o':
792 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
793 break;
794 case 'x':
795 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
796 break;
797 case 'u':
798 monitor_printf(mon, "%*" PRIu64, max_digits, v);
799 break;
800 case 'd':
801 monitor_printf(mon, "%*" PRId64, max_digits, v);
802 break;
803 case 'c':
804 monitor_printc(mon, v);
805 break;
807 i += wsize;
809 monitor_printf(mon, "\n");
810 addr += l;
811 len -= l;
815 static void do_memory_dump(Monitor *mon, const QDict *qdict)
817 int count = qdict_get_int(qdict, "count");
818 int format = qdict_get_int(qdict, "format");
819 int size = qdict_get_int(qdict, "size");
820 target_long addr = qdict_get_int(qdict, "addr");
822 memory_dump(mon, count, format, size, addr, 0);
825 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
827 int count = qdict_get_int(qdict, "count");
828 int format = qdict_get_int(qdict, "format");
829 int size = qdict_get_int(qdict, "size");
830 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
832 memory_dump(mon, count, format, size, addr, 1);
835 static void do_print(Monitor *mon, const QDict *qdict)
837 int format = qdict_get_int(qdict, "format");
838 target_phys_addr_t val = qdict_get_int(qdict, "val");
840 #if TARGET_PHYS_ADDR_BITS == 32
841 switch(format) {
842 case 'o':
843 monitor_printf(mon, "%#o", val);
844 break;
845 case 'x':
846 monitor_printf(mon, "%#x", val);
847 break;
848 case 'u':
849 monitor_printf(mon, "%u", val);
850 break;
851 default:
852 case 'd':
853 monitor_printf(mon, "%d", val);
854 break;
855 case 'c':
856 monitor_printc(mon, val);
857 break;
859 #else
860 switch(format) {
861 case 'o':
862 monitor_printf(mon, "%#" PRIo64, val);
863 break;
864 case 'x':
865 monitor_printf(mon, "%#" PRIx64, val);
866 break;
867 case 'u':
868 monitor_printf(mon, "%" PRIu64, val);
869 break;
870 default:
871 case 'd':
872 monitor_printf(mon, "%" PRId64, val);
873 break;
874 case 'c':
875 monitor_printc(mon, val);
876 break;
878 #endif
879 monitor_printf(mon, "\n");
882 static void do_memory_save(Monitor *mon, const QDict *qdict)
884 FILE *f;
885 uint32_t size = qdict_get_int(qdict, "size");
886 const char *filename = qdict_get_str(qdict, "filename");
887 target_long addr = qdict_get_int(qdict, "val");
888 uint32_t l;
889 CPUState *env;
890 uint8_t buf[1024];
892 env = mon_get_cpu();
893 if (!env)
894 return;
896 f = fopen(filename, "wb");
897 if (!f) {
898 monitor_printf(mon, "could not open '%s'\n", filename);
899 return;
901 while (size != 0) {
902 l = sizeof(buf);
903 if (l > size)
904 l = size;
905 cpu_memory_rw_debug(env, addr, buf, l, 0);
906 fwrite(buf, 1, l, f);
907 addr += l;
908 size -= l;
910 fclose(f);
913 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
915 FILE *f;
916 uint32_t l;
917 uint8_t buf[1024];
918 uint32_t size = qdict_get_int(qdict, "size");
919 const char *filename = qdict_get_str(qdict, "filename");
920 target_phys_addr_t addr = qdict_get_int(qdict, "val");
922 f = fopen(filename, "wb");
923 if (!f) {
924 monitor_printf(mon, "could not open '%s'\n", filename);
925 return;
927 while (size != 0) {
928 l = sizeof(buf);
929 if (l > size)
930 l = size;
931 cpu_physical_memory_rw(addr, buf, l, 0);
932 fwrite(buf, 1, l, f);
933 fflush(f);
934 addr += l;
935 size -= l;
937 fclose(f);
940 static void do_sum(Monitor *mon, const QDict *qdict)
942 uint32_t addr;
943 uint8_t buf[1];
944 uint16_t sum;
945 uint32_t start = qdict_get_int(qdict, "start");
946 uint32_t size = qdict_get_int(qdict, "size");
948 sum = 0;
949 for(addr = start; addr < (start + size); addr++) {
950 cpu_physical_memory_rw(addr, buf, 1, 0);
951 /* BSD sum algorithm ('sum' Unix command) */
952 sum = (sum >> 1) | (sum << 15);
953 sum += buf[0];
955 monitor_printf(mon, "%05d\n", sum);
958 typedef struct {
959 int keycode;
960 const char *name;
961 } KeyDef;
963 static const KeyDef key_defs[] = {
964 { 0x2a, "shift" },
965 { 0x36, "shift_r" },
967 { 0x38, "alt" },
968 { 0xb8, "alt_r" },
969 { 0x64, "altgr" },
970 { 0xe4, "altgr_r" },
971 { 0x1d, "ctrl" },
972 { 0x9d, "ctrl_r" },
974 { 0xdd, "menu" },
976 { 0x01, "esc" },
978 { 0x02, "1" },
979 { 0x03, "2" },
980 { 0x04, "3" },
981 { 0x05, "4" },
982 { 0x06, "5" },
983 { 0x07, "6" },
984 { 0x08, "7" },
985 { 0x09, "8" },
986 { 0x0a, "9" },
987 { 0x0b, "0" },
988 { 0x0c, "minus" },
989 { 0x0d, "equal" },
990 { 0x0e, "backspace" },
992 { 0x0f, "tab" },
993 { 0x10, "q" },
994 { 0x11, "w" },
995 { 0x12, "e" },
996 { 0x13, "r" },
997 { 0x14, "t" },
998 { 0x15, "y" },
999 { 0x16, "u" },
1000 { 0x17, "i" },
1001 { 0x18, "o" },
1002 { 0x19, "p" },
1004 { 0x1c, "ret" },
1006 { 0x1e, "a" },
1007 { 0x1f, "s" },
1008 { 0x20, "d" },
1009 { 0x21, "f" },
1010 { 0x22, "g" },
1011 { 0x23, "h" },
1012 { 0x24, "j" },
1013 { 0x25, "k" },
1014 { 0x26, "l" },
1016 { 0x2c, "z" },
1017 { 0x2d, "x" },
1018 { 0x2e, "c" },
1019 { 0x2f, "v" },
1020 { 0x30, "b" },
1021 { 0x31, "n" },
1022 { 0x32, "m" },
1023 { 0x33, "comma" },
1024 { 0x34, "dot" },
1025 { 0x35, "slash" },
1027 { 0x37, "asterisk" },
1029 { 0x39, "spc" },
1030 { 0x3a, "caps_lock" },
1031 { 0x3b, "f1" },
1032 { 0x3c, "f2" },
1033 { 0x3d, "f3" },
1034 { 0x3e, "f4" },
1035 { 0x3f, "f5" },
1036 { 0x40, "f6" },
1037 { 0x41, "f7" },
1038 { 0x42, "f8" },
1039 { 0x43, "f9" },
1040 { 0x44, "f10" },
1041 { 0x45, "num_lock" },
1042 { 0x46, "scroll_lock" },
1044 { 0xb5, "kp_divide" },
1045 { 0x37, "kp_multiply" },
1046 { 0x4a, "kp_subtract" },
1047 { 0x4e, "kp_add" },
1048 { 0x9c, "kp_enter" },
1049 { 0x53, "kp_decimal" },
1050 { 0x54, "sysrq" },
1052 { 0x52, "kp_0" },
1053 { 0x4f, "kp_1" },
1054 { 0x50, "kp_2" },
1055 { 0x51, "kp_3" },
1056 { 0x4b, "kp_4" },
1057 { 0x4c, "kp_5" },
1058 { 0x4d, "kp_6" },
1059 { 0x47, "kp_7" },
1060 { 0x48, "kp_8" },
1061 { 0x49, "kp_9" },
1063 { 0x56, "<" },
1065 { 0x57, "f11" },
1066 { 0x58, "f12" },
1068 { 0xb7, "print" },
1070 { 0xc7, "home" },
1071 { 0xc9, "pgup" },
1072 { 0xd1, "pgdn" },
1073 { 0xcf, "end" },
1075 { 0xcb, "left" },
1076 { 0xc8, "up" },
1077 { 0xd0, "down" },
1078 { 0xcd, "right" },
1080 { 0xd2, "insert" },
1081 { 0xd3, "delete" },
1082 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1083 { 0xf0, "stop" },
1084 { 0xf1, "again" },
1085 { 0xf2, "props" },
1086 { 0xf3, "undo" },
1087 { 0xf4, "front" },
1088 { 0xf5, "copy" },
1089 { 0xf6, "open" },
1090 { 0xf7, "paste" },
1091 { 0xf8, "find" },
1092 { 0xf9, "cut" },
1093 { 0xfa, "lf" },
1094 { 0xfb, "help" },
1095 { 0xfc, "meta_l" },
1096 { 0xfd, "meta_r" },
1097 { 0xfe, "compose" },
1098 #endif
1099 { 0, NULL },
1102 static int get_keycode(const char *key)
1104 const KeyDef *p;
1105 char *endp;
1106 int ret;
1108 for(p = key_defs; p->name != NULL; p++) {
1109 if (!strcmp(key, p->name))
1110 return p->keycode;
1112 if (strstart(key, "0x", NULL)) {
1113 ret = strtoul(key, &endp, 0);
1114 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1115 return ret;
1117 return -1;
1120 #define MAX_KEYCODES 16
1121 static uint8_t keycodes[MAX_KEYCODES];
1122 static int nb_pending_keycodes;
1123 static QEMUTimer *key_timer;
1125 static void release_keys(void *opaque)
1127 int keycode;
1129 while (nb_pending_keycodes > 0) {
1130 nb_pending_keycodes--;
1131 keycode = keycodes[nb_pending_keycodes];
1132 if (keycode & 0x80)
1133 kbd_put_keycode(0xe0);
1134 kbd_put_keycode(keycode | 0x80);
1138 static void do_sendkey(Monitor *mon, const QDict *qdict)
1140 char keyname_buf[16];
1141 char *separator;
1142 int keyname_len, keycode, i;
1143 const char *string = qdict_get_str(qdict, "string");
1144 int has_hold_time = qdict_haskey(qdict, "hold_time");
1145 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1147 if (nb_pending_keycodes > 0) {
1148 qemu_del_timer(key_timer);
1149 release_keys(NULL);
1151 if (!has_hold_time)
1152 hold_time = 100;
1153 i = 0;
1154 while (1) {
1155 separator = strchr(string, '-');
1156 keyname_len = separator ? separator - string : strlen(string);
1157 if (keyname_len > 0) {
1158 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1159 if (keyname_len > sizeof(keyname_buf) - 1) {
1160 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1161 return;
1163 if (i == MAX_KEYCODES) {
1164 monitor_printf(mon, "too many keys\n");
1165 return;
1167 keyname_buf[keyname_len] = 0;
1168 keycode = get_keycode(keyname_buf);
1169 if (keycode < 0) {
1170 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1171 return;
1173 keycodes[i++] = keycode;
1175 if (!separator)
1176 break;
1177 string = separator + 1;
1179 nb_pending_keycodes = i;
1180 /* key down events */
1181 for (i = 0; i < nb_pending_keycodes; i++) {
1182 keycode = keycodes[i];
1183 if (keycode & 0x80)
1184 kbd_put_keycode(0xe0);
1185 kbd_put_keycode(keycode & 0x7f);
1187 /* delayed key up events */
1188 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1189 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1192 static int mouse_button_state;
1194 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1196 int dx, dy, dz;
1197 const char *dx_str = qdict_get_str(qdict, "dx_str");
1198 const char *dy_str = qdict_get_str(qdict, "dy_str");
1199 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1200 dx = strtol(dx_str, NULL, 0);
1201 dy = strtol(dy_str, NULL, 0);
1202 dz = 0;
1203 if (dz_str)
1204 dz = strtol(dz_str, NULL, 0);
1205 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1208 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1210 int button_state = qdict_get_int(qdict, "button_state");
1211 mouse_button_state = button_state;
1212 kbd_mouse_event(0, 0, 0, mouse_button_state);
1215 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1217 int size = qdict_get_int(qdict, "size");
1218 int addr = qdict_get_int(qdict, "addr");
1219 int has_index = qdict_haskey(qdict, "index");
1220 uint32_t val;
1221 int suffix;
1223 if (has_index) {
1224 int index = qdict_get_int(qdict, "index");
1225 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1226 addr++;
1228 addr &= 0xffff;
1230 switch(size) {
1231 default:
1232 case 1:
1233 val = cpu_inb(addr);
1234 suffix = 'b';
1235 break;
1236 case 2:
1237 val = cpu_inw(addr);
1238 suffix = 'w';
1239 break;
1240 case 4:
1241 val = cpu_inl(addr);
1242 suffix = 'l';
1243 break;
1245 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1246 suffix, addr, size * 2, val);
1249 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1251 int size = qdict_get_int(qdict, "size");
1252 int addr = qdict_get_int(qdict, "addr");
1253 int val = qdict_get_int(qdict, "val");
1255 addr &= IOPORTS_MASK;
1257 switch (size) {
1258 default:
1259 case 1:
1260 cpu_outb(addr, val);
1261 break;
1262 case 2:
1263 cpu_outw(addr, val);
1264 break;
1265 case 4:
1266 cpu_outl(addr, val);
1267 break;
1271 static void do_boot_set(Monitor *mon, const QDict *qdict)
1273 int res;
1274 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1276 res = qemu_boot_set(bootdevice);
1277 if (res == 0) {
1278 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1279 } else if (res > 0) {
1280 monitor_printf(mon, "setting boot device list failed\n");
1281 } else {
1282 monitor_printf(mon, "no function defined to set boot device list for "
1283 "this architecture\n");
1287 static void do_system_reset(Monitor *mon, const QDict *qdict)
1289 qemu_system_reset_request();
1292 static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1294 qemu_system_powerdown_request();
1297 #if defined(TARGET_I386)
1298 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1300 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1301 addr,
1302 pte & mask,
1303 pte & PG_GLOBAL_MASK ? 'G' : '-',
1304 pte & PG_PSE_MASK ? 'P' : '-',
1305 pte & PG_DIRTY_MASK ? 'D' : '-',
1306 pte & PG_ACCESSED_MASK ? 'A' : '-',
1307 pte & PG_PCD_MASK ? 'C' : '-',
1308 pte & PG_PWT_MASK ? 'T' : '-',
1309 pte & PG_USER_MASK ? 'U' : '-',
1310 pte & PG_RW_MASK ? 'W' : '-');
1313 static void tlb_info(Monitor *mon)
1315 CPUState *env;
1316 int l1, l2;
1317 uint32_t pgd, pde, pte;
1319 env = mon_get_cpu();
1320 if (!env)
1321 return;
1323 if (!(env->cr[0] & CR0_PG_MASK)) {
1324 monitor_printf(mon, "PG disabled\n");
1325 return;
1327 pgd = env->cr[3] & ~0xfff;
1328 for(l1 = 0; l1 < 1024; l1++) {
1329 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1330 pde = le32_to_cpu(pde);
1331 if (pde & PG_PRESENT_MASK) {
1332 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1333 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1334 } else {
1335 for(l2 = 0; l2 < 1024; l2++) {
1336 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1337 (uint8_t *)&pte, 4);
1338 pte = le32_to_cpu(pte);
1339 if (pte & PG_PRESENT_MASK) {
1340 print_pte(mon, (l1 << 22) + (l2 << 12),
1341 pte & ~PG_PSE_MASK,
1342 ~0xfff);
1350 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1351 uint32_t end, int prot)
1353 int prot1;
1354 prot1 = *plast_prot;
1355 if (prot != prot1) {
1356 if (*pstart != -1) {
1357 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1358 *pstart, end, end - *pstart,
1359 prot1 & PG_USER_MASK ? 'u' : '-',
1360 'r',
1361 prot1 & PG_RW_MASK ? 'w' : '-');
1363 if (prot != 0)
1364 *pstart = end;
1365 else
1366 *pstart = -1;
1367 *plast_prot = prot;
1371 static void mem_info(Monitor *mon)
1373 CPUState *env;
1374 int l1, l2, prot, last_prot;
1375 uint32_t pgd, pde, pte, start, end;
1377 env = mon_get_cpu();
1378 if (!env)
1379 return;
1381 if (!(env->cr[0] & CR0_PG_MASK)) {
1382 monitor_printf(mon, "PG disabled\n");
1383 return;
1385 pgd = env->cr[3] & ~0xfff;
1386 last_prot = 0;
1387 start = -1;
1388 for(l1 = 0; l1 < 1024; l1++) {
1389 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1390 pde = le32_to_cpu(pde);
1391 end = l1 << 22;
1392 if (pde & PG_PRESENT_MASK) {
1393 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1394 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1395 mem_print(mon, &start, &last_prot, end, prot);
1396 } else {
1397 for(l2 = 0; l2 < 1024; l2++) {
1398 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1399 (uint8_t *)&pte, 4);
1400 pte = le32_to_cpu(pte);
1401 end = (l1 << 22) + (l2 << 12);
1402 if (pte & PG_PRESENT_MASK) {
1403 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1404 } else {
1405 prot = 0;
1407 mem_print(mon, &start, &last_prot, end, prot);
1410 } else {
1411 prot = 0;
1412 mem_print(mon, &start, &last_prot, end, prot);
1416 #endif
1418 #if defined(TARGET_SH4)
1420 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1422 monitor_printf(mon, " tlb%i:\t"
1423 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1424 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1425 "dirty=%hhu writethrough=%hhu\n",
1426 idx,
1427 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1428 tlb->v, tlb->sh, tlb->c, tlb->pr,
1429 tlb->d, tlb->wt);
1432 static void tlb_info(Monitor *mon)
1434 CPUState *env = mon_get_cpu();
1435 int i;
1437 monitor_printf (mon, "ITLB:\n");
1438 for (i = 0 ; i < ITLB_SIZE ; i++)
1439 print_tlb (mon, i, &env->itlb[i]);
1440 monitor_printf (mon, "UTLB:\n");
1441 for (i = 0 ; i < UTLB_SIZE ; i++)
1442 print_tlb (mon, i, &env->utlb[i]);
1445 #endif
1447 static void do_info_kvm(Monitor *mon)
1449 #if defined(USE_KVM) || defined(CONFIG_KVM)
1450 monitor_printf(mon, "kvm support: ");
1451 if (kvm_enabled())
1452 monitor_printf(mon, "enabled\n");
1453 else
1454 monitor_printf(mon, "disabled\n");
1455 #else
1456 monitor_printf(mon, "kvm support: not compiled\n");
1457 #endif
1460 static void do_info_numa(Monitor *mon)
1462 int i;
1463 CPUState *env;
1465 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1466 for (i = 0; i < nb_numa_nodes; i++) {
1467 monitor_printf(mon, "node %d cpus:", i);
1468 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1469 if (env->numa_node == i) {
1470 monitor_printf(mon, " %d", env->cpu_index);
1473 monitor_printf(mon, "\n");
1474 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1475 node_mem[i] >> 20);
1479 #ifdef CONFIG_PROFILER
1481 int64_t qemu_time;
1482 int64_t dev_time;
1484 static void do_info_profile(Monitor *mon)
1486 int64_t total;
1487 total = qemu_time;
1488 if (total == 0)
1489 total = 1;
1490 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1491 dev_time, dev_time / (double)get_ticks_per_sec());
1492 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1493 qemu_time, qemu_time / (double)get_ticks_per_sec());
1494 qemu_time = 0;
1495 dev_time = 0;
1497 #else
1498 static void do_info_profile(Monitor *mon)
1500 monitor_printf(mon, "Internal profiler not compiled\n");
1502 #endif
1504 /* Capture support */
1505 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1507 static void do_info_capture(Monitor *mon)
1509 int i;
1510 CaptureState *s;
1512 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1513 monitor_printf(mon, "[%d]: ", i);
1514 s->ops.info (s->opaque);
1518 #ifdef HAS_AUDIO
1519 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1521 int i;
1522 int n = qdict_get_int(qdict, "n");
1523 CaptureState *s;
1525 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1526 if (i == n) {
1527 s->ops.destroy (s->opaque);
1528 QLIST_REMOVE (s, entries);
1529 qemu_free (s);
1530 return;
1535 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1537 const char *path = qdict_get_str(qdict, "path");
1538 int has_freq = qdict_haskey(qdict, "freq");
1539 int freq = qdict_get_try_int(qdict, "freq", -1);
1540 int has_bits = qdict_haskey(qdict, "bits");
1541 int bits = qdict_get_try_int(qdict, "bits", -1);
1542 int has_channels = qdict_haskey(qdict, "nchannels");
1543 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1544 CaptureState *s;
1546 s = qemu_mallocz (sizeof (*s));
1548 freq = has_freq ? freq : 44100;
1549 bits = has_bits ? bits : 16;
1550 nchannels = has_channels ? nchannels : 2;
1552 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1553 monitor_printf(mon, "Faied to add wave capture\n");
1554 qemu_free (s);
1556 QLIST_INSERT_HEAD (&capture_head, s, entries);
1558 #endif
1560 #if defined(TARGET_I386)
1561 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1563 CPUState *env;
1564 int cpu_index = qdict_get_int(qdict, "cpu_index");
1566 for (env = first_cpu; env != NULL; env = env->next_cpu)
1567 if (env->cpu_index == cpu_index) {
1568 if (kvm_enabled())
1569 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1570 else
1571 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1572 break;
1575 #endif
1577 static void do_info_status(Monitor *mon)
1579 if (vm_running) {
1580 if (singlestep) {
1581 monitor_printf(mon, "VM status: running (single step mode)\n");
1582 } else {
1583 monitor_printf(mon, "VM status: running\n");
1585 } else
1586 monitor_printf(mon, "VM status: paused\n");
1590 static void do_balloon(Monitor *mon, const QDict *qdict)
1592 int value = qdict_get_int(qdict, "value");
1593 ram_addr_t target = value;
1594 qemu_balloon(target << 20);
1597 static void do_info_balloon(Monitor *mon)
1599 ram_addr_t actual;
1601 actual = qemu_balloon_status();
1602 if (kvm_enabled() && !kvm_has_sync_mmu())
1603 monitor_printf(mon, "Using KVM without synchronous MMU, "
1604 "ballooning disabled\n");
1605 else if (actual == 0)
1606 monitor_printf(mon, "Ballooning not activated in VM\n");
1607 else
1608 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1611 static qemu_acl *find_acl(Monitor *mon, const char *name)
1613 qemu_acl *acl = qemu_acl_find(name);
1615 if (!acl) {
1616 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1618 return acl;
1621 static void do_acl_show(Monitor *mon, const QDict *qdict)
1623 const char *aclname = qdict_get_str(qdict, "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 QTAILQ_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 QDict *qdict)
1641 const char *aclname = qdict_get_str(qdict, "aclname");
1642 qemu_acl *acl = find_acl(mon, aclname);
1644 if (acl) {
1645 qemu_acl_reset(acl);
1646 monitor_printf(mon, "acl: removed all rules\n");
1650 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1652 const char *aclname = qdict_get_str(qdict, "aclname");
1653 const char *policy = qdict_get_str(qdict, "policy");
1654 qemu_acl *acl = find_acl(mon, aclname);
1656 if (acl) {
1657 if (strcmp(policy, "allow") == 0) {
1658 acl->defaultDeny = 0;
1659 monitor_printf(mon, "acl: policy set to 'allow'\n");
1660 } else if (strcmp(policy, "deny") == 0) {
1661 acl->defaultDeny = 1;
1662 monitor_printf(mon, "acl: policy set to 'deny'\n");
1663 } else {
1664 monitor_printf(mon, "acl: unknown policy '%s', "
1665 "expected 'deny' or 'allow'\n", policy);
1670 static void do_acl_add(Monitor *mon, const QDict *qdict)
1672 const char *aclname = qdict_get_str(qdict, "aclname");
1673 const char *match = qdict_get_str(qdict, "match");
1674 const char *policy = qdict_get_str(qdict, "policy");
1675 int has_index = qdict_haskey(qdict, "index");
1676 int index = qdict_get_try_int(qdict, "index", -1);
1677 qemu_acl *acl = find_acl(mon, aclname);
1678 int deny, ret;
1680 if (acl) {
1681 if (strcmp(policy, "allow") == 0) {
1682 deny = 0;
1683 } else if (strcmp(policy, "deny") == 0) {
1684 deny = 1;
1685 } else {
1686 monitor_printf(mon, "acl: unknown policy '%s', "
1687 "expected 'deny' or 'allow'\n", policy);
1688 return;
1690 if (has_index)
1691 ret = qemu_acl_insert(acl, deny, match, index);
1692 else
1693 ret = qemu_acl_append(acl, deny, match);
1694 if (ret < 0)
1695 monitor_printf(mon, "acl: unable to add acl entry\n");
1696 else
1697 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1701 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1703 const char *aclname = qdict_get_str(qdict, "aclname");
1704 const char *match = qdict_get_str(qdict, "match");
1705 qemu_acl *acl = find_acl(mon, aclname);
1706 int ret;
1708 if (acl) {
1709 ret = qemu_acl_remove(acl, match);
1710 if (ret < 0)
1711 monitor_printf(mon, "acl: no matching acl entry\n");
1712 else
1713 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1717 #if defined(TARGET_I386)
1718 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1720 CPUState *cenv;
1721 int cpu_index = qdict_get_int(qdict, "cpu_index");
1722 int bank = qdict_get_int(qdict, "bank");
1723 uint64_t status = qdict_get_int(qdict, "status");
1724 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1725 uint64_t addr = qdict_get_int(qdict, "addr");
1726 uint64_t misc = qdict_get_int(qdict, "misc");
1728 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1729 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1730 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1731 break;
1734 #endif
1736 static void do_getfd(Monitor *mon, const QDict *qdict)
1738 const char *fdname = qdict_get_str(qdict, "fdname");
1739 mon_fd_t *monfd;
1740 int fd;
1742 fd = qemu_chr_get_msgfd(mon->chr);
1743 if (fd == -1) {
1744 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1745 return;
1748 if (qemu_isdigit(fdname[0])) {
1749 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1750 return;
1753 fd = dup(fd);
1754 if (fd == -1) {
1755 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1756 strerror(errno));
1757 return;
1760 QLIST_FOREACH(monfd, &mon->fds, next) {
1761 if (strcmp(monfd->name, fdname) != 0) {
1762 continue;
1765 close(monfd->fd);
1766 monfd->fd = fd;
1767 return;
1770 monfd = qemu_mallocz(sizeof(mon_fd_t));
1771 monfd->name = qemu_strdup(fdname);
1772 monfd->fd = fd;
1774 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1777 static void do_closefd(Monitor *mon, const QDict *qdict)
1779 const char *fdname = qdict_get_str(qdict, "fdname");
1780 mon_fd_t *monfd;
1782 QLIST_FOREACH(monfd, &mon->fds, next) {
1783 if (strcmp(monfd->name, fdname) != 0) {
1784 continue;
1787 QLIST_REMOVE(monfd, next);
1788 close(monfd->fd);
1789 qemu_free(monfd->name);
1790 qemu_free(monfd);
1791 return;
1794 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1795 fdname);
1798 static void do_loadvm(Monitor *mon, const QDict *qdict)
1800 int saved_vm_running = vm_running;
1801 const char *name = qdict_get_str(qdict, "name");
1803 vm_stop(0);
1805 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1806 vm_start();
1809 int monitor_get_fd(Monitor *mon, const char *fdname)
1811 mon_fd_t *monfd;
1813 QLIST_FOREACH(monfd, &mon->fds, next) {
1814 int fd;
1816 if (strcmp(monfd->name, fdname) != 0) {
1817 continue;
1820 fd = monfd->fd;
1822 /* caller takes ownership of fd */
1823 QLIST_REMOVE(monfd, next);
1824 qemu_free(monfd->name);
1825 qemu_free(monfd);
1827 return fd;
1830 return -1;
1833 static const mon_cmd_t mon_cmds[] = {
1834 #include "qemu-monitor.h"
1835 { NULL, NULL, },
1838 /* Please update qemu-monitor.hx when adding or changing commands */
1839 static const mon_cmd_t info_cmds[] = {
1840 { "version", "", do_info_version,
1841 "", "show the version of QEMU" },
1842 { "network", "", do_info_network,
1843 "", "show the network state" },
1844 { "chardev", "", qemu_chr_info,
1845 "", "show the character devices" },
1846 { "block", "", bdrv_info,
1847 "", "show the block devices" },
1848 { "blockstats", "", bdrv_info_stats,
1849 "", "show block device statistics" },
1850 { "registers", "", do_info_registers,
1851 "", "show the cpu registers" },
1852 { "cpus", "", do_info_cpus,
1853 "", "show infos for each CPU" },
1854 { "history", "", do_info_history,
1855 "", "show the command line history", },
1856 { "irq", "", irq_info,
1857 "", "show the interrupts statistics (if available)", },
1858 { "pic", "", pic_info,
1859 "", "show i8259 (PIC) state", },
1860 { "pci", "", pci_info,
1861 "", "show PCI info", },
1862 #if defined(TARGET_I386) || defined(TARGET_SH4)
1863 { "tlb", "", tlb_info,
1864 "", "show virtual to physical memory mappings", },
1865 #endif
1866 #if defined(TARGET_I386)
1867 { "mem", "", mem_info,
1868 "", "show the active virtual memory mappings", },
1869 { "hpet", "", do_info_hpet,
1870 "", "show state of HPET", },
1871 #endif
1872 { "jit", "", do_info_jit,
1873 "", "show dynamic compiler info", },
1874 { "kvm", "", do_info_kvm,
1875 "", "show KVM information", },
1876 { "numa", "", do_info_numa,
1877 "", "show NUMA information", },
1878 { "usb", "", usb_info,
1879 "", "show guest USB devices", },
1880 { "usbhost", "", usb_host_info,
1881 "", "show host USB devices", },
1882 { "profile", "", do_info_profile,
1883 "", "show profiling information", },
1884 { "capture", "", do_info_capture,
1885 "", "show capture information" },
1886 { "snapshots", "", do_info_snapshots,
1887 "", "show the currently saved VM snapshots" },
1888 { "status", "", do_info_status,
1889 "", "show the current VM status (running|paused)" },
1890 { "pcmcia", "", pcmcia_info,
1891 "", "show guest PCMCIA status" },
1892 { "mice", "", do_info_mice,
1893 "", "show which guest mouse is receiving events" },
1894 { "vnc", "", do_info_vnc,
1895 "", "show the vnc server status"},
1896 { "name", "", do_info_name,
1897 "", "show the current VM name" },
1898 { "uuid", "", do_info_uuid,
1899 "", "show the current VM UUID" },
1900 #if defined(TARGET_PPC)
1901 { "cpustats", "", do_info_cpu_stats,
1902 "", "show CPU statistics", },
1903 #endif
1904 #if defined(CONFIG_SLIRP)
1905 { "usernet", "", do_info_usernet,
1906 "", "show user network stack connection states", },
1907 #endif
1908 { "migrate", "", do_info_migrate, "", "show migration status" },
1909 { "balloon", "", do_info_balloon,
1910 "", "show balloon information" },
1911 { "qtree", "", do_info_qtree,
1912 "", "show device tree" },
1913 { "qdm", "", do_info_qdm,
1914 "", "show qdev device model list" },
1915 { "roms", "", do_info_roms,
1916 "", "show roms" },
1917 { NULL, NULL, },
1920 /*******************************************************************/
1922 static const char *pch;
1923 static jmp_buf expr_env;
1925 #define MD_TLONG 0
1926 #define MD_I32 1
1928 typedef struct MonitorDef {
1929 const char *name;
1930 int offset;
1931 target_long (*get_value)(const struct MonitorDef *md, int val);
1932 int type;
1933 } MonitorDef;
1935 #if defined(TARGET_I386)
1936 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1938 CPUState *env = mon_get_cpu();
1939 if (!env)
1940 return 0;
1941 return env->eip + env->segs[R_CS].base;
1943 #endif
1945 #if defined(TARGET_PPC)
1946 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1948 CPUState *env = mon_get_cpu();
1949 unsigned int u;
1950 int i;
1952 if (!env)
1953 return 0;
1955 u = 0;
1956 for (i = 0; i < 8; i++)
1957 u |= env->crf[i] << (32 - (4 * i));
1959 return u;
1962 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1964 CPUState *env = mon_get_cpu();
1965 if (!env)
1966 return 0;
1967 return env->msr;
1970 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1972 CPUState *env = mon_get_cpu();
1973 if (!env)
1974 return 0;
1975 return env->xer;
1978 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1980 CPUState *env = mon_get_cpu();
1981 if (!env)
1982 return 0;
1983 return cpu_ppc_load_decr(env);
1986 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1988 CPUState *env = mon_get_cpu();
1989 if (!env)
1990 return 0;
1991 return cpu_ppc_load_tbu(env);
1994 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1996 CPUState *env = mon_get_cpu();
1997 if (!env)
1998 return 0;
1999 return cpu_ppc_load_tbl(env);
2001 #endif
2003 #if defined(TARGET_SPARC)
2004 #ifndef TARGET_SPARC64
2005 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2007 CPUState *env = mon_get_cpu();
2008 if (!env)
2009 return 0;
2010 return GET_PSR(env);
2012 #endif
2014 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2016 CPUState *env = mon_get_cpu();
2017 if (!env)
2018 return 0;
2019 return env->regwptr[val];
2021 #endif
2023 static const MonitorDef monitor_defs[] = {
2024 #ifdef TARGET_I386
2026 #define SEG(name, seg) \
2027 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2028 { name ".base", offsetof(CPUState, segs[seg].base) },\
2029 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2031 { "eax", offsetof(CPUState, regs[0]) },
2032 { "ecx", offsetof(CPUState, regs[1]) },
2033 { "edx", offsetof(CPUState, regs[2]) },
2034 { "ebx", offsetof(CPUState, regs[3]) },
2035 { "esp|sp", offsetof(CPUState, regs[4]) },
2036 { "ebp|fp", offsetof(CPUState, regs[5]) },
2037 { "esi", offsetof(CPUState, regs[6]) },
2038 { "edi", offsetof(CPUState, regs[7]) },
2039 #ifdef TARGET_X86_64
2040 { "r8", offsetof(CPUState, regs[8]) },
2041 { "r9", offsetof(CPUState, regs[9]) },
2042 { "r10", offsetof(CPUState, regs[10]) },
2043 { "r11", offsetof(CPUState, regs[11]) },
2044 { "r12", offsetof(CPUState, regs[12]) },
2045 { "r13", offsetof(CPUState, regs[13]) },
2046 { "r14", offsetof(CPUState, regs[14]) },
2047 { "r15", offsetof(CPUState, regs[15]) },
2048 #endif
2049 { "eflags", offsetof(CPUState, eflags) },
2050 { "eip", offsetof(CPUState, eip) },
2051 SEG("cs", R_CS)
2052 SEG("ds", R_DS)
2053 SEG("es", R_ES)
2054 SEG("ss", R_SS)
2055 SEG("fs", R_FS)
2056 SEG("gs", R_GS)
2057 { "pc", 0, monitor_get_pc, },
2058 #elif defined(TARGET_PPC)
2059 /* General purpose registers */
2060 { "r0", offsetof(CPUState, gpr[0]) },
2061 { "r1", offsetof(CPUState, gpr[1]) },
2062 { "r2", offsetof(CPUState, gpr[2]) },
2063 { "r3", offsetof(CPUState, gpr[3]) },
2064 { "r4", offsetof(CPUState, gpr[4]) },
2065 { "r5", offsetof(CPUState, gpr[5]) },
2066 { "r6", offsetof(CPUState, gpr[6]) },
2067 { "r7", offsetof(CPUState, gpr[7]) },
2068 { "r8", offsetof(CPUState, gpr[8]) },
2069 { "r9", offsetof(CPUState, gpr[9]) },
2070 { "r10", offsetof(CPUState, gpr[10]) },
2071 { "r11", offsetof(CPUState, gpr[11]) },
2072 { "r12", offsetof(CPUState, gpr[12]) },
2073 { "r13", offsetof(CPUState, gpr[13]) },
2074 { "r14", offsetof(CPUState, gpr[14]) },
2075 { "r15", offsetof(CPUState, gpr[15]) },
2076 { "r16", offsetof(CPUState, gpr[16]) },
2077 { "r17", offsetof(CPUState, gpr[17]) },
2078 { "r18", offsetof(CPUState, gpr[18]) },
2079 { "r19", offsetof(CPUState, gpr[19]) },
2080 { "r20", offsetof(CPUState, gpr[20]) },
2081 { "r21", offsetof(CPUState, gpr[21]) },
2082 { "r22", offsetof(CPUState, gpr[22]) },
2083 { "r23", offsetof(CPUState, gpr[23]) },
2084 { "r24", offsetof(CPUState, gpr[24]) },
2085 { "r25", offsetof(CPUState, gpr[25]) },
2086 { "r26", offsetof(CPUState, gpr[26]) },
2087 { "r27", offsetof(CPUState, gpr[27]) },
2088 { "r28", offsetof(CPUState, gpr[28]) },
2089 { "r29", offsetof(CPUState, gpr[29]) },
2090 { "r30", offsetof(CPUState, gpr[30]) },
2091 { "r31", offsetof(CPUState, gpr[31]) },
2092 /* Floating point registers */
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 { "fpscr", offsetof(CPUState, fpscr) },
2126 /* Next instruction pointer */
2127 { "nip|pc", offsetof(CPUState, nip) },
2128 { "lr", offsetof(CPUState, lr) },
2129 { "ctr", offsetof(CPUState, ctr) },
2130 { "decr", 0, &monitor_get_decr, },
2131 { "ccr", 0, &monitor_get_ccr, },
2132 /* Machine state register */
2133 { "msr", 0, &monitor_get_msr, },
2134 { "xer", 0, &monitor_get_xer, },
2135 { "tbu", 0, &monitor_get_tbu, },
2136 { "tbl", 0, &monitor_get_tbl, },
2137 #if defined(TARGET_PPC64)
2138 /* Address space register */
2139 { "asr", offsetof(CPUState, asr) },
2140 #endif
2141 /* Segment registers */
2142 { "sdr1", offsetof(CPUState, sdr1) },
2143 { "sr0", offsetof(CPUState, sr[0]) },
2144 { "sr1", offsetof(CPUState, sr[1]) },
2145 { "sr2", offsetof(CPUState, sr[2]) },
2146 { "sr3", offsetof(CPUState, sr[3]) },
2147 { "sr4", offsetof(CPUState, sr[4]) },
2148 { "sr5", offsetof(CPUState, sr[5]) },
2149 { "sr6", offsetof(CPUState, sr[6]) },
2150 { "sr7", offsetof(CPUState, sr[7]) },
2151 { "sr8", offsetof(CPUState, sr[8]) },
2152 { "sr9", offsetof(CPUState, sr[9]) },
2153 { "sr10", offsetof(CPUState, sr[10]) },
2154 { "sr11", offsetof(CPUState, sr[11]) },
2155 { "sr12", offsetof(CPUState, sr[12]) },
2156 { "sr13", offsetof(CPUState, sr[13]) },
2157 { "sr14", offsetof(CPUState, sr[14]) },
2158 { "sr15", offsetof(CPUState, sr[15]) },
2159 /* Too lazy to put BATs and SPRs ... */
2160 #elif defined(TARGET_SPARC)
2161 { "g0", offsetof(CPUState, gregs[0]) },
2162 { "g1", offsetof(CPUState, gregs[1]) },
2163 { "g2", offsetof(CPUState, gregs[2]) },
2164 { "g3", offsetof(CPUState, gregs[3]) },
2165 { "g4", offsetof(CPUState, gregs[4]) },
2166 { "g5", offsetof(CPUState, gregs[5]) },
2167 { "g6", offsetof(CPUState, gregs[6]) },
2168 { "g7", offsetof(CPUState, gregs[7]) },
2169 { "o0", 0, monitor_get_reg },
2170 { "o1", 1, monitor_get_reg },
2171 { "o2", 2, monitor_get_reg },
2172 { "o3", 3, monitor_get_reg },
2173 { "o4", 4, monitor_get_reg },
2174 { "o5", 5, monitor_get_reg },
2175 { "o6", 6, monitor_get_reg },
2176 { "o7", 7, monitor_get_reg },
2177 { "l0", 8, monitor_get_reg },
2178 { "l1", 9, monitor_get_reg },
2179 { "l2", 10, monitor_get_reg },
2180 { "l3", 11, monitor_get_reg },
2181 { "l4", 12, monitor_get_reg },
2182 { "l5", 13, monitor_get_reg },
2183 { "l6", 14, monitor_get_reg },
2184 { "l7", 15, monitor_get_reg },
2185 { "i0", 16, monitor_get_reg },
2186 { "i1", 17, monitor_get_reg },
2187 { "i2", 18, monitor_get_reg },
2188 { "i3", 19, monitor_get_reg },
2189 { "i4", 20, monitor_get_reg },
2190 { "i5", 21, monitor_get_reg },
2191 { "i6", 22, monitor_get_reg },
2192 { "i7", 23, monitor_get_reg },
2193 { "pc", offsetof(CPUState, pc) },
2194 { "npc", offsetof(CPUState, npc) },
2195 { "y", offsetof(CPUState, y) },
2196 #ifndef TARGET_SPARC64
2197 { "psr", 0, &monitor_get_psr, },
2198 { "wim", offsetof(CPUState, wim) },
2199 #endif
2200 { "tbr", offsetof(CPUState, tbr) },
2201 { "fsr", offsetof(CPUState, fsr) },
2202 { "f0", offsetof(CPUState, fpr[0]) },
2203 { "f1", offsetof(CPUState, fpr[1]) },
2204 { "f2", offsetof(CPUState, fpr[2]) },
2205 { "f3", offsetof(CPUState, fpr[3]) },
2206 { "f4", offsetof(CPUState, fpr[4]) },
2207 { "f5", offsetof(CPUState, fpr[5]) },
2208 { "f6", offsetof(CPUState, fpr[6]) },
2209 { "f7", offsetof(CPUState, fpr[7]) },
2210 { "f8", offsetof(CPUState, fpr[8]) },
2211 { "f9", offsetof(CPUState, fpr[9]) },
2212 { "f10", offsetof(CPUState, fpr[10]) },
2213 { "f11", offsetof(CPUState, fpr[11]) },
2214 { "f12", offsetof(CPUState, fpr[12]) },
2215 { "f13", offsetof(CPUState, fpr[13]) },
2216 { "f14", offsetof(CPUState, fpr[14]) },
2217 { "f15", offsetof(CPUState, fpr[15]) },
2218 { "f16", offsetof(CPUState, fpr[16]) },
2219 { "f17", offsetof(CPUState, fpr[17]) },
2220 { "f18", offsetof(CPUState, fpr[18]) },
2221 { "f19", offsetof(CPUState, fpr[19]) },
2222 { "f20", offsetof(CPUState, fpr[20]) },
2223 { "f21", offsetof(CPUState, fpr[21]) },
2224 { "f22", offsetof(CPUState, fpr[22]) },
2225 { "f23", offsetof(CPUState, fpr[23]) },
2226 { "f24", offsetof(CPUState, fpr[24]) },
2227 { "f25", offsetof(CPUState, fpr[25]) },
2228 { "f26", offsetof(CPUState, fpr[26]) },
2229 { "f27", offsetof(CPUState, fpr[27]) },
2230 { "f28", offsetof(CPUState, fpr[28]) },
2231 { "f29", offsetof(CPUState, fpr[29]) },
2232 { "f30", offsetof(CPUState, fpr[30]) },
2233 { "f31", offsetof(CPUState, fpr[31]) },
2234 #ifdef TARGET_SPARC64
2235 { "f32", offsetof(CPUState, fpr[32]) },
2236 { "f34", offsetof(CPUState, fpr[34]) },
2237 { "f36", offsetof(CPUState, fpr[36]) },
2238 { "f38", offsetof(CPUState, fpr[38]) },
2239 { "f40", offsetof(CPUState, fpr[40]) },
2240 { "f42", offsetof(CPUState, fpr[42]) },
2241 { "f44", offsetof(CPUState, fpr[44]) },
2242 { "f46", offsetof(CPUState, fpr[46]) },
2243 { "f48", offsetof(CPUState, fpr[48]) },
2244 { "f50", offsetof(CPUState, fpr[50]) },
2245 { "f52", offsetof(CPUState, fpr[52]) },
2246 { "f54", offsetof(CPUState, fpr[54]) },
2247 { "f56", offsetof(CPUState, fpr[56]) },
2248 { "f58", offsetof(CPUState, fpr[58]) },
2249 { "f60", offsetof(CPUState, fpr[60]) },
2250 { "f62", offsetof(CPUState, fpr[62]) },
2251 { "asi", offsetof(CPUState, asi) },
2252 { "pstate", offsetof(CPUState, pstate) },
2253 { "cansave", offsetof(CPUState, cansave) },
2254 { "canrestore", offsetof(CPUState, canrestore) },
2255 { "otherwin", offsetof(CPUState, otherwin) },
2256 { "wstate", offsetof(CPUState, wstate) },
2257 { "cleanwin", offsetof(CPUState, cleanwin) },
2258 { "fprs", offsetof(CPUState, fprs) },
2259 #endif
2260 #endif
2261 { NULL },
2264 static void expr_error(Monitor *mon, const char *msg)
2266 monitor_printf(mon, "%s\n", msg);
2267 longjmp(expr_env, 1);
2270 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2271 static int get_monitor_def(target_long *pval, const char *name)
2273 const MonitorDef *md;
2274 void *ptr;
2276 for(md = monitor_defs; md->name != NULL; md++) {
2277 if (compare_cmd(name, md->name)) {
2278 if (md->get_value) {
2279 *pval = md->get_value(md, md->offset);
2280 } else {
2281 CPUState *env = mon_get_cpu();
2282 if (!env)
2283 return -2;
2284 ptr = (uint8_t *)env + md->offset;
2285 switch(md->type) {
2286 case MD_I32:
2287 *pval = *(int32_t *)ptr;
2288 break;
2289 case MD_TLONG:
2290 *pval = *(target_long *)ptr;
2291 break;
2292 default:
2293 *pval = 0;
2294 break;
2297 return 0;
2300 return -1;
2303 static void next(void)
2305 if (*pch != '\0') {
2306 pch++;
2307 while (qemu_isspace(*pch))
2308 pch++;
2312 static int64_t expr_sum(Monitor *mon);
2314 static int64_t expr_unary(Monitor *mon)
2316 int64_t n;
2317 char *p;
2318 int ret;
2320 switch(*pch) {
2321 case '+':
2322 next();
2323 n = expr_unary(mon);
2324 break;
2325 case '-':
2326 next();
2327 n = -expr_unary(mon);
2328 break;
2329 case '~':
2330 next();
2331 n = ~expr_unary(mon);
2332 break;
2333 case '(':
2334 next();
2335 n = expr_sum(mon);
2336 if (*pch != ')') {
2337 expr_error(mon, "')' expected");
2339 next();
2340 break;
2341 case '\'':
2342 pch++;
2343 if (*pch == '\0')
2344 expr_error(mon, "character constant expected");
2345 n = *pch;
2346 pch++;
2347 if (*pch != '\'')
2348 expr_error(mon, "missing terminating \' character");
2349 next();
2350 break;
2351 case '$':
2353 char buf[128], *q;
2354 target_long reg=0;
2356 pch++;
2357 q = buf;
2358 while ((*pch >= 'a' && *pch <= 'z') ||
2359 (*pch >= 'A' && *pch <= 'Z') ||
2360 (*pch >= '0' && *pch <= '9') ||
2361 *pch == '_' || *pch == '.') {
2362 if ((q - buf) < sizeof(buf) - 1)
2363 *q++ = *pch;
2364 pch++;
2366 while (qemu_isspace(*pch))
2367 pch++;
2368 *q = 0;
2369 ret = get_monitor_def(&reg, buf);
2370 if (ret == -1)
2371 expr_error(mon, "unknown register");
2372 else if (ret == -2)
2373 expr_error(mon, "no cpu defined");
2374 n = reg;
2376 break;
2377 case '\0':
2378 expr_error(mon, "unexpected end of expression");
2379 n = 0;
2380 break;
2381 default:
2382 #if TARGET_PHYS_ADDR_BITS > 32
2383 n = strtoull(pch, &p, 0);
2384 #else
2385 n = strtoul(pch, &p, 0);
2386 #endif
2387 if (pch == p) {
2388 expr_error(mon, "invalid char in expression");
2390 pch = p;
2391 while (qemu_isspace(*pch))
2392 pch++;
2393 break;
2395 return n;
2399 static int64_t expr_prod(Monitor *mon)
2401 int64_t val, val2;
2402 int op;
2404 val = expr_unary(mon);
2405 for(;;) {
2406 op = *pch;
2407 if (op != '*' && op != '/' && op != '%')
2408 break;
2409 next();
2410 val2 = expr_unary(mon);
2411 switch(op) {
2412 default:
2413 case '*':
2414 val *= val2;
2415 break;
2416 case '/':
2417 case '%':
2418 if (val2 == 0)
2419 expr_error(mon, "division by zero");
2420 if (op == '/')
2421 val /= val2;
2422 else
2423 val %= val2;
2424 break;
2427 return val;
2430 static int64_t expr_logic(Monitor *mon)
2432 int64_t val, val2;
2433 int op;
2435 val = expr_prod(mon);
2436 for(;;) {
2437 op = *pch;
2438 if (op != '&' && op != '|' && op != '^')
2439 break;
2440 next();
2441 val2 = expr_prod(mon);
2442 switch(op) {
2443 default:
2444 case '&':
2445 val &= val2;
2446 break;
2447 case '|':
2448 val |= val2;
2449 break;
2450 case '^':
2451 val ^= val2;
2452 break;
2455 return val;
2458 static int64_t expr_sum(Monitor *mon)
2460 int64_t val, val2;
2461 int op;
2463 val = expr_logic(mon);
2464 for(;;) {
2465 op = *pch;
2466 if (op != '+' && op != '-')
2467 break;
2468 next();
2469 val2 = expr_logic(mon);
2470 if (op == '+')
2471 val += val2;
2472 else
2473 val -= val2;
2475 return val;
2478 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2480 pch = *pp;
2481 if (setjmp(expr_env)) {
2482 *pp = pch;
2483 return -1;
2485 while (qemu_isspace(*pch))
2486 pch++;
2487 *pval = expr_sum(mon);
2488 *pp = pch;
2489 return 0;
2492 static int get_str(char *buf, int buf_size, const char **pp)
2494 const char *p;
2495 char *q;
2496 int c;
2498 q = buf;
2499 p = *pp;
2500 while (qemu_isspace(*p))
2501 p++;
2502 if (*p == '\0') {
2503 fail:
2504 *q = '\0';
2505 *pp = p;
2506 return -1;
2508 if (*p == '\"') {
2509 p++;
2510 while (*p != '\0' && *p != '\"') {
2511 if (*p == '\\') {
2512 p++;
2513 c = *p++;
2514 switch(c) {
2515 case 'n':
2516 c = '\n';
2517 break;
2518 case 'r':
2519 c = '\r';
2520 break;
2521 case '\\':
2522 case '\'':
2523 case '\"':
2524 break;
2525 default:
2526 qemu_printf("unsupported escape code: '\\%c'\n", c);
2527 goto fail;
2529 if ((q - buf) < buf_size - 1) {
2530 *q++ = c;
2532 } else {
2533 if ((q - buf) < buf_size - 1) {
2534 *q++ = *p;
2536 p++;
2539 if (*p != '\"') {
2540 qemu_printf("unterminated string\n");
2541 goto fail;
2543 p++;
2544 } else {
2545 while (*p != '\0' && !qemu_isspace(*p)) {
2546 if ((q - buf) < buf_size - 1) {
2547 *q++ = *p;
2549 p++;
2552 *q = '\0';
2553 *pp = p;
2554 return 0;
2558 * Store the command-name in cmdname, and return a pointer to
2559 * the remaining of the command string.
2561 static const char *get_command_name(const char *cmdline,
2562 char *cmdname, size_t nlen)
2564 size_t len;
2565 const char *p, *pstart;
2567 p = cmdline;
2568 while (qemu_isspace(*p))
2569 p++;
2570 if (*p == '\0')
2571 return NULL;
2572 pstart = p;
2573 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2574 p++;
2575 len = p - pstart;
2576 if (len > nlen - 1)
2577 len = nlen - 1;
2578 memcpy(cmdname, pstart, len);
2579 cmdname[len] = '\0';
2580 return p;
2584 * Read key of 'type' into 'key' and return the current
2585 * 'type' pointer.
2587 static char *key_get_info(const char *type, char **key)
2589 size_t len;
2590 char *p, *str;
2592 if (*type == ',')
2593 type++;
2595 p = strchr(type, ':');
2596 if (!p) {
2597 *key = NULL;
2598 return NULL;
2600 len = p - type;
2602 str = qemu_malloc(len + 1);
2603 memcpy(str, type, len);
2604 str[len] = '\0';
2606 *key = str;
2607 return ++p;
2610 static int default_fmt_format = 'x';
2611 static int default_fmt_size = 4;
2613 #define MAX_ARGS 16
2615 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2616 const char *cmdline,
2617 QDict *qdict)
2619 const char *p, *typestr;
2620 int c;
2621 const mon_cmd_t *cmd;
2622 char cmdname[256];
2623 char buf[1024];
2624 char *key;
2626 #ifdef DEBUG
2627 monitor_printf(mon, "command='%s'\n", cmdline);
2628 #endif
2630 /* extract the command name */
2631 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2632 if (!p)
2633 return NULL;
2635 /* find the command */
2636 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2637 if (compare_cmd(cmdname, cmd->name))
2638 break;
2641 if (cmd->name == NULL) {
2642 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2643 return NULL;
2646 /* parse the parameters */
2647 typestr = cmd->args_type;
2648 for(;;) {
2649 typestr = key_get_info(typestr, &key);
2650 if (!typestr)
2651 break;
2652 c = *typestr;
2653 typestr++;
2654 switch(c) {
2655 case 'F':
2656 case 'B':
2657 case 's':
2659 int ret;
2661 while (qemu_isspace(*p))
2662 p++;
2663 if (*typestr == '?') {
2664 typestr++;
2665 if (*p == '\0') {
2666 /* no optional string: NULL argument */
2667 break;
2670 ret = get_str(buf, sizeof(buf), &p);
2671 if (ret < 0) {
2672 switch(c) {
2673 case 'F':
2674 monitor_printf(mon, "%s: filename expected\n",
2675 cmdname);
2676 break;
2677 case 'B':
2678 monitor_printf(mon, "%s: block device name expected\n",
2679 cmdname);
2680 break;
2681 default:
2682 monitor_printf(mon, "%s: string expected\n", cmdname);
2683 break;
2685 goto fail;
2687 qdict_put(qdict, key, qstring_from_str(buf));
2689 break;
2690 case '/':
2692 int count, format, size;
2694 while (qemu_isspace(*p))
2695 p++;
2696 if (*p == '/') {
2697 /* format found */
2698 p++;
2699 count = 1;
2700 if (qemu_isdigit(*p)) {
2701 count = 0;
2702 while (qemu_isdigit(*p)) {
2703 count = count * 10 + (*p - '0');
2704 p++;
2707 size = -1;
2708 format = -1;
2709 for(;;) {
2710 switch(*p) {
2711 case 'o':
2712 case 'd':
2713 case 'u':
2714 case 'x':
2715 case 'i':
2716 case 'c':
2717 format = *p++;
2718 break;
2719 case 'b':
2720 size = 1;
2721 p++;
2722 break;
2723 case 'h':
2724 size = 2;
2725 p++;
2726 break;
2727 case 'w':
2728 size = 4;
2729 p++;
2730 break;
2731 case 'g':
2732 case 'L':
2733 size = 8;
2734 p++;
2735 break;
2736 default:
2737 goto next;
2740 next:
2741 if (*p != '\0' && !qemu_isspace(*p)) {
2742 monitor_printf(mon, "invalid char in format: '%c'\n",
2743 *p);
2744 goto fail;
2746 if (format < 0)
2747 format = default_fmt_format;
2748 if (format != 'i') {
2749 /* for 'i', not specifying a size gives -1 as size */
2750 if (size < 0)
2751 size = default_fmt_size;
2752 default_fmt_size = size;
2754 default_fmt_format = format;
2755 } else {
2756 count = 1;
2757 format = default_fmt_format;
2758 if (format != 'i') {
2759 size = default_fmt_size;
2760 } else {
2761 size = -1;
2764 qdict_put(qdict, "count", qint_from_int(count));
2765 qdict_put(qdict, "format", qint_from_int(format));
2766 qdict_put(qdict, "size", qint_from_int(size));
2768 break;
2769 case 'i':
2770 case 'l':
2772 int64_t val;
2774 while (qemu_isspace(*p))
2775 p++;
2776 if (*typestr == '?' || *typestr == '.') {
2777 if (*typestr == '?') {
2778 if (*p == '\0') {
2779 typestr++;
2780 break;
2782 } else {
2783 if (*p == '.') {
2784 p++;
2785 while (qemu_isspace(*p))
2786 p++;
2787 } else {
2788 typestr++;
2789 break;
2792 typestr++;
2794 if (get_expr(mon, &val, &p))
2795 goto fail;
2796 /* Check if 'i' is greater than 32-bit */
2797 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2798 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2799 monitor_printf(mon, "integer is for 32-bit values\n");
2800 goto fail;
2802 qdict_put(qdict, key, qint_from_int(val));
2804 break;
2805 case '-':
2807 int has_option;
2808 /* option */
2810 c = *typestr++;
2811 if (c == '\0')
2812 goto bad_type;
2813 while (qemu_isspace(*p))
2814 p++;
2815 has_option = 0;
2816 if (*p == '-') {
2817 p++;
2818 if (*p != c) {
2819 monitor_printf(mon, "%s: unsupported option -%c\n",
2820 cmdname, *p);
2821 goto fail;
2823 p++;
2824 has_option = 1;
2826 qdict_put(qdict, key, qint_from_int(has_option));
2828 break;
2829 default:
2830 bad_type:
2831 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2832 goto fail;
2834 qemu_free(key);
2835 key = NULL;
2837 /* check that all arguments were parsed */
2838 while (qemu_isspace(*p))
2839 p++;
2840 if (*p != '\0') {
2841 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2842 cmdname);
2843 goto fail;
2846 return cmd;
2848 fail:
2849 qemu_free(key);
2850 return NULL;
2853 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2855 QDict *qdict;
2856 const mon_cmd_t *cmd;
2858 qdict = qdict_new();
2860 cmd = monitor_parse_command(mon, cmdline, qdict);
2861 if (cmd) {
2862 void (*handler)(Monitor *mon, const QDict *qdict);
2864 qemu_errors_to_mon(mon);
2866 handler = cmd->handler;
2867 handler(mon, qdict);
2869 qemu_errors_to_previous();
2872 QDECREF(qdict);
2875 static void cmd_completion(const char *name, const char *list)
2877 const char *p, *pstart;
2878 char cmd[128];
2879 int len;
2881 p = list;
2882 for(;;) {
2883 pstart = p;
2884 p = strchr(p, '|');
2885 if (!p)
2886 p = pstart + strlen(pstart);
2887 len = p - pstart;
2888 if (len > sizeof(cmd) - 2)
2889 len = sizeof(cmd) - 2;
2890 memcpy(cmd, pstart, len);
2891 cmd[len] = '\0';
2892 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2893 readline_add_completion(cur_mon->rs, cmd);
2895 if (*p == '\0')
2896 break;
2897 p++;
2901 static void file_completion(const char *input)
2903 DIR *ffs;
2904 struct dirent *d;
2905 char path[1024];
2906 char file[1024], file_prefix[1024];
2907 int input_path_len;
2908 const char *p;
2910 p = strrchr(input, '/');
2911 if (!p) {
2912 input_path_len = 0;
2913 pstrcpy(file_prefix, sizeof(file_prefix), input);
2914 pstrcpy(path, sizeof(path), ".");
2915 } else {
2916 input_path_len = p - input + 1;
2917 memcpy(path, input, input_path_len);
2918 if (input_path_len > sizeof(path) - 1)
2919 input_path_len = sizeof(path) - 1;
2920 path[input_path_len] = '\0';
2921 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2923 #ifdef DEBUG_COMPLETION
2924 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2925 input, path, file_prefix);
2926 #endif
2927 ffs = opendir(path);
2928 if (!ffs)
2929 return;
2930 for(;;) {
2931 struct stat sb;
2932 d = readdir(ffs);
2933 if (!d)
2934 break;
2935 if (strstart(d->d_name, file_prefix, NULL)) {
2936 memcpy(file, input, input_path_len);
2937 if (input_path_len < sizeof(file))
2938 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2939 d->d_name);
2940 /* stat the file to find out if it's a directory.
2941 * In that case add a slash to speed up typing long paths
2943 stat(file, &sb);
2944 if(S_ISDIR(sb.st_mode))
2945 pstrcat(file, sizeof(file), "/");
2946 readline_add_completion(cur_mon->rs, file);
2949 closedir(ffs);
2952 static void block_completion_it(void *opaque, BlockDriverState *bs)
2954 const char *name = bdrv_get_device_name(bs);
2955 const char *input = opaque;
2957 if (input[0] == '\0' ||
2958 !strncmp(name, (char *)input, strlen(input))) {
2959 readline_add_completion(cur_mon->rs, name);
2963 /* NOTE: this parser is an approximate form of the real command parser */
2964 static void parse_cmdline(const char *cmdline,
2965 int *pnb_args, char **args)
2967 const char *p;
2968 int nb_args, ret;
2969 char buf[1024];
2971 p = cmdline;
2972 nb_args = 0;
2973 for(;;) {
2974 while (qemu_isspace(*p))
2975 p++;
2976 if (*p == '\0')
2977 break;
2978 if (nb_args >= MAX_ARGS)
2979 break;
2980 ret = get_str(buf, sizeof(buf), &p);
2981 args[nb_args] = qemu_strdup(buf);
2982 nb_args++;
2983 if (ret < 0)
2984 break;
2986 *pnb_args = nb_args;
2989 static const char *next_arg_type(const char *typestr)
2991 const char *p = strchr(typestr, ':');
2992 return (p != NULL ? ++p : typestr);
2995 static void monitor_find_completion(const char *cmdline)
2997 const char *cmdname;
2998 char *args[MAX_ARGS];
2999 int nb_args, i, len;
3000 const char *ptype, *str;
3001 const mon_cmd_t *cmd;
3002 const KeyDef *key;
3004 parse_cmdline(cmdline, &nb_args, args);
3005 #ifdef DEBUG_COMPLETION
3006 for(i = 0; i < nb_args; i++) {
3007 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3009 #endif
3011 /* if the line ends with a space, it means we want to complete the
3012 next arg */
3013 len = strlen(cmdline);
3014 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3015 if (nb_args >= MAX_ARGS)
3016 return;
3017 args[nb_args++] = qemu_strdup("");
3019 if (nb_args <= 1) {
3020 /* command completion */
3021 if (nb_args == 0)
3022 cmdname = "";
3023 else
3024 cmdname = args[0];
3025 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3026 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3027 cmd_completion(cmdname, cmd->name);
3029 } else {
3030 /* find the command */
3031 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3032 if (compare_cmd(args[0], cmd->name))
3033 goto found;
3035 return;
3036 found:
3037 ptype = next_arg_type(cmd->args_type);
3038 for(i = 0; i < nb_args - 2; i++) {
3039 if (*ptype != '\0') {
3040 ptype = next_arg_type(ptype);
3041 while (*ptype == '?')
3042 ptype = next_arg_type(ptype);
3045 str = args[nb_args - 1];
3046 if (*ptype == '-' && ptype[1] != '\0') {
3047 ptype += 2;
3049 switch(*ptype) {
3050 case 'F':
3051 /* file completion */
3052 readline_set_completion_index(cur_mon->rs, strlen(str));
3053 file_completion(str);
3054 break;
3055 case 'B':
3056 /* block device name completion */
3057 readline_set_completion_index(cur_mon->rs, strlen(str));
3058 bdrv_iterate(block_completion_it, (void *)str);
3059 break;
3060 case 's':
3061 /* XXX: more generic ? */
3062 if (!strcmp(cmd->name, "info")) {
3063 readline_set_completion_index(cur_mon->rs, strlen(str));
3064 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3065 cmd_completion(str, cmd->name);
3067 } else if (!strcmp(cmd->name, "sendkey")) {
3068 char *sep = strrchr(str, '-');
3069 if (sep)
3070 str = sep + 1;
3071 readline_set_completion_index(cur_mon->rs, strlen(str));
3072 for(key = key_defs; key->name != NULL; key++) {
3073 cmd_completion(str, key->name);
3075 } else if (!strcmp(cmd->name, "help|?")) {
3076 readline_set_completion_index(cur_mon->rs, strlen(str));
3077 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3078 cmd_completion(str, cmd->name);
3081 break;
3082 default:
3083 break;
3086 for(i = 0; i < nb_args; i++)
3087 qemu_free(args[i]);
3090 static int monitor_can_read(void *opaque)
3092 Monitor *mon = opaque;
3094 return (mon->suspend_cnt == 0) ? 128 : 0;
3097 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3099 Monitor *old_mon = cur_mon;
3100 int i;
3102 cur_mon = opaque;
3104 if (cur_mon->rs) {
3105 for (i = 0; i < size; i++)
3106 readline_handle_byte(cur_mon->rs, buf[i]);
3107 } else {
3108 if (size == 0 || buf[size - 1] != 0)
3109 monitor_printf(cur_mon, "corrupted command\n");
3110 else
3111 monitor_handle_command(cur_mon, (char *)buf);
3114 cur_mon = old_mon;
3117 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3119 monitor_suspend(mon);
3120 monitor_handle_command(mon, cmdline);
3121 monitor_resume(mon);
3124 int monitor_suspend(Monitor *mon)
3126 if (!mon->rs)
3127 return -ENOTTY;
3128 mon->suspend_cnt++;
3129 return 0;
3132 void monitor_resume(Monitor *mon)
3134 if (!mon->rs)
3135 return;
3136 if (--mon->suspend_cnt == 0)
3137 readline_show_prompt(mon->rs);
3140 static void monitor_event(void *opaque, int event)
3142 Monitor *mon = opaque;
3144 switch (event) {
3145 case CHR_EVENT_MUX_IN:
3146 mon->mux_out = 0;
3147 if (mon->reset_seen) {
3148 readline_restart(mon->rs);
3149 monitor_resume(mon);
3150 monitor_flush(mon);
3151 } else {
3152 mon->suspend_cnt = 0;
3154 break;
3156 case CHR_EVENT_MUX_OUT:
3157 if (mon->reset_seen) {
3158 if (mon->suspend_cnt == 0) {
3159 monitor_printf(mon, "\n");
3161 monitor_flush(mon);
3162 monitor_suspend(mon);
3163 } else {
3164 mon->suspend_cnt++;
3166 mon->mux_out = 1;
3167 break;
3169 case CHR_EVENT_RESET:
3170 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3171 "information\n", QEMU_VERSION);
3172 if (!mon->mux_out) {
3173 readline_show_prompt(mon->rs);
3175 mon->reset_seen = 1;
3176 break;
3182 * Local variables:
3183 * c-indent-level: 4
3184 * c-basic-offset: 4
3185 * tab-width: 8
3186 * End:
3189 void monitor_init(CharDriverState *chr, int flags)
3191 static int is_first_init = 1;
3192 Monitor *mon;
3194 if (is_first_init) {
3195 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3196 is_first_init = 0;
3199 mon = qemu_mallocz(sizeof(*mon));
3201 mon->chr = chr;
3202 mon->flags = flags;
3203 if (flags & MONITOR_USE_READLINE) {
3204 mon->rs = readline_init(mon, monitor_find_completion);
3205 monitor_read_command(mon, 0);
3208 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3209 mon);
3211 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3212 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3213 cur_mon = mon;
3216 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3218 BlockDriverState *bs = opaque;
3219 int ret = 0;
3221 if (bdrv_set_key(bs, password) != 0) {
3222 monitor_printf(mon, "invalid password\n");
3223 ret = -EPERM;
3225 if (mon->password_completion_cb)
3226 mon->password_completion_cb(mon->password_opaque, ret);
3228 monitor_read_command(mon, 1);
3231 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3232 BlockDriverCompletionFunc *completion_cb,
3233 void *opaque)
3235 int err;
3237 if (!bdrv_key_required(bs)) {
3238 if (completion_cb)
3239 completion_cb(opaque, 0);
3240 return;
3243 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3244 bdrv_get_encrypted_filename(bs));
3246 mon->password_completion_cb = completion_cb;
3247 mon->password_opaque = opaque;
3249 err = monitor_read_password(mon, bdrv_password_cb, bs);
3251 if (err && completion_cb)
3252 completion_cb(opaque, err);
3255 typedef struct QemuErrorSink QemuErrorSink;
3256 struct QemuErrorSink {
3257 enum {
3258 ERR_SINK_FILE,
3259 ERR_SINK_MONITOR,
3260 } dest;
3261 union {
3262 FILE *fp;
3263 Monitor *mon;
3265 QemuErrorSink *previous;
3268 static QemuErrorSink *qemu_error_sink;
3270 void qemu_errors_to_file(FILE *fp)
3272 QemuErrorSink *sink;
3274 sink = qemu_mallocz(sizeof(*sink));
3275 sink->dest = ERR_SINK_FILE;
3276 sink->fp = fp;
3277 sink->previous = qemu_error_sink;
3278 qemu_error_sink = sink;
3281 void qemu_errors_to_mon(Monitor *mon)
3283 QemuErrorSink *sink;
3285 sink = qemu_mallocz(sizeof(*sink));
3286 sink->dest = ERR_SINK_MONITOR;
3287 sink->mon = mon;
3288 sink->previous = qemu_error_sink;
3289 qemu_error_sink = sink;
3292 void qemu_errors_to_previous(void)
3294 QemuErrorSink *sink;
3296 assert(qemu_error_sink != NULL);
3297 sink = qemu_error_sink;
3298 qemu_error_sink = sink->previous;
3299 qemu_free(sink);
3302 void qemu_error(const char *fmt, ...)
3304 va_list args;
3306 assert(qemu_error_sink != NULL);
3307 switch (qemu_error_sink->dest) {
3308 case ERR_SINK_FILE:
3309 va_start(args, fmt);
3310 vfprintf(qemu_error_sink->fp, fmt, args);
3311 va_end(args);
3312 break;
3313 case ERR_SINK_MONITOR:
3314 va_start(args, fmt);
3315 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3316 va_end(args);
3317 break;