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