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