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