libkvm: remove ppc functions from callbacks
[qemu-kvm/fedora.git] / monitor.c
blob8d5165cc7663b864d583e44d7f761ee7ff66ed73
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 struct Monitor {
77 CharDriverState *chr;
78 int flags;
79 int suspend_cnt;
80 uint8_t outbuf[1024];
81 int outbuf_index;
82 ReadLineState *rs;
83 CPUState *mon_cpu;
84 BlockDriverCompletionFunc *password_completion_cb;
85 void *password_opaque;
86 LIST_ENTRY(Monitor) entry;
89 static LIST_HEAD(mon_list, Monitor) mon_list;
91 static const mon_cmd_t mon_cmds[];
92 static const mon_cmd_t info_cmds[];
94 Monitor *cur_mon = NULL;
96 static void monitor_command_cb(Monitor *mon, const char *cmdline,
97 void *opaque);
99 static void monitor_read_command(Monitor *mon, int show_prompt)
101 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
102 if (show_prompt)
103 readline_show_prompt(mon->rs);
106 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
107 void *opaque)
109 if (mon->rs) {
110 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
111 /* prompt is printed on return from the command handler */
112 return 0;
113 } else {
114 monitor_printf(mon, "terminal does not support password prompting\n");
115 return -ENOTTY;
119 void monitor_flush(Monitor *mon)
121 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
122 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
123 mon->outbuf_index = 0;
127 /* flush at every end of line or if the buffer is full */
128 static void monitor_puts(Monitor *mon, const char *str)
130 char c;
132 if (!mon)
133 return;
135 for(;;) {
136 c = *str++;
137 if (c == '\0')
138 break;
139 if (c == '\n')
140 mon->outbuf[mon->outbuf_index++] = '\r';
141 mon->outbuf[mon->outbuf_index++] = c;
142 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
143 || c == '\n')
144 monitor_flush(mon);
148 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
150 char buf[4096];
151 vsnprintf(buf, sizeof(buf), fmt, ap);
152 monitor_puts(mon, buf);
155 void monitor_printf(Monitor *mon, const char *fmt, ...)
157 va_list ap;
158 va_start(ap, fmt);
159 monitor_vprintf(mon, fmt, ap);
160 va_end(ap);
163 void monitor_print_filename(Monitor *mon, const char *filename)
165 int i;
167 for (i = 0; filename[i]; i++) {
168 switch (filename[i]) {
169 case ' ':
170 case '"':
171 case '\\':
172 monitor_printf(mon, "\\%c", filename[i]);
173 break;
174 case '\t':
175 monitor_printf(mon, "\\t");
176 break;
177 case '\r':
178 monitor_printf(mon, "\\r");
179 break;
180 case '\n':
181 monitor_printf(mon, "\\n");
182 break;
183 default:
184 monitor_printf(mon, "%c", filename[i]);
185 break;
190 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
192 va_list ap;
193 va_start(ap, fmt);
194 monitor_vprintf((Monitor *)stream, fmt, ap);
195 va_end(ap);
196 return 0;
199 static int compare_cmd(const char *name, const char *list)
201 const char *p, *pstart;
202 int len;
203 len = strlen(name);
204 p = list;
205 for(;;) {
206 pstart = p;
207 p = strchr(p, '|');
208 if (!p)
209 p = pstart + strlen(pstart);
210 if ((p - pstart) == len && !memcmp(pstart, name, len))
211 return 1;
212 if (*p == '\0')
213 break;
214 p++;
216 return 0;
219 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
220 const char *prefix, const char *name)
222 const mon_cmd_t *cmd;
224 for(cmd = cmds; cmd->name != NULL; cmd++) {
225 if (!name || !strcmp(name, cmd->name))
226 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
227 cmd->params, cmd->help);
231 static void help_cmd(Monitor *mon, const char *name)
233 if (name && !strcmp(name, "info")) {
234 help_cmd_dump(mon, info_cmds, "info ", NULL);
235 } else {
236 help_cmd_dump(mon, mon_cmds, "", name);
237 if (name && !strcmp(name, "log")) {
238 const CPULogItem *item;
239 monitor_printf(mon, "Log items (comma separated):\n");
240 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
241 for(item = cpu_log_items; item->mask != 0; item++) {
242 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
248 static void do_commit(Monitor *mon, const char *device)
250 int i, all_devices;
252 all_devices = !strcmp(device, "all");
253 for (i = 0; i < nb_drives; i++) {
254 if (all_devices ||
255 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
256 bdrv_commit(drives_table[i].bdrv);
260 static void do_info(Monitor *mon, const char *item)
262 const mon_cmd_t *cmd;
263 void (*handler)(Monitor *);
265 if (!item)
266 goto help;
267 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
268 if (compare_cmd(item, cmd->name))
269 goto found;
271 help:
272 help_cmd(mon, "info");
273 return;
274 found:
275 handler = cmd->handler;
276 handler(mon);
279 static void do_info_version(Monitor *mon)
281 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
284 static void do_info_name(Monitor *mon)
286 if (qemu_name)
287 monitor_printf(mon, "%s\n", qemu_name);
290 #if defined(TARGET_I386)
291 static void do_info_hpet(Monitor *mon)
293 monitor_printf(mon, "HPET is %s by QEMU\n",
294 (no_hpet) ? "disabled" : "enabled");
296 #endif
298 static void do_info_uuid(Monitor *mon)
300 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
301 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
302 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
303 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
304 qemu_uuid[14], qemu_uuid[15]);
307 /* get the current CPU defined by the user */
308 static int mon_set_cpu(int cpu_index)
310 CPUState *env;
312 for(env = first_cpu; env != NULL; env = env->next_cpu) {
313 if (env->cpu_index == cpu_index) {
314 cur_mon->mon_cpu = env;
315 return 0;
318 return -1;
321 static CPUState *mon_get_cpu(void)
323 if (!cur_mon->mon_cpu) {
324 mon_set_cpu(0);
326 cpu_synchronize_state(cur_mon->mon_cpu, 0);
327 return cur_mon->mon_cpu;
330 static void do_info_registers(Monitor *mon)
332 CPUState *env;
333 env = mon_get_cpu();
334 if (!env)
335 return;
336 #ifdef TARGET_I386
337 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
338 X86_DUMP_FPU);
339 #else
340 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
342 #endif
345 static void do_info_cpus(Monitor *mon)
347 CPUState *env;
349 /* just to set the default cpu if not already done */
350 mon_get_cpu();
352 for(env = first_cpu; env != NULL; env = env->next_cpu) {
353 cpu_synchronize_state(env, 0);
354 monitor_printf(mon, "%c CPU #%d:",
355 (env == mon->mon_cpu) ? '*' : ' ',
356 env->cpu_index);
357 #if defined(TARGET_I386)
358 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
359 env->eip + env->segs[R_CS].base);
360 #elif defined(TARGET_PPC)
361 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
362 #elif defined(TARGET_SPARC)
363 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
364 env->pc, env->npc);
365 #elif defined(TARGET_MIPS)
366 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
367 #endif
368 if (env->halted)
369 monitor_printf(mon, " (halted)");
370 monitor_printf(mon," thread_id=%d", env->thread_id);
371 monitor_printf(mon, "\n");
375 static void do_cpu_set(Monitor *mon, int index)
377 if (mon_set_cpu(index) < 0)
378 monitor_printf(mon, "Invalid CPU index\n");
381 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
383 int state;
385 if (!strcmp(status, "online"))
386 state = 1;
387 else if (!strcmp(status, "offline"))
388 state = 0;
389 else {
390 monitor_printf(mon, "invalid status: %s\n", status);
391 return;
393 #if defined(TARGET_I386) || defined(TARGET_X86_64)
394 qemu_system_cpu_hot_add(value, state);
395 #endif
398 static void do_info_jit(Monitor *mon)
400 dump_exec_info((FILE *)mon, monitor_fprintf);
403 static void do_info_history(Monitor *mon)
405 int i;
406 const char *str;
408 if (!mon->rs)
409 return;
410 i = 0;
411 for(;;) {
412 str = readline_get_history(mon->rs, i);
413 if (!str)
414 break;
415 monitor_printf(mon, "%d: '%s'\n", i, str);
416 i++;
420 #if defined(TARGET_PPC)
421 /* XXX: not implemented in other targets */
422 static void do_info_cpu_stats(Monitor *mon)
424 CPUState *env;
426 env = mon_get_cpu();
427 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
429 #endif
431 static void do_quit(Monitor *mon)
433 exit(0);
436 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
438 if (bdrv_is_inserted(bs)) {
439 if (!force) {
440 if (!bdrv_is_removable(bs)) {
441 monitor_printf(mon, "device is not removable\n");
442 return -1;
444 if (bdrv_is_locked(bs)) {
445 monitor_printf(mon, "device is locked\n");
446 return -1;
449 bdrv_close(bs);
451 return 0;
454 static void do_eject(Monitor *mon, int force, const char *filename)
456 BlockDriverState *bs;
458 bs = bdrv_find(filename);
459 if (!bs) {
460 monitor_printf(mon, "device not found\n");
461 return;
463 eject_device(mon, bs, force);
466 static void do_change_block(Monitor *mon, const char *device,
467 const char *filename, const char *fmt)
469 BlockDriverState *bs;
470 BlockDriver *drv = NULL;
472 bs = bdrv_find(device);
473 if (!bs) {
474 monitor_printf(mon, "device not found\n");
475 return;
477 if (fmt) {
478 drv = bdrv_find_format(fmt);
479 if (!drv) {
480 monitor_printf(mon, "invalid format %s\n", fmt);
481 return;
484 if (eject_device(mon, bs, 0) < 0)
485 return;
486 bdrv_open2(bs, filename, 0, drv);
487 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
490 static void change_vnc_password_cb(Monitor *mon, const char *password,
491 void *opaque)
493 if (vnc_display_password(NULL, password) < 0)
494 monitor_printf(mon, "could not set VNC server password\n");
496 monitor_read_command(mon, 1);
499 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
501 if (strcmp(target, "passwd") == 0 ||
502 strcmp(target, "password") == 0) {
503 if (arg) {
504 char password[9];
505 strncpy(password, arg, sizeof(password));
506 password[sizeof(password) - 1] = '\0';
507 change_vnc_password_cb(mon, password, NULL);
508 } else {
509 monitor_read_password(mon, change_vnc_password_cb, NULL);
511 } else {
512 if (vnc_display_open(NULL, target) < 0)
513 monitor_printf(mon, "could not start VNC server on %s\n", target);
517 static void do_change(Monitor *mon, const char *device, const char *target,
518 const char *arg)
520 if (strcmp(device, "vnc") == 0) {
521 do_change_vnc(mon, target, arg);
522 } else {
523 do_change_block(mon, device, target, arg);
527 static void do_screen_dump(Monitor *mon, const char *filename)
529 vga_hw_screen_dump(filename);
532 static void do_logfile(Monitor *mon, const char *filename)
534 cpu_set_log_filename(filename);
537 static void do_log(Monitor *mon, const char *items)
539 int mask;
541 if (!strcmp(items, "none")) {
542 mask = 0;
543 } else {
544 mask = cpu_str_to_log_mask(items);
545 if (!mask) {
546 help_cmd(mon, "log");
547 return;
550 cpu_set_log(mask);
553 static void do_singlestep(Monitor *mon, const char *option)
555 if (!option || !strcmp(option, "on")) {
556 singlestep = 1;
557 } else if (!strcmp(option, "off")) {
558 singlestep = 0;
559 } else {
560 monitor_printf(mon, "unexpected option %s\n", option);
564 static void do_stop(Monitor *mon)
566 vm_stop(EXCP_INTERRUPT);
569 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
571 struct bdrv_iterate_context {
572 Monitor *mon;
573 int err;
576 static void do_cont(Monitor *mon)
578 struct bdrv_iterate_context context = { mon, 0 };
580 bdrv_iterate(encrypted_bdrv_it, &context);
581 /* only resume the vm if all keys are set and valid */
582 if (!context.err)
583 vm_start();
586 static void bdrv_key_cb(void *opaque, int err)
588 Monitor *mon = opaque;
590 /* another key was set successfully, retry to continue */
591 if (!err)
592 do_cont(mon);
595 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
597 struct bdrv_iterate_context *context = opaque;
599 if (!context->err && bdrv_key_required(bs)) {
600 context->err = -EBUSY;
601 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
602 context->mon);
606 static void do_gdbserver(Monitor *mon, const char *device)
608 if (!device)
609 device = "tcp::" DEFAULT_GDBSTUB_PORT;
610 if (gdbserver_start(device) < 0) {
611 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
612 device);
613 } else if (strcmp(device, "none") == 0) {
614 monitor_printf(mon, "Disabled gdbserver\n");
615 } else {
616 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
617 device);
621 static void do_watchdog_action(Monitor *mon, const char *action)
623 if (select_watchdog_action(action) == -1) {
624 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
628 static void monitor_printc(Monitor *mon, int c)
630 monitor_printf(mon, "'");
631 switch(c) {
632 case '\'':
633 monitor_printf(mon, "\\'");
634 break;
635 case '\\':
636 monitor_printf(mon, "\\\\");
637 break;
638 case '\n':
639 monitor_printf(mon, "\\n");
640 break;
641 case '\r':
642 monitor_printf(mon, "\\r");
643 break;
644 default:
645 if (c >= 32 && c <= 126) {
646 monitor_printf(mon, "%c", c);
647 } else {
648 monitor_printf(mon, "\\x%02x", c);
650 break;
652 monitor_printf(mon, "'");
655 static void memory_dump(Monitor *mon, int count, int format, int wsize,
656 target_phys_addr_t addr, int is_physical)
658 CPUState *env;
659 int nb_per_line, l, line_size, i, max_digits, len;
660 uint8_t buf[16];
661 uint64_t v;
663 if (format == 'i') {
664 int flags;
665 flags = 0;
666 env = mon_get_cpu();
667 if (!env && !is_physical)
668 return;
669 #ifdef TARGET_I386
670 if (wsize == 2) {
671 flags = 1;
672 } else if (wsize == 4) {
673 flags = 0;
674 } else {
675 /* as default we use the current CS size */
676 flags = 0;
677 if (env) {
678 #ifdef TARGET_X86_64
679 if ((env->efer & MSR_EFER_LMA) &&
680 (env->segs[R_CS].flags & DESC_L_MASK))
681 flags = 2;
682 else
683 #endif
684 if (!(env->segs[R_CS].flags & DESC_B_MASK))
685 flags = 1;
688 #endif
689 monitor_disas(mon, env, addr, count, is_physical, flags);
690 return;
693 len = wsize * count;
694 if (wsize == 1)
695 line_size = 8;
696 else
697 line_size = 16;
698 nb_per_line = line_size / wsize;
699 max_digits = 0;
701 switch(format) {
702 case 'o':
703 max_digits = (wsize * 8 + 2) / 3;
704 break;
705 default:
706 case 'x':
707 max_digits = (wsize * 8) / 4;
708 break;
709 case 'u':
710 case 'd':
711 max_digits = (wsize * 8 * 10 + 32) / 33;
712 break;
713 case 'c':
714 wsize = 1;
715 break;
718 while (len > 0) {
719 if (is_physical)
720 monitor_printf(mon, TARGET_FMT_plx ":", addr);
721 else
722 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
723 l = len;
724 if (l > line_size)
725 l = line_size;
726 if (is_physical) {
727 cpu_physical_memory_rw(addr, buf, l, 0);
728 } else {
729 env = mon_get_cpu();
730 if (!env)
731 break;
732 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
733 monitor_printf(mon, " Cannot access memory\n");
734 break;
737 i = 0;
738 while (i < l) {
739 switch(wsize) {
740 default:
741 case 1:
742 v = ldub_raw(buf + i);
743 break;
744 case 2:
745 v = lduw_raw(buf + i);
746 break;
747 case 4:
748 v = (uint32_t)ldl_raw(buf + i);
749 break;
750 case 8:
751 v = ldq_raw(buf + i);
752 break;
754 monitor_printf(mon, " ");
755 switch(format) {
756 case 'o':
757 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
758 break;
759 case 'x':
760 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
761 break;
762 case 'u':
763 monitor_printf(mon, "%*" PRIu64, max_digits, v);
764 break;
765 case 'd':
766 monitor_printf(mon, "%*" PRId64, max_digits, v);
767 break;
768 case 'c':
769 monitor_printc(mon, v);
770 break;
772 i += wsize;
774 monitor_printf(mon, "\n");
775 addr += l;
776 len -= l;
780 #if TARGET_LONG_BITS == 64
781 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
782 #else
783 #define GET_TLONG(h, l) (l)
784 #endif
786 static void do_memory_dump(Monitor *mon, int count, int format, int size,
787 uint32_t addrh, uint32_t addrl)
789 target_long addr = GET_TLONG(addrh, addrl);
790 memory_dump(mon, count, format, size, addr, 0);
793 #if TARGET_PHYS_ADDR_BITS > 32
794 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
795 #else
796 #define GET_TPHYSADDR(h, l) (l)
797 #endif
799 static void do_physical_memory_dump(Monitor *mon, int count, int format,
800 int size, uint32_t addrh, uint32_t addrl)
803 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
804 memory_dump(mon, count, format, size, addr, 1);
807 static void do_print(Monitor *mon, int count, int format, int size,
808 unsigned int valh, unsigned int vall)
810 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
811 #if TARGET_PHYS_ADDR_BITS == 32
812 switch(format) {
813 case 'o':
814 monitor_printf(mon, "%#o", val);
815 break;
816 case 'x':
817 monitor_printf(mon, "%#x", val);
818 break;
819 case 'u':
820 monitor_printf(mon, "%u", val);
821 break;
822 default:
823 case 'd':
824 monitor_printf(mon, "%d", val);
825 break;
826 case 'c':
827 monitor_printc(mon, val);
828 break;
830 #else
831 switch(format) {
832 case 'o':
833 monitor_printf(mon, "%#" PRIo64, val);
834 break;
835 case 'x':
836 monitor_printf(mon, "%#" PRIx64, val);
837 break;
838 case 'u':
839 monitor_printf(mon, "%" PRIu64, val);
840 break;
841 default:
842 case 'd':
843 monitor_printf(mon, "%" PRId64, val);
844 break;
845 case 'c':
846 monitor_printc(mon, val);
847 break;
849 #endif
850 monitor_printf(mon, "\n");
853 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
854 uint32_t size, const char *filename)
856 FILE *f;
857 target_long addr = GET_TLONG(valh, vall);
858 uint32_t l;
859 CPUState *env;
860 uint8_t buf[1024];
862 env = mon_get_cpu();
863 if (!env)
864 return;
866 f = fopen(filename, "wb");
867 if (!f) {
868 monitor_printf(mon, "could not open '%s'\n", filename);
869 return;
871 while (size != 0) {
872 l = sizeof(buf);
873 if (l > size)
874 l = size;
875 cpu_memory_rw_debug(env, addr, buf, l, 0);
876 fwrite(buf, 1, l, f);
877 addr += l;
878 size -= l;
880 fclose(f);
883 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
884 unsigned int vall, uint32_t size,
885 const char *filename)
887 FILE *f;
888 uint32_t l;
889 uint8_t buf[1024];
890 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
892 f = fopen(filename, "wb");
893 if (!f) {
894 monitor_printf(mon, "could not open '%s'\n", filename);
895 return;
897 while (size != 0) {
898 l = sizeof(buf);
899 if (l > size)
900 l = size;
901 cpu_physical_memory_rw(addr, buf, l, 0);
902 fwrite(buf, 1, l, f);
903 fflush(f);
904 addr += l;
905 size -= l;
907 fclose(f);
910 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
912 uint32_t addr;
913 uint8_t buf[1];
914 uint16_t sum;
916 sum = 0;
917 for(addr = start; addr < (start + size); addr++) {
918 cpu_physical_memory_rw(addr, buf, 1, 0);
919 /* BSD sum algorithm ('sum' Unix command) */
920 sum = (sum >> 1) | (sum << 15);
921 sum += buf[0];
923 monitor_printf(mon, "%05d\n", sum);
926 typedef struct {
927 int keycode;
928 const char *name;
929 } KeyDef;
931 static const KeyDef key_defs[] = {
932 { 0x2a, "shift" },
933 { 0x36, "shift_r" },
935 { 0x38, "alt" },
936 { 0xb8, "alt_r" },
937 { 0x64, "altgr" },
938 { 0xe4, "altgr_r" },
939 { 0x1d, "ctrl" },
940 { 0x9d, "ctrl_r" },
942 { 0xdd, "menu" },
944 { 0x01, "esc" },
946 { 0x02, "1" },
947 { 0x03, "2" },
948 { 0x04, "3" },
949 { 0x05, "4" },
950 { 0x06, "5" },
951 { 0x07, "6" },
952 { 0x08, "7" },
953 { 0x09, "8" },
954 { 0x0a, "9" },
955 { 0x0b, "0" },
956 { 0x0c, "minus" },
957 { 0x0d, "equal" },
958 { 0x0e, "backspace" },
960 { 0x0f, "tab" },
961 { 0x10, "q" },
962 { 0x11, "w" },
963 { 0x12, "e" },
964 { 0x13, "r" },
965 { 0x14, "t" },
966 { 0x15, "y" },
967 { 0x16, "u" },
968 { 0x17, "i" },
969 { 0x18, "o" },
970 { 0x19, "p" },
972 { 0x1c, "ret" },
974 { 0x1e, "a" },
975 { 0x1f, "s" },
976 { 0x20, "d" },
977 { 0x21, "f" },
978 { 0x22, "g" },
979 { 0x23, "h" },
980 { 0x24, "j" },
981 { 0x25, "k" },
982 { 0x26, "l" },
984 { 0x2c, "z" },
985 { 0x2d, "x" },
986 { 0x2e, "c" },
987 { 0x2f, "v" },
988 { 0x30, "b" },
989 { 0x31, "n" },
990 { 0x32, "m" },
991 { 0x33, "comma" },
992 { 0x34, "dot" },
993 { 0x35, "slash" },
995 { 0x37, "asterisk" },
997 { 0x39, "spc" },
998 { 0x3a, "caps_lock" },
999 { 0x3b, "f1" },
1000 { 0x3c, "f2" },
1001 { 0x3d, "f3" },
1002 { 0x3e, "f4" },
1003 { 0x3f, "f5" },
1004 { 0x40, "f6" },
1005 { 0x41, "f7" },
1006 { 0x42, "f8" },
1007 { 0x43, "f9" },
1008 { 0x44, "f10" },
1009 { 0x45, "num_lock" },
1010 { 0x46, "scroll_lock" },
1012 { 0xb5, "kp_divide" },
1013 { 0x37, "kp_multiply" },
1014 { 0x4a, "kp_subtract" },
1015 { 0x4e, "kp_add" },
1016 { 0x9c, "kp_enter" },
1017 { 0x53, "kp_decimal" },
1018 { 0x54, "sysrq" },
1020 { 0x52, "kp_0" },
1021 { 0x4f, "kp_1" },
1022 { 0x50, "kp_2" },
1023 { 0x51, "kp_3" },
1024 { 0x4b, "kp_4" },
1025 { 0x4c, "kp_5" },
1026 { 0x4d, "kp_6" },
1027 { 0x47, "kp_7" },
1028 { 0x48, "kp_8" },
1029 { 0x49, "kp_9" },
1031 { 0x56, "<" },
1033 { 0x57, "f11" },
1034 { 0x58, "f12" },
1036 { 0xb7, "print" },
1038 { 0xc7, "home" },
1039 { 0xc9, "pgup" },
1040 { 0xd1, "pgdn" },
1041 { 0xcf, "end" },
1043 { 0xcb, "left" },
1044 { 0xc8, "up" },
1045 { 0xd0, "down" },
1046 { 0xcd, "right" },
1048 { 0xd2, "insert" },
1049 { 0xd3, "delete" },
1050 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1051 { 0xf0, "stop" },
1052 { 0xf1, "again" },
1053 { 0xf2, "props" },
1054 { 0xf3, "undo" },
1055 { 0xf4, "front" },
1056 { 0xf5, "copy" },
1057 { 0xf6, "open" },
1058 { 0xf7, "paste" },
1059 { 0xf8, "find" },
1060 { 0xf9, "cut" },
1061 { 0xfa, "lf" },
1062 { 0xfb, "help" },
1063 { 0xfc, "meta_l" },
1064 { 0xfd, "meta_r" },
1065 { 0xfe, "compose" },
1066 #endif
1067 { 0, NULL },
1070 static int get_keycode(const char *key)
1072 const KeyDef *p;
1073 char *endp;
1074 int ret;
1076 for(p = key_defs; p->name != NULL; p++) {
1077 if (!strcmp(key, p->name))
1078 return p->keycode;
1080 if (strstart(key, "0x", NULL)) {
1081 ret = strtoul(key, &endp, 0);
1082 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1083 return ret;
1085 return -1;
1088 #define MAX_KEYCODES 16
1089 static uint8_t keycodes[MAX_KEYCODES];
1090 static int nb_pending_keycodes;
1091 static QEMUTimer *key_timer;
1093 static void release_keys(void *opaque)
1095 int keycode;
1097 while (nb_pending_keycodes > 0) {
1098 nb_pending_keycodes--;
1099 keycode = keycodes[nb_pending_keycodes];
1100 if (keycode & 0x80)
1101 kbd_put_keycode(0xe0);
1102 kbd_put_keycode(keycode | 0x80);
1106 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1107 int hold_time)
1109 char keyname_buf[16];
1110 char *separator;
1111 int keyname_len, keycode, i;
1113 if (nb_pending_keycodes > 0) {
1114 qemu_del_timer(key_timer);
1115 release_keys(NULL);
1117 if (!has_hold_time)
1118 hold_time = 100;
1119 i = 0;
1120 while (1) {
1121 separator = strchr(string, '-');
1122 keyname_len = separator ? separator - string : strlen(string);
1123 if (keyname_len > 0) {
1124 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1125 if (keyname_len > sizeof(keyname_buf) - 1) {
1126 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1127 return;
1129 if (i == MAX_KEYCODES) {
1130 monitor_printf(mon, "too many keys\n");
1131 return;
1133 keyname_buf[keyname_len] = 0;
1134 keycode = get_keycode(keyname_buf);
1135 if (keycode < 0) {
1136 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1137 return;
1139 keycodes[i++] = keycode;
1141 if (!separator)
1142 break;
1143 string = separator + 1;
1145 nb_pending_keycodes = i;
1146 /* key down events */
1147 for (i = 0; i < nb_pending_keycodes; i++) {
1148 keycode = keycodes[i];
1149 if (keycode & 0x80)
1150 kbd_put_keycode(0xe0);
1151 kbd_put_keycode(keycode & 0x7f);
1153 /* delayed key up events */
1154 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1155 muldiv64(ticks_per_sec, hold_time, 1000));
1158 static int mouse_button_state;
1160 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1161 const char *dz_str)
1163 int dx, dy, dz;
1164 dx = strtol(dx_str, NULL, 0);
1165 dy = strtol(dy_str, NULL, 0);
1166 dz = 0;
1167 if (dz_str)
1168 dz = strtol(dz_str, NULL, 0);
1169 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1172 static void do_mouse_button(Monitor *mon, int button_state)
1174 mouse_button_state = button_state;
1175 kbd_mouse_event(0, 0, 0, mouse_button_state);
1178 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1179 int addr, int has_index, int index)
1181 uint32_t val;
1182 int suffix;
1184 if (has_index) {
1185 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1186 addr++;
1188 addr &= 0xffff;
1190 switch(size) {
1191 default:
1192 case 1:
1193 val = cpu_inb(NULL, addr);
1194 suffix = 'b';
1195 break;
1196 case 2:
1197 val = cpu_inw(NULL, addr);
1198 suffix = 'w';
1199 break;
1200 case 4:
1201 val = cpu_inl(NULL, addr);
1202 suffix = 'l';
1203 break;
1205 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1206 suffix, addr, size * 2, val);
1209 /* boot_set handler */
1210 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1211 static void *boot_opaque;
1213 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1215 qemu_boot_set_handler = func;
1216 boot_opaque = opaque;
1219 static void do_boot_set(Monitor *mon, const char *bootdevice)
1221 int res;
1223 if (qemu_boot_set_handler) {
1224 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1225 if (res == 0)
1226 monitor_printf(mon, "boot device list now set to %s\n",
1227 bootdevice);
1228 else
1229 monitor_printf(mon, "setting boot device list failed with "
1230 "error %i\n", res);
1231 } else {
1232 monitor_printf(mon, "no function defined to set boot device list for "
1233 "this architecture\n");
1237 static void do_system_reset(Monitor *mon)
1239 qemu_system_reset_request();
1242 static void do_system_powerdown(Monitor *mon)
1244 qemu_system_powerdown_request();
1247 #if defined(TARGET_I386)
1248 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1250 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1251 addr,
1252 pte & mask,
1253 pte & PG_GLOBAL_MASK ? 'G' : '-',
1254 pte & PG_PSE_MASK ? 'P' : '-',
1255 pte & PG_DIRTY_MASK ? 'D' : '-',
1256 pte & PG_ACCESSED_MASK ? 'A' : '-',
1257 pte & PG_PCD_MASK ? 'C' : '-',
1258 pte & PG_PWT_MASK ? 'T' : '-',
1259 pte & PG_USER_MASK ? 'U' : '-',
1260 pte & PG_RW_MASK ? 'W' : '-');
1263 static void tlb_info(Monitor *mon)
1265 CPUState *env;
1266 int l1, l2;
1267 uint32_t pgd, pde, pte;
1269 env = mon_get_cpu();
1270 if (!env)
1271 return;
1273 if (!(env->cr[0] & CR0_PG_MASK)) {
1274 monitor_printf(mon, "PG disabled\n");
1275 return;
1277 pgd = env->cr[3] & ~0xfff;
1278 for(l1 = 0; l1 < 1024; l1++) {
1279 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1280 pde = le32_to_cpu(pde);
1281 if (pde & PG_PRESENT_MASK) {
1282 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1283 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1284 } else {
1285 for(l2 = 0; l2 < 1024; l2++) {
1286 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1287 (uint8_t *)&pte, 4);
1288 pte = le32_to_cpu(pte);
1289 if (pte & PG_PRESENT_MASK) {
1290 print_pte(mon, (l1 << 22) + (l2 << 12),
1291 pte & ~PG_PSE_MASK,
1292 ~0xfff);
1300 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1301 uint32_t end, int prot)
1303 int prot1;
1304 prot1 = *plast_prot;
1305 if (prot != prot1) {
1306 if (*pstart != -1) {
1307 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1308 *pstart, end, end - *pstart,
1309 prot1 & PG_USER_MASK ? 'u' : '-',
1310 'r',
1311 prot1 & PG_RW_MASK ? 'w' : '-');
1313 if (prot != 0)
1314 *pstart = end;
1315 else
1316 *pstart = -1;
1317 *plast_prot = prot;
1321 static void mem_info(Monitor *mon)
1323 CPUState *env;
1324 int l1, l2, prot, last_prot;
1325 uint32_t pgd, pde, pte, start, end;
1327 env = mon_get_cpu();
1328 if (!env)
1329 return;
1331 if (!(env->cr[0] & CR0_PG_MASK)) {
1332 monitor_printf(mon, "PG disabled\n");
1333 return;
1335 pgd = env->cr[3] & ~0xfff;
1336 last_prot = 0;
1337 start = -1;
1338 for(l1 = 0; l1 < 1024; l1++) {
1339 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1340 pde = le32_to_cpu(pde);
1341 end = l1 << 22;
1342 if (pde & PG_PRESENT_MASK) {
1343 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1344 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1345 mem_print(mon, &start, &last_prot, end, prot);
1346 } else {
1347 for(l2 = 0; l2 < 1024; l2++) {
1348 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1349 (uint8_t *)&pte, 4);
1350 pte = le32_to_cpu(pte);
1351 end = (l1 << 22) + (l2 << 12);
1352 if (pte & PG_PRESENT_MASK) {
1353 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1354 } else {
1355 prot = 0;
1357 mem_print(mon, &start, &last_prot, end, prot);
1360 } else {
1361 prot = 0;
1362 mem_print(mon, &start, &last_prot, end, prot);
1366 #endif
1368 #if defined(TARGET_SH4)
1370 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1372 monitor_printf(mon, " tlb%i:\t"
1373 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1374 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1375 "dirty=%hhu writethrough=%hhu\n",
1376 idx,
1377 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1378 tlb->v, tlb->sh, tlb->c, tlb->pr,
1379 tlb->d, tlb->wt);
1382 static void tlb_info(Monitor *mon)
1384 CPUState *env = mon_get_cpu();
1385 int i;
1387 monitor_printf (mon, "ITLB:\n");
1388 for (i = 0 ; i < ITLB_SIZE ; i++)
1389 print_tlb (mon, i, &env->itlb[i]);
1390 monitor_printf (mon, "UTLB:\n");
1391 for (i = 0 ; i < UTLB_SIZE ; i++)
1392 print_tlb (mon, i, &env->utlb[i]);
1395 #endif
1397 static void do_info_kqemu(Monitor *mon)
1399 #ifdef CONFIG_KQEMU
1400 CPUState *env;
1401 int val;
1402 val = 0;
1403 env = mon_get_cpu();
1404 if (!env) {
1405 monitor_printf(mon, "No cpu initialized yet");
1406 return;
1408 val = env->kqemu_enabled;
1409 monitor_printf(mon, "kqemu support: ");
1410 switch(val) {
1411 default:
1412 case 0:
1413 monitor_printf(mon, "disabled\n");
1414 break;
1415 case 1:
1416 monitor_printf(mon, "enabled for user code\n");
1417 break;
1418 case 2:
1419 monitor_printf(mon, "enabled for user and kernel code\n");
1420 break;
1422 #else
1423 monitor_printf(mon, "kqemu support: not compiled\n");
1424 #endif
1427 static void do_info_kvm(Monitor *mon)
1429 #if defined(USE_KVM) || defined(CONFIG_KVM)
1430 monitor_printf(mon, "kvm support: ");
1431 if (kvm_enabled())
1432 monitor_printf(mon, "enabled\n");
1433 else
1434 monitor_printf(mon, "disabled\n");
1435 #else
1436 monitor_printf(mon, "kvm support: not compiled\n");
1437 #endif
1440 static void do_info_numa(Monitor *mon)
1442 int i;
1443 CPUState *env;
1445 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1446 for (i = 0; i < nb_numa_nodes; i++) {
1447 monitor_printf(mon, "node %d cpus:", i);
1448 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1449 if (env->numa_node == i) {
1450 monitor_printf(mon, " %d", env->cpu_index);
1453 monitor_printf(mon, "\n");
1454 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1455 node_mem[i] >> 20);
1459 #ifdef CONFIG_PROFILER
1461 int64_t kqemu_time;
1462 int64_t qemu_time;
1463 int64_t kqemu_exec_count;
1464 int64_t dev_time;
1465 int64_t kqemu_ret_int_count;
1466 int64_t kqemu_ret_excp_count;
1467 int64_t kqemu_ret_intr_count;
1469 static void do_info_profile(Monitor *mon)
1471 int64_t total;
1472 total = qemu_time;
1473 if (total == 0)
1474 total = 1;
1475 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1476 dev_time, dev_time / (double)ticks_per_sec);
1477 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1478 qemu_time, qemu_time / (double)ticks_per_sec);
1479 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1480 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1481 PRId64 "\n",
1482 kqemu_time, kqemu_time / (double)ticks_per_sec,
1483 kqemu_time / (double)total * 100.0,
1484 kqemu_exec_count,
1485 kqemu_ret_int_count,
1486 kqemu_ret_excp_count,
1487 kqemu_ret_intr_count);
1488 qemu_time = 0;
1489 kqemu_time = 0;
1490 kqemu_exec_count = 0;
1491 dev_time = 0;
1492 kqemu_ret_int_count = 0;
1493 kqemu_ret_excp_count = 0;
1494 kqemu_ret_intr_count = 0;
1495 #ifdef CONFIG_KQEMU
1496 kqemu_record_dump();
1497 #endif
1499 #else
1500 static void do_info_profile(Monitor *mon)
1502 monitor_printf(mon, "Internal profiler not compiled\n");
1504 #endif
1506 /* Capture support */
1507 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1509 static void do_info_capture(Monitor *mon)
1511 int i;
1512 CaptureState *s;
1514 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1515 monitor_printf(mon, "[%d]: ", i);
1516 s->ops.info (s->opaque);
1520 #ifdef HAS_AUDIO
1521 static void do_stop_capture(Monitor *mon, int n)
1523 int i;
1524 CaptureState *s;
1526 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1527 if (i == n) {
1528 s->ops.destroy (s->opaque);
1529 LIST_REMOVE (s, entries);
1530 qemu_free (s);
1531 return;
1536 static void do_wav_capture(Monitor *mon, const char *path,
1537 int has_freq, int freq,
1538 int has_bits, int bits,
1539 int has_channels, int nchannels)
1541 CaptureState *s;
1543 s = qemu_mallocz (sizeof (*s));
1545 freq = has_freq ? freq : 44100;
1546 bits = has_bits ? bits : 16;
1547 nchannels = has_channels ? nchannels : 2;
1549 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1550 monitor_printf(mon, "Faied to add wave capture\n");
1551 qemu_free (s);
1553 LIST_INSERT_HEAD (&capture_head, s, entries);
1555 #endif
1557 #if defined(TARGET_I386)
1558 static void do_inject_nmi(Monitor *mon, int cpu_index)
1560 CPUState *env;
1562 for (env = first_cpu; env != NULL; env = env->next_cpu)
1563 if (env->cpu_index == cpu_index) {
1564 if (kvm_enabled())
1565 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1566 else
1567 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1568 break;
1571 #endif
1573 static void do_info_status(Monitor *mon)
1575 if (vm_running) {
1576 if (singlestep) {
1577 monitor_printf(mon, "VM status: running (single step mode)\n");
1578 } else {
1579 monitor_printf(mon, "VM status: running\n");
1581 } else
1582 monitor_printf(mon, "VM status: paused\n");
1586 static void do_balloon(Monitor *mon, int value)
1588 ram_addr_t target = value;
1589 qemu_balloon(target << 20);
1592 static void do_info_balloon(Monitor *mon)
1594 ram_addr_t actual;
1596 actual = qemu_balloon_status();
1597 if (kvm_enabled() && !kvm_has_sync_mmu())
1598 monitor_printf(mon, "Using KVM without synchronous MMU, "
1599 "ballooning disabled\n");
1600 else if (actual == 0)
1601 monitor_printf(mon, "Ballooning not activated in VM\n");
1602 else
1603 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1606 static void do_acl(Monitor *mon,
1607 const char *command,
1608 const char *aclname,
1609 const char *match,
1610 int has_index,
1611 int index)
1613 qemu_acl *acl;
1615 acl = qemu_acl_find(aclname);
1616 if (!acl) {
1617 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1618 return;
1621 if (strcmp(command, "show") == 0) {
1622 int i = 0;
1623 qemu_acl_entry *entry;
1624 monitor_printf(mon, "policy: %s\n",
1625 acl->defaultDeny ? "deny" : "allow");
1626 TAILQ_FOREACH(entry, &acl->entries, next) {
1627 i++;
1628 monitor_printf(mon, "%d: %s %s\n", i,
1629 entry->deny ? "deny" : "allow",
1630 entry->match);
1632 } else if (strcmp(command, "reset") == 0) {
1633 qemu_acl_reset(acl);
1634 monitor_printf(mon, "acl: removed all rules\n");
1635 } else if (strcmp(command, "policy") == 0) {
1636 if (!match) {
1637 monitor_printf(mon, "acl: missing policy parameter\n");
1638 return;
1641 if (strcmp(match, "allow") == 0) {
1642 acl->defaultDeny = 0;
1643 monitor_printf(mon, "acl: policy set to 'allow'\n");
1644 } else if (strcmp(match, "deny") == 0) {
1645 acl->defaultDeny = 1;
1646 monitor_printf(mon, "acl: policy set to 'deny'\n");
1647 } else {
1648 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1650 } else if ((strcmp(command, "allow") == 0) ||
1651 (strcmp(command, "deny") == 0)) {
1652 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1653 int ret;
1655 if (!match) {
1656 monitor_printf(mon, "acl: missing match parameter\n");
1657 return;
1660 if (has_index)
1661 ret = qemu_acl_insert(acl, deny, match, index);
1662 else
1663 ret = qemu_acl_append(acl, deny, match);
1664 if (ret < 0)
1665 monitor_printf(mon, "acl: unable to add acl entry\n");
1666 else
1667 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1668 } else if (strcmp(command, "remove") == 0) {
1669 int ret;
1671 if (!match) {
1672 monitor_printf(mon, "acl: missing match parameter\n");
1673 return;
1676 ret = qemu_acl_remove(acl, match);
1677 if (ret < 0)
1678 monitor_printf(mon, "acl: no matching acl entry\n");
1679 else
1680 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1681 } else {
1682 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1686 static const mon_cmd_t mon_cmds[] = {
1687 #include "qemu-monitor.h"
1688 { NULL, NULL, },
1691 /* Please update qemu-monitor.hx when adding or changing commands */
1692 static const mon_cmd_t info_cmds[] = {
1693 { "version", "", do_info_version,
1694 "", "show the version of QEMU" },
1695 { "network", "", do_info_network,
1696 "", "show the network state" },
1697 { "chardev", "", qemu_chr_info,
1698 "", "show the character devices" },
1699 { "block", "", bdrv_info,
1700 "", "show the block devices" },
1701 { "blockstats", "", bdrv_info_stats,
1702 "", "show block device statistics" },
1703 { "registers", "", do_info_registers,
1704 "", "show the cpu registers" },
1705 { "cpus", "", do_info_cpus,
1706 "", "show infos for each CPU" },
1707 { "history", "", do_info_history,
1708 "", "show the command line history", },
1709 { "irq", "", irq_info,
1710 "", "show the interrupts statistics (if available)", },
1711 { "pic", "", pic_info,
1712 "", "show i8259 (PIC) state", },
1713 { "pci", "", pci_info,
1714 "", "show PCI info", },
1715 #if defined(TARGET_I386) || defined(TARGET_SH4)
1716 { "tlb", "", tlb_info,
1717 "", "show virtual to physical memory mappings", },
1718 #endif
1719 #if defined(TARGET_I386)
1720 { "mem", "", mem_info,
1721 "", "show the active virtual memory mappings", },
1722 { "hpet", "", do_info_hpet,
1723 "", "show state of HPET", },
1724 #endif
1725 { "jit", "", do_info_jit,
1726 "", "show dynamic compiler info", },
1727 { "kqemu", "", do_info_kqemu,
1728 "", "show KQEMU information", },
1729 { "kvm", "", do_info_kvm,
1730 "", "show KVM information", },
1731 { "numa", "", do_info_numa,
1732 "", "show NUMA information", },
1733 { "usb", "", usb_info,
1734 "", "show guest USB devices", },
1735 { "usbhost", "", usb_host_info,
1736 "", "show host USB devices", },
1737 { "profile", "", do_info_profile,
1738 "", "show profiling information", },
1739 { "capture", "", do_info_capture,
1740 "", "show capture information" },
1741 { "snapshots", "", do_info_snapshots,
1742 "", "show the currently saved VM snapshots" },
1743 { "status", "", do_info_status,
1744 "", "show the current VM status (running|paused)" },
1745 { "pcmcia", "", pcmcia_info,
1746 "", "show guest PCMCIA status" },
1747 { "mice", "", do_info_mice,
1748 "", "show which guest mouse is receiving events" },
1749 { "vnc", "", do_info_vnc,
1750 "", "show the vnc server status"},
1751 { "name", "", do_info_name,
1752 "", "show the current VM name" },
1753 { "uuid", "", do_info_uuid,
1754 "", "show the current VM UUID" },
1755 #if defined(TARGET_PPC)
1756 { "cpustats", "", do_info_cpu_stats,
1757 "", "show CPU statistics", },
1758 #endif
1759 #if defined(CONFIG_SLIRP)
1760 { "slirp", "", do_info_slirp,
1761 "", "show SLIRP statistics", },
1762 #endif
1763 { "migrate", "", do_info_migrate, "", "show migration status" },
1764 { "balloon", "", do_info_balloon,
1765 "", "show balloon information" },
1766 { "qtree", "", do_info_qtree,
1767 "", "show device tree" },
1768 { NULL, NULL, },
1771 /*******************************************************************/
1773 static const char *pch;
1774 static jmp_buf expr_env;
1776 #define MD_TLONG 0
1777 #define MD_I32 1
1779 typedef struct MonitorDef {
1780 const char *name;
1781 int offset;
1782 target_long (*get_value)(const struct MonitorDef *md, int val);
1783 int type;
1784 } MonitorDef;
1786 #if defined(TARGET_I386)
1787 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1789 CPUState *env = mon_get_cpu();
1790 if (!env)
1791 return 0;
1792 return env->eip + env->segs[R_CS].base;
1794 #endif
1796 #if defined(TARGET_PPC)
1797 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1799 CPUState *env = mon_get_cpu();
1800 unsigned int u;
1801 int i;
1803 if (!env)
1804 return 0;
1806 u = 0;
1807 for (i = 0; i < 8; i++)
1808 u |= env->crf[i] << (32 - (4 * i));
1810 return u;
1813 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1815 CPUState *env = mon_get_cpu();
1816 if (!env)
1817 return 0;
1818 return env->msr;
1821 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1823 CPUState *env = mon_get_cpu();
1824 if (!env)
1825 return 0;
1826 return env->xer;
1829 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1831 CPUState *env = mon_get_cpu();
1832 if (!env)
1833 return 0;
1834 return cpu_ppc_load_decr(env);
1837 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1839 CPUState *env = mon_get_cpu();
1840 if (!env)
1841 return 0;
1842 return cpu_ppc_load_tbu(env);
1845 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1847 CPUState *env = mon_get_cpu();
1848 if (!env)
1849 return 0;
1850 return cpu_ppc_load_tbl(env);
1852 #endif
1854 #if defined(TARGET_SPARC)
1855 #ifndef TARGET_SPARC64
1856 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1858 CPUState *env = mon_get_cpu();
1859 if (!env)
1860 return 0;
1861 return GET_PSR(env);
1863 #endif
1865 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1867 CPUState *env = mon_get_cpu();
1868 if (!env)
1869 return 0;
1870 return env->regwptr[val];
1872 #endif
1874 static const MonitorDef monitor_defs[] = {
1875 #ifdef TARGET_I386
1877 #define SEG(name, seg) \
1878 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1879 { name ".base", offsetof(CPUState, segs[seg].base) },\
1880 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1882 { "eax", offsetof(CPUState, regs[0]) },
1883 { "ecx", offsetof(CPUState, regs[1]) },
1884 { "edx", offsetof(CPUState, regs[2]) },
1885 { "ebx", offsetof(CPUState, regs[3]) },
1886 { "esp|sp", offsetof(CPUState, regs[4]) },
1887 { "ebp|fp", offsetof(CPUState, regs[5]) },
1888 { "esi", offsetof(CPUState, regs[6]) },
1889 { "edi", offsetof(CPUState, regs[7]) },
1890 #ifdef TARGET_X86_64
1891 { "r8", offsetof(CPUState, regs[8]) },
1892 { "r9", offsetof(CPUState, regs[9]) },
1893 { "r10", offsetof(CPUState, regs[10]) },
1894 { "r11", offsetof(CPUState, regs[11]) },
1895 { "r12", offsetof(CPUState, regs[12]) },
1896 { "r13", offsetof(CPUState, regs[13]) },
1897 { "r14", offsetof(CPUState, regs[14]) },
1898 { "r15", offsetof(CPUState, regs[15]) },
1899 #endif
1900 { "eflags", offsetof(CPUState, eflags) },
1901 { "eip", offsetof(CPUState, eip) },
1902 SEG("cs", R_CS)
1903 SEG("ds", R_DS)
1904 SEG("es", R_ES)
1905 SEG("ss", R_SS)
1906 SEG("fs", R_FS)
1907 SEG("gs", R_GS)
1908 { "pc", 0, monitor_get_pc, },
1909 #elif defined(TARGET_PPC)
1910 /* General purpose registers */
1911 { "r0", offsetof(CPUState, gpr[0]) },
1912 { "r1", offsetof(CPUState, gpr[1]) },
1913 { "r2", offsetof(CPUState, gpr[2]) },
1914 { "r3", offsetof(CPUState, gpr[3]) },
1915 { "r4", offsetof(CPUState, gpr[4]) },
1916 { "r5", offsetof(CPUState, gpr[5]) },
1917 { "r6", offsetof(CPUState, gpr[6]) },
1918 { "r7", offsetof(CPUState, gpr[7]) },
1919 { "r8", offsetof(CPUState, gpr[8]) },
1920 { "r9", offsetof(CPUState, gpr[9]) },
1921 { "r10", offsetof(CPUState, gpr[10]) },
1922 { "r11", offsetof(CPUState, gpr[11]) },
1923 { "r12", offsetof(CPUState, gpr[12]) },
1924 { "r13", offsetof(CPUState, gpr[13]) },
1925 { "r14", offsetof(CPUState, gpr[14]) },
1926 { "r15", offsetof(CPUState, gpr[15]) },
1927 { "r16", offsetof(CPUState, gpr[16]) },
1928 { "r17", offsetof(CPUState, gpr[17]) },
1929 { "r18", offsetof(CPUState, gpr[18]) },
1930 { "r19", offsetof(CPUState, gpr[19]) },
1931 { "r20", offsetof(CPUState, gpr[20]) },
1932 { "r21", offsetof(CPUState, gpr[21]) },
1933 { "r22", offsetof(CPUState, gpr[22]) },
1934 { "r23", offsetof(CPUState, gpr[23]) },
1935 { "r24", offsetof(CPUState, gpr[24]) },
1936 { "r25", offsetof(CPUState, gpr[25]) },
1937 { "r26", offsetof(CPUState, gpr[26]) },
1938 { "r27", offsetof(CPUState, gpr[27]) },
1939 { "r28", offsetof(CPUState, gpr[28]) },
1940 { "r29", offsetof(CPUState, gpr[29]) },
1941 { "r30", offsetof(CPUState, gpr[30]) },
1942 { "r31", offsetof(CPUState, gpr[31]) },
1943 /* Floating point registers */
1944 { "f0", offsetof(CPUState, fpr[0]) },
1945 { "f1", offsetof(CPUState, fpr[1]) },
1946 { "f2", offsetof(CPUState, fpr[2]) },
1947 { "f3", offsetof(CPUState, fpr[3]) },
1948 { "f4", offsetof(CPUState, fpr[4]) },
1949 { "f5", offsetof(CPUState, fpr[5]) },
1950 { "f6", offsetof(CPUState, fpr[6]) },
1951 { "f7", offsetof(CPUState, fpr[7]) },
1952 { "f8", offsetof(CPUState, fpr[8]) },
1953 { "f9", offsetof(CPUState, fpr[9]) },
1954 { "f10", offsetof(CPUState, fpr[10]) },
1955 { "f11", offsetof(CPUState, fpr[11]) },
1956 { "f12", offsetof(CPUState, fpr[12]) },
1957 { "f13", offsetof(CPUState, fpr[13]) },
1958 { "f14", offsetof(CPUState, fpr[14]) },
1959 { "f15", offsetof(CPUState, fpr[15]) },
1960 { "f16", offsetof(CPUState, fpr[16]) },
1961 { "f17", offsetof(CPUState, fpr[17]) },
1962 { "f18", offsetof(CPUState, fpr[18]) },
1963 { "f19", offsetof(CPUState, fpr[19]) },
1964 { "f20", offsetof(CPUState, fpr[20]) },
1965 { "f21", offsetof(CPUState, fpr[21]) },
1966 { "f22", offsetof(CPUState, fpr[22]) },
1967 { "f23", offsetof(CPUState, fpr[23]) },
1968 { "f24", offsetof(CPUState, fpr[24]) },
1969 { "f25", offsetof(CPUState, fpr[25]) },
1970 { "f26", offsetof(CPUState, fpr[26]) },
1971 { "f27", offsetof(CPUState, fpr[27]) },
1972 { "f28", offsetof(CPUState, fpr[28]) },
1973 { "f29", offsetof(CPUState, fpr[29]) },
1974 { "f30", offsetof(CPUState, fpr[30]) },
1975 { "f31", offsetof(CPUState, fpr[31]) },
1976 { "fpscr", offsetof(CPUState, fpscr) },
1977 /* Next instruction pointer */
1978 { "nip|pc", offsetof(CPUState, nip) },
1979 { "lr", offsetof(CPUState, lr) },
1980 { "ctr", offsetof(CPUState, ctr) },
1981 { "decr", 0, &monitor_get_decr, },
1982 { "ccr", 0, &monitor_get_ccr, },
1983 /* Machine state register */
1984 { "msr", 0, &monitor_get_msr, },
1985 { "xer", 0, &monitor_get_xer, },
1986 { "tbu", 0, &monitor_get_tbu, },
1987 { "tbl", 0, &monitor_get_tbl, },
1988 #if defined(TARGET_PPC64)
1989 /* Address space register */
1990 { "asr", offsetof(CPUState, asr) },
1991 #endif
1992 /* Segment registers */
1993 { "sdr1", offsetof(CPUState, sdr1) },
1994 { "sr0", offsetof(CPUState, sr[0]) },
1995 { "sr1", offsetof(CPUState, sr[1]) },
1996 { "sr2", offsetof(CPUState, sr[2]) },
1997 { "sr3", offsetof(CPUState, sr[3]) },
1998 { "sr4", offsetof(CPUState, sr[4]) },
1999 { "sr5", offsetof(CPUState, sr[5]) },
2000 { "sr6", offsetof(CPUState, sr[6]) },
2001 { "sr7", offsetof(CPUState, sr[7]) },
2002 { "sr8", offsetof(CPUState, sr[8]) },
2003 { "sr9", offsetof(CPUState, sr[9]) },
2004 { "sr10", offsetof(CPUState, sr[10]) },
2005 { "sr11", offsetof(CPUState, sr[11]) },
2006 { "sr12", offsetof(CPUState, sr[12]) },
2007 { "sr13", offsetof(CPUState, sr[13]) },
2008 { "sr14", offsetof(CPUState, sr[14]) },
2009 { "sr15", offsetof(CPUState, sr[15]) },
2010 /* Too lazy to put BATs and SPRs ... */
2011 #elif defined(TARGET_SPARC)
2012 { "g0", offsetof(CPUState, gregs[0]) },
2013 { "g1", offsetof(CPUState, gregs[1]) },
2014 { "g2", offsetof(CPUState, gregs[2]) },
2015 { "g3", offsetof(CPUState, gregs[3]) },
2016 { "g4", offsetof(CPUState, gregs[4]) },
2017 { "g5", offsetof(CPUState, gregs[5]) },
2018 { "g6", offsetof(CPUState, gregs[6]) },
2019 { "g7", offsetof(CPUState, gregs[7]) },
2020 { "o0", 0, monitor_get_reg },
2021 { "o1", 1, monitor_get_reg },
2022 { "o2", 2, monitor_get_reg },
2023 { "o3", 3, monitor_get_reg },
2024 { "o4", 4, monitor_get_reg },
2025 { "o5", 5, monitor_get_reg },
2026 { "o6", 6, monitor_get_reg },
2027 { "o7", 7, monitor_get_reg },
2028 { "l0", 8, monitor_get_reg },
2029 { "l1", 9, monitor_get_reg },
2030 { "l2", 10, monitor_get_reg },
2031 { "l3", 11, monitor_get_reg },
2032 { "l4", 12, monitor_get_reg },
2033 { "l5", 13, monitor_get_reg },
2034 { "l6", 14, monitor_get_reg },
2035 { "l7", 15, monitor_get_reg },
2036 { "i0", 16, monitor_get_reg },
2037 { "i1", 17, monitor_get_reg },
2038 { "i2", 18, monitor_get_reg },
2039 { "i3", 19, monitor_get_reg },
2040 { "i4", 20, monitor_get_reg },
2041 { "i5", 21, monitor_get_reg },
2042 { "i6", 22, monitor_get_reg },
2043 { "i7", 23, monitor_get_reg },
2044 { "pc", offsetof(CPUState, pc) },
2045 { "npc", offsetof(CPUState, npc) },
2046 { "y", offsetof(CPUState, y) },
2047 #ifndef TARGET_SPARC64
2048 { "psr", 0, &monitor_get_psr, },
2049 { "wim", offsetof(CPUState, wim) },
2050 #endif
2051 { "tbr", offsetof(CPUState, tbr) },
2052 { "fsr", offsetof(CPUState, fsr) },
2053 { "f0", offsetof(CPUState, fpr[0]) },
2054 { "f1", offsetof(CPUState, fpr[1]) },
2055 { "f2", offsetof(CPUState, fpr[2]) },
2056 { "f3", offsetof(CPUState, fpr[3]) },
2057 { "f4", offsetof(CPUState, fpr[4]) },
2058 { "f5", offsetof(CPUState, fpr[5]) },
2059 { "f6", offsetof(CPUState, fpr[6]) },
2060 { "f7", offsetof(CPUState, fpr[7]) },
2061 { "f8", offsetof(CPUState, fpr[8]) },
2062 { "f9", offsetof(CPUState, fpr[9]) },
2063 { "f10", offsetof(CPUState, fpr[10]) },
2064 { "f11", offsetof(CPUState, fpr[11]) },
2065 { "f12", offsetof(CPUState, fpr[12]) },
2066 { "f13", offsetof(CPUState, fpr[13]) },
2067 { "f14", offsetof(CPUState, fpr[14]) },
2068 { "f15", offsetof(CPUState, fpr[15]) },
2069 { "f16", offsetof(CPUState, fpr[16]) },
2070 { "f17", offsetof(CPUState, fpr[17]) },
2071 { "f18", offsetof(CPUState, fpr[18]) },
2072 { "f19", offsetof(CPUState, fpr[19]) },
2073 { "f20", offsetof(CPUState, fpr[20]) },
2074 { "f21", offsetof(CPUState, fpr[21]) },
2075 { "f22", offsetof(CPUState, fpr[22]) },
2076 { "f23", offsetof(CPUState, fpr[23]) },
2077 { "f24", offsetof(CPUState, fpr[24]) },
2078 { "f25", offsetof(CPUState, fpr[25]) },
2079 { "f26", offsetof(CPUState, fpr[26]) },
2080 { "f27", offsetof(CPUState, fpr[27]) },
2081 { "f28", offsetof(CPUState, fpr[28]) },
2082 { "f29", offsetof(CPUState, fpr[29]) },
2083 { "f30", offsetof(CPUState, fpr[30]) },
2084 { "f31", offsetof(CPUState, fpr[31]) },
2085 #ifdef TARGET_SPARC64
2086 { "f32", offsetof(CPUState, fpr[32]) },
2087 { "f34", offsetof(CPUState, fpr[34]) },
2088 { "f36", offsetof(CPUState, fpr[36]) },
2089 { "f38", offsetof(CPUState, fpr[38]) },
2090 { "f40", offsetof(CPUState, fpr[40]) },
2091 { "f42", offsetof(CPUState, fpr[42]) },
2092 { "f44", offsetof(CPUState, fpr[44]) },
2093 { "f46", offsetof(CPUState, fpr[46]) },
2094 { "f48", offsetof(CPUState, fpr[48]) },
2095 { "f50", offsetof(CPUState, fpr[50]) },
2096 { "f52", offsetof(CPUState, fpr[52]) },
2097 { "f54", offsetof(CPUState, fpr[54]) },
2098 { "f56", offsetof(CPUState, fpr[56]) },
2099 { "f58", offsetof(CPUState, fpr[58]) },
2100 { "f60", offsetof(CPUState, fpr[60]) },
2101 { "f62", offsetof(CPUState, fpr[62]) },
2102 { "asi", offsetof(CPUState, asi) },
2103 { "pstate", offsetof(CPUState, pstate) },
2104 { "cansave", offsetof(CPUState, cansave) },
2105 { "canrestore", offsetof(CPUState, canrestore) },
2106 { "otherwin", offsetof(CPUState, otherwin) },
2107 { "wstate", offsetof(CPUState, wstate) },
2108 { "cleanwin", offsetof(CPUState, cleanwin) },
2109 { "fprs", offsetof(CPUState, fprs) },
2110 #endif
2111 #endif
2112 { NULL },
2115 static void expr_error(Monitor *mon, const char *msg)
2117 monitor_printf(mon, "%s\n", msg);
2118 longjmp(expr_env, 1);
2121 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2122 static int get_monitor_def(target_long *pval, const char *name)
2124 const MonitorDef *md;
2125 void *ptr;
2127 for(md = monitor_defs; md->name != NULL; md++) {
2128 if (compare_cmd(name, md->name)) {
2129 if (md->get_value) {
2130 *pval = md->get_value(md, md->offset);
2131 } else {
2132 CPUState *env = mon_get_cpu();
2133 if (!env)
2134 return -2;
2135 ptr = (uint8_t *)env + md->offset;
2136 switch(md->type) {
2137 case MD_I32:
2138 *pval = *(int32_t *)ptr;
2139 break;
2140 case MD_TLONG:
2141 *pval = *(target_long *)ptr;
2142 break;
2143 default:
2144 *pval = 0;
2145 break;
2148 return 0;
2151 return -1;
2154 static void next(void)
2156 if (pch != '\0') {
2157 pch++;
2158 while (qemu_isspace(*pch))
2159 pch++;
2163 static int64_t expr_sum(Monitor *mon);
2165 static int64_t expr_unary(Monitor *mon)
2167 int64_t n;
2168 char *p;
2169 int ret;
2171 switch(*pch) {
2172 case '+':
2173 next();
2174 n = expr_unary(mon);
2175 break;
2176 case '-':
2177 next();
2178 n = -expr_unary(mon);
2179 break;
2180 case '~':
2181 next();
2182 n = ~expr_unary(mon);
2183 break;
2184 case '(':
2185 next();
2186 n = expr_sum(mon);
2187 if (*pch != ')') {
2188 expr_error(mon, "')' expected");
2190 next();
2191 break;
2192 case '\'':
2193 pch++;
2194 if (*pch == '\0')
2195 expr_error(mon, "character constant expected");
2196 n = *pch;
2197 pch++;
2198 if (*pch != '\'')
2199 expr_error(mon, "missing terminating \' character");
2200 next();
2201 break;
2202 case '$':
2204 char buf[128], *q;
2205 target_long reg=0;
2207 pch++;
2208 q = buf;
2209 while ((*pch >= 'a' && *pch <= 'z') ||
2210 (*pch >= 'A' && *pch <= 'Z') ||
2211 (*pch >= '0' && *pch <= '9') ||
2212 *pch == '_' || *pch == '.') {
2213 if ((q - buf) < sizeof(buf) - 1)
2214 *q++ = *pch;
2215 pch++;
2217 while (qemu_isspace(*pch))
2218 pch++;
2219 *q = 0;
2220 ret = get_monitor_def(&reg, buf);
2221 if (ret == -1)
2222 expr_error(mon, "unknown register");
2223 else if (ret == -2)
2224 expr_error(mon, "no cpu defined");
2225 n = reg;
2227 break;
2228 case '\0':
2229 expr_error(mon, "unexpected end of expression");
2230 n = 0;
2231 break;
2232 default:
2233 #if TARGET_PHYS_ADDR_BITS > 32
2234 n = strtoull(pch, &p, 0);
2235 #else
2236 n = strtoul(pch, &p, 0);
2237 #endif
2238 if (pch == p) {
2239 expr_error(mon, "invalid char in expression");
2241 pch = p;
2242 while (qemu_isspace(*pch))
2243 pch++;
2244 break;
2246 return n;
2250 static int64_t expr_prod(Monitor *mon)
2252 int64_t val, val2;
2253 int op;
2255 val = expr_unary(mon);
2256 for(;;) {
2257 op = *pch;
2258 if (op != '*' && op != '/' && op != '%')
2259 break;
2260 next();
2261 val2 = expr_unary(mon);
2262 switch(op) {
2263 default:
2264 case '*':
2265 val *= val2;
2266 break;
2267 case '/':
2268 case '%':
2269 if (val2 == 0)
2270 expr_error(mon, "division by zero");
2271 if (op == '/')
2272 val /= val2;
2273 else
2274 val %= val2;
2275 break;
2278 return val;
2281 static int64_t expr_logic(Monitor *mon)
2283 int64_t val, val2;
2284 int op;
2286 val = expr_prod(mon);
2287 for(;;) {
2288 op = *pch;
2289 if (op != '&' && op != '|' && op != '^')
2290 break;
2291 next();
2292 val2 = expr_prod(mon);
2293 switch(op) {
2294 default:
2295 case '&':
2296 val &= val2;
2297 break;
2298 case '|':
2299 val |= val2;
2300 break;
2301 case '^':
2302 val ^= val2;
2303 break;
2306 return val;
2309 static int64_t expr_sum(Monitor *mon)
2311 int64_t val, val2;
2312 int op;
2314 val = expr_logic(mon);
2315 for(;;) {
2316 op = *pch;
2317 if (op != '+' && op != '-')
2318 break;
2319 next();
2320 val2 = expr_logic(mon);
2321 if (op == '+')
2322 val += val2;
2323 else
2324 val -= val2;
2326 return val;
2329 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2331 pch = *pp;
2332 if (setjmp(expr_env)) {
2333 *pp = pch;
2334 return -1;
2336 while (qemu_isspace(*pch))
2337 pch++;
2338 *pval = expr_sum(mon);
2339 *pp = pch;
2340 return 0;
2343 static int get_str(char *buf, int buf_size, const char **pp)
2345 const char *p;
2346 char *q;
2347 int c;
2349 q = buf;
2350 p = *pp;
2351 while (qemu_isspace(*p))
2352 p++;
2353 if (*p == '\0') {
2354 fail:
2355 *q = '\0';
2356 *pp = p;
2357 return -1;
2359 if (*p == '\"') {
2360 p++;
2361 while (*p != '\0' && *p != '\"') {
2362 if (*p == '\\') {
2363 p++;
2364 c = *p++;
2365 switch(c) {
2366 case 'n':
2367 c = '\n';
2368 break;
2369 case 'r':
2370 c = '\r';
2371 break;
2372 case '\\':
2373 case '\'':
2374 case '\"':
2375 break;
2376 default:
2377 qemu_printf("unsupported escape code: '\\%c'\n", c);
2378 goto fail;
2380 if ((q - buf) < buf_size - 1) {
2381 *q++ = c;
2383 } else {
2384 if ((q - buf) < buf_size - 1) {
2385 *q++ = *p;
2387 p++;
2390 if (*p != '\"') {
2391 qemu_printf("unterminated string\n");
2392 goto fail;
2394 p++;
2395 } else {
2396 while (*p != '\0' && !qemu_isspace(*p)) {
2397 if ((q - buf) < buf_size - 1) {
2398 *q++ = *p;
2400 p++;
2403 *q = '\0';
2404 *pp = p;
2405 return 0;
2409 * Store the command-name in cmdname, and return a pointer to
2410 * the remaining of the command string.
2412 static const char *get_command_name(const char *cmdline,
2413 char *cmdname, size_t nlen)
2415 size_t len;
2416 const char *p, *pstart;
2418 p = cmdline;
2419 while (qemu_isspace(*p))
2420 p++;
2421 if (*p == '\0')
2422 return NULL;
2423 pstart = p;
2424 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2425 p++;
2426 len = p - pstart;
2427 if (len > nlen - 1)
2428 len = nlen - 1;
2429 memcpy(cmdname, pstart, len);
2430 cmdname[len] = '\0';
2431 return p;
2434 static int default_fmt_format = 'x';
2435 static int default_fmt_size = 4;
2437 #define MAX_ARGS 16
2439 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2441 const char *p, *typestr;
2442 int c, nb_args, i, has_arg;
2443 const mon_cmd_t *cmd;
2444 char cmdname[256];
2445 char buf[1024];
2446 void *str_allocated[MAX_ARGS];
2447 void *args[MAX_ARGS];
2448 void (*handler_0)(Monitor *mon);
2449 void (*handler_1)(Monitor *mon, void *arg0);
2450 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2451 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2452 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2453 void *arg3);
2454 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2455 void *arg3, void *arg4);
2456 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2457 void *arg3, void *arg4, void *arg5);
2458 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2459 void *arg3, void *arg4, void *arg5, void *arg6);
2461 #ifdef DEBUG
2462 monitor_printf(mon, "command='%s'\n", cmdline);
2463 #endif
2465 /* extract the command name */
2466 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2467 if (!p)
2468 return;
2470 /* find the command */
2471 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2472 if (compare_cmd(cmdname, cmd->name))
2473 break;
2476 if (cmd->name == NULL) {
2477 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2478 return;
2481 for(i = 0; i < MAX_ARGS; i++)
2482 str_allocated[i] = NULL;
2484 /* parse the parameters */
2485 typestr = cmd->args_type;
2486 nb_args = 0;
2487 for(;;) {
2488 c = *typestr;
2489 if (c == '\0')
2490 break;
2491 typestr++;
2492 switch(c) {
2493 case 'F':
2494 case 'B':
2495 case 's':
2497 int ret;
2498 char *str;
2500 while (qemu_isspace(*p))
2501 p++;
2502 if (*typestr == '?') {
2503 typestr++;
2504 if (*p == '\0') {
2505 /* no optional string: NULL argument */
2506 str = NULL;
2507 goto add_str;
2510 ret = get_str(buf, sizeof(buf), &p);
2511 if (ret < 0) {
2512 switch(c) {
2513 case 'F':
2514 monitor_printf(mon, "%s: filename expected\n",
2515 cmdname);
2516 break;
2517 case 'B':
2518 monitor_printf(mon, "%s: block device name expected\n",
2519 cmdname);
2520 break;
2521 default:
2522 monitor_printf(mon, "%s: string expected\n", cmdname);
2523 break;
2525 goto fail;
2527 str = qemu_malloc(strlen(buf) + 1);
2528 pstrcpy(str, sizeof(buf), buf);
2529 str_allocated[nb_args] = str;
2530 add_str:
2531 if (nb_args >= MAX_ARGS) {
2532 error_args:
2533 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2534 goto fail;
2536 args[nb_args++] = str;
2538 break;
2539 case '/':
2541 int count, format, size;
2543 while (qemu_isspace(*p))
2544 p++;
2545 if (*p == '/') {
2546 /* format found */
2547 p++;
2548 count = 1;
2549 if (qemu_isdigit(*p)) {
2550 count = 0;
2551 while (qemu_isdigit(*p)) {
2552 count = count * 10 + (*p - '0');
2553 p++;
2556 size = -1;
2557 format = -1;
2558 for(;;) {
2559 switch(*p) {
2560 case 'o':
2561 case 'd':
2562 case 'u':
2563 case 'x':
2564 case 'i':
2565 case 'c':
2566 format = *p++;
2567 break;
2568 case 'b':
2569 size = 1;
2570 p++;
2571 break;
2572 case 'h':
2573 size = 2;
2574 p++;
2575 break;
2576 case 'w':
2577 size = 4;
2578 p++;
2579 break;
2580 case 'g':
2581 case 'L':
2582 size = 8;
2583 p++;
2584 break;
2585 default:
2586 goto next;
2589 next:
2590 if (*p != '\0' && !qemu_isspace(*p)) {
2591 monitor_printf(mon, "invalid char in format: '%c'\n",
2592 *p);
2593 goto fail;
2595 if (format < 0)
2596 format = default_fmt_format;
2597 if (format != 'i') {
2598 /* for 'i', not specifying a size gives -1 as size */
2599 if (size < 0)
2600 size = default_fmt_size;
2601 default_fmt_size = size;
2603 default_fmt_format = format;
2604 } else {
2605 count = 1;
2606 format = default_fmt_format;
2607 if (format != 'i') {
2608 size = default_fmt_size;
2609 } else {
2610 size = -1;
2613 if (nb_args + 3 > MAX_ARGS)
2614 goto error_args;
2615 args[nb_args++] = (void*)(long)count;
2616 args[nb_args++] = (void*)(long)format;
2617 args[nb_args++] = (void*)(long)size;
2619 break;
2620 case 'i':
2621 case 'l':
2623 int64_t val;
2625 while (qemu_isspace(*p))
2626 p++;
2627 if (*typestr == '?' || *typestr == '.') {
2628 if (*typestr == '?') {
2629 if (*p == '\0')
2630 has_arg = 0;
2631 else
2632 has_arg = 1;
2633 } else {
2634 if (*p == '.') {
2635 p++;
2636 while (qemu_isspace(*p))
2637 p++;
2638 has_arg = 1;
2639 } else {
2640 has_arg = 0;
2643 typestr++;
2644 if (nb_args >= MAX_ARGS)
2645 goto error_args;
2646 args[nb_args++] = (void *)(long)has_arg;
2647 if (!has_arg) {
2648 if (nb_args >= MAX_ARGS)
2649 goto error_args;
2650 val = -1;
2651 goto add_num;
2654 if (get_expr(mon, &val, &p))
2655 goto fail;
2656 add_num:
2657 if (c == 'i') {
2658 if (nb_args >= MAX_ARGS)
2659 goto error_args;
2660 args[nb_args++] = (void *)(long)val;
2661 } else {
2662 if ((nb_args + 1) >= MAX_ARGS)
2663 goto error_args;
2664 #if TARGET_PHYS_ADDR_BITS > 32
2665 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2666 #else
2667 args[nb_args++] = (void *)0;
2668 #endif
2669 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2672 break;
2673 case '-':
2675 int has_option;
2676 /* option */
2678 c = *typestr++;
2679 if (c == '\0')
2680 goto bad_type;
2681 while (qemu_isspace(*p))
2682 p++;
2683 has_option = 0;
2684 if (*p == '-') {
2685 p++;
2686 if (*p != c) {
2687 monitor_printf(mon, "%s: unsupported option -%c\n",
2688 cmdname, *p);
2689 goto fail;
2691 p++;
2692 has_option = 1;
2694 if (nb_args >= MAX_ARGS)
2695 goto error_args;
2696 args[nb_args++] = (void *)(long)has_option;
2698 break;
2699 default:
2700 bad_type:
2701 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2702 goto fail;
2705 /* check that all arguments were parsed */
2706 while (qemu_isspace(*p))
2707 p++;
2708 if (*p != '\0') {
2709 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2710 cmdname);
2711 goto fail;
2714 switch(nb_args) {
2715 case 0:
2716 handler_0 = cmd->handler;
2717 handler_0(mon);
2718 break;
2719 case 1:
2720 handler_1 = cmd->handler;
2721 handler_1(mon, args[0]);
2722 break;
2723 case 2:
2724 handler_2 = cmd->handler;
2725 handler_2(mon, args[0], args[1]);
2726 break;
2727 case 3:
2728 handler_3 = cmd->handler;
2729 handler_3(mon, args[0], args[1], args[2]);
2730 break;
2731 case 4:
2732 handler_4 = cmd->handler;
2733 handler_4(mon, args[0], args[1], args[2], args[3]);
2734 break;
2735 case 5:
2736 handler_5 = cmd->handler;
2737 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2738 break;
2739 case 6:
2740 handler_6 = cmd->handler;
2741 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2742 break;
2743 case 7:
2744 handler_7 = cmd->handler;
2745 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2746 args[6]);
2747 break;
2748 default:
2749 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2750 goto fail;
2752 fail:
2753 for(i = 0; i < MAX_ARGS; i++)
2754 qemu_free(str_allocated[i]);
2757 static void cmd_completion(const char *name, const char *list)
2759 const char *p, *pstart;
2760 char cmd[128];
2761 int len;
2763 p = list;
2764 for(;;) {
2765 pstart = p;
2766 p = strchr(p, '|');
2767 if (!p)
2768 p = pstart + strlen(pstart);
2769 len = p - pstart;
2770 if (len > sizeof(cmd) - 2)
2771 len = sizeof(cmd) - 2;
2772 memcpy(cmd, pstart, len);
2773 cmd[len] = '\0';
2774 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2775 readline_add_completion(cur_mon->rs, cmd);
2777 if (*p == '\0')
2778 break;
2779 p++;
2783 static void file_completion(const char *input)
2785 DIR *ffs;
2786 struct dirent *d;
2787 char path[1024];
2788 char file[1024], file_prefix[1024];
2789 int input_path_len;
2790 const char *p;
2792 p = strrchr(input, '/');
2793 if (!p) {
2794 input_path_len = 0;
2795 pstrcpy(file_prefix, sizeof(file_prefix), input);
2796 pstrcpy(path, sizeof(path), ".");
2797 } else {
2798 input_path_len = p - input + 1;
2799 memcpy(path, input, input_path_len);
2800 if (input_path_len > sizeof(path) - 1)
2801 input_path_len = sizeof(path) - 1;
2802 path[input_path_len] = '\0';
2803 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2805 #ifdef DEBUG_COMPLETION
2806 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2807 input, path, file_prefix);
2808 #endif
2809 ffs = opendir(path);
2810 if (!ffs)
2811 return;
2812 for(;;) {
2813 struct stat sb;
2814 d = readdir(ffs);
2815 if (!d)
2816 break;
2817 if (strstart(d->d_name, file_prefix, NULL)) {
2818 memcpy(file, input, input_path_len);
2819 if (input_path_len < sizeof(file))
2820 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2821 d->d_name);
2822 /* stat the file to find out if it's a directory.
2823 * In that case add a slash to speed up typing long paths
2825 stat(file, &sb);
2826 if(S_ISDIR(sb.st_mode))
2827 pstrcat(file, sizeof(file), "/");
2828 readline_add_completion(cur_mon->rs, file);
2831 closedir(ffs);
2834 static void block_completion_it(void *opaque, BlockDriverState *bs)
2836 const char *name = bdrv_get_device_name(bs);
2837 const char *input = opaque;
2839 if (input[0] == '\0' ||
2840 !strncmp(name, (char *)input, strlen(input))) {
2841 readline_add_completion(cur_mon->rs, name);
2845 /* NOTE: this parser is an approximate form of the real command parser */
2846 static void parse_cmdline(const char *cmdline,
2847 int *pnb_args, char **args)
2849 const char *p;
2850 int nb_args, ret;
2851 char buf[1024];
2853 p = cmdline;
2854 nb_args = 0;
2855 for(;;) {
2856 while (qemu_isspace(*p))
2857 p++;
2858 if (*p == '\0')
2859 break;
2860 if (nb_args >= MAX_ARGS)
2861 break;
2862 ret = get_str(buf, sizeof(buf), &p);
2863 args[nb_args] = qemu_strdup(buf);
2864 nb_args++;
2865 if (ret < 0)
2866 break;
2868 *pnb_args = nb_args;
2871 static void monitor_find_completion(const char *cmdline)
2873 const char *cmdname;
2874 char *args[MAX_ARGS];
2875 int nb_args, i, len;
2876 const char *ptype, *str;
2877 const mon_cmd_t *cmd;
2878 const KeyDef *key;
2880 parse_cmdline(cmdline, &nb_args, args);
2881 #ifdef DEBUG_COMPLETION
2882 for(i = 0; i < nb_args; i++) {
2883 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2885 #endif
2887 /* if the line ends with a space, it means we want to complete the
2888 next arg */
2889 len = strlen(cmdline);
2890 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2891 if (nb_args >= MAX_ARGS)
2892 return;
2893 args[nb_args++] = qemu_strdup("");
2895 if (nb_args <= 1) {
2896 /* command completion */
2897 if (nb_args == 0)
2898 cmdname = "";
2899 else
2900 cmdname = args[0];
2901 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2902 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2903 cmd_completion(cmdname, cmd->name);
2905 } else {
2906 /* find the command */
2907 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2908 if (compare_cmd(args[0], cmd->name))
2909 goto found;
2911 return;
2912 found:
2913 ptype = cmd->args_type;
2914 for(i = 0; i < nb_args - 2; i++) {
2915 if (*ptype != '\0') {
2916 ptype++;
2917 while (*ptype == '?')
2918 ptype++;
2921 str = args[nb_args - 1];
2922 switch(*ptype) {
2923 case 'F':
2924 /* file completion */
2925 readline_set_completion_index(cur_mon->rs, strlen(str));
2926 file_completion(str);
2927 break;
2928 case 'B':
2929 /* block device name completion */
2930 readline_set_completion_index(cur_mon->rs, strlen(str));
2931 bdrv_iterate(block_completion_it, (void *)str);
2932 break;
2933 case 's':
2934 /* XXX: more generic ? */
2935 if (!strcmp(cmd->name, "info")) {
2936 readline_set_completion_index(cur_mon->rs, strlen(str));
2937 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2938 cmd_completion(str, cmd->name);
2940 } else if (!strcmp(cmd->name, "sendkey")) {
2941 char *sep = strrchr(str, '-');
2942 if (sep)
2943 str = sep + 1;
2944 readline_set_completion_index(cur_mon->rs, strlen(str));
2945 for(key = key_defs; key->name != NULL; key++) {
2946 cmd_completion(str, key->name);
2949 break;
2950 default:
2951 break;
2954 for(i = 0; i < nb_args; i++)
2955 qemu_free(args[i]);
2958 static int monitor_can_read(void *opaque)
2960 Monitor *mon = opaque;
2962 return (mon->suspend_cnt == 0) ? 128 : 0;
2965 static void monitor_read(void *opaque, const uint8_t *buf, int size)
2967 Monitor *old_mon = cur_mon;
2968 int i;
2970 cur_mon = opaque;
2972 if (cur_mon->rs) {
2973 for (i = 0; i < size; i++)
2974 readline_handle_byte(cur_mon->rs, buf[i]);
2975 } else {
2976 if (size == 0 || buf[size - 1] != 0)
2977 monitor_printf(cur_mon, "corrupted command\n");
2978 else
2979 monitor_handle_command(cur_mon, (char *)buf);
2982 cur_mon = old_mon;
2985 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
2987 monitor_suspend(mon);
2988 monitor_handle_command(mon, cmdline);
2989 monitor_resume(mon);
2992 int monitor_suspend(Monitor *mon)
2994 if (!mon->rs)
2995 return -ENOTTY;
2996 mon->suspend_cnt++;
2997 return 0;
3000 void monitor_resume(Monitor *mon)
3002 if (!mon->rs)
3003 return;
3004 if (--mon->suspend_cnt == 0)
3005 readline_show_prompt(mon->rs);
3008 static void monitor_event(void *opaque, int event)
3010 Monitor *mon = opaque;
3012 switch (event) {
3013 case CHR_EVENT_MUX_IN:
3014 readline_restart(mon->rs);
3015 monitor_resume(mon);
3016 monitor_flush(mon);
3017 break;
3019 case CHR_EVENT_MUX_OUT:
3020 if (mon->suspend_cnt == 0)
3021 monitor_printf(mon, "\n");
3022 monitor_flush(mon);
3023 monitor_suspend(mon);
3024 break;
3026 case CHR_EVENT_RESET:
3027 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3028 "information\n", QEMU_VERSION);
3029 if (mon->chr->focus == 0)
3030 readline_show_prompt(mon->rs);
3031 break;
3037 * Local variables:
3038 * c-indent-level: 4
3039 * c-basic-offset: 4
3040 * tab-width: 8
3041 * End:
3044 void monitor_init(CharDriverState *chr, int flags)
3046 static int is_first_init = 1;
3047 Monitor *mon;
3049 if (is_first_init) {
3050 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3051 is_first_init = 0;
3054 mon = qemu_mallocz(sizeof(*mon));
3056 mon->chr = chr;
3057 mon->flags = flags;
3058 if (mon->chr->focus != 0)
3059 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3060 if (flags & MONITOR_USE_READLINE) {
3061 mon->rs = readline_init(mon, monitor_find_completion);
3062 monitor_read_command(mon, 0);
3065 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3066 mon);
3068 LIST_INSERT_HEAD(&mon_list, mon, entry);
3069 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3070 cur_mon = mon;
3073 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3075 BlockDriverState *bs = opaque;
3076 int ret = 0;
3078 if (bdrv_set_key(bs, password) != 0) {
3079 monitor_printf(mon, "invalid password\n");
3080 ret = -EPERM;
3082 if (mon->password_completion_cb)
3083 mon->password_completion_cb(mon->password_opaque, ret);
3085 monitor_read_command(mon, 1);
3088 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3089 BlockDriverCompletionFunc *completion_cb,
3090 void *opaque)
3092 int err;
3094 if (!bdrv_key_required(bs)) {
3095 if (completion_cb)
3096 completion_cb(opaque, 0);
3097 return;
3100 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3101 bdrv_get_encrypted_filename(bs));
3103 mon->password_completion_cb = completion_cb;
3104 mon->password_opaque = opaque;
3106 err = monitor_read_password(mon, bdrv_password_cb, bs);
3108 if (err && completion_cb)
3109 completion_cb(opaque, err);