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