Merge commit 'fd37d8813ca796f5c9993404baffe79981d9b634' into upstream-merge
[qemu/qemu-dev-zwu.git] / monitor.c
blob98393715e8eac41a8aca8f3279afe9896b9cf9cc
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <dirent.h>
25 #include "hw/hw.h"
26 #include "hw/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/pcmcia.h"
29 #include "hw/pc.h"
30 #include "hw/pci.h"
31 #include "hw/watchdog.h"
32 #include "gdbstub.h"
33 #include "net.h"
34 #include "qemu-char.h"
35 #include "sysemu.h"
36 #include "monitor.h"
37 #include "readline.h"
38 #include "console.h"
39 #include "block.h"
40 #include "audio/audio.h"
41 #include "disas.h"
42 #include "balloon.h"
43 #include "qemu-timer.h"
44 #include "migration.h"
45 #include "kvm.h"
46 #include "acl.h"
47 #include "exec-all.h"
49 #include "qemu-kvm.h"
51 //#define DEBUG
52 //#define DEBUG_COMPLETION
55 * Supported types:
57 * 'F' filename
58 * 'B' block device name
59 * 's' string (accept optional quote)
60 * 'i' 32 bit integer
61 * 'l' target long (32 or 64 bit)
62 * '/' optional gdb-like print format (like "/10x")
64 * '?' optional type (for 'F', 's' and 'i')
68 typedef struct mon_cmd_t {
69 const char *name;
70 const char *args_type;
71 void *handler;
72 const char *params;
73 const char *help;
74 } mon_cmd_t;
76 /* file descriptors passed via SCM_RIGHTS */
77 typedef struct mon_fd_t mon_fd_t;
78 struct mon_fd_t {
79 char *name;
80 int fd;
81 LIST_ENTRY(mon_fd_t) next;
84 struct Monitor {
85 CharDriverState *chr;
86 int flags;
87 int suspend_cnt;
88 uint8_t outbuf[1024];
89 int outbuf_index;
90 ReadLineState *rs;
91 CPUState *mon_cpu;
92 BlockDriverCompletionFunc *password_completion_cb;
93 void *password_opaque;
94 LIST_HEAD(,mon_fd_t) fds;
95 LIST_ENTRY(Monitor) entry;
98 static LIST_HEAD(mon_list, Monitor) mon_list;
100 static const mon_cmd_t mon_cmds[];
101 static const mon_cmd_t info_cmds[];
103 Monitor *cur_mon = NULL;
105 static void monitor_command_cb(Monitor *mon, const char *cmdline,
106 void *opaque);
108 static void monitor_read_command(Monitor *mon, int show_prompt)
110 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
111 if (show_prompt)
112 readline_show_prompt(mon->rs);
115 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
116 void *opaque)
118 if (mon->rs) {
119 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
120 /* prompt is printed on return from the command handler */
121 return 0;
122 } else {
123 monitor_printf(mon, "terminal does not support password prompting\n");
124 return -ENOTTY;
128 void monitor_flush(Monitor *mon)
130 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
131 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
132 mon->outbuf_index = 0;
136 /* flush at every end of line or if the buffer is full */
137 static void monitor_puts(Monitor *mon, const char *str)
139 char c;
141 if (!mon)
142 return;
144 for(;;) {
145 c = *str++;
146 if (c == '\0')
147 break;
148 if (c == '\n')
149 mon->outbuf[mon->outbuf_index++] = '\r';
150 mon->outbuf[mon->outbuf_index++] = c;
151 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
152 || c == '\n')
153 monitor_flush(mon);
157 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
159 char buf[4096];
160 vsnprintf(buf, sizeof(buf), fmt, ap);
161 monitor_puts(mon, buf);
164 void monitor_printf(Monitor *mon, const char *fmt, ...)
166 va_list ap;
167 va_start(ap, fmt);
168 monitor_vprintf(mon, fmt, ap);
169 va_end(ap);
172 void monitor_print_filename(Monitor *mon, const char *filename)
174 int i;
176 for (i = 0; filename[i]; i++) {
177 switch (filename[i]) {
178 case ' ':
179 case '"':
180 case '\\':
181 monitor_printf(mon, "\\%c", filename[i]);
182 break;
183 case '\t':
184 monitor_printf(mon, "\\t");
185 break;
186 case '\r':
187 monitor_printf(mon, "\\r");
188 break;
189 case '\n':
190 monitor_printf(mon, "\\n");
191 break;
192 default:
193 monitor_printf(mon, "%c", filename[i]);
194 break;
199 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
201 va_list ap;
202 va_start(ap, fmt);
203 monitor_vprintf((Monitor *)stream, fmt, ap);
204 va_end(ap);
205 return 0;
208 static int compare_cmd(const char *name, const char *list)
210 const char *p, *pstart;
211 int len;
212 len = strlen(name);
213 p = list;
214 for(;;) {
215 pstart = p;
216 p = strchr(p, '|');
217 if (!p)
218 p = pstart + strlen(pstart);
219 if ((p - pstart) == len && !memcmp(pstart, name, len))
220 return 1;
221 if (*p == '\0')
222 break;
223 p++;
225 return 0;
228 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
229 const char *prefix, const char *name)
231 const mon_cmd_t *cmd;
233 for(cmd = cmds; cmd->name != NULL; cmd++) {
234 if (!name || !strcmp(name, cmd->name))
235 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
236 cmd->params, cmd->help);
240 static void help_cmd(Monitor *mon, const char *name)
242 if (name && !strcmp(name, "info")) {
243 help_cmd_dump(mon, info_cmds, "info ", NULL);
244 } else {
245 help_cmd_dump(mon, mon_cmds, "", name);
246 if (name && !strcmp(name, "log")) {
247 const CPULogItem *item;
248 monitor_printf(mon, "Log items (comma separated):\n");
249 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
250 for(item = cpu_log_items; item->mask != 0; item++) {
251 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
257 static void do_commit(Monitor *mon, const char *device)
259 int all_devices;
260 DriveInfo *dinfo;
262 all_devices = !strcmp(device, "all");
263 TAILQ_FOREACH(dinfo, &drives, next) {
264 if (!all_devices)
265 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
266 continue;
267 bdrv_commit(dinfo->bdrv);
271 static void do_info(Monitor *mon, const char *item)
273 const mon_cmd_t *cmd;
274 void (*handler)(Monitor *);
276 if (!item)
277 goto help;
278 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
279 if (compare_cmd(item, cmd->name))
280 goto found;
282 help:
283 help_cmd(mon, "info");
284 return;
285 found:
286 handler = cmd->handler;
287 handler(mon);
290 static void do_info_version(Monitor *mon)
292 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
295 static void do_info_name(Monitor *mon)
297 if (qemu_name)
298 monitor_printf(mon, "%s\n", qemu_name);
301 #if defined(TARGET_I386)
302 static void do_info_hpet(Monitor *mon)
304 monitor_printf(mon, "HPET is %s by QEMU\n",
305 (no_hpet) ? "disabled" : "enabled");
307 #endif
309 static void do_info_uuid(Monitor *mon)
311 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
312 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
313 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
314 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
315 qemu_uuid[14], qemu_uuid[15]);
318 /* get the current CPU defined by the user */
319 static int mon_set_cpu(int cpu_index)
321 CPUState *env;
323 for(env = first_cpu; env != NULL; env = env->next_cpu) {
324 if (env->cpu_index == cpu_index) {
325 cur_mon->mon_cpu = env;
326 return 0;
329 return -1;
332 static CPUState *mon_get_cpu(void)
334 if (!cur_mon->mon_cpu) {
335 mon_set_cpu(0);
337 cpu_synchronize_state(cur_mon->mon_cpu);
338 return cur_mon->mon_cpu;
341 static void do_info_registers(Monitor *mon)
343 CPUState *env;
344 env = mon_get_cpu();
345 if (!env)
346 return;
347 #ifdef TARGET_I386
348 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
349 X86_DUMP_FPU);
350 #else
351 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
353 #endif
356 static void do_info_cpus(Monitor *mon)
358 CPUState *env;
360 /* just to set the default cpu if not already done */
361 mon_get_cpu();
363 for(env = first_cpu; env != NULL; env = env->next_cpu) {
364 cpu_synchronize_state(env);
365 monitor_printf(mon, "%c CPU #%d:",
366 (env == mon->mon_cpu) ? '*' : ' ',
367 env->cpu_index);
368 #if defined(TARGET_I386)
369 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
370 env->eip + env->segs[R_CS].base);
371 #elif defined(TARGET_PPC)
372 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
373 #elif defined(TARGET_SPARC)
374 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
375 env->pc, env->npc);
376 #elif defined(TARGET_MIPS)
377 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
378 #endif
379 if (env->halted)
380 monitor_printf(mon, " (halted)");
381 monitor_printf(mon," thread_id=%d", env->thread_id);
382 monitor_printf(mon, "\n");
386 static void do_cpu_set(Monitor *mon, int index)
388 if (mon_set_cpu(index) < 0)
389 monitor_printf(mon, "Invalid CPU index\n");
392 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
394 int state;
396 if (!strcmp(status, "online"))
397 state = 1;
398 else if (!strcmp(status, "offline"))
399 state = 0;
400 else {
401 monitor_printf(mon, "invalid status: %s\n", status);
402 return;
404 #if defined(TARGET_I386) || defined(TARGET_X86_64)
405 qemu_system_cpu_hot_add(value, state);
406 #endif
409 static void do_info_jit(Monitor *mon)
411 dump_exec_info((FILE *)mon, monitor_fprintf);
414 static void do_info_history(Monitor *mon)
416 int i;
417 const char *str;
419 if (!mon->rs)
420 return;
421 i = 0;
422 for(;;) {
423 str = readline_get_history(mon->rs, i);
424 if (!str)
425 break;
426 monitor_printf(mon, "%d: '%s'\n", i, str);
427 i++;
431 #if defined(TARGET_PPC)
432 /* XXX: not implemented in other targets */
433 static void do_info_cpu_stats(Monitor *mon)
435 CPUState *env;
437 env = mon_get_cpu();
438 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
440 #endif
442 static void do_quit(Monitor *mon)
444 exit(0);
447 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
449 if (bdrv_is_inserted(bs)) {
450 if (!force) {
451 if (!bdrv_is_removable(bs)) {
452 monitor_printf(mon, "device is not removable\n");
453 return -1;
455 if (bdrv_is_locked(bs)) {
456 monitor_printf(mon, "device is locked\n");
457 return -1;
460 bdrv_close(bs);
462 return 0;
465 static void do_eject(Monitor *mon, int force, const char *filename)
467 BlockDriverState *bs;
469 bs = bdrv_find(filename);
470 if (!bs) {
471 monitor_printf(mon, "device not found\n");
472 return;
474 eject_device(mon, bs, force);
477 static void do_change_block(Monitor *mon, const char *device,
478 const char *filename, const char *fmt)
480 BlockDriverState *bs;
481 BlockDriver *drv = NULL;
483 bs = bdrv_find(device);
484 if (!bs) {
485 monitor_printf(mon, "device not found\n");
486 return;
488 if (fmt) {
489 drv = bdrv_find_format(fmt);
490 if (!drv) {
491 monitor_printf(mon, "invalid format %s\n", fmt);
492 return;
495 if (eject_device(mon, bs, 0) < 0)
496 return;
497 bdrv_open2(bs, filename, 0, drv);
498 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
501 static void change_vnc_password_cb(Monitor *mon, const char *password,
502 void *opaque)
504 if (vnc_display_password(NULL, password) < 0)
505 monitor_printf(mon, "could not set VNC server password\n");
507 monitor_read_command(mon, 1);
510 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
512 if (strcmp(target, "passwd") == 0 ||
513 strcmp(target, "password") == 0) {
514 if (arg) {
515 char password[9];
516 strncpy(password, arg, sizeof(password));
517 password[sizeof(password) - 1] = '\0';
518 change_vnc_password_cb(mon, password, NULL);
519 } else {
520 monitor_read_password(mon, change_vnc_password_cb, NULL);
522 } else {
523 if (vnc_display_open(NULL, target) < 0)
524 monitor_printf(mon, "could not start VNC server on %s\n", target);
528 static void do_change(Monitor *mon, const char *device, const char *target,
529 const char *arg)
531 if (strcmp(device, "vnc") == 0) {
532 do_change_vnc(mon, target, arg);
533 } else {
534 do_change_block(mon, device, target, arg);
538 static void do_screen_dump(Monitor *mon, const char *filename)
540 vga_hw_screen_dump(filename);
543 static void do_logfile(Monitor *mon, const char *filename)
545 cpu_set_log_filename(filename);
548 static void do_log(Monitor *mon, const char *items)
550 int mask;
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 char *option)
566 if (!option || !strcmp(option, "on")) {
567 singlestep = 1;
568 } else if (!strcmp(option, "off")) {
569 singlestep = 0;
570 } else {
571 monitor_printf(mon, "unexpected option %s\n", option);
575 static void do_stop(Monitor *mon)
577 vm_stop(EXCP_INTERRUPT);
580 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
582 struct bdrv_iterate_context {
583 Monitor *mon;
584 int err;
587 static void do_cont(Monitor *mon)
589 struct bdrv_iterate_context context = { mon, 0 };
591 bdrv_iterate(encrypted_bdrv_it, &context);
592 /* only resume the vm if all keys are set and valid */
593 if (!context.err)
594 vm_start();
597 static void bdrv_key_cb(void *opaque, int err)
599 Monitor *mon = opaque;
601 /* another key was set successfully, retry to continue */
602 if (!err)
603 do_cont(mon);
606 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
608 struct bdrv_iterate_context *context = opaque;
610 if (!context->err && bdrv_key_required(bs)) {
611 context->err = -EBUSY;
612 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
613 context->mon);
617 static void do_gdbserver(Monitor *mon, const char *device)
619 if (!device)
620 device = "tcp::" DEFAULT_GDBSTUB_PORT;
621 if (gdbserver_start(device) < 0) {
622 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
623 device);
624 } else if (strcmp(device, "none") == 0) {
625 monitor_printf(mon, "Disabled gdbserver\n");
626 } else {
627 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
628 device);
632 static void do_watchdog_action(Monitor *mon, const char *action)
634 if (select_watchdog_action(action) == -1) {
635 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
639 static void monitor_printc(Monitor *mon, int c)
641 monitor_printf(mon, "'");
642 switch(c) {
643 case '\'':
644 monitor_printf(mon, "\\'");
645 break;
646 case '\\':
647 monitor_printf(mon, "\\\\");
648 break;
649 case '\n':
650 monitor_printf(mon, "\\n");
651 break;
652 case '\r':
653 monitor_printf(mon, "\\r");
654 break;
655 default:
656 if (c >= 32 && c <= 126) {
657 monitor_printf(mon, "%c", c);
658 } else {
659 monitor_printf(mon, "\\x%02x", c);
661 break;
663 monitor_printf(mon, "'");
666 static void memory_dump(Monitor *mon, int count, int format, int wsize,
667 target_phys_addr_t addr, int is_physical)
669 CPUState *env;
670 int nb_per_line, l, line_size, i, max_digits, len;
671 uint8_t buf[16];
672 uint64_t v;
674 if (format == 'i') {
675 int flags;
676 flags = 0;
677 env = mon_get_cpu();
678 if (!env && !is_physical)
679 return;
680 #ifdef TARGET_I386
681 if (wsize == 2) {
682 flags = 1;
683 } else if (wsize == 4) {
684 flags = 0;
685 } else {
686 /* as default we use the current CS size */
687 flags = 0;
688 if (env) {
689 #ifdef TARGET_X86_64
690 if ((env->efer & MSR_EFER_LMA) &&
691 (env->segs[R_CS].flags & DESC_L_MASK))
692 flags = 2;
693 else
694 #endif
695 if (!(env->segs[R_CS].flags & DESC_B_MASK))
696 flags = 1;
699 #endif
700 monitor_disas(mon, env, addr, count, is_physical, flags);
701 return;
704 len = wsize * count;
705 if (wsize == 1)
706 line_size = 8;
707 else
708 line_size = 16;
709 nb_per_line = line_size / wsize;
710 max_digits = 0;
712 switch(format) {
713 case 'o':
714 max_digits = (wsize * 8 + 2) / 3;
715 break;
716 default:
717 case 'x':
718 max_digits = (wsize * 8) / 4;
719 break;
720 case 'u':
721 case 'd':
722 max_digits = (wsize * 8 * 10 + 32) / 33;
723 break;
724 case 'c':
725 wsize = 1;
726 break;
729 while (len > 0) {
730 if (is_physical)
731 monitor_printf(mon, TARGET_FMT_plx ":", addr);
732 else
733 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
734 l = len;
735 if (l > line_size)
736 l = line_size;
737 if (is_physical) {
738 cpu_physical_memory_rw(addr, buf, l, 0);
739 } else {
740 env = mon_get_cpu();
741 if (!env)
742 break;
743 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
744 monitor_printf(mon, " Cannot access memory\n");
745 break;
748 i = 0;
749 while (i < l) {
750 switch(wsize) {
751 default:
752 case 1:
753 v = ldub_raw(buf + i);
754 break;
755 case 2:
756 v = lduw_raw(buf + i);
757 break;
758 case 4:
759 v = (uint32_t)ldl_raw(buf + i);
760 break;
761 case 8:
762 v = ldq_raw(buf + i);
763 break;
765 monitor_printf(mon, " ");
766 switch(format) {
767 case 'o':
768 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
769 break;
770 case 'x':
771 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
772 break;
773 case 'u':
774 monitor_printf(mon, "%*" PRIu64, max_digits, v);
775 break;
776 case 'd':
777 monitor_printf(mon, "%*" PRId64, max_digits, v);
778 break;
779 case 'c':
780 monitor_printc(mon, v);
781 break;
783 i += wsize;
785 monitor_printf(mon, "\n");
786 addr += l;
787 len -= l;
791 #if TARGET_LONG_BITS == 64
792 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
793 #else
794 #define GET_TLONG(h, l) (l)
795 #endif
797 static void do_memory_dump(Monitor *mon, int count, int format, int size,
798 uint32_t addrh, uint32_t addrl)
800 target_long addr = GET_TLONG(addrh, addrl);
801 memory_dump(mon, count, format, size, addr, 0);
804 #if TARGET_PHYS_ADDR_BITS > 32
805 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
806 #else
807 #define GET_TPHYSADDR(h, l) (l)
808 #endif
810 static void do_physical_memory_dump(Monitor *mon, int count, int format,
811 int size, uint32_t addrh, uint32_t addrl)
814 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
815 memory_dump(mon, count, format, size, addr, 1);
818 static void do_print(Monitor *mon, int count, int format, int size,
819 unsigned int valh, unsigned int vall)
821 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
822 #if TARGET_PHYS_ADDR_BITS == 32
823 switch(format) {
824 case 'o':
825 monitor_printf(mon, "%#o", val);
826 break;
827 case 'x':
828 monitor_printf(mon, "%#x", val);
829 break;
830 case 'u':
831 monitor_printf(mon, "%u", val);
832 break;
833 default:
834 case 'd':
835 monitor_printf(mon, "%d", val);
836 break;
837 case 'c':
838 monitor_printc(mon, val);
839 break;
841 #else
842 switch(format) {
843 case 'o':
844 monitor_printf(mon, "%#" PRIo64, val);
845 break;
846 case 'x':
847 monitor_printf(mon, "%#" PRIx64, val);
848 break;
849 case 'u':
850 monitor_printf(mon, "%" PRIu64, val);
851 break;
852 default:
853 case 'd':
854 monitor_printf(mon, "%" PRId64, val);
855 break;
856 case 'c':
857 monitor_printc(mon, val);
858 break;
860 #endif
861 monitor_printf(mon, "\n");
864 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
865 uint32_t size, const char *filename)
867 FILE *f;
868 target_long addr = GET_TLONG(valh, vall);
869 uint32_t l;
870 CPUState *env;
871 uint8_t buf[1024];
873 env = mon_get_cpu();
874 if (!env)
875 return;
877 f = fopen(filename, "wb");
878 if (!f) {
879 monitor_printf(mon, "could not open '%s'\n", filename);
880 return;
882 while (size != 0) {
883 l = sizeof(buf);
884 if (l > size)
885 l = size;
886 cpu_memory_rw_debug(env, addr, buf, l, 0);
887 fwrite(buf, 1, l, f);
888 addr += l;
889 size -= l;
891 fclose(f);
894 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
895 unsigned int vall, uint32_t size,
896 const char *filename)
898 FILE *f;
899 uint32_t l;
900 uint8_t buf[1024];
901 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
903 f = fopen(filename, "wb");
904 if (!f) {
905 monitor_printf(mon, "could not open '%s'\n", filename);
906 return;
908 while (size != 0) {
909 l = sizeof(buf);
910 if (l > size)
911 l = size;
912 cpu_physical_memory_rw(addr, buf, l, 0);
913 fwrite(buf, 1, l, f);
914 fflush(f);
915 addr += l;
916 size -= l;
918 fclose(f);
921 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
923 uint32_t addr;
924 uint8_t buf[1];
925 uint16_t sum;
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 char *string, int has_hold_time,
1118 int hold_time)
1120 char keyname_buf[16];
1121 char *separator;
1122 int keyname_len, keycode, i;
1124 if (nb_pending_keycodes > 0) {
1125 qemu_del_timer(key_timer);
1126 release_keys(NULL);
1128 if (!has_hold_time)
1129 hold_time = 100;
1130 i = 0;
1131 while (1) {
1132 separator = strchr(string, '-');
1133 keyname_len = separator ? separator - string : strlen(string);
1134 if (keyname_len > 0) {
1135 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1136 if (keyname_len > sizeof(keyname_buf) - 1) {
1137 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1138 return;
1140 if (i == MAX_KEYCODES) {
1141 monitor_printf(mon, "too many keys\n");
1142 return;
1144 keyname_buf[keyname_len] = 0;
1145 keycode = get_keycode(keyname_buf);
1146 if (keycode < 0) {
1147 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1148 return;
1150 keycodes[i++] = keycode;
1152 if (!separator)
1153 break;
1154 string = separator + 1;
1156 nb_pending_keycodes = i;
1157 /* key down events */
1158 for (i = 0; i < nb_pending_keycodes; i++) {
1159 keycode = keycodes[i];
1160 if (keycode & 0x80)
1161 kbd_put_keycode(0xe0);
1162 kbd_put_keycode(keycode & 0x7f);
1164 /* delayed key up events */
1165 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1166 muldiv64(ticks_per_sec, hold_time, 1000));
1169 static int mouse_button_state;
1171 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1172 const char *dz_str)
1174 int dx, dy, dz;
1175 dx = strtol(dx_str, NULL, 0);
1176 dy = strtol(dy_str, NULL, 0);
1177 dz = 0;
1178 if (dz_str)
1179 dz = strtol(dz_str, NULL, 0);
1180 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1183 static void do_mouse_button(Monitor *mon, int button_state)
1185 mouse_button_state = button_state;
1186 kbd_mouse_event(0, 0, 0, mouse_button_state);
1189 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1190 int addr, int has_index, int index)
1192 uint32_t val;
1193 int suffix;
1195 if (has_index) {
1196 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1197 addr++;
1199 addr &= 0xffff;
1201 switch(size) {
1202 default:
1203 case 1:
1204 val = cpu_inb(NULL, addr);
1205 suffix = 'b';
1206 break;
1207 case 2:
1208 val = cpu_inw(NULL, addr);
1209 suffix = 'w';
1210 break;
1211 case 4:
1212 val = cpu_inl(NULL, addr);
1213 suffix = 'l';
1214 break;
1216 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1217 suffix, addr, size * 2, val);
1220 static void do_ioport_write(Monitor *mon, int count, int format, int size,
1221 int addr, int val)
1223 addr &= IOPORTS_MASK;
1225 switch (size) {
1226 default:
1227 case 1:
1228 cpu_outb(NULL, addr, val);
1229 break;
1230 case 2:
1231 cpu_outw(NULL, addr, val);
1232 break;
1233 case 4:
1234 cpu_outl(NULL, addr, val);
1235 break;
1239 static void do_boot_set(Monitor *mon, const char *bootdevice)
1241 int res;
1243 res = qemu_boot_set(bootdevice);
1244 if (res == 0) {
1245 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1246 } else if (res > 0) {
1247 monitor_printf(mon, "setting boot device list failed\n");
1248 } else {
1249 monitor_printf(mon, "no function defined to set boot device list for "
1250 "this architecture\n");
1254 static void do_system_reset(Monitor *mon)
1256 qemu_system_reset_request();
1259 static void do_system_powerdown(Monitor *mon)
1261 qemu_system_powerdown_request();
1264 #if defined(TARGET_I386)
1265 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1267 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1268 addr,
1269 pte & mask,
1270 pte & PG_GLOBAL_MASK ? 'G' : '-',
1271 pte & PG_PSE_MASK ? 'P' : '-',
1272 pte & PG_DIRTY_MASK ? 'D' : '-',
1273 pte & PG_ACCESSED_MASK ? 'A' : '-',
1274 pte & PG_PCD_MASK ? 'C' : '-',
1275 pte & PG_PWT_MASK ? 'T' : '-',
1276 pte & PG_USER_MASK ? 'U' : '-',
1277 pte & PG_RW_MASK ? 'W' : '-');
1280 static void tlb_info(Monitor *mon)
1282 CPUState *env;
1283 int l1, l2;
1284 uint32_t pgd, pde, pte;
1286 env = mon_get_cpu();
1287 if (!env)
1288 return;
1290 if (!(env->cr[0] & CR0_PG_MASK)) {
1291 monitor_printf(mon, "PG disabled\n");
1292 return;
1294 pgd = env->cr[3] & ~0xfff;
1295 for(l1 = 0; l1 < 1024; l1++) {
1296 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1297 pde = le32_to_cpu(pde);
1298 if (pde & PG_PRESENT_MASK) {
1299 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1300 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1301 } else {
1302 for(l2 = 0; l2 < 1024; l2++) {
1303 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1304 (uint8_t *)&pte, 4);
1305 pte = le32_to_cpu(pte);
1306 if (pte & PG_PRESENT_MASK) {
1307 print_pte(mon, (l1 << 22) + (l2 << 12),
1308 pte & ~PG_PSE_MASK,
1309 ~0xfff);
1317 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1318 uint32_t end, int prot)
1320 int prot1;
1321 prot1 = *plast_prot;
1322 if (prot != prot1) {
1323 if (*pstart != -1) {
1324 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1325 *pstart, end, end - *pstart,
1326 prot1 & PG_USER_MASK ? 'u' : '-',
1327 'r',
1328 prot1 & PG_RW_MASK ? 'w' : '-');
1330 if (prot != 0)
1331 *pstart = end;
1332 else
1333 *pstart = -1;
1334 *plast_prot = prot;
1338 static void mem_info(Monitor *mon)
1340 CPUState *env;
1341 int l1, l2, prot, last_prot;
1342 uint32_t pgd, pde, pte, start, end;
1344 env = mon_get_cpu();
1345 if (!env)
1346 return;
1348 if (!(env->cr[0] & CR0_PG_MASK)) {
1349 monitor_printf(mon, "PG disabled\n");
1350 return;
1352 pgd = env->cr[3] & ~0xfff;
1353 last_prot = 0;
1354 start = -1;
1355 for(l1 = 0; l1 < 1024; l1++) {
1356 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1357 pde = le32_to_cpu(pde);
1358 end = l1 << 22;
1359 if (pde & PG_PRESENT_MASK) {
1360 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1361 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1362 mem_print(mon, &start, &last_prot, end, prot);
1363 } else {
1364 for(l2 = 0; l2 < 1024; l2++) {
1365 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1366 (uint8_t *)&pte, 4);
1367 pte = le32_to_cpu(pte);
1368 end = (l1 << 22) + (l2 << 12);
1369 if (pte & PG_PRESENT_MASK) {
1370 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1371 } else {
1372 prot = 0;
1374 mem_print(mon, &start, &last_prot, end, prot);
1377 } else {
1378 prot = 0;
1379 mem_print(mon, &start, &last_prot, end, prot);
1383 #endif
1385 #if defined(TARGET_SH4)
1387 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1389 monitor_printf(mon, " tlb%i:\t"
1390 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1391 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1392 "dirty=%hhu writethrough=%hhu\n",
1393 idx,
1394 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1395 tlb->v, tlb->sh, tlb->c, tlb->pr,
1396 tlb->d, tlb->wt);
1399 static void tlb_info(Monitor *mon)
1401 CPUState *env = mon_get_cpu();
1402 int i;
1404 monitor_printf (mon, "ITLB:\n");
1405 for (i = 0 ; i < ITLB_SIZE ; i++)
1406 print_tlb (mon, i, &env->itlb[i]);
1407 monitor_printf (mon, "UTLB:\n");
1408 for (i = 0 ; i < UTLB_SIZE ; i++)
1409 print_tlb (mon, i, &env->utlb[i]);
1412 #endif
1414 static void do_info_kvm(Monitor *mon)
1416 #if defined(USE_KVM) || defined(CONFIG_KVM)
1417 monitor_printf(mon, "kvm support: ");
1418 if (kvm_enabled())
1419 monitor_printf(mon, "enabled\n");
1420 else
1421 monitor_printf(mon, "disabled\n");
1422 #else
1423 monitor_printf(mon, "kvm support: not compiled\n");
1424 #endif
1427 static void do_info_numa(Monitor *mon)
1429 int i;
1430 CPUState *env;
1432 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1433 for (i = 0; i < nb_numa_nodes; i++) {
1434 monitor_printf(mon, "node %d cpus:", i);
1435 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1436 if (env->numa_node == i) {
1437 monitor_printf(mon, " %d", env->cpu_index);
1440 monitor_printf(mon, "\n");
1441 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1442 node_mem[i] >> 20);
1446 #ifdef CONFIG_PROFILER
1448 static void do_info_profile(Monitor *mon)
1450 int64_t total;
1451 total = qemu_time;
1452 if (total == 0)
1453 total = 1;
1454 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1455 dev_time, dev_time / (double)ticks_per_sec);
1456 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1457 qemu_time, qemu_time / (double)ticks_per_sec);
1458 qemu_time = 0;
1459 dev_time = 0;
1461 #else
1462 static void do_info_profile(Monitor *mon)
1464 monitor_printf(mon, "Internal profiler not compiled\n");
1466 #endif
1468 /* Capture support */
1469 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1471 static void do_info_capture(Monitor *mon)
1473 int i;
1474 CaptureState *s;
1476 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1477 monitor_printf(mon, "[%d]: ", i);
1478 s->ops.info (s->opaque);
1482 #ifdef HAS_AUDIO
1483 static void do_stop_capture(Monitor *mon, int n)
1485 int i;
1486 CaptureState *s;
1488 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1489 if (i == n) {
1490 s->ops.destroy (s->opaque);
1491 LIST_REMOVE (s, entries);
1492 qemu_free (s);
1493 return;
1498 static void do_wav_capture(Monitor *mon, const char *path,
1499 int has_freq, int freq,
1500 int has_bits, int bits,
1501 int has_channels, int nchannels)
1503 CaptureState *s;
1505 s = qemu_mallocz (sizeof (*s));
1507 freq = has_freq ? freq : 44100;
1508 bits = has_bits ? bits : 16;
1509 nchannels = has_channels ? nchannels : 2;
1511 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1512 monitor_printf(mon, "Faied to add wave capture\n");
1513 qemu_free (s);
1515 LIST_INSERT_HEAD (&capture_head, s, entries);
1517 #endif
1519 #if defined(TARGET_I386)
1520 static void do_inject_nmi(Monitor *mon, int cpu_index)
1522 CPUState *env;
1524 for (env = first_cpu; env != NULL; env = env->next_cpu)
1525 if (env->cpu_index == cpu_index) {
1526 if (kvm_enabled())
1527 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1528 else
1529 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1530 break;
1533 #endif
1535 static void do_info_status(Monitor *mon)
1537 if (vm_running) {
1538 if (singlestep) {
1539 monitor_printf(mon, "VM status: running (single step mode)\n");
1540 } else {
1541 monitor_printf(mon, "VM status: running\n");
1543 } else
1544 monitor_printf(mon, "VM status: paused\n");
1548 static void do_balloon(Monitor *mon, int value)
1550 ram_addr_t target = value;
1551 qemu_balloon(target << 20);
1554 static void do_info_balloon(Monitor *mon)
1556 ram_addr_t actual;
1558 actual = qemu_balloon_status();
1559 if (kvm_enabled() && !kvm_has_sync_mmu())
1560 monitor_printf(mon, "Using KVM without synchronous MMU, "
1561 "ballooning disabled\n");
1562 else if (actual == 0)
1563 monitor_printf(mon, "Ballooning not activated in VM\n");
1564 else
1565 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1568 static qemu_acl *find_acl(Monitor *mon, const char *name)
1570 qemu_acl *acl = qemu_acl_find(name);
1572 if (!acl) {
1573 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1575 return acl;
1578 static void do_acl_show(Monitor *mon, const char *aclname)
1580 qemu_acl *acl = find_acl(mon, aclname);
1581 qemu_acl_entry *entry;
1582 int i = 0;
1584 if (acl) {
1585 monitor_printf(mon, "policy: %s\n",
1586 acl->defaultDeny ? "deny" : "allow");
1587 TAILQ_FOREACH(entry, &acl->entries, next) {
1588 i++;
1589 monitor_printf(mon, "%d: %s %s\n", i,
1590 entry->deny ? "deny" : "allow", entry->match);
1595 static void do_acl_reset(Monitor *mon, const char *aclname)
1597 qemu_acl *acl = find_acl(mon, aclname);
1599 if (acl) {
1600 qemu_acl_reset(acl);
1601 monitor_printf(mon, "acl: removed all rules\n");
1605 static void do_acl_policy(Monitor *mon, const char *aclname,
1606 const char *policy)
1608 qemu_acl *acl = find_acl(mon, aclname);
1610 if (acl) {
1611 if (strcmp(policy, "allow") == 0) {
1612 acl->defaultDeny = 0;
1613 monitor_printf(mon, "acl: policy set to 'allow'\n");
1614 } else if (strcmp(policy, "deny") == 0) {
1615 acl->defaultDeny = 1;
1616 monitor_printf(mon, "acl: policy set to 'deny'\n");
1617 } else {
1618 monitor_printf(mon, "acl: unknown policy '%s', "
1619 "expected 'deny' or 'allow'\n", policy);
1624 static void do_acl_add(Monitor *mon, const char *aclname,
1625 const char *match, const char *policy,
1626 int has_index, int index)
1628 qemu_acl *acl = find_acl(mon, aclname);
1629 int deny, ret;
1631 if (acl) {
1632 if (strcmp(policy, "allow") == 0) {
1633 deny = 0;
1634 } else if (strcmp(policy, "deny") == 0) {
1635 deny = 1;
1636 } else {
1637 monitor_printf(mon, "acl: unknown policy '%s', "
1638 "expected 'deny' or 'allow'\n", policy);
1639 return;
1641 if (has_index)
1642 ret = qemu_acl_insert(acl, deny, match, index);
1643 else
1644 ret = qemu_acl_append(acl, deny, match);
1645 if (ret < 0)
1646 monitor_printf(mon, "acl: unable to add acl entry\n");
1647 else
1648 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1652 static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1654 qemu_acl *acl = find_acl(mon, aclname);
1655 int ret;
1657 if (acl) {
1658 ret = qemu_acl_remove(acl, match);
1659 if (ret < 0)
1660 monitor_printf(mon, "acl: no matching acl entry\n");
1661 else
1662 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1666 #if defined(TARGET_I386)
1667 static void do_inject_mce(Monitor *mon,
1668 int cpu_index, int bank,
1669 unsigned status_hi, unsigned status_lo,
1670 unsigned mcg_status_hi, unsigned mcg_status_lo,
1671 unsigned addr_hi, unsigned addr_lo,
1672 unsigned misc_hi, unsigned misc_lo)
1674 CPUState *cenv;
1675 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1676 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1677 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1678 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1680 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1681 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1682 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1683 break;
1686 #endif
1688 static void do_getfd(Monitor *mon, const char *fdname)
1690 mon_fd_t *monfd;
1691 int fd;
1693 fd = qemu_chr_get_msgfd(mon->chr);
1694 if (fd == -1) {
1695 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1696 return;
1699 if (qemu_isdigit(fdname[0])) {
1700 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1701 return;
1704 fd = dup(fd);
1705 if (fd == -1) {
1706 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1707 strerror(errno));
1708 return;
1711 LIST_FOREACH(monfd, &mon->fds, next) {
1712 if (strcmp(monfd->name, fdname) != 0) {
1713 continue;
1716 close(monfd->fd);
1717 monfd->fd = fd;
1718 return;
1721 monfd = qemu_mallocz(sizeof(mon_fd_t));
1722 monfd->name = qemu_strdup(fdname);
1723 monfd->fd = fd;
1725 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1728 static void do_closefd(Monitor *mon, const char *fdname)
1730 mon_fd_t *monfd;
1732 LIST_FOREACH(monfd, &mon->fds, next) {
1733 if (strcmp(monfd->name, fdname) != 0) {
1734 continue;
1737 LIST_REMOVE(monfd, next);
1738 close(monfd->fd);
1739 qemu_free(monfd->name);
1740 qemu_free(monfd);
1741 return;
1744 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1745 fdname);
1748 static void do_loadvm(Monitor *mon, const char *name)
1750 int saved_vm_running = vm_running;
1752 vm_stop(0);
1754 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1755 vm_start();
1758 int monitor_get_fd(Monitor *mon, const char *fdname)
1760 mon_fd_t *monfd;
1762 LIST_FOREACH(monfd, &mon->fds, next) {
1763 int fd;
1765 if (strcmp(monfd->name, fdname) != 0) {
1766 continue;
1769 fd = monfd->fd;
1771 /* caller takes ownership of fd */
1772 LIST_REMOVE(monfd, next);
1773 qemu_free(monfd->name);
1774 qemu_free(monfd);
1776 return fd;
1779 return -1;
1782 static const mon_cmd_t mon_cmds[] = {
1783 #include "qemu-monitor.h"
1784 { NULL, NULL, },
1787 /* Please update qemu-monitor.hx when adding or changing commands */
1788 static const mon_cmd_t info_cmds[] = {
1789 { "version", "", do_info_version,
1790 "", "show the version of QEMU" },
1791 { "network", "", do_info_network,
1792 "", "show the network state" },
1793 { "chardev", "", qemu_chr_info,
1794 "", "show the character devices" },
1795 { "block", "", bdrv_info,
1796 "", "show the block devices" },
1797 { "blockstats", "", bdrv_info_stats,
1798 "", "show block device statistics" },
1799 { "registers", "", do_info_registers,
1800 "", "show the cpu registers" },
1801 { "cpus", "", do_info_cpus,
1802 "", "show infos for each CPU" },
1803 { "history", "", do_info_history,
1804 "", "show the command line history", },
1805 { "irq", "", irq_info,
1806 "", "show the interrupts statistics (if available)", },
1807 { "pic", "", pic_info,
1808 "", "show i8259 (PIC) state", },
1809 { "pci", "", pci_info,
1810 "", "show PCI info", },
1811 #if defined(TARGET_I386) || defined(TARGET_SH4)
1812 { "tlb", "", tlb_info,
1813 "", "show virtual to physical memory mappings", },
1814 #endif
1815 #if defined(TARGET_I386)
1816 { "mem", "", mem_info,
1817 "", "show the active virtual memory mappings", },
1818 { "hpet", "", do_info_hpet,
1819 "", "show state of HPET", },
1820 #endif
1821 { "jit", "", do_info_jit,
1822 "", "show dynamic compiler info", },
1823 { "kvm", "", do_info_kvm,
1824 "", "show KVM information", },
1825 { "numa", "", do_info_numa,
1826 "", "show NUMA information", },
1827 { "usb", "", usb_info,
1828 "", "show guest USB devices", },
1829 { "usbhost", "", usb_host_info,
1830 "", "show host USB devices", },
1831 { "profile", "", do_info_profile,
1832 "", "show profiling information", },
1833 { "capture", "", do_info_capture,
1834 "", "show capture information" },
1835 { "snapshots", "", do_info_snapshots,
1836 "", "show the currently saved VM snapshots" },
1837 { "status", "", do_info_status,
1838 "", "show the current VM status (running|paused)" },
1839 { "pcmcia", "", pcmcia_info,
1840 "", "show guest PCMCIA status" },
1841 { "mice", "", do_info_mice,
1842 "", "show which guest mouse is receiving events" },
1843 { "vnc", "", do_info_vnc,
1844 "", "show the vnc server status"},
1845 { "name", "", do_info_name,
1846 "", "show the current VM name" },
1847 { "uuid", "", do_info_uuid,
1848 "", "show the current VM UUID" },
1849 #if defined(TARGET_PPC)
1850 { "cpustats", "", do_info_cpu_stats,
1851 "", "show CPU statistics", },
1852 #endif
1853 #if defined(CONFIG_SLIRP)
1854 { "usernet", "", do_info_usernet,
1855 "", "show user network stack connection states", },
1856 #endif
1857 { "migrate", "", do_info_migrate, "", "show migration status" },
1858 { "balloon", "", do_info_balloon,
1859 "", "show balloon information" },
1860 { "qtree", "", do_info_qtree,
1861 "", "show device tree" },
1862 { "qdm", "", do_info_qdm,
1863 "", "show qdev device model list" },
1864 { NULL, NULL, },
1867 /*******************************************************************/
1869 static const char *pch;
1870 static jmp_buf expr_env;
1872 #define MD_TLONG 0
1873 #define MD_I32 1
1875 typedef struct MonitorDef {
1876 const char *name;
1877 int offset;
1878 target_long (*get_value)(const struct MonitorDef *md, int val);
1879 int type;
1880 } MonitorDef;
1882 #if defined(TARGET_I386)
1883 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1885 CPUState *env = mon_get_cpu();
1886 if (!env)
1887 return 0;
1888 return env->eip + env->segs[R_CS].base;
1890 #endif
1892 #if defined(TARGET_PPC)
1893 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1895 CPUState *env = mon_get_cpu();
1896 unsigned int u;
1897 int i;
1899 if (!env)
1900 return 0;
1902 u = 0;
1903 for (i = 0; i < 8; i++)
1904 u |= env->crf[i] << (32 - (4 * i));
1906 return u;
1909 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1911 CPUState *env = mon_get_cpu();
1912 if (!env)
1913 return 0;
1914 return env->msr;
1917 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1919 CPUState *env = mon_get_cpu();
1920 if (!env)
1921 return 0;
1922 return env->xer;
1925 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1927 CPUState *env = mon_get_cpu();
1928 if (!env)
1929 return 0;
1930 return cpu_ppc_load_decr(env);
1933 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1935 CPUState *env = mon_get_cpu();
1936 if (!env)
1937 return 0;
1938 return cpu_ppc_load_tbu(env);
1941 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1943 CPUState *env = mon_get_cpu();
1944 if (!env)
1945 return 0;
1946 return cpu_ppc_load_tbl(env);
1948 #endif
1950 #if defined(TARGET_SPARC)
1951 #ifndef TARGET_SPARC64
1952 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1954 CPUState *env = mon_get_cpu();
1955 if (!env)
1956 return 0;
1957 return GET_PSR(env);
1959 #endif
1961 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1963 CPUState *env = mon_get_cpu();
1964 if (!env)
1965 return 0;
1966 return env->regwptr[val];
1968 #endif
1970 static const MonitorDef monitor_defs[] = {
1971 #ifdef TARGET_I386
1973 #define SEG(name, seg) \
1974 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1975 { name ".base", offsetof(CPUState, segs[seg].base) },\
1976 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1978 { "eax", offsetof(CPUState, regs[0]) },
1979 { "ecx", offsetof(CPUState, regs[1]) },
1980 { "edx", offsetof(CPUState, regs[2]) },
1981 { "ebx", offsetof(CPUState, regs[3]) },
1982 { "esp|sp", offsetof(CPUState, regs[4]) },
1983 { "ebp|fp", offsetof(CPUState, regs[5]) },
1984 { "esi", offsetof(CPUState, regs[6]) },
1985 { "edi", offsetof(CPUState, regs[7]) },
1986 #ifdef TARGET_X86_64
1987 { "r8", offsetof(CPUState, regs[8]) },
1988 { "r9", offsetof(CPUState, regs[9]) },
1989 { "r10", offsetof(CPUState, regs[10]) },
1990 { "r11", offsetof(CPUState, regs[11]) },
1991 { "r12", offsetof(CPUState, regs[12]) },
1992 { "r13", offsetof(CPUState, regs[13]) },
1993 { "r14", offsetof(CPUState, regs[14]) },
1994 { "r15", offsetof(CPUState, regs[15]) },
1995 #endif
1996 { "eflags", offsetof(CPUState, eflags) },
1997 { "eip", offsetof(CPUState, eip) },
1998 SEG("cs", R_CS)
1999 SEG("ds", R_DS)
2000 SEG("es", R_ES)
2001 SEG("ss", R_SS)
2002 SEG("fs", R_FS)
2003 SEG("gs", R_GS)
2004 { "pc", 0, monitor_get_pc, },
2005 #elif defined(TARGET_PPC)
2006 /* General purpose registers */
2007 { "r0", offsetof(CPUState, gpr[0]) },
2008 { "r1", offsetof(CPUState, gpr[1]) },
2009 { "r2", offsetof(CPUState, gpr[2]) },
2010 { "r3", offsetof(CPUState, gpr[3]) },
2011 { "r4", offsetof(CPUState, gpr[4]) },
2012 { "r5", offsetof(CPUState, gpr[5]) },
2013 { "r6", offsetof(CPUState, gpr[6]) },
2014 { "r7", offsetof(CPUState, gpr[7]) },
2015 { "r8", offsetof(CPUState, gpr[8]) },
2016 { "r9", offsetof(CPUState, gpr[9]) },
2017 { "r10", offsetof(CPUState, gpr[10]) },
2018 { "r11", offsetof(CPUState, gpr[11]) },
2019 { "r12", offsetof(CPUState, gpr[12]) },
2020 { "r13", offsetof(CPUState, gpr[13]) },
2021 { "r14", offsetof(CPUState, gpr[14]) },
2022 { "r15", offsetof(CPUState, gpr[15]) },
2023 { "r16", offsetof(CPUState, gpr[16]) },
2024 { "r17", offsetof(CPUState, gpr[17]) },
2025 { "r18", offsetof(CPUState, gpr[18]) },
2026 { "r19", offsetof(CPUState, gpr[19]) },
2027 { "r20", offsetof(CPUState, gpr[20]) },
2028 { "r21", offsetof(CPUState, gpr[21]) },
2029 { "r22", offsetof(CPUState, gpr[22]) },
2030 { "r23", offsetof(CPUState, gpr[23]) },
2031 { "r24", offsetof(CPUState, gpr[24]) },
2032 { "r25", offsetof(CPUState, gpr[25]) },
2033 { "r26", offsetof(CPUState, gpr[26]) },
2034 { "r27", offsetof(CPUState, gpr[27]) },
2035 { "r28", offsetof(CPUState, gpr[28]) },
2036 { "r29", offsetof(CPUState, gpr[29]) },
2037 { "r30", offsetof(CPUState, gpr[30]) },
2038 { "r31", offsetof(CPUState, gpr[31]) },
2039 /* Floating point registers */
2040 { "f0", offsetof(CPUState, fpr[0]) },
2041 { "f1", offsetof(CPUState, fpr[1]) },
2042 { "f2", offsetof(CPUState, fpr[2]) },
2043 { "f3", offsetof(CPUState, fpr[3]) },
2044 { "f4", offsetof(CPUState, fpr[4]) },
2045 { "f5", offsetof(CPUState, fpr[5]) },
2046 { "f6", offsetof(CPUState, fpr[6]) },
2047 { "f7", offsetof(CPUState, fpr[7]) },
2048 { "f8", offsetof(CPUState, fpr[8]) },
2049 { "f9", offsetof(CPUState, fpr[9]) },
2050 { "f10", offsetof(CPUState, fpr[10]) },
2051 { "f11", offsetof(CPUState, fpr[11]) },
2052 { "f12", offsetof(CPUState, fpr[12]) },
2053 { "f13", offsetof(CPUState, fpr[13]) },
2054 { "f14", offsetof(CPUState, fpr[14]) },
2055 { "f15", offsetof(CPUState, fpr[15]) },
2056 { "f16", offsetof(CPUState, fpr[16]) },
2057 { "f17", offsetof(CPUState, fpr[17]) },
2058 { "f18", offsetof(CPUState, fpr[18]) },
2059 { "f19", offsetof(CPUState, fpr[19]) },
2060 { "f20", offsetof(CPUState, fpr[20]) },
2061 { "f21", offsetof(CPUState, fpr[21]) },
2062 { "f22", offsetof(CPUState, fpr[22]) },
2063 { "f23", offsetof(CPUState, fpr[23]) },
2064 { "f24", offsetof(CPUState, fpr[24]) },
2065 { "f25", offsetof(CPUState, fpr[25]) },
2066 { "f26", offsetof(CPUState, fpr[26]) },
2067 { "f27", offsetof(CPUState, fpr[27]) },
2068 { "f28", offsetof(CPUState, fpr[28]) },
2069 { "f29", offsetof(CPUState, fpr[29]) },
2070 { "f30", offsetof(CPUState, fpr[30]) },
2071 { "f31", offsetof(CPUState, fpr[31]) },
2072 { "fpscr", offsetof(CPUState, fpscr) },
2073 /* Next instruction pointer */
2074 { "nip|pc", offsetof(CPUState, nip) },
2075 { "lr", offsetof(CPUState, lr) },
2076 { "ctr", offsetof(CPUState, ctr) },
2077 { "decr", 0, &monitor_get_decr, },
2078 { "ccr", 0, &monitor_get_ccr, },
2079 /* Machine state register */
2080 { "msr", 0, &monitor_get_msr, },
2081 { "xer", 0, &monitor_get_xer, },
2082 { "tbu", 0, &monitor_get_tbu, },
2083 { "tbl", 0, &monitor_get_tbl, },
2084 #if defined(TARGET_PPC64)
2085 /* Address space register */
2086 { "asr", offsetof(CPUState, asr) },
2087 #endif
2088 /* Segment registers */
2089 { "sdr1", offsetof(CPUState, sdr1) },
2090 { "sr0", offsetof(CPUState, sr[0]) },
2091 { "sr1", offsetof(CPUState, sr[1]) },
2092 { "sr2", offsetof(CPUState, sr[2]) },
2093 { "sr3", offsetof(CPUState, sr[3]) },
2094 { "sr4", offsetof(CPUState, sr[4]) },
2095 { "sr5", offsetof(CPUState, sr[5]) },
2096 { "sr6", offsetof(CPUState, sr[6]) },
2097 { "sr7", offsetof(CPUState, sr[7]) },
2098 { "sr8", offsetof(CPUState, sr[8]) },
2099 { "sr9", offsetof(CPUState, sr[9]) },
2100 { "sr10", offsetof(CPUState, sr[10]) },
2101 { "sr11", offsetof(CPUState, sr[11]) },
2102 { "sr12", offsetof(CPUState, sr[12]) },
2103 { "sr13", offsetof(CPUState, sr[13]) },
2104 { "sr14", offsetof(CPUState, sr[14]) },
2105 { "sr15", offsetof(CPUState, sr[15]) },
2106 /* Too lazy to put BATs and SPRs ... */
2107 #elif defined(TARGET_SPARC)
2108 { "g0", offsetof(CPUState, gregs[0]) },
2109 { "g1", offsetof(CPUState, gregs[1]) },
2110 { "g2", offsetof(CPUState, gregs[2]) },
2111 { "g3", offsetof(CPUState, gregs[3]) },
2112 { "g4", offsetof(CPUState, gregs[4]) },
2113 { "g5", offsetof(CPUState, gregs[5]) },
2114 { "g6", offsetof(CPUState, gregs[6]) },
2115 { "g7", offsetof(CPUState, gregs[7]) },
2116 { "o0", 0, monitor_get_reg },
2117 { "o1", 1, monitor_get_reg },
2118 { "o2", 2, monitor_get_reg },
2119 { "o3", 3, monitor_get_reg },
2120 { "o4", 4, monitor_get_reg },
2121 { "o5", 5, monitor_get_reg },
2122 { "o6", 6, monitor_get_reg },
2123 { "o7", 7, monitor_get_reg },
2124 { "l0", 8, monitor_get_reg },
2125 { "l1", 9, monitor_get_reg },
2126 { "l2", 10, monitor_get_reg },
2127 { "l3", 11, monitor_get_reg },
2128 { "l4", 12, monitor_get_reg },
2129 { "l5", 13, monitor_get_reg },
2130 { "l6", 14, monitor_get_reg },
2131 { "l7", 15, monitor_get_reg },
2132 { "i0", 16, monitor_get_reg },
2133 { "i1", 17, monitor_get_reg },
2134 { "i2", 18, monitor_get_reg },
2135 { "i3", 19, monitor_get_reg },
2136 { "i4", 20, monitor_get_reg },
2137 { "i5", 21, monitor_get_reg },
2138 { "i6", 22, monitor_get_reg },
2139 { "i7", 23, monitor_get_reg },
2140 { "pc", offsetof(CPUState, pc) },
2141 { "npc", offsetof(CPUState, npc) },
2142 { "y", offsetof(CPUState, y) },
2143 #ifndef TARGET_SPARC64
2144 { "psr", 0, &monitor_get_psr, },
2145 { "wim", offsetof(CPUState, wim) },
2146 #endif
2147 { "tbr", offsetof(CPUState, tbr) },
2148 { "fsr", offsetof(CPUState, fsr) },
2149 { "f0", offsetof(CPUState, fpr[0]) },
2150 { "f1", offsetof(CPUState, fpr[1]) },
2151 { "f2", offsetof(CPUState, fpr[2]) },
2152 { "f3", offsetof(CPUState, fpr[3]) },
2153 { "f4", offsetof(CPUState, fpr[4]) },
2154 { "f5", offsetof(CPUState, fpr[5]) },
2155 { "f6", offsetof(CPUState, fpr[6]) },
2156 { "f7", offsetof(CPUState, fpr[7]) },
2157 { "f8", offsetof(CPUState, fpr[8]) },
2158 { "f9", offsetof(CPUState, fpr[9]) },
2159 { "f10", offsetof(CPUState, fpr[10]) },
2160 { "f11", offsetof(CPUState, fpr[11]) },
2161 { "f12", offsetof(CPUState, fpr[12]) },
2162 { "f13", offsetof(CPUState, fpr[13]) },
2163 { "f14", offsetof(CPUState, fpr[14]) },
2164 { "f15", offsetof(CPUState, fpr[15]) },
2165 { "f16", offsetof(CPUState, fpr[16]) },
2166 { "f17", offsetof(CPUState, fpr[17]) },
2167 { "f18", offsetof(CPUState, fpr[18]) },
2168 { "f19", offsetof(CPUState, fpr[19]) },
2169 { "f20", offsetof(CPUState, fpr[20]) },
2170 { "f21", offsetof(CPUState, fpr[21]) },
2171 { "f22", offsetof(CPUState, fpr[22]) },
2172 { "f23", offsetof(CPUState, fpr[23]) },
2173 { "f24", offsetof(CPUState, fpr[24]) },
2174 { "f25", offsetof(CPUState, fpr[25]) },
2175 { "f26", offsetof(CPUState, fpr[26]) },
2176 { "f27", offsetof(CPUState, fpr[27]) },
2177 { "f28", offsetof(CPUState, fpr[28]) },
2178 { "f29", offsetof(CPUState, fpr[29]) },
2179 { "f30", offsetof(CPUState, fpr[30]) },
2180 { "f31", offsetof(CPUState, fpr[31]) },
2181 #ifdef TARGET_SPARC64
2182 { "f32", offsetof(CPUState, fpr[32]) },
2183 { "f34", offsetof(CPUState, fpr[34]) },
2184 { "f36", offsetof(CPUState, fpr[36]) },
2185 { "f38", offsetof(CPUState, fpr[38]) },
2186 { "f40", offsetof(CPUState, fpr[40]) },
2187 { "f42", offsetof(CPUState, fpr[42]) },
2188 { "f44", offsetof(CPUState, fpr[44]) },
2189 { "f46", offsetof(CPUState, fpr[46]) },
2190 { "f48", offsetof(CPUState, fpr[48]) },
2191 { "f50", offsetof(CPUState, fpr[50]) },
2192 { "f52", offsetof(CPUState, fpr[52]) },
2193 { "f54", offsetof(CPUState, fpr[54]) },
2194 { "f56", offsetof(CPUState, fpr[56]) },
2195 { "f58", offsetof(CPUState, fpr[58]) },
2196 { "f60", offsetof(CPUState, fpr[60]) },
2197 { "f62", offsetof(CPUState, fpr[62]) },
2198 { "asi", offsetof(CPUState, asi) },
2199 { "pstate", offsetof(CPUState, pstate) },
2200 { "cansave", offsetof(CPUState, cansave) },
2201 { "canrestore", offsetof(CPUState, canrestore) },
2202 { "otherwin", offsetof(CPUState, otherwin) },
2203 { "wstate", offsetof(CPUState, wstate) },
2204 { "cleanwin", offsetof(CPUState, cleanwin) },
2205 { "fprs", offsetof(CPUState, fprs) },
2206 #endif
2207 #endif
2208 { NULL },
2211 static void expr_error(Monitor *mon, const char *msg)
2213 monitor_printf(mon, "%s\n", msg);
2214 longjmp(expr_env, 1);
2217 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2218 static int get_monitor_def(target_long *pval, const char *name)
2220 const MonitorDef *md;
2221 void *ptr;
2223 for(md = monitor_defs; md->name != NULL; md++) {
2224 if (compare_cmd(name, md->name)) {
2225 if (md->get_value) {
2226 *pval = md->get_value(md, md->offset);
2227 } else {
2228 CPUState *env = mon_get_cpu();
2229 if (!env)
2230 return -2;
2231 ptr = (uint8_t *)env + md->offset;
2232 switch(md->type) {
2233 case MD_I32:
2234 *pval = *(int32_t *)ptr;
2235 break;
2236 case MD_TLONG:
2237 *pval = *(target_long *)ptr;
2238 break;
2239 default:
2240 *pval = 0;
2241 break;
2244 return 0;
2247 return -1;
2250 static void next(void)
2252 if (*pch != '\0') {
2253 pch++;
2254 while (qemu_isspace(*pch))
2255 pch++;
2259 static int64_t expr_sum(Monitor *mon);
2261 static int64_t expr_unary(Monitor *mon)
2263 int64_t n;
2264 char *p;
2265 int ret;
2267 switch(*pch) {
2268 case '+':
2269 next();
2270 n = expr_unary(mon);
2271 break;
2272 case '-':
2273 next();
2274 n = -expr_unary(mon);
2275 break;
2276 case '~':
2277 next();
2278 n = ~expr_unary(mon);
2279 break;
2280 case '(':
2281 next();
2282 n = expr_sum(mon);
2283 if (*pch != ')') {
2284 expr_error(mon, "')' expected");
2286 next();
2287 break;
2288 case '\'':
2289 pch++;
2290 if (*pch == '\0')
2291 expr_error(mon, "character constant expected");
2292 n = *pch;
2293 pch++;
2294 if (*pch != '\'')
2295 expr_error(mon, "missing terminating \' character");
2296 next();
2297 break;
2298 case '$':
2300 char buf[128], *q;
2301 target_long reg=0;
2303 pch++;
2304 q = buf;
2305 while ((*pch >= 'a' && *pch <= 'z') ||
2306 (*pch >= 'A' && *pch <= 'Z') ||
2307 (*pch >= '0' && *pch <= '9') ||
2308 *pch == '_' || *pch == '.') {
2309 if ((q - buf) < sizeof(buf) - 1)
2310 *q++ = *pch;
2311 pch++;
2313 while (qemu_isspace(*pch))
2314 pch++;
2315 *q = 0;
2316 ret = get_monitor_def(&reg, buf);
2317 if (ret == -1)
2318 expr_error(mon, "unknown register");
2319 else if (ret == -2)
2320 expr_error(mon, "no cpu defined");
2321 n = reg;
2323 break;
2324 case '\0':
2325 expr_error(mon, "unexpected end of expression");
2326 n = 0;
2327 break;
2328 default:
2329 #if TARGET_PHYS_ADDR_BITS > 32
2330 n = strtoull(pch, &p, 0);
2331 #else
2332 n = strtoul(pch, &p, 0);
2333 #endif
2334 if (pch == p) {
2335 expr_error(mon, "invalid char in expression");
2337 pch = p;
2338 while (qemu_isspace(*pch))
2339 pch++;
2340 break;
2342 return n;
2346 static int64_t expr_prod(Monitor *mon)
2348 int64_t val, val2;
2349 int op;
2351 val = expr_unary(mon);
2352 for(;;) {
2353 op = *pch;
2354 if (op != '*' && op != '/' && op != '%')
2355 break;
2356 next();
2357 val2 = expr_unary(mon);
2358 switch(op) {
2359 default:
2360 case '*':
2361 val *= val2;
2362 break;
2363 case '/':
2364 case '%':
2365 if (val2 == 0)
2366 expr_error(mon, "division by zero");
2367 if (op == '/')
2368 val /= val2;
2369 else
2370 val %= val2;
2371 break;
2374 return val;
2377 static int64_t expr_logic(Monitor *mon)
2379 int64_t val, val2;
2380 int op;
2382 val = expr_prod(mon);
2383 for(;;) {
2384 op = *pch;
2385 if (op != '&' && op != '|' && op != '^')
2386 break;
2387 next();
2388 val2 = expr_prod(mon);
2389 switch(op) {
2390 default:
2391 case '&':
2392 val &= val2;
2393 break;
2394 case '|':
2395 val |= val2;
2396 break;
2397 case '^':
2398 val ^= val2;
2399 break;
2402 return val;
2405 static int64_t expr_sum(Monitor *mon)
2407 int64_t val, val2;
2408 int op;
2410 val = expr_logic(mon);
2411 for(;;) {
2412 op = *pch;
2413 if (op != '+' && op != '-')
2414 break;
2415 next();
2416 val2 = expr_logic(mon);
2417 if (op == '+')
2418 val += val2;
2419 else
2420 val -= val2;
2422 return val;
2425 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2427 pch = *pp;
2428 if (setjmp(expr_env)) {
2429 *pp = pch;
2430 return -1;
2432 while (qemu_isspace(*pch))
2433 pch++;
2434 *pval = expr_sum(mon);
2435 *pp = pch;
2436 return 0;
2439 static int get_str(char *buf, int buf_size, const char **pp)
2441 const char *p;
2442 char *q;
2443 int c;
2445 q = buf;
2446 p = *pp;
2447 while (qemu_isspace(*p))
2448 p++;
2449 if (*p == '\0') {
2450 fail:
2451 *q = '\0';
2452 *pp = p;
2453 return -1;
2455 if (*p == '\"') {
2456 p++;
2457 while (*p != '\0' && *p != '\"') {
2458 if (*p == '\\') {
2459 p++;
2460 c = *p++;
2461 switch(c) {
2462 case 'n':
2463 c = '\n';
2464 break;
2465 case 'r':
2466 c = '\r';
2467 break;
2468 case '\\':
2469 case '\'':
2470 case '\"':
2471 break;
2472 default:
2473 qemu_printf("unsupported escape code: '\\%c'\n", c);
2474 goto fail;
2476 if ((q - buf) < buf_size - 1) {
2477 *q++ = c;
2479 } else {
2480 if ((q - buf) < buf_size - 1) {
2481 *q++ = *p;
2483 p++;
2486 if (*p != '\"') {
2487 qemu_printf("unterminated string\n");
2488 goto fail;
2490 p++;
2491 } else {
2492 while (*p != '\0' && !qemu_isspace(*p)) {
2493 if ((q - buf) < buf_size - 1) {
2494 *q++ = *p;
2496 p++;
2499 *q = '\0';
2500 *pp = p;
2501 return 0;
2505 * Store the command-name in cmdname, and return a pointer to
2506 * the remaining of the command string.
2508 static const char *get_command_name(const char *cmdline,
2509 char *cmdname, size_t nlen)
2511 size_t len;
2512 const char *p, *pstart;
2514 p = cmdline;
2515 while (qemu_isspace(*p))
2516 p++;
2517 if (*p == '\0')
2518 return NULL;
2519 pstart = p;
2520 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2521 p++;
2522 len = p - pstart;
2523 if (len > nlen - 1)
2524 len = nlen - 1;
2525 memcpy(cmdname, pstart, len);
2526 cmdname[len] = '\0';
2527 return p;
2530 static int default_fmt_format = 'x';
2531 static int default_fmt_size = 4;
2533 #define MAX_ARGS 16
2535 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2537 const char *p, *typestr;
2538 int c, nb_args, i, has_arg;
2539 const mon_cmd_t *cmd;
2540 char cmdname[256];
2541 char buf[1024];
2542 void *str_allocated[MAX_ARGS];
2543 void *args[MAX_ARGS];
2544 void (*handler_0)(Monitor *mon);
2545 void (*handler_1)(Monitor *mon, void *arg0);
2546 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2547 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2548 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2549 void *arg3);
2550 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2551 void *arg3, void *arg4);
2552 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2553 void *arg3, void *arg4, void *arg5);
2554 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2555 void *arg3, void *arg4, void *arg5, void *arg6);
2556 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2557 void *arg3, void *arg4, void *arg5, void *arg6,
2558 void *arg7);
2559 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2560 void *arg3, void *arg4, void *arg5, void *arg6,
2561 void *arg7, void *arg8);
2562 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2563 void *arg3, void *arg4, void *arg5, void *arg6,
2564 void *arg7, void *arg8, void *arg9);
2566 #ifdef DEBUG
2567 monitor_printf(mon, "command='%s'\n", cmdline);
2568 #endif
2570 /* extract the command name */
2571 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2572 if (!p)
2573 return;
2575 /* find the command */
2576 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2577 if (compare_cmd(cmdname, cmd->name))
2578 break;
2581 if (cmd->name == NULL) {
2582 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2583 return;
2586 for(i = 0; i < MAX_ARGS; i++)
2587 str_allocated[i] = NULL;
2589 /* parse the parameters */
2590 typestr = cmd->args_type;
2591 nb_args = 0;
2592 for(;;) {
2593 c = *typestr;
2594 if (c == '\0')
2595 break;
2596 typestr++;
2597 switch(c) {
2598 case 'F':
2599 case 'B':
2600 case 's':
2602 int ret;
2603 char *str;
2605 while (qemu_isspace(*p))
2606 p++;
2607 if (*typestr == '?') {
2608 typestr++;
2609 if (*p == '\0') {
2610 /* no optional string: NULL argument */
2611 str = NULL;
2612 goto add_str;
2615 ret = get_str(buf, sizeof(buf), &p);
2616 if (ret < 0) {
2617 switch(c) {
2618 case 'F':
2619 monitor_printf(mon, "%s: filename expected\n",
2620 cmdname);
2621 break;
2622 case 'B':
2623 monitor_printf(mon, "%s: block device name expected\n",
2624 cmdname);
2625 break;
2626 default:
2627 monitor_printf(mon, "%s: string expected\n", cmdname);
2628 break;
2630 goto fail;
2632 str = qemu_malloc(strlen(buf) + 1);
2633 pstrcpy(str, sizeof(buf), buf);
2634 str_allocated[nb_args] = str;
2635 add_str:
2636 if (nb_args >= MAX_ARGS) {
2637 error_args:
2638 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2639 goto fail;
2641 args[nb_args++] = str;
2643 break;
2644 case '/':
2646 int count, format, size;
2648 while (qemu_isspace(*p))
2649 p++;
2650 if (*p == '/') {
2651 /* format found */
2652 p++;
2653 count = 1;
2654 if (qemu_isdigit(*p)) {
2655 count = 0;
2656 while (qemu_isdigit(*p)) {
2657 count = count * 10 + (*p - '0');
2658 p++;
2661 size = -1;
2662 format = -1;
2663 for(;;) {
2664 switch(*p) {
2665 case 'o':
2666 case 'd':
2667 case 'u':
2668 case 'x':
2669 case 'i':
2670 case 'c':
2671 format = *p++;
2672 break;
2673 case 'b':
2674 size = 1;
2675 p++;
2676 break;
2677 case 'h':
2678 size = 2;
2679 p++;
2680 break;
2681 case 'w':
2682 size = 4;
2683 p++;
2684 break;
2685 case 'g':
2686 case 'L':
2687 size = 8;
2688 p++;
2689 break;
2690 default:
2691 goto next;
2694 next:
2695 if (*p != '\0' && !qemu_isspace(*p)) {
2696 monitor_printf(mon, "invalid char in format: '%c'\n",
2697 *p);
2698 goto fail;
2700 if (format < 0)
2701 format = default_fmt_format;
2702 if (format != 'i') {
2703 /* for 'i', not specifying a size gives -1 as size */
2704 if (size < 0)
2705 size = default_fmt_size;
2706 default_fmt_size = size;
2708 default_fmt_format = format;
2709 } else {
2710 count = 1;
2711 format = default_fmt_format;
2712 if (format != 'i') {
2713 size = default_fmt_size;
2714 } else {
2715 size = -1;
2718 if (nb_args + 3 > MAX_ARGS)
2719 goto error_args;
2720 args[nb_args++] = (void*)(long)count;
2721 args[nb_args++] = (void*)(long)format;
2722 args[nb_args++] = (void*)(long)size;
2724 break;
2725 case 'i':
2726 case 'l':
2728 int64_t val;
2730 while (qemu_isspace(*p))
2731 p++;
2732 if (*typestr == '?' || *typestr == '.') {
2733 if (*typestr == '?') {
2734 if (*p == '\0')
2735 has_arg = 0;
2736 else
2737 has_arg = 1;
2738 } else {
2739 if (*p == '.') {
2740 p++;
2741 while (qemu_isspace(*p))
2742 p++;
2743 has_arg = 1;
2744 } else {
2745 has_arg = 0;
2748 typestr++;
2749 if (nb_args >= MAX_ARGS)
2750 goto error_args;
2751 args[nb_args++] = (void *)(long)has_arg;
2752 if (!has_arg) {
2753 if (nb_args >= MAX_ARGS)
2754 goto error_args;
2755 val = -1;
2756 goto add_num;
2759 if (get_expr(mon, &val, &p))
2760 goto fail;
2761 add_num:
2762 if (c == 'i') {
2763 if (nb_args >= MAX_ARGS)
2764 goto error_args;
2765 args[nb_args++] = (void *)(long)val;
2766 } else {
2767 if ((nb_args + 1) >= MAX_ARGS)
2768 goto error_args;
2769 #if TARGET_PHYS_ADDR_BITS > 32
2770 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2771 #else
2772 args[nb_args++] = (void *)0;
2773 #endif
2774 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2777 break;
2778 case '-':
2780 int has_option;
2781 /* option */
2783 c = *typestr++;
2784 if (c == '\0')
2785 goto bad_type;
2786 while (qemu_isspace(*p))
2787 p++;
2788 has_option = 0;
2789 if (*p == '-') {
2790 p++;
2791 if (*p != c) {
2792 monitor_printf(mon, "%s: unsupported option -%c\n",
2793 cmdname, *p);
2794 goto fail;
2796 p++;
2797 has_option = 1;
2799 if (nb_args >= MAX_ARGS)
2800 goto error_args;
2801 args[nb_args++] = (void *)(long)has_option;
2803 break;
2804 default:
2805 bad_type:
2806 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2807 goto fail;
2810 /* check that all arguments were parsed */
2811 while (qemu_isspace(*p))
2812 p++;
2813 if (*p != '\0') {
2814 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2815 cmdname);
2816 goto fail;
2819 qemu_errors_to_mon(mon);
2820 switch(nb_args) {
2821 case 0:
2822 handler_0 = cmd->handler;
2823 handler_0(mon);
2824 break;
2825 case 1:
2826 handler_1 = cmd->handler;
2827 handler_1(mon, args[0]);
2828 break;
2829 case 2:
2830 handler_2 = cmd->handler;
2831 handler_2(mon, args[0], args[1]);
2832 break;
2833 case 3:
2834 handler_3 = cmd->handler;
2835 handler_3(mon, args[0], args[1], args[2]);
2836 break;
2837 case 4:
2838 handler_4 = cmd->handler;
2839 handler_4(mon, args[0], args[1], args[2], args[3]);
2840 break;
2841 case 5:
2842 handler_5 = cmd->handler;
2843 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2844 break;
2845 case 6:
2846 handler_6 = cmd->handler;
2847 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2848 break;
2849 case 7:
2850 handler_7 = cmd->handler;
2851 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2852 args[6]);
2853 break;
2854 case 8:
2855 handler_8 = cmd->handler;
2856 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2857 args[6], args[7]);
2858 break;
2859 case 9:
2860 handler_9 = cmd->handler;
2861 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2862 args[6], args[7], args[8]);
2863 break;
2864 case 10:
2865 handler_10 = cmd->handler;
2866 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2867 args[6], args[7], args[8], args[9]);
2868 break;
2869 default:
2870 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2871 break;
2873 qemu_errors_to_previous();
2875 fail:
2876 for(i = 0; i < MAX_ARGS; i++)
2877 qemu_free(str_allocated[i]);
2880 static void cmd_completion(const char *name, const char *list)
2882 const char *p, *pstart;
2883 char cmd[128];
2884 int len;
2886 p = list;
2887 for(;;) {
2888 pstart = p;
2889 p = strchr(p, '|');
2890 if (!p)
2891 p = pstart + strlen(pstart);
2892 len = p - pstart;
2893 if (len > sizeof(cmd) - 2)
2894 len = sizeof(cmd) - 2;
2895 memcpy(cmd, pstart, len);
2896 cmd[len] = '\0';
2897 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2898 readline_add_completion(cur_mon->rs, cmd);
2900 if (*p == '\0')
2901 break;
2902 p++;
2906 static void file_completion(const char *input)
2908 DIR *ffs;
2909 struct dirent *d;
2910 char path[1024];
2911 char file[1024], file_prefix[1024];
2912 int input_path_len;
2913 const char *p;
2915 p = strrchr(input, '/');
2916 if (!p) {
2917 input_path_len = 0;
2918 pstrcpy(file_prefix, sizeof(file_prefix), input);
2919 pstrcpy(path, sizeof(path), ".");
2920 } else {
2921 input_path_len = p - input + 1;
2922 memcpy(path, input, input_path_len);
2923 if (input_path_len > sizeof(path) - 1)
2924 input_path_len = sizeof(path) - 1;
2925 path[input_path_len] = '\0';
2926 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2928 #ifdef DEBUG_COMPLETION
2929 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2930 input, path, file_prefix);
2931 #endif
2932 ffs = opendir(path);
2933 if (!ffs)
2934 return;
2935 for(;;) {
2936 struct stat sb;
2937 d = readdir(ffs);
2938 if (!d)
2939 break;
2940 if (strstart(d->d_name, file_prefix, NULL)) {
2941 memcpy(file, input, input_path_len);
2942 if (input_path_len < sizeof(file))
2943 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2944 d->d_name);
2945 /* stat the file to find out if it's a directory.
2946 * In that case add a slash to speed up typing long paths
2948 stat(file, &sb);
2949 if(S_ISDIR(sb.st_mode))
2950 pstrcat(file, sizeof(file), "/");
2951 readline_add_completion(cur_mon->rs, file);
2954 closedir(ffs);
2957 static void block_completion_it(void *opaque, BlockDriverState *bs)
2959 const char *name = bdrv_get_device_name(bs);
2960 const char *input = opaque;
2962 if (input[0] == '\0' ||
2963 !strncmp(name, (char *)input, strlen(input))) {
2964 readline_add_completion(cur_mon->rs, name);
2968 /* NOTE: this parser is an approximate form of the real command parser */
2969 static void parse_cmdline(const char *cmdline,
2970 int *pnb_args, char **args)
2972 const char *p;
2973 int nb_args, ret;
2974 char buf[1024];
2976 p = cmdline;
2977 nb_args = 0;
2978 for(;;) {
2979 while (qemu_isspace(*p))
2980 p++;
2981 if (*p == '\0')
2982 break;
2983 if (nb_args >= MAX_ARGS)
2984 break;
2985 ret = get_str(buf, sizeof(buf), &p);
2986 args[nb_args] = qemu_strdup(buf);
2987 nb_args++;
2988 if (ret < 0)
2989 break;
2991 *pnb_args = nb_args;
2994 static void monitor_find_completion(const char *cmdline)
2996 const char *cmdname;
2997 char *args[MAX_ARGS];
2998 int nb_args, i, len;
2999 const char *ptype, *str;
3000 const mon_cmd_t *cmd;
3001 const KeyDef *key;
3003 parse_cmdline(cmdline, &nb_args, args);
3004 #ifdef DEBUG_COMPLETION
3005 for(i = 0; i < nb_args; i++) {
3006 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3008 #endif
3010 /* if the line ends with a space, it means we want to complete the
3011 next arg */
3012 len = strlen(cmdline);
3013 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3014 if (nb_args >= MAX_ARGS)
3015 return;
3016 args[nb_args++] = qemu_strdup("");
3018 if (nb_args <= 1) {
3019 /* command completion */
3020 if (nb_args == 0)
3021 cmdname = "";
3022 else
3023 cmdname = args[0];
3024 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3025 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3026 cmd_completion(cmdname, cmd->name);
3028 } else {
3029 /* find the command */
3030 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3031 if (compare_cmd(args[0], cmd->name))
3032 goto found;
3034 return;
3035 found:
3036 ptype = cmd->args_type;
3037 for(i = 0; i < nb_args - 2; i++) {
3038 if (*ptype != '\0') {
3039 ptype++;
3040 while (*ptype == '?')
3041 ptype++;
3044 str = args[nb_args - 1];
3045 if (*ptype == '-' && ptype[1] != '\0') {
3046 ptype += 2;
3048 switch(*ptype) {
3049 case 'F':
3050 /* file completion */
3051 readline_set_completion_index(cur_mon->rs, strlen(str));
3052 file_completion(str);
3053 break;
3054 case 'B':
3055 /* block device name completion */
3056 readline_set_completion_index(cur_mon->rs, strlen(str));
3057 bdrv_iterate(block_completion_it, (void *)str);
3058 break;
3059 case 's':
3060 /* XXX: more generic ? */
3061 if (!strcmp(cmd->name, "info")) {
3062 readline_set_completion_index(cur_mon->rs, strlen(str));
3063 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3064 cmd_completion(str, cmd->name);
3066 } else if (!strcmp(cmd->name, "sendkey")) {
3067 char *sep = strrchr(str, '-');
3068 if (sep)
3069 str = sep + 1;
3070 readline_set_completion_index(cur_mon->rs, strlen(str));
3071 for(key = key_defs; key->name != NULL; key++) {
3072 cmd_completion(str, key->name);
3074 } else if (!strcmp(cmd->name, "help|?")) {
3075 readline_set_completion_index(cur_mon->rs, strlen(str));
3076 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3077 cmd_completion(str, cmd->name);
3080 break;
3081 default:
3082 break;
3085 for(i = 0; i < nb_args; i++)
3086 qemu_free(args[i]);
3089 static int monitor_can_read(void *opaque)
3091 Monitor *mon = opaque;
3093 return (mon->suspend_cnt == 0) ? 128 : 0;
3096 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3098 Monitor *old_mon = cur_mon;
3099 int i;
3101 cur_mon = opaque;
3103 if (cur_mon->rs) {
3104 for (i = 0; i < size; i++)
3105 readline_handle_byte(cur_mon->rs, buf[i]);
3106 } else {
3107 if (size == 0 || buf[size - 1] != 0)
3108 monitor_printf(cur_mon, "corrupted command\n");
3109 else
3110 monitor_handle_command(cur_mon, (char *)buf);
3113 cur_mon = old_mon;
3116 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3118 monitor_suspend(mon);
3119 monitor_handle_command(mon, cmdline);
3120 monitor_resume(mon);
3123 int monitor_suspend(Monitor *mon)
3125 if (!mon->rs)
3126 return -ENOTTY;
3127 mon->suspend_cnt++;
3128 return 0;
3131 void monitor_resume(Monitor *mon)
3133 if (!mon->rs)
3134 return;
3135 if (--mon->suspend_cnt == 0)
3136 readline_show_prompt(mon->rs);
3139 static void monitor_event(void *opaque, int event)
3141 Monitor *mon = opaque;
3143 switch (event) {
3144 case CHR_EVENT_MUX_IN:
3145 readline_restart(mon->rs);
3146 monitor_resume(mon);
3147 monitor_flush(mon);
3148 break;
3150 case CHR_EVENT_MUX_OUT:
3151 if (mon->suspend_cnt == 0)
3152 monitor_printf(mon, "\n");
3153 monitor_flush(mon);
3154 monitor_suspend(mon);
3155 break;
3157 case CHR_EVENT_RESET:
3158 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3159 "information\n", QEMU_VERSION);
3160 if (mon->chr->focus == 0)
3161 readline_show_prompt(mon->rs);
3162 break;
3168 * Local variables:
3169 * c-indent-level: 4
3170 * c-basic-offset: 4
3171 * tab-width: 8
3172 * End:
3175 void monitor_init(CharDriverState *chr, int flags)
3177 static int is_first_init = 1;
3178 Monitor *mon;
3180 if (is_first_init) {
3181 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3182 is_first_init = 0;
3185 mon = qemu_mallocz(sizeof(*mon));
3187 mon->chr = chr;
3188 mon->flags = flags;
3189 if (mon->chr->focus != 0)
3190 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3191 if (flags & MONITOR_USE_READLINE) {
3192 mon->rs = readline_init(mon, monitor_find_completion);
3193 monitor_read_command(mon, 0);
3196 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3197 mon);
3199 LIST_INSERT_HEAD(&mon_list, mon, entry);
3200 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3201 cur_mon = mon;
3204 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3206 BlockDriverState *bs = opaque;
3207 int ret = 0;
3209 if (bdrv_set_key(bs, password) != 0) {
3210 monitor_printf(mon, "invalid password\n");
3211 ret = -EPERM;
3213 if (mon->password_completion_cb)
3214 mon->password_completion_cb(mon->password_opaque, ret);
3216 monitor_read_command(mon, 1);
3219 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3220 BlockDriverCompletionFunc *completion_cb,
3221 void *opaque)
3223 int err;
3225 if (!bdrv_key_required(bs)) {
3226 if (completion_cb)
3227 completion_cb(opaque, 0);
3228 return;
3231 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3232 bdrv_get_encrypted_filename(bs));
3234 mon->password_completion_cb = completion_cb;
3235 mon->password_opaque = opaque;
3237 err = monitor_read_password(mon, bdrv_password_cb, bs);
3239 if (err && completion_cb)
3240 completion_cb(opaque, err);
3243 typedef struct QemuErrorSink QemuErrorSink;
3244 struct QemuErrorSink {
3245 enum {
3246 ERR_SINK_FILE,
3247 ERR_SINK_MONITOR,
3248 } dest;
3249 union {
3250 FILE *fp;
3251 Monitor *mon;
3253 QemuErrorSink *previous;
3256 static QemuErrorSink *qemu_error_sink;
3258 void qemu_errors_to_file(FILE *fp)
3260 QemuErrorSink *sink;
3262 sink = qemu_mallocz(sizeof(*sink));
3263 sink->dest = ERR_SINK_FILE;
3264 sink->fp = fp;
3265 sink->previous = qemu_error_sink;
3266 qemu_error_sink = sink;
3269 void qemu_errors_to_mon(Monitor *mon)
3271 QemuErrorSink *sink;
3273 sink = qemu_mallocz(sizeof(*sink));
3274 sink->dest = ERR_SINK_MONITOR;
3275 sink->mon = mon;
3276 sink->previous = qemu_error_sink;
3277 qemu_error_sink = sink;
3280 void qemu_errors_to_previous(void)
3282 QemuErrorSink *sink;
3284 assert(qemu_error_sink != NULL);
3285 sink = qemu_error_sink;
3286 qemu_error_sink = sink->previous;
3287 qemu_free(sink);
3290 void qemu_error(const char *fmt, ...)
3292 va_list args;
3294 assert(qemu_error_sink != NULL);
3295 switch (qemu_error_sink->dest) {
3296 case ERR_SINK_FILE:
3297 va_start(args, fmt);
3298 vfprintf(qemu_error_sink->fp, fmt, args);
3299 va_end(args);
3300 break;
3301 case ERR_SINK_MONITOR:
3302 va_start(args, fmt);
3303 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3304 va_end(args);
3305 break;