qemu/pci: make pci not depend on msix
[armpft.git] / monitor.c
blobf79b83355aa60bc8039faa7a28a8d4f2d6370814
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <dirent.h>
25 #include "hw/hw.h"
26 #include "hw/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/pcmcia.h"
29 #include "hw/pc.h"
30 #include "hw/pci.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
33 #include "gdbstub.h"
34 #include "net.h"
35 #include "qemu-char.h"
36 #include "sysemu.h"
37 #include "monitor.h"
38 #include "readline.h"
39 #include "console.h"
40 #include "block.h"
41 #include "audio/audio.h"
42 #include "disas.h"
43 #include "balloon.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
46 #include "kvm.h"
47 #include "acl.h"
48 #include "qint.h"
49 #include "qdict.h"
50 #include "qstring.h"
52 //#define DEBUG
53 //#define DEBUG_COMPLETION
56 * Supported types:
58 * 'F' filename
59 * 'B' block device name
60 * 's' string (accept optional quote)
61 * 'i' 32 bit integer
62 * 'l' target long (32 or 64 bit)
63 * '/' optional gdb-like print format (like "/10x")
65 * '?' optional type (for all types, except '/')
66 * '.' other form of optional type (for 'i' and 'l')
67 * '-' optional parameter (eg. '-f')
71 typedef struct mon_cmd_t {
72 const char *name;
73 const char *args_type;
74 void *handler;
75 const char *params;
76 const char *help;
77 } mon_cmd_t;
79 /* file descriptors passed via SCM_RIGHTS */
80 typedef struct mon_fd_t mon_fd_t;
81 struct mon_fd_t {
82 char *name;
83 int fd;
84 QLIST_ENTRY(mon_fd_t) next;
87 struct Monitor {
88 CharDriverState *chr;
89 int mux_out;
90 int reset_seen;
91 int flags;
92 int suspend_cnt;
93 uint8_t outbuf[1024];
94 int outbuf_index;
95 ReadLineState *rs;
96 CPUState *mon_cpu;
97 BlockDriverCompletionFunc *password_completion_cb;
98 void *password_opaque;
99 QLIST_HEAD(,mon_fd_t) fds;
100 QLIST_ENTRY(Monitor) entry;
103 static QLIST_HEAD(mon_list, Monitor) mon_list;
105 static const mon_cmd_t mon_cmds[];
106 static const mon_cmd_t info_cmds[];
108 Monitor *cur_mon = NULL;
110 static void monitor_command_cb(Monitor *mon, const char *cmdline,
111 void *opaque);
113 static void monitor_read_command(Monitor *mon, int show_prompt)
115 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
116 if (show_prompt)
117 readline_show_prompt(mon->rs);
120 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
121 void *opaque)
123 if (mon->rs) {
124 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
125 /* prompt is printed on return from the command handler */
126 return 0;
127 } else {
128 monitor_printf(mon, "terminal does not support password prompting\n");
129 return -ENOTTY;
133 void monitor_flush(Monitor *mon)
135 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
136 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
137 mon->outbuf_index = 0;
141 /* flush at every end of line or if the buffer is full */
142 static void monitor_puts(Monitor *mon, const char *str)
144 char c;
146 if (!mon)
147 return;
149 for(;;) {
150 c = *str++;
151 if (c == '\0')
152 break;
153 if (c == '\n')
154 mon->outbuf[mon->outbuf_index++] = '\r';
155 mon->outbuf[mon->outbuf_index++] = c;
156 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
157 || c == '\n')
158 monitor_flush(mon);
162 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
164 char buf[4096];
165 vsnprintf(buf, sizeof(buf), fmt, ap);
166 monitor_puts(mon, buf);
169 void monitor_printf(Monitor *mon, const char *fmt, ...)
171 va_list ap;
172 va_start(ap, fmt);
173 monitor_vprintf(mon, fmt, ap);
174 va_end(ap);
177 void monitor_print_filename(Monitor *mon, const char *filename)
179 int i;
181 for (i = 0; filename[i]; i++) {
182 switch (filename[i]) {
183 case ' ':
184 case '"':
185 case '\\':
186 monitor_printf(mon, "\\%c", filename[i]);
187 break;
188 case '\t':
189 monitor_printf(mon, "\\t");
190 break;
191 case '\r':
192 monitor_printf(mon, "\\r");
193 break;
194 case '\n':
195 monitor_printf(mon, "\\n");
196 break;
197 default:
198 monitor_printf(mon, "%c", filename[i]);
199 break;
204 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
206 va_list ap;
207 va_start(ap, fmt);
208 monitor_vprintf((Monitor *)stream, fmt, ap);
209 va_end(ap);
210 return 0;
213 static int compare_cmd(const char *name, const char *list)
215 const char *p, *pstart;
216 int len;
217 len = strlen(name);
218 p = list;
219 for(;;) {
220 pstart = p;
221 p = strchr(p, '|');
222 if (!p)
223 p = pstart + strlen(pstart);
224 if ((p - pstart) == len && !memcmp(pstart, name, len))
225 return 1;
226 if (*p == '\0')
227 break;
228 p++;
230 return 0;
233 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
234 const char *prefix, const char *name)
236 const mon_cmd_t *cmd;
238 for(cmd = cmds; cmd->name != NULL; cmd++) {
239 if (!name || !strcmp(name, cmd->name))
240 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
241 cmd->params, cmd->help);
245 static void help_cmd(Monitor *mon, const char *name)
247 if (name && !strcmp(name, "info")) {
248 help_cmd_dump(mon, info_cmds, "info ", NULL);
249 } else {
250 help_cmd_dump(mon, mon_cmds, "", name);
251 if (name && !strcmp(name, "log")) {
252 const CPULogItem *item;
253 monitor_printf(mon, "Log items (comma separated):\n");
254 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
255 for(item = cpu_log_items; item->mask != 0; item++) {
256 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
262 static void do_help_cmd(Monitor *mon, const QDict *qdict)
264 help_cmd(mon, qdict_get_try_str(qdict, "name"));
267 static void do_commit(Monitor *mon, const QDict *qdict)
269 int all_devices;
270 DriveInfo *dinfo;
271 const char *device = qdict_get_str(qdict, "device");
273 all_devices = !strcmp(device, "all");
274 QTAILQ_FOREACH(dinfo, &drives, next) {
275 if (!all_devices)
276 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
277 continue;
278 bdrv_commit(dinfo->bdrv);
282 static void do_info(Monitor *mon, const QDict *qdict)
284 const mon_cmd_t *cmd;
285 const char *item = qdict_get_try_str(qdict, "item");
286 void (*handler)(Monitor *);
288 if (!item)
289 goto help;
290 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
291 if (compare_cmd(item, cmd->name))
292 goto found;
294 help:
295 help_cmd(mon, "info");
296 return;
297 found:
298 handler = cmd->handler;
299 handler(mon);
302 static void do_info_version(Monitor *mon)
304 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
307 static void do_info_name(Monitor *mon)
309 if (qemu_name)
310 monitor_printf(mon, "%s\n", qemu_name);
313 #if defined(TARGET_I386)
314 static void do_info_hpet(Monitor *mon)
316 monitor_printf(mon, "HPET is %s by QEMU\n",
317 (no_hpet) ? "disabled" : "enabled");
319 #endif
321 static void do_info_uuid(Monitor *mon)
323 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
324 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
325 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
326 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
327 qemu_uuid[14], qemu_uuid[15]);
330 /* get the current CPU defined by the user */
331 static int mon_set_cpu(int cpu_index)
333 CPUState *env;
335 for(env = first_cpu; env != NULL; env = env->next_cpu) {
336 if (env->cpu_index == cpu_index) {
337 cur_mon->mon_cpu = env;
338 return 0;
341 return -1;
344 static CPUState *mon_get_cpu(void)
346 if (!cur_mon->mon_cpu) {
347 mon_set_cpu(0);
349 cpu_synchronize_state(cur_mon->mon_cpu);
350 return cur_mon->mon_cpu;
353 static void do_info_registers(Monitor *mon)
355 CPUState *env;
356 env = mon_get_cpu();
357 if (!env)
358 return;
359 #ifdef TARGET_I386
360 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
361 X86_DUMP_FPU);
362 #else
363 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
365 #endif
368 static void do_info_cpus(Monitor *mon)
370 CPUState *env;
372 /* just to set the default cpu if not already done */
373 mon_get_cpu();
375 for(env = first_cpu; env != NULL; env = env->next_cpu) {
376 cpu_synchronize_state(env);
377 monitor_printf(mon, "%c CPU #%d:",
378 (env == mon->mon_cpu) ? '*' : ' ',
379 env->cpu_index);
380 #if defined(TARGET_I386)
381 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
382 env->eip + env->segs[R_CS].base);
383 #elif defined(TARGET_PPC)
384 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
385 #elif defined(TARGET_SPARC)
386 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
387 env->pc, env->npc);
388 #elif defined(TARGET_MIPS)
389 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
390 #endif
391 if (env->halted)
392 monitor_printf(mon, " (halted)");
393 monitor_printf(mon, "\n");
397 static void do_cpu_set(Monitor *mon, const QDict *qdict)
399 int index = qdict_get_int(qdict, "index");
400 if (mon_set_cpu(index) < 0)
401 monitor_printf(mon, "Invalid CPU index\n");
404 static void do_info_jit(Monitor *mon)
406 dump_exec_info((FILE *)mon, monitor_fprintf);
409 static void do_info_history(Monitor *mon)
411 int i;
412 const char *str;
414 if (!mon->rs)
415 return;
416 i = 0;
417 for(;;) {
418 str = readline_get_history(mon->rs, i);
419 if (!str)
420 break;
421 monitor_printf(mon, "%d: '%s'\n", i, str);
422 i++;
426 #if defined(TARGET_PPC)
427 /* XXX: not implemented in other targets */
428 static void do_info_cpu_stats(Monitor *mon)
430 CPUState *env;
432 env = mon_get_cpu();
433 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
435 #endif
437 static void do_quit(Monitor *mon, const QDict *qdict)
439 exit(0);
442 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
444 if (bdrv_is_inserted(bs)) {
445 if (!force) {
446 if (!bdrv_is_removable(bs)) {
447 monitor_printf(mon, "device is not removable\n");
448 return -1;
450 if (bdrv_is_locked(bs)) {
451 monitor_printf(mon, "device is locked\n");
452 return -1;
455 bdrv_close(bs);
457 return 0;
460 static void do_eject(Monitor *mon, const QDict *qdict)
462 BlockDriverState *bs;
463 int force = qdict_get_int(qdict, "force");
464 const char *filename = qdict_get_str(qdict, "filename");
466 bs = bdrv_find(filename);
467 if (!bs) {
468 monitor_printf(mon, "device not found\n");
469 return;
471 eject_device(mon, bs, force);
474 static void do_change_block(Monitor *mon, const char *device,
475 const char *filename, const char *fmt)
477 BlockDriverState *bs;
478 BlockDriver *drv = NULL;
480 bs = bdrv_find(device);
481 if (!bs) {
482 monitor_printf(mon, "device not found\n");
483 return;
485 if (fmt) {
486 drv = bdrv_find_format(fmt);
487 if (!drv) {
488 monitor_printf(mon, "invalid format %s\n", fmt);
489 return;
492 if (eject_device(mon, bs, 0) < 0)
493 return;
494 bdrv_open2(bs, filename, 0, drv);
495 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
498 static void change_vnc_password_cb(Monitor *mon, const char *password,
499 void *opaque)
501 if (vnc_display_password(NULL, password) < 0)
502 monitor_printf(mon, "could not set VNC server password\n");
504 monitor_read_command(mon, 1);
507 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
509 if (strcmp(target, "passwd") == 0 ||
510 strcmp(target, "password") == 0) {
511 if (arg) {
512 char password[9];
513 strncpy(password, arg, sizeof(password));
514 password[sizeof(password) - 1] = '\0';
515 change_vnc_password_cb(mon, password, NULL);
516 } else {
517 monitor_read_password(mon, change_vnc_password_cb, NULL);
519 } else {
520 if (vnc_display_open(NULL, target) < 0)
521 monitor_printf(mon, "could not start VNC server on %s\n", target);
525 static void do_change(Monitor *mon, const QDict *qdict)
527 const char *device = qdict_get_str(qdict, "device");
528 const char *target = qdict_get_str(qdict, "target");
529 const char *arg = qdict_get_try_str(qdict, "arg");
530 if (strcmp(device, "vnc") == 0) {
531 do_change_vnc(mon, target, arg);
532 } else {
533 do_change_block(mon, device, target, arg);
537 static void do_screen_dump(Monitor *mon, const QDict *qdict)
539 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
542 static void do_logfile(Monitor *mon, const QDict *qdict)
544 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
547 static void do_log(Monitor *mon, const QDict *qdict)
549 int mask;
550 const char *items = qdict_get_str(qdict, "items");
552 if (!strcmp(items, "none")) {
553 mask = 0;
554 } else {
555 mask = cpu_str_to_log_mask(items);
556 if (!mask) {
557 help_cmd(mon, "log");
558 return;
561 cpu_set_log(mask);
564 static void do_singlestep(Monitor *mon, const QDict *qdict)
566 const char *option = qdict_get_try_str(qdict, "option");
567 if (!option || !strcmp(option, "on")) {
568 singlestep = 1;
569 } else if (!strcmp(option, "off")) {
570 singlestep = 0;
571 } else {
572 monitor_printf(mon, "unexpected option %s\n", option);
576 static void do_stop(Monitor *mon, const QDict *qdict)
578 vm_stop(EXCP_INTERRUPT);
581 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
583 struct bdrv_iterate_context {
584 Monitor *mon;
585 int err;
588 static void do_cont(Monitor *mon, const QDict *qdict)
590 struct bdrv_iterate_context context = { mon, 0 };
592 bdrv_iterate(encrypted_bdrv_it, &context);
593 /* only resume the vm if all keys are set and valid */
594 if (!context.err)
595 vm_start();
598 static void bdrv_key_cb(void *opaque, int err)
600 Monitor *mon = opaque;
602 /* another key was set successfully, retry to continue */
603 if (!err)
604 do_cont(mon, NULL);
607 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
609 struct bdrv_iterate_context *context = opaque;
611 if (!context->err && bdrv_key_required(bs)) {
612 context->err = -EBUSY;
613 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
614 context->mon);
618 static void do_gdbserver(Monitor *mon, const QDict *qdict)
620 const char *device = qdict_get_try_str(qdict, "device");
621 if (!device)
622 device = "tcp::" DEFAULT_GDBSTUB_PORT;
623 if (gdbserver_start(device) < 0) {
624 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
625 device);
626 } else if (strcmp(device, "none") == 0) {
627 monitor_printf(mon, "Disabled gdbserver\n");
628 } else {
629 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
630 device);
634 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
636 const char *action = qdict_get_str(qdict, "action");
637 if (select_watchdog_action(action) == -1) {
638 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
642 static void monitor_printc(Monitor *mon, int c)
644 monitor_printf(mon, "'");
645 switch(c) {
646 case '\'':
647 monitor_printf(mon, "\\'");
648 break;
649 case '\\':
650 monitor_printf(mon, "\\\\");
651 break;
652 case '\n':
653 monitor_printf(mon, "\\n");
654 break;
655 case '\r':
656 monitor_printf(mon, "\\r");
657 break;
658 default:
659 if (c >= 32 && c <= 126) {
660 monitor_printf(mon, "%c", c);
661 } else {
662 monitor_printf(mon, "\\x%02x", c);
664 break;
666 monitor_printf(mon, "'");
669 static void memory_dump(Monitor *mon, int count, int format, int wsize,
670 target_phys_addr_t addr, int is_physical)
672 CPUState *env;
673 int nb_per_line, l, line_size, i, max_digits, len;
674 uint8_t buf[16];
675 uint64_t v;
677 if (format == 'i') {
678 int flags;
679 flags = 0;
680 env = mon_get_cpu();
681 if (!env && !is_physical)
682 return;
683 #ifdef TARGET_I386
684 if (wsize == 2) {
685 flags = 1;
686 } else if (wsize == 4) {
687 flags = 0;
688 } else {
689 /* as default we use the current CS size */
690 flags = 0;
691 if (env) {
692 #ifdef TARGET_X86_64
693 if ((env->efer & MSR_EFER_LMA) &&
694 (env->segs[R_CS].flags & DESC_L_MASK))
695 flags = 2;
696 else
697 #endif
698 if (!(env->segs[R_CS].flags & DESC_B_MASK))
699 flags = 1;
702 #endif
703 monitor_disas(mon, env, addr, count, is_physical, flags);
704 return;
707 len = wsize * count;
708 if (wsize == 1)
709 line_size = 8;
710 else
711 line_size = 16;
712 nb_per_line = line_size / wsize;
713 max_digits = 0;
715 switch(format) {
716 case 'o':
717 max_digits = (wsize * 8 + 2) / 3;
718 break;
719 default:
720 case 'x':
721 max_digits = (wsize * 8) / 4;
722 break;
723 case 'u':
724 case 'd':
725 max_digits = (wsize * 8 * 10 + 32) / 33;
726 break;
727 case 'c':
728 wsize = 1;
729 break;
732 while (len > 0) {
733 if (is_physical)
734 monitor_printf(mon, TARGET_FMT_plx ":", addr);
735 else
736 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
737 l = len;
738 if (l > line_size)
739 l = line_size;
740 if (is_physical) {
741 cpu_physical_memory_rw(addr, buf, l, 0);
742 } else {
743 env = mon_get_cpu();
744 if (!env)
745 break;
746 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
747 monitor_printf(mon, " Cannot access memory\n");
748 break;
751 i = 0;
752 while (i < l) {
753 switch(wsize) {
754 default:
755 case 1:
756 v = ldub_raw(buf + i);
757 break;
758 case 2:
759 v = lduw_raw(buf + i);
760 break;
761 case 4:
762 v = (uint32_t)ldl_raw(buf + i);
763 break;
764 case 8:
765 v = ldq_raw(buf + i);
766 break;
768 monitor_printf(mon, " ");
769 switch(format) {
770 case 'o':
771 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
772 break;
773 case 'x':
774 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
775 break;
776 case 'u':
777 monitor_printf(mon, "%*" PRIu64, max_digits, v);
778 break;
779 case 'd':
780 monitor_printf(mon, "%*" PRId64, max_digits, v);
781 break;
782 case 'c':
783 monitor_printc(mon, v);
784 break;
786 i += wsize;
788 monitor_printf(mon, "\n");
789 addr += l;
790 len -= l;
794 static void do_memory_dump(Monitor *mon, const QDict *qdict)
796 int count = qdict_get_int(qdict, "count");
797 int format = qdict_get_int(qdict, "format");
798 int size = qdict_get_int(qdict, "size");
799 target_long addr = qdict_get_int(qdict, "addr");
801 memory_dump(mon, count, format, size, addr, 0);
804 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
806 int count = qdict_get_int(qdict, "count");
807 int format = qdict_get_int(qdict, "format");
808 int size = qdict_get_int(qdict, "size");
809 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
811 memory_dump(mon, count, format, size, addr, 1);
814 static void do_print(Monitor *mon, const QDict *qdict)
816 int format = qdict_get_int(qdict, "format");
817 target_phys_addr_t val = qdict_get_int(qdict, "val");
819 #if TARGET_PHYS_ADDR_BITS == 32
820 switch(format) {
821 case 'o':
822 monitor_printf(mon, "%#o", val);
823 break;
824 case 'x':
825 monitor_printf(mon, "%#x", val);
826 break;
827 case 'u':
828 monitor_printf(mon, "%u", val);
829 break;
830 default:
831 case 'd':
832 monitor_printf(mon, "%d", val);
833 break;
834 case 'c':
835 monitor_printc(mon, val);
836 break;
838 #else
839 switch(format) {
840 case 'o':
841 monitor_printf(mon, "%#" PRIo64, val);
842 break;
843 case 'x':
844 monitor_printf(mon, "%#" PRIx64, val);
845 break;
846 case 'u':
847 monitor_printf(mon, "%" PRIu64, val);
848 break;
849 default:
850 case 'd':
851 monitor_printf(mon, "%" PRId64, val);
852 break;
853 case 'c':
854 monitor_printc(mon, val);
855 break;
857 #endif
858 monitor_printf(mon, "\n");
861 static void do_memory_save(Monitor *mon, const QDict *qdict)
863 FILE *f;
864 uint32_t size = qdict_get_int(qdict, "size");
865 const char *filename = qdict_get_str(qdict, "filename");
866 target_long addr = qdict_get_int(qdict, "val");
867 uint32_t l;
868 CPUState *env;
869 uint8_t buf[1024];
871 env = mon_get_cpu();
872 if (!env)
873 return;
875 f = fopen(filename, "wb");
876 if (!f) {
877 monitor_printf(mon, "could not open '%s'\n", filename);
878 return;
880 while (size != 0) {
881 l = sizeof(buf);
882 if (l > size)
883 l = size;
884 cpu_memory_rw_debug(env, addr, buf, l, 0);
885 fwrite(buf, 1, l, f);
886 addr += l;
887 size -= l;
889 fclose(f);
892 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
894 FILE *f;
895 uint32_t l;
896 uint8_t buf[1024];
897 uint32_t size = qdict_get_int(qdict, "size");
898 const char *filename = qdict_get_str(qdict, "filename");
899 target_phys_addr_t addr = qdict_get_int(qdict, "val");
901 f = fopen(filename, "wb");
902 if (!f) {
903 monitor_printf(mon, "could not open '%s'\n", filename);
904 return;
906 while (size != 0) {
907 l = sizeof(buf);
908 if (l > size)
909 l = size;
910 cpu_physical_memory_rw(addr, buf, l, 0);
911 fwrite(buf, 1, l, f);
912 fflush(f);
913 addr += l;
914 size -= l;
916 fclose(f);
919 static void do_sum(Monitor *mon, const QDict *qdict)
921 uint32_t addr;
922 uint8_t buf[1];
923 uint16_t sum;
924 uint32_t start = qdict_get_int(qdict, "start");
925 uint32_t size = qdict_get_int(qdict, "size");
927 sum = 0;
928 for(addr = start; addr < (start + size); addr++) {
929 cpu_physical_memory_rw(addr, buf, 1, 0);
930 /* BSD sum algorithm ('sum' Unix command) */
931 sum = (sum >> 1) | (sum << 15);
932 sum += buf[0];
934 monitor_printf(mon, "%05d\n", sum);
937 typedef struct {
938 int keycode;
939 const char *name;
940 } KeyDef;
942 static const KeyDef key_defs[] = {
943 { 0x2a, "shift" },
944 { 0x36, "shift_r" },
946 { 0x38, "alt" },
947 { 0xb8, "alt_r" },
948 { 0x64, "altgr" },
949 { 0xe4, "altgr_r" },
950 { 0x1d, "ctrl" },
951 { 0x9d, "ctrl_r" },
953 { 0xdd, "menu" },
955 { 0x01, "esc" },
957 { 0x02, "1" },
958 { 0x03, "2" },
959 { 0x04, "3" },
960 { 0x05, "4" },
961 { 0x06, "5" },
962 { 0x07, "6" },
963 { 0x08, "7" },
964 { 0x09, "8" },
965 { 0x0a, "9" },
966 { 0x0b, "0" },
967 { 0x0c, "minus" },
968 { 0x0d, "equal" },
969 { 0x0e, "backspace" },
971 { 0x0f, "tab" },
972 { 0x10, "q" },
973 { 0x11, "w" },
974 { 0x12, "e" },
975 { 0x13, "r" },
976 { 0x14, "t" },
977 { 0x15, "y" },
978 { 0x16, "u" },
979 { 0x17, "i" },
980 { 0x18, "o" },
981 { 0x19, "p" },
983 { 0x1c, "ret" },
985 { 0x1e, "a" },
986 { 0x1f, "s" },
987 { 0x20, "d" },
988 { 0x21, "f" },
989 { 0x22, "g" },
990 { 0x23, "h" },
991 { 0x24, "j" },
992 { 0x25, "k" },
993 { 0x26, "l" },
995 { 0x2c, "z" },
996 { 0x2d, "x" },
997 { 0x2e, "c" },
998 { 0x2f, "v" },
999 { 0x30, "b" },
1000 { 0x31, "n" },
1001 { 0x32, "m" },
1002 { 0x33, "comma" },
1003 { 0x34, "dot" },
1004 { 0x35, "slash" },
1006 { 0x37, "asterisk" },
1008 { 0x39, "spc" },
1009 { 0x3a, "caps_lock" },
1010 { 0x3b, "f1" },
1011 { 0x3c, "f2" },
1012 { 0x3d, "f3" },
1013 { 0x3e, "f4" },
1014 { 0x3f, "f5" },
1015 { 0x40, "f6" },
1016 { 0x41, "f7" },
1017 { 0x42, "f8" },
1018 { 0x43, "f9" },
1019 { 0x44, "f10" },
1020 { 0x45, "num_lock" },
1021 { 0x46, "scroll_lock" },
1023 { 0xb5, "kp_divide" },
1024 { 0x37, "kp_multiply" },
1025 { 0x4a, "kp_subtract" },
1026 { 0x4e, "kp_add" },
1027 { 0x9c, "kp_enter" },
1028 { 0x53, "kp_decimal" },
1029 { 0x54, "sysrq" },
1031 { 0x52, "kp_0" },
1032 { 0x4f, "kp_1" },
1033 { 0x50, "kp_2" },
1034 { 0x51, "kp_3" },
1035 { 0x4b, "kp_4" },
1036 { 0x4c, "kp_5" },
1037 { 0x4d, "kp_6" },
1038 { 0x47, "kp_7" },
1039 { 0x48, "kp_8" },
1040 { 0x49, "kp_9" },
1042 { 0x56, "<" },
1044 { 0x57, "f11" },
1045 { 0x58, "f12" },
1047 { 0xb7, "print" },
1049 { 0xc7, "home" },
1050 { 0xc9, "pgup" },
1051 { 0xd1, "pgdn" },
1052 { 0xcf, "end" },
1054 { 0xcb, "left" },
1055 { 0xc8, "up" },
1056 { 0xd0, "down" },
1057 { 0xcd, "right" },
1059 { 0xd2, "insert" },
1060 { 0xd3, "delete" },
1061 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1062 { 0xf0, "stop" },
1063 { 0xf1, "again" },
1064 { 0xf2, "props" },
1065 { 0xf3, "undo" },
1066 { 0xf4, "front" },
1067 { 0xf5, "copy" },
1068 { 0xf6, "open" },
1069 { 0xf7, "paste" },
1070 { 0xf8, "find" },
1071 { 0xf9, "cut" },
1072 { 0xfa, "lf" },
1073 { 0xfb, "help" },
1074 { 0xfc, "meta_l" },
1075 { 0xfd, "meta_r" },
1076 { 0xfe, "compose" },
1077 #endif
1078 { 0, NULL },
1081 static int get_keycode(const char *key)
1083 const KeyDef *p;
1084 char *endp;
1085 int ret;
1087 for(p = key_defs; p->name != NULL; p++) {
1088 if (!strcmp(key, p->name))
1089 return p->keycode;
1091 if (strstart(key, "0x", NULL)) {
1092 ret = strtoul(key, &endp, 0);
1093 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1094 return ret;
1096 return -1;
1099 #define MAX_KEYCODES 16
1100 static uint8_t keycodes[MAX_KEYCODES];
1101 static int nb_pending_keycodes;
1102 static QEMUTimer *key_timer;
1104 static void release_keys(void *opaque)
1106 int keycode;
1108 while (nb_pending_keycodes > 0) {
1109 nb_pending_keycodes--;
1110 keycode = keycodes[nb_pending_keycodes];
1111 if (keycode & 0x80)
1112 kbd_put_keycode(0xe0);
1113 kbd_put_keycode(keycode | 0x80);
1117 static void do_sendkey(Monitor *mon, const QDict *qdict)
1119 char keyname_buf[16];
1120 char *separator;
1121 int keyname_len, keycode, i;
1122 const char *string = qdict_get_str(qdict, "string");
1123 int has_hold_time = qdict_haskey(qdict, "hold_time");
1124 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1126 if (nb_pending_keycodes > 0) {
1127 qemu_del_timer(key_timer);
1128 release_keys(NULL);
1130 if (!has_hold_time)
1131 hold_time = 100;
1132 i = 0;
1133 while (1) {
1134 separator = strchr(string, '-');
1135 keyname_len = separator ? separator - string : strlen(string);
1136 if (keyname_len > 0) {
1137 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1138 if (keyname_len > sizeof(keyname_buf) - 1) {
1139 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1140 return;
1142 if (i == MAX_KEYCODES) {
1143 monitor_printf(mon, "too many keys\n");
1144 return;
1146 keyname_buf[keyname_len] = 0;
1147 keycode = get_keycode(keyname_buf);
1148 if (keycode < 0) {
1149 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1150 return;
1152 keycodes[i++] = keycode;
1154 if (!separator)
1155 break;
1156 string = separator + 1;
1158 nb_pending_keycodes = i;
1159 /* key down events */
1160 for (i = 0; i < nb_pending_keycodes; i++) {
1161 keycode = keycodes[i];
1162 if (keycode & 0x80)
1163 kbd_put_keycode(0xe0);
1164 kbd_put_keycode(keycode & 0x7f);
1166 /* delayed key up events */
1167 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1168 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1171 static int mouse_button_state;
1173 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1175 int dx, dy, dz;
1176 const char *dx_str = qdict_get_str(qdict, "dx_str");
1177 const char *dy_str = qdict_get_str(qdict, "dy_str");
1178 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1179 dx = strtol(dx_str, NULL, 0);
1180 dy = strtol(dy_str, NULL, 0);
1181 dz = 0;
1182 if (dz_str)
1183 dz = strtol(dz_str, NULL, 0);
1184 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1187 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1189 int button_state = qdict_get_int(qdict, "button_state");
1190 mouse_button_state = button_state;
1191 kbd_mouse_event(0, 0, 0, mouse_button_state);
1194 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1196 int size = qdict_get_int(qdict, "size");
1197 int addr = qdict_get_int(qdict, "addr");
1198 int has_index = qdict_haskey(qdict, "index");
1199 uint32_t val;
1200 int suffix;
1202 if (has_index) {
1203 int index = qdict_get_int(qdict, "index");
1204 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1205 addr++;
1207 addr &= 0xffff;
1209 switch(size) {
1210 default:
1211 case 1:
1212 val = cpu_inb(addr);
1213 suffix = 'b';
1214 break;
1215 case 2:
1216 val = cpu_inw(addr);
1217 suffix = 'w';
1218 break;
1219 case 4:
1220 val = cpu_inl(addr);
1221 suffix = 'l';
1222 break;
1224 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1225 suffix, addr, size * 2, val);
1228 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1230 int size = qdict_get_int(qdict, "size");
1231 int addr = qdict_get_int(qdict, "addr");
1232 int val = qdict_get_int(qdict, "val");
1234 addr &= IOPORTS_MASK;
1236 switch (size) {
1237 default:
1238 case 1:
1239 cpu_outb(addr, val);
1240 break;
1241 case 2:
1242 cpu_outw(addr, val);
1243 break;
1244 case 4:
1245 cpu_outl(addr, val);
1246 break;
1250 static void do_boot_set(Monitor *mon, const QDict *qdict)
1252 int res;
1253 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1255 res = qemu_boot_set(bootdevice);
1256 if (res == 0) {
1257 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1258 } else if (res > 0) {
1259 monitor_printf(mon, "setting boot device list failed\n");
1260 } else {
1261 monitor_printf(mon, "no function defined to set boot device list for "
1262 "this architecture\n");
1266 static void do_system_reset(Monitor *mon, const QDict *qdict)
1268 qemu_system_reset_request();
1271 static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1273 qemu_system_powerdown_request();
1276 #if defined(TARGET_I386)
1277 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1279 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1280 addr,
1281 pte & mask,
1282 pte & PG_GLOBAL_MASK ? 'G' : '-',
1283 pte & PG_PSE_MASK ? 'P' : '-',
1284 pte & PG_DIRTY_MASK ? 'D' : '-',
1285 pte & PG_ACCESSED_MASK ? 'A' : '-',
1286 pte & PG_PCD_MASK ? 'C' : '-',
1287 pte & PG_PWT_MASK ? 'T' : '-',
1288 pte & PG_USER_MASK ? 'U' : '-',
1289 pte & PG_RW_MASK ? 'W' : '-');
1292 static void tlb_info(Monitor *mon)
1294 CPUState *env;
1295 int l1, l2;
1296 uint32_t pgd, pde, pte;
1298 env = mon_get_cpu();
1299 if (!env)
1300 return;
1302 if (!(env->cr[0] & CR0_PG_MASK)) {
1303 monitor_printf(mon, "PG disabled\n");
1304 return;
1306 pgd = env->cr[3] & ~0xfff;
1307 for(l1 = 0; l1 < 1024; l1++) {
1308 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1309 pde = le32_to_cpu(pde);
1310 if (pde & PG_PRESENT_MASK) {
1311 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1312 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1313 } else {
1314 for(l2 = 0; l2 < 1024; l2++) {
1315 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1316 (uint8_t *)&pte, 4);
1317 pte = le32_to_cpu(pte);
1318 if (pte & PG_PRESENT_MASK) {
1319 print_pte(mon, (l1 << 22) + (l2 << 12),
1320 pte & ~PG_PSE_MASK,
1321 ~0xfff);
1329 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1330 uint32_t end, int prot)
1332 int prot1;
1333 prot1 = *plast_prot;
1334 if (prot != prot1) {
1335 if (*pstart != -1) {
1336 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1337 *pstart, end, end - *pstart,
1338 prot1 & PG_USER_MASK ? 'u' : '-',
1339 'r',
1340 prot1 & PG_RW_MASK ? 'w' : '-');
1342 if (prot != 0)
1343 *pstart = end;
1344 else
1345 *pstart = -1;
1346 *plast_prot = prot;
1350 static void mem_info(Monitor *mon)
1352 CPUState *env;
1353 int l1, l2, prot, last_prot;
1354 uint32_t pgd, pde, pte, start, end;
1356 env = mon_get_cpu();
1357 if (!env)
1358 return;
1360 if (!(env->cr[0] & CR0_PG_MASK)) {
1361 monitor_printf(mon, "PG disabled\n");
1362 return;
1364 pgd = env->cr[3] & ~0xfff;
1365 last_prot = 0;
1366 start = -1;
1367 for(l1 = 0; l1 < 1024; l1++) {
1368 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1369 pde = le32_to_cpu(pde);
1370 end = l1 << 22;
1371 if (pde & PG_PRESENT_MASK) {
1372 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1373 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1374 mem_print(mon, &start, &last_prot, end, prot);
1375 } else {
1376 for(l2 = 0; l2 < 1024; l2++) {
1377 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1378 (uint8_t *)&pte, 4);
1379 pte = le32_to_cpu(pte);
1380 end = (l1 << 22) + (l2 << 12);
1381 if (pte & PG_PRESENT_MASK) {
1382 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1383 } else {
1384 prot = 0;
1386 mem_print(mon, &start, &last_prot, end, prot);
1389 } else {
1390 prot = 0;
1391 mem_print(mon, &start, &last_prot, end, prot);
1395 #endif
1397 #if defined(TARGET_SH4)
1399 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1401 monitor_printf(mon, " tlb%i:\t"
1402 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1403 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1404 "dirty=%hhu writethrough=%hhu\n",
1405 idx,
1406 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1407 tlb->v, tlb->sh, tlb->c, tlb->pr,
1408 tlb->d, tlb->wt);
1411 static void tlb_info(Monitor *mon)
1413 CPUState *env = mon_get_cpu();
1414 int i;
1416 monitor_printf (mon, "ITLB:\n");
1417 for (i = 0 ; i < ITLB_SIZE ; i++)
1418 print_tlb (mon, i, &env->itlb[i]);
1419 monitor_printf (mon, "UTLB:\n");
1420 for (i = 0 ; i < UTLB_SIZE ; i++)
1421 print_tlb (mon, i, &env->utlb[i]);
1424 #endif
1426 static void do_info_kvm(Monitor *mon)
1428 #ifdef CONFIG_KVM
1429 monitor_printf(mon, "kvm support: ");
1430 if (kvm_enabled())
1431 monitor_printf(mon, "enabled\n");
1432 else
1433 monitor_printf(mon, "disabled\n");
1434 #else
1435 monitor_printf(mon, "kvm support: not compiled\n");
1436 #endif
1439 static void do_info_numa(Monitor *mon)
1441 int i;
1442 CPUState *env;
1444 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1445 for (i = 0; i < nb_numa_nodes; i++) {
1446 monitor_printf(mon, "node %d cpus:", i);
1447 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1448 if (env->numa_node == i) {
1449 monitor_printf(mon, " %d", env->cpu_index);
1452 monitor_printf(mon, "\n");
1453 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1454 node_mem[i] >> 20);
1458 #ifdef CONFIG_PROFILER
1460 int64_t qemu_time;
1461 int64_t dev_time;
1463 static void do_info_profile(Monitor *mon)
1465 int64_t total;
1466 total = qemu_time;
1467 if (total == 0)
1468 total = 1;
1469 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1470 dev_time, dev_time / (double)get_ticks_per_sec());
1471 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1472 qemu_time, qemu_time / (double)get_ticks_per_sec());
1473 qemu_time = 0;
1474 dev_time = 0;
1476 #else
1477 static void do_info_profile(Monitor *mon)
1479 monitor_printf(mon, "Internal profiler not compiled\n");
1481 #endif
1483 /* Capture support */
1484 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1486 static void do_info_capture(Monitor *mon)
1488 int i;
1489 CaptureState *s;
1491 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1492 monitor_printf(mon, "[%d]: ", i);
1493 s->ops.info (s->opaque);
1497 #ifdef HAS_AUDIO
1498 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1500 int i;
1501 int n = qdict_get_int(qdict, "n");
1502 CaptureState *s;
1504 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1505 if (i == n) {
1506 s->ops.destroy (s->opaque);
1507 QLIST_REMOVE (s, entries);
1508 qemu_free (s);
1509 return;
1514 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1516 const char *path = qdict_get_str(qdict, "path");
1517 int has_freq = qdict_haskey(qdict, "freq");
1518 int freq = qdict_get_try_int(qdict, "freq", -1);
1519 int has_bits = qdict_haskey(qdict, "bits");
1520 int bits = qdict_get_try_int(qdict, "bits", -1);
1521 int has_channels = qdict_haskey(qdict, "nchannels");
1522 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1523 CaptureState *s;
1525 s = qemu_mallocz (sizeof (*s));
1527 freq = has_freq ? freq : 44100;
1528 bits = has_bits ? bits : 16;
1529 nchannels = has_channels ? nchannels : 2;
1531 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1532 monitor_printf(mon, "Faied to add wave capture\n");
1533 qemu_free (s);
1535 QLIST_INSERT_HEAD (&capture_head, s, entries);
1537 #endif
1539 #if defined(TARGET_I386)
1540 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1542 CPUState *env;
1543 int cpu_index = qdict_get_int(qdict, "cpu_index");
1545 for (env = first_cpu; env != NULL; env = env->next_cpu)
1546 if (env->cpu_index == cpu_index) {
1547 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1548 break;
1551 #endif
1553 static void do_info_status(Monitor *mon)
1555 if (vm_running) {
1556 if (singlestep) {
1557 monitor_printf(mon, "VM status: running (single step mode)\n");
1558 } else {
1559 monitor_printf(mon, "VM status: running\n");
1561 } else
1562 monitor_printf(mon, "VM status: paused\n");
1566 static void do_balloon(Monitor *mon, const QDict *qdict)
1568 int value = qdict_get_int(qdict, "value");
1569 ram_addr_t target = value;
1570 qemu_balloon(target << 20);
1573 static void do_info_balloon(Monitor *mon)
1575 ram_addr_t actual;
1577 actual = qemu_balloon_status();
1578 if (kvm_enabled() && !kvm_has_sync_mmu())
1579 monitor_printf(mon, "Using KVM without synchronous MMU, "
1580 "ballooning disabled\n");
1581 else if (actual == 0)
1582 monitor_printf(mon, "Ballooning not activated in VM\n");
1583 else
1584 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1587 static qemu_acl *find_acl(Monitor *mon, const char *name)
1589 qemu_acl *acl = qemu_acl_find(name);
1591 if (!acl) {
1592 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1594 return acl;
1597 static void do_acl_show(Monitor *mon, const QDict *qdict)
1599 const char *aclname = qdict_get_str(qdict, "aclname");
1600 qemu_acl *acl = find_acl(mon, aclname);
1601 qemu_acl_entry *entry;
1602 int i = 0;
1604 if (acl) {
1605 monitor_printf(mon, "policy: %s\n",
1606 acl->defaultDeny ? "deny" : "allow");
1607 QTAILQ_FOREACH(entry, &acl->entries, next) {
1608 i++;
1609 monitor_printf(mon, "%d: %s %s\n", i,
1610 entry->deny ? "deny" : "allow", entry->match);
1615 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1617 const char *aclname = qdict_get_str(qdict, "aclname");
1618 qemu_acl *acl = find_acl(mon, aclname);
1620 if (acl) {
1621 qemu_acl_reset(acl);
1622 monitor_printf(mon, "acl: removed all rules\n");
1626 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1628 const char *aclname = qdict_get_str(qdict, "aclname");
1629 const char *policy = qdict_get_str(qdict, "policy");
1630 qemu_acl *acl = find_acl(mon, aclname);
1632 if (acl) {
1633 if (strcmp(policy, "allow") == 0) {
1634 acl->defaultDeny = 0;
1635 monitor_printf(mon, "acl: policy set to 'allow'\n");
1636 } else if (strcmp(policy, "deny") == 0) {
1637 acl->defaultDeny = 1;
1638 monitor_printf(mon, "acl: policy set to 'deny'\n");
1639 } else {
1640 monitor_printf(mon, "acl: unknown policy '%s', "
1641 "expected 'deny' or 'allow'\n", policy);
1646 static void do_acl_add(Monitor *mon, const QDict *qdict)
1648 const char *aclname = qdict_get_str(qdict, "aclname");
1649 const char *match = qdict_get_str(qdict, "match");
1650 const char *policy = qdict_get_str(qdict, "policy");
1651 int has_index = qdict_haskey(qdict, "index");
1652 int index = qdict_get_try_int(qdict, "index", -1);
1653 qemu_acl *acl = find_acl(mon, aclname);
1654 int deny, ret;
1656 if (acl) {
1657 if (strcmp(policy, "allow") == 0) {
1658 deny = 0;
1659 } else if (strcmp(policy, "deny") == 0) {
1660 deny = 1;
1661 } else {
1662 monitor_printf(mon, "acl: unknown policy '%s', "
1663 "expected 'deny' or 'allow'\n", policy);
1664 return;
1666 if (has_index)
1667 ret = qemu_acl_insert(acl, deny, match, index);
1668 else
1669 ret = qemu_acl_append(acl, deny, match);
1670 if (ret < 0)
1671 monitor_printf(mon, "acl: unable to add acl entry\n");
1672 else
1673 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1677 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1679 const char *aclname = qdict_get_str(qdict, "aclname");
1680 const char *match = qdict_get_str(qdict, "match");
1681 qemu_acl *acl = find_acl(mon, aclname);
1682 int ret;
1684 if (acl) {
1685 ret = qemu_acl_remove(acl, match);
1686 if (ret < 0)
1687 monitor_printf(mon, "acl: no matching acl entry\n");
1688 else
1689 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1693 #if defined(TARGET_I386)
1694 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1696 CPUState *cenv;
1697 int cpu_index = qdict_get_int(qdict, "cpu_index");
1698 int bank = qdict_get_int(qdict, "bank");
1699 uint64_t status = qdict_get_int(qdict, "status");
1700 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1701 uint64_t addr = qdict_get_int(qdict, "addr");
1702 uint64_t misc = qdict_get_int(qdict, "misc");
1704 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1705 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1706 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1707 break;
1710 #endif
1712 static void do_getfd(Monitor *mon, const QDict *qdict)
1714 const char *fdname = qdict_get_str(qdict, "fdname");
1715 mon_fd_t *monfd;
1716 int fd;
1718 fd = qemu_chr_get_msgfd(mon->chr);
1719 if (fd == -1) {
1720 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1721 return;
1724 if (qemu_isdigit(fdname[0])) {
1725 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1726 return;
1729 fd = dup(fd);
1730 if (fd == -1) {
1731 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1732 strerror(errno));
1733 return;
1736 QLIST_FOREACH(monfd, &mon->fds, next) {
1737 if (strcmp(monfd->name, fdname) != 0) {
1738 continue;
1741 close(monfd->fd);
1742 monfd->fd = fd;
1743 return;
1746 monfd = qemu_mallocz(sizeof(mon_fd_t));
1747 monfd->name = qemu_strdup(fdname);
1748 monfd->fd = fd;
1750 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1753 static void do_closefd(Monitor *mon, const QDict *qdict)
1755 const char *fdname = qdict_get_str(qdict, "fdname");
1756 mon_fd_t *monfd;
1758 QLIST_FOREACH(monfd, &mon->fds, next) {
1759 if (strcmp(monfd->name, fdname) != 0) {
1760 continue;
1763 QLIST_REMOVE(monfd, next);
1764 close(monfd->fd);
1765 qemu_free(monfd->name);
1766 qemu_free(monfd);
1767 return;
1770 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1771 fdname);
1774 static void do_loadvm(Monitor *mon, const QDict *qdict)
1776 int saved_vm_running = vm_running;
1777 const char *name = qdict_get_str(qdict, "name");
1779 vm_stop(0);
1781 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1782 vm_start();
1785 int monitor_get_fd(Monitor *mon, const char *fdname)
1787 mon_fd_t *monfd;
1789 QLIST_FOREACH(monfd, &mon->fds, next) {
1790 int fd;
1792 if (strcmp(monfd->name, fdname) != 0) {
1793 continue;
1796 fd = monfd->fd;
1798 /* caller takes ownership of fd */
1799 QLIST_REMOVE(monfd, next);
1800 qemu_free(monfd->name);
1801 qemu_free(monfd);
1803 return fd;
1806 return -1;
1809 static const mon_cmd_t mon_cmds[] = {
1810 #include "qemu-monitor.h"
1811 { NULL, NULL, },
1814 /* Please update qemu-monitor.hx when adding or changing commands */
1815 static const mon_cmd_t info_cmds[] = {
1816 { "version", "", do_info_version,
1817 "", "show the version of QEMU" },
1818 { "network", "", do_info_network,
1819 "", "show the network state" },
1820 { "chardev", "", qemu_chr_info,
1821 "", "show the character devices" },
1822 { "block", "", bdrv_info,
1823 "", "show the block devices" },
1824 { "blockstats", "", bdrv_info_stats,
1825 "", "show block device statistics" },
1826 { "registers", "", do_info_registers,
1827 "", "show the cpu registers" },
1828 { "cpus", "", do_info_cpus,
1829 "", "show infos for each CPU" },
1830 { "history", "", do_info_history,
1831 "", "show the command line history", },
1832 { "irq", "", irq_info,
1833 "", "show the interrupts statistics (if available)", },
1834 { "pic", "", pic_info,
1835 "", "show i8259 (PIC) state", },
1836 { "pci", "", pci_info,
1837 "", "show PCI info", },
1838 #if defined(TARGET_I386) || defined(TARGET_SH4)
1839 { "tlb", "", tlb_info,
1840 "", "show virtual to physical memory mappings", },
1841 #endif
1842 #if defined(TARGET_I386)
1843 { "mem", "", mem_info,
1844 "", "show the active virtual memory mappings", },
1845 { "hpet", "", do_info_hpet,
1846 "", "show state of HPET", },
1847 #endif
1848 { "jit", "", do_info_jit,
1849 "", "show dynamic compiler info", },
1850 { "kvm", "", do_info_kvm,
1851 "", "show KVM information", },
1852 { "numa", "", do_info_numa,
1853 "", "show NUMA information", },
1854 { "usb", "", usb_info,
1855 "", "show guest USB devices", },
1856 { "usbhost", "", usb_host_info,
1857 "", "show host USB devices", },
1858 { "profile", "", do_info_profile,
1859 "", "show profiling information", },
1860 { "capture", "", do_info_capture,
1861 "", "show capture information" },
1862 { "snapshots", "", do_info_snapshots,
1863 "", "show the currently saved VM snapshots" },
1864 { "status", "", do_info_status,
1865 "", "show the current VM status (running|paused)" },
1866 { "pcmcia", "", pcmcia_info,
1867 "", "show guest PCMCIA status" },
1868 { "mice", "", do_info_mice,
1869 "", "show which guest mouse is receiving events" },
1870 { "vnc", "", do_info_vnc,
1871 "", "show the vnc server status"},
1872 { "name", "", do_info_name,
1873 "", "show the current VM name" },
1874 { "uuid", "", do_info_uuid,
1875 "", "show the current VM UUID" },
1876 #if defined(TARGET_PPC)
1877 { "cpustats", "", do_info_cpu_stats,
1878 "", "show CPU statistics", },
1879 #endif
1880 #if defined(CONFIG_SLIRP)
1881 { "usernet", "", do_info_usernet,
1882 "", "show user network stack connection states", },
1883 #endif
1884 { "migrate", "", do_info_migrate, "", "show migration status" },
1885 { "balloon", "", do_info_balloon,
1886 "", "show balloon information" },
1887 { "qtree", "", do_info_qtree,
1888 "", "show device tree" },
1889 { "qdm", "", do_info_qdm,
1890 "", "show qdev device model list" },
1891 { "roms", "", do_info_roms,
1892 "", "show roms" },
1893 { NULL, NULL, },
1896 /*******************************************************************/
1898 static const char *pch;
1899 static jmp_buf expr_env;
1901 #define MD_TLONG 0
1902 #define MD_I32 1
1904 typedef struct MonitorDef {
1905 const char *name;
1906 int offset;
1907 target_long (*get_value)(const struct MonitorDef *md, int val);
1908 int type;
1909 } MonitorDef;
1911 #if defined(TARGET_I386)
1912 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1914 CPUState *env = mon_get_cpu();
1915 if (!env)
1916 return 0;
1917 return env->eip + env->segs[R_CS].base;
1919 #endif
1921 #if defined(TARGET_PPC)
1922 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1924 CPUState *env = mon_get_cpu();
1925 unsigned int u;
1926 int i;
1928 if (!env)
1929 return 0;
1931 u = 0;
1932 for (i = 0; i < 8; i++)
1933 u |= env->crf[i] << (32 - (4 * i));
1935 return u;
1938 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1940 CPUState *env = mon_get_cpu();
1941 if (!env)
1942 return 0;
1943 return env->msr;
1946 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1948 CPUState *env = mon_get_cpu();
1949 if (!env)
1950 return 0;
1951 return env->xer;
1954 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1956 CPUState *env = mon_get_cpu();
1957 if (!env)
1958 return 0;
1959 return cpu_ppc_load_decr(env);
1962 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1964 CPUState *env = mon_get_cpu();
1965 if (!env)
1966 return 0;
1967 return cpu_ppc_load_tbu(env);
1970 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1972 CPUState *env = mon_get_cpu();
1973 if (!env)
1974 return 0;
1975 return cpu_ppc_load_tbl(env);
1977 #endif
1979 #if defined(TARGET_SPARC)
1980 #ifndef TARGET_SPARC64
1981 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1983 CPUState *env = mon_get_cpu();
1984 if (!env)
1985 return 0;
1986 return GET_PSR(env);
1988 #endif
1990 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1992 CPUState *env = mon_get_cpu();
1993 if (!env)
1994 return 0;
1995 return env->regwptr[val];
1997 #endif
1999 static const MonitorDef monitor_defs[] = {
2000 #ifdef TARGET_I386
2002 #define SEG(name, seg) \
2003 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2004 { name ".base", offsetof(CPUState, segs[seg].base) },\
2005 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2007 { "eax", offsetof(CPUState, regs[0]) },
2008 { "ecx", offsetof(CPUState, regs[1]) },
2009 { "edx", offsetof(CPUState, regs[2]) },
2010 { "ebx", offsetof(CPUState, regs[3]) },
2011 { "esp|sp", offsetof(CPUState, regs[4]) },
2012 { "ebp|fp", offsetof(CPUState, regs[5]) },
2013 { "esi", offsetof(CPUState, regs[6]) },
2014 { "edi", offsetof(CPUState, regs[7]) },
2015 #ifdef TARGET_X86_64
2016 { "r8", offsetof(CPUState, regs[8]) },
2017 { "r9", offsetof(CPUState, regs[9]) },
2018 { "r10", offsetof(CPUState, regs[10]) },
2019 { "r11", offsetof(CPUState, regs[11]) },
2020 { "r12", offsetof(CPUState, regs[12]) },
2021 { "r13", offsetof(CPUState, regs[13]) },
2022 { "r14", offsetof(CPUState, regs[14]) },
2023 { "r15", offsetof(CPUState, regs[15]) },
2024 #endif
2025 { "eflags", offsetof(CPUState, eflags) },
2026 { "eip", offsetof(CPUState, eip) },
2027 SEG("cs", R_CS)
2028 SEG("ds", R_DS)
2029 SEG("es", R_ES)
2030 SEG("ss", R_SS)
2031 SEG("fs", R_FS)
2032 SEG("gs", R_GS)
2033 { "pc", 0, monitor_get_pc, },
2034 #elif defined(TARGET_PPC)
2035 /* General purpose registers */
2036 { "r0", offsetof(CPUState, gpr[0]) },
2037 { "r1", offsetof(CPUState, gpr[1]) },
2038 { "r2", offsetof(CPUState, gpr[2]) },
2039 { "r3", offsetof(CPUState, gpr[3]) },
2040 { "r4", offsetof(CPUState, gpr[4]) },
2041 { "r5", offsetof(CPUState, gpr[5]) },
2042 { "r6", offsetof(CPUState, gpr[6]) },
2043 { "r7", offsetof(CPUState, gpr[7]) },
2044 { "r8", offsetof(CPUState, gpr[8]) },
2045 { "r9", offsetof(CPUState, gpr[9]) },
2046 { "r10", offsetof(CPUState, gpr[10]) },
2047 { "r11", offsetof(CPUState, gpr[11]) },
2048 { "r12", offsetof(CPUState, gpr[12]) },
2049 { "r13", offsetof(CPUState, gpr[13]) },
2050 { "r14", offsetof(CPUState, gpr[14]) },
2051 { "r15", offsetof(CPUState, gpr[15]) },
2052 { "r16", offsetof(CPUState, gpr[16]) },
2053 { "r17", offsetof(CPUState, gpr[17]) },
2054 { "r18", offsetof(CPUState, gpr[18]) },
2055 { "r19", offsetof(CPUState, gpr[19]) },
2056 { "r20", offsetof(CPUState, gpr[20]) },
2057 { "r21", offsetof(CPUState, gpr[21]) },
2058 { "r22", offsetof(CPUState, gpr[22]) },
2059 { "r23", offsetof(CPUState, gpr[23]) },
2060 { "r24", offsetof(CPUState, gpr[24]) },
2061 { "r25", offsetof(CPUState, gpr[25]) },
2062 { "r26", offsetof(CPUState, gpr[26]) },
2063 { "r27", offsetof(CPUState, gpr[27]) },
2064 { "r28", offsetof(CPUState, gpr[28]) },
2065 { "r29", offsetof(CPUState, gpr[29]) },
2066 { "r30", offsetof(CPUState, gpr[30]) },
2067 { "r31", offsetof(CPUState, gpr[31]) },
2068 /* Floating point registers */
2069 { "f0", offsetof(CPUState, fpr[0]) },
2070 { "f1", offsetof(CPUState, fpr[1]) },
2071 { "f2", offsetof(CPUState, fpr[2]) },
2072 { "f3", offsetof(CPUState, fpr[3]) },
2073 { "f4", offsetof(CPUState, fpr[4]) },
2074 { "f5", offsetof(CPUState, fpr[5]) },
2075 { "f6", offsetof(CPUState, fpr[6]) },
2076 { "f7", offsetof(CPUState, fpr[7]) },
2077 { "f8", offsetof(CPUState, fpr[8]) },
2078 { "f9", offsetof(CPUState, fpr[9]) },
2079 { "f10", offsetof(CPUState, fpr[10]) },
2080 { "f11", offsetof(CPUState, fpr[11]) },
2081 { "f12", offsetof(CPUState, fpr[12]) },
2082 { "f13", offsetof(CPUState, fpr[13]) },
2083 { "f14", offsetof(CPUState, fpr[14]) },
2084 { "f15", offsetof(CPUState, fpr[15]) },
2085 { "f16", offsetof(CPUState, fpr[16]) },
2086 { "f17", offsetof(CPUState, fpr[17]) },
2087 { "f18", offsetof(CPUState, fpr[18]) },
2088 { "f19", offsetof(CPUState, fpr[19]) },
2089 { "f20", offsetof(CPUState, fpr[20]) },
2090 { "f21", offsetof(CPUState, fpr[21]) },
2091 { "f22", offsetof(CPUState, fpr[22]) },
2092 { "f23", offsetof(CPUState, fpr[23]) },
2093 { "f24", offsetof(CPUState, fpr[24]) },
2094 { "f25", offsetof(CPUState, fpr[25]) },
2095 { "f26", offsetof(CPUState, fpr[26]) },
2096 { "f27", offsetof(CPUState, fpr[27]) },
2097 { "f28", offsetof(CPUState, fpr[28]) },
2098 { "f29", offsetof(CPUState, fpr[29]) },
2099 { "f30", offsetof(CPUState, fpr[30]) },
2100 { "f31", offsetof(CPUState, fpr[31]) },
2101 { "fpscr", offsetof(CPUState, fpscr) },
2102 /* Next instruction pointer */
2103 { "nip|pc", offsetof(CPUState, nip) },
2104 { "lr", offsetof(CPUState, lr) },
2105 { "ctr", offsetof(CPUState, ctr) },
2106 { "decr", 0, &monitor_get_decr, },
2107 { "ccr", 0, &monitor_get_ccr, },
2108 /* Machine state register */
2109 { "msr", 0, &monitor_get_msr, },
2110 { "xer", 0, &monitor_get_xer, },
2111 { "tbu", 0, &monitor_get_tbu, },
2112 { "tbl", 0, &monitor_get_tbl, },
2113 #if defined(TARGET_PPC64)
2114 /* Address space register */
2115 { "asr", offsetof(CPUState, asr) },
2116 #endif
2117 /* Segment registers */
2118 { "sdr1", offsetof(CPUState, sdr1) },
2119 { "sr0", offsetof(CPUState, sr[0]) },
2120 { "sr1", offsetof(CPUState, sr[1]) },
2121 { "sr2", offsetof(CPUState, sr[2]) },
2122 { "sr3", offsetof(CPUState, sr[3]) },
2123 { "sr4", offsetof(CPUState, sr[4]) },
2124 { "sr5", offsetof(CPUState, sr[5]) },
2125 { "sr6", offsetof(CPUState, sr[6]) },
2126 { "sr7", offsetof(CPUState, sr[7]) },
2127 { "sr8", offsetof(CPUState, sr[8]) },
2128 { "sr9", offsetof(CPUState, sr[9]) },
2129 { "sr10", offsetof(CPUState, sr[10]) },
2130 { "sr11", offsetof(CPUState, sr[11]) },
2131 { "sr12", offsetof(CPUState, sr[12]) },
2132 { "sr13", offsetof(CPUState, sr[13]) },
2133 { "sr14", offsetof(CPUState, sr[14]) },
2134 { "sr15", offsetof(CPUState, sr[15]) },
2135 /* Too lazy to put BATs and SPRs ... */
2136 #elif defined(TARGET_SPARC)
2137 { "g0", offsetof(CPUState, gregs[0]) },
2138 { "g1", offsetof(CPUState, gregs[1]) },
2139 { "g2", offsetof(CPUState, gregs[2]) },
2140 { "g3", offsetof(CPUState, gregs[3]) },
2141 { "g4", offsetof(CPUState, gregs[4]) },
2142 { "g5", offsetof(CPUState, gregs[5]) },
2143 { "g6", offsetof(CPUState, gregs[6]) },
2144 { "g7", offsetof(CPUState, gregs[7]) },
2145 { "o0", 0, monitor_get_reg },
2146 { "o1", 1, monitor_get_reg },
2147 { "o2", 2, monitor_get_reg },
2148 { "o3", 3, monitor_get_reg },
2149 { "o4", 4, monitor_get_reg },
2150 { "o5", 5, monitor_get_reg },
2151 { "o6", 6, monitor_get_reg },
2152 { "o7", 7, monitor_get_reg },
2153 { "l0", 8, monitor_get_reg },
2154 { "l1", 9, monitor_get_reg },
2155 { "l2", 10, monitor_get_reg },
2156 { "l3", 11, monitor_get_reg },
2157 { "l4", 12, monitor_get_reg },
2158 { "l5", 13, monitor_get_reg },
2159 { "l6", 14, monitor_get_reg },
2160 { "l7", 15, monitor_get_reg },
2161 { "i0", 16, monitor_get_reg },
2162 { "i1", 17, monitor_get_reg },
2163 { "i2", 18, monitor_get_reg },
2164 { "i3", 19, monitor_get_reg },
2165 { "i4", 20, monitor_get_reg },
2166 { "i5", 21, monitor_get_reg },
2167 { "i6", 22, monitor_get_reg },
2168 { "i7", 23, monitor_get_reg },
2169 { "pc", offsetof(CPUState, pc) },
2170 { "npc", offsetof(CPUState, npc) },
2171 { "y", offsetof(CPUState, y) },
2172 #ifndef TARGET_SPARC64
2173 { "psr", 0, &monitor_get_psr, },
2174 { "wim", offsetof(CPUState, wim) },
2175 #endif
2176 { "tbr", offsetof(CPUState, tbr) },
2177 { "fsr", offsetof(CPUState, fsr) },
2178 { "f0", offsetof(CPUState, fpr[0]) },
2179 { "f1", offsetof(CPUState, fpr[1]) },
2180 { "f2", offsetof(CPUState, fpr[2]) },
2181 { "f3", offsetof(CPUState, fpr[3]) },
2182 { "f4", offsetof(CPUState, fpr[4]) },
2183 { "f5", offsetof(CPUState, fpr[5]) },
2184 { "f6", offsetof(CPUState, fpr[6]) },
2185 { "f7", offsetof(CPUState, fpr[7]) },
2186 { "f8", offsetof(CPUState, fpr[8]) },
2187 { "f9", offsetof(CPUState, fpr[9]) },
2188 { "f10", offsetof(CPUState, fpr[10]) },
2189 { "f11", offsetof(CPUState, fpr[11]) },
2190 { "f12", offsetof(CPUState, fpr[12]) },
2191 { "f13", offsetof(CPUState, fpr[13]) },
2192 { "f14", offsetof(CPUState, fpr[14]) },
2193 { "f15", offsetof(CPUState, fpr[15]) },
2194 { "f16", offsetof(CPUState, fpr[16]) },
2195 { "f17", offsetof(CPUState, fpr[17]) },
2196 { "f18", offsetof(CPUState, fpr[18]) },
2197 { "f19", offsetof(CPUState, fpr[19]) },
2198 { "f20", offsetof(CPUState, fpr[20]) },
2199 { "f21", offsetof(CPUState, fpr[21]) },
2200 { "f22", offsetof(CPUState, fpr[22]) },
2201 { "f23", offsetof(CPUState, fpr[23]) },
2202 { "f24", offsetof(CPUState, fpr[24]) },
2203 { "f25", offsetof(CPUState, fpr[25]) },
2204 { "f26", offsetof(CPUState, fpr[26]) },
2205 { "f27", offsetof(CPUState, fpr[27]) },
2206 { "f28", offsetof(CPUState, fpr[28]) },
2207 { "f29", offsetof(CPUState, fpr[29]) },
2208 { "f30", offsetof(CPUState, fpr[30]) },
2209 { "f31", offsetof(CPUState, fpr[31]) },
2210 #ifdef TARGET_SPARC64
2211 { "f32", offsetof(CPUState, fpr[32]) },
2212 { "f34", offsetof(CPUState, fpr[34]) },
2213 { "f36", offsetof(CPUState, fpr[36]) },
2214 { "f38", offsetof(CPUState, fpr[38]) },
2215 { "f40", offsetof(CPUState, fpr[40]) },
2216 { "f42", offsetof(CPUState, fpr[42]) },
2217 { "f44", offsetof(CPUState, fpr[44]) },
2218 { "f46", offsetof(CPUState, fpr[46]) },
2219 { "f48", offsetof(CPUState, fpr[48]) },
2220 { "f50", offsetof(CPUState, fpr[50]) },
2221 { "f52", offsetof(CPUState, fpr[52]) },
2222 { "f54", offsetof(CPUState, fpr[54]) },
2223 { "f56", offsetof(CPUState, fpr[56]) },
2224 { "f58", offsetof(CPUState, fpr[58]) },
2225 { "f60", offsetof(CPUState, fpr[60]) },
2226 { "f62", offsetof(CPUState, fpr[62]) },
2227 { "asi", offsetof(CPUState, asi) },
2228 { "pstate", offsetof(CPUState, pstate) },
2229 { "cansave", offsetof(CPUState, cansave) },
2230 { "canrestore", offsetof(CPUState, canrestore) },
2231 { "otherwin", offsetof(CPUState, otherwin) },
2232 { "wstate", offsetof(CPUState, wstate) },
2233 { "cleanwin", offsetof(CPUState, cleanwin) },
2234 { "fprs", offsetof(CPUState, fprs) },
2235 #endif
2236 #endif
2237 { NULL },
2240 static void expr_error(Monitor *mon, const char *msg)
2242 monitor_printf(mon, "%s\n", msg);
2243 longjmp(expr_env, 1);
2246 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2247 static int get_monitor_def(target_long *pval, const char *name)
2249 const MonitorDef *md;
2250 void *ptr;
2252 for(md = monitor_defs; md->name != NULL; md++) {
2253 if (compare_cmd(name, md->name)) {
2254 if (md->get_value) {
2255 *pval = md->get_value(md, md->offset);
2256 } else {
2257 CPUState *env = mon_get_cpu();
2258 if (!env)
2259 return -2;
2260 ptr = (uint8_t *)env + md->offset;
2261 switch(md->type) {
2262 case MD_I32:
2263 *pval = *(int32_t *)ptr;
2264 break;
2265 case MD_TLONG:
2266 *pval = *(target_long *)ptr;
2267 break;
2268 default:
2269 *pval = 0;
2270 break;
2273 return 0;
2276 return -1;
2279 static void next(void)
2281 if (*pch != '\0') {
2282 pch++;
2283 while (qemu_isspace(*pch))
2284 pch++;
2288 static int64_t expr_sum(Monitor *mon);
2290 static int64_t expr_unary(Monitor *mon)
2292 int64_t n;
2293 char *p;
2294 int ret;
2296 switch(*pch) {
2297 case '+':
2298 next();
2299 n = expr_unary(mon);
2300 break;
2301 case '-':
2302 next();
2303 n = -expr_unary(mon);
2304 break;
2305 case '~':
2306 next();
2307 n = ~expr_unary(mon);
2308 break;
2309 case '(':
2310 next();
2311 n = expr_sum(mon);
2312 if (*pch != ')') {
2313 expr_error(mon, "')' expected");
2315 next();
2316 break;
2317 case '\'':
2318 pch++;
2319 if (*pch == '\0')
2320 expr_error(mon, "character constant expected");
2321 n = *pch;
2322 pch++;
2323 if (*pch != '\'')
2324 expr_error(mon, "missing terminating \' character");
2325 next();
2326 break;
2327 case '$':
2329 char buf[128], *q;
2330 target_long reg=0;
2332 pch++;
2333 q = buf;
2334 while ((*pch >= 'a' && *pch <= 'z') ||
2335 (*pch >= 'A' && *pch <= 'Z') ||
2336 (*pch >= '0' && *pch <= '9') ||
2337 *pch == '_' || *pch == '.') {
2338 if ((q - buf) < sizeof(buf) - 1)
2339 *q++ = *pch;
2340 pch++;
2342 while (qemu_isspace(*pch))
2343 pch++;
2344 *q = 0;
2345 ret = get_monitor_def(&reg, buf);
2346 if (ret == -1)
2347 expr_error(mon, "unknown register");
2348 else if (ret == -2)
2349 expr_error(mon, "no cpu defined");
2350 n = reg;
2352 break;
2353 case '\0':
2354 expr_error(mon, "unexpected end of expression");
2355 n = 0;
2356 break;
2357 default:
2358 #if TARGET_PHYS_ADDR_BITS > 32
2359 n = strtoull(pch, &p, 0);
2360 #else
2361 n = strtoul(pch, &p, 0);
2362 #endif
2363 if (pch == p) {
2364 expr_error(mon, "invalid char in expression");
2366 pch = p;
2367 while (qemu_isspace(*pch))
2368 pch++;
2369 break;
2371 return n;
2375 static int64_t expr_prod(Monitor *mon)
2377 int64_t val, val2;
2378 int op;
2380 val = expr_unary(mon);
2381 for(;;) {
2382 op = *pch;
2383 if (op != '*' && op != '/' && op != '%')
2384 break;
2385 next();
2386 val2 = expr_unary(mon);
2387 switch(op) {
2388 default:
2389 case '*':
2390 val *= val2;
2391 break;
2392 case '/':
2393 case '%':
2394 if (val2 == 0)
2395 expr_error(mon, "division by zero");
2396 if (op == '/')
2397 val /= val2;
2398 else
2399 val %= val2;
2400 break;
2403 return val;
2406 static int64_t expr_logic(Monitor *mon)
2408 int64_t val, val2;
2409 int op;
2411 val = expr_prod(mon);
2412 for(;;) {
2413 op = *pch;
2414 if (op != '&' && op != '|' && op != '^')
2415 break;
2416 next();
2417 val2 = expr_prod(mon);
2418 switch(op) {
2419 default:
2420 case '&':
2421 val &= val2;
2422 break;
2423 case '|':
2424 val |= val2;
2425 break;
2426 case '^':
2427 val ^= val2;
2428 break;
2431 return val;
2434 static int64_t expr_sum(Monitor *mon)
2436 int64_t val, val2;
2437 int op;
2439 val = expr_logic(mon);
2440 for(;;) {
2441 op = *pch;
2442 if (op != '+' && op != '-')
2443 break;
2444 next();
2445 val2 = expr_logic(mon);
2446 if (op == '+')
2447 val += val2;
2448 else
2449 val -= val2;
2451 return val;
2454 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2456 pch = *pp;
2457 if (setjmp(expr_env)) {
2458 *pp = pch;
2459 return -1;
2461 while (qemu_isspace(*pch))
2462 pch++;
2463 *pval = expr_sum(mon);
2464 *pp = pch;
2465 return 0;
2468 static int get_str(char *buf, int buf_size, const char **pp)
2470 const char *p;
2471 char *q;
2472 int c;
2474 q = buf;
2475 p = *pp;
2476 while (qemu_isspace(*p))
2477 p++;
2478 if (*p == '\0') {
2479 fail:
2480 *q = '\0';
2481 *pp = p;
2482 return -1;
2484 if (*p == '\"') {
2485 p++;
2486 while (*p != '\0' && *p != '\"') {
2487 if (*p == '\\') {
2488 p++;
2489 c = *p++;
2490 switch(c) {
2491 case 'n':
2492 c = '\n';
2493 break;
2494 case 'r':
2495 c = '\r';
2496 break;
2497 case '\\':
2498 case '\'':
2499 case '\"':
2500 break;
2501 default:
2502 qemu_printf("unsupported escape code: '\\%c'\n", c);
2503 goto fail;
2505 if ((q - buf) < buf_size - 1) {
2506 *q++ = c;
2508 } else {
2509 if ((q - buf) < buf_size - 1) {
2510 *q++ = *p;
2512 p++;
2515 if (*p != '\"') {
2516 qemu_printf("unterminated string\n");
2517 goto fail;
2519 p++;
2520 } else {
2521 while (*p != '\0' && !qemu_isspace(*p)) {
2522 if ((q - buf) < buf_size - 1) {
2523 *q++ = *p;
2525 p++;
2528 *q = '\0';
2529 *pp = p;
2530 return 0;
2534 * Store the command-name in cmdname, and return a pointer to
2535 * the remaining of the command string.
2537 static const char *get_command_name(const char *cmdline,
2538 char *cmdname, size_t nlen)
2540 size_t len;
2541 const char *p, *pstart;
2543 p = cmdline;
2544 while (qemu_isspace(*p))
2545 p++;
2546 if (*p == '\0')
2547 return NULL;
2548 pstart = p;
2549 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2550 p++;
2551 len = p - pstart;
2552 if (len > nlen - 1)
2553 len = nlen - 1;
2554 memcpy(cmdname, pstart, len);
2555 cmdname[len] = '\0';
2556 return p;
2560 * Read key of 'type' into 'key' and return the current
2561 * 'type' pointer.
2563 static char *key_get_info(const char *type, char **key)
2565 size_t len;
2566 char *p, *str;
2568 if (*type == ',')
2569 type++;
2571 p = strchr(type, ':');
2572 if (!p) {
2573 *key = NULL;
2574 return NULL;
2576 len = p - type;
2578 str = qemu_malloc(len + 1);
2579 memcpy(str, type, len);
2580 str[len] = '\0';
2582 *key = str;
2583 return ++p;
2586 static int default_fmt_format = 'x';
2587 static int default_fmt_size = 4;
2589 #define MAX_ARGS 16
2591 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2592 const char *cmdline,
2593 QDict *qdict)
2595 const char *p, *typestr;
2596 int c;
2597 const mon_cmd_t *cmd;
2598 char cmdname[256];
2599 char buf[1024];
2600 char *key;
2602 #ifdef DEBUG
2603 monitor_printf(mon, "command='%s'\n", cmdline);
2604 #endif
2606 /* extract the command name */
2607 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2608 if (!p)
2609 return NULL;
2611 /* find the command */
2612 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2613 if (compare_cmd(cmdname, cmd->name))
2614 break;
2617 if (cmd->name == NULL) {
2618 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2619 return NULL;
2622 /* parse the parameters */
2623 typestr = cmd->args_type;
2624 for(;;) {
2625 typestr = key_get_info(typestr, &key);
2626 if (!typestr)
2627 break;
2628 c = *typestr;
2629 typestr++;
2630 switch(c) {
2631 case 'F':
2632 case 'B':
2633 case 's':
2635 int ret;
2637 while (qemu_isspace(*p))
2638 p++;
2639 if (*typestr == '?') {
2640 typestr++;
2641 if (*p == '\0') {
2642 /* no optional string: NULL argument */
2643 break;
2646 ret = get_str(buf, sizeof(buf), &p);
2647 if (ret < 0) {
2648 switch(c) {
2649 case 'F':
2650 monitor_printf(mon, "%s: filename expected\n",
2651 cmdname);
2652 break;
2653 case 'B':
2654 monitor_printf(mon, "%s: block device name expected\n",
2655 cmdname);
2656 break;
2657 default:
2658 monitor_printf(mon, "%s: string expected\n", cmdname);
2659 break;
2661 goto fail;
2663 qdict_put(qdict, key, qstring_from_str(buf));
2665 break;
2666 case '/':
2668 int count, format, size;
2670 while (qemu_isspace(*p))
2671 p++;
2672 if (*p == '/') {
2673 /* format found */
2674 p++;
2675 count = 1;
2676 if (qemu_isdigit(*p)) {
2677 count = 0;
2678 while (qemu_isdigit(*p)) {
2679 count = count * 10 + (*p - '0');
2680 p++;
2683 size = -1;
2684 format = -1;
2685 for(;;) {
2686 switch(*p) {
2687 case 'o':
2688 case 'd':
2689 case 'u':
2690 case 'x':
2691 case 'i':
2692 case 'c':
2693 format = *p++;
2694 break;
2695 case 'b':
2696 size = 1;
2697 p++;
2698 break;
2699 case 'h':
2700 size = 2;
2701 p++;
2702 break;
2703 case 'w':
2704 size = 4;
2705 p++;
2706 break;
2707 case 'g':
2708 case 'L':
2709 size = 8;
2710 p++;
2711 break;
2712 default:
2713 goto next;
2716 next:
2717 if (*p != '\0' && !qemu_isspace(*p)) {
2718 monitor_printf(mon, "invalid char in format: '%c'\n",
2719 *p);
2720 goto fail;
2722 if (format < 0)
2723 format = default_fmt_format;
2724 if (format != 'i') {
2725 /* for 'i', not specifying a size gives -1 as size */
2726 if (size < 0)
2727 size = default_fmt_size;
2728 default_fmt_size = size;
2730 default_fmt_format = format;
2731 } else {
2732 count = 1;
2733 format = default_fmt_format;
2734 if (format != 'i') {
2735 size = default_fmt_size;
2736 } else {
2737 size = -1;
2740 qdict_put(qdict, "count", qint_from_int(count));
2741 qdict_put(qdict, "format", qint_from_int(format));
2742 qdict_put(qdict, "size", qint_from_int(size));
2744 break;
2745 case 'i':
2746 case 'l':
2748 int64_t val;
2750 while (qemu_isspace(*p))
2751 p++;
2752 if (*typestr == '?' || *typestr == '.') {
2753 if (*typestr == '?') {
2754 if (*p == '\0') {
2755 typestr++;
2756 break;
2758 } else {
2759 if (*p == '.') {
2760 p++;
2761 while (qemu_isspace(*p))
2762 p++;
2763 } else {
2764 typestr++;
2765 break;
2768 typestr++;
2770 if (get_expr(mon, &val, &p))
2771 goto fail;
2772 /* Check if 'i' is greater than 32-bit */
2773 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2774 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2775 monitor_printf(mon, "integer is for 32-bit values\n");
2776 goto fail;
2778 qdict_put(qdict, key, qint_from_int(val));
2780 break;
2781 case '-':
2783 int has_option;
2784 /* option */
2786 c = *typestr++;
2787 if (c == '\0')
2788 goto bad_type;
2789 while (qemu_isspace(*p))
2790 p++;
2791 has_option = 0;
2792 if (*p == '-') {
2793 p++;
2794 if (*p != c) {
2795 monitor_printf(mon, "%s: unsupported option -%c\n",
2796 cmdname, *p);
2797 goto fail;
2799 p++;
2800 has_option = 1;
2802 qdict_put(qdict, key, qint_from_int(has_option));
2804 break;
2805 default:
2806 bad_type:
2807 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2808 goto fail;
2810 qemu_free(key);
2811 key = NULL;
2813 /* check that all arguments were parsed */
2814 while (qemu_isspace(*p))
2815 p++;
2816 if (*p != '\0') {
2817 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2818 cmdname);
2819 goto fail;
2822 return cmd;
2824 fail:
2825 qemu_free(key);
2826 return NULL;
2829 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2831 QDict *qdict;
2832 const mon_cmd_t *cmd;
2834 qdict = qdict_new();
2836 cmd = monitor_parse_command(mon, cmdline, qdict);
2837 if (cmd) {
2838 void (*handler)(Monitor *mon, const QDict *qdict);
2840 qemu_errors_to_mon(mon);
2842 handler = cmd->handler;
2843 handler(mon, qdict);
2845 qemu_errors_to_previous();
2848 QDECREF(qdict);
2851 static void cmd_completion(const char *name, const char *list)
2853 const char *p, *pstart;
2854 char cmd[128];
2855 int len;
2857 p = list;
2858 for(;;) {
2859 pstart = p;
2860 p = strchr(p, '|');
2861 if (!p)
2862 p = pstart + strlen(pstart);
2863 len = p - pstart;
2864 if (len > sizeof(cmd) - 2)
2865 len = sizeof(cmd) - 2;
2866 memcpy(cmd, pstart, len);
2867 cmd[len] = '\0';
2868 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2869 readline_add_completion(cur_mon->rs, cmd);
2871 if (*p == '\0')
2872 break;
2873 p++;
2877 static void file_completion(const char *input)
2879 DIR *ffs;
2880 struct dirent *d;
2881 char path[1024];
2882 char file[1024], file_prefix[1024];
2883 int input_path_len;
2884 const char *p;
2886 p = strrchr(input, '/');
2887 if (!p) {
2888 input_path_len = 0;
2889 pstrcpy(file_prefix, sizeof(file_prefix), input);
2890 pstrcpy(path, sizeof(path), ".");
2891 } else {
2892 input_path_len = p - input + 1;
2893 memcpy(path, input, input_path_len);
2894 if (input_path_len > sizeof(path) - 1)
2895 input_path_len = sizeof(path) - 1;
2896 path[input_path_len] = '\0';
2897 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2899 #ifdef DEBUG_COMPLETION
2900 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2901 input, path, file_prefix);
2902 #endif
2903 ffs = opendir(path);
2904 if (!ffs)
2905 return;
2906 for(;;) {
2907 struct stat sb;
2908 d = readdir(ffs);
2909 if (!d)
2910 break;
2911 if (strstart(d->d_name, file_prefix, NULL)) {
2912 memcpy(file, input, input_path_len);
2913 if (input_path_len < sizeof(file))
2914 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2915 d->d_name);
2916 /* stat the file to find out if it's a directory.
2917 * In that case add a slash to speed up typing long paths
2919 stat(file, &sb);
2920 if(S_ISDIR(sb.st_mode))
2921 pstrcat(file, sizeof(file), "/");
2922 readline_add_completion(cur_mon->rs, file);
2925 closedir(ffs);
2928 static void block_completion_it(void *opaque, BlockDriverState *bs)
2930 const char *name = bdrv_get_device_name(bs);
2931 const char *input = opaque;
2933 if (input[0] == '\0' ||
2934 !strncmp(name, (char *)input, strlen(input))) {
2935 readline_add_completion(cur_mon->rs, name);
2939 /* NOTE: this parser is an approximate form of the real command parser */
2940 static void parse_cmdline(const char *cmdline,
2941 int *pnb_args, char **args)
2943 const char *p;
2944 int nb_args, ret;
2945 char buf[1024];
2947 p = cmdline;
2948 nb_args = 0;
2949 for(;;) {
2950 while (qemu_isspace(*p))
2951 p++;
2952 if (*p == '\0')
2953 break;
2954 if (nb_args >= MAX_ARGS)
2955 break;
2956 ret = get_str(buf, sizeof(buf), &p);
2957 args[nb_args] = qemu_strdup(buf);
2958 nb_args++;
2959 if (ret < 0)
2960 break;
2962 *pnb_args = nb_args;
2965 static const char *next_arg_type(const char *typestr)
2967 const char *p = strchr(typestr, ':');
2968 return (p != NULL ? ++p : typestr);
2971 static void monitor_find_completion(const char *cmdline)
2973 const char *cmdname;
2974 char *args[MAX_ARGS];
2975 int nb_args, i, len;
2976 const char *ptype, *str;
2977 const mon_cmd_t *cmd;
2978 const KeyDef *key;
2980 parse_cmdline(cmdline, &nb_args, args);
2981 #ifdef DEBUG_COMPLETION
2982 for(i = 0; i < nb_args; i++) {
2983 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2985 #endif
2987 /* if the line ends with a space, it means we want to complete the
2988 next arg */
2989 len = strlen(cmdline);
2990 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2991 if (nb_args >= MAX_ARGS)
2992 return;
2993 args[nb_args++] = qemu_strdup("");
2995 if (nb_args <= 1) {
2996 /* command completion */
2997 if (nb_args == 0)
2998 cmdname = "";
2999 else
3000 cmdname = args[0];
3001 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3002 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3003 cmd_completion(cmdname, cmd->name);
3005 } else {
3006 /* find the command */
3007 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3008 if (compare_cmd(args[0], cmd->name))
3009 goto found;
3011 return;
3012 found:
3013 ptype = next_arg_type(cmd->args_type);
3014 for(i = 0; i < nb_args - 2; i++) {
3015 if (*ptype != '\0') {
3016 ptype = next_arg_type(ptype);
3017 while (*ptype == '?')
3018 ptype = next_arg_type(ptype);
3021 str = args[nb_args - 1];
3022 if (*ptype == '-' && ptype[1] != '\0') {
3023 ptype += 2;
3025 switch(*ptype) {
3026 case 'F':
3027 /* file completion */
3028 readline_set_completion_index(cur_mon->rs, strlen(str));
3029 file_completion(str);
3030 break;
3031 case 'B':
3032 /* block device name completion */
3033 readline_set_completion_index(cur_mon->rs, strlen(str));
3034 bdrv_iterate(block_completion_it, (void *)str);
3035 break;
3036 case 's':
3037 /* XXX: more generic ? */
3038 if (!strcmp(cmd->name, "info")) {
3039 readline_set_completion_index(cur_mon->rs, strlen(str));
3040 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3041 cmd_completion(str, cmd->name);
3043 } else if (!strcmp(cmd->name, "sendkey")) {
3044 char *sep = strrchr(str, '-');
3045 if (sep)
3046 str = sep + 1;
3047 readline_set_completion_index(cur_mon->rs, strlen(str));
3048 for(key = key_defs; key->name != NULL; key++) {
3049 cmd_completion(str, key->name);
3051 } else if (!strcmp(cmd->name, "help|?")) {
3052 readline_set_completion_index(cur_mon->rs, strlen(str));
3053 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3054 cmd_completion(str, cmd->name);
3057 break;
3058 default:
3059 break;
3062 for(i = 0; i < nb_args; i++)
3063 qemu_free(args[i]);
3066 static int monitor_can_read(void *opaque)
3068 Monitor *mon = opaque;
3070 return (mon->suspend_cnt == 0) ? 128 : 0;
3073 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3075 Monitor *old_mon = cur_mon;
3076 int i;
3078 cur_mon = opaque;
3080 if (cur_mon->rs) {
3081 for (i = 0; i < size; i++)
3082 readline_handle_byte(cur_mon->rs, buf[i]);
3083 } else {
3084 if (size == 0 || buf[size - 1] != 0)
3085 monitor_printf(cur_mon, "corrupted command\n");
3086 else
3087 monitor_handle_command(cur_mon, (char *)buf);
3090 cur_mon = old_mon;
3093 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3095 monitor_suspend(mon);
3096 monitor_handle_command(mon, cmdline);
3097 monitor_resume(mon);
3100 int monitor_suspend(Monitor *mon)
3102 if (!mon->rs)
3103 return -ENOTTY;
3104 mon->suspend_cnt++;
3105 return 0;
3108 void monitor_resume(Monitor *mon)
3110 if (!mon->rs)
3111 return;
3112 if (--mon->suspend_cnt == 0)
3113 readline_show_prompt(mon->rs);
3116 static void monitor_event(void *opaque, int event)
3118 Monitor *mon = opaque;
3120 switch (event) {
3121 case CHR_EVENT_MUX_IN:
3122 mon->mux_out = 0;
3123 if (mon->reset_seen) {
3124 readline_restart(mon->rs);
3125 monitor_resume(mon);
3126 monitor_flush(mon);
3127 } else {
3128 mon->suspend_cnt = 0;
3130 break;
3132 case CHR_EVENT_MUX_OUT:
3133 if (mon->reset_seen) {
3134 if (mon->suspend_cnt == 0) {
3135 monitor_printf(mon, "\n");
3137 monitor_flush(mon);
3138 monitor_suspend(mon);
3139 } else {
3140 mon->suspend_cnt++;
3142 mon->mux_out = 1;
3143 break;
3145 case CHR_EVENT_RESET:
3146 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3147 "information\n", QEMU_VERSION);
3148 if (!mon->mux_out) {
3149 readline_show_prompt(mon->rs);
3151 mon->reset_seen = 1;
3152 break;
3158 * Local variables:
3159 * c-indent-level: 4
3160 * c-basic-offset: 4
3161 * tab-width: 8
3162 * End:
3165 void monitor_init(CharDriverState *chr, int flags)
3167 static int is_first_init = 1;
3168 Monitor *mon;
3170 if (is_first_init) {
3171 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3172 is_first_init = 0;
3175 mon = qemu_mallocz(sizeof(*mon));
3177 mon->chr = chr;
3178 mon->flags = flags;
3179 if (flags & MONITOR_USE_READLINE) {
3180 mon->rs = readline_init(mon, monitor_find_completion);
3181 monitor_read_command(mon, 0);
3184 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3185 mon);
3187 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3188 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3189 cur_mon = mon;
3192 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3194 BlockDriverState *bs = opaque;
3195 int ret = 0;
3197 if (bdrv_set_key(bs, password) != 0) {
3198 monitor_printf(mon, "invalid password\n");
3199 ret = -EPERM;
3201 if (mon->password_completion_cb)
3202 mon->password_completion_cb(mon->password_opaque, ret);
3204 monitor_read_command(mon, 1);
3207 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3208 BlockDriverCompletionFunc *completion_cb,
3209 void *opaque)
3211 int err;
3213 if (!bdrv_key_required(bs)) {
3214 if (completion_cb)
3215 completion_cb(opaque, 0);
3216 return;
3219 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3220 bdrv_get_encrypted_filename(bs));
3222 mon->password_completion_cb = completion_cb;
3223 mon->password_opaque = opaque;
3225 err = monitor_read_password(mon, bdrv_password_cb, bs);
3227 if (err && completion_cb)
3228 completion_cb(opaque, err);
3231 typedef struct QemuErrorSink QemuErrorSink;
3232 struct QemuErrorSink {
3233 enum {
3234 ERR_SINK_FILE,
3235 ERR_SINK_MONITOR,
3236 } dest;
3237 union {
3238 FILE *fp;
3239 Monitor *mon;
3241 QemuErrorSink *previous;
3244 static QemuErrorSink *qemu_error_sink;
3246 void qemu_errors_to_file(FILE *fp)
3248 QemuErrorSink *sink;
3250 sink = qemu_mallocz(sizeof(*sink));
3251 sink->dest = ERR_SINK_FILE;
3252 sink->fp = fp;
3253 sink->previous = qemu_error_sink;
3254 qemu_error_sink = sink;
3257 void qemu_errors_to_mon(Monitor *mon)
3259 QemuErrorSink *sink;
3261 sink = qemu_mallocz(sizeof(*sink));
3262 sink->dest = ERR_SINK_MONITOR;
3263 sink->mon = mon;
3264 sink->previous = qemu_error_sink;
3265 qemu_error_sink = sink;
3268 void qemu_errors_to_previous(void)
3270 QemuErrorSink *sink;
3272 assert(qemu_error_sink != NULL);
3273 sink = qemu_error_sink;
3274 qemu_error_sink = sink->previous;
3275 qemu_free(sink);
3278 void qemu_error(const char *fmt, ...)
3280 va_list args;
3282 assert(qemu_error_sink != NULL);
3283 switch (qemu_error_sink->dest) {
3284 case ERR_SINK_FILE:
3285 va_start(args, fmt);
3286 vfprintf(qemu_error_sink->fp, fmt, args);
3287 va_end(args);
3288 break;
3289 case ERR_SINK_MONITOR:
3290 va_start(args, fmt);
3291 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3292 va_end(args);
3293 break;