#include "qemu/osdep.h"
#include <dirent.h>
+#include "hw/qdev-core.h"
#include "monitor-internal.h"
+#include "monitor/hmp.h"
#include "qapi/error.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qnum.h"
return strchr(cmd->flags, 'p');
}
+static bool cmd_available(const HMPCommand *cmd)
+{
+ return phase_check(PHASE_MACHINE_READY) || cmd_can_preconfig(cmd);
+}
+
static void help_cmd_dump_one(Monitor *mon,
const HMPCommand *cmd,
char **prefix_args,
{
int i;
- if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
+ if (!cmd_available(cmd)) {
return;
}
/* Find one entry to dump */
for (cmd = cmds; cmd->name != NULL; cmd++) {
if (hmp_compare_cmd(args[arg_index], cmd->name) &&
- ((!runstate_check(RUN_STATE_PRECONFIG) ||
- cmd_can_preconfig(cmd)))) {
+ cmd_available(cmd)) {
if (cmd->sub_table) {
/* continue with next arg */
help_cmd_dump(mon, cmd->sub_table,
static const char *pch;
static sigjmp_buf expr_env;
-static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
+static void G_GNUC_PRINTF(2, 3) QEMU_NORETURN
expr_error(Monitor *mon, const char *fmt, ...)
{
va_list ap;
pch++;
}
*q = 0;
- ret = get_monitor_def(®, buf);
+ ret = get_monitor_def(mon, ®, buf);
if (ret < 0) {
expr_error(mon, "unknown register");
}
(int)(p - cmdp_start), cmdp_start);
return NULL;
}
- if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) {
- monitor_printf(mon, "Command '%.*s' not available with -preconfig "
- "until after exit_preconfig.\n",
+ if (!cmd_available(cmd)) {
+ monitor_printf(mon, "Command '%.*s' not available "
+ "until machine initialization has completed.\n",
(int)(p - cmdp_start), cmdp_start);
return NULL;
}
{
const char *tmp = p;
int skip_key = 0;
+ int ret;
/* option */
c = *typestr++;
}
if (skip_key) {
p = tmp;
+ } else if (*typestr == 's') {
+ /* has option with string value */
+ typestr++;
+ tmp = p++;
+ while (qemu_isspace(*p)) {
+ p++;
+ }
+ ret = get_str(buf, sizeof(buf), &p);
+ if (ret < 0) {
+ monitor_printf(mon, "%s: value expected for -%c\n",
+ cmd->name, *tmp);
+ goto fail;
+ }
+ qdict_put_str(qdict, key, buf);
} else {
- /* has option */
+ /* has boolean option */
p++;
qdict_put_bool(qdict, key, true);
}
+ } else if (*typestr == 's') {
+ typestr++;
}
}
break;
return NULL;
}
+static void hmp_info_human_readable_text(Monitor *mon,
+ HumanReadableText *(*handler)(Error **))
+{
+ Error *err = NULL;
+ g_autoptr(HumanReadableText) info = handler(&err);
+
+ if (hmp_handle_error(mon, err)) {
+ return;
+ }
+
+ monitor_printf(mon, "%s", info->human_readable_text);
+}
+
+static void handle_hmp_command_exec(Monitor *mon,
+ const HMPCommand *cmd,
+ QDict *qdict)
+{
+ if (cmd->cmd_info_hrt) {
+ hmp_info_human_readable_text(mon,
+ cmd->cmd_info_hrt);
+ } else {
+ cmd->cmd(mon, qdict);
+ }
+}
+
+typedef struct HandleHmpCommandCo {
+ Monitor *mon;
+ const HMPCommand *cmd;
+ QDict *qdict;
+ bool done;
+} HandleHmpCommandCo;
+
+static void handle_hmp_command_co(void *opaque)
+{
+ HandleHmpCommandCo *data = opaque;
+ handle_hmp_command_exec(data->mon, data->cmd, data->qdict);
+ monitor_set_cur(qemu_coroutine_self(), NULL);
+ data->done = true;
+}
+
void handle_hmp_command(MonitorHMP *mon, const char *cmdline)
{
QDict *qdict;
return;
}
+ if (!cmd->cmd && !cmd->cmd_info_hrt) {
+ /* FIXME: is it useful to try autoload modules here ??? */
+ monitor_printf(&mon->common, "Command \"%.*s\" is not available.\n",
+ (int)(cmdline - cmd_start), cmd_start);
+ return;
+ }
+
qdict = monitor_parse_arguments(&mon->common, &cmdline, cmd);
if (!qdict) {
while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) {
return;
}
- cmd->cmd(&mon->common, qdict);
+ if (!cmd->coroutine) {
+ /* old_mon is non-NULL when called from qmp_human_monitor_command() */
+ Monitor *old_mon = monitor_set_cur(qemu_coroutine_self(), &mon->common);
+ handle_hmp_command_exec(&mon->common, cmd, qdict);
+ monitor_set_cur(qemu_coroutine_self(), old_mon);
+ } else {
+ HandleHmpCommandCo data = {
+ .mon = &mon->common,
+ .cmd = cmd,
+ .qdict = qdict,
+ .done = false,
+ };
+ Coroutine *co = qemu_coroutine_create(handle_hmp_command_co, &data);
+ monitor_set_cur(co, &mon->common);
+ aio_co_enter(qemu_get_aio_context(), co);
+ AIO_WAIT_WHILE(qemu_get_aio_context(), !data.done);
+ }
+
qobject_unref(qdict);
}
}
readline_set_completion_index(mon->rs, strlen(cmdname));
for (cmd = cmd_table; cmd->name != NULL; cmd++) {
- if (!runstate_check(RUN_STATE_PRECONFIG) ||
- cmd_can_preconfig(cmd)) {
+ if (cmd_available(cmd)) {
cmd_completion(mon, cmdname, cmd->name);
}
}
/* find the command */
for (cmd = cmd_table; cmd->name != NULL; cmd++) {
if (hmp_compare_cmd(args[0], cmd->name) &&
- (!runstate_check(RUN_STATE_PRECONFIG) ||
- cmd_can_preconfig(cmd))) {
+ cmd_available(cmd)) {
break;
}
}
static void monitor_read(void *opaque, const uint8_t *buf, int size)
{
- MonitorHMP *mon;
- Monitor *old_mon = cur_mon;
+ MonitorHMP *mon = container_of(opaque, MonitorHMP, common);
int i;
- cur_mon = opaque;
- mon = container_of(cur_mon, MonitorHMP, common);
-
if (mon->rs) {
for (i = 0; i < size; i++) {
readline_handle_byte(mon->rs, buf[i]);
}
} else {
if (size == 0 || buf[size - 1] != 0) {
- monitor_printf(cur_mon, "corrupted command\n");
+ monitor_printf(&mon->common, "corrupted command\n");
} else {
handle_hmp_command(mon, (char *)buf);
}
}
-
- cur_mon = old_mon;
}
-static void monitor_event(void *opaque, int event)
+static void monitor_event(void *opaque, QEMUChrEvent event)
{
Monitor *mon = opaque;
MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
monitor_resume(mon);
monitor_flush(mon);
} else {
- atomic_mb_set(&mon->suspend_cnt, 0);
+ qatomic_mb_set(&mon->suspend_cnt, 0);
}
break;
case CHR_EVENT_MUX_OUT:
if (mon->reset_seen) {
- if (atomic_mb_read(&mon->suspend_cnt) == 0) {
+ if (qatomic_mb_read(&mon->suspend_cnt) == 0) {
monitor_printf(mon, "\n");
}
monitor_flush(mon);
monitor_suspend(mon);
} else {
- atomic_inc(&mon->suspend_cnt);
+ qatomic_inc(&mon->suspend_cnt);
}
qemu_mutex_lock(&mon->mon_lock);
mon->mux_out = 1;
mon_refcount--;
monitor_fdsets_cleanup();
break;
+
+ case CHR_EVENT_BREAK:
+ /* Ignored */
+ break;
}
}
* These functions just adapt the readline interface in a typesafe way. We
* could cast function pointers but that discards compiler checks.
*/
-static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
+static void G_GNUC_PRINTF(2, 3) monitor_readline_printf(void *opaque,
const char *fmt, ...)
{
MonitorHMP *mon = opaque;
monitor_flush(&mon->common);
}
-void monitor_init_hmp(Chardev *chr, bool use_readline)
+void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp)
{
MonitorHMP *mon = g_new0(MonitorHMP, 1);
+ if (!qemu_chr_fe_init(&mon->common.chr, chr, errp)) {
+ g_free(mon);
+ return;
+ }
+
monitor_data_init(&mon->common, false, false, false);
- qemu_chr_fe_init(&mon->common.chr, chr, &error_abort);
mon->use_readline = use_readline;
if (mon->use_readline) {