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 void help_cmd_dump_one(Monitor
*mon
,
217 const HMPCommand
*cmd
,
223 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
227 for (i
= 0; i
< prefix_args_nb
; i
++) {
228 monitor_printf(mon
, "%s ", prefix_args
[i
]);
230 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
233 /* @args[@arg_index] is the valid command need to find in @cmds */
234 static void help_cmd_dump(Monitor
*mon
, const HMPCommand
*cmds
,
235 char **args
, int nb_args
, int arg_index
)
237 const HMPCommand
*cmd
;
240 /* No valid arg need to compare with, dump all in *cmds */
241 if (arg_index
>= nb_args
) {
242 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
243 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
248 /* Find one entry to dump */
249 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
250 if (hmp_compare_cmd(args
[arg_index
], cmd
->name
) &&
251 ((!runstate_check(RUN_STATE_PRECONFIG
) ||
252 cmd_can_preconfig(cmd
)))) {
253 if (cmd
->sub_table
) {
254 /* continue with next arg */
255 help_cmd_dump(mon
, cmd
->sub_table
,
256 args
, nb_args
, arg_index
+ 1);
258 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
264 /* Command not found */
265 monitor_printf(mon
, "unknown command: '");
266 for (i
= 0; i
<= arg_index
; i
++) {
267 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
271 void help_cmd(Monitor
*mon
, const char *name
)
273 char *args
[MAX_ARGS
];
276 /* 1. parse user input */
278 /* special case for log, directly dump and return */
279 if (!strcmp(name
, "log")) {
280 const QEMULogItem
*item
;
281 monitor_printf(mon
, "Log items (comma separated):\n");
282 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
283 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
284 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
289 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
294 /* 2. dump the contents according to parsed args */
295 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
297 free_cmdline_args(args
, nb_args
);
300 /*******************************************************************/
302 static const char *pch
;
303 static sigjmp_buf expr_env
;
305 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
306 expr_error(Monitor
*mon
, const char *fmt
, ...)
310 monitor_vprintf(mon
, fmt
, ap
);
311 monitor_printf(mon
, "\n");
313 siglongjmp(expr_env
, 1);
316 static void next(void)
320 while (qemu_isspace(*pch
)) {
326 static int64_t expr_sum(Monitor
*mon
);
328 static int64_t expr_unary(Monitor
*mon
)
341 n
= -expr_unary(mon
);
345 n
= ~expr_unary(mon
);
351 expr_error(mon
, "')' expected");
358 expr_error(mon
, "character constant expected");
363 expr_error(mon
, "missing terminating \' character");
374 while ((*pch
>= 'a' && *pch
<= 'z') ||
375 (*pch
>= 'A' && *pch
<= 'Z') ||
376 (*pch
>= '0' && *pch
<= '9') ||
377 *pch
== '_' || *pch
== '.') {
378 if ((q
- buf
) < sizeof(buf
) - 1) {
383 while (qemu_isspace(*pch
)) {
387 ret
= get_monitor_def(®
, buf
);
389 expr_error(mon
, "unknown register");
395 expr_error(mon
, "unexpected end of expression");
400 n
= strtoull(pch
, &p
, 0);
401 if (errno
== ERANGE
) {
402 expr_error(mon
, "number too large");
405 expr_error(mon
, "invalid char '%c' in expression", *p
);
408 while (qemu_isspace(*pch
)) {
416 static int64_t expr_prod(Monitor
*mon
)
421 val
= expr_unary(mon
);
424 if (op
!= '*' && op
!= '/' && op
!= '%') {
428 val2
= expr_unary(mon
);
437 expr_error(mon
, "division by zero");
450 static int64_t expr_logic(Monitor
*mon
)
455 val
= expr_prod(mon
);
458 if (op
!= '&' && op
!= '|' && op
!= '^') {
462 val2
= expr_prod(mon
);
479 static int64_t expr_sum(Monitor
*mon
)
484 val
= expr_logic(mon
);
487 if (op
!= '+' && op
!= '-') {
491 val2
= expr_logic(mon
);
501 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
504 if (sigsetjmp(expr_env
, 0)) {
508 while (qemu_isspace(*pch
)) {
511 *pval
= expr_sum(mon
);
516 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
522 d
= strtod(p
, &tailp
);
524 monitor_printf(mon
, "Number expected\n");
527 if (d
!= d
|| d
- d
!= 0) {
528 /* NaN or infinity */
529 monitor_printf(mon
, "Bad number\n");
538 * Store the command-name in cmdname, and return a pointer to
539 * the remaining of the command string.
541 static const char *get_command_name(const char *cmdline
,
542 char *cmdname
, size_t nlen
)
545 const char *p
, *pstart
;
548 while (qemu_isspace(*p
)) {
555 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
)) {
559 if (len
> nlen
- 1) {
562 memcpy(cmdname
, pstart
, len
);
568 * Read key of 'type' into 'key' and return the current
571 static char *key_get_info(const char *type
, char **key
)
580 p
= strchr(type
, ':');
587 str
= g_malloc(len
+ 1);
588 memcpy(str
, type
, len
);
595 static int default_fmt_format
= 'x';
596 static int default_fmt_size
= 4;
598 static int is_valid_option(const char *c
, const char *typestr
)
606 typestr
= strstr(typestr
, option
);
607 return (typestr
!= NULL
);
610 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
613 const HMPCommand
*cmd
;
615 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
616 if (hmp_compare_cmd(cmdname
, cmd
->name
)) {
625 * Parse command name from @cmdp according to command table @table.
626 * If blank, return NULL.
627 * Else, if no valid command can be found, report to @mon, and return
629 * Else, change @cmdp to point right behind the name, and return its
630 * command table entry.
631 * Do not assume the return value points into @table! It doesn't when
632 * the command is found in a sub-command table.
634 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
635 const char *cmdp_start
,
639 Monitor
*mon
= &hmp_mon
->common
;
641 const HMPCommand
*cmd
;
644 /* extract the command name */
645 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
650 cmd
= search_dispatch_table(table
, cmdname
);
652 monitor_printf(mon
, "unknown command: '%.*s'\n",
653 (int)(p
- cmdp_start
), cmdp_start
);
656 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
657 monitor_printf(mon
, "Command '%.*s' not available with -preconfig "
658 "until after exit_preconfig.\n",
659 (int)(p
- cmdp_start
), cmdp_start
);
663 /* filter out following useless space */
664 while (qemu_isspace(*p
)) {
669 /* search sub command */
670 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
671 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
678 * Parse arguments for @cmd.
679 * If it can't be parsed, report to @mon, and return NULL.
680 * Else, insert command arguments into a QDict, and return it.
681 * Note: On success, caller has to free the QDict structure.
683 static QDict
*monitor_parse_arguments(Monitor
*mon
,
685 const HMPCommand
*cmd
)
690 const char *p
= *endp
;
692 QDict
*qdict
= qdict_new();
694 /* parse the parameters */
695 typestr
= cmd
->args_type
;
697 typestr
= key_get_info(typestr
, &key
);
710 while (qemu_isspace(*p
)) {
713 if (*typestr
== '?') {
716 /* no optional string: NULL argument */
720 ret
= get_str(buf
, sizeof(buf
), &p
);
724 monitor_printf(mon
, "%s: filename expected\n",
728 monitor_printf(mon
, "%s: block device name expected\n",
732 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
737 qdict_put_str(qdict
, key
, buf
);
742 QemuOptsList
*opts_list
;
745 opts_list
= qemu_find_opts(key
);
746 if (!opts_list
|| opts_list
->desc
->name
) {
749 while (qemu_isspace(*p
)) {
755 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
758 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
762 qemu_opts_to_qdict(opts
, qdict
);
768 int count
, format
, size
;
770 while (qemu_isspace(*p
)) {
777 if (qemu_isdigit(*p
)) {
779 while (qemu_isdigit(*p
)) {
780 count
= count
* 10 + (*p
- '0');
818 if (*p
!= '\0' && !qemu_isspace(*p
)) {
819 monitor_printf(mon
, "invalid char in format: '%c'\n",
824 format
= default_fmt_format
;
827 /* for 'i', not specifying a size gives -1 as size */
829 size
= default_fmt_size
;
831 default_fmt_size
= size
;
833 default_fmt_format
= format
;
836 format
= default_fmt_format
;
838 size
= default_fmt_size
;
843 qdict_put_int(qdict
, "count", count
);
844 qdict_put_int(qdict
, "format", format
);
845 qdict_put_int(qdict
, "size", size
);
854 while (qemu_isspace(*p
)) {
857 if (*typestr
== '?' || *typestr
== '.') {
858 if (*typestr
== '?') {
866 while (qemu_isspace(*p
)) {
876 if (get_expr(mon
, &val
, &p
)) {
879 /* Check if 'i' is greater than 32-bit */
880 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
881 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
882 monitor_printf(mon
, "integer is for 32-bit values\n");
884 } else if (c
== 'M') {
886 monitor_printf(mon
, "enter a positive value\n");
891 qdict_put_int(qdict
, key
, val
);
900 while (qemu_isspace(*p
)) {
903 if (*typestr
== '?') {
909 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
910 if (ret
< 0 || val
> INT64_MAX
) {
911 monitor_printf(mon
, "invalid size\n");
914 qdict_put_int(qdict
, key
, val
);
922 while (qemu_isspace(*p
)) {
925 if (*typestr
== '?') {
931 if (get_double(mon
, &val
, &p
) < 0) {
934 if (p
[0] && p
[1] == 's') {
937 val
/= 1e3
; p
+= 2; break;
939 val
/= 1e6
; p
+= 2; break;
941 val
/= 1e9
; p
+= 2; break;
944 if (*p
&& !qemu_isspace(*p
)) {
945 monitor_printf(mon
, "Unknown unit suffix\n");
948 qdict_put(qdict
, key
, qnum_from_double(val
));
956 while (qemu_isspace(*p
)) {
960 while (qemu_isgraph(*p
)) {
963 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
965 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
968 monitor_printf(mon
, "Expected 'on' or 'off'\n");
971 qdict_put_bool(qdict
, key
, val
);
984 while (qemu_isspace(*p
)) {
990 if (!is_valid_option(p
, typestr
)) {
991 monitor_printf(mon
, "%s: unsupported option -%c\n",
1003 qdict_put_bool(qdict
, key
, true);
1010 /* package all remaining string */
1013 while (qemu_isspace(*p
)) {
1016 if (*typestr
== '?') {
1019 /* no remaining string: NULL argument */
1025 monitor_printf(mon
, "%s: string expected\n",
1029 qdict_put_str(qdict
, key
, p
);
1035 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
1041 /* check that all arguments were parsed */
1042 while (qemu_isspace(*p
)) {
1046 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
1054 qobject_unref(qdict
);
1059 typedef struct HandleHmpCommandCo
{
1061 const HMPCommand
*cmd
;
1064 } HandleHmpCommandCo
;
1066 static void handle_hmp_command_co(void *opaque
)
1068 HandleHmpCommandCo
*data
= opaque
;
1069 data
->cmd
->cmd(data
->mon
, data
->qdict
);
1070 monitor_set_cur(qemu_coroutine_self(), NULL
);
1074 void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
1077 const HMPCommand
*cmd
;
1078 const char *cmd_start
= cmdline
;
1080 trace_handle_hmp_command(mon
, cmdline
);
1082 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
1087 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
1089 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
1092 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
1093 (int)(cmdline
- cmd_start
), cmd_start
);
1097 if (!cmd
->coroutine
) {
1098 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1099 Monitor
*old_mon
= monitor_set_cur(qemu_coroutine_self(), &mon
->common
);
1100 cmd
->cmd(&mon
->common
, qdict
);
1101 monitor_set_cur(qemu_coroutine_self(), old_mon
);
1103 HandleHmpCommandCo data
= {
1104 .mon
= &mon
->common
,
1109 Coroutine
*co
= qemu_coroutine_create(handle_hmp_command_co
, &data
);
1110 monitor_set_cur(co
, &mon
->common
);
1111 aio_co_enter(qemu_get_aio_context(), co
);
1112 AIO_WAIT_WHILE(qemu_get_aio_context(), !data
.done
);
1115 qobject_unref(qdict
);
1118 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
1120 const char *p
, *pstart
;
1127 p
= qemu_strchrnul(p
, '|');
1129 if (len
> sizeof(cmd
) - 2) {
1130 len
= sizeof(cmd
) - 2;
1132 memcpy(cmd
, pstart
, len
);
1134 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1135 readline_add_completion(mon
->rs
, cmd
);
1144 static void file_completion(MonitorHMP
*mon
, const char *input
)
1149 char file
[1024], file_prefix
[1024];
1153 p
= strrchr(input
, '/');
1156 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1157 pstrcpy(path
, sizeof(path
), ".");
1159 input_path_len
= p
- input
+ 1;
1160 memcpy(path
, input
, input_path_len
);
1161 if (input_path_len
> sizeof(path
) - 1) {
1162 input_path_len
= sizeof(path
) - 1;
1164 path
[input_path_len
] = '\0';
1165 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1168 ffs
= opendir(path
);
1179 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
1183 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1184 memcpy(file
, input
, input_path_len
);
1185 if (input_path_len
< sizeof(file
)) {
1186 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
1190 * stat the file to find out if it's a directory.
1191 * In that case add a slash to speed up typing long paths
1193 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
1194 pstrcat(file
, sizeof(file
), "/");
1196 readline_add_completion(mon
->rs
, file
);
1202 static const char *next_arg_type(const char *typestr
)
1204 const char *p
= strchr(typestr
, ':');
1205 return (p
!= NULL
? ++p
: typestr
);
1208 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
1209 const HMPCommand
*cmd_table
,
1213 const char *cmdname
;
1215 const char *ptype
, *old_ptype
, *str
, *name
;
1216 const HMPCommand
*cmd
;
1217 BlockBackend
*blk
= NULL
;
1220 /* command completion */
1226 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
1227 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1228 if (!runstate_check(RUN_STATE_PRECONFIG
) ||
1229 cmd_can_preconfig(cmd
)) {
1230 cmd_completion(mon
, cmdname
, cmd
->name
);
1234 /* find the command */
1235 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1236 if (hmp_compare_cmd(args
[0], cmd
->name
) &&
1237 (!runstate_check(RUN_STATE_PRECONFIG
) ||
1238 cmd_can_preconfig(cmd
))) {
1246 if (cmd
->sub_table
) {
1247 /* do the job again */
1248 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
1249 &args
[1], nb_args
- 1);
1252 if (cmd
->command_completion
) {
1253 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
1257 ptype
= next_arg_type(cmd
->args_type
);
1258 for (i
= 0; i
< nb_args
- 2; i
++) {
1259 if (*ptype
!= '\0') {
1260 ptype
= next_arg_type(ptype
);
1261 while (*ptype
== '?') {
1262 ptype
= next_arg_type(ptype
);
1266 str
= args
[nb_args
- 1];
1268 while (*ptype
== '-' && old_ptype
!= ptype
) {
1270 ptype
= next_arg_type(ptype
);
1274 /* file completion */
1275 readline_set_completion_index(mon
->rs
, strlen(str
));
1276 file_completion(mon
, str
);
1279 /* block device name completion */
1280 readline_set_completion_index(mon
->rs
, strlen(str
));
1281 while ((blk
= blk_next(blk
)) != NULL
) {
1282 name
= blk_name(blk
);
1283 if (str
[0] == '\0' ||
1284 !strncmp(name
, str
, strlen(str
))) {
1285 readline_add_completion(mon
->rs
, name
);
1291 if (!strcmp(cmd
->name
, "help|?")) {
1292 monitor_find_completion_by_table(mon
, cmd_table
,
1293 &args
[1], nb_args
- 1);
1302 static void monitor_find_completion(void *opaque
,
1303 const char *cmdline
)
1305 MonitorHMP
*mon
= opaque
;
1306 char *args
[MAX_ARGS
];
1309 /* 1. parse the cmdline */
1310 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
1315 * if the line ends with a space, it means we want to complete the
1318 len
= strlen(cmdline
);
1319 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
1320 if (nb_args
>= MAX_ARGS
) {
1323 args
[nb_args
++] = g_strdup("");
1326 /* 2. auto complete according to args */
1327 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
1330 free_cmdline_args(args
, nb_args
);
1333 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
1335 MonitorHMP
*mon
= container_of(opaque
, MonitorHMP
, common
);
1339 for (i
= 0; i
< size
; i
++) {
1340 readline_handle_byte(mon
->rs
, buf
[i
]);
1343 if (size
== 0 || buf
[size
- 1] != 0) {
1344 monitor_printf(&mon
->common
, "corrupted command\n");
1346 handle_hmp_command(mon
, (char *)buf
);
1351 static void monitor_event(void *opaque
, QEMUChrEvent event
)
1353 Monitor
*mon
= opaque
;
1354 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1357 case CHR_EVENT_MUX_IN
:
1358 qemu_mutex_lock(&mon
->mon_lock
);
1360 qemu_mutex_unlock(&mon
->mon_lock
);
1361 if (mon
->reset_seen
) {
1362 readline_restart(hmp_mon
->rs
);
1363 monitor_resume(mon
);
1366 qatomic_mb_set(&mon
->suspend_cnt
, 0);
1370 case CHR_EVENT_MUX_OUT
:
1371 if (mon
->reset_seen
) {
1372 if (qatomic_mb_read(&mon
->suspend_cnt
) == 0) {
1373 monitor_printf(mon
, "\n");
1376 monitor_suspend(mon
);
1378 qatomic_inc(&mon
->suspend_cnt
);
1380 qemu_mutex_lock(&mon
->mon_lock
);
1382 qemu_mutex_unlock(&mon
->mon_lock
);
1385 case CHR_EVENT_OPENED
:
1386 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
1387 "information\n", QEMU_VERSION
);
1388 if (!mon
->mux_out
) {
1389 readline_restart(hmp_mon
->rs
);
1390 readline_show_prompt(hmp_mon
->rs
);
1392 mon
->reset_seen
= 1;
1396 case CHR_EVENT_CLOSED
:
1398 monitor_fdsets_cleanup();
1401 case CHR_EVENT_BREAK
:
1409 * These functions just adapt the readline interface in a typesafe way. We
1410 * could cast function pointers but that discards compiler checks.
1412 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
1413 const char *fmt
, ...)
1415 MonitorHMP
*mon
= opaque
;
1418 monitor_vprintf(&mon
->common
, fmt
, ap
);
1422 static void monitor_readline_flush(void *opaque
)
1424 MonitorHMP
*mon
= opaque
;
1425 monitor_flush(&mon
->common
);
1428 void monitor_init_hmp(Chardev
*chr
, bool use_readline
, Error
**errp
)
1430 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
1432 if (!qemu_chr_fe_init(&mon
->common
.chr
, chr
, errp
)) {
1437 monitor_data_init(&mon
->common
, false, false, false);
1439 mon
->use_readline
= use_readline
;
1440 if (mon
->use_readline
) {
1441 mon
->rs
= readline_init(monitor_readline_printf
,
1442 monitor_readline_flush
,
1444 monitor_find_completion
);
1445 monitor_read_command(mon
, 0);
1448 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
1449 monitor_event
, NULL
, &mon
->common
, NULL
, true);
1450 monitor_list_append(&mon
->common
);