Skip to content

Commit 947e474

Browse files
kevmwMarkus Armbruster
authored andcommitted
monitor: Use getter/setter functions for cur_mon
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>
1 parent 87e6f4a commit 947e474

File tree

21 files changed

+73
-46
lines changed

21 files changed

+73
-46
lines changed

audio/wavcapture.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "qemu/osdep.h"
2-
#include "monitor/monitor.h"
2+
#include "qemu/qemu-print.h"
33
#include "qapi/error.h"
44
#include "qemu/error-report.h"
55
#include "audio.h"
@@ -94,9 +94,9 @@ static void wav_capture_info (void *opaque)
9494
WAVState *wav = opaque;
9595
char *path = wav->path;
9696

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

102102
static struct capture_ops wav_capture_ops = {

dump/dump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
19861986

19871987
#if !defined(WIN32)
19881988
if (strstart(file, "fd:", &p)) {
1989-
fd = monitor_get_fd(cur_mon, p, errp);
1989+
fd = monitor_get_fd(monitor_cur(), p, errp);
19901990
if (fd == -1) {
19911991
return;
19921992
}

hw/scsi/vhost-scsi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static void vhost_scsi_realize(DeviceState *dev, Error **errp)
177177
}
178178

179179
if (vs->conf.vhostfd) {
180-
vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, errp);
180+
vhostfd = monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp);
181181
if (vhostfd == -1) {
182182
error_prepend(errp, "vhost-scsi: unable to parse vhostfd: ");
183183
return;

hw/virtio/vhost-vsock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
143143
}
144144

145145
if (vsock->conf.vhostfd) {
146-
vhostfd = monitor_fd_param(cur_mon, vsock->conf.vhostfd, errp);
146+
vhostfd = monitor_fd_param(monitor_cur(), vsock->conf.vhostfd, errp);
147147
if (vhostfd == -1) {
148148
error_prepend(errp, "vhost-vsock: unable to parse vhostfd: ");
149149
return;

include/monitor/monitor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
#include "qapi/qapi-types-misc.h"
66
#include "qemu/readline.h"
77

8-
extern __thread Monitor *cur_mon;
98
typedef struct MonitorHMP MonitorHMP;
109
typedef struct MonitorOptions MonitorOptions;
1110

1211
#define QMP_REQ_QUEUE_LEN_MAX 8
1312

1413
extern QemuOptsList qemu_mon_opts;
1514

15+
Monitor *monitor_cur(void);
16+
Monitor *monitor_set_cur(Monitor *mon);
1617
bool monitor_cur_is_qmp(void);
1718

1819
void monitor_init_globals(void);

migration/fd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Error **errp)
2727
{
2828
QIOChannel *ioc;
29-
int fd = monitor_get_fd(cur_mon, fdname, errp);
29+
int fd = monitor_get_fd(monitor_cur(), fdname, errp);
3030
if (fd == -1) {
3131
return;
3232
}
@@ -55,7 +55,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *ioc,
5555
void fd_start_incoming_migration(const char *fdname, Error **errp)
5656
{
5757
QIOChannel *ioc;
58-
int fd = monitor_fd_param(cur_mon, fdname, errp);
58+
int fd = monitor_fd_param(monitor_cur(), fdname, errp);
5959
if (fd == -1) {
6060
return;
6161
}

monitor/hmp.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,26 +1300,25 @@ static void monitor_find_completion(void *opaque,
13001300

13011301
static void monitor_read(void *opaque, const uint8_t *buf, int size)
13021302
{
1303-
MonitorHMP *mon;
1304-
Monitor *old_mon = cur_mon;
1303+
MonitorHMP *mon = container_of(opaque, MonitorHMP, common);
1304+
Monitor *old_mon;
13051305
int i;
13061306

1307-
cur_mon = opaque;
1308-
mon = container_of(cur_mon, MonitorHMP, common);
1307+
old_mon = monitor_set_cur(&mon->common);
13091308

13101309
if (mon->rs) {
13111310
for (i = 0; i < size; i++) {
13121311
readline_handle_byte(mon->rs, buf[i]);
13131312
}
13141313
} else {
13151314
if (size == 0 || buf[size - 1] != 0) {
1316-
monitor_printf(cur_mon, "corrupted command\n");
1315+
monitor_printf(&mon->common, "corrupted command\n");
13171316
} else {
13181317
handle_hmp_command(mon, (char *)buf);
13191318
}
13201319
}
13211320

1322-
cur_mon = old_mon;
1321+
monitor_set_cur(old_mon);
13231322
}
13241323

13251324
static void monitor_event(void *opaque, QEMUChrEvent event)

monitor/misc.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,20 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
125125

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

128-
old_mon = cur_mon;
129-
cur_mon = &hmp.common;
128+
old_mon = monitor_set_cur(&hmp.common);
130129

131130
if (has_cpu_index) {
132131
int ret = monitor_set_cpu(&hmp.common, cpu_index);
133132
if (ret < 0) {
134-
cur_mon = old_mon;
133+
monitor_set_cur(old_mon);
135134
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
136135
"a CPU number");
137136
goto out;
138137
}
139138
}
140139

141140
handle_hmp_command(&hmp, command_line);
142-
cur_mon = old_mon;
141+
monitor_set_cur(old_mon);
143142

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

298297
CPUState *mon_get_cpu(void)
299298
{
300-
return mon_get_cpu_sync(cur_mon, true);
299+
return mon_get_cpu_sync(monitor_cur(), true);
301300
}
302301

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

12331232
void qmp_getfd(const char *fdname, Error **errp)
12341233
{
1234+
Monitor *cur_mon = monitor_cur();
12351235
mon_fd_t *monfd;
12361236
int fd, tmp_fd;
12371237

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

12711271
void qmp_closefd(const char *fdname, Error **errp)
12721272
{
1273+
Monitor *cur_mon = monitor_cur();
12731274
mon_fd_t *monfd;
12741275
int tmp_fd;
12751276

@@ -1356,7 +1357,7 @@ AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
13561357
const char *opaque, Error **errp)
13571358
{
13581359
int fd;
1359-
Monitor *mon = cur_mon;
1360+
Monitor *mon = monitor_cur();
13601361
AddfdInfo *fdinfo;
13611362

13621363
fd = qemu_chr_fe_get_msgfd(&mon->chr);

monitor/monitor.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,31 @@ MonitorList mon_list;
6666
int mon_refcount;
6767
static bool monitor_destroyed;
6868

69-
__thread Monitor *cur_mon;
69+
static __thread Monitor *cur_monitor;
70+
71+
Monitor *monitor_cur(void)
72+
{
73+
return cur_monitor;
74+
}
75+
76+
/**
77+
* Sets a new current monitor and returns the old one.
78+
*/
79+
Monitor *monitor_set_cur(Monitor *mon)
80+
{
81+
Monitor *old_monitor = cur_monitor;
82+
83+
cur_monitor = mon;
84+
return old_monitor;
85+
}
7086

7187
/**
7288
* Is the current monitor, if any, a QMP monitor?
7389
*/
7490
bool monitor_cur_is_qmp(void)
7591
{
92+
Monitor *cur_mon = monitor_cur();
93+
7694
return cur_mon && monitor_is_qmp(cur_mon);
7795
}
7896

@@ -209,6 +227,8 @@ int monitor_printf(Monitor *mon, const char *fmt, ...)
209227
*/
210228
int error_vprintf(const char *fmt, va_list ap)
211229
{
230+
Monitor *cur_mon = monitor_cur();
231+
212232
if (cur_mon && !monitor_cur_is_qmp()) {
213233
return monitor_vprintf(cur_mon, fmt, ap);
214234
}
@@ -217,6 +237,8 @@ int error_vprintf(const char *fmt, va_list ap)
217237

218238
int error_vprintf_unless_qmp(const char *fmt, va_list ap)
219239
{
240+
Monitor *cur_mon = monitor_cur();
241+
220242
if (!cur_mon) {
221243
return vfprintf(stderr, fmt, ap);
222244
}

monitor/qmp-cmds-control.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
6969
void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
7070
Error **errp)
7171
{
72+
Monitor *cur_mon = monitor_cur();
7273
MonitorQMP *mon;
7374

7475
assert(monitor_is_qmp(cur_mon));
@@ -119,6 +120,7 @@ static void query_commands_cb(const QmpCommand *cmd, void *opaque)
119120
CommandInfoList *qmp_query_commands(Error **errp)
120121
{
121122
CommandInfoList *list = NULL;
123+
Monitor *cur_mon = monitor_cur();
122124
MonitorQMP *mon;
123125

124126
assert(monitor_is_qmp(cur_mon));

0 commit comments

Comments
 (0)