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