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