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