monitor: Convert do_info_version() to QObject
[qemu.git] / monitor.c
blob8c9cc9b190e2131d8401b7018a78c1327e728bca
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 "hw/loader.h"
33 #include "gdbstub.h"
34 #include "net.h"
35 #include "qemu-char.h"
36 #include "sysemu.h"
37 #include "monitor.h"
38 #include "readline.h"
39 #include "console.h"
40 #include "block.h"
41 #include "audio/audio.h"
42 #include "disas.h"
43 #include "balloon.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
46 #include "kvm.h"
47 #include "acl.h"
48 #include "qint.h"
49 #include "qdict.h"
50 #include "qstring.h"
52 //#define DEBUG
53 //#define DEBUG_COMPLETION
56 * Supported types:
58 * 'F' filename
59 * 'B' block device name
60 * 's' string (accept optional quote)
61 * 'i' 32 bit integer
62 * 'l' target long (32 or 64 bit)
63 * '/' optional gdb-like print format (like "/10x")
65 * '?' optional type (for all types, except '/')
66 * '.' other form of optional type (for 'i' and 'l')
67 * '-' optional parameter (eg. '-f')
71 typedef struct mon_cmd_t {
72 const char *name;
73 const char *args_type;
74 const char *params;
75 const char *help;
76 void (*user_print)(Monitor *mon, const QObject *data);
77 union {
78 void (*info)(Monitor *mon);
79 void (*info_new)(Monitor *mon, QObject **ret_data);
80 void (*cmd)(Monitor *mon, const QDict *qdict);
81 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
82 } mhandler;
83 } mon_cmd_t;
85 /* file descriptors passed via SCM_RIGHTS */
86 typedef struct mon_fd_t mon_fd_t;
87 struct mon_fd_t {
88 char *name;
89 int fd;
90 QLIST_ENTRY(mon_fd_t) next;
93 struct Monitor {
94 CharDriverState *chr;
95 int mux_out;
96 int reset_seen;
97 int flags;
98 int suspend_cnt;
99 uint8_t outbuf[1024];
100 int outbuf_index;
101 ReadLineState *rs;
102 CPUState *mon_cpu;
103 BlockDriverCompletionFunc *password_completion_cb;
104 void *password_opaque;
105 QLIST_HEAD(,mon_fd_t) fds;
106 QLIST_ENTRY(Monitor) entry;
109 static QLIST_HEAD(mon_list, Monitor) mon_list;
111 static const mon_cmd_t mon_cmds[];
112 static const mon_cmd_t info_cmds[];
114 Monitor *cur_mon = NULL;
116 static void monitor_command_cb(Monitor *mon, const char *cmdline,
117 void *opaque);
119 static void monitor_read_command(Monitor *mon, int show_prompt)
121 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
122 if (show_prompt)
123 readline_show_prompt(mon->rs);
126 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
127 void *opaque)
129 if (mon->rs) {
130 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
131 /* prompt is printed on return from the command handler */
132 return 0;
133 } else {
134 monitor_printf(mon, "terminal does not support password prompting\n");
135 return -ENOTTY;
139 void monitor_flush(Monitor *mon)
141 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
142 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
143 mon->outbuf_index = 0;
147 /* flush at every end of line or if the buffer is full */
148 static void monitor_puts(Monitor *mon, const char *str)
150 char c;
152 if (!mon)
153 return;
155 for(;;) {
156 c = *str++;
157 if (c == '\0')
158 break;
159 if (c == '\n')
160 mon->outbuf[mon->outbuf_index++] = '\r';
161 mon->outbuf[mon->outbuf_index++] = c;
162 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
163 || c == '\n')
164 monitor_flush(mon);
168 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
170 char buf[4096];
171 vsnprintf(buf, sizeof(buf), fmt, ap);
172 monitor_puts(mon, buf);
175 void monitor_printf(Monitor *mon, const char *fmt, ...)
177 va_list ap;
178 va_start(ap, fmt);
179 monitor_vprintf(mon, fmt, ap);
180 va_end(ap);
183 void monitor_print_filename(Monitor *mon, const char *filename)
185 int i;
187 for (i = 0; filename[i]; i++) {
188 switch (filename[i]) {
189 case ' ':
190 case '"':
191 case '\\':
192 monitor_printf(mon, "\\%c", filename[i]);
193 break;
194 case '\t':
195 monitor_printf(mon, "\\t");
196 break;
197 case '\r':
198 monitor_printf(mon, "\\r");
199 break;
200 case '\n':
201 monitor_printf(mon, "\\n");
202 break;
203 default:
204 monitor_printf(mon, "%c", filename[i]);
205 break;
210 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
212 va_list ap;
213 va_start(ap, fmt);
214 monitor_vprintf((Monitor *)stream, fmt, ap);
215 va_end(ap);
216 return 0;
219 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
221 static inline int monitor_handler_ported(const mon_cmd_t *cmd)
223 return cmd->user_print != NULL;
226 static void monitor_print_qobject(Monitor *mon, const QObject *data)
228 switch (qobject_type(data)) {
229 case QTYPE_QSTRING:
230 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
231 break;
232 case QTYPE_QINT:
233 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
234 break;
235 default:
236 monitor_printf(mon, "ERROR: unsupported type: %d",
237 qobject_type(data));
238 break;
241 monitor_puts(mon, "\n");
244 static int compare_cmd(const char *name, const char *list)
246 const char *p, *pstart;
247 int len;
248 len = strlen(name);
249 p = list;
250 for(;;) {
251 pstart = p;
252 p = strchr(p, '|');
253 if (!p)
254 p = pstart + strlen(pstart);
255 if ((p - pstart) == len && !memcmp(pstart, name, len))
256 return 1;
257 if (*p == '\0')
258 break;
259 p++;
261 return 0;
264 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
265 const char *prefix, const char *name)
267 const mon_cmd_t *cmd;
269 for(cmd = cmds; cmd->name != NULL; cmd++) {
270 if (!name || !strcmp(name, cmd->name))
271 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
272 cmd->params, cmd->help);
276 static void help_cmd(Monitor *mon, const char *name)
278 if (name && !strcmp(name, "info")) {
279 help_cmd_dump(mon, info_cmds, "info ", NULL);
280 } else {
281 help_cmd_dump(mon, mon_cmds, "", name);
282 if (name && !strcmp(name, "log")) {
283 const CPULogItem *item;
284 monitor_printf(mon, "Log items (comma separated):\n");
285 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
286 for(item = cpu_log_items; item->mask != 0; item++) {
287 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
293 static void do_help_cmd(Monitor *mon, const QDict *qdict)
295 help_cmd(mon, qdict_get_try_str(qdict, "name"));
298 static void do_commit(Monitor *mon, const QDict *qdict)
300 int all_devices;
301 DriveInfo *dinfo;
302 const char *device = qdict_get_str(qdict, "device");
304 all_devices = !strcmp(device, "all");
305 QTAILQ_FOREACH(dinfo, &drives, next) {
306 if (!all_devices)
307 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
308 continue;
309 bdrv_commit(dinfo->bdrv);
313 static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
315 const mon_cmd_t *cmd;
316 const char *item = qdict_get_try_str(qdict, "item");
318 if (!item)
319 goto help;
321 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
322 if (compare_cmd(item, cmd->name))
323 break;
326 if (cmd->name == NULL)
327 goto help;
329 if (monitor_handler_ported(cmd)) {
330 cmd->mhandler.info_new(mon, ret_data);
331 if (*ret_data)
332 cmd->user_print(mon, *ret_data);
333 } else {
334 cmd->mhandler.info(mon);
337 return;
339 help:
340 help_cmd(mon, "info");
344 * do_info_version(): Show QEMU version
346 static void do_info_version(Monitor *mon, QObject **ret_data)
348 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
351 static void do_info_name(Monitor *mon)
353 if (qemu_name)
354 monitor_printf(mon, "%s\n", qemu_name);
357 #if defined(TARGET_I386)
358 static void do_info_hpet(Monitor *mon)
360 monitor_printf(mon, "HPET is %s by QEMU\n",
361 (no_hpet) ? "disabled" : "enabled");
363 #endif
365 static void do_info_uuid(Monitor *mon)
367 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
368 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
369 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
370 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
371 qemu_uuid[14], qemu_uuid[15]);
374 /* get the current CPU defined by the user */
375 static int mon_set_cpu(int cpu_index)
377 CPUState *env;
379 for(env = first_cpu; env != NULL; env = env->next_cpu) {
380 if (env->cpu_index == cpu_index) {
381 cur_mon->mon_cpu = env;
382 return 0;
385 return -1;
388 static CPUState *mon_get_cpu(void)
390 if (!cur_mon->mon_cpu) {
391 mon_set_cpu(0);
393 cpu_synchronize_state(cur_mon->mon_cpu);
394 return cur_mon->mon_cpu;
397 static void do_info_registers(Monitor *mon)
399 CPUState *env;
400 env = mon_get_cpu();
401 if (!env)
402 return;
403 #ifdef TARGET_I386
404 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
405 X86_DUMP_FPU);
406 #else
407 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
409 #endif
412 static void do_info_cpus(Monitor *mon)
414 CPUState *env;
416 /* just to set the default cpu if not already done */
417 mon_get_cpu();
419 for(env = first_cpu; env != NULL; env = env->next_cpu) {
420 cpu_synchronize_state(env);
421 monitor_printf(mon, "%c CPU #%d:",
422 (env == mon->mon_cpu) ? '*' : ' ',
423 env->cpu_index);
424 #if defined(TARGET_I386)
425 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
426 env->eip + env->segs[R_CS].base);
427 #elif defined(TARGET_PPC)
428 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
429 #elif defined(TARGET_SPARC)
430 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
431 env->pc, env->npc);
432 #elif defined(TARGET_MIPS)
433 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
434 #endif
435 if (env->halted)
436 monitor_printf(mon, " (halted)");
437 monitor_printf(mon, "\n");
441 static void do_cpu_set(Monitor *mon, const QDict *qdict)
443 int index = qdict_get_int(qdict, "index");
444 if (mon_set_cpu(index) < 0)
445 monitor_printf(mon, "Invalid CPU index\n");
448 static void do_info_jit(Monitor *mon)
450 dump_exec_info((FILE *)mon, monitor_fprintf);
453 static void do_info_history(Monitor *mon)
455 int i;
456 const char *str;
458 if (!mon->rs)
459 return;
460 i = 0;
461 for(;;) {
462 str = readline_get_history(mon->rs, i);
463 if (!str)
464 break;
465 monitor_printf(mon, "%d: '%s'\n", i, str);
466 i++;
470 #if defined(TARGET_PPC)
471 /* XXX: not implemented in other targets */
472 static void do_info_cpu_stats(Monitor *mon)
474 CPUState *env;
476 env = mon_get_cpu();
477 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
479 #endif
482 * do_quit(): Quit QEMU execution
484 static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
486 exit(0);
489 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
491 if (bdrv_is_inserted(bs)) {
492 if (!force) {
493 if (!bdrv_is_removable(bs)) {
494 monitor_printf(mon, "device is not removable\n");
495 return -1;
497 if (bdrv_is_locked(bs)) {
498 monitor_printf(mon, "device is locked\n");
499 return -1;
502 bdrv_close(bs);
504 return 0;
507 static void do_eject(Monitor *mon, const QDict *qdict)
509 BlockDriverState *bs;
510 int force = qdict_get_int(qdict, "force");
511 const char *filename = qdict_get_str(qdict, "filename");
513 bs = bdrv_find(filename);
514 if (!bs) {
515 monitor_printf(mon, "device not found\n");
516 return;
518 eject_device(mon, bs, force);
521 static void do_change_block(Monitor *mon, const char *device,
522 const char *filename, const char *fmt)
524 BlockDriverState *bs;
525 BlockDriver *drv = NULL;
527 bs = bdrv_find(device);
528 if (!bs) {
529 monitor_printf(mon, "device not found\n");
530 return;
532 if (fmt) {
533 drv = bdrv_find_format(fmt);
534 if (!drv) {
535 monitor_printf(mon, "invalid format %s\n", fmt);
536 return;
539 if (eject_device(mon, bs, 0) < 0)
540 return;
541 bdrv_open2(bs, filename, 0, drv);
542 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
545 static void change_vnc_password_cb(Monitor *mon, const char *password,
546 void *opaque)
548 if (vnc_display_password(NULL, password) < 0)
549 monitor_printf(mon, "could not set VNC server password\n");
551 monitor_read_command(mon, 1);
554 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
556 if (strcmp(target, "passwd") == 0 ||
557 strcmp(target, "password") == 0) {
558 if (arg) {
559 char password[9];
560 strncpy(password, arg, sizeof(password));
561 password[sizeof(password) - 1] = '\0';
562 change_vnc_password_cb(mon, password, NULL);
563 } else {
564 monitor_read_password(mon, change_vnc_password_cb, NULL);
566 } else {
567 if (vnc_display_open(NULL, target) < 0)
568 monitor_printf(mon, "could not start VNC server on %s\n", target);
572 static void do_change(Monitor *mon, const QDict *qdict)
574 const char *device = qdict_get_str(qdict, "device");
575 const char *target = qdict_get_str(qdict, "target");
576 const char *arg = qdict_get_try_str(qdict, "arg");
577 if (strcmp(device, "vnc") == 0) {
578 do_change_vnc(mon, target, arg);
579 } else {
580 do_change_block(mon, device, target, arg);
584 static void do_screen_dump(Monitor *mon, const QDict *qdict)
586 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
589 static void do_logfile(Monitor *mon, const QDict *qdict)
591 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
594 static void do_log(Monitor *mon, const QDict *qdict)
596 int mask;
597 const char *items = qdict_get_str(qdict, "items");
599 if (!strcmp(items, "none")) {
600 mask = 0;
601 } else {
602 mask = cpu_str_to_log_mask(items);
603 if (!mask) {
604 help_cmd(mon, "log");
605 return;
608 cpu_set_log(mask);
611 static void do_singlestep(Monitor *mon, const QDict *qdict)
613 const char *option = qdict_get_try_str(qdict, "option");
614 if (!option || !strcmp(option, "on")) {
615 singlestep = 1;
616 } else if (!strcmp(option, "off")) {
617 singlestep = 0;
618 } else {
619 monitor_printf(mon, "unexpected option %s\n", option);
624 * do_stop(): Stop VM execution
626 static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
628 vm_stop(EXCP_INTERRUPT);
631 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
633 struct bdrv_iterate_context {
634 Monitor *mon;
635 int err;
639 * do_cont(): Resume emulation.
641 static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
643 struct bdrv_iterate_context context = { mon, 0 };
645 bdrv_iterate(encrypted_bdrv_it, &context);
646 /* only resume the vm if all keys are set and valid */
647 if (!context.err)
648 vm_start();
651 static void bdrv_key_cb(void *opaque, int err)
653 Monitor *mon = opaque;
655 /* another key was set successfully, retry to continue */
656 if (!err)
657 do_cont(mon, NULL, NULL);
660 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
662 struct bdrv_iterate_context *context = opaque;
664 if (!context->err && bdrv_key_required(bs)) {
665 context->err = -EBUSY;
666 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
667 context->mon);
671 static void do_gdbserver(Monitor *mon, const QDict *qdict)
673 const char *device = qdict_get_try_str(qdict, "device");
674 if (!device)
675 device = "tcp::" DEFAULT_GDBSTUB_PORT;
676 if (gdbserver_start(device) < 0) {
677 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
678 device);
679 } else if (strcmp(device, "none") == 0) {
680 monitor_printf(mon, "Disabled gdbserver\n");
681 } else {
682 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
683 device);
687 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
689 const char *action = qdict_get_str(qdict, "action");
690 if (select_watchdog_action(action) == -1) {
691 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
695 static void monitor_printc(Monitor *mon, int c)
697 monitor_printf(mon, "'");
698 switch(c) {
699 case '\'':
700 monitor_printf(mon, "\\'");
701 break;
702 case '\\':
703 monitor_printf(mon, "\\\\");
704 break;
705 case '\n':
706 monitor_printf(mon, "\\n");
707 break;
708 case '\r':
709 monitor_printf(mon, "\\r");
710 break;
711 default:
712 if (c >= 32 && c <= 126) {
713 monitor_printf(mon, "%c", c);
714 } else {
715 monitor_printf(mon, "\\x%02x", c);
717 break;
719 monitor_printf(mon, "'");
722 static void memory_dump(Monitor *mon, int count, int format, int wsize,
723 target_phys_addr_t addr, int is_physical)
725 CPUState *env;
726 int nb_per_line, l, line_size, i, max_digits, len;
727 uint8_t buf[16];
728 uint64_t v;
730 if (format == 'i') {
731 int flags;
732 flags = 0;
733 env = mon_get_cpu();
734 if (!env && !is_physical)
735 return;
736 #ifdef TARGET_I386
737 if (wsize == 2) {
738 flags = 1;
739 } else if (wsize == 4) {
740 flags = 0;
741 } else {
742 /* as default we use the current CS size */
743 flags = 0;
744 if (env) {
745 #ifdef TARGET_X86_64
746 if ((env->efer & MSR_EFER_LMA) &&
747 (env->segs[R_CS].flags & DESC_L_MASK))
748 flags = 2;
749 else
750 #endif
751 if (!(env->segs[R_CS].flags & DESC_B_MASK))
752 flags = 1;
755 #endif
756 monitor_disas(mon, env, addr, count, is_physical, flags);
757 return;
760 len = wsize * count;
761 if (wsize == 1)
762 line_size = 8;
763 else
764 line_size = 16;
765 nb_per_line = line_size / wsize;
766 max_digits = 0;
768 switch(format) {
769 case 'o':
770 max_digits = (wsize * 8 + 2) / 3;
771 break;
772 default:
773 case 'x':
774 max_digits = (wsize * 8) / 4;
775 break;
776 case 'u':
777 case 'd':
778 max_digits = (wsize * 8 * 10 + 32) / 33;
779 break;
780 case 'c':
781 wsize = 1;
782 break;
785 while (len > 0) {
786 if (is_physical)
787 monitor_printf(mon, TARGET_FMT_plx ":", addr);
788 else
789 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
790 l = len;
791 if (l > line_size)
792 l = line_size;
793 if (is_physical) {
794 cpu_physical_memory_rw(addr, buf, l, 0);
795 } else {
796 env = mon_get_cpu();
797 if (!env)
798 break;
799 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
800 monitor_printf(mon, " Cannot access memory\n");
801 break;
804 i = 0;
805 while (i < l) {
806 switch(wsize) {
807 default:
808 case 1:
809 v = ldub_raw(buf + i);
810 break;
811 case 2:
812 v = lduw_raw(buf + i);
813 break;
814 case 4:
815 v = (uint32_t)ldl_raw(buf + i);
816 break;
817 case 8:
818 v = ldq_raw(buf + i);
819 break;
821 monitor_printf(mon, " ");
822 switch(format) {
823 case 'o':
824 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
825 break;
826 case 'x':
827 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
828 break;
829 case 'u':
830 monitor_printf(mon, "%*" PRIu64, max_digits, v);
831 break;
832 case 'd':
833 monitor_printf(mon, "%*" PRId64, max_digits, v);
834 break;
835 case 'c':
836 monitor_printc(mon, v);
837 break;
839 i += wsize;
841 monitor_printf(mon, "\n");
842 addr += l;
843 len -= l;
847 static void do_memory_dump(Monitor *mon, const QDict *qdict)
849 int count = qdict_get_int(qdict, "count");
850 int format = qdict_get_int(qdict, "format");
851 int size = qdict_get_int(qdict, "size");
852 target_long addr = qdict_get_int(qdict, "addr");
854 memory_dump(mon, count, format, size, addr, 0);
857 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
859 int count = qdict_get_int(qdict, "count");
860 int format = qdict_get_int(qdict, "format");
861 int size = qdict_get_int(qdict, "size");
862 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
864 memory_dump(mon, count, format, size, addr, 1);
867 static void do_print(Monitor *mon, const QDict *qdict)
869 int format = qdict_get_int(qdict, "format");
870 target_phys_addr_t val = qdict_get_int(qdict, "val");
872 #if TARGET_PHYS_ADDR_BITS == 32
873 switch(format) {
874 case 'o':
875 monitor_printf(mon, "%#o", val);
876 break;
877 case 'x':
878 monitor_printf(mon, "%#x", val);
879 break;
880 case 'u':
881 monitor_printf(mon, "%u", val);
882 break;
883 default:
884 case 'd':
885 monitor_printf(mon, "%d", val);
886 break;
887 case 'c':
888 monitor_printc(mon, val);
889 break;
891 #else
892 switch(format) {
893 case 'o':
894 monitor_printf(mon, "%#" PRIo64, val);
895 break;
896 case 'x':
897 monitor_printf(mon, "%#" PRIx64, val);
898 break;
899 case 'u':
900 monitor_printf(mon, "%" PRIu64, val);
901 break;
902 default:
903 case 'd':
904 monitor_printf(mon, "%" PRId64, val);
905 break;
906 case 'c':
907 monitor_printc(mon, val);
908 break;
910 #endif
911 monitor_printf(mon, "\n");
914 static void do_memory_save(Monitor *mon, const QDict *qdict)
916 FILE *f;
917 uint32_t size = qdict_get_int(qdict, "size");
918 const char *filename = qdict_get_str(qdict, "filename");
919 target_long addr = qdict_get_int(qdict, "val");
920 uint32_t l;
921 CPUState *env;
922 uint8_t buf[1024];
924 env = mon_get_cpu();
925 if (!env)
926 return;
928 f = fopen(filename, "wb");
929 if (!f) {
930 monitor_printf(mon, "could not open '%s'\n", filename);
931 return;
933 while (size != 0) {
934 l = sizeof(buf);
935 if (l > size)
936 l = size;
937 cpu_memory_rw_debug(env, addr, buf, l, 0);
938 fwrite(buf, 1, l, f);
939 addr += l;
940 size -= l;
942 fclose(f);
945 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
947 FILE *f;
948 uint32_t l;
949 uint8_t buf[1024];
950 uint32_t size = qdict_get_int(qdict, "size");
951 const char *filename = qdict_get_str(qdict, "filename");
952 target_phys_addr_t addr = qdict_get_int(qdict, "val");
954 f = fopen(filename, "wb");
955 if (!f) {
956 monitor_printf(mon, "could not open '%s'\n", filename);
957 return;
959 while (size != 0) {
960 l = sizeof(buf);
961 if (l > size)
962 l = size;
963 cpu_physical_memory_rw(addr, buf, l, 0);
964 fwrite(buf, 1, l, f);
965 fflush(f);
966 addr += l;
967 size -= l;
969 fclose(f);
972 static void do_sum(Monitor *mon, const QDict *qdict)
974 uint32_t addr;
975 uint8_t buf[1];
976 uint16_t sum;
977 uint32_t start = qdict_get_int(qdict, "start");
978 uint32_t size = qdict_get_int(qdict, "size");
980 sum = 0;
981 for(addr = start; addr < (start + size); addr++) {
982 cpu_physical_memory_rw(addr, buf, 1, 0);
983 /* BSD sum algorithm ('sum' Unix command) */
984 sum = (sum >> 1) | (sum << 15);
985 sum += buf[0];
987 monitor_printf(mon, "%05d\n", sum);
990 typedef struct {
991 int keycode;
992 const char *name;
993 } KeyDef;
995 static const KeyDef key_defs[] = {
996 { 0x2a, "shift" },
997 { 0x36, "shift_r" },
999 { 0x38, "alt" },
1000 { 0xb8, "alt_r" },
1001 { 0x64, "altgr" },
1002 { 0xe4, "altgr_r" },
1003 { 0x1d, "ctrl" },
1004 { 0x9d, "ctrl_r" },
1006 { 0xdd, "menu" },
1008 { 0x01, "esc" },
1010 { 0x02, "1" },
1011 { 0x03, "2" },
1012 { 0x04, "3" },
1013 { 0x05, "4" },
1014 { 0x06, "5" },
1015 { 0x07, "6" },
1016 { 0x08, "7" },
1017 { 0x09, "8" },
1018 { 0x0a, "9" },
1019 { 0x0b, "0" },
1020 { 0x0c, "minus" },
1021 { 0x0d, "equal" },
1022 { 0x0e, "backspace" },
1024 { 0x0f, "tab" },
1025 { 0x10, "q" },
1026 { 0x11, "w" },
1027 { 0x12, "e" },
1028 { 0x13, "r" },
1029 { 0x14, "t" },
1030 { 0x15, "y" },
1031 { 0x16, "u" },
1032 { 0x17, "i" },
1033 { 0x18, "o" },
1034 { 0x19, "p" },
1036 { 0x1c, "ret" },
1038 { 0x1e, "a" },
1039 { 0x1f, "s" },
1040 { 0x20, "d" },
1041 { 0x21, "f" },
1042 { 0x22, "g" },
1043 { 0x23, "h" },
1044 { 0x24, "j" },
1045 { 0x25, "k" },
1046 { 0x26, "l" },
1048 { 0x2c, "z" },
1049 { 0x2d, "x" },
1050 { 0x2e, "c" },
1051 { 0x2f, "v" },
1052 { 0x30, "b" },
1053 { 0x31, "n" },
1054 { 0x32, "m" },
1055 { 0x33, "comma" },
1056 { 0x34, "dot" },
1057 { 0x35, "slash" },
1059 { 0x37, "asterisk" },
1061 { 0x39, "spc" },
1062 { 0x3a, "caps_lock" },
1063 { 0x3b, "f1" },
1064 { 0x3c, "f2" },
1065 { 0x3d, "f3" },
1066 { 0x3e, "f4" },
1067 { 0x3f, "f5" },
1068 { 0x40, "f6" },
1069 { 0x41, "f7" },
1070 { 0x42, "f8" },
1071 { 0x43, "f9" },
1072 { 0x44, "f10" },
1073 { 0x45, "num_lock" },
1074 { 0x46, "scroll_lock" },
1076 { 0xb5, "kp_divide" },
1077 { 0x37, "kp_multiply" },
1078 { 0x4a, "kp_subtract" },
1079 { 0x4e, "kp_add" },
1080 { 0x9c, "kp_enter" },
1081 { 0x53, "kp_decimal" },
1082 { 0x54, "sysrq" },
1084 { 0x52, "kp_0" },
1085 { 0x4f, "kp_1" },
1086 { 0x50, "kp_2" },
1087 { 0x51, "kp_3" },
1088 { 0x4b, "kp_4" },
1089 { 0x4c, "kp_5" },
1090 { 0x4d, "kp_6" },
1091 { 0x47, "kp_7" },
1092 { 0x48, "kp_8" },
1093 { 0x49, "kp_9" },
1095 { 0x56, "<" },
1097 { 0x57, "f11" },
1098 { 0x58, "f12" },
1100 { 0xb7, "print" },
1102 { 0xc7, "home" },
1103 { 0xc9, "pgup" },
1104 { 0xd1, "pgdn" },
1105 { 0xcf, "end" },
1107 { 0xcb, "left" },
1108 { 0xc8, "up" },
1109 { 0xd0, "down" },
1110 { 0xcd, "right" },
1112 { 0xd2, "insert" },
1113 { 0xd3, "delete" },
1114 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1115 { 0xf0, "stop" },
1116 { 0xf1, "again" },
1117 { 0xf2, "props" },
1118 { 0xf3, "undo" },
1119 { 0xf4, "front" },
1120 { 0xf5, "copy" },
1121 { 0xf6, "open" },
1122 { 0xf7, "paste" },
1123 { 0xf8, "find" },
1124 { 0xf9, "cut" },
1125 { 0xfa, "lf" },
1126 { 0xfb, "help" },
1127 { 0xfc, "meta_l" },
1128 { 0xfd, "meta_r" },
1129 { 0xfe, "compose" },
1130 #endif
1131 { 0, NULL },
1134 static int get_keycode(const char *key)
1136 const KeyDef *p;
1137 char *endp;
1138 int ret;
1140 for(p = key_defs; p->name != NULL; p++) {
1141 if (!strcmp(key, p->name))
1142 return p->keycode;
1144 if (strstart(key, "0x", NULL)) {
1145 ret = strtoul(key, &endp, 0);
1146 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1147 return ret;
1149 return -1;
1152 #define MAX_KEYCODES 16
1153 static uint8_t keycodes[MAX_KEYCODES];
1154 static int nb_pending_keycodes;
1155 static QEMUTimer *key_timer;
1157 static void release_keys(void *opaque)
1159 int keycode;
1161 while (nb_pending_keycodes > 0) {
1162 nb_pending_keycodes--;
1163 keycode = keycodes[nb_pending_keycodes];
1164 if (keycode & 0x80)
1165 kbd_put_keycode(0xe0);
1166 kbd_put_keycode(keycode | 0x80);
1170 static void do_sendkey(Monitor *mon, const QDict *qdict)
1172 char keyname_buf[16];
1173 char *separator;
1174 int keyname_len, keycode, i;
1175 const char *string = qdict_get_str(qdict, "string");
1176 int has_hold_time = qdict_haskey(qdict, "hold_time");
1177 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1179 if (nb_pending_keycodes > 0) {
1180 qemu_del_timer(key_timer);
1181 release_keys(NULL);
1183 if (!has_hold_time)
1184 hold_time = 100;
1185 i = 0;
1186 while (1) {
1187 separator = strchr(string, '-');
1188 keyname_len = separator ? separator - string : strlen(string);
1189 if (keyname_len > 0) {
1190 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1191 if (keyname_len > sizeof(keyname_buf) - 1) {
1192 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1193 return;
1195 if (i == MAX_KEYCODES) {
1196 monitor_printf(mon, "too many keys\n");
1197 return;
1199 keyname_buf[keyname_len] = 0;
1200 keycode = get_keycode(keyname_buf);
1201 if (keycode < 0) {
1202 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1203 return;
1205 keycodes[i++] = keycode;
1207 if (!separator)
1208 break;
1209 string = separator + 1;
1211 nb_pending_keycodes = i;
1212 /* key down events */
1213 for (i = 0; i < nb_pending_keycodes; i++) {
1214 keycode = keycodes[i];
1215 if (keycode & 0x80)
1216 kbd_put_keycode(0xe0);
1217 kbd_put_keycode(keycode & 0x7f);
1219 /* delayed key up events */
1220 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1221 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1224 static int mouse_button_state;
1226 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1228 int dx, dy, dz;
1229 const char *dx_str = qdict_get_str(qdict, "dx_str");
1230 const char *dy_str = qdict_get_str(qdict, "dy_str");
1231 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1232 dx = strtol(dx_str, NULL, 0);
1233 dy = strtol(dy_str, NULL, 0);
1234 dz = 0;
1235 if (dz_str)
1236 dz = strtol(dz_str, NULL, 0);
1237 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1240 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1242 int button_state = qdict_get_int(qdict, "button_state");
1243 mouse_button_state = button_state;
1244 kbd_mouse_event(0, 0, 0, mouse_button_state);
1247 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1249 int size = qdict_get_int(qdict, "size");
1250 int addr = qdict_get_int(qdict, "addr");
1251 int has_index = qdict_haskey(qdict, "index");
1252 uint32_t val;
1253 int suffix;
1255 if (has_index) {
1256 int index = qdict_get_int(qdict, "index");
1257 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1258 addr++;
1260 addr &= 0xffff;
1262 switch(size) {
1263 default:
1264 case 1:
1265 val = cpu_inb(addr);
1266 suffix = 'b';
1267 break;
1268 case 2:
1269 val = cpu_inw(addr);
1270 suffix = 'w';
1271 break;
1272 case 4:
1273 val = cpu_inl(addr);
1274 suffix = 'l';
1275 break;
1277 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1278 suffix, addr, size * 2, val);
1281 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1283 int size = qdict_get_int(qdict, "size");
1284 int addr = qdict_get_int(qdict, "addr");
1285 int val = qdict_get_int(qdict, "val");
1287 addr &= IOPORTS_MASK;
1289 switch (size) {
1290 default:
1291 case 1:
1292 cpu_outb(addr, val);
1293 break;
1294 case 2:
1295 cpu_outw(addr, val);
1296 break;
1297 case 4:
1298 cpu_outl(addr, val);
1299 break;
1303 static void do_boot_set(Monitor *mon, const QDict *qdict)
1305 int res;
1306 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1308 res = qemu_boot_set(bootdevice);
1309 if (res == 0) {
1310 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1311 } else if (res > 0) {
1312 monitor_printf(mon, "setting boot device list failed\n");
1313 } else {
1314 monitor_printf(mon, "no function defined to set boot device list for "
1315 "this architecture\n");
1320 * do_system_reset(): Issue a machine reset
1322 static void do_system_reset(Monitor *mon, const QDict *qdict,
1323 QObject **ret_data)
1325 qemu_system_reset_request();
1329 * do_system_powerdown(): Issue a machine powerdown
1331 static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1332 QObject **ret_data)
1334 qemu_system_powerdown_request();
1337 #if defined(TARGET_I386)
1338 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1340 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1341 addr,
1342 pte & mask,
1343 pte & PG_GLOBAL_MASK ? 'G' : '-',
1344 pte & PG_PSE_MASK ? 'P' : '-',
1345 pte & PG_DIRTY_MASK ? 'D' : '-',
1346 pte & PG_ACCESSED_MASK ? 'A' : '-',
1347 pte & PG_PCD_MASK ? 'C' : '-',
1348 pte & PG_PWT_MASK ? 'T' : '-',
1349 pte & PG_USER_MASK ? 'U' : '-',
1350 pte & PG_RW_MASK ? 'W' : '-');
1353 static void tlb_info(Monitor *mon)
1355 CPUState *env;
1356 int l1, l2;
1357 uint32_t pgd, pde, pte;
1359 env = mon_get_cpu();
1360 if (!env)
1361 return;
1363 if (!(env->cr[0] & CR0_PG_MASK)) {
1364 monitor_printf(mon, "PG disabled\n");
1365 return;
1367 pgd = env->cr[3] & ~0xfff;
1368 for(l1 = 0; l1 < 1024; l1++) {
1369 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1370 pde = le32_to_cpu(pde);
1371 if (pde & PG_PRESENT_MASK) {
1372 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1373 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1374 } else {
1375 for(l2 = 0; l2 < 1024; l2++) {
1376 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1377 (uint8_t *)&pte, 4);
1378 pte = le32_to_cpu(pte);
1379 if (pte & PG_PRESENT_MASK) {
1380 print_pte(mon, (l1 << 22) + (l2 << 12),
1381 pte & ~PG_PSE_MASK,
1382 ~0xfff);
1390 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1391 uint32_t end, int prot)
1393 int prot1;
1394 prot1 = *plast_prot;
1395 if (prot != prot1) {
1396 if (*pstart != -1) {
1397 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1398 *pstart, end, end - *pstart,
1399 prot1 & PG_USER_MASK ? 'u' : '-',
1400 'r',
1401 prot1 & PG_RW_MASK ? 'w' : '-');
1403 if (prot != 0)
1404 *pstart = end;
1405 else
1406 *pstart = -1;
1407 *plast_prot = prot;
1411 static void mem_info(Monitor *mon)
1413 CPUState *env;
1414 int l1, l2, prot, last_prot;
1415 uint32_t pgd, pde, pte, start, end;
1417 env = mon_get_cpu();
1418 if (!env)
1419 return;
1421 if (!(env->cr[0] & CR0_PG_MASK)) {
1422 monitor_printf(mon, "PG disabled\n");
1423 return;
1425 pgd = env->cr[3] & ~0xfff;
1426 last_prot = 0;
1427 start = -1;
1428 for(l1 = 0; l1 < 1024; l1++) {
1429 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1430 pde = le32_to_cpu(pde);
1431 end = l1 << 22;
1432 if (pde & PG_PRESENT_MASK) {
1433 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1434 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1435 mem_print(mon, &start, &last_prot, end, prot);
1436 } else {
1437 for(l2 = 0; l2 < 1024; l2++) {
1438 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1439 (uint8_t *)&pte, 4);
1440 pte = le32_to_cpu(pte);
1441 end = (l1 << 22) + (l2 << 12);
1442 if (pte & PG_PRESENT_MASK) {
1443 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1444 } else {
1445 prot = 0;
1447 mem_print(mon, &start, &last_prot, end, prot);
1450 } else {
1451 prot = 0;
1452 mem_print(mon, &start, &last_prot, end, prot);
1456 #endif
1458 #if defined(TARGET_SH4)
1460 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1462 monitor_printf(mon, " tlb%i:\t"
1463 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1464 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1465 "dirty=%hhu writethrough=%hhu\n",
1466 idx,
1467 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1468 tlb->v, tlb->sh, tlb->c, tlb->pr,
1469 tlb->d, tlb->wt);
1472 static void tlb_info(Monitor *mon)
1474 CPUState *env = mon_get_cpu();
1475 int i;
1477 monitor_printf (mon, "ITLB:\n");
1478 for (i = 0 ; i < ITLB_SIZE ; i++)
1479 print_tlb (mon, i, &env->itlb[i]);
1480 monitor_printf (mon, "UTLB:\n");
1481 for (i = 0 ; i < UTLB_SIZE ; i++)
1482 print_tlb (mon, i, &env->utlb[i]);
1485 #endif
1487 static void do_info_kvm(Monitor *mon)
1489 #ifdef CONFIG_KVM
1490 monitor_printf(mon, "kvm support: ");
1491 if (kvm_enabled())
1492 monitor_printf(mon, "enabled\n");
1493 else
1494 monitor_printf(mon, "disabled\n");
1495 #else
1496 monitor_printf(mon, "kvm support: not compiled\n");
1497 #endif
1500 static void do_info_numa(Monitor *mon)
1502 int i;
1503 CPUState *env;
1505 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1506 for (i = 0; i < nb_numa_nodes; i++) {
1507 monitor_printf(mon, "node %d cpus:", i);
1508 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1509 if (env->numa_node == i) {
1510 monitor_printf(mon, " %d", env->cpu_index);
1513 monitor_printf(mon, "\n");
1514 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1515 node_mem[i] >> 20);
1519 #ifdef CONFIG_PROFILER
1521 int64_t qemu_time;
1522 int64_t dev_time;
1524 static void do_info_profile(Monitor *mon)
1526 int64_t total;
1527 total = qemu_time;
1528 if (total == 0)
1529 total = 1;
1530 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1531 dev_time, dev_time / (double)get_ticks_per_sec());
1532 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1533 qemu_time, qemu_time / (double)get_ticks_per_sec());
1534 qemu_time = 0;
1535 dev_time = 0;
1537 #else
1538 static void do_info_profile(Monitor *mon)
1540 monitor_printf(mon, "Internal profiler not compiled\n");
1542 #endif
1544 /* Capture support */
1545 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1547 static void do_info_capture(Monitor *mon)
1549 int i;
1550 CaptureState *s;
1552 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1553 monitor_printf(mon, "[%d]: ", i);
1554 s->ops.info (s->opaque);
1558 #ifdef HAS_AUDIO
1559 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1561 int i;
1562 int n = qdict_get_int(qdict, "n");
1563 CaptureState *s;
1565 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1566 if (i == n) {
1567 s->ops.destroy (s->opaque);
1568 QLIST_REMOVE (s, entries);
1569 qemu_free (s);
1570 return;
1575 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1577 const char *path = qdict_get_str(qdict, "path");
1578 int has_freq = qdict_haskey(qdict, "freq");
1579 int freq = qdict_get_try_int(qdict, "freq", -1);
1580 int has_bits = qdict_haskey(qdict, "bits");
1581 int bits = qdict_get_try_int(qdict, "bits", -1);
1582 int has_channels = qdict_haskey(qdict, "nchannels");
1583 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1584 CaptureState *s;
1586 s = qemu_mallocz (sizeof (*s));
1588 freq = has_freq ? freq : 44100;
1589 bits = has_bits ? bits : 16;
1590 nchannels = has_channels ? nchannels : 2;
1592 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1593 monitor_printf(mon, "Faied to add wave capture\n");
1594 qemu_free (s);
1596 QLIST_INSERT_HEAD (&capture_head, s, entries);
1598 #endif
1600 #if defined(TARGET_I386)
1601 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1603 CPUState *env;
1604 int cpu_index = qdict_get_int(qdict, "cpu_index");
1606 for (env = first_cpu; env != NULL; env = env->next_cpu)
1607 if (env->cpu_index == cpu_index) {
1608 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1609 break;
1612 #endif
1614 static void do_info_status(Monitor *mon)
1616 if (vm_running) {
1617 if (singlestep) {
1618 monitor_printf(mon, "VM status: running (single step mode)\n");
1619 } else {
1620 monitor_printf(mon, "VM status: running\n");
1622 } else
1623 monitor_printf(mon, "VM status: paused\n");
1627 * do_balloon(): Request VM to change its memory allocation
1629 static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
1631 int value = qdict_get_int(qdict, "value");
1632 ram_addr_t target = value;
1633 qemu_balloon(target << 20);
1636 static void do_info_balloon(Monitor *mon)
1638 ram_addr_t actual;
1640 actual = qemu_balloon_status();
1641 if (kvm_enabled() && !kvm_has_sync_mmu())
1642 monitor_printf(mon, "Using KVM without synchronous MMU, "
1643 "ballooning disabled\n");
1644 else if (actual == 0)
1645 monitor_printf(mon, "Ballooning not activated in VM\n");
1646 else
1647 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1650 static qemu_acl *find_acl(Monitor *mon, const char *name)
1652 qemu_acl *acl = qemu_acl_find(name);
1654 if (!acl) {
1655 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1657 return acl;
1660 static void do_acl_show(Monitor *mon, const QDict *qdict)
1662 const char *aclname = qdict_get_str(qdict, "aclname");
1663 qemu_acl *acl = find_acl(mon, aclname);
1664 qemu_acl_entry *entry;
1665 int i = 0;
1667 if (acl) {
1668 monitor_printf(mon, "policy: %s\n",
1669 acl->defaultDeny ? "deny" : "allow");
1670 QTAILQ_FOREACH(entry, &acl->entries, next) {
1671 i++;
1672 monitor_printf(mon, "%d: %s %s\n", i,
1673 entry->deny ? "deny" : "allow", entry->match);
1678 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1680 const char *aclname = qdict_get_str(qdict, "aclname");
1681 qemu_acl *acl = find_acl(mon, aclname);
1683 if (acl) {
1684 qemu_acl_reset(acl);
1685 monitor_printf(mon, "acl: removed all rules\n");
1689 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1691 const char *aclname = qdict_get_str(qdict, "aclname");
1692 const char *policy = qdict_get_str(qdict, "policy");
1693 qemu_acl *acl = find_acl(mon, aclname);
1695 if (acl) {
1696 if (strcmp(policy, "allow") == 0) {
1697 acl->defaultDeny = 0;
1698 monitor_printf(mon, "acl: policy set to 'allow'\n");
1699 } else if (strcmp(policy, "deny") == 0) {
1700 acl->defaultDeny = 1;
1701 monitor_printf(mon, "acl: policy set to 'deny'\n");
1702 } else {
1703 monitor_printf(mon, "acl: unknown policy '%s', "
1704 "expected 'deny' or 'allow'\n", policy);
1709 static void do_acl_add(Monitor *mon, const QDict *qdict)
1711 const char *aclname = qdict_get_str(qdict, "aclname");
1712 const char *match = qdict_get_str(qdict, "match");
1713 const char *policy = qdict_get_str(qdict, "policy");
1714 int has_index = qdict_haskey(qdict, "index");
1715 int index = qdict_get_try_int(qdict, "index", -1);
1716 qemu_acl *acl = find_acl(mon, aclname);
1717 int deny, ret;
1719 if (acl) {
1720 if (strcmp(policy, "allow") == 0) {
1721 deny = 0;
1722 } else if (strcmp(policy, "deny") == 0) {
1723 deny = 1;
1724 } else {
1725 monitor_printf(mon, "acl: unknown policy '%s', "
1726 "expected 'deny' or 'allow'\n", policy);
1727 return;
1729 if (has_index)
1730 ret = qemu_acl_insert(acl, deny, match, index);
1731 else
1732 ret = qemu_acl_append(acl, deny, match);
1733 if (ret < 0)
1734 monitor_printf(mon, "acl: unable to add acl entry\n");
1735 else
1736 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1740 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1742 const char *aclname = qdict_get_str(qdict, "aclname");
1743 const char *match = qdict_get_str(qdict, "match");
1744 qemu_acl *acl = find_acl(mon, aclname);
1745 int ret;
1747 if (acl) {
1748 ret = qemu_acl_remove(acl, match);
1749 if (ret < 0)
1750 monitor_printf(mon, "acl: no matching acl entry\n");
1751 else
1752 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1756 #if defined(TARGET_I386)
1757 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1759 CPUState *cenv;
1760 int cpu_index = qdict_get_int(qdict, "cpu_index");
1761 int bank = qdict_get_int(qdict, "bank");
1762 uint64_t status = qdict_get_int(qdict, "status");
1763 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1764 uint64_t addr = qdict_get_int(qdict, "addr");
1765 uint64_t misc = qdict_get_int(qdict, "misc");
1767 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1768 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1769 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1770 break;
1773 #endif
1775 static void do_getfd(Monitor *mon, const QDict *qdict)
1777 const char *fdname = qdict_get_str(qdict, "fdname");
1778 mon_fd_t *monfd;
1779 int fd;
1781 fd = qemu_chr_get_msgfd(mon->chr);
1782 if (fd == -1) {
1783 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1784 return;
1787 if (qemu_isdigit(fdname[0])) {
1788 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1789 return;
1792 fd = dup(fd);
1793 if (fd == -1) {
1794 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1795 strerror(errno));
1796 return;
1799 QLIST_FOREACH(monfd, &mon->fds, next) {
1800 if (strcmp(monfd->name, fdname) != 0) {
1801 continue;
1804 close(monfd->fd);
1805 monfd->fd = fd;
1806 return;
1809 monfd = qemu_mallocz(sizeof(mon_fd_t));
1810 monfd->name = qemu_strdup(fdname);
1811 monfd->fd = fd;
1813 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1816 static void do_closefd(Monitor *mon, const QDict *qdict)
1818 const char *fdname = qdict_get_str(qdict, "fdname");
1819 mon_fd_t *monfd;
1821 QLIST_FOREACH(monfd, &mon->fds, next) {
1822 if (strcmp(monfd->name, fdname) != 0) {
1823 continue;
1826 QLIST_REMOVE(monfd, next);
1827 close(monfd->fd);
1828 qemu_free(monfd->name);
1829 qemu_free(monfd);
1830 return;
1833 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1834 fdname);
1837 static void do_loadvm(Monitor *mon, const QDict *qdict)
1839 int saved_vm_running = vm_running;
1840 const char *name = qdict_get_str(qdict, "name");
1842 vm_stop(0);
1844 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1845 vm_start();
1848 int monitor_get_fd(Monitor *mon, const char *fdname)
1850 mon_fd_t *monfd;
1852 QLIST_FOREACH(monfd, &mon->fds, next) {
1853 int fd;
1855 if (strcmp(monfd->name, fdname) != 0) {
1856 continue;
1859 fd = monfd->fd;
1861 /* caller takes ownership of fd */
1862 QLIST_REMOVE(monfd, next);
1863 qemu_free(monfd->name);
1864 qemu_free(monfd);
1866 return fd;
1869 return -1;
1872 static const mon_cmd_t mon_cmds[] = {
1873 #include "qemu-monitor.h"
1874 { NULL, NULL, },
1877 /* Please update qemu-monitor.hx when adding or changing commands */
1878 static const mon_cmd_t info_cmds[] = {
1880 .name = "version",
1881 .args_type = "",
1882 .params = "",
1883 .help = "show the version of QEMU",
1884 .user_print = monitor_print_qobject,
1885 .mhandler.info_new = do_info_version,
1888 .name = "network",
1889 .args_type = "",
1890 .params = "",
1891 .help = "show the network state",
1892 .mhandler.info = do_info_network,
1895 .name = "chardev",
1896 .args_type = "",
1897 .params = "",
1898 .help = "show the character devices",
1899 .mhandler.info = qemu_chr_info,
1902 .name = "block",
1903 .args_type = "",
1904 .params = "",
1905 .help = "show the block devices",
1906 .mhandler.info = bdrv_info,
1909 .name = "blockstats",
1910 .args_type = "",
1911 .params = "",
1912 .help = "show block device statistics",
1913 .mhandler.info = bdrv_info_stats,
1916 .name = "registers",
1917 .args_type = "",
1918 .params = "",
1919 .help = "show the cpu registers",
1920 .mhandler.info = do_info_registers,
1923 .name = "cpus",
1924 .args_type = "",
1925 .params = "",
1926 .help = "show infos for each CPU",
1927 .mhandler.info = do_info_cpus,
1930 .name = "history",
1931 .args_type = "",
1932 .params = "",
1933 .help = "show the command line history",
1934 .mhandler.info = do_info_history,
1937 .name = "irq",
1938 .args_type = "",
1939 .params = "",
1940 .help = "show the interrupts statistics (if available)",
1941 .mhandler.info = irq_info,
1944 .name = "pic",
1945 .args_type = "",
1946 .params = "",
1947 .help = "show i8259 (PIC) state",
1948 .mhandler.info = pic_info,
1951 .name = "pci",
1952 .args_type = "",
1953 .params = "",
1954 .help = "show PCI info",
1955 .mhandler.info = pci_info,
1957 #if defined(TARGET_I386) || defined(TARGET_SH4)
1959 .name = "tlb",
1960 .args_type = "",
1961 .params = "",
1962 .help = "show virtual to physical memory mappings",
1963 .mhandler.info = tlb_info,
1965 #endif
1966 #if defined(TARGET_I386)
1968 .name = "mem",
1969 .args_type = "",
1970 .params = "",
1971 .help = "show the active virtual memory mappings",
1972 .mhandler.info = mem_info,
1975 .name = "hpet",
1976 .args_type = "",
1977 .params = "",
1978 .help = "show state of HPET",
1979 .mhandler.info = do_info_hpet,
1981 #endif
1983 .name = "jit",
1984 .args_type = "",
1985 .params = "",
1986 .help = "show dynamic compiler info",
1987 .mhandler.info = do_info_jit,
1990 .name = "kvm",
1991 .args_type = "",
1992 .params = "",
1993 .help = "show KVM information",
1994 .mhandler.info = do_info_kvm,
1997 .name = "numa",
1998 .args_type = "",
1999 .params = "",
2000 .help = "show NUMA information",
2001 .mhandler.info = do_info_numa,
2004 .name = "usb",
2005 .args_type = "",
2006 .params = "",
2007 .help = "show guest USB devices",
2008 .mhandler.info = usb_info,
2011 .name = "usbhost",
2012 .args_type = "",
2013 .params = "",
2014 .help = "show host USB devices",
2015 .mhandler.info = usb_host_info,
2018 .name = "profile",
2019 .args_type = "",
2020 .params = "",
2021 .help = "show profiling information",
2022 .mhandler.info = do_info_profile,
2025 .name = "capture",
2026 .args_type = "",
2027 .params = "",
2028 .help = "show capture information",
2029 .mhandler.info = do_info_capture,
2032 .name = "snapshots",
2033 .args_type = "",
2034 .params = "",
2035 .help = "show the currently saved VM snapshots",
2036 .mhandler.info = do_info_snapshots,
2039 .name = "status",
2040 .args_type = "",
2041 .params = "",
2042 .help = "show the current VM status (running|paused)",
2043 .mhandler.info = do_info_status,
2046 .name = "pcmcia",
2047 .args_type = "",
2048 .params = "",
2049 .help = "show guest PCMCIA status",
2050 .mhandler.info = pcmcia_info,
2053 .name = "mice",
2054 .args_type = "",
2055 .params = "",
2056 .help = "show which guest mouse is receiving events",
2057 .mhandler.info = do_info_mice,
2060 .name = "vnc",
2061 .args_type = "",
2062 .params = "",
2063 .help = "show the vnc server status",
2064 .mhandler.info = do_info_vnc,
2067 .name = "name",
2068 .args_type = "",
2069 .params = "",
2070 .help = "show the current VM name",
2071 .mhandler.info = do_info_name,
2074 .name = "uuid",
2075 .args_type = "",
2076 .params = "",
2077 .help = "show the current VM UUID",
2078 .mhandler.info = do_info_uuid,
2080 #if defined(TARGET_PPC)
2082 .name = "cpustats",
2083 .args_type = "",
2084 .params = "",
2085 .help = "show CPU statistics",
2086 .mhandler.info = do_info_cpu_stats,
2088 #endif
2089 #if defined(CONFIG_SLIRP)
2091 .name = "usernet",
2092 .args_type = "",
2093 .params = "",
2094 .help = "show user network stack connection states",
2095 .mhandler.info = do_info_usernet,
2097 #endif
2099 .name = "migrate",
2100 .args_type = "",
2101 .params = "",
2102 .help = "show migration status",
2103 .mhandler.info = do_info_migrate,
2106 .name = "balloon",
2107 .args_type = "",
2108 .params = "",
2109 .help = "show balloon information",
2110 .mhandler.info = do_info_balloon,
2113 .name = "qtree",
2114 .args_type = "",
2115 .params = "",
2116 .help = "show device tree",
2117 .mhandler.info = do_info_qtree,
2120 .name = "qdm",
2121 .args_type = "",
2122 .params = "",
2123 .help = "show qdev device model list",
2124 .mhandler.info = do_info_qdm,
2127 .name = "roms",
2128 .args_type = "",
2129 .params = "",
2130 .help = "show roms",
2131 .mhandler.info = do_info_roms,
2134 .name = NULL,
2138 /*******************************************************************/
2140 static const char *pch;
2141 static jmp_buf expr_env;
2143 #define MD_TLONG 0
2144 #define MD_I32 1
2146 typedef struct MonitorDef {
2147 const char *name;
2148 int offset;
2149 target_long (*get_value)(const struct MonitorDef *md, int val);
2150 int type;
2151 } MonitorDef;
2153 #if defined(TARGET_I386)
2154 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2156 CPUState *env = mon_get_cpu();
2157 if (!env)
2158 return 0;
2159 return env->eip + env->segs[R_CS].base;
2161 #endif
2163 #if defined(TARGET_PPC)
2164 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2166 CPUState *env = mon_get_cpu();
2167 unsigned int u;
2168 int i;
2170 if (!env)
2171 return 0;
2173 u = 0;
2174 for (i = 0; i < 8; i++)
2175 u |= env->crf[i] << (32 - (4 * i));
2177 return u;
2180 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2182 CPUState *env = mon_get_cpu();
2183 if (!env)
2184 return 0;
2185 return env->msr;
2188 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2190 CPUState *env = mon_get_cpu();
2191 if (!env)
2192 return 0;
2193 return env->xer;
2196 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2198 CPUState *env = mon_get_cpu();
2199 if (!env)
2200 return 0;
2201 return cpu_ppc_load_decr(env);
2204 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2206 CPUState *env = mon_get_cpu();
2207 if (!env)
2208 return 0;
2209 return cpu_ppc_load_tbu(env);
2212 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2214 CPUState *env = mon_get_cpu();
2215 if (!env)
2216 return 0;
2217 return cpu_ppc_load_tbl(env);
2219 #endif
2221 #if defined(TARGET_SPARC)
2222 #ifndef TARGET_SPARC64
2223 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2225 CPUState *env = mon_get_cpu();
2226 if (!env)
2227 return 0;
2228 return GET_PSR(env);
2230 #endif
2232 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2234 CPUState *env = mon_get_cpu();
2235 if (!env)
2236 return 0;
2237 return env->regwptr[val];
2239 #endif
2241 static const MonitorDef monitor_defs[] = {
2242 #ifdef TARGET_I386
2244 #define SEG(name, seg) \
2245 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2246 { name ".base", offsetof(CPUState, segs[seg].base) },\
2247 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2249 { "eax", offsetof(CPUState, regs[0]) },
2250 { "ecx", offsetof(CPUState, regs[1]) },
2251 { "edx", offsetof(CPUState, regs[2]) },
2252 { "ebx", offsetof(CPUState, regs[3]) },
2253 { "esp|sp", offsetof(CPUState, regs[4]) },
2254 { "ebp|fp", offsetof(CPUState, regs[5]) },
2255 { "esi", offsetof(CPUState, regs[6]) },
2256 { "edi", offsetof(CPUState, regs[7]) },
2257 #ifdef TARGET_X86_64
2258 { "r8", offsetof(CPUState, regs[8]) },
2259 { "r9", offsetof(CPUState, regs[9]) },
2260 { "r10", offsetof(CPUState, regs[10]) },
2261 { "r11", offsetof(CPUState, regs[11]) },
2262 { "r12", offsetof(CPUState, regs[12]) },
2263 { "r13", offsetof(CPUState, regs[13]) },
2264 { "r14", offsetof(CPUState, regs[14]) },
2265 { "r15", offsetof(CPUState, regs[15]) },
2266 #endif
2267 { "eflags", offsetof(CPUState, eflags) },
2268 { "eip", offsetof(CPUState, eip) },
2269 SEG("cs", R_CS)
2270 SEG("ds", R_DS)
2271 SEG("es", R_ES)
2272 SEG("ss", R_SS)
2273 SEG("fs", R_FS)
2274 SEG("gs", R_GS)
2275 { "pc", 0, monitor_get_pc, },
2276 #elif defined(TARGET_PPC)
2277 /* General purpose registers */
2278 { "r0", offsetof(CPUState, gpr[0]) },
2279 { "r1", offsetof(CPUState, gpr[1]) },
2280 { "r2", offsetof(CPUState, gpr[2]) },
2281 { "r3", offsetof(CPUState, gpr[3]) },
2282 { "r4", offsetof(CPUState, gpr[4]) },
2283 { "r5", offsetof(CPUState, gpr[5]) },
2284 { "r6", offsetof(CPUState, gpr[6]) },
2285 { "r7", offsetof(CPUState, gpr[7]) },
2286 { "r8", offsetof(CPUState, gpr[8]) },
2287 { "r9", offsetof(CPUState, gpr[9]) },
2288 { "r10", offsetof(CPUState, gpr[10]) },
2289 { "r11", offsetof(CPUState, gpr[11]) },
2290 { "r12", offsetof(CPUState, gpr[12]) },
2291 { "r13", offsetof(CPUState, gpr[13]) },
2292 { "r14", offsetof(CPUState, gpr[14]) },
2293 { "r15", offsetof(CPUState, gpr[15]) },
2294 { "r16", offsetof(CPUState, gpr[16]) },
2295 { "r17", offsetof(CPUState, gpr[17]) },
2296 { "r18", offsetof(CPUState, gpr[18]) },
2297 { "r19", offsetof(CPUState, gpr[19]) },
2298 { "r20", offsetof(CPUState, gpr[20]) },
2299 { "r21", offsetof(CPUState, gpr[21]) },
2300 { "r22", offsetof(CPUState, gpr[22]) },
2301 { "r23", offsetof(CPUState, gpr[23]) },
2302 { "r24", offsetof(CPUState, gpr[24]) },
2303 { "r25", offsetof(CPUState, gpr[25]) },
2304 { "r26", offsetof(CPUState, gpr[26]) },
2305 { "r27", offsetof(CPUState, gpr[27]) },
2306 { "r28", offsetof(CPUState, gpr[28]) },
2307 { "r29", offsetof(CPUState, gpr[29]) },
2308 { "r30", offsetof(CPUState, gpr[30]) },
2309 { "r31", offsetof(CPUState, gpr[31]) },
2310 /* Floating point registers */
2311 { "f0", offsetof(CPUState, fpr[0]) },
2312 { "f1", offsetof(CPUState, fpr[1]) },
2313 { "f2", offsetof(CPUState, fpr[2]) },
2314 { "f3", offsetof(CPUState, fpr[3]) },
2315 { "f4", offsetof(CPUState, fpr[4]) },
2316 { "f5", offsetof(CPUState, fpr[5]) },
2317 { "f6", offsetof(CPUState, fpr[6]) },
2318 { "f7", offsetof(CPUState, fpr[7]) },
2319 { "f8", offsetof(CPUState, fpr[8]) },
2320 { "f9", offsetof(CPUState, fpr[9]) },
2321 { "f10", offsetof(CPUState, fpr[10]) },
2322 { "f11", offsetof(CPUState, fpr[11]) },
2323 { "f12", offsetof(CPUState, fpr[12]) },
2324 { "f13", offsetof(CPUState, fpr[13]) },
2325 { "f14", offsetof(CPUState, fpr[14]) },
2326 { "f15", offsetof(CPUState, fpr[15]) },
2327 { "f16", offsetof(CPUState, fpr[16]) },
2328 { "f17", offsetof(CPUState, fpr[17]) },
2329 { "f18", offsetof(CPUState, fpr[18]) },
2330 { "f19", offsetof(CPUState, fpr[19]) },
2331 { "f20", offsetof(CPUState, fpr[20]) },
2332 { "f21", offsetof(CPUState, fpr[21]) },
2333 { "f22", offsetof(CPUState, fpr[22]) },
2334 { "f23", offsetof(CPUState, fpr[23]) },
2335 { "f24", offsetof(CPUState, fpr[24]) },
2336 { "f25", offsetof(CPUState, fpr[25]) },
2337 { "f26", offsetof(CPUState, fpr[26]) },
2338 { "f27", offsetof(CPUState, fpr[27]) },
2339 { "f28", offsetof(CPUState, fpr[28]) },
2340 { "f29", offsetof(CPUState, fpr[29]) },
2341 { "f30", offsetof(CPUState, fpr[30]) },
2342 { "f31", offsetof(CPUState, fpr[31]) },
2343 { "fpscr", offsetof(CPUState, fpscr) },
2344 /* Next instruction pointer */
2345 { "nip|pc", offsetof(CPUState, nip) },
2346 { "lr", offsetof(CPUState, lr) },
2347 { "ctr", offsetof(CPUState, ctr) },
2348 { "decr", 0, &monitor_get_decr, },
2349 { "ccr", 0, &monitor_get_ccr, },
2350 /* Machine state register */
2351 { "msr", 0, &monitor_get_msr, },
2352 { "xer", 0, &monitor_get_xer, },
2353 { "tbu", 0, &monitor_get_tbu, },
2354 { "tbl", 0, &monitor_get_tbl, },
2355 #if defined(TARGET_PPC64)
2356 /* Address space register */
2357 { "asr", offsetof(CPUState, asr) },
2358 #endif
2359 /* Segment registers */
2360 { "sdr1", offsetof(CPUState, sdr1) },
2361 { "sr0", offsetof(CPUState, sr[0]) },
2362 { "sr1", offsetof(CPUState, sr[1]) },
2363 { "sr2", offsetof(CPUState, sr[2]) },
2364 { "sr3", offsetof(CPUState, sr[3]) },
2365 { "sr4", offsetof(CPUState, sr[4]) },
2366 { "sr5", offsetof(CPUState, sr[5]) },
2367 { "sr6", offsetof(CPUState, sr[6]) },
2368 { "sr7", offsetof(CPUState, sr[7]) },
2369 { "sr8", offsetof(CPUState, sr[8]) },
2370 { "sr9", offsetof(CPUState, sr[9]) },
2371 { "sr10", offsetof(CPUState, sr[10]) },
2372 { "sr11", offsetof(CPUState, sr[11]) },
2373 { "sr12", offsetof(CPUState, sr[12]) },
2374 { "sr13", offsetof(CPUState, sr[13]) },
2375 { "sr14", offsetof(CPUState, sr[14]) },
2376 { "sr15", offsetof(CPUState, sr[15]) },
2377 /* Too lazy to put BATs and SPRs ... */
2378 #elif defined(TARGET_SPARC)
2379 { "g0", offsetof(CPUState, gregs[0]) },
2380 { "g1", offsetof(CPUState, gregs[1]) },
2381 { "g2", offsetof(CPUState, gregs[2]) },
2382 { "g3", offsetof(CPUState, gregs[3]) },
2383 { "g4", offsetof(CPUState, gregs[4]) },
2384 { "g5", offsetof(CPUState, gregs[5]) },
2385 { "g6", offsetof(CPUState, gregs[6]) },
2386 { "g7", offsetof(CPUState, gregs[7]) },
2387 { "o0", 0, monitor_get_reg },
2388 { "o1", 1, monitor_get_reg },
2389 { "o2", 2, monitor_get_reg },
2390 { "o3", 3, monitor_get_reg },
2391 { "o4", 4, monitor_get_reg },
2392 { "o5", 5, monitor_get_reg },
2393 { "o6", 6, monitor_get_reg },
2394 { "o7", 7, monitor_get_reg },
2395 { "l0", 8, monitor_get_reg },
2396 { "l1", 9, monitor_get_reg },
2397 { "l2", 10, monitor_get_reg },
2398 { "l3", 11, monitor_get_reg },
2399 { "l4", 12, monitor_get_reg },
2400 { "l5", 13, monitor_get_reg },
2401 { "l6", 14, monitor_get_reg },
2402 { "l7", 15, monitor_get_reg },
2403 { "i0", 16, monitor_get_reg },
2404 { "i1", 17, monitor_get_reg },
2405 { "i2", 18, monitor_get_reg },
2406 { "i3", 19, monitor_get_reg },
2407 { "i4", 20, monitor_get_reg },
2408 { "i5", 21, monitor_get_reg },
2409 { "i6", 22, monitor_get_reg },
2410 { "i7", 23, monitor_get_reg },
2411 { "pc", offsetof(CPUState, pc) },
2412 { "npc", offsetof(CPUState, npc) },
2413 { "y", offsetof(CPUState, y) },
2414 #ifndef TARGET_SPARC64
2415 { "psr", 0, &monitor_get_psr, },
2416 { "wim", offsetof(CPUState, wim) },
2417 #endif
2418 { "tbr", offsetof(CPUState, tbr) },
2419 { "fsr", offsetof(CPUState, fsr) },
2420 { "f0", offsetof(CPUState, fpr[0]) },
2421 { "f1", offsetof(CPUState, fpr[1]) },
2422 { "f2", offsetof(CPUState, fpr[2]) },
2423 { "f3", offsetof(CPUState, fpr[3]) },
2424 { "f4", offsetof(CPUState, fpr[4]) },
2425 { "f5", offsetof(CPUState, fpr[5]) },
2426 { "f6", offsetof(CPUState, fpr[6]) },
2427 { "f7", offsetof(CPUState, fpr[7]) },
2428 { "f8", offsetof(CPUState, fpr[8]) },
2429 { "f9", offsetof(CPUState, fpr[9]) },
2430 { "f10", offsetof(CPUState, fpr[10]) },
2431 { "f11", offsetof(CPUState, fpr[11]) },
2432 { "f12", offsetof(CPUState, fpr[12]) },
2433 { "f13", offsetof(CPUState, fpr[13]) },
2434 { "f14", offsetof(CPUState, fpr[14]) },
2435 { "f15", offsetof(CPUState, fpr[15]) },
2436 { "f16", offsetof(CPUState, fpr[16]) },
2437 { "f17", offsetof(CPUState, fpr[17]) },
2438 { "f18", offsetof(CPUState, fpr[18]) },
2439 { "f19", offsetof(CPUState, fpr[19]) },
2440 { "f20", offsetof(CPUState, fpr[20]) },
2441 { "f21", offsetof(CPUState, fpr[21]) },
2442 { "f22", offsetof(CPUState, fpr[22]) },
2443 { "f23", offsetof(CPUState, fpr[23]) },
2444 { "f24", offsetof(CPUState, fpr[24]) },
2445 { "f25", offsetof(CPUState, fpr[25]) },
2446 { "f26", offsetof(CPUState, fpr[26]) },
2447 { "f27", offsetof(CPUState, fpr[27]) },
2448 { "f28", offsetof(CPUState, fpr[28]) },
2449 { "f29", offsetof(CPUState, fpr[29]) },
2450 { "f30", offsetof(CPUState, fpr[30]) },
2451 { "f31", offsetof(CPUState, fpr[31]) },
2452 #ifdef TARGET_SPARC64
2453 { "f32", offsetof(CPUState, fpr[32]) },
2454 { "f34", offsetof(CPUState, fpr[34]) },
2455 { "f36", offsetof(CPUState, fpr[36]) },
2456 { "f38", offsetof(CPUState, fpr[38]) },
2457 { "f40", offsetof(CPUState, fpr[40]) },
2458 { "f42", offsetof(CPUState, fpr[42]) },
2459 { "f44", offsetof(CPUState, fpr[44]) },
2460 { "f46", offsetof(CPUState, fpr[46]) },
2461 { "f48", offsetof(CPUState, fpr[48]) },
2462 { "f50", offsetof(CPUState, fpr[50]) },
2463 { "f52", offsetof(CPUState, fpr[52]) },
2464 { "f54", offsetof(CPUState, fpr[54]) },
2465 { "f56", offsetof(CPUState, fpr[56]) },
2466 { "f58", offsetof(CPUState, fpr[58]) },
2467 { "f60", offsetof(CPUState, fpr[60]) },
2468 { "f62", offsetof(CPUState, fpr[62]) },
2469 { "asi", offsetof(CPUState, asi) },
2470 { "pstate", offsetof(CPUState, pstate) },
2471 { "cansave", offsetof(CPUState, cansave) },
2472 { "canrestore", offsetof(CPUState, canrestore) },
2473 { "otherwin", offsetof(CPUState, otherwin) },
2474 { "wstate", offsetof(CPUState, wstate) },
2475 { "cleanwin", offsetof(CPUState, cleanwin) },
2476 { "fprs", offsetof(CPUState, fprs) },
2477 #endif
2478 #endif
2479 { NULL },
2482 static void expr_error(Monitor *mon, const char *msg)
2484 monitor_printf(mon, "%s\n", msg);
2485 longjmp(expr_env, 1);
2488 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2489 static int get_monitor_def(target_long *pval, const char *name)
2491 const MonitorDef *md;
2492 void *ptr;
2494 for(md = monitor_defs; md->name != NULL; md++) {
2495 if (compare_cmd(name, md->name)) {
2496 if (md->get_value) {
2497 *pval = md->get_value(md, md->offset);
2498 } else {
2499 CPUState *env = mon_get_cpu();
2500 if (!env)
2501 return -2;
2502 ptr = (uint8_t *)env + md->offset;
2503 switch(md->type) {
2504 case MD_I32:
2505 *pval = *(int32_t *)ptr;
2506 break;
2507 case MD_TLONG:
2508 *pval = *(target_long *)ptr;
2509 break;
2510 default:
2511 *pval = 0;
2512 break;
2515 return 0;
2518 return -1;
2521 static void next(void)
2523 if (*pch != '\0') {
2524 pch++;
2525 while (qemu_isspace(*pch))
2526 pch++;
2530 static int64_t expr_sum(Monitor *mon);
2532 static int64_t expr_unary(Monitor *mon)
2534 int64_t n;
2535 char *p;
2536 int ret;
2538 switch(*pch) {
2539 case '+':
2540 next();
2541 n = expr_unary(mon);
2542 break;
2543 case '-':
2544 next();
2545 n = -expr_unary(mon);
2546 break;
2547 case '~':
2548 next();
2549 n = ~expr_unary(mon);
2550 break;
2551 case '(':
2552 next();
2553 n = expr_sum(mon);
2554 if (*pch != ')') {
2555 expr_error(mon, "')' expected");
2557 next();
2558 break;
2559 case '\'':
2560 pch++;
2561 if (*pch == '\0')
2562 expr_error(mon, "character constant expected");
2563 n = *pch;
2564 pch++;
2565 if (*pch != '\'')
2566 expr_error(mon, "missing terminating \' character");
2567 next();
2568 break;
2569 case '$':
2571 char buf[128], *q;
2572 target_long reg=0;
2574 pch++;
2575 q = buf;
2576 while ((*pch >= 'a' && *pch <= 'z') ||
2577 (*pch >= 'A' && *pch <= 'Z') ||
2578 (*pch >= '0' && *pch <= '9') ||
2579 *pch == '_' || *pch == '.') {
2580 if ((q - buf) < sizeof(buf) - 1)
2581 *q++ = *pch;
2582 pch++;
2584 while (qemu_isspace(*pch))
2585 pch++;
2586 *q = 0;
2587 ret = get_monitor_def(&reg, buf);
2588 if (ret == -1)
2589 expr_error(mon, "unknown register");
2590 else if (ret == -2)
2591 expr_error(mon, "no cpu defined");
2592 n = reg;
2594 break;
2595 case '\0':
2596 expr_error(mon, "unexpected end of expression");
2597 n = 0;
2598 break;
2599 default:
2600 #if TARGET_PHYS_ADDR_BITS > 32
2601 n = strtoull(pch, &p, 0);
2602 #else
2603 n = strtoul(pch, &p, 0);
2604 #endif
2605 if (pch == p) {
2606 expr_error(mon, "invalid char in expression");
2608 pch = p;
2609 while (qemu_isspace(*pch))
2610 pch++;
2611 break;
2613 return n;
2617 static int64_t expr_prod(Monitor *mon)
2619 int64_t val, val2;
2620 int op;
2622 val = expr_unary(mon);
2623 for(;;) {
2624 op = *pch;
2625 if (op != '*' && op != '/' && op != '%')
2626 break;
2627 next();
2628 val2 = expr_unary(mon);
2629 switch(op) {
2630 default:
2631 case '*':
2632 val *= val2;
2633 break;
2634 case '/':
2635 case '%':
2636 if (val2 == 0)
2637 expr_error(mon, "division by zero");
2638 if (op == '/')
2639 val /= val2;
2640 else
2641 val %= val2;
2642 break;
2645 return val;
2648 static int64_t expr_logic(Monitor *mon)
2650 int64_t val, val2;
2651 int op;
2653 val = expr_prod(mon);
2654 for(;;) {
2655 op = *pch;
2656 if (op != '&' && op != '|' && op != '^')
2657 break;
2658 next();
2659 val2 = expr_prod(mon);
2660 switch(op) {
2661 default:
2662 case '&':
2663 val &= val2;
2664 break;
2665 case '|':
2666 val |= val2;
2667 break;
2668 case '^':
2669 val ^= val2;
2670 break;
2673 return val;
2676 static int64_t expr_sum(Monitor *mon)
2678 int64_t val, val2;
2679 int op;
2681 val = expr_logic(mon);
2682 for(;;) {
2683 op = *pch;
2684 if (op != '+' && op != '-')
2685 break;
2686 next();
2687 val2 = expr_logic(mon);
2688 if (op == '+')
2689 val += val2;
2690 else
2691 val -= val2;
2693 return val;
2696 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2698 pch = *pp;
2699 if (setjmp(expr_env)) {
2700 *pp = pch;
2701 return -1;
2703 while (qemu_isspace(*pch))
2704 pch++;
2705 *pval = expr_sum(mon);
2706 *pp = pch;
2707 return 0;
2710 static int get_str(char *buf, int buf_size, const char **pp)
2712 const char *p;
2713 char *q;
2714 int c;
2716 q = buf;
2717 p = *pp;
2718 while (qemu_isspace(*p))
2719 p++;
2720 if (*p == '\0') {
2721 fail:
2722 *q = '\0';
2723 *pp = p;
2724 return -1;
2726 if (*p == '\"') {
2727 p++;
2728 while (*p != '\0' && *p != '\"') {
2729 if (*p == '\\') {
2730 p++;
2731 c = *p++;
2732 switch(c) {
2733 case 'n':
2734 c = '\n';
2735 break;
2736 case 'r':
2737 c = '\r';
2738 break;
2739 case '\\':
2740 case '\'':
2741 case '\"':
2742 break;
2743 default:
2744 qemu_printf("unsupported escape code: '\\%c'\n", c);
2745 goto fail;
2747 if ((q - buf) < buf_size - 1) {
2748 *q++ = c;
2750 } else {
2751 if ((q - buf) < buf_size - 1) {
2752 *q++ = *p;
2754 p++;
2757 if (*p != '\"') {
2758 qemu_printf("unterminated string\n");
2759 goto fail;
2761 p++;
2762 } else {
2763 while (*p != '\0' && !qemu_isspace(*p)) {
2764 if ((q - buf) < buf_size - 1) {
2765 *q++ = *p;
2767 p++;
2770 *q = '\0';
2771 *pp = p;
2772 return 0;
2776 * Store the command-name in cmdname, and return a pointer to
2777 * the remaining of the command string.
2779 static const char *get_command_name(const char *cmdline,
2780 char *cmdname, size_t nlen)
2782 size_t len;
2783 const char *p, *pstart;
2785 p = cmdline;
2786 while (qemu_isspace(*p))
2787 p++;
2788 if (*p == '\0')
2789 return NULL;
2790 pstart = p;
2791 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2792 p++;
2793 len = p - pstart;
2794 if (len > nlen - 1)
2795 len = nlen - 1;
2796 memcpy(cmdname, pstart, len);
2797 cmdname[len] = '\0';
2798 return p;
2802 * Read key of 'type' into 'key' and return the current
2803 * 'type' pointer.
2805 static char *key_get_info(const char *type, char **key)
2807 size_t len;
2808 char *p, *str;
2810 if (*type == ',')
2811 type++;
2813 p = strchr(type, ':');
2814 if (!p) {
2815 *key = NULL;
2816 return NULL;
2818 len = p - type;
2820 str = qemu_malloc(len + 1);
2821 memcpy(str, type, len);
2822 str[len] = '\0';
2824 *key = str;
2825 return ++p;
2828 static int default_fmt_format = 'x';
2829 static int default_fmt_size = 4;
2831 #define MAX_ARGS 16
2833 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2834 const char *cmdline,
2835 QDict *qdict)
2837 const char *p, *typestr;
2838 int c;
2839 const mon_cmd_t *cmd;
2840 char cmdname[256];
2841 char buf[1024];
2842 char *key;
2844 #ifdef DEBUG
2845 monitor_printf(mon, "command='%s'\n", cmdline);
2846 #endif
2848 /* extract the command name */
2849 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2850 if (!p)
2851 return NULL;
2853 /* find the command */
2854 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2855 if (compare_cmd(cmdname, cmd->name))
2856 break;
2859 if (cmd->name == NULL) {
2860 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2861 return NULL;
2864 /* parse the parameters */
2865 typestr = cmd->args_type;
2866 for(;;) {
2867 typestr = key_get_info(typestr, &key);
2868 if (!typestr)
2869 break;
2870 c = *typestr;
2871 typestr++;
2872 switch(c) {
2873 case 'F':
2874 case 'B':
2875 case 's':
2877 int ret;
2879 while (qemu_isspace(*p))
2880 p++;
2881 if (*typestr == '?') {
2882 typestr++;
2883 if (*p == '\0') {
2884 /* no optional string: NULL argument */
2885 break;
2888 ret = get_str(buf, sizeof(buf), &p);
2889 if (ret < 0) {
2890 switch(c) {
2891 case 'F':
2892 monitor_printf(mon, "%s: filename expected\n",
2893 cmdname);
2894 break;
2895 case 'B':
2896 monitor_printf(mon, "%s: block device name expected\n",
2897 cmdname);
2898 break;
2899 default:
2900 monitor_printf(mon, "%s: string expected\n", cmdname);
2901 break;
2903 goto fail;
2905 qdict_put(qdict, key, qstring_from_str(buf));
2907 break;
2908 case '/':
2910 int count, format, size;
2912 while (qemu_isspace(*p))
2913 p++;
2914 if (*p == '/') {
2915 /* format found */
2916 p++;
2917 count = 1;
2918 if (qemu_isdigit(*p)) {
2919 count = 0;
2920 while (qemu_isdigit(*p)) {
2921 count = count * 10 + (*p - '0');
2922 p++;
2925 size = -1;
2926 format = -1;
2927 for(;;) {
2928 switch(*p) {
2929 case 'o':
2930 case 'd':
2931 case 'u':
2932 case 'x':
2933 case 'i':
2934 case 'c':
2935 format = *p++;
2936 break;
2937 case 'b':
2938 size = 1;
2939 p++;
2940 break;
2941 case 'h':
2942 size = 2;
2943 p++;
2944 break;
2945 case 'w':
2946 size = 4;
2947 p++;
2948 break;
2949 case 'g':
2950 case 'L':
2951 size = 8;
2952 p++;
2953 break;
2954 default:
2955 goto next;
2958 next:
2959 if (*p != '\0' && !qemu_isspace(*p)) {
2960 monitor_printf(mon, "invalid char in format: '%c'\n",
2961 *p);
2962 goto fail;
2964 if (format < 0)
2965 format = default_fmt_format;
2966 if (format != 'i') {
2967 /* for 'i', not specifying a size gives -1 as size */
2968 if (size < 0)
2969 size = default_fmt_size;
2970 default_fmt_size = size;
2972 default_fmt_format = format;
2973 } else {
2974 count = 1;
2975 format = default_fmt_format;
2976 if (format != 'i') {
2977 size = default_fmt_size;
2978 } else {
2979 size = -1;
2982 qdict_put(qdict, "count", qint_from_int(count));
2983 qdict_put(qdict, "format", qint_from_int(format));
2984 qdict_put(qdict, "size", qint_from_int(size));
2986 break;
2987 case 'i':
2988 case 'l':
2990 int64_t val;
2992 while (qemu_isspace(*p))
2993 p++;
2994 if (*typestr == '?' || *typestr == '.') {
2995 if (*typestr == '?') {
2996 if (*p == '\0') {
2997 typestr++;
2998 break;
3000 } else {
3001 if (*p == '.') {
3002 p++;
3003 while (qemu_isspace(*p))
3004 p++;
3005 } else {
3006 typestr++;
3007 break;
3010 typestr++;
3012 if (get_expr(mon, &val, &p))
3013 goto fail;
3014 /* Check if 'i' is greater than 32-bit */
3015 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3016 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3017 monitor_printf(mon, "integer is for 32-bit values\n");
3018 goto fail;
3020 qdict_put(qdict, key, qint_from_int(val));
3022 break;
3023 case '-':
3025 int has_option;
3026 /* option */
3028 c = *typestr++;
3029 if (c == '\0')
3030 goto bad_type;
3031 while (qemu_isspace(*p))
3032 p++;
3033 has_option = 0;
3034 if (*p == '-') {
3035 p++;
3036 if (*p != c) {
3037 monitor_printf(mon, "%s: unsupported option -%c\n",
3038 cmdname, *p);
3039 goto fail;
3041 p++;
3042 has_option = 1;
3044 qdict_put(qdict, key, qint_from_int(has_option));
3046 break;
3047 default:
3048 bad_type:
3049 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3050 goto fail;
3052 qemu_free(key);
3053 key = NULL;
3055 /* check that all arguments were parsed */
3056 while (qemu_isspace(*p))
3057 p++;
3058 if (*p != '\0') {
3059 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3060 cmdname);
3061 goto fail;
3064 return cmd;
3066 fail:
3067 qemu_free(key);
3068 return NULL;
3071 static void monitor_handle_command(Monitor *mon, const char *cmdline)
3073 QDict *qdict;
3074 const mon_cmd_t *cmd;
3076 qdict = qdict_new();
3078 cmd = monitor_parse_command(mon, cmdline, qdict);
3079 if (!cmd)
3080 goto out;
3082 qemu_errors_to_mon(mon);
3084 if (monitor_handler_ported(cmd)) {
3085 QObject *data = NULL;
3087 cmd->mhandler.cmd_new(mon, qdict, &data);
3088 if (data)
3089 cmd->user_print(mon, data);
3091 qobject_decref(data);
3092 } else {
3093 cmd->mhandler.cmd(mon, qdict);
3096 qemu_errors_to_previous();
3098 out:
3099 QDECREF(qdict);
3102 static void cmd_completion(const char *name, const char *list)
3104 const char *p, *pstart;
3105 char cmd[128];
3106 int len;
3108 p = list;
3109 for(;;) {
3110 pstart = p;
3111 p = strchr(p, '|');
3112 if (!p)
3113 p = pstart + strlen(pstart);
3114 len = p - pstart;
3115 if (len > sizeof(cmd) - 2)
3116 len = sizeof(cmd) - 2;
3117 memcpy(cmd, pstart, len);
3118 cmd[len] = '\0';
3119 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3120 readline_add_completion(cur_mon->rs, cmd);
3122 if (*p == '\0')
3123 break;
3124 p++;
3128 static void file_completion(const char *input)
3130 DIR *ffs;
3131 struct dirent *d;
3132 char path[1024];
3133 char file[1024], file_prefix[1024];
3134 int input_path_len;
3135 const char *p;
3137 p = strrchr(input, '/');
3138 if (!p) {
3139 input_path_len = 0;
3140 pstrcpy(file_prefix, sizeof(file_prefix), input);
3141 pstrcpy(path, sizeof(path), ".");
3142 } else {
3143 input_path_len = p - input + 1;
3144 memcpy(path, input, input_path_len);
3145 if (input_path_len > sizeof(path) - 1)
3146 input_path_len = sizeof(path) - 1;
3147 path[input_path_len] = '\0';
3148 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3150 #ifdef DEBUG_COMPLETION
3151 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3152 input, path, file_prefix);
3153 #endif
3154 ffs = opendir(path);
3155 if (!ffs)
3156 return;
3157 for(;;) {
3158 struct stat sb;
3159 d = readdir(ffs);
3160 if (!d)
3161 break;
3162 if (strstart(d->d_name, file_prefix, NULL)) {
3163 memcpy(file, input, input_path_len);
3164 if (input_path_len < sizeof(file))
3165 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3166 d->d_name);
3167 /* stat the file to find out if it's a directory.
3168 * In that case add a slash to speed up typing long paths
3170 stat(file, &sb);
3171 if(S_ISDIR(sb.st_mode))
3172 pstrcat(file, sizeof(file), "/");
3173 readline_add_completion(cur_mon->rs, file);
3176 closedir(ffs);
3179 static void block_completion_it(void *opaque, BlockDriverState *bs)
3181 const char *name = bdrv_get_device_name(bs);
3182 const char *input = opaque;
3184 if (input[0] == '\0' ||
3185 !strncmp(name, (char *)input, strlen(input))) {
3186 readline_add_completion(cur_mon->rs, name);
3190 /* NOTE: this parser is an approximate form of the real command parser */
3191 static void parse_cmdline(const char *cmdline,
3192 int *pnb_args, char **args)
3194 const char *p;
3195 int nb_args, ret;
3196 char buf[1024];
3198 p = cmdline;
3199 nb_args = 0;
3200 for(;;) {
3201 while (qemu_isspace(*p))
3202 p++;
3203 if (*p == '\0')
3204 break;
3205 if (nb_args >= MAX_ARGS)
3206 break;
3207 ret = get_str(buf, sizeof(buf), &p);
3208 args[nb_args] = qemu_strdup(buf);
3209 nb_args++;
3210 if (ret < 0)
3211 break;
3213 *pnb_args = nb_args;
3216 static const char *next_arg_type(const char *typestr)
3218 const char *p = strchr(typestr, ':');
3219 return (p != NULL ? ++p : typestr);
3222 static void monitor_find_completion(const char *cmdline)
3224 const char *cmdname;
3225 char *args[MAX_ARGS];
3226 int nb_args, i, len;
3227 const char *ptype, *str;
3228 const mon_cmd_t *cmd;
3229 const KeyDef *key;
3231 parse_cmdline(cmdline, &nb_args, args);
3232 #ifdef DEBUG_COMPLETION
3233 for(i = 0; i < nb_args; i++) {
3234 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3236 #endif
3238 /* if the line ends with a space, it means we want to complete the
3239 next arg */
3240 len = strlen(cmdline);
3241 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3242 if (nb_args >= MAX_ARGS)
3243 return;
3244 args[nb_args++] = qemu_strdup("");
3246 if (nb_args <= 1) {
3247 /* command completion */
3248 if (nb_args == 0)
3249 cmdname = "";
3250 else
3251 cmdname = args[0];
3252 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3253 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3254 cmd_completion(cmdname, cmd->name);
3256 } else {
3257 /* find the command */
3258 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3259 if (compare_cmd(args[0], cmd->name))
3260 goto found;
3262 return;
3263 found:
3264 ptype = next_arg_type(cmd->args_type);
3265 for(i = 0; i < nb_args - 2; i++) {
3266 if (*ptype != '\0') {
3267 ptype = next_arg_type(ptype);
3268 while (*ptype == '?')
3269 ptype = next_arg_type(ptype);
3272 str = args[nb_args - 1];
3273 if (*ptype == '-' && ptype[1] != '\0') {
3274 ptype += 2;
3276 switch(*ptype) {
3277 case 'F':
3278 /* file completion */
3279 readline_set_completion_index(cur_mon->rs, strlen(str));
3280 file_completion(str);
3281 break;
3282 case 'B':
3283 /* block device name completion */
3284 readline_set_completion_index(cur_mon->rs, strlen(str));
3285 bdrv_iterate(block_completion_it, (void *)str);
3286 break;
3287 case 's':
3288 /* XXX: more generic ? */
3289 if (!strcmp(cmd->name, "info")) {
3290 readline_set_completion_index(cur_mon->rs, strlen(str));
3291 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3292 cmd_completion(str, cmd->name);
3294 } else if (!strcmp(cmd->name, "sendkey")) {
3295 char *sep = strrchr(str, '-');
3296 if (sep)
3297 str = sep + 1;
3298 readline_set_completion_index(cur_mon->rs, strlen(str));
3299 for(key = key_defs; key->name != NULL; key++) {
3300 cmd_completion(str, key->name);
3302 } else if (!strcmp(cmd->name, "help|?")) {
3303 readline_set_completion_index(cur_mon->rs, strlen(str));
3304 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3305 cmd_completion(str, cmd->name);
3308 break;
3309 default:
3310 break;
3313 for(i = 0; i < nb_args; i++)
3314 qemu_free(args[i]);
3317 static int monitor_can_read(void *opaque)
3319 Monitor *mon = opaque;
3321 return (mon->suspend_cnt == 0) ? 128 : 0;
3324 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3326 Monitor *old_mon = cur_mon;
3327 int i;
3329 cur_mon = opaque;
3331 if (cur_mon->rs) {
3332 for (i = 0; i < size; i++)
3333 readline_handle_byte(cur_mon->rs, buf[i]);
3334 } else {
3335 if (size == 0 || buf[size - 1] != 0)
3336 monitor_printf(cur_mon, "corrupted command\n");
3337 else
3338 monitor_handle_command(cur_mon, (char *)buf);
3341 cur_mon = old_mon;
3344 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3346 monitor_suspend(mon);
3347 monitor_handle_command(mon, cmdline);
3348 monitor_resume(mon);
3351 int monitor_suspend(Monitor *mon)
3353 if (!mon->rs)
3354 return -ENOTTY;
3355 mon->suspend_cnt++;
3356 return 0;
3359 void monitor_resume(Monitor *mon)
3361 if (!mon->rs)
3362 return;
3363 if (--mon->suspend_cnt == 0)
3364 readline_show_prompt(mon->rs);
3367 static void monitor_event(void *opaque, int event)
3369 Monitor *mon = opaque;
3371 switch (event) {
3372 case CHR_EVENT_MUX_IN:
3373 mon->mux_out = 0;
3374 if (mon->reset_seen) {
3375 readline_restart(mon->rs);
3376 monitor_resume(mon);
3377 monitor_flush(mon);
3378 } else {
3379 mon->suspend_cnt = 0;
3381 break;
3383 case CHR_EVENT_MUX_OUT:
3384 if (mon->reset_seen) {
3385 if (mon->suspend_cnt == 0) {
3386 monitor_printf(mon, "\n");
3388 monitor_flush(mon);
3389 monitor_suspend(mon);
3390 } else {
3391 mon->suspend_cnt++;
3393 mon->mux_out = 1;
3394 break;
3396 case CHR_EVENT_RESET:
3397 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3398 "information\n", QEMU_VERSION);
3399 if (!mon->mux_out) {
3400 readline_show_prompt(mon->rs);
3402 mon->reset_seen = 1;
3403 break;
3409 * Local variables:
3410 * c-indent-level: 4
3411 * c-basic-offset: 4
3412 * tab-width: 8
3413 * End:
3416 void monitor_init(CharDriverState *chr, int flags)
3418 static int is_first_init = 1;
3419 Monitor *mon;
3421 if (is_first_init) {
3422 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3423 is_first_init = 0;
3426 mon = qemu_mallocz(sizeof(*mon));
3428 mon->chr = chr;
3429 mon->flags = flags;
3430 if (flags & MONITOR_USE_READLINE) {
3431 mon->rs = readline_init(mon, monitor_find_completion);
3432 monitor_read_command(mon, 0);
3435 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3436 mon);
3438 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3439 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3440 cur_mon = mon;
3443 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3445 BlockDriverState *bs = opaque;
3446 int ret = 0;
3448 if (bdrv_set_key(bs, password) != 0) {
3449 monitor_printf(mon, "invalid password\n");
3450 ret = -EPERM;
3452 if (mon->password_completion_cb)
3453 mon->password_completion_cb(mon->password_opaque, ret);
3455 monitor_read_command(mon, 1);
3458 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3459 BlockDriverCompletionFunc *completion_cb,
3460 void *opaque)
3462 int err;
3464 if (!bdrv_key_required(bs)) {
3465 if (completion_cb)
3466 completion_cb(opaque, 0);
3467 return;
3470 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3471 bdrv_get_encrypted_filename(bs));
3473 mon->password_completion_cb = completion_cb;
3474 mon->password_opaque = opaque;
3476 err = monitor_read_password(mon, bdrv_password_cb, bs);
3478 if (err && completion_cb)
3479 completion_cb(opaque, err);
3482 typedef struct QemuErrorSink QemuErrorSink;
3483 struct QemuErrorSink {
3484 enum {
3485 ERR_SINK_FILE,
3486 ERR_SINK_MONITOR,
3487 } dest;
3488 union {
3489 FILE *fp;
3490 Monitor *mon;
3492 QemuErrorSink *previous;
3495 static QemuErrorSink *qemu_error_sink;
3497 void qemu_errors_to_file(FILE *fp)
3499 QemuErrorSink *sink;
3501 sink = qemu_mallocz(sizeof(*sink));
3502 sink->dest = ERR_SINK_FILE;
3503 sink->fp = fp;
3504 sink->previous = qemu_error_sink;
3505 qemu_error_sink = sink;
3508 void qemu_errors_to_mon(Monitor *mon)
3510 QemuErrorSink *sink;
3512 sink = qemu_mallocz(sizeof(*sink));
3513 sink->dest = ERR_SINK_MONITOR;
3514 sink->mon = mon;
3515 sink->previous = qemu_error_sink;
3516 qemu_error_sink = sink;
3519 void qemu_errors_to_previous(void)
3521 QemuErrorSink *sink;
3523 assert(qemu_error_sink != NULL);
3524 sink = qemu_error_sink;
3525 qemu_error_sink = sink->previous;
3526 qemu_free(sink);
3529 void qemu_error(const char *fmt, ...)
3531 va_list args;
3533 assert(qemu_error_sink != NULL);
3534 switch (qemu_error_sink->dest) {
3535 case ERR_SINK_FILE:
3536 va_start(args, fmt);
3537 vfprintf(qemu_error_sink->fp, fmt, args);
3538 va_end(args);
3539 break;
3540 case ERR_SINK_MONITOR:
3541 va_start(args, fmt);
3542 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3543 va_end(args);
3544 break;