Merge commit '907265dbb67a3cd42f545bd2717c01b24146faba' into upstream-merge
[qemu/qemu-dev-zwu.git] / monitor.c
blobe5aa0d2825d354b422f8b50fa62e1d7a41edcc11
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 "qint.h"
48 #include "qdict.h"
49 #include "qstring.h"
50 #include "exec-all.h"
52 #include "qemu-kvm.h"
54 //#define DEBUG
55 //#define DEBUG_COMPLETION
58 * Supported types:
60 * 'F' filename
61 * 'B' block device name
62 * 's' string (accept optional quote)
63 * 'i' 32 bit integer
64 * 'l' target long (32 or 64 bit)
65 * '/' optional gdb-like print format (like "/10x")
67 * '?' optional type (for all types, except '/')
68 * '.' other form of optional type (for 'i' and 'l')
69 * '-' optional parameter (eg. '-f')
73 typedef struct mon_cmd_t {
74 const char *name;
75 const char *args_type;
76 void *handler;
77 const char *params;
78 const char *help;
79 } mon_cmd_t;
81 /* file descriptors passed via SCM_RIGHTS */
82 typedef struct mon_fd_t mon_fd_t;
83 struct mon_fd_t {
84 char *name;
85 int fd;
86 QLIST_ENTRY(mon_fd_t) next;
89 struct Monitor {
90 CharDriverState *chr;
91 int mux_out;
92 int reset_seen;
93 int flags;
94 int suspend_cnt;
95 uint8_t outbuf[1024];
96 int outbuf_index;
97 ReadLineState *rs;
98 CPUState *mon_cpu;
99 BlockDriverCompletionFunc *password_completion_cb;
100 void *password_opaque;
101 QLIST_HEAD(,mon_fd_t) fds;
102 QLIST_ENTRY(Monitor) entry;
105 static QLIST_HEAD(mon_list, Monitor) mon_list;
107 static const mon_cmd_t mon_cmds[];
108 static const mon_cmd_t info_cmds[];
110 Monitor *cur_mon = NULL;
112 static void monitor_command_cb(Monitor *mon, const char *cmdline,
113 void *opaque);
115 static void monitor_read_command(Monitor *mon, int show_prompt)
117 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
118 if (show_prompt)
119 readline_show_prompt(mon->rs);
122 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
123 void *opaque)
125 if (mon->rs) {
126 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
127 /* prompt is printed on return from the command handler */
128 return 0;
129 } else {
130 monitor_printf(mon, "terminal does not support password prompting\n");
131 return -ENOTTY;
135 void monitor_flush(Monitor *mon)
137 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
138 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
139 mon->outbuf_index = 0;
143 /* flush at every end of line or if the buffer is full */
144 static void monitor_puts(Monitor *mon, const char *str)
146 char c;
148 if (!mon)
149 return;
151 for(;;) {
152 c = *str++;
153 if (c == '\0')
154 break;
155 if (c == '\n')
156 mon->outbuf[mon->outbuf_index++] = '\r';
157 mon->outbuf[mon->outbuf_index++] = c;
158 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
159 || c == '\n')
160 monitor_flush(mon);
164 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
166 char buf[4096];
167 vsnprintf(buf, sizeof(buf), fmt, ap);
168 monitor_puts(mon, buf);
171 void monitor_printf(Monitor *mon, const char *fmt, ...)
173 va_list ap;
174 va_start(ap, fmt);
175 monitor_vprintf(mon, fmt, ap);
176 va_end(ap);
179 void monitor_print_filename(Monitor *mon, const char *filename)
181 int i;
183 for (i = 0; filename[i]; i++) {
184 switch (filename[i]) {
185 case ' ':
186 case '"':
187 case '\\':
188 monitor_printf(mon, "\\%c", filename[i]);
189 break;
190 case '\t':
191 monitor_printf(mon, "\\t");
192 break;
193 case '\r':
194 monitor_printf(mon, "\\r");
195 break;
196 case '\n':
197 monitor_printf(mon, "\\n");
198 break;
199 default:
200 monitor_printf(mon, "%c", filename[i]);
201 break;
206 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
208 va_list ap;
209 va_start(ap, fmt);
210 monitor_vprintf((Monitor *)stream, fmt, ap);
211 va_end(ap);
212 return 0;
215 static int compare_cmd(const char *name, const char *list)
217 const char *p, *pstart;
218 int len;
219 len = strlen(name);
220 p = list;
221 for(;;) {
222 pstart = p;
223 p = strchr(p, '|');
224 if (!p)
225 p = pstart + strlen(pstart);
226 if ((p - pstart) == len && !memcmp(pstart, name, len))
227 return 1;
228 if (*p == '\0')
229 break;
230 p++;
232 return 0;
235 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
236 const char *prefix, const char *name)
238 const mon_cmd_t *cmd;
240 for(cmd = cmds; cmd->name != NULL; cmd++) {
241 if (!name || !strcmp(name, cmd->name))
242 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
243 cmd->params, cmd->help);
247 static void help_cmd(Monitor *mon, const char *name)
249 if (name && !strcmp(name, "info")) {
250 help_cmd_dump(mon, info_cmds, "info ", NULL);
251 } else {
252 help_cmd_dump(mon, mon_cmds, "", name);
253 if (name && !strcmp(name, "log")) {
254 const CPULogItem *item;
255 monitor_printf(mon, "Log items (comma separated):\n");
256 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
257 for(item = cpu_log_items; item->mask != 0; item++) {
258 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
264 static void do_help_cmd(Monitor *mon, const QDict *qdict)
266 help_cmd(mon, qdict_get_try_str(qdict, "name"));
269 static void do_commit(Monitor *mon, const QDict *qdict)
271 int all_devices;
272 DriveInfo *dinfo;
273 const char *device = qdict_get_str(qdict, "device");
275 all_devices = !strcmp(device, "all");
276 QTAILQ_FOREACH(dinfo, &drives, next) {
277 if (!all_devices)
278 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
279 continue;
280 bdrv_commit(dinfo->bdrv);
284 static void do_info(Monitor *mon, const QDict *qdict)
286 const mon_cmd_t *cmd;
287 const char *item = qdict_get_try_str(qdict, "item");
288 void (*handler)(Monitor *);
290 if (!item)
291 goto help;
292 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
293 if (compare_cmd(item, cmd->name))
294 goto found;
296 help:
297 help_cmd(mon, "info");
298 return;
299 found:
300 handler = cmd->handler;
301 handler(mon);
304 static void do_info_version(Monitor *mon)
306 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
309 static void do_info_name(Monitor *mon)
311 if (qemu_name)
312 monitor_printf(mon, "%s\n", qemu_name);
315 #if defined(TARGET_I386)
316 static void do_info_hpet(Monitor *mon)
318 monitor_printf(mon, "HPET is %s by QEMU\n",
319 (no_hpet) ? "disabled" : "enabled");
321 #endif
323 static void do_info_uuid(Monitor *mon)
325 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
326 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
327 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
328 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
329 qemu_uuid[14], qemu_uuid[15]);
332 /* get the current CPU defined by the user */
333 static int mon_set_cpu(int cpu_index)
335 CPUState *env;
337 for(env = first_cpu; env != NULL; env = env->next_cpu) {
338 if (env->cpu_index == cpu_index) {
339 cur_mon->mon_cpu = env;
340 return 0;
343 return -1;
346 static CPUState *mon_get_cpu(void)
348 if (!cur_mon->mon_cpu) {
349 mon_set_cpu(0);
351 cpu_synchronize_state(cur_mon->mon_cpu);
352 return cur_mon->mon_cpu;
355 static void do_info_registers(Monitor *mon)
357 CPUState *env;
358 env = mon_get_cpu();
359 if (!env)
360 return;
361 #ifdef TARGET_I386
362 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
363 X86_DUMP_FPU);
364 #else
365 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
367 #endif
370 static void do_info_cpus(Monitor *mon)
372 CPUState *env;
374 /* just to set the default cpu if not already done */
375 mon_get_cpu();
377 for(env = first_cpu; env != NULL; env = env->next_cpu) {
378 cpu_synchronize_state(env);
379 monitor_printf(mon, "%c CPU #%d:",
380 (env == mon->mon_cpu) ? '*' : ' ',
381 env->cpu_index);
382 #if defined(TARGET_I386)
383 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
384 env->eip + env->segs[R_CS].base);
385 #elif defined(TARGET_PPC)
386 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
387 #elif defined(TARGET_SPARC)
388 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
389 env->pc, env->npc);
390 #elif defined(TARGET_MIPS)
391 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
392 #endif
393 if (env->halted)
394 monitor_printf(mon, " (halted)");
395 monitor_printf(mon," thread_id=%d", env->thread_id);
396 monitor_printf(mon, "\n");
400 static void do_cpu_set(Monitor *mon, const QDict *qdict)
402 int index = qdict_get_int(qdict, "index");
403 if (mon_set_cpu(index) < 0)
404 monitor_printf(mon, "Invalid CPU index\n");
407 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
409 int state;
411 if (!strcmp(status, "online"))
412 state = 1;
413 else if (!strcmp(status, "offline"))
414 state = 0;
415 else {
416 monitor_printf(mon, "invalid status: %s\n", status);
417 return;
419 #if defined(TARGET_I386) || defined(TARGET_X86_64)
420 qemu_system_cpu_hot_add(value, state);
421 #endif
424 static void do_info_jit(Monitor *mon)
426 dump_exec_info((FILE *)mon, monitor_fprintf);
429 static void do_info_history(Monitor *mon)
431 int i;
432 const char *str;
434 if (!mon->rs)
435 return;
436 i = 0;
437 for(;;) {
438 str = readline_get_history(mon->rs, i);
439 if (!str)
440 break;
441 monitor_printf(mon, "%d: '%s'\n", i, str);
442 i++;
446 #if defined(TARGET_PPC)
447 /* XXX: not implemented in other targets */
448 static void do_info_cpu_stats(Monitor *mon)
450 CPUState *env;
452 env = mon_get_cpu();
453 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
455 #endif
457 static void do_quit(Monitor *mon, const QDict *qdict)
459 exit(0);
462 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
464 if (bdrv_is_inserted(bs)) {
465 if (!force) {
466 if (!bdrv_is_removable(bs)) {
467 monitor_printf(mon, "device is not removable\n");
468 return -1;
470 if (bdrv_is_locked(bs)) {
471 monitor_printf(mon, "device is locked\n");
472 return -1;
475 bdrv_close(bs);
477 return 0;
480 static void do_eject(Monitor *mon, const QDict *qdict)
482 BlockDriverState *bs;
483 int force = qdict_get_int(qdict, "force");
484 const char *filename = qdict_get_str(qdict, "filename");
486 bs = bdrv_find(filename);
487 if (!bs) {
488 monitor_printf(mon, "device not found\n");
489 return;
491 eject_device(mon, bs, force);
494 static void do_change_block(Monitor *mon, const char *device,
495 const char *filename, const char *fmt)
497 BlockDriverState *bs;
498 BlockDriver *drv = NULL;
500 bs = bdrv_find(device);
501 if (!bs) {
502 monitor_printf(mon, "device not found\n");
503 return;
505 if (fmt) {
506 drv = bdrv_find_format(fmt);
507 if (!drv) {
508 monitor_printf(mon, "invalid format %s\n", fmt);
509 return;
512 if (eject_device(mon, bs, 0) < 0)
513 return;
514 bdrv_open2(bs, filename, 0, drv);
515 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
518 static void change_vnc_password_cb(Monitor *mon, const char *password,
519 void *opaque)
521 if (vnc_display_password(NULL, password) < 0)
522 monitor_printf(mon, "could not set VNC server password\n");
524 monitor_read_command(mon, 1);
527 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
529 if (strcmp(target, "passwd") == 0 ||
530 strcmp(target, "password") == 0) {
531 if (arg) {
532 char password[9];
533 strncpy(password, arg, sizeof(password));
534 password[sizeof(password) - 1] = '\0';
535 change_vnc_password_cb(mon, password, NULL);
536 } else {
537 monitor_read_password(mon, change_vnc_password_cb, NULL);
539 } else {
540 if (vnc_display_open(NULL, target) < 0)
541 monitor_printf(mon, "could not start VNC server on %s\n", target);
545 static void do_change(Monitor *mon, const QDict *qdict)
547 const char *device = qdict_get_str(qdict, "device");
548 const char *target = qdict_get_str(qdict, "target");
549 const char *arg = qdict_get_try_str(qdict, "arg");
550 if (strcmp(device, "vnc") == 0) {
551 do_change_vnc(mon, target, arg);
552 } else {
553 do_change_block(mon, device, target, arg);
557 static void do_screen_dump(Monitor *mon, const QDict *qdict)
559 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
562 static void do_logfile(Monitor *mon, const QDict *qdict)
564 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
567 static void do_log(Monitor *mon, const QDict *qdict)
569 int mask;
570 const char *items = qdict_get_str(qdict, "items");
572 if (!strcmp(items, "none")) {
573 mask = 0;
574 } else {
575 mask = cpu_str_to_log_mask(items);
576 if (!mask) {
577 help_cmd(mon, "log");
578 return;
581 cpu_set_log(mask);
584 static void do_singlestep(Monitor *mon, const QDict *qdict)
586 const char *option = qdict_get_try_str(qdict, "option");
587 if (!option || !strcmp(option, "on")) {
588 singlestep = 1;
589 } else if (!strcmp(option, "off")) {
590 singlestep = 0;
591 } else {
592 monitor_printf(mon, "unexpected option %s\n", option);
596 static void do_stop(Monitor *mon, const QDict *qdict)
598 vm_stop(EXCP_INTERRUPT);
601 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
603 struct bdrv_iterate_context {
604 Monitor *mon;
605 int err;
608 static void do_cont(Monitor *mon, const QDict *qdict)
610 struct bdrv_iterate_context context = { mon, 0 };
612 bdrv_iterate(encrypted_bdrv_it, &context);
613 /* only resume the vm if all keys are set and valid */
614 if (!context.err)
615 vm_start();
618 static void bdrv_key_cb(void *opaque, int err)
620 Monitor *mon = opaque;
622 /* another key was set successfully, retry to continue */
623 if (!err)
624 do_cont(mon, NULL);
627 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
629 struct bdrv_iterate_context *context = opaque;
631 if (!context->err && bdrv_key_required(bs)) {
632 context->err = -EBUSY;
633 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
634 context->mon);
638 static void do_gdbserver(Monitor *mon, const QDict *qdict)
640 const char *device = qdict_get_try_str(qdict, "device");
641 if (!device)
642 device = "tcp::" DEFAULT_GDBSTUB_PORT;
643 if (gdbserver_start(device) < 0) {
644 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
645 device);
646 } else if (strcmp(device, "none") == 0) {
647 monitor_printf(mon, "Disabled gdbserver\n");
648 } else {
649 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
650 device);
654 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
656 const char *action = qdict_get_str(qdict, "action");
657 if (select_watchdog_action(action) == -1) {
658 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
662 static void monitor_printc(Monitor *mon, int c)
664 monitor_printf(mon, "'");
665 switch(c) {
666 case '\'':
667 monitor_printf(mon, "\\'");
668 break;
669 case '\\':
670 monitor_printf(mon, "\\\\");
671 break;
672 case '\n':
673 monitor_printf(mon, "\\n");
674 break;
675 case '\r':
676 monitor_printf(mon, "\\r");
677 break;
678 default:
679 if (c >= 32 && c <= 126) {
680 monitor_printf(mon, "%c", c);
681 } else {
682 monitor_printf(mon, "\\x%02x", c);
684 break;
686 monitor_printf(mon, "'");
689 static void memory_dump(Monitor *mon, int count, int format, int wsize,
690 target_phys_addr_t addr, int is_physical)
692 CPUState *env;
693 int nb_per_line, l, line_size, i, max_digits, len;
694 uint8_t buf[16];
695 uint64_t v;
697 if (format == 'i') {
698 int flags;
699 flags = 0;
700 env = mon_get_cpu();
701 if (!env && !is_physical)
702 return;
703 #ifdef TARGET_I386
704 if (wsize == 2) {
705 flags = 1;
706 } else if (wsize == 4) {
707 flags = 0;
708 } else {
709 /* as default we use the current CS size */
710 flags = 0;
711 if (env) {
712 #ifdef TARGET_X86_64
713 if ((env->efer & MSR_EFER_LMA) &&
714 (env->segs[R_CS].flags & DESC_L_MASK))
715 flags = 2;
716 else
717 #endif
718 if (!(env->segs[R_CS].flags & DESC_B_MASK))
719 flags = 1;
722 #endif
723 monitor_disas(mon, env, addr, count, is_physical, flags);
724 return;
727 len = wsize * count;
728 if (wsize == 1)
729 line_size = 8;
730 else
731 line_size = 16;
732 nb_per_line = line_size / wsize;
733 max_digits = 0;
735 switch(format) {
736 case 'o':
737 max_digits = (wsize * 8 + 2) / 3;
738 break;
739 default:
740 case 'x':
741 max_digits = (wsize * 8) / 4;
742 break;
743 case 'u':
744 case 'd':
745 max_digits = (wsize * 8 * 10 + 32) / 33;
746 break;
747 case 'c':
748 wsize = 1;
749 break;
752 while (len > 0) {
753 if (is_physical)
754 monitor_printf(mon, TARGET_FMT_plx ":", addr);
755 else
756 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
757 l = len;
758 if (l > line_size)
759 l = line_size;
760 if (is_physical) {
761 cpu_physical_memory_rw(addr, buf, l, 0);
762 } else {
763 env = mon_get_cpu();
764 if (!env)
765 break;
766 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
767 monitor_printf(mon, " Cannot access memory\n");
768 break;
771 i = 0;
772 while (i < l) {
773 switch(wsize) {
774 default:
775 case 1:
776 v = ldub_raw(buf + i);
777 break;
778 case 2:
779 v = lduw_raw(buf + i);
780 break;
781 case 4:
782 v = (uint32_t)ldl_raw(buf + i);
783 break;
784 case 8:
785 v = ldq_raw(buf + i);
786 break;
788 monitor_printf(mon, " ");
789 switch(format) {
790 case 'o':
791 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
792 break;
793 case 'x':
794 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
795 break;
796 case 'u':
797 monitor_printf(mon, "%*" PRIu64, max_digits, v);
798 break;
799 case 'd':
800 monitor_printf(mon, "%*" PRId64, max_digits, v);
801 break;
802 case 'c':
803 monitor_printc(mon, v);
804 break;
806 i += wsize;
808 monitor_printf(mon, "\n");
809 addr += l;
810 len -= l;
814 static void do_memory_dump(Monitor *mon, const QDict *qdict)
816 int count = qdict_get_int(qdict, "count");
817 int format = qdict_get_int(qdict, "format");
818 int size = qdict_get_int(qdict, "size");
819 target_long addr = qdict_get_int(qdict, "addr");
821 memory_dump(mon, count, format, size, addr, 0);
824 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
826 int count = qdict_get_int(qdict, "count");
827 int format = qdict_get_int(qdict, "format");
828 int size = qdict_get_int(qdict, "size");
829 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
831 memory_dump(mon, count, format, size, addr, 1);
834 static void do_print(Monitor *mon, const QDict *qdict)
836 int format = qdict_get_int(qdict, "format");
837 target_phys_addr_t val = qdict_get_int(qdict, "val");
839 #if TARGET_PHYS_ADDR_BITS == 32
840 switch(format) {
841 case 'o':
842 monitor_printf(mon, "%#o", val);
843 break;
844 case 'x':
845 monitor_printf(mon, "%#x", val);
846 break;
847 case 'u':
848 monitor_printf(mon, "%u", val);
849 break;
850 default:
851 case 'd':
852 monitor_printf(mon, "%d", val);
853 break;
854 case 'c':
855 monitor_printc(mon, val);
856 break;
858 #else
859 switch(format) {
860 case 'o':
861 monitor_printf(mon, "%#" PRIo64, val);
862 break;
863 case 'x':
864 monitor_printf(mon, "%#" PRIx64, val);
865 break;
866 case 'u':
867 monitor_printf(mon, "%" PRIu64, val);
868 break;
869 default:
870 case 'd':
871 monitor_printf(mon, "%" PRId64, val);
872 break;
873 case 'c':
874 monitor_printc(mon, val);
875 break;
877 #endif
878 monitor_printf(mon, "\n");
881 static void do_memory_save(Monitor *mon, const QDict *qdict)
883 FILE *f;
884 uint32_t size = qdict_get_int(qdict, "size");
885 const char *filename = qdict_get_str(qdict, "filename");
886 target_long addr = qdict_get_int(qdict, "val");
887 uint32_t l;
888 CPUState *env;
889 uint8_t buf[1024];
891 env = mon_get_cpu();
892 if (!env)
893 return;
895 f = fopen(filename, "wb");
896 if (!f) {
897 monitor_printf(mon, "could not open '%s'\n", filename);
898 return;
900 while (size != 0) {
901 l = sizeof(buf);
902 if (l > size)
903 l = size;
904 cpu_memory_rw_debug(env, addr, buf, l, 0);
905 fwrite(buf, 1, l, f);
906 addr += l;
907 size -= l;
909 fclose(f);
912 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
914 FILE *f;
915 uint32_t l;
916 uint8_t buf[1024];
917 uint32_t size = qdict_get_int(qdict, "size");
918 const char *filename = qdict_get_str(qdict, "filename");
919 target_phys_addr_t addr = qdict_get_int(qdict, "val");
921 f = fopen(filename, "wb");
922 if (!f) {
923 monitor_printf(mon, "could not open '%s'\n", filename);
924 return;
926 while (size != 0) {
927 l = sizeof(buf);
928 if (l > size)
929 l = size;
930 cpu_physical_memory_rw(addr, buf, l, 0);
931 fwrite(buf, 1, l, f);
932 fflush(f);
933 addr += l;
934 size -= l;
936 fclose(f);
939 static void do_sum(Monitor *mon, const QDict *qdict)
941 uint32_t addr;
942 uint8_t buf[1];
943 uint16_t sum;
944 uint32_t start = qdict_get_int(qdict, "start");
945 uint32_t size = qdict_get_int(qdict, "size");
947 sum = 0;
948 for(addr = start; addr < (start + size); addr++) {
949 cpu_physical_memory_rw(addr, buf, 1, 0);
950 /* BSD sum algorithm ('sum' Unix command) */
951 sum = (sum >> 1) | (sum << 15);
952 sum += buf[0];
954 monitor_printf(mon, "%05d\n", sum);
957 typedef struct {
958 int keycode;
959 const char *name;
960 } KeyDef;
962 static const KeyDef key_defs[] = {
963 { 0x2a, "shift" },
964 { 0x36, "shift_r" },
966 { 0x38, "alt" },
967 { 0xb8, "alt_r" },
968 { 0x64, "altgr" },
969 { 0xe4, "altgr_r" },
970 { 0x1d, "ctrl" },
971 { 0x9d, "ctrl_r" },
973 { 0xdd, "menu" },
975 { 0x01, "esc" },
977 { 0x02, "1" },
978 { 0x03, "2" },
979 { 0x04, "3" },
980 { 0x05, "4" },
981 { 0x06, "5" },
982 { 0x07, "6" },
983 { 0x08, "7" },
984 { 0x09, "8" },
985 { 0x0a, "9" },
986 { 0x0b, "0" },
987 { 0x0c, "minus" },
988 { 0x0d, "equal" },
989 { 0x0e, "backspace" },
991 { 0x0f, "tab" },
992 { 0x10, "q" },
993 { 0x11, "w" },
994 { 0x12, "e" },
995 { 0x13, "r" },
996 { 0x14, "t" },
997 { 0x15, "y" },
998 { 0x16, "u" },
999 { 0x17, "i" },
1000 { 0x18, "o" },
1001 { 0x19, "p" },
1003 { 0x1c, "ret" },
1005 { 0x1e, "a" },
1006 { 0x1f, "s" },
1007 { 0x20, "d" },
1008 { 0x21, "f" },
1009 { 0x22, "g" },
1010 { 0x23, "h" },
1011 { 0x24, "j" },
1012 { 0x25, "k" },
1013 { 0x26, "l" },
1015 { 0x2c, "z" },
1016 { 0x2d, "x" },
1017 { 0x2e, "c" },
1018 { 0x2f, "v" },
1019 { 0x30, "b" },
1020 { 0x31, "n" },
1021 { 0x32, "m" },
1022 { 0x33, "comma" },
1023 { 0x34, "dot" },
1024 { 0x35, "slash" },
1026 { 0x37, "asterisk" },
1028 { 0x39, "spc" },
1029 { 0x3a, "caps_lock" },
1030 { 0x3b, "f1" },
1031 { 0x3c, "f2" },
1032 { 0x3d, "f3" },
1033 { 0x3e, "f4" },
1034 { 0x3f, "f5" },
1035 { 0x40, "f6" },
1036 { 0x41, "f7" },
1037 { 0x42, "f8" },
1038 { 0x43, "f9" },
1039 { 0x44, "f10" },
1040 { 0x45, "num_lock" },
1041 { 0x46, "scroll_lock" },
1043 { 0xb5, "kp_divide" },
1044 { 0x37, "kp_multiply" },
1045 { 0x4a, "kp_subtract" },
1046 { 0x4e, "kp_add" },
1047 { 0x9c, "kp_enter" },
1048 { 0x53, "kp_decimal" },
1049 { 0x54, "sysrq" },
1051 { 0x52, "kp_0" },
1052 { 0x4f, "kp_1" },
1053 { 0x50, "kp_2" },
1054 { 0x51, "kp_3" },
1055 { 0x4b, "kp_4" },
1056 { 0x4c, "kp_5" },
1057 { 0x4d, "kp_6" },
1058 { 0x47, "kp_7" },
1059 { 0x48, "kp_8" },
1060 { 0x49, "kp_9" },
1062 { 0x56, "<" },
1064 { 0x57, "f11" },
1065 { 0x58, "f12" },
1067 { 0xb7, "print" },
1069 { 0xc7, "home" },
1070 { 0xc9, "pgup" },
1071 { 0xd1, "pgdn" },
1072 { 0xcf, "end" },
1074 { 0xcb, "left" },
1075 { 0xc8, "up" },
1076 { 0xd0, "down" },
1077 { 0xcd, "right" },
1079 { 0xd2, "insert" },
1080 { 0xd3, "delete" },
1081 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1082 { 0xf0, "stop" },
1083 { 0xf1, "again" },
1084 { 0xf2, "props" },
1085 { 0xf3, "undo" },
1086 { 0xf4, "front" },
1087 { 0xf5, "copy" },
1088 { 0xf6, "open" },
1089 { 0xf7, "paste" },
1090 { 0xf8, "find" },
1091 { 0xf9, "cut" },
1092 { 0xfa, "lf" },
1093 { 0xfb, "help" },
1094 { 0xfc, "meta_l" },
1095 { 0xfd, "meta_r" },
1096 { 0xfe, "compose" },
1097 #endif
1098 { 0, NULL },
1101 static int get_keycode(const char *key)
1103 const KeyDef *p;
1104 char *endp;
1105 int ret;
1107 for(p = key_defs; p->name != NULL; p++) {
1108 if (!strcmp(key, p->name))
1109 return p->keycode;
1111 if (strstart(key, "0x", NULL)) {
1112 ret = strtoul(key, &endp, 0);
1113 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1114 return ret;
1116 return -1;
1119 #define MAX_KEYCODES 16
1120 static uint8_t keycodes[MAX_KEYCODES];
1121 static int nb_pending_keycodes;
1122 static QEMUTimer *key_timer;
1124 static void release_keys(void *opaque)
1126 int keycode;
1128 while (nb_pending_keycodes > 0) {
1129 nb_pending_keycodes--;
1130 keycode = keycodes[nb_pending_keycodes];
1131 if (keycode & 0x80)
1132 kbd_put_keycode(0xe0);
1133 kbd_put_keycode(keycode | 0x80);
1137 static void do_sendkey(Monitor *mon, const QDict *qdict)
1139 char keyname_buf[16];
1140 char *separator;
1141 int keyname_len, keycode, i;
1142 const char *string = qdict_get_str(qdict, "string");
1143 int has_hold_time = qdict_haskey(qdict, "hold_time");
1144 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1146 if (nb_pending_keycodes > 0) {
1147 qemu_del_timer(key_timer);
1148 release_keys(NULL);
1150 if (!has_hold_time)
1151 hold_time = 100;
1152 i = 0;
1153 while (1) {
1154 separator = strchr(string, '-');
1155 keyname_len = separator ? separator - string : strlen(string);
1156 if (keyname_len > 0) {
1157 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1158 if (keyname_len > sizeof(keyname_buf) - 1) {
1159 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1160 return;
1162 if (i == MAX_KEYCODES) {
1163 monitor_printf(mon, "too many keys\n");
1164 return;
1166 keyname_buf[keyname_len] = 0;
1167 keycode = get_keycode(keyname_buf);
1168 if (keycode < 0) {
1169 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1170 return;
1172 keycodes[i++] = keycode;
1174 if (!separator)
1175 break;
1176 string = separator + 1;
1178 nb_pending_keycodes = i;
1179 /* key down events */
1180 for (i = 0; i < nb_pending_keycodes; i++) {
1181 keycode = keycodes[i];
1182 if (keycode & 0x80)
1183 kbd_put_keycode(0xe0);
1184 kbd_put_keycode(keycode & 0x7f);
1186 /* delayed key up events */
1187 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1188 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1191 static int mouse_button_state;
1193 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1195 int dx, dy, dz;
1196 const char *dx_str = qdict_get_str(qdict, "dx_str");
1197 const char *dy_str = qdict_get_str(qdict, "dy_str");
1198 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1199 dx = strtol(dx_str, NULL, 0);
1200 dy = strtol(dy_str, NULL, 0);
1201 dz = 0;
1202 if (dz_str)
1203 dz = strtol(dz_str, NULL, 0);
1204 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1207 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1209 int button_state = qdict_get_int(qdict, "button_state");
1210 mouse_button_state = button_state;
1211 kbd_mouse_event(0, 0, 0, mouse_button_state);
1214 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1216 int size = qdict_get_int(qdict, "size");
1217 int addr = qdict_get_int(qdict, "addr");
1218 int has_index = qdict_haskey(qdict, "index");
1219 uint32_t val;
1220 int suffix;
1222 if (has_index) {
1223 int index = qdict_get_int(qdict, "index");
1224 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1225 addr++;
1227 addr &= 0xffff;
1229 switch(size) {
1230 default:
1231 case 1:
1232 val = cpu_inb(addr);
1233 suffix = 'b';
1234 break;
1235 case 2:
1236 val = cpu_inw(addr);
1237 suffix = 'w';
1238 break;
1239 case 4:
1240 val = cpu_inl(addr);
1241 suffix = 'l';
1242 break;
1244 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1245 suffix, addr, size * 2, val);
1248 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1250 int size = qdict_get_int(qdict, "size");
1251 int addr = qdict_get_int(qdict, "addr");
1252 int val = qdict_get_int(qdict, "val");
1254 addr &= IOPORTS_MASK;
1256 switch (size) {
1257 default:
1258 case 1:
1259 cpu_outb(addr, val);
1260 break;
1261 case 2:
1262 cpu_outw(addr, val);
1263 break;
1264 case 4:
1265 cpu_outl(addr, val);
1266 break;
1270 static void do_boot_set(Monitor *mon, const QDict *qdict)
1272 int res;
1273 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1275 res = qemu_boot_set(bootdevice);
1276 if (res == 0) {
1277 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1278 } else if (res > 0) {
1279 monitor_printf(mon, "setting boot device list failed\n");
1280 } else {
1281 monitor_printf(mon, "no function defined to set boot device list for "
1282 "this architecture\n");
1286 static void do_system_reset(Monitor *mon, const QDict *qdict)
1288 qemu_system_reset_request();
1291 static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1293 qemu_system_powerdown_request();
1296 #if defined(TARGET_I386)
1297 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1299 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1300 addr,
1301 pte & mask,
1302 pte & PG_GLOBAL_MASK ? 'G' : '-',
1303 pte & PG_PSE_MASK ? 'P' : '-',
1304 pte & PG_DIRTY_MASK ? 'D' : '-',
1305 pte & PG_ACCESSED_MASK ? 'A' : '-',
1306 pte & PG_PCD_MASK ? 'C' : '-',
1307 pte & PG_PWT_MASK ? 'T' : '-',
1308 pte & PG_USER_MASK ? 'U' : '-',
1309 pte & PG_RW_MASK ? 'W' : '-');
1312 static void tlb_info(Monitor *mon)
1314 CPUState *env;
1315 int l1, l2;
1316 uint32_t pgd, pde, pte;
1318 env = mon_get_cpu();
1319 if (!env)
1320 return;
1322 if (!(env->cr[0] & CR0_PG_MASK)) {
1323 monitor_printf(mon, "PG disabled\n");
1324 return;
1326 pgd = env->cr[3] & ~0xfff;
1327 for(l1 = 0; l1 < 1024; l1++) {
1328 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1329 pde = le32_to_cpu(pde);
1330 if (pde & PG_PRESENT_MASK) {
1331 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1332 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1333 } else {
1334 for(l2 = 0; l2 < 1024; l2++) {
1335 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1336 (uint8_t *)&pte, 4);
1337 pte = le32_to_cpu(pte);
1338 if (pte & PG_PRESENT_MASK) {
1339 print_pte(mon, (l1 << 22) + (l2 << 12),
1340 pte & ~PG_PSE_MASK,
1341 ~0xfff);
1349 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1350 uint32_t end, int prot)
1352 int prot1;
1353 prot1 = *plast_prot;
1354 if (prot != prot1) {
1355 if (*pstart != -1) {
1356 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1357 *pstart, end, end - *pstart,
1358 prot1 & PG_USER_MASK ? 'u' : '-',
1359 'r',
1360 prot1 & PG_RW_MASK ? 'w' : '-');
1362 if (prot != 0)
1363 *pstart = end;
1364 else
1365 *pstart = -1;
1366 *plast_prot = prot;
1370 static void mem_info(Monitor *mon)
1372 CPUState *env;
1373 int l1, l2, prot, last_prot;
1374 uint32_t pgd, pde, pte, start, end;
1376 env = mon_get_cpu();
1377 if (!env)
1378 return;
1380 if (!(env->cr[0] & CR0_PG_MASK)) {
1381 monitor_printf(mon, "PG disabled\n");
1382 return;
1384 pgd = env->cr[3] & ~0xfff;
1385 last_prot = 0;
1386 start = -1;
1387 for(l1 = 0; l1 < 1024; l1++) {
1388 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1389 pde = le32_to_cpu(pde);
1390 end = l1 << 22;
1391 if (pde & PG_PRESENT_MASK) {
1392 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1393 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1394 mem_print(mon, &start, &last_prot, end, prot);
1395 } else {
1396 for(l2 = 0; l2 < 1024; l2++) {
1397 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1398 (uint8_t *)&pte, 4);
1399 pte = le32_to_cpu(pte);
1400 end = (l1 << 22) + (l2 << 12);
1401 if (pte & PG_PRESENT_MASK) {
1402 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1403 } else {
1404 prot = 0;
1406 mem_print(mon, &start, &last_prot, end, prot);
1409 } else {
1410 prot = 0;
1411 mem_print(mon, &start, &last_prot, end, prot);
1415 #endif
1417 #if defined(TARGET_SH4)
1419 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1421 monitor_printf(mon, " tlb%i:\t"
1422 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1423 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1424 "dirty=%hhu writethrough=%hhu\n",
1425 idx,
1426 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1427 tlb->v, tlb->sh, tlb->c, tlb->pr,
1428 tlb->d, tlb->wt);
1431 static void tlb_info(Monitor *mon)
1433 CPUState *env = mon_get_cpu();
1434 int i;
1436 monitor_printf (mon, "ITLB:\n");
1437 for (i = 0 ; i < ITLB_SIZE ; i++)
1438 print_tlb (mon, i, &env->itlb[i]);
1439 monitor_printf (mon, "UTLB:\n");
1440 for (i = 0 ; i < UTLB_SIZE ; i++)
1441 print_tlb (mon, i, &env->utlb[i]);
1444 #endif
1446 static void do_info_kvm(Monitor *mon)
1448 #if defined(USE_KVM) || defined(CONFIG_KVM)
1449 monitor_printf(mon, "kvm support: ");
1450 if (kvm_enabled())
1451 monitor_printf(mon, "enabled\n");
1452 else
1453 monitor_printf(mon, "disabled\n");
1454 #else
1455 monitor_printf(mon, "kvm support: not compiled\n");
1456 #endif
1459 static void do_info_numa(Monitor *mon)
1461 int i;
1462 CPUState *env;
1464 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1465 for (i = 0; i < nb_numa_nodes; i++) {
1466 monitor_printf(mon, "node %d cpus:", i);
1467 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1468 if (env->numa_node == i) {
1469 monitor_printf(mon, " %d", env->cpu_index);
1472 monitor_printf(mon, "\n");
1473 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1474 node_mem[i] >> 20);
1478 #ifdef CONFIG_PROFILER
1480 int64_t qemu_time;
1481 int64_t dev_time;
1483 static void do_info_profile(Monitor *mon)
1485 int64_t total;
1486 total = qemu_time;
1487 if (total == 0)
1488 total = 1;
1489 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1490 dev_time, dev_time / (double)get_ticks_per_sec());
1491 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1492 qemu_time, qemu_time / (double)get_ticks_per_sec());
1493 qemu_time = 0;
1494 dev_time = 0;
1496 #else
1497 static void do_info_profile(Monitor *mon)
1499 monitor_printf(mon, "Internal profiler not compiled\n");
1501 #endif
1503 /* Capture support */
1504 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1506 static void do_info_capture(Monitor *mon)
1508 int i;
1509 CaptureState *s;
1511 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1512 monitor_printf(mon, "[%d]: ", i);
1513 s->ops.info (s->opaque);
1517 #ifdef HAS_AUDIO
1518 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1520 int i;
1521 int n = qdict_get_int(qdict, "n");
1522 CaptureState *s;
1524 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1525 if (i == n) {
1526 s->ops.destroy (s->opaque);
1527 QLIST_REMOVE (s, entries);
1528 qemu_free (s);
1529 return;
1534 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1536 const char *path = qdict_get_str(qdict, "path");
1537 int has_freq = qdict_haskey(qdict, "freq");
1538 int freq = qdict_get_try_int(qdict, "freq", -1);
1539 int has_bits = qdict_haskey(qdict, "bits");
1540 int bits = qdict_get_try_int(qdict, "bits", -1);
1541 int has_channels = qdict_haskey(qdict, "nchannels");
1542 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1543 CaptureState *s;
1545 s = qemu_mallocz (sizeof (*s));
1547 freq = has_freq ? freq : 44100;
1548 bits = has_bits ? bits : 16;
1549 nchannels = has_channels ? nchannels : 2;
1551 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1552 monitor_printf(mon, "Faied to add wave capture\n");
1553 qemu_free (s);
1555 QLIST_INSERT_HEAD (&capture_head, s, entries);
1557 #endif
1559 #if defined(TARGET_I386)
1560 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1562 CPUState *env;
1563 int cpu_index = qdict_get_int(qdict, "cpu_index");
1565 for (env = first_cpu; env != NULL; env = env->next_cpu)
1566 if (env->cpu_index == cpu_index) {
1567 if (kvm_enabled())
1568 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1569 else
1570 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1571 break;
1574 #endif
1576 static void do_info_status(Monitor *mon)
1578 if (vm_running) {
1579 if (singlestep) {
1580 monitor_printf(mon, "VM status: running (single step mode)\n");
1581 } else {
1582 monitor_printf(mon, "VM status: running\n");
1584 } else
1585 monitor_printf(mon, "VM status: paused\n");
1589 static void do_balloon(Monitor *mon, const QDict *qdict)
1591 int value = qdict_get_int(qdict, "value");
1592 ram_addr_t target = value;
1593 qemu_balloon(target << 20);
1596 static void do_info_balloon(Monitor *mon)
1598 ram_addr_t actual;
1600 actual = qemu_balloon_status();
1601 if (kvm_enabled() && !kvm_has_sync_mmu())
1602 monitor_printf(mon, "Using KVM without synchronous MMU, "
1603 "ballooning disabled\n");
1604 else if (actual == 0)
1605 monitor_printf(mon, "Ballooning not activated in VM\n");
1606 else
1607 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1610 static qemu_acl *find_acl(Monitor *mon, const char *name)
1612 qemu_acl *acl = qemu_acl_find(name);
1614 if (!acl) {
1615 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1617 return acl;
1620 static void do_acl_show(Monitor *mon, const QDict *qdict)
1622 const char *aclname = qdict_get_str(qdict, "aclname");
1623 qemu_acl *acl = find_acl(mon, aclname);
1624 qemu_acl_entry *entry;
1625 int i = 0;
1627 if (acl) {
1628 monitor_printf(mon, "policy: %s\n",
1629 acl->defaultDeny ? "deny" : "allow");
1630 QTAILQ_FOREACH(entry, &acl->entries, next) {
1631 i++;
1632 monitor_printf(mon, "%d: %s %s\n", i,
1633 entry->deny ? "deny" : "allow", entry->match);
1638 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1640 const char *aclname = qdict_get_str(qdict, "aclname");
1641 qemu_acl *acl = find_acl(mon, aclname);
1643 if (acl) {
1644 qemu_acl_reset(acl);
1645 monitor_printf(mon, "acl: removed all rules\n");
1649 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1651 const char *aclname = qdict_get_str(qdict, "aclname");
1652 const char *policy = qdict_get_str(qdict, "policy");
1653 qemu_acl *acl = find_acl(mon, aclname);
1655 if (acl) {
1656 if (strcmp(policy, "allow") == 0) {
1657 acl->defaultDeny = 0;
1658 monitor_printf(mon, "acl: policy set to 'allow'\n");
1659 } else if (strcmp(policy, "deny") == 0) {
1660 acl->defaultDeny = 1;
1661 monitor_printf(mon, "acl: policy set to 'deny'\n");
1662 } else {
1663 monitor_printf(mon, "acl: unknown policy '%s', "
1664 "expected 'deny' or 'allow'\n", policy);
1669 static void do_acl_add(Monitor *mon, const QDict *qdict)
1671 const char *aclname = qdict_get_str(qdict, "aclname");
1672 const char *match = qdict_get_str(qdict, "match");
1673 const char *policy = qdict_get_str(qdict, "policy");
1674 int has_index = qdict_haskey(qdict, "index");
1675 int index = qdict_get_try_int(qdict, "index", -1);
1676 qemu_acl *acl = find_acl(mon, aclname);
1677 int deny, ret;
1679 if (acl) {
1680 if (strcmp(policy, "allow") == 0) {
1681 deny = 0;
1682 } else if (strcmp(policy, "deny") == 0) {
1683 deny = 1;
1684 } else {
1685 monitor_printf(mon, "acl: unknown policy '%s', "
1686 "expected 'deny' or 'allow'\n", policy);
1687 return;
1689 if (has_index)
1690 ret = qemu_acl_insert(acl, deny, match, index);
1691 else
1692 ret = qemu_acl_append(acl, deny, match);
1693 if (ret < 0)
1694 monitor_printf(mon, "acl: unable to add acl entry\n");
1695 else
1696 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1700 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1702 const char *aclname = qdict_get_str(qdict, "aclname");
1703 const char *match = qdict_get_str(qdict, "match");
1704 qemu_acl *acl = find_acl(mon, aclname);
1705 int ret;
1707 if (acl) {
1708 ret = qemu_acl_remove(acl, match);
1709 if (ret < 0)
1710 monitor_printf(mon, "acl: no matching acl entry\n");
1711 else
1712 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1716 #if defined(TARGET_I386)
1717 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1719 CPUState *cenv;
1720 int cpu_index = qdict_get_int(qdict, "cpu_index");
1721 int bank = qdict_get_int(qdict, "bank");
1722 uint64_t status = qdict_get_int(qdict, "status");
1723 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1724 uint64_t addr = qdict_get_int(qdict, "addr");
1725 uint64_t misc = qdict_get_int(qdict, "misc");
1727 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1728 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1729 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1730 break;
1733 #endif
1735 static void do_getfd(Monitor *mon, const QDict *qdict)
1737 const char *fdname = qdict_get_str(qdict, "fdname");
1738 mon_fd_t *monfd;
1739 int fd;
1741 fd = qemu_chr_get_msgfd(mon->chr);
1742 if (fd == -1) {
1743 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1744 return;
1747 if (qemu_isdigit(fdname[0])) {
1748 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1749 return;
1752 fd = dup(fd);
1753 if (fd == -1) {
1754 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1755 strerror(errno));
1756 return;
1759 QLIST_FOREACH(monfd, &mon->fds, next) {
1760 if (strcmp(monfd->name, fdname) != 0) {
1761 continue;
1764 close(monfd->fd);
1765 monfd->fd = fd;
1766 return;
1769 monfd = qemu_mallocz(sizeof(mon_fd_t));
1770 monfd->name = qemu_strdup(fdname);
1771 monfd->fd = fd;
1773 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1776 static void do_closefd(Monitor *mon, const QDict *qdict)
1778 const char *fdname = qdict_get_str(qdict, "fdname");
1779 mon_fd_t *monfd;
1781 QLIST_FOREACH(monfd, &mon->fds, next) {
1782 if (strcmp(monfd->name, fdname) != 0) {
1783 continue;
1786 QLIST_REMOVE(monfd, next);
1787 close(monfd->fd);
1788 qemu_free(monfd->name);
1789 qemu_free(monfd);
1790 return;
1793 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1794 fdname);
1797 static void do_loadvm(Monitor *mon, const QDict *qdict)
1799 int saved_vm_running = vm_running;
1800 const char *name = qdict_get_str(qdict, "name");
1802 vm_stop(0);
1804 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1805 vm_start();
1808 int monitor_get_fd(Monitor *mon, const char *fdname)
1810 mon_fd_t *monfd;
1812 QLIST_FOREACH(monfd, &mon->fds, next) {
1813 int fd;
1815 if (strcmp(monfd->name, fdname) != 0) {
1816 continue;
1819 fd = monfd->fd;
1821 /* caller takes ownership of fd */
1822 QLIST_REMOVE(monfd, next);
1823 qemu_free(monfd->name);
1824 qemu_free(monfd);
1826 return fd;
1829 return -1;
1832 static const mon_cmd_t mon_cmds[] = {
1833 #include "qemu-monitor.h"
1834 { NULL, NULL, },
1837 /* Please update qemu-monitor.hx when adding or changing commands */
1838 static const mon_cmd_t info_cmds[] = {
1839 { "version", "", do_info_version,
1840 "", "show the version of QEMU" },
1841 { "network", "", do_info_network,
1842 "", "show the network state" },
1843 { "chardev", "", qemu_chr_info,
1844 "", "show the character devices" },
1845 { "block", "", bdrv_info,
1846 "", "show the block devices" },
1847 { "blockstats", "", bdrv_info_stats,
1848 "", "show block device statistics" },
1849 { "registers", "", do_info_registers,
1850 "", "show the cpu registers" },
1851 { "cpus", "", do_info_cpus,
1852 "", "show infos for each CPU" },
1853 { "history", "", do_info_history,
1854 "", "show the command line history", },
1855 { "irq", "", irq_info,
1856 "", "show the interrupts statistics (if available)", },
1857 { "pic", "", pic_info,
1858 "", "show i8259 (PIC) state", },
1859 { "pci", "", pci_info,
1860 "", "show PCI info", },
1861 #if defined(TARGET_I386) || defined(TARGET_SH4)
1862 { "tlb", "", tlb_info,
1863 "", "show virtual to physical memory mappings", },
1864 #endif
1865 #if defined(TARGET_I386)
1866 { "mem", "", mem_info,
1867 "", "show the active virtual memory mappings", },
1868 { "hpet", "", do_info_hpet,
1869 "", "show state of HPET", },
1870 #endif
1871 { "jit", "", do_info_jit,
1872 "", "show dynamic compiler info", },
1873 { "kvm", "", do_info_kvm,
1874 "", "show KVM information", },
1875 { "numa", "", do_info_numa,
1876 "", "show NUMA information", },
1877 { "usb", "", usb_info,
1878 "", "show guest USB devices", },
1879 { "usbhost", "", usb_host_info,
1880 "", "show host USB devices", },
1881 { "profile", "", do_info_profile,
1882 "", "show profiling information", },
1883 { "capture", "", do_info_capture,
1884 "", "show capture information" },
1885 { "snapshots", "", do_info_snapshots,
1886 "", "show the currently saved VM snapshots" },
1887 { "status", "", do_info_status,
1888 "", "show the current VM status (running|paused)" },
1889 { "pcmcia", "", pcmcia_info,
1890 "", "show guest PCMCIA status" },
1891 { "mice", "", do_info_mice,
1892 "", "show which guest mouse is receiving events" },
1893 { "vnc", "", do_info_vnc,
1894 "", "show the vnc server status"},
1895 { "name", "", do_info_name,
1896 "", "show the current VM name" },
1897 { "uuid", "", do_info_uuid,
1898 "", "show the current VM UUID" },
1899 #if defined(TARGET_PPC)
1900 { "cpustats", "", do_info_cpu_stats,
1901 "", "show CPU statistics", },
1902 #endif
1903 #if defined(CONFIG_SLIRP)
1904 { "usernet", "", do_info_usernet,
1905 "", "show user network stack connection states", },
1906 #endif
1907 { "migrate", "", do_info_migrate, "", "show migration status" },
1908 { "balloon", "", do_info_balloon,
1909 "", "show balloon information" },
1910 { "qtree", "", do_info_qtree,
1911 "", "show device tree" },
1912 { "qdm", "", do_info_qdm,
1913 "", "show qdev device model list" },
1914 { NULL, NULL, },
1917 /*******************************************************************/
1919 static const char *pch;
1920 static jmp_buf expr_env;
1922 #define MD_TLONG 0
1923 #define MD_I32 1
1925 typedef struct MonitorDef {
1926 const char *name;
1927 int offset;
1928 target_long (*get_value)(const struct MonitorDef *md, int val);
1929 int type;
1930 } MonitorDef;
1932 #if defined(TARGET_I386)
1933 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1935 CPUState *env = mon_get_cpu();
1936 if (!env)
1937 return 0;
1938 return env->eip + env->segs[R_CS].base;
1940 #endif
1942 #if defined(TARGET_PPC)
1943 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1945 CPUState *env = mon_get_cpu();
1946 unsigned int u;
1947 int i;
1949 if (!env)
1950 return 0;
1952 u = 0;
1953 for (i = 0; i < 8; i++)
1954 u |= env->crf[i] << (32 - (4 * i));
1956 return u;
1959 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1961 CPUState *env = mon_get_cpu();
1962 if (!env)
1963 return 0;
1964 return env->msr;
1967 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1969 CPUState *env = mon_get_cpu();
1970 if (!env)
1971 return 0;
1972 return env->xer;
1975 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1977 CPUState *env = mon_get_cpu();
1978 if (!env)
1979 return 0;
1980 return cpu_ppc_load_decr(env);
1983 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1985 CPUState *env = mon_get_cpu();
1986 if (!env)
1987 return 0;
1988 return cpu_ppc_load_tbu(env);
1991 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1993 CPUState *env = mon_get_cpu();
1994 if (!env)
1995 return 0;
1996 return cpu_ppc_load_tbl(env);
1998 #endif
2000 #if defined(TARGET_SPARC)
2001 #ifndef TARGET_SPARC64
2002 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2004 CPUState *env = mon_get_cpu();
2005 if (!env)
2006 return 0;
2007 return GET_PSR(env);
2009 #endif
2011 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2013 CPUState *env = mon_get_cpu();
2014 if (!env)
2015 return 0;
2016 return env->regwptr[val];
2018 #endif
2020 static const MonitorDef monitor_defs[] = {
2021 #ifdef TARGET_I386
2023 #define SEG(name, seg) \
2024 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2025 { name ".base", offsetof(CPUState, segs[seg].base) },\
2026 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2028 { "eax", offsetof(CPUState, regs[0]) },
2029 { "ecx", offsetof(CPUState, regs[1]) },
2030 { "edx", offsetof(CPUState, regs[2]) },
2031 { "ebx", offsetof(CPUState, regs[3]) },
2032 { "esp|sp", offsetof(CPUState, regs[4]) },
2033 { "ebp|fp", offsetof(CPUState, regs[5]) },
2034 { "esi", offsetof(CPUState, regs[6]) },
2035 { "edi", offsetof(CPUState, regs[7]) },
2036 #ifdef TARGET_X86_64
2037 { "r8", offsetof(CPUState, regs[8]) },
2038 { "r9", offsetof(CPUState, regs[9]) },
2039 { "r10", offsetof(CPUState, regs[10]) },
2040 { "r11", offsetof(CPUState, regs[11]) },
2041 { "r12", offsetof(CPUState, regs[12]) },
2042 { "r13", offsetof(CPUState, regs[13]) },
2043 { "r14", offsetof(CPUState, regs[14]) },
2044 { "r15", offsetof(CPUState, regs[15]) },
2045 #endif
2046 { "eflags", offsetof(CPUState, eflags) },
2047 { "eip", offsetof(CPUState, eip) },
2048 SEG("cs", R_CS)
2049 SEG("ds", R_DS)
2050 SEG("es", R_ES)
2051 SEG("ss", R_SS)
2052 SEG("fs", R_FS)
2053 SEG("gs", R_GS)
2054 { "pc", 0, monitor_get_pc, },
2055 #elif defined(TARGET_PPC)
2056 /* General purpose registers */
2057 { "r0", offsetof(CPUState, gpr[0]) },
2058 { "r1", offsetof(CPUState, gpr[1]) },
2059 { "r2", offsetof(CPUState, gpr[2]) },
2060 { "r3", offsetof(CPUState, gpr[3]) },
2061 { "r4", offsetof(CPUState, gpr[4]) },
2062 { "r5", offsetof(CPUState, gpr[5]) },
2063 { "r6", offsetof(CPUState, gpr[6]) },
2064 { "r7", offsetof(CPUState, gpr[7]) },
2065 { "r8", offsetof(CPUState, gpr[8]) },
2066 { "r9", offsetof(CPUState, gpr[9]) },
2067 { "r10", offsetof(CPUState, gpr[10]) },
2068 { "r11", offsetof(CPUState, gpr[11]) },
2069 { "r12", offsetof(CPUState, gpr[12]) },
2070 { "r13", offsetof(CPUState, gpr[13]) },
2071 { "r14", offsetof(CPUState, gpr[14]) },
2072 { "r15", offsetof(CPUState, gpr[15]) },
2073 { "r16", offsetof(CPUState, gpr[16]) },
2074 { "r17", offsetof(CPUState, gpr[17]) },
2075 { "r18", offsetof(CPUState, gpr[18]) },
2076 { "r19", offsetof(CPUState, gpr[19]) },
2077 { "r20", offsetof(CPUState, gpr[20]) },
2078 { "r21", offsetof(CPUState, gpr[21]) },
2079 { "r22", offsetof(CPUState, gpr[22]) },
2080 { "r23", offsetof(CPUState, gpr[23]) },
2081 { "r24", offsetof(CPUState, gpr[24]) },
2082 { "r25", offsetof(CPUState, gpr[25]) },
2083 { "r26", offsetof(CPUState, gpr[26]) },
2084 { "r27", offsetof(CPUState, gpr[27]) },
2085 { "r28", offsetof(CPUState, gpr[28]) },
2086 { "r29", offsetof(CPUState, gpr[29]) },
2087 { "r30", offsetof(CPUState, gpr[30]) },
2088 { "r31", offsetof(CPUState, gpr[31]) },
2089 /* Floating point registers */
2090 { "f0", offsetof(CPUState, fpr[0]) },
2091 { "f1", offsetof(CPUState, fpr[1]) },
2092 { "f2", offsetof(CPUState, fpr[2]) },
2093 { "f3", offsetof(CPUState, fpr[3]) },
2094 { "f4", offsetof(CPUState, fpr[4]) },
2095 { "f5", offsetof(CPUState, fpr[5]) },
2096 { "f6", offsetof(CPUState, fpr[6]) },
2097 { "f7", offsetof(CPUState, fpr[7]) },
2098 { "f8", offsetof(CPUState, fpr[8]) },
2099 { "f9", offsetof(CPUState, fpr[9]) },
2100 { "f10", offsetof(CPUState, fpr[10]) },
2101 { "f11", offsetof(CPUState, fpr[11]) },
2102 { "f12", offsetof(CPUState, fpr[12]) },
2103 { "f13", offsetof(CPUState, fpr[13]) },
2104 { "f14", offsetof(CPUState, fpr[14]) },
2105 { "f15", offsetof(CPUState, fpr[15]) },
2106 { "f16", offsetof(CPUState, fpr[16]) },
2107 { "f17", offsetof(CPUState, fpr[17]) },
2108 { "f18", offsetof(CPUState, fpr[18]) },
2109 { "f19", offsetof(CPUState, fpr[19]) },
2110 { "f20", offsetof(CPUState, fpr[20]) },
2111 { "f21", offsetof(CPUState, fpr[21]) },
2112 { "f22", offsetof(CPUState, fpr[22]) },
2113 { "f23", offsetof(CPUState, fpr[23]) },
2114 { "f24", offsetof(CPUState, fpr[24]) },
2115 { "f25", offsetof(CPUState, fpr[25]) },
2116 { "f26", offsetof(CPUState, fpr[26]) },
2117 { "f27", offsetof(CPUState, fpr[27]) },
2118 { "f28", offsetof(CPUState, fpr[28]) },
2119 { "f29", offsetof(CPUState, fpr[29]) },
2120 { "f30", offsetof(CPUState, fpr[30]) },
2121 { "f31", offsetof(CPUState, fpr[31]) },
2122 { "fpscr", offsetof(CPUState, fpscr) },
2123 /* Next instruction pointer */
2124 { "nip|pc", offsetof(CPUState, nip) },
2125 { "lr", offsetof(CPUState, lr) },
2126 { "ctr", offsetof(CPUState, ctr) },
2127 { "decr", 0, &monitor_get_decr, },
2128 { "ccr", 0, &monitor_get_ccr, },
2129 /* Machine state register */
2130 { "msr", 0, &monitor_get_msr, },
2131 { "xer", 0, &monitor_get_xer, },
2132 { "tbu", 0, &monitor_get_tbu, },
2133 { "tbl", 0, &monitor_get_tbl, },
2134 #if defined(TARGET_PPC64)
2135 /* Address space register */
2136 { "asr", offsetof(CPUState, asr) },
2137 #endif
2138 /* Segment registers */
2139 { "sdr1", offsetof(CPUState, sdr1) },
2140 { "sr0", offsetof(CPUState, sr[0]) },
2141 { "sr1", offsetof(CPUState, sr[1]) },
2142 { "sr2", offsetof(CPUState, sr[2]) },
2143 { "sr3", offsetof(CPUState, sr[3]) },
2144 { "sr4", offsetof(CPUState, sr[4]) },
2145 { "sr5", offsetof(CPUState, sr[5]) },
2146 { "sr6", offsetof(CPUState, sr[6]) },
2147 { "sr7", offsetof(CPUState, sr[7]) },
2148 { "sr8", offsetof(CPUState, sr[8]) },
2149 { "sr9", offsetof(CPUState, sr[9]) },
2150 { "sr10", offsetof(CPUState, sr[10]) },
2151 { "sr11", offsetof(CPUState, sr[11]) },
2152 { "sr12", offsetof(CPUState, sr[12]) },
2153 { "sr13", offsetof(CPUState, sr[13]) },
2154 { "sr14", offsetof(CPUState, sr[14]) },
2155 { "sr15", offsetof(CPUState, sr[15]) },
2156 /* Too lazy to put BATs and SPRs ... */
2157 #elif defined(TARGET_SPARC)
2158 { "g0", offsetof(CPUState, gregs[0]) },
2159 { "g1", offsetof(CPUState, gregs[1]) },
2160 { "g2", offsetof(CPUState, gregs[2]) },
2161 { "g3", offsetof(CPUState, gregs[3]) },
2162 { "g4", offsetof(CPUState, gregs[4]) },
2163 { "g5", offsetof(CPUState, gregs[5]) },
2164 { "g6", offsetof(CPUState, gregs[6]) },
2165 { "g7", offsetof(CPUState, gregs[7]) },
2166 { "o0", 0, monitor_get_reg },
2167 { "o1", 1, monitor_get_reg },
2168 { "o2", 2, monitor_get_reg },
2169 { "o3", 3, monitor_get_reg },
2170 { "o4", 4, monitor_get_reg },
2171 { "o5", 5, monitor_get_reg },
2172 { "o6", 6, monitor_get_reg },
2173 { "o7", 7, monitor_get_reg },
2174 { "l0", 8, monitor_get_reg },
2175 { "l1", 9, monitor_get_reg },
2176 { "l2", 10, monitor_get_reg },
2177 { "l3", 11, monitor_get_reg },
2178 { "l4", 12, monitor_get_reg },
2179 { "l5", 13, monitor_get_reg },
2180 { "l6", 14, monitor_get_reg },
2181 { "l7", 15, monitor_get_reg },
2182 { "i0", 16, monitor_get_reg },
2183 { "i1", 17, monitor_get_reg },
2184 { "i2", 18, monitor_get_reg },
2185 { "i3", 19, monitor_get_reg },
2186 { "i4", 20, monitor_get_reg },
2187 { "i5", 21, monitor_get_reg },
2188 { "i6", 22, monitor_get_reg },
2189 { "i7", 23, monitor_get_reg },
2190 { "pc", offsetof(CPUState, pc) },
2191 { "npc", offsetof(CPUState, npc) },
2192 { "y", offsetof(CPUState, y) },
2193 #ifndef TARGET_SPARC64
2194 { "psr", 0, &monitor_get_psr, },
2195 { "wim", offsetof(CPUState, wim) },
2196 #endif
2197 { "tbr", offsetof(CPUState, tbr) },
2198 { "fsr", offsetof(CPUState, fsr) },
2199 { "f0", offsetof(CPUState, fpr[0]) },
2200 { "f1", offsetof(CPUState, fpr[1]) },
2201 { "f2", offsetof(CPUState, fpr[2]) },
2202 { "f3", offsetof(CPUState, fpr[3]) },
2203 { "f4", offsetof(CPUState, fpr[4]) },
2204 { "f5", offsetof(CPUState, fpr[5]) },
2205 { "f6", offsetof(CPUState, fpr[6]) },
2206 { "f7", offsetof(CPUState, fpr[7]) },
2207 { "f8", offsetof(CPUState, fpr[8]) },
2208 { "f9", offsetof(CPUState, fpr[9]) },
2209 { "f10", offsetof(CPUState, fpr[10]) },
2210 { "f11", offsetof(CPUState, fpr[11]) },
2211 { "f12", offsetof(CPUState, fpr[12]) },
2212 { "f13", offsetof(CPUState, fpr[13]) },
2213 { "f14", offsetof(CPUState, fpr[14]) },
2214 { "f15", offsetof(CPUState, fpr[15]) },
2215 { "f16", offsetof(CPUState, fpr[16]) },
2216 { "f17", offsetof(CPUState, fpr[17]) },
2217 { "f18", offsetof(CPUState, fpr[18]) },
2218 { "f19", offsetof(CPUState, fpr[19]) },
2219 { "f20", offsetof(CPUState, fpr[20]) },
2220 { "f21", offsetof(CPUState, fpr[21]) },
2221 { "f22", offsetof(CPUState, fpr[22]) },
2222 { "f23", offsetof(CPUState, fpr[23]) },
2223 { "f24", offsetof(CPUState, fpr[24]) },
2224 { "f25", offsetof(CPUState, fpr[25]) },
2225 { "f26", offsetof(CPUState, fpr[26]) },
2226 { "f27", offsetof(CPUState, fpr[27]) },
2227 { "f28", offsetof(CPUState, fpr[28]) },
2228 { "f29", offsetof(CPUState, fpr[29]) },
2229 { "f30", offsetof(CPUState, fpr[30]) },
2230 { "f31", offsetof(CPUState, fpr[31]) },
2231 #ifdef TARGET_SPARC64
2232 { "f32", offsetof(CPUState, fpr[32]) },
2233 { "f34", offsetof(CPUState, fpr[34]) },
2234 { "f36", offsetof(CPUState, fpr[36]) },
2235 { "f38", offsetof(CPUState, fpr[38]) },
2236 { "f40", offsetof(CPUState, fpr[40]) },
2237 { "f42", offsetof(CPUState, fpr[42]) },
2238 { "f44", offsetof(CPUState, fpr[44]) },
2239 { "f46", offsetof(CPUState, fpr[46]) },
2240 { "f48", offsetof(CPUState, fpr[48]) },
2241 { "f50", offsetof(CPUState, fpr[50]) },
2242 { "f52", offsetof(CPUState, fpr[52]) },
2243 { "f54", offsetof(CPUState, fpr[54]) },
2244 { "f56", offsetof(CPUState, fpr[56]) },
2245 { "f58", offsetof(CPUState, fpr[58]) },
2246 { "f60", offsetof(CPUState, fpr[60]) },
2247 { "f62", offsetof(CPUState, fpr[62]) },
2248 { "asi", offsetof(CPUState, asi) },
2249 { "pstate", offsetof(CPUState, pstate) },
2250 { "cansave", offsetof(CPUState, cansave) },
2251 { "canrestore", offsetof(CPUState, canrestore) },
2252 { "otherwin", offsetof(CPUState, otherwin) },
2253 { "wstate", offsetof(CPUState, wstate) },
2254 { "cleanwin", offsetof(CPUState, cleanwin) },
2255 { "fprs", offsetof(CPUState, fprs) },
2256 #endif
2257 #endif
2258 { NULL },
2261 static void expr_error(Monitor *mon, const char *msg)
2263 monitor_printf(mon, "%s\n", msg);
2264 longjmp(expr_env, 1);
2267 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2268 static int get_monitor_def(target_long *pval, const char *name)
2270 const MonitorDef *md;
2271 void *ptr;
2273 for(md = monitor_defs; md->name != NULL; md++) {
2274 if (compare_cmd(name, md->name)) {
2275 if (md->get_value) {
2276 *pval = md->get_value(md, md->offset);
2277 } else {
2278 CPUState *env = mon_get_cpu();
2279 if (!env)
2280 return -2;
2281 ptr = (uint8_t *)env + md->offset;
2282 switch(md->type) {
2283 case MD_I32:
2284 *pval = *(int32_t *)ptr;
2285 break;
2286 case MD_TLONG:
2287 *pval = *(target_long *)ptr;
2288 break;
2289 default:
2290 *pval = 0;
2291 break;
2294 return 0;
2297 return -1;
2300 static void next(void)
2302 if (*pch != '\0') {
2303 pch++;
2304 while (qemu_isspace(*pch))
2305 pch++;
2309 static int64_t expr_sum(Monitor *mon);
2311 static int64_t expr_unary(Monitor *mon)
2313 int64_t n;
2314 char *p;
2315 int ret;
2317 switch(*pch) {
2318 case '+':
2319 next();
2320 n = expr_unary(mon);
2321 break;
2322 case '-':
2323 next();
2324 n = -expr_unary(mon);
2325 break;
2326 case '~':
2327 next();
2328 n = ~expr_unary(mon);
2329 break;
2330 case '(':
2331 next();
2332 n = expr_sum(mon);
2333 if (*pch != ')') {
2334 expr_error(mon, "')' expected");
2336 next();
2337 break;
2338 case '\'':
2339 pch++;
2340 if (*pch == '\0')
2341 expr_error(mon, "character constant expected");
2342 n = *pch;
2343 pch++;
2344 if (*pch != '\'')
2345 expr_error(mon, "missing terminating \' character");
2346 next();
2347 break;
2348 case '$':
2350 char buf[128], *q;
2351 target_long reg=0;
2353 pch++;
2354 q = buf;
2355 while ((*pch >= 'a' && *pch <= 'z') ||
2356 (*pch >= 'A' && *pch <= 'Z') ||
2357 (*pch >= '0' && *pch <= '9') ||
2358 *pch == '_' || *pch == '.') {
2359 if ((q - buf) < sizeof(buf) - 1)
2360 *q++ = *pch;
2361 pch++;
2363 while (qemu_isspace(*pch))
2364 pch++;
2365 *q = 0;
2366 ret = get_monitor_def(&reg, buf);
2367 if (ret == -1)
2368 expr_error(mon, "unknown register");
2369 else if (ret == -2)
2370 expr_error(mon, "no cpu defined");
2371 n = reg;
2373 break;
2374 case '\0':
2375 expr_error(mon, "unexpected end of expression");
2376 n = 0;
2377 break;
2378 default:
2379 #if TARGET_PHYS_ADDR_BITS > 32
2380 n = strtoull(pch, &p, 0);
2381 #else
2382 n = strtoul(pch, &p, 0);
2383 #endif
2384 if (pch == p) {
2385 expr_error(mon, "invalid char in expression");
2387 pch = p;
2388 while (qemu_isspace(*pch))
2389 pch++;
2390 break;
2392 return n;
2396 static int64_t expr_prod(Monitor *mon)
2398 int64_t val, val2;
2399 int op;
2401 val = expr_unary(mon);
2402 for(;;) {
2403 op = *pch;
2404 if (op != '*' && op != '/' && op != '%')
2405 break;
2406 next();
2407 val2 = expr_unary(mon);
2408 switch(op) {
2409 default:
2410 case '*':
2411 val *= val2;
2412 break;
2413 case '/':
2414 case '%':
2415 if (val2 == 0)
2416 expr_error(mon, "division by zero");
2417 if (op == '/')
2418 val /= val2;
2419 else
2420 val %= val2;
2421 break;
2424 return val;
2427 static int64_t expr_logic(Monitor *mon)
2429 int64_t val, val2;
2430 int op;
2432 val = expr_prod(mon);
2433 for(;;) {
2434 op = *pch;
2435 if (op != '&' && op != '|' && op != '^')
2436 break;
2437 next();
2438 val2 = expr_prod(mon);
2439 switch(op) {
2440 default:
2441 case '&':
2442 val &= val2;
2443 break;
2444 case '|':
2445 val |= val2;
2446 break;
2447 case '^':
2448 val ^= val2;
2449 break;
2452 return val;
2455 static int64_t expr_sum(Monitor *mon)
2457 int64_t val, val2;
2458 int op;
2460 val = expr_logic(mon);
2461 for(;;) {
2462 op = *pch;
2463 if (op != '+' && op != '-')
2464 break;
2465 next();
2466 val2 = expr_logic(mon);
2467 if (op == '+')
2468 val += val2;
2469 else
2470 val -= val2;
2472 return val;
2475 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2477 pch = *pp;
2478 if (setjmp(expr_env)) {
2479 *pp = pch;
2480 return -1;
2482 while (qemu_isspace(*pch))
2483 pch++;
2484 *pval = expr_sum(mon);
2485 *pp = pch;
2486 return 0;
2489 static int get_str(char *buf, int buf_size, const char **pp)
2491 const char *p;
2492 char *q;
2493 int c;
2495 q = buf;
2496 p = *pp;
2497 while (qemu_isspace(*p))
2498 p++;
2499 if (*p == '\0') {
2500 fail:
2501 *q = '\0';
2502 *pp = p;
2503 return -1;
2505 if (*p == '\"') {
2506 p++;
2507 while (*p != '\0' && *p != '\"') {
2508 if (*p == '\\') {
2509 p++;
2510 c = *p++;
2511 switch(c) {
2512 case 'n':
2513 c = '\n';
2514 break;
2515 case 'r':
2516 c = '\r';
2517 break;
2518 case '\\':
2519 case '\'':
2520 case '\"':
2521 break;
2522 default:
2523 qemu_printf("unsupported escape code: '\\%c'\n", c);
2524 goto fail;
2526 if ((q - buf) < buf_size - 1) {
2527 *q++ = c;
2529 } else {
2530 if ((q - buf) < buf_size - 1) {
2531 *q++ = *p;
2533 p++;
2536 if (*p != '\"') {
2537 qemu_printf("unterminated string\n");
2538 goto fail;
2540 p++;
2541 } else {
2542 while (*p != '\0' && !qemu_isspace(*p)) {
2543 if ((q - buf) < buf_size - 1) {
2544 *q++ = *p;
2546 p++;
2549 *q = '\0';
2550 *pp = p;
2551 return 0;
2555 * Store the command-name in cmdname, and return a pointer to
2556 * the remaining of the command string.
2558 static const char *get_command_name(const char *cmdline,
2559 char *cmdname, size_t nlen)
2561 size_t len;
2562 const char *p, *pstart;
2564 p = cmdline;
2565 while (qemu_isspace(*p))
2566 p++;
2567 if (*p == '\0')
2568 return NULL;
2569 pstart = p;
2570 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2571 p++;
2572 len = p - pstart;
2573 if (len > nlen - 1)
2574 len = nlen - 1;
2575 memcpy(cmdname, pstart, len);
2576 cmdname[len] = '\0';
2577 return p;
2581 * Read key of 'type' into 'key' and return the current
2582 * 'type' pointer.
2584 static char *key_get_info(const char *type, char **key)
2586 size_t len;
2587 char *p, *str;
2589 if (*type == ',')
2590 type++;
2592 p = strchr(type, ':');
2593 if (!p) {
2594 *key = NULL;
2595 return NULL;
2597 len = p - type;
2599 str = qemu_malloc(len + 1);
2600 memcpy(str, type, len);
2601 str[len] = '\0';
2603 *key = str;
2604 return ++p;
2607 static int default_fmt_format = 'x';
2608 static int default_fmt_size = 4;
2610 #define MAX_ARGS 16
2612 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2613 const char *cmdline,
2614 QDict *qdict)
2616 const char *p, *typestr;
2617 int c;
2618 const mon_cmd_t *cmd;
2619 char cmdname[256];
2620 char buf[1024];
2621 char *key;
2623 #ifdef DEBUG
2624 monitor_printf(mon, "command='%s'\n", cmdline);
2625 #endif
2627 /* extract the command name */
2628 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2629 if (!p)
2630 return NULL;
2632 /* find the command */
2633 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2634 if (compare_cmd(cmdname, cmd->name))
2635 break;
2638 if (cmd->name == NULL) {
2639 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2640 return NULL;
2643 /* parse the parameters */
2644 typestr = cmd->args_type;
2645 for(;;) {
2646 typestr = key_get_info(typestr, &key);
2647 if (!typestr)
2648 break;
2649 c = *typestr;
2650 typestr++;
2651 switch(c) {
2652 case 'F':
2653 case 'B':
2654 case 's':
2656 int ret;
2658 while (qemu_isspace(*p))
2659 p++;
2660 if (*typestr == '?') {
2661 typestr++;
2662 if (*p == '\0') {
2663 /* no optional string: NULL argument */
2664 break;
2667 ret = get_str(buf, sizeof(buf), &p);
2668 if (ret < 0) {
2669 switch(c) {
2670 case 'F':
2671 monitor_printf(mon, "%s: filename expected\n",
2672 cmdname);
2673 break;
2674 case 'B':
2675 monitor_printf(mon, "%s: block device name expected\n",
2676 cmdname);
2677 break;
2678 default:
2679 monitor_printf(mon, "%s: string expected\n", cmdname);
2680 break;
2682 goto fail;
2684 qdict_put(qdict, key, qstring_from_str(buf));
2686 break;
2687 case '/':
2689 int count, format, size;
2691 while (qemu_isspace(*p))
2692 p++;
2693 if (*p == '/') {
2694 /* format found */
2695 p++;
2696 count = 1;
2697 if (qemu_isdigit(*p)) {
2698 count = 0;
2699 while (qemu_isdigit(*p)) {
2700 count = count * 10 + (*p - '0');
2701 p++;
2704 size = -1;
2705 format = -1;
2706 for(;;) {
2707 switch(*p) {
2708 case 'o':
2709 case 'd':
2710 case 'u':
2711 case 'x':
2712 case 'i':
2713 case 'c':
2714 format = *p++;
2715 break;
2716 case 'b':
2717 size = 1;
2718 p++;
2719 break;
2720 case 'h':
2721 size = 2;
2722 p++;
2723 break;
2724 case 'w':
2725 size = 4;
2726 p++;
2727 break;
2728 case 'g':
2729 case 'L':
2730 size = 8;
2731 p++;
2732 break;
2733 default:
2734 goto next;
2737 next:
2738 if (*p != '\0' && !qemu_isspace(*p)) {
2739 monitor_printf(mon, "invalid char in format: '%c'\n",
2740 *p);
2741 goto fail;
2743 if (format < 0)
2744 format = default_fmt_format;
2745 if (format != 'i') {
2746 /* for 'i', not specifying a size gives -1 as size */
2747 if (size < 0)
2748 size = default_fmt_size;
2749 default_fmt_size = size;
2751 default_fmt_format = format;
2752 } else {
2753 count = 1;
2754 format = default_fmt_format;
2755 if (format != 'i') {
2756 size = default_fmt_size;
2757 } else {
2758 size = -1;
2761 qdict_put(qdict, "count", qint_from_int(count));
2762 qdict_put(qdict, "format", qint_from_int(format));
2763 qdict_put(qdict, "size", qint_from_int(size));
2765 break;
2766 case 'i':
2767 case 'l':
2769 int64_t val;
2771 while (qemu_isspace(*p))
2772 p++;
2773 if (*typestr == '?' || *typestr == '.') {
2774 if (*typestr == '?') {
2775 if (*p == '\0') {
2776 typestr++;
2777 break;
2779 } else {
2780 if (*p == '.') {
2781 p++;
2782 while (qemu_isspace(*p))
2783 p++;
2784 } else {
2785 typestr++;
2786 break;
2789 typestr++;
2791 if (get_expr(mon, &val, &p))
2792 goto fail;
2793 /* Check if 'i' is greater than 32-bit */
2794 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2795 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2796 monitor_printf(mon, "integer is for 32-bit values\n");
2797 goto fail;
2799 qdict_put(qdict, key, qint_from_int(val));
2801 break;
2802 case '-':
2804 int has_option;
2805 /* option */
2807 c = *typestr++;
2808 if (c == '\0')
2809 goto bad_type;
2810 while (qemu_isspace(*p))
2811 p++;
2812 has_option = 0;
2813 if (*p == '-') {
2814 p++;
2815 if (*p != c) {
2816 monitor_printf(mon, "%s: unsupported option -%c\n",
2817 cmdname, *p);
2818 goto fail;
2820 p++;
2821 has_option = 1;
2823 qdict_put(qdict, key, qint_from_int(has_option));
2825 break;
2826 default:
2827 bad_type:
2828 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2829 goto fail;
2831 qemu_free(key);
2832 key = NULL;
2834 /* check that all arguments were parsed */
2835 while (qemu_isspace(*p))
2836 p++;
2837 if (*p != '\0') {
2838 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2839 cmdname);
2840 goto fail;
2843 return cmd;
2845 fail:
2846 qemu_free(key);
2847 return NULL;
2850 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2852 QDict *qdict;
2853 const mon_cmd_t *cmd;
2855 qdict = qdict_new();
2857 cmd = monitor_parse_command(mon, cmdline, qdict);
2858 if (cmd) {
2859 void (*handler)(Monitor *mon, const QDict *qdict);
2861 qemu_errors_to_mon(mon);
2863 handler = cmd->handler;
2864 handler(mon, qdict);
2866 qemu_errors_to_previous();
2869 QDECREF(qdict);
2872 static void cmd_completion(const char *name, const char *list)
2874 const char *p, *pstart;
2875 char cmd[128];
2876 int len;
2878 p = list;
2879 for(;;) {
2880 pstart = p;
2881 p = strchr(p, '|');
2882 if (!p)
2883 p = pstart + strlen(pstart);
2884 len = p - pstart;
2885 if (len > sizeof(cmd) - 2)
2886 len = sizeof(cmd) - 2;
2887 memcpy(cmd, pstart, len);
2888 cmd[len] = '\0';
2889 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2890 readline_add_completion(cur_mon->rs, cmd);
2892 if (*p == '\0')
2893 break;
2894 p++;
2898 static void file_completion(const char *input)
2900 DIR *ffs;
2901 struct dirent *d;
2902 char path[1024];
2903 char file[1024], file_prefix[1024];
2904 int input_path_len;
2905 const char *p;
2907 p = strrchr(input, '/');
2908 if (!p) {
2909 input_path_len = 0;
2910 pstrcpy(file_prefix, sizeof(file_prefix), input);
2911 pstrcpy(path, sizeof(path), ".");
2912 } else {
2913 input_path_len = p - input + 1;
2914 memcpy(path, input, input_path_len);
2915 if (input_path_len > sizeof(path) - 1)
2916 input_path_len = sizeof(path) - 1;
2917 path[input_path_len] = '\0';
2918 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2920 #ifdef DEBUG_COMPLETION
2921 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2922 input, path, file_prefix);
2923 #endif
2924 ffs = opendir(path);
2925 if (!ffs)
2926 return;
2927 for(;;) {
2928 struct stat sb;
2929 d = readdir(ffs);
2930 if (!d)
2931 break;
2932 if (strstart(d->d_name, file_prefix, NULL)) {
2933 memcpy(file, input, input_path_len);
2934 if (input_path_len < sizeof(file))
2935 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2936 d->d_name);
2937 /* stat the file to find out if it's a directory.
2938 * In that case add a slash to speed up typing long paths
2940 stat(file, &sb);
2941 if(S_ISDIR(sb.st_mode))
2942 pstrcat(file, sizeof(file), "/");
2943 readline_add_completion(cur_mon->rs, file);
2946 closedir(ffs);
2949 static void block_completion_it(void *opaque, BlockDriverState *bs)
2951 const char *name = bdrv_get_device_name(bs);
2952 const char *input = opaque;
2954 if (input[0] == '\0' ||
2955 !strncmp(name, (char *)input, strlen(input))) {
2956 readline_add_completion(cur_mon->rs, name);
2960 /* NOTE: this parser is an approximate form of the real command parser */
2961 static void parse_cmdline(const char *cmdline,
2962 int *pnb_args, char **args)
2964 const char *p;
2965 int nb_args, ret;
2966 char buf[1024];
2968 p = cmdline;
2969 nb_args = 0;
2970 for(;;) {
2971 while (qemu_isspace(*p))
2972 p++;
2973 if (*p == '\0')
2974 break;
2975 if (nb_args >= MAX_ARGS)
2976 break;
2977 ret = get_str(buf, sizeof(buf), &p);
2978 args[nb_args] = qemu_strdup(buf);
2979 nb_args++;
2980 if (ret < 0)
2981 break;
2983 *pnb_args = nb_args;
2986 static const char *next_arg_type(const char *typestr)
2988 const char *p = strchr(typestr, ':');
2989 return (p != NULL ? ++p : typestr);
2992 static void monitor_find_completion(const char *cmdline)
2994 const char *cmdname;
2995 char *args[MAX_ARGS];
2996 int nb_args, i, len;
2997 const char *ptype, *str;
2998 const mon_cmd_t *cmd;
2999 const KeyDef *key;
3001 parse_cmdline(cmdline, &nb_args, args);
3002 #ifdef DEBUG_COMPLETION
3003 for(i = 0; i < nb_args; i++) {
3004 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3006 #endif
3008 /* if the line ends with a space, it means we want to complete the
3009 next arg */
3010 len = strlen(cmdline);
3011 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3012 if (nb_args >= MAX_ARGS)
3013 return;
3014 args[nb_args++] = qemu_strdup("");
3016 if (nb_args <= 1) {
3017 /* command completion */
3018 if (nb_args == 0)
3019 cmdname = "";
3020 else
3021 cmdname = args[0];
3022 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3023 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3024 cmd_completion(cmdname, cmd->name);
3026 } else {
3027 /* find the command */
3028 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3029 if (compare_cmd(args[0], cmd->name))
3030 goto found;
3032 return;
3033 found:
3034 ptype = next_arg_type(cmd->args_type);
3035 for(i = 0; i < nb_args - 2; i++) {
3036 if (*ptype != '\0') {
3037 ptype = next_arg_type(ptype);
3038 while (*ptype == '?')
3039 ptype = next_arg_type(ptype);
3042 str = args[nb_args - 1];
3043 if (*ptype == '-' && ptype[1] != '\0') {
3044 ptype += 2;
3046 switch(*ptype) {
3047 case 'F':
3048 /* file completion */
3049 readline_set_completion_index(cur_mon->rs, strlen(str));
3050 file_completion(str);
3051 break;
3052 case 'B':
3053 /* block device name completion */
3054 readline_set_completion_index(cur_mon->rs, strlen(str));
3055 bdrv_iterate(block_completion_it, (void *)str);
3056 break;
3057 case 's':
3058 /* XXX: more generic ? */
3059 if (!strcmp(cmd->name, "info")) {
3060 readline_set_completion_index(cur_mon->rs, strlen(str));
3061 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3062 cmd_completion(str, cmd->name);
3064 } else if (!strcmp(cmd->name, "sendkey")) {
3065 char *sep = strrchr(str, '-');
3066 if (sep)
3067 str = sep + 1;
3068 readline_set_completion_index(cur_mon->rs, strlen(str));
3069 for(key = key_defs; key->name != NULL; key++) {
3070 cmd_completion(str, key->name);
3072 } else if (!strcmp(cmd->name, "help|?")) {
3073 readline_set_completion_index(cur_mon->rs, strlen(str));
3074 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3075 cmd_completion(str, cmd->name);
3078 break;
3079 default:
3080 break;
3083 for(i = 0; i < nb_args; i++)
3084 qemu_free(args[i]);
3087 static int monitor_can_read(void *opaque)
3089 Monitor *mon = opaque;
3091 return (mon->suspend_cnt == 0) ? 128 : 0;
3094 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3096 Monitor *old_mon = cur_mon;
3097 int i;
3099 cur_mon = opaque;
3101 if (cur_mon->rs) {
3102 for (i = 0; i < size; i++)
3103 readline_handle_byte(cur_mon->rs, buf[i]);
3104 } else {
3105 if (size == 0 || buf[size - 1] != 0)
3106 monitor_printf(cur_mon, "corrupted command\n");
3107 else
3108 monitor_handle_command(cur_mon, (char *)buf);
3111 cur_mon = old_mon;
3114 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3116 monitor_suspend(mon);
3117 monitor_handle_command(mon, cmdline);
3118 monitor_resume(mon);
3121 int monitor_suspend(Monitor *mon)
3123 if (!mon->rs)
3124 return -ENOTTY;
3125 mon->suspend_cnt++;
3126 return 0;
3129 void monitor_resume(Monitor *mon)
3131 if (!mon->rs)
3132 return;
3133 if (--mon->suspend_cnt == 0)
3134 readline_show_prompt(mon->rs);
3137 static void monitor_event(void *opaque, int event)
3139 Monitor *mon = opaque;
3141 switch (event) {
3142 case CHR_EVENT_MUX_IN:
3143 mon->mux_out = 0;
3144 if (mon->reset_seen) {
3145 readline_restart(mon->rs);
3146 monitor_resume(mon);
3147 monitor_flush(mon);
3148 } else {
3149 mon->suspend_cnt = 0;
3151 break;
3153 case CHR_EVENT_MUX_OUT:
3154 if (mon->reset_seen) {
3155 if (mon->suspend_cnt == 0) {
3156 monitor_printf(mon, "\n");
3158 monitor_flush(mon);
3159 monitor_suspend(mon);
3160 } else {
3161 mon->suspend_cnt++;
3163 mon->mux_out = 1;
3164 break;
3166 case CHR_EVENT_RESET:
3167 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3168 "information\n", QEMU_VERSION);
3169 if (!mon->mux_out) {
3170 readline_show_prompt(mon->rs);
3172 mon->reset_seen = 1;
3173 break;
3179 * Local variables:
3180 * c-indent-level: 4
3181 * c-basic-offset: 4
3182 * tab-width: 8
3183 * End:
3186 void monitor_init(CharDriverState *chr, int flags)
3188 static int is_first_init = 1;
3189 Monitor *mon;
3191 if (is_first_init) {
3192 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3193 is_first_init = 0;
3196 mon = qemu_mallocz(sizeof(*mon));
3198 mon->chr = chr;
3199 mon->flags = flags;
3200 if (flags & MONITOR_USE_READLINE) {
3201 mon->rs = readline_init(mon, monitor_find_completion);
3202 monitor_read_command(mon, 0);
3205 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3206 mon);
3208 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3209 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3210 cur_mon = mon;
3213 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3215 BlockDriverState *bs = opaque;
3216 int ret = 0;
3218 if (bdrv_set_key(bs, password) != 0) {
3219 monitor_printf(mon, "invalid password\n");
3220 ret = -EPERM;
3222 if (mon->password_completion_cb)
3223 mon->password_completion_cb(mon->password_opaque, ret);
3225 monitor_read_command(mon, 1);
3228 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3229 BlockDriverCompletionFunc *completion_cb,
3230 void *opaque)
3232 int err;
3234 if (!bdrv_key_required(bs)) {
3235 if (completion_cb)
3236 completion_cb(opaque, 0);
3237 return;
3240 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3241 bdrv_get_encrypted_filename(bs));
3243 mon->password_completion_cb = completion_cb;
3244 mon->password_opaque = opaque;
3246 err = monitor_read_password(mon, bdrv_password_cb, bs);
3248 if (err && completion_cb)
3249 completion_cb(opaque, err);
3252 typedef struct QemuErrorSink QemuErrorSink;
3253 struct QemuErrorSink {
3254 enum {
3255 ERR_SINK_FILE,
3256 ERR_SINK_MONITOR,
3257 } dest;
3258 union {
3259 FILE *fp;
3260 Monitor *mon;
3262 QemuErrorSink *previous;
3265 static QemuErrorSink *qemu_error_sink;
3267 void qemu_errors_to_file(FILE *fp)
3269 QemuErrorSink *sink;
3271 sink = qemu_mallocz(sizeof(*sink));
3272 sink->dest = ERR_SINK_FILE;
3273 sink->fp = fp;
3274 sink->previous = qemu_error_sink;
3275 qemu_error_sink = sink;
3278 void qemu_errors_to_mon(Monitor *mon)
3280 QemuErrorSink *sink;
3282 sink = qemu_mallocz(sizeof(*sink));
3283 sink->dest = ERR_SINK_MONITOR;
3284 sink->mon = mon;
3285 sink->previous = qemu_error_sink;
3286 qemu_error_sink = sink;
3289 void qemu_errors_to_previous(void)
3291 QemuErrorSink *sink;
3293 assert(qemu_error_sink != NULL);
3294 sink = qemu_error_sink;
3295 qemu_error_sink = sink->previous;
3296 qemu_free(sink);
3299 void qemu_error(const char *fmt, ...)
3301 va_list args;
3303 assert(qemu_error_sink != NULL);
3304 switch (qemu_error_sink->dest) {
3305 case ERR_SINK_FILE:
3306 va_start(args, fmt);
3307 vfprintf(qemu_error_sink->fp, fmt, args);
3308 va_end(args);
3309 break;
3310 case ERR_SINK_MONITOR:
3311 va_start(args, fmt);
3312 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3313 va_end(args);
3314 break;