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
, "%-10s %s\n", "none", "remove all logs");
289 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
290 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
295 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
300 /* 2. dump the contents according to parsed args */
301 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
303 free_cmdline_args(args
, nb_args
);
306 /*******************************************************************/
308 static const char *pch
;
309 static sigjmp_buf expr_env
;
311 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
312 expr_error(Monitor
*mon
, const char *fmt
, ...)
316 monitor_vprintf(mon
, fmt
, ap
);
317 monitor_printf(mon
, "\n");
319 siglongjmp(expr_env
, 1);
322 static void next(void)
326 while (qemu_isspace(*pch
)) {
332 static int64_t expr_sum(Monitor
*mon
);
334 static int64_t expr_unary(Monitor
*mon
)
347 n
= -expr_unary(mon
);
351 n
= ~expr_unary(mon
);
357 expr_error(mon
, "')' expected");
364 expr_error(mon
, "character constant expected");
369 expr_error(mon
, "missing terminating \' character");
380 while ((*pch
>= 'a' && *pch
<= 'z') ||
381 (*pch
>= 'A' && *pch
<= 'Z') ||
382 (*pch
>= '0' && *pch
<= '9') ||
383 *pch
== '_' || *pch
== '.') {
384 if ((q
- buf
) < sizeof(buf
) - 1) {
389 while (qemu_isspace(*pch
)) {
393 ret
= get_monitor_def(mon
, ®
, buf
);
395 expr_error(mon
, "unknown register");
401 expr_error(mon
, "unexpected end of expression");
406 n
= strtoull(pch
, &p
, 0);
407 if (errno
== ERANGE
) {
408 expr_error(mon
, "number too large");
411 expr_error(mon
, "invalid char '%c' in expression", *p
);
414 while (qemu_isspace(*pch
)) {
422 static int64_t expr_prod(Monitor
*mon
)
427 val
= expr_unary(mon
);
430 if (op
!= '*' && op
!= '/' && op
!= '%') {
434 val2
= expr_unary(mon
);
443 expr_error(mon
, "division by zero");
456 static int64_t expr_logic(Monitor
*mon
)
461 val
= expr_prod(mon
);
464 if (op
!= '&' && op
!= '|' && op
!= '^') {
468 val2
= expr_prod(mon
);
485 static int64_t expr_sum(Monitor
*mon
)
490 val
= expr_logic(mon
);
493 if (op
!= '+' && op
!= '-') {
497 val2
= expr_logic(mon
);
507 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
510 if (sigsetjmp(expr_env
, 0)) {
514 while (qemu_isspace(*pch
)) {
517 *pval
= expr_sum(mon
);
522 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
528 d
= strtod(p
, &tailp
);
530 monitor_printf(mon
, "Number expected\n");
533 if (d
!= d
|| d
- d
!= 0) {
534 /* NaN or infinity */
535 monitor_printf(mon
, "Bad number\n");
544 * Store the command-name in cmdname, and return a pointer to
545 * the remaining of the command string.
547 static const char *get_command_name(const char *cmdline
,
548 char *cmdname
, size_t nlen
)
551 const char *p
, *pstart
;
554 while (qemu_isspace(*p
)) {
561 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
)) {
565 if (len
> nlen
- 1) {
568 memcpy(cmdname
, pstart
, len
);
574 * Read key of 'type' into 'key' and return the current
577 static char *key_get_info(const char *type
, char **key
)
586 p
= strchr(type
, ':');
593 str
= g_malloc(len
+ 1);
594 memcpy(str
, type
, len
);
601 static int default_fmt_format
= 'x';
602 static int default_fmt_size
= 4;
604 static int is_valid_option(const char *c
, const char *typestr
)
612 typestr
= strstr(typestr
, option
);
613 return (typestr
!= NULL
);
616 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
619 const HMPCommand
*cmd
;
621 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
622 if (hmp_compare_cmd(cmdname
, cmd
->name
)) {
631 * Parse command name from @cmdp according to command table @table.
632 * If blank, return NULL.
633 * Else, if no valid command can be found, report to @mon, and return
635 * Else, change @cmdp to point right behind the name, and return its
636 * command table entry.
637 * Do not assume the return value points into @table! It doesn't when
638 * the command is found in a sub-command table.
640 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
641 const char *cmdp_start
,
645 Monitor
*mon
= &hmp_mon
->common
;
647 const HMPCommand
*cmd
;
650 /* extract the command name */
651 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
656 cmd
= search_dispatch_table(table
, cmdname
);
658 monitor_printf(mon
, "unknown command: '%.*s'\n",
659 (int)(p
- cmdp_start
), cmdp_start
);
662 if (!cmd_available(cmd
)) {
663 monitor_printf(mon
, "Command '%.*s' not available "
664 "until machine initialization has completed.\n",
665 (int)(p
- cmdp_start
), cmdp_start
);
669 /* filter out following useless space */
670 while (qemu_isspace(*p
)) {
675 /* search sub command */
676 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
677 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
684 * Parse arguments for @cmd.
685 * If it can't be parsed, report to @mon, and return NULL.
686 * Else, insert command arguments into a QDict, and return it.
687 * Note: On success, caller has to free the QDict structure.
689 static QDict
*monitor_parse_arguments(Monitor
*mon
,
691 const HMPCommand
*cmd
)
696 const char *p
= *endp
;
698 QDict
*qdict
= qdict_new();
700 /* parse the parameters */
701 typestr
= cmd
->args_type
;
703 typestr
= key_get_info(typestr
, &key
);
716 while (qemu_isspace(*p
)) {
719 if (*typestr
== '?') {
722 /* no optional string: NULL argument */
726 ret
= get_str(buf
, sizeof(buf
), &p
);
730 monitor_printf(mon
, "%s: filename expected\n",
734 monitor_printf(mon
, "%s: block device name expected\n",
738 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
743 qdict_put_str(qdict
, key
, buf
);
748 QemuOptsList
*opts_list
;
751 opts_list
= qemu_find_opts(key
);
752 if (!opts_list
|| opts_list
->desc
->name
) {
755 while (qemu_isspace(*p
)) {
761 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
764 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
768 qemu_opts_to_qdict(opts
, qdict
);
774 int count
, format
, size
;
776 while (qemu_isspace(*p
)) {
783 if (qemu_isdigit(*p
)) {
785 while (qemu_isdigit(*p
)) {
786 count
= count
* 10 + (*p
- '0');
824 if (*p
!= '\0' && !qemu_isspace(*p
)) {
825 monitor_printf(mon
, "invalid char in format: '%c'\n",
830 format
= default_fmt_format
;
833 /* for 'i', not specifying a size gives -1 as size */
835 size
= default_fmt_size
;
837 default_fmt_size
= size
;
839 default_fmt_format
= format
;
842 format
= default_fmt_format
;
844 size
= default_fmt_size
;
849 qdict_put_int(qdict
, "count", count
);
850 qdict_put_int(qdict
, "format", format
);
851 qdict_put_int(qdict
, "size", size
);
860 while (qemu_isspace(*p
)) {
863 if (*typestr
== '?' || *typestr
== '.') {
864 if (*typestr
== '?') {
872 while (qemu_isspace(*p
)) {
882 if (get_expr(mon
, &val
, &p
)) {
885 /* Check if 'i' is greater than 32-bit */
886 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
887 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
888 monitor_printf(mon
, "integer is for 32-bit values\n");
890 } else if (c
== 'M') {
892 monitor_printf(mon
, "enter a positive value\n");
897 qdict_put_int(qdict
, key
, val
);
906 while (qemu_isspace(*p
)) {
909 if (*typestr
== '?') {
915 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
916 if (ret
< 0 || val
> INT64_MAX
) {
917 monitor_printf(mon
, "invalid size\n");
920 qdict_put_int(qdict
, key
, val
);
928 while (qemu_isspace(*p
)) {
931 if (*typestr
== '?') {
937 if (get_double(mon
, &val
, &p
) < 0) {
940 if (p
[0] && p
[1] == 's') {
943 val
/= 1e3
; p
+= 2; break;
945 val
/= 1e6
; p
+= 2; break;
947 val
/= 1e9
; p
+= 2; break;
950 if (*p
&& !qemu_isspace(*p
)) {
951 monitor_printf(mon
, "Unknown unit suffix\n");
954 qdict_put(qdict
, key
, qnum_from_double(val
));
962 while (qemu_isspace(*p
)) {
966 while (qemu_isgraph(*p
)) {
969 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
971 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
974 monitor_printf(mon
, "Expected 'on' or 'off'\n");
977 qdict_put_bool(qdict
, key
, val
);
991 while (qemu_isspace(*p
)) {
997 if (!is_valid_option(p
, typestr
)) {
998 monitor_printf(mon
, "%s: unsupported option -%c\n",
1007 } else if (*typestr
== 's') {
1008 /* has option with string value */
1011 while (qemu_isspace(*p
)) {
1014 ret
= get_str(buf
, sizeof(buf
), &p
);
1016 monitor_printf(mon
, "%s: value expected for -%c\n",
1020 qdict_put_str(qdict
, key
, buf
);
1022 /* has boolean option */
1024 qdict_put_bool(qdict
, key
, true);
1026 } else if (*typestr
== 's') {
1033 /* package all remaining string */
1036 while (qemu_isspace(*p
)) {
1039 if (*typestr
== '?') {
1042 /* no remaining string: NULL argument */
1048 monitor_printf(mon
, "%s: string expected\n",
1052 qdict_put_str(qdict
, key
, p
);
1058 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
1064 /* check that all arguments were parsed */
1065 while (qemu_isspace(*p
)) {
1069 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
1077 qobject_unref(qdict
);
1082 static void hmp_info_human_readable_text(Monitor
*mon
,
1083 HumanReadableText
*(*handler
)(Error
**))
1086 g_autoptr(HumanReadableText
) info
= handler(&err
);
1088 if (hmp_handle_error(mon
, err
)) {
1092 monitor_printf(mon
, "%s", info
->human_readable_text
);
1095 static void handle_hmp_command_exec(Monitor
*mon
,
1096 const HMPCommand
*cmd
,
1099 if (cmd
->cmd_info_hrt
) {
1100 hmp_info_human_readable_text(mon
,
1103 cmd
->cmd(mon
, qdict
);
1107 typedef struct HandleHmpCommandCo
{
1109 const HMPCommand
*cmd
;
1112 } HandleHmpCommandCo
;
1114 static void handle_hmp_command_co(void *opaque
)
1116 HandleHmpCommandCo
*data
= opaque
;
1117 handle_hmp_command_exec(data
->mon
, data
->cmd
, data
->qdict
);
1118 monitor_set_cur(qemu_coroutine_self(), NULL
);
1122 void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
1125 const HMPCommand
*cmd
;
1126 const char *cmd_start
= cmdline
;
1128 trace_handle_hmp_command(mon
, cmdline
);
1130 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
1135 if (!cmd
->cmd
&& !cmd
->cmd_info_hrt
) {
1136 /* FIXME: is it useful to try autoload modules here ??? */
1137 monitor_printf(&mon
->common
, "Command \"%.*s\" is not available.\n",
1138 (int)(cmdline
- cmd_start
), cmd_start
);
1142 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
1144 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
1147 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
1148 (int)(cmdline
- cmd_start
), cmd_start
);
1152 if (!cmd
->coroutine
) {
1153 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1154 Monitor
*old_mon
= monitor_set_cur(qemu_coroutine_self(), &mon
->common
);
1155 handle_hmp_command_exec(&mon
->common
, cmd
, qdict
);
1156 monitor_set_cur(qemu_coroutine_self(), old_mon
);
1158 HandleHmpCommandCo data
= {
1159 .mon
= &mon
->common
,
1164 Coroutine
*co
= qemu_coroutine_create(handle_hmp_command_co
, &data
);
1165 monitor_set_cur(co
, &mon
->common
);
1166 aio_co_enter(qemu_get_aio_context(), co
);
1167 AIO_WAIT_WHILE(qemu_get_aio_context(), !data
.done
);
1170 qobject_unref(qdict
);
1173 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
1175 const char *p
, *pstart
;
1182 p
= qemu_strchrnul(p
, '|');
1184 if (len
> sizeof(cmd
) - 2) {
1185 len
= sizeof(cmd
) - 2;
1187 memcpy(cmd
, pstart
, len
);
1189 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1190 readline_add_completion(mon
->rs
, cmd
);
1199 static void file_completion(MonitorHMP
*mon
, const char *input
)
1204 char file
[1024], file_prefix
[1024];
1208 p
= strrchr(input
, '/');
1211 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1212 pstrcpy(path
, sizeof(path
), ".");
1214 input_path_len
= p
- input
+ 1;
1215 memcpy(path
, input
, input_path_len
);
1216 if (input_path_len
> sizeof(path
) - 1) {
1217 input_path_len
= sizeof(path
) - 1;
1219 path
[input_path_len
] = '\0';
1220 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1223 ffs
= opendir(path
);
1234 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
1238 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1239 memcpy(file
, input
, input_path_len
);
1240 if (input_path_len
< sizeof(file
)) {
1241 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
1245 * stat the file to find out if it's a directory.
1246 * In that case add a slash to speed up typing long paths
1248 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
1249 pstrcat(file
, sizeof(file
), "/");
1251 readline_add_completion(mon
->rs
, file
);
1257 static const char *next_arg_type(const char *typestr
)
1259 const char *p
= strchr(typestr
, ':');
1260 return (p
!= NULL
? ++p
: typestr
);
1263 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
1264 const HMPCommand
*cmd_table
,
1268 const char *cmdname
;
1270 const char *ptype
, *old_ptype
, *str
, *name
;
1271 const HMPCommand
*cmd
;
1272 BlockBackend
*blk
= NULL
;
1275 /* command completion */
1281 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
1282 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1283 if (cmd_available(cmd
)) {
1284 cmd_completion(mon
, cmdname
, cmd
->name
);
1288 /* find the command */
1289 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
1290 if (hmp_compare_cmd(args
[0], cmd
->name
) &&
1291 cmd_available(cmd
)) {
1299 if (cmd
->sub_table
) {
1300 /* do the job again */
1301 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
1302 &args
[1], nb_args
- 1);
1305 if (cmd
->command_completion
) {
1306 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
1310 ptype
= next_arg_type(cmd
->args_type
);
1311 for (i
= 0; i
< nb_args
- 2; i
++) {
1312 if (*ptype
!= '\0') {
1313 ptype
= next_arg_type(ptype
);
1314 while (*ptype
== '?') {
1315 ptype
= next_arg_type(ptype
);
1319 str
= args
[nb_args
- 1];
1321 while (*ptype
== '-' && old_ptype
!= ptype
) {
1323 ptype
= next_arg_type(ptype
);
1327 /* file completion */
1328 readline_set_completion_index(mon
->rs
, strlen(str
));
1329 file_completion(mon
, str
);
1332 /* block device name completion */
1333 readline_set_completion_index(mon
->rs
, strlen(str
));
1334 while ((blk
= blk_next(blk
)) != NULL
) {
1335 name
= blk_name(blk
);
1336 if (str
[0] == '\0' ||
1337 !strncmp(name
, str
, strlen(str
))) {
1338 readline_add_completion(mon
->rs
, name
);
1344 if (!strcmp(cmd
->name
, "help|?")) {
1345 monitor_find_completion_by_table(mon
, cmd_table
,
1346 &args
[1], nb_args
- 1);
1355 static void monitor_find_completion(void *opaque
,
1356 const char *cmdline
)
1358 MonitorHMP
*mon
= opaque
;
1359 char *args
[MAX_ARGS
];
1362 /* 1. parse the cmdline */
1363 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
1368 * if the line ends with a space, it means we want to complete the
1371 len
= strlen(cmdline
);
1372 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
1373 if (nb_args
>= MAX_ARGS
) {
1376 args
[nb_args
++] = g_strdup("");
1379 /* 2. auto complete according to args */
1380 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
1383 free_cmdline_args(args
, nb_args
);
1386 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
1388 MonitorHMP
*mon
= container_of(opaque
, MonitorHMP
, common
);
1392 for (i
= 0; i
< size
; i
++) {
1393 readline_handle_byte(mon
->rs
, buf
[i
]);
1396 if (size
== 0 || buf
[size
- 1] != 0) {
1397 monitor_printf(&mon
->common
, "corrupted command\n");
1399 handle_hmp_command(mon
, (char *)buf
);
1404 static void monitor_event(void *opaque
, QEMUChrEvent event
)
1406 Monitor
*mon
= opaque
;
1407 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1410 case CHR_EVENT_MUX_IN
:
1411 qemu_mutex_lock(&mon
->mon_lock
);
1413 qemu_mutex_unlock(&mon
->mon_lock
);
1414 if (mon
->reset_seen
) {
1415 readline_restart(hmp_mon
->rs
);
1416 monitor_resume(mon
);
1419 qatomic_mb_set(&mon
->suspend_cnt
, 0);
1423 case CHR_EVENT_MUX_OUT
:
1424 if (mon
->reset_seen
) {
1425 if (qatomic_mb_read(&mon
->suspend_cnt
) == 0) {
1426 monitor_printf(mon
, "\n");
1429 monitor_suspend(mon
);
1431 qatomic_inc(&mon
->suspend_cnt
);
1433 qemu_mutex_lock(&mon
->mon_lock
);
1435 qemu_mutex_unlock(&mon
->mon_lock
);
1438 case CHR_EVENT_OPENED
:
1439 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
1440 "information\n", QEMU_VERSION
);
1441 if (!mon
->mux_out
) {
1442 readline_restart(hmp_mon
->rs
);
1443 readline_show_prompt(hmp_mon
->rs
);
1445 mon
->reset_seen
= 1;
1449 case CHR_EVENT_CLOSED
:
1451 monitor_fdsets_cleanup();
1454 case CHR_EVENT_BREAK
:
1462 * These functions just adapt the readline interface in a typesafe way. We
1463 * could cast function pointers but that discards compiler checks.
1465 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
1466 const char *fmt
, ...)
1468 MonitorHMP
*mon
= opaque
;
1471 monitor_vprintf(&mon
->common
, fmt
, ap
);
1475 static void monitor_readline_flush(void *opaque
)
1477 MonitorHMP
*mon
= opaque
;
1478 monitor_flush(&mon
->common
);
1481 void monitor_init_hmp(Chardev
*chr
, bool use_readline
, Error
**errp
)
1483 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
1485 if (!qemu_chr_fe_init(&mon
->common
.chr
, chr
, errp
)) {
1490 monitor_data_init(&mon
->common
, false, false, false);
1492 mon
->use_readline
= use_readline
;
1493 if (mon
->use_readline
) {
1494 mon
->rs
= readline_init(monitor_readline_printf
,
1495 monitor_readline_flush
,
1497 monitor_find_completion
);
1498 monitor_read_command(mon
, 0);
1501 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
1502 monitor_event
, NULL
, &mon
->common
, NULL
, true);
1503 monitor_list_append(&mon
->common
);