]>
Commit | Line | Data |
---|---|---|
9dc39cba FB |
1 | /* |
2 | * QEMU monitor | |
5fafdf24 | 3 | * |
9dc39cba | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
9dc39cba FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
511d2b14 | 24 | #include <dirent.h> |
87ecb68b | 25 | #include "hw/hw.h" |
cae4956e | 26 | #include "hw/qdev.h" |
87ecb68b PB |
27 | #include "hw/usb.h" |
28 | #include "hw/pcmcia.h" | |
29 | #include "hw/pc.h" | |
30 | #include "hw/pci.h" | |
9dd986cc | 31 | #include "hw/watchdog.h" |
45a50b16 | 32 | #include "hw/loader.h" |
87ecb68b PB |
33 | #include "gdbstub.h" |
34 | #include "net.h" | |
68ac40d2 | 35 | #include "net/slirp.h" |
87ecb68b | 36 | #include "qemu-char.h" |
7572150c | 37 | #include "ui/qemu-spice.h" |
87ecb68b | 38 | #include "sysemu.h" |
376253ec AL |
39 | #include "monitor.h" |
40 | #include "readline.h" | |
87ecb68b | 41 | #include "console.h" |
666daa68 | 42 | #include "blockdev.h" |
87ecb68b | 43 | #include "audio/audio.h" |
9307c4c1 | 44 | #include "disas.h" |
df751fa8 | 45 | #include "balloon.h" |
c8256f9d | 46 | #include "qemu-timer.h" |
5bb7910a | 47 | #include "migration.h" |
7ba1e619 | 48 | #include "kvm.h" |
76655d6d | 49 | #include "acl.h" |
f7188bbe | 50 | #include "qint.h" |
3350a4dd | 51 | #include "qfloat.h" |
8f3cec0b | 52 | #include "qlist.h" |
5fa737a4 | 53 | #include "qbool.h" |
f7188bbe | 54 | #include "qstring.h" |
9b57c02e | 55 | #include "qjson.h" |
5fa737a4 LC |
56 | #include "json-streamer.h" |
57 | #include "json-parser.h" | |
d08d6f04 | 58 | #include "osdep.h" |
2b41f10e | 59 | #include "cpu.h" |
89bd820a | 60 | #include "trace.h" |
31965ae2 | 61 | #include "trace/control.h" |
6d8a764e | 62 | #ifdef CONFIG_TRACE_SIMPLE |
31965ae2 | 63 | #include "trace/simple.h" |
22890ab5 | 64 | #endif |
6f8c63fb | 65 | #include "ui/qemu-spice.h" |
314e2987 | 66 | #include "memory.h" |
48a32bed AL |
67 | #include "qmp-commands.h" |
68 | #include "hmp.h" | |
afeecec2 | 69 | #include "qemu-thread.h" |
6a5bd307 | 70 | |
661f1929 JK |
71 | /* for pic/irq_info */ |
72 | #if defined(TARGET_SPARC) | |
73 | #include "hw/sun4m.h" | |
74 | #endif | |
75 | #include "hw/lm32_pic.h" | |
76 | ||
9dc39cba | 77 | //#define DEBUG |
81d0912d | 78 | //#define DEBUG_COMPLETION |
9dc39cba | 79 | |
9307c4c1 FB |
80 | /* |
81 | * Supported types: | |
5fafdf24 | 82 | * |
9307c4c1 | 83 | * 'F' filename |
81d0912d | 84 | * 'B' block device name |
9307c4c1 | 85 | * 's' string (accept optional quote) |
361127df MA |
86 | * 'O' option string of the form NAME=VALUE,... |
87 | * parsed according to QemuOptsList given by its name | |
88 | * Example: 'device:O' uses qemu_device_opts. | |
89 | * Restriction: only lists with empty desc are supported | |
90 | * TODO lift the restriction | |
92a31b1f FB |
91 | * 'i' 32 bit integer |
92 | * 'l' target long (32 or 64 bit) | |
91162849 LC |
93 | * 'M' Non-negative target long (32 or 64 bit), in user mode the |
94 | * value is multiplied by 2^20 (think Mebibyte) | |
dbc0c67f JS |
95 | * 'o' octets (aka bytes) |
96 | * user mode accepts an optional T, t, G, g, M, m, K, k | |
97 | * suffix, which multiplies the value by 2^40 for | |
98 | * suffixes T and t, 2^30 for suffixes G and g, 2^20 for | |
99 | * M and m, 2^10 for K and k | |
fccfb11e MA |
100 | * 'T' double |
101 | * user mode accepts an optional ms, us, ns suffix, | |
102 | * which divides the value by 1e3, 1e6, 1e9, respectively | |
9307c4c1 FB |
103 | * '/' optional gdb-like print format (like "/10x") |
104 | * | |
fb46660e LC |
105 | * '?' optional type (for all types, except '/') |
106 | * '.' other form of optional type (for 'i' and 'l') | |
942cd1f2 MA |
107 | * 'b' boolean |
108 | * user mode accepts "on" or "off" | |
fb46660e | 109 | * '-' optional parameter (eg. '-f') |
9307c4c1 FB |
110 | * |
111 | */ | |
112 | ||
940cc30d AL |
113 | typedef struct MonitorCompletionData MonitorCompletionData; |
114 | struct MonitorCompletionData { | |
115 | Monitor *mon; | |
116 | void (*user_print)(Monitor *mon, const QObject *data); | |
117 | }; | |
118 | ||
c227f099 | 119 | typedef struct mon_cmd_t { |
9dc39cba | 120 | const char *name; |
9307c4c1 | 121 | const char *args_type; |
9dc39cba FB |
122 | const char *params; |
123 | const char *help; | |
a2876f59 | 124 | void (*user_print)(Monitor *mon, const QObject *data); |
910df89d LC |
125 | union { |
126 | void (*info)(Monitor *mon); | |
af4ce882 | 127 | void (*cmd)(Monitor *mon, const QDict *qdict); |
261394db | 128 | int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data); |
940cc30d AL |
129 | int (*cmd_async)(Monitor *mon, const QDict *params, |
130 | MonitorCompletion *cb, void *opaque); | |
910df89d | 131 | } mhandler; |
8ac470c1 | 132 | int flags; |
c227f099 | 133 | } mon_cmd_t; |
9dc39cba | 134 | |
f07918fd | 135 | /* file descriptors passed via SCM_RIGHTS */ |
c227f099 AL |
136 | typedef struct mon_fd_t mon_fd_t; |
137 | struct mon_fd_t { | |
f07918fd MM |
138 | char *name; |
139 | int fd; | |
c227f099 | 140 | QLIST_ENTRY(mon_fd_t) next; |
f07918fd MM |
141 | }; |
142 | ||
ba1c048a CB |
143 | /* file descriptor associated with a file descriptor set */ |
144 | typedef struct MonFdsetFd MonFdsetFd; | |
145 | struct MonFdsetFd { | |
146 | int fd; | |
147 | bool removed; | |
148 | char *opaque; | |
149 | QLIST_ENTRY(MonFdsetFd) next; | |
150 | }; | |
151 | ||
152 | /* file descriptor set containing fds passed via SCM_RIGHTS */ | |
153 | typedef struct MonFdset MonFdset; | |
154 | struct MonFdset { | |
155 | int64_t id; | |
156 | QLIST_HEAD(, MonFdsetFd) fds; | |
adb696f3 | 157 | QLIST_HEAD(, MonFdsetFd) dup_fds; |
ba1c048a CB |
158 | QLIST_ENTRY(MonFdset) next; |
159 | }; | |
160 | ||
5fa737a4 LC |
161 | typedef struct MonitorControl { |
162 | QObject *id; | |
163 | JSONMessageParser parser; | |
4a7e1190 | 164 | int command_mode; |
5fa737a4 LC |
165 | } MonitorControl; |
166 | ||
afeecec2 DB |
167 | /* |
168 | * To prevent flooding clients, events can be throttled. The | |
169 | * throttling is calculated globally, rather than per-Monitor | |
170 | * instance. | |
171 | */ | |
172 | typedef struct MonitorEventState { | |
173 | MonitorEvent event; /* Event being tracked */ | |
174 | int64_t rate; /* Period over which to throttle. 0 to disable */ | |
175 | int64_t last; /* Time at which event was last emitted */ | |
176 | QEMUTimer *timer; /* Timer for handling delayed events */ | |
177 | QObject *data; /* Event pending delayed dispatch */ | |
178 | } MonitorEventState; | |
179 | ||
87127161 AL |
180 | struct Monitor { |
181 | CharDriverState *chr; | |
a7aec5da GH |
182 | int mux_out; |
183 | int reset_seen; | |
731b0364 AL |
184 | int flags; |
185 | int suspend_cnt; | |
186 | uint8_t outbuf[1024]; | |
187 | int outbuf_index; | |
188 | ReadLineState *rs; | |
5fa737a4 | 189 | MonitorControl *mc; |
9349b4f9 | 190 | CPUArchState *mon_cpu; |
731b0364 AL |
191 | BlockDriverCompletionFunc *password_completion_cb; |
192 | void *password_opaque; | |
8204a918 | 193 | QError *error; |
c227f099 | 194 | QLIST_HEAD(,mon_fd_t) fds; |
72cf2d4f | 195 | QLIST_ENTRY(Monitor) entry; |
87127161 AL |
196 | }; |
197 | ||
2dbc8db0 LC |
198 | /* QMP checker flags */ |
199 | #define QMP_ACCEPT_UNKNOWNS 1 | |
200 | ||
72cf2d4f | 201 | static QLIST_HEAD(mon_list, Monitor) mon_list; |
ba1c048a | 202 | static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; |
efb87c16 | 203 | static int mon_refcount; |
7e2515e8 | 204 | |
816f8925 WX |
205 | static mon_cmd_t mon_cmds[]; |
206 | static mon_cmd_t info_cmds[]; | |
9dc39cba | 207 | |
f36b4afb LC |
208 | static const mon_cmd_t qmp_cmds[]; |
209 | ||
8631b608 MA |
210 | Monitor *cur_mon; |
211 | Monitor *default_mon; | |
376253ec | 212 | |
731b0364 AL |
213 | static void monitor_command_cb(Monitor *mon, const char *cmdline, |
214 | void *opaque); | |
83ab7950 | 215 | |
09069b19 LC |
216 | static inline int qmp_cmd_mode(const Monitor *mon) |
217 | { | |
218 | return (mon->mc ? mon->mc->command_mode : 0); | |
219 | } | |
220 | ||
418173c7 LC |
221 | /* Return true if in control mode, false otherwise */ |
222 | static inline int monitor_ctrl_mode(const Monitor *mon) | |
223 | { | |
224 | return (mon->flags & MONITOR_USE_CONTROL); | |
225 | } | |
226 | ||
6620d3ce MA |
227 | /* Return non-zero iff we have a current monitor, and it is in QMP mode. */ |
228 | int monitor_cur_is_qmp(void) | |
229 | { | |
230 | return cur_mon && monitor_ctrl_mode(cur_mon); | |
231 | } | |
232 | ||
7060b478 | 233 | void monitor_read_command(Monitor *mon, int show_prompt) |
731b0364 | 234 | { |
183e6e52 LC |
235 | if (!mon->rs) |
236 | return; | |
237 | ||
731b0364 AL |
238 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); |
239 | if (show_prompt) | |
240 | readline_show_prompt(mon->rs); | |
241 | } | |
6a00d601 | 242 | |
7060b478 AL |
243 | int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
244 | void *opaque) | |
bb5fc20f | 245 | { |
94171e11 | 246 | if (monitor_ctrl_mode(mon)) { |
ab5b027e | 247 | qerror_report(QERR_MISSING_PARAMETER, "password"); |
94171e11 LC |
248 | return -EINVAL; |
249 | } else if (mon->rs) { | |
cde76ee1 AL |
250 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); |
251 | /* prompt is printed on return from the command handler */ | |
252 | return 0; | |
253 | } else { | |
254 | monitor_printf(mon, "terminal does not support password prompting\n"); | |
255 | return -ENOTTY; | |
256 | } | |
bb5fc20f AL |
257 | } |
258 | ||
376253ec | 259 | void monitor_flush(Monitor *mon) |
7e2515e8 | 260 | { |
a7aec5da | 261 | if (mon && mon->outbuf_index != 0 && !mon->mux_out) { |
2cc6e0a1 | 262 | qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index); |
731b0364 | 263 | mon->outbuf_index = 0; |
7e2515e8 FB |
264 | } |
265 | } | |
266 | ||
267 | /* flush at every end of line or if the buffer is full */ | |
376253ec | 268 | static void monitor_puts(Monitor *mon, const char *str) |
7e2515e8 | 269 | { |
60fe76f3 | 270 | char c; |
731b0364 | 271 | |
7e2515e8 FB |
272 | for(;;) { |
273 | c = *str++; | |
274 | if (c == '\0') | |
275 | break; | |
7ba1260a | 276 | if (c == '\n') |
731b0364 AL |
277 | mon->outbuf[mon->outbuf_index++] = '\r'; |
278 | mon->outbuf[mon->outbuf_index++] = c; | |
279 | if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1) | |
280 | || c == '\n') | |
376253ec | 281 | monitor_flush(mon); |
7e2515e8 FB |
282 | } |
283 | } | |
284 | ||
376253ec | 285 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
9dc39cba | 286 | { |
b8b08266 LC |
287 | char buf[4096]; |
288 | ||
2daa1191 LC |
289 | if (!mon) |
290 | return; | |
291 | ||
b8b08266 | 292 | if (monitor_ctrl_mode(mon)) { |
b8b08266 | 293 | return; |
4a29a85d | 294 | } |
b8b08266 LC |
295 | |
296 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
297 | monitor_puts(mon, buf); | |
9dc39cba FB |
298 | } |
299 | ||
376253ec | 300 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
9dc39cba | 301 | { |
7e2515e8 FB |
302 | va_list ap; |
303 | va_start(ap, fmt); | |
376253ec | 304 | monitor_vprintf(mon, fmt, ap); |
7e2515e8 | 305 | va_end(ap); |
9dc39cba FB |
306 | } |
307 | ||
376253ec | 308 | void monitor_print_filename(Monitor *mon, const char *filename) |
fef30743 TS |
309 | { |
310 | int i; | |
311 | ||
312 | for (i = 0; filename[i]; i++) { | |
28a76be8 AL |
313 | switch (filename[i]) { |
314 | case ' ': | |
315 | case '"': | |
316 | case '\\': | |
317 | monitor_printf(mon, "\\%c", filename[i]); | |
318 | break; | |
319 | case '\t': | |
320 | monitor_printf(mon, "\\t"); | |
321 | break; | |
322 | case '\r': | |
323 | monitor_printf(mon, "\\r"); | |
324 | break; | |
325 | case '\n': | |
326 | monitor_printf(mon, "\\n"); | |
327 | break; | |
328 | default: | |
329 | monitor_printf(mon, "%c", filename[i]); | |
330 | break; | |
331 | } | |
fef30743 TS |
332 | } |
333 | } | |
334 | ||
8b7968f7 SW |
335 | static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream, |
336 | const char *fmt, ...) | |
7fe48483 FB |
337 | { |
338 | va_list ap; | |
339 | va_start(ap, fmt); | |
376253ec | 340 | monitor_vprintf((Monitor *)stream, fmt, ap); |
7fe48483 FB |
341 | va_end(ap); |
342 | return 0; | |
343 | } | |
344 | ||
13c7425e LC |
345 | static void monitor_user_noop(Monitor *mon, const QObject *data) { } |
346 | ||
9e80721e | 347 | static inline int handler_is_qobject(const mon_cmd_t *cmd) |
13917bee LC |
348 | { |
349 | return cmd->user_print != NULL; | |
350 | } | |
351 | ||
4903de0c | 352 | static inline bool handler_is_async(const mon_cmd_t *cmd) |
940cc30d | 353 | { |
8ac470c1 | 354 | return cmd->flags & MONITOR_CMD_ASYNC; |
940cc30d AL |
355 | } |
356 | ||
8204a918 LC |
357 | static inline int monitor_has_error(const Monitor *mon) |
358 | { | |
359 | return mon->error != NULL; | |
360 | } | |
361 | ||
9b57c02e LC |
362 | static void monitor_json_emitter(Monitor *mon, const QObject *data) |
363 | { | |
364 | QString *json; | |
365 | ||
83a27d4d LC |
366 | json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) : |
367 | qobject_to_json(data); | |
9b57c02e LC |
368 | assert(json != NULL); |
369 | ||
b8b08266 LC |
370 | qstring_append_chr(json, '\n'); |
371 | monitor_puts(mon, qstring_get_str(json)); | |
4a29a85d | 372 | |
9b57c02e LC |
373 | QDECREF(json); |
374 | } | |
375 | ||
de253f14 LC |
376 | static QDict *build_qmp_error_dict(const QError *err) |
377 | { | |
378 | QObject *obj; | |
379 | ||
380 | obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }", | |
381 | ErrorClass_lookup[err->err_class], | |
382 | qerror_human(err)); | |
383 | ||
384 | return qobject_to_qdict(obj); | |
385 | } | |
386 | ||
25b422eb LC |
387 | static void monitor_protocol_emitter(Monitor *mon, QObject *data) |
388 | { | |
389 | QDict *qmp; | |
390 | ||
89bd820a SH |
391 | trace_monitor_protocol_emitter(mon); |
392 | ||
25b422eb LC |
393 | if (!monitor_has_error(mon)) { |
394 | /* success response */ | |
de253f14 | 395 | qmp = qdict_new(); |
25b422eb LC |
396 | if (data) { |
397 | qobject_incref(data); | |
398 | qdict_put_obj(qmp, "return", data); | |
399 | } else { | |
0abc6579 LC |
400 | /* return an empty QDict by default */ |
401 | qdict_put(qmp, "return", qdict_new()); | |
25b422eb LC |
402 | } |
403 | } else { | |
404 | /* error response */ | |
de253f14 | 405 | qmp = build_qmp_error_dict(mon->error); |
25b422eb LC |
406 | QDECREF(mon->error); |
407 | mon->error = NULL; | |
408 | } | |
409 | ||
5fa737a4 LC |
410 | if (mon->mc->id) { |
411 | qdict_put_obj(qmp, "id", mon->mc->id); | |
412 | mon->mc->id = NULL; | |
413 | } | |
414 | ||
25b422eb LC |
415 | monitor_json_emitter(mon, QOBJECT(qmp)); |
416 | QDECREF(qmp); | |
417 | } | |
418 | ||
0d1ea871 LC |
419 | static void timestamp_put(QDict *qdict) |
420 | { | |
421 | int err; | |
422 | QObject *obj; | |
d08d6f04 | 423 | qemu_timeval tv; |
0d1ea871 | 424 | |
d08d6f04 | 425 | err = qemu_gettimeofday(&tv); |
0d1ea871 LC |
426 | if (err < 0) |
427 | return; | |
428 | ||
429 | obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", " | |
430 | "'microseconds': %" PRId64 " }", | |
431 | (int64_t) tv.tv_sec, (int64_t) tv.tv_usec); | |
0d1ea871 LC |
432 | qdict_put_obj(qdict, "timestamp", obj); |
433 | } | |
434 | ||
4860853d DB |
435 | |
436 | static const char *monitor_event_names[] = { | |
437 | [QEVENT_SHUTDOWN] = "SHUTDOWN", | |
438 | [QEVENT_RESET] = "RESET", | |
439 | [QEVENT_POWERDOWN] = "POWERDOWN", | |
440 | [QEVENT_STOP] = "STOP", | |
441 | [QEVENT_RESUME] = "RESUME", | |
442 | [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED", | |
443 | [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED", | |
444 | [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED", | |
445 | [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR", | |
446 | [QEVENT_RTC_CHANGE] = "RTC_CHANGE", | |
447 | [QEVENT_WATCHDOG] = "WATCHDOG", | |
448 | [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED", | |
449 | [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED", | |
450 | [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED", | |
451 | [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED", | |
452 | [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED", | |
453 | [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED", | |
454 | [QEVENT_SUSPEND] = "SUSPEND", | |
25df49f6 | 455 | [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK", |
4860853d | 456 | [QEVENT_WAKEUP] = "WAKEUP", |
973603a8 | 457 | [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE", |
4860853d DB |
458 | }; |
459 | QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX) | |
460 | ||
afeecec2 DB |
461 | MonitorEventState monitor_event_state[QEVENT_MAX]; |
462 | QemuMutex monitor_event_state_lock; | |
463 | ||
464 | /* | |
465 | * Emits the event to every monitor instance | |
466 | */ | |
467 | static void | |
468 | monitor_protocol_event_emit(MonitorEvent event, | |
469 | QObject *data) | |
470 | { | |
471 | Monitor *mon; | |
472 | ||
473 | trace_monitor_protocol_event_emit(event, data); | |
474 | QLIST_FOREACH(mon, &mon_list, entry) { | |
475 | if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) { | |
476 | monitor_json_emitter(mon, data); | |
477 | } | |
478 | } | |
479 | } | |
480 | ||
481 | ||
482 | /* | |
483 | * Queue a new event for emission to Monitor instances, | |
484 | * applying any rate limiting if required. | |
485 | */ | |
486 | static void | |
487 | monitor_protocol_event_queue(MonitorEvent event, | |
488 | QObject *data) | |
489 | { | |
490 | MonitorEventState *evstate; | |
491 | int64_t now = qemu_get_clock_ns(rt_clock); | |
492 | assert(event < QEVENT_MAX); | |
493 | ||
494 | qemu_mutex_lock(&monitor_event_state_lock); | |
495 | evstate = &(monitor_event_state[event]); | |
496 | trace_monitor_protocol_event_queue(event, | |
497 | data, | |
498 | evstate->rate, | |
499 | evstate->last, | |
500 | now); | |
501 | ||
502 | /* Rate limit of 0 indicates no throttling */ | |
503 | if (!evstate->rate) { | |
504 | monitor_protocol_event_emit(event, data); | |
505 | evstate->last = now; | |
506 | } else { | |
507 | int64_t delta = now - evstate->last; | |
508 | if (evstate->data || | |
509 | delta < evstate->rate) { | |
510 | /* If there's an existing event pending, replace | |
511 | * it with the new event, otherwise schedule a | |
512 | * timer for delayed emission | |
513 | */ | |
514 | if (evstate->data) { | |
515 | qobject_decref(evstate->data); | |
516 | } else { | |
517 | int64_t then = evstate->last + evstate->rate; | |
518 | qemu_mod_timer_ns(evstate->timer, then); | |
519 | } | |
520 | evstate->data = data; | |
521 | qobject_incref(evstate->data); | |
522 | } else { | |
523 | monitor_protocol_event_emit(event, data); | |
524 | evstate->last = now; | |
525 | } | |
526 | } | |
527 | qemu_mutex_unlock(&monitor_event_state_lock); | |
528 | } | |
529 | ||
530 | ||
531 | /* | |
532 | * The callback invoked by QemuTimer when a delayed | |
533 | * event is ready to be emitted | |
534 | */ | |
535 | static void monitor_protocol_event_handler(void *opaque) | |
536 | { | |
537 | MonitorEventState *evstate = opaque; | |
538 | int64_t now = qemu_get_clock_ns(rt_clock); | |
539 | ||
540 | qemu_mutex_lock(&monitor_event_state_lock); | |
541 | ||
542 | trace_monitor_protocol_event_handler(evstate->event, | |
543 | evstate->data, | |
544 | evstate->last, | |
545 | now); | |
546 | if (evstate->data) { | |
547 | monitor_protocol_event_emit(evstate->event, evstate->data); | |
548 | qobject_decref(evstate->data); | |
549 | evstate->data = NULL; | |
550 | } | |
551 | evstate->last = now; | |
552 | qemu_mutex_unlock(&monitor_event_state_lock); | |
553 | } | |
554 | ||
555 | ||
556 | /* | |
557 | * @event: the event ID to be limited | |
558 | * @rate: the rate limit in milliseconds | |
559 | * | |
560 | * Sets a rate limit on a particular event, so no | |
561 | * more than 1 event will be emitted within @rate | |
562 | * milliseconds | |
563 | */ | |
564 | static void | |
565 | monitor_protocol_event_throttle(MonitorEvent event, | |
566 | int64_t rate) | |
567 | { | |
568 | MonitorEventState *evstate; | |
569 | assert(event < QEVENT_MAX); | |
570 | ||
571 | evstate = &(monitor_event_state[event]); | |
572 | ||
573 | trace_monitor_protocol_event_throttle(event, rate); | |
574 | evstate->event = event; | |
575 | evstate->rate = rate * SCALE_MS; | |
576 | evstate->timer = qemu_new_timer(rt_clock, | |
577 | SCALE_MS, | |
578 | monitor_protocol_event_handler, | |
579 | evstate); | |
580 | evstate->last = 0; | |
581 | evstate->data = NULL; | |
582 | } | |
583 | ||
584 | ||
585 | /* Global, one-time initializer to configure the rate limiting | |
586 | * and initialize state */ | |
587 | static void monitor_protocol_event_init(void) | |
588 | { | |
589 | qemu_mutex_init(&monitor_event_state_lock); | |
590 | /* Limit RTC & BALLOON events to 1 per second */ | |
591 | monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000); | |
592 | monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000); | |
593 | monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000); | |
594 | } | |
595 | ||
0d1ea871 LC |
596 | /** |
597 | * monitor_protocol_event(): Generate a Monitor event | |
598 | * | |
599 | * Event-specific data can be emitted through the (optional) 'data' parameter. | |
600 | */ | |
601 | void monitor_protocol_event(MonitorEvent event, QObject *data) | |
602 | { | |
603 | QDict *qmp; | |
604 | const char *event_name; | |
0d1ea871 | 605 | |
242cd003 | 606 | assert(event < QEVENT_MAX); |
0d1ea871 | 607 | |
4860853d DB |
608 | event_name = monitor_event_names[event]; |
609 | assert(event_name != NULL); | |
0d1ea871 LC |
610 | |
611 | qmp = qdict_new(); | |
612 | timestamp_put(qmp); | |
613 | qdict_put(qmp, "event", qstring_from_str(event_name)); | |
3d72f9a2 LC |
614 | if (data) { |
615 | qobject_incref(data); | |
0d1ea871 | 616 | qdict_put_obj(qmp, "data", data); |
3d72f9a2 | 617 | } |
0d1ea871 | 618 | |
afeecec2 DB |
619 | trace_monitor_protocol_event(event, event_name, qmp); |
620 | monitor_protocol_event_queue(event, QOBJECT(qmp)); | |
0d1ea871 LC |
621 | QDECREF(qmp); |
622 | } | |
623 | ||
ef4b7eee LC |
624 | static int do_qmp_capabilities(Monitor *mon, const QDict *params, |
625 | QObject **ret_data) | |
4a7e1190 LC |
626 | { |
627 | /* Will setup QMP capabilities in the future */ | |
628 | if (monitor_ctrl_mode(mon)) { | |
629 | mon->mc->command_mode = 1; | |
630 | } | |
ef4b7eee LC |
631 | |
632 | return 0; | |
4a7e1190 LC |
633 | } |
634 | ||
0268d97c LC |
635 | static void handle_user_command(Monitor *mon, const char *cmdline); |
636 | ||
d51a67b4 LC |
637 | char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, |
638 | int64_t cpu_index, Error **errp) | |
0268d97c | 639 | { |
d51a67b4 | 640 | char *output = NULL; |
0268d97c LC |
641 | Monitor *old_mon, hmp; |
642 | CharDriverState mchar; | |
643 | ||
644 | memset(&hmp, 0, sizeof(hmp)); | |
645 | qemu_chr_init_mem(&mchar); | |
646 | hmp.chr = &mchar; | |
647 | ||
648 | old_mon = cur_mon; | |
649 | cur_mon = &hmp; | |
650 | ||
d51a67b4 LC |
651 | if (has_cpu_index) { |
652 | int ret = monitor_set_cpu(cpu_index); | |
0268d97c LC |
653 | if (ret < 0) { |
654 | cur_mon = old_mon; | |
d51a67b4 LC |
655 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
656 | "a CPU number"); | |
0268d97c LC |
657 | goto out; |
658 | } | |
659 | } | |
660 | ||
d51a67b4 | 661 | handle_user_command(&hmp, command_line); |
0268d97c LC |
662 | cur_mon = old_mon; |
663 | ||
664 | if (qemu_chr_mem_osize(hmp.chr) > 0) { | |
d51a67b4 LC |
665 | QString *str = qemu_chr_mem_to_qs(hmp.chr); |
666 | output = g_strdup(qstring_get_str(str)); | |
667 | QDECREF(str); | |
668 | } else { | |
669 | output = g_strdup(""); | |
0268d97c LC |
670 | } |
671 | ||
672 | out: | |
673 | qemu_chr_close_mem(hmp.chr); | |
d51a67b4 | 674 | return output; |
0268d97c LC |
675 | } |
676 | ||
9dc39cba FB |
677 | static int compare_cmd(const char *name, const char *list) |
678 | { | |
679 | const char *p, *pstart; | |
680 | int len; | |
681 | len = strlen(name); | |
682 | p = list; | |
683 | for(;;) { | |
684 | pstart = p; | |
685 | p = strchr(p, '|'); | |
686 | if (!p) | |
687 | p = pstart + strlen(pstart); | |
688 | if ((p - pstart) == len && !memcmp(pstart, name, len)) | |
689 | return 1; | |
690 | if (*p == '\0') | |
691 | break; | |
692 | p++; | |
693 | } | |
694 | return 0; | |
695 | } | |
696 | ||
c227f099 | 697 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
376253ec | 698 | const char *prefix, const char *name) |
9dc39cba | 699 | { |
c227f099 | 700 | const mon_cmd_t *cmd; |
9dc39cba FB |
701 | |
702 | for(cmd = cmds; cmd->name != NULL; cmd++) { | |
703 | if (!name || !strcmp(name, cmd->name)) | |
376253ec AL |
704 | monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name, |
705 | cmd->params, cmd->help); | |
9dc39cba FB |
706 | } |
707 | } | |
708 | ||
376253ec | 709 | static void help_cmd(Monitor *mon, const char *name) |
9dc39cba FB |
710 | { |
711 | if (name && !strcmp(name, "info")) { | |
376253ec | 712 | help_cmd_dump(mon, info_cmds, "info ", NULL); |
9dc39cba | 713 | } else { |
376253ec | 714 | help_cmd_dump(mon, mon_cmds, "", name); |
f193c797 | 715 | if (name && !strcmp(name, "log")) { |
8662d656 | 716 | const CPULogItem *item; |
376253ec AL |
717 | monitor_printf(mon, "Log items (comma separated):\n"); |
718 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); | |
f193c797 | 719 | for(item = cpu_log_items; item->mask != 0; item++) { |
376253ec | 720 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
f193c797 FB |
721 | } |
722 | } | |
9dc39cba FB |
723 | } |
724 | } | |
725 | ||
d54908a5 | 726 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
38183186 | 727 | { |
d54908a5 | 728 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
38183186 LC |
729 | } |
730 | ||
fc764105 | 731 | static void do_trace_event_set_state(Monitor *mon, const QDict *qdict) |
22890ab5 PS |
732 | { |
733 | const char *tp_name = qdict_get_str(qdict, "name"); | |
734 | bool new_state = qdict_get_bool(qdict, "option"); | |
fc764105 | 735 | int ret = trace_event_set_state(tp_name, new_state); |
f871d689 BS |
736 | |
737 | if (!ret) { | |
738 | monitor_printf(mon, "unknown event name \"%s\"\n", tp_name); | |
739 | } | |
22890ab5 | 740 | } |
c5ceb523 | 741 | |
c45a8168 | 742 | #ifdef CONFIG_TRACE_SIMPLE |
c5ceb523 SH |
743 | static void do_trace_file(Monitor *mon, const QDict *qdict) |
744 | { | |
745 | const char *op = qdict_get_try_str(qdict, "op"); | |
746 | const char *arg = qdict_get_try_str(qdict, "arg"); | |
747 | ||
748 | if (!op) { | |
749 | st_print_trace_file_status((FILE *)mon, &monitor_fprintf); | |
750 | } else if (!strcmp(op, "on")) { | |
751 | st_set_trace_file_enabled(true); | |
752 | } else if (!strcmp(op, "off")) { | |
753 | st_set_trace_file_enabled(false); | |
754 | } else if (!strcmp(op, "flush")) { | |
755 | st_flush_trace_buffer(); | |
756 | } else if (!strcmp(op, "set")) { | |
757 | if (arg) { | |
758 | st_set_trace_file(arg); | |
759 | } | |
760 | } else { | |
761 | monitor_printf(mon, "unexpected argument \"%s\"\n", op); | |
762 | help_cmd(mon, "trace-file"); | |
763 | } | |
764 | } | |
22890ab5 PS |
765 | #endif |
766 | ||
940cc30d AL |
767 | static void user_monitor_complete(void *opaque, QObject *ret_data) |
768 | { | |
769 | MonitorCompletionData *data = (MonitorCompletionData *)opaque; | |
770 | ||
771 | if (ret_data) { | |
772 | data->user_print(data->mon, ret_data); | |
773 | } | |
774 | monitor_resume(data->mon); | |
7267c094 | 775 | g_free(data); |
940cc30d AL |
776 | } |
777 | ||
778 | static void qmp_monitor_complete(void *opaque, QObject *ret_data) | |
779 | { | |
780 | monitor_protocol_emitter(opaque, ret_data); | |
781 | } | |
782 | ||
5af7bbae LC |
783 | static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd, |
784 | const QDict *params) | |
940cc30d | 785 | { |
5af7bbae | 786 | return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon); |
940cc30d AL |
787 | } |
788 | ||
940cc30d AL |
789 | static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd, |
790 | const QDict *params) | |
791 | { | |
792 | int ret; | |
793 | ||
7267c094 | 794 | MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data)); |
940cc30d AL |
795 | cb_data->mon = mon; |
796 | cb_data->user_print = cmd->user_print; | |
797 | monitor_suspend(mon); | |
798 | ret = cmd->mhandler.cmd_async(mon, params, | |
799 | user_monitor_complete, cb_data); | |
800 | if (ret < 0) { | |
801 | monitor_resume(mon); | |
7267c094 | 802 | g_free(cb_data); |
940cc30d AL |
803 | } |
804 | } | |
805 | ||
1162daa6 | 806 | static void do_info(Monitor *mon, const QDict *qdict) |
9dc39cba | 807 | { |
c227f099 | 808 | const mon_cmd_t *cmd; |
d54908a5 | 809 | const char *item = qdict_get_try_str(qdict, "item"); |
9dc39cba | 810 | |
956f1a0d | 811 | if (!item) { |
9dc39cba | 812 | goto help; |
956f1a0d | 813 | } |
13c7425e LC |
814 | |
815 | for (cmd = info_cmds; cmd->name != NULL; cmd++) { | |
5fafdf24 | 816 | if (compare_cmd(item, cmd->name)) |
13c7425e | 817 | break; |
9dc39cba | 818 | } |
13c7425e | 819 | |
956f1a0d | 820 | if (cmd->name == NULL) { |
13c7425e | 821 | goto help; |
956f1a0d | 822 | } |
13c7425e | 823 | |
b5c30586 | 824 | cmd->mhandler.info(mon); |
1162daa6 | 825 | return; |
13c7425e LC |
826 | |
827 | help: | |
828 | help_cmd(mon, "info"); | |
9dc39cba FB |
829 | } |
830 | ||
aa9b79bc | 831 | CommandInfoList *qmp_query_commands(Error **errp) |
e3bba9d0 | 832 | { |
aa9b79bc | 833 | CommandInfoList *info, *cmd_list = NULL; |
e3bba9d0 LC |
834 | const mon_cmd_t *cmd; |
835 | ||
f36b4afb | 836 | for (cmd = qmp_cmds; cmd->name != NULL; cmd++) { |
40e5a01d LC |
837 | info = g_malloc0(sizeof(*info)); |
838 | info->value = g_malloc0(sizeof(*info->value)); | |
839 | info->value->name = g_strdup(cmd->name); | |
e3bba9d0 | 840 | |
aa9b79bc LC |
841 | info->next = cmd_list; |
842 | cmd_list = info; | |
e3bba9d0 LC |
843 | } |
844 | ||
aa9b79bc | 845 | return cmd_list; |
a36e69dd TS |
846 | } |
847 | ||
4860853d DB |
848 | EventInfoList *qmp_query_events(Error **errp) |
849 | { | |
850 | EventInfoList *info, *ev_list = NULL; | |
851 | MonitorEvent e; | |
852 | ||
853 | for (e = 0 ; e < QEVENT_MAX ; e++) { | |
854 | const char *event_name = monitor_event_names[e]; | |
855 | assert(event_name != NULL); | |
856 | info = g_malloc0(sizeof(*info)); | |
857 | info->value = g_malloc0(sizeof(*info->value)); | |
858 | info->value->name = g_strdup(event_name); | |
859 | ||
860 | info->next = ev_list; | |
861 | ev_list = info; | |
862 | } | |
863 | ||
864 | return ev_list; | |
865 | } | |
866 | ||
b025c8b4 LC |
867 | /* set the current CPU defined by the user */ |
868 | int monitor_set_cpu(int cpu_index) | |
6a00d601 | 869 | { |
9349b4f9 | 870 | CPUArchState *env; |
6a00d601 FB |
871 | |
872 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
873 | if (env->cpu_index == cpu_index) { | |
731b0364 | 874 | cur_mon->mon_cpu = env; |
6a00d601 FB |
875 | return 0; |
876 | } | |
877 | } | |
878 | return -1; | |
879 | } | |
880 | ||
9349b4f9 | 881 | static CPUArchState *mon_get_cpu(void) |
6a00d601 | 882 | { |
731b0364 | 883 | if (!cur_mon->mon_cpu) { |
b025c8b4 | 884 | monitor_set_cpu(0); |
6a00d601 | 885 | } |
4c0960c0 | 886 | cpu_synchronize_state(cur_mon->mon_cpu); |
731b0364 | 887 | return cur_mon->mon_cpu; |
6a00d601 FB |
888 | } |
889 | ||
99b7796f LC |
890 | int monitor_get_cpu_index(void) |
891 | { | |
892 | return mon_get_cpu()->cpu_index; | |
893 | } | |
894 | ||
376253ec | 895 | static void do_info_registers(Monitor *mon) |
9307c4c1 | 896 | { |
9349b4f9 | 897 | CPUArchState *env; |
6a00d601 | 898 | env = mon_get_cpu(); |
9307c4c1 | 899 | #ifdef TARGET_I386 |
376253ec | 900 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
d24b15a8 | 901 | X86_DUMP_FPU); |
9307c4c1 | 902 | #else |
376253ec | 903 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
7fe48483 | 904 | 0); |
9307c4c1 FB |
905 | #endif |
906 | } | |
907 | ||
376253ec | 908 | static void do_info_jit(Monitor *mon) |
e3db7226 | 909 | { |
376253ec | 910 | dump_exec_info((FILE *)mon, monitor_fprintf); |
e3db7226 FB |
911 | } |
912 | ||
376253ec | 913 | static void do_info_history(Monitor *mon) |
aa455485 FB |
914 | { |
915 | int i; | |
7e2515e8 | 916 | const char *str; |
3b46e624 | 917 | |
cde76ee1 AL |
918 | if (!mon->rs) |
919 | return; | |
7e2515e8 FB |
920 | i = 0; |
921 | for(;;) { | |
731b0364 | 922 | str = readline_get_history(mon->rs, i); |
7e2515e8 FB |
923 | if (!str) |
924 | break; | |
376253ec | 925 | monitor_printf(mon, "%d: '%s'\n", i, str); |
8e3a9fd2 | 926 | i++; |
aa455485 FB |
927 | } |
928 | } | |
929 | ||
76a66253 JM |
930 | #if defined(TARGET_PPC) |
931 | /* XXX: not implemented in other targets */ | |
376253ec | 932 | static void do_info_cpu_stats(Monitor *mon) |
76a66253 | 933 | { |
9349b4f9 | 934 | CPUArchState *env; |
76a66253 JM |
935 | |
936 | env = mon_get_cpu(); | |
376253ec | 937 | cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0); |
76a66253 JM |
938 | } |
939 | #endif | |
940 | ||
fc764105 | 941 | static void do_trace_print_events(Monitor *mon) |
22890ab5 | 942 | { |
fc764105 | 943 | trace_print_events((FILE *)mon, &monitor_fprintf); |
22890ab5 | 944 | } |
22890ab5 | 945 | |
13661089 DB |
946 | static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data) |
947 | { | |
948 | const char *protocol = qdict_get_str(qdict, "protocol"); | |
949 | const char *fdname = qdict_get_str(qdict, "fdname"); | |
13661089 DB |
950 | CharDriverState *s; |
951 | ||
952 | if (strcmp(protocol, "spice") == 0) { | |
f1f5f407 DB |
953 | int fd = monitor_get_fd(mon, fdname); |
954 | int skipauth = qdict_get_try_bool(qdict, "skipauth", 0); | |
955 | int tls = qdict_get_try_bool(qdict, "tls", 0); | |
13661089 DB |
956 | if (!using_spice) { |
957 | /* correct one? spice isn't a device ,,, */ | |
958 | qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice"); | |
959 | return -1; | |
960 | } | |
f1f5f407 DB |
961 | if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) { |
962 | close(fd); | |
963 | } | |
964 | return 0; | |
c62f6d1d | 965 | #ifdef CONFIG_VNC |
13661089 DB |
966 | } else if (strcmp(protocol, "vnc") == 0) { |
967 | int fd = monitor_get_fd(mon, fdname); | |
860341f6 | 968 | int skipauth = qdict_get_try_bool(qdict, "skipauth", 0); |
13661089 DB |
969 | vnc_display_add_client(NULL, fd, skipauth); |
970 | return 0; | |
c62f6d1d | 971 | #endif |
13661089 DB |
972 | } else if ((s = qemu_chr_find(protocol)) != NULL) { |
973 | int fd = monitor_get_fd(mon, fdname); | |
974 | if (qemu_chr_add_client(s, fd) < 0) { | |
975 | qerror_report(QERR_ADD_CLIENT_FAILED); | |
976 | return -1; | |
977 | } | |
978 | return 0; | |
979 | } | |
980 | ||
981 | qerror_report(QERR_INVALID_PARAMETER, "protocol"); | |
982 | return -1; | |
983 | } | |
984 | ||
edc5cb1a YH |
985 | static int client_migrate_info(Monitor *mon, const QDict *qdict, |
986 | MonitorCompletion cb, void *opaque) | |
e866e239 GH |
987 | { |
988 | const char *protocol = qdict_get_str(qdict, "protocol"); | |
989 | const char *hostname = qdict_get_str(qdict, "hostname"); | |
990 | const char *subject = qdict_get_try_str(qdict, "cert-subject"); | |
991 | int port = qdict_get_try_int(qdict, "port", -1); | |
992 | int tls_port = qdict_get_try_int(qdict, "tls-port", -1); | |
993 | int ret; | |
994 | ||
995 | if (strcmp(protocol, "spice") == 0) { | |
996 | if (!using_spice) { | |
997 | qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice"); | |
998 | return -1; | |
999 | } | |
1000 | ||
6ec5dae5 YH |
1001 | if (port == -1 && tls_port == -1) { |
1002 | qerror_report(QERR_MISSING_PARAMETER, "port/tls-port"); | |
1003 | return -1; | |
1004 | } | |
1005 | ||
edc5cb1a YH |
1006 | ret = qemu_spice_migrate_info(hostname, port, tls_port, subject, |
1007 | cb, opaque); | |
e866e239 GH |
1008 | if (ret != 0) { |
1009 | qerror_report(QERR_UNDEFINED_ERROR); | |
1010 | return -1; | |
1011 | } | |
1012 | return 0; | |
1013 | } | |
1014 | ||
1015 | qerror_report(QERR_INVALID_PARAMETER, "protocol"); | |
1016 | return -1; | |
1017 | } | |
1018 | ||
f1dc58e0 | 1019 | static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data) |
59a983b9 | 1020 | { |
d54908a5 | 1021 | vga_hw_screen_dump(qdict_get_str(qdict, "filename")); |
f1dc58e0 | 1022 | return 0; |
59a983b9 FB |
1023 | } |
1024 | ||
d54908a5 | 1025 | static void do_logfile(Monitor *mon, const QDict *qdict) |
e735b91c | 1026 | { |
d54908a5 | 1027 | cpu_set_log_filename(qdict_get_str(qdict, "filename")); |
e735b91c PB |
1028 | } |
1029 | ||
d54908a5 | 1030 | static void do_log(Monitor *mon, const QDict *qdict) |
f193c797 FB |
1031 | { |
1032 | int mask; | |
d54908a5 | 1033 | const char *items = qdict_get_str(qdict, "items"); |
3b46e624 | 1034 | |
9307c4c1 | 1035 | if (!strcmp(items, "none")) { |
f193c797 FB |
1036 | mask = 0; |
1037 | } else { | |
9307c4c1 | 1038 | mask = cpu_str_to_log_mask(items); |
f193c797 | 1039 | if (!mask) { |
376253ec | 1040 | help_cmd(mon, "log"); |
f193c797 FB |
1041 | return; |
1042 | } | |
1043 | } | |
1044 | cpu_set_log(mask); | |
1045 | } | |
1046 | ||
d54908a5 | 1047 | static void do_singlestep(Monitor *mon, const QDict *qdict) |
1b530a6d | 1048 | { |
d54908a5 | 1049 | const char *option = qdict_get_try_str(qdict, "option"); |
1b530a6d AJ |
1050 | if (!option || !strcmp(option, "on")) { |
1051 | singlestep = 1; | |
1052 | } else if (!strcmp(option, "off")) { | |
1053 | singlestep = 0; | |
1054 | } else { | |
1055 | monitor_printf(mon, "unexpected option %s\n", option); | |
1056 | } | |
1057 | } | |
1058 | ||
d54908a5 | 1059 | static void do_gdbserver(Monitor *mon, const QDict *qdict) |
59030a8c | 1060 | { |
d54908a5 | 1061 | const char *device = qdict_get_try_str(qdict, "device"); |
59030a8c AL |
1062 | if (!device) |
1063 | device = "tcp::" DEFAULT_GDBSTUB_PORT; | |
1064 | if (gdbserver_start(device) < 0) { | |
1065 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", | |
1066 | device); | |
1067 | } else if (strcmp(device, "none") == 0) { | |
36556b20 | 1068 | monitor_printf(mon, "Disabled gdbserver\n"); |
8a7ddc38 | 1069 | } else { |
59030a8c AL |
1070 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
1071 | device); | |
8a7ddc38 FB |
1072 | } |
1073 | } | |
1074 | ||
d54908a5 | 1075 | static void do_watchdog_action(Monitor *mon, const QDict *qdict) |
9dd986cc | 1076 | { |
d54908a5 | 1077 | const char *action = qdict_get_str(qdict, "action"); |
9dd986cc RJ |
1078 | if (select_watchdog_action(action) == -1) { |
1079 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); | |
1080 | } | |
1081 | } | |
1082 | ||
376253ec | 1083 | static void monitor_printc(Monitor *mon, int c) |
9307c4c1 | 1084 | { |
376253ec | 1085 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1086 | switch(c) { |
1087 | case '\'': | |
376253ec | 1088 | monitor_printf(mon, "\\'"); |
9307c4c1 FB |
1089 | break; |
1090 | case '\\': | |
376253ec | 1091 | monitor_printf(mon, "\\\\"); |
9307c4c1 FB |
1092 | break; |
1093 | case '\n': | |
376253ec | 1094 | monitor_printf(mon, "\\n"); |
9307c4c1 FB |
1095 | break; |
1096 | case '\r': | |
376253ec | 1097 | monitor_printf(mon, "\\r"); |
9307c4c1 FB |
1098 | break; |
1099 | default: | |
1100 | if (c >= 32 && c <= 126) { | |
376253ec | 1101 | monitor_printf(mon, "%c", c); |
9307c4c1 | 1102 | } else { |
376253ec | 1103 | monitor_printf(mon, "\\x%02x", c); |
9307c4c1 FB |
1104 | } |
1105 | break; | |
1106 | } | |
376253ec | 1107 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1108 | } |
1109 | ||
376253ec | 1110 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
c227f099 | 1111 | target_phys_addr_t addr, int is_physical) |
9307c4c1 | 1112 | { |
9349b4f9 | 1113 | CPUArchState *env; |
23842aab | 1114 | int l, line_size, i, max_digits, len; |
9307c4c1 FB |
1115 | uint8_t buf[16]; |
1116 | uint64_t v; | |
1117 | ||
1118 | if (format == 'i') { | |
1119 | int flags; | |
1120 | flags = 0; | |
6a00d601 | 1121 | env = mon_get_cpu(); |
9307c4c1 | 1122 | #ifdef TARGET_I386 |
4c27ba27 | 1123 | if (wsize == 2) { |
9307c4c1 | 1124 | flags = 1; |
4c27ba27 FB |
1125 | } else if (wsize == 4) { |
1126 | flags = 0; | |
1127 | } else { | |
6a15fd12 | 1128 | /* as default we use the current CS size */ |
4c27ba27 | 1129 | flags = 0; |
6a15fd12 FB |
1130 | if (env) { |
1131 | #ifdef TARGET_X86_64 | |
5fafdf24 | 1132 | if ((env->efer & MSR_EFER_LMA) && |
6a15fd12 FB |
1133 | (env->segs[R_CS].flags & DESC_L_MASK)) |
1134 | flags = 2; | |
1135 | else | |
1136 | #endif | |
1137 | if (!(env->segs[R_CS].flags & DESC_B_MASK)) | |
1138 | flags = 1; | |
1139 | } | |
4c27ba27 FB |
1140 | } |
1141 | #endif | |
376253ec | 1142 | monitor_disas(mon, env, addr, count, is_physical, flags); |
9307c4c1 FB |
1143 | return; |
1144 | } | |
1145 | ||
1146 | len = wsize * count; | |
1147 | if (wsize == 1) | |
1148 | line_size = 8; | |
1149 | else | |
1150 | line_size = 16; | |
9307c4c1 FB |
1151 | max_digits = 0; |
1152 | ||
1153 | switch(format) { | |
1154 | case 'o': | |
1155 | max_digits = (wsize * 8 + 2) / 3; | |
1156 | break; | |
1157 | default: | |
1158 | case 'x': | |
1159 | max_digits = (wsize * 8) / 4; | |
1160 | break; | |
1161 | case 'u': | |
1162 | case 'd': | |
1163 | max_digits = (wsize * 8 * 10 + 32) / 33; | |
1164 | break; | |
1165 | case 'c': | |
1166 | wsize = 1; | |
1167 | break; | |
1168 | } | |
1169 | ||
1170 | while (len > 0) { | |
7743e588 | 1171 | if (is_physical) |
376253ec | 1172 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
7743e588 | 1173 | else |
376253ec | 1174 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
9307c4c1 FB |
1175 | l = len; |
1176 | if (l > line_size) | |
1177 | l = line_size; | |
1178 | if (is_physical) { | |
54f7b4a3 | 1179 | cpu_physical_memory_read(addr, buf, l); |
9307c4c1 | 1180 | } else { |
6a00d601 | 1181 | env = mon_get_cpu(); |
c8f79b67 | 1182 | if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) { |
376253ec | 1183 | monitor_printf(mon, " Cannot access memory\n"); |
c8f79b67 AL |
1184 | break; |
1185 | } | |
9307c4c1 | 1186 | } |
5fafdf24 | 1187 | i = 0; |
9307c4c1 FB |
1188 | while (i < l) { |
1189 | switch(wsize) { | |
1190 | default: | |
1191 | case 1: | |
1192 | v = ldub_raw(buf + i); | |
1193 | break; | |
1194 | case 2: | |
1195 | v = lduw_raw(buf + i); | |
1196 | break; | |
1197 | case 4: | |
92a31b1f | 1198 | v = (uint32_t)ldl_raw(buf + i); |
9307c4c1 FB |
1199 | break; |
1200 | case 8: | |
1201 | v = ldq_raw(buf + i); | |
1202 | break; | |
1203 | } | |
376253ec | 1204 | monitor_printf(mon, " "); |
9307c4c1 FB |
1205 | switch(format) { |
1206 | case 'o': | |
376253ec | 1207 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
9307c4c1 FB |
1208 | break; |
1209 | case 'x': | |
376253ec | 1210 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
9307c4c1 FB |
1211 | break; |
1212 | case 'u': | |
376253ec | 1213 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
9307c4c1 FB |
1214 | break; |
1215 | case 'd': | |
376253ec | 1216 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
9307c4c1 FB |
1217 | break; |
1218 | case 'c': | |
376253ec | 1219 | monitor_printc(mon, v); |
9307c4c1 FB |
1220 | break; |
1221 | } | |
1222 | i += wsize; | |
1223 | } | |
376253ec | 1224 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1225 | addr += l; |
1226 | len -= l; | |
1227 | } | |
1228 | } | |
1229 | ||
1bd1442e | 1230 | static void do_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1231 | { |
1bd1442e LC |
1232 | int count = qdict_get_int(qdict, "count"); |
1233 | int format = qdict_get_int(qdict, "format"); | |
1234 | int size = qdict_get_int(qdict, "size"); | |
1235 | target_long addr = qdict_get_int(qdict, "addr"); | |
1236 | ||
376253ec | 1237 | memory_dump(mon, count, format, size, addr, 0); |
9307c4c1 FB |
1238 | } |
1239 | ||
1bd1442e | 1240 | static void do_physical_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1241 | { |
1bd1442e LC |
1242 | int count = qdict_get_int(qdict, "count"); |
1243 | int format = qdict_get_int(qdict, "format"); | |
1244 | int size = qdict_get_int(qdict, "size"); | |
c227f099 | 1245 | target_phys_addr_t addr = qdict_get_int(qdict, "addr"); |
1bd1442e | 1246 | |
376253ec | 1247 | memory_dump(mon, count, format, size, addr, 1); |
9307c4c1 FB |
1248 | } |
1249 | ||
1bd1442e | 1250 | static void do_print(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1251 | { |
1bd1442e | 1252 | int format = qdict_get_int(qdict, "format"); |
c227f099 | 1253 | target_phys_addr_t val = qdict_get_int(qdict, "val"); |
1bd1442e | 1254 | |
9307c4c1 FB |
1255 | switch(format) { |
1256 | case 'o': | |
66f27e63 | 1257 | monitor_printf(mon, "%#" TARGET_PRIoPHYS, val); |
9307c4c1 FB |
1258 | break; |
1259 | case 'x': | |
66f27e63 | 1260 | monitor_printf(mon, "%#" TARGET_PRIxPHYS, val); |
9307c4c1 FB |
1261 | break; |
1262 | case 'u': | |
66f27e63 | 1263 | monitor_printf(mon, "%" TARGET_PRIuPHYS, val); |
9307c4c1 FB |
1264 | break; |
1265 | default: | |
1266 | case 'd': | |
66f27e63 | 1267 | monitor_printf(mon, "%" TARGET_PRIdPHYS, val); |
9307c4c1 FB |
1268 | break; |
1269 | case 'c': | |
376253ec | 1270 | monitor_printc(mon, val); |
9307c4c1 FB |
1271 | break; |
1272 | } | |
376253ec | 1273 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1274 | } |
1275 | ||
f18c16de | 1276 | static void do_sum(Monitor *mon, const QDict *qdict) |
e4cf1adc FB |
1277 | { |
1278 | uint32_t addr; | |
e4cf1adc | 1279 | uint16_t sum; |
f18c16de LC |
1280 | uint32_t start = qdict_get_int(qdict, "start"); |
1281 | uint32_t size = qdict_get_int(qdict, "size"); | |
e4cf1adc FB |
1282 | |
1283 | sum = 0; | |
1284 | for(addr = start; addr < (start + size); addr++) { | |
54f7b4a3 | 1285 | uint8_t val = ldub_phys(addr); |
e4cf1adc FB |
1286 | /* BSD sum algorithm ('sum' Unix command) */ |
1287 | sum = (sum >> 1) | (sum << 15); | |
54f7b4a3 | 1288 | sum += val; |
e4cf1adc | 1289 | } |
376253ec | 1290 | monitor_printf(mon, "%05d\n", sum); |
e4cf1adc FB |
1291 | } |
1292 | ||
13224a87 FB |
1293 | static int mouse_button_state; |
1294 | ||
1d4daa91 | 1295 | static void do_mouse_move(Monitor *mon, const QDict *qdict) |
13224a87 FB |
1296 | { |
1297 | int dx, dy, dz; | |
1d4daa91 LC |
1298 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
1299 | const char *dy_str = qdict_get_str(qdict, "dy_str"); | |
1300 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); | |
13224a87 FB |
1301 | dx = strtol(dx_str, NULL, 0); |
1302 | dy = strtol(dy_str, NULL, 0); | |
1303 | dz = 0; | |
5fafdf24 | 1304 | if (dz_str) |
13224a87 FB |
1305 | dz = strtol(dz_str, NULL, 0); |
1306 | kbd_mouse_event(dx, dy, dz, mouse_button_state); | |
1307 | } | |
1308 | ||
d54908a5 | 1309 | static void do_mouse_button(Monitor *mon, const QDict *qdict) |
13224a87 | 1310 | { |
d54908a5 | 1311 | int button_state = qdict_get_int(qdict, "button_state"); |
13224a87 FB |
1312 | mouse_button_state = button_state; |
1313 | kbd_mouse_event(0, 0, 0, mouse_button_state); | |
1314 | } | |
1315 | ||
aa93e39c | 1316 | static void do_ioport_read(Monitor *mon, const QDict *qdict) |
3440557b | 1317 | { |
aa93e39c LC |
1318 | int size = qdict_get_int(qdict, "size"); |
1319 | int addr = qdict_get_int(qdict, "addr"); | |
1320 | int has_index = qdict_haskey(qdict, "index"); | |
3440557b FB |
1321 | uint32_t val; |
1322 | int suffix; | |
1323 | ||
1324 | if (has_index) { | |
aa93e39c | 1325 | int index = qdict_get_int(qdict, "index"); |
afcea8cb | 1326 | cpu_outb(addr & IOPORTS_MASK, index & 0xff); |
3440557b FB |
1327 | addr++; |
1328 | } | |
1329 | addr &= 0xffff; | |
1330 | ||
1331 | switch(size) { | |
1332 | default: | |
1333 | case 1: | |
afcea8cb | 1334 | val = cpu_inb(addr); |
3440557b FB |
1335 | suffix = 'b'; |
1336 | break; | |
1337 | case 2: | |
afcea8cb | 1338 | val = cpu_inw(addr); |
3440557b FB |
1339 | suffix = 'w'; |
1340 | break; | |
1341 | case 4: | |
afcea8cb | 1342 | val = cpu_inl(addr); |
3440557b FB |
1343 | suffix = 'l'; |
1344 | break; | |
1345 | } | |
376253ec AL |
1346 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
1347 | suffix, addr, size * 2, val); | |
3440557b | 1348 | } |
a3a91a35 | 1349 | |
1bd1442e | 1350 | static void do_ioport_write(Monitor *mon, const QDict *qdict) |
f114784f | 1351 | { |
1bd1442e LC |
1352 | int size = qdict_get_int(qdict, "size"); |
1353 | int addr = qdict_get_int(qdict, "addr"); | |
1354 | int val = qdict_get_int(qdict, "val"); | |
1355 | ||
f114784f JK |
1356 | addr &= IOPORTS_MASK; |
1357 | ||
1358 | switch (size) { | |
1359 | default: | |
1360 | case 1: | |
afcea8cb | 1361 | cpu_outb(addr, val); |
f114784f JK |
1362 | break; |
1363 | case 2: | |
afcea8cb | 1364 | cpu_outw(addr, val); |
f114784f JK |
1365 | break; |
1366 | case 4: | |
afcea8cb | 1367 | cpu_outl(addr, val); |
f114784f JK |
1368 | break; |
1369 | } | |
1370 | } | |
1371 | ||
d54908a5 | 1372 | static void do_boot_set(Monitor *mon, const QDict *qdict) |
0ecdffbb AJ |
1373 | { |
1374 | int res; | |
d54908a5 | 1375 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
0ecdffbb | 1376 | |
76e30d0f JK |
1377 | res = qemu_boot_set(bootdevice); |
1378 | if (res == 0) { | |
1379 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); | |
1380 | } else if (res > 0) { | |
1381 | monitor_printf(mon, "setting boot device list failed\n"); | |
0ecdffbb | 1382 | } else { |
376253ec AL |
1383 | monitor_printf(mon, "no function defined to set boot device list for " |
1384 | "this architecture\n"); | |
0ecdffbb AJ |
1385 | } |
1386 | } | |
1387 | ||
b86bda5b | 1388 | #if defined(TARGET_I386) |
d65aaf37 BS |
1389 | static void print_pte(Monitor *mon, target_phys_addr_t addr, |
1390 | target_phys_addr_t pte, | |
1391 | target_phys_addr_t mask) | |
b86bda5b | 1392 | { |
d65aaf37 BS |
1393 | #ifdef TARGET_X86_64 |
1394 | if (addr & (1ULL << 47)) { | |
1395 | addr |= -1LL << 48; | |
1396 | } | |
1397 | #endif | |
1398 | monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx | |
1399 | " %c%c%c%c%c%c%c%c%c\n", | |
376253ec AL |
1400 | addr, |
1401 | pte & mask, | |
d65aaf37 | 1402 | pte & PG_NX_MASK ? 'X' : '-', |
376253ec AL |
1403 | pte & PG_GLOBAL_MASK ? 'G' : '-', |
1404 | pte & PG_PSE_MASK ? 'P' : '-', | |
1405 | pte & PG_DIRTY_MASK ? 'D' : '-', | |
1406 | pte & PG_ACCESSED_MASK ? 'A' : '-', | |
1407 | pte & PG_PCD_MASK ? 'C' : '-', | |
1408 | pte & PG_PWT_MASK ? 'T' : '-', | |
1409 | pte & PG_USER_MASK ? 'U' : '-', | |
1410 | pte & PG_RW_MASK ? 'W' : '-'); | |
b86bda5b FB |
1411 | } |
1412 | ||
9349b4f9 | 1413 | static void tlb_info_32(Monitor *mon, CPUArchState *env) |
b86bda5b | 1414 | { |
94ac5cd2 | 1415 | unsigned int l1, l2; |
b86bda5b FB |
1416 | uint32_t pgd, pde, pte; |
1417 | ||
b86bda5b FB |
1418 | pgd = env->cr[3] & ~0xfff; |
1419 | for(l1 = 0; l1 < 1024; l1++) { | |
b8b79323 | 1420 | cpu_physical_memory_read(pgd + l1 * 4, &pde, 4); |
b86bda5b FB |
1421 | pde = le32_to_cpu(pde); |
1422 | if (pde & PG_PRESENT_MASK) { | |
1423 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
d65aaf37 BS |
1424 | /* 4M pages */ |
1425 | print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1)); | |
b86bda5b FB |
1426 | } else { |
1427 | for(l2 = 0; l2 < 1024; l2++) { | |
b8b79323 | 1428 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4); |
b86bda5b FB |
1429 | pte = le32_to_cpu(pte); |
1430 | if (pte & PG_PRESENT_MASK) { | |
376253ec | 1431 | print_pte(mon, (l1 << 22) + (l2 << 12), |
5fafdf24 | 1432 | pte & ~PG_PSE_MASK, |
b86bda5b FB |
1433 | ~0xfff); |
1434 | } | |
1435 | } | |
1436 | } | |
1437 | } | |
1438 | } | |
1439 | } | |
1440 | ||
9349b4f9 | 1441 | static void tlb_info_pae32(Monitor *mon, CPUArchState *env) |
d65aaf37 | 1442 | { |
94ac5cd2 | 1443 | unsigned int l1, l2, l3; |
d65aaf37 BS |
1444 | uint64_t pdpe, pde, pte; |
1445 | uint64_t pdp_addr, pd_addr, pt_addr; | |
1446 | ||
1447 | pdp_addr = env->cr[3] & ~0x1f; | |
1448 | for (l1 = 0; l1 < 4; l1++) { | |
b8b79323 | 1449 | cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8); |
d65aaf37 BS |
1450 | pdpe = le64_to_cpu(pdpe); |
1451 | if (pdpe & PG_PRESENT_MASK) { | |
1452 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
1453 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1454 | cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8); |
d65aaf37 BS |
1455 | pde = le64_to_cpu(pde); |
1456 | if (pde & PG_PRESENT_MASK) { | |
1457 | if (pde & PG_PSE_MASK) { | |
1458 | /* 2M pages with PAE, CR4.PSE is ignored */ | |
1459 | print_pte(mon, (l1 << 30 ) + (l2 << 21), pde, | |
1460 | ~((target_phys_addr_t)(1 << 20) - 1)); | |
1461 | } else { | |
1462 | pt_addr = pde & 0x3fffffffff000ULL; | |
1463 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 1464 | cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8); |
d65aaf37 BS |
1465 | pte = le64_to_cpu(pte); |
1466 | if (pte & PG_PRESENT_MASK) { | |
1467 | print_pte(mon, (l1 << 30 ) + (l2 << 21) | |
1468 | + (l3 << 12), | |
1469 | pte & ~PG_PSE_MASK, | |
1470 | ~(target_phys_addr_t)0xfff); | |
1471 | } | |
1472 | } | |
1473 | } | |
1474 | } | |
1475 | } | |
1476 | } | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | #ifdef TARGET_X86_64 | |
9349b4f9 | 1481 | static void tlb_info_64(Monitor *mon, CPUArchState *env) |
d65aaf37 BS |
1482 | { |
1483 | uint64_t l1, l2, l3, l4; | |
1484 | uint64_t pml4e, pdpe, pde, pte; | |
1485 | uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr; | |
1486 | ||
1487 | pml4_addr = env->cr[3] & 0x3fffffffff000ULL; | |
1488 | for (l1 = 0; l1 < 512; l1++) { | |
b8b79323 | 1489 | cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8); |
d65aaf37 BS |
1490 | pml4e = le64_to_cpu(pml4e); |
1491 | if (pml4e & PG_PRESENT_MASK) { | |
1492 | pdp_addr = pml4e & 0x3fffffffff000ULL; | |
1493 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1494 | cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8); |
d65aaf37 BS |
1495 | pdpe = le64_to_cpu(pdpe); |
1496 | if (pdpe & PG_PRESENT_MASK) { | |
1497 | if (pdpe & PG_PSE_MASK) { | |
1498 | /* 1G pages, CR4.PSE is ignored */ | |
1499 | print_pte(mon, (l1 << 39) + (l2 << 30), pdpe, | |
1500 | 0x3ffffc0000000ULL); | |
1501 | } else { | |
1502 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
1503 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 1504 | cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8); |
d65aaf37 BS |
1505 | pde = le64_to_cpu(pde); |
1506 | if (pde & PG_PRESENT_MASK) { | |
1507 | if (pde & PG_PSE_MASK) { | |
1508 | /* 2M pages, CR4.PSE is ignored */ | |
1509 | print_pte(mon, (l1 << 39) + (l2 << 30) + | |
1510 | (l3 << 21), pde, | |
1511 | 0x3ffffffe00000ULL); | |
1512 | } else { | |
1513 | pt_addr = pde & 0x3fffffffff000ULL; | |
1514 | for (l4 = 0; l4 < 512; l4++) { | |
1515 | cpu_physical_memory_read(pt_addr | |
1516 | + l4 * 8, | |
b8b79323 | 1517 | &pte, 8); |
d65aaf37 BS |
1518 | pte = le64_to_cpu(pte); |
1519 | if (pte & PG_PRESENT_MASK) { | |
1520 | print_pte(mon, (l1 << 39) + | |
1521 | (l2 << 30) + | |
1522 | (l3 << 21) + (l4 << 12), | |
1523 | pte & ~PG_PSE_MASK, | |
1524 | 0x3fffffffff000ULL); | |
1525 | } | |
1526 | } | |
1527 | } | |
1528 | } | |
1529 | } | |
1530 | } | |
1531 | } | |
1532 | } | |
1533 | } | |
1534 | } | |
1535 | } | |
1536 | #endif | |
1537 | ||
1538 | static void tlb_info(Monitor *mon) | |
1539 | { | |
9349b4f9 | 1540 | CPUArchState *env; |
d65aaf37 BS |
1541 | |
1542 | env = mon_get_cpu(); | |
1543 | ||
1544 | if (!(env->cr[0] & CR0_PG_MASK)) { | |
1545 | monitor_printf(mon, "PG disabled\n"); | |
1546 | return; | |
1547 | } | |
1548 | if (env->cr[4] & CR4_PAE_MASK) { | |
1549 | #ifdef TARGET_X86_64 | |
1550 | if (env->hflags & HF_LMA_MASK) { | |
1551 | tlb_info_64(mon, env); | |
1552 | } else | |
1553 | #endif | |
1554 | { | |
1555 | tlb_info_pae32(mon, env); | |
1556 | } | |
1557 | } else { | |
1558 | tlb_info_32(mon, env); | |
1559 | } | |
1560 | } | |
1561 | ||
1b3cba6e BS |
1562 | static void mem_print(Monitor *mon, target_phys_addr_t *pstart, |
1563 | int *plast_prot, | |
1564 | target_phys_addr_t end, int prot) | |
b86bda5b | 1565 | { |
9746b15b FB |
1566 | int prot1; |
1567 | prot1 = *plast_prot; | |
1568 | if (prot != prot1) { | |
b86bda5b | 1569 | if (*pstart != -1) { |
1b3cba6e BS |
1570 | monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " " |
1571 | TARGET_FMT_plx " %c%c%c\n", | |
376253ec AL |
1572 | *pstart, end, end - *pstart, |
1573 | prot1 & PG_USER_MASK ? 'u' : '-', | |
1574 | 'r', | |
1575 | prot1 & PG_RW_MASK ? 'w' : '-'); | |
b86bda5b FB |
1576 | } |
1577 | if (prot != 0) | |
1578 | *pstart = end; | |
1579 | else | |
1580 | *pstart = -1; | |
1581 | *plast_prot = prot; | |
1582 | } | |
1583 | } | |
1584 | ||
9349b4f9 | 1585 | static void mem_info_32(Monitor *mon, CPUArchState *env) |
b86bda5b | 1586 | { |
b49ca72d AC |
1587 | unsigned int l1, l2; |
1588 | int prot, last_prot; | |
1b3cba6e BS |
1589 | uint32_t pgd, pde, pte; |
1590 | target_phys_addr_t start, end; | |
6a00d601 | 1591 | |
b86bda5b FB |
1592 | pgd = env->cr[3] & ~0xfff; |
1593 | last_prot = 0; | |
1594 | start = -1; | |
1595 | for(l1 = 0; l1 < 1024; l1++) { | |
b8b79323 | 1596 | cpu_physical_memory_read(pgd + l1 * 4, &pde, 4); |
b86bda5b FB |
1597 | pde = le32_to_cpu(pde); |
1598 | end = l1 << 22; | |
1599 | if (pde & PG_PRESENT_MASK) { | |
1600 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1601 | prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
376253ec | 1602 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1603 | } else { |
1604 | for(l2 = 0; l2 < 1024; l2++) { | |
b8b79323 | 1605 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4); |
b86bda5b FB |
1606 | pte = le32_to_cpu(pte); |
1607 | end = (l1 << 22) + (l2 << 12); | |
1608 | if (pte & PG_PRESENT_MASK) { | |
c76c8416 AC |
1609 | prot = pte & pde & |
1610 | (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
b86bda5b FB |
1611 | } else { |
1612 | prot = 0; | |
1613 | } | |
376253ec | 1614 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1615 | } |
1616 | } | |
1617 | } else { | |
1618 | prot = 0; | |
376253ec | 1619 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1620 | } |
1621 | } | |
8a94b8ca AC |
1622 | /* Flush last range */ |
1623 | mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0); | |
b86bda5b | 1624 | } |
1b3cba6e | 1625 | |
9349b4f9 | 1626 | static void mem_info_pae32(Monitor *mon, CPUArchState *env) |
1b3cba6e | 1627 | { |
b49ca72d AC |
1628 | unsigned int l1, l2, l3; |
1629 | int prot, last_prot; | |
1b3cba6e BS |
1630 | uint64_t pdpe, pde, pte; |
1631 | uint64_t pdp_addr, pd_addr, pt_addr; | |
1632 | target_phys_addr_t start, end; | |
1633 | ||
1634 | pdp_addr = env->cr[3] & ~0x1f; | |
1635 | last_prot = 0; | |
1636 | start = -1; | |
1637 | for (l1 = 0; l1 < 4; l1++) { | |
b8b79323 | 1638 | cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8); |
1b3cba6e BS |
1639 | pdpe = le64_to_cpu(pdpe); |
1640 | end = l1 << 30; | |
1641 | if (pdpe & PG_PRESENT_MASK) { | |
1642 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
1643 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1644 | cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8); |
1b3cba6e BS |
1645 | pde = le64_to_cpu(pde); |
1646 | end = (l1 << 30) + (l2 << 21); | |
1647 | if (pde & PG_PRESENT_MASK) { | |
1648 | if (pde & PG_PSE_MASK) { | |
1649 | prot = pde & (PG_USER_MASK | PG_RW_MASK | | |
1650 | PG_PRESENT_MASK); | |
1651 | mem_print(mon, &start, &last_prot, end, prot); | |
1652 | } else { | |
1653 | pt_addr = pde & 0x3fffffffff000ULL; | |
1654 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 1655 | cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8); |
1b3cba6e BS |
1656 | pte = le64_to_cpu(pte); |
1657 | end = (l1 << 30) + (l2 << 21) + (l3 << 12); | |
1658 | if (pte & PG_PRESENT_MASK) { | |
c76c8416 AC |
1659 | prot = pte & pde & (PG_USER_MASK | PG_RW_MASK | |
1660 | PG_PRESENT_MASK); | |
1b3cba6e BS |
1661 | } else { |
1662 | prot = 0; | |
1663 | } | |
1664 | mem_print(mon, &start, &last_prot, end, prot); | |
1665 | } | |
1666 | } | |
1667 | } else { | |
1668 | prot = 0; | |
1669 | mem_print(mon, &start, &last_prot, end, prot); | |
1670 | } | |
1671 | } | |
1672 | } else { | |
1673 | prot = 0; | |
1674 | mem_print(mon, &start, &last_prot, end, prot); | |
1675 | } | |
1676 | } | |
8a94b8ca AC |
1677 | /* Flush last range */ |
1678 | mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0); | |
1b3cba6e BS |
1679 | } |
1680 | ||
1681 | ||
1682 | #ifdef TARGET_X86_64 | |
9349b4f9 | 1683 | static void mem_info_64(Monitor *mon, CPUArchState *env) |
1b3cba6e BS |
1684 | { |
1685 | int prot, last_prot; | |
1686 | uint64_t l1, l2, l3, l4; | |
1687 | uint64_t pml4e, pdpe, pde, pte; | |
1688 | uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end; | |
1689 | ||
1690 | pml4_addr = env->cr[3] & 0x3fffffffff000ULL; | |
1691 | last_prot = 0; | |
1692 | start = -1; | |
1693 | for (l1 = 0; l1 < 512; l1++) { | |
b8b79323 | 1694 | cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8); |
1b3cba6e BS |
1695 | pml4e = le64_to_cpu(pml4e); |
1696 | end = l1 << 39; | |
1697 | if (pml4e & PG_PRESENT_MASK) { | |
1698 | pdp_addr = pml4e & 0x3fffffffff000ULL; | |
1699 | for (l2 = 0; l2 < 512; l2++) { | |
b8b79323 | 1700 | cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8); |
1b3cba6e BS |
1701 | pdpe = le64_to_cpu(pdpe); |
1702 | end = (l1 << 39) + (l2 << 30); | |
1703 | if (pdpe & PG_PRESENT_MASK) { | |
1704 | if (pdpe & PG_PSE_MASK) { | |
2d5b5074 BS |
1705 | prot = pdpe & (PG_USER_MASK | PG_RW_MASK | |
1706 | PG_PRESENT_MASK); | |
c76c8416 | 1707 | prot &= pml4e; |
1b3cba6e BS |
1708 | mem_print(mon, &start, &last_prot, end, prot); |
1709 | } else { | |
1710 | pd_addr = pdpe & 0x3fffffffff000ULL; | |
1711 | for (l3 = 0; l3 < 512; l3++) { | |
b8b79323 | 1712 | cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8); |
1b3cba6e BS |
1713 | pde = le64_to_cpu(pde); |
1714 | end = (l1 << 39) + (l2 << 30) + (l3 << 21); | |
1715 | if (pde & PG_PRESENT_MASK) { | |
1716 | if (pde & PG_PSE_MASK) { | |
1717 | prot = pde & (PG_USER_MASK | PG_RW_MASK | | |
1718 | PG_PRESENT_MASK); | |
c76c8416 | 1719 | prot &= pml4e & pdpe; |
1b3cba6e BS |
1720 | mem_print(mon, &start, &last_prot, end, prot); |
1721 | } else { | |
1722 | pt_addr = pde & 0x3fffffffff000ULL; | |
1723 | for (l4 = 0; l4 < 512; l4++) { | |
1724 | cpu_physical_memory_read(pt_addr | |
1725 | + l4 * 8, | |
b8b79323 | 1726 | &pte, 8); |
1b3cba6e BS |
1727 | pte = le64_to_cpu(pte); |
1728 | end = (l1 << 39) + (l2 << 30) + | |
1729 | (l3 << 21) + (l4 << 12); | |
1730 | if (pte & PG_PRESENT_MASK) { | |
1731 | prot = pte & (PG_USER_MASK | PG_RW_MASK | | |
1732 | PG_PRESENT_MASK); | |
c76c8416 | 1733 | prot &= pml4e & pdpe & pde; |
1b3cba6e BS |
1734 | } else { |
1735 | prot = 0; | |
1736 | } | |
1737 | mem_print(mon, &start, &last_prot, end, prot); | |
1738 | } | |
1739 | } | |
1740 | } else { | |
1741 | prot = 0; | |
1742 | mem_print(mon, &start, &last_prot, end, prot); | |
1743 | } | |
1744 | } | |
1745 | } | |
1746 | } else { | |
1747 | prot = 0; | |
1748 | mem_print(mon, &start, &last_prot, end, prot); | |
1749 | } | |
1750 | } | |
1751 | } else { | |
1752 | prot = 0; | |
1753 | mem_print(mon, &start, &last_prot, end, prot); | |
1754 | } | |
1755 | } | |
8a94b8ca AC |
1756 | /* Flush last range */ |
1757 | mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0); | |
1b3cba6e BS |
1758 | } |
1759 | #endif | |
1760 | ||
1761 | static void mem_info(Monitor *mon) | |
1762 | { | |
9349b4f9 | 1763 | CPUArchState *env; |
1b3cba6e BS |
1764 | |
1765 | env = mon_get_cpu(); | |
1766 | ||
1767 | if (!(env->cr[0] & CR0_PG_MASK)) { | |
1768 | monitor_printf(mon, "PG disabled\n"); | |
1769 | return; | |
1770 | } | |
1771 | if (env->cr[4] & CR4_PAE_MASK) { | |
1772 | #ifdef TARGET_X86_64 | |
1773 | if (env->hflags & HF_LMA_MASK) { | |
1774 | mem_info_64(mon, env); | |
1775 | } else | |
1776 | #endif | |
1777 | { | |
1778 | mem_info_pae32(mon, env); | |
1779 | } | |
1780 | } else { | |
1781 | mem_info_32(mon, env); | |
1782 | } | |
1783 | } | |
b86bda5b FB |
1784 | #endif |
1785 | ||
7c664e2f AJ |
1786 | #if defined(TARGET_SH4) |
1787 | ||
376253ec | 1788 | static void print_tlb(Monitor *mon, int idx, tlb_t *tlb) |
7c664e2f | 1789 | { |
376253ec AL |
1790 | monitor_printf(mon, " tlb%i:\t" |
1791 | "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t" | |
1792 | "v=%hhu shared=%hhu cached=%hhu prot=%hhu " | |
1793 | "dirty=%hhu writethrough=%hhu\n", | |
1794 | idx, | |
1795 | tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size, | |
1796 | tlb->v, tlb->sh, tlb->c, tlb->pr, | |
1797 | tlb->d, tlb->wt); | |
7c664e2f AJ |
1798 | } |
1799 | ||
376253ec | 1800 | static void tlb_info(Monitor *mon) |
7c664e2f | 1801 | { |
9349b4f9 | 1802 | CPUArchState *env = mon_get_cpu(); |
7c664e2f AJ |
1803 | int i; |
1804 | ||
376253ec | 1805 | monitor_printf (mon, "ITLB:\n"); |
7c664e2f | 1806 | for (i = 0 ; i < ITLB_SIZE ; i++) |
376253ec AL |
1807 | print_tlb (mon, i, &env->itlb[i]); |
1808 | monitor_printf (mon, "UTLB:\n"); | |
7c664e2f | 1809 | for (i = 0 ; i < UTLB_SIZE ; i++) |
376253ec | 1810 | print_tlb (mon, i, &env->utlb[i]); |
7c664e2f AJ |
1811 | } |
1812 | ||
1813 | #endif | |
1814 | ||
692f737c | 1815 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA) |
d41160a3 BS |
1816 | static void tlb_info(Monitor *mon) |
1817 | { | |
9349b4f9 | 1818 | CPUArchState *env1 = mon_get_cpu(); |
d41160a3 BS |
1819 | |
1820 | dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1); | |
1821 | } | |
1822 | #endif | |
1823 | ||
314e2987 BS |
1824 | static void do_info_mtree(Monitor *mon) |
1825 | { | |
1826 | mtree_info((fprintf_function)monitor_printf, mon); | |
1827 | } | |
1828 | ||
030ea37b AL |
1829 | static void do_info_numa(Monitor *mon) |
1830 | { | |
b28b6230 | 1831 | int i; |
9349b4f9 | 1832 | CPUArchState *env; |
030ea37b AL |
1833 | |
1834 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); | |
1835 | for (i = 0; i < nb_numa_nodes; i++) { | |
1836 | monitor_printf(mon, "node %d cpus:", i); | |
1837 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
1838 | if (env->numa_node == i) { | |
1839 | monitor_printf(mon, " %d", env->cpu_index); | |
1840 | } | |
1841 | } | |
1842 | monitor_printf(mon, "\n"); | |
1843 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, | |
1844 | node_mem[i] >> 20); | |
1845 | } | |
1846 | } | |
1847 | ||
5f1ce948 FB |
1848 | #ifdef CONFIG_PROFILER |
1849 | ||
e9a6625e AJ |
1850 | int64_t qemu_time; |
1851 | int64_t dev_time; | |
1852 | ||
376253ec | 1853 | static void do_info_profile(Monitor *mon) |
5f1ce948 FB |
1854 | { |
1855 | int64_t total; | |
1856 | total = qemu_time; | |
1857 | if (total == 0) | |
1858 | total = 1; | |
376253ec | 1859 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 1860 | dev_time, dev_time / (double)get_ticks_per_sec()); |
376253ec | 1861 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 1862 | qemu_time, qemu_time / (double)get_ticks_per_sec()); |
5f1ce948 | 1863 | qemu_time = 0; |
5f1ce948 | 1864 | dev_time = 0; |
5f1ce948 FB |
1865 | } |
1866 | #else | |
376253ec | 1867 | static void do_info_profile(Monitor *mon) |
5f1ce948 | 1868 | { |
376253ec | 1869 | monitor_printf(mon, "Internal profiler not compiled\n"); |
5f1ce948 FB |
1870 | } |
1871 | #endif | |
1872 | ||
ec36b695 | 1873 | /* Capture support */ |
72cf2d4f | 1874 | static QLIST_HEAD (capture_list_head, CaptureState) capture_head; |
ec36b695 | 1875 | |
376253ec | 1876 | static void do_info_capture(Monitor *mon) |
ec36b695 FB |
1877 | { |
1878 | int i; | |
1879 | CaptureState *s; | |
1880 | ||
1881 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
376253ec | 1882 | monitor_printf(mon, "[%d]: ", i); |
ec36b695 FB |
1883 | s->ops.info (s->opaque); |
1884 | } | |
1885 | } | |
1886 | ||
2313086a | 1887 | #ifdef HAS_AUDIO |
d54908a5 | 1888 | static void do_stop_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
1889 | { |
1890 | int i; | |
d54908a5 | 1891 | int n = qdict_get_int(qdict, "n"); |
ec36b695 FB |
1892 | CaptureState *s; |
1893 | ||
1894 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
1895 | if (i == n) { | |
1896 | s->ops.destroy (s->opaque); | |
72cf2d4f | 1897 | QLIST_REMOVE (s, entries); |
7267c094 | 1898 | g_free (s); |
ec36b695 FB |
1899 | return; |
1900 | } | |
1901 | } | |
1902 | } | |
1903 | ||
c1925484 LC |
1904 | static void do_wav_capture(Monitor *mon, const QDict *qdict) |
1905 | { | |
1906 | const char *path = qdict_get_str(qdict, "path"); | |
1907 | int has_freq = qdict_haskey(qdict, "freq"); | |
1908 | int freq = qdict_get_try_int(qdict, "freq", -1); | |
1909 | int has_bits = qdict_haskey(qdict, "bits"); | |
1910 | int bits = qdict_get_try_int(qdict, "bits", -1); | |
1911 | int has_channels = qdict_haskey(qdict, "nchannels"); | |
1912 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); | |
ec36b695 FB |
1913 | CaptureState *s; |
1914 | ||
7267c094 | 1915 | s = g_malloc0 (sizeof (*s)); |
ec36b695 FB |
1916 | |
1917 | freq = has_freq ? freq : 44100; | |
1918 | bits = has_bits ? bits : 16; | |
1919 | nchannels = has_channels ? nchannels : 2; | |
1920 | ||
1921 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
d00b2618 | 1922 | monitor_printf(mon, "Failed to add wave capture\n"); |
7267c094 | 1923 | g_free (s); |
d00b2618 | 1924 | return; |
ec36b695 | 1925 | } |
72cf2d4f | 1926 | QLIST_INSERT_HEAD (&capture_head, s, entries); |
ec36b695 FB |
1927 | } |
1928 | #endif | |
1929 | ||
15dfcd45 | 1930 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
76655d6d | 1931 | { |
15dfcd45 | 1932 | qemu_acl *acl = qemu_acl_find(name); |
76655d6d | 1933 | |
76655d6d | 1934 | if (!acl) { |
15dfcd45 | 1935 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
76655d6d | 1936 | } |
15dfcd45 JK |
1937 | return acl; |
1938 | } | |
1939 | ||
d54908a5 | 1940 | static void do_acl_show(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1941 | { |
d54908a5 | 1942 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
1943 | qemu_acl *acl = find_acl(mon, aclname); |
1944 | qemu_acl_entry *entry; | |
1945 | int i = 0; | |
76655d6d | 1946 | |
15dfcd45 | 1947 | if (acl) { |
28a76be8 | 1948 | monitor_printf(mon, "policy: %s\n", |
76655d6d | 1949 | acl->defaultDeny ? "deny" : "allow"); |
72cf2d4f | 1950 | QTAILQ_FOREACH(entry, &acl->entries, next) { |
28a76be8 AL |
1951 | i++; |
1952 | monitor_printf(mon, "%d: %s %s\n", i, | |
15dfcd45 | 1953 | entry->deny ? "deny" : "allow", entry->match); |
28a76be8 | 1954 | } |
15dfcd45 JK |
1955 | } |
1956 | } | |
1957 | ||
d54908a5 | 1958 | static void do_acl_reset(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1959 | { |
d54908a5 | 1960 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
1961 | qemu_acl *acl = find_acl(mon, aclname); |
1962 | ||
1963 | if (acl) { | |
28a76be8 AL |
1964 | qemu_acl_reset(acl); |
1965 | monitor_printf(mon, "acl: removed all rules\n"); | |
15dfcd45 JK |
1966 | } |
1967 | } | |
1968 | ||
f18c16de | 1969 | static void do_acl_policy(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1970 | { |
f18c16de LC |
1971 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1972 | const char *policy = qdict_get_str(qdict, "policy"); | |
15dfcd45 | 1973 | qemu_acl *acl = find_acl(mon, aclname); |
28a76be8 | 1974 | |
15dfcd45 JK |
1975 | if (acl) { |
1976 | if (strcmp(policy, "allow") == 0) { | |
28a76be8 AL |
1977 | acl->defaultDeny = 0; |
1978 | monitor_printf(mon, "acl: policy set to 'allow'\n"); | |
15dfcd45 | 1979 | } else if (strcmp(policy, "deny") == 0) { |
28a76be8 AL |
1980 | acl->defaultDeny = 1; |
1981 | monitor_printf(mon, "acl: policy set to 'deny'\n"); | |
1982 | } else { | |
15dfcd45 JK |
1983 | monitor_printf(mon, "acl: unknown policy '%s', " |
1984 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 | 1985 | } |
15dfcd45 JK |
1986 | } |
1987 | } | |
28a76be8 | 1988 | |
1bd1442e | 1989 | static void do_acl_add(Monitor *mon, const QDict *qdict) |
15dfcd45 | 1990 | { |
1bd1442e LC |
1991 | const char *aclname = qdict_get_str(qdict, "aclname"); |
1992 | const char *match = qdict_get_str(qdict, "match"); | |
1993 | const char *policy = qdict_get_str(qdict, "policy"); | |
1994 | int has_index = qdict_haskey(qdict, "index"); | |
1995 | int index = qdict_get_try_int(qdict, "index", -1); | |
15dfcd45 JK |
1996 | qemu_acl *acl = find_acl(mon, aclname); |
1997 | int deny, ret; | |
1998 | ||
1999 | if (acl) { | |
2000 | if (strcmp(policy, "allow") == 0) { | |
2001 | deny = 0; | |
2002 | } else if (strcmp(policy, "deny") == 0) { | |
2003 | deny = 1; | |
2004 | } else { | |
2005 | monitor_printf(mon, "acl: unknown policy '%s', " | |
2006 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 AL |
2007 | return; |
2008 | } | |
28a76be8 AL |
2009 | if (has_index) |
2010 | ret = qemu_acl_insert(acl, deny, match, index); | |
2011 | else | |
2012 | ret = qemu_acl_append(acl, deny, match); | |
2013 | if (ret < 0) | |
2014 | monitor_printf(mon, "acl: unable to add acl entry\n"); | |
2015 | else | |
2016 | monitor_printf(mon, "acl: added rule at position %d\n", ret); | |
15dfcd45 JK |
2017 | } |
2018 | } | |
28a76be8 | 2019 | |
f18c16de | 2020 | static void do_acl_remove(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2021 | { |
f18c16de LC |
2022 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2023 | const char *match = qdict_get_str(qdict, "match"); | |
15dfcd45 JK |
2024 | qemu_acl *acl = find_acl(mon, aclname); |
2025 | int ret; | |
28a76be8 | 2026 | |
15dfcd45 | 2027 | if (acl) { |
28a76be8 AL |
2028 | ret = qemu_acl_remove(acl, match); |
2029 | if (ret < 0) | |
2030 | monitor_printf(mon, "acl: no matching acl entry\n"); | |
2031 | else | |
2032 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); | |
76655d6d AL |
2033 | } |
2034 | } | |
2035 | ||
79c4f6b0 | 2036 | #if defined(TARGET_I386) |
37b7ad48 | 2037 | static void do_inject_mce(Monitor *mon, const QDict *qdict) |
79c4f6b0 | 2038 | { |
9349b4f9 | 2039 | CPUArchState *cenv; |
37b7ad48 LC |
2040 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
2041 | int bank = qdict_get_int(qdict, "bank"); | |
2042 | uint64_t status = qdict_get_int(qdict, "status"); | |
2043 | uint64_t mcg_status = qdict_get_int(qdict, "mcg_status"); | |
2044 | uint64_t addr = qdict_get_int(qdict, "addr"); | |
2045 | uint64_t misc = qdict_get_int(qdict, "misc"); | |
747461c7 | 2046 | int flags = MCE_INJECT_UNCOND_AO; |
79c4f6b0 | 2047 | |
747461c7 JK |
2048 | if (qdict_get_try_bool(qdict, "broadcast", 0)) { |
2049 | flags |= MCE_INJECT_BROADCAST; | |
2050 | } | |
31ce5e0c | 2051 | for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) { |
316378e4 JK |
2052 | if (cenv->cpu_index == cpu_index) { |
2053 | cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc, | |
747461c7 | 2054 | flags); |
79c4f6b0 HY |
2055 | break; |
2056 | } | |
31ce5e0c | 2057 | } |
79c4f6b0 HY |
2058 | } |
2059 | #endif | |
2060 | ||
208c9d1b | 2061 | void qmp_getfd(const char *fdname, Error **errp) |
f07918fd | 2062 | { |
c227f099 | 2063 | mon_fd_t *monfd; |
f07918fd MM |
2064 | int fd; |
2065 | ||
208c9d1b | 2066 | fd = qemu_chr_fe_get_msgfd(cur_mon->chr); |
f07918fd | 2067 | if (fd == -1) { |
208c9d1b CB |
2068 | error_set(errp, QERR_FD_NOT_SUPPLIED); |
2069 | return; | |
f07918fd MM |
2070 | } |
2071 | ||
2072 | if (qemu_isdigit(fdname[0])) { | |
208c9d1b CB |
2073 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname", |
2074 | "a name not starting with a digit"); | |
2075 | return; | |
f07918fd MM |
2076 | } |
2077 | ||
208c9d1b | 2078 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
f07918fd MM |
2079 | if (strcmp(monfd->name, fdname) != 0) { |
2080 | continue; | |
2081 | } | |
2082 | ||
2083 | close(monfd->fd); | |
2084 | monfd->fd = fd; | |
208c9d1b | 2085 | return; |
f07918fd MM |
2086 | } |
2087 | ||
7267c094 AL |
2088 | monfd = g_malloc0(sizeof(mon_fd_t)); |
2089 | monfd->name = g_strdup(fdname); | |
f07918fd MM |
2090 | monfd->fd = fd; |
2091 | ||
208c9d1b | 2092 | QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next); |
f07918fd MM |
2093 | } |
2094 | ||
208c9d1b | 2095 | void qmp_closefd(const char *fdname, Error **errp) |
f07918fd | 2096 | { |
c227f099 | 2097 | mon_fd_t *monfd; |
f07918fd | 2098 | |
208c9d1b | 2099 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
f07918fd MM |
2100 | if (strcmp(monfd->name, fdname) != 0) { |
2101 | continue; | |
2102 | } | |
2103 | ||
72cf2d4f | 2104 | QLIST_REMOVE(monfd, next); |
f07918fd | 2105 | close(monfd->fd); |
7267c094 AL |
2106 | g_free(monfd->name); |
2107 | g_free(monfd); | |
208c9d1b | 2108 | return; |
f07918fd MM |
2109 | } |
2110 | ||
208c9d1b | 2111 | error_set(errp, QERR_FD_NOT_FOUND, fdname); |
f07918fd MM |
2112 | } |
2113 | ||
d54908a5 | 2114 | static void do_loadvm(Monitor *mon, const QDict *qdict) |
c8d41b2c | 2115 | { |
1354869c | 2116 | int saved_vm_running = runstate_is_running(); |
d54908a5 | 2117 | const char *name = qdict_get_str(qdict, "name"); |
c8d41b2c | 2118 | |
0461d5a6 | 2119 | vm_stop(RUN_STATE_RESTORE_VM); |
c8d41b2c | 2120 | |
f0aa7a8b | 2121 | if (load_vmstate(name) == 0 && saved_vm_running) { |
c8d41b2c | 2122 | vm_start(); |
f0aa7a8b | 2123 | } |
c8d41b2c JQ |
2124 | } |
2125 | ||
7768e04c MM |
2126 | int monitor_get_fd(Monitor *mon, const char *fdname) |
2127 | { | |
c227f099 | 2128 | mon_fd_t *monfd; |
7768e04c | 2129 | |
72cf2d4f | 2130 | QLIST_FOREACH(monfd, &mon->fds, next) { |
7768e04c MM |
2131 | int fd; |
2132 | ||
2133 | if (strcmp(monfd->name, fdname) != 0) { | |
2134 | continue; | |
2135 | } | |
2136 | ||
2137 | fd = monfd->fd; | |
2138 | ||
2139 | /* caller takes ownership of fd */ | |
72cf2d4f | 2140 | QLIST_REMOVE(monfd, next); |
7267c094 AL |
2141 | g_free(monfd->name); |
2142 | g_free(monfd); | |
7768e04c MM |
2143 | |
2144 | return fd; | |
2145 | } | |
2146 | ||
2147 | return -1; | |
2148 | } | |
2149 | ||
ba1c048a CB |
2150 | static void monitor_fdset_cleanup(MonFdset *mon_fdset) |
2151 | { | |
2152 | MonFdsetFd *mon_fdset_fd; | |
2153 | MonFdsetFd *mon_fdset_fd_next; | |
2154 | ||
2155 | QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) { | |
efb87c16 CB |
2156 | if (mon_fdset_fd->removed || |
2157 | (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) { | |
ba1c048a CB |
2158 | close(mon_fdset_fd->fd); |
2159 | g_free(mon_fdset_fd->opaque); | |
2160 | QLIST_REMOVE(mon_fdset_fd, next); | |
2161 | g_free(mon_fdset_fd); | |
2162 | } | |
2163 | } | |
2164 | ||
adb696f3 | 2165 | if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) { |
ba1c048a CB |
2166 | QLIST_REMOVE(mon_fdset, next); |
2167 | g_free(mon_fdset); | |
2168 | } | |
2169 | } | |
2170 | ||
efb87c16 CB |
2171 | static void monitor_fdsets_cleanup(void) |
2172 | { | |
2173 | MonFdset *mon_fdset; | |
2174 | MonFdset *mon_fdset_next; | |
2175 | ||
2176 | QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) { | |
2177 | monitor_fdset_cleanup(mon_fdset); | |
2178 | } | |
2179 | } | |
2180 | ||
ba1c048a CB |
2181 | AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque, |
2182 | const char *opaque, Error **errp) | |
2183 | { | |
2184 | int fd; | |
2185 | Monitor *mon = cur_mon; | |
2186 | MonFdset *mon_fdset; | |
2187 | MonFdsetFd *mon_fdset_fd; | |
2188 | AddfdInfo *fdinfo; | |
2189 | ||
2190 | fd = qemu_chr_fe_get_msgfd(mon->chr); | |
2191 | if (fd == -1) { | |
2192 | error_set(errp, QERR_FD_NOT_SUPPLIED); | |
2193 | goto error; | |
2194 | } | |
2195 | ||
2196 | if (has_fdset_id) { | |
2197 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2198 | if (mon_fdset->id == fdset_id) { | |
2199 | break; | |
2200 | } | |
2201 | } | |
2202 | if (mon_fdset == NULL) { | |
2203 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id", | |
2204 | "an existing fdset-id"); | |
2205 | goto error; | |
2206 | } | |
2207 | } else { | |
2208 | int64_t fdset_id_prev = -1; | |
2209 | MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets); | |
2210 | ||
2211 | /* Use first available fdset ID */ | |
2212 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2213 | mon_fdset_cur = mon_fdset; | |
2214 | if (fdset_id_prev == mon_fdset_cur->id - 1) { | |
2215 | fdset_id_prev = mon_fdset_cur->id; | |
2216 | continue; | |
2217 | } | |
2218 | break; | |
2219 | } | |
2220 | ||
2221 | mon_fdset = g_malloc0(sizeof(*mon_fdset)); | |
2222 | mon_fdset->id = fdset_id_prev + 1; | |
2223 | ||
2224 | /* The fdset list is ordered by fdset ID */ | |
2225 | if (mon_fdset->id == 0) { | |
2226 | QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next); | |
2227 | } else if (mon_fdset->id < mon_fdset_cur->id) { | |
2228 | QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next); | |
2229 | } else { | |
2230 | QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next); | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd)); | |
2235 | mon_fdset_fd->fd = fd; | |
2236 | mon_fdset_fd->removed = false; | |
2237 | if (has_opaque) { | |
2238 | mon_fdset_fd->opaque = g_strdup(opaque); | |
2239 | } | |
2240 | QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next); | |
2241 | ||
2242 | fdinfo = g_malloc0(sizeof(*fdinfo)); | |
2243 | fdinfo->fdset_id = mon_fdset->id; | |
2244 | fdinfo->fd = mon_fdset_fd->fd; | |
2245 | ||
2246 | return fdinfo; | |
2247 | ||
2248 | error: | |
2249 | if (fd != -1) { | |
2250 | close(fd); | |
2251 | } | |
2252 | return NULL; | |
2253 | } | |
2254 | ||
2255 | void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) | |
2256 | { | |
2257 | MonFdset *mon_fdset; | |
2258 | MonFdsetFd *mon_fdset_fd; | |
2259 | char fd_str[60]; | |
2260 | ||
2261 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2262 | if (mon_fdset->id != fdset_id) { | |
2263 | continue; | |
2264 | } | |
2265 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2266 | if (has_fd) { | |
2267 | if (mon_fdset_fd->fd != fd) { | |
2268 | continue; | |
2269 | } | |
2270 | mon_fdset_fd->removed = true; | |
2271 | break; | |
2272 | } else { | |
2273 | mon_fdset_fd->removed = true; | |
2274 | } | |
2275 | } | |
2276 | if (has_fd && !mon_fdset_fd) { | |
2277 | goto error; | |
2278 | } | |
2279 | monitor_fdset_cleanup(mon_fdset); | |
2280 | return; | |
2281 | } | |
2282 | ||
2283 | error: | |
2284 | if (has_fd) { | |
2285 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64, | |
2286 | fdset_id, fd); | |
2287 | } else { | |
2288 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id); | |
2289 | } | |
2290 | error_set(errp, QERR_FD_NOT_FOUND, fd_str); | |
2291 | } | |
2292 | ||
2293 | FdsetInfoList *qmp_query_fdsets(Error **errp) | |
2294 | { | |
2295 | MonFdset *mon_fdset; | |
2296 | MonFdsetFd *mon_fdset_fd; | |
2297 | FdsetInfoList *fdset_list = NULL; | |
2298 | ||
2299 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2300 | FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info)); | |
2301 | FdsetFdInfoList *fdsetfd_list = NULL; | |
2302 | ||
2303 | fdset_info->value = g_malloc0(sizeof(*fdset_info->value)); | |
2304 | fdset_info->value->fdset_id = mon_fdset->id; | |
2305 | ||
2306 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2307 | FdsetFdInfoList *fdsetfd_info; | |
2308 | ||
2309 | fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info)); | |
2310 | fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value)); | |
2311 | fdsetfd_info->value->fd = mon_fdset_fd->fd; | |
2312 | if (mon_fdset_fd->opaque) { | |
2313 | fdsetfd_info->value->has_opaque = true; | |
2314 | fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque); | |
2315 | } else { | |
2316 | fdsetfd_info->value->has_opaque = false; | |
2317 | } | |
2318 | ||
2319 | fdsetfd_info->next = fdsetfd_list; | |
2320 | fdsetfd_list = fdsetfd_info; | |
2321 | } | |
2322 | ||
2323 | fdset_info->value->fds = fdsetfd_list; | |
2324 | ||
2325 | fdset_info->next = fdset_list; | |
2326 | fdset_list = fdset_info; | |
2327 | } | |
2328 | ||
2329 | return fdset_list; | |
2330 | } | |
2331 | ||
adb696f3 CB |
2332 | int monitor_fdset_get_fd(int64_t fdset_id, int flags) |
2333 | { | |
b2dc64c3 | 2334 | #ifndef _WIN32 |
adb696f3 CB |
2335 | MonFdset *mon_fdset; |
2336 | MonFdsetFd *mon_fdset_fd; | |
2337 | int mon_fd_flags; | |
2338 | ||
adb696f3 CB |
2339 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
2340 | if (mon_fdset->id != fdset_id) { | |
2341 | continue; | |
2342 | } | |
2343 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { | |
2344 | mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL); | |
2345 | if (mon_fd_flags == -1) { | |
2346 | return -1; | |
2347 | } | |
2348 | ||
2349 | if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) { | |
2350 | return mon_fdset_fd->fd; | |
2351 | } | |
2352 | } | |
2353 | errno = EACCES; | |
2354 | return -1; | |
2355 | } | |
2356 | #endif | |
2357 | ||
2358 | errno = ENOENT; | |
2359 | return -1; | |
2360 | } | |
2361 | ||
2362 | int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd) | |
2363 | { | |
2364 | MonFdset *mon_fdset; | |
2365 | MonFdsetFd *mon_fdset_fd_dup; | |
2366 | ||
2367 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2368 | if (mon_fdset->id != fdset_id) { | |
2369 | continue; | |
2370 | } | |
2371 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { | |
2372 | if (mon_fdset_fd_dup->fd == dup_fd) { | |
2373 | return -1; | |
2374 | } | |
2375 | } | |
2376 | mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup)); | |
2377 | mon_fdset_fd_dup->fd = dup_fd; | |
2378 | QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next); | |
2379 | return 0; | |
2380 | } | |
2381 | return -1; | |
2382 | } | |
2383 | ||
2384 | static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) | |
2385 | { | |
2386 | MonFdset *mon_fdset; | |
2387 | MonFdsetFd *mon_fdset_fd_dup; | |
2388 | ||
2389 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { | |
2390 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { | |
2391 | if (mon_fdset_fd_dup->fd == dup_fd) { | |
2392 | if (remove) { | |
2393 | QLIST_REMOVE(mon_fdset_fd_dup, next); | |
2394 | if (QLIST_EMPTY(&mon_fdset->dup_fds)) { | |
2395 | monitor_fdset_cleanup(mon_fdset); | |
2396 | } | |
2397 | } | |
2398 | return mon_fdset->id; | |
2399 | } | |
2400 | } | |
2401 | } | |
2402 | return -1; | |
2403 | } | |
2404 | ||
2405 | int monitor_fdset_dup_fd_find(int dup_fd) | |
2406 | { | |
2407 | return monitor_fdset_dup_fd_find_remove(dup_fd, false); | |
2408 | } | |
2409 | ||
2410 | int monitor_fdset_dup_fd_remove(int dup_fd) | |
2411 | { | |
2412 | return monitor_fdset_dup_fd_find_remove(dup_fd, true); | |
2413 | } | |
2414 | ||
816f8925 WX |
2415 | /* mon_cmds and info_cmds would be sorted at runtime */ |
2416 | static mon_cmd_t mon_cmds[] = { | |
acd0a093 | 2417 | #include "hmp-commands.h" |
5fafdf24 | 2418 | { NULL, NULL, }, |
9dc39cba FB |
2419 | }; |
2420 | ||
acd0a093 | 2421 | /* Please update hmp-commands.hx when adding or changing commands */ |
816f8925 | 2422 | static mon_cmd_t info_cmds[] = { |
d7f9b689 LC |
2423 | { |
2424 | .name = "version", | |
2425 | .args_type = "", | |
d7f9b689 LC |
2426 | .params = "", |
2427 | .help = "show the version of QEMU", | |
b9c15f16 | 2428 | .mhandler.info = hmp_info_version, |
d7f9b689 LC |
2429 | }, |
2430 | { | |
2431 | .name = "network", | |
2432 | .args_type = "", | |
d7f9b689 LC |
2433 | .params = "", |
2434 | .help = "show the network state", | |
910df89d | 2435 | .mhandler.info = do_info_network, |
d7f9b689 LC |
2436 | }, |
2437 | { | |
2438 | .name = "chardev", | |
2439 | .args_type = "", | |
d7f9b689 LC |
2440 | .params = "", |
2441 | .help = "show the character devices", | |
c5a415a0 | 2442 | .mhandler.info = hmp_info_chardev, |
d7f9b689 LC |
2443 | }, |
2444 | { | |
2445 | .name = "block", | |
2446 | .args_type = "", | |
d7f9b689 LC |
2447 | .params = "", |
2448 | .help = "show the block devices", | |
b2023818 | 2449 | .mhandler.info = hmp_info_block, |
d7f9b689 LC |
2450 | }, |
2451 | { | |
2452 | .name = "blockstats", | |
2453 | .args_type = "", | |
d7f9b689 LC |
2454 | .params = "", |
2455 | .help = "show block device statistics", | |
f11f57e4 | 2456 | .mhandler.info = hmp_info_blockstats, |
d7f9b689 | 2457 | }, |
fb5458cd SH |
2458 | { |
2459 | .name = "block-jobs", | |
2460 | .args_type = "", | |
2461 | .params = "", | |
2462 | .help = "show progress of ongoing block device operations", | |
2463 | .mhandler.info = hmp_info_block_jobs, | |
2464 | }, | |
d7f9b689 LC |
2465 | { |
2466 | .name = "registers", | |
2467 | .args_type = "", | |
d7f9b689 LC |
2468 | .params = "", |
2469 | .help = "show the cpu registers", | |
910df89d | 2470 | .mhandler.info = do_info_registers, |
d7f9b689 LC |
2471 | }, |
2472 | { | |
2473 | .name = "cpus", | |
2474 | .args_type = "", | |
d7f9b689 LC |
2475 | .params = "", |
2476 | .help = "show infos for each CPU", | |
de0b36b6 | 2477 | .mhandler.info = hmp_info_cpus, |
d7f9b689 LC |
2478 | }, |
2479 | { | |
2480 | .name = "history", | |
2481 | .args_type = "", | |
d7f9b689 LC |
2482 | .params = "", |
2483 | .help = "show the command line history", | |
910df89d | 2484 | .mhandler.info = do_info_history, |
d7f9b689 | 2485 | }, |
661f1929 JK |
2486 | #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \ |
2487 | defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64)) | |
d7f9b689 LC |
2488 | { |
2489 | .name = "irq", | |
2490 | .args_type = "", | |
d7f9b689 LC |
2491 | .params = "", |
2492 | .help = "show the interrupts statistics (if available)", | |
661f1929 JK |
2493 | #ifdef TARGET_SPARC |
2494 | .mhandler.info = sun4m_irq_info, | |
2495 | #elif defined(TARGET_LM32) | |
2496 | .mhandler.info = lm32_irq_info, | |
2497 | #else | |
910df89d | 2498 | .mhandler.info = irq_info, |
661f1929 | 2499 | #endif |
d7f9b689 LC |
2500 | }, |
2501 | { | |
2502 | .name = "pic", | |
2503 | .args_type = "", | |
d7f9b689 LC |
2504 | .params = "", |
2505 | .help = "show i8259 (PIC) state", | |
661f1929 JK |
2506 | #ifdef TARGET_SPARC |
2507 | .mhandler.info = sun4m_pic_info, | |
2508 | #elif defined(TARGET_LM32) | |
2509 | .mhandler.info = lm32_do_pic_info, | |
2510 | #else | |
910df89d | 2511 | .mhandler.info = pic_info, |
661f1929 | 2512 | #endif |
d7f9b689 | 2513 | }, |
661f1929 | 2514 | #endif |
d7f9b689 LC |
2515 | { |
2516 | .name = "pci", | |
2517 | .args_type = "", | |
d7f9b689 LC |
2518 | .params = "", |
2519 | .help = "show PCI info", | |
79627472 | 2520 | .mhandler.info = hmp_info_pci, |
d7f9b689 | 2521 | }, |
bebabbc7 | 2522 | #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \ |
692f737c | 2523 | defined(TARGET_PPC) || defined(TARGET_XTENSA) |
d7f9b689 LC |
2524 | { |
2525 | .name = "tlb", | |
2526 | .args_type = "", | |
d7f9b689 LC |
2527 | .params = "", |
2528 | .help = "show virtual to physical memory mappings", | |
910df89d | 2529 | .mhandler.info = tlb_info, |
d7f9b689 | 2530 | }, |
7c664e2f AJ |
2531 | #endif |
2532 | #if defined(TARGET_I386) | |
d7f9b689 LC |
2533 | { |
2534 | .name = "mem", | |
2535 | .args_type = "", | |
d7f9b689 LC |
2536 | .params = "", |
2537 | .help = "show the active virtual memory mappings", | |
910df89d | 2538 | .mhandler.info = mem_info, |
d7f9b689 | 2539 | }, |
b86bda5b | 2540 | #endif |
314e2987 BS |
2541 | { |
2542 | .name = "mtree", | |
2543 | .args_type = "", | |
2544 | .params = "", | |
2545 | .help = "show memory tree", | |
2546 | .mhandler.info = do_info_mtree, | |
2547 | }, | |
d7f9b689 LC |
2548 | { |
2549 | .name = "jit", | |
2550 | .args_type = "", | |
d7f9b689 LC |
2551 | .params = "", |
2552 | .help = "show dynamic compiler info", | |
910df89d | 2553 | .mhandler.info = do_info_jit, |
d7f9b689 LC |
2554 | }, |
2555 | { | |
2556 | .name = "kvm", | |
2557 | .args_type = "", | |
d7f9b689 LC |
2558 | .params = "", |
2559 | .help = "show KVM information", | |
292a2602 | 2560 | .mhandler.info = hmp_info_kvm, |
d7f9b689 LC |
2561 | }, |
2562 | { | |
2563 | .name = "numa", | |
2564 | .args_type = "", | |
d7f9b689 LC |
2565 | .params = "", |
2566 | .help = "show NUMA information", | |
910df89d | 2567 | .mhandler.info = do_info_numa, |
d7f9b689 LC |
2568 | }, |
2569 | { | |
2570 | .name = "usb", | |
2571 | .args_type = "", | |
d7f9b689 LC |
2572 | .params = "", |
2573 | .help = "show guest USB devices", | |
910df89d | 2574 | .mhandler.info = usb_info, |
d7f9b689 LC |
2575 | }, |
2576 | { | |
2577 | .name = "usbhost", | |
2578 | .args_type = "", | |
d7f9b689 LC |
2579 | .params = "", |
2580 | .help = "show host USB devices", | |
910df89d | 2581 | .mhandler.info = usb_host_info, |
d7f9b689 LC |
2582 | }, |
2583 | { | |
2584 | .name = "profile", | |
2585 | .args_type = "", | |
d7f9b689 LC |
2586 | .params = "", |
2587 | .help = "show profiling information", | |
910df89d | 2588 | .mhandler.info = do_info_profile, |
d7f9b689 LC |
2589 | }, |
2590 | { | |
2591 | .name = "capture", | |
2592 | .args_type = "", | |
d7f9b689 LC |
2593 | .params = "", |
2594 | .help = "show capture information", | |
910df89d | 2595 | .mhandler.info = do_info_capture, |
d7f9b689 LC |
2596 | }, |
2597 | { | |
2598 | .name = "snapshots", | |
2599 | .args_type = "", | |
d7f9b689 LC |
2600 | .params = "", |
2601 | .help = "show the currently saved VM snapshots", | |
910df89d | 2602 | .mhandler.info = do_info_snapshots, |
d7f9b689 LC |
2603 | }, |
2604 | { | |
2605 | .name = "status", | |
2606 | .args_type = "", | |
d7f9b689 LC |
2607 | .params = "", |
2608 | .help = "show the current VM status (running|paused)", | |
1fa9a5e4 | 2609 | .mhandler.info = hmp_info_status, |
d7f9b689 LC |
2610 | }, |
2611 | { | |
2612 | .name = "pcmcia", | |
2613 | .args_type = "", | |
d7f9b689 LC |
2614 | .params = "", |
2615 | .help = "show guest PCMCIA status", | |
910df89d | 2616 | .mhandler.info = pcmcia_info, |
d7f9b689 LC |
2617 | }, |
2618 | { | |
2619 | .name = "mice", | |
2620 | .args_type = "", | |
d7f9b689 LC |
2621 | .params = "", |
2622 | .help = "show which guest mouse is receiving events", | |
e235cec3 | 2623 | .mhandler.info = hmp_info_mice, |
d7f9b689 LC |
2624 | }, |
2625 | { | |
2626 | .name = "vnc", | |
2627 | .args_type = "", | |
d7f9b689 LC |
2628 | .params = "", |
2629 | .help = "show the vnc server status", | |
2b54aa87 | 2630 | .mhandler.info = hmp_info_vnc, |
d7f9b689 | 2631 | }, |
cb42a870 GH |
2632 | #if defined(CONFIG_SPICE) |
2633 | { | |
2634 | .name = "spice", | |
2635 | .args_type = "", | |
2636 | .params = "", | |
2637 | .help = "show the spice server status", | |
d1f29646 | 2638 | .mhandler.info = hmp_info_spice, |
cb42a870 GH |
2639 | }, |
2640 | #endif | |
d7f9b689 LC |
2641 | { |
2642 | .name = "name", | |
2643 | .args_type = "", | |
d7f9b689 LC |
2644 | .params = "", |
2645 | .help = "show the current VM name", | |
48a32bed | 2646 | .mhandler.info = hmp_info_name, |
d7f9b689 LC |
2647 | }, |
2648 | { | |
2649 | .name = "uuid", | |
2650 | .args_type = "", | |
d7f9b689 LC |
2651 | .params = "", |
2652 | .help = "show the current VM UUID", | |
efab767e | 2653 | .mhandler.info = hmp_info_uuid, |
d7f9b689 | 2654 | }, |
76a66253 | 2655 | #if defined(TARGET_PPC) |
d7f9b689 LC |
2656 | { |
2657 | .name = "cpustats", | |
2658 | .args_type = "", | |
d7f9b689 LC |
2659 | .params = "", |
2660 | .help = "show CPU statistics", | |
910df89d | 2661 | .mhandler.info = do_info_cpu_stats, |
d7f9b689 | 2662 | }, |
76a66253 | 2663 | #endif |
31a60e22 | 2664 | #if defined(CONFIG_SLIRP) |
d7f9b689 LC |
2665 | { |
2666 | .name = "usernet", | |
2667 | .args_type = "", | |
d7f9b689 LC |
2668 | .params = "", |
2669 | .help = "show user network stack connection states", | |
910df89d | 2670 | .mhandler.info = do_info_usernet, |
d7f9b689 | 2671 | }, |
31a60e22 | 2672 | #endif |
d7f9b689 LC |
2673 | { |
2674 | .name = "migrate", | |
2675 | .args_type = "", | |
d7f9b689 LC |
2676 | .params = "", |
2677 | .help = "show migration status", | |
791e7c82 | 2678 | .mhandler.info = hmp_info_migrate, |
d7f9b689 | 2679 | }, |
bbf6da32 OW |
2680 | { |
2681 | .name = "migrate_capabilities", | |
2682 | .args_type = "", | |
2683 | .params = "", | |
2684 | .help = "show current migration capabilities", | |
2685 | .mhandler.info = hmp_info_migrate_capabilities, | |
2686 | }, | |
9e1ba4cc OW |
2687 | { |
2688 | .name = "migrate_cache_size", | |
2689 | .args_type = "", | |
2690 | .params = "", | |
2691 | .help = "show current migration xbzrle cache size", | |
2692 | .mhandler.info = hmp_info_migrate_cache_size, | |
2693 | }, | |
d7f9b689 LC |
2694 | { |
2695 | .name = "balloon", | |
2696 | .args_type = "", | |
d7f9b689 LC |
2697 | .params = "", |
2698 | .help = "show balloon information", | |
96637bcd | 2699 | .mhandler.info = hmp_info_balloon, |
d7f9b689 LC |
2700 | }, |
2701 | { | |
2702 | .name = "qtree", | |
2703 | .args_type = "", | |
d7f9b689 LC |
2704 | .params = "", |
2705 | .help = "show device tree", | |
910df89d | 2706 | .mhandler.info = do_info_qtree, |
d7f9b689 LC |
2707 | }, |
2708 | { | |
2709 | .name = "qdm", | |
2710 | .args_type = "", | |
d7f9b689 LC |
2711 | .params = "", |
2712 | .help = "show qdev device model list", | |
910df89d | 2713 | .mhandler.info = do_info_qdm, |
d7f9b689 LC |
2714 | }, |
2715 | { | |
2716 | .name = "roms", | |
2717 | .args_type = "", | |
d7f9b689 LC |
2718 | .params = "", |
2719 | .help = "show roms", | |
910df89d | 2720 | .mhandler.info = do_info_roms, |
d7f9b689 | 2721 | }, |
22890ab5 PS |
2722 | { |
2723 | .name = "trace-events", | |
2724 | .args_type = "", | |
2725 | .params = "", | |
2726 | .help = "show available trace-events & their state", | |
fc764105 | 2727 | .mhandler.info = do_trace_print_events, |
22890ab5 | 2728 | }, |
d7f9b689 LC |
2729 | { |
2730 | .name = NULL, | |
2731 | }, | |
9dc39cba FB |
2732 | }; |
2733 | ||
f36b4afb | 2734 | static const mon_cmd_t qmp_cmds[] = { |
e3193601 | 2735 | #include "qmp-commands-old.h" |
f36b4afb LC |
2736 | { /* NULL */ }, |
2737 | }; | |
2738 | ||
9307c4c1 FB |
2739 | /*******************************************************************/ |
2740 | ||
2741 | static const char *pch; | |
2742 | static jmp_buf expr_env; | |
2743 | ||
92a31b1f FB |
2744 | #define MD_TLONG 0 |
2745 | #define MD_I32 1 | |
2746 | ||
9307c4c1 FB |
2747 | typedef struct MonitorDef { |
2748 | const char *name; | |
2749 | int offset; | |
8662d656 | 2750 | target_long (*get_value)(const struct MonitorDef *md, int val); |
92a31b1f | 2751 | int type; |
9307c4c1 FB |
2752 | } MonitorDef; |
2753 | ||
57206fd4 | 2754 | #if defined(TARGET_I386) |
8662d656 | 2755 | static target_long monitor_get_pc (const struct MonitorDef *md, int val) |
57206fd4 | 2756 | { |
9349b4f9 | 2757 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 2758 | return env->eip + env->segs[R_CS].base; |
57206fd4 FB |
2759 | } |
2760 | #endif | |
2761 | ||
a541f297 | 2762 | #if defined(TARGET_PPC) |
8662d656 | 2763 | static target_long monitor_get_ccr (const struct MonitorDef *md, int val) |
a541f297 | 2764 | { |
9349b4f9 | 2765 | CPUArchState *env = mon_get_cpu(); |
a541f297 FB |
2766 | unsigned int u; |
2767 | int i; | |
2768 | ||
2769 | u = 0; | |
2770 | for (i = 0; i < 8; i++) | |
28a76be8 | 2771 | u |= env->crf[i] << (32 - (4 * i)); |
a541f297 FB |
2772 | |
2773 | return u; | |
2774 | } | |
2775 | ||
8662d656 | 2776 | static target_long monitor_get_msr (const struct MonitorDef *md, int val) |
a541f297 | 2777 | { |
9349b4f9 | 2778 | CPUArchState *env = mon_get_cpu(); |
0411a972 | 2779 | return env->msr; |
a541f297 FB |
2780 | } |
2781 | ||
8662d656 | 2782 | static target_long monitor_get_xer (const struct MonitorDef *md, int val) |
a541f297 | 2783 | { |
9349b4f9 | 2784 | CPUArchState *env = mon_get_cpu(); |
3d7b417e | 2785 | return env->xer; |
a541f297 | 2786 | } |
9fddaa0c | 2787 | |
8662d656 | 2788 | static target_long monitor_get_decr (const struct MonitorDef *md, int val) |
9fddaa0c | 2789 | { |
9349b4f9 | 2790 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 2791 | return cpu_ppc_load_decr(env); |
9fddaa0c FB |
2792 | } |
2793 | ||
8662d656 | 2794 | static target_long monitor_get_tbu (const struct MonitorDef *md, int val) |
9fddaa0c | 2795 | { |
9349b4f9 | 2796 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 2797 | return cpu_ppc_load_tbu(env); |
9fddaa0c FB |
2798 | } |
2799 | ||
8662d656 | 2800 | static target_long monitor_get_tbl (const struct MonitorDef *md, int val) |
9fddaa0c | 2801 | { |
9349b4f9 | 2802 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 2803 | return cpu_ppc_load_tbl(env); |
9fddaa0c | 2804 | } |
a541f297 FB |
2805 | #endif |
2806 | ||
e95c8d51 | 2807 | #if defined(TARGET_SPARC) |
7b936c0c | 2808 | #ifndef TARGET_SPARC64 |
8662d656 | 2809 | static target_long monitor_get_psr (const struct MonitorDef *md, int val) |
e95c8d51 | 2810 | { |
9349b4f9 | 2811 | CPUArchState *env = mon_get_cpu(); |
5a834bb4 BS |
2812 | |
2813 | return cpu_get_psr(env); | |
e95c8d51 | 2814 | } |
7b936c0c | 2815 | #endif |
e95c8d51 | 2816 | |
8662d656 | 2817 | static target_long monitor_get_reg(const struct MonitorDef *md, int val) |
e95c8d51 | 2818 | { |
9349b4f9 | 2819 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 2820 | return env->regwptr[val]; |
e95c8d51 FB |
2821 | } |
2822 | #endif | |
2823 | ||
8662d656 | 2824 | static const MonitorDef monitor_defs[] = { |
9307c4c1 | 2825 | #ifdef TARGET_I386 |
57206fd4 FB |
2826 | |
2827 | #define SEG(name, seg) \ | |
e59d167f AF |
2828 | { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\ |
2829 | { name ".base", offsetof(CPUX86State, segs[seg].base) },\ | |
2830 | { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 }, | |
2831 | ||
2832 | { "eax", offsetof(CPUX86State, regs[0]) }, | |
2833 | { "ecx", offsetof(CPUX86State, regs[1]) }, | |
2834 | { "edx", offsetof(CPUX86State, regs[2]) }, | |
2835 | { "ebx", offsetof(CPUX86State, regs[3]) }, | |
2836 | { "esp|sp", offsetof(CPUX86State, regs[4]) }, | |
2837 | { "ebp|fp", offsetof(CPUX86State, regs[5]) }, | |
2838 | { "esi", offsetof(CPUX86State, regs[6]) }, | |
2839 | { "edi", offsetof(CPUX86State, regs[7]) }, | |
92a31b1f | 2840 | #ifdef TARGET_X86_64 |
e59d167f AF |
2841 | { "r8", offsetof(CPUX86State, regs[8]) }, |
2842 | { "r9", offsetof(CPUX86State, regs[9]) }, | |
2843 | { "r10", offsetof(CPUX86State, regs[10]) }, | |
2844 | { "r11", offsetof(CPUX86State, regs[11]) }, | |
2845 | { "r12", offsetof(CPUX86State, regs[12]) }, | |
2846 | { "r13", offsetof(CPUX86State, regs[13]) }, | |
2847 | { "r14", offsetof(CPUX86State, regs[14]) }, | |
2848 | { "r15", offsetof(CPUX86State, regs[15]) }, | |
92a31b1f | 2849 | #endif |
e59d167f AF |
2850 | { "eflags", offsetof(CPUX86State, eflags) }, |
2851 | { "eip", offsetof(CPUX86State, eip) }, | |
57206fd4 FB |
2852 | SEG("cs", R_CS) |
2853 | SEG("ds", R_DS) | |
2854 | SEG("es", R_ES) | |
01038d2a | 2855 | SEG("ss", R_SS) |
57206fd4 FB |
2856 | SEG("fs", R_FS) |
2857 | SEG("gs", R_GS) | |
2858 | { "pc", 0, monitor_get_pc, }, | |
a541f297 | 2859 | #elif defined(TARGET_PPC) |
ff937dba | 2860 | /* General purpose registers */ |
e59d167f AF |
2861 | { "r0", offsetof(CPUPPCState, gpr[0]) }, |
2862 | { "r1", offsetof(CPUPPCState, gpr[1]) }, | |
2863 | { "r2", offsetof(CPUPPCState, gpr[2]) }, | |
2864 | { "r3", offsetof(CPUPPCState, gpr[3]) }, | |
2865 | { "r4", offsetof(CPUPPCState, gpr[4]) }, | |
2866 | { "r5", offsetof(CPUPPCState, gpr[5]) }, | |
2867 | { "r6", offsetof(CPUPPCState, gpr[6]) }, | |
2868 | { "r7", offsetof(CPUPPCState, gpr[7]) }, | |
2869 | { "r8", offsetof(CPUPPCState, gpr[8]) }, | |
2870 | { "r9", offsetof(CPUPPCState, gpr[9]) }, | |
2871 | { "r10", offsetof(CPUPPCState, gpr[10]) }, | |
2872 | { "r11", offsetof(CPUPPCState, gpr[11]) }, | |
2873 | { "r12", offsetof(CPUPPCState, gpr[12]) }, | |
2874 | { "r13", offsetof(CPUPPCState, gpr[13]) }, | |
2875 | { "r14", offsetof(CPUPPCState, gpr[14]) }, | |
2876 | { "r15", offsetof(CPUPPCState, gpr[15]) }, | |
2877 | { "r16", offsetof(CPUPPCState, gpr[16]) }, | |
2878 | { "r17", offsetof(CPUPPCState, gpr[17]) }, | |
2879 | { "r18", offsetof(CPUPPCState, gpr[18]) }, | |
2880 | { "r19", offsetof(CPUPPCState, gpr[19]) }, | |
2881 | { "r20", offsetof(CPUPPCState, gpr[20]) }, | |
2882 | { "r21", offsetof(CPUPPCState, gpr[21]) }, | |
2883 | { "r22", offsetof(CPUPPCState, gpr[22]) }, | |
2884 | { "r23", offsetof(CPUPPCState, gpr[23]) }, | |
2885 | { "r24", offsetof(CPUPPCState, gpr[24]) }, | |
2886 | { "r25", offsetof(CPUPPCState, gpr[25]) }, | |
2887 | { "r26", offsetof(CPUPPCState, gpr[26]) }, | |
2888 | { "r27", offsetof(CPUPPCState, gpr[27]) }, | |
2889 | { "r28", offsetof(CPUPPCState, gpr[28]) }, | |
2890 | { "r29", offsetof(CPUPPCState, gpr[29]) }, | |
2891 | { "r30", offsetof(CPUPPCState, gpr[30]) }, | |
2892 | { "r31", offsetof(CPUPPCState, gpr[31]) }, | |
ff937dba | 2893 | /* Floating point registers */ |
e59d167f AF |
2894 | { "f0", offsetof(CPUPPCState, fpr[0]) }, |
2895 | { "f1", offsetof(CPUPPCState, fpr[1]) }, | |
2896 | { "f2", offsetof(CPUPPCState, fpr[2]) }, | |
2897 | { "f3", offsetof(CPUPPCState, fpr[3]) }, | |
2898 | { "f4", offsetof(CPUPPCState, fpr[4]) }, | |
2899 | { "f5", offsetof(CPUPPCState, fpr[5]) }, | |
2900 | { "f6", offsetof(CPUPPCState, fpr[6]) }, | |
2901 | { "f7", offsetof(CPUPPCState, fpr[7]) }, | |
2902 | { "f8", offsetof(CPUPPCState, fpr[8]) }, | |
2903 | { "f9", offsetof(CPUPPCState, fpr[9]) }, | |
2904 | { "f10", offsetof(CPUPPCState, fpr[10]) }, | |
2905 | { "f11", offsetof(CPUPPCState, fpr[11]) }, | |
2906 | { "f12", offsetof(CPUPPCState, fpr[12]) }, | |
2907 | { "f13", offsetof(CPUPPCState, fpr[13]) }, | |
2908 | { "f14", offsetof(CPUPPCState, fpr[14]) }, | |
2909 | { "f15", offsetof(CPUPPCState, fpr[15]) }, | |
2910 | { "f16", offsetof(CPUPPCState, fpr[16]) }, | |
2911 | { "f17", offsetof(CPUPPCState, fpr[17]) }, | |
2912 | { "f18", offsetof(CPUPPCState, fpr[18]) }, | |
2913 | { "f19", offsetof(CPUPPCState, fpr[19]) }, | |
2914 | { "f20", offsetof(CPUPPCState, fpr[20]) }, | |
2915 | { "f21", offsetof(CPUPPCState, fpr[21]) }, | |
2916 | { "f22", offsetof(CPUPPCState, fpr[22]) }, | |
2917 | { "f23", offsetof(CPUPPCState, fpr[23]) }, | |
2918 | { "f24", offsetof(CPUPPCState, fpr[24]) }, | |
2919 | { "f25", offsetof(CPUPPCState, fpr[25]) }, | |
2920 | { "f26", offsetof(CPUPPCState, fpr[26]) }, | |
2921 | { "f27", offsetof(CPUPPCState, fpr[27]) }, | |
2922 | { "f28", offsetof(CPUPPCState, fpr[28]) }, | |
2923 | { "f29", offsetof(CPUPPCState, fpr[29]) }, | |
2924 | { "f30", offsetof(CPUPPCState, fpr[30]) }, | |
2925 | { "f31", offsetof(CPUPPCState, fpr[31]) }, | |
2926 | { "fpscr", offsetof(CPUPPCState, fpscr) }, | |
ff937dba | 2927 | /* Next instruction pointer */ |
e59d167f AF |
2928 | { "nip|pc", offsetof(CPUPPCState, nip) }, |
2929 | { "lr", offsetof(CPUPPCState, lr) }, | |
2930 | { "ctr", offsetof(CPUPPCState, ctr) }, | |
9fddaa0c | 2931 | { "decr", 0, &monitor_get_decr, }, |
a541f297 | 2932 | { "ccr", 0, &monitor_get_ccr, }, |
ff937dba | 2933 | /* Machine state register */ |
a541f297 FB |
2934 | { "msr", 0, &monitor_get_msr, }, |
2935 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
2936 | { "tbu", 0, &monitor_get_tbu, }, |
2937 | { "tbl", 0, &monitor_get_tbl, }, | |
ff937dba JM |
2938 | #if defined(TARGET_PPC64) |
2939 | /* Address space register */ | |
e59d167f | 2940 | { "asr", offsetof(CPUPPCState, asr) }, |
ff937dba JM |
2941 | #endif |
2942 | /* Segment registers */ | |
e59d167f AF |
2943 | { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) }, |
2944 | { "sr0", offsetof(CPUPPCState, sr[0]) }, | |
2945 | { "sr1", offsetof(CPUPPCState, sr[1]) }, | |
2946 | { "sr2", offsetof(CPUPPCState, sr[2]) }, | |
2947 | { "sr3", offsetof(CPUPPCState, sr[3]) }, | |
2948 | { "sr4", offsetof(CPUPPCState, sr[4]) }, | |
2949 | { "sr5", offsetof(CPUPPCState, sr[5]) }, | |
2950 | { "sr6", offsetof(CPUPPCState, sr[6]) }, | |
2951 | { "sr7", offsetof(CPUPPCState, sr[7]) }, | |
2952 | { "sr8", offsetof(CPUPPCState, sr[8]) }, | |
2953 | { "sr9", offsetof(CPUPPCState, sr[9]) }, | |
2954 | { "sr10", offsetof(CPUPPCState, sr[10]) }, | |
2955 | { "sr11", offsetof(CPUPPCState, sr[11]) }, | |
2956 | { "sr12", offsetof(CPUPPCState, sr[12]) }, | |
2957 | { "sr13", offsetof(CPUPPCState, sr[13]) }, | |
2958 | { "sr14", offsetof(CPUPPCState, sr[14]) }, | |
2959 | { "sr15", offsetof(CPUPPCState, sr[15]) }, | |
90dc8812 | 2960 | /* Too lazy to put BATs... */ |
e59d167f AF |
2961 | { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) }, |
2962 | ||
2963 | { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) }, | |
2964 | { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) }, | |
2965 | { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) }, | |
2966 | { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) }, | |
2967 | { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) }, | |
2968 | { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) }, | |
2969 | { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) }, | |
2970 | { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) }, | |
2971 | { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) }, | |
2972 | { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) }, | |
2973 | { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) }, | |
2974 | { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) }, | |
2975 | { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) }, | |
2976 | { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) }, | |
2977 | { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) }, | |
2978 | { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) }, | |
2979 | { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) }, | |
2980 | { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) }, | |
2981 | { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) }, | |
2982 | { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) }, | |
2983 | { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) }, | |
2984 | { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) }, | |
2985 | { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) }, | |
2986 | { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) }, | |
2987 | { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) }, | |
2988 | { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) }, | |
2989 | { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) }, | |
2990 | { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) }, | |
2991 | { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) }, | |
2992 | { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) }, | |
2993 | { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) }, | |
2994 | { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) }, | |
2995 | { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) }, | |
2996 | { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) }, | |
2997 | { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) }, | |
2998 | { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) }, | |
2999 | { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) }, | |
3000 | { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) }, | |
3001 | { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) }, | |
3002 | { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) }, | |
3003 | { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) }, | |
3004 | { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) }, | |
3005 | { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) }, | |
3006 | { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) }, | |
3007 | { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) }, | |
3008 | { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) }, | |
3009 | { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) }, | |
3010 | { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) }, | |
3011 | { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) }, | |
3012 | { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) }, | |
3013 | { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) }, | |
3014 | { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) }, | |
3015 | { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) }, | |
3016 | { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) }, | |
3017 | { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) }, | |
3018 | { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) }, | |
3019 | { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) }, | |
3020 | { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) }, | |
3021 | { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) }, | |
3022 | { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) }, | |
3023 | { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) }, | |
3024 | { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) }, | |
3025 | { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) }, | |
3026 | { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) }, | |
3027 | { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) }, | |
3028 | { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) }, | |
90dc8812 | 3029 | |
e95c8d51 | 3030 | #elif defined(TARGET_SPARC) |
e59d167f AF |
3031 | { "g0", offsetof(CPUSPARCState, gregs[0]) }, |
3032 | { "g1", offsetof(CPUSPARCState, gregs[1]) }, | |
3033 | { "g2", offsetof(CPUSPARCState, gregs[2]) }, | |
3034 | { "g3", offsetof(CPUSPARCState, gregs[3]) }, | |
3035 | { "g4", offsetof(CPUSPARCState, gregs[4]) }, | |
3036 | { "g5", offsetof(CPUSPARCState, gregs[5]) }, | |
3037 | { "g6", offsetof(CPUSPARCState, gregs[6]) }, | |
3038 | { "g7", offsetof(CPUSPARCState, gregs[7]) }, | |
e95c8d51 FB |
3039 | { "o0", 0, monitor_get_reg }, |
3040 | { "o1", 1, monitor_get_reg }, | |
3041 | { "o2", 2, monitor_get_reg }, | |
3042 | { "o3", 3, monitor_get_reg }, | |
3043 | { "o4", 4, monitor_get_reg }, | |
3044 | { "o5", 5, monitor_get_reg }, | |
3045 | { "o6", 6, monitor_get_reg }, | |
3046 | { "o7", 7, monitor_get_reg }, | |
3047 | { "l0", 8, monitor_get_reg }, | |
3048 | { "l1", 9, monitor_get_reg }, | |
3049 | { "l2", 10, monitor_get_reg }, | |
3050 | { "l3", 11, monitor_get_reg }, | |
3051 | { "l4", 12, monitor_get_reg }, | |
3052 | { "l5", 13, monitor_get_reg }, | |
3053 | { "l6", 14, monitor_get_reg }, | |
3054 | { "l7", 15, monitor_get_reg }, | |
3055 | { "i0", 16, monitor_get_reg }, | |
3056 | { "i1", 17, monitor_get_reg }, | |
3057 | { "i2", 18, monitor_get_reg }, | |
3058 | { "i3", 19, monitor_get_reg }, | |
3059 | { "i4", 20, monitor_get_reg }, | |
3060 | { "i5", 21, monitor_get_reg }, | |
3061 | { "i6", 22, monitor_get_reg }, | |
3062 | { "i7", 23, monitor_get_reg }, | |
e59d167f AF |
3063 | { "pc", offsetof(CPUSPARCState, pc) }, |
3064 | { "npc", offsetof(CPUSPARCState, npc) }, | |
3065 | { "y", offsetof(CPUSPARCState, y) }, | |
7b936c0c | 3066 | #ifndef TARGET_SPARC64 |
e95c8d51 | 3067 | { "psr", 0, &monitor_get_psr, }, |
e59d167f | 3068 | { "wim", offsetof(CPUSPARCState, wim) }, |
7b936c0c | 3069 | #endif |
e59d167f AF |
3070 | { "tbr", offsetof(CPUSPARCState, tbr) }, |
3071 | { "fsr", offsetof(CPUSPARCState, fsr) }, | |
3072 | { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) }, | |
3073 | { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) }, | |
3074 | { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) }, | |
3075 | { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) }, | |
3076 | { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) }, | |
3077 | { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) }, | |
3078 | { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) }, | |
3079 | { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) }, | |
3080 | { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) }, | |
3081 | { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) }, | |
3082 | { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) }, | |
3083 | { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) }, | |
3084 | { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) }, | |
3085 | { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) }, | |
3086 | { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) }, | |
3087 | { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) }, | |
3088 | { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) }, | |
3089 | { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) }, | |
3090 | { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) }, | |
3091 | { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) }, | |
3092 | { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) }, | |
3093 | { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) }, | |
3094 | { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) }, | |
3095 | { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) }, | |
3096 | { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) }, | |
3097 | { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) }, | |
3098 | { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) }, | |
3099 | { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) }, | |
3100 | { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) }, | |
3101 | { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) }, | |
3102 | { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) }, | |
3103 | { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) }, | |
7b936c0c | 3104 | #ifdef TARGET_SPARC64 |
e59d167f AF |
3105 | { "f32", offsetof(CPUSPARCState, fpr[16]) }, |
3106 | { "f34", offsetof(CPUSPARCState, fpr[17]) }, | |
3107 | { "f36", offsetof(CPUSPARCState, fpr[18]) }, | |
3108 | { "f38", offsetof(CPUSPARCState, fpr[19]) }, | |
3109 | { "f40", offsetof(CPUSPARCState, fpr[20]) }, | |
3110 | { "f42", offsetof(CPUSPARCState, fpr[21]) }, | |
3111 | { "f44", offsetof(CPUSPARCState, fpr[22]) }, | |
3112 | { "f46", offsetof(CPUSPARCState, fpr[23]) }, | |
3113 | { "f48", offsetof(CPUSPARCState, fpr[24]) }, | |
3114 | { "f50", offsetof(CPUSPARCState, fpr[25]) }, | |
3115 | { "f52", offsetof(CPUSPARCState, fpr[26]) }, | |
3116 | { "f54", offsetof(CPUSPARCState, fpr[27]) }, | |
3117 | { "f56", offsetof(CPUSPARCState, fpr[28]) }, | |
3118 | { "f58", offsetof(CPUSPARCState, fpr[29]) }, | |
3119 | { "f60", offsetof(CPUSPARCState, fpr[30]) }, | |
3120 | { "f62", offsetof(CPUSPARCState, fpr[31]) }, | |
3121 | { "asi", offsetof(CPUSPARCState, asi) }, | |
3122 | { "pstate", offsetof(CPUSPARCState, pstate) }, | |
3123 | { "cansave", offsetof(CPUSPARCState, cansave) }, | |
3124 | { "canrestore", offsetof(CPUSPARCState, canrestore) }, | |
3125 | { "otherwin", offsetof(CPUSPARCState, otherwin) }, | |
3126 | { "wstate", offsetof(CPUSPARCState, wstate) }, | |
3127 | { "cleanwin", offsetof(CPUSPARCState, cleanwin) }, | |
3128 | { "fprs", offsetof(CPUSPARCState, fprs) }, | |
7b936c0c | 3129 | #endif |
9307c4c1 FB |
3130 | #endif |
3131 | { NULL }, | |
3132 | }; | |
3133 | ||
376253ec | 3134 | static void expr_error(Monitor *mon, const char *msg) |
9dc39cba | 3135 | { |
376253ec | 3136 | monitor_printf(mon, "%s\n", msg); |
9307c4c1 FB |
3137 | longjmp(expr_env, 1); |
3138 | } | |
3139 | ||
09b9418c | 3140 | /* return 0 if OK, -1 if not found */ |
92a31b1f | 3141 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 3142 | { |
8662d656 | 3143 | const MonitorDef *md; |
92a31b1f FB |
3144 | void *ptr; |
3145 | ||
9307c4c1 FB |
3146 | for(md = monitor_defs; md->name != NULL; md++) { |
3147 | if (compare_cmd(name, md->name)) { | |
3148 | if (md->get_value) { | |
e95c8d51 | 3149 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 3150 | } else { |
9349b4f9 | 3151 | CPUArchState *env = mon_get_cpu(); |
6a00d601 | 3152 | ptr = (uint8_t *)env + md->offset; |
92a31b1f FB |
3153 | switch(md->type) { |
3154 | case MD_I32: | |
3155 | *pval = *(int32_t *)ptr; | |
3156 | break; | |
3157 | case MD_TLONG: | |
3158 | *pval = *(target_long *)ptr; | |
3159 | break; | |
3160 | default: | |
3161 | *pval = 0; | |
3162 | break; | |
3163 | } | |
9307c4c1 FB |
3164 | } |
3165 | return 0; | |
3166 | } | |
3167 | } | |
3168 | return -1; | |
3169 | } | |
3170 | ||
3171 | static void next(void) | |
3172 | { | |
660f11be | 3173 | if (*pch != '\0') { |
9307c4c1 | 3174 | pch++; |
cd390083 | 3175 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3176 | pch++; |
3177 | } | |
3178 | } | |
3179 | ||
376253ec | 3180 | static int64_t expr_sum(Monitor *mon); |
9307c4c1 | 3181 | |
376253ec | 3182 | static int64_t expr_unary(Monitor *mon) |
9307c4c1 | 3183 | { |
c2efc95d | 3184 | int64_t n; |
9307c4c1 | 3185 | char *p; |
6a00d601 | 3186 | int ret; |
9307c4c1 FB |
3187 | |
3188 | switch(*pch) { | |
3189 | case '+': | |
3190 | next(); | |
376253ec | 3191 | n = expr_unary(mon); |
9307c4c1 FB |
3192 | break; |
3193 | case '-': | |
3194 | next(); | |
376253ec | 3195 | n = -expr_unary(mon); |
9307c4c1 FB |
3196 | break; |
3197 | case '~': | |
3198 | next(); | |
376253ec | 3199 | n = ~expr_unary(mon); |
9307c4c1 FB |
3200 | break; |
3201 | case '(': | |
3202 | next(); | |
376253ec | 3203 | n = expr_sum(mon); |
9307c4c1 | 3204 | if (*pch != ')') { |
376253ec | 3205 | expr_error(mon, "')' expected"); |
9307c4c1 FB |
3206 | } |
3207 | next(); | |
3208 | break; | |
81d0912d FB |
3209 | case '\'': |
3210 | pch++; | |
3211 | if (*pch == '\0') | |
376253ec | 3212 | expr_error(mon, "character constant expected"); |
81d0912d FB |
3213 | n = *pch; |
3214 | pch++; | |
3215 | if (*pch != '\'') | |
376253ec | 3216 | expr_error(mon, "missing terminating \' character"); |
81d0912d FB |
3217 | next(); |
3218 | break; | |
9307c4c1 FB |
3219 | case '$': |
3220 | { | |
3221 | char buf[128], *q; | |
69b34976 | 3222 | target_long reg=0; |
3b46e624 | 3223 | |
9307c4c1 FB |
3224 | pch++; |
3225 | q = buf; | |
3226 | while ((*pch >= 'a' && *pch <= 'z') || | |
3227 | (*pch >= 'A' && *pch <= 'Z') || | |
3228 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 3229 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
3230 | if ((q - buf) < sizeof(buf) - 1) |
3231 | *q++ = *pch; | |
3232 | pch++; | |
3233 | } | |
cd390083 | 3234 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3235 | pch++; |
3236 | *q = 0; | |
7743e588 | 3237 | ret = get_monitor_def(®, buf); |
09b9418c | 3238 | if (ret < 0) |
376253ec | 3239 | expr_error(mon, "unknown register"); |
7743e588 | 3240 | n = reg; |
9307c4c1 FB |
3241 | } |
3242 | break; | |
3243 | case '\0': | |
376253ec | 3244 | expr_error(mon, "unexpected end of expression"); |
9307c4c1 FB |
3245 | n = 0; |
3246 | break; | |
3247 | default: | |
6b0e33be | 3248 | errno = 0; |
7743e588 | 3249 | #if TARGET_PHYS_ADDR_BITS > 32 |
4f4fbf77 FB |
3250 | n = strtoull(pch, &p, 0); |
3251 | #else | |
9307c4c1 | 3252 | n = strtoul(pch, &p, 0); |
4f4fbf77 | 3253 | #endif |
6b0e33be LC |
3254 | if (errno == ERANGE) { |
3255 | expr_error(mon, "number too large"); | |
3256 | } | |
9307c4c1 | 3257 | if (pch == p) { |
376253ec | 3258 | expr_error(mon, "invalid char in expression"); |
9307c4c1 FB |
3259 | } |
3260 | pch = p; | |
cd390083 | 3261 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3262 | pch++; |
3263 | break; | |
3264 | } | |
3265 | return n; | |
3266 | } | |
3267 | ||
3268 | ||
376253ec | 3269 | static int64_t expr_prod(Monitor *mon) |
9307c4c1 | 3270 | { |
c2efc95d | 3271 | int64_t val, val2; |
92a31b1f | 3272 | int op; |
3b46e624 | 3273 | |
376253ec | 3274 | val = expr_unary(mon); |
9307c4c1 FB |
3275 | for(;;) { |
3276 | op = *pch; | |
3277 | if (op != '*' && op != '/' && op != '%') | |
3278 | break; | |
3279 | next(); | |
376253ec | 3280 | val2 = expr_unary(mon); |
9307c4c1 FB |
3281 | switch(op) { |
3282 | default: | |
3283 | case '*': | |
3284 | val *= val2; | |
3285 | break; | |
3286 | case '/': | |
3287 | case '%': | |
5fafdf24 | 3288 | if (val2 == 0) |
376253ec | 3289 | expr_error(mon, "division by zero"); |
9307c4c1 FB |
3290 | if (op == '/') |
3291 | val /= val2; | |
3292 | else | |
3293 | val %= val2; | |
3294 | break; | |
3295 | } | |
3296 | } | |
3297 | return val; | |
3298 | } | |
3299 | ||
376253ec | 3300 | static int64_t expr_logic(Monitor *mon) |
9307c4c1 | 3301 | { |
c2efc95d | 3302 | int64_t val, val2; |
92a31b1f | 3303 | int op; |
9307c4c1 | 3304 | |
376253ec | 3305 | val = expr_prod(mon); |
9307c4c1 FB |
3306 | for(;;) { |
3307 | op = *pch; | |
3308 | if (op != '&' && op != '|' && op != '^') | |
3309 | break; | |
3310 | next(); | |
376253ec | 3311 | val2 = expr_prod(mon); |
9307c4c1 FB |
3312 | switch(op) { |
3313 | default: | |
3314 | case '&': | |
3315 | val &= val2; | |
3316 | break; | |
3317 | case '|': | |
3318 | val |= val2; | |
3319 | break; | |
3320 | case '^': | |
3321 | val ^= val2; | |
3322 | break; | |
3323 | } | |
3324 | } | |
3325 | return val; | |
3326 | } | |
3327 | ||
376253ec | 3328 | static int64_t expr_sum(Monitor *mon) |
9307c4c1 | 3329 | { |
c2efc95d | 3330 | int64_t val, val2; |
92a31b1f | 3331 | int op; |
9307c4c1 | 3332 | |
376253ec | 3333 | val = expr_logic(mon); |
9307c4c1 FB |
3334 | for(;;) { |
3335 | op = *pch; | |
3336 | if (op != '+' && op != '-') | |
3337 | break; | |
3338 | next(); | |
376253ec | 3339 | val2 = expr_logic(mon); |
9307c4c1 FB |
3340 | if (op == '+') |
3341 | val += val2; | |
3342 | else | |
3343 | val -= val2; | |
3344 | } | |
3345 | return val; | |
3346 | } | |
3347 | ||
376253ec | 3348 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
9307c4c1 FB |
3349 | { |
3350 | pch = *pp; | |
3351 | if (setjmp(expr_env)) { | |
3352 | *pp = pch; | |
3353 | return -1; | |
3354 | } | |
cd390083 | 3355 | while (qemu_isspace(*pch)) |
9307c4c1 | 3356 | pch++; |
376253ec | 3357 | *pval = expr_sum(mon); |
9307c4c1 FB |
3358 | *pp = pch; |
3359 | return 0; | |
3360 | } | |
3361 | ||
3350a4dd MA |
3362 | static int get_double(Monitor *mon, double *pval, const char **pp) |
3363 | { | |
3364 | const char *p = *pp; | |
3365 | char *tailp; | |
3366 | double d; | |
3367 | ||
3368 | d = strtod(p, &tailp); | |
3369 | if (tailp == p) { | |
3370 | monitor_printf(mon, "Number expected\n"); | |
3371 | return -1; | |
3372 | } | |
3373 | if (d != d || d - d != 0) { | |
3374 | /* NaN or infinity */ | |
3375 | monitor_printf(mon, "Bad number\n"); | |
3376 | return -1; | |
3377 | } | |
3378 | *pval = d; | |
3379 | *pp = tailp; | |
3380 | return 0; | |
3381 | } | |
3382 | ||
9307c4c1 FB |
3383 | static int get_str(char *buf, int buf_size, const char **pp) |
3384 | { | |
3385 | const char *p; | |
3386 | char *q; | |
3387 | int c; | |
3388 | ||
81d0912d | 3389 | q = buf; |
9307c4c1 | 3390 | p = *pp; |
cd390083 | 3391 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3392 | p++; |
3393 | if (*p == '\0') { | |
3394 | fail: | |
81d0912d | 3395 | *q = '\0'; |
9307c4c1 FB |
3396 | *pp = p; |
3397 | return -1; | |
3398 | } | |
9307c4c1 FB |
3399 | if (*p == '\"') { |
3400 | p++; | |
3401 | while (*p != '\0' && *p != '\"') { | |
3402 | if (*p == '\\') { | |
3403 | p++; | |
3404 | c = *p++; | |
3405 | switch(c) { | |
3406 | case 'n': | |
3407 | c = '\n'; | |
3408 | break; | |
3409 | case 'r': | |
3410 | c = '\r'; | |
3411 | break; | |
3412 | case '\\': | |
3413 | case '\'': | |
3414 | case '\"': | |
3415 | break; | |
3416 | default: | |
3417 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
3418 | goto fail; | |
3419 | } | |
3420 | if ((q - buf) < buf_size - 1) { | |
3421 | *q++ = c; | |
3422 | } | |
3423 | } else { | |
3424 | if ((q - buf) < buf_size - 1) { | |
3425 | *q++ = *p; | |
3426 | } | |
3427 | p++; | |
3428 | } | |
3429 | } | |
3430 | if (*p != '\"') { | |
5b60212f | 3431 | qemu_printf("unterminated string\n"); |
9307c4c1 FB |
3432 | goto fail; |
3433 | } | |
3434 | p++; | |
3435 | } else { | |
cd390083 | 3436 | while (*p != '\0' && !qemu_isspace(*p)) { |
9307c4c1 FB |
3437 | if ((q - buf) < buf_size - 1) { |
3438 | *q++ = *p; | |
3439 | } | |
3440 | p++; | |
3441 | } | |
9307c4c1 | 3442 | } |
81d0912d | 3443 | *q = '\0'; |
9307c4c1 FB |
3444 | *pp = p; |
3445 | return 0; | |
3446 | } | |
3447 | ||
4590fd80 LC |
3448 | /* |
3449 | * Store the command-name in cmdname, and return a pointer to | |
3450 | * the remaining of the command string. | |
3451 | */ | |
3452 | static const char *get_command_name(const char *cmdline, | |
3453 | char *cmdname, size_t nlen) | |
3454 | { | |
3455 | size_t len; | |
3456 | const char *p, *pstart; | |
3457 | ||
3458 | p = cmdline; | |
3459 | while (qemu_isspace(*p)) | |
3460 | p++; | |
3461 | if (*p == '\0') | |
3462 | return NULL; | |
3463 | pstart = p; | |
3464 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | |
3465 | p++; | |
3466 | len = p - pstart; | |
3467 | if (len > nlen - 1) | |
3468 | len = nlen - 1; | |
3469 | memcpy(cmdname, pstart, len); | |
3470 | cmdname[len] = '\0'; | |
3471 | return p; | |
3472 | } | |
3473 | ||
4d76d2ba LC |
3474 | /** |
3475 | * Read key of 'type' into 'key' and return the current | |
3476 | * 'type' pointer. | |
3477 | */ | |
3478 | static char *key_get_info(const char *type, char **key) | |
3479 | { | |
3480 | size_t len; | |
3481 | char *p, *str; | |
3482 | ||
3483 | if (*type == ',') | |
3484 | type++; | |
3485 | ||
3486 | p = strchr(type, ':'); | |
3487 | if (!p) { | |
3488 | *key = NULL; | |
3489 | return NULL; | |
3490 | } | |
3491 | len = p - type; | |
3492 | ||
7267c094 | 3493 | str = g_malloc(len + 1); |
4d76d2ba LC |
3494 | memcpy(str, type, len); |
3495 | str[len] = '\0'; | |
3496 | ||
3497 | *key = str; | |
3498 | return ++p; | |
3499 | } | |
3500 | ||
9307c4c1 FB |
3501 | static int default_fmt_format = 'x'; |
3502 | static int default_fmt_size = 4; | |
3503 | ||
3504 | #define MAX_ARGS 16 | |
3505 | ||
fbc3d96c | 3506 | static int is_valid_option(const char *c, const char *typestr) |
3507 | { | |
3508 | char option[3]; | |
3509 | ||
3510 | option[0] = '-'; | |
3511 | option[1] = *c; | |
3512 | option[2] = '\0'; | |
3513 | ||
3514 | typestr = strstr(typestr, option); | |
3515 | return (typestr != NULL); | |
3516 | } | |
3517 | ||
945c5ac8 LC |
3518 | static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table, |
3519 | const char *cmdname) | |
7fd669a1 LC |
3520 | { |
3521 | const mon_cmd_t *cmd; | |
3522 | ||
945c5ac8 | 3523 | for (cmd = disp_table; cmd->name != NULL; cmd++) { |
7fd669a1 LC |
3524 | if (compare_cmd(cmdname, cmd->name)) { |
3525 | return cmd; | |
3526 | } | |
3527 | } | |
3528 | ||
3529 | return NULL; | |
3530 | } | |
3531 | ||
945c5ac8 LC |
3532 | static const mon_cmd_t *monitor_find_command(const char *cmdname) |
3533 | { | |
3534 | return search_dispatch_table(mon_cmds, cmdname); | |
3535 | } | |
3536 | ||
bead3ce1 LC |
3537 | static const mon_cmd_t *qmp_find_cmd(const char *cmdname) |
3538 | { | |
f36b4afb | 3539 | return search_dispatch_table(qmp_cmds, cmdname); |
bead3ce1 LC |
3540 | } |
3541 | ||
c227f099 | 3542 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
55f81d96 | 3543 | const char *cmdline, |
55f81d96 | 3544 | QDict *qdict) |
9307c4c1 | 3545 | { |
4590fd80 | 3546 | const char *p, *typestr; |
53773581 | 3547 | int c; |
c227f099 | 3548 | const mon_cmd_t *cmd; |
9307c4c1 FB |
3549 | char cmdname[256]; |
3550 | char buf[1024]; | |
4d76d2ba | 3551 | char *key; |
9dc39cba FB |
3552 | |
3553 | #ifdef DEBUG | |
376253ec | 3554 | monitor_printf(mon, "command='%s'\n", cmdline); |
9dc39cba | 3555 | #endif |
3b46e624 | 3556 | |
9307c4c1 | 3557 | /* extract the command name */ |
4590fd80 LC |
3558 | p = get_command_name(cmdline, cmdname, sizeof(cmdname)); |
3559 | if (!p) | |
55f81d96 | 3560 | return NULL; |
3b46e624 | 3561 | |
7fd669a1 LC |
3562 | cmd = monitor_find_command(cmdname); |
3563 | if (!cmd) { | |
d91d9bf6 | 3564 | monitor_printf(mon, "unknown command: '%s'\n", cmdname); |
55f81d96 | 3565 | return NULL; |
9307c4c1 | 3566 | } |
9307c4c1 | 3567 | |
9307c4c1 FB |
3568 | /* parse the parameters */ |
3569 | typestr = cmd->args_type; | |
9dc39cba | 3570 | for(;;) { |
4d76d2ba LC |
3571 | typestr = key_get_info(typestr, &key); |
3572 | if (!typestr) | |
9dc39cba | 3573 | break; |
4d76d2ba | 3574 | c = *typestr; |
9307c4c1 FB |
3575 | typestr++; |
3576 | switch(c) { | |
3577 | case 'F': | |
81d0912d | 3578 | case 'B': |
9307c4c1 FB |
3579 | case 's': |
3580 | { | |
3581 | int ret; | |
3b46e624 | 3582 | |
cd390083 | 3583 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3584 | p++; |
3585 | if (*typestr == '?') { | |
3586 | typestr++; | |
3587 | if (*p == '\0') { | |
3588 | /* no optional string: NULL argument */ | |
53773581 | 3589 | break; |
9307c4c1 FB |
3590 | } |
3591 | } | |
3592 | ret = get_str(buf, sizeof(buf), &p); | |
3593 | if (ret < 0) { | |
81d0912d FB |
3594 | switch(c) { |
3595 | case 'F': | |
376253ec AL |
3596 | monitor_printf(mon, "%s: filename expected\n", |
3597 | cmdname); | |
81d0912d FB |
3598 | break; |
3599 | case 'B': | |
376253ec AL |
3600 | monitor_printf(mon, "%s: block device name expected\n", |
3601 | cmdname); | |
81d0912d FB |
3602 | break; |
3603 | default: | |
376253ec | 3604 | monitor_printf(mon, "%s: string expected\n", cmdname); |
81d0912d FB |
3605 | break; |
3606 | } | |
9307c4c1 FB |
3607 | goto fail; |
3608 | } | |
53773581 | 3609 | qdict_put(qdict, key, qstring_from_str(buf)); |
9307c4c1 | 3610 | } |
9dc39cba | 3611 | break; |
361127df MA |
3612 | case 'O': |
3613 | { | |
3614 | QemuOptsList *opts_list; | |
3615 | QemuOpts *opts; | |
3616 | ||
3617 | opts_list = qemu_find_opts(key); | |
3618 | if (!opts_list || opts_list->desc->name) { | |
3619 | goto bad_type; | |
3620 | } | |
3621 | while (qemu_isspace(*p)) { | |
3622 | p++; | |
3623 | } | |
3624 | if (!*p) | |
3625 | break; | |
3626 | if (get_str(buf, sizeof(buf), &p) < 0) { | |
3627 | goto fail; | |
3628 | } | |
3629 | opts = qemu_opts_parse(opts_list, buf, 1); | |
3630 | if (!opts) { | |
3631 | goto fail; | |
3632 | } | |
3633 | qemu_opts_to_qdict(opts, qdict); | |
3634 | qemu_opts_del(opts); | |
3635 | } | |
3636 | break; | |
9307c4c1 FB |
3637 | case '/': |
3638 | { | |
3639 | int count, format, size; | |
3b46e624 | 3640 | |
cd390083 | 3641 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3642 | p++; |
3643 | if (*p == '/') { | |
3644 | /* format found */ | |
3645 | p++; | |
3646 | count = 1; | |
cd390083 | 3647 | if (qemu_isdigit(*p)) { |
9307c4c1 | 3648 | count = 0; |
cd390083 | 3649 | while (qemu_isdigit(*p)) { |
9307c4c1 FB |
3650 | count = count * 10 + (*p - '0'); |
3651 | p++; | |
3652 | } | |
3653 | } | |
3654 | size = -1; | |
3655 | format = -1; | |
3656 | for(;;) { | |
3657 | switch(*p) { | |
3658 | case 'o': | |
3659 | case 'd': | |
3660 | case 'u': | |
3661 | case 'x': | |
3662 | case 'i': | |
3663 | case 'c': | |
3664 | format = *p++; | |
3665 | break; | |
3666 | case 'b': | |
3667 | size = 1; | |
3668 | p++; | |
3669 | break; | |
3670 | case 'h': | |
3671 | size = 2; | |
3672 | p++; | |
3673 | break; | |
3674 | case 'w': | |
3675 | size = 4; | |
3676 | p++; | |
3677 | break; | |
3678 | case 'g': | |
3679 | case 'L': | |
3680 | size = 8; | |
3681 | p++; | |
3682 | break; | |
3683 | default: | |
3684 | goto next; | |
3685 | } | |
3686 | } | |
3687 | next: | |
cd390083 | 3688 | if (*p != '\0' && !qemu_isspace(*p)) { |
376253ec AL |
3689 | monitor_printf(mon, "invalid char in format: '%c'\n", |
3690 | *p); | |
9307c4c1 FB |
3691 | goto fail; |
3692 | } | |
9307c4c1 FB |
3693 | if (format < 0) |
3694 | format = default_fmt_format; | |
4c27ba27 FB |
3695 | if (format != 'i') { |
3696 | /* for 'i', not specifying a size gives -1 as size */ | |
3697 | if (size < 0) | |
3698 | size = default_fmt_size; | |
e90f009b | 3699 | default_fmt_size = size; |
4c27ba27 | 3700 | } |
9307c4c1 FB |
3701 | default_fmt_format = format; |
3702 | } else { | |
3703 | count = 1; | |
3704 | format = default_fmt_format; | |
4c27ba27 FB |
3705 | if (format != 'i') { |
3706 | size = default_fmt_size; | |
3707 | } else { | |
3708 | size = -1; | |
3709 | } | |
9307c4c1 | 3710 | } |
f7188bbe LC |
3711 | qdict_put(qdict, "count", qint_from_int(count)); |
3712 | qdict_put(qdict, "format", qint_from_int(format)); | |
3713 | qdict_put(qdict, "size", qint_from_int(size)); | |
9307c4c1 | 3714 | } |
9dc39cba | 3715 | break; |
9307c4c1 | 3716 | case 'i': |
92a31b1f | 3717 | case 'l': |
b6e098d7 | 3718 | case 'M': |
9307c4c1 | 3719 | { |
c2efc95d | 3720 | int64_t val; |
7743e588 | 3721 | |
cd390083 | 3722 | while (qemu_isspace(*p)) |
9307c4c1 | 3723 | p++; |
3440557b | 3724 | if (*typestr == '?' || *typestr == '.') { |
3440557b | 3725 | if (*typestr == '?') { |
53773581 LC |
3726 | if (*p == '\0') { |
3727 | typestr++; | |
3728 | break; | |
3729 | } | |
3440557b FB |
3730 | } else { |
3731 | if (*p == '.') { | |
3732 | p++; | |
cd390083 | 3733 | while (qemu_isspace(*p)) |
3440557b | 3734 | p++; |
3440557b | 3735 | } else { |
53773581 LC |
3736 | typestr++; |
3737 | break; | |
3440557b FB |
3738 | } |
3739 | } | |
13224a87 | 3740 | typestr++; |
9307c4c1 | 3741 | } |
376253ec | 3742 | if (get_expr(mon, &val, &p)) |
9307c4c1 | 3743 | goto fail; |
675ebef9 LC |
3744 | /* Check if 'i' is greater than 32-bit */ |
3745 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { | |
3746 | monitor_printf(mon, "\'%s\' has failed: ", cmdname); | |
3747 | monitor_printf(mon, "integer is for 32-bit values\n"); | |
3748 | goto fail; | |
b6e098d7 | 3749 | } else if (c == 'M') { |
91162849 LC |
3750 | if (val < 0) { |
3751 | monitor_printf(mon, "enter a positive value\n"); | |
3752 | goto fail; | |
3753 | } | |
b6e098d7 | 3754 | val <<= 20; |
675ebef9 | 3755 | } |
53773581 | 3756 | qdict_put(qdict, key, qint_from_int(val)); |
9307c4c1 FB |
3757 | } |
3758 | break; | |
dbc0c67f JS |
3759 | case 'o': |
3760 | { | |
70b4f4bb | 3761 | int64_t val; |
dbc0c67f JS |
3762 | char *end; |
3763 | ||
3764 | while (qemu_isspace(*p)) { | |
3765 | p++; | |
3766 | } | |
3767 | if (*typestr == '?') { | |
3768 | typestr++; | |
3769 | if (*p == '\0') { | |
3770 | break; | |
3771 | } | |
3772 | } | |
3773 | val = strtosz(p, &end); | |
3774 | if (val < 0) { | |
3775 | monitor_printf(mon, "invalid size\n"); | |
3776 | goto fail; | |
3777 | } | |
3778 | qdict_put(qdict, key, qint_from_int(val)); | |
3779 | p = end; | |
3780 | } | |
3781 | break; | |
fccfb11e | 3782 | case 'T': |
3350a4dd MA |
3783 | { |
3784 | double val; | |
3785 | ||
3786 | while (qemu_isspace(*p)) | |
3787 | p++; | |
3788 | if (*typestr == '?') { | |
3789 | typestr++; | |
3790 | if (*p == '\0') { | |
3791 | break; | |
3792 | } | |
3793 | } | |
3794 | if (get_double(mon, &val, &p) < 0) { | |
3795 | goto fail; | |
3796 | } | |
07de3e60 | 3797 | if (p[0] && p[1] == 's') { |
fccfb11e MA |
3798 | switch (*p) { |
3799 | case 'm': | |
3800 | val /= 1e3; p += 2; break; | |
3801 | case 'u': | |
3802 | val /= 1e6; p += 2; break; | |
3803 | case 'n': | |
3804 | val /= 1e9; p += 2; break; | |
3805 | } | |
3806 | } | |
3350a4dd MA |
3807 | if (*p && !qemu_isspace(*p)) { |
3808 | monitor_printf(mon, "Unknown unit suffix\n"); | |
3809 | goto fail; | |
3810 | } | |
3811 | qdict_put(qdict, key, qfloat_from_double(val)); | |
3812 | } | |
3813 | break; | |
942cd1f2 MA |
3814 | case 'b': |
3815 | { | |
3816 | const char *beg; | |
3817 | int val; | |
3818 | ||
3819 | while (qemu_isspace(*p)) { | |
3820 | p++; | |
3821 | } | |
3822 | beg = p; | |
3823 | while (qemu_isgraph(*p)) { | |
3824 | p++; | |
3825 | } | |
3826 | if (p - beg == 2 && !memcmp(beg, "on", p - beg)) { | |
3827 | val = 1; | |
3828 | } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) { | |
3829 | val = 0; | |
3830 | } else { | |
3831 | monitor_printf(mon, "Expected 'on' or 'off'\n"); | |
3832 | goto fail; | |
3833 | } | |
3834 | qdict_put(qdict, key, qbool_from_int(val)); | |
3835 | } | |
3836 | break; | |
9307c4c1 FB |
3837 | case '-': |
3838 | { | |
fbc3d96c | 3839 | const char *tmp = p; |
eb159d13 | 3840 | int skip_key = 0; |
9307c4c1 | 3841 | /* option */ |
3b46e624 | 3842 | |
9307c4c1 FB |
3843 | c = *typestr++; |
3844 | if (c == '\0') | |
3845 | goto bad_type; | |
cd390083 | 3846 | while (qemu_isspace(*p)) |
9307c4c1 | 3847 | p++; |
9307c4c1 FB |
3848 | if (*p == '-') { |
3849 | p++; | |
fbc3d96c | 3850 | if(c != *p) { |
3851 | if(!is_valid_option(p, typestr)) { | |
3852 | ||
3853 | monitor_printf(mon, "%s: unsupported option -%c\n", | |
3854 | cmdname, *p); | |
3855 | goto fail; | |
3856 | } else { | |
3857 | skip_key = 1; | |
3858 | } | |
3859 | } | |
3860 | if(skip_key) { | |
3861 | p = tmp; | |
3862 | } else { | |
eb159d13 | 3863 | /* has option */ |
fbc3d96c | 3864 | p++; |
eb159d13 | 3865 | qdict_put(qdict, key, qbool_from_int(1)); |
9307c4c1 | 3866 | } |
9307c4c1 | 3867 | } |
9307c4c1 FB |
3868 | } |
3869 | break; | |
3870 | default: | |
3871 | bad_type: | |
376253ec | 3872 | monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c); |
9307c4c1 FB |
3873 | goto fail; |
3874 | } | |
7267c094 | 3875 | g_free(key); |
4d76d2ba | 3876 | key = NULL; |
9dc39cba | 3877 | } |
9307c4c1 | 3878 | /* check that all arguments were parsed */ |
cd390083 | 3879 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3880 | p++; |
3881 | if (*p != '\0') { | |
376253ec AL |
3882 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
3883 | cmdname); | |
9307c4c1 | 3884 | goto fail; |
9dc39cba | 3885 | } |
9307c4c1 | 3886 | |
55f81d96 | 3887 | return cmd; |
ac7531ec | 3888 | |
55f81d96 | 3889 | fail: |
7267c094 | 3890 | g_free(key); |
55f81d96 LC |
3891 | return NULL; |
3892 | } | |
3893 | ||
d6f46833 MA |
3894 | void monitor_set_error(Monitor *mon, QError *qerror) |
3895 | { | |
3896 | /* report only the first error */ | |
3897 | if (!mon->error) { | |
3898 | mon->error = qerror; | |
3899 | } else { | |
d6f46833 MA |
3900 | QDECREF(qerror); |
3901 | } | |
3902 | } | |
3903 | ||
bb89c2e9 LC |
3904 | static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret) |
3905 | { | |
6d441430 LC |
3906 | if (ret && !monitor_has_error(mon)) { |
3907 | /* | |
3908 | * If it returns failure, it must have passed on error. | |
3909 | * | |
3910 | * Action: Report an internal error to the client if in QMP. | |
3911 | */ | |
3912 | qerror_report(QERR_UNDEFINED_ERROR); | |
6d441430 | 3913 | } |
bb89c2e9 LC |
3914 | } |
3915 | ||
f3c157c4 | 3916 | static void handle_user_command(Monitor *mon, const char *cmdline) |
55f81d96 | 3917 | { |
55f81d96 | 3918 | QDict *qdict; |
c227f099 | 3919 | const mon_cmd_t *cmd; |
55f81d96 LC |
3920 | |
3921 | qdict = qdict_new(); | |
3922 | ||
590fb3b7 | 3923 | cmd = monitor_parse_command(mon, cmdline, qdict); |
13917bee LC |
3924 | if (!cmd) |
3925 | goto out; | |
3926 | ||
4903de0c | 3927 | if (handler_is_async(cmd)) { |
940cc30d | 3928 | user_async_cmd_handler(mon, cmd, qdict); |
9e80721e | 3929 | } else if (handler_is_qobject(cmd)) { |
de79ba6f LC |
3930 | QObject *data = NULL; |
3931 | ||
3932 | /* XXX: ignores the error code */ | |
3933 | cmd->mhandler.cmd_new(mon, qdict, &data); | |
3934 | assert(!monitor_has_error(mon)); | |
3935 | if (data) { | |
3936 | cmd->user_print(mon, data); | |
3937 | qobject_decref(data); | |
3938 | } | |
13917bee | 3939 | } else { |
af4ce882 | 3940 | cmd->mhandler.cmd(mon, qdict); |
55f81d96 LC |
3941 | } |
3942 | ||
13917bee | 3943 | out: |
f7188bbe | 3944 | QDECREF(qdict); |
9dc39cba FB |
3945 | } |
3946 | ||
81d0912d FB |
3947 | static void cmd_completion(const char *name, const char *list) |
3948 | { | |
3949 | const char *p, *pstart; | |
3950 | char cmd[128]; | |
3951 | int len; | |
3952 | ||
3953 | p = list; | |
3954 | for(;;) { | |
3955 | pstart = p; | |
3956 | p = strchr(p, '|'); | |
3957 | if (!p) | |
3958 | p = pstart + strlen(pstart); | |
3959 | len = p - pstart; | |
3960 | if (len > sizeof(cmd) - 2) | |
3961 | len = sizeof(cmd) - 2; | |
3962 | memcpy(cmd, pstart, len); | |
3963 | cmd[len] = '\0'; | |
3964 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
731b0364 | 3965 | readline_add_completion(cur_mon->rs, cmd); |
81d0912d FB |
3966 | } |
3967 | if (*p == '\0') | |
3968 | break; | |
3969 | p++; | |
3970 | } | |
3971 | } | |
3972 | ||
3973 | static void file_completion(const char *input) | |
3974 | { | |
3975 | DIR *ffs; | |
3976 | struct dirent *d; | |
3977 | char path[1024]; | |
3978 | char file[1024], file_prefix[1024]; | |
3979 | int input_path_len; | |
3980 | const char *p; | |
3981 | ||
5fafdf24 | 3982 | p = strrchr(input, '/'); |
81d0912d FB |
3983 | if (!p) { |
3984 | input_path_len = 0; | |
3985 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 3986 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
3987 | } else { |
3988 | input_path_len = p - input + 1; | |
3989 | memcpy(path, input, input_path_len); | |
3990 | if (input_path_len > sizeof(path) - 1) | |
3991 | input_path_len = sizeof(path) - 1; | |
3992 | path[input_path_len] = '\0'; | |
3993 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
3994 | } | |
3995 | #ifdef DEBUG_COMPLETION | |
376253ec AL |
3996 | monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n", |
3997 | input, path, file_prefix); | |
81d0912d FB |
3998 | #endif |
3999 | ffs = opendir(path); | |
4000 | if (!ffs) | |
4001 | return; | |
4002 | for(;;) { | |
4003 | struct stat sb; | |
4004 | d = readdir(ffs); | |
4005 | if (!d) | |
4006 | break; | |
46c7fc18 KK |
4007 | |
4008 | if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) { | |
4009 | continue; | |
4010 | } | |
4011 | ||
81d0912d FB |
4012 | if (strstart(d->d_name, file_prefix, NULL)) { |
4013 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
4014 | if (input_path_len < sizeof(file)) |
4015 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
4016 | d->d_name); | |
81d0912d FB |
4017 | /* stat the file to find out if it's a directory. |
4018 | * In that case add a slash to speed up typing long paths | |
4019 | */ | |
c951d9a6 | 4020 | if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
363a37d5 | 4021 | pstrcat(file, sizeof(file), "/"); |
c951d9a6 | 4022 | } |
731b0364 | 4023 | readline_add_completion(cur_mon->rs, file); |
81d0912d FB |
4024 | } |
4025 | } | |
4026 | closedir(ffs); | |
4027 | } | |
4028 | ||
51de9760 | 4029 | static void block_completion_it(void *opaque, BlockDriverState *bs) |
81d0912d | 4030 | { |
51de9760 | 4031 | const char *name = bdrv_get_device_name(bs); |
81d0912d FB |
4032 | const char *input = opaque; |
4033 | ||
4034 | if (input[0] == '\0' || | |
4035 | !strncmp(name, (char *)input, strlen(input))) { | |
731b0364 | 4036 | readline_add_completion(cur_mon->rs, name); |
81d0912d FB |
4037 | } |
4038 | } | |
4039 | ||
4040 | /* NOTE: this parser is an approximate form of the real command parser */ | |
4041 | static void parse_cmdline(const char *cmdline, | |
4042 | int *pnb_args, char **args) | |
4043 | { | |
4044 | const char *p; | |
4045 | int nb_args, ret; | |
4046 | char buf[1024]; | |
4047 | ||
4048 | p = cmdline; | |
4049 | nb_args = 0; | |
4050 | for(;;) { | |
cd390083 | 4051 | while (qemu_isspace(*p)) |
81d0912d FB |
4052 | p++; |
4053 | if (*p == '\0') | |
4054 | break; | |
4055 | if (nb_args >= MAX_ARGS) | |
4056 | break; | |
4057 | ret = get_str(buf, sizeof(buf), &p); | |
7267c094 | 4058 | args[nb_args] = g_strdup(buf); |
81d0912d FB |
4059 | nb_args++; |
4060 | if (ret < 0) | |
4061 | break; | |
4062 | } | |
4063 | *pnb_args = nb_args; | |
4064 | } | |
4065 | ||
4d76d2ba LC |
4066 | static const char *next_arg_type(const char *typestr) |
4067 | { | |
4068 | const char *p = strchr(typestr, ':'); | |
4069 | return (p != NULL ? ++p : typestr); | |
4070 | } | |
4071 | ||
4c36ba32 | 4072 | static void monitor_find_completion(const char *cmdline) |
81d0912d FB |
4073 | { |
4074 | const char *cmdname; | |
4075 | char *args[MAX_ARGS]; | |
4076 | int nb_args, i, len; | |
4077 | const char *ptype, *str; | |
c227f099 | 4078 | const mon_cmd_t *cmd; |
81d0912d FB |
4079 | |
4080 | parse_cmdline(cmdline, &nb_args, args); | |
4081 | #ifdef DEBUG_COMPLETION | |
4082 | for(i = 0; i < nb_args; i++) { | |
376253ec | 4083 | monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]); |
81d0912d FB |
4084 | } |
4085 | #endif | |
4086 | ||
4087 | /* if the line ends with a space, it means we want to complete the | |
4088 | next arg */ | |
4089 | len = strlen(cmdline); | |
cd390083 | 4090 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { |
03a63484 JK |
4091 | if (nb_args >= MAX_ARGS) { |
4092 | goto cleanup; | |
4093 | } | |
7267c094 | 4094 | args[nb_args++] = g_strdup(""); |
81d0912d FB |
4095 | } |
4096 | if (nb_args <= 1) { | |
4097 | /* command completion */ | |
4098 | if (nb_args == 0) | |
4099 | cmdname = ""; | |
4100 | else | |
4101 | cmdname = args[0]; | |
731b0364 | 4102 | readline_set_completion_index(cur_mon->rs, strlen(cmdname)); |
376253ec | 4103 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
4104 | cmd_completion(cmdname, cmd->name); |
4105 | } | |
4106 | } else { | |
4107 | /* find the command */ | |
03a63484 JK |
4108 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { |
4109 | if (compare_cmd(args[0], cmd->name)) { | |
4110 | break; | |
4111 | } | |
81d0912d | 4112 | } |
03a63484 JK |
4113 | if (!cmd->name) { |
4114 | goto cleanup; | |
4115 | } | |
4116 | ||
4d76d2ba | 4117 | ptype = next_arg_type(cmd->args_type); |
81d0912d FB |
4118 | for(i = 0; i < nb_args - 2; i++) { |
4119 | if (*ptype != '\0') { | |
4d76d2ba | 4120 | ptype = next_arg_type(ptype); |
81d0912d | 4121 | while (*ptype == '?') |
4d76d2ba | 4122 | ptype = next_arg_type(ptype); |
81d0912d FB |
4123 | } |
4124 | } | |
4125 | str = args[nb_args - 1]; | |
2a1704a7 | 4126 | if (*ptype == '-' && ptype[1] != '\0') { |
3b6dbf27 | 4127 | ptype = next_arg_type(ptype); |
2a1704a7 | 4128 | } |
81d0912d FB |
4129 | switch(*ptype) { |
4130 | case 'F': | |
4131 | /* file completion */ | |
731b0364 | 4132 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
4133 | file_completion(str); |
4134 | break; | |
4135 | case 'B': | |
4136 | /* block device name completion */ | |
731b0364 | 4137 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
4138 | bdrv_iterate(block_completion_it, (void *)str); |
4139 | break; | |
7fe48483 FB |
4140 | case 's': |
4141 | /* XXX: more generic ? */ | |
4142 | if (!strcmp(cmd->name, "info")) { | |
731b0364 | 4143 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
7fe48483 FB |
4144 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
4145 | cmd_completion(str, cmd->name); | |
4146 | } | |
64866c3d | 4147 | } else if (!strcmp(cmd->name, "sendkey")) { |
e600d1ef BS |
4148 | char *sep = strrchr(str, '-'); |
4149 | if (sep) | |
4150 | str = sep + 1; | |
731b0364 | 4151 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
1048c88f AK |
4152 | for (i = 0; i < Q_KEY_CODE_MAX; i++) { |
4153 | cmd_completion(str, QKeyCode_lookup[i]); | |
64866c3d | 4154 | } |
f3353c6b JK |
4155 | } else if (!strcmp(cmd->name, "help|?")) { |
4156 | readline_set_completion_index(cur_mon->rs, strlen(str)); | |
4157 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
4158 | cmd_completion(str, cmd->name); | |
4159 | } | |
7fe48483 FB |
4160 | } |
4161 | break; | |
81d0912d FB |
4162 | default: |
4163 | break; | |
4164 | } | |
4165 | } | |
03a63484 JK |
4166 | |
4167 | cleanup: | |
4168 | for (i = 0; i < nb_args; i++) { | |
7267c094 | 4169 | g_free(args[i]); |
03a63484 | 4170 | } |
81d0912d FB |
4171 | } |
4172 | ||
731b0364 | 4173 | static int monitor_can_read(void *opaque) |
9dc39cba | 4174 | { |
731b0364 AL |
4175 | Monitor *mon = opaque; |
4176 | ||
c62313bb | 4177 | return (mon->suspend_cnt == 0) ? 1 : 0; |
9dc39cba FB |
4178 | } |
4179 | ||
09069b19 | 4180 | static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name) |
5fa737a4 | 4181 | { |
09069b19 LC |
4182 | int is_cap = compare_cmd(cmd_name, "qmp_capabilities"); |
4183 | return (qmp_cmd_mode(mon) ? is_cap : !is_cap); | |
5fa737a4 LC |
4184 | } |
4185 | ||
4af9193a LC |
4186 | /* |
4187 | * Argument validation rules: | |
4188 | * | |
4189 | * 1. The argument must exist in cmd_args qdict | |
4190 | * 2. The argument type must be the expected one | |
4191 | * | |
4192 | * Special case: If the argument doesn't exist in cmd_args and | |
4193 | * the QMP_ACCEPT_UNKNOWNS flag is set, then the | |
4194 | * checking is skipped for it. | |
4195 | */ | |
4196 | static int check_client_args_type(const QDict *client_args, | |
4197 | const QDict *cmd_args, int flags) | |
5fa737a4 | 4198 | { |
4af9193a | 4199 | const QDictEntry *ent; |
5fa737a4 | 4200 | |
4af9193a LC |
4201 | for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){ |
4202 | QObject *obj; | |
4203 | QString *arg_type; | |
4204 | const QObject *client_arg = qdict_entry_value(ent); | |
4205 | const char *client_arg_name = qdict_entry_key(ent); | |
4206 | ||
4207 | obj = qdict_get(cmd_args, client_arg_name); | |
4208 | if (!obj) { | |
4209 | if (flags & QMP_ACCEPT_UNKNOWNS) { | |
4210 | /* handler accepts unknowns */ | |
4211 | continue; | |
4212 | } | |
4213 | /* client arg doesn't exist */ | |
4214 | qerror_report(QERR_INVALID_PARAMETER, client_arg_name); | |
4215 | return -1; | |
4216 | } | |
5fa737a4 | 4217 | |
4af9193a LC |
4218 | arg_type = qobject_to_qstring(obj); |
4219 | assert(arg_type != NULL); | |
5fa737a4 | 4220 | |
4af9193a LC |
4221 | /* check if argument's type is correct */ |
4222 | switch (qstring_get_str(arg_type)[0]) { | |
5fa737a4 LC |
4223 | case 'F': |
4224 | case 'B': | |
4225 | case 's': | |
4af9193a LC |
4226 | if (qobject_type(client_arg) != QTYPE_QSTRING) { |
4227 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
4228 | "string"); | |
5fa737a4 LC |
4229 | return -1; |
4230 | } | |
4af9193a | 4231 | break; |
5fa737a4 LC |
4232 | case 'i': |
4233 | case 'l': | |
b6e098d7 | 4234 | case 'M': |
dbc0c67f | 4235 | case 'o': |
4af9193a LC |
4236 | if (qobject_type(client_arg) != QTYPE_QINT) { |
4237 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
4238 | "int"); | |
4239 | return -1; | |
5fa737a4 LC |
4240 | } |
4241 | break; | |
fccfb11e | 4242 | case 'T': |
4af9193a LC |
4243 | if (qobject_type(client_arg) != QTYPE_QINT && |
4244 | qobject_type(client_arg) != QTYPE_QFLOAT) { | |
4245 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
4246 | "number"); | |
4247 | return -1; | |
3350a4dd MA |
4248 | } |
4249 | break; | |
942cd1f2 | 4250 | case 'b': |
5fa737a4 | 4251 | case '-': |
4af9193a LC |
4252 | if (qobject_type(client_arg) != QTYPE_QBOOL) { |
4253 | qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name, | |
4254 | "bool"); | |
4255 | return -1; | |
5fa737a4 LC |
4256 | } |
4257 | break; | |
361127df | 4258 | case 'O': |
4af9193a LC |
4259 | assert(flags & QMP_ACCEPT_UNKNOWNS); |
4260 | break; | |
b9f8978c PB |
4261 | case 'q': |
4262 | /* Any QObject can be passed. */ | |
4263 | break; | |
4af9193a LC |
4264 | case '/': |
4265 | case '.': | |
4266 | /* | |
4267 | * These types are not supported by QMP and thus are not | |
4268 | * handled here. Fall through. | |
4269 | */ | |
5fa737a4 | 4270 | default: |
5fa737a4 | 4271 | abort(); |
4af9193a LC |
4272 | } |
4273 | } | |
4274 | ||
4275 | return 0; | |
4276 | } | |
4277 | ||
2dbc8db0 LC |
4278 | /* |
4279 | * - Check if the client has passed all mandatory args | |
4280 | * - Set special flags for argument validation | |
4281 | */ | |
4282 | static int check_mandatory_args(const QDict *cmd_args, | |
4283 | const QDict *client_args, int *flags) | |
4284 | { | |
4285 | const QDictEntry *ent; | |
4286 | ||
4287 | for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) { | |
4288 | const char *cmd_arg_name = qdict_entry_key(ent); | |
4289 | QString *type = qobject_to_qstring(qdict_entry_value(ent)); | |
4290 | assert(type != NULL); | |
4291 | ||
4292 | if (qstring_get_str(type)[0] == 'O') { | |
4293 | assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0); | |
4294 | *flags |= QMP_ACCEPT_UNKNOWNS; | |
4295 | } else if (qstring_get_str(type)[0] != '-' && | |
4296 | qstring_get_str(type)[1] != '?' && | |
4297 | !qdict_haskey(client_args, cmd_arg_name)) { | |
4298 | qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name); | |
4299 | return -1; | |
4300 | } | |
5fa737a4 LC |
4301 | } |
4302 | ||
4303 | return 0; | |
4304 | } | |
4305 | ||
2dbc8db0 | 4306 | static QDict *qdict_from_args_type(const char *args_type) |
5fa737a4 | 4307 | { |
2dbc8db0 LC |
4308 | int i; |
4309 | QDict *qdict; | |
4310 | QString *key, *type, *cur_qs; | |
4311 | ||
4312 | assert(args_type != NULL); | |
4313 | ||
4314 | qdict = qdict_new(); | |
4315 | ||
4316 | if (args_type == NULL || args_type[0] == '\0') { | |
4317 | /* no args, empty qdict */ | |
4318 | goto out; | |
4319 | } | |
4320 | ||
4321 | key = qstring_new(); | |
4322 | type = qstring_new(); | |
4323 | ||
4324 | cur_qs = key; | |
4325 | ||
4326 | for (i = 0;; i++) { | |
4327 | switch (args_type[i]) { | |
4328 | case ',': | |
4329 | case '\0': | |
4330 | qdict_put(qdict, qstring_get_str(key), type); | |
4331 | QDECREF(key); | |
4332 | if (args_type[i] == '\0') { | |
4333 | goto out; | |
4334 | } | |
4335 | type = qstring_new(); /* qdict has ref */ | |
4336 | cur_qs = key = qstring_new(); | |
4337 | break; | |
4338 | case ':': | |
4339 | cur_qs = type; | |
4340 | break; | |
4341 | default: | |
4342 | qstring_append_chr(cur_qs, args_type[i]); | |
4343 | break; | |
4344 | } | |
4345 | } | |
4346 | ||
4347 | out: | |
4348 | return qdict; | |
5fa737a4 LC |
4349 | } |
4350 | ||
2dbc8db0 LC |
4351 | /* |
4352 | * Client argument checking rules: | |
4353 | * | |
4354 | * 1. Client must provide all mandatory arguments | |
4af9193a LC |
4355 | * 2. Each argument provided by the client must be expected |
4356 | * 3. Each argument provided by the client must have the type expected | |
4357 | * by the command | |
2dbc8db0 LC |
4358 | */ |
4359 | static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args) | |
361127df | 4360 | { |
2dbc8db0 LC |
4361 | int flags, err; |
4362 | QDict *cmd_args; | |
4363 | ||
4364 | cmd_args = qdict_from_args_type(cmd->args_type); | |
4365 | ||
4366 | flags = 0; | |
4367 | err = check_mandatory_args(cmd_args, client_args, &flags); | |
4368 | if (err) { | |
4369 | goto out; | |
4370 | } | |
4371 | ||
4af9193a | 4372 | err = check_client_args_type(client_args, cmd_args, flags); |
2dbc8db0 LC |
4373 | |
4374 | out: | |
4375 | QDECREF(cmd_args); | |
4376 | return err; | |
361127df MA |
4377 | } |
4378 | ||
5fa737a4 | 4379 | /* |
c917c8f3 | 4380 | * Input object checking rules |
5fa737a4 | 4381 | * |
c917c8f3 LC |
4382 | * 1. Input object must be a dict |
4383 | * 2. The "execute" key must exist | |
4384 | * 3. The "execute" key must be a string | |
4385 | * 4. If the "arguments" key exists, it must be a dict | |
4386 | * 5. If the "id" key exists, it can be anything (ie. json-value) | |
4387 | * 6. Any argument not listed above is considered invalid | |
5fa737a4 | 4388 | */ |
c917c8f3 | 4389 | static QDict *qmp_check_input_obj(QObject *input_obj) |
5fa737a4 | 4390 | { |
c917c8f3 LC |
4391 | const QDictEntry *ent; |
4392 | int has_exec_key = 0; | |
4393 | QDict *input_dict; | |
5fa737a4 | 4394 | |
c917c8f3 LC |
4395 | if (qobject_type(input_obj) != QTYPE_QDICT) { |
4396 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object"); | |
4397 | return NULL; | |
5fa737a4 LC |
4398 | } |
4399 | ||
c917c8f3 | 4400 | input_dict = qobject_to_qdict(input_obj); |
5fa737a4 | 4401 | |
c917c8f3 LC |
4402 | for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){ |
4403 | const char *arg_name = qdict_entry_key(ent); | |
4404 | const QObject *arg_obj = qdict_entry_value(ent); | |
5fa737a4 | 4405 | |
c917c8f3 LC |
4406 | if (!strcmp(arg_name, "execute")) { |
4407 | if (qobject_type(arg_obj) != QTYPE_QSTRING) { | |
4408 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute", | |
4409 | "string"); | |
4410 | return NULL; | |
361127df | 4411 | } |
c917c8f3 LC |
4412 | has_exec_key = 1; |
4413 | } else if (!strcmp(arg_name, "arguments")) { | |
4414 | if (qobject_type(arg_obj) != QTYPE_QDICT) { | |
4415 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments", | |
4416 | "object"); | |
4417 | return NULL; | |
5fa737a4 | 4418 | } |
c917c8f3 LC |
4419 | } else if (!strcmp(arg_name, "id")) { |
4420 | /* FIXME: check duplicated IDs for async commands */ | |
5fa737a4 | 4421 | } else { |
c917c8f3 LC |
4422 | qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name); |
4423 | return NULL; | |
5fa737a4 LC |
4424 | } |
4425 | } | |
4426 | ||
c917c8f3 LC |
4427 | if (!has_exec_key) { |
4428 | qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute"); | |
4429 | return NULL; | |
4430 | } | |
5fa737a4 | 4431 | |
c917c8f3 | 4432 | return input_dict; |
09069b19 LC |
4433 | } |
4434 | ||
fc29df75 LC |
4435 | static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd, |
4436 | const QDict *params) | |
4437 | { | |
4438 | int ret; | |
4439 | QObject *data = NULL; | |
4440 | ||
fc29df75 LC |
4441 | ret = cmd->mhandler.cmd_new(mon, params, &data); |
4442 | handler_audit(mon, cmd, ret); | |
4443 | monitor_protocol_emitter(mon, data); | |
4444 | qobject_decref(data); | |
4445 | } | |
4446 | ||
5fa737a4 LC |
4447 | static void handle_qmp_command(JSONMessageParser *parser, QList *tokens) |
4448 | { | |
4449 | int err; | |
4450 | QObject *obj; | |
4451 | QDict *input, *args; | |
5fa737a4 | 4452 | const mon_cmd_t *cmd; |
40e5a01d | 4453 | const char *cmd_name; |
5fa737a4 LC |
4454 | Monitor *mon = cur_mon; |
4455 | ||
e4940c60 | 4456 | args = input = NULL; |
5fa737a4 LC |
4457 | |
4458 | obj = json_parser_parse(tokens, NULL); | |
4459 | if (!obj) { | |
4460 | // FIXME: should be triggered in json_parser_parse() | |
ab5b027e | 4461 | qerror_report(QERR_JSON_PARSING); |
5fa737a4 | 4462 | goto err_out; |
5fa737a4 LC |
4463 | } |
4464 | ||
c917c8f3 LC |
4465 | input = qmp_check_input_obj(obj); |
4466 | if (!input) { | |
5fa737a4 LC |
4467 | qobject_decref(obj); |
4468 | goto err_out; | |
4469 | } | |
4470 | ||
5fa737a4 LC |
4471 | mon->mc->id = qdict_get(input, "id"); |
4472 | qobject_incref(mon->mc->id); | |
4473 | ||
0bbab46d | 4474 | cmd_name = qdict_get_str(input, "execute"); |
89bd820a | 4475 | trace_handle_qmp_command(mon, cmd_name); |
09069b19 | 4476 | if (invalid_qmp_mode(mon, cmd_name)) { |
ab5b027e | 4477 | qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name); |
e4940c60 | 4478 | goto err_out; |
09069b19 LC |
4479 | } |
4480 | ||
e3193601 | 4481 | cmd = qmp_find_cmd(cmd_name); |
d1249eaa | 4482 | if (!cmd) { |
ab5b027e | 4483 | qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name); |
e4940c60 | 4484 | goto err_out; |
5fa737a4 LC |
4485 | } |
4486 | ||
4487 | obj = qdict_get(input, "arguments"); | |
4488 | if (!obj) { | |
4489 | args = qdict_new(); | |
4490 | } else { | |
4491 | args = qobject_to_qdict(obj); | |
4492 | QINCREF(args); | |
4493 | } | |
4494 | ||
2dbc8db0 | 4495 | err = qmp_check_client_args(cmd, args); |
5fa737a4 LC |
4496 | if (err < 0) { |
4497 | goto err_out; | |
4498 | } | |
4499 | ||
40e5a01d | 4500 | if (handler_is_async(cmd)) { |
5af7bbae LC |
4501 | err = qmp_async_cmd_handler(mon, cmd, args); |
4502 | if (err) { | |
4503 | /* emit the error response */ | |
4504 | goto err_out; | |
4505 | } | |
940cc30d | 4506 | } else { |
fc29df75 | 4507 | qmp_call_cmd(mon, cmd, args); |
940cc30d | 4508 | } |
e4940c60 | 4509 | |
5fa737a4 LC |
4510 | goto out; |
4511 | ||
5fa737a4 LC |
4512 | err_out: |
4513 | monitor_protocol_emitter(mon, NULL); | |
4514 | out: | |
e4940c60 | 4515 | QDECREF(input); |
5fa737a4 | 4516 | QDECREF(args); |
5fa737a4 LC |
4517 | } |
4518 | ||
9b57c02e LC |
4519 | /** |
4520 | * monitor_control_read(): Read and handle QMP input | |
4521 | */ | |
4522 | static void monitor_control_read(void *opaque, const uint8_t *buf, int size) | |
4523 | { | |
4524 | Monitor *old_mon = cur_mon; | |
4525 | ||
4526 | cur_mon = opaque; | |
4527 | ||
5fa737a4 | 4528 | json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size); |
9b57c02e LC |
4529 | |
4530 | cur_mon = old_mon; | |
4531 | } | |
4532 | ||
731b0364 | 4533 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 4534 | { |
731b0364 | 4535 | Monitor *old_mon = cur_mon; |
7e2515e8 | 4536 | int i; |
376253ec | 4537 | |
731b0364 AL |
4538 | cur_mon = opaque; |
4539 | ||
cde76ee1 AL |
4540 | if (cur_mon->rs) { |
4541 | for (i = 0; i < size; i++) | |
4542 | readline_handle_byte(cur_mon->rs, buf[i]); | |
4543 | } else { | |
4544 | if (size == 0 || buf[size - 1] != 0) | |
4545 | monitor_printf(cur_mon, "corrupted command\n"); | |
4546 | else | |
f3c157c4 | 4547 | handle_user_command(cur_mon, (char *)buf); |
cde76ee1 | 4548 | } |
9dc39cba | 4549 | |
731b0364 AL |
4550 | cur_mon = old_mon; |
4551 | } | |
d8f44609 | 4552 | |
376253ec | 4553 | static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque) |
aa455485 | 4554 | { |
731b0364 | 4555 | monitor_suspend(mon); |
f3c157c4 | 4556 | handle_user_command(mon, cmdline); |
731b0364 | 4557 | monitor_resume(mon); |
d8f44609 AL |
4558 | } |
4559 | ||
cde76ee1 | 4560 | int monitor_suspend(Monitor *mon) |
d8f44609 | 4561 | { |
cde76ee1 AL |
4562 | if (!mon->rs) |
4563 | return -ENOTTY; | |
731b0364 | 4564 | mon->suspend_cnt++; |
cde76ee1 | 4565 | return 0; |
d8f44609 AL |
4566 | } |
4567 | ||
376253ec | 4568 | void monitor_resume(Monitor *mon) |
d8f44609 | 4569 | { |
cde76ee1 AL |
4570 | if (!mon->rs) |
4571 | return; | |
731b0364 AL |
4572 | if (--mon->suspend_cnt == 0) |
4573 | readline_show_prompt(mon->rs); | |
aa455485 FB |
4574 | } |
4575 | ||
ca9567e2 LC |
4576 | static QObject *get_qmp_greeting(void) |
4577 | { | |
b9c15f16 | 4578 | QObject *ver = NULL; |
ca9567e2 | 4579 | |
b9c15f16 | 4580 | qmp_marshal_input_query_version(NULL, NULL, &ver); |
ca9567e2 LC |
4581 | return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver); |
4582 | } | |
4583 | ||
9b57c02e LC |
4584 | /** |
4585 | * monitor_control_event(): Print QMP gretting | |
4586 | */ | |
4587 | static void monitor_control_event(void *opaque, int event) | |
4588 | { | |
47116d1c LC |
4589 | QObject *data; |
4590 | Monitor *mon = opaque; | |
9b57c02e | 4591 | |
47116d1c LC |
4592 | switch (event) { |
4593 | case CHR_EVENT_OPENED: | |
4a7e1190 | 4594 | mon->mc->command_mode = 0; |
ca9567e2 | 4595 | data = get_qmp_greeting(); |
9b57c02e LC |
4596 | monitor_json_emitter(mon, data); |
4597 | qobject_decref(data); | |
efb87c16 | 4598 | mon_refcount++; |
47116d1c LC |
4599 | break; |
4600 | case CHR_EVENT_CLOSED: | |
4601 | json_message_parser_destroy(&mon->mc->parser); | |
58617a79 | 4602 | json_message_parser_init(&mon->mc->parser, handle_qmp_command); |
efb87c16 CB |
4603 | mon_refcount--; |
4604 | monitor_fdsets_cleanup(); | |
47116d1c | 4605 | break; |
9b57c02e LC |
4606 | } |
4607 | } | |
4608 | ||
731b0364 | 4609 | static void monitor_event(void *opaque, int event) |
86e94dea | 4610 | { |
376253ec AL |
4611 | Monitor *mon = opaque; |
4612 | ||
2724b180 AL |
4613 | switch (event) { |
4614 | case CHR_EVENT_MUX_IN: | |
a7aec5da GH |
4615 | mon->mux_out = 0; |
4616 | if (mon->reset_seen) { | |
4617 | readline_restart(mon->rs); | |
4618 | monitor_resume(mon); | |
4619 | monitor_flush(mon); | |
4620 | } else { | |
4621 | mon->suspend_cnt = 0; | |
4622 | } | |
2724b180 AL |
4623 | break; |
4624 | ||
4625 | case CHR_EVENT_MUX_OUT: | |
a7aec5da GH |
4626 | if (mon->reset_seen) { |
4627 | if (mon->suspend_cnt == 0) { | |
4628 | monitor_printf(mon, "\n"); | |
4629 | } | |
4630 | monitor_flush(mon); | |
4631 | monitor_suspend(mon); | |
4632 | } else { | |
4633 | mon->suspend_cnt++; | |
4634 | } | |
4635 | mon->mux_out = 1; | |
2724b180 | 4636 | break; |
86e94dea | 4637 | |
b6b8df56 | 4638 | case CHR_EVENT_OPENED: |
2724b180 AL |
4639 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " |
4640 | "information\n", QEMU_VERSION); | |
a7aec5da | 4641 | if (!mon->mux_out) { |
2724b180 | 4642 | readline_show_prompt(mon->rs); |
a7aec5da GH |
4643 | } |
4644 | mon->reset_seen = 1; | |
efb87c16 CB |
4645 | mon_refcount++; |
4646 | break; | |
4647 | ||
4648 | case CHR_EVENT_CLOSED: | |
4649 | mon_refcount--; | |
4650 | monitor_fdsets_cleanup(); | |
2724b180 AL |
4651 | break; |
4652 | } | |
86e94dea TS |
4653 | } |
4654 | ||
816f8925 WX |
4655 | static int |
4656 | compare_mon_cmd(const void *a, const void *b) | |
4657 | { | |
4658 | return strcmp(((const mon_cmd_t *)a)->name, | |
4659 | ((const mon_cmd_t *)b)->name); | |
4660 | } | |
4661 | ||
4662 | static void sortcmdlist(void) | |
4663 | { | |
4664 | int array_num; | |
4665 | int elem_size = sizeof(mon_cmd_t); | |
4666 | ||
4667 | array_num = sizeof(mon_cmds)/elem_size-1; | |
4668 | qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd); | |
4669 | ||
4670 | array_num = sizeof(info_cmds)/elem_size-1; | |
4671 | qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd); | |
4672 | } | |
4673 | ||
76655d6d AL |
4674 | |
4675 | /* | |
4676 | * Local variables: | |
4677 | * c-indent-level: 4 | |
4678 | * c-basic-offset: 4 | |
4679 | * tab-width: 8 | |
4680 | * End: | |
4681 | */ | |
4682 | ||
731b0364 | 4683 | void monitor_init(CharDriverState *chr, int flags) |
aa455485 | 4684 | { |
731b0364 | 4685 | static int is_first_init = 1; |
87127161 | 4686 | Monitor *mon; |
20d8a3ed TS |
4687 | |
4688 | if (is_first_init) { | |
afeecec2 | 4689 | monitor_protocol_event_init(); |
20d8a3ed TS |
4690 | is_first_init = 0; |
4691 | } | |
87127161 | 4692 | |
7267c094 | 4693 | mon = g_malloc0(sizeof(*mon)); |
20d8a3ed | 4694 | |
87127161 | 4695 | mon->chr = chr; |
731b0364 | 4696 | mon->flags = flags; |
cde76ee1 AL |
4697 | if (flags & MONITOR_USE_READLINE) { |
4698 | mon->rs = readline_init(mon, monitor_find_completion); | |
4699 | monitor_read_command(mon, 0); | |
4700 | } | |
87127161 | 4701 | |
9b57c02e | 4702 | if (monitor_ctrl_mode(mon)) { |
7267c094 | 4703 | mon->mc = g_malloc0(sizeof(MonitorControl)); |
9b57c02e LC |
4704 | /* Control mode requires special handlers */ |
4705 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read, | |
4706 | monitor_control_event, mon); | |
15f31519 | 4707 | qemu_chr_fe_set_echo(chr, true); |
26efaca3 AL |
4708 | |
4709 | json_message_parser_init(&mon->mc->parser, handle_qmp_command); | |
9b57c02e LC |
4710 | } else { |
4711 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, | |
4712 | monitor_event, mon); | |
4713 | } | |
87127161 | 4714 | |
72cf2d4f | 4715 | QLIST_INSERT_HEAD(&mon_list, mon, entry); |
8631b608 MA |
4716 | if (!default_mon || (flags & MONITOR_IS_DEFAULT)) |
4717 | default_mon = mon; | |
816f8925 WX |
4718 | |
4719 | sortcmdlist(); | |
aa455485 FB |
4720 | } |
4721 | ||
376253ec | 4722 | static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque) |
81d0912d | 4723 | { |
bb5fc20f AL |
4724 | BlockDriverState *bs = opaque; |
4725 | int ret = 0; | |
81d0912d | 4726 | |
bb5fc20f | 4727 | if (bdrv_set_key(bs, password) != 0) { |
376253ec | 4728 | monitor_printf(mon, "invalid password\n"); |
bb5fc20f | 4729 | ret = -EPERM; |
9dc39cba | 4730 | } |
731b0364 AL |
4731 | if (mon->password_completion_cb) |
4732 | mon->password_completion_cb(mon->password_opaque, ret); | |
bb5fc20f | 4733 | |
731b0364 | 4734 | monitor_read_command(mon, 1); |
9dc39cba | 4735 | } |
c0f4ce77 | 4736 | |
7060b478 AL |
4737 | ReadLineState *monitor_get_rs(Monitor *mon) |
4738 | { | |
4739 | return mon->rs; | |
4740 | } | |
4741 | ||
0bbc47bb LC |
4742 | int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs, |
4743 | BlockDriverCompletionFunc *completion_cb, | |
4744 | void *opaque) | |
c0f4ce77 | 4745 | { |
cde76ee1 AL |
4746 | int err; |
4747 | ||
bb5fc20f AL |
4748 | if (!bdrv_key_required(bs)) { |
4749 | if (completion_cb) | |
4750 | completion_cb(opaque, 0); | |
0bbc47bb | 4751 | return 0; |
bb5fc20f | 4752 | } |
c0f4ce77 | 4753 | |
94171e11 | 4754 | if (monitor_ctrl_mode(mon)) { |
903a8814 LC |
4755 | qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), |
4756 | bdrv_get_encrypted_filename(bs)); | |
0bbc47bb | 4757 | return -1; |
94171e11 LC |
4758 | } |
4759 | ||
376253ec AL |
4760 | monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs), |
4761 | bdrv_get_encrypted_filename(bs)); | |
bb5fc20f | 4762 | |
731b0364 AL |
4763 | mon->password_completion_cb = completion_cb; |
4764 | mon->password_opaque = opaque; | |
bb5fc20f | 4765 | |
cde76ee1 AL |
4766 | err = monitor_read_password(mon, bdrv_password_cb, bs); |
4767 | ||
4768 | if (err && completion_cb) | |
4769 | completion_cb(opaque, err); | |
0bbc47bb LC |
4770 | |
4771 | return err; | |
c0f4ce77 | 4772 | } |
e42e818b LC |
4773 | |
4774 | int monitor_read_block_device_key(Monitor *mon, const char *device, | |
4775 | BlockDriverCompletionFunc *completion_cb, | |
4776 | void *opaque) | |
4777 | { | |
4778 | BlockDriverState *bs; | |
4779 | ||
4780 | bs = bdrv_find(device); | |
4781 | if (!bs) { | |
4782 | monitor_printf(mon, "Device not found %s\n", device); | |
4783 | return -1; | |
4784 | } | |
4785 | ||
4786 | return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque); | |
4787 | } |