]>
Commit | Line | Data |
---|---|---|
9dc39cba FB |
1 | /* |
2 | * QEMU monitor | |
5fafdf24 | 3 | * |
9dc39cba | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
9dc39cba FB |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
e688df6b | 24 | |
d38ea87a | 25 | #include "qemu/osdep.h" |
8ec338ac | 26 | #include "qemu/units.h" |
511d2b14 | 27 | #include <dirent.h> |
33c11879 | 28 | #include "cpu.h" |
87ecb68b | 29 | #include "hw/hw.h" |
b4a42f81 | 30 | #include "monitor/qdev.h" |
87ecb68b | 31 | #include "hw/usb.h" |
a2cb15b0 | 32 | #include "hw/pci/pci.h" |
0d09e41a | 33 | #include "sysemu/watchdog.h" |
45a50b16 | 34 | #include "hw/loader.h" |
022c62cb | 35 | #include "exec/gdbstub.h" |
1422e32d | 36 | #include "net/net.h" |
68ac40d2 | 37 | #include "net/slirp.h" |
4d43a603 | 38 | #include "chardev/char-fe.h" |
a5ed3525 | 39 | #include "chardev/char-io.h" |
be933ffc | 40 | #include "chardev/char-mux.h" |
7572150c | 41 | #include "ui/qemu-spice.h" |
e35704ba | 42 | #include "sysemu/numa.h" |
83c9089e | 43 | #include "monitor/monitor.h" |
213dcb06 | 44 | #include "qemu/config-file.h" |
0150cd81 | 45 | #include "qemu/readline.h" |
28ecbaee | 46 | #include "ui/console.h" |
c751a74a | 47 | #include "ui/input.h" |
da27a00e | 48 | #include "sysemu/block-backend.h" |
87ecb68b | 49 | #include "audio/audio.h" |
76cad711 | 50 | #include "disas/disas.h" |
9c17d615 | 51 | #include "sysemu/balloon.h" |
1de7afc9 | 52 | #include "qemu/timer.h" |
b3946626 | 53 | #include "sysemu/hw_accel.h" |
1de7afc9 | 54 | #include "qemu/acl.h" |
bdee56f5 | 55 | #include "sysemu/tpm.h" |
452fcdbc | 56 | #include "qapi/qmp/qdict.h" |
cc7a8ea7 | 57 | #include "qapi/qmp/qerror.h" |
15280c36 | 58 | #include "qapi/qmp/qnum.h" |
fc81fa1e | 59 | #include "qapi/qmp/qstring.h" |
7b1b5d19 PB |
60 | #include "qapi/qmp/qjson.h" |
61 | #include "qapi/qmp/json-streamer.h" | |
62 | #include "qapi/qmp/json-parser.h" | |
02130314 | 63 | #include "qapi/qmp/qlist.h" |
a9c94277 | 64 | #include "qom/object_interfaces.h" |
0ab8ed18 | 65 | #include "trace-root.h" |
31965ae2 | 66 | #include "trace/control.h" |
bf957284 | 67 | #include "monitor/hmp-target.h" |
6d8a764e | 68 | #ifdef CONFIG_TRACE_SIMPLE |
31965ae2 | 69 | #include "trace/simple.h" |
22890ab5 | 70 | #endif |
022c62cb | 71 | #include "exec/memory.h" |
63c91552 | 72 | #include "exec/exec-all.h" |
03dd024f | 73 | #include "qemu/log.h" |
922a01a0 | 74 | #include "qemu/option.h" |
48a32bed | 75 | #include "hmp.h" |
1de7afc9 | 76 | #include "qemu/thread.h" |
b21631f3 | 77 | #include "block/qapi.h" |
eb815e24 MA |
78 | #include "qapi/qapi-commands.h" |
79 | #include "qapi/qapi-events.h" | |
e688df6b | 80 | #include "qapi/error.h" |
43a14cfc | 81 | #include "qapi/qmp-event.h" |
eb815e24 | 82 | #include "qapi/qapi-introspect.h" |
dc599978 | 83 | #include "sysemu/qtest.h" |
d2528bdc | 84 | #include "sysemu/cpus.h" |
a5ed3525 | 85 | #include "sysemu/iothread.h" |
f348b6d1 | 86 | #include "qemu/cutils.h" |
6a5bd307 | 87 | |
a4538a5c JH |
88 | #if defined(TARGET_S390X) |
89 | #include "hw/s390x/storage-keys.h" | |
f860d497 | 90 | #include "hw/s390x/storage-attributes.h" |
a4538a5c JH |
91 | #endif |
92 | ||
9307c4c1 FB |
93 | /* |
94 | * Supported types: | |
5fafdf24 | 95 | * |
9307c4c1 | 96 | * 'F' filename |
81d0912d | 97 | * 'B' block device name |
9307c4c1 | 98 | * 's' string (accept optional quote) |
129be006 | 99 | * 'S' it just appends the rest of the string (accept optional quote) |
361127df MA |
100 | * 'O' option string of the form NAME=VALUE,... |
101 | * parsed according to QemuOptsList given by its name | |
102 | * Example: 'device:O' uses qemu_device_opts. | |
103 | * Restriction: only lists with empty desc are supported | |
104 | * TODO lift the restriction | |
92a31b1f FB |
105 | * 'i' 32 bit integer |
106 | * 'l' target long (32 or 64 bit) | |
91162849 LC |
107 | * 'M' Non-negative target long (32 or 64 bit), in user mode the |
108 | * value is multiplied by 2^20 (think Mebibyte) | |
dbc0c67f | 109 | * 'o' octets (aka bytes) |
5e00984a KW |
110 | * user mode accepts an optional E, e, P, p, T, t, G, g, M, m, |
111 | * K, k suffix, which multiplies the value by 2^60 for suffixes E | |
112 | * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t, | |
113 | * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k | |
fccfb11e MA |
114 | * 'T' double |
115 | * user mode accepts an optional ms, us, ns suffix, | |
116 | * which divides the value by 1e3, 1e6, 1e9, respectively | |
9307c4c1 FB |
117 | * '/' optional gdb-like print format (like "/10x") |
118 | * | |
fb46660e LC |
119 | * '?' optional type (for all types, except '/') |
120 | * '.' other form of optional type (for 'i' and 'l') | |
942cd1f2 MA |
121 | * 'b' boolean |
122 | * user mode accepts "on" or "off" | |
fb46660e | 123 | * '-' optional parameter (eg. '-f') |
9307c4c1 FB |
124 | * |
125 | */ | |
126 | ||
c227f099 | 127 | typedef struct mon_cmd_t { |
9dc39cba | 128 | const char *name; |
9307c4c1 | 129 | const char *args_type; |
9dc39cba FB |
130 | const char *params; |
131 | const char *help; | |
c3120f71 | 132 | const char *flags; /* p=preconfig */ |
2b9e3576 MAL |
133 | void (*cmd)(Monitor *mon, const QDict *qdict); |
134 | /* @sub_table is a list of 2nd level of commands. If it does not exist, | |
135 | * cmd should be used. If it exists, sub_table[?].cmd should be | |
136 | * used, and cmd of 1st level plays the role of help function. | |
5f3d335f WX |
137 | */ |
138 | struct mon_cmd_t *sub_table; | |
bfa40f77 | 139 | void (*command_completion)(ReadLineState *rs, int nb_args, const char *str); |
c227f099 | 140 | } mon_cmd_t; |
9dc39cba | 141 | |
f07918fd | 142 | /* file descriptors passed via SCM_RIGHTS */ |
c227f099 AL |
143 | typedef struct mon_fd_t mon_fd_t; |
144 | struct mon_fd_t { | |
f07918fd MM |
145 | char *name; |
146 | int fd; | |
c227f099 | 147 | QLIST_ENTRY(mon_fd_t) next; |
f07918fd MM |
148 | }; |
149 | ||
ba1c048a CB |
150 | /* file descriptor associated with a file descriptor set */ |
151 | typedef struct MonFdsetFd MonFdsetFd; | |
152 | struct MonFdsetFd { | |
153 | int fd; | |
154 | bool removed; | |
155 | char *opaque; | |
156 | QLIST_ENTRY(MonFdsetFd) next; | |
157 | }; | |
158 | ||
159 | /* file descriptor set containing fds passed via SCM_RIGHTS */ | |
160 | typedef struct MonFdset MonFdset; | |
161 | struct MonFdset { | |
162 | int64_t id; | |
163 | QLIST_HEAD(, MonFdsetFd) fds; | |
adb696f3 | 164 | QLIST_HEAD(, MonFdsetFd) dup_fds; |
ba1c048a CB |
165 | QLIST_ENTRY(MonFdset) next; |
166 | }; | |
167 | ||
74358f2a | 168 | typedef struct { |
5fa737a4 | 169 | JSONMessageParser parser; |
f994b258 MA |
170 | /* |
171 | * When a client connects, we're in capabilities negotiation mode. | |
774a6b67 MA |
172 | * @commands is &qmp_cap_negotiation_commands then. When command |
173 | * qmp_capabilities succeeds, we go into command mode, and | |
174 | * @command becomes &qmp_commands. | |
f994b258 | 175 | */ |
635db18f | 176 | QmpCommandList *commands; |
279f9e08 MA |
177 | bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */ |
178 | bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */ | |
71da4667 | 179 | /* |
774a6b67 MA |
180 | * Protects qmp request/response queue. |
181 | * Take monitor_lock first when you need both. | |
71da4667 PX |
182 | */ |
183 | QemuMutex qmp_queue_lock; | |
184 | /* Input queue that holds all the parsed QMP requests */ | |
185 | GQueue *qmp_requests; | |
abe3cd0f PX |
186 | /* Output queue contains all the QMP responses in order */ |
187 | GQueue *qmp_responses; | |
74358f2a | 188 | } MonitorQMP; |
5fa737a4 | 189 | |
afeecec2 DB |
190 | /* |
191 | * To prevent flooding clients, events can be throttled. The | |
192 | * throttling is calculated globally, rather than per-Monitor | |
193 | * instance. | |
194 | */ | |
43a14cfc | 195 | typedef struct MonitorQAPIEventState { |
7de0be65 MA |
196 | QAPIEvent event; /* Throttling state for this event type and... */ |
197 | QDict *data; /* ... data, see qapi_event_throttle_equal() */ | |
afeecec2 | 198 | QEMUTimer *timer; /* Timer for handling delayed events */ |
688b4b7d | 199 | QDict *qdict; /* Delayed event (if any) */ |
43a14cfc | 200 | } MonitorQAPIEventState; |
afeecec2 | 201 | |
b9b03ab0 MA |
202 | typedef struct { |
203 | int64_t rate; /* Minimum time (in ns) between two events */ | |
204 | } MonitorQAPIEventConf; | |
205 | ||
87127161 | 206 | struct Monitor { |
32a6ebec | 207 | CharBackend chr; |
a7aec5da | 208 | int reset_seen; |
731b0364 | 209 | int flags; |
df152fb9 | 210 | int suspend_cnt; /* Needs to be accessed atomically */ |
48c043d0 | 211 | bool skip_flush; |
f91dc2a0 | 212 | bool use_io_thread; |
d9f25280 PX |
213 | |
214 | /* | |
215 | * State used only in the thread "owning" the monitor. | |
cab5ad86 | 216 | * If @use_io_thread, this is @mon_iothread. |
d9f25280 PX |
217 | * Else, it's the main thread. |
218 | * These members can be safely accessed without locks. | |
219 | */ | |
731b0364 | 220 | ReadLineState *rs; |
d9f25280 | 221 | |
74358f2a | 222 | MonitorQMP qmp; |
751f8cfe | 223 | gchar *mon_cpu_path; |
097310b5 | 224 | BlockCompletionFunc *password_completion_cb; |
731b0364 | 225 | void *password_opaque; |
7717239d | 226 | mon_cmd_t *cmd_table; |
238d9f34 | 227 | QTAILQ_ENTRY(Monitor) entry; |
dc7cbcd8 PX |
228 | |
229 | /* | |
230 | * The per-monitor lock. We can't access guest memory when holding | |
231 | * the lock. | |
232 | */ | |
233 | QemuMutex mon_lock; | |
234 | ||
235 | /* | |
774a6b67 | 236 | * Members that are protected by the per-monitor lock |
dc7cbcd8 | 237 | */ |
9409fc05 | 238 | QLIST_HEAD(, mon_fd_t) fds; |
dc7cbcd8 PX |
239 | QString *outbuf; |
240 | guint out_watch; | |
241 | /* Read under either BQL or mon_lock, written with BQL+mon_lock. */ | |
242 | int mux_out; | |
87127161 AL |
243 | }; |
244 | ||
774a6b67 | 245 | /* Shared monitor I/O thread */ |
cab5ad86 MA |
246 | IOThread *mon_iothread; |
247 | ||
248 | /* Bottom half to dispatch the requests received from I/O thread */ | |
249 | QEMUBH *qmp_dispatcher_bh; | |
250 | ||
251 | /* Bottom half to deliver the responses back to clients */ | |
252 | QEMUBH *qmp_respond_bh; | |
a5ed3525 | 253 | |
6d2d563f PX |
254 | struct QMPRequest { |
255 | /* Owner of the request */ | |
256 | Monitor *mon; | |
257 | /* "id" field of the request */ | |
258 | QObject *id; | |
1cc37471 MA |
259 | /* |
260 | * Request object to be handled or Error to be reported | |
261 | * (exactly one of them is non-null) | |
262 | */ | |
6d2d563f | 263 | QObject *req; |
1cc37471 | 264 | Error *err; |
6d2d563f PX |
265 | /* |
266 | * Whether we need to resume the monitor afterward. This flag is | |
267 | * used to emulate the old QMP server behavior that the current | |
268 | * command must be completed before execution of the next one. | |
269 | */ | |
270 | bool need_resume; | |
271 | }; | |
272 | typedef struct QMPRequest QMPRequest; | |
273 | ||
2dbc8db0 LC |
274 | /* QMP checker flags */ |
275 | #define QMP_ACCEPT_UNKNOWNS 1 | |
276 | ||
095cb1bf | 277 | /* Protects mon_list, monitor_qapi_event_state. */ |
d622cb58 | 278 | static QemuMutex monitor_lock; |
095cb1bf | 279 | static GHashTable *monitor_qapi_event_state; |
238d9f34 | 280 | static QTAILQ_HEAD(mon_list, Monitor) mon_list; |
095cb1bf | 281 | |
47451466 PX |
282 | /* Protects mon_fdsets */ |
283 | static QemuMutex mon_fdsets_lock; | |
ba1c048a | 284 | static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; |
47451466 | 285 | |
efb87c16 | 286 | static int mon_refcount; |
7e2515e8 | 287 | |
816f8925 WX |
288 | static mon_cmd_t mon_cmds[]; |
289 | static mon_cmd_t info_cmds[]; | |
9dc39cba | 290 | |
635db18f | 291 | QmpCommandList qmp_commands, qmp_cap_negotiation_commands; |
1527badb | 292 | |
8631b608 | 293 | Monitor *cur_mon; |
376253ec | 294 | |
c60bf339 SH |
295 | static void monitor_command_cb(void *opaque, const char *cmdline, |
296 | void *readline_opaque); | |
83ab7950 | 297 | |
9f3982f2 MA |
298 | /** |
299 | * Is @mon a QMP monitor? | |
300 | */ | |
301 | static inline bool monitor_is_qmp(const Monitor *mon) | |
418173c7 LC |
302 | { |
303 | return (mon->flags & MONITOR_USE_CONTROL); | |
304 | } | |
305 | ||
e3e977d4 | 306 | /** |
774a6b67 MA |
307 | * Is @mon is using readline? |
308 | * Note: not all HMP monitors use readline, e.g., gdbserver has a | |
309 | * non-interactive HMP monitor, so readline is not used there. | |
e3e977d4 PX |
310 | */ |
311 | static inline bool monitor_uses_readline(const Monitor *mon) | |
312 | { | |
313 | return mon->flags & MONITOR_USE_READLINE; | |
314 | } | |
315 | ||
316 | static inline bool monitor_is_hmp_non_interactive(const Monitor *mon) | |
317 | { | |
318 | return !monitor_is_qmp(mon) && !monitor_uses_readline(mon); | |
319 | } | |
320 | ||
6e8c5f4d PX |
321 | /* |
322 | * Return the clock to use for recording an event's time. | |
774a6b67 MA |
323 | * It's QEMU_CLOCK_REALTIME, except for qtests it's |
324 | * QEMU_CLOCK_VIRTUAL, to support testing rate limits. | |
6e8c5f4d PX |
325 | * Beware: result is invalid before configure_accelerator(). |
326 | */ | |
327 | static inline QEMUClockType monitor_get_event_clock(void) | |
328 | { | |
6e8c5f4d PX |
329 | return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME; |
330 | } | |
331 | ||
489653b5 MA |
332 | /** |
333 | * Is the current monitor, if any, a QMP monitor? | |
334 | */ | |
335 | bool monitor_cur_is_qmp(void) | |
6620d3ce | 336 | { |
9f3982f2 | 337 | return cur_mon && monitor_is_qmp(cur_mon); |
6620d3ce MA |
338 | } |
339 | ||
7060b478 | 340 | void monitor_read_command(Monitor *mon, int show_prompt) |
731b0364 | 341 | { |
183e6e52 LC |
342 | if (!mon->rs) |
343 | return; | |
344 | ||
731b0364 AL |
345 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); |
346 | if (show_prompt) | |
347 | readline_show_prompt(mon->rs); | |
348 | } | |
6a00d601 | 349 | |
7060b478 AL |
350 | int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
351 | void *opaque) | |
bb5fc20f | 352 | { |
bcf5d19c | 353 | if (mon->rs) { |
cde76ee1 AL |
354 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); |
355 | /* prompt is printed on return from the command handler */ | |
356 | return 0; | |
357 | } else { | |
358 | monitor_printf(mon, "terminal does not support password prompting\n"); | |
359 | return -ENOTTY; | |
360 | } | |
bb5fc20f AL |
361 | } |
362 | ||
6d2d563f PX |
363 | static void qmp_request_free(QMPRequest *req) |
364 | { | |
cb3e7f08 MAL |
365 | qobject_unref(req->id); |
366 | qobject_unref(req->req); | |
1cc37471 | 367 | error_free(req->err); |
6d2d563f PX |
368 | g_free(req); |
369 | } | |
370 | ||
774a6b67 | 371 | /* Caller must hold mon->qmp.qmp_queue_lock */ |
6d2d563f PX |
372 | static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon) |
373 | { | |
374 | while (!g_queue_is_empty(mon->qmp.qmp_requests)) { | |
375 | qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests)); | |
376 | } | |
377 | } | |
378 | ||
774a6b67 | 379 | /* Caller must hold the mon->qmp.qmp_queue_lock */ |
6d2d563f PX |
380 | static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon) |
381 | { | |
382 | while (!g_queue_is_empty(mon->qmp.qmp_responses)) { | |
d43b1694 | 383 | qobject_unref((QDict *)g_queue_pop_head(mon->qmp.qmp_responses)); |
6d2d563f PX |
384 | } |
385 | } | |
386 | ||
387 | static void monitor_qmp_cleanup_queues(Monitor *mon) | |
388 | { | |
389 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); | |
390 | monitor_qmp_cleanup_req_queue_locked(mon); | |
391 | monitor_qmp_cleanup_resp_queue_locked(mon); | |
392 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); | |
393 | } | |
394 | ||
395 | ||
6cff3e85 PB |
396 | static void monitor_flush_locked(Monitor *mon); |
397 | ||
f628926b GH |
398 | static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond, |
399 | void *opaque) | |
400 | { | |
293d2a00 LE |
401 | Monitor *mon = opaque; |
402 | ||
dc7cbcd8 | 403 | qemu_mutex_lock(&mon->mon_lock); |
6cff3e85 PB |
404 | mon->out_watch = 0; |
405 | monitor_flush_locked(mon); | |
dc7cbcd8 | 406 | qemu_mutex_unlock(&mon->mon_lock); |
f628926b GH |
407 | return FALSE; |
408 | } | |
409 | ||
774a6b67 | 410 | /* Caller must hold mon->mon_lock */ |
6cff3e85 | 411 | static void monitor_flush_locked(Monitor *mon) |
7e2515e8 | 412 | { |
f628926b | 413 | int rc; |
e1f2641b LC |
414 | size_t len; |
415 | const char *buf; | |
416 | ||
48c043d0 LC |
417 | if (mon->skip_flush) { |
418 | return; | |
419 | } | |
420 | ||
e1f2641b LC |
421 | buf = qstring_get_str(mon->outbuf); |
422 | len = qstring_get_length(mon->outbuf); | |
f628926b | 423 | |
a4cc73d6 | 424 | if (len && !mon->mux_out) { |
5345fdb4 | 425 | rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len); |
056f49ff SP |
426 | if ((rc < 0 && errno != EAGAIN) || (rc == len)) { |
427 | /* all flushed or error */ | |
cb3e7f08 | 428 | qobject_unref(mon->outbuf); |
e1f2641b | 429 | mon->outbuf = qstring_new(); |
f628926b GH |
430 | return; |
431 | } | |
432 | if (rc > 0) { | |
3b7c78c8 | 433 | /* partial write */ |
e1f2641b | 434 | QString *tmp = qstring_from_str(buf + rc); |
cb3e7f08 | 435 | qobject_unref(mon->outbuf); |
e1f2641b | 436 | mon->outbuf = tmp; |
f628926b | 437 | } |
6cff3e85 | 438 | if (mon->out_watch == 0) { |
32a6ebec | 439 | mon->out_watch = |
5345fdb4 | 440 | qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP, |
32a6ebec | 441 | monitor_unblocked, mon); |
293d2a00 | 442 | } |
7e2515e8 FB |
443 | } |
444 | } | |
445 | ||
6cff3e85 PB |
446 | void monitor_flush(Monitor *mon) |
447 | { | |
dc7cbcd8 | 448 | qemu_mutex_lock(&mon->mon_lock); |
6cff3e85 | 449 | monitor_flush_locked(mon); |
dc7cbcd8 | 450 | qemu_mutex_unlock(&mon->mon_lock); |
6cff3e85 PB |
451 | } |
452 | ||
e1f2641b | 453 | /* flush at every end of line */ |
376253ec | 454 | static void monitor_puts(Monitor *mon, const char *str) |
7e2515e8 | 455 | { |
60fe76f3 | 456 | char c; |
731b0364 | 457 | |
dc7cbcd8 | 458 | qemu_mutex_lock(&mon->mon_lock); |
7e2515e8 FB |
459 | for(;;) { |
460 | c = *str++; | |
461 | if (c == '\0') | |
462 | break; | |
e1f2641b LC |
463 | if (c == '\n') { |
464 | qstring_append_chr(mon->outbuf, '\r'); | |
465 | } | |
466 | qstring_append_chr(mon->outbuf, c); | |
467 | if (c == '\n') { | |
6cff3e85 | 468 | monitor_flush_locked(mon); |
e1f2641b | 469 | } |
7e2515e8 | 470 | } |
dc7cbcd8 | 471 | qemu_mutex_unlock(&mon->mon_lock); |
7e2515e8 FB |
472 | } |
473 | ||
376253ec | 474 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
9dc39cba | 475 | { |
e1f2641b | 476 | char *buf; |
b8b08266 | 477 | |
2daa1191 LC |
478 | if (!mon) |
479 | return; | |
480 | ||
9f3982f2 | 481 | if (monitor_is_qmp(mon)) { |
b8b08266 | 482 | return; |
4a29a85d | 483 | } |
b8b08266 | 484 | |
e1f2641b | 485 | buf = g_strdup_vprintf(fmt, ap); |
b8b08266 | 486 | monitor_puts(mon, buf); |
e1f2641b | 487 | g_free(buf); |
9dc39cba FB |
488 | } |
489 | ||
376253ec | 490 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
9dc39cba | 491 | { |
7e2515e8 FB |
492 | va_list ap; |
493 | va_start(ap, fmt); | |
376253ec | 494 | monitor_vprintf(mon, fmt, ap); |
7e2515e8 | 495 | va_end(ap); |
9dc39cba FB |
496 | } |
497 | ||
caf15319 | 498 | int monitor_fprintf(FILE *stream, const char *fmt, ...) |
7fe48483 FB |
499 | { |
500 | va_list ap; | |
501 | va_start(ap, fmt); | |
376253ec | 502 | monitor_vprintf((Monitor *)stream, fmt, ap); |
7fe48483 FB |
503 | va_end(ap); |
504 | return 0; | |
505 | } | |
506 | ||
65e3fe67 | 507 | static void qmp_send_response(Monitor *mon, QDict *rsp) |
9b57c02e | 508 | { |
65e3fe67 | 509 | QObject *data = QOBJECT(rsp); |
9b57c02e LC |
510 | QString *json; |
511 | ||
83a27d4d LC |
512 | json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) : |
513 | qobject_to_json(data); | |
9b57c02e LC |
514 | assert(json != NULL); |
515 | ||
b8b08266 LC |
516 | qstring_append_chr(json, '\n'); |
517 | monitor_puts(mon, qstring_get_str(json)); | |
4a29a85d | 518 | |
cb3e7f08 | 519 | qobject_unref(json); |
9b57c02e LC |
520 | } |
521 | ||
65e3fe67 | 522 | static void qmp_queue_response(Monitor *mon, QDict *rsp) |
abe3cd0f | 523 | { |
f91dc2a0 | 524 | if (mon->use_io_thread) { |
abe3cd0f | 525 | /* |
774a6b67 MA |
526 | * Push a reference to the response queue. The I/O thread |
527 | * drains that queue and emits. | |
abe3cd0f | 528 | */ |
abe3cd0f | 529 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); |
65e3fe67 | 530 | g_queue_push_tail(mon->qmp.qmp_responses, qobject_ref(rsp)); |
abe3cd0f | 531 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); |
cab5ad86 | 532 | qemu_bh_schedule(qmp_respond_bh); |
abe3cd0f PX |
533 | } else { |
534 | /* | |
774a6b67 MA |
535 | * Not using monitor I/O thread, i.e. we are in the main thread. |
536 | * Emit right away. | |
abe3cd0f | 537 | */ |
65e3fe67 | 538 | qmp_send_response(mon, rsp); |
abe3cd0f PX |
539 | } |
540 | } | |
541 | ||
542 | struct QMPResponse { | |
543 | Monitor *mon; | |
d43b1694 | 544 | QDict *data; |
abe3cd0f PX |
545 | }; |
546 | typedef struct QMPResponse QMPResponse; | |
547 | ||
d43b1694 | 548 | static QDict *monitor_qmp_response_pop_one(Monitor *mon) |
c73a843b | 549 | { |
d43b1694 | 550 | QDict *data; |
c73a843b PX |
551 | |
552 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); | |
553 | data = g_queue_pop_head(mon->qmp.qmp_responses); | |
554 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); | |
555 | ||
556 | return data; | |
557 | } | |
558 | ||
559 | static void monitor_qmp_response_flush(Monitor *mon) | |
560 | { | |
d43b1694 | 561 | QDict *data; |
c73a843b PX |
562 | |
563 | while ((data = monitor_qmp_response_pop_one(mon))) { | |
65e3fe67 | 564 | qmp_send_response(mon, data); |
c73a843b PX |
565 | qobject_unref(data); |
566 | } | |
567 | } | |
568 | ||
abe3cd0f | 569 | /* |
40687eb7 PX |
570 | * Pop a QMPResponse from any monitor's response queue into @response. |
571 | * Return false if all the queues are empty; else true. | |
abe3cd0f | 572 | */ |
40687eb7 | 573 | static bool monitor_qmp_response_pop_any(QMPResponse *response) |
abe3cd0f PX |
574 | { |
575 | Monitor *mon; | |
d43b1694 | 576 | QDict *data = NULL; |
abe3cd0f PX |
577 | |
578 | qemu_mutex_lock(&monitor_lock); | |
579 | QTAILQ_FOREACH(mon, &mon_list, entry) { | |
c73a843b | 580 | data = monitor_qmp_response_pop_one(mon); |
abe3cd0f | 581 | if (data) { |
40687eb7 PX |
582 | response->mon = mon; |
583 | response->data = data; | |
abe3cd0f PX |
584 | break; |
585 | } | |
586 | } | |
587 | qemu_mutex_unlock(&monitor_lock); | |
40687eb7 | 588 | return data != NULL; |
abe3cd0f PX |
589 | } |
590 | ||
591 | static void monitor_qmp_bh_responder(void *opaque) | |
592 | { | |
593 | QMPResponse response; | |
594 | ||
40687eb7 | 595 | while (monitor_qmp_response_pop_any(&response)) { |
65e3fe67 | 596 | qmp_send_response(response.mon, response.data); |
cb3e7f08 | 597 | qobject_unref(response.data); |
abe3cd0f PX |
598 | } |
599 | } | |
600 | ||
7fb1cf16 | 601 | static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = { |
b9b03ab0 MA |
602 | /* Limit guest-triggerable events to 1 per second */ |
603 | [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS }, | |
604 | [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS }, | |
605 | [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS }, | |
606 | [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS }, | |
607 | [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS }, | |
608 | [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS }, | |
609 | }; | |
610 | ||
afeecec2 | 611 | /* |
774a6b67 MA |
612 | * Broadcast an event to all monitors. |
613 | * @qdict is the event object. Its member "event" must match @event. | |
614 | * Caller must hold monitor_lock. | |
afeecec2 | 615 | */ |
688b4b7d | 616 | static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) |
afeecec2 DB |
617 | { |
618 | Monitor *mon; | |
619 | ||
688b4b7d | 620 | trace_monitor_protocol_event_emit(event, qdict); |
238d9f34 | 621 | QTAILQ_FOREACH(mon, &mon_list, entry) { |
635db18f MA |
622 | if (monitor_is_qmp(mon) |
623 | && mon->qmp.commands != &qmp_cap_negotiation_commands) { | |
65e3fe67 | 624 | qmp_queue_response(mon, qdict); |
afeecec2 DB |
625 | } |
626 | } | |
627 | } | |
628 | ||
a24712af MA |
629 | static void monitor_qapi_event_handler(void *opaque); |
630 | ||
afeecec2 DB |
631 | /* |
632 | * Queue a new event for emission to Monitor instances, | |
633 | * applying any rate limiting if required. | |
634 | */ | |
635 | static void | |
688b4b7d | 636 | monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp) |
afeecec2 | 637 | { |
b9b03ab0 | 638 | MonitorQAPIEventConf *evconf; |
43a14cfc | 639 | MonitorQAPIEventState *evstate; |
afeecec2 | 640 | |
7fb1cf16 | 641 | assert(event < QAPI_EVENT__MAX); |
b9b03ab0 | 642 | evconf = &monitor_qapi_event_conf[event]; |
b9b03ab0 | 643 | trace_monitor_protocol_event_queue(event, qdict, evconf->rate); |
afeecec2 | 644 | |
d622cb58 | 645 | qemu_mutex_lock(&monitor_lock); |
93f8f982 | 646 | |
b9b03ab0 | 647 | if (!evconf->rate) { |
93f8f982 | 648 | /* Unthrottled event */ |
688b4b7d | 649 | monitor_qapi_event_emit(event, qdict); |
afeecec2 | 650 | } else { |
7dc847eb | 651 | QDict *data = qobject_to(QDict, qdict_get(qdict, "data")); |
7de0be65 | 652 | MonitorQAPIEventState key = { .event = event, .data = data }; |
a24712af MA |
653 | |
654 | evstate = g_hash_table_lookup(monitor_qapi_event_state, &key); | |
655 | assert(!evstate || timer_pending(evstate->timer)); | |
656 | ||
657 | if (evstate) { | |
93f8f982 | 658 | /* |
b9b03ab0 | 659 | * Timer is pending for (at least) evconf->rate ns after |
93f8f982 MA |
660 | * last send. Store event for sending when timer fires, |
661 | * replacing a prior stored event if any. | |
afeecec2 | 662 | */ |
cb3e7f08 | 663 | qobject_unref(evstate->qdict); |
f5a74a5a | 664 | evstate->qdict = qobject_ref(qdict); |
afeecec2 | 665 | } else { |
93f8f982 | 666 | /* |
b9b03ab0 | 667 | * Last send was (at least) evconf->rate ns ago. |
93f8f982 | 668 | * Send immediately, and arm the timer to call |
b9b03ab0 | 669 | * monitor_qapi_event_handler() in evconf->rate ns. Any |
93f8f982 MA |
670 | * events arriving before then will be delayed until then. |
671 | */ | |
6e8c5f4d | 672 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
93f8f982 | 673 | |
688b4b7d | 674 | monitor_qapi_event_emit(event, qdict); |
a24712af MA |
675 | |
676 | evstate = g_new(MonitorQAPIEventState, 1); | |
677 | evstate->event = event; | |
f5a74a5a | 678 | evstate->data = qobject_ref(data); |
a24712af | 679 | evstate->qdict = NULL; |
6e8c5f4d | 680 | evstate->timer = timer_new_ns(monitor_get_event_clock(), |
a24712af MA |
681 | monitor_qapi_event_handler, |
682 | evstate); | |
683 | g_hash_table_add(monitor_qapi_event_state, evstate); | |
b9b03ab0 | 684 | timer_mod_ns(evstate->timer, now + evconf->rate); |
afeecec2 DB |
685 | } |
686 | } | |
93f8f982 | 687 | |
d622cb58 | 688 | qemu_mutex_unlock(&monitor_lock); |
afeecec2 DB |
689 | } |
690 | ||
afeecec2 | 691 | /* |
b9b03ab0 | 692 | * This function runs evconf->rate ns after sending a throttled |
93f8f982 MA |
693 | * event. |
694 | * If another event has since been stored, send it. | |
afeecec2 | 695 | */ |
43a14cfc | 696 | static void monitor_qapi_event_handler(void *opaque) |
afeecec2 | 697 | { |
43a14cfc | 698 | MonitorQAPIEventState *evstate = opaque; |
b9b03ab0 | 699 | MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event]; |
afeecec2 | 700 | |
93f8f982 | 701 | trace_monitor_protocol_event_handler(evstate->event, evstate->qdict); |
d622cb58 | 702 | qemu_mutex_lock(&monitor_lock); |
93f8f982 | 703 | |
688b4b7d | 704 | if (evstate->qdict) { |
6e8c5f4d | 705 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
93f8f982 | 706 | |
688b4b7d | 707 | monitor_qapi_event_emit(evstate->event, evstate->qdict); |
cb3e7f08 | 708 | qobject_unref(evstate->qdict); |
688b4b7d | 709 | evstate->qdict = NULL; |
b9b03ab0 | 710 | timer_mod_ns(evstate->timer, now + evconf->rate); |
a24712af MA |
711 | } else { |
712 | g_hash_table_remove(monitor_qapi_event_state, evstate); | |
cb3e7f08 | 713 | qobject_unref(evstate->data); |
a24712af MA |
714 | timer_free(evstate->timer); |
715 | g_free(evstate); | |
afeecec2 | 716 | } |
93f8f982 | 717 | |
d622cb58 | 718 | qemu_mutex_unlock(&monitor_lock); |
afeecec2 DB |
719 | } |
720 | ||
a24712af | 721 | static unsigned int qapi_event_throttle_hash(const void *key) |
afeecec2 | 722 | { |
a24712af | 723 | const MonitorQAPIEventState *evstate = key; |
7de0be65 | 724 | unsigned int hash = evstate->event * 255; |
afeecec2 | 725 | |
7de0be65 MA |
726 | if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) { |
727 | hash += g_str_hash(qdict_get_str(evstate->data, "id")); | |
728 | } | |
729 | ||
6d425eb9 AG |
730 | if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
731 | hash += g_str_hash(qdict_get_str(evstate->data, "node-name")); | |
732 | } | |
733 | ||
7de0be65 | 734 | return hash; |
a24712af | 735 | } |
0d1ea871 | 736 | |
a24712af MA |
737 | static gboolean qapi_event_throttle_equal(const void *a, const void *b) |
738 | { | |
739 | const MonitorQAPIEventState *eva = a; | |
740 | const MonitorQAPIEventState *evb = b; | |
741 | ||
7de0be65 MA |
742 | if (eva->event != evb->event) { |
743 | return FALSE; | |
744 | } | |
745 | ||
746 | if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) { | |
747 | return !strcmp(qdict_get_str(eva->data, "id"), | |
748 | qdict_get_str(evb->data, "id")); | |
749 | } | |
750 | ||
6d425eb9 AG |
751 | if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
752 | return !strcmp(qdict_get_str(eva->data, "node-name"), | |
753 | qdict_get_str(evb->data, "node-name")); | |
754 | } | |
755 | ||
7de0be65 | 756 | return TRUE; |
a24712af MA |
757 | } |
758 | ||
759 | static void monitor_qapi_event_init(void) | |
760 | { | |
761 | monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash, | |
762 | qapi_event_throttle_equal); | |
43a14cfc | 763 | qmp_event_set_func_emit(monitor_qapi_event_queue); |
0d1ea871 LC |
764 | } |
765 | ||
7ef6cf63 | 766 | static void handle_hmp_command(Monitor *mon, const char *cmdline); |
0268d97c | 767 | |
a5ed3525 | 768 | static void monitor_data_init(Monitor *mon, bool skip_flush, |
f91dc2a0 | 769 | bool use_io_thread) |
b01fe89e WX |
770 | { |
771 | memset(mon, 0, sizeof(Monitor)); | |
dc7cbcd8 | 772 | qemu_mutex_init(&mon->mon_lock); |
71da4667 | 773 | qemu_mutex_init(&mon->qmp.qmp_queue_lock); |
b01fe89e | 774 | mon->outbuf = qstring_new(); |
7717239d WX |
775 | /* Use *mon_cmds by default. */ |
776 | mon->cmd_table = mon_cmds; | |
88a95d10 | 777 | mon->skip_flush = skip_flush; |
f91dc2a0 | 778 | mon->use_io_thread = use_io_thread; |
71da4667 | 779 | mon->qmp.qmp_requests = g_queue_new(); |
abe3cd0f | 780 | mon->qmp.qmp_responses = g_queue_new(); |
b01fe89e WX |
781 | } |
782 | ||
783 | static void monitor_data_destroy(Monitor *mon) | |
784 | { | |
751f8cfe | 785 | g_free(mon->mon_cpu_path); |
1ce2610c | 786 | qemu_chr_fe_deinit(&mon->chr, false); |
2ef45716 MAL |
787 | if (monitor_is_qmp(mon)) { |
788 | json_message_parser_destroy(&mon->qmp.parser); | |
789 | } | |
e5dc1a6c | 790 | readline_free(mon->rs); |
cb3e7f08 | 791 | qobject_unref(mon->outbuf); |
dc7cbcd8 | 792 | qemu_mutex_destroy(&mon->mon_lock); |
71da4667 | 793 | qemu_mutex_destroy(&mon->qmp.qmp_queue_lock); |
6d2d563f PX |
794 | monitor_qmp_cleanup_req_queue_locked(mon); |
795 | monitor_qmp_cleanup_resp_queue_locked(mon); | |
71da4667 | 796 | g_queue_free(mon->qmp.qmp_requests); |
abe3cd0f | 797 | g_queue_free(mon->qmp.qmp_responses); |
b01fe89e WX |
798 | } |
799 | ||
d51a67b4 LC |
800 | char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, |
801 | int64_t cpu_index, Error **errp) | |
0268d97c | 802 | { |
d51a67b4 | 803 | char *output = NULL; |
0268d97c | 804 | Monitor *old_mon, hmp; |
0268d97c | 805 | |
a5ed3525 | 806 | monitor_data_init(&hmp, true, false); |
0268d97c LC |
807 | |
808 | old_mon = cur_mon; | |
809 | cur_mon = &hmp; | |
810 | ||
d51a67b4 LC |
811 | if (has_cpu_index) { |
812 | int ret = monitor_set_cpu(cpu_index); | |
0268d97c LC |
813 | if (ret < 0) { |
814 | cur_mon = old_mon; | |
c6bd8c70 MA |
815 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
816 | "a CPU number"); | |
0268d97c LC |
817 | goto out; |
818 | } | |
819 | } | |
820 | ||
7ef6cf63 | 821 | handle_hmp_command(&hmp, command_line); |
0268d97c LC |
822 | cur_mon = old_mon; |
823 | ||
dc7cbcd8 | 824 | qemu_mutex_lock(&hmp.mon_lock); |
48c043d0 LC |
825 | if (qstring_get_length(hmp.outbuf) > 0) { |
826 | output = g_strdup(qstring_get_str(hmp.outbuf)); | |
d51a67b4 LC |
827 | } else { |
828 | output = g_strdup(""); | |
0268d97c | 829 | } |
dc7cbcd8 | 830 | qemu_mutex_unlock(&hmp.mon_lock); |
0268d97c LC |
831 | |
832 | out: | |
b01fe89e | 833 | monitor_data_destroy(&hmp); |
d51a67b4 | 834 | return output; |
0268d97c LC |
835 | } |
836 | ||
9dc39cba FB |
837 | static int compare_cmd(const char *name, const char *list) |
838 | { | |
839 | const char *p, *pstart; | |
840 | int len; | |
841 | len = strlen(name); | |
842 | p = list; | |
843 | for(;;) { | |
844 | pstart = p; | |
5c99fa37 | 845 | p = qemu_strchrnul(p, '|'); |
9dc39cba FB |
846 | if ((p - pstart) == len && !memcmp(pstart, name, len)) |
847 | return 1; | |
848 | if (*p == '\0') | |
849 | break; | |
850 | p++; | |
851 | } | |
852 | return 0; | |
853 | } | |
854 | ||
f5438c05 WX |
855 | static int get_str(char *buf, int buf_size, const char **pp) |
856 | { | |
857 | const char *p; | |
858 | char *q; | |
859 | int c; | |
860 | ||
861 | q = buf; | |
862 | p = *pp; | |
863 | while (qemu_isspace(*p)) { | |
864 | p++; | |
865 | } | |
866 | if (*p == '\0') { | |
867 | fail: | |
868 | *q = '\0'; | |
869 | *pp = p; | |
870 | return -1; | |
871 | } | |
872 | if (*p == '\"') { | |
873 | p++; | |
874 | while (*p != '\0' && *p != '\"') { | |
875 | if (*p == '\\') { | |
876 | p++; | |
877 | c = *p++; | |
878 | switch (c) { | |
879 | case 'n': | |
880 | c = '\n'; | |
881 | break; | |
882 | case 'r': | |
883 | c = '\r'; | |
884 | break; | |
885 | case '\\': | |
886 | case '\'': | |
887 | case '\"': | |
888 | break; | |
889 | default: | |
71baf787 | 890 | printf("unsupported escape code: '\\%c'\n", c); |
f5438c05 WX |
891 | goto fail; |
892 | } | |
893 | if ((q - buf) < buf_size - 1) { | |
894 | *q++ = c; | |
895 | } | |
896 | } else { | |
897 | if ((q - buf) < buf_size - 1) { | |
898 | *q++ = *p; | |
899 | } | |
900 | p++; | |
901 | } | |
902 | } | |
903 | if (*p != '\"') { | |
71baf787 | 904 | printf("unterminated string\n"); |
f5438c05 WX |
905 | goto fail; |
906 | } | |
907 | p++; | |
908 | } else { | |
909 | while (*p != '\0' && !qemu_isspace(*p)) { | |
910 | if ((q - buf) < buf_size - 1) { | |
911 | *q++ = *p; | |
912 | } | |
913 | p++; | |
914 | } | |
915 | } | |
916 | *q = '\0'; | |
917 | *pp = p; | |
918 | return 0; | |
919 | } | |
920 | ||
921 | #define MAX_ARGS 16 | |
922 | ||
dcc70cdf WX |
923 | static void free_cmdline_args(char **args, int nb_args) |
924 | { | |
925 | int i; | |
926 | ||
927 | assert(nb_args <= MAX_ARGS); | |
928 | ||
929 | for (i = 0; i < nb_args; i++) { | |
930 | g_free(args[i]); | |
931 | } | |
932 | ||
933 | } | |
934 | ||
935 | /* | |
936 | * Parse the command line to get valid args. | |
937 | * @cmdline: command line to be parsed. | |
938 | * @pnb_args: location to store the number of args, must NOT be NULL. | |
939 | * @args: location to store the args, which should be freed by caller, must | |
940 | * NOT be NULL. | |
941 | * | |
942 | * Returns 0 on success, negative on failure. | |
943 | * | |
944 | * NOTE: this parser is an approximate form of the real command parser. Number | |
945 | * of args have a limit of MAX_ARGS. If cmdline contains more, it will | |
946 | * return with failure. | |
947 | */ | |
948 | static int parse_cmdline(const char *cmdline, | |
949 | int *pnb_args, char **args) | |
f5438c05 WX |
950 | { |
951 | const char *p; | |
952 | int nb_args, ret; | |
953 | char buf[1024]; | |
954 | ||
955 | p = cmdline; | |
956 | nb_args = 0; | |
957 | for (;;) { | |
958 | while (qemu_isspace(*p)) { | |
959 | p++; | |
960 | } | |
961 | if (*p == '\0') { | |
962 | break; | |
963 | } | |
964 | if (nb_args >= MAX_ARGS) { | |
dcc70cdf | 965 | goto fail; |
f5438c05 WX |
966 | } |
967 | ret = get_str(buf, sizeof(buf), &p); | |
f5438c05 | 968 | if (ret < 0) { |
dcc70cdf | 969 | goto fail; |
f5438c05 | 970 | } |
dcc70cdf WX |
971 | args[nb_args] = g_strdup(buf); |
972 | nb_args++; | |
f5438c05 WX |
973 | } |
974 | *pnb_args = nb_args; | |
dcc70cdf WX |
975 | return 0; |
976 | ||
977 | fail: | |
978 | free_cmdline_args(args, nb_args); | |
979 | return -1; | |
f5438c05 WX |
980 | } |
981 | ||
c3120f71 | 982 | /* |
774a6b67 | 983 | * Can command @cmd be executed in preconfig state? |
c3120f71 DDAG |
984 | */ |
985 | static bool cmd_can_preconfig(const mon_cmd_t *cmd) | |
986 | { | |
987 | if (!cmd->flags) { | |
988 | return false; | |
989 | } | |
990 | ||
991 | return strchr(cmd->flags, 'p'); | |
992 | } | |
993 | ||
66855495 WX |
994 | static void help_cmd_dump_one(Monitor *mon, |
995 | const mon_cmd_t *cmd, | |
996 | char **prefix_args, | |
997 | int prefix_args_nb) | |
998 | { | |
999 | int i; | |
1000 | ||
31785f1b DDAG |
1001 | if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) { |
1002 | return; | |
1003 | } | |
1004 | ||
66855495 WX |
1005 | for (i = 0; i < prefix_args_nb; i++) { |
1006 | monitor_printf(mon, "%s ", prefix_args[i]); | |
1007 | } | |
1008 | monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help); | |
1009 | } | |
1010 | ||
1011 | /* @args[@arg_index] is the valid command need to find in @cmds */ | |
c227f099 | 1012 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
66855495 | 1013 | char **args, int nb_args, int arg_index) |
9dc39cba | 1014 | { |
c227f099 | 1015 | const mon_cmd_t *cmd; |
9dc39cba | 1016 | |
66855495 WX |
1017 | /* No valid arg need to compare with, dump all in *cmds */ |
1018 | if (arg_index >= nb_args) { | |
1019 | for (cmd = cmds; cmd->name != NULL; cmd++) { | |
1020 | help_cmd_dump_one(mon, cmd, args, arg_index); | |
1021 | } | |
1022 | return; | |
1023 | } | |
1024 | ||
1025 | /* Find one entry to dump */ | |
1026 | for (cmd = cmds; cmd->name != NULL; cmd++) { | |
31785f1b DDAG |
1027 | if (compare_cmd(args[arg_index], cmd->name) && |
1028 | ((!runstate_check(RUN_STATE_PRECONFIG) || | |
1029 | cmd_can_preconfig(cmd)))) { | |
66855495 WX |
1030 | if (cmd->sub_table) { |
1031 | /* continue with next arg */ | |
1032 | help_cmd_dump(mon, cmd->sub_table, | |
1033 | args, nb_args, arg_index + 1); | |
1034 | } else { | |
1035 | help_cmd_dump_one(mon, cmd, args, arg_index); | |
1036 | } | |
1037 | break; | |
1038 | } | |
9dc39cba FB |
1039 | } |
1040 | } | |
1041 | ||
376253ec | 1042 | static void help_cmd(Monitor *mon, const char *name) |
9dc39cba | 1043 | { |
66855495 WX |
1044 | char *args[MAX_ARGS]; |
1045 | int nb_args = 0; | |
1046 | ||
1047 | /* 1. parse user input */ | |
1048 | if (name) { | |
1049 | /* special case for log, directly dump and return */ | |
1050 | if (!strcmp(name, "log")) { | |
38dad9e5 | 1051 | const QEMULogItem *item; |
376253ec AL |
1052 | monitor_printf(mon, "Log items (comma separated):\n"); |
1053 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); | |
38dad9e5 | 1054 | for (item = qemu_log_items; item->mask != 0; item++) { |
376253ec | 1055 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
f193c797 | 1056 | } |
66855495 WX |
1057 | return; |
1058 | } | |
1059 | ||
1060 | if (parse_cmdline(name, &nb_args, args) < 0) { | |
1061 | return; | |
f193c797 | 1062 | } |
9dc39cba | 1063 | } |
66855495 WX |
1064 | |
1065 | /* 2. dump the contents according to parsed args */ | |
1066 | help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0); | |
1067 | ||
1068 | free_cmdline_args(args, nb_args); | |
9dc39cba FB |
1069 | } |
1070 | ||
d54908a5 | 1071 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
38183186 | 1072 | { |
d54908a5 | 1073 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
38183186 LC |
1074 | } |
1075 | ||
3e5a50d6 | 1076 | static void hmp_trace_event(Monitor *mon, const QDict *qdict) |
22890ab5 PS |
1077 | { |
1078 | const char *tp_name = qdict_get_str(qdict, "name"); | |
1079 | bool new_state = qdict_get_bool(qdict, "option"); | |
77e2b172 LV |
1080 | bool has_vcpu = qdict_haskey(qdict, "vcpu"); |
1081 | int vcpu = qdict_get_try_int(qdict, "vcpu", 0); | |
14101d02 | 1082 | Error *local_err = NULL; |
f871d689 | 1083 | |
77e2b172 LV |
1084 | if (vcpu < 0) { |
1085 | monitor_printf(mon, "argument vcpu must be positive"); | |
1086 | return; | |
1087 | } | |
1088 | ||
1089 | qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err); | |
14101d02 | 1090 | if (local_err) { |
091e38b7 | 1091 | error_report_err(local_err); |
f871d689 | 1092 | } |
22890ab5 | 1093 | } |
c5ceb523 | 1094 | |
c45a8168 | 1095 | #ifdef CONFIG_TRACE_SIMPLE |
3e5a50d6 | 1096 | static void hmp_trace_file(Monitor *mon, const QDict *qdict) |
c5ceb523 SH |
1097 | { |
1098 | const char *op = qdict_get_try_str(qdict, "op"); | |
1099 | const char *arg = qdict_get_try_str(qdict, "arg"); | |
1100 | ||
1101 | if (!op) { | |
1102 | st_print_trace_file_status((FILE *)mon, &monitor_fprintf); | |
1103 | } else if (!strcmp(op, "on")) { | |
1104 | st_set_trace_file_enabled(true); | |
1105 | } else if (!strcmp(op, "off")) { | |
1106 | st_set_trace_file_enabled(false); | |
1107 | } else if (!strcmp(op, "flush")) { | |
1108 | st_flush_trace_buffer(); | |
1109 | } else if (!strcmp(op, "set")) { | |
1110 | if (arg) { | |
1111 | st_set_trace_file(arg); | |
1112 | } | |
1113 | } else { | |
1114 | monitor_printf(mon, "unexpected argument \"%s\"\n", op); | |
1115 | help_cmd(mon, "trace-file"); | |
1116 | } | |
1117 | } | |
22890ab5 PS |
1118 | #endif |
1119 | ||
3e5a50d6 | 1120 | static void hmp_info_help(Monitor *mon, const QDict *qdict) |
9dc39cba | 1121 | { |
13c7425e | 1122 | help_cmd(mon, "info"); |
9dc39cba FB |
1123 | } |
1124 | ||
9e812b6a | 1125 | static void query_commands_cb(QmpCommand *cmd, void *opaque) |
e3bba9d0 | 1126 | { |
9e812b6a | 1127 | CommandInfoList *info, **list = opaque; |
e3bba9d0 | 1128 | |
9e812b6a MAL |
1129 | if (!cmd->enabled) { |
1130 | return; | |
e3bba9d0 LC |
1131 | } |
1132 | ||
9e812b6a MAL |
1133 | info = g_malloc0(sizeof(*info)); |
1134 | info->value = g_malloc0(sizeof(*info->value)); | |
1135 | info->value->name = g_strdup(cmd->name); | |
1136 | info->next = *list; | |
1137 | *list = info; | |
1138 | } | |
1139 | ||
1140 | CommandInfoList *qmp_query_commands(Error **errp) | |
1141 | { | |
1142 | CommandInfoList *list = NULL; | |
1143 | ||
635db18f | 1144 | qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list); |
9e812b6a MAL |
1145 | |
1146 | return list; | |
a36e69dd TS |
1147 | } |
1148 | ||
4860853d DB |
1149 | EventInfoList *qmp_query_events(Error **errp) |
1150 | { | |
1151 | EventInfoList *info, *ev_list = NULL; | |
75175173 | 1152 | QAPIEvent e; |
4860853d | 1153 | |
7fb1cf16 | 1154 | for (e = 0 ; e < QAPI_EVENT__MAX ; e++) { |
977c736f | 1155 | const char *event_name = QAPIEvent_str(e); |
4860853d DB |
1156 | assert(event_name != NULL); |
1157 | info = g_malloc0(sizeof(*info)); | |
1158 | info->value = g_malloc0(sizeof(*info->value)); | |
1159 | info->value->name = g_strdup(event_name); | |
1160 | ||
1161 | info->next = ev_list; | |
1162 | ev_list = info; | |
1163 | } | |
1164 | ||
1165 | return ev_list; | |
1166 | } | |
1167 | ||
39a18158 MA |
1168 | /* |
1169 | * Minor hack: generated marshalling suppressed for this command | |
1170 | * ('gen': false in the schema) so we can parse the JSON string | |
1171 | * directly into QObject instead of first parsing it with | |
1172 | * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it | |
1173 | * to QObject with generated output marshallers, every time. Instead, | |
b3db211f | 1174 | * we do it in test-qobject-input-visitor.c, just to make sure |
fb0bc835 | 1175 | * qapi-gen.py's output actually conforms to the schema. |
39a18158 MA |
1176 | */ |
1177 | static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, | |
1178 | Error **errp) | |
1179 | { | |
7d0f982b | 1180 | *ret_data = qobject_from_qlit(&qmp_schema_qlit); |
39a18158 MA |
1181 | } |
1182 | ||
5032a16d | 1183 | /* |
5032a16d MAL |
1184 | * We used to define commands in qmp-commands.hx in addition to the |
1185 | * QAPI schema. This permitted defining some of them only in certain | |
1186 | * configurations. query-commands has always reflected that (good, | |
1187 | * because it lets QMP clients figure out what's actually available), | |
1188 | * while query-qmp-schema never did (not so good). This function is a | |
1189 | * hack to keep the configuration-specific commands defined exactly as | |
1190 | * before, even though qmp-commands.hx is gone. | |
1191 | * | |
1192 | * FIXME Educate the QAPI schema on configuration-specific commands, | |
1193 | * and drop this hack. | |
1194 | */ | |
1195 | static void qmp_unregister_commands_hack(void) | |
1196 | { | |
38bb54f3 MA |
1197 | #ifndef CONFIG_REPLICATION |
1198 | qmp_unregister_command(&qmp_commands, "xen-set-replication"); | |
1199 | qmp_unregister_command(&qmp_commands, "query-xen-replication-status"); | |
1200 | qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint"); | |
1201 | #endif | |
5032a16d | 1202 | #ifndef TARGET_I386 |
1527badb | 1203 | qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection"); |
08a161fd | 1204 | qmp_unregister_command(&qmp_commands, "query-sev"); |
1b6a034f | 1205 | qmp_unregister_command(&qmp_commands, "query-sev-launch-measure"); |
31dd67f6 | 1206 | qmp_unregister_command(&qmp_commands, "query-sev-capabilities"); |
5032a16d MAL |
1207 | #endif |
1208 | #ifndef TARGET_S390X | |
1527badb | 1209 | qmp_unregister_command(&qmp_commands, "dump-skeys"); |
5032a16d MAL |
1210 | #endif |
1211 | #ifndef TARGET_ARM | |
1527badb | 1212 | qmp_unregister_command(&qmp_commands, "query-gic-capabilities"); |
5032a16d | 1213 | #endif |
f99fd7ca | 1214 | #if !defined(TARGET_S390X) && !defined(TARGET_I386) |
1527badb | 1215 | qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion"); |
f99fd7ca EH |
1216 | #endif |
1217 | #if !defined(TARGET_S390X) | |
1527badb MA |
1218 | qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline"); |
1219 | qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison"); | |
728b1429 EH |
1220 | #endif |
1221 | #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \ | |
1222 | && !defined(TARGET_S390X) | |
1527badb | 1223 | qmp_unregister_command(&qmp_commands, "query-cpu-definitions"); |
728b1429 | 1224 | #endif |
5032a16d MAL |
1225 | } |
1226 | ||
6adf08dd | 1227 | static void monitor_init_qmp_commands(void) |
edcfaefe | 1228 | { |
635db18f MA |
1229 | /* |
1230 | * Two command lists: | |
1231 | * - qmp_commands contains all QMP commands | |
1232 | * - qmp_cap_negotiation_commands contains just | |
1233 | * "qmp_capabilities", to enforce capability negotiation | |
1234 | */ | |
1235 | ||
1527badb | 1236 | qmp_init_marshal(&qmp_commands); |
05875687 | 1237 | |
1527badb | 1238 | qmp_register_command(&qmp_commands, "query-qmp-schema", |
d6fe3d02 | 1239 | qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG); |
1527badb | 1240 | qmp_register_command(&qmp_commands, "device_add", qmp_device_add, |
edcfaefe | 1241 | QCO_NO_OPTIONS); |
1527badb | 1242 | qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add, |
edcfaefe | 1243 | QCO_NO_OPTIONS); |
5032a16d | 1244 | |
05875687 | 1245 | qmp_unregister_commands_hack(); |
635db18f MA |
1246 | |
1247 | QTAILQ_INIT(&qmp_cap_negotiation_commands); | |
1248 | qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities", | |
d6fe3d02 | 1249 | qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG); |
635db18f MA |
1250 | } |
1251 | ||
71da4667 PX |
1252 | static bool qmp_oob_enabled(Monitor *mon) |
1253 | { | |
279f9e08 | 1254 | return mon->qmp.capab[QMP_CAPABILITY_OOB]; |
02130314 PX |
1255 | } |
1256 | ||
279f9e08 | 1257 | static void monitor_qmp_caps_reset(Monitor *mon) |
635db18f | 1258 | { |
279f9e08 MA |
1259 | memset(mon->qmp.capab_offered, 0, sizeof(mon->qmp.capab_offered)); |
1260 | memset(mon->qmp.capab, 0, sizeof(mon->qmp.capab)); | |
1261 | mon->qmp.capab_offered[QMP_CAPABILITY_OOB] = mon->use_io_thread; | |
02130314 PX |
1262 | } |
1263 | ||
cf869d53 | 1264 | /* |
279f9e08 MA |
1265 | * Accept QMP capabilities in @list for @mon. |
1266 | * On success, set mon->qmp.capab[], and return true. | |
1267 | * On error, set @errp, and return false. | |
cf869d53 | 1268 | */ |
279f9e08 MA |
1269 | static bool qmp_caps_accept(Monitor *mon, QMPCapabilityList *list, |
1270 | Error **errp) | |
cf869d53 | 1271 | { |
279f9e08 MA |
1272 | GString *unavailable = NULL; |
1273 | bool capab[QMP_CAPABILITY__MAX]; | |
cf869d53 | 1274 | |
279f9e08 | 1275 | memset(capab, 0, sizeof(capab)); |
cf869d53 | 1276 | |
02130314 | 1277 | for (; list; list = list->next) { |
279f9e08 MA |
1278 | if (!mon->qmp.capab_offered[list->value]) { |
1279 | if (!unavailable) { | |
1280 | unavailable = g_string_new(QMPCapability_str(list->value)); | |
1281 | } else { | |
1282 | g_string_append_printf(unavailable, ", %s", | |
1283 | QMPCapability_str(list->value)); | |
02130314 | 1284 | } |
9ddb7456 | 1285 | } |
279f9e08 | 1286 | capab[list->value] = true; |
cf869d53 PX |
1287 | } |
1288 | ||
279f9e08 MA |
1289 | if (unavailable) { |
1290 | error_setg(errp, "Capability %s not available", unavailable->str); | |
1291 | g_string_free(unavailable, true); | |
1292 | return false; | |
cf869d53 PX |
1293 | } |
1294 | ||
279f9e08 | 1295 | memcpy(mon->qmp.capab, capab, sizeof(capab)); |
cf869d53 PX |
1296 | return true; |
1297 | } | |
1298 | ||
02130314 PX |
1299 | void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable, |
1300 | Error **errp) | |
1301 | { | |
635db18f MA |
1302 | if (cur_mon->qmp.commands == &qmp_commands) { |
1303 | error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, | |
1304 | "Capabilities negotiation is already complete, command " | |
1305 | "ignored"); | |
1306 | return; | |
1307 | } | |
1308 | ||
279f9e08 MA |
1309 | if (!qmp_caps_accept(cur_mon, enable, errp)) { |
1310 | return; | |
02130314 PX |
1311 | } |
1312 | ||
635db18f | 1313 | cur_mon->qmp.commands = &qmp_commands; |
edcfaefe MAL |
1314 | } |
1315 | ||
d9f25280 | 1316 | /* Set the current CPU defined by the user. Callers must hold BQL. */ |
b025c8b4 | 1317 | int monitor_set_cpu(int cpu_index) |
6a00d601 | 1318 | { |
55e5c285 | 1319 | CPUState *cpu; |
6a00d601 | 1320 | |
1c8bb3cc AF |
1321 | cpu = qemu_get_cpu(cpu_index); |
1322 | if (cpu == NULL) { | |
1323 | return -1; | |
6a00d601 | 1324 | } |
751f8cfe GK |
1325 | g_free(cur_mon->mon_cpu_path); |
1326 | cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu)); | |
1c8bb3cc | 1327 | return 0; |
6a00d601 FB |
1328 | } |
1329 | ||
d9f25280 | 1330 | /* Callers must hold BQL. */ |
137b5cb6 | 1331 | static CPUState *mon_get_cpu_sync(bool synchronize) |
6a00d601 | 1332 | { |
751f8cfe GK |
1333 | CPUState *cpu; |
1334 | ||
1335 | if (cur_mon->mon_cpu_path) { | |
1336 | cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path, | |
1337 | TYPE_CPU, NULL); | |
1338 | if (!cpu) { | |
1339 | g_free(cur_mon->mon_cpu_path); | |
1340 | cur_mon->mon_cpu_path = NULL; | |
1341 | } | |
1342 | } | |
1343 | if (!cur_mon->mon_cpu_path) { | |
854e67fe TH |
1344 | if (!first_cpu) { |
1345 | return NULL; | |
1346 | } | |
27a83f8e | 1347 | monitor_set_cpu(first_cpu->cpu_index); |
751f8cfe | 1348 | cpu = first_cpu; |
6a00d601 | 1349 | } |
137b5cb6 VM |
1350 | if (synchronize) { |
1351 | cpu_synchronize_state(cpu); | |
1352 | } | |
751f8cfe | 1353 | return cpu; |
5bcda5f7 PC |
1354 | } |
1355 | ||
137b5cb6 VM |
1356 | CPUState *mon_get_cpu(void) |
1357 | { | |
1358 | return mon_get_cpu_sync(true); | |
1359 | } | |
1360 | ||
bf957284 | 1361 | CPUArchState *mon_get_cpu_env(void) |
5bcda5f7 | 1362 | { |
854e67fe TH |
1363 | CPUState *cs = mon_get_cpu(); |
1364 | ||
1365 | return cs ? cs->env_ptr : NULL; | |
6a00d601 FB |
1366 | } |
1367 | ||
99b7796f LC |
1368 | int monitor_get_cpu_index(void) |
1369 | { | |
137b5cb6 | 1370 | CPUState *cs = mon_get_cpu_sync(false); |
854e67fe TH |
1371 | |
1372 | return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX; | |
99b7796f LC |
1373 | } |
1374 | ||
1ce6be24 | 1375 | static void hmp_info_registers(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1376 | { |
18f08282 SJS |
1377 | bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false); |
1378 | CPUState *cs; | |
854e67fe | 1379 | |
18f08282 SJS |
1380 | if (all_cpus) { |
1381 | CPU_FOREACH(cs) { | |
1382 | monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index); | |
1383 | cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU); | |
1384 | } | |
1385 | } else { | |
1386 | cs = mon_get_cpu(); | |
1387 | ||
1388 | if (!cs) { | |
1389 | monitor_printf(mon, "No CPU available\n"); | |
1390 | return; | |
1391 | } | |
1392 | ||
1393 | cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU); | |
854e67fe | 1394 | } |
9307c4c1 FB |
1395 | } |
1396 | ||
f0d14a95 | 1397 | #ifdef CONFIG_TCG |
1ce6be24 | 1398 | static void hmp_info_jit(Monitor *mon, const QDict *qdict) |
e3db7226 | 1399 | { |
b7da97ee TH |
1400 | if (!tcg_enabled()) { |
1401 | error_report("JIT information is only available with accel=tcg"); | |
1402 | return; | |
1403 | } | |
1404 | ||
376253ec | 1405 | dump_exec_info((FILE *)mon, monitor_fprintf); |
27498bef | 1406 | dump_drift_info((FILE *)mon, monitor_fprintf); |
e3db7226 FB |
1407 | } |
1408 | ||
1ce6be24 | 1409 | static void hmp_info_opcount(Monitor *mon, const QDict *qdict) |
246ae24d MF |
1410 | { |
1411 | dump_opcount_info((FILE *)mon, monitor_fprintf); | |
1412 | } | |
f0d14a95 | 1413 | #endif |
246ae24d | 1414 | |
1ce6be24 | 1415 | static void hmp_info_history(Monitor *mon, const QDict *qdict) |
aa455485 FB |
1416 | { |
1417 | int i; | |
7e2515e8 | 1418 | const char *str; |
3b46e624 | 1419 | |
cde76ee1 AL |
1420 | if (!mon->rs) |
1421 | return; | |
7e2515e8 FB |
1422 | i = 0; |
1423 | for(;;) { | |
731b0364 | 1424 | str = readline_get_history(mon->rs, i); |
7e2515e8 FB |
1425 | if (!str) |
1426 | break; | |
376253ec | 1427 | monitor_printf(mon, "%d: '%s'\n", i, str); |
8e3a9fd2 | 1428 | i++; |
aa455485 FB |
1429 | } |
1430 | } | |
1431 | ||
1ce6be24 | 1432 | static void hmp_info_cpustats(Monitor *mon, const QDict *qdict) |
76a66253 | 1433 | { |
854e67fe TH |
1434 | CPUState *cs = mon_get_cpu(); |
1435 | ||
1436 | if (!cs) { | |
1437 | monitor_printf(mon, "No CPU available\n"); | |
1438 | return; | |
1439 | } | |
1440 | cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0); | |
76a66253 | 1441 | } |
76a66253 | 1442 | |
1ce6be24 | 1443 | static void hmp_info_trace_events(Monitor *mon, const QDict *qdict) |
22890ab5 | 1444 | { |
bd71211d | 1445 | const char *name = qdict_get_try_str(qdict, "name"); |
77e2b172 LV |
1446 | bool has_vcpu = qdict_haskey(qdict, "vcpu"); |
1447 | int vcpu = qdict_get_try_int(qdict, "vcpu", 0); | |
bd71211d | 1448 | TraceEventInfoList *events; |
14101d02 | 1449 | TraceEventInfoList *elem; |
bd71211d LV |
1450 | Error *local_err = NULL; |
1451 | ||
1452 | if (name == NULL) { | |
1453 | name = "*"; | |
1454 | } | |
77e2b172 LV |
1455 | if (vcpu < 0) { |
1456 | monitor_printf(mon, "argument vcpu must be positive"); | |
1457 | return; | |
1458 | } | |
bd71211d | 1459 | |
77e2b172 | 1460 | events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err); |
bd71211d LV |
1461 | if (local_err) { |
1462 | error_report_err(local_err); | |
1463 | return; | |
1464 | } | |
14101d02 LV |
1465 | |
1466 | for (elem = events; elem != NULL; elem = elem->next) { | |
1467 | monitor_printf(mon, "%s : state %u\n", | |
1468 | elem->value->name, | |
1469 | elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0); | |
1470 | } | |
1471 | qapi_free_TraceEventInfoList(events); | |
22890ab5 | 1472 | } |
22890ab5 | 1473 | |
b8a185bc MA |
1474 | void qmp_client_migrate_info(const char *protocol, const char *hostname, |
1475 | bool has_port, int64_t port, | |
1476 | bool has_tls_port, int64_t tls_port, | |
1477 | bool has_cert_subject, const char *cert_subject, | |
1478 | Error **errp) | |
e866e239 | 1479 | { |
e866e239 | 1480 | if (strcmp(protocol, "spice") == 0) { |
b8a185bc MA |
1481 | if (!qemu_using_spice(errp)) { |
1482 | return; | |
e866e239 GH |
1483 | } |
1484 | ||
b8a185bc | 1485 | if (!has_port && !has_tls_port) { |
c6bd8c70 | 1486 | error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port"); |
b8a185bc | 1487 | return; |
6ec5dae5 YH |
1488 | } |
1489 | ||
b8a185bc MA |
1490 | if (qemu_spice_migrate_info(hostname, |
1491 | has_port ? port : -1, | |
1492 | has_tls_port ? tls_port : -1, | |
1493 | cert_subject)) { | |
c6bd8c70 | 1494 | error_setg(errp, QERR_UNDEFINED_ERROR); |
b8a185bc | 1495 | return; |
e866e239 | 1496 | } |
b8a185bc | 1497 | return; |
e866e239 GH |
1498 | } |
1499 | ||
c6bd8c70 | 1500 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice"); |
e866e239 GH |
1501 | } |
1502 | ||
3e5a50d6 | 1503 | static void hmp_logfile(Monitor *mon, const QDict *qdict) |
e735b91c | 1504 | { |
daa76aa4 MA |
1505 | Error *err = NULL; |
1506 | ||
1507 | qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err); | |
1508 | if (err) { | |
1509 | error_report_err(err); | |
1510 | } | |
e735b91c PB |
1511 | } |
1512 | ||
3e5a50d6 | 1513 | static void hmp_log(Monitor *mon, const QDict *qdict) |
f193c797 FB |
1514 | { |
1515 | int mask; | |
d54908a5 | 1516 | const char *items = qdict_get_str(qdict, "items"); |
3b46e624 | 1517 | |
9307c4c1 | 1518 | if (!strcmp(items, "none")) { |
f193c797 FB |
1519 | mask = 0; |
1520 | } else { | |
4fde1eba | 1521 | mask = qemu_str_to_log_mask(items); |
f193c797 | 1522 | if (!mask) { |
376253ec | 1523 | help_cmd(mon, "log"); |
f193c797 FB |
1524 | return; |
1525 | } | |
1526 | } | |
24537a01 | 1527 | qemu_set_log(mask); |
f193c797 FB |
1528 | } |
1529 | ||
3e5a50d6 | 1530 | static void hmp_singlestep(Monitor *mon, const QDict *qdict) |
1b530a6d | 1531 | { |
d54908a5 | 1532 | const char *option = qdict_get_try_str(qdict, "option"); |
1b530a6d AJ |
1533 | if (!option || !strcmp(option, "on")) { |
1534 | singlestep = 1; | |
1535 | } else if (!strcmp(option, "off")) { | |
1536 | singlestep = 0; | |
1537 | } else { | |
1538 | monitor_printf(mon, "unexpected option %s\n", option); | |
1539 | } | |
1540 | } | |
1541 | ||
3e5a50d6 | 1542 | static void hmp_gdbserver(Monitor *mon, const QDict *qdict) |
59030a8c | 1543 | { |
d54908a5 | 1544 | const char *device = qdict_get_try_str(qdict, "device"); |
59030a8c AL |
1545 | if (!device) |
1546 | device = "tcp::" DEFAULT_GDBSTUB_PORT; | |
1547 | if (gdbserver_start(device) < 0) { | |
1548 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", | |
1549 | device); | |
1550 | } else if (strcmp(device, "none") == 0) { | |
36556b20 | 1551 | monitor_printf(mon, "Disabled gdbserver\n"); |
8a7ddc38 | 1552 | } else { |
59030a8c AL |
1553 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
1554 | device); | |
8a7ddc38 FB |
1555 | } |
1556 | } | |
1557 | ||
3e5a50d6 | 1558 | static void hmp_watchdog_action(Monitor *mon, const QDict *qdict) |
9dd986cc | 1559 | { |
d54908a5 | 1560 | const char *action = qdict_get_str(qdict, "action"); |
9dd986cc RJ |
1561 | if (select_watchdog_action(action) == -1) { |
1562 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); | |
1563 | } | |
1564 | } | |
1565 | ||
376253ec | 1566 | static void monitor_printc(Monitor *mon, int c) |
9307c4c1 | 1567 | { |
376253ec | 1568 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1569 | switch(c) { |
1570 | case '\'': | |
376253ec | 1571 | monitor_printf(mon, "\\'"); |
9307c4c1 FB |
1572 | break; |
1573 | case '\\': | |
376253ec | 1574 | monitor_printf(mon, "\\\\"); |
9307c4c1 FB |
1575 | break; |
1576 | case '\n': | |
376253ec | 1577 | monitor_printf(mon, "\\n"); |
9307c4c1 FB |
1578 | break; |
1579 | case '\r': | |
376253ec | 1580 | monitor_printf(mon, "\\r"); |
9307c4c1 FB |
1581 | break; |
1582 | default: | |
1583 | if (c >= 32 && c <= 126) { | |
376253ec | 1584 | monitor_printf(mon, "%c", c); |
9307c4c1 | 1585 | } else { |
376253ec | 1586 | monitor_printf(mon, "\\x%02x", c); |
9307c4c1 FB |
1587 | } |
1588 | break; | |
1589 | } | |
376253ec | 1590 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1591 | } |
1592 | ||
376253ec | 1593 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
a8170e5e | 1594 | hwaddr addr, int is_physical) |
9307c4c1 | 1595 | { |
23842aab | 1596 | int l, line_size, i, max_digits, len; |
9307c4c1 FB |
1597 | uint8_t buf[16]; |
1598 | uint64_t v; | |
854e67fe TH |
1599 | CPUState *cs = mon_get_cpu(); |
1600 | ||
1601 | if (!cs && (format == 'i' || !is_physical)) { | |
1602 | monitor_printf(mon, "Can not dump without CPU\n"); | |
1603 | return; | |
1604 | } | |
9307c4c1 FB |
1605 | |
1606 | if (format == 'i') { | |
1d48474d | 1607 | monitor_disas(mon, cs, addr, count, is_physical); |
9307c4c1 FB |
1608 | return; |
1609 | } | |
1610 | ||
1611 | len = wsize * count; | |
1612 | if (wsize == 1) | |
1613 | line_size = 8; | |
1614 | else | |
1615 | line_size = 16; | |
9307c4c1 FB |
1616 | max_digits = 0; |
1617 | ||
1618 | switch(format) { | |
1619 | case 'o': | |
69db8dfc | 1620 | max_digits = DIV_ROUND_UP(wsize * 8, 3); |
9307c4c1 FB |
1621 | break; |
1622 | default: | |
1623 | case 'x': | |
1624 | max_digits = (wsize * 8) / 4; | |
1625 | break; | |
1626 | case 'u': | |
1627 | case 'd': | |
69db8dfc | 1628 | max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33); |
9307c4c1 FB |
1629 | break; |
1630 | case 'c': | |
1631 | wsize = 1; | |
1632 | break; | |
1633 | } | |
1634 | ||
1635 | while (len > 0) { | |
7743e588 | 1636 | if (is_physical) |
376253ec | 1637 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
7743e588 | 1638 | else |
376253ec | 1639 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
9307c4c1 FB |
1640 | l = len; |
1641 | if (l > line_size) | |
1642 | l = line_size; | |
1643 | if (is_physical) { | |
54f7b4a3 | 1644 | cpu_physical_memory_read(addr, buf, l); |
9307c4c1 | 1645 | } else { |
854e67fe | 1646 | if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) { |
376253ec | 1647 | monitor_printf(mon, " Cannot access memory\n"); |
c8f79b67 AL |
1648 | break; |
1649 | } | |
9307c4c1 | 1650 | } |
5fafdf24 | 1651 | i = 0; |
9307c4c1 FB |
1652 | while (i < l) { |
1653 | switch(wsize) { | |
1654 | default: | |
1655 | case 1: | |
24e60305 | 1656 | v = ldub_p(buf + i); |
9307c4c1 FB |
1657 | break; |
1658 | case 2: | |
24e60305 | 1659 | v = lduw_p(buf + i); |
9307c4c1 FB |
1660 | break; |
1661 | case 4: | |
24e60305 | 1662 | v = (uint32_t)ldl_p(buf + i); |
9307c4c1 FB |
1663 | break; |
1664 | case 8: | |
24e60305 | 1665 | v = ldq_p(buf + i); |
9307c4c1 FB |
1666 | break; |
1667 | } | |
376253ec | 1668 | monitor_printf(mon, " "); |
9307c4c1 FB |
1669 | switch(format) { |
1670 | case 'o': | |
376253ec | 1671 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
9307c4c1 FB |
1672 | break; |
1673 | case 'x': | |
376253ec | 1674 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
9307c4c1 FB |
1675 | break; |
1676 | case 'u': | |
376253ec | 1677 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
9307c4c1 FB |
1678 | break; |
1679 | case 'd': | |
376253ec | 1680 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
9307c4c1 FB |
1681 | break; |
1682 | case 'c': | |
376253ec | 1683 | monitor_printc(mon, v); |
9307c4c1 FB |
1684 | break; |
1685 | } | |
1686 | i += wsize; | |
1687 | } | |
376253ec | 1688 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1689 | addr += l; |
1690 | len -= l; | |
1691 | } | |
1692 | } | |
1693 | ||
3e5a50d6 | 1694 | static void hmp_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1695 | { |
1bd1442e LC |
1696 | int count = qdict_get_int(qdict, "count"); |
1697 | int format = qdict_get_int(qdict, "format"); | |
1698 | int size = qdict_get_int(qdict, "size"); | |
1699 | target_long addr = qdict_get_int(qdict, "addr"); | |
1700 | ||
376253ec | 1701 | memory_dump(mon, count, format, size, addr, 0); |
9307c4c1 FB |
1702 | } |
1703 | ||
3e5a50d6 | 1704 | static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1705 | { |
1bd1442e LC |
1706 | int count = qdict_get_int(qdict, "count"); |
1707 | int format = qdict_get_int(qdict, "format"); | |
1708 | int size = qdict_get_int(qdict, "size"); | |
a8170e5e | 1709 | hwaddr addr = qdict_get_int(qdict, "addr"); |
1bd1442e | 1710 | |
376253ec | 1711 | memory_dump(mon, count, format, size, addr, 1); |
9307c4c1 FB |
1712 | } |
1713 | ||
e9628441 PB |
1714 | static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp) |
1715 | { | |
1716 | MemoryRegionSection mrs = memory_region_find(get_system_memory(), | |
1717 | addr, 1); | |
1718 | ||
1719 | if (!mrs.mr) { | |
1720 | error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr); | |
1721 | return NULL; | |
1722 | } | |
1723 | ||
1724 | if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) { | |
1725 | error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr); | |
1726 | memory_region_unref(mrs.mr); | |
1727 | return NULL; | |
1728 | } | |
1729 | ||
1730 | *p_mr = mrs.mr; | |
1731 | return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region); | |
1732 | } | |
1733 | ||
1734 | static void hmp_gpa2hva(Monitor *mon, const QDict *qdict) | |
1735 | { | |
1736 | hwaddr addr = qdict_get_int(qdict, "addr"); | |
1737 | Error *local_err = NULL; | |
1738 | MemoryRegion *mr = NULL; | |
1739 | void *ptr; | |
1740 | ||
1741 | ptr = gpa2hva(&mr, addr, &local_err); | |
1742 | if (local_err) { | |
1743 | error_report_err(local_err); | |
1744 | return; | |
1745 | } | |
1746 | ||
1747 | monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx | |
1748 | " (%s) is %p\n", | |
1749 | addr, mr->name, ptr); | |
1750 | ||
1751 | memory_region_unref(mr); | |
1752 | } | |
1753 | ||
1754 | #ifdef CONFIG_LINUX | |
1755 | static uint64_t vtop(void *ptr, Error **errp) | |
1756 | { | |
1757 | uint64_t pinfo; | |
1758 | uint64_t ret = -1; | |
1759 | uintptr_t addr = (uintptr_t) ptr; | |
1760 | uintptr_t pagesize = getpagesize(); | |
1761 | off_t offset = addr / pagesize * sizeof(pinfo); | |
1762 | int fd; | |
1763 | ||
1764 | fd = open("/proc/self/pagemap", O_RDONLY); | |
1765 | if (fd == -1) { | |
1766 | error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap"); | |
1767 | return -1; | |
1768 | } | |
1769 | ||
1770 | /* Force copy-on-write if necessary. */ | |
1771 | atomic_add((uint8_t *)ptr, 0); | |
1772 | ||
1773 | if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) { | |
1774 | error_setg_errno(errp, errno, "Cannot read pagemap"); | |
1775 | goto out; | |
1776 | } | |
1777 | if ((pinfo & (1ull << 63)) == 0) { | |
1778 | error_setg(errp, "Page not present"); | |
1779 | goto out; | |
1780 | } | |
1781 | ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1)); | |
1782 | ||
1783 | out: | |
1784 | close(fd); | |
1785 | return ret; | |
1786 | } | |
1787 | ||
1788 | static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict) | |
1789 | { | |
1790 | hwaddr addr = qdict_get_int(qdict, "addr"); | |
1791 | Error *local_err = NULL; | |
1792 | MemoryRegion *mr = NULL; | |
1793 | void *ptr; | |
1794 | uint64_t physaddr; | |
1795 | ||
1796 | ptr = gpa2hva(&mr, addr, &local_err); | |
1797 | if (local_err) { | |
1798 | error_report_err(local_err); | |
1799 | return; | |
1800 | } | |
1801 | ||
1802 | physaddr = vtop(ptr, &local_err); | |
1803 | if (local_err) { | |
1804 | error_report_err(local_err); | |
1805 | } else { | |
1806 | monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx | |
1807 | " (%s) is 0x%" PRIx64 "\n", | |
1808 | addr, mr->name, (uint64_t) physaddr); | |
1809 | } | |
1810 | ||
1811 | memory_region_unref(mr); | |
1812 | } | |
1813 | #endif | |
1814 | ||
1bd1442e | 1815 | static void do_print(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1816 | { |
1bd1442e | 1817 | int format = qdict_get_int(qdict, "format"); |
a8170e5e | 1818 | hwaddr val = qdict_get_int(qdict, "val"); |
1bd1442e | 1819 | |
9307c4c1 FB |
1820 | switch(format) { |
1821 | case 'o': | |
a8170e5e | 1822 | monitor_printf(mon, "%#" HWADDR_PRIo, val); |
9307c4c1 FB |
1823 | break; |
1824 | case 'x': | |
a8170e5e | 1825 | monitor_printf(mon, "%#" HWADDR_PRIx, val); |
9307c4c1 FB |
1826 | break; |
1827 | case 'u': | |
a8170e5e | 1828 | monitor_printf(mon, "%" HWADDR_PRIu, val); |
9307c4c1 FB |
1829 | break; |
1830 | default: | |
1831 | case 'd': | |
a8170e5e | 1832 | monitor_printf(mon, "%" HWADDR_PRId, val); |
9307c4c1 FB |
1833 | break; |
1834 | case 'c': | |
376253ec | 1835 | monitor_printc(mon, val); |
9307c4c1 FB |
1836 | break; |
1837 | } | |
376253ec | 1838 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1839 | } |
1840 | ||
3e5a50d6 | 1841 | static void hmp_sum(Monitor *mon, const QDict *qdict) |
e4cf1adc FB |
1842 | { |
1843 | uint32_t addr; | |
e4cf1adc | 1844 | uint16_t sum; |
f18c16de LC |
1845 | uint32_t start = qdict_get_int(qdict, "start"); |
1846 | uint32_t size = qdict_get_int(qdict, "size"); | |
e4cf1adc FB |
1847 | |
1848 | sum = 0; | |
1849 | for(addr = start; addr < (start + size); addr++) { | |
42874d3a PM |
1850 | uint8_t val = address_space_ldub(&address_space_memory, addr, |
1851 | MEMTXATTRS_UNSPECIFIED, NULL); | |
e4cf1adc FB |
1852 | /* BSD sum algorithm ('sum' Unix command) */ |
1853 | sum = (sum >> 1) | (sum << 15); | |
54f7b4a3 | 1854 | sum += val; |
e4cf1adc | 1855 | } |
376253ec | 1856 | monitor_printf(mon, "%05d\n", sum); |
e4cf1adc FB |
1857 | } |
1858 | ||
13224a87 FB |
1859 | static int mouse_button_state; |
1860 | ||
3e5a50d6 | 1861 | static void hmp_mouse_move(Monitor *mon, const QDict *qdict) |
13224a87 | 1862 | { |
c751a74a | 1863 | int dx, dy, dz, button; |
1d4daa91 LC |
1864 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
1865 | const char *dy_str = qdict_get_str(qdict, "dy_str"); | |
1866 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); | |
c751a74a | 1867 | |
13224a87 FB |
1868 | dx = strtol(dx_str, NULL, 0); |
1869 | dy = strtol(dy_str, NULL, 0); | |
c751a74a GH |
1870 | qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx); |
1871 | qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy); | |
1872 | ||
1873 | if (dz_str) { | |
13224a87 | 1874 | dz = strtol(dz_str, NULL, 0); |
c751a74a | 1875 | if (dz != 0) { |
f22d0af0 | 1876 | button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN; |
c751a74a GH |
1877 | qemu_input_queue_btn(NULL, button, true); |
1878 | qemu_input_event_sync(); | |
1879 | qemu_input_queue_btn(NULL, button, false); | |
1880 | } | |
1881 | } | |
1882 | qemu_input_event_sync(); | |
13224a87 FB |
1883 | } |
1884 | ||
3e5a50d6 | 1885 | static void hmp_mouse_button(Monitor *mon, const QDict *qdict) |
13224a87 | 1886 | { |
7fb1cf16 | 1887 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
c751a74a GH |
1888 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
1889 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, | |
1890 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, | |
1891 | }; | |
d54908a5 | 1892 | int button_state = qdict_get_int(qdict, "button_state"); |
c751a74a GH |
1893 | |
1894 | if (mouse_button_state == button_state) { | |
1895 | return; | |
1896 | } | |
1897 | qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state); | |
1898 | qemu_input_event_sync(); | |
13224a87 | 1899 | mouse_button_state = button_state; |
13224a87 FB |
1900 | } |
1901 | ||
3e5a50d6 | 1902 | static void hmp_ioport_read(Monitor *mon, const QDict *qdict) |
3440557b | 1903 | { |
aa93e39c LC |
1904 | int size = qdict_get_int(qdict, "size"); |
1905 | int addr = qdict_get_int(qdict, "addr"); | |
1906 | int has_index = qdict_haskey(qdict, "index"); | |
3440557b FB |
1907 | uint32_t val; |
1908 | int suffix; | |
1909 | ||
1910 | if (has_index) { | |
aa93e39c | 1911 | int index = qdict_get_int(qdict, "index"); |
afcea8cb | 1912 | cpu_outb(addr & IOPORTS_MASK, index & 0xff); |
3440557b FB |
1913 | addr++; |
1914 | } | |
1915 | addr &= 0xffff; | |
1916 | ||
1917 | switch(size) { | |
1918 | default: | |
1919 | case 1: | |
afcea8cb | 1920 | val = cpu_inb(addr); |
3440557b FB |
1921 | suffix = 'b'; |
1922 | break; | |
1923 | case 2: | |
afcea8cb | 1924 | val = cpu_inw(addr); |
3440557b FB |
1925 | suffix = 'w'; |
1926 | break; | |
1927 | case 4: | |
afcea8cb | 1928 | val = cpu_inl(addr); |
3440557b FB |
1929 | suffix = 'l'; |
1930 | break; | |
1931 | } | |
376253ec AL |
1932 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
1933 | suffix, addr, size * 2, val); | |
3440557b | 1934 | } |
a3a91a35 | 1935 | |
3e5a50d6 | 1936 | static void hmp_ioport_write(Monitor *mon, const QDict *qdict) |
f114784f | 1937 | { |
1bd1442e LC |
1938 | int size = qdict_get_int(qdict, "size"); |
1939 | int addr = qdict_get_int(qdict, "addr"); | |
1940 | int val = qdict_get_int(qdict, "val"); | |
1941 | ||
f114784f JK |
1942 | addr &= IOPORTS_MASK; |
1943 | ||
1944 | switch (size) { | |
1945 | default: | |
1946 | case 1: | |
afcea8cb | 1947 | cpu_outb(addr, val); |
f114784f JK |
1948 | break; |
1949 | case 2: | |
afcea8cb | 1950 | cpu_outw(addr, val); |
f114784f JK |
1951 | break; |
1952 | case 4: | |
afcea8cb | 1953 | cpu_outl(addr, val); |
f114784f JK |
1954 | break; |
1955 | } | |
1956 | } | |
1957 | ||
3e5a50d6 | 1958 | static void hmp_boot_set(Monitor *mon, const QDict *qdict) |
0ecdffbb | 1959 | { |
f1839938 | 1960 | Error *local_err = NULL; |
d54908a5 | 1961 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
0ecdffbb | 1962 | |
f1839938 GA |
1963 | qemu_boot_set(bootdevice, &local_err); |
1964 | if (local_err) { | |
193227f9 | 1965 | error_report_err(local_err); |
0ecdffbb | 1966 | } else { |
f1839938 | 1967 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); |
0ecdffbb AJ |
1968 | } |
1969 | } | |
1970 | ||
1ce6be24 | 1971 | static void hmp_info_mtree(Monitor *mon, const QDict *qdict) |
314e2987 | 1972 | { |
57bb40c9 | 1973 | bool flatview = qdict_get_try_bool(qdict, "flatview", false); |
5e8fd947 | 1974 | bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false); |
fc051ae6 | 1975 | bool owner = qdict_get_try_bool(qdict, "owner", false); |
57bb40c9 | 1976 | |
fc051ae6 AK |
1977 | mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree, |
1978 | owner); | |
314e2987 BS |
1979 | } |
1980 | ||
1ce6be24 | 1981 | static void hmp_info_numa(Monitor *mon, const QDict *qdict) |
030ea37b | 1982 | { |
b28b6230 | 1983 | int i; |
31959e82 | 1984 | NumaNodeMem *node_mem; |
f75cd44d | 1985 | CpuInfoList *cpu_list, *cpu; |
030ea37b | 1986 | |
f75cd44d | 1987 | cpu_list = qmp_query_cpus(&error_abort); |
31959e82 VG |
1988 | node_mem = g_new0(NumaNodeMem, nb_numa_nodes); |
1989 | ||
5b009e40 | 1990 | query_numa_node_mem(node_mem); |
030ea37b AL |
1991 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); |
1992 | for (i = 0; i < nb_numa_nodes; i++) { | |
1993 | monitor_printf(mon, "node %d cpus:", i); | |
f75cd44d IM |
1994 | for (cpu = cpu_list; cpu; cpu = cpu->next) { |
1995 | if (cpu->value->has_props && cpu->value->props->has_node_id && | |
1996 | cpu->value->props->node_id == i) { | |
1997 | monitor_printf(mon, " %" PRIi64, cpu->value->CPU); | |
030ea37b AL |
1998 | } |
1999 | } | |
2000 | monitor_printf(mon, "\n"); | |
2001 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, | |
31959e82 VG |
2002 | node_mem[i].node_mem >> 20); |
2003 | monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i, | |
2004 | node_mem[i].node_plugged_mem >> 20); | |
030ea37b | 2005 | } |
f75cd44d | 2006 | qapi_free_CpuInfoList(cpu_list); |
5b009e40 | 2007 | g_free(node_mem); |
030ea37b AL |
2008 | } |
2009 | ||
5f1ce948 FB |
2010 | #ifdef CONFIG_PROFILER |
2011 | ||
89d5cbdd | 2012 | int64_t tcg_time; |
e9a6625e AJ |
2013 | int64_t dev_time; |
2014 | ||
1ce6be24 | 2015 | static void hmp_info_profile(Monitor *mon, const QDict *qdict) |
5f1ce948 | 2016 | { |
376253ec | 2017 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
73bcb24d | 2018 | dev_time, dev_time / (double)NANOSECONDS_PER_SECOND); |
376253ec | 2019 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", |
73bcb24d | 2020 | tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND); |
89d5cbdd | 2021 | tcg_time = 0; |
5f1ce948 | 2022 | dev_time = 0; |
5f1ce948 FB |
2023 | } |
2024 | #else | |
1ce6be24 | 2025 | static void hmp_info_profile(Monitor *mon, const QDict *qdict) |
5f1ce948 | 2026 | { |
376253ec | 2027 | monitor_printf(mon, "Internal profiler not compiled\n"); |
5f1ce948 FB |
2028 | } |
2029 | #endif | |
2030 | ||
ec36b695 | 2031 | /* Capture support */ |
72cf2d4f | 2032 | static QLIST_HEAD (capture_list_head, CaptureState) capture_head; |
ec36b695 | 2033 | |
1ce6be24 | 2034 | static void hmp_info_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
2035 | { |
2036 | int i; | |
2037 | CaptureState *s; | |
2038 | ||
2039 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
376253ec | 2040 | monitor_printf(mon, "[%d]: ", i); |
ec36b695 FB |
2041 | s->ops.info (s->opaque); |
2042 | } | |
2043 | } | |
2044 | ||
3e5a50d6 | 2045 | static void hmp_stopcapture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
2046 | { |
2047 | int i; | |
d54908a5 | 2048 | int n = qdict_get_int(qdict, "n"); |
ec36b695 FB |
2049 | CaptureState *s; |
2050 | ||
2051 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
2052 | if (i == n) { | |
2053 | s->ops.destroy (s->opaque); | |
72cf2d4f | 2054 | QLIST_REMOVE (s, entries); |
7267c094 | 2055 | g_free (s); |
ec36b695 FB |
2056 | return; |
2057 | } | |
2058 | } | |
2059 | } | |
2060 | ||
3e5a50d6 | 2061 | static void hmp_wavcapture(Monitor *mon, const QDict *qdict) |
c1925484 LC |
2062 | { |
2063 | const char *path = qdict_get_str(qdict, "path"); | |
2064 | int has_freq = qdict_haskey(qdict, "freq"); | |
2065 | int freq = qdict_get_try_int(qdict, "freq", -1); | |
2066 | int has_bits = qdict_haskey(qdict, "bits"); | |
2067 | int bits = qdict_get_try_int(qdict, "bits", -1); | |
2068 | int has_channels = qdict_haskey(qdict, "nchannels"); | |
2069 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); | |
ec36b695 FB |
2070 | CaptureState *s; |
2071 | ||
7267c094 | 2072 | s = g_malloc0 (sizeof (*s)); |
ec36b695 FB |
2073 | |
2074 | freq = has_freq ? freq : 44100; | |
2075 | bits = has_bits ? bits : 16; | |
2076 | nchannels = has_channels ? nchannels : 2; | |
2077 | ||
2078 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
d00b2618 | 2079 | monitor_printf(mon, "Failed to add wave capture\n"); |
7267c094 | 2080 | g_free (s); |
d00b2618 | 2081 | return; |
ec36b695 | 2082 | } |
72cf2d4f | 2083 | QLIST_INSERT_HEAD (&capture_head, s, entries); |
ec36b695 | 2084 | } |
ec36b695 | 2085 | |
15dfcd45 | 2086 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
76655d6d | 2087 | { |
15dfcd45 | 2088 | qemu_acl *acl = qemu_acl_find(name); |
76655d6d | 2089 | |
76655d6d | 2090 | if (!acl) { |
15dfcd45 | 2091 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
76655d6d | 2092 | } |
15dfcd45 JK |
2093 | return acl; |
2094 | } | |
2095 | ||
3e5a50d6 | 2096 | static void hmp_acl_show(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2097 | { |
d54908a5 | 2098 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2099 | qemu_acl *acl = find_acl(mon, aclname); |
2100 | qemu_acl_entry *entry; | |
2101 | int i = 0; | |
76655d6d | 2102 | |
15dfcd45 | 2103 | if (acl) { |
28a76be8 | 2104 | monitor_printf(mon, "policy: %s\n", |
76655d6d | 2105 | acl->defaultDeny ? "deny" : "allow"); |
72cf2d4f | 2106 | QTAILQ_FOREACH(entry, &acl->entries, next) { |
28a76be8 AL |
2107 | i++; |
2108 | monitor_printf(mon, "%d: %s %s\n", i, | |
15dfcd45 | 2109 | entry->deny ? "deny" : "allow", entry->match); |
28a76be8 | 2110 | } |
15dfcd45 JK |
2111 | } |
2112 | } | |
2113 | ||
3e5a50d6 | 2114 | static void hmp_acl_reset(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2115 | { |
d54908a5 | 2116 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2117 | qemu_acl *acl = find_acl(mon, aclname); |
2118 | ||
2119 | if (acl) { | |
28a76be8 AL |
2120 | qemu_acl_reset(acl); |
2121 | monitor_printf(mon, "acl: removed all rules\n"); | |
15dfcd45 JK |
2122 | } |
2123 | } | |
2124 | ||
3e5a50d6 | 2125 | static void hmp_acl_policy(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2126 | { |
f18c16de LC |
2127 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2128 | const char *policy = qdict_get_str(qdict, "policy"); | |
15dfcd45 | 2129 | qemu_acl *acl = find_acl(mon, aclname); |
28a76be8 | 2130 | |
15dfcd45 JK |
2131 | if (acl) { |
2132 | if (strcmp(policy, "allow") == 0) { | |
28a76be8 AL |
2133 | acl->defaultDeny = 0; |
2134 | monitor_printf(mon, "acl: policy set to 'allow'\n"); | |
15dfcd45 | 2135 | } else if (strcmp(policy, "deny") == 0) { |
28a76be8 AL |
2136 | acl->defaultDeny = 1; |
2137 | monitor_printf(mon, "acl: policy set to 'deny'\n"); | |
2138 | } else { | |
15dfcd45 JK |
2139 | monitor_printf(mon, "acl: unknown policy '%s', " |
2140 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 | 2141 | } |
15dfcd45 JK |
2142 | } |
2143 | } | |
28a76be8 | 2144 | |
3e5a50d6 | 2145 | static void hmp_acl_add(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2146 | { |
1bd1442e LC |
2147 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2148 | const char *match = qdict_get_str(qdict, "match"); | |
2149 | const char *policy = qdict_get_str(qdict, "policy"); | |
2150 | int has_index = qdict_haskey(qdict, "index"); | |
2151 | int index = qdict_get_try_int(qdict, "index", -1); | |
15dfcd45 JK |
2152 | qemu_acl *acl = find_acl(mon, aclname); |
2153 | int deny, ret; | |
2154 | ||
2155 | if (acl) { | |
2156 | if (strcmp(policy, "allow") == 0) { | |
2157 | deny = 0; | |
2158 | } else if (strcmp(policy, "deny") == 0) { | |
2159 | deny = 1; | |
2160 | } else { | |
2161 | monitor_printf(mon, "acl: unknown policy '%s', " | |
2162 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 AL |
2163 | return; |
2164 | } | |
28a76be8 AL |
2165 | if (has_index) |
2166 | ret = qemu_acl_insert(acl, deny, match, index); | |
2167 | else | |
2168 | ret = qemu_acl_append(acl, deny, match); | |
2169 | if (ret < 0) | |
2170 | monitor_printf(mon, "acl: unable to add acl entry\n"); | |
2171 | else | |
2172 | monitor_printf(mon, "acl: added rule at position %d\n", ret); | |
15dfcd45 JK |
2173 | } |
2174 | } | |
28a76be8 | 2175 | |
3e5a50d6 | 2176 | static void hmp_acl_remove(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2177 | { |
f18c16de LC |
2178 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2179 | const char *match = qdict_get_str(qdict, "match"); | |
15dfcd45 JK |
2180 | qemu_acl *acl = find_acl(mon, aclname); |
2181 | int ret; | |
28a76be8 | 2182 | |
15dfcd45 | 2183 | if (acl) { |
28a76be8 AL |
2184 | ret = qemu_acl_remove(acl, match); |
2185 | if (ret < 0) | |
2186 | monitor_printf(mon, "acl: no matching acl entry\n"); | |
2187 | else | |
2188 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); | |
76655d6d AL |
2189 | } |
2190 | } | |
2191 | ||
208c9d1b | 2192 | void qmp_getfd(const char *fdname, Error **errp) |
f07918fd | 2193 | { |
c227f099 | 2194 | mon_fd_t *monfd; |
9409fc05 | 2195 | int fd, tmp_fd; |
f07918fd | 2196 | |
5345fdb4 | 2197 | fd = qemu_chr_fe_get_msgfd(&cur_mon->chr); |
f07918fd | 2198 | if (fd == -1) { |
c6bd8c70 | 2199 | error_setg(errp, QERR_FD_NOT_SUPPLIED); |
208c9d1b | 2200 | return; |
f07918fd MM |
2201 | } |
2202 | ||
2203 | if (qemu_isdigit(fdname[0])) { | |
0b9f0e2f | 2204 | close(fd); |
c6bd8c70 MA |
2205 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname", |
2206 | "a name not starting with a digit"); | |
208c9d1b | 2207 | return; |
f07918fd MM |
2208 | } |
2209 | ||
9409fc05 | 2210 | qemu_mutex_lock(&cur_mon->mon_lock); |
208c9d1b | 2211 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
f07918fd MM |
2212 | if (strcmp(monfd->name, fdname) != 0) { |
2213 | continue; | |
2214 | } | |
2215 | ||
9409fc05 | 2216 | tmp_fd = monfd->fd; |
f07918fd | 2217 | monfd->fd = fd; |
9409fc05 | 2218 | qemu_mutex_unlock(&cur_mon->mon_lock); |
774a6b67 | 2219 | /* Make sure close() is outside critical section */ |
9409fc05 | 2220 | close(tmp_fd); |
208c9d1b | 2221 | return; |
f07918fd MM |
2222 | } |
2223 | ||
7267c094 AL |
2224 | monfd = g_malloc0(sizeof(mon_fd_t)); |
2225 | monfd->name = g_strdup(fdname); | |
f07918fd MM |
2226 | monfd->fd = fd; |
2227 | ||
208c9d1b | 2228 | QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next); |
9409fc05 | 2229 | qemu_mutex_unlock(&cur_mon->mon_lock); |
f07918fd MM |
2230 | } |
2231 | ||
208c9d1b | 2232 | void qmp_closefd(const char *fdname, Error **errp) |
f07918fd | 2233 | { |
c227f099 | 2234 | mon_fd_t *monfd; |
9409fc05 | 2235 | int tmp_fd; |
f07918fd | 2236 | |
9409fc05 | 2237 | qemu_mutex_lock(&cur_mon->mon_lock); |
208c9d1b | 2238 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
f07918fd MM |
2239 | if (strcmp(monfd->name, fdname) != 0) { |
2240 | continue; | |
2241 | } | |
2242 | ||
72cf2d4f | 2243 | QLIST_REMOVE(monfd, next); |
9409fc05 | 2244 | tmp_fd = monfd->fd; |
7267c094 AL |
2245 | g_free(monfd->name); |
2246 | g_free(monfd); | |
9409fc05 | 2247 | qemu_mutex_unlock(&cur_mon->mon_lock); |
774a6b67 | 2248 | /* Make sure close() is outside critical section */ |
9409fc05 | 2249 | close(tmp_fd); |
208c9d1b | 2250 | return; |
f07918fd MM |
2251 | } |
2252 | ||
9409fc05 | 2253 | qemu_mutex_unlock(&cur_mon->mon_lock); |
c6bd8c70 | 2254 | error_setg(errp, QERR_FD_NOT_FOUND, fdname); |
f07918fd MM |
2255 | } |
2256 | ||
a9940fc4 | 2257 | int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) |
7768e04c | 2258 | { |
c227f099 | 2259 | mon_fd_t *monfd; |
7768e04c | 2260 | |
9409fc05 | 2261 | qemu_mutex_lock(&mon->mon_lock); |
72cf2d4f | 2262 | QLIST_FOREACH(monfd, &mon->fds, next) { |
7768e04c MM |
2263 | int fd; |
2264 | ||
2265 | if (strcmp(monfd->name, fdname) != 0) { | |
2266 | continue; | |
2267 | } | |
2268 | ||
2269 | fd = monfd->fd; | |
2270 | ||
2271 | /* caller takes ownership of fd */ | |
72cf2d4f | 2272 | QLIST_REMOVE(monfd, next); |
7267c094 AL |
2273 | g_free(monfd->name); |
2274 | g_free(monfd); | |
9409fc05 | 2275 | qemu_mutex_unlock(&mon->mon_lock); |
7768e04c MM |
2276 | |
2277 | return fd; | |
2278 | } | |
2279 | ||
9409fc05 | 2280 | qemu_mutex_unlock(&mon->mon_lock); |
a9940fc4 | 2281 | error_setg(errp, "File descriptor named '%s' has not been found", fdname); |
7768e04c MM |
2282 | return -1; |
2283 | } | |
2284 | ||
ba1c048a CB |
2285 | static void monitor_fdset_cleanup(MonFdset *mon_fdset) |
2286 | { | |
2287 | MonFdsetFd *mon_fdset_fd; | |
2288 | MonFdsetFd *mon_fdset_fd_next; | |
2289 | ||
2290 | QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) { | |
ebe52b59 CB |
2291 | if ((mon_fdset_fd->removed || |
2292 | (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) && | |
2293 | runstate_is_running()) { | |
ba1c048a CB |
2294 | close(mon_fdset_fd->fd); |
2295 | g_free(mon_fdset_fd->opaque); | |
2296 | QLIST_REMOVE(mon_fdset_fd, next); | |
2297 | g_free(mon_fdset_fd); | |
2298 | } | |
2299 | } | |
2300 | ||
adb696f3 | 2301 | if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) { |
ba1c048a CB |
2302 | QLIST_REMOVE(mon_fdset, next); |
2303 | g_free(mon_fdset); | |
2304 | } | |
2305 | } | |
2306 | ||
efb87c16 CB |
2307 | static void monitor_fdsets_cleanup(void) |
2308 | { | |
2309 | MonFdset *mon_fdset; | |
2310 | MonFdset *mon_fdset_next; | |
2311 | ||
47451466 | 2312 | qemu_mutex_lock(&mon_fdsets_lock); |
efb87c16 CB |
2313 | QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) { |
2314 | monitor_fdset_cleanup(mon_fdset); | |
2315 | } | |
47451466 | 2316 | qemu_mutex_unlock(&mon_fdsets_lock); |
efb87c16 CB |
2317 | } |
2318 | ||
ba1c048a CB |
2319 | AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque, |
2320 | const char *opaque, Error **errp) | |
2321 | { | |
2322 | int fd; | |
2323 | Monitor *mon = cur_mon; | |
ba1c048a CB |
2324 | AddfdInfo *fdinfo; |
2325 | ||
5345fdb4 | 2326 | fd = qemu_chr_fe_get_msgfd(&mon->chr); |
ba1c048a | 2327 | if (fd == -1) { |
c6bd8c70 | 2328 | error_setg(errp, QERR_FD_NOT_SUPPLIED); |
ba1c048a CB |
2329 | goto error; |
2330 | } | |
2331 | ||
e446f70d CB |
2332 | fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id, |
2333 | has_opaque, opaque, errp); | |
2334 | if (fdinfo) { | |
2335 | return fdinfo; | |
ba1c048a | 2336 | } |
ba1c048a CB |
2337 | |
2338 | error: | |
2339 | if (fd != -1) { | |
2340 | close(fd); | |
2341 | } | |
2342 | return NULL; | |
2343 | } | |
2344 | ||
2345 | void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) | |
2346 | { | |
2347 | MonFdset *mon_fdset; | |
2348 | MonFdsetFd *mon_fdset_fd; | |
2349 | char fd_str[60]; | |
2350 | ||
47451466 | 2351 | qemu_mutex_lock(&mon_fdsets_lock); |
ba1c048a CB |
2352 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
2353 | if (mon_fdset->id != fdset_id) { | |
2354 | continue; | |
2355 | } | |
2356 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2357 | if (has_fd) { | |
2358 | if (mon_fdset_fd->fd != fd) { | |
2359 | continue; | |
2360 | } | |
2361 | mon_fdset_fd->removed = true; | |
2362 | break; | |
2363 | } else { | |
2364 | mon_fdset_fd->removed = true; | |
2365 | } | |
2366 | } | |
2367 | if (has_fd && !mon_fdset_fd) { | |
2368 | goto error; | |
2369 | } | |
2370 | monitor_fdset_cleanup(mon_fdset); | |
47451466 | 2371 | qemu_mutex_unlock(&mon_fdsets_lock); |
ba1c048a CB |
2372 | return; |
2373 | } | |
2374 | ||
2375 | error: | |
47451466 | 2376 | qemu_mutex_unlock(&mon_fdsets_lock); |
ba1c048a CB |
2377 | if (has_fd) { |
2378 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64, | |
2379 | fdset_id, fd); | |
2380 | } else { | |
2381 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id); | |
2382 | } | |
c6bd8c70 | 2383 | error_setg(errp, QERR_FD_NOT_FOUND, fd_str); |
ba1c048a CB |
2384 | } |
2385 | ||
2386 | FdsetInfoList *qmp_query_fdsets(Error **errp) | |
2387 | { | |
2388 | MonFdset *mon_fdset; | |
2389 | MonFdsetFd *mon_fdset_fd; | |
2390 | FdsetInfoList *fdset_list = NULL; | |
2391 | ||
47451466 | 2392 | qemu_mutex_lock(&mon_fdsets_lock); |
ba1c048a CB |
2393 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
2394 | FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info)); | |
2395 | FdsetFdInfoList *fdsetfd_list = NULL; | |
2396 | ||
2397 | fdset_info->value = g_malloc0(sizeof(*fdset_info->value)); | |
2398 | fdset_info->value->fdset_id = mon_fdset->id; | |
2399 | ||
2400 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2401 | FdsetFdInfoList *fdsetfd_info; | |
2402 | ||
2403 | fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info)); | |
2404 | fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value)); | |
2405 | fdsetfd_info->value->fd = mon_fdset_fd->fd; | |
2406 | if (mon_fdset_fd->opaque) { | |
2407 | fdsetfd_info->value->has_opaque = true; | |
2408 | fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque); | |
2409 | } else { | |
2410 | fdsetfd_info->value->has_opaque = false; | |
2411 | } | |
2412 | ||
2413 | fdsetfd_info->next = fdsetfd_list; | |
2414 | fdsetfd_list = fdsetfd_info; | |
2415 | } | |
2416 | ||
2417 | fdset_info->value->fds = fdsetfd_list; | |
2418 | ||
2419 | fdset_info->next = fdset_list; | |
2420 | fdset_list = fdset_info; | |
2421 | } | |
47451466 | 2422 | qemu_mutex_unlock(&mon_fdsets_lock); |
ba1c048a CB |
2423 | |
2424 | return fdset_list; | |
2425 | } | |
2426 | ||
e446f70d CB |
2427 | AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id, |
2428 | bool has_opaque, const char *opaque, | |
2429 | Error **errp) | |
2430 | { | |
2431 | MonFdset *mon_fdset = NULL; | |
2432 | MonFdsetFd *mon_fdset_fd; | |
2433 | AddfdInfo *fdinfo; | |
2434 | ||
47451466 | 2435 | qemu_mutex_lock(&mon_fdsets_lock); |
e446f70d CB |
2436 | if (has_fdset_id) { |
2437 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2438 | /* Break if match found or match impossible due to ordering by ID */ | |
2439 | if (fdset_id <= mon_fdset->id) { | |
2440 | if (fdset_id < mon_fdset->id) { | |
2441 | mon_fdset = NULL; | |
2442 | } | |
2443 | break; | |
2444 | } | |
2445 | } | |
2446 | } | |
2447 | ||
2448 | if (mon_fdset == NULL) { | |
2449 | int64_t fdset_id_prev = -1; | |
2450 | MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets); | |
2451 | ||
2452 | if (has_fdset_id) { | |
2453 | if (fdset_id < 0) { | |
c6bd8c70 MA |
2454 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id", |
2455 | "a non-negative value"); | |
47451466 | 2456 | qemu_mutex_unlock(&mon_fdsets_lock); |
e446f70d CB |
2457 | return NULL; |
2458 | } | |
2459 | /* Use specified fdset ID */ | |
2460 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2461 | mon_fdset_cur = mon_fdset; | |
2462 | if (fdset_id < mon_fdset_cur->id) { | |
2463 | break; | |
2464 | } | |
2465 | } | |
2466 | } else { | |
2467 | /* Use first available fdset ID */ | |
2468 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2469 | mon_fdset_cur = mon_fdset; | |
2470 | if (fdset_id_prev == mon_fdset_cur->id - 1) { | |
2471 | fdset_id_prev = mon_fdset_cur->id; | |
2472 | continue; | |
2473 | } | |
2474 | break; | |
2475 | } | |
2476 | } | |
2477 | ||
2478 | mon_fdset = g_malloc0(sizeof(*mon_fdset)); | |
2479 | if (has_fdset_id) { | |
2480 | mon_fdset->id = fdset_id; | |
2481 | } else { | |
2482 | mon_fdset->id = fdset_id_prev + 1; | |
2483 | } | |
2484 | ||
2485 | /* The fdset list is ordered by fdset ID */ | |
2486 | if (!mon_fdset_cur) { | |
2487 | QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next); | |
2488 | } else if (mon_fdset->id < mon_fdset_cur->id) { | |
2489 | QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next); | |
2490 | } else { | |
2491 | QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next); | |
2492 | } | |
2493 | } | |
2494 | ||
2495 | mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd)); | |
2496 | mon_fdset_fd->fd = fd; | |
2497 | mon_fdset_fd->removed = false; | |
2498 | if (has_opaque) { | |
2499 | mon_fdset_fd->opaque = g_strdup(opaque); | |
2500 | } | |
2501 | QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next); | |
2502 | ||
2503 | fdinfo = g_malloc0(sizeof(*fdinfo)); | |
2504 | fdinfo->fdset_id = mon_fdset->id; | |
2505 | fdinfo->fd = mon_fdset_fd->fd; | |
2506 | ||
47451466 | 2507 | qemu_mutex_unlock(&mon_fdsets_lock); |
e446f70d CB |
2508 | return fdinfo; |
2509 | } | |
2510 | ||
adb696f3 CB |
2511 | int monitor_fdset_get_fd(int64_t fdset_id, int flags) |
2512 | { | |
47451466 PX |
2513 | #ifdef _WIN32 |
2514 | return -ENOENT; | |
2515 | #else | |
adb696f3 CB |
2516 | MonFdset *mon_fdset; |
2517 | MonFdsetFd *mon_fdset_fd; | |
2518 | int mon_fd_flags; | |
47451466 | 2519 | int ret; |
adb696f3 | 2520 | |
47451466 | 2521 | qemu_mutex_lock(&mon_fdsets_lock); |
adb696f3 CB |
2522 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
2523 | if (mon_fdset->id != fdset_id) { | |
2524 | continue; | |
2525 | } | |
2526 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2527 | mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL); | |
2528 | if (mon_fd_flags == -1) { | |
47451466 PX |
2529 | ret = -errno; |
2530 | goto out; | |
adb696f3 CB |
2531 | } |
2532 | ||
2533 | if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) { | |
47451466 PX |
2534 | ret = mon_fdset_fd->fd; |
2535 | goto out; | |
adb696f3 CB |
2536 | } |
2537 | } | |
47451466 PX |
2538 | ret = -EACCES; |
2539 | goto out; | |
adb696f3 | 2540 | } |
47451466 | 2541 | ret = -ENOENT; |
adb696f3 | 2542 | |
47451466 PX |
2543 | out: |
2544 | qemu_mutex_unlock(&mon_fdsets_lock); | |
2545 | return ret; | |
2546 | #endif | |
adb696f3 CB |
2547 | } |
2548 | ||
2549 | int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd) | |
2550 | { | |
2551 | MonFdset *mon_fdset; | |
2552 | MonFdsetFd *mon_fdset_fd_dup; | |
2553 | ||
47451466 | 2554 | qemu_mutex_lock(&mon_fdsets_lock); |
adb696f3 CB |
2555 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
2556 | if (mon_fdset->id != fdset_id) { | |
2557 | continue; | |
2558 | } | |
2559 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { | |
2560 | if (mon_fdset_fd_dup->fd == dup_fd) { | |
47451466 | 2561 | goto err; |
adb696f3 CB |
2562 | } |
2563 | } | |
2564 | mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup)); | |
2565 | mon_fdset_fd_dup->fd = dup_fd; | |
2566 | QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next); | |
47451466 | 2567 | qemu_mutex_unlock(&mon_fdsets_lock); |
adb696f3 CB |
2568 | return 0; |
2569 | } | |
47451466 PX |
2570 | |
2571 | err: | |
2572 | qemu_mutex_unlock(&mon_fdsets_lock); | |
adb696f3 CB |
2573 | return -1; |
2574 | } | |
2575 | ||
2576 | static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) | |
2577 | { | |
2578 | MonFdset *mon_fdset; | |
2579 | MonFdsetFd *mon_fdset_fd_dup; | |
2580 | ||
47451466 | 2581 | qemu_mutex_lock(&mon_fdsets_lock); |
adb696f3 CB |
2582 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
2583 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { | |
2584 | if (mon_fdset_fd_dup->fd == dup_fd) { | |
2585 | if (remove) { | |
2586 | QLIST_REMOVE(mon_fdset_fd_dup, next); | |
2587 | if (QLIST_EMPTY(&mon_fdset->dup_fds)) { | |
2588 | monitor_fdset_cleanup(mon_fdset); | |
2589 | } | |
47451466 | 2590 | goto err; |
b3dd1b8c | 2591 | } else { |
47451466 | 2592 | qemu_mutex_unlock(&mon_fdsets_lock); |
b3dd1b8c | 2593 | return mon_fdset->id; |
adb696f3 | 2594 | } |
adb696f3 CB |
2595 | } |
2596 | } | |
2597 | } | |
47451466 PX |
2598 | |
2599 | err: | |
2600 | qemu_mutex_unlock(&mon_fdsets_lock); | |
adb696f3 CB |
2601 | return -1; |
2602 | } | |
2603 | ||
2604 | int monitor_fdset_dup_fd_find(int dup_fd) | |
2605 | { | |
2606 | return monitor_fdset_dup_fd_find_remove(dup_fd, false); | |
2607 | } | |
2608 | ||
b3dd1b8c | 2609 | void monitor_fdset_dup_fd_remove(int dup_fd) |
adb696f3 | 2610 | { |
b3dd1b8c | 2611 | monitor_fdset_dup_fd_find_remove(dup_fd, true); |
adb696f3 CB |
2612 | } |
2613 | ||
1677f4c6 | 2614 | int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp) |
5906366e LE |
2615 | { |
2616 | int fd; | |
2617 | Error *local_err = NULL; | |
a96ed02f | 2618 | |
5906366e | 2619 | if (!qemu_isdigit(fdname[0]) && mon) { |
a9940fc4 | 2620 | fd = monitor_get_fd(mon, fdname, &local_err); |
5906366e LE |
2621 | } else { |
2622 | fd = qemu_parse_fd(fdname); | |
a96ed02f | 2623 | if (fd == -1) { |
5906366e LE |
2624 | error_setg(&local_err, "Invalid file descriptor number '%s'", |
2625 | fdname); | |
a96ed02f | 2626 | } |
5906366e LE |
2627 | } |
2628 | if (local_err) { | |
2629 | error_propagate(errp, local_err); | |
2630 | assert(fd == -1); | |
a96ed02f | 2631 | } else { |
5906366e | 2632 | assert(fd != -1); |
a96ed02f NB |
2633 | } |
2634 | ||
2635 | return fd; | |
2636 | } | |
2637 | ||
acd0a093 | 2638 | /* Please update hmp-commands.hx when adding or changing commands */ |
816f8925 | 2639 | static mon_cmd_t info_cmds[] = { |
da76ee76 PB |
2640 | #include "hmp-commands-info.h" |
2641 | { NULL, NULL, }, | |
9dc39cba FB |
2642 | }; |
2643 | ||
a13ced59 WX |
2644 | /* mon_cmds and info_cmds would be sorted at runtime */ |
2645 | static mon_cmd_t mon_cmds[] = { | |
2646 | #include "hmp-commands.h" | |
2647 | { NULL, NULL, }, | |
2648 | }; | |
2649 | ||
9307c4c1 FB |
2650 | /*******************************************************************/ |
2651 | ||
2652 | static const char *pch; | |
6ab7e546 | 2653 | static sigjmp_buf expr_env; |
9307c4c1 | 2654 | |
9307c4c1 | 2655 | |
9c3175cc SW |
2656 | static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN |
2657 | expr_error(Monitor *mon, const char *fmt, ...) | |
9dc39cba | 2658 | { |
277acfe8 FZ |
2659 | va_list ap; |
2660 | va_start(ap, fmt); | |
2661 | monitor_vprintf(mon, fmt, ap); | |
2662 | monitor_printf(mon, "\n"); | |
2663 | va_end(ap); | |
6ab7e546 | 2664 | siglongjmp(expr_env, 1); |
9307c4c1 FB |
2665 | } |
2666 | ||
09b9418c | 2667 | /* return 0 if OK, -1 if not found */ |
92a31b1f | 2668 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 2669 | { |
bf957284 | 2670 | const MonitorDef *md = target_monitor_defs(); |
854e67fe | 2671 | CPUState *cs = mon_get_cpu(); |
92a31b1f | 2672 | void *ptr; |
0a9516c2 AK |
2673 | uint64_t tmp = 0; |
2674 | int ret; | |
92a31b1f | 2675 | |
854e67fe | 2676 | if (cs == NULL || md == NULL) { |
bf957284 PB |
2677 | return -1; |
2678 | } | |
2679 | ||
2680 | for(; md->name != NULL; md++) { | |
9307c4c1 FB |
2681 | if (compare_cmd(name, md->name)) { |
2682 | if (md->get_value) { | |
e95c8d51 | 2683 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 2684 | } else { |
5bcda5f7 | 2685 | CPUArchState *env = mon_get_cpu_env(); |
6a00d601 | 2686 | ptr = (uint8_t *)env + md->offset; |
92a31b1f FB |
2687 | switch(md->type) { |
2688 | case MD_I32: | |
2689 | *pval = *(int32_t *)ptr; | |
2690 | break; | |
2691 | case MD_TLONG: | |
2692 | *pval = *(target_long *)ptr; | |
2693 | break; | |
2694 | default: | |
2695 | *pval = 0; | |
2696 | break; | |
2697 | } | |
9307c4c1 FB |
2698 | } |
2699 | return 0; | |
2700 | } | |
2701 | } | |
0a9516c2 | 2702 | |
854e67fe | 2703 | ret = target_get_monitor_def(cs, name, &tmp); |
0a9516c2 AK |
2704 | if (!ret) { |
2705 | *pval = (target_long) tmp; | |
2706 | } | |
2707 | ||
2708 | return ret; | |
9307c4c1 FB |
2709 | } |
2710 | ||
2711 | static void next(void) | |
2712 | { | |
660f11be | 2713 | if (*pch != '\0') { |
9307c4c1 | 2714 | pch++; |
cd390083 | 2715 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2716 | pch++; |
2717 | } | |
2718 | } | |
2719 | ||
376253ec | 2720 | static int64_t expr_sum(Monitor *mon); |
9307c4c1 | 2721 | |
376253ec | 2722 | static int64_t expr_unary(Monitor *mon) |
9307c4c1 | 2723 | { |
c2efc95d | 2724 | int64_t n; |
9307c4c1 | 2725 | char *p; |
6a00d601 | 2726 | int ret; |
9307c4c1 FB |
2727 | |
2728 | switch(*pch) { | |
2729 | case '+': | |
2730 | next(); | |
376253ec | 2731 | n = expr_unary(mon); |
9307c4c1 FB |
2732 | break; |
2733 | case '-': | |
2734 | next(); | |
376253ec | 2735 | n = -expr_unary(mon); |
9307c4c1 FB |
2736 | break; |
2737 | case '~': | |
2738 | next(); | |
376253ec | 2739 | n = ~expr_unary(mon); |
9307c4c1 FB |
2740 | break; |
2741 | case '(': | |
2742 | next(); | |
376253ec | 2743 | n = expr_sum(mon); |
9307c4c1 | 2744 | if (*pch != ')') { |
376253ec | 2745 | expr_error(mon, "')' expected"); |
9307c4c1 FB |
2746 | } |
2747 | next(); | |
2748 | break; | |
81d0912d FB |
2749 | case '\'': |
2750 | pch++; | |
2751 | if (*pch == '\0') | |
376253ec | 2752 | expr_error(mon, "character constant expected"); |
81d0912d FB |
2753 | n = *pch; |
2754 | pch++; | |
2755 | if (*pch != '\'') | |
376253ec | 2756 | expr_error(mon, "missing terminating \' character"); |
81d0912d FB |
2757 | next(); |
2758 | break; | |
9307c4c1 FB |
2759 | case '$': |
2760 | { | |
2761 | char buf[128], *q; | |
69b34976 | 2762 | target_long reg=0; |
3b46e624 | 2763 | |
9307c4c1 FB |
2764 | pch++; |
2765 | q = buf; | |
2766 | while ((*pch >= 'a' && *pch <= 'z') || | |
2767 | (*pch >= 'A' && *pch <= 'Z') || | |
2768 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 2769 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
2770 | if ((q - buf) < sizeof(buf) - 1) |
2771 | *q++ = *pch; | |
2772 | pch++; | |
2773 | } | |
cd390083 | 2774 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2775 | pch++; |
2776 | *q = 0; | |
7743e588 | 2777 | ret = get_monitor_def(®, buf); |
09b9418c | 2778 | if (ret < 0) |
376253ec | 2779 | expr_error(mon, "unknown register"); |
7743e588 | 2780 | n = reg; |
9307c4c1 FB |
2781 | } |
2782 | break; | |
2783 | case '\0': | |
376253ec | 2784 | expr_error(mon, "unexpected end of expression"); |
9307c4c1 FB |
2785 | n = 0; |
2786 | break; | |
2787 | default: | |
6b0e33be | 2788 | errno = 0; |
4f4fbf77 | 2789 | n = strtoull(pch, &p, 0); |
6b0e33be LC |
2790 | if (errno == ERANGE) { |
2791 | expr_error(mon, "number too large"); | |
2792 | } | |
9307c4c1 | 2793 | if (pch == p) { |
277acfe8 | 2794 | expr_error(mon, "invalid char '%c' in expression", *p); |
9307c4c1 FB |
2795 | } |
2796 | pch = p; | |
cd390083 | 2797 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
2798 | pch++; |
2799 | break; | |
2800 | } | |
2801 | return n; | |
2802 | } | |
2803 | ||
2804 | ||
376253ec | 2805 | static int64_t expr_prod(Monitor *mon) |
9307c4c1 | 2806 | { |
c2efc95d | 2807 | int64_t val, val2; |
92a31b1f | 2808 | int op; |
3b46e624 | 2809 | |
376253ec | 2810 | val = expr_unary(mon); |
9307c4c1 FB |
2811 | for(;;) { |
2812 | op = *pch; | |
2813 | if (op != '*' && op != '/' && op != '%') | |
2814 | break; | |
2815 | next(); | |
376253ec | 2816 | val2 = expr_unary(mon); |
9307c4c1 FB |
2817 | switch(op) { |
2818 | default: | |
2819 | case '*': | |
2820 | val *= val2; | |
2821 | break; | |
2822 | case '/': | |
2823 | case '%': | |
5fafdf24 | 2824 | if (val2 == 0) |
376253ec | 2825 | expr_error(mon, "division by zero"); |
9307c4c1 FB |
2826 | if (op == '/') |
2827 | val /= val2; | |
2828 | else | |
2829 | val %= val2; | |
2830 | break; | |
2831 | } | |
2832 | } | |
2833 | return val; | |
2834 | } | |
2835 | ||
376253ec | 2836 | static int64_t expr_logic(Monitor *mon) |
9307c4c1 | 2837 | { |
c2efc95d | 2838 | int64_t val, val2; |
92a31b1f | 2839 | int op; |
9307c4c1 | 2840 | |
376253ec | 2841 | val = expr_prod(mon); |
9307c4c1 FB |
2842 | for(;;) { |
2843 | op = *pch; | |
2844 | if (op != '&' && op != '|' && op != '^') | |
2845 | break; | |
2846 | next(); | |
376253ec | 2847 | val2 = expr_prod(mon); |
9307c4c1 FB |
2848 | switch(op) { |
2849 | default: | |
2850 | case '&': | |
2851 | val &= val2; | |
2852 | break; | |
2853 | case '|': | |
2854 | val |= val2; | |
2855 | break; | |
2856 | case '^': | |
2857 | val ^= val2; | |
2858 | break; | |
2859 | } | |
2860 | } | |
2861 | return val; | |
2862 | } | |
2863 | ||
376253ec | 2864 | static int64_t expr_sum(Monitor *mon) |
9307c4c1 | 2865 | { |
c2efc95d | 2866 | int64_t val, val2; |
92a31b1f | 2867 | int op; |
9307c4c1 | 2868 | |
376253ec | 2869 | val = expr_logic(mon); |
9307c4c1 FB |
2870 | for(;;) { |
2871 | op = *pch; | |
2872 | if (op != '+' && op != '-') | |
2873 | break; | |
2874 | next(); | |
376253ec | 2875 | val2 = expr_logic(mon); |
9307c4c1 FB |
2876 | if (op == '+') |
2877 | val += val2; | |
2878 | else | |
2879 | val -= val2; | |
2880 | } | |
2881 | return val; | |
2882 | } | |
2883 | ||
376253ec | 2884 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
9307c4c1 FB |
2885 | { |
2886 | pch = *pp; | |
6ab7e546 | 2887 | if (sigsetjmp(expr_env, 0)) { |
9307c4c1 FB |
2888 | *pp = pch; |
2889 | return -1; | |
2890 | } | |
cd390083 | 2891 | while (qemu_isspace(*pch)) |
9307c4c1 | 2892 | pch++; |
376253ec | 2893 | *pval = expr_sum(mon); |
9307c4c1 FB |
2894 | *pp = pch; |
2895 | return 0; | |
2896 | } | |
2897 | ||
3350a4dd MA |
2898 | static int get_double(Monitor *mon, double *pval, const char **pp) |
2899 | { | |
2900 | const char *p = *pp; | |
2901 | char *tailp; | |
2902 | double d; | |
2903 | ||
2904 | d = strtod(p, &tailp); | |
2905 | if (tailp == p) { | |
2906 | monitor_printf(mon, "Number expected\n"); | |
2907 | return -1; | |
2908 | } | |
2909 | if (d != d || d - d != 0) { | |
2910 | /* NaN or infinity */ | |
2911 | monitor_printf(mon, "Bad number\n"); | |
2912 | return -1; | |
2913 | } | |
2914 | *pval = d; | |
2915 | *pp = tailp; | |
2916 | return 0; | |
2917 | } | |
2918 | ||
4590fd80 LC |
2919 | /* |
2920 | * Store the command-name in cmdname, and return a pointer to | |
2921 | * the remaining of the command string. | |
2922 | */ | |
2923 | static const char *get_command_name(const char *cmdline, | |
2924 | char *cmdname, size_t nlen) | |
2925 | { | |
2926 | size_t len; | |
2927 | const char *p, *pstart; | |
2928 | ||
2929 | p = cmdline; | |
2930 | while (qemu_isspace(*p)) | |
2931 | p++; | |
2932 | if (*p == '\0') | |
2933 | return NULL; | |
2934 | pstart = p; | |
2935 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | |
2936 | p++; | |
2937 | len = p - pstart; | |
2938 | if (len > nlen - 1) | |
2939 | len = nlen - 1; | |
2940 | memcpy(cmdname, pstart, len); | |
2941 | cmdname[len] = '\0'; | |
2942 | return p; | |
2943 | } | |
2944 | ||
4d76d2ba LC |
2945 | /** |
2946 | * Read key of 'type' into 'key' and return the current | |
2947 | * 'type' pointer. | |
2948 | */ | |
2949 | static char *key_get_info(const char *type, char **key) | |
2950 | { | |
2951 | size_t len; | |
2952 | char *p, *str; | |
2953 | ||
2954 | if (*type == ',') | |
2955 | type++; | |
2956 | ||
2957 | p = strchr(type, ':'); | |
2958 | if (!p) { | |
2959 | *key = NULL; | |
2960 | return NULL; | |
2961 | } | |
2962 | len = p - type; | |
2963 | ||
7267c094 | 2964 | str = g_malloc(len + 1); |
4d76d2ba LC |
2965 | memcpy(str, type, len); |
2966 | str[len] = '\0'; | |
2967 | ||
2968 | *key = str; | |
2969 | return ++p; | |
2970 | } | |
2971 | ||
9307c4c1 FB |
2972 | static int default_fmt_format = 'x'; |
2973 | static int default_fmt_size = 4; | |
2974 | ||
fbc3d96c LS |
2975 | static int is_valid_option(const char *c, const char *typestr) |
2976 | { | |
2977 | char option[3]; | |
2978 | ||
2979 | option[0] = '-'; | |
2980 | option[1] = *c; | |
2981 | option[2] = '\0'; | |
2982 | ||
2983 | typestr = strstr(typestr, option); | |
2984 | return (typestr != NULL); | |
2985 | } | |
2986 | ||
945c5ac8 LC |
2987 | static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table, |
2988 | const char *cmdname) | |
7fd669a1 LC |
2989 | { |
2990 | const mon_cmd_t *cmd; | |
2991 | ||
945c5ac8 | 2992 | for (cmd = disp_table; cmd->name != NULL; cmd++) { |
7fd669a1 LC |
2993 | if (compare_cmd(cmdname, cmd->name)) { |
2994 | return cmd; | |
2995 | } | |
2996 | } | |
2997 | ||
2998 | return NULL; | |
2999 | } | |
3000 | ||
5f3d335f | 3001 | /* |
ae50212f BD |
3002 | * Parse command name from @cmdp according to command table @table. |
3003 | * If blank, return NULL. | |
3004 | * Else, if no valid command can be found, report to @mon, and return | |
3005 | * NULL. | |
3006 | * Else, change @cmdp to point right behind the name, and return its | |
3007 | * command table entry. | |
3008 | * Do not assume the return value points into @table! It doesn't when | |
3009 | * the command is found in a sub-command table. | |
5f3d335f | 3010 | */ |
c227f099 | 3011 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
250b8197 | 3012 | const char *cmdp_start, |
ae50212f BD |
3013 | const char **cmdp, |
3014 | mon_cmd_t *table) | |
9307c4c1 | 3015 | { |
ae50212f | 3016 | const char *p; |
c227f099 | 3017 | const mon_cmd_t *cmd; |
9307c4c1 | 3018 | char cmdname[256]; |
9dc39cba | 3019 | |
9307c4c1 | 3020 | /* extract the command name */ |
ae50212f | 3021 | p = get_command_name(*cmdp, cmdname, sizeof(cmdname)); |
4590fd80 | 3022 | if (!p) |
55f81d96 | 3023 | return NULL; |
3b46e624 | 3024 | |
5f3d335f | 3025 | cmd = search_dispatch_table(table, cmdname); |
7fd669a1 | 3026 | if (!cmd) { |
5f3d335f | 3027 | monitor_printf(mon, "unknown command: '%.*s'\n", |
250b8197 | 3028 | (int)(p - cmdp_start), cmdp_start); |
55f81d96 | 3029 | return NULL; |
9307c4c1 | 3030 | } |
c3120f71 DDAG |
3031 | if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) { |
3032 | monitor_printf(mon, "Command '%.*s' not available with -preconfig " | |
3033 | "until after exit_preconfig.\n", | |
3034 | (int)(p - cmdp_start), cmdp_start); | |
3035 | return NULL; | |
3036 | } | |
9307c4c1 | 3037 | |
5f3d335f WX |
3038 | /* filter out following useless space */ |
3039 | while (qemu_isspace(*p)) { | |
3040 | p++; | |
3041 | } | |
ae50212f BD |
3042 | |
3043 | *cmdp = p; | |
5f3d335f | 3044 | /* search sub command */ |
ae50212f | 3045 | if (cmd->sub_table != NULL && *p != '\0') { |
250b8197 | 3046 | return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table); |
5f3d335f WX |
3047 | } |
3048 | ||
ae50212f BD |
3049 | return cmd; |
3050 | } | |
3051 | ||
3052 | /* | |
3053 | * Parse arguments for @cmd. | |
3054 | * If it can't be parsed, report to @mon, and return NULL. | |
3055 | * Else, insert command arguments into a QDict, and return it. | |
3056 | * Note: On success, caller has to free the QDict structure. | |
3057 | */ | |
3058 | ||
3059 | static QDict *monitor_parse_arguments(Monitor *mon, | |
3060 | const char **endp, | |
3061 | const mon_cmd_t *cmd) | |
3062 | { | |
3063 | const char *typestr; | |
3064 | char *key; | |
3065 | int c; | |
3066 | const char *p = *endp; | |
3067 | char buf[1024]; | |
3068 | QDict *qdict = qdict_new(); | |
3069 | ||
9307c4c1 FB |
3070 | /* parse the parameters */ |
3071 | typestr = cmd->args_type; | |
9dc39cba | 3072 | for(;;) { |
4d76d2ba LC |
3073 | typestr = key_get_info(typestr, &key); |
3074 | if (!typestr) | |
9dc39cba | 3075 | break; |
4d76d2ba | 3076 | c = *typestr; |
9307c4c1 FB |
3077 | typestr++; |
3078 | switch(c) { | |
3079 | case 'F': | |
81d0912d | 3080 | case 'B': |
9307c4c1 FB |
3081 | case 's': |
3082 | { | |
3083 | int ret; | |
3b46e624 | 3084 | |
cd390083 | 3085 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3086 | p++; |
3087 | if (*typestr == '?') { | |
3088 | typestr++; | |
3089 | if (*p == '\0') { | |
3090 | /* no optional string: NULL argument */ | |
53773581 | 3091 | break; |
9307c4c1 FB |
3092 | } |
3093 | } | |
3094 | ret = get_str(buf, sizeof(buf), &p); | |
3095 | if (ret < 0) { | |
81d0912d FB |
3096 | switch(c) { |
3097 | case 'F': | |
376253ec | 3098 | monitor_printf(mon, "%s: filename expected\n", |
ae50212f | 3099 | cmd->name); |
81d0912d FB |
3100 | break; |
3101 | case 'B': | |
376253ec | 3102 | monitor_printf(mon, "%s: block device name expected\n", |
ae50212f | 3103 | cmd->name); |
81d0912d FB |
3104 | break; |
3105 | default: | |
ae50212f | 3106 | monitor_printf(mon, "%s: string expected\n", cmd->name); |
81d0912d FB |
3107 | break; |
3108 | } | |
9307c4c1 FB |
3109 | goto fail; |
3110 | } | |
46f5ac20 | 3111 | qdict_put_str(qdict, key, buf); |
9307c4c1 | 3112 | } |
9dc39cba | 3113 | break; |
361127df MA |
3114 | case 'O': |
3115 | { | |
3116 | QemuOptsList *opts_list; | |
3117 | QemuOpts *opts; | |
3118 | ||
3119 | opts_list = qemu_find_opts(key); | |
3120 | if (!opts_list || opts_list->desc->name) { | |
3121 | goto bad_type; | |
3122 | } | |
3123 | while (qemu_isspace(*p)) { | |
3124 | p++; | |
3125 | } | |
3126 | if (!*p) | |
3127 | break; | |
3128 | if (get_str(buf, sizeof(buf), &p) < 0) { | |
3129 | goto fail; | |
3130 | } | |
70b94331 | 3131 | opts = qemu_opts_parse_noisily(opts_list, buf, true); |
361127df MA |
3132 | if (!opts) { |
3133 | goto fail; | |
3134 | } | |
3135 | qemu_opts_to_qdict(opts, qdict); | |
3136 | qemu_opts_del(opts); | |
3137 | } | |
3138 | break; | |
9307c4c1 FB |
3139 | case '/': |
3140 | { | |
3141 | int count, format, size; | |
3b46e624 | 3142 | |
cd390083 | 3143 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3144 | p++; |
3145 | if (*p == '/') { | |
3146 | /* format found */ | |
3147 | p++; | |
3148 | count = 1; | |
cd390083 | 3149 | if (qemu_isdigit(*p)) { |
9307c4c1 | 3150 | count = 0; |
cd390083 | 3151 | while (qemu_isdigit(*p)) { |
9307c4c1 FB |
3152 | count = count * 10 + (*p - '0'); |
3153 | p++; | |
3154 | } | |
3155 | } | |
3156 | size = -1; | |
3157 | format = -1; | |
3158 | for(;;) { | |
3159 | switch(*p) { | |
3160 | case 'o': | |
3161 | case 'd': | |
3162 | case 'u': | |
3163 | case 'x': | |
3164 | case 'i': | |
3165 | case 'c': | |
3166 | format = *p++; | |
3167 | break; | |
3168 | case 'b': | |
3169 | size = 1; | |
3170 | p++; | |
3171 | break; | |
3172 | case 'h': | |
3173 | size = 2; | |
3174 | p++; | |
3175 | break; | |
3176 | case 'w': | |
3177 | size = 4; | |
3178 | p++; | |
3179 | break; | |
3180 | case 'g': | |
3181 | case 'L': | |
3182 | size = 8; | |
3183 | p++; | |
3184 | break; | |
3185 | default: | |
3186 | goto next; | |
3187 | } | |
3188 | } | |
3189 | next: | |
cd390083 | 3190 | if (*p != '\0' && !qemu_isspace(*p)) { |
376253ec AL |
3191 | monitor_printf(mon, "invalid char in format: '%c'\n", |
3192 | *p); | |
9307c4c1 FB |
3193 | goto fail; |
3194 | } | |
9307c4c1 FB |
3195 | if (format < 0) |
3196 | format = default_fmt_format; | |
4c27ba27 FB |
3197 | if (format != 'i') { |
3198 | /* for 'i', not specifying a size gives -1 as size */ | |
3199 | if (size < 0) | |
3200 | size = default_fmt_size; | |
e90f009b | 3201 | default_fmt_size = size; |
4c27ba27 | 3202 | } |
9307c4c1 FB |
3203 | default_fmt_format = format; |
3204 | } else { | |
3205 | count = 1; | |
3206 | format = default_fmt_format; | |
4c27ba27 FB |
3207 | if (format != 'i') { |
3208 | size = default_fmt_size; | |
3209 | } else { | |
3210 | size = -1; | |
3211 | } | |
9307c4c1 | 3212 | } |
46f5ac20 EB |
3213 | qdict_put_int(qdict, "count", count); |
3214 | qdict_put_int(qdict, "format", format); | |
3215 | qdict_put_int(qdict, "size", size); | |
9307c4c1 | 3216 | } |
9dc39cba | 3217 | break; |
9307c4c1 | 3218 | case 'i': |
92a31b1f | 3219 | case 'l': |
b6e098d7 | 3220 | case 'M': |
9307c4c1 | 3221 | { |
c2efc95d | 3222 | int64_t val; |
7743e588 | 3223 | |
cd390083 | 3224 | while (qemu_isspace(*p)) |
9307c4c1 | 3225 | p++; |
3440557b | 3226 | if (*typestr == '?' || *typestr == '.') { |
3440557b | 3227 | if (*typestr == '?') { |
53773581 LC |
3228 | if (*p == '\0') { |
3229 | typestr++; | |
3230 | break; | |
3231 | } | |
3440557b FB |
3232 | } else { |
3233 | if (*p == '.') { | |
3234 | p++; | |
cd390083 | 3235 | while (qemu_isspace(*p)) |
3440557b | 3236 | p++; |
3440557b | 3237 | } else { |
53773581 LC |
3238 | typestr++; |
3239 | break; | |
3440557b FB |
3240 | } |
3241 | } | |
13224a87 | 3242 | typestr++; |
9307c4c1 | 3243 | } |
376253ec | 3244 | if (get_expr(mon, &val, &p)) |
9307c4c1 | 3245 | goto fail; |
675ebef9 LC |
3246 | /* Check if 'i' is greater than 32-bit */ |
3247 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { | |
ae50212f | 3248 | monitor_printf(mon, "\'%s\' has failed: ", cmd->name); |
675ebef9 LC |
3249 | monitor_printf(mon, "integer is for 32-bit values\n"); |
3250 | goto fail; | |
b6e098d7 | 3251 | } else if (c == 'M') { |
91162849 LC |
3252 | if (val < 0) { |
3253 | monitor_printf(mon, "enter a positive value\n"); | |
3254 | goto fail; | |
3255 | } | |
8ec338ac | 3256 | val *= MiB; |
675ebef9 | 3257 | } |
46f5ac20 | 3258 | qdict_put_int(qdict, key, val); |
9307c4c1 FB |
3259 | } |
3260 | break; | |
dbc0c67f JS |
3261 | case 'o': |
3262 | { | |
f17fd4fd | 3263 | int ret; |
f46bfdbf | 3264 | uint64_t val; |
dbc0c67f JS |
3265 | char *end; |
3266 | ||
3267 | while (qemu_isspace(*p)) { | |
3268 | p++; | |
3269 | } | |
3270 | if (*typestr == '?') { | |
3271 | typestr++; | |
3272 | if (*p == '\0') { | |
3273 | break; | |
3274 | } | |
3275 | } | |
f17fd4fd | 3276 | ret = qemu_strtosz_MiB(p, &end, &val); |
f46bfdbf | 3277 | if (ret < 0 || val > INT64_MAX) { |
dbc0c67f JS |
3278 | monitor_printf(mon, "invalid size\n"); |
3279 | goto fail; | |
3280 | } | |
46f5ac20 | 3281 | qdict_put_int(qdict, key, val); |
dbc0c67f JS |
3282 | p = end; |
3283 | } | |
3284 | break; | |
fccfb11e | 3285 | case 'T': |
3350a4dd MA |
3286 | { |
3287 | double val; | |
3288 | ||
3289 | while (qemu_isspace(*p)) | |
3290 | p++; | |
3291 | if (*typestr == '?') { | |
3292 | typestr++; | |
3293 | if (*p == '\0') { | |
3294 | break; | |
3295 | } | |
3296 | } | |
3297 | if (get_double(mon, &val, &p) < 0) { | |
3298 | goto fail; | |
3299 | } | |
07de3e60 | 3300 | if (p[0] && p[1] == 's') { |
fccfb11e MA |
3301 | switch (*p) { |
3302 | case 'm': | |
3303 | val /= 1e3; p += 2; break; | |
3304 | case 'u': | |
3305 | val /= 1e6; p += 2; break; | |
3306 | case 'n': | |
3307 | val /= 1e9; p += 2; break; | |
3308 | } | |
3309 | } | |
3350a4dd MA |
3310 | if (*p && !qemu_isspace(*p)) { |
3311 | monitor_printf(mon, "Unknown unit suffix\n"); | |
3312 | goto fail; | |
3313 | } | |
01b2ffce | 3314 | qdict_put(qdict, key, qnum_from_double(val)); |
3350a4dd MA |
3315 | } |
3316 | break; | |
942cd1f2 MA |
3317 | case 'b': |
3318 | { | |
3319 | const char *beg; | |
fc48ffc3 | 3320 | bool val; |
942cd1f2 MA |
3321 | |
3322 | while (qemu_isspace(*p)) { | |
3323 | p++; | |
3324 | } | |
3325 | beg = p; | |
3326 | while (qemu_isgraph(*p)) { | |
3327 | p++; | |
3328 | } | |
3329 | if (p - beg == 2 && !memcmp(beg, "on", p - beg)) { | |
fc48ffc3 | 3330 | val = true; |
942cd1f2 | 3331 | } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) { |
fc48ffc3 | 3332 | val = false; |
942cd1f2 MA |
3333 | } else { |
3334 | monitor_printf(mon, "Expected 'on' or 'off'\n"); | |
3335 | goto fail; | |
3336 | } | |
46f5ac20 | 3337 | qdict_put_bool(qdict, key, val); |
942cd1f2 MA |
3338 | } |
3339 | break; | |
9307c4c1 FB |
3340 | case '-': |
3341 | { | |
fbc3d96c | 3342 | const char *tmp = p; |
eb159d13 | 3343 | int skip_key = 0; |
9307c4c1 | 3344 | /* option */ |
3b46e624 | 3345 | |
9307c4c1 FB |
3346 | c = *typestr++; |
3347 | if (c == '\0') | |
3348 | goto bad_type; | |
cd390083 | 3349 | while (qemu_isspace(*p)) |
9307c4c1 | 3350 | p++; |
9307c4c1 FB |
3351 | if (*p == '-') { |
3352 | p++; | |
fbc3d96c LS |
3353 | if(c != *p) { |
3354 | if(!is_valid_option(p, typestr)) { | |
3355 | ||
3356 | monitor_printf(mon, "%s: unsupported option -%c\n", | |
ae50212f | 3357 | cmd->name, *p); |
fbc3d96c LS |
3358 | goto fail; |
3359 | } else { | |
3360 | skip_key = 1; | |
3361 | } | |
3362 | } | |
3363 | if(skip_key) { | |
3364 | p = tmp; | |
3365 | } else { | |
eb159d13 | 3366 | /* has option */ |
fbc3d96c | 3367 | p++; |
46f5ac20 | 3368 | qdict_put_bool(qdict, key, true); |
9307c4c1 | 3369 | } |
9307c4c1 | 3370 | } |
9307c4c1 FB |
3371 | } |
3372 | break; | |
129be006 WX |
3373 | case 'S': |
3374 | { | |
3375 | /* package all remaining string */ | |
3376 | int len; | |
3377 | ||
3378 | while (qemu_isspace(*p)) { | |
3379 | p++; | |
3380 | } | |
3381 | if (*typestr == '?') { | |
3382 | typestr++; | |
3383 | if (*p == '\0') { | |
3384 | /* no remaining string: NULL argument */ | |
3385 | break; | |
3386 | } | |
3387 | } | |
3388 | len = strlen(p); | |
3389 | if (len <= 0) { | |
3390 | monitor_printf(mon, "%s: string expected\n", | |
ae50212f | 3391 | cmd->name); |
e549d2aa | 3392 | goto fail; |
129be006 | 3393 | } |
46f5ac20 | 3394 | qdict_put_str(qdict, key, p); |
129be006 WX |
3395 | p += len; |
3396 | } | |
3397 | break; | |
9307c4c1 FB |
3398 | default: |
3399 | bad_type: | |
ae50212f | 3400 | monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c); |
9307c4c1 FB |
3401 | goto fail; |
3402 | } | |
7267c094 | 3403 | g_free(key); |
4d76d2ba | 3404 | key = NULL; |
9dc39cba | 3405 | } |
9307c4c1 | 3406 | /* check that all arguments were parsed */ |
cd390083 | 3407 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3408 | p++; |
3409 | if (*p != '\0') { | |
376253ec | 3410 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
ae50212f | 3411 | cmd->name); |
9307c4c1 | 3412 | goto fail; |
9dc39cba | 3413 | } |
9307c4c1 | 3414 | |
ae50212f | 3415 | return qdict; |
ac7531ec | 3416 | |
55f81d96 | 3417 | fail: |
cb3e7f08 | 3418 | qobject_unref(qdict); |
7267c094 | 3419 | g_free(key); |
55f81d96 LC |
3420 | return NULL; |
3421 | } | |
3422 | ||
7ef6cf63 | 3423 | static void handle_hmp_command(Monitor *mon, const char *cmdline) |
55f81d96 | 3424 | { |
55f81d96 | 3425 | QDict *qdict; |
c227f099 | 3426 | const mon_cmd_t *cmd; |
317c52cc | 3427 | const char *cmd_start = cmdline; |
55f81d96 | 3428 | |
79cad8b4 SH |
3429 | trace_handle_hmp_command(mon, cmdline); |
3430 | ||
250b8197 | 3431 | cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table); |
ae50212f BD |
3432 | if (!cmd) { |
3433 | return; | |
3434 | } | |
55f81d96 | 3435 | |
ae50212f BD |
3436 | qdict = monitor_parse_arguments(mon, &cmdline, cmd); |
3437 | if (!qdict) { | |
317c52cc CW |
3438 | while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) { |
3439 | cmdline--; | |
3440 | } | |
3441 | monitor_printf(mon, "Try \"help %.*s\" for more information\n", | |
3442 | (int)(cmdline - cmd_start), cmd_start); | |
ae50212f | 3443 | return; |
55f81d96 LC |
3444 | } |
3445 | ||
2b9e3576 | 3446 | cmd->cmd(mon, qdict); |
cb3e7f08 | 3447 | qobject_unref(qdict); |
9dc39cba FB |
3448 | } |
3449 | ||
cd5c6bba | 3450 | static void cmd_completion(Monitor *mon, const char *name, const char *list) |
81d0912d FB |
3451 | { |
3452 | const char *p, *pstart; | |
3453 | char cmd[128]; | |
3454 | int len; | |
3455 | ||
3456 | p = list; | |
3457 | for(;;) { | |
3458 | pstart = p; | |
5c99fa37 | 3459 | p = qemu_strchrnul(p, '|'); |
81d0912d FB |
3460 | len = p - pstart; |
3461 | if (len > sizeof(cmd) - 2) | |
3462 | len = sizeof(cmd) - 2; | |
3463 | memcpy(cmd, pstart, len); | |
3464 | cmd[len] = '\0'; | |
3465 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
cd5c6bba | 3466 | readline_add_completion(mon->rs, cmd); |
81d0912d FB |
3467 | } |
3468 | if (*p == '\0') | |
3469 | break; | |
3470 | p++; | |
3471 | } | |
3472 | } | |
3473 | ||
cb8f68b1 | 3474 | static void file_completion(Monitor *mon, const char *input) |
81d0912d FB |
3475 | { |
3476 | DIR *ffs; | |
3477 | struct dirent *d; | |
3478 | char path[1024]; | |
3479 | char file[1024], file_prefix[1024]; | |
3480 | int input_path_len; | |
3481 | const char *p; | |
3482 | ||
5fafdf24 | 3483 | p = strrchr(input, '/'); |
81d0912d FB |
3484 | if (!p) { |
3485 | input_path_len = 0; | |
3486 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 3487 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
3488 | } else { |
3489 | input_path_len = p - input + 1; | |
3490 | memcpy(path, input, input_path_len); | |
3491 | if (input_path_len > sizeof(path) - 1) | |
3492 | input_path_len = sizeof(path) - 1; | |
3493 | path[input_path_len] = '\0'; | |
3494 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
3495 | } | |
19f2db5c | 3496 | |
81d0912d FB |
3497 | ffs = opendir(path); |
3498 | if (!ffs) | |
3499 | return; | |
3500 | for(;;) { | |
3501 | struct stat sb; | |
3502 | d = readdir(ffs); | |
3503 | if (!d) | |
3504 | break; | |
46c7fc18 KK |
3505 | |
3506 | if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) { | |
3507 | continue; | |
3508 | } | |
3509 | ||
81d0912d FB |
3510 | if (strstart(d->d_name, file_prefix, NULL)) { |
3511 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
3512 | if (input_path_len < sizeof(file)) |
3513 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
3514 | d->d_name); | |
81d0912d FB |
3515 | /* stat the file to find out if it's a directory. |
3516 | * In that case add a slash to speed up typing long paths | |
3517 | */ | |
c951d9a6 | 3518 | if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
363a37d5 | 3519 | pstrcat(file, sizeof(file), "/"); |
c951d9a6 | 3520 | } |
cb8f68b1 | 3521 | readline_add_completion(mon->rs, file); |
81d0912d FB |
3522 | } |
3523 | } | |
3524 | closedir(ffs); | |
3525 | } | |
3526 | ||
4d76d2ba LC |
3527 | static const char *next_arg_type(const char *typestr) |
3528 | { | |
3529 | const char *p = strchr(typestr, ':'); | |
3530 | return (p != NULL ? ++p : typestr); | |
3531 | } | |
3532 | ||
40d19394 HB |
3533 | static void add_completion_option(ReadLineState *rs, const char *str, |
3534 | const char *option) | |
3535 | { | |
3536 | if (!str || !option) { | |
3537 | return; | |
3538 | } | |
3539 | if (!strncmp(option, str, strlen(str))) { | |
3540 | readline_add_completion(rs, option); | |
3541 | } | |
3542 | } | |
3543 | ||
13e315da HB |
3544 | void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
3545 | { | |
3546 | size_t len; | |
3547 | ChardevBackendInfoList *list, *start; | |
3548 | ||
3549 | if (nb_args != 2) { | |
3550 | return; | |
3551 | } | |
3552 | len = strlen(str); | |
3553 | readline_set_completion_index(rs, len); | |
3554 | ||
3555 | start = list = qmp_query_chardev_backends(NULL); | |
3556 | while (list) { | |
3557 | const char *chr_name = list->value->name; | |
3558 | ||
3559 | if (!strncmp(chr_name, str, len)) { | |
3560 | readline_add_completion(rs, chr_name); | |
3561 | } | |
3562 | list = list->next; | |
3563 | } | |
3564 | qapi_free_ChardevBackendInfoList(start); | |
3565 | } | |
3566 | ||
b162b49a HB |
3567 | void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
3568 | { | |
3569 | size_t len; | |
3570 | int i; | |
3571 | ||
3572 | if (nb_args != 2) { | |
3573 | return; | |
3574 | } | |
3575 | len = strlen(str); | |
3576 | readline_set_completion_index(rs, len); | |
1c236ba5 | 3577 | for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) { |
977c736f | 3578 | add_completion_option(rs, str, NetClientDriver_str(i)); |
b162b49a HB |
3579 | } |
3580 | } | |
3581 | ||
2da1b3ab | 3582 | void device_add_completion(ReadLineState *rs, int nb_args, const char *str) |
992d3e64 HB |
3583 | { |
3584 | GSList *list, *elt; | |
3585 | size_t len; | |
3586 | ||
2da1b3ab HB |
3587 | if (nb_args != 2) { |
3588 | return; | |
3589 | } | |
3590 | ||
992d3e64 HB |
3591 | len = strlen(str); |
3592 | readline_set_completion_index(rs, len); | |
3593 | list = elt = object_class_get_list(TYPE_DEVICE, false); | |
3594 | while (elt) { | |
3595 | const char *name; | |
3596 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, | |
3597 | TYPE_DEVICE); | |
3598 | name = object_class_get_name(OBJECT_CLASS(dc)); | |
2da1b3ab | 3599 | |
e90f2a8c | 3600 | if (dc->user_creatable |
2da1b3ab | 3601 | && !strncmp(name, str, len)) { |
992d3e64 HB |
3602 | readline_add_completion(rs, name); |
3603 | } | |
3604 | elt = elt->next; | |
3605 | } | |
3606 | g_slist_free(list); | |
3607 | } | |
3608 | ||
bfa40f77 | 3609 | void object_add_completion(ReadLineState *rs, int nb_args, const char *str) |
1094fd3a HB |
3610 | { |
3611 | GSList *list, *elt; | |
3612 | size_t len; | |
3613 | ||
bfa40f77 HB |
3614 | if (nb_args != 2) { |
3615 | return; | |
3616 | } | |
3617 | ||
1094fd3a HB |
3618 | len = strlen(str); |
3619 | readline_set_completion_index(rs, len); | |
3620 | list = elt = object_class_get_list(TYPE_USER_CREATABLE, false); | |
3621 | while (elt) { | |
3622 | const char *name; | |
3623 | ||
3624 | name = object_class_get_name(OBJECT_CLASS(elt->data)); | |
3625 | if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) { | |
3626 | readline_add_completion(rs, name); | |
3627 | } | |
3628 | elt = elt->next; | |
3629 | } | |
3630 | g_slist_free(list); | |
3631 | } | |
3632 | ||
6a1fa9f5 ZG |
3633 | static void peripheral_device_del_completion(ReadLineState *rs, |
3634 | const char *str, size_t len) | |
3635 | { | |
4cae4d5a MA |
3636 | Object *peripheral = container_get(qdev_get_machine(), "/peripheral"); |
3637 | GSList *list, *item; | |
6a1fa9f5 | 3638 | |
4cae4d5a MA |
3639 | list = qdev_build_hotpluggable_device_list(peripheral); |
3640 | if (!list) { | |
6a1fa9f5 ZG |
3641 | return; |
3642 | } | |
3643 | ||
6a1fa9f5 ZG |
3644 | for (item = list; item; item = g_slist_next(item)) { |
3645 | DeviceState *dev = item->data; | |
3646 | ||
3647 | if (dev->id && !strncmp(str, dev->id, len)) { | |
3648 | readline_add_completion(rs, dev->id); | |
3649 | } | |
3650 | } | |
3651 | ||
3652 | g_slist_free(list); | |
3653 | } | |
3654 | ||
6297d9a2 HB |
3655 | void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str) |
3656 | { | |
3657 | size_t len; | |
3658 | ChardevInfoList *list, *start; | |
3659 | ||
3660 | if (nb_args != 2) { | |
3661 | return; | |
3662 | } | |
3663 | len = strlen(str); | |
3664 | readline_set_completion_index(rs, len); | |
3665 | ||
3666 | start = list = qmp_query_chardev(NULL); | |
3667 | while (list) { | |
3668 | ChardevInfo *chr = list->value; | |
3669 | ||
3670 | if (!strncmp(chr->label, str, len)) { | |
3671 | readline_add_completion(rs, chr->label); | |
3672 | } | |
3673 | list = list->next; | |
3674 | } | |
3675 | qapi_free_ChardevInfoList(start); | |
3676 | } | |
3677 | ||
8e597779 HB |
3678 | static void ringbuf_completion(ReadLineState *rs, const char *str) |
3679 | { | |
3680 | size_t len; | |
3681 | ChardevInfoList *list, *start; | |
3682 | ||
3683 | len = strlen(str); | |
3684 | readline_set_completion_index(rs, len); | |
3685 | ||
3686 | start = list = qmp_query_chardev(NULL); | |
3687 | while (list) { | |
3688 | ChardevInfo *chr_info = list->value; | |
3689 | ||
3690 | if (!strncmp(chr_info->label, str, len)) { | |
0ec7b3e7 | 3691 | Chardev *chr = qemu_chr_find(chr_info->label); |
777357d7 | 3692 | if (chr && CHARDEV_IS_RINGBUF(chr)) { |
8e597779 HB |
3693 | readline_add_completion(rs, chr_info->label); |
3694 | } | |
3695 | } | |
3696 | list = list->next; | |
3697 | } | |
3698 | qapi_free_ChardevInfoList(start); | |
3699 | } | |
3700 | ||
8e597779 HB |
3701 | void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str) |
3702 | { | |
3703 | if (nb_args != 2) { | |
3704 | return; | |
3705 | } | |
3706 | ringbuf_completion(rs, str); | |
3707 | } | |
3708 | ||
2da1b3ab HB |
3709 | void device_del_completion(ReadLineState *rs, int nb_args, const char *str) |
3710 | { | |
3711 | size_t len; | |
3712 | ||
3713 | if (nb_args != 2) { | |
3714 | return; | |
3715 | } | |
3716 | ||
3717 | len = strlen(str); | |
3718 | readline_set_completion_index(rs, len); | |
6a1fa9f5 | 3719 | peripheral_device_del_completion(rs, str, len); |
2da1b3ab HB |
3720 | } |
3721 | ||
bfa40f77 | 3722 | void object_del_completion(ReadLineState *rs, int nb_args, const char *str) |
b48fa074 HB |
3723 | { |
3724 | ObjectPropertyInfoList *list, *start; | |
3725 | size_t len; | |
3726 | ||
bfa40f77 HB |
3727 | if (nb_args != 2) { |
3728 | return; | |
3729 | } | |
b48fa074 HB |
3730 | len = strlen(str); |
3731 | readline_set_completion_index(rs, len); | |
3732 | ||
3733 | start = list = qmp_qom_list("/objects", NULL); | |
3734 | while (list) { | |
3735 | ObjectPropertyInfo *info = list->value; | |
3736 | ||
3737 | if (!strncmp(info->type, "child<", 5) | |
3738 | && !strncmp(info->name, str, len)) { | |
3739 | readline_add_completion(rs, info->name); | |
3740 | } | |
3741 | list = list->next; | |
3742 | } | |
3743 | qapi_free_ObjectPropertyInfoList(start); | |
3744 | } | |
3745 | ||
29136cd8 HB |
3746 | void sendkey_completion(ReadLineState *rs, int nb_args, const char *str) |
3747 | { | |
3748 | int i; | |
3749 | char *sep; | |
3750 | size_t len; | |
3751 | ||
3752 | if (nb_args != 2) { | |
3753 | return; | |
3754 | } | |
3755 | sep = strrchr(str, '-'); | |
3756 | if (sep) { | |
3757 | str = sep + 1; | |
3758 | } | |
3759 | len = strlen(str); | |
3760 | readline_set_completion_index(rs, len); | |
7fb1cf16 | 3761 | for (i = 0; i < Q_KEY_CODE__MAX; i++) { |
977c736f MA |
3762 | if (!strncmp(str, QKeyCode_str(i), len)) { |
3763 | readline_add_completion(rs, QKeyCode_str(i)); | |
29136cd8 HB |
3764 | } |
3765 | } | |
3766 | } | |
3767 | ||
40d19394 HB |
3768 | void set_link_completion(ReadLineState *rs, int nb_args, const char *str) |
3769 | { | |
3770 | size_t len; | |
3771 | ||
3772 | len = strlen(str); | |
3773 | readline_set_completion_index(rs, len); | |
3774 | if (nb_args == 2) { | |
eaed483c | 3775 | NetClientState *ncs[MAX_QUEUE_NUM]; |
40d19394 HB |
3776 | int count, i; |
3777 | count = qemu_find_net_clients_except(NULL, ncs, | |
f394b2e2 | 3778 | NET_CLIENT_DRIVER_NONE, |
eaed483c | 3779 | MAX_QUEUE_NUM); |
bcfa4d60 | 3780 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
40d19394 HB |
3781 | const char *name = ncs[i]->name; |
3782 | if (!strncmp(str, name, len)) { | |
3783 | readline_add_completion(rs, name); | |
3784 | } | |
3785 | } | |
3786 | } else if (nb_args == 3) { | |
3787 | add_completion_option(rs, str, "on"); | |
3788 | add_completion_option(rs, str, "off"); | |
3789 | } | |
3790 | } | |
3791 | ||
11b389f2 HB |
3792 | void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str) |
3793 | { | |
3794 | int len, count, i; | |
eaed483c | 3795 | NetClientState *ncs[MAX_QUEUE_NUM]; |
11b389f2 HB |
3796 | |
3797 | if (nb_args != 2) { | |
3798 | return; | |
3799 | } | |
3800 | ||
3801 | len = strlen(str); | |
3802 | readline_set_completion_index(rs, len); | |
f394b2e2 | 3803 | count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC, |
eaed483c | 3804 | MAX_QUEUE_NUM); |
bcfa4d60 | 3805 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
11b389f2 HB |
3806 | QemuOpts *opts; |
3807 | const char *name = ncs[i]->name; | |
3808 | if (strncmp(str, name, len)) { | |
3809 | continue; | |
3810 | } | |
3811 | opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name); | |
3812 | if (opts) { | |
3813 | readline_add_completion(rs, name); | |
3814 | } | |
3815 | } | |
3816 | } | |
3817 | ||
bd71211d LV |
3818 | void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str) |
3819 | { | |
3820 | size_t len; | |
3821 | ||
3822 | len = strlen(str); | |
3823 | readline_set_completion_index(rs, len); | |
3824 | if (nb_args == 2) { | |
0d4e995c DB |
3825 | TraceEventIter iter; |
3826 | TraceEvent *ev; | |
3827 | char *pattern = g_strdup_printf("%s*", str); | |
3828 | trace_event_iter_init(&iter, pattern); | |
3829 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
3830 | readline_add_completion(rs, trace_event_get_name(ev)); | |
bd71211d | 3831 | } |
0d4e995c | 3832 | g_free(pattern); |
bd71211d LV |
3833 | } |
3834 | } | |
3835 | ||
987bd270 DDAG |
3836 | void trace_event_completion(ReadLineState *rs, int nb_args, const char *str) |
3837 | { | |
3838 | size_t len; | |
3839 | ||
3840 | len = strlen(str); | |
3841 | readline_set_completion_index(rs, len); | |
3842 | if (nb_args == 2) { | |
0d4e995c DB |
3843 | TraceEventIter iter; |
3844 | TraceEvent *ev; | |
3845 | char *pattern = g_strdup_printf("%s*", str); | |
3846 | trace_event_iter_init(&iter, pattern); | |
3847 | while ((ev = trace_event_iter_next(&iter)) != NULL) { | |
3848 | readline_add_completion(rs, trace_event_get_name(ev)); | |
3849 | } | |
3850 | g_free(pattern); | |
987bd270 DDAG |
3851 | } else if (nb_args == 3) { |
3852 | add_completion_option(rs, str, "on"); | |
3853 | add_completion_option(rs, str, "off"); | |
3854 | } | |
3855 | } | |
3856 | ||
d0ece345 HB |
3857 | void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str) |
3858 | { | |
4bb08af3 HB |
3859 | int i; |
3860 | ||
d0ece345 HB |
3861 | if (nb_args != 2) { |
3862 | return; | |
3863 | } | |
3864 | readline_set_completion_index(rs, strlen(str)); | |
14d53b4f MP |
3865 | for (i = 0; i < WATCHDOG_ACTION__MAX; i++) { |
3866 | add_completion_option(rs, str, WatchdogAction_str(i)); | |
4bb08af3 | 3867 | } |
d0ece345 HB |
3868 | } |
3869 | ||
c68a0409 HB |
3870 | void migrate_set_capability_completion(ReadLineState *rs, int nb_args, |
3871 | const char *str) | |
3872 | { | |
3873 | size_t len; | |
3874 | ||
3875 | len = strlen(str); | |
3876 | readline_set_completion_index(rs, len); | |
3877 | if (nb_args == 2) { | |
3878 | int i; | |
7fb1cf16 | 3879 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
977c736f | 3880 | const char *name = MigrationCapability_str(i); |
c68a0409 HB |
3881 | if (!strncmp(str, name, len)) { |
3882 | readline_add_completion(rs, name); | |
3883 | } | |
3884 | } | |
3885 | } else if (nb_args == 3) { | |
3886 | add_completion_option(rs, str, "on"); | |
3887 | add_completion_option(rs, str, "off"); | |
3888 | } | |
3889 | } | |
3890 | ||
50e9a629 LL |
3891 | void migrate_set_parameter_completion(ReadLineState *rs, int nb_args, |
3892 | const char *str) | |
3893 | { | |
3894 | size_t len; | |
3895 | ||
3896 | len = strlen(str); | |
3897 | readline_set_completion_index(rs, len); | |
3898 | if (nb_args == 2) { | |
3899 | int i; | |
7fb1cf16 | 3900 | for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) { |
977c736f | 3901 | const char *name = MigrationParameter_str(i); |
50e9a629 LL |
3902 | if (!strncmp(str, name, len)) { |
3903 | readline_add_completion(rs, name); | |
3904 | } | |
3905 | } | |
3906 | } | |
3907 | } | |
3908 | ||
b21631f3 HB |
3909 | static void vm_completion(ReadLineState *rs, const char *str) |
3910 | { | |
3911 | size_t len; | |
7c8eece4 | 3912 | BlockDriverState *bs; |
88be7b4b | 3913 | BdrvNextIterator it; |
b21631f3 HB |
3914 | |
3915 | len = strlen(str); | |
3916 | readline_set_completion_index(rs, len); | |
7c8eece4 | 3917 | |
88be7b4b | 3918 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
b21631f3 | 3919 | SnapshotInfoList *snapshots, *snapshot; |
6bf1faa8 DL |
3920 | AioContext *ctx = bdrv_get_aio_context(bs); |
3921 | bool ok = false; | |
b21631f3 | 3922 | |
6bf1faa8 DL |
3923 | aio_context_acquire(ctx); |
3924 | if (bdrv_can_snapshot(bs)) { | |
3925 | ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0; | |
b21631f3 | 3926 | } |
6bf1faa8 DL |
3927 | aio_context_release(ctx); |
3928 | if (!ok) { | |
b21631f3 HB |
3929 | continue; |
3930 | } | |
6bf1faa8 | 3931 | |
b21631f3 HB |
3932 | snapshot = snapshots; |
3933 | while (snapshot) { | |
3934 | char *completion = snapshot->value->name; | |
3935 | if (!strncmp(str, completion, len)) { | |
3936 | readline_add_completion(rs, completion); | |
3937 | } | |
3938 | completion = snapshot->value->id; | |
3939 | if (!strncmp(str, completion, len)) { | |
3940 | readline_add_completion(rs, completion); | |
3941 | } | |
3942 | snapshot = snapshot->next; | |
3943 | } | |
3944 | qapi_free_SnapshotInfoList(snapshots); | |
3945 | } | |
3946 | ||
3947 | } | |
3948 | ||
3949 | void delvm_completion(ReadLineState *rs, int nb_args, const char *str) | |
3950 | { | |
3951 | if (nb_args == 2) { | |
3952 | vm_completion(rs, str); | |
3953 | } | |
3954 | } | |
3955 | ||
3956 | void loadvm_completion(ReadLineState *rs, int nb_args, const char *str) | |
3957 | { | |
3958 | if (nb_args == 2) { | |
3959 | vm_completion(rs, str); | |
3960 | } | |
3961 | } | |
3962 | ||
c35b6400 WX |
3963 | static void monitor_find_completion_by_table(Monitor *mon, |
3964 | const mon_cmd_t *cmd_table, | |
3965 | char **args, | |
3966 | int nb_args) | |
81d0912d FB |
3967 | { |
3968 | const char *cmdname; | |
c35b6400 | 3969 | int i; |
bf67f1c0 | 3970 | const char *ptype, *old_ptype, *str, *name; |
c227f099 | 3971 | const mon_cmd_t *cmd; |
da27a00e | 3972 | BlockBackend *blk = NULL; |
81d0912d | 3973 | |
81d0912d FB |
3974 | if (nb_args <= 1) { |
3975 | /* command completion */ | |
3976 | if (nb_args == 0) | |
3977 | cmdname = ""; | |
3978 | else | |
3979 | cmdname = args[0]; | |
d2674b2c | 3980 | readline_set_completion_index(mon->rs, strlen(cmdname)); |
c35b6400 | 3981 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
6d9f7839 DDAG |
3982 | if (!runstate_check(RUN_STATE_PRECONFIG) || |
3983 | cmd_can_preconfig(cmd)) { | |
3984 | cmd_completion(mon, cmdname, cmd->name); | |
3985 | } | |
81d0912d FB |
3986 | } |
3987 | } else { | |
3988 | /* find the command */ | |
c35b6400 | 3989 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
6d9f7839 DDAG |
3990 | if (compare_cmd(args[0], cmd->name) && |
3991 | (!runstate_check(RUN_STATE_PRECONFIG) || | |
3992 | cmd_can_preconfig(cmd))) { | |
03a63484 JK |
3993 | break; |
3994 | } | |
81d0912d | 3995 | } |
03a63484 | 3996 | if (!cmd->name) { |
c35b6400 | 3997 | return; |
03a63484 JK |
3998 | } |
3999 | ||
d903a779 WX |
4000 | if (cmd->sub_table) { |
4001 | /* do the job again */ | |
e7ae771f SW |
4002 | monitor_find_completion_by_table(mon, cmd->sub_table, |
4003 | &args[1], nb_args - 1); | |
4004 | return; | |
d903a779 | 4005 | } |
bfa40f77 | 4006 | if (cmd->command_completion) { |
e7ae771f SW |
4007 | cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]); |
4008 | return; | |
bfa40f77 | 4009 | } |
d903a779 | 4010 | |
4d76d2ba | 4011 | ptype = next_arg_type(cmd->args_type); |
81d0912d FB |
4012 | for(i = 0; i < nb_args - 2; i++) { |
4013 | if (*ptype != '\0') { | |
4d76d2ba | 4014 | ptype = next_arg_type(ptype); |
81d0912d | 4015 | while (*ptype == '?') |
4d76d2ba | 4016 | ptype = next_arg_type(ptype); |
81d0912d FB |
4017 | } |
4018 | } | |
4019 | str = args[nb_args - 1]; | |
bf67f1c0 DDAG |
4020 | old_ptype = NULL; |
4021 | while (*ptype == '-' && old_ptype != ptype) { | |
4022 | old_ptype = ptype; | |
3b6dbf27 | 4023 | ptype = next_arg_type(ptype); |
2a1704a7 | 4024 | } |
81d0912d FB |
4025 | switch(*ptype) { |
4026 | case 'F': | |
4027 | /* file completion */ | |
d2674b2c | 4028 | readline_set_completion_index(mon->rs, strlen(str)); |
cb8f68b1 | 4029 | file_completion(mon, str); |
81d0912d FB |
4030 | break; |
4031 | case 'B': | |
4032 | /* block device name completion */ | |
599a926a | 4033 | readline_set_completion_index(mon->rs, strlen(str)); |
da27a00e HR |
4034 | while ((blk = blk_next(blk)) != NULL) { |
4035 | name = blk_name(blk); | |
fea68bb6 MA |
4036 | if (str[0] == '\0' || |
4037 | !strncmp(name, str, strlen(str))) { | |
4038 | readline_add_completion(mon->rs, name); | |
4039 | } | |
4040 | } | |
81d0912d | 4041 | break; |
7fe48483 | 4042 | case 's': |
129be006 | 4043 | case 'S': |
29136cd8 | 4044 | if (!strcmp(cmd->name, "help|?")) { |
7ca0e061 WX |
4045 | monitor_find_completion_by_table(mon, cmd_table, |
4046 | &args[1], nb_args - 1); | |
7fe48483 FB |
4047 | } |
4048 | break; | |
81d0912d FB |
4049 | default: |
4050 | break; | |
4051 | } | |
4052 | } | |
c35b6400 WX |
4053 | } |
4054 | ||
c60bf339 | 4055 | static void monitor_find_completion(void *opaque, |
c35b6400 WX |
4056 | const char *cmdline) |
4057 | { | |
c60bf339 | 4058 | Monitor *mon = opaque; |
c35b6400 WX |
4059 | char *args[MAX_ARGS]; |
4060 | int nb_args, len; | |
4061 | ||
4062 | /* 1. parse the cmdline */ | |
4063 | if (parse_cmdline(cmdline, &nb_args, args) < 0) { | |
4064 | return; | |
4065 | } | |
c35b6400 WX |
4066 | |
4067 | /* if the line ends with a space, it means we want to complete the | |
4068 | next arg */ | |
4069 | len = strlen(cmdline); | |
4070 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { | |
4071 | if (nb_args >= MAX_ARGS) { | |
4072 | goto cleanup; | |
4073 | } | |
4074 | args[nb_args++] = g_strdup(""); | |
4075 | } | |
4076 | ||
4077 | /* 2. auto complete according to args */ | |
4078 | monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args); | |
03a63484 JK |
4079 | |
4080 | cleanup: | |
dcc70cdf | 4081 | free_cmdline_args(args, nb_args); |
81d0912d FB |
4082 | } |
4083 | ||
731b0364 | 4084 | static int monitor_can_read(void *opaque) |
9dc39cba | 4085 | { |
731b0364 AL |
4086 | Monitor *mon = opaque; |
4087 | ||
df152fb9 | 4088 | return !atomic_mb_read(&mon->suspend_cnt); |
9dc39cba FB |
4089 | } |
4090 | ||
546aa566 | 4091 | /* |
7cb2123f MA |
4092 | * Emit QMP response @rsp with ID @id to @mon. |
4093 | * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP. | |
4094 | * Nothing is emitted then. | |
546aa566 | 4095 | */ |
7cb2123f | 4096 | static void monitor_qmp_respond(Monitor *mon, QDict *rsp, QObject *id) |
546aa566 | 4097 | { |
546aa566 PX |
4098 | if (rsp) { |
4099 | if (id) { | |
d43b1694 | 4100 | qdict_put_obj(rsp, "id", qobject_ref(id)); |
546aa566 PX |
4101 | } |
4102 | ||
65e3fe67 | 4103 | qmp_queue_response(mon, rsp); |
546aa566 | 4104 | } |
546aa566 PX |
4105 | } |
4106 | ||
b2731456 | 4107 | static void monitor_qmp_dispatch(Monitor *mon, QObject *req, QObject *id) |
5fa737a4 | 4108 | { |
b2731456 | 4109 | Monitor *old_mon; |
d43b1694 | 4110 | QDict *rsp; |
69240fe6 | 4111 | QDict *error; |
b097efc0 | 4112 | |
227a0755 PX |
4113 | old_mon = cur_mon; |
4114 | cur_mon = mon; | |
4115 | ||
674ed722 | 4116 | rsp = qmp_dispatch(mon->qmp.commands, req, qmp_oob_enabled(mon)); |
5fa737a4 | 4117 | |
227a0755 PX |
4118 | cur_mon = old_mon; |
4119 | ||
69240fe6 | 4120 | if (mon->qmp.commands == &qmp_cap_negotiation_commands) { |
d43b1694 | 4121 | error = qdict_get_qdict(rsp, "error"); |
69240fe6 MA |
4122 | if (error |
4123 | && !g_strcmp0(qdict_get_try_str(error, "class"), | |
4124 | QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) { | |
4125 | /* Provide a more useful error message */ | |
4126 | qdict_del(error, "desc"); | |
4127 | qdict_put_str(error, "desc", "Expecting capabilities negotiation" | |
4128 | " with 'qmp_capabilities'"); | |
4129 | } | |
71da4667 | 4130 | } |
5fa737a4 | 4131 | |
7cb2123f MA |
4132 | monitor_qmp_respond(mon, rsp, id); |
4133 | qobject_unref(rsp); | |
5fa737a4 LC |
4134 | } |
4135 | ||
71da4667 | 4136 | /* |
774a6b67 MA |
4137 | * Pop a QMP request from a monitor request queue. |
4138 | * Return the request, or NULL all request queues are empty. | |
71da4667 PX |
4139 | * We are using round-robin fashion to pop the request, to avoid |
4140 | * processing commands only on a very busy monitor. To achieve that, | |
4141 | * when we process one request on a specific monitor, we put that | |
4142 | * monitor to the end of mon_list queue. | |
4143 | */ | |
40687eb7 | 4144 | static QMPRequest *monitor_qmp_requests_pop_any(void) |
71da4667 PX |
4145 | { |
4146 | QMPRequest *req_obj = NULL; | |
4147 | Monitor *mon; | |
4148 | ||
4149 | qemu_mutex_lock(&monitor_lock); | |
4150 | ||
4151 | QTAILQ_FOREACH(mon, &mon_list, entry) { | |
4152 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); | |
4153 | req_obj = g_queue_pop_head(mon->qmp.qmp_requests); | |
4154 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); | |
4155 | if (req_obj) { | |
4156 | break; | |
4157 | } | |
4158 | } | |
4159 | ||
4160 | if (req_obj) { | |
4161 | /* | |
4162 | * We found one request on the monitor. Degrade this monitor's | |
4163 | * priority to lowest by re-inserting it to end of queue. | |
4164 | */ | |
4165 | QTAILQ_REMOVE(&mon_list, mon, entry); | |
4166 | QTAILQ_INSERT_TAIL(&mon_list, mon, entry); | |
4167 | } | |
4168 | ||
4169 | qemu_mutex_unlock(&monitor_lock); | |
4170 | ||
4171 | return req_obj; | |
4172 | } | |
4173 | ||
4174 | static void monitor_qmp_bh_dispatcher(void *data) | |
4175 | { | |
40687eb7 | 4176 | QMPRequest *req_obj = monitor_qmp_requests_pop_any(); |
7cb2123f | 4177 | QDict *rsp; |
71da4667 | 4178 | |
b2731456 MA |
4179 | if (!req_obj) { |
4180 | return; | |
71da4667 | 4181 | } |
b2731456 | 4182 | |
1cc37471 | 4183 | if (req_obj->req) { |
cf869d53 | 4184 | trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: ""); |
1cc37471 MA |
4185 | monitor_qmp_dispatch(req_obj->mon, req_obj->req, req_obj->id); |
4186 | } else { | |
4187 | assert(req_obj->err); | |
7cb2123f MA |
4188 | rsp = qmp_error_response(req_obj->err); |
4189 | monitor_qmp_respond(req_obj->mon, rsp, NULL); | |
4190 | qobject_unref(rsp); | |
1cc37471 MA |
4191 | } |
4192 | ||
b2731456 MA |
4193 | if (req_obj->need_resume) { |
4194 | /* Pairs with the monitor_suspend() in handle_qmp_command() */ | |
4195 | monitor_resume(req_obj->mon); | |
71da4667 | 4196 | } |
b2731456 MA |
4197 | qmp_request_free(req_obj); |
4198 | ||
4199 | /* Reschedule instead of looping so the main loop stays responsive */ | |
cab5ad86 | 4200 | qemu_bh_schedule(qmp_dispatcher_bh); |
71da4667 PX |
4201 | } |
4202 | ||
bf1e7301 PX |
4203 | #define QMP_REQ_QUEUE_LEN_MAX (8) |
4204 | ||
71da4667 PX |
4205 | static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens) |
4206 | { | |
4207 | QObject *req, *id = NULL; | |
0fa39d0b | 4208 | QDict *qdict; |
71da4667 PX |
4209 | MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser); |
4210 | Monitor *mon = container_of(mon_qmp, Monitor, qmp); | |
4211 | Error *err = NULL; | |
4212 | QMPRequest *req_obj; | |
4213 | ||
4214 | req = json_parser_parse_err(tokens, NULL, &err); | |
4215 | if (!req && !err) { | |
4216 | /* json_parser_parse_err() sucks: can fail without setting @err */ | |
4217 | error_setg(&err, QERR_JSON_PARSING); | |
4218 | } | |
cf869d53 | 4219 | |
0fa39d0b MA |
4220 | qdict = qobject_to(QDict, req); |
4221 | if (qdict) { | |
4222 | id = qobject_ref(qdict_get(qdict, "id")); | |
4223 | qdict_del(qdict, "id"); | |
4224 | } /* else will fail qmp_dispatch() */ | |
71da4667 | 4225 | |
45434ba4 MA |
4226 | if (trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) { |
4227 | QString *req_json = qobject_to_json(req); | |
4228 | trace_handle_qmp_command(mon, qstring_get_str(req_json)); | |
4229 | qobject_unref(req_json); | |
cf869d53 PX |
4230 | } |
4231 | ||
69240fe6 | 4232 | if (qdict && qmp_is_oob(qdict)) { |
774a6b67 | 4233 | /* OOB commands are executed immediately */ |
b2731456 | 4234 | trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id) |
cf869d53 | 4235 | ?: ""); |
b2731456 | 4236 | monitor_qmp_dispatch(mon, req, id); |
cf869d53 | 4237 | return; |
cf869d53 PX |
4238 | } |
4239 | ||
71da4667 PX |
4240 | req_obj = g_new0(QMPRequest, 1); |
4241 | req_obj->mon = mon; | |
b2731456 | 4242 | req_obj->id = id; |
71da4667 | 4243 | req_obj->req = req; |
1cc37471 | 4244 | req_obj->err = err; |
71da4667 PX |
4245 | req_obj->need_resume = false; |
4246 | ||
bf1e7301 PX |
4247 | /* Protect qmp_requests and fetching its length. */ |
4248 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); | |
4249 | ||
71da4667 PX |
4250 | /* |
4251 | * If OOB is not enabled on the current monitor, we'll emulate the | |
4252 | * old behavior that we won't process the current monitor any more | |
4253 | * until it has responded. This helps make sure that as long as | |
4254 | * OOB is not enabled, the server will never drop any command. | |
4255 | */ | |
4256 | if (!qmp_oob_enabled(mon)) { | |
4257 | monitor_suspend(mon); | |
4258 | req_obj->need_resume = true; | |
bf1e7301 PX |
4259 | } else { |
4260 | /* Drop the request if queue is full. */ | |
4261 | if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) { | |
4262 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); | |
d621cfe0 MA |
4263 | /* |
4264 | * FIXME @id's scope is just @mon, and broadcasting it is | |
4265 | * wrong. If another monitor's client has a command with | |
4266 | * the same ID in flight, the event will incorrectly claim | |
4267 | * that command was dropped. | |
4268 | */ | |
bf1e7301 PX |
4269 | qapi_event_send_command_dropped(id, |
4270 | COMMAND_DROP_REASON_QUEUE_FULL, | |
4271 | &error_abort); | |
6d2d563f | 4272 | qmp_request_free(req_obj); |
bf1e7301 PX |
4273 | return; |
4274 | } | |
71da4667 PX |
4275 | } |
4276 | ||
4277 | /* | |
4278 | * Put the request to the end of queue so that requests will be | |
4279 | * handled in time order. Ownership for req_obj, req, id, | |
4280 | * etc. will be delivered to the handler side. | |
4281 | */ | |
71da4667 PX |
4282 | g_queue_push_tail(mon->qmp.qmp_requests, req_obj); |
4283 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); | |
4284 | ||
4285 | /* Kick the dispatcher routine */ | |
cab5ad86 | 4286 | qemu_bh_schedule(qmp_dispatcher_bh); |
71da4667 PX |
4287 | } |
4288 | ||
c83fe23b | 4289 | static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) |
9b57c02e | 4290 | { |
227a0755 | 4291 | Monitor *mon = opaque; |
9b57c02e | 4292 | |
227a0755 | 4293 | json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size); |
9b57c02e LC |
4294 | } |
4295 | ||
731b0364 | 4296 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 4297 | { |
731b0364 | 4298 | Monitor *old_mon = cur_mon; |
7e2515e8 | 4299 | int i; |
376253ec | 4300 | |
731b0364 AL |
4301 | cur_mon = opaque; |
4302 | ||
cde76ee1 AL |
4303 | if (cur_mon->rs) { |
4304 | for (i = 0; i < size; i++) | |
4305 | readline_handle_byte(cur_mon->rs, buf[i]); | |
4306 | } else { | |
4307 | if (size == 0 || buf[size - 1] != 0) | |
4308 | monitor_printf(cur_mon, "corrupted command\n"); | |
4309 | else | |
7ef6cf63 | 4310 | handle_hmp_command(cur_mon, (char *)buf); |
cde76ee1 | 4311 | } |
9dc39cba | 4312 | |
731b0364 AL |
4313 | cur_mon = old_mon; |
4314 | } | |
d8f44609 | 4315 | |
c60bf339 SH |
4316 | static void monitor_command_cb(void *opaque, const char *cmdline, |
4317 | void *readline_opaque) | |
aa455485 | 4318 | { |
c60bf339 SH |
4319 | Monitor *mon = opaque; |
4320 | ||
731b0364 | 4321 | monitor_suspend(mon); |
7ef6cf63 | 4322 | handle_hmp_command(mon, cmdline); |
731b0364 | 4323 | monitor_resume(mon); |
d8f44609 AL |
4324 | } |
4325 | ||
cde76ee1 | 4326 | int monitor_suspend(Monitor *mon) |
d8f44609 | 4327 | { |
e3e977d4 | 4328 | if (monitor_is_hmp_non_interactive(mon)) { |
cde76ee1 | 4329 | return -ENOTTY; |
e3e977d4 PX |
4330 | } |
4331 | ||
df152fb9 | 4332 | atomic_inc(&mon->suspend_cnt); |
e3e977d4 PX |
4333 | |
4334 | if (monitor_is_qmp(mon)) { | |
4335 | /* | |
c5f57ed0 | 4336 | * Kick I/O thread to make sure this takes effect. It'll be |
e3e977d4 PX |
4337 | * evaluated again in prepare() of the watch object. |
4338 | */ | |
cab5ad86 | 4339 | aio_notify(iothread_get_aio_context(mon_iothread)); |
e3e977d4 PX |
4340 | } |
4341 | ||
4342 | trace_monitor_suspend(mon, 1); | |
cde76ee1 | 4343 | return 0; |
d8f44609 AL |
4344 | } |
4345 | ||
376253ec | 4346 | void monitor_resume(Monitor *mon) |
d8f44609 | 4347 | { |
e3e977d4 | 4348 | if (monitor_is_hmp_non_interactive(mon)) { |
cde76ee1 | 4349 | return; |
e3e977d4 PX |
4350 | } |
4351 | ||
df152fb9 | 4352 | if (atomic_dec_fetch(&mon->suspend_cnt) == 0) { |
e3e977d4 PX |
4353 | if (monitor_is_qmp(mon)) { |
4354 | /* | |
774a6b67 MA |
4355 | * For QMP monitors that are running in the I/O thread, |
4356 | * let's kick the thread in case it's sleeping. | |
e3e977d4 | 4357 | */ |
f91dc2a0 | 4358 | if (mon->use_io_thread) { |
cab5ad86 | 4359 | aio_notify(iothread_get_aio_context(mon_iothread)); |
e3e977d4 PX |
4360 | } |
4361 | } else { | |
4362 | assert(mon->rs); | |
4363 | readline_show_prompt(mon->rs); | |
4364 | } | |
df152fb9 | 4365 | } |
e3e977d4 | 4366 | trace_monitor_suspend(mon, -1); |
aa455485 FB |
4367 | } |
4368 | ||
1816604b | 4369 | static QDict *qmp_greeting(Monitor *mon) |
ca9567e2 | 4370 | { |
02130314 | 4371 | QList *cap_list = qlist_new(); |
b9c15f16 | 4372 | QObject *ver = NULL; |
02130314 | 4373 | QMPCapability cap; |
ca9567e2 | 4374 | |
7fad30f0 | 4375 | qmp_marshal_query_version(NULL, &ver, NULL); |
c823501e | 4376 | |
02130314 | 4377 | for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) { |
279f9e08 MA |
4378 | if (mon->qmp.capab_offered[cap]) { |
4379 | qlist_append_str(cap_list, QMPCapability_str(cap)); | |
02130314 | 4380 | } |
02130314 PX |
4381 | } |
4382 | ||
1816604b MA |
4383 | return qdict_from_jsonf_nofail( |
4384 | "{'QMP': {'version': %p, 'capabilities': %p}}", | |
4385 | ver, cap_list); | |
ca9567e2 LC |
4386 | } |
4387 | ||
c83fe23b | 4388 | static void monitor_qmp_event(void *opaque, int event) |
9b57c02e | 4389 | { |
1816604b | 4390 | QDict *data; |
47116d1c | 4391 | Monitor *mon = opaque; |
9b57c02e | 4392 | |
47116d1c LC |
4393 | switch (event) { |
4394 | case CHR_EVENT_OPENED: | |
635db18f | 4395 | mon->qmp.commands = &qmp_cap_negotiation_commands; |
02130314 | 4396 | monitor_qmp_caps_reset(mon); |
1816604b MA |
4397 | data = qmp_greeting(mon); |
4398 | qmp_queue_response(mon, data); | |
cb3e7f08 | 4399 | qobject_unref(data); |
efb87c16 | 4400 | mon_refcount++; |
47116d1c LC |
4401 | break; |
4402 | case CHR_EVENT_CLOSED: | |
c73a843b PX |
4403 | /* |
4404 | * Note: this is only useful when the output of the chardev | |
4405 | * backend is still open. For example, when the backend is | |
4406 | * stdio, it's possible that stdout is still open when stdin | |
4407 | * is closed. | |
4408 | */ | |
4409 | monitor_qmp_response_flush(mon); | |
6d2d563f | 4410 | monitor_qmp_cleanup_queues(mon); |
74358f2a MA |
4411 | json_message_parser_destroy(&mon->qmp.parser); |
4412 | json_message_parser_init(&mon->qmp.parser, handle_qmp_command); | |
efb87c16 CB |
4413 | mon_refcount--; |
4414 | monitor_fdsets_cleanup(); | |
47116d1c | 4415 | break; |
9b57c02e LC |
4416 | } |
4417 | } | |
4418 | ||
731b0364 | 4419 | static void monitor_event(void *opaque, int event) |
86e94dea | 4420 | { |
376253ec AL |
4421 | Monitor *mon = opaque; |
4422 | ||
2724b180 AL |
4423 | switch (event) { |
4424 | case CHR_EVENT_MUX_IN: | |
dc7cbcd8 | 4425 | qemu_mutex_lock(&mon->mon_lock); |
a7aec5da | 4426 | mon->mux_out = 0; |
dc7cbcd8 | 4427 | qemu_mutex_unlock(&mon->mon_lock); |
a7aec5da GH |
4428 | if (mon->reset_seen) { |
4429 | readline_restart(mon->rs); | |
4430 | monitor_resume(mon); | |
4431 | monitor_flush(mon); | |
4432 | } else { | |
df152fb9 | 4433 | atomic_mb_set(&mon->suspend_cnt, 0); |
a7aec5da | 4434 | } |
2724b180 AL |
4435 | break; |
4436 | ||
4437 | case CHR_EVENT_MUX_OUT: | |
a7aec5da | 4438 | if (mon->reset_seen) { |
df152fb9 | 4439 | if (atomic_mb_read(&mon->suspend_cnt) == 0) { |
a7aec5da GH |
4440 | monitor_printf(mon, "\n"); |
4441 | } | |
4442 | monitor_flush(mon); | |
4443 | monitor_suspend(mon); | |
4444 | } else { | |
df152fb9 | 4445 | atomic_inc(&mon->suspend_cnt); |
a7aec5da | 4446 | } |
dc7cbcd8 | 4447 | qemu_mutex_lock(&mon->mon_lock); |
a7aec5da | 4448 | mon->mux_out = 1; |
dc7cbcd8 | 4449 | qemu_mutex_unlock(&mon->mon_lock); |
2724b180 | 4450 | break; |
86e94dea | 4451 | |
b6b8df56 | 4452 | case CHR_EVENT_OPENED: |
2724b180 AL |
4453 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " |
4454 | "information\n", QEMU_VERSION); | |
a7aec5da | 4455 | if (!mon->mux_out) { |
e5554e20 | 4456 | readline_restart(mon->rs); |
2724b180 | 4457 | readline_show_prompt(mon->rs); |
a7aec5da GH |
4458 | } |
4459 | mon->reset_seen = 1; | |
efb87c16 CB |
4460 | mon_refcount++; |
4461 | break; | |
4462 | ||
4463 | case CHR_EVENT_CLOSED: | |
4464 | mon_refcount--; | |
4465 | monitor_fdsets_cleanup(); | |
2724b180 AL |
4466 | break; |
4467 | } | |
86e94dea TS |
4468 | } |
4469 | ||
816f8925 WX |
4470 | static int |
4471 | compare_mon_cmd(const void *a, const void *b) | |
4472 | { | |
4473 | return strcmp(((const mon_cmd_t *)a)->name, | |
4474 | ((const mon_cmd_t *)b)->name); | |
4475 | } | |
4476 | ||
4477 | static void sortcmdlist(void) | |
4478 | { | |
4479 | int array_num; | |
4480 | int elem_size = sizeof(mon_cmd_t); | |
4481 | ||
4482 | array_num = sizeof(mon_cmds)/elem_size-1; | |
4483 | qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd); | |
4484 | ||
4485 | array_num = sizeof(info_cmds)/elem_size-1; | |
4486 | qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd); | |
4487 | } | |
4488 | ||
a5ed3525 PX |
4489 | static GMainContext *monitor_get_io_context(void) |
4490 | { | |
cab5ad86 | 4491 | return iothread_get_g_main_context(mon_iothread); |
a5ed3525 PX |
4492 | } |
4493 | ||
4494 | static AioContext *monitor_get_aio_context(void) | |
4495 | { | |
cab5ad86 | 4496 | return iothread_get_aio_context(mon_iothread); |
a5ed3525 PX |
4497 | } |
4498 | ||
4499 | static void monitor_iothread_init(void) | |
4500 | { | |
cab5ad86 | 4501 | mon_iothread = iothread_create("mon_iothread", &error_abort); |
71da4667 PX |
4502 | |
4503 | /* | |
774a6b67 MA |
4504 | * The dispatcher BH must run in the main loop thread, since we |
4505 | * have commands assuming that context. It would be nice to get | |
4506 | * rid of those assumptions. | |
71da4667 | 4507 | */ |
cab5ad86 MA |
4508 | qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(), |
4509 | monitor_qmp_bh_dispatcher, | |
4510 | NULL); | |
abe3cd0f PX |
4511 | |
4512 | /* | |
774a6b67 MA |
4513 | * The responder BH must be run in the monitor I/O thread, so that |
4514 | * monitors that are using the I/O thread have their output | |
4515 | * written by the I/O thread. | |
abe3cd0f | 4516 | */ |
cab5ad86 MA |
4517 | qmp_respond_bh = aio_bh_new(monitor_get_aio_context(), |
4518 | monitor_qmp_bh_responder, | |
4519 | NULL); | |
a5ed3525 PX |
4520 | } |
4521 | ||
6adf08dd PX |
4522 | void monitor_init_globals(void) |
4523 | { | |
4524 | monitor_init_qmp_commands(); | |
4525 | monitor_qapi_event_init(); | |
4526 | sortcmdlist(); | |
4527 | qemu_mutex_init(&monitor_lock); | |
47451466 | 4528 | qemu_mutex_init(&mon_fdsets_lock); |
a5ed3525 | 4529 | monitor_iothread_init(); |
6adf08dd PX |
4530 | } |
4531 | ||
c60bf339 SH |
4532 | /* These functions just adapt the readline interface in a typesafe way. We |
4533 | * could cast function pointers but that discards compiler checks. | |
4534 | */ | |
d5d1507b SW |
4535 | static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque, |
4536 | const char *fmt, ...) | |
c60bf339 SH |
4537 | { |
4538 | va_list ap; | |
4539 | va_start(ap, fmt); | |
4540 | monitor_vprintf(opaque, fmt, ap); | |
4541 | va_end(ap); | |
4542 | } | |
4543 | ||
4544 | static void monitor_readline_flush(void *opaque) | |
4545 | { | |
4546 | monitor_flush(opaque); | |
4547 | } | |
4548 | ||
397d30e9 PB |
4549 | /* |
4550 | * Print to current monitor if we have one, else to stderr. | |
4551 | * TODO should return int, so callers can calculate width, but that | |
4552 | * requires surgery to monitor_vprintf(). Left for another day. | |
4553 | */ | |
4554 | void error_vprintf(const char *fmt, va_list ap) | |
4555 | { | |
4556 | if (cur_mon && !monitor_cur_is_qmp()) { | |
4557 | monitor_vprintf(cur_mon, fmt, ap); | |
4558 | } else { | |
4559 | vfprintf(stderr, fmt, ap); | |
4560 | } | |
4561 | } | |
4562 | ||
4563 | void error_vprintf_unless_qmp(const char *fmt, va_list ap) | |
4564 | { | |
4565 | if (cur_mon && !monitor_cur_is_qmp()) { | |
4566 | monitor_vprintf(cur_mon, fmt, ap); | |
0d6b50d4 MAL |
4567 | } else if (!cur_mon) { |
4568 | vfprintf(stderr, fmt, ap); | |
397d30e9 PB |
4569 | } |
4570 | } | |
4571 | ||
a5ed3525 PX |
4572 | static void monitor_list_append(Monitor *mon) |
4573 | { | |
4574 | qemu_mutex_lock(&monitor_lock); | |
4575 | QTAILQ_INSERT_HEAD(&mon_list, mon, entry); | |
4576 | qemu_mutex_unlock(&monitor_lock); | |
4577 | } | |
4578 | ||
4579 | static void monitor_qmp_setup_handlers_bh(void *opaque) | |
4580 | { | |
4581 | Monitor *mon = opaque; | |
4582 | GMainContext *context; | |
4583 | ||
f91dc2a0 | 4584 | if (mon->use_io_thread) { |
774a6b67 | 4585 | /* Use @mon_iothread context */ |
a5ed3525 | 4586 | context = monitor_get_io_context(); |
a5ed3525 PX |
4587 | assert(context); |
4588 | } else { | |
774a6b67 | 4589 | /* Use default main loop context */ |
a5ed3525 PX |
4590 | context = NULL; |
4591 | } | |
4592 | ||
4593 | qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read, | |
4594 | monitor_qmp_event, NULL, mon, context, true); | |
4595 | monitor_list_append(mon); | |
4596 | } | |
4597 | ||
0ec7b3e7 | 4598 | void monitor_init(Chardev *chr, int flags) |
aa455485 | 4599 | { |
6adf08dd | 4600 | Monitor *mon = g_malloc(sizeof(*mon)); |
be933ffc PX |
4601 | bool use_readline = flags & MONITOR_USE_READLINE; |
4602 | bool use_oob = flags & MONITOR_USE_OOB; | |
4603 | ||
4604 | if (use_oob) { | |
4605 | if (CHARDEV_IS_MUX(chr)) { | |
c0698212 | 4606 | error_report("Monitor out-of-band is not supported with " |
be933ffc PX |
4607 | "MUX typed chardev backend"); |
4608 | exit(1); | |
4609 | } | |
4610 | if (use_readline) { | |
c0698212 | 4611 | error_report("Monitor out-of-band is only supported by QMP"); |
be933ffc PX |
4612 | exit(1); |
4613 | } | |
4614 | } | |
87127161 | 4615 | |
be933ffc | 4616 | monitor_data_init(mon, false, use_oob); |
20d8a3ed | 4617 | |
32a6ebec | 4618 | qemu_chr_fe_init(&mon->chr, chr, &error_abort); |
731b0364 | 4619 | mon->flags = flags; |
be933ffc | 4620 | if (use_readline) { |
c60bf339 SH |
4621 | mon->rs = readline_init(monitor_readline_printf, |
4622 | monitor_readline_flush, | |
4623 | mon, | |
4624 | monitor_find_completion); | |
cde76ee1 AL |
4625 | monitor_read_command(mon, 0); |
4626 | } | |
87127161 | 4627 | |
9f3982f2 | 4628 | if (monitor_is_qmp(mon)) { |
5345fdb4 | 4629 | qemu_chr_fe_set_echo(&mon->chr, true); |
74358f2a | 4630 | json_message_parser_init(&mon->qmp.parser, handle_qmp_command); |
f91dc2a0 | 4631 | if (mon->use_io_thread) { |
a5ed3525 PX |
4632 | /* |
4633 | * Make sure the old iowatch is gone. It's possible when | |
4634 | * e.g. the chardev is in client mode, with wait=on. | |
4635 | */ | |
4636 | remove_fd_in_watch(chr); | |
4637 | /* | |
4638 | * We can't call qemu_chr_fe_set_handlers() directly here | |
774a6b67 MA |
4639 | * since chardev might be running in the monitor I/O |
4640 | * thread. Schedule a bottom half. | |
a5ed3525 PX |
4641 | */ |
4642 | aio_bh_schedule_oneshot(monitor_get_aio_context(), | |
4643 | monitor_qmp_setup_handlers_bh, mon); | |
774a6b67 | 4644 | /* The bottom half will add @mon to @mon_list */ |
a5ed3525 PX |
4645 | return; |
4646 | } else { | |
4647 | qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, | |
4648 | monitor_qmp_read, monitor_qmp_event, | |
4649 | NULL, mon, NULL, true); | |
4650 | } | |
9b57c02e | 4651 | } else { |
5345fdb4 | 4652 | qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read, |
81517ba3 | 4653 | monitor_event, NULL, mon, NULL, true); |
9b57c02e | 4654 | } |
87127161 | 4655 | |
a5ed3525 | 4656 | monitor_list_append(mon); |
aa455485 FB |
4657 | } |
4658 | ||
2ef45716 MAL |
4659 | void monitor_cleanup(void) |
4660 | { | |
4661 | Monitor *mon, *next; | |
4662 | ||
a5ed3525 | 4663 | /* |
c5f57ed0 | 4664 | * We need to explicitly stop the I/O thread (but not destroy it), |
774a6b67 | 4665 | * clean up the monitor resources, then destroy the I/O thread since |
a5ed3525 PX |
4666 | * we need to unregister from chardev below in |
4667 | * monitor_data_destroy(), and chardev is not thread-safe yet | |
4668 | */ | |
cab5ad86 | 4669 | iothread_stop(mon_iothread); |
a5ed3525 | 4670 | |
abe3cd0f | 4671 | /* |
774a6b67 MA |
4672 | * Flush all response queues. Note that even after this flush, |
4673 | * data may remain in output buffers. | |
abe3cd0f PX |
4674 | */ |
4675 | monitor_qmp_bh_responder(NULL); | |
4676 | ||
774a6b67 | 4677 | /* Flush output buffers and destroy monitors */ |
2ef45716 | 4678 | qemu_mutex_lock(&monitor_lock); |
238d9f34 PX |
4679 | QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) { |
4680 | QTAILQ_REMOVE(&mon_list, mon, entry); | |
abe3cd0f | 4681 | monitor_flush(mon); |
2ef45716 MAL |
4682 | monitor_data_destroy(mon); |
4683 | g_free(mon); | |
4684 | } | |
4685 | qemu_mutex_unlock(&monitor_lock); | |
a5ed3525 | 4686 | |
c5f57ed0 | 4687 | /* QEMUBHs needs to be deleted before destroying the I/O thread */ |
cab5ad86 MA |
4688 | qemu_bh_delete(qmp_dispatcher_bh); |
4689 | qmp_dispatcher_bh = NULL; | |
4690 | qemu_bh_delete(qmp_respond_bh); | |
4691 | qmp_respond_bh = NULL; | |
71da4667 | 4692 | |
cab5ad86 MA |
4693 | iothread_destroy(mon_iothread); |
4694 | mon_iothread = NULL; | |
2ef45716 MAL |
4695 | } |
4696 | ||
4d454574 PB |
4697 | QemuOptsList qemu_mon_opts = { |
4698 | .name = "mon", | |
4699 | .implied_opt_name = "chardev", | |
4700 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), | |
4701 | .desc = { | |
4702 | { | |
4703 | .name = "mode", | |
4704 | .type = QEMU_OPT_STRING, | |
4705 | },{ | |
4706 | .name = "chardev", | |
4707 | .type = QEMU_OPT_STRING, | |
4d454574 PB |
4708 | },{ |
4709 | .name = "pretty", | |
4710 | .type = QEMU_OPT_BOOL, | |
be933ffc PX |
4711 | },{ |
4712 | .name = "x-oob", | |
4713 | .type = QEMU_OPT_BOOL, | |
4d454574 PB |
4714 | }, |
4715 | { /* end of list */ } | |
4716 | }, | |
4717 | }; | |
f2ae8abf MT |
4718 | |
4719 | #ifndef TARGET_I386 | |
4720 | void qmp_rtc_reset_reinjection(Error **errp) | |
4721 | { | |
c6bd8c70 | 4722 | error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection"); |
f2ae8abf | 4723 | } |
08a161fd BS |
4724 | |
4725 | SevInfo *qmp_query_sev(Error **errp) | |
4726 | { | |
4727 | error_setg(errp, QERR_FEATURE_DISABLED, "query-sev"); | |
4728 | return NULL; | |
4729 | } | |
1b6a034f BS |
4730 | |
4731 | SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp) | |
4732 | { | |
4733 | error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure"); | |
4734 | return NULL; | |
4735 | } | |
31dd67f6 BS |
4736 | |
4737 | SevCapability *qmp_query_sev_capabilities(Error **errp) | |
4738 | { | |
4739 | error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities"); | |
4740 | return NULL; | |
4741 | } | |
f2ae8abf | 4742 | #endif |
7ee0c3e3 JH |
4743 | |
4744 | #ifndef TARGET_S390X | |
4745 | void qmp_dump_skeys(const char *filename, Error **errp) | |
4746 | { | |
4747 | error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys"); | |
4748 | } | |
4749 | #endif | |
ae50a770 PX |
4750 | |
4751 | #ifndef TARGET_ARM | |
4752 | GICCapabilityList *qmp_query_gic_capabilities(Error **errp) | |
4753 | { | |
4754 | error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities"); | |
4755 | return NULL; | |
4756 | } | |
4757 | #endif | |
d4633541 IM |
4758 | |
4759 | HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp) | |
4760 | { | |
4761 | MachineState *ms = MACHINE(qdev_get_machine()); | |
4762 | MachineClass *mc = MACHINE_GET_CLASS(ms); | |
4763 | ||
c5514d0e | 4764 | if (!mc->has_hotpluggable_cpus) { |
d4633541 IM |
4765 | error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus"); |
4766 | return NULL; | |
4767 | } | |
4768 | ||
c5514d0e | 4769 | return machine_query_hotpluggable_cpus(ms); |
d4633541 | 4770 | } |