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