]>
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 PB |
36 | #include "qemu-char.h" |
37 | #include "sysemu.h" | |
376253ec AL |
38 | #include "monitor.h" |
39 | #include "readline.h" | |
87ecb68b PB |
40 | #include "console.h" |
41 | #include "block.h" | |
42 | #include "audio/audio.h" | |
9307c4c1 | 43 | #include "disas.h" |
df751fa8 | 44 | #include "balloon.h" |
c8256f9d | 45 | #include "qemu-timer.h" |
5bb7910a | 46 | #include "migration.h" |
7ba1e619 | 47 | #include "kvm.h" |
76655d6d | 48 | #include "acl.h" |
f7188bbe | 49 | #include "qint.h" |
8f3cec0b | 50 | #include "qlist.h" |
f7188bbe | 51 | #include "qdict.h" |
5fa737a4 | 52 | #include "qbool.h" |
f7188bbe | 53 | #include "qstring.h" |
8204a918 | 54 | #include "qerror.h" |
9b57c02e | 55 | #include "qjson.h" |
5fa737a4 LC |
56 | #include "json-streamer.h" |
57 | #include "json-parser.h" | |
d08d6f04 | 58 | #include "osdep.h" |
6a5bd307 | 59 | |
9dc39cba | 60 | //#define DEBUG |
81d0912d | 61 | //#define DEBUG_COMPLETION |
9dc39cba | 62 | |
9307c4c1 FB |
63 | /* |
64 | * Supported types: | |
5fafdf24 | 65 | * |
9307c4c1 | 66 | * 'F' filename |
81d0912d | 67 | * 'B' block device name |
9307c4c1 | 68 | * 's' string (accept optional quote) |
92a31b1f FB |
69 | * 'i' 32 bit integer |
70 | * 'l' target long (32 or 64 bit) | |
9307c4c1 FB |
71 | * '/' optional gdb-like print format (like "/10x") |
72 | * | |
fb46660e LC |
73 | * '?' optional type (for all types, except '/') |
74 | * '.' other form of optional type (for 'i' and 'l') | |
75 | * '-' optional parameter (eg. '-f') | |
9307c4c1 FB |
76 | * |
77 | */ | |
78 | ||
c227f099 | 79 | typedef struct mon_cmd_t { |
9dc39cba | 80 | const char *name; |
9307c4c1 | 81 | const char *args_type; |
9dc39cba FB |
82 | const char *params; |
83 | const char *help; | |
a2876f59 | 84 | void (*user_print)(Monitor *mon, const QObject *data); |
910df89d LC |
85 | union { |
86 | void (*info)(Monitor *mon); | |
13c7425e | 87 | void (*info_new)(Monitor *mon, QObject **ret_data); |
af4ce882 | 88 | void (*cmd)(Monitor *mon, const QDict *qdict); |
13917bee | 89 | void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data); |
910df89d | 90 | } mhandler; |
c227f099 | 91 | } mon_cmd_t; |
9dc39cba | 92 | |
f07918fd | 93 | /* file descriptors passed via SCM_RIGHTS */ |
c227f099 AL |
94 | typedef struct mon_fd_t mon_fd_t; |
95 | struct mon_fd_t { | |
f07918fd MM |
96 | char *name; |
97 | int fd; | |
c227f099 | 98 | QLIST_ENTRY(mon_fd_t) next; |
f07918fd MM |
99 | }; |
100 | ||
5fa737a4 LC |
101 | typedef struct MonitorControl { |
102 | QObject *id; | |
4a29a85d | 103 | int print_enabled; |
5fa737a4 LC |
104 | JSONMessageParser parser; |
105 | } MonitorControl; | |
106 | ||
87127161 AL |
107 | struct Monitor { |
108 | CharDriverState *chr; | |
a7aec5da GH |
109 | int mux_out; |
110 | int reset_seen; | |
731b0364 AL |
111 | int flags; |
112 | int suspend_cnt; | |
113 | uint8_t outbuf[1024]; | |
114 | int outbuf_index; | |
115 | ReadLineState *rs; | |
5fa737a4 | 116 | MonitorControl *mc; |
731b0364 AL |
117 | CPUState *mon_cpu; |
118 | BlockDriverCompletionFunc *password_completion_cb; | |
119 | void *password_opaque; | |
8204a918 | 120 | QError *error; |
c227f099 | 121 | QLIST_HEAD(,mon_fd_t) fds; |
72cf2d4f | 122 | QLIST_ENTRY(Monitor) entry; |
87127161 AL |
123 | }; |
124 | ||
72cf2d4f | 125 | static QLIST_HEAD(mon_list, Monitor) mon_list; |
7e2515e8 | 126 | |
c227f099 AL |
127 | static const mon_cmd_t mon_cmds[]; |
128 | static const mon_cmd_t info_cmds[]; | |
9dc39cba | 129 | |
87127161 | 130 | Monitor *cur_mon = NULL; |
376253ec | 131 | |
731b0364 AL |
132 | static void monitor_command_cb(Monitor *mon, const char *cmdline, |
133 | void *opaque); | |
83ab7950 | 134 | |
418173c7 LC |
135 | /* Return true if in control mode, false otherwise */ |
136 | static inline int monitor_ctrl_mode(const Monitor *mon) | |
137 | { | |
138 | return (mon->flags & MONITOR_USE_CONTROL); | |
139 | } | |
140 | ||
731b0364 AL |
141 | static void monitor_read_command(Monitor *mon, int show_prompt) |
142 | { | |
602e97b7 LC |
143 | if (!mon->rs) |
144 | return; | |
145 | ||
731b0364 AL |
146 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); |
147 | if (show_prompt) | |
148 | readline_show_prompt(mon->rs); | |
149 | } | |
6a00d601 | 150 | |
cde76ee1 AL |
151 | static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
152 | void *opaque) | |
bb5fc20f | 153 | { |
931a548b LC |
154 | if (monitor_ctrl_mode(mon)) { |
155 | qemu_error_new(QERR_MISSING_PARAMETER, "password"); | |
156 | return -EINVAL; | |
157 | } else if (mon->rs) { | |
cde76ee1 AL |
158 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); |
159 | /* prompt is printed on return from the command handler */ | |
160 | return 0; | |
161 | } else { | |
162 | monitor_printf(mon, "terminal does not support password prompting\n"); | |
163 | return -ENOTTY; | |
164 | } | |
bb5fc20f AL |
165 | } |
166 | ||
376253ec | 167 | void monitor_flush(Monitor *mon) |
7e2515e8 | 168 | { |
a7aec5da | 169 | if (mon && mon->outbuf_index != 0 && !mon->mux_out) { |
731b0364 AL |
170 | qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index); |
171 | mon->outbuf_index = 0; | |
7e2515e8 FB |
172 | } |
173 | } | |
174 | ||
175 | /* flush at every end of line or if the buffer is full */ | |
376253ec | 176 | static void monitor_puts(Monitor *mon, const char *str) |
7e2515e8 | 177 | { |
60fe76f3 | 178 | char c; |
731b0364 | 179 | |
7e2515e8 FB |
180 | for(;;) { |
181 | c = *str++; | |
182 | if (c == '\0') | |
183 | break; | |
7ba1260a | 184 | if (c == '\n') |
731b0364 AL |
185 | mon->outbuf[mon->outbuf_index++] = '\r'; |
186 | mon->outbuf[mon->outbuf_index++] = c; | |
187 | if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1) | |
188 | || c == '\n') | |
376253ec | 189 | monitor_flush(mon); |
7e2515e8 FB |
190 | } |
191 | } | |
192 | ||
376253ec | 193 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
9dc39cba | 194 | { |
5fd5f699 LC |
195 | if (!mon) |
196 | return; | |
197 | ||
4a29a85d LC |
198 | if (mon->mc && !mon->mc->print_enabled) { |
199 | qemu_error_new(QERR_UNDEFINED_ERROR); | |
200 | } else { | |
201 | char buf[4096]; | |
202 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
203 | monitor_puts(mon, buf); | |
204 | } | |
9dc39cba FB |
205 | } |
206 | ||
376253ec | 207 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
9dc39cba | 208 | { |
7e2515e8 FB |
209 | va_list ap; |
210 | va_start(ap, fmt); | |
376253ec | 211 | monitor_vprintf(mon, fmt, ap); |
7e2515e8 | 212 | va_end(ap); |
9dc39cba FB |
213 | } |
214 | ||
376253ec | 215 | void monitor_print_filename(Monitor *mon, const char *filename) |
fef30743 TS |
216 | { |
217 | int i; | |
218 | ||
219 | for (i = 0; filename[i]; i++) { | |
28a76be8 AL |
220 | switch (filename[i]) { |
221 | case ' ': | |
222 | case '"': | |
223 | case '\\': | |
224 | monitor_printf(mon, "\\%c", filename[i]); | |
225 | break; | |
226 | case '\t': | |
227 | monitor_printf(mon, "\\t"); | |
228 | break; | |
229 | case '\r': | |
230 | monitor_printf(mon, "\\r"); | |
231 | break; | |
232 | case '\n': | |
233 | monitor_printf(mon, "\\n"); | |
234 | break; | |
235 | default: | |
236 | monitor_printf(mon, "%c", filename[i]); | |
237 | break; | |
238 | } | |
fef30743 TS |
239 | } |
240 | } | |
241 | ||
7fe48483 FB |
242 | static int monitor_fprintf(FILE *stream, const char *fmt, ...) |
243 | { | |
244 | va_list ap; | |
245 | va_start(ap, fmt); | |
376253ec | 246 | monitor_vprintf((Monitor *)stream, fmt, ap); |
7fe48483 FB |
247 | va_end(ap); |
248 | return 0; | |
249 | } | |
250 | ||
13c7425e LC |
251 | static void monitor_user_noop(Monitor *mon, const QObject *data) { } |
252 | ||
13917bee LC |
253 | static inline int monitor_handler_ported(const mon_cmd_t *cmd) |
254 | { | |
255 | return cmd->user_print != NULL; | |
256 | } | |
257 | ||
8204a918 LC |
258 | static inline int monitor_has_error(const Monitor *mon) |
259 | { | |
260 | return mon->error != NULL; | |
261 | } | |
262 | ||
9b57c02e LC |
263 | static void monitor_json_emitter(Monitor *mon, const QObject *data) |
264 | { | |
265 | QString *json; | |
266 | ||
267 | json = qobject_to_json(data); | |
268 | assert(json != NULL); | |
269 | ||
4a29a85d | 270 | mon->mc->print_enabled = 1; |
9b57c02e | 271 | monitor_printf(mon, "%s\n", qstring_get_str(json)); |
4a29a85d LC |
272 | mon->mc->print_enabled = 0; |
273 | ||
9b57c02e LC |
274 | QDECREF(json); |
275 | } | |
276 | ||
25b422eb LC |
277 | static void monitor_protocol_emitter(Monitor *mon, QObject *data) |
278 | { | |
279 | QDict *qmp; | |
280 | ||
281 | qmp = qdict_new(); | |
282 | ||
283 | if (!monitor_has_error(mon)) { | |
284 | /* success response */ | |
285 | if (data) { | |
286 | qobject_incref(data); | |
287 | qdict_put_obj(qmp, "return", data); | |
288 | } else { | |
3733a1e8 LC |
289 | /* return an empty QDict by default */ |
290 | qdict_put(qmp, "return", qdict_new()); | |
25b422eb LC |
291 | } |
292 | } else { | |
293 | /* error response */ | |
143d288c | 294 | qdict_put(mon->error->error, "desc", qerror_human(mon->error)); |
25b422eb LC |
295 | qdict_put(qmp, "error", mon->error->error); |
296 | QINCREF(mon->error->error); | |
297 | QDECREF(mon->error); | |
298 | mon->error = NULL; | |
299 | } | |
300 | ||
5fa737a4 LC |
301 | if (mon->mc->id) { |
302 | qdict_put_obj(qmp, "id", mon->mc->id); | |
303 | mon->mc->id = NULL; | |
304 | } | |
305 | ||
25b422eb LC |
306 | monitor_json_emitter(mon, QOBJECT(qmp)); |
307 | QDECREF(qmp); | |
308 | } | |
309 | ||
0d1ea871 LC |
310 | static void timestamp_put(QDict *qdict) |
311 | { | |
312 | int err; | |
313 | QObject *obj; | |
d08d6f04 | 314 | qemu_timeval tv; |
0d1ea871 | 315 | |
d08d6f04 | 316 | err = qemu_gettimeofday(&tv); |
0d1ea871 LC |
317 | if (err < 0) |
318 | return; | |
319 | ||
320 | obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", " | |
321 | "'microseconds': %" PRId64 " }", | |
322 | (int64_t) tv.tv_sec, (int64_t) tv.tv_usec); | |
323 | assert(obj != NULL); | |
324 | ||
325 | qdict_put_obj(qdict, "timestamp", obj); | |
326 | } | |
327 | ||
328 | /** | |
329 | * monitor_protocol_event(): Generate a Monitor event | |
330 | * | |
331 | * Event-specific data can be emitted through the (optional) 'data' parameter. | |
332 | */ | |
333 | void monitor_protocol_event(MonitorEvent event, QObject *data) | |
334 | { | |
335 | QDict *qmp; | |
336 | const char *event_name; | |
41ae9ece | 337 | Monitor *mon; |
0d1ea871 | 338 | |
242cd003 | 339 | assert(event < QEVENT_MAX); |
0d1ea871 | 340 | |
0d1ea871 | 341 | switch (event) { |
242cd003 | 342 | case QEVENT_DEBUG: |
b1a15e7e LC |
343 | event_name = "DEBUG"; |
344 | break; | |
242cd003 | 345 | case QEVENT_SHUTDOWN: |
b1a15e7e LC |
346 | event_name = "SHUTDOWN"; |
347 | break; | |
242cd003 | 348 | case QEVENT_RESET: |
b1a15e7e LC |
349 | event_name = "RESET"; |
350 | break; | |
242cd003 | 351 | case QEVENT_POWERDOWN: |
b1a15e7e LC |
352 | event_name = "POWERDOWN"; |
353 | break; | |
242cd003 | 354 | case QEVENT_STOP: |
b1a15e7e LC |
355 | event_name = "STOP"; |
356 | break; | |
0d1ea871 LC |
357 | default: |
358 | abort(); | |
359 | break; | |
360 | } | |
361 | ||
362 | qmp = qdict_new(); | |
363 | timestamp_put(qmp); | |
364 | qdict_put(qmp, "event", qstring_from_str(event_name)); | |
eacad66d LC |
365 | if (data) { |
366 | qobject_incref(data); | |
0d1ea871 | 367 | qdict_put_obj(qmp, "data", data); |
eacad66d | 368 | } |
0d1ea871 | 369 | |
41ae9ece | 370 | QLIST_FOREACH(mon, &mon_list, entry) { |
d3bf9367 LC |
371 | if (monitor_ctrl_mode(mon)) { |
372 | monitor_json_emitter(mon, QOBJECT(qmp)); | |
373 | } | |
41ae9ece | 374 | } |
0d1ea871 LC |
375 | QDECREF(qmp); |
376 | } | |
377 | ||
9dc39cba FB |
378 | static int compare_cmd(const char *name, const char *list) |
379 | { | |
380 | const char *p, *pstart; | |
381 | int len; | |
382 | len = strlen(name); | |
383 | p = list; | |
384 | for(;;) { | |
385 | pstart = p; | |
386 | p = strchr(p, '|'); | |
387 | if (!p) | |
388 | p = pstart + strlen(pstart); | |
389 | if ((p - pstart) == len && !memcmp(pstart, name, len)) | |
390 | return 1; | |
391 | if (*p == '\0') | |
392 | break; | |
393 | p++; | |
394 | } | |
395 | return 0; | |
396 | } | |
397 | ||
c227f099 | 398 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
376253ec | 399 | const char *prefix, const char *name) |
9dc39cba | 400 | { |
c227f099 | 401 | const mon_cmd_t *cmd; |
9dc39cba FB |
402 | |
403 | for(cmd = cmds; cmd->name != NULL; cmd++) { | |
404 | if (!name || !strcmp(name, cmd->name)) | |
376253ec AL |
405 | monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name, |
406 | cmd->params, cmd->help); | |
9dc39cba FB |
407 | } |
408 | } | |
409 | ||
376253ec | 410 | static void help_cmd(Monitor *mon, const char *name) |
9dc39cba FB |
411 | { |
412 | if (name && !strcmp(name, "info")) { | |
376253ec | 413 | help_cmd_dump(mon, info_cmds, "info ", NULL); |
9dc39cba | 414 | } else { |
376253ec | 415 | help_cmd_dump(mon, mon_cmds, "", name); |
f193c797 | 416 | if (name && !strcmp(name, "log")) { |
8662d656 | 417 | const CPULogItem *item; |
376253ec AL |
418 | monitor_printf(mon, "Log items (comma separated):\n"); |
419 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); | |
f193c797 | 420 | for(item = cpu_log_items; item->mask != 0; item++) { |
376253ec | 421 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
f193c797 FB |
422 | } |
423 | } | |
9dc39cba FB |
424 | } |
425 | } | |
426 | ||
d54908a5 | 427 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
38183186 | 428 | { |
d54908a5 | 429 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
38183186 LC |
430 | } |
431 | ||
d54908a5 | 432 | static void do_commit(Monitor *mon, const QDict *qdict) |
9dc39cba | 433 | { |
751c6a17 GH |
434 | int all_devices; |
435 | DriveInfo *dinfo; | |
d54908a5 | 436 | const char *device = qdict_get_str(qdict, "device"); |
2dc7b602 | 437 | |
7954c734 | 438 | all_devices = !strcmp(device, "all"); |
72cf2d4f | 439 | QTAILQ_FOREACH(dinfo, &drives, next) { |
751c6a17 | 440 | if (!all_devices) |
73006d2a | 441 | if (strcmp(bdrv_get_device_name(dinfo->bdrv), device)) |
751c6a17 GH |
442 | continue; |
443 | bdrv_commit(dinfo->bdrv); | |
9dc39cba FB |
444 | } |
445 | } | |
446 | ||
13c7425e | 447 | static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data) |
9dc39cba | 448 | { |
c227f099 | 449 | const mon_cmd_t *cmd; |
d54908a5 | 450 | const char *item = qdict_get_try_str(qdict, "item"); |
9dc39cba | 451 | |
956f1a0d LC |
452 | if (!item) { |
453 | assert(monitor_ctrl_mode(mon) == 0); | |
9dc39cba | 454 | goto help; |
956f1a0d | 455 | } |
13c7425e LC |
456 | |
457 | for (cmd = info_cmds; cmd->name != NULL; cmd++) { | |
5fafdf24 | 458 | if (compare_cmd(item, cmd->name)) |
13c7425e | 459 | break; |
9dc39cba | 460 | } |
13c7425e | 461 | |
956f1a0d LC |
462 | if (cmd->name == NULL) { |
463 | if (monitor_ctrl_mode(mon)) { | |
464 | qemu_error_new(QERR_COMMAND_NOT_FOUND, item); | |
465 | return; | |
466 | } | |
13c7425e | 467 | goto help; |
956f1a0d | 468 | } |
13c7425e LC |
469 | |
470 | if (monitor_handler_ported(cmd)) { | |
471 | cmd->mhandler.info_new(mon, ret_data); | |
25b422eb LC |
472 | |
473 | if (!monitor_ctrl_mode(mon)) { | |
474 | /* | |
475 | * User Protocol function is called here, Monitor Protocol is | |
476 | * handled by monitor_call_handler() | |
477 | */ | |
478 | if (*ret_data) | |
479 | cmd->user_print(mon, *ret_data); | |
480 | } | |
13c7425e | 481 | } else { |
956f1a0d LC |
482 | if (monitor_ctrl_mode(mon)) { |
483 | /* handler not converted yet */ | |
484 | qemu_error_new(QERR_COMMAND_NOT_FOUND, item); | |
485 | } else { | |
486 | cmd->mhandler.info(mon); | |
487 | } | |
13c7425e LC |
488 | } |
489 | ||
9dc39cba | 490 | return; |
13c7425e LC |
491 | |
492 | help: | |
493 | help_cmd(mon, "info"); | |
9dc39cba FB |
494 | } |
495 | ||
6e785bee LC |
496 | static void do_info_version_print(Monitor *mon, const QObject *data) |
497 | { | |
498 | QDict *qdict; | |
499 | ||
500 | qdict = qobject_to_qdict(data); | |
501 | ||
502 | monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"), | |
503 | qdict_get_str(qdict, "package")); | |
504 | } | |
505 | ||
ab2d3187 LC |
506 | /** |
507 | * do_info_version(): Show QEMU version | |
6e785bee LC |
508 | * |
509 | * Return a QDict with the following information: | |
510 | * | |
511 | * - "qemu": QEMU's version | |
512 | * - "package": package's version | |
513 | * | |
514 | * Example: | |
515 | * | |
516 | * { "qemu": "0.11.50", "package": "" } | |
ab2d3187 LC |
517 | */ |
518 | static void do_info_version(Monitor *mon, QObject **ret_data) | |
9bc9d1c7 | 519 | { |
6e785bee LC |
520 | *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }", |
521 | QEMU_VERSION, QEMU_PKGVERSION); | |
9bc9d1c7 FB |
522 | } |
523 | ||
7589acc9 | 524 | static void do_info_name_print(Monitor *mon, const QObject *data) |
c35734b2 | 525 | { |
7589acc9 LC |
526 | QDict *qdict; |
527 | ||
528 | qdict = qobject_to_qdict(data); | |
529 | if (qdict_size(qdict) == 0) { | |
530 | return; | |
531 | } | |
532 | ||
533 | monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name")); | |
534 | } | |
535 | ||
536 | /** | |
537 | * do_info_name(): Show VM name | |
538 | * | |
539 | * Return a QDict with the following information: | |
540 | * | |
541 | * - "name": VM's name (optional) | |
542 | * | |
543 | * Example: | |
544 | * | |
545 | * { "name": "qemu-name" } | |
546 | */ | |
547 | static void do_info_name(Monitor *mon, QObject **ret_data) | |
548 | { | |
549 | *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) : | |
550 | qobject_from_jsonf("{}"); | |
c35734b2 TS |
551 | } |
552 | ||
5daa7bb7 LC |
553 | static QObject *get_cmd_dict(const char *name) |
554 | { | |
555 | const char *p; | |
556 | ||
557 | /* Remove '|' from some commands */ | |
558 | p = strchr(name, '|'); | |
559 | if (p) { | |
560 | p++; | |
561 | } else { | |
562 | p = name; | |
563 | } | |
564 | ||
565 | return qobject_from_jsonf("{ 'name': %s }", p); | |
566 | } | |
567 | ||
e3bba9d0 LC |
568 | /** |
569 | * do_info_commands(): List QMP available commands | |
570 | * | |
5daa7bb7 LC |
571 | * Each command is represented by a QDict, the returned QObject is a QList |
572 | * of all commands. | |
573 | * | |
574 | * The QDict contains: | |
575 | * | |
576 | * - "name": command's name | |
577 | * | |
578 | * Example: | |
579 | * | |
580 | * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] } | |
e3bba9d0 LC |
581 | */ |
582 | static void do_info_commands(Monitor *mon, QObject **ret_data) | |
583 | { | |
584 | QList *cmd_list; | |
585 | const mon_cmd_t *cmd; | |
586 | ||
587 | cmd_list = qlist_new(); | |
588 | ||
589 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
590 | if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) { | |
5daa7bb7 | 591 | qlist_append_obj(cmd_list, get_cmd_dict(cmd->name)); |
e3bba9d0 LC |
592 | } |
593 | } | |
594 | ||
595 | for (cmd = info_cmds; cmd->name != NULL; cmd++) { | |
596 | if (monitor_handler_ported(cmd)) { | |
597 | char buf[128]; | |
598 | snprintf(buf, sizeof(buf), "query-%s", cmd->name); | |
5daa7bb7 | 599 | qlist_append_obj(cmd_list, get_cmd_dict(buf)); |
e3bba9d0 LC |
600 | } |
601 | } | |
602 | ||
603 | *ret_data = QOBJECT(cmd_list); | |
604 | } | |
605 | ||
bf4f74c0 | 606 | #if defined(TARGET_I386) |
5f9fe0f8 | 607 | static void do_info_hpet_print(Monitor *mon, const QObject *data) |
16b29ae1 | 608 | { |
376253ec | 609 | monitor_printf(mon, "HPET is %s by QEMU\n", |
5f9fe0f8 LC |
610 | qdict_get_bool(qobject_to_qdict(data), "enabled") ? |
611 | "enabled" : "disabled"); | |
612 | } | |
613 | ||
614 | /** | |
615 | * do_info_hpet(): Show HPET state | |
616 | * | |
617 | * Return a QDict with the following information: | |
618 | * | |
619 | * - "enabled": true if hpet if enabled, false otherwise | |
620 | * | |
621 | * Example: | |
622 | * | |
623 | * { "enabled": true } | |
624 | */ | |
625 | static void do_info_hpet(Monitor *mon, QObject **ret_data) | |
626 | { | |
627 | *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet); | |
16b29ae1 | 628 | } |
bf4f74c0 | 629 | #endif |
16b29ae1 | 630 | |
ee70ef87 | 631 | static void do_info_uuid_print(Monitor *mon, const QObject *data) |
a36e69dd | 632 | { |
ee70ef87 LC |
633 | monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID")); |
634 | } | |
635 | ||
636 | /** | |
637 | * do_info_uuid(): Show VM UUID | |
638 | * | |
639 | * Return a QDict with the following information: | |
640 | * | |
641 | * - "UUID": Universally Unique Identifier | |
642 | * | |
643 | * Example: | |
644 | * | |
645 | * { "UUID": "550e8400-e29b-41d4-a716-446655440000" } | |
646 | */ | |
647 | static void do_info_uuid(Monitor *mon, QObject **ret_data) | |
648 | { | |
649 | char uuid[64]; | |
650 | ||
651 | snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1], | |
376253ec AL |
652 | qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], |
653 | qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], | |
654 | qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], | |
655 | qemu_uuid[14], qemu_uuid[15]); | |
ee70ef87 | 656 | *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid); |
a36e69dd TS |
657 | } |
658 | ||
6a00d601 | 659 | /* get the current CPU defined by the user */ |
9596ebb7 | 660 | static int mon_set_cpu(int cpu_index) |
6a00d601 FB |
661 | { |
662 | CPUState *env; | |
663 | ||
664 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
665 | if (env->cpu_index == cpu_index) { | |
731b0364 | 666 | cur_mon->mon_cpu = env; |
6a00d601 FB |
667 | return 0; |
668 | } | |
669 | } | |
670 | return -1; | |
671 | } | |
672 | ||
9596ebb7 | 673 | static CPUState *mon_get_cpu(void) |
6a00d601 | 674 | { |
731b0364 | 675 | if (!cur_mon->mon_cpu) { |
6a00d601 FB |
676 | mon_set_cpu(0); |
677 | } | |
4c0960c0 | 678 | cpu_synchronize_state(cur_mon->mon_cpu); |
731b0364 | 679 | return cur_mon->mon_cpu; |
6a00d601 FB |
680 | } |
681 | ||
376253ec | 682 | static void do_info_registers(Monitor *mon) |
9307c4c1 | 683 | { |
6a00d601 FB |
684 | CPUState *env; |
685 | env = mon_get_cpu(); | |
686 | if (!env) | |
687 | return; | |
9307c4c1 | 688 | #ifdef TARGET_I386 |
376253ec | 689 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
d24b15a8 | 690 | X86_DUMP_FPU); |
9307c4c1 | 691 | #else |
376253ec | 692 | cpu_dump_state(env, (FILE *)mon, monitor_fprintf, |
7fe48483 | 693 | 0); |
9307c4c1 FB |
694 | #endif |
695 | } | |
696 | ||
8f3cec0b LC |
697 | static void print_cpu_iter(QObject *obj, void *opaque) |
698 | { | |
699 | QDict *cpu; | |
700 | int active = ' '; | |
701 | Monitor *mon = opaque; | |
702 | ||
703 | assert(qobject_type(obj) == QTYPE_QDICT); | |
704 | cpu = qobject_to_qdict(obj); | |
705 | ||
f883e4f7 | 706 | if (qdict_get_bool(cpu, "current")) { |
8f3cec0b | 707 | active = '*'; |
f883e4f7 | 708 | } |
8f3cec0b LC |
709 | |
710 | monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU")); | |
711 | ||
712 | #if defined(TARGET_I386) | |
713 | monitor_printf(mon, "pc=0x" TARGET_FMT_lx, | |
714 | (target_ulong) qdict_get_int(cpu, "pc")); | |
715 | #elif defined(TARGET_PPC) | |
716 | monitor_printf(mon, "nip=0x" TARGET_FMT_lx, | |
717 | (target_long) qdict_get_int(cpu, "nip")); | |
718 | #elif defined(TARGET_SPARC) | |
719 | monitor_printf(mon, "pc=0x " TARGET_FMT_lx, | |
720 | (target_long) qdict_get_int(cpu, "pc")); | |
721 | monitor_printf(mon, "npc=0x" TARGET_FMT_lx, | |
722 | (target_long) qdict_get_int(cpu, "npc")); | |
723 | #elif defined(TARGET_MIPS) | |
724 | monitor_printf(mon, "PC=0x" TARGET_FMT_lx, | |
725 | (target_long) qdict_get_int(cpu, "PC")); | |
726 | #endif | |
727 | ||
f883e4f7 | 728 | if (qdict_get_bool(cpu, "halted")) { |
8f3cec0b | 729 | monitor_printf(mon, " (halted)"); |
f883e4f7 | 730 | } |
8f3cec0b LC |
731 | |
732 | monitor_printf(mon, "\n"); | |
733 | } | |
734 | ||
735 | static void monitor_print_cpus(Monitor *mon, const QObject *data) | |
736 | { | |
737 | QList *cpu_list; | |
738 | ||
739 | assert(qobject_type(data) == QTYPE_QLIST); | |
740 | cpu_list = qobject_to_qlist(data); | |
741 | qlist_iter(cpu_list, print_cpu_iter, mon); | |
742 | } | |
743 | ||
744 | /** | |
745 | * do_info_cpus(): Show CPU information | |
746 | * | |
f883e4f7 | 747 | * Return a QList. Each CPU is represented by a QDict, which contains: |
8f3cec0b | 748 | * |
f883e4f7 LC |
749 | * - "cpu": CPU index |
750 | * - "current": true if this is the current CPU, false otherwise | |
751 | * - "halted": true if the cpu is halted, false otherwise | |
752 | * - Current program counter. The key's name depends on the architecture: | |
753 | * "pc": i386/x86)64 | |
754 | * "nip": PPC | |
755 | * "pc" and "npc": sparc | |
756 | * "PC": mips | |
8f3cec0b | 757 | * |
f883e4f7 LC |
758 | * Example: |
759 | * | |
760 | * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 }, | |
761 | * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ] | |
8f3cec0b LC |
762 | */ |
763 | static void do_info_cpus(Monitor *mon, QObject **ret_data) | |
6a00d601 FB |
764 | { |
765 | CPUState *env; | |
8f3cec0b LC |
766 | QList *cpu_list; |
767 | ||
768 | cpu_list = qlist_new(); | |
6a00d601 FB |
769 | |
770 | /* just to set the default cpu if not already done */ | |
771 | mon_get_cpu(); | |
772 | ||
773 | for(env = first_cpu; env != NULL; env = env->next_cpu) { | |
f883e4f7 LC |
774 | QDict *cpu; |
775 | QObject *obj; | |
8f3cec0b | 776 | |
4c0960c0 | 777 | cpu_synchronize_state(env); |
8f3cec0b | 778 | |
f883e4f7 LC |
779 | obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }", |
780 | env->cpu_index, env == mon->mon_cpu, | |
781 | env->halted); | |
782 | assert(obj != NULL); | |
783 | ||
784 | cpu = qobject_to_qdict(obj); | |
8f3cec0b | 785 | |
6a00d601 | 786 | #if defined(TARGET_I386) |
8f3cec0b | 787 | qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base)); |
e80e1cc4 | 788 | #elif defined(TARGET_PPC) |
8f3cec0b | 789 | qdict_put(cpu, "nip", qint_from_int(env->nip)); |
ba3c64fb | 790 | #elif defined(TARGET_SPARC) |
8f3cec0b LC |
791 | qdict_put(cpu, "pc", qint_from_int(env->pc)); |
792 | qdict_put(cpu, "npc", qint_from_int(env->npc)); | |
ead9360e | 793 | #elif defined(TARGET_MIPS) |
8f3cec0b | 794 | qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC)); |
ce5232c5 | 795 | #endif |
8f3cec0b LC |
796 | |
797 | qlist_append(cpu_list, cpu); | |
6a00d601 | 798 | } |
8f3cec0b LC |
799 | |
800 | *ret_data = QOBJECT(cpu_list); | |
6a00d601 FB |
801 | } |
802 | ||
d54908a5 | 803 | static void do_cpu_set(Monitor *mon, const QDict *qdict) |
6a00d601 | 804 | { |
d54908a5 | 805 | int index = qdict_get_int(qdict, "index"); |
6a00d601 | 806 | if (mon_set_cpu(index) < 0) |
376253ec | 807 | monitor_printf(mon, "Invalid CPU index\n"); |
6a00d601 FB |
808 | } |
809 | ||
376253ec | 810 | static void do_info_jit(Monitor *mon) |
e3db7226 | 811 | { |
376253ec | 812 | dump_exec_info((FILE *)mon, monitor_fprintf); |
e3db7226 FB |
813 | } |
814 | ||
376253ec | 815 | static void do_info_history(Monitor *mon) |
aa455485 FB |
816 | { |
817 | int i; | |
7e2515e8 | 818 | const char *str; |
3b46e624 | 819 | |
cde76ee1 AL |
820 | if (!mon->rs) |
821 | return; | |
7e2515e8 FB |
822 | i = 0; |
823 | for(;;) { | |
731b0364 | 824 | str = readline_get_history(mon->rs, i); |
7e2515e8 FB |
825 | if (!str) |
826 | break; | |
376253ec | 827 | monitor_printf(mon, "%d: '%s'\n", i, str); |
8e3a9fd2 | 828 | i++; |
aa455485 FB |
829 | } |
830 | } | |
831 | ||
76a66253 JM |
832 | #if defined(TARGET_PPC) |
833 | /* XXX: not implemented in other targets */ | |
376253ec | 834 | static void do_info_cpu_stats(Monitor *mon) |
76a66253 JM |
835 | { |
836 | CPUState *env; | |
837 | ||
838 | env = mon_get_cpu(); | |
376253ec | 839 | cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0); |
76a66253 JM |
840 | } |
841 | #endif | |
842 | ||
b223f35f LC |
843 | /** |
844 | * do_quit(): Quit QEMU execution | |
845 | */ | |
846 | static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
9dc39cba FB |
847 | { |
848 | exit(0); | |
849 | } | |
850 | ||
376253ec | 851 | static int eject_device(Monitor *mon, BlockDriverState *bs, int force) |
9dc39cba FB |
852 | { |
853 | if (bdrv_is_inserted(bs)) { | |
854 | if (!force) { | |
855 | if (!bdrv_is_removable(bs)) { | |
06921ec8 MA |
856 | qemu_error_new(QERR_DEVICE_NOT_REMOVABLE, |
857 | bdrv_get_device_name(bs)); | |
9dc39cba FB |
858 | return -1; |
859 | } | |
860 | if (bdrv_is_locked(bs)) { | |
06921ec8 | 861 | qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); |
9dc39cba FB |
862 | return -1; |
863 | } | |
864 | } | |
865 | bdrv_close(bs); | |
866 | } | |
867 | return 0; | |
868 | } | |
869 | ||
e1c923a6 | 870 | static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data) |
9dc39cba FB |
871 | { |
872 | BlockDriverState *bs; | |
f18c16de | 873 | int force = qdict_get_int(qdict, "force"); |
fb8cf78d | 874 | const char *filename = qdict_get_str(qdict, "device"); |
9dc39cba | 875 | |
9307c4c1 | 876 | bs = bdrv_find(filename); |
9dc39cba | 877 | if (!bs) { |
06921ec8 | 878 | qemu_error_new(QERR_DEVICE_NOT_FOUND, filename); |
9dc39cba FB |
879 | return; |
880 | } | |
376253ec | 881 | eject_device(mon, bs, force); |
9dc39cba FB |
882 | } |
883 | ||
b3dfdb5a LC |
884 | static void do_block_set_passwd(Monitor *mon, const QDict *qdict, |
885 | QObject **ret_data) | |
886 | { | |
887 | BlockDriverState *bs; | |
888 | ||
889 | bs = bdrv_find(qdict_get_str(qdict, "device")); | |
890 | if (!bs) { | |
891 | qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device")); | |
892 | return; | |
893 | } | |
894 | ||
895 | if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) { | |
896 | qemu_error_new(QERR_INVALID_PASSWORD); | |
897 | } | |
898 | } | |
899 | ||
376253ec AL |
900 | static void do_change_block(Monitor *mon, const char *device, |
901 | const char *filename, const char *fmt) | |
9dc39cba FB |
902 | { |
903 | BlockDriverState *bs; | |
2ecea9b8 | 904 | BlockDriver *drv = NULL; |
9dc39cba | 905 | |
9307c4c1 | 906 | bs = bdrv_find(device); |
9dc39cba | 907 | if (!bs) { |
06976f82 | 908 | qemu_error_new(QERR_DEVICE_NOT_FOUND, device); |
9dc39cba FB |
909 | return; |
910 | } | |
2ecea9b8 | 911 | if (fmt) { |
eb852011 | 912 | drv = bdrv_find_whitelisted_format(fmt); |
2ecea9b8 | 913 | if (!drv) { |
06976f82 | 914 | qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt); |
2ecea9b8 AJ |
915 | return; |
916 | } | |
917 | } | |
376253ec | 918 | if (eject_device(mon, bs, 0) < 0) |
9dc39cba | 919 | return; |
2ecea9b8 | 920 | bdrv_open2(bs, filename, 0, drv); |
376253ec | 921 | monitor_read_bdrv_key_start(mon, bs, NULL, NULL); |
bb5fc20f AL |
922 | } |
923 | ||
06976f82 | 924 | static void change_vnc_password(const char *password) |
bb5fc20f AL |
925 | { |
926 | if (vnc_display_password(NULL, password) < 0) | |
06976f82 | 927 | qemu_error_new(QERR_SET_PASSWD_FAILED); |
bb5fc20f | 928 | |
a7d5da88 MA |
929 | } |
930 | ||
931 | static void change_vnc_password_cb(Monitor *mon, const char *password, | |
932 | void *opaque) | |
933 | { | |
06976f82 | 934 | change_vnc_password(password); |
731b0364 | 935 | monitor_read_command(mon, 1); |
9dc39cba FB |
936 | } |
937 | ||
376253ec | 938 | static void do_change_vnc(Monitor *mon, const char *target, const char *arg) |
e25a5822 | 939 | { |
70848515 | 940 | if (strcmp(target, "passwd") == 0 || |
28a76be8 AL |
941 | strcmp(target, "password") == 0) { |
942 | if (arg) { | |
bb5fc20f | 943 | char password[9]; |
28a76be8 AL |
944 | strncpy(password, arg, sizeof(password)); |
945 | password[sizeof(password) - 1] = '\0'; | |
06976f82 | 946 | change_vnc_password(password); |
bb5fc20f | 947 | } else { |
376253ec | 948 | monitor_read_password(mon, change_vnc_password_cb, NULL); |
bb5fc20f | 949 | } |
70848515 | 950 | } else { |
28a76be8 | 951 | if (vnc_display_open(NULL, target) < 0) |
06976f82 | 952 | qemu_error_new(QERR_VNC_SERVER_FAILED, target); |
70848515 | 953 | } |
e25a5822 TS |
954 | } |
955 | ||
06976f82 MA |
956 | /** |
957 | * do_change(): Change a removable medium, or VNC configuration | |
958 | */ | |
959 | static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
e25a5822 | 960 | { |
1d4daa91 LC |
961 | const char *device = qdict_get_str(qdict, "device"); |
962 | const char *target = qdict_get_str(qdict, "target"); | |
963 | const char *arg = qdict_get_try_str(qdict, "arg"); | |
e25a5822 | 964 | if (strcmp(device, "vnc") == 0) { |
28a76be8 | 965 | do_change_vnc(mon, target, arg); |
e25a5822 | 966 | } else { |
28a76be8 | 967 | do_change_block(mon, device, target, arg); |
e25a5822 TS |
968 | } |
969 | } | |
970 | ||
d54908a5 | 971 | static void do_screen_dump(Monitor *mon, const QDict *qdict) |
59a983b9 | 972 | { |
d54908a5 | 973 | vga_hw_screen_dump(qdict_get_str(qdict, "filename")); |
59a983b9 FB |
974 | } |
975 | ||
d54908a5 | 976 | static void do_logfile(Monitor *mon, const QDict *qdict) |
e735b91c | 977 | { |
d54908a5 | 978 | cpu_set_log_filename(qdict_get_str(qdict, "filename")); |
e735b91c PB |
979 | } |
980 | ||
d54908a5 | 981 | static void do_log(Monitor *mon, const QDict *qdict) |
f193c797 FB |
982 | { |
983 | int mask; | |
d54908a5 | 984 | const char *items = qdict_get_str(qdict, "items"); |
3b46e624 | 985 | |
9307c4c1 | 986 | if (!strcmp(items, "none")) { |
f193c797 FB |
987 | mask = 0; |
988 | } else { | |
9307c4c1 | 989 | mask = cpu_str_to_log_mask(items); |
f193c797 | 990 | if (!mask) { |
376253ec | 991 | help_cmd(mon, "log"); |
f193c797 FB |
992 | return; |
993 | } | |
994 | } | |
995 | cpu_set_log(mask); | |
996 | } | |
997 | ||
d54908a5 | 998 | static void do_singlestep(Monitor *mon, const QDict *qdict) |
1b530a6d | 999 | { |
d54908a5 | 1000 | const char *option = qdict_get_try_str(qdict, "option"); |
1b530a6d AJ |
1001 | if (!option || !strcmp(option, "on")) { |
1002 | singlestep = 1; | |
1003 | } else if (!strcmp(option, "off")) { | |
1004 | singlestep = 0; | |
1005 | } else { | |
1006 | monitor_printf(mon, "unexpected option %s\n", option); | |
1007 | } | |
1008 | } | |
1009 | ||
e0c97bde LC |
1010 | /** |
1011 | * do_stop(): Stop VM execution | |
1012 | */ | |
1013 | static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
8a7ddc38 FB |
1014 | { |
1015 | vm_stop(EXCP_INTERRUPT); | |
1016 | } | |
1017 | ||
bb5fc20f | 1018 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs); |
c0f4ce77 | 1019 | |
376253ec AL |
1020 | struct bdrv_iterate_context { |
1021 | Monitor *mon; | |
1022 | int err; | |
1023 | }; | |
1024 | ||
a1f896a0 LC |
1025 | /** |
1026 | * do_cont(): Resume emulation. | |
1027 | */ | |
1028 | static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
8a7ddc38 | 1029 | { |
376253ec | 1030 | struct bdrv_iterate_context context = { mon, 0 }; |
c0f4ce77 | 1031 | |
376253ec | 1032 | bdrv_iterate(encrypted_bdrv_it, &context); |
c0f4ce77 | 1033 | /* only resume the vm if all keys are set and valid */ |
376253ec | 1034 | if (!context.err) |
c0f4ce77 | 1035 | vm_start(); |
8a7ddc38 FB |
1036 | } |
1037 | ||
bb5fc20f AL |
1038 | static void bdrv_key_cb(void *opaque, int err) |
1039 | { | |
376253ec AL |
1040 | Monitor *mon = opaque; |
1041 | ||
bb5fc20f AL |
1042 | /* another key was set successfully, retry to continue */ |
1043 | if (!err) | |
a1f896a0 | 1044 | do_cont(mon, NULL, NULL); |
bb5fc20f AL |
1045 | } |
1046 | ||
1047 | static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs) | |
1048 | { | |
376253ec | 1049 | struct bdrv_iterate_context *context = opaque; |
bb5fc20f | 1050 | |
376253ec AL |
1051 | if (!context->err && bdrv_key_required(bs)) { |
1052 | context->err = -EBUSY; | |
1053 | monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb, | |
1054 | context->mon); | |
bb5fc20f AL |
1055 | } |
1056 | } | |
1057 | ||
d54908a5 | 1058 | static void do_gdbserver(Monitor *mon, const QDict *qdict) |
59030a8c | 1059 | { |
d54908a5 | 1060 | const char *device = qdict_get_try_str(qdict, "device"); |
59030a8c AL |
1061 | if (!device) |
1062 | device = "tcp::" DEFAULT_GDBSTUB_PORT; | |
1063 | if (gdbserver_start(device) < 0) { | |
1064 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", | |
1065 | device); | |
1066 | } else if (strcmp(device, "none") == 0) { | |
36556b20 | 1067 | monitor_printf(mon, "Disabled gdbserver\n"); |
8a7ddc38 | 1068 | } else { |
59030a8c AL |
1069 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
1070 | device); | |
8a7ddc38 FB |
1071 | } |
1072 | } | |
1073 | ||
d54908a5 | 1074 | static void do_watchdog_action(Monitor *mon, const QDict *qdict) |
9dd986cc | 1075 | { |
d54908a5 | 1076 | const char *action = qdict_get_str(qdict, "action"); |
9dd986cc RJ |
1077 | if (select_watchdog_action(action) == -1) { |
1078 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); | |
1079 | } | |
1080 | } | |
1081 | ||
376253ec | 1082 | static void monitor_printc(Monitor *mon, int c) |
9307c4c1 | 1083 | { |
376253ec | 1084 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1085 | switch(c) { |
1086 | case '\'': | |
376253ec | 1087 | monitor_printf(mon, "\\'"); |
9307c4c1 FB |
1088 | break; |
1089 | case '\\': | |
376253ec | 1090 | monitor_printf(mon, "\\\\"); |
9307c4c1 FB |
1091 | break; |
1092 | case '\n': | |
376253ec | 1093 | monitor_printf(mon, "\\n"); |
9307c4c1 FB |
1094 | break; |
1095 | case '\r': | |
376253ec | 1096 | monitor_printf(mon, "\\r"); |
9307c4c1 FB |
1097 | break; |
1098 | default: | |
1099 | if (c >= 32 && c <= 126) { | |
376253ec | 1100 | monitor_printf(mon, "%c", c); |
9307c4c1 | 1101 | } else { |
376253ec | 1102 | monitor_printf(mon, "\\x%02x", c); |
9307c4c1 FB |
1103 | } |
1104 | break; | |
1105 | } | |
376253ec | 1106 | monitor_printf(mon, "'"); |
9307c4c1 FB |
1107 | } |
1108 | ||
376253ec | 1109 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
c227f099 | 1110 | target_phys_addr_t addr, int is_physical) |
9307c4c1 | 1111 | { |
6a00d601 | 1112 | CPUState *env; |
9307c4c1 FB |
1113 | int nb_per_line, l, line_size, i, max_digits, len; |
1114 | uint8_t buf[16]; | |
1115 | uint64_t v; | |
1116 | ||
1117 | if (format == 'i') { | |
1118 | int flags; | |
1119 | flags = 0; | |
6a00d601 FB |
1120 | env = mon_get_cpu(); |
1121 | if (!env && !is_physical) | |
1122 | return; | |
9307c4c1 | 1123 | #ifdef TARGET_I386 |
4c27ba27 | 1124 | if (wsize == 2) { |
9307c4c1 | 1125 | flags = 1; |
4c27ba27 FB |
1126 | } else if (wsize == 4) { |
1127 | flags = 0; | |
1128 | } else { | |
6a15fd12 | 1129 | /* as default we use the current CS size */ |
4c27ba27 | 1130 | flags = 0; |
6a15fd12 FB |
1131 | if (env) { |
1132 | #ifdef TARGET_X86_64 | |
5fafdf24 | 1133 | if ((env->efer & MSR_EFER_LMA) && |
6a15fd12 FB |
1134 | (env->segs[R_CS].flags & DESC_L_MASK)) |
1135 | flags = 2; | |
1136 | else | |
1137 | #endif | |
1138 | if (!(env->segs[R_CS].flags & DESC_B_MASK)) | |
1139 | flags = 1; | |
1140 | } | |
4c27ba27 FB |
1141 | } |
1142 | #endif | |
376253ec | 1143 | monitor_disas(mon, env, addr, count, is_physical, flags); |
9307c4c1 FB |
1144 | return; |
1145 | } | |
1146 | ||
1147 | len = wsize * count; | |
1148 | if (wsize == 1) | |
1149 | line_size = 8; | |
1150 | else | |
1151 | line_size = 16; | |
1152 | nb_per_line = line_size / wsize; | |
1153 | max_digits = 0; | |
1154 | ||
1155 | switch(format) { | |
1156 | case 'o': | |
1157 | max_digits = (wsize * 8 + 2) / 3; | |
1158 | break; | |
1159 | default: | |
1160 | case 'x': | |
1161 | max_digits = (wsize * 8) / 4; | |
1162 | break; | |
1163 | case 'u': | |
1164 | case 'd': | |
1165 | max_digits = (wsize * 8 * 10 + 32) / 33; | |
1166 | break; | |
1167 | case 'c': | |
1168 | wsize = 1; | |
1169 | break; | |
1170 | } | |
1171 | ||
1172 | while (len > 0) { | |
7743e588 | 1173 | if (is_physical) |
376253ec | 1174 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
7743e588 | 1175 | else |
376253ec | 1176 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
9307c4c1 FB |
1177 | l = len; |
1178 | if (l > line_size) | |
1179 | l = line_size; | |
1180 | if (is_physical) { | |
1181 | cpu_physical_memory_rw(addr, buf, l, 0); | |
1182 | } else { | |
6a00d601 FB |
1183 | env = mon_get_cpu(); |
1184 | if (!env) | |
1185 | break; | |
c8f79b67 | 1186 | if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) { |
376253ec | 1187 | monitor_printf(mon, " Cannot access memory\n"); |
c8f79b67 AL |
1188 | break; |
1189 | } | |
9307c4c1 | 1190 | } |
5fafdf24 | 1191 | i = 0; |
9307c4c1 FB |
1192 | while (i < l) { |
1193 | switch(wsize) { | |
1194 | default: | |
1195 | case 1: | |
1196 | v = ldub_raw(buf + i); | |
1197 | break; | |
1198 | case 2: | |
1199 | v = lduw_raw(buf + i); | |
1200 | break; | |
1201 | case 4: | |
92a31b1f | 1202 | v = (uint32_t)ldl_raw(buf + i); |
9307c4c1 FB |
1203 | break; |
1204 | case 8: | |
1205 | v = ldq_raw(buf + i); | |
1206 | break; | |
1207 | } | |
376253ec | 1208 | monitor_printf(mon, " "); |
9307c4c1 FB |
1209 | switch(format) { |
1210 | case 'o': | |
376253ec | 1211 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
9307c4c1 FB |
1212 | break; |
1213 | case 'x': | |
376253ec | 1214 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
9307c4c1 FB |
1215 | break; |
1216 | case 'u': | |
376253ec | 1217 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
9307c4c1 FB |
1218 | break; |
1219 | case 'd': | |
376253ec | 1220 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
9307c4c1 FB |
1221 | break; |
1222 | case 'c': | |
376253ec | 1223 | monitor_printc(mon, v); |
9307c4c1 FB |
1224 | break; |
1225 | } | |
1226 | i += wsize; | |
1227 | } | |
376253ec | 1228 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1229 | addr += l; |
1230 | len -= l; | |
1231 | } | |
1232 | } | |
1233 | ||
1bd1442e | 1234 | static void do_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1235 | { |
1bd1442e LC |
1236 | int count = qdict_get_int(qdict, "count"); |
1237 | int format = qdict_get_int(qdict, "format"); | |
1238 | int size = qdict_get_int(qdict, "size"); | |
1239 | target_long addr = qdict_get_int(qdict, "addr"); | |
1240 | ||
376253ec | 1241 | memory_dump(mon, count, format, size, addr, 0); |
9307c4c1 FB |
1242 | } |
1243 | ||
1bd1442e | 1244 | static void do_physical_memory_dump(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1245 | { |
1bd1442e LC |
1246 | int count = qdict_get_int(qdict, "count"); |
1247 | int format = qdict_get_int(qdict, "format"); | |
1248 | int size = qdict_get_int(qdict, "size"); | |
c227f099 | 1249 | target_phys_addr_t addr = qdict_get_int(qdict, "addr"); |
1bd1442e | 1250 | |
376253ec | 1251 | memory_dump(mon, count, format, size, addr, 1); |
9307c4c1 FB |
1252 | } |
1253 | ||
1bd1442e | 1254 | static void do_print(Monitor *mon, const QDict *qdict) |
9307c4c1 | 1255 | { |
1bd1442e | 1256 | int format = qdict_get_int(qdict, "format"); |
c227f099 | 1257 | target_phys_addr_t val = qdict_get_int(qdict, "val"); |
1bd1442e | 1258 | |
7743e588 | 1259 | #if TARGET_PHYS_ADDR_BITS == 32 |
9307c4c1 FB |
1260 | switch(format) { |
1261 | case 'o': | |
376253ec | 1262 | monitor_printf(mon, "%#o", val); |
9307c4c1 FB |
1263 | break; |
1264 | case 'x': | |
376253ec | 1265 | monitor_printf(mon, "%#x", val); |
9307c4c1 FB |
1266 | break; |
1267 | case 'u': | |
376253ec | 1268 | monitor_printf(mon, "%u", val); |
9307c4c1 FB |
1269 | break; |
1270 | default: | |
1271 | case 'd': | |
376253ec | 1272 | monitor_printf(mon, "%d", val); |
9307c4c1 FB |
1273 | break; |
1274 | case 'c': | |
376253ec | 1275 | monitor_printc(mon, val); |
9307c4c1 FB |
1276 | break; |
1277 | } | |
92a31b1f FB |
1278 | #else |
1279 | switch(format) { | |
1280 | case 'o': | |
376253ec | 1281 | monitor_printf(mon, "%#" PRIo64, val); |
92a31b1f FB |
1282 | break; |
1283 | case 'x': | |
376253ec | 1284 | monitor_printf(mon, "%#" PRIx64, val); |
92a31b1f FB |
1285 | break; |
1286 | case 'u': | |
376253ec | 1287 | monitor_printf(mon, "%" PRIu64, val); |
92a31b1f FB |
1288 | break; |
1289 | default: | |
1290 | case 'd': | |
376253ec | 1291 | monitor_printf(mon, "%" PRId64, val); |
92a31b1f FB |
1292 | break; |
1293 | case 'c': | |
376253ec | 1294 | monitor_printc(mon, val); |
92a31b1f FB |
1295 | break; |
1296 | } | |
1297 | #endif | |
376253ec | 1298 | monitor_printf(mon, "\n"); |
9307c4c1 FB |
1299 | } |
1300 | ||
57e09454 | 1301 | static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data) |
b371dc59 FB |
1302 | { |
1303 | FILE *f; | |
afe67ef2 LC |
1304 | uint32_t size = qdict_get_int(qdict, "size"); |
1305 | const char *filename = qdict_get_str(qdict, "filename"); | |
1306 | target_long addr = qdict_get_int(qdict, "val"); | |
b371dc59 FB |
1307 | uint32_t l; |
1308 | CPUState *env; | |
1309 | uint8_t buf[1024]; | |
1310 | ||
1311 | env = mon_get_cpu(); | |
1312 | if (!env) | |
1313 | return; | |
1314 | ||
1315 | f = fopen(filename, "wb"); | |
1316 | if (!f) { | |
376253ec | 1317 | monitor_printf(mon, "could not open '%s'\n", filename); |
b371dc59 FB |
1318 | return; |
1319 | } | |
1320 | while (size != 0) { | |
1321 | l = sizeof(buf); | |
1322 | if (l > size) | |
1323 | l = size; | |
1324 | cpu_memory_rw_debug(env, addr, buf, l, 0); | |
1325 | fwrite(buf, 1, l, f); | |
1326 | addr += l; | |
1327 | size -= l; | |
1328 | } | |
1329 | fclose(f); | |
1330 | } | |
1331 | ||
18f5a8bf LC |
1332 | static void do_physical_memory_save(Monitor *mon, const QDict *qdict, |
1333 | QObject **ret_data) | |
a8bdf7a6 AJ |
1334 | { |
1335 | FILE *f; | |
1336 | uint32_t l; | |
1337 | uint8_t buf[1024]; | |
afe67ef2 LC |
1338 | uint32_t size = qdict_get_int(qdict, "size"); |
1339 | const char *filename = qdict_get_str(qdict, "filename"); | |
c227f099 | 1340 | target_phys_addr_t addr = qdict_get_int(qdict, "val"); |
a8bdf7a6 AJ |
1341 | |
1342 | f = fopen(filename, "wb"); | |
1343 | if (!f) { | |
376253ec | 1344 | monitor_printf(mon, "could not open '%s'\n", filename); |
a8bdf7a6 AJ |
1345 | return; |
1346 | } | |
1347 | while (size != 0) { | |
1348 | l = sizeof(buf); | |
1349 | if (l > size) | |
1350 | l = size; | |
1351 | cpu_physical_memory_rw(addr, buf, l, 0); | |
1352 | fwrite(buf, 1, l, f); | |
1353 | fflush(f); | |
1354 | addr += l; | |
1355 | size -= l; | |
1356 | } | |
1357 | fclose(f); | |
1358 | } | |
1359 | ||
f18c16de | 1360 | static void do_sum(Monitor *mon, const QDict *qdict) |
e4cf1adc FB |
1361 | { |
1362 | uint32_t addr; | |
1363 | uint8_t buf[1]; | |
1364 | uint16_t sum; | |
f18c16de LC |
1365 | uint32_t start = qdict_get_int(qdict, "start"); |
1366 | uint32_t size = qdict_get_int(qdict, "size"); | |
e4cf1adc FB |
1367 | |
1368 | sum = 0; | |
1369 | for(addr = start; addr < (start + size); addr++) { | |
1370 | cpu_physical_memory_rw(addr, buf, 1, 0); | |
1371 | /* BSD sum algorithm ('sum' Unix command) */ | |
1372 | sum = (sum >> 1) | (sum << 15); | |
1373 | sum += buf[0]; | |
1374 | } | |
376253ec | 1375 | monitor_printf(mon, "%05d\n", sum); |
e4cf1adc FB |
1376 | } |
1377 | ||
a3a91a35 FB |
1378 | typedef struct { |
1379 | int keycode; | |
1380 | const char *name; | |
1381 | } KeyDef; | |
1382 | ||
1383 | static const KeyDef key_defs[] = { | |
1384 | { 0x2a, "shift" }, | |
1385 | { 0x36, "shift_r" }, | |
3b46e624 | 1386 | |
a3a91a35 FB |
1387 | { 0x38, "alt" }, |
1388 | { 0xb8, "alt_r" }, | |
2ba27c7f TS |
1389 | { 0x64, "altgr" }, |
1390 | { 0xe4, "altgr_r" }, | |
a3a91a35 FB |
1391 | { 0x1d, "ctrl" }, |
1392 | { 0x9d, "ctrl_r" }, | |
1393 | ||
1394 | { 0xdd, "menu" }, | |
1395 | ||
1396 | { 0x01, "esc" }, | |
1397 | ||
1398 | { 0x02, "1" }, | |
1399 | { 0x03, "2" }, | |
1400 | { 0x04, "3" }, | |
1401 | { 0x05, "4" }, | |
1402 | { 0x06, "5" }, | |
1403 | { 0x07, "6" }, | |
1404 | { 0x08, "7" }, | |
1405 | { 0x09, "8" }, | |
1406 | { 0x0a, "9" }, | |
1407 | { 0x0b, "0" }, | |
64866c3d FB |
1408 | { 0x0c, "minus" }, |
1409 | { 0x0d, "equal" }, | |
a3a91a35 FB |
1410 | { 0x0e, "backspace" }, |
1411 | ||
1412 | { 0x0f, "tab" }, | |
1413 | { 0x10, "q" }, | |
1414 | { 0x11, "w" }, | |
1415 | { 0x12, "e" }, | |
1416 | { 0x13, "r" }, | |
1417 | { 0x14, "t" }, | |
1418 | { 0x15, "y" }, | |
1419 | { 0x16, "u" }, | |
1420 | { 0x17, "i" }, | |
1421 | { 0x18, "o" }, | |
1422 | { 0x19, "p" }, | |
1423 | ||
1424 | { 0x1c, "ret" }, | |
1425 | ||
1426 | { 0x1e, "a" }, | |
1427 | { 0x1f, "s" }, | |
1428 | { 0x20, "d" }, | |
1429 | { 0x21, "f" }, | |
1430 | { 0x22, "g" }, | |
1431 | { 0x23, "h" }, | |
1432 | { 0x24, "j" }, | |
1433 | { 0x25, "k" }, | |
1434 | { 0x26, "l" }, | |
1435 | ||
1436 | { 0x2c, "z" }, | |
1437 | { 0x2d, "x" }, | |
1438 | { 0x2e, "c" }, | |
1439 | { 0x2f, "v" }, | |
1440 | { 0x30, "b" }, | |
1441 | { 0x31, "n" }, | |
1442 | { 0x32, "m" }, | |
9155fc45 AJ |
1443 | { 0x33, "comma" }, |
1444 | { 0x34, "dot" }, | |
1445 | { 0x35, "slash" }, | |
3b46e624 | 1446 | |
4d3b6f6e AZ |
1447 | { 0x37, "asterisk" }, |
1448 | ||
a3a91a35 | 1449 | { 0x39, "spc" }, |
00ffa62a | 1450 | { 0x3a, "caps_lock" }, |
a3a91a35 FB |
1451 | { 0x3b, "f1" }, |
1452 | { 0x3c, "f2" }, | |
1453 | { 0x3d, "f3" }, | |
1454 | { 0x3e, "f4" }, | |
1455 | { 0x3f, "f5" }, | |
1456 | { 0x40, "f6" }, | |
1457 | { 0x41, "f7" }, | |
1458 | { 0x42, "f8" }, | |
1459 | { 0x43, "f9" }, | |
1460 | { 0x44, "f10" }, | |
00ffa62a | 1461 | { 0x45, "num_lock" }, |
a3a91a35 FB |
1462 | { 0x46, "scroll_lock" }, |
1463 | ||
64866c3d FB |
1464 | { 0xb5, "kp_divide" }, |
1465 | { 0x37, "kp_multiply" }, | |
0cfec834 | 1466 | { 0x4a, "kp_subtract" }, |
64866c3d FB |
1467 | { 0x4e, "kp_add" }, |
1468 | { 0x9c, "kp_enter" }, | |
1469 | { 0x53, "kp_decimal" }, | |
f2289cb6 | 1470 | { 0x54, "sysrq" }, |
64866c3d FB |
1471 | |
1472 | { 0x52, "kp_0" }, | |
1473 | { 0x4f, "kp_1" }, | |
1474 | { 0x50, "kp_2" }, | |
1475 | { 0x51, "kp_3" }, | |
1476 | { 0x4b, "kp_4" }, | |
1477 | { 0x4c, "kp_5" }, | |
1478 | { 0x4d, "kp_6" }, | |
1479 | { 0x47, "kp_7" }, | |
1480 | { 0x48, "kp_8" }, | |
1481 | { 0x49, "kp_9" }, | |
3b46e624 | 1482 | |
a3a91a35 FB |
1483 | { 0x56, "<" }, |
1484 | ||
1485 | { 0x57, "f11" }, | |
1486 | { 0x58, "f12" }, | |
1487 | ||
1488 | { 0xb7, "print" }, | |
1489 | ||
1490 | { 0xc7, "home" }, | |
1491 | { 0xc9, "pgup" }, | |
1492 | { 0xd1, "pgdn" }, | |
1493 | { 0xcf, "end" }, | |
1494 | ||
1495 | { 0xcb, "left" }, | |
1496 | { 0xc8, "up" }, | |
1497 | { 0xd0, "down" }, | |
1498 | { 0xcd, "right" }, | |
1499 | ||
1500 | { 0xd2, "insert" }, | |
1501 | { 0xd3, "delete" }, | |
c0b5b109 BS |
1502 | #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64) |
1503 | { 0xf0, "stop" }, | |
1504 | { 0xf1, "again" }, | |
1505 | { 0xf2, "props" }, | |
1506 | { 0xf3, "undo" }, | |
1507 | { 0xf4, "front" }, | |
1508 | { 0xf5, "copy" }, | |
1509 | { 0xf6, "open" }, | |
1510 | { 0xf7, "paste" }, | |
1511 | { 0xf8, "find" }, | |
1512 | { 0xf9, "cut" }, | |
1513 | { 0xfa, "lf" }, | |
1514 | { 0xfb, "help" }, | |
1515 | { 0xfc, "meta_l" }, | |
1516 | { 0xfd, "meta_r" }, | |
1517 | { 0xfe, "compose" }, | |
1518 | #endif | |
a3a91a35 FB |
1519 | { 0, NULL }, |
1520 | }; | |
1521 | ||
1522 | static int get_keycode(const char *key) | |
1523 | { | |
1524 | const KeyDef *p; | |
64866c3d FB |
1525 | char *endp; |
1526 | int ret; | |
a3a91a35 FB |
1527 | |
1528 | for(p = key_defs; p->name != NULL; p++) { | |
1529 | if (!strcmp(key, p->name)) | |
1530 | return p->keycode; | |
1531 | } | |
64866c3d FB |
1532 | if (strstart(key, "0x", NULL)) { |
1533 | ret = strtoul(key, &endp, 0); | |
1534 | if (*endp == '\0' && ret >= 0x01 && ret <= 0xff) | |
1535 | return ret; | |
1536 | } | |
a3a91a35 FB |
1537 | return -1; |
1538 | } | |
1539 | ||
c8256f9d AZ |
1540 | #define MAX_KEYCODES 16 |
1541 | static uint8_t keycodes[MAX_KEYCODES]; | |
1542 | static int nb_pending_keycodes; | |
1543 | static QEMUTimer *key_timer; | |
1544 | ||
1545 | static void release_keys(void *opaque) | |
1546 | { | |
1547 | int keycode; | |
1548 | ||
1549 | while (nb_pending_keycodes > 0) { | |
1550 | nb_pending_keycodes--; | |
1551 | keycode = keycodes[nb_pending_keycodes]; | |
1552 | if (keycode & 0x80) | |
1553 | kbd_put_keycode(0xe0); | |
1554 | kbd_put_keycode(keycode | 0x80); | |
1555 | } | |
1556 | } | |
1557 | ||
1d4daa91 | 1558 | static void do_sendkey(Monitor *mon, const QDict *qdict) |
a3a91a35 | 1559 | { |
3401c0d9 AZ |
1560 | char keyname_buf[16]; |
1561 | char *separator; | |
1562 | int keyname_len, keycode, i; | |
1d4daa91 LC |
1563 | const char *string = qdict_get_str(qdict, "string"); |
1564 | int has_hold_time = qdict_haskey(qdict, "hold_time"); | |
1565 | int hold_time = qdict_get_try_int(qdict, "hold_time", -1); | |
3401c0d9 | 1566 | |
c8256f9d AZ |
1567 | if (nb_pending_keycodes > 0) { |
1568 | qemu_del_timer(key_timer); | |
1569 | release_keys(NULL); | |
1570 | } | |
1571 | if (!has_hold_time) | |
1572 | hold_time = 100; | |
1573 | i = 0; | |
3401c0d9 AZ |
1574 | while (1) { |
1575 | separator = strchr(string, '-'); | |
1576 | keyname_len = separator ? separator - string : strlen(string); | |
1577 | if (keyname_len > 0) { | |
1578 | pstrcpy(keyname_buf, sizeof(keyname_buf), string); | |
1579 | if (keyname_len > sizeof(keyname_buf) - 1) { | |
376253ec | 1580 | monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf); |
3401c0d9 | 1581 | return; |
a3a91a35 | 1582 | } |
c8256f9d | 1583 | if (i == MAX_KEYCODES) { |
376253ec | 1584 | monitor_printf(mon, "too many keys\n"); |
3401c0d9 AZ |
1585 | return; |
1586 | } | |
1587 | keyname_buf[keyname_len] = 0; | |
1588 | keycode = get_keycode(keyname_buf); | |
1589 | if (keycode < 0) { | |
376253ec | 1590 | monitor_printf(mon, "unknown key: '%s'\n", keyname_buf); |
3401c0d9 AZ |
1591 | return; |
1592 | } | |
c8256f9d | 1593 | keycodes[i++] = keycode; |
a3a91a35 | 1594 | } |
3401c0d9 | 1595 | if (!separator) |
a3a91a35 | 1596 | break; |
3401c0d9 | 1597 | string = separator + 1; |
a3a91a35 | 1598 | } |
c8256f9d | 1599 | nb_pending_keycodes = i; |
a3a91a35 | 1600 | /* key down events */ |
c8256f9d | 1601 | for (i = 0; i < nb_pending_keycodes; i++) { |
a3a91a35 FB |
1602 | keycode = keycodes[i]; |
1603 | if (keycode & 0x80) | |
1604 | kbd_put_keycode(0xe0); | |
1605 | kbd_put_keycode(keycode & 0x7f); | |
1606 | } | |
c8256f9d | 1607 | /* delayed key up events */ |
f227f17d | 1608 | qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) + |
6ee093c9 | 1609 | muldiv64(get_ticks_per_sec(), hold_time, 1000)); |
a3a91a35 FB |
1610 | } |
1611 | ||
13224a87 FB |
1612 | static int mouse_button_state; |
1613 | ||
1d4daa91 | 1614 | static void do_mouse_move(Monitor *mon, const QDict *qdict) |
13224a87 FB |
1615 | { |
1616 | int dx, dy, dz; | |
1d4daa91 LC |
1617 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
1618 | const char *dy_str = qdict_get_str(qdict, "dy_str"); | |
1619 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); | |
13224a87 FB |
1620 | dx = strtol(dx_str, NULL, 0); |
1621 | dy = strtol(dy_str, NULL, 0); | |
1622 | dz = 0; | |
5fafdf24 | 1623 | if (dz_str) |
13224a87 FB |
1624 | dz = strtol(dz_str, NULL, 0); |
1625 | kbd_mouse_event(dx, dy, dz, mouse_button_state); | |
1626 | } | |
1627 | ||
d54908a5 | 1628 | static void do_mouse_button(Monitor *mon, const QDict *qdict) |
13224a87 | 1629 | { |
d54908a5 | 1630 | int button_state = qdict_get_int(qdict, "button_state"); |
13224a87 FB |
1631 | mouse_button_state = button_state; |
1632 | kbd_mouse_event(0, 0, 0, mouse_button_state); | |
1633 | } | |
1634 | ||
aa93e39c | 1635 | static void do_ioport_read(Monitor *mon, const QDict *qdict) |
3440557b | 1636 | { |
aa93e39c LC |
1637 | int size = qdict_get_int(qdict, "size"); |
1638 | int addr = qdict_get_int(qdict, "addr"); | |
1639 | int has_index = qdict_haskey(qdict, "index"); | |
3440557b FB |
1640 | uint32_t val; |
1641 | int suffix; | |
1642 | ||
1643 | if (has_index) { | |
aa93e39c | 1644 | int index = qdict_get_int(qdict, "index"); |
afcea8cb | 1645 | cpu_outb(addr & IOPORTS_MASK, index & 0xff); |
3440557b FB |
1646 | addr++; |
1647 | } | |
1648 | addr &= 0xffff; | |
1649 | ||
1650 | switch(size) { | |
1651 | default: | |
1652 | case 1: | |
afcea8cb | 1653 | val = cpu_inb(addr); |
3440557b FB |
1654 | suffix = 'b'; |
1655 | break; | |
1656 | case 2: | |
afcea8cb | 1657 | val = cpu_inw(addr); |
3440557b FB |
1658 | suffix = 'w'; |
1659 | break; | |
1660 | case 4: | |
afcea8cb | 1661 | val = cpu_inl(addr); |
3440557b FB |
1662 | suffix = 'l'; |
1663 | break; | |
1664 | } | |
376253ec AL |
1665 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
1666 | suffix, addr, size * 2, val); | |
3440557b | 1667 | } |
a3a91a35 | 1668 | |
1bd1442e | 1669 | static void do_ioport_write(Monitor *mon, const QDict *qdict) |
f114784f | 1670 | { |
1bd1442e LC |
1671 | int size = qdict_get_int(qdict, "size"); |
1672 | int addr = qdict_get_int(qdict, "addr"); | |
1673 | int val = qdict_get_int(qdict, "val"); | |
1674 | ||
f114784f JK |
1675 | addr &= IOPORTS_MASK; |
1676 | ||
1677 | switch (size) { | |
1678 | default: | |
1679 | case 1: | |
afcea8cb | 1680 | cpu_outb(addr, val); |
f114784f JK |
1681 | break; |
1682 | case 2: | |
afcea8cb | 1683 | cpu_outw(addr, val); |
f114784f JK |
1684 | break; |
1685 | case 4: | |
afcea8cb | 1686 | cpu_outl(addr, val); |
f114784f JK |
1687 | break; |
1688 | } | |
1689 | } | |
1690 | ||
d54908a5 | 1691 | static void do_boot_set(Monitor *mon, const QDict *qdict) |
0ecdffbb AJ |
1692 | { |
1693 | int res; | |
d54908a5 | 1694 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
0ecdffbb | 1695 | |
76e30d0f JK |
1696 | res = qemu_boot_set(bootdevice); |
1697 | if (res == 0) { | |
1698 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); | |
1699 | } else if (res > 0) { | |
1700 | monitor_printf(mon, "setting boot device list failed\n"); | |
0ecdffbb | 1701 | } else { |
376253ec AL |
1702 | monitor_printf(mon, "no function defined to set boot device list for " |
1703 | "this architecture\n"); | |
0ecdffbb AJ |
1704 | } |
1705 | } | |
1706 | ||
c80d259e LC |
1707 | /** |
1708 | * do_system_reset(): Issue a machine reset | |
1709 | */ | |
1710 | static void do_system_reset(Monitor *mon, const QDict *qdict, | |
1711 | QObject **ret_data) | |
e4f9082b FB |
1712 | { |
1713 | qemu_system_reset_request(); | |
1714 | } | |
1715 | ||
43076664 LC |
1716 | /** |
1717 | * do_system_powerdown(): Issue a machine powerdown | |
1718 | */ | |
1719 | static void do_system_powerdown(Monitor *mon, const QDict *qdict, | |
1720 | QObject **ret_data) | |
3475187d FB |
1721 | { |
1722 | qemu_system_powerdown_request(); | |
1723 | } | |
1724 | ||
b86bda5b | 1725 | #if defined(TARGET_I386) |
376253ec | 1726 | static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask) |
b86bda5b | 1727 | { |
376253ec AL |
1728 | monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n", |
1729 | addr, | |
1730 | pte & mask, | |
1731 | pte & PG_GLOBAL_MASK ? 'G' : '-', | |
1732 | pte & PG_PSE_MASK ? 'P' : '-', | |
1733 | pte & PG_DIRTY_MASK ? 'D' : '-', | |
1734 | pte & PG_ACCESSED_MASK ? 'A' : '-', | |
1735 | pte & PG_PCD_MASK ? 'C' : '-', | |
1736 | pte & PG_PWT_MASK ? 'T' : '-', | |
1737 | pte & PG_USER_MASK ? 'U' : '-', | |
1738 | pte & PG_RW_MASK ? 'W' : '-'); | |
b86bda5b FB |
1739 | } |
1740 | ||
376253ec | 1741 | static void tlb_info(Monitor *mon) |
b86bda5b | 1742 | { |
6a00d601 | 1743 | CPUState *env; |
b86bda5b FB |
1744 | int l1, l2; |
1745 | uint32_t pgd, pde, pte; | |
1746 | ||
6a00d601 FB |
1747 | env = mon_get_cpu(); |
1748 | if (!env) | |
1749 | return; | |
1750 | ||
b86bda5b | 1751 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1752 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1753 | return; |
1754 | } | |
1755 | pgd = env->cr[3] & ~0xfff; | |
1756 | for(l1 = 0; l1 < 1024; l1++) { | |
1757 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1758 | pde = le32_to_cpu(pde); | |
1759 | if (pde & PG_PRESENT_MASK) { | |
1760 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
376253ec | 1761 | print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1)); |
b86bda5b FB |
1762 | } else { |
1763 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1764 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1765 | (uint8_t *)&pte, 4); |
1766 | pte = le32_to_cpu(pte); | |
1767 | if (pte & PG_PRESENT_MASK) { | |
376253ec | 1768 | print_pte(mon, (l1 << 22) + (l2 << 12), |
5fafdf24 | 1769 | pte & ~PG_PSE_MASK, |
b86bda5b FB |
1770 | ~0xfff); |
1771 | } | |
1772 | } | |
1773 | } | |
1774 | } | |
1775 | } | |
1776 | } | |
1777 | ||
376253ec | 1778 | static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot, |
b86bda5b FB |
1779 | uint32_t end, int prot) |
1780 | { | |
9746b15b FB |
1781 | int prot1; |
1782 | prot1 = *plast_prot; | |
1783 | if (prot != prot1) { | |
b86bda5b | 1784 | if (*pstart != -1) { |
376253ec AL |
1785 | monitor_printf(mon, "%08x-%08x %08x %c%c%c\n", |
1786 | *pstart, end, end - *pstart, | |
1787 | prot1 & PG_USER_MASK ? 'u' : '-', | |
1788 | 'r', | |
1789 | prot1 & PG_RW_MASK ? 'w' : '-'); | |
b86bda5b FB |
1790 | } |
1791 | if (prot != 0) | |
1792 | *pstart = end; | |
1793 | else | |
1794 | *pstart = -1; | |
1795 | *plast_prot = prot; | |
1796 | } | |
1797 | } | |
1798 | ||
376253ec | 1799 | static void mem_info(Monitor *mon) |
b86bda5b | 1800 | { |
6a00d601 | 1801 | CPUState *env; |
b86bda5b FB |
1802 | int l1, l2, prot, last_prot; |
1803 | uint32_t pgd, pde, pte, start, end; | |
1804 | ||
6a00d601 FB |
1805 | env = mon_get_cpu(); |
1806 | if (!env) | |
1807 | return; | |
1808 | ||
b86bda5b | 1809 | if (!(env->cr[0] & CR0_PG_MASK)) { |
376253ec | 1810 | monitor_printf(mon, "PG disabled\n"); |
b86bda5b FB |
1811 | return; |
1812 | } | |
1813 | pgd = env->cr[3] & ~0xfff; | |
1814 | last_prot = 0; | |
1815 | start = -1; | |
1816 | for(l1 = 0; l1 < 1024; l1++) { | |
1817 | cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4); | |
1818 | pde = le32_to_cpu(pde); | |
1819 | end = l1 << 22; | |
1820 | if (pde & PG_PRESENT_MASK) { | |
1821 | if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) { | |
1822 | prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
376253ec | 1823 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1824 | } else { |
1825 | for(l2 = 0; l2 < 1024; l2++) { | |
5fafdf24 | 1826 | cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, |
b86bda5b FB |
1827 | (uint8_t *)&pte, 4); |
1828 | pte = le32_to_cpu(pte); | |
1829 | end = (l1 << 22) + (l2 << 12); | |
1830 | if (pte & PG_PRESENT_MASK) { | |
1831 | prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK); | |
1832 | } else { | |
1833 | prot = 0; | |
1834 | } | |
376253ec | 1835 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1836 | } |
1837 | } | |
1838 | } else { | |
1839 | prot = 0; | |
376253ec | 1840 | mem_print(mon, &start, &last_prot, end, prot); |
b86bda5b FB |
1841 | } |
1842 | } | |
1843 | } | |
1844 | #endif | |
1845 | ||
7c664e2f AJ |
1846 | #if defined(TARGET_SH4) |
1847 | ||
376253ec | 1848 | static void print_tlb(Monitor *mon, int idx, tlb_t *tlb) |
7c664e2f | 1849 | { |
376253ec AL |
1850 | monitor_printf(mon, " tlb%i:\t" |
1851 | "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t" | |
1852 | "v=%hhu shared=%hhu cached=%hhu prot=%hhu " | |
1853 | "dirty=%hhu writethrough=%hhu\n", | |
1854 | idx, | |
1855 | tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size, | |
1856 | tlb->v, tlb->sh, tlb->c, tlb->pr, | |
1857 | tlb->d, tlb->wt); | |
7c664e2f AJ |
1858 | } |
1859 | ||
376253ec | 1860 | static void tlb_info(Monitor *mon) |
7c664e2f AJ |
1861 | { |
1862 | CPUState *env = mon_get_cpu(); | |
1863 | int i; | |
1864 | ||
376253ec | 1865 | monitor_printf (mon, "ITLB:\n"); |
7c664e2f | 1866 | for (i = 0 ; i < ITLB_SIZE ; i++) |
376253ec AL |
1867 | print_tlb (mon, i, &env->itlb[i]); |
1868 | monitor_printf (mon, "UTLB:\n"); | |
7c664e2f | 1869 | for (i = 0 ; i < UTLB_SIZE ; i++) |
376253ec | 1870 | print_tlb (mon, i, &env->utlb[i]); |
7c664e2f AJ |
1871 | } |
1872 | ||
1873 | #endif | |
1874 | ||
94f539bd | 1875 | static void do_info_kvm_print(Monitor *mon, const QObject *data) |
7ba1e619 | 1876 | { |
94f539bd LC |
1877 | QDict *qdict; |
1878 | ||
1879 | qdict = qobject_to_qdict(data); | |
1880 | ||
376253ec | 1881 | monitor_printf(mon, "kvm support: "); |
94f539bd LC |
1882 | if (qdict_get_bool(qdict, "present")) { |
1883 | monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ? | |
1884 | "enabled" : "disabled"); | |
1885 | } else { | |
1886 | monitor_printf(mon, "not compiled\n"); | |
1887 | } | |
1888 | } | |
1889 | ||
1890 | /** | |
1891 | * do_info_kvm(): Show KVM information | |
1892 | * | |
1893 | * Return a QDict with the following information: | |
1894 | * | |
1895 | * - "enabled": true if KVM support is enabled, false otherwise | |
1896 | * - "present": true if QEMU has KVM support, false otherwise | |
1897 | * | |
1898 | * Example: | |
1899 | * | |
1900 | * { "enabled": true, "present": true } | |
1901 | */ | |
1902 | static void do_info_kvm(Monitor *mon, QObject **ret_data) | |
1903 | { | |
1904 | #ifdef CONFIG_KVM | |
1905 | *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }", | |
1906 | kvm_enabled()); | |
7ba1e619 | 1907 | #else |
94f539bd | 1908 | *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }"); |
7ba1e619 AL |
1909 | #endif |
1910 | } | |
1911 | ||
030ea37b AL |
1912 | static void do_info_numa(Monitor *mon) |
1913 | { | |
b28b6230 | 1914 | int i; |
030ea37b AL |
1915 | CPUState *env; |
1916 | ||
1917 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); | |
1918 | for (i = 0; i < nb_numa_nodes; i++) { | |
1919 | monitor_printf(mon, "node %d cpus:", i); | |
1920 | for (env = first_cpu; env != NULL; env = env->next_cpu) { | |
1921 | if (env->numa_node == i) { | |
1922 | monitor_printf(mon, " %d", env->cpu_index); | |
1923 | } | |
1924 | } | |
1925 | monitor_printf(mon, "\n"); | |
1926 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, | |
1927 | node_mem[i] >> 20); | |
1928 | } | |
1929 | } | |
1930 | ||
5f1ce948 FB |
1931 | #ifdef CONFIG_PROFILER |
1932 | ||
e9a6625e AJ |
1933 | int64_t qemu_time; |
1934 | int64_t dev_time; | |
1935 | ||
376253ec | 1936 | static void do_info_profile(Monitor *mon) |
5f1ce948 FB |
1937 | { |
1938 | int64_t total; | |
1939 | total = qemu_time; | |
1940 | if (total == 0) | |
1941 | total = 1; | |
376253ec | 1942 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 1943 | dev_time, dev_time / (double)get_ticks_per_sec()); |
376253ec | 1944 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", |
6ee093c9 | 1945 | qemu_time, qemu_time / (double)get_ticks_per_sec()); |
5f1ce948 | 1946 | qemu_time = 0; |
5f1ce948 | 1947 | dev_time = 0; |
5f1ce948 FB |
1948 | } |
1949 | #else | |
376253ec | 1950 | static void do_info_profile(Monitor *mon) |
5f1ce948 | 1951 | { |
376253ec | 1952 | monitor_printf(mon, "Internal profiler not compiled\n"); |
5f1ce948 FB |
1953 | } |
1954 | #endif | |
1955 | ||
ec36b695 | 1956 | /* Capture support */ |
72cf2d4f | 1957 | static QLIST_HEAD (capture_list_head, CaptureState) capture_head; |
ec36b695 | 1958 | |
376253ec | 1959 | static void do_info_capture(Monitor *mon) |
ec36b695 FB |
1960 | { |
1961 | int i; | |
1962 | CaptureState *s; | |
1963 | ||
1964 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
376253ec | 1965 | monitor_printf(mon, "[%d]: ", i); |
ec36b695 FB |
1966 | s->ops.info (s->opaque); |
1967 | } | |
1968 | } | |
1969 | ||
2313086a | 1970 | #ifdef HAS_AUDIO |
d54908a5 | 1971 | static void do_stop_capture(Monitor *mon, const QDict *qdict) |
ec36b695 FB |
1972 | { |
1973 | int i; | |
d54908a5 | 1974 | int n = qdict_get_int(qdict, "n"); |
ec36b695 FB |
1975 | CaptureState *s; |
1976 | ||
1977 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { | |
1978 | if (i == n) { | |
1979 | s->ops.destroy (s->opaque); | |
72cf2d4f | 1980 | QLIST_REMOVE (s, entries); |
ec36b695 FB |
1981 | qemu_free (s); |
1982 | return; | |
1983 | } | |
1984 | } | |
1985 | } | |
1986 | ||
c1925484 LC |
1987 | static void do_wav_capture(Monitor *mon, const QDict *qdict) |
1988 | { | |
1989 | const char *path = qdict_get_str(qdict, "path"); | |
1990 | int has_freq = qdict_haskey(qdict, "freq"); | |
1991 | int freq = qdict_get_try_int(qdict, "freq", -1); | |
1992 | int has_bits = qdict_haskey(qdict, "bits"); | |
1993 | int bits = qdict_get_try_int(qdict, "bits", -1); | |
1994 | int has_channels = qdict_haskey(qdict, "nchannels"); | |
1995 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); | |
ec36b695 FB |
1996 | CaptureState *s; |
1997 | ||
1998 | s = qemu_mallocz (sizeof (*s)); | |
ec36b695 FB |
1999 | |
2000 | freq = has_freq ? freq : 44100; | |
2001 | bits = has_bits ? bits : 16; | |
2002 | nchannels = has_channels ? nchannels : 2; | |
2003 | ||
2004 | if (wav_start_capture (s, path, freq, bits, nchannels)) { | |
376253ec | 2005 | monitor_printf(mon, "Faied to add wave capture\n"); |
ec36b695 FB |
2006 | qemu_free (s); |
2007 | } | |
72cf2d4f | 2008 | QLIST_INSERT_HEAD (&capture_head, s, entries); |
ec36b695 FB |
2009 | } |
2010 | #endif | |
2011 | ||
dc1c0b74 | 2012 | #if defined(TARGET_I386) |
d54908a5 | 2013 | static void do_inject_nmi(Monitor *mon, const QDict *qdict) |
dc1c0b74 AJ |
2014 | { |
2015 | CPUState *env; | |
d54908a5 | 2016 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
dc1c0b74 AJ |
2017 | |
2018 | for (env = first_cpu; env != NULL; env = env->next_cpu) | |
2019 | if (env->cpu_index == cpu_index) { | |
2020 | cpu_interrupt(env, CPU_INTERRUPT_NMI); | |
2021 | break; | |
2022 | } | |
2023 | } | |
2024 | #endif | |
2025 | ||
e637fd23 | 2026 | static void do_info_status_print(Monitor *mon, const QObject *data) |
6f9c5ee7 | 2027 | { |
e637fd23 LC |
2028 | QDict *qdict; |
2029 | ||
2030 | qdict = qobject_to_qdict(data); | |
2031 | ||
2032 | monitor_printf(mon, "VM status: "); | |
2033 | if (qdict_get_bool(qdict, "running")) { | |
2034 | monitor_printf(mon, "running"); | |
2035 | if (qdict_get_bool(qdict, "singlestep")) { | |
2036 | monitor_printf(mon, " (single step mode)"); | |
1b530a6d | 2037 | } |
e637fd23 LC |
2038 | } else { |
2039 | monitor_printf(mon, "paused"); | |
2040 | } | |
2041 | ||
2042 | monitor_printf(mon, "\n"); | |
2043 | } | |
2044 | ||
2045 | /** | |
2046 | * do_info_status(): VM status | |
2047 | * | |
2048 | * Return a QDict with the following information: | |
2049 | * | |
2050 | * - "running": true if the VM is running, or false if it is paused | |
2051 | * - "singlestep": true if the VM is in single step mode, false otherwise | |
2052 | * | |
2053 | * Example: | |
2054 | * | |
2055 | * { "running": true, "singlestep": false } | |
2056 | */ | |
2057 | static void do_info_status(Monitor *mon, QObject **ret_data) | |
2058 | { | |
2059 | *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }", | |
2060 | vm_running, singlestep); | |
6f9c5ee7 AJ |
2061 | } |
2062 | ||
97b766df LC |
2063 | static ram_addr_t balloon_get_value(void) |
2064 | { | |
2065 | ram_addr_t actual; | |
2066 | ||
2067 | if (kvm_enabled() && !kvm_has_sync_mmu()) { | |
2068 | qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon"); | |
2069 | return 0; | |
2070 | } | |
2071 | ||
2072 | actual = qemu_balloon_status(); | |
2073 | if (actual == 0) { | |
2074 | qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon"); | |
2075 | return 0; | |
2076 | } | |
2077 | ||
2078 | return actual; | |
2079 | } | |
2080 | ||
83fb1de2 LC |
2081 | /** |
2082 | * do_balloon(): Request VM to change its memory allocation | |
2083 | */ | |
2084 | static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
df751fa8 | 2085 | { |
97b766df LC |
2086 | if (balloon_get_value()) { |
2087 | /* ballooning is active */ | |
5543b411 | 2088 | qemu_balloon(qdict_get_int(qdict, "value")); |
97b766df | 2089 | } |
df751fa8 AL |
2090 | } |
2091 | ||
cc1d9c70 LC |
2092 | static void monitor_print_balloon(Monitor *mon, const QObject *data) |
2093 | { | |
b0a84d05 LC |
2094 | QDict *qdict; |
2095 | ||
2096 | qdict = qobject_to_qdict(data); | |
2097 | ||
2098 | monitor_printf(mon, "balloon: actual=%" PRId64 "\n", | |
2099 | qdict_get_int(qdict, "balloon") >> 20); | |
cc1d9c70 LC |
2100 | } |
2101 | ||
2102 | /** | |
2103 | * do_info_balloon(): Balloon information | |
b0a84d05 LC |
2104 | * |
2105 | * Return a QDict with the following information: | |
2106 | * | |
2107 | * - "balloon": current balloon value in bytes | |
2108 | * | |
2109 | * Example: | |
2110 | * | |
2111 | * { "balloon": 1073741824 } | |
cc1d9c70 LC |
2112 | */ |
2113 | static void do_info_balloon(Monitor *mon, QObject **ret_data) | |
df751fa8 | 2114 | { |
c227f099 | 2115 | ram_addr_t actual; |
df751fa8 | 2116 | |
97b766df LC |
2117 | actual = balloon_get_value(); |
2118 | if (actual != 0) { | |
b0a84d05 LC |
2119 | *ret_data = qobject_from_jsonf("{ 'balloon': %" PRId64 "}", |
2120 | (int64_t) actual); | |
97b766df | 2121 | } |
df751fa8 AL |
2122 | } |
2123 | ||
15dfcd45 | 2124 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
76655d6d | 2125 | { |
15dfcd45 | 2126 | qemu_acl *acl = qemu_acl_find(name); |
76655d6d | 2127 | |
76655d6d | 2128 | if (!acl) { |
15dfcd45 | 2129 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
76655d6d | 2130 | } |
15dfcd45 JK |
2131 | return acl; |
2132 | } | |
2133 | ||
d54908a5 | 2134 | static void do_acl_show(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2135 | { |
d54908a5 | 2136 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2137 | qemu_acl *acl = find_acl(mon, aclname); |
2138 | qemu_acl_entry *entry; | |
2139 | int i = 0; | |
76655d6d | 2140 | |
15dfcd45 | 2141 | if (acl) { |
28a76be8 | 2142 | monitor_printf(mon, "policy: %s\n", |
76655d6d | 2143 | acl->defaultDeny ? "deny" : "allow"); |
72cf2d4f | 2144 | QTAILQ_FOREACH(entry, &acl->entries, next) { |
28a76be8 AL |
2145 | i++; |
2146 | monitor_printf(mon, "%d: %s %s\n", i, | |
15dfcd45 | 2147 | entry->deny ? "deny" : "allow", entry->match); |
28a76be8 | 2148 | } |
15dfcd45 JK |
2149 | } |
2150 | } | |
2151 | ||
d54908a5 | 2152 | static void do_acl_reset(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2153 | { |
d54908a5 | 2154 | const char *aclname = qdict_get_str(qdict, "aclname"); |
15dfcd45 JK |
2155 | qemu_acl *acl = find_acl(mon, aclname); |
2156 | ||
2157 | if (acl) { | |
28a76be8 AL |
2158 | qemu_acl_reset(acl); |
2159 | monitor_printf(mon, "acl: removed all rules\n"); | |
15dfcd45 JK |
2160 | } |
2161 | } | |
2162 | ||
f18c16de | 2163 | static void do_acl_policy(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2164 | { |
f18c16de LC |
2165 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2166 | const char *policy = qdict_get_str(qdict, "policy"); | |
15dfcd45 | 2167 | qemu_acl *acl = find_acl(mon, aclname); |
28a76be8 | 2168 | |
15dfcd45 JK |
2169 | if (acl) { |
2170 | if (strcmp(policy, "allow") == 0) { | |
28a76be8 AL |
2171 | acl->defaultDeny = 0; |
2172 | monitor_printf(mon, "acl: policy set to 'allow'\n"); | |
15dfcd45 | 2173 | } else if (strcmp(policy, "deny") == 0) { |
28a76be8 AL |
2174 | acl->defaultDeny = 1; |
2175 | monitor_printf(mon, "acl: policy set to 'deny'\n"); | |
2176 | } else { | |
15dfcd45 JK |
2177 | monitor_printf(mon, "acl: unknown policy '%s', " |
2178 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 | 2179 | } |
15dfcd45 JK |
2180 | } |
2181 | } | |
28a76be8 | 2182 | |
1bd1442e | 2183 | static void do_acl_add(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2184 | { |
1bd1442e LC |
2185 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2186 | const char *match = qdict_get_str(qdict, "match"); | |
2187 | const char *policy = qdict_get_str(qdict, "policy"); | |
2188 | int has_index = qdict_haskey(qdict, "index"); | |
2189 | int index = qdict_get_try_int(qdict, "index", -1); | |
15dfcd45 JK |
2190 | qemu_acl *acl = find_acl(mon, aclname); |
2191 | int deny, ret; | |
2192 | ||
2193 | if (acl) { | |
2194 | if (strcmp(policy, "allow") == 0) { | |
2195 | deny = 0; | |
2196 | } else if (strcmp(policy, "deny") == 0) { | |
2197 | deny = 1; | |
2198 | } else { | |
2199 | monitor_printf(mon, "acl: unknown policy '%s', " | |
2200 | "expected 'deny' or 'allow'\n", policy); | |
28a76be8 AL |
2201 | return; |
2202 | } | |
28a76be8 AL |
2203 | if (has_index) |
2204 | ret = qemu_acl_insert(acl, deny, match, index); | |
2205 | else | |
2206 | ret = qemu_acl_append(acl, deny, match); | |
2207 | if (ret < 0) | |
2208 | monitor_printf(mon, "acl: unable to add acl entry\n"); | |
2209 | else | |
2210 | monitor_printf(mon, "acl: added rule at position %d\n", ret); | |
15dfcd45 JK |
2211 | } |
2212 | } | |
28a76be8 | 2213 | |
f18c16de | 2214 | static void do_acl_remove(Monitor *mon, const QDict *qdict) |
15dfcd45 | 2215 | { |
f18c16de LC |
2216 | const char *aclname = qdict_get_str(qdict, "aclname"); |
2217 | const char *match = qdict_get_str(qdict, "match"); | |
15dfcd45 JK |
2218 | qemu_acl *acl = find_acl(mon, aclname); |
2219 | int ret; | |
28a76be8 | 2220 | |
15dfcd45 | 2221 | if (acl) { |
28a76be8 AL |
2222 | ret = qemu_acl_remove(acl, match); |
2223 | if (ret < 0) | |
2224 | monitor_printf(mon, "acl: no matching acl entry\n"); | |
2225 | else | |
2226 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); | |
76655d6d AL |
2227 | } |
2228 | } | |
2229 | ||
79c4f6b0 | 2230 | #if defined(TARGET_I386) |
37b7ad48 | 2231 | static void do_inject_mce(Monitor *mon, const QDict *qdict) |
79c4f6b0 HY |
2232 | { |
2233 | CPUState *cenv; | |
37b7ad48 LC |
2234 | int cpu_index = qdict_get_int(qdict, "cpu_index"); |
2235 | int bank = qdict_get_int(qdict, "bank"); | |
2236 | uint64_t status = qdict_get_int(qdict, "status"); | |
2237 | uint64_t mcg_status = qdict_get_int(qdict, "mcg_status"); | |
2238 | uint64_t addr = qdict_get_int(qdict, "addr"); | |
2239 | uint64_t misc = qdict_get_int(qdict, "misc"); | |
79c4f6b0 HY |
2240 | |
2241 | for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) | |
2242 | if (cenv->cpu_index == cpu_index && cenv->mcg_cap) { | |
2243 | cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc); | |
2244 | break; | |
2245 | } | |
2246 | } | |
2247 | #endif | |
2248 | ||
f0d6000a | 2249 | static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data) |
f07918fd | 2250 | { |
d54908a5 | 2251 | const char *fdname = qdict_get_str(qdict, "fdname"); |
c227f099 | 2252 | mon_fd_t *monfd; |
f07918fd MM |
2253 | int fd; |
2254 | ||
2255 | fd = qemu_chr_get_msgfd(mon->chr); | |
2256 | if (fd == -1) { | |
13a2ccc4 | 2257 | qemu_error_new(QERR_FD_NOT_SUPPLIED); |
f07918fd MM |
2258 | return; |
2259 | } | |
2260 | ||
2261 | if (qemu_isdigit(fdname[0])) { | |
13a2ccc4 | 2262 | qemu_error_new(QERR_INVALID_PARAMETER, "fdname"); |
f07918fd MM |
2263 | return; |
2264 | } | |
2265 | ||
2266 | fd = dup(fd); | |
2267 | if (fd == -1) { | |
13a2ccc4 MA |
2268 | if (errno == EMFILE) |
2269 | qemu_error_new(QERR_TOO_MANY_FILES); | |
2270 | else | |
2271 | qemu_error_new(QERR_UNDEFINED_ERROR); | |
f07918fd MM |
2272 | return; |
2273 | } | |
2274 | ||
72cf2d4f | 2275 | QLIST_FOREACH(monfd, &mon->fds, next) { |
f07918fd MM |
2276 | if (strcmp(monfd->name, fdname) != 0) { |
2277 | continue; | |
2278 | } | |
2279 | ||
2280 | close(monfd->fd); | |
2281 | monfd->fd = fd; | |
2282 | return; | |
2283 | } | |
2284 | ||
c227f099 | 2285 | monfd = qemu_mallocz(sizeof(mon_fd_t)); |
f07918fd MM |
2286 | monfd->name = qemu_strdup(fdname); |
2287 | monfd->fd = fd; | |
2288 | ||
72cf2d4f | 2289 | QLIST_INSERT_HEAD(&mon->fds, monfd, next); |
f07918fd MM |
2290 | } |
2291 | ||
18f3a515 | 2292 | static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data) |
f07918fd | 2293 | { |
d54908a5 | 2294 | const char *fdname = qdict_get_str(qdict, "fdname"); |
c227f099 | 2295 | mon_fd_t *monfd; |
f07918fd | 2296 | |
72cf2d4f | 2297 | QLIST_FOREACH(monfd, &mon->fds, next) { |
f07918fd MM |
2298 | if (strcmp(monfd->name, fdname) != 0) { |
2299 | continue; | |
2300 | } | |
2301 | ||
72cf2d4f | 2302 | QLIST_REMOVE(monfd, next); |
f07918fd MM |
2303 | close(monfd->fd); |
2304 | qemu_free(monfd->name); | |
2305 | qemu_free(monfd); | |
2306 | return; | |
2307 | } | |
2308 | ||
e5fc266b | 2309 | qemu_error_new(QERR_FD_NOT_FOUND, fdname); |
f07918fd MM |
2310 | } |
2311 | ||
d54908a5 | 2312 | static void do_loadvm(Monitor *mon, const QDict *qdict) |
c8d41b2c JQ |
2313 | { |
2314 | int saved_vm_running = vm_running; | |
d54908a5 | 2315 | const char *name = qdict_get_str(qdict, "name"); |
c8d41b2c JQ |
2316 | |
2317 | vm_stop(0); | |
2318 | ||
05f2401e | 2319 | if (load_vmstate(mon, name) >= 0 && saved_vm_running) |
c8d41b2c JQ |
2320 | vm_start(); |
2321 | } | |
2322 | ||
7768e04c MM |
2323 | int monitor_get_fd(Monitor *mon, const char *fdname) |
2324 | { | |
c227f099 | 2325 | mon_fd_t *monfd; |
7768e04c | 2326 | |
72cf2d4f | 2327 | QLIST_FOREACH(monfd, &mon->fds, next) { |
7768e04c MM |
2328 | int fd; |
2329 | ||
2330 | if (strcmp(monfd->name, fdname) != 0) { | |
2331 | continue; | |
2332 | } | |
2333 | ||
2334 | fd = monfd->fd; | |
2335 | ||
2336 | /* caller takes ownership of fd */ | |
72cf2d4f | 2337 | QLIST_REMOVE(monfd, next); |
7768e04c MM |
2338 | qemu_free(monfd->name); |
2339 | qemu_free(monfd); | |
2340 | ||
2341 | return fd; | |
2342 | } | |
2343 | ||
2344 | return -1; | |
2345 | } | |
2346 | ||
c227f099 | 2347 | static const mon_cmd_t mon_cmds[] = { |
2313086a | 2348 | #include "qemu-monitor.h" |
5fafdf24 | 2349 | { NULL, NULL, }, |
9dc39cba FB |
2350 | }; |
2351 | ||
2313086a | 2352 | /* Please update qemu-monitor.hx when adding or changing commands */ |
c227f099 | 2353 | static const mon_cmd_t info_cmds[] = { |
d7f9b689 LC |
2354 | { |
2355 | .name = "version", | |
2356 | .args_type = "", | |
d7f9b689 LC |
2357 | .params = "", |
2358 | .help = "show the version of QEMU", | |
6e785bee | 2359 | .user_print = do_info_version_print, |
ab2d3187 | 2360 | .mhandler.info_new = do_info_version, |
d7f9b689 | 2361 | }, |
e3bba9d0 LC |
2362 | { |
2363 | .name = "commands", | |
2364 | .args_type = "", | |
2365 | .params = "", | |
2366 | .help = "list QMP available commands", | |
2367 | .user_print = monitor_user_noop, | |
2368 | .mhandler.info_new = do_info_commands, | |
2369 | }, | |
d7f9b689 LC |
2370 | { |
2371 | .name = "network", | |
2372 | .args_type = "", | |
d7f9b689 LC |
2373 | .params = "", |
2374 | .help = "show the network state", | |
910df89d | 2375 | .mhandler.info = do_info_network, |
d7f9b689 LC |
2376 | }, |
2377 | { | |
2378 | .name = "chardev", | |
2379 | .args_type = "", | |
d7f9b689 LC |
2380 | .params = "", |
2381 | .help = "show the character devices", | |
bdae662c LC |
2382 | .user_print = qemu_chr_info_print, |
2383 | .mhandler.info_new = qemu_chr_info, | |
d7f9b689 LC |
2384 | }, |
2385 | { | |
2386 | .name = "block", | |
2387 | .args_type = "", | |
d7f9b689 LC |
2388 | .params = "", |
2389 | .help = "show the block devices", | |
4305793b LC |
2390 | .user_print = bdrv_info_print, |
2391 | .mhandler.info_new = bdrv_info, | |
d7f9b689 LC |
2392 | }, |
2393 | { | |
2394 | .name = "blockstats", | |
2395 | .args_type = "", | |
d7f9b689 LC |
2396 | .params = "", |
2397 | .help = "show block device statistics", | |
0108d4e3 LC |
2398 | .user_print = bdrv_stats_print, |
2399 | .mhandler.info_new = bdrv_info_stats, | |
d7f9b689 LC |
2400 | }, |
2401 | { | |
2402 | .name = "registers", | |
2403 | .args_type = "", | |
d7f9b689 LC |
2404 | .params = "", |
2405 | .help = "show the cpu registers", | |
910df89d | 2406 | .mhandler.info = do_info_registers, |
d7f9b689 LC |
2407 | }, |
2408 | { | |
2409 | .name = "cpus", | |
2410 | .args_type = "", | |
d7f9b689 LC |
2411 | .params = "", |
2412 | .help = "show infos for each CPU", | |
8f3cec0b LC |
2413 | .user_print = monitor_print_cpus, |
2414 | .mhandler.info_new = do_info_cpus, | |
d7f9b689 LC |
2415 | }, |
2416 | { | |
2417 | .name = "history", | |
2418 | .args_type = "", | |
d7f9b689 LC |
2419 | .params = "", |
2420 | .help = "show the command line history", | |
910df89d | 2421 | .mhandler.info = do_info_history, |
d7f9b689 LC |
2422 | }, |
2423 | { | |
2424 | .name = "irq", | |
2425 | .args_type = "", | |
d7f9b689 LC |
2426 | .params = "", |
2427 | .help = "show the interrupts statistics (if available)", | |
910df89d | 2428 | .mhandler.info = irq_info, |
d7f9b689 LC |
2429 | }, |
2430 | { | |
2431 | .name = "pic", | |
2432 | .args_type = "", | |
d7f9b689 LC |
2433 | .params = "", |
2434 | .help = "show i8259 (PIC) state", | |
910df89d | 2435 | .mhandler.info = pic_info, |
d7f9b689 LC |
2436 | }, |
2437 | { | |
2438 | .name = "pci", | |
2439 | .args_type = "", | |
d7f9b689 LC |
2440 | .params = "", |
2441 | .help = "show PCI info", | |
910df89d | 2442 | .mhandler.info = pci_info, |
d7f9b689 | 2443 | }, |
7c664e2f | 2444 | #if defined(TARGET_I386) || defined(TARGET_SH4) |
d7f9b689 LC |
2445 | { |
2446 | .name = "tlb", | |
2447 | .args_type = "", | |
d7f9b689 LC |
2448 | .params = "", |
2449 | .help = "show virtual to physical memory mappings", | |
910df89d | 2450 | .mhandler.info = tlb_info, |
d7f9b689 | 2451 | }, |
7c664e2f AJ |
2452 | #endif |
2453 | #if defined(TARGET_I386) | |
d7f9b689 LC |
2454 | { |
2455 | .name = "mem", | |
2456 | .args_type = "", | |
d7f9b689 LC |
2457 | .params = "", |
2458 | .help = "show the active virtual memory mappings", | |
910df89d | 2459 | .mhandler.info = mem_info, |
d7f9b689 LC |
2460 | }, |
2461 | { | |
2462 | .name = "hpet", | |
2463 | .args_type = "", | |
d7f9b689 LC |
2464 | .params = "", |
2465 | .help = "show state of HPET", | |
5f9fe0f8 LC |
2466 | .user_print = do_info_hpet_print, |
2467 | .mhandler.info_new = do_info_hpet, | |
d7f9b689 | 2468 | }, |
b86bda5b | 2469 | #endif |
d7f9b689 LC |
2470 | { |
2471 | .name = "jit", | |
2472 | .args_type = "", | |
d7f9b689 LC |
2473 | .params = "", |
2474 | .help = "show dynamic compiler info", | |
910df89d | 2475 | .mhandler.info = do_info_jit, |
d7f9b689 LC |
2476 | }, |
2477 | { | |
2478 | .name = "kvm", | |
2479 | .args_type = "", | |
d7f9b689 LC |
2480 | .params = "", |
2481 | .help = "show KVM information", | |
94f539bd LC |
2482 | .user_print = do_info_kvm_print, |
2483 | .mhandler.info_new = do_info_kvm, | |
d7f9b689 LC |
2484 | }, |
2485 | { | |
2486 | .name = "numa", | |
2487 | .args_type = "", | |
d7f9b689 LC |
2488 | .params = "", |
2489 | .help = "show NUMA information", | |
910df89d | 2490 | .mhandler.info = do_info_numa, |
d7f9b689 LC |
2491 | }, |
2492 | { | |
2493 | .name = "usb", | |
2494 | .args_type = "", | |
d7f9b689 LC |
2495 | .params = "", |
2496 | .help = "show guest USB devices", | |
910df89d | 2497 | .mhandler.info = usb_info, |
d7f9b689 LC |
2498 | }, |
2499 | { | |
2500 | .name = "usbhost", | |
2501 | .args_type = "", | |
d7f9b689 LC |
2502 | .params = "", |
2503 | .help = "show host USB devices", | |
910df89d | 2504 | .mhandler.info = usb_host_info, |
d7f9b689 LC |
2505 | }, |
2506 | { | |
2507 | .name = "profile", | |
2508 | .args_type = "", | |
d7f9b689 LC |
2509 | .params = "", |
2510 | .help = "show profiling information", | |
910df89d | 2511 | .mhandler.info = do_info_profile, |
d7f9b689 LC |
2512 | }, |
2513 | { | |
2514 | .name = "capture", | |
2515 | .args_type = "", | |
d7f9b689 LC |
2516 | .params = "", |
2517 | .help = "show capture information", | |
910df89d | 2518 | .mhandler.info = do_info_capture, |
d7f9b689 LC |
2519 | }, |
2520 | { | |
2521 | .name = "snapshots", | |
2522 | .args_type = "", | |
d7f9b689 LC |
2523 | .params = "", |
2524 | .help = "show the currently saved VM snapshots", | |
910df89d | 2525 | .mhandler.info = do_info_snapshots, |
d7f9b689 LC |
2526 | }, |
2527 | { | |
2528 | .name = "status", | |
2529 | .args_type = "", | |
d7f9b689 LC |
2530 | .params = "", |
2531 | .help = "show the current VM status (running|paused)", | |
e637fd23 LC |
2532 | .user_print = do_info_status_print, |
2533 | .mhandler.info_new = do_info_status, | |
d7f9b689 LC |
2534 | }, |
2535 | { | |
2536 | .name = "pcmcia", | |
2537 | .args_type = "", | |
d7f9b689 LC |
2538 | .params = "", |
2539 | .help = "show guest PCMCIA status", | |
910df89d | 2540 | .mhandler.info = pcmcia_info, |
d7f9b689 LC |
2541 | }, |
2542 | { | |
2543 | .name = "mice", | |
2544 | .args_type = "", | |
d7f9b689 LC |
2545 | .params = "", |
2546 | .help = "show which guest mouse is receiving events", | |
3be42b28 LC |
2547 | .user_print = do_info_mice_print, |
2548 | .mhandler.info_new = do_info_mice, | |
d7f9b689 LC |
2549 | }, |
2550 | { | |
2551 | .name = "vnc", | |
2552 | .args_type = "", | |
d7f9b689 LC |
2553 | .params = "", |
2554 | .help = "show the vnc server status", | |
5118f7b4 LC |
2555 | .user_print = do_info_vnc_print, |
2556 | .mhandler.info_new = do_info_vnc, | |
d7f9b689 LC |
2557 | }, |
2558 | { | |
2559 | .name = "name", | |
2560 | .args_type = "", | |
d7f9b689 LC |
2561 | .params = "", |
2562 | .help = "show the current VM name", | |
7589acc9 LC |
2563 | .user_print = do_info_name_print, |
2564 | .mhandler.info_new = do_info_name, | |
d7f9b689 LC |
2565 | }, |
2566 | { | |
2567 | .name = "uuid", | |
2568 | .args_type = "", | |
d7f9b689 LC |
2569 | .params = "", |
2570 | .help = "show the current VM UUID", | |
ee70ef87 LC |
2571 | .user_print = do_info_uuid_print, |
2572 | .mhandler.info_new = do_info_uuid, | |
d7f9b689 | 2573 | }, |
76a66253 | 2574 | #if defined(TARGET_PPC) |
d7f9b689 LC |
2575 | { |
2576 | .name = "cpustats", | |
2577 | .args_type = "", | |
d7f9b689 LC |
2578 | .params = "", |
2579 | .help = "show CPU statistics", | |
910df89d | 2580 | .mhandler.info = do_info_cpu_stats, |
d7f9b689 | 2581 | }, |
76a66253 | 2582 | #endif |
31a60e22 | 2583 | #if defined(CONFIG_SLIRP) |
d7f9b689 LC |
2584 | { |
2585 | .name = "usernet", | |
2586 | .args_type = "", | |
d7f9b689 LC |
2587 | .params = "", |
2588 | .help = "show user network stack connection states", | |
910df89d | 2589 | .mhandler.info = do_info_usernet, |
d7f9b689 | 2590 | }, |
31a60e22 | 2591 | #endif |
d7f9b689 LC |
2592 | { |
2593 | .name = "migrate", | |
2594 | .args_type = "", | |
d7f9b689 LC |
2595 | .params = "", |
2596 | .help = "show migration status", | |
d2d51eef LC |
2597 | .user_print = do_info_migrate_print, |
2598 | .mhandler.info_new = do_info_migrate, | |
d7f9b689 LC |
2599 | }, |
2600 | { | |
2601 | .name = "balloon", | |
2602 | .args_type = "", | |
d7f9b689 LC |
2603 | .params = "", |
2604 | .help = "show balloon information", | |
cc1d9c70 LC |
2605 | .user_print = monitor_print_balloon, |
2606 | .mhandler.info_new = do_info_balloon, | |
d7f9b689 LC |
2607 | }, |
2608 | { | |
2609 | .name = "qtree", | |
2610 | .args_type = "", | |
d7f9b689 LC |
2611 | .params = "", |
2612 | .help = "show device tree", | |
910df89d | 2613 | .mhandler.info = do_info_qtree, |
d7f9b689 LC |
2614 | }, |
2615 | { | |
2616 | .name = "qdm", | |
2617 | .args_type = "", | |
d7f9b689 LC |
2618 | .params = "", |
2619 | .help = "show qdev device model list", | |
910df89d | 2620 | .mhandler.info = do_info_qdm, |
d7f9b689 LC |
2621 | }, |
2622 | { | |
2623 | .name = "roms", | |
2624 | .args_type = "", | |
d7f9b689 LC |
2625 | .params = "", |
2626 | .help = "show roms", | |
910df89d | 2627 | .mhandler.info = do_info_roms, |
d7f9b689 LC |
2628 | }, |
2629 | { | |
2630 | .name = NULL, | |
2631 | }, | |
9dc39cba FB |
2632 | }; |
2633 | ||
9307c4c1 FB |
2634 | /*******************************************************************/ |
2635 | ||
2636 | static const char *pch; | |
2637 | static jmp_buf expr_env; | |
2638 | ||
92a31b1f FB |
2639 | #define MD_TLONG 0 |
2640 | #define MD_I32 1 | |
2641 | ||
9307c4c1 FB |
2642 | typedef struct MonitorDef { |
2643 | const char *name; | |
2644 | int offset; | |
8662d656 | 2645 | target_long (*get_value)(const struct MonitorDef *md, int val); |
92a31b1f | 2646 | int type; |
9307c4c1 FB |
2647 | } MonitorDef; |
2648 | ||
57206fd4 | 2649 | #if defined(TARGET_I386) |
8662d656 | 2650 | static target_long monitor_get_pc (const struct MonitorDef *md, int val) |
57206fd4 | 2651 | { |
6a00d601 FB |
2652 | CPUState *env = mon_get_cpu(); |
2653 | if (!env) | |
2654 | return 0; | |
2655 | return env->eip + env->segs[R_CS].base; | |
57206fd4 FB |
2656 | } |
2657 | #endif | |
2658 | ||
a541f297 | 2659 | #if defined(TARGET_PPC) |
8662d656 | 2660 | static target_long monitor_get_ccr (const struct MonitorDef *md, int val) |
a541f297 | 2661 | { |
6a00d601 | 2662 | CPUState *env = mon_get_cpu(); |
a541f297 FB |
2663 | unsigned int u; |
2664 | int i; | |
2665 | ||
6a00d601 FB |
2666 | if (!env) |
2667 | return 0; | |
2668 | ||
a541f297 FB |
2669 | u = 0; |
2670 | for (i = 0; i < 8; i++) | |
28a76be8 | 2671 | u |= env->crf[i] << (32 - (4 * i)); |
a541f297 FB |
2672 | |
2673 | return u; | |
2674 | } | |
2675 | ||
8662d656 | 2676 | static target_long monitor_get_msr (const struct MonitorDef *md, int val) |
a541f297 | 2677 | { |
6a00d601 FB |
2678 | CPUState *env = mon_get_cpu(); |
2679 | if (!env) | |
2680 | return 0; | |
0411a972 | 2681 | return env->msr; |
a541f297 FB |
2682 | } |
2683 | ||
8662d656 | 2684 | static target_long monitor_get_xer (const struct MonitorDef *md, int val) |
a541f297 | 2685 | { |
6a00d601 FB |
2686 | CPUState *env = mon_get_cpu(); |
2687 | if (!env) | |
2688 | return 0; | |
3d7b417e | 2689 | return env->xer; |
a541f297 | 2690 | } |
9fddaa0c | 2691 | |
8662d656 | 2692 | static target_long monitor_get_decr (const struct MonitorDef *md, int val) |
9fddaa0c | 2693 | { |
6a00d601 FB |
2694 | CPUState *env = mon_get_cpu(); |
2695 | if (!env) | |
2696 | return 0; | |
2697 | return cpu_ppc_load_decr(env); | |
9fddaa0c FB |
2698 | } |
2699 | ||
8662d656 | 2700 | static target_long monitor_get_tbu (const struct MonitorDef *md, int val) |
9fddaa0c | 2701 | { |
6a00d601 FB |
2702 | CPUState *env = mon_get_cpu(); |
2703 | if (!env) | |
2704 | return 0; | |
2705 | return cpu_ppc_load_tbu(env); | |
9fddaa0c FB |
2706 | } |
2707 | ||
8662d656 | 2708 | static target_long monitor_get_tbl (const struct MonitorDef *md, int val) |
9fddaa0c | 2709 | { |
6a00d601 FB |
2710 | CPUState *env = mon_get_cpu(); |
2711 | if (!env) | |
2712 | return 0; | |
2713 | return cpu_ppc_load_tbl(env); | |
9fddaa0c | 2714 | } |
a541f297 FB |
2715 | #endif |
2716 | ||
e95c8d51 | 2717 | #if defined(TARGET_SPARC) |
7b936c0c | 2718 | #ifndef TARGET_SPARC64 |
8662d656 | 2719 | static target_long monitor_get_psr (const struct MonitorDef *md, int val) |
e95c8d51 | 2720 | { |
6a00d601 FB |
2721 | CPUState *env = mon_get_cpu(); |
2722 | if (!env) | |
2723 | return 0; | |
2724 | return GET_PSR(env); | |
e95c8d51 | 2725 | } |
7b936c0c | 2726 | #endif |
e95c8d51 | 2727 | |
8662d656 | 2728 | static target_long monitor_get_reg(const struct MonitorDef *md, int val) |
e95c8d51 | 2729 | { |
6a00d601 FB |
2730 | CPUState *env = mon_get_cpu(); |
2731 | if (!env) | |
2732 | return 0; | |
2733 | return env->regwptr[val]; | |
e95c8d51 FB |
2734 | } |
2735 | #endif | |
2736 | ||
8662d656 | 2737 | static const MonitorDef monitor_defs[] = { |
9307c4c1 | 2738 | #ifdef TARGET_I386 |
57206fd4 FB |
2739 | |
2740 | #define SEG(name, seg) \ | |
92a31b1f | 2741 | { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\ |
57206fd4 | 2742 | { name ".base", offsetof(CPUState, segs[seg].base) },\ |
92a31b1f | 2743 | { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 }, |
57206fd4 | 2744 | |
9307c4c1 FB |
2745 | { "eax", offsetof(CPUState, regs[0]) }, |
2746 | { "ecx", offsetof(CPUState, regs[1]) }, | |
2747 | { "edx", offsetof(CPUState, regs[2]) }, | |
2748 | { "ebx", offsetof(CPUState, regs[3]) }, | |
2749 | { "esp|sp", offsetof(CPUState, regs[4]) }, | |
2750 | { "ebp|fp", offsetof(CPUState, regs[5]) }, | |
2751 | { "esi", offsetof(CPUState, regs[6]) }, | |
01038d2a | 2752 | { "edi", offsetof(CPUState, regs[7]) }, |
92a31b1f FB |
2753 | #ifdef TARGET_X86_64 |
2754 | { "r8", offsetof(CPUState, regs[8]) }, | |
2755 | { "r9", offsetof(CPUState, regs[9]) }, | |
2756 | { "r10", offsetof(CPUState, regs[10]) }, | |
2757 | { "r11", offsetof(CPUState, regs[11]) }, | |
2758 | { "r12", offsetof(CPUState, regs[12]) }, | |
2759 | { "r13", offsetof(CPUState, regs[13]) }, | |
2760 | { "r14", offsetof(CPUState, regs[14]) }, | |
2761 | { "r15", offsetof(CPUState, regs[15]) }, | |
2762 | #endif | |
9307c4c1 | 2763 | { "eflags", offsetof(CPUState, eflags) }, |
57206fd4 FB |
2764 | { "eip", offsetof(CPUState, eip) }, |
2765 | SEG("cs", R_CS) | |
2766 | SEG("ds", R_DS) | |
2767 | SEG("es", R_ES) | |
01038d2a | 2768 | SEG("ss", R_SS) |
57206fd4 FB |
2769 | SEG("fs", R_FS) |
2770 | SEG("gs", R_GS) | |
2771 | { "pc", 0, monitor_get_pc, }, | |
a541f297 | 2772 | #elif defined(TARGET_PPC) |
ff937dba | 2773 | /* General purpose registers */ |
a541f297 FB |
2774 | { "r0", offsetof(CPUState, gpr[0]) }, |
2775 | { "r1", offsetof(CPUState, gpr[1]) }, | |
2776 | { "r2", offsetof(CPUState, gpr[2]) }, | |
2777 | { "r3", offsetof(CPUState, gpr[3]) }, | |
2778 | { "r4", offsetof(CPUState, gpr[4]) }, | |
2779 | { "r5", offsetof(CPUState, gpr[5]) }, | |
2780 | { "r6", offsetof(CPUState, gpr[6]) }, | |
2781 | { "r7", offsetof(CPUState, gpr[7]) }, | |
2782 | { "r8", offsetof(CPUState, gpr[8]) }, | |
2783 | { "r9", offsetof(CPUState, gpr[9]) }, | |
2784 | { "r10", offsetof(CPUState, gpr[10]) }, | |
2785 | { "r11", offsetof(CPUState, gpr[11]) }, | |
2786 | { "r12", offsetof(CPUState, gpr[12]) }, | |
2787 | { "r13", offsetof(CPUState, gpr[13]) }, | |
2788 | { "r14", offsetof(CPUState, gpr[14]) }, | |
2789 | { "r15", offsetof(CPUState, gpr[15]) }, | |
2790 | { "r16", offsetof(CPUState, gpr[16]) }, | |
2791 | { "r17", offsetof(CPUState, gpr[17]) }, | |
2792 | { "r18", offsetof(CPUState, gpr[18]) }, | |
2793 | { "r19", offsetof(CPUState, gpr[19]) }, | |
2794 | { "r20", offsetof(CPUState, gpr[20]) }, | |
2795 | { "r21", offsetof(CPUState, gpr[21]) }, | |
2796 | { "r22", offsetof(CPUState, gpr[22]) }, | |
2797 | { "r23", offsetof(CPUState, gpr[23]) }, | |
2798 | { "r24", offsetof(CPUState, gpr[24]) }, | |
2799 | { "r25", offsetof(CPUState, gpr[25]) }, | |
2800 | { "r26", offsetof(CPUState, gpr[26]) }, | |
2801 | { "r27", offsetof(CPUState, gpr[27]) }, | |
2802 | { "r28", offsetof(CPUState, gpr[28]) }, | |
2803 | { "r29", offsetof(CPUState, gpr[29]) }, | |
2804 | { "r30", offsetof(CPUState, gpr[30]) }, | |
2805 | { "r31", offsetof(CPUState, gpr[31]) }, | |
ff937dba JM |
2806 | /* Floating point registers */ |
2807 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2808 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2809 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2810 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2811 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2812 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2813 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2814 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2815 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2816 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2817 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2818 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2819 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2820 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2821 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2822 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2823 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2824 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2825 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2826 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2827 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2828 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2829 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2830 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2831 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2832 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2833 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2834 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2835 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2836 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2837 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2838 | { "f31", offsetof(CPUState, fpr[31]) }, | |
2839 | { "fpscr", offsetof(CPUState, fpscr) }, | |
2840 | /* Next instruction pointer */ | |
57206fd4 | 2841 | { "nip|pc", offsetof(CPUState, nip) }, |
a541f297 FB |
2842 | { "lr", offsetof(CPUState, lr) }, |
2843 | { "ctr", offsetof(CPUState, ctr) }, | |
9fddaa0c | 2844 | { "decr", 0, &monitor_get_decr, }, |
a541f297 | 2845 | { "ccr", 0, &monitor_get_ccr, }, |
ff937dba | 2846 | /* Machine state register */ |
a541f297 FB |
2847 | { "msr", 0, &monitor_get_msr, }, |
2848 | { "xer", 0, &monitor_get_xer, }, | |
9fddaa0c FB |
2849 | { "tbu", 0, &monitor_get_tbu, }, |
2850 | { "tbl", 0, &monitor_get_tbl, }, | |
ff937dba JM |
2851 | #if defined(TARGET_PPC64) |
2852 | /* Address space register */ | |
2853 | { "asr", offsetof(CPUState, asr) }, | |
2854 | #endif | |
2855 | /* Segment registers */ | |
a541f297 FB |
2856 | { "sdr1", offsetof(CPUState, sdr1) }, |
2857 | { "sr0", offsetof(CPUState, sr[0]) }, | |
2858 | { "sr1", offsetof(CPUState, sr[1]) }, | |
2859 | { "sr2", offsetof(CPUState, sr[2]) }, | |
2860 | { "sr3", offsetof(CPUState, sr[3]) }, | |
2861 | { "sr4", offsetof(CPUState, sr[4]) }, | |
2862 | { "sr5", offsetof(CPUState, sr[5]) }, | |
2863 | { "sr6", offsetof(CPUState, sr[6]) }, | |
2864 | { "sr7", offsetof(CPUState, sr[7]) }, | |
2865 | { "sr8", offsetof(CPUState, sr[8]) }, | |
2866 | { "sr9", offsetof(CPUState, sr[9]) }, | |
2867 | { "sr10", offsetof(CPUState, sr[10]) }, | |
2868 | { "sr11", offsetof(CPUState, sr[11]) }, | |
2869 | { "sr12", offsetof(CPUState, sr[12]) }, | |
2870 | { "sr13", offsetof(CPUState, sr[13]) }, | |
2871 | { "sr14", offsetof(CPUState, sr[14]) }, | |
2872 | { "sr15", offsetof(CPUState, sr[15]) }, | |
2873 | /* Too lazy to put BATs and SPRs ... */ | |
e95c8d51 FB |
2874 | #elif defined(TARGET_SPARC) |
2875 | { "g0", offsetof(CPUState, gregs[0]) }, | |
2876 | { "g1", offsetof(CPUState, gregs[1]) }, | |
2877 | { "g2", offsetof(CPUState, gregs[2]) }, | |
2878 | { "g3", offsetof(CPUState, gregs[3]) }, | |
2879 | { "g4", offsetof(CPUState, gregs[4]) }, | |
2880 | { "g5", offsetof(CPUState, gregs[5]) }, | |
2881 | { "g6", offsetof(CPUState, gregs[6]) }, | |
2882 | { "g7", offsetof(CPUState, gregs[7]) }, | |
2883 | { "o0", 0, monitor_get_reg }, | |
2884 | { "o1", 1, monitor_get_reg }, | |
2885 | { "o2", 2, monitor_get_reg }, | |
2886 | { "o3", 3, monitor_get_reg }, | |
2887 | { "o4", 4, monitor_get_reg }, | |
2888 | { "o5", 5, monitor_get_reg }, | |
2889 | { "o6", 6, monitor_get_reg }, | |
2890 | { "o7", 7, monitor_get_reg }, | |
2891 | { "l0", 8, monitor_get_reg }, | |
2892 | { "l1", 9, monitor_get_reg }, | |
2893 | { "l2", 10, monitor_get_reg }, | |
2894 | { "l3", 11, monitor_get_reg }, | |
2895 | { "l4", 12, monitor_get_reg }, | |
2896 | { "l5", 13, monitor_get_reg }, | |
2897 | { "l6", 14, monitor_get_reg }, | |
2898 | { "l7", 15, monitor_get_reg }, | |
2899 | { "i0", 16, monitor_get_reg }, | |
2900 | { "i1", 17, monitor_get_reg }, | |
2901 | { "i2", 18, monitor_get_reg }, | |
2902 | { "i3", 19, monitor_get_reg }, | |
2903 | { "i4", 20, monitor_get_reg }, | |
2904 | { "i5", 21, monitor_get_reg }, | |
2905 | { "i6", 22, monitor_get_reg }, | |
2906 | { "i7", 23, monitor_get_reg }, | |
2907 | { "pc", offsetof(CPUState, pc) }, | |
2908 | { "npc", offsetof(CPUState, npc) }, | |
2909 | { "y", offsetof(CPUState, y) }, | |
7b936c0c | 2910 | #ifndef TARGET_SPARC64 |
e95c8d51 FB |
2911 | { "psr", 0, &monitor_get_psr, }, |
2912 | { "wim", offsetof(CPUState, wim) }, | |
7b936c0c | 2913 | #endif |
e95c8d51 FB |
2914 | { "tbr", offsetof(CPUState, tbr) }, |
2915 | { "fsr", offsetof(CPUState, fsr) }, | |
2916 | { "f0", offsetof(CPUState, fpr[0]) }, | |
2917 | { "f1", offsetof(CPUState, fpr[1]) }, | |
2918 | { "f2", offsetof(CPUState, fpr[2]) }, | |
2919 | { "f3", offsetof(CPUState, fpr[3]) }, | |
2920 | { "f4", offsetof(CPUState, fpr[4]) }, | |
2921 | { "f5", offsetof(CPUState, fpr[5]) }, | |
2922 | { "f6", offsetof(CPUState, fpr[6]) }, | |
2923 | { "f7", offsetof(CPUState, fpr[7]) }, | |
2924 | { "f8", offsetof(CPUState, fpr[8]) }, | |
2925 | { "f9", offsetof(CPUState, fpr[9]) }, | |
2926 | { "f10", offsetof(CPUState, fpr[10]) }, | |
2927 | { "f11", offsetof(CPUState, fpr[11]) }, | |
2928 | { "f12", offsetof(CPUState, fpr[12]) }, | |
2929 | { "f13", offsetof(CPUState, fpr[13]) }, | |
2930 | { "f14", offsetof(CPUState, fpr[14]) }, | |
2931 | { "f15", offsetof(CPUState, fpr[15]) }, | |
2932 | { "f16", offsetof(CPUState, fpr[16]) }, | |
2933 | { "f17", offsetof(CPUState, fpr[17]) }, | |
2934 | { "f18", offsetof(CPUState, fpr[18]) }, | |
2935 | { "f19", offsetof(CPUState, fpr[19]) }, | |
2936 | { "f20", offsetof(CPUState, fpr[20]) }, | |
2937 | { "f21", offsetof(CPUState, fpr[21]) }, | |
2938 | { "f22", offsetof(CPUState, fpr[22]) }, | |
2939 | { "f23", offsetof(CPUState, fpr[23]) }, | |
2940 | { "f24", offsetof(CPUState, fpr[24]) }, | |
2941 | { "f25", offsetof(CPUState, fpr[25]) }, | |
2942 | { "f26", offsetof(CPUState, fpr[26]) }, | |
2943 | { "f27", offsetof(CPUState, fpr[27]) }, | |
2944 | { "f28", offsetof(CPUState, fpr[28]) }, | |
2945 | { "f29", offsetof(CPUState, fpr[29]) }, | |
2946 | { "f30", offsetof(CPUState, fpr[30]) }, | |
2947 | { "f31", offsetof(CPUState, fpr[31]) }, | |
7b936c0c FB |
2948 | #ifdef TARGET_SPARC64 |
2949 | { "f32", offsetof(CPUState, fpr[32]) }, | |
2950 | { "f34", offsetof(CPUState, fpr[34]) }, | |
2951 | { "f36", offsetof(CPUState, fpr[36]) }, | |
2952 | { "f38", offsetof(CPUState, fpr[38]) }, | |
2953 | { "f40", offsetof(CPUState, fpr[40]) }, | |
2954 | { "f42", offsetof(CPUState, fpr[42]) }, | |
2955 | { "f44", offsetof(CPUState, fpr[44]) }, | |
2956 | { "f46", offsetof(CPUState, fpr[46]) }, | |
2957 | { "f48", offsetof(CPUState, fpr[48]) }, | |
2958 | { "f50", offsetof(CPUState, fpr[50]) }, | |
2959 | { "f52", offsetof(CPUState, fpr[52]) }, | |
2960 | { "f54", offsetof(CPUState, fpr[54]) }, | |
2961 | { "f56", offsetof(CPUState, fpr[56]) }, | |
2962 | { "f58", offsetof(CPUState, fpr[58]) }, | |
2963 | { "f60", offsetof(CPUState, fpr[60]) }, | |
2964 | { "f62", offsetof(CPUState, fpr[62]) }, | |
2965 | { "asi", offsetof(CPUState, asi) }, | |
2966 | { "pstate", offsetof(CPUState, pstate) }, | |
2967 | { "cansave", offsetof(CPUState, cansave) }, | |
2968 | { "canrestore", offsetof(CPUState, canrestore) }, | |
2969 | { "otherwin", offsetof(CPUState, otherwin) }, | |
2970 | { "wstate", offsetof(CPUState, wstate) }, | |
2971 | { "cleanwin", offsetof(CPUState, cleanwin) }, | |
2972 | { "fprs", offsetof(CPUState, fprs) }, | |
2973 | #endif | |
9307c4c1 FB |
2974 | #endif |
2975 | { NULL }, | |
2976 | }; | |
2977 | ||
376253ec | 2978 | static void expr_error(Monitor *mon, const char *msg) |
9dc39cba | 2979 | { |
376253ec | 2980 | monitor_printf(mon, "%s\n", msg); |
9307c4c1 FB |
2981 | longjmp(expr_env, 1); |
2982 | } | |
2983 | ||
6a00d601 | 2984 | /* return 0 if OK, -1 if not found, -2 if no CPU defined */ |
92a31b1f | 2985 | static int get_monitor_def(target_long *pval, const char *name) |
9307c4c1 | 2986 | { |
8662d656 | 2987 | const MonitorDef *md; |
92a31b1f FB |
2988 | void *ptr; |
2989 | ||
9307c4c1 FB |
2990 | for(md = monitor_defs; md->name != NULL; md++) { |
2991 | if (compare_cmd(name, md->name)) { | |
2992 | if (md->get_value) { | |
e95c8d51 | 2993 | *pval = md->get_value(md, md->offset); |
9307c4c1 | 2994 | } else { |
6a00d601 FB |
2995 | CPUState *env = mon_get_cpu(); |
2996 | if (!env) | |
2997 | return -2; | |
2998 | ptr = (uint8_t *)env + md->offset; | |
92a31b1f FB |
2999 | switch(md->type) { |
3000 | case MD_I32: | |
3001 | *pval = *(int32_t *)ptr; | |
3002 | break; | |
3003 | case MD_TLONG: | |
3004 | *pval = *(target_long *)ptr; | |
3005 | break; | |
3006 | default: | |
3007 | *pval = 0; | |
3008 | break; | |
3009 | } | |
9307c4c1 FB |
3010 | } |
3011 | return 0; | |
3012 | } | |
3013 | } | |
3014 | return -1; | |
3015 | } | |
3016 | ||
3017 | static void next(void) | |
3018 | { | |
660f11be | 3019 | if (*pch != '\0') { |
9307c4c1 | 3020 | pch++; |
cd390083 | 3021 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3022 | pch++; |
3023 | } | |
3024 | } | |
3025 | ||
376253ec | 3026 | static int64_t expr_sum(Monitor *mon); |
9307c4c1 | 3027 | |
376253ec | 3028 | static int64_t expr_unary(Monitor *mon) |
9307c4c1 | 3029 | { |
c2efc95d | 3030 | int64_t n; |
9307c4c1 | 3031 | char *p; |
6a00d601 | 3032 | int ret; |
9307c4c1 FB |
3033 | |
3034 | switch(*pch) { | |
3035 | case '+': | |
3036 | next(); | |
376253ec | 3037 | n = expr_unary(mon); |
9307c4c1 FB |
3038 | break; |
3039 | case '-': | |
3040 | next(); | |
376253ec | 3041 | n = -expr_unary(mon); |
9307c4c1 FB |
3042 | break; |
3043 | case '~': | |
3044 | next(); | |
376253ec | 3045 | n = ~expr_unary(mon); |
9307c4c1 FB |
3046 | break; |
3047 | case '(': | |
3048 | next(); | |
376253ec | 3049 | n = expr_sum(mon); |
9307c4c1 | 3050 | if (*pch != ')') { |
376253ec | 3051 | expr_error(mon, "')' expected"); |
9307c4c1 FB |
3052 | } |
3053 | next(); | |
3054 | break; | |
81d0912d FB |
3055 | case '\'': |
3056 | pch++; | |
3057 | if (*pch == '\0') | |
376253ec | 3058 | expr_error(mon, "character constant expected"); |
81d0912d FB |
3059 | n = *pch; |
3060 | pch++; | |
3061 | if (*pch != '\'') | |
376253ec | 3062 | expr_error(mon, "missing terminating \' character"); |
81d0912d FB |
3063 | next(); |
3064 | break; | |
9307c4c1 FB |
3065 | case '$': |
3066 | { | |
3067 | char buf[128], *q; | |
69b34976 | 3068 | target_long reg=0; |
3b46e624 | 3069 | |
9307c4c1 FB |
3070 | pch++; |
3071 | q = buf; | |
3072 | while ((*pch >= 'a' && *pch <= 'z') || | |
3073 | (*pch >= 'A' && *pch <= 'Z') || | |
3074 | (*pch >= '0' && *pch <= '9') || | |
57206fd4 | 3075 | *pch == '_' || *pch == '.') { |
9307c4c1 FB |
3076 | if ((q - buf) < sizeof(buf) - 1) |
3077 | *q++ = *pch; | |
3078 | pch++; | |
3079 | } | |
cd390083 | 3080 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3081 | pch++; |
3082 | *q = 0; | |
7743e588 | 3083 | ret = get_monitor_def(®, buf); |
6a00d601 | 3084 | if (ret == -1) |
376253ec | 3085 | expr_error(mon, "unknown register"); |
5fafdf24 | 3086 | else if (ret == -2) |
376253ec | 3087 | expr_error(mon, "no cpu defined"); |
7743e588 | 3088 | n = reg; |
9307c4c1 FB |
3089 | } |
3090 | break; | |
3091 | case '\0': | |
376253ec | 3092 | expr_error(mon, "unexpected end of expression"); |
9307c4c1 FB |
3093 | n = 0; |
3094 | break; | |
3095 | default: | |
7743e588 | 3096 | #if TARGET_PHYS_ADDR_BITS > 32 |
4f4fbf77 FB |
3097 | n = strtoull(pch, &p, 0); |
3098 | #else | |
9307c4c1 | 3099 | n = strtoul(pch, &p, 0); |
4f4fbf77 | 3100 | #endif |
9307c4c1 | 3101 | if (pch == p) { |
376253ec | 3102 | expr_error(mon, "invalid char in expression"); |
9307c4c1 FB |
3103 | } |
3104 | pch = p; | |
cd390083 | 3105 | while (qemu_isspace(*pch)) |
9307c4c1 FB |
3106 | pch++; |
3107 | break; | |
3108 | } | |
3109 | return n; | |
3110 | } | |
3111 | ||
3112 | ||
376253ec | 3113 | static int64_t expr_prod(Monitor *mon) |
9307c4c1 | 3114 | { |
c2efc95d | 3115 | int64_t val, val2; |
92a31b1f | 3116 | int op; |
3b46e624 | 3117 | |
376253ec | 3118 | val = expr_unary(mon); |
9307c4c1 FB |
3119 | for(;;) { |
3120 | op = *pch; | |
3121 | if (op != '*' && op != '/' && op != '%') | |
3122 | break; | |
3123 | next(); | |
376253ec | 3124 | val2 = expr_unary(mon); |
9307c4c1 FB |
3125 | switch(op) { |
3126 | default: | |
3127 | case '*': | |
3128 | val *= val2; | |
3129 | break; | |
3130 | case '/': | |
3131 | case '%': | |
5fafdf24 | 3132 | if (val2 == 0) |
376253ec | 3133 | expr_error(mon, "division by zero"); |
9307c4c1 FB |
3134 | if (op == '/') |
3135 | val /= val2; | |
3136 | else | |
3137 | val %= val2; | |
3138 | break; | |
3139 | } | |
3140 | } | |
3141 | return val; | |
3142 | } | |
3143 | ||
376253ec | 3144 | static int64_t expr_logic(Monitor *mon) |
9307c4c1 | 3145 | { |
c2efc95d | 3146 | int64_t val, val2; |
92a31b1f | 3147 | int op; |
9307c4c1 | 3148 | |
376253ec | 3149 | val = expr_prod(mon); |
9307c4c1 FB |
3150 | for(;;) { |
3151 | op = *pch; | |
3152 | if (op != '&' && op != '|' && op != '^') | |
3153 | break; | |
3154 | next(); | |
376253ec | 3155 | val2 = expr_prod(mon); |
9307c4c1 FB |
3156 | switch(op) { |
3157 | default: | |
3158 | case '&': | |
3159 | val &= val2; | |
3160 | break; | |
3161 | case '|': | |
3162 | val |= val2; | |
3163 | break; | |
3164 | case '^': | |
3165 | val ^= val2; | |
3166 | break; | |
3167 | } | |
3168 | } | |
3169 | return val; | |
3170 | } | |
3171 | ||
376253ec | 3172 | static int64_t expr_sum(Monitor *mon) |
9307c4c1 | 3173 | { |
c2efc95d | 3174 | int64_t val, val2; |
92a31b1f | 3175 | int op; |
9307c4c1 | 3176 | |
376253ec | 3177 | val = expr_logic(mon); |
9307c4c1 FB |
3178 | for(;;) { |
3179 | op = *pch; | |
3180 | if (op != '+' && op != '-') | |
3181 | break; | |
3182 | next(); | |
376253ec | 3183 | val2 = expr_logic(mon); |
9307c4c1 FB |
3184 | if (op == '+') |
3185 | val += val2; | |
3186 | else | |
3187 | val -= val2; | |
3188 | } | |
3189 | return val; | |
3190 | } | |
3191 | ||
376253ec | 3192 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
9307c4c1 FB |
3193 | { |
3194 | pch = *pp; | |
3195 | if (setjmp(expr_env)) { | |
3196 | *pp = pch; | |
3197 | return -1; | |
3198 | } | |
cd390083 | 3199 | while (qemu_isspace(*pch)) |
9307c4c1 | 3200 | pch++; |
376253ec | 3201 | *pval = expr_sum(mon); |
9307c4c1 FB |
3202 | *pp = pch; |
3203 | return 0; | |
3204 | } | |
3205 | ||
3206 | static int get_str(char *buf, int buf_size, const char **pp) | |
3207 | { | |
3208 | const char *p; | |
3209 | char *q; | |
3210 | int c; | |
3211 | ||
81d0912d | 3212 | q = buf; |
9307c4c1 | 3213 | p = *pp; |
cd390083 | 3214 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3215 | p++; |
3216 | if (*p == '\0') { | |
3217 | fail: | |
81d0912d | 3218 | *q = '\0'; |
9307c4c1 FB |
3219 | *pp = p; |
3220 | return -1; | |
3221 | } | |
9307c4c1 FB |
3222 | if (*p == '\"') { |
3223 | p++; | |
3224 | while (*p != '\0' && *p != '\"') { | |
3225 | if (*p == '\\') { | |
3226 | p++; | |
3227 | c = *p++; | |
3228 | switch(c) { | |
3229 | case 'n': | |
3230 | c = '\n'; | |
3231 | break; | |
3232 | case 'r': | |
3233 | c = '\r'; | |
3234 | break; | |
3235 | case '\\': | |
3236 | case '\'': | |
3237 | case '\"': | |
3238 | break; | |
3239 | default: | |
3240 | qemu_printf("unsupported escape code: '\\%c'\n", c); | |
3241 | goto fail; | |
3242 | } | |
3243 | if ((q - buf) < buf_size - 1) { | |
3244 | *q++ = c; | |
3245 | } | |
3246 | } else { | |
3247 | if ((q - buf) < buf_size - 1) { | |
3248 | *q++ = *p; | |
3249 | } | |
3250 | p++; | |
3251 | } | |
3252 | } | |
3253 | if (*p != '\"') { | |
5b60212f | 3254 | qemu_printf("unterminated string\n"); |
9307c4c1 FB |
3255 | goto fail; |
3256 | } | |
3257 | p++; | |
3258 | } else { | |
cd390083 | 3259 | while (*p != '\0' && !qemu_isspace(*p)) { |
9307c4c1 FB |
3260 | if ((q - buf) < buf_size - 1) { |
3261 | *q++ = *p; | |
3262 | } | |
3263 | p++; | |
3264 | } | |
9307c4c1 | 3265 | } |
81d0912d | 3266 | *q = '\0'; |
9307c4c1 FB |
3267 | *pp = p; |
3268 | return 0; | |
3269 | } | |
3270 | ||
4590fd80 LC |
3271 | /* |
3272 | * Store the command-name in cmdname, and return a pointer to | |
3273 | * the remaining of the command string. | |
3274 | */ | |
3275 | static const char *get_command_name(const char *cmdline, | |
3276 | char *cmdname, size_t nlen) | |
3277 | { | |
3278 | size_t len; | |
3279 | const char *p, *pstart; | |
3280 | ||
3281 | p = cmdline; | |
3282 | while (qemu_isspace(*p)) | |
3283 | p++; | |
3284 | if (*p == '\0') | |
3285 | return NULL; | |
3286 | pstart = p; | |
3287 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | |
3288 | p++; | |
3289 | len = p - pstart; | |
3290 | if (len > nlen - 1) | |
3291 | len = nlen - 1; | |
3292 | memcpy(cmdname, pstart, len); | |
3293 | cmdname[len] = '\0'; | |
3294 | return p; | |
3295 | } | |
3296 | ||
4d76d2ba LC |
3297 | /** |
3298 | * Read key of 'type' into 'key' and return the current | |
3299 | * 'type' pointer. | |
3300 | */ | |
3301 | static char *key_get_info(const char *type, char **key) | |
3302 | { | |
3303 | size_t len; | |
3304 | char *p, *str; | |
3305 | ||
3306 | if (*type == ',') | |
3307 | type++; | |
3308 | ||
3309 | p = strchr(type, ':'); | |
3310 | if (!p) { | |
3311 | *key = NULL; | |
3312 | return NULL; | |
3313 | } | |
3314 | len = p - type; | |
3315 | ||
3316 | str = qemu_malloc(len + 1); | |
3317 | memcpy(str, type, len); | |
3318 | str[len] = '\0'; | |
3319 | ||
3320 | *key = str; | |
3321 | return ++p; | |
3322 | } | |
3323 | ||
9307c4c1 FB |
3324 | static int default_fmt_format = 'x'; |
3325 | static int default_fmt_size = 4; | |
3326 | ||
3327 | #define MAX_ARGS 16 | |
3328 | ||
fbc3d96c | 3329 | static int is_valid_option(const char *c, const char *typestr) |
3330 | { | |
3331 | char option[3]; | |
3332 | ||
3333 | option[0] = '-'; | |
3334 | option[1] = *c; | |
3335 | option[2] = '\0'; | |
3336 | ||
3337 | typestr = strstr(typestr, option); | |
3338 | return (typestr != NULL); | |
3339 | } | |
3340 | ||
7fd669a1 LC |
3341 | static const mon_cmd_t *monitor_find_command(const char *cmdname) |
3342 | { | |
3343 | const mon_cmd_t *cmd; | |
3344 | ||
3345 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
3346 | if (compare_cmd(cmdname, cmd->name)) { | |
3347 | return cmd; | |
3348 | } | |
3349 | } | |
3350 | ||
3351 | return NULL; | |
3352 | } | |
3353 | ||
c227f099 | 3354 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
55f81d96 | 3355 | const char *cmdline, |
55f81d96 | 3356 | QDict *qdict) |
9307c4c1 | 3357 | { |
4590fd80 | 3358 | const char *p, *typestr; |
53773581 | 3359 | int c; |
c227f099 | 3360 | const mon_cmd_t *cmd; |
9307c4c1 FB |
3361 | char cmdname[256]; |
3362 | char buf[1024]; | |
4d76d2ba | 3363 | char *key; |
9dc39cba FB |
3364 | |
3365 | #ifdef DEBUG | |
376253ec | 3366 | monitor_printf(mon, "command='%s'\n", cmdline); |
9dc39cba | 3367 | #endif |
3b46e624 | 3368 | |
9307c4c1 | 3369 | /* extract the command name */ |
4590fd80 LC |
3370 | p = get_command_name(cmdline, cmdname, sizeof(cmdname)); |
3371 | if (!p) | |
55f81d96 | 3372 | return NULL; |
3b46e624 | 3373 | |
7fd669a1 LC |
3374 | cmd = monitor_find_command(cmdname); |
3375 | if (!cmd) { | |
d91d9bf6 | 3376 | monitor_printf(mon, "unknown command: '%s'\n", cmdname); |
55f81d96 | 3377 | return NULL; |
9307c4c1 | 3378 | } |
9307c4c1 | 3379 | |
9307c4c1 FB |
3380 | /* parse the parameters */ |
3381 | typestr = cmd->args_type; | |
9dc39cba | 3382 | for(;;) { |
4d76d2ba LC |
3383 | typestr = key_get_info(typestr, &key); |
3384 | if (!typestr) | |
9dc39cba | 3385 | break; |
4d76d2ba | 3386 | c = *typestr; |
9307c4c1 FB |
3387 | typestr++; |
3388 | switch(c) { | |
3389 | case 'F': | |
81d0912d | 3390 | case 'B': |
9307c4c1 FB |
3391 | case 's': |
3392 | { | |
3393 | int ret; | |
3b46e624 | 3394 | |
cd390083 | 3395 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3396 | p++; |
3397 | if (*typestr == '?') { | |
3398 | typestr++; | |
3399 | if (*p == '\0') { | |
3400 | /* no optional string: NULL argument */ | |
53773581 | 3401 | break; |
9307c4c1 FB |
3402 | } |
3403 | } | |
3404 | ret = get_str(buf, sizeof(buf), &p); | |
3405 | if (ret < 0) { | |
81d0912d FB |
3406 | switch(c) { |
3407 | case 'F': | |
376253ec AL |
3408 | monitor_printf(mon, "%s: filename expected\n", |
3409 | cmdname); | |
81d0912d FB |
3410 | break; |
3411 | case 'B': | |
376253ec AL |
3412 | monitor_printf(mon, "%s: block device name expected\n", |
3413 | cmdname); | |
81d0912d FB |
3414 | break; |
3415 | default: | |
376253ec | 3416 | monitor_printf(mon, "%s: string expected\n", cmdname); |
81d0912d FB |
3417 | break; |
3418 | } | |
9307c4c1 FB |
3419 | goto fail; |
3420 | } | |
53773581 | 3421 | qdict_put(qdict, key, qstring_from_str(buf)); |
9307c4c1 | 3422 | } |
9dc39cba | 3423 | break; |
9307c4c1 FB |
3424 | case '/': |
3425 | { | |
3426 | int count, format, size; | |
3b46e624 | 3427 | |
cd390083 | 3428 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3429 | p++; |
3430 | if (*p == '/') { | |
3431 | /* format found */ | |
3432 | p++; | |
3433 | count = 1; | |
cd390083 | 3434 | if (qemu_isdigit(*p)) { |
9307c4c1 | 3435 | count = 0; |
cd390083 | 3436 | while (qemu_isdigit(*p)) { |
9307c4c1 FB |
3437 | count = count * 10 + (*p - '0'); |
3438 | p++; | |
3439 | } | |
3440 | } | |
3441 | size = -1; | |
3442 | format = -1; | |
3443 | for(;;) { | |
3444 | switch(*p) { | |
3445 | case 'o': | |
3446 | case 'd': | |
3447 | case 'u': | |
3448 | case 'x': | |
3449 | case 'i': | |
3450 | case 'c': | |
3451 | format = *p++; | |
3452 | break; | |
3453 | case 'b': | |
3454 | size = 1; | |
3455 | p++; | |
3456 | break; | |
3457 | case 'h': | |
3458 | size = 2; | |
3459 | p++; | |
3460 | break; | |
3461 | case 'w': | |
3462 | size = 4; | |
3463 | p++; | |
3464 | break; | |
3465 | case 'g': | |
3466 | case 'L': | |
3467 | size = 8; | |
3468 | p++; | |
3469 | break; | |
3470 | default: | |
3471 | goto next; | |
3472 | } | |
3473 | } | |
3474 | next: | |
cd390083 | 3475 | if (*p != '\0' && !qemu_isspace(*p)) { |
376253ec AL |
3476 | monitor_printf(mon, "invalid char in format: '%c'\n", |
3477 | *p); | |
9307c4c1 FB |
3478 | goto fail; |
3479 | } | |
9307c4c1 FB |
3480 | if (format < 0) |
3481 | format = default_fmt_format; | |
4c27ba27 FB |
3482 | if (format != 'i') { |
3483 | /* for 'i', not specifying a size gives -1 as size */ | |
3484 | if (size < 0) | |
3485 | size = default_fmt_size; | |
e90f009b | 3486 | default_fmt_size = size; |
4c27ba27 | 3487 | } |
9307c4c1 FB |
3488 | default_fmt_format = format; |
3489 | } else { | |
3490 | count = 1; | |
3491 | format = default_fmt_format; | |
4c27ba27 FB |
3492 | if (format != 'i') { |
3493 | size = default_fmt_size; | |
3494 | } else { | |
3495 | size = -1; | |
3496 | } | |
9307c4c1 | 3497 | } |
f7188bbe LC |
3498 | qdict_put(qdict, "count", qint_from_int(count)); |
3499 | qdict_put(qdict, "format", qint_from_int(format)); | |
3500 | qdict_put(qdict, "size", qint_from_int(size)); | |
9307c4c1 | 3501 | } |
9dc39cba | 3502 | break; |
9307c4c1 | 3503 | case 'i': |
92a31b1f | 3504 | case 'l': |
31d85f6a | 3505 | case 'M': |
9307c4c1 | 3506 | { |
c2efc95d | 3507 | int64_t val; |
7743e588 | 3508 | |
cd390083 | 3509 | while (qemu_isspace(*p)) |
9307c4c1 | 3510 | p++; |
3440557b | 3511 | if (*typestr == '?' || *typestr == '.') { |
3440557b | 3512 | if (*typestr == '?') { |
53773581 LC |
3513 | if (*p == '\0') { |
3514 | typestr++; | |
3515 | break; | |
3516 | } | |
3440557b FB |
3517 | } else { |
3518 | if (*p == '.') { | |
3519 | p++; | |
cd390083 | 3520 | while (qemu_isspace(*p)) |
3440557b | 3521 | p++; |
3440557b | 3522 | } else { |
53773581 LC |
3523 | typestr++; |
3524 | break; | |
3440557b FB |
3525 | } |
3526 | } | |
13224a87 | 3527 | typestr++; |
9307c4c1 | 3528 | } |
376253ec | 3529 | if (get_expr(mon, &val, &p)) |
9307c4c1 | 3530 | goto fail; |
675ebef9 LC |
3531 | /* Check if 'i' is greater than 32-bit */ |
3532 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { | |
3533 | monitor_printf(mon, "\'%s\' has failed: ", cmdname); | |
3534 | monitor_printf(mon, "integer is for 32-bit values\n"); | |
3535 | goto fail; | |
31d85f6a LC |
3536 | } else if (c == 'M') { |
3537 | val <<= 20; | |
675ebef9 | 3538 | } |
53773581 | 3539 | qdict_put(qdict, key, qint_from_int(val)); |
9307c4c1 FB |
3540 | } |
3541 | break; | |
3542 | case '-': | |
3543 | { | |
fbc3d96c | 3544 | const char *tmp = p; |
3545 | int has_option, skip_key = 0; | |
9307c4c1 | 3546 | /* option */ |
3b46e624 | 3547 | |
9307c4c1 FB |
3548 | c = *typestr++; |
3549 | if (c == '\0') | |
3550 | goto bad_type; | |
cd390083 | 3551 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3552 | p++; |
3553 | has_option = 0; | |
3554 | if (*p == '-') { | |
3555 | p++; | |
fbc3d96c | 3556 | if(c != *p) { |
3557 | if(!is_valid_option(p, typestr)) { | |
3558 | ||
3559 | monitor_printf(mon, "%s: unsupported option -%c\n", | |
3560 | cmdname, *p); | |
3561 | goto fail; | |
3562 | } else { | |
3563 | skip_key = 1; | |
3564 | } | |
3565 | } | |
3566 | if(skip_key) { | |
3567 | p = tmp; | |
3568 | } else { | |
3569 | p++; | |
3570 | has_option = 1; | |
9307c4c1 | 3571 | } |
9307c4c1 | 3572 | } |
f7188bbe | 3573 | qdict_put(qdict, key, qint_from_int(has_option)); |
9307c4c1 FB |
3574 | } |
3575 | break; | |
3576 | default: | |
3577 | bad_type: | |
376253ec | 3578 | monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c); |
9307c4c1 FB |
3579 | goto fail; |
3580 | } | |
4d76d2ba LC |
3581 | qemu_free(key); |
3582 | key = NULL; | |
9dc39cba | 3583 | } |
9307c4c1 | 3584 | /* check that all arguments were parsed */ |
cd390083 | 3585 | while (qemu_isspace(*p)) |
9307c4c1 FB |
3586 | p++; |
3587 | if (*p != '\0') { | |
376253ec AL |
3588 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
3589 | cmdname); | |
9307c4c1 | 3590 | goto fail; |
9dc39cba | 3591 | } |
9307c4c1 | 3592 | |
55f81d96 | 3593 | return cmd; |
ac7531ec | 3594 | |
55f81d96 | 3595 | fail: |
4d76d2ba | 3596 | qemu_free(key); |
55f81d96 LC |
3597 | return NULL; |
3598 | } | |
3599 | ||
8204a918 LC |
3600 | static void monitor_print_error(Monitor *mon) |
3601 | { | |
3602 | qerror_print(mon->error); | |
3603 | QDECREF(mon->error); | |
3604 | mon->error = NULL; | |
3605 | } | |
3606 | ||
99e2fc16 LC |
3607 | static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd, |
3608 | const QDict *params) | |
3609 | { | |
3610 | QObject *data = NULL; | |
3611 | ||
3612 | cmd->mhandler.cmd_new(mon, params, &data); | |
25b422eb LC |
3613 | |
3614 | if (monitor_ctrl_mode(mon)) { | |
3615 | /* Monitor Protocol */ | |
3616 | monitor_protocol_emitter(mon, data); | |
3617 | } else { | |
3618 | /* User Protocol */ | |
3619 | if (data) | |
3620 | cmd->user_print(mon, data); | |
3621 | } | |
99e2fc16 LC |
3622 | |
3623 | qobject_decref(data); | |
3624 | } | |
3625 | ||
f3c157c4 | 3626 | static void handle_user_command(Monitor *mon, const char *cmdline) |
55f81d96 | 3627 | { |
55f81d96 | 3628 | QDict *qdict; |
c227f099 | 3629 | const mon_cmd_t *cmd; |
55f81d96 LC |
3630 | |
3631 | qdict = qdict_new(); | |
3632 | ||
590fb3b7 | 3633 | cmd = monitor_parse_command(mon, cmdline, qdict); |
13917bee LC |
3634 | if (!cmd) |
3635 | goto out; | |
3636 | ||
3637 | qemu_errors_to_mon(mon); | |
3638 | ||
3639 | if (monitor_handler_ported(cmd)) { | |
99e2fc16 | 3640 | monitor_call_handler(mon, cmd, qdict); |
13917bee | 3641 | } else { |
af4ce882 | 3642 | cmd->mhandler.cmd(mon, qdict); |
55f81d96 LC |
3643 | } |
3644 | ||
8204a918 LC |
3645 | if (monitor_has_error(mon)) |
3646 | monitor_print_error(mon); | |
3647 | ||
3648 | qemu_errors_to_previous(); | |
13917bee LC |
3649 | |
3650 | out: | |
f7188bbe | 3651 | QDECREF(qdict); |
9dc39cba FB |
3652 | } |
3653 | ||
81d0912d FB |
3654 | static void cmd_completion(const char *name, const char *list) |
3655 | { | |
3656 | const char *p, *pstart; | |
3657 | char cmd[128]; | |
3658 | int len; | |
3659 | ||
3660 | p = list; | |
3661 | for(;;) { | |
3662 | pstart = p; | |
3663 | p = strchr(p, '|'); | |
3664 | if (!p) | |
3665 | p = pstart + strlen(pstart); | |
3666 | len = p - pstart; | |
3667 | if (len > sizeof(cmd) - 2) | |
3668 | len = sizeof(cmd) - 2; | |
3669 | memcpy(cmd, pstart, len); | |
3670 | cmd[len] = '\0'; | |
3671 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { | |
731b0364 | 3672 | readline_add_completion(cur_mon->rs, cmd); |
81d0912d FB |
3673 | } |
3674 | if (*p == '\0') | |
3675 | break; | |
3676 | p++; | |
3677 | } | |
3678 | } | |
3679 | ||
3680 | static void file_completion(const char *input) | |
3681 | { | |
3682 | DIR *ffs; | |
3683 | struct dirent *d; | |
3684 | char path[1024]; | |
3685 | char file[1024], file_prefix[1024]; | |
3686 | int input_path_len; | |
3687 | const char *p; | |
3688 | ||
5fafdf24 | 3689 | p = strrchr(input, '/'); |
81d0912d FB |
3690 | if (!p) { |
3691 | input_path_len = 0; | |
3692 | pstrcpy(file_prefix, sizeof(file_prefix), input); | |
363a37d5 | 3693 | pstrcpy(path, sizeof(path), "."); |
81d0912d FB |
3694 | } else { |
3695 | input_path_len = p - input + 1; | |
3696 | memcpy(path, input, input_path_len); | |
3697 | if (input_path_len > sizeof(path) - 1) | |
3698 | input_path_len = sizeof(path) - 1; | |
3699 | path[input_path_len] = '\0'; | |
3700 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); | |
3701 | } | |
3702 | #ifdef DEBUG_COMPLETION | |
376253ec AL |
3703 | monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n", |
3704 | input, path, file_prefix); | |
81d0912d FB |
3705 | #endif |
3706 | ffs = opendir(path); | |
3707 | if (!ffs) | |
3708 | return; | |
3709 | for(;;) { | |
3710 | struct stat sb; | |
3711 | d = readdir(ffs); | |
3712 | if (!d) | |
3713 | break; | |
3714 | if (strstart(d->d_name, file_prefix, NULL)) { | |
3715 | memcpy(file, input, input_path_len); | |
363a37d5 BS |
3716 | if (input_path_len < sizeof(file)) |
3717 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, | |
3718 | d->d_name); | |
81d0912d FB |
3719 | /* stat the file to find out if it's a directory. |
3720 | * In that case add a slash to speed up typing long paths | |
3721 | */ | |
3722 | stat(file, &sb); | |
3723 | if(S_ISDIR(sb.st_mode)) | |
363a37d5 | 3724 | pstrcat(file, sizeof(file), "/"); |
731b0364 | 3725 | readline_add_completion(cur_mon->rs, file); |
81d0912d FB |
3726 | } |
3727 | } | |
3728 | closedir(ffs); | |
3729 | } | |
3730 | ||
51de9760 | 3731 | static void block_completion_it(void *opaque, BlockDriverState *bs) |
81d0912d | 3732 | { |
51de9760 | 3733 | const char *name = bdrv_get_device_name(bs); |
81d0912d FB |
3734 | const char *input = opaque; |
3735 | ||
3736 | if (input[0] == '\0' || | |
3737 | !strncmp(name, (char *)input, strlen(input))) { | |
731b0364 | 3738 | readline_add_completion(cur_mon->rs, name); |
81d0912d FB |
3739 | } |
3740 | } | |
3741 | ||
3742 | /* NOTE: this parser is an approximate form of the real command parser */ | |
3743 | static void parse_cmdline(const char *cmdline, | |
3744 | int *pnb_args, char **args) | |
3745 | { | |
3746 | const char *p; | |
3747 | int nb_args, ret; | |
3748 | char buf[1024]; | |
3749 | ||
3750 | p = cmdline; | |
3751 | nb_args = 0; | |
3752 | for(;;) { | |
cd390083 | 3753 | while (qemu_isspace(*p)) |
81d0912d FB |
3754 | p++; |
3755 | if (*p == '\0') | |
3756 | break; | |
3757 | if (nb_args >= MAX_ARGS) | |
3758 | break; | |
3759 | ret = get_str(buf, sizeof(buf), &p); | |
3760 | args[nb_args] = qemu_strdup(buf); | |
3761 | nb_args++; | |
3762 | if (ret < 0) | |
3763 | break; | |
3764 | } | |
3765 | *pnb_args = nb_args; | |
3766 | } | |
3767 | ||
4d76d2ba LC |
3768 | static const char *next_arg_type(const char *typestr) |
3769 | { | |
3770 | const char *p = strchr(typestr, ':'); | |
3771 | return (p != NULL ? ++p : typestr); | |
3772 | } | |
3773 | ||
4c36ba32 | 3774 | static void monitor_find_completion(const char *cmdline) |
81d0912d FB |
3775 | { |
3776 | const char *cmdname; | |
3777 | char *args[MAX_ARGS]; | |
3778 | int nb_args, i, len; | |
3779 | const char *ptype, *str; | |
c227f099 | 3780 | const mon_cmd_t *cmd; |
64866c3d | 3781 | const KeyDef *key; |
81d0912d FB |
3782 | |
3783 | parse_cmdline(cmdline, &nb_args, args); | |
3784 | #ifdef DEBUG_COMPLETION | |
3785 | for(i = 0; i < nb_args; i++) { | |
376253ec | 3786 | monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]); |
81d0912d FB |
3787 | } |
3788 | #endif | |
3789 | ||
3790 | /* if the line ends with a space, it means we want to complete the | |
3791 | next arg */ | |
3792 | len = strlen(cmdline); | |
cd390083 | 3793 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { |
81d0912d FB |
3794 | if (nb_args >= MAX_ARGS) |
3795 | return; | |
3796 | args[nb_args++] = qemu_strdup(""); | |
3797 | } | |
3798 | if (nb_args <= 1) { | |
3799 | /* command completion */ | |
3800 | if (nb_args == 0) | |
3801 | cmdname = ""; | |
3802 | else | |
3803 | cmdname = args[0]; | |
731b0364 | 3804 | readline_set_completion_index(cur_mon->rs, strlen(cmdname)); |
376253ec | 3805 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
3806 | cmd_completion(cmdname, cmd->name); |
3807 | } | |
3808 | } else { | |
3809 | /* find the command */ | |
376253ec | 3810 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |
81d0912d FB |
3811 | if (compare_cmd(args[0], cmd->name)) |
3812 | goto found; | |
3813 | } | |
3814 | return; | |
3815 | found: | |
4d76d2ba | 3816 | ptype = next_arg_type(cmd->args_type); |
81d0912d FB |
3817 | for(i = 0; i < nb_args - 2; i++) { |
3818 | if (*ptype != '\0') { | |
4d76d2ba | 3819 | ptype = next_arg_type(ptype); |
81d0912d | 3820 | while (*ptype == '?') |
4d76d2ba | 3821 | ptype = next_arg_type(ptype); |
81d0912d FB |
3822 | } |
3823 | } | |
3824 | str = args[nb_args - 1]; | |
2a1704a7 BS |
3825 | if (*ptype == '-' && ptype[1] != '\0') { |
3826 | ptype += 2; | |
3827 | } | |
81d0912d FB |
3828 | switch(*ptype) { |
3829 | case 'F': | |
3830 | /* file completion */ | |
731b0364 | 3831 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3832 | file_completion(str); |
3833 | break; | |
3834 | case 'B': | |
3835 | /* block device name completion */ | |
731b0364 | 3836 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
81d0912d FB |
3837 | bdrv_iterate(block_completion_it, (void *)str); |
3838 | break; | |
7fe48483 FB |
3839 | case 's': |
3840 | /* XXX: more generic ? */ | |
3841 | if (!strcmp(cmd->name, "info")) { | |
731b0364 | 3842 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
7fe48483 FB |
3843 | for(cmd = info_cmds; cmd->name != NULL; cmd++) { |
3844 | cmd_completion(str, cmd->name); | |
3845 | } | |
64866c3d | 3846 | } else if (!strcmp(cmd->name, "sendkey")) { |
e600d1ef BS |
3847 | char *sep = strrchr(str, '-'); |
3848 | if (sep) | |
3849 | str = sep + 1; | |
731b0364 | 3850 | readline_set_completion_index(cur_mon->rs, strlen(str)); |
64866c3d FB |
3851 | for(key = key_defs; key->name != NULL; key++) { |
3852 | cmd_completion(str, key->name); | |
3853 | } | |
f3353c6b JK |
3854 | } else if (!strcmp(cmd->name, "help|?")) { |
3855 | readline_set_completion_index(cur_mon->rs, strlen(str)); | |
3856 | for (cmd = mon_cmds; cmd->name != NULL; cmd++) { | |
3857 | cmd_completion(str, cmd->name); | |
3858 | } | |
7fe48483 FB |
3859 | } |
3860 | break; | |
81d0912d FB |
3861 | default: |
3862 | break; | |
3863 | } | |
3864 | } | |
3865 | for(i = 0; i < nb_args; i++) | |
3866 | qemu_free(args[i]); | |
3867 | } | |
3868 | ||
731b0364 | 3869 | static int monitor_can_read(void *opaque) |
9dc39cba | 3870 | { |
731b0364 AL |
3871 | Monitor *mon = opaque; |
3872 | ||
1c3f96be | 3873 | return (mon->suspend_cnt == 0) ? 1 : 0; |
9dc39cba FB |
3874 | } |
3875 | ||
5fa737a4 LC |
3876 | typedef struct CmdArgs { |
3877 | QString *name; | |
3878 | int type; | |
3879 | int flag; | |
3880 | int optional; | |
3881 | } CmdArgs; | |
3882 | ||
3883 | static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args) | |
3884 | { | |
3885 | if (!cmd_args->optional) { | |
3886 | qemu_error_new(QERR_MISSING_PARAMETER, name); | |
3887 | return -1; | |
3888 | } | |
3889 | ||
3890 | if (cmd_args->type == '-') { | |
3891 | /* handlers expect a value, they need to be changed */ | |
3892 | qdict_put(args, name, qint_from_int(0)); | |
3893 | } | |
3894 | ||
3895 | return 0; | |
3896 | } | |
3897 | ||
3898 | static int check_arg(const CmdArgs *cmd_args, QDict *args) | |
3899 | { | |
3900 | QObject *value; | |
3901 | const char *name; | |
3902 | ||
3903 | name = qstring_get_str(cmd_args->name); | |
3904 | ||
3905 | if (!args) { | |
3906 | return check_opt(cmd_args, name, args); | |
3907 | } | |
3908 | ||
3909 | value = qdict_get(args, name); | |
3910 | if (!value) { | |
3911 | return check_opt(cmd_args, name, args); | |
3912 | } | |
3913 | ||
3914 | switch (cmd_args->type) { | |
3915 | case 'F': | |
3916 | case 'B': | |
3917 | case 's': | |
3918 | if (qobject_type(value) != QTYPE_QSTRING) { | |
3919 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string"); | |
3920 | return -1; | |
3921 | } | |
3922 | break; | |
3923 | case '/': { | |
3924 | int i; | |
3925 | const char *keys[] = { "count", "format", "size", NULL }; | |
3926 | ||
3927 | for (i = 0; keys[i]; i++) { | |
3928 | QObject *obj = qdict_get(args, keys[i]); | |
3929 | if (!obj) { | |
3930 | qemu_error_new(QERR_MISSING_PARAMETER, name); | |
3931 | return -1; | |
3932 | } | |
3933 | if (qobject_type(obj) != QTYPE_QINT) { | |
3934 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
3935 | return -1; | |
3936 | } | |
3937 | } | |
3938 | break; | |
3939 | } | |
3940 | case 'i': | |
3941 | case 'l': | |
31d85f6a | 3942 | case 'M': |
5fa737a4 LC |
3943 | if (qobject_type(value) != QTYPE_QINT) { |
3944 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
3945 | return -1; | |
3946 | } | |
3947 | break; | |
3948 | case '-': | |
3949 | if (qobject_type(value) != QTYPE_QINT && | |
3950 | qobject_type(value) != QTYPE_QBOOL) { | |
3951 | qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool"); | |
3952 | return -1; | |
3953 | } | |
3954 | if (qobject_type(value) == QTYPE_QBOOL) { | |
3955 | /* handlers expect a QInt, they need to be changed */ | |
3956 | qdict_put(args, name, | |
3957 | qint_from_int(qbool_get_int(qobject_to_qbool(value)))); | |
3958 | } | |
3959 | break; | |
3960 | default: | |
3961 | /* impossible */ | |
3962 | abort(); | |
3963 | } | |
3964 | ||
3965 | return 0; | |
3966 | } | |
3967 | ||
3968 | static void cmd_args_init(CmdArgs *cmd_args) | |
3969 | { | |
3970 | cmd_args->name = qstring_new(); | |
3971 | cmd_args->type = cmd_args->flag = cmd_args->optional = 0; | |
3972 | } | |
3973 | ||
3974 | /* | |
3975 | * This is not trivial, we have to parse Monitor command's argument | |
3976 | * type syntax to be able to check the arguments provided by clients. | |
3977 | * | |
3978 | * In the near future we will be using an array for that and will be | |
3979 | * able to drop all this parsing... | |
3980 | */ | |
3981 | static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args) | |
3982 | { | |
3983 | int err; | |
3984 | const char *p; | |
3985 | CmdArgs cmd_args; | |
3986 | ||
dd5121bd | 3987 | if (cmd->args_type == NULL) { |
5fa737a4 LC |
3988 | return (qdict_size(args) == 0 ? 0 : -1); |
3989 | } | |
3990 | ||
3991 | err = 0; | |
3992 | cmd_args_init(&cmd_args); | |
3993 | ||
3994 | for (p = cmd->args_type;; p++) { | |
3995 | if (*p == ':') { | |
3996 | cmd_args.type = *++p; | |
3997 | p++; | |
3998 | if (cmd_args.type == '-') { | |
3999 | cmd_args.flag = *p++; | |
4000 | cmd_args.optional = 1; | |
4001 | } else if (*p == '?') { | |
4002 | cmd_args.optional = 1; | |
4003 | p++; | |
4004 | } | |
4005 | ||
4006 | assert(*p == ',' || *p == '\0'); | |
4007 | err = check_arg(&cmd_args, args); | |
4008 | ||
4009 | QDECREF(cmd_args.name); | |
4010 | cmd_args_init(&cmd_args); | |
4011 | ||
4012 | if (err < 0) { | |
4013 | break; | |
4014 | } | |
4015 | } else { | |
4016 | qstring_append_chr(cmd_args.name, *p); | |
4017 | } | |
4018 | ||
4019 | if (*p == '\0') { | |
4020 | break; | |
4021 | } | |
4022 | } | |
4023 | ||
4024 | QDECREF(cmd_args.name); | |
4025 | return err; | |
4026 | } | |
4027 | ||
4028 | static void handle_qmp_command(JSONMessageParser *parser, QList *tokens) | |
4029 | { | |
4030 | int err; | |
4031 | QObject *obj; | |
4032 | QDict *input, *args; | |
5fa737a4 LC |
4033 | const mon_cmd_t *cmd; |
4034 | Monitor *mon = cur_mon; | |
5e23f480 | 4035 | const char *cmd_name, *info_item; |
5fa737a4 LC |
4036 | |
4037 | args = NULL; | |
4038 | qemu_errors_to_mon(mon); | |
4039 | ||
4040 | obj = json_parser_parse(tokens, NULL); | |
4041 | if (!obj) { | |
4042 | // FIXME: should be triggered in json_parser_parse() | |
4043 | qemu_error_new(QERR_JSON_PARSING); | |
4044 | goto err_out; | |
4045 | } else if (qobject_type(obj) != QTYPE_QDICT) { | |
4046 | qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object"); | |
4047 | qobject_decref(obj); | |
4048 | goto err_out; | |
4049 | } | |
4050 | ||
4051 | input = qobject_to_qdict(obj); | |
4052 | ||
4053 | mon->mc->id = qdict_get(input, "id"); | |
4054 | qobject_incref(mon->mc->id); | |
4055 | ||
4056 | obj = qdict_get(input, "execute"); | |
4057 | if (!obj) { | |
4058 | qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute"); | |
4059 | goto err_input; | |
4060 | } else if (qobject_type(obj) != QTYPE_QSTRING) { | |
4061 | qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string"); | |
4062 | goto err_input; | |
4063 | } | |
4064 | ||
4065 | cmd_name = qstring_get_str(qobject_to_qstring(obj)); | |
5e23f480 LC |
4066 | |
4067 | /* | |
4068 | * XXX: We need this special case until we get info handlers | |
4069 | * converted into 'query-' commands | |
4070 | */ | |
4071 | if (compare_cmd(cmd_name, "info")) { | |
5fa737a4 LC |
4072 | qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name); |
4073 | goto err_input; | |
5e23f480 LC |
4074 | } else if (strstart(cmd_name, "query-", &info_item)) { |
4075 | cmd = monitor_find_command("info"); | |
4076 | qdict_put_obj(input, "arguments", | |
4077 | qobject_from_jsonf("{ 'item': %s }", info_item)); | |
4078 | } else { | |
4079 | cmd = monitor_find_command(cmd_name); | |
5b06a3f7 | 4080 | if (!cmd || !monitor_handler_ported(cmd)) { |
5e23f480 LC |
4081 | qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name); |
4082 | goto err_input; | |
4083 | } | |
5fa737a4 LC |
4084 | } |
4085 | ||
4086 | obj = qdict_get(input, "arguments"); | |
4087 | if (!obj) { | |
4088 | args = qdict_new(); | |
4089 | } else { | |
4090 | args = qobject_to_qdict(obj); | |
4091 | QINCREF(args); | |
4092 | } | |
4093 | ||
4094 | QDECREF(input); | |
4095 | ||
4096 | err = monitor_check_qmp_args(cmd, args); | |
4097 | if (err < 0) { | |
4098 | goto err_out; | |
4099 | } | |
4100 | ||
4101 | monitor_call_handler(mon, cmd, args); | |
4102 | goto out; | |
4103 | ||
4104 | err_input: | |
4105 | QDECREF(input); | |
4106 | err_out: | |
4107 | monitor_protocol_emitter(mon, NULL); | |
4108 | out: | |
4109 | QDECREF(args); | |
4110 | qemu_errors_to_previous(); | |
4111 | } | |
4112 | ||
9b57c02e LC |
4113 | /** |
4114 | * monitor_control_read(): Read and handle QMP input | |
4115 | */ | |
4116 | static void monitor_control_read(void *opaque, const uint8_t *buf, int size) | |
4117 | { | |
4118 | Monitor *old_mon = cur_mon; | |
4119 | ||
4120 | cur_mon = opaque; | |
4121 | ||
5fa737a4 | 4122 | json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size); |
9b57c02e LC |
4123 | |
4124 | cur_mon = old_mon; | |
4125 | } | |
4126 | ||
731b0364 | 4127 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
9dc39cba | 4128 | { |
731b0364 | 4129 | Monitor *old_mon = cur_mon; |
7e2515e8 | 4130 | int i; |
376253ec | 4131 | |
731b0364 AL |
4132 | cur_mon = opaque; |
4133 | ||
cde76ee1 AL |
4134 | if (cur_mon->rs) { |
4135 | for (i = 0; i < size; i++) | |
4136 | readline_handle_byte(cur_mon->rs, buf[i]); | |
4137 | } else { | |
4138 | if (size == 0 || buf[size - 1] != 0) | |
4139 | monitor_printf(cur_mon, "corrupted command\n"); | |
4140 | else | |
f3c157c4 | 4141 | handle_user_command(cur_mon, (char *)buf); |
cde76ee1 | 4142 | } |
9dc39cba | 4143 | |
731b0364 AL |
4144 | cur_mon = old_mon; |
4145 | } | |
d8f44609 | 4146 | |
376253ec | 4147 | static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque) |
aa455485 | 4148 | { |
731b0364 | 4149 | monitor_suspend(mon); |
f3c157c4 | 4150 | handle_user_command(mon, cmdline); |
731b0364 | 4151 | monitor_resume(mon); |
d8f44609 AL |
4152 | } |
4153 | ||
cde76ee1 | 4154 | int monitor_suspend(Monitor *mon) |
d8f44609 | 4155 | { |
cde76ee1 AL |
4156 | if (!mon->rs) |
4157 | return -ENOTTY; | |
731b0364 | 4158 | mon->suspend_cnt++; |
cde76ee1 | 4159 | return 0; |
d8f44609 AL |
4160 | } |
4161 | ||
376253ec | 4162 | void monitor_resume(Monitor *mon) |
d8f44609 | 4163 | { |
cde76ee1 AL |
4164 | if (!mon->rs) |
4165 | return; | |
731b0364 AL |
4166 | if (--mon->suspend_cnt == 0) |
4167 | readline_show_prompt(mon->rs); | |
aa455485 FB |
4168 | } |
4169 | ||
9b57c02e LC |
4170 | /** |
4171 | * monitor_control_event(): Print QMP gretting | |
4172 | */ | |
4173 | static void monitor_control_event(void *opaque, int event) | |
4174 | { | |
4175 | if (event == CHR_EVENT_OPENED) { | |
4176 | QObject *data; | |
4177 | Monitor *mon = opaque; | |
4178 | ||
5fa737a4 LC |
4179 | json_message_parser_init(&mon->mc->parser, handle_qmp_command); |
4180 | ||
9b57c02e LC |
4181 | data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }"); |
4182 | assert(data != NULL); | |
4183 | ||
4184 | monitor_json_emitter(mon, data); | |
4185 | qobject_decref(data); | |
4186 | } | |
4187 | } | |
4188 | ||
731b0364 | 4189 | static void monitor_event(void *opaque, int event) |
86e94dea | 4190 | { |
376253ec AL |
4191 | Monitor *mon = opaque; |
4192 | ||
2724b180 AL |
4193 | switch (event) { |
4194 | case CHR_EVENT_MUX_IN: | |
a7aec5da GH |
4195 | mon->mux_out = 0; |
4196 | if (mon->reset_seen) { | |
4197 | readline_restart(mon->rs); | |
4198 | monitor_resume(mon); | |
4199 | monitor_flush(mon); | |
4200 | } else { | |
4201 | mon->suspend_cnt = 0; | |
4202 | } | |
2724b180 AL |
4203 | break; |
4204 | ||
4205 | case CHR_EVENT_MUX_OUT: | |
a7aec5da GH |
4206 | if (mon->reset_seen) { |
4207 | if (mon->suspend_cnt == 0) { | |
4208 | monitor_printf(mon, "\n"); | |
4209 | } | |
4210 | monitor_flush(mon); | |
4211 | monitor_suspend(mon); | |
4212 | } else { | |
4213 | mon->suspend_cnt++; | |
4214 | } | |
4215 | mon->mux_out = 1; | |
2724b180 | 4216 | break; |
86e94dea | 4217 | |
b6b8df56 | 4218 | case CHR_EVENT_OPENED: |
2724b180 AL |
4219 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " |
4220 | "information\n", QEMU_VERSION); | |
a7aec5da | 4221 | if (!mon->mux_out) { |
2724b180 | 4222 | readline_show_prompt(mon->rs); |
a7aec5da GH |
4223 | } |
4224 | mon->reset_seen = 1; | |
2724b180 AL |
4225 | break; |
4226 | } | |
86e94dea TS |
4227 | } |
4228 | ||
76655d6d AL |
4229 | |
4230 | /* | |
4231 | * Local variables: | |
4232 | * c-indent-level: 4 | |
4233 | * c-basic-offset: 4 | |
4234 | * tab-width: 8 | |
4235 | * End: | |
4236 | */ | |
4237 | ||
731b0364 | 4238 | void monitor_init(CharDriverState *chr, int flags) |
aa455485 | 4239 | { |
731b0364 | 4240 | static int is_first_init = 1; |
87127161 | 4241 | Monitor *mon; |
20d8a3ed TS |
4242 | |
4243 | if (is_first_init) { | |
c8256f9d | 4244 | key_timer = qemu_new_timer(vm_clock, release_keys, NULL); |
20d8a3ed TS |
4245 | is_first_init = 0; |
4246 | } | |
87127161 AL |
4247 | |
4248 | mon = qemu_mallocz(sizeof(*mon)); | |
20d8a3ed | 4249 | |
87127161 | 4250 | mon->chr = chr; |
731b0364 | 4251 | mon->flags = flags; |
cde76ee1 AL |
4252 | if (flags & MONITOR_USE_READLINE) { |
4253 | mon->rs = readline_init(mon, monitor_find_completion); | |
4254 | monitor_read_command(mon, 0); | |
4255 | } | |
87127161 | 4256 | |
9b57c02e | 4257 | if (monitor_ctrl_mode(mon)) { |
5fa737a4 | 4258 | mon->mc = qemu_mallocz(sizeof(MonitorControl)); |
9b57c02e LC |
4259 | /* Control mode requires special handlers */ |
4260 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read, | |
4261 | monitor_control_event, mon); | |
4262 | } else { | |
4263 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, | |
4264 | monitor_event, mon); | |
4265 | } | |
87127161 | 4266 | |
72cf2d4f | 4267 | QLIST_INSERT_HEAD(&mon_list, mon, entry); |
731b0364 | 4268 | if (!cur_mon || (flags & MONITOR_IS_DEFAULT)) |
87127161 | 4269 | cur_mon = mon; |
aa455485 FB |
4270 | } |
4271 | ||
376253ec | 4272 | static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque) |
81d0912d | 4273 | { |
bb5fc20f AL |
4274 | BlockDriverState *bs = opaque; |
4275 | int ret = 0; | |
81d0912d | 4276 | |
bb5fc20f | 4277 | if (bdrv_set_key(bs, password) != 0) { |
376253ec | 4278 | monitor_printf(mon, "invalid password\n"); |
bb5fc20f | 4279 | ret = -EPERM; |
9dc39cba | 4280 | } |
731b0364 AL |
4281 | if (mon->password_completion_cb) |
4282 | mon->password_completion_cb(mon->password_opaque, ret); | |
bb5fc20f | 4283 | |
731b0364 | 4284 | monitor_read_command(mon, 1); |
9dc39cba | 4285 | } |
c0f4ce77 | 4286 | |
376253ec | 4287 | void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs, |
bb5fc20f AL |
4288 | BlockDriverCompletionFunc *completion_cb, |
4289 | void *opaque) | |
c0f4ce77 | 4290 | { |
cde76ee1 AL |
4291 | int err; |
4292 | ||
bb5fc20f AL |
4293 | if (!bdrv_key_required(bs)) { |
4294 | if (completion_cb) | |
4295 | completion_cb(opaque, 0); | |
4296 | return; | |
4297 | } | |
c0f4ce77 | 4298 | |
931a548b LC |
4299 | if (monitor_ctrl_mode(mon)) { |
4300 | qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs)); | |
4301 | return; | |
4302 | } | |
4303 | ||
376253ec AL |
4304 | monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs), |
4305 | bdrv_get_encrypted_filename(bs)); | |
bb5fc20f | 4306 | |
731b0364 AL |
4307 | mon->password_completion_cb = completion_cb; |
4308 | mon->password_opaque = opaque; | |
bb5fc20f | 4309 | |
cde76ee1 AL |
4310 | err = monitor_read_password(mon, bdrv_password_cb, bs); |
4311 | ||
4312 | if (err && completion_cb) | |
4313 | completion_cb(opaque, err); | |
c0f4ce77 | 4314 | } |
ac7531ec GH |
4315 | |
4316 | typedef struct QemuErrorSink QemuErrorSink; | |
4317 | struct QemuErrorSink { | |
4318 | enum { | |
4319 | ERR_SINK_FILE, | |
4320 | ERR_SINK_MONITOR, | |
4321 | } dest; | |
4322 | union { | |
4323 | FILE *fp; | |
4324 | Monitor *mon; | |
4325 | }; | |
4326 | QemuErrorSink *previous; | |
4327 | }; | |
4328 | ||
528e93a9 | 4329 | static QemuErrorSink *qemu_error_sink; |
ac7531ec GH |
4330 | |
4331 | void qemu_errors_to_file(FILE *fp) | |
4332 | { | |
4333 | QemuErrorSink *sink; | |
4334 | ||
4335 | sink = qemu_mallocz(sizeof(*sink)); | |
4336 | sink->dest = ERR_SINK_FILE; | |
4337 | sink->fp = fp; | |
4338 | sink->previous = qemu_error_sink; | |
4339 | qemu_error_sink = sink; | |
4340 | } | |
4341 | ||
4342 | void qemu_errors_to_mon(Monitor *mon) | |
4343 | { | |
4344 | QemuErrorSink *sink; | |
4345 | ||
4346 | sink = qemu_mallocz(sizeof(*sink)); | |
4347 | sink->dest = ERR_SINK_MONITOR; | |
4348 | sink->mon = mon; | |
4349 | sink->previous = qemu_error_sink; | |
4350 | qemu_error_sink = sink; | |
4351 | } | |
4352 | ||
4353 | void qemu_errors_to_previous(void) | |
4354 | { | |
4355 | QemuErrorSink *sink; | |
4356 | ||
4357 | assert(qemu_error_sink != NULL); | |
4358 | sink = qemu_error_sink; | |
4359 | qemu_error_sink = sink->previous; | |
4360 | qemu_free(sink); | |
4361 | } | |
4362 | ||
4363 | void qemu_error(const char *fmt, ...) | |
4364 | { | |
4365 | va_list args; | |
4366 | ||
4367 | assert(qemu_error_sink != NULL); | |
4368 | switch (qemu_error_sink->dest) { | |
4369 | case ERR_SINK_FILE: | |
4370 | va_start(args, fmt); | |
4371 | vfprintf(qemu_error_sink->fp, fmt, args); | |
4372 | va_end(args); | |
4373 | break; | |
4374 | case ERR_SINK_MONITOR: | |
4375 | va_start(args, fmt); | |
4376 | monitor_vprintf(qemu_error_sink->mon, fmt, args); | |
4377 | va_end(args); | |
4378 | break; | |
4379 | } | |
4380 | } | |
8204a918 LC |
4381 | |
4382 | void qemu_error_internal(const char *file, int linenr, const char *func, | |
4383 | const char *fmt, ...) | |
4384 | { | |
4385 | va_list va; | |
4386 | QError *qerror; | |
4387 | ||
4388 | assert(qemu_error_sink != NULL); | |
4389 | ||
4390 | va_start(va, fmt); | |
4391 | qerror = qerror_from_info(file, linenr, func, fmt, &va); | |
4392 | va_end(va); | |
4393 | ||
4394 | switch (qemu_error_sink->dest) { | |
4395 | case ERR_SINK_FILE: | |
4396 | qerror_print(qerror); | |
4397 | QDECREF(qerror); | |
4398 | break; | |
4399 | case ERR_SINK_MONITOR: | |
4400 | assert(qemu_error_sink->mon->error == NULL); | |
4401 | qemu_error_sink->mon->error = qerror; | |
4402 | break; | |
4403 | } | |
4404 | } |