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