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