Re-add -drive boot= parameter help text
[qemu-kvm/fedora.git] / monitor.c
blob11e48c77e674ef92389cea34d2edd53bd019a238
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/usb.h"
27 #include "hw/pcmcia.h"
28 #include "hw/pc.h"
29 #include "hw/pci.h"
30 #include "gdbstub.h"
31 #include "net.h"
32 #include "qemu-char.h"
33 #include "sysemu.h"
34 #include "monitor.h"
35 #include "readline.h"
36 #include "console.h"
37 #include "block.h"
38 #include "audio/audio.h"
39 #include "disas.h"
40 #include "balloon.h"
41 #include "qemu-timer.h"
42 #include "migration.h"
43 #include "kvm.h"
44 #include "acl.h"
45 #include "exec-all.h"
47 #include "qemu-kvm.h"
49 //#define DEBUG
50 //#define DEBUG_COMPLETION
53 * Supported types:
55 * 'F' filename
56 * 'B' block device name
57 * 's' string (accept optional quote)
58 * 'i' 32 bit integer
59 * 'l' target long (32 or 64 bit)
60 * '/' optional gdb-like print format (like "/10x")
62 * '?' optional type (for 'F', 's' and 'i')
66 typedef struct mon_cmd_t {
67 const char *name;
68 const char *args_type;
69 void *handler;
70 const char *params;
71 const char *help;
72 } mon_cmd_t;
74 struct Monitor {
75 CharDriverState *chr;
76 int flags;
77 int suspend_cnt;
78 uint8_t outbuf[1024];
79 int outbuf_index;
80 ReadLineState *rs;
81 CPUState *mon_cpu;
82 BlockDriverCompletionFunc *password_completion_cb;
83 void *password_opaque;
84 LIST_ENTRY(Monitor) entry;
87 static LIST_HEAD(mon_list, Monitor) mon_list;
89 static const mon_cmd_t mon_cmds[];
90 static const mon_cmd_t info_cmds[];
92 Monitor *cur_mon = NULL;
94 static void monitor_command_cb(Monitor *mon, const char *cmdline,
95 void *opaque);
97 static void monitor_read_command(Monitor *mon, int show_prompt)
99 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
100 if (show_prompt)
101 readline_show_prompt(mon->rs);
104 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
105 void *opaque)
107 if (mon->rs) {
108 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
109 /* prompt is printed on return from the command handler */
110 return 0;
111 } else {
112 monitor_printf(mon, "terminal does not support password prompting\n");
113 return -ENOTTY;
117 void monitor_flush(Monitor *mon)
119 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
120 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
121 mon->outbuf_index = 0;
125 /* flush at every end of line or if the buffer is full */
126 static void monitor_puts(Monitor *mon, const char *str)
128 char c;
130 if (!mon)
131 return;
133 for(;;) {
134 c = *str++;
135 if (c == '\0')
136 break;
137 if (c == '\n')
138 mon->outbuf[mon->outbuf_index++] = '\r';
139 mon->outbuf[mon->outbuf_index++] = c;
140 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
141 || c == '\n')
142 monitor_flush(mon);
146 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
148 char buf[4096];
149 vsnprintf(buf, sizeof(buf), fmt, ap);
150 monitor_puts(mon, buf);
153 void monitor_printf(Monitor *mon, const char *fmt, ...)
155 va_list ap;
156 va_start(ap, fmt);
157 monitor_vprintf(mon, fmt, ap);
158 va_end(ap);
161 void monitor_print_filename(Monitor *mon, const char *filename)
163 int i;
165 for (i = 0; filename[i]; i++) {
166 switch (filename[i]) {
167 case ' ':
168 case '"':
169 case '\\':
170 monitor_printf(mon, "\\%c", filename[i]);
171 break;
172 case '\t':
173 monitor_printf(mon, "\\t");
174 break;
175 case '\r':
176 monitor_printf(mon, "\\r");
177 break;
178 case '\n':
179 monitor_printf(mon, "\\n");
180 break;
181 default:
182 monitor_printf(mon, "%c", filename[i]);
183 break;
188 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
190 va_list ap;
191 va_start(ap, fmt);
192 monitor_vprintf((Monitor *)stream, fmt, ap);
193 va_end(ap);
194 return 0;
197 static int compare_cmd(const char *name, const char *list)
199 const char *p, *pstart;
200 int len;
201 len = strlen(name);
202 p = list;
203 for(;;) {
204 pstart = p;
205 p = strchr(p, '|');
206 if (!p)
207 p = pstart + strlen(pstart);
208 if ((p - pstart) == len && !memcmp(pstart, name, len))
209 return 1;
210 if (*p == '\0')
211 break;
212 p++;
214 return 0;
217 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
218 const char *prefix, const char *name)
220 const mon_cmd_t *cmd;
222 for(cmd = cmds; cmd->name != NULL; cmd++) {
223 if (!name || !strcmp(name, cmd->name))
224 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
225 cmd->params, cmd->help);
229 static void help_cmd(Monitor *mon, const char *name)
231 if (name && !strcmp(name, "info")) {
232 help_cmd_dump(mon, info_cmds, "info ", NULL);
233 } else {
234 help_cmd_dump(mon, mon_cmds, "", name);
235 if (name && !strcmp(name, "log")) {
236 const CPULogItem *item;
237 monitor_printf(mon, "Log items (comma separated):\n");
238 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
239 for(item = cpu_log_items; item->mask != 0; item++) {
240 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
246 static void do_commit(Monitor *mon, const char *device)
248 int i, all_devices;
250 all_devices = !strcmp(device, "all");
251 for (i = 0; i < nb_drives; i++) {
252 if (all_devices ||
253 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
254 bdrv_commit(drives_table[i].bdrv);
258 static void do_info(Monitor *mon, const char *item)
260 const mon_cmd_t *cmd;
261 void (*handler)(Monitor *);
263 if (!item)
264 goto help;
265 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
266 if (compare_cmd(item, cmd->name))
267 goto found;
269 help:
270 help_cmd(mon, "info");
271 return;
272 found:
273 handler = cmd->handler;
274 handler(mon);
277 static void do_info_version(Monitor *mon)
279 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
282 static void do_info_name(Monitor *mon)
284 if (qemu_name)
285 monitor_printf(mon, "%s\n", qemu_name);
288 #if defined(TARGET_I386)
289 static void do_info_hpet(Monitor *mon)
291 monitor_printf(mon, "HPET is %s by QEMU\n",
292 (no_hpet) ? "disabled" : "enabled");
294 #endif
296 static void do_info_uuid(Monitor *mon)
298 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
299 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
300 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
301 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
302 qemu_uuid[14], qemu_uuid[15]);
305 /* get the current CPU defined by the user */
306 static int mon_set_cpu(int cpu_index)
308 CPUState *env;
310 for(env = first_cpu; env != NULL; env = env->next_cpu) {
311 if (env->cpu_index == cpu_index) {
312 cur_mon->mon_cpu = env;
313 return 0;
316 return -1;
319 static CPUState *mon_get_cpu(void)
321 if (!cur_mon->mon_cpu) {
322 mon_set_cpu(0);
324 cpu_synchronize_state(cur_mon->mon_cpu, 0);
325 return cur_mon->mon_cpu;
328 static void do_info_registers(Monitor *mon)
330 CPUState *env;
331 env = mon_get_cpu();
332 if (!env)
333 return;
334 #ifdef TARGET_I386
335 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
336 X86_DUMP_FPU);
337 #else
338 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
340 #endif
343 static void do_info_cpus(Monitor *mon)
345 CPUState *env;
347 /* just to set the default cpu if not already done */
348 mon_get_cpu();
350 for(env = first_cpu; env != NULL; env = env->next_cpu) {
351 cpu_synchronize_state(env, 0);
352 monitor_printf(mon, "%c CPU #%d:",
353 (env == mon->mon_cpu) ? '*' : ' ',
354 env->cpu_index);
355 #if defined(TARGET_I386)
356 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
357 env->eip + env->segs[R_CS].base);
358 #elif defined(TARGET_PPC)
359 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
360 #elif defined(TARGET_SPARC)
361 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
362 env->pc, env->npc);
363 #elif defined(TARGET_MIPS)
364 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
365 #endif
366 if (env->halted)
367 monitor_printf(mon, " (halted)");
368 monitor_printf(mon," thread_id=%d", env->thread_id);
369 monitor_printf(mon, "\n");
373 static void do_cpu_set(Monitor *mon, int index)
375 if (mon_set_cpu(index) < 0)
376 monitor_printf(mon, "Invalid CPU index\n");
379 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
381 int state;
383 if (!strcmp(status, "online"))
384 state = 1;
385 else if (!strcmp(status, "offline"))
386 state = 0;
387 else {
388 monitor_printf(mon, "invalid status: %s\n", status);
389 return;
391 #if defined(TARGET_I386) || defined(TARGET_X86_64)
392 qemu_system_cpu_hot_add(value, state);
393 #endif
396 static void do_info_jit(Monitor *mon)
398 dump_exec_info((FILE *)mon, monitor_fprintf);
401 static void do_info_history(Monitor *mon)
403 int i;
404 const char *str;
406 if (!mon->rs)
407 return;
408 i = 0;
409 for(;;) {
410 str = readline_get_history(mon->rs, i);
411 if (!str)
412 break;
413 monitor_printf(mon, "%d: '%s'\n", i, str);
414 i++;
418 #if defined(TARGET_PPC)
419 /* XXX: not implemented in other targets */
420 static void do_info_cpu_stats(Monitor *mon)
422 CPUState *env;
424 env = mon_get_cpu();
425 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
427 #endif
429 static void do_quit(Monitor *mon)
431 exit(0);
434 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
436 if (bdrv_is_inserted(bs)) {
437 if (!force) {
438 if (!bdrv_is_removable(bs)) {
439 monitor_printf(mon, "device is not removable\n");
440 return -1;
442 if (bdrv_is_locked(bs)) {
443 monitor_printf(mon, "device is locked\n");
444 return -1;
447 bdrv_close(bs);
449 return 0;
452 static void do_eject(Monitor *mon, int force, const char *filename)
454 BlockDriverState *bs;
456 bs = bdrv_find(filename);
457 if (!bs) {
458 monitor_printf(mon, "device not found\n");
459 return;
461 eject_device(mon, bs, force);
464 static void do_change_block(Monitor *mon, const char *device,
465 const char *filename, const char *fmt)
467 BlockDriverState *bs;
468 BlockDriver *drv = NULL;
470 bs = bdrv_find(device);
471 if (!bs) {
472 monitor_printf(mon, "device not found\n");
473 return;
475 if (fmt) {
476 drv = bdrv_find_format(fmt);
477 if (!drv) {
478 monitor_printf(mon, "invalid format %s\n", fmt);
479 return;
482 if (eject_device(mon, bs, 0) < 0)
483 return;
484 bdrv_open2(bs, filename, 0, drv);
485 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
488 static void change_vnc_password_cb(Monitor *mon, const char *password,
489 void *opaque)
491 if (vnc_display_password(NULL, password) < 0)
492 monitor_printf(mon, "could not set VNC server password\n");
494 monitor_read_command(mon, 1);
497 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
499 if (strcmp(target, "passwd") == 0 ||
500 strcmp(target, "password") == 0) {
501 if (arg) {
502 char password[9];
503 strncpy(password, arg, sizeof(password));
504 password[sizeof(password) - 1] = '\0';
505 change_vnc_password_cb(mon, password, NULL);
506 } else {
507 monitor_read_password(mon, change_vnc_password_cb, NULL);
509 } else {
510 if (vnc_display_open(NULL, target) < 0)
511 monitor_printf(mon, "could not start VNC server on %s\n", target);
515 static void do_change(Monitor *mon, const char *device, const char *target,
516 const char *arg)
518 if (strcmp(device, "vnc") == 0) {
519 do_change_vnc(mon, target, arg);
520 } else {
521 do_change_block(mon, device, target, arg);
525 static void do_screen_dump(Monitor *mon, const char *filename)
527 vga_hw_screen_dump(filename);
530 static void do_logfile(Monitor *mon, const char *filename)
532 cpu_set_log_filename(filename);
535 static void do_log(Monitor *mon, const char *items)
537 int mask;
539 if (!strcmp(items, "none")) {
540 mask = 0;
541 } else {
542 mask = cpu_str_to_log_mask(items);
543 if (!mask) {
544 help_cmd(mon, "log");
545 return;
548 cpu_set_log(mask);
551 static void do_singlestep(Monitor *mon, const char *option)
553 if (!option || !strcmp(option, "on")) {
554 singlestep = 1;
555 } else if (!strcmp(option, "off")) {
556 singlestep = 0;
557 } else {
558 monitor_printf(mon, "unexpected option %s\n", option);
562 static void do_stop(Monitor *mon)
564 vm_stop(EXCP_INTERRUPT);
567 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
569 struct bdrv_iterate_context {
570 Monitor *mon;
571 int err;
574 static void do_cont(Monitor *mon)
576 struct bdrv_iterate_context context = { mon, 0 };
578 bdrv_iterate(encrypted_bdrv_it, &context);
579 /* only resume the vm if all keys are set and valid */
580 if (!context.err)
581 vm_start();
584 static void bdrv_key_cb(void *opaque, int err)
586 Monitor *mon = opaque;
588 /* another key was set successfully, retry to continue */
589 if (!err)
590 do_cont(mon);
593 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
595 struct bdrv_iterate_context *context = opaque;
597 if (!context->err && bdrv_key_required(bs)) {
598 context->err = -EBUSY;
599 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
600 context->mon);
604 #ifdef CONFIG_GDBSTUB
605 static void do_gdbserver(Monitor *mon, const char *device)
607 if (!device)
608 device = "tcp::" DEFAULT_GDBSTUB_PORT;
609 if (gdbserver_start(device) < 0) {
610 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
611 device);
612 } else if (strcmp(device, "none") == 0) {
613 monitor_printf(mon, "Disabled gdbserver\n");
614 } else {
615 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
616 device);
619 #endif
621 static void monitor_printc(Monitor *mon, int c)
623 monitor_printf(mon, "'");
624 switch(c) {
625 case '\'':
626 monitor_printf(mon, "\\'");
627 break;
628 case '\\':
629 monitor_printf(mon, "\\\\");
630 break;
631 case '\n':
632 monitor_printf(mon, "\\n");
633 break;
634 case '\r':
635 monitor_printf(mon, "\\r");
636 break;
637 default:
638 if (c >= 32 && c <= 126) {
639 monitor_printf(mon, "%c", c);
640 } else {
641 monitor_printf(mon, "\\x%02x", c);
643 break;
645 monitor_printf(mon, "'");
648 static void memory_dump(Monitor *mon, int count, int format, int wsize,
649 target_phys_addr_t addr, int is_physical)
651 CPUState *env;
652 int nb_per_line, l, line_size, i, max_digits, len;
653 uint8_t buf[16];
654 uint64_t v;
656 if (format == 'i') {
657 int flags;
658 flags = 0;
659 env = mon_get_cpu();
660 if (!env && !is_physical)
661 return;
662 #ifdef TARGET_I386
663 if (wsize == 2) {
664 flags = 1;
665 } else if (wsize == 4) {
666 flags = 0;
667 } else {
668 /* as default we use the current CS size */
669 flags = 0;
670 if (env) {
671 #ifdef TARGET_X86_64
672 if ((env->efer & MSR_EFER_LMA) &&
673 (env->segs[R_CS].flags & DESC_L_MASK))
674 flags = 2;
675 else
676 #endif
677 if (!(env->segs[R_CS].flags & DESC_B_MASK))
678 flags = 1;
681 #endif
682 monitor_disas(mon, env, addr, count, is_physical, flags);
683 return;
686 len = wsize * count;
687 if (wsize == 1)
688 line_size = 8;
689 else
690 line_size = 16;
691 nb_per_line = line_size / wsize;
692 max_digits = 0;
694 switch(format) {
695 case 'o':
696 max_digits = (wsize * 8 + 2) / 3;
697 break;
698 default:
699 case 'x':
700 max_digits = (wsize * 8) / 4;
701 break;
702 case 'u':
703 case 'd':
704 max_digits = (wsize * 8 * 10 + 32) / 33;
705 break;
706 case 'c':
707 wsize = 1;
708 break;
711 while (len > 0) {
712 if (is_physical)
713 monitor_printf(mon, TARGET_FMT_plx ":", addr);
714 else
715 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
716 l = len;
717 if (l > line_size)
718 l = line_size;
719 if (is_physical) {
720 cpu_physical_memory_rw(addr, buf, l, 0);
721 } else {
722 env = mon_get_cpu();
723 if (!env)
724 break;
725 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
726 monitor_printf(mon, " Cannot access memory\n");
727 break;
730 i = 0;
731 while (i < l) {
732 switch(wsize) {
733 default:
734 case 1:
735 v = ldub_raw(buf + i);
736 break;
737 case 2:
738 v = lduw_raw(buf + i);
739 break;
740 case 4:
741 v = (uint32_t)ldl_raw(buf + i);
742 break;
743 case 8:
744 v = ldq_raw(buf + i);
745 break;
747 monitor_printf(mon, " ");
748 switch(format) {
749 case 'o':
750 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
751 break;
752 case 'x':
753 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
754 break;
755 case 'u':
756 monitor_printf(mon, "%*" PRIu64, max_digits, v);
757 break;
758 case 'd':
759 monitor_printf(mon, "%*" PRId64, max_digits, v);
760 break;
761 case 'c':
762 monitor_printc(mon, v);
763 break;
765 i += wsize;
767 monitor_printf(mon, "\n");
768 addr += l;
769 len -= l;
773 #if TARGET_LONG_BITS == 64
774 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
775 #else
776 #define GET_TLONG(h, l) (l)
777 #endif
779 static void do_memory_dump(Monitor *mon, int count, int format, int size,
780 uint32_t addrh, uint32_t addrl)
782 target_long addr = GET_TLONG(addrh, addrl);
783 memory_dump(mon, count, format, size, addr, 0);
786 #if TARGET_PHYS_ADDR_BITS > 32
787 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
788 #else
789 #define GET_TPHYSADDR(h, l) (l)
790 #endif
792 static void do_physical_memory_dump(Monitor *mon, int count, int format,
793 int size, uint32_t addrh, uint32_t addrl)
796 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
797 memory_dump(mon, count, format, size, addr, 1);
800 static void do_print(Monitor *mon, int count, int format, int size,
801 unsigned int valh, unsigned int vall)
803 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
804 #if TARGET_PHYS_ADDR_BITS == 32
805 switch(format) {
806 case 'o':
807 monitor_printf(mon, "%#o", val);
808 break;
809 case 'x':
810 monitor_printf(mon, "%#x", val);
811 break;
812 case 'u':
813 monitor_printf(mon, "%u", val);
814 break;
815 default:
816 case 'd':
817 monitor_printf(mon, "%d", val);
818 break;
819 case 'c':
820 monitor_printc(mon, val);
821 break;
823 #else
824 switch(format) {
825 case 'o':
826 monitor_printf(mon, "%#" PRIo64, val);
827 break;
828 case 'x':
829 monitor_printf(mon, "%#" PRIx64, val);
830 break;
831 case 'u':
832 monitor_printf(mon, "%" PRIu64, val);
833 break;
834 default:
835 case 'd':
836 monitor_printf(mon, "%" PRId64, val);
837 break;
838 case 'c':
839 monitor_printc(mon, val);
840 break;
842 #endif
843 monitor_printf(mon, "\n");
846 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
847 uint32_t size, const char *filename)
849 FILE *f;
850 target_long addr = GET_TLONG(valh, vall);
851 uint32_t l;
852 CPUState *env;
853 uint8_t buf[1024];
855 env = mon_get_cpu();
856 if (!env)
857 return;
859 f = fopen(filename, "wb");
860 if (!f) {
861 monitor_printf(mon, "could not open '%s'\n", filename);
862 return;
864 while (size != 0) {
865 l = sizeof(buf);
866 if (l > size)
867 l = size;
868 cpu_memory_rw_debug(env, addr, buf, l, 0);
869 fwrite(buf, 1, l, f);
870 addr += l;
871 size -= l;
873 fclose(f);
876 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
877 unsigned int vall, uint32_t size,
878 const char *filename)
880 FILE *f;
881 uint32_t l;
882 uint8_t buf[1024];
883 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
885 f = fopen(filename, "wb");
886 if (!f) {
887 monitor_printf(mon, "could not open '%s'\n", filename);
888 return;
890 while (size != 0) {
891 l = sizeof(buf);
892 if (l > size)
893 l = size;
894 cpu_physical_memory_rw(addr, buf, l, 0);
895 fwrite(buf, 1, l, f);
896 fflush(f);
897 addr += l;
898 size -= l;
900 fclose(f);
903 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
905 uint32_t addr;
906 uint8_t buf[1];
907 uint16_t sum;
909 sum = 0;
910 for(addr = start; addr < (start + size); addr++) {
911 cpu_physical_memory_rw(addr, buf, 1, 0);
912 /* BSD sum algorithm ('sum' Unix command) */
913 sum = (sum >> 1) | (sum << 15);
914 sum += buf[0];
916 monitor_printf(mon, "%05d\n", sum);
919 typedef struct {
920 int keycode;
921 const char *name;
922 } KeyDef;
924 static const KeyDef key_defs[] = {
925 { 0x2a, "shift" },
926 { 0x36, "shift_r" },
928 { 0x38, "alt" },
929 { 0xb8, "alt_r" },
930 { 0x64, "altgr" },
931 { 0xe4, "altgr_r" },
932 { 0x1d, "ctrl" },
933 { 0x9d, "ctrl_r" },
935 { 0xdd, "menu" },
937 { 0x01, "esc" },
939 { 0x02, "1" },
940 { 0x03, "2" },
941 { 0x04, "3" },
942 { 0x05, "4" },
943 { 0x06, "5" },
944 { 0x07, "6" },
945 { 0x08, "7" },
946 { 0x09, "8" },
947 { 0x0a, "9" },
948 { 0x0b, "0" },
949 { 0x0c, "minus" },
950 { 0x0d, "equal" },
951 { 0x0e, "backspace" },
953 { 0x0f, "tab" },
954 { 0x10, "q" },
955 { 0x11, "w" },
956 { 0x12, "e" },
957 { 0x13, "r" },
958 { 0x14, "t" },
959 { 0x15, "y" },
960 { 0x16, "u" },
961 { 0x17, "i" },
962 { 0x18, "o" },
963 { 0x19, "p" },
965 { 0x1c, "ret" },
967 { 0x1e, "a" },
968 { 0x1f, "s" },
969 { 0x20, "d" },
970 { 0x21, "f" },
971 { 0x22, "g" },
972 { 0x23, "h" },
973 { 0x24, "j" },
974 { 0x25, "k" },
975 { 0x26, "l" },
977 { 0x2c, "z" },
978 { 0x2d, "x" },
979 { 0x2e, "c" },
980 { 0x2f, "v" },
981 { 0x30, "b" },
982 { 0x31, "n" },
983 { 0x32, "m" },
984 { 0x33, "comma" },
985 { 0x34, "dot" },
986 { 0x35, "slash" },
988 { 0x37, "asterisk" },
990 { 0x39, "spc" },
991 { 0x3a, "caps_lock" },
992 { 0x3b, "f1" },
993 { 0x3c, "f2" },
994 { 0x3d, "f3" },
995 { 0x3e, "f4" },
996 { 0x3f, "f5" },
997 { 0x40, "f6" },
998 { 0x41, "f7" },
999 { 0x42, "f8" },
1000 { 0x43, "f9" },
1001 { 0x44, "f10" },
1002 { 0x45, "num_lock" },
1003 { 0x46, "scroll_lock" },
1005 { 0xb5, "kp_divide" },
1006 { 0x37, "kp_multiply" },
1007 { 0x4a, "kp_subtract" },
1008 { 0x4e, "kp_add" },
1009 { 0x9c, "kp_enter" },
1010 { 0x53, "kp_decimal" },
1011 { 0x54, "sysrq" },
1013 { 0x52, "kp_0" },
1014 { 0x4f, "kp_1" },
1015 { 0x50, "kp_2" },
1016 { 0x51, "kp_3" },
1017 { 0x4b, "kp_4" },
1018 { 0x4c, "kp_5" },
1019 { 0x4d, "kp_6" },
1020 { 0x47, "kp_7" },
1021 { 0x48, "kp_8" },
1022 { 0x49, "kp_9" },
1024 { 0x56, "<" },
1026 { 0x57, "f11" },
1027 { 0x58, "f12" },
1029 { 0xb7, "print" },
1031 { 0xc7, "home" },
1032 { 0xc9, "pgup" },
1033 { 0xd1, "pgdn" },
1034 { 0xcf, "end" },
1036 { 0xcb, "left" },
1037 { 0xc8, "up" },
1038 { 0xd0, "down" },
1039 { 0xcd, "right" },
1041 { 0xd2, "insert" },
1042 { 0xd3, "delete" },
1043 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1044 { 0xf0, "stop" },
1045 { 0xf1, "again" },
1046 { 0xf2, "props" },
1047 { 0xf3, "undo" },
1048 { 0xf4, "front" },
1049 { 0xf5, "copy" },
1050 { 0xf6, "open" },
1051 { 0xf7, "paste" },
1052 { 0xf8, "find" },
1053 { 0xf9, "cut" },
1054 { 0xfa, "lf" },
1055 { 0xfb, "help" },
1056 { 0xfc, "meta_l" },
1057 { 0xfd, "meta_r" },
1058 { 0xfe, "compose" },
1059 #endif
1060 { 0, NULL },
1063 static int get_keycode(const char *key)
1065 const KeyDef *p;
1066 char *endp;
1067 int ret;
1069 for(p = key_defs; p->name != NULL; p++) {
1070 if (!strcmp(key, p->name))
1071 return p->keycode;
1073 if (strstart(key, "0x", NULL)) {
1074 ret = strtoul(key, &endp, 0);
1075 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1076 return ret;
1078 return -1;
1081 #define MAX_KEYCODES 16
1082 static uint8_t keycodes[MAX_KEYCODES];
1083 static int nb_pending_keycodes;
1084 static QEMUTimer *key_timer;
1086 static void release_keys(void *opaque)
1088 int keycode;
1090 while (nb_pending_keycodes > 0) {
1091 nb_pending_keycodes--;
1092 keycode = keycodes[nb_pending_keycodes];
1093 if (keycode & 0x80)
1094 kbd_put_keycode(0xe0);
1095 kbd_put_keycode(keycode | 0x80);
1099 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1100 int hold_time)
1102 char keyname_buf[16];
1103 char *separator;
1104 int keyname_len, keycode, i;
1106 if (nb_pending_keycodes > 0) {
1107 qemu_del_timer(key_timer);
1108 release_keys(NULL);
1110 if (!has_hold_time)
1111 hold_time = 100;
1112 i = 0;
1113 while (1) {
1114 separator = strchr(string, '-');
1115 keyname_len = separator ? separator - string : strlen(string);
1116 if (keyname_len > 0) {
1117 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1118 if (keyname_len > sizeof(keyname_buf) - 1) {
1119 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1120 return;
1122 if (i == MAX_KEYCODES) {
1123 monitor_printf(mon, "too many keys\n");
1124 return;
1126 keyname_buf[keyname_len] = 0;
1127 keycode = get_keycode(keyname_buf);
1128 if (keycode < 0) {
1129 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1130 return;
1132 keycodes[i++] = keycode;
1134 if (!separator)
1135 break;
1136 string = separator + 1;
1138 nb_pending_keycodes = i;
1139 /* key down events */
1140 for (i = 0; i < nb_pending_keycodes; i++) {
1141 keycode = keycodes[i];
1142 if (keycode & 0x80)
1143 kbd_put_keycode(0xe0);
1144 kbd_put_keycode(keycode & 0x7f);
1146 /* delayed key up events */
1147 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1148 muldiv64(ticks_per_sec, hold_time, 1000));
1151 static int mouse_button_state;
1153 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1154 const char *dz_str)
1156 int dx, dy, dz;
1157 dx = strtol(dx_str, NULL, 0);
1158 dy = strtol(dy_str, NULL, 0);
1159 dz = 0;
1160 if (dz_str)
1161 dz = strtol(dz_str, NULL, 0);
1162 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1165 static void do_mouse_button(Monitor *mon, int button_state)
1167 mouse_button_state = button_state;
1168 kbd_mouse_event(0, 0, 0, mouse_button_state);
1171 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1172 int addr, int has_index, int index)
1174 uint32_t val;
1175 int suffix;
1177 if (has_index) {
1178 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1179 addr++;
1181 addr &= 0xffff;
1183 switch(size) {
1184 default:
1185 case 1:
1186 val = cpu_inb(NULL, addr);
1187 suffix = 'b';
1188 break;
1189 case 2:
1190 val = cpu_inw(NULL, addr);
1191 suffix = 'w';
1192 break;
1193 case 4:
1194 val = cpu_inl(NULL, addr);
1195 suffix = 'l';
1196 break;
1198 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1199 suffix, addr, size * 2, val);
1202 /* boot_set handler */
1203 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1204 static void *boot_opaque;
1206 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1208 qemu_boot_set_handler = func;
1209 boot_opaque = opaque;
1212 static void do_boot_set(Monitor *mon, const char *bootdevice)
1214 int res;
1216 if (qemu_boot_set_handler) {
1217 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1218 if (res == 0)
1219 monitor_printf(mon, "boot device list now set to %s\n",
1220 bootdevice);
1221 else
1222 monitor_printf(mon, "setting boot device list failed with "
1223 "error %i\n", res);
1224 } else {
1225 monitor_printf(mon, "no function defined to set boot device list for "
1226 "this architecture\n");
1230 static void do_system_reset(Monitor *mon)
1232 qemu_system_reset_request();
1235 static void do_system_powerdown(Monitor *mon)
1237 qemu_system_powerdown_request();
1240 #if defined(TARGET_I386)
1241 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1243 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1244 addr,
1245 pte & mask,
1246 pte & PG_GLOBAL_MASK ? 'G' : '-',
1247 pte & PG_PSE_MASK ? 'P' : '-',
1248 pte & PG_DIRTY_MASK ? 'D' : '-',
1249 pte & PG_ACCESSED_MASK ? 'A' : '-',
1250 pte & PG_PCD_MASK ? 'C' : '-',
1251 pte & PG_PWT_MASK ? 'T' : '-',
1252 pte & PG_USER_MASK ? 'U' : '-',
1253 pte & PG_RW_MASK ? 'W' : '-');
1256 static void tlb_info(Monitor *mon)
1258 CPUState *env;
1259 int l1, l2;
1260 uint32_t pgd, pde, pte;
1262 env = mon_get_cpu();
1263 if (!env)
1264 return;
1266 if (!(env->cr[0] & CR0_PG_MASK)) {
1267 monitor_printf(mon, "PG disabled\n");
1268 return;
1270 pgd = env->cr[3] & ~0xfff;
1271 for(l1 = 0; l1 < 1024; l1++) {
1272 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1273 pde = le32_to_cpu(pde);
1274 if (pde & PG_PRESENT_MASK) {
1275 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1276 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1277 } else {
1278 for(l2 = 0; l2 < 1024; l2++) {
1279 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1280 (uint8_t *)&pte, 4);
1281 pte = le32_to_cpu(pte);
1282 if (pte & PG_PRESENT_MASK) {
1283 print_pte(mon, (l1 << 22) + (l2 << 12),
1284 pte & ~PG_PSE_MASK,
1285 ~0xfff);
1293 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1294 uint32_t end, int prot)
1296 int prot1;
1297 prot1 = *plast_prot;
1298 if (prot != prot1) {
1299 if (*pstart != -1) {
1300 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1301 *pstart, end, end - *pstart,
1302 prot1 & PG_USER_MASK ? 'u' : '-',
1303 'r',
1304 prot1 & PG_RW_MASK ? 'w' : '-');
1306 if (prot != 0)
1307 *pstart = end;
1308 else
1309 *pstart = -1;
1310 *plast_prot = prot;
1314 static void mem_info(Monitor *mon)
1316 CPUState *env;
1317 int l1, l2, prot, last_prot;
1318 uint32_t pgd, pde, pte, start, end;
1320 env = mon_get_cpu();
1321 if (!env)
1322 return;
1324 if (!(env->cr[0] & CR0_PG_MASK)) {
1325 monitor_printf(mon, "PG disabled\n");
1326 return;
1328 pgd = env->cr[3] & ~0xfff;
1329 last_prot = 0;
1330 start = -1;
1331 for(l1 = 0; l1 < 1024; l1++) {
1332 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1333 pde = le32_to_cpu(pde);
1334 end = l1 << 22;
1335 if (pde & PG_PRESENT_MASK) {
1336 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1337 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1338 mem_print(mon, &start, &last_prot, end, prot);
1339 } else {
1340 for(l2 = 0; l2 < 1024; l2++) {
1341 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1342 (uint8_t *)&pte, 4);
1343 pte = le32_to_cpu(pte);
1344 end = (l1 << 22) + (l2 << 12);
1345 if (pte & PG_PRESENT_MASK) {
1346 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1347 } else {
1348 prot = 0;
1350 mem_print(mon, &start, &last_prot, end, prot);
1353 } else {
1354 prot = 0;
1355 mem_print(mon, &start, &last_prot, end, prot);
1359 #endif
1361 #if defined(TARGET_SH4)
1363 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1365 monitor_printf(mon, " tlb%i:\t"
1366 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1367 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1368 "dirty=%hhu writethrough=%hhu\n",
1369 idx,
1370 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1371 tlb->v, tlb->sh, tlb->c, tlb->pr,
1372 tlb->d, tlb->wt);
1375 static void tlb_info(Monitor *mon)
1377 CPUState *env = mon_get_cpu();
1378 int i;
1380 monitor_printf (mon, "ITLB:\n");
1381 for (i = 0 ; i < ITLB_SIZE ; i++)
1382 print_tlb (mon, i, &env->itlb[i]);
1383 monitor_printf (mon, "UTLB:\n");
1384 for (i = 0 ; i < UTLB_SIZE ; i++)
1385 print_tlb (mon, i, &env->utlb[i]);
1388 #endif
1390 static void do_info_kqemu(Monitor *mon)
1392 #ifdef CONFIG_KQEMU
1393 CPUState *env;
1394 int val;
1395 val = 0;
1396 env = mon_get_cpu();
1397 if (!env) {
1398 monitor_printf(mon, "No cpu initialized yet");
1399 return;
1401 val = env->kqemu_enabled;
1402 monitor_printf(mon, "kqemu support: ");
1403 switch(val) {
1404 default:
1405 case 0:
1406 monitor_printf(mon, "disabled\n");
1407 break;
1408 case 1:
1409 monitor_printf(mon, "enabled for user code\n");
1410 break;
1411 case 2:
1412 monitor_printf(mon, "enabled for user and kernel code\n");
1413 break;
1415 #else
1416 monitor_printf(mon, "kqemu support: not compiled\n");
1417 #endif
1420 static void do_info_kvm(Monitor *mon)
1422 #if defined(USE_KVM) || defined(CONFIG_KVM)
1423 monitor_printf(mon, "kvm support: ");
1424 if (kvm_enabled())
1425 monitor_printf(mon, "enabled\n");
1426 else
1427 monitor_printf(mon, "disabled\n");
1428 #else
1429 monitor_printf(mon, "kvm support: not compiled\n");
1430 #endif
1433 static void do_info_numa(Monitor *mon)
1435 int i, j;
1436 CPUState *env;
1438 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1439 for (i = 0; i < nb_numa_nodes; i++) {
1440 monitor_printf(mon, "node %d cpus:", i);
1441 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1442 if (env->numa_node == i) {
1443 monitor_printf(mon, " %d", env->cpu_index);
1446 monitor_printf(mon, "\n");
1447 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1448 node_mem[i] >> 20);
1452 #ifdef CONFIG_PROFILER
1454 int64_t kqemu_time;
1455 int64_t qemu_time;
1456 int64_t kqemu_exec_count;
1457 int64_t dev_time;
1458 int64_t kqemu_ret_int_count;
1459 int64_t kqemu_ret_excp_count;
1460 int64_t kqemu_ret_intr_count;
1462 static void do_info_profile(Monitor *mon)
1464 int64_t total;
1465 total = qemu_time;
1466 if (total == 0)
1467 total = 1;
1468 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1469 dev_time, dev_time / (double)ticks_per_sec);
1470 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1471 qemu_time, qemu_time / (double)ticks_per_sec);
1472 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1473 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1474 PRId64 "\n",
1475 kqemu_time, kqemu_time / (double)ticks_per_sec,
1476 kqemu_time / (double)total * 100.0,
1477 kqemu_exec_count,
1478 kqemu_ret_int_count,
1479 kqemu_ret_excp_count,
1480 kqemu_ret_intr_count);
1481 qemu_time = 0;
1482 kqemu_time = 0;
1483 kqemu_exec_count = 0;
1484 dev_time = 0;
1485 kqemu_ret_int_count = 0;
1486 kqemu_ret_excp_count = 0;
1487 kqemu_ret_intr_count = 0;
1488 #ifdef CONFIG_KQEMU
1489 kqemu_record_dump();
1490 #endif
1492 #else
1493 static void do_info_profile(Monitor *mon)
1495 monitor_printf(mon, "Internal profiler not compiled\n");
1497 #endif
1499 /* Capture support */
1500 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1502 static void do_info_capture(Monitor *mon)
1504 int i;
1505 CaptureState *s;
1507 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1508 monitor_printf(mon, "[%d]: ", i);
1509 s->ops.info (s->opaque);
1513 static void do_stop_capture(Monitor *mon, int n)
1515 int i;
1516 CaptureState *s;
1518 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1519 if (i == n) {
1520 s->ops.destroy (s->opaque);
1521 LIST_REMOVE (s, entries);
1522 qemu_free (s);
1523 return;
1528 #ifdef HAS_AUDIO
1529 static void do_wav_capture(Monitor *mon, const char *path,
1530 int has_freq, int freq,
1531 int has_bits, int bits,
1532 int has_channels, int nchannels)
1534 CaptureState *s;
1536 s = qemu_mallocz (sizeof (*s));
1538 freq = has_freq ? freq : 44100;
1539 bits = has_bits ? bits : 16;
1540 nchannels = has_channels ? nchannels : 2;
1542 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1543 monitor_printf(mon, "Faied to add wave capture\n");
1544 qemu_free (s);
1546 LIST_INSERT_HEAD (&capture_head, s, entries);
1548 #endif
1550 #if defined(TARGET_I386)
1551 static void do_inject_nmi(Monitor *mon, int cpu_index)
1553 CPUState *env;
1555 for (env = first_cpu; env != NULL; env = env->next_cpu)
1556 if (env->cpu_index == cpu_index) {
1557 if (kvm_enabled())
1558 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1559 else
1560 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1561 break;
1564 #endif
1566 static void do_info_status(Monitor *mon)
1568 if (vm_running) {
1569 if (singlestep) {
1570 monitor_printf(mon, "VM status: running (single step mode)\n");
1571 } else {
1572 monitor_printf(mon, "VM status: running\n");
1574 } else
1575 monitor_printf(mon, "VM status: paused\n");
1579 static void do_balloon(Monitor *mon, int value)
1581 ram_addr_t target = value;
1582 qemu_balloon(target << 20);
1585 static void do_info_balloon(Monitor *mon)
1587 ram_addr_t actual;
1589 actual = qemu_balloon_status();
1590 if (kvm_enabled() && !kvm_has_sync_mmu())
1591 monitor_printf(mon, "Using KVM without synchronous MMU, "
1592 "ballooning disabled\n");
1593 else if (actual == 0)
1594 monitor_printf(mon, "Ballooning not activated in VM\n");
1595 else
1596 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1599 static void do_acl(Monitor *mon,
1600 const char *command,
1601 const char *aclname,
1602 const char *match,
1603 int has_index,
1604 int index)
1606 qemu_acl *acl;
1608 acl = qemu_acl_find(aclname);
1609 if (!acl) {
1610 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1611 return;
1614 if (strcmp(command, "show") == 0) {
1615 int i = 0;
1616 qemu_acl_entry *entry;
1617 monitor_printf(mon, "policy: %s\n",
1618 acl->defaultDeny ? "deny" : "allow");
1619 TAILQ_FOREACH(entry, &acl->entries, next) {
1620 i++;
1621 monitor_printf(mon, "%d: %s %s\n", i,
1622 entry->deny ? "deny" : "allow",
1623 entry->match);
1625 } else if (strcmp(command, "reset") == 0) {
1626 qemu_acl_reset(acl);
1627 monitor_printf(mon, "acl: removed all rules\n");
1628 } else if (strcmp(command, "policy") == 0) {
1629 if (!match) {
1630 monitor_printf(mon, "acl: missing policy parameter\n");
1631 return;
1634 if (strcmp(match, "allow") == 0) {
1635 acl->defaultDeny = 0;
1636 monitor_printf(mon, "acl: policy set to 'allow'\n");
1637 } else if (strcmp(match, "deny") == 0) {
1638 acl->defaultDeny = 1;
1639 monitor_printf(mon, "acl: policy set to 'deny'\n");
1640 } else {
1641 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1643 } else if ((strcmp(command, "allow") == 0) ||
1644 (strcmp(command, "deny") == 0)) {
1645 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1646 int ret;
1648 if (!match) {
1649 monitor_printf(mon, "acl: missing match parameter\n");
1650 return;
1653 if (has_index)
1654 ret = qemu_acl_insert(acl, deny, match, index);
1655 else
1656 ret = qemu_acl_append(acl, deny, match);
1657 if (ret < 0)
1658 monitor_printf(mon, "acl: unable to add acl entry\n");
1659 else
1660 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1661 } else if (strcmp(command, "remove") == 0) {
1662 int ret;
1664 if (!match) {
1665 monitor_printf(mon, "acl: missing match parameter\n");
1666 return;
1669 ret = qemu_acl_remove(acl, match);
1670 if (ret < 0)
1671 monitor_printf(mon, "acl: no matching acl entry\n");
1672 else
1673 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1674 } else {
1675 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1679 /* Please update qemu-doc.texi when adding or changing commands */
1680 static const mon_cmd_t mon_cmds[] = {
1681 { "help|?", "s?", help_cmd,
1682 "[cmd]", "show the help" },
1683 { "commit", "s", do_commit,
1684 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1685 { "info", "s?", do_info,
1686 "[subcommand]", "show various information about the system state" },
1687 { "q|quit", "", do_quit,
1688 "", "quit the emulator" },
1689 { "eject", "-fB", do_eject,
1690 "[-f] device", "eject a removable medium (use -f to force it)" },
1691 { "change", "BFs?", do_change,
1692 "device filename [format]", "change a removable medium, optional format" },
1693 { "screendump", "F", do_screen_dump,
1694 "filename", "save screen into PPM image 'filename'" },
1695 { "logfile", "F", do_logfile,
1696 "filename", "output logs to 'filename'" },
1697 { "log", "s", do_log,
1698 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1699 { "savevm", "s?", do_savevm,
1700 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1701 { "loadvm", "s", do_loadvm,
1702 "tag|id", "restore a VM snapshot from its tag or id" },
1703 { "delvm", "s", do_delvm,
1704 "tag|id", "delete a VM snapshot from its tag or id" },
1705 { "singlestep", "s?", do_singlestep,
1706 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1707 { "stop", "", do_stop,
1708 "", "stop emulation", },
1709 { "c|cont", "", do_cont,
1710 "", "resume emulation", },
1711 #ifdef CONFIG_GDBSTUB
1712 { "gdbserver", "s?", do_gdbserver,
1713 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
1714 #endif
1715 { "x", "/l", do_memory_dump,
1716 "/fmt addr", "virtual memory dump starting at 'addr'", },
1717 { "xp", "/l", do_physical_memory_dump,
1718 "/fmt addr", "physical memory dump starting at 'addr'", },
1719 { "p|print", "/l", do_print,
1720 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1721 { "i", "/ii.", do_ioport_read,
1722 "/fmt addr", "I/O port read" },
1724 { "sendkey", "si?", do_sendkey,
1725 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1726 { "system_reset", "", do_system_reset,
1727 "", "reset the system" },
1728 { "system_powerdown", "", do_system_powerdown,
1729 "", "send system power down event" },
1730 { "sum", "ii", do_sum,
1731 "addr size", "compute the checksum of a memory region" },
1732 { "usb_add", "s", do_usb_add,
1733 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1734 { "usb_del", "s", do_usb_del,
1735 "device", "remove USB device 'bus.addr'" },
1736 { "cpu", "i", do_cpu_set,
1737 "index", "set the default CPU" },
1738 { "mouse_move", "sss?", do_mouse_move,
1739 "dx dy [dz]", "send mouse move events" },
1740 { "mouse_button", "i", do_mouse_button,
1741 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1742 { "mouse_set", "i", do_mouse_set,
1743 "index", "set which mouse device receives events" },
1744 #ifdef HAS_AUDIO
1745 { "wavcapture", "si?i?i?", do_wav_capture,
1746 "path [frequency [bits [channels]]]",
1747 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1748 #endif
1749 { "stopcapture", "i", do_stop_capture,
1750 "capture index", "stop capture" },
1751 { "memsave", "lis", do_memory_save,
1752 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1753 { "pmemsave", "lis", do_physical_memory_save,
1754 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1755 { "boot_set", "s", do_boot_set,
1756 "bootdevice", "define new values for the boot device list" },
1757 #if defined(TARGET_I386)
1758 { "nmi", "i", do_inject_nmi,
1759 "cpu", "inject an NMI on the given CPU", },
1760 #endif
1761 { "migrate", "-ds", do_migrate,
1762 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1763 { "migrate_cancel", "", do_migrate_cancel,
1764 "", "cancel the current VM migration" },
1765 { "migrate_set_speed", "s", do_migrate_set_speed,
1766 "value", "set maximum speed (in bytes) for migrations" },
1767 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1768 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1769 "[file=file][,if=type][,bus=n]\n"
1770 "[,unit=m][,media=d][index=i]\n"
1771 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1772 "[snapshot=on|off][,cache=on|off]",
1773 "add drive to PCI storage controller" },
1774 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]", "hot-add PCI device" },
1775 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1776 #endif
1777 { "host_net_add", "ss?", net_host_device_add,
1778 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
1779 { "host_net_remove", "is", net_host_device_remove,
1780 "vlan_id name", "remove host VLAN client" },
1781 #ifdef CONFIG_SLIRP
1782 { "host_net_redir", "s", net_slirp_redir,
1783 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
1784 #endif
1785 { "balloon", "i", do_balloon,
1786 "target", "request VM to change it's memory allocation (in MB)" },
1787 { "set_link", "ss", do_set_link,
1788 "name up|down", "change the link status of a network adapter" },
1789 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match> [<index>]]\n",
1790 "acl show vnc.username\n"
1791 "acl policy vnc.username deny\n"
1792 "acl allow vnc.username fred\n"
1793 "acl deny vnc.username bob\n"
1794 "acl reset vnc.username\n" },
1795 { "set_link", "ss", do_set_link, "name [up|down]" },
1796 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1797 { NULL, NULL, },
1800 /* Please update qemu-doc.texi when adding or changing commands */
1801 static const mon_cmd_t info_cmds[] = {
1802 { "version", "", do_info_version,
1803 "", "show the version of QEMU" },
1804 { "network", "", do_info_network,
1805 "", "show the network state" },
1806 { "chardev", "", qemu_chr_info,
1807 "", "show the character devices" },
1808 { "block", "", bdrv_info,
1809 "", "show the block devices" },
1810 { "blockstats", "", bdrv_info_stats,
1811 "", "show block device statistics" },
1812 { "registers", "", do_info_registers,
1813 "", "show the cpu registers" },
1814 { "cpus", "", do_info_cpus,
1815 "", "show infos for each CPU" },
1816 { "history", "", do_info_history,
1817 "", "show the command line history", },
1818 { "irq", "", irq_info,
1819 "", "show the interrupts statistics (if available)", },
1820 { "pic", "", pic_info,
1821 "", "show i8259 (PIC) state", },
1822 { "pci", "", pci_info,
1823 "", "show PCI info", },
1824 #if defined(TARGET_I386) || defined(TARGET_SH4)
1825 { "tlb", "", tlb_info,
1826 "", "show virtual to physical memory mappings", },
1827 #endif
1828 #if defined(TARGET_I386)
1829 { "mem", "", mem_info,
1830 "", "show the active virtual memory mappings", },
1831 { "hpet", "", do_info_hpet,
1832 "", "show state of HPET", },
1833 #endif
1834 { "jit", "", do_info_jit,
1835 "", "show dynamic compiler info", },
1836 { "kqemu", "", do_info_kqemu,
1837 "", "show KQEMU information", },
1838 { "kvm", "", do_info_kvm,
1839 "", "show KVM information", },
1840 { "numa", "", do_info_numa,
1841 "", "show NUMA information", },
1842 { "usb", "", usb_info,
1843 "", "show guest USB devices", },
1844 { "usbhost", "", usb_host_info,
1845 "", "show host USB devices", },
1846 { "profile", "", do_info_profile,
1847 "", "show profiling information", },
1848 { "capture", "", do_info_capture,
1849 "", "show capture information" },
1850 { "snapshots", "", do_info_snapshots,
1851 "", "show the currently saved VM snapshots" },
1852 { "status", "", do_info_status,
1853 "", "show the current VM status (running|paused)" },
1854 { "pcmcia", "", pcmcia_info,
1855 "", "show guest PCMCIA status" },
1856 { "mice", "", do_info_mice,
1857 "", "show which guest mouse is receiving events" },
1858 { "vnc", "", do_info_vnc,
1859 "", "show the vnc server status"},
1860 { "name", "", do_info_name,
1861 "", "show the current VM name" },
1862 { "uuid", "", do_info_uuid,
1863 "", "show the current VM UUID" },
1864 #if defined(TARGET_PPC)
1865 { "cpustats", "", do_info_cpu_stats,
1866 "", "show CPU statistics", },
1867 #endif
1868 #if defined(CONFIG_SLIRP)
1869 { "slirp", "", do_info_slirp,
1870 "", "show SLIRP statistics", },
1871 #endif
1872 { "migrate", "", do_info_migrate, "", "show migration status" },
1873 { "balloon", "", do_info_balloon,
1874 "", "show balloon information" },
1875 { NULL, NULL, },
1878 /*******************************************************************/
1880 static const char *pch;
1881 static jmp_buf expr_env;
1883 #define MD_TLONG 0
1884 #define MD_I32 1
1886 typedef struct MonitorDef {
1887 const char *name;
1888 int offset;
1889 target_long (*get_value)(const struct MonitorDef *md, int val);
1890 int type;
1891 } MonitorDef;
1893 #if defined(TARGET_I386)
1894 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1896 CPUState *env = mon_get_cpu();
1897 if (!env)
1898 return 0;
1899 return env->eip + env->segs[R_CS].base;
1901 #endif
1903 #if defined(TARGET_PPC)
1904 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1906 CPUState *env = mon_get_cpu();
1907 unsigned int u;
1908 int i;
1910 if (!env)
1911 return 0;
1913 u = 0;
1914 for (i = 0; i < 8; i++)
1915 u |= env->crf[i] << (32 - (4 * i));
1917 return u;
1920 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1922 CPUState *env = mon_get_cpu();
1923 if (!env)
1924 return 0;
1925 return env->msr;
1928 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1930 CPUState *env = mon_get_cpu();
1931 if (!env)
1932 return 0;
1933 return env->xer;
1936 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1938 CPUState *env = mon_get_cpu();
1939 if (!env)
1940 return 0;
1941 return cpu_ppc_load_decr(env);
1944 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1946 CPUState *env = mon_get_cpu();
1947 if (!env)
1948 return 0;
1949 return cpu_ppc_load_tbu(env);
1952 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1954 CPUState *env = mon_get_cpu();
1955 if (!env)
1956 return 0;
1957 return cpu_ppc_load_tbl(env);
1959 #endif
1961 #if defined(TARGET_SPARC)
1962 #ifndef TARGET_SPARC64
1963 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1965 CPUState *env = mon_get_cpu();
1966 if (!env)
1967 return 0;
1968 return GET_PSR(env);
1970 #endif
1972 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1974 CPUState *env = mon_get_cpu();
1975 if (!env)
1976 return 0;
1977 return env->regwptr[val];
1979 #endif
1981 static const MonitorDef monitor_defs[] = {
1982 #ifdef TARGET_I386
1984 #define SEG(name, seg) \
1985 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1986 { name ".base", offsetof(CPUState, segs[seg].base) },\
1987 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1989 { "eax", offsetof(CPUState, regs[0]) },
1990 { "ecx", offsetof(CPUState, regs[1]) },
1991 { "edx", offsetof(CPUState, regs[2]) },
1992 { "ebx", offsetof(CPUState, regs[3]) },
1993 { "esp|sp", offsetof(CPUState, regs[4]) },
1994 { "ebp|fp", offsetof(CPUState, regs[5]) },
1995 { "esi", offsetof(CPUState, regs[6]) },
1996 { "edi", offsetof(CPUState, regs[7]) },
1997 #ifdef TARGET_X86_64
1998 { "r8", offsetof(CPUState, regs[8]) },
1999 { "r9", offsetof(CPUState, regs[9]) },
2000 { "r10", offsetof(CPUState, regs[10]) },
2001 { "r11", offsetof(CPUState, regs[11]) },
2002 { "r12", offsetof(CPUState, regs[12]) },
2003 { "r13", offsetof(CPUState, regs[13]) },
2004 { "r14", offsetof(CPUState, regs[14]) },
2005 { "r15", offsetof(CPUState, regs[15]) },
2006 #endif
2007 { "eflags", offsetof(CPUState, eflags) },
2008 { "eip", offsetof(CPUState, eip) },
2009 SEG("cs", R_CS)
2010 SEG("ds", R_DS)
2011 SEG("es", R_ES)
2012 SEG("ss", R_SS)
2013 SEG("fs", R_FS)
2014 SEG("gs", R_GS)
2015 { "pc", 0, monitor_get_pc, },
2016 #elif defined(TARGET_PPC)
2017 /* General purpose registers */
2018 { "r0", offsetof(CPUState, gpr[0]) },
2019 { "r1", offsetof(CPUState, gpr[1]) },
2020 { "r2", offsetof(CPUState, gpr[2]) },
2021 { "r3", offsetof(CPUState, gpr[3]) },
2022 { "r4", offsetof(CPUState, gpr[4]) },
2023 { "r5", offsetof(CPUState, gpr[5]) },
2024 { "r6", offsetof(CPUState, gpr[6]) },
2025 { "r7", offsetof(CPUState, gpr[7]) },
2026 { "r8", offsetof(CPUState, gpr[8]) },
2027 { "r9", offsetof(CPUState, gpr[9]) },
2028 { "r10", offsetof(CPUState, gpr[10]) },
2029 { "r11", offsetof(CPUState, gpr[11]) },
2030 { "r12", offsetof(CPUState, gpr[12]) },
2031 { "r13", offsetof(CPUState, gpr[13]) },
2032 { "r14", offsetof(CPUState, gpr[14]) },
2033 { "r15", offsetof(CPUState, gpr[15]) },
2034 { "r16", offsetof(CPUState, gpr[16]) },
2035 { "r17", offsetof(CPUState, gpr[17]) },
2036 { "r18", offsetof(CPUState, gpr[18]) },
2037 { "r19", offsetof(CPUState, gpr[19]) },
2038 { "r20", offsetof(CPUState, gpr[20]) },
2039 { "r21", offsetof(CPUState, gpr[21]) },
2040 { "r22", offsetof(CPUState, gpr[22]) },
2041 { "r23", offsetof(CPUState, gpr[23]) },
2042 { "r24", offsetof(CPUState, gpr[24]) },
2043 { "r25", offsetof(CPUState, gpr[25]) },
2044 { "r26", offsetof(CPUState, gpr[26]) },
2045 { "r27", offsetof(CPUState, gpr[27]) },
2046 { "r28", offsetof(CPUState, gpr[28]) },
2047 { "r29", offsetof(CPUState, gpr[29]) },
2048 { "r30", offsetof(CPUState, gpr[30]) },
2049 { "r31", offsetof(CPUState, gpr[31]) },
2050 /* Floating point registers */
2051 { "f0", offsetof(CPUState, fpr[0]) },
2052 { "f1", offsetof(CPUState, fpr[1]) },
2053 { "f2", offsetof(CPUState, fpr[2]) },
2054 { "f3", offsetof(CPUState, fpr[3]) },
2055 { "f4", offsetof(CPUState, fpr[4]) },
2056 { "f5", offsetof(CPUState, fpr[5]) },
2057 { "f6", offsetof(CPUState, fpr[6]) },
2058 { "f7", offsetof(CPUState, fpr[7]) },
2059 { "f8", offsetof(CPUState, fpr[8]) },
2060 { "f9", offsetof(CPUState, fpr[9]) },
2061 { "f10", offsetof(CPUState, fpr[10]) },
2062 { "f11", offsetof(CPUState, fpr[11]) },
2063 { "f12", offsetof(CPUState, fpr[12]) },
2064 { "f13", offsetof(CPUState, fpr[13]) },
2065 { "f14", offsetof(CPUState, fpr[14]) },
2066 { "f15", offsetof(CPUState, fpr[15]) },
2067 { "f16", offsetof(CPUState, fpr[16]) },
2068 { "f17", offsetof(CPUState, fpr[17]) },
2069 { "f18", offsetof(CPUState, fpr[18]) },
2070 { "f19", offsetof(CPUState, fpr[19]) },
2071 { "f20", offsetof(CPUState, fpr[20]) },
2072 { "f21", offsetof(CPUState, fpr[21]) },
2073 { "f22", offsetof(CPUState, fpr[22]) },
2074 { "f23", offsetof(CPUState, fpr[23]) },
2075 { "f24", offsetof(CPUState, fpr[24]) },
2076 { "f25", offsetof(CPUState, fpr[25]) },
2077 { "f26", offsetof(CPUState, fpr[26]) },
2078 { "f27", offsetof(CPUState, fpr[27]) },
2079 { "f28", offsetof(CPUState, fpr[28]) },
2080 { "f29", offsetof(CPUState, fpr[29]) },
2081 { "f30", offsetof(CPUState, fpr[30]) },
2082 { "f31", offsetof(CPUState, fpr[31]) },
2083 { "fpscr", offsetof(CPUState, fpscr) },
2084 /* Next instruction pointer */
2085 { "nip|pc", offsetof(CPUState, nip) },
2086 { "lr", offsetof(CPUState, lr) },
2087 { "ctr", offsetof(CPUState, ctr) },
2088 { "decr", 0, &monitor_get_decr, },
2089 { "ccr", 0, &monitor_get_ccr, },
2090 /* Machine state register */
2091 { "msr", 0, &monitor_get_msr, },
2092 { "xer", 0, &monitor_get_xer, },
2093 { "tbu", 0, &monitor_get_tbu, },
2094 { "tbl", 0, &monitor_get_tbl, },
2095 #if defined(TARGET_PPC64)
2096 /* Address space register */
2097 { "asr", offsetof(CPUState, asr) },
2098 #endif
2099 /* Segment registers */
2100 { "sdr1", offsetof(CPUState, sdr1) },
2101 { "sr0", offsetof(CPUState, sr[0]) },
2102 { "sr1", offsetof(CPUState, sr[1]) },
2103 { "sr2", offsetof(CPUState, sr[2]) },
2104 { "sr3", offsetof(CPUState, sr[3]) },
2105 { "sr4", offsetof(CPUState, sr[4]) },
2106 { "sr5", offsetof(CPUState, sr[5]) },
2107 { "sr6", offsetof(CPUState, sr[6]) },
2108 { "sr7", offsetof(CPUState, sr[7]) },
2109 { "sr8", offsetof(CPUState, sr[8]) },
2110 { "sr9", offsetof(CPUState, sr[9]) },
2111 { "sr10", offsetof(CPUState, sr[10]) },
2112 { "sr11", offsetof(CPUState, sr[11]) },
2113 { "sr12", offsetof(CPUState, sr[12]) },
2114 { "sr13", offsetof(CPUState, sr[13]) },
2115 { "sr14", offsetof(CPUState, sr[14]) },
2116 { "sr15", offsetof(CPUState, sr[15]) },
2117 /* Too lazy to put BATs and SPRs ... */
2118 #elif defined(TARGET_SPARC)
2119 { "g0", offsetof(CPUState, gregs[0]) },
2120 { "g1", offsetof(CPUState, gregs[1]) },
2121 { "g2", offsetof(CPUState, gregs[2]) },
2122 { "g3", offsetof(CPUState, gregs[3]) },
2123 { "g4", offsetof(CPUState, gregs[4]) },
2124 { "g5", offsetof(CPUState, gregs[5]) },
2125 { "g6", offsetof(CPUState, gregs[6]) },
2126 { "g7", offsetof(CPUState, gregs[7]) },
2127 { "o0", 0, monitor_get_reg },
2128 { "o1", 1, monitor_get_reg },
2129 { "o2", 2, monitor_get_reg },
2130 { "o3", 3, monitor_get_reg },
2131 { "o4", 4, monitor_get_reg },
2132 { "o5", 5, monitor_get_reg },
2133 { "o6", 6, monitor_get_reg },
2134 { "o7", 7, monitor_get_reg },
2135 { "l0", 8, monitor_get_reg },
2136 { "l1", 9, monitor_get_reg },
2137 { "l2", 10, monitor_get_reg },
2138 { "l3", 11, monitor_get_reg },
2139 { "l4", 12, monitor_get_reg },
2140 { "l5", 13, monitor_get_reg },
2141 { "l6", 14, monitor_get_reg },
2142 { "l7", 15, monitor_get_reg },
2143 { "i0", 16, monitor_get_reg },
2144 { "i1", 17, monitor_get_reg },
2145 { "i2", 18, monitor_get_reg },
2146 { "i3", 19, monitor_get_reg },
2147 { "i4", 20, monitor_get_reg },
2148 { "i5", 21, monitor_get_reg },
2149 { "i6", 22, monitor_get_reg },
2150 { "i7", 23, monitor_get_reg },
2151 { "pc", offsetof(CPUState, pc) },
2152 { "npc", offsetof(CPUState, npc) },
2153 { "y", offsetof(CPUState, y) },
2154 #ifndef TARGET_SPARC64
2155 { "psr", 0, &monitor_get_psr, },
2156 { "wim", offsetof(CPUState, wim) },
2157 #endif
2158 { "tbr", offsetof(CPUState, tbr) },
2159 { "fsr", offsetof(CPUState, fsr) },
2160 { "f0", offsetof(CPUState, fpr[0]) },
2161 { "f1", offsetof(CPUState, fpr[1]) },
2162 { "f2", offsetof(CPUState, fpr[2]) },
2163 { "f3", offsetof(CPUState, fpr[3]) },
2164 { "f4", offsetof(CPUState, fpr[4]) },
2165 { "f5", offsetof(CPUState, fpr[5]) },
2166 { "f6", offsetof(CPUState, fpr[6]) },
2167 { "f7", offsetof(CPUState, fpr[7]) },
2168 { "f8", offsetof(CPUState, fpr[8]) },
2169 { "f9", offsetof(CPUState, fpr[9]) },
2170 { "f10", offsetof(CPUState, fpr[10]) },
2171 { "f11", offsetof(CPUState, fpr[11]) },
2172 { "f12", offsetof(CPUState, fpr[12]) },
2173 { "f13", offsetof(CPUState, fpr[13]) },
2174 { "f14", offsetof(CPUState, fpr[14]) },
2175 { "f15", offsetof(CPUState, fpr[15]) },
2176 { "f16", offsetof(CPUState, fpr[16]) },
2177 { "f17", offsetof(CPUState, fpr[17]) },
2178 { "f18", offsetof(CPUState, fpr[18]) },
2179 { "f19", offsetof(CPUState, fpr[19]) },
2180 { "f20", offsetof(CPUState, fpr[20]) },
2181 { "f21", offsetof(CPUState, fpr[21]) },
2182 { "f22", offsetof(CPUState, fpr[22]) },
2183 { "f23", offsetof(CPUState, fpr[23]) },
2184 { "f24", offsetof(CPUState, fpr[24]) },
2185 { "f25", offsetof(CPUState, fpr[25]) },
2186 { "f26", offsetof(CPUState, fpr[26]) },
2187 { "f27", offsetof(CPUState, fpr[27]) },
2188 { "f28", offsetof(CPUState, fpr[28]) },
2189 { "f29", offsetof(CPUState, fpr[29]) },
2190 { "f30", offsetof(CPUState, fpr[30]) },
2191 { "f31", offsetof(CPUState, fpr[31]) },
2192 #ifdef TARGET_SPARC64
2193 { "f32", offsetof(CPUState, fpr[32]) },
2194 { "f34", offsetof(CPUState, fpr[34]) },
2195 { "f36", offsetof(CPUState, fpr[36]) },
2196 { "f38", offsetof(CPUState, fpr[38]) },
2197 { "f40", offsetof(CPUState, fpr[40]) },
2198 { "f42", offsetof(CPUState, fpr[42]) },
2199 { "f44", offsetof(CPUState, fpr[44]) },
2200 { "f46", offsetof(CPUState, fpr[46]) },
2201 { "f48", offsetof(CPUState, fpr[48]) },
2202 { "f50", offsetof(CPUState, fpr[50]) },
2203 { "f52", offsetof(CPUState, fpr[52]) },
2204 { "f54", offsetof(CPUState, fpr[54]) },
2205 { "f56", offsetof(CPUState, fpr[56]) },
2206 { "f58", offsetof(CPUState, fpr[58]) },
2207 { "f60", offsetof(CPUState, fpr[60]) },
2208 { "f62", offsetof(CPUState, fpr[62]) },
2209 { "asi", offsetof(CPUState, asi) },
2210 { "pstate", offsetof(CPUState, pstate) },
2211 { "cansave", offsetof(CPUState, cansave) },
2212 { "canrestore", offsetof(CPUState, canrestore) },
2213 { "otherwin", offsetof(CPUState, otherwin) },
2214 { "wstate", offsetof(CPUState, wstate) },
2215 { "cleanwin", offsetof(CPUState, cleanwin) },
2216 { "fprs", offsetof(CPUState, fprs) },
2217 #endif
2218 #endif
2219 { NULL },
2222 static void expr_error(Monitor *mon, const char *msg)
2224 monitor_printf(mon, "%s\n", msg);
2225 longjmp(expr_env, 1);
2228 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2229 static int get_monitor_def(target_long *pval, const char *name)
2231 const MonitorDef *md;
2232 void *ptr;
2234 for(md = monitor_defs; md->name != NULL; md++) {
2235 if (compare_cmd(name, md->name)) {
2236 if (md->get_value) {
2237 *pval = md->get_value(md, md->offset);
2238 } else {
2239 CPUState *env = mon_get_cpu();
2240 if (!env)
2241 return -2;
2242 ptr = (uint8_t *)env + md->offset;
2243 switch(md->type) {
2244 case MD_I32:
2245 *pval = *(int32_t *)ptr;
2246 break;
2247 case MD_TLONG:
2248 *pval = *(target_long *)ptr;
2249 break;
2250 default:
2251 *pval = 0;
2252 break;
2255 return 0;
2258 return -1;
2261 static void next(void)
2263 if (pch != '\0') {
2264 pch++;
2265 while (qemu_isspace(*pch))
2266 pch++;
2270 static int64_t expr_sum(Monitor *mon);
2272 static int64_t expr_unary(Monitor *mon)
2274 int64_t n;
2275 char *p;
2276 int ret;
2278 switch(*pch) {
2279 case '+':
2280 next();
2281 n = expr_unary(mon);
2282 break;
2283 case '-':
2284 next();
2285 n = -expr_unary(mon);
2286 break;
2287 case '~':
2288 next();
2289 n = ~expr_unary(mon);
2290 break;
2291 case '(':
2292 next();
2293 n = expr_sum(mon);
2294 if (*pch != ')') {
2295 expr_error(mon, "')' expected");
2297 next();
2298 break;
2299 case '\'':
2300 pch++;
2301 if (*pch == '\0')
2302 expr_error(mon, "character constant expected");
2303 n = *pch;
2304 pch++;
2305 if (*pch != '\'')
2306 expr_error(mon, "missing terminating \' character");
2307 next();
2308 break;
2309 case '$':
2311 char buf[128], *q;
2312 target_long reg=0;
2314 pch++;
2315 q = buf;
2316 while ((*pch >= 'a' && *pch <= 'z') ||
2317 (*pch >= 'A' && *pch <= 'Z') ||
2318 (*pch >= '0' && *pch <= '9') ||
2319 *pch == '_' || *pch == '.') {
2320 if ((q - buf) < sizeof(buf) - 1)
2321 *q++ = *pch;
2322 pch++;
2324 while (qemu_isspace(*pch))
2325 pch++;
2326 *q = 0;
2327 ret = get_monitor_def(&reg, buf);
2328 if (ret == -1)
2329 expr_error(mon, "unknown register");
2330 else if (ret == -2)
2331 expr_error(mon, "no cpu defined");
2332 n = reg;
2334 break;
2335 case '\0':
2336 expr_error(mon, "unexpected end of expression");
2337 n = 0;
2338 break;
2339 default:
2340 #if TARGET_PHYS_ADDR_BITS > 32
2341 n = strtoull(pch, &p, 0);
2342 #else
2343 n = strtoul(pch, &p, 0);
2344 #endif
2345 if (pch == p) {
2346 expr_error(mon, "invalid char in expression");
2348 pch = p;
2349 while (qemu_isspace(*pch))
2350 pch++;
2351 break;
2353 return n;
2357 static int64_t expr_prod(Monitor *mon)
2359 int64_t val, val2;
2360 int op;
2362 val = expr_unary(mon);
2363 for(;;) {
2364 op = *pch;
2365 if (op != '*' && op != '/' && op != '%')
2366 break;
2367 next();
2368 val2 = expr_unary(mon);
2369 switch(op) {
2370 default:
2371 case '*':
2372 val *= val2;
2373 break;
2374 case '/':
2375 case '%':
2376 if (val2 == 0)
2377 expr_error(mon, "division by zero");
2378 if (op == '/')
2379 val /= val2;
2380 else
2381 val %= val2;
2382 break;
2385 return val;
2388 static int64_t expr_logic(Monitor *mon)
2390 int64_t val, val2;
2391 int op;
2393 val = expr_prod(mon);
2394 for(;;) {
2395 op = *pch;
2396 if (op != '&' && op != '|' && op != '^')
2397 break;
2398 next();
2399 val2 = expr_prod(mon);
2400 switch(op) {
2401 default:
2402 case '&':
2403 val &= val2;
2404 break;
2405 case '|':
2406 val |= val2;
2407 break;
2408 case '^':
2409 val ^= val2;
2410 break;
2413 return val;
2416 static int64_t expr_sum(Monitor *mon)
2418 int64_t val, val2;
2419 int op;
2421 val = expr_logic(mon);
2422 for(;;) {
2423 op = *pch;
2424 if (op != '+' && op != '-')
2425 break;
2426 next();
2427 val2 = expr_logic(mon);
2428 if (op == '+')
2429 val += val2;
2430 else
2431 val -= val2;
2433 return val;
2436 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2438 pch = *pp;
2439 if (setjmp(expr_env)) {
2440 *pp = pch;
2441 return -1;
2443 while (qemu_isspace(*pch))
2444 pch++;
2445 *pval = expr_sum(mon);
2446 *pp = pch;
2447 return 0;
2450 static int get_str(char *buf, int buf_size, const char **pp)
2452 const char *p;
2453 char *q;
2454 int c;
2456 q = buf;
2457 p = *pp;
2458 while (qemu_isspace(*p))
2459 p++;
2460 if (*p == '\0') {
2461 fail:
2462 *q = '\0';
2463 *pp = p;
2464 return -1;
2466 if (*p == '\"') {
2467 p++;
2468 while (*p != '\0' && *p != '\"') {
2469 if (*p == '\\') {
2470 p++;
2471 c = *p++;
2472 switch(c) {
2473 case 'n':
2474 c = '\n';
2475 break;
2476 case 'r':
2477 c = '\r';
2478 break;
2479 case '\\':
2480 case '\'':
2481 case '\"':
2482 break;
2483 default:
2484 qemu_printf("unsupported escape code: '\\%c'\n", c);
2485 goto fail;
2487 if ((q - buf) < buf_size - 1) {
2488 *q++ = c;
2490 } else {
2491 if ((q - buf) < buf_size - 1) {
2492 *q++ = *p;
2494 p++;
2497 if (*p != '\"') {
2498 qemu_printf("unterminated string\n");
2499 goto fail;
2501 p++;
2502 } else {
2503 while (*p != '\0' && !qemu_isspace(*p)) {
2504 if ((q - buf) < buf_size - 1) {
2505 *q++ = *p;
2507 p++;
2510 *q = '\0';
2511 *pp = p;
2512 return 0;
2515 static int default_fmt_format = 'x';
2516 static int default_fmt_size = 4;
2518 #define MAX_ARGS 16
2520 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2522 const char *p, *pstart, *typestr;
2523 char *q;
2524 int c, nb_args, len, i, has_arg;
2525 const mon_cmd_t *cmd;
2526 char cmdname[256];
2527 char buf[1024];
2528 void *str_allocated[MAX_ARGS];
2529 void *args[MAX_ARGS];
2530 void (*handler_0)(Monitor *mon);
2531 void (*handler_1)(Monitor *mon, void *arg0);
2532 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2533 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2534 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2535 void *arg3);
2536 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2537 void *arg3, void *arg4);
2538 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2539 void *arg3, void *arg4, void *arg5);
2540 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2541 void *arg3, void *arg4, void *arg5, void *arg6);
2543 #ifdef DEBUG
2544 monitor_printf(mon, "command='%s'\n", cmdline);
2545 #endif
2547 /* extract the command name */
2548 p = cmdline;
2549 q = cmdname;
2550 while (qemu_isspace(*p))
2551 p++;
2552 if (*p == '\0')
2553 return;
2554 pstart = p;
2555 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2556 p++;
2557 len = p - pstart;
2558 if (len > sizeof(cmdname) - 1)
2559 len = sizeof(cmdname) - 1;
2560 memcpy(cmdname, pstart, len);
2561 cmdname[len] = '\0';
2563 /* find the command */
2564 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2565 if (compare_cmd(cmdname, cmd->name))
2566 goto found;
2568 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2569 return;
2570 found:
2572 for(i = 0; i < MAX_ARGS; i++)
2573 str_allocated[i] = NULL;
2575 /* parse the parameters */
2576 typestr = cmd->args_type;
2577 nb_args = 0;
2578 for(;;) {
2579 c = *typestr;
2580 if (c == '\0')
2581 break;
2582 typestr++;
2583 switch(c) {
2584 case 'F':
2585 case 'B':
2586 case 's':
2588 int ret;
2589 char *str;
2591 while (qemu_isspace(*p))
2592 p++;
2593 if (*typestr == '?') {
2594 typestr++;
2595 if (*p == '\0') {
2596 /* no optional string: NULL argument */
2597 str = NULL;
2598 goto add_str;
2601 ret = get_str(buf, sizeof(buf), &p);
2602 if (ret < 0) {
2603 switch(c) {
2604 case 'F':
2605 monitor_printf(mon, "%s: filename expected\n",
2606 cmdname);
2607 break;
2608 case 'B':
2609 monitor_printf(mon, "%s: block device name expected\n",
2610 cmdname);
2611 break;
2612 default:
2613 monitor_printf(mon, "%s: string expected\n", cmdname);
2614 break;
2616 goto fail;
2618 str = qemu_malloc(strlen(buf) + 1);
2619 pstrcpy(str, sizeof(buf), buf);
2620 str_allocated[nb_args] = str;
2621 add_str:
2622 if (nb_args >= MAX_ARGS) {
2623 error_args:
2624 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2625 goto fail;
2627 args[nb_args++] = str;
2629 break;
2630 case '/':
2632 int count, format, size;
2634 while (qemu_isspace(*p))
2635 p++;
2636 if (*p == '/') {
2637 /* format found */
2638 p++;
2639 count = 1;
2640 if (qemu_isdigit(*p)) {
2641 count = 0;
2642 while (qemu_isdigit(*p)) {
2643 count = count * 10 + (*p - '0');
2644 p++;
2647 size = -1;
2648 format = -1;
2649 for(;;) {
2650 switch(*p) {
2651 case 'o':
2652 case 'd':
2653 case 'u':
2654 case 'x':
2655 case 'i':
2656 case 'c':
2657 format = *p++;
2658 break;
2659 case 'b':
2660 size = 1;
2661 p++;
2662 break;
2663 case 'h':
2664 size = 2;
2665 p++;
2666 break;
2667 case 'w':
2668 size = 4;
2669 p++;
2670 break;
2671 case 'g':
2672 case 'L':
2673 size = 8;
2674 p++;
2675 break;
2676 default:
2677 goto next;
2680 next:
2681 if (*p != '\0' && !qemu_isspace(*p)) {
2682 monitor_printf(mon, "invalid char in format: '%c'\n",
2683 *p);
2684 goto fail;
2686 if (format < 0)
2687 format = default_fmt_format;
2688 if (format != 'i') {
2689 /* for 'i', not specifying a size gives -1 as size */
2690 if (size < 0)
2691 size = default_fmt_size;
2692 default_fmt_size = size;
2694 default_fmt_format = format;
2695 } else {
2696 count = 1;
2697 format = default_fmt_format;
2698 if (format != 'i') {
2699 size = default_fmt_size;
2700 } else {
2701 size = -1;
2704 if (nb_args + 3 > MAX_ARGS)
2705 goto error_args;
2706 args[nb_args++] = (void*)(long)count;
2707 args[nb_args++] = (void*)(long)format;
2708 args[nb_args++] = (void*)(long)size;
2710 break;
2711 case 'i':
2712 case 'l':
2714 int64_t val;
2716 while (qemu_isspace(*p))
2717 p++;
2718 if (*typestr == '?' || *typestr == '.') {
2719 if (*typestr == '?') {
2720 if (*p == '\0')
2721 has_arg = 0;
2722 else
2723 has_arg = 1;
2724 } else {
2725 if (*p == '.') {
2726 p++;
2727 while (qemu_isspace(*p))
2728 p++;
2729 has_arg = 1;
2730 } else {
2731 has_arg = 0;
2734 typestr++;
2735 if (nb_args >= MAX_ARGS)
2736 goto error_args;
2737 args[nb_args++] = (void *)(long)has_arg;
2738 if (!has_arg) {
2739 if (nb_args >= MAX_ARGS)
2740 goto error_args;
2741 val = -1;
2742 goto add_num;
2745 if (get_expr(mon, &val, &p))
2746 goto fail;
2747 add_num:
2748 if (c == 'i') {
2749 if (nb_args >= MAX_ARGS)
2750 goto error_args;
2751 args[nb_args++] = (void *)(long)val;
2752 } else {
2753 if ((nb_args + 1) >= MAX_ARGS)
2754 goto error_args;
2755 #if TARGET_PHYS_ADDR_BITS > 32
2756 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2757 #else
2758 args[nb_args++] = (void *)0;
2759 #endif
2760 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2763 break;
2764 case '-':
2766 int has_option;
2767 /* option */
2769 c = *typestr++;
2770 if (c == '\0')
2771 goto bad_type;
2772 while (qemu_isspace(*p))
2773 p++;
2774 has_option = 0;
2775 if (*p == '-') {
2776 p++;
2777 if (*p != c) {
2778 monitor_printf(mon, "%s: unsupported option -%c\n",
2779 cmdname, *p);
2780 goto fail;
2782 p++;
2783 has_option = 1;
2785 if (nb_args >= MAX_ARGS)
2786 goto error_args;
2787 args[nb_args++] = (void *)(long)has_option;
2789 break;
2790 default:
2791 bad_type:
2792 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2793 goto fail;
2796 /* check that all arguments were parsed */
2797 while (qemu_isspace(*p))
2798 p++;
2799 if (*p != '\0') {
2800 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2801 cmdname);
2802 goto fail;
2805 switch(nb_args) {
2806 case 0:
2807 handler_0 = cmd->handler;
2808 handler_0(mon);
2809 break;
2810 case 1:
2811 handler_1 = cmd->handler;
2812 handler_1(mon, args[0]);
2813 break;
2814 case 2:
2815 handler_2 = cmd->handler;
2816 handler_2(mon, args[0], args[1]);
2817 break;
2818 case 3:
2819 handler_3 = cmd->handler;
2820 handler_3(mon, args[0], args[1], args[2]);
2821 break;
2822 case 4:
2823 handler_4 = cmd->handler;
2824 handler_4(mon, args[0], args[1], args[2], args[3]);
2825 break;
2826 case 5:
2827 handler_5 = cmd->handler;
2828 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2829 break;
2830 case 6:
2831 handler_6 = cmd->handler;
2832 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2833 break;
2834 case 7:
2835 handler_7 = cmd->handler;
2836 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2837 args[6]);
2838 break;
2839 default:
2840 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2841 goto fail;
2843 fail:
2844 for(i = 0; i < MAX_ARGS; i++)
2845 qemu_free(str_allocated[i]);
2846 return;
2849 static void cmd_completion(const char *name, const char *list)
2851 const char *p, *pstart;
2852 char cmd[128];
2853 int len;
2855 p = list;
2856 for(;;) {
2857 pstart = p;
2858 p = strchr(p, '|');
2859 if (!p)
2860 p = pstart + strlen(pstart);
2861 len = p - pstart;
2862 if (len > sizeof(cmd) - 2)
2863 len = sizeof(cmd) - 2;
2864 memcpy(cmd, pstart, len);
2865 cmd[len] = '\0';
2866 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2867 readline_add_completion(cur_mon->rs, cmd);
2869 if (*p == '\0')
2870 break;
2871 p++;
2875 static void file_completion(const char *input)
2877 DIR *ffs;
2878 struct dirent *d;
2879 char path[1024];
2880 char file[1024], file_prefix[1024];
2881 int input_path_len;
2882 const char *p;
2884 p = strrchr(input, '/');
2885 if (!p) {
2886 input_path_len = 0;
2887 pstrcpy(file_prefix, sizeof(file_prefix), input);
2888 pstrcpy(path, sizeof(path), ".");
2889 } else {
2890 input_path_len = p - input + 1;
2891 memcpy(path, input, input_path_len);
2892 if (input_path_len > sizeof(path) - 1)
2893 input_path_len = sizeof(path) - 1;
2894 path[input_path_len] = '\0';
2895 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2897 #ifdef DEBUG_COMPLETION
2898 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2899 input, path, file_prefix);
2900 #endif
2901 ffs = opendir(path);
2902 if (!ffs)
2903 return;
2904 for(;;) {
2905 struct stat sb;
2906 d = readdir(ffs);
2907 if (!d)
2908 break;
2909 if (strstart(d->d_name, file_prefix, NULL)) {
2910 memcpy(file, input, input_path_len);
2911 if (input_path_len < sizeof(file))
2912 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2913 d->d_name);
2914 /* stat the file to find out if it's a directory.
2915 * In that case add a slash to speed up typing long paths
2917 stat(file, &sb);
2918 if(S_ISDIR(sb.st_mode))
2919 pstrcat(file, sizeof(file), "/");
2920 readline_add_completion(cur_mon->rs, file);
2923 closedir(ffs);
2926 static void block_completion_it(void *opaque, BlockDriverState *bs)
2928 const char *name = bdrv_get_device_name(bs);
2929 const char *input = opaque;
2931 if (input[0] == '\0' ||
2932 !strncmp(name, (char *)input, strlen(input))) {
2933 readline_add_completion(cur_mon->rs, name);
2937 /* NOTE: this parser is an approximate form of the real command parser */
2938 static void parse_cmdline(const char *cmdline,
2939 int *pnb_args, char **args)
2941 const char *p;
2942 int nb_args, ret;
2943 char buf[1024];
2945 p = cmdline;
2946 nb_args = 0;
2947 for(;;) {
2948 while (qemu_isspace(*p))
2949 p++;
2950 if (*p == '\0')
2951 break;
2952 if (nb_args >= MAX_ARGS)
2953 break;
2954 ret = get_str(buf, sizeof(buf), &p);
2955 args[nb_args] = qemu_strdup(buf);
2956 nb_args++;
2957 if (ret < 0)
2958 break;
2960 *pnb_args = nb_args;
2963 static void monitor_find_completion(const char *cmdline)
2965 const char *cmdname;
2966 char *args[MAX_ARGS];
2967 int nb_args, i, len;
2968 const char *ptype, *str;
2969 const mon_cmd_t *cmd;
2970 const KeyDef *key;
2972 parse_cmdline(cmdline, &nb_args, args);
2973 #ifdef DEBUG_COMPLETION
2974 for(i = 0; i < nb_args; i++) {
2975 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2977 #endif
2979 /* if the line ends with a space, it means we want to complete the
2980 next arg */
2981 len = strlen(cmdline);
2982 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2983 if (nb_args >= MAX_ARGS)
2984 return;
2985 args[nb_args++] = qemu_strdup("");
2987 if (nb_args <= 1) {
2988 /* command completion */
2989 if (nb_args == 0)
2990 cmdname = "";
2991 else
2992 cmdname = args[0];
2993 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2994 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2995 cmd_completion(cmdname, cmd->name);
2997 } else {
2998 /* find the command */
2999 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3000 if (compare_cmd(args[0], cmd->name))
3001 goto found;
3003 return;
3004 found:
3005 ptype = cmd->args_type;
3006 for(i = 0; i < nb_args - 2; i++) {
3007 if (*ptype != '\0') {
3008 ptype++;
3009 while (*ptype == '?')
3010 ptype++;
3013 str = args[nb_args - 1];
3014 switch(*ptype) {
3015 case 'F':
3016 /* file completion */
3017 readline_set_completion_index(cur_mon->rs, strlen(str));
3018 file_completion(str);
3019 break;
3020 case 'B':
3021 /* block device name completion */
3022 readline_set_completion_index(cur_mon->rs, strlen(str));
3023 bdrv_iterate(block_completion_it, (void *)str);
3024 break;
3025 case 's':
3026 /* XXX: more generic ? */
3027 if (!strcmp(cmd->name, "info")) {
3028 readline_set_completion_index(cur_mon->rs, strlen(str));
3029 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3030 cmd_completion(str, cmd->name);
3032 } else if (!strcmp(cmd->name, "sendkey")) {
3033 char *sep = strrchr(str, '-');
3034 if (sep)
3035 str = sep + 1;
3036 readline_set_completion_index(cur_mon->rs, strlen(str));
3037 for(key = key_defs; key->name != NULL; key++) {
3038 cmd_completion(str, key->name);
3041 break;
3042 default:
3043 break;
3046 for(i = 0; i < nb_args; i++)
3047 qemu_free(args[i]);
3050 static int monitor_can_read(void *opaque)
3052 Monitor *mon = opaque;
3054 return (mon->suspend_cnt == 0) ? 128 : 0;
3057 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3059 Monitor *old_mon = cur_mon;
3060 int i;
3062 cur_mon = opaque;
3064 if (cur_mon->rs) {
3065 for (i = 0; i < size; i++)
3066 readline_handle_byte(cur_mon->rs, buf[i]);
3067 } else {
3068 if (size == 0 || buf[size - 1] != 0)
3069 monitor_printf(cur_mon, "corrupted command\n");
3070 else
3071 monitor_handle_command(cur_mon, (char *)buf);
3074 cur_mon = old_mon;
3077 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3079 monitor_suspend(mon);
3080 monitor_handle_command(mon, cmdline);
3081 monitor_resume(mon);
3084 int monitor_suspend(Monitor *mon)
3086 if (!mon->rs)
3087 return -ENOTTY;
3088 mon->suspend_cnt++;
3089 return 0;
3092 void monitor_resume(Monitor *mon)
3094 if (!mon->rs)
3095 return;
3096 if (--mon->suspend_cnt == 0)
3097 readline_show_prompt(mon->rs);
3100 static void monitor_event(void *opaque, int event)
3102 Monitor *mon = opaque;
3104 switch (event) {
3105 case CHR_EVENT_MUX_IN:
3106 readline_restart(mon->rs);
3107 monitor_resume(mon);
3108 monitor_flush(mon);
3109 break;
3111 case CHR_EVENT_MUX_OUT:
3112 if (mon->suspend_cnt == 0)
3113 monitor_printf(mon, "\n");
3114 monitor_flush(mon);
3115 monitor_suspend(mon);
3116 break;
3118 case CHR_EVENT_RESET:
3119 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3120 "information\n", QEMU_VERSION);
3121 if (mon->chr->focus == 0)
3122 readline_show_prompt(mon->rs);
3123 break;
3129 * Local variables:
3130 * c-indent-level: 4
3131 * c-basic-offset: 4
3132 * tab-width: 8
3133 * End:
3136 void monitor_init(CharDriverState *chr, int flags)
3138 static int is_first_init = 1;
3139 Monitor *mon;
3141 if (is_first_init) {
3142 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3143 is_first_init = 0;
3146 mon = qemu_mallocz(sizeof(*mon));
3148 mon->chr = chr;
3149 mon->flags = flags;
3150 if (mon->chr->focus != 0)
3151 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3152 if (flags & MONITOR_USE_READLINE) {
3153 mon->rs = readline_init(mon, monitor_find_completion);
3154 monitor_read_command(mon, 0);
3157 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3158 mon);
3160 LIST_INSERT_HEAD(&mon_list, mon, entry);
3161 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3162 cur_mon = mon;
3165 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3167 BlockDriverState *bs = opaque;
3168 int ret = 0;
3170 if (bdrv_set_key(bs, password) != 0) {
3171 monitor_printf(mon, "invalid password\n");
3172 ret = -EPERM;
3174 if (mon->password_completion_cb)
3175 mon->password_completion_cb(mon->password_opaque, ret);
3177 monitor_read_command(mon, 1);
3180 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3181 BlockDriverCompletionFunc *completion_cb,
3182 void *opaque)
3184 int err;
3186 if (!bdrv_key_required(bs)) {
3187 if (completion_cb)
3188 completion_cb(opaque, 0);
3189 return;
3192 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3193 bdrv_get_encrypted_filename(bs));
3195 mon->password_completion_cb = completion_cb;
3196 mon->password_opaque = opaque;
3198 err = monitor_read_password(mon, bdrv_password_cb, bs);
3200 if (err && completion_cb)
3201 completion_cb(opaque, err);