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