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 "hw/qdev-core.h"
28 #include "monitor-internal.h"
29 #include "qapi/error.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qnum.h"
32 #include "qemu/config-file.h"
33 #include "qemu/ctype.h"
34 #include "qemu/cutils.h"
36 #include "qemu/option.h"
37 #include "qemu/units.h"
38 #include "sysemu/block-backend.h"
39 #include "sysemu/runstate.h"
42 static void monitor_command_cb(void *opaque
, const char *cmdline
,
43 void *readline_opaque
)
45 MonitorHMP
*mon
= opaque
;
47 monitor_suspend(&mon
->common
);
48 handle_hmp_command(mon
, cmdline
);
49 monitor_resume(&mon
->common
);
52 void monitor_read_command(MonitorHMP
*mon
, int show_prompt
)
58 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
60 readline_show_prompt(mon
->rs
);
64 int monitor_read_password(MonitorHMP
*mon
, ReadLineFunc
*readline_func
,
68 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
69 /* prompt is printed on return from the command handler */
72 monitor_printf(&mon
->common
,
73 "terminal does not support password prompting\n");
78 static int get_str(char *buf
, int buf_size
, const char **pp
)
86 while (qemu_isspace(*p
)) {
97 while (*p
!= '\0' && *p
!= '\"') {
113 printf("unsupported escape code: '\\%c'\n", c
);
116 if ((q
- buf
) < buf_size
- 1) {
120 if ((q
- buf
) < buf_size
- 1) {
127 printf("unterminated string\n");
132 while (*p
!= '\0' && !qemu_isspace(*p
)) {
133 if ((q
- buf
) < buf_size
- 1) {
146 static void free_cmdline_args(char **args
, int nb_args
)
150 assert(nb_args
<= MAX_ARGS
);
152 for (i
= 0; i
< nb_args
; i
++) {
159 * Parse the command line to get valid args.
160 * @cmdline: command line to be parsed.
161 * @pnb_args: location to store the number of args, must NOT be NULL.
162 * @args: location to store the args, which should be freed by caller, must
165 * Returns 0 on success, negative on failure.
167 * NOTE: this parser is an approximate form of the real command parser. Number
168 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
169 * return with failure.
171 static int parse_cmdline(const char *cmdline
,
172 int *pnb_args
, char **args
)
181 while (qemu_isspace(*p
)) {
187 if (nb_args
>= MAX_ARGS
) {
190 ret
= get_str(buf
, sizeof(buf
), &p
);
194 args
[nb_args
] = g_strdup(buf
);
201 free_cmdline_args(args
, nb_args
);
206 * Can command @cmd be executed in preconfig state?
208 static bool cmd_can_preconfig(const HMPCommand
*cmd
)
214 return strchr(cmd
->flags
, 'p');
217 static bool cmd_available(const HMPCommand
*cmd
)
219 return phase_check(PHASE_MACHINE_READY
) || cmd_can_preconfig(cmd
);
222 static void help_cmd_dump_one(Monitor
*mon
,
223 const HMPCommand
*cmd
,
229 if (!cmd_available(cmd
)) {
233 for (i
= 0; i
< prefix_args_nb
; i
++) {
234 monitor_printf(mon
, "%s ", prefix_args
[i
]);
236 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
239 /* @args[@arg_index] is the valid command need to find in @cmds */
240 static void help_cmd_dump(Monitor
*mon
, const HMPCommand
*cmds
,
241 char **args
, int nb_args
, int arg_index
)
243 const HMPCommand
*cmd
;
246 /* No valid arg need to compare with, dump all in *cmds */
247 if (arg_index
>= nb_args
) {
248 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
249 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
254 /* Find one entry to dump */
255 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
256 if (hmp_compare_cmd(args
[arg_index
], cmd
->name
) &&
257 cmd_available(cmd
)) {
258 if (cmd
->sub_table
) {
259 /* continue with next arg */
260 help_cmd_dump(mon
, cmd
->sub_table
,
261 args
, nb_args
, arg_index
+ 1);
263 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
269 /* Command not found */
270 monitor_printf(mon
, "unknown command: '");
271 for (i
= 0; i
<= arg_index
; i
++) {
272 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
276 void help_cmd(Monitor
*mon
, const char *name
)
278 char *args
[MAX_ARGS
];
281 /* 1. parse user input */
283 /* special case for log, directly dump and return */
284 if (!strcmp(name
, "log")) {
285 const QEMULogItem
*item
;
286 monitor_printf(mon
, "Log items (comma separated):\n");
287 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
288 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
289 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
294 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
299 /* 2. dump the contents according to parsed args */
300 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
302 free_cmdline_args(args
, nb_args
);
305 /*******************************************************************/
307 static const char *pch
;
308 static sigjmp_buf expr_env
;
310 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
311 expr_error(Monitor
*mon
, const char *fmt
, ...)
315 monitor_vprintf(mon
, fmt
, ap
);
316 monitor_printf(mon
, "\n");
318 siglongjmp(expr_env
, 1);
321 static void next(void)
325 while (qemu_isspace(*pch
)) {
331 static int64_t expr_sum(Monitor
*mon
);
333 static int64_t expr_unary(Monitor
*mon
)
346 n
= -expr_unary(mon
);
350 n
= ~expr_unary(mon
);
356 expr_error(mon
, "')' expected");
363 expr_error(mon
, "character constant expected");
368 expr_error(mon
, "missing terminating \' character");
379 while ((*pch
>= 'a' && *pch
<= 'z') ||
380 (*pch
>= 'A' && *pch
<= 'Z') ||
381 (*pch
>= '0' && *pch
<= '9') ||
382 *pch
== '_' || *pch
== '.') {
383 if ((q
- buf
) < sizeof(buf
) - 1) {
388 while (qemu_isspace(*pch
)) {
392 ret
= get_monitor_def(mon
, ®
, buf
);
394 expr_error(mon
, "unknown register");
400 expr_error(mon
, "unexpected end of expression");
405 n
= strtoull(pch
, &p
, 0);
406 if (errno
== ERANGE
) {
407 expr_error(mon
, "number too large");
410 expr_error(mon
, "invalid char '%c' in expression", *p
);
413 while (qemu_isspace(*pch
)) {
421 static int64_t expr_prod(Monitor
*mon
)
426 val
= expr_unary(mon
);
429 if (op
!= '*' && op
!= '/' && op
!= '%') {
433 val2
= expr_unary(mon
);
442 expr_error(mon
, "division by zero");
455 static int64_t expr_logic(Monitor
*mon
)
460 val
= expr_prod(mon
);
463 if (op
!= '&' && op
!= '|' && op
!= '^') {
467 val2
= expr_prod(mon
);
484 static int64_t expr_sum(Monitor
*mon
)
489 val
= expr_logic(mon
);
492 if (op
!= '+' && op
!= '-') {
496 val2
= expr_logic(mon
);
506 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
509 if (sigsetjmp(expr_env
, 0)) {
513 while (qemu_isspace(*pch
)) {
516 *pval
= expr_sum(mon
);
521 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
527 d
= strtod(p
, &tailp
);
529 monitor_printf(mon
, "Number expected\n");
532 if (d
!= d
|| d
- d
!= 0) {
533 /* NaN or infinity */
534 monitor_printf(mon
, "Bad number\n");
543 * Store the command-name in cmdname, and return a pointer to
544 * the remaining of the command string.
546 static const char *get_command_name(const char *cmdline
,
547 char *cmdname
, size_t nlen
)
550 const char *p
, *pstart
;
553 while (qemu_isspace(*p
)) {
560 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
)) {
564 if (len
> nlen
- 1) {
567 memcpy(cmdname
, pstart
, len
);
573 * Read key of 'type' into 'key' and return the current
576 static char *key_get_info(const char *type
, char **key
)
585 p
= strchr(type
, ':');
592 str
= g_malloc(len
+ 1);
593 memcpy(str
, type
, len
);
600 static int default_fmt_format
= 'x';
601 static int default_fmt_size
= 4;
603 static int is_valid_option(const char *c
, const char *typestr
)
611 typestr
= strstr(typestr
, option
);
612 return (typestr
!= NULL
);
615 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
618 const HMPCommand
*cmd
;
620 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
621 if (hmp_compare_cmd(cmdname
, cmd
->name
)) {
630 * Parse command name from @cmdp according to command table @table.
631 * If blank, return NULL.
632 * Else, if no valid command can be found, report to @mon, and return
634 * Else, change @cmdp to point right behind the name, and return its
635 * command table entry.
636 * Do not assume the return value points into @table! It doesn't when
637 * the command is found in a sub-command table.
639 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
640 const char *cmdp_start
,
644 Monitor
*mon
= &hmp_mon
->common
;
646 const HMPCommand
*cmd
;
649 /* extract the command name */
650 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
655 cmd
= search_dispatch_table(table
, cmdname
);
657 monitor_printf(mon
, "unknown command: '%.*s'\n",
658 (int)(p
- cmdp_start
), cmdp_start
);
661 if (!cmd_available(cmd
)) {
662 monitor_printf(mon
, "Command '%.*s' not available "
663 "until machine initialization has completed.\n",
664 (int)(p
- cmdp_start
), cmdp_start
);
668 /* filter out following useless space */
669 while (qemu_isspace(*p
)) {
674 /* search sub command */
675 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
676 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
683 * Parse arguments for @cmd.
684 * If it can't be parsed, report to @mon, and return NULL.
685 * Else, insert command arguments into a QDict, and return it.
686 * Note: On success, caller has to free the QDict structure.
688 static QDict
*monitor_parse_arguments(Monitor
*mon
,
690 const HMPCommand
*cmd
)
695 const char *p
= *endp
;
697 QDict
*qdict
= qdict_new();
699 /* parse the parameters */
700 typestr
= cmd
->args_type
;
702 typestr
= key_get_info(typestr
, &key
);
715 while (qemu_isspace(*p
)) {
718 if (*typestr
== '?') {
721 /* no optional string: NULL argument */
725 ret
= get_str(buf
, sizeof(buf
), &p
);
729 monitor_printf(mon
, "%s: filename expected\n",
733 monitor_printf(mon
, "%s: block device name expected\n",
737 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
742 qdict_put_str(qdict
, key
, buf
);
747 QemuOptsList
*opts_list
;
750 opts_list
= qemu_find_opts(key
);
751 if (!opts_list
|| opts_list
->desc
->name
) {
754 while (qemu_isspace(*p
)) {
760 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
763 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
767 qemu_opts_to_qdict(opts
, qdict
);
773 int count
, format
, size
;
775 while (qemu_isspace(*p
)) {
782 if (qemu_isdigit(*p
)) {
784 while (qemu_isdigit(*p
)) {
785 count
= count
* 10 + (*p
- '0');
823 if (*p
!= '\0' && !qemu_isspace(*p
)) {
824 monitor_printf(mon
, "invalid char in format: '%c'\n",
829 format
= default_fmt_format
;
832 /* for 'i', not specifying a size gives -1 as size */
834 size
= default_fmt_size
;
836 default_fmt_size
= size
;
838 default_fmt_format
= format
;
841 format
= default_fmt_format
;
843 size
= default_fmt_size
;
848 qdict_put_int(qdict
, "count", count
);
849 qdict_put_int(qdict
, "format", format
);
850 qdict_put_int(qdict
, "size", size
);
859 while (qemu_isspace(*p
)) {
862 if (*typestr
== '?' || *typestr
== '.') {
863 if (*typestr
== '?') {
871 while (qemu_isspace(*p
)) {
881 if (get_expr(mon
, &val
, &p
)) {
884 /* Check if 'i' is greater than 32-bit */
885 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
886 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
887 monitor_printf(mon
, "integer is for 32-bit values\n");
889 } else if (c
== 'M') {
891 monitor_printf(mon
, "enter a positive value\n");
896 qdict_put_int(qdict
, key
, val
);
905 while (qemu_isspace(*p
)) {
908 if (*typestr
== '?') {
914 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
915 if (ret
< 0 || val
> INT64_MAX
) {
916 monitor_printf(mon
, "invalid size\n");
919 qdict_put_int(qdict
, key
, val
);
927 while (qemu_isspace(*p
)) {
930 if (*typestr
== '?') {
936 if (get_double(mon
, &val
, &p
) < 0) {
939 if (p
[0] && p
[1] == 's') {
942 val
/= 1e3
; p
+= 2; break;
944 val
/= 1e6
; p
+= 2; break;
946 val
/= 1e9
; p
+= 2; break;
949 if (*p
&& !qemu_isspace(*p
)) {
950 monitor_printf(mon
, "Unknown unit suffix\n");
953 qdict_put(qdict
, key
, qnum_from_double(val
));
961 while (qemu_isspace(*p
)) {
965 while (qemu_isgraph(*p
)) {
968 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
970 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
973 monitor_printf(mon
, "Expected 'on' or 'off'\n");
976 qdict_put_bool(qdict
, key
, val
);
989 while (qemu_isspace(*p
)) {
995 if (!is_valid_option(p
, typestr
)) {
996 monitor_printf(mon
, "%s: unsupported option -%c\n",
1008 qdict_put_bool(qdict
, key
, true);
1015 /* package all remaining string */
1018 while (qemu_isspace(*p
)) {
1021 if (*typestr
== '?') {
1024 /* no remaining string: NULL argument */
1030 monitor_printf(mon
, "%s: string expected\n",
1034 qdict_put_str(qdict
, key
, p
);
1040 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
1046 /* check that all arguments were parsed */
1047 while (qemu_isspace(*p
)) {
1051 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
1059 qobject_unref(qdict
);
1064 typedef struct HandleHmpCommandCo
{
1066 const HMPCommand
*cmd
;
1069 } HandleHmpCommandCo
;
1071 static void handle_hmp_command_co(void *opaque
)
1073 HandleHmpCommandCo
*data
= opaque
;
1074 data
->cmd
->cmd(data
->mon
, data
->qdict
);
1075 monitor_set_cur(qemu_coroutine_self(), NULL
);
1079 void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
1082 const HMPCommand
*cmd
;
1083 const char *cmd_start
= cmdline
;
1085 trace_handle_hmp_command(mon
, cmdline
);
1087 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
1092 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
1094 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
1097 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
1098 (int)(cmdline
- cmd_start
), cmd_start
);
1102 if (!cmd
->coroutine
) {
1103 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1104 Monitor
*old_mon
= monitor_set_cur(qemu_coroutine_self(), &mon
->common
);
1105 cmd
->cmd(&mon
->common
, qdict
);
1106 monitor_set_cur(qemu_coroutine_self(), old_mon
);
1108 HandleHmpCommandCo data
= {
1109 .mon
= &mon
->common
,
1114 Coroutine
*co
= qemu_coroutine_create(handle_hmp_command_co
, &data
);
1115 monitor_set_cur(co
, &mon
->common
);
1116 aio_co_enter(qemu_get_aio_context(), co
);
1117 AIO_WAIT_WHILE(qemu_get_aio_context(), !data
.done
);
1120 qobject_unref(qdict
);
1123 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
1125 const char *p
, *pstart
;
1132 p
= qemu_strchrnul(p
, '|');
1134 if (len
> sizeof(cmd
) - 2) {
1135 len
= sizeof(cmd
) - 2;
1137 memcpy(cmd
, pstart
, len
);
1139 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1140 readline_add_completion(mon
->rs
, cmd
);
1149 static void file_completion(MonitorHMP
*mon
, const char *input
)
1154 char file
[1024], file_prefix
[1024];
1158 p
= strrchr(input
, '/');
1161 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1162 pstrcpy(path
, sizeof(path
), ".");
1164 input_path_len
= p
- input
+ 1;
1165 memcpy(path
, input
, input_path_len
);
1166 if (input_path_len
> sizeof(path
) - 1) {
1167 input_path_len
= sizeof(path
) - 1;
1169 path
[input_path_len
] = '\0';
1170 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1173 ffs
= opendir(path
);
1184 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
1188 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1189 memcpy(file
, input
, input_path_len
);
1190 if (input_path_len
< sizeof(file
)) {
1191 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
1195 * stat the file to find out if it's a directory.
1196 * In that case add a slash to speed up typing long paths
1198 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
1199 pstrcat(file
, sizeof(file
), "/");
1201 readline_add_completion(mon
->rs
, file
);
1207 static const char *next_arg_type(const char *typestr
)
1209 const char *p
= strchr(typestr
, ':');
1210 return (p
!= NULL
? ++p
: typestr
);
1213 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
1214 const HMPCommand
*cmd_table
,
1218 const char *cmdname
;
1220 const char *ptype
, *old_ptype
, *str
, *name
;
1221 const HMPCommand
*cmd
;
1222 BlockBackend
*blk
= NULL
;
1225 /* command completion */
1231 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
1232 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1233 if (cmd_available(cmd
)) {
1234 cmd_completion(mon
, cmdname
, cmd
->name
);
1238 /* find the command */
1239 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1240 if (hmp_compare_cmd(args
[0], cmd
->name
) &&
1241 cmd_available(cmd
)) {
1249 if (cmd
->sub_table
) {
1250 /* do the job again */
1251 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
1252 &args
[1], nb_args
- 1);
1255 if (cmd
->command_completion
) {
1256 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
1260 ptype
= next_arg_type(cmd
->args_type
);
1261 for (i
= 0; i
< nb_args
- 2; i
++) {
1262 if (*ptype
!= '\0') {
1263 ptype
= next_arg_type(ptype
);
1264 while (*ptype
== '?') {
1265 ptype
= next_arg_type(ptype
);
1269 str
= args
[nb_args
- 1];
1271 while (*ptype
== '-' && old_ptype
!= ptype
) {
1273 ptype
= next_arg_type(ptype
);
1277 /* file completion */
1278 readline_set_completion_index(mon
->rs
, strlen(str
));
1279 file_completion(mon
, str
);
1282 /* block device name completion */
1283 readline_set_completion_index(mon
->rs
, strlen(str
));
1284 while ((blk
= blk_next(blk
)) != NULL
) {
1285 name
= blk_name(blk
);
1286 if (str
[0] == '\0' ||
1287 !strncmp(name
, str
, strlen(str
))) {
1288 readline_add_completion(mon
->rs
, name
);
1294 if (!strcmp(cmd
->name
, "help|?")) {
1295 monitor_find_completion_by_table(mon
, cmd_table
,
1296 &args
[1], nb_args
- 1);
1305 static void monitor_find_completion(void *opaque
,
1306 const char *cmdline
)
1308 MonitorHMP
*mon
= opaque
;
1309 char *args
[MAX_ARGS
];
1312 /* 1. parse the cmdline */
1313 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
1318 * if the line ends with a space, it means we want to complete the
1321 len
= strlen(cmdline
);
1322 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
1323 if (nb_args
>= MAX_ARGS
) {
1326 args
[nb_args
++] = g_strdup("");
1329 /* 2. auto complete according to args */
1330 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
1333 free_cmdline_args(args
, nb_args
);
1336 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
1338 MonitorHMP
*mon
= container_of(opaque
, MonitorHMP
, common
);
1342 for (i
= 0; i
< size
; i
++) {
1343 readline_handle_byte(mon
->rs
, buf
[i
]);
1346 if (size
== 0 || buf
[size
- 1] != 0) {
1347 monitor_printf(&mon
->common
, "corrupted command\n");
1349 handle_hmp_command(mon
, (char *)buf
);
1354 static void monitor_event(void *opaque
, QEMUChrEvent event
)
1356 Monitor
*mon
= opaque
;
1357 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1360 case CHR_EVENT_MUX_IN
:
1361 qemu_mutex_lock(&mon
->mon_lock
);
1363 qemu_mutex_unlock(&mon
->mon_lock
);
1364 if (mon
->reset_seen
) {
1365 readline_restart(hmp_mon
->rs
);
1366 monitor_resume(mon
);
1369 qatomic_mb_set(&mon
->suspend_cnt
, 0);
1373 case CHR_EVENT_MUX_OUT
:
1374 if (mon
->reset_seen
) {
1375 if (qatomic_mb_read(&mon
->suspend_cnt
) == 0) {
1376 monitor_printf(mon
, "\n");
1379 monitor_suspend(mon
);
1381 qatomic_inc(&mon
->suspend_cnt
);
1383 qemu_mutex_lock(&mon
->mon_lock
);
1385 qemu_mutex_unlock(&mon
->mon_lock
);
1388 case CHR_EVENT_OPENED
:
1389 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
1390 "information\n", QEMU_VERSION
);
1391 if (!mon
->mux_out
) {
1392 readline_restart(hmp_mon
->rs
);
1393 readline_show_prompt(hmp_mon
->rs
);
1395 mon
->reset_seen
= 1;
1399 case CHR_EVENT_CLOSED
:
1401 monitor_fdsets_cleanup();
1404 case CHR_EVENT_BREAK
:
1412 * These functions just adapt the readline interface in a typesafe way. We
1413 * could cast function pointers but that discards compiler checks.
1415 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
1416 const char *fmt
, ...)
1418 MonitorHMP
*mon
= opaque
;
1421 monitor_vprintf(&mon
->common
, fmt
, ap
);
1425 static void monitor_readline_flush(void *opaque
)
1427 MonitorHMP
*mon
= opaque
;
1428 monitor_flush(&mon
->common
);
1431 void monitor_init_hmp(Chardev
*chr
, bool use_readline
, Error
**errp
)
1433 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
1435 if (!qemu_chr_fe_init(&mon
->common
.chr
, chr
, errp
)) {
1440 monitor_data_init(&mon
->common
, false, false, false);
1442 mon
->use_readline
= use_readline
;
1443 if (mon
->use_readline
) {
1444 mon
->rs
= readline_init(monitor_readline_printf
,
1445 monitor_readline_flush
,
1447 monitor_find_completion
);
1448 monitor_read_command(mon
, 0);
1451 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
1452 monitor_event
, NULL
, &mon
->common
, NULL
, true);
1453 monitor_list_append(&mon
->common
);