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
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
42 #include "audio/audio.h"
45 #include "qemu-timer.h"
46 #include "migration.h"
55 #include "json-streamer.h"
56 #include "json-parser.h"
60 //#define DEBUG_COMPLETION
66 * 'B' block device name
67 * 's' string (accept optional quote)
68 * 'O' option string of the form NAME=VALUE,...
69 * parsed according to QemuOptsList given by its name
70 * Example: 'device:O' uses qemu_device_opts.
71 * Restriction: only lists with empty desc are supported
72 * TODO lift the restriction
74 * 'l' target long (32 or 64 bit)
75 * 'M' just like 'l', except in user mode the value is
76 * multiplied by 2^20 (think Mebibyte)
78 * user mode accepts an optional G, g, M, m, K, k suffix,
79 * which multiplies the value by 2^30 for suffixes G and
80 * g, 2^20 for M and m, 2^10 for K and k
82 * user mode accepts an optional ms, us, ns suffix,
83 * which divides the value by 1e3, 1e6, 1e9, respectively
84 * '/' optional gdb-like print format (like "/10x")
86 * '?' optional type (for all types, except '/')
87 * '.' other form of optional type (for 'i' and 'l')
89 * user mode accepts "on" or "off"
90 * '-' optional parameter (eg. '-f')
94 typedef struct MonitorCompletionData MonitorCompletionData;
95 struct MonitorCompletionData {
97 void (*user_print)(Monitor *mon, const QObject *data);
100 typedef struct mon_cmd_t {
102 const char *args_type;
105 void (*user_print)(Monitor *mon, const QObject *data);
107 void (*info)(Monitor *mon);
108 void (*info_new)(Monitor *mon, QObject **ret_data);
109 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
110 void (*cmd)(Monitor *mon, const QDict *qdict);
111 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
112 int (*cmd_async)(Monitor *mon, const QDict *params,
113 MonitorCompletion *cb, void *opaque);
118 /* file descriptors passed via SCM_RIGHTS */
119 typedef struct mon_fd_t mon_fd_t;
123 QLIST_ENTRY(mon_fd_t) next;
126 typedef struct MonitorControl {
128 JSONMessageParser parser;
133 CharDriverState *chr;
138 uint8_t outbuf[1024];
143 BlockDriverCompletionFunc *password_completion_cb;
144 void *password_opaque;
145 #ifdef CONFIG_DEBUG_MONITOR
149 QLIST_HEAD(,mon_fd_t) fds;
150 QLIST_ENTRY(Monitor) entry;
153 #ifdef CONFIG_DEBUG_MONITOR
154 #define MON_DEBUG(fmt, ...) do { \
155 fprintf(stderr, "Monitor: "); \
156 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
158 static inline void mon_print_count_inc(Monitor *mon)
160 mon->print_calls_nr++;
163 static inline void mon_print_count_init(Monitor *mon)
165 mon->print_calls_nr = 0;
168 static inline int mon_print_count_get(const Monitor *mon)
170 return mon->print_calls_nr;
173 #else /* !CONFIG_DEBUG_MONITOR */
174 #define MON_DEBUG(fmt, ...) do { } while (0)
175 static inline void mon_print_count_inc(Monitor *mon) { }
176 static inline void mon_print_count_init(Monitor *mon) { }
177 static inline int mon_print_count_get(const Monitor *mon) { return 0; }
178 #endif /* CONFIG_DEBUG_MONITOR */
180 /* QMP checker flags */
181 #define QMP_ACCEPT_UNKNOWNS 1
183 static QLIST_HEAD(mon_list, Monitor) mon_list;
185 static const mon_cmd_t mon_cmds[];
186 static const mon_cmd_t info_cmds[];
189 Monitor *default_mon;
191 static void monitor_command_cb(Monitor *mon, const char *cmdline,
194 static inline int qmp_cmd_mode(const Monitor *mon)
196 return (mon->mc ? mon->mc->command_mode : 0);
199 /* Return true if in control mode, false otherwise */
200 static inline int monitor_ctrl_mode(const Monitor *mon)
202 return (mon->flags & MONITOR_USE_CONTROL);
205 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
206 int monitor_cur_is_qmp(void)
208 return cur_mon && monitor_ctrl_mode(cur_mon);
211 static void monitor_read_command(Monitor *mon, int show_prompt)
216 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
218 readline_show_prompt(mon->rs);
221 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
224 if (monitor_ctrl_mode(mon)) {
225 qerror_report(QERR_MISSING_PARAMETER, "password");
227 } else if (mon->rs) {
228 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
229 /* prompt is printed on return from the command handler */
232 monitor_printf(mon, "terminal does not support password prompting\n");
237 void monitor_flush(Monitor *mon)
239 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
240 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
241 mon->outbuf_index = 0;
245 /* flush at every end of line or if the buffer is full */
246 static void monitor_puts(Monitor *mon, const char *str)
255 mon->outbuf[mon->outbuf_index++] = '\r';
256 mon->outbuf[mon->outbuf_index++] = c;
257 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
263 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
270 mon_print_count_inc(mon);
272 if (monitor_ctrl_mode(mon)) {
276 vsnprintf(buf, sizeof(buf), fmt, ap);
277 monitor_puts(mon, buf);
280 void monitor_printf(Monitor *mon, const char *fmt, ...)
284 monitor_vprintf(mon, fmt, ap);
288 void monitor_print_filename(Monitor *mon, const char *filename)
292 for (i = 0; filename[i]; i++) {
293 switch (filename[i]) {
297 monitor_printf(mon, "\\%c", filename[i]);
300 monitor_printf(mon, "\\t");
303 monitor_printf(mon, "\\r");
306 monitor_printf(mon, "\\n");
309 monitor_printf(mon, "%c", filename[i]);
315 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
319 monitor_vprintf((Monitor *)stream, fmt, ap);
324 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
326 static inline int monitor_handler_ported(const mon_cmd_t *cmd)
328 return cmd->user_print != NULL;
331 static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
333 return cmd->flags & MONITOR_CMD_ASYNC;
336 static inline int monitor_has_error(const Monitor *mon)
338 return mon->error != NULL;
341 static void monitor_json_emitter(Monitor *mon, const QObject *data)
345 json = qobject_to_json(data);
346 assert(json != NULL);
348 qstring_append_chr(json, '\n');
349 monitor_puts(mon, qstring_get_str(json));
354 static void monitor_protocol_emitter(Monitor *mon, QObject *data)
360 if (!monitor_has_error(mon)) {
361 /* success response */
363 qobject_incref(data);
364 qdict_put_obj(qmp, "return", data);
366 /* return an empty QDict by default */
367 qdict_put(qmp, "return", qdict_new());
371 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
372 qdict_put(qmp, "error", mon->error->error);
373 QINCREF(mon->error->error);
379 qdict_put_obj(qmp, "id", mon->mc->id);
383 monitor_json_emitter(mon, QOBJECT(qmp));
387 static void timestamp_put(QDict *qdict)
393 err = qemu_gettimeofday(&tv);
397 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
398 "'microseconds': %" PRId64 " }",
399 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
400 qdict_put_obj(qdict, "timestamp", obj);
404 * monitor_protocol_event(): Generate a Monitor event
406 * Event-specific data can be emitted through the (optional) 'data' parameter.
408 void monitor_protocol_event(MonitorEvent event, QObject *data)
411 const char *event_name;
414 assert(event < QEVENT_MAX);
417 case QEVENT_SHUTDOWN:
418 event_name = "SHUTDOWN";
421 event_name = "RESET";
423 case QEVENT_POWERDOWN:
424 event_name = "POWERDOWN";
430 event_name = "RESUME";
432 case QEVENT_VNC_CONNECTED:
433 event_name = "VNC_CONNECTED";
435 case QEVENT_VNC_INITIALIZED:
436 event_name = "VNC_INITIALIZED";
438 case QEVENT_VNC_DISCONNECTED:
439 event_name = "VNC_DISCONNECTED";
441 case QEVENT_BLOCK_IO_ERROR:
442 event_name = "BLOCK_IO_ERROR";
444 case QEVENT_RTC_CHANGE:
445 event_name = "RTC_CHANGE";
447 case QEVENT_WATCHDOG:
448 event_name = "WATCHDOG";
457 qdict_put(qmp, "event", qstring_from_str(event_name));
459 qobject_incref(data);
460 qdict_put_obj(qmp, "data", data);
463 QLIST_FOREACH(mon, &mon_list, entry) {
464 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
465 monitor_json_emitter(mon, QOBJECT(qmp));
471 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
474 /* Will setup QMP capabilities in the future */
475 if (monitor_ctrl_mode(mon)) {
476 mon->mc->command_mode = 1;
482 static int compare_cmd(const char *name, const char *list)
484 const char *p, *pstart;
492 p = pstart + strlen(pstart);
493 if ((p - pstart) == len && !memcmp(pstart, name, len))
502 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
503 const char *prefix, const char *name)
505 const mon_cmd_t *cmd;
507 for(cmd = cmds; cmd->name != NULL; cmd++) {
508 if (!name || !strcmp(name, cmd->name))
509 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
510 cmd->params, cmd->help);
514 static void help_cmd(Monitor *mon, const char *name)
516 if (name && !strcmp(name, "info")) {
517 help_cmd_dump(mon, info_cmds, "info ", NULL);
519 help_cmd_dump(mon, mon_cmds, "", name);
520 if (name && !strcmp(name, "log")) {
521 const CPULogItem *item;
522 monitor_printf(mon, "Log items (comma separated):\n");
523 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
524 for(item = cpu_log_items; item->mask != 0; item++) {
525 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
531 static void do_help_cmd(Monitor *mon, const QDict *qdict)
533 help_cmd(mon, qdict_get_try_str(qdict, "name"));
536 static void user_monitor_complete(void *opaque, QObject *ret_data)
538 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
541 data->user_print(data->mon, ret_data);
543 monitor_resume(data->mon);
547 static void qmp_monitor_complete(void *opaque, QObject *ret_data)
549 monitor_protocol_emitter(opaque, ret_data);
552 static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
555 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
558 static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
560 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
563 static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
568 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
570 cb_data->user_print = cmd->user_print;
571 monitor_suspend(mon);
572 ret = cmd->mhandler.cmd_async(mon, params,
573 user_monitor_complete, cb_data);
580 static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
584 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
586 cb_data->user_print = cmd->user_print;
587 monitor_suspend(mon);
588 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
595 static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
597 const mon_cmd_t *cmd;
598 const char *item = qdict_get_try_str(qdict, "item");
601 assert(monitor_ctrl_mode(mon) == 0);
605 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
606 if (compare_cmd(item, cmd->name))
610 if (cmd->name == NULL) {
611 if (monitor_ctrl_mode(mon)) {
612 qerror_report(QERR_COMMAND_NOT_FOUND, item);
618 if (monitor_handler_is_async(cmd)) {
619 if (monitor_ctrl_mode(mon)) {
620 qmp_async_info_handler(mon, cmd);
622 user_async_info_handler(mon, cmd);
625 * Indicate that this command is asynchronous and will not return any
626 * data (not even empty). Instead, the data will be returned via a
627 * completion callback.
629 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
630 } else if (monitor_handler_ported(cmd)) {
631 cmd->mhandler.info_new(mon, ret_data);
633 if (!monitor_ctrl_mode(mon)) {
635 * User Protocol function is called here, Monitor Protocol is
636 * handled by monitor_call_handler()
639 cmd->user_print(mon, *ret_data);
642 if (monitor_ctrl_mode(mon)) {
643 /* handler not converted yet */
644 qerror_report(QERR_COMMAND_NOT_FOUND, item);
647 cmd->mhandler.info(mon);
654 help_cmd(mon, "info");
658 static void do_info_version_print(Monitor *mon, const QObject *data)
662 qdict = qobject_to_qdict(data);
664 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
665 qdict_get_str(qdict, "package"));
668 static void do_info_version(Monitor *mon, QObject **ret_data)
670 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
671 QEMU_VERSION, QEMU_PKGVERSION);
674 static void do_info_name_print(Monitor *mon, const QObject *data)
678 qdict = qobject_to_qdict(data);
679 if (qdict_size(qdict) == 0) {
683 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
686 static void do_info_name(Monitor *mon, QObject **ret_data)
688 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
689 qobject_from_jsonf("{}");
692 static QObject *get_cmd_dict(const char *name)
696 /* Remove '|' from some commands */
697 p = strchr(name, '|');
704 return qobject_from_jsonf("{ 'name': %s }", p);
707 static void do_info_commands(Monitor *mon, QObject **ret_data)
710 const mon_cmd_t *cmd;
712 cmd_list = qlist_new();
714 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
715 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
716 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
720 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
721 if (monitor_handler_ported(cmd)) {
723 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
724 qlist_append_obj(cmd_list, get_cmd_dict(buf));
728 *ret_data = QOBJECT(cmd_list);
731 static void do_info_uuid_print(Monitor *mon, const QObject *data)
733 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
736 static void do_info_uuid(Monitor *mon, QObject **ret_data)
740 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
741 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
742 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
743 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
744 qemu_uuid[14], qemu_uuid[15]);
745 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
748 /* get the current CPU defined by the user */
749 static int mon_set_cpu(int cpu_index)
753 for(env = first_cpu; env != NULL; env = env->next_cpu) {
754 if (env->cpu_index == cpu_index) {
755 cur_mon->mon_cpu = env;
762 static CPUState *mon_get_cpu(void)
764 if (!cur_mon->mon_cpu) {
767 cpu_synchronize_state(cur_mon->mon_cpu);
768 return cur_mon->mon_cpu;
771 static void do_info_registers(Monitor *mon)
776 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
779 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
784 static void print_cpu_iter(QObject *obj, void *opaque)
788 Monitor *mon = opaque;
790 assert(qobject_type(obj) == QTYPE_QDICT);
791 cpu = qobject_to_qdict(obj);
793 if (qdict_get_bool(cpu, "current")) {
797 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
799 #if defined(TARGET_I386)
800 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
801 (target_ulong) qdict_get_int(cpu, "pc"));
802 #elif defined(TARGET_PPC)
803 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
804 (target_long) qdict_get_int(cpu, "nip"));
805 #elif defined(TARGET_SPARC)
806 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
807 (target_long) qdict_get_int(cpu, "pc"));
808 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
809 (target_long) qdict_get_int(cpu, "npc"));
810 #elif defined(TARGET_MIPS)
811 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
812 (target_long) qdict_get_int(cpu, "PC"));
815 if (qdict_get_bool(cpu, "halted")) {
816 monitor_printf(mon, " (halted)");
819 monitor_printf(mon, "\n");
822 static void monitor_print_cpus(Monitor *mon, const QObject *data)
826 assert(qobject_type(data) == QTYPE_QLIST);
827 cpu_list = qobject_to_qlist(data);
828 qlist_iter(cpu_list, print_cpu_iter, mon);
831 static void do_info_cpus(Monitor *mon, QObject **ret_data)
836 cpu_list = qlist_new();
838 /* just to set the default cpu if not already done */
841 for(env = first_cpu; env != NULL; env = env->next_cpu) {
845 cpu_synchronize_state(env);
847 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
848 env->cpu_index, env == mon->mon_cpu,
851 cpu = qobject_to_qdict(obj);
853 #if defined(TARGET_I386)
854 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
855 #elif defined(TARGET_PPC)
856 qdict_put(cpu, "nip", qint_from_int(env->nip));
857 #elif defined(TARGET_SPARC)
858 qdict_put(cpu, "pc", qint_from_int(env->pc));
859 qdict_put(cpu, "npc", qint_from_int(env->npc));
860 #elif defined(TARGET_MIPS)
861 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
864 qlist_append(cpu_list, cpu);
867 *ret_data = QOBJECT(cpu_list);
870 static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
872 int index = qdict_get_int(qdict, "index");
873 if (mon_set_cpu(index) < 0) {
874 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
881 static void do_info_jit(Monitor *mon)
883 dump_exec_info((FILE *)mon, monitor_fprintf);
886 static void do_info_history(Monitor *mon)
895 str = readline_get_history(mon->rs, i);
898 monitor_printf(mon, "%d: '%s'\n", i, str);
903 #if defined(TARGET_PPC)
904 /* XXX: not implemented in other targets */
905 static void do_info_cpu_stats(Monitor *mon)
910 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
915 * do_quit(): Quit QEMU execution
917 static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
919 monitor_suspend(mon);
921 qemu_system_shutdown_request();
926 static int change_vnc_password(const char *password)
928 if (vnc_display_password(NULL, password) < 0) {
929 qerror_report(QERR_SET_PASSWD_FAILED);
936 static void change_vnc_password_cb(Monitor *mon, const char *password,
939 change_vnc_password(password);
940 monitor_read_command(mon, 1);
943 static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
945 if (strcmp(target, "passwd") == 0 ||
946 strcmp(target, "password") == 0) {
949 strncpy(password, arg, sizeof(password));
950 password[sizeof(password) - 1] = '\0';
951 return change_vnc_password(password);
953 return monitor_read_password(mon, change_vnc_password_cb, NULL);
956 if (vnc_display_open(NULL, target) < 0) {
957 qerror_report(QERR_VNC_SERVER_FAILED, target);
966 * do_change(): Change a removable medium, or VNC configuration
968 static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
970 const char *device = qdict_get_str(qdict, "device");
971 const char *target = qdict_get_str(qdict, "target");
972 const char *arg = qdict_get_try_str(qdict, "arg");
975 if (strcmp(device, "vnc") == 0) {
976 ret = do_change_vnc(mon, target, arg);
978 ret = do_change_block(mon, device, target, arg);
984 static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
986 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
990 static void do_logfile(Monitor *mon, const QDict *qdict)
992 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
995 static void do_log(Monitor *mon, const QDict *qdict)
998 const char *items = qdict_get_str(qdict, "items");
1000 if (!strcmp(items, "none")) {
1003 mask = cpu_str_to_log_mask(items);
1005 help_cmd(mon, "log");
1012 static void do_singlestep(Monitor *mon, const QDict *qdict)
1014 const char *option = qdict_get_try_str(qdict, "option");
1015 if (!option || !strcmp(option, "on")) {
1017 } else if (!strcmp(option, "off")) {
1020 monitor_printf(mon, "unexpected option %s\n", option);
1025 * do_stop(): Stop VM execution
1027 static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
1029 vm_stop(EXCP_INTERRUPT);
1033 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
1035 struct bdrv_iterate_context {
1041 * do_cont(): Resume emulation.
1043 static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
1045 struct bdrv_iterate_context context = { mon, 0 };
1047 bdrv_iterate(encrypted_bdrv_it, &context);
1048 /* only resume the vm if all keys are set and valid */
1057 static void bdrv_key_cb(void *opaque, int err)
1059 Monitor *mon = opaque;
1061 /* another key was set successfully, retry to continue */
1063 do_cont(mon, NULL, NULL);
1066 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1068 struct bdrv_iterate_context *context = opaque;
1070 if (!context->err && bdrv_key_required(bs)) {
1071 context->err = -EBUSY;
1072 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1077 static void do_gdbserver(Monitor *mon, const QDict *qdict)
1079 const char *device = qdict_get_try_str(qdict, "device");
1081 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1082 if (gdbserver_start(device) < 0) {
1083 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1085 } else if (strcmp(device, "none") == 0) {
1086 monitor_printf(mon, "Disabled gdbserver\n");
1088 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1093 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
1095 const char *action = qdict_get_str(qdict, "action");
1096 if (select_watchdog_action(action) == -1) {
1097 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1101 static void monitor_printc(Monitor *mon, int c)
1103 monitor_printf(mon, "'");
1106 monitor_printf(mon, "\\'");
1109 monitor_printf(mon, "\\\\");
1112 monitor_printf(mon, "\\n");
1115 monitor_printf(mon, "\\r");
1118 if (c >= 32 && c <= 126) {
1119 monitor_printf(mon, "%c", c);
1121 monitor_printf(mon, "\\x%02x", c);
1125 monitor_printf(mon, "'");
1128 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1129 target_phys_addr_t addr, int is_physical)
1132 int l, line_size, i, max_digits, len;
1136 if (format == 'i') {
1139 env = mon_get_cpu();
1143 } else if (wsize == 4) {
1146 /* as default we use the current CS size */
1149 #ifdef TARGET_X86_64
1150 if ((env->efer & MSR_EFER_LMA) &&
1151 (env->segs[R_CS].flags & DESC_L_MASK))
1155 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1160 monitor_disas(mon, env, addr, count, is_physical, flags);
1164 len = wsize * count;
1173 max_digits = (wsize * 8 + 2) / 3;
1177 max_digits = (wsize * 8) / 4;
1181 max_digits = (wsize * 8 * 10 + 32) / 33;
1190 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1192 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1197 cpu_physical_memory_rw(addr, buf, l, 0);
1199 env = mon_get_cpu();
1200 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
1201 monitor_printf(mon, " Cannot access memory\n");
1210 v = ldub_raw(buf + i);
1213 v = lduw_raw(buf + i);
1216 v = (uint32_t)ldl_raw(buf + i);
1219 v = ldq_raw(buf + i);
1222 monitor_printf(mon, " ");
1225 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1228 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1231 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1234 monitor_printf(mon, "%*" PRId64, max_digits, v);
1237 monitor_printc(mon, v);
1242 monitor_printf(mon, "\n");
1248 static void do_memory_dump(Monitor *mon, const QDict *qdict)
1250 int count = qdict_get_int(qdict, "count");
1251 int format = qdict_get_int(qdict, "format");
1252 int size = qdict_get_int(qdict, "size");
1253 target_long addr = qdict_get_int(qdict, "addr");
1255 memory_dump(mon, count, format, size, addr, 0);
1258 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1260 int count = qdict_get_int(qdict, "count");
1261 int format = qdict_get_int(qdict, "format");
1262 int size = qdict_get_int(qdict, "size");
1263 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
1265 memory_dump(mon, count, format, size, addr, 1);
1268 static void do_print(Monitor *mon, const QDict *qdict)
1270 int format = qdict_get_int(qdict, "format");
1271 target_phys_addr_t val = qdict_get_int(qdict, "val");
1273 #if TARGET_PHYS_ADDR_BITS == 32
1276 monitor_printf(mon, "%#o", val);
1279 monitor_printf(mon, "%#x", val);
1282 monitor_printf(mon, "%u", val);
1286 monitor_printf(mon, "%d", val);
1289 monitor_printc(mon, val);
1295 monitor_printf(mon, "%#" PRIo64, val);
1298 monitor_printf(mon, "%#" PRIx64, val);
1301 monitor_printf(mon, "%" PRIu64, val);
1305 monitor_printf(mon, "%" PRId64, val);
1308 monitor_printc(mon, val);
1312 monitor_printf(mon, "\n");
1315 static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
1318 uint32_t size = qdict_get_int(qdict, "size");
1319 const char *filename = qdict_get_str(qdict, "filename");
1320 target_long addr = qdict_get_int(qdict, "val");
1326 env = mon_get_cpu();
1328 f = fopen(filename, "wb");
1330 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1337 cpu_memory_rw_debug(env, addr, buf, l, 0);
1338 if (fwrite(buf, 1, l, f) != l) {
1339 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1353 static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
1359 uint32_t size = qdict_get_int(qdict, "size");
1360 const char *filename = qdict_get_str(qdict, "filename");
1361 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1364 f = fopen(filename, "wb");
1366 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1373 cpu_physical_memory_rw(addr, buf, l, 0);
1374 if (fwrite(buf, 1, l, f) != l) {
1375 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1390 static void do_sum(Monitor *mon, const QDict *qdict)
1395 uint32_t start = qdict_get_int(qdict, "start");
1396 uint32_t size = qdict_get_int(qdict, "size");
1399 for(addr = start; addr < (start + size); addr++) {
1400 cpu_physical_memory_rw(addr, buf, 1, 0);
1401 /* BSD sum algorithm ('sum' Unix command) */
1402 sum = (sum >> 1) | (sum << 15);
1405 monitor_printf(mon, "%05d\n", sum);
1413 static const KeyDef key_defs[] = {
1415 { 0x36, "shift_r" },
1420 { 0xe4, "altgr_r" },
1440 { 0x0e, "backspace" },
1453 { 0x1a, "bracket_left" },
1454 { 0x1b, "bracket_right" },
1466 { 0x27, "semicolon" },
1467 { 0x28, "apostrophe" },
1468 { 0x29, "grave_accent" },
1470 { 0x2b, "backslash" },
1482 { 0x37, "asterisk" },
1485 { 0x3a, "caps_lock" },
1496 { 0x45, "num_lock" },
1497 { 0x46, "scroll_lock" },
1499 { 0xb5, "kp_divide" },
1500 { 0x37, "kp_multiply" },
1501 { 0x4a, "kp_subtract" },
1503 { 0x9c, "kp_enter" },
1504 { 0x53, "kp_decimal" },
1537 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1552 { 0xfe, "compose" },
1557 static int get_keycode(const char *key)
1563 for(p = key_defs; p->name != NULL; p++) {
1564 if (!strcmp(key, p->name))
1567 if (strstart(key, "0x", NULL)) {
1568 ret = strtoul(key, &endp, 0);
1569 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1575 #define MAX_KEYCODES 16
1576 static uint8_t keycodes[MAX_KEYCODES];
1577 static int nb_pending_keycodes;
1578 static QEMUTimer *key_timer;
1580 static void release_keys(void *opaque)
1584 while (nb_pending_keycodes > 0) {
1585 nb_pending_keycodes--;
1586 keycode = keycodes[nb_pending_keycodes];
1588 kbd_put_keycode(0xe0);
1589 kbd_put_keycode(keycode | 0x80);
1593 static void do_sendkey(Monitor *mon, const QDict *qdict)
1595 char keyname_buf[16];
1597 int keyname_len, keycode, i;
1598 const char *string = qdict_get_str(qdict, "string");
1599 int has_hold_time = qdict_haskey(qdict, "hold_time");
1600 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1602 if (nb_pending_keycodes > 0) {
1603 qemu_del_timer(key_timer);
1610 separator = strchr(string, '-');
1611 keyname_len = separator ? separator - string : strlen(string);
1612 if (keyname_len > 0) {
1613 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1614 if (keyname_len > sizeof(keyname_buf) - 1) {
1615 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1618 if (i == MAX_KEYCODES) {
1619 monitor_printf(mon, "too many keys\n");
1622 keyname_buf[keyname_len] = 0;
1623 keycode = get_keycode(keyname_buf);
1625 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1628 keycodes[i++] = keycode;
1632 string = separator + 1;
1634 nb_pending_keycodes = i;
1635 /* key down events */
1636 for (i = 0; i < nb_pending_keycodes; i++) {
1637 keycode = keycodes[i];
1639 kbd_put_keycode(0xe0);
1640 kbd_put_keycode(keycode & 0x7f);
1642 /* delayed key up events */
1643 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1644 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1647 static int mouse_button_state;
1649 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1652 const char *dx_str = qdict_get_str(qdict, "dx_str");
1653 const char *dy_str = qdict_get_str(qdict, "dy_str");
1654 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1655 dx = strtol(dx_str, NULL, 0);
1656 dy = strtol(dy_str, NULL, 0);
1659 dz = strtol(dz_str, NULL, 0);
1660 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1663 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1665 int button_state = qdict_get_int(qdict, "button_state");
1666 mouse_button_state = button_state;
1667 kbd_mouse_event(0, 0, 0, mouse_button_state);
1670 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1672 int size = qdict_get_int(qdict, "size");
1673 int addr = qdict_get_int(qdict, "addr");
1674 int has_index = qdict_haskey(qdict, "index");
1679 int index = qdict_get_int(qdict, "index");
1680 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1688 val = cpu_inb(addr);
1692 val = cpu_inw(addr);
1696 val = cpu_inl(addr);
1700 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1701 suffix, addr, size * 2, val);
1704 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1706 int size = qdict_get_int(qdict, "size");
1707 int addr = qdict_get_int(qdict, "addr");
1708 int val = qdict_get_int(qdict, "val");
1710 addr &= IOPORTS_MASK;
1715 cpu_outb(addr, val);
1718 cpu_outw(addr, val);
1721 cpu_outl(addr, val);
1726 static void do_boot_set(Monitor *mon, const QDict *qdict)
1729 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1731 res = qemu_boot_set(bootdevice);
1733 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1734 } else if (res > 0) {
1735 monitor_printf(mon, "setting boot device list failed\n");
1737 monitor_printf(mon, "no function defined to set boot device list for "
1738 "this architecture\n");
1743 * do_system_reset(): Issue a machine reset
1745 static int do_system_reset(Monitor *mon, const QDict *qdict,
1748 qemu_system_reset_request();
1753 * do_system_powerdown(): Issue a machine powerdown
1755 static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1758 qemu_system_powerdown_request();
1762 #if defined(TARGET_I386)
1763 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1765 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1768 pte & PG_GLOBAL_MASK ? 'G' : '-',
1769 pte & PG_PSE_MASK ? 'P' : '-',
1770 pte & PG_DIRTY_MASK ? 'D' : '-',
1771 pte & PG_ACCESSED_MASK ? 'A' : '-',
1772 pte & PG_PCD_MASK ? 'C' : '-',
1773 pte & PG_PWT_MASK ? 'T' : '-',
1774 pte & PG_USER_MASK ? 'U' : '-',
1775 pte & PG_RW_MASK ? 'W' : '-');
1778 static void tlb_info(Monitor *mon)
1782 uint32_t pgd, pde, pte;
1784 env = mon_get_cpu();
1786 if (!(env->cr[0] & CR0_PG_MASK)) {
1787 monitor_printf(mon, "PG disabled\n");
1790 pgd = env->cr[3] & ~0xfff;
1791 for(l1 = 0; l1 < 1024; l1++) {
1792 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1793 pde = le32_to_cpu(pde);
1794 if (pde & PG_PRESENT_MASK) {
1795 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1796 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1798 for(l2 = 0; l2 < 1024; l2++) {
1799 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1800 (uint8_t *)&pte, 4);
1801 pte = le32_to_cpu(pte);
1802 if (pte & PG_PRESENT_MASK) {
1803 print_pte(mon, (l1 << 22) + (l2 << 12),
1813 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1814 uint32_t end, int prot)
1817 prot1 = *plast_prot;
1818 if (prot != prot1) {
1819 if (*pstart != -1) {
1820 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1821 *pstart, end, end - *pstart,
1822 prot1 & PG_USER_MASK ? 'u' : '-',
1824 prot1 & PG_RW_MASK ? 'w' : '-');
1834 static void mem_info(Monitor *mon)
1837 int l1, l2, prot, last_prot;
1838 uint32_t pgd, pde, pte, start, end;
1840 env = mon_get_cpu();
1842 if (!(env->cr[0] & CR0_PG_MASK)) {
1843 monitor_printf(mon, "PG disabled\n");
1846 pgd = env->cr[3] & ~0xfff;
1849 for(l1 = 0; l1 < 1024; l1++) {
1850 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1851 pde = le32_to_cpu(pde);
1853 if (pde & PG_PRESENT_MASK) {
1854 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1855 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1856 mem_print(mon, &start, &last_prot, end, prot);
1858 for(l2 = 0; l2 < 1024; l2++) {
1859 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1860 (uint8_t *)&pte, 4);
1861 pte = le32_to_cpu(pte);
1862 end = (l1 << 22) + (l2 << 12);
1863 if (pte & PG_PRESENT_MASK) {
1864 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1868 mem_print(mon, &start, &last_prot, end, prot);
1873 mem_print(mon, &start, &last_prot, end, prot);
1879 #if defined(TARGET_SH4)
1881 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1883 monitor_printf(mon, " tlb%i:\t"
1884 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1885 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1886 "dirty=%hhu writethrough=%hhu\n",
1888 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1889 tlb->v, tlb->sh, tlb->c, tlb->pr,
1893 static void tlb_info(Monitor *mon)
1895 CPUState *env = mon_get_cpu();
1898 monitor_printf (mon, "ITLB:\n");
1899 for (i = 0 ; i < ITLB_SIZE ; i++)
1900 print_tlb (mon, i, &env->itlb[i]);
1901 monitor_printf (mon, "UTLB:\n");
1902 for (i = 0 ; i < UTLB_SIZE ; i++)
1903 print_tlb (mon, i, &env->utlb[i]);
1908 static void do_info_kvm_print(Monitor *mon, const QObject *data)
1912 qdict = qobject_to_qdict(data);
1914 monitor_printf(mon, "kvm support: ");
1915 if (qdict_get_bool(qdict, "present")) {
1916 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1917 "enabled" : "disabled");
1919 monitor_printf(mon, "not compiled\n");
1923 static void do_info_kvm(Monitor *mon, QObject **ret_data)
1926 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1929 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
1933 static void do_info_numa(Monitor *mon)
1938 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1939 for (i = 0; i < nb_numa_nodes; i++) {
1940 monitor_printf(mon, "node %d cpus:", i);
1941 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1942 if (env->numa_node == i) {
1943 monitor_printf(mon, " %d", env->cpu_index);
1946 monitor_printf(mon, "\n");
1947 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1952 #ifdef CONFIG_PROFILER
1957 static void do_info_profile(Monitor *mon)
1963 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1964 dev_time, dev_time / (double)get_ticks_per_sec());
1965 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1966 qemu_time, qemu_time / (double)get_ticks_per_sec());
1971 static void do_info_profile(Monitor *mon)
1973 monitor_printf(mon, "Internal profiler not compiled\n");
1977 /* Capture support */
1978 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1980 static void do_info_capture(Monitor *mon)
1985 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1986 monitor_printf(mon, "[%d]: ", i);
1987 s->ops.info (s->opaque);
1992 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1995 int n = qdict_get_int(qdict, "n");
1998 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2000 s->ops.destroy (s->opaque);
2001 QLIST_REMOVE (s, entries);
2008 static void do_wav_capture(Monitor *mon, const QDict *qdict)
2010 const char *path = qdict_get_str(qdict, "path");
2011 int has_freq = qdict_haskey(qdict, "freq");
2012 int freq = qdict_get_try_int(qdict, "freq", -1);
2013 int has_bits = qdict_haskey(qdict, "bits");
2014 int bits = qdict_get_try_int(qdict, "bits", -1);
2015 int has_channels = qdict_haskey(qdict, "nchannels");
2016 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2019 s = qemu_mallocz (sizeof (*s));
2021 freq = has_freq ? freq : 44100;
2022 bits = has_bits ? bits : 16;
2023 nchannels = has_channels ? nchannels : 2;
2025 if (wav_start_capture (s, path, freq, bits, nchannels)) {
2026 monitor_printf(mon, "Faied to add wave capture\n");
2029 QLIST_INSERT_HEAD (&capture_head, s, entries);
2033 #if defined(TARGET_I386)
2034 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
2037 int cpu_index = qdict_get_int(qdict, "cpu_index");
2039 for (env = first_cpu; env != NULL; env = env->next_cpu)
2040 if (env->cpu_index == cpu_index) {
2041 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2047 static void do_info_status_print(Monitor *mon, const QObject *data)
2051 qdict = qobject_to_qdict(data);
2053 monitor_printf(mon, "VM status: ");
2054 if (qdict_get_bool(qdict, "running")) {
2055 monitor_printf(mon, "running");
2056 if (qdict_get_bool(qdict, "singlestep")) {
2057 monitor_printf(mon, " (single step mode)");
2060 monitor_printf(mon, "paused");
2063 monitor_printf(mon, "\n");
2066 static void do_info_status(Monitor *mon, QObject **ret_data)
2068 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2069 vm_running, singlestep);
2072 static qemu_acl *find_acl(Monitor *mon, const char *name)
2074 qemu_acl *acl = qemu_acl_find(name);
2077 monitor_printf(mon, "acl: unknown list '%s'\n", name);
2082 static void do_acl_show(Monitor *mon, const QDict *qdict)
2084 const char *aclname = qdict_get_str(qdict, "aclname");
2085 qemu_acl *acl = find_acl(mon, aclname);
2086 qemu_acl_entry *entry;
2090 monitor_printf(mon, "policy: %s\n",
2091 acl->defaultDeny ? "deny" : "allow");
2092 QTAILQ_FOREACH(entry, &acl->entries, next) {
2094 monitor_printf(mon, "%d: %s %s\n", i,
2095 entry->deny ? "deny" : "allow", entry->match);
2100 static void do_acl_reset(Monitor *mon, const QDict *qdict)
2102 const char *aclname = qdict_get_str(qdict, "aclname");
2103 qemu_acl *acl = find_acl(mon, aclname);
2106 qemu_acl_reset(acl);
2107 monitor_printf(mon, "acl: removed all rules\n");
2111 static void do_acl_policy(Monitor *mon, const QDict *qdict)
2113 const char *aclname = qdict_get_str(qdict, "aclname");
2114 const char *policy = qdict_get_str(qdict, "policy");
2115 qemu_acl *acl = find_acl(mon, aclname);
2118 if (strcmp(policy, "allow") == 0) {
2119 acl->defaultDeny = 0;
2120 monitor_printf(mon, "acl: policy set to 'allow'\n");
2121 } else if (strcmp(policy, "deny") == 0) {
2122 acl->defaultDeny = 1;
2123 monitor_printf(mon, "acl: policy set to 'deny'\n");
2125 monitor_printf(mon, "acl: unknown policy '%s', "
2126 "expected 'deny' or 'allow'\n", policy);
2131 static void do_acl_add(Monitor *mon, const QDict *qdict)
2133 const char *aclname = qdict_get_str(qdict, "aclname");
2134 const char *match = qdict_get_str(qdict, "match");
2135 const char *policy = qdict_get_str(qdict, "policy");
2136 int has_index = qdict_haskey(qdict, "index");
2137 int index = qdict_get_try_int(qdict, "index", -1);
2138 qemu_acl *acl = find_acl(mon, aclname);
2142 if (strcmp(policy, "allow") == 0) {
2144 } else if (strcmp(policy, "deny") == 0) {
2147 monitor_printf(mon, "acl: unknown policy '%s', "
2148 "expected 'deny' or 'allow'\n", policy);
2152 ret = qemu_acl_insert(acl, deny, match, index);
2154 ret = qemu_acl_append(acl, deny, match);
2156 monitor_printf(mon, "acl: unable to add acl entry\n");
2158 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2162 static void do_acl_remove(Monitor *mon, const QDict *qdict)
2164 const char *aclname = qdict_get_str(qdict, "aclname");
2165 const char *match = qdict_get_str(qdict, "match");
2166 qemu_acl *acl = find_acl(mon, aclname);
2170 ret = qemu_acl_remove(acl, match);
2172 monitor_printf(mon, "acl: no matching acl entry\n");
2174 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2178 #if defined(TARGET_I386)
2179 static void do_inject_mce(Monitor *mon, const QDict *qdict)
2182 int cpu_index = qdict_get_int(qdict, "cpu_index");
2183 int bank = qdict_get_int(qdict, "bank");
2184 uint64_t status = qdict_get_int(qdict, "status");
2185 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2186 uint64_t addr = qdict_get_int(qdict, "addr");
2187 uint64_t misc = qdict_get_int(qdict, "misc");
2189 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2190 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2191 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2197 static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2199 const char *fdname = qdict_get_str(qdict, "fdname");
2203 fd = qemu_chr_get_msgfd(mon->chr);
2205 qerror_report(QERR_FD_NOT_SUPPLIED);
2209 if (qemu_isdigit(fdname[0])) {
2210 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2211 "a name not starting with a digit");
2215 QLIST_FOREACH(monfd, &mon->fds, next) {
2216 if (strcmp(monfd->name, fdname) != 0) {
2225 monfd = qemu_mallocz(sizeof(mon_fd_t));
2226 monfd->name = qemu_strdup(fdname);
2229 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
2233 static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2235 const char *fdname = qdict_get_str(qdict, "fdname");
2238 QLIST_FOREACH(monfd, &mon->fds, next) {
2239 if (strcmp(monfd->name, fdname) != 0) {
2243 QLIST_REMOVE(monfd, next);
2245 qemu_free(monfd->name);
2250 qerror_report(QERR_FD_NOT_FOUND, fdname);
2254 static void do_loadvm(Monitor *mon, const QDict *qdict)
2256 int saved_vm_running = vm_running;
2257 const char *name = qdict_get_str(qdict, "name");
2261 if (load_vmstate(name) >= 0 && saved_vm_running)
2265 int monitor_get_fd(Monitor *mon, const char *fdname)
2269 QLIST_FOREACH(monfd, &mon->fds, next) {
2272 if (strcmp(monfd->name, fdname) != 0) {
2278 /* caller takes ownership of fd */
2279 QLIST_REMOVE(monfd, next);
2280 qemu_free(monfd->name);
2289 static const mon_cmd_t mon_cmds[] = {
2290 #include "qemu-monitor.h"
2294 /* Please update qemu-monitor.hx when adding or changing commands */
2295 static const mon_cmd_t info_cmds[] = {
2300 .help = "show the version of QEMU",
2301 .user_print = do_info_version_print,
2302 .mhandler.info_new = do_info_version,
2308 .help = "list QMP available commands",
2309 .user_print = monitor_user_noop,
2310 .mhandler.info_new = do_info_commands,
2316 .help = "show the network state",
2317 .mhandler.info = do_info_network,
2323 .help = "show the character devices",
2324 .user_print = qemu_chr_info_print,
2325 .mhandler.info_new = qemu_chr_info,
2331 .help = "show the block devices",
2332 .user_print = bdrv_info_print,
2333 .mhandler.info_new = bdrv_info,
2336 .name = "blockstats",
2339 .help = "show block device statistics",
2340 .user_print = bdrv_stats_print,
2341 .mhandler.info_new = bdrv_info_stats,
2344 .name = "registers",
2347 .help = "show the cpu registers",
2348 .mhandler.info = do_info_registers,
2354 .help = "show infos for each CPU",
2355 .user_print = monitor_print_cpus,
2356 .mhandler.info_new = do_info_cpus,
2362 .help = "show the command line history",
2363 .mhandler.info = do_info_history,
2369 .help = "show the interrupts statistics (if available)",
2370 .mhandler.info = irq_info,
2376 .help = "show i8259 (PIC) state",
2377 .mhandler.info = pic_info,
2383 .help = "show PCI info",
2384 .user_print = do_pci_info_print,
2385 .mhandler.info_new = do_pci_info,
2387 #if defined(TARGET_I386) || defined(TARGET_SH4)
2392 .help = "show virtual to physical memory mappings",
2393 .mhandler.info = tlb_info,
2396 #if defined(TARGET_I386)
2401 .help = "show the active virtual memory mappings",
2402 .mhandler.info = mem_info,
2409 .help = "show dynamic compiler info",
2410 .mhandler.info = do_info_jit,
2416 .help = "show KVM information",
2417 .user_print = do_info_kvm_print,
2418 .mhandler.info_new = do_info_kvm,
2424 .help = "show NUMA information",
2425 .mhandler.info = do_info_numa,
2431 .help = "show guest USB devices",
2432 .mhandler.info = usb_info,
2438 .help = "show host USB devices",
2439 .mhandler.info = usb_host_info,
2445 .help = "show profiling information",
2446 .mhandler.info = do_info_profile,
2452 .help = "show capture information",
2453 .mhandler.info = do_info_capture,
2456 .name = "snapshots",
2459 .help = "show the currently saved VM snapshots",
2460 .mhandler.info = do_info_snapshots,
2466 .help = "show the current VM status (running|paused)",
2467 .user_print = do_info_status_print,
2468 .mhandler.info_new = do_info_status,
2474 .help = "show guest PCMCIA status",
2475 .mhandler.info = pcmcia_info,
2481 .help = "show which guest mouse is receiving events",
2482 .user_print = do_info_mice_print,
2483 .mhandler.info_new = do_info_mice,
2489 .help = "show the vnc server status",
2490 .user_print = do_info_vnc_print,
2491 .mhandler.info_new = do_info_vnc,
2497 .help = "show the current VM name",
2498 .user_print = do_info_name_print,
2499 .mhandler.info_new = do_info_name,
2505 .help = "show the current VM UUID",
2506 .user_print = do_info_uuid_print,
2507 .mhandler.info_new = do_info_uuid,
2509 #if defined(TARGET_PPC)
2514 .help = "show CPU statistics",
2515 .mhandler.info = do_info_cpu_stats,
2518 #if defined(CONFIG_SLIRP)
2523 .help = "show user network stack connection states",
2524 .mhandler.info = do_info_usernet,
2531 .help = "show migration status",
2532 .user_print = do_info_migrate_print,
2533 .mhandler.info_new = do_info_migrate,
2539 .help = "show balloon information",
2540 .user_print = monitor_print_balloon,
2541 .mhandler.info_async = do_info_balloon,
2542 .flags = MONITOR_CMD_ASYNC,
2548 .help = "show device tree",
2549 .mhandler.info = do_info_qtree,
2555 .help = "show qdev device model list",
2556 .mhandler.info = do_info_qdm,
2562 .help = "show roms",
2563 .mhandler.info = do_info_roms,
2570 /*******************************************************************/
2572 static const char *pch;
2573 static jmp_buf expr_env;
2578 typedef struct MonitorDef {
2581 target_long (*get_value)(const struct MonitorDef *md, int val);
2585 #if defined(TARGET_I386)
2586 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2588 CPUState *env = mon_get_cpu();
2589 return env->eip + env->segs[R_CS].base;
2593 #if defined(TARGET_PPC)
2594 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2596 CPUState *env = mon_get_cpu();
2601 for (i = 0; i < 8; i++)
2602 u |= env->crf[i] << (32 - (4 * i));
2607 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2609 CPUState *env = mon_get_cpu();
2613 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2615 CPUState *env = mon_get_cpu();
2619 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2621 CPUState *env = mon_get_cpu();
2622 return cpu_ppc_load_decr(env);
2625 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2627 CPUState *env = mon_get_cpu();
2628 return cpu_ppc_load_tbu(env);
2631 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2633 CPUState *env = mon_get_cpu();
2634 return cpu_ppc_load_tbl(env);
2638 #if defined(TARGET_SPARC)
2639 #ifndef TARGET_SPARC64
2640 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2642 CPUState *env = mon_get_cpu();
2644 return cpu_get_psr(env);
2648 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2650 CPUState *env = mon_get_cpu();
2651 return env->regwptr[val];
2655 static const MonitorDef monitor_defs[] = {
2658 #define SEG(name, seg) \
2659 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2660 { name ".base", offsetof(CPUState, segs[seg].base) },\
2661 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2663 { "eax", offsetof(CPUState, regs[0]) },
2664 { "ecx", offsetof(CPUState, regs[1]) },
2665 { "edx", offsetof(CPUState, regs[2]) },
2666 { "ebx", offsetof(CPUState, regs[3]) },
2667 { "esp|sp", offsetof(CPUState, regs[4]) },
2668 { "ebp|fp", offsetof(CPUState, regs[5]) },
2669 { "esi", offsetof(CPUState, regs[6]) },
2670 { "edi", offsetof(CPUState, regs[7]) },
2671 #ifdef TARGET_X86_64
2672 { "r8", offsetof(CPUState, regs[8]) },
2673 { "r9", offsetof(CPUState, regs[9]) },
2674 { "r10", offsetof(CPUState, regs[10]) },
2675 { "r11", offsetof(CPUState, regs[11]) },
2676 { "r12", offsetof(CPUState, regs[12]) },
2677 { "r13", offsetof(CPUState, regs[13]) },
2678 { "r14", offsetof(CPUState, regs[14]) },
2679 { "r15", offsetof(CPUState, regs[15]) },
2681 { "eflags", offsetof(CPUState, eflags) },
2682 { "eip", offsetof(CPUState, eip) },
2689 { "pc", 0, monitor_get_pc, },
2690 #elif defined(TARGET_PPC)
2691 /* General purpose registers */
2692 { "r0", offsetof(CPUState, gpr[0]) },
2693 { "r1", offsetof(CPUState, gpr[1]) },
2694 { "r2", offsetof(CPUState, gpr[2]) },
2695 { "r3", offsetof(CPUState, gpr[3]) },
2696 { "r4", offsetof(CPUState, gpr[4]) },
2697 { "r5", offsetof(CPUState, gpr[5]) },
2698 { "r6", offsetof(CPUState, gpr[6]) },
2699 { "r7", offsetof(CPUState, gpr[7]) },
2700 { "r8", offsetof(CPUState, gpr[8]) },
2701 { "r9", offsetof(CPUState, gpr[9]) },
2702 { "r10", offsetof(CPUState, gpr[10]) },
2703 { "r11", offsetof(CPUState, gpr[11]) },
2704 { "r12", offsetof(CPUState, gpr[12]) },
2705 { "r13", offsetof(CPUState, gpr[13]) },
2706 { "r14", offsetof(CPUState, gpr[14]) },
2707 { "r15", offsetof(CPUState, gpr[15]) },
2708 { "r16", offsetof(CPUState, gpr[16]) },
2709 { "r17", offsetof(CPUState, gpr[17]) },
2710 { "r18", offsetof(CPUState, gpr[18]) },
2711 { "r19", offsetof(CPUState, gpr[19]) },
2712 { "r20", offsetof(CPUState, gpr[20]) },
2713 { "r21", offsetof(CPUState, gpr[21]) },
2714 { "r22", offsetof(CPUState, gpr[22]) },
2715 { "r23", offsetof(CPUState, gpr[23]) },
2716 { "r24", offsetof(CPUState, gpr[24]) },
2717 { "r25", offsetof(CPUState, gpr[25]) },
2718 { "r26", offsetof(CPUState, gpr[26]) },
2719 { "r27", offsetof(CPUState, gpr[27]) },
2720 { "r28", offsetof(CPUState, gpr[28]) },
2721 { "r29", offsetof(CPUState, gpr[29]) },
2722 { "r30", offsetof(CPUState, gpr[30]) },
2723 { "r31", offsetof(CPUState, gpr[31]) },
2724 /* Floating point registers */
2725 { "f0", offsetof(CPUState, fpr[0]) },
2726 { "f1", offsetof(CPUState, fpr[1]) },
2727 { "f2", offsetof(CPUState, fpr[2]) },
2728 { "f3", offsetof(CPUState, fpr[3]) },
2729 { "f4", offsetof(CPUState, fpr[4]) },
2730 { "f5", offsetof(CPUState, fpr[5]) },
2731 { "f6", offsetof(CPUState, fpr[6]) },
2732 { "f7", offsetof(CPUState, fpr[7]) },
2733 { "f8", offsetof(CPUState, fpr[8]) },
2734 { "f9", offsetof(CPUState, fpr[9]) },
2735 { "f10", offsetof(CPUState, fpr[10]) },
2736 { "f11", offsetof(CPUState, fpr[11]) },
2737 { "f12", offsetof(CPUState, fpr[12]) },
2738 { "f13", offsetof(CPUState, fpr[13]) },
2739 { "f14", offsetof(CPUState, fpr[14]) },
2740 { "f15", offsetof(CPUState, fpr[15]) },
2741 { "f16", offsetof(CPUState, fpr[16]) },
2742 { "f17", offsetof(CPUState, fpr[17]) },
2743 { "f18", offsetof(CPUState, fpr[18]) },
2744 { "f19", offsetof(CPUState, fpr[19]) },
2745 { "f20", offsetof(CPUState, fpr[20]) },
2746 { "f21", offsetof(CPUState, fpr[21]) },
2747 { "f22", offsetof(CPUState, fpr[22]) },
2748 { "f23", offsetof(CPUState, fpr[23]) },
2749 { "f24", offsetof(CPUState, fpr[24]) },
2750 { "f25", offsetof(CPUState, fpr[25]) },
2751 { "f26", offsetof(CPUState, fpr[26]) },
2752 { "f27", offsetof(CPUState, fpr[27]) },
2753 { "f28", offsetof(CPUState, fpr[28]) },
2754 { "f29", offsetof(CPUState, fpr[29]) },
2755 { "f30", offsetof(CPUState, fpr[30]) },
2756 { "f31", offsetof(CPUState, fpr[31]) },
2757 { "fpscr", offsetof(CPUState, fpscr) },
2758 /* Next instruction pointer */
2759 { "nip|pc", offsetof(CPUState, nip) },
2760 { "lr", offsetof(CPUState, lr) },
2761 { "ctr", offsetof(CPUState, ctr) },
2762 { "decr", 0, &monitor_get_decr, },
2763 { "ccr", 0, &monitor_get_ccr, },
2764 /* Machine state register */
2765 { "msr", 0, &monitor_get_msr, },
2766 { "xer", 0, &monitor_get_xer, },
2767 { "tbu", 0, &monitor_get_tbu, },
2768 { "tbl", 0, &monitor_get_tbl, },
2769 #if defined(TARGET_PPC64)
2770 /* Address space register */
2771 { "asr", offsetof(CPUState, asr) },
2773 /* Segment registers */
2774 { "sdr1", offsetof(CPUState, sdr1) },
2775 { "sr0", offsetof(CPUState, sr[0]) },
2776 { "sr1", offsetof(CPUState, sr[1]) },
2777 { "sr2", offsetof(CPUState, sr[2]) },
2778 { "sr3", offsetof(CPUState, sr[3]) },
2779 { "sr4", offsetof(CPUState, sr[4]) },
2780 { "sr5", offsetof(CPUState, sr[5]) },
2781 { "sr6", offsetof(CPUState, sr[6]) },
2782 { "sr7", offsetof(CPUState, sr[7]) },
2783 { "sr8", offsetof(CPUState, sr[8]) },
2784 { "sr9", offsetof(CPUState, sr[9]) },
2785 { "sr10", offsetof(CPUState, sr[10]) },
2786 { "sr11", offsetof(CPUState, sr[11]) },
2787 { "sr12", offsetof(CPUState, sr[12]) },
2788 { "sr13", offsetof(CPUState, sr[13]) },
2789 { "sr14", offsetof(CPUState, sr[14]) },
2790 { "sr15", offsetof(CPUState, sr[15]) },
2791 /* Too lazy to put BATs and SPRs ... */
2792 #elif defined(TARGET_SPARC)
2793 { "g0", offsetof(CPUState, gregs[0]) },
2794 { "g1", offsetof(CPUState, gregs[1]) },
2795 { "g2", offsetof(CPUState, gregs[2]) },
2796 { "g3", offsetof(CPUState, gregs[3]) },
2797 { "g4", offsetof(CPUState, gregs[4]) },
2798 { "g5", offsetof(CPUState, gregs[5]) },
2799 { "g6", offsetof(CPUState, gregs[6]) },
2800 { "g7", offsetof(CPUState, gregs[7]) },
2801 { "o0", 0, monitor_get_reg },
2802 { "o1", 1, monitor_get_reg },
2803 { "o2", 2, monitor_get_reg },
2804 { "o3", 3, monitor_get_reg },
2805 { "o4", 4, monitor_get_reg },
2806 { "o5", 5, monitor_get_reg },
2807 { "o6", 6, monitor_get_reg },
2808 { "o7", 7, monitor_get_reg },
2809 { "l0", 8, monitor_get_reg },
2810 { "l1", 9, monitor_get_reg },
2811 { "l2", 10, monitor_get_reg },
2812 { "l3", 11, monitor_get_reg },
2813 { "l4", 12, monitor_get_reg },
2814 { "l5", 13, monitor_get_reg },
2815 { "l6", 14, monitor_get_reg },
2816 { "l7", 15, monitor_get_reg },
2817 { "i0", 16, monitor_get_reg },
2818 { "i1", 17, monitor_get_reg },
2819 { "i2", 18, monitor_get_reg },
2820 { "i3", 19, monitor_get_reg },
2821 { "i4", 20, monitor_get_reg },
2822 { "i5", 21, monitor_get_reg },
2823 { "i6", 22, monitor_get_reg },
2824 { "i7", 23, monitor_get_reg },
2825 { "pc", offsetof(CPUState, pc) },
2826 { "npc", offsetof(CPUState, npc) },
2827 { "y", offsetof(CPUState, y) },
2828 #ifndef TARGET_SPARC64
2829 { "psr", 0, &monitor_get_psr, },
2830 { "wim", offsetof(CPUState, wim) },
2832 { "tbr", offsetof(CPUState, tbr) },
2833 { "fsr", offsetof(CPUState, fsr) },
2834 { "f0", offsetof(CPUState, fpr[0]) },
2835 { "f1", offsetof(CPUState, fpr[1]) },
2836 { "f2", offsetof(CPUState, fpr[2]) },
2837 { "f3", offsetof(CPUState, fpr[3]) },
2838 { "f4", offsetof(CPUState, fpr[4]) },
2839 { "f5", offsetof(CPUState, fpr[5]) },
2840 { "f6", offsetof(CPUState, fpr[6]) },
2841 { "f7", offsetof(CPUState, fpr[7]) },
2842 { "f8", offsetof(CPUState, fpr[8]) },
2843 { "f9", offsetof(CPUState, fpr[9]) },
2844 { "f10", offsetof(CPUState, fpr[10]) },
2845 { "f11", offsetof(CPUState, fpr[11]) },
2846 { "f12", offsetof(CPUState, fpr[12]) },
2847 { "f13", offsetof(CPUState, fpr[13]) },
2848 { "f14", offsetof(CPUState, fpr[14]) },
2849 { "f15", offsetof(CPUState, fpr[15]) },
2850 { "f16", offsetof(CPUState, fpr[16]) },
2851 { "f17", offsetof(CPUState, fpr[17]) },
2852 { "f18", offsetof(CPUState, fpr[18]) },
2853 { "f19", offsetof(CPUState, fpr[19]) },
2854 { "f20", offsetof(CPUState, fpr[20]) },
2855 { "f21", offsetof(CPUState, fpr[21]) },
2856 { "f22", offsetof(CPUState, fpr[22]) },
2857 { "f23", offsetof(CPUState, fpr[23]) },
2858 { "f24", offsetof(CPUState, fpr[24]) },
2859 { "f25", offsetof(CPUState, fpr[25]) },
2860 { "f26", offsetof(CPUState, fpr[26]) },
2861 { "f27", offsetof(CPUState, fpr[27]) },
2862 { "f28", offsetof(CPUState, fpr[28]) },
2863 { "f29", offsetof(CPUState, fpr[29]) },
2864 { "f30", offsetof(CPUState, fpr[30]) },
2865 { "f31", offsetof(CPUState, fpr[31]) },
2866 #ifdef TARGET_SPARC64
2867 { "f32", offsetof(CPUState, fpr[32]) },
2868 { "f34", offsetof(CPUState, fpr[34]) },
2869 { "f36", offsetof(CPUState, fpr[36]) },
2870 { "f38", offsetof(CPUState, fpr[38]) },
2871 { "f40", offsetof(CPUState, fpr[40]) },
2872 { "f42", offsetof(CPUState, fpr[42]) },
2873 { "f44", offsetof(CPUState, fpr[44]) },
2874 { "f46", offsetof(CPUState, fpr[46]) },
2875 { "f48", offsetof(CPUState, fpr[48]) },
2876 { "f50", offsetof(CPUState, fpr[50]) },
2877 { "f52", offsetof(CPUState, fpr[52]) },
2878 { "f54", offsetof(CPUState, fpr[54]) },
2879 { "f56", offsetof(CPUState, fpr[56]) },
2880 { "f58", offsetof(CPUState, fpr[58]) },
2881 { "f60", offsetof(CPUState, fpr[60]) },
2882 { "f62", offsetof(CPUState, fpr[62]) },
2883 { "asi", offsetof(CPUState, asi) },
2884 { "pstate", offsetof(CPUState, pstate) },
2885 { "cansave", offsetof(CPUState, cansave) },
2886 { "canrestore", offsetof(CPUState, canrestore) },
2887 { "otherwin", offsetof(CPUState, otherwin) },
2888 { "wstate", offsetof(CPUState, wstate) },
2889 { "cleanwin", offsetof(CPUState, cleanwin) },
2890 { "fprs", offsetof(CPUState, fprs) },
2896 static void expr_error(Monitor *mon, const char *msg)
2898 monitor_printf(mon, "%s\n", msg);
2899 longjmp(expr_env, 1);
2902 /* return 0 if OK, -1 if not found */
2903 static int get_monitor_def(target_long *pval, const char *name)
2905 const MonitorDef *md;
2908 for(md = monitor_defs; md->name != NULL; md++) {
2909 if (compare_cmd(name, md->name)) {
2910 if (md->get_value) {
2911 *pval = md->get_value(md, md->offset);
2913 CPUState *env = mon_get_cpu();
2914 ptr = (uint8_t *)env + md->offset;
2917 *pval = *(int32_t *)ptr;
2920 *pval = *(target_long *)ptr;
2933 static void next(void)
2937 while (qemu_isspace(*pch))
2942 static int64_t expr_sum(Monitor *mon);
2944 static int64_t expr_unary(Monitor *mon)
2953 n = expr_unary(mon);
2957 n = -expr_unary(mon);
2961 n = ~expr_unary(mon);
2967 expr_error(mon, "')' expected");
2974 expr_error(mon, "character constant expected");
2978 expr_error(mon, "missing terminating \' character");
2988 while ((*pch >= 'a' && *pch <= 'z') ||
2989 (*pch >= 'A' && *pch <= 'Z') ||
2990 (*pch >= '0' && *pch <= '9') ||
2991 *pch == '_' || *pch == '.') {
2992 if ((q - buf) < sizeof(buf) - 1)
2996 while (qemu_isspace(*pch))
2999 ret = get_monitor_def(®, buf);
3001 expr_error(mon, "unknown register");
3006 expr_error(mon, "unexpected end of expression");
3010 #if TARGET_PHYS_ADDR_BITS > 32
3011 n = strtoull(pch, &p, 0);
3013 n = strtoul(pch, &p, 0);
3016 expr_error(mon, "invalid char in expression");
3019 while (qemu_isspace(*pch))
3027 static int64_t expr_prod(Monitor *mon)
3032 val = expr_unary(mon);
3035 if (op != '*' && op != '/' && op != '%')
3038 val2 = expr_unary(mon);
3047 expr_error(mon, "division by zero");
3058 static int64_t expr_logic(Monitor *mon)
3063 val = expr_prod(mon);
3066 if (op != '&' && op != '|' && op != '^')
3069 val2 = expr_prod(mon);
3086 static int64_t expr_sum(Monitor *mon)
3091 val = expr_logic(mon);
3094 if (op != '+' && op != '-')
3097 val2 = expr_logic(mon);
3106 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3109 if (setjmp(expr_env)) {
3113 while (qemu_isspace(*pch))
3115 *pval = expr_sum(mon);
3120 static int get_double(Monitor *mon, double *pval, const char **pp)
3122 const char *p = *pp;
3126 d = strtod(p, &tailp);
3128 monitor_printf(mon, "Number expected\n");
3131 if (d != d || d - d != 0) {
3132 /* NaN or infinity */
3133 monitor_printf(mon, "Bad number\n");
3141 static int get_str(char *buf, int buf_size, const char **pp)
3149 while (qemu_isspace(*p))
3159 while (*p != '\0' && *p != '\"') {
3175 qemu_printf("unsupported escape code: '\\%c'\n", c);
3178 if ((q - buf) < buf_size - 1) {
3182 if ((q - buf) < buf_size - 1) {
3189 qemu_printf("unterminated string\n");
3194 while (*p != '\0' && !qemu_isspace(*p)) {
3195 if ((q - buf) < buf_size - 1) {
3207 * Store the command-name in cmdname, and return a pointer to
3208 * the remaining of the command string.
3210 static const char *get_command_name(const char *cmdline,
3211 char *cmdname, size_t nlen)
3214 const char *p, *pstart;
3217 while (qemu_isspace(*p))
3222 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3227 memcpy(cmdname, pstart, len);
3228 cmdname[len] = '\0';
3233 * Read key of 'type' into 'key' and return the current
3236 static char *key_get_info(const char *type, char **key)
3244 p = strchr(type, ':');
3251 str = qemu_malloc(len + 1);
3252 memcpy(str, type, len);
3259 static int default_fmt_format = 'x';
3260 static int default_fmt_size = 4;
3264 static int is_valid_option(const char *c, const char *typestr)
3272 typestr = strstr(typestr, option);
3273 return (typestr != NULL);
3276 static const mon_cmd_t *monitor_find_command(const char *cmdname)
3278 const mon_cmd_t *cmd;
3280 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3281 if (compare_cmd(cmdname, cmd->name)) {
3289 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3290 const char *cmdline,
3293 const char *p, *typestr;
3295 const mon_cmd_t *cmd;
3301 monitor_printf(mon, "command='%s'\n", cmdline);
3304 /* extract the command name */
3305 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3309 cmd = monitor_find_command(cmdname);
3311 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
3315 /* parse the parameters */
3316 typestr = cmd->args_type;
3318 typestr = key_get_info(typestr, &key);
3330 while (qemu_isspace(*p))
3332 if (*typestr == '?') {
3335 /* no optional string: NULL argument */
3339 ret = get_str(buf, sizeof(buf), &p);
3343 monitor_printf(mon, "%s: filename expected\n",
3347 monitor_printf(mon, "%s: block device name expected\n",
3351 monitor_printf(mon, "%s: string expected\n", cmdname);
3356 qdict_put(qdict, key, qstring_from_str(buf));
3361 QemuOptsList *opts_list;
3364 opts_list = qemu_find_opts(key);
3365 if (!opts_list || opts_list->desc->name) {
3368 while (qemu_isspace(*p)) {
3373 if (get_str(buf, sizeof(buf), &p) < 0) {
3376 opts = qemu_opts_parse(opts_list, buf, 1);
3380 qemu_opts_to_qdict(opts, qdict);
3381 qemu_opts_del(opts);
3386 int count, format, size;
3388 while (qemu_isspace(*p))
3394 if (qemu_isdigit(*p)) {
3396 while (qemu_isdigit(*p)) {
3397 count = count * 10 + (*p - '0');
3435 if (*p != '\0' && !qemu_isspace(*p)) {
3436 monitor_printf(mon, "invalid char in format: '%c'\n",
3441 format = default_fmt_format;
3442 if (format != 'i') {
3443 /* for 'i', not specifying a size gives -1 as size */
3445 size = default_fmt_size;
3446 default_fmt_size = size;
3448 default_fmt_format = format;
3451 format = default_fmt_format;
3452 if (format != 'i') {
3453 size = default_fmt_size;
3458 qdict_put(qdict, "count", qint_from_int(count));
3459 qdict_put(qdict, "format", qint_from_int(format));
3460 qdict_put(qdict, "size", qint_from_int(size));
3469 while (qemu_isspace(*p))
3471 if (*typestr == '?' || *typestr == '.') {
3472 if (*typestr == '?') {
3480 while (qemu_isspace(*p))
3489 if (get_expr(mon, &val, &p))
3491 /* Check if 'i' is greater than 32-bit */
3492 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3493 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3494 monitor_printf(mon, "integer is for 32-bit values\n");
3496 } else if (c == 'M') {
3499 qdict_put(qdict, key, qint_from_int(val));
3507 while (qemu_isspace(*p))
3509 if (*typestr == '?') {
3515 if (get_double(mon, &val, &p) < 0) {
3518 if (c == 'f' && *p) {
3521 val *= 1 << 10; p++; break;
3523 val *= 1 << 20; p++; break;
3525 val *= 1 << 30; p++; break;
3528 if (c == 'T' && p[0] && p[1] == 's') {
3531 val /= 1e3; p += 2; break;
3533 val /= 1e6; p += 2; break;
3535 val /= 1e9; p += 2; break;
3538 if (*p && !qemu_isspace(*p)) {
3539 monitor_printf(mon, "Unknown unit suffix\n");
3542 qdict_put(qdict, key, qfloat_from_double(val));
3550 while (qemu_isspace(*p)) {
3554 while (qemu_isgraph(*p)) {
3557 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3559 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3562 monitor_printf(mon, "Expected 'on' or 'off'\n");
3565 qdict_put(qdict, key, qbool_from_int(val));
3570 const char *tmp = p;
3577 while (qemu_isspace(*p))
3582 if(!is_valid_option(p, typestr)) {
3584 monitor_printf(mon, "%s: unsupported option -%c\n",
3596 qdict_put(qdict, key, qbool_from_int(1));
3603 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3609 /* check that all arguments were parsed */
3610 while (qemu_isspace(*p))
3613 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3625 void monitor_set_error(Monitor *mon, QError *qerror)
3627 /* report only the first error */
3629 mon->error = qerror;
3631 MON_DEBUG("Additional error report at %s:%d\n",
3632 qerror->file, qerror->linenr);
3637 static int is_async_return(const QObject *data)
3639 if (data && qobject_type(data) == QTYPE_QDICT) {
3640 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3646 static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3648 if (monitor_ctrl_mode(mon)) {
3649 if (ret && !monitor_has_error(mon)) {
3651 * If it returns failure, it must have passed on error.
3653 * Action: Report an internal error to the client if in QMP.
3655 qerror_report(QERR_UNDEFINED_ERROR);
3656 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3660 #ifdef CONFIG_DEBUG_MONITOR
3661 if (!ret && monitor_has_error(mon)) {
3663 * If it returns success, it must not have passed an error.
3665 * Action: Report the passed error to the client.
3667 MON_DEBUG("command '%s' returned success but passed an error\n",
3671 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3673 * Handlers should not call Monitor print functions.
3675 * Action: Ignore them in QMP.
3677 * (XXX: we don't check any 'info' or 'query' command here
3678 * because the user print function _is_ called by do_info(), hence
3679 * we will trigger this check. This problem will go away when we
3680 * make 'query' commands real and kill do_info())
3682 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3683 cmd->name, mon_print_count_get(mon));
3687 assert(!monitor_has_error(mon));
3688 QDECREF(mon->error);
3693 static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3694 const QDict *params)
3697 QObject *data = NULL;
3699 mon_print_count_init(mon);
3701 ret = cmd->mhandler.cmd_new(mon, params, &data);
3702 handler_audit(mon, cmd, ret);
3704 if (is_async_return(data)) {
3706 * Asynchronous commands have no initial return data but they can
3707 * generate errors. Data is returned via the async completion handler.
3709 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3710 monitor_protocol_emitter(mon, NULL);
3712 } else if (monitor_ctrl_mode(mon)) {
3713 /* Monitor Protocol */
3714 monitor_protocol_emitter(mon, data);
3718 cmd->user_print(mon, data);
3721 qobject_decref(data);
3724 static void handle_user_command(Monitor *mon, const char *cmdline)
3727 const mon_cmd_t *cmd;
3729 qdict = qdict_new();
3731 cmd = monitor_parse_command(mon, cmdline, qdict);
3735 if (monitor_handler_is_async(cmd)) {
3736 user_async_cmd_handler(mon, cmd, qdict);
3737 } else if (monitor_handler_ported(cmd)) {
3738 monitor_call_handler(mon, cmd, qdict);
3740 cmd->mhandler.cmd(mon, qdict);
3747 static void cmd_completion(const char *name, const char *list)
3749 const char *p, *pstart;
3758 p = pstart + strlen(pstart);
3760 if (len > sizeof(cmd) - 2)
3761 len = sizeof(cmd) - 2;
3762 memcpy(cmd, pstart, len);
3764 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3765 readline_add_completion(cur_mon->rs, cmd);
3773 static void file_completion(const char *input)
3778 char file[1024], file_prefix[1024];
3782 p = strrchr(input, '/');
3785 pstrcpy(file_prefix, sizeof(file_prefix), input);
3786 pstrcpy(path, sizeof(path), ".");
3788 input_path_len = p - input + 1;
3789 memcpy(path, input, input_path_len);
3790 if (input_path_len > sizeof(path) - 1)
3791 input_path_len = sizeof(path) - 1;
3792 path[input_path_len] = '\0';
3793 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3795 #ifdef DEBUG_COMPLETION
3796 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3797 input, path, file_prefix);
3799 ffs = opendir(path);
3807 if (strstart(d->d_name, file_prefix, NULL)) {
3808 memcpy(file, input, input_path_len);
3809 if (input_path_len < sizeof(file))
3810 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3812 /* stat the file to find out if it's a directory.
3813 * In that case add a slash to speed up typing long paths
3816 if(S_ISDIR(sb.st_mode))
3817 pstrcat(file, sizeof(file), "/");
3818 readline_add_completion(cur_mon->rs, file);
3824 static void block_completion_it(void *opaque, BlockDriverState *bs)
3826 const char *name = bdrv_get_device_name(bs);
3827 const char *input = opaque;
3829 if (input[0] == '\0' ||
3830 !strncmp(name, (char *)input, strlen(input))) {
3831 readline_add_completion(cur_mon->rs, name);
3835 /* NOTE: this parser is an approximate form of the real command parser */
3836 static void parse_cmdline(const char *cmdline,
3837 int *pnb_args, char **args)
3846 while (qemu_isspace(*p))
3850 if (nb_args >= MAX_ARGS)
3852 ret = get_str(buf, sizeof(buf), &p);
3853 args[nb_args] = qemu_strdup(buf);
3858 *pnb_args = nb_args;
3861 static const char *next_arg_type(const char *typestr)
3863 const char *p = strchr(typestr, ':');
3864 return (p != NULL ? ++p : typestr);
3867 static void monitor_find_completion(const char *cmdline)
3869 const char *cmdname;
3870 char *args[MAX_ARGS];
3871 int nb_args, i, len;
3872 const char *ptype, *str;
3873 const mon_cmd_t *cmd;
3876 parse_cmdline(cmdline, &nb_args, args);
3877 #ifdef DEBUG_COMPLETION
3878 for(i = 0; i < nb_args; i++) {
3879 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3883 /* if the line ends with a space, it means we want to complete the
3885 len = strlen(cmdline);
3886 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3887 if (nb_args >= MAX_ARGS) {
3890 args[nb_args++] = qemu_strdup("");
3893 /* command completion */
3898 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3899 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3900 cmd_completion(cmdname, cmd->name);
3903 /* find the command */
3904 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3905 if (compare_cmd(args[0], cmd->name)) {
3913 ptype = next_arg_type(cmd->args_type);
3914 for(i = 0; i < nb_args - 2; i++) {
3915 if (*ptype != '\0') {
3916 ptype = next_arg_type(ptype);
3917 while (*ptype == '?')
3918 ptype = next_arg_type(ptype);
3921 str = args[nb_args - 1];
3922 if (*ptype == '-' && ptype[1] != '\0') {
3923 ptype = next_arg_type(ptype);
3927 /* file completion */
3928 readline_set_completion_index(cur_mon->rs, strlen(str));
3929 file_completion(str);
3932 /* block device name completion */
3933 readline_set_completion_index(cur_mon->rs, strlen(str));
3934 bdrv_iterate(block_completion_it, (void *)str);
3937 /* XXX: more generic ? */
3938 if (!strcmp(cmd->name, "info")) {
3939 readline_set_completion_index(cur_mon->rs, strlen(str));
3940 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3941 cmd_completion(str, cmd->name);
3943 } else if (!strcmp(cmd->name, "sendkey")) {
3944 char *sep = strrchr(str, '-');
3947 readline_set_completion_index(cur_mon->rs, strlen(str));
3948 for(key = key_defs; key->name != NULL; key++) {
3949 cmd_completion(str, key->name);
3951 } else if (!strcmp(cmd->name, "help|?")) {
3952 readline_set_completion_index(cur_mon->rs, strlen(str));
3953 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3954 cmd_completion(str, cmd->name);
3964 for (i = 0; i < nb_args; i++) {
3969 static int monitor_can_read(void *opaque)
3971 Monitor *mon = opaque;
3973 return (mon->suspend_cnt == 0) ? 1 : 0;
3976 static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
3978 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
3979 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
3983 * Argument validation rules:
3985 * 1. The argument must exist in cmd_args qdict
3986 * 2. The argument type must be the expected one
3988 * Special case: If the argument doesn't exist in cmd_args and
3989 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3990 * checking is skipped for it.
3992 static int check_client_args_type(const QDict *client_args,
3993 const QDict *cmd_args, int flags)
3995 const QDictEntry *ent;
3997 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4000 const QObject *client_arg = qdict_entry_value(ent);
4001 const char *client_arg_name = qdict_entry_key(ent);
4003 obj = qdict_get(cmd_args, client_arg_name);
4005 if (flags & QMP_ACCEPT_UNKNOWNS) {
4006 /* handler accepts unknowns */
4009 /* client arg doesn't exist */
4010 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4014 arg_type = qobject_to_qstring(obj);
4015 assert(arg_type != NULL);
4017 /* check if argument's type is correct */
4018 switch (qstring_get_str(arg_type)[0]) {
4022 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4023 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4031 if (qobject_type(client_arg) != QTYPE_QINT) {
4032 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4039 if (qobject_type(client_arg) != QTYPE_QINT &&
4040 qobject_type(client_arg) != QTYPE_QFLOAT) {
4041 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4048 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4049 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4055 assert(flags & QMP_ACCEPT_UNKNOWNS);
4060 * These types are not supported by QMP and thus are not
4061 * handled here. Fall through.
4072 * - Check if the client has passed all mandatory args
4073 * - Set special flags for argument validation
4075 static int check_mandatory_args(const QDict *cmd_args,
4076 const QDict *client_args, int *flags)
4078 const QDictEntry *ent;
4080 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4081 const char *cmd_arg_name = qdict_entry_key(ent);
4082 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4083 assert(type != NULL);
4085 if (qstring_get_str(type)[0] == 'O') {
4086 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4087 *flags |= QMP_ACCEPT_UNKNOWNS;
4088 } else if (qstring_get_str(type)[0] != '-' &&
4089 qstring_get_str(type)[1] != '?' &&
4090 !qdict_haskey(client_args, cmd_arg_name)) {
4091 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4099 static QDict *qdict_from_args_type(const char *args_type)
4103 QString *key, *type, *cur_qs;
4105 assert(args_type != NULL);
4107 qdict = qdict_new();
4109 if (args_type == NULL || args_type[0] == '\0') {
4110 /* no args, empty qdict */
4114 key = qstring_new();
4115 type = qstring_new();
4120 switch (args_type[i]) {
4123 qdict_put(qdict, qstring_get_str(key), type);
4125 if (args_type[i] == '\0') {
4128 type = qstring_new(); /* qdict has ref */
4129 cur_qs = key = qstring_new();
4135 qstring_append_chr(cur_qs, args_type[i]);
4145 * Client argument checking rules:
4147 * 1. Client must provide all mandatory arguments
4148 * 2. Each argument provided by the client must be expected
4149 * 3. Each argument provided by the client must have the type expected
4152 static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4157 cmd_args = qdict_from_args_type(cmd->args_type);
4160 err = check_mandatory_args(cmd_args, client_args, &flags);
4165 err = check_client_args_type(client_args, cmd_args, flags);
4173 * Input object checking rules
4175 * 1. Input object must be a dict
4176 * 2. The "execute" key must exist
4177 * 3. The "execute" key must be a string
4178 * 4. If the "arguments" key exists, it must be a dict
4179 * 5. If the "id" key exists, it can be anything (ie. json-value)
4180 * 6. Any argument not listed above is considered invalid
4182 static QDict *qmp_check_input_obj(QObject *input_obj)
4184 const QDictEntry *ent;
4185 int has_exec_key = 0;
4188 if (qobject_type(input_obj) != QTYPE_QDICT) {
4189 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4193 input_dict = qobject_to_qdict(input_obj);
4195 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4196 const char *arg_name = qdict_entry_key(ent);
4197 const QObject *arg_obj = qdict_entry_value(ent);
4199 if (!strcmp(arg_name, "execute")) {
4200 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4201 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4206 } else if (!strcmp(arg_name, "arguments")) {
4207 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4208 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4212 } else if (!strcmp(arg_name, "id")) {
4213 /* FIXME: check duplicated IDs for async commands */
4215 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4220 if (!has_exec_key) {
4221 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4228 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4232 QDict *input, *args;
4233 const mon_cmd_t *cmd;
4234 Monitor *mon = cur_mon;
4235 const char *cmd_name, *info_item;
4239 obj = json_parser_parse(tokens, NULL);
4241 // FIXME: should be triggered in json_parser_parse()
4242 qerror_report(QERR_JSON_PARSING);
4244 } else if (qobject_type(obj) != QTYPE_QDICT) {
4245 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4246 qobject_decref(obj);
4250 input = qmp_check_input_obj(obj);
4252 qobject_decref(obj);
4256 mon->mc->id = qdict_get(input, "id");
4257 qobject_incref(mon->mc->id);
4259 obj = qdict_get(input, "execute");
4261 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4263 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4264 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute", "string");
4268 cmd_name = qstring_get_str(qobject_to_qstring(obj));
4270 if (invalid_qmp_mode(mon, cmd_name)) {
4271 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4276 * XXX: We need this special case until we get info handlers
4277 * converted into 'query-' commands
4279 if (compare_cmd(cmd_name, "info")) {
4280 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4282 } else if (strstart(cmd_name, "query-", &info_item)) {
4283 cmd = monitor_find_command("info");
4284 qdict_put_obj(input, "arguments",
4285 qobject_from_jsonf("{ 'item': %s }", info_item));
4287 cmd = monitor_find_command(cmd_name);
4288 if (!cmd || !monitor_handler_ported(cmd)) {
4289 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4294 obj = qdict_get(input, "arguments");
4297 } else if (qobject_type(obj) != QTYPE_QDICT) {
4298 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments", "object");
4301 args = qobject_to_qdict(obj);
4307 err = qmp_check_client_args(cmd, args);
4312 if (monitor_handler_is_async(cmd)) {
4313 err = qmp_async_cmd_handler(mon, cmd, args);
4315 /* emit the error response */
4319 monitor_call_handler(mon, cmd, args);
4326 monitor_protocol_emitter(mon, NULL);
4332 * monitor_control_read(): Read and handle QMP input
4334 static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4336 Monitor *old_mon = cur_mon;
4340 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
4345 static void monitor_read(void *opaque, const uint8_t *buf, int size)
4347 Monitor *old_mon = cur_mon;
4353 for (i = 0; i < size; i++)
4354 readline_handle_byte(cur_mon->rs, buf[i]);
4356 if (size == 0 || buf[size - 1] != 0)
4357 monitor_printf(cur_mon, "corrupted command\n");
4359 handle_user_command(cur_mon, (char *)buf);
4365 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
4367 monitor_suspend(mon);
4368 handle_user_command(mon, cmdline);
4369 monitor_resume(mon);
4372 int monitor_suspend(Monitor *mon)
4380 void monitor_resume(Monitor *mon)
4384 if (--mon->suspend_cnt == 0)
4385 readline_show_prompt(mon->rs);
4388 static QObject *get_qmp_greeting(void)
4392 do_info_version(NULL, &ver);
4393 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4397 * monitor_control_event(): Print QMP gretting
4399 static void monitor_control_event(void *opaque, int event)
4402 Monitor *mon = opaque;
4405 case CHR_EVENT_OPENED:
4406 mon->mc->command_mode = 0;
4407 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4408 data = get_qmp_greeting();
4409 monitor_json_emitter(mon, data);
4410 qobject_decref(data);
4412 case CHR_EVENT_CLOSED:
4413 json_message_parser_destroy(&mon->mc->parser);
4418 static void monitor_event(void *opaque, int event)
4420 Monitor *mon = opaque;
4423 case CHR_EVENT_MUX_IN:
4425 if (mon->reset_seen) {
4426 readline_restart(mon->rs);
4427 monitor_resume(mon);
4430 mon->suspend_cnt = 0;
4434 case CHR_EVENT_MUX_OUT:
4435 if (mon->reset_seen) {
4436 if (mon->suspend_cnt == 0) {
4437 monitor_printf(mon, "\n");
4440 monitor_suspend(mon);
4447 case CHR_EVENT_OPENED:
4448 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4449 "information\n", QEMU_VERSION);
4450 if (!mon->mux_out) {
4451 readline_show_prompt(mon->rs);
4453 mon->reset_seen = 1;
4467 void monitor_init(CharDriverState *chr, int flags)
4469 static int is_first_init = 1;
4472 if (is_first_init) {
4473 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
4477 mon = qemu_mallocz(sizeof(*mon));
4481 if (flags & MONITOR_USE_READLINE) {
4482 mon->rs = readline_init(mon, monitor_find_completion);
4483 monitor_read_command(mon, 0);
4486 if (monitor_ctrl_mode(mon)) {
4487 mon->mc = qemu_mallocz(sizeof(MonitorControl));
4488 /* Control mode requires special handlers */
4489 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4490 monitor_control_event, mon);
4492 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4493 monitor_event, mon);
4496 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4497 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4501 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
4503 BlockDriverState *bs = opaque;
4506 if (bdrv_set_key(bs, password) != 0) {
4507 monitor_printf(mon, "invalid password\n");
4510 if (mon->password_completion_cb)
4511 mon->password_completion_cb(mon->password_opaque, ret);
4513 monitor_read_command(mon, 1);
4516 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4517 BlockDriverCompletionFunc *completion_cb,
4522 if (!bdrv_key_required(bs)) {
4524 completion_cb(opaque, 0);
4528 if (monitor_ctrl_mode(mon)) {
4529 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4533 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4534 bdrv_get_encrypted_filename(bs));
4536 mon->password_completion_cb = completion_cb;
4537 mon->password_opaque = opaque;
4539 err = monitor_read_password(mon, bdrv_password_cb, bs);
4541 if (err && completion_cb)
4542 completion_cb(opaque, err);