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 int 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
);
611 cmd
->mhandler
.info(mon
);
618 help_cmd(mon
, "info");
622 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
626 qdict
= qobject_to_qdict(data
);
628 monitor_printf(mon
, "%s%s\n", qdict_get_str(qdict
, "qemu"),
629 qdict_get_str(qdict
, "package"));
633 * do_info_version(): Show QEMU version
635 * Return a QDict with the following information:
637 * - "qemu": QEMU's version
638 * - "package": package's version
642 * { "qemu": "0.11.50", "package": "" }
644 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
646 *ret_data
= qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
647 QEMU_VERSION
, QEMU_PKGVERSION
);
650 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
654 qdict
= qobject_to_qdict(data
);
655 if (qdict_size(qdict
) == 0) {
659 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
663 * do_info_name(): Show VM name
665 * Return a QDict with the following information:
667 * - "name": VM's name (optional)
671 * { "name": "qemu-name" }
673 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
675 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
676 qobject_from_jsonf("{}");
679 static QObject
*get_cmd_dict(const char *name
)
683 /* Remove '|' from some commands */
684 p
= strchr(name
, '|');
691 return qobject_from_jsonf("{ 'name': %s }", p
);
695 * do_info_commands(): List QMP available commands
697 * Each command is represented by a QDict, the returned QObject is a QList
700 * The QDict contains:
702 * - "name": command's name
706 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
708 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
711 const mon_cmd_t
*cmd
;
713 cmd_list
= qlist_new();
715 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
716 if (monitor_handler_ported(cmd
) && !compare_cmd(cmd
->name
, "info")) {
717 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
721 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
722 if (monitor_handler_ported(cmd
)) {
724 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
725 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
729 *ret_data
= QOBJECT(cmd_list
);
732 #if defined(TARGET_I386)
733 static void do_info_hpet_print(Monitor
*mon
, const QObject
*data
)
735 monitor_printf(mon
, "HPET is %s by QEMU\n",
736 qdict_get_bool(qobject_to_qdict(data
), "enabled") ?
737 "enabled" : "disabled");
741 * do_info_hpet(): Show HPET state
743 * Return a QDict with the following information:
745 * - "enabled": true if hpet if enabled, false otherwise
749 * { "enabled": true }
751 static void do_info_hpet(Monitor
*mon
, QObject
**ret_data
)
753 *ret_data
= qobject_from_jsonf("{ 'enabled': %i }", !no_hpet
);
757 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
759 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
763 * do_info_uuid(): Show VM UUID
765 * Return a QDict with the following information:
767 * - "UUID": Universally Unique Identifier
771 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
773 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
777 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
778 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
779 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
780 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
781 qemu_uuid
[14], qemu_uuid
[15]);
782 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
785 /* get the current CPU defined by the user */
786 static int mon_set_cpu(int cpu_index
)
790 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
791 if (env
->cpu_index
== cpu_index
) {
792 cur_mon
->mon_cpu
= env
;
799 static CPUState
*mon_get_cpu(void)
801 if (!cur_mon
->mon_cpu
) {
804 cpu_synchronize_state(cur_mon
->mon_cpu
);
805 return cur_mon
->mon_cpu
;
808 static void do_info_registers(Monitor
*mon
)
813 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
816 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
821 static void print_cpu_iter(QObject
*obj
, void *opaque
)
825 Monitor
*mon
= opaque
;
827 assert(qobject_type(obj
) == QTYPE_QDICT
);
828 cpu
= qobject_to_qdict(obj
);
830 if (qdict_get_bool(cpu
, "current")) {
834 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
836 #if defined(TARGET_I386)
837 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
838 (target_ulong
) qdict_get_int(cpu
, "pc"));
839 #elif defined(TARGET_PPC)
840 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
841 (target_long
) qdict_get_int(cpu
, "nip"));
842 #elif defined(TARGET_SPARC)
843 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
844 (target_long
) qdict_get_int(cpu
, "pc"));
845 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
846 (target_long
) qdict_get_int(cpu
, "npc"));
847 #elif defined(TARGET_MIPS)
848 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
849 (target_long
) qdict_get_int(cpu
, "PC"));
852 if (qdict_get_bool(cpu
, "halted")) {
853 monitor_printf(mon
, " (halted)");
856 monitor_printf(mon
, "\n");
859 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
863 assert(qobject_type(data
) == QTYPE_QLIST
);
864 cpu_list
= qobject_to_qlist(data
);
865 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
869 * do_info_cpus(): Show CPU information
871 * Return a QList. Each CPU is represented by a QDict, which contains:
874 * - "current": true if this is the current CPU, false otherwise
875 * - "halted": true if the cpu is halted, false otherwise
876 * - Current program counter. The key's name depends on the architecture:
879 * "pc" and "npc": sparc
884 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
885 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
887 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
892 cpu_list
= qlist_new();
894 /* just to set the default cpu if not already done */
897 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
901 cpu_synchronize_state(env
);
903 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
904 env
->cpu_index
, env
== mon
->mon_cpu
,
907 cpu
= qobject_to_qdict(obj
);
909 #if defined(TARGET_I386)
910 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
911 #elif defined(TARGET_PPC)
912 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
913 #elif defined(TARGET_SPARC)
914 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
915 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
916 #elif defined(TARGET_MIPS)
917 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
920 qlist_append(cpu_list
, cpu
);
923 *ret_data
= QOBJECT(cpu_list
);
926 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
928 int index
= qdict_get_int(qdict
, "index");
929 if (mon_set_cpu(index
) < 0) {
930 qemu_error_new(QERR_INVALID_PARAMETER
, "index");
936 static void do_info_jit(Monitor
*mon
)
938 dump_exec_info((FILE *)mon
, monitor_fprintf
);
941 static void do_info_history(Monitor
*mon
)
950 str
= readline_get_history(mon
->rs
, i
);
953 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
958 #if defined(TARGET_PPC)
959 /* XXX: not implemented in other targets */
960 static void do_info_cpu_stats(Monitor
*mon
)
965 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
970 * do_quit(): Quit QEMU execution
972 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
978 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
980 if (bdrv_is_inserted(bs
)) {
982 if (!bdrv_is_removable(bs
)) {
983 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE
,
984 bdrv_get_device_name(bs
));
987 if (bdrv_is_locked(bs
)) {
988 qemu_error_new(QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
997 static int do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
999 BlockDriverState
*bs
;
1000 int force
= qdict_get_int(qdict
, "force");
1001 const char *filename
= qdict_get_str(qdict
, "device");
1003 bs
= bdrv_find(filename
);
1005 qemu_error_new(QERR_DEVICE_NOT_FOUND
, filename
);
1008 return eject_device(mon
, bs
, force
);
1011 static int do_block_set_passwd(Monitor
*mon
, const QDict
*qdict
,
1014 BlockDriverState
*bs
;
1016 bs
= bdrv_find(qdict_get_str(qdict
, "device"));
1018 qemu_error_new(QERR_DEVICE_NOT_FOUND
, qdict_get_str(qdict
, "device"));
1022 if (bdrv_set_key(bs
, qdict_get_str(qdict
, "password")) < 0) {
1023 qemu_error_new(QERR_INVALID_PASSWORD
);
1030 static int do_change_block(Monitor
*mon
, const char *device
,
1031 const char *filename
, const char *fmt
)
1033 BlockDriverState
*bs
;
1034 BlockDriver
*drv
= NULL
;
1036 bs
= bdrv_find(device
);
1038 qemu_error_new(QERR_DEVICE_NOT_FOUND
, device
);
1042 drv
= bdrv_find_whitelisted_format(fmt
);
1044 qemu_error_new(QERR_INVALID_BLOCK_FORMAT
, fmt
);
1048 if (eject_device(mon
, bs
, 0) < 0) {
1051 if (bdrv_open2(bs
, filename
, BDRV_O_RDWR
, drv
) < 0) {
1054 return monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
1057 static int change_vnc_password(const char *password
)
1059 if (vnc_display_password(NULL
, password
) < 0) {
1060 qemu_error_new(QERR_SET_PASSWD_FAILED
);
1067 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
1070 change_vnc_password(password
);
1071 monitor_read_command(mon
, 1);
1074 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1076 if (strcmp(target
, "passwd") == 0 ||
1077 strcmp(target
, "password") == 0) {
1080 strncpy(password
, arg
, sizeof(password
));
1081 password
[sizeof(password
) - 1] = '\0';
1082 return change_vnc_password(password
);
1084 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1087 if (vnc_display_open(NULL
, target
) < 0) {
1088 qemu_error_new(QERR_VNC_SERVER_FAILED
, target
);
1097 * do_change(): Change a removable medium, or VNC configuration
1099 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1101 const char *device
= qdict_get_str(qdict
, "device");
1102 const char *target
= qdict_get_str(qdict
, "target");
1103 const char *arg
= qdict_get_try_str(qdict
, "arg");
1106 if (strcmp(device
, "vnc") == 0) {
1107 ret
= do_change_vnc(mon
, target
, arg
);
1109 ret
= do_change_block(mon
, device
, target
, arg
);
1115 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
1117 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1120 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1122 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1125 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1128 const char *items
= qdict_get_str(qdict
, "items");
1130 if (!strcmp(items
, "none")) {
1133 mask
= cpu_str_to_log_mask(items
);
1135 help_cmd(mon
, "log");
1142 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1144 const char *option
= qdict_get_try_str(qdict
, "option");
1145 if (!option
|| !strcmp(option
, "on")) {
1147 } else if (!strcmp(option
, "off")) {
1150 monitor_printf(mon
, "unexpected option %s\n", option
);
1155 * do_stop(): Stop VM execution
1157 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1159 vm_stop(EXCP_INTERRUPT
);
1163 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1165 struct bdrv_iterate_context
{
1171 * do_cont(): Resume emulation.
1173 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1175 struct bdrv_iterate_context context
= { mon
, 0 };
1177 bdrv_iterate(encrypted_bdrv_it
, &context
);
1178 /* only resume the vm if all keys are set and valid */
1187 static void bdrv_key_cb(void *opaque
, int err
)
1189 Monitor
*mon
= opaque
;
1191 /* another key was set successfully, retry to continue */
1193 do_cont(mon
, NULL
, NULL
);
1196 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1198 struct bdrv_iterate_context
*context
= opaque
;
1200 if (!context
->err
&& bdrv_key_required(bs
)) {
1201 context
->err
= -EBUSY
;
1202 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1207 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1209 const char *device
= qdict_get_try_str(qdict
, "device");
1211 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1212 if (gdbserver_start(device
) < 0) {
1213 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1215 } else if (strcmp(device
, "none") == 0) {
1216 monitor_printf(mon
, "Disabled gdbserver\n");
1218 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1223 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1225 const char *action
= qdict_get_str(qdict
, "action");
1226 if (select_watchdog_action(action
) == -1) {
1227 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1231 static void monitor_printc(Monitor
*mon
, int c
)
1233 monitor_printf(mon
, "'");
1236 monitor_printf(mon
, "\\'");
1239 monitor_printf(mon
, "\\\\");
1242 monitor_printf(mon
, "\\n");
1245 monitor_printf(mon
, "\\r");
1248 if (c
>= 32 && c
<= 126) {
1249 monitor_printf(mon
, "%c", c
);
1251 monitor_printf(mon
, "\\x%02x", c
);
1255 monitor_printf(mon
, "'");
1258 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1259 target_phys_addr_t addr
, int is_physical
)
1262 int l
, line_size
, i
, max_digits
, len
;
1266 if (format
== 'i') {
1269 env
= mon_get_cpu();
1275 } else if (wsize
== 4) {
1278 /* as default we use the current CS size */
1281 #ifdef TARGET_X86_64
1282 if ((env
->efer
& MSR_EFER_LMA
) &&
1283 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1287 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1292 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1296 len
= wsize
* count
;
1305 max_digits
= (wsize
* 8 + 2) / 3;
1309 max_digits
= (wsize
* 8) / 4;
1313 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1322 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1324 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1329 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1331 env
= mon_get_cpu();
1332 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1333 monitor_printf(mon
, " Cannot access memory\n");
1342 v
= ldub_raw(buf
+ i
);
1345 v
= lduw_raw(buf
+ i
);
1348 v
= (uint32_t)ldl_raw(buf
+ i
);
1351 v
= ldq_raw(buf
+ i
);
1354 monitor_printf(mon
, " ");
1357 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1360 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1363 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1366 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1369 monitor_printc(mon
, v
);
1374 monitor_printf(mon
, "\n");
1380 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1382 int count
= qdict_get_int(qdict
, "count");
1383 int format
= qdict_get_int(qdict
, "format");
1384 int size
= qdict_get_int(qdict
, "size");
1385 target_long addr
= qdict_get_int(qdict
, "addr");
1387 memory_dump(mon
, count
, format
, size
, addr
, 0);
1390 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1392 int count
= qdict_get_int(qdict
, "count");
1393 int format
= qdict_get_int(qdict
, "format");
1394 int size
= qdict_get_int(qdict
, "size");
1395 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1397 memory_dump(mon
, count
, format
, size
, addr
, 1);
1400 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1402 int format
= qdict_get_int(qdict
, "format");
1403 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1405 #if TARGET_PHYS_ADDR_BITS == 32
1408 monitor_printf(mon
, "%#o", val
);
1411 monitor_printf(mon
, "%#x", val
);
1414 monitor_printf(mon
, "%u", val
);
1418 monitor_printf(mon
, "%d", val
);
1421 monitor_printc(mon
, val
);
1427 monitor_printf(mon
, "%#" PRIo64
, val
);
1430 monitor_printf(mon
, "%#" PRIx64
, val
);
1433 monitor_printf(mon
, "%" PRIu64
, val
);
1437 monitor_printf(mon
, "%" PRId64
, val
);
1440 monitor_printc(mon
, val
);
1444 monitor_printf(mon
, "\n");
1447 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1450 uint32_t size
= qdict_get_int(qdict
, "size");
1451 const char *filename
= qdict_get_str(qdict
, "filename");
1452 target_long addr
= qdict_get_int(qdict
, "val");
1458 env
= mon_get_cpu();
1460 f
= fopen(filename
, "wb");
1462 qemu_error_new(QERR_OPEN_FILE_FAILED
, filename
);
1469 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1470 if (fwrite(buf
, 1, l
, f
) != l
) {
1471 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1485 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1491 uint32_t size
= qdict_get_int(qdict
, "size");
1492 const char *filename
= qdict_get_str(qdict
, "filename");
1493 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1496 f
= fopen(filename
, "wb");
1498 qemu_error_new(QERR_OPEN_FILE_FAILED
, filename
);
1505 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1506 if (fwrite(buf
, 1, l
, f
) != l
) {
1507 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1522 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1527 uint32_t start
= qdict_get_int(qdict
, "start");
1528 uint32_t size
= qdict_get_int(qdict
, "size");
1531 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1532 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1533 /* BSD sum algorithm ('sum' Unix command) */
1534 sum
= (sum
>> 1) | (sum
<< 15);
1537 monitor_printf(mon
, "%05d\n", sum
);
1545 static const KeyDef key_defs
[] = {
1547 { 0x36, "shift_r" },
1552 { 0xe4, "altgr_r" },
1572 { 0x0e, "backspace" },
1609 { 0x37, "asterisk" },
1612 { 0x3a, "caps_lock" },
1623 { 0x45, "num_lock" },
1624 { 0x46, "scroll_lock" },
1626 { 0xb5, "kp_divide" },
1627 { 0x37, "kp_multiply" },
1628 { 0x4a, "kp_subtract" },
1630 { 0x9c, "kp_enter" },
1631 { 0x53, "kp_decimal" },
1664 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1679 { 0xfe, "compose" },
1684 static int get_keycode(const char *key
)
1690 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1691 if (!strcmp(key
, p
->name
))
1694 if (strstart(key
, "0x", NULL
)) {
1695 ret
= strtoul(key
, &endp
, 0);
1696 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1702 #define MAX_KEYCODES 16
1703 static uint8_t keycodes
[MAX_KEYCODES
];
1704 static int nb_pending_keycodes
;
1705 static QEMUTimer
*key_timer
;
1707 static void release_keys(void *opaque
)
1711 while (nb_pending_keycodes
> 0) {
1712 nb_pending_keycodes
--;
1713 keycode
= keycodes
[nb_pending_keycodes
];
1715 kbd_put_keycode(0xe0);
1716 kbd_put_keycode(keycode
| 0x80);
1720 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1722 char keyname_buf
[16];
1724 int keyname_len
, keycode
, i
;
1725 const char *string
= qdict_get_str(qdict
, "string");
1726 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1727 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1729 if (nb_pending_keycodes
> 0) {
1730 qemu_del_timer(key_timer
);
1737 separator
= strchr(string
, '-');
1738 keyname_len
= separator
? separator
- string
: strlen(string
);
1739 if (keyname_len
> 0) {
1740 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1741 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1742 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1745 if (i
== MAX_KEYCODES
) {
1746 monitor_printf(mon
, "too many keys\n");
1749 keyname_buf
[keyname_len
] = 0;
1750 keycode
= get_keycode(keyname_buf
);
1752 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1755 keycodes
[i
++] = keycode
;
1759 string
= separator
+ 1;
1761 nb_pending_keycodes
= i
;
1762 /* key down events */
1763 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1764 keycode
= keycodes
[i
];
1766 kbd_put_keycode(0xe0);
1767 kbd_put_keycode(keycode
& 0x7f);
1769 /* delayed key up events */
1770 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1771 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1774 static int mouse_button_state
;
1776 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1779 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1780 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1781 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1782 dx
= strtol(dx_str
, NULL
, 0);
1783 dy
= strtol(dy_str
, NULL
, 0);
1786 dz
= strtol(dz_str
, NULL
, 0);
1787 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1790 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1792 int button_state
= qdict_get_int(qdict
, "button_state");
1793 mouse_button_state
= button_state
;
1794 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1797 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1799 int size
= qdict_get_int(qdict
, "size");
1800 int addr
= qdict_get_int(qdict
, "addr");
1801 int has_index
= qdict_haskey(qdict
, "index");
1806 int index
= qdict_get_int(qdict
, "index");
1807 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1815 val
= cpu_inb(addr
);
1819 val
= cpu_inw(addr
);
1823 val
= cpu_inl(addr
);
1827 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1828 suffix
, addr
, size
* 2, val
);
1831 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1833 int size
= qdict_get_int(qdict
, "size");
1834 int addr
= qdict_get_int(qdict
, "addr");
1835 int val
= qdict_get_int(qdict
, "val");
1837 addr
&= IOPORTS_MASK
;
1842 cpu_outb(addr
, val
);
1845 cpu_outw(addr
, val
);
1848 cpu_outl(addr
, val
);
1853 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1856 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1858 res
= qemu_boot_set(bootdevice
);
1860 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1861 } else if (res
> 0) {
1862 monitor_printf(mon
, "setting boot device list failed\n");
1864 monitor_printf(mon
, "no function defined to set boot device list for "
1865 "this architecture\n");
1870 * do_system_reset(): Issue a machine reset
1872 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1875 qemu_system_reset_request();
1880 * do_system_powerdown(): Issue a machine powerdown
1882 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1885 qemu_system_powerdown_request();
1889 #if defined(TARGET_I386)
1890 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1892 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1895 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1896 pte
& PG_PSE_MASK
? 'P' : '-',
1897 pte
& PG_DIRTY_MASK
? 'D' : '-',
1898 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1899 pte
& PG_PCD_MASK
? 'C' : '-',
1900 pte
& PG_PWT_MASK
? 'T' : '-',
1901 pte
& PG_USER_MASK
? 'U' : '-',
1902 pte
& PG_RW_MASK
? 'W' : '-');
1905 static void tlb_info(Monitor
*mon
)
1909 uint32_t pgd
, pde
, pte
;
1911 env
= mon_get_cpu();
1913 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1914 monitor_printf(mon
, "PG disabled\n");
1917 pgd
= env
->cr
[3] & ~0xfff;
1918 for(l1
= 0; l1
< 1024; l1
++) {
1919 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1920 pde
= le32_to_cpu(pde
);
1921 if (pde
& PG_PRESENT_MASK
) {
1922 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1923 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1925 for(l2
= 0; l2
< 1024; l2
++) {
1926 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1927 (uint8_t *)&pte
, 4);
1928 pte
= le32_to_cpu(pte
);
1929 if (pte
& PG_PRESENT_MASK
) {
1930 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1940 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1941 uint32_t end
, int prot
)
1944 prot1
= *plast_prot
;
1945 if (prot
!= prot1
) {
1946 if (*pstart
!= -1) {
1947 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1948 *pstart
, end
, end
- *pstart
,
1949 prot1
& PG_USER_MASK
? 'u' : '-',
1951 prot1
& PG_RW_MASK
? 'w' : '-');
1961 static void mem_info(Monitor
*mon
)
1964 int l1
, l2
, prot
, last_prot
;
1965 uint32_t pgd
, pde
, pte
, start
, end
;
1967 env
= mon_get_cpu();
1969 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1970 monitor_printf(mon
, "PG disabled\n");
1973 pgd
= env
->cr
[3] & ~0xfff;
1976 for(l1
= 0; l1
< 1024; l1
++) {
1977 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1978 pde
= le32_to_cpu(pde
);
1980 if (pde
& PG_PRESENT_MASK
) {
1981 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1982 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1983 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1985 for(l2
= 0; l2
< 1024; l2
++) {
1986 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1987 (uint8_t *)&pte
, 4);
1988 pte
= le32_to_cpu(pte
);
1989 end
= (l1
<< 22) + (l2
<< 12);
1990 if (pte
& PG_PRESENT_MASK
) {
1991 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1995 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2000 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2006 #if defined(TARGET_SH4)
2008 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2010 monitor_printf(mon
, " tlb%i:\t"
2011 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2012 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2013 "dirty=%hhu writethrough=%hhu\n",
2015 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2016 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2020 static void tlb_info(Monitor
*mon
)
2022 CPUState
*env
= mon_get_cpu();
2025 monitor_printf (mon
, "ITLB:\n");
2026 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2027 print_tlb (mon
, i
, &env
->itlb
[i
]);
2028 monitor_printf (mon
, "UTLB:\n");
2029 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2030 print_tlb (mon
, i
, &env
->utlb
[i
]);
2035 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
2039 qdict
= qobject_to_qdict(data
);
2041 monitor_printf(mon
, "kvm support: ");
2042 if (qdict_get_bool(qdict
, "present")) {
2043 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
2044 "enabled" : "disabled");
2046 monitor_printf(mon
, "not compiled\n");
2051 * do_info_kvm(): Show KVM information
2053 * Return a QDict with the following information:
2055 * - "enabled": true if KVM support is enabled, false otherwise
2056 * - "present": true if QEMU has KVM support, false otherwise
2060 * { "enabled": true, "present": true }
2062 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
2065 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2068 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2072 static void do_info_numa(Monitor
*mon
)
2077 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2078 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2079 monitor_printf(mon
, "node %d cpus:", i
);
2080 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2081 if (env
->numa_node
== i
) {
2082 monitor_printf(mon
, " %d", env
->cpu_index
);
2085 monitor_printf(mon
, "\n");
2086 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2091 #ifdef CONFIG_PROFILER
2096 static void do_info_profile(Monitor
*mon
)
2102 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2103 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2104 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2105 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2110 static void do_info_profile(Monitor
*mon
)
2112 monitor_printf(mon
, "Internal profiler not compiled\n");
2116 /* Capture support */
2117 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2119 static void do_info_capture(Monitor
*mon
)
2124 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2125 monitor_printf(mon
, "[%d]: ", i
);
2126 s
->ops
.info (s
->opaque
);
2131 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2134 int n
= qdict_get_int(qdict
, "n");
2137 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2139 s
->ops
.destroy (s
->opaque
);
2140 QLIST_REMOVE (s
, entries
);
2147 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2149 const char *path
= qdict_get_str(qdict
, "path");
2150 int has_freq
= qdict_haskey(qdict
, "freq");
2151 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2152 int has_bits
= qdict_haskey(qdict
, "bits");
2153 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2154 int has_channels
= qdict_haskey(qdict
, "nchannels");
2155 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2158 s
= qemu_mallocz (sizeof (*s
));
2160 freq
= has_freq
? freq
: 44100;
2161 bits
= has_bits
? bits
: 16;
2162 nchannels
= has_channels
? nchannels
: 2;
2164 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2165 monitor_printf(mon
, "Faied to add wave capture\n");
2168 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2172 #if defined(TARGET_I386)
2173 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
2176 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2178 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2179 if (env
->cpu_index
== cpu_index
) {
2180 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2186 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2190 qdict
= qobject_to_qdict(data
);
2192 monitor_printf(mon
, "VM status: ");
2193 if (qdict_get_bool(qdict
, "running")) {
2194 monitor_printf(mon
, "running");
2195 if (qdict_get_bool(qdict
, "singlestep")) {
2196 monitor_printf(mon
, " (single step mode)");
2199 monitor_printf(mon
, "paused");
2202 monitor_printf(mon
, "\n");
2206 * do_info_status(): VM status
2208 * Return a QDict with the following information:
2210 * - "running": true if the VM is running, or false if it is paused
2211 * - "singlestep": true if the VM is in single step mode, false otherwise
2215 * { "running": true, "singlestep": false }
2217 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2219 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2220 vm_running
, singlestep
);
2223 static void print_balloon_stat(const char *key
, QObject
*obj
, void *opaque
)
2225 Monitor
*mon
= opaque
;
2227 if (strcmp(key
, "actual"))
2228 monitor_printf(mon
, ",%s=%" PRId64
, key
,
2229 qint_get_int(qobject_to_qint(obj
)));
2232 static void monitor_print_balloon(Monitor
*mon
, const QObject
*data
)
2236 qdict
= qobject_to_qdict(data
);
2237 if (!qdict_haskey(qdict
, "actual"))
2240 monitor_printf(mon
, "balloon: actual=%" PRId64
,
2241 qdict_get_int(qdict
, "actual") >> 20);
2242 qdict_iter(qdict
, print_balloon_stat
, mon
);
2243 monitor_printf(mon
, "\n");
2247 * do_info_balloon(): Balloon information
2249 * Make an asynchronous request for balloon info. When the request completes
2250 * a QDict will be returned according to the following specification:
2252 * - "actual": current balloon value in bytes
2253 * The following fields may or may not be present:
2254 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2255 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2256 * - "major_page_faults": Number of major faults
2257 * - "minor_page_faults": Number of minor faults
2258 * - "free_mem": Total amount of free and unused memory (bytes)
2259 * - "total_mem": Total amount of available memory (bytes)
2263 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2264 * "major_page_faults": 142, "minor_page_faults": 239245,
2265 * "free_mem": 1014185984, "total_mem": 1044668416 }
2267 static int do_info_balloon(Monitor
*mon
, MonitorCompletion cb
, void *opaque
)
2271 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2272 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
2276 ret
= qemu_balloon_status(cb
, opaque
);
2278 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
2286 * do_balloon(): Request VM to change its memory allocation
2288 static int do_balloon(Monitor
*mon
, const QDict
*params
,
2289 MonitorCompletion cb
, void *opaque
)
2293 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2294 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
2298 ret
= qemu_balloon(qdict_get_int(params
, "value"), cb
, opaque
);
2300 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
2307 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2309 qemu_acl
*acl
= qemu_acl_find(name
);
2312 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2317 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2319 const char *aclname
= qdict_get_str(qdict
, "aclname");
2320 qemu_acl
*acl
= find_acl(mon
, aclname
);
2321 qemu_acl_entry
*entry
;
2325 monitor_printf(mon
, "policy: %s\n",
2326 acl
->defaultDeny
? "deny" : "allow");
2327 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2329 monitor_printf(mon
, "%d: %s %s\n", i
,
2330 entry
->deny
? "deny" : "allow", entry
->match
);
2335 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2337 const char *aclname
= qdict_get_str(qdict
, "aclname");
2338 qemu_acl
*acl
= find_acl(mon
, aclname
);
2341 qemu_acl_reset(acl
);
2342 monitor_printf(mon
, "acl: removed all rules\n");
2346 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2348 const char *aclname
= qdict_get_str(qdict
, "aclname");
2349 const char *policy
= qdict_get_str(qdict
, "policy");
2350 qemu_acl
*acl
= find_acl(mon
, aclname
);
2353 if (strcmp(policy
, "allow") == 0) {
2354 acl
->defaultDeny
= 0;
2355 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2356 } else if (strcmp(policy
, "deny") == 0) {
2357 acl
->defaultDeny
= 1;
2358 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2360 monitor_printf(mon
, "acl: unknown policy '%s', "
2361 "expected 'deny' or 'allow'\n", policy
);
2366 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2368 const char *aclname
= qdict_get_str(qdict
, "aclname");
2369 const char *match
= qdict_get_str(qdict
, "match");
2370 const char *policy
= qdict_get_str(qdict
, "policy");
2371 int has_index
= qdict_haskey(qdict
, "index");
2372 int index
= qdict_get_try_int(qdict
, "index", -1);
2373 qemu_acl
*acl
= find_acl(mon
, aclname
);
2377 if (strcmp(policy
, "allow") == 0) {
2379 } else if (strcmp(policy
, "deny") == 0) {
2382 monitor_printf(mon
, "acl: unknown policy '%s', "
2383 "expected 'deny' or 'allow'\n", policy
);
2387 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2389 ret
= qemu_acl_append(acl
, deny
, match
);
2391 monitor_printf(mon
, "acl: unable to add acl entry\n");
2393 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2397 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2399 const char *aclname
= qdict_get_str(qdict
, "aclname");
2400 const char *match
= qdict_get_str(qdict
, "match");
2401 qemu_acl
*acl
= find_acl(mon
, aclname
);
2405 ret
= qemu_acl_remove(acl
, match
);
2407 monitor_printf(mon
, "acl: no matching acl entry\n");
2409 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2413 #if defined(TARGET_I386)
2414 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2417 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2418 int bank
= qdict_get_int(qdict
, "bank");
2419 uint64_t status
= qdict_get_int(qdict
, "status");
2420 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2421 uint64_t addr
= qdict_get_int(qdict
, "addr");
2422 uint64_t misc
= qdict_get_int(qdict
, "misc");
2424 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
2425 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
2426 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
2432 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2434 const char *fdname
= qdict_get_str(qdict
, "fdname");
2438 fd
= qemu_chr_get_msgfd(mon
->chr
);
2440 qemu_error_new(QERR_FD_NOT_SUPPLIED
);
2444 if (qemu_isdigit(fdname
[0])) {
2445 qemu_error_new(QERR_INVALID_PARAMETER
, "fdname");
2451 if (errno
== EMFILE
)
2452 qemu_error_new(QERR_TOO_MANY_FILES
);
2454 qemu_error_new(QERR_UNDEFINED_ERROR
);
2458 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2459 if (strcmp(monfd
->name
, fdname
) != 0) {
2468 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
2469 monfd
->name
= qemu_strdup(fdname
);
2472 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2476 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2478 const char *fdname
= qdict_get_str(qdict
, "fdname");
2481 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2482 if (strcmp(monfd
->name
, fdname
) != 0) {
2486 QLIST_REMOVE(monfd
, next
);
2488 qemu_free(monfd
->name
);
2493 qemu_error_new(QERR_FD_NOT_FOUND
, fdname
);
2497 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2499 int saved_vm_running
= vm_running
;
2500 const char *name
= qdict_get_str(qdict
, "name");
2504 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
2508 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2512 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2515 if (strcmp(monfd
->name
, fdname
) != 0) {
2521 /* caller takes ownership of fd */
2522 QLIST_REMOVE(monfd
, next
);
2523 qemu_free(monfd
->name
);
2532 static const mon_cmd_t mon_cmds
[] = {
2533 #include "qemu-monitor.h"
2537 /* Please update qemu-monitor.hx when adding or changing commands */
2538 static const mon_cmd_t info_cmds
[] = {
2543 .help
= "show the version of QEMU",
2544 .user_print
= do_info_version_print
,
2545 .mhandler
.info_new
= do_info_version
,
2551 .help
= "list QMP available commands",
2552 .user_print
= monitor_user_noop
,
2553 .mhandler
.info_new
= do_info_commands
,
2559 .help
= "show the network state",
2560 .mhandler
.info
= do_info_network
,
2566 .help
= "show the character devices",
2567 .user_print
= qemu_chr_info_print
,
2568 .mhandler
.info_new
= qemu_chr_info
,
2574 .help
= "show the block devices",
2575 .user_print
= bdrv_info_print
,
2576 .mhandler
.info_new
= bdrv_info
,
2579 .name
= "blockstats",
2582 .help
= "show block device statistics",
2583 .user_print
= bdrv_stats_print
,
2584 .mhandler
.info_new
= bdrv_info_stats
,
2587 .name
= "registers",
2590 .help
= "show the cpu registers",
2591 .mhandler
.info
= do_info_registers
,
2597 .help
= "show infos for each CPU",
2598 .user_print
= monitor_print_cpus
,
2599 .mhandler
.info_new
= do_info_cpus
,
2605 .help
= "show the command line history",
2606 .mhandler
.info
= do_info_history
,
2612 .help
= "show the interrupts statistics (if available)",
2613 .mhandler
.info
= irq_info
,
2619 .help
= "show i8259 (PIC) state",
2620 .mhandler
.info
= pic_info
,
2626 .help
= "show PCI info",
2627 .user_print
= do_pci_info_print
,
2628 .mhandler
.info_new
= do_pci_info
,
2630 #if defined(TARGET_I386) || defined(TARGET_SH4)
2635 .help
= "show virtual to physical memory mappings",
2636 .mhandler
.info
= tlb_info
,
2639 #if defined(TARGET_I386)
2644 .help
= "show the active virtual memory mappings",
2645 .mhandler
.info
= mem_info
,
2651 .help
= "show state of HPET",
2652 .user_print
= do_info_hpet_print
,
2653 .mhandler
.info_new
= do_info_hpet
,
2660 .help
= "show dynamic compiler info",
2661 .mhandler
.info
= do_info_jit
,
2667 .help
= "show KVM information",
2668 .user_print
= do_info_kvm_print
,
2669 .mhandler
.info_new
= do_info_kvm
,
2675 .help
= "show NUMA information",
2676 .mhandler
.info
= do_info_numa
,
2682 .help
= "show guest USB devices",
2683 .mhandler
.info
= usb_info
,
2689 .help
= "show host USB devices",
2690 .mhandler
.info
= usb_host_info
,
2696 .help
= "show profiling information",
2697 .mhandler
.info
= do_info_profile
,
2703 .help
= "show capture information",
2704 .mhandler
.info
= do_info_capture
,
2707 .name
= "snapshots",
2710 .help
= "show the currently saved VM snapshots",
2711 .mhandler
.info
= do_info_snapshots
,
2717 .help
= "show the current VM status (running|paused)",
2718 .user_print
= do_info_status_print
,
2719 .mhandler
.info_new
= do_info_status
,
2725 .help
= "show guest PCMCIA status",
2726 .mhandler
.info
= pcmcia_info
,
2732 .help
= "show which guest mouse is receiving events",
2733 .user_print
= do_info_mice_print
,
2734 .mhandler
.info_new
= do_info_mice
,
2740 .help
= "show the vnc server status",
2741 .user_print
= do_info_vnc_print
,
2742 .mhandler
.info_new
= do_info_vnc
,
2748 .help
= "show the current VM name",
2749 .user_print
= do_info_name_print
,
2750 .mhandler
.info_new
= do_info_name
,
2756 .help
= "show the current VM UUID",
2757 .user_print
= do_info_uuid_print
,
2758 .mhandler
.info_new
= do_info_uuid
,
2760 #if defined(TARGET_PPC)
2765 .help
= "show CPU statistics",
2766 .mhandler
.info
= do_info_cpu_stats
,
2769 #if defined(CONFIG_SLIRP)
2774 .help
= "show user network stack connection states",
2775 .mhandler
.info
= do_info_usernet
,
2782 .help
= "show migration status",
2783 .user_print
= do_info_migrate_print
,
2784 .mhandler
.info_new
= do_info_migrate
,
2790 .help
= "show balloon information",
2791 .user_print
= monitor_print_balloon
,
2792 .mhandler
.info_async
= do_info_balloon
,
2799 .help
= "show device tree",
2800 .mhandler
.info
= do_info_qtree
,
2806 .help
= "show qdev device model list",
2807 .mhandler
.info
= do_info_qdm
,
2813 .help
= "show roms",
2814 .mhandler
.info
= do_info_roms
,
2821 /*******************************************************************/
2823 static const char *pch
;
2824 static jmp_buf expr_env
;
2829 typedef struct MonitorDef
{
2832 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2836 #if defined(TARGET_I386)
2837 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2839 CPUState
*env
= mon_get_cpu();
2840 return env
->eip
+ env
->segs
[R_CS
].base
;
2844 #if defined(TARGET_PPC)
2845 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2847 CPUState
*env
= mon_get_cpu();
2852 for (i
= 0; i
< 8; i
++)
2853 u
|= env
->crf
[i
] << (32 - (4 * i
));
2858 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2860 CPUState
*env
= mon_get_cpu();
2864 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2866 CPUState
*env
= mon_get_cpu();
2870 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2872 CPUState
*env
= mon_get_cpu();
2873 return cpu_ppc_load_decr(env
);
2876 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2878 CPUState
*env
= mon_get_cpu();
2879 return cpu_ppc_load_tbu(env
);
2882 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2884 CPUState
*env
= mon_get_cpu();
2885 return cpu_ppc_load_tbl(env
);
2889 #if defined(TARGET_SPARC)
2890 #ifndef TARGET_SPARC64
2891 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2893 CPUState
*env
= mon_get_cpu();
2894 return GET_PSR(env
);
2898 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2900 CPUState
*env
= mon_get_cpu();
2901 return env
->regwptr
[val
];
2905 static const MonitorDef monitor_defs
[] = {
2908 #define SEG(name, seg) \
2909 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2910 { name ".base", offsetof(CPUState, segs[seg].base) },\
2911 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2913 { "eax", offsetof(CPUState
, regs
[0]) },
2914 { "ecx", offsetof(CPUState
, regs
[1]) },
2915 { "edx", offsetof(CPUState
, regs
[2]) },
2916 { "ebx", offsetof(CPUState
, regs
[3]) },
2917 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2918 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2919 { "esi", offsetof(CPUState
, regs
[6]) },
2920 { "edi", offsetof(CPUState
, regs
[7]) },
2921 #ifdef TARGET_X86_64
2922 { "r8", offsetof(CPUState
, regs
[8]) },
2923 { "r9", offsetof(CPUState
, regs
[9]) },
2924 { "r10", offsetof(CPUState
, regs
[10]) },
2925 { "r11", offsetof(CPUState
, regs
[11]) },
2926 { "r12", offsetof(CPUState
, regs
[12]) },
2927 { "r13", offsetof(CPUState
, regs
[13]) },
2928 { "r14", offsetof(CPUState
, regs
[14]) },
2929 { "r15", offsetof(CPUState
, regs
[15]) },
2931 { "eflags", offsetof(CPUState
, eflags
) },
2932 { "eip", offsetof(CPUState
, eip
) },
2939 { "pc", 0, monitor_get_pc
, },
2940 #elif defined(TARGET_PPC)
2941 /* General purpose registers */
2942 { "r0", offsetof(CPUState
, gpr
[0]) },
2943 { "r1", offsetof(CPUState
, gpr
[1]) },
2944 { "r2", offsetof(CPUState
, gpr
[2]) },
2945 { "r3", offsetof(CPUState
, gpr
[3]) },
2946 { "r4", offsetof(CPUState
, gpr
[4]) },
2947 { "r5", offsetof(CPUState
, gpr
[5]) },
2948 { "r6", offsetof(CPUState
, gpr
[6]) },
2949 { "r7", offsetof(CPUState
, gpr
[7]) },
2950 { "r8", offsetof(CPUState
, gpr
[8]) },
2951 { "r9", offsetof(CPUState
, gpr
[9]) },
2952 { "r10", offsetof(CPUState
, gpr
[10]) },
2953 { "r11", offsetof(CPUState
, gpr
[11]) },
2954 { "r12", offsetof(CPUState
, gpr
[12]) },
2955 { "r13", offsetof(CPUState
, gpr
[13]) },
2956 { "r14", offsetof(CPUState
, gpr
[14]) },
2957 { "r15", offsetof(CPUState
, gpr
[15]) },
2958 { "r16", offsetof(CPUState
, gpr
[16]) },
2959 { "r17", offsetof(CPUState
, gpr
[17]) },
2960 { "r18", offsetof(CPUState
, gpr
[18]) },
2961 { "r19", offsetof(CPUState
, gpr
[19]) },
2962 { "r20", offsetof(CPUState
, gpr
[20]) },
2963 { "r21", offsetof(CPUState
, gpr
[21]) },
2964 { "r22", offsetof(CPUState
, gpr
[22]) },
2965 { "r23", offsetof(CPUState
, gpr
[23]) },
2966 { "r24", offsetof(CPUState
, gpr
[24]) },
2967 { "r25", offsetof(CPUState
, gpr
[25]) },
2968 { "r26", offsetof(CPUState
, gpr
[26]) },
2969 { "r27", offsetof(CPUState
, gpr
[27]) },
2970 { "r28", offsetof(CPUState
, gpr
[28]) },
2971 { "r29", offsetof(CPUState
, gpr
[29]) },
2972 { "r30", offsetof(CPUState
, gpr
[30]) },
2973 { "r31", offsetof(CPUState
, gpr
[31]) },
2974 /* Floating point registers */
2975 { "f0", offsetof(CPUState
, fpr
[0]) },
2976 { "f1", offsetof(CPUState
, fpr
[1]) },
2977 { "f2", offsetof(CPUState
, fpr
[2]) },
2978 { "f3", offsetof(CPUState
, fpr
[3]) },
2979 { "f4", offsetof(CPUState
, fpr
[4]) },
2980 { "f5", offsetof(CPUState
, fpr
[5]) },
2981 { "f6", offsetof(CPUState
, fpr
[6]) },
2982 { "f7", offsetof(CPUState
, fpr
[7]) },
2983 { "f8", offsetof(CPUState
, fpr
[8]) },
2984 { "f9", offsetof(CPUState
, fpr
[9]) },
2985 { "f10", offsetof(CPUState
, fpr
[10]) },
2986 { "f11", offsetof(CPUState
, fpr
[11]) },
2987 { "f12", offsetof(CPUState
, fpr
[12]) },
2988 { "f13", offsetof(CPUState
, fpr
[13]) },
2989 { "f14", offsetof(CPUState
, fpr
[14]) },
2990 { "f15", offsetof(CPUState
, fpr
[15]) },
2991 { "f16", offsetof(CPUState
, fpr
[16]) },
2992 { "f17", offsetof(CPUState
, fpr
[17]) },
2993 { "f18", offsetof(CPUState
, fpr
[18]) },
2994 { "f19", offsetof(CPUState
, fpr
[19]) },
2995 { "f20", offsetof(CPUState
, fpr
[20]) },
2996 { "f21", offsetof(CPUState
, fpr
[21]) },
2997 { "f22", offsetof(CPUState
, fpr
[22]) },
2998 { "f23", offsetof(CPUState
, fpr
[23]) },
2999 { "f24", offsetof(CPUState
, fpr
[24]) },
3000 { "f25", offsetof(CPUState
, fpr
[25]) },
3001 { "f26", offsetof(CPUState
, fpr
[26]) },
3002 { "f27", offsetof(CPUState
, fpr
[27]) },
3003 { "f28", offsetof(CPUState
, fpr
[28]) },
3004 { "f29", offsetof(CPUState
, fpr
[29]) },
3005 { "f30", offsetof(CPUState
, fpr
[30]) },
3006 { "f31", offsetof(CPUState
, fpr
[31]) },
3007 { "fpscr", offsetof(CPUState
, fpscr
) },
3008 /* Next instruction pointer */
3009 { "nip|pc", offsetof(CPUState
, nip
) },
3010 { "lr", offsetof(CPUState
, lr
) },
3011 { "ctr", offsetof(CPUState
, ctr
) },
3012 { "decr", 0, &monitor_get_decr
, },
3013 { "ccr", 0, &monitor_get_ccr
, },
3014 /* Machine state register */
3015 { "msr", 0, &monitor_get_msr
, },
3016 { "xer", 0, &monitor_get_xer
, },
3017 { "tbu", 0, &monitor_get_tbu
, },
3018 { "tbl", 0, &monitor_get_tbl
, },
3019 #if defined(TARGET_PPC64)
3020 /* Address space register */
3021 { "asr", offsetof(CPUState
, asr
) },
3023 /* Segment registers */
3024 { "sdr1", offsetof(CPUState
, sdr1
) },
3025 { "sr0", offsetof(CPUState
, sr
[0]) },
3026 { "sr1", offsetof(CPUState
, sr
[1]) },
3027 { "sr2", offsetof(CPUState
, sr
[2]) },
3028 { "sr3", offsetof(CPUState
, sr
[3]) },
3029 { "sr4", offsetof(CPUState
, sr
[4]) },
3030 { "sr5", offsetof(CPUState
, sr
[5]) },
3031 { "sr6", offsetof(CPUState
, sr
[6]) },
3032 { "sr7", offsetof(CPUState
, sr
[7]) },
3033 { "sr8", offsetof(CPUState
, sr
[8]) },
3034 { "sr9", offsetof(CPUState
, sr
[9]) },
3035 { "sr10", offsetof(CPUState
, sr
[10]) },
3036 { "sr11", offsetof(CPUState
, sr
[11]) },
3037 { "sr12", offsetof(CPUState
, sr
[12]) },
3038 { "sr13", offsetof(CPUState
, sr
[13]) },
3039 { "sr14", offsetof(CPUState
, sr
[14]) },
3040 { "sr15", offsetof(CPUState
, sr
[15]) },
3041 /* Too lazy to put BATs and SPRs ... */
3042 #elif defined(TARGET_SPARC)
3043 { "g0", offsetof(CPUState
, gregs
[0]) },
3044 { "g1", offsetof(CPUState
, gregs
[1]) },
3045 { "g2", offsetof(CPUState
, gregs
[2]) },
3046 { "g3", offsetof(CPUState
, gregs
[3]) },
3047 { "g4", offsetof(CPUState
, gregs
[4]) },
3048 { "g5", offsetof(CPUState
, gregs
[5]) },
3049 { "g6", offsetof(CPUState
, gregs
[6]) },
3050 { "g7", offsetof(CPUState
, gregs
[7]) },
3051 { "o0", 0, monitor_get_reg
},
3052 { "o1", 1, monitor_get_reg
},
3053 { "o2", 2, monitor_get_reg
},
3054 { "o3", 3, monitor_get_reg
},
3055 { "o4", 4, monitor_get_reg
},
3056 { "o5", 5, monitor_get_reg
},
3057 { "o6", 6, monitor_get_reg
},
3058 { "o7", 7, monitor_get_reg
},
3059 { "l0", 8, monitor_get_reg
},
3060 { "l1", 9, monitor_get_reg
},
3061 { "l2", 10, monitor_get_reg
},
3062 { "l3", 11, monitor_get_reg
},
3063 { "l4", 12, monitor_get_reg
},
3064 { "l5", 13, monitor_get_reg
},
3065 { "l6", 14, monitor_get_reg
},
3066 { "l7", 15, monitor_get_reg
},
3067 { "i0", 16, monitor_get_reg
},
3068 { "i1", 17, monitor_get_reg
},
3069 { "i2", 18, monitor_get_reg
},
3070 { "i3", 19, monitor_get_reg
},
3071 { "i4", 20, monitor_get_reg
},
3072 { "i5", 21, monitor_get_reg
},
3073 { "i6", 22, monitor_get_reg
},
3074 { "i7", 23, monitor_get_reg
},
3075 { "pc", offsetof(CPUState
, pc
) },
3076 { "npc", offsetof(CPUState
, npc
) },
3077 { "y", offsetof(CPUState
, y
) },
3078 #ifndef TARGET_SPARC64
3079 { "psr", 0, &monitor_get_psr
, },
3080 { "wim", offsetof(CPUState
, wim
) },
3082 { "tbr", offsetof(CPUState
, tbr
) },
3083 { "fsr", offsetof(CPUState
, fsr
) },
3084 { "f0", offsetof(CPUState
, fpr
[0]) },
3085 { "f1", offsetof(CPUState
, fpr
[1]) },
3086 { "f2", offsetof(CPUState
, fpr
[2]) },
3087 { "f3", offsetof(CPUState
, fpr
[3]) },
3088 { "f4", offsetof(CPUState
, fpr
[4]) },
3089 { "f5", offsetof(CPUState
, fpr
[5]) },
3090 { "f6", offsetof(CPUState
, fpr
[6]) },
3091 { "f7", offsetof(CPUState
, fpr
[7]) },
3092 { "f8", offsetof(CPUState
, fpr
[8]) },
3093 { "f9", offsetof(CPUState
, fpr
[9]) },
3094 { "f10", offsetof(CPUState
, fpr
[10]) },
3095 { "f11", offsetof(CPUState
, fpr
[11]) },
3096 { "f12", offsetof(CPUState
, fpr
[12]) },
3097 { "f13", offsetof(CPUState
, fpr
[13]) },
3098 { "f14", offsetof(CPUState
, fpr
[14]) },
3099 { "f15", offsetof(CPUState
, fpr
[15]) },
3100 { "f16", offsetof(CPUState
, fpr
[16]) },
3101 { "f17", offsetof(CPUState
, fpr
[17]) },
3102 { "f18", offsetof(CPUState
, fpr
[18]) },
3103 { "f19", offsetof(CPUState
, fpr
[19]) },
3104 { "f20", offsetof(CPUState
, fpr
[20]) },
3105 { "f21", offsetof(CPUState
, fpr
[21]) },
3106 { "f22", offsetof(CPUState
, fpr
[22]) },
3107 { "f23", offsetof(CPUState
, fpr
[23]) },
3108 { "f24", offsetof(CPUState
, fpr
[24]) },
3109 { "f25", offsetof(CPUState
, fpr
[25]) },
3110 { "f26", offsetof(CPUState
, fpr
[26]) },
3111 { "f27", offsetof(CPUState
, fpr
[27]) },
3112 { "f28", offsetof(CPUState
, fpr
[28]) },
3113 { "f29", offsetof(CPUState
, fpr
[29]) },
3114 { "f30", offsetof(CPUState
, fpr
[30]) },
3115 { "f31", offsetof(CPUState
, fpr
[31]) },
3116 #ifdef TARGET_SPARC64
3117 { "f32", offsetof(CPUState
, fpr
[32]) },
3118 { "f34", offsetof(CPUState
, fpr
[34]) },
3119 { "f36", offsetof(CPUState
, fpr
[36]) },
3120 { "f38", offsetof(CPUState
, fpr
[38]) },
3121 { "f40", offsetof(CPUState
, fpr
[40]) },
3122 { "f42", offsetof(CPUState
, fpr
[42]) },
3123 { "f44", offsetof(CPUState
, fpr
[44]) },
3124 { "f46", offsetof(CPUState
, fpr
[46]) },
3125 { "f48", offsetof(CPUState
, fpr
[48]) },
3126 { "f50", offsetof(CPUState
, fpr
[50]) },
3127 { "f52", offsetof(CPUState
, fpr
[52]) },
3128 { "f54", offsetof(CPUState
, fpr
[54]) },
3129 { "f56", offsetof(CPUState
, fpr
[56]) },
3130 { "f58", offsetof(CPUState
, fpr
[58]) },
3131 { "f60", offsetof(CPUState
, fpr
[60]) },
3132 { "f62", offsetof(CPUState
, fpr
[62]) },
3133 { "asi", offsetof(CPUState
, asi
) },
3134 { "pstate", offsetof(CPUState
, pstate
) },
3135 { "cansave", offsetof(CPUState
, cansave
) },
3136 { "canrestore", offsetof(CPUState
, canrestore
) },
3137 { "otherwin", offsetof(CPUState
, otherwin
) },
3138 { "wstate", offsetof(CPUState
, wstate
) },
3139 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3140 { "fprs", offsetof(CPUState
, fprs
) },
3146 static void expr_error(Monitor
*mon
, const char *msg
)
3148 monitor_printf(mon
, "%s\n", msg
);
3149 longjmp(expr_env
, 1);
3152 /* return 0 if OK, -1 if not found */
3153 static int get_monitor_def(target_long
*pval
, const char *name
)
3155 const MonitorDef
*md
;
3158 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3159 if (compare_cmd(name
, md
->name
)) {
3160 if (md
->get_value
) {
3161 *pval
= md
->get_value(md
, md
->offset
);
3163 CPUState
*env
= mon_get_cpu();
3164 ptr
= (uint8_t *)env
+ md
->offset
;
3167 *pval
= *(int32_t *)ptr
;
3170 *pval
= *(target_long
*)ptr
;
3183 static void next(void)
3187 while (qemu_isspace(*pch
))
3192 static int64_t expr_sum(Monitor
*mon
);
3194 static int64_t expr_unary(Monitor
*mon
)
3203 n
= expr_unary(mon
);
3207 n
= -expr_unary(mon
);
3211 n
= ~expr_unary(mon
);
3217 expr_error(mon
, "')' expected");
3224 expr_error(mon
, "character constant expected");
3228 expr_error(mon
, "missing terminating \' character");
3238 while ((*pch
>= 'a' && *pch
<= 'z') ||
3239 (*pch
>= 'A' && *pch
<= 'Z') ||
3240 (*pch
>= '0' && *pch
<= '9') ||
3241 *pch
== '_' || *pch
== '.') {
3242 if ((q
- buf
) < sizeof(buf
) - 1)
3246 while (qemu_isspace(*pch
))
3249 ret
= get_monitor_def(®
, buf
);
3251 expr_error(mon
, "unknown register");
3256 expr_error(mon
, "unexpected end of expression");
3260 #if TARGET_PHYS_ADDR_BITS > 32
3261 n
= strtoull(pch
, &p
, 0);
3263 n
= strtoul(pch
, &p
, 0);
3266 expr_error(mon
, "invalid char in expression");
3269 while (qemu_isspace(*pch
))
3277 static int64_t expr_prod(Monitor
*mon
)
3282 val
= expr_unary(mon
);
3285 if (op
!= '*' && op
!= '/' && op
!= '%')
3288 val2
= expr_unary(mon
);
3297 expr_error(mon
, "division by zero");
3308 static int64_t expr_logic(Monitor
*mon
)
3313 val
= expr_prod(mon
);
3316 if (op
!= '&' && op
!= '|' && op
!= '^')
3319 val2
= expr_prod(mon
);
3336 static int64_t expr_sum(Monitor
*mon
)
3341 val
= expr_logic(mon
);
3344 if (op
!= '+' && op
!= '-')
3347 val2
= expr_logic(mon
);
3356 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3359 if (setjmp(expr_env
)) {
3363 while (qemu_isspace(*pch
))
3365 *pval
= expr_sum(mon
);
3370 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3372 const char *p
= *pp
;
3376 d
= strtod(p
, &tailp
);
3378 monitor_printf(mon
, "Number expected\n");
3381 if (d
!= d
|| d
- d
!= 0) {
3382 /* NaN or infinity */
3383 monitor_printf(mon
, "Bad number\n");
3391 static int get_str(char *buf
, int buf_size
, const char **pp
)
3399 while (qemu_isspace(*p
))
3409 while (*p
!= '\0' && *p
!= '\"') {
3425 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3428 if ((q
- buf
) < buf_size
- 1) {
3432 if ((q
- buf
) < buf_size
- 1) {
3439 qemu_printf("unterminated string\n");
3444 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3445 if ((q
- buf
) < buf_size
- 1) {
3457 * Store the command-name in cmdname, and return a pointer to
3458 * the remaining of the command string.
3460 static const char *get_command_name(const char *cmdline
,
3461 char *cmdname
, size_t nlen
)
3464 const char *p
, *pstart
;
3467 while (qemu_isspace(*p
))
3472 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3477 memcpy(cmdname
, pstart
, len
);
3478 cmdname
[len
] = '\0';
3483 * Read key of 'type' into 'key' and return the current
3486 static char *key_get_info(const char *type
, char **key
)
3494 p
= strchr(type
, ':');
3501 str
= qemu_malloc(len
+ 1);
3502 memcpy(str
, type
, len
);
3509 static int default_fmt_format
= 'x';
3510 static int default_fmt_size
= 4;
3514 static int is_valid_option(const char *c
, const char *typestr
)
3522 typestr
= strstr(typestr
, option
);
3523 return (typestr
!= NULL
);
3526 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3528 const mon_cmd_t
*cmd
;
3530 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3531 if (compare_cmd(cmdname
, cmd
->name
)) {
3539 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3540 const char *cmdline
,
3543 const char *p
, *typestr
;
3545 const mon_cmd_t
*cmd
;
3551 monitor_printf(mon
, "command='%s'\n", cmdline
);
3554 /* extract the command name */
3555 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3559 cmd
= monitor_find_command(cmdname
);
3561 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3565 /* parse the parameters */
3566 typestr
= cmd
->args_type
;
3568 typestr
= key_get_info(typestr
, &key
);
3580 while (qemu_isspace(*p
))
3582 if (*typestr
== '?') {
3585 /* no optional string: NULL argument */
3589 ret
= get_str(buf
, sizeof(buf
), &p
);
3593 monitor_printf(mon
, "%s: filename expected\n",
3597 monitor_printf(mon
, "%s: block device name expected\n",
3601 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3606 qdict_put(qdict
, key
, qstring_from_str(buf
));
3611 int count
, format
, size
;
3613 while (qemu_isspace(*p
))
3619 if (qemu_isdigit(*p
)) {
3621 while (qemu_isdigit(*p
)) {
3622 count
= count
* 10 + (*p
- '0');
3660 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3661 monitor_printf(mon
, "invalid char in format: '%c'\n",
3666 format
= default_fmt_format
;
3667 if (format
!= 'i') {
3668 /* for 'i', not specifying a size gives -1 as size */
3670 size
= default_fmt_size
;
3671 default_fmt_size
= size
;
3673 default_fmt_format
= format
;
3676 format
= default_fmt_format
;
3677 if (format
!= 'i') {
3678 size
= default_fmt_size
;
3683 qdict_put(qdict
, "count", qint_from_int(count
));
3684 qdict_put(qdict
, "format", qint_from_int(format
));
3685 qdict_put(qdict
, "size", qint_from_int(size
));
3694 while (qemu_isspace(*p
))
3696 if (*typestr
== '?' || *typestr
== '.') {
3697 if (*typestr
== '?') {
3705 while (qemu_isspace(*p
))
3714 if (get_expr(mon
, &val
, &p
))
3716 /* Check if 'i' is greater than 32-bit */
3717 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3718 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3719 monitor_printf(mon
, "integer is for 32-bit values\n");
3721 } else if (c
== 'M') {
3724 qdict_put(qdict
, key
, qint_from_int(val
));
3732 while (qemu_isspace(*p
))
3734 if (*typestr
== '?') {
3740 if (get_double(mon
, &val
, &p
) < 0) {
3743 if (c
== 'b' && *p
) {
3746 val
*= 1 << 10; p
++; break;
3748 val
*= 1 << 20; p
++; break;
3750 val
*= 1 << 30; p
++; break;
3753 if (c
== 'T' && p
[0] && p
[1] == 's') {
3756 val
/= 1e3
; p
+= 2; break;
3758 val
/= 1e6
; p
+= 2; break;
3760 val
/= 1e9
; p
+= 2; break;
3763 if (*p
&& !qemu_isspace(*p
)) {
3764 monitor_printf(mon
, "Unknown unit suffix\n");
3767 qdict_put(qdict
, key
, qfloat_from_double(val
));
3772 const char *tmp
= p
;
3773 int has_option
, skip_key
= 0;
3779 while (qemu_isspace(*p
))
3785 if(!is_valid_option(p
, typestr
)) {
3787 monitor_printf(mon
, "%s: unsupported option -%c\n",
3801 qdict_put(qdict
, key
, qint_from_int(has_option
));
3806 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3812 /* check that all arguments were parsed */
3813 while (qemu_isspace(*p
))
3816 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3828 static void monitor_print_error(Monitor
*mon
)
3830 qerror_print(mon
->error
);
3831 QDECREF(mon
->error
);
3835 static int is_async_return(const QObject
*data
)
3837 if (data
&& qobject_type(data
) == QTYPE_QDICT
) {
3838 return qdict_haskey(qobject_to_qdict(data
), "__mon_async");
3844 static void monitor_call_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
3845 const QDict
*params
)
3847 QObject
*data
= NULL
;
3849 if (cmd
->cmd_new_ret
) {
3850 cmd
->cmd_new_ret(mon
, params
, &data
);
3852 cmd
->mhandler
.cmd_new(mon
, params
, &data
);
3855 if (is_async_return(data
)) {
3857 * Asynchronous commands have no initial return data but they can
3858 * generate errors. Data is returned via the async completion handler.
3860 if (monitor_ctrl_mode(mon
) && monitor_has_error(mon
)) {
3861 monitor_protocol_emitter(mon
, NULL
);
3863 } else if (monitor_ctrl_mode(mon
)) {
3864 /* Monitor Protocol */
3865 monitor_protocol_emitter(mon
, data
);
3869 cmd
->user_print(mon
, data
);
3872 qobject_decref(data
);
3875 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
3878 const mon_cmd_t
*cmd
;
3880 qdict
= qdict_new();
3882 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3886 qemu_errors_to_mon(mon
);
3888 if (monitor_handler_is_async(cmd
)) {
3889 user_async_cmd_handler(mon
, cmd
, qdict
);
3890 } else if (monitor_handler_ported(cmd
)) {
3891 monitor_call_handler(mon
, cmd
, qdict
);
3893 cmd
->mhandler
.cmd(mon
, qdict
);
3896 if (monitor_has_error(mon
))
3897 monitor_print_error(mon
);
3899 qemu_errors_to_previous();
3905 static void cmd_completion(const char *name
, const char *list
)
3907 const char *p
, *pstart
;
3916 p
= pstart
+ strlen(pstart
);
3918 if (len
> sizeof(cmd
) - 2)
3919 len
= sizeof(cmd
) - 2;
3920 memcpy(cmd
, pstart
, len
);
3922 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3923 readline_add_completion(cur_mon
->rs
, cmd
);
3931 static void file_completion(const char *input
)
3936 char file
[1024], file_prefix
[1024];
3940 p
= strrchr(input
, '/');
3943 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3944 pstrcpy(path
, sizeof(path
), ".");
3946 input_path_len
= p
- input
+ 1;
3947 memcpy(path
, input
, input_path_len
);
3948 if (input_path_len
> sizeof(path
) - 1)
3949 input_path_len
= sizeof(path
) - 1;
3950 path
[input_path_len
] = '\0';
3951 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3953 #ifdef DEBUG_COMPLETION
3954 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3955 input
, path
, file_prefix
);
3957 ffs
= opendir(path
);
3965 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3966 memcpy(file
, input
, input_path_len
);
3967 if (input_path_len
< sizeof(file
))
3968 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3970 /* stat the file to find out if it's a directory.
3971 * In that case add a slash to speed up typing long paths
3974 if(S_ISDIR(sb
.st_mode
))
3975 pstrcat(file
, sizeof(file
), "/");
3976 readline_add_completion(cur_mon
->rs
, file
);
3982 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3984 const char *name
= bdrv_get_device_name(bs
);
3985 const char *input
= opaque
;
3987 if (input
[0] == '\0' ||
3988 !strncmp(name
, (char *)input
, strlen(input
))) {
3989 readline_add_completion(cur_mon
->rs
, name
);
3993 /* NOTE: this parser is an approximate form of the real command parser */
3994 static void parse_cmdline(const char *cmdline
,
3995 int *pnb_args
, char **args
)
4004 while (qemu_isspace(*p
))
4008 if (nb_args
>= MAX_ARGS
)
4010 ret
= get_str(buf
, sizeof(buf
), &p
);
4011 args
[nb_args
] = qemu_strdup(buf
);
4016 *pnb_args
= nb_args
;
4019 static const char *next_arg_type(const char *typestr
)
4021 const char *p
= strchr(typestr
, ':');
4022 return (p
!= NULL
? ++p
: typestr
);
4025 static void monitor_find_completion(const char *cmdline
)
4027 const char *cmdname
;
4028 char *args
[MAX_ARGS
];
4029 int nb_args
, i
, len
;
4030 const char *ptype
, *str
;
4031 const mon_cmd_t
*cmd
;
4034 parse_cmdline(cmdline
, &nb_args
, args
);
4035 #ifdef DEBUG_COMPLETION
4036 for(i
= 0; i
< nb_args
; i
++) {
4037 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4041 /* if the line ends with a space, it means we want to complete the
4043 len
= strlen(cmdline
);
4044 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4045 if (nb_args
>= MAX_ARGS
)
4047 args
[nb_args
++] = qemu_strdup("");
4050 /* command completion */
4055 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4056 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4057 cmd_completion(cmdname
, cmd
->name
);
4060 /* find the command */
4061 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4062 if (compare_cmd(args
[0], cmd
->name
))
4067 ptype
= next_arg_type(cmd
->args_type
);
4068 for(i
= 0; i
< nb_args
- 2; i
++) {
4069 if (*ptype
!= '\0') {
4070 ptype
= next_arg_type(ptype
);
4071 while (*ptype
== '?')
4072 ptype
= next_arg_type(ptype
);
4075 str
= args
[nb_args
- 1];
4076 if (*ptype
== '-' && ptype
[1] != '\0') {
4081 /* file completion */
4082 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4083 file_completion(str
);
4086 /* block device name completion */
4087 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4088 bdrv_iterate(block_completion_it
, (void *)str
);
4091 /* XXX: more generic ? */
4092 if (!strcmp(cmd
->name
, "info")) {
4093 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4094 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4095 cmd_completion(str
, cmd
->name
);
4097 } else if (!strcmp(cmd
->name
, "sendkey")) {
4098 char *sep
= strrchr(str
, '-');
4101 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4102 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4103 cmd_completion(str
, key
->name
);
4105 } else if (!strcmp(cmd
->name
, "help|?")) {
4106 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4107 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4108 cmd_completion(str
, cmd
->name
);
4116 for(i
= 0; i
< nb_args
; i
++)
4120 static int monitor_can_read(void *opaque
)
4122 Monitor
*mon
= opaque
;
4124 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4127 typedef struct CmdArgs
{
4134 static int check_opt(const CmdArgs
*cmd_args
, const char *name
, QDict
*args
)
4136 if (!cmd_args
->optional
) {
4137 qemu_error_new(QERR_MISSING_PARAMETER
, name
);
4141 if (cmd_args
->type
== '-') {
4142 /* handlers expect a value, they need to be changed */
4143 qdict_put(args
, name
, qint_from_int(0));
4149 static int check_arg(const CmdArgs
*cmd_args
, QDict
*args
)
4154 name
= qstring_get_str(cmd_args
->name
);
4157 return check_opt(cmd_args
, name
, args
);
4160 value
= qdict_get(args
, name
);
4162 return check_opt(cmd_args
, name
, args
);
4165 switch (cmd_args
->type
) {
4169 if (qobject_type(value
) != QTYPE_QSTRING
) {
4170 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "string");
4176 const char *keys
[] = { "count", "format", "size", NULL
};
4178 for (i
= 0; keys
[i
]; i
++) {
4179 QObject
*obj
= qdict_get(args
, keys
[i
]);
4181 qemu_error_new(QERR_MISSING_PARAMETER
, name
);
4184 if (qobject_type(obj
) != QTYPE_QINT
) {
4185 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "int");
4194 if (qobject_type(value
) != QTYPE_QINT
) {
4195 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "int");
4201 if (qobject_type(value
) != QTYPE_QINT
&& qobject_type(value
) != QTYPE_QFLOAT
) {
4202 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "number");
4207 if (qobject_type(value
) != QTYPE_QINT
&&
4208 qobject_type(value
) != QTYPE_QBOOL
) {
4209 qemu_error_new(QERR_INVALID_PARAMETER_TYPE
, name
, "bool");
4212 if (qobject_type(value
) == QTYPE_QBOOL
) {
4213 /* handlers expect a QInt, they need to be changed */
4214 qdict_put(args
, name
,
4215 qint_from_int(qbool_get_int(qobject_to_qbool(value
))));
4226 static void cmd_args_init(CmdArgs
*cmd_args
)
4228 cmd_args
->name
= qstring_new();
4229 cmd_args
->type
= cmd_args
->flag
= cmd_args
->optional
= 0;
4233 * This is not trivial, we have to parse Monitor command's argument
4234 * type syntax to be able to check the arguments provided by clients.
4236 * In the near future we will be using an array for that and will be
4237 * able to drop all this parsing...
4239 static int monitor_check_qmp_args(const mon_cmd_t
*cmd
, QDict
*args
)
4245 if (cmd
->args_type
== NULL
) {
4246 return (qdict_size(args
) == 0 ? 0 : -1);
4250 cmd_args_init(&cmd_args
);
4252 for (p
= cmd
->args_type
;; p
++) {
4254 cmd_args
.type
= *++p
;
4256 if (cmd_args
.type
== '-') {
4257 cmd_args
.flag
= *p
++;
4258 cmd_args
.optional
= 1;
4259 } else if (*p
== '?') {
4260 cmd_args
.optional
= 1;
4264 assert(*p
== ',' || *p
== '\0');
4265 err
= check_arg(&cmd_args
, args
);
4267 QDECREF(cmd_args
.name
);
4268 cmd_args_init(&cmd_args
);
4274 qstring_append_chr(cmd_args
.name
, *p
);
4282 QDECREF(cmd_args
.name
);
4286 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4288 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4289 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4292 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4296 QDict
*input
, *args
;
4297 const mon_cmd_t
*cmd
;
4298 Monitor
*mon
= cur_mon
;
4299 const char *cmd_name
, *info_item
;
4302 qemu_errors_to_mon(mon
);
4304 obj
= json_parser_parse(tokens
, NULL
);
4306 // FIXME: should be triggered in json_parser_parse()
4307 qemu_error_new(QERR_JSON_PARSING
);
4309 } else if (qobject_type(obj
) != QTYPE_QDICT
) {
4310 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4311 qobject_decref(obj
);
4315 input
= qobject_to_qdict(obj
);
4317 mon
->mc
->id
= qdict_get(input
, "id");
4318 qobject_incref(mon
->mc
->id
);
4320 obj
= qdict_get(input
, "execute");
4322 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4324 } else if (qobject_type(obj
) != QTYPE_QSTRING
) {
4325 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT
, "string");
4329 cmd_name
= qstring_get_str(qobject_to_qstring(obj
));
4331 if (invalid_qmp_mode(mon
, cmd_name
)) {
4332 qemu_error_new(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4337 * XXX: We need this special case until we get info handlers
4338 * converted into 'query-' commands
4340 if (compare_cmd(cmd_name
, "info")) {
4341 qemu_error_new(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4343 } else if (strstart(cmd_name
, "query-", &info_item
)) {
4344 cmd
= monitor_find_command("info");
4345 qdict_put_obj(input
, "arguments",
4346 qobject_from_jsonf("{ 'item': %s }", info_item
));
4348 cmd
= monitor_find_command(cmd_name
);
4349 if (!cmd
|| !monitor_handler_ported(cmd
)) {
4350 qemu_error_new(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4355 obj
= qdict_get(input
, "arguments");
4359 args
= qobject_to_qdict(obj
);
4365 err
= monitor_check_qmp_args(cmd
, args
);
4370 if (monitor_handler_is_async(cmd
)) {
4371 qmp_async_cmd_handler(mon
, cmd
, args
);
4373 monitor_call_handler(mon
, cmd
, args
);
4380 monitor_protocol_emitter(mon
, NULL
);
4383 qemu_errors_to_previous();
4387 * monitor_control_read(): Read and handle QMP input
4389 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4391 Monitor
*old_mon
= cur_mon
;
4395 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4400 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4402 Monitor
*old_mon
= cur_mon
;
4408 for (i
= 0; i
< size
; i
++)
4409 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4411 if (size
== 0 || buf
[size
- 1] != 0)
4412 monitor_printf(cur_mon
, "corrupted command\n");
4414 handle_user_command(cur_mon
, (char *)buf
);
4420 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4422 monitor_suspend(mon
);
4423 handle_user_command(mon
, cmdline
);
4424 monitor_resume(mon
);
4427 int monitor_suspend(Monitor
*mon
)
4435 void monitor_resume(Monitor
*mon
)
4439 if (--mon
->suspend_cnt
== 0)
4440 readline_show_prompt(mon
->rs
);
4443 static QObject
*get_qmp_greeting(void)
4447 do_info_version(NULL
, &ver
);
4448 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4452 * monitor_control_event(): Print QMP gretting
4454 static void monitor_control_event(void *opaque
, int event
)
4457 Monitor
*mon
= opaque
;
4460 case CHR_EVENT_OPENED
:
4461 mon
->mc
->command_mode
= 0;
4462 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4463 data
= get_qmp_greeting();
4464 monitor_json_emitter(mon
, data
);
4465 qobject_decref(data
);
4467 case CHR_EVENT_CLOSED
:
4468 json_message_parser_destroy(&mon
->mc
->parser
);
4473 static void monitor_event(void *opaque
, int event
)
4475 Monitor
*mon
= opaque
;
4478 case CHR_EVENT_MUX_IN
:
4480 if (mon
->reset_seen
) {
4481 readline_restart(mon
->rs
);
4482 monitor_resume(mon
);
4485 mon
->suspend_cnt
= 0;
4489 case CHR_EVENT_MUX_OUT
:
4490 if (mon
->reset_seen
) {
4491 if (mon
->suspend_cnt
== 0) {
4492 monitor_printf(mon
, "\n");
4495 monitor_suspend(mon
);
4502 case CHR_EVENT_OPENED
:
4503 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4504 "information\n", QEMU_VERSION
);
4505 if (!mon
->mux_out
) {
4506 readline_show_prompt(mon
->rs
);
4508 mon
->reset_seen
= 1;
4522 void monitor_init(CharDriverState
*chr
, int flags
)
4524 static int is_first_init
= 1;
4527 if (is_first_init
) {
4528 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
4532 mon
= qemu_mallocz(sizeof(*mon
));
4536 if (flags
& MONITOR_USE_READLINE
) {
4537 mon
->rs
= readline_init(mon
, monitor_find_completion
);
4538 monitor_read_command(mon
, 0);
4541 if (monitor_ctrl_mode(mon
)) {
4542 mon
->mc
= qemu_mallocz(sizeof(MonitorControl
));
4543 /* Control mode requires special handlers */
4544 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
4545 monitor_control_event
, mon
);
4547 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4548 monitor_event
, mon
);
4551 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4552 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
4556 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
4558 BlockDriverState
*bs
= opaque
;
4561 if (bdrv_set_key(bs
, password
) != 0) {
4562 monitor_printf(mon
, "invalid password\n");
4565 if (mon
->password_completion_cb
)
4566 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4568 monitor_read_command(mon
, 1);
4571 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4572 BlockDriverCompletionFunc
*completion_cb
,
4577 if (!bdrv_key_required(bs
)) {
4579 completion_cb(opaque
, 0);
4583 if (monitor_ctrl_mode(mon
)) {
4584 qemu_error_new(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
4588 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4589 bdrv_get_encrypted_filename(bs
));
4591 mon
->password_completion_cb
= completion_cb
;
4592 mon
->password_opaque
= opaque
;
4594 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4596 if (err
&& completion_cb
)
4597 completion_cb(opaque
, err
);
4602 typedef struct QemuErrorSink QemuErrorSink
;
4603 struct QemuErrorSink
{
4612 QemuErrorSink
*previous
;
4615 static QemuErrorSink
*qemu_error_sink
;
4617 void qemu_errors_to_file(FILE *fp
)
4619 QemuErrorSink
*sink
;
4621 sink
= qemu_mallocz(sizeof(*sink
));
4622 sink
->dest
= ERR_SINK_FILE
;
4624 sink
->previous
= qemu_error_sink
;
4625 qemu_error_sink
= sink
;
4628 void qemu_errors_to_mon(Monitor
*mon
)
4630 QemuErrorSink
*sink
;
4632 sink
= qemu_mallocz(sizeof(*sink
));
4633 sink
->dest
= ERR_SINK_MONITOR
;
4635 sink
->previous
= qemu_error_sink
;
4636 qemu_error_sink
= sink
;
4639 void qemu_errors_to_previous(void)
4641 QemuErrorSink
*sink
;
4643 assert(qemu_error_sink
!= NULL
);
4644 sink
= qemu_error_sink
;
4645 qemu_error_sink
= sink
->previous
;
4649 void qemu_error(const char *fmt
, ...)
4653 assert(qemu_error_sink
!= NULL
);
4654 switch (qemu_error_sink
->dest
) {
4656 va_start(args
, fmt
);
4657 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
4660 case ERR_SINK_MONITOR
:
4661 va_start(args
, fmt
);
4662 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);
4668 void qemu_error_internal(const char *file
, int linenr
, const char *func
,
4669 const char *fmt
, ...)
4674 assert(qemu_error_sink
!= NULL
);
4677 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
4680 switch (qemu_error_sink
->dest
) {
4682 qerror_print(qerror
);
4685 case ERR_SINK_MONITOR
:
4686 /* report only the first error */
4687 if (!qemu_error_sink
->mon
->error
) {
4688 qemu_error_sink
->mon
->error
= qerror
;
4690 /* XXX: warn the programmer */