remove dead code and change debug output from file to console
[qemu/qemu-loongson.git] / monitor.c
blobda106d8bc4744318eac637664bca0d51c01458af
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 //#define DEBUG
47 //#define DEBUG_COMPLETION
50 * Supported types:
52 * 'F' filename
53 * 'B' block device name
54 * 's' string (accept optional quote)
55 * 'i' 32 bit integer
56 * 'l' target long (32 or 64 bit)
57 * '/' optional gdb-like print format (like "/10x")
59 * '?' optional type (for 'F', 's' and 'i')
63 typedef struct mon_cmd_t {
64 const char *name;
65 const char *args_type;
66 void *handler;
67 const char *params;
68 const char *help;
69 } mon_cmd_t;
71 struct Monitor {
72 CharDriverState *chr;
73 int flags;
74 int suspend_cnt;
75 uint8_t outbuf[1024];
76 int outbuf_index;
77 ReadLineState *rs;
78 CPUState *mon_cpu;
79 BlockDriverCompletionFunc *password_completion_cb;
80 void *password_opaque;
81 LIST_ENTRY(Monitor) entry;
84 static LIST_HEAD(mon_list, Monitor) mon_list;
86 static const mon_cmd_t mon_cmds[];
87 static const mon_cmd_t info_cmds[];
89 Monitor *cur_mon = NULL;
91 static void monitor_command_cb(Monitor *mon, const char *cmdline,
92 void *opaque);
94 static void monitor_read_command(Monitor *mon, int show_prompt)
96 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
97 if (show_prompt)
98 readline_show_prompt(mon->rs);
101 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
102 void *opaque)
104 if (mon->rs) {
105 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
106 /* prompt is printed on return from the command handler */
107 return 0;
108 } else {
109 monitor_printf(mon, "terminal does not support password prompting\n");
110 return -ENOTTY;
114 void monitor_flush(Monitor *mon)
116 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
117 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
118 mon->outbuf_index = 0;
122 /* flush at every end of line or if the buffer is full */
123 static void monitor_puts(Monitor *mon, const char *str)
125 char c;
127 if (!mon)
128 return;
130 for(;;) {
131 c = *str++;
132 if (c == '\0')
133 break;
134 if (c == '\n')
135 mon->outbuf[mon->outbuf_index++] = '\r';
136 mon->outbuf[mon->outbuf_index++] = c;
137 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
138 || c == '\n')
139 monitor_flush(mon);
143 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
145 char buf[4096];
146 vsnprintf(buf, sizeof(buf), fmt, ap);
147 monitor_puts(mon, buf);
150 void monitor_printf(Monitor *mon, const char *fmt, ...)
152 va_list ap;
153 va_start(ap, fmt);
154 monitor_vprintf(mon, fmt, ap);
155 va_end(ap);
158 void monitor_print_filename(Monitor *mon, const char *filename)
160 int i;
162 for (i = 0; filename[i]; i++) {
163 switch (filename[i]) {
164 case ' ':
165 case '"':
166 case '\\':
167 monitor_printf(mon, "\\%c", filename[i]);
168 break;
169 case '\t':
170 monitor_printf(mon, "\\t");
171 break;
172 case '\r':
173 monitor_printf(mon, "\\r");
174 break;
175 case '\n':
176 monitor_printf(mon, "\\n");
177 break;
178 default:
179 monitor_printf(mon, "%c", filename[i]);
180 break;
185 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
187 va_list ap;
188 va_start(ap, fmt);
189 monitor_vprintf((Monitor *)stream, fmt, ap);
190 va_end(ap);
191 return 0;
194 static int compare_cmd(const char *name, const char *list)
196 const char *p, *pstart;
197 int len;
198 len = strlen(name);
199 p = list;
200 for(;;) {
201 pstart = p;
202 p = strchr(p, '|');
203 if (!p)
204 p = pstart + strlen(pstart);
205 if ((p - pstart) == len && !memcmp(pstart, name, len))
206 return 1;
207 if (*p == '\0')
208 break;
209 p++;
211 return 0;
214 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
215 const char *prefix, const char *name)
217 const mon_cmd_t *cmd;
219 for(cmd = cmds; cmd->name != NULL; cmd++) {
220 if (!name || !strcmp(name, cmd->name))
221 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
222 cmd->params, cmd->help);
226 static void help_cmd(Monitor *mon, const char *name)
228 if (name && !strcmp(name, "info")) {
229 help_cmd_dump(mon, info_cmds, "info ", NULL);
230 } else {
231 help_cmd_dump(mon, mon_cmds, "", name);
232 if (name && !strcmp(name, "log")) {
233 const CPULogItem *item;
234 monitor_printf(mon, "Log items (comma separated):\n");
235 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
236 for(item = cpu_log_items; item->mask != 0; item++) {
237 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
243 static void do_commit(Monitor *mon, const char *device)
245 int i, all_devices;
247 all_devices = !strcmp(device, "all");
248 for (i = 0; i < nb_drives; i++) {
249 if (all_devices ||
250 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
251 bdrv_commit(drives_table[i].bdrv);
255 static void do_info(Monitor *mon, const char *item)
257 const mon_cmd_t *cmd;
258 void (*handler)(Monitor *);
260 if (!item)
261 goto help;
262 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
263 if (compare_cmd(item, cmd->name))
264 goto found;
266 help:
267 help_cmd(mon, "info");
268 return;
269 found:
270 handler = cmd->handler;
271 handler(mon);
274 static void do_info_version(Monitor *mon)
276 monitor_printf(mon, "%s\n", QEMU_VERSION);
279 static void do_info_name(Monitor *mon)
281 if (qemu_name)
282 monitor_printf(mon, "%s\n", qemu_name);
285 #if defined(TARGET_I386)
286 static void do_info_hpet(Monitor *mon)
288 monitor_printf(mon, "HPET is %s by QEMU\n",
289 (no_hpet) ? "disabled" : "enabled");
291 #endif
293 static void do_info_uuid(Monitor *mon)
295 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
296 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
297 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
298 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
299 qemu_uuid[14], qemu_uuid[15]);
302 /* get the current CPU defined by the user */
303 static int mon_set_cpu(int cpu_index)
305 CPUState *env;
307 for(env = first_cpu; env != NULL; env = env->next_cpu) {
308 if (env->cpu_index == cpu_index) {
309 cur_mon->mon_cpu = env;
310 return 0;
313 return -1;
316 static CPUState *mon_get_cpu(void)
318 if (!cur_mon->mon_cpu) {
319 mon_set_cpu(0);
321 cpu_synchronize_state(cur_mon->mon_cpu, 0);
322 return cur_mon->mon_cpu;
325 static void do_info_registers(Monitor *mon)
327 CPUState *env;
328 env = mon_get_cpu();
329 if (!env)
330 return;
331 #ifdef TARGET_I386
332 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
333 X86_DUMP_FPU);
334 #else
335 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
337 #endif
340 static void do_info_cpus(Monitor *mon)
342 CPUState *env;
344 /* just to set the default cpu if not already done */
345 mon_get_cpu();
347 for(env = first_cpu; env != NULL; env = env->next_cpu) {
348 cpu_synchronize_state(env, 0);
349 monitor_printf(mon, "%c CPU #%d:",
350 (env == mon->mon_cpu) ? '*' : ' ',
351 env->cpu_index);
352 #if defined(TARGET_I386)
353 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
354 env->eip + env->segs[R_CS].base);
355 #elif defined(TARGET_PPC)
356 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
357 #elif defined(TARGET_SPARC)
358 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
359 env->pc, env->npc);
360 #elif defined(TARGET_MIPS)
361 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
362 #endif
363 if (env->halted)
364 monitor_printf(mon, " (halted)");
365 monitor_printf(mon, "\n");
369 static void do_cpu_set(Monitor *mon, int index)
371 if (mon_set_cpu(index) < 0)
372 monitor_printf(mon, "Invalid CPU index\n");
375 static void do_info_jit(Monitor *mon)
377 dump_exec_info((FILE *)mon, monitor_fprintf);
380 static void do_info_history(Monitor *mon)
382 int i;
383 const char *str;
385 if (!mon->rs)
386 return;
387 i = 0;
388 for(;;) {
389 str = readline_get_history(mon->rs, i);
390 if (!str)
391 break;
392 monitor_printf(mon, "%d: '%s'\n", i, str);
393 i++;
397 #if defined(TARGET_PPC)
398 /* XXX: not implemented in other targets */
399 static void do_info_cpu_stats(Monitor *mon)
401 CPUState *env;
403 env = mon_get_cpu();
404 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
406 #endif
408 static void do_quit(Monitor *mon)
410 exit(0);
413 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
415 if (bdrv_is_inserted(bs)) {
416 if (!force) {
417 if (!bdrv_is_removable(bs)) {
418 monitor_printf(mon, "device is not removable\n");
419 return -1;
421 if (bdrv_is_locked(bs)) {
422 monitor_printf(mon, "device is locked\n");
423 return -1;
426 bdrv_close(bs);
428 return 0;
431 static void do_eject(Monitor *mon, int force, const char *filename)
433 BlockDriverState *bs;
435 bs = bdrv_find(filename);
436 if (!bs) {
437 monitor_printf(mon, "device not found\n");
438 return;
440 eject_device(mon, bs, force);
443 static void do_change_block(Monitor *mon, const char *device,
444 const char *filename, const char *fmt)
446 BlockDriverState *bs;
447 BlockDriver *drv = NULL;
449 bs = bdrv_find(device);
450 if (!bs) {
451 monitor_printf(mon, "device not found\n");
452 return;
454 if (fmt) {
455 drv = bdrv_find_format(fmt);
456 if (!drv) {
457 monitor_printf(mon, "invalid format %s\n", fmt);
458 return;
461 if (eject_device(mon, bs, 0) < 0)
462 return;
463 bdrv_open2(bs, filename, 0, drv);
464 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
467 static void change_vnc_password_cb(Monitor *mon, const char *password,
468 void *opaque)
470 if (vnc_display_password(NULL, password) < 0)
471 monitor_printf(mon, "could not set VNC server password\n");
473 monitor_read_command(mon, 1);
476 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
478 if (strcmp(target, "passwd") == 0 ||
479 strcmp(target, "password") == 0) {
480 if (arg) {
481 char password[9];
482 strncpy(password, arg, sizeof(password));
483 password[sizeof(password) - 1] = '\0';
484 change_vnc_password_cb(mon, password, NULL);
485 } else {
486 monitor_read_password(mon, change_vnc_password_cb, NULL);
488 } else {
489 if (vnc_display_open(NULL, target) < 0)
490 monitor_printf(mon, "could not start VNC server on %s\n", target);
494 static void do_change(Monitor *mon, const char *device, const char *target,
495 const char *arg)
497 if (strcmp(device, "vnc") == 0) {
498 do_change_vnc(mon, target, arg);
499 } else {
500 do_change_block(mon, device, target, arg);
504 static void do_screen_dump(Monitor *mon, const char *filename)
506 vga_hw_screen_dump(filename);
509 static void do_logfile(Monitor *mon, const char *filename)
511 cpu_set_log_filename(filename);
514 static void do_log(Monitor *mon, const char *items)
516 int mask;
518 if (!strcmp(items, "none")) {
519 mask = 0;
520 } else {
521 mask = cpu_str_to_log_mask(items);
522 if (!mask) {
523 help_cmd(mon, "log");
524 return;
527 cpu_set_log(mask);
530 static void do_stop(Monitor *mon)
532 vm_stop(EXCP_INTERRUPT);
535 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
537 struct bdrv_iterate_context {
538 Monitor *mon;
539 int err;
542 static void do_cont(Monitor *mon)
544 struct bdrv_iterate_context context = { mon, 0 };
546 bdrv_iterate(encrypted_bdrv_it, &context);
547 /* only resume the vm if all keys are set and valid */
548 if (!context.err)
549 vm_start();
552 static void bdrv_key_cb(void *opaque, int err)
554 Monitor *mon = opaque;
556 /* another key was set successfully, retry to continue */
557 if (!err)
558 do_cont(mon);
561 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
563 struct bdrv_iterate_context *context = opaque;
565 if (!context->err && bdrv_key_required(bs)) {
566 context->err = -EBUSY;
567 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
568 context->mon);
572 #ifdef CONFIG_GDBSTUB
573 static void do_gdbserver(Monitor *mon, const char *port)
575 if (!port)
576 port = DEFAULT_GDBSTUB_PORT;
577 if (gdbserver_start(port) < 0) {
578 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
579 port);
580 } else {
581 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
584 #endif
586 static void monitor_printc(Monitor *mon, int c)
588 monitor_printf(mon, "'");
589 switch(c) {
590 case '\'':
591 monitor_printf(mon, "\\'");
592 break;
593 case '\\':
594 monitor_printf(mon, "\\\\");
595 break;
596 case '\n':
597 monitor_printf(mon, "\\n");
598 break;
599 case '\r':
600 monitor_printf(mon, "\\r");
601 break;
602 default:
603 if (c >= 32 && c <= 126) {
604 monitor_printf(mon, "%c", c);
605 } else {
606 monitor_printf(mon, "\\x%02x", c);
608 break;
610 monitor_printf(mon, "'");
613 static void memory_dump(Monitor *mon, int count, int format, int wsize,
614 target_phys_addr_t addr, int is_physical)
616 CPUState *env;
617 int nb_per_line, l, line_size, i, max_digits, len;
618 uint8_t buf[16];
619 uint64_t v;
621 if (format == 'i') {
622 int flags;
623 flags = 0;
624 env = mon_get_cpu();
625 if (!env && !is_physical)
626 return;
627 #ifdef TARGET_I386
628 if (wsize == 2) {
629 flags = 1;
630 } else if (wsize == 4) {
631 flags = 0;
632 } else {
633 /* as default we use the current CS size */
634 flags = 0;
635 if (env) {
636 #ifdef TARGET_X86_64
637 if ((env->efer & MSR_EFER_LMA) &&
638 (env->segs[R_CS].flags & DESC_L_MASK))
639 flags = 2;
640 else
641 #endif
642 if (!(env->segs[R_CS].flags & DESC_B_MASK))
643 flags = 1;
646 #endif
647 monitor_disas(mon, env, addr, count, is_physical, flags);
648 return;
651 len = wsize * count;
652 if (wsize == 1)
653 line_size = 8;
654 else
655 line_size = 16;
656 nb_per_line = line_size / wsize;
657 max_digits = 0;
659 switch(format) {
660 case 'o':
661 max_digits = (wsize * 8 + 2) / 3;
662 break;
663 default:
664 case 'x':
665 max_digits = (wsize * 8) / 4;
666 break;
667 case 'u':
668 case 'd':
669 max_digits = (wsize * 8 * 10 + 32) / 33;
670 break;
671 case 'c':
672 wsize = 1;
673 break;
676 while (len > 0) {
677 if (is_physical)
678 monitor_printf(mon, TARGET_FMT_plx ":", addr);
679 else
680 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
681 l = len;
682 if (l > line_size)
683 l = line_size;
684 if (is_physical) {
685 cpu_physical_memory_rw(addr, buf, l, 0);
686 } else {
687 env = mon_get_cpu();
688 if (!env)
689 break;
690 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
691 monitor_printf(mon, " Cannot access memory\n");
692 break;
695 i = 0;
696 while (i < l) {
697 switch(wsize) {
698 default:
699 case 1:
700 v = ldub_raw(buf + i);
701 break;
702 case 2:
703 v = lduw_raw(buf + i);
704 break;
705 case 4:
706 v = (uint32_t)ldl_raw(buf + i);
707 break;
708 case 8:
709 v = ldq_raw(buf + i);
710 break;
712 monitor_printf(mon, " ");
713 switch(format) {
714 case 'o':
715 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
716 break;
717 case 'x':
718 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
719 break;
720 case 'u':
721 monitor_printf(mon, "%*" PRIu64, max_digits, v);
722 break;
723 case 'd':
724 monitor_printf(mon, "%*" PRId64, max_digits, v);
725 break;
726 case 'c':
727 monitor_printc(mon, v);
728 break;
730 i += wsize;
732 monitor_printf(mon, "\n");
733 addr += l;
734 len -= l;
738 #if TARGET_LONG_BITS == 64
739 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
740 #else
741 #define GET_TLONG(h, l) (l)
742 #endif
744 static void do_memory_dump(Monitor *mon, int count, int format, int size,
745 uint32_t addrh, uint32_t addrl)
747 target_long addr = GET_TLONG(addrh, addrl);
748 memory_dump(mon, count, format, size, addr, 0);
751 #if TARGET_PHYS_ADDR_BITS > 32
752 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
753 #else
754 #define GET_TPHYSADDR(h, l) (l)
755 #endif
757 static void do_physical_memory_dump(Monitor *mon, int count, int format,
758 int size, uint32_t addrh, uint32_t addrl)
761 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
762 memory_dump(mon, count, format, size, addr, 1);
765 static void do_print(Monitor *mon, int count, int format, int size,
766 unsigned int valh, unsigned int vall)
768 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
769 #if TARGET_PHYS_ADDR_BITS == 32
770 switch(format) {
771 case 'o':
772 monitor_printf(mon, "%#o", val);
773 break;
774 case 'x':
775 monitor_printf(mon, "%#x", val);
776 break;
777 case 'u':
778 monitor_printf(mon, "%u", val);
779 break;
780 default:
781 case 'd':
782 monitor_printf(mon, "%d", val);
783 break;
784 case 'c':
785 monitor_printc(mon, val);
786 break;
788 #else
789 switch(format) {
790 case 'o':
791 monitor_printf(mon, "%#" PRIo64, val);
792 break;
793 case 'x':
794 monitor_printf(mon, "%#" PRIx64, val);
795 break;
796 case 'u':
797 monitor_printf(mon, "%" PRIu64, val);
798 break;
799 default:
800 case 'd':
801 monitor_printf(mon, "%" PRId64, val);
802 break;
803 case 'c':
804 monitor_printc(mon, val);
805 break;
807 #endif
808 monitor_printf(mon, "\n");
811 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
812 uint32_t size, const char *filename)
814 FILE *f;
815 target_long addr = GET_TLONG(valh, vall);
816 uint32_t l;
817 CPUState *env;
818 uint8_t buf[1024];
820 env = mon_get_cpu();
821 if (!env)
822 return;
824 f = fopen(filename, "wb");
825 if (!f) {
826 monitor_printf(mon, "could not open '%s'\n", filename);
827 return;
829 while (size != 0) {
830 l = sizeof(buf);
831 if (l > size)
832 l = size;
833 cpu_memory_rw_debug(env, addr, buf, l, 0);
834 fwrite(buf, 1, l, f);
835 addr += l;
836 size -= l;
838 fclose(f);
841 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
842 unsigned int vall, uint32_t size,
843 const char *filename)
845 FILE *f;
846 uint32_t l;
847 uint8_t buf[1024];
848 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
850 f = fopen(filename, "wb");
851 if (!f) {
852 monitor_printf(mon, "could not open '%s'\n", filename);
853 return;
855 while (size != 0) {
856 l = sizeof(buf);
857 if (l > size)
858 l = size;
859 cpu_physical_memory_rw(addr, buf, l, 0);
860 fwrite(buf, 1, l, f);
861 fflush(f);
862 addr += l;
863 size -= l;
865 fclose(f);
868 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
870 uint32_t addr;
871 uint8_t buf[1];
872 uint16_t sum;
874 sum = 0;
875 for(addr = start; addr < (start + size); addr++) {
876 cpu_physical_memory_rw(addr, buf, 1, 0);
877 /* BSD sum algorithm ('sum' Unix command) */
878 sum = (sum >> 1) | (sum << 15);
879 sum += buf[0];
881 monitor_printf(mon, "%05d\n", sum);
884 typedef struct {
885 int keycode;
886 const char *name;
887 } KeyDef;
889 static const KeyDef key_defs[] = {
890 { 0x2a, "shift" },
891 { 0x36, "shift_r" },
893 { 0x38, "alt" },
894 { 0xb8, "alt_r" },
895 { 0x64, "altgr" },
896 { 0xe4, "altgr_r" },
897 { 0x1d, "ctrl" },
898 { 0x9d, "ctrl_r" },
900 { 0xdd, "menu" },
902 { 0x01, "esc" },
904 { 0x02, "1" },
905 { 0x03, "2" },
906 { 0x04, "3" },
907 { 0x05, "4" },
908 { 0x06, "5" },
909 { 0x07, "6" },
910 { 0x08, "7" },
911 { 0x09, "8" },
912 { 0x0a, "9" },
913 { 0x0b, "0" },
914 { 0x0c, "minus" },
915 { 0x0d, "equal" },
916 { 0x0e, "backspace" },
918 { 0x0f, "tab" },
919 { 0x10, "q" },
920 { 0x11, "w" },
921 { 0x12, "e" },
922 { 0x13, "r" },
923 { 0x14, "t" },
924 { 0x15, "y" },
925 { 0x16, "u" },
926 { 0x17, "i" },
927 { 0x18, "o" },
928 { 0x19, "p" },
930 { 0x1c, "ret" },
932 { 0x1e, "a" },
933 { 0x1f, "s" },
934 { 0x20, "d" },
935 { 0x21, "f" },
936 { 0x22, "g" },
937 { 0x23, "h" },
938 { 0x24, "j" },
939 { 0x25, "k" },
940 { 0x26, "l" },
942 { 0x2c, "z" },
943 { 0x2d, "x" },
944 { 0x2e, "c" },
945 { 0x2f, "v" },
946 { 0x30, "b" },
947 { 0x31, "n" },
948 { 0x32, "m" },
949 { 0x33, "comma" },
950 { 0x34, "dot" },
951 { 0x35, "slash" },
953 { 0x37, "asterisk" },
955 { 0x39, "spc" },
956 { 0x3a, "caps_lock" },
957 { 0x3b, "f1" },
958 { 0x3c, "f2" },
959 { 0x3d, "f3" },
960 { 0x3e, "f4" },
961 { 0x3f, "f5" },
962 { 0x40, "f6" },
963 { 0x41, "f7" },
964 { 0x42, "f8" },
965 { 0x43, "f9" },
966 { 0x44, "f10" },
967 { 0x45, "num_lock" },
968 { 0x46, "scroll_lock" },
970 { 0xb5, "kp_divide" },
971 { 0x37, "kp_multiply" },
972 { 0x4a, "kp_subtract" },
973 { 0x4e, "kp_add" },
974 { 0x9c, "kp_enter" },
975 { 0x53, "kp_decimal" },
976 { 0x54, "sysrq" },
978 { 0x52, "kp_0" },
979 { 0x4f, "kp_1" },
980 { 0x50, "kp_2" },
981 { 0x51, "kp_3" },
982 { 0x4b, "kp_4" },
983 { 0x4c, "kp_5" },
984 { 0x4d, "kp_6" },
985 { 0x47, "kp_7" },
986 { 0x48, "kp_8" },
987 { 0x49, "kp_9" },
989 { 0x56, "<" },
991 { 0x57, "f11" },
992 { 0x58, "f12" },
994 { 0xb7, "print" },
996 { 0xc7, "home" },
997 { 0xc9, "pgup" },
998 { 0xd1, "pgdn" },
999 { 0xcf, "end" },
1001 { 0xcb, "left" },
1002 { 0xc8, "up" },
1003 { 0xd0, "down" },
1004 { 0xcd, "right" },
1006 { 0xd2, "insert" },
1007 { 0xd3, "delete" },
1008 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1009 { 0xf0, "stop" },
1010 { 0xf1, "again" },
1011 { 0xf2, "props" },
1012 { 0xf3, "undo" },
1013 { 0xf4, "front" },
1014 { 0xf5, "copy" },
1015 { 0xf6, "open" },
1016 { 0xf7, "paste" },
1017 { 0xf8, "find" },
1018 { 0xf9, "cut" },
1019 { 0xfa, "lf" },
1020 { 0xfb, "help" },
1021 { 0xfc, "meta_l" },
1022 { 0xfd, "meta_r" },
1023 { 0xfe, "compose" },
1024 #endif
1025 { 0, NULL },
1028 static int get_keycode(const char *key)
1030 const KeyDef *p;
1031 char *endp;
1032 int ret;
1034 for(p = key_defs; p->name != NULL; p++) {
1035 if (!strcmp(key, p->name))
1036 return p->keycode;
1038 if (strstart(key, "0x", NULL)) {
1039 ret = strtoul(key, &endp, 0);
1040 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1041 return ret;
1043 return -1;
1046 #define MAX_KEYCODES 16
1047 static uint8_t keycodes[MAX_KEYCODES];
1048 static int nb_pending_keycodes;
1049 static QEMUTimer *key_timer;
1051 static void release_keys(void *opaque)
1053 int keycode;
1055 while (nb_pending_keycodes > 0) {
1056 nb_pending_keycodes--;
1057 keycode = keycodes[nb_pending_keycodes];
1058 if (keycode & 0x80)
1059 kbd_put_keycode(0xe0);
1060 kbd_put_keycode(keycode | 0x80);
1064 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1065 int hold_time)
1067 char keyname_buf[16];
1068 char *separator;
1069 int keyname_len, keycode, i;
1071 if (nb_pending_keycodes > 0) {
1072 qemu_del_timer(key_timer);
1073 release_keys(NULL);
1075 if (!has_hold_time)
1076 hold_time = 100;
1077 i = 0;
1078 while (1) {
1079 separator = strchr(string, '-');
1080 keyname_len = separator ? separator - string : strlen(string);
1081 if (keyname_len > 0) {
1082 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1083 if (keyname_len > sizeof(keyname_buf) - 1) {
1084 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1085 return;
1087 if (i == MAX_KEYCODES) {
1088 monitor_printf(mon, "too many keys\n");
1089 return;
1091 keyname_buf[keyname_len] = 0;
1092 keycode = get_keycode(keyname_buf);
1093 if (keycode < 0) {
1094 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1095 return;
1097 keycodes[i++] = keycode;
1099 if (!separator)
1100 break;
1101 string = separator + 1;
1103 nb_pending_keycodes = i;
1104 /* key down events */
1105 for (i = 0; i < nb_pending_keycodes; i++) {
1106 keycode = keycodes[i];
1107 if (keycode & 0x80)
1108 kbd_put_keycode(0xe0);
1109 kbd_put_keycode(keycode & 0x7f);
1111 /* delayed key up events */
1112 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1113 muldiv64(ticks_per_sec, hold_time, 1000));
1116 static int mouse_button_state;
1118 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1119 const char *dz_str)
1121 int dx, dy, dz;
1122 dx = strtol(dx_str, NULL, 0);
1123 dy = strtol(dy_str, NULL, 0);
1124 dz = 0;
1125 if (dz_str)
1126 dz = strtol(dz_str, NULL, 0);
1127 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1130 static void do_mouse_button(Monitor *mon, int button_state)
1132 mouse_button_state = button_state;
1133 kbd_mouse_event(0, 0, 0, mouse_button_state);
1136 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1137 int addr, int has_index, int index)
1139 uint32_t val;
1140 int suffix;
1142 if (has_index) {
1143 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1144 addr++;
1146 addr &= 0xffff;
1148 switch(size) {
1149 default:
1150 case 1:
1151 val = cpu_inb(NULL, addr);
1152 suffix = 'b';
1153 break;
1154 case 2:
1155 val = cpu_inw(NULL, addr);
1156 suffix = 'w';
1157 break;
1158 case 4:
1159 val = cpu_inl(NULL, addr);
1160 suffix = 'l';
1161 break;
1163 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1164 suffix, addr, size * 2, val);
1167 /* boot_set handler */
1168 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1169 static void *boot_opaque;
1171 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1173 qemu_boot_set_handler = func;
1174 boot_opaque = opaque;
1177 static void do_boot_set(Monitor *mon, const char *bootdevice)
1179 int res;
1181 if (qemu_boot_set_handler) {
1182 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1183 if (res == 0)
1184 monitor_printf(mon, "boot device list now set to %s\n",
1185 bootdevice);
1186 else
1187 monitor_printf(mon, "setting boot device list failed with "
1188 "error %i\n", res);
1189 } else {
1190 monitor_printf(mon, "no function defined to set boot device list for "
1191 "this architecture\n");
1195 static void do_system_reset(Monitor *mon)
1197 qemu_system_reset_request();
1200 static void do_system_powerdown(Monitor *mon)
1202 qemu_system_powerdown_request();
1205 #if defined(TARGET_I386)
1206 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1208 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1209 addr,
1210 pte & mask,
1211 pte & PG_GLOBAL_MASK ? 'G' : '-',
1212 pte & PG_PSE_MASK ? 'P' : '-',
1213 pte & PG_DIRTY_MASK ? 'D' : '-',
1214 pte & PG_ACCESSED_MASK ? 'A' : '-',
1215 pte & PG_PCD_MASK ? 'C' : '-',
1216 pte & PG_PWT_MASK ? 'T' : '-',
1217 pte & PG_USER_MASK ? 'U' : '-',
1218 pte & PG_RW_MASK ? 'W' : '-');
1221 static void tlb_info(Monitor *mon)
1223 CPUState *env;
1224 int l1, l2;
1225 uint32_t pgd, pde, pte;
1227 env = mon_get_cpu();
1228 if (!env)
1229 return;
1231 if (!(env->cr[0] & CR0_PG_MASK)) {
1232 monitor_printf(mon, "PG disabled\n");
1233 return;
1235 pgd = env->cr[3] & ~0xfff;
1236 for(l1 = 0; l1 < 1024; l1++) {
1237 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1238 pde = le32_to_cpu(pde);
1239 if (pde & PG_PRESENT_MASK) {
1240 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1241 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1242 } else {
1243 for(l2 = 0; l2 < 1024; l2++) {
1244 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1245 (uint8_t *)&pte, 4);
1246 pte = le32_to_cpu(pte);
1247 if (pte & PG_PRESENT_MASK) {
1248 print_pte(mon, (l1 << 22) + (l2 << 12),
1249 pte & ~PG_PSE_MASK,
1250 ~0xfff);
1258 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1259 uint32_t end, int prot)
1261 int prot1;
1262 prot1 = *plast_prot;
1263 if (prot != prot1) {
1264 if (*pstart != -1) {
1265 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1266 *pstart, end, end - *pstart,
1267 prot1 & PG_USER_MASK ? 'u' : '-',
1268 'r',
1269 prot1 & PG_RW_MASK ? 'w' : '-');
1271 if (prot != 0)
1272 *pstart = end;
1273 else
1274 *pstart = -1;
1275 *plast_prot = prot;
1279 static void mem_info(Monitor *mon)
1281 CPUState *env;
1282 int l1, l2, prot, last_prot;
1283 uint32_t pgd, pde, pte, start, end;
1285 env = mon_get_cpu();
1286 if (!env)
1287 return;
1289 if (!(env->cr[0] & CR0_PG_MASK)) {
1290 monitor_printf(mon, "PG disabled\n");
1291 return;
1293 pgd = env->cr[3] & ~0xfff;
1294 last_prot = 0;
1295 start = -1;
1296 for(l1 = 0; l1 < 1024; l1++) {
1297 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1298 pde = le32_to_cpu(pde);
1299 end = l1 << 22;
1300 if (pde & PG_PRESENT_MASK) {
1301 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1302 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1303 mem_print(mon, &start, &last_prot, end, prot);
1304 } else {
1305 for(l2 = 0; l2 < 1024; l2++) {
1306 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1307 (uint8_t *)&pte, 4);
1308 pte = le32_to_cpu(pte);
1309 end = (l1 << 22) + (l2 << 12);
1310 if (pte & PG_PRESENT_MASK) {
1311 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1312 } else {
1313 prot = 0;
1315 mem_print(mon, &start, &last_prot, end, prot);
1318 } else {
1319 prot = 0;
1320 mem_print(mon, &start, &last_prot, end, prot);
1324 #endif
1326 #if defined(TARGET_SH4)
1328 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1330 monitor_printf(mon, " tlb%i:\t"
1331 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1332 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1333 "dirty=%hhu writethrough=%hhu\n",
1334 idx,
1335 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1336 tlb->v, tlb->sh, tlb->c, tlb->pr,
1337 tlb->d, tlb->wt);
1340 static void tlb_info(Monitor *mon)
1342 CPUState *env = mon_get_cpu();
1343 int i;
1345 monitor_printf (mon, "ITLB:\n");
1346 for (i = 0 ; i < ITLB_SIZE ; i++)
1347 print_tlb (mon, i, &env->itlb[i]);
1348 monitor_printf (mon, "UTLB:\n");
1349 for (i = 0 ; i < UTLB_SIZE ; i++)
1350 print_tlb (mon, i, &env->utlb[i]);
1353 #endif
1355 static void do_info_kqemu(Monitor *mon)
1357 #ifdef USE_KQEMU
1358 CPUState *env;
1359 int val;
1360 val = 0;
1361 env = mon_get_cpu();
1362 if (!env) {
1363 monitor_printf(mon, "No cpu initialized yet");
1364 return;
1366 val = env->kqemu_enabled;
1367 monitor_printf(mon, "kqemu support: ");
1368 switch(val) {
1369 default:
1370 case 0:
1371 monitor_printf(mon, "disabled\n");
1372 break;
1373 case 1:
1374 monitor_printf(mon, "enabled for user code\n");
1375 break;
1376 case 2:
1377 monitor_printf(mon, "enabled for user and kernel code\n");
1378 break;
1380 #else
1381 monitor_printf(mon, "kqemu support: not compiled\n");
1382 #endif
1385 static void do_info_kvm(Monitor *mon)
1387 #ifdef CONFIG_KVM
1388 monitor_printf(mon, "kvm support: ");
1389 if (kvm_enabled())
1390 monitor_printf(mon, "enabled\n");
1391 else
1392 monitor_printf(mon, "disabled\n");
1393 #else
1394 monitor_printf(mon, "kvm support: not compiled\n");
1395 #endif
1398 #ifdef CONFIG_PROFILER
1400 int64_t kqemu_time;
1401 int64_t qemu_time;
1402 int64_t kqemu_exec_count;
1403 int64_t dev_time;
1404 int64_t kqemu_ret_int_count;
1405 int64_t kqemu_ret_excp_count;
1406 int64_t kqemu_ret_intr_count;
1408 static void do_info_profile(Monitor *mon)
1410 int64_t total;
1411 total = qemu_time;
1412 if (total == 0)
1413 total = 1;
1414 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1415 dev_time, dev_time / (double)ticks_per_sec);
1416 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1417 qemu_time, qemu_time / (double)ticks_per_sec);
1418 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1419 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1420 PRId64 "\n",
1421 kqemu_time, kqemu_time / (double)ticks_per_sec,
1422 kqemu_time / (double)total * 100.0,
1423 kqemu_exec_count,
1424 kqemu_ret_int_count,
1425 kqemu_ret_excp_count,
1426 kqemu_ret_intr_count);
1427 qemu_time = 0;
1428 kqemu_time = 0;
1429 kqemu_exec_count = 0;
1430 dev_time = 0;
1431 kqemu_ret_int_count = 0;
1432 kqemu_ret_excp_count = 0;
1433 kqemu_ret_intr_count = 0;
1434 #ifdef USE_KQEMU
1435 kqemu_record_dump();
1436 #endif
1438 #else
1439 static void do_info_profile(Monitor *mon)
1441 monitor_printf(mon, "Internal profiler not compiled\n");
1443 #endif
1445 /* Capture support */
1446 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1448 static void do_info_capture(Monitor *mon)
1450 int i;
1451 CaptureState *s;
1453 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1454 monitor_printf(mon, "[%d]: ", i);
1455 s->ops.info (s->opaque);
1459 static void do_stop_capture(Monitor *mon, int n)
1461 int i;
1462 CaptureState *s;
1464 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1465 if (i == n) {
1466 s->ops.destroy (s->opaque);
1467 LIST_REMOVE (s, entries);
1468 qemu_free (s);
1469 return;
1474 #ifdef HAS_AUDIO
1475 static void do_wav_capture(Monitor *mon, const char *path,
1476 int has_freq, int freq,
1477 int has_bits, int bits,
1478 int has_channels, int nchannels)
1480 CaptureState *s;
1482 s = qemu_mallocz (sizeof (*s));
1484 freq = has_freq ? freq : 44100;
1485 bits = has_bits ? bits : 16;
1486 nchannels = has_channels ? nchannels : 2;
1488 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1489 monitor_printf(mon, "Faied to add wave capture\n");
1490 qemu_free (s);
1492 LIST_INSERT_HEAD (&capture_head, s, entries);
1494 #endif
1496 #if defined(TARGET_I386)
1497 static void do_inject_nmi(Monitor *mon, int cpu_index)
1499 CPUState *env;
1501 for (env = first_cpu; env != NULL; env = env->next_cpu)
1502 if (env->cpu_index == cpu_index) {
1503 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1504 break;
1507 #endif
1509 static void do_info_status(Monitor *mon)
1511 if (vm_running)
1512 monitor_printf(mon, "VM status: running\n");
1513 else
1514 monitor_printf(mon, "VM status: paused\n");
1518 static void do_balloon(Monitor *mon, int value)
1520 ram_addr_t target = value;
1521 qemu_balloon(target << 20);
1524 static void do_info_balloon(Monitor *mon)
1526 ram_addr_t actual;
1528 actual = qemu_balloon_status();
1529 if (kvm_enabled() && !kvm_has_sync_mmu())
1530 monitor_printf(mon, "Using KVM without synchronous MMU, "
1531 "ballooning disabled\n");
1532 else if (actual == 0)
1533 monitor_printf(mon, "Ballooning not activated in VM\n");
1534 else
1535 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1538 static void do_acl(Monitor *mon,
1539 const char *command,
1540 const char *aclname,
1541 const char *match,
1542 int has_index,
1543 int index)
1545 qemu_acl *acl;
1547 acl = qemu_acl_find(aclname);
1548 if (!acl) {
1549 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1550 return;
1553 if (strcmp(command, "show") == 0) {
1554 int i = 0;
1555 qemu_acl_entry *entry;
1556 monitor_printf(mon, "policy: %s\n",
1557 acl->defaultDeny ? "deny" : "allow");
1558 TAILQ_FOREACH(entry, &acl->entries, next) {
1559 i++;
1560 monitor_printf(mon, "%d: %s %s\n", i,
1561 entry->deny ? "deny" : "allow",
1562 entry->match);
1564 } else if (strcmp(command, "reset") == 0) {
1565 qemu_acl_reset(acl);
1566 monitor_printf(mon, "acl: removed all rules\n");
1567 } else if (strcmp(command, "policy") == 0) {
1568 if (!match) {
1569 monitor_printf(mon, "acl: missing policy parameter\n");
1570 return;
1573 if (strcmp(match, "allow") == 0) {
1574 acl->defaultDeny = 0;
1575 monitor_printf(mon, "acl: policy set to 'allow'\n");
1576 } else if (strcmp(match, "deny") == 0) {
1577 acl->defaultDeny = 1;
1578 monitor_printf(mon, "acl: policy set to 'deny'\n");
1579 } else {
1580 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1582 } else if ((strcmp(command, "allow") == 0) ||
1583 (strcmp(command, "deny") == 0)) {
1584 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1585 int ret;
1587 if (!match) {
1588 monitor_printf(mon, "acl: missing match parameter\n");
1589 return;
1592 if (has_index)
1593 ret = qemu_acl_insert(acl, deny, match, index);
1594 else
1595 ret = qemu_acl_append(acl, deny, match);
1596 if (ret < 0)
1597 monitor_printf(mon, "acl: unable to add acl entry\n");
1598 else
1599 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1600 } else if (strcmp(command, "remove") == 0) {
1601 int ret;
1603 if (!match) {
1604 monitor_printf(mon, "acl: missing match parameter\n");
1605 return;
1608 ret = qemu_acl_remove(acl, match);
1609 if (ret < 0)
1610 monitor_printf(mon, "acl: no matching acl entry\n");
1611 else
1612 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1613 } else {
1614 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1618 /* Please update qemu-doc.texi when adding or changing commands */
1619 static const mon_cmd_t mon_cmds[] = {
1620 { "help|?", "s?", help_cmd,
1621 "[cmd]", "show the help" },
1622 { "commit", "s", do_commit,
1623 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1624 { "info", "s?", do_info,
1625 "subcommand", "show various information about the system state" },
1626 { "q|quit", "", do_quit,
1627 "", "quit the emulator" },
1628 { "eject", "-fB", do_eject,
1629 "[-f] device", "eject a removable medium (use -f to force it)" },
1630 { "change", "BFs?", do_change,
1631 "device filename [format]", "change a removable medium, optional format" },
1632 { "screendump", "F", do_screen_dump,
1633 "filename", "save screen into PPM image 'filename'" },
1634 { "logfile", "F", do_logfile,
1635 "filename", "output logs to 'filename'" },
1636 { "log", "s", do_log,
1637 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1638 { "savevm", "s?", do_savevm,
1639 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1640 { "loadvm", "s", do_loadvm,
1641 "tag|id", "restore a VM snapshot from its tag or id" },
1642 { "delvm", "s", do_delvm,
1643 "tag|id", "delete a VM snapshot from its tag or id" },
1644 { "stop", "", do_stop,
1645 "", "stop emulation", },
1646 { "c|cont", "", do_cont,
1647 "", "resume emulation", },
1648 #ifdef CONFIG_GDBSTUB
1649 { "gdbserver", "s?", do_gdbserver,
1650 "[port]", "start gdbserver session (default port=1234)", },
1651 #endif
1652 { "x", "/l", do_memory_dump,
1653 "/fmt addr", "virtual memory dump starting at 'addr'", },
1654 { "xp", "/l", do_physical_memory_dump,
1655 "/fmt addr", "physical memory dump starting at 'addr'", },
1656 { "p|print", "/l", do_print,
1657 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1658 { "i", "/ii.", do_ioport_read,
1659 "/fmt addr", "I/O port read" },
1661 { "sendkey", "si?", do_sendkey,
1662 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1663 { "system_reset", "", do_system_reset,
1664 "", "reset the system" },
1665 { "system_powerdown", "", do_system_powerdown,
1666 "", "send system power down event" },
1667 { "sum", "ii", do_sum,
1668 "addr size", "compute the checksum of a memory region" },
1669 { "usb_add", "s", do_usb_add,
1670 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1671 { "usb_del", "s", do_usb_del,
1672 "device", "remove USB device 'bus.addr'" },
1673 { "cpu", "i", do_cpu_set,
1674 "index", "set the default CPU" },
1675 { "mouse_move", "sss?", do_mouse_move,
1676 "dx dy [dz]", "send mouse move events" },
1677 { "mouse_button", "i", do_mouse_button,
1678 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1679 { "mouse_set", "i", do_mouse_set,
1680 "index", "set which mouse device receives events" },
1681 #ifdef HAS_AUDIO
1682 { "wavcapture", "si?i?i?", do_wav_capture,
1683 "path [frequency bits channels]",
1684 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1685 #endif
1686 { "stopcapture", "i", do_stop_capture,
1687 "capture index", "stop capture" },
1688 { "memsave", "lis", do_memory_save,
1689 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1690 { "pmemsave", "lis", do_physical_memory_save,
1691 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1692 { "boot_set", "s", do_boot_set,
1693 "bootdevice", "define new values for the boot device list" },
1694 #if defined(TARGET_I386)
1695 { "nmi", "i", do_inject_nmi,
1696 "cpu", "inject an NMI on the given CPU", },
1697 #endif
1698 { "migrate", "-ds", do_migrate,
1699 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1700 { "migrate_cancel", "", do_migrate_cancel,
1701 "", "cancel the current VM migration" },
1702 { "migrate_set_speed", "s", do_migrate_set_speed,
1703 "value", "set maximum speed (in bytes) for migrations" },
1704 #if defined(TARGET_I386)
1705 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1706 "[file=file][,if=type][,bus=n]\n"
1707 "[,unit=m][,media=d][index=i]\n"
1708 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1709 "[snapshot=on|off][,cache=on|off]",
1710 "add drive to PCI storage controller" },
1711 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1712 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1713 { "host_net_add", "ss", net_host_device_add,
1714 "[tap,user,socket,vde] options", "add host VLAN client" },
1715 { "host_net_remove", "is", net_host_device_remove,
1716 "vlan_id name", "remove host VLAN client" },
1717 #endif
1718 { "balloon", "i", do_balloon,
1719 "target", "request VM to change it's memory allocation (in MB)" },
1720 { "set_link", "ss", do_set_link,
1721 "name [up|down]", "change the link status of a network adapter" },
1722 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1723 "acl show vnc.username\n"
1724 "acl policy vnc.username deny\n"
1725 "acl allow vnc.username fred\n"
1726 "acl deny vnc.username bob\n"
1727 "acl reset vnc.username\n" },
1728 { NULL, NULL, },
1731 /* Please update qemu-doc.texi when adding or changing commands */
1732 static const mon_cmd_t info_cmds[] = {
1733 { "version", "", do_info_version,
1734 "", "show the version of QEMU" },
1735 { "network", "", do_info_network,
1736 "", "show the network state" },
1737 { "chardev", "", qemu_chr_info,
1738 "", "show the character devices" },
1739 { "block", "", bdrv_info,
1740 "", "show the block devices" },
1741 { "blockstats", "", bdrv_info_stats,
1742 "", "show block device statistics" },
1743 { "registers", "", do_info_registers,
1744 "", "show the cpu registers" },
1745 { "cpus", "", do_info_cpus,
1746 "", "show infos for each CPU" },
1747 { "history", "", do_info_history,
1748 "", "show the command line history", },
1749 { "irq", "", irq_info,
1750 "", "show the interrupts statistics (if available)", },
1751 { "pic", "", pic_info,
1752 "", "show i8259 (PIC) state", },
1753 { "pci", "", pci_info,
1754 "", "show PCI info", },
1755 #if defined(TARGET_I386) || defined(TARGET_SH4)
1756 { "tlb", "", tlb_info,
1757 "", "show virtual to physical memory mappings", },
1758 #endif
1759 #if defined(TARGET_I386)
1760 { "mem", "", mem_info,
1761 "", "show the active virtual memory mappings", },
1762 { "hpet", "", do_info_hpet,
1763 "", "show state of HPET", },
1764 #endif
1765 { "jit", "", do_info_jit,
1766 "", "show dynamic compiler info", },
1767 { "kqemu", "", do_info_kqemu,
1768 "", "show KQEMU information", },
1769 { "kvm", "", do_info_kvm,
1770 "", "show KVM information", },
1771 { "usb", "", usb_info,
1772 "", "show guest USB devices", },
1773 { "usbhost", "", usb_host_info,
1774 "", "show host USB devices", },
1775 { "profile", "", do_info_profile,
1776 "", "show profiling information", },
1777 { "capture", "", do_info_capture,
1778 "", "show capture information" },
1779 { "snapshots", "", do_info_snapshots,
1780 "", "show the currently saved VM snapshots" },
1781 { "status", "", do_info_status,
1782 "", "show the current VM status (running|paused)" },
1783 { "pcmcia", "", pcmcia_info,
1784 "", "show guest PCMCIA status" },
1785 { "mice", "", do_info_mice,
1786 "", "show which guest mouse is receiving events" },
1787 { "vnc", "", do_info_vnc,
1788 "", "show the vnc server status"},
1789 { "name", "", do_info_name,
1790 "", "show the current VM name" },
1791 { "uuid", "", do_info_uuid,
1792 "", "show the current VM UUID" },
1793 #if defined(TARGET_PPC)
1794 { "cpustats", "", do_info_cpu_stats,
1795 "", "show CPU statistics", },
1796 #endif
1797 #if defined(CONFIG_SLIRP)
1798 { "slirp", "", do_info_slirp,
1799 "", "show SLIRP statistics", },
1800 #endif
1801 { "migrate", "", do_info_migrate, "", "show migration status" },
1802 { "balloon", "", do_info_balloon,
1803 "", "show balloon information" },
1804 { NULL, NULL, },
1807 /*******************************************************************/
1809 static const char *pch;
1810 static jmp_buf expr_env;
1812 #define MD_TLONG 0
1813 #define MD_I32 1
1815 typedef struct MonitorDef {
1816 const char *name;
1817 int offset;
1818 target_long (*get_value)(const struct MonitorDef *md, int val);
1819 int type;
1820 } MonitorDef;
1822 #if defined(TARGET_I386)
1823 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1825 CPUState *env = mon_get_cpu();
1826 if (!env)
1827 return 0;
1828 return env->eip + env->segs[R_CS].base;
1830 #endif
1832 #if defined(TARGET_PPC)
1833 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1835 CPUState *env = mon_get_cpu();
1836 unsigned int u;
1837 int i;
1839 if (!env)
1840 return 0;
1842 u = 0;
1843 for (i = 0; i < 8; i++)
1844 u |= env->crf[i] << (32 - (4 * i));
1846 return u;
1849 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1851 CPUState *env = mon_get_cpu();
1852 if (!env)
1853 return 0;
1854 return env->msr;
1857 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1859 CPUState *env = mon_get_cpu();
1860 if (!env)
1861 return 0;
1862 return env->xer;
1865 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1867 CPUState *env = mon_get_cpu();
1868 if (!env)
1869 return 0;
1870 return cpu_ppc_load_decr(env);
1873 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1875 CPUState *env = mon_get_cpu();
1876 if (!env)
1877 return 0;
1878 return cpu_ppc_load_tbu(env);
1881 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1883 CPUState *env = mon_get_cpu();
1884 if (!env)
1885 return 0;
1886 return cpu_ppc_load_tbl(env);
1888 #endif
1890 #if defined(TARGET_SPARC)
1891 #ifndef TARGET_SPARC64
1892 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1894 CPUState *env = mon_get_cpu();
1895 if (!env)
1896 return 0;
1897 return GET_PSR(env);
1899 #endif
1901 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1903 CPUState *env = mon_get_cpu();
1904 if (!env)
1905 return 0;
1906 return env->regwptr[val];
1908 #endif
1910 static const MonitorDef monitor_defs[] = {
1911 #ifdef TARGET_I386
1913 #define SEG(name, seg) \
1914 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1915 { name ".base", offsetof(CPUState, segs[seg].base) },\
1916 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1918 { "eax", offsetof(CPUState, regs[0]) },
1919 { "ecx", offsetof(CPUState, regs[1]) },
1920 { "edx", offsetof(CPUState, regs[2]) },
1921 { "ebx", offsetof(CPUState, regs[3]) },
1922 { "esp|sp", offsetof(CPUState, regs[4]) },
1923 { "ebp|fp", offsetof(CPUState, regs[5]) },
1924 { "esi", offsetof(CPUState, regs[6]) },
1925 { "edi", offsetof(CPUState, regs[7]) },
1926 #ifdef TARGET_X86_64
1927 { "r8", offsetof(CPUState, regs[8]) },
1928 { "r9", offsetof(CPUState, regs[9]) },
1929 { "r10", offsetof(CPUState, regs[10]) },
1930 { "r11", offsetof(CPUState, regs[11]) },
1931 { "r12", offsetof(CPUState, regs[12]) },
1932 { "r13", offsetof(CPUState, regs[13]) },
1933 { "r14", offsetof(CPUState, regs[14]) },
1934 { "r15", offsetof(CPUState, regs[15]) },
1935 #endif
1936 { "eflags", offsetof(CPUState, eflags) },
1937 { "eip", offsetof(CPUState, eip) },
1938 SEG("cs", R_CS)
1939 SEG("ds", R_DS)
1940 SEG("es", R_ES)
1941 SEG("ss", R_SS)
1942 SEG("fs", R_FS)
1943 SEG("gs", R_GS)
1944 { "pc", 0, monitor_get_pc, },
1945 #elif defined(TARGET_PPC)
1946 /* General purpose registers */
1947 { "r0", offsetof(CPUState, gpr[0]) },
1948 { "r1", offsetof(CPUState, gpr[1]) },
1949 { "r2", offsetof(CPUState, gpr[2]) },
1950 { "r3", offsetof(CPUState, gpr[3]) },
1951 { "r4", offsetof(CPUState, gpr[4]) },
1952 { "r5", offsetof(CPUState, gpr[5]) },
1953 { "r6", offsetof(CPUState, gpr[6]) },
1954 { "r7", offsetof(CPUState, gpr[7]) },
1955 { "r8", offsetof(CPUState, gpr[8]) },
1956 { "r9", offsetof(CPUState, gpr[9]) },
1957 { "r10", offsetof(CPUState, gpr[10]) },
1958 { "r11", offsetof(CPUState, gpr[11]) },
1959 { "r12", offsetof(CPUState, gpr[12]) },
1960 { "r13", offsetof(CPUState, gpr[13]) },
1961 { "r14", offsetof(CPUState, gpr[14]) },
1962 { "r15", offsetof(CPUState, gpr[15]) },
1963 { "r16", offsetof(CPUState, gpr[16]) },
1964 { "r17", offsetof(CPUState, gpr[17]) },
1965 { "r18", offsetof(CPUState, gpr[18]) },
1966 { "r19", offsetof(CPUState, gpr[19]) },
1967 { "r20", offsetof(CPUState, gpr[20]) },
1968 { "r21", offsetof(CPUState, gpr[21]) },
1969 { "r22", offsetof(CPUState, gpr[22]) },
1970 { "r23", offsetof(CPUState, gpr[23]) },
1971 { "r24", offsetof(CPUState, gpr[24]) },
1972 { "r25", offsetof(CPUState, gpr[25]) },
1973 { "r26", offsetof(CPUState, gpr[26]) },
1974 { "r27", offsetof(CPUState, gpr[27]) },
1975 { "r28", offsetof(CPUState, gpr[28]) },
1976 { "r29", offsetof(CPUState, gpr[29]) },
1977 { "r30", offsetof(CPUState, gpr[30]) },
1978 { "r31", offsetof(CPUState, gpr[31]) },
1979 /* Floating point registers */
1980 { "f0", offsetof(CPUState, fpr[0]) },
1981 { "f1", offsetof(CPUState, fpr[1]) },
1982 { "f2", offsetof(CPUState, fpr[2]) },
1983 { "f3", offsetof(CPUState, fpr[3]) },
1984 { "f4", offsetof(CPUState, fpr[4]) },
1985 { "f5", offsetof(CPUState, fpr[5]) },
1986 { "f6", offsetof(CPUState, fpr[6]) },
1987 { "f7", offsetof(CPUState, fpr[7]) },
1988 { "f8", offsetof(CPUState, fpr[8]) },
1989 { "f9", offsetof(CPUState, fpr[9]) },
1990 { "f10", offsetof(CPUState, fpr[10]) },
1991 { "f11", offsetof(CPUState, fpr[11]) },
1992 { "f12", offsetof(CPUState, fpr[12]) },
1993 { "f13", offsetof(CPUState, fpr[13]) },
1994 { "f14", offsetof(CPUState, fpr[14]) },
1995 { "f15", offsetof(CPUState, fpr[15]) },
1996 { "f16", offsetof(CPUState, fpr[16]) },
1997 { "f17", offsetof(CPUState, fpr[17]) },
1998 { "f18", offsetof(CPUState, fpr[18]) },
1999 { "f19", offsetof(CPUState, fpr[19]) },
2000 { "f20", offsetof(CPUState, fpr[20]) },
2001 { "f21", offsetof(CPUState, fpr[21]) },
2002 { "f22", offsetof(CPUState, fpr[22]) },
2003 { "f23", offsetof(CPUState, fpr[23]) },
2004 { "f24", offsetof(CPUState, fpr[24]) },
2005 { "f25", offsetof(CPUState, fpr[25]) },
2006 { "f26", offsetof(CPUState, fpr[26]) },
2007 { "f27", offsetof(CPUState, fpr[27]) },
2008 { "f28", offsetof(CPUState, fpr[28]) },
2009 { "f29", offsetof(CPUState, fpr[29]) },
2010 { "f30", offsetof(CPUState, fpr[30]) },
2011 { "f31", offsetof(CPUState, fpr[31]) },
2012 { "fpscr", offsetof(CPUState, fpscr) },
2013 /* Next instruction pointer */
2014 { "nip|pc", offsetof(CPUState, nip) },
2015 { "lr", offsetof(CPUState, lr) },
2016 { "ctr", offsetof(CPUState, ctr) },
2017 { "decr", 0, &monitor_get_decr, },
2018 { "ccr", 0, &monitor_get_ccr, },
2019 /* Machine state register */
2020 { "msr", 0, &monitor_get_msr, },
2021 { "xer", 0, &monitor_get_xer, },
2022 { "tbu", 0, &monitor_get_tbu, },
2023 { "tbl", 0, &monitor_get_tbl, },
2024 #if defined(TARGET_PPC64)
2025 /* Address space register */
2026 { "asr", offsetof(CPUState, asr) },
2027 #endif
2028 /* Segment registers */
2029 { "sdr1", offsetof(CPUState, sdr1) },
2030 { "sr0", offsetof(CPUState, sr[0]) },
2031 { "sr1", offsetof(CPUState, sr[1]) },
2032 { "sr2", offsetof(CPUState, sr[2]) },
2033 { "sr3", offsetof(CPUState, sr[3]) },
2034 { "sr4", offsetof(CPUState, sr[4]) },
2035 { "sr5", offsetof(CPUState, sr[5]) },
2036 { "sr6", offsetof(CPUState, sr[6]) },
2037 { "sr7", offsetof(CPUState, sr[7]) },
2038 { "sr8", offsetof(CPUState, sr[8]) },
2039 { "sr9", offsetof(CPUState, sr[9]) },
2040 { "sr10", offsetof(CPUState, sr[10]) },
2041 { "sr11", offsetof(CPUState, sr[11]) },
2042 { "sr12", offsetof(CPUState, sr[12]) },
2043 { "sr13", offsetof(CPUState, sr[13]) },
2044 { "sr14", offsetof(CPUState, sr[14]) },
2045 { "sr15", offsetof(CPUState, sr[15]) },
2046 /* Too lazy to put BATs and SPRs ... */
2047 #elif defined(TARGET_SPARC)
2048 { "g0", offsetof(CPUState, gregs[0]) },
2049 { "g1", offsetof(CPUState, gregs[1]) },
2050 { "g2", offsetof(CPUState, gregs[2]) },
2051 { "g3", offsetof(CPUState, gregs[3]) },
2052 { "g4", offsetof(CPUState, gregs[4]) },
2053 { "g5", offsetof(CPUState, gregs[5]) },
2054 { "g6", offsetof(CPUState, gregs[6]) },
2055 { "g7", offsetof(CPUState, gregs[7]) },
2056 { "o0", 0, monitor_get_reg },
2057 { "o1", 1, monitor_get_reg },
2058 { "o2", 2, monitor_get_reg },
2059 { "o3", 3, monitor_get_reg },
2060 { "o4", 4, monitor_get_reg },
2061 { "o5", 5, monitor_get_reg },
2062 { "o6", 6, monitor_get_reg },
2063 { "o7", 7, monitor_get_reg },
2064 { "l0", 8, monitor_get_reg },
2065 { "l1", 9, monitor_get_reg },
2066 { "l2", 10, monitor_get_reg },
2067 { "l3", 11, monitor_get_reg },
2068 { "l4", 12, monitor_get_reg },
2069 { "l5", 13, monitor_get_reg },
2070 { "l6", 14, monitor_get_reg },
2071 { "l7", 15, monitor_get_reg },
2072 { "i0", 16, monitor_get_reg },
2073 { "i1", 17, monitor_get_reg },
2074 { "i2", 18, monitor_get_reg },
2075 { "i3", 19, monitor_get_reg },
2076 { "i4", 20, monitor_get_reg },
2077 { "i5", 21, monitor_get_reg },
2078 { "i6", 22, monitor_get_reg },
2079 { "i7", 23, monitor_get_reg },
2080 { "pc", offsetof(CPUState, pc) },
2081 { "npc", offsetof(CPUState, npc) },
2082 { "y", offsetof(CPUState, y) },
2083 #ifndef TARGET_SPARC64
2084 { "psr", 0, &monitor_get_psr, },
2085 { "wim", offsetof(CPUState, wim) },
2086 #endif
2087 { "tbr", offsetof(CPUState, tbr) },
2088 { "fsr", offsetof(CPUState, fsr) },
2089 { "f0", offsetof(CPUState, fpr[0]) },
2090 { "f1", offsetof(CPUState, fpr[1]) },
2091 { "f2", offsetof(CPUState, fpr[2]) },
2092 { "f3", offsetof(CPUState, fpr[3]) },
2093 { "f4", offsetof(CPUState, fpr[4]) },
2094 { "f5", offsetof(CPUState, fpr[5]) },
2095 { "f6", offsetof(CPUState, fpr[6]) },
2096 { "f7", offsetof(CPUState, fpr[7]) },
2097 { "f8", offsetof(CPUState, fpr[8]) },
2098 { "f9", offsetof(CPUState, fpr[9]) },
2099 { "f10", offsetof(CPUState, fpr[10]) },
2100 { "f11", offsetof(CPUState, fpr[11]) },
2101 { "f12", offsetof(CPUState, fpr[12]) },
2102 { "f13", offsetof(CPUState, fpr[13]) },
2103 { "f14", offsetof(CPUState, fpr[14]) },
2104 { "f15", offsetof(CPUState, fpr[15]) },
2105 { "f16", offsetof(CPUState, fpr[16]) },
2106 { "f17", offsetof(CPUState, fpr[17]) },
2107 { "f18", offsetof(CPUState, fpr[18]) },
2108 { "f19", offsetof(CPUState, fpr[19]) },
2109 { "f20", offsetof(CPUState, fpr[20]) },
2110 { "f21", offsetof(CPUState, fpr[21]) },
2111 { "f22", offsetof(CPUState, fpr[22]) },
2112 { "f23", offsetof(CPUState, fpr[23]) },
2113 { "f24", offsetof(CPUState, fpr[24]) },
2114 { "f25", offsetof(CPUState, fpr[25]) },
2115 { "f26", offsetof(CPUState, fpr[26]) },
2116 { "f27", offsetof(CPUState, fpr[27]) },
2117 { "f28", offsetof(CPUState, fpr[28]) },
2118 { "f29", offsetof(CPUState, fpr[29]) },
2119 { "f30", offsetof(CPUState, fpr[30]) },
2120 { "f31", offsetof(CPUState, fpr[31]) },
2121 #ifdef TARGET_SPARC64
2122 { "f32", offsetof(CPUState, fpr[32]) },
2123 { "f34", offsetof(CPUState, fpr[34]) },
2124 { "f36", offsetof(CPUState, fpr[36]) },
2125 { "f38", offsetof(CPUState, fpr[38]) },
2126 { "f40", offsetof(CPUState, fpr[40]) },
2127 { "f42", offsetof(CPUState, fpr[42]) },
2128 { "f44", offsetof(CPUState, fpr[44]) },
2129 { "f46", offsetof(CPUState, fpr[46]) },
2130 { "f48", offsetof(CPUState, fpr[48]) },
2131 { "f50", offsetof(CPUState, fpr[50]) },
2132 { "f52", offsetof(CPUState, fpr[52]) },
2133 { "f54", offsetof(CPUState, fpr[54]) },
2134 { "f56", offsetof(CPUState, fpr[56]) },
2135 { "f58", offsetof(CPUState, fpr[58]) },
2136 { "f60", offsetof(CPUState, fpr[60]) },
2137 { "f62", offsetof(CPUState, fpr[62]) },
2138 { "asi", offsetof(CPUState, asi) },
2139 { "pstate", offsetof(CPUState, pstate) },
2140 { "cansave", offsetof(CPUState, cansave) },
2141 { "canrestore", offsetof(CPUState, canrestore) },
2142 { "otherwin", offsetof(CPUState, otherwin) },
2143 { "wstate", offsetof(CPUState, wstate) },
2144 { "cleanwin", offsetof(CPUState, cleanwin) },
2145 { "fprs", offsetof(CPUState, fprs) },
2146 #endif
2147 #endif
2148 { NULL },
2151 static void expr_error(Monitor *mon, const char *msg)
2153 monitor_printf(mon, "%s\n", msg);
2154 longjmp(expr_env, 1);
2157 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2158 static int get_monitor_def(target_long *pval, const char *name)
2160 const MonitorDef *md;
2161 void *ptr;
2163 for(md = monitor_defs; md->name != NULL; md++) {
2164 if (compare_cmd(name, md->name)) {
2165 if (md->get_value) {
2166 *pval = md->get_value(md, md->offset);
2167 } else {
2168 CPUState *env = mon_get_cpu();
2169 if (!env)
2170 return -2;
2171 ptr = (uint8_t *)env + md->offset;
2172 switch(md->type) {
2173 case MD_I32:
2174 *pval = *(int32_t *)ptr;
2175 break;
2176 case MD_TLONG:
2177 *pval = *(target_long *)ptr;
2178 break;
2179 default:
2180 *pval = 0;
2181 break;
2184 return 0;
2187 return -1;
2190 static void next(void)
2192 if (pch != '\0') {
2193 pch++;
2194 while (qemu_isspace(*pch))
2195 pch++;
2199 static int64_t expr_sum(Monitor *mon);
2201 static int64_t expr_unary(Monitor *mon)
2203 int64_t n;
2204 char *p;
2205 int ret;
2207 switch(*pch) {
2208 case '+':
2209 next();
2210 n = expr_unary(mon);
2211 break;
2212 case '-':
2213 next();
2214 n = -expr_unary(mon);
2215 break;
2216 case '~':
2217 next();
2218 n = ~expr_unary(mon);
2219 break;
2220 case '(':
2221 next();
2222 n = expr_sum(mon);
2223 if (*pch != ')') {
2224 expr_error(mon, "')' expected");
2226 next();
2227 break;
2228 case '\'':
2229 pch++;
2230 if (*pch == '\0')
2231 expr_error(mon, "character constant expected");
2232 n = *pch;
2233 pch++;
2234 if (*pch != '\'')
2235 expr_error(mon, "missing terminating \' character");
2236 next();
2237 break;
2238 case '$':
2240 char buf[128], *q;
2241 target_long reg=0;
2243 pch++;
2244 q = buf;
2245 while ((*pch >= 'a' && *pch <= 'z') ||
2246 (*pch >= 'A' && *pch <= 'Z') ||
2247 (*pch >= '0' && *pch <= '9') ||
2248 *pch == '_' || *pch == '.') {
2249 if ((q - buf) < sizeof(buf) - 1)
2250 *q++ = *pch;
2251 pch++;
2253 while (qemu_isspace(*pch))
2254 pch++;
2255 *q = 0;
2256 ret = get_monitor_def(&reg, buf);
2257 if (ret == -1)
2258 expr_error(mon, "unknown register");
2259 else if (ret == -2)
2260 expr_error(mon, "no cpu defined");
2261 n = reg;
2263 break;
2264 case '\0':
2265 expr_error(mon, "unexpected end of expression");
2266 n = 0;
2267 break;
2268 default:
2269 #if TARGET_PHYS_ADDR_BITS > 32
2270 n = strtoull(pch, &p, 0);
2271 #else
2272 n = strtoul(pch, &p, 0);
2273 #endif
2274 if (pch == p) {
2275 expr_error(mon, "invalid char in expression");
2277 pch = p;
2278 while (qemu_isspace(*pch))
2279 pch++;
2280 break;
2282 return n;
2286 static int64_t expr_prod(Monitor *mon)
2288 int64_t val, val2;
2289 int op;
2291 val = expr_unary(mon);
2292 for(;;) {
2293 op = *pch;
2294 if (op != '*' && op != '/' && op != '%')
2295 break;
2296 next();
2297 val2 = expr_unary(mon);
2298 switch(op) {
2299 default:
2300 case '*':
2301 val *= val2;
2302 break;
2303 case '/':
2304 case '%':
2305 if (val2 == 0)
2306 expr_error(mon, "division by zero");
2307 if (op == '/')
2308 val /= val2;
2309 else
2310 val %= val2;
2311 break;
2314 return val;
2317 static int64_t expr_logic(Monitor *mon)
2319 int64_t val, val2;
2320 int op;
2322 val = expr_prod(mon);
2323 for(;;) {
2324 op = *pch;
2325 if (op != '&' && op != '|' && op != '^')
2326 break;
2327 next();
2328 val2 = expr_prod(mon);
2329 switch(op) {
2330 default:
2331 case '&':
2332 val &= val2;
2333 break;
2334 case '|':
2335 val |= val2;
2336 break;
2337 case '^':
2338 val ^= val2;
2339 break;
2342 return val;
2345 static int64_t expr_sum(Monitor *mon)
2347 int64_t val, val2;
2348 int op;
2350 val = expr_logic(mon);
2351 for(;;) {
2352 op = *pch;
2353 if (op != '+' && op != '-')
2354 break;
2355 next();
2356 val2 = expr_logic(mon);
2357 if (op == '+')
2358 val += val2;
2359 else
2360 val -= val2;
2362 return val;
2365 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2367 pch = *pp;
2368 if (setjmp(expr_env)) {
2369 *pp = pch;
2370 return -1;
2372 while (qemu_isspace(*pch))
2373 pch++;
2374 *pval = expr_sum(mon);
2375 *pp = pch;
2376 return 0;
2379 static int get_str(char *buf, int buf_size, const char **pp)
2381 const char *p;
2382 char *q;
2383 int c;
2385 q = buf;
2386 p = *pp;
2387 while (qemu_isspace(*p))
2388 p++;
2389 if (*p == '\0') {
2390 fail:
2391 *q = '\0';
2392 *pp = p;
2393 return -1;
2395 if (*p == '\"') {
2396 p++;
2397 while (*p != '\0' && *p != '\"') {
2398 if (*p == '\\') {
2399 p++;
2400 c = *p++;
2401 switch(c) {
2402 case 'n':
2403 c = '\n';
2404 break;
2405 case 'r':
2406 c = '\r';
2407 break;
2408 case '\\':
2409 case '\'':
2410 case '\"':
2411 break;
2412 default:
2413 qemu_printf("unsupported escape code: '\\%c'\n", c);
2414 goto fail;
2416 if ((q - buf) < buf_size - 1) {
2417 *q++ = c;
2419 } else {
2420 if ((q - buf) < buf_size - 1) {
2421 *q++ = *p;
2423 p++;
2426 if (*p != '\"') {
2427 qemu_printf("unterminated string\n");
2428 goto fail;
2430 p++;
2431 } else {
2432 while (*p != '\0' && !qemu_isspace(*p)) {
2433 if ((q - buf) < buf_size - 1) {
2434 *q++ = *p;
2436 p++;
2439 *q = '\0';
2440 *pp = p;
2441 return 0;
2444 static int default_fmt_format = 'x';
2445 static int default_fmt_size = 4;
2447 #define MAX_ARGS 16
2449 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2451 const char *p, *pstart, *typestr;
2452 char *q;
2453 int c, nb_args, len, i, has_arg;
2454 const mon_cmd_t *cmd;
2455 char cmdname[256];
2456 char buf[1024];
2457 void *str_allocated[MAX_ARGS];
2458 void *args[MAX_ARGS];
2459 void (*handler_0)(Monitor *mon);
2460 void (*handler_1)(Monitor *mon, void *arg0);
2461 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2462 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2463 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2464 void *arg3);
2465 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2466 void *arg3, void *arg4);
2467 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2468 void *arg3, void *arg4, void *arg5);
2469 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2470 void *arg3, void *arg4, void *arg5, void *arg6);
2472 #ifdef DEBUG
2473 monitor_printf(mon, "command='%s'\n", cmdline);
2474 #endif
2476 /* extract the command name */
2477 p = cmdline;
2478 q = cmdname;
2479 while (qemu_isspace(*p))
2480 p++;
2481 if (*p == '\0')
2482 return;
2483 pstart = p;
2484 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2485 p++;
2486 len = p - pstart;
2487 if (len > sizeof(cmdname) - 1)
2488 len = sizeof(cmdname) - 1;
2489 memcpy(cmdname, pstart, len);
2490 cmdname[len] = '\0';
2492 /* find the command */
2493 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2494 if (compare_cmd(cmdname, cmd->name))
2495 goto found;
2497 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2498 return;
2499 found:
2501 for(i = 0; i < MAX_ARGS; i++)
2502 str_allocated[i] = NULL;
2504 /* parse the parameters */
2505 typestr = cmd->args_type;
2506 nb_args = 0;
2507 for(;;) {
2508 c = *typestr;
2509 if (c == '\0')
2510 break;
2511 typestr++;
2512 switch(c) {
2513 case 'F':
2514 case 'B':
2515 case 's':
2517 int ret;
2518 char *str;
2520 while (qemu_isspace(*p))
2521 p++;
2522 if (*typestr == '?') {
2523 typestr++;
2524 if (*p == '\0') {
2525 /* no optional string: NULL argument */
2526 str = NULL;
2527 goto add_str;
2530 ret = get_str(buf, sizeof(buf), &p);
2531 if (ret < 0) {
2532 switch(c) {
2533 case 'F':
2534 monitor_printf(mon, "%s: filename expected\n",
2535 cmdname);
2536 break;
2537 case 'B':
2538 monitor_printf(mon, "%s: block device name expected\n",
2539 cmdname);
2540 break;
2541 default:
2542 monitor_printf(mon, "%s: string expected\n", cmdname);
2543 break;
2545 goto fail;
2547 str = qemu_malloc(strlen(buf) + 1);
2548 pstrcpy(str, sizeof(buf), buf);
2549 str_allocated[nb_args] = str;
2550 add_str:
2551 if (nb_args >= MAX_ARGS) {
2552 error_args:
2553 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2554 goto fail;
2556 args[nb_args++] = str;
2558 break;
2559 case '/':
2561 int count, format, size;
2563 while (qemu_isspace(*p))
2564 p++;
2565 if (*p == '/') {
2566 /* format found */
2567 p++;
2568 count = 1;
2569 if (qemu_isdigit(*p)) {
2570 count = 0;
2571 while (qemu_isdigit(*p)) {
2572 count = count * 10 + (*p - '0');
2573 p++;
2576 size = -1;
2577 format = -1;
2578 for(;;) {
2579 switch(*p) {
2580 case 'o':
2581 case 'd':
2582 case 'u':
2583 case 'x':
2584 case 'i':
2585 case 'c':
2586 format = *p++;
2587 break;
2588 case 'b':
2589 size = 1;
2590 p++;
2591 break;
2592 case 'h':
2593 size = 2;
2594 p++;
2595 break;
2596 case 'w':
2597 size = 4;
2598 p++;
2599 break;
2600 case 'g':
2601 case 'L':
2602 size = 8;
2603 p++;
2604 break;
2605 default:
2606 goto next;
2609 next:
2610 if (*p != '\0' && !qemu_isspace(*p)) {
2611 monitor_printf(mon, "invalid char in format: '%c'\n",
2612 *p);
2613 goto fail;
2615 if (format < 0)
2616 format = default_fmt_format;
2617 if (format != 'i') {
2618 /* for 'i', not specifying a size gives -1 as size */
2619 if (size < 0)
2620 size = default_fmt_size;
2621 default_fmt_size = size;
2623 default_fmt_format = format;
2624 } else {
2625 count = 1;
2626 format = default_fmt_format;
2627 if (format != 'i') {
2628 size = default_fmt_size;
2629 } else {
2630 size = -1;
2633 if (nb_args + 3 > MAX_ARGS)
2634 goto error_args;
2635 args[nb_args++] = (void*)(long)count;
2636 args[nb_args++] = (void*)(long)format;
2637 args[nb_args++] = (void*)(long)size;
2639 break;
2640 case 'i':
2641 case 'l':
2643 int64_t val;
2645 while (qemu_isspace(*p))
2646 p++;
2647 if (*typestr == '?' || *typestr == '.') {
2648 if (*typestr == '?') {
2649 if (*p == '\0')
2650 has_arg = 0;
2651 else
2652 has_arg = 1;
2653 } else {
2654 if (*p == '.') {
2655 p++;
2656 while (qemu_isspace(*p))
2657 p++;
2658 has_arg = 1;
2659 } else {
2660 has_arg = 0;
2663 typestr++;
2664 if (nb_args >= MAX_ARGS)
2665 goto error_args;
2666 args[nb_args++] = (void *)(long)has_arg;
2667 if (!has_arg) {
2668 if (nb_args >= MAX_ARGS)
2669 goto error_args;
2670 val = -1;
2671 goto add_num;
2674 if (get_expr(mon, &val, &p))
2675 goto fail;
2676 add_num:
2677 if (c == 'i') {
2678 if (nb_args >= MAX_ARGS)
2679 goto error_args;
2680 args[nb_args++] = (void *)(long)val;
2681 } else {
2682 if ((nb_args + 1) >= MAX_ARGS)
2683 goto error_args;
2684 #if TARGET_PHYS_ADDR_BITS > 32
2685 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2686 #else
2687 args[nb_args++] = (void *)0;
2688 #endif
2689 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2692 break;
2693 case '-':
2695 int has_option;
2696 /* option */
2698 c = *typestr++;
2699 if (c == '\0')
2700 goto bad_type;
2701 while (qemu_isspace(*p))
2702 p++;
2703 has_option = 0;
2704 if (*p == '-') {
2705 p++;
2706 if (*p != c) {
2707 monitor_printf(mon, "%s: unsupported option -%c\n",
2708 cmdname, *p);
2709 goto fail;
2711 p++;
2712 has_option = 1;
2714 if (nb_args >= MAX_ARGS)
2715 goto error_args;
2716 args[nb_args++] = (void *)(long)has_option;
2718 break;
2719 default:
2720 bad_type:
2721 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2722 goto fail;
2725 /* check that all arguments were parsed */
2726 while (qemu_isspace(*p))
2727 p++;
2728 if (*p != '\0') {
2729 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2730 cmdname);
2731 goto fail;
2734 switch(nb_args) {
2735 case 0:
2736 handler_0 = cmd->handler;
2737 handler_0(mon);
2738 break;
2739 case 1:
2740 handler_1 = cmd->handler;
2741 handler_1(mon, args[0]);
2742 break;
2743 case 2:
2744 handler_2 = cmd->handler;
2745 handler_2(mon, args[0], args[1]);
2746 break;
2747 case 3:
2748 handler_3 = cmd->handler;
2749 handler_3(mon, args[0], args[1], args[2]);
2750 break;
2751 case 4:
2752 handler_4 = cmd->handler;
2753 handler_4(mon, args[0], args[1], args[2], args[3]);
2754 break;
2755 case 5:
2756 handler_5 = cmd->handler;
2757 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2758 break;
2759 case 6:
2760 handler_6 = cmd->handler;
2761 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2762 break;
2763 case 7:
2764 handler_7 = cmd->handler;
2765 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2766 args[6]);
2767 break;
2768 default:
2769 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2770 goto fail;
2772 fail:
2773 for(i = 0; i < MAX_ARGS; i++)
2774 qemu_free(str_allocated[i]);
2775 return;
2778 static void cmd_completion(const char *name, const char *list)
2780 const char *p, *pstart;
2781 char cmd[128];
2782 int len;
2784 p = list;
2785 for(;;) {
2786 pstart = p;
2787 p = strchr(p, '|');
2788 if (!p)
2789 p = pstart + strlen(pstart);
2790 len = p - pstart;
2791 if (len > sizeof(cmd) - 2)
2792 len = sizeof(cmd) - 2;
2793 memcpy(cmd, pstart, len);
2794 cmd[len] = '\0';
2795 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2796 readline_add_completion(cur_mon->rs, cmd);
2798 if (*p == '\0')
2799 break;
2800 p++;
2804 static void file_completion(const char *input)
2806 DIR *ffs;
2807 struct dirent *d;
2808 char path[1024];
2809 char file[1024], file_prefix[1024];
2810 int input_path_len;
2811 const char *p;
2813 p = strrchr(input, '/');
2814 if (!p) {
2815 input_path_len = 0;
2816 pstrcpy(file_prefix, sizeof(file_prefix), input);
2817 pstrcpy(path, sizeof(path), ".");
2818 } else {
2819 input_path_len = p - input + 1;
2820 memcpy(path, input, input_path_len);
2821 if (input_path_len > sizeof(path) - 1)
2822 input_path_len = sizeof(path) - 1;
2823 path[input_path_len] = '\0';
2824 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2826 #ifdef DEBUG_COMPLETION
2827 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2828 input, path, file_prefix);
2829 #endif
2830 ffs = opendir(path);
2831 if (!ffs)
2832 return;
2833 for(;;) {
2834 struct stat sb;
2835 d = readdir(ffs);
2836 if (!d)
2837 break;
2838 if (strstart(d->d_name, file_prefix, NULL)) {
2839 memcpy(file, input, input_path_len);
2840 if (input_path_len < sizeof(file))
2841 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2842 d->d_name);
2843 /* stat the file to find out if it's a directory.
2844 * In that case add a slash to speed up typing long paths
2846 stat(file, &sb);
2847 if(S_ISDIR(sb.st_mode))
2848 pstrcat(file, sizeof(file), "/");
2849 readline_add_completion(cur_mon->rs, file);
2852 closedir(ffs);
2855 static void block_completion_it(void *opaque, BlockDriverState *bs)
2857 const char *name = bdrv_get_device_name(bs);
2858 const char *input = opaque;
2860 if (input[0] == '\0' ||
2861 !strncmp(name, (char *)input, strlen(input))) {
2862 readline_add_completion(cur_mon->rs, name);
2866 /* NOTE: this parser is an approximate form of the real command parser */
2867 static void parse_cmdline(const char *cmdline,
2868 int *pnb_args, char **args)
2870 const char *p;
2871 int nb_args, ret;
2872 char buf[1024];
2874 p = cmdline;
2875 nb_args = 0;
2876 for(;;) {
2877 while (qemu_isspace(*p))
2878 p++;
2879 if (*p == '\0')
2880 break;
2881 if (nb_args >= MAX_ARGS)
2882 break;
2883 ret = get_str(buf, sizeof(buf), &p);
2884 args[nb_args] = qemu_strdup(buf);
2885 nb_args++;
2886 if (ret < 0)
2887 break;
2889 *pnb_args = nb_args;
2892 static void monitor_find_completion(const char *cmdline)
2894 const char *cmdname;
2895 char *args[MAX_ARGS];
2896 int nb_args, i, len;
2897 const char *ptype, *str;
2898 const mon_cmd_t *cmd;
2899 const KeyDef *key;
2901 parse_cmdline(cmdline, &nb_args, args);
2902 #ifdef DEBUG_COMPLETION
2903 for(i = 0; i < nb_args; i++) {
2904 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2906 #endif
2908 /* if the line ends with a space, it means we want to complete the
2909 next arg */
2910 len = strlen(cmdline);
2911 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2912 if (nb_args >= MAX_ARGS)
2913 return;
2914 args[nb_args++] = qemu_strdup("");
2916 if (nb_args <= 1) {
2917 /* command completion */
2918 if (nb_args == 0)
2919 cmdname = "";
2920 else
2921 cmdname = args[0];
2922 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2923 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2924 cmd_completion(cmdname, cmd->name);
2926 } else {
2927 /* find the command */
2928 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2929 if (compare_cmd(args[0], cmd->name))
2930 goto found;
2932 return;
2933 found:
2934 ptype = cmd->args_type;
2935 for(i = 0; i < nb_args - 2; i++) {
2936 if (*ptype != '\0') {
2937 ptype++;
2938 while (*ptype == '?')
2939 ptype++;
2942 str = args[nb_args - 1];
2943 switch(*ptype) {
2944 case 'F':
2945 /* file completion */
2946 readline_set_completion_index(cur_mon->rs, strlen(str));
2947 file_completion(str);
2948 break;
2949 case 'B':
2950 /* block device name completion */
2951 readline_set_completion_index(cur_mon->rs, strlen(str));
2952 bdrv_iterate(block_completion_it, (void *)str);
2953 break;
2954 case 's':
2955 /* XXX: more generic ? */
2956 if (!strcmp(cmd->name, "info")) {
2957 readline_set_completion_index(cur_mon->rs, strlen(str));
2958 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2959 cmd_completion(str, cmd->name);
2961 } else if (!strcmp(cmd->name, "sendkey")) {
2962 char *sep = strrchr(str, '-');
2963 if (sep)
2964 str = sep + 1;
2965 readline_set_completion_index(cur_mon->rs, strlen(str));
2966 for(key = key_defs; key->name != NULL; key++) {
2967 cmd_completion(str, key->name);
2970 break;
2971 default:
2972 break;
2975 for(i = 0; i < nb_args; i++)
2976 qemu_free(args[i]);
2979 static int monitor_can_read(void *opaque)
2981 Monitor *mon = opaque;
2983 return (mon->suspend_cnt == 0) ? 128 : 0;
2986 static void monitor_read(void *opaque, const uint8_t *buf, int size)
2988 Monitor *old_mon = cur_mon;
2989 int i;
2991 cur_mon = opaque;
2993 if (cur_mon->rs) {
2994 for (i = 0; i < size; i++)
2995 readline_handle_byte(cur_mon->rs, buf[i]);
2996 } else {
2997 if (size == 0 || buf[size - 1] != 0)
2998 monitor_printf(cur_mon, "corrupted command\n");
2999 else
3000 monitor_handle_command(cur_mon, (char *)buf);
3003 cur_mon = old_mon;
3006 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3008 monitor_suspend(mon);
3009 monitor_handle_command(mon, cmdline);
3010 monitor_resume(mon);
3013 int monitor_suspend(Monitor *mon)
3015 if (!mon->rs)
3016 return -ENOTTY;
3017 mon->suspend_cnt++;
3018 return 0;
3021 void monitor_resume(Monitor *mon)
3023 if (!mon->rs)
3024 return;
3025 if (--mon->suspend_cnt == 0)
3026 readline_show_prompt(mon->rs);
3029 static void monitor_event(void *opaque, int event)
3031 Monitor *mon = opaque;
3033 switch (event) {
3034 case CHR_EVENT_MUX_IN:
3035 readline_restart(mon->rs);
3036 monitor_resume(mon);
3037 monitor_flush(mon);
3038 break;
3040 case CHR_EVENT_MUX_OUT:
3041 if (mon->suspend_cnt == 0)
3042 monitor_printf(mon, "\n");
3043 monitor_flush(mon);
3044 monitor_suspend(mon);
3045 break;
3047 case CHR_EVENT_RESET:
3048 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3049 "information\n", QEMU_VERSION);
3050 if (mon->chr->focus == 0)
3051 readline_show_prompt(mon->rs);
3052 break;
3058 * Local variables:
3059 * c-indent-level: 4
3060 * c-basic-offset: 4
3061 * tab-width: 8
3062 * End:
3065 void monitor_init(CharDriverState *chr, int flags)
3067 static int is_first_init = 1;
3068 Monitor *mon;
3070 if (is_first_init) {
3071 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3072 is_first_init = 0;
3075 mon = qemu_mallocz(sizeof(*mon));
3077 mon->chr = chr;
3078 mon->flags = flags;
3079 if (mon->chr->focus != 0)
3080 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3081 if (flags & MONITOR_USE_READLINE) {
3082 mon->rs = readline_init(mon, monitor_find_completion);
3083 monitor_read_command(mon, 0);
3086 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3087 mon);
3089 LIST_INSERT_HEAD(&mon_list, mon, entry);
3090 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3091 cur_mon = mon;
3094 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3096 BlockDriverState *bs = opaque;
3097 int ret = 0;
3099 if (bdrv_set_key(bs, password) != 0) {
3100 monitor_printf(mon, "invalid password\n");
3101 ret = -EPERM;
3103 if (mon->password_completion_cb)
3104 mon->password_completion_cb(mon->password_opaque, ret);
3106 monitor_read_command(mon, 1);
3109 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3110 BlockDriverCompletionFunc *completion_cb,
3111 void *opaque)
3113 int err;
3115 if (!bdrv_key_required(bs)) {
3116 if (completion_cb)
3117 completion_cb(opaque, 0);
3118 return;
3121 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3122 bdrv_get_encrypted_filename(bs));
3124 mon->password_completion_cb = completion_cb;
3125 mon->password_opaque = opaque;
3127 err = monitor_read_password(mon, bdrv_password_cb, bs);
3129 if (err && completion_cb)
3130 completion_cb(opaque, err);