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