Fix error handling in msix vector add
[qemu-kvm/fedora.git] / monitor.c
blob86ff296da2d96d406aa7250dc8c0adf56722a610
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/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/pcmcia.h"
29 #include "hw/pc.h"
30 #include "hw/pci.h"
31 #include "hw/watchdog.h"
32 #include "gdbstub.h"
33 #include "net.h"
34 #include "qemu-char.h"
35 #include "sysemu.h"
36 #include "monitor.h"
37 #include "readline.h"
38 #include "console.h"
39 #include "block.h"
40 #include "audio/audio.h"
41 #include "disas.h"
42 #include "balloon.h"
43 #include "qemu-timer.h"
44 #include "migration.h"
45 #include "kvm.h"
46 #include "acl.h"
47 #include "exec-all.h"
49 #include "qemu-kvm.h"
51 //#define DEBUG
52 //#define DEBUG_COMPLETION
55 * Supported types:
57 * 'F' filename
58 * 'B' block device name
59 * 's' string (accept optional quote)
60 * 'i' 32 bit integer
61 * 'l' target long (32 or 64 bit)
62 * '/' optional gdb-like print format (like "/10x")
64 * '?' optional type (for 'F', 's' and 'i')
68 typedef struct mon_cmd_t {
69 const char *name;
70 const char *args_type;
71 void *handler;
72 const char *params;
73 const char *help;
74 } mon_cmd_t;
76 /* file descriptors passed via SCM_RIGHTS */
77 typedef struct mon_fd_t mon_fd_t;
78 struct mon_fd_t {
79 char *name;
80 int fd;
81 LIST_ENTRY(mon_fd_t) next;
84 struct Monitor {
85 CharDriverState *chr;
86 int flags;
87 int suspend_cnt;
88 uint8_t outbuf[1024];
89 int outbuf_index;
90 ReadLineState *rs;
91 CPUState *mon_cpu;
92 BlockDriverCompletionFunc *password_completion_cb;
93 void *password_opaque;
94 LIST_HEAD(,mon_fd_t) fds;
95 LIST_ENTRY(Monitor) entry;
98 static LIST_HEAD(mon_list, Monitor) mon_list;
100 static const mon_cmd_t mon_cmds[];
101 static const mon_cmd_t info_cmds[];
103 Monitor *cur_mon = NULL;
105 static void monitor_command_cb(Monitor *mon, const char *cmdline,
106 void *opaque);
108 static void monitor_read_command(Monitor *mon, int show_prompt)
110 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
111 if (show_prompt)
112 readline_show_prompt(mon->rs);
115 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
116 void *opaque)
118 if (mon->rs) {
119 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
120 /* prompt is printed on return from the command handler */
121 return 0;
122 } else {
123 monitor_printf(mon, "terminal does not support password prompting\n");
124 return -ENOTTY;
128 void monitor_flush(Monitor *mon)
130 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
131 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
132 mon->outbuf_index = 0;
136 /* flush at every end of line or if the buffer is full */
137 static void monitor_puts(Monitor *mon, const char *str)
139 char c;
141 if (!mon)
142 return;
144 for(;;) {
145 c = *str++;
146 if (c == '\0')
147 break;
148 if (c == '\n')
149 mon->outbuf[mon->outbuf_index++] = '\r';
150 mon->outbuf[mon->outbuf_index++] = c;
151 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
152 || c == '\n')
153 monitor_flush(mon);
157 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
159 char buf[4096];
160 vsnprintf(buf, sizeof(buf), fmt, ap);
161 monitor_puts(mon, buf);
164 void monitor_printf(Monitor *mon, const char *fmt, ...)
166 va_list ap;
167 va_start(ap, fmt);
168 monitor_vprintf(mon, fmt, ap);
169 va_end(ap);
172 void monitor_print_filename(Monitor *mon, const char *filename)
174 int i;
176 for (i = 0; filename[i]; i++) {
177 switch (filename[i]) {
178 case ' ':
179 case '"':
180 case '\\':
181 monitor_printf(mon, "\\%c", filename[i]);
182 break;
183 case '\t':
184 monitor_printf(mon, "\\t");
185 break;
186 case '\r':
187 monitor_printf(mon, "\\r");
188 break;
189 case '\n':
190 monitor_printf(mon, "\\n");
191 break;
192 default:
193 monitor_printf(mon, "%c", filename[i]);
194 break;
199 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
201 va_list ap;
202 va_start(ap, fmt);
203 monitor_vprintf((Monitor *)stream, fmt, ap);
204 va_end(ap);
205 return 0;
208 static int compare_cmd(const char *name, const char *list)
210 const char *p, *pstart;
211 int len;
212 len = strlen(name);
213 p = list;
214 for(;;) {
215 pstart = p;
216 p = strchr(p, '|');
217 if (!p)
218 p = pstart + strlen(pstart);
219 if ((p - pstart) == len && !memcmp(pstart, name, len))
220 return 1;
221 if (*p == '\0')
222 break;
223 p++;
225 return 0;
228 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
229 const char *prefix, const char *name)
231 const mon_cmd_t *cmd;
233 for(cmd = cmds; cmd->name != NULL; cmd++) {
234 if (!name || !strcmp(name, cmd->name))
235 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
236 cmd->params, cmd->help);
240 static void help_cmd(Monitor *mon, const char *name)
242 if (name && !strcmp(name, "info")) {
243 help_cmd_dump(mon, info_cmds, "info ", NULL);
244 } else {
245 help_cmd_dump(mon, mon_cmds, "", name);
246 if (name && !strcmp(name, "log")) {
247 const CPULogItem *item;
248 monitor_printf(mon, "Log items (comma separated):\n");
249 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
250 for(item = cpu_log_items; item->mask != 0; item++) {
251 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
257 static void do_commit(Monitor *mon, const char *device)
259 int i, all_devices;
261 all_devices = !strcmp(device, "all");
262 for (i = 0; i < nb_drives; i++) {
263 if (all_devices ||
264 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
265 bdrv_commit(drives_table[i].bdrv);
269 static void do_info(Monitor *mon, const char *item)
271 const mon_cmd_t *cmd;
272 void (*handler)(Monitor *);
274 if (!item)
275 goto help;
276 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
277 if (compare_cmd(item, cmd->name))
278 goto found;
280 help:
281 help_cmd(mon, "info");
282 return;
283 found:
284 handler = cmd->handler;
285 handler(mon);
288 static void do_info_version(Monitor *mon)
290 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
293 static void do_info_name(Monitor *mon)
295 if (qemu_name)
296 monitor_printf(mon, "%s\n", qemu_name);
299 #if defined(TARGET_I386)
300 static void do_info_hpet(Monitor *mon)
302 monitor_printf(mon, "HPET is %s by QEMU\n",
303 (no_hpet) ? "disabled" : "enabled");
305 #endif
307 static void do_info_uuid(Monitor *mon)
309 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
310 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
311 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
312 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
313 qemu_uuid[14], qemu_uuid[15]);
316 /* get the current CPU defined by the user */
317 static int mon_set_cpu(int cpu_index)
319 CPUState *env;
321 for(env = first_cpu; env != NULL; env = env->next_cpu) {
322 if (env->cpu_index == cpu_index) {
323 cur_mon->mon_cpu = env;
324 return 0;
327 return -1;
330 static CPUState *mon_get_cpu(void)
332 if (!cur_mon->mon_cpu) {
333 mon_set_cpu(0);
335 cpu_synchronize_state(cur_mon->mon_cpu, 0);
336 return cur_mon->mon_cpu;
339 static void do_info_registers(Monitor *mon)
341 CPUState *env;
342 env = mon_get_cpu();
343 if (!env)
344 return;
345 #ifdef TARGET_I386
346 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
347 X86_DUMP_FPU);
348 #else
349 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
351 #endif
354 static void do_info_cpus(Monitor *mon)
356 CPUState *env;
358 /* just to set the default cpu if not already done */
359 mon_get_cpu();
361 for(env = first_cpu; env != NULL; env = env->next_cpu) {
362 cpu_synchronize_state(env, 0);
363 monitor_printf(mon, "%c CPU #%d:",
364 (env == mon->mon_cpu) ? '*' : ' ',
365 env->cpu_index);
366 #if defined(TARGET_I386)
367 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
368 env->eip + env->segs[R_CS].base);
369 #elif defined(TARGET_PPC)
370 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
371 #elif defined(TARGET_SPARC)
372 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
373 env->pc, env->npc);
374 #elif defined(TARGET_MIPS)
375 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
376 #endif
377 if (env->halted)
378 monitor_printf(mon, " (halted)");
379 monitor_printf(mon," thread_id=%d", env->thread_id);
380 monitor_printf(mon, "\n");
384 static void do_cpu_set(Monitor *mon, int index)
386 if (mon_set_cpu(index) < 0)
387 monitor_printf(mon, "Invalid CPU index\n");
390 static void do_cpu_set_nr(Monitor *mon, int value, const char *status)
392 int state;
394 if (!strcmp(status, "online"))
395 state = 1;
396 else if (!strcmp(status, "offline"))
397 state = 0;
398 else {
399 monitor_printf(mon, "invalid status: %s\n", status);
400 return;
402 #if defined(TARGET_I386) || defined(TARGET_X86_64)
403 qemu_system_cpu_hot_add(value, state);
404 #endif
407 static void do_info_jit(Monitor *mon)
409 dump_exec_info((FILE *)mon, monitor_fprintf);
412 static void do_info_history(Monitor *mon)
414 int i;
415 const char *str;
417 if (!mon->rs)
418 return;
419 i = 0;
420 for(;;) {
421 str = readline_get_history(mon->rs, i);
422 if (!str)
423 break;
424 monitor_printf(mon, "%d: '%s'\n", i, str);
425 i++;
429 #if defined(TARGET_PPC)
430 /* XXX: not implemented in other targets */
431 static void do_info_cpu_stats(Monitor *mon)
433 CPUState *env;
435 env = mon_get_cpu();
436 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
438 #endif
440 static void do_quit(Monitor *mon)
442 exit(0);
445 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
447 if (bdrv_is_inserted(bs)) {
448 if (!force) {
449 if (!bdrv_is_removable(bs)) {
450 monitor_printf(mon, "device is not removable\n");
451 return -1;
453 if (bdrv_is_locked(bs)) {
454 monitor_printf(mon, "device is locked\n");
455 return -1;
458 bdrv_close(bs);
460 return 0;
463 static void do_eject(Monitor *mon, int force, const char *filename)
465 BlockDriverState *bs;
467 bs = bdrv_find(filename);
468 if (!bs) {
469 monitor_printf(mon, "device not found\n");
470 return;
472 eject_device(mon, bs, force);
475 static void do_change_block(Monitor *mon, const char *device,
476 const char *filename, const char *fmt)
478 BlockDriverState *bs;
479 BlockDriver *drv = NULL;
481 bs = bdrv_find(device);
482 if (!bs) {
483 monitor_printf(mon, "device not found\n");
484 return;
486 if (fmt) {
487 drv = bdrv_find_format(fmt);
488 if (!drv) {
489 monitor_printf(mon, "invalid format %s\n", fmt);
490 return;
493 if (eject_device(mon, bs, 0) < 0)
494 return;
495 bdrv_open2(bs, filename, 0, drv);
496 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
499 static void change_vnc_password_cb(Monitor *mon, const char *password,
500 void *opaque)
502 if (vnc_display_password(NULL, password) < 0)
503 monitor_printf(mon, "could not set VNC server password\n");
505 monitor_read_command(mon, 1);
508 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
510 if (strcmp(target, "passwd") == 0 ||
511 strcmp(target, "password") == 0) {
512 if (arg) {
513 char password[9];
514 strncpy(password, arg, sizeof(password));
515 password[sizeof(password) - 1] = '\0';
516 change_vnc_password_cb(mon, password, NULL);
517 } else {
518 monitor_read_password(mon, change_vnc_password_cb, NULL);
520 } else {
521 if (vnc_display_open(NULL, target) < 0)
522 monitor_printf(mon, "could not start VNC server on %s\n", target);
526 static void do_change(Monitor *mon, const char *device, const char *target,
527 const char *arg)
529 if (strcmp(device, "vnc") == 0) {
530 do_change_vnc(mon, target, arg);
531 } else {
532 do_change_block(mon, device, target, arg);
536 static void do_screen_dump(Monitor *mon, const char *filename)
538 vga_hw_screen_dump(filename);
541 static void do_logfile(Monitor *mon, const char *filename)
543 cpu_set_log_filename(filename);
546 static void do_log(Monitor *mon, const char *items)
548 int mask;
550 if (!strcmp(items, "none")) {
551 mask = 0;
552 } else {
553 mask = cpu_str_to_log_mask(items);
554 if (!mask) {
555 help_cmd(mon, "log");
556 return;
559 cpu_set_log(mask);
562 static void do_singlestep(Monitor *mon, const char *option)
564 if (!option || !strcmp(option, "on")) {
565 singlestep = 1;
566 } else if (!strcmp(option, "off")) {
567 singlestep = 0;
568 } else {
569 monitor_printf(mon, "unexpected option %s\n", option);
573 static void do_stop(Monitor *mon)
575 vm_stop(EXCP_INTERRUPT);
578 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
580 struct bdrv_iterate_context {
581 Monitor *mon;
582 int err;
585 static void do_cont(Monitor *mon)
587 struct bdrv_iterate_context context = { mon, 0 };
589 bdrv_iterate(encrypted_bdrv_it, &context);
590 /* only resume the vm if all keys are set and valid */
591 if (!context.err)
592 vm_start();
595 static void bdrv_key_cb(void *opaque, int err)
597 Monitor *mon = opaque;
599 /* another key was set successfully, retry to continue */
600 if (!err)
601 do_cont(mon);
604 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
606 struct bdrv_iterate_context *context = opaque;
608 if (!context->err && bdrv_key_required(bs)) {
609 context->err = -EBUSY;
610 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
611 context->mon);
615 static void do_gdbserver(Monitor *mon, const char *device)
617 if (!device)
618 device = "tcp::" DEFAULT_GDBSTUB_PORT;
619 if (gdbserver_start(device) < 0) {
620 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
621 device);
622 } else if (strcmp(device, "none") == 0) {
623 monitor_printf(mon, "Disabled gdbserver\n");
624 } else {
625 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
626 device);
630 static void do_watchdog_action(Monitor *mon, const char *action)
632 if (select_watchdog_action(action) == -1) {
633 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
637 static void monitor_printc(Monitor *mon, int c)
639 monitor_printf(mon, "'");
640 switch(c) {
641 case '\'':
642 monitor_printf(mon, "\\'");
643 break;
644 case '\\':
645 monitor_printf(mon, "\\\\");
646 break;
647 case '\n':
648 monitor_printf(mon, "\\n");
649 break;
650 case '\r':
651 monitor_printf(mon, "\\r");
652 break;
653 default:
654 if (c >= 32 && c <= 126) {
655 monitor_printf(mon, "%c", c);
656 } else {
657 monitor_printf(mon, "\\x%02x", c);
659 break;
661 monitor_printf(mon, "'");
664 static void memory_dump(Monitor *mon, int count, int format, int wsize,
665 target_phys_addr_t addr, int is_physical)
667 CPUState *env;
668 int nb_per_line, l, line_size, i, max_digits, len;
669 uint8_t buf[16];
670 uint64_t v;
672 if (format == 'i') {
673 int flags;
674 flags = 0;
675 env = mon_get_cpu();
676 if (!env && !is_physical)
677 return;
678 #ifdef TARGET_I386
679 if (wsize == 2) {
680 flags = 1;
681 } else if (wsize == 4) {
682 flags = 0;
683 } else {
684 /* as default we use the current CS size */
685 flags = 0;
686 if (env) {
687 #ifdef TARGET_X86_64
688 if ((env->efer & MSR_EFER_LMA) &&
689 (env->segs[R_CS].flags & DESC_L_MASK))
690 flags = 2;
691 else
692 #endif
693 if (!(env->segs[R_CS].flags & DESC_B_MASK))
694 flags = 1;
697 #endif
698 monitor_disas(mon, env, addr, count, is_physical, flags);
699 return;
702 len = wsize * count;
703 if (wsize == 1)
704 line_size = 8;
705 else
706 line_size = 16;
707 nb_per_line = line_size / wsize;
708 max_digits = 0;
710 switch(format) {
711 case 'o':
712 max_digits = (wsize * 8 + 2) / 3;
713 break;
714 default:
715 case 'x':
716 max_digits = (wsize * 8) / 4;
717 break;
718 case 'u':
719 case 'd':
720 max_digits = (wsize * 8 * 10 + 32) / 33;
721 break;
722 case 'c':
723 wsize = 1;
724 break;
727 while (len > 0) {
728 if (is_physical)
729 monitor_printf(mon, TARGET_FMT_plx ":", addr);
730 else
731 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
732 l = len;
733 if (l > line_size)
734 l = line_size;
735 if (is_physical) {
736 cpu_physical_memory_rw(addr, buf, l, 0);
737 } else {
738 env = mon_get_cpu();
739 if (!env)
740 break;
741 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
742 monitor_printf(mon, " Cannot access memory\n");
743 break;
746 i = 0;
747 while (i < l) {
748 switch(wsize) {
749 default:
750 case 1:
751 v = ldub_raw(buf + i);
752 break;
753 case 2:
754 v = lduw_raw(buf + i);
755 break;
756 case 4:
757 v = (uint32_t)ldl_raw(buf + i);
758 break;
759 case 8:
760 v = ldq_raw(buf + i);
761 break;
763 monitor_printf(mon, " ");
764 switch(format) {
765 case 'o':
766 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
767 break;
768 case 'x':
769 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
770 break;
771 case 'u':
772 monitor_printf(mon, "%*" PRIu64, max_digits, v);
773 break;
774 case 'd':
775 monitor_printf(mon, "%*" PRId64, max_digits, v);
776 break;
777 case 'c':
778 monitor_printc(mon, v);
779 break;
781 i += wsize;
783 monitor_printf(mon, "\n");
784 addr += l;
785 len -= l;
789 #if TARGET_LONG_BITS == 64
790 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
791 #else
792 #define GET_TLONG(h, l) (l)
793 #endif
795 static void do_memory_dump(Monitor *mon, int count, int format, int size,
796 uint32_t addrh, uint32_t addrl)
798 target_long addr = GET_TLONG(addrh, addrl);
799 memory_dump(mon, count, format, size, addr, 0);
802 #if TARGET_PHYS_ADDR_BITS > 32
803 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
804 #else
805 #define GET_TPHYSADDR(h, l) (l)
806 #endif
808 static void do_physical_memory_dump(Monitor *mon, int count, int format,
809 int size, uint32_t addrh, uint32_t addrl)
812 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
813 memory_dump(mon, count, format, size, addr, 1);
816 static void do_print(Monitor *mon, int count, int format, int size,
817 unsigned int valh, unsigned int vall)
819 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
820 #if TARGET_PHYS_ADDR_BITS == 32
821 switch(format) {
822 case 'o':
823 monitor_printf(mon, "%#o", val);
824 break;
825 case 'x':
826 monitor_printf(mon, "%#x", val);
827 break;
828 case 'u':
829 monitor_printf(mon, "%u", val);
830 break;
831 default:
832 case 'd':
833 monitor_printf(mon, "%d", val);
834 break;
835 case 'c':
836 monitor_printc(mon, val);
837 break;
839 #else
840 switch(format) {
841 case 'o':
842 monitor_printf(mon, "%#" PRIo64, val);
843 break;
844 case 'x':
845 monitor_printf(mon, "%#" PRIx64, val);
846 break;
847 case 'u':
848 monitor_printf(mon, "%" PRIu64, val);
849 break;
850 default:
851 case 'd':
852 monitor_printf(mon, "%" PRId64, val);
853 break;
854 case 'c':
855 monitor_printc(mon, val);
856 break;
858 #endif
859 monitor_printf(mon, "\n");
862 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
863 uint32_t size, const char *filename)
865 FILE *f;
866 target_long addr = GET_TLONG(valh, vall);
867 uint32_t l;
868 CPUState *env;
869 uint8_t buf[1024];
871 env = mon_get_cpu();
872 if (!env)
873 return;
875 f = fopen(filename, "wb");
876 if (!f) {
877 monitor_printf(mon, "could not open '%s'\n", filename);
878 return;
880 while (size != 0) {
881 l = sizeof(buf);
882 if (l > size)
883 l = size;
884 cpu_memory_rw_debug(env, addr, buf, l, 0);
885 fwrite(buf, 1, l, f);
886 addr += l;
887 size -= l;
889 fclose(f);
892 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
893 unsigned int vall, uint32_t size,
894 const char *filename)
896 FILE *f;
897 uint32_t l;
898 uint8_t buf[1024];
899 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
901 f = fopen(filename, "wb");
902 if (!f) {
903 monitor_printf(mon, "could not open '%s'\n", filename);
904 return;
906 while (size != 0) {
907 l = sizeof(buf);
908 if (l > size)
909 l = size;
910 cpu_physical_memory_rw(addr, buf, l, 0);
911 fwrite(buf, 1, l, f);
912 fflush(f);
913 addr += l;
914 size -= l;
916 fclose(f);
919 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
921 uint32_t addr;
922 uint8_t buf[1];
923 uint16_t sum;
925 sum = 0;
926 for(addr = start; addr < (start + size); addr++) {
927 cpu_physical_memory_rw(addr, buf, 1, 0);
928 /* BSD sum algorithm ('sum' Unix command) */
929 sum = (sum >> 1) | (sum << 15);
930 sum += buf[0];
932 monitor_printf(mon, "%05d\n", sum);
935 typedef struct {
936 int keycode;
937 const char *name;
938 } KeyDef;
940 static const KeyDef key_defs[] = {
941 { 0x2a, "shift" },
942 { 0x36, "shift_r" },
944 { 0x38, "alt" },
945 { 0xb8, "alt_r" },
946 { 0x64, "altgr" },
947 { 0xe4, "altgr_r" },
948 { 0x1d, "ctrl" },
949 { 0x9d, "ctrl_r" },
951 { 0xdd, "menu" },
953 { 0x01, "esc" },
955 { 0x02, "1" },
956 { 0x03, "2" },
957 { 0x04, "3" },
958 { 0x05, "4" },
959 { 0x06, "5" },
960 { 0x07, "6" },
961 { 0x08, "7" },
962 { 0x09, "8" },
963 { 0x0a, "9" },
964 { 0x0b, "0" },
965 { 0x0c, "minus" },
966 { 0x0d, "equal" },
967 { 0x0e, "backspace" },
969 { 0x0f, "tab" },
970 { 0x10, "q" },
971 { 0x11, "w" },
972 { 0x12, "e" },
973 { 0x13, "r" },
974 { 0x14, "t" },
975 { 0x15, "y" },
976 { 0x16, "u" },
977 { 0x17, "i" },
978 { 0x18, "o" },
979 { 0x19, "p" },
981 { 0x1c, "ret" },
983 { 0x1e, "a" },
984 { 0x1f, "s" },
985 { 0x20, "d" },
986 { 0x21, "f" },
987 { 0x22, "g" },
988 { 0x23, "h" },
989 { 0x24, "j" },
990 { 0x25, "k" },
991 { 0x26, "l" },
993 { 0x2c, "z" },
994 { 0x2d, "x" },
995 { 0x2e, "c" },
996 { 0x2f, "v" },
997 { 0x30, "b" },
998 { 0x31, "n" },
999 { 0x32, "m" },
1000 { 0x33, "comma" },
1001 { 0x34, "dot" },
1002 { 0x35, "slash" },
1004 { 0x37, "asterisk" },
1006 { 0x39, "spc" },
1007 { 0x3a, "caps_lock" },
1008 { 0x3b, "f1" },
1009 { 0x3c, "f2" },
1010 { 0x3d, "f3" },
1011 { 0x3e, "f4" },
1012 { 0x3f, "f5" },
1013 { 0x40, "f6" },
1014 { 0x41, "f7" },
1015 { 0x42, "f8" },
1016 { 0x43, "f9" },
1017 { 0x44, "f10" },
1018 { 0x45, "num_lock" },
1019 { 0x46, "scroll_lock" },
1021 { 0xb5, "kp_divide" },
1022 { 0x37, "kp_multiply" },
1023 { 0x4a, "kp_subtract" },
1024 { 0x4e, "kp_add" },
1025 { 0x9c, "kp_enter" },
1026 { 0x53, "kp_decimal" },
1027 { 0x54, "sysrq" },
1029 { 0x52, "kp_0" },
1030 { 0x4f, "kp_1" },
1031 { 0x50, "kp_2" },
1032 { 0x51, "kp_3" },
1033 { 0x4b, "kp_4" },
1034 { 0x4c, "kp_5" },
1035 { 0x4d, "kp_6" },
1036 { 0x47, "kp_7" },
1037 { 0x48, "kp_8" },
1038 { 0x49, "kp_9" },
1040 { 0x56, "<" },
1042 { 0x57, "f11" },
1043 { 0x58, "f12" },
1045 { 0xb7, "print" },
1047 { 0xc7, "home" },
1048 { 0xc9, "pgup" },
1049 { 0xd1, "pgdn" },
1050 { 0xcf, "end" },
1052 { 0xcb, "left" },
1053 { 0xc8, "up" },
1054 { 0xd0, "down" },
1055 { 0xcd, "right" },
1057 { 0xd2, "insert" },
1058 { 0xd3, "delete" },
1059 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1060 { 0xf0, "stop" },
1061 { 0xf1, "again" },
1062 { 0xf2, "props" },
1063 { 0xf3, "undo" },
1064 { 0xf4, "front" },
1065 { 0xf5, "copy" },
1066 { 0xf6, "open" },
1067 { 0xf7, "paste" },
1068 { 0xf8, "find" },
1069 { 0xf9, "cut" },
1070 { 0xfa, "lf" },
1071 { 0xfb, "help" },
1072 { 0xfc, "meta_l" },
1073 { 0xfd, "meta_r" },
1074 { 0xfe, "compose" },
1075 #endif
1076 { 0, NULL },
1079 static int get_keycode(const char *key)
1081 const KeyDef *p;
1082 char *endp;
1083 int ret;
1085 for(p = key_defs; p->name != NULL; p++) {
1086 if (!strcmp(key, p->name))
1087 return p->keycode;
1089 if (strstart(key, "0x", NULL)) {
1090 ret = strtoul(key, &endp, 0);
1091 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1092 return ret;
1094 return -1;
1097 #define MAX_KEYCODES 16
1098 static uint8_t keycodes[MAX_KEYCODES];
1099 static int nb_pending_keycodes;
1100 static QEMUTimer *key_timer;
1102 static void release_keys(void *opaque)
1104 int keycode;
1106 while (nb_pending_keycodes > 0) {
1107 nb_pending_keycodes--;
1108 keycode = keycodes[nb_pending_keycodes];
1109 if (keycode & 0x80)
1110 kbd_put_keycode(0xe0);
1111 kbd_put_keycode(keycode | 0x80);
1115 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1116 int hold_time)
1118 char keyname_buf[16];
1119 char *separator;
1120 int keyname_len, keycode, i;
1122 if (nb_pending_keycodes > 0) {
1123 qemu_del_timer(key_timer);
1124 release_keys(NULL);
1126 if (!has_hold_time)
1127 hold_time = 100;
1128 i = 0;
1129 while (1) {
1130 separator = strchr(string, '-');
1131 keyname_len = separator ? separator - string : strlen(string);
1132 if (keyname_len > 0) {
1133 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1134 if (keyname_len > sizeof(keyname_buf) - 1) {
1135 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1136 return;
1138 if (i == MAX_KEYCODES) {
1139 monitor_printf(mon, "too many keys\n");
1140 return;
1142 keyname_buf[keyname_len] = 0;
1143 keycode = get_keycode(keyname_buf);
1144 if (keycode < 0) {
1145 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1146 return;
1148 keycodes[i++] = keycode;
1150 if (!separator)
1151 break;
1152 string = separator + 1;
1154 nb_pending_keycodes = i;
1155 /* key down events */
1156 for (i = 0; i < nb_pending_keycodes; i++) {
1157 keycode = keycodes[i];
1158 if (keycode & 0x80)
1159 kbd_put_keycode(0xe0);
1160 kbd_put_keycode(keycode & 0x7f);
1162 /* delayed key up events */
1163 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1164 muldiv64(ticks_per_sec, hold_time, 1000));
1167 static int mouse_button_state;
1169 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1170 const char *dz_str)
1172 int dx, dy, dz;
1173 dx = strtol(dx_str, NULL, 0);
1174 dy = strtol(dy_str, NULL, 0);
1175 dz = 0;
1176 if (dz_str)
1177 dz = strtol(dz_str, NULL, 0);
1178 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1181 static void do_mouse_button(Monitor *mon, int button_state)
1183 mouse_button_state = button_state;
1184 kbd_mouse_event(0, 0, 0, mouse_button_state);
1187 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1188 int addr, int has_index, int index)
1190 uint32_t val;
1191 int suffix;
1193 if (has_index) {
1194 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1195 addr++;
1197 addr &= 0xffff;
1199 switch(size) {
1200 default:
1201 case 1:
1202 val = cpu_inb(NULL, addr);
1203 suffix = 'b';
1204 break;
1205 case 2:
1206 val = cpu_inw(NULL, addr);
1207 suffix = 'w';
1208 break;
1209 case 4:
1210 val = cpu_inl(NULL, addr);
1211 suffix = 'l';
1212 break;
1214 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1215 suffix, addr, size * 2, val);
1218 static void do_ioport_write(Monitor *mon, int count, int format, int size,
1219 int addr, int val)
1221 addr &= IOPORTS_MASK;
1223 switch (size) {
1224 default:
1225 case 1:
1226 cpu_outb(NULL, addr, val);
1227 break;
1228 case 2:
1229 cpu_outw(NULL, addr, val);
1230 break;
1231 case 4:
1232 cpu_outl(NULL, addr, val);
1233 break;
1237 static void do_boot_set(Monitor *mon, const char *bootdevice)
1239 int res;
1241 res = qemu_boot_set(bootdevice);
1242 if (res == 0) {
1243 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1244 } else if (res > 0) {
1245 monitor_printf(mon, "setting boot device list failed\n");
1246 } else {
1247 monitor_printf(mon, "no function defined to set boot device list for "
1248 "this architecture\n");
1252 static void do_system_reset(Monitor *mon)
1254 qemu_system_reset_request();
1257 static void do_system_powerdown(Monitor *mon)
1259 qemu_system_powerdown_request();
1262 #if defined(TARGET_I386)
1263 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1265 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1266 addr,
1267 pte & mask,
1268 pte & PG_GLOBAL_MASK ? 'G' : '-',
1269 pte & PG_PSE_MASK ? 'P' : '-',
1270 pte & PG_DIRTY_MASK ? 'D' : '-',
1271 pte & PG_ACCESSED_MASK ? 'A' : '-',
1272 pte & PG_PCD_MASK ? 'C' : '-',
1273 pte & PG_PWT_MASK ? 'T' : '-',
1274 pte & PG_USER_MASK ? 'U' : '-',
1275 pte & PG_RW_MASK ? 'W' : '-');
1278 static void tlb_info(Monitor *mon)
1280 CPUState *env;
1281 int l1, l2;
1282 uint32_t pgd, pde, pte;
1284 env = mon_get_cpu();
1285 if (!env)
1286 return;
1288 if (!(env->cr[0] & CR0_PG_MASK)) {
1289 monitor_printf(mon, "PG disabled\n");
1290 return;
1292 pgd = env->cr[3] & ~0xfff;
1293 for(l1 = 0; l1 < 1024; l1++) {
1294 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1295 pde = le32_to_cpu(pde);
1296 if (pde & PG_PRESENT_MASK) {
1297 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1298 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1299 } else {
1300 for(l2 = 0; l2 < 1024; l2++) {
1301 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1302 (uint8_t *)&pte, 4);
1303 pte = le32_to_cpu(pte);
1304 if (pte & PG_PRESENT_MASK) {
1305 print_pte(mon, (l1 << 22) + (l2 << 12),
1306 pte & ~PG_PSE_MASK,
1307 ~0xfff);
1315 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1316 uint32_t end, int prot)
1318 int prot1;
1319 prot1 = *plast_prot;
1320 if (prot != prot1) {
1321 if (*pstart != -1) {
1322 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1323 *pstart, end, end - *pstart,
1324 prot1 & PG_USER_MASK ? 'u' : '-',
1325 'r',
1326 prot1 & PG_RW_MASK ? 'w' : '-');
1328 if (prot != 0)
1329 *pstart = end;
1330 else
1331 *pstart = -1;
1332 *plast_prot = prot;
1336 static void mem_info(Monitor *mon)
1338 CPUState *env;
1339 int l1, l2, prot, last_prot;
1340 uint32_t pgd, pde, pte, start, end;
1342 env = mon_get_cpu();
1343 if (!env)
1344 return;
1346 if (!(env->cr[0] & CR0_PG_MASK)) {
1347 monitor_printf(mon, "PG disabled\n");
1348 return;
1350 pgd = env->cr[3] & ~0xfff;
1351 last_prot = 0;
1352 start = -1;
1353 for(l1 = 0; l1 < 1024; l1++) {
1354 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1355 pde = le32_to_cpu(pde);
1356 end = l1 << 22;
1357 if (pde & PG_PRESENT_MASK) {
1358 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1359 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1360 mem_print(mon, &start, &last_prot, end, prot);
1361 } else {
1362 for(l2 = 0; l2 < 1024; l2++) {
1363 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1364 (uint8_t *)&pte, 4);
1365 pte = le32_to_cpu(pte);
1366 end = (l1 << 22) + (l2 << 12);
1367 if (pte & PG_PRESENT_MASK) {
1368 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1369 } else {
1370 prot = 0;
1372 mem_print(mon, &start, &last_prot, end, prot);
1375 } else {
1376 prot = 0;
1377 mem_print(mon, &start, &last_prot, end, prot);
1381 #endif
1383 #if defined(TARGET_SH4)
1385 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1387 monitor_printf(mon, " tlb%i:\t"
1388 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1389 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1390 "dirty=%hhu writethrough=%hhu\n",
1391 idx,
1392 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1393 tlb->v, tlb->sh, tlb->c, tlb->pr,
1394 tlb->d, tlb->wt);
1397 static void tlb_info(Monitor *mon)
1399 CPUState *env = mon_get_cpu();
1400 int i;
1402 monitor_printf (mon, "ITLB:\n");
1403 for (i = 0 ; i < ITLB_SIZE ; i++)
1404 print_tlb (mon, i, &env->itlb[i]);
1405 monitor_printf (mon, "UTLB:\n");
1406 for (i = 0 ; i < UTLB_SIZE ; i++)
1407 print_tlb (mon, i, &env->utlb[i]);
1410 #endif
1412 static void do_info_kqemu(Monitor *mon)
1414 #ifdef CONFIG_KQEMU
1415 CPUState *env;
1416 int val;
1417 val = 0;
1418 env = mon_get_cpu();
1419 if (!env) {
1420 monitor_printf(mon, "No cpu initialized yet");
1421 return;
1423 val = env->kqemu_enabled;
1424 monitor_printf(mon, "kqemu support: ");
1425 switch(val) {
1426 default:
1427 case 0:
1428 monitor_printf(mon, "disabled\n");
1429 break;
1430 case 1:
1431 monitor_printf(mon, "enabled for user code\n");
1432 break;
1433 case 2:
1434 monitor_printf(mon, "enabled for user and kernel code\n");
1435 break;
1437 #else
1438 monitor_printf(mon, "kqemu support: not compiled\n");
1439 #endif
1442 static void do_info_kvm(Monitor *mon)
1444 #if defined(USE_KVM) || defined(CONFIG_KVM)
1445 monitor_printf(mon, "kvm support: ");
1446 if (kvm_enabled())
1447 monitor_printf(mon, "enabled\n");
1448 else
1449 monitor_printf(mon, "disabled\n");
1450 #else
1451 monitor_printf(mon, "kvm support: not compiled\n");
1452 #endif
1455 static void do_info_numa(Monitor *mon)
1457 int i;
1458 CPUState *env;
1460 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1461 for (i = 0; i < nb_numa_nodes; i++) {
1462 monitor_printf(mon, "node %d cpus:", i);
1463 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1464 if (env->numa_node == i) {
1465 monitor_printf(mon, " %d", env->cpu_index);
1468 monitor_printf(mon, "\n");
1469 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1470 node_mem[i] >> 20);
1474 #ifdef CONFIG_PROFILER
1476 int64_t kqemu_time;
1477 int64_t qemu_time;
1478 int64_t kqemu_exec_count;
1479 int64_t dev_time;
1480 int64_t kqemu_ret_int_count;
1481 int64_t kqemu_ret_excp_count;
1482 int64_t kqemu_ret_intr_count;
1484 static void do_info_profile(Monitor *mon)
1486 int64_t total;
1487 total = qemu_time;
1488 if (total == 0)
1489 total = 1;
1490 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1491 dev_time, dev_time / (double)ticks_per_sec);
1492 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1493 qemu_time, qemu_time / (double)ticks_per_sec);
1494 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1495 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1496 PRId64 "\n",
1497 kqemu_time, kqemu_time / (double)ticks_per_sec,
1498 kqemu_time / (double)total * 100.0,
1499 kqemu_exec_count,
1500 kqemu_ret_int_count,
1501 kqemu_ret_excp_count,
1502 kqemu_ret_intr_count);
1503 qemu_time = 0;
1504 kqemu_time = 0;
1505 kqemu_exec_count = 0;
1506 dev_time = 0;
1507 kqemu_ret_int_count = 0;
1508 kqemu_ret_excp_count = 0;
1509 kqemu_ret_intr_count = 0;
1510 #ifdef CONFIG_KQEMU
1511 kqemu_record_dump();
1512 #endif
1514 #else
1515 static void do_info_profile(Monitor *mon)
1517 monitor_printf(mon, "Internal profiler not compiled\n");
1519 #endif
1521 /* Capture support */
1522 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1524 static void do_info_capture(Monitor *mon)
1526 int i;
1527 CaptureState *s;
1529 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1530 monitor_printf(mon, "[%d]: ", i);
1531 s->ops.info (s->opaque);
1535 #ifdef HAS_AUDIO
1536 static void do_stop_capture(Monitor *mon, int n)
1538 int i;
1539 CaptureState *s;
1541 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1542 if (i == n) {
1543 s->ops.destroy (s->opaque);
1544 LIST_REMOVE (s, entries);
1545 qemu_free (s);
1546 return;
1551 static void do_wav_capture(Monitor *mon, const char *path,
1552 int has_freq, int freq,
1553 int has_bits, int bits,
1554 int has_channels, int nchannels)
1556 CaptureState *s;
1558 s = qemu_mallocz (sizeof (*s));
1560 freq = has_freq ? freq : 44100;
1561 bits = has_bits ? bits : 16;
1562 nchannels = has_channels ? nchannels : 2;
1564 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1565 monitor_printf(mon, "Faied to add wave capture\n");
1566 qemu_free (s);
1568 LIST_INSERT_HEAD (&capture_head, s, entries);
1570 #endif
1572 #if defined(TARGET_I386)
1573 static void do_inject_nmi(Monitor *mon, int cpu_index)
1575 CPUState *env;
1577 for (env = first_cpu; env != NULL; env = env->next_cpu)
1578 if (env->cpu_index == cpu_index) {
1579 if (kvm_enabled())
1580 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1581 else
1582 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1583 break;
1586 #endif
1588 static void do_info_status(Monitor *mon)
1590 if (vm_running) {
1591 if (singlestep) {
1592 monitor_printf(mon, "VM status: running (single step mode)\n");
1593 } else {
1594 monitor_printf(mon, "VM status: running\n");
1596 } else
1597 monitor_printf(mon, "VM status: paused\n");
1601 static void do_balloon(Monitor *mon, int value)
1603 ram_addr_t target = value;
1604 qemu_balloon(target << 20);
1607 static void do_info_balloon(Monitor *mon)
1609 ram_addr_t actual;
1611 actual = qemu_balloon_status();
1612 if (kvm_enabled() && !kvm_has_sync_mmu())
1613 monitor_printf(mon, "Using KVM without synchronous MMU, "
1614 "ballooning disabled\n");
1615 else if (actual == 0)
1616 monitor_printf(mon, "Ballooning not activated in VM\n");
1617 else
1618 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1621 static qemu_acl *find_acl(Monitor *mon, const char *name)
1623 qemu_acl *acl = qemu_acl_find(name);
1625 if (!acl) {
1626 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1628 return acl;
1631 static void do_acl_show(Monitor *mon, const char *aclname)
1633 qemu_acl *acl = find_acl(mon, aclname);
1634 qemu_acl_entry *entry;
1635 int i = 0;
1637 if (acl) {
1638 monitor_printf(mon, "policy: %s\n",
1639 acl->defaultDeny ? "deny" : "allow");
1640 TAILQ_FOREACH(entry, &acl->entries, next) {
1641 i++;
1642 monitor_printf(mon, "%d: %s %s\n", i,
1643 entry->deny ? "deny" : "allow", entry->match);
1648 static void do_acl_reset(Monitor *mon, const char *aclname)
1650 qemu_acl *acl = find_acl(mon, aclname);
1652 if (acl) {
1653 qemu_acl_reset(acl);
1654 monitor_printf(mon, "acl: removed all rules\n");
1658 static void do_acl_policy(Monitor *mon, const char *aclname,
1659 const char *policy)
1661 qemu_acl *acl = find_acl(mon, aclname);
1663 if (acl) {
1664 if (strcmp(policy, "allow") == 0) {
1665 acl->defaultDeny = 0;
1666 monitor_printf(mon, "acl: policy set to 'allow'\n");
1667 } else if (strcmp(policy, "deny") == 0) {
1668 acl->defaultDeny = 1;
1669 monitor_printf(mon, "acl: policy set to 'deny'\n");
1670 } else {
1671 monitor_printf(mon, "acl: unknown policy '%s', "
1672 "expected 'deny' or 'allow'\n", policy);
1677 static void do_acl_add(Monitor *mon, const char *aclname,
1678 const char *match, const char *policy,
1679 int has_index, int index)
1681 qemu_acl *acl = find_acl(mon, aclname);
1682 int deny, ret;
1684 if (acl) {
1685 if (strcmp(policy, "allow") == 0) {
1686 deny = 0;
1687 } else if (strcmp(policy, "deny") == 0) {
1688 deny = 1;
1689 } else {
1690 monitor_printf(mon, "acl: unknown policy '%s', "
1691 "expected 'deny' or 'allow'\n", policy);
1692 return;
1694 if (has_index)
1695 ret = qemu_acl_insert(acl, deny, match, index);
1696 else
1697 ret = qemu_acl_append(acl, deny, match);
1698 if (ret < 0)
1699 monitor_printf(mon, "acl: unable to add acl entry\n");
1700 else
1701 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1705 static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1707 qemu_acl *acl = find_acl(mon, aclname);
1708 int ret;
1710 if (acl) {
1711 ret = qemu_acl_remove(acl, match);
1712 if (ret < 0)
1713 monitor_printf(mon, "acl: no matching acl entry\n");
1714 else
1715 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1719 #if defined(TARGET_I386)
1720 static void do_inject_mce(Monitor *mon,
1721 int cpu_index, int bank,
1722 unsigned status_hi, unsigned status_lo,
1723 unsigned mcg_status_hi, unsigned mcg_status_lo,
1724 unsigned addr_hi, unsigned addr_lo,
1725 unsigned misc_hi, unsigned misc_lo)
1727 CPUState *cenv;
1728 uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1729 uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1730 uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1731 uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1733 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1734 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1735 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1736 break;
1739 #endif
1741 static void do_getfd(Monitor *mon, const char *fdname)
1743 mon_fd_t *monfd;
1744 int fd;
1746 fd = qemu_chr_get_msgfd(mon->chr);
1747 if (fd == -1) {
1748 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1749 return;
1752 if (qemu_isdigit(fdname[0])) {
1753 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1754 return;
1757 fd = dup(fd);
1758 if (fd == -1) {
1759 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1760 strerror(errno));
1761 return;
1764 LIST_FOREACH(monfd, &mon->fds, next) {
1765 if (strcmp(monfd->name, fdname) != 0) {
1766 continue;
1769 close(monfd->fd);
1770 monfd->fd = fd;
1771 return;
1774 monfd = qemu_mallocz(sizeof(mon_fd_t));
1775 monfd->name = qemu_strdup(fdname);
1776 monfd->fd = fd;
1778 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1781 static void do_closefd(Monitor *mon, const char *fdname)
1783 mon_fd_t *monfd;
1785 LIST_FOREACH(monfd, &mon->fds, next) {
1786 if (strcmp(monfd->name, fdname) != 0) {
1787 continue;
1790 LIST_REMOVE(monfd, next);
1791 close(monfd->fd);
1792 qemu_free(monfd->name);
1793 qemu_free(monfd);
1794 return;
1797 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1798 fdname);
1801 int monitor_get_fd(Monitor *mon, const char *fdname)
1803 mon_fd_t *monfd;
1805 LIST_FOREACH(monfd, &mon->fds, next) {
1806 int fd;
1808 if (strcmp(monfd->name, fdname) != 0) {
1809 continue;
1812 fd = monfd->fd;
1814 /* caller takes ownership of fd */
1815 LIST_REMOVE(monfd, next);
1816 qemu_free(monfd->name);
1817 qemu_free(monfd);
1819 return fd;
1822 return -1;
1825 static const mon_cmd_t mon_cmds[] = {
1826 #include "qemu-monitor.h"
1827 { NULL, NULL, },
1830 /* Please update qemu-monitor.hx when adding or changing commands */
1831 static const mon_cmd_t info_cmds[] = {
1832 { "version", "", do_info_version,
1833 "", "show the version of QEMU" },
1834 { "network", "", do_info_network,
1835 "", "show the network state" },
1836 { "chardev", "", qemu_chr_info,
1837 "", "show the character devices" },
1838 { "block", "", bdrv_info,
1839 "", "show the block devices" },
1840 { "blockstats", "", bdrv_info_stats,
1841 "", "show block device statistics" },
1842 { "registers", "", do_info_registers,
1843 "", "show the cpu registers" },
1844 { "cpus", "", do_info_cpus,
1845 "", "show infos for each CPU" },
1846 { "history", "", do_info_history,
1847 "", "show the command line history", },
1848 { "irq", "", irq_info,
1849 "", "show the interrupts statistics (if available)", },
1850 { "pic", "", pic_info,
1851 "", "show i8259 (PIC) state", },
1852 { "pci", "", pci_info,
1853 "", "show PCI info", },
1854 #if defined(TARGET_I386) || defined(TARGET_SH4)
1855 { "tlb", "", tlb_info,
1856 "", "show virtual to physical memory mappings", },
1857 #endif
1858 #if defined(TARGET_I386)
1859 { "mem", "", mem_info,
1860 "", "show the active virtual memory mappings", },
1861 { "hpet", "", do_info_hpet,
1862 "", "show state of HPET", },
1863 #endif
1864 { "jit", "", do_info_jit,
1865 "", "show dynamic compiler info", },
1866 { "kqemu", "", do_info_kqemu,
1867 "", "show KQEMU information", },
1868 { "kvm", "", do_info_kvm,
1869 "", "show KVM information", },
1870 { "numa", "", do_info_numa,
1871 "", "show NUMA information", },
1872 { "usb", "", usb_info,
1873 "", "show guest USB devices", },
1874 { "usbhost", "", usb_host_info,
1875 "", "show host USB devices", },
1876 { "profile", "", do_info_profile,
1877 "", "show profiling information", },
1878 { "capture", "", do_info_capture,
1879 "", "show capture information" },
1880 { "snapshots", "", do_info_snapshots,
1881 "", "show the currently saved VM snapshots" },
1882 { "status", "", do_info_status,
1883 "", "show the current VM status (running|paused)" },
1884 { "pcmcia", "", pcmcia_info,
1885 "", "show guest PCMCIA status" },
1886 { "mice", "", do_info_mice,
1887 "", "show which guest mouse is receiving events" },
1888 { "vnc", "", do_info_vnc,
1889 "", "show the vnc server status"},
1890 { "name", "", do_info_name,
1891 "", "show the current VM name" },
1892 { "uuid", "", do_info_uuid,
1893 "", "show the current VM UUID" },
1894 #if defined(TARGET_PPC)
1895 { "cpustats", "", do_info_cpu_stats,
1896 "", "show CPU statistics", },
1897 #endif
1898 #if defined(CONFIG_SLIRP)
1899 { "usernet", "", do_info_usernet,
1900 "", "show user network stack connection states", },
1901 #endif
1902 { "migrate", "", do_info_migrate, "", "show migration status" },
1903 { "balloon", "", do_info_balloon,
1904 "", "show balloon information" },
1905 { "qtree", "", do_info_qtree,
1906 "", "show device tree" },
1907 { NULL, NULL, },
1910 /*******************************************************************/
1912 static const char *pch;
1913 static jmp_buf expr_env;
1915 #define MD_TLONG 0
1916 #define MD_I32 1
1918 typedef struct MonitorDef {
1919 const char *name;
1920 int offset;
1921 target_long (*get_value)(const struct MonitorDef *md, int val);
1922 int type;
1923 } MonitorDef;
1925 #if defined(TARGET_I386)
1926 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1928 CPUState *env = mon_get_cpu();
1929 if (!env)
1930 return 0;
1931 return env->eip + env->segs[R_CS].base;
1933 #endif
1935 #if defined(TARGET_PPC)
1936 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1938 CPUState *env = mon_get_cpu();
1939 unsigned int u;
1940 int i;
1942 if (!env)
1943 return 0;
1945 u = 0;
1946 for (i = 0; i < 8; i++)
1947 u |= env->crf[i] << (32 - (4 * i));
1949 return u;
1952 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1954 CPUState *env = mon_get_cpu();
1955 if (!env)
1956 return 0;
1957 return env->msr;
1960 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1962 CPUState *env = mon_get_cpu();
1963 if (!env)
1964 return 0;
1965 return env->xer;
1968 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1970 CPUState *env = mon_get_cpu();
1971 if (!env)
1972 return 0;
1973 return cpu_ppc_load_decr(env);
1976 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1978 CPUState *env = mon_get_cpu();
1979 if (!env)
1980 return 0;
1981 return cpu_ppc_load_tbu(env);
1984 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1986 CPUState *env = mon_get_cpu();
1987 if (!env)
1988 return 0;
1989 return cpu_ppc_load_tbl(env);
1991 #endif
1993 #if defined(TARGET_SPARC)
1994 #ifndef TARGET_SPARC64
1995 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1997 CPUState *env = mon_get_cpu();
1998 if (!env)
1999 return 0;
2000 return GET_PSR(env);
2002 #endif
2004 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2006 CPUState *env = mon_get_cpu();
2007 if (!env)
2008 return 0;
2009 return env->regwptr[val];
2011 #endif
2013 static const MonitorDef monitor_defs[] = {
2014 #ifdef TARGET_I386
2016 #define SEG(name, seg) \
2017 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2018 { name ".base", offsetof(CPUState, segs[seg].base) },\
2019 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2021 { "eax", offsetof(CPUState, regs[0]) },
2022 { "ecx", offsetof(CPUState, regs[1]) },
2023 { "edx", offsetof(CPUState, regs[2]) },
2024 { "ebx", offsetof(CPUState, regs[3]) },
2025 { "esp|sp", offsetof(CPUState, regs[4]) },
2026 { "ebp|fp", offsetof(CPUState, regs[5]) },
2027 { "esi", offsetof(CPUState, regs[6]) },
2028 { "edi", offsetof(CPUState, regs[7]) },
2029 #ifdef TARGET_X86_64
2030 { "r8", offsetof(CPUState, regs[8]) },
2031 { "r9", offsetof(CPUState, regs[9]) },
2032 { "r10", offsetof(CPUState, regs[10]) },
2033 { "r11", offsetof(CPUState, regs[11]) },
2034 { "r12", offsetof(CPUState, regs[12]) },
2035 { "r13", offsetof(CPUState, regs[13]) },
2036 { "r14", offsetof(CPUState, regs[14]) },
2037 { "r15", offsetof(CPUState, regs[15]) },
2038 #endif
2039 { "eflags", offsetof(CPUState, eflags) },
2040 { "eip", offsetof(CPUState, eip) },
2041 SEG("cs", R_CS)
2042 SEG("ds", R_DS)
2043 SEG("es", R_ES)
2044 SEG("ss", R_SS)
2045 SEG("fs", R_FS)
2046 SEG("gs", R_GS)
2047 { "pc", 0, monitor_get_pc, },
2048 #elif defined(TARGET_PPC)
2049 /* General purpose registers */
2050 { "r0", offsetof(CPUState, gpr[0]) },
2051 { "r1", offsetof(CPUState, gpr[1]) },
2052 { "r2", offsetof(CPUState, gpr[2]) },
2053 { "r3", offsetof(CPUState, gpr[3]) },
2054 { "r4", offsetof(CPUState, gpr[4]) },
2055 { "r5", offsetof(CPUState, gpr[5]) },
2056 { "r6", offsetof(CPUState, gpr[6]) },
2057 { "r7", offsetof(CPUState, gpr[7]) },
2058 { "r8", offsetof(CPUState, gpr[8]) },
2059 { "r9", offsetof(CPUState, gpr[9]) },
2060 { "r10", offsetof(CPUState, gpr[10]) },
2061 { "r11", offsetof(CPUState, gpr[11]) },
2062 { "r12", offsetof(CPUState, gpr[12]) },
2063 { "r13", offsetof(CPUState, gpr[13]) },
2064 { "r14", offsetof(CPUState, gpr[14]) },
2065 { "r15", offsetof(CPUState, gpr[15]) },
2066 { "r16", offsetof(CPUState, gpr[16]) },
2067 { "r17", offsetof(CPUState, gpr[17]) },
2068 { "r18", offsetof(CPUState, gpr[18]) },
2069 { "r19", offsetof(CPUState, gpr[19]) },
2070 { "r20", offsetof(CPUState, gpr[20]) },
2071 { "r21", offsetof(CPUState, gpr[21]) },
2072 { "r22", offsetof(CPUState, gpr[22]) },
2073 { "r23", offsetof(CPUState, gpr[23]) },
2074 { "r24", offsetof(CPUState, gpr[24]) },
2075 { "r25", offsetof(CPUState, gpr[25]) },
2076 { "r26", offsetof(CPUState, gpr[26]) },
2077 { "r27", offsetof(CPUState, gpr[27]) },
2078 { "r28", offsetof(CPUState, gpr[28]) },
2079 { "r29", offsetof(CPUState, gpr[29]) },
2080 { "r30", offsetof(CPUState, gpr[30]) },
2081 { "r31", offsetof(CPUState, gpr[31]) },
2082 /* Floating point registers */
2083 { "f0", offsetof(CPUState, fpr[0]) },
2084 { "f1", offsetof(CPUState, fpr[1]) },
2085 { "f2", offsetof(CPUState, fpr[2]) },
2086 { "f3", offsetof(CPUState, fpr[3]) },
2087 { "f4", offsetof(CPUState, fpr[4]) },
2088 { "f5", offsetof(CPUState, fpr[5]) },
2089 { "f6", offsetof(CPUState, fpr[6]) },
2090 { "f7", offsetof(CPUState, fpr[7]) },
2091 { "f8", offsetof(CPUState, fpr[8]) },
2092 { "f9", offsetof(CPUState, fpr[9]) },
2093 { "f10", offsetof(CPUState, fpr[10]) },
2094 { "f11", offsetof(CPUState, fpr[11]) },
2095 { "f12", offsetof(CPUState, fpr[12]) },
2096 { "f13", offsetof(CPUState, fpr[13]) },
2097 { "f14", offsetof(CPUState, fpr[14]) },
2098 { "f15", offsetof(CPUState, fpr[15]) },
2099 { "f16", offsetof(CPUState, fpr[16]) },
2100 { "f17", offsetof(CPUState, fpr[17]) },
2101 { "f18", offsetof(CPUState, fpr[18]) },
2102 { "f19", offsetof(CPUState, fpr[19]) },
2103 { "f20", offsetof(CPUState, fpr[20]) },
2104 { "f21", offsetof(CPUState, fpr[21]) },
2105 { "f22", offsetof(CPUState, fpr[22]) },
2106 { "f23", offsetof(CPUState, fpr[23]) },
2107 { "f24", offsetof(CPUState, fpr[24]) },
2108 { "f25", offsetof(CPUState, fpr[25]) },
2109 { "f26", offsetof(CPUState, fpr[26]) },
2110 { "f27", offsetof(CPUState, fpr[27]) },
2111 { "f28", offsetof(CPUState, fpr[28]) },
2112 { "f29", offsetof(CPUState, fpr[29]) },
2113 { "f30", offsetof(CPUState, fpr[30]) },
2114 { "f31", offsetof(CPUState, fpr[31]) },
2115 { "fpscr", offsetof(CPUState, fpscr) },
2116 /* Next instruction pointer */
2117 { "nip|pc", offsetof(CPUState, nip) },
2118 { "lr", offsetof(CPUState, lr) },
2119 { "ctr", offsetof(CPUState, ctr) },
2120 { "decr", 0, &monitor_get_decr, },
2121 { "ccr", 0, &monitor_get_ccr, },
2122 /* Machine state register */
2123 { "msr", 0, &monitor_get_msr, },
2124 { "xer", 0, &monitor_get_xer, },
2125 { "tbu", 0, &monitor_get_tbu, },
2126 { "tbl", 0, &monitor_get_tbl, },
2127 #if defined(TARGET_PPC64)
2128 /* Address space register */
2129 { "asr", offsetof(CPUState, asr) },
2130 #endif
2131 /* Segment registers */
2132 { "sdr1", offsetof(CPUState, sdr1) },
2133 { "sr0", offsetof(CPUState, sr[0]) },
2134 { "sr1", offsetof(CPUState, sr[1]) },
2135 { "sr2", offsetof(CPUState, sr[2]) },
2136 { "sr3", offsetof(CPUState, sr[3]) },
2137 { "sr4", offsetof(CPUState, sr[4]) },
2138 { "sr5", offsetof(CPUState, sr[5]) },
2139 { "sr6", offsetof(CPUState, sr[6]) },
2140 { "sr7", offsetof(CPUState, sr[7]) },
2141 { "sr8", offsetof(CPUState, sr[8]) },
2142 { "sr9", offsetof(CPUState, sr[9]) },
2143 { "sr10", offsetof(CPUState, sr[10]) },
2144 { "sr11", offsetof(CPUState, sr[11]) },
2145 { "sr12", offsetof(CPUState, sr[12]) },
2146 { "sr13", offsetof(CPUState, sr[13]) },
2147 { "sr14", offsetof(CPUState, sr[14]) },
2148 { "sr15", offsetof(CPUState, sr[15]) },
2149 /* Too lazy to put BATs and SPRs ... */
2150 #elif defined(TARGET_SPARC)
2151 { "g0", offsetof(CPUState, gregs[0]) },
2152 { "g1", offsetof(CPUState, gregs[1]) },
2153 { "g2", offsetof(CPUState, gregs[2]) },
2154 { "g3", offsetof(CPUState, gregs[3]) },
2155 { "g4", offsetof(CPUState, gregs[4]) },
2156 { "g5", offsetof(CPUState, gregs[5]) },
2157 { "g6", offsetof(CPUState, gregs[6]) },
2158 { "g7", offsetof(CPUState, gregs[7]) },
2159 { "o0", 0, monitor_get_reg },
2160 { "o1", 1, monitor_get_reg },
2161 { "o2", 2, monitor_get_reg },
2162 { "o3", 3, monitor_get_reg },
2163 { "o4", 4, monitor_get_reg },
2164 { "o5", 5, monitor_get_reg },
2165 { "o6", 6, monitor_get_reg },
2166 { "o7", 7, monitor_get_reg },
2167 { "l0", 8, monitor_get_reg },
2168 { "l1", 9, monitor_get_reg },
2169 { "l2", 10, monitor_get_reg },
2170 { "l3", 11, monitor_get_reg },
2171 { "l4", 12, monitor_get_reg },
2172 { "l5", 13, monitor_get_reg },
2173 { "l6", 14, monitor_get_reg },
2174 { "l7", 15, monitor_get_reg },
2175 { "i0", 16, monitor_get_reg },
2176 { "i1", 17, monitor_get_reg },
2177 { "i2", 18, monitor_get_reg },
2178 { "i3", 19, monitor_get_reg },
2179 { "i4", 20, monitor_get_reg },
2180 { "i5", 21, monitor_get_reg },
2181 { "i6", 22, monitor_get_reg },
2182 { "i7", 23, monitor_get_reg },
2183 { "pc", offsetof(CPUState, pc) },
2184 { "npc", offsetof(CPUState, npc) },
2185 { "y", offsetof(CPUState, y) },
2186 #ifndef TARGET_SPARC64
2187 { "psr", 0, &monitor_get_psr, },
2188 { "wim", offsetof(CPUState, wim) },
2189 #endif
2190 { "tbr", offsetof(CPUState, tbr) },
2191 { "fsr", offsetof(CPUState, fsr) },
2192 { "f0", offsetof(CPUState, fpr[0]) },
2193 { "f1", offsetof(CPUState, fpr[1]) },
2194 { "f2", offsetof(CPUState, fpr[2]) },
2195 { "f3", offsetof(CPUState, fpr[3]) },
2196 { "f4", offsetof(CPUState, fpr[4]) },
2197 { "f5", offsetof(CPUState, fpr[5]) },
2198 { "f6", offsetof(CPUState, fpr[6]) },
2199 { "f7", offsetof(CPUState, fpr[7]) },
2200 { "f8", offsetof(CPUState, fpr[8]) },
2201 { "f9", offsetof(CPUState, fpr[9]) },
2202 { "f10", offsetof(CPUState, fpr[10]) },
2203 { "f11", offsetof(CPUState, fpr[11]) },
2204 { "f12", offsetof(CPUState, fpr[12]) },
2205 { "f13", offsetof(CPUState, fpr[13]) },
2206 { "f14", offsetof(CPUState, fpr[14]) },
2207 { "f15", offsetof(CPUState, fpr[15]) },
2208 { "f16", offsetof(CPUState, fpr[16]) },
2209 { "f17", offsetof(CPUState, fpr[17]) },
2210 { "f18", offsetof(CPUState, fpr[18]) },
2211 { "f19", offsetof(CPUState, fpr[19]) },
2212 { "f20", offsetof(CPUState, fpr[20]) },
2213 { "f21", offsetof(CPUState, fpr[21]) },
2214 { "f22", offsetof(CPUState, fpr[22]) },
2215 { "f23", offsetof(CPUState, fpr[23]) },
2216 { "f24", offsetof(CPUState, fpr[24]) },
2217 { "f25", offsetof(CPUState, fpr[25]) },
2218 { "f26", offsetof(CPUState, fpr[26]) },
2219 { "f27", offsetof(CPUState, fpr[27]) },
2220 { "f28", offsetof(CPUState, fpr[28]) },
2221 { "f29", offsetof(CPUState, fpr[29]) },
2222 { "f30", offsetof(CPUState, fpr[30]) },
2223 { "f31", offsetof(CPUState, fpr[31]) },
2224 #ifdef TARGET_SPARC64
2225 { "f32", offsetof(CPUState, fpr[32]) },
2226 { "f34", offsetof(CPUState, fpr[34]) },
2227 { "f36", offsetof(CPUState, fpr[36]) },
2228 { "f38", offsetof(CPUState, fpr[38]) },
2229 { "f40", offsetof(CPUState, fpr[40]) },
2230 { "f42", offsetof(CPUState, fpr[42]) },
2231 { "f44", offsetof(CPUState, fpr[44]) },
2232 { "f46", offsetof(CPUState, fpr[46]) },
2233 { "f48", offsetof(CPUState, fpr[48]) },
2234 { "f50", offsetof(CPUState, fpr[50]) },
2235 { "f52", offsetof(CPUState, fpr[52]) },
2236 { "f54", offsetof(CPUState, fpr[54]) },
2237 { "f56", offsetof(CPUState, fpr[56]) },
2238 { "f58", offsetof(CPUState, fpr[58]) },
2239 { "f60", offsetof(CPUState, fpr[60]) },
2240 { "f62", offsetof(CPUState, fpr[62]) },
2241 { "asi", offsetof(CPUState, asi) },
2242 { "pstate", offsetof(CPUState, pstate) },
2243 { "cansave", offsetof(CPUState, cansave) },
2244 { "canrestore", offsetof(CPUState, canrestore) },
2245 { "otherwin", offsetof(CPUState, otherwin) },
2246 { "wstate", offsetof(CPUState, wstate) },
2247 { "cleanwin", offsetof(CPUState, cleanwin) },
2248 { "fprs", offsetof(CPUState, fprs) },
2249 #endif
2250 #endif
2251 { NULL },
2254 static void expr_error(Monitor *mon, const char *msg)
2256 monitor_printf(mon, "%s\n", msg);
2257 longjmp(expr_env, 1);
2260 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2261 static int get_monitor_def(target_long *pval, const char *name)
2263 const MonitorDef *md;
2264 void *ptr;
2266 for(md = monitor_defs; md->name != NULL; md++) {
2267 if (compare_cmd(name, md->name)) {
2268 if (md->get_value) {
2269 *pval = md->get_value(md, md->offset);
2270 } else {
2271 CPUState *env = mon_get_cpu();
2272 if (!env)
2273 return -2;
2274 ptr = (uint8_t *)env + md->offset;
2275 switch(md->type) {
2276 case MD_I32:
2277 *pval = *(int32_t *)ptr;
2278 break;
2279 case MD_TLONG:
2280 *pval = *(target_long *)ptr;
2281 break;
2282 default:
2283 *pval = 0;
2284 break;
2287 return 0;
2290 return -1;
2293 static void next(void)
2295 if (pch != '\0') {
2296 pch++;
2297 while (qemu_isspace(*pch))
2298 pch++;
2302 static int64_t expr_sum(Monitor *mon);
2304 static int64_t expr_unary(Monitor *mon)
2306 int64_t n;
2307 char *p;
2308 int ret;
2310 switch(*pch) {
2311 case '+':
2312 next();
2313 n = expr_unary(mon);
2314 break;
2315 case '-':
2316 next();
2317 n = -expr_unary(mon);
2318 break;
2319 case '~':
2320 next();
2321 n = ~expr_unary(mon);
2322 break;
2323 case '(':
2324 next();
2325 n = expr_sum(mon);
2326 if (*pch != ')') {
2327 expr_error(mon, "')' expected");
2329 next();
2330 break;
2331 case '\'':
2332 pch++;
2333 if (*pch == '\0')
2334 expr_error(mon, "character constant expected");
2335 n = *pch;
2336 pch++;
2337 if (*pch != '\'')
2338 expr_error(mon, "missing terminating \' character");
2339 next();
2340 break;
2341 case '$':
2343 char buf[128], *q;
2344 target_long reg=0;
2346 pch++;
2347 q = buf;
2348 while ((*pch >= 'a' && *pch <= 'z') ||
2349 (*pch >= 'A' && *pch <= 'Z') ||
2350 (*pch >= '0' && *pch <= '9') ||
2351 *pch == '_' || *pch == '.') {
2352 if ((q - buf) < sizeof(buf) - 1)
2353 *q++ = *pch;
2354 pch++;
2356 while (qemu_isspace(*pch))
2357 pch++;
2358 *q = 0;
2359 ret = get_monitor_def(&reg, buf);
2360 if (ret == -1)
2361 expr_error(mon, "unknown register");
2362 else if (ret == -2)
2363 expr_error(mon, "no cpu defined");
2364 n = reg;
2366 break;
2367 case '\0':
2368 expr_error(mon, "unexpected end of expression");
2369 n = 0;
2370 break;
2371 default:
2372 #if TARGET_PHYS_ADDR_BITS > 32
2373 n = strtoull(pch, &p, 0);
2374 #else
2375 n = strtoul(pch, &p, 0);
2376 #endif
2377 if (pch == p) {
2378 expr_error(mon, "invalid char in expression");
2380 pch = p;
2381 while (qemu_isspace(*pch))
2382 pch++;
2383 break;
2385 return n;
2389 static int64_t expr_prod(Monitor *mon)
2391 int64_t val, val2;
2392 int op;
2394 val = expr_unary(mon);
2395 for(;;) {
2396 op = *pch;
2397 if (op != '*' && op != '/' && op != '%')
2398 break;
2399 next();
2400 val2 = expr_unary(mon);
2401 switch(op) {
2402 default:
2403 case '*':
2404 val *= val2;
2405 break;
2406 case '/':
2407 case '%':
2408 if (val2 == 0)
2409 expr_error(mon, "division by zero");
2410 if (op == '/')
2411 val /= val2;
2412 else
2413 val %= val2;
2414 break;
2417 return val;
2420 static int64_t expr_logic(Monitor *mon)
2422 int64_t val, val2;
2423 int op;
2425 val = expr_prod(mon);
2426 for(;;) {
2427 op = *pch;
2428 if (op != '&' && op != '|' && op != '^')
2429 break;
2430 next();
2431 val2 = expr_prod(mon);
2432 switch(op) {
2433 default:
2434 case '&':
2435 val &= val2;
2436 break;
2437 case '|':
2438 val |= val2;
2439 break;
2440 case '^':
2441 val ^= val2;
2442 break;
2445 return val;
2448 static int64_t expr_sum(Monitor *mon)
2450 int64_t val, val2;
2451 int op;
2453 val = expr_logic(mon);
2454 for(;;) {
2455 op = *pch;
2456 if (op != '+' && op != '-')
2457 break;
2458 next();
2459 val2 = expr_logic(mon);
2460 if (op == '+')
2461 val += val2;
2462 else
2463 val -= val2;
2465 return val;
2468 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2470 pch = *pp;
2471 if (setjmp(expr_env)) {
2472 *pp = pch;
2473 return -1;
2475 while (qemu_isspace(*pch))
2476 pch++;
2477 *pval = expr_sum(mon);
2478 *pp = pch;
2479 return 0;
2482 static int get_str(char *buf, int buf_size, const char **pp)
2484 const char *p;
2485 char *q;
2486 int c;
2488 q = buf;
2489 p = *pp;
2490 while (qemu_isspace(*p))
2491 p++;
2492 if (*p == '\0') {
2493 fail:
2494 *q = '\0';
2495 *pp = p;
2496 return -1;
2498 if (*p == '\"') {
2499 p++;
2500 while (*p != '\0' && *p != '\"') {
2501 if (*p == '\\') {
2502 p++;
2503 c = *p++;
2504 switch(c) {
2505 case 'n':
2506 c = '\n';
2507 break;
2508 case 'r':
2509 c = '\r';
2510 break;
2511 case '\\':
2512 case '\'':
2513 case '\"':
2514 break;
2515 default:
2516 qemu_printf("unsupported escape code: '\\%c'\n", c);
2517 goto fail;
2519 if ((q - buf) < buf_size - 1) {
2520 *q++ = c;
2522 } else {
2523 if ((q - buf) < buf_size - 1) {
2524 *q++ = *p;
2526 p++;
2529 if (*p != '\"') {
2530 qemu_printf("unterminated string\n");
2531 goto fail;
2533 p++;
2534 } else {
2535 while (*p != '\0' && !qemu_isspace(*p)) {
2536 if ((q - buf) < buf_size - 1) {
2537 *q++ = *p;
2539 p++;
2542 *q = '\0';
2543 *pp = p;
2544 return 0;
2548 * Store the command-name in cmdname, and return a pointer to
2549 * the remaining of the command string.
2551 static const char *get_command_name(const char *cmdline,
2552 char *cmdname, size_t nlen)
2554 size_t len;
2555 const char *p, *pstart;
2557 p = cmdline;
2558 while (qemu_isspace(*p))
2559 p++;
2560 if (*p == '\0')
2561 return NULL;
2562 pstart = p;
2563 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2564 p++;
2565 len = p - pstart;
2566 if (len > nlen - 1)
2567 len = nlen - 1;
2568 memcpy(cmdname, pstart, len);
2569 cmdname[len] = '\0';
2570 return p;
2573 static int default_fmt_format = 'x';
2574 static int default_fmt_size = 4;
2576 #define MAX_ARGS 16
2578 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2580 const char *p, *typestr;
2581 int c, nb_args, i, has_arg;
2582 const mon_cmd_t *cmd;
2583 char cmdname[256];
2584 char buf[1024];
2585 void *str_allocated[MAX_ARGS];
2586 void *args[MAX_ARGS];
2587 void (*handler_0)(Monitor *mon);
2588 void (*handler_1)(Monitor *mon, void *arg0);
2589 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2590 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2591 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2592 void *arg3);
2593 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2594 void *arg3, void *arg4);
2595 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2596 void *arg3, void *arg4, void *arg5);
2597 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2598 void *arg3, void *arg4, void *arg5, void *arg6);
2599 void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2600 void *arg3, void *arg4, void *arg5, void *arg6,
2601 void *arg7);
2602 void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2603 void *arg3, void *arg4, void *arg5, void *arg6,
2604 void *arg7, void *arg8);
2605 void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2606 void *arg3, void *arg4, void *arg5, void *arg6,
2607 void *arg7, void *arg8, void *arg9);
2609 #ifdef DEBUG
2610 monitor_printf(mon, "command='%s'\n", cmdline);
2611 #endif
2613 /* extract the command name */
2614 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2615 if (!p)
2616 return;
2618 /* find the command */
2619 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2620 if (compare_cmd(cmdname, cmd->name))
2621 break;
2624 if (cmd->name == NULL) {
2625 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2626 return;
2629 for(i = 0; i < MAX_ARGS; i++)
2630 str_allocated[i] = NULL;
2632 /* parse the parameters */
2633 typestr = cmd->args_type;
2634 nb_args = 0;
2635 for(;;) {
2636 c = *typestr;
2637 if (c == '\0')
2638 break;
2639 typestr++;
2640 switch(c) {
2641 case 'F':
2642 case 'B':
2643 case 's':
2645 int ret;
2646 char *str;
2648 while (qemu_isspace(*p))
2649 p++;
2650 if (*typestr == '?') {
2651 typestr++;
2652 if (*p == '\0') {
2653 /* no optional string: NULL argument */
2654 str = NULL;
2655 goto add_str;
2658 ret = get_str(buf, sizeof(buf), &p);
2659 if (ret < 0) {
2660 switch(c) {
2661 case 'F':
2662 monitor_printf(mon, "%s: filename expected\n",
2663 cmdname);
2664 break;
2665 case 'B':
2666 monitor_printf(mon, "%s: block device name expected\n",
2667 cmdname);
2668 break;
2669 default:
2670 monitor_printf(mon, "%s: string expected\n", cmdname);
2671 break;
2673 goto fail;
2675 str = qemu_malloc(strlen(buf) + 1);
2676 pstrcpy(str, sizeof(buf), buf);
2677 str_allocated[nb_args] = str;
2678 add_str:
2679 if (nb_args >= MAX_ARGS) {
2680 error_args:
2681 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2682 goto fail;
2684 args[nb_args++] = str;
2686 break;
2687 case '/':
2689 int count, format, size;
2691 while (qemu_isspace(*p))
2692 p++;
2693 if (*p == '/') {
2694 /* format found */
2695 p++;
2696 count = 1;
2697 if (qemu_isdigit(*p)) {
2698 count = 0;
2699 while (qemu_isdigit(*p)) {
2700 count = count * 10 + (*p - '0');
2701 p++;
2704 size = -1;
2705 format = -1;
2706 for(;;) {
2707 switch(*p) {
2708 case 'o':
2709 case 'd':
2710 case 'u':
2711 case 'x':
2712 case 'i':
2713 case 'c':
2714 format = *p++;
2715 break;
2716 case 'b':
2717 size = 1;
2718 p++;
2719 break;
2720 case 'h':
2721 size = 2;
2722 p++;
2723 break;
2724 case 'w':
2725 size = 4;
2726 p++;
2727 break;
2728 case 'g':
2729 case 'L':
2730 size = 8;
2731 p++;
2732 break;
2733 default:
2734 goto next;
2737 next:
2738 if (*p != '\0' && !qemu_isspace(*p)) {
2739 monitor_printf(mon, "invalid char in format: '%c'\n",
2740 *p);
2741 goto fail;
2743 if (format < 0)
2744 format = default_fmt_format;
2745 if (format != 'i') {
2746 /* for 'i', not specifying a size gives -1 as size */
2747 if (size < 0)
2748 size = default_fmt_size;
2749 default_fmt_size = size;
2751 default_fmt_format = format;
2752 } else {
2753 count = 1;
2754 format = default_fmt_format;
2755 if (format != 'i') {
2756 size = default_fmt_size;
2757 } else {
2758 size = -1;
2761 if (nb_args + 3 > MAX_ARGS)
2762 goto error_args;
2763 args[nb_args++] = (void*)(long)count;
2764 args[nb_args++] = (void*)(long)format;
2765 args[nb_args++] = (void*)(long)size;
2767 break;
2768 case 'i':
2769 case 'l':
2771 int64_t val;
2773 while (qemu_isspace(*p))
2774 p++;
2775 if (*typestr == '?' || *typestr == '.') {
2776 if (*typestr == '?') {
2777 if (*p == '\0')
2778 has_arg = 0;
2779 else
2780 has_arg = 1;
2781 } else {
2782 if (*p == '.') {
2783 p++;
2784 while (qemu_isspace(*p))
2785 p++;
2786 has_arg = 1;
2787 } else {
2788 has_arg = 0;
2791 typestr++;
2792 if (nb_args >= MAX_ARGS)
2793 goto error_args;
2794 args[nb_args++] = (void *)(long)has_arg;
2795 if (!has_arg) {
2796 if (nb_args >= MAX_ARGS)
2797 goto error_args;
2798 val = -1;
2799 goto add_num;
2802 if (get_expr(mon, &val, &p))
2803 goto fail;
2804 add_num:
2805 if (c == 'i') {
2806 if (nb_args >= MAX_ARGS)
2807 goto error_args;
2808 args[nb_args++] = (void *)(long)val;
2809 } else {
2810 if ((nb_args + 1) >= MAX_ARGS)
2811 goto error_args;
2812 #if TARGET_PHYS_ADDR_BITS > 32
2813 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2814 #else
2815 args[nb_args++] = (void *)0;
2816 #endif
2817 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2820 break;
2821 case '-':
2823 int has_option;
2824 /* option */
2826 c = *typestr++;
2827 if (c == '\0')
2828 goto bad_type;
2829 while (qemu_isspace(*p))
2830 p++;
2831 has_option = 0;
2832 if (*p == '-') {
2833 p++;
2834 if (*p != c) {
2835 monitor_printf(mon, "%s: unsupported option -%c\n",
2836 cmdname, *p);
2837 goto fail;
2839 p++;
2840 has_option = 1;
2842 if (nb_args >= MAX_ARGS)
2843 goto error_args;
2844 args[nb_args++] = (void *)(long)has_option;
2846 break;
2847 default:
2848 bad_type:
2849 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2850 goto fail;
2853 /* check that all arguments were parsed */
2854 while (qemu_isspace(*p))
2855 p++;
2856 if (*p != '\0') {
2857 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2858 cmdname);
2859 goto fail;
2862 switch(nb_args) {
2863 case 0:
2864 handler_0 = cmd->handler;
2865 handler_0(mon);
2866 break;
2867 case 1:
2868 handler_1 = cmd->handler;
2869 handler_1(mon, args[0]);
2870 break;
2871 case 2:
2872 handler_2 = cmd->handler;
2873 handler_2(mon, args[0], args[1]);
2874 break;
2875 case 3:
2876 handler_3 = cmd->handler;
2877 handler_3(mon, args[0], args[1], args[2]);
2878 break;
2879 case 4:
2880 handler_4 = cmd->handler;
2881 handler_4(mon, args[0], args[1], args[2], args[3]);
2882 break;
2883 case 5:
2884 handler_5 = cmd->handler;
2885 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2886 break;
2887 case 6:
2888 handler_6 = cmd->handler;
2889 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2890 break;
2891 case 7:
2892 handler_7 = cmd->handler;
2893 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2894 args[6]);
2895 break;
2896 case 8:
2897 handler_8 = cmd->handler;
2898 handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2899 args[6], args[7]);
2900 break;
2901 case 9:
2902 handler_9 = cmd->handler;
2903 handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2904 args[6], args[7], args[8]);
2905 break;
2906 case 10:
2907 handler_10 = cmd->handler;
2908 handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2909 args[6], args[7], args[8], args[9]);
2910 break;
2911 default:
2912 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2913 goto fail;
2915 fail:
2916 for(i = 0; i < MAX_ARGS; i++)
2917 qemu_free(str_allocated[i]);
2920 static void cmd_completion(const char *name, const char *list)
2922 const char *p, *pstart;
2923 char cmd[128];
2924 int len;
2926 p = list;
2927 for(;;) {
2928 pstart = p;
2929 p = strchr(p, '|');
2930 if (!p)
2931 p = pstart + strlen(pstart);
2932 len = p - pstart;
2933 if (len > sizeof(cmd) - 2)
2934 len = sizeof(cmd) - 2;
2935 memcpy(cmd, pstart, len);
2936 cmd[len] = '\0';
2937 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2938 readline_add_completion(cur_mon->rs, cmd);
2940 if (*p == '\0')
2941 break;
2942 p++;
2946 static void file_completion(const char *input)
2948 DIR *ffs;
2949 struct dirent *d;
2950 char path[1024];
2951 char file[1024], file_prefix[1024];
2952 int input_path_len;
2953 const char *p;
2955 p = strrchr(input, '/');
2956 if (!p) {
2957 input_path_len = 0;
2958 pstrcpy(file_prefix, sizeof(file_prefix), input);
2959 pstrcpy(path, sizeof(path), ".");
2960 } else {
2961 input_path_len = p - input + 1;
2962 memcpy(path, input, input_path_len);
2963 if (input_path_len > sizeof(path) - 1)
2964 input_path_len = sizeof(path) - 1;
2965 path[input_path_len] = '\0';
2966 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2968 #ifdef DEBUG_COMPLETION
2969 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2970 input, path, file_prefix);
2971 #endif
2972 ffs = opendir(path);
2973 if (!ffs)
2974 return;
2975 for(;;) {
2976 struct stat sb;
2977 d = readdir(ffs);
2978 if (!d)
2979 break;
2980 if (strstart(d->d_name, file_prefix, NULL)) {
2981 memcpy(file, input, input_path_len);
2982 if (input_path_len < sizeof(file))
2983 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2984 d->d_name);
2985 /* stat the file to find out if it's a directory.
2986 * In that case add a slash to speed up typing long paths
2988 stat(file, &sb);
2989 if(S_ISDIR(sb.st_mode))
2990 pstrcat(file, sizeof(file), "/");
2991 readline_add_completion(cur_mon->rs, file);
2994 closedir(ffs);
2997 static void block_completion_it(void *opaque, BlockDriverState *bs)
2999 const char *name = bdrv_get_device_name(bs);
3000 const char *input = opaque;
3002 if (input[0] == '\0' ||
3003 !strncmp(name, (char *)input, strlen(input))) {
3004 readline_add_completion(cur_mon->rs, name);
3008 /* NOTE: this parser is an approximate form of the real command parser */
3009 static void parse_cmdline(const char *cmdline,
3010 int *pnb_args, char **args)
3012 const char *p;
3013 int nb_args, ret;
3014 char buf[1024];
3016 p = cmdline;
3017 nb_args = 0;
3018 for(;;) {
3019 while (qemu_isspace(*p))
3020 p++;
3021 if (*p == '\0')
3022 break;
3023 if (nb_args >= MAX_ARGS)
3024 break;
3025 ret = get_str(buf, sizeof(buf), &p);
3026 args[nb_args] = qemu_strdup(buf);
3027 nb_args++;
3028 if (ret < 0)
3029 break;
3031 *pnb_args = nb_args;
3034 static void monitor_find_completion(const char *cmdline)
3036 const char *cmdname;
3037 char *args[MAX_ARGS];
3038 int nb_args, i, len;
3039 const char *ptype, *str;
3040 const mon_cmd_t *cmd;
3041 const KeyDef *key;
3043 parse_cmdline(cmdline, &nb_args, args);
3044 #ifdef DEBUG_COMPLETION
3045 for(i = 0; i < nb_args; i++) {
3046 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3048 #endif
3050 /* if the line ends with a space, it means we want to complete the
3051 next arg */
3052 len = strlen(cmdline);
3053 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3054 if (nb_args >= MAX_ARGS)
3055 return;
3056 args[nb_args++] = qemu_strdup("");
3058 if (nb_args <= 1) {
3059 /* command completion */
3060 if (nb_args == 0)
3061 cmdname = "";
3062 else
3063 cmdname = args[0];
3064 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3065 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3066 cmd_completion(cmdname, cmd->name);
3068 } else {
3069 /* find the command */
3070 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3071 if (compare_cmd(args[0], cmd->name))
3072 goto found;
3074 return;
3075 found:
3076 ptype = cmd->args_type;
3077 for(i = 0; i < nb_args - 2; i++) {
3078 if (*ptype != '\0') {
3079 ptype++;
3080 while (*ptype == '?')
3081 ptype++;
3084 str = args[nb_args - 1];
3085 switch(*ptype) {
3086 case 'F':
3087 /* file completion */
3088 readline_set_completion_index(cur_mon->rs, strlen(str));
3089 file_completion(str);
3090 break;
3091 case 'B':
3092 /* block device name completion */
3093 readline_set_completion_index(cur_mon->rs, strlen(str));
3094 bdrv_iterate(block_completion_it, (void *)str);
3095 break;
3096 case 's':
3097 /* XXX: more generic ? */
3098 if (!strcmp(cmd->name, "info")) {
3099 readline_set_completion_index(cur_mon->rs, strlen(str));
3100 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3101 cmd_completion(str, cmd->name);
3103 } else if (!strcmp(cmd->name, "sendkey")) {
3104 char *sep = strrchr(str, '-');
3105 if (sep)
3106 str = sep + 1;
3107 readline_set_completion_index(cur_mon->rs, strlen(str));
3108 for(key = key_defs; key->name != NULL; key++) {
3109 cmd_completion(str, key->name);
3111 } else if (!strcmp(cmd->name, "help|?")) {
3112 readline_set_completion_index(cur_mon->rs, strlen(str));
3113 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3114 cmd_completion(str, cmd->name);
3117 break;
3118 default:
3119 break;
3122 for(i = 0; i < nb_args; i++)
3123 qemu_free(args[i]);
3126 static int monitor_can_read(void *opaque)
3128 Monitor *mon = opaque;
3130 return (mon->suspend_cnt == 0) ? 128 : 0;
3133 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3135 Monitor *old_mon = cur_mon;
3136 int i;
3138 cur_mon = opaque;
3140 if (cur_mon->rs) {
3141 for (i = 0; i < size; i++)
3142 readline_handle_byte(cur_mon->rs, buf[i]);
3143 } else {
3144 if (size == 0 || buf[size - 1] != 0)
3145 monitor_printf(cur_mon, "corrupted command\n");
3146 else
3147 monitor_handle_command(cur_mon, (char *)buf);
3150 cur_mon = old_mon;
3153 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3155 monitor_suspend(mon);
3156 monitor_handle_command(mon, cmdline);
3157 monitor_resume(mon);
3160 int monitor_suspend(Monitor *mon)
3162 if (!mon->rs)
3163 return -ENOTTY;
3164 mon->suspend_cnt++;
3165 return 0;
3168 void monitor_resume(Monitor *mon)
3170 if (!mon->rs)
3171 return;
3172 if (--mon->suspend_cnt == 0)
3173 readline_show_prompt(mon->rs);
3176 static void monitor_event(void *opaque, int event)
3178 Monitor *mon = opaque;
3180 switch (event) {
3181 case CHR_EVENT_MUX_IN:
3182 readline_restart(mon->rs);
3183 monitor_resume(mon);
3184 monitor_flush(mon);
3185 break;
3187 case CHR_EVENT_MUX_OUT:
3188 if (mon->suspend_cnt == 0)
3189 monitor_printf(mon, "\n");
3190 monitor_flush(mon);
3191 monitor_suspend(mon);
3192 break;
3194 case CHR_EVENT_RESET:
3195 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3196 "information\n", QEMU_VERSION);
3197 if (mon->chr->focus == 0)
3198 readline_show_prompt(mon->rs);
3199 break;
3205 * Local variables:
3206 * c-indent-level: 4
3207 * c-basic-offset: 4
3208 * tab-width: 8
3209 * End:
3212 void monitor_init(CharDriverState *chr, int flags)
3214 static int is_first_init = 1;
3215 Monitor *mon;
3217 if (is_first_init) {
3218 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3219 is_first_init = 0;
3222 mon = qemu_mallocz(sizeof(*mon));
3224 mon->chr = chr;
3225 mon->flags = flags;
3226 if (mon->chr->focus != 0)
3227 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3228 if (flags & MONITOR_USE_READLINE) {
3229 mon->rs = readline_init(mon, monitor_find_completion);
3230 monitor_read_command(mon, 0);
3233 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3234 mon);
3236 LIST_INSERT_HEAD(&mon_list, mon, entry);
3237 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3238 cur_mon = mon;
3241 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3243 BlockDriverState *bs = opaque;
3244 int ret = 0;
3246 if (bdrv_set_key(bs, password) != 0) {
3247 monitor_printf(mon, "invalid password\n");
3248 ret = -EPERM;
3250 if (mon->password_completion_cb)
3251 mon->password_completion_cb(mon->password_opaque, ret);
3253 monitor_read_command(mon, 1);
3256 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3257 BlockDriverCompletionFunc *completion_cb,
3258 void *opaque)
3260 int err;
3262 if (!bdrv_key_required(bs)) {
3263 if (completion_cb)
3264 completion_cb(opaque, 0);
3265 return;
3268 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3269 bdrv_get_encrypted_filename(bs));
3271 mon->password_completion_cb = completion_cb;
3272 mon->password_opaque = opaque;
3274 err = monitor_read_password(mon, bdrv_password_cb, bs);
3276 if (err && completion_cb)
3277 completion_cb(opaque, err);