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