Work around supported cpuid ioctl() brokenness
[qemu-kvm/fedora.git] / monitor.c
blob2620203029a784dc1c8d6970a89b059d30c21c90
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 "hw/watchdog.h"
31 #include "gdbstub.h"
32 #include "net.h"
33 #include "qemu-char.h"
34 #include "sysemu.h"
35 #include "monitor.h"
36 #include "readline.h"
37 #include "console.h"
38 #include "block.h"
39 #include "audio/audio.h"
40 #include "disas.h"
41 #include "balloon.h"
42 #include "qemu-timer.h"
43 #include "migration.h"
44 #include "kvm.h"
45 #include "acl.h"
46 #include "exec-all.h"
48 #include "qemu-kvm.h"
50 //#define DEBUG
51 //#define DEBUG_COMPLETION
54 * Supported types:
56 * 'F' filename
57 * 'B' block device name
58 * 's' string (accept optional quote)
59 * 'i' 32 bit integer
60 * 'l' target long (32 or 64 bit)
61 * '/' optional gdb-like print format (like "/10x")
63 * '?' optional type (for 'F', 's' and 'i')
67 typedef struct mon_cmd_t {
68 const char *name;
69 const char *args_type;
70 void *handler;
71 const char *params;
72 const char *help;
73 } mon_cmd_t;
75 struct Monitor {
76 CharDriverState *chr;
77 int flags;
78 int suspend_cnt;
79 uint8_t outbuf[1024];
80 int outbuf_index;
81 ReadLineState *rs;
82 CPUState *mon_cpu;
83 BlockDriverCompletionFunc *password_completion_cb;
84 void *password_opaque;
85 LIST_ENTRY(Monitor) entry;
88 static LIST_HEAD(mon_list, Monitor) mon_list;
90 static const mon_cmd_t mon_cmds[];
91 static const mon_cmd_t info_cmds[];
93 Monitor *cur_mon = NULL;
95 static void monitor_command_cb(Monitor *mon, const char *cmdline,
96 void *opaque);
98 static void monitor_read_command(Monitor *mon, int show_prompt)
100 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
101 if (show_prompt)
102 readline_show_prompt(mon->rs);
105 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
106 void *opaque)
108 if (mon->rs) {
109 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
110 /* prompt is printed on return from the command handler */
111 return 0;
112 } else {
113 monitor_printf(mon, "terminal does not support password prompting\n");
114 return -ENOTTY;
118 void monitor_flush(Monitor *mon)
120 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
121 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
122 mon->outbuf_index = 0;
126 /* flush at every end of line or if the buffer is full */
127 static void monitor_puts(Monitor *mon, const char *str)
129 char c;
131 if (!mon)
132 return;
134 for(;;) {
135 c = *str++;
136 if (c == '\0')
137 break;
138 if (c == '\n')
139 mon->outbuf[mon->outbuf_index++] = '\r';
140 mon->outbuf[mon->outbuf_index++] = c;
141 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
142 || c == '\n')
143 monitor_flush(mon);
147 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
149 char buf[4096];
150 vsnprintf(buf, sizeof(buf), fmt, ap);
151 monitor_puts(mon, buf);
154 void monitor_printf(Monitor *mon, const char *fmt, ...)
156 va_list ap;
157 va_start(ap, fmt);
158 monitor_vprintf(mon, fmt, ap);
159 va_end(ap);
162 void monitor_print_filename(Monitor *mon, const char *filename)
164 int i;
166 for (i = 0; filename[i]; i++) {
167 switch (filename[i]) {
168 case ' ':
169 case '"':
170 case '\\':
171 monitor_printf(mon, "\\%c", filename[i]);
172 break;
173 case '\t':
174 monitor_printf(mon, "\\t");
175 break;
176 case '\r':
177 monitor_printf(mon, "\\r");
178 break;
179 case '\n':
180 monitor_printf(mon, "\\n");
181 break;
182 default:
183 monitor_printf(mon, "%c", filename[i]);
184 break;
189 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
191 va_list ap;
192 va_start(ap, fmt);
193 monitor_vprintf((Monitor *)stream, fmt, ap);
194 va_end(ap);
195 return 0;
198 static int compare_cmd(const char *name, const char *list)
200 const char *p, *pstart;
201 int len;
202 len = strlen(name);
203 p = list;
204 for(;;) {
205 pstart = p;
206 p = strchr(p, '|');
207 if (!p)
208 p = pstart + strlen(pstart);
209 if ((p - pstart) == len && !memcmp(pstart, name, len))
210 return 1;
211 if (*p == '\0')
212 break;
213 p++;
215 return 0;
218 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
219 const char *prefix, const char *name)
221 const mon_cmd_t *cmd;
223 for(cmd = cmds; cmd->name != NULL; cmd++) {
224 if (!name || !strcmp(name, cmd->name))
225 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
226 cmd->params, cmd->help);
230 static void help_cmd(Monitor *mon, const char *name)
232 if (name && !strcmp(name, "info")) {
233 help_cmd_dump(mon, info_cmds, "info ", NULL);
234 } else {
235 help_cmd_dump(mon, mon_cmds, "", name);
236 if (name && !strcmp(name, "log")) {
237 const CPULogItem *item;
238 monitor_printf(mon, "Log items (comma separated):\n");
239 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
240 for(item = cpu_log_items; item->mask != 0; item++) {
241 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
247 static void do_commit(Monitor *mon, const char *device)
249 int i, all_devices;
251 all_devices = !strcmp(device, "all");
252 for (i = 0; i < nb_drives; i++) {
253 if (all_devices ||
254 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
255 bdrv_commit(drives_table[i].bdrv);
259 static void do_info(Monitor *mon, const char *item)
261 const mon_cmd_t *cmd;
262 void (*handler)(Monitor *);
264 if (!item)
265 goto help;
266 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
267 if (compare_cmd(item, cmd->name))
268 goto found;
270 help:
271 help_cmd(mon, "info");
272 return;
273 found:
274 handler = cmd->handler;
275 handler(mon);
278 static void do_info_version(Monitor *mon)
280 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
283 static void do_info_name(Monitor *mon)
285 if (qemu_name)
286 monitor_printf(mon, "%s\n", qemu_name);
289 #if defined(TARGET_I386)
290 static void do_info_hpet(Monitor *mon)
292 monitor_printf(mon, "HPET is %s by QEMU\n",
293 (no_hpet) ? "disabled" : "enabled");
295 #endif
297 static void do_info_uuid(Monitor *mon)
299 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
300 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
301 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
302 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
303 qemu_uuid[14], qemu_uuid[15]);
306 /* get the current CPU defined by the user */
307 static int mon_set_cpu(int cpu_index)
309 CPUState *env;
311 for(env = first_cpu; env != NULL; env = env->next_cpu) {
312 if (env->cpu_index == cpu_index) {
313 cur_mon->mon_cpu = env;
314 return 0;
317 return -1;
320 static CPUState *mon_get_cpu(void)
322 if (!cur_mon->mon_cpu) {
323 mon_set_cpu(0);
325 cpu_synchronize_state(cur_mon->mon_cpu, 0);
326 return cur_mon->mon_cpu;
329 static void do_info_registers(Monitor *mon)
331 CPUState *env;
332 env = mon_get_cpu();
333 if (!env)
334 return;
335 #ifdef TARGET_I386
336 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
337 X86_DUMP_FPU);
338 #else
339 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
341 #endif
344 static void do_info_cpus(Monitor *mon)
346 CPUState *env;
348 /* just to set the default cpu if not already done */
349 mon_get_cpu();
351 for(env = first_cpu; env != NULL; env = env->next_cpu) {
352 cpu_synchronize_state(env, 0);
353 monitor_printf(mon, "%c CPU #%d:",
354 (env == mon->mon_cpu) ? '*' : ' ',
355 env->cpu_index);
356 #if defined(TARGET_I386)
357 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
358 env->eip + env->segs[R_CS].base);
359 #elif defined(TARGET_PPC)
360 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
361 #elif defined(TARGET_SPARC)
362 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
363 env->pc, env->npc);
364 #elif defined(TARGET_MIPS)
365 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
366 #endif
367 if (env->halted)
368 monitor_printf(mon, " (halted)");
369 monitor_printf(mon," thread_id=%d", env->thread_id);
370 monitor_printf(mon, "\n");
374 static void do_cpu_set(Monitor *mon, int index)
376 if (mon_set_cpu(index) < 0)
377 monitor_printf(mon, "Invalid CPU index\n");
380 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
382 int state;
384 if (!strcmp(status, "online"))
385 state = 1;
386 else if (!strcmp(status, "offline"))
387 state = 0;
388 else {
389 monitor_printf(mon, "invalid status: %s\n", status);
390 return;
392 #if defined(TARGET_I386) || defined(TARGET_X86_64)
393 qemu_system_cpu_hot_add(value, state);
394 #endif
397 static void do_info_jit(Monitor *mon)
399 dump_exec_info((FILE *)mon, monitor_fprintf);
402 static void do_info_history(Monitor *mon)
404 int i;
405 const char *str;
407 if (!mon->rs)
408 return;
409 i = 0;
410 for(;;) {
411 str = readline_get_history(mon->rs, i);
412 if (!str)
413 break;
414 monitor_printf(mon, "%d: '%s'\n", i, str);
415 i++;
419 #if defined(TARGET_PPC)
420 /* XXX: not implemented in other targets */
421 static void do_info_cpu_stats(Monitor *mon)
423 CPUState *env;
425 env = mon_get_cpu();
426 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
428 #endif
430 static void do_quit(Monitor *mon)
432 exit(0);
435 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
437 if (bdrv_is_inserted(bs)) {
438 if (!force) {
439 if (!bdrv_is_removable(bs)) {
440 monitor_printf(mon, "device is not removable\n");
441 return -1;
443 if (bdrv_is_locked(bs)) {
444 monitor_printf(mon, "device is locked\n");
445 return -1;
448 bdrv_close(bs);
450 return 0;
453 static void do_eject(Monitor *mon, int force, const char *filename)
455 BlockDriverState *bs;
457 bs = bdrv_find(filename);
458 if (!bs) {
459 monitor_printf(mon, "device not found\n");
460 return;
462 eject_device(mon, bs, force);
465 static void do_change_block(Monitor *mon, const char *device,
466 const char *filename, const char *fmt)
468 BlockDriverState *bs;
469 BlockDriver *drv = NULL;
471 bs = bdrv_find(device);
472 if (!bs) {
473 monitor_printf(mon, "device not found\n");
474 return;
476 if (fmt) {
477 drv = bdrv_find_format(fmt);
478 if (!drv) {
479 monitor_printf(mon, "invalid format %s\n", fmt);
480 return;
483 if (eject_device(mon, bs, 0) < 0)
484 return;
485 bdrv_open2(bs, filename, 0, drv);
486 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
489 static void change_vnc_password_cb(Monitor *mon, const char *password,
490 void *opaque)
492 if (vnc_display_password(NULL, password) < 0)
493 monitor_printf(mon, "could not set VNC server password\n");
495 monitor_read_command(mon, 1);
498 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
500 if (strcmp(target, "passwd") == 0 ||
501 strcmp(target, "password") == 0) {
502 if (arg) {
503 char password[9];
504 strncpy(password, arg, sizeof(password));
505 password[sizeof(password) - 1] = '\0';
506 change_vnc_password_cb(mon, password, NULL);
507 } else {
508 monitor_read_password(mon, change_vnc_password_cb, NULL);
510 } else {
511 if (vnc_display_open(NULL, target) < 0)
512 monitor_printf(mon, "could not start VNC server on %s\n", target);
516 static void do_change(Monitor *mon, const char *device, const char *target,
517 const char *arg)
519 if (strcmp(device, "vnc") == 0) {
520 do_change_vnc(mon, target, arg);
521 } else {
522 do_change_block(mon, device, target, arg);
526 static void do_screen_dump(Monitor *mon, const char *filename)
528 vga_hw_screen_dump(filename);
531 static void do_logfile(Monitor *mon, const char *filename)
533 cpu_set_log_filename(filename);
536 static void do_log(Monitor *mon, const char *items)
538 int mask;
540 if (!strcmp(items, "none")) {
541 mask = 0;
542 } else {
543 mask = cpu_str_to_log_mask(items);
544 if (!mask) {
545 help_cmd(mon, "log");
546 return;
549 cpu_set_log(mask);
552 static void do_singlestep(Monitor *mon, const char *option)
554 if (!option || !strcmp(option, "on")) {
555 singlestep = 1;
556 } else if (!strcmp(option, "off")) {
557 singlestep = 0;
558 } else {
559 monitor_printf(mon, "unexpected option %s\n", option);
563 static void do_stop(Monitor *mon)
565 vm_stop(EXCP_INTERRUPT);
568 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
570 struct bdrv_iterate_context {
571 Monitor *mon;
572 int err;
575 static void do_cont(Monitor *mon)
577 struct bdrv_iterate_context context = { mon, 0 };
579 bdrv_iterate(encrypted_bdrv_it, &context);
580 /* only resume the vm if all keys are set and valid */
581 if (!context.err)
582 vm_start();
585 static void bdrv_key_cb(void *opaque, int err)
587 Monitor *mon = opaque;
589 /* another key was set successfully, retry to continue */
590 if (!err)
591 do_cont(mon);
594 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
596 struct bdrv_iterate_context *context = opaque;
598 if (!context->err && bdrv_key_required(bs)) {
599 context->err = -EBUSY;
600 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
601 context->mon);
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);
620 static void do_watchdog_action(Monitor *mon, const char *action)
622 if (select_watchdog_action(action) == -1) {
623 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
627 static void monitor_printc(Monitor *mon, int c)
629 monitor_printf(mon, "'");
630 switch(c) {
631 case '\'':
632 monitor_printf(mon, "\\'");
633 break;
634 case '\\':
635 monitor_printf(mon, "\\\\");
636 break;
637 case '\n':
638 monitor_printf(mon, "\\n");
639 break;
640 case '\r':
641 monitor_printf(mon, "\\r");
642 break;
643 default:
644 if (c >= 32 && c <= 126) {
645 monitor_printf(mon, "%c", c);
646 } else {
647 monitor_printf(mon, "\\x%02x", c);
649 break;
651 monitor_printf(mon, "'");
654 static void memory_dump(Monitor *mon, int count, int format, int wsize,
655 target_phys_addr_t addr, int is_physical)
657 CPUState *env;
658 int nb_per_line, l, line_size, i, max_digits, len;
659 uint8_t buf[16];
660 uint64_t v;
662 if (format == 'i') {
663 int flags;
664 flags = 0;
665 env = mon_get_cpu();
666 if (!env && !is_physical)
667 return;
668 #ifdef TARGET_I386
669 if (wsize == 2) {
670 flags = 1;
671 } else if (wsize == 4) {
672 flags = 0;
673 } else {
674 /* as default we use the current CS size */
675 flags = 0;
676 if (env) {
677 #ifdef TARGET_X86_64
678 if ((env->efer & MSR_EFER_LMA) &&
679 (env->segs[R_CS].flags & DESC_L_MASK))
680 flags = 2;
681 else
682 #endif
683 if (!(env->segs[R_CS].flags & DESC_B_MASK))
684 flags = 1;
687 #endif
688 monitor_disas(mon, env, addr, count, is_physical, flags);
689 return;
692 len = wsize * count;
693 if (wsize == 1)
694 line_size = 8;
695 else
696 line_size = 16;
697 nb_per_line = line_size / wsize;
698 max_digits = 0;
700 switch(format) {
701 case 'o':
702 max_digits = (wsize * 8 + 2) / 3;
703 break;
704 default:
705 case 'x':
706 max_digits = (wsize * 8) / 4;
707 break;
708 case 'u':
709 case 'd':
710 max_digits = (wsize * 8 * 10 + 32) / 33;
711 break;
712 case 'c':
713 wsize = 1;
714 break;
717 while (len > 0) {
718 if (is_physical)
719 monitor_printf(mon, TARGET_FMT_plx ":", addr);
720 else
721 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
722 l = len;
723 if (l > line_size)
724 l = line_size;
725 if (is_physical) {
726 cpu_physical_memory_rw(addr, buf, l, 0);
727 } else {
728 env = mon_get_cpu();
729 if (!env)
730 break;
731 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
732 monitor_printf(mon, " Cannot access memory\n");
733 break;
736 i = 0;
737 while (i < l) {
738 switch(wsize) {
739 default:
740 case 1:
741 v = ldub_raw(buf + i);
742 break;
743 case 2:
744 v = lduw_raw(buf + i);
745 break;
746 case 4:
747 v = (uint32_t)ldl_raw(buf + i);
748 break;
749 case 8:
750 v = ldq_raw(buf + i);
751 break;
753 monitor_printf(mon, " ");
754 switch(format) {
755 case 'o':
756 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
757 break;
758 case 'x':
759 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
760 break;
761 case 'u':
762 monitor_printf(mon, "%*" PRIu64, max_digits, v);
763 break;
764 case 'd':
765 monitor_printf(mon, "%*" PRId64, max_digits, v);
766 break;
767 case 'c':
768 monitor_printc(mon, v);
769 break;
771 i += wsize;
773 monitor_printf(mon, "\n");
774 addr += l;
775 len -= l;
779 #if TARGET_LONG_BITS == 64
780 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
781 #else
782 #define GET_TLONG(h, l) (l)
783 #endif
785 static void do_memory_dump(Monitor *mon, int count, int format, int size,
786 uint32_t addrh, uint32_t addrl)
788 target_long addr = GET_TLONG(addrh, addrl);
789 memory_dump(mon, count, format, size, addr, 0);
792 #if TARGET_PHYS_ADDR_BITS > 32
793 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
794 #else
795 #define GET_TPHYSADDR(h, l) (l)
796 #endif
798 static void do_physical_memory_dump(Monitor *mon, int count, int format,
799 int size, uint32_t addrh, uint32_t addrl)
802 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
803 memory_dump(mon, count, format, size, addr, 1);
806 static void do_print(Monitor *mon, int count, int format, int size,
807 unsigned int valh, unsigned int vall)
809 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
810 #if TARGET_PHYS_ADDR_BITS == 32
811 switch(format) {
812 case 'o':
813 monitor_printf(mon, "%#o", val);
814 break;
815 case 'x':
816 monitor_printf(mon, "%#x", val);
817 break;
818 case 'u':
819 monitor_printf(mon, "%u", val);
820 break;
821 default:
822 case 'd':
823 monitor_printf(mon, "%d", val);
824 break;
825 case 'c':
826 monitor_printc(mon, val);
827 break;
829 #else
830 switch(format) {
831 case 'o':
832 monitor_printf(mon, "%#" PRIo64, val);
833 break;
834 case 'x':
835 monitor_printf(mon, "%#" PRIx64, val);
836 break;
837 case 'u':
838 monitor_printf(mon, "%" PRIu64, val);
839 break;
840 default:
841 case 'd':
842 monitor_printf(mon, "%" PRId64, val);
843 break;
844 case 'c':
845 monitor_printc(mon, val);
846 break;
848 #endif
849 monitor_printf(mon, "\n");
852 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
853 uint32_t size, const char *filename)
855 FILE *f;
856 target_long addr = GET_TLONG(valh, vall);
857 uint32_t l;
858 CPUState *env;
859 uint8_t buf[1024];
861 env = mon_get_cpu();
862 if (!env)
863 return;
865 f = fopen(filename, "wb");
866 if (!f) {
867 monitor_printf(mon, "could not open '%s'\n", filename);
868 return;
870 while (size != 0) {
871 l = sizeof(buf);
872 if (l > size)
873 l = size;
874 cpu_memory_rw_debug(env, addr, buf, l, 0);
875 fwrite(buf, 1, l, f);
876 addr += l;
877 size -= l;
879 fclose(f);
882 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
883 unsigned int vall, uint32_t size,
884 const char *filename)
886 FILE *f;
887 uint32_t l;
888 uint8_t buf[1024];
889 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
891 f = fopen(filename, "wb");
892 if (!f) {
893 monitor_printf(mon, "could not open '%s'\n", filename);
894 return;
896 while (size != 0) {
897 l = sizeof(buf);
898 if (l > size)
899 l = size;
900 cpu_physical_memory_rw(addr, buf, l, 0);
901 fwrite(buf, 1, l, f);
902 fflush(f);
903 addr += l;
904 size -= l;
906 fclose(f);
909 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
911 uint32_t addr;
912 uint8_t buf[1];
913 uint16_t sum;
915 sum = 0;
916 for(addr = start; addr < (start + size); addr++) {
917 cpu_physical_memory_rw(addr, buf, 1, 0);
918 /* BSD sum algorithm ('sum' Unix command) */
919 sum = (sum >> 1) | (sum << 15);
920 sum += buf[0];
922 monitor_printf(mon, "%05d\n", sum);
925 typedef struct {
926 int keycode;
927 const char *name;
928 } KeyDef;
930 static const KeyDef key_defs[] = {
931 { 0x2a, "shift" },
932 { 0x36, "shift_r" },
934 { 0x38, "alt" },
935 { 0xb8, "alt_r" },
936 { 0x64, "altgr" },
937 { 0xe4, "altgr_r" },
938 { 0x1d, "ctrl" },
939 { 0x9d, "ctrl_r" },
941 { 0xdd, "menu" },
943 { 0x01, "esc" },
945 { 0x02, "1" },
946 { 0x03, "2" },
947 { 0x04, "3" },
948 { 0x05, "4" },
949 { 0x06, "5" },
950 { 0x07, "6" },
951 { 0x08, "7" },
952 { 0x09, "8" },
953 { 0x0a, "9" },
954 { 0x0b, "0" },
955 { 0x0c, "minus" },
956 { 0x0d, "equal" },
957 { 0x0e, "backspace" },
959 { 0x0f, "tab" },
960 { 0x10, "q" },
961 { 0x11, "w" },
962 { 0x12, "e" },
963 { 0x13, "r" },
964 { 0x14, "t" },
965 { 0x15, "y" },
966 { 0x16, "u" },
967 { 0x17, "i" },
968 { 0x18, "o" },
969 { 0x19, "p" },
971 { 0x1c, "ret" },
973 { 0x1e, "a" },
974 { 0x1f, "s" },
975 { 0x20, "d" },
976 { 0x21, "f" },
977 { 0x22, "g" },
978 { 0x23, "h" },
979 { 0x24, "j" },
980 { 0x25, "k" },
981 { 0x26, "l" },
983 { 0x2c, "z" },
984 { 0x2d, "x" },
985 { 0x2e, "c" },
986 { 0x2f, "v" },
987 { 0x30, "b" },
988 { 0x31, "n" },
989 { 0x32, "m" },
990 { 0x33, "comma" },
991 { 0x34, "dot" },
992 { 0x35, "slash" },
994 { 0x37, "asterisk" },
996 { 0x39, "spc" },
997 { 0x3a, "caps_lock" },
998 { 0x3b, "f1" },
999 { 0x3c, "f2" },
1000 { 0x3d, "f3" },
1001 { 0x3e, "f4" },
1002 { 0x3f, "f5" },
1003 { 0x40, "f6" },
1004 { 0x41, "f7" },
1005 { 0x42, "f8" },
1006 { 0x43, "f9" },
1007 { 0x44, "f10" },
1008 { 0x45, "num_lock" },
1009 { 0x46, "scroll_lock" },
1011 { 0xb5, "kp_divide" },
1012 { 0x37, "kp_multiply" },
1013 { 0x4a, "kp_subtract" },
1014 { 0x4e, "kp_add" },
1015 { 0x9c, "kp_enter" },
1016 { 0x53, "kp_decimal" },
1017 { 0x54, "sysrq" },
1019 { 0x52, "kp_0" },
1020 { 0x4f, "kp_1" },
1021 { 0x50, "kp_2" },
1022 { 0x51, "kp_3" },
1023 { 0x4b, "kp_4" },
1024 { 0x4c, "kp_5" },
1025 { 0x4d, "kp_6" },
1026 { 0x47, "kp_7" },
1027 { 0x48, "kp_8" },
1028 { 0x49, "kp_9" },
1030 { 0x56, "<" },
1032 { 0x57, "f11" },
1033 { 0x58, "f12" },
1035 { 0xb7, "print" },
1037 { 0xc7, "home" },
1038 { 0xc9, "pgup" },
1039 { 0xd1, "pgdn" },
1040 { 0xcf, "end" },
1042 { 0xcb, "left" },
1043 { 0xc8, "up" },
1044 { 0xd0, "down" },
1045 { 0xcd, "right" },
1047 { 0xd2, "insert" },
1048 { 0xd3, "delete" },
1049 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1050 { 0xf0, "stop" },
1051 { 0xf1, "again" },
1052 { 0xf2, "props" },
1053 { 0xf3, "undo" },
1054 { 0xf4, "front" },
1055 { 0xf5, "copy" },
1056 { 0xf6, "open" },
1057 { 0xf7, "paste" },
1058 { 0xf8, "find" },
1059 { 0xf9, "cut" },
1060 { 0xfa, "lf" },
1061 { 0xfb, "help" },
1062 { 0xfc, "meta_l" },
1063 { 0xfd, "meta_r" },
1064 { 0xfe, "compose" },
1065 #endif
1066 { 0, NULL },
1069 static int get_keycode(const char *key)
1071 const KeyDef *p;
1072 char *endp;
1073 int ret;
1075 for(p = key_defs; p->name != NULL; p++) {
1076 if (!strcmp(key, p->name))
1077 return p->keycode;
1079 if (strstart(key, "0x", NULL)) {
1080 ret = strtoul(key, &endp, 0);
1081 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1082 return ret;
1084 return -1;
1087 #define MAX_KEYCODES 16
1088 static uint8_t keycodes[MAX_KEYCODES];
1089 static int nb_pending_keycodes;
1090 static QEMUTimer *key_timer;
1092 static void release_keys(void *opaque)
1094 int keycode;
1096 while (nb_pending_keycodes > 0) {
1097 nb_pending_keycodes--;
1098 keycode = keycodes[nb_pending_keycodes];
1099 if (keycode & 0x80)
1100 kbd_put_keycode(0xe0);
1101 kbd_put_keycode(keycode | 0x80);
1105 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1106 int hold_time)
1108 char keyname_buf[16];
1109 char *separator;
1110 int keyname_len, keycode, i;
1112 if (nb_pending_keycodes > 0) {
1113 qemu_del_timer(key_timer);
1114 release_keys(NULL);
1116 if (!has_hold_time)
1117 hold_time = 100;
1118 i = 0;
1119 while (1) {
1120 separator = strchr(string, '-');
1121 keyname_len = separator ? separator - string : strlen(string);
1122 if (keyname_len > 0) {
1123 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1124 if (keyname_len > sizeof(keyname_buf) - 1) {
1125 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1126 return;
1128 if (i == MAX_KEYCODES) {
1129 monitor_printf(mon, "too many keys\n");
1130 return;
1132 keyname_buf[keyname_len] = 0;
1133 keycode = get_keycode(keyname_buf);
1134 if (keycode < 0) {
1135 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1136 return;
1138 keycodes[i++] = keycode;
1140 if (!separator)
1141 break;
1142 string = separator + 1;
1144 nb_pending_keycodes = i;
1145 /* key down events */
1146 for (i = 0; i < nb_pending_keycodes; i++) {
1147 keycode = keycodes[i];
1148 if (keycode & 0x80)
1149 kbd_put_keycode(0xe0);
1150 kbd_put_keycode(keycode & 0x7f);
1152 /* delayed key up events */
1153 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1154 muldiv64(ticks_per_sec, hold_time, 1000));
1157 static int mouse_button_state;
1159 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1160 const char *dz_str)
1162 int dx, dy, dz;
1163 dx = strtol(dx_str, NULL, 0);
1164 dy = strtol(dy_str, NULL, 0);
1165 dz = 0;
1166 if (dz_str)
1167 dz = strtol(dz_str, NULL, 0);
1168 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1171 static void do_mouse_button(Monitor *mon, int button_state)
1173 mouse_button_state = button_state;
1174 kbd_mouse_event(0, 0, 0, mouse_button_state);
1177 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1178 int addr, int has_index, int index)
1180 uint32_t val;
1181 int suffix;
1183 if (has_index) {
1184 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1185 addr++;
1187 addr &= 0xffff;
1189 switch(size) {
1190 default:
1191 case 1:
1192 val = cpu_inb(NULL, addr);
1193 suffix = 'b';
1194 break;
1195 case 2:
1196 val = cpu_inw(NULL, addr);
1197 suffix = 'w';
1198 break;
1199 case 4:
1200 val = cpu_inl(NULL, addr);
1201 suffix = 'l';
1202 break;
1204 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1205 suffix, addr, size * 2, val);
1208 /* boot_set handler */
1209 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1210 static void *boot_opaque;
1212 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1214 qemu_boot_set_handler = func;
1215 boot_opaque = opaque;
1218 static void do_boot_set(Monitor *mon, const char *bootdevice)
1220 int res;
1222 if (qemu_boot_set_handler) {
1223 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1224 if (res == 0)
1225 monitor_printf(mon, "boot device list now set to %s\n",
1226 bootdevice);
1227 else
1228 monitor_printf(mon, "setting boot device list failed with "
1229 "error %i\n", res);
1230 } else {
1231 monitor_printf(mon, "no function defined to set boot device list for "
1232 "this architecture\n");
1236 static void do_system_reset(Monitor *mon)
1238 qemu_system_reset_request();
1241 static void do_system_powerdown(Monitor *mon)
1243 qemu_system_powerdown_request();
1246 #if defined(TARGET_I386)
1247 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1249 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1250 addr,
1251 pte & mask,
1252 pte & PG_GLOBAL_MASK ? 'G' : '-',
1253 pte & PG_PSE_MASK ? 'P' : '-',
1254 pte & PG_DIRTY_MASK ? 'D' : '-',
1255 pte & PG_ACCESSED_MASK ? 'A' : '-',
1256 pte & PG_PCD_MASK ? 'C' : '-',
1257 pte & PG_PWT_MASK ? 'T' : '-',
1258 pte & PG_USER_MASK ? 'U' : '-',
1259 pte & PG_RW_MASK ? 'W' : '-');
1262 static void tlb_info(Monitor *mon)
1264 CPUState *env;
1265 int l1, l2;
1266 uint32_t pgd, pde, pte;
1268 env = mon_get_cpu();
1269 if (!env)
1270 return;
1272 if (!(env->cr[0] & CR0_PG_MASK)) {
1273 monitor_printf(mon, "PG disabled\n");
1274 return;
1276 pgd = env->cr[3] & ~0xfff;
1277 for(l1 = 0; l1 < 1024; l1++) {
1278 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1279 pde = le32_to_cpu(pde);
1280 if (pde & PG_PRESENT_MASK) {
1281 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1282 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1283 } else {
1284 for(l2 = 0; l2 < 1024; l2++) {
1285 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1286 (uint8_t *)&pte, 4);
1287 pte = le32_to_cpu(pte);
1288 if (pte & PG_PRESENT_MASK) {
1289 print_pte(mon, (l1 << 22) + (l2 << 12),
1290 pte & ~PG_PSE_MASK,
1291 ~0xfff);
1299 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1300 uint32_t end, int prot)
1302 int prot1;
1303 prot1 = *plast_prot;
1304 if (prot != prot1) {
1305 if (*pstart != -1) {
1306 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1307 *pstart, end, end - *pstart,
1308 prot1 & PG_USER_MASK ? 'u' : '-',
1309 'r',
1310 prot1 & PG_RW_MASK ? 'w' : '-');
1312 if (prot != 0)
1313 *pstart = end;
1314 else
1315 *pstart = -1;
1316 *plast_prot = prot;
1320 static void mem_info(Monitor *mon)
1322 CPUState *env;
1323 int l1, l2, prot, last_prot;
1324 uint32_t pgd, pde, pte, start, end;
1326 env = mon_get_cpu();
1327 if (!env)
1328 return;
1330 if (!(env->cr[0] & CR0_PG_MASK)) {
1331 monitor_printf(mon, "PG disabled\n");
1332 return;
1334 pgd = env->cr[3] & ~0xfff;
1335 last_prot = 0;
1336 start = -1;
1337 for(l1 = 0; l1 < 1024; l1++) {
1338 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1339 pde = le32_to_cpu(pde);
1340 end = l1 << 22;
1341 if (pde & PG_PRESENT_MASK) {
1342 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1343 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1344 mem_print(mon, &start, &last_prot, end, prot);
1345 } else {
1346 for(l2 = 0; l2 < 1024; l2++) {
1347 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1348 (uint8_t *)&pte, 4);
1349 pte = le32_to_cpu(pte);
1350 end = (l1 << 22) + (l2 << 12);
1351 if (pte & PG_PRESENT_MASK) {
1352 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1353 } else {
1354 prot = 0;
1356 mem_print(mon, &start, &last_prot, end, prot);
1359 } else {
1360 prot = 0;
1361 mem_print(mon, &start, &last_prot, end, prot);
1365 #endif
1367 #if defined(TARGET_SH4)
1369 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1371 monitor_printf(mon, " tlb%i:\t"
1372 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1373 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1374 "dirty=%hhu writethrough=%hhu\n",
1375 idx,
1376 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1377 tlb->v, tlb->sh, tlb->c, tlb->pr,
1378 tlb->d, tlb->wt);
1381 static void tlb_info(Monitor *mon)
1383 CPUState *env = mon_get_cpu();
1384 int i;
1386 monitor_printf (mon, "ITLB:\n");
1387 for (i = 0 ; i < ITLB_SIZE ; i++)
1388 print_tlb (mon, i, &env->itlb[i]);
1389 monitor_printf (mon, "UTLB:\n");
1390 for (i = 0 ; i < UTLB_SIZE ; i++)
1391 print_tlb (mon, i, &env->utlb[i]);
1394 #endif
1396 static void do_info_kqemu(Monitor *mon)
1398 #ifdef CONFIG_KQEMU
1399 CPUState *env;
1400 int val;
1401 val = 0;
1402 env = mon_get_cpu();
1403 if (!env) {
1404 monitor_printf(mon, "No cpu initialized yet");
1405 return;
1407 val = env->kqemu_enabled;
1408 monitor_printf(mon, "kqemu support: ");
1409 switch(val) {
1410 default:
1411 case 0:
1412 monitor_printf(mon, "disabled\n");
1413 break;
1414 case 1:
1415 monitor_printf(mon, "enabled for user code\n");
1416 break;
1417 case 2:
1418 monitor_printf(mon, "enabled for user and kernel code\n");
1419 break;
1421 #else
1422 monitor_printf(mon, "kqemu support: not compiled\n");
1423 #endif
1426 static void do_info_kvm(Monitor *mon)
1428 #if defined(USE_KVM) || defined(CONFIG_KVM)
1429 monitor_printf(mon, "kvm support: ");
1430 if (kvm_enabled())
1431 monitor_printf(mon, "enabled\n");
1432 else
1433 monitor_printf(mon, "disabled\n");
1434 #else
1435 monitor_printf(mon, "kvm support: not compiled\n");
1436 #endif
1439 static void do_info_numa(Monitor *mon)
1441 int i;
1442 CPUState *env;
1444 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1445 for (i = 0; i < nb_numa_nodes; i++) {
1446 monitor_printf(mon, "node %d cpus:", i);
1447 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1448 if (env->numa_node == i) {
1449 monitor_printf(mon, " %d", env->cpu_index);
1452 monitor_printf(mon, "\n");
1453 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1454 node_mem[i] >> 20);
1458 #ifdef CONFIG_PROFILER
1460 int64_t kqemu_time;
1461 int64_t qemu_time;
1462 int64_t kqemu_exec_count;
1463 int64_t dev_time;
1464 int64_t kqemu_ret_int_count;
1465 int64_t kqemu_ret_excp_count;
1466 int64_t kqemu_ret_intr_count;
1468 static void do_info_profile(Monitor *mon)
1470 int64_t total;
1471 total = qemu_time;
1472 if (total == 0)
1473 total = 1;
1474 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1475 dev_time, dev_time / (double)ticks_per_sec);
1476 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1477 qemu_time, qemu_time / (double)ticks_per_sec);
1478 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1479 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1480 PRId64 "\n",
1481 kqemu_time, kqemu_time / (double)ticks_per_sec,
1482 kqemu_time / (double)total * 100.0,
1483 kqemu_exec_count,
1484 kqemu_ret_int_count,
1485 kqemu_ret_excp_count,
1486 kqemu_ret_intr_count);
1487 qemu_time = 0;
1488 kqemu_time = 0;
1489 kqemu_exec_count = 0;
1490 dev_time = 0;
1491 kqemu_ret_int_count = 0;
1492 kqemu_ret_excp_count = 0;
1493 kqemu_ret_intr_count = 0;
1494 #ifdef CONFIG_KQEMU
1495 kqemu_record_dump();
1496 #endif
1498 #else
1499 static void do_info_profile(Monitor *mon)
1501 monitor_printf(mon, "Internal profiler not compiled\n");
1503 #endif
1505 /* Capture support */
1506 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1508 static void do_info_capture(Monitor *mon)
1510 int i;
1511 CaptureState *s;
1513 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1514 monitor_printf(mon, "[%d]: ", i);
1515 s->ops.info (s->opaque);
1519 static void do_stop_capture(Monitor *mon, int n)
1521 int i;
1522 CaptureState *s;
1524 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1525 if (i == n) {
1526 s->ops.destroy (s->opaque);
1527 LIST_REMOVE (s, entries);
1528 qemu_free (s);
1529 return;
1534 #ifdef HAS_AUDIO
1535 static void do_wav_capture(Monitor *mon, const char *path,
1536 int has_freq, int freq,
1537 int has_bits, int bits,
1538 int has_channels, int nchannels)
1540 CaptureState *s;
1542 s = qemu_mallocz (sizeof (*s));
1544 freq = has_freq ? freq : 44100;
1545 bits = has_bits ? bits : 16;
1546 nchannels = has_channels ? nchannels : 2;
1548 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1549 monitor_printf(mon, "Faied to add wave capture\n");
1550 qemu_free (s);
1552 LIST_INSERT_HEAD (&capture_head, s, entries);
1554 #endif
1556 #if defined(TARGET_I386)
1557 static void do_inject_nmi(Monitor *mon, int cpu_index)
1559 CPUState *env;
1561 for (env = first_cpu; env != NULL; env = env->next_cpu)
1562 if (env->cpu_index == cpu_index) {
1563 if (kvm_enabled())
1564 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1565 else
1566 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1567 break;
1570 #endif
1572 static void do_info_status(Monitor *mon)
1574 if (vm_running) {
1575 if (singlestep) {
1576 monitor_printf(mon, "VM status: running (single step mode)\n");
1577 } else {
1578 monitor_printf(mon, "VM status: running\n");
1580 } else
1581 monitor_printf(mon, "VM status: paused\n");
1585 static void do_balloon(Monitor *mon, int value)
1587 ram_addr_t target = value;
1588 qemu_balloon(target << 20);
1591 static void do_info_balloon(Monitor *mon)
1593 ram_addr_t actual;
1595 actual = qemu_balloon_status();
1596 if (kvm_enabled() && !kvm_has_sync_mmu())
1597 monitor_printf(mon, "Using KVM without synchronous MMU, "
1598 "ballooning disabled\n");
1599 else if (actual == 0)
1600 monitor_printf(mon, "Ballooning not activated in VM\n");
1601 else
1602 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1605 static void do_acl(Monitor *mon,
1606 const char *command,
1607 const char *aclname,
1608 const char *match,
1609 int has_index,
1610 int index)
1612 qemu_acl *acl;
1614 acl = qemu_acl_find(aclname);
1615 if (!acl) {
1616 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1617 return;
1620 if (strcmp(command, "show") == 0) {
1621 int i = 0;
1622 qemu_acl_entry *entry;
1623 monitor_printf(mon, "policy: %s\n",
1624 acl->defaultDeny ? "deny" : "allow");
1625 TAILQ_FOREACH(entry, &acl->entries, next) {
1626 i++;
1627 monitor_printf(mon, "%d: %s %s\n", i,
1628 entry->deny ? "deny" : "allow",
1629 entry->match);
1631 } else if (strcmp(command, "reset") == 0) {
1632 qemu_acl_reset(acl);
1633 monitor_printf(mon, "acl: removed all rules\n");
1634 } else if (strcmp(command, "policy") == 0) {
1635 if (!match) {
1636 monitor_printf(mon, "acl: missing policy parameter\n");
1637 return;
1640 if (strcmp(match, "allow") == 0) {
1641 acl->defaultDeny = 0;
1642 monitor_printf(mon, "acl: policy set to 'allow'\n");
1643 } else if (strcmp(match, "deny") == 0) {
1644 acl->defaultDeny = 1;
1645 monitor_printf(mon, "acl: policy set to 'deny'\n");
1646 } else {
1647 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1649 } else if ((strcmp(command, "allow") == 0) ||
1650 (strcmp(command, "deny") == 0)) {
1651 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1652 int ret;
1654 if (!match) {
1655 monitor_printf(mon, "acl: missing match parameter\n");
1656 return;
1659 if (has_index)
1660 ret = qemu_acl_insert(acl, deny, match, index);
1661 else
1662 ret = qemu_acl_append(acl, deny, match);
1663 if (ret < 0)
1664 monitor_printf(mon, "acl: unable to add acl entry\n");
1665 else
1666 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1667 } else if (strcmp(command, "remove") == 0) {
1668 int ret;
1670 if (!match) {
1671 monitor_printf(mon, "acl: missing match parameter\n");
1672 return;
1675 ret = qemu_acl_remove(acl, match);
1676 if (ret < 0)
1677 monitor_printf(mon, "acl: no matching acl entry\n");
1678 else
1679 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1680 } else {
1681 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1685 /* Please update qemu-doc.texi when adding or changing commands */
1686 static const mon_cmd_t mon_cmds[] = {
1687 { "help|?", "s?", help_cmd,
1688 "[cmd]", "show the help" },
1689 { "commit", "s", do_commit,
1690 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1691 { "info", "s?", do_info,
1692 "[subcommand]", "show various information about the system state" },
1693 { "q|quit", "", do_quit,
1694 "", "quit the emulator" },
1695 { "eject", "-fB", do_eject,
1696 "[-f] device", "eject a removable medium (use -f to force it)" },
1697 { "change", "BFs?", do_change,
1698 "device filename [format]", "change a removable medium, optional format" },
1699 { "screendump", "F", do_screen_dump,
1700 "filename", "save screen into PPM image 'filename'" },
1701 { "logfile", "F", do_logfile,
1702 "filename", "output logs to 'filename'" },
1703 { "log", "s", do_log,
1704 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1705 { "savevm", "s?", do_savevm,
1706 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1707 { "loadvm", "s", do_loadvm,
1708 "tag|id", "restore a VM snapshot from its tag or id" },
1709 { "delvm", "s", do_delvm,
1710 "tag|id", "delete a VM snapshot from its tag or id" },
1711 { "singlestep", "s?", do_singlestep,
1712 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1713 { "stop", "", do_stop,
1714 "", "stop emulation", },
1715 { "c|cont", "", do_cont,
1716 "", "resume emulation", },
1717 { "gdbserver", "s?", do_gdbserver,
1718 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
1719 { "x", "/l", do_memory_dump,
1720 "/fmt addr", "virtual memory dump starting at 'addr'", },
1721 { "xp", "/l", do_physical_memory_dump,
1722 "/fmt addr", "physical memory dump starting at 'addr'", },
1723 { "p|print", "/l", do_print,
1724 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1725 { "i", "/ii.", do_ioport_read,
1726 "/fmt addr", "I/O port read" },
1728 { "sendkey", "si?", do_sendkey,
1729 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1730 { "system_reset", "", do_system_reset,
1731 "", "reset the system" },
1732 { "system_powerdown", "", do_system_powerdown,
1733 "", "send system power down event" },
1734 { "sum", "ii", do_sum,
1735 "addr size", "compute the checksum of a memory region" },
1736 { "usb_add", "s", do_usb_add,
1737 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1738 { "usb_del", "s", do_usb_del,
1739 "device", "remove USB device 'bus.addr'" },
1740 { "cpu", "i", do_cpu_set,
1741 "index", "set the default CPU" },
1742 { "mouse_move", "sss?", do_mouse_move,
1743 "dx dy [dz]", "send mouse move events" },
1744 { "mouse_button", "i", do_mouse_button,
1745 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1746 { "mouse_set", "i", do_mouse_set,
1747 "index", "set which mouse device receives events" },
1748 #ifdef HAS_AUDIO
1749 { "wavcapture", "si?i?i?", do_wav_capture,
1750 "path [frequency [bits [channels]]]",
1751 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1752 #endif
1753 { "stopcapture", "i", do_stop_capture,
1754 "capture index", "stop capture" },
1755 { "memsave", "lis", do_memory_save,
1756 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1757 { "pmemsave", "lis", do_physical_memory_save,
1758 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1759 { "boot_set", "s", do_boot_set,
1760 "bootdevice", "define new values for the boot device list" },
1761 #if defined(TARGET_I386)
1762 { "nmi", "i", do_inject_nmi,
1763 "cpu", "inject an NMI on the given CPU", },
1764 #endif
1765 { "migrate", "-ds", do_migrate,
1766 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1767 { "migrate_cancel", "", do_migrate_cancel,
1768 "", "cancel the current VM migration" },
1769 { "migrate_set_speed", "s", do_migrate_set_speed,
1770 "value", "set maximum speed (in bytes) for migrations" },
1771 #if defined(TARGET_I386)
1772 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1773 "[file=file][,if=type][,bus=n]\n"
1774 "[,unit=m][,media=d][index=i]\n"
1775 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1776 "[snapshot=on|off][,cache=on|off]",
1777 "add drive to PCI storage controller" },
1778 { "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" },
1779 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1780 #endif
1781 { "host_net_add", "ss?", net_host_device_add,
1782 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
1783 { "host_net_remove", "is", net_host_device_remove,
1784 "vlan_id name", "remove host VLAN client" },
1785 #ifdef CONFIG_SLIRP
1786 { "host_net_redir", "s", net_slirp_redir,
1787 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
1788 #endif
1789 { "balloon", "i", do_balloon,
1790 "target", "request VM to change it's memory allocation (in MB)" },
1791 { "set_link", "ss", do_set_link,
1792 "name up|down", "change the link status of a network adapter" },
1793 { "watchdog_action", "s", do_watchdog_action,
1794 "[reset|shutdown|poweroff|pause|debug|none]", "change watchdog action" },
1795 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match> [<index>]]\n",
1796 "acl show vnc.username\n"
1797 "acl policy vnc.username deny\n"
1798 "acl allow vnc.username fred\n"
1799 "acl deny vnc.username bob\n"
1800 "acl reset vnc.username\n" },
1801 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1802 { NULL, NULL, },
1805 /* Please update qemu-doc.texi when adding or changing commands */
1806 static const mon_cmd_t info_cmds[] = {
1807 { "version", "", do_info_version,
1808 "", "show the version of QEMU" },
1809 { "network", "", do_info_network,
1810 "", "show the network state" },
1811 { "chardev", "", qemu_chr_info,
1812 "", "show the character devices" },
1813 { "block", "", bdrv_info,
1814 "", "show the block devices" },
1815 { "blockstats", "", bdrv_info_stats,
1816 "", "show block device statistics" },
1817 { "registers", "", do_info_registers,
1818 "", "show the cpu registers" },
1819 { "cpus", "", do_info_cpus,
1820 "", "show infos for each CPU" },
1821 { "history", "", do_info_history,
1822 "", "show the command line history", },
1823 { "irq", "", irq_info,
1824 "", "show the interrupts statistics (if available)", },
1825 { "pic", "", pic_info,
1826 "", "show i8259 (PIC) state", },
1827 { "pci", "", pci_info,
1828 "", "show PCI info", },
1829 #if defined(TARGET_I386) || defined(TARGET_SH4)
1830 { "tlb", "", tlb_info,
1831 "", "show virtual to physical memory mappings", },
1832 #endif
1833 #if defined(TARGET_I386)
1834 { "mem", "", mem_info,
1835 "", "show the active virtual memory mappings", },
1836 { "hpet", "", do_info_hpet,
1837 "", "show state of HPET", },
1838 #endif
1839 { "jit", "", do_info_jit,
1840 "", "show dynamic compiler info", },
1841 { "kqemu", "", do_info_kqemu,
1842 "", "show KQEMU information", },
1843 { "kvm", "", do_info_kvm,
1844 "", "show KVM information", },
1845 { "numa", "", do_info_numa,
1846 "", "show NUMA information", },
1847 { "usb", "", usb_info,
1848 "", "show guest USB devices", },
1849 { "usbhost", "", usb_host_info,
1850 "", "show host USB devices", },
1851 { "profile", "", do_info_profile,
1852 "", "show profiling information", },
1853 { "capture", "", do_info_capture,
1854 "", "show capture information" },
1855 { "snapshots", "", do_info_snapshots,
1856 "", "show the currently saved VM snapshots" },
1857 { "status", "", do_info_status,
1858 "", "show the current VM status (running|paused)" },
1859 { "pcmcia", "", pcmcia_info,
1860 "", "show guest PCMCIA status" },
1861 { "mice", "", do_info_mice,
1862 "", "show which guest mouse is receiving events" },
1863 { "vnc", "", do_info_vnc,
1864 "", "show the vnc server status"},
1865 { "name", "", do_info_name,
1866 "", "show the current VM name" },
1867 { "uuid", "", do_info_uuid,
1868 "", "show the current VM UUID" },
1869 #if defined(TARGET_PPC)
1870 { "cpustats", "", do_info_cpu_stats,
1871 "", "show CPU statistics", },
1872 #endif
1873 #if defined(CONFIG_SLIRP)
1874 { "slirp", "", do_info_slirp,
1875 "", "show SLIRP statistics", },
1876 #endif
1877 { "migrate", "", do_info_migrate, "", "show migration status" },
1878 { "balloon", "", do_info_balloon,
1879 "", "show balloon information" },
1880 { NULL, NULL, },
1883 /*******************************************************************/
1885 static const char *pch;
1886 static jmp_buf expr_env;
1888 #define MD_TLONG 0
1889 #define MD_I32 1
1891 typedef struct MonitorDef {
1892 const char *name;
1893 int offset;
1894 target_long (*get_value)(const struct MonitorDef *md, int val);
1895 int type;
1896 } MonitorDef;
1898 #if defined(TARGET_I386)
1899 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1901 CPUState *env = mon_get_cpu();
1902 if (!env)
1903 return 0;
1904 return env->eip + env->segs[R_CS].base;
1906 #endif
1908 #if defined(TARGET_PPC)
1909 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1911 CPUState *env = mon_get_cpu();
1912 unsigned int u;
1913 int i;
1915 if (!env)
1916 return 0;
1918 u = 0;
1919 for (i = 0; i < 8; i++)
1920 u |= env->crf[i] << (32 - (4 * i));
1922 return u;
1925 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1927 CPUState *env = mon_get_cpu();
1928 if (!env)
1929 return 0;
1930 return env->msr;
1933 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1935 CPUState *env = mon_get_cpu();
1936 if (!env)
1937 return 0;
1938 return env->xer;
1941 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1943 CPUState *env = mon_get_cpu();
1944 if (!env)
1945 return 0;
1946 return cpu_ppc_load_decr(env);
1949 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1951 CPUState *env = mon_get_cpu();
1952 if (!env)
1953 return 0;
1954 return cpu_ppc_load_tbu(env);
1957 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1959 CPUState *env = mon_get_cpu();
1960 if (!env)
1961 return 0;
1962 return cpu_ppc_load_tbl(env);
1964 #endif
1966 #if defined(TARGET_SPARC)
1967 #ifndef TARGET_SPARC64
1968 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1970 CPUState *env = mon_get_cpu();
1971 if (!env)
1972 return 0;
1973 return GET_PSR(env);
1975 #endif
1977 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1979 CPUState *env = mon_get_cpu();
1980 if (!env)
1981 return 0;
1982 return env->regwptr[val];
1984 #endif
1986 static const MonitorDef monitor_defs[] = {
1987 #ifdef TARGET_I386
1989 #define SEG(name, seg) \
1990 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1991 { name ".base", offsetof(CPUState, segs[seg].base) },\
1992 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1994 { "eax", offsetof(CPUState, regs[0]) },
1995 { "ecx", offsetof(CPUState, regs[1]) },
1996 { "edx", offsetof(CPUState, regs[2]) },
1997 { "ebx", offsetof(CPUState, regs[3]) },
1998 { "esp|sp", offsetof(CPUState, regs[4]) },
1999 { "ebp|fp", offsetof(CPUState, regs[5]) },
2000 { "esi", offsetof(CPUState, regs[6]) },
2001 { "edi", offsetof(CPUState, regs[7]) },
2002 #ifdef TARGET_X86_64
2003 { "r8", offsetof(CPUState, regs[8]) },
2004 { "r9", offsetof(CPUState, regs[9]) },
2005 { "r10", offsetof(CPUState, regs[10]) },
2006 { "r11", offsetof(CPUState, regs[11]) },
2007 { "r12", offsetof(CPUState, regs[12]) },
2008 { "r13", offsetof(CPUState, regs[13]) },
2009 { "r14", offsetof(CPUState, regs[14]) },
2010 { "r15", offsetof(CPUState, regs[15]) },
2011 #endif
2012 { "eflags", offsetof(CPUState, eflags) },
2013 { "eip", offsetof(CPUState, eip) },
2014 SEG("cs", R_CS)
2015 SEG("ds", R_DS)
2016 SEG("es", R_ES)
2017 SEG("ss", R_SS)
2018 SEG("fs", R_FS)
2019 SEG("gs", R_GS)
2020 { "pc", 0, monitor_get_pc, },
2021 #elif defined(TARGET_PPC)
2022 /* General purpose registers */
2023 { "r0", offsetof(CPUState, gpr[0]) },
2024 { "r1", offsetof(CPUState, gpr[1]) },
2025 { "r2", offsetof(CPUState, gpr[2]) },
2026 { "r3", offsetof(CPUState, gpr[3]) },
2027 { "r4", offsetof(CPUState, gpr[4]) },
2028 { "r5", offsetof(CPUState, gpr[5]) },
2029 { "r6", offsetof(CPUState, gpr[6]) },
2030 { "r7", offsetof(CPUState, gpr[7]) },
2031 { "r8", offsetof(CPUState, gpr[8]) },
2032 { "r9", offsetof(CPUState, gpr[9]) },
2033 { "r10", offsetof(CPUState, gpr[10]) },
2034 { "r11", offsetof(CPUState, gpr[11]) },
2035 { "r12", offsetof(CPUState, gpr[12]) },
2036 { "r13", offsetof(CPUState, gpr[13]) },
2037 { "r14", offsetof(CPUState, gpr[14]) },
2038 { "r15", offsetof(CPUState, gpr[15]) },
2039 { "r16", offsetof(CPUState, gpr[16]) },
2040 { "r17", offsetof(CPUState, gpr[17]) },
2041 { "r18", offsetof(CPUState, gpr[18]) },
2042 { "r19", offsetof(CPUState, gpr[19]) },
2043 { "r20", offsetof(CPUState, gpr[20]) },
2044 { "r21", offsetof(CPUState, gpr[21]) },
2045 { "r22", offsetof(CPUState, gpr[22]) },
2046 { "r23", offsetof(CPUState, gpr[23]) },
2047 { "r24", offsetof(CPUState, gpr[24]) },
2048 { "r25", offsetof(CPUState, gpr[25]) },
2049 { "r26", offsetof(CPUState, gpr[26]) },
2050 { "r27", offsetof(CPUState, gpr[27]) },
2051 { "r28", offsetof(CPUState, gpr[28]) },
2052 { "r29", offsetof(CPUState, gpr[29]) },
2053 { "r30", offsetof(CPUState, gpr[30]) },
2054 { "r31", offsetof(CPUState, gpr[31]) },
2055 /* Floating point registers */
2056 { "f0", offsetof(CPUState, fpr[0]) },
2057 { "f1", offsetof(CPUState, fpr[1]) },
2058 { "f2", offsetof(CPUState, fpr[2]) },
2059 { "f3", offsetof(CPUState, fpr[3]) },
2060 { "f4", offsetof(CPUState, fpr[4]) },
2061 { "f5", offsetof(CPUState, fpr[5]) },
2062 { "f6", offsetof(CPUState, fpr[6]) },
2063 { "f7", offsetof(CPUState, fpr[7]) },
2064 { "f8", offsetof(CPUState, fpr[8]) },
2065 { "f9", offsetof(CPUState, fpr[9]) },
2066 { "f10", offsetof(CPUState, fpr[10]) },
2067 { "f11", offsetof(CPUState, fpr[11]) },
2068 { "f12", offsetof(CPUState, fpr[12]) },
2069 { "f13", offsetof(CPUState, fpr[13]) },
2070 { "f14", offsetof(CPUState, fpr[14]) },
2071 { "f15", offsetof(CPUState, fpr[15]) },
2072 { "f16", offsetof(CPUState, fpr[16]) },
2073 { "f17", offsetof(CPUState, fpr[17]) },
2074 { "f18", offsetof(CPUState, fpr[18]) },
2075 { "f19", offsetof(CPUState, fpr[19]) },
2076 { "f20", offsetof(CPUState, fpr[20]) },
2077 { "f21", offsetof(CPUState, fpr[21]) },
2078 { "f22", offsetof(CPUState, fpr[22]) },
2079 { "f23", offsetof(CPUState, fpr[23]) },
2080 { "f24", offsetof(CPUState, fpr[24]) },
2081 { "f25", offsetof(CPUState, fpr[25]) },
2082 { "f26", offsetof(CPUState, fpr[26]) },
2083 { "f27", offsetof(CPUState, fpr[27]) },
2084 { "f28", offsetof(CPUState, fpr[28]) },
2085 { "f29", offsetof(CPUState, fpr[29]) },
2086 { "f30", offsetof(CPUState, fpr[30]) },
2087 { "f31", offsetof(CPUState, fpr[31]) },
2088 { "fpscr", offsetof(CPUState, fpscr) },
2089 /* Next instruction pointer */
2090 { "nip|pc", offsetof(CPUState, nip) },
2091 { "lr", offsetof(CPUState, lr) },
2092 { "ctr", offsetof(CPUState, ctr) },
2093 { "decr", 0, &monitor_get_decr, },
2094 { "ccr", 0, &monitor_get_ccr, },
2095 /* Machine state register */
2096 { "msr", 0, &monitor_get_msr, },
2097 { "xer", 0, &monitor_get_xer, },
2098 { "tbu", 0, &monitor_get_tbu, },
2099 { "tbl", 0, &monitor_get_tbl, },
2100 #if defined(TARGET_PPC64)
2101 /* Address space register */
2102 { "asr", offsetof(CPUState, asr) },
2103 #endif
2104 /* Segment registers */
2105 { "sdr1", offsetof(CPUState, sdr1) },
2106 { "sr0", offsetof(CPUState, sr[0]) },
2107 { "sr1", offsetof(CPUState, sr[1]) },
2108 { "sr2", offsetof(CPUState, sr[2]) },
2109 { "sr3", offsetof(CPUState, sr[3]) },
2110 { "sr4", offsetof(CPUState, sr[4]) },
2111 { "sr5", offsetof(CPUState, sr[5]) },
2112 { "sr6", offsetof(CPUState, sr[6]) },
2113 { "sr7", offsetof(CPUState, sr[7]) },
2114 { "sr8", offsetof(CPUState, sr[8]) },
2115 { "sr9", offsetof(CPUState, sr[9]) },
2116 { "sr10", offsetof(CPUState, sr[10]) },
2117 { "sr11", offsetof(CPUState, sr[11]) },
2118 { "sr12", offsetof(CPUState, sr[12]) },
2119 { "sr13", offsetof(CPUState, sr[13]) },
2120 { "sr14", offsetof(CPUState, sr[14]) },
2121 { "sr15", offsetof(CPUState, sr[15]) },
2122 /* Too lazy to put BATs and SPRs ... */
2123 #elif defined(TARGET_SPARC)
2124 { "g0", offsetof(CPUState, gregs[0]) },
2125 { "g1", offsetof(CPUState, gregs[1]) },
2126 { "g2", offsetof(CPUState, gregs[2]) },
2127 { "g3", offsetof(CPUState, gregs[3]) },
2128 { "g4", offsetof(CPUState, gregs[4]) },
2129 { "g5", offsetof(CPUState, gregs[5]) },
2130 { "g6", offsetof(CPUState, gregs[6]) },
2131 { "g7", offsetof(CPUState, gregs[7]) },
2132 { "o0", 0, monitor_get_reg },
2133 { "o1", 1, monitor_get_reg },
2134 { "o2", 2, monitor_get_reg },
2135 { "o3", 3, monitor_get_reg },
2136 { "o4", 4, monitor_get_reg },
2137 { "o5", 5, monitor_get_reg },
2138 { "o6", 6, monitor_get_reg },
2139 { "o7", 7, monitor_get_reg },
2140 { "l0", 8, monitor_get_reg },
2141 { "l1", 9, monitor_get_reg },
2142 { "l2", 10, monitor_get_reg },
2143 { "l3", 11, monitor_get_reg },
2144 { "l4", 12, monitor_get_reg },
2145 { "l5", 13, monitor_get_reg },
2146 { "l6", 14, monitor_get_reg },
2147 { "l7", 15, monitor_get_reg },
2148 { "i0", 16, monitor_get_reg },
2149 { "i1", 17, monitor_get_reg },
2150 { "i2", 18, monitor_get_reg },
2151 { "i3", 19, monitor_get_reg },
2152 { "i4", 20, monitor_get_reg },
2153 { "i5", 21, monitor_get_reg },
2154 { "i6", 22, monitor_get_reg },
2155 { "i7", 23, monitor_get_reg },
2156 { "pc", offsetof(CPUState, pc) },
2157 { "npc", offsetof(CPUState, npc) },
2158 { "y", offsetof(CPUState, y) },
2159 #ifndef TARGET_SPARC64
2160 { "psr", 0, &monitor_get_psr, },
2161 { "wim", offsetof(CPUState, wim) },
2162 #endif
2163 { "tbr", offsetof(CPUState, tbr) },
2164 { "fsr", offsetof(CPUState, fsr) },
2165 { "f0", offsetof(CPUState, fpr[0]) },
2166 { "f1", offsetof(CPUState, fpr[1]) },
2167 { "f2", offsetof(CPUState, fpr[2]) },
2168 { "f3", offsetof(CPUState, fpr[3]) },
2169 { "f4", offsetof(CPUState, fpr[4]) },
2170 { "f5", offsetof(CPUState, fpr[5]) },
2171 { "f6", offsetof(CPUState, fpr[6]) },
2172 { "f7", offsetof(CPUState, fpr[7]) },
2173 { "f8", offsetof(CPUState, fpr[8]) },
2174 { "f9", offsetof(CPUState, fpr[9]) },
2175 { "f10", offsetof(CPUState, fpr[10]) },
2176 { "f11", offsetof(CPUState, fpr[11]) },
2177 { "f12", offsetof(CPUState, fpr[12]) },
2178 { "f13", offsetof(CPUState, fpr[13]) },
2179 { "f14", offsetof(CPUState, fpr[14]) },
2180 { "f15", offsetof(CPUState, fpr[15]) },
2181 { "f16", offsetof(CPUState, fpr[16]) },
2182 { "f17", offsetof(CPUState, fpr[17]) },
2183 { "f18", offsetof(CPUState, fpr[18]) },
2184 { "f19", offsetof(CPUState, fpr[19]) },
2185 { "f20", offsetof(CPUState, fpr[20]) },
2186 { "f21", offsetof(CPUState, fpr[21]) },
2187 { "f22", offsetof(CPUState, fpr[22]) },
2188 { "f23", offsetof(CPUState, fpr[23]) },
2189 { "f24", offsetof(CPUState, fpr[24]) },
2190 { "f25", offsetof(CPUState, fpr[25]) },
2191 { "f26", offsetof(CPUState, fpr[26]) },
2192 { "f27", offsetof(CPUState, fpr[27]) },
2193 { "f28", offsetof(CPUState, fpr[28]) },
2194 { "f29", offsetof(CPUState, fpr[29]) },
2195 { "f30", offsetof(CPUState, fpr[30]) },
2196 { "f31", offsetof(CPUState, fpr[31]) },
2197 #ifdef TARGET_SPARC64
2198 { "f32", offsetof(CPUState, fpr[32]) },
2199 { "f34", offsetof(CPUState, fpr[34]) },
2200 { "f36", offsetof(CPUState, fpr[36]) },
2201 { "f38", offsetof(CPUState, fpr[38]) },
2202 { "f40", offsetof(CPUState, fpr[40]) },
2203 { "f42", offsetof(CPUState, fpr[42]) },
2204 { "f44", offsetof(CPUState, fpr[44]) },
2205 { "f46", offsetof(CPUState, fpr[46]) },
2206 { "f48", offsetof(CPUState, fpr[48]) },
2207 { "f50", offsetof(CPUState, fpr[50]) },
2208 { "f52", offsetof(CPUState, fpr[52]) },
2209 { "f54", offsetof(CPUState, fpr[54]) },
2210 { "f56", offsetof(CPUState, fpr[56]) },
2211 { "f58", offsetof(CPUState, fpr[58]) },
2212 { "f60", offsetof(CPUState, fpr[60]) },
2213 { "f62", offsetof(CPUState, fpr[62]) },
2214 { "asi", offsetof(CPUState, asi) },
2215 { "pstate", offsetof(CPUState, pstate) },
2216 { "cansave", offsetof(CPUState, cansave) },
2217 { "canrestore", offsetof(CPUState, canrestore) },
2218 { "otherwin", offsetof(CPUState, otherwin) },
2219 { "wstate", offsetof(CPUState, wstate) },
2220 { "cleanwin", offsetof(CPUState, cleanwin) },
2221 { "fprs", offsetof(CPUState, fprs) },
2222 #endif
2223 #endif
2224 { NULL },
2227 static void expr_error(Monitor *mon, const char *msg)
2229 monitor_printf(mon, "%s\n", msg);
2230 longjmp(expr_env, 1);
2233 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2234 static int get_monitor_def(target_long *pval, const char *name)
2236 const MonitorDef *md;
2237 void *ptr;
2239 for(md = monitor_defs; md->name != NULL; md++) {
2240 if (compare_cmd(name, md->name)) {
2241 if (md->get_value) {
2242 *pval = md->get_value(md, md->offset);
2243 } else {
2244 CPUState *env = mon_get_cpu();
2245 if (!env)
2246 return -2;
2247 ptr = (uint8_t *)env + md->offset;
2248 switch(md->type) {
2249 case MD_I32:
2250 *pval = *(int32_t *)ptr;
2251 break;
2252 case MD_TLONG:
2253 *pval = *(target_long *)ptr;
2254 break;
2255 default:
2256 *pval = 0;
2257 break;
2260 return 0;
2263 return -1;
2266 static void next(void)
2268 if (pch != '\0') {
2269 pch++;
2270 while (qemu_isspace(*pch))
2271 pch++;
2275 static int64_t expr_sum(Monitor *mon);
2277 static int64_t expr_unary(Monitor *mon)
2279 int64_t n;
2280 char *p;
2281 int ret;
2283 switch(*pch) {
2284 case '+':
2285 next();
2286 n = expr_unary(mon);
2287 break;
2288 case '-':
2289 next();
2290 n = -expr_unary(mon);
2291 break;
2292 case '~':
2293 next();
2294 n = ~expr_unary(mon);
2295 break;
2296 case '(':
2297 next();
2298 n = expr_sum(mon);
2299 if (*pch != ')') {
2300 expr_error(mon, "')' expected");
2302 next();
2303 break;
2304 case '\'':
2305 pch++;
2306 if (*pch == '\0')
2307 expr_error(mon, "character constant expected");
2308 n = *pch;
2309 pch++;
2310 if (*pch != '\'')
2311 expr_error(mon, "missing terminating \' character");
2312 next();
2313 break;
2314 case '$':
2316 char buf[128], *q;
2317 target_long reg=0;
2319 pch++;
2320 q = buf;
2321 while ((*pch >= 'a' && *pch <= 'z') ||
2322 (*pch >= 'A' && *pch <= 'Z') ||
2323 (*pch >= '0' && *pch <= '9') ||
2324 *pch == '_' || *pch == '.') {
2325 if ((q - buf) < sizeof(buf) - 1)
2326 *q++ = *pch;
2327 pch++;
2329 while (qemu_isspace(*pch))
2330 pch++;
2331 *q = 0;
2332 ret = get_monitor_def(&reg, buf);
2333 if (ret == -1)
2334 expr_error(mon, "unknown register");
2335 else if (ret == -2)
2336 expr_error(mon, "no cpu defined");
2337 n = reg;
2339 break;
2340 case '\0':
2341 expr_error(mon, "unexpected end of expression");
2342 n = 0;
2343 break;
2344 default:
2345 #if TARGET_PHYS_ADDR_BITS > 32
2346 n = strtoull(pch, &p, 0);
2347 #else
2348 n = strtoul(pch, &p, 0);
2349 #endif
2350 if (pch == p) {
2351 expr_error(mon, "invalid char in expression");
2353 pch = p;
2354 while (qemu_isspace(*pch))
2355 pch++;
2356 break;
2358 return n;
2362 static int64_t expr_prod(Monitor *mon)
2364 int64_t val, val2;
2365 int op;
2367 val = expr_unary(mon);
2368 for(;;) {
2369 op = *pch;
2370 if (op != '*' && op != '/' && op != '%')
2371 break;
2372 next();
2373 val2 = expr_unary(mon);
2374 switch(op) {
2375 default:
2376 case '*':
2377 val *= val2;
2378 break;
2379 case '/':
2380 case '%':
2381 if (val2 == 0)
2382 expr_error(mon, "division by zero");
2383 if (op == '/')
2384 val /= val2;
2385 else
2386 val %= val2;
2387 break;
2390 return val;
2393 static int64_t expr_logic(Monitor *mon)
2395 int64_t val, val2;
2396 int op;
2398 val = expr_prod(mon);
2399 for(;;) {
2400 op = *pch;
2401 if (op != '&' && op != '|' && op != '^')
2402 break;
2403 next();
2404 val2 = expr_prod(mon);
2405 switch(op) {
2406 default:
2407 case '&':
2408 val &= val2;
2409 break;
2410 case '|':
2411 val |= val2;
2412 break;
2413 case '^':
2414 val ^= val2;
2415 break;
2418 return val;
2421 static int64_t expr_sum(Monitor *mon)
2423 int64_t val, val2;
2424 int op;
2426 val = expr_logic(mon);
2427 for(;;) {
2428 op = *pch;
2429 if (op != '+' && op != '-')
2430 break;
2431 next();
2432 val2 = expr_logic(mon);
2433 if (op == '+')
2434 val += val2;
2435 else
2436 val -= val2;
2438 return val;
2441 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2443 pch = *pp;
2444 if (setjmp(expr_env)) {
2445 *pp = pch;
2446 return -1;
2448 while (qemu_isspace(*pch))
2449 pch++;
2450 *pval = expr_sum(mon);
2451 *pp = pch;
2452 return 0;
2455 static int get_str(char *buf, int buf_size, const char **pp)
2457 const char *p;
2458 char *q;
2459 int c;
2461 q = buf;
2462 p = *pp;
2463 while (qemu_isspace(*p))
2464 p++;
2465 if (*p == '\0') {
2466 fail:
2467 *q = '\0';
2468 *pp = p;
2469 return -1;
2471 if (*p == '\"') {
2472 p++;
2473 while (*p != '\0' && *p != '\"') {
2474 if (*p == '\\') {
2475 p++;
2476 c = *p++;
2477 switch(c) {
2478 case 'n':
2479 c = '\n';
2480 break;
2481 case 'r':
2482 c = '\r';
2483 break;
2484 case '\\':
2485 case '\'':
2486 case '\"':
2487 break;
2488 default:
2489 qemu_printf("unsupported escape code: '\\%c'\n", c);
2490 goto fail;
2492 if ((q - buf) < buf_size - 1) {
2493 *q++ = c;
2495 } else {
2496 if ((q - buf) < buf_size - 1) {
2497 *q++ = *p;
2499 p++;
2502 if (*p != '\"') {
2503 qemu_printf("unterminated string\n");
2504 goto fail;
2506 p++;
2507 } else {
2508 while (*p != '\0' && !qemu_isspace(*p)) {
2509 if ((q - buf) < buf_size - 1) {
2510 *q++ = *p;
2512 p++;
2515 *q = '\0';
2516 *pp = p;
2517 return 0;
2520 static int default_fmt_format = 'x';
2521 static int default_fmt_size = 4;
2523 #define MAX_ARGS 16
2525 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2527 const char *p, *pstart, *typestr;
2528 char *q;
2529 int c, nb_args, len, i, has_arg;
2530 const mon_cmd_t *cmd;
2531 char cmdname[256];
2532 char buf[1024];
2533 void *str_allocated[MAX_ARGS];
2534 void *args[MAX_ARGS];
2535 void (*handler_0)(Monitor *mon);
2536 void (*handler_1)(Monitor *mon, void *arg0);
2537 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2538 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2539 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2540 void *arg3);
2541 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2542 void *arg3, void *arg4);
2543 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2544 void *arg3, void *arg4, void *arg5);
2545 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2546 void *arg3, void *arg4, void *arg5, void *arg6);
2548 #ifdef DEBUG
2549 monitor_printf(mon, "command='%s'\n", cmdline);
2550 #endif
2552 /* extract the command name */
2553 p = cmdline;
2554 q = cmdname;
2555 while (qemu_isspace(*p))
2556 p++;
2557 if (*p == '\0')
2558 return;
2559 pstart = p;
2560 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2561 p++;
2562 len = p - pstart;
2563 if (len > sizeof(cmdname) - 1)
2564 len = sizeof(cmdname) - 1;
2565 memcpy(cmdname, pstart, len);
2566 cmdname[len] = '\0';
2568 /* find the command */
2569 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2570 if (compare_cmd(cmdname, cmd->name))
2571 goto found;
2573 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2574 return;
2575 found:
2577 for(i = 0; i < MAX_ARGS; i++)
2578 str_allocated[i] = NULL;
2580 /* parse the parameters */
2581 typestr = cmd->args_type;
2582 nb_args = 0;
2583 for(;;) {
2584 c = *typestr;
2585 if (c == '\0')
2586 break;
2587 typestr++;
2588 switch(c) {
2589 case 'F':
2590 case 'B':
2591 case 's':
2593 int ret;
2594 char *str;
2596 while (qemu_isspace(*p))
2597 p++;
2598 if (*typestr == '?') {
2599 typestr++;
2600 if (*p == '\0') {
2601 /* no optional string: NULL argument */
2602 str = NULL;
2603 goto add_str;
2606 ret = get_str(buf, sizeof(buf), &p);
2607 if (ret < 0) {
2608 switch(c) {
2609 case 'F':
2610 monitor_printf(mon, "%s: filename expected\n",
2611 cmdname);
2612 break;
2613 case 'B':
2614 monitor_printf(mon, "%s: block device name expected\n",
2615 cmdname);
2616 break;
2617 default:
2618 monitor_printf(mon, "%s: string expected\n", cmdname);
2619 break;
2621 goto fail;
2623 str = qemu_malloc(strlen(buf) + 1);
2624 pstrcpy(str, sizeof(buf), buf);
2625 str_allocated[nb_args] = str;
2626 add_str:
2627 if (nb_args >= MAX_ARGS) {
2628 error_args:
2629 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2630 goto fail;
2632 args[nb_args++] = str;
2634 break;
2635 case '/':
2637 int count, format, size;
2639 while (qemu_isspace(*p))
2640 p++;
2641 if (*p == '/') {
2642 /* format found */
2643 p++;
2644 count = 1;
2645 if (qemu_isdigit(*p)) {
2646 count = 0;
2647 while (qemu_isdigit(*p)) {
2648 count = count * 10 + (*p - '0');
2649 p++;
2652 size = -1;
2653 format = -1;
2654 for(;;) {
2655 switch(*p) {
2656 case 'o':
2657 case 'd':
2658 case 'u':
2659 case 'x':
2660 case 'i':
2661 case 'c':
2662 format = *p++;
2663 break;
2664 case 'b':
2665 size = 1;
2666 p++;
2667 break;
2668 case 'h':
2669 size = 2;
2670 p++;
2671 break;
2672 case 'w':
2673 size = 4;
2674 p++;
2675 break;
2676 case 'g':
2677 case 'L':
2678 size = 8;
2679 p++;
2680 break;
2681 default:
2682 goto next;
2685 next:
2686 if (*p != '\0' && !qemu_isspace(*p)) {
2687 monitor_printf(mon, "invalid char in format: '%c'\n",
2688 *p);
2689 goto fail;
2691 if (format < 0)
2692 format = default_fmt_format;
2693 if (format != 'i') {
2694 /* for 'i', not specifying a size gives -1 as size */
2695 if (size < 0)
2696 size = default_fmt_size;
2697 default_fmt_size = size;
2699 default_fmt_format = format;
2700 } else {
2701 count = 1;
2702 format = default_fmt_format;
2703 if (format != 'i') {
2704 size = default_fmt_size;
2705 } else {
2706 size = -1;
2709 if (nb_args + 3 > MAX_ARGS)
2710 goto error_args;
2711 args[nb_args++] = (void*)(long)count;
2712 args[nb_args++] = (void*)(long)format;
2713 args[nb_args++] = (void*)(long)size;
2715 break;
2716 case 'i':
2717 case 'l':
2719 int64_t val;
2721 while (qemu_isspace(*p))
2722 p++;
2723 if (*typestr == '?' || *typestr == '.') {
2724 if (*typestr == '?') {
2725 if (*p == '\0')
2726 has_arg = 0;
2727 else
2728 has_arg = 1;
2729 } else {
2730 if (*p == '.') {
2731 p++;
2732 while (qemu_isspace(*p))
2733 p++;
2734 has_arg = 1;
2735 } else {
2736 has_arg = 0;
2739 typestr++;
2740 if (nb_args >= MAX_ARGS)
2741 goto error_args;
2742 args[nb_args++] = (void *)(long)has_arg;
2743 if (!has_arg) {
2744 if (nb_args >= MAX_ARGS)
2745 goto error_args;
2746 val = -1;
2747 goto add_num;
2750 if (get_expr(mon, &val, &p))
2751 goto fail;
2752 add_num:
2753 if (c == 'i') {
2754 if (nb_args >= MAX_ARGS)
2755 goto error_args;
2756 args[nb_args++] = (void *)(long)val;
2757 } else {
2758 if ((nb_args + 1) >= MAX_ARGS)
2759 goto error_args;
2760 #if TARGET_PHYS_ADDR_BITS > 32
2761 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2762 #else
2763 args[nb_args++] = (void *)0;
2764 #endif
2765 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2768 break;
2769 case '-':
2771 int has_option;
2772 /* option */
2774 c = *typestr++;
2775 if (c == '\0')
2776 goto bad_type;
2777 while (qemu_isspace(*p))
2778 p++;
2779 has_option = 0;
2780 if (*p == '-') {
2781 p++;
2782 if (*p != c) {
2783 monitor_printf(mon, "%s: unsupported option -%c\n",
2784 cmdname, *p);
2785 goto fail;
2787 p++;
2788 has_option = 1;
2790 if (nb_args >= MAX_ARGS)
2791 goto error_args;
2792 args[nb_args++] = (void *)(long)has_option;
2794 break;
2795 default:
2796 bad_type:
2797 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2798 goto fail;
2801 /* check that all arguments were parsed */
2802 while (qemu_isspace(*p))
2803 p++;
2804 if (*p != '\0') {
2805 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2806 cmdname);
2807 goto fail;
2810 switch(nb_args) {
2811 case 0:
2812 handler_0 = cmd->handler;
2813 handler_0(mon);
2814 break;
2815 case 1:
2816 handler_1 = cmd->handler;
2817 handler_1(mon, args[0]);
2818 break;
2819 case 2:
2820 handler_2 = cmd->handler;
2821 handler_2(mon, args[0], args[1]);
2822 break;
2823 case 3:
2824 handler_3 = cmd->handler;
2825 handler_3(mon, args[0], args[1], args[2]);
2826 break;
2827 case 4:
2828 handler_4 = cmd->handler;
2829 handler_4(mon, args[0], args[1], args[2], args[3]);
2830 break;
2831 case 5:
2832 handler_5 = cmd->handler;
2833 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2834 break;
2835 case 6:
2836 handler_6 = cmd->handler;
2837 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2838 break;
2839 case 7:
2840 handler_7 = cmd->handler;
2841 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2842 args[6]);
2843 break;
2844 default:
2845 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2846 goto fail;
2848 fail:
2849 for(i = 0; i < MAX_ARGS; i++)
2850 qemu_free(str_allocated[i]);
2851 return;
2854 static void cmd_completion(const char *name, const char *list)
2856 const char *p, *pstart;
2857 char cmd[128];
2858 int len;
2860 p = list;
2861 for(;;) {
2862 pstart = p;
2863 p = strchr(p, '|');
2864 if (!p)
2865 p = pstart + strlen(pstart);
2866 len = p - pstart;
2867 if (len > sizeof(cmd) - 2)
2868 len = sizeof(cmd) - 2;
2869 memcpy(cmd, pstart, len);
2870 cmd[len] = '\0';
2871 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2872 readline_add_completion(cur_mon->rs, cmd);
2874 if (*p == '\0')
2875 break;
2876 p++;
2880 static void file_completion(const char *input)
2882 DIR *ffs;
2883 struct dirent *d;
2884 char path[1024];
2885 char file[1024], file_prefix[1024];
2886 int input_path_len;
2887 const char *p;
2889 p = strrchr(input, '/');
2890 if (!p) {
2891 input_path_len = 0;
2892 pstrcpy(file_prefix, sizeof(file_prefix), input);
2893 pstrcpy(path, sizeof(path), ".");
2894 } else {
2895 input_path_len = p - input + 1;
2896 memcpy(path, input, input_path_len);
2897 if (input_path_len > sizeof(path) - 1)
2898 input_path_len = sizeof(path) - 1;
2899 path[input_path_len] = '\0';
2900 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2902 #ifdef DEBUG_COMPLETION
2903 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2904 input, path, file_prefix);
2905 #endif
2906 ffs = opendir(path);
2907 if (!ffs)
2908 return;
2909 for(;;) {
2910 struct stat sb;
2911 d = readdir(ffs);
2912 if (!d)
2913 break;
2914 if (strstart(d->d_name, file_prefix, NULL)) {
2915 memcpy(file, input, input_path_len);
2916 if (input_path_len < sizeof(file))
2917 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2918 d->d_name);
2919 /* stat the file to find out if it's a directory.
2920 * In that case add a slash to speed up typing long paths
2922 stat(file, &sb);
2923 if(S_ISDIR(sb.st_mode))
2924 pstrcat(file, sizeof(file), "/");
2925 readline_add_completion(cur_mon->rs, file);
2928 closedir(ffs);
2931 static void block_completion_it(void *opaque, BlockDriverState *bs)
2933 const char *name = bdrv_get_device_name(bs);
2934 const char *input = opaque;
2936 if (input[0] == '\0' ||
2937 !strncmp(name, (char *)input, strlen(input))) {
2938 readline_add_completion(cur_mon->rs, name);
2942 /* NOTE: this parser is an approximate form of the real command parser */
2943 static void parse_cmdline(const char *cmdline,
2944 int *pnb_args, char **args)
2946 const char *p;
2947 int nb_args, ret;
2948 char buf[1024];
2950 p = cmdline;
2951 nb_args = 0;
2952 for(;;) {
2953 while (qemu_isspace(*p))
2954 p++;
2955 if (*p == '\0')
2956 break;
2957 if (nb_args >= MAX_ARGS)
2958 break;
2959 ret = get_str(buf, sizeof(buf), &p);
2960 args[nb_args] = qemu_strdup(buf);
2961 nb_args++;
2962 if (ret < 0)
2963 break;
2965 *pnb_args = nb_args;
2968 static void monitor_find_completion(const char *cmdline)
2970 const char *cmdname;
2971 char *args[MAX_ARGS];
2972 int nb_args, i, len;
2973 const char *ptype, *str;
2974 const mon_cmd_t *cmd;
2975 const KeyDef *key;
2977 parse_cmdline(cmdline, &nb_args, args);
2978 #ifdef DEBUG_COMPLETION
2979 for(i = 0; i < nb_args; i++) {
2980 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2982 #endif
2984 /* if the line ends with a space, it means we want to complete the
2985 next arg */
2986 len = strlen(cmdline);
2987 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2988 if (nb_args >= MAX_ARGS)
2989 return;
2990 args[nb_args++] = qemu_strdup("");
2992 if (nb_args <= 1) {
2993 /* command completion */
2994 if (nb_args == 0)
2995 cmdname = "";
2996 else
2997 cmdname = args[0];
2998 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2999 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3000 cmd_completion(cmdname, cmd->name);
3002 } else {
3003 /* find the command */
3004 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3005 if (compare_cmd(args[0], cmd->name))
3006 goto found;
3008 return;
3009 found:
3010 ptype = cmd->args_type;
3011 for(i = 0; i < nb_args - 2; i++) {
3012 if (*ptype != '\0') {
3013 ptype++;
3014 while (*ptype == '?')
3015 ptype++;
3018 str = args[nb_args - 1];
3019 switch(*ptype) {
3020 case 'F':
3021 /* file completion */
3022 readline_set_completion_index(cur_mon->rs, strlen(str));
3023 file_completion(str);
3024 break;
3025 case 'B':
3026 /* block device name completion */
3027 readline_set_completion_index(cur_mon->rs, strlen(str));
3028 bdrv_iterate(block_completion_it, (void *)str);
3029 break;
3030 case 's':
3031 /* XXX: more generic ? */
3032 if (!strcmp(cmd->name, "info")) {
3033 readline_set_completion_index(cur_mon->rs, strlen(str));
3034 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3035 cmd_completion(str, cmd->name);
3037 } else if (!strcmp(cmd->name, "sendkey")) {
3038 char *sep = strrchr(str, '-');
3039 if (sep)
3040 str = sep + 1;
3041 readline_set_completion_index(cur_mon->rs, strlen(str));
3042 for(key = key_defs; key->name != NULL; key++) {
3043 cmd_completion(str, key->name);
3046 break;
3047 default:
3048 break;
3051 for(i = 0; i < nb_args; i++)
3052 qemu_free(args[i]);
3055 static int monitor_can_read(void *opaque)
3057 Monitor *mon = opaque;
3059 return (mon->suspend_cnt == 0) ? 128 : 0;
3062 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3064 Monitor *old_mon = cur_mon;
3065 int i;
3067 cur_mon = opaque;
3069 if (cur_mon->rs) {
3070 for (i = 0; i < size; i++)
3071 readline_handle_byte(cur_mon->rs, buf[i]);
3072 } else {
3073 if (size == 0 || buf[size - 1] != 0)
3074 monitor_printf(cur_mon, "corrupted command\n");
3075 else
3076 monitor_handle_command(cur_mon, (char *)buf);
3079 cur_mon = old_mon;
3082 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3084 monitor_suspend(mon);
3085 monitor_handle_command(mon, cmdline);
3086 monitor_resume(mon);
3089 int monitor_suspend(Monitor *mon)
3091 if (!mon->rs)
3092 return -ENOTTY;
3093 mon->suspend_cnt++;
3094 return 0;
3097 void monitor_resume(Monitor *mon)
3099 if (!mon->rs)
3100 return;
3101 if (--mon->suspend_cnt == 0)
3102 readline_show_prompt(mon->rs);
3105 static void monitor_event(void *opaque, int event)
3107 Monitor *mon = opaque;
3109 switch (event) {
3110 case CHR_EVENT_MUX_IN:
3111 readline_restart(mon->rs);
3112 monitor_resume(mon);
3113 monitor_flush(mon);
3114 break;
3116 case CHR_EVENT_MUX_OUT:
3117 if (mon->suspend_cnt == 0)
3118 monitor_printf(mon, "\n");
3119 monitor_flush(mon);
3120 monitor_suspend(mon);
3121 break;
3123 case CHR_EVENT_RESET:
3124 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3125 "information\n", QEMU_VERSION);
3126 if (mon->chr->focus == 0)
3127 readline_show_prompt(mon->rs);
3128 break;
3134 * Local variables:
3135 * c-indent-level: 4
3136 * c-basic-offset: 4
3137 * tab-width: 8
3138 * End:
3141 void monitor_init(CharDriverState *chr, int flags)
3143 static int is_first_init = 1;
3144 Monitor *mon;
3146 if (is_first_init) {
3147 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3148 is_first_init = 0;
3151 mon = qemu_mallocz(sizeof(*mon));
3153 mon->chr = chr;
3154 mon->flags = flags;
3155 if (mon->chr->focus != 0)
3156 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3157 if (flags & MONITOR_USE_READLINE) {
3158 mon->rs = readline_init(mon, monitor_find_completion);
3159 monitor_read_command(mon, 0);
3162 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3163 mon);
3165 LIST_INSERT_HEAD(&mon_list, mon, entry);
3166 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3167 cur_mon = mon;
3170 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3172 BlockDriverState *bs = opaque;
3173 int ret = 0;
3175 if (bdrv_set_key(bs, password) != 0) {
3176 monitor_printf(mon, "invalid password\n");
3177 ret = -EPERM;
3179 if (mon->password_completion_cb)
3180 mon->password_completion_cb(mon->password_opaque, ret);
3182 monitor_read_command(mon, 1);
3185 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3186 BlockDriverCompletionFunc *completion_cb,
3187 void *opaque)
3189 int err;
3191 if (!bdrv_key_required(bs)) {
3192 if (completion_cb)
3193 completion_cb(opaque, 0);
3194 return;
3197 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3198 bdrv_get_encrypted_filename(bs));
3200 mon->password_completion_cb = completion_cb;
3201 mon->password_opaque = opaque;
3203 err = monitor_read_password(mon, bdrv_password_cb, bs);
3205 if (err && completion_cb)
3206 completion_cb(opaque, err);