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