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