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