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 "monitor/hmp.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"
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 phase_check(PHASE_MACHINE_READY
) || 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 hmp_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
, "%-15s %s\n", "none", "remove all logs");
287 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
288 monitor_printf(mon
, "%-15s %s\n", item
->name
, item
->help
);
290 #ifdef CONFIG_TRACE_LOG
291 monitor_printf(mon
, "trace:PATTERN enable trace events\n");
292 monitor_printf(mon
, "\nUse \"log trace:help\" to get a list of "
293 "trace events.\n\n");
298 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
303 /* 2. dump the contents according to parsed args */
304 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
306 free_cmdline_args(args
, nb_args
);
309 /*******************************************************************/
311 static const char *pch
;
312 static sigjmp_buf expr_env
;
314 static G_NORETURN
G_GNUC_PRINTF(2, 3)
315 void expr_error(Monitor
*mon
, const char *fmt
, ...)
319 monitor_vprintf(mon
, fmt
, ap
);
320 monitor_printf(mon
, "\n");
322 siglongjmp(expr_env
, 1);
325 static void next(void)
329 while (qemu_isspace(*pch
)) {
335 static int64_t expr_sum(Monitor
*mon
);
337 static int64_t expr_unary(Monitor
*mon
)
350 n
= -expr_unary(mon
);
354 n
= ~expr_unary(mon
);
360 expr_error(mon
, "')' expected");
367 expr_error(mon
, "character constant expected");
372 expr_error(mon
, "missing terminating \' character");
383 while ((*pch
>= 'a' && *pch
<= 'z') ||
384 (*pch
>= 'A' && *pch
<= 'Z') ||
385 (*pch
>= '0' && *pch
<= '9') ||
386 *pch
== '_' || *pch
== '.') {
387 if ((q
- buf
) < sizeof(buf
) - 1) {
392 while (qemu_isspace(*pch
)) {
396 ret
= get_monitor_def(mon
, ®
, buf
);
398 expr_error(mon
, "unknown register");
404 expr_error(mon
, "unexpected end of expression");
409 n
= strtoull(pch
, &p
, 0);
410 if (errno
== ERANGE
) {
411 expr_error(mon
, "number too large");
414 expr_error(mon
, "invalid char '%c' in expression", *p
);
417 while (qemu_isspace(*pch
)) {
425 static int64_t expr_prod(Monitor
*mon
)
430 val
= expr_unary(mon
);
433 if (op
!= '*' && op
!= '/' && op
!= '%') {
437 val2
= expr_unary(mon
);
446 expr_error(mon
, "division by zero");
459 static int64_t expr_logic(Monitor
*mon
)
464 val
= expr_prod(mon
);
467 if (op
!= '&' && op
!= '|' && op
!= '^') {
471 val2
= expr_prod(mon
);
488 static int64_t expr_sum(Monitor
*mon
)
493 val
= expr_logic(mon
);
496 if (op
!= '+' && op
!= '-') {
500 val2
= expr_logic(mon
);
510 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
513 if (sigsetjmp(expr_env
, 0)) {
517 while (qemu_isspace(*pch
)) {
520 *pval
= expr_sum(mon
);
525 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
531 d
= strtod(p
, &tailp
);
533 monitor_printf(mon
, "Number expected\n");
536 if (d
!= d
|| d
- d
!= 0) {
537 /* NaN or infinity */
538 monitor_printf(mon
, "Bad number\n");
547 * Store the command-name in cmdname, and return a pointer to
548 * the remaining of the command string.
550 static const char *get_command_name(const char *cmdline
,
551 char *cmdname
, size_t nlen
)
554 const char *p
, *pstart
;
557 while (qemu_isspace(*p
)) {
564 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
)) {
568 if (len
> nlen
- 1) {
571 memcpy(cmdname
, pstart
, len
);
577 * Read key of 'type' into 'key' and return the current
580 static char *key_get_info(const char *type
, char **key
)
589 p
= strchr(type
, ':');
596 str
= g_malloc(len
+ 1);
597 memcpy(str
, type
, len
);
604 static int default_fmt_format
= 'x';
605 static int default_fmt_size
= 4;
607 static int is_valid_option(const char *c
, const char *typestr
)
615 typestr
= strstr(typestr
, option
);
616 return (typestr
!= NULL
);
619 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
622 const HMPCommand
*cmd
;
624 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
625 if (hmp_compare_cmd(cmdname
, cmd
->name
)) {
634 * Parse command name from @cmdp according to command table @table.
635 * If blank, return NULL.
636 * Else, if no valid command can be found, report to @mon, and return
638 * Else, change @cmdp to point right behind the name, and return its
639 * command table entry.
640 * Do not assume the return value points into @table! It doesn't when
641 * the command is found in a sub-command table.
643 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
644 const char *cmdp_start
,
648 Monitor
*mon
= &hmp_mon
->common
;
650 const HMPCommand
*cmd
;
653 /* extract the command name */
654 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
659 cmd
= search_dispatch_table(table
, cmdname
);
661 monitor_printf(mon
, "unknown command: '%.*s'\n",
662 (int)(p
- cmdp_start
), cmdp_start
);
665 if (!cmd_available(cmd
)) {
666 monitor_printf(mon
, "Command '%.*s' not available "
667 "until machine initialization has completed.\n",
668 (int)(p
- cmdp_start
), cmdp_start
);
672 /* filter out following useless space */
673 while (qemu_isspace(*p
)) {
678 /* search sub command */
679 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
680 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
687 * Parse arguments for @cmd.
688 * If it can't be parsed, report to @mon, and return NULL.
689 * Else, insert command arguments into a QDict, and return it.
690 * Note: On success, caller has to free the QDict structure.
692 static QDict
*monitor_parse_arguments(Monitor
*mon
,
694 const HMPCommand
*cmd
)
699 const char *p
= *endp
;
701 QDict
*qdict
= qdict_new();
703 /* parse the parameters */
704 typestr
= cmd
->args_type
;
706 typestr
= key_get_info(typestr
, &key
);
719 while (qemu_isspace(*p
)) {
722 if (*typestr
== '?') {
725 /* no optional string: NULL argument */
729 ret
= get_str(buf
, sizeof(buf
), &p
);
733 monitor_printf(mon
, "%s: filename expected\n",
737 monitor_printf(mon
, "%s: block device name expected\n",
741 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
746 qdict_put_str(qdict
, key
, buf
);
751 QemuOptsList
*opts_list
;
754 opts_list
= qemu_find_opts(key
);
755 if (!opts_list
|| opts_list
->desc
->name
) {
758 while (qemu_isspace(*p
)) {
764 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
767 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
771 qemu_opts_to_qdict(opts
, qdict
);
777 int count
, format
, size
;
779 while (qemu_isspace(*p
)) {
786 if (qemu_isdigit(*p
)) {
788 while (qemu_isdigit(*p
)) {
789 count
= count
* 10 + (*p
- '0');
827 if (*p
!= '\0' && !qemu_isspace(*p
)) {
828 monitor_printf(mon
, "invalid char in format: '%c'\n",
833 format
= default_fmt_format
;
836 /* for 'i', not specifying a size gives -1 as size */
838 size
= default_fmt_size
;
840 default_fmt_size
= size
;
842 default_fmt_format
= format
;
845 format
= default_fmt_format
;
847 size
= default_fmt_size
;
852 qdict_put_int(qdict
, "count", count
);
853 qdict_put_int(qdict
, "format", format
);
854 qdict_put_int(qdict
, "size", size
);
863 while (qemu_isspace(*p
)) {
866 if (*typestr
== '?' || *typestr
== '.') {
867 if (*typestr
== '?') {
875 while (qemu_isspace(*p
)) {
885 if (get_expr(mon
, &val
, &p
)) {
888 /* Check if 'i' is greater than 32-bit */
889 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
890 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
891 monitor_printf(mon
, "integer is for 32-bit values\n");
893 } else if (c
== 'M') {
895 monitor_printf(mon
, "enter a positive value\n");
900 qdict_put_int(qdict
, key
, val
);
909 while (qemu_isspace(*p
)) {
912 if (*typestr
== '?') {
918 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
919 if (ret
< 0 || val
> INT64_MAX
) {
920 monitor_printf(mon
, "invalid size\n");
923 qdict_put_int(qdict
, key
, val
);
931 while (qemu_isspace(*p
)) {
934 if (*typestr
== '?') {
940 if (get_double(mon
, &val
, &p
) < 0) {
943 if (p
[0] && p
[1] == 's') {
946 val
/= 1e3
; p
+= 2; break;
948 val
/= 1e6
; p
+= 2; break;
950 val
/= 1e9
; p
+= 2; break;
953 if (*p
&& !qemu_isspace(*p
)) {
954 monitor_printf(mon
, "Unknown unit suffix\n");
957 qdict_put(qdict
, key
, qnum_from_double(val
));
965 while (qemu_isspace(*p
)) {
969 while (qemu_isgraph(*p
)) {
972 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
974 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
977 monitor_printf(mon
, "Expected 'on' or 'off'\n");
980 qdict_put_bool(qdict
, key
, val
);
994 while (qemu_isspace(*p
)) {
1000 if (!is_valid_option(p
, typestr
)) {
1001 monitor_printf(mon
, "%s: unsupported option -%c\n",
1010 } else if (*typestr
== 's') {
1011 /* has option with string value */
1014 while (qemu_isspace(*p
)) {
1017 ret
= get_str(buf
, sizeof(buf
), &p
);
1019 monitor_printf(mon
, "%s: value expected for -%c\n",
1023 qdict_put_str(qdict
, key
, buf
);
1025 /* has boolean option */
1027 qdict_put_bool(qdict
, key
, true);
1029 } else if (*typestr
== 's') {
1036 /* package all remaining string */
1039 while (qemu_isspace(*p
)) {
1042 if (*typestr
== '?') {
1045 /* no remaining string: NULL argument */
1051 monitor_printf(mon
, "%s: string expected\n",
1055 qdict_put_str(qdict
, key
, p
);
1061 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
1067 /* check that all arguments were parsed */
1068 while (qemu_isspace(*p
)) {
1072 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
1080 qobject_unref(qdict
);
1085 static void hmp_info_human_readable_text(Monitor
*mon
,
1086 HumanReadableText
*(*handler
)(Error
**))
1089 g_autoptr(HumanReadableText
) info
= handler(&err
);
1091 if (hmp_handle_error(mon
, err
)) {
1095 monitor_puts(mon
, info
->human_readable_text
);
1098 static void handle_hmp_command_exec(Monitor
*mon
,
1099 const HMPCommand
*cmd
,
1102 if (cmd
->cmd_info_hrt
) {
1103 hmp_info_human_readable_text(mon
,
1106 cmd
->cmd(mon
, qdict
);
1110 typedef struct HandleHmpCommandCo
{
1112 const HMPCommand
*cmd
;
1115 } HandleHmpCommandCo
;
1117 static void handle_hmp_command_co(void *opaque
)
1119 HandleHmpCommandCo
*data
= opaque
;
1120 handle_hmp_command_exec(data
->mon
, data
->cmd
, data
->qdict
);
1121 monitor_set_cur(qemu_coroutine_self(), NULL
);
1125 void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
1128 const HMPCommand
*cmd
;
1129 const char *cmd_start
= cmdline
;
1131 trace_handle_hmp_command(mon
, cmdline
);
1133 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
1138 if (!cmd
->cmd
&& !cmd
->cmd_info_hrt
) {
1139 /* FIXME: is it useful to try autoload modules here ??? */
1140 monitor_printf(&mon
->common
, "Command \"%.*s\" is not available.\n",
1141 (int)(cmdline
- cmd_start
), cmd_start
);
1145 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
1147 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
1150 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
1151 (int)(cmdline
- cmd_start
), cmd_start
);
1155 if (!cmd
->coroutine
) {
1156 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1157 Monitor
*old_mon
= monitor_set_cur(qemu_coroutine_self(), &mon
->common
);
1158 handle_hmp_command_exec(&mon
->common
, cmd
, qdict
);
1159 monitor_set_cur(qemu_coroutine_self(), old_mon
);
1161 HandleHmpCommandCo data
= {
1162 .mon
= &mon
->common
,
1167 Coroutine
*co
= qemu_coroutine_create(handle_hmp_command_co
, &data
);
1168 monitor_set_cur(co
, &mon
->common
);
1169 aio_co_enter(qemu_get_aio_context(), co
);
1170 AIO_WAIT_WHILE_UNLOCKED(NULL
, !data
.done
);
1173 qobject_unref(qdict
);
1176 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
1178 const char *p
, *pstart
;
1185 p
= qemu_strchrnul(p
, '|');
1187 if (len
> sizeof(cmd
) - 2) {
1188 len
= sizeof(cmd
) - 2;
1190 memcpy(cmd
, pstart
, len
);
1192 readline_add_completion_of(mon
->rs
, name
, cmd
);
1200 static void file_completion(MonitorHMP
*mon
, const char *input
)
1205 char file
[1024], file_prefix
[1024];
1209 p
= strrchr(input
, '/');
1212 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1213 pstrcpy(path
, sizeof(path
), ".");
1215 input_path_len
= p
- input
+ 1;
1216 memcpy(path
, input
, input_path_len
);
1217 if (input_path_len
> sizeof(path
) - 1) {
1218 input_path_len
= sizeof(path
) - 1;
1220 path
[input_path_len
] = '\0';
1221 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1224 ffs
= opendir(path
);
1235 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
1239 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1240 memcpy(file
, input
, input_path_len
);
1241 if (input_path_len
< sizeof(file
)) {
1242 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
1246 * stat the file to find out if it's a directory.
1247 * In that case add a slash to speed up typing long paths
1249 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
1250 pstrcat(file
, sizeof(file
), "/");
1252 readline_add_completion(mon
->rs
, file
);
1258 static const char *next_arg_type(const char *typestr
)
1260 const char *p
= strchr(typestr
, ':');
1261 return (p
!= NULL
? ++p
: typestr
);
1264 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
1265 const HMPCommand
*cmd_table
,
1269 const char *cmdname
;
1271 const char *ptype
, *old_ptype
, *str
;
1272 const HMPCommand
*cmd
;
1273 BlockBackend
*blk
= NULL
;
1276 /* command completion */
1282 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
1283 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1284 if (cmd_available(cmd
)) {
1285 cmd_completion(mon
, cmdname
, cmd
->name
);
1289 /* find the command */
1290 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1291 if (hmp_compare_cmd(args
[0], cmd
->name
) &&
1292 cmd_available(cmd
)) {
1300 if (cmd
->sub_table
) {
1301 /* do the job again */
1302 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
1303 &args
[1], nb_args
- 1);
1306 if (cmd
->command_completion
) {
1307 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
1311 ptype
= next_arg_type(cmd
->args_type
);
1312 for (i
= 0; i
< nb_args
- 2; i
++) {
1313 if (*ptype
!= '\0') {
1314 ptype
= next_arg_type(ptype
);
1315 while (*ptype
== '?') {
1316 ptype
= next_arg_type(ptype
);
1320 str
= args
[nb_args
- 1];
1322 while (*ptype
== '-' && old_ptype
!= ptype
) {
1324 ptype
= next_arg_type(ptype
);
1328 /* file completion */
1329 readline_set_completion_index(mon
->rs
, strlen(str
));
1330 file_completion(mon
, str
);
1333 /* block device name completion */
1334 readline_set_completion_index(mon
->rs
, strlen(str
));
1335 while ((blk
= blk_next(blk
)) != NULL
) {
1336 readline_add_completion_of(mon
->rs
, str
, blk_name(blk
));
1341 if (!strcmp(cmd
->name
, "help|?")) {
1342 monitor_find_completion_by_table(mon
, cmd_table
,
1343 &args
[1], nb_args
- 1);
1352 static void monitor_find_completion(void *opaque
,
1353 const char *cmdline
)
1355 MonitorHMP
*mon
= opaque
;
1356 char *args
[MAX_ARGS
];
1359 /* 1. parse the cmdline */
1360 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
1365 * if the line ends with a space, it means we want to complete the
1368 len
= strlen(cmdline
);
1369 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
1370 if (nb_args
>= MAX_ARGS
) {
1373 args
[nb_args
++] = g_strdup("");
1376 /* 2. auto complete according to args */
1377 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
1380 free_cmdline_args(args
, nb_args
);
1383 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
1385 MonitorHMP
*mon
= container_of(opaque
, MonitorHMP
, common
);
1389 for (i
= 0; i
< size
; i
++) {
1390 readline_handle_byte(mon
->rs
, buf
[i
]);
1393 if (size
== 0 || buf
[size
- 1] != 0) {
1394 monitor_printf(&mon
->common
, "corrupted command\n");
1396 handle_hmp_command(mon
, (char *)buf
);
1401 static void monitor_event(void *opaque
, QEMUChrEvent event
)
1403 Monitor
*mon
= opaque
;
1406 case CHR_EVENT_MUX_IN
:
1407 qemu_mutex_lock(&mon
->mon_lock
);
1410 monitor_resume(mon
);
1412 qemu_mutex_unlock(&mon
->mon_lock
);
1415 case CHR_EVENT_MUX_OUT
:
1416 qemu_mutex_lock(&mon
->mon_lock
);
1417 if (!mon
->mux_out
) {
1418 if (mon
->reset_seen
&& !mon
->suspend_cnt
) {
1419 monitor_puts_locked(mon
, "\n");
1421 monitor_flush_locked(mon
);
1423 monitor_suspend(mon
);
1426 qemu_mutex_unlock(&mon
->mon_lock
);
1429 case CHR_EVENT_OPENED
:
1430 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
1431 "information\n", QEMU_VERSION
);
1432 qemu_mutex_lock(&mon
->mon_lock
);
1433 mon
->reset_seen
= 1;
1434 if (!mon
->mux_out
) {
1435 /* Suspend-resume forces the prompt to be printed. */
1436 monitor_suspend(mon
);
1437 monitor_resume(mon
);
1439 qemu_mutex_unlock(&mon
->mon_lock
);
1442 case CHR_EVENT_CLOSED
:
1443 monitor_fdsets_cleanup();
1446 case CHR_EVENT_BREAK
:
1454 * These functions just adapt the readline interface in a typesafe way. We
1455 * could cast function pointers but that discards compiler checks.
1457 static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque
,
1458 const char *fmt
, ...)
1460 MonitorHMP
*mon
= opaque
;
1463 monitor_vprintf(&mon
->common
, fmt
, ap
);
1467 static void monitor_readline_flush(void *opaque
)
1469 MonitorHMP
*mon
= opaque
;
1470 monitor_flush(&mon
->common
);
1473 void monitor_init_hmp(Chardev
*chr
, bool use_readline
, Error
**errp
)
1475 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
1477 if (!qemu_chr_fe_init(&mon
->common
.chr
, chr
, errp
)) {
1482 monitor_data_init(&mon
->common
, false, false, false);
1484 mon
->use_readline
= use_readline
;
1485 if (mon
->use_readline
) {
1486 mon
->rs
= readline_init(monitor_readline_printf
,
1487 monitor_readline_flush
,
1489 monitor_find_completion
);
1490 monitor_read_command(mon
, 0);
1493 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
1494 monitor_event
, NULL
, &mon
->common
, NULL
, true);
1495 monitor_list_append(&mon
->common
);