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
25 #include "qemu/osdep.h"
27 #include "monitor-internal.h"
28 #include "qapi/error.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qnum.h"
31 #include "qemu/config-file.h"
32 #include "qemu/ctype.h"
33 #include "qemu/cutils.h"
35 #include "qemu/option.h"
36 #include "qemu/units.h"
37 #include "sysemu/block-backend.h"
38 #include "sysemu/runstate.h"
41 static void monitor_command_cb(void *opaque
, const char *cmdline
,
42 void *readline_opaque
)
44 MonitorHMP
*mon
= opaque
;
46 monitor_suspend(&mon
->common
);
47 handle_hmp_command(mon
, cmdline
);
48 monitor_resume(&mon
->common
);
51 void monitor_read_command(MonitorHMP
*mon
, int show_prompt
)
57 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
59 readline_show_prompt(mon
->rs
);
63 int monitor_read_password(MonitorHMP
*mon
, ReadLineFunc
*readline_func
,
67 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
68 /* prompt is printed on return from the command handler */
71 monitor_printf(&mon
->common
,
72 "terminal does not support password prompting\n");
77 static int get_str(char *buf
, int buf_size
, const char **pp
)
85 while (qemu_isspace(*p
)) {
96 while (*p
!= '\0' && *p
!= '\"') {
112 printf("unsupported escape code: '\\%c'\n", c
);
115 if ((q
- buf
) < buf_size
- 1) {
119 if ((q
- buf
) < buf_size
- 1) {
126 printf("unterminated string\n");
131 while (*p
!= '\0' && !qemu_isspace(*p
)) {
132 if ((q
- buf
) < buf_size
- 1) {
145 static void free_cmdline_args(char **args
, int nb_args
)
149 assert(nb_args
<= MAX_ARGS
);
151 for (i
= 0; i
< nb_args
; i
++) {
158 * Parse the command line to get valid args.
159 * @cmdline: command line to be parsed.
160 * @pnb_args: location to store the number of args, must NOT be NULL.
161 * @args: location to store the args, which should be freed by caller, must
164 * Returns 0 on success, negative on failure.
166 * NOTE: this parser is an approximate form of the real command parser. Number
167 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
168 * return with failure.
170 static int parse_cmdline(const char *cmdline
,
171 int *pnb_args
, char **args
)
180 while (qemu_isspace(*p
)) {
186 if (nb_args
>= MAX_ARGS
) {
189 ret
= get_str(buf
, sizeof(buf
), &p
);
193 args
[nb_args
] = g_strdup(buf
);
200 free_cmdline_args(args
, nb_args
);
205 * Can command @cmd be executed in preconfig state?
207 static bool cmd_can_preconfig(const HMPCommand
*cmd
)
213 return strchr(cmd
->flags
, 'p');
216 static bool cmd_available(const HMPCommand
*cmd
)
218 return !runstate_check(RUN_STATE_PRECONFIG
) || cmd_can_preconfig(cmd
);
221 static void help_cmd_dump_one(Monitor
*mon
,
222 const HMPCommand
*cmd
,
228 if (!cmd_available(cmd
)) {
232 for (i
= 0; i
< prefix_args_nb
; i
++) {
233 monitor_printf(mon
, "%s ", prefix_args
[i
]);
235 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
238 /* @args[@arg_index] is the valid command need to find in @cmds */
239 static void help_cmd_dump(Monitor
*mon
, const HMPCommand
*cmds
,
240 char **args
, int nb_args
, int arg_index
)
242 const HMPCommand
*cmd
;
245 /* No valid arg need to compare with, dump all in *cmds */
246 if (arg_index
>= nb_args
) {
247 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
248 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
253 /* Find one entry to dump */
254 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
255 if (hmp_compare_cmd(args
[arg_index
], cmd
->name
) &&
256 cmd_available(cmd
)) {
257 if (cmd
->sub_table
) {
258 /* continue with next arg */
259 help_cmd_dump(mon
, cmd
->sub_table
,
260 args
, nb_args
, arg_index
+ 1);
262 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
268 /* Command not found */
269 monitor_printf(mon
, "unknown command: '");
270 for (i
= 0; i
<= arg_index
; i
++) {
271 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
275 void help_cmd(Monitor
*mon
, const char *name
)
277 char *args
[MAX_ARGS
];
280 /* 1. parse user input */
282 /* special case for log, directly dump and return */
283 if (!strcmp(name
, "log")) {
284 const QEMULogItem
*item
;
285 monitor_printf(mon
, "Log items (comma separated):\n");
286 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
287 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
288 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
293 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
298 /* 2. dump the contents according to parsed args */
299 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
301 free_cmdline_args(args
, nb_args
);
304 /*******************************************************************/
306 static const char *pch
;
307 static sigjmp_buf expr_env
;
309 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
310 expr_error(Monitor
*mon
, const char *fmt
, ...)
314 monitor_vprintf(mon
, fmt
, ap
);
315 monitor_printf(mon
, "\n");
317 siglongjmp(expr_env
, 1);
320 static void next(void)
324 while (qemu_isspace(*pch
)) {
330 static int64_t expr_sum(Monitor
*mon
);
332 static int64_t expr_unary(Monitor
*mon
)
345 n
= -expr_unary(mon
);
349 n
= ~expr_unary(mon
);
355 expr_error(mon
, "')' expected");
362 expr_error(mon
, "character constant expected");
367 expr_error(mon
, "missing terminating \' character");
378 while ((*pch
>= 'a' && *pch
<= 'z') ||
379 (*pch
>= 'A' && *pch
<= 'Z') ||
380 (*pch
>= '0' && *pch
<= '9') ||
381 *pch
== '_' || *pch
== '.') {
382 if ((q
- buf
) < sizeof(buf
) - 1) {
387 while (qemu_isspace(*pch
)) {
391 ret
= get_monitor_def(mon
, ®
, buf
);
393 expr_error(mon
, "unknown register");
399 expr_error(mon
, "unexpected end of expression");
404 n
= strtoull(pch
, &p
, 0);
405 if (errno
== ERANGE
) {
406 expr_error(mon
, "number too large");
409 expr_error(mon
, "invalid char '%c' in expression", *p
);
412 while (qemu_isspace(*pch
)) {
420 static int64_t expr_prod(Monitor
*mon
)
425 val
= expr_unary(mon
);
428 if (op
!= '*' && op
!= '/' && op
!= '%') {
432 val2
= expr_unary(mon
);
441 expr_error(mon
, "division by zero");
454 static int64_t expr_logic(Monitor
*mon
)
459 val
= expr_prod(mon
);
462 if (op
!= '&' && op
!= '|' && op
!= '^') {
466 val2
= expr_prod(mon
);
483 static int64_t expr_sum(Monitor
*mon
)
488 val
= expr_logic(mon
);
491 if (op
!= '+' && op
!= '-') {
495 val2
= expr_logic(mon
);
505 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
508 if (sigsetjmp(expr_env
, 0)) {
512 while (qemu_isspace(*pch
)) {
515 *pval
= expr_sum(mon
);
520 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
526 d
= strtod(p
, &tailp
);
528 monitor_printf(mon
, "Number expected\n");
531 if (d
!= d
|| d
- d
!= 0) {
532 /* NaN or infinity */
533 monitor_printf(mon
, "Bad number\n");
542 * Store the command-name in cmdname, and return a pointer to
543 * the remaining of the command string.
545 static const char *get_command_name(const char *cmdline
,
546 char *cmdname
, size_t nlen
)
549 const char *p
, *pstart
;
552 while (qemu_isspace(*p
)) {
559 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
)) {
563 if (len
> nlen
- 1) {
566 memcpy(cmdname
, pstart
, len
);
572 * Read key of 'type' into 'key' and return the current
575 static char *key_get_info(const char *type
, char **key
)
584 p
= strchr(type
, ':');
591 str
= g_malloc(len
+ 1);
592 memcpy(str
, type
, len
);
599 static int default_fmt_format
= 'x';
600 static int default_fmt_size
= 4;
602 static int is_valid_option(const char *c
, const char *typestr
)
610 typestr
= strstr(typestr
, option
);
611 return (typestr
!= NULL
);
614 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
617 const HMPCommand
*cmd
;
619 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
620 if (hmp_compare_cmd(cmdname
, cmd
->name
)) {
629 * Parse command name from @cmdp according to command table @table.
630 * If blank, return NULL.
631 * Else, if no valid command can be found, report to @mon, and return
633 * Else, change @cmdp to point right behind the name, and return its
634 * command table entry.
635 * Do not assume the return value points into @table! It doesn't when
636 * the command is found in a sub-command table.
638 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
639 const char *cmdp_start
,
643 Monitor
*mon
= &hmp_mon
->common
;
645 const HMPCommand
*cmd
;
648 /* extract the command name */
649 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
654 cmd
= search_dispatch_table(table
, cmdname
);
656 monitor_printf(mon
, "unknown command: '%.*s'\n",
657 (int)(p
- cmdp_start
), cmdp_start
);
660 if (!cmd_available(cmd
)) {
661 monitor_printf(mon
, "Command '%.*s' not available with -preconfig "
662 "until after exit_preconfig.\n",
663 (int)(p
- cmdp_start
), cmdp_start
);
667 /* filter out following useless space */
668 while (qemu_isspace(*p
)) {
673 /* search sub command */
674 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
675 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
682 * Parse arguments for @cmd.
683 * If it can't be parsed, report to @mon, and return NULL.
684 * Else, insert command arguments into a QDict, and return it.
685 * Note: On success, caller has to free the QDict structure.
687 static QDict
*monitor_parse_arguments(Monitor
*mon
,
689 const HMPCommand
*cmd
)
694 const char *p
= *endp
;
696 QDict
*qdict
= qdict_new();
698 /* parse the parameters */
699 typestr
= cmd
->args_type
;
701 typestr
= key_get_info(typestr
, &key
);
714 while (qemu_isspace(*p
)) {
717 if (*typestr
== '?') {
720 /* no optional string: NULL argument */
724 ret
= get_str(buf
, sizeof(buf
), &p
);
728 monitor_printf(mon
, "%s: filename expected\n",
732 monitor_printf(mon
, "%s: block device name expected\n",
736 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
741 qdict_put_str(qdict
, key
, buf
);
746 QemuOptsList
*opts_list
;
749 opts_list
= qemu_find_opts(key
);
750 if (!opts_list
|| opts_list
->desc
->name
) {
753 while (qemu_isspace(*p
)) {
759 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
762 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
766 qemu_opts_to_qdict(opts
, qdict
);
772 int count
, format
, size
;
774 while (qemu_isspace(*p
)) {
781 if (qemu_isdigit(*p
)) {
783 while (qemu_isdigit(*p
)) {
784 count
= count
* 10 + (*p
- '0');
822 if (*p
!= '\0' && !qemu_isspace(*p
)) {
823 monitor_printf(mon
, "invalid char in format: '%c'\n",
828 format
= default_fmt_format
;
831 /* for 'i', not specifying a size gives -1 as size */
833 size
= default_fmt_size
;
835 default_fmt_size
= size
;
837 default_fmt_format
= format
;
840 format
= default_fmt_format
;
842 size
= default_fmt_size
;
847 qdict_put_int(qdict
, "count", count
);
848 qdict_put_int(qdict
, "format", format
);
849 qdict_put_int(qdict
, "size", size
);
858 while (qemu_isspace(*p
)) {
861 if (*typestr
== '?' || *typestr
== '.') {
862 if (*typestr
== '?') {
870 while (qemu_isspace(*p
)) {
880 if (get_expr(mon
, &val
, &p
)) {
883 /* Check if 'i' is greater than 32-bit */
884 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
885 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
886 monitor_printf(mon
, "integer is for 32-bit values\n");
888 } else if (c
== 'M') {
890 monitor_printf(mon
, "enter a positive value\n");
895 qdict_put_int(qdict
, key
, val
);
904 while (qemu_isspace(*p
)) {
907 if (*typestr
== '?') {
913 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
914 if (ret
< 0 || val
> INT64_MAX
) {
915 monitor_printf(mon
, "invalid size\n");
918 qdict_put_int(qdict
, key
, val
);
926 while (qemu_isspace(*p
)) {
929 if (*typestr
== '?') {
935 if (get_double(mon
, &val
, &p
) < 0) {
938 if (p
[0] && p
[1] == 's') {
941 val
/= 1e3
; p
+= 2; break;
943 val
/= 1e6
; p
+= 2; break;
945 val
/= 1e9
; p
+= 2; break;
948 if (*p
&& !qemu_isspace(*p
)) {
949 monitor_printf(mon
, "Unknown unit suffix\n");
952 qdict_put(qdict
, key
, qnum_from_double(val
));
960 while (qemu_isspace(*p
)) {
964 while (qemu_isgraph(*p
)) {
967 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
969 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
972 monitor_printf(mon
, "Expected 'on' or 'off'\n");
975 qdict_put_bool(qdict
, key
, val
);
988 while (qemu_isspace(*p
)) {
994 if (!is_valid_option(p
, typestr
)) {
995 monitor_printf(mon
, "%s: unsupported option -%c\n",
1007 qdict_put_bool(qdict
, key
, true);
1014 /* package all remaining string */
1017 while (qemu_isspace(*p
)) {
1020 if (*typestr
== '?') {
1023 /* no remaining string: NULL argument */
1029 monitor_printf(mon
, "%s: string expected\n",
1033 qdict_put_str(qdict
, key
, p
);
1039 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
1045 /* check that all arguments were parsed */
1046 while (qemu_isspace(*p
)) {
1050 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
1058 qobject_unref(qdict
);
1063 typedef struct HandleHmpCommandCo
{
1065 const HMPCommand
*cmd
;
1068 } HandleHmpCommandCo
;
1070 static void handle_hmp_command_co(void *opaque
)
1072 HandleHmpCommandCo
*data
= opaque
;
1073 data
->cmd
->cmd(data
->mon
, data
->qdict
);
1074 monitor_set_cur(qemu_coroutine_self(), NULL
);
1078 void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
1081 const HMPCommand
*cmd
;
1082 const char *cmd_start
= cmdline
;
1084 trace_handle_hmp_command(mon
, cmdline
);
1086 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
1091 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
1093 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
1096 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
1097 (int)(cmdline
- cmd_start
), cmd_start
);
1101 if (!cmd
->coroutine
) {
1102 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1103 Monitor
*old_mon
= monitor_set_cur(qemu_coroutine_self(), &mon
->common
);
1104 cmd
->cmd(&mon
->common
, qdict
);
1105 monitor_set_cur(qemu_coroutine_self(), old_mon
);
1107 HandleHmpCommandCo data
= {
1108 .mon
= &mon
->common
,
1113 Coroutine
*co
= qemu_coroutine_create(handle_hmp_command_co
, &data
);
1114 monitor_set_cur(co
, &mon
->common
);
1115 aio_co_enter(qemu_get_aio_context(), co
);
1116 AIO_WAIT_WHILE(qemu_get_aio_context(), !data
.done
);
1119 qobject_unref(qdict
);
1122 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
1124 const char *p
, *pstart
;
1131 p
= qemu_strchrnul(p
, '|');
1133 if (len
> sizeof(cmd
) - 2) {
1134 len
= sizeof(cmd
) - 2;
1136 memcpy(cmd
, pstart
, len
);
1138 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1139 readline_add_completion(mon
->rs
, cmd
);
1148 static void file_completion(MonitorHMP
*mon
, const char *input
)
1153 char file
[1024], file_prefix
[1024];
1157 p
= strrchr(input
, '/');
1160 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1161 pstrcpy(path
, sizeof(path
), ".");
1163 input_path_len
= p
- input
+ 1;
1164 memcpy(path
, input
, input_path_len
);
1165 if (input_path_len
> sizeof(path
) - 1) {
1166 input_path_len
= sizeof(path
) - 1;
1168 path
[input_path_len
] = '\0';
1169 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1172 ffs
= opendir(path
);
1183 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
1187 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1188 memcpy(file
, input
, input_path_len
);
1189 if (input_path_len
< sizeof(file
)) {
1190 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
1194 * stat the file to find out if it's a directory.
1195 * In that case add a slash to speed up typing long paths
1197 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
1198 pstrcat(file
, sizeof(file
), "/");
1200 readline_add_completion(mon
->rs
, file
);
1206 static const char *next_arg_type(const char *typestr
)
1208 const char *p
= strchr(typestr
, ':');
1209 return (p
!= NULL
? ++p
: typestr
);
1212 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
1213 const HMPCommand
*cmd_table
,
1217 const char *cmdname
;
1219 const char *ptype
, *old_ptype
, *str
, *name
;
1220 const HMPCommand
*cmd
;
1221 BlockBackend
*blk
= NULL
;
1224 /* command completion */
1230 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
1231 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1232 if (cmd_available(cmd
)) {
1233 cmd_completion(mon
, cmdname
, cmd
->name
);
1237 /* find the command */
1238 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1239 if (hmp_compare_cmd(args
[0], cmd
->name
) &&
1240 cmd_available(cmd
)) {
1248 if (cmd
->sub_table
) {
1249 /* do the job again */
1250 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
1251 &args
[1], nb_args
- 1);
1254 if (cmd
->command_completion
) {
1255 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
1259 ptype
= next_arg_type(cmd
->args_type
);
1260 for (i
= 0; i
< nb_args
- 2; i
++) {
1261 if (*ptype
!= '\0') {
1262 ptype
= next_arg_type(ptype
);
1263 while (*ptype
== '?') {
1264 ptype
= next_arg_type(ptype
);
1268 str
= args
[nb_args
- 1];
1270 while (*ptype
== '-' && old_ptype
!= ptype
) {
1272 ptype
= next_arg_type(ptype
);
1276 /* file completion */
1277 readline_set_completion_index(mon
->rs
, strlen(str
));
1278 file_completion(mon
, str
);
1281 /* block device name completion */
1282 readline_set_completion_index(mon
->rs
, strlen(str
));
1283 while ((blk
= blk_next(blk
)) != NULL
) {
1284 name
= blk_name(blk
);
1285 if (str
[0] == '\0' ||
1286 !strncmp(name
, str
, strlen(str
))) {
1287 readline_add_completion(mon
->rs
, name
);
1293 if (!strcmp(cmd
->name
, "help|?")) {
1294 monitor_find_completion_by_table(mon
, cmd_table
,
1295 &args
[1], nb_args
- 1);
1304 static void monitor_find_completion(void *opaque
,
1305 const char *cmdline
)
1307 MonitorHMP
*mon
= opaque
;
1308 char *args
[MAX_ARGS
];
1311 /* 1. parse the cmdline */
1312 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
1317 * if the line ends with a space, it means we want to complete the
1320 len
= strlen(cmdline
);
1321 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
1322 if (nb_args
>= MAX_ARGS
) {
1325 args
[nb_args
++] = g_strdup("");
1328 /* 2. auto complete according to args */
1329 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
1332 free_cmdline_args(args
, nb_args
);
1335 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
1337 MonitorHMP
*mon
= container_of(opaque
, MonitorHMP
, common
);
1341 for (i
= 0; i
< size
; i
++) {
1342 readline_handle_byte(mon
->rs
, buf
[i
]);
1345 if (size
== 0 || buf
[size
- 1] != 0) {
1346 monitor_printf(&mon
->common
, "corrupted command\n");
1348 handle_hmp_command(mon
, (char *)buf
);
1353 static void monitor_event(void *opaque
, QEMUChrEvent event
)
1355 Monitor
*mon
= opaque
;
1356 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1359 case CHR_EVENT_MUX_IN
:
1360 qemu_mutex_lock(&mon
->mon_lock
);
1362 qemu_mutex_unlock(&mon
->mon_lock
);
1363 if (mon
->reset_seen
) {
1364 readline_restart(hmp_mon
->rs
);
1365 monitor_resume(mon
);
1368 qatomic_mb_set(&mon
->suspend_cnt
, 0);
1372 case CHR_EVENT_MUX_OUT
:
1373 if (mon
->reset_seen
) {
1374 if (qatomic_mb_read(&mon
->suspend_cnt
) == 0) {
1375 monitor_printf(mon
, "\n");
1378 monitor_suspend(mon
);
1380 qatomic_inc(&mon
->suspend_cnt
);
1382 qemu_mutex_lock(&mon
->mon_lock
);
1384 qemu_mutex_unlock(&mon
->mon_lock
);
1387 case CHR_EVENT_OPENED
:
1388 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
1389 "information\n", QEMU_VERSION
);
1390 if (!mon
->mux_out
) {
1391 readline_restart(hmp_mon
->rs
);
1392 readline_show_prompt(hmp_mon
->rs
);
1394 mon
->reset_seen
= 1;
1398 case CHR_EVENT_CLOSED
:
1400 monitor_fdsets_cleanup();
1403 case CHR_EVENT_BREAK
:
1411 * These functions just adapt the readline interface in a typesafe way. We
1412 * could cast function pointers but that discards compiler checks.
1414 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
1415 const char *fmt
, ...)
1417 MonitorHMP
*mon
= opaque
;
1420 monitor_vprintf(&mon
->common
, fmt
, ap
);
1424 static void monitor_readline_flush(void *opaque
)
1426 MonitorHMP
*mon
= opaque
;
1427 monitor_flush(&mon
->common
);
1430 void monitor_init_hmp(Chardev
*chr
, bool use_readline
, Error
**errp
)
1432 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
1434 if (!qemu_chr_fe_init(&mon
->common
.chr
, chr
, errp
)) {
1439 monitor_data_init(&mon
->common
, false, false, false);
1441 mon
->use_readline
= use_readline
;
1442 if (mon
->use_readline
) {
1443 mon
->rs
= readline_init(monitor_readline_printf
,
1444 monitor_readline_flush
,
1446 monitor_find_completion
);
1447 monitor_read_command(mon
, 0);
1450 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
1451 monitor_event
, NULL
, &mon
->common
, NULL
, true);
1452 monitor_list_append(&mon
->common
);