kvm: qemu: Make device assignment depend on libpci
[kvm-userspace.git] / qemu / monitor.c
blob76d9f49ef3b1597e24d2093f864641dfd432d1c8
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_stop(Monitor *mon)
552 vm_stop(EXCP_INTERRUPT);
555 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
557 struct bdrv_iterate_context {
558 Monitor *mon;
559 int err;
562 static void do_cont(Monitor *mon)
564 struct bdrv_iterate_context context = { mon, 0 };
566 bdrv_iterate(encrypted_bdrv_it, &context);
567 /* only resume the vm if all keys are set and valid */
568 if (!context.err)
569 vm_start();
572 static void bdrv_key_cb(void *opaque, int err)
574 Monitor *mon = opaque;
576 /* another key was set successfully, retry to continue */
577 if (!err)
578 do_cont(mon);
581 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
583 struct bdrv_iterate_context *context = opaque;
585 if (!context->err && bdrv_key_required(bs)) {
586 context->err = -EBUSY;
587 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
588 context->mon);
592 #ifdef CONFIG_GDBSTUB
593 static void do_gdbserver(Monitor *mon, const char *port)
595 if (!port)
596 port = DEFAULT_GDBSTUB_PORT;
597 if (gdbserver_start(port) < 0) {
598 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
599 port);
600 } else {
601 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
604 #endif
606 static void monitor_printc(Monitor *mon, int c)
608 monitor_printf(mon, "'");
609 switch(c) {
610 case '\'':
611 monitor_printf(mon, "\\'");
612 break;
613 case '\\':
614 monitor_printf(mon, "\\\\");
615 break;
616 case '\n':
617 monitor_printf(mon, "\\n");
618 break;
619 case '\r':
620 monitor_printf(mon, "\\r");
621 break;
622 default:
623 if (c >= 32 && c <= 126) {
624 monitor_printf(mon, "%c", c);
625 } else {
626 monitor_printf(mon, "\\x%02x", c);
628 break;
630 monitor_printf(mon, "'");
633 static void memory_dump(Monitor *mon, int count, int format, int wsize,
634 target_phys_addr_t addr, int is_physical)
636 CPUState *env;
637 int nb_per_line, l, line_size, i, max_digits, len;
638 uint8_t buf[16];
639 uint64_t v;
641 if (format == 'i') {
642 int flags;
643 flags = 0;
644 env = mon_get_cpu();
645 if (!env && !is_physical)
646 return;
647 #ifdef TARGET_I386
648 if (wsize == 2) {
649 flags = 1;
650 } else if (wsize == 4) {
651 flags = 0;
652 } else {
653 /* as default we use the current CS size */
654 flags = 0;
655 if (env) {
656 #ifdef TARGET_X86_64
657 if ((env->efer & MSR_EFER_LMA) &&
658 (env->segs[R_CS].flags & DESC_L_MASK))
659 flags = 2;
660 else
661 #endif
662 if (!(env->segs[R_CS].flags & DESC_B_MASK))
663 flags = 1;
666 #endif
667 monitor_disas(mon, env, addr, count, is_physical, flags);
668 return;
671 len = wsize * count;
672 if (wsize == 1)
673 line_size = 8;
674 else
675 line_size = 16;
676 nb_per_line = line_size / wsize;
677 max_digits = 0;
679 switch(format) {
680 case 'o':
681 max_digits = (wsize * 8 + 2) / 3;
682 break;
683 default:
684 case 'x':
685 max_digits = (wsize * 8) / 4;
686 break;
687 case 'u':
688 case 'd':
689 max_digits = (wsize * 8 * 10 + 32) / 33;
690 break;
691 case 'c':
692 wsize = 1;
693 break;
696 while (len > 0) {
697 if (is_physical)
698 monitor_printf(mon, TARGET_FMT_plx ":", addr);
699 else
700 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
701 l = len;
702 if (l > line_size)
703 l = line_size;
704 if (is_physical) {
705 cpu_physical_memory_rw(addr, buf, l, 0);
706 } else {
707 env = mon_get_cpu();
708 if (!env)
709 break;
710 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
711 monitor_printf(mon, " Cannot access memory\n");
712 break;
715 i = 0;
716 while (i < l) {
717 switch(wsize) {
718 default:
719 case 1:
720 v = ldub_raw(buf + i);
721 break;
722 case 2:
723 v = lduw_raw(buf + i);
724 break;
725 case 4:
726 v = (uint32_t)ldl_raw(buf + i);
727 break;
728 case 8:
729 v = ldq_raw(buf + i);
730 break;
732 monitor_printf(mon, " ");
733 switch(format) {
734 case 'o':
735 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
736 break;
737 case 'x':
738 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
739 break;
740 case 'u':
741 monitor_printf(mon, "%*" PRIu64, max_digits, v);
742 break;
743 case 'd':
744 monitor_printf(mon, "%*" PRId64, max_digits, v);
745 break;
746 case 'c':
747 monitor_printc(mon, v);
748 break;
750 i += wsize;
752 monitor_printf(mon, "\n");
753 addr += l;
754 len -= l;
758 #if TARGET_LONG_BITS == 64
759 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
760 #else
761 #define GET_TLONG(h, l) (l)
762 #endif
764 static void do_memory_dump(Monitor *mon, int count, int format, int size,
765 uint32_t addrh, uint32_t addrl)
767 target_long addr = GET_TLONG(addrh, addrl);
768 memory_dump(mon, count, format, size, addr, 0);
771 #if TARGET_PHYS_ADDR_BITS > 32
772 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
773 #else
774 #define GET_TPHYSADDR(h, l) (l)
775 #endif
777 static void do_physical_memory_dump(Monitor *mon, int count, int format,
778 int size, uint32_t addrh, uint32_t addrl)
781 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
782 memory_dump(mon, count, format, size, addr, 1);
785 static void do_print(Monitor *mon, int count, int format, int size,
786 unsigned int valh, unsigned int vall)
788 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
789 #if TARGET_PHYS_ADDR_BITS == 32
790 switch(format) {
791 case 'o':
792 monitor_printf(mon, "%#o", val);
793 break;
794 case 'x':
795 monitor_printf(mon, "%#x", val);
796 break;
797 case 'u':
798 monitor_printf(mon, "%u", val);
799 break;
800 default:
801 case 'd':
802 monitor_printf(mon, "%d", val);
803 break;
804 case 'c':
805 monitor_printc(mon, val);
806 break;
808 #else
809 switch(format) {
810 case 'o':
811 monitor_printf(mon, "%#" PRIo64, val);
812 break;
813 case 'x':
814 monitor_printf(mon, "%#" PRIx64, val);
815 break;
816 case 'u':
817 monitor_printf(mon, "%" PRIu64, val);
818 break;
819 default:
820 case 'd':
821 monitor_printf(mon, "%" PRId64, val);
822 break;
823 case 'c':
824 monitor_printc(mon, val);
825 break;
827 #endif
828 monitor_printf(mon, "\n");
831 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
832 uint32_t size, const char *filename)
834 FILE *f;
835 target_long addr = GET_TLONG(valh, vall);
836 uint32_t l;
837 CPUState *env;
838 uint8_t buf[1024];
840 env = mon_get_cpu();
841 if (!env)
842 return;
844 f = fopen(filename, "wb");
845 if (!f) {
846 monitor_printf(mon, "could not open '%s'\n", filename);
847 return;
849 while (size != 0) {
850 l = sizeof(buf);
851 if (l > size)
852 l = size;
853 cpu_memory_rw_debug(env, addr, buf, l, 0);
854 fwrite(buf, 1, l, f);
855 addr += l;
856 size -= l;
858 fclose(f);
861 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
862 unsigned int vall, uint32_t size,
863 const char *filename)
865 FILE *f;
866 uint32_t l;
867 uint8_t buf[1024];
868 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
870 f = fopen(filename, "wb");
871 if (!f) {
872 monitor_printf(mon, "could not open '%s'\n", filename);
873 return;
875 while (size != 0) {
876 l = sizeof(buf);
877 if (l > size)
878 l = size;
879 cpu_physical_memory_rw(addr, buf, l, 0);
880 fwrite(buf, 1, l, f);
881 fflush(f);
882 addr += l;
883 size -= l;
885 fclose(f);
888 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
890 uint32_t addr;
891 uint8_t buf[1];
892 uint16_t sum;
894 sum = 0;
895 for(addr = start; addr < (start + size); addr++) {
896 cpu_physical_memory_rw(addr, buf, 1, 0);
897 /* BSD sum algorithm ('sum' Unix command) */
898 sum = (sum >> 1) | (sum << 15);
899 sum += buf[0];
901 monitor_printf(mon, "%05d\n", sum);
904 typedef struct {
905 int keycode;
906 const char *name;
907 } KeyDef;
909 static const KeyDef key_defs[] = {
910 { 0x2a, "shift" },
911 { 0x36, "shift_r" },
913 { 0x38, "alt" },
914 { 0xb8, "alt_r" },
915 { 0x64, "altgr" },
916 { 0xe4, "altgr_r" },
917 { 0x1d, "ctrl" },
918 { 0x9d, "ctrl_r" },
920 { 0xdd, "menu" },
922 { 0x01, "esc" },
924 { 0x02, "1" },
925 { 0x03, "2" },
926 { 0x04, "3" },
927 { 0x05, "4" },
928 { 0x06, "5" },
929 { 0x07, "6" },
930 { 0x08, "7" },
931 { 0x09, "8" },
932 { 0x0a, "9" },
933 { 0x0b, "0" },
934 { 0x0c, "minus" },
935 { 0x0d, "equal" },
936 { 0x0e, "backspace" },
938 { 0x0f, "tab" },
939 { 0x10, "q" },
940 { 0x11, "w" },
941 { 0x12, "e" },
942 { 0x13, "r" },
943 { 0x14, "t" },
944 { 0x15, "y" },
945 { 0x16, "u" },
946 { 0x17, "i" },
947 { 0x18, "o" },
948 { 0x19, "p" },
950 { 0x1c, "ret" },
952 { 0x1e, "a" },
953 { 0x1f, "s" },
954 { 0x20, "d" },
955 { 0x21, "f" },
956 { 0x22, "g" },
957 { 0x23, "h" },
958 { 0x24, "j" },
959 { 0x25, "k" },
960 { 0x26, "l" },
962 { 0x2c, "z" },
963 { 0x2d, "x" },
964 { 0x2e, "c" },
965 { 0x2f, "v" },
966 { 0x30, "b" },
967 { 0x31, "n" },
968 { 0x32, "m" },
969 { 0x33, "comma" },
970 { 0x34, "dot" },
971 { 0x35, "slash" },
973 { 0x37, "asterisk" },
975 { 0x39, "spc" },
976 { 0x3a, "caps_lock" },
977 { 0x3b, "f1" },
978 { 0x3c, "f2" },
979 { 0x3d, "f3" },
980 { 0x3e, "f4" },
981 { 0x3f, "f5" },
982 { 0x40, "f6" },
983 { 0x41, "f7" },
984 { 0x42, "f8" },
985 { 0x43, "f9" },
986 { 0x44, "f10" },
987 { 0x45, "num_lock" },
988 { 0x46, "scroll_lock" },
990 { 0xb5, "kp_divide" },
991 { 0x37, "kp_multiply" },
992 { 0x4a, "kp_subtract" },
993 { 0x4e, "kp_add" },
994 { 0x9c, "kp_enter" },
995 { 0x53, "kp_decimal" },
996 { 0x54, "sysrq" },
998 { 0x52, "kp_0" },
999 { 0x4f, "kp_1" },
1000 { 0x50, "kp_2" },
1001 { 0x51, "kp_3" },
1002 { 0x4b, "kp_4" },
1003 { 0x4c, "kp_5" },
1004 { 0x4d, "kp_6" },
1005 { 0x47, "kp_7" },
1006 { 0x48, "kp_8" },
1007 { 0x49, "kp_9" },
1009 { 0x56, "<" },
1011 { 0x57, "f11" },
1012 { 0x58, "f12" },
1014 { 0xb7, "print" },
1016 { 0xc7, "home" },
1017 { 0xc9, "pgup" },
1018 { 0xd1, "pgdn" },
1019 { 0xcf, "end" },
1021 { 0xcb, "left" },
1022 { 0xc8, "up" },
1023 { 0xd0, "down" },
1024 { 0xcd, "right" },
1026 { 0xd2, "insert" },
1027 { 0xd3, "delete" },
1028 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1029 { 0xf0, "stop" },
1030 { 0xf1, "again" },
1031 { 0xf2, "props" },
1032 { 0xf3, "undo" },
1033 { 0xf4, "front" },
1034 { 0xf5, "copy" },
1035 { 0xf6, "open" },
1036 { 0xf7, "paste" },
1037 { 0xf8, "find" },
1038 { 0xf9, "cut" },
1039 { 0xfa, "lf" },
1040 { 0xfb, "help" },
1041 { 0xfc, "meta_l" },
1042 { 0xfd, "meta_r" },
1043 { 0xfe, "compose" },
1044 #endif
1045 { 0, NULL },
1048 static int get_keycode(const char *key)
1050 const KeyDef *p;
1051 char *endp;
1052 int ret;
1054 for(p = key_defs; p->name != NULL; p++) {
1055 if (!strcmp(key, p->name))
1056 return p->keycode;
1058 if (strstart(key, "0x", NULL)) {
1059 ret = strtoul(key, &endp, 0);
1060 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1061 return ret;
1063 return -1;
1066 #define MAX_KEYCODES 16
1067 static uint8_t keycodes[MAX_KEYCODES];
1068 static int nb_pending_keycodes;
1069 static QEMUTimer *key_timer;
1071 static void release_keys(void *opaque)
1073 int keycode;
1075 while (nb_pending_keycodes > 0) {
1076 nb_pending_keycodes--;
1077 keycode = keycodes[nb_pending_keycodes];
1078 if (keycode & 0x80)
1079 kbd_put_keycode(0xe0);
1080 kbd_put_keycode(keycode | 0x80);
1084 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1085 int hold_time)
1087 char keyname_buf[16];
1088 char *separator;
1089 int keyname_len, keycode, i;
1091 if (nb_pending_keycodes > 0) {
1092 qemu_del_timer(key_timer);
1093 release_keys(NULL);
1095 if (!has_hold_time)
1096 hold_time = 100;
1097 i = 0;
1098 while (1) {
1099 separator = strchr(string, '-');
1100 keyname_len = separator ? separator - string : strlen(string);
1101 if (keyname_len > 0) {
1102 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1103 if (keyname_len > sizeof(keyname_buf) - 1) {
1104 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1105 return;
1107 if (i == MAX_KEYCODES) {
1108 monitor_printf(mon, "too many keys\n");
1109 return;
1111 keyname_buf[keyname_len] = 0;
1112 keycode = get_keycode(keyname_buf);
1113 if (keycode < 0) {
1114 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1115 return;
1117 keycodes[i++] = keycode;
1119 if (!separator)
1120 break;
1121 string = separator + 1;
1123 nb_pending_keycodes = i;
1124 /* key down events */
1125 for (i = 0; i < nb_pending_keycodes; i++) {
1126 keycode = keycodes[i];
1127 if (keycode & 0x80)
1128 kbd_put_keycode(0xe0);
1129 kbd_put_keycode(keycode & 0x7f);
1131 /* delayed key up events */
1132 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1133 muldiv64(ticks_per_sec, hold_time, 1000));
1136 static int mouse_button_state;
1138 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1139 const char *dz_str)
1141 int dx, dy, dz;
1142 dx = strtol(dx_str, NULL, 0);
1143 dy = strtol(dy_str, NULL, 0);
1144 dz = 0;
1145 if (dz_str)
1146 dz = strtol(dz_str, NULL, 0);
1147 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1150 static void do_mouse_button(Monitor *mon, int button_state)
1152 mouse_button_state = button_state;
1153 kbd_mouse_event(0, 0, 0, mouse_button_state);
1156 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1157 int addr, int has_index, int index)
1159 uint32_t val;
1160 int suffix;
1162 if (has_index) {
1163 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1164 addr++;
1166 addr &= 0xffff;
1168 switch(size) {
1169 default:
1170 case 1:
1171 val = cpu_inb(NULL, addr);
1172 suffix = 'b';
1173 break;
1174 case 2:
1175 val = cpu_inw(NULL, addr);
1176 suffix = 'w';
1177 break;
1178 case 4:
1179 val = cpu_inl(NULL, addr);
1180 suffix = 'l';
1181 break;
1183 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1184 suffix, addr, size * 2, val);
1187 /* boot_set handler */
1188 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1189 static void *boot_opaque;
1191 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1193 qemu_boot_set_handler = func;
1194 boot_opaque = opaque;
1197 static void do_boot_set(Monitor *mon, const char *bootdevice)
1199 int res;
1201 if (qemu_boot_set_handler) {
1202 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1203 if (res == 0)
1204 monitor_printf(mon, "boot device list now set to %s\n",
1205 bootdevice);
1206 else
1207 monitor_printf(mon, "setting boot device list failed with "
1208 "error %i\n", res);
1209 } else {
1210 monitor_printf(mon, "no function defined to set boot device list for "
1211 "this architecture\n");
1215 static void do_system_reset(Monitor *mon)
1217 qemu_system_reset_request();
1220 static void do_system_powerdown(Monitor *mon)
1222 qemu_system_powerdown_request();
1225 #if defined(TARGET_I386)
1226 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1228 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1229 addr,
1230 pte & mask,
1231 pte & PG_GLOBAL_MASK ? 'G' : '-',
1232 pte & PG_PSE_MASK ? 'P' : '-',
1233 pte & PG_DIRTY_MASK ? 'D' : '-',
1234 pte & PG_ACCESSED_MASK ? 'A' : '-',
1235 pte & PG_PCD_MASK ? 'C' : '-',
1236 pte & PG_PWT_MASK ? 'T' : '-',
1237 pte & PG_USER_MASK ? 'U' : '-',
1238 pte & PG_RW_MASK ? 'W' : '-');
1241 static void tlb_info(Monitor *mon)
1243 CPUState *env;
1244 int l1, l2;
1245 uint32_t pgd, pde, pte;
1247 env = mon_get_cpu();
1248 if (!env)
1249 return;
1251 if (!(env->cr[0] & CR0_PG_MASK)) {
1252 monitor_printf(mon, "PG disabled\n");
1253 return;
1255 pgd = env->cr[3] & ~0xfff;
1256 for(l1 = 0; l1 < 1024; l1++) {
1257 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1258 pde = le32_to_cpu(pde);
1259 if (pde & PG_PRESENT_MASK) {
1260 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1261 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1262 } else {
1263 for(l2 = 0; l2 < 1024; l2++) {
1264 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1265 (uint8_t *)&pte, 4);
1266 pte = le32_to_cpu(pte);
1267 if (pte & PG_PRESENT_MASK) {
1268 print_pte(mon, (l1 << 22) + (l2 << 12),
1269 pte & ~PG_PSE_MASK,
1270 ~0xfff);
1278 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1279 uint32_t end, int prot)
1281 int prot1;
1282 prot1 = *plast_prot;
1283 if (prot != prot1) {
1284 if (*pstart != -1) {
1285 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1286 *pstart, end, end - *pstart,
1287 prot1 & PG_USER_MASK ? 'u' : '-',
1288 'r',
1289 prot1 & PG_RW_MASK ? 'w' : '-');
1291 if (prot != 0)
1292 *pstart = end;
1293 else
1294 *pstart = -1;
1295 *plast_prot = prot;
1299 static void mem_info(Monitor *mon)
1301 CPUState *env;
1302 int l1, l2, prot, last_prot;
1303 uint32_t pgd, pde, pte, start, end;
1305 env = mon_get_cpu();
1306 if (!env)
1307 return;
1309 if (!(env->cr[0] & CR0_PG_MASK)) {
1310 monitor_printf(mon, "PG disabled\n");
1311 return;
1313 pgd = env->cr[3] & ~0xfff;
1314 last_prot = 0;
1315 start = -1;
1316 for(l1 = 0; l1 < 1024; l1++) {
1317 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1318 pde = le32_to_cpu(pde);
1319 end = l1 << 22;
1320 if (pde & PG_PRESENT_MASK) {
1321 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1322 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1323 mem_print(mon, &start, &last_prot, end, prot);
1324 } else {
1325 for(l2 = 0; l2 < 1024; l2++) {
1326 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1327 (uint8_t *)&pte, 4);
1328 pte = le32_to_cpu(pte);
1329 end = (l1 << 22) + (l2 << 12);
1330 if (pte & PG_PRESENT_MASK) {
1331 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1332 } else {
1333 prot = 0;
1335 mem_print(mon, &start, &last_prot, end, prot);
1338 } else {
1339 prot = 0;
1340 mem_print(mon, &start, &last_prot, end, prot);
1344 #endif
1346 #if defined(TARGET_SH4)
1348 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1350 monitor_printf(mon, " tlb%i:\t"
1351 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1352 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1353 "dirty=%hhu writethrough=%hhu\n",
1354 idx,
1355 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1356 tlb->v, tlb->sh, tlb->c, tlb->pr,
1357 tlb->d, tlb->wt);
1360 static void tlb_info(Monitor *mon)
1362 CPUState *env = mon_get_cpu();
1363 int i;
1365 monitor_printf (mon, "ITLB:\n");
1366 for (i = 0 ; i < ITLB_SIZE ; i++)
1367 print_tlb (mon, i, &env->itlb[i]);
1368 monitor_printf (mon, "UTLB:\n");
1369 for (i = 0 ; i < UTLB_SIZE ; i++)
1370 print_tlb (mon, i, &env->utlb[i]);
1373 #endif
1375 static void do_info_kqemu(Monitor *mon)
1377 #ifdef USE_KQEMU
1378 CPUState *env;
1379 int val;
1380 val = 0;
1381 env = mon_get_cpu();
1382 if (!env) {
1383 monitor_printf(mon, "No cpu initialized yet");
1384 return;
1386 val = env->kqemu_enabled;
1387 monitor_printf(mon, "kqemu support: ");
1388 switch(val) {
1389 default:
1390 case 0:
1391 monitor_printf(mon, "disabled\n");
1392 break;
1393 case 1:
1394 monitor_printf(mon, "enabled for user code\n");
1395 break;
1396 case 2:
1397 monitor_printf(mon, "enabled for user and kernel code\n");
1398 break;
1400 #else
1401 monitor_printf(mon, "kqemu support: not compiled\n");
1402 #endif
1405 static void do_info_kvm(Monitor *mon)
1407 #if defined(USE_KVM) || defined(CONFIG_KVM)
1408 monitor_printf(mon, "kvm support: ");
1409 if (kvm_enabled())
1410 monitor_printf(mon, "enabled\n");
1411 else
1412 monitor_printf(mon, "disabled\n");
1413 #else
1414 monitor_printf(mon, "kvm support: not compiled\n");
1415 #endif
1418 #ifdef CONFIG_PROFILER
1420 int64_t kqemu_time;
1421 int64_t qemu_time;
1422 int64_t kqemu_exec_count;
1423 int64_t dev_time;
1424 int64_t kqemu_ret_int_count;
1425 int64_t kqemu_ret_excp_count;
1426 int64_t kqemu_ret_intr_count;
1428 static void do_info_profile(Monitor *mon)
1430 int64_t total;
1431 total = qemu_time;
1432 if (total == 0)
1433 total = 1;
1434 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1435 dev_time, dev_time / (double)ticks_per_sec);
1436 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1437 qemu_time, qemu_time / (double)ticks_per_sec);
1438 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1439 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1440 PRId64 "\n",
1441 kqemu_time, kqemu_time / (double)ticks_per_sec,
1442 kqemu_time / (double)total * 100.0,
1443 kqemu_exec_count,
1444 kqemu_ret_int_count,
1445 kqemu_ret_excp_count,
1446 kqemu_ret_intr_count);
1447 qemu_time = 0;
1448 kqemu_time = 0;
1449 kqemu_exec_count = 0;
1450 dev_time = 0;
1451 kqemu_ret_int_count = 0;
1452 kqemu_ret_excp_count = 0;
1453 kqemu_ret_intr_count = 0;
1454 #ifdef USE_KQEMU
1455 kqemu_record_dump();
1456 #endif
1458 #else
1459 static void do_info_profile(Monitor *mon)
1461 monitor_printf(mon, "Internal profiler not compiled\n");
1463 #endif
1465 /* Capture support */
1466 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1468 static void do_info_capture(Monitor *mon)
1470 int i;
1471 CaptureState *s;
1473 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1474 monitor_printf(mon, "[%d]: ", i);
1475 s->ops.info (s->opaque);
1479 static void do_stop_capture(Monitor *mon, int n)
1481 int i;
1482 CaptureState *s;
1484 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1485 if (i == n) {
1486 s->ops.destroy (s->opaque);
1487 LIST_REMOVE (s, entries);
1488 qemu_free (s);
1489 return;
1494 #ifdef HAS_AUDIO
1495 static void do_wav_capture(Monitor *mon, const char *path,
1496 int has_freq, int freq,
1497 int has_bits, int bits,
1498 int has_channels, int nchannels)
1500 CaptureState *s;
1502 s = qemu_mallocz (sizeof (*s));
1504 freq = has_freq ? freq : 44100;
1505 bits = has_bits ? bits : 16;
1506 nchannels = has_channels ? nchannels : 2;
1508 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1509 monitor_printf(mon, "Faied to add wave capture\n");
1510 qemu_free (s);
1512 LIST_INSERT_HEAD (&capture_head, s, entries);
1514 #endif
1516 #if defined(TARGET_I386)
1517 static void do_inject_nmi(Monitor *mon, int cpu_index)
1519 CPUState *env;
1521 for (env = first_cpu; env != NULL; env = env->next_cpu)
1522 if (env->cpu_index == cpu_index) {
1523 if (kvm_enabled())
1524 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1525 else
1526 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1527 break;
1530 #endif
1532 static void do_info_status(Monitor *mon)
1534 if (vm_running)
1535 monitor_printf(mon, "VM status: running\n");
1536 else
1537 monitor_printf(mon, "VM status: paused\n");
1541 static void do_balloon(Monitor *mon, int value)
1543 ram_addr_t target = value;
1544 qemu_balloon(target << 20);
1547 static void do_info_balloon(Monitor *mon)
1549 ram_addr_t actual;
1551 actual = qemu_balloon_status();
1552 if (kvm_enabled() && !kvm_has_sync_mmu())
1553 monitor_printf(mon, "Using KVM without synchronous MMU, "
1554 "ballooning disabled\n");
1555 else if (actual == 0)
1556 monitor_printf(mon, "Ballooning not activated in VM\n");
1557 else
1558 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1561 static void do_acl(Monitor *mon,
1562 const char *command,
1563 const char *aclname,
1564 const char *match,
1565 int has_index,
1566 int index)
1568 qemu_acl *acl;
1570 acl = qemu_acl_find(aclname);
1571 if (!acl) {
1572 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1573 return;
1576 if (strcmp(command, "show") == 0) {
1577 int i = 0;
1578 qemu_acl_entry *entry;
1579 monitor_printf(mon, "policy: %s\n",
1580 acl->defaultDeny ? "deny" : "allow");
1581 TAILQ_FOREACH(entry, &acl->entries, next) {
1582 i++;
1583 monitor_printf(mon, "%d: %s %s\n", i,
1584 entry->deny ? "deny" : "allow",
1585 entry->match);
1587 } else if (strcmp(command, "reset") == 0) {
1588 qemu_acl_reset(acl);
1589 monitor_printf(mon, "acl: removed all rules\n");
1590 } else if (strcmp(command, "policy") == 0) {
1591 if (!match) {
1592 monitor_printf(mon, "acl: missing policy parameter\n");
1593 return;
1596 if (strcmp(match, "allow") == 0) {
1597 acl->defaultDeny = 0;
1598 monitor_printf(mon, "acl: policy set to 'allow'\n");
1599 } else if (strcmp(match, "deny") == 0) {
1600 acl->defaultDeny = 1;
1601 monitor_printf(mon, "acl: policy set to 'deny'\n");
1602 } else {
1603 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1605 } else if ((strcmp(command, "allow") == 0) ||
1606 (strcmp(command, "deny") == 0)) {
1607 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1608 int ret;
1610 if (!match) {
1611 monitor_printf(mon, "acl: missing match parameter\n");
1612 return;
1615 if (has_index)
1616 ret = qemu_acl_insert(acl, deny, match, index);
1617 else
1618 ret = qemu_acl_append(acl, deny, match);
1619 if (ret < 0)
1620 monitor_printf(mon, "acl: unable to add acl entry\n");
1621 else
1622 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1623 } else if (strcmp(command, "remove") == 0) {
1624 int ret;
1626 if (!match) {
1627 monitor_printf(mon, "acl: missing match parameter\n");
1628 return;
1631 ret = qemu_acl_remove(acl, match);
1632 if (ret < 0)
1633 monitor_printf(mon, "acl: no matching acl entry\n");
1634 else
1635 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1636 } else {
1637 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1641 /* Please update qemu-doc.texi when adding or changing commands */
1642 static const mon_cmd_t mon_cmds[] = {
1643 { "help|?", "s?", help_cmd,
1644 "[cmd]", "show the help" },
1645 { "commit", "s", do_commit,
1646 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1647 { "info", "s?", do_info,
1648 "subcommand", "show various information about the system state" },
1649 { "q|quit", "", do_quit,
1650 "", "quit the emulator" },
1651 { "eject", "-fB", do_eject,
1652 "[-f] device", "eject a removable medium (use -f to force it)" },
1653 { "change", "BFs?", do_change,
1654 "device filename [format]", "change a removable medium, optional format" },
1655 { "screendump", "F", do_screen_dump,
1656 "filename", "save screen into PPM image 'filename'" },
1657 { "logfile", "F", do_logfile,
1658 "filename", "output logs to 'filename'" },
1659 { "log", "s", do_log,
1660 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1661 { "savevm", "s?", do_savevm,
1662 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1663 { "loadvm", "s", do_loadvm,
1664 "tag|id", "restore a VM snapshot from its tag or id" },
1665 { "delvm", "s", do_delvm,
1666 "tag|id", "delete a VM snapshot from its tag or id" },
1667 { "stop", "", do_stop,
1668 "", "stop emulation", },
1669 { "c|cont", "", do_cont,
1670 "", "resume emulation", },
1671 #ifdef CONFIG_GDBSTUB
1672 { "gdbserver", "s?", do_gdbserver,
1673 "[port]", "start gdbserver session (default port=1234)", },
1674 #endif
1675 { "x", "/l", do_memory_dump,
1676 "/fmt addr", "virtual memory dump starting at 'addr'", },
1677 { "xp", "/l", do_physical_memory_dump,
1678 "/fmt addr", "physical memory dump starting at 'addr'", },
1679 { "p|print", "/l", do_print,
1680 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1681 { "i", "/ii.", do_ioport_read,
1682 "/fmt addr", "I/O port read" },
1684 { "sendkey", "si?", do_sendkey,
1685 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1686 { "system_reset", "", do_system_reset,
1687 "", "reset the system" },
1688 { "system_powerdown", "", do_system_powerdown,
1689 "", "send system power down event" },
1690 { "sum", "ii", do_sum,
1691 "addr size", "compute the checksum of a memory region" },
1692 { "usb_add", "s", do_usb_add,
1693 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1694 { "usb_del", "s", do_usb_del,
1695 "device", "remove USB device 'bus.addr'" },
1696 { "cpu", "i", do_cpu_set,
1697 "index", "set the default CPU" },
1698 { "mouse_move", "sss?", do_mouse_move,
1699 "dx dy [dz]", "send mouse move events" },
1700 { "mouse_button", "i", do_mouse_button,
1701 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1702 { "mouse_set", "i", do_mouse_set,
1703 "index", "set which mouse device receives events" },
1704 #ifdef HAS_AUDIO
1705 { "wavcapture", "si?i?i?", do_wav_capture,
1706 "path [frequency bits channels]",
1707 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1708 #endif
1709 { "stopcapture", "i", do_stop_capture,
1710 "capture index", "stop capture" },
1711 { "memsave", "lis", do_memory_save,
1712 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1713 { "pmemsave", "lis", do_physical_memory_save,
1714 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1715 { "boot_set", "s", do_boot_set,
1716 "bootdevice", "define new values for the boot device list" },
1717 #if defined(TARGET_I386)
1718 { "nmi", "i", do_inject_nmi,
1719 "cpu", "inject an NMI on the given CPU", },
1720 #endif
1721 { "migrate", "-ds", do_migrate,
1722 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1723 { "migrate_cancel", "", do_migrate_cancel,
1724 "", "cancel the current VM migration" },
1725 { "migrate_set_speed", "s", do_migrate_set_speed,
1726 "value", "set maximum speed (in bytes) for migrations" },
1727 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1728 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1729 "[file=file][,if=type][,bus=n]\n"
1730 "[,unit=m][,media=d][index=i]\n"
1731 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1732 "[snapshot=on|off][,cache=on|off]",
1733 "add drive to PCI storage controller" },
1734 { "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" },
1735 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1736 { "host_net_add", "ss", net_host_device_add,
1737 "[tap,user,socket,vde] options", "add host VLAN client" },
1738 { "host_net_remove", "is", net_host_device_remove,
1739 "vlan_id name", "remove host VLAN client" },
1740 #endif
1741 { "balloon", "i", do_balloon,
1742 "target", "request VM to change it's memory allocation (in MB)" },
1743 { "set_link", "ss", do_set_link,
1744 "name [up|down]", "change the link status of a network adapter" },
1745 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1746 "acl show vnc.username\n"
1747 "acl policy vnc.username deny\n"
1748 "acl allow vnc.username fred\n"
1749 "acl deny vnc.username bob\n"
1750 "acl reset vnc.username\n" },
1751 { "set_link", "ss", do_set_link, "name [up|down]" },
1752 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1753 { NULL, NULL, },
1756 /* Please update qemu-doc.texi when adding or changing commands */
1757 static const mon_cmd_t info_cmds[] = {
1758 { "version", "", do_info_version,
1759 "", "show the version of QEMU" },
1760 { "network", "", do_info_network,
1761 "", "show the network state" },
1762 { "chardev", "", qemu_chr_info,
1763 "", "show the character devices" },
1764 { "block", "", bdrv_info,
1765 "", "show the block devices" },
1766 { "blockstats", "", bdrv_info_stats,
1767 "", "show block device statistics" },
1768 { "registers", "", do_info_registers,
1769 "", "show the cpu registers" },
1770 { "cpus", "", do_info_cpus,
1771 "", "show infos for each CPU" },
1772 { "history", "", do_info_history,
1773 "", "show the command line history", },
1774 { "irq", "", irq_info,
1775 "", "show the interrupts statistics (if available)", },
1776 { "pic", "", pic_info,
1777 "", "show i8259 (PIC) state", },
1778 { "pci", "", pci_info,
1779 "", "show PCI info", },
1780 #if defined(TARGET_I386) || defined(TARGET_SH4)
1781 { "tlb", "", tlb_info,
1782 "", "show virtual to physical memory mappings", },
1783 #endif
1784 #if defined(TARGET_I386)
1785 { "mem", "", mem_info,
1786 "", "show the active virtual memory mappings", },
1787 { "hpet", "", do_info_hpet,
1788 "", "show state of HPET", },
1789 #endif
1790 { "jit", "", do_info_jit,
1791 "", "show dynamic compiler info", },
1792 { "kqemu", "", do_info_kqemu,
1793 "", "show KQEMU information", },
1794 { "kvm", "", do_info_kvm,
1795 "", "show KVM information", },
1796 { "usb", "", usb_info,
1797 "", "show guest USB devices", },
1798 { "usbhost", "", usb_host_info,
1799 "", "show host USB devices", },
1800 { "profile", "", do_info_profile,
1801 "", "show profiling information", },
1802 { "capture", "", do_info_capture,
1803 "", "show capture information" },
1804 { "snapshots", "", do_info_snapshots,
1805 "", "show the currently saved VM snapshots" },
1806 { "status", "", do_info_status,
1807 "", "show the current VM status (running|paused)" },
1808 { "pcmcia", "", pcmcia_info,
1809 "", "show guest PCMCIA status" },
1810 { "mice", "", do_info_mice,
1811 "", "show which guest mouse is receiving events" },
1812 { "vnc", "", do_info_vnc,
1813 "", "show the vnc server status"},
1814 { "name", "", do_info_name,
1815 "", "show the current VM name" },
1816 { "uuid", "", do_info_uuid,
1817 "", "show the current VM UUID" },
1818 #if defined(TARGET_PPC)
1819 { "cpustats", "", do_info_cpu_stats,
1820 "", "show CPU statistics", },
1821 #endif
1822 #if defined(CONFIG_SLIRP)
1823 { "slirp", "", do_info_slirp,
1824 "", "show SLIRP statistics", },
1825 #endif
1826 { "migrate", "", do_info_migrate, "", "show migration status" },
1827 { "balloon", "", do_info_balloon,
1828 "", "show balloon information" },
1829 { NULL, NULL, },
1832 /*******************************************************************/
1834 static const char *pch;
1835 static jmp_buf expr_env;
1837 #define MD_TLONG 0
1838 #define MD_I32 1
1840 typedef struct MonitorDef {
1841 const char *name;
1842 int offset;
1843 target_long (*get_value)(const struct MonitorDef *md, int val);
1844 int type;
1845 } MonitorDef;
1847 #if defined(TARGET_I386)
1848 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1850 CPUState *env = mon_get_cpu();
1851 if (!env)
1852 return 0;
1853 return env->eip + env->segs[R_CS].base;
1855 #endif
1857 #if defined(TARGET_PPC)
1858 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1860 CPUState *env = mon_get_cpu();
1861 unsigned int u;
1862 int i;
1864 if (!env)
1865 return 0;
1867 u = 0;
1868 for (i = 0; i < 8; i++)
1869 u |= env->crf[i] << (32 - (4 * i));
1871 return u;
1874 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1876 CPUState *env = mon_get_cpu();
1877 if (!env)
1878 return 0;
1879 return env->msr;
1882 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1884 CPUState *env = mon_get_cpu();
1885 if (!env)
1886 return 0;
1887 return env->xer;
1890 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1892 CPUState *env = mon_get_cpu();
1893 if (!env)
1894 return 0;
1895 return cpu_ppc_load_decr(env);
1898 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1900 CPUState *env = mon_get_cpu();
1901 if (!env)
1902 return 0;
1903 return cpu_ppc_load_tbu(env);
1906 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1908 CPUState *env = mon_get_cpu();
1909 if (!env)
1910 return 0;
1911 return cpu_ppc_load_tbl(env);
1913 #endif
1915 #if defined(TARGET_SPARC)
1916 #ifndef TARGET_SPARC64
1917 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1919 CPUState *env = mon_get_cpu();
1920 if (!env)
1921 return 0;
1922 return GET_PSR(env);
1924 #endif
1926 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1928 CPUState *env = mon_get_cpu();
1929 if (!env)
1930 return 0;
1931 return env->regwptr[val];
1933 #endif
1935 static const MonitorDef monitor_defs[] = {
1936 #ifdef TARGET_I386
1938 #define SEG(name, seg) \
1939 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1940 { name ".base", offsetof(CPUState, segs[seg].base) },\
1941 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1943 { "eax", offsetof(CPUState, regs[0]) },
1944 { "ecx", offsetof(CPUState, regs[1]) },
1945 { "edx", offsetof(CPUState, regs[2]) },
1946 { "ebx", offsetof(CPUState, regs[3]) },
1947 { "esp|sp", offsetof(CPUState, regs[4]) },
1948 { "ebp|fp", offsetof(CPUState, regs[5]) },
1949 { "esi", offsetof(CPUState, regs[6]) },
1950 { "edi", offsetof(CPUState, regs[7]) },
1951 #ifdef TARGET_X86_64
1952 { "r8", offsetof(CPUState, regs[8]) },
1953 { "r9", offsetof(CPUState, regs[9]) },
1954 { "r10", offsetof(CPUState, regs[10]) },
1955 { "r11", offsetof(CPUState, regs[11]) },
1956 { "r12", offsetof(CPUState, regs[12]) },
1957 { "r13", offsetof(CPUState, regs[13]) },
1958 { "r14", offsetof(CPUState, regs[14]) },
1959 { "r15", offsetof(CPUState, regs[15]) },
1960 #endif
1961 { "eflags", offsetof(CPUState, eflags) },
1962 { "eip", offsetof(CPUState, eip) },
1963 SEG("cs", R_CS)
1964 SEG("ds", R_DS)
1965 SEG("es", R_ES)
1966 SEG("ss", R_SS)
1967 SEG("fs", R_FS)
1968 SEG("gs", R_GS)
1969 { "pc", 0, monitor_get_pc, },
1970 #elif defined(TARGET_PPC)
1971 /* General purpose registers */
1972 { "r0", offsetof(CPUState, gpr[0]) },
1973 { "r1", offsetof(CPUState, gpr[1]) },
1974 { "r2", offsetof(CPUState, gpr[2]) },
1975 { "r3", offsetof(CPUState, gpr[3]) },
1976 { "r4", offsetof(CPUState, gpr[4]) },
1977 { "r5", offsetof(CPUState, gpr[5]) },
1978 { "r6", offsetof(CPUState, gpr[6]) },
1979 { "r7", offsetof(CPUState, gpr[7]) },
1980 { "r8", offsetof(CPUState, gpr[8]) },
1981 { "r9", offsetof(CPUState, gpr[9]) },
1982 { "r10", offsetof(CPUState, gpr[10]) },
1983 { "r11", offsetof(CPUState, gpr[11]) },
1984 { "r12", offsetof(CPUState, gpr[12]) },
1985 { "r13", offsetof(CPUState, gpr[13]) },
1986 { "r14", offsetof(CPUState, gpr[14]) },
1987 { "r15", offsetof(CPUState, gpr[15]) },
1988 { "r16", offsetof(CPUState, gpr[16]) },
1989 { "r17", offsetof(CPUState, gpr[17]) },
1990 { "r18", offsetof(CPUState, gpr[18]) },
1991 { "r19", offsetof(CPUState, gpr[19]) },
1992 { "r20", offsetof(CPUState, gpr[20]) },
1993 { "r21", offsetof(CPUState, gpr[21]) },
1994 { "r22", offsetof(CPUState, gpr[22]) },
1995 { "r23", offsetof(CPUState, gpr[23]) },
1996 { "r24", offsetof(CPUState, gpr[24]) },
1997 { "r25", offsetof(CPUState, gpr[25]) },
1998 { "r26", offsetof(CPUState, gpr[26]) },
1999 { "r27", offsetof(CPUState, gpr[27]) },
2000 { "r28", offsetof(CPUState, gpr[28]) },
2001 { "r29", offsetof(CPUState, gpr[29]) },
2002 { "r30", offsetof(CPUState, gpr[30]) },
2003 { "r31", offsetof(CPUState, gpr[31]) },
2004 /* Floating point registers */
2005 { "f0", offsetof(CPUState, fpr[0]) },
2006 { "f1", offsetof(CPUState, fpr[1]) },
2007 { "f2", offsetof(CPUState, fpr[2]) },
2008 { "f3", offsetof(CPUState, fpr[3]) },
2009 { "f4", offsetof(CPUState, fpr[4]) },
2010 { "f5", offsetof(CPUState, fpr[5]) },
2011 { "f6", offsetof(CPUState, fpr[6]) },
2012 { "f7", offsetof(CPUState, fpr[7]) },
2013 { "f8", offsetof(CPUState, fpr[8]) },
2014 { "f9", offsetof(CPUState, fpr[9]) },
2015 { "f10", offsetof(CPUState, fpr[10]) },
2016 { "f11", offsetof(CPUState, fpr[11]) },
2017 { "f12", offsetof(CPUState, fpr[12]) },
2018 { "f13", offsetof(CPUState, fpr[13]) },
2019 { "f14", offsetof(CPUState, fpr[14]) },
2020 { "f15", offsetof(CPUState, fpr[15]) },
2021 { "f16", offsetof(CPUState, fpr[16]) },
2022 { "f17", offsetof(CPUState, fpr[17]) },
2023 { "f18", offsetof(CPUState, fpr[18]) },
2024 { "f19", offsetof(CPUState, fpr[19]) },
2025 { "f20", offsetof(CPUState, fpr[20]) },
2026 { "f21", offsetof(CPUState, fpr[21]) },
2027 { "f22", offsetof(CPUState, fpr[22]) },
2028 { "f23", offsetof(CPUState, fpr[23]) },
2029 { "f24", offsetof(CPUState, fpr[24]) },
2030 { "f25", offsetof(CPUState, fpr[25]) },
2031 { "f26", offsetof(CPUState, fpr[26]) },
2032 { "f27", offsetof(CPUState, fpr[27]) },
2033 { "f28", offsetof(CPUState, fpr[28]) },
2034 { "f29", offsetof(CPUState, fpr[29]) },
2035 { "f30", offsetof(CPUState, fpr[30]) },
2036 { "f31", offsetof(CPUState, fpr[31]) },
2037 { "fpscr", offsetof(CPUState, fpscr) },
2038 /* Next instruction pointer */
2039 { "nip|pc", offsetof(CPUState, nip) },
2040 { "lr", offsetof(CPUState, lr) },
2041 { "ctr", offsetof(CPUState, ctr) },
2042 { "decr", 0, &monitor_get_decr, },
2043 { "ccr", 0, &monitor_get_ccr, },
2044 /* Machine state register */
2045 { "msr", 0, &monitor_get_msr, },
2046 { "xer", 0, &monitor_get_xer, },
2047 { "tbu", 0, &monitor_get_tbu, },
2048 { "tbl", 0, &monitor_get_tbl, },
2049 #if defined(TARGET_PPC64)
2050 /* Address space register */
2051 { "asr", offsetof(CPUState, asr) },
2052 #endif
2053 /* Segment registers */
2054 { "sdr1", offsetof(CPUState, sdr1) },
2055 { "sr0", offsetof(CPUState, sr[0]) },
2056 { "sr1", offsetof(CPUState, sr[1]) },
2057 { "sr2", offsetof(CPUState, sr[2]) },
2058 { "sr3", offsetof(CPUState, sr[3]) },
2059 { "sr4", offsetof(CPUState, sr[4]) },
2060 { "sr5", offsetof(CPUState, sr[5]) },
2061 { "sr6", offsetof(CPUState, sr[6]) },
2062 { "sr7", offsetof(CPUState, sr[7]) },
2063 { "sr8", offsetof(CPUState, sr[8]) },
2064 { "sr9", offsetof(CPUState, sr[9]) },
2065 { "sr10", offsetof(CPUState, sr[10]) },
2066 { "sr11", offsetof(CPUState, sr[11]) },
2067 { "sr12", offsetof(CPUState, sr[12]) },
2068 { "sr13", offsetof(CPUState, sr[13]) },
2069 { "sr14", offsetof(CPUState, sr[14]) },
2070 { "sr15", offsetof(CPUState, sr[15]) },
2071 /* Too lazy to put BATs and SPRs ... */
2072 #elif defined(TARGET_SPARC)
2073 { "g0", offsetof(CPUState, gregs[0]) },
2074 { "g1", offsetof(CPUState, gregs[1]) },
2075 { "g2", offsetof(CPUState, gregs[2]) },
2076 { "g3", offsetof(CPUState, gregs[3]) },
2077 { "g4", offsetof(CPUState, gregs[4]) },
2078 { "g5", offsetof(CPUState, gregs[5]) },
2079 { "g6", offsetof(CPUState, gregs[6]) },
2080 { "g7", offsetof(CPUState, gregs[7]) },
2081 { "o0", 0, monitor_get_reg },
2082 { "o1", 1, monitor_get_reg },
2083 { "o2", 2, monitor_get_reg },
2084 { "o3", 3, monitor_get_reg },
2085 { "o4", 4, monitor_get_reg },
2086 { "o5", 5, monitor_get_reg },
2087 { "o6", 6, monitor_get_reg },
2088 { "o7", 7, monitor_get_reg },
2089 { "l0", 8, monitor_get_reg },
2090 { "l1", 9, monitor_get_reg },
2091 { "l2", 10, monitor_get_reg },
2092 { "l3", 11, monitor_get_reg },
2093 { "l4", 12, monitor_get_reg },
2094 { "l5", 13, monitor_get_reg },
2095 { "l6", 14, monitor_get_reg },
2096 { "l7", 15, monitor_get_reg },
2097 { "i0", 16, monitor_get_reg },
2098 { "i1", 17, monitor_get_reg },
2099 { "i2", 18, monitor_get_reg },
2100 { "i3", 19, monitor_get_reg },
2101 { "i4", 20, monitor_get_reg },
2102 { "i5", 21, monitor_get_reg },
2103 { "i6", 22, monitor_get_reg },
2104 { "i7", 23, monitor_get_reg },
2105 { "pc", offsetof(CPUState, pc) },
2106 { "npc", offsetof(CPUState, npc) },
2107 { "y", offsetof(CPUState, y) },
2108 #ifndef TARGET_SPARC64
2109 { "psr", 0, &monitor_get_psr, },
2110 { "wim", offsetof(CPUState, wim) },
2111 #endif
2112 { "tbr", offsetof(CPUState, tbr) },
2113 { "fsr", offsetof(CPUState, fsr) },
2114 { "f0", offsetof(CPUState, fpr[0]) },
2115 { "f1", offsetof(CPUState, fpr[1]) },
2116 { "f2", offsetof(CPUState, fpr[2]) },
2117 { "f3", offsetof(CPUState, fpr[3]) },
2118 { "f4", offsetof(CPUState, fpr[4]) },
2119 { "f5", offsetof(CPUState, fpr[5]) },
2120 { "f6", offsetof(CPUState, fpr[6]) },
2121 { "f7", offsetof(CPUState, fpr[7]) },
2122 { "f8", offsetof(CPUState, fpr[8]) },
2123 { "f9", offsetof(CPUState, fpr[9]) },
2124 { "f10", offsetof(CPUState, fpr[10]) },
2125 { "f11", offsetof(CPUState, fpr[11]) },
2126 { "f12", offsetof(CPUState, fpr[12]) },
2127 { "f13", offsetof(CPUState, fpr[13]) },
2128 { "f14", offsetof(CPUState, fpr[14]) },
2129 { "f15", offsetof(CPUState, fpr[15]) },
2130 { "f16", offsetof(CPUState, fpr[16]) },
2131 { "f17", offsetof(CPUState, fpr[17]) },
2132 { "f18", offsetof(CPUState, fpr[18]) },
2133 { "f19", offsetof(CPUState, fpr[19]) },
2134 { "f20", offsetof(CPUState, fpr[20]) },
2135 { "f21", offsetof(CPUState, fpr[21]) },
2136 { "f22", offsetof(CPUState, fpr[22]) },
2137 { "f23", offsetof(CPUState, fpr[23]) },
2138 { "f24", offsetof(CPUState, fpr[24]) },
2139 { "f25", offsetof(CPUState, fpr[25]) },
2140 { "f26", offsetof(CPUState, fpr[26]) },
2141 { "f27", offsetof(CPUState, fpr[27]) },
2142 { "f28", offsetof(CPUState, fpr[28]) },
2143 { "f29", offsetof(CPUState, fpr[29]) },
2144 { "f30", offsetof(CPUState, fpr[30]) },
2145 { "f31", offsetof(CPUState, fpr[31]) },
2146 #ifdef TARGET_SPARC64
2147 { "f32", offsetof(CPUState, fpr[32]) },
2148 { "f34", offsetof(CPUState, fpr[34]) },
2149 { "f36", offsetof(CPUState, fpr[36]) },
2150 { "f38", offsetof(CPUState, fpr[38]) },
2151 { "f40", offsetof(CPUState, fpr[40]) },
2152 { "f42", offsetof(CPUState, fpr[42]) },
2153 { "f44", offsetof(CPUState, fpr[44]) },
2154 { "f46", offsetof(CPUState, fpr[46]) },
2155 { "f48", offsetof(CPUState, fpr[48]) },
2156 { "f50", offsetof(CPUState, fpr[50]) },
2157 { "f52", offsetof(CPUState, fpr[52]) },
2158 { "f54", offsetof(CPUState, fpr[54]) },
2159 { "f56", offsetof(CPUState, fpr[56]) },
2160 { "f58", offsetof(CPUState, fpr[58]) },
2161 { "f60", offsetof(CPUState, fpr[60]) },
2162 { "f62", offsetof(CPUState, fpr[62]) },
2163 { "asi", offsetof(CPUState, asi) },
2164 { "pstate", offsetof(CPUState, pstate) },
2165 { "cansave", offsetof(CPUState, cansave) },
2166 { "canrestore", offsetof(CPUState, canrestore) },
2167 { "otherwin", offsetof(CPUState, otherwin) },
2168 { "wstate", offsetof(CPUState, wstate) },
2169 { "cleanwin", offsetof(CPUState, cleanwin) },
2170 { "fprs", offsetof(CPUState, fprs) },
2171 #endif
2172 #endif
2173 { NULL },
2176 static void expr_error(Monitor *mon, const char *msg)
2178 monitor_printf(mon, "%s\n", msg);
2179 longjmp(expr_env, 1);
2182 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2183 static int get_monitor_def(target_long *pval, const char *name)
2185 const MonitorDef *md;
2186 void *ptr;
2188 for(md = monitor_defs; md->name != NULL; md++) {
2189 if (compare_cmd(name, md->name)) {
2190 if (md->get_value) {
2191 *pval = md->get_value(md, md->offset);
2192 } else {
2193 CPUState *env = mon_get_cpu();
2194 if (!env)
2195 return -2;
2196 ptr = (uint8_t *)env + md->offset;
2197 switch(md->type) {
2198 case MD_I32:
2199 *pval = *(int32_t *)ptr;
2200 break;
2201 case MD_TLONG:
2202 *pval = *(target_long *)ptr;
2203 break;
2204 default:
2205 *pval = 0;
2206 break;
2209 return 0;
2212 return -1;
2215 static void next(void)
2217 if (pch != '\0') {
2218 pch++;
2219 while (qemu_isspace(*pch))
2220 pch++;
2224 static int64_t expr_sum(Monitor *mon);
2226 static int64_t expr_unary(Monitor *mon)
2228 int64_t n;
2229 char *p;
2230 int ret;
2232 switch(*pch) {
2233 case '+':
2234 next();
2235 n = expr_unary(mon);
2236 break;
2237 case '-':
2238 next();
2239 n = -expr_unary(mon);
2240 break;
2241 case '~':
2242 next();
2243 n = ~expr_unary(mon);
2244 break;
2245 case '(':
2246 next();
2247 n = expr_sum(mon);
2248 if (*pch != ')') {
2249 expr_error(mon, "')' expected");
2251 next();
2252 break;
2253 case '\'':
2254 pch++;
2255 if (*pch == '\0')
2256 expr_error(mon, "character constant expected");
2257 n = *pch;
2258 pch++;
2259 if (*pch != '\'')
2260 expr_error(mon, "missing terminating \' character");
2261 next();
2262 break;
2263 case '$':
2265 char buf[128], *q;
2266 target_long reg=0;
2268 pch++;
2269 q = buf;
2270 while ((*pch >= 'a' && *pch <= 'z') ||
2271 (*pch >= 'A' && *pch <= 'Z') ||
2272 (*pch >= '0' && *pch <= '9') ||
2273 *pch == '_' || *pch == '.') {
2274 if ((q - buf) < sizeof(buf) - 1)
2275 *q++ = *pch;
2276 pch++;
2278 while (qemu_isspace(*pch))
2279 pch++;
2280 *q = 0;
2281 ret = get_monitor_def(&reg, buf);
2282 if (ret == -1)
2283 expr_error(mon, "unknown register");
2284 else if (ret == -2)
2285 expr_error(mon, "no cpu defined");
2286 n = reg;
2288 break;
2289 case '\0':
2290 expr_error(mon, "unexpected end of expression");
2291 n = 0;
2292 break;
2293 default:
2294 #if TARGET_PHYS_ADDR_BITS > 32
2295 n = strtoull(pch, &p, 0);
2296 #else
2297 n = strtoul(pch, &p, 0);
2298 #endif
2299 if (pch == p) {
2300 expr_error(mon, "invalid char in expression");
2302 pch = p;
2303 while (qemu_isspace(*pch))
2304 pch++;
2305 break;
2307 return n;
2311 static int64_t expr_prod(Monitor *mon)
2313 int64_t val, val2;
2314 int op;
2316 val = expr_unary(mon);
2317 for(;;) {
2318 op = *pch;
2319 if (op != '*' && op != '/' && op != '%')
2320 break;
2321 next();
2322 val2 = expr_unary(mon);
2323 switch(op) {
2324 default:
2325 case '*':
2326 val *= val2;
2327 break;
2328 case '/':
2329 case '%':
2330 if (val2 == 0)
2331 expr_error(mon, "division by zero");
2332 if (op == '/')
2333 val /= val2;
2334 else
2335 val %= val2;
2336 break;
2339 return val;
2342 static int64_t expr_logic(Monitor *mon)
2344 int64_t val, val2;
2345 int op;
2347 val = expr_prod(mon);
2348 for(;;) {
2349 op = *pch;
2350 if (op != '&' && op != '|' && op != '^')
2351 break;
2352 next();
2353 val2 = expr_prod(mon);
2354 switch(op) {
2355 default:
2356 case '&':
2357 val &= val2;
2358 break;
2359 case '|':
2360 val |= val2;
2361 break;
2362 case '^':
2363 val ^= val2;
2364 break;
2367 return val;
2370 static int64_t expr_sum(Monitor *mon)
2372 int64_t val, val2;
2373 int op;
2375 val = expr_logic(mon);
2376 for(;;) {
2377 op = *pch;
2378 if (op != '+' && op != '-')
2379 break;
2380 next();
2381 val2 = expr_logic(mon);
2382 if (op == '+')
2383 val += val2;
2384 else
2385 val -= val2;
2387 return val;
2390 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2392 pch = *pp;
2393 if (setjmp(expr_env)) {
2394 *pp = pch;
2395 return -1;
2397 while (qemu_isspace(*pch))
2398 pch++;
2399 *pval = expr_sum(mon);
2400 *pp = pch;
2401 return 0;
2404 static int get_str(char *buf, int buf_size, const char **pp)
2406 const char *p;
2407 char *q;
2408 int c;
2410 q = buf;
2411 p = *pp;
2412 while (qemu_isspace(*p))
2413 p++;
2414 if (*p == '\0') {
2415 fail:
2416 *q = '\0';
2417 *pp = p;
2418 return -1;
2420 if (*p == '\"') {
2421 p++;
2422 while (*p != '\0' && *p != '\"') {
2423 if (*p == '\\') {
2424 p++;
2425 c = *p++;
2426 switch(c) {
2427 case 'n':
2428 c = '\n';
2429 break;
2430 case 'r':
2431 c = '\r';
2432 break;
2433 case '\\':
2434 case '\'':
2435 case '\"':
2436 break;
2437 default:
2438 qemu_printf("unsupported escape code: '\\%c'\n", c);
2439 goto fail;
2441 if ((q - buf) < buf_size - 1) {
2442 *q++ = c;
2444 } else {
2445 if ((q - buf) < buf_size - 1) {
2446 *q++ = *p;
2448 p++;
2451 if (*p != '\"') {
2452 qemu_printf("unterminated string\n");
2453 goto fail;
2455 p++;
2456 } else {
2457 while (*p != '\0' && !qemu_isspace(*p)) {
2458 if ((q - buf) < buf_size - 1) {
2459 *q++ = *p;
2461 p++;
2464 *q = '\0';
2465 *pp = p;
2466 return 0;
2469 static int default_fmt_format = 'x';
2470 static int default_fmt_size = 4;
2472 #define MAX_ARGS 16
2474 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2476 const char *p, *pstart, *typestr;
2477 char *q;
2478 int c, nb_args, len, i, has_arg;
2479 const mon_cmd_t *cmd;
2480 char cmdname[256];
2481 char buf[1024];
2482 void *str_allocated[MAX_ARGS];
2483 void *args[MAX_ARGS];
2484 void (*handler_0)(Monitor *mon);
2485 void (*handler_1)(Monitor *mon, void *arg0);
2486 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2487 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2488 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2489 void *arg3);
2490 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2491 void *arg3, void *arg4);
2492 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2493 void *arg3, void *arg4, void *arg5);
2494 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2495 void *arg3, void *arg4, void *arg5, void *arg6);
2497 #ifdef DEBUG
2498 monitor_printf(mon, "command='%s'\n", cmdline);
2499 #endif
2501 /* extract the command name */
2502 p = cmdline;
2503 q = cmdname;
2504 while (qemu_isspace(*p))
2505 p++;
2506 if (*p == '\0')
2507 return;
2508 pstart = p;
2509 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2510 p++;
2511 len = p - pstart;
2512 if (len > sizeof(cmdname) - 1)
2513 len = sizeof(cmdname) - 1;
2514 memcpy(cmdname, pstart, len);
2515 cmdname[len] = '\0';
2517 /* find the command */
2518 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2519 if (compare_cmd(cmdname, cmd->name))
2520 goto found;
2522 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2523 return;
2524 found:
2526 for(i = 0; i < MAX_ARGS; i++)
2527 str_allocated[i] = NULL;
2529 /* parse the parameters */
2530 typestr = cmd->args_type;
2531 nb_args = 0;
2532 for(;;) {
2533 c = *typestr;
2534 if (c == '\0')
2535 break;
2536 typestr++;
2537 switch(c) {
2538 case 'F':
2539 case 'B':
2540 case 's':
2542 int ret;
2543 char *str;
2545 while (qemu_isspace(*p))
2546 p++;
2547 if (*typestr == '?') {
2548 typestr++;
2549 if (*p == '\0') {
2550 /* no optional string: NULL argument */
2551 str = NULL;
2552 goto add_str;
2555 ret = get_str(buf, sizeof(buf), &p);
2556 if (ret < 0) {
2557 switch(c) {
2558 case 'F':
2559 monitor_printf(mon, "%s: filename expected\n",
2560 cmdname);
2561 break;
2562 case 'B':
2563 monitor_printf(mon, "%s: block device name expected\n",
2564 cmdname);
2565 break;
2566 default:
2567 monitor_printf(mon, "%s: string expected\n", cmdname);
2568 break;
2570 goto fail;
2572 str = qemu_malloc(strlen(buf) + 1);
2573 pstrcpy(str, sizeof(buf), buf);
2574 str_allocated[nb_args] = str;
2575 add_str:
2576 if (nb_args >= MAX_ARGS) {
2577 error_args:
2578 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2579 goto fail;
2581 args[nb_args++] = str;
2583 break;
2584 case '/':
2586 int count, format, size;
2588 while (qemu_isspace(*p))
2589 p++;
2590 if (*p == '/') {
2591 /* format found */
2592 p++;
2593 count = 1;
2594 if (qemu_isdigit(*p)) {
2595 count = 0;
2596 while (qemu_isdigit(*p)) {
2597 count = count * 10 + (*p - '0');
2598 p++;
2601 size = -1;
2602 format = -1;
2603 for(;;) {
2604 switch(*p) {
2605 case 'o':
2606 case 'd':
2607 case 'u':
2608 case 'x':
2609 case 'i':
2610 case 'c':
2611 format = *p++;
2612 break;
2613 case 'b':
2614 size = 1;
2615 p++;
2616 break;
2617 case 'h':
2618 size = 2;
2619 p++;
2620 break;
2621 case 'w':
2622 size = 4;
2623 p++;
2624 break;
2625 case 'g':
2626 case 'L':
2627 size = 8;
2628 p++;
2629 break;
2630 default:
2631 goto next;
2634 next:
2635 if (*p != '\0' && !qemu_isspace(*p)) {
2636 monitor_printf(mon, "invalid char in format: '%c'\n",
2637 *p);
2638 goto fail;
2640 if (format < 0)
2641 format = default_fmt_format;
2642 if (format != 'i') {
2643 /* for 'i', not specifying a size gives -1 as size */
2644 if (size < 0)
2645 size = default_fmt_size;
2646 default_fmt_size = size;
2648 default_fmt_format = format;
2649 } else {
2650 count = 1;
2651 format = default_fmt_format;
2652 if (format != 'i') {
2653 size = default_fmt_size;
2654 } else {
2655 size = -1;
2658 if (nb_args + 3 > MAX_ARGS)
2659 goto error_args;
2660 args[nb_args++] = (void*)(long)count;
2661 args[nb_args++] = (void*)(long)format;
2662 args[nb_args++] = (void*)(long)size;
2664 break;
2665 case 'i':
2666 case 'l':
2668 int64_t val;
2670 while (qemu_isspace(*p))
2671 p++;
2672 if (*typestr == '?' || *typestr == '.') {
2673 if (*typestr == '?') {
2674 if (*p == '\0')
2675 has_arg = 0;
2676 else
2677 has_arg = 1;
2678 } else {
2679 if (*p == '.') {
2680 p++;
2681 while (qemu_isspace(*p))
2682 p++;
2683 has_arg = 1;
2684 } else {
2685 has_arg = 0;
2688 typestr++;
2689 if (nb_args >= MAX_ARGS)
2690 goto error_args;
2691 args[nb_args++] = (void *)(long)has_arg;
2692 if (!has_arg) {
2693 if (nb_args >= MAX_ARGS)
2694 goto error_args;
2695 val = -1;
2696 goto add_num;
2699 if (get_expr(mon, &val, &p))
2700 goto fail;
2701 add_num:
2702 if (c == 'i') {
2703 if (nb_args >= MAX_ARGS)
2704 goto error_args;
2705 args[nb_args++] = (void *)(long)val;
2706 } else {
2707 if ((nb_args + 1) >= MAX_ARGS)
2708 goto error_args;
2709 #if TARGET_PHYS_ADDR_BITS > 32
2710 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2711 #else
2712 args[nb_args++] = (void *)0;
2713 #endif
2714 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2717 break;
2718 case '-':
2720 int has_option;
2721 /* option */
2723 c = *typestr++;
2724 if (c == '\0')
2725 goto bad_type;
2726 while (qemu_isspace(*p))
2727 p++;
2728 has_option = 0;
2729 if (*p == '-') {
2730 p++;
2731 if (*p != c) {
2732 monitor_printf(mon, "%s: unsupported option -%c\n",
2733 cmdname, *p);
2734 goto fail;
2736 p++;
2737 has_option = 1;
2739 if (nb_args >= MAX_ARGS)
2740 goto error_args;
2741 args[nb_args++] = (void *)(long)has_option;
2743 break;
2744 default:
2745 bad_type:
2746 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2747 goto fail;
2750 /* check that all arguments were parsed */
2751 while (qemu_isspace(*p))
2752 p++;
2753 if (*p != '\0') {
2754 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2755 cmdname);
2756 goto fail;
2759 switch(nb_args) {
2760 case 0:
2761 handler_0 = cmd->handler;
2762 handler_0(mon);
2763 break;
2764 case 1:
2765 handler_1 = cmd->handler;
2766 handler_1(mon, args[0]);
2767 break;
2768 case 2:
2769 handler_2 = cmd->handler;
2770 handler_2(mon, args[0], args[1]);
2771 break;
2772 case 3:
2773 handler_3 = cmd->handler;
2774 handler_3(mon, args[0], args[1], args[2]);
2775 break;
2776 case 4:
2777 handler_4 = cmd->handler;
2778 handler_4(mon, args[0], args[1], args[2], args[3]);
2779 break;
2780 case 5:
2781 handler_5 = cmd->handler;
2782 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2783 break;
2784 case 6:
2785 handler_6 = cmd->handler;
2786 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2787 break;
2788 case 7:
2789 handler_7 = cmd->handler;
2790 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2791 args[6]);
2792 break;
2793 default:
2794 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2795 goto fail;
2797 fail:
2798 for(i = 0; i < MAX_ARGS; i++)
2799 qemu_free(str_allocated[i]);
2800 return;
2803 static void cmd_completion(const char *name, const char *list)
2805 const char *p, *pstart;
2806 char cmd[128];
2807 int len;
2809 p = list;
2810 for(;;) {
2811 pstart = p;
2812 p = strchr(p, '|');
2813 if (!p)
2814 p = pstart + strlen(pstart);
2815 len = p - pstart;
2816 if (len > sizeof(cmd) - 2)
2817 len = sizeof(cmd) - 2;
2818 memcpy(cmd, pstart, len);
2819 cmd[len] = '\0';
2820 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2821 readline_add_completion(cur_mon->rs, cmd);
2823 if (*p == '\0')
2824 break;
2825 p++;
2829 static void file_completion(const char *input)
2831 DIR *ffs;
2832 struct dirent *d;
2833 char path[1024];
2834 char file[1024], file_prefix[1024];
2835 int input_path_len;
2836 const char *p;
2838 p = strrchr(input, '/');
2839 if (!p) {
2840 input_path_len = 0;
2841 pstrcpy(file_prefix, sizeof(file_prefix), input);
2842 pstrcpy(path, sizeof(path), ".");
2843 } else {
2844 input_path_len = p - input + 1;
2845 memcpy(path, input, input_path_len);
2846 if (input_path_len > sizeof(path) - 1)
2847 input_path_len = sizeof(path) - 1;
2848 path[input_path_len] = '\0';
2849 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2851 #ifdef DEBUG_COMPLETION
2852 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2853 input, path, file_prefix);
2854 #endif
2855 ffs = opendir(path);
2856 if (!ffs)
2857 return;
2858 for(;;) {
2859 struct stat sb;
2860 d = readdir(ffs);
2861 if (!d)
2862 break;
2863 if (strstart(d->d_name, file_prefix, NULL)) {
2864 memcpy(file, input, input_path_len);
2865 if (input_path_len < sizeof(file))
2866 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2867 d->d_name);
2868 /* stat the file to find out if it's a directory.
2869 * In that case add a slash to speed up typing long paths
2871 stat(file, &sb);
2872 if(S_ISDIR(sb.st_mode))
2873 pstrcat(file, sizeof(file), "/");
2874 readline_add_completion(cur_mon->rs, file);
2877 closedir(ffs);
2880 static void block_completion_it(void *opaque, BlockDriverState *bs)
2882 const char *name = bdrv_get_device_name(bs);
2883 const char *input = opaque;
2885 if (input[0] == '\0' ||
2886 !strncmp(name, (char *)input, strlen(input))) {
2887 readline_add_completion(cur_mon->rs, name);
2891 /* NOTE: this parser is an approximate form of the real command parser */
2892 static void parse_cmdline(const char *cmdline,
2893 int *pnb_args, char **args)
2895 const char *p;
2896 int nb_args, ret;
2897 char buf[1024];
2899 p = cmdline;
2900 nb_args = 0;
2901 for(;;) {
2902 while (qemu_isspace(*p))
2903 p++;
2904 if (*p == '\0')
2905 break;
2906 if (nb_args >= MAX_ARGS)
2907 break;
2908 ret = get_str(buf, sizeof(buf), &p);
2909 args[nb_args] = qemu_strdup(buf);
2910 nb_args++;
2911 if (ret < 0)
2912 break;
2914 *pnb_args = nb_args;
2917 static void monitor_find_completion(const char *cmdline)
2919 const char *cmdname;
2920 char *args[MAX_ARGS];
2921 int nb_args, i, len;
2922 const char *ptype, *str;
2923 const mon_cmd_t *cmd;
2924 const KeyDef *key;
2926 parse_cmdline(cmdline, &nb_args, args);
2927 #ifdef DEBUG_COMPLETION
2928 for(i = 0; i < nb_args; i++) {
2929 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2931 #endif
2933 /* if the line ends with a space, it means we want to complete the
2934 next arg */
2935 len = strlen(cmdline);
2936 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2937 if (nb_args >= MAX_ARGS)
2938 return;
2939 args[nb_args++] = qemu_strdup("");
2941 if (nb_args <= 1) {
2942 /* command completion */
2943 if (nb_args == 0)
2944 cmdname = "";
2945 else
2946 cmdname = args[0];
2947 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2948 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2949 cmd_completion(cmdname, cmd->name);
2951 } else {
2952 /* find the command */
2953 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2954 if (compare_cmd(args[0], cmd->name))
2955 goto found;
2957 return;
2958 found:
2959 ptype = cmd->args_type;
2960 for(i = 0; i < nb_args - 2; i++) {
2961 if (*ptype != '\0') {
2962 ptype++;
2963 while (*ptype == '?')
2964 ptype++;
2967 str = args[nb_args - 1];
2968 switch(*ptype) {
2969 case 'F':
2970 /* file completion */
2971 readline_set_completion_index(cur_mon->rs, strlen(str));
2972 file_completion(str);
2973 break;
2974 case 'B':
2975 /* block device name completion */
2976 readline_set_completion_index(cur_mon->rs, strlen(str));
2977 bdrv_iterate(block_completion_it, (void *)str);
2978 break;
2979 case 's':
2980 /* XXX: more generic ? */
2981 if (!strcmp(cmd->name, "info")) {
2982 readline_set_completion_index(cur_mon->rs, strlen(str));
2983 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2984 cmd_completion(str, cmd->name);
2986 } else if (!strcmp(cmd->name, "sendkey")) {
2987 char *sep = strrchr(str, '-');
2988 if (sep)
2989 str = sep + 1;
2990 readline_set_completion_index(cur_mon->rs, strlen(str));
2991 for(key = key_defs; key->name != NULL; key++) {
2992 cmd_completion(str, key->name);
2995 break;
2996 default:
2997 break;
3000 for(i = 0; i < nb_args; i++)
3001 qemu_free(args[i]);
3004 static int monitor_can_read(void *opaque)
3006 Monitor *mon = opaque;
3008 return (mon->suspend_cnt == 0) ? 128 : 0;
3011 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3013 Monitor *old_mon = cur_mon;
3014 int i;
3016 cur_mon = opaque;
3018 if (cur_mon->rs) {
3019 for (i = 0; i < size; i++)
3020 readline_handle_byte(cur_mon->rs, buf[i]);
3021 } else {
3022 if (size == 0 || buf[size - 1] != 0)
3023 monitor_printf(cur_mon, "corrupted command\n");
3024 else
3025 monitor_handle_command(cur_mon, (char *)buf);
3028 cur_mon = old_mon;
3031 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3033 monitor_suspend(mon);
3034 monitor_handle_command(mon, cmdline);
3035 monitor_resume(mon);
3038 int monitor_suspend(Monitor *mon)
3040 if (!mon->rs)
3041 return -ENOTTY;
3042 mon->suspend_cnt++;
3043 return 0;
3046 void monitor_resume(Monitor *mon)
3048 if (!mon->rs)
3049 return;
3050 if (--mon->suspend_cnt == 0)
3051 readline_show_prompt(mon->rs);
3054 static void monitor_event(void *opaque, int event)
3056 Monitor *mon = opaque;
3058 switch (event) {
3059 case CHR_EVENT_MUX_IN:
3060 readline_restart(mon->rs);
3061 monitor_resume(mon);
3062 monitor_flush(mon);
3063 break;
3065 case CHR_EVENT_MUX_OUT:
3066 if (mon->suspend_cnt == 0)
3067 monitor_printf(mon, "\n");
3068 monitor_flush(mon);
3069 monitor_suspend(mon);
3070 break;
3072 case CHR_EVENT_RESET:
3073 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3074 "information\n", QEMU_VERSION);
3075 if (mon->chr->focus == 0)
3076 readline_show_prompt(mon->rs);
3077 break;
3083 * Local variables:
3084 * c-indent-level: 4
3085 * c-basic-offset: 4
3086 * tab-width: 8
3087 * End:
3090 void monitor_init(CharDriverState *chr, int flags)
3092 static int is_first_init = 1;
3093 Monitor *mon;
3095 if (is_first_init) {
3096 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3097 is_first_init = 0;
3100 mon = qemu_mallocz(sizeof(*mon));
3102 mon->chr = chr;
3103 mon->flags = flags;
3104 if (mon->chr->focus != 0)
3105 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3106 if (flags & MONITOR_USE_READLINE) {
3107 mon->rs = readline_init(mon, monitor_find_completion);
3108 monitor_read_command(mon, 0);
3111 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3112 mon);
3114 LIST_INSERT_HEAD(&mon_list, mon, entry);
3115 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3116 cur_mon = mon;
3119 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3121 BlockDriverState *bs = opaque;
3122 int ret = 0;
3124 if (bdrv_set_key(bs, password) != 0) {
3125 monitor_printf(mon, "invalid password\n");
3126 ret = -EPERM;
3128 if (mon->password_completion_cb)
3129 mon->password_completion_cb(mon->password_opaque, ret);
3131 monitor_read_command(mon, 1);
3134 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3135 BlockDriverCompletionFunc *completion_cb,
3136 void *opaque)
3138 int err;
3140 if (!bdrv_key_required(bs)) {
3141 if (completion_cb)
3142 completion_cb(opaque, 0);
3143 return;
3146 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3147 bdrv_get_encrypted_filename(bs));
3149 mon->password_completion_cb = completion_cb;
3150 mon->password_opaque = opaque;
3152 err = monitor_read_password(mon, bdrv_password_cb, bs);
3154 if (err && completion_cb)
3155 completion_cb(opaque, err);