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);
1093 /* FIXME: is it useful to try autoload modules here ??? */
1094 monitor_printf(&mon->common, "Command \"%.*s\" is not available.\n",
1095 (int)(cmdline - cmd_start), cmd_start);
1099 qdict = monitor_parse_arguments(&mon->common, &cmdline, cmd);
1101 while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
1104 monitor_printf(&mon->common, "Try \"help %.*s\" for more information\n",
1105 (int)(cmdline - cmd_start), cmd_start);
1109 if (!cmd->coroutine) {
1110 /* old_mon is non-NULL when called from qmp_human_monitor_command() */
1111 Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(), &mon->common);
1112 cmd->cmd(&mon->common, qdict);
1113 monitor_set_cur(qemu_coroutine_self(), old_mon);
1115 HandleHmpCommandCo data = {
1116 .mon = &mon->common,
1121 Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data);
1122 monitor_set_cur(co, &mon->common);
1123 aio_co_enter(qemu_get_aio_context(), co);
1124 AIO_WAIT_WHILE(qemu_get_aio_context(), !data.done);
1127 qobject_unref(qdict);
1130 static void cmd_completion(MonitorHMP *mon, const char *name, const char *list)
1132 const char *p, *pstart;
1139 p = qemu_strchrnul(p, '|');
1141 if (len > sizeof(cmd) - 2) {
1142 len = sizeof(cmd) - 2;
1144 memcpy(cmd, pstart, len);
1146 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1147 readline_add_completion(mon->rs, cmd);
1156 static void file_completion(MonitorHMP *mon, const char *input)
1161 char file[1024], file_prefix[1024];
1165 p = strrchr(input, '/');
1168 pstrcpy(file_prefix, sizeof(file_prefix), input);
1169 pstrcpy(path, sizeof(path), ".");
1171 input_path_len = p - input + 1;
1172 memcpy(path, input, input_path_len);
1173 if (input_path_len > sizeof(path) - 1) {
1174 input_path_len = sizeof(path) - 1;
1176 path[input_path_len] = '\0';
1177 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1180 ffs = opendir(path);
1191 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
1195 if (strstart(d->d_name, file_prefix, NULL)) {
1196 memcpy(file, input, input_path_len);
1197 if (input_path_len < sizeof(file)) {
1198 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
1202 * stat the file to find out if it's a directory.
1203 * In that case add a slash to speed up typing long paths
1205 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
1206 pstrcat(file, sizeof(file), "/");
1208 readline_add_completion(mon->rs, file);
1214 static const char *next_arg_type(const char *typestr)
1216 const char *p = strchr(typestr, ':');
1217 return (p != NULL ? ++p : typestr);
1220 static void monitor_find_completion_by_table(MonitorHMP *mon,
1221 const HMPCommand *cmd_table,
1225 const char *cmdname;
1227 const char *ptype, *old_ptype, *str, *name;
1228 const HMPCommand *cmd;
1229 BlockBackend *blk = NULL;
1232 /* command completion */
1238 readline_set_completion_index(mon->rs, strlen(cmdname));
1239 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
1240 if (cmd_available(cmd)) {
1241 cmd_completion(mon, cmdname, cmd->name);
1245 /* find the command */
1246 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
1247 if (hmp_compare_cmd(args[0], cmd->name) &&
1248 cmd_available(cmd)) {
1256 if (cmd->sub_table) {
1257 /* do the job again */
1258 monitor_find_completion_by_table(mon, cmd->sub_table,
1259 &args[1], nb_args - 1);
1262 if (cmd->command_completion) {
1263 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
1267 ptype = next_arg_type(cmd->args_type);
1268 for (i = 0; i < nb_args - 2; i++) {
1269 if (*ptype != '\0') {
1270 ptype = next_arg_type(ptype);
1271 while (*ptype == '?') {
1272 ptype = next_arg_type(ptype);
1276 str = args[nb_args - 1];
1278 while (*ptype == '-' && old_ptype != ptype) {
1280 ptype = next_arg_type(ptype);
1284 /* file completion */
1285 readline_set_completion_index(mon->rs, strlen(str));
1286 file_completion(mon, str);
1289 /* block device name completion */
1290 readline_set_completion_index(mon->rs, strlen(str));
1291 while ((blk = blk_next(blk)) != NULL) {
1292 name = blk_name(blk);
1293 if (str[0] == '\0' ||
1294 !strncmp(name, str, strlen(str))) {
1295 readline_add_completion(mon->rs, name);
1301 if (!strcmp(cmd->name, "help|?")) {
1302 monitor_find_completion_by_table(mon, cmd_table,
1303 &args[1], nb_args - 1);
1312 static void monitor_find_completion(void *opaque,
1313 const char *cmdline)
1315 MonitorHMP *mon = opaque;
1316 char *args[MAX_ARGS];
1319 /* 1. parse the cmdline */
1320 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
1325 * if the line ends with a space, it means we want to complete the
1328 len = strlen(cmdline);
1329 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
1330 if (nb_args >= MAX_ARGS) {
1333 args[nb_args++] = g_strdup("");
1336 /* 2. auto complete according to args */
1337 monitor_find_completion_by_table(mon, hmp_cmds, args, nb_args);
1340 free_cmdline_args(args, nb_args);
1343 static void monitor_read(void *opaque, const uint8_t *buf, int size)
1345 MonitorHMP *mon = container_of(opaque, MonitorHMP, common);
1349 for (i = 0; i < size; i++) {
1350 readline_handle_byte(mon->rs, buf[i]);
1353 if (size == 0 || buf[size - 1] != 0) {
1354 monitor_printf(&mon->common, "corrupted command\n");
1356 handle_hmp_command(mon, (char *)buf);
1361 static void monitor_event(void *opaque, QEMUChrEvent event)
1363 Monitor *mon = opaque;
1364 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
1367 case CHR_EVENT_MUX_IN:
1368 qemu_mutex_lock(&mon->mon_lock);
1370 qemu_mutex_unlock(&mon->mon_lock);
1371 if (mon->reset_seen) {
1372 readline_restart(hmp_mon->rs);
1373 monitor_resume(mon);
1376 qatomic_mb_set(&mon->suspend_cnt, 0);
1380 case CHR_EVENT_MUX_OUT:
1381 if (mon->reset_seen) {
1382 if (qatomic_mb_read(&mon->suspend_cnt) == 0) {
1383 monitor_printf(mon, "\n");
1386 monitor_suspend(mon);
1388 qatomic_inc(&mon->suspend_cnt);
1390 qemu_mutex_lock(&mon->mon_lock);
1392 qemu_mutex_unlock(&mon->mon_lock);
1395 case CHR_EVENT_OPENED:
1396 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
1397 "information\n", QEMU_VERSION);
1398 if (!mon->mux_out) {
1399 readline_restart(hmp_mon->rs);
1400 readline_show_prompt(hmp_mon->rs);
1402 mon->reset_seen = 1;
1406 case CHR_EVENT_CLOSED:
1408 monitor_fdsets_cleanup();
1411 case CHR_EVENT_BREAK:
1419 * These functions just adapt the readline interface in a typesafe way. We
1420 * could cast function pointers but that discards compiler checks.
1422 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
1423 const char *fmt, ...)
1425 MonitorHMP *mon = opaque;
1428 monitor_vprintf(&mon->common, fmt, ap);
1432 static void monitor_readline_flush(void *opaque)
1434 MonitorHMP *mon = opaque;
1435 monitor_flush(&mon->common);
1438 void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
1440 MonitorHMP *mon = g_new0(MonitorHMP, 1);
1442 if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
1447 monitor_data_init(&mon->common, false, false, false);
1449 mon->use_readline = use_readline;
1450 if (mon->use_readline) {
1451 mon->rs = readline_init(monitor_readline_printf,
1452 monitor_readline_flush,
1454 monitor_find_completion);
1455 monitor_read_command(mon, 0);
1458 qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, monitor_read,
1459 monitor_event, NULL, &mon->common, NULL, true);
1460 monitor_list_append(&mon->common);