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