Merge branch 'upstream-merge'
[qemu-kvm/markmc.git] / monitor.c
blobc683eb298324f251fa73c3d3e71cbbb654239366
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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 "qemu-char.h"
36 #include "sysemu.h"
37 #include "monitor.h"
38 #include "readline.h"
39 #include "console.h"
40 #include "block.h"
41 #include "audio/audio.h"
42 #include "disas.h"
43 #include "balloon.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
46 #include "kvm.h"
47 #include "acl.h"
48 #include "qint.h"
49 #include "qlist.h"
50 #include "qdict.h"
51 #include "qstring.h"
52 #include "exec-all.h"
54 #include "qemu-kvm.h"
56 //#define DEBUG
57 //#define DEBUG_COMPLETION
60 * Supported types:
62 * 'F' filename
63 * 'B' block device name
64 * 's' string (accept optional quote)
65 * 'i' 32 bit integer
66 * 'l' target long (32 or 64 bit)
67 * '/' optional gdb-like print format (like "/10x")
69 * '?' optional type (for all types, except '/')
70 * '.' other form of optional type (for 'i' and 'l')
71 * '-' optional parameter (eg. '-f')
75 typedef struct mon_cmd_t {
76 const char *name;
77 const char *args_type;
78 const char *params;
79 const char *help;
80 void (*user_print)(Monitor *mon, const QObject *data);
81 union {
82 void (*info)(Monitor *mon);
83 void (*info_new)(Monitor *mon, QObject **ret_data);
84 void (*cmd)(Monitor *mon, const QDict *qdict);
85 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
86 } mhandler;
87 } mon_cmd_t;
89 /* file descriptors passed via SCM_RIGHTS */
90 typedef struct mon_fd_t mon_fd_t;
91 struct mon_fd_t {
92 char *name;
93 int fd;
94 QLIST_ENTRY(mon_fd_t) next;
97 struct Monitor {
98 CharDriverState *chr;
99 int mux_out;
100 int reset_seen;
101 int flags;
102 int suspend_cnt;
103 uint8_t outbuf[1024];
104 int outbuf_index;
105 ReadLineState *rs;
106 CPUState *mon_cpu;
107 BlockDriverCompletionFunc *password_completion_cb;
108 void *password_opaque;
109 QLIST_HEAD(,mon_fd_t) fds;
110 QLIST_ENTRY(Monitor) entry;
113 static QLIST_HEAD(mon_list, Monitor) mon_list;
115 static const mon_cmd_t mon_cmds[];
116 static const mon_cmd_t info_cmds[];
118 Monitor *cur_mon = NULL;
120 static void monitor_command_cb(Monitor *mon, const char *cmdline,
121 void *opaque);
123 static void monitor_read_command(Monitor *mon, int show_prompt)
125 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
126 if (show_prompt)
127 readline_show_prompt(mon->rs);
130 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
131 void *opaque)
133 if (mon->rs) {
134 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
135 /* prompt is printed on return from the command handler */
136 return 0;
137 } else {
138 monitor_printf(mon, "terminal does not support password prompting\n");
139 return -ENOTTY;
143 void monitor_flush(Monitor *mon)
145 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
146 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
147 mon->outbuf_index = 0;
151 /* flush at every end of line or if the buffer is full */
152 static void monitor_puts(Monitor *mon, const char *str)
154 char c;
156 if (!mon)
157 return;
159 for(;;) {
160 c = *str++;
161 if (c == '\0')
162 break;
163 if (c == '\n')
164 mon->outbuf[mon->outbuf_index++] = '\r';
165 mon->outbuf[mon->outbuf_index++] = c;
166 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
167 || c == '\n')
168 monitor_flush(mon);
172 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
174 char buf[4096];
175 vsnprintf(buf, sizeof(buf), fmt, ap);
176 monitor_puts(mon, buf);
179 void monitor_printf(Monitor *mon, const char *fmt, ...)
181 va_list ap;
182 va_start(ap, fmt);
183 monitor_vprintf(mon, fmt, ap);
184 va_end(ap);
187 void monitor_print_filename(Monitor *mon, const char *filename)
189 int i;
191 for (i = 0; filename[i]; i++) {
192 switch (filename[i]) {
193 case ' ':
194 case '"':
195 case '\\':
196 monitor_printf(mon, "\\%c", filename[i]);
197 break;
198 case '\t':
199 monitor_printf(mon, "\\t");
200 break;
201 case '\r':
202 monitor_printf(mon, "\\r");
203 break;
204 case '\n':
205 monitor_printf(mon, "\\n");
206 break;
207 default:
208 monitor_printf(mon, "%c", filename[i]);
209 break;
214 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
216 va_list ap;
217 va_start(ap, fmt);
218 monitor_vprintf((Monitor *)stream, fmt, ap);
219 va_end(ap);
220 return 0;
223 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
225 static inline int monitor_handler_ported(const mon_cmd_t *cmd)
227 return cmd->user_print != NULL;
230 static void monitor_print_qobject(Monitor *mon, const QObject *data)
232 switch (qobject_type(data)) {
233 case QTYPE_QSTRING:
234 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
235 break;
236 case QTYPE_QINT:
237 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
238 break;
239 default:
240 monitor_printf(mon, "ERROR: unsupported type: %d",
241 qobject_type(data));
242 break;
245 monitor_puts(mon, "\n");
248 static int compare_cmd(const char *name, const char *list)
250 const char *p, *pstart;
251 int len;
252 len = strlen(name);
253 p = list;
254 for(;;) {
255 pstart = p;
256 p = strchr(p, '|');
257 if (!p)
258 p = pstart + strlen(pstart);
259 if ((p - pstart) == len && !memcmp(pstart, name, len))
260 return 1;
261 if (*p == '\0')
262 break;
263 p++;
265 return 0;
268 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
269 const char *prefix, const char *name)
271 const mon_cmd_t *cmd;
273 for(cmd = cmds; cmd->name != NULL; cmd++) {
274 if (!name || !strcmp(name, cmd->name))
275 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
276 cmd->params, cmd->help);
280 static void help_cmd(Monitor *mon, const char *name)
282 if (name && !strcmp(name, "info")) {
283 help_cmd_dump(mon, info_cmds, "info ", NULL);
284 } else {
285 help_cmd_dump(mon, mon_cmds, "", name);
286 if (name && !strcmp(name, "log")) {
287 const CPULogItem *item;
288 monitor_printf(mon, "Log items (comma separated):\n");
289 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
290 for(item = cpu_log_items; item->mask != 0; item++) {
291 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
297 static void do_help_cmd(Monitor *mon, const QDict *qdict)
299 help_cmd(mon, qdict_get_try_str(qdict, "name"));
302 static void do_commit(Monitor *mon, const QDict *qdict)
304 int all_devices;
305 DriveInfo *dinfo;
306 const char *device = qdict_get_str(qdict, "device");
308 all_devices = !strcmp(device, "all");
309 QTAILQ_FOREACH(dinfo, &drives, next) {
310 if (!all_devices)
311 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
312 continue;
313 bdrv_commit(dinfo->bdrv);
317 static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
319 const mon_cmd_t *cmd;
320 const char *item = qdict_get_try_str(qdict, "item");
322 if (!item)
323 goto help;
325 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
326 if (compare_cmd(item, cmd->name))
327 break;
330 if (cmd->name == NULL)
331 goto help;
333 if (monitor_handler_ported(cmd)) {
334 cmd->mhandler.info_new(mon, ret_data);
335 if (*ret_data)
336 cmd->user_print(mon, *ret_data);
337 } else {
338 cmd->mhandler.info(mon);
341 return;
343 help:
344 help_cmd(mon, "info");
348 * do_info_version(): Show QEMU version
350 static void do_info_version(Monitor *mon, QObject **ret_data)
352 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
355 static void do_info_name(Monitor *mon)
357 if (qemu_name)
358 monitor_printf(mon, "%s\n", qemu_name);
361 #if defined(TARGET_I386)
362 static void do_info_hpet(Monitor *mon)
364 monitor_printf(mon, "HPET is %s by QEMU\n",
365 (no_hpet) ? "disabled" : "enabled");
367 #endif
369 static void do_info_uuid(Monitor *mon)
371 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
372 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
373 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
374 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
375 qemu_uuid[14], qemu_uuid[15]);
378 /* get the current CPU defined by the user */
379 static int mon_set_cpu(int cpu_index)
381 CPUState *env;
383 for(env = first_cpu; env != NULL; env = env->next_cpu) {
384 if (env->cpu_index == cpu_index) {
385 cur_mon->mon_cpu = env;
386 return 0;
389 return -1;
392 static CPUState *mon_get_cpu(void)
394 if (!cur_mon->mon_cpu) {
395 mon_set_cpu(0);
397 cpu_synchronize_state(cur_mon->mon_cpu);
398 kvm_save_mpstate(cur_mon->mon_cpu);
399 return cur_mon->mon_cpu;
402 static void do_info_registers(Monitor *mon)
404 CPUState *env;
405 env = mon_get_cpu();
406 if (!env)
407 return;
408 #ifdef TARGET_I386
409 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
410 X86_DUMP_FPU);
411 #else
412 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
414 #endif
417 static void print_cpu_iter(QObject *obj, void *opaque)
419 QDict *cpu;
420 int active = ' ';
421 Monitor *mon = opaque;
423 assert(qobject_type(obj) == QTYPE_QDICT);
424 cpu = qobject_to_qdict(obj);
426 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
427 active = '*';
429 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
431 #if defined(TARGET_I386)
432 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
433 (target_ulong) qdict_get_int(cpu, "pc"));
434 #elif defined(TARGET_PPC)
435 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
436 (target_long) qdict_get_int(cpu, "nip"));
437 #elif defined(TARGET_SPARC)
438 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
439 (target_long) qdict_get_int(cpu, "pc"));
440 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
441 (target_long) qdict_get_int(cpu, "npc"));
442 #elif defined(TARGET_MIPS)
443 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
444 (target_long) qdict_get_int(cpu, "PC"));
445 #endif
447 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
448 monitor_printf(mon, " (halted)");
450 monitor_printf(mon, " thread_id=%" PRId64 " ",
451 qdict_get_int(cpu, "thread_id"));
453 monitor_printf(mon, "\n");
456 static void monitor_print_cpus(Monitor *mon, const QObject *data)
458 QList *cpu_list;
460 assert(qobject_type(data) == QTYPE_QLIST);
461 cpu_list = qobject_to_qlist(data);
462 qlist_iter(cpu_list, print_cpu_iter, mon);
466 * do_info_cpus(): Show CPU information
468 * Return a QList with a QDict for each CPU.
470 * For example:
472 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
473 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
475 static void do_info_cpus(Monitor *mon, QObject **ret_data)
477 CPUState *env;
478 QList *cpu_list;
480 cpu_list = qlist_new();
482 /* just to set the default cpu if not already done */
483 mon_get_cpu();
485 for(env = first_cpu; env != NULL; env = env->next_cpu) {
486 const char *answer;
487 QDict *cpu = qdict_new();
489 cpu_synchronize_state(env);
490 kvm_save_mpstate(env);
492 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
493 answer = (env == mon->mon_cpu) ? "yes" : "no";
494 qdict_put(cpu, "current", qstring_from_str(answer));
495 #if defined(TARGET_I386)
496 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
497 #elif defined(TARGET_PPC)
498 qdict_put(cpu, "nip", qint_from_int(env->nip));
499 #elif defined(TARGET_SPARC)
500 qdict_put(cpu, "pc", qint_from_int(env->pc));
501 qdict_put(cpu, "npc", qint_from_int(env->npc));
502 #elif defined(TARGET_MIPS)
503 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
504 #endif
505 answer = env->halted ? "yes" : "no";
506 qdict_put(cpu, "halted", qstring_from_str(answer));
507 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
509 qlist_append(cpu_list, cpu);
512 *ret_data = QOBJECT(cpu_list);
515 static void do_cpu_set(Monitor *mon, const QDict *qdict)
517 int index = qdict_get_int(qdict, "index");
518 if (mon_set_cpu(index) < 0)
519 monitor_printf(mon, "Invalid CPU index\n");
522 static void do_cpu_set_nr(Monitor *mon, const QDict *qdict)
524 int state, value;
525 const char *status;
527 status = qdict_get_str(qdict, "state");
528 value = qdict_get_int(qdict, "cpu");
530 if (!strcmp(status, "online"))
531 state = 1;
532 else if (!strcmp(status, "offline"))
533 state = 0;
534 else {
535 monitor_printf(mon, "invalid status: %s\n", status);
536 return;
538 #if defined(TARGET_I386) || defined(TARGET_X86_64)
539 qemu_system_cpu_hot_add(value, state);
540 #endif
543 static void do_info_jit(Monitor *mon)
545 dump_exec_info((FILE *)mon, monitor_fprintf);
548 static void do_info_history(Monitor *mon)
550 int i;
551 const char *str;
553 if (!mon->rs)
554 return;
555 i = 0;
556 for(;;) {
557 str = readline_get_history(mon->rs, i);
558 if (!str)
559 break;
560 monitor_printf(mon, "%d: '%s'\n", i, str);
561 i++;
565 #if defined(TARGET_PPC)
566 /* XXX: not implemented in other targets */
567 static void do_info_cpu_stats(Monitor *mon)
569 CPUState *env;
571 env = mon_get_cpu();
572 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
574 #endif
577 * do_quit(): Quit QEMU execution
579 static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
581 exit(0);
584 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
586 if (bdrv_is_inserted(bs)) {
587 if (!force) {
588 if (!bdrv_is_removable(bs)) {
589 monitor_printf(mon, "device is not removable\n");
590 return -1;
592 if (bdrv_is_locked(bs)) {
593 monitor_printf(mon, "device is locked\n");
594 return -1;
597 bdrv_close(bs);
599 return 0;
602 static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
604 BlockDriverState *bs;
605 int force = qdict_get_int(qdict, "force");
606 const char *filename = qdict_get_str(qdict, "filename");
608 bs = bdrv_find(filename);
609 if (!bs) {
610 monitor_printf(mon, "device not found\n");
611 return;
613 eject_device(mon, bs, force);
616 static void do_change_block(Monitor *mon, const char *device,
617 const char *filename, const char *fmt)
619 BlockDriverState *bs;
620 BlockDriver *drv = NULL;
622 bs = bdrv_find(device);
623 if (!bs) {
624 monitor_printf(mon, "device not found\n");
625 return;
627 if (fmt) {
628 drv = bdrv_find_format(fmt);
629 if (!drv) {
630 monitor_printf(mon, "invalid format %s\n", fmt);
631 return;
634 if (eject_device(mon, bs, 0) < 0)
635 return;
636 bdrv_open2(bs, filename, 0, drv);
637 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
640 static void change_vnc_password_cb(Monitor *mon, const char *password,
641 void *opaque)
643 if (vnc_display_password(NULL, password) < 0)
644 monitor_printf(mon, "could not set VNC server password\n");
646 monitor_read_command(mon, 1);
649 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
651 if (strcmp(target, "passwd") == 0 ||
652 strcmp(target, "password") == 0) {
653 if (arg) {
654 char password[9];
655 strncpy(password, arg, sizeof(password));
656 password[sizeof(password) - 1] = '\0';
657 change_vnc_password_cb(mon, password, NULL);
658 } else {
659 monitor_read_password(mon, change_vnc_password_cb, NULL);
661 } else {
662 if (vnc_display_open(NULL, target) < 0)
663 monitor_printf(mon, "could not start VNC server on %s\n", target);
667 static void do_change(Monitor *mon, const QDict *qdict)
669 const char *device = qdict_get_str(qdict, "device");
670 const char *target = qdict_get_str(qdict, "target");
671 const char *arg = qdict_get_try_str(qdict, "arg");
672 if (strcmp(device, "vnc") == 0) {
673 do_change_vnc(mon, target, arg);
674 } else {
675 do_change_block(mon, device, target, arg);
679 static void do_screen_dump(Monitor *mon, const QDict *qdict)
681 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
684 static void do_logfile(Monitor *mon, const QDict *qdict)
686 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
689 static void do_log(Monitor *mon, const QDict *qdict)
691 int mask;
692 const char *items = qdict_get_str(qdict, "items");
694 if (!strcmp(items, "none")) {
695 mask = 0;
696 } else {
697 mask = cpu_str_to_log_mask(items);
698 if (!mask) {
699 help_cmd(mon, "log");
700 return;
703 cpu_set_log(mask);
706 static void do_singlestep(Monitor *mon, const QDict *qdict)
708 const char *option = qdict_get_try_str(qdict, "option");
709 if (!option || !strcmp(option, "on")) {
710 singlestep = 1;
711 } else if (!strcmp(option, "off")) {
712 singlestep = 0;
713 } else {
714 monitor_printf(mon, "unexpected option %s\n", option);
719 * do_stop(): Stop VM execution
721 static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
723 vm_stop(EXCP_INTERRUPT);
726 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
728 struct bdrv_iterate_context {
729 Monitor *mon;
730 int err;
734 * do_cont(): Resume emulation.
736 static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
738 struct bdrv_iterate_context context = { mon, 0 };
740 bdrv_iterate(encrypted_bdrv_it, &context);
741 /* only resume the vm if all keys are set and valid */
742 if (!context.err)
743 vm_start();
746 static void bdrv_key_cb(void *opaque, int err)
748 Monitor *mon = opaque;
750 /* another key was set successfully, retry to continue */
751 if (!err)
752 do_cont(mon, NULL, NULL);
755 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
757 struct bdrv_iterate_context *context = opaque;
759 if (!context->err && bdrv_key_required(bs)) {
760 context->err = -EBUSY;
761 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
762 context->mon);
766 static void do_gdbserver(Monitor *mon, const QDict *qdict)
768 const char *device = qdict_get_try_str(qdict, "device");
769 if (!device)
770 device = "tcp::" DEFAULT_GDBSTUB_PORT;
771 if (gdbserver_start(device) < 0) {
772 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
773 device);
774 } else if (strcmp(device, "none") == 0) {
775 monitor_printf(mon, "Disabled gdbserver\n");
776 } else {
777 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
778 device);
782 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
784 const char *action = qdict_get_str(qdict, "action");
785 if (select_watchdog_action(action) == -1) {
786 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
790 static void monitor_printc(Monitor *mon, int c)
792 monitor_printf(mon, "'");
793 switch(c) {
794 case '\'':
795 monitor_printf(mon, "\\'");
796 break;
797 case '\\':
798 monitor_printf(mon, "\\\\");
799 break;
800 case '\n':
801 monitor_printf(mon, "\\n");
802 break;
803 case '\r':
804 monitor_printf(mon, "\\r");
805 break;
806 default:
807 if (c >= 32 && c <= 126) {
808 monitor_printf(mon, "%c", c);
809 } else {
810 monitor_printf(mon, "\\x%02x", c);
812 break;
814 monitor_printf(mon, "'");
817 static void memory_dump(Monitor *mon, int count, int format, int wsize,
818 target_phys_addr_t addr, int is_physical)
820 CPUState *env;
821 int nb_per_line, l, line_size, i, max_digits, len;
822 uint8_t buf[16];
823 uint64_t v;
825 if (format == 'i') {
826 int flags;
827 flags = 0;
828 env = mon_get_cpu();
829 if (!env && !is_physical)
830 return;
831 #ifdef TARGET_I386
832 if (wsize == 2) {
833 flags = 1;
834 } else if (wsize == 4) {
835 flags = 0;
836 } else {
837 /* as default we use the current CS size */
838 flags = 0;
839 if (env) {
840 #ifdef TARGET_X86_64
841 if ((env->efer & MSR_EFER_LMA) &&
842 (env->segs[R_CS].flags & DESC_L_MASK))
843 flags = 2;
844 else
845 #endif
846 if (!(env->segs[R_CS].flags & DESC_B_MASK))
847 flags = 1;
850 #endif
851 monitor_disas(mon, env, addr, count, is_physical, flags);
852 return;
855 len = wsize * count;
856 if (wsize == 1)
857 line_size = 8;
858 else
859 line_size = 16;
860 nb_per_line = line_size / wsize;
861 max_digits = 0;
863 switch(format) {
864 case 'o':
865 max_digits = (wsize * 8 + 2) / 3;
866 break;
867 default:
868 case 'x':
869 max_digits = (wsize * 8) / 4;
870 break;
871 case 'u':
872 case 'd':
873 max_digits = (wsize * 8 * 10 + 32) / 33;
874 break;
875 case 'c':
876 wsize = 1;
877 break;
880 while (len > 0) {
881 if (is_physical)
882 monitor_printf(mon, TARGET_FMT_plx ":", addr);
883 else
884 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
885 l = len;
886 if (l > line_size)
887 l = line_size;
888 if (is_physical) {
889 cpu_physical_memory_rw(addr, buf, l, 0);
890 } else {
891 env = mon_get_cpu();
892 if (!env)
893 break;
894 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
895 monitor_printf(mon, " Cannot access memory\n");
896 break;
899 i = 0;
900 while (i < l) {
901 switch(wsize) {
902 default:
903 case 1:
904 v = ldub_raw(buf + i);
905 break;
906 case 2:
907 v = lduw_raw(buf + i);
908 break;
909 case 4:
910 v = (uint32_t)ldl_raw(buf + i);
911 break;
912 case 8:
913 v = ldq_raw(buf + i);
914 break;
916 monitor_printf(mon, " ");
917 switch(format) {
918 case 'o':
919 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
920 break;
921 case 'x':
922 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
923 break;
924 case 'u':
925 monitor_printf(mon, "%*" PRIu64, max_digits, v);
926 break;
927 case 'd':
928 monitor_printf(mon, "%*" PRId64, max_digits, v);
929 break;
930 case 'c':
931 monitor_printc(mon, v);
932 break;
934 i += wsize;
936 monitor_printf(mon, "\n");
937 addr += l;
938 len -= l;
942 static void do_memory_dump(Monitor *mon, const QDict *qdict)
944 int count = qdict_get_int(qdict, "count");
945 int format = qdict_get_int(qdict, "format");
946 int size = qdict_get_int(qdict, "size");
947 target_long addr = qdict_get_int(qdict, "addr");
949 memory_dump(mon, count, format, size, addr, 0);
952 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
954 int count = qdict_get_int(qdict, "count");
955 int format = qdict_get_int(qdict, "format");
956 int size = qdict_get_int(qdict, "size");
957 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
959 memory_dump(mon, count, format, size, addr, 1);
962 static void do_print(Monitor *mon, const QDict *qdict)
964 int format = qdict_get_int(qdict, "format");
965 target_phys_addr_t val = qdict_get_int(qdict, "val");
967 #if TARGET_PHYS_ADDR_BITS == 32
968 switch(format) {
969 case 'o':
970 monitor_printf(mon, "%#o", val);
971 break;
972 case 'x':
973 monitor_printf(mon, "%#x", val);
974 break;
975 case 'u':
976 monitor_printf(mon, "%u", val);
977 break;
978 default:
979 case 'd':
980 monitor_printf(mon, "%d", val);
981 break;
982 case 'c':
983 monitor_printc(mon, val);
984 break;
986 #else
987 switch(format) {
988 case 'o':
989 monitor_printf(mon, "%#" PRIo64, val);
990 break;
991 case 'x':
992 monitor_printf(mon, "%#" PRIx64, val);
993 break;
994 case 'u':
995 monitor_printf(mon, "%" PRIu64, val);
996 break;
997 default:
998 case 'd':
999 monitor_printf(mon, "%" PRId64, val);
1000 break;
1001 case 'c':
1002 monitor_printc(mon, val);
1003 break;
1005 #endif
1006 monitor_printf(mon, "\n");
1009 static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
1011 FILE *f;
1012 uint32_t size = qdict_get_int(qdict, "size");
1013 const char *filename = qdict_get_str(qdict, "filename");
1014 target_long addr = qdict_get_int(qdict, "val");
1015 uint32_t l;
1016 CPUState *env;
1017 uint8_t buf[1024];
1019 env = mon_get_cpu();
1020 if (!env)
1021 return;
1023 f = fopen(filename, "wb");
1024 if (!f) {
1025 monitor_printf(mon, "could not open '%s'\n", filename);
1026 return;
1028 while (size != 0) {
1029 l = sizeof(buf);
1030 if (l > size)
1031 l = size;
1032 cpu_memory_rw_debug(env, addr, buf, l, 0);
1033 fwrite(buf, 1, l, f);
1034 addr += l;
1035 size -= l;
1037 fclose(f);
1040 static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1041 QObject **ret_data)
1043 FILE *f;
1044 uint32_t l;
1045 uint8_t buf[1024];
1046 uint32_t size = qdict_get_int(qdict, "size");
1047 const char *filename = qdict_get_str(qdict, "filename");
1048 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1050 f = fopen(filename, "wb");
1051 if (!f) {
1052 monitor_printf(mon, "could not open '%s'\n", filename);
1053 return;
1055 while (size != 0) {
1056 l = sizeof(buf);
1057 if (l > size)
1058 l = size;
1059 cpu_physical_memory_rw(addr, buf, l, 0);
1060 fwrite(buf, 1, l, f);
1061 fflush(f);
1062 addr += l;
1063 size -= l;
1065 fclose(f);
1068 static void do_sum(Monitor *mon, const QDict *qdict)
1070 uint32_t addr;
1071 uint8_t buf[1];
1072 uint16_t sum;
1073 uint32_t start = qdict_get_int(qdict, "start");
1074 uint32_t size = qdict_get_int(qdict, "size");
1076 sum = 0;
1077 for(addr = start; addr < (start + size); addr++) {
1078 cpu_physical_memory_rw(addr, buf, 1, 0);
1079 /* BSD sum algorithm ('sum' Unix command) */
1080 sum = (sum >> 1) | (sum << 15);
1081 sum += buf[0];
1083 monitor_printf(mon, "%05d\n", sum);
1086 typedef struct {
1087 int keycode;
1088 const char *name;
1089 } KeyDef;
1091 static const KeyDef key_defs[] = {
1092 { 0x2a, "shift" },
1093 { 0x36, "shift_r" },
1095 { 0x38, "alt" },
1096 { 0xb8, "alt_r" },
1097 { 0x64, "altgr" },
1098 { 0xe4, "altgr_r" },
1099 { 0x1d, "ctrl" },
1100 { 0x9d, "ctrl_r" },
1102 { 0xdd, "menu" },
1104 { 0x01, "esc" },
1106 { 0x02, "1" },
1107 { 0x03, "2" },
1108 { 0x04, "3" },
1109 { 0x05, "4" },
1110 { 0x06, "5" },
1111 { 0x07, "6" },
1112 { 0x08, "7" },
1113 { 0x09, "8" },
1114 { 0x0a, "9" },
1115 { 0x0b, "0" },
1116 { 0x0c, "minus" },
1117 { 0x0d, "equal" },
1118 { 0x0e, "backspace" },
1120 { 0x0f, "tab" },
1121 { 0x10, "q" },
1122 { 0x11, "w" },
1123 { 0x12, "e" },
1124 { 0x13, "r" },
1125 { 0x14, "t" },
1126 { 0x15, "y" },
1127 { 0x16, "u" },
1128 { 0x17, "i" },
1129 { 0x18, "o" },
1130 { 0x19, "p" },
1132 { 0x1c, "ret" },
1134 { 0x1e, "a" },
1135 { 0x1f, "s" },
1136 { 0x20, "d" },
1137 { 0x21, "f" },
1138 { 0x22, "g" },
1139 { 0x23, "h" },
1140 { 0x24, "j" },
1141 { 0x25, "k" },
1142 { 0x26, "l" },
1144 { 0x2c, "z" },
1145 { 0x2d, "x" },
1146 { 0x2e, "c" },
1147 { 0x2f, "v" },
1148 { 0x30, "b" },
1149 { 0x31, "n" },
1150 { 0x32, "m" },
1151 { 0x33, "comma" },
1152 { 0x34, "dot" },
1153 { 0x35, "slash" },
1155 { 0x37, "asterisk" },
1157 { 0x39, "spc" },
1158 { 0x3a, "caps_lock" },
1159 { 0x3b, "f1" },
1160 { 0x3c, "f2" },
1161 { 0x3d, "f3" },
1162 { 0x3e, "f4" },
1163 { 0x3f, "f5" },
1164 { 0x40, "f6" },
1165 { 0x41, "f7" },
1166 { 0x42, "f8" },
1167 { 0x43, "f9" },
1168 { 0x44, "f10" },
1169 { 0x45, "num_lock" },
1170 { 0x46, "scroll_lock" },
1172 { 0xb5, "kp_divide" },
1173 { 0x37, "kp_multiply" },
1174 { 0x4a, "kp_subtract" },
1175 { 0x4e, "kp_add" },
1176 { 0x9c, "kp_enter" },
1177 { 0x53, "kp_decimal" },
1178 { 0x54, "sysrq" },
1180 { 0x52, "kp_0" },
1181 { 0x4f, "kp_1" },
1182 { 0x50, "kp_2" },
1183 { 0x51, "kp_3" },
1184 { 0x4b, "kp_4" },
1185 { 0x4c, "kp_5" },
1186 { 0x4d, "kp_6" },
1187 { 0x47, "kp_7" },
1188 { 0x48, "kp_8" },
1189 { 0x49, "kp_9" },
1191 { 0x56, "<" },
1193 { 0x57, "f11" },
1194 { 0x58, "f12" },
1196 { 0xb7, "print" },
1198 { 0xc7, "home" },
1199 { 0xc9, "pgup" },
1200 { 0xd1, "pgdn" },
1201 { 0xcf, "end" },
1203 { 0xcb, "left" },
1204 { 0xc8, "up" },
1205 { 0xd0, "down" },
1206 { 0xcd, "right" },
1208 { 0xd2, "insert" },
1209 { 0xd3, "delete" },
1210 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1211 { 0xf0, "stop" },
1212 { 0xf1, "again" },
1213 { 0xf2, "props" },
1214 { 0xf3, "undo" },
1215 { 0xf4, "front" },
1216 { 0xf5, "copy" },
1217 { 0xf6, "open" },
1218 { 0xf7, "paste" },
1219 { 0xf8, "find" },
1220 { 0xf9, "cut" },
1221 { 0xfa, "lf" },
1222 { 0xfb, "help" },
1223 { 0xfc, "meta_l" },
1224 { 0xfd, "meta_r" },
1225 { 0xfe, "compose" },
1226 #endif
1227 { 0, NULL },
1230 static int get_keycode(const char *key)
1232 const KeyDef *p;
1233 char *endp;
1234 int ret;
1236 for(p = key_defs; p->name != NULL; p++) {
1237 if (!strcmp(key, p->name))
1238 return p->keycode;
1240 if (strstart(key, "0x", NULL)) {
1241 ret = strtoul(key, &endp, 0);
1242 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1243 return ret;
1245 return -1;
1248 #define MAX_KEYCODES 16
1249 static uint8_t keycodes[MAX_KEYCODES];
1250 static int nb_pending_keycodes;
1251 static QEMUTimer *key_timer;
1253 static void release_keys(void *opaque)
1255 int keycode;
1257 while (nb_pending_keycodes > 0) {
1258 nb_pending_keycodes--;
1259 keycode = keycodes[nb_pending_keycodes];
1260 if (keycode & 0x80)
1261 kbd_put_keycode(0xe0);
1262 kbd_put_keycode(keycode | 0x80);
1266 static void do_sendkey(Monitor *mon, const QDict *qdict)
1268 char keyname_buf[16];
1269 char *separator;
1270 int keyname_len, keycode, i;
1271 const char *string = qdict_get_str(qdict, "string");
1272 int has_hold_time = qdict_haskey(qdict, "hold_time");
1273 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1275 if (nb_pending_keycodes > 0) {
1276 qemu_del_timer(key_timer);
1277 release_keys(NULL);
1279 if (!has_hold_time)
1280 hold_time = 100;
1281 i = 0;
1282 while (1) {
1283 separator = strchr(string, '-');
1284 keyname_len = separator ? separator - string : strlen(string);
1285 if (keyname_len > 0) {
1286 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1287 if (keyname_len > sizeof(keyname_buf) - 1) {
1288 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1289 return;
1291 if (i == MAX_KEYCODES) {
1292 monitor_printf(mon, "too many keys\n");
1293 return;
1295 keyname_buf[keyname_len] = 0;
1296 keycode = get_keycode(keyname_buf);
1297 if (keycode < 0) {
1298 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1299 return;
1301 keycodes[i++] = keycode;
1303 if (!separator)
1304 break;
1305 string = separator + 1;
1307 nb_pending_keycodes = i;
1308 /* key down events */
1309 for (i = 0; i < nb_pending_keycodes; i++) {
1310 keycode = keycodes[i];
1311 if (keycode & 0x80)
1312 kbd_put_keycode(0xe0);
1313 kbd_put_keycode(keycode & 0x7f);
1315 /* delayed key up events */
1316 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1317 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1320 static int mouse_button_state;
1322 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1324 int dx, dy, dz;
1325 const char *dx_str = qdict_get_str(qdict, "dx_str");
1326 const char *dy_str = qdict_get_str(qdict, "dy_str");
1327 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1328 dx = strtol(dx_str, NULL, 0);
1329 dy = strtol(dy_str, NULL, 0);
1330 dz = 0;
1331 if (dz_str)
1332 dz = strtol(dz_str, NULL, 0);
1333 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1336 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1338 int button_state = qdict_get_int(qdict, "button_state");
1339 mouse_button_state = button_state;
1340 kbd_mouse_event(0, 0, 0, mouse_button_state);
1343 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1345 int size = qdict_get_int(qdict, "size");
1346 int addr = qdict_get_int(qdict, "addr");
1347 int has_index = qdict_haskey(qdict, "index");
1348 uint32_t val;
1349 int suffix;
1351 if (has_index) {
1352 int index = qdict_get_int(qdict, "index");
1353 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1354 addr++;
1356 addr &= 0xffff;
1358 switch(size) {
1359 default:
1360 case 1:
1361 val = cpu_inb(addr);
1362 suffix = 'b';
1363 break;
1364 case 2:
1365 val = cpu_inw(addr);
1366 suffix = 'w';
1367 break;
1368 case 4:
1369 val = cpu_inl(addr);
1370 suffix = 'l';
1371 break;
1373 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1374 suffix, addr, size * 2, val);
1377 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1379 int size = qdict_get_int(qdict, "size");
1380 int addr = qdict_get_int(qdict, "addr");
1381 int val = qdict_get_int(qdict, "val");
1383 addr &= IOPORTS_MASK;
1385 switch (size) {
1386 default:
1387 case 1:
1388 cpu_outb(addr, val);
1389 break;
1390 case 2:
1391 cpu_outw(addr, val);
1392 break;
1393 case 4:
1394 cpu_outl(addr, val);
1395 break;
1399 static void do_boot_set(Monitor *mon, const QDict *qdict)
1401 int res;
1402 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1404 res = qemu_boot_set(bootdevice);
1405 if (res == 0) {
1406 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1407 } else if (res > 0) {
1408 monitor_printf(mon, "setting boot device list failed\n");
1409 } else {
1410 monitor_printf(mon, "no function defined to set boot device list for "
1411 "this architecture\n");
1416 * do_system_reset(): Issue a machine reset
1418 static void do_system_reset(Monitor *mon, const QDict *qdict,
1419 QObject **ret_data)
1421 qemu_system_reset_request();
1425 * do_system_powerdown(): Issue a machine powerdown
1427 static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1428 QObject **ret_data)
1430 qemu_system_powerdown_request();
1433 #if defined(TARGET_I386)
1434 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1436 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1437 addr,
1438 pte & mask,
1439 pte & PG_GLOBAL_MASK ? 'G' : '-',
1440 pte & PG_PSE_MASK ? 'P' : '-',
1441 pte & PG_DIRTY_MASK ? 'D' : '-',
1442 pte & PG_ACCESSED_MASK ? 'A' : '-',
1443 pte & PG_PCD_MASK ? 'C' : '-',
1444 pte & PG_PWT_MASK ? 'T' : '-',
1445 pte & PG_USER_MASK ? 'U' : '-',
1446 pte & PG_RW_MASK ? 'W' : '-');
1449 static void tlb_info(Monitor *mon)
1451 CPUState *env;
1452 int l1, l2;
1453 uint32_t pgd, pde, pte;
1455 env = mon_get_cpu();
1456 if (!env)
1457 return;
1459 if (!(env->cr[0] & CR0_PG_MASK)) {
1460 monitor_printf(mon, "PG disabled\n");
1461 return;
1463 pgd = env->cr[3] & ~0xfff;
1464 for(l1 = 0; l1 < 1024; l1++) {
1465 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1466 pde = le32_to_cpu(pde);
1467 if (pde & PG_PRESENT_MASK) {
1468 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1469 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1470 } else {
1471 for(l2 = 0; l2 < 1024; l2++) {
1472 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1473 (uint8_t *)&pte, 4);
1474 pte = le32_to_cpu(pte);
1475 if (pte & PG_PRESENT_MASK) {
1476 print_pte(mon, (l1 << 22) + (l2 << 12),
1477 pte & ~PG_PSE_MASK,
1478 ~0xfff);
1486 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1487 uint32_t end, int prot)
1489 int prot1;
1490 prot1 = *plast_prot;
1491 if (prot != prot1) {
1492 if (*pstart != -1) {
1493 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1494 *pstart, end, end - *pstart,
1495 prot1 & PG_USER_MASK ? 'u' : '-',
1496 'r',
1497 prot1 & PG_RW_MASK ? 'w' : '-');
1499 if (prot != 0)
1500 *pstart = end;
1501 else
1502 *pstart = -1;
1503 *plast_prot = prot;
1507 static void mem_info(Monitor *mon)
1509 CPUState *env;
1510 int l1, l2, prot, last_prot;
1511 uint32_t pgd, pde, pte, start, end;
1513 env = mon_get_cpu();
1514 if (!env)
1515 return;
1517 if (!(env->cr[0] & CR0_PG_MASK)) {
1518 monitor_printf(mon, "PG disabled\n");
1519 return;
1521 pgd = env->cr[3] & ~0xfff;
1522 last_prot = 0;
1523 start = -1;
1524 for(l1 = 0; l1 < 1024; l1++) {
1525 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1526 pde = le32_to_cpu(pde);
1527 end = l1 << 22;
1528 if (pde & PG_PRESENT_MASK) {
1529 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1530 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1531 mem_print(mon, &start, &last_prot, end, prot);
1532 } else {
1533 for(l2 = 0; l2 < 1024; l2++) {
1534 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1535 (uint8_t *)&pte, 4);
1536 pte = le32_to_cpu(pte);
1537 end = (l1 << 22) + (l2 << 12);
1538 if (pte & PG_PRESENT_MASK) {
1539 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1540 } else {
1541 prot = 0;
1543 mem_print(mon, &start, &last_prot, end, prot);
1546 } else {
1547 prot = 0;
1548 mem_print(mon, &start, &last_prot, end, prot);
1552 #endif
1554 #if defined(TARGET_SH4)
1556 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1558 monitor_printf(mon, " tlb%i:\t"
1559 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1560 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1561 "dirty=%hhu writethrough=%hhu\n",
1562 idx,
1563 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1564 tlb->v, tlb->sh, tlb->c, tlb->pr,
1565 tlb->d, tlb->wt);
1568 static void tlb_info(Monitor *mon)
1570 CPUState *env = mon_get_cpu();
1571 int i;
1573 monitor_printf (mon, "ITLB:\n");
1574 for (i = 0 ; i < ITLB_SIZE ; i++)
1575 print_tlb (mon, i, &env->itlb[i]);
1576 monitor_printf (mon, "UTLB:\n");
1577 for (i = 0 ; i < UTLB_SIZE ; i++)
1578 print_tlb (mon, i, &env->utlb[i]);
1581 #endif
1583 static void do_info_kvm(Monitor *mon)
1585 #if defined(USE_KVM) || defined(CONFIG_KVM)
1586 monitor_printf(mon, "kvm support: ");
1587 if (kvm_enabled())
1588 monitor_printf(mon, "enabled\n");
1589 else
1590 monitor_printf(mon, "disabled\n");
1591 #else
1592 monitor_printf(mon, "kvm support: not compiled\n");
1593 #endif
1596 static void do_info_numa(Monitor *mon)
1598 int i;
1599 CPUState *env;
1601 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1602 for (i = 0; i < nb_numa_nodes; i++) {
1603 monitor_printf(mon, "node %d cpus:", i);
1604 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1605 if (env->numa_node == i) {
1606 monitor_printf(mon, " %d", env->cpu_index);
1609 monitor_printf(mon, "\n");
1610 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1611 node_mem[i] >> 20);
1615 #ifdef CONFIG_PROFILER
1617 int64_t qemu_time;
1618 int64_t dev_time;
1620 static void do_info_profile(Monitor *mon)
1622 int64_t total;
1623 total = qemu_time;
1624 if (total == 0)
1625 total = 1;
1626 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1627 dev_time, dev_time / (double)get_ticks_per_sec());
1628 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1629 qemu_time, qemu_time / (double)get_ticks_per_sec());
1630 qemu_time = 0;
1631 dev_time = 0;
1633 #else
1634 static void do_info_profile(Monitor *mon)
1636 monitor_printf(mon, "Internal profiler not compiled\n");
1638 #endif
1640 /* Capture support */
1641 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1643 static void do_info_capture(Monitor *mon)
1645 int i;
1646 CaptureState *s;
1648 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1649 monitor_printf(mon, "[%d]: ", i);
1650 s->ops.info (s->opaque);
1654 #ifdef HAS_AUDIO
1655 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1657 int i;
1658 int n = qdict_get_int(qdict, "n");
1659 CaptureState *s;
1661 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1662 if (i == n) {
1663 s->ops.destroy (s->opaque);
1664 QLIST_REMOVE (s, entries);
1665 qemu_free (s);
1666 return;
1671 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1673 const char *path = qdict_get_str(qdict, "path");
1674 int has_freq = qdict_haskey(qdict, "freq");
1675 int freq = qdict_get_try_int(qdict, "freq", -1);
1676 int has_bits = qdict_haskey(qdict, "bits");
1677 int bits = qdict_get_try_int(qdict, "bits", -1);
1678 int has_channels = qdict_haskey(qdict, "nchannels");
1679 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1680 CaptureState *s;
1682 s = qemu_mallocz (sizeof (*s));
1684 freq = has_freq ? freq : 44100;
1685 bits = has_bits ? bits : 16;
1686 nchannels = has_channels ? nchannels : 2;
1688 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1689 monitor_printf(mon, "Faied to add wave capture\n");
1690 qemu_free (s);
1692 QLIST_INSERT_HEAD (&capture_head, s, entries);
1694 #endif
1696 #if defined(TARGET_I386)
1697 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1699 CPUState *env;
1700 int cpu_index = qdict_get_int(qdict, "cpu_index");
1702 for (env = first_cpu; env != NULL; env = env->next_cpu)
1703 if (env->cpu_index == cpu_index) {
1704 if (kvm_enabled())
1705 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1706 else
1707 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1708 break;
1711 #endif
1713 static void do_info_status(Monitor *mon)
1715 if (vm_running) {
1716 if (singlestep) {
1717 monitor_printf(mon, "VM status: running (single step mode)\n");
1718 } else {
1719 monitor_printf(mon, "VM status: running\n");
1721 } else
1722 monitor_printf(mon, "VM status: paused\n");
1726 * do_balloon(): Request VM to change its memory allocation
1728 static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
1730 int value = qdict_get_int(qdict, "value");
1731 ram_addr_t target = value;
1732 qemu_balloon(target << 20);
1735 static void monitor_print_balloon(Monitor *mon, const QObject *data)
1737 monitor_printf(mon, "balloon: actual=%d\n",
1738 (int)qint_get_int(qobject_to_qint(data)));
1742 * do_info_balloon(): Balloon information
1744 static void do_info_balloon(Monitor *mon, QObject **ret_data)
1746 ram_addr_t actual;
1748 actual = qemu_balloon_status();
1749 if (kvm_enabled() && !kvm_has_sync_mmu())
1750 monitor_printf(mon, "Using KVM without synchronous MMU, "
1751 "ballooning disabled\n");
1752 else if (actual == 0)
1753 monitor_printf(mon, "Ballooning not activated in VM\n");
1754 else
1755 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
1758 static qemu_acl *find_acl(Monitor *mon, const char *name)
1760 qemu_acl *acl = qemu_acl_find(name);
1762 if (!acl) {
1763 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1765 return acl;
1768 static void do_acl_show(Monitor *mon, const QDict *qdict)
1770 const char *aclname = qdict_get_str(qdict, "aclname");
1771 qemu_acl *acl = find_acl(mon, aclname);
1772 qemu_acl_entry *entry;
1773 int i = 0;
1775 if (acl) {
1776 monitor_printf(mon, "policy: %s\n",
1777 acl->defaultDeny ? "deny" : "allow");
1778 QTAILQ_FOREACH(entry, &acl->entries, next) {
1779 i++;
1780 monitor_printf(mon, "%d: %s %s\n", i,
1781 entry->deny ? "deny" : "allow", entry->match);
1786 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1788 const char *aclname = qdict_get_str(qdict, "aclname");
1789 qemu_acl *acl = find_acl(mon, aclname);
1791 if (acl) {
1792 qemu_acl_reset(acl);
1793 monitor_printf(mon, "acl: removed all rules\n");
1797 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1799 const char *aclname = qdict_get_str(qdict, "aclname");
1800 const char *policy = qdict_get_str(qdict, "policy");
1801 qemu_acl *acl = find_acl(mon, aclname);
1803 if (acl) {
1804 if (strcmp(policy, "allow") == 0) {
1805 acl->defaultDeny = 0;
1806 monitor_printf(mon, "acl: policy set to 'allow'\n");
1807 } else if (strcmp(policy, "deny") == 0) {
1808 acl->defaultDeny = 1;
1809 monitor_printf(mon, "acl: policy set to 'deny'\n");
1810 } else {
1811 monitor_printf(mon, "acl: unknown policy '%s', "
1812 "expected 'deny' or 'allow'\n", policy);
1817 static void do_acl_add(Monitor *mon, const QDict *qdict)
1819 const char *aclname = qdict_get_str(qdict, "aclname");
1820 const char *match = qdict_get_str(qdict, "match");
1821 const char *policy = qdict_get_str(qdict, "policy");
1822 int has_index = qdict_haskey(qdict, "index");
1823 int index = qdict_get_try_int(qdict, "index", -1);
1824 qemu_acl *acl = find_acl(mon, aclname);
1825 int deny, ret;
1827 if (acl) {
1828 if (strcmp(policy, "allow") == 0) {
1829 deny = 0;
1830 } else if (strcmp(policy, "deny") == 0) {
1831 deny = 1;
1832 } else {
1833 monitor_printf(mon, "acl: unknown policy '%s', "
1834 "expected 'deny' or 'allow'\n", policy);
1835 return;
1837 if (has_index)
1838 ret = qemu_acl_insert(acl, deny, match, index);
1839 else
1840 ret = qemu_acl_append(acl, deny, match);
1841 if (ret < 0)
1842 monitor_printf(mon, "acl: unable to add acl entry\n");
1843 else
1844 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1848 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1850 const char *aclname = qdict_get_str(qdict, "aclname");
1851 const char *match = qdict_get_str(qdict, "match");
1852 qemu_acl *acl = find_acl(mon, aclname);
1853 int ret;
1855 if (acl) {
1856 ret = qemu_acl_remove(acl, match);
1857 if (ret < 0)
1858 monitor_printf(mon, "acl: no matching acl entry\n");
1859 else
1860 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1864 #if defined(TARGET_I386)
1865 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1867 CPUState *cenv;
1868 int cpu_index = qdict_get_int(qdict, "cpu_index");
1869 int bank = qdict_get_int(qdict, "bank");
1870 uint64_t status = qdict_get_int(qdict, "status");
1871 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1872 uint64_t addr = qdict_get_int(qdict, "addr");
1873 uint64_t misc = qdict_get_int(qdict, "misc");
1875 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1876 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1877 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1878 break;
1881 #endif
1883 static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
1885 const char *fdname = qdict_get_str(qdict, "fdname");
1886 mon_fd_t *monfd;
1887 int fd;
1889 fd = qemu_chr_get_msgfd(mon->chr);
1890 if (fd == -1) {
1891 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1892 return;
1895 if (qemu_isdigit(fdname[0])) {
1896 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1897 return;
1900 fd = dup(fd);
1901 if (fd == -1) {
1902 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1903 strerror(errno));
1904 return;
1907 QLIST_FOREACH(monfd, &mon->fds, next) {
1908 if (strcmp(monfd->name, fdname) != 0) {
1909 continue;
1912 close(monfd->fd);
1913 monfd->fd = fd;
1914 return;
1917 monfd = qemu_mallocz(sizeof(mon_fd_t));
1918 monfd->name = qemu_strdup(fdname);
1919 monfd->fd = fd;
1921 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1924 static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
1926 const char *fdname = qdict_get_str(qdict, "fdname");
1927 mon_fd_t *monfd;
1929 QLIST_FOREACH(monfd, &mon->fds, next) {
1930 if (strcmp(monfd->name, fdname) != 0) {
1931 continue;
1934 QLIST_REMOVE(monfd, next);
1935 close(monfd->fd);
1936 qemu_free(monfd->name);
1937 qemu_free(monfd);
1938 return;
1941 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1942 fdname);
1945 static void do_loadvm(Monitor *mon, const QDict *qdict)
1947 int saved_vm_running = vm_running;
1948 const char *name = qdict_get_str(qdict, "name");
1950 vm_stop(0);
1952 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1953 vm_start();
1956 int monitor_get_fd(Monitor *mon, const char *fdname)
1958 mon_fd_t *monfd;
1960 QLIST_FOREACH(monfd, &mon->fds, next) {
1961 int fd;
1963 if (strcmp(monfd->name, fdname) != 0) {
1964 continue;
1967 fd = monfd->fd;
1969 /* caller takes ownership of fd */
1970 QLIST_REMOVE(monfd, next);
1971 qemu_free(monfd->name);
1972 qemu_free(monfd);
1974 return fd;
1977 return -1;
1980 static const mon_cmd_t mon_cmds[] = {
1981 #include "qemu-monitor.h"
1982 { NULL, NULL, },
1985 /* Please update qemu-monitor.hx when adding or changing commands */
1986 static const mon_cmd_t info_cmds[] = {
1988 .name = "version",
1989 .args_type = "",
1990 .params = "",
1991 .help = "show the version of QEMU",
1992 .user_print = monitor_print_qobject,
1993 .mhandler.info_new = do_info_version,
1996 .name = "network",
1997 .args_type = "",
1998 .params = "",
1999 .help = "show the network state",
2000 .mhandler.info = do_info_network,
2003 .name = "chardev",
2004 .args_type = "",
2005 .params = "",
2006 .help = "show the character devices",
2007 .mhandler.info = qemu_chr_info,
2010 .name = "block",
2011 .args_type = "",
2012 .params = "",
2013 .help = "show the block devices",
2014 .mhandler.info = bdrv_info,
2017 .name = "blockstats",
2018 .args_type = "",
2019 .params = "",
2020 .help = "show block device statistics",
2021 .mhandler.info = bdrv_info_stats,
2024 .name = "registers",
2025 .args_type = "",
2026 .params = "",
2027 .help = "show the cpu registers",
2028 .mhandler.info = do_info_registers,
2031 .name = "cpus",
2032 .args_type = "",
2033 .params = "",
2034 .help = "show infos for each CPU",
2035 .user_print = monitor_print_cpus,
2036 .mhandler.info_new = do_info_cpus,
2039 .name = "history",
2040 .args_type = "",
2041 .params = "",
2042 .help = "show the command line history",
2043 .mhandler.info = do_info_history,
2046 .name = "irq",
2047 .args_type = "",
2048 .params = "",
2049 .help = "show the interrupts statistics (if available)",
2050 .mhandler.info = irq_info,
2053 .name = "pic",
2054 .args_type = "",
2055 .params = "",
2056 .help = "show i8259 (PIC) state",
2057 .mhandler.info = pic_info,
2060 .name = "pci",
2061 .args_type = "",
2062 .params = "",
2063 .help = "show PCI info",
2064 .mhandler.info = pci_info,
2066 #if defined(TARGET_I386) || defined(TARGET_SH4)
2068 .name = "tlb",
2069 .args_type = "",
2070 .params = "",
2071 .help = "show virtual to physical memory mappings",
2072 .mhandler.info = tlb_info,
2074 #endif
2075 #if defined(TARGET_I386)
2077 .name = "mem",
2078 .args_type = "",
2079 .params = "",
2080 .help = "show the active virtual memory mappings",
2081 .mhandler.info = mem_info,
2084 .name = "hpet",
2085 .args_type = "",
2086 .params = "",
2087 .help = "show state of HPET",
2088 .mhandler.info = do_info_hpet,
2090 #endif
2092 .name = "jit",
2093 .args_type = "",
2094 .params = "",
2095 .help = "show dynamic compiler info",
2096 .mhandler.info = do_info_jit,
2099 .name = "kvm",
2100 .args_type = "",
2101 .params = "",
2102 .help = "show KVM information",
2103 .mhandler.info = do_info_kvm,
2106 .name = "numa",
2107 .args_type = "",
2108 .params = "",
2109 .help = "show NUMA information",
2110 .mhandler.info = do_info_numa,
2113 .name = "usb",
2114 .args_type = "",
2115 .params = "",
2116 .help = "show guest USB devices",
2117 .mhandler.info = usb_info,
2120 .name = "usbhost",
2121 .args_type = "",
2122 .params = "",
2123 .help = "show host USB devices",
2124 .mhandler.info = usb_host_info,
2127 .name = "profile",
2128 .args_type = "",
2129 .params = "",
2130 .help = "show profiling information",
2131 .mhandler.info = do_info_profile,
2134 .name = "capture",
2135 .args_type = "",
2136 .params = "",
2137 .help = "show capture information",
2138 .mhandler.info = do_info_capture,
2141 .name = "snapshots",
2142 .args_type = "",
2143 .params = "",
2144 .help = "show the currently saved VM snapshots",
2145 .mhandler.info = do_info_snapshots,
2148 .name = "status",
2149 .args_type = "",
2150 .params = "",
2151 .help = "show the current VM status (running|paused)",
2152 .mhandler.info = do_info_status,
2155 .name = "pcmcia",
2156 .args_type = "",
2157 .params = "",
2158 .help = "show guest PCMCIA status",
2159 .mhandler.info = pcmcia_info,
2162 .name = "mice",
2163 .args_type = "",
2164 .params = "",
2165 .help = "show which guest mouse is receiving events",
2166 .mhandler.info = do_info_mice,
2169 .name = "vnc",
2170 .args_type = "",
2171 .params = "",
2172 .help = "show the vnc server status",
2173 .mhandler.info = do_info_vnc,
2176 .name = "name",
2177 .args_type = "",
2178 .params = "",
2179 .help = "show the current VM name",
2180 .mhandler.info = do_info_name,
2183 .name = "uuid",
2184 .args_type = "",
2185 .params = "",
2186 .help = "show the current VM UUID",
2187 .mhandler.info = do_info_uuid,
2189 #if defined(TARGET_PPC)
2191 .name = "cpustats",
2192 .args_type = "",
2193 .params = "",
2194 .help = "show CPU statistics",
2195 .mhandler.info = do_info_cpu_stats,
2197 #endif
2198 #if defined(CONFIG_SLIRP)
2200 .name = "usernet",
2201 .args_type = "",
2202 .params = "",
2203 .help = "show user network stack connection states",
2204 .mhandler.info = do_info_usernet,
2206 #endif
2208 .name = "migrate",
2209 .args_type = "",
2210 .params = "",
2211 .help = "show migration status",
2212 .mhandler.info = do_info_migrate,
2215 .name = "balloon",
2216 .args_type = "",
2217 .params = "",
2218 .help = "show balloon information",
2219 .user_print = monitor_print_balloon,
2220 .mhandler.info_new = do_info_balloon,
2223 .name = "qtree",
2224 .args_type = "",
2225 .params = "",
2226 .help = "show device tree",
2227 .mhandler.info = do_info_qtree,
2230 .name = "qdm",
2231 .args_type = "",
2232 .params = "",
2233 .help = "show qdev device model list",
2234 .mhandler.info = do_info_qdm,
2237 .name = "roms",
2238 .args_type = "",
2239 .params = "",
2240 .help = "show roms",
2241 .mhandler.info = do_info_roms,
2244 .name = NULL,
2248 /*******************************************************************/
2250 static const char *pch;
2251 static jmp_buf expr_env;
2253 #define MD_TLONG 0
2254 #define MD_I32 1
2256 typedef struct MonitorDef {
2257 const char *name;
2258 int offset;
2259 target_long (*get_value)(const struct MonitorDef *md, int val);
2260 int type;
2261 } MonitorDef;
2263 #if defined(TARGET_I386)
2264 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2266 CPUState *env = mon_get_cpu();
2267 if (!env)
2268 return 0;
2269 return env->eip + env->segs[R_CS].base;
2271 #endif
2273 #if defined(TARGET_PPC)
2274 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2276 CPUState *env = mon_get_cpu();
2277 unsigned int u;
2278 int i;
2280 if (!env)
2281 return 0;
2283 u = 0;
2284 for (i = 0; i < 8; i++)
2285 u |= env->crf[i] << (32 - (4 * i));
2287 return u;
2290 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2292 CPUState *env = mon_get_cpu();
2293 if (!env)
2294 return 0;
2295 return env->msr;
2298 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2300 CPUState *env = mon_get_cpu();
2301 if (!env)
2302 return 0;
2303 return env->xer;
2306 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2308 CPUState *env = mon_get_cpu();
2309 if (!env)
2310 return 0;
2311 return cpu_ppc_load_decr(env);
2314 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2316 CPUState *env = mon_get_cpu();
2317 if (!env)
2318 return 0;
2319 return cpu_ppc_load_tbu(env);
2322 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2324 CPUState *env = mon_get_cpu();
2325 if (!env)
2326 return 0;
2327 return cpu_ppc_load_tbl(env);
2329 #endif
2331 #if defined(TARGET_SPARC)
2332 #ifndef TARGET_SPARC64
2333 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2335 CPUState *env = mon_get_cpu();
2336 if (!env)
2337 return 0;
2338 return GET_PSR(env);
2340 #endif
2342 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2344 CPUState *env = mon_get_cpu();
2345 if (!env)
2346 return 0;
2347 return env->regwptr[val];
2349 #endif
2351 static const MonitorDef monitor_defs[] = {
2352 #ifdef TARGET_I386
2354 #define SEG(name, seg) \
2355 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2356 { name ".base", offsetof(CPUState, segs[seg].base) },\
2357 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2359 { "eax", offsetof(CPUState, regs[0]) },
2360 { "ecx", offsetof(CPUState, regs[1]) },
2361 { "edx", offsetof(CPUState, regs[2]) },
2362 { "ebx", offsetof(CPUState, regs[3]) },
2363 { "esp|sp", offsetof(CPUState, regs[4]) },
2364 { "ebp|fp", offsetof(CPUState, regs[5]) },
2365 { "esi", offsetof(CPUState, regs[6]) },
2366 { "edi", offsetof(CPUState, regs[7]) },
2367 #ifdef TARGET_X86_64
2368 { "r8", offsetof(CPUState, regs[8]) },
2369 { "r9", offsetof(CPUState, regs[9]) },
2370 { "r10", offsetof(CPUState, regs[10]) },
2371 { "r11", offsetof(CPUState, regs[11]) },
2372 { "r12", offsetof(CPUState, regs[12]) },
2373 { "r13", offsetof(CPUState, regs[13]) },
2374 { "r14", offsetof(CPUState, regs[14]) },
2375 { "r15", offsetof(CPUState, regs[15]) },
2376 #endif
2377 { "eflags", offsetof(CPUState, eflags) },
2378 { "eip", offsetof(CPUState, eip) },
2379 SEG("cs", R_CS)
2380 SEG("ds", R_DS)
2381 SEG("es", R_ES)
2382 SEG("ss", R_SS)
2383 SEG("fs", R_FS)
2384 SEG("gs", R_GS)
2385 { "pc", 0, monitor_get_pc, },
2386 #elif defined(TARGET_PPC)
2387 /* General purpose registers */
2388 { "r0", offsetof(CPUState, gpr[0]) },
2389 { "r1", offsetof(CPUState, gpr[1]) },
2390 { "r2", offsetof(CPUState, gpr[2]) },
2391 { "r3", offsetof(CPUState, gpr[3]) },
2392 { "r4", offsetof(CPUState, gpr[4]) },
2393 { "r5", offsetof(CPUState, gpr[5]) },
2394 { "r6", offsetof(CPUState, gpr[6]) },
2395 { "r7", offsetof(CPUState, gpr[7]) },
2396 { "r8", offsetof(CPUState, gpr[8]) },
2397 { "r9", offsetof(CPUState, gpr[9]) },
2398 { "r10", offsetof(CPUState, gpr[10]) },
2399 { "r11", offsetof(CPUState, gpr[11]) },
2400 { "r12", offsetof(CPUState, gpr[12]) },
2401 { "r13", offsetof(CPUState, gpr[13]) },
2402 { "r14", offsetof(CPUState, gpr[14]) },
2403 { "r15", offsetof(CPUState, gpr[15]) },
2404 { "r16", offsetof(CPUState, gpr[16]) },
2405 { "r17", offsetof(CPUState, gpr[17]) },
2406 { "r18", offsetof(CPUState, gpr[18]) },
2407 { "r19", offsetof(CPUState, gpr[19]) },
2408 { "r20", offsetof(CPUState, gpr[20]) },
2409 { "r21", offsetof(CPUState, gpr[21]) },
2410 { "r22", offsetof(CPUState, gpr[22]) },
2411 { "r23", offsetof(CPUState, gpr[23]) },
2412 { "r24", offsetof(CPUState, gpr[24]) },
2413 { "r25", offsetof(CPUState, gpr[25]) },
2414 { "r26", offsetof(CPUState, gpr[26]) },
2415 { "r27", offsetof(CPUState, gpr[27]) },
2416 { "r28", offsetof(CPUState, gpr[28]) },
2417 { "r29", offsetof(CPUState, gpr[29]) },
2418 { "r30", offsetof(CPUState, gpr[30]) },
2419 { "r31", offsetof(CPUState, gpr[31]) },
2420 /* Floating point registers */
2421 { "f0", offsetof(CPUState, fpr[0]) },
2422 { "f1", offsetof(CPUState, fpr[1]) },
2423 { "f2", offsetof(CPUState, fpr[2]) },
2424 { "f3", offsetof(CPUState, fpr[3]) },
2425 { "f4", offsetof(CPUState, fpr[4]) },
2426 { "f5", offsetof(CPUState, fpr[5]) },
2427 { "f6", offsetof(CPUState, fpr[6]) },
2428 { "f7", offsetof(CPUState, fpr[7]) },
2429 { "f8", offsetof(CPUState, fpr[8]) },
2430 { "f9", offsetof(CPUState, fpr[9]) },
2431 { "f10", offsetof(CPUState, fpr[10]) },
2432 { "f11", offsetof(CPUState, fpr[11]) },
2433 { "f12", offsetof(CPUState, fpr[12]) },
2434 { "f13", offsetof(CPUState, fpr[13]) },
2435 { "f14", offsetof(CPUState, fpr[14]) },
2436 { "f15", offsetof(CPUState, fpr[15]) },
2437 { "f16", offsetof(CPUState, fpr[16]) },
2438 { "f17", offsetof(CPUState, fpr[17]) },
2439 { "f18", offsetof(CPUState, fpr[18]) },
2440 { "f19", offsetof(CPUState, fpr[19]) },
2441 { "f20", offsetof(CPUState, fpr[20]) },
2442 { "f21", offsetof(CPUState, fpr[21]) },
2443 { "f22", offsetof(CPUState, fpr[22]) },
2444 { "f23", offsetof(CPUState, fpr[23]) },
2445 { "f24", offsetof(CPUState, fpr[24]) },
2446 { "f25", offsetof(CPUState, fpr[25]) },
2447 { "f26", offsetof(CPUState, fpr[26]) },
2448 { "f27", offsetof(CPUState, fpr[27]) },
2449 { "f28", offsetof(CPUState, fpr[28]) },
2450 { "f29", offsetof(CPUState, fpr[29]) },
2451 { "f30", offsetof(CPUState, fpr[30]) },
2452 { "f31", offsetof(CPUState, fpr[31]) },
2453 { "fpscr", offsetof(CPUState, fpscr) },
2454 /* Next instruction pointer */
2455 { "nip|pc", offsetof(CPUState, nip) },
2456 { "lr", offsetof(CPUState, lr) },
2457 { "ctr", offsetof(CPUState, ctr) },
2458 { "decr", 0, &monitor_get_decr, },
2459 { "ccr", 0, &monitor_get_ccr, },
2460 /* Machine state register */
2461 { "msr", 0, &monitor_get_msr, },
2462 { "xer", 0, &monitor_get_xer, },
2463 { "tbu", 0, &monitor_get_tbu, },
2464 { "tbl", 0, &monitor_get_tbl, },
2465 #if defined(TARGET_PPC64)
2466 /* Address space register */
2467 { "asr", offsetof(CPUState, asr) },
2468 #endif
2469 /* Segment registers */
2470 { "sdr1", offsetof(CPUState, sdr1) },
2471 { "sr0", offsetof(CPUState, sr[0]) },
2472 { "sr1", offsetof(CPUState, sr[1]) },
2473 { "sr2", offsetof(CPUState, sr[2]) },
2474 { "sr3", offsetof(CPUState, sr[3]) },
2475 { "sr4", offsetof(CPUState, sr[4]) },
2476 { "sr5", offsetof(CPUState, sr[5]) },
2477 { "sr6", offsetof(CPUState, sr[6]) },
2478 { "sr7", offsetof(CPUState, sr[7]) },
2479 { "sr8", offsetof(CPUState, sr[8]) },
2480 { "sr9", offsetof(CPUState, sr[9]) },
2481 { "sr10", offsetof(CPUState, sr[10]) },
2482 { "sr11", offsetof(CPUState, sr[11]) },
2483 { "sr12", offsetof(CPUState, sr[12]) },
2484 { "sr13", offsetof(CPUState, sr[13]) },
2485 { "sr14", offsetof(CPUState, sr[14]) },
2486 { "sr15", offsetof(CPUState, sr[15]) },
2487 /* Too lazy to put BATs and SPRs ... */
2488 #elif defined(TARGET_SPARC)
2489 { "g0", offsetof(CPUState, gregs[0]) },
2490 { "g1", offsetof(CPUState, gregs[1]) },
2491 { "g2", offsetof(CPUState, gregs[2]) },
2492 { "g3", offsetof(CPUState, gregs[3]) },
2493 { "g4", offsetof(CPUState, gregs[4]) },
2494 { "g5", offsetof(CPUState, gregs[5]) },
2495 { "g6", offsetof(CPUState, gregs[6]) },
2496 { "g7", offsetof(CPUState, gregs[7]) },
2497 { "o0", 0, monitor_get_reg },
2498 { "o1", 1, monitor_get_reg },
2499 { "o2", 2, monitor_get_reg },
2500 { "o3", 3, monitor_get_reg },
2501 { "o4", 4, monitor_get_reg },
2502 { "o5", 5, monitor_get_reg },
2503 { "o6", 6, monitor_get_reg },
2504 { "o7", 7, monitor_get_reg },
2505 { "l0", 8, monitor_get_reg },
2506 { "l1", 9, monitor_get_reg },
2507 { "l2", 10, monitor_get_reg },
2508 { "l3", 11, monitor_get_reg },
2509 { "l4", 12, monitor_get_reg },
2510 { "l5", 13, monitor_get_reg },
2511 { "l6", 14, monitor_get_reg },
2512 { "l7", 15, monitor_get_reg },
2513 { "i0", 16, monitor_get_reg },
2514 { "i1", 17, monitor_get_reg },
2515 { "i2", 18, monitor_get_reg },
2516 { "i3", 19, monitor_get_reg },
2517 { "i4", 20, monitor_get_reg },
2518 { "i5", 21, monitor_get_reg },
2519 { "i6", 22, monitor_get_reg },
2520 { "i7", 23, monitor_get_reg },
2521 { "pc", offsetof(CPUState, pc) },
2522 { "npc", offsetof(CPUState, npc) },
2523 { "y", offsetof(CPUState, y) },
2524 #ifndef TARGET_SPARC64
2525 { "psr", 0, &monitor_get_psr, },
2526 { "wim", offsetof(CPUState, wim) },
2527 #endif
2528 { "tbr", offsetof(CPUState, tbr) },
2529 { "fsr", offsetof(CPUState, fsr) },
2530 { "f0", offsetof(CPUState, fpr[0]) },
2531 { "f1", offsetof(CPUState, fpr[1]) },
2532 { "f2", offsetof(CPUState, fpr[2]) },
2533 { "f3", offsetof(CPUState, fpr[3]) },
2534 { "f4", offsetof(CPUState, fpr[4]) },
2535 { "f5", offsetof(CPUState, fpr[5]) },
2536 { "f6", offsetof(CPUState, fpr[6]) },
2537 { "f7", offsetof(CPUState, fpr[7]) },
2538 { "f8", offsetof(CPUState, fpr[8]) },
2539 { "f9", offsetof(CPUState, fpr[9]) },
2540 { "f10", offsetof(CPUState, fpr[10]) },
2541 { "f11", offsetof(CPUState, fpr[11]) },
2542 { "f12", offsetof(CPUState, fpr[12]) },
2543 { "f13", offsetof(CPUState, fpr[13]) },
2544 { "f14", offsetof(CPUState, fpr[14]) },
2545 { "f15", offsetof(CPUState, fpr[15]) },
2546 { "f16", offsetof(CPUState, fpr[16]) },
2547 { "f17", offsetof(CPUState, fpr[17]) },
2548 { "f18", offsetof(CPUState, fpr[18]) },
2549 { "f19", offsetof(CPUState, fpr[19]) },
2550 { "f20", offsetof(CPUState, fpr[20]) },
2551 { "f21", offsetof(CPUState, fpr[21]) },
2552 { "f22", offsetof(CPUState, fpr[22]) },
2553 { "f23", offsetof(CPUState, fpr[23]) },
2554 { "f24", offsetof(CPUState, fpr[24]) },
2555 { "f25", offsetof(CPUState, fpr[25]) },
2556 { "f26", offsetof(CPUState, fpr[26]) },
2557 { "f27", offsetof(CPUState, fpr[27]) },
2558 { "f28", offsetof(CPUState, fpr[28]) },
2559 { "f29", offsetof(CPUState, fpr[29]) },
2560 { "f30", offsetof(CPUState, fpr[30]) },
2561 { "f31", offsetof(CPUState, fpr[31]) },
2562 #ifdef TARGET_SPARC64
2563 { "f32", offsetof(CPUState, fpr[32]) },
2564 { "f34", offsetof(CPUState, fpr[34]) },
2565 { "f36", offsetof(CPUState, fpr[36]) },
2566 { "f38", offsetof(CPUState, fpr[38]) },
2567 { "f40", offsetof(CPUState, fpr[40]) },
2568 { "f42", offsetof(CPUState, fpr[42]) },
2569 { "f44", offsetof(CPUState, fpr[44]) },
2570 { "f46", offsetof(CPUState, fpr[46]) },
2571 { "f48", offsetof(CPUState, fpr[48]) },
2572 { "f50", offsetof(CPUState, fpr[50]) },
2573 { "f52", offsetof(CPUState, fpr[52]) },
2574 { "f54", offsetof(CPUState, fpr[54]) },
2575 { "f56", offsetof(CPUState, fpr[56]) },
2576 { "f58", offsetof(CPUState, fpr[58]) },
2577 { "f60", offsetof(CPUState, fpr[60]) },
2578 { "f62", offsetof(CPUState, fpr[62]) },
2579 { "asi", offsetof(CPUState, asi) },
2580 { "pstate", offsetof(CPUState, pstate) },
2581 { "cansave", offsetof(CPUState, cansave) },
2582 { "canrestore", offsetof(CPUState, canrestore) },
2583 { "otherwin", offsetof(CPUState, otherwin) },
2584 { "wstate", offsetof(CPUState, wstate) },
2585 { "cleanwin", offsetof(CPUState, cleanwin) },
2586 { "fprs", offsetof(CPUState, fprs) },
2587 #endif
2588 #endif
2589 { NULL },
2592 static void expr_error(Monitor *mon, const char *msg)
2594 monitor_printf(mon, "%s\n", msg);
2595 longjmp(expr_env, 1);
2598 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2599 static int get_monitor_def(target_long *pval, const char *name)
2601 const MonitorDef *md;
2602 void *ptr;
2604 for(md = monitor_defs; md->name != NULL; md++) {
2605 if (compare_cmd(name, md->name)) {
2606 if (md->get_value) {
2607 *pval = md->get_value(md, md->offset);
2608 } else {
2609 CPUState *env = mon_get_cpu();
2610 if (!env)
2611 return -2;
2612 ptr = (uint8_t *)env + md->offset;
2613 switch(md->type) {
2614 case MD_I32:
2615 *pval = *(int32_t *)ptr;
2616 break;
2617 case MD_TLONG:
2618 *pval = *(target_long *)ptr;
2619 break;
2620 default:
2621 *pval = 0;
2622 break;
2625 return 0;
2628 return -1;
2631 static void next(void)
2633 if (*pch != '\0') {
2634 pch++;
2635 while (qemu_isspace(*pch))
2636 pch++;
2640 static int64_t expr_sum(Monitor *mon);
2642 static int64_t expr_unary(Monitor *mon)
2644 int64_t n;
2645 char *p;
2646 int ret;
2648 switch(*pch) {
2649 case '+':
2650 next();
2651 n = expr_unary(mon);
2652 break;
2653 case '-':
2654 next();
2655 n = -expr_unary(mon);
2656 break;
2657 case '~':
2658 next();
2659 n = ~expr_unary(mon);
2660 break;
2661 case '(':
2662 next();
2663 n = expr_sum(mon);
2664 if (*pch != ')') {
2665 expr_error(mon, "')' expected");
2667 next();
2668 break;
2669 case '\'':
2670 pch++;
2671 if (*pch == '\0')
2672 expr_error(mon, "character constant expected");
2673 n = *pch;
2674 pch++;
2675 if (*pch != '\'')
2676 expr_error(mon, "missing terminating \' character");
2677 next();
2678 break;
2679 case '$':
2681 char buf[128], *q;
2682 target_long reg=0;
2684 pch++;
2685 q = buf;
2686 while ((*pch >= 'a' && *pch <= 'z') ||
2687 (*pch >= 'A' && *pch <= 'Z') ||
2688 (*pch >= '0' && *pch <= '9') ||
2689 *pch == '_' || *pch == '.') {
2690 if ((q - buf) < sizeof(buf) - 1)
2691 *q++ = *pch;
2692 pch++;
2694 while (qemu_isspace(*pch))
2695 pch++;
2696 *q = 0;
2697 ret = get_monitor_def(&reg, buf);
2698 if (ret == -1)
2699 expr_error(mon, "unknown register");
2700 else if (ret == -2)
2701 expr_error(mon, "no cpu defined");
2702 n = reg;
2704 break;
2705 case '\0':
2706 expr_error(mon, "unexpected end of expression");
2707 n = 0;
2708 break;
2709 default:
2710 #if TARGET_PHYS_ADDR_BITS > 32
2711 n = strtoull(pch, &p, 0);
2712 #else
2713 n = strtoul(pch, &p, 0);
2714 #endif
2715 if (pch == p) {
2716 expr_error(mon, "invalid char in expression");
2718 pch = p;
2719 while (qemu_isspace(*pch))
2720 pch++;
2721 break;
2723 return n;
2727 static int64_t expr_prod(Monitor *mon)
2729 int64_t val, val2;
2730 int op;
2732 val = expr_unary(mon);
2733 for(;;) {
2734 op = *pch;
2735 if (op != '*' && op != '/' && op != '%')
2736 break;
2737 next();
2738 val2 = expr_unary(mon);
2739 switch(op) {
2740 default:
2741 case '*':
2742 val *= val2;
2743 break;
2744 case '/':
2745 case '%':
2746 if (val2 == 0)
2747 expr_error(mon, "division by zero");
2748 if (op == '/')
2749 val /= val2;
2750 else
2751 val %= val2;
2752 break;
2755 return val;
2758 static int64_t expr_logic(Monitor *mon)
2760 int64_t val, val2;
2761 int op;
2763 val = expr_prod(mon);
2764 for(;;) {
2765 op = *pch;
2766 if (op != '&' && op != '|' && op != '^')
2767 break;
2768 next();
2769 val2 = expr_prod(mon);
2770 switch(op) {
2771 default:
2772 case '&':
2773 val &= val2;
2774 break;
2775 case '|':
2776 val |= val2;
2777 break;
2778 case '^':
2779 val ^= val2;
2780 break;
2783 return val;
2786 static int64_t expr_sum(Monitor *mon)
2788 int64_t val, val2;
2789 int op;
2791 val = expr_logic(mon);
2792 for(;;) {
2793 op = *pch;
2794 if (op != '+' && op != '-')
2795 break;
2796 next();
2797 val2 = expr_logic(mon);
2798 if (op == '+')
2799 val += val2;
2800 else
2801 val -= val2;
2803 return val;
2806 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2808 pch = *pp;
2809 if (setjmp(expr_env)) {
2810 *pp = pch;
2811 return -1;
2813 while (qemu_isspace(*pch))
2814 pch++;
2815 *pval = expr_sum(mon);
2816 *pp = pch;
2817 return 0;
2820 static int get_str(char *buf, int buf_size, const char **pp)
2822 const char *p;
2823 char *q;
2824 int c;
2826 q = buf;
2827 p = *pp;
2828 while (qemu_isspace(*p))
2829 p++;
2830 if (*p == '\0') {
2831 fail:
2832 *q = '\0';
2833 *pp = p;
2834 return -1;
2836 if (*p == '\"') {
2837 p++;
2838 while (*p != '\0' && *p != '\"') {
2839 if (*p == '\\') {
2840 p++;
2841 c = *p++;
2842 switch(c) {
2843 case 'n':
2844 c = '\n';
2845 break;
2846 case 'r':
2847 c = '\r';
2848 break;
2849 case '\\':
2850 case '\'':
2851 case '\"':
2852 break;
2853 default:
2854 qemu_printf("unsupported escape code: '\\%c'\n", c);
2855 goto fail;
2857 if ((q - buf) < buf_size - 1) {
2858 *q++ = c;
2860 } else {
2861 if ((q - buf) < buf_size - 1) {
2862 *q++ = *p;
2864 p++;
2867 if (*p != '\"') {
2868 qemu_printf("unterminated string\n");
2869 goto fail;
2871 p++;
2872 } else {
2873 while (*p != '\0' && !qemu_isspace(*p)) {
2874 if ((q - buf) < buf_size - 1) {
2875 *q++ = *p;
2877 p++;
2880 *q = '\0';
2881 *pp = p;
2882 return 0;
2886 * Store the command-name in cmdname, and return a pointer to
2887 * the remaining of the command string.
2889 static const char *get_command_name(const char *cmdline,
2890 char *cmdname, size_t nlen)
2892 size_t len;
2893 const char *p, *pstart;
2895 p = cmdline;
2896 while (qemu_isspace(*p))
2897 p++;
2898 if (*p == '\0')
2899 return NULL;
2900 pstart = p;
2901 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2902 p++;
2903 len = p - pstart;
2904 if (len > nlen - 1)
2905 len = nlen - 1;
2906 memcpy(cmdname, pstart, len);
2907 cmdname[len] = '\0';
2908 return p;
2912 * Read key of 'type' into 'key' and return the current
2913 * 'type' pointer.
2915 static char *key_get_info(const char *type, char **key)
2917 size_t len;
2918 char *p, *str;
2920 if (*type == ',')
2921 type++;
2923 p = strchr(type, ':');
2924 if (!p) {
2925 *key = NULL;
2926 return NULL;
2928 len = p - type;
2930 str = qemu_malloc(len + 1);
2931 memcpy(str, type, len);
2932 str[len] = '\0';
2934 *key = str;
2935 return ++p;
2938 static int default_fmt_format = 'x';
2939 static int default_fmt_size = 4;
2941 #define MAX_ARGS 16
2943 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2944 const char *cmdline,
2945 QDict *qdict)
2947 const char *p, *typestr;
2948 int c;
2949 const mon_cmd_t *cmd;
2950 char cmdname[256];
2951 char buf[1024];
2952 char *key;
2954 #ifdef DEBUG
2955 monitor_printf(mon, "command='%s'\n", cmdline);
2956 #endif
2958 /* extract the command name */
2959 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2960 if (!p)
2961 return NULL;
2963 /* find the command */
2964 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2965 if (compare_cmd(cmdname, cmd->name))
2966 break;
2969 if (cmd->name == NULL) {
2970 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2971 return NULL;
2974 /* parse the parameters */
2975 typestr = cmd->args_type;
2976 for(;;) {
2977 typestr = key_get_info(typestr, &key);
2978 if (!typestr)
2979 break;
2980 c = *typestr;
2981 typestr++;
2982 switch(c) {
2983 case 'F':
2984 case 'B':
2985 case 's':
2987 int ret;
2989 while (qemu_isspace(*p))
2990 p++;
2991 if (*typestr == '?') {
2992 typestr++;
2993 if (*p == '\0') {
2994 /* no optional string: NULL argument */
2995 break;
2998 ret = get_str(buf, sizeof(buf), &p);
2999 if (ret < 0) {
3000 switch(c) {
3001 case 'F':
3002 monitor_printf(mon, "%s: filename expected\n",
3003 cmdname);
3004 break;
3005 case 'B':
3006 monitor_printf(mon, "%s: block device name expected\n",
3007 cmdname);
3008 break;
3009 default:
3010 monitor_printf(mon, "%s: string expected\n", cmdname);
3011 break;
3013 goto fail;
3015 qdict_put(qdict, key, qstring_from_str(buf));
3017 break;
3018 case '/':
3020 int count, format, size;
3022 while (qemu_isspace(*p))
3023 p++;
3024 if (*p == '/') {
3025 /* format found */
3026 p++;
3027 count = 1;
3028 if (qemu_isdigit(*p)) {
3029 count = 0;
3030 while (qemu_isdigit(*p)) {
3031 count = count * 10 + (*p - '0');
3032 p++;
3035 size = -1;
3036 format = -1;
3037 for(;;) {
3038 switch(*p) {
3039 case 'o':
3040 case 'd':
3041 case 'u':
3042 case 'x':
3043 case 'i':
3044 case 'c':
3045 format = *p++;
3046 break;
3047 case 'b':
3048 size = 1;
3049 p++;
3050 break;
3051 case 'h':
3052 size = 2;
3053 p++;
3054 break;
3055 case 'w':
3056 size = 4;
3057 p++;
3058 break;
3059 case 'g':
3060 case 'L':
3061 size = 8;
3062 p++;
3063 break;
3064 default:
3065 goto next;
3068 next:
3069 if (*p != '\0' && !qemu_isspace(*p)) {
3070 monitor_printf(mon, "invalid char in format: '%c'\n",
3071 *p);
3072 goto fail;
3074 if (format < 0)
3075 format = default_fmt_format;
3076 if (format != 'i') {
3077 /* for 'i', not specifying a size gives -1 as size */
3078 if (size < 0)
3079 size = default_fmt_size;
3080 default_fmt_size = size;
3082 default_fmt_format = format;
3083 } else {
3084 count = 1;
3085 format = default_fmt_format;
3086 if (format != 'i') {
3087 size = default_fmt_size;
3088 } else {
3089 size = -1;
3092 qdict_put(qdict, "count", qint_from_int(count));
3093 qdict_put(qdict, "format", qint_from_int(format));
3094 qdict_put(qdict, "size", qint_from_int(size));
3096 break;
3097 case 'i':
3098 case 'l':
3100 int64_t val;
3102 while (qemu_isspace(*p))
3103 p++;
3104 if (*typestr == '?' || *typestr == '.') {
3105 if (*typestr == '?') {
3106 if (*p == '\0') {
3107 typestr++;
3108 break;
3110 } else {
3111 if (*p == '.') {
3112 p++;
3113 while (qemu_isspace(*p))
3114 p++;
3115 } else {
3116 typestr++;
3117 break;
3120 typestr++;
3122 if (get_expr(mon, &val, &p))
3123 goto fail;
3124 /* Check if 'i' is greater than 32-bit */
3125 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3126 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3127 monitor_printf(mon, "integer is for 32-bit values\n");
3128 goto fail;
3130 qdict_put(qdict, key, qint_from_int(val));
3132 break;
3133 case '-':
3135 int has_option;
3136 /* option */
3138 c = *typestr++;
3139 if (c == '\0')
3140 goto bad_type;
3141 while (qemu_isspace(*p))
3142 p++;
3143 has_option = 0;
3144 if (*p == '-') {
3145 p++;
3146 if (*p != c) {
3147 monitor_printf(mon, "%s: unsupported option -%c\n",
3148 cmdname, *p);
3149 goto fail;
3151 p++;
3152 has_option = 1;
3154 qdict_put(qdict, key, qint_from_int(has_option));
3156 break;
3157 default:
3158 bad_type:
3159 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3160 goto fail;
3162 qemu_free(key);
3163 key = NULL;
3165 /* check that all arguments were parsed */
3166 while (qemu_isspace(*p))
3167 p++;
3168 if (*p != '\0') {
3169 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3170 cmdname);
3171 goto fail;
3174 return cmd;
3176 fail:
3177 qemu_free(key);
3178 return NULL;
3181 static void monitor_handle_command(Monitor *mon, const char *cmdline)
3183 QDict *qdict;
3184 const mon_cmd_t *cmd;
3186 qdict = qdict_new();
3188 cmd = monitor_parse_command(mon, cmdline, qdict);
3189 if (!cmd)
3190 goto out;
3192 qemu_errors_to_mon(mon);
3194 if (monitor_handler_ported(cmd)) {
3195 QObject *data = NULL;
3197 cmd->mhandler.cmd_new(mon, qdict, &data);
3198 if (data)
3199 cmd->user_print(mon, data);
3201 qobject_decref(data);
3202 } else {
3203 cmd->mhandler.cmd(mon, qdict);
3206 qemu_errors_to_previous();
3208 out:
3209 QDECREF(qdict);
3212 static void cmd_completion(const char *name, const char *list)
3214 const char *p, *pstart;
3215 char cmd[128];
3216 int len;
3218 p = list;
3219 for(;;) {
3220 pstart = p;
3221 p = strchr(p, '|');
3222 if (!p)
3223 p = pstart + strlen(pstart);
3224 len = p - pstart;
3225 if (len > sizeof(cmd) - 2)
3226 len = sizeof(cmd) - 2;
3227 memcpy(cmd, pstart, len);
3228 cmd[len] = '\0';
3229 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3230 readline_add_completion(cur_mon->rs, cmd);
3232 if (*p == '\0')
3233 break;
3234 p++;
3238 static void file_completion(const char *input)
3240 DIR *ffs;
3241 struct dirent *d;
3242 char path[1024];
3243 char file[1024], file_prefix[1024];
3244 int input_path_len;
3245 const char *p;
3247 p = strrchr(input, '/');
3248 if (!p) {
3249 input_path_len = 0;
3250 pstrcpy(file_prefix, sizeof(file_prefix), input);
3251 pstrcpy(path, sizeof(path), ".");
3252 } else {
3253 input_path_len = p - input + 1;
3254 memcpy(path, input, input_path_len);
3255 if (input_path_len > sizeof(path) - 1)
3256 input_path_len = sizeof(path) - 1;
3257 path[input_path_len] = '\0';
3258 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3260 #ifdef DEBUG_COMPLETION
3261 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3262 input, path, file_prefix);
3263 #endif
3264 ffs = opendir(path);
3265 if (!ffs)
3266 return;
3267 for(;;) {
3268 struct stat sb;
3269 d = readdir(ffs);
3270 if (!d)
3271 break;
3272 if (strstart(d->d_name, file_prefix, NULL)) {
3273 memcpy(file, input, input_path_len);
3274 if (input_path_len < sizeof(file))
3275 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3276 d->d_name);
3277 /* stat the file to find out if it's a directory.
3278 * In that case add a slash to speed up typing long paths
3280 stat(file, &sb);
3281 if(S_ISDIR(sb.st_mode))
3282 pstrcat(file, sizeof(file), "/");
3283 readline_add_completion(cur_mon->rs, file);
3286 closedir(ffs);
3289 static void block_completion_it(void *opaque, BlockDriverState *bs)
3291 const char *name = bdrv_get_device_name(bs);
3292 const char *input = opaque;
3294 if (input[0] == '\0' ||
3295 !strncmp(name, (char *)input, strlen(input))) {
3296 readline_add_completion(cur_mon->rs, name);
3300 /* NOTE: this parser is an approximate form of the real command parser */
3301 static void parse_cmdline(const char *cmdline,
3302 int *pnb_args, char **args)
3304 const char *p;
3305 int nb_args, ret;
3306 char buf[1024];
3308 p = cmdline;
3309 nb_args = 0;
3310 for(;;) {
3311 while (qemu_isspace(*p))
3312 p++;
3313 if (*p == '\0')
3314 break;
3315 if (nb_args >= MAX_ARGS)
3316 break;
3317 ret = get_str(buf, sizeof(buf), &p);
3318 args[nb_args] = qemu_strdup(buf);
3319 nb_args++;
3320 if (ret < 0)
3321 break;
3323 *pnb_args = nb_args;
3326 static const char *next_arg_type(const char *typestr)
3328 const char *p = strchr(typestr, ':');
3329 return (p != NULL ? ++p : typestr);
3332 static void monitor_find_completion(const char *cmdline)
3334 const char *cmdname;
3335 char *args[MAX_ARGS];
3336 int nb_args, i, len;
3337 const char *ptype, *str;
3338 const mon_cmd_t *cmd;
3339 const KeyDef *key;
3341 parse_cmdline(cmdline, &nb_args, args);
3342 #ifdef DEBUG_COMPLETION
3343 for(i = 0; i < nb_args; i++) {
3344 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3346 #endif
3348 /* if the line ends with a space, it means we want to complete the
3349 next arg */
3350 len = strlen(cmdline);
3351 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3352 if (nb_args >= MAX_ARGS)
3353 return;
3354 args[nb_args++] = qemu_strdup("");
3356 if (nb_args <= 1) {
3357 /* command completion */
3358 if (nb_args == 0)
3359 cmdname = "";
3360 else
3361 cmdname = args[0];
3362 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3363 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3364 cmd_completion(cmdname, cmd->name);
3366 } else {
3367 /* find the command */
3368 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3369 if (compare_cmd(args[0], cmd->name))
3370 goto found;
3372 return;
3373 found:
3374 ptype = next_arg_type(cmd->args_type);
3375 for(i = 0; i < nb_args - 2; i++) {
3376 if (*ptype != '\0') {
3377 ptype = next_arg_type(ptype);
3378 while (*ptype == '?')
3379 ptype = next_arg_type(ptype);
3382 str = args[nb_args - 1];
3383 if (*ptype == '-' && ptype[1] != '\0') {
3384 ptype += 2;
3386 switch(*ptype) {
3387 case 'F':
3388 /* file completion */
3389 readline_set_completion_index(cur_mon->rs, strlen(str));
3390 file_completion(str);
3391 break;
3392 case 'B':
3393 /* block device name completion */
3394 readline_set_completion_index(cur_mon->rs, strlen(str));
3395 bdrv_iterate(block_completion_it, (void *)str);
3396 break;
3397 case 's':
3398 /* XXX: more generic ? */
3399 if (!strcmp(cmd->name, "info")) {
3400 readline_set_completion_index(cur_mon->rs, strlen(str));
3401 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3402 cmd_completion(str, cmd->name);
3404 } else if (!strcmp(cmd->name, "sendkey")) {
3405 char *sep = strrchr(str, '-');
3406 if (sep)
3407 str = sep + 1;
3408 readline_set_completion_index(cur_mon->rs, strlen(str));
3409 for(key = key_defs; key->name != NULL; key++) {
3410 cmd_completion(str, key->name);
3412 } else if (!strcmp(cmd->name, "help|?")) {
3413 readline_set_completion_index(cur_mon->rs, strlen(str));
3414 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3415 cmd_completion(str, cmd->name);
3418 break;
3419 default:
3420 break;
3423 for(i = 0; i < nb_args; i++)
3424 qemu_free(args[i]);
3427 static int monitor_can_read(void *opaque)
3429 Monitor *mon = opaque;
3431 return (mon->suspend_cnt == 0) ? 128 : 0;
3434 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3436 Monitor *old_mon = cur_mon;
3437 int i;
3439 cur_mon = opaque;
3441 if (cur_mon->rs) {
3442 for (i = 0; i < size; i++)
3443 readline_handle_byte(cur_mon->rs, buf[i]);
3444 } else {
3445 if (size == 0 || buf[size - 1] != 0)
3446 monitor_printf(cur_mon, "corrupted command\n");
3447 else
3448 monitor_handle_command(cur_mon, (char *)buf);
3451 cur_mon = old_mon;
3454 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3456 monitor_suspend(mon);
3457 monitor_handle_command(mon, cmdline);
3458 monitor_resume(mon);
3461 int monitor_suspend(Monitor *mon)
3463 if (!mon->rs)
3464 return -ENOTTY;
3465 mon->suspend_cnt++;
3466 return 0;
3469 void monitor_resume(Monitor *mon)
3471 if (!mon->rs)
3472 return;
3473 if (--mon->suspend_cnt == 0)
3474 readline_show_prompt(mon->rs);
3477 static void monitor_event(void *opaque, int event)
3479 Monitor *mon = opaque;
3481 switch (event) {
3482 case CHR_EVENT_MUX_IN:
3483 mon->mux_out = 0;
3484 if (mon->reset_seen) {
3485 readline_restart(mon->rs);
3486 monitor_resume(mon);
3487 monitor_flush(mon);
3488 } else {
3489 mon->suspend_cnt = 0;
3491 break;
3493 case CHR_EVENT_MUX_OUT:
3494 if (mon->reset_seen) {
3495 if (mon->suspend_cnt == 0) {
3496 monitor_printf(mon, "\n");
3498 monitor_flush(mon);
3499 monitor_suspend(mon);
3500 } else {
3501 mon->suspend_cnt++;
3503 mon->mux_out = 1;
3504 break;
3506 case CHR_EVENT_OPENED:
3507 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3508 "information\n", QEMU_VERSION);
3509 if (!mon->mux_out) {
3510 readline_show_prompt(mon->rs);
3512 mon->reset_seen = 1;
3513 break;
3519 * Local variables:
3520 * c-indent-level: 4
3521 * c-basic-offset: 4
3522 * tab-width: 8
3523 * End:
3526 void monitor_init(CharDriverState *chr, int flags)
3528 static int is_first_init = 1;
3529 Monitor *mon;
3531 if (is_first_init) {
3532 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3533 is_first_init = 0;
3536 mon = qemu_mallocz(sizeof(*mon));
3538 mon->chr = chr;
3539 mon->flags = flags;
3540 if (flags & MONITOR_USE_READLINE) {
3541 mon->rs = readline_init(mon, monitor_find_completion);
3542 monitor_read_command(mon, 0);
3545 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3546 mon);
3548 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3549 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3550 cur_mon = mon;
3553 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3555 BlockDriverState *bs = opaque;
3556 int ret = 0;
3558 if (bdrv_set_key(bs, password) != 0) {
3559 monitor_printf(mon, "invalid password\n");
3560 ret = -EPERM;
3562 if (mon->password_completion_cb)
3563 mon->password_completion_cb(mon->password_opaque, ret);
3565 monitor_read_command(mon, 1);
3568 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3569 BlockDriverCompletionFunc *completion_cb,
3570 void *opaque)
3572 int err;
3574 if (!bdrv_key_required(bs)) {
3575 if (completion_cb)
3576 completion_cb(opaque, 0);
3577 return;
3580 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3581 bdrv_get_encrypted_filename(bs));
3583 mon->password_completion_cb = completion_cb;
3584 mon->password_opaque = opaque;
3586 err = monitor_read_password(mon, bdrv_password_cb, bs);
3588 if (err && completion_cb)
3589 completion_cb(opaque, err);
3592 typedef struct QemuErrorSink QemuErrorSink;
3593 struct QemuErrorSink {
3594 enum {
3595 ERR_SINK_FILE,
3596 ERR_SINK_MONITOR,
3597 } dest;
3598 union {
3599 FILE *fp;
3600 Monitor *mon;
3602 QemuErrorSink *previous;
3605 static QemuErrorSink *qemu_error_sink;
3607 void qemu_errors_to_file(FILE *fp)
3609 QemuErrorSink *sink;
3611 sink = qemu_mallocz(sizeof(*sink));
3612 sink->dest = ERR_SINK_FILE;
3613 sink->fp = fp;
3614 sink->previous = qemu_error_sink;
3615 qemu_error_sink = sink;
3618 void qemu_errors_to_mon(Monitor *mon)
3620 QemuErrorSink *sink;
3622 sink = qemu_mallocz(sizeof(*sink));
3623 sink->dest = ERR_SINK_MONITOR;
3624 sink->mon = mon;
3625 sink->previous = qemu_error_sink;
3626 qemu_error_sink = sink;
3629 void qemu_errors_to_previous(void)
3631 QemuErrorSink *sink;
3633 assert(qemu_error_sink != NULL);
3634 sink = qemu_error_sink;
3635 qemu_error_sink = sink->previous;
3636 qemu_free(sink);
3639 void qemu_error(const char *fmt, ...)
3641 va_list args;
3643 assert(qemu_error_sink != NULL);
3644 switch (qemu_error_sink->dest) {
3645 case ERR_SINK_FILE:
3646 va_start(args, fmt);
3647 vfprintf(qemu_error_sink->fp, fmt, args);
3648 va_end(args);
3649 break;
3650 case ERR_SINK_MONITOR:
3651 va_start(args, fmt);
3652 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3653 va_end(args);
3654 break;