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