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