Skip to content

Commit

Permalink
monitor: Use getter/setter functions for cur_mon
Browse files Browse the repository at this point in the history
cur_mon really needs to be coroutine-local as soon as we move monitor
command handlers to coroutines and let them yield. As a first step, just
remove all direct accesses to cur_mon so that we can implement this in
the getter function later.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20201005155855.256490-4-kwolf@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
  • Loading branch information
kevmw authored and Markus Armbruster committed Oct 9, 2020
1 parent 87e6f4a commit 947e474
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 46 deletions.
8 changes: 4 additions & 4 deletions audio/wavcapture.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "qemu/osdep.h"
#include "monitor/monitor.h"
#include "qemu/qemu-print.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "audio.h"
Expand Down Expand Up @@ -94,9 +94,9 @@ static void wav_capture_info (void *opaque)
WAVState *wav = opaque;
char *path = wav->path;

monitor_printf (cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
wav->freq, wav->bits, wav->nchannels,
path ? path : "<not available>", wav->bytes);
qemu_printf("Capturing audio(%d,%d,%d) to %s: %d bytes\n",
wav->freq, wav->bits, wav->nchannels,
path ? path : "<not available>", wav->bytes);
}

static struct capture_ops wav_capture_ops = {
Expand Down
2 changes: 1 addition & 1 deletion dump/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,

#if !defined(WIN32)
if (strstart(file, "fd:", &p)) {
fd = monitor_get_fd(cur_mon, p, errp);
fd = monitor_get_fd(monitor_cur(), p, errp);
if (fd == -1) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion hw/scsi/vhost-scsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
}

if (vs->conf.vhostfd) {
vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, errp);
vhostfd = monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp);
if (vhostfd == -1) {
error_prepend(errp, "vhost-scsi: unable to parse vhostfd: ");
return;
Expand Down
2 changes: 1 addition & 1 deletion hw/virtio/vhost-vsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
}

if (vsock->conf.vhostfd) {
vhostfd = monitor_fd_param(cur_mon, vsock->conf.vhostfd, errp);
vhostfd = monitor_fd_param(monitor_cur(), vsock->conf.vhostfd, errp);
if (vhostfd == -1) {
error_prepend(errp, "vhost-vsock: unable to parse vhostfd: ");
return;
Expand Down
3 changes: 2 additions & 1 deletion include/monitor/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include "qapi/qapi-types-misc.h"
#include "qemu/readline.h"

extern __thread Monitor *cur_mon;
typedef struct MonitorHMP MonitorHMP;
typedef struct MonitorOptions MonitorOptions;

#define QMP_REQ_QUEUE_LEN_MAX 8

extern QemuOptsList qemu_mon_opts;

Monitor *monitor_cur(void);
Monitor *monitor_set_cur(Monitor *mon);
bool monitor_cur_is_qmp(void);

void monitor_init_globals(void);
Expand Down
4 changes: 2 additions & 2 deletions migration/fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
{
QIOChannel *ioc;
int fd = monitor_get_fd(cur_mon, fdname, errp);
int fd = monitor_get_fd(monitor_cur(), fdname, errp);
if (fd == -1) {
return;
}
Expand Down Expand Up @@ -55,7 +55,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
void fd_start_incoming_migration(const char *fdname, Error **errp)
{
QIOChannel *ioc;
int fd = monitor_fd_param(cur_mon, fdname, errp);
int fd = monitor_fd_param(monitor_cur(), fdname, errp);
if (fd == -1) {
return;
}
Expand Down
11 changes: 5 additions & 6 deletions monitor/hmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,26 +1300,25 @@ static void monitor_find_completion(void *opaque,

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);
Monitor *old_mon;
int i;

cur_mon = opaque;
mon = container_of(cur_mon, MonitorHMP, common);
old_mon = monitor_set_cur(&mon->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;
monitor_set_cur(old_mon);
}

static void monitor_event(void *opaque, QEMUChrEvent event)
Expand Down
13 changes: 7 additions & 6 deletions monitor/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,20 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,

monitor_data_init(&hmp.common, false, true, false);

old_mon = cur_mon;
cur_mon = &hmp.common;
old_mon = monitor_set_cur(&hmp.common);

if (has_cpu_index) {
int ret = monitor_set_cpu(&hmp.common, cpu_index);
if (ret < 0) {
cur_mon = old_mon;
monitor_set_cur(old_mon);
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
"a CPU number");
goto out;
}
}

handle_hmp_command(&hmp, command_line);
cur_mon = old_mon;
monitor_set_cur(old_mon);

WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
if (qstring_get_length(hmp.common.outbuf) > 0) {
Expand Down Expand Up @@ -297,7 +296,7 @@ static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)

CPUState *mon_get_cpu(void)
{
return mon_get_cpu_sync(cur_mon, true);
return mon_get_cpu_sync(monitor_cur(), true);
}

CPUArchState *mon_get_cpu_env(void)
Expand Down Expand Up @@ -1232,6 +1231,7 @@ static void hmp_acl_remove(Monitor *mon, const QDict *qdict)

void qmp_getfd(const char *fdname, Error **errp)
{
Monitor *cur_mon = monitor_cur();
mon_fd_t *monfd;
int fd, tmp_fd;

Expand Down Expand Up @@ -1270,6 +1270,7 @@ void qmp_getfd(const char *fdname, Error **errp)

void qmp_closefd(const char *fdname, Error **errp)
{
Monitor *cur_mon = monitor_cur();
mon_fd_t *monfd;
int tmp_fd;

Expand Down Expand Up @@ -1356,7 +1357,7 @@ AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
const char *opaque, Error **errp)
{
int fd;
Monitor *mon = cur_mon;
Monitor *mon = monitor_cur();
AddfdInfo *fdinfo;

fd = qemu_chr_fe_get_msgfd(&mon->chr);
Expand Down
24 changes: 23 additions & 1 deletion monitor/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,31 @@ MonitorList mon_list;
int mon_refcount;
static bool monitor_destroyed;

__thread Monitor *cur_mon;
static __thread Monitor *cur_monitor;

Monitor *monitor_cur(void)
{
return cur_monitor;
}

/**
* Sets a new current monitor and returns the old one.
*/
Monitor *monitor_set_cur(Monitor *mon)
{
Monitor *old_monitor = cur_monitor;

cur_monitor = mon;
return old_monitor;
}

/**
* Is the current monitor, if any, a QMP monitor?
*/
bool monitor_cur_is_qmp(void)
{
Monitor *cur_mon = monitor_cur();

return cur_mon && monitor_is_qmp(cur_mon);
}

Expand Down Expand Up @@ -209,6 +227,8 @@ int monitor_printf(Monitor *mon, const char *fmt, ...)
*/
int error_vprintf(const char *fmt, va_list ap)
{
Monitor *cur_mon = monitor_cur();

if (cur_mon && !monitor_cur_is_qmp()) {
return monitor_vprintf(cur_mon, fmt, ap);
}
Expand All @@ -217,6 +237,8 @@ int error_vprintf(const char *fmt, va_list ap)

int error_vprintf_unless_qmp(const char *fmt, va_list ap)
{
Monitor *cur_mon = monitor_cur();

if (!cur_mon) {
return vfprintf(stderr, fmt, ap);
}
Expand Down
2 changes: 2 additions & 0 deletions monitor/qmp-cmds-control.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
Error **errp)
{
Monitor *cur_mon = monitor_cur();
MonitorQMP *mon;

assert(monitor_is_qmp(cur_mon));
Expand Down Expand Up @@ -119,6 +120,7 @@ static void query_commands_cb(const QmpCommand *cmd, void *opaque)
CommandInfoList *qmp_query_commands(Error **errp)
{
CommandInfoList *list = NULL;
Monitor *cur_mon = monitor_cur();
MonitorQMP *mon;

assert(monitor_is_qmp(cur_mon));
Expand Down
2 changes: 1 addition & 1 deletion monitor/qmp-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ void qmp_add_client(const char *protocol, const char *fdname,
Chardev *s;
int fd;

fd = monitor_get_fd(cur_mon, fdname, errp);
fd = monitor_get_fd(monitor_cur(), fdname, errp);
if (fd < 0) {
return;
}
Expand Down
7 changes: 2 additions & 5 deletions monitor/qmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,9 @@ static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req)
QDict *rsp;
QDict *error;

old_mon = cur_mon;
cur_mon = &mon->common;

old_mon = monitor_set_cur(&mon->common);
rsp = qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon));

cur_mon = old_mon;
monitor_set_cur(old_mon);

if (mon->commands == &qmp_cap_negotiation_commands) {
error = qdict_get_qdict(rsp, "error");
Expand Down
2 changes: 1 addition & 1 deletion net/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ int net_init_socket(const Netdev *netdev, const char *name,
if (sock->has_fd) {
int fd, ret;

fd = monitor_fd_param(cur_mon, sock->fd, errp);
fd = monitor_fd_param(monitor_cur(), sock->fd, errp);
if (fd == -1) {
return -1;
}
Expand Down
6 changes: 3 additions & 3 deletions net/tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
if (vhostfdname) {
int ret;

vhostfd = monitor_fd_param(cur_mon, vhostfdname, &err);
vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err);
if (vhostfd == -1) {
if (tap->has_vhostforce && tap->vhostforce) {
error_propagate(errp, err);
Expand Down Expand Up @@ -808,7 +808,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
return -1;
}

fd = monitor_fd_param(cur_mon, tap->fd, errp);
fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
if (fd == -1) {
return -1;
}
Expand Down Expand Up @@ -862,7 +862,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
}

for (i = 0; i < nfds; i++) {
fd = monitor_fd_param(cur_mon, fds[i], errp);
fd = monitor_fd_param(monitor_cur(), fds[i], errp);
if (fd == -1) {
ret = -1;
goto free_fail;
Expand Down
2 changes: 1 addition & 1 deletion softmmu/cpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,6 @@ void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,

void qmp_inject_nmi(Error **errp)
{
nmi_monitor_handle(monitor_get_cpu_index(cur_mon), errp);
nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp);
}

5 changes: 4 additions & 1 deletion stubs/monitor-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include "qemu-common.h"
#include "qapi/qapi-emit-events.h"

__thread Monitor *cur_mon;
Monitor *monitor_cur(void)
{
return NULL;
}

void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp)
{
Expand Down
12 changes: 6 additions & 6 deletions tests/test-util-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static void test_fd_is_socket_good(void)

static int mon_fd = -1;
static const char *mon_fdname;
__thread Monitor *cur_mon;

int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
{
Expand All @@ -65,15 +66,14 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
}

/*
* Syms of stubs in libqemuutil.a are discarded at .o file granularity.
* To replace monitor_get_fd() we must ensure everything in
* stubs/monitor.c is defined, to make sure monitor.o is discarded
* Syms of stubs in libqemuutil.a are discarded at .o file
* granularity. To replace monitor_get_fd() and monitor_cur(), we
* must ensure that we also replace any other symbol that is used in
* the binary and would be taken from the same stub object file,
* otherwise we get duplicate syms at link time.
*/
__thread Monitor *cur_mon;
Monitor *monitor_cur(void) { return cur_mon; }
int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); }
void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) {}
void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) {}

#ifndef _WIN32
static void test_socket_fd_pass_name_good(void)
Expand Down
2 changes: 1 addition & 1 deletion trace/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void trace_enable_events(const char *line_buf)
{
if (is_help_option(line_buf)) {
trace_list_events();
if (cur_mon == NULL) {
if (monitor_cur() == NULL) {
exit(0);
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions util/qemu-error.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static void print_loc(void)
int i;
const char *const *argp;

if (!cur_mon && progname) {
if (!monitor_cur() && progname) {
fprintf(stderr, "%s:", progname);
sep = " ";
}
Expand Down Expand Up @@ -208,15 +208,15 @@ static void vreport(report_type type, const char *fmt, va_list ap)
GTimeVal tv;
gchar *timestr;

if (error_with_timestamp && !cur_mon) {
if (error_with_timestamp && !monitor_cur()) {
g_get_current_time(&tv);
timestr = g_time_val_to_iso8601(&tv);
error_printf("%s ", timestr);
g_free(timestr);
}

/* Only prepend guest name if -msg guest-name and -name guest=... are set */
if (error_with_guestname && error_guest_name && !cur_mon) {
if (error_with_guestname && error_guest_name && !monitor_cur()) {
error_printf("%s ", error_guest_name);
}

Expand Down
Loading

0 comments on commit 947e474

Please sign in to comment.