build-fix typo in target-ia64/libkvm.c
[qemu-kvm/fedora.git] / monitor.c
blob540e95b3461feae5d938c8aa07ca8c9cec5ed730
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", "ss?", net_slirp_redir,
1787 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
1788 "host_net_redir remove [tcp:|udp:]host-port -- remove redirection\n"
1789 "host_net_redir list -- show all redirections" },
1790 #endif
1791 { "balloon", "i", do_balloon,
1792 "target", "request VM to change it's memory allocation (in MB)" },
1793 { "set_link", "ss", do_set_link,
1794 "name up|down", "change the link status of a network adapter" },
1795 { "watchdog_action", "s", do_watchdog_action,
1796 "[reset|shutdown|poweroff|pause|debug|none]", "change watchdog action" },
1797 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match> [<index>]]\n",
1798 "acl show vnc.username\n"
1799 "acl policy vnc.username deny\n"
1800 "acl allow vnc.username fred\n"
1801 "acl deny vnc.username bob\n"
1802 "acl reset vnc.username\n" },
1803 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1804 { NULL, NULL, },
1807 /* Please update qemu-doc.texi when adding or changing commands */
1808 static const mon_cmd_t info_cmds[] = {
1809 { "version", "", do_info_version,
1810 "", "show the version of QEMU" },
1811 { "network", "", do_info_network,
1812 "", "show the network state" },
1813 { "chardev", "", qemu_chr_info,
1814 "", "show the character devices" },
1815 { "block", "", bdrv_info,
1816 "", "show the block devices" },
1817 { "blockstats", "", bdrv_info_stats,
1818 "", "show block device statistics" },
1819 { "registers", "", do_info_registers,
1820 "", "show the cpu registers" },
1821 { "cpus", "", do_info_cpus,
1822 "", "show infos for each CPU" },
1823 { "history", "", do_info_history,
1824 "", "show the command line history", },
1825 { "irq", "", irq_info,
1826 "", "show the interrupts statistics (if available)", },
1827 { "pic", "", pic_info,
1828 "", "show i8259 (PIC) state", },
1829 { "pci", "", pci_info,
1830 "", "show PCI info", },
1831 #if defined(TARGET_I386) || defined(TARGET_SH4)
1832 { "tlb", "", tlb_info,
1833 "", "show virtual to physical memory mappings", },
1834 #endif
1835 #if defined(TARGET_I386)
1836 { "mem", "", mem_info,
1837 "", "show the active virtual memory mappings", },
1838 { "hpet", "", do_info_hpet,
1839 "", "show state of HPET", },
1840 #endif
1841 { "jit", "", do_info_jit,
1842 "", "show dynamic compiler info", },
1843 { "kqemu", "", do_info_kqemu,
1844 "", "show KQEMU information", },
1845 { "kvm", "", do_info_kvm,
1846 "", "show KVM information", },
1847 { "numa", "", do_info_numa,
1848 "", "show NUMA information", },
1849 { "usb", "", usb_info,
1850 "", "show guest USB devices", },
1851 { "usbhost", "", usb_host_info,
1852 "", "show host USB devices", },
1853 { "profile", "", do_info_profile,
1854 "", "show profiling information", },
1855 { "capture", "", do_info_capture,
1856 "", "show capture information" },
1857 { "snapshots", "", do_info_snapshots,
1858 "", "show the currently saved VM snapshots" },
1859 { "status", "", do_info_status,
1860 "", "show the current VM status (running|paused)" },
1861 { "pcmcia", "", pcmcia_info,
1862 "", "show guest PCMCIA status" },
1863 { "mice", "", do_info_mice,
1864 "", "show which guest mouse is receiving events" },
1865 { "vnc", "", do_info_vnc,
1866 "", "show the vnc server status"},
1867 { "name", "", do_info_name,
1868 "", "show the current VM name" },
1869 { "uuid", "", do_info_uuid,
1870 "", "show the current VM UUID" },
1871 #if defined(TARGET_PPC)
1872 { "cpustats", "", do_info_cpu_stats,
1873 "", "show CPU statistics", },
1874 #endif
1875 #if defined(CONFIG_SLIRP)
1876 { "slirp", "", do_info_slirp,
1877 "", "show SLIRP statistics", },
1878 #endif
1879 { "migrate", "", do_info_migrate, "", "show migration status" },
1880 { "balloon", "", do_info_balloon,
1881 "", "show balloon information" },
1882 { NULL, NULL, },
1885 /*******************************************************************/
1887 static const char *pch;
1888 static jmp_buf expr_env;
1890 #define MD_TLONG 0
1891 #define MD_I32 1
1893 typedef struct MonitorDef {
1894 const char *name;
1895 int offset;
1896 target_long (*get_value)(const struct MonitorDef *md, int val);
1897 int type;
1898 } MonitorDef;
1900 #if defined(TARGET_I386)
1901 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1903 CPUState *env = mon_get_cpu();
1904 if (!env)
1905 return 0;
1906 return env->eip + env->segs[R_CS].base;
1908 #endif
1910 #if defined(TARGET_PPC)
1911 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1913 CPUState *env = mon_get_cpu();
1914 unsigned int u;
1915 int i;
1917 if (!env)
1918 return 0;
1920 u = 0;
1921 for (i = 0; i < 8; i++)
1922 u |= env->crf[i] << (32 - (4 * i));
1924 return u;
1927 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1929 CPUState *env = mon_get_cpu();
1930 if (!env)
1931 return 0;
1932 return env->msr;
1935 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1937 CPUState *env = mon_get_cpu();
1938 if (!env)
1939 return 0;
1940 return env->xer;
1943 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1945 CPUState *env = mon_get_cpu();
1946 if (!env)
1947 return 0;
1948 return cpu_ppc_load_decr(env);
1951 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1953 CPUState *env = mon_get_cpu();
1954 if (!env)
1955 return 0;
1956 return cpu_ppc_load_tbu(env);
1959 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1961 CPUState *env = mon_get_cpu();
1962 if (!env)
1963 return 0;
1964 return cpu_ppc_load_tbl(env);
1966 #endif
1968 #if defined(TARGET_SPARC)
1969 #ifndef TARGET_SPARC64
1970 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1972 CPUState *env = mon_get_cpu();
1973 if (!env)
1974 return 0;
1975 return GET_PSR(env);
1977 #endif
1979 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1981 CPUState *env = mon_get_cpu();
1982 if (!env)
1983 return 0;
1984 return env->regwptr[val];
1986 #endif
1988 static const MonitorDef monitor_defs[] = {
1989 #ifdef TARGET_I386
1991 #define SEG(name, seg) \
1992 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1993 { name ".base", offsetof(CPUState, segs[seg].base) },\
1994 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1996 { "eax", offsetof(CPUState, regs[0]) },
1997 { "ecx", offsetof(CPUState, regs[1]) },
1998 { "edx", offsetof(CPUState, regs[2]) },
1999 { "ebx", offsetof(CPUState, regs[3]) },
2000 { "esp|sp", offsetof(CPUState, regs[4]) },
2001 { "ebp|fp", offsetof(CPUState, regs[5]) },
2002 { "esi", offsetof(CPUState, regs[6]) },
2003 { "edi", offsetof(CPUState, regs[7]) },
2004 #ifdef TARGET_X86_64
2005 { "r8", offsetof(CPUState, regs[8]) },
2006 { "r9", offsetof(CPUState, regs[9]) },
2007 { "r10", offsetof(CPUState, regs[10]) },
2008 { "r11", offsetof(CPUState, regs[11]) },
2009 { "r12", offsetof(CPUState, regs[12]) },
2010 { "r13", offsetof(CPUState, regs[13]) },
2011 { "r14", offsetof(CPUState, regs[14]) },
2012 { "r15", offsetof(CPUState, regs[15]) },
2013 #endif
2014 { "eflags", offsetof(CPUState, eflags) },
2015 { "eip", offsetof(CPUState, eip) },
2016 SEG("cs", R_CS)
2017 SEG("ds", R_DS)
2018 SEG("es", R_ES)
2019 SEG("ss", R_SS)
2020 SEG("fs", R_FS)
2021 SEG("gs", R_GS)
2022 { "pc", 0, monitor_get_pc, },
2023 #elif defined(TARGET_PPC)
2024 /* General purpose registers */
2025 { "r0", offsetof(CPUState, gpr[0]) },
2026 { "r1", offsetof(CPUState, gpr[1]) },
2027 { "r2", offsetof(CPUState, gpr[2]) },
2028 { "r3", offsetof(CPUState, gpr[3]) },
2029 { "r4", offsetof(CPUState, gpr[4]) },
2030 { "r5", offsetof(CPUState, gpr[5]) },
2031 { "r6", offsetof(CPUState, gpr[6]) },
2032 { "r7", offsetof(CPUState, gpr[7]) },
2033 { "r8", offsetof(CPUState, gpr[8]) },
2034 { "r9", offsetof(CPUState, gpr[9]) },
2035 { "r10", offsetof(CPUState, gpr[10]) },
2036 { "r11", offsetof(CPUState, gpr[11]) },
2037 { "r12", offsetof(CPUState, gpr[12]) },
2038 { "r13", offsetof(CPUState, gpr[13]) },
2039 { "r14", offsetof(CPUState, gpr[14]) },
2040 { "r15", offsetof(CPUState, gpr[15]) },
2041 { "r16", offsetof(CPUState, gpr[16]) },
2042 { "r17", offsetof(CPUState, gpr[17]) },
2043 { "r18", offsetof(CPUState, gpr[18]) },
2044 { "r19", offsetof(CPUState, gpr[19]) },
2045 { "r20", offsetof(CPUState, gpr[20]) },
2046 { "r21", offsetof(CPUState, gpr[21]) },
2047 { "r22", offsetof(CPUState, gpr[22]) },
2048 { "r23", offsetof(CPUState, gpr[23]) },
2049 { "r24", offsetof(CPUState, gpr[24]) },
2050 { "r25", offsetof(CPUState, gpr[25]) },
2051 { "r26", offsetof(CPUState, gpr[26]) },
2052 { "r27", offsetof(CPUState, gpr[27]) },
2053 { "r28", offsetof(CPUState, gpr[28]) },
2054 { "r29", offsetof(CPUState, gpr[29]) },
2055 { "r30", offsetof(CPUState, gpr[30]) },
2056 { "r31", offsetof(CPUState, gpr[31]) },
2057 /* Floating point registers */
2058 { "f0", offsetof(CPUState, fpr[0]) },
2059 { "f1", offsetof(CPUState, fpr[1]) },
2060 { "f2", offsetof(CPUState, fpr[2]) },
2061 { "f3", offsetof(CPUState, fpr[3]) },
2062 { "f4", offsetof(CPUState, fpr[4]) },
2063 { "f5", offsetof(CPUState, fpr[5]) },
2064 { "f6", offsetof(CPUState, fpr[6]) },
2065 { "f7", offsetof(CPUState, fpr[7]) },
2066 { "f8", offsetof(CPUState, fpr[8]) },
2067 { "f9", offsetof(CPUState, fpr[9]) },
2068 { "f10", offsetof(CPUState, fpr[10]) },
2069 { "f11", offsetof(CPUState, fpr[11]) },
2070 { "f12", offsetof(CPUState, fpr[12]) },
2071 { "f13", offsetof(CPUState, fpr[13]) },
2072 { "f14", offsetof(CPUState, fpr[14]) },
2073 { "f15", offsetof(CPUState, fpr[15]) },
2074 { "f16", offsetof(CPUState, fpr[16]) },
2075 { "f17", offsetof(CPUState, fpr[17]) },
2076 { "f18", offsetof(CPUState, fpr[18]) },
2077 { "f19", offsetof(CPUState, fpr[19]) },
2078 { "f20", offsetof(CPUState, fpr[20]) },
2079 { "f21", offsetof(CPUState, fpr[21]) },
2080 { "f22", offsetof(CPUState, fpr[22]) },
2081 { "f23", offsetof(CPUState, fpr[23]) },
2082 { "f24", offsetof(CPUState, fpr[24]) },
2083 { "f25", offsetof(CPUState, fpr[25]) },
2084 { "f26", offsetof(CPUState, fpr[26]) },
2085 { "f27", offsetof(CPUState, fpr[27]) },
2086 { "f28", offsetof(CPUState, fpr[28]) },
2087 { "f29", offsetof(CPUState, fpr[29]) },
2088 { "f30", offsetof(CPUState, fpr[30]) },
2089 { "f31", offsetof(CPUState, fpr[31]) },
2090 { "fpscr", offsetof(CPUState, fpscr) },
2091 /* Next instruction pointer */
2092 { "nip|pc", offsetof(CPUState, nip) },
2093 { "lr", offsetof(CPUState, lr) },
2094 { "ctr", offsetof(CPUState, ctr) },
2095 { "decr", 0, &monitor_get_decr, },
2096 { "ccr", 0, &monitor_get_ccr, },
2097 /* Machine state register */
2098 { "msr", 0, &monitor_get_msr, },
2099 { "xer", 0, &monitor_get_xer, },
2100 { "tbu", 0, &monitor_get_tbu, },
2101 { "tbl", 0, &monitor_get_tbl, },
2102 #if defined(TARGET_PPC64)
2103 /* Address space register */
2104 { "asr", offsetof(CPUState, asr) },
2105 #endif
2106 /* Segment registers */
2107 { "sdr1", offsetof(CPUState, sdr1) },
2108 { "sr0", offsetof(CPUState, sr[0]) },
2109 { "sr1", offsetof(CPUState, sr[1]) },
2110 { "sr2", offsetof(CPUState, sr[2]) },
2111 { "sr3", offsetof(CPUState, sr[3]) },
2112 { "sr4", offsetof(CPUState, sr[4]) },
2113 { "sr5", offsetof(CPUState, sr[5]) },
2114 { "sr6", offsetof(CPUState, sr[6]) },
2115 { "sr7", offsetof(CPUState, sr[7]) },
2116 { "sr8", offsetof(CPUState, sr[8]) },
2117 { "sr9", offsetof(CPUState, sr[9]) },
2118 { "sr10", offsetof(CPUState, sr[10]) },
2119 { "sr11", offsetof(CPUState, sr[11]) },
2120 { "sr12", offsetof(CPUState, sr[12]) },
2121 { "sr13", offsetof(CPUState, sr[13]) },
2122 { "sr14", offsetof(CPUState, sr[14]) },
2123 { "sr15", offsetof(CPUState, sr[15]) },
2124 /* Too lazy to put BATs and SPRs ... */
2125 #elif defined(TARGET_SPARC)
2126 { "g0", offsetof(CPUState, gregs[0]) },
2127 { "g1", offsetof(CPUState, gregs[1]) },
2128 { "g2", offsetof(CPUState, gregs[2]) },
2129 { "g3", offsetof(CPUState, gregs[3]) },
2130 { "g4", offsetof(CPUState, gregs[4]) },
2131 { "g5", offsetof(CPUState, gregs[5]) },
2132 { "g6", offsetof(CPUState, gregs[6]) },
2133 { "g7", offsetof(CPUState, gregs[7]) },
2134 { "o0", 0, monitor_get_reg },
2135 { "o1", 1, monitor_get_reg },
2136 { "o2", 2, monitor_get_reg },
2137 { "o3", 3, monitor_get_reg },
2138 { "o4", 4, monitor_get_reg },
2139 { "o5", 5, monitor_get_reg },
2140 { "o6", 6, monitor_get_reg },
2141 { "o7", 7, monitor_get_reg },
2142 { "l0", 8, monitor_get_reg },
2143 { "l1", 9, monitor_get_reg },
2144 { "l2", 10, monitor_get_reg },
2145 { "l3", 11, monitor_get_reg },
2146 { "l4", 12, monitor_get_reg },
2147 { "l5", 13, monitor_get_reg },
2148 { "l6", 14, monitor_get_reg },
2149 { "l7", 15, monitor_get_reg },
2150 { "i0", 16, monitor_get_reg },
2151 { "i1", 17, monitor_get_reg },
2152 { "i2", 18, monitor_get_reg },
2153 { "i3", 19, monitor_get_reg },
2154 { "i4", 20, monitor_get_reg },
2155 { "i5", 21, monitor_get_reg },
2156 { "i6", 22, monitor_get_reg },
2157 { "i7", 23, monitor_get_reg },
2158 { "pc", offsetof(CPUState, pc) },
2159 { "npc", offsetof(CPUState, npc) },
2160 { "y", offsetof(CPUState, y) },
2161 #ifndef TARGET_SPARC64
2162 { "psr", 0, &monitor_get_psr, },
2163 { "wim", offsetof(CPUState, wim) },
2164 #endif
2165 { "tbr", offsetof(CPUState, tbr) },
2166 { "fsr", offsetof(CPUState, fsr) },
2167 { "f0", offsetof(CPUState, fpr[0]) },
2168 { "f1", offsetof(CPUState, fpr[1]) },
2169 { "f2", offsetof(CPUState, fpr[2]) },
2170 { "f3", offsetof(CPUState, fpr[3]) },
2171 { "f4", offsetof(CPUState, fpr[4]) },
2172 { "f5", offsetof(CPUState, fpr[5]) },
2173 { "f6", offsetof(CPUState, fpr[6]) },
2174 { "f7", offsetof(CPUState, fpr[7]) },
2175 { "f8", offsetof(CPUState, fpr[8]) },
2176 { "f9", offsetof(CPUState, fpr[9]) },
2177 { "f10", offsetof(CPUState, fpr[10]) },
2178 { "f11", offsetof(CPUState, fpr[11]) },
2179 { "f12", offsetof(CPUState, fpr[12]) },
2180 { "f13", offsetof(CPUState, fpr[13]) },
2181 { "f14", offsetof(CPUState, fpr[14]) },
2182 { "f15", offsetof(CPUState, fpr[15]) },
2183 { "f16", offsetof(CPUState, fpr[16]) },
2184 { "f17", offsetof(CPUState, fpr[17]) },
2185 { "f18", offsetof(CPUState, fpr[18]) },
2186 { "f19", offsetof(CPUState, fpr[19]) },
2187 { "f20", offsetof(CPUState, fpr[20]) },
2188 { "f21", offsetof(CPUState, fpr[21]) },
2189 { "f22", offsetof(CPUState, fpr[22]) },
2190 { "f23", offsetof(CPUState, fpr[23]) },
2191 { "f24", offsetof(CPUState, fpr[24]) },
2192 { "f25", offsetof(CPUState, fpr[25]) },
2193 { "f26", offsetof(CPUState, fpr[26]) },
2194 { "f27", offsetof(CPUState, fpr[27]) },
2195 { "f28", offsetof(CPUState, fpr[28]) },
2196 { "f29", offsetof(CPUState, fpr[29]) },
2197 { "f30", offsetof(CPUState, fpr[30]) },
2198 { "f31", offsetof(CPUState, fpr[31]) },
2199 #ifdef TARGET_SPARC64
2200 { "f32", offsetof(CPUState, fpr[32]) },
2201 { "f34", offsetof(CPUState, fpr[34]) },
2202 { "f36", offsetof(CPUState, fpr[36]) },
2203 { "f38", offsetof(CPUState, fpr[38]) },
2204 { "f40", offsetof(CPUState, fpr[40]) },
2205 { "f42", offsetof(CPUState, fpr[42]) },
2206 { "f44", offsetof(CPUState, fpr[44]) },
2207 { "f46", offsetof(CPUState, fpr[46]) },
2208 { "f48", offsetof(CPUState, fpr[48]) },
2209 { "f50", offsetof(CPUState, fpr[50]) },
2210 { "f52", offsetof(CPUState, fpr[52]) },
2211 { "f54", offsetof(CPUState, fpr[54]) },
2212 { "f56", offsetof(CPUState, fpr[56]) },
2213 { "f58", offsetof(CPUState, fpr[58]) },
2214 { "f60", offsetof(CPUState, fpr[60]) },
2215 { "f62", offsetof(CPUState, fpr[62]) },
2216 { "asi", offsetof(CPUState, asi) },
2217 { "pstate", offsetof(CPUState, pstate) },
2218 { "cansave", offsetof(CPUState, cansave) },
2219 { "canrestore", offsetof(CPUState, canrestore) },
2220 { "otherwin", offsetof(CPUState, otherwin) },
2221 { "wstate", offsetof(CPUState, wstate) },
2222 { "cleanwin", offsetof(CPUState, cleanwin) },
2223 { "fprs", offsetof(CPUState, fprs) },
2224 #endif
2225 #endif
2226 { NULL },
2229 static void expr_error(Monitor *mon, const char *msg)
2231 monitor_printf(mon, "%s\n", msg);
2232 longjmp(expr_env, 1);
2235 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2236 static int get_monitor_def(target_long *pval, const char *name)
2238 const MonitorDef *md;
2239 void *ptr;
2241 for(md = monitor_defs; md->name != NULL; md++) {
2242 if (compare_cmd(name, md->name)) {
2243 if (md->get_value) {
2244 *pval = md->get_value(md, md->offset);
2245 } else {
2246 CPUState *env = mon_get_cpu();
2247 if (!env)
2248 return -2;
2249 ptr = (uint8_t *)env + md->offset;
2250 switch(md->type) {
2251 case MD_I32:
2252 *pval = *(int32_t *)ptr;
2253 break;
2254 case MD_TLONG:
2255 *pval = *(target_long *)ptr;
2256 break;
2257 default:
2258 *pval = 0;
2259 break;
2262 return 0;
2265 return -1;
2268 static void next(void)
2270 if (pch != '\0') {
2271 pch++;
2272 while (qemu_isspace(*pch))
2273 pch++;
2277 static int64_t expr_sum(Monitor *mon);
2279 static int64_t expr_unary(Monitor *mon)
2281 int64_t n;
2282 char *p;
2283 int ret;
2285 switch(*pch) {
2286 case '+':
2287 next();
2288 n = expr_unary(mon);
2289 break;
2290 case '-':
2291 next();
2292 n = -expr_unary(mon);
2293 break;
2294 case '~':
2295 next();
2296 n = ~expr_unary(mon);
2297 break;
2298 case '(':
2299 next();
2300 n = expr_sum(mon);
2301 if (*pch != ')') {
2302 expr_error(mon, "')' expected");
2304 next();
2305 break;
2306 case '\'':
2307 pch++;
2308 if (*pch == '\0')
2309 expr_error(mon, "character constant expected");
2310 n = *pch;
2311 pch++;
2312 if (*pch != '\'')
2313 expr_error(mon, "missing terminating \' character");
2314 next();
2315 break;
2316 case '$':
2318 char buf[128], *q;
2319 target_long reg=0;
2321 pch++;
2322 q = buf;
2323 while ((*pch >= 'a' && *pch <= 'z') ||
2324 (*pch >= 'A' && *pch <= 'Z') ||
2325 (*pch >= '0' && *pch <= '9') ||
2326 *pch == '_' || *pch == '.') {
2327 if ((q - buf) < sizeof(buf) - 1)
2328 *q++ = *pch;
2329 pch++;
2331 while (qemu_isspace(*pch))
2332 pch++;
2333 *q = 0;
2334 ret = get_monitor_def(&reg, buf);
2335 if (ret == -1)
2336 expr_error(mon, "unknown register");
2337 else if (ret == -2)
2338 expr_error(mon, "no cpu defined");
2339 n = reg;
2341 break;
2342 case '\0':
2343 expr_error(mon, "unexpected end of expression");
2344 n = 0;
2345 break;
2346 default:
2347 #if TARGET_PHYS_ADDR_BITS > 32
2348 n = strtoull(pch, &p, 0);
2349 #else
2350 n = strtoul(pch, &p, 0);
2351 #endif
2352 if (pch == p) {
2353 expr_error(mon, "invalid char in expression");
2355 pch = p;
2356 while (qemu_isspace(*pch))
2357 pch++;
2358 break;
2360 return n;
2364 static int64_t expr_prod(Monitor *mon)
2366 int64_t val, val2;
2367 int op;
2369 val = expr_unary(mon);
2370 for(;;) {
2371 op = *pch;
2372 if (op != '*' && op != '/' && op != '%')
2373 break;
2374 next();
2375 val2 = expr_unary(mon);
2376 switch(op) {
2377 default:
2378 case '*':
2379 val *= val2;
2380 break;
2381 case '/':
2382 case '%':
2383 if (val2 == 0)
2384 expr_error(mon, "division by zero");
2385 if (op == '/')
2386 val /= val2;
2387 else
2388 val %= val2;
2389 break;
2392 return val;
2395 static int64_t expr_logic(Monitor *mon)
2397 int64_t val, val2;
2398 int op;
2400 val = expr_prod(mon);
2401 for(;;) {
2402 op = *pch;
2403 if (op != '&' && op != '|' && op != '^')
2404 break;
2405 next();
2406 val2 = expr_prod(mon);
2407 switch(op) {
2408 default:
2409 case '&':
2410 val &= val2;
2411 break;
2412 case '|':
2413 val |= val2;
2414 break;
2415 case '^':
2416 val ^= val2;
2417 break;
2420 return val;
2423 static int64_t expr_sum(Monitor *mon)
2425 int64_t val, val2;
2426 int op;
2428 val = expr_logic(mon);
2429 for(;;) {
2430 op = *pch;
2431 if (op != '+' && op != '-')
2432 break;
2433 next();
2434 val2 = expr_logic(mon);
2435 if (op == '+')
2436 val += val2;
2437 else
2438 val -= val2;
2440 return val;
2443 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2445 pch = *pp;
2446 if (setjmp(expr_env)) {
2447 *pp = pch;
2448 return -1;
2450 while (qemu_isspace(*pch))
2451 pch++;
2452 *pval = expr_sum(mon);
2453 *pp = pch;
2454 return 0;
2457 static int get_str(char *buf, int buf_size, const char **pp)
2459 const char *p;
2460 char *q;
2461 int c;
2463 q = buf;
2464 p = *pp;
2465 while (qemu_isspace(*p))
2466 p++;
2467 if (*p == '\0') {
2468 fail:
2469 *q = '\0';
2470 *pp = p;
2471 return -1;
2473 if (*p == '\"') {
2474 p++;
2475 while (*p != '\0' && *p != '\"') {
2476 if (*p == '\\') {
2477 p++;
2478 c = *p++;
2479 switch(c) {
2480 case 'n':
2481 c = '\n';
2482 break;
2483 case 'r':
2484 c = '\r';
2485 break;
2486 case '\\':
2487 case '\'':
2488 case '\"':
2489 break;
2490 default:
2491 qemu_printf("unsupported escape code: '\\%c'\n", c);
2492 goto fail;
2494 if ((q - buf) < buf_size - 1) {
2495 *q++ = c;
2497 } else {
2498 if ((q - buf) < buf_size - 1) {
2499 *q++ = *p;
2501 p++;
2504 if (*p != '\"') {
2505 qemu_printf("unterminated string\n");
2506 goto fail;
2508 p++;
2509 } else {
2510 while (*p != '\0' && !qemu_isspace(*p)) {
2511 if ((q - buf) < buf_size - 1) {
2512 *q++ = *p;
2514 p++;
2517 *q = '\0';
2518 *pp = p;
2519 return 0;
2522 static int default_fmt_format = 'x';
2523 static int default_fmt_size = 4;
2525 #define MAX_ARGS 16
2527 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2529 const char *p, *pstart, *typestr;
2530 char *q;
2531 int c, nb_args, len, i, has_arg;
2532 const mon_cmd_t *cmd;
2533 char cmdname[256];
2534 char buf[1024];
2535 void *str_allocated[MAX_ARGS];
2536 void *args[MAX_ARGS];
2537 void (*handler_0)(Monitor *mon);
2538 void (*handler_1)(Monitor *mon, void *arg0);
2539 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2540 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2541 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2542 void *arg3);
2543 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2544 void *arg3, void *arg4);
2545 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2546 void *arg3, void *arg4, void *arg5);
2547 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2548 void *arg3, void *arg4, void *arg5, void *arg6);
2550 #ifdef DEBUG
2551 monitor_printf(mon, "command='%s'\n", cmdline);
2552 #endif
2554 /* extract the command name */
2555 p = cmdline;
2556 q = cmdname;
2557 while (qemu_isspace(*p))
2558 p++;
2559 if (*p == '\0')
2560 return;
2561 pstart = p;
2562 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2563 p++;
2564 len = p - pstart;
2565 if (len > sizeof(cmdname) - 1)
2566 len = sizeof(cmdname) - 1;
2567 memcpy(cmdname, pstart, len);
2568 cmdname[len] = '\0';
2570 /* find the command */
2571 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2572 if (compare_cmd(cmdname, cmd->name))
2573 goto found;
2575 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2576 return;
2577 found:
2579 for(i = 0; i < MAX_ARGS; i++)
2580 str_allocated[i] = NULL;
2582 /* parse the parameters */
2583 typestr = cmd->args_type;
2584 nb_args = 0;
2585 for(;;) {
2586 c = *typestr;
2587 if (c == '\0')
2588 break;
2589 typestr++;
2590 switch(c) {
2591 case 'F':
2592 case 'B':
2593 case 's':
2595 int ret;
2596 char *str;
2598 while (qemu_isspace(*p))
2599 p++;
2600 if (*typestr == '?') {
2601 typestr++;
2602 if (*p == '\0') {
2603 /* no optional string: NULL argument */
2604 str = NULL;
2605 goto add_str;
2608 ret = get_str(buf, sizeof(buf), &p);
2609 if (ret < 0) {
2610 switch(c) {
2611 case 'F':
2612 monitor_printf(mon, "%s: filename expected\n",
2613 cmdname);
2614 break;
2615 case 'B':
2616 monitor_printf(mon, "%s: block device name expected\n",
2617 cmdname);
2618 break;
2619 default:
2620 monitor_printf(mon, "%s: string expected\n", cmdname);
2621 break;
2623 goto fail;
2625 str = qemu_malloc(strlen(buf) + 1);
2626 pstrcpy(str, sizeof(buf), buf);
2627 str_allocated[nb_args] = str;
2628 add_str:
2629 if (nb_args >= MAX_ARGS) {
2630 error_args:
2631 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2632 goto fail;
2634 args[nb_args++] = str;
2636 break;
2637 case '/':
2639 int count, format, size;
2641 while (qemu_isspace(*p))
2642 p++;
2643 if (*p == '/') {
2644 /* format found */
2645 p++;
2646 count = 1;
2647 if (qemu_isdigit(*p)) {
2648 count = 0;
2649 while (qemu_isdigit(*p)) {
2650 count = count * 10 + (*p - '0');
2651 p++;
2654 size = -1;
2655 format = -1;
2656 for(;;) {
2657 switch(*p) {
2658 case 'o':
2659 case 'd':
2660 case 'u':
2661 case 'x':
2662 case 'i':
2663 case 'c':
2664 format = *p++;
2665 break;
2666 case 'b':
2667 size = 1;
2668 p++;
2669 break;
2670 case 'h':
2671 size = 2;
2672 p++;
2673 break;
2674 case 'w':
2675 size = 4;
2676 p++;
2677 break;
2678 case 'g':
2679 case 'L':
2680 size = 8;
2681 p++;
2682 break;
2683 default:
2684 goto next;
2687 next:
2688 if (*p != '\0' && !qemu_isspace(*p)) {
2689 monitor_printf(mon, "invalid char in format: '%c'\n",
2690 *p);
2691 goto fail;
2693 if (format < 0)
2694 format = default_fmt_format;
2695 if (format != 'i') {
2696 /* for 'i', not specifying a size gives -1 as size */
2697 if (size < 0)
2698 size = default_fmt_size;
2699 default_fmt_size = size;
2701 default_fmt_format = format;
2702 } else {
2703 count = 1;
2704 format = default_fmt_format;
2705 if (format != 'i') {
2706 size = default_fmt_size;
2707 } else {
2708 size = -1;
2711 if (nb_args + 3 > MAX_ARGS)
2712 goto error_args;
2713 args[nb_args++] = (void*)(long)count;
2714 args[nb_args++] = (void*)(long)format;
2715 args[nb_args++] = (void*)(long)size;
2717 break;
2718 case 'i':
2719 case 'l':
2721 int64_t val;
2723 while (qemu_isspace(*p))
2724 p++;
2725 if (*typestr == '?' || *typestr == '.') {
2726 if (*typestr == '?') {
2727 if (*p == '\0')
2728 has_arg = 0;
2729 else
2730 has_arg = 1;
2731 } else {
2732 if (*p == '.') {
2733 p++;
2734 while (qemu_isspace(*p))
2735 p++;
2736 has_arg = 1;
2737 } else {
2738 has_arg = 0;
2741 typestr++;
2742 if (nb_args >= MAX_ARGS)
2743 goto error_args;
2744 args[nb_args++] = (void *)(long)has_arg;
2745 if (!has_arg) {
2746 if (nb_args >= MAX_ARGS)
2747 goto error_args;
2748 val = -1;
2749 goto add_num;
2752 if (get_expr(mon, &val, &p))
2753 goto fail;
2754 add_num:
2755 if (c == 'i') {
2756 if (nb_args >= MAX_ARGS)
2757 goto error_args;
2758 args[nb_args++] = (void *)(long)val;
2759 } else {
2760 if ((nb_args + 1) >= MAX_ARGS)
2761 goto error_args;
2762 #if TARGET_PHYS_ADDR_BITS > 32
2763 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2764 #else
2765 args[nb_args++] = (void *)0;
2766 #endif
2767 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2770 break;
2771 case '-':
2773 int has_option;
2774 /* option */
2776 c = *typestr++;
2777 if (c == '\0')
2778 goto bad_type;
2779 while (qemu_isspace(*p))
2780 p++;
2781 has_option = 0;
2782 if (*p == '-') {
2783 p++;
2784 if (*p != c) {
2785 monitor_printf(mon, "%s: unsupported option -%c\n",
2786 cmdname, *p);
2787 goto fail;
2789 p++;
2790 has_option = 1;
2792 if (nb_args >= MAX_ARGS)
2793 goto error_args;
2794 args[nb_args++] = (void *)(long)has_option;
2796 break;
2797 default:
2798 bad_type:
2799 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2800 goto fail;
2803 /* check that all arguments were parsed */
2804 while (qemu_isspace(*p))
2805 p++;
2806 if (*p != '\0') {
2807 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2808 cmdname);
2809 goto fail;
2812 switch(nb_args) {
2813 case 0:
2814 handler_0 = cmd->handler;
2815 handler_0(mon);
2816 break;
2817 case 1:
2818 handler_1 = cmd->handler;
2819 handler_1(mon, args[0]);
2820 break;
2821 case 2:
2822 handler_2 = cmd->handler;
2823 handler_2(mon, args[0], args[1]);
2824 break;
2825 case 3:
2826 handler_3 = cmd->handler;
2827 handler_3(mon, args[0], args[1], args[2]);
2828 break;
2829 case 4:
2830 handler_4 = cmd->handler;
2831 handler_4(mon, args[0], args[1], args[2], args[3]);
2832 break;
2833 case 5:
2834 handler_5 = cmd->handler;
2835 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2836 break;
2837 case 6:
2838 handler_6 = cmd->handler;
2839 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2840 break;
2841 case 7:
2842 handler_7 = cmd->handler;
2843 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2844 args[6]);
2845 break;
2846 default:
2847 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2848 goto fail;
2850 fail:
2851 for(i = 0; i < MAX_ARGS; i++)
2852 qemu_free(str_allocated[i]);
2853 return;
2856 static void cmd_completion(const char *name, const char *list)
2858 const char *p, *pstart;
2859 char cmd[128];
2860 int len;
2862 p = list;
2863 for(;;) {
2864 pstart = p;
2865 p = strchr(p, '|');
2866 if (!p)
2867 p = pstart + strlen(pstart);
2868 len = p - pstart;
2869 if (len > sizeof(cmd) - 2)
2870 len = sizeof(cmd) - 2;
2871 memcpy(cmd, pstart, len);
2872 cmd[len] = '\0';
2873 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2874 readline_add_completion(cur_mon->rs, cmd);
2876 if (*p == '\0')
2877 break;
2878 p++;
2882 static void file_completion(const char *input)
2884 DIR *ffs;
2885 struct dirent *d;
2886 char path[1024];
2887 char file[1024], file_prefix[1024];
2888 int input_path_len;
2889 const char *p;
2891 p = strrchr(input, '/');
2892 if (!p) {
2893 input_path_len = 0;
2894 pstrcpy(file_prefix, sizeof(file_prefix), input);
2895 pstrcpy(path, sizeof(path), ".");
2896 } else {
2897 input_path_len = p - input + 1;
2898 memcpy(path, input, input_path_len);
2899 if (input_path_len > sizeof(path) - 1)
2900 input_path_len = sizeof(path) - 1;
2901 path[input_path_len] = '\0';
2902 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2904 #ifdef DEBUG_COMPLETION
2905 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2906 input, path, file_prefix);
2907 #endif
2908 ffs = opendir(path);
2909 if (!ffs)
2910 return;
2911 for(;;) {
2912 struct stat sb;
2913 d = readdir(ffs);
2914 if (!d)
2915 break;
2916 if (strstart(d->d_name, file_prefix, NULL)) {
2917 memcpy(file, input, input_path_len);
2918 if (input_path_len < sizeof(file))
2919 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2920 d->d_name);
2921 /* stat the file to find out if it's a directory.
2922 * In that case add a slash to speed up typing long paths
2924 stat(file, &sb);
2925 if(S_ISDIR(sb.st_mode))
2926 pstrcat(file, sizeof(file), "/");
2927 readline_add_completion(cur_mon->rs, file);
2930 closedir(ffs);
2933 static void block_completion_it(void *opaque, BlockDriverState *bs)
2935 const char *name = bdrv_get_device_name(bs);
2936 const char *input = opaque;
2938 if (input[0] == '\0' ||
2939 !strncmp(name, (char *)input, strlen(input))) {
2940 readline_add_completion(cur_mon->rs, name);
2944 /* NOTE: this parser is an approximate form of the real command parser */
2945 static void parse_cmdline(const char *cmdline,
2946 int *pnb_args, char **args)
2948 const char *p;
2949 int nb_args, ret;
2950 char buf[1024];
2952 p = cmdline;
2953 nb_args = 0;
2954 for(;;) {
2955 while (qemu_isspace(*p))
2956 p++;
2957 if (*p == '\0')
2958 break;
2959 if (nb_args >= MAX_ARGS)
2960 break;
2961 ret = get_str(buf, sizeof(buf), &p);
2962 args[nb_args] = qemu_strdup(buf);
2963 nb_args++;
2964 if (ret < 0)
2965 break;
2967 *pnb_args = nb_args;
2970 static void monitor_find_completion(const char *cmdline)
2972 const char *cmdname;
2973 char *args[MAX_ARGS];
2974 int nb_args, i, len;
2975 const char *ptype, *str;
2976 const mon_cmd_t *cmd;
2977 const KeyDef *key;
2979 parse_cmdline(cmdline, &nb_args, args);
2980 #ifdef DEBUG_COMPLETION
2981 for(i = 0; i < nb_args; i++) {
2982 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2984 #endif
2986 /* if the line ends with a space, it means we want to complete the
2987 next arg */
2988 len = strlen(cmdline);
2989 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2990 if (nb_args >= MAX_ARGS)
2991 return;
2992 args[nb_args++] = qemu_strdup("");
2994 if (nb_args <= 1) {
2995 /* command completion */
2996 if (nb_args == 0)
2997 cmdname = "";
2998 else
2999 cmdname = args[0];
3000 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3001 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3002 cmd_completion(cmdname, cmd->name);
3004 } else {
3005 /* find the command */
3006 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3007 if (compare_cmd(args[0], cmd->name))
3008 goto found;
3010 return;
3011 found:
3012 ptype = cmd->args_type;
3013 for(i = 0; i < nb_args - 2; i++) {
3014 if (*ptype != '\0') {
3015 ptype++;
3016 while (*ptype == '?')
3017 ptype++;
3020 str = args[nb_args - 1];
3021 switch(*ptype) {
3022 case 'F':
3023 /* file completion */
3024 readline_set_completion_index(cur_mon->rs, strlen(str));
3025 file_completion(str);
3026 break;
3027 case 'B':
3028 /* block device name completion */
3029 readline_set_completion_index(cur_mon->rs, strlen(str));
3030 bdrv_iterate(block_completion_it, (void *)str);
3031 break;
3032 case 's':
3033 /* XXX: more generic ? */
3034 if (!strcmp(cmd->name, "info")) {
3035 readline_set_completion_index(cur_mon->rs, strlen(str));
3036 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3037 cmd_completion(str, cmd->name);
3039 } else if (!strcmp(cmd->name, "sendkey")) {
3040 char *sep = strrchr(str, '-');
3041 if (sep)
3042 str = sep + 1;
3043 readline_set_completion_index(cur_mon->rs, strlen(str));
3044 for(key = key_defs; key->name != NULL; key++) {
3045 cmd_completion(str, key->name);
3048 break;
3049 default:
3050 break;
3053 for(i = 0; i < nb_args; i++)
3054 qemu_free(args[i]);
3057 static int monitor_can_read(void *opaque)
3059 Monitor *mon = opaque;
3061 return (mon->suspend_cnt == 0) ? 128 : 0;
3064 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3066 Monitor *old_mon = cur_mon;
3067 int i;
3069 cur_mon = opaque;
3071 if (cur_mon->rs) {
3072 for (i = 0; i < size; i++)
3073 readline_handle_byte(cur_mon->rs, buf[i]);
3074 } else {
3075 if (size == 0 || buf[size - 1] != 0)
3076 monitor_printf(cur_mon, "corrupted command\n");
3077 else
3078 monitor_handle_command(cur_mon, (char *)buf);
3081 cur_mon = old_mon;
3084 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3086 monitor_suspend(mon);
3087 monitor_handle_command(mon, cmdline);
3088 monitor_resume(mon);
3091 int monitor_suspend(Monitor *mon)
3093 if (!mon->rs)
3094 return -ENOTTY;
3095 mon->suspend_cnt++;
3096 return 0;
3099 void monitor_resume(Monitor *mon)
3101 if (!mon->rs)
3102 return;
3103 if (--mon->suspend_cnt == 0)
3104 readline_show_prompt(mon->rs);
3107 static void monitor_event(void *opaque, int event)
3109 Monitor *mon = opaque;
3111 switch (event) {
3112 case CHR_EVENT_MUX_IN:
3113 readline_restart(mon->rs);
3114 monitor_resume(mon);
3115 monitor_flush(mon);
3116 break;
3118 case CHR_EVENT_MUX_OUT:
3119 if (mon->suspend_cnt == 0)
3120 monitor_printf(mon, "\n");
3121 monitor_flush(mon);
3122 monitor_suspend(mon);
3123 break;
3125 case CHR_EVENT_RESET:
3126 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3127 "information\n", QEMU_VERSION);
3128 if (mon->chr->focus == 0)
3129 readline_show_prompt(mon->rs);
3130 break;
3136 * Local variables:
3137 * c-indent-level: 4
3138 * c-basic-offset: 4
3139 * tab-width: 8
3140 * End:
3143 void monitor_init(CharDriverState *chr, int flags)
3145 static int is_first_init = 1;
3146 Monitor *mon;
3148 if (is_first_init) {
3149 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3150 is_first_init = 0;
3153 mon = qemu_mallocz(sizeof(*mon));
3155 mon->chr = chr;
3156 mon->flags = flags;
3157 if (mon->chr->focus != 0)
3158 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3159 if (flags & MONITOR_USE_READLINE) {
3160 mon->rs = readline_init(mon, monitor_find_completion);
3161 monitor_read_command(mon, 0);
3164 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3165 mon);
3167 LIST_INSERT_HEAD(&mon_list, mon, entry);
3168 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3169 cur_mon = mon;
3172 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3174 BlockDriverState *bs = opaque;
3175 int ret = 0;
3177 if (bdrv_set_key(bs, password) != 0) {
3178 monitor_printf(mon, "invalid password\n");
3179 ret = -EPERM;
3181 if (mon->password_completion_cb)
3182 mon->password_completion_cb(mon->password_opaque, ret);
3184 monitor_read_command(mon, 1);
3187 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3188 BlockDriverCompletionFunc *completion_cb,
3189 void *opaque)
3191 int err;
3193 if (!bdrv_key_required(bs)) {
3194 if (completion_cb)
3195 completion_cb(opaque, 0);
3196 return;
3199 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3200 bdrv_get_encrypted_filename(bs));
3202 mon->password_completion_cb = completion_cb;
3203 mon->password_opaque = opaque;
3205 err = monitor_read_password(mon, bdrv_password_cb, bs);
3207 if (err && completion_cb)
3208 completion_cb(opaque, err);