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