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