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