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