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