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