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