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