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