test: Fix compilation rule for assembly files wrt autodependencies
[qemu-kvm/amd-iommu.git] / monitor.c
blobda573c8c43ef0d041b73ee1abebe0b7614b8a702
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(NULL, addr & IOPORTS_MASK, index & 0xff);
1225 addr++;
1227 addr &= 0xffff;
1229 switch(size) {
1230 default:
1231 case 1:
1232 val = cpu_inb(NULL, addr);
1233 suffix = 'b';
1234 break;
1235 case 2:
1236 val = cpu_inw(NULL, addr);
1237 suffix = 'w';
1238 break;
1239 case 4:
1240 val = cpu_inl(NULL, 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(NULL, addr, val);
1260 break;
1261 case 2:
1262 cpu_outw(NULL, addr, val);
1263 break;
1264 case 4:
1265 cpu_outl(NULL, 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 static void do_info_profile(Monitor *mon)
1482 int64_t total;
1483 total = qemu_time;
1484 if (total == 0)
1485 total = 1;
1486 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1487 dev_time, dev_time / (double)get_ticks_per_sec());
1488 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1489 qemu_time, qemu_time / (double)get_ticks_per_sec());
1490 qemu_time = 0;
1491 dev_time = 0;
1493 #else
1494 static void do_info_profile(Monitor *mon)
1496 monitor_printf(mon, "Internal profiler not compiled\n");
1498 #endif
1500 /* Capture support */
1501 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1503 static void do_info_capture(Monitor *mon)
1505 int i;
1506 CaptureState *s;
1508 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1509 monitor_printf(mon, "[%d]: ", i);
1510 s->ops.info (s->opaque);
1514 #ifdef HAS_AUDIO
1515 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1517 int i;
1518 int n = qdict_get_int(qdict, "n");
1519 CaptureState *s;
1521 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1522 if (i == n) {
1523 s->ops.destroy (s->opaque);
1524 QLIST_REMOVE (s, entries);
1525 qemu_free (s);
1526 return;
1531 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1533 const char *path = qdict_get_str(qdict, "path");
1534 int has_freq = qdict_haskey(qdict, "freq");
1535 int freq = qdict_get_try_int(qdict, "freq", -1);
1536 int has_bits = qdict_haskey(qdict, "bits");
1537 int bits = qdict_get_try_int(qdict, "bits", -1);
1538 int has_channels = qdict_haskey(qdict, "nchannels");
1539 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1540 CaptureState *s;
1542 s = qemu_mallocz (sizeof (*s));
1544 freq = has_freq ? freq : 44100;
1545 bits = has_bits ? bits : 16;
1546 nchannels = has_channels ? nchannels : 2;
1548 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1549 monitor_printf(mon, "Faied to add wave capture\n");
1550 qemu_free (s);
1552 QLIST_INSERT_HEAD (&capture_head, s, entries);
1554 #endif
1556 #if defined(TARGET_I386)
1557 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1559 CPUState *env;
1560 int cpu_index = qdict_get_int(qdict, "cpu_index");
1562 for (env = first_cpu; env != NULL; env = env->next_cpu)
1563 if (env->cpu_index == cpu_index) {
1564 if (kvm_enabled())
1565 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1566 else
1567 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1568 break;
1571 #endif
1573 static void do_info_status(Monitor *mon)
1575 if (vm_running) {
1576 if (singlestep) {
1577 monitor_printf(mon, "VM status: running (single step mode)\n");
1578 } else {
1579 monitor_printf(mon, "VM status: running\n");
1581 } else
1582 monitor_printf(mon, "VM status: paused\n");
1586 static void do_balloon(Monitor *mon, const QDict *qdict)
1588 int value = qdict_get_int(qdict, "value");
1589 ram_addr_t target = value;
1590 qemu_balloon(target << 20);
1593 static void do_info_balloon(Monitor *mon)
1595 ram_addr_t actual;
1597 actual = qemu_balloon_status();
1598 if (kvm_enabled() && !kvm_has_sync_mmu())
1599 monitor_printf(mon, "Using KVM without synchronous MMU, "
1600 "ballooning disabled\n");
1601 else if (actual == 0)
1602 monitor_printf(mon, "Ballooning not activated in VM\n");
1603 else
1604 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1607 static qemu_acl *find_acl(Monitor *mon, const char *name)
1609 qemu_acl *acl = qemu_acl_find(name);
1611 if (!acl) {
1612 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1614 return acl;
1617 static void do_acl_show(Monitor *mon, const QDict *qdict)
1619 const char *aclname = qdict_get_str(qdict, "aclname");
1620 qemu_acl *acl = find_acl(mon, aclname);
1621 qemu_acl_entry *entry;
1622 int i = 0;
1624 if (acl) {
1625 monitor_printf(mon, "policy: %s\n",
1626 acl->defaultDeny ? "deny" : "allow");
1627 QTAILQ_FOREACH(entry, &acl->entries, next) {
1628 i++;
1629 monitor_printf(mon, "%d: %s %s\n", i,
1630 entry->deny ? "deny" : "allow", entry->match);
1635 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1637 const char *aclname = qdict_get_str(qdict, "aclname");
1638 qemu_acl *acl = find_acl(mon, aclname);
1640 if (acl) {
1641 qemu_acl_reset(acl);
1642 monitor_printf(mon, "acl: removed all rules\n");
1646 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1648 const char *aclname = qdict_get_str(qdict, "aclname");
1649 const char *policy = qdict_get_str(qdict, "policy");
1650 qemu_acl *acl = find_acl(mon, aclname);
1652 if (acl) {
1653 if (strcmp(policy, "allow") == 0) {
1654 acl->defaultDeny = 0;
1655 monitor_printf(mon, "acl: policy set to 'allow'\n");
1656 } else if (strcmp(policy, "deny") == 0) {
1657 acl->defaultDeny = 1;
1658 monitor_printf(mon, "acl: policy set to 'deny'\n");
1659 } else {
1660 monitor_printf(mon, "acl: unknown policy '%s', "
1661 "expected 'deny' or 'allow'\n", policy);
1666 static void do_acl_add(Monitor *mon, const QDict *qdict)
1668 const char *aclname = qdict_get_str(qdict, "aclname");
1669 const char *match = qdict_get_str(qdict, "match");
1670 const char *policy = qdict_get_str(qdict, "policy");
1671 int has_index = qdict_haskey(qdict, "index");
1672 int index = qdict_get_try_int(qdict, "index", -1);
1673 qemu_acl *acl = find_acl(mon, aclname);
1674 int deny, ret;
1676 if (acl) {
1677 if (strcmp(policy, "allow") == 0) {
1678 deny = 0;
1679 } else if (strcmp(policy, "deny") == 0) {
1680 deny = 1;
1681 } else {
1682 monitor_printf(mon, "acl: unknown policy '%s', "
1683 "expected 'deny' or 'allow'\n", policy);
1684 return;
1686 if (has_index)
1687 ret = qemu_acl_insert(acl, deny, match, index);
1688 else
1689 ret = qemu_acl_append(acl, deny, match);
1690 if (ret < 0)
1691 monitor_printf(mon, "acl: unable to add acl entry\n");
1692 else
1693 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1697 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1699 const char *aclname = qdict_get_str(qdict, "aclname");
1700 const char *match = qdict_get_str(qdict, "match");
1701 qemu_acl *acl = find_acl(mon, aclname);
1702 int ret;
1704 if (acl) {
1705 ret = qemu_acl_remove(acl, match);
1706 if (ret < 0)
1707 monitor_printf(mon, "acl: no matching acl entry\n");
1708 else
1709 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1713 #if defined(TARGET_I386)
1714 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1716 CPUState *cenv;
1717 int cpu_index = qdict_get_int(qdict, "cpu_index");
1718 int bank = qdict_get_int(qdict, "bank");
1719 uint64_t status = qdict_get_int(qdict, "status");
1720 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1721 uint64_t addr = qdict_get_int(qdict, "addr");
1722 uint64_t misc = qdict_get_int(qdict, "misc");
1724 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1725 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1726 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1727 break;
1730 #endif
1732 static void do_getfd(Monitor *mon, const QDict *qdict)
1734 const char *fdname = qdict_get_str(qdict, "fdname");
1735 mon_fd_t *monfd;
1736 int fd;
1738 fd = qemu_chr_get_msgfd(mon->chr);
1739 if (fd == -1) {
1740 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1741 return;
1744 if (qemu_isdigit(fdname[0])) {
1745 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1746 return;
1749 fd = dup(fd);
1750 if (fd == -1) {
1751 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1752 strerror(errno));
1753 return;
1756 QLIST_FOREACH(monfd, &mon->fds, next) {
1757 if (strcmp(monfd->name, fdname) != 0) {
1758 continue;
1761 close(monfd->fd);
1762 monfd->fd = fd;
1763 return;
1766 monfd = qemu_mallocz(sizeof(mon_fd_t));
1767 monfd->name = qemu_strdup(fdname);
1768 monfd->fd = fd;
1770 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1773 static void do_closefd(Monitor *mon, const QDict *qdict)
1775 const char *fdname = qdict_get_str(qdict, "fdname");
1776 mon_fd_t *monfd;
1778 QLIST_FOREACH(monfd, &mon->fds, next) {
1779 if (strcmp(monfd->name, fdname) != 0) {
1780 continue;
1783 QLIST_REMOVE(monfd, next);
1784 close(monfd->fd);
1785 qemu_free(monfd->name);
1786 qemu_free(monfd);
1787 return;
1790 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1791 fdname);
1794 static void do_loadvm(Monitor *mon, const QDict *qdict)
1796 int saved_vm_running = vm_running;
1797 const char *name = qdict_get_str(qdict, "name");
1799 vm_stop(0);
1801 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1802 vm_start();
1805 int monitor_get_fd(Monitor *mon, const char *fdname)
1807 mon_fd_t *monfd;
1809 QLIST_FOREACH(monfd, &mon->fds, next) {
1810 int fd;
1812 if (strcmp(monfd->name, fdname) != 0) {
1813 continue;
1816 fd = monfd->fd;
1818 /* caller takes ownership of fd */
1819 QLIST_REMOVE(monfd, next);
1820 qemu_free(monfd->name);
1821 qemu_free(monfd);
1823 return fd;
1826 return -1;
1829 static const mon_cmd_t mon_cmds[] = {
1830 #include "qemu-monitor.h"
1831 { NULL, NULL, },
1834 /* Please update qemu-monitor.hx when adding or changing commands */
1835 static const mon_cmd_t info_cmds[] = {
1836 { "version", "", do_info_version,
1837 "", "show the version of QEMU" },
1838 { "network", "", do_info_network,
1839 "", "show the network state" },
1840 { "chardev", "", qemu_chr_info,
1841 "", "show the character devices" },
1842 { "block", "", bdrv_info,
1843 "", "show the block devices" },
1844 { "blockstats", "", bdrv_info_stats,
1845 "", "show block device statistics" },
1846 { "registers", "", do_info_registers,
1847 "", "show the cpu registers" },
1848 { "cpus", "", do_info_cpus,
1849 "", "show infos for each CPU" },
1850 { "history", "", do_info_history,
1851 "", "show the command line history", },
1852 { "irq", "", irq_info,
1853 "", "show the interrupts statistics (if available)", },
1854 { "pic", "", pic_info,
1855 "", "show i8259 (PIC) state", },
1856 { "pci", "", pci_info,
1857 "", "show PCI info", },
1858 #if defined(TARGET_I386) || defined(TARGET_SH4)
1859 { "tlb", "", tlb_info,
1860 "", "show virtual to physical memory mappings", },
1861 #endif
1862 #if defined(TARGET_I386)
1863 { "mem", "", mem_info,
1864 "", "show the active virtual memory mappings", },
1865 { "hpet", "", do_info_hpet,
1866 "", "show state of HPET", },
1867 #endif
1868 { "jit", "", do_info_jit,
1869 "", "show dynamic compiler info", },
1870 { "kvm", "", do_info_kvm,
1871 "", "show KVM information", },
1872 { "numa", "", do_info_numa,
1873 "", "show NUMA information", },
1874 { "usb", "", usb_info,
1875 "", "show guest USB devices", },
1876 { "usbhost", "", usb_host_info,
1877 "", "show host USB devices", },
1878 { "profile", "", do_info_profile,
1879 "", "show profiling information", },
1880 { "capture", "", do_info_capture,
1881 "", "show capture information" },
1882 { "snapshots", "", do_info_snapshots,
1883 "", "show the currently saved VM snapshots" },
1884 { "status", "", do_info_status,
1885 "", "show the current VM status (running|paused)" },
1886 { "pcmcia", "", pcmcia_info,
1887 "", "show guest PCMCIA status" },
1888 { "mice", "", do_info_mice,
1889 "", "show which guest mouse is receiving events" },
1890 { "vnc", "", do_info_vnc,
1891 "", "show the vnc server status"},
1892 { "name", "", do_info_name,
1893 "", "show the current VM name" },
1894 { "uuid", "", do_info_uuid,
1895 "", "show the current VM UUID" },
1896 #if defined(TARGET_PPC)
1897 { "cpustats", "", do_info_cpu_stats,
1898 "", "show CPU statistics", },
1899 #endif
1900 #if defined(CONFIG_SLIRP)
1901 { "usernet", "", do_info_usernet,
1902 "", "show user network stack connection states", },
1903 #endif
1904 { "migrate", "", do_info_migrate, "", "show migration status" },
1905 { "balloon", "", do_info_balloon,
1906 "", "show balloon information" },
1907 { "qtree", "", do_info_qtree,
1908 "", "show device tree" },
1909 { "qdm", "", do_info_qdm,
1910 "", "show qdev device model list" },
1911 { NULL, NULL, },
1914 /*******************************************************************/
1916 static const char *pch;
1917 static jmp_buf expr_env;
1919 #define MD_TLONG 0
1920 #define MD_I32 1
1922 typedef struct MonitorDef {
1923 const char *name;
1924 int offset;
1925 target_long (*get_value)(const struct MonitorDef *md, int val);
1926 int type;
1927 } MonitorDef;
1929 #if defined(TARGET_I386)
1930 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1932 CPUState *env = mon_get_cpu();
1933 if (!env)
1934 return 0;
1935 return env->eip + env->segs[R_CS].base;
1937 #endif
1939 #if defined(TARGET_PPC)
1940 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1942 CPUState *env = mon_get_cpu();
1943 unsigned int u;
1944 int i;
1946 if (!env)
1947 return 0;
1949 u = 0;
1950 for (i = 0; i < 8; i++)
1951 u |= env->crf[i] << (32 - (4 * i));
1953 return u;
1956 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1958 CPUState *env = mon_get_cpu();
1959 if (!env)
1960 return 0;
1961 return env->msr;
1964 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1966 CPUState *env = mon_get_cpu();
1967 if (!env)
1968 return 0;
1969 return env->xer;
1972 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1974 CPUState *env = mon_get_cpu();
1975 if (!env)
1976 return 0;
1977 return cpu_ppc_load_decr(env);
1980 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1982 CPUState *env = mon_get_cpu();
1983 if (!env)
1984 return 0;
1985 return cpu_ppc_load_tbu(env);
1988 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1990 CPUState *env = mon_get_cpu();
1991 if (!env)
1992 return 0;
1993 return cpu_ppc_load_tbl(env);
1995 #endif
1997 #if defined(TARGET_SPARC)
1998 #ifndef TARGET_SPARC64
1999 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2001 CPUState *env = mon_get_cpu();
2002 if (!env)
2003 return 0;
2004 return GET_PSR(env);
2006 #endif
2008 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2010 CPUState *env = mon_get_cpu();
2011 if (!env)
2012 return 0;
2013 return env->regwptr[val];
2015 #endif
2017 static const MonitorDef monitor_defs[] = {
2018 #ifdef TARGET_I386
2020 #define SEG(name, seg) \
2021 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2022 { name ".base", offsetof(CPUState, segs[seg].base) },\
2023 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2025 { "eax", offsetof(CPUState, regs[0]) },
2026 { "ecx", offsetof(CPUState, regs[1]) },
2027 { "edx", offsetof(CPUState, regs[2]) },
2028 { "ebx", offsetof(CPUState, regs[3]) },
2029 { "esp|sp", offsetof(CPUState, regs[4]) },
2030 { "ebp|fp", offsetof(CPUState, regs[5]) },
2031 { "esi", offsetof(CPUState, regs[6]) },
2032 { "edi", offsetof(CPUState, regs[7]) },
2033 #ifdef TARGET_X86_64
2034 { "r8", offsetof(CPUState, regs[8]) },
2035 { "r9", offsetof(CPUState, regs[9]) },
2036 { "r10", offsetof(CPUState, regs[10]) },
2037 { "r11", offsetof(CPUState, regs[11]) },
2038 { "r12", offsetof(CPUState, regs[12]) },
2039 { "r13", offsetof(CPUState, regs[13]) },
2040 { "r14", offsetof(CPUState, regs[14]) },
2041 { "r15", offsetof(CPUState, regs[15]) },
2042 #endif
2043 { "eflags", offsetof(CPUState, eflags) },
2044 { "eip", offsetof(CPUState, eip) },
2045 SEG("cs", R_CS)
2046 SEG("ds", R_DS)
2047 SEG("es", R_ES)
2048 SEG("ss", R_SS)
2049 SEG("fs", R_FS)
2050 SEG("gs", R_GS)
2051 { "pc", 0, monitor_get_pc, },
2052 #elif defined(TARGET_PPC)
2053 /* General purpose registers */
2054 { "r0", offsetof(CPUState, gpr[0]) },
2055 { "r1", offsetof(CPUState, gpr[1]) },
2056 { "r2", offsetof(CPUState, gpr[2]) },
2057 { "r3", offsetof(CPUState, gpr[3]) },
2058 { "r4", offsetof(CPUState, gpr[4]) },
2059 { "r5", offsetof(CPUState, gpr[5]) },
2060 { "r6", offsetof(CPUState, gpr[6]) },
2061 { "r7", offsetof(CPUState, gpr[7]) },
2062 { "r8", offsetof(CPUState, gpr[8]) },
2063 { "r9", offsetof(CPUState, gpr[9]) },
2064 { "r10", offsetof(CPUState, gpr[10]) },
2065 { "r11", offsetof(CPUState, gpr[11]) },
2066 { "r12", offsetof(CPUState, gpr[12]) },
2067 { "r13", offsetof(CPUState, gpr[13]) },
2068 { "r14", offsetof(CPUState, gpr[14]) },
2069 { "r15", offsetof(CPUState, gpr[15]) },
2070 { "r16", offsetof(CPUState, gpr[16]) },
2071 { "r17", offsetof(CPUState, gpr[17]) },
2072 { "r18", offsetof(CPUState, gpr[18]) },
2073 { "r19", offsetof(CPUState, gpr[19]) },
2074 { "r20", offsetof(CPUState, gpr[20]) },
2075 { "r21", offsetof(CPUState, gpr[21]) },
2076 { "r22", offsetof(CPUState, gpr[22]) },
2077 { "r23", offsetof(CPUState, gpr[23]) },
2078 { "r24", offsetof(CPUState, gpr[24]) },
2079 { "r25", offsetof(CPUState, gpr[25]) },
2080 { "r26", offsetof(CPUState, gpr[26]) },
2081 { "r27", offsetof(CPUState, gpr[27]) },
2082 { "r28", offsetof(CPUState, gpr[28]) },
2083 { "r29", offsetof(CPUState, gpr[29]) },
2084 { "r30", offsetof(CPUState, gpr[30]) },
2085 { "r31", offsetof(CPUState, gpr[31]) },
2086 /* Floating point registers */
2087 { "f0", offsetof(CPUState, fpr[0]) },
2088 { "f1", offsetof(CPUState, fpr[1]) },
2089 { "f2", offsetof(CPUState, fpr[2]) },
2090 { "f3", offsetof(CPUState, fpr[3]) },
2091 { "f4", offsetof(CPUState, fpr[4]) },
2092 { "f5", offsetof(CPUState, fpr[5]) },
2093 { "f6", offsetof(CPUState, fpr[6]) },
2094 { "f7", offsetof(CPUState, fpr[7]) },
2095 { "f8", offsetof(CPUState, fpr[8]) },
2096 { "f9", offsetof(CPUState, fpr[9]) },
2097 { "f10", offsetof(CPUState, fpr[10]) },
2098 { "f11", offsetof(CPUState, fpr[11]) },
2099 { "f12", offsetof(CPUState, fpr[12]) },
2100 { "f13", offsetof(CPUState, fpr[13]) },
2101 { "f14", offsetof(CPUState, fpr[14]) },
2102 { "f15", offsetof(CPUState, fpr[15]) },
2103 { "f16", offsetof(CPUState, fpr[16]) },
2104 { "f17", offsetof(CPUState, fpr[17]) },
2105 { "f18", offsetof(CPUState, fpr[18]) },
2106 { "f19", offsetof(CPUState, fpr[19]) },
2107 { "f20", offsetof(CPUState, fpr[20]) },
2108 { "f21", offsetof(CPUState, fpr[21]) },
2109 { "f22", offsetof(CPUState, fpr[22]) },
2110 { "f23", offsetof(CPUState, fpr[23]) },
2111 { "f24", offsetof(CPUState, fpr[24]) },
2112 { "f25", offsetof(CPUState, fpr[25]) },
2113 { "f26", offsetof(CPUState, fpr[26]) },
2114 { "f27", offsetof(CPUState, fpr[27]) },
2115 { "f28", offsetof(CPUState, fpr[28]) },
2116 { "f29", offsetof(CPUState, fpr[29]) },
2117 { "f30", offsetof(CPUState, fpr[30]) },
2118 { "f31", offsetof(CPUState, fpr[31]) },
2119 { "fpscr", offsetof(CPUState, fpscr) },
2120 /* Next instruction pointer */
2121 { "nip|pc", offsetof(CPUState, nip) },
2122 { "lr", offsetof(CPUState, lr) },
2123 { "ctr", offsetof(CPUState, ctr) },
2124 { "decr", 0, &monitor_get_decr, },
2125 { "ccr", 0, &monitor_get_ccr, },
2126 /* Machine state register */
2127 { "msr", 0, &monitor_get_msr, },
2128 { "xer", 0, &monitor_get_xer, },
2129 { "tbu", 0, &monitor_get_tbu, },
2130 { "tbl", 0, &monitor_get_tbl, },
2131 #if defined(TARGET_PPC64)
2132 /* Address space register */
2133 { "asr", offsetof(CPUState, asr) },
2134 #endif
2135 /* Segment registers */
2136 { "sdr1", offsetof(CPUState, sdr1) },
2137 { "sr0", offsetof(CPUState, sr[0]) },
2138 { "sr1", offsetof(CPUState, sr[1]) },
2139 { "sr2", offsetof(CPUState, sr[2]) },
2140 { "sr3", offsetof(CPUState, sr[3]) },
2141 { "sr4", offsetof(CPUState, sr[4]) },
2142 { "sr5", offsetof(CPUState, sr[5]) },
2143 { "sr6", offsetof(CPUState, sr[6]) },
2144 { "sr7", offsetof(CPUState, sr[7]) },
2145 { "sr8", offsetof(CPUState, sr[8]) },
2146 { "sr9", offsetof(CPUState, sr[9]) },
2147 { "sr10", offsetof(CPUState, sr[10]) },
2148 { "sr11", offsetof(CPUState, sr[11]) },
2149 { "sr12", offsetof(CPUState, sr[12]) },
2150 { "sr13", offsetof(CPUState, sr[13]) },
2151 { "sr14", offsetof(CPUState, sr[14]) },
2152 { "sr15", offsetof(CPUState, sr[15]) },
2153 /* Too lazy to put BATs and SPRs ... */
2154 #elif defined(TARGET_SPARC)
2155 { "g0", offsetof(CPUState, gregs[0]) },
2156 { "g1", offsetof(CPUState, gregs[1]) },
2157 { "g2", offsetof(CPUState, gregs[2]) },
2158 { "g3", offsetof(CPUState, gregs[3]) },
2159 { "g4", offsetof(CPUState, gregs[4]) },
2160 { "g5", offsetof(CPUState, gregs[5]) },
2161 { "g6", offsetof(CPUState, gregs[6]) },
2162 { "g7", offsetof(CPUState, gregs[7]) },
2163 { "o0", 0, monitor_get_reg },
2164 { "o1", 1, monitor_get_reg },
2165 { "o2", 2, monitor_get_reg },
2166 { "o3", 3, monitor_get_reg },
2167 { "o4", 4, monitor_get_reg },
2168 { "o5", 5, monitor_get_reg },
2169 { "o6", 6, monitor_get_reg },
2170 { "o7", 7, monitor_get_reg },
2171 { "l0", 8, monitor_get_reg },
2172 { "l1", 9, monitor_get_reg },
2173 { "l2", 10, monitor_get_reg },
2174 { "l3", 11, monitor_get_reg },
2175 { "l4", 12, monitor_get_reg },
2176 { "l5", 13, monitor_get_reg },
2177 { "l6", 14, monitor_get_reg },
2178 { "l7", 15, monitor_get_reg },
2179 { "i0", 16, monitor_get_reg },
2180 { "i1", 17, monitor_get_reg },
2181 { "i2", 18, monitor_get_reg },
2182 { "i3", 19, monitor_get_reg },
2183 { "i4", 20, monitor_get_reg },
2184 { "i5", 21, monitor_get_reg },
2185 { "i6", 22, monitor_get_reg },
2186 { "i7", 23, monitor_get_reg },
2187 { "pc", offsetof(CPUState, pc) },
2188 { "npc", offsetof(CPUState, npc) },
2189 { "y", offsetof(CPUState, y) },
2190 #ifndef TARGET_SPARC64
2191 { "psr", 0, &monitor_get_psr, },
2192 { "wim", offsetof(CPUState, wim) },
2193 #endif
2194 { "tbr", offsetof(CPUState, tbr) },
2195 { "fsr", offsetof(CPUState, fsr) },
2196 { "f0", offsetof(CPUState, fpr[0]) },
2197 { "f1", offsetof(CPUState, fpr[1]) },
2198 { "f2", offsetof(CPUState, fpr[2]) },
2199 { "f3", offsetof(CPUState, fpr[3]) },
2200 { "f4", offsetof(CPUState, fpr[4]) },
2201 { "f5", offsetof(CPUState, fpr[5]) },
2202 { "f6", offsetof(CPUState, fpr[6]) },
2203 { "f7", offsetof(CPUState, fpr[7]) },
2204 { "f8", offsetof(CPUState, fpr[8]) },
2205 { "f9", offsetof(CPUState, fpr[9]) },
2206 { "f10", offsetof(CPUState, fpr[10]) },
2207 { "f11", offsetof(CPUState, fpr[11]) },
2208 { "f12", offsetof(CPUState, fpr[12]) },
2209 { "f13", offsetof(CPUState, fpr[13]) },
2210 { "f14", offsetof(CPUState, fpr[14]) },
2211 { "f15", offsetof(CPUState, fpr[15]) },
2212 { "f16", offsetof(CPUState, fpr[16]) },
2213 { "f17", offsetof(CPUState, fpr[17]) },
2214 { "f18", offsetof(CPUState, fpr[18]) },
2215 { "f19", offsetof(CPUState, fpr[19]) },
2216 { "f20", offsetof(CPUState, fpr[20]) },
2217 { "f21", offsetof(CPUState, fpr[21]) },
2218 { "f22", offsetof(CPUState, fpr[22]) },
2219 { "f23", offsetof(CPUState, fpr[23]) },
2220 { "f24", offsetof(CPUState, fpr[24]) },
2221 { "f25", offsetof(CPUState, fpr[25]) },
2222 { "f26", offsetof(CPUState, fpr[26]) },
2223 { "f27", offsetof(CPUState, fpr[27]) },
2224 { "f28", offsetof(CPUState, fpr[28]) },
2225 { "f29", offsetof(CPUState, fpr[29]) },
2226 { "f30", offsetof(CPUState, fpr[30]) },
2227 { "f31", offsetof(CPUState, fpr[31]) },
2228 #ifdef TARGET_SPARC64
2229 { "f32", offsetof(CPUState, fpr[32]) },
2230 { "f34", offsetof(CPUState, fpr[34]) },
2231 { "f36", offsetof(CPUState, fpr[36]) },
2232 { "f38", offsetof(CPUState, fpr[38]) },
2233 { "f40", offsetof(CPUState, fpr[40]) },
2234 { "f42", offsetof(CPUState, fpr[42]) },
2235 { "f44", offsetof(CPUState, fpr[44]) },
2236 { "f46", offsetof(CPUState, fpr[46]) },
2237 { "f48", offsetof(CPUState, fpr[48]) },
2238 { "f50", offsetof(CPUState, fpr[50]) },
2239 { "f52", offsetof(CPUState, fpr[52]) },
2240 { "f54", offsetof(CPUState, fpr[54]) },
2241 { "f56", offsetof(CPUState, fpr[56]) },
2242 { "f58", offsetof(CPUState, fpr[58]) },
2243 { "f60", offsetof(CPUState, fpr[60]) },
2244 { "f62", offsetof(CPUState, fpr[62]) },
2245 { "asi", offsetof(CPUState, asi) },
2246 { "pstate", offsetof(CPUState, pstate) },
2247 { "cansave", offsetof(CPUState, cansave) },
2248 { "canrestore", offsetof(CPUState, canrestore) },
2249 { "otherwin", offsetof(CPUState, otherwin) },
2250 { "wstate", offsetof(CPUState, wstate) },
2251 { "cleanwin", offsetof(CPUState, cleanwin) },
2252 { "fprs", offsetof(CPUState, fprs) },
2253 #endif
2254 #endif
2255 { NULL },
2258 static void expr_error(Monitor *mon, const char *msg)
2260 monitor_printf(mon, "%s\n", msg);
2261 longjmp(expr_env, 1);
2264 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2265 static int get_monitor_def(target_long *pval, const char *name)
2267 const MonitorDef *md;
2268 void *ptr;
2270 for(md = monitor_defs; md->name != NULL; md++) {
2271 if (compare_cmd(name, md->name)) {
2272 if (md->get_value) {
2273 *pval = md->get_value(md, md->offset);
2274 } else {
2275 CPUState *env = mon_get_cpu();
2276 if (!env)
2277 return -2;
2278 ptr = (uint8_t *)env + md->offset;
2279 switch(md->type) {
2280 case MD_I32:
2281 *pval = *(int32_t *)ptr;
2282 break;
2283 case MD_TLONG:
2284 *pval = *(target_long *)ptr;
2285 break;
2286 default:
2287 *pval = 0;
2288 break;
2291 return 0;
2294 return -1;
2297 static void next(void)
2299 if (*pch != '\0') {
2300 pch++;
2301 while (qemu_isspace(*pch))
2302 pch++;
2306 static int64_t expr_sum(Monitor *mon);
2308 static int64_t expr_unary(Monitor *mon)
2310 int64_t n;
2311 char *p;
2312 int ret;
2314 switch(*pch) {
2315 case '+':
2316 next();
2317 n = expr_unary(mon);
2318 break;
2319 case '-':
2320 next();
2321 n = -expr_unary(mon);
2322 break;
2323 case '~':
2324 next();
2325 n = ~expr_unary(mon);
2326 break;
2327 case '(':
2328 next();
2329 n = expr_sum(mon);
2330 if (*pch != ')') {
2331 expr_error(mon, "')' expected");
2333 next();
2334 break;
2335 case '\'':
2336 pch++;
2337 if (*pch == '\0')
2338 expr_error(mon, "character constant expected");
2339 n = *pch;
2340 pch++;
2341 if (*pch != '\'')
2342 expr_error(mon, "missing terminating \' character");
2343 next();
2344 break;
2345 case '$':
2347 char buf[128], *q;
2348 target_long reg=0;
2350 pch++;
2351 q = buf;
2352 while ((*pch >= 'a' && *pch <= 'z') ||
2353 (*pch >= 'A' && *pch <= 'Z') ||
2354 (*pch >= '0' && *pch <= '9') ||
2355 *pch == '_' || *pch == '.') {
2356 if ((q - buf) < sizeof(buf) - 1)
2357 *q++ = *pch;
2358 pch++;
2360 while (qemu_isspace(*pch))
2361 pch++;
2362 *q = 0;
2363 ret = get_monitor_def(&reg, buf);
2364 if (ret == -1)
2365 expr_error(mon, "unknown register");
2366 else if (ret == -2)
2367 expr_error(mon, "no cpu defined");
2368 n = reg;
2370 break;
2371 case '\0':
2372 expr_error(mon, "unexpected end of expression");
2373 n = 0;
2374 break;
2375 default:
2376 #if TARGET_PHYS_ADDR_BITS > 32
2377 n = strtoull(pch, &p, 0);
2378 #else
2379 n = strtoul(pch, &p, 0);
2380 #endif
2381 if (pch == p) {
2382 expr_error(mon, "invalid char in expression");
2384 pch = p;
2385 while (qemu_isspace(*pch))
2386 pch++;
2387 break;
2389 return n;
2393 static int64_t expr_prod(Monitor *mon)
2395 int64_t val, val2;
2396 int op;
2398 val = expr_unary(mon);
2399 for(;;) {
2400 op = *pch;
2401 if (op != '*' && op != '/' && op != '%')
2402 break;
2403 next();
2404 val2 = expr_unary(mon);
2405 switch(op) {
2406 default:
2407 case '*':
2408 val *= val2;
2409 break;
2410 case '/':
2411 case '%':
2412 if (val2 == 0)
2413 expr_error(mon, "division by zero");
2414 if (op == '/')
2415 val /= val2;
2416 else
2417 val %= val2;
2418 break;
2421 return val;
2424 static int64_t expr_logic(Monitor *mon)
2426 int64_t val, val2;
2427 int op;
2429 val = expr_prod(mon);
2430 for(;;) {
2431 op = *pch;
2432 if (op != '&' && op != '|' && op != '^')
2433 break;
2434 next();
2435 val2 = expr_prod(mon);
2436 switch(op) {
2437 default:
2438 case '&':
2439 val &= val2;
2440 break;
2441 case '|':
2442 val |= val2;
2443 break;
2444 case '^':
2445 val ^= val2;
2446 break;
2449 return val;
2452 static int64_t expr_sum(Monitor *mon)
2454 int64_t val, val2;
2455 int op;
2457 val = expr_logic(mon);
2458 for(;;) {
2459 op = *pch;
2460 if (op != '+' && op != '-')
2461 break;
2462 next();
2463 val2 = expr_logic(mon);
2464 if (op == '+')
2465 val += val2;
2466 else
2467 val -= val2;
2469 return val;
2472 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2474 pch = *pp;
2475 if (setjmp(expr_env)) {
2476 *pp = pch;
2477 return -1;
2479 while (qemu_isspace(*pch))
2480 pch++;
2481 *pval = expr_sum(mon);
2482 *pp = pch;
2483 return 0;
2486 static int get_str(char *buf, int buf_size, const char **pp)
2488 const char *p;
2489 char *q;
2490 int c;
2492 q = buf;
2493 p = *pp;
2494 while (qemu_isspace(*p))
2495 p++;
2496 if (*p == '\0') {
2497 fail:
2498 *q = '\0';
2499 *pp = p;
2500 return -1;
2502 if (*p == '\"') {
2503 p++;
2504 while (*p != '\0' && *p != '\"') {
2505 if (*p == '\\') {
2506 p++;
2507 c = *p++;
2508 switch(c) {
2509 case 'n':
2510 c = '\n';
2511 break;
2512 case 'r':
2513 c = '\r';
2514 break;
2515 case '\\':
2516 case '\'':
2517 case '\"':
2518 break;
2519 default:
2520 qemu_printf("unsupported escape code: '\\%c'\n", c);
2521 goto fail;
2523 if ((q - buf) < buf_size - 1) {
2524 *q++ = c;
2526 } else {
2527 if ((q - buf) < buf_size - 1) {
2528 *q++ = *p;
2530 p++;
2533 if (*p != '\"') {
2534 qemu_printf("unterminated string\n");
2535 goto fail;
2537 p++;
2538 } else {
2539 while (*p != '\0' && !qemu_isspace(*p)) {
2540 if ((q - buf) < buf_size - 1) {
2541 *q++ = *p;
2543 p++;
2546 *q = '\0';
2547 *pp = p;
2548 return 0;
2552 * Store the command-name in cmdname, and return a pointer to
2553 * the remaining of the command string.
2555 static const char *get_command_name(const char *cmdline,
2556 char *cmdname, size_t nlen)
2558 size_t len;
2559 const char *p, *pstart;
2561 p = cmdline;
2562 while (qemu_isspace(*p))
2563 p++;
2564 if (*p == '\0')
2565 return NULL;
2566 pstart = p;
2567 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2568 p++;
2569 len = p - pstart;
2570 if (len > nlen - 1)
2571 len = nlen - 1;
2572 memcpy(cmdname, pstart, len);
2573 cmdname[len] = '\0';
2574 return p;
2578 * Read key of 'type' into 'key' and return the current
2579 * 'type' pointer.
2581 static char *key_get_info(const char *type, char **key)
2583 size_t len;
2584 char *p, *str;
2586 if (*type == ',')
2587 type++;
2589 p = strchr(type, ':');
2590 if (!p) {
2591 *key = NULL;
2592 return NULL;
2594 len = p - type;
2596 str = qemu_malloc(len + 1);
2597 memcpy(str, type, len);
2598 str[len] = '\0';
2600 *key = str;
2601 return ++p;
2604 static int default_fmt_format = 'x';
2605 static int default_fmt_size = 4;
2607 #define MAX_ARGS 16
2609 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2610 const char *cmdline,
2611 QDict *qdict)
2613 const char *p, *typestr;
2614 int c;
2615 const mon_cmd_t *cmd;
2616 char cmdname[256];
2617 char buf[1024];
2618 char *key;
2620 #ifdef DEBUG
2621 monitor_printf(mon, "command='%s'\n", cmdline);
2622 #endif
2624 /* extract the command name */
2625 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2626 if (!p)
2627 return NULL;
2629 /* find the command */
2630 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2631 if (compare_cmd(cmdname, cmd->name))
2632 break;
2635 if (cmd->name == NULL) {
2636 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2637 return NULL;
2640 /* parse the parameters */
2641 typestr = cmd->args_type;
2642 for(;;) {
2643 typestr = key_get_info(typestr, &key);
2644 if (!typestr)
2645 break;
2646 c = *typestr;
2647 typestr++;
2648 switch(c) {
2649 case 'F':
2650 case 'B':
2651 case 's':
2653 int ret;
2655 while (qemu_isspace(*p))
2656 p++;
2657 if (*typestr == '?') {
2658 typestr++;
2659 if (*p == '\0') {
2660 /* no optional string: NULL argument */
2661 break;
2664 ret = get_str(buf, sizeof(buf), &p);
2665 if (ret < 0) {
2666 switch(c) {
2667 case 'F':
2668 monitor_printf(mon, "%s: filename expected\n",
2669 cmdname);
2670 break;
2671 case 'B':
2672 monitor_printf(mon, "%s: block device name expected\n",
2673 cmdname);
2674 break;
2675 default:
2676 monitor_printf(mon, "%s: string expected\n", cmdname);
2677 break;
2679 goto fail;
2681 qdict_put(qdict, key, qstring_from_str(buf));
2683 break;
2684 case '/':
2686 int count, format, size;
2688 while (qemu_isspace(*p))
2689 p++;
2690 if (*p == '/') {
2691 /* format found */
2692 p++;
2693 count = 1;
2694 if (qemu_isdigit(*p)) {
2695 count = 0;
2696 while (qemu_isdigit(*p)) {
2697 count = count * 10 + (*p - '0');
2698 p++;
2701 size = -1;
2702 format = -1;
2703 for(;;) {
2704 switch(*p) {
2705 case 'o':
2706 case 'd':
2707 case 'u':
2708 case 'x':
2709 case 'i':
2710 case 'c':
2711 format = *p++;
2712 break;
2713 case 'b':
2714 size = 1;
2715 p++;
2716 break;
2717 case 'h':
2718 size = 2;
2719 p++;
2720 break;
2721 case 'w':
2722 size = 4;
2723 p++;
2724 break;
2725 case 'g':
2726 case 'L':
2727 size = 8;
2728 p++;
2729 break;
2730 default:
2731 goto next;
2734 next:
2735 if (*p != '\0' && !qemu_isspace(*p)) {
2736 monitor_printf(mon, "invalid char in format: '%c'\n",
2737 *p);
2738 goto fail;
2740 if (format < 0)
2741 format = default_fmt_format;
2742 if (format != 'i') {
2743 /* for 'i', not specifying a size gives -1 as size */
2744 if (size < 0)
2745 size = default_fmt_size;
2746 default_fmt_size = size;
2748 default_fmt_format = format;
2749 } else {
2750 count = 1;
2751 format = default_fmt_format;
2752 if (format != 'i') {
2753 size = default_fmt_size;
2754 } else {
2755 size = -1;
2758 qdict_put(qdict, "count", qint_from_int(count));
2759 qdict_put(qdict, "format", qint_from_int(format));
2760 qdict_put(qdict, "size", qint_from_int(size));
2762 break;
2763 case 'i':
2764 case 'l':
2766 int64_t val;
2768 while (qemu_isspace(*p))
2769 p++;
2770 if (*typestr == '?' || *typestr == '.') {
2771 if (*typestr == '?') {
2772 if (*p == '\0') {
2773 typestr++;
2774 break;
2776 } else {
2777 if (*p == '.') {
2778 p++;
2779 while (qemu_isspace(*p))
2780 p++;
2781 } else {
2782 typestr++;
2783 break;
2786 typestr++;
2788 if (get_expr(mon, &val, &p))
2789 goto fail;
2790 /* Check if 'i' is greater than 32-bit */
2791 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2792 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2793 monitor_printf(mon, "integer is for 32-bit values\n");
2794 goto fail;
2796 qdict_put(qdict, key, qint_from_int(val));
2798 break;
2799 case '-':
2801 int has_option;
2802 /* option */
2804 c = *typestr++;
2805 if (c == '\0')
2806 goto bad_type;
2807 while (qemu_isspace(*p))
2808 p++;
2809 has_option = 0;
2810 if (*p == '-') {
2811 p++;
2812 if (*p != c) {
2813 monitor_printf(mon, "%s: unsupported option -%c\n",
2814 cmdname, *p);
2815 goto fail;
2817 p++;
2818 has_option = 1;
2820 qdict_put(qdict, key, qint_from_int(has_option));
2822 break;
2823 default:
2824 bad_type:
2825 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2826 goto fail;
2828 qemu_free(key);
2829 key = NULL;
2831 /* check that all arguments were parsed */
2832 while (qemu_isspace(*p))
2833 p++;
2834 if (*p != '\0') {
2835 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2836 cmdname);
2837 goto fail;
2840 return cmd;
2842 fail:
2843 qemu_free(key);
2844 return NULL;
2847 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2849 QDict *qdict;
2850 const mon_cmd_t *cmd;
2852 qdict = qdict_new();
2854 cmd = monitor_parse_command(mon, cmdline, qdict);
2855 if (cmd) {
2856 void (*handler)(Monitor *mon, const QDict *qdict);
2858 qemu_errors_to_mon(mon);
2860 handler = cmd->handler;
2861 handler(mon, qdict);
2863 qemu_errors_to_previous();
2866 QDECREF(qdict);
2869 static void cmd_completion(const char *name, const char *list)
2871 const char *p, *pstart;
2872 char cmd[128];
2873 int len;
2875 p = list;
2876 for(;;) {
2877 pstart = p;
2878 p = strchr(p, '|');
2879 if (!p)
2880 p = pstart + strlen(pstart);
2881 len = p - pstart;
2882 if (len > sizeof(cmd) - 2)
2883 len = sizeof(cmd) - 2;
2884 memcpy(cmd, pstart, len);
2885 cmd[len] = '\0';
2886 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2887 readline_add_completion(cur_mon->rs, cmd);
2889 if (*p == '\0')
2890 break;
2891 p++;
2895 static void file_completion(const char *input)
2897 DIR *ffs;
2898 struct dirent *d;
2899 char path[1024];
2900 char file[1024], file_prefix[1024];
2901 int input_path_len;
2902 const char *p;
2904 p = strrchr(input, '/');
2905 if (!p) {
2906 input_path_len = 0;
2907 pstrcpy(file_prefix, sizeof(file_prefix), input);
2908 pstrcpy(path, sizeof(path), ".");
2909 } else {
2910 input_path_len = p - input + 1;
2911 memcpy(path, input, input_path_len);
2912 if (input_path_len > sizeof(path) - 1)
2913 input_path_len = sizeof(path) - 1;
2914 path[input_path_len] = '\0';
2915 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2917 #ifdef DEBUG_COMPLETION
2918 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2919 input, path, file_prefix);
2920 #endif
2921 ffs = opendir(path);
2922 if (!ffs)
2923 return;
2924 for(;;) {
2925 struct stat sb;
2926 d = readdir(ffs);
2927 if (!d)
2928 break;
2929 if (strstart(d->d_name, file_prefix, NULL)) {
2930 memcpy(file, input, input_path_len);
2931 if (input_path_len < sizeof(file))
2932 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2933 d->d_name);
2934 /* stat the file to find out if it's a directory.
2935 * In that case add a slash to speed up typing long paths
2937 stat(file, &sb);
2938 if(S_ISDIR(sb.st_mode))
2939 pstrcat(file, sizeof(file), "/");
2940 readline_add_completion(cur_mon->rs, file);
2943 closedir(ffs);
2946 static void block_completion_it(void *opaque, BlockDriverState *bs)
2948 const char *name = bdrv_get_device_name(bs);
2949 const char *input = opaque;
2951 if (input[0] == '\0' ||
2952 !strncmp(name, (char *)input, strlen(input))) {
2953 readline_add_completion(cur_mon->rs, name);
2957 /* NOTE: this parser is an approximate form of the real command parser */
2958 static void parse_cmdline(const char *cmdline,
2959 int *pnb_args, char **args)
2961 const char *p;
2962 int nb_args, ret;
2963 char buf[1024];
2965 p = cmdline;
2966 nb_args = 0;
2967 for(;;) {
2968 while (qemu_isspace(*p))
2969 p++;
2970 if (*p == '\0')
2971 break;
2972 if (nb_args >= MAX_ARGS)
2973 break;
2974 ret = get_str(buf, sizeof(buf), &p);
2975 args[nb_args] = qemu_strdup(buf);
2976 nb_args++;
2977 if (ret < 0)
2978 break;
2980 *pnb_args = nb_args;
2983 static const char *next_arg_type(const char *typestr)
2985 const char *p = strchr(typestr, ':');
2986 return (p != NULL ? ++p : typestr);
2989 static void monitor_find_completion(const char *cmdline)
2991 const char *cmdname;
2992 char *args[MAX_ARGS];
2993 int nb_args, i, len;
2994 const char *ptype, *str;
2995 const mon_cmd_t *cmd;
2996 const KeyDef *key;
2998 parse_cmdline(cmdline, &nb_args, args);
2999 #ifdef DEBUG_COMPLETION
3000 for(i = 0; i < nb_args; i++) {
3001 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3003 #endif
3005 /* if the line ends with a space, it means we want to complete the
3006 next arg */
3007 len = strlen(cmdline);
3008 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3009 if (nb_args >= MAX_ARGS)
3010 return;
3011 args[nb_args++] = qemu_strdup("");
3013 if (nb_args <= 1) {
3014 /* command completion */
3015 if (nb_args == 0)
3016 cmdname = "";
3017 else
3018 cmdname = args[0];
3019 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3020 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3021 cmd_completion(cmdname, cmd->name);
3023 } else {
3024 /* find the command */
3025 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3026 if (compare_cmd(args[0], cmd->name))
3027 goto found;
3029 return;
3030 found:
3031 ptype = next_arg_type(cmd->args_type);
3032 for(i = 0; i < nb_args - 2; i++) {
3033 if (*ptype != '\0') {
3034 ptype = next_arg_type(ptype);
3035 while (*ptype == '?')
3036 ptype = next_arg_type(ptype);
3039 str = args[nb_args - 1];
3040 if (*ptype == '-' && ptype[1] != '\0') {
3041 ptype += 2;
3043 switch(*ptype) {
3044 case 'F':
3045 /* file completion */
3046 readline_set_completion_index(cur_mon->rs, strlen(str));
3047 file_completion(str);
3048 break;
3049 case 'B':
3050 /* block device name completion */
3051 readline_set_completion_index(cur_mon->rs, strlen(str));
3052 bdrv_iterate(block_completion_it, (void *)str);
3053 break;
3054 case 's':
3055 /* XXX: more generic ? */
3056 if (!strcmp(cmd->name, "info")) {
3057 readline_set_completion_index(cur_mon->rs, strlen(str));
3058 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3059 cmd_completion(str, cmd->name);
3061 } else if (!strcmp(cmd->name, "sendkey")) {
3062 char *sep = strrchr(str, '-');
3063 if (sep)
3064 str = sep + 1;
3065 readline_set_completion_index(cur_mon->rs, strlen(str));
3066 for(key = key_defs; key->name != NULL; key++) {
3067 cmd_completion(str, key->name);
3069 } else if (!strcmp(cmd->name, "help|?")) {
3070 readline_set_completion_index(cur_mon->rs, strlen(str));
3071 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3072 cmd_completion(str, cmd->name);
3075 break;
3076 default:
3077 break;
3080 for(i = 0; i < nb_args; i++)
3081 qemu_free(args[i]);
3084 static int monitor_can_read(void *opaque)
3086 Monitor *mon = opaque;
3088 return (mon->suspend_cnt == 0) ? 128 : 0;
3091 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3093 Monitor *old_mon = cur_mon;
3094 int i;
3096 cur_mon = opaque;
3098 if (cur_mon->rs) {
3099 for (i = 0; i < size; i++)
3100 readline_handle_byte(cur_mon->rs, buf[i]);
3101 } else {
3102 if (size == 0 || buf[size - 1] != 0)
3103 monitor_printf(cur_mon, "corrupted command\n");
3104 else
3105 monitor_handle_command(cur_mon, (char *)buf);
3108 cur_mon = old_mon;
3111 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3113 monitor_suspend(mon);
3114 monitor_handle_command(mon, cmdline);
3115 monitor_resume(mon);
3118 int monitor_suspend(Monitor *mon)
3120 if (!mon->rs)
3121 return -ENOTTY;
3122 mon->suspend_cnt++;
3123 return 0;
3126 void monitor_resume(Monitor *mon)
3128 if (!mon->rs)
3129 return;
3130 if (--mon->suspend_cnt == 0)
3131 readline_show_prompt(mon->rs);
3134 static void monitor_event(void *opaque, int event)
3136 Monitor *mon = opaque;
3138 switch (event) {
3139 case CHR_EVENT_MUX_IN:
3140 mon->mux_out = 0;
3141 if (mon->reset_seen) {
3142 readline_restart(mon->rs);
3143 monitor_resume(mon);
3144 monitor_flush(mon);
3145 } else {
3146 mon->suspend_cnt = 0;
3148 break;
3150 case CHR_EVENT_MUX_OUT:
3151 if (mon->reset_seen) {
3152 if (mon->suspend_cnt == 0) {
3153 monitor_printf(mon, "\n");
3155 monitor_flush(mon);
3156 monitor_suspend(mon);
3157 } else {
3158 mon->suspend_cnt++;
3160 mon->mux_out = 1;
3161 break;
3163 case CHR_EVENT_RESET:
3164 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3165 "information\n", QEMU_VERSION);
3166 if (!mon->mux_out) {
3167 readline_show_prompt(mon->rs);
3169 mon->reset_seen = 1;
3170 break;
3176 * Local variables:
3177 * c-indent-level: 4
3178 * c-basic-offset: 4
3179 * tab-width: 8
3180 * End:
3183 void monitor_init(CharDriverState *chr, int flags)
3185 static int is_first_init = 1;
3186 Monitor *mon;
3188 if (is_first_init) {
3189 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3190 is_first_init = 0;
3193 mon = qemu_mallocz(sizeof(*mon));
3195 mon->chr = chr;
3196 mon->flags = flags;
3197 if (flags & MONITOR_USE_READLINE) {
3198 mon->rs = readline_init(mon, monitor_find_completion);
3199 monitor_read_command(mon, 0);
3202 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3203 mon);
3205 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3206 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3207 cur_mon = mon;
3210 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3212 BlockDriverState *bs = opaque;
3213 int ret = 0;
3215 if (bdrv_set_key(bs, password) != 0) {
3216 monitor_printf(mon, "invalid password\n");
3217 ret = -EPERM;
3219 if (mon->password_completion_cb)
3220 mon->password_completion_cb(mon->password_opaque, ret);
3222 monitor_read_command(mon, 1);
3225 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3226 BlockDriverCompletionFunc *completion_cb,
3227 void *opaque)
3229 int err;
3231 if (!bdrv_key_required(bs)) {
3232 if (completion_cb)
3233 completion_cb(opaque, 0);
3234 return;
3237 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3238 bdrv_get_encrypted_filename(bs));
3240 mon->password_completion_cb = completion_cb;
3241 mon->password_opaque = opaque;
3243 err = monitor_read_password(mon, bdrv_password_cb, bs);
3245 if (err && completion_cb)
3246 completion_cb(opaque, err);
3249 typedef struct QemuErrorSink QemuErrorSink;
3250 struct QemuErrorSink {
3251 enum {
3252 ERR_SINK_FILE,
3253 ERR_SINK_MONITOR,
3254 } dest;
3255 union {
3256 FILE *fp;
3257 Monitor *mon;
3259 QemuErrorSink *previous;
3262 static QemuErrorSink *qemu_error_sink;
3264 void qemu_errors_to_file(FILE *fp)
3266 QemuErrorSink *sink;
3268 sink = qemu_mallocz(sizeof(*sink));
3269 sink->dest = ERR_SINK_FILE;
3270 sink->fp = fp;
3271 sink->previous = qemu_error_sink;
3272 qemu_error_sink = sink;
3275 void qemu_errors_to_mon(Monitor *mon)
3277 QemuErrorSink *sink;
3279 sink = qemu_mallocz(sizeof(*sink));
3280 sink->dest = ERR_SINK_MONITOR;
3281 sink->mon = mon;
3282 sink->previous = qemu_error_sink;
3283 qemu_error_sink = sink;
3286 void qemu_errors_to_previous(void)
3288 QemuErrorSink *sink;
3290 assert(qemu_error_sink != NULL);
3291 sink = qemu_error_sink;
3292 qemu_error_sink = sink->previous;
3293 qemu_free(sink);
3296 void qemu_error(const char *fmt, ...)
3298 va_list args;
3300 assert(qemu_error_sink != NULL);
3301 switch (qemu_error_sink->dest) {
3302 case ERR_SINK_FILE:
3303 va_start(args, fmt);
3304 vfprintf(qemu_error_sink->fp, fmt, args);
3305 va_end(args);
3306 break;
3307 case ERR_SINK_MONITOR:
3308 va_start(args, fmt);
3309 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3310 va_end(args);
3311 break;