Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[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 "qemu/readline.h"
41 #include "ui/console.h"
42 #include "ui/input.h"
43 #include "sysemu/blockdev.h"
44 #include "audio/audio.h"
45 #include "disas/disas.h"
46 #include "sysemu/balloon.h"
47 #include "qemu/timer.h"
48 #include "migration/migration.h"
49 #include "sysemu/kvm.h"
50 #include "qemu/acl.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qint.h"
53 #include "qapi/qmp/qfloat.h"
54 #include "qapi/qmp/qlist.h"
55 #include "qapi/qmp/qbool.h"
56 #include "qapi/qmp/qstring.h"
57 #include "qapi/qmp/qjson.h"
58 #include "qapi/qmp/json-streamer.h"
59 #include "qapi/qmp/json-parser.h"
60 #include <qom/object_interfaces.h>
61 #include "qemu/osdep.h"
62 #include "cpu.h"
63 #include "trace.h"
64 #include "trace/control.h"
65 #ifdef CONFIG_TRACE_SIMPLE
66 #include "trace/simple.h"
67 #endif
68 #include "exec/memory.h"
69 #include "qmp-commands.h"
70 #include "hmp.h"
71 #include "qemu/thread.h"
72
73 /* for pic/irq_info */
74 #if defined(TARGET_SPARC)
75 #include "hw/sparc/sun4m.h"
76 #endif
77 #include "hw/lm32/lm32_pic.h"
78
79 //#define DEBUG
80 //#define DEBUG_COMPLETION
81
82 /*
83 * Supported types:
84 *
85 * 'F' filename
86 * 'B' block device name
87 * 's' string (accept optional quote)
88 * 'S' it just appends the rest of the string (accept optional quote)
89 * 'O' option string of the form NAME=VALUE,...
90 * parsed according to QemuOptsList given by its name
91 * Example: 'device:O' uses qemu_device_opts.
92 * Restriction: only lists with empty desc are supported
93 * TODO lift the restriction
94 * 'i' 32 bit integer
95 * 'l' target long (32 or 64 bit)
96 * 'M' Non-negative target long (32 or 64 bit), in user mode the
97 * value is multiplied by 2^20 (think Mebibyte)
98 * 'o' octets (aka bytes)
99 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
100 * K, k suffix, which multiplies the value by 2^60 for suffixes E
101 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
102 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
103 * 'T' double
104 * user mode accepts an optional ms, us, ns suffix,
105 * which divides the value by 1e3, 1e6, 1e9, respectively
106 * '/' optional gdb-like print format (like "/10x")
107 *
108 * '?' optional type (for all types, except '/')
109 * '.' other form of optional type (for 'i' and 'l')
110 * 'b' boolean
111 * user mode accepts "on" or "off"
112 * '-' optional parameter (eg. '-f')
113 *
114 */
115
116 typedef struct MonitorCompletionData MonitorCompletionData;
117 struct MonitorCompletionData {
118 Monitor *mon;
119 void (*user_print)(Monitor *mon, const QObject *data);
120 };
121
122 typedef struct mon_cmd_t {
123 const char *name;
124 const char *args_type;
125 const char *params;
126 const char *help;
127 void (*user_print)(Monitor *mon, const QObject *data);
128 union {
129 void (*cmd)(Monitor *mon, const QDict *qdict);
130 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
131 int (*cmd_async)(Monitor *mon, const QDict *params,
132 MonitorCompletion *cb, void *opaque);
133 } mhandler;
134 int flags;
135 /* @sub_table is a list of 2nd level of commands. If it do not exist,
136 * mhandler should be used. If it exist, sub_table[?].mhandler should be
137 * used, and mhandler of 1st level plays the role of help function.
138 */
139 struct mon_cmd_t *sub_table;
140 } mon_cmd_t;
141
142 /* file descriptors passed via SCM_RIGHTS */
143 typedef struct mon_fd_t mon_fd_t;
144 struct mon_fd_t {
145 char *name;
146 int fd;
147 QLIST_ENTRY(mon_fd_t) next;
148 };
149
150 /* file descriptor associated with a file descriptor set */
151 typedef struct MonFdsetFd MonFdsetFd;
152 struct MonFdsetFd {
153 int fd;
154 bool removed;
155 char *opaque;
156 QLIST_ENTRY(MonFdsetFd) next;
157 };
158
159 /* file descriptor set containing fds passed via SCM_RIGHTS */
160 typedef struct MonFdset MonFdset;
161 struct MonFdset {
162 int64_t id;
163 QLIST_HEAD(, MonFdsetFd) fds;
164 QLIST_HEAD(, MonFdsetFd) dup_fds;
165 QLIST_ENTRY(MonFdset) next;
166 };
167
168 typedef struct MonitorControl {
169 QObject *id;
170 JSONMessageParser parser;
171 int command_mode;
172 } MonitorControl;
173
174 /*
175 * To prevent flooding clients, events can be throttled. The
176 * throttling is calculated globally, rather than per-Monitor
177 * instance.
178 */
179 typedef struct MonitorEventState {
180 MonitorEvent event; /* Event being tracked */
181 int64_t rate; /* Period over which to throttle. 0 to disable */
182 int64_t last; /* Time at which event was last emitted */
183 QEMUTimer *timer; /* Timer for handling delayed events */
184 QObject *data; /* Event pending delayed dispatch */
185 } MonitorEventState;
186
187 struct Monitor {
188 CharDriverState *chr;
189 int mux_out;
190 int reset_seen;
191 int flags;
192 int suspend_cnt;
193 bool skip_flush;
194 QString *outbuf;
195 guint watch;
196 ReadLineState *rs;
197 MonitorControl *mc;
198 CPUState *mon_cpu;
199 BlockDriverCompletionFunc *password_completion_cb;
200 void *password_opaque;
201 mon_cmd_t *cmd_table;
202 QError *error;
203 QLIST_HEAD(,mon_fd_t) fds;
204 QLIST_ENTRY(Monitor) entry;
205 };
206
207 /* QMP checker flags */
208 #define QMP_ACCEPT_UNKNOWNS 1
209
210 static QLIST_HEAD(mon_list, Monitor) mon_list;
211 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
212 static int mon_refcount;
213
214 static mon_cmd_t mon_cmds[];
215 static mon_cmd_t info_cmds[];
216
217 static const mon_cmd_t qmp_cmds[];
218
219 Monitor *cur_mon;
220 Monitor *default_mon;
221
222 static void monitor_command_cb(void *opaque, const char *cmdline,
223 void *readline_opaque);
224
225 static inline int qmp_cmd_mode(const Monitor *mon)
226 {
227 return (mon->mc ? mon->mc->command_mode : 0);
228 }
229
230 /* Return true if in control mode, false otherwise */
231 static inline int monitor_ctrl_mode(const Monitor *mon)
232 {
233 return (mon->flags & MONITOR_USE_CONTROL);
234 }
235
236 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
237 int monitor_cur_is_qmp(void)
238 {
239 return cur_mon && monitor_ctrl_mode(cur_mon);
240 }
241
242 void monitor_read_command(Monitor *mon, int show_prompt)
243 {
244 if (!mon->rs)
245 return;
246
247 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
248 if (show_prompt)
249 readline_show_prompt(mon->rs);
250 }
251
252 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
253 void *opaque)
254 {
255 if (monitor_ctrl_mode(mon)) {
256 qerror_report(QERR_MISSING_PARAMETER, "password");
257 return -EINVAL;
258 } else if (mon->rs) {
259 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
260 /* prompt is printed on return from the command handler */
261 return 0;
262 } else {
263 monitor_printf(mon, "terminal does not support password prompting\n");
264 return -ENOTTY;
265 }
266 }
267
268 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
269 void *opaque)
270 {
271 Monitor *mon = opaque;
272
273 mon->watch = 0;
274 monitor_flush(mon);
275 return FALSE;
276 }
277
278 void monitor_flush(Monitor *mon)
279 {
280 int rc;
281 size_t len;
282 const char *buf;
283
284 if (mon->skip_flush) {
285 return;
286 }
287
288 buf = qstring_get_str(mon->outbuf);
289 len = qstring_get_length(mon->outbuf);
290
291 if (len && !mon->mux_out) {
292 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
293 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
294 /* all flushed or error */
295 QDECREF(mon->outbuf);
296 mon->outbuf = qstring_new();
297 return;
298 }
299 if (rc > 0) {
300 /* partinal write */
301 QString *tmp = qstring_from_str(buf + rc);
302 QDECREF(mon->outbuf);
303 mon->outbuf = tmp;
304 }
305 if (mon->watch == 0) {
306 mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
307 monitor_unblocked, mon);
308 }
309 }
310 }
311
312 /* flush at every end of line */
313 static void monitor_puts(Monitor *mon, const char *str)
314 {
315 char c;
316
317 for(;;) {
318 c = *str++;
319 if (c == '\0')
320 break;
321 if (c == '\n') {
322 qstring_append_chr(mon->outbuf, '\r');
323 }
324 qstring_append_chr(mon->outbuf, c);
325 if (c == '\n') {
326 monitor_flush(mon);
327 }
328 }
329 }
330
331 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
332 {
333 char *buf;
334
335 if (!mon)
336 return;
337
338 if (monitor_ctrl_mode(mon)) {
339 return;
340 }
341
342 buf = g_strdup_vprintf(fmt, ap);
343 monitor_puts(mon, buf);
344 g_free(buf);
345 }
346
347 void monitor_printf(Monitor *mon, const char *fmt, ...)
348 {
349 va_list ap;
350 va_start(ap, fmt);
351 monitor_vprintf(mon, fmt, ap);
352 va_end(ap);
353 }
354
355 void monitor_print_filename(Monitor *mon, const char *filename)
356 {
357 int i;
358
359 for (i = 0; filename[i]; i++) {
360 switch (filename[i]) {
361 case ' ':
362 case '"':
363 case '\\':
364 monitor_printf(mon, "\\%c", filename[i]);
365 break;
366 case '\t':
367 monitor_printf(mon, "\\t");
368 break;
369 case '\r':
370 monitor_printf(mon, "\\r");
371 break;
372 case '\n':
373 monitor_printf(mon, "\\n");
374 break;
375 default:
376 monitor_printf(mon, "%c", filename[i]);
377 break;
378 }
379 }
380 }
381
382 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
383 const char *fmt, ...)
384 {
385 va_list ap;
386 va_start(ap, fmt);
387 monitor_vprintf((Monitor *)stream, fmt, ap);
388 va_end(ap);
389 return 0;
390 }
391
392 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
393
394 static inline int handler_is_qobject(const mon_cmd_t *cmd)
395 {
396 return cmd->user_print != NULL;
397 }
398
399 static inline bool handler_is_async(const mon_cmd_t *cmd)
400 {
401 return cmd->flags & MONITOR_CMD_ASYNC;
402 }
403
404 static inline int monitor_has_error(const Monitor *mon)
405 {
406 return mon->error != NULL;
407 }
408
409 static void monitor_json_emitter(Monitor *mon, const QObject *data)
410 {
411 QString *json;
412
413 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
414 qobject_to_json(data);
415 assert(json != NULL);
416
417 qstring_append_chr(json, '\n');
418 monitor_puts(mon, qstring_get_str(json));
419
420 QDECREF(json);
421 }
422
423 static QDict *build_qmp_error_dict(const QError *err)
424 {
425 QObject *obj;
426
427 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
428 ErrorClass_lookup[err->err_class],
429 qerror_human(err));
430
431 return qobject_to_qdict(obj);
432 }
433
434 static void monitor_protocol_emitter(Monitor *mon, QObject *data)
435 {
436 QDict *qmp;
437
438 trace_monitor_protocol_emitter(mon);
439
440 if (!monitor_has_error(mon)) {
441 /* success response */
442 qmp = qdict_new();
443 if (data) {
444 qobject_incref(data);
445 qdict_put_obj(qmp, "return", data);
446 } else {
447 /* return an empty QDict by default */
448 qdict_put(qmp, "return", qdict_new());
449 }
450 } else {
451 /* error response */
452 qmp = build_qmp_error_dict(mon->error);
453 QDECREF(mon->error);
454 mon->error = NULL;
455 }
456
457 if (mon->mc->id) {
458 qdict_put_obj(qmp, "id", mon->mc->id);
459 mon->mc->id = NULL;
460 }
461
462 monitor_json_emitter(mon, QOBJECT(qmp));
463 QDECREF(qmp);
464 }
465
466 static void timestamp_put(QDict *qdict)
467 {
468 int err;
469 QObject *obj;
470 qemu_timeval tv;
471
472 err = qemu_gettimeofday(&tv);
473 if (err < 0)
474 return;
475
476 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
477 "'microseconds': %" PRId64 " }",
478 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
479 qdict_put_obj(qdict, "timestamp", obj);
480 }
481
482
483 static const char *monitor_event_names[] = {
484 [QEVENT_SHUTDOWN] = "SHUTDOWN",
485 [QEVENT_RESET] = "RESET",
486 [QEVENT_POWERDOWN] = "POWERDOWN",
487 [QEVENT_STOP] = "STOP",
488 [QEVENT_RESUME] = "RESUME",
489 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
490 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
491 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
492 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
493 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
494 [QEVENT_WATCHDOG] = "WATCHDOG",
495 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
496 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
497 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
498 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
499 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
500 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
501 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
502 [QEVENT_DEVICE_DELETED] = "DEVICE_DELETED",
503 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
504 [QEVENT_NIC_RX_FILTER_CHANGED] = "NIC_RX_FILTER_CHANGED",
505 [QEVENT_SUSPEND] = "SUSPEND",
506 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
507 [QEVENT_WAKEUP] = "WAKEUP",
508 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
509 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
510 [QEVENT_GUEST_PANICKED] = "GUEST_PANICKED",
511 [QEVENT_BLOCK_IMAGE_CORRUPTED] = "BLOCK_IMAGE_CORRUPTED",
512 [QEVENT_QUORUM_FAILURE] = "QUORUM_FAILURE",
513 [QEVENT_QUORUM_REPORT_BAD] = "QUORUM_REPORT_BAD",
514 };
515 QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
516
517 MonitorEventState monitor_event_state[QEVENT_MAX];
518
519 /*
520 * Emits the event to every monitor instance
521 */
522 static void
523 monitor_protocol_event_emit(MonitorEvent event,
524 QObject *data)
525 {
526 Monitor *mon;
527
528 trace_monitor_protocol_event_emit(event, data);
529 QLIST_FOREACH(mon, &mon_list, entry) {
530 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
531 monitor_json_emitter(mon, data);
532 }
533 }
534 }
535
536
537 /*
538 * Queue a new event for emission to Monitor instances,
539 * applying any rate limiting if required.
540 */
541 static void
542 monitor_protocol_event_queue(MonitorEvent event,
543 QObject *data)
544 {
545 MonitorEventState *evstate;
546 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
547 assert(event < QEVENT_MAX);
548
549 evstate = &(monitor_event_state[event]);
550 trace_monitor_protocol_event_queue(event,
551 data,
552 evstate->rate,
553 evstate->last,
554 now);
555
556 /* Rate limit of 0 indicates no throttling */
557 if (!evstate->rate) {
558 monitor_protocol_event_emit(event, data);
559 evstate->last = now;
560 } else {
561 int64_t delta = now - evstate->last;
562 if (evstate->data ||
563 delta < evstate->rate) {
564 /* If there's an existing event pending, replace
565 * it with the new event, otherwise schedule a
566 * timer for delayed emission
567 */
568 if (evstate->data) {
569 qobject_decref(evstate->data);
570 } else {
571 int64_t then = evstate->last + evstate->rate;
572 timer_mod_ns(evstate->timer, then);
573 }
574 evstate->data = data;
575 qobject_incref(evstate->data);
576 } else {
577 monitor_protocol_event_emit(event, data);
578 evstate->last = now;
579 }
580 }
581 }
582
583
584 /*
585 * The callback invoked by QemuTimer when a delayed
586 * event is ready to be emitted
587 */
588 static void monitor_protocol_event_handler(void *opaque)
589 {
590 MonitorEventState *evstate = opaque;
591 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
592
593
594 trace_monitor_protocol_event_handler(evstate->event,
595 evstate->data,
596 evstate->last,
597 now);
598 if (evstate->data) {
599 monitor_protocol_event_emit(evstate->event, evstate->data);
600 qobject_decref(evstate->data);
601 evstate->data = NULL;
602 }
603 evstate->last = now;
604 }
605
606
607 /*
608 * @event: the event ID to be limited
609 * @rate: the rate limit in milliseconds
610 *
611 * Sets a rate limit on a particular event, so no
612 * more than 1 event will be emitted within @rate
613 * milliseconds
614 */
615 static void
616 monitor_protocol_event_throttle(MonitorEvent event,
617 int64_t rate)
618 {
619 MonitorEventState *evstate;
620 assert(event < QEVENT_MAX);
621
622 evstate = &(monitor_event_state[event]);
623
624 trace_monitor_protocol_event_throttle(event, rate);
625 evstate->event = event;
626 evstate->rate = rate * SCALE_MS;
627 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
628 SCALE_MS,
629 monitor_protocol_event_handler,
630 evstate);
631 evstate->last = 0;
632 evstate->data = NULL;
633 }
634
635
636 /* Global, one-time initializer to configure the rate limiting
637 * and initialize state */
638 static void monitor_protocol_event_init(void)
639 {
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 /* limit the rate of quorum events to avoid hammering the management */
645 monitor_protocol_event_throttle(QEVENT_QUORUM_REPORT_BAD, 1000);
646 monitor_protocol_event_throttle(QEVENT_QUORUM_FAILURE, 1000);
647 }
648
649 /**
650 * monitor_protocol_event(): Generate a Monitor event
651 *
652 * Event-specific data can be emitted through the (optional) 'data' parameter.
653 */
654 void monitor_protocol_event(MonitorEvent event, QObject *data)
655 {
656 QDict *qmp;
657 const char *event_name;
658
659 assert(event < QEVENT_MAX);
660
661 event_name = monitor_event_names[event];
662 assert(event_name != NULL);
663
664 qmp = qdict_new();
665 timestamp_put(qmp);
666 qdict_put(qmp, "event", qstring_from_str(event_name));
667 if (data) {
668 qobject_incref(data);
669 qdict_put_obj(qmp, "data", data);
670 }
671
672 trace_monitor_protocol_event(event, event_name, qmp);
673 monitor_protocol_event_queue(event, QOBJECT(qmp));
674 QDECREF(qmp);
675 }
676
677 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
678 QObject **ret_data)
679 {
680 /* Will setup QMP capabilities in the future */
681 if (monitor_ctrl_mode(mon)) {
682 mon->mc->command_mode = 1;
683 }
684
685 return 0;
686 }
687
688 static void handle_user_command(Monitor *mon, const char *cmdline);
689
690 static void monitor_data_init(Monitor *mon)
691 {
692 memset(mon, 0, sizeof(Monitor));
693 mon->outbuf = qstring_new();
694 /* Use *mon_cmds by default. */
695 mon->cmd_table = mon_cmds;
696 }
697
698 static void monitor_data_destroy(Monitor *mon)
699 {
700 QDECREF(mon->outbuf);
701 }
702
703 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
704 int64_t cpu_index, Error **errp)
705 {
706 char *output = NULL;
707 Monitor *old_mon, hmp;
708
709 monitor_data_init(&hmp);
710 hmp.skip_flush = true;
711
712 old_mon = cur_mon;
713 cur_mon = &hmp;
714
715 if (has_cpu_index) {
716 int ret = monitor_set_cpu(cpu_index);
717 if (ret < 0) {
718 cur_mon = old_mon;
719 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
720 "a CPU number");
721 goto out;
722 }
723 }
724
725 handle_user_command(&hmp, command_line);
726 cur_mon = old_mon;
727
728 if (qstring_get_length(hmp.outbuf) > 0) {
729 output = g_strdup(qstring_get_str(hmp.outbuf));
730 } else {
731 output = g_strdup("");
732 }
733
734 out:
735 monitor_data_destroy(&hmp);
736 return output;
737 }
738
739 static int compare_cmd(const char *name, const char *list)
740 {
741 const char *p, *pstart;
742 int len;
743 len = strlen(name);
744 p = list;
745 for(;;) {
746 pstart = p;
747 p = strchr(p, '|');
748 if (!p)
749 p = pstart + strlen(pstart);
750 if ((p - pstart) == len && !memcmp(pstart, name, len))
751 return 1;
752 if (*p == '\0')
753 break;
754 p++;
755 }
756 return 0;
757 }
758
759 static int get_str(char *buf, int buf_size, const char **pp)
760 {
761 const char *p;
762 char *q;
763 int c;
764
765 q = buf;
766 p = *pp;
767 while (qemu_isspace(*p)) {
768 p++;
769 }
770 if (*p == '\0') {
771 fail:
772 *q = '\0';
773 *pp = p;
774 return -1;
775 }
776 if (*p == '\"') {
777 p++;
778 while (*p != '\0' && *p != '\"') {
779 if (*p == '\\') {
780 p++;
781 c = *p++;
782 switch (c) {
783 case 'n':
784 c = '\n';
785 break;
786 case 'r':
787 c = '\r';
788 break;
789 case '\\':
790 case '\'':
791 case '\"':
792 break;
793 default:
794 qemu_printf("unsupported escape code: '\\%c'\n", c);
795 goto fail;
796 }
797 if ((q - buf) < buf_size - 1) {
798 *q++ = c;
799 }
800 } else {
801 if ((q - buf) < buf_size - 1) {
802 *q++ = *p;
803 }
804 p++;
805 }
806 }
807 if (*p != '\"') {
808 qemu_printf("unterminated string\n");
809 goto fail;
810 }
811 p++;
812 } else {
813 while (*p != '\0' && !qemu_isspace(*p)) {
814 if ((q - buf) < buf_size - 1) {
815 *q++ = *p;
816 }
817 p++;
818 }
819 }
820 *q = '\0';
821 *pp = p;
822 return 0;
823 }
824
825 #define MAX_ARGS 16
826
827 static void free_cmdline_args(char **args, int nb_args)
828 {
829 int i;
830
831 assert(nb_args <= MAX_ARGS);
832
833 for (i = 0; i < nb_args; i++) {
834 g_free(args[i]);
835 }
836
837 }
838
839 /*
840 * Parse the command line to get valid args.
841 * @cmdline: command line to be parsed.
842 * @pnb_args: location to store the number of args, must NOT be NULL.
843 * @args: location to store the args, which should be freed by caller, must
844 * NOT be NULL.
845 *
846 * Returns 0 on success, negative on failure.
847 *
848 * NOTE: this parser is an approximate form of the real command parser. Number
849 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
850 * return with failure.
851 */
852 static int parse_cmdline(const char *cmdline,
853 int *pnb_args, char **args)
854 {
855 const char *p;
856 int nb_args, ret;
857 char buf[1024];
858
859 p = cmdline;
860 nb_args = 0;
861 for (;;) {
862 while (qemu_isspace(*p)) {
863 p++;
864 }
865 if (*p == '\0') {
866 break;
867 }
868 if (nb_args >= MAX_ARGS) {
869 goto fail;
870 }
871 ret = get_str(buf, sizeof(buf), &p);
872 if (ret < 0) {
873 goto fail;
874 }
875 args[nb_args] = g_strdup(buf);
876 nb_args++;
877 }
878 *pnb_args = nb_args;
879 return 0;
880
881 fail:
882 free_cmdline_args(args, nb_args);
883 return -1;
884 }
885
886 static void help_cmd_dump_one(Monitor *mon,
887 const mon_cmd_t *cmd,
888 char **prefix_args,
889 int prefix_args_nb)
890 {
891 int i;
892
893 for (i = 0; i < prefix_args_nb; i++) {
894 monitor_printf(mon, "%s ", prefix_args[i]);
895 }
896 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
897 }
898
899 /* @args[@arg_index] is the valid command need to find in @cmds */
900 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
901 char **args, int nb_args, int arg_index)
902 {
903 const mon_cmd_t *cmd;
904
905 /* No valid arg need to compare with, dump all in *cmds */
906 if (arg_index >= nb_args) {
907 for (cmd = cmds; cmd->name != NULL; cmd++) {
908 help_cmd_dump_one(mon, cmd, args, arg_index);
909 }
910 return;
911 }
912
913 /* Find one entry to dump */
914 for (cmd = cmds; cmd->name != NULL; cmd++) {
915 if (compare_cmd(args[arg_index], cmd->name)) {
916 if (cmd->sub_table) {
917 /* continue with next arg */
918 help_cmd_dump(mon, cmd->sub_table,
919 args, nb_args, arg_index + 1);
920 } else {
921 help_cmd_dump_one(mon, cmd, args, arg_index);
922 }
923 break;
924 }
925 }
926 }
927
928 static void help_cmd(Monitor *mon, const char *name)
929 {
930 char *args[MAX_ARGS];
931 int nb_args = 0;
932
933 /* 1. parse user input */
934 if (name) {
935 /* special case for log, directly dump and return */
936 if (!strcmp(name, "log")) {
937 const QEMULogItem *item;
938 monitor_printf(mon, "Log items (comma separated):\n");
939 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
940 for (item = qemu_log_items; item->mask != 0; item++) {
941 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
942 }
943 return;
944 }
945
946 if (parse_cmdline(name, &nb_args, args) < 0) {
947 return;
948 }
949 }
950
951 /* 2. dump the contents according to parsed args */
952 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
953
954 free_cmdline_args(args, nb_args);
955 }
956
957 static void do_help_cmd(Monitor *mon, const QDict *qdict)
958 {
959 help_cmd(mon, qdict_get_try_str(qdict, "name"));
960 }
961
962 static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
963 {
964 const char *tp_name = qdict_get_str(qdict, "name");
965 bool new_state = qdict_get_bool(qdict, "option");
966
967 bool found = false;
968 TraceEvent *ev = NULL;
969 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
970 found = true;
971 if (!trace_event_get_state_static(ev)) {
972 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
973 } else {
974 trace_event_set_state_dynamic(ev, new_state);
975 }
976 }
977 if (!trace_event_is_pattern(tp_name) && !found) {
978 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
979 }
980 }
981
982 #ifdef CONFIG_TRACE_SIMPLE
983 static void do_trace_file(Monitor *mon, const QDict *qdict)
984 {
985 const char *op = qdict_get_try_str(qdict, "op");
986 const char *arg = qdict_get_try_str(qdict, "arg");
987
988 if (!op) {
989 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
990 } else if (!strcmp(op, "on")) {
991 st_set_trace_file_enabled(true);
992 } else if (!strcmp(op, "off")) {
993 st_set_trace_file_enabled(false);
994 } else if (!strcmp(op, "flush")) {
995 st_flush_trace_buffer();
996 } else if (!strcmp(op, "set")) {
997 if (arg) {
998 st_set_trace_file(arg);
999 }
1000 } else {
1001 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
1002 help_cmd(mon, "trace-file");
1003 }
1004 }
1005 #endif
1006
1007 static void user_monitor_complete(void *opaque, QObject *ret_data)
1008 {
1009 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
1010
1011 if (ret_data) {
1012 data->user_print(data->mon, ret_data);
1013 }
1014 monitor_resume(data->mon);
1015 g_free(data);
1016 }
1017
1018 static void qmp_monitor_complete(void *opaque, QObject *ret_data)
1019 {
1020 monitor_protocol_emitter(opaque, ret_data);
1021 }
1022
1023 static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1024 const QDict *params)
1025 {
1026 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
1027 }
1028
1029 static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
1030 const QDict *params)
1031 {
1032 int ret;
1033
1034 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
1035 cb_data->mon = mon;
1036 cb_data->user_print = cmd->user_print;
1037 monitor_suspend(mon);
1038 ret = cmd->mhandler.cmd_async(mon, params,
1039 user_monitor_complete, cb_data);
1040 if (ret < 0) {
1041 monitor_resume(mon);
1042 g_free(cb_data);
1043 }
1044 }
1045
1046 static void do_info_help(Monitor *mon, const QDict *qdict)
1047 {
1048 help_cmd(mon, "info");
1049 }
1050
1051 CommandInfoList *qmp_query_commands(Error **errp)
1052 {
1053 CommandInfoList *info, *cmd_list = NULL;
1054 const mon_cmd_t *cmd;
1055
1056 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
1057 info = g_malloc0(sizeof(*info));
1058 info->value = g_malloc0(sizeof(*info->value));
1059 info->value->name = g_strdup(cmd->name);
1060
1061 info->next = cmd_list;
1062 cmd_list = info;
1063 }
1064
1065 return cmd_list;
1066 }
1067
1068 EventInfoList *qmp_query_events(Error **errp)
1069 {
1070 EventInfoList *info, *ev_list = NULL;
1071 MonitorEvent e;
1072
1073 for (e = 0 ; e < QEVENT_MAX ; e++) {
1074 const char *event_name = monitor_event_names[e];
1075 assert(event_name != NULL);
1076 info = g_malloc0(sizeof(*info));
1077 info->value = g_malloc0(sizeof(*info->value));
1078 info->value->name = g_strdup(event_name);
1079
1080 info->next = ev_list;
1081 ev_list = info;
1082 }
1083
1084 return ev_list;
1085 }
1086
1087 /* set the current CPU defined by the user */
1088 int monitor_set_cpu(int cpu_index)
1089 {
1090 CPUState *cpu;
1091
1092 cpu = qemu_get_cpu(cpu_index);
1093 if (cpu == NULL) {
1094 return -1;
1095 }
1096 cur_mon->mon_cpu = cpu;
1097 return 0;
1098 }
1099
1100 static CPUArchState *mon_get_cpu(void)
1101 {
1102 if (!cur_mon->mon_cpu) {
1103 monitor_set_cpu(0);
1104 }
1105 cpu_synchronize_state(cur_mon->mon_cpu);
1106 return cur_mon->mon_cpu->env_ptr;
1107 }
1108
1109 int monitor_get_cpu_index(void)
1110 {
1111 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1112 return cpu->cpu_index;
1113 }
1114
1115 static void do_info_registers(Monitor *mon, const QDict *qdict)
1116 {
1117 CPUState *cpu;
1118 CPUArchState *env;
1119 env = mon_get_cpu();
1120 cpu = ENV_GET_CPU(env);
1121 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1122 }
1123
1124 static void do_info_jit(Monitor *mon, const QDict *qdict)
1125 {
1126 dump_exec_info((FILE *)mon, monitor_fprintf);
1127 }
1128
1129 static void do_info_history(Monitor *mon, const QDict *qdict)
1130 {
1131 int i;
1132 const char *str;
1133
1134 if (!mon->rs)
1135 return;
1136 i = 0;
1137 for(;;) {
1138 str = readline_get_history(mon->rs, i);
1139 if (!str)
1140 break;
1141 monitor_printf(mon, "%d: '%s'\n", i, str);
1142 i++;
1143 }
1144 }
1145
1146 static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
1147 {
1148 CPUState *cpu;
1149 CPUArchState *env;
1150
1151 env = mon_get_cpu();
1152 cpu = ENV_GET_CPU(env);
1153 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
1154 }
1155
1156 static void do_trace_print_events(Monitor *mon, const QDict *qdict)
1157 {
1158 trace_print_events((FILE *)mon, &monitor_fprintf);
1159 }
1160
1161 static int client_migrate_info(Monitor *mon, const QDict *qdict,
1162 MonitorCompletion cb, void *opaque)
1163 {
1164 const char *protocol = qdict_get_str(qdict, "protocol");
1165 const char *hostname = qdict_get_str(qdict, "hostname");
1166 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1167 int port = qdict_get_try_int(qdict, "port", -1);
1168 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1169 int ret;
1170
1171 if (strcmp(protocol, "spice") == 0) {
1172 if (!using_spice) {
1173 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1174 return -1;
1175 }
1176
1177 if (port == -1 && tls_port == -1) {
1178 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1179 return -1;
1180 }
1181
1182 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1183 cb, opaque);
1184 if (ret != 0) {
1185 qerror_report(QERR_UNDEFINED_ERROR);
1186 return -1;
1187 }
1188 return 0;
1189 }
1190
1191 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1192 return -1;
1193 }
1194
1195 static void do_logfile(Monitor *mon, const QDict *qdict)
1196 {
1197 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1198 }
1199
1200 static void do_log(Monitor *mon, const QDict *qdict)
1201 {
1202 int mask;
1203 const char *items = qdict_get_str(qdict, "items");
1204
1205 if (!strcmp(items, "none")) {
1206 mask = 0;
1207 } else {
1208 mask = qemu_str_to_log_mask(items);
1209 if (!mask) {
1210 help_cmd(mon, "log");
1211 return;
1212 }
1213 }
1214 qemu_set_log(mask);
1215 }
1216
1217 static void do_singlestep(Monitor *mon, const QDict *qdict)
1218 {
1219 const char *option = qdict_get_try_str(qdict, "option");
1220 if (!option || !strcmp(option, "on")) {
1221 singlestep = 1;
1222 } else if (!strcmp(option, "off")) {
1223 singlestep = 0;
1224 } else {
1225 monitor_printf(mon, "unexpected option %s\n", option);
1226 }
1227 }
1228
1229 static void do_gdbserver(Monitor *mon, const QDict *qdict)
1230 {
1231 const char *device = qdict_get_try_str(qdict, "device");
1232 if (!device)
1233 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1234 if (gdbserver_start(device) < 0) {
1235 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1236 device);
1237 } else if (strcmp(device, "none") == 0) {
1238 monitor_printf(mon, "Disabled gdbserver\n");
1239 } else {
1240 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1241 device);
1242 }
1243 }
1244
1245 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
1246 {
1247 const char *action = qdict_get_str(qdict, "action");
1248 if (select_watchdog_action(action) == -1) {
1249 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1250 }
1251 }
1252
1253 static void monitor_printc(Monitor *mon, int c)
1254 {
1255 monitor_printf(mon, "'");
1256 switch(c) {
1257 case '\'':
1258 monitor_printf(mon, "\\'");
1259 break;
1260 case '\\':
1261 monitor_printf(mon, "\\\\");
1262 break;
1263 case '\n':
1264 monitor_printf(mon, "\\n");
1265 break;
1266 case '\r':
1267 monitor_printf(mon, "\\r");
1268 break;
1269 default:
1270 if (c >= 32 && c <= 126) {
1271 monitor_printf(mon, "%c", c);
1272 } else {
1273 monitor_printf(mon, "\\x%02x", c);
1274 }
1275 break;
1276 }
1277 monitor_printf(mon, "'");
1278 }
1279
1280 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1281 hwaddr addr, int is_physical)
1282 {
1283 CPUArchState *env;
1284 int l, line_size, i, max_digits, len;
1285 uint8_t buf[16];
1286 uint64_t v;
1287
1288 if (format == 'i') {
1289 int flags;
1290 flags = 0;
1291 env = mon_get_cpu();
1292 #ifdef TARGET_I386
1293 if (wsize == 2) {
1294 flags = 1;
1295 } else if (wsize == 4) {
1296 flags = 0;
1297 } else {
1298 /* as default we use the current CS size */
1299 flags = 0;
1300 if (env) {
1301 #ifdef TARGET_X86_64
1302 if ((env->efer & MSR_EFER_LMA) &&
1303 (env->segs[R_CS].flags & DESC_L_MASK))
1304 flags = 2;
1305 else
1306 #endif
1307 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1308 flags = 1;
1309 }
1310 }
1311 #endif
1312 monitor_disas(mon, env, addr, count, is_physical, flags);
1313 return;
1314 }
1315
1316 len = wsize * count;
1317 if (wsize == 1)
1318 line_size = 8;
1319 else
1320 line_size = 16;
1321 max_digits = 0;
1322
1323 switch(format) {
1324 case 'o':
1325 max_digits = (wsize * 8 + 2) / 3;
1326 break;
1327 default:
1328 case 'x':
1329 max_digits = (wsize * 8) / 4;
1330 break;
1331 case 'u':
1332 case 'd':
1333 max_digits = (wsize * 8 * 10 + 32) / 33;
1334 break;
1335 case 'c':
1336 wsize = 1;
1337 break;
1338 }
1339
1340 while (len > 0) {
1341 if (is_physical)
1342 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1343 else
1344 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1345 l = len;
1346 if (l > line_size)
1347 l = line_size;
1348 if (is_physical) {
1349 cpu_physical_memory_read(addr, buf, l);
1350 } else {
1351 env = mon_get_cpu();
1352 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
1353 monitor_printf(mon, " Cannot access memory\n");
1354 break;
1355 }
1356 }
1357 i = 0;
1358 while (i < l) {
1359 switch(wsize) {
1360 default:
1361 case 1:
1362 v = ldub_raw(buf + i);
1363 break;
1364 case 2:
1365 v = lduw_raw(buf + i);
1366 break;
1367 case 4:
1368 v = (uint32_t)ldl_raw(buf + i);
1369 break;
1370 case 8:
1371 v = ldq_raw(buf + i);
1372 break;
1373 }
1374 monitor_printf(mon, " ");
1375 switch(format) {
1376 case 'o':
1377 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1378 break;
1379 case 'x':
1380 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1381 break;
1382 case 'u':
1383 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1384 break;
1385 case 'd':
1386 monitor_printf(mon, "%*" PRId64, max_digits, v);
1387 break;
1388 case 'c':
1389 monitor_printc(mon, v);
1390 break;
1391 }
1392 i += wsize;
1393 }
1394 monitor_printf(mon, "\n");
1395 addr += l;
1396 len -= l;
1397 }
1398 }
1399
1400 static void do_memory_dump(Monitor *mon, const QDict *qdict)
1401 {
1402 int count = qdict_get_int(qdict, "count");
1403 int format = qdict_get_int(qdict, "format");
1404 int size = qdict_get_int(qdict, "size");
1405 target_long addr = qdict_get_int(qdict, "addr");
1406
1407 memory_dump(mon, count, format, size, addr, 0);
1408 }
1409
1410 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1411 {
1412 int count = qdict_get_int(qdict, "count");
1413 int format = qdict_get_int(qdict, "format");
1414 int size = qdict_get_int(qdict, "size");
1415 hwaddr addr = qdict_get_int(qdict, "addr");
1416
1417 memory_dump(mon, count, format, size, addr, 1);
1418 }
1419
1420 static void do_print(Monitor *mon, const QDict *qdict)
1421 {
1422 int format = qdict_get_int(qdict, "format");
1423 hwaddr val = qdict_get_int(qdict, "val");
1424
1425 switch(format) {
1426 case 'o':
1427 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1428 break;
1429 case 'x':
1430 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1431 break;
1432 case 'u':
1433 monitor_printf(mon, "%" HWADDR_PRIu, val);
1434 break;
1435 default:
1436 case 'd':
1437 monitor_printf(mon, "%" HWADDR_PRId, val);
1438 break;
1439 case 'c':
1440 monitor_printc(mon, val);
1441 break;
1442 }
1443 monitor_printf(mon, "\n");
1444 }
1445
1446 static void do_sum(Monitor *mon, const QDict *qdict)
1447 {
1448 uint32_t addr;
1449 uint16_t sum;
1450 uint32_t start = qdict_get_int(qdict, "start");
1451 uint32_t size = qdict_get_int(qdict, "size");
1452
1453 sum = 0;
1454 for(addr = start; addr < (start + size); addr++) {
1455 uint8_t val = ldub_phys(&address_space_memory, addr);
1456 /* BSD sum algorithm ('sum' Unix command) */
1457 sum = (sum >> 1) | (sum << 15);
1458 sum += val;
1459 }
1460 monitor_printf(mon, "%05d\n", sum);
1461 }
1462
1463 static int mouse_button_state;
1464
1465 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1466 {
1467 int dx, dy, dz, button;
1468 const char *dx_str = qdict_get_str(qdict, "dx_str");
1469 const char *dy_str = qdict_get_str(qdict, "dy_str");
1470 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1471
1472 dx = strtol(dx_str, NULL, 0);
1473 dy = strtol(dy_str, NULL, 0);
1474 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1475 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1476
1477 if (dz_str) {
1478 dz = strtol(dz_str, NULL, 0);
1479 if (dz != 0) {
1480 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1481 qemu_input_queue_btn(NULL, button, true);
1482 qemu_input_event_sync();
1483 qemu_input_queue_btn(NULL, button, false);
1484 }
1485 }
1486 qemu_input_event_sync();
1487 }
1488
1489 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1490 {
1491 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1492 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1493 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1494 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1495 };
1496 int button_state = qdict_get_int(qdict, "button_state");
1497
1498 if (mouse_button_state == button_state) {
1499 return;
1500 }
1501 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1502 qemu_input_event_sync();
1503 mouse_button_state = button_state;
1504 }
1505
1506 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1507 {
1508 int size = qdict_get_int(qdict, "size");
1509 int addr = qdict_get_int(qdict, "addr");
1510 int has_index = qdict_haskey(qdict, "index");
1511 uint32_t val;
1512 int suffix;
1513
1514 if (has_index) {
1515 int index = qdict_get_int(qdict, "index");
1516 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1517 addr++;
1518 }
1519 addr &= 0xffff;
1520
1521 switch(size) {
1522 default:
1523 case 1:
1524 val = cpu_inb(addr);
1525 suffix = 'b';
1526 break;
1527 case 2:
1528 val = cpu_inw(addr);
1529 suffix = 'w';
1530 break;
1531 case 4:
1532 val = cpu_inl(addr);
1533 suffix = 'l';
1534 break;
1535 }
1536 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1537 suffix, addr, size * 2, val);
1538 }
1539
1540 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1541 {
1542 int size = qdict_get_int(qdict, "size");
1543 int addr = qdict_get_int(qdict, "addr");
1544 int val = qdict_get_int(qdict, "val");
1545
1546 addr &= IOPORTS_MASK;
1547
1548 switch (size) {
1549 default:
1550 case 1:
1551 cpu_outb(addr, val);
1552 break;
1553 case 2:
1554 cpu_outw(addr, val);
1555 break;
1556 case 4:
1557 cpu_outl(addr, val);
1558 break;
1559 }
1560 }
1561
1562 static void do_boot_set(Monitor *mon, const QDict *qdict)
1563 {
1564 int res;
1565 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1566
1567 res = qemu_boot_set(bootdevice);
1568 if (res == 0) {
1569 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1570 } else if (res > 0) {
1571 monitor_printf(mon, "setting boot device list failed\n");
1572 } else {
1573 monitor_printf(mon, "no function defined to set boot device list for "
1574 "this architecture\n");
1575 }
1576 }
1577
1578 #if defined(TARGET_I386)
1579 static void print_pte(Monitor *mon, hwaddr addr,
1580 hwaddr pte,
1581 hwaddr mask)
1582 {
1583 #ifdef TARGET_X86_64
1584 if (addr & (1ULL << 47)) {
1585 addr |= -1LL << 48;
1586 }
1587 #endif
1588 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1589 " %c%c%c%c%c%c%c%c%c\n",
1590 addr,
1591 pte & mask,
1592 pte & PG_NX_MASK ? 'X' : '-',
1593 pte & PG_GLOBAL_MASK ? 'G' : '-',
1594 pte & PG_PSE_MASK ? 'P' : '-',
1595 pte & PG_DIRTY_MASK ? 'D' : '-',
1596 pte & PG_ACCESSED_MASK ? 'A' : '-',
1597 pte & PG_PCD_MASK ? 'C' : '-',
1598 pte & PG_PWT_MASK ? 'T' : '-',
1599 pte & PG_USER_MASK ? 'U' : '-',
1600 pte & PG_RW_MASK ? 'W' : '-');
1601 }
1602
1603 static void tlb_info_32(Monitor *mon, CPUArchState *env)
1604 {
1605 unsigned int l1, l2;
1606 uint32_t pgd, pde, pte;
1607
1608 pgd = env->cr[3] & ~0xfff;
1609 for(l1 = 0; l1 < 1024; l1++) {
1610 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1611 pde = le32_to_cpu(pde);
1612 if (pde & PG_PRESENT_MASK) {
1613 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1614 /* 4M pages */
1615 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1616 } else {
1617 for(l2 = 0; l2 < 1024; l2++) {
1618 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1619 pte = le32_to_cpu(pte);
1620 if (pte & PG_PRESENT_MASK) {
1621 print_pte(mon, (l1 << 22) + (l2 << 12),
1622 pte & ~PG_PSE_MASK,
1623 ~0xfff);
1624 }
1625 }
1626 }
1627 }
1628 }
1629 }
1630
1631 static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
1632 {
1633 unsigned int l1, l2, l3;
1634 uint64_t pdpe, pde, pte;
1635 uint64_t pdp_addr, pd_addr, pt_addr;
1636
1637 pdp_addr = env->cr[3] & ~0x1f;
1638 for (l1 = 0; l1 < 4; l1++) {
1639 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1640 pdpe = le64_to_cpu(pdpe);
1641 if (pdpe & PG_PRESENT_MASK) {
1642 pd_addr = pdpe & 0x3fffffffff000ULL;
1643 for (l2 = 0; l2 < 512; l2++) {
1644 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1645 pde = le64_to_cpu(pde);
1646 if (pde & PG_PRESENT_MASK) {
1647 if (pde & PG_PSE_MASK) {
1648 /* 2M pages with PAE, CR4.PSE is ignored */
1649 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1650 ~((hwaddr)(1 << 20) - 1));
1651 } else {
1652 pt_addr = pde & 0x3fffffffff000ULL;
1653 for (l3 = 0; l3 < 512; l3++) {
1654 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1655 pte = le64_to_cpu(pte);
1656 if (pte & PG_PRESENT_MASK) {
1657 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1658 + (l3 << 12),
1659 pte & ~PG_PSE_MASK,
1660 ~(hwaddr)0xfff);
1661 }
1662 }
1663 }
1664 }
1665 }
1666 }
1667 }
1668 }
1669
1670 #ifdef TARGET_X86_64
1671 static void tlb_info_64(Monitor *mon, CPUArchState *env)
1672 {
1673 uint64_t l1, l2, l3, l4;
1674 uint64_t pml4e, pdpe, pde, pte;
1675 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1676
1677 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1678 for (l1 = 0; l1 < 512; l1++) {
1679 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1680 pml4e = le64_to_cpu(pml4e);
1681 if (pml4e & PG_PRESENT_MASK) {
1682 pdp_addr = pml4e & 0x3fffffffff000ULL;
1683 for (l2 = 0; l2 < 512; l2++) {
1684 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1685 pdpe = le64_to_cpu(pdpe);
1686 if (pdpe & PG_PRESENT_MASK) {
1687 if (pdpe & PG_PSE_MASK) {
1688 /* 1G pages, CR4.PSE is ignored */
1689 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1690 0x3ffffc0000000ULL);
1691 } else {
1692 pd_addr = pdpe & 0x3fffffffff000ULL;
1693 for (l3 = 0; l3 < 512; l3++) {
1694 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1695 pde = le64_to_cpu(pde);
1696 if (pde & PG_PRESENT_MASK) {
1697 if (pde & PG_PSE_MASK) {
1698 /* 2M pages, CR4.PSE is ignored */
1699 print_pte(mon, (l1 << 39) + (l2 << 30) +
1700 (l3 << 21), pde,
1701 0x3ffffffe00000ULL);
1702 } else {
1703 pt_addr = pde & 0x3fffffffff000ULL;
1704 for (l4 = 0; l4 < 512; l4++) {
1705 cpu_physical_memory_read(pt_addr
1706 + l4 * 8,
1707 &pte, 8);
1708 pte = le64_to_cpu(pte);
1709 if (pte & PG_PRESENT_MASK) {
1710 print_pte(mon, (l1 << 39) +
1711 (l2 << 30) +
1712 (l3 << 21) + (l4 << 12),
1713 pte & ~PG_PSE_MASK,
1714 0x3fffffffff000ULL);
1715 }
1716 }
1717 }
1718 }
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726 #endif
1727
1728 static void tlb_info(Monitor *mon, const QDict *qdict)
1729 {
1730 CPUArchState *env;
1731
1732 env = mon_get_cpu();
1733
1734 if (!(env->cr[0] & CR0_PG_MASK)) {
1735 monitor_printf(mon, "PG disabled\n");
1736 return;
1737 }
1738 if (env->cr[4] & CR4_PAE_MASK) {
1739 #ifdef TARGET_X86_64
1740 if (env->hflags & HF_LMA_MASK) {
1741 tlb_info_64(mon, env);
1742 } else
1743 #endif
1744 {
1745 tlb_info_pae32(mon, env);
1746 }
1747 } else {
1748 tlb_info_32(mon, env);
1749 }
1750 }
1751
1752 static void mem_print(Monitor *mon, hwaddr *pstart,
1753 int *plast_prot,
1754 hwaddr end, int prot)
1755 {
1756 int prot1;
1757 prot1 = *plast_prot;
1758 if (prot != prot1) {
1759 if (*pstart != -1) {
1760 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1761 TARGET_FMT_plx " %c%c%c\n",
1762 *pstart, end, end - *pstart,
1763 prot1 & PG_USER_MASK ? 'u' : '-',
1764 'r',
1765 prot1 & PG_RW_MASK ? 'w' : '-');
1766 }
1767 if (prot != 0)
1768 *pstart = end;
1769 else
1770 *pstart = -1;
1771 *plast_prot = prot;
1772 }
1773 }
1774
1775 static void mem_info_32(Monitor *mon, CPUArchState *env)
1776 {
1777 unsigned int l1, l2;
1778 int prot, last_prot;
1779 uint32_t pgd, pde, pte;
1780 hwaddr start, end;
1781
1782 pgd = env->cr[3] & ~0xfff;
1783 last_prot = 0;
1784 start = -1;
1785 for(l1 = 0; l1 < 1024; l1++) {
1786 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1787 pde = le32_to_cpu(pde);
1788 end = l1 << 22;
1789 if (pde & PG_PRESENT_MASK) {
1790 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1791 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1792 mem_print(mon, &start, &last_prot, end, prot);
1793 } else {
1794 for(l2 = 0; l2 < 1024; l2++) {
1795 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1796 pte = le32_to_cpu(pte);
1797 end = (l1 << 22) + (l2 << 12);
1798 if (pte & PG_PRESENT_MASK) {
1799 prot = pte & pde &
1800 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1801 } else {
1802 prot = 0;
1803 }
1804 mem_print(mon, &start, &last_prot, end, prot);
1805 }
1806 }
1807 } else {
1808 prot = 0;
1809 mem_print(mon, &start, &last_prot, end, prot);
1810 }
1811 }
1812 /* Flush last range */
1813 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1814 }
1815
1816 static void mem_info_pae32(Monitor *mon, CPUArchState *env)
1817 {
1818 unsigned int l1, l2, l3;
1819 int prot, last_prot;
1820 uint64_t pdpe, pde, pte;
1821 uint64_t pdp_addr, pd_addr, pt_addr;
1822 hwaddr start, end;
1823
1824 pdp_addr = env->cr[3] & ~0x1f;
1825 last_prot = 0;
1826 start = -1;
1827 for (l1 = 0; l1 < 4; l1++) {
1828 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1829 pdpe = le64_to_cpu(pdpe);
1830 end = l1 << 30;
1831 if (pdpe & PG_PRESENT_MASK) {
1832 pd_addr = pdpe & 0x3fffffffff000ULL;
1833 for (l2 = 0; l2 < 512; l2++) {
1834 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1835 pde = le64_to_cpu(pde);
1836 end = (l1 << 30) + (l2 << 21);
1837 if (pde & PG_PRESENT_MASK) {
1838 if (pde & PG_PSE_MASK) {
1839 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1840 PG_PRESENT_MASK);
1841 mem_print(mon, &start, &last_prot, end, prot);
1842 } else {
1843 pt_addr = pde & 0x3fffffffff000ULL;
1844 for (l3 = 0; l3 < 512; l3++) {
1845 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1846 pte = le64_to_cpu(pte);
1847 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1848 if (pte & PG_PRESENT_MASK) {
1849 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1850 PG_PRESENT_MASK);
1851 } else {
1852 prot = 0;
1853 }
1854 mem_print(mon, &start, &last_prot, end, prot);
1855 }
1856 }
1857 } else {
1858 prot = 0;
1859 mem_print(mon, &start, &last_prot, end, prot);
1860 }
1861 }
1862 } else {
1863 prot = 0;
1864 mem_print(mon, &start, &last_prot, end, prot);
1865 }
1866 }
1867 /* Flush last range */
1868 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1869 }
1870
1871
1872 #ifdef TARGET_X86_64
1873 static void mem_info_64(Monitor *mon, CPUArchState *env)
1874 {
1875 int prot, last_prot;
1876 uint64_t l1, l2, l3, l4;
1877 uint64_t pml4e, pdpe, pde, pte;
1878 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1879
1880 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1881 last_prot = 0;
1882 start = -1;
1883 for (l1 = 0; l1 < 512; l1++) {
1884 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1885 pml4e = le64_to_cpu(pml4e);
1886 end = l1 << 39;
1887 if (pml4e & PG_PRESENT_MASK) {
1888 pdp_addr = pml4e & 0x3fffffffff000ULL;
1889 for (l2 = 0; l2 < 512; l2++) {
1890 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1891 pdpe = le64_to_cpu(pdpe);
1892 end = (l1 << 39) + (l2 << 30);
1893 if (pdpe & PG_PRESENT_MASK) {
1894 if (pdpe & PG_PSE_MASK) {
1895 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1896 PG_PRESENT_MASK);
1897 prot &= pml4e;
1898 mem_print(mon, &start, &last_prot, end, prot);
1899 } else {
1900 pd_addr = pdpe & 0x3fffffffff000ULL;
1901 for (l3 = 0; l3 < 512; l3++) {
1902 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1903 pde = le64_to_cpu(pde);
1904 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1905 if (pde & PG_PRESENT_MASK) {
1906 if (pde & PG_PSE_MASK) {
1907 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1908 PG_PRESENT_MASK);
1909 prot &= pml4e & pdpe;
1910 mem_print(mon, &start, &last_prot, end, prot);
1911 } else {
1912 pt_addr = pde & 0x3fffffffff000ULL;
1913 for (l4 = 0; l4 < 512; l4++) {
1914 cpu_physical_memory_read(pt_addr
1915 + l4 * 8,
1916 &pte, 8);
1917 pte = le64_to_cpu(pte);
1918 end = (l1 << 39) + (l2 << 30) +
1919 (l3 << 21) + (l4 << 12);
1920 if (pte & PG_PRESENT_MASK) {
1921 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1922 PG_PRESENT_MASK);
1923 prot &= pml4e & pdpe & pde;
1924 } else {
1925 prot = 0;
1926 }
1927 mem_print(mon, &start, &last_prot, end, prot);
1928 }
1929 }
1930 } else {
1931 prot = 0;
1932 mem_print(mon, &start, &last_prot, end, prot);
1933 }
1934 }
1935 }
1936 } else {
1937 prot = 0;
1938 mem_print(mon, &start, &last_prot, end, prot);
1939 }
1940 }
1941 } else {
1942 prot = 0;
1943 mem_print(mon, &start, &last_prot, end, prot);
1944 }
1945 }
1946 /* Flush last range */
1947 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
1948 }
1949 #endif
1950
1951 static void mem_info(Monitor *mon, const QDict *qdict)
1952 {
1953 CPUArchState *env;
1954
1955 env = mon_get_cpu();
1956
1957 if (!(env->cr[0] & CR0_PG_MASK)) {
1958 monitor_printf(mon, "PG disabled\n");
1959 return;
1960 }
1961 if (env->cr[4] & CR4_PAE_MASK) {
1962 #ifdef TARGET_X86_64
1963 if (env->hflags & HF_LMA_MASK) {
1964 mem_info_64(mon, env);
1965 } else
1966 #endif
1967 {
1968 mem_info_pae32(mon, env);
1969 }
1970 } else {
1971 mem_info_32(mon, env);
1972 }
1973 }
1974 #endif
1975
1976 #if defined(TARGET_SH4)
1977
1978 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1979 {
1980 monitor_printf(mon, " tlb%i:\t"
1981 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1982 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1983 "dirty=%hhu writethrough=%hhu\n",
1984 idx,
1985 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1986 tlb->v, tlb->sh, tlb->c, tlb->pr,
1987 tlb->d, tlb->wt);
1988 }
1989
1990 static void tlb_info(Monitor *mon, const QDict *qdict)
1991 {
1992 CPUArchState *env = mon_get_cpu();
1993 int i;
1994
1995 monitor_printf (mon, "ITLB:\n");
1996 for (i = 0 ; i < ITLB_SIZE ; i++)
1997 print_tlb (mon, i, &env->itlb[i]);
1998 monitor_printf (mon, "UTLB:\n");
1999 for (i = 0 ; i < UTLB_SIZE ; i++)
2000 print_tlb (mon, i, &env->utlb[i]);
2001 }
2002
2003 #endif
2004
2005 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
2006 static void tlb_info(Monitor *mon, const QDict *qdict)
2007 {
2008 CPUArchState *env1 = mon_get_cpu();
2009
2010 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2011 }
2012 #endif
2013
2014 static void do_info_mtree(Monitor *mon, const QDict *qdict)
2015 {
2016 mtree_info((fprintf_function)monitor_printf, mon);
2017 }
2018
2019 static void do_info_numa(Monitor *mon, const QDict *qdict)
2020 {
2021 int i;
2022 CPUState *cpu;
2023
2024 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2025 for (i = 0; i < nb_numa_nodes; i++) {
2026 monitor_printf(mon, "node %d cpus:", i);
2027 CPU_FOREACH(cpu) {
2028 if (cpu->numa_node == i) {
2029 monitor_printf(mon, " %d", cpu->cpu_index);
2030 }
2031 }
2032 monitor_printf(mon, "\n");
2033 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2034 node_mem[i] >> 20);
2035 }
2036 }
2037
2038 #ifdef CONFIG_PROFILER
2039
2040 int64_t qemu_time;
2041 int64_t dev_time;
2042
2043 static void do_info_profile(Monitor *mon, const QDict *qdict)
2044 {
2045 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
2046 dev_time, dev_time / (double)get_ticks_per_sec());
2047 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
2048 qemu_time, qemu_time / (double)get_ticks_per_sec());
2049 qemu_time = 0;
2050 dev_time = 0;
2051 }
2052 #else
2053 static void do_info_profile(Monitor *mon, const QDict *qdict)
2054 {
2055 monitor_printf(mon, "Internal profiler not compiled\n");
2056 }
2057 #endif
2058
2059 /* Capture support */
2060 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
2061
2062 static void do_info_capture(Monitor *mon, const QDict *qdict)
2063 {
2064 int i;
2065 CaptureState *s;
2066
2067 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2068 monitor_printf(mon, "[%d]: ", i);
2069 s->ops.info (s->opaque);
2070 }
2071 }
2072
2073 static void do_stop_capture(Monitor *mon, const QDict *qdict)
2074 {
2075 int i;
2076 int n = qdict_get_int(qdict, "n");
2077 CaptureState *s;
2078
2079 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2080 if (i == n) {
2081 s->ops.destroy (s->opaque);
2082 QLIST_REMOVE (s, entries);
2083 g_free (s);
2084 return;
2085 }
2086 }
2087 }
2088
2089 static void do_wav_capture(Monitor *mon, const QDict *qdict)
2090 {
2091 const char *path = qdict_get_str(qdict, "path");
2092 int has_freq = qdict_haskey(qdict, "freq");
2093 int freq = qdict_get_try_int(qdict, "freq", -1);
2094 int has_bits = qdict_haskey(qdict, "bits");
2095 int bits = qdict_get_try_int(qdict, "bits", -1);
2096 int has_channels = qdict_haskey(qdict, "nchannels");
2097 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2098 CaptureState *s;
2099
2100 s = g_malloc0 (sizeof (*s));
2101
2102 freq = has_freq ? freq : 44100;
2103 bits = has_bits ? bits : 16;
2104 nchannels = has_channels ? nchannels : 2;
2105
2106 if (wav_start_capture (s, path, freq, bits, nchannels)) {
2107 monitor_printf(mon, "Failed to add wave capture\n");
2108 g_free (s);
2109 return;
2110 }
2111 QLIST_INSERT_HEAD (&capture_head, s, entries);
2112 }
2113
2114 static qemu_acl *find_acl(Monitor *mon, const char *name)
2115 {
2116 qemu_acl *acl = qemu_acl_find(name);
2117
2118 if (!acl) {
2119 monitor_printf(mon, "acl: unknown list '%s'\n", name);
2120 }
2121 return acl;
2122 }
2123
2124 static void do_acl_show(Monitor *mon, const QDict *qdict)
2125 {
2126 const char *aclname = qdict_get_str(qdict, "aclname");
2127 qemu_acl *acl = find_acl(mon, aclname);
2128 qemu_acl_entry *entry;
2129 int i = 0;
2130
2131 if (acl) {
2132 monitor_printf(mon, "policy: %s\n",
2133 acl->defaultDeny ? "deny" : "allow");
2134 QTAILQ_FOREACH(entry, &acl->entries, next) {
2135 i++;
2136 monitor_printf(mon, "%d: %s %s\n", i,
2137 entry->deny ? "deny" : "allow", entry->match);
2138 }
2139 }
2140 }
2141
2142 static void do_acl_reset(Monitor *mon, const QDict *qdict)
2143 {
2144 const char *aclname = qdict_get_str(qdict, "aclname");
2145 qemu_acl *acl = find_acl(mon, aclname);
2146
2147 if (acl) {
2148 qemu_acl_reset(acl);
2149 monitor_printf(mon, "acl: removed all rules\n");
2150 }
2151 }
2152
2153 static void do_acl_policy(Monitor *mon, const QDict *qdict)
2154 {
2155 const char *aclname = qdict_get_str(qdict, "aclname");
2156 const char *policy = qdict_get_str(qdict, "policy");
2157 qemu_acl *acl = find_acl(mon, aclname);
2158
2159 if (acl) {
2160 if (strcmp(policy, "allow") == 0) {
2161 acl->defaultDeny = 0;
2162 monitor_printf(mon, "acl: policy set to 'allow'\n");
2163 } else if (strcmp(policy, "deny") == 0) {
2164 acl->defaultDeny = 1;
2165 monitor_printf(mon, "acl: policy set to 'deny'\n");
2166 } else {
2167 monitor_printf(mon, "acl: unknown policy '%s', "
2168 "expected 'deny' or 'allow'\n", policy);
2169 }
2170 }
2171 }
2172
2173 static void do_acl_add(Monitor *mon, const QDict *qdict)
2174 {
2175 const char *aclname = qdict_get_str(qdict, "aclname");
2176 const char *match = qdict_get_str(qdict, "match");
2177 const char *policy = qdict_get_str(qdict, "policy");
2178 int has_index = qdict_haskey(qdict, "index");
2179 int index = qdict_get_try_int(qdict, "index", -1);
2180 qemu_acl *acl = find_acl(mon, aclname);
2181 int deny, ret;
2182
2183 if (acl) {
2184 if (strcmp(policy, "allow") == 0) {
2185 deny = 0;
2186 } else if (strcmp(policy, "deny") == 0) {
2187 deny = 1;
2188 } else {
2189 monitor_printf(mon, "acl: unknown policy '%s', "
2190 "expected 'deny' or 'allow'\n", policy);
2191 return;
2192 }
2193 if (has_index)
2194 ret = qemu_acl_insert(acl, deny, match, index);
2195 else
2196 ret = qemu_acl_append(acl, deny, match);
2197 if (ret < 0)
2198 monitor_printf(mon, "acl: unable to add acl entry\n");
2199 else
2200 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2201 }
2202 }
2203
2204 static void do_acl_remove(Monitor *mon, const QDict *qdict)
2205 {
2206 const char *aclname = qdict_get_str(qdict, "aclname");
2207 const char *match = qdict_get_str(qdict, "match");
2208 qemu_acl *acl = find_acl(mon, aclname);
2209 int ret;
2210
2211 if (acl) {
2212 ret = qemu_acl_remove(acl, match);
2213 if (ret < 0)
2214 monitor_printf(mon, "acl: no matching acl entry\n");
2215 else
2216 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2217 }
2218 }
2219
2220 #if defined(TARGET_I386)
2221 static void do_inject_mce(Monitor *mon, const QDict *qdict)
2222 {
2223 X86CPU *cpu;
2224 CPUState *cs;
2225 int cpu_index = qdict_get_int(qdict, "cpu_index");
2226 int bank = qdict_get_int(qdict, "bank");
2227 uint64_t status = qdict_get_int(qdict, "status");
2228 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2229 uint64_t addr = qdict_get_int(qdict, "addr");
2230 uint64_t misc = qdict_get_int(qdict, "misc");
2231 int flags = MCE_INJECT_UNCOND_AO;
2232
2233 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2234 flags |= MCE_INJECT_BROADCAST;
2235 }
2236 cs = qemu_get_cpu(cpu_index);
2237 if (cs != NULL) {
2238 cpu = X86_CPU(cs);
2239 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2240 flags);
2241 }
2242 }
2243 #endif
2244
2245 void qmp_getfd(const char *fdname, Error **errp)
2246 {
2247 mon_fd_t *monfd;
2248 int fd;
2249
2250 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
2251 if (fd == -1) {
2252 error_set(errp, QERR_FD_NOT_SUPPLIED);
2253 return;
2254 }
2255
2256 if (qemu_isdigit(fdname[0])) {
2257 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2258 "a name not starting with a digit");
2259 return;
2260 }
2261
2262 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2263 if (strcmp(monfd->name, fdname) != 0) {
2264 continue;
2265 }
2266
2267 close(monfd->fd);
2268 monfd->fd = fd;
2269 return;
2270 }
2271
2272 monfd = g_malloc0(sizeof(mon_fd_t));
2273 monfd->name = g_strdup(fdname);
2274 monfd->fd = fd;
2275
2276 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
2277 }
2278
2279 void qmp_closefd(const char *fdname, Error **errp)
2280 {
2281 mon_fd_t *monfd;
2282
2283 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2284 if (strcmp(monfd->name, fdname) != 0) {
2285 continue;
2286 }
2287
2288 QLIST_REMOVE(monfd, next);
2289 close(monfd->fd);
2290 g_free(monfd->name);
2291 g_free(monfd);
2292 return;
2293 }
2294
2295 error_set(errp, QERR_FD_NOT_FOUND, fdname);
2296 }
2297
2298 static void do_loadvm(Monitor *mon, const QDict *qdict)
2299 {
2300 int saved_vm_running = runstate_is_running();
2301 const char *name = qdict_get_str(qdict, "name");
2302
2303 vm_stop(RUN_STATE_RESTORE_VM);
2304
2305 if (load_vmstate(name) == 0 && saved_vm_running) {
2306 vm_start();
2307 }
2308 }
2309
2310 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
2311 {
2312 mon_fd_t *monfd;
2313
2314 QLIST_FOREACH(monfd, &mon->fds, next) {
2315 int fd;
2316
2317 if (strcmp(monfd->name, fdname) != 0) {
2318 continue;
2319 }
2320
2321 fd = monfd->fd;
2322
2323 /* caller takes ownership of fd */
2324 QLIST_REMOVE(monfd, next);
2325 g_free(monfd->name);
2326 g_free(monfd);
2327
2328 return fd;
2329 }
2330
2331 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
2332 return -1;
2333 }
2334
2335 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2336 {
2337 MonFdsetFd *mon_fdset_fd;
2338 MonFdsetFd *mon_fdset_fd_next;
2339
2340 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
2341 if ((mon_fdset_fd->removed ||
2342 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2343 runstate_is_running()) {
2344 close(mon_fdset_fd->fd);
2345 g_free(mon_fdset_fd->opaque);
2346 QLIST_REMOVE(mon_fdset_fd, next);
2347 g_free(mon_fdset_fd);
2348 }
2349 }
2350
2351 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
2352 QLIST_REMOVE(mon_fdset, next);
2353 g_free(mon_fdset);
2354 }
2355 }
2356
2357 static void monitor_fdsets_cleanup(void)
2358 {
2359 MonFdset *mon_fdset;
2360 MonFdset *mon_fdset_next;
2361
2362 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2363 monitor_fdset_cleanup(mon_fdset);
2364 }
2365 }
2366
2367 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2368 const char *opaque, Error **errp)
2369 {
2370 int fd;
2371 Monitor *mon = cur_mon;
2372 AddfdInfo *fdinfo;
2373
2374 fd = qemu_chr_fe_get_msgfd(mon->chr);
2375 if (fd == -1) {
2376 error_set(errp, QERR_FD_NOT_SUPPLIED);
2377 goto error;
2378 }
2379
2380 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2381 has_opaque, opaque, errp);
2382 if (fdinfo) {
2383 return fdinfo;
2384 }
2385
2386 error:
2387 if (fd != -1) {
2388 close(fd);
2389 }
2390 return NULL;
2391 }
2392
2393 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2394 {
2395 MonFdset *mon_fdset;
2396 MonFdsetFd *mon_fdset_fd;
2397 char fd_str[60];
2398
2399 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2400 if (mon_fdset->id != fdset_id) {
2401 continue;
2402 }
2403 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2404 if (has_fd) {
2405 if (mon_fdset_fd->fd != fd) {
2406 continue;
2407 }
2408 mon_fdset_fd->removed = true;
2409 break;
2410 } else {
2411 mon_fdset_fd->removed = true;
2412 }
2413 }
2414 if (has_fd && !mon_fdset_fd) {
2415 goto error;
2416 }
2417 monitor_fdset_cleanup(mon_fdset);
2418 return;
2419 }
2420
2421 error:
2422 if (has_fd) {
2423 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2424 fdset_id, fd);
2425 } else {
2426 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2427 }
2428 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2429 }
2430
2431 FdsetInfoList *qmp_query_fdsets(Error **errp)
2432 {
2433 MonFdset *mon_fdset;
2434 MonFdsetFd *mon_fdset_fd;
2435 FdsetInfoList *fdset_list = NULL;
2436
2437 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2438 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2439 FdsetFdInfoList *fdsetfd_list = NULL;
2440
2441 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2442 fdset_info->value->fdset_id = mon_fdset->id;
2443
2444 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2445 FdsetFdInfoList *fdsetfd_info;
2446
2447 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2448 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2449 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2450 if (mon_fdset_fd->opaque) {
2451 fdsetfd_info->value->has_opaque = true;
2452 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2453 } else {
2454 fdsetfd_info->value->has_opaque = false;
2455 }
2456
2457 fdsetfd_info->next = fdsetfd_list;
2458 fdsetfd_list = fdsetfd_info;
2459 }
2460
2461 fdset_info->value->fds = fdsetfd_list;
2462
2463 fdset_info->next = fdset_list;
2464 fdset_list = fdset_info;
2465 }
2466
2467 return fdset_list;
2468 }
2469
2470 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2471 bool has_opaque, const char *opaque,
2472 Error **errp)
2473 {
2474 MonFdset *mon_fdset = NULL;
2475 MonFdsetFd *mon_fdset_fd;
2476 AddfdInfo *fdinfo;
2477
2478 if (has_fdset_id) {
2479 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2480 /* Break if match found or match impossible due to ordering by ID */
2481 if (fdset_id <= mon_fdset->id) {
2482 if (fdset_id < mon_fdset->id) {
2483 mon_fdset = NULL;
2484 }
2485 break;
2486 }
2487 }
2488 }
2489
2490 if (mon_fdset == NULL) {
2491 int64_t fdset_id_prev = -1;
2492 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2493
2494 if (has_fdset_id) {
2495 if (fdset_id < 0) {
2496 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2497 "a non-negative value");
2498 return NULL;
2499 }
2500 /* Use specified fdset ID */
2501 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2502 mon_fdset_cur = mon_fdset;
2503 if (fdset_id < mon_fdset_cur->id) {
2504 break;
2505 }
2506 }
2507 } else {
2508 /* Use first available fdset ID */
2509 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2510 mon_fdset_cur = mon_fdset;
2511 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2512 fdset_id_prev = mon_fdset_cur->id;
2513 continue;
2514 }
2515 break;
2516 }
2517 }
2518
2519 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2520 if (has_fdset_id) {
2521 mon_fdset->id = fdset_id;
2522 } else {
2523 mon_fdset->id = fdset_id_prev + 1;
2524 }
2525
2526 /* The fdset list is ordered by fdset ID */
2527 if (!mon_fdset_cur) {
2528 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2529 } else if (mon_fdset->id < mon_fdset_cur->id) {
2530 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2531 } else {
2532 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2533 }
2534 }
2535
2536 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2537 mon_fdset_fd->fd = fd;
2538 mon_fdset_fd->removed = false;
2539 if (has_opaque) {
2540 mon_fdset_fd->opaque = g_strdup(opaque);
2541 }
2542 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2543
2544 fdinfo = g_malloc0(sizeof(*fdinfo));
2545 fdinfo->fdset_id = mon_fdset->id;
2546 fdinfo->fd = mon_fdset_fd->fd;
2547
2548 return fdinfo;
2549 }
2550
2551 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2552 {
2553 #ifndef _WIN32
2554 MonFdset *mon_fdset;
2555 MonFdsetFd *mon_fdset_fd;
2556 int mon_fd_flags;
2557
2558 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2559 if (mon_fdset->id != fdset_id) {
2560 continue;
2561 }
2562 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2563 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2564 if (mon_fd_flags == -1) {
2565 return -1;
2566 }
2567
2568 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2569 return mon_fdset_fd->fd;
2570 }
2571 }
2572 errno = EACCES;
2573 return -1;
2574 }
2575 #endif
2576
2577 errno = ENOENT;
2578 return -1;
2579 }
2580
2581 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2582 {
2583 MonFdset *mon_fdset;
2584 MonFdsetFd *mon_fdset_fd_dup;
2585
2586 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2587 if (mon_fdset->id != fdset_id) {
2588 continue;
2589 }
2590 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2591 if (mon_fdset_fd_dup->fd == dup_fd) {
2592 return -1;
2593 }
2594 }
2595 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2596 mon_fdset_fd_dup->fd = dup_fd;
2597 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2598 return 0;
2599 }
2600 return -1;
2601 }
2602
2603 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2604 {
2605 MonFdset *mon_fdset;
2606 MonFdsetFd *mon_fdset_fd_dup;
2607
2608 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2609 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2610 if (mon_fdset_fd_dup->fd == dup_fd) {
2611 if (remove) {
2612 QLIST_REMOVE(mon_fdset_fd_dup, next);
2613 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2614 monitor_fdset_cleanup(mon_fdset);
2615 }
2616 }
2617 return mon_fdset->id;
2618 }
2619 }
2620 }
2621 return -1;
2622 }
2623
2624 int monitor_fdset_dup_fd_find(int dup_fd)
2625 {
2626 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2627 }
2628
2629 int monitor_fdset_dup_fd_remove(int dup_fd)
2630 {
2631 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2632 }
2633
2634 int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2635 {
2636 int fd;
2637 Error *local_err = NULL;
2638
2639 if (!qemu_isdigit(fdname[0]) && mon) {
2640
2641 fd = monitor_get_fd(mon, fdname, &local_err);
2642 if (fd == -1) {
2643 qerror_report_err(local_err);
2644 error_free(local_err);
2645 return -1;
2646 }
2647 } else {
2648 fd = qemu_parse_fd(fdname);
2649 }
2650
2651 return fd;
2652 }
2653
2654 /* Please update hmp-commands.hx when adding or changing commands */
2655 static mon_cmd_t info_cmds[] = {
2656 {
2657 .name = "version",
2658 .args_type = "",
2659 .params = "",
2660 .help = "show the version of QEMU",
2661 .mhandler.cmd = hmp_info_version,
2662 },
2663 {
2664 .name = "network",
2665 .args_type = "",
2666 .params = "",
2667 .help = "show the network state",
2668 .mhandler.cmd = do_info_network,
2669 },
2670 {
2671 .name = "chardev",
2672 .args_type = "",
2673 .params = "",
2674 .help = "show the character devices",
2675 .mhandler.cmd = hmp_info_chardev,
2676 },
2677 {
2678 .name = "block",
2679 .args_type = "verbose:-v,device:B?",
2680 .params = "[-v] [device]",
2681 .help = "show info of one block device or all block devices "
2682 "(and details of images with -v option)",
2683 .mhandler.cmd = hmp_info_block,
2684 },
2685 {
2686 .name = "blockstats",
2687 .args_type = "",
2688 .params = "",
2689 .help = "show block device statistics",
2690 .mhandler.cmd = hmp_info_blockstats,
2691 },
2692 {
2693 .name = "block-jobs",
2694 .args_type = "",
2695 .params = "",
2696 .help = "show progress of ongoing block device operations",
2697 .mhandler.cmd = hmp_info_block_jobs,
2698 },
2699 {
2700 .name = "registers",
2701 .args_type = "",
2702 .params = "",
2703 .help = "show the cpu registers",
2704 .mhandler.cmd = do_info_registers,
2705 },
2706 {
2707 .name = "cpus",
2708 .args_type = "",
2709 .params = "",
2710 .help = "show infos for each CPU",
2711 .mhandler.cmd = hmp_info_cpus,
2712 },
2713 {
2714 .name = "history",
2715 .args_type = "",
2716 .params = "",
2717 .help = "show the command line history",
2718 .mhandler.cmd = do_info_history,
2719 },
2720 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2721 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2722 {
2723 .name = "irq",
2724 .args_type = "",
2725 .params = "",
2726 .help = "show the interrupts statistics (if available)",
2727 #ifdef TARGET_SPARC
2728 .mhandler.cmd = sun4m_irq_info,
2729 #elif defined(TARGET_LM32)
2730 .mhandler.cmd = lm32_irq_info,
2731 #else
2732 .mhandler.cmd = irq_info,
2733 #endif
2734 },
2735 {
2736 .name = "pic",
2737 .args_type = "",
2738 .params = "",
2739 .help = "show i8259 (PIC) state",
2740 #ifdef TARGET_SPARC
2741 .mhandler.cmd = sun4m_pic_info,
2742 #elif defined(TARGET_LM32)
2743 .mhandler.cmd = lm32_do_pic_info,
2744 #else
2745 .mhandler.cmd = pic_info,
2746 #endif
2747 },
2748 #endif
2749 {
2750 .name = "pci",
2751 .args_type = "",
2752 .params = "",
2753 .help = "show PCI info",
2754 .mhandler.cmd = hmp_info_pci,
2755 },
2756 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2757 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2758 {
2759 .name = "tlb",
2760 .args_type = "",
2761 .params = "",
2762 .help = "show virtual to physical memory mappings",
2763 .mhandler.cmd = tlb_info,
2764 },
2765 #endif
2766 #if defined(TARGET_I386)
2767 {
2768 .name = "mem",
2769 .args_type = "",
2770 .params = "",
2771 .help = "show the active virtual memory mappings",
2772 .mhandler.cmd = mem_info,
2773 },
2774 #endif
2775 {
2776 .name = "mtree",
2777 .args_type = "",
2778 .params = "",
2779 .help = "show memory tree",
2780 .mhandler.cmd = do_info_mtree,
2781 },
2782 {
2783 .name = "jit",
2784 .args_type = "",
2785 .params = "",
2786 .help = "show dynamic compiler info",
2787 .mhandler.cmd = do_info_jit,
2788 },
2789 {
2790 .name = "kvm",
2791 .args_type = "",
2792 .params = "",
2793 .help = "show KVM information",
2794 .mhandler.cmd = hmp_info_kvm,
2795 },
2796 {
2797 .name = "numa",
2798 .args_type = "",
2799 .params = "",
2800 .help = "show NUMA information",
2801 .mhandler.cmd = do_info_numa,
2802 },
2803 {
2804 .name = "usb",
2805 .args_type = "",
2806 .params = "",
2807 .help = "show guest USB devices",
2808 .mhandler.cmd = usb_info,
2809 },
2810 {
2811 .name = "usbhost",
2812 .args_type = "",
2813 .params = "",
2814 .help = "show host USB devices",
2815 .mhandler.cmd = usb_host_info,
2816 },
2817 {
2818 .name = "profile",
2819 .args_type = "",
2820 .params = "",
2821 .help = "show profiling information",
2822 .mhandler.cmd = do_info_profile,
2823 },
2824 {
2825 .name = "capture",
2826 .args_type = "",
2827 .params = "",
2828 .help = "show capture information",
2829 .mhandler.cmd = do_info_capture,
2830 },
2831 {
2832 .name = "snapshots",
2833 .args_type = "",
2834 .params = "",
2835 .help = "show the currently saved VM snapshots",
2836 .mhandler.cmd = do_info_snapshots,
2837 },
2838 {
2839 .name = "status",
2840 .args_type = "",
2841 .params = "",
2842 .help = "show the current VM status (running|paused)",
2843 .mhandler.cmd = hmp_info_status,
2844 },
2845 {
2846 .name = "pcmcia",
2847 .args_type = "",
2848 .params = "",
2849 .help = "show guest PCMCIA status",
2850 .mhandler.cmd = pcmcia_info,
2851 },
2852 {
2853 .name = "mice",
2854 .args_type = "",
2855 .params = "",
2856 .help = "show which guest mouse is receiving events",
2857 .mhandler.cmd = hmp_info_mice,
2858 },
2859 {
2860 .name = "vnc",
2861 .args_type = "",
2862 .params = "",
2863 .help = "show the vnc server status",
2864 .mhandler.cmd = hmp_info_vnc,
2865 },
2866 #if defined(CONFIG_SPICE)
2867 {
2868 .name = "spice",
2869 .args_type = "",
2870 .params = "",
2871 .help = "show the spice server status",
2872 .mhandler.cmd = hmp_info_spice,
2873 },
2874 #endif
2875 {
2876 .name = "name",
2877 .args_type = "",
2878 .params = "",
2879 .help = "show the current VM name",
2880 .mhandler.cmd = hmp_info_name,
2881 },
2882 {
2883 .name = "uuid",
2884 .args_type = "",
2885 .params = "",
2886 .help = "show the current VM UUID",
2887 .mhandler.cmd = hmp_info_uuid,
2888 },
2889 {
2890 .name = "cpustats",
2891 .args_type = "",
2892 .params = "",
2893 .help = "show CPU statistics",
2894 .mhandler.cmd = do_info_cpu_stats,
2895 },
2896 #if defined(CONFIG_SLIRP)
2897 {
2898 .name = "usernet",
2899 .args_type = "",
2900 .params = "",
2901 .help = "show user network stack connection states",
2902 .mhandler.cmd = do_info_usernet,
2903 },
2904 #endif
2905 {
2906 .name = "migrate",
2907 .args_type = "",
2908 .params = "",
2909 .help = "show migration status",
2910 .mhandler.cmd = hmp_info_migrate,
2911 },
2912 {
2913 .name = "migrate_capabilities",
2914 .args_type = "",
2915 .params = "",
2916 .help = "show current migration capabilities",
2917 .mhandler.cmd = hmp_info_migrate_capabilities,
2918 },
2919 {
2920 .name = "migrate_cache_size",
2921 .args_type = "",
2922 .params = "",
2923 .help = "show current migration xbzrle cache size",
2924 .mhandler.cmd = hmp_info_migrate_cache_size,
2925 },
2926 {
2927 .name = "balloon",
2928 .args_type = "",
2929 .params = "",
2930 .help = "show balloon information",
2931 .mhandler.cmd = hmp_info_balloon,
2932 },
2933 {
2934 .name = "qtree",
2935 .args_type = "",
2936 .params = "",
2937 .help = "show device tree",
2938 .mhandler.cmd = do_info_qtree,
2939 },
2940 {
2941 .name = "qdm",
2942 .args_type = "",
2943 .params = "",
2944 .help = "show qdev device model list",
2945 .mhandler.cmd = do_info_qdm,
2946 },
2947 {
2948 .name = "roms",
2949 .args_type = "",
2950 .params = "",
2951 .help = "show roms",
2952 .mhandler.cmd = do_info_roms,
2953 },
2954 {
2955 .name = "trace-events",
2956 .args_type = "",
2957 .params = "",
2958 .help = "show available trace-events & their state",
2959 .mhandler.cmd = do_trace_print_events,
2960 },
2961 {
2962 .name = "tpm",
2963 .args_type = "",
2964 .params = "",
2965 .help = "show the TPM device",
2966 .mhandler.cmd = hmp_info_tpm,
2967 },
2968 {
2969 .name = NULL,
2970 },
2971 };
2972
2973 /* mon_cmds and info_cmds would be sorted at runtime */
2974 static mon_cmd_t mon_cmds[] = {
2975 #include "hmp-commands.h"
2976 { NULL, NULL, },
2977 };
2978
2979 static const mon_cmd_t qmp_cmds[] = {
2980 #include "qmp-commands-old.h"
2981 { /* NULL */ },
2982 };
2983
2984 /*******************************************************************/
2985
2986 static const char *pch;
2987 static sigjmp_buf expr_env;
2988
2989 #define MD_TLONG 0
2990 #define MD_I32 1
2991
2992 typedef struct MonitorDef {
2993 const char *name;
2994 int offset;
2995 target_long (*get_value)(const struct MonitorDef *md, int val);
2996 int type;
2997 } MonitorDef;
2998
2999 #if defined(TARGET_I386)
3000 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
3001 {
3002 CPUArchState *env = mon_get_cpu();
3003 return env->eip + env->segs[R_CS].base;
3004 }
3005 #endif
3006
3007 #if defined(TARGET_PPC)
3008 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
3009 {
3010 CPUArchState *env = mon_get_cpu();
3011 unsigned int u;
3012 int i;
3013
3014 u = 0;
3015 for (i = 0; i < 8; i++)
3016 u |= env->crf[i] << (32 - (4 * i));
3017
3018 return u;
3019 }
3020
3021 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
3022 {
3023 CPUArchState *env = mon_get_cpu();
3024 return env->msr;
3025 }
3026
3027 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
3028 {
3029 CPUArchState *env = mon_get_cpu();
3030 return env->xer;
3031 }
3032
3033 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
3034 {
3035 CPUArchState *env = mon_get_cpu();
3036 return cpu_ppc_load_decr(env);
3037 }
3038
3039 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
3040 {
3041 CPUArchState *env = mon_get_cpu();
3042 return cpu_ppc_load_tbu(env);
3043 }
3044
3045 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
3046 {
3047 CPUArchState *env = mon_get_cpu();
3048 return cpu_ppc_load_tbl(env);
3049 }
3050 #endif
3051
3052 #if defined(TARGET_SPARC)
3053 #ifndef TARGET_SPARC64
3054 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
3055 {
3056 CPUArchState *env = mon_get_cpu();
3057
3058 return cpu_get_psr(env);
3059 }
3060 #endif
3061
3062 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
3063 {
3064 CPUArchState *env = mon_get_cpu();
3065 return env->regwptr[val];
3066 }
3067 #endif
3068
3069 static const MonitorDef monitor_defs[] = {
3070 #ifdef TARGET_I386
3071
3072 #define SEG(name, seg) \
3073 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3074 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3075 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
3076
3077 { "eax", offsetof(CPUX86State, regs[0]) },
3078 { "ecx", offsetof(CPUX86State, regs[1]) },
3079 { "edx", offsetof(CPUX86State, regs[2]) },
3080 { "ebx", offsetof(CPUX86State, regs[3]) },
3081 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3082 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3083 { "esi", offsetof(CPUX86State, regs[6]) },
3084 { "edi", offsetof(CPUX86State, regs[7]) },
3085 #ifdef TARGET_X86_64
3086 { "r8", offsetof(CPUX86State, regs[8]) },
3087 { "r9", offsetof(CPUX86State, regs[9]) },
3088 { "r10", offsetof(CPUX86State, regs[10]) },
3089 { "r11", offsetof(CPUX86State, regs[11]) },
3090 { "r12", offsetof(CPUX86State, regs[12]) },
3091 { "r13", offsetof(CPUX86State, regs[13]) },
3092 { "r14", offsetof(CPUX86State, regs[14]) },
3093 { "r15", offsetof(CPUX86State, regs[15]) },
3094 #endif
3095 { "eflags", offsetof(CPUX86State, eflags) },
3096 { "eip", offsetof(CPUX86State, eip) },
3097 SEG("cs", R_CS)
3098 SEG("ds", R_DS)
3099 SEG("es", R_ES)
3100 SEG("ss", R_SS)
3101 SEG("fs", R_FS)
3102 SEG("gs", R_GS)
3103 { "pc", 0, monitor_get_pc, },
3104 #elif defined(TARGET_PPC)
3105 /* General purpose registers */
3106 { "r0", offsetof(CPUPPCState, gpr[0]) },
3107 { "r1", offsetof(CPUPPCState, gpr[1]) },
3108 { "r2", offsetof(CPUPPCState, gpr[2]) },
3109 { "r3", offsetof(CPUPPCState, gpr[3]) },
3110 { "r4", offsetof(CPUPPCState, gpr[4]) },
3111 { "r5", offsetof(CPUPPCState, gpr[5]) },
3112 { "r6", offsetof(CPUPPCState, gpr[6]) },
3113 { "r7", offsetof(CPUPPCState, gpr[7]) },
3114 { "r8", offsetof(CPUPPCState, gpr[8]) },
3115 { "r9", offsetof(CPUPPCState, gpr[9]) },
3116 { "r10", offsetof(CPUPPCState, gpr[10]) },
3117 { "r11", offsetof(CPUPPCState, gpr[11]) },
3118 { "r12", offsetof(CPUPPCState, gpr[12]) },
3119 { "r13", offsetof(CPUPPCState, gpr[13]) },
3120 { "r14", offsetof(CPUPPCState, gpr[14]) },
3121 { "r15", offsetof(CPUPPCState, gpr[15]) },
3122 { "r16", offsetof(CPUPPCState, gpr[16]) },
3123 { "r17", offsetof(CPUPPCState, gpr[17]) },
3124 { "r18", offsetof(CPUPPCState, gpr[18]) },
3125 { "r19", offsetof(CPUPPCState, gpr[19]) },
3126 { "r20", offsetof(CPUPPCState, gpr[20]) },
3127 { "r21", offsetof(CPUPPCState, gpr[21]) },
3128 { "r22", offsetof(CPUPPCState, gpr[22]) },
3129 { "r23", offsetof(CPUPPCState, gpr[23]) },
3130 { "r24", offsetof(CPUPPCState, gpr[24]) },
3131 { "r25", offsetof(CPUPPCState, gpr[25]) },
3132 { "r26", offsetof(CPUPPCState, gpr[26]) },
3133 { "r27", offsetof(CPUPPCState, gpr[27]) },
3134 { "r28", offsetof(CPUPPCState, gpr[28]) },
3135 { "r29", offsetof(CPUPPCState, gpr[29]) },
3136 { "r30", offsetof(CPUPPCState, gpr[30]) },
3137 { "r31", offsetof(CPUPPCState, gpr[31]) },
3138 /* Floating point registers */
3139 { "f0", offsetof(CPUPPCState, fpr[0]) },
3140 { "f1", offsetof(CPUPPCState, fpr[1]) },
3141 { "f2", offsetof(CPUPPCState, fpr[2]) },
3142 { "f3", offsetof(CPUPPCState, fpr[3]) },
3143 { "f4", offsetof(CPUPPCState, fpr[4]) },
3144 { "f5", offsetof(CPUPPCState, fpr[5]) },
3145 { "f6", offsetof(CPUPPCState, fpr[6]) },
3146 { "f7", offsetof(CPUPPCState, fpr[7]) },
3147 { "f8", offsetof(CPUPPCState, fpr[8]) },
3148 { "f9", offsetof(CPUPPCState, fpr[9]) },
3149 { "f10", offsetof(CPUPPCState, fpr[10]) },
3150 { "f11", offsetof(CPUPPCState, fpr[11]) },
3151 { "f12", offsetof(CPUPPCState, fpr[12]) },
3152 { "f13", offsetof(CPUPPCState, fpr[13]) },
3153 { "f14", offsetof(CPUPPCState, fpr[14]) },
3154 { "f15", offsetof(CPUPPCState, fpr[15]) },
3155 { "f16", offsetof(CPUPPCState, fpr[16]) },
3156 { "f17", offsetof(CPUPPCState, fpr[17]) },
3157 { "f18", offsetof(CPUPPCState, fpr[18]) },
3158 { "f19", offsetof(CPUPPCState, fpr[19]) },
3159 { "f20", offsetof(CPUPPCState, fpr[20]) },
3160 { "f21", offsetof(CPUPPCState, fpr[21]) },
3161 { "f22", offsetof(CPUPPCState, fpr[22]) },
3162 { "f23", offsetof(CPUPPCState, fpr[23]) },
3163 { "f24", offsetof(CPUPPCState, fpr[24]) },
3164 { "f25", offsetof(CPUPPCState, fpr[25]) },
3165 { "f26", offsetof(CPUPPCState, fpr[26]) },
3166 { "f27", offsetof(CPUPPCState, fpr[27]) },
3167 { "f28", offsetof(CPUPPCState, fpr[28]) },
3168 { "f29", offsetof(CPUPPCState, fpr[29]) },
3169 { "f30", offsetof(CPUPPCState, fpr[30]) },
3170 { "f31", offsetof(CPUPPCState, fpr[31]) },
3171 { "fpscr", offsetof(CPUPPCState, fpscr) },
3172 /* Next instruction pointer */
3173 { "nip|pc", offsetof(CPUPPCState, nip) },
3174 { "lr", offsetof(CPUPPCState, lr) },
3175 { "ctr", offsetof(CPUPPCState, ctr) },
3176 { "decr", 0, &monitor_get_decr, },
3177 { "ccr", 0, &monitor_get_ccr, },
3178 /* Machine state register */
3179 { "msr", 0, &monitor_get_msr, },
3180 { "xer", 0, &monitor_get_xer, },
3181 { "tbu", 0, &monitor_get_tbu, },
3182 { "tbl", 0, &monitor_get_tbl, },
3183 /* Segment registers */
3184 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3185 { "sr0", offsetof(CPUPPCState, sr[0]) },
3186 { "sr1", offsetof(CPUPPCState, sr[1]) },
3187 { "sr2", offsetof(CPUPPCState, sr[2]) },
3188 { "sr3", offsetof(CPUPPCState, sr[3]) },
3189 { "sr4", offsetof(CPUPPCState, sr[4]) },
3190 { "sr5", offsetof(CPUPPCState, sr[5]) },
3191 { "sr6", offsetof(CPUPPCState, sr[6]) },
3192 { "sr7", offsetof(CPUPPCState, sr[7]) },
3193 { "sr8", offsetof(CPUPPCState, sr[8]) },
3194 { "sr9", offsetof(CPUPPCState, sr[9]) },
3195 { "sr10", offsetof(CPUPPCState, sr[10]) },
3196 { "sr11", offsetof(CPUPPCState, sr[11]) },
3197 { "sr12", offsetof(CPUPPCState, sr[12]) },
3198 { "sr13", offsetof(CPUPPCState, sr[13]) },
3199 { "sr14", offsetof(CPUPPCState, sr[14]) },
3200 { "sr15", offsetof(CPUPPCState, sr[15]) },
3201 /* Too lazy to put BATs... */
3202 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
3203
3204 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3205 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3206 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3207 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3208 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
3209 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3210 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3211 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3212 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3213 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3214 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3215 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3216 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3217 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3218 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3219 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3220 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3221 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3222 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3223 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3224 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3225 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3226 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3227 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3228 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3229 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3230 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3231 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3232 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3233 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3234 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3235 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3236 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3237 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3238 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3239 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3240 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3241 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3242 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3243 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3244 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3245 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3246 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3247 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3248 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3249 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3250 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3251 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3252 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3253 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3254 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3255 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3256 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3257 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3258 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3259 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3260 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3261 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3262 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3263 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3264 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3265 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3266 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3267 { "epsc", offsetof(CPUPPCState