kvm: scripts: drop asm symlinks before generating release
[kvm-userspace.git] / qemu / monitor.c
blobb3ea22ac6ac5e3288e0faa111793daf0f89df93a
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <dirent.h>
25 #include "hw/hw.h"
26 #include "hw/usb.h"
27 #include "hw/pcmcia.h"
28 #include "hw/pc.h"
29 #include "hw/pci.h"
30 #include "gdbstub.h"
31 #include "net.h"
32 #include "qemu-char.h"
33 #include "sysemu.h"
34 #include "monitor.h"
35 #include "readline.h"
36 #include "console.h"
37 #include "block.h"
38 #include "audio/audio.h"
39 #include "disas.h"
40 #include "balloon.h"
41 #include "qemu-timer.h"
42 #include "migration.h"
43 #include "kvm.h"
44 #include "acl.h"
46 #include "qemu-kvm.h"
48 //#define DEBUG
49 //#define DEBUG_COMPLETION
52 * Supported types:
54 * 'F' filename
55 * 'B' block device name
56 * 's' string (accept optional quote)
57 * 'i' 32 bit integer
58 * 'l' target long (32 or 64 bit)
59 * '/' optional gdb-like print format (like "/10x")
61 * '?' optional type (for 'F', 's' and 'i')
65 typedef struct mon_cmd_t {
66 const char *name;
67 const char *args_type;
68 void *handler;
69 const char *params;
70 const char *help;
71 } mon_cmd_t;
73 struct Monitor {
74 CharDriverState *chr;
75 int flags;
76 int suspend_cnt;
77 uint8_t outbuf[1024];
78 int outbuf_index;
79 ReadLineState *rs;
80 CPUState *mon_cpu;
81 BlockDriverCompletionFunc *password_completion_cb;
82 void *password_opaque;
83 LIST_ENTRY(Monitor) entry;
86 static LIST_HEAD(mon_list, Monitor) mon_list;
88 static const mon_cmd_t mon_cmds[];
89 static const mon_cmd_t info_cmds[];
91 Monitor *cur_mon = NULL;
93 static void monitor_command_cb(Monitor *mon, const char *cmdline,
94 void *opaque);
96 static void monitor_read_command(Monitor *mon, int show_prompt)
98 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
99 if (show_prompt)
100 readline_show_prompt(mon->rs);
103 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
104 void *opaque)
106 if (mon->rs) {
107 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
108 /* prompt is printed on return from the command handler */
109 return 0;
110 } else {
111 monitor_printf(mon, "terminal does not support password prompting\n");
112 return -ENOTTY;
116 void monitor_flush(Monitor *mon)
118 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
119 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
120 mon->outbuf_index = 0;
124 /* flush at every end of line or if the buffer is full */
125 static void monitor_puts(Monitor *mon, const char *str)
127 char c;
129 if (!mon)
130 return;
132 for(;;) {
133 c = *str++;
134 if (c == '\0')
135 break;
136 if (c == '\n')
137 mon->outbuf[mon->outbuf_index++] = '\r';
138 mon->outbuf[mon->outbuf_index++] = c;
139 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
140 || c == '\n')
141 monitor_flush(mon);
145 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
147 char buf[4096];
148 vsnprintf(buf, sizeof(buf), fmt, ap);
149 monitor_puts(mon, buf);
152 void monitor_printf(Monitor *mon, const char *fmt, ...)
154 va_list ap;
155 va_start(ap, fmt);
156 monitor_vprintf(mon, fmt, ap);
157 va_end(ap);
160 void monitor_print_filename(Monitor *mon, const char *filename)
162 int i;
164 for (i = 0; filename[i]; i++) {
165 switch (filename[i]) {
166 case ' ':
167 case '"':
168 case '\\':
169 monitor_printf(mon, "\\%c", filename[i]);
170 break;
171 case '\t':
172 monitor_printf(mon, "\\t");
173 break;
174 case '\r':
175 monitor_printf(mon, "\\r");
176 break;
177 case '\n':
178 monitor_printf(mon, "\\n");
179 break;
180 default:
181 monitor_printf(mon, "%c", filename[i]);
182 break;
187 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
189 va_list ap;
190 va_start(ap, fmt);
191 monitor_vprintf((Monitor *)stream, fmt, ap);
192 va_end(ap);
193 return 0;
196 static int compare_cmd(const char *name, const char *list)
198 const char *p, *pstart;
199 int len;
200 len = strlen(name);
201 p = list;
202 for(;;) {
203 pstart = p;
204 p = strchr(p, '|');
205 if (!p)
206 p = pstart + strlen(pstart);
207 if ((p - pstart) == len && !memcmp(pstart, name, len))
208 return 1;
209 if (*p == '\0')
210 break;
211 p++;
213 return 0;
216 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
217 const char *prefix, const char *name)
219 const mon_cmd_t *cmd;
221 for(cmd = cmds; cmd->name != NULL; cmd++) {
222 if (!name || !strcmp(name, cmd->name))
223 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
224 cmd->params, cmd->help);
228 static void help_cmd(Monitor *mon, const char *name)
230 if (name && !strcmp(name, "info")) {
231 help_cmd_dump(mon, info_cmds, "info ", NULL);
232 } else {
233 help_cmd_dump(mon, mon_cmds, "", name);
234 if (name && !strcmp(name, "log")) {
235 const CPULogItem *item;
236 monitor_printf(mon, "Log items (comma separated):\n");
237 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
238 for(item = cpu_log_items; item->mask != 0; item++) {
239 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
245 static void do_commit(Monitor *mon, const char *device)
247 int i, all_devices;
249 all_devices = !strcmp(device, "all");
250 for (i = 0; i < nb_drives; i++) {
251 if (all_devices ||
252 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
253 bdrv_commit(drives_table[i].bdrv);
257 static void do_info(Monitor *mon, const char *item)
259 const mon_cmd_t *cmd;
260 void (*handler)(Monitor *);
262 if (!item)
263 goto help;
264 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
265 if (compare_cmd(item, cmd->name))
266 goto found;
268 help:
269 help_cmd(mon, "info");
270 return;
271 found:
272 handler = cmd->handler;
273 handler(mon);
276 static void do_info_version(Monitor *mon)
278 monitor_printf(mon, "%s\n", QEMU_VERSION);
281 static void do_info_name(Monitor *mon)
283 if (qemu_name)
284 monitor_printf(mon, "%s\n", qemu_name);
287 #if defined(TARGET_I386)
288 static void do_info_hpet(Monitor *mon)
290 monitor_printf(mon, "HPET is %s by QEMU\n",
291 (no_hpet) ? "disabled" : "enabled");
293 #endif
295 static void do_info_uuid(Monitor *mon)
297 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
298 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
299 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
300 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
301 qemu_uuid[14], qemu_uuid[15]);
304 /* get the current CPU defined by the user */
305 static int mon_set_cpu(int cpu_index)
307 CPUState *env;
309 for(env = first_cpu; env != NULL; env = env->next_cpu) {
310 if (env->cpu_index == cpu_index) {
311 cur_mon->mon_cpu = env;
312 return 0;
315 return -1;
318 static CPUState *mon_get_cpu(void)
320 if (!cur_mon->mon_cpu) {
321 mon_set_cpu(0);
323 cpu_synchronize_state(cur_mon->mon_cpu, 0);
324 return cur_mon->mon_cpu;
327 static void do_info_registers(Monitor *mon)
329 CPUState *env;
330 env = mon_get_cpu();
331 if (!env)
332 return;
333 #ifdef TARGET_I386
334 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
335 X86_DUMP_FPU);
336 #else
337 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
339 #endif
342 static void do_info_cpus(Monitor *mon)
344 CPUState *env;
346 /* just to set the default cpu if not already done */
347 mon_get_cpu();
349 for(env = first_cpu; env != NULL; env = env->next_cpu) {
350 cpu_synchronize_state(env, 0);
351 monitor_printf(mon, "%c CPU #%d:",
352 (env == mon->mon_cpu) ? '*' : ' ',
353 env->cpu_index);
354 #if defined(TARGET_I386)
355 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
356 env->eip + env->segs[R_CS].base);
357 #elif defined(TARGET_PPC)
358 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
359 #elif defined(TARGET_SPARC)
360 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
361 env->pc, env->npc);
362 #elif defined(TARGET_MIPS)
363 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
364 #endif
365 if (env->halted)
366 monitor_printf(mon, " (halted)");
367 monitor_printf(mon," thread_id=%d", env->thread_id);
368 monitor_printf(mon, "\n");
372 static void do_cpu_set(Monitor *mon, int index)
374 if (mon_set_cpu(index) < 0)
375 monitor_printf(mon, "Invalid CPU index\n");
378 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
380 int state;
382 if (!strcmp(status, "online"))
383 state = 1;
384 else if (!strcmp(status, "offline"))
385 state = 0;
386 else {
387 monitor_printf(mon, "invalid status: %s\n", status);
388 return;
390 #if defined(TARGET_I386) || defined(TARGET_X86_64)
391 qemu_system_cpu_hot_add(value, state);
392 #endif
395 static void do_info_jit(Monitor *mon)
397 dump_exec_info((FILE *)mon, monitor_fprintf);
400 static void do_info_history(Monitor *mon)
402 int i;
403 const char *str;
405 if (!mon->rs)
406 return;
407 i = 0;
408 for(;;) {
409 str = readline_get_history(mon->rs, i);
410 if (!str)
411 break;
412 monitor_printf(mon, "%d: '%s'\n", i, str);
413 i++;
417 #if defined(TARGET_PPC)
418 /* XXX: not implemented in other targets */
419 static void do_info_cpu_stats(Monitor *mon)
421 CPUState *env;
423 env = mon_get_cpu();
424 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
426 #endif
428 static void do_quit(Monitor *mon)
430 exit(0);
433 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
435 if (bdrv_is_inserted(bs)) {
436 if (!force) {
437 if (!bdrv_is_removable(bs)) {
438 monitor_printf(mon, "device is not removable\n");
439 return -1;
441 if (bdrv_is_locked(bs)) {
442 monitor_printf(mon, "device is locked\n");
443 return -1;
446 bdrv_close(bs);
448 return 0;
451 static void do_eject(Monitor *mon, int force, const char *filename)
453 BlockDriverState *bs;
455 bs = bdrv_find(filename);
456 if (!bs) {
457 monitor_printf(mon, "device not found\n");
458 return;
460 eject_device(mon, bs, force);
463 static void do_change_block(Monitor *mon, const char *device,
464 const char *filename, const char *fmt)
466 BlockDriverState *bs;
467 BlockDriver *drv = NULL;
469 bs = bdrv_find(device);
470 if (!bs) {
471 monitor_printf(mon, "device not found\n");
472 return;
474 if (fmt) {
475 drv = bdrv_find_format(fmt);
476 if (!drv) {
477 monitor_printf(mon, "invalid format %s\n", fmt);
478 return;
481 if (eject_device(mon, bs, 0) < 0)
482 return;
483 bdrv_open2(bs, filename, 0, drv);
484 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
487 static void change_vnc_password_cb(Monitor *mon, const char *password,
488 void *opaque)
490 if (vnc_display_password(NULL, password) < 0)
491 monitor_printf(mon, "could not set VNC server password\n");
493 monitor_read_command(mon, 1);
496 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
498 if (strcmp(target, "passwd") == 0 ||
499 strcmp(target, "password") == 0) {
500 if (arg) {
501 char password[9];
502 strncpy(password, arg, sizeof(password));
503 password[sizeof(password) - 1] = '\0';
504 change_vnc_password_cb(mon, password, NULL);
505 } else {
506 monitor_read_password(mon, change_vnc_password_cb, NULL);
508 } else {
509 if (vnc_display_open(NULL, target) < 0)
510 monitor_printf(mon, "could not start VNC server on %s\n", target);
514 static void do_change(Monitor *mon, const char *device, const char *target,
515 const char *arg)
517 if (strcmp(device, "vnc") == 0) {
518 do_change_vnc(mon, target, arg);
519 } else {
520 do_change_block(mon, device, target, arg);
524 static void do_screen_dump(Monitor *mon, const char *filename)
526 vga_hw_screen_dump(filename);
529 static void do_logfile(Monitor *mon, const char *filename)
531 cpu_set_log_filename(filename);
534 static void do_log(Monitor *mon, const char *items)
536 int mask;
538 if (!strcmp(items, "none")) {
539 mask = 0;
540 } else {
541 mask = cpu_str_to_log_mask(items);
542 if (!mask) {
543 help_cmd(mon, "log");
544 return;
547 cpu_set_log(mask);
550 static void do_singlestep(Monitor *mon, const char *option)
552 if (!option || !strcmp(option, "on")) {
553 singlestep = 1;
554 } else if (!strcmp(option, "off")) {
555 singlestep = 0;
556 } else {
557 monitor_printf(mon, "unexpected option %s\n", option);
561 static void do_stop(Monitor *mon)
563 vm_stop(EXCP_INTERRUPT);
566 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
568 struct bdrv_iterate_context {
569 Monitor *mon;
570 int err;
573 static void do_cont(Monitor *mon)
575 struct bdrv_iterate_context context = { mon, 0 };
577 bdrv_iterate(encrypted_bdrv_it, &context);
578 /* only resume the vm if all keys are set and valid */
579 if (!context.err)
580 vm_start();
583 static void bdrv_key_cb(void *opaque, int err)
585 Monitor *mon = opaque;
587 /* another key was set successfully, retry to continue */
588 if (!err)
589 do_cont(mon);
592 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
594 struct bdrv_iterate_context *context = opaque;
596 if (!context->err && bdrv_key_required(bs)) {
597 context->err = -EBUSY;
598 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
599 context->mon);
603 #ifdef CONFIG_GDBSTUB
604 static void do_gdbserver(Monitor *mon, const char *device)
606 if (!device)
607 device = "tcp::" DEFAULT_GDBSTUB_PORT;
608 if (gdbserver_start(device) < 0) {
609 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
610 device);
611 } else if (strcmp(device, "none") == 0) {
612 monitor_printf(mon, "Disabled gdbserver\n");
613 } else {
614 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
615 device);
618 #endif
620 static void monitor_printc(Monitor *mon, int c)
622 monitor_printf(mon, "'");
623 switch(c) {
624 case '\'':
625 monitor_printf(mon, "\\'");
626 break;
627 case '\\':
628 monitor_printf(mon, "\\\\");
629 break;
630 case '\n':
631 monitor_printf(mon, "\\n");
632 break;
633 case '\r':
634 monitor_printf(mon, "\\r");
635 break;
636 default:
637 if (c >= 32 && c <= 126) {
638 monitor_printf(mon, "%c", c);
639 } else {
640 monitor_printf(mon, "\\x%02x", c);
642 break;
644 monitor_printf(mon, "'");
647 static void memory_dump(Monitor *mon, int count, int format, int wsize,
648 target_phys_addr_t addr, int is_physical)
650 CPUState *env;
651 int nb_per_line, l, line_size, i, max_digits, len;
652 uint8_t buf[16];
653 uint64_t v;
655 if (format == 'i') {
656 int flags;
657 flags = 0;
658 env = mon_get_cpu();
659 if (!env && !is_physical)
660 return;
661 #ifdef TARGET_I386
662 if (wsize == 2) {
663 flags = 1;
664 } else if (wsize == 4) {
665 flags = 0;
666 } else {
667 /* as default we use the current CS size */
668 flags = 0;
669 if (env) {
670 #ifdef TARGET_X86_64
671 if ((env->efer & MSR_EFER_LMA) &&
672 (env->segs[R_CS].flags & DESC_L_MASK))
673 flags = 2;
674 else
675 #endif
676 if (!(env->segs[R_CS].flags & DESC_B_MASK))
677 flags = 1;
680 #endif
681 monitor_disas(mon, env, addr, count, is_physical, flags);
682 return;
685 len = wsize * count;
686 if (wsize == 1)
687 line_size = 8;
688 else
689 line_size = 16;
690 nb_per_line = line_size / wsize;
691 max_digits = 0;
693 switch(format) {
694 case 'o':
695 max_digits = (wsize * 8 + 2) / 3;
696 break;
697 default:
698 case 'x':
699 max_digits = (wsize * 8) / 4;
700 break;
701 case 'u':
702 case 'd':
703 max_digits = (wsize * 8 * 10 + 32) / 33;
704 break;
705 case 'c':
706 wsize = 1;
707 break;
710 while (len > 0) {
711 if (is_physical)
712 monitor_printf(mon, TARGET_FMT_plx ":", addr);
713 else
714 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
715 l = len;
716 if (l > line_size)
717 l = line_size;
718 if (is_physical) {
719 cpu_physical_memory_rw(addr, buf, l, 0);
720 } else {
721 env = mon_get_cpu();
722 if (!env)
723 break;
724 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
725 monitor_printf(mon, " Cannot access memory\n");
726 break;
729 i = 0;
730 while (i < l) {
731 switch(wsize) {
732 default:
733 case 1:
734 v = ldub_raw(buf + i);
735 break;
736 case 2:
737 v = lduw_raw(buf + i);
738 break;
739 case 4:
740 v = (uint32_t)ldl_raw(buf + i);
741 break;
742 case 8:
743 v = ldq_raw(buf + i);
744 break;
746 monitor_printf(mon, " ");
747 switch(format) {
748 case 'o':
749 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
750 break;
751 case 'x':
752 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
753 break;
754 case 'u':
755 monitor_printf(mon, "%*" PRIu64, max_digits, v);
756 break;
757 case 'd':
758 monitor_printf(mon, "%*" PRId64, max_digits, v);
759 break;
760 case 'c':
761 monitor_printc(mon, v);
762 break;
764 i += wsize;
766 monitor_printf(mon, "\n");
767 addr += l;
768 len -= l;
772 #if TARGET_LONG_BITS == 64
773 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
774 #else
775 #define GET_TLONG(h, l) (l)
776 #endif
778 static void do_memory_dump(Monitor *mon, int count, int format, int size,
779 uint32_t addrh, uint32_t addrl)
781 target_long addr = GET_TLONG(addrh, addrl);
782 memory_dump(mon, count, format, size, addr, 0);
785 #if TARGET_PHYS_ADDR_BITS > 32
786 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
787 #else
788 #define GET_TPHYSADDR(h, l) (l)
789 #endif
791 static void do_physical_memory_dump(Monitor *mon, int count, int format,
792 int size, uint32_t addrh, uint32_t addrl)
795 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
796 memory_dump(mon, count, format, size, addr, 1);
799 static void do_print(Monitor *mon, int count, int format, int size,
800 unsigned int valh, unsigned int vall)
802 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
803 #if TARGET_PHYS_ADDR_BITS == 32
804 switch(format) {
805 case 'o':
806 monitor_printf(mon, "%#o", val);
807 break;
808 case 'x':
809 monitor_printf(mon, "%#x", val);
810 break;
811 case 'u':
812 monitor_printf(mon, "%u", val);
813 break;
814 default:
815 case 'd':
816 monitor_printf(mon, "%d", val);
817 break;
818 case 'c':
819 monitor_printc(mon, val);
820 break;
822 #else
823 switch(format) {
824 case 'o':
825 monitor_printf(mon, "%#" PRIo64, val);
826 break;
827 case 'x':
828 monitor_printf(mon, "%#" PRIx64, val);
829 break;
830 case 'u':
831 monitor_printf(mon, "%" PRIu64, val);
832 break;
833 default:
834 case 'd':
835 monitor_printf(mon, "%" PRId64, val);
836 break;
837 case 'c':
838 monitor_printc(mon, val);
839 break;
841 #endif
842 monitor_printf(mon, "\n");
845 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
846 uint32_t size, const char *filename)
848 FILE *f;
849 target_long addr = GET_TLONG(valh, vall);
850 uint32_t l;
851 CPUState *env;
852 uint8_t buf[1024];
854 env = mon_get_cpu();
855 if (!env)
856 return;
858 f = fopen(filename, "wb");
859 if (!f) {
860 monitor_printf(mon, "could not open '%s'\n", filename);
861 return;
863 while (size != 0) {
864 l = sizeof(buf);
865 if (l > size)
866 l = size;
867 cpu_memory_rw_debug(env, addr, buf, l, 0);
868 fwrite(buf, 1, l, f);
869 addr += l;
870 size -= l;
872 fclose(f);
875 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
876 unsigned int vall, uint32_t size,
877 const char *filename)
879 FILE *f;
880 uint32_t l;
881 uint8_t buf[1024];
882 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
884 f = fopen(filename, "wb");
885 if (!f) {
886 monitor_printf(mon, "could not open '%s'\n", filename);
887 return;
889 while (size != 0) {
890 l = sizeof(buf);
891 if (l > size)
892 l = size;
893 cpu_physical_memory_rw(addr, buf, l, 0);
894 fwrite(buf, 1, l, f);
895 fflush(f);
896 addr += l;
897 size -= l;
899 fclose(f);
902 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
904 uint32_t addr;
905 uint8_t buf[1];
906 uint16_t sum;
908 sum = 0;
909 for(addr = start; addr < (start + size); addr++) {
910 cpu_physical_memory_rw(addr, buf, 1, 0);
911 /* BSD sum algorithm ('sum' Unix command) */
912 sum = (sum >> 1) | (sum << 15);
913 sum += buf[0];
915 monitor_printf(mon, "%05d\n", sum);
918 typedef struct {
919 int keycode;
920 const char *name;
921 } KeyDef;
923 static const KeyDef key_defs[] = {
924 { 0x2a, "shift" },
925 { 0x36, "shift_r" },
927 { 0x38, "alt" },
928 { 0xb8, "alt_r" },
929 { 0x64, "altgr" },
930 { 0xe4, "altgr_r" },
931 { 0x1d, "ctrl" },
932 { 0x9d, "ctrl_r" },
934 { 0xdd, "menu" },
936 { 0x01, "esc" },
938 { 0x02, "1" },
939 { 0x03, "2" },
940 { 0x04, "3" },
941 { 0x05, "4" },
942 { 0x06, "5" },
943 { 0x07, "6" },
944 { 0x08, "7" },
945 { 0x09, "8" },
946 { 0x0a, "9" },
947 { 0x0b, "0" },
948 { 0x0c, "minus" },
949 { 0x0d, "equal" },
950 { 0x0e, "backspace" },
952 { 0x0f, "tab" },
953 { 0x10, "q" },
954 { 0x11, "w" },
955 { 0x12, "e" },
956 { 0x13, "r" },
957 { 0x14, "t" },
958 { 0x15, "y" },
959 { 0x16, "u" },
960 { 0x17, "i" },
961 { 0x18, "o" },
962 { 0x19, "p" },
964 { 0x1c, "ret" },
966 { 0x1e, "a" },
967 { 0x1f, "s" },
968 { 0x20, "d" },
969 { 0x21, "f" },
970 { 0x22, "g" },
971 { 0x23, "h" },
972 { 0x24, "j" },
973 { 0x25, "k" },
974 { 0x26, "l" },
976 { 0x2c, "z" },
977 { 0x2d, "x" },
978 { 0x2e, "c" },
979 { 0x2f, "v" },
980 { 0x30, "b" },
981 { 0x31, "n" },
982 { 0x32, "m" },
983 { 0x33, "comma" },
984 { 0x34, "dot" },
985 { 0x35, "slash" },
987 { 0x37, "asterisk" },
989 { 0x39, "spc" },
990 { 0x3a, "caps_lock" },
991 { 0x3b, "f1" },
992 { 0x3c, "f2" },
993 { 0x3d, "f3" },
994 { 0x3e, "f4" },
995 { 0x3f, "f5" },
996 { 0x40, "f6" },
997 { 0x41, "f7" },
998 { 0x42, "f8" },
999 { 0x43, "f9" },
1000 { 0x44, "f10" },
1001 { 0x45, "num_lock" },
1002 { 0x46, "scroll_lock" },
1004 { 0xb5, "kp_divide" },
1005 { 0x37, "kp_multiply" },
1006 { 0x4a, "kp_subtract" },
1007 { 0x4e, "kp_add" },
1008 { 0x9c, "kp_enter" },
1009 { 0x53, "kp_decimal" },
1010 { 0x54, "sysrq" },
1012 { 0x52, "kp_0" },
1013 { 0x4f, "kp_1" },
1014 { 0x50, "kp_2" },
1015 { 0x51, "kp_3" },
1016 { 0x4b, "kp_4" },
1017 { 0x4c, "kp_5" },
1018 { 0x4d, "kp_6" },
1019 { 0x47, "kp_7" },
1020 { 0x48, "kp_8" },
1021 { 0x49, "kp_9" },
1023 { 0x56, "<" },
1025 { 0x57, "f11" },
1026 { 0x58, "f12" },
1028 { 0xb7, "print" },
1030 { 0xc7, "home" },
1031 { 0xc9, "pgup" },
1032 { 0xd1, "pgdn" },
1033 { 0xcf, "end" },
1035 { 0xcb, "left" },
1036 { 0xc8, "up" },
1037 { 0xd0, "down" },
1038 { 0xcd, "right" },
1040 { 0xd2, "insert" },
1041 { 0xd3, "delete" },
1042 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1043 { 0xf0, "stop" },
1044 { 0xf1, "again" },
1045 { 0xf2, "props" },
1046 { 0xf3, "undo" },
1047 { 0xf4, "front" },
1048 { 0xf5, "copy" },
1049 { 0xf6, "open" },
1050 { 0xf7, "paste" },
1051 { 0xf8, "find" },
1052 { 0xf9, "cut" },
1053 { 0xfa, "lf" },
1054 { 0xfb, "help" },
1055 { 0xfc, "meta_l" },
1056 { 0xfd, "meta_r" },
1057 { 0xfe, "compose" },
1058 #endif
1059 { 0, NULL },
1062 static int get_keycode(const char *key)
1064 const KeyDef *p;
1065 char *endp;
1066 int ret;
1068 for(p = key_defs; p->name != NULL; p++) {
1069 if (!strcmp(key, p->name))
1070 return p->keycode;
1072 if (strstart(key, "0x", NULL)) {
1073 ret = strtoul(key, &endp, 0);
1074 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1075 return ret;
1077 return -1;
1080 #define MAX_KEYCODES 16
1081 static uint8_t keycodes[MAX_KEYCODES];
1082 static int nb_pending_keycodes;
1083 static QEMUTimer *key_timer;
1085 static void release_keys(void *opaque)
1087 int keycode;
1089 while (nb_pending_keycodes > 0) {
1090 nb_pending_keycodes--;
1091 keycode = keycodes[nb_pending_keycodes];
1092 if (keycode & 0x80)
1093 kbd_put_keycode(0xe0);
1094 kbd_put_keycode(keycode | 0x80);
1098 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1099 int hold_time)
1101 char keyname_buf[16];
1102 char *separator;
1103 int keyname_len, keycode, i;
1105 if (nb_pending_keycodes > 0) {
1106 qemu_del_timer(key_timer);
1107 release_keys(NULL);
1109 if (!has_hold_time)
1110 hold_time = 100;
1111 i = 0;
1112 while (1) {
1113 separator = strchr(string, '-');
1114 keyname_len = separator ? separator - string : strlen(string);
1115 if (keyname_len > 0) {
1116 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1117 if (keyname_len > sizeof(keyname_buf) - 1) {
1118 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1119 return;
1121 if (i == MAX_KEYCODES) {
1122 monitor_printf(mon, "too many keys\n");
1123 return;
1125 keyname_buf[keyname_len] = 0;
1126 keycode = get_keycode(keyname_buf);
1127 if (keycode < 0) {
1128 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1129 return;
1131 keycodes[i++] = keycode;
1133 if (!separator)
1134 break;
1135 string = separator + 1;
1137 nb_pending_keycodes = i;
1138 /* key down events */
1139 for (i = 0; i < nb_pending_keycodes; i++) {
1140 keycode = keycodes[i];
1141 if (keycode & 0x80)
1142 kbd_put_keycode(0xe0);
1143 kbd_put_keycode(keycode & 0x7f);
1145 /* delayed key up events */
1146 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1147 muldiv64(ticks_per_sec, hold_time, 1000));
1150 static int mouse_button_state;
1152 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1153 const char *dz_str)
1155 int dx, dy, dz;
1156 dx = strtol(dx_str, NULL, 0);
1157 dy = strtol(dy_str, NULL, 0);
1158 dz = 0;
1159 if (dz_str)
1160 dz = strtol(dz_str, NULL, 0);
1161 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1164 static void do_mouse_button(Monitor *mon, int button_state)
1166 mouse_button_state = button_state;
1167 kbd_mouse_event(0, 0, 0, mouse_button_state);
1170 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1171 int addr, int has_index, int index)
1173 uint32_t val;
1174 int suffix;
1176 if (has_index) {
1177 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1178 addr++;
1180 addr &= 0xffff;
1182 switch(size) {
1183 default:
1184 case 1:
1185 val = cpu_inb(NULL, addr);
1186 suffix = 'b';
1187 break;
1188 case 2:
1189 val = cpu_inw(NULL, addr);
1190 suffix = 'w';
1191 break;
1192 case 4:
1193 val = cpu_inl(NULL, addr);
1194 suffix = 'l';
1195 break;
1197 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1198 suffix, addr, size * 2, val);
1201 /* boot_set handler */
1202 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1203 static void *boot_opaque;
1205 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1207 qemu_boot_set_handler = func;
1208 boot_opaque = opaque;
1211 static void do_boot_set(Monitor *mon, const char *bootdevice)
1213 int res;
1215 if (qemu_boot_set_handler) {
1216 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1217 if (res == 0)
1218 monitor_printf(mon, "boot device list now set to %s\n",
1219 bootdevice);
1220 else
1221 monitor_printf(mon, "setting boot device list failed with "
1222 "error %i\n", res);
1223 } else {
1224 monitor_printf(mon, "no function defined to set boot device list for "
1225 "this architecture\n");
1229 static void do_system_reset(Monitor *mon)
1231 qemu_system_reset_request();
1234 static void do_system_powerdown(Monitor *mon)
1236 qemu_system_powerdown_request();
1239 #if defined(TARGET_I386)
1240 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1242 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1243 addr,
1244 pte & mask,
1245 pte & PG_GLOBAL_MASK ? 'G' : '-',
1246 pte & PG_PSE_MASK ? 'P' : '-',
1247 pte & PG_DIRTY_MASK ? 'D' : '-',
1248 pte & PG_ACCESSED_MASK ? 'A' : '-',
1249 pte & PG_PCD_MASK ? 'C' : '-',
1250 pte & PG_PWT_MASK ? 'T' : '-',
1251 pte & PG_USER_MASK ? 'U' : '-',
1252 pte & PG_RW_MASK ? 'W' : '-');
1255 static void tlb_info(Monitor *mon)
1257 CPUState *env;
1258 int l1, l2;
1259 uint32_t pgd, pde, pte;
1261 env = mon_get_cpu();
1262 if (!env)
1263 return;
1265 if (!(env->cr[0] & CR0_PG_MASK)) {
1266 monitor_printf(mon, "PG disabled\n");
1267 return;
1269 pgd = env->cr[3] & ~0xfff;
1270 for(l1 = 0; l1 < 1024; l1++) {
1271 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1272 pde = le32_to_cpu(pde);
1273 if (pde & PG_PRESENT_MASK) {
1274 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1275 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1276 } else {
1277 for(l2 = 0; l2 < 1024; l2++) {
1278 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1279 (uint8_t *)&pte, 4);
1280 pte = le32_to_cpu(pte);
1281 if (pte & PG_PRESENT_MASK) {
1282 print_pte(mon, (l1 << 22) + (l2 << 12),
1283 pte & ~PG_PSE_MASK,
1284 ~0xfff);
1292 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1293 uint32_t end, int prot)
1295 int prot1;
1296 prot1 = *plast_prot;
1297 if (prot != prot1) {
1298 if (*pstart != -1) {
1299 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1300 *pstart, end, end - *pstart,
1301 prot1 & PG_USER_MASK ? 'u' : '-',
1302 'r',
1303 prot1 & PG_RW_MASK ? 'w' : '-');
1305 if (prot != 0)
1306 *pstart = end;
1307 else
1308 *pstart = -1;
1309 *plast_prot = prot;
1313 static void mem_info(Monitor *mon)
1315 CPUState *env;
1316 int l1, l2, prot, last_prot;
1317 uint32_t pgd, pde, pte, start, end;
1319 env = mon_get_cpu();
1320 if (!env)
1321 return;
1323 if (!(env->cr[0] & CR0_PG_MASK)) {
1324 monitor_printf(mon, "PG disabled\n");
1325 return;
1327 pgd = env->cr[3] & ~0xfff;
1328 last_prot = 0;
1329 start = -1;
1330 for(l1 = 0; l1 < 1024; l1++) {
1331 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1332 pde = le32_to_cpu(pde);
1333 end = l1 << 22;
1334 if (pde & PG_PRESENT_MASK) {
1335 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1336 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1337 mem_print(mon, &start, &last_prot, end, prot);
1338 } else {
1339 for(l2 = 0; l2 < 1024; l2++) {
1340 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1341 (uint8_t *)&pte, 4);
1342 pte = le32_to_cpu(pte);
1343 end = (l1 << 22) + (l2 << 12);
1344 if (pte & PG_PRESENT_MASK) {
1345 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1346 } else {
1347 prot = 0;
1349 mem_print(mon, &start, &last_prot, end, prot);
1352 } else {
1353 prot = 0;
1354 mem_print(mon, &start, &last_prot, end, prot);
1358 #endif
1360 #if defined(TARGET_SH4)
1362 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1364 monitor_printf(mon, " tlb%i:\t"
1365 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1366 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1367 "dirty=%hhu writethrough=%hhu\n",
1368 idx,
1369 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1370 tlb->v, tlb->sh, tlb->c, tlb->pr,
1371 tlb->d, tlb->wt);
1374 static void tlb_info(Monitor *mon)
1376 CPUState *env = mon_get_cpu();
1377 int i;
1379 monitor_printf (mon, "ITLB:\n");
1380 for (i = 0 ; i < ITLB_SIZE ; i++)
1381 print_tlb (mon, i, &env->itlb[i]);
1382 monitor_printf (mon, "UTLB:\n");
1383 for (i = 0 ; i < UTLB_SIZE ; i++)
1384 print_tlb (mon, i, &env->utlb[i]);
1387 #endif
1389 static void do_info_kqemu(Monitor *mon)
1391 #ifdef USE_KQEMU
1392 CPUState *env;
1393 int val;
1394 val = 0;
1395 env = mon_get_cpu();
1396 if (!env) {
1397 monitor_printf(mon, "No cpu initialized yet");
1398 return;
1400 val = env->kqemu_enabled;
1401 monitor_printf(mon, "kqemu support: ");
1402 switch(val) {
1403 default:
1404 case 0:
1405 monitor_printf(mon, "disabled\n");
1406 break;
1407 case 1:
1408 monitor_printf(mon, "enabled for user code\n");
1409 break;
1410 case 2:
1411 monitor_printf(mon, "enabled for user and kernel code\n");
1412 break;
1414 #else
1415 monitor_printf(mon, "kqemu support: not compiled\n");
1416 #endif
1419 static void do_info_kvm(Monitor *mon)
1421 #if defined(USE_KVM) || defined(CONFIG_KVM)
1422 monitor_printf(mon, "kvm support: ");
1423 if (kvm_enabled())
1424 monitor_printf(mon, "enabled\n");
1425 else
1426 monitor_printf(mon, "disabled\n");
1427 #else
1428 monitor_printf(mon, "kvm support: not compiled\n");
1429 #endif
1432 #ifdef CONFIG_PROFILER
1434 int64_t kqemu_time;
1435 int64_t qemu_time;
1436 int64_t kqemu_exec_count;
1437 int64_t dev_time;
1438 int64_t kqemu_ret_int_count;
1439 int64_t kqemu_ret_excp_count;
1440 int64_t kqemu_ret_intr_count;
1442 static void do_info_profile(Monitor *mon)
1444 int64_t total;
1445 total = qemu_time;
1446 if (total == 0)
1447 total = 1;
1448 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1449 dev_time, dev_time / (double)ticks_per_sec);
1450 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1451 qemu_time, qemu_time / (double)ticks_per_sec);
1452 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1453 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1454 PRId64 "\n",
1455 kqemu_time, kqemu_time / (double)ticks_per_sec,
1456 kqemu_time / (double)total * 100.0,
1457 kqemu_exec_count,
1458 kqemu_ret_int_count,
1459 kqemu_ret_excp_count,
1460 kqemu_ret_intr_count);
1461 qemu_time = 0;
1462 kqemu_time = 0;
1463 kqemu_exec_count = 0;
1464 dev_time = 0;
1465 kqemu_ret_int_count = 0;
1466 kqemu_ret_excp_count = 0;
1467 kqemu_ret_intr_count = 0;
1468 #ifdef USE_KQEMU
1469 kqemu_record_dump();
1470 #endif
1472 #else
1473 static void do_info_profile(Monitor *mon)
1475 monitor_printf(mon, "Internal profiler not compiled\n");
1477 #endif
1479 /* Capture support */
1480 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1482 static void do_info_capture(Monitor *mon)
1484 int i;
1485 CaptureState *s;
1487 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1488 monitor_printf(mon, "[%d]: ", i);
1489 s->ops.info (s->opaque);
1493 static void do_stop_capture(Monitor *mon, int n)
1495 int i;
1496 CaptureState *s;
1498 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1499 if (i == n) {
1500 s->ops.destroy (s->opaque);
1501 LIST_REMOVE (s, entries);
1502 qemu_free (s);
1503 return;
1508 #ifdef HAS_AUDIO
1509 static void do_wav_capture(Monitor *mon, const char *path,
1510 int has_freq, int freq,
1511 int has_bits, int bits,
1512 int has_channels, int nchannels)
1514 CaptureState *s;
1516 s = qemu_mallocz (sizeof (*s));
1518 freq = has_freq ? freq : 44100;
1519 bits = has_bits ? bits : 16;
1520 nchannels = has_channels ? nchannels : 2;
1522 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1523 monitor_printf(mon, "Faied to add wave capture\n");
1524 qemu_free (s);
1526 LIST_INSERT_HEAD (&capture_head, s, entries);
1528 #endif
1530 #if defined(TARGET_I386)
1531 static void do_inject_nmi(Monitor *mon, int cpu_index)
1533 CPUState *env;
1535 for (env = first_cpu; env != NULL; env = env->next_cpu)
1536 if (env->cpu_index == cpu_index) {
1537 if (kvm_enabled())
1538 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1539 else
1540 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1541 break;
1544 #endif
1546 static void do_info_status(Monitor *mon)
1548 if (vm_running) {
1549 if (singlestep) {
1550 monitor_printf(mon, "VM status: running (single step mode)\n");
1551 } else {
1552 monitor_printf(mon, "VM status: running\n");
1554 } else
1555 monitor_printf(mon, "VM status: paused\n");
1559 static void do_balloon(Monitor *mon, int value)
1561 ram_addr_t target = value;
1562 qemu_balloon(target << 20);
1565 static void do_info_balloon(Monitor *mon)
1567 ram_addr_t actual;
1569 actual = qemu_balloon_status();
1570 if (kvm_enabled() && !kvm_has_sync_mmu())
1571 monitor_printf(mon, "Using KVM without synchronous MMU, "
1572 "ballooning disabled\n");
1573 else if (actual == 0)
1574 monitor_printf(mon, "Ballooning not activated in VM\n");
1575 else
1576 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1579 static void do_acl(Monitor *mon,
1580 const char *command,
1581 const char *aclname,
1582 const char *match,
1583 int has_index,
1584 int index)
1586 qemu_acl *acl;
1588 acl = qemu_acl_find(aclname);
1589 if (!acl) {
1590 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1591 return;
1594 if (strcmp(command, "show") == 0) {
1595 int i = 0;
1596 qemu_acl_entry *entry;
1597 monitor_printf(mon, "policy: %s\n",
1598 acl->defaultDeny ? "deny" : "allow");
1599 TAILQ_FOREACH(entry, &acl->entries, next) {
1600 i++;
1601 monitor_printf(mon, "%d: %s %s\n", i,
1602 entry->deny ? "deny" : "allow",
1603 entry->match);
1605 } else if (strcmp(command, "reset") == 0) {
1606 qemu_acl_reset(acl);
1607 monitor_printf(mon, "acl: removed all rules\n");
1608 } else if (strcmp(command, "policy") == 0) {
1609 if (!match) {
1610 monitor_printf(mon, "acl: missing policy parameter\n");
1611 return;
1614 if (strcmp(match, "allow") == 0) {
1615 acl->defaultDeny = 0;
1616 monitor_printf(mon, "acl: policy set to 'allow'\n");
1617 } else if (strcmp(match, "deny") == 0) {
1618 acl->defaultDeny = 1;
1619 monitor_printf(mon, "acl: policy set to 'deny'\n");
1620 } else {
1621 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1623 } else if ((strcmp(command, "allow") == 0) ||
1624 (strcmp(command, "deny") == 0)) {
1625 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1626 int ret;
1628 if (!match) {
1629 monitor_printf(mon, "acl: missing match parameter\n");
1630 return;
1633 if (has_index)
1634 ret = qemu_acl_insert(acl, deny, match, index);
1635 else
1636 ret = qemu_acl_append(acl, deny, match);
1637 if (ret < 0)
1638 monitor_printf(mon, "acl: unable to add acl entry\n");
1639 else
1640 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1641 } else if (strcmp(command, "remove") == 0) {
1642 int ret;
1644 if (!match) {
1645 monitor_printf(mon, "acl: missing match parameter\n");
1646 return;
1649 ret = qemu_acl_remove(acl, match);
1650 if (ret < 0)
1651 monitor_printf(mon, "acl: no matching acl entry\n");
1652 else
1653 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1654 } else {
1655 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1659 /* Please update qemu-doc.texi when adding or changing commands */
1660 static const mon_cmd_t mon_cmds[] = {
1661 { "help|?", "s?", help_cmd,
1662 "[cmd]", "show the help" },
1663 { "commit", "s", do_commit,
1664 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1665 { "info", "s?", do_info,
1666 "subcommand", "show various information about the system state" },
1667 { "q|quit", "", do_quit,
1668 "", "quit the emulator" },
1669 { "eject", "-fB", do_eject,
1670 "[-f] device", "eject a removable medium (use -f to force it)" },
1671 { "change", "BFs?", do_change,
1672 "device filename [format]", "change a removable medium, optional format" },
1673 { "screendump", "F", do_screen_dump,
1674 "filename", "save screen into PPM image 'filename'" },
1675 { "logfile", "F", do_logfile,
1676 "filename", "output logs to 'filename'" },
1677 { "log", "s", do_log,
1678 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1679 { "savevm", "s?", do_savevm,
1680 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1681 { "loadvm", "s", do_loadvm,
1682 "tag|id", "restore a VM snapshot from its tag or id" },
1683 { "delvm", "s", do_delvm,
1684 "tag|id", "delete a VM snapshot from its tag or id" },
1685 { "singlestep", "s?", do_singlestep,
1686 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1687 { "stop", "", do_stop,
1688 "", "stop emulation", },
1689 { "c|cont", "", do_cont,
1690 "", "resume emulation", },
1691 #ifdef CONFIG_GDBSTUB
1692 { "gdbserver", "s?", do_gdbserver,
1693 "[port]", "start gdbserver session (default port=1234)", },
1694 #endif
1695 { "x", "/l", do_memory_dump,
1696 "/fmt addr", "virtual memory dump starting at 'addr'", },
1697 { "xp", "/l", do_physical_memory_dump,
1698 "/fmt addr", "physical memory dump starting at 'addr'", },
1699 { "p|print", "/l", do_print,
1700 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1701 { "i", "/ii.", do_ioport_read,
1702 "/fmt addr", "I/O port read" },
1704 { "sendkey", "si?", do_sendkey,
1705 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1706 { "system_reset", "", do_system_reset,
1707 "", "reset the system" },
1708 { "system_powerdown", "", do_system_powerdown,
1709 "", "send system power down event" },
1710 { "sum", "ii", do_sum,
1711 "addr size", "compute the checksum of a memory region" },
1712 { "usb_add", "s", do_usb_add,
1713 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1714 { "usb_del", "s", do_usb_del,
1715 "device", "remove USB device 'bus.addr'" },
1716 { "cpu", "i", do_cpu_set,
1717 "index", "set the default CPU" },
1718 { "mouse_move", "sss?", do_mouse_move,
1719 "dx dy [dz]", "send mouse move events" },
1720 { "mouse_button", "i", do_mouse_button,
1721 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1722 { "mouse_set", "i", do_mouse_set,
1723 "index", "set which mouse device receives events" },
1724 #ifdef HAS_AUDIO
1725 { "wavcapture", "si?i?i?", do_wav_capture,
1726 "path [frequency bits channels]",
1727 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1728 #endif
1729 { "stopcapture", "i", do_stop_capture,
1730 "capture index", "stop capture" },
1731 { "memsave", "lis", do_memory_save,
1732 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1733 { "pmemsave", "lis", do_physical_memory_save,
1734 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1735 { "boot_set", "s", do_boot_set,
1736 "bootdevice", "define new values for the boot device list" },
1737 #if defined(TARGET_I386)
1738 { "nmi", "i", do_inject_nmi,
1739 "cpu", "inject an NMI on the given CPU", },
1740 #endif
1741 { "migrate", "-ds", do_migrate,
1742 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1743 { "migrate_cancel", "", do_migrate_cancel,
1744 "", "cancel the current VM migration" },
1745 { "migrate_set_speed", "s", do_migrate_set_speed,
1746 "value", "set maximum speed (in bytes) for migrations" },
1747 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1748 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1749 "[file=file][,if=type][,bus=n]\n"
1750 "[,unit=m][,media=d][index=i]\n"
1751 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1752 "[snapshot=on|off][,cache=on|off]",
1753 "add drive to PCI storage controller" },
1754 { "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" },
1755 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1756 { "host_net_add", "ss", net_host_device_add,
1757 "[tap,user,socket,vde] options", "add host VLAN client" },
1758 { "host_net_remove", "is", net_host_device_remove,
1759 "vlan_id name", "remove host VLAN client" },
1760 #endif
1761 { "balloon", "i", do_balloon,
1762 "target", "request VM to change it's memory allocation (in MB)" },
1763 { "set_link", "ss", do_set_link,
1764 "name [up|down]", "change the link status of a network adapter" },
1765 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1766 "acl show vnc.username\n"
1767 "acl policy vnc.username deny\n"
1768 "acl allow vnc.username fred\n"
1769 "acl deny vnc.username bob\n"
1770 "acl reset vnc.username\n" },
1771 { "set_link", "ss", do_set_link, "name [up|down]" },
1772 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1773 { NULL, NULL, },
1776 /* Please update qemu-doc.texi when adding or changing commands */
1777 static const mon_cmd_t info_cmds[] = {
1778 { "version", "", do_info_version,
1779 "", "show the version of QEMU" },
1780 { "network", "", do_info_network,
1781 "", "show the network state" },
1782 { "chardev", "", qemu_chr_info,
1783 "", "show the character devices" },
1784 { "block", "", bdrv_info,
1785 "", "show the block devices" },
1786 { "blockstats", "", bdrv_info_stats,
1787 "", "show block device statistics" },
1788 { "registers", "", do_info_registers,
1789 "", "show the cpu registers" },
1790 { "cpus", "", do_info_cpus,
1791 "", "show infos for each CPU" },
1792 { "history", "", do_info_history,
1793 "", "show the command line history", },
1794 { "irq", "", irq_info,
1795 "", "show the interrupts statistics (if available)", },
1796 { "pic", "", pic_info,
1797 "", "show i8259 (PIC) state", },
1798 { "pci", "", pci_info,
1799 "", "show PCI info", },
1800 #if defined(TARGET_I386) || defined(TARGET_SH4)
1801 { "tlb", "", tlb_info,
1802 "", "show virtual to physical memory mappings", },
1803 #endif
1804 #if defined(TARGET_I386)
1805 { "mem", "", mem_info,
1806 "", "show the active virtual memory mappings", },
1807 { "hpet", "", do_info_hpet,
1808 "", "show state of HPET", },
1809 #endif
1810 { "jit", "", do_info_jit,
1811 "", "show dynamic compiler info", },
1812 { "kqemu", "", do_info_kqemu,
1813 "", "show KQEMU information", },
1814 { "kvm", "", do_info_kvm,
1815 "", "show KVM information", },
1816 { "usb", "", usb_info,
1817 "", "show guest USB devices", },
1818 { "usbhost", "", usb_host_info,
1819 "", "show host USB devices", },
1820 { "profile", "", do_info_profile,
1821 "", "show profiling information", },
1822 { "capture", "", do_info_capture,
1823 "", "show capture information" },
1824 { "snapshots", "", do_info_snapshots,
1825 "", "show the currently saved VM snapshots" },
1826 { "status", "", do_info_status,
1827 "", "show the current VM status (running|paused)" },
1828 { "pcmcia", "", pcmcia_info,
1829 "", "show guest PCMCIA status" },
1830 { "mice", "", do_info_mice,
1831 "", "show which guest mouse is receiving events" },
1832 { "vnc", "", do_info_vnc,
1833 "", "show the vnc server status"},
1834 { "name", "", do_info_name,
1835 "", "show the current VM name" },
1836 { "uuid", "", do_info_uuid,
1837 "", "show the current VM UUID" },
1838 #if defined(TARGET_PPC)
1839 { "cpustats", "", do_info_cpu_stats,
1840 "", "show CPU statistics", },
1841 #endif
1842 #if defined(CONFIG_SLIRP)
1843 { "slirp", "", do_info_slirp,
1844 "", "show SLIRP statistics", },
1845 #endif
1846 { "migrate", "", do_info_migrate, "", "show migration status" },
1847 { "balloon", "", do_info_balloon,
1848 "", "show balloon information" },
1849 { NULL, NULL, },
1852 /*******************************************************************/
1854 static const char *pch;
1855 static jmp_buf expr_env;
1857 #define MD_TLONG 0
1858 #define MD_I32 1
1860 typedef struct MonitorDef {
1861 const char *name;
1862 int offset;
1863 target_long (*get_value)(const struct MonitorDef *md, int val);
1864 int type;
1865 } MonitorDef;
1867 #if defined(TARGET_I386)
1868 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1870 CPUState *env = mon_get_cpu();
1871 if (!env)
1872 return 0;
1873 return env->eip + env->segs[R_CS].base;
1875 #endif
1877 #if defined(TARGET_PPC)
1878 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1880 CPUState *env = mon_get_cpu();
1881 unsigned int u;
1882 int i;
1884 if (!env)
1885 return 0;
1887 u = 0;
1888 for (i = 0; i < 8; i++)
1889 u |= env->crf[i] << (32 - (4 * i));
1891 return u;
1894 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1896 CPUState *env = mon_get_cpu();
1897 if (!env)
1898 return 0;
1899 return env->msr;
1902 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1904 CPUState *env = mon_get_cpu();
1905 if (!env)
1906 return 0;
1907 return env->xer;
1910 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1912 CPUState *env = mon_get_cpu();
1913 if (!env)
1914 return 0;
1915 return cpu_ppc_load_decr(env);
1918 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1920 CPUState *env = mon_get_cpu();
1921 if (!env)
1922 return 0;
1923 return cpu_ppc_load_tbu(env);
1926 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1928 CPUState *env = mon_get_cpu();
1929 if (!env)
1930 return 0;
1931 return cpu_ppc_load_tbl(env);
1933 #endif
1935 #if defined(TARGET_SPARC)
1936 #ifndef TARGET_SPARC64
1937 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1939 CPUState *env = mon_get_cpu();
1940 if (!env)
1941 return 0;
1942 return GET_PSR(env);
1944 #endif
1946 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1948 CPUState *env = mon_get_cpu();
1949 if (!env)
1950 return 0;
1951 return env->regwptr[val];
1953 #endif
1955 static const MonitorDef monitor_defs[] = {
1956 #ifdef TARGET_I386
1958 #define SEG(name, seg) \
1959 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1960 { name ".base", offsetof(CPUState, segs[seg].base) },\
1961 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1963 { "eax", offsetof(CPUState, regs[0]) },
1964 { "ecx", offsetof(CPUState, regs[1]) },
1965 { "edx", offsetof(CPUState, regs[2]) },
1966 { "ebx", offsetof(CPUState, regs[3]) },
1967 { "esp|sp", offsetof(CPUState, regs[4]) },
1968 { "ebp|fp", offsetof(CPUState, regs[5]) },
1969 { "esi", offsetof(CPUState, regs[6]) },
1970 { "edi", offsetof(CPUState, regs[7]) },
1971 #ifdef TARGET_X86_64
1972 { "r8", offsetof(CPUState, regs[8]) },
1973 { "r9", offsetof(CPUState, regs[9]) },
1974 { "r10", offsetof(CPUState, regs[10]) },
1975 { "r11", offsetof(CPUState, regs[11]) },
1976 { "r12", offsetof(CPUState, regs[12]) },
1977 { "r13", offsetof(CPUState, regs[13]) },
1978 { "r14", offsetof(CPUState, regs[14]) },
1979 { "r15", offsetof(CPUState, regs[15]) },
1980 #endif
1981 { "eflags", offsetof(CPUState, eflags) },
1982 { "eip", offsetof(CPUState, eip) },
1983 SEG("cs", R_CS)
1984 SEG("ds", R_DS)
1985 SEG("es", R_ES)
1986 SEG("ss", R_SS)
1987 SEG("fs", R_FS)
1988 SEG("gs", R_GS)
1989 { "pc", 0, monitor_get_pc, },
1990 #elif defined(TARGET_PPC)
1991 /* General purpose registers */
1992 { "r0", offsetof(CPUState, gpr[0]) },
1993 { "r1", offsetof(CPUState, gpr[1]) },
1994 { "r2", offsetof(CPUState, gpr[2]) },
1995 { "r3", offsetof(CPUState, gpr[3]) },
1996 { "r4", offsetof(CPUState, gpr[4]) },
1997 { "r5", offsetof(CPUState, gpr[5]) },
1998 { "r6", offsetof(CPUState, gpr[6]) },
1999 { "r7", offsetof(CPUState, gpr[7]) },
2000 { "r8", offsetof(CPUState, gpr[8]) },
2001 { "r9", offsetof(CPUState, gpr[9]) },
2002 { "r10", offsetof(CPUState, gpr[10]) },
2003 { "r11", offsetof(CPUState, gpr[11]) },
2004 { "r12", offsetof(CPUState, gpr[12]) },
2005 { "r13", offsetof(CPUState, gpr[13]) },
2006 { "r14", offsetof(CPUState, gpr[14]) },
2007 { "r15", offsetof(CPUState, gpr[15]) },
2008 { "r16", offsetof(CPUState, gpr[16]) },
2009 { "r17", offsetof(CPUState, gpr[17]) },
2010 { "r18", offsetof(CPUState, gpr[18]) },
2011 { "r19", offsetof(CPUState, gpr[19]) },
2012 { "r20", offsetof(CPUState, gpr[20]) },
2013 { "r21", offsetof(CPUState, gpr[21]) },
2014 { "r22", offsetof(CPUState, gpr[22]) },
2015 { "r23", offsetof(CPUState, gpr[23]) },
2016 { "r24", offsetof(CPUState, gpr[24]) },
2017 { "r25", offsetof(CPUState, gpr[25]) },
2018 { "r26", offsetof(CPUState, gpr[26]) },
2019 { "r27", offsetof(CPUState, gpr[27]) },
2020 { "r28", offsetof(CPUState, gpr[28]) },
2021 { "r29", offsetof(CPUState, gpr[29]) },
2022 { "r30", offsetof(CPUState, gpr[30]) },
2023 { "r31", offsetof(CPUState, gpr[31]) },
2024 /* Floating point registers */
2025 { "f0", offsetof(CPUState, fpr[0]) },
2026 { "f1", offsetof(CPUState, fpr[1]) },
2027 { "f2", offsetof(CPUState, fpr[2]) },
2028 { "f3", offsetof(CPUState, fpr[3]) },
2029 { "f4", offsetof(CPUState, fpr[4]) },
2030 { "f5", offsetof(CPUState, fpr[5]) },
2031 { "f6", offsetof(CPUState, fpr[6]) },
2032 { "f7", offsetof(CPUState, fpr[7]) },
2033 { "f8", offsetof(CPUState, fpr[8]) },
2034 { "f9", offsetof(CPUState, fpr[9]) },
2035 { "f10", offsetof(CPUState, fpr[10]) },
2036 { "f11", offsetof(CPUState, fpr[11]) },
2037 { "f12", offsetof(CPUState, fpr[12]) },
2038 { "f13", offsetof(CPUState, fpr[13]) },
2039 { "f14", offsetof(CPUState, fpr[14]) },
2040 { "f15", offsetof(CPUState, fpr[15]) },
2041 { "f16", offsetof(CPUState, fpr[16]) },
2042 { "f17", offsetof(CPUState, fpr[17]) },
2043 { "f18", offsetof(CPUState, fpr[18]) },
2044 { "f19", offsetof(CPUState, fpr[19]) },
2045 { "f20", offsetof(CPUState, fpr[20]) },
2046 { "f21", offsetof(CPUState, fpr[21]) },
2047 { "f22", offsetof(CPUState, fpr[22]) },
2048 { "f23", offsetof(CPUState, fpr[23]) },
2049 { "f24", offsetof(CPUState, fpr[24]) },
2050 { "f25", offsetof(CPUState, fpr[25]) },
2051 { "f26", offsetof(CPUState, fpr[26]) },
2052 { "f27", offsetof(CPUState, fpr[27]) },
2053 { "f28", offsetof(CPUState, fpr[28]) },
2054 { "f29", offsetof(CPUState, fpr[29]) },
2055 { "f30", offsetof(CPUState, fpr[30]) },
2056 { "f31", offsetof(CPUState, fpr[31]) },
2057 { "fpscr", offsetof(CPUState, fpscr) },
2058 /* Next instruction pointer */
2059 { "nip|pc", offsetof(CPUState, nip) },
2060 { "lr", offsetof(CPUState, lr) },
2061 { "ctr", offsetof(CPUState, ctr) },
2062 { "decr", 0, &monitor_get_decr, },
2063 { "ccr", 0, &monitor_get_ccr, },
2064 /* Machine state register */
2065 { "msr", 0, &monitor_get_msr, },
2066 { "xer", 0, &monitor_get_xer, },
2067 { "tbu", 0, &monitor_get_tbu, },
2068 { "tbl", 0, &monitor_get_tbl, },
2069 #if defined(TARGET_PPC64)
2070 /* Address space register */
2071 { "asr", offsetof(CPUState, asr) },
2072 #endif
2073 /* Segment registers */
2074 { "sdr1", offsetof(CPUState, sdr1) },
2075 { "sr0", offsetof(CPUState, sr[0]) },
2076 { "sr1", offsetof(CPUState, sr[1]) },
2077 { "sr2", offsetof(CPUState, sr[2]) },
2078 { "sr3", offsetof(CPUState, sr[3]) },
2079 { "sr4", offsetof(CPUState, sr[4]) },
2080 { "sr5", offsetof(CPUState, sr[5]) },
2081 { "sr6", offsetof(CPUState, sr[6]) },
2082 { "sr7", offsetof(CPUState, sr[7]) },
2083 { "sr8", offsetof(CPUState, sr[8]) },
2084 { "sr9", offsetof(CPUState, sr[9]) },
2085 { "sr10", offsetof(CPUState, sr[10]) },
2086 { "sr11", offsetof(CPUState, sr[11]) },
2087 { "sr12", offsetof(CPUState, sr[12]) },
2088 { "sr13", offsetof(CPUState, sr[13]) },
2089 { "sr14", offsetof(CPUState, sr[14]) },
2090 { "sr15", offsetof(CPUState, sr[15]) },
2091 /* Too lazy to put BATs and SPRs ... */
2092 #elif defined(TARGET_SPARC)
2093 { "g0", offsetof(CPUState, gregs[0]) },
2094 { "g1", offsetof(CPUState, gregs[1]) },
2095 { "g2", offsetof(CPUState, gregs[2]) },
2096 { "g3", offsetof(CPUState, gregs[3]) },
2097 { "g4", offsetof(CPUState, gregs[4]) },
2098 { "g5", offsetof(CPUState, gregs[5]) },
2099 { "g6", offsetof(CPUState, gregs[6]) },
2100 { "g7", offsetof(CPUState, gregs[7]) },
2101 { "o0", 0, monitor_get_reg },
2102 { "o1", 1, monitor_get_reg },
2103 { "o2", 2, monitor_get_reg },
2104 { "o3", 3, monitor_get_reg },
2105 { "o4", 4, monitor_get_reg },
2106 { "o5", 5, monitor_get_reg },
2107 { "o6", 6, monitor_get_reg },
2108 { "o7", 7, monitor_get_reg },
2109 { "l0", 8, monitor_get_reg },
2110 { "l1", 9, monitor_get_reg },
2111 { "l2", 10, monitor_get_reg },
2112 { "l3", 11, monitor_get_reg },
2113 { "l4", 12, monitor_get_reg },
2114 { "l5", 13, monitor_get_reg },
2115 { "l6", 14, monitor_get_reg },
2116 { "l7", 15, monitor_get_reg },
2117 { "i0", 16, monitor_get_reg },
2118 { "i1", 17, monitor_get_reg },
2119 { "i2", 18, monitor_get_reg },
2120 { "i3", 19, monitor_get_reg },
2121 { "i4", 20, monitor_get_reg },
2122 { "i5", 21, monitor_get_reg },
2123 { "i6", 22, monitor_get_reg },
2124 { "i7", 23, monitor_get_reg },
2125 { "pc", offsetof(CPUState, pc) },
2126 { "npc", offsetof(CPUState, npc) },
2127 { "y", offsetof(CPUState, y) },
2128 #ifndef TARGET_SPARC64
2129 { "psr", 0, &monitor_get_psr, },
2130 { "wim", offsetof(CPUState, wim) },
2131 #endif
2132 { "tbr", offsetof(CPUState, tbr) },
2133 { "fsr", offsetof(CPUState, fsr) },
2134 { "f0", offsetof(CPUState, fpr[0]) },
2135 { "f1", offsetof(CPUState, fpr[1]) },
2136 { "f2", offsetof(CPUState, fpr[2]) },
2137 { "f3", offsetof(CPUState, fpr[3]) },
2138 { "f4", offsetof(CPUState, fpr[4]) },
2139 { "f5", offsetof(CPUState, fpr[5]) },
2140 { "f6", offsetof(CPUState, fpr[6]) },
2141 { "f7", offsetof(CPUState, fpr[7]) },
2142 { "f8", offsetof(CPUState, fpr[8]) },
2143 { "f9", offsetof(CPUState, fpr[9]) },
2144 { "f10", offsetof(CPUState, fpr[10]) },
2145 { "f11", offsetof(CPUState, fpr[11]) },
2146 { "f12", offsetof(CPUState, fpr[12]) },
2147 { "f13", offsetof(CPUState, fpr[13]) },
2148 { "f14", offsetof(CPUState, fpr[14]) },
2149 { "f15", offsetof(CPUState, fpr[15]) },
2150 { "f16", offsetof(CPUState, fpr[16]) },
2151 { "f17", offsetof(CPUState, fpr[17]) },
2152 { "f18", offsetof(CPUState, fpr[18]) },
2153 { "f19", offsetof(CPUState, fpr[19]) },
2154 { "f20", offsetof(CPUState, fpr[20]) },
2155 { "f21", offsetof(CPUState, fpr[21]) },
2156 { "f22", offsetof(CPUState, fpr[22]) },
2157 { "f23", offsetof(CPUState, fpr[23]) },
2158 { "f24", offsetof(CPUState, fpr[24]) },
2159 { "f25", offsetof(CPUState, fpr[25]) },
2160 { "f26", offsetof(CPUState, fpr[26]) },
2161 { "f27", offsetof(CPUState, fpr[27]) },
2162 { "f28", offsetof(CPUState, fpr[28]) },
2163 { "f29", offsetof(CPUState, fpr[29]) },
2164 { "f30", offsetof(CPUState, fpr[30]) },
2165 { "f31", offsetof(CPUState, fpr[31]) },
2166 #ifdef TARGET_SPARC64
2167 { "f32", offsetof(CPUState, fpr[32]) },
2168 { "f34", offsetof(CPUState, fpr[34]) },
2169 { "f36", offsetof(CPUState, fpr[36]) },
2170 { "f38", offsetof(CPUState, fpr[38]) },
2171 { "f40", offsetof(CPUState, fpr[40]) },
2172 { "f42", offsetof(CPUState, fpr[42]) },
2173 { "f44", offsetof(CPUState, fpr[44]) },
2174 { "f46", offsetof(CPUState, fpr[46]) },
2175 { "f48", offsetof(CPUState, fpr[48]) },
2176 { "f50", offsetof(CPUState, fpr[50]) },
2177 { "f52", offsetof(CPUState, fpr[52]) },
2178 { "f54", offsetof(CPUState, fpr[54]) },
2179 { "f56", offsetof(CPUState, fpr[56]) },
2180 { "f58", offsetof(CPUState, fpr[58]) },
2181 { "f60", offsetof(CPUState, fpr[60]) },
2182 { "f62", offsetof(CPUState, fpr[62]) },
2183 { "asi", offsetof(CPUState, asi) },
2184 { "pstate", offsetof(CPUState, pstate) },
2185 { "cansave", offsetof(CPUState, cansave) },
2186 { "canrestore", offsetof(CPUState, canrestore) },
2187 { "otherwin", offsetof(CPUState, otherwin) },
2188 { "wstate", offsetof(CPUState, wstate) },
2189 { "cleanwin", offsetof(CPUState, cleanwin) },
2190 { "fprs", offsetof(CPUState, fprs) },
2191 #endif
2192 #endif
2193 { NULL },
2196 static void expr_error(Monitor *mon, const char *msg)
2198 monitor_printf(mon, "%s\n", msg);
2199 longjmp(expr_env, 1);
2202 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2203 static int get_monitor_def(target_long *pval, const char *name)
2205 const MonitorDef *md;
2206 void *ptr;
2208 for(md = monitor_defs; md->name != NULL; md++) {
2209 if (compare_cmd(name, md->name)) {
2210 if (md->get_value) {
2211 *pval = md->get_value(md, md->offset);
2212 } else {
2213 CPUState *env = mon_get_cpu();
2214 if (!env)
2215 return -2;
2216 ptr = (uint8_t *)env + md->offset;
2217 switch(md->type) {
2218 case MD_I32:
2219 *pval = *(int32_t *)ptr;
2220 break;
2221 case MD_TLONG:
2222 *pval = *(target_long *)ptr;
2223 break;
2224 default:
2225 *pval = 0;
2226 break;
2229 return 0;
2232 return -1;
2235 static void next(void)
2237 if (pch != '\0') {
2238 pch++;
2239 while (qemu_isspace(*pch))
2240 pch++;
2244 static int64_t expr_sum(Monitor *mon);
2246 static int64_t expr_unary(Monitor *mon)
2248 int64_t n;
2249 char *p;
2250 int ret;
2252 switch(*pch) {
2253 case '+':
2254 next();
2255 n = expr_unary(mon);
2256 break;
2257 case '-':
2258 next();
2259 n = -expr_unary(mon);
2260 break;
2261 case '~':
2262 next();
2263 n = ~expr_unary(mon);
2264 break;
2265 case '(':
2266 next();
2267 n = expr_sum(mon);
2268 if (*pch != ')') {
2269 expr_error(mon, "')' expected");
2271 next();
2272 break;
2273 case '\'':
2274 pch++;
2275 if (*pch == '\0')
2276 expr_error(mon, "character constant expected");
2277 n = *pch;
2278 pch++;
2279 if (*pch != '\'')
2280 expr_error(mon, "missing terminating \' character");
2281 next();
2282 break;
2283 case '$':
2285 char buf[128], *q;
2286 target_long reg=0;
2288 pch++;
2289 q = buf;
2290 while ((*pch >= 'a' && *pch <= 'z') ||
2291 (*pch >= 'A' && *pch <= 'Z') ||
2292 (*pch >= '0' && *pch <= '9') ||
2293 *pch == '_' || *pch == '.') {
2294 if ((q - buf) < sizeof(buf) - 1)
2295 *q++ = *pch;
2296 pch++;
2298 while (qemu_isspace(*pch))
2299 pch++;
2300 *q = 0;
2301 ret = get_monitor_def(&reg, buf);
2302 if (ret == -1)
2303 expr_error(mon, "unknown register");
2304 else if (ret == -2)
2305 expr_error(mon, "no cpu defined");
2306 n = reg;
2308 break;
2309 case '\0':
2310 expr_error(mon, "unexpected end of expression");
2311 n = 0;
2312 break;
2313 default:
2314 #if TARGET_PHYS_ADDR_BITS > 32
2315 n = strtoull(pch, &p, 0);
2316 #else
2317 n = strtoul(pch, &p, 0);
2318 #endif
2319 if (pch == p) {
2320 expr_error(mon, "invalid char in expression");
2322 pch = p;
2323 while (qemu_isspace(*pch))
2324 pch++;
2325 break;
2327 return n;
2331 static int64_t expr_prod(Monitor *mon)
2333 int64_t val, val2;
2334 int op;
2336 val = expr_unary(mon);
2337 for(;;) {
2338 op = *pch;
2339 if (op != '*' && op != '/' && op != '%')
2340 break;
2341 next();
2342 val2 = expr_unary(mon);
2343 switch(op) {
2344 default:
2345 case '*':
2346 val *= val2;
2347 break;
2348 case '/':
2349 case '%':
2350 if (val2 == 0)
2351 expr_error(mon, "division by zero");
2352 if (op == '/')
2353 val /= val2;
2354 else
2355 val %= val2;
2356 break;
2359 return val;
2362 static int64_t expr_logic(Monitor *mon)
2364 int64_t val, val2;
2365 int op;
2367 val = expr_prod(mon);
2368 for(;;) {
2369 op = *pch;
2370 if (op != '&' && op != '|' && op != '^')
2371 break;
2372 next();
2373 val2 = expr_prod(mon);
2374 switch(op) {
2375 default:
2376 case '&':
2377 val &= val2;
2378 break;
2379 case '|':
2380 val |= val2;
2381 break;
2382 case '^':
2383 val ^= val2;
2384 break;
2387 return val;
2390 static int64_t expr_sum(Monitor *mon)
2392 int64_t val, val2;
2393 int op;
2395 val = expr_logic(mon);
2396 for(;;) {
2397 op = *pch;
2398 if (op != '+' && op != '-')
2399 break;
2400 next();
2401 val2 = expr_logic(mon);
2402 if (op == '+')
2403 val += val2;
2404 else
2405 val -= val2;
2407 return val;
2410 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2412 pch = *pp;
2413 if (setjmp(expr_env)) {
2414 *pp = pch;
2415 return -1;
2417 while (qemu_isspace(*pch))
2418 pch++;
2419 *pval = expr_sum(mon);
2420 *pp = pch;
2421 return 0;
2424 static int get_str(char *buf, int buf_size, const char **pp)
2426 const char *p;
2427 char *q;
2428 int c;
2430 q = buf;
2431 p = *pp;
2432 while (qemu_isspace(*p))
2433 p++;
2434 if (*p == '\0') {
2435 fail:
2436 *q = '\0';
2437 *pp = p;
2438 return -1;
2440 if (*p == '\"') {
2441 p++;
2442 while (*p != '\0' && *p != '\"') {
2443 if (*p == '\\') {
2444 p++;
2445 c = *p++;
2446 switch(c) {
2447 case 'n':
2448 c = '\n';
2449 break;
2450 case 'r':
2451 c = '\r';
2452 break;
2453 case '\\':
2454 case '\'':
2455 case '\"':
2456 break;
2457 default:
2458 qemu_printf("unsupported escape code: '\\%c'\n", c);
2459 goto fail;
2461 if ((q - buf) < buf_size - 1) {
2462 *q++ = c;
2464 } else {
2465 if ((q - buf) < buf_size - 1) {
2466 *q++ = *p;
2468 p++;
2471 if (*p != '\"') {
2472 qemu_printf("unterminated string\n");
2473 goto fail;
2475 p++;
2476 } else {
2477 while (*p != '\0' && !qemu_isspace(*p)) {
2478 if ((q - buf) < buf_size - 1) {
2479 *q++ = *p;
2481 p++;
2484 *q = '\0';
2485 *pp = p;
2486 return 0;
2489 static int default_fmt_format = 'x';
2490 static int default_fmt_size = 4;
2492 #define MAX_ARGS 16
2494 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2496 const char *p, *pstart, *typestr;
2497 char *q;
2498 int c, nb_args, len, i, has_arg;
2499 const mon_cmd_t *cmd;
2500 char cmdname[256];
2501 char buf[1024];
2502 void *str_allocated[MAX_ARGS];
2503 void *args[MAX_ARGS];
2504 void (*handler_0)(Monitor *mon);
2505 void (*handler_1)(Monitor *mon, void *arg0);
2506 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2507 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2508 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2509 void *arg3);
2510 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2511 void *arg3, void *arg4);
2512 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2513 void *arg3, void *arg4, void *arg5);
2514 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2515 void *arg3, void *arg4, void *arg5, void *arg6);
2517 #ifdef DEBUG
2518 monitor_printf(mon, "command='%s'\n", cmdline);
2519 #endif
2521 /* extract the command name */
2522 p = cmdline;
2523 q = cmdname;
2524 while (qemu_isspace(*p))
2525 p++;
2526 if (*p == '\0')
2527 return;
2528 pstart = p;
2529 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2530 p++;
2531 len = p - pstart;
2532 if (len > sizeof(cmdname) - 1)
2533 len = sizeof(cmdname) - 1;
2534 memcpy(cmdname, pstart, len);
2535 cmdname[len] = '\0';
2537 /* find the command */
2538 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2539 if (compare_cmd(cmdname, cmd->name))
2540 goto found;
2542 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2543 return;
2544 found:
2546 for(i = 0; i < MAX_ARGS; i++)
2547 str_allocated[i] = NULL;
2549 /* parse the parameters */
2550 typestr = cmd->args_type;
2551 nb_args = 0;
2552 for(;;) {
2553 c = *typestr;
2554 if (c == '\0')
2555 break;
2556 typestr++;
2557 switch(c) {
2558 case 'F':
2559 case 'B':
2560 case 's':
2562 int ret;
2563 char *str;
2565 while (qemu_isspace(*p))
2566 p++;
2567 if (*typestr == '?') {
2568 typestr++;
2569 if (*p == '\0') {
2570 /* no optional string: NULL argument */
2571 str = NULL;
2572 goto add_str;
2575 ret = get_str(buf, sizeof(buf), &p);
2576 if (ret < 0) {
2577 switch(c) {
2578 case 'F':
2579 monitor_printf(mon, "%s: filename expected\n",
2580 cmdname);
2581 break;
2582 case 'B':
2583 monitor_printf(mon, "%s: block device name expected\n",
2584 cmdname);
2585 break;
2586 default:
2587 monitor_printf(mon, "%s: string expected\n", cmdname);
2588 break;
2590 goto fail;
2592 str = qemu_malloc(strlen(buf) + 1);
2593 pstrcpy(str, sizeof(buf), buf);
2594 str_allocated[nb_args] = str;
2595 add_str:
2596 if (nb_args >= MAX_ARGS) {
2597 error_args:
2598 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2599 goto fail;
2601 args[nb_args++] = str;
2603 break;
2604 case '/':
2606 int count, format, size;
2608 while (qemu_isspace(*p))
2609 p++;
2610 if (*p == '/') {
2611 /* format found */
2612 p++;
2613 count = 1;
2614 if (qemu_isdigit(*p)) {
2615 count = 0;
2616 while (qemu_isdigit(*p)) {
2617 count = count * 10 + (*p - '0');
2618 p++;
2621 size = -1;
2622 format = -1;
2623 for(;;) {
2624 switch(*p) {
2625 case 'o':
2626 case 'd':
2627 case 'u':
2628 case 'x':
2629 case 'i':
2630 case 'c':
2631 format = *p++;
2632 break;
2633 case 'b':
2634 size = 1;
2635 p++;
2636 break;
2637 case 'h':
2638 size = 2;
2639 p++;
2640 break;
2641 case 'w':
2642 size = 4;
2643 p++;
2644 break;
2645 case 'g':
2646 case 'L':
2647 size = 8;
2648 p++;
2649 break;
2650 default:
2651 goto next;
2654 next:
2655 if (*p != '\0' && !qemu_isspace(*p)) {
2656 monitor_printf(mon, "invalid char in format: '%c'\n",
2657 *p);
2658 goto fail;
2660 if (format < 0)
2661 format = default_fmt_format;
2662 if (format != 'i') {
2663 /* for 'i', not specifying a size gives -1 as size */
2664 if (size < 0)
2665 size = default_fmt_size;
2666 default_fmt_size = size;
2668 default_fmt_format = format;
2669 } else {
2670 count = 1;
2671 format = default_fmt_format;
2672 if (format != 'i') {
2673 size = default_fmt_size;
2674 } else {
2675 size = -1;
2678 if (nb_args + 3 > MAX_ARGS)
2679 goto error_args;
2680 args[nb_args++] = (void*)(long)count;
2681 args[nb_args++] = (void*)(long)format;
2682 args[nb_args++] = (void*)(long)size;
2684 break;
2685 case 'i':
2686 case 'l':
2688 int64_t val;
2690 while (qemu_isspace(*p))
2691 p++;
2692 if (*typestr == '?' || *typestr == '.') {
2693 if (*typestr == '?') {
2694 if (*p == '\0')
2695 has_arg = 0;
2696 else
2697 has_arg = 1;
2698 } else {
2699 if (*p == '.') {
2700 p++;
2701 while (qemu_isspace(*p))
2702 p++;
2703 has_arg = 1;
2704 } else {
2705 has_arg = 0;
2708 typestr++;
2709 if (nb_args >= MAX_ARGS)
2710 goto error_args;
2711 args[nb_args++] = (void *)(long)has_arg;
2712 if (!has_arg) {
2713 if (nb_args >= MAX_ARGS)
2714 goto error_args;
2715 val = -1;
2716 goto add_num;
2719 if (get_expr(mon, &val, &p))
2720 goto fail;
2721 add_num:
2722 if (c == 'i') {
2723 if (nb_args >= MAX_ARGS)
2724 goto error_args;
2725 args[nb_args++] = (void *)(long)val;
2726 } else {
2727 if ((nb_args + 1) >= MAX_ARGS)
2728 goto error_args;
2729 #if TARGET_PHYS_ADDR_BITS > 32
2730 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2731 #else
2732 args[nb_args++] = (void *)0;
2733 #endif
2734 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2737 break;
2738 case '-':
2740 int has_option;
2741 /* option */
2743 c = *typestr++;
2744 if (c == '\0')
2745 goto bad_type;
2746 while (qemu_isspace(*p))
2747 p++;
2748 has_option = 0;
2749 if (*p == '-') {
2750 p++;
2751 if (*p != c) {
2752 monitor_printf(mon, "%s: unsupported option -%c\n",
2753 cmdname, *p);
2754 goto fail;
2756 p++;
2757 has_option = 1;
2759 if (nb_args >= MAX_ARGS)
2760 goto error_args;
2761 args[nb_args++] = (void *)(long)has_option;
2763 break;
2764 default:
2765 bad_type:
2766 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2767 goto fail;
2770 /* check that all arguments were parsed */
2771 while (qemu_isspace(*p))
2772 p++;
2773 if (*p != '\0') {
2774 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2775 cmdname);
2776 goto fail;
2779 switch(nb_args) {
2780 case 0:
2781 handler_0 = cmd->handler;
2782 handler_0(mon);
2783 break;
2784 case 1:
2785 handler_1 = cmd->handler;
2786 handler_1(mon, args[0]);
2787 break;
2788 case 2:
2789 handler_2 = cmd->handler;
2790 handler_2(mon, args[0], args[1]);
2791 break;
2792 case 3:
2793 handler_3 = cmd->handler;
2794 handler_3(mon, args[0], args[1], args[2]);
2795 break;
2796 case 4:
2797 handler_4 = cmd->handler;
2798 handler_4(mon, args[0], args[1], args[2], args[3]);
2799 break;
2800 case 5:
2801 handler_5 = cmd->handler;
2802 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2803 break;
2804 case 6:
2805 handler_6 = cmd->handler;
2806 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2807 break;
2808 case 7:
2809 handler_7 = cmd->handler;
2810 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2811 args[6]);
2812 break;
2813 default:
2814 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2815 goto fail;
2817 fail:
2818 for(i = 0; i < MAX_ARGS; i++)
2819 qemu_free(str_allocated[i]);
2820 return;
2823 static void cmd_completion(const char *name, const char *list)
2825 const char *p, *pstart;
2826 char cmd[128];
2827 int len;
2829 p = list;
2830 for(;;) {
2831 pstart = p;
2832 p = strchr(p, '|');
2833 if (!p)
2834 p = pstart + strlen(pstart);
2835 len = p - pstart;
2836 if (len > sizeof(cmd) - 2)
2837 len = sizeof(cmd) - 2;
2838 memcpy(cmd, pstart, len);
2839 cmd[len] = '\0';
2840 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2841 readline_add_completion(cur_mon->rs, cmd);
2843 if (*p == '\0')
2844 break;
2845 p++;
2849 static void file_completion(const char *input)
2851 DIR *ffs;
2852 struct dirent *d;
2853 char path[1024];
2854 char file[1024], file_prefix[1024];
2855 int input_path_len;
2856 const char *p;
2858 p = strrchr(input, '/');
2859 if (!p) {
2860 input_path_len = 0;
2861 pstrcpy(file_prefix, sizeof(file_prefix), input);
2862 pstrcpy(path, sizeof(path), ".");
2863 } else {
2864 input_path_len = p - input + 1;
2865 memcpy(path, input, input_path_len);
2866 if (input_path_len > sizeof(path) - 1)
2867 input_path_len = sizeof(path) - 1;
2868 path[input_path_len] = '\0';
2869 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2871 #ifdef DEBUG_COMPLETION
2872 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2873 input, path, file_prefix);
2874 #endif
2875 ffs = opendir(path);
2876 if (!ffs)
2877 return;
2878 for(;;) {
2879 struct stat sb;
2880 d = readdir(ffs);
2881 if (!d)
2882 break;
2883 if (strstart(d->d_name, file_prefix, NULL)) {
2884 memcpy(file, input, input_path_len);
2885 if (input_path_len < sizeof(file))
2886 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2887 d->d_name);
2888 /* stat the file to find out if it's a directory.
2889 * In that case add a slash to speed up typing long paths
2891 stat(file, &sb);
2892 if(S_ISDIR(sb.st_mode))
2893 pstrcat(file, sizeof(file), "/");
2894 readline_add_completion(cur_mon->rs, file);
2897 closedir(ffs);
2900 static void block_completion_it(void *opaque, BlockDriverState *bs)
2902 const char *name = bdrv_get_device_name(bs);
2903 const char *input = opaque;
2905 if (input[0] == '\0' ||
2906 !strncmp(name, (char *)input, strlen(input))) {
2907 readline_add_completion(cur_mon->rs, name);
2911 /* NOTE: this parser is an approximate form of the real command parser */
2912 static void parse_cmdline(const char *cmdline,
2913 int *pnb_args, char **args)
2915 const char *p;
2916 int nb_args, ret;
2917 char buf[1024];
2919 p = cmdline;
2920 nb_args = 0;
2921 for(;;) {
2922 while (qemu_isspace(*p))
2923 p++;
2924 if (*p == '\0')
2925 break;
2926 if (nb_args >= MAX_ARGS)
2927 break;
2928 ret = get_str(buf, sizeof(buf), &p);
2929 args[nb_args] = qemu_strdup(buf);
2930 nb_args++;
2931 if (ret < 0)
2932 break;
2934 *pnb_args = nb_args;
2937 static void monitor_find_completion(const char *cmdline)
2939 const char *cmdname;
2940 char *args[MAX_ARGS];
2941 int nb_args, i, len;
2942 const char *ptype, *str;
2943 const mon_cmd_t *cmd;
2944 const KeyDef *key;
2946 parse_cmdline(cmdline, &nb_args, args);
2947 #ifdef DEBUG_COMPLETION
2948 for(i = 0; i < nb_args; i++) {
2949 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2951 #endif
2953 /* if the line ends with a space, it means we want to complete the
2954 next arg */
2955 len = strlen(cmdline);
2956 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2957 if (nb_args >= MAX_ARGS)
2958 return;
2959 args[nb_args++] = qemu_strdup("");
2961 if (nb_args <= 1) {
2962 /* command completion */
2963 if (nb_args == 0)
2964 cmdname = "";
2965 else
2966 cmdname = args[0];
2967 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2968 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2969 cmd_completion(cmdname, cmd->name);
2971 } else {
2972 /* find the command */
2973 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2974 if (compare_cmd(args[0], cmd->name))
2975 goto found;
2977 return;
2978 found:
2979 ptype = cmd->args_type;
2980 for(i = 0; i < nb_args - 2; i++) {
2981 if (*ptype != '\0') {
2982 ptype++;
2983 while (*ptype == '?')
2984 ptype++;
2987 str = args[nb_args - 1];
2988 switch(*ptype) {
2989 case 'F':
2990 /* file completion */
2991 readline_set_completion_index(cur_mon->rs, strlen(str));
2992 file_completion(str);
2993 break;
2994 case 'B':
2995 /* block device name completion */
2996 readline_set_completion_index(cur_mon->rs, strlen(str));
2997 bdrv_iterate(block_completion_it, (void *)str);
2998 break;
2999 case 's':
3000 /* XXX: more generic ? */
3001 if (!strcmp(cmd->name, "info")) {
3002 readline_set_completion_index(cur_mon->rs, strlen(str));
3003 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3004 cmd_completion(str, cmd->name);
3006 } else if (!strcmp(cmd->name, "sendkey")) {
3007 char *sep = strrchr(str, '-');
3008 if (sep)
3009 str = sep + 1;
3010 readline_set_completion_index(cur_mon->rs, strlen(str));
3011 for(key = key_defs; key->name != NULL; key++) {
3012 cmd_completion(str, key->name);
3015 break;
3016 default:
3017 break;
3020 for(i = 0; i < nb_args; i++)
3021 qemu_free(args[i]);
3024 static int monitor_can_read(void *opaque)
3026 Monitor *mon = opaque;
3028 return (mon->suspend_cnt == 0) ? 128 : 0;
3031 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3033 Monitor *old_mon = cur_mon;
3034 int i;
3036 cur_mon = opaque;
3038 if (cur_mon->rs) {
3039 for (i = 0; i < size; i++)
3040 readline_handle_byte(cur_mon->rs, buf[i]);
3041 } else {
3042 if (size == 0 || buf[size - 1] != 0)
3043 monitor_printf(cur_mon, "corrupted command\n");
3044 else
3045 monitor_handle_command(cur_mon, (char *)buf);
3048 cur_mon = old_mon;
3051 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3053 monitor_suspend(mon);
3054 monitor_handle_command(mon, cmdline);
3055 monitor_resume(mon);
3058 int monitor_suspend(Monitor *mon)
3060 if (!mon->rs)
3061 return -ENOTTY;
3062 mon->suspend_cnt++;
3063 return 0;
3066 void monitor_resume(Monitor *mon)
3068 if (!mon->rs)
3069 return;
3070 if (--mon->suspend_cnt == 0)
3071 readline_show_prompt(mon->rs);
3074 static void monitor_event(void *opaque, int event)
3076 Monitor *mon = opaque;
3078 switch (event) {
3079 case CHR_EVENT_MUX_IN:
3080 readline_restart(mon->rs);
3081 monitor_resume(mon);
3082 monitor_flush(mon);
3083 break;
3085 case CHR_EVENT_MUX_OUT:
3086 if (mon->suspend_cnt == 0)
3087 monitor_printf(mon, "\n");
3088 monitor_flush(mon);
3089 monitor_suspend(mon);
3090 break;
3092 case CHR_EVENT_RESET:
3093 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3094 "information\n", QEMU_VERSION);
3095 if (mon->chr->focus == 0)
3096 readline_show_prompt(mon->rs);
3097 break;
3103 * Local variables:
3104 * c-indent-level: 4
3105 * c-basic-offset: 4
3106 * tab-width: 8
3107 * End:
3110 void monitor_init(CharDriverState *chr, int flags)
3112 static int is_first_init = 1;
3113 Monitor *mon;
3115 if (is_first_init) {
3116 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3117 is_first_init = 0;
3120 mon = qemu_mallocz(sizeof(*mon));
3122 mon->chr = chr;
3123 mon->flags = flags;
3124 if (mon->chr->focus != 0)
3125 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3126 if (flags & MONITOR_USE_READLINE) {
3127 mon->rs = readline_init(mon, monitor_find_completion);
3128 monitor_read_command(mon, 0);
3131 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3132 mon);
3134 LIST_INSERT_HEAD(&mon_list, mon, entry);
3135 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3136 cur_mon = mon;
3139 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3141 BlockDriverState *bs = opaque;
3142 int ret = 0;
3144 if (bdrv_set_key(bs, password) != 0) {
3145 monitor_printf(mon, "invalid password\n");
3146 ret = -EPERM;
3148 if (mon->password_completion_cb)
3149 mon->password_completion_cb(mon->password_opaque, ret);
3151 monitor_read_command(mon, 1);
3154 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3155 BlockDriverCompletionFunc *completion_cb,
3156 void *opaque)
3158 int err;
3160 if (!bdrv_key_required(bs)) {
3161 if (completion_cb)
3162 completion_cb(opaque, 0);
3163 return;
3166 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3167 bdrv_get_encrypted_filename(bs));
3169 mon->password_completion_cb = completion_cb;
3170 mon->password_opaque = opaque;
3172 err = monitor_read_password(mon, bdrv_password_cb, bs);
3174 if (err && completion_cb)
3175 completion_cb(opaque, err);