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