From 7e3c0deab1b76f37ac0b3199324db976a6cd1b2c Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Thu, 13 Jun 2019 17:34:00 +0200 Subject: monitor: Split out monitor/qmp.c Move QMP infrastructure from monitor/misc.c to monitor/qmp.c. This is code that can be shared for all targets, so compile it only once. The amount of function and particularly extern variables in monitor_int.h is probably a bit larger than it needs to be, but this way no non-trivial code modifications are needed. The interfaces between QMP and the monitor core can be cleaned up later. Signed-off-by: Kevin Wolf Reviewed-by: Dr. David Alan Gilbert Message-Id: <20190613153405.24769-11-kwolf@redhat.com> Reviewed-by: Markus Armbruster [monitor_is_qmp() tidied up to make checkpatch.pl happy, superfluous #include dropped] Signed-off-by: Markus Armbruster --- monitor/qmp.c | 406 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 406 insertions(+) create mode 100644 monitor/qmp.c (limited to 'monitor/qmp.c') diff --git a/monitor/qmp.c b/monitor/qmp.c new file mode 100644 index 0000000000..7c3d081a72 --- /dev/null +++ b/monitor/qmp.c @@ -0,0 +1,406 @@ +/* + * QEMU monitor + * + * Copyright (c) 2003-2004 Fabrice Bellard + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" + +#include "chardev/char-io.h" +#include "monitor-internal.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-misc.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qjson.h" +#include "qapi/qmp/qlist.h" +#include "qapi/qmp/qstring.h" +#include "trace.h" + +struct QMPRequest { + /* Owner of the request */ + MonitorQMP *mon; + /* + * Request object to be handled or Error to be reported + * (exactly one of them is non-null) + */ + QObject *req; + Error *err; +}; +typedef struct QMPRequest QMPRequest; + +QmpCommandList qmp_commands, qmp_cap_negotiation_commands; + +static bool qmp_oob_enabled(MonitorQMP *mon) +{ + return mon->capab[QMP_CAPABILITY_OOB]; +} + +static void monitor_qmp_caps_reset(MonitorQMP *mon) +{ + memset(mon->capab_offered, 0, sizeof(mon->capab_offered)); + memset(mon->capab, 0, sizeof(mon->capab)); + mon->capab_offered[QMP_CAPABILITY_OOB] = mon->common.use_io_thread; +} + +static void qmp_request_free(QMPRequest *req) +{ + qobject_unref(req->req); + error_free(req->err); + g_free(req); +} + +/* Caller must hold mon->qmp.qmp_queue_lock */ +static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon) +{ + while (!g_queue_is_empty(mon->qmp_requests)) { + qmp_request_free(g_queue_pop_head(mon->qmp_requests)); + } +} + +static void monitor_qmp_cleanup_queues(MonitorQMP *mon) +{ + qemu_mutex_lock(&mon->qmp_queue_lock); + monitor_qmp_cleanup_req_queue_locked(mon); + qemu_mutex_unlock(&mon->qmp_queue_lock); +} + +void qmp_send_response(MonitorQMP *mon, const QDict *rsp) +{ + const QObject *data = QOBJECT(rsp); + QString *json; + + json = mon->common.flags & MONITOR_USE_PRETTY ? + qobject_to_json_pretty(data) : qobject_to_json(data); + assert(json != NULL); + + qstring_append_chr(json, '\n'); + monitor_puts(&mon->common, qstring_get_str(json)); + + qobject_unref(json); +} + +/* + * Emit QMP response @rsp with ID @id to @mon. + * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP. + * Nothing is emitted then. + */ +static void monitor_qmp_respond(MonitorQMP *mon, QDict *rsp) +{ + if (rsp) { + qmp_send_response(mon, rsp); + } +} + +static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req) +{ + Monitor *old_mon; + QDict *rsp; + QDict *error; + + old_mon = cur_mon; + cur_mon = &mon->common; + + rsp = qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon)); + + cur_mon = old_mon; + + if (mon->commands == &qmp_cap_negotiation_commands) { + error = qdict_get_qdict(rsp, "error"); + if (error + && !g_strcmp0(qdict_get_try_str(error, "class"), + QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) { + /* Provide a more useful error message */ + qdict_del(error, "desc"); + qdict_put_str(error, "desc", "Expecting capabilities negotiation" + " with 'qmp_capabilities'"); + } + } + + monitor_qmp_respond(mon, rsp); + qobject_unref(rsp); +} + +/* + * Pop a QMP request from a monitor request queue. + * Return the request, or NULL all request queues are empty. + * We are using round-robin fashion to pop the request, to avoid + * processing commands only on a very busy monitor. To achieve that, + * when we process one request on a specific monitor, we put that + * monitor to the end of mon_list queue. + * + * Note: if the function returned with non-NULL, then the caller will + * be with qmp_mon->qmp_queue_lock held, and the caller is responsible + * to release it. + */ +static QMPRequest *monitor_qmp_requests_pop_any_with_lock(void) +{ + QMPRequest *req_obj = NULL; + Monitor *mon; + MonitorQMP *qmp_mon; + + qemu_mutex_lock(&monitor_lock); + + QTAILQ_FOREACH(mon, &mon_list, entry) { + if (!monitor_is_qmp(mon)) { + continue; + } + + qmp_mon = container_of(mon, MonitorQMP, common); + qemu_mutex_lock(&qmp_mon->qmp_queue_lock); + req_obj = g_queue_pop_head(qmp_mon->qmp_requests); + if (req_obj) { + /* With the lock of corresponding queue held */ + break; + } + qemu_mutex_unlock(&qmp_mon->qmp_queue_lock); + } + + if (req_obj) { + /* + * We found one request on the monitor. Degrade this monitor's + * priority to lowest by re-inserting it to end of queue. + */ + QTAILQ_REMOVE(&mon_list, mon, entry); + QTAILQ_INSERT_TAIL(&mon_list, mon, entry); + } + + qemu_mutex_unlock(&monitor_lock); + + return req_obj; +} + +void monitor_qmp_bh_dispatcher(void *data) +{ + QMPRequest *req_obj = monitor_qmp_requests_pop_any_with_lock(); + QDict *rsp; + bool need_resume; + MonitorQMP *mon; + + if (!req_obj) { + return; + } + + mon = req_obj->mon; + /* qmp_oob_enabled() might change after "qmp_capabilities" */ + need_resume = !qmp_oob_enabled(mon) || + mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1; + qemu_mutex_unlock(&mon->qmp_queue_lock); + if (req_obj->req) { + QDict *qdict = qobject_to(QDict, req_obj->req); + QObject *id = qdict ? qdict_get(qdict, "id") : NULL; + trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id) ?: ""); + monitor_qmp_dispatch(mon, req_obj->req); + } else { + assert(req_obj->err); + rsp = qmp_error_response(req_obj->err); + req_obj->err = NULL; + monitor_qmp_respond(mon, rsp); + qobject_unref(rsp); + } + + if (need_resume) { + /* Pairs with the monitor_suspend() in handle_qmp_command() */ + monitor_resume(&mon->common); + } + qmp_request_free(req_obj); + + /* Reschedule instead of looping so the main loop stays responsive */ + qemu_bh_schedule(qmp_dispatcher_bh); +} + +static void handle_qmp_command(void *opaque, QObject *req, Error *err) +{ + MonitorQMP *mon = opaque; + QObject *id = NULL; + QDict *qdict; + QMPRequest *req_obj; + + assert(!req != !err); + + qdict = qobject_to(QDict, req); + if (qdict) { + id = qdict_get(qdict, "id"); + } /* else will fail qmp_dispatch() */ + + if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) { + QString *req_json = qobject_to_json(req); + trace_handle_qmp_command(mon, qstring_get_str(req_json)); + qobject_unref(req_json); + } + + if (qdict && qmp_is_oob(qdict)) { + /* OOB commands are executed immediately */ + trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id) ?: ""); + monitor_qmp_dispatch(mon, req); + qobject_unref(req); + return; + } + + req_obj = g_new0(QMPRequest, 1); + req_obj->mon = mon; + req_obj->req = req; + req_obj->err = err; + + /* Protect qmp_requests and fetching its length. */ + qemu_mutex_lock(&mon->qmp_queue_lock); + + /* + * Suspend the monitor when we can't queue more requests after + * this one. Dequeuing in monitor_qmp_bh_dispatcher() will resume + * it. Note that when OOB is disabled, we queue at most one + * command, for backward compatibility. + */ + if (!qmp_oob_enabled(mon) || + mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX - 1) { + monitor_suspend(&mon->common); + } + + /* + * Put the request to the end of queue so that requests will be + * handled in time order. Ownership for req_obj, req, + * etc. will be delivered to the handler side. + */ + assert(mon->qmp_requests->length < QMP_REQ_QUEUE_LEN_MAX); + g_queue_push_tail(mon->qmp_requests, req_obj); + qemu_mutex_unlock(&mon->qmp_queue_lock); + + /* Kick the dispatcher routine */ + qemu_bh_schedule(qmp_dispatcher_bh); +} + +static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) +{ + MonitorQMP *mon = opaque; + + json_message_parser_feed(&mon->parser, (const char *) buf, size); +} + +static QDict *qmp_greeting(MonitorQMP *mon) +{ + QList *cap_list = qlist_new(); + QObject *ver = NULL; + QMPCapability cap; + + qmp_marshal_query_version(NULL, &ver, NULL); + + for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) { + if (mon->capab_offered[cap]) { + qlist_append_str(cap_list, QMPCapability_str(cap)); + } + } + + return qdict_from_jsonf_nofail( + "{'QMP': {'version': %p, 'capabilities': %p}}", + ver, cap_list); +} + +static void monitor_qmp_event(void *opaque, int event) +{ + QDict *data; + MonitorQMP *mon = opaque; + + switch (event) { + case CHR_EVENT_OPENED: + mon->commands = &qmp_cap_negotiation_commands; + monitor_qmp_caps_reset(mon); + data = qmp_greeting(mon); + qmp_send_response(mon, data); + qobject_unref(data); + mon_refcount++; + break; + case CHR_EVENT_CLOSED: + /* + * Note: this is only useful when the output of the chardev + * backend is still open. For example, when the backend is + * stdio, it's possible that stdout is still open when stdin + * is closed. + */ + monitor_qmp_cleanup_queues(mon); + json_message_parser_destroy(&mon->parser); + json_message_parser_init(&mon->parser, handle_qmp_command, + mon, NULL); + mon_refcount--; + monitor_fdsets_cleanup(); + break; + } +} + +void monitor_data_destroy_qmp(MonitorQMP *mon) +{ + json_message_parser_destroy(&mon->parser); + qemu_mutex_destroy(&mon->qmp_queue_lock); + monitor_qmp_cleanup_req_queue_locked(mon); + g_queue_free(mon->qmp_requests); +} + +static void monitor_qmp_setup_handlers_bh(void *opaque) +{ + MonitorQMP *mon = opaque; + GMainContext *context; + + assert(mon->common.use_io_thread); + context = iothread_get_g_main_context(mon_iothread); + assert(context); + qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, + monitor_qmp_read, monitor_qmp_event, + NULL, &mon->common, context, true); + monitor_list_append(&mon->common); +} + +void monitor_init_qmp(Chardev *chr, int flags) +{ + MonitorQMP *mon = g_new0(MonitorQMP, 1); + + /* Only HMP supports readline */ + assert(!(flags & MONITOR_USE_READLINE)); + + /* Note: we run QMP monitor in I/O thread when @chr supports that */ + monitor_data_init(&mon->common, flags, false, + qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT)); + + qemu_mutex_init(&mon->qmp_queue_lock); + mon->qmp_requests = g_queue_new(); + + qemu_chr_fe_init(&mon->common.chr, chr, &error_abort); + qemu_chr_fe_set_echo(&mon->common.chr, true); + + json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL); + if (mon->common.use_io_thread) { + /* + * Make sure the old iowatch is gone. It's possible when + * e.g. the chardev is in client mode, with wait=on. + */ + remove_fd_in_watch(chr); + /* + * We can't call qemu_chr_fe_set_handlers() directly here + * since chardev might be running in the monitor I/O + * thread. Schedule a bottom half. + */ + aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread), + monitor_qmp_setup_handlers_bh, mon); + /* The bottom half will add @mon to @mon_list */ + } else { + qemu_chr_fe_set_handlers(&mon->common.chr, monitor_can_read, + monitor_qmp_read, monitor_qmp_event, + NULL, &mon->common, NULL, true); + monitor_list_append(&mon->common); + } +} -- cgit 1.4.1 From 920824165c49bfbd1e0e9e07fd92e0bbbf32aea3 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Thu, 13 Jun 2019 17:34:03 +0200 Subject: monitor: Split Monitor.flags into separate bools Monitor.flags contains three different flags: One to distinguish HMP from QMP; one specific to HMP (MONITOR_USE_READLINE) that is ignored with QMP; and another one specific to QMP (MONITOR_USE_PRETTY) that is ignored with HMP. Split the flags field into three bools and move them to the right subclass. Flags are still in use for the monitor_init() interface. Signed-off-by: Kevin Wolf Message-Id: <20190613153405.24769-14-kwolf@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Markus Armbruster --- monitor/hmp.c | 6 +++--- monitor/misc.c | 2 +- monitor/monitor-internal.h | 8 +++++--- monitor/monitor.c | 14 +++++++++----- monitor/qmp.c | 7 ++++--- 5 files changed, 22 insertions(+), 15 deletions(-) (limited to 'monitor/qmp.c') diff --git a/monitor/hmp.c b/monitor/hmp.c index cbbee6477a..379e366984 100644 --- a/monitor/hmp.c +++ b/monitor/hmp.c @@ -1398,12 +1398,12 @@ static void monitor_readline_flush(void *opaque) void monitor_init_hmp(Chardev *chr, int flags) { MonitorHMP *mon = g_new0(MonitorHMP, 1); - bool use_readline = flags & MONITOR_USE_READLINE; - monitor_data_init(&mon->common, flags, false, false); + monitor_data_init(&mon->common, false, false, false); qemu_chr_fe_init(&mon->common.chr, chr, &error_abort); - if (use_readline) { + mon->use_readline = flags & MONITOR_USE_READLINE; + if (mon->use_readline) { mon->rs = readline_init(monitor_readline_printf, monitor_readline_flush, mon, diff --git a/monitor/misc.c b/monitor/misc.c index 6e3d580ae7..bf9faceb86 100644 --- a/monitor/misc.c +++ b/monitor/misc.c @@ -121,7 +121,7 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, Monitor *old_mon; MonitorHMP hmp = {}; - monitor_data_init(&hmp.common, 0, true, false); + monitor_data_init(&hmp.common, false, true, false); old_mon = cur_mon; cur_mon = &hmp.common; diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h index b4996c14ac..03ea0239ef 100644 --- a/monitor/monitor-internal.h +++ b/monitor/monitor-internal.h @@ -86,8 +86,8 @@ typedef struct HMPCommand { struct Monitor { CharBackend chr; int reset_seen; - int flags; int suspend_cnt; /* Needs to be accessed atomically */ + bool is_qmp; bool skip_flush; bool use_io_thread; @@ -112,6 +112,7 @@ struct Monitor { struct MonitorHMP { Monitor common; + bool use_readline; /* * State used only in the thread "owning" the monitor. * If @use_io_thread, this is @mon_iothread. (This does not actually happen @@ -125,6 +126,7 @@ struct MonitorHMP { typedef struct { Monitor common; JSONMessageParser parser; + bool pretty; /* * When a client connects, we're in capabilities negotiation mode. * @commands is &qmp_cap_negotiation_commands then. When command @@ -148,7 +150,7 @@ typedef struct { */ static inline bool monitor_is_qmp(const Monitor *mon) { - return mon->flags & MONITOR_USE_CONTROL; + return mon->is_qmp; } typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList; @@ -165,7 +167,7 @@ void monitor_init_qmp(Chardev *chr, int flags); void monitor_init_hmp(Chardev *chr, int flags); int monitor_puts(Monitor *mon, const char *str); -void monitor_data_init(Monitor *mon, int flags, bool skip_flush, +void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush, bool use_io_thread); void monitor_data_destroy(Monitor *mon); int monitor_can_read(void *opaque); diff --git a/monitor/monitor.c b/monitor/monitor.c index db3d5ece99..3f4808240a 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -78,14 +78,18 @@ bool monitor_cur_is_qmp(void) * Note: not all HMP monitors use readline, e.g., gdbserver has a * non-interactive HMP monitor, so readline is not used there. */ -static inline bool monitor_uses_readline(const Monitor *mon) +static inline bool monitor_uses_readline(const MonitorHMP *mon) { - return mon->flags & MONITOR_USE_READLINE; + return mon->use_readline; } static inline bool monitor_is_hmp_non_interactive(const Monitor *mon) { - return !monitor_is_qmp(mon) && !monitor_uses_readline(mon); + if (monitor_is_qmp(mon)) { + return false; + } + + return !monitor_uses_readline(container_of(mon, MonitorHMP, common)); } static void monitor_flush_locked(Monitor *mon); @@ -521,17 +525,17 @@ static void monitor_iothread_init(void) mon_iothread = iothread_create("mon_iothread", &error_abort); } -void monitor_data_init(Monitor *mon, int flags, bool skip_flush, +void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush, bool use_io_thread) { if (use_io_thread && !mon_iothread) { monitor_iothread_init(); } qemu_mutex_init(&mon->mon_lock); + mon->is_qmp = is_qmp; mon->outbuf = qstring_new(); mon->skip_flush = skip_flush; mon->use_io_thread = use_io_thread; - mon->flags = flags; } void monitor_data_destroy(Monitor *mon) diff --git a/monitor/qmp.c b/monitor/qmp.c index 7c3d081a72..940649f688 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -87,8 +87,7 @@ void qmp_send_response(MonitorQMP *mon, const QDict *rsp) const QObject *data = QOBJECT(rsp); QString *json; - json = mon->common.flags & MONITOR_USE_PRETTY ? - qobject_to_json_pretty(data) : qobject_to_json(data); + json = mon->pretty ? qobject_to_json_pretty(data) : qobject_to_json(data); assert(json != NULL); qstring_append_chr(json, '\n'); @@ -373,9 +372,11 @@ void monitor_init_qmp(Chardev *chr, int flags) assert(!(flags & MONITOR_USE_READLINE)); /* Note: we run QMP monitor in I/O thread when @chr supports that */ - monitor_data_init(&mon->common, flags, false, + monitor_data_init(&mon->common, true, false, qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT)); + mon->pretty = flags & MONITOR_USE_PRETTY; + qemu_mutex_init(&mon->qmp_queue_lock); mon->qmp_requests = g_queue_new(); -- cgit 1.4.1 From fbfc29e3bf145581e84c12ffc432ab56ce1dea0d Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Thu, 13 Jun 2019 17:34:04 +0200 Subject: monitor: Replace monitor_init() with monitor_init_{hmp, qmp}() Most callers know which monitor type they want to have. Instead of calling monitor_init() with flags that can describe both types of monitors, make monitor_init_{hmp,qmp}() public interfaces that take specific bools instead of flags and call these functions directly. Signed-off-by: Kevin Wolf Message-Id: <20190613153405.24769-15-kwolf@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Markus Armbruster --- chardev/char.c | 2 +- gdbstub.c | 2 +- include/monitor/monitor.h | 9 ++------- monitor/hmp.c | 4 ++-- monitor/monitor-internal.h | 3 --- monitor/monitor.c | 9 --------- monitor/qmp.c | 7 ++----- stubs/monitor.c | 6 +++++- tests/test-util-sockets.c | 3 ++- vl.c | 18 ++++++++++++------ 10 files changed, 27 insertions(+), 36 deletions(-) (limited to 'monitor/qmp.c') diff --git a/chardev/char.c b/chardev/char.c index e4887bcc82..7b6b2cb123 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -731,7 +731,7 @@ Chardev *qemu_chr_new_noreplay(const char *label, const char *filename, if (qemu_opt_get_bool(opts, "mux", 0)) { assert(permit_mux_mon); - monitor_init(chr, MONITOR_USE_READLINE); + monitor_init_hmp(chr, true); } out: diff --git a/gdbstub.c b/gdbstub.c index d614a1f3c0..8618e34311 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -3344,7 +3344,7 @@ int gdbserver_start(const char *device) /* Initialize a monitor terminal for gdb */ mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, NULL, NULL, &error_abort); - monitor_init(mon_chr, 0); + monitor_init_hmp(mon_chr, false); } else { qemu_chr_fe_deinit(&s->chr, true); mon_chr = s->mon_chr; diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 44ac43df34..a81eeff5f8 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -8,19 +8,14 @@ extern __thread Monitor *cur_mon; typedef struct MonitorHMP MonitorHMP; -/* flags for monitor_init */ -/* 0x01 unused */ -#define MONITOR_USE_READLINE 0x02 -#define MONITOR_USE_CONTROL 0x04 -#define MONITOR_USE_PRETTY 0x08 - #define QMP_REQ_QUEUE_LEN_MAX 8 bool monitor_cur_is_qmp(void); void monitor_init_globals(void); void monitor_init_globals_core(void); -void monitor_init(Chardev *chr, int flags); +void monitor_init_qmp(Chardev *chr, bool pretty); +void monitor_init_hmp(Chardev *chr, bool use_readline); void monitor_cleanup(void); int monitor_suspend(Monitor *mon); diff --git a/monitor/hmp.c b/monitor/hmp.c index 379e366984..5223661e82 100644 --- a/monitor/hmp.c +++ b/monitor/hmp.c @@ -1395,14 +1395,14 @@ static void monitor_readline_flush(void *opaque) monitor_flush(&mon->common); } -void monitor_init_hmp(Chardev *chr, int flags) +void monitor_init_hmp(Chardev *chr, bool use_readline) { MonitorHMP *mon = g_new0(MonitorHMP, 1); monitor_data_init(&mon->common, false, false, false); qemu_chr_fe_init(&mon->common.chr, chr, &error_abort); - mon->use_readline = flags & MONITOR_USE_READLINE; + mon->use_readline = use_readline; if (mon->use_readline) { mon->rs = readline_init(monitor_readline_printf, monitor_readline_flush, diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h index 03ea0239ef..7760b22ba3 100644 --- a/monitor/monitor-internal.h +++ b/monitor/monitor-internal.h @@ -163,9 +163,6 @@ extern int mon_refcount; extern HMPCommand hmp_cmds[]; -void monitor_init_qmp(Chardev *chr, int flags); -void monitor_init_hmp(Chardev *chr, int flags); - int monitor_puts(Monitor *mon, const char *str); void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush, bool use_io_thread); diff --git a/monitor/monitor.c b/monitor/monitor.c index 3f4808240a..3ef28171c0 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -551,15 +551,6 @@ void monitor_data_destroy(Monitor *mon) qemu_mutex_destroy(&mon->mon_lock); } -void monitor_init(Chardev *chr, int flags) -{ - if (flags & MONITOR_USE_CONTROL) { - monitor_init_qmp(chr, flags); - } else { - monitor_init_hmp(chr, flags); - } -} - void monitor_cleanup(void) { /* diff --git a/monitor/qmp.c b/monitor/qmp.c index 940649f688..e1b196217d 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -364,18 +364,15 @@ static void monitor_qmp_setup_handlers_bh(void *opaque) monitor_list_append(&mon->common); } -void monitor_init_qmp(Chardev *chr, int flags) +void monitor_init_qmp(Chardev *chr, bool pretty) { MonitorQMP *mon = g_new0(MonitorQMP, 1); - /* Only HMP supports readline */ - assert(!(flags & MONITOR_USE_READLINE)); - /* Note: we run QMP monitor in I/O thread when @chr supports that */ monitor_data_init(&mon->common, true, false, qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_GCONTEXT)); - mon->pretty = flags & MONITOR_USE_PRETTY; + mon->pretty = pretty; qemu_mutex_init(&mon->qmp_queue_lock); mon->qmp_requests = g_queue_new(); diff --git a/stubs/monitor.c b/stubs/monitor.c index cdbf5c5f9a..c3e9a2e4dc 100644 --- a/stubs/monitor.c +++ b/stubs/monitor.c @@ -16,7 +16,11 @@ int monitor_get_fd(Monitor *mon, const char *name, Error **errp) return -1; } -void monitor_init(Chardev *chr, int flags) +void monitor_init_qmp(Chardev *chr, bool pretty) +{ +} + +void monitor_init_hmp(Chardev *chr, bool use_readline) { } diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c index fd1ced058c..f1ebffee5a 100644 --- a/tests/test-util-sockets.c +++ b/tests/test-util-sockets.c @@ -71,7 +71,8 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) */ __thread Monitor *cur_mon; int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); } -void monitor_init(Chardev *chr, int flags) {} +void monitor_init_qmp(Chardev *chr, bool pretty) {} +void monitor_init_hmp(Chardev *chr, bool use_readline) {} static void test_socket_fd_pass_name_good(void) diff --git a/vl.c b/vl.c index 005468cbfb..32daa434eb 100644 --- a/vl.c +++ b/vl.c @@ -2299,25 +2299,27 @@ static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp) static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp) { Chardev *chr; + bool qmp; + bool pretty = false; const char *chardev; const char *mode; - int flags; mode = qemu_opt_get(opts, "mode"); if (mode == NULL) { mode = "readline"; } if (strcmp(mode, "readline") == 0) { - flags = MONITOR_USE_READLINE; + qmp = false; } else if (strcmp(mode, "control") == 0) { - flags = MONITOR_USE_CONTROL; + qmp = true; } else { error_setg(errp, "unknown monitor mode \"%s\"", mode); return -1; } - if (qemu_opt_get_bool(opts, "pretty", 0)) - flags |= MONITOR_USE_PRETTY; + if (qemu_opt_get_bool(opts, "pretty", 0)) { + pretty = true; + } chardev = qemu_opt_get(opts, "chardev"); if (!chardev) { @@ -2330,7 +2332,11 @@ static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp) return -1; } - monitor_init(chr, flags); + if (qmp) { + monitor_init_qmp(chr, pretty); + } else { + monitor_init_hmp(chr, true); + } return 0; } -- cgit 1.4.1