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/error.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qemu/config-file.h"
34 #include "qemu/ctype.h"
35 #include "qemu/cutils.h"
37 #include "qemu/option.h"
38 #include "qemu/units.h"
39 #include "sysemu/block-backend.h"
40 #include "sysemu/runstate.h"
43 static void monitor_command_cb(void *opaque
, const char *cmdline
,
44 void *readline_opaque
)
46 MonitorHMP
*mon
= opaque
;
48 monitor_suspend(&mon
->common
);
49 handle_hmp_command(mon
, cmdline
);
50 monitor_resume(&mon
->common
);
53 void monitor_read_command(MonitorHMP
*mon
, int show_prompt
)
59 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
61 readline_show_prompt(mon
->rs
);
65 int monitor_read_password(MonitorHMP
*mon
, ReadLineFunc
*readline_func
,
69 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
70 /* prompt is printed on return from the command handler */
73 monitor_printf(&mon
->common
,
74 "terminal does not support password prompting\n");
79 static int get_str(char *buf
, int buf_size
, const char **pp
)
87 while (qemu_isspace(*p
)) {
98 while (*p
!= '\0' && *p
!= '\"') {
114 printf("unsupported escape code: '\\%c'\n", c
);
117 if ((q
- buf
) < buf_size
- 1) {
121 if ((q
- buf
) < buf_size
- 1) {
128 printf("unterminated string\n");
133 while (*p
!= '\0' && !qemu_isspace(*p
)) {
134 if ((q
- buf
) < buf_size
- 1) {
147 static void free_cmdline_args(char **args
, int nb_args
)
151 assert(nb_args
<= MAX_ARGS
);
153 for (i
= 0; i
< nb_args
; i
++) {
160 * Parse the command line to get valid args.
161 * @cmdline: command line to be parsed.
162 * @pnb_args: location to store the number of args, must NOT be NULL.
163 * @args: location to store the args, which should be freed by caller, must
166 * Returns 0 on success, negative on failure.
168 * NOTE: this parser is an approximate form of the real command parser. Number
169 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
170 * return with failure.
172 static int parse_cmdline(const char *cmdline
,
173 int *pnb_args
, char **args
)
182 while (qemu_isspace(*p
)) {
188 if (nb_args
>= MAX_ARGS
) {
191 ret
= get_str(buf
, sizeof(buf
), &p
);
195 args
[nb_args
] = g_strdup(buf
);
202 free_cmdline_args(args
, nb_args
);
207 * Can command @cmd be executed in preconfig state?
209 static bool cmd_can_preconfig(const HMPCommand
*cmd
)
215 return strchr(cmd
->flags
, 'p');
218 static bool cmd_available(const HMPCommand
*cmd
)
220 return phase_check(PHASE_MACHINE_READY
) || cmd_can_preconfig(cmd
);
223 static void help_cmd_dump_one(Monitor
*mon
,
224 const HMPCommand
*cmd
,
230 if (!cmd_available(cmd
)) {
234 for (i
= 0; i
< prefix_args_nb
; i
++) {
235 monitor_printf(mon
, "%s ", prefix_args
[i
]);
237 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
240 /* @args[@arg_index] is the valid command need to find in @cmds */
241 static void help_cmd_dump(Monitor
*mon
, const HMPCommand
*cmds
,
242 char **args
, int nb_args
, int arg_index
)
244 const HMPCommand
*cmd
;
247 /* No valid arg need to compare with, dump all in *cmds */
248 if (arg_index
>= nb_args
) {
249 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
250 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
255 /* Find one entry to dump */
256 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
257 if (hmp_compare_cmd(args
[arg_index
], cmd
->name
) &&
258 cmd_available(cmd
)) {
259 if (cmd
->sub_table
) {
260 /* continue with next arg */
261 help_cmd_dump(mon
, cmd
->sub_table
,
262 args
, nb_args
, arg_index
+ 1);
264 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
270 /* Command not found */
271 monitor_printf(mon
, "unknown command: '");
272 for (i
= 0; i
<= arg_index
; i
++) {
273 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
277 void help_cmd(Monitor
*mon
, const char *name
)
279 char *args
[MAX_ARGS
];
282 /* 1. parse user input */
284 /* special case for log, directly dump and return */
285 if (!strcmp(name
, "log")) {
286 const QEMULogItem
*item
;
287 monitor_printf(mon
, "Log items (comma separated):\n");
288 monitor_printf(mon
, "%-15s %s\n", "none", "remove all logs");
289 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
290 monitor_printf(mon
, "%-15s %s\n", item
->name
, item
->help
);
292 #ifdef CONFIG_TRACE_LOG
293 monitor_printf(mon
, "trace:PATTERN enable trace events\n");
294 monitor_printf(mon
, "\nUse \"log trace:help\" to get a list of "
295 "trace events.\n\n");
300 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
305 /* 2. dump the contents according to parsed args */
306 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
308 free_cmdline_args(args
, nb_args
);
311 /*******************************************************************/
313 static const char *pch
;
314 static sigjmp_buf expr_env
;
316 static G_NORETURN
G_GNUC_PRINTF(2, 3)
317 void expr_error(Monitor
*mon
, const char *fmt
, ...)
321 monitor_vprintf(mon
, fmt
, ap
);
322 monitor_printf(mon
, "\n");
324 siglongjmp(expr_env
, 1);
327 static void next(void)
331 while (qemu_isspace(*pch
)) {
337 static int64_t expr_sum(Monitor
*mon
);
339 static int64_t expr_unary(Monitor
*mon
)
352 n
= -expr_unary(mon
);
356 n
= ~expr_unary(mon
);
362 expr_error(mon
, "')' expected");
369 expr_error(mon
, "character constant expected");
374 expr_error(mon
, "missing terminating \' character");
385 while ((*pch
>= 'a' && *pch
<= 'z') ||
386 (*pch
>= 'A' && *pch
<= 'Z') ||
387 (*pch
>= '0' && *pch
<= '9') ||
388 *pch
== '_' || *pch
== '.') {
389 if ((q
- buf
) < sizeof(buf
) - 1) {
394 while (qemu_isspace(*pch
)) {
398 ret
= get_monitor_def(mon
, ®
, buf
);
400 expr_error(mon
, "unknown register");
406 expr_error(mon
, "unexpected end of expression");
411 n
= strtoull(pch
, &p
, 0);
412 if (errno
== ERANGE
) {
413 expr_error(mon
, "number too large");
416 expr_error(mon
, "invalid char '%c' in expression", *p
);
419 while (qemu_isspace(*pch
)) {
427 static int64_t expr_prod(Monitor
*mon
)
432 val
= expr_unary(mon
);
435 if (op
!= '*' && op
!= '/' && op
!= '%') {
439 val2
= expr_unary(mon
);
448 expr_error(mon
, "division by zero");
461 static int64_t expr_logic(Monitor
*mon
)
466 val
= expr_prod(mon
);
469 if (op
!= '&' && op
!= '|' && op
!= '^') {
473 val2
= expr_prod(mon
);
490 static int64_t expr_sum(Monitor
*mon
)
495 val
= expr_logic(mon
);
498 if (op
!= '+' && op
!= '-') {
502 val2
= expr_logic(mon
);
512 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
515 if (sigsetjmp(expr_env
, 0)) {
519 while (qemu_isspace(*pch
)) {
522 *pval
= expr_sum(mon
);
527 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
533 d
= strtod(p
, &tailp
);
535 monitor_printf(mon
, "Number expected\n");
538 if (d
!= d
|| d
- d
!= 0) {
539 /* NaN or infinity */
540 monitor_printf(mon
, "Bad number\n");
549 * Store the command-name in cmdname, and return a pointer to
550 * the remaining of the command string.
552 static const char *get_command_name(const char *cmdline
,
553 char *cmdname
, size_t nlen
)
556 const char *p
, *pstart
;
559 while (qemu_isspace(*p
)) {
566 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
)) {
570 if (len
> nlen
- 1) {
573 memcpy(cmdname
, pstart
, len
);
579 * Read key of 'type' into 'key' and return the current
582 static char *key_get_info(const char *type
, char **key
)
591 p
= strchr(type
, ':');
598 str
= g_malloc(len
+ 1);
599 memcpy(str
, type
, len
);
606 static int default_fmt_format
= 'x';
607 static int default_fmt_size
= 4;
609 static int is_valid_option(const char *c
, const char *typestr
)
617 typestr
= strstr(typestr
, option
);
618 return (typestr
!= NULL
);
621 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
624 const HMPCommand
*cmd
;
626 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
627 if (hmp_compare_cmd(cmdname
, cmd
->name
)) {
636 * Parse command name from @cmdp according to command table @table.
637 * If blank, return NULL.
638 * Else, if no valid command can be found, report to @mon, and return
640 * Else, change @cmdp to point right behind the name, and return its
641 * command table entry.
642 * Do not assume the return value points into @table! It doesn't when
643 * the command is found in a sub-command table.
645 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
646 const char *cmdp_start
,
650 Monitor
*mon
= &hmp_mon
->common
;
652 const HMPCommand
*cmd
;
655 /* extract the command name */
656 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
661 cmd
= search_dispatch_table(table
, cmdname
);
663 monitor_printf(mon
, "unknown command: '%.*s'\n",
664 (int)(p
- cmdp_start
), cmdp_start
);
667 if (!cmd_available(cmd
)) {
668 monitor_printf(mon
, "Command '%.*s' not available "
669 "until machine initialization has completed.\n",
670 (int)(p
- cmdp_start
), cmdp_start
);
674 /* filter out following useless space */
675 while (qemu_isspace(*p
)) {
680 /* search sub command */
681 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
682 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
689 * Parse arguments for @cmd.
690 * If it can't be parsed, report to @mon, and return NULL.
691 * Else, insert command arguments into a QDict, and return it.
692 * Note: On success, caller has to free the QDict structure.
694 static QDict
*monitor_parse_arguments(Monitor
*mon
,
696 const HMPCommand
*cmd
)
701 const char *p
= *endp
;
703 QDict
*qdict
= qdict_new();
705 /* parse the parameters */
706 typestr
= cmd
->args_type
;
708 typestr
= key_get_info(typestr
, &key
);
721 while (qemu_isspace(*p
)) {
724 if (*typestr
== '?') {
727 /* no optional string: NULL argument */
731 ret
= get_str(buf
, sizeof(buf
), &p
);
735 monitor_printf(mon
, "%s: filename expected\n",
739 monitor_printf(mon
, "%s: block device name expected\n",
743 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
748 qdict_put_str(qdict
, key
, buf
);
753 QemuOptsList
*opts_list
;
756 opts_list
= qemu_find_opts(key
);
757 if (!opts_list
|| opts_list
->desc
->name
) {
760 while (qemu_isspace(*p
)) {
766 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
769 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
773 qemu_opts_to_qdict(opts
, qdict
);
779 int count
, format
, size
;
781 while (qemu_isspace(*p
)) {
788 if (qemu_isdigit(*p
)) {
790 while (qemu_isdigit(*p
)) {
791 count
= count
* 10 + (*p
- '0');
829 if (*p
!= '\0' && !qemu_isspace(*p
)) {
830 monitor_printf(mon
, "invalid char in format: '%c'\n",
835 format
= default_fmt_format
;
838 /* for 'i', not specifying a size gives -1 as size */
840 size
= default_fmt_size
;
842 default_fmt_size
= size
;
844 default_fmt_format
= format
;
847 format
= default_fmt_format
;
849 size
= default_fmt_size
;
854 qdict_put_int(qdict
, "count", count
);
855 qdict_put_int(qdict
, "format", format
);
856 qdict_put_int(qdict
, "size", size
);
865 while (qemu_isspace(*p
)) {
868 if (*typestr
== '?' || *typestr
== '.') {
869 if (*typestr
== '?') {
877 while (qemu_isspace(*p
)) {
887 if (get_expr(mon
, &val
, &p
)) {
890 /* Check if 'i' is greater than 32-bit */
891 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
892 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
893 monitor_printf(mon
, "integer is for 32-bit values\n");
895 } else if (c
== 'M') {
897 monitor_printf(mon
, "enter a positive value\n");
902 qdict_put_int(qdict
, key
, val
);
911 while (qemu_isspace(*p
)) {
914 if (*typestr
== '?') {
920 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
921 if (ret
< 0 || val
> INT64_MAX
) {
922 monitor_printf(mon
, "invalid size\n");
925 qdict_put_int(qdict
, key
, val
);
933 while (qemu_isspace(*p
)) {
936 if (*typestr
== '?') {
942 if (get_double(mon
, &val
, &p
) < 0) {
945 if (p
[0] && p
[1] == 's') {
948 val
/= 1e3
; p
+= 2; break;
950 val
/= 1e6
; p
+= 2; break;
952 val
/= 1e9
; p
+= 2; break;
955 if (*p
&& !qemu_isspace(*p
)) {
956 monitor_printf(mon
, "Unknown unit suffix\n");
959 qdict_put(qdict
, key
, qnum_from_double(val
));
967 while (qemu_isspace(*p
)) {
971 while (qemu_isgraph(*p
)) {
974 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
976 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
979 monitor_printf(mon
, "Expected 'on' or 'off'\n");
982 qdict_put_bool(qdict
, key
, val
);
996 while (qemu_isspace(*p
)) {
1002 if (!is_valid_option(p
, typestr
)) {
1003 monitor_printf(mon
, "%s: unsupported option -%c\n",
1012 } else if (*typestr
== 's') {
1013 /* has option with string value */
1016 while (qemu_isspace(*p
)) {
1019 ret
= get_str(buf
, sizeof(buf
), &p
);
1021 monitor_printf(mon
, "%s: value expected for -%c\n",
1025 qdict_put_str(qdict
, key
, buf
);
1027 /* has boolean option */
1029 qdict_put_bool(qdict
, key
, true);
1031 } else if (*typestr
== 's') {
1038 /* package all remaining string */
1041 while (qemu_isspace(*p
)) {
1044 if (*typestr
== '?') {
1047 /* no remaining string: NULL argument */
1053 monitor_printf(mon
, "%s: string expected\n",
1057 qdict_put_str(qdict
, key
, p
);
1063 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
1069 /* check that all arguments were parsed */
1070 while (qemu_isspace(*p
)) {
1074 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
1082 qobject_unref(qdict
);
1087 static void hmp_info_human_readable_text(Monitor
*mon
,
1088 HumanReadableText
*(*handler
)(Error
**))
1091 g_autoptr(HumanReadableText
) info
= handler(&err
);
1093 if (hmp_handle_error(mon
, err
)) {
1097 monitor_printf(mon
, "%s", info
->human_readable_text
);
1100 static void handle_hmp_command_exec(Monitor
*mon
,
1101 const HMPCommand
*cmd
,
1104 if (cmd
->cmd_info_hrt
) {
1105 hmp_info_human_readable_text(mon
,
1108 cmd
->cmd(mon
, qdict
);
1112 typedef struct HandleHmpCommandCo
{
1114 const HMPCommand
*cmd
;
1117 } HandleHmpCommandCo
;
1119 static void handle_hmp_command_co(void *opaque
)
1121 HandleHmpCommandCo
*data
= opaque
;
1122 handle_hmp_command_exec(data
->mon
, data
->cmd
, data
->qdict
);
1123 monitor_set_cur(qemu_coroutine_self(), NULL
);
1127 void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
1130 const HMPCommand
*cmd
;
1131 const char *cmd_start
= cmdline
;
1133 trace_handle_hmp_command(mon
, cmdline
);
1135 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
1140 if (!cmd
->cmd
&& !cmd
->cmd_info_hrt
) {
1141 /* FIXME: is it useful to try autoload modules here ??? */
1142 monitor_printf(&mon
->common
, "Command \"%.*s\" is not available.\n",
1143 (int)(cmdline
- cmd_start
), cmd_start
);
1147 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
1149 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
1152 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
1153 (int)(cmdline
- cmd_start
), cmd_start
);
1157 if (!cmd
->coroutine
) {
1158 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1159 Monitor
*old_mon
= monitor_set_cur(qemu_coroutine_self(), &mon
->common
);
1160 handle_hmp_command_exec(&mon
->common
, cmd
, qdict
);
1161 monitor_set_cur(qemu_coroutine_self(), old_mon
);
1163 HandleHmpCommandCo data
= {
1164 .mon
= &mon
->common
,
1169 Coroutine
*co
= qemu_coroutine_create(handle_hmp_command_co
, &data
);
1170 monitor_set_cur(co
, &mon
->common
);
1171 aio_co_enter(qemu_get_aio_context(), co
);
1172 AIO_WAIT_WHILE(qemu_get_aio_context(), !data
.done
);
1175 qobject_unref(qdict
);
1178 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
1180 const char *p
, *pstart
;
1187 p
= qemu_strchrnul(p
, '|');
1189 if (len
> sizeof(cmd
) - 2) {
1190 len
= sizeof(cmd
) - 2;
1192 memcpy(cmd
, pstart
, len
);
1194 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1195 readline_add_completion(mon
->rs
, cmd
);
1204 static void file_completion(MonitorHMP
*mon
, const char *input
)
1209 char file
[1024], file_prefix
[1024];
1213 p
= strrchr(input
, '/');
1216 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1217 pstrcpy(path
, sizeof(path
), ".");
1219 input_path_len
= p
- input
+ 1;
1220 memcpy(path
, input
, input_path_len
);
1221 if (input_path_len
> sizeof(path
) - 1) {
1222 input_path_len
= sizeof(path
) - 1;
1224 path
[input_path_len
] = '\0';
1225 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1228 ffs
= opendir(path
);
1239 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
1243 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1244 memcpy(file
, input
, input_path_len
);
1245 if (input_path_len
< sizeof(file
)) {
1246 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
1250 * stat the file to find out if it's a directory.
1251 * In that case add a slash to speed up typing long paths
1253 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
1254 pstrcat(file
, sizeof(file
), "/");
1256 readline_add_completion(mon
->rs
, file
);
1262 static const char *next_arg_type(const char *typestr
)
1264 const char *p
= strchr(typestr
, ':');
1265 return (p
!= NULL
? ++p
: typestr
);
1268 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
1269 const HMPCommand
*cmd_table
,
1273 const char *cmdname
;
1275 const char *ptype
, *old_ptype
, *str
, *name
;
1276 const HMPCommand
*cmd
;
1277 BlockBackend
*blk
= NULL
;
1280 /* command completion */
1286 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
1287 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1288 if (cmd_available(cmd
)) {
1289 cmd_completion(mon
, cmdname
, cmd
->name
);
1293 /* find the command */
1294 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1295 if (hmp_compare_cmd(args
[0], cmd
->name
) &&
1296 cmd_available(cmd
)) {
1304 if (cmd
->sub_table
) {
1305 /* do the job again */
1306 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
1307 &args
[1], nb_args
- 1);
1310 if (cmd
->command_completion
) {
1311 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
1315 ptype
= next_arg_type(cmd
->args_type
);
1316 for (i
= 0; i
< nb_args
- 2; i
++) {
1317 if (*ptype
!= '\0') {
1318 ptype
= next_arg_type(ptype
);
1319 while (*ptype
== '?') {
1320 ptype
= next_arg_type(ptype
);
1324 str
= args
[nb_args
- 1];
1326 while (*ptype
== '-' && old_ptype
!= ptype
) {
1328 ptype
= next_arg_type(ptype
);
1332 /* file completion */
1333 readline_set_completion_index(mon
->rs
, strlen(str
));
1334 file_completion(mon
, str
);
1337 /* block device name completion */
1338 readline_set_completion_index(mon
->rs
, strlen(str
));
1339 while ((blk
= blk_next(blk
)) != NULL
) {
1340 name
= blk_name(blk
);
1341 if (str
[0] == '\0' ||
1342 !strncmp(name
, str
, strlen(str
))) {
1343 readline_add_completion(mon
->rs
, name
);
1349 if (!strcmp(cmd
->name
, "help|?")) {
1350 monitor_find_completion_by_table(mon
, cmd_table
,
1351 &args
[1], nb_args
- 1);
1360 static void monitor_find_completion(void *opaque
,
1361 const char *cmdline
)
1363 MonitorHMP
*mon
= opaque
;
1364 char *args
[MAX_ARGS
];
1367 /* 1. parse the cmdline */
1368 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
1373 * if the line ends with a space, it means we want to complete the
1376 len
= strlen(cmdline
);
1377 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
1378 if (nb_args
>= MAX_ARGS
) {
1381 args
[nb_args
++] = g_strdup("");
1384 /* 2. auto complete according to args */
1385 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
1388 free_cmdline_args(args
, nb_args
);
1391 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
1393 MonitorHMP
*mon
= container_of(opaque
, MonitorHMP
, common
);
1397 for (i
= 0; i
< size
; i
++) {
1398 readline_handle_byte(mon
->rs
, buf
[i
]);
1401 if (size
== 0 || buf
[size
- 1] != 0) {
1402 monitor_printf(&mon
->common
, "corrupted command\n");
1404 handle_hmp_command(mon
, (char *)buf
);
1409 static void monitor_event(void *opaque
, QEMUChrEvent event
)
1411 Monitor
*mon
= opaque
;
1412 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1415 case CHR_EVENT_MUX_IN
:
1416 qemu_mutex_lock(&mon
->mon_lock
);
1418 qemu_mutex_unlock(&mon
->mon_lock
);
1419 if (mon
->reset_seen
) {
1420 readline_restart(hmp_mon
->rs
);
1421 monitor_resume(mon
);
1424 qatomic_mb_set(&mon
->suspend_cnt
, 0);
1428 case CHR_EVENT_MUX_OUT
:
1429 if (mon
->reset_seen
) {
1430 if (qatomic_mb_read(&mon
->suspend_cnt
) == 0) {
1431 monitor_printf(mon
, "\n");
1434 monitor_suspend(mon
);
1436 qatomic_inc(&mon
->suspend_cnt
);
1438 qemu_mutex_lock(&mon
->mon_lock
);
1440 qemu_mutex_unlock(&mon
->mon_lock
);
1443 case CHR_EVENT_OPENED
:
1444 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
1445 "information\n", QEMU_VERSION
);
1446 if (!mon
->mux_out
) {
1447 readline_restart(hmp_mon
->rs
);
1448 readline_show_prompt(hmp_mon
->rs
);
1450 mon
->reset_seen
= 1;
1454 case CHR_EVENT_CLOSED
:
1456 monitor_fdsets_cleanup();
1459 case CHR_EVENT_BREAK
:
1467 * These functions just adapt the readline interface in a typesafe way. We
1468 * could cast function pointers but that discards compiler checks.
1470 static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque
,
1471 const char *fmt
, ...)
1473 MonitorHMP
*mon
= opaque
;
1476 monitor_vprintf(&mon
->common
, fmt
, ap
);
1480 static void monitor_readline_flush(void *opaque
)
1482 MonitorHMP
*mon
= opaque
;
1483 monitor_flush(&mon
->common
);
1486 void monitor_init_hmp(Chardev
*chr
, bool use_readline
, Error
**errp
)
1488 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
1490 if (!qemu_chr_fe_init(&mon
->common
.chr
, chr
, errp
)) {
1495 monitor_data_init(&mon
->common
, false, false, false);
1497 mon
->use_readline
= use_readline
;
1498 if (mon
->use_readline
) {
1499 mon
->rs
= readline_init(monitor_readline_printf
,
1500 monitor_readline_flush
,
1502 monitor_find_completion
);
1503 monitor_read_command(mon
, 0);
1506 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
1507 monitor_event
, NULL
, &mon
->common
, NULL
, true);
1508 monitor_list_append(&mon
->common
);