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