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"
57 #include "json-streamer.h"
58 #include "json-parser.h"
62 //#define DEBUG_COMPLETION
68 * 'B' block device name
69 * 's' string (accept optional quote)
71 * 'l' target long (32 or 64 bit)
72 * 'M' just like 'l', except in user mode the value is
73 * multiplied by 2^20 (think Mebibyte)
75 * user mode accepts an optional G, g, M, m, K, k suffix,
76 * which multiplies the value by 2^30 for suffixes G and
77 * g, 2^20 for M and m, 2^10 for K and k
79 * user mode accepts an optional ms, us, ns suffix,
80 * which divides the value by 1e3, 1e6, 1e9, respectively
81 * '/' optional gdb-like print format (like "/10x")
83 * '?' optional type (for all types, except '/')
84 * '.' other form of optional type (for 'i' and 'l')
85 * '-' optional parameter (eg. '-f')
89 typedef struct MonitorCompletionData MonitorCompletionData
;
90 struct MonitorCompletionData
{
92 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
95 typedef struct mon_cmd_t
{
97 const char *args_type
;
100 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
101 int (*cmd_new_ret
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
103 void (*info
)(Monitor
*mon
);
104 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
105 int (*info_async
)(Monitor
*mon
, MonitorCompletion
*cb
, void *opaque
);
106 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
107 void (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
108 int (*cmd_async
)(Monitor
*mon
, const QDict
*params
,
109 MonitorCompletion
*cb
, void *opaque
);
114 /* file descriptors passed via SCM_RIGHTS */
115 typedef struct mon_fd_t mon_fd_t
;
119 QLIST_ENTRY(mon_fd_t
) next
;
122 typedef struct MonitorControl
{
125 JSONMessageParser parser
;
130 CharDriverState
*chr
;
135 uint8_t outbuf
[1024];
140 BlockDriverCompletionFunc
*password_completion_cb
;
141 void *password_opaque
;
143 QLIST_HEAD(,mon_fd_t
) fds
;
144 QLIST_ENTRY(Monitor
) entry
;
147 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
149 static const mon_cmd_t mon_cmds
[];
150 static const mon_cmd_t info_cmds
[];
152 Monitor
*cur_mon
= NULL
;
154 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
157 static inline int qmp_cmd_mode(const Monitor
*mon
)
159 return (mon
->mc
? mon
->mc
->command_mode
: 0);
162 /* Return true if in control mode, false otherwise */
163 static inline int monitor_ctrl_mode(const Monitor
*mon
)
165 return (mon
->flags
& MONITOR_USE_CONTROL
);
168 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
173 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
175 readline_show_prompt(mon
->rs
);
178 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
181 if (monitor_ctrl_mode(mon
)) {
182 qemu_error_new(QERR_MISSING_PARAMETER
, "password");
184 } else if (mon
->rs
) {
185 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
186 /* prompt is printed on return from the command handler */
189 monitor_printf(mon
, "terminal does not support password prompting\n");
194 void monitor_flush(Monitor
*mon
)
196 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
197 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
198 mon
->outbuf_index
= 0;
202 /* flush at every end of line or if the buffer is full */
203 static void monitor_puts(Monitor
*mon
, const char *str
)
212 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
213 mon
->outbuf
[mon
->outbuf_index
++] = c
;
214 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
220 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
225 if (mon
->mc
&& !mon
->mc
->print_enabled
) {
226 qemu_error_new(QERR_UNDEFINED_ERROR
);
229 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
230 monitor_puts(mon
, buf
);
234 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
238 monitor_vprintf(mon
, fmt
, ap
);
242 void monitor_print_filename(Monitor
*mon
, const char *filename
)
246 for (i
= 0; filename
[i
]; i
++) {
247 switch (filename
[i
]) {
251 monitor_printf(mon
, "\\%c", filename
[i
]);
254 monitor_printf(mon
, "\\t");
257 monitor_printf(mon
, "\\r");
260 monitor_printf(mon
, "\\n");
263 monitor_printf(mon
, "%c", filename
[i
]);
269 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
273 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
278 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
280 static inline int monitor_handler_ported(const mon_cmd_t
*cmd
)
282 return cmd
->user_print
!= NULL
;
285 static inline bool monitor_handler_is_async(const mon_cmd_t
*cmd
)
287 return cmd
->async
!= 0;
290 static inline int monitor_has_error(const Monitor
*mon
)
292 return mon
->error
!= NULL
;
295 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
299 json
= qobject_to_json(data
);
300 assert(json
!= NULL
);
302 mon
->mc
->print_enabled
= 1;
303 monitor_printf(mon
, "%s\n", qstring_get_str(json
));
304 mon
->mc
->print_enabled
= 0;
309 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
315 if (!monitor_has_error(mon
)) {
316 /* success response */
318 qobject_incref(data
);
319 qdict_put_obj(qmp
, "return", data
);
321 /* return an empty QDict by default */
322 qdict_put(qmp
, "return", qdict_new());
326 qdict_put(mon
->error
->error
, "desc", qerror_human(mon
->error
));
327 qdict_put(qmp
, "error", mon
->error
->error
);
328 QINCREF(mon
->error
->error
);
334 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
338 monitor_json_emitter(mon
, QOBJECT(qmp
));
342 static void timestamp_put(QDict
*qdict
)
348 err
= qemu_gettimeofday(&tv
);
352 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
353 "'microseconds': %" PRId64
" }",
354 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
355 qdict_put_obj(qdict
, "timestamp", obj
);
359 * monitor_protocol_event(): Generate a Monitor event
361 * Event-specific data can be emitted through the (optional) 'data' parameter.
363 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
366 const char *event_name
;
369 assert(event
< QEVENT_MAX
);
373 event_name
= "DEBUG";
375 case QEVENT_SHUTDOWN
:
376 event_name
= "SHUTDOWN";
379 event_name
= "RESET";
381 case QEVENT_POWERDOWN
:
382 event_name
= "POWERDOWN";
387 case QEVENT_VNC_CONNECTED
:
388 event_name
= "VNC_CONNECTED";
390 case QEVENT_VNC_INITIALIZED
:
391 event_name
= "VNC_INITIALIZED";
393 case QEVENT_VNC_DISCONNECTED
:
394 event_name
= "VNC_DISCONNECTED";
396 case QEVENT_BLOCK_IO_ERROR
:
397 event_name
= "BLOCK_IO_ERROR";
406 qdict_put(qmp
, "event", qstring_from_str(event_name
));
408 qobject_incref(data
);
409 qdict_put_obj(qmp
, "data", data
);
412 QLIST_FOREACH(mon
, &mon_list
, entry
) {
413 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
414 monitor_json_emitter(mon
, QOBJECT(qmp
));
420 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
423 /* Will setup QMP capabilities in the future */
424 if (monitor_ctrl_mode(mon
)) {
425 mon
->mc
->command_mode
= 1;
431 static int compare_cmd(const char *name
, const char *list
)
433 const char *p
, *pstart
;
441 p
= pstart
+ strlen(pstart
);
442 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
451 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
452 const char *prefix
, const char *name
)
454 const mon_cmd_t
*cmd
;
456 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
457 if (!name
|| !strcmp(name
, cmd
->name
))
458 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
459 cmd
->params
, cmd
->help
);
463 static void help_cmd(Monitor
*mon
, const char *name
)
465 if (name
&& !strcmp(name
, "info")) {
466 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
468 help_cmd_dump(mon
, mon_cmds
, "", name
);
469 if (name
&& !strcmp(name
, "log")) {
470 const CPULogItem
*item
;
471 monitor_printf(mon
, "Log items (comma separated):\n");
472 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
473 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
474 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
480 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
482 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
485 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
489 const char *device
= qdict_get_str(qdict
, "device");
491 all_devices
= !strcmp(device
, "all");
492 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
494 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
496 bdrv_commit(dinfo
->bdrv
);
500 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
502 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
505 data
->user_print(data
->mon
, ret_data
);
507 monitor_resume(data
->mon
);
511 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
513 monitor_protocol_emitter(opaque
, ret_data
);
516 static void qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
519 cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
522 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
524 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
527 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
532 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
534 cb_data
->user_print
= cmd
->user_print
;
535 monitor_suspend(mon
);
536 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
537 user_monitor_complete
, cb_data
);
544 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
548 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
550 cb_data
->user_print
= cmd
->user_print
;
551 monitor_suspend(mon
);
552 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
559 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
561 const mon_cmd_t
*cmd
;
562 const char *item
= qdict_get_try_str(qdict
, "item");
565 assert(monitor_ctrl_mode(mon
) == 0);
569 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
570 if (compare_cmd(item
, cmd
->name
))
574 if (cmd
->name
== NULL
) {
575 if (monitor_ctrl_mode(mon
)) {
576 qemu_error_new(QERR_COMMAND_NOT_FOUND
, item
);
582 if (monitor_handler_is_async(cmd
)) {
583 if (monitor_ctrl_mode(mon
)) {
584 qmp_async_info_handler(mon
, cmd
);
586 user_async_info_handler(mon
, cmd
);
589 * Indicate that this command is asynchronous and will not return any
590 * data (not even empty). Instead, the data will be returned via a
591 * completion callback.
593 *ret_data
= qobject_from_jsonf("{ '__mon_async': 'return' }");
594 } else if (monitor_handler_ported(cmd
)) {
595 cmd
->mhandler
.info_new(mon
, ret_data
);
597 if (!monitor_ctrl_mode(mon
)) {
599 * User Protocol function is called here, Monitor Protocol is
600 * handled by monitor_call_handler()
603 cmd
->user_print(mon
, *ret_data
);
606 if (monitor_ctrl_mode(mon
)) {
607 /* handler not converted yet */
608 qemu_error_new(QERR_COMMAND_NOT_FOUND
, item
);
610 cmd
->mhandler
.info(mon
);
617 help_cmd(mon
, "info");
620 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
624 qdict
= qobject_to_qdict(data
);
626 monitor_printf(mon
, "%s%s\n", qdict_get_str(qdict
, "qemu"),
627 qdict_get_str(qdict
, "package"));
631 * do_info_version(): Show QEMU version
633 * Return a QDict with the following information:
635 * - "qemu": QEMU's version
636 * - "package": package's version
640 * { "qemu": "0.11.50", "package": "" }
642 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
644 *ret_data
= qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
645 QEMU_VERSION
, QEMU_PKGVERSION
);
648 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
652 qdict
= qobject_to_qdict(data
);
653 if (qdict_size(qdict
) == 0) {
657 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
661 * do_info_name(): Show VM name
663 * Return a QDict with the following information:
665 * - "name": VM's name (optional)
669 * { "name": "qemu-name" }
671 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
673 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
674 qobject_from_jsonf("{}");
677 static QObject
*get_cmd_dict(const char *name
)
681 /* Remove '|' from some commands */
682 p
= strchr(name
, '|');
689 return qobject_from_jsonf("{ 'name': %s }", p
);
693 * do_info_commands(): List QMP available commands
695 * Each command is represented by a QDict, the returned QObject is a QList
698 * The QDict contains:
700 * - "name": command's name
704 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
706 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
709 const mon_cmd_t
*cmd
;
711 cmd_list
= qlist_new();
713 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
714 if (monitor_handler_ported(cmd
) && !compare_cmd(cmd
->name
, "info")) {
715 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
719 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
720 if (monitor_handler_ported(cmd
)) {
722 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
723 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
727 *ret_data
= QOBJECT(cmd_list
);
730 #if defined(TARGET_I386)
731 static void do_info_hpet_print(Monitor
*mon
, const QObject
*data
)
733 monitor_printf(mon
, "HPET is %s by QEMU\n",
734 qdict_get_bool(qobject_to_qdict(data
), "enabled") ?
735 "enabled" : "disabled");
739 * do_info_hpet(): Show HPET state
741 * Return a QDict with the following information:
743 * - "enabled": true if hpet if enabled, false otherwise
747 * { "enabled": true }
749 static void do_info_hpet(Monitor
*mon
, QObject
**ret_data
)
751 *ret_data
= qobject_from_jsonf("{ 'enabled': %i }", !no_hpet
);
755 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
757 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
761 * do_info_uuid(): Show VM UUID
763 * Return a QDict with the following information:
765 * - "UUID": Universally Unique Identifier
769 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
771 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
775 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
776 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
777 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
778 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
779 qemu_uuid
[14], qemu_uuid
[15]);
780 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
783 /* get the current CPU defined by the user */
784 static int mon_set_cpu(int cpu_index
)
788 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
789 if (env
->cpu_index
== cpu_index
) {
790 cur_mon
->mon_cpu
= env
;
797 static CPUState
*mon_get_cpu(void)
799 if (!cur_mon
->mon_cpu
) {
802 cpu_synchronize_state(cur_mon
->mon_cpu
);
803 return cur_mon
->mon_cpu
;
806 static void do_info_registers(Monitor
*mon
)
811 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
814 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
819 static void print_cpu_iter(QObject
*obj
, void *opaque
)
823 Monitor
*mon
= opaque
;
825 assert(qobject_type(obj
) == QTYPE_QDICT
);
826 cpu
= qobject_to_qdict(obj
);
828 if (qdict_get_bool(cpu
, "current")) {
832 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
834 #if defined(TARGET_I386)
835 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
836 (target_ulong
) qdict_get_int(cpu
, "pc"));
837 #elif defined(TARGET_PPC)
838 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
839 (target_long
) qdict_get_int(cpu
, "nip"));
840 #elif defined(TARGET_SPARC)
841 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
842 (target_long
) qdict_get_int(cpu
, "pc"));
843 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
844 (target_long
) qdict_get_int(cpu
, "npc"));
845 #elif defined(TARGET_MIPS)
846 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
847 (target_long
) qdict_get_int(cpu
, "PC"));
850 if (qdict_get_bool(cpu
, "halted")) {
851 monitor_printf(mon
, " (halted)");
854 monitor_printf(mon
, "\n");
857 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
861 assert(qobject_type(data
) == QTYPE_QLIST
);
862 cpu_list
= qobject_to_qlist(data
);
863 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
867 * do_info_cpus(): Show CPU information
869 * Return a QList. Each CPU is represented by a QDict, which contains:
872 * - "current": true if this is the current CPU, false otherwise
873 * - "halted": true if the cpu is halted, false otherwise
874 * - Current program counter. The key's name depends on the architecture:
877 * "pc" and "npc": sparc
882 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
883 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
885 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
890 cpu_list
= qlist_new();
892 /* just to set the default cpu if not already done */
895 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
899 cpu_synchronize_state(env
);
901 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
902 env
->cpu_index
, env
== mon
->mon_cpu
,
905 cpu
= qobject_to_qdict(obj
);
907 #if defined(TARGET_I386)
908 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
909 #elif defined(TARGET_PPC)
910 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
911 #elif defined(TARGET_SPARC)
912 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
913 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
914 #elif defined(TARGET_MIPS)
915 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
918 qlist_append(cpu_list
, cpu
);
921 *ret_data
= QOBJECT(cpu_list
);
924 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
926 int index
= qdict_get_int(qdict
, "index");
927 if (mon_set_cpu(index
) < 0) {
928 qemu_error_new(QERR_INVALID_PARAMETER
, "index");
934 static void do_info_jit(Monitor
*mon
)
936 dump_exec_info((FILE *)mon
, monitor_fprintf
);
939 static void do_info_history(Monitor
*mon
)
948 str
= readline_get_history(mon
->rs
, i
);
951 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
956 #if defined(TARGET_PPC)
957 /* XXX: not implemented in other targets */
958 static void do_info_cpu_stats(Monitor
*mon
)
963 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
968 * do_quit(): Quit QEMU execution
970 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
976 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
978 if (bdrv_is_inserted(bs
)) {
980 if (!bdrv_is_removable(bs
)) {
981 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE
,
982 bdrv_get_device_name(bs
));
985 if (bdrv_is_locked(bs
)) {
986 qemu_error_new(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
995 static int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
997 BlockDriverState
*bs
;
998 int force
= qdict_get_int(qdict
, "force");
999 const char *filename
= qdict_get_str(qdict
, "device");
1001 bs
= bdrv_find(filename
);
1003 qemu_error_new(QERR_DEVICE_NOT_FOUND
, filename
);
1006 return eject_device(mon
, bs
, force
);
1009 static void do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
1012 BlockDriverState
*bs
;
1014 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
1016 qemu_error_new(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
1020 if (bdrv_set_key(bs
, qdict_get_str(qdict
, "password")) < 0) {
1021 qemu_error_new(QERR_INVALID_PASSWORD
);
1025 static void do_change_block(Monitor
*mon
, const char *device
,
1026 const char *filename
, const char *fmt
)
1028 BlockDriverState
*bs
;
1029 BlockDriver
*drv
= NULL
;
1031 bs
= bdrv_find(device
);
1033 qemu_error_new(QERR_DEVICE_NOT_FOUND
, device
);
1037 drv
= bdrv_find_whitelisted_format(fmt
);
1039 qemu_error_new(QERR_INVALID_BLOCK_FORMAT
, fmt
);
1043 if (eject_device(mon
, bs
, 0) < 0)
1045 bdrv_open2(bs
, filename
, BDRV_O_RDWR
, drv
);
1046 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
1049 static void change_vnc_password(const char *password
)
1051 if (vnc_display_password(NULL
, password
) < 0)
1052 qemu_error_new(QERR_SET_PASSWD_FAILED
);
1056 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
1059 change_vnc_password(password
);
1060 monitor_read_command(mon
, 1);
1063 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1065 if (strcmp(target
, "passwd") == 0 ||
1066 strcmp(target
, "password") == 0) {
1069 strncpy(password
, arg
, sizeof(password
));
1070 password
[sizeof(password
) - 1] = '\0';
1071 change_vnc_password(password
);
1073 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1076 if (vnc_display_open(NULL
, target
) < 0)
1077 qemu_error_new(QERR_VNC_SERVER_FAILED
, target
);
1082 * do_change(): Change a removable medium, or VNC configuration
1084 static void do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1086 const char *device
= qdict_get_str(qdict
, "device");
1087 const char *target
= qdict_get_str(qdict
, "target");
1088 const char *arg
= qdict_get_try_str(qdict
, "arg");
1089 if (strcmp(device
, "vnc") == 0) {
1090 do_change_vnc(mon
, target
, arg
);
1092 do_change_block(mon
, device
, target
, arg
);
1096 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
1098 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1101 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1103 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1106 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1109 const char *items
= qdict_get_str(qdict
, "items");
1111 if (!strcmp(items
, "none")) {
1114 mask
= cpu_str_to_log_mask(items
);
1116 help_cmd(mon
, "log");
1123 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1125 const char *option
= qdict_get_try_str(qdict
, "option");
1126 if (!option
|| !strcmp(option
, "on")) {
1128 } else if (!strcmp(option
, "off")) {
1131 monitor_printf(mon
, "unexpected option %s\n", option
);
1136 * do_stop(): Stop VM execution
1138 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1140 vm_stop(EXCP_INTERRUPT
);
1144 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1146 struct bdrv_iterate_context
{
1152 * do_cont(): Resume emulation.
1154 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1156 struct bdrv_iterate_context context
= { mon
, 0 };
1158 bdrv_iterate(encrypted_bdrv_it
, &context
);
1159 /* only resume the vm if all keys are set and valid */
1168 static void bdrv_key_cb(void *opaque
, int err
)
1170 Monitor
*mon
= opaque
;
1172 /* another key was set successfully, retry to continue */
1174 do_cont(mon
, NULL
, NULL
);
1177 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1179 struct bdrv_iterate_context
*context
= opaque
;
1181 if (!context
->err
&& bdrv_key_required(bs
)) {
1182 context
->err
= -EBUSY
;
1183 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1188 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1190 const char *device
= qdict_get_try_str(qdict
, "device");
1192 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1193 if (gdbserver_start(device
) < 0) {
1194 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1196 } else if (strcmp(device
, "none") == 0) {
1197 monitor_printf(mon
, "Disabled gdbserver\n");
1199 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1204 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1206 const char *action
= qdict_get_str(qdict
, "action");
1207 if (select_watchdog_action(action
) == -1) {
1208 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1212 static void monitor_printc(Monitor
*mon
, int c
)
1214 monitor_printf(mon
, "'");
1217 monitor_printf(mon
, "\\'");
1220 monitor_printf(mon
, "\\\\");
1223 monitor_printf(mon
, "\\n");
1226 monitor_printf(mon
, "\\r");
1229 if (c
>= 32 && c
<= 126) {
1230 monitor_printf(mon
, "%c", c
);
1232 monitor_printf(mon
, "\\x%02x", c
);
1236 monitor_printf(mon
, "'");
1239 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1240 target_phys_addr_t addr
, int is_physical
)
1243 int l
, line_size
, i
, max_digits
, len
;
1247 if (format
== 'i') {
1250 env
= mon_get_cpu();
1256 } else if (wsize
== 4) {
1259 /* as default we use the current CS size */
1262 #ifdef TARGET_X86_64
1263 if ((env
->efer
& MSR_EFER_LMA
) &&
1264 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1268 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1273 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1277 len
= wsize
* count
;
1286 max_digits
= (wsize
* 8 + 2) / 3;
1290 max_digits
= (wsize
* 8) / 4;
1294 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1303 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1305 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1310 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1312 env
= mon_get_cpu();
1313 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1314 monitor_printf(mon
, " Cannot access memory\n");
1323 v
= ldub_raw(buf
+ i
);
1326 v
= lduw_raw(buf
+ i
);
1329 v
= (uint32_t)ldl_raw(buf
+ i
);
1332 v
= ldq_raw(buf
+ i
);
1335 monitor_printf(mon
, " ");
1338 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1341 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1344 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1347 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1350 monitor_printc(mon
, v
);
1355 monitor_printf(mon
, "\n");
1361 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1363 int count
= qdict_get_int(qdict
, "count");
1364 int format
= qdict_get_int(qdict
, "format");
1365 int size
= qdict_get_int(qdict
, "size");
1366 target_long addr
= qdict_get_int(qdict
, "addr");
1368 memory_dump(mon
, count
, format
, size
, addr
, 0);
1371 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1373 int count
= qdict_get_int(qdict
, "count");
1374 int format
= qdict_get_int(qdict
, "format");
1375 int size
= qdict_get_int(qdict
, "size");
1376 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1378 memory_dump(mon
, count
, format
, size
, addr
, 1);
1381 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1383 int format
= qdict_get_int(qdict
, "format");
1384 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1386 #if TARGET_PHYS_ADDR_BITS == 32
1389 monitor_printf(mon
, "%#o", val
);
1392 monitor_printf(mon
, "%#x", val
);
1395 monitor_printf(mon
, "%u", val
);
1399 monitor_printf(mon
, "%d", val
);
1402 monitor_printc(mon
, val
);
1408 monitor_printf(mon
, "%#" PRIo64
, val
);
1411 monitor_printf(mon
, "%#" PRIx64
, val
);
1414 monitor_printf(mon
, "%" PRIu64
, val
);
1418 monitor_printf(mon
, "%" PRId64
, val
);
1421 monitor_printc(mon
, val
);
1425 monitor_printf(mon
, "\n");
1428 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1431 uint32_t size
= qdict_get_int(qdict
, "size");
1432 const char *filename
= qdict_get_str(qdict
, "filename");
1433 target_long addr
= qdict_get_int(qdict
, "val");
1438 env
= mon_get_cpu();
1440 f
= fopen(filename
, "wb");
1442 qemu_error_new(QERR_OPEN_FILE_FAILED
, filename
);
1449 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1450 if (fwrite(buf
, 1, l
, f
) != l
) {
1451 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1461 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1467 uint32_t size
= qdict_get_int(qdict
, "size");
1468 const char *filename
= qdict_get_str(qdict
, "filename");
1469 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1471 f
= fopen(filename
, "wb");
1473 qemu_error_new(QERR_OPEN_FILE_FAILED
, filename
);
1480 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1481 if (fwrite(buf
, 1, l
, f
) != l
) {
1482 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1493 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1498 uint32_t start
= qdict_get_int(qdict
, "start");
1499 uint32_t size
= qdict_get_int(qdict
, "size");
1502 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1503 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1504 /* BSD sum algorithm ('sum' Unix command) */
1505 sum
= (sum
>> 1) | (sum
<< 15);
1508 monitor_printf(mon
, "%05d\n", sum
);
1516 static const KeyDef key_defs
[] = {
1518 { 0x36, "shift_r" },
1523 { 0xe4, "altgr_r" },
1543 { 0x0e, "backspace" },
1580 { 0x37, "asterisk" },
1583 { 0x3a, "caps_lock" },
1594 { 0x45, "num_lock" },
1595 { 0x46, "scroll_lock" },
1597 { 0xb5, "kp_divide" },
1598 { 0x37, "kp_multiply" },
1599 { 0x4a, "kp_subtract" },
1601 { 0x9c, "kp_enter" },
1602 { 0x53, "kp_decimal" },
1635 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1650 { 0xfe, "compose" },
1655 static int get_keycode(const char *key
)
1661 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1662 if (!strcmp(key
, p
->name
))
1665 if (strstart(key
, "0x", NULL
)) {
1666 ret
= strtoul(key
, &endp
, 0);
1667 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1673 #define MAX_KEYCODES 16
1674 static uint8_t keycodes
[MAX_KEYCODES
];
1675 static int nb_pending_keycodes
;
1676 static QEMUTimer
*key_timer
;
1678 static void release_keys(void *opaque
)
1682 while (nb_pending_keycodes
> 0) {
1683 nb_pending_keycodes
--;
1684 keycode
= keycodes
[nb_pending_keycodes
];
1686 kbd_put_keycode(0xe0);
1687 kbd_put_keycode(keycode
| 0x80);
1691 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1693 char keyname_buf
[16];
1695 int keyname_len
, keycode
, i
;
1696 const char *string
= qdict_get_str(qdict
, "string");
1697 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1698 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1700 if (nb_pending_keycodes
> 0) {
1701 qemu_del_timer(key_timer
);
1708 separator
= strchr(string
, '-');
1709 keyname_len
= separator
? separator
- string
: strlen(string
);
1710 if (keyname_len
> 0) {
1711 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1712 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1713 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1716 if (i
== MAX_KEYCODES
) {
1717 monitor_printf(mon
, "too many keys\n");
1720 keyname_buf
[keyname_len
] = 0;
1721 keycode
= get_keycode(keyname_buf
);
1723 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1726 keycodes
[i
++] = keycode
;
1730 string
= separator
+ 1;
1732 nb_pending_keycodes
= i
;
1733 /* key down events */
1734 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1735 keycode
= keycodes
[i
];
1737 kbd_put_keycode(0xe0);
1738 kbd_put_keycode(keycode
& 0x7f);
1740 /* delayed key up events */
1741 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1742 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1745 static int mouse_button_state
;
1747 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1750 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1751 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1752 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1753 dx
= strtol(dx_str
, NULL
, 0);
1754 dy
= strtol(dy_str
, NULL
, 0);
1757 dz
= strtol(dz_str
, NULL
, 0);
1758 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1761 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1763 int button_state
= qdict_get_int(qdict
, "button_state");
1764 mouse_button_state
= button_state
;
1765 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1768 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1770 int size
= qdict_get_int(qdict
, "size");
1771 int addr
= qdict_get_int(qdict
, "addr");
1772 int has_index
= qdict_haskey(qdict
, "index");
1777 int index
= qdict_get_int(qdict
, "index");
1778 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1786 val
= cpu_inb(addr
);
1790 val
= cpu_inw(addr
);
1794 val
= cpu_inl(addr
);
1798 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1799 suffix
, addr
, size
* 2, val
);
1802 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1804 int size
= qdict_get_int(qdict
, "size");
1805 int addr
= qdict_get_int(qdict
, "addr");
1806 int val
= qdict_get_int(qdict
, "val");
1808 addr
&= IOPORTS_MASK
;
1813 cpu_outb(addr
, val
);
1816 cpu_outw(addr
, val
);
1819 cpu_outl(addr
, val
);
1824 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1827 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1829 res
= qemu_boot_set(bootdevice
);
1831 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1832 } else if (res
> 0) {
1833 monitor_printf(mon
, "setting boot device list failed\n");
1835 monitor_printf(mon
, "no function defined to set boot device list for "
1836 "this architecture\n");
1841 * do_system_reset(): Issue a machine reset
1843 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1846 qemu_system_reset_request();
1851 * do_system_powerdown(): Issue a machine powerdown
1853 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1856 qemu_system_powerdown_request();
1860 #if defined(TARGET_I386)
1861 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1863 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1866 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1867 pte
& PG_PSE_MASK
? 'P' : '-',
1868 pte
& PG_DIRTY_MASK
? 'D' : '-',
1869 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1870 pte
& PG_PCD_MASK
? 'C' : '-',
1871 pte
& PG_PWT_MASK
? 'T' : '-',
1872 pte
& PG_USER_MASK
? 'U' : '-',
1873 pte
& PG_RW_MASK
? 'W' : '-');
1876 static void tlb_info(Monitor
*mon
)
1880 uint32_t pgd
, pde
, pte
;
1882 env
= mon_get_cpu();
1884 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1885 monitor_printf(mon
, "PG disabled\n");
1888 pgd
= env
->cr
[3] & ~0xfff;
1889 for(l1
= 0; l1
< 1024; l1
++) {
1890 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1891 pde
= le32_to_cpu(pde
);
1892 if (pde
& PG_PRESENT_MASK
) {
1893 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1894 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1896 for(l2
= 0; l2
< 1024; l2
++) {
1897 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1898 (uint8_t *)&pte
, 4);
1899 pte
= le32_to_cpu(pte
);
1900 if (pte
& PG_PRESENT_MASK
) {
1901 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1911 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1912 uint32_t end
, int prot
)
1915 prot1
= *plast_prot
;
1916 if (prot
!= prot1
) {
1917 if (*pstart
!= -1) {
1918 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1919 *pstart
, end
, end
- *pstart
,
1920 prot1
& PG_USER_MASK
? 'u' : '-',
1922 prot1
& PG_RW_MASK
? 'w' : '-');
1932 static void mem_info(Monitor
*mon
)
1935 int l1
, l2
, prot
, last_prot
;
1936 uint32_t pgd
, pde
, pte
, start
, end
;
1938 env
= mon_get_cpu();
1940 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1941 monitor_printf(mon
, "PG disabled\n");
1944 pgd
= env
->cr
[3] & ~0xfff;
1947 for(l1
= 0; l1
< 1024; l1
++) {
1948 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1949 pde
= le32_to_cpu(pde
);
1951 if (pde
& PG_PRESENT_MASK
) {
1952 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1953 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1954 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1956 for(l2
= 0; l2
< 1024; l2
++) {
1957 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1958 (uint8_t *)&pte
, 4);
1959 pte
= le32_to_cpu(pte
);
1960 end
= (l1
<< 22) + (l2
<< 12);
1961 if (pte
& PG_PRESENT_MASK
) {
1962 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1966 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1971 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1977 #if defined(TARGET_SH4)
1979 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1981 monitor_printf(mon
, " tlb%i:\t"
1982 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1983 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1984 "dirty=%hhu writethrough=%hhu\n",
1986 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1987 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1991 static void tlb_info(Monitor
*mon
)
1993 CPUState
*env
= mon_get_cpu();
1996 monitor_printf (mon
, "ITLB:\n");
1997 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1998 print_tlb (mon
, i
, &env
->itlb
[i
]);
1999 monitor_printf (mon
, "UTLB:\n");
2000 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2001 print_tlb (mon
, i
, &env
->utlb
[i
]);
2006 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
2010 qdict
= qobject_to_qdict(data
);
2012 monitor_printf(mon
, "kvm support: ");
2013 if (qdict_get_bool(qdict
, "present")) {
2014 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
2015 "enabled" : "disabled");
2017 monitor_printf(mon
, "not compiled\n");
2022 * do_info_kvm(): Show KVM information
2024 * Return a QDict with the following information:
2026 * - "enabled": true if KVM support is enabled, false otherwise
2027 * - "present": true if QEMU has KVM support, false otherwise
2031 * { "enabled": true, "present": true }
2033 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
2036 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2039 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2043 static void do_info_numa(Monitor
*mon
)
2048 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2049 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2050 monitor_printf(mon
, "node %d cpus:", i
);
2051 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2052 if (env
->numa_node
== i
) {
2053 monitor_printf(mon
, " %d", env
->cpu_index
);
2056 monitor_printf(mon
, "\n");
2057 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2062 #ifdef CONFIG_PROFILER
2067 static void do_info_profile(Monitor
*mon
)
2073 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2074 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2075 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2076 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2081 static void do_info_profile(Monitor
*mon
)
2083 monitor_printf(mon
, "Internal profiler not compiled\n");
2087 /* Capture support */
2088 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2090 static void do_info_capture(Monitor
*mon
)
2095 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2096 monitor_printf(mon
, "[%d]: ", i
);
2097 s
->ops
.info (s
->opaque
);
2102 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2105 int n
= qdict_get_int(qdict
, "n");
2108 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2110 s
->ops
.destroy (s
->opaque
);
2111 QLIST_REMOVE (s
, entries
);
2118 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2120 const char *path
= qdict_get_str(qdict
, "path");
2121 int has_freq
= qdict_haskey(qdict
, "freq");
2122 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2123 int has_bits
= qdict_haskey(qdict
, "bits");
2124 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2125 int has_channels
= qdict_haskey(qdict
, "nchannels");
2126 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2129 s
= qemu_mallocz (sizeof (*s
));
2131 freq
= has_freq
? freq
: 44100;
2132 bits
= has_bits
? bits
: 16;
2133 nchannels
= has_channels
? nchannels
: 2;
2135 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2136 monitor_printf(mon
, "Faied to add wave capture\n");
2139 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2143 #if defined(TARGET_I386)
2144 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
2147 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2149 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2150 if (env
->cpu_index
== cpu_index
) {
2151 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2157 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2161 qdict
= qobject_to_qdict(data
);
2163 monitor_printf(mon
, "VM status: ");
2164 if (qdict_get_bool(qdict
, "running")) {
2165 monitor_printf(mon
, "running");
2166 if (qdict_get_bool(qdict
, "singlestep")) {
2167 monitor_printf(mon
, " (single step mode)");
2170 monitor_printf(mon
, "paused");
2173 monitor_printf(mon
, "\n");
2177 * do_info_status(): VM status
2179 * Return a QDict with the following information:
2181 * - "running": true if the VM is running, or false if it is paused
2182 * - "singlestep": true if the VM is in single step mode, false otherwise
2186 * { "running": true, "singlestep": false }
2188 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2190 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2191 vm_running
, singlestep
);
2194 static void print_balloon_stat(const char *key
, QObject
*obj
, void *opaque
)
2196 Monitor
*mon
= opaque
;
2198 if (strcmp(key
, "actual"))
2199 monitor_printf(mon
, ",%s=%" PRId64
, key
,
2200 qint_get_int(qobject_to_qint(obj
)));
2203 static void monitor_print_balloon(Monitor
*mon
, const QObject
*data
)
2207 qdict
= qobject_to_qdict(data
);
2208 if (!qdict_haskey(qdict
, "actual"))
2211 monitor_printf(mon
, "balloon: actual=%" PRId64
,
2212 qdict_get_int(qdict
, "actual") >> 20);
2213 qdict_iter(qdict
, print_balloon_stat
, mon
);
2214 monitor_printf(mon
, "\n");
2218 * do_info_balloon(): Balloon information
2220 * Make an asynchronous request for balloon info. When the request completes
2221 * a QDict will be returned according to the following specification:
2223 * - "actual": current balloon value in bytes
2224 * The following fields may or may not be present:
2225 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2226 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2227 * - "major_page_faults": Number of major faults
2228 * - "minor_page_faults": Number of minor faults
2229 * - "free_mem": Total amount of free and unused memory (bytes)
2230 * - "total_mem": Total amount of available memory (bytes)
2234 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2235 * "major_page_faults": 142, "minor_page_faults": 239245,
2236 * "free_mem": 1014185984, "total_mem": 1044668416 }
2238 static int do_info_balloon(Monitor
*mon
, MonitorCompletion cb
, void *opaque
)
2242 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2243 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
2247 ret
= qemu_balloon_status(cb
, opaque
);
2249 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
2257 * do_balloon(): Request VM to change its memory allocation
2259 static int do_balloon(Monitor
*mon
, const QDict
*params
,
2260 MonitorCompletion cb
, void *opaque
)
2264 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2265 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
2269 ret
= qemu_balloon(qdict_get_int(params
, "value"), cb
, opaque
);
2271 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
2278 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2280 qemu_acl
*acl
= qemu_acl_find(name
);
2283 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2288 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2290 const char *aclname
= qdict_get_str(qdict
, "aclname");
2291 qemu_acl
*acl
= find_acl(mon
, aclname
);
2292 qemu_acl_entry
*entry
;
2296 monitor_printf(mon
, "policy: %s\n",
2297 acl
->defaultDeny
? "deny" : "allow");
2298 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2300 monitor_printf(mon
, "%d: %s %s\n", i
,
2301 entry
->deny
? "deny" : "allow", entry
->match
);
2306 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2308 const char *aclname
= qdict_get_str(qdict
, "aclname");
2309 qemu_acl
*acl
= find_acl(mon
, aclname
);
2312 qemu_acl_reset(acl
);
2313 monitor_printf(mon
, "acl: removed all rules\n");
2317 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2319 const char *aclname
= qdict_get_str(qdict
, "aclname");
2320 const char *policy
= qdict_get_str(qdict
, "policy");
2321 qemu_acl
*acl
= find_acl(mon
, aclname
);
2324 if (strcmp(policy
, "allow") == 0) {
2325 acl
->defaultDeny
= 0;
2326 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2327 } else if (strcmp(policy
, "deny") == 0) {
2328 acl
->defaultDeny
= 1;
2329 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2331 monitor_printf(mon
, "acl: unknown policy '%s', "
2332 "expected 'deny' or 'allow'\n", policy
);
2337 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2339 const char *aclname
= qdict_get_str(qdict
, "aclname");
2340 const char *match
= qdict_get_str(qdict
, "match");
2341 const char *policy
= qdict_get_str(qdict
, "policy");
2342 int has_index
= qdict_haskey(qdict
, "index");
2343 int index
= qdict_get_try_int(qdict
, "index", -1);
2344 qemu_acl
*acl
= find_acl(mon
, aclname
);
2348 if (strcmp(policy
, "allow") == 0) {
2350 } else if (strcmp(policy
, "deny") == 0) {
2353 monitor_printf(mon
, "acl: unknown policy '%s', "
2354 "expected 'deny' or 'allow'\n", policy
);
2358 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2360 ret
= qemu_acl_append(acl
, deny
, match
);
2362 monitor_printf(mon
, "acl: unable to add acl entry\n");
2364 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2368 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2370 const char *aclname
= qdict_get_str(qdict
, "aclname");
2371 const char *match
= qdict_get_str(qdict
, "match");
2372 qemu_acl
*acl
= find_acl(mon
, aclname
);
2376 ret
= qemu_acl_remove(acl
, match
);
2378 monitor_printf(mon
, "acl: no matching acl entry\n");
2380 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2384 #if defined(TARGET_I386)
2385 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2388 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2389 int bank
= qdict_get_int(qdict
, "bank");
2390 uint64_t status
= qdict_get_int(qdict
, "status");
2391 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2392 uint64_t addr
= qdict_get_int(qdict
, "addr");
2393 uint64_t misc
= qdict_get_int(qdict
, "misc");
2395 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
2396 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
2397 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
2403 static void do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2405 const char *fdname
= qdict_get_str(qdict
, "fdname");
2409 fd
= qemu_chr_get_msgfd(mon
->chr
);
2411 qemu_error_new(QERR_FD_NOT_SUPPLIED
);
2415 if (qemu_isdigit(fdname
[0])) {
2416 qemu_error_new(QERR_INVALID_PARAMETER
, "fdname");
2422 if (errno
== EMFILE
)
2423 qemu_error_new(QERR_TOO_MANY_FILES
);
2425 qemu_error_new(QERR_UNDEFINED_ERROR
);
2429 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2430 if (strcmp(monfd
->name
, fdname
) != 0) {
2439 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
2440 monfd
->name
= qemu_strdup(fdname
);
2443 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2446 static void do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2448 const char *fdname
= qdict_get_str(qdict
, "fdname");
2451 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2452 if (strcmp(monfd
->name
, fdname
) != 0) {
2456 QLIST_REMOVE(monfd
, next
);
2458 qemu_free(monfd
->name
);
2463 qemu_error_new(QERR_FD_NOT_FOUND
, fdname
);
2466 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2468 int saved_vm_running
= vm_running
;
2469 const char *name
= qdict_get_str(qdict
, "name");
2473 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
2477 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2481 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2484 if (strcmp(monfd
->name
, fdname
) != 0) {
2490 /* caller takes ownership of fd */
2491 QLIST_REMOVE(monfd
, next
);
2492 qemu_free(monfd
->name
);
2501 static const mon_cmd_t mon_cmds
[] = {
2502 #include "qemu-monitor.h"
2506 /* Please update qemu-monitor.hx when adding or changing commands */
2507 static const mon_cmd_t info_cmds
[] = {
2512 .help
= "show the version of QEMU",
2513 .user_print
= do_info_version_print
,
2514 .mhandler
.info_new
= do_info_version
,
2520 .help
= "list QMP available commands",
2521 .user_print
= monitor_user_noop
,
2522 .mhandler
.info_new
= do_info_commands
,
2528 .help
= "show the network state",
2529 .mhandler
.info
= do_info_network
,
2535 .help
= "show the character devices",
2536 .user_print
= qemu_chr_info_print
,
2537 .mhandler
.info_new
= qemu_chr_info
,
2543 .help
= "show the block devices",
2544 .user_print
= bdrv_info_print
,
2545 .mhandler
.info_new
= bdrv_info
,
2548 .name
= "blockstats",
2551 .help
= "show block device statistics",
2552 .user_print
= bdrv_stats_print
,
2553 .mhandler
.info_new
= bdrv_info_stats
,
2556 .name
= "registers",
2559 .help
= "show the cpu registers",
2560 .mhandler
.info
= do_info_registers
,
2566 .help
= "show infos for each CPU",
2567 .user_print
= monitor_print_cpus
,
2568 .mhandler
.info_new
= do_info_cpus
,
2574 .help
= "show the command line history",
2575 .mhandler
.info
= do_info_history
,
2581 .help
= "show the interrupts statistics (if available)",
2582 .mhandler
.info
= irq_info
,
2588 .help
= "show i8259 (PIC) state",
2589 .mhandler
.info
= pic_info
,
2595 .help
= "show PCI info",
2596 .user_print
= do_pci_info_print
,
2597 .mhandler
.info_new
= do_pci_info
,
2599 #if defined(TARGET_I386) || defined(TARGET_SH4)
2604 .help
= "show virtual to physical memory mappings",
2605 .mhandler
.info
= tlb_info
,
2608 #if defined(TARGET_I386)
2613 .help
= "show the active virtual memory mappings",
2614 .mhandler
.info
= mem_info
,
2620 .help
= "show state of HPET",
2621 .user_print
= do_info_hpet_print
,
2622 .mhandler
.info_new
= do_info_hpet
,
2629 .help
= "show dynamic compiler info",
2630 .mhandler
.info
= do_info_jit
,
2636 .help
= "show KVM information",
2637 .user_print
= do_info_kvm_print
,
2638 .mhandler
.info_new
= do_info_kvm
,
2644 .help
= "show NUMA information",
2645 .mhandler
.info
= do_info_numa
,
2651 .help
= "show guest USB devices",
2652 .mhandler
.info
= usb_info
,
2658 .help
= "show host USB devices",
2659 .mhandler
.info
= usb_host_info
,
2665 .help
= "show profiling information",
2666 .mhandler
.info
= do_info_profile
,
2672 .help
= "show capture information",
2673 .mhandler
.info
= do_info_capture
,
2676 .name
= "snapshots",
2679 .help
= "show the currently saved VM snapshots",
2680 .mhandler
.info
= do_info_snapshots
,
2686 .help
= "show the current VM status (running|paused)",
2687 .user_print
= do_info_status_print
,
2688 .mhandler
.info_new
= do_info_status
,
2694 .help
= "show guest PCMCIA status",
2695 .mhandler
.info
= pcmcia_info
,
2701 .help
= "show which guest mouse is receiving events",
2702 .user_print
= do_info_mice_print
,
2703 .mhandler
.info_new
= do_info_mice
,
2709 .help
= "show the vnc server status",
2710 .user_print
= do_info_vnc_print
,
2711 .mhandler
.info_new
= do_info_vnc
,
2717 .help
= "show the current VM name",
2718 .user_print
= do_info_name_print
,
2719 .mhandler
.info_new
= do_info_name
,
2725 .help
= "show the current VM UUID",
2726 .user_print
= do_info_uuid_print
,
2727 .mhandler
.info_new
= do_info_uuid
,
2729 #if defined(TARGET_PPC)
2734 .help
= "show CPU statistics",
2735 .mhandler
.info
= do_info_cpu_stats
,
2738 #if defined(CONFIG_SLIRP)
2743 .help
= "show user network stack connection states",
2744 .mhandler
.info
= do_info_usernet
,
2751 .help
= "show migration status",
2752 .user_print
= do_info_migrate_print
,
2753 .mhandler
.info_new
= do_info_migrate
,
2759 .help
= "show balloon information",
2760 .user_print
= monitor_print_balloon
,
2761 .mhandler
.info_async
= do_info_balloon
,
2768 .help
= "show device tree",
2769 .mhandler
.info
= do_info_qtree
,
2775 .help
= "show qdev device model list",
2776 .mhandler
.info
= do_info_qdm
,
2782 .help
= "show roms",
2783 .mhandler
.info
= do_info_roms
,
2790 /*******************************************************************/
2792 static const char *pch
;
2793 static jmp_buf expr_env
;
2798 typedef struct MonitorDef
{
2801 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2805 #if defined(TARGET_I386)
2806 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2808 CPUState
*env
= mon_get_cpu();
2809 return env
->eip
+ env
->segs
[R_CS
].base
;
2813 #if defined(TARGET_PPC)
2814 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2816 CPUState
*env
= mon_get_cpu();
2821 for (i
= 0; i
< 8; i
++)
2822 u
|= env
->crf
[i
] << (32 - (4 * i
));
2827 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2829 CPUState
*env
= mon_get_cpu();
2833 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2835 CPUState
*env
= mon_get_cpu();
2839 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2841 CPUState
*env
= mon_get_cpu();
2842 return cpu_ppc_load_decr(env
);
2845 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2847 CPUState
*env
= mon_get_cpu();
2848 return cpu_ppc_load_tbu(env
);
2851 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2853 CPUState
*env
= mon_get_cpu();
2854 return cpu_ppc_load_tbl(env
);
2858 #if defined(TARGET_SPARC)
2859 #ifndef TARGET_SPARC64
2860 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2862 CPUState
*env
= mon_get_cpu();
2863 return GET_PSR(env
);
2867 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2869 CPUState
*env
= mon_get_cpu();
2870 return env
->regwptr
[val
];
2874 static const MonitorDef monitor_defs
[] = {
2877 #define SEG(name, seg) \
2878 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2879 { name ".base", offsetof(CPUState, segs[seg].base) },\
2880 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2882 { "eax", offsetof(CPUState
, regs
[0]) },
2883 { "ecx", offsetof(CPUState
, regs
[1]) },
2884 { "edx", offsetof(CPUState
, regs
[2]) },
2885 { "ebx", offsetof(CPUState
, regs
[3]) },
2886 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2887 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2888 { "esi", offsetof(CPUState
, regs
[6]) },
2889 { "edi", offsetof(CPUState
, regs
[7]) },
2890 #ifdef TARGET_X86_64
2891 { "r8", offsetof(CPUState
, regs
[8]) },
2892 { "r9", offsetof(CPUState
, regs
[9]) },
2893 { "r10", offsetof(CPUState
, regs
[10]) },
2894 { "r11", offsetof(CPUState
, regs
[11]) },
2895 { "r12", offsetof(CPUState
, regs
[12]) },
2896 { "r13", offsetof(CPUState
, regs
[13]) },
2897 { "r14", offsetof(CPUState
, regs
[14]) },
2898 { "r15", offsetof(CPUState
, regs
[15]) },
2900 { "eflags", offsetof(CPUState
, eflags
) },
2901 { "eip", offsetof(CPUState
, eip
) },
2908 { "pc", 0, monitor_get_pc
, },
2909 #elif defined(TARGET_PPC)
2910 /* General purpose registers */
2911 { "r0", offsetof(CPUState
, gpr
[0]) },
2912 { "r1", offsetof(CPUState
, gpr
[1]) },
2913 { "r2", offsetof(CPUState
, gpr
[2]) },
2914 { "r3", offsetof(CPUState
, gpr
[3]) },
2915 { "r4", offsetof(CPUState
, gpr
[4]) },
2916 { "r5", offsetof(CPUState
, gpr
[5]) },
2917 { "r6", offsetof(CPUState
, gpr
[6]) },
2918 { "r7", offsetof(CPUState
, gpr
[7]) },
2919 { "r8", offsetof(CPUState
, gpr
[8]) },
2920 { "r9", offsetof(CPUState
, gpr
[9]) },
2921 { "r10", offsetof(CPUState
, gpr
[10]) },
2922 { "r11", offsetof(CPUState
, gpr
[11]) },
2923 { "r12", offsetof(CPUState
, gpr
[12]) },
2924 { "r13", offsetof(CPUState
, gpr
[13]) },
2925 { "r14", offsetof(CPUState
, gpr
[14]) },
2926 { "r15", offsetof(CPUState
, gpr
[15]) },
2927 { "r16", offsetof(CPUState
, gpr
[16]) },
2928 { "r17", offsetof(CPUState
, gpr
[17]) },
2929 { "r18", offsetof(CPUState
, gpr
[18]) },
2930 { "r19", offsetof(CPUState
, gpr
[19]) },
2931 { "r20", offsetof(CPUState
, gpr
[20]) },
2932 { "r21", offsetof(CPUState
, gpr
[21]) },
2933 { "r22", offsetof(CPUState
, gpr
[22]) },
2934 { "r23", offsetof(CPUState
, gpr
[23]) },
2935 { "r24", offsetof(CPUState
, gpr
[24]) },
2936 { "r25", offsetof(CPUState
, gpr
[25]) },
2937 { "r26", offsetof(CPUState
, gpr
[26]) },
2938 { "r27", offsetof(CPUState
, gpr
[27]) },
2939 { "r28", offsetof(CPUState
, gpr
[28]) },
2940 { "r29", offsetof(CPUState
, gpr
[29]) },
2941 { "r30", offsetof(CPUState
, gpr
[30]) },
2942 { "r31", offsetof(CPUState
, gpr
[31]) },
2943 /* Floating point registers */
2944 { "f0", offsetof(CPUState
, fpr
[0]) },
2945 { "f1", offsetof(CPUState
, fpr
[1]) },
2946 { "f2", offsetof(CPUState
, fpr
[2]) },
2947 { "f3", offsetof(CPUState
, fpr
[3]) },
2948 { "f4", offsetof(CPUState
, fpr
[4]) },
2949 { "f5", offsetof(CPUState
, fpr
[5]) },
2950 { "f6", offsetof(CPUState
, fpr
[6]) },
2951 { "f7", offsetof(CPUState
, fpr
[7]) },
2952 { "f8", offsetof(CPUState
, fpr
[8]) },
2953 { "f9", offsetof(CPUState
, fpr
[9]) },
2954 { "f10", offsetof(CPUState
, fpr
[10]) },
2955 { "f11", offsetof(CPUState
, fpr
[11]) },
2956 { "f12", offsetof(CPUState
, fpr
[12]) },
2957 { "f13", offsetof(CPUState
, fpr
[13]) },
2958 { "f14", offsetof(CPUState
, fpr
[14]) },
2959 { "f15", offsetof(CPUState
, fpr
[15]) },
2960 { "f16", offsetof(CPUState
, fpr
[16]) },
2961 { "f17", offsetof(CPUState
, fpr
[17]) },
2962 { "f18", offsetof(CPUState
, fpr
[18]) },
2963 { "f19", offsetof(CPUState
, fpr
[19]) },
2964 { "f20", offsetof(CPUState
, fpr
[20]) },
2965 { "f21", offsetof(CPUState
, fpr
[21]) },
2966 { "f22", offsetof(CPUState
, fpr
[22]) },
2967 { "f23", offsetof(CPUState
, fpr
[23]) },
2968 { "f24", offsetof(CPUState
, fpr
[24]) },
2969 { "f25", offsetof(CPUState
, fpr
[25]) },
2970 { "f26", offsetof(CPUState
, fpr
[26]) },
2971 { "f27", offsetof(CPUState
, fpr
[27]) },
2972 { "f28", offsetof(CPUState
, fpr
[28]) },
2973 { "f29", offsetof(CPUState
, fpr
[29]) },
2974 { "f30", offsetof(CPUState
, fpr
[30]) },
2975 { "f31", offsetof(CPUState
, fpr
[31]) },
2976 { "fpscr", offsetof(CPUState
, fpscr
) },
2977 /* Next instruction pointer */
2978 { "nip|pc", offsetof(CPUState
, nip
) },
2979 { "lr", offsetof(CPUState
, lr
) },
2980 { "ctr", offsetof(CPUState
, ctr
) },
2981 { "decr", 0, &monitor_get_decr
, },
2982 { "ccr", 0, &monitor_get_ccr
, },
2983 /* Machine state register */
2984 { "msr", 0, &monitor_get_msr
, },
2985 { "xer", 0, &monitor_get_xer
, },
2986 { "tbu", 0, &monitor_get_tbu
, },
2987 { "tbl", 0, &monitor_get_tbl
, },
2988 #if defined(TARGET_PPC64)
2989 /* Address space register */
2990 { "asr", offsetof(CPUState
, asr
) },
2992 /* Segment registers */
2993 { "sdr1", offsetof(CPUState
, sdr1
) },
2994 { "sr0", offsetof(CPUState
, sr
[0]) },
2995 { "sr1", offsetof(CPUState
, sr
[1]) },
2996 { "sr2", offsetof(CPUState
, sr
[2]) },
2997 { "sr3", offsetof(CPUState
, sr
[3]) },
2998 { "sr4", offsetof(CPUState
, sr
[4]) },
2999 { "sr5", offsetof(CPUState
, sr
[5]) },
3000 { "sr6", offsetof(CPUState
, sr
[6]) },
3001 { "sr7", offsetof(CPUState
, sr
[7]) },
3002 { "sr8", offsetof(CPUState
, sr
[8]) },
3003 { "sr9", offsetof(CPUState
, sr
[9]) },
3004 { "sr10", offsetof(CPUState
, sr
[10]) },
3005 { "sr11", offsetof(CPUState
, sr
[11]) },
3006 { "sr12", offsetof(CPUState
, sr
[12]) },
3007 { "sr13", offsetof(CPUState
, sr
[13]) },
3008 { "sr14", offsetof(CPUState
, sr
[14]) },
3009 { "sr15", offsetof(CPUState
, sr
[15]) },
3010 /* Too lazy to put BATs and SPRs ... */
3011 #elif defined(TARGET_SPARC)
3012 { "g0", offsetof(CPUState
, gregs
[0]) },
3013 { "g1", offsetof(CPUState
, gregs
[1]) },
3014 { "g2", offsetof(CPUState
, gregs
[2]) },
3015 { "g3", offsetof(CPUState
, gregs
[3]) },
3016 { "g4", offsetof(CPUState
, gregs
[4]) },
3017 { "g5", offsetof(CPUState
, gregs
[5]) },
3018 { "g6", offsetof(CPUState
, gregs
[6]) },
3019 { "g7", offsetof(CPUState
, gregs
[7]) },
3020 { "o0", 0, monitor_get_reg
},
3021 { "o1", 1, monitor_get_reg
},
3022 { "o2", 2, monitor_get_reg
},
3023 { "o3", 3, monitor_get_reg
},
3024 { "o4", 4, monitor_get_reg
},
3025 { "o5", 5, monitor_get_reg
},
3026 { "o6", 6, monitor_get_reg
},
3027 { "o7", 7, monitor_get_reg
},
3028 { "l0", 8, monitor_get_reg
},
3029 { "l1", 9, monitor_get_reg
},
3030 { "l2", 10, monitor_get_reg
},
3031 { "l3", 11, monitor_get_reg
},
3032 { "l4", 12, monitor_get_reg
},
3033 { "l5", 13, monitor_get_reg
},
3034 { "l6", 14, monitor_get_reg
},
3035 { "l7", 15, monitor_get_reg
},
3036 { "i0", 16, monitor_get_reg
},
3037 { "i1", 17, monitor_get_reg
},
3038 { "i2", 18, monitor_get_reg
},
3039 { "i3", 19, monitor_get_reg
},
3040 { "i4", 20, monitor_get_reg
},
3041 { "i5", 21, monitor_get_reg
},
3042 { "i6", 22, monitor_get_reg
},
3043 { "i7", 23, monitor_get_reg
},
3044 { "pc", offsetof(CPUState
, pc
) },
3045 { "npc", offsetof(CPUState
, npc
) },
3046 { "y", offsetof(CPUState
, y
) },
3047 #ifndef TARGET_SPARC64
3048 { "psr", 0, &monitor_get_psr
, },
3049 { "wim", offsetof(CPUState
, wim
) },
3051 { "tbr", offsetof(CPUState
, tbr
) },
3052 { "fsr", offsetof(CPUState
, fsr
) },
3053 { "f0", offsetof(CPUState
, fpr
[0]) },
3054 { "f1", offsetof(CPUState
, fpr
[1]) },
3055 { "f2", offsetof(CPUState
, fpr
[2]) },
3056 { "f3", offsetof(CPUState
, fpr
[3]) },
3057 { "f4", offsetof(CPUState
, fpr
[4]) },
3058 { "f5", offsetof(CPUState
, fpr
[5]) },
3059 { "f6", offsetof(CPUState
, fpr
[6]) },
3060 { "f7", offsetof(CPUState
, fpr
[7]) },
3061 { "f8", offsetof(CPUState
, fpr
[8]) },
3062 { "f9", offsetof(CPUState
, fpr
[9]) },
3063 { "f10", offsetof(CPUState
, fpr
[10]) },
3064 { "f11", offsetof(CPUState
, fpr
[11]) },
3065 { "f12", offsetof(CPUState
, fpr
[12]) },
3066 { "f13", offsetof(CPUState
, fpr
[13]) },
3067 { "f14", offsetof(CPUState
, fpr
[14]) },
3068 { "f15", offsetof(CPUState
, fpr
[15]) },
3069 { "f16", offsetof(CPUState
, fpr
[16]) },
3070 { "f17", offsetof(CPUState
, fpr
[17]) },
3071 { "f18", offsetof(CPUState
, fpr
[18]) },
3072 { "f19", offsetof(CPUState
, fpr
[19]) },
3073 { "f20", offsetof(CPUState
, fpr
[20]) },
3074 { "f21", offsetof(CPUState
, fpr
[21]) },
3075 { "f22", offsetof(CPUState
, fpr
[22]) },
3076 { "f23", offsetof(CPUState
, fpr
[23]) },
3077 { "f24", offsetof(CPUState
, fpr
[24]) },
3078 { "f25", offsetof(CPUState
, fpr
[25]) },
3079 { "f26", offsetof(CPUState
, fpr
[26]) },
3080 { "f27", offsetof(CPUState
, fpr
[27]) },
3081 { "f28", offsetof(CPUState
, fpr
[28]) },
3082 { "f29", offsetof(CPUState
, fpr
[29]) },
3083 { "f30", offsetof(CPUState
, fpr
[30]) },
3084 { "f31", offsetof(CPUState
, fpr
[31]) },
3085 #ifdef TARGET_SPARC64
3086 { "f32", offsetof(CPUState
, fpr
[32]) },
3087 { "f34", offsetof(CPUState
, fpr
[34]) },
3088 { "f36", offsetof(CPUState
, fpr
[36]) },
3089 { "f38", offsetof(CPUState
, fpr
[38]) },
3090 { "f40", offsetof(CPUState
, fpr
[40]) },
3091 { "f42", offsetof(CPUState
, fpr
[42]) },
3092 { "f44", offsetof(CPUState
, fpr
[44]) },
3093 { "f46", offsetof(CPUState
, fpr
[46]) },
3094 { "f48", offsetof(CPUState
, fpr
[48]) },
3095 { "f50", offsetof(CPUState
, fpr
[50]) },
3096 { "f52", offsetof(CPUState
, fpr
[52]) },
3097 { "f54", offsetof(CPUState
, fpr
[54]) },
3098 { "f56", offsetof(CPUState
, fpr
[56]) },
3099 { "f58", offsetof(CPUState
, fpr
[58]) },
3100 { "f60", offsetof(CPUState
, fpr
[60]) },
3101 { "f62", offsetof(CPUState
, fpr
[62]) },
3102 { "asi", offsetof(CPUState
, asi
) },
3103 { "pstate", offsetof(CPUState
, pstate
) },
3104 { "cansave", offsetof(CPUState
, cansave
) },
3105 { "canrestore", offsetof(CPUState
, canrestore
) },
3106 { "otherwin", offsetof(CPUState
, otherwin
) },
3107 { "wstate", offsetof(CPUState
, wstate
) },
3108 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3109 { "fprs", offsetof(CPUState
, fprs
) },
3115 static void expr_error(Monitor
*mon
, const char *msg
)
3117 monitor_printf(mon
, "%s\n", msg
);
3118 longjmp(expr_env
, 1);
3121 /* return 0 if OK, -1 if not found */
3122 static int get_monitor_def(target_long
*pval
, const char *name
)
3124 const MonitorDef
*md
;
3127 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3128 if (compare_cmd(name
, md
->name
)) {
3129 if (md
->get_value
) {
3130 *pval
= md
->get_value(md
, md
->offset
);
3132 CPUState
*env
= mon_get_cpu();
3133 ptr
= (uint8_t *)env
+ md
->offset
;
3136 *pval
= *(int32_t *)ptr
;
3139 *pval
= *(target_long
*)ptr
;
3152 static void next(void)
3156 while (qemu_isspace(*pch
))
3161 static int64_t expr_sum(Monitor
*mon
);
3163 static int64_t expr_unary(Monitor
*mon
)
3172 n
= expr_unary(mon
);
3176 n
= -expr_unary(mon
);
3180 n
= ~expr_unary(mon
);
3186 expr_error(mon
, "')' expected");
3193 expr_error(mon
, "character constant expected");
3197 expr_error(mon
, "missing terminating \' character");
3207 while ((*pch
>= 'a' && *pch
<= 'z') ||
3208 (*pch
>= 'A' && *pch
<= 'Z') ||
3209 (*pch
>= '0' && *pch
<= '9') ||
3210 *pch
== '_' || *pch
== '.') {
3211 if ((q
- buf
) < sizeof(buf
) - 1)
3215 while (qemu_isspace(*pch
))
3218 ret
= get_monitor_def(®
, buf
);
3220 expr_error(mon
, "unknown register");
3225 expr_error(mon
, "unexpected end of expression");
3229 #if TARGET_PHYS_ADDR_BITS > 32
3230 n
= strtoull(pch
, &p
, 0);
3232 n
= strtoul(pch
, &p
, 0);
3235 expr_error(mon
, "invalid char in expression");
3238 while (qemu_isspace(*pch
))
3246 static int64_t expr_prod(Monitor
*mon
)
3251 val
= expr_unary(mon
);
3254 if (op
!= '*' && op
!= '/' && op
!= '%')
3257 val2
= expr_unary(mon
);
3266 expr_error(mon
, "division by zero");
3277 static int64_t expr_logic(Monitor
*mon
)
3282 val
= expr_prod(mon
);
3285 if (op
!= '&' && op
!= '|' && op
!= '^')
3288 val2
= expr_prod(mon
);
3305 static int64_t expr_sum(Monitor
*mon
)
3310 val
= expr_logic(mon
);
3313 if (op
!= '+' && op
!= '-')
3316 val2
= expr_logic(mon
);
3325 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3328 if (setjmp(expr_env
)) {
3332 while (qemu_isspace(*pch
))
3334 *pval
= expr_sum(mon
);
3339 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3341 const char *p
= *pp
;
3345 d
= strtod(p
, &tailp
);
3347 monitor_printf(mon
, "Number expected\n");
3350 if (d
!= d
|| d
- d
!= 0) {
3351 /* NaN or infinity */
3352 monitor_printf(mon
, "Bad number\n");
3360 static int get_str(char *buf
, int buf_size
, const char **pp
)
3368 while (qemu_isspace(*p
))
3378 while (*p
!= '\0' && *p
!= '\"') {
3394 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3397 if ((q
- buf
) < buf_size
- 1) {
3401 if ((q
- buf
) < buf_size
- 1) {
3408 qemu_printf("unterminated string\n");
3413 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3414 if ((q
- buf
) < buf_size
- 1) {
3426 * Store the command-name in cmdname, and return a pointer to
3427 * the remaining of the command string.
3429 static const char *get_command_name(const char *cmdline
,
3430 char *cmdname
, size_t nlen
)
3433 const char *p
, *pstart
;
3436 while (qemu_isspace(*p
))
3441 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3446 memcpy(cmdname
, pstart
, len
);
3447 cmdname
[len
] = '\0';
3452 * Read key of 'type' into 'key' and return the current
3455 static char *key_get_info(const char *type
, char **key
)
3463 p
= strchr(type
, ':');
3470 str
= qemu_malloc(len
+ 1);
3471 memcpy(str
, type
, len
);
3478 static int default_fmt_format
= 'x';
3479 static int default_fmt_size
= 4;
3483 static int is_valid_option(const char *c
, const char *typestr
)
3491 typestr
= strstr(typestr
, option
);
3492 return (typestr
!= NULL
);
3495 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3497 const mon_cmd_t
*cmd
;
3499 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3500 if (compare_cmd(cmdname
, cmd
->name
)) {
3508 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3509 const char *cmdline
,
3512 const char *p
, *typestr
;
3514 const mon_cmd_t
*cmd
;
3520 monitor_printf(mon
, "command='%s'\n", cmdline
);
3523 /* extract the command name */
3524 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3528 cmd
= monitor_find_command(cmdname
);
3530 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3534 /* parse the parameters */
3535 typestr
= cmd
->args_type
;
3537 typestr
= key_get_info(typestr
, &key
);
3549 while (qemu_isspace(*p
))
3551 if (*typestr
== '?') {
3554 /* no optional string: NULL argument */
3558 ret
= get_str(buf
, sizeof(buf
), &p
);
3562 monitor_printf(mon
, "%s: filename expected\n",
3566 monitor_printf(mon
, "%s: block device name expected\n",
3570 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3575 qdict_put(qdict
, key
, qstring_from_str(buf
));
3580 int count
, format
, size
;
3582 while (qemu_isspace(*p
))
3588 if (qemu_isdigit(*p
)) {
3590 while (qemu_isdigit(*p
)) {
3591 count
= count
* 10 + (*p
- '0');
3629 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3630 monitor_printf(mon
, "invalid char in format: '%c'\n",
3635 format
= default_fmt_format
;
3636 if (format
!= 'i') {
3637 /* for 'i', not specifying a size gives -1 as size */
3639 size
= default_fmt_size
;
3640 default_fmt_size
= size
;
3642 default_fmt_format
= format
;
3645 format
= default_fmt_format
;
3646 if (format
!= 'i') {
3647 size
= default_fmt_size
;
3652 qdict_put(qdict
, "count", qint_from_int(count
));
3653 qdict_put(qdict
, "format", qint_from_int(format
));
3654 qdict_put(qdict
, "size", qint_from_int(size
));
3663 while (qemu_isspace(*p
))
3665 if (*typestr
== '?' || *typestr
== '.') {
3666 if (*typestr
== '?') {
3674 while (qemu_isspace(*p
))
3683 if (get_expr(mon
, &val
, &p
))
3685 /* Check if 'i' is greater than 32-bit */
3686 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3687 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3688 monitor_printf(mon
, "integer is for 32-bit values\n");
3690 } else if (c
== 'M') {
3693 qdict_put(qdict
, key
, qint_from_int(val
));
3701 while (qemu_isspace(*p
))
3703 if (*typestr
== '?') {
3709 if (get_double(mon
, &val
, &p
) < 0) {
3712 if (c
== 'b' && *p
) {
3715 val
*= 1 << 10; p
++; break;
3717 val
*= 1 << 20; p
++; break;
3719 val
*= 1 << 30; p
++; break;
3722 if (c
== 'T' && p
[0] && p
[1] == 's') {
3725 val
/= 1e3
; p
+= 2; break;
3727 val
/= 1e6
; p
+= 2; break;
3729 val
/= 1e9
; p
+= 2; break;
3732 if (*p
&& !qemu_isspace(*p
)) {
3733 monitor_printf(mon
, "Unknown unit suffix\n");
3736 qdict_put(qdict
, key
, qfloat_from_double(val
));
3741 const char *tmp
= p
;
3742 int has_option
, skip_key
= 0;
3748 while (qemu_isspace(*p
))
3754 if(!is_valid_option(p
, typestr
)) {
3756 monitor_printf(mon
, "%s: unsupported option -%c\n",
3770 qdict_put(qdict
, key
, qint_from_int(has_option
));
3775 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3781 /* check that all arguments were parsed */
3782 while (qemu_isspace(*p
))
3785 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3797 static void monitor_print_error(Monitor
*mon
)
3799 qerror_print(mon
->error
);
3800 QDECREF(mon
->error
);
3804 static int is_async_return(const QObject
*data
)
3806 if (data
&& qobject_type(data
) == QTYPE_QDICT
) {
3807 return qdict_haskey(qobject_to_qdict(data
), "__mon_async");
3813 static void monitor_call_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
3814 const QDict
*params
)
3816 QObject
*data
= NULL
;
3818 if (cmd
->cmd_new_ret
) {
3819 cmd
->cmd_new_ret(mon
, params
, &data
);
3821 cmd
->mhandler
.cmd_new(mon
, params
, &data
);
3824 if (is_async_return(data
)) {
3826 * Asynchronous commands have no initial return data but they can
3827 * generate errors. Data is returned via the async completion handler.
3829 if (monitor_ctrl_mode(mon
) && monitor_has_error(mon
)) {
3830 monitor_protocol_emitter(mon
, NULL
);
3832 } else if (monitor_ctrl_mode(mon
)) {
3833 /* Monitor Protocol */
3834 monitor_protocol_emitter(mon
, data
);
3838 cmd
->user_print(mon
, data
);
3841 qobject_decref(data
);
3844 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
3847 const mon_cmd_t
*cmd
;
3849 qdict
= qdict_new();
3851 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3855 qemu_errors_to_mon(mon
);
3857 if (monitor_handler_is_async(cmd
)) {
3858 user_async_cmd_handler(mon
, cmd
, qdict
);
3859 } else if (monitor_handler_ported(cmd
)) {
3860 monitor_call_handler(mon
, cmd
, qdict
);
3862 cmd
->mhandler
.cmd(mon
, qdict
);
3865 if (monitor_has_error(mon
))
3866 monitor_print_error(mon
);
3868 qemu_errors_to_previous();
3874 static void cmd_completion(const char *name
, const char *list
)
3876 const char *p
, *pstart
;
3885 p
= pstart
+ strlen(pstart
);
3887 if (len
> sizeof(cmd
) - 2)
3888 len
= sizeof(cmd
) - 2;
3889 memcpy(cmd
, pstart
, len
);
3891 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3892 readline_add_completion(cur_mon
->rs
, cmd
);
3900 static void file_completion(const char *input
)
3905 char file
[1024], file_prefix
[1024];
3909 p
= strrchr(input
, '/');
3912 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3913 pstrcpy(path
, sizeof(path
), ".");
3915 input_path_len
= p
- input
+ 1;
3916 memcpy(path
, input
, input_path_len
);
3917 if (input_path_len
> sizeof(path
) - 1)
3918 input_path_len
= sizeof(path
) - 1;
3919 path
[input_path_len
] = '\0';
3920 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3922 #ifdef DEBUG_COMPLETION
3923 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3924 input
, path
, file_prefix
);
3926 ffs
= opendir(path
);
3934 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3935 memcpy(file
, input
, input_path_len
);
3936 if (input_path_len
< sizeof(file
))
3937 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3939 /* stat the file to find out if it's a directory.
3940 * In that case add a slash to speed up typing long paths
3943 if(S_ISDIR(sb
.st_mode
))
3944 pstrcat(file
, sizeof(file
), "/");
3945 readline_add_completion(cur_mon
->rs
, file
);
3951 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3953 const char *name
= bdrv_get_device_name(bs
);
3954 const char *input
= opaque
;
3956 if (input
[0] == '\0' ||
3957 !strncmp(name
, (char *)input
, strlen(input
))) {
3958 readline_add_completion(cur_mon
->rs
, name
);
3962 /* NOTE: this parser is an approximate form of the real command parser */
3963 static void parse_cmdline(const char *cmdline
,
3964 int *pnb_args
, char **args
)
3973 while (qemu_isspace(*p
))
3977 if (nb_args
>= MAX_ARGS
)
3979 ret
= get_str(buf
, sizeof(buf
), &p
);
3980 args
[nb_args
] = qemu_strdup(buf
);
3985 *pnb_args
= nb_args
;
3988 static const char *next_arg_type(const char *typestr
)
3990 const char *p
= strchr(typestr
, ':');
3991 return (p
!= NULL
? ++p
: typestr
);
3994 static void monitor_find_completion(const char *cmdline
)
3996 const char *cmdname
;
3997 char *args
[MAX_ARGS
];
3998 int nb_args
, i
, len
;
3999 const char *ptype
, *str
;
4000 const mon_cmd_t
*cmd
;
4003 parse_cmdline(cmdline
, &nb_args
, args
);
4004 #ifdef DEBUG_COMPLETION
4005 for(i
= 0; i
< nb_args
; i
++) {
4006 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4010 /* if the line ends with a space, it means we want to complete the
4012 len
= strlen(cmdline
);
4013 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4014 if (nb_args
>= MAX_ARGS
)
4016 args
[nb_args
++] = qemu_strdup("");
4019 /* command completion */
4024 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4025 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4026 cmd_completion(cmdname
, cmd
->name
);
4029 /* find the command */
4030 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4031 if (compare_cmd(args
[0], cmd
->name
))
4036 ptype
= next_arg_type(cmd
->args_type
);
4037 for(i
= 0; i
< nb_args
- 2; i
++) {
4038 if (*ptype
!= '\0') {
4039 ptype
= next_arg_type(ptype
);
4040 while (*ptype
== '?')
4041 ptype
= next_arg_type(ptype
);
4044 str
= args
[nb_args
- 1];
4045 if (*ptype
== '-' && ptype
[1] != '\0') {
4050 /* file completion */
4051 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4052 file_completion(str
);
4055 /* block device name completion */
4056 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4057 bdrv_iterate(block_completion_it
, (void *)str
);
4060 /* XXX: more generic ? */
4061 if (!strcmp(cmd
->name
, "info")) {
4062 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4063 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4064 cmd_completion(str
, cmd
->name
);
4066 } else if (!strcmp(cmd
->name
, "sendkey")) {
4067 char *sep
= strrchr(str
, '-');
4070 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4071 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4072 cmd_completion(str
, key
->name
);
4074 } else if (!strcmp(cmd
->name
, "help|?")) {
4075 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4076 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4077 cmd_completion(str
, cmd
->name
);
4085 for(i
= 0; i
< nb_args
; i
++)
4089 static int monitor_can_read(void *opaque
)
4091 Monitor
*mon
= opaque
;
4093 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4096 typedef struct CmdArgs
{
4103 static int check_opt(const CmdArgs
*cmd_args
, const char *name
, QDict
*args
)
4105 if (!cmd_args
->optional
) {
4106 qemu_error_new(QERR_MISSING_PARAMETER
, name
);
4110 if (cmd_args
->type
== '-') {
4111 /* handlers expect a value, they need to be changed */
4112 qdict_put(args
, name
, qint_from_int(0));
4118 static int check_arg(const CmdArgs
*cmd_args
, QDict
*args
)
4123 name
= qstring_get_str(cmd_args
->name
);
4126 return check_opt(cmd_args
, name
, args
);
4129 value
= qdict_get(args
, name
);
4131 return check_opt(cmd_args
, name
, args
);
4134 switch (cmd_args
->type
) {
4138 if (qobject_type(value
) != QTYPE_QSTRING
) {
4139 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "string");
4145 const char *keys
[] = { "count", "format", "size", NULL
};
4147 for (i
= 0; keys
[i
]; i
++) {
4148 QObject
*obj
= qdict_get(args
, keys
[i
]);
4150 qemu_error_new(QERR_MISSING_PARAMETER
, name
);
4153 if (qobject_type(obj
) != QTYPE_QINT
) {
4154 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "int");
4163 if (qobject_type(value
) != QTYPE_QINT
) {
4164 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "int");
4170 if (qobject_type(value
) != QTYPE_QINT
&& qobject_type(value
) != QTYPE_QFLOAT
) {
4171 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "number");
4176 if (qobject_type(value
) != QTYPE_QINT
&&
4177 qobject_type(value
) != QTYPE_QBOOL
) {
4178 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "bool");
4181 if (qobject_type(value
) == QTYPE_QBOOL
) {
4182 /* handlers expect a QInt, they need to be changed */
4183 qdict_put(args
, name
,
4184 qint_from_int(qbool_get_int(qobject_to_qbool(value
))));
4195 static void cmd_args_init(CmdArgs
*cmd_args
)
4197 cmd_args
->name
= qstring_new();
4198 cmd_args
->type
= cmd_args
->flag
= cmd_args
->optional
= 0;
4202 * This is not trivial, we have to parse Monitor command's argument
4203 * type syntax to be able to check the arguments provided by clients.
4205 * In the near future we will be using an array for that and will be
4206 * able to drop all this parsing...
4208 static int monitor_check_qmp_args(const mon_cmd_t
*cmd
, QDict
*args
)
4214 if (cmd
->args_type
== NULL
) {
4215 return (qdict_size(args
) == 0 ? 0 : -1);
4219 cmd_args_init(&cmd_args
);
4221 for (p
= cmd
->args_type
;; p
++) {
4223 cmd_args
.type
= *++p
;
4225 if (cmd_args
.type
== '-') {
4226 cmd_args
.flag
= *p
++;
4227 cmd_args
.optional
= 1;
4228 } else if (*p
== '?') {
4229 cmd_args
.optional
= 1;
4233 assert(*p
== ',' || *p
== '\0');
4234 err
= check_arg(&cmd_args
, args
);
4236 QDECREF(cmd_args
.name
);
4237 cmd_args_init(&cmd_args
);
4243 qstring_append_chr(cmd_args
.name
, *p
);
4251 QDECREF(cmd_args
.name
);
4255 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4257 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4258 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4261 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4265 QDict
*input
, *args
;
4266 const mon_cmd_t
*cmd
;
4267 Monitor
*mon
= cur_mon
;
4268 const char *cmd_name
, *info_item
;
4271 qemu_errors_to_mon(mon
);
4273 obj
= json_parser_parse(tokens
, NULL
);
4275 // FIXME: should be triggered in json_parser_parse()
4276 qemu_error_new(QERR_JSON_PARSING
);
4278 } else if (qobject_type(obj
) != QTYPE_QDICT
) {
4279 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4280 qobject_decref(obj
);
4284 input
= qobject_to_qdict(obj
);
4286 mon
->mc
->id
= qdict_get(input
, "id");
4287 qobject_incref(mon
->mc
->id
);
4289 obj
= qdict_get(input
, "execute");
4291 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4293 } else if (qobject_type(obj
) != QTYPE_QSTRING
) {
4294 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT
, "string");
4298 cmd_name
= qstring_get_str(qobject_to_qstring(obj
));
4300 if (invalid_qmp_mode(mon
, cmd_name
)) {
4301 qemu_error_new(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4306 * XXX: We need this special case until we get info handlers
4307 * converted into 'query-' commands
4309 if (compare_cmd(cmd_name
, "info")) {
4310 qemu_error_new(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4312 } else if (strstart(cmd_name
, "query-", &info_item
)) {
4313 cmd
= monitor_find_command("info");
4314 qdict_put_obj(input
, "arguments",
4315 qobject_from_jsonf("{ 'item': %s }", info_item
));
4317 cmd
= monitor_find_command(cmd_name
);
4318 if (!cmd
|| !monitor_handler_ported(cmd
)) {
4319 qemu_error_new(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4324 obj
= qdict_get(input
, "arguments");
4328 args
= qobject_to_qdict(obj
);
4334 err
= monitor_check_qmp_args(cmd
, args
);
4339 if (monitor_handler_is_async(cmd
)) {
4340 qmp_async_cmd_handler(mon
, cmd
, args
);
4342 monitor_call_handler(mon
, cmd
, args
);
4349 monitor_protocol_emitter(mon
, NULL
);
4352 qemu_errors_to_previous();
4356 * monitor_control_read(): Read and handle QMP input
4358 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4360 Monitor
*old_mon
= cur_mon
;
4364 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4369 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4371 Monitor
*old_mon
= cur_mon
;
4377 for (i
= 0; i
< size
; i
++)
4378 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4380 if (size
== 0 || buf
[size
- 1] != 0)
4381 monitor_printf(cur_mon
, "corrupted command\n");
4383 handle_user_command(cur_mon
, (char *)buf
);
4389 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4391 monitor_suspend(mon
);
4392 handle_user_command(mon
, cmdline
);
4393 monitor_resume(mon
);
4396 int monitor_suspend(Monitor
*mon
)
4404 void monitor_resume(Monitor
*mon
)
4408 if (--mon
->suspend_cnt
== 0)
4409 readline_show_prompt(mon
->rs
);
4412 static QObject
*get_qmp_greeting(void)
4416 do_info_version(NULL
, &ver
);
4417 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4421 * monitor_control_event(): Print QMP gretting
4423 static void monitor_control_event(void *opaque
, int event
)
4426 Monitor
*mon
= opaque
;
4429 case CHR_EVENT_OPENED
:
4430 mon
->mc
->command_mode
= 0;
4431 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4432 data
= get_qmp_greeting();
4433 monitor_json_emitter(mon
, data
);
4434 qobject_decref(data
);
4436 case CHR_EVENT_CLOSED
:
4437 json_message_parser_destroy(&mon
->mc
->parser
);
4442 static void monitor_event(void *opaque
, int event
)
4444 Monitor
*mon
= opaque
;
4447 case CHR_EVENT_MUX_IN
:
4449 if (mon
->reset_seen
) {
4450 readline_restart(mon
->rs
);
4451 monitor_resume(mon
);
4454 mon
->suspend_cnt
= 0;
4458 case CHR_EVENT_MUX_OUT
:
4459 if (mon
->reset_seen
) {
4460 if (mon
->suspend_cnt
== 0) {
4461 monitor_printf(mon
, "\n");
4464 monitor_suspend(mon
);
4471 case CHR_EVENT_OPENED
:
4472 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4473 "information\n", QEMU_VERSION
);
4474 if (!mon
->mux_out
) {
4475 readline_show_prompt(mon
->rs
);
4477 mon
->reset_seen
= 1;
4491 void monitor_init(CharDriverState
*chr
, int flags
)
4493 static int is_first_init
= 1;
4496 if (is_first_init
) {
4497 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
4501 mon
= qemu_mallocz(sizeof(*mon
));
4505 if (flags
& MONITOR_USE_READLINE
) {
4506 mon
->rs
= readline_init(mon
, monitor_find_completion
);
4507 monitor_read_command(mon
, 0);
4510 if (monitor_ctrl_mode(mon
)) {
4511 mon
->mc
= qemu_mallocz(sizeof(MonitorControl
));
4512 /* Control mode requires special handlers */
4513 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
4514 monitor_control_event
, mon
);
4516 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4517 monitor_event
, mon
);
4520 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4521 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
4525 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
4527 BlockDriverState
*bs
= opaque
;
4530 if (bdrv_set_key(bs
, password
) != 0) {
4531 monitor_printf(mon
, "invalid password\n");
4534 if (mon
->password_completion_cb
)
4535 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4537 monitor_read_command(mon
, 1);
4540 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4541 BlockDriverCompletionFunc
*completion_cb
,
4546 if (!bdrv_key_required(bs
)) {
4548 completion_cb(opaque
, 0);
4552 if (monitor_ctrl_mode(mon
)) {
4553 qemu_error_new(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
4557 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4558 bdrv_get_encrypted_filename(bs
));
4560 mon
->password_completion_cb
= completion_cb
;
4561 mon
->password_opaque
= opaque
;
4563 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4565 if (err
&& completion_cb
)
4566 completion_cb(opaque
, err
);
4569 typedef struct QemuErrorSink QemuErrorSink
;
4570 struct QemuErrorSink
{
4579 QemuErrorSink
*previous
;
4582 static QemuErrorSink
*qemu_error_sink
;
4584 void qemu_errors_to_file(FILE *fp
)
4586 QemuErrorSink
*sink
;
4588 sink
= qemu_mallocz(sizeof(*sink
));
4589 sink
->dest
= ERR_SINK_FILE
;
4591 sink
->previous
= qemu_error_sink
;
4592 qemu_error_sink
= sink
;
4595 void qemu_errors_to_mon(Monitor
*mon
)
4597 QemuErrorSink
*sink
;
4599 sink
= qemu_mallocz(sizeof(*sink
));
4600 sink
->dest
= ERR_SINK_MONITOR
;
4602 sink
->previous
= qemu_error_sink
;
4603 qemu_error_sink
= sink
;
4606 void qemu_errors_to_previous(void)
4608 QemuErrorSink
*sink
;
4610 assert(qemu_error_sink
!= NULL
);
4611 sink
= qemu_error_sink
;
4612 qemu_error_sink
= sink
->previous
;
4616 void qemu_error(const char *fmt
, ...)
4620 assert(qemu_error_sink
!= NULL
);
4621 switch (qemu_error_sink
->dest
) {
4623 va_start(args
, fmt
);
4624 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
4627 case ERR_SINK_MONITOR
:
4628 va_start(args
, fmt
);
4629 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);
4635 void qemu_error_internal(const char *file
, int linenr
, const char *func
,
4636 const char *fmt
, ...)
4641 assert(qemu_error_sink
!= NULL
);
4644 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
4647 switch (qemu_error_sink
->dest
) {
4649 qerror_print(qerror
);
4652 case ERR_SINK_MONITOR
:
4653 /* report only the first error */
4654 if (!qemu_error_sink
->mon
->error
) {
4655 qemu_error_sink
->mon
->error
= qerror
;
4657 /* XXX: warn the programmer */