kvm userspace: ksm support
[qemu-kvm/fedora.git] / monitor.c
blob61e0ed3fb71751662b5bc6aba55e85cefa9a5342
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 i, all_devices;
261 all_devices = !strcmp(device, "all");
262 for (i = 0; i < nb_drives; i++) {
263 if (!all_devices)
264 if (strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
265 continue;
266 bdrv_commit(drives_table[i].bdrv);
270 static void do_info(Monitor *mon, const char *item)
272 const mon_cmd_t *cmd;
273 void (*handler)(Monitor *);
275 if (!item)
276 goto help;
277 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
278 if (compare_cmd(item, cmd->name))
279 goto found;
281 help:
282 help_cmd(mon, "info");
283 return;
284 found:
285 handler = cmd->handler;
286 handler(mon);
289 static void do_info_version(Monitor *mon)
291 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
294 static void do_info_name(Monitor *mon)
296 if (qemu_name)
297 monitor_printf(mon, "%s\n", qemu_name);
300 #if defined(TARGET_I386)
301 static void do_info_hpet(Monitor *mon)
303 monitor_printf(mon, "HPET is %s by QEMU\n",
304 (no_hpet) ? "disabled" : "enabled");
306 #endif
308 static void do_info_uuid(Monitor *mon)
310 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
311 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
312 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
313 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
314 qemu_uuid[14], qemu_uuid[15]);
317 /* get the current CPU defined by the user */
318 static int mon_set_cpu(int cpu_index)
320 CPUState *env;
322 for(env = first_cpu; env != NULL; env = env->next_cpu) {
323 if (env->cpu_index == cpu_index) {
324 cur_mon->mon_cpu = env;
325 return 0;
328 return -1;
331 static CPUState *mon_get_cpu(void)
333 if (!cur_mon->mon_cpu) {
334 mon_set_cpu(0);
336 cpu_synchronize_state(cur_mon->mon_cpu, 0);
337 return cur_mon->mon_cpu;
340 static void do_info_registers(Monitor *mon)
342 CPUState *env;
343 env = mon_get_cpu();
344 if (!env)
345 return;
346 #ifdef TARGET_I386
347 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
348 X86_DUMP_FPU);
349 #else
350 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
352 #endif
355 static void do_info_cpus(Monitor *mon)
357 CPUState *env;
359 /* just to set the default cpu if not already done */
360 mon_get_cpu();
362 for(env = first_cpu; env != NULL; env = env->next_cpu) {
363 cpu_synchronize_state(env, 0);
364 monitor_printf(mon, "%c CPU #%d:",
365 (env == mon->mon_cpu) ? '*' : ' ',
366 env->cpu_index);
367 #if defined(TARGET_I386)
368 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
369 env->eip + env->segs[R_CS].base);
370 #elif defined(TARGET_PPC)
371 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
372 #elif defined(TARGET_SPARC)
373 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
374 env->pc, env->npc);
375 #elif defined(TARGET_MIPS)
376 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
377 #endif
378 if (env->halted)
379 monitor_printf(mon, " (halted)");
380 monitor_printf(mon," thread_id=%d", env->thread_id);
381 monitor_printf(mon, "\n");
385 static void do_cpu_set(Monitor *mon, int index)
387 if (mon_set_cpu(index) < 0)
388 monitor_printf(mon, "Invalid CPU index\n");
391 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
393 int state;
395 if (!strcmp(status, "online"))
396 state = 1;
397 else if (!strcmp(status, "offline"))
398 state = 0;
399 else {
400 monitor_printf(mon, "invalid status: %s\n", status);
401 return;
403 #if defined(TARGET_I386) || defined(TARGET_X86_64)
404 qemu_system_cpu_hot_add(value, state);
405 #endif
408 static void do_info_jit(Monitor *mon)
410 dump_exec_info((FILE *)mon, monitor_fprintf);
413 static void do_info_history(Monitor *mon)
415 int i;
416 const char *str;
418 if (!mon->rs)
419 return;
420 i = 0;
421 for(;;) {
422 str = readline_get_history(mon->rs, i);
423 if (!str)
424 break;
425 monitor_printf(mon, "%d: '%s'\n", i, str);
426 i++;
430 #if defined(TARGET_PPC)
431 /* XXX: not implemented in other targets */
432 static void do_info_cpu_stats(Monitor *mon)
434 CPUState *env;
436 env = mon_get_cpu();
437 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
439 #endif
441 static void do_quit(Monitor *mon)
443 exit(0);
446 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
448 if (bdrv_is_inserted(bs)) {
449 if (!force) {
450 if (!bdrv_is_removable(bs)) {
451 monitor_printf(mon, "device is not removable\n");
452 return -1;
454 if (bdrv_is_locked(bs)) {
455 monitor_printf(mon, "device is locked\n");
456 return -1;
459 bdrv_close(bs);
461 return 0;
464 static void do_eject(Monitor *mon, int force, const char *filename)
466 BlockDriverState *bs;
468 bs = bdrv_find(filename);
469 if (!bs) {
470 monitor_printf(mon, "device not found\n");
471 return;
473 eject_device(mon, bs, force);
476 static void do_change_block(Monitor *mon, const char *device,
477 const char *filename, const char *fmt)
479 BlockDriverState *bs;
480 BlockDriver *drv = NULL;
482 bs = bdrv_find(device);
483 if (!bs) {
484 monitor_printf(mon, "device not found\n");
485 return;
487 if (fmt) {
488 drv = bdrv_find_format(fmt);
489 if (!drv) {
490 monitor_printf(mon, "invalid format %s\n", fmt);
491 return;
494 if (eject_device(mon, bs, 0) < 0)
495 return;
496 bdrv_open2(bs, filename, 0, drv);
497 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
500 static void change_vnc_password_cb(Monitor *mon, const char *password,
501 void *opaque)
503 if (vnc_display_password(NULL, password) < 0)
504 monitor_printf(mon, "could not set VNC server password\n");
506 monitor_read_command(mon, 1);
509 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
511 if (strcmp(target, "passwd") == 0 ||
512 strcmp(target, "password") == 0) {
513 if (arg) {
514 char password[9];
515 strncpy(password, arg, sizeof(password));
516 password[sizeof(password) - 1] = '\0';
517 change_vnc_password_cb(mon, password, NULL);
518 } else {
519 monitor_read_password(mon, change_vnc_password_cb, NULL);
521 } else {
522 if (vnc_display_open(NULL, target) < 0)
523 monitor_printf(mon, "could not start VNC server on %s\n", target);
527 static void do_change(Monitor *mon, const char *device, const char *target,
528 const char *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 char *filename)
539 vga_hw_screen_dump(filename);
542 static void do_logfile(Monitor *mon, const char *filename)
544 cpu_set_log_filename(filename);
547 static void do_log(Monitor *mon, const char *items)
549 int mask;
551 if (!strcmp(items, "none")) {
552 mask = 0;
553 } else {
554 mask = cpu_str_to_log_mask(items);
555 if (!mask) {
556 help_cmd(mon, "log");
557 return;
560 cpu_set_log(mask);
563 static void do_singlestep(Monitor *mon, const char *option)
565 if (!option || !strcmp(option, "on")) {
566 singlestep = 1;
567 } else if (!strcmp(option, "off")) {
568 singlestep = 0;
569 } else {
570 monitor_printf(mon, "unexpected option %s\n", option);
574 static void do_stop(Monitor *mon)
576 vm_stop(EXCP_INTERRUPT);
579 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
581 struct bdrv_iterate_context {
582 Monitor *mon;
583 int err;
586 static void do_cont(Monitor *mon)
588 struct bdrv_iterate_context context = { mon, 0 };
590 bdrv_iterate(encrypted_bdrv_it, &context);
591 /* only resume the vm if all keys are set and valid */
592 if (!context.err)
593 vm_start();
596 static void bdrv_key_cb(void *opaque, int err)
598 Monitor *mon = opaque;
600 /* another key was set successfully, retry to continue */
601 if (!err)
602 do_cont(mon);
605 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
607 struct bdrv_iterate_context *context = opaque;
609 if (!context->err && bdrv_key_required(bs)) {
610 context->err = -EBUSY;
611 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
612 context->mon);
616 static void do_gdbserver(Monitor *mon, const char *device)
618 if (!device)
619 device = "tcp::" DEFAULT_GDBSTUB_PORT;
620 if (gdbserver_start(device) < 0) {
621 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
622 device);
623 } else if (strcmp(device, "none") == 0) {
624 monitor_printf(mon, "Disabled gdbserver\n");
625 } else {
626 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
627 device);
631 static void do_watchdog_action(Monitor *mon, const char *action)
633 if (select_watchdog_action(action) == -1) {
634 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
638 static void monitor_printc(Monitor *mon, int c)
640 monitor_printf(mon, "'");
641 switch(c) {
642 case '\'':
643 monitor_printf(mon, "\\'");
644 break;
645 case '\\':
646 monitor_printf(mon, "\\\\");
647 break;
648 case '\n':
649 monitor_printf(mon, "\\n");
650 break;
651 case '\r':
652 monitor_printf(mon, "\\r");
653 break;
654 default:
655 if (c >= 32 && c <= 126) {
656 monitor_printf(mon, "%c", c);
657 } else {
658 monitor_printf(mon, "\\x%02x", c);
660 break;
662 monitor_printf(mon, "'");
665 static void memory_dump(Monitor *mon, int count, int format, int wsize,
666 target_phys_addr_t addr, int is_physical)
668 CPUState *env;
669 int nb_per_line, l, line_size, i, max_digits, len;
670 uint8_t buf[16];
671 uint64_t v;
673 if (format == 'i') {
674 int flags;
675 flags = 0;
676 env = mon_get_cpu();
677 if (!env && !is_physical)
678 return;
679 #ifdef TARGET_I386
680 if (wsize == 2) {
681 flags = 1;
682 } else if (wsize == 4) {
683 flags = 0;
684 } else {
685 /* as default we use the current CS size */
686 flags = 0;
687 if (env) {
688 #ifdef TARGET_X86_64
689 if ((env->efer & MSR_EFER_LMA) &&
690 (env->segs[R_CS].flags & DESC_L_MASK))
691 flags = 2;
692 else
693 #endif
694 if (!(env->segs[R_CS].flags & DESC_B_MASK))
695 flags = 1;
698 #endif
699 monitor_disas(mon, env, addr, count, is_physical, flags);
700 return;
703 len = wsize * count;
704 if (wsize == 1)
705 line_size = 8;
706 else
707 line_size = 16;
708 nb_per_line = line_size / wsize;
709 max_digits = 0;
711 switch(format) {
712 case 'o':
713 max_digits = (wsize * 8 + 2) / 3;
714 break;
715 default:
716 case 'x':
717 max_digits = (wsize * 8) / 4;
718 break;
719 case 'u':
720 case 'd':
721 max_digits = (wsize * 8 * 10 + 32) / 33;
722 break;
723 case 'c':
724 wsize = 1;
725 break;
728 while (len > 0) {
729 if (is_physical)
730 monitor_printf(mon, TARGET_FMT_plx ":", addr);
731 else
732 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
733 l = len;
734 if (l > line_size)
735 l = line_size;
736 if (is_physical) {
737 cpu_physical_memory_rw(addr, buf, l, 0);
738 } else {
739 env = mon_get_cpu();
740 if (!env)
741 break;
742 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
743 monitor_printf(mon, " Cannot access memory\n");
744 break;
747 i = 0;
748 while (i < l) {
749 switch(wsize) {
750 default:
751 case 1:
752 v = ldub_raw(buf + i);
753 break;
754 case 2:
755 v = lduw_raw(buf + i);
756 break;
757 case 4:
758 v = (uint32_t)ldl_raw(buf + i);
759 break;
760 case 8:
761 v = ldq_raw(buf + i);
762 break;
764 monitor_printf(mon, " ");
765 switch(format) {
766 case 'o':
767 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
768 break;
769 case 'x':
770 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
771 break;
772 case 'u':
773 monitor_printf(mon, "%*" PRIu64, max_digits, v);
774 break;
775 case 'd':
776 monitor_printf(mon, "%*" PRId64, max_digits, v);
777 break;
778 case 'c':
779 monitor_printc(mon, v);
780 break;
782 i += wsize;
784 monitor_printf(mon, "\n");
785 addr += l;
786 len -= l;
790 #if TARGET_LONG_BITS == 64
791 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
792 #else
793 #define GET_TLONG(h, l) (l)
794 #endif
796 static void do_memory_dump(Monitor *mon, int count, int format, int size,
797 uint32_t addrh, uint32_t addrl)
799 target_long addr = GET_TLONG(addrh, addrl);
800 memory_dump(mon, count, format, size, addr, 0);
803 #if TARGET_PHYS_ADDR_BITS > 32
804 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
805 #else
806 #define GET_TPHYSADDR(h, l) (l)
807 #endif
809 static void do_physical_memory_dump(Monitor *mon, int count, int format,
810 int size, uint32_t addrh, uint32_t addrl)
813 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
814 memory_dump(mon, count, format, size, addr, 1);
817 static void do_print(Monitor *mon, int count, int format, int size,
818 unsigned int valh, unsigned int vall)
820 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
821 #if TARGET_PHYS_ADDR_BITS == 32
822 switch(format) {
823 case 'o':
824 monitor_printf(mon, "%#o", val);
825 break;
826 case 'x':
827 monitor_printf(mon, "%#x", val);
828 break;
829 case 'u':
830 monitor_printf(mon, "%u", val);
831 break;
832 default:
833 case 'd':
834 monitor_printf(mon, "%d", val);
835 break;
836 case 'c':
837 monitor_printc(mon, val);
838 break;
840 #else
841 switch(format) {
842 case 'o':
843 monitor_printf(mon, "%#" PRIo64, val);
844 break;
845 case 'x':
846 monitor_printf(mon, "%#" PRIx64, val);
847 break;
848 case 'u':
849 monitor_printf(mon, "%" PRIu64, val);
850 break;
851 default:
852 case 'd':
853 monitor_printf(mon, "%" PRId64, val);
854 break;
855 case 'c':
856 monitor_printc(mon, val);
857 break;
859 #endif
860 monitor_printf(mon, "\n");
863 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
864 uint32_t size, const char *filename)
866 FILE *f;
867 target_long addr = GET_TLONG(valh, vall);
868 uint32_t l;
869 CPUState *env;
870 uint8_t buf[1024];
872 env = mon_get_cpu();
873 if (!env)
874 return;
876 f = fopen(filename, "wb");
877 if (!f) {
878 monitor_printf(mon, "could not open '%s'\n", filename);
879 return;
881 while (size != 0) {
882 l = sizeof(buf);
883 if (l > size)
884 l = size;
885 cpu_memory_rw_debug(env, addr, buf, l, 0);
886 fwrite(buf, 1, l, f);
887 addr += l;
888 size -= l;
890 fclose(f);
893 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
894 unsigned int vall, uint32_t size,
895 const char *filename)
897 FILE *f;
898 uint32_t l;
899 uint8_t buf[1024];
900 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
902 f = fopen(filename, "wb");
903 if (!f) {
904 monitor_printf(mon, "could not open '%s'\n", filename);
905 return;
907 while (size != 0) {
908 l = sizeof(buf);
909 if (l > size)
910 l = size;
911 cpu_physical_memory_rw(addr, buf, l, 0);
912 fwrite(buf, 1, l, f);
913 fflush(f);
914 addr += l;
915 size -= l;
917 fclose(f);
920 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
922 uint32_t addr;
923 uint8_t buf[1];
924 uint16_t sum;
926 sum = 0;
927 for(addr = start; addr < (start + size); addr++) {
928 cpu_physical_memory_rw(addr, buf, 1, 0);
929 /* BSD sum algorithm ('sum' Unix command) */
930 sum = (sum >> 1) | (sum << 15);
931 sum += buf[0];
933 monitor_printf(mon, "%05d\n", sum);
936 typedef struct {
937 int keycode;
938 const char *name;
939 } KeyDef;
941 static const KeyDef key_defs[] = {
942 { 0x2a, "shift" },
943 { 0x36, "shift_r" },
945 { 0x38, "alt" },
946 { 0xb8, "alt_r" },
947 { 0x64, "altgr" },
948 { 0xe4, "altgr_r" },
949 { 0x1d, "ctrl" },
950 { 0x9d, "ctrl_r" },
952 { 0xdd, "menu" },
954 { 0x01, "esc" },
956 { 0x02, "1" },
957 { 0x03, "2" },
958 { 0x04, "3" },
959 { 0x05, "4" },
960 { 0x06, "5" },
961 { 0x07, "6" },
962 { 0x08, "7" },
963 { 0x09, "8" },
964 { 0x0a, "9" },
965 { 0x0b, "0" },
966 { 0x0c, "minus" },
967 { 0x0d, "equal" },
968 { 0x0e, "backspace" },
970 { 0x0f, "tab" },
971 { 0x10, "q" },
972 { 0x11, "w" },
973 { 0x12, "e" },
974 { 0x13, "r" },
975 { 0x14, "t" },
976 { 0x15, "y" },
977 { 0x16, "u" },
978 { 0x17, "i" },
979 { 0x18, "o" },
980 { 0x19, "p" },
982 { 0x1c, "ret" },
984 { 0x1e, "a" },
985 { 0x1f, "s" },
986 { 0x20, "d" },
987 { 0x21, "f" },
988 { 0x22, "g" },
989 { 0x23, "h" },
990 { 0x24, "j" },
991 { 0x25, "k" },
992 { 0x26, "l" },
994 { 0x2c, "z" },
995 { 0x2d, "x" },
996 { 0x2e, "c" },
997 { 0x2f, "v" },
998 { 0x30, "b" },
999 { 0x31, "n" },
1000 { 0x32, "m" },
1001 { 0x33, "comma" },
1002 { 0x34, "dot" },
1003 { 0x35, "slash" },
1005 { 0x37, "asterisk" },
1007 { 0x39, "spc" },
1008 { 0x3a, "caps_lock" },
1009 { 0x3b, "f1" },
1010 { 0x3c, "f2" },
1011 { 0x3d, "f3" },
1012 { 0x3e, "f4" },
1013 { 0x3f, "f5" },
1014 { 0x40, "f6" },
1015 { 0x41, "f7" },
1016 { 0x42, "f8" },
1017 { 0x43, "f9" },
1018 { 0x44, "f10" },
1019 { 0x45, "num_lock" },
1020 { 0x46, "scroll_lock" },
1022 { 0xb5, "kp_divide" },
1023 { 0x37, "kp_multiply" },
1024 { 0x4a, "kp_subtract" },
1025 { 0x4e, "kp_add" },
1026 { 0x9c, "kp_enter" },
1027 { 0x53, "kp_decimal" },
1028 { 0x54, "sysrq" },
1030 { 0x52, "kp_0" },
1031 { 0x4f, "kp_1" },
1032 { 0x50, "kp_2" },
1033 { 0x51, "kp_3" },
1034 { 0x4b, "kp_4" },
1035 { 0x4c, "kp_5" },
1036 { 0x4d, "kp_6" },
1037 { 0x47, "kp_7" },
1038 { 0x48, "kp_8" },
1039 { 0x49, "kp_9" },
1041 { 0x56, "<" },
1043 { 0x57, "f11" },
1044 { 0x58, "f12" },
1046 { 0xb7, "print" },
1048 { 0xc7, "home" },
1049 { 0xc9, "pgup" },
1050 { 0xd1, "pgdn" },
1051 { 0xcf, "end" },
1053 { 0xcb, "left" },
1054 { 0xc8, "up" },
1055 { 0xd0, "down" },
1056 { 0xcd, "right" },
1058 { 0xd2, "insert" },
1059 { 0xd3, "delete" },
1060 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1061 { 0xf0, "stop" },
1062 { 0xf1, "again" },
1063 { 0xf2, "props" },
1064 { 0xf3, "undo" },
1065 { 0xf4, "front" },
1066 { 0xf5, "copy" },
1067 { 0xf6, "open" },
1068 { 0xf7, "paste" },
1069 { 0xf8, "find" },
1070 { 0xf9, "cut" },
1071 { 0xfa, "lf" },
1072 { 0xfb, "help" },
1073 { 0xfc, "meta_l" },
1074 { 0xfd, "meta_r" },
1075 { 0xfe, "compose" },
1076 #endif
1077 { 0, NULL },
1080 static int get_keycode(const char *key)
1082 const KeyDef *p;
1083 char *endp;
1084 int ret;
1086 for(p = key_defs; p->name != NULL; p++) {
1087 if (!strcmp(key, p->name))
1088 return p->keycode;
1090 if (strstart(key, "0x", NULL)) {
1091 ret = strtoul(key, &endp, 0);
1092 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1093 return ret;
1095 return -1;
1098 #define MAX_KEYCODES 16
1099 static uint8_t keycodes[MAX_KEYCODES];
1100 static int nb_pending_keycodes;
1101 static QEMUTimer *key_timer;
1103 static void release_keys(void *opaque)
1105 int keycode;
1107 while (nb_pending_keycodes > 0) {
1108 nb_pending_keycodes--;
1109 keycode = keycodes[nb_pending_keycodes];
1110 if (keycode & 0x80)
1111 kbd_put_keycode(0xe0);
1112 kbd_put_keycode(keycode | 0x80);
1116 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1117 int hold_time)
1119 char keyname_buf[16];
1120 char *separator;
1121 int keyname_len, keycode, i;
1123 if (nb_pending_keycodes > 0) {
1124 qemu_del_timer(key_timer);
1125 release_keys(NULL);
1127 if (!has_hold_time)
1128 hold_time = 100;
1129 i = 0;
1130 while (1) {
1131 separator = strchr(string, '-');
1132 keyname_len = separator ? separator - string : strlen(string);
1133 if (keyname_len > 0) {
1134 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1135 if (keyname_len > sizeof(keyname_buf) - 1) {
1136 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1137 return;
1139 if (i == MAX_KEYCODES) {
1140 monitor_printf(mon, "too many keys\n");
1141 return;
1143 keyname_buf[keyname_len] = 0;
1144 keycode = get_keycode(keyname_buf);
1145 if (keycode < 0) {
1146 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1147 return;
1149 keycodes[i++] = keycode;
1151 if (!separator)
1152 break;
1153 string = separator + 1;
1155 nb_pending_keycodes = i;
1156 /* key down events */
1157 for (i = 0; i < nb_pending_keycodes; i++) {
1158 keycode = keycodes[i];
1159 if (keycode & 0x80)
1160 kbd_put_keycode(0xe0);
1161 kbd_put_keycode(keycode & 0x7f);
1163 /* delayed key up events */
1164 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1165 muldiv64(ticks_per_sec, hold_time, 1000));
1168 static int mouse_button_state;
1170 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1171 const char *dz_str)
1173 int dx, dy, dz;
1174 dx = strtol(dx_str, NULL, 0);
1175 dy = strtol(dy_str, NULL, 0);
1176 dz = 0;
1177 if (dz_str)
1178 dz = strtol(dz_str, NULL, 0);
1179 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1182 static void do_mouse_button(Monitor *mon, int button_state)
1184 mouse_button_state = button_state;
1185 kbd_mouse_event(0, 0, 0, mouse_button_state);
1188 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1189 int addr, int has_index, int index)
1191 uint32_t val;
1192 int suffix;
1194 if (has_index) {
1195 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1196 addr++;
1198 addr &= 0xffff;
1200 switch(size) {
1201 default:
1202 case 1:
1203 val = cpu_inb(NULL, addr);
1204 suffix = 'b';
1205 break;
1206 case 2:
1207 val = cpu_inw(NULL, addr);
1208 suffix = 'w';
1209 break;
1210 case 4:
1211 val = cpu_inl(NULL, addr);
1212 suffix = 'l';
1213 break;
1215 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1216 suffix, addr, size * 2, val);
1219 static void do_ioport_write(Monitor *mon, int count, int format, int size,
1220 int addr, int val)
1222 addr &= IOPORTS_MASK;
1224 switch (size) {
1225 default:
1226 case 1:
1227 cpu_outb(NULL, addr, val);
1228 break;
1229 case 2:
1230 cpu_outw(NULL, addr, val);
1231 break;
1232 case 4:
1233 cpu_outl(NULL, addr, val);
1234 break;
1238 static void do_boot_set(Monitor *mon, const char *bootdevice)
1240 int res;
1242 res = qemu_boot_set(bootdevice);
1243 if (res == 0) {
1244 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1245 } else if (res > 0) {
1246 monitor_printf(mon, "setting boot device list failed\n");
1247 } else {
1248 monitor_printf(mon, "no function defined to set boot device list for "
1249 "this architecture\n");
1253 static void do_system_reset(Monitor *mon)
1255 qemu_system_reset_request();
1258 static void do_system_powerdown(Monitor *mon)
1260 qemu_system_powerdown_request();
1263 #if defined(TARGET_I386)
1264 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1266 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1267 addr,
1268 pte & mask,
1269 pte & PG_GLOBAL_MASK ? 'G' : '-',
1270 pte & PG_PSE_MASK ? 'P' : '-',
1271 pte & PG_DIRTY_MASK ? 'D' : '-',
1272 pte & PG_ACCESSED_MASK ? 'A' : '-',
1273 pte & PG_PCD_MASK ? 'C' : '-',
1274 pte & PG_PWT_MASK ? 'T' : '-',
1275 pte & PG_USER_MASK ? 'U' : '-',
1276 pte & PG_RW_MASK ? 'W' : '-');
1279 static void tlb_info(Monitor *mon)
1281 CPUState *env;
1282 int l1, l2;
1283 uint32_t pgd, pde, pte;
1285 env = mon_get_cpu();
1286 if (!env)
1287 return;
1289 if (!(env->cr[0] & CR0_PG_MASK)) {
1290 monitor_printf(mon, "PG disabled\n");
1291 return;
1293 pgd = env->cr[3] & ~0xfff;
1294 for(l1 = 0; l1 < 1024; l1++) {
1295 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1296 pde = le32_to_cpu(pde);
1297 if (pde & PG_PRESENT_MASK) {
1298 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1299 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1300 } else {
1301 for(l2 = 0; l2 < 1024; l2++) {
1302 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1303 (uint8_t *)&pte, 4);
1304 pte = le32_to_cpu(pte);
1305 if (pte & PG_PRESENT_MASK) {
1306 print_pte(mon, (l1 << 22) + (l2 << 12),
1307 pte & ~PG_PSE_MASK,
1308 ~0xfff);
1316 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1317 uint32_t end, int prot)
1319 int prot1;
1320 prot1 = *plast_prot;
1321 if (prot != prot1) {
1322 if (*pstart != -1) {
1323 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1324 *pstart, end, end - *pstart,
1325 prot1 & PG_USER_MASK ? 'u' : '-',
1326 'r',
1327 prot1 & PG_RW_MASK ? 'w' : '-');
1329 if (prot != 0)
1330 *pstart = end;
1331 else
1332 *pstart = -1;
1333 *plast_prot = prot;
1337 static void mem_info(Monitor *mon)
1339 CPUState *env;
1340 int l1, l2, prot, last_prot;
1341 uint32_t pgd, pde, pte, start, end;
1343 env = mon_get_cpu();
1344 if (!env)
1345 return;
1347 if (!(env->cr[0] & CR0_PG_MASK)) {
1348 monitor_printf(mon, "PG disabled\n");
1349 return;
1351 pgd = env->cr[3] & ~0xfff;
1352 last_prot = 0;
1353 start = -1;
1354 for(l1 = 0; l1 < 1024; l1++) {
1355 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1356 pde = le32_to_cpu(pde);
1357 end = l1 << 22;
1358 if (pde & PG_PRESENT_MASK) {
1359 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1360 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1361 mem_print(mon, &start, &last_prot, end, prot);
1362 } else {
1363 for(l2 = 0; l2 < 1024; l2++) {
1364 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1365 (uint8_t *)&pte, 4);
1366 pte = le32_to_cpu(pte);
1367 end = (l1 << 22) + (l2 << 12);
1368 if (pte & PG_PRESENT_MASK) {
1369 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1370 } else {
1371 prot = 0;
1373 mem_print(mon, &start, &last_prot, end, prot);
1376 } else {
1377 prot = 0;
1378 mem_print(mon, &start, &last_prot, end, prot);
1382 #endif
1384 #if defined(TARGET_SH4)
1386 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1388 monitor_printf(mon, " tlb%i:\t"
1389 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1390 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1391 "dirty=%hhu writethrough=%hhu\n",
1392 idx,
1393 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1394 tlb->v, tlb->sh, tlb->c, tlb->pr,
1395 tlb->d, tlb->wt);
1398 static void tlb_info(Monitor *mon)
1400 CPUState *env = mon_get_cpu();
1401 int i;
1403 monitor_printf (mon, "ITLB:\n");
1404 for (i = 0 ; i < ITLB_SIZE ; i++)
1405 print_tlb (mon, i, &env->itlb[i]);
1406 monitor_printf (mon, "UTLB:\n");
1407 for (i = 0 ; i < UTLB_SIZE ; i++)
1408 print_tlb (mon, i, &env->utlb[i]);
1411 #endif
1413 static void do_info_kqemu(Monitor *mon)
1415 #ifdef CONFIG_KQEMU
1416 CPUState *env;
1417 int val;
1418 val = 0;
1419 env = mon_get_cpu();
1420 if (!env) {
1421 monitor_printf(mon, "No cpu initialized yet");
1422 return;
1424 val = env->kqemu_enabled;
1425 monitor_printf(mon, "kqemu support: ");
1426 switch(val) {
1427 default:
1428 case 0:
1429 monitor_printf(mon, "disabled\n");
1430 break;
1431 case 1:
1432 monitor_printf(mon, "enabled for user code\n");
1433 break;
1434 case 2:
1435 monitor_printf(mon, "enabled for user and kernel code\n");
1436 break;
1438 #else
1439 monitor_printf(mon, "kqemu support: not compiled\n");
1440 #endif
1443 static void do_info_kvm(Monitor *mon)
1445 #if defined(USE_KVM) || defined(CONFIG_KVM)
1446 monitor_printf(mon, "kvm support: ");
1447 if (kvm_enabled())
1448 monitor_printf(mon, "enabled\n");
1449 else
1450 monitor_printf(mon, "disabled\n");
1451 #else
1452 monitor_printf(mon, "kvm support: not compiled\n");
1453 #endif
1456 static void do_info_numa(Monitor *mon)
1458 int i;
1459 CPUState *env;
1461 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1462 for (i = 0; i < nb_numa_nodes; i++) {
1463 monitor_printf(mon, "node %d cpus:", i);
1464 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1465 if (env->numa_node == i) {
1466 monitor_printf(mon, " %d", env->cpu_index);
1469 monitor_printf(mon, "\n");
1470 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1471 node_mem[i] >> 20);
1475 #ifdef CONFIG_PROFILER
1477 int64_t kqemu_time;
1478 int64_t qemu_time;
1479 int64_t kqemu_exec_count;
1480 int64_t dev_time;
1481 int64_t kqemu_ret_int_count;
1482 int64_t kqemu_ret_excp_count;
1483 int64_t kqemu_ret_intr_count;
1485 static void do_info_profile(Monitor *mon)
1487 int64_t total;
1488 total = qemu_time;
1489 if (total == 0)
1490 total = 1;
1491 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1492 dev_time, dev_time / (double)ticks_per_sec);
1493 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1494 qemu_time, qemu_time / (double)ticks_per_sec);
1495 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1496 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1497 PRId64 "\n",
1498 kqemu_time, kqemu_time / (double)ticks_per_sec,
1499 kqemu_time / (double)total * 100.0,
1500 kqemu_exec_count,
1501 kqemu_ret_int_count,
1502 kqemu_ret_excp_count,
1503 kqemu_ret_intr_count);
1504 qemu_time = 0;
1505 kqemu_time = 0;
1506 kqemu_exec_count = 0;
1507 dev_time = 0;
1508 kqemu_ret_int_count = 0;
1509 kqemu_ret_excp_count = 0;
1510 kqemu_ret_intr_count = 0;
1511 #ifdef CONFIG_KQEMU
1512 kqemu_record_dump();
1513 #endif
1515 #else
1516 static void do_info_profile(Monitor *mon)
1518 monitor_printf(mon, "Internal profiler not compiled\n");
1520 #endif
1522 /* Capture support */
1523 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1525 static void do_info_capture(Monitor *mon)
1527 int i;
1528 CaptureState *s;
1530 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1531 monitor_printf(mon, "[%d]: ", i);
1532 s->ops.info (s->opaque);
1536 #ifdef HAS_AUDIO
1537 static void do_stop_capture(Monitor *mon, int n)
1539 int i;
1540 CaptureState *s;
1542 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1543 if (i == n) {
1544 s->ops.destroy (s->opaque);
1545 LIST_REMOVE (s, entries);
1546 qemu_free (s);
1547 return;
1552 static void do_wav_capture(Monitor *mon, const char *path,
1553 int has_freq, int freq,
1554 int has_bits, int bits,
1555 int has_channels, int nchannels)
1557 CaptureState *s;
1559 s = qemu_mallocz (sizeof (*s));
1561 freq = has_freq ? freq : 44100;
1562 bits = has_bits ? bits : 16;
1563 nchannels = has_channels ? nchannels : 2;
1565 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1566 monitor_printf(mon, "Faied to add wave capture\n");
1567 qemu_free (s);
1569 LIST_INSERT_HEAD (&capture_head, s, entries);
1571 #endif
1573 #if defined(TARGET_I386)
1574 static void do_inject_nmi(Monitor *mon, int cpu_index)
1576 CPUState *env;
1578 for (env = first_cpu; env != NULL; env = env->next_cpu)
1579 if (env->cpu_index == cpu_index) {
1580 if (kvm_enabled())
1581 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1582 else
1583 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1584 break;
1587 #endif
1589 static void do_info_status(Monitor *mon)
1591 if (vm_running) {
1592 if (singlestep) {
1593 monitor_printf(mon, "VM status: running (single step mode)\n");
1594 } else {
1595 monitor_printf(mon, "VM status: running\n");
1597 } else
1598 monitor_printf(mon, "VM status: paused\n");
1602 static void do_balloon(Monitor *mon, int value)
1604 ram_addr_t target = value;
1605 qemu_balloon(target << 20);
1608 static void do_info_balloon(Monitor *mon)
1610 ram_addr_t actual;
1612 actual = qemu_balloon_status();
1613 if (kvm_enabled() && !kvm_has_sync_mmu())
1614 monitor_printf(mon, "Using KVM without synchronous MMU, "
1615 "ballooning disabled\n");
1616 else if (actual == 0)
1617 monitor_printf(mon, "Ballooning not activated in VM\n");
1618 else
1619 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1622 static qemu_acl *find_acl(Monitor *mon, const char *name)
1624 qemu_acl *acl = qemu_acl_find(name);
1626 if (!acl) {
1627 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1629 return acl;
1632 static void do_acl_show(Monitor *mon, const char *aclname)
1634 qemu_acl *acl = find_acl(mon, aclname);
1635 qemu_acl_entry *entry;
1636 int i = 0;
1638 if (acl) {
1639 monitor_printf(mon, "policy: %s\n",
1640 acl->defaultDeny ? "deny" : "allow");
1641 TAILQ_FOREACH(entry, &acl->entries, next) {
1642 i++;
1643 monitor_printf(mon, "%d: %s %s\n", i,
1644 entry->deny ? "deny" : "allow", entry->match);
1649 static void do_acl_reset(Monitor *mon, const char *aclname)
1651 qemu_acl *acl = find_acl(mon, aclname);
1653 if (acl) {
1654 qemu_acl_reset(acl);
1655 monitor_printf(mon, "acl: removed all rules\n");
1659 static void do_acl_policy(Monitor *mon, const char *aclname,
1660 const char *policy)
1662 qemu_acl *acl = find_acl(mon, aclname);
1664 if (acl) {
1665 if (strcmp(policy, "allow") == 0) {
1666 acl->defaultDeny = 0;
1667 monitor_printf(mon, "acl: policy set to 'allow'\n");
1668 } else if (strcmp(policy, "deny") == 0) {
1669 acl->defaultDeny = 1;
1670 monitor_printf(mon, "acl: policy set to 'deny'\n");
1671 } else {
1672 monitor_printf(mon, "acl: unknown policy '%s', "
1673 "expected 'deny' or 'allow'\n", policy);
1678 static void do_acl_add(Monitor *mon, const char *aclname,
1679 const char *match, const char *policy,
1680 int has_index, int index)
1682 qemu_acl *acl = find_acl(mon, aclname);
1683 int deny, ret;
1685 if (acl) {
1686 if (strcmp(policy, "allow") == 0) {
1687 deny = 0;
1688 } else if (strcmp(policy, "deny") == 0) {
1689 deny = 1;
1690 } else {
1691 monitor_printf(mon, "acl: unknown policy '%s', "
1692 "expected 'deny' or 'allow'\n", policy);
1693 return;
1695 if (has_index)
1696 ret = qemu_acl_insert(acl, deny, match, index);
1697 else
1698 ret = qemu_acl_append(acl, deny, match);
1699 if (ret < 0)
1700 monitor_printf(mon, "acl: unable to add acl entry\n");
1701 else
1702 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1706 static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1708 qemu_acl *acl = find_acl(mon, aclname);
1709 int ret;
1711 if (acl) {
1712 ret = qemu_acl_remove(acl, match);
1713 if (ret < 0)
1714 monitor_printf(mon, "acl: no matching acl entry\n");
1715 else
1716 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1720 #if defined(TARGET_I386)
1721 static void do_inject_mce(Monitor *mon,
1722 int cpu_index, int bank,
1723 unsigned status_hi, unsigned status_lo,
1724 unsigned mcg_status_hi, unsigned mcg_status_lo,
1725 unsigned addr_hi, unsigned addr_lo,
1726 unsigned misc_hi, unsigned misc_lo)
1728 CPUState *cenv;
1729 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1730 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1731 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1732 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1734 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1735 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1736 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1737 break;
1740 #endif
1742 static void do_getfd(Monitor *mon, const char *fdname)
1744 mon_fd_t *monfd;
1745 int fd;
1747 fd = qemu_chr_get_msgfd(mon->chr);
1748 if (fd == -1) {
1749 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1750 return;
1753 if (qemu_isdigit(fdname[0])) {
1754 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1755 return;
1758 fd = dup(fd);
1759 if (fd == -1) {
1760 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1761 strerror(errno));
1762 return;
1765 LIST_FOREACH(monfd, &mon->fds, next) {
1766 if (strcmp(monfd->name, fdname) != 0) {
1767 continue;
1770 close(monfd->fd);
1771 monfd->fd = fd;
1772 return;
1775 monfd = qemu_mallocz(sizeof(mon_fd_t));
1776 monfd->name = qemu_strdup(fdname);
1777 monfd->fd = fd;
1779 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1782 static void do_closefd(Monitor *mon, const char *fdname)
1784 mon_fd_t *monfd;
1786 LIST_FOREACH(monfd, &mon->fds, next) {
1787 if (strcmp(monfd->name, fdname) != 0) {
1788 continue;
1791 LIST_REMOVE(monfd, next);
1792 close(monfd->fd);
1793 qemu_free(monfd->name);
1794 qemu_free(monfd);
1795 return;
1798 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1799 fdname);
1802 int monitor_get_fd(Monitor *mon, const char *fdname)
1804 mon_fd_t *monfd;
1806 LIST_FOREACH(monfd, &mon->fds, next) {
1807 int fd;
1809 if (strcmp(monfd->name, fdname) != 0) {
1810 continue;
1813 fd = monfd->fd;
1815 /* caller takes ownership of fd */
1816 LIST_REMOVE(monfd, next);
1817 qemu_free(monfd->name);
1818 qemu_free(monfd);
1820 return fd;
1823 return -1;
1826 static const mon_cmd_t mon_cmds[] = {
1827 #include "qemu-monitor.h"
1828 { NULL, NULL, },
1831 /* Please update qemu-monitor.hx when adding or changing commands */
1832 static const mon_cmd_t info_cmds[] = {
1833 { "version", "", do_info_version,
1834 "", "show the version of QEMU" },
1835 { "network", "", do_info_network,
1836 "", "show the network state" },
1837 { "chardev", "", qemu_chr_info,
1838 "", "show the character devices" },
1839 { "block", "", bdrv_info,
1840 "", "show the block devices" },
1841 { "blockstats", "", bdrv_info_stats,
1842 "", "show block device statistics" },
1843 { "registers", "", do_info_registers,
1844 "", "show the cpu registers" },
1845 { "cpus", "", do_info_cpus,
1846 "", "show infos for each CPU" },
1847 { "history", "", do_info_history,
1848 "", "show the command line history", },
1849 { "irq", "", irq_info,
1850 "", "show the interrupts statistics (if available)", },
1851 { "pic", "", pic_info,
1852 "", "show i8259 (PIC) state", },
1853 { "pci", "", pci_info,
1854 "", "show PCI info", },
1855 #if defined(TARGET_I386) || defined(TARGET_SH4)
1856 { "tlb", "", tlb_info,
1857 "", "show virtual to physical memory mappings", },
1858 #endif
1859 #if defined(TARGET_I386)
1860 { "mem", "", mem_info,
1861 "", "show the active virtual memory mappings", },
1862 { "hpet", "", do_info_hpet,
1863 "", "show state of HPET", },
1864 #endif
1865 { "jit", "", do_info_jit,
1866 "", "show dynamic compiler info", },
1867 { "kqemu", "", do_info_kqemu,
1868 "", "show KQEMU information", },
1869 { "kvm", "", do_info_kvm,
1870 "", "show KVM information", },
1871 { "numa", "", do_info_numa,
1872 "", "show NUMA information", },
1873 { "usb", "", usb_info,
1874 "", "show guest USB devices", },
1875 { "usbhost", "", usb_host_info,
1876 "", "show host USB devices", },
1877 { "profile", "", do_info_profile,
1878 "", "show profiling information", },
1879 { "capture", "", do_info_capture,
1880 "", "show capture information" },
1881 { "snapshots", "", do_info_snapshots,
1882 "", "show the currently saved VM snapshots" },
1883 { "status", "", do_info_status,
1884 "", "show the current VM status (running|paused)" },
1885 { "pcmcia", "", pcmcia_info,
1886 "", "show guest PCMCIA status" },
1887 { "mice", "", do_info_mice,
1888 "", "show which guest mouse is receiving events" },
1889 { "vnc", "", do_info_vnc,
1890 "", "show the vnc server status"},
1891 { "name", "", do_info_name,
1892 "", "show the current VM name" },
1893 { "uuid", "", do_info_uuid,
1894 "", "show the current VM UUID" },
1895 #if defined(TARGET_PPC)
1896 { "cpustats", "", do_info_cpu_stats,
1897 "", "show CPU statistics", },
1898 #endif
1899 #if defined(CONFIG_SLIRP)
1900 { "usernet", "", do_info_usernet,
1901 "", "show user network stack connection states", },
1902 #endif
1903 { "migrate", "", do_info_migrate, "", "show migration status" },
1904 { "balloon", "", do_info_balloon,
1905 "", "show balloon information" },
1906 { "qtree", "", do_info_qtree,
1907 "", "show device tree" },
1908 { NULL, NULL, },
1911 /*******************************************************************/
1913 static const char *pch;
1914 static jmp_buf expr_env;
1916 #define MD_TLONG 0
1917 #define MD_I32 1
1919 typedef struct MonitorDef {
1920 const char *name;
1921 int offset;
1922 target_long (*get_value)(const struct MonitorDef *md, int val);
1923 int type;
1924 } MonitorDef;
1926 #if defined(TARGET_I386)
1927 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1929 CPUState *env = mon_get_cpu();
1930 if (!env)
1931 return 0;
1932 return env->eip + env->segs[R_CS].base;
1934 #endif
1936 #if defined(TARGET_PPC)
1937 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1939 CPUState *env = mon_get_cpu();
1940 unsigned int u;
1941 int i;
1943 if (!env)
1944 return 0;
1946 u = 0;
1947 for (i = 0; i < 8; i++)
1948 u |= env->crf[i] << (32 - (4 * i));
1950 return u;
1953 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1955 CPUState *env = mon_get_cpu();
1956 if (!env)
1957 return 0;
1958 return env->msr;
1961 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1963 CPUState *env = mon_get_cpu();
1964 if (!env)
1965 return 0;
1966 return env->xer;
1969 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1971 CPUState *env = mon_get_cpu();
1972 if (!env)
1973 return 0;
1974 return cpu_ppc_load_decr(env);
1977 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1979 CPUState *env = mon_get_cpu();
1980 if (!env)
1981 return 0;
1982 return cpu_ppc_load_tbu(env);
1985 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1987 CPUState *env = mon_get_cpu();
1988 if (!env)
1989 return 0;
1990 return cpu_ppc_load_tbl(env);
1992 #endif
1994 #if defined(TARGET_SPARC)
1995 #ifndef TARGET_SPARC64
1996 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1998 CPUState *env = mon_get_cpu();
1999 if (!env)
2000 return 0;
2001 return GET_PSR(env);
2003 #endif
2005 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2007 CPUState *env = mon_get_cpu();
2008 if (!env)
2009 return 0;
2010 return env->regwptr[val];
2012 #endif
2014 static const MonitorDef monitor_defs[] = {
2015 #ifdef TARGET_I386
2017 #define SEG(name, seg) \
2018 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2019 { name ".base", offsetof(CPUState, segs[seg].base) },\
2020 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2022 { "eax", offsetof(CPUState, regs[0]) },
2023 { "ecx", offsetof(CPUState, regs[1]) },
2024 { "edx", offsetof(CPUState, regs[2]) },
2025 { "ebx", offsetof(CPUState, regs[3]) },
2026 { "esp|sp", offsetof(CPUState, regs[4]) },
2027 { "ebp|fp", offsetof(CPUState, regs[5]) },
2028 { "esi", offsetof(CPUState, regs[6]) },
2029 { "edi", offsetof(CPUState, regs[7]) },
2030 #ifdef TARGET_X86_64
2031 { "r8", offsetof(CPUState, regs[8]) },
2032 { "r9", offsetof(CPUState, regs[9]) },
2033 { "r10", offsetof(CPUState, regs[10]) },
2034 { "r11", offsetof(CPUState, regs[11]) },
2035 { "r12", offsetof(CPUState, regs[12]) },
2036 { "r13", offsetof(CPUState, regs[13]) },
2037 { "r14", offsetof(CPUState, regs[14]) },
2038 { "r15", offsetof(CPUState, regs[15]) },
2039 #endif
2040 { "eflags", offsetof(CPUState, eflags) },
2041 { "eip", offsetof(CPUState, eip) },
2042 SEG("cs", R_CS)
2043 SEG("ds", R_DS)
2044 SEG("es", R_ES)
2045 SEG("ss", R_SS)
2046 SEG("fs", R_FS)
2047 SEG("gs", R_GS)
2048 { "pc", 0, monitor_get_pc, },
2049 #elif defined(TARGET_PPC)
2050 /* General purpose registers */
2051 { "r0", offsetof(CPUState, gpr[0]) },
2052 { "r1", offsetof(CPUState, gpr[1]) },
2053 { "r2", offsetof(CPUState, gpr[2]) },
2054 { "r3", offsetof(CPUState, gpr[3]) },
2055 { "r4", offsetof(CPUState, gpr[4]) },
2056 { "r5", offsetof(CPUState, gpr[5]) },
2057 { "r6", offsetof(CPUState, gpr[6]) },
2058 { "r7", offsetof(CPUState, gpr[7]) },
2059 { "r8", offsetof(CPUState, gpr[8]) },
2060 { "r9", offsetof(CPUState, gpr[9]) },
2061 { "r10", offsetof(CPUState, gpr[10]) },
2062 { "r11", offsetof(CPUState, gpr[11]) },
2063 { "r12", offsetof(CPUState, gpr[12]) },
2064 { "r13", offsetof(CPUState, gpr[13]) },
2065 { "r14", offsetof(CPUState, gpr[14]) },
2066 { "r15", offsetof(CPUState, gpr[15]) },
2067 { "r16", offsetof(CPUState, gpr[16]) },
2068 { "r17", offsetof(CPUState, gpr[17]) },
2069 { "r18", offsetof(CPUState, gpr[18]) },
2070 { "r19", offsetof(CPUState, gpr[19]) },
2071 { "r20", offsetof(CPUState, gpr[20]) },
2072 { "r21", offsetof(CPUState, gpr[21]) },
2073 { "r22", offsetof(CPUState, gpr[22]) },
2074 { "r23", offsetof(CPUState, gpr[23]) },
2075 { "r24", offsetof(CPUState, gpr[24]) },
2076 { "r25", offsetof(CPUState, gpr[25]) },
2077 { "r26", offsetof(CPUState, gpr[26]) },
2078 { "r27", offsetof(CPUState, gpr[27]) },
2079 { "r28", offsetof(CPUState, gpr[28]) },
2080 { "r29", offsetof(CPUState, gpr[29]) },
2081 { "r30", offsetof(CPUState, gpr[30]) },
2082 { "r31", offsetof(CPUState, gpr[31]) },
2083 /* Floating point registers */
2084 { "f0", offsetof(CPUState, fpr[0]) },
2085 { "f1", offsetof(CPUState, fpr[1]) },
2086 { "f2", offsetof(CPUState, fpr[2]) },
2087 { "f3", offsetof(CPUState, fpr[3]) },
2088 { "f4", offsetof(CPUState, fpr[4]) },
2089 { "f5", offsetof(CPUState, fpr[5]) },
2090 { "f6", offsetof(CPUState, fpr[6]) },
2091 { "f7", offsetof(CPUState, fpr[7]) },
2092 { "f8", offsetof(CPUState, fpr[8]) },
2093 { "f9", offsetof(CPUState, fpr[9]) },
2094 { "f10", offsetof(CPUState, fpr[10]) },
2095 { "f11", offsetof(CPUState, fpr[11]) },
2096 { "f12", offsetof(CPUState, fpr[12]) },
2097 { "f13", offsetof(CPUState, fpr[13]) },
2098 { "f14", offsetof(CPUState, fpr[14]) },
2099 { "f15", offsetof(CPUState, fpr[15]) },
2100 { "f16", offsetof(CPUState, fpr[16]) },
2101 { "f17", offsetof(CPUState, fpr[17]) },
2102 { "f18", offsetof(CPUState, fpr[18]) },
2103 { "f19", offsetof(CPUState, fpr[19]) },
2104 { "f20", offsetof(CPUState, fpr[20]) },
2105 { "f21", offsetof(CPUState, fpr[21]) },
2106 { "f22", offsetof(CPUState, fpr[22]) },
2107 { "f23", offsetof(CPUState, fpr[23]) },
2108 { "f24", offsetof(CPUState, fpr[24]) },
2109 { "f25", offsetof(CPUState, fpr[25]) },
2110 { "f26", offsetof(CPUState, fpr[26]) },
2111 { "f27", offsetof(CPUState, fpr[27]) },
2112 { "f28", offsetof(CPUState, fpr[28]) },
2113 { "f29", offsetof(CPUState, fpr[29]) },
2114 { "f30", offsetof(CPUState, fpr[30]) },
2115 { "f31", offsetof(CPUState, fpr[31]) },
2116 { "fpscr", offsetof(CPUState, fpscr) },
2117 /* Next instruction pointer */
2118 { "nip|pc", offsetof(CPUState, nip) },
2119 { "lr", offsetof(CPUState, lr) },
2120 { "ctr", offsetof(CPUState, ctr) },
2121 { "decr", 0, &monitor_get_decr, },
2122 { "ccr", 0, &monitor_get_ccr, },
2123 /* Machine state register */
2124 { "msr", 0, &monitor_get_msr, },
2125 { "xer", 0, &monitor_get_xer, },
2126 { "tbu", 0, &monitor_get_tbu, },
2127 { "tbl", 0, &monitor_get_tbl, },
2128 #if defined(TARGET_PPC64)
2129 /* Address space register */
2130 { "asr", offsetof(CPUState, asr) },
2131 #endif
2132 /* Segment registers */
2133 { "sdr1", offsetof(CPUState, sdr1) },
2134 { "sr0", offsetof(CPUState, sr[0]) },
2135 { "sr1", offsetof(CPUState, sr[1]) },
2136 { "sr2", offsetof(CPUState, sr[2]) },
2137 { "sr3", offsetof(CPUState, sr[3]) },
2138 { "sr4", offsetof(CPUState, sr[4]) },
2139 { "sr5", offsetof(CPUState, sr[5]) },
2140 { "sr6", offsetof(CPUState, sr[6]) },
2141 { "sr7", offsetof(CPUState, sr[7]) },
2142 { "sr8", offsetof(CPUState, sr[8]) },
2143 { "sr9", offsetof(CPUState, sr[9]) },
2144 { "sr10", offsetof(CPUState, sr[10]) },
2145 { "sr11", offsetof(CPUState, sr[11]) },
2146 { "sr12", offsetof(CPUState, sr[12]) },
2147 { "sr13", offsetof(CPUState, sr[13]) },
2148 { "sr14", offsetof(CPUState, sr[14]) },
2149 { "sr15", offsetof(CPUState, sr[15]) },
2150 /* Too lazy to put BATs and SPRs ... */
2151 #elif defined(TARGET_SPARC)
2152 { "g0", offsetof(CPUState, gregs[0]) },
2153 { "g1", offsetof(CPUState, gregs[1]) },
2154 { "g2", offsetof(CPUState, gregs[2]) },
2155 { "g3", offsetof(CPUState, gregs[3]) },
2156 { "g4", offsetof(CPUState, gregs[4]) },
2157 { "g5", offsetof(CPUState, gregs[5]) },
2158 { "g6", offsetof(CPUState, gregs[6]) },
2159 { "g7", offsetof(CPUState, gregs[7]) },
2160 { "o0", 0, monitor_get_reg },
2161 { "o1", 1, monitor_get_reg },
2162 { "o2", 2, monitor_get_reg },
2163 { "o3", 3, monitor_get_reg },
2164 { "o4", 4, monitor_get_reg },
2165 { "o5", 5, monitor_get_reg },
2166 { "o6", 6, monitor_get_reg },
2167 { "o7", 7, monitor_get_reg },
2168 { "l0", 8, monitor_get_reg },
2169 { "l1", 9, monitor_get_reg },
2170 { "l2", 10, monitor_get_reg },
2171 { "l3", 11, monitor_get_reg },
2172 { "l4", 12, monitor_get_reg },
2173 { "l5", 13, monitor_get_reg },
2174 { "l6", 14, monitor_get_reg },
2175 { "l7", 15, monitor_get_reg },
2176 { "i0", 16, monitor_get_reg },
2177 { "i1", 17, monitor_get_reg },
2178 { "i2", 18, monitor_get_reg },
2179 { "i3", 19, monitor_get_reg },
2180 { "i4", 20, monitor_get_reg },
2181 { "i5", 21, monitor_get_reg },
2182 { "i6", 22, monitor_get_reg },
2183 { "i7", 23, monitor_get_reg },
2184 { "pc", offsetof(CPUState, pc) },
2185 { "npc", offsetof(CPUState, npc) },
2186 { "y", offsetof(CPUState, y) },
2187 #ifndef TARGET_SPARC64
2188 { "psr", 0, &monitor_get_psr, },
2189 { "wim", offsetof(CPUState, wim) },
2190 #endif
2191 { "tbr", offsetof(CPUState, tbr) },
2192 { "fsr", offsetof(CPUState, fsr) },
2193 { "f0", offsetof(CPUState, fpr[0]) },
2194 { "f1", offsetof(CPUState, fpr[1]) },
2195 { "f2", offsetof(CPUState, fpr[2]) },
2196 { "f3", offsetof(CPUState, fpr[3]) },
2197 { "f4", offsetof(CPUState, fpr[4]) },
2198 { "f5", offsetof(CPUState, fpr[5]) },
2199 { "f6", offsetof(CPUState, fpr[6]) },
2200 { "f7", offsetof(CPUState, fpr[7]) },
2201 { "f8", offsetof(CPUState, fpr[8]) },
2202 { "f9", offsetof(CPUState, fpr[9]) },
2203 { "f10", offsetof(CPUState, fpr[10]) },
2204 { "f11", offsetof(CPUState, fpr[11]) },
2205 { "f12", offsetof(CPUState, fpr[12]) },
2206 { "f13", offsetof(CPUState, fpr[13]) },
2207 { "f14", offsetof(CPUState, fpr[14]) },
2208 { "f15", offsetof(CPUState, fpr[15]) },
2209 { "f16", offsetof(CPUState, fpr[16]) },
2210 { "f17", offsetof(CPUState, fpr[17]) },
2211 { "f18", offsetof(CPUState, fpr[18]) },
2212 { "f19", offsetof(CPUState, fpr[19]) },
2213 { "f20", offsetof(CPUState, fpr[20]) },
2214 { "f21", offsetof(CPUState, fpr[21]) },
2215 { "f22", offsetof(CPUState, fpr[22]) },
2216 { "f23", offsetof(CPUState, fpr[23]) },
2217 { "f24", offsetof(CPUState, fpr[24]) },
2218 { "f25", offsetof(CPUState, fpr[25]) },
2219 { "f26", offsetof(CPUState, fpr[26]) },
2220 { "f27", offsetof(CPUState, fpr[27]) },
2221 { "f28", offsetof(CPUState, fpr[28]) },
2222 { "f29", offsetof(CPUState, fpr[29]) },
2223 { "f30", offsetof(CPUState, fpr[30]) },
2224 { "f31", offsetof(CPUState, fpr[31]) },
2225 #ifdef TARGET_SPARC64
2226 { "f32", offsetof(CPUState, fpr[32]) },
2227 { "f34", offsetof(CPUState, fpr[34]) },
2228 { "f36", offsetof(CPUState, fpr[36]) },
2229 { "f38", offsetof(CPUState, fpr[38]) },
2230 { "f40", offsetof(CPUState, fpr[40]) },
2231 { "f42", offsetof(CPUState, fpr[42]) },
2232 { "f44", offsetof(CPUState, fpr[44]) },
2233 { "f46", offsetof(CPUState, fpr[46]) },
2234 { "f48", offsetof(CPUState, fpr[48]) },
2235 { "f50", offsetof(CPUState, fpr[50]) },
2236 { "f52", offsetof(CPUState, fpr[52]) },
2237 { "f54", offsetof(CPUState, fpr[54]) },
2238 { "f56", offsetof(CPUState, fpr[56]) },
2239 { "f58", offsetof(CPUState, fpr[58]) },
2240 { "f60", offsetof(CPUState, fpr[60]) },
2241 { "f62", offsetof(CPUState, fpr[62]) },
2242 { "asi", offsetof(CPUState, asi) },
2243 { "pstate", offsetof(CPUState, pstate) },
2244 { "cansave", offsetof(CPUState, cansave) },
2245 { "canrestore", offsetof(CPUState, canrestore) },
2246 { "otherwin", offsetof(CPUState, otherwin) },
2247 { "wstate", offsetof(CPUState, wstate) },
2248 { "cleanwin", offsetof(CPUState, cleanwin) },
2249 { "fprs", offsetof(CPUState, fprs) },
2250 #endif
2251 #endif
2252 { NULL },
2255 static void expr_error(Monitor *mon, const char *msg)
2257 monitor_printf(mon, "%s\n", msg);
2258 longjmp(expr_env, 1);
2261 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2262 static int get_monitor_def(target_long *pval, const char *name)
2264 const MonitorDef *md;
2265 void *ptr;
2267 for(md = monitor_defs; md->name != NULL; md++) {
2268 if (compare_cmd(name, md->name)) {
2269 if (md->get_value) {
2270 *pval = md->get_value(md, md->offset);
2271 } else {
2272 CPUState *env = mon_get_cpu();
2273 if (!env)
2274 return -2;
2275 ptr = (uint8_t *)env + md->offset;
2276 switch(md->type) {
2277 case MD_I32:
2278 *pval = *(int32_t *)ptr;
2279 break;
2280 case MD_TLONG:
2281 *pval = *(target_long *)ptr;
2282 break;
2283 default:
2284 *pval = 0;
2285 break;
2288 return 0;
2291 return -1;
2294 static void next(void)
2296 if (*pch != '\0') {
2297 pch++;
2298 while (qemu_isspace(*pch))
2299 pch++;
2303 static int64_t expr_sum(Monitor *mon);
2305 static int64_t expr_unary(Monitor *mon)
2307 int64_t n;
2308 char *p;
2309 int ret;
2311 switch(*pch) {
2312 case '+':
2313 next();
2314 n = expr_unary(mon);
2315 break;
2316 case '-':
2317 next();
2318 n = -expr_unary(mon);
2319 break;
2320 case '~':
2321 next();
2322 n = ~expr_unary(mon);
2323 break;
2324 case '(':
2325 next();
2326 n = expr_sum(mon);
2327 if (*pch != ')') {
2328 expr_error(mon, "')' expected");
2330 next();
2331 break;
2332 case '\'':
2333 pch++;
2334 if (*pch == '\0')
2335 expr_error(mon, "character constant expected");
2336 n = *pch;
2337 pch++;
2338 if (*pch != '\'')
2339 expr_error(mon, "missing terminating \' character");
2340 next();
2341 break;
2342 case '$':
2344 char buf[128], *q;
2345 target_long reg=0;
2347 pch++;
2348 q = buf;
2349 while ((*pch >= 'a' && *pch <= 'z') ||
2350 (*pch >= 'A' && *pch <= 'Z') ||
2351 (*pch >= '0' && *pch <= '9') ||
2352 *pch == '_' || *pch == '.') {
2353 if ((q - buf) < sizeof(buf) - 1)
2354 *q++ = *pch;
2355 pch++;
2357 while (qemu_isspace(*pch))
2358 pch++;
2359 *q = 0;
2360 ret = get_monitor_def(&reg, buf);
2361 if (ret == -1)
2362 expr_error(mon, "unknown register");
2363 else if (ret == -2)
2364 expr_error(mon, "no cpu defined");
2365 n = reg;
2367 break;
2368 case '\0':
2369 expr_error(mon, "unexpected end of expression");
2370 n = 0;
2371 break;
2372 default:
2373 #if TARGET_PHYS_ADDR_BITS > 32
2374 n = strtoull(pch, &p, 0);
2375 #else
2376 n = strtoul(pch, &p, 0);
2377 #endif
2378 if (pch == p) {
2379 expr_error(mon, "invalid char in expression");
2381 pch = p;
2382 while (qemu_isspace(*pch))
2383 pch++;
2384 break;
2386 return n;
2390 static int64_t expr_prod(Monitor *mon)
2392 int64_t val, val2;
2393 int op;
2395 val = expr_unary(mon);
2396 for(;;) {
2397 op = *pch;
2398 if (op != '*' && op != '/' && op != '%')
2399 break;
2400 next();
2401 val2 = expr_unary(mon);
2402 switch(op) {
2403 default:
2404 case '*':
2405 val *= val2;
2406 break;
2407 case '/':
2408 case '%':
2409 if (val2 == 0)
2410 expr_error(mon, "division by zero");
2411 if (op == '/')
2412 val /= val2;
2413 else
2414 val %= val2;
2415 break;
2418 return val;
2421 static int64_t expr_logic(Monitor *mon)
2423 int64_t val, val2;
2424 int op;
2426 val = expr_prod(mon);
2427 for(;;) {
2428 op = *pch;
2429 if (op != '&' && op != '|' && op != '^')
2430 break;
2431 next();
2432 val2 = expr_prod(mon);
2433 switch(op) {
2434 default:
2435 case '&':
2436 val &= val2;
2437 break;
2438 case '|':
2439 val |= val2;
2440 break;
2441 case '^':
2442 val ^= val2;
2443 break;
2446 return val;
2449 static int64_t expr_sum(Monitor *mon)
2451 int64_t val, val2;
2452 int op;
2454 val = expr_logic(mon);
2455 for(;;) {
2456 op = *pch;
2457 if (op != '+' && op != '-')
2458 break;
2459 next();
2460 val2 = expr_logic(mon);
2461 if (op == '+')
2462 val += val2;
2463 else
2464 val -= val2;
2466 return val;
2469 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2471 pch = *pp;
2472 if (setjmp(expr_env)) {
2473 *pp = pch;
2474 return -1;
2476 while (qemu_isspace(*pch))
2477 pch++;
2478 *pval = expr_sum(mon);
2479 *pp = pch;
2480 return 0;
2483 static int get_str(char *buf, int buf_size, const char **pp)
2485 const char *p;
2486 char *q;
2487 int c;
2489 q = buf;
2490 p = *pp;
2491 while (qemu_isspace(*p))
2492 p++;
2493 if (*p == '\0') {
2494 fail:
2495 *q = '\0';
2496 *pp = p;
2497 return -1;
2499 if (*p == '\"') {
2500 p++;
2501 while (*p != '\0' && *p != '\"') {
2502 if (*p == '\\') {
2503 p++;
2504 c = *p++;
2505 switch(c) {
2506 case 'n':
2507 c = '\n';
2508 break;
2509 case 'r':
2510 c = '\r';
2511 break;
2512 case '\\':
2513 case '\'':
2514 case '\"':
2515 break;
2516 default:
2517 qemu_printf("unsupported escape code: '\\%c'\n", c);
2518 goto fail;
2520 if ((q - buf) < buf_size - 1) {
2521 *q++ = c;
2523 } else {
2524 if ((q - buf) < buf_size - 1) {
2525 *q++ = *p;
2527 p++;
2530 if (*p != '\"') {
2531 qemu_printf("unterminated string\n");
2532 goto fail;
2534 p++;
2535 } else {
2536 while (*p != '\0' && !qemu_isspace(*p)) {
2537 if ((q - buf) < buf_size - 1) {
2538 *q++ = *p;
2540 p++;
2543 *q = '\0';
2544 *pp = p;
2545 return 0;
2549 * Store the command-name in cmdname, and return a pointer to
2550 * the remaining of the command string.
2552 static const char *get_command_name(const char *cmdline,
2553 char *cmdname, size_t nlen)
2555 size_t len;
2556 const char *p, *pstart;
2558 p = cmdline;
2559 while (qemu_isspace(*p))
2560 p++;
2561 if (*p == '\0')
2562 return NULL;
2563 pstart = p;
2564 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2565 p++;
2566 len = p - pstart;
2567 if (len > nlen - 1)
2568 len = nlen - 1;
2569 memcpy(cmdname, pstart, len);
2570 cmdname[len] = '\0';
2571 return p;
2574 static int default_fmt_format = 'x';
2575 static int default_fmt_size = 4;
2577 #define MAX_ARGS 16
2579 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2581 const char *p, *typestr;
2582 int c, nb_args, i, has_arg;
2583 const mon_cmd_t *cmd;
2584 char cmdname[256];
2585 char buf[1024];
2586 void *str_allocated[MAX_ARGS];
2587 void *args[MAX_ARGS];
2588 void (*handler_0)(Monitor *mon);
2589 void (*handler_1)(Monitor *mon, void *arg0);
2590 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2591 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2592 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2593 void *arg3);
2594 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2595 void *arg3, void *arg4);
2596 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2597 void *arg3, void *arg4, void *arg5);
2598 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2599 void *arg3, void *arg4, void *arg5, void *arg6);
2600 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2601 void *arg3, void *arg4, void *arg5, void *arg6,
2602 void *arg7);
2603 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2604 void *arg3, void *arg4, void *arg5, void *arg6,
2605 void *arg7, void *arg8);
2606 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2607 void *arg3, void *arg4, void *arg5, void *arg6,
2608 void *arg7, void *arg8, void *arg9);
2610 #ifdef DEBUG
2611 monitor_printf(mon, "command='%s'\n", cmdline);
2612 #endif
2614 /* extract the command name */
2615 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2616 if (!p)
2617 return;
2619 /* find the command */
2620 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2621 if (compare_cmd(cmdname, cmd->name))
2622 break;
2625 if (cmd->name == NULL) {
2626 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2627 return;
2630 for(i = 0; i < MAX_ARGS; i++)
2631 str_allocated[i] = NULL;
2633 /* parse the parameters */
2634 typestr = cmd->args_type;
2635 nb_args = 0;
2636 for(;;) {
2637 c = *typestr;
2638 if (c == '\0')
2639 break;
2640 typestr++;
2641 switch(c) {
2642 case 'F':
2643 case 'B':
2644 case 's':
2646 int ret;
2647 char *str;
2649 while (qemu_isspace(*p))
2650 p++;
2651 if (*typestr == '?') {
2652 typestr++;
2653 if (*p == '\0') {
2654 /* no optional string: NULL argument */
2655 str = NULL;
2656 goto add_str;
2659 ret = get_str(buf, sizeof(buf), &p);
2660 if (ret < 0) {
2661 switch(c) {
2662 case 'F':
2663 monitor_printf(mon, "%s: filename expected\n",
2664 cmdname);
2665 break;
2666 case 'B':
2667 monitor_printf(mon, "%s: block device name expected\n",
2668 cmdname);
2669 break;
2670 default:
2671 monitor_printf(mon, "%s: string expected\n", cmdname);
2672 break;
2674 goto fail;
2676 str = qemu_malloc(strlen(buf) + 1);
2677 pstrcpy(str, sizeof(buf), buf);
2678 str_allocated[nb_args] = str;
2679 add_str:
2680 if (nb_args >= MAX_ARGS) {
2681 error_args:
2682 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2683 goto fail;
2685 args[nb_args++] = str;
2687 break;
2688 case '/':
2690 int count, format, size;
2692 while (qemu_isspace(*p))
2693 p++;
2694 if (*p == '/') {
2695 /* format found */
2696 p++;
2697 count = 1;
2698 if (qemu_isdigit(*p)) {
2699 count = 0;
2700 while (qemu_isdigit(*p)) {
2701 count = count * 10 + (*p - '0');
2702 p++;
2705 size = -1;
2706 format = -1;
2707 for(;;) {
2708 switch(*p) {
2709 case 'o':
2710 case 'd':
2711 case 'u':
2712 case 'x':
2713 case 'i':
2714 case 'c':
2715 format = *p++;
2716 break;
2717 case 'b':
2718 size = 1;
2719 p++;
2720 break;
2721 case 'h':
2722 size = 2;
2723 p++;
2724 break;
2725 case 'w':
2726 size = 4;
2727 p++;
2728 break;
2729 case 'g':
2730 case 'L':
2731 size = 8;
2732 p++;
2733 break;
2734 default:
2735 goto next;
2738 next:
2739 if (*p != '\0' && !qemu_isspace(*p)) {
2740 monitor_printf(mon, "invalid char in format: '%c'\n",
2741 *p);
2742 goto fail;
2744 if (format < 0)
2745 format = default_fmt_format;
2746 if (format != 'i') {
2747 /* for 'i', not specifying a size gives -1 as size */
2748 if (size < 0)
2749 size = default_fmt_size;
2750 default_fmt_size = size;
2752 default_fmt_format = format;
2753 } else {
2754 count = 1;
2755 format = default_fmt_format;
2756 if (format != 'i') {
2757 size = default_fmt_size;
2758 } else {
2759 size = -1;
2762 if (nb_args + 3 > MAX_ARGS)
2763 goto error_args;
2764 args[nb_args++] = (void*)(long)count;
2765 args[nb_args++] = (void*)(long)format;
2766 args[nb_args++] = (void*)(long)size;
2768 break;
2769 case 'i':
2770 case 'l':
2772 int64_t val;
2774 while (qemu_isspace(*p))
2775 p++;
2776 if (*typestr == '?' || *typestr == '.') {
2777 if (*typestr == '?') {
2778 if (*p == '\0')
2779 has_arg = 0;
2780 else
2781 has_arg = 1;
2782 } else {
2783 if (*p == '.') {
2784 p++;
2785 while (qemu_isspace(*p))
2786 p++;
2787 has_arg = 1;
2788 } else {
2789 has_arg = 0;
2792 typestr++;
2793 if (nb_args >= MAX_ARGS)
2794 goto error_args;
2795 args[nb_args++] = (void *)(long)has_arg;
2796 if (!has_arg) {
2797 if (nb_args >= MAX_ARGS)
2798 goto error_args;
2799 val = -1;
2800 goto add_num;
2803 if (get_expr(mon, &val, &p))
2804 goto fail;
2805 add_num:
2806 if (c == 'i') {
2807 if (nb_args >= MAX_ARGS)
2808 goto error_args;
2809 args[nb_args++] = (void *)(long)val;
2810 } else {
2811 if ((nb_args + 1) >= MAX_ARGS)
2812 goto error_args;
2813 #if TARGET_PHYS_ADDR_BITS > 32
2814 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2815 #else
2816 args[nb_args++] = (void *)0;
2817 #endif
2818 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2821 break;
2822 case '-':
2824 int has_option;
2825 /* option */
2827 c = *typestr++;
2828 if (c == '\0')
2829 goto bad_type;
2830 while (qemu_isspace(*p))
2831 p++;
2832 has_option = 0;
2833 if (*p == '-') {
2834 p++;
2835 if (*p != c) {
2836 monitor_printf(mon, "%s: unsupported option -%c\n",
2837 cmdname, *p);
2838 goto fail;
2840 p++;
2841 has_option = 1;
2843 if (nb_args >= MAX_ARGS)
2844 goto error_args;
2845 args[nb_args++] = (void *)(long)has_option;
2847 break;
2848 default:
2849 bad_type:
2850 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2851 goto fail;
2854 /* check that all arguments were parsed */
2855 while (qemu_isspace(*p))
2856 p++;
2857 if (*p != '\0') {
2858 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2859 cmdname);
2860 goto fail;
2863 switch(nb_args) {
2864 case 0:
2865 handler_0 = cmd->handler;
2866 handler_0(mon);
2867 break;
2868 case 1:
2869 handler_1 = cmd->handler;
2870 handler_1(mon, args[0]);
2871 break;
2872 case 2:
2873 handler_2 = cmd->handler;
2874 handler_2(mon, args[0], args[1]);
2875 break;
2876 case 3:
2877 handler_3 = cmd->handler;
2878 handler_3(mon, args[0], args[1], args[2]);
2879 break;
2880 case 4:
2881 handler_4 = cmd->handler;
2882 handler_4(mon, args[0], args[1], args[2], args[3]);
2883 break;
2884 case 5:
2885 handler_5 = cmd->handler;
2886 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2887 break;
2888 case 6:
2889 handler_6 = cmd->handler;
2890 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2891 break;
2892 case 7:
2893 handler_7 = cmd->handler;
2894 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2895 args[6]);
2896 break;
2897 case 8:
2898 handler_8 = cmd->handler;
2899 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2900 args[6], args[7]);
2901 break;
2902 case 9:
2903 handler_9 = cmd->handler;
2904 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2905 args[6], args[7], args[8]);
2906 break;
2907 case 10:
2908 handler_10 = cmd->handler;
2909 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2910 args[6], args[7], args[8], args[9]);
2911 break;
2912 default:
2913 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2914 goto fail;
2916 fail:
2917 for(i = 0; i < MAX_ARGS; i++)
2918 qemu_free(str_allocated[i]);
2921 static void cmd_completion(const char *name, const char *list)
2923 const char *p, *pstart;
2924 char cmd[128];
2925 int len;
2927 p = list;
2928 for(;;) {
2929 pstart = p;
2930 p = strchr(p, '|');
2931 if (!p)
2932 p = pstart + strlen(pstart);
2933 len = p - pstart;
2934 if (len > sizeof(cmd) - 2)
2935 len = sizeof(cmd) - 2;
2936 memcpy(cmd, pstart, len);
2937 cmd[len] = '\0';
2938 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2939 readline_add_completion(cur_mon->rs, cmd);
2941 if (*p == '\0')
2942 break;
2943 p++;
2947 static void file_completion(const char *input)
2949 DIR *ffs;
2950 struct dirent *d;
2951 char path[1024];
2952 char file[1024], file_prefix[1024];
2953 int input_path_len;
2954 const char *p;
2956 p = strrchr(input, '/');
2957 if (!p) {
2958 input_path_len = 0;
2959 pstrcpy(file_prefix, sizeof(file_prefix), input);
2960 pstrcpy(path, sizeof(path), ".");
2961 } else {
2962 input_path_len = p - input + 1;
2963 memcpy(path, input, input_path_len);
2964 if (input_path_len > sizeof(path) - 1)
2965 input_path_len = sizeof(path) - 1;
2966 path[input_path_len] = '\0';
2967 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2969 #ifdef DEBUG_COMPLETION
2970 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2971 input, path, file_prefix);
2972 #endif
2973 ffs = opendir(path);
2974 if (!ffs)
2975 return;
2976 for(;;) {
2977 struct stat sb;
2978 d = readdir(ffs);
2979 if (!d)
2980 break;
2981 if (strstart(d->d_name, file_prefix, NULL)) {
2982 memcpy(file, input, input_path_len);
2983 if (input_path_len < sizeof(file))
2984 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2985 d->d_name);
2986 /* stat the file to find out if it's a directory.
2987 * In that case add a slash to speed up typing long paths
2989 stat(file, &sb);
2990 if(S_ISDIR(sb.st_mode))
2991 pstrcat(file, sizeof(file), "/");
2992 readline_add_completion(cur_mon->rs, file);
2995 closedir(ffs);
2998 static void block_completion_it(void *opaque, BlockDriverState *bs)
3000 const char *name = bdrv_get_device_name(bs);
3001 const char *input = opaque;
3003 if (input[0] == '\0' ||
3004 !strncmp(name, (char *)input, strlen(input))) {
3005 readline_add_completion(cur_mon->rs, name);
3009 /* NOTE: this parser is an approximate form of the real command parser */
3010 static void parse_cmdline(const char *cmdline,
3011 int *pnb_args, char **args)
3013 const char *p;
3014 int nb_args, ret;
3015 char buf[1024];
3017 p = cmdline;
3018 nb_args = 0;
3019 for(;;) {
3020 while (qemu_isspace(*p))
3021 p++;
3022 if (*p == '\0')
3023 break;
3024 if (nb_args >= MAX_ARGS)
3025 break;
3026 ret = get_str(buf, sizeof(buf), &p);
3027 args[nb_args] = qemu_strdup(buf);
3028 nb_args++;
3029 if (ret < 0)
3030 break;
3032 *pnb_args = nb_args;
3035 static void monitor_find_completion(const char *cmdline)
3037 const char *cmdname;
3038 char *args[MAX_ARGS];
3039 int nb_args, i, len;
3040 const char *ptype, *str;
3041 const mon_cmd_t *cmd;
3042 const KeyDef *key;
3044 parse_cmdline(cmdline, &nb_args, args);
3045 #ifdef DEBUG_COMPLETION
3046 for(i = 0; i < nb_args; i++) {
3047 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3049 #endif
3051 /* if the line ends with a space, it means we want to complete the
3052 next arg */
3053 len = strlen(cmdline);
3054 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3055 if (nb_args >= MAX_ARGS)
3056 return;
3057 args[nb_args++] = qemu_strdup("");
3059 if (nb_args <= 1) {
3060 /* command completion */
3061 if (nb_args == 0)
3062 cmdname = "";
3063 else
3064 cmdname = args[0];
3065 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3066 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3067 cmd_completion(cmdname, cmd->name);
3069 } else {
3070 /* find the command */
3071 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3072 if (compare_cmd(args[0], cmd->name))
3073 goto found;
3075 return;
3076 found:
3077 ptype = cmd->args_type;
3078 for(i = 0; i < nb_args - 2; i++) {
3079 if (*ptype != '\0') {
3080 ptype++;
3081 while (*ptype == '?')
3082 ptype++;
3085 str = args[nb_args - 1];
3086 if (*ptype == '-' && ptype[1] != '\0') {
3087 ptype += 2;
3089 switch(*ptype) {
3090 case 'F':
3091 /* file completion */
3092 readline_set_completion_index(cur_mon->rs, strlen(str));
3093 file_completion(str);
3094 break;
3095 case 'B':
3096 /* block device name completion */
3097 readline_set_completion_index(cur_mon->rs, strlen(str));
3098 bdrv_iterate(block_completion_it, (void *)str);
3099 break;
3100 case 's':
3101 /* XXX: more generic ? */
3102 if (!strcmp(cmd->name, "info")) {
3103 readline_set_completion_index(cur_mon->rs, strlen(str));
3104 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3105 cmd_completion(str, cmd->name);
3107 } else if (!strcmp(cmd->name, "sendkey")) {
3108 char *sep = strrchr(str, '-');
3109 if (sep)
3110 str = sep + 1;
3111 readline_set_completion_index(cur_mon->rs, strlen(str));
3112 for(key = key_defs; key->name != NULL; key++) {
3113 cmd_completion(str, key->name);
3115 } else if (!strcmp(cmd->name, "help|?")) {
3116 readline_set_completion_index(cur_mon->rs, strlen(str));
3117 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3118 cmd_completion(str, cmd->name);
3121 break;
3122 default:
3123 break;
3126 for(i = 0; i < nb_args; i++)
3127 qemu_free(args[i]);
3130 static int monitor_can_read(void *opaque)
3132 Monitor *mon = opaque;
3134 return (mon->suspend_cnt == 0) ? 128 : 0;
3137 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3139 Monitor *old_mon = cur_mon;
3140 int i;
3142 cur_mon = opaque;
3144 if (cur_mon->rs) {
3145 for (i = 0; i < size; i++)
3146 readline_handle_byte(cur_mon->rs, buf[i]);
3147 } else {
3148 if (size == 0 || buf[size - 1] != 0)
3149 monitor_printf(cur_mon, "corrupted command\n");
3150 else
3151 monitor_handle_command(cur_mon, (char *)buf);
3154 cur_mon = old_mon;
3157 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3159 monitor_suspend(mon);
3160 monitor_handle_command(mon, cmdline);
3161 monitor_resume(mon);
3164 int monitor_suspend(Monitor *mon)
3166 if (!mon->rs)
3167 return -ENOTTY;
3168 mon->suspend_cnt++;
3169 return 0;
3172 void monitor_resume(Monitor *mon)
3174 if (!mon->rs)
3175 return;
3176 if (--mon->suspend_cnt == 0)
3177 readline_show_prompt(mon->rs);
3180 static void monitor_event(void *opaque, int event)
3182 Monitor *mon = opaque;
3184 switch (event) {
3185 case CHR_EVENT_MUX_IN:
3186 readline_restart(mon->rs);
3187 monitor_resume(mon);
3188 monitor_flush(mon);
3189 break;
3191 case CHR_EVENT_MUX_OUT:
3192 if (mon->suspend_cnt == 0)
3193 monitor_printf(mon, "\n");
3194 monitor_flush(mon);
3195 monitor_suspend(mon);
3196 break;
3198 case CHR_EVENT_RESET:
3199 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3200 "information\n", QEMU_VERSION);
3201 if (mon->chr->focus == 0)
3202 readline_show_prompt(mon->rs);
3203 break;
3209 * Local variables:
3210 * c-indent-level: 4
3211 * c-basic-offset: 4
3212 * tab-width: 8
3213 * End:
3216 void monitor_init(CharDriverState *chr, int flags)
3218 static int is_first_init = 1;
3219 Monitor *mon;
3221 if (is_first_init) {
3222 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3223 is_first_init = 0;
3226 mon = qemu_mallocz(sizeof(*mon));
3228 mon->chr = chr;
3229 mon->flags = flags;
3230 if (mon->chr->focus != 0)
3231 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3232 if (flags & MONITOR_USE_READLINE) {
3233 mon->rs = readline_init(mon, monitor_find_completion);
3234 monitor_read_command(mon, 0);
3237 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3238 mon);
3240 LIST_INSERT_HEAD(&mon_list, mon, entry);
3241 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3242 cur_mon = mon;
3245 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3247 BlockDriverState *bs = opaque;
3248 int ret = 0;
3250 if (bdrv_set_key(bs, password) != 0) {
3251 monitor_printf(mon, "invalid password\n");
3252 ret = -EPERM;
3254 if (mon->password_completion_cb)
3255 mon->password_completion_cb(mon->password_opaque, ret);
3257 monitor_read_command(mon, 1);
3260 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3261 BlockDriverCompletionFunc *completion_cb,
3262 void *opaque)
3264 int err;
3266 if (!bdrv_key_required(bs)) {
3267 if (completion_cb)
3268 completion_cb(opaque, 0);
3269 return;
3272 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3273 bdrv_get_encrypted_filename(bs));
3275 mon->password_completion_cb = completion_cb;
3276 mon->password_opaque = opaque;
3278 err = monitor_read_password(mon, bdrv_password_cb, bs);
3280 if (err && completion_cb)
3281 completion_cb(opaque, err);