Switch roms/pcbios submodule to qemu-kvm branch
[qemu/qemu-dev-zwu.git] / monitor.c
blobb4c4878bdb33613c43343e823cec1a73a3efad76
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=%ld", qdict_get_int(cpu, "thread_id"));
452 monitor_printf(mon, "\n");
455 static void monitor_print_cpus(Monitor *mon, const QObject *data)
457 QList *cpu_list;
459 assert(qobject_type(data) == QTYPE_QLIST);
460 cpu_list = qobject_to_qlist(data);
461 qlist_iter(cpu_list, print_cpu_iter, mon);
465 * do_info_cpus(): Show CPU information
467 * Return a QList with a QDict for each CPU.
469 * For example:
471 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
472 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
474 static void do_info_cpus(Monitor *mon, QObject **ret_data)
476 CPUState *env;
477 QList *cpu_list;
479 cpu_list = qlist_new();
481 /* just to set the default cpu if not already done */
482 mon_get_cpu();
484 for(env = first_cpu; env != NULL; env = env->next_cpu) {
485 const char *answer;
486 QDict *cpu = qdict_new();
488 cpu_synchronize_state(env);
489 kvm_save_mpstate(env);
491 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
492 answer = (env == mon->mon_cpu) ? "yes" : "no";
493 qdict_put(cpu, "current", qstring_from_str(answer));
494 #if defined(TARGET_I386)
495 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
496 #elif defined(TARGET_PPC)
497 qdict_put(cpu, "nip", qint_from_int(env->nip));
498 #elif defined(TARGET_SPARC)
499 qdict_put(cpu, "pc", qint_from_int(env->pc));
500 qdict_put(cpu, "npc", qint_from_int(env->npc));
501 #elif defined(TARGET_MIPS)
502 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
503 #endif
504 answer = env->halted ? "yes" : "no";
505 qdict_put(cpu, "halted", qstring_from_str(answer));
506 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
508 qlist_append(cpu_list, cpu);
511 *ret_data = QOBJECT(cpu_list);
514 static void do_cpu_set(Monitor *mon, const QDict *qdict)
516 int index = qdict_get_int(qdict, "index");
517 if (mon_set_cpu(index) < 0)
518 monitor_printf(mon, "Invalid CPU index\n");
521 static void do_cpu_set_nr(Monitor *mon, const QDict *qdict)
523 int state, value;
524 const char *status;
526 status = qdict_get_str(qdict, "state");
527 value = qdict_get_int(qdict, "cpu");
529 if (!strcmp(status, "online"))
530 state = 1;
531 else if (!strcmp(status, "offline"))
532 state = 0;
533 else {
534 monitor_printf(mon, "invalid status: %s\n", status);
535 return;
537 #if defined(TARGET_I386) || defined(TARGET_X86_64)
538 qemu_system_cpu_hot_add(value, state);
539 #endif
542 static void do_info_jit(Monitor *mon)
544 dump_exec_info((FILE *)mon, monitor_fprintf);
547 static void do_info_history(Monitor *mon)
549 int i;
550 const char *str;
552 if (!mon->rs)
553 return;
554 i = 0;
555 for(;;) {
556 str = readline_get_history(mon->rs, i);
557 if (!str)
558 break;
559 monitor_printf(mon, "%d: '%s'\n", i, str);
560 i++;
564 #if defined(TARGET_PPC)
565 /* XXX: not implemented in other targets */
566 static void do_info_cpu_stats(Monitor *mon)
568 CPUState *env;
570 env = mon_get_cpu();
571 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
573 #endif
576 * do_quit(): Quit QEMU execution
578 static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
580 exit(0);
583 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
585 if (bdrv_is_inserted(bs)) {
586 if (!force) {
587 if (!bdrv_is_removable(bs)) {
588 monitor_printf(mon, "device is not removable\n");
589 return -1;
591 if (bdrv_is_locked(bs)) {
592 monitor_printf(mon, "device is locked\n");
593 return -1;
596 bdrv_close(bs);
598 return 0;
601 static void do_eject(Monitor *mon, const QDict *qdict)
603 BlockDriverState *bs;
604 int force = qdict_get_int(qdict, "force");
605 const char *filename = qdict_get_str(qdict, "filename");
607 bs = bdrv_find(filename);
608 if (!bs) {
609 monitor_printf(mon, "device not found\n");
610 return;
612 eject_device(mon, bs, force);
615 static void do_change_block(Monitor *mon, const char *device,
616 const char *filename, const char *fmt)
618 BlockDriverState *bs;
619 BlockDriver *drv = NULL;
621 bs = bdrv_find(device);
622 if (!bs) {
623 monitor_printf(mon, "device not found\n");
624 return;
626 if (fmt) {
627 drv = bdrv_find_format(fmt);
628 if (!drv) {
629 monitor_printf(mon, "invalid format %s\n", fmt);
630 return;
633 if (eject_device(mon, bs, 0) < 0)
634 return;
635 bdrv_open2(bs, filename, 0, drv);
636 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
639 static void change_vnc_password_cb(Monitor *mon, const char *password,
640 void *opaque)
642 if (vnc_display_password(NULL, password) < 0)
643 monitor_printf(mon, "could not set VNC server password\n");
645 monitor_read_command(mon, 1);
648 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
650 if (strcmp(target, "passwd") == 0 ||
651 strcmp(target, "password") == 0) {
652 if (arg) {
653 char password[9];
654 strncpy(password, arg, sizeof(password));
655 password[sizeof(password) - 1] = '\0';
656 change_vnc_password_cb(mon, password, NULL);
657 } else {
658 monitor_read_password(mon, change_vnc_password_cb, NULL);
660 } else {
661 if (vnc_display_open(NULL, target) < 0)
662 monitor_printf(mon, "could not start VNC server on %s\n", target);
666 static void do_change(Monitor *mon, const QDict *qdict)
668 const char *device = qdict_get_str(qdict, "device");
669 const char *target = qdict_get_str(qdict, "target");
670 const char *arg = qdict_get_try_str(qdict, "arg");
671 if (strcmp(device, "vnc") == 0) {
672 do_change_vnc(mon, target, arg);
673 } else {
674 do_change_block(mon, device, target, arg);
678 static void do_screen_dump(Monitor *mon, const QDict *qdict)
680 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
683 static void do_logfile(Monitor *mon, const QDict *qdict)
685 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
688 static void do_log(Monitor *mon, const QDict *qdict)
690 int mask;
691 const char *items = qdict_get_str(qdict, "items");
693 if (!strcmp(items, "none")) {
694 mask = 0;
695 } else {
696 mask = cpu_str_to_log_mask(items);
697 if (!mask) {
698 help_cmd(mon, "log");
699 return;
702 cpu_set_log(mask);
705 static void do_singlestep(Monitor *mon, const QDict *qdict)
707 const char *option = qdict_get_try_str(qdict, "option");
708 if (!option || !strcmp(option, "on")) {
709 singlestep = 1;
710 } else if (!strcmp(option, "off")) {
711 singlestep = 0;
712 } else {
713 monitor_printf(mon, "unexpected option %s\n", option);
718 * do_stop(): Stop VM execution
720 static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
722 vm_stop(EXCP_INTERRUPT);
725 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
727 struct bdrv_iterate_context {
728 Monitor *mon;
729 int err;
733 * do_cont(): Resume emulation.
735 static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
737 struct bdrv_iterate_context context = { mon, 0 };
739 bdrv_iterate(encrypted_bdrv_it, &context);
740 /* only resume the vm if all keys are set and valid */
741 if (!context.err)
742 vm_start();
745 static void bdrv_key_cb(void *opaque, int err)
747 Monitor *mon = opaque;
749 /* another key was set successfully, retry to continue */
750 if (!err)
751 do_cont(mon, NULL, NULL);
754 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
756 struct bdrv_iterate_context *context = opaque;
758 if (!context->err && bdrv_key_required(bs)) {
759 context->err = -EBUSY;
760 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
761 context->mon);
765 static void do_gdbserver(Monitor *mon, const QDict *qdict)
767 const char *device = qdict_get_try_str(qdict, "device");
768 if (!device)
769 device = "tcp::" DEFAULT_GDBSTUB_PORT;
770 if (gdbserver_start(device) < 0) {
771 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
772 device);
773 } else if (strcmp(device, "none") == 0) {
774 monitor_printf(mon, "Disabled gdbserver\n");
775 } else {
776 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
777 device);
781 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
783 const char *action = qdict_get_str(qdict, "action");
784 if (select_watchdog_action(action) == -1) {
785 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
789 static void monitor_printc(Monitor *mon, int c)
791 monitor_printf(mon, "'");
792 switch(c) {
793 case '\'':
794 monitor_printf(mon, "\\'");
795 break;
796 case '\\':
797 monitor_printf(mon, "\\\\");
798 break;
799 case '\n':
800 monitor_printf(mon, "\\n");
801 break;
802 case '\r':
803 monitor_printf(mon, "\\r");
804 break;
805 default:
806 if (c >= 32 && c <= 126) {
807 monitor_printf(mon, "%c", c);
808 } else {
809 monitor_printf(mon, "\\x%02x", c);
811 break;
813 monitor_printf(mon, "'");
816 static void memory_dump(Monitor *mon, int count, int format, int wsize,
817 target_phys_addr_t addr, int is_physical)
819 CPUState *env;
820 int nb_per_line, l, line_size, i, max_digits, len;
821 uint8_t buf[16];
822 uint64_t v;
824 if (format == 'i') {
825 int flags;
826 flags = 0;
827 env = mon_get_cpu();
828 if (!env && !is_physical)
829 return;
830 #ifdef TARGET_I386
831 if (wsize == 2) {
832 flags = 1;
833 } else if (wsize == 4) {
834 flags = 0;
835 } else {
836 /* as default we use the current CS size */
837 flags = 0;
838 if (env) {
839 #ifdef TARGET_X86_64
840 if ((env->efer & MSR_EFER_LMA) &&
841 (env->segs[R_CS].flags & DESC_L_MASK))
842 flags = 2;
843 else
844 #endif
845 if (!(env->segs[R_CS].flags & DESC_B_MASK))
846 flags = 1;
849 #endif
850 monitor_disas(mon, env, addr, count, is_physical, flags);
851 return;
854 len = wsize * count;
855 if (wsize == 1)
856 line_size = 8;
857 else
858 line_size = 16;
859 nb_per_line = line_size / wsize;
860 max_digits = 0;
862 switch(format) {
863 case 'o':
864 max_digits = (wsize * 8 + 2) / 3;
865 break;
866 default:
867 case 'x':
868 max_digits = (wsize * 8) / 4;
869 break;
870 case 'u':
871 case 'd':
872 max_digits = (wsize * 8 * 10 + 32) / 33;
873 break;
874 case 'c':
875 wsize = 1;
876 break;
879 while (len > 0) {
880 if (is_physical)
881 monitor_printf(mon, TARGET_FMT_plx ":", addr);
882 else
883 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
884 l = len;
885 if (l > line_size)
886 l = line_size;
887 if (is_physical) {
888 cpu_physical_memory_rw(addr, buf, l, 0);
889 } else {
890 env = mon_get_cpu();
891 if (!env)
892 break;
893 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
894 monitor_printf(mon, " Cannot access memory\n");
895 break;
898 i = 0;
899 while (i < l) {
900 switch(wsize) {
901 default:
902 case 1:
903 v = ldub_raw(buf + i);
904 break;
905 case 2:
906 v = lduw_raw(buf + i);
907 break;
908 case 4:
909 v = (uint32_t)ldl_raw(buf + i);
910 break;
911 case 8:
912 v = ldq_raw(buf + i);
913 break;
915 monitor_printf(mon, " ");
916 switch(format) {
917 case 'o':
918 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
919 break;
920 case 'x':
921 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
922 break;
923 case 'u':
924 monitor_printf(mon, "%*" PRIu64, max_digits, v);
925 break;
926 case 'd':
927 monitor_printf(mon, "%*" PRId64, max_digits, v);
928 break;
929 case 'c':
930 monitor_printc(mon, v);
931 break;
933 i += wsize;
935 monitor_printf(mon, "\n");
936 addr += l;
937 len -= l;
941 static void do_memory_dump(Monitor *mon, const QDict *qdict)
943 int count = qdict_get_int(qdict, "count");
944 int format = qdict_get_int(qdict, "format");
945 int size = qdict_get_int(qdict, "size");
946 target_long addr = qdict_get_int(qdict, "addr");
948 memory_dump(mon, count, format, size, addr, 0);
951 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
953 int count = qdict_get_int(qdict, "count");
954 int format = qdict_get_int(qdict, "format");
955 int size = qdict_get_int(qdict, "size");
956 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
958 memory_dump(mon, count, format, size, addr, 1);
961 static void do_print(Monitor *mon, const QDict *qdict)
963 int format = qdict_get_int(qdict, "format");
964 target_phys_addr_t val = qdict_get_int(qdict, "val");
966 #if TARGET_PHYS_ADDR_BITS == 32
967 switch(format) {
968 case 'o':
969 monitor_printf(mon, "%#o", val);
970 break;
971 case 'x':
972 monitor_printf(mon, "%#x", val);
973 break;
974 case 'u':
975 monitor_printf(mon, "%u", val);
976 break;
977 default:
978 case 'd':
979 monitor_printf(mon, "%d", val);
980 break;
981 case 'c':
982 monitor_printc(mon, val);
983 break;
985 #else
986 switch(format) {
987 case 'o':
988 monitor_printf(mon, "%#" PRIo64, val);
989 break;
990 case 'x':
991 monitor_printf(mon, "%#" PRIx64, val);
992 break;
993 case 'u':
994 monitor_printf(mon, "%" PRIu64, val);
995 break;
996 default:
997 case 'd':
998 monitor_printf(mon, "%" PRId64, val);
999 break;
1000 case 'c':
1001 monitor_printc(mon, val);
1002 break;
1004 #endif
1005 monitor_printf(mon, "\n");
1008 static void do_memory_save(Monitor *mon, const QDict *qdict)
1010 FILE *f;
1011 uint32_t size = qdict_get_int(qdict, "size");
1012 const char *filename = qdict_get_str(qdict, "filename");
1013 target_long addr = qdict_get_int(qdict, "val");
1014 uint32_t l;
1015 CPUState *env;
1016 uint8_t buf[1024];
1018 env = mon_get_cpu();
1019 if (!env)
1020 return;
1022 f = fopen(filename, "wb");
1023 if (!f) {
1024 monitor_printf(mon, "could not open '%s'\n", filename);
1025 return;
1027 while (size != 0) {
1028 l = sizeof(buf);
1029 if (l > size)
1030 l = size;
1031 cpu_memory_rw_debug(env, addr, buf, l, 0);
1032 fwrite(buf, 1, l, f);
1033 addr += l;
1034 size -= l;
1036 fclose(f);
1039 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
1041 FILE *f;
1042 uint32_t l;
1043 uint8_t buf[1024];
1044 uint32_t size = qdict_get_int(qdict, "size");
1045 const char *filename = qdict_get_str(qdict, "filename");
1046 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1048 f = fopen(filename, "wb");
1049 if (!f) {
1050 monitor_printf(mon, "could not open '%s'\n", filename);
1051 return;
1053 while (size != 0) {
1054 l = sizeof(buf);
1055 if (l > size)
1056 l = size;
1057 cpu_physical_memory_rw(addr, buf, l, 0);
1058 fwrite(buf, 1, l, f);
1059 fflush(f);
1060 addr += l;
1061 size -= l;
1063 fclose(f);
1066 static void do_sum(Monitor *mon, const QDict *qdict)
1068 uint32_t addr;
1069 uint8_t buf[1];
1070 uint16_t sum;
1071 uint32_t start = qdict_get_int(qdict, "start");
1072 uint32_t size = qdict_get_int(qdict, "size");
1074 sum = 0;
1075 for(addr = start; addr < (start + size); addr++) {
1076 cpu_physical_memory_rw(addr, buf, 1, 0);
1077 /* BSD sum algorithm ('sum' Unix command) */
1078 sum = (sum >> 1) | (sum << 15);
1079 sum += buf[0];
1081 monitor_printf(mon, "%05d\n", sum);
1084 typedef struct {
1085 int keycode;
1086 const char *name;
1087 } KeyDef;
1089 static const KeyDef key_defs[] = {
1090 { 0x2a, "shift" },
1091 { 0x36, "shift_r" },
1093 { 0x38, "alt" },
1094 { 0xb8, "alt_r" },
1095 { 0x64, "altgr" },
1096 { 0xe4, "altgr_r" },
1097 { 0x1d, "ctrl" },
1098 { 0x9d, "ctrl_r" },
1100 { 0xdd, "menu" },
1102 { 0x01, "esc" },
1104 { 0x02, "1" },
1105 { 0x03, "2" },
1106 { 0x04, "3" },
1107 { 0x05, "4" },
1108 { 0x06, "5" },
1109 { 0x07, "6" },
1110 { 0x08, "7" },
1111 { 0x09, "8" },
1112 { 0x0a, "9" },
1113 { 0x0b, "0" },
1114 { 0x0c, "minus" },
1115 { 0x0d, "equal" },
1116 { 0x0e, "backspace" },
1118 { 0x0f, "tab" },
1119 { 0x10, "q" },
1120 { 0x11, "w" },
1121 { 0x12, "e" },
1122 { 0x13, "r" },
1123 { 0x14, "t" },
1124 { 0x15, "y" },
1125 { 0x16, "u" },
1126 { 0x17, "i" },
1127 { 0x18, "o" },
1128 { 0x19, "p" },
1130 { 0x1c, "ret" },
1132 { 0x1e, "a" },
1133 { 0x1f, "s" },
1134 { 0x20, "d" },
1135 { 0x21, "f" },
1136 { 0x22, "g" },
1137 { 0x23, "h" },
1138 { 0x24, "j" },
1139 { 0x25, "k" },
1140 { 0x26, "l" },
1142 { 0x2c, "z" },
1143 { 0x2d, "x" },
1144 { 0x2e, "c" },
1145 { 0x2f, "v" },
1146 { 0x30, "b" },
1147 { 0x31, "n" },
1148 { 0x32, "m" },
1149 { 0x33, "comma" },
1150 { 0x34, "dot" },
1151 { 0x35, "slash" },
1153 { 0x37, "asterisk" },
1155 { 0x39, "spc" },
1156 { 0x3a, "caps_lock" },
1157 { 0x3b, "f1" },
1158 { 0x3c, "f2" },
1159 { 0x3d, "f3" },
1160 { 0x3e, "f4" },
1161 { 0x3f, "f5" },
1162 { 0x40, "f6" },
1163 { 0x41, "f7" },
1164 { 0x42, "f8" },
1165 { 0x43, "f9" },
1166 { 0x44, "f10" },
1167 { 0x45, "num_lock" },
1168 { 0x46, "scroll_lock" },
1170 { 0xb5, "kp_divide" },
1171 { 0x37, "kp_multiply" },
1172 { 0x4a, "kp_subtract" },
1173 { 0x4e, "kp_add" },
1174 { 0x9c, "kp_enter" },
1175 { 0x53, "kp_decimal" },
1176 { 0x54, "sysrq" },
1178 { 0x52, "kp_0" },
1179 { 0x4f, "kp_1" },
1180 { 0x50, "kp_2" },
1181 { 0x51, "kp_3" },
1182 { 0x4b, "kp_4" },
1183 { 0x4c, "kp_5" },
1184 { 0x4d, "kp_6" },
1185 { 0x47, "kp_7" },
1186 { 0x48, "kp_8" },
1187 { 0x49, "kp_9" },
1189 { 0x56, "<" },
1191 { 0x57, "f11" },
1192 { 0x58, "f12" },
1194 { 0xb7, "print" },
1196 { 0xc7, "home" },
1197 { 0xc9, "pgup" },
1198 { 0xd1, "pgdn" },
1199 { 0xcf, "end" },
1201 { 0xcb, "left" },
1202 { 0xc8, "up" },
1203 { 0xd0, "down" },
1204 { 0xcd, "right" },
1206 { 0xd2, "insert" },
1207 { 0xd3, "delete" },
1208 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1209 { 0xf0, "stop" },
1210 { 0xf1, "again" },
1211 { 0xf2, "props" },
1212 { 0xf3, "undo" },
1213 { 0xf4, "front" },
1214 { 0xf5, "copy" },
1215 { 0xf6, "open" },
1216 { 0xf7, "paste" },
1217 { 0xf8, "find" },
1218 { 0xf9, "cut" },
1219 { 0xfa, "lf" },
1220 { 0xfb, "help" },
1221 { 0xfc, "meta_l" },
1222 { 0xfd, "meta_r" },
1223 { 0xfe, "compose" },
1224 #endif
1225 { 0, NULL },
1228 static int get_keycode(const char *key)
1230 const KeyDef *p;
1231 char *endp;
1232 int ret;
1234 for(p = key_defs; p->name != NULL; p++) {
1235 if (!strcmp(key, p->name))
1236 return p->keycode;
1238 if (strstart(key, "0x", NULL)) {
1239 ret = strtoul(key, &endp, 0);
1240 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1241 return ret;
1243 return -1;
1246 #define MAX_KEYCODES 16
1247 static uint8_t keycodes[MAX_KEYCODES];
1248 static int nb_pending_keycodes;
1249 static QEMUTimer *key_timer;
1251 static void release_keys(void *opaque)
1253 int keycode;
1255 while (nb_pending_keycodes > 0) {
1256 nb_pending_keycodes--;
1257 keycode = keycodes[nb_pending_keycodes];
1258 if (keycode & 0x80)
1259 kbd_put_keycode(0xe0);
1260 kbd_put_keycode(keycode | 0x80);
1264 static void do_sendkey(Monitor *mon, const QDict *qdict)
1266 char keyname_buf[16];
1267 char *separator;
1268 int keyname_len, keycode, i;
1269 const char *string = qdict_get_str(qdict, "string");
1270 int has_hold_time = qdict_haskey(qdict, "hold_time");
1271 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1273 if (nb_pending_keycodes > 0) {
1274 qemu_del_timer(key_timer);
1275 release_keys(NULL);
1277 if (!has_hold_time)
1278 hold_time = 100;
1279 i = 0;
1280 while (1) {
1281 separator = strchr(string, '-');
1282 keyname_len = separator ? separator - string : strlen(string);
1283 if (keyname_len > 0) {
1284 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1285 if (keyname_len > sizeof(keyname_buf) - 1) {
1286 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1287 return;
1289 if (i == MAX_KEYCODES) {
1290 monitor_printf(mon, "too many keys\n");
1291 return;
1293 keyname_buf[keyname_len] = 0;
1294 keycode = get_keycode(keyname_buf);
1295 if (keycode < 0) {
1296 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1297 return;
1299 keycodes[i++] = keycode;
1301 if (!separator)
1302 break;
1303 string = separator + 1;
1305 nb_pending_keycodes = i;
1306 /* key down events */
1307 for (i = 0; i < nb_pending_keycodes; i++) {
1308 keycode = keycodes[i];
1309 if (keycode & 0x80)
1310 kbd_put_keycode(0xe0);
1311 kbd_put_keycode(keycode & 0x7f);
1313 /* delayed key up events */
1314 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1315 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1318 static int mouse_button_state;
1320 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1322 int dx, dy, dz;
1323 const char *dx_str = qdict_get_str(qdict, "dx_str");
1324 const char *dy_str = qdict_get_str(qdict, "dy_str");
1325 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1326 dx = strtol(dx_str, NULL, 0);
1327 dy = strtol(dy_str, NULL, 0);
1328 dz = 0;
1329 if (dz_str)
1330 dz = strtol(dz_str, NULL, 0);
1331 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1334 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1336 int button_state = qdict_get_int(qdict, "button_state");
1337 mouse_button_state = button_state;
1338 kbd_mouse_event(0, 0, 0, mouse_button_state);
1341 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1343 int size = qdict_get_int(qdict, "size");
1344 int addr = qdict_get_int(qdict, "addr");
1345 int has_index = qdict_haskey(qdict, "index");
1346 uint32_t val;
1347 int suffix;
1349 if (has_index) {
1350 int index = qdict_get_int(qdict, "index");
1351 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1352 addr++;
1354 addr &= 0xffff;
1356 switch(size) {
1357 default:
1358 case 1:
1359 val = cpu_inb(addr);
1360 suffix = 'b';
1361 break;
1362 case 2:
1363 val = cpu_inw(addr);
1364 suffix = 'w';
1365 break;
1366 case 4:
1367 val = cpu_inl(addr);
1368 suffix = 'l';
1369 break;
1371 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1372 suffix, addr, size * 2, val);
1375 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1377 int size = qdict_get_int(qdict, "size");
1378 int addr = qdict_get_int(qdict, "addr");
1379 int val = qdict_get_int(qdict, "val");
1381 addr &= IOPORTS_MASK;
1383 switch (size) {
1384 default:
1385 case 1:
1386 cpu_outb(addr, val);
1387 break;
1388 case 2:
1389 cpu_outw(addr, val);
1390 break;
1391 case 4:
1392 cpu_outl(addr, val);
1393 break;
1397 static void do_boot_set(Monitor *mon, const QDict *qdict)
1399 int res;
1400 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1402 res = qemu_boot_set(bootdevice);
1403 if (res == 0) {
1404 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1405 } else if (res > 0) {
1406 monitor_printf(mon, "setting boot device list failed\n");
1407 } else {
1408 monitor_printf(mon, "no function defined to set boot device list for "
1409 "this architecture\n");
1414 * do_system_reset(): Issue a machine reset
1416 static void do_system_reset(Monitor *mon, const QDict *qdict,
1417 QObject **ret_data)
1419 qemu_system_reset_request();
1423 * do_system_powerdown(): Issue a machine powerdown
1425 static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1426 QObject **ret_data)
1428 qemu_system_powerdown_request();
1431 #if defined(TARGET_I386)
1432 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1434 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1435 addr,
1436 pte & mask,
1437 pte & PG_GLOBAL_MASK ? 'G' : '-',
1438 pte & PG_PSE_MASK ? 'P' : '-',
1439 pte & PG_DIRTY_MASK ? 'D' : '-',
1440 pte & PG_ACCESSED_MASK ? 'A' : '-',
1441 pte & PG_PCD_MASK ? 'C' : '-',
1442 pte & PG_PWT_MASK ? 'T' : '-',
1443 pte & PG_USER_MASK ? 'U' : '-',
1444 pte & PG_RW_MASK ? 'W' : '-');
1447 static void tlb_info(Monitor *mon)
1449 CPUState *env;
1450 int l1, l2;
1451 uint32_t pgd, pde, pte;
1453 env = mon_get_cpu();
1454 if (!env)
1455 return;
1457 if (!(env->cr[0] & CR0_PG_MASK)) {
1458 monitor_printf(mon, "PG disabled\n");
1459 return;
1461 pgd = env->cr[3] & ~0xfff;
1462 for(l1 = 0; l1 < 1024; l1++) {
1463 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1464 pde = le32_to_cpu(pde);
1465 if (pde & PG_PRESENT_MASK) {
1466 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1467 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1468 } else {
1469 for(l2 = 0; l2 < 1024; l2++) {
1470 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1471 (uint8_t *)&pte, 4);
1472 pte = le32_to_cpu(pte);
1473 if (pte & PG_PRESENT_MASK) {
1474 print_pte(mon, (l1 << 22) + (l2 << 12),
1475 pte & ~PG_PSE_MASK,
1476 ~0xfff);
1484 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1485 uint32_t end, int prot)
1487 int prot1;
1488 prot1 = *plast_prot;
1489 if (prot != prot1) {
1490 if (*pstart != -1) {
1491 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1492 *pstart, end, end - *pstart,
1493 prot1 & PG_USER_MASK ? 'u' : '-',
1494 'r',
1495 prot1 & PG_RW_MASK ? 'w' : '-');
1497 if (prot != 0)
1498 *pstart = end;
1499 else
1500 *pstart = -1;
1501 *plast_prot = prot;
1505 static void mem_info(Monitor *mon)
1507 CPUState *env;
1508 int l1, l2, prot, last_prot;
1509 uint32_t pgd, pde, pte, start, end;
1511 env = mon_get_cpu();
1512 if (!env)
1513 return;
1515 if (!(env->cr[0] & CR0_PG_MASK)) {
1516 monitor_printf(mon, "PG disabled\n");
1517 return;
1519 pgd = env->cr[3] & ~0xfff;
1520 last_prot = 0;
1521 start = -1;
1522 for(l1 = 0; l1 < 1024; l1++) {
1523 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1524 pde = le32_to_cpu(pde);
1525 end = l1 << 22;
1526 if (pde & PG_PRESENT_MASK) {
1527 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1528 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1529 mem_print(mon, &start, &last_prot, end, prot);
1530 } else {
1531 for(l2 = 0; l2 < 1024; l2++) {
1532 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1533 (uint8_t *)&pte, 4);
1534 pte = le32_to_cpu(pte);
1535 end = (l1 << 22) + (l2 << 12);
1536 if (pte & PG_PRESENT_MASK) {
1537 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1538 } else {
1539 prot = 0;
1541 mem_print(mon, &start, &last_prot, end, prot);
1544 } else {
1545 prot = 0;
1546 mem_print(mon, &start, &last_prot, end, prot);
1550 #endif
1552 #if defined(TARGET_SH4)
1554 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1556 monitor_printf(mon, " tlb%i:\t"
1557 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1558 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1559 "dirty=%hhu writethrough=%hhu\n",
1560 idx,
1561 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1562 tlb->v, tlb->sh, tlb->c, tlb->pr,
1563 tlb->d, tlb->wt);
1566 static void tlb_info(Monitor *mon)
1568 CPUState *env = mon_get_cpu();
1569 int i;
1571 monitor_printf (mon, "ITLB:\n");
1572 for (i = 0 ; i < ITLB_SIZE ; i++)
1573 print_tlb (mon, i, &env->itlb[i]);
1574 monitor_printf (mon, "UTLB:\n");
1575 for (i = 0 ; i < UTLB_SIZE ; i++)
1576 print_tlb (mon, i, &env->utlb[i]);
1579 #endif
1581 static void do_info_kvm(Monitor *mon)
1583 #if defined(USE_KVM) || defined(CONFIG_KVM)
1584 monitor_printf(mon, "kvm support: ");
1585 if (kvm_enabled())
1586 monitor_printf(mon, "enabled\n");
1587 else
1588 monitor_printf(mon, "disabled\n");
1589 #else
1590 monitor_printf(mon, "kvm support: not compiled\n");
1591 #endif
1594 static void do_info_numa(Monitor *mon)
1596 int i;
1597 CPUState *env;
1599 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1600 for (i = 0; i < nb_numa_nodes; i++) {
1601 monitor_printf(mon, "node %d cpus:", i);
1602 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1603 if (env->numa_node == i) {
1604 monitor_printf(mon, " %d", env->cpu_index);
1607 monitor_printf(mon, "\n");
1608 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1609 node_mem[i] >> 20);
1613 #ifdef CONFIG_PROFILER
1615 int64_t qemu_time;
1616 int64_t dev_time;
1618 static void do_info_profile(Monitor *mon)
1620 int64_t total;
1621 total = qemu_time;
1622 if (total == 0)
1623 total = 1;
1624 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1625 dev_time, dev_time / (double)get_ticks_per_sec());
1626 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1627 qemu_time, qemu_time / (double)get_ticks_per_sec());
1628 qemu_time = 0;
1629 dev_time = 0;
1631 #else
1632 static void do_info_profile(Monitor *mon)
1634 monitor_printf(mon, "Internal profiler not compiled\n");
1636 #endif
1638 /* Capture support */
1639 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1641 static void do_info_capture(Monitor *mon)
1643 int i;
1644 CaptureState *s;
1646 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1647 monitor_printf(mon, "[%d]: ", i);
1648 s->ops.info (s->opaque);
1652 #ifdef HAS_AUDIO
1653 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1655 int i;
1656 int n = qdict_get_int(qdict, "n");
1657 CaptureState *s;
1659 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1660 if (i == n) {
1661 s->ops.destroy (s->opaque);
1662 QLIST_REMOVE (s, entries);
1663 qemu_free (s);
1664 return;
1669 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1671 const char *path = qdict_get_str(qdict, "path");
1672 int has_freq = qdict_haskey(qdict, "freq");
1673 int freq = qdict_get_try_int(qdict, "freq", -1);
1674 int has_bits = qdict_haskey(qdict, "bits");
1675 int bits = qdict_get_try_int(qdict, "bits", -1);
1676 int has_channels = qdict_haskey(qdict, "nchannels");
1677 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1678 CaptureState *s;
1680 s = qemu_mallocz (sizeof (*s));
1682 freq = has_freq ? freq : 44100;
1683 bits = has_bits ? bits : 16;
1684 nchannels = has_channels ? nchannels : 2;
1686 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1687 monitor_printf(mon, "Faied to add wave capture\n");
1688 qemu_free (s);
1690 QLIST_INSERT_HEAD (&capture_head, s, entries);
1692 #endif
1694 #if defined(TARGET_I386)
1695 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1697 CPUState *env;
1698 int cpu_index = qdict_get_int(qdict, "cpu_index");
1700 for (env = first_cpu; env != NULL; env = env->next_cpu)
1701 if (env->cpu_index == cpu_index) {
1702 if (kvm_enabled())
1703 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1704 else
1705 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1706 break;
1709 #endif
1711 static void do_info_status(Monitor *mon)
1713 if (vm_running) {
1714 if (singlestep) {
1715 monitor_printf(mon, "VM status: running (single step mode)\n");
1716 } else {
1717 monitor_printf(mon, "VM status: running\n");
1719 } else
1720 monitor_printf(mon, "VM status: paused\n");
1724 * do_balloon(): Request VM to change its memory allocation
1726 static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
1728 int value = qdict_get_int(qdict, "value");
1729 ram_addr_t target = value;
1730 qemu_balloon(target << 20);
1733 static void monitor_print_balloon(Monitor *mon, const QObject *data)
1735 monitor_printf(mon, "balloon: actual=%d\n",
1736 (int)qint_get_int(qobject_to_qint(data)));
1740 * do_info_balloon(): Balloon information
1742 static void do_info_balloon(Monitor *mon, QObject **ret_data)
1744 ram_addr_t actual;
1746 actual = qemu_balloon_status();
1747 if (kvm_enabled() && !kvm_has_sync_mmu())
1748 monitor_printf(mon, "Using KVM without synchronous MMU, "
1749 "ballooning disabled\n");
1750 else if (actual == 0)
1751 monitor_printf(mon, "Ballooning not activated in VM\n");
1752 else
1753 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
1756 static qemu_acl *find_acl(Monitor *mon, const char *name)
1758 qemu_acl *acl = qemu_acl_find(name);
1760 if (!acl) {
1761 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1763 return acl;
1766 static void do_acl_show(Monitor *mon, const QDict *qdict)
1768 const char *aclname = qdict_get_str(qdict, "aclname");
1769 qemu_acl *acl = find_acl(mon, aclname);
1770 qemu_acl_entry *entry;
1771 int i = 0;
1773 if (acl) {
1774 monitor_printf(mon, "policy: %s\n",
1775 acl->defaultDeny ? "deny" : "allow");
1776 QTAILQ_FOREACH(entry, &acl->entries, next) {
1777 i++;
1778 monitor_printf(mon, "%d: %s %s\n", i,
1779 entry->deny ? "deny" : "allow", entry->match);
1784 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1786 const char *aclname = qdict_get_str(qdict, "aclname");
1787 qemu_acl *acl = find_acl(mon, aclname);
1789 if (acl) {
1790 qemu_acl_reset(acl);
1791 monitor_printf(mon, "acl: removed all rules\n");
1795 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1797 const char *aclname = qdict_get_str(qdict, "aclname");
1798 const char *policy = qdict_get_str(qdict, "policy");
1799 qemu_acl *acl = find_acl(mon, aclname);
1801 if (acl) {
1802 if (strcmp(policy, "allow") == 0) {
1803 acl->defaultDeny = 0;
1804 monitor_printf(mon, "acl: policy set to 'allow'\n");
1805 } else if (strcmp(policy, "deny") == 0) {
1806 acl->defaultDeny = 1;
1807 monitor_printf(mon, "acl: policy set to 'deny'\n");
1808 } else {
1809 monitor_printf(mon, "acl: unknown policy '%s', "
1810 "expected 'deny' or 'allow'\n", policy);
1815 static void do_acl_add(Monitor *mon, const QDict *qdict)
1817 const char *aclname = qdict_get_str(qdict, "aclname");
1818 const char *match = qdict_get_str(qdict, "match");
1819 const char *policy = qdict_get_str(qdict, "policy");
1820 int has_index = qdict_haskey(qdict, "index");
1821 int index = qdict_get_try_int(qdict, "index", -1);
1822 qemu_acl *acl = find_acl(mon, aclname);
1823 int deny, ret;
1825 if (acl) {
1826 if (strcmp(policy, "allow") == 0) {
1827 deny = 0;
1828 } else if (strcmp(policy, "deny") == 0) {
1829 deny = 1;
1830 } else {
1831 monitor_printf(mon, "acl: unknown policy '%s', "
1832 "expected 'deny' or 'allow'\n", policy);
1833 return;
1835 if (has_index)
1836 ret = qemu_acl_insert(acl, deny, match, index);
1837 else
1838 ret = qemu_acl_append(acl, deny, match);
1839 if (ret < 0)
1840 monitor_printf(mon, "acl: unable to add acl entry\n");
1841 else
1842 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1846 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1848 const char *aclname = qdict_get_str(qdict, "aclname");
1849 const char *match = qdict_get_str(qdict, "match");
1850 qemu_acl *acl = find_acl(mon, aclname);
1851 int ret;
1853 if (acl) {
1854 ret = qemu_acl_remove(acl, match);
1855 if (ret < 0)
1856 monitor_printf(mon, "acl: no matching acl entry\n");
1857 else
1858 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1862 #if defined(TARGET_I386)
1863 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1865 CPUState *cenv;
1866 int cpu_index = qdict_get_int(qdict, "cpu_index");
1867 int bank = qdict_get_int(qdict, "bank");
1868 uint64_t status = qdict_get_int(qdict, "status");
1869 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1870 uint64_t addr = qdict_get_int(qdict, "addr");
1871 uint64_t misc = qdict_get_int(qdict, "misc");
1873 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1874 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1875 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1876 break;
1879 #endif
1881 static void do_getfd(Monitor *mon, const QDict *qdict)
1883 const char *fdname = qdict_get_str(qdict, "fdname");
1884 mon_fd_t *monfd;
1885 int fd;
1887 fd = qemu_chr_get_msgfd(mon->chr);
1888 if (fd == -1) {
1889 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1890 return;
1893 if (qemu_isdigit(fdname[0])) {
1894 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1895 return;
1898 fd = dup(fd);
1899 if (fd == -1) {
1900 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1901 strerror(errno));
1902 return;
1905 QLIST_FOREACH(monfd, &mon->fds, next) {
1906 if (strcmp(monfd->name, fdname) != 0) {
1907 continue;
1910 close(monfd->fd);
1911 monfd->fd = fd;
1912 return;
1915 monfd = qemu_mallocz(sizeof(mon_fd_t));
1916 monfd->name = qemu_strdup(fdname);
1917 monfd->fd = fd;
1919 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1922 static void do_closefd(Monitor *mon, const QDict *qdict)
1924 const char *fdname = qdict_get_str(qdict, "fdname");
1925 mon_fd_t *monfd;
1927 QLIST_FOREACH(monfd, &mon->fds, next) {
1928 if (strcmp(monfd->name, fdname) != 0) {
1929 continue;
1932 QLIST_REMOVE(monfd, next);
1933 close(monfd->fd);
1934 qemu_free(monfd->name);
1935 qemu_free(monfd);
1936 return;
1939 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1940 fdname);
1943 static void do_loadvm(Monitor *mon, const QDict *qdict)
1945 int saved_vm_running = vm_running;
1946 const char *name = qdict_get_str(qdict, "name");
1948 vm_stop(0);
1950 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1951 vm_start();
1954 int monitor_get_fd(Monitor *mon, const char *fdname)
1956 mon_fd_t *monfd;
1958 QLIST_FOREACH(monfd, &mon->fds, next) {
1959 int fd;
1961 if (strcmp(monfd->name, fdname) != 0) {
1962 continue;
1965 fd = monfd->fd;
1967 /* caller takes ownership of fd */
1968 QLIST_REMOVE(monfd, next);
1969 qemu_free(monfd->name);
1970 qemu_free(monfd);
1972 return fd;
1975 return -1;
1978 static const mon_cmd_t mon_cmds[] = {
1979 #include "qemu-monitor.h"
1980 { NULL, NULL, },
1983 /* Please update qemu-monitor.hx when adding or changing commands */
1984 static const mon_cmd_t info_cmds[] = {
1986 .name = "version",
1987 .args_type = "",
1988 .params = "",
1989 .help = "show the version of QEMU",
1990 .user_print = monitor_print_qobject,
1991 .mhandler.info_new = do_info_version,
1994 .name = "network",
1995 .args_type = "",
1996 .params = "",
1997 .help = "show the network state",
1998 .mhandler.info = do_info_network,
2001 .name = "chardev",
2002 .args_type = "",
2003 .params = "",
2004 .help = "show the character devices",
2005 .mhandler.info = qemu_chr_info,
2008 .name = "block",
2009 .args_type = "",
2010 .params = "",
2011 .help = "show the block devices",
2012 .mhandler.info = bdrv_info,
2015 .name = "blockstats",
2016 .args_type = "",
2017 .params = "",
2018 .help = "show block device statistics",
2019 .mhandler.info = bdrv_info_stats,
2022 .name = "registers",
2023 .args_type = "",
2024 .params = "",
2025 .help = "show the cpu registers",
2026 .mhandler.info = do_info_registers,
2029 .name = "cpus",
2030 .args_type = "",
2031 .params = "",
2032 .help = "show infos for each CPU",
2033 .user_print = monitor_print_cpus,
2034 .mhandler.info_new = do_info_cpus,
2037 .name = "history",
2038 .args_type = "",
2039 .params = "",
2040 .help = "show the command line history",
2041 .mhandler.info = do_info_history,
2044 .name = "irq",
2045 .args_type = "",
2046 .params = "",
2047 .help = "show the interrupts statistics (if available)",
2048 .mhandler.info = irq_info,
2051 .name = "pic",
2052 .args_type = "",
2053 .params = "",
2054 .help = "show i8259 (PIC) state",
2055 .mhandler.info = pic_info,
2058 .name = "pci",
2059 .args_type = "",
2060 .params = "",
2061 .help = "show PCI info",
2062 .mhandler.info = pci_info,
2064 #if defined(TARGET_I386) || defined(TARGET_SH4)
2066 .name = "tlb",
2067 .args_type = "",
2068 .params = "",
2069 .help = "show virtual to physical memory mappings",
2070 .mhandler.info = tlb_info,
2072 #endif
2073 #if defined(TARGET_I386)
2075 .name = "mem",
2076 .args_type = "",
2077 .params = "",
2078 .help = "show the active virtual memory mappings",
2079 .mhandler.info = mem_info,
2082 .name = "hpet",
2083 .args_type = "",
2084 .params = "",
2085 .help = "show state of HPET",
2086 .mhandler.info = do_info_hpet,
2088 #endif
2090 .name = "jit",
2091 .args_type = "",
2092 .params = "",
2093 .help = "show dynamic compiler info",
2094 .mhandler.info = do_info_jit,
2097 .name = "kvm",
2098 .args_type = "",
2099 .params = "",
2100 .help = "show KVM information",
2101 .mhandler.info = do_info_kvm,
2104 .name = "numa",
2105 .args_type = "",
2106 .params = "",
2107 .help = "show NUMA information",
2108 .mhandler.info = do_info_numa,
2111 .name = "usb",
2112 .args_type = "",
2113 .params = "",
2114 .help = "show guest USB devices",
2115 .mhandler.info = usb_info,
2118 .name = "usbhost",
2119 .args_type = "",
2120 .params = "",
2121 .help = "show host USB devices",
2122 .mhandler.info = usb_host_info,
2125 .name = "profile",
2126 .args_type = "",
2127 .params = "",
2128 .help = "show profiling information",
2129 .mhandler.info = do_info_profile,
2132 .name = "capture",
2133 .args_type = "",
2134 .params = "",
2135 .help = "show capture information",
2136 .mhandler.info = do_info_capture,
2139 .name = "snapshots",
2140 .args_type = "",
2141 .params = "",
2142 .help = "show the currently saved VM snapshots",
2143 .mhandler.info = do_info_snapshots,
2146 .name = "status",
2147 .args_type = "",
2148 .params = "",
2149 .help = "show the current VM status (running|paused)",
2150 .mhandler.info = do_info_status,
2153 .name = "pcmcia",
2154 .args_type = "",
2155 .params = "",
2156 .help = "show guest PCMCIA status",
2157 .mhandler.info = pcmcia_info,
2160 .name = "mice",
2161 .args_type = "",
2162 .params = "",
2163 .help = "show which guest mouse is receiving events",
2164 .mhandler.info = do_info_mice,
2167 .name = "vnc",
2168 .args_type = "",
2169 .params = "",
2170 .help = "show the vnc server status",
2171 .mhandler.info = do_info_vnc,
2174 .name = "name",
2175 .args_type = "",
2176 .params = "",
2177 .help = "show the current VM name",
2178 .mhandler.info = do_info_name,
2181 .name = "uuid",
2182 .args_type = "",
2183 .params = "",
2184 .help = "show the current VM UUID",
2185 .mhandler.info = do_info_uuid,
2187 #if defined(TARGET_PPC)
2189 .name = "cpustats",
2190 .args_type = "",
2191 .params = "",
2192 .help = "show CPU statistics",
2193 .mhandler.info = do_info_cpu_stats,
2195 #endif
2196 #if defined(CONFIG_SLIRP)
2198 .name = "usernet",
2199 .args_type = "",
2200 .params = "",
2201 .help = "show user network stack connection states",
2202 .mhandler.info = do_info_usernet,
2204 #endif
2206 .name = "migrate",
2207 .args_type = "",
2208 .params = "",
2209 .help = "show migration status",
2210 .mhandler.info = do_info_migrate,
2213 .name = "balloon",
2214 .args_type = "",
2215 .params = "",
2216 .help = "show balloon information",
2217 .user_print = monitor_print_balloon,
2218 .mhandler.info_new = do_info_balloon,
2221 .name = "qtree",
2222 .args_type = "",
2223 .params = "",
2224 .help = "show device tree",
2225 .mhandler.info = do_info_qtree,
2228 .name = "qdm",
2229 .args_type = "",
2230 .params = "",
2231 .help = "show qdev device model list",
2232 .mhandler.info = do_info_qdm,
2235 .name = "roms",
2236 .args_type = "",
2237 .params = "",
2238 .help = "show roms",
2239 .mhandler.info = do_info_roms,
2242 .name = NULL,
2246 /*******************************************************************/
2248 static const char *pch;
2249 static jmp_buf expr_env;
2251 #define MD_TLONG 0
2252 #define MD_I32 1
2254 typedef struct MonitorDef {
2255 const char *name;
2256 int offset;
2257 target_long (*get_value)(const struct MonitorDef *md, int val);
2258 int type;
2259 } MonitorDef;
2261 #if defined(TARGET_I386)
2262 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2264 CPUState *env = mon_get_cpu();
2265 if (!env)
2266 return 0;
2267 return env->eip + env->segs[R_CS].base;
2269 #endif
2271 #if defined(TARGET_PPC)
2272 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2274 CPUState *env = mon_get_cpu();
2275 unsigned int u;
2276 int i;
2278 if (!env)
2279 return 0;
2281 u = 0;
2282 for (i = 0; i < 8; i++)
2283 u |= env->crf[i] << (32 - (4 * i));
2285 return u;
2288 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2290 CPUState *env = mon_get_cpu();
2291 if (!env)
2292 return 0;
2293 return env->msr;
2296 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2298 CPUState *env = mon_get_cpu();
2299 if (!env)
2300 return 0;
2301 return env->xer;
2304 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2306 CPUState *env = mon_get_cpu();
2307 if (!env)
2308 return 0;
2309 return cpu_ppc_load_decr(env);
2312 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2314 CPUState *env = mon_get_cpu();
2315 if (!env)
2316 return 0;
2317 return cpu_ppc_load_tbu(env);
2320 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2322 CPUState *env = mon_get_cpu();
2323 if (!env)
2324 return 0;
2325 return cpu_ppc_load_tbl(env);
2327 #endif
2329 #if defined(TARGET_SPARC)
2330 #ifndef TARGET_SPARC64
2331 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2333 CPUState *env = mon_get_cpu();
2334 if (!env)
2335 return 0;
2336 return GET_PSR(env);
2338 #endif
2340 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2342 CPUState *env = mon_get_cpu();
2343 if (!env)
2344 return 0;
2345 return env->regwptr[val];
2347 #endif
2349 static const MonitorDef monitor_defs[] = {
2350 #ifdef TARGET_I386
2352 #define SEG(name, seg) \
2353 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2354 { name ".base", offsetof(CPUState, segs[seg].base) },\
2355 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2357 { "eax", offsetof(CPUState, regs[0]) },
2358 { "ecx", offsetof(CPUState, regs[1]) },
2359 { "edx", offsetof(CPUState, regs[2]) },
2360 { "ebx", offsetof(CPUState, regs[3]) },
2361 { "esp|sp", offsetof(CPUState, regs[4]) },
2362 { "ebp|fp", offsetof(CPUState, regs[5]) },
2363 { "esi", offsetof(CPUState, regs[6]) },
2364 { "edi", offsetof(CPUState, regs[7]) },
2365 #ifdef TARGET_X86_64
2366 { "r8", offsetof(CPUState, regs[8]) },
2367 { "r9", offsetof(CPUState, regs[9]) },
2368 { "r10", offsetof(CPUState, regs[10]) },
2369 { "r11", offsetof(CPUState, regs[11]) },
2370 { "r12", offsetof(CPUState, regs[12]) },
2371 { "r13", offsetof(CPUState, regs[13]) },
2372 { "r14", offsetof(CPUState, regs[14]) },
2373 { "r15", offsetof(CPUState, regs[15]) },
2374 #endif
2375 { "eflags", offsetof(CPUState, eflags) },
2376 { "eip", offsetof(CPUState, eip) },
2377 SEG("cs", R_CS)
2378 SEG("ds", R_DS)
2379 SEG("es", R_ES)
2380 SEG("ss", R_SS)
2381 SEG("fs", R_FS)
2382 SEG("gs", R_GS)
2383 { "pc", 0, monitor_get_pc, },
2384 #elif defined(TARGET_PPC)
2385 /* General purpose registers */
2386 { "r0", offsetof(CPUState, gpr[0]) },
2387 { "r1", offsetof(CPUState, gpr[1]) },
2388 { "r2", offsetof(CPUState, gpr[2]) },
2389 { "r3", offsetof(CPUState, gpr[3]) },
2390 { "r4", offsetof(CPUState, gpr[4]) },
2391 { "r5", offsetof(CPUState, gpr[5]) },
2392 { "r6", offsetof(CPUState, gpr[6]) },
2393 { "r7", offsetof(CPUState, gpr[7]) },
2394 { "r8", offsetof(CPUState, gpr[8]) },
2395 { "r9", offsetof(CPUState, gpr[9]) },
2396 { "r10", offsetof(CPUState, gpr[10]) },
2397 { "r11", offsetof(CPUState, gpr[11]) },
2398 { "r12", offsetof(CPUState, gpr[12]) },
2399 { "r13", offsetof(CPUState, gpr[13]) },
2400 { "r14", offsetof(CPUState, gpr[14]) },
2401 { "r15", offsetof(CPUState, gpr[15]) },
2402 { "r16", offsetof(CPUState, gpr[16]) },
2403 { "r17", offsetof(CPUState, gpr[17]) },
2404 { "r18", offsetof(CPUState, gpr[18]) },
2405 { "r19", offsetof(CPUState, gpr[19]) },
2406 { "r20", offsetof(CPUState, gpr[20]) },
2407 { "r21", offsetof(CPUState, gpr[21]) },
2408 { "r22", offsetof(CPUState, gpr[22]) },
2409 { "r23", offsetof(CPUState, gpr[23]) },
2410 { "r24", offsetof(CPUState, gpr[24]) },
2411 { "r25", offsetof(CPUState, gpr[25]) },
2412 { "r26", offsetof(CPUState, gpr[26]) },
2413 { "r27", offsetof(CPUState, gpr[27]) },
2414 { "r28", offsetof(CPUState, gpr[28]) },
2415 { "r29", offsetof(CPUState, gpr[29]) },
2416 { "r30", offsetof(CPUState, gpr[30]) },
2417 { "r31", offsetof(CPUState, gpr[31]) },
2418 /* Floating point registers */
2419 { "f0", offsetof(CPUState, fpr[0]) },
2420 { "f1", offsetof(CPUState, fpr[1]) },
2421 { "f2", offsetof(CPUState, fpr[2]) },
2422 { "f3", offsetof(CPUState, fpr[3]) },
2423 { "f4", offsetof(CPUState, fpr[4]) },
2424 { "f5", offsetof(CPUState, fpr[5]) },
2425 { "f6", offsetof(CPUState, fpr[6]) },
2426 { "f7", offsetof(CPUState, fpr[7]) },
2427 { "f8", offsetof(CPUState, fpr[8]) },
2428 { "f9", offsetof(CPUState, fpr[9]) },
2429 { "f10", offsetof(CPUState, fpr[10]) },
2430 { "f11", offsetof(CPUState, fpr[11]) },
2431 { "f12", offsetof(CPUState, fpr[12]) },
2432 { "f13", offsetof(CPUState, fpr[13]) },
2433 { "f14", offsetof(CPUState, fpr[14]) },
2434 { "f15", offsetof(CPUState, fpr[15]) },
2435 { "f16", offsetof(CPUState, fpr[16]) },
2436 { "f17", offsetof(CPUState, fpr[17]) },
2437 { "f18", offsetof(CPUState, fpr[18]) },
2438 { "f19", offsetof(CPUState, fpr[19]) },
2439 { "f20", offsetof(CPUState, fpr[20]) },
2440 { "f21", offsetof(CPUState, fpr[21]) },
2441 { "f22", offsetof(CPUState, fpr[22]) },
2442 { "f23", offsetof(CPUState, fpr[23]) },
2443 { "f24", offsetof(CPUState, fpr[24]) },
2444 { "f25", offsetof(CPUState, fpr[25]) },
2445 { "f26", offsetof(CPUState, fpr[26]) },
2446 { "f27", offsetof(CPUState, fpr[27]) },
2447 { "f28", offsetof(CPUState, fpr[28]) },
2448 { "f29", offsetof(CPUState, fpr[29]) },
2449 { "f30", offsetof(CPUState, fpr[30]) },
2450 { "f31", offsetof(CPUState, fpr[31]) },
2451 { "fpscr", offsetof(CPUState, fpscr) },
2452 /* Next instruction pointer */
2453 { "nip|pc", offsetof(CPUState, nip) },
2454 { "lr", offsetof(CPUState, lr) },
2455 { "ctr", offsetof(CPUState, ctr) },
2456 { "decr", 0, &monitor_get_decr, },
2457 { "ccr", 0, &monitor_get_ccr, },
2458 /* Machine state register */
2459 { "msr", 0, &monitor_get_msr, },
2460 { "xer", 0, &monitor_get_xer, },
2461 { "tbu", 0, &monitor_get_tbu, },
2462 { "tbl", 0, &monitor_get_tbl, },
2463 #if defined(TARGET_PPC64)
2464 /* Address space register */
2465 { "asr", offsetof(CPUState, asr) },
2466 #endif
2467 /* Segment registers */
2468 { "sdr1", offsetof(CPUState, sdr1) },
2469 { "sr0", offsetof(CPUState, sr[0]) },
2470 { "sr1", offsetof(CPUState, sr[1]) },
2471 { "sr2", offsetof(CPUState, sr[2]) },
2472 { "sr3", offsetof(CPUState, sr[3]) },
2473 { "sr4", offsetof(CPUState, sr[4]) },
2474 { "sr5", offsetof(CPUState, sr[5]) },
2475 { "sr6", offsetof(CPUState, sr[6]) },
2476 { "sr7", offsetof(CPUState, sr[7]) },
2477 { "sr8", offsetof(CPUState, sr[8]) },
2478 { "sr9", offsetof(CPUState, sr[9]) },
2479 { "sr10", offsetof(CPUState, sr[10]) },
2480 { "sr11", offsetof(CPUState, sr[11]) },
2481 { "sr12", offsetof(CPUState, sr[12]) },
2482 { "sr13", offsetof(CPUState, sr[13]) },
2483 { "sr14", offsetof(CPUState, sr[14]) },
2484 { "sr15", offsetof(CPUState, sr[15]) },
2485 /* Too lazy to put BATs and SPRs ... */
2486 #elif defined(TARGET_SPARC)
2487 { "g0", offsetof(CPUState, gregs[0]) },
2488 { "g1", offsetof(CPUState, gregs[1]) },
2489 { "g2", offsetof(CPUState, gregs[2]) },
2490 { "g3", offsetof(CPUState, gregs[3]) },
2491 { "g4", offsetof(CPUState, gregs[4]) },
2492 { "g5", offsetof(CPUState, gregs[5]) },
2493 { "g6", offsetof(CPUState, gregs[6]) },
2494 { "g7", offsetof(CPUState, gregs[7]) },
2495 { "o0", 0, monitor_get_reg },
2496 { "o1", 1, monitor_get_reg },
2497 { "o2", 2, monitor_get_reg },
2498 { "o3", 3, monitor_get_reg },
2499 { "o4", 4, monitor_get_reg },
2500 { "o5", 5, monitor_get_reg },
2501 { "o6", 6, monitor_get_reg },
2502 { "o7", 7, monitor_get_reg },
2503 { "l0", 8, monitor_get_reg },
2504 { "l1", 9, monitor_get_reg },
2505 { "l2", 10, monitor_get_reg },
2506 { "l3", 11, monitor_get_reg },
2507 { "l4", 12, monitor_get_reg },
2508 { "l5", 13, monitor_get_reg },
2509 { "l6", 14, monitor_get_reg },
2510 { "l7", 15, monitor_get_reg },
2511 { "i0", 16, monitor_get_reg },
2512 { "i1", 17, monitor_get_reg },
2513 { "i2", 18, monitor_get_reg },
2514 { "i3", 19, monitor_get_reg },
2515 { "i4", 20, monitor_get_reg },
2516 { "i5", 21, monitor_get_reg },
2517 { "i6", 22, monitor_get_reg },
2518 { "i7", 23, monitor_get_reg },
2519 { "pc", offsetof(CPUState, pc) },
2520 { "npc", offsetof(CPUState, npc) },
2521 { "y", offsetof(CPUState, y) },
2522 #ifndef TARGET_SPARC64
2523 { "psr", 0, &monitor_get_psr, },
2524 { "wim", offsetof(CPUState, wim) },
2525 #endif
2526 { "tbr", offsetof(CPUState, tbr) },
2527 { "fsr", offsetof(CPUState, fsr) },
2528 { "f0", offsetof(CPUState, fpr[0]) },
2529 { "f1", offsetof(CPUState, fpr[1]) },
2530 { "f2", offsetof(CPUState, fpr[2]) },
2531 { "f3", offsetof(CPUState, fpr[3]) },
2532 { "f4", offsetof(CPUState, fpr[4]) },
2533 { "f5", offsetof(CPUState, fpr[5]) },
2534 { "f6", offsetof(CPUState, fpr[6]) },
2535 { "f7", offsetof(CPUState, fpr[7]) },
2536 { "f8", offsetof(CPUState, fpr[8]) },
2537 { "f9", offsetof(CPUState, fpr[9]) },
2538 { "f10", offsetof(CPUState, fpr[10]) },
2539 { "f11", offsetof(CPUState, fpr[11]) },
2540 { "f12", offsetof(CPUState, fpr[12]) },
2541 { "f13", offsetof(CPUState, fpr[13]) },
2542 { "f14", offsetof(CPUState, fpr[14]) },
2543 { "f15", offsetof(CPUState, fpr[15]) },
2544 { "f16", offsetof(CPUState, fpr[16]) },
2545 { "f17", offsetof(CPUState, fpr[17]) },
2546 { "f18", offsetof(CPUState, fpr[18]) },
2547 { "f19", offsetof(CPUState, fpr[19]) },
2548 { "f20", offsetof(CPUState, fpr[20]) },
2549 { "f21", offsetof(CPUState, fpr[21]) },
2550 { "f22", offsetof(CPUState, fpr[22]) },
2551 { "f23", offsetof(CPUState, fpr[23]) },
2552 { "f24", offsetof(CPUState, fpr[24]) },
2553 { "f25", offsetof(CPUState, fpr[25]) },
2554 { "f26", offsetof(CPUState, fpr[26]) },
2555 { "f27", offsetof(CPUState, fpr[27]) },
2556 { "f28", offsetof(CPUState, fpr[28]) },
2557 { "f29", offsetof(CPUState, fpr[29]) },
2558 { "f30", offsetof(CPUState, fpr[30]) },
2559 { "f31", offsetof(CPUState, fpr[31]) },
2560 #ifdef TARGET_SPARC64
2561 { "f32", offsetof(CPUState, fpr[32]) },
2562 { "f34", offsetof(CPUState, fpr[34]) },
2563 { "f36", offsetof(CPUState, fpr[36]) },
2564 { "f38", offsetof(CPUState, fpr[38]) },
2565 { "f40", offsetof(CPUState, fpr[40]) },
2566 { "f42", offsetof(CPUState, fpr[42]) },
2567 { "f44", offsetof(CPUState, fpr[44]) },
2568 { "f46", offsetof(CPUState, fpr[46]) },
2569 { "f48", offsetof(CPUState, fpr[48]) },
2570 { "f50", offsetof(CPUState, fpr[50]) },
2571 { "f52", offsetof(CPUState, fpr[52]) },
2572 { "f54", offsetof(CPUState, fpr[54]) },
2573 { "f56", offsetof(CPUState, fpr[56]) },
2574 { "f58", offsetof(CPUState, fpr[58]) },
2575 { "f60", offsetof(CPUState, fpr[60]) },
2576 { "f62", offsetof(CPUState, fpr[62]) },
2577 { "asi", offsetof(CPUState, asi) },
2578 { "pstate", offsetof(CPUState, pstate) },
2579 { "cansave", offsetof(CPUState, cansave) },
2580 { "canrestore", offsetof(CPUState, canrestore) },
2581 { "otherwin", offsetof(CPUState, otherwin) },
2582 { "wstate", offsetof(CPUState, wstate) },
2583 { "cleanwin", offsetof(CPUState, cleanwin) },
2584 { "fprs", offsetof(CPUState, fprs) },
2585 #endif
2586 #endif
2587 { NULL },
2590 static void expr_error(Monitor *mon, const char *msg)
2592 monitor_printf(mon, "%s\n", msg);
2593 longjmp(expr_env, 1);
2596 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2597 static int get_monitor_def(target_long *pval, const char *name)
2599 const MonitorDef *md;
2600 void *ptr;
2602 for(md = monitor_defs; md->name != NULL; md++) {
2603 if (compare_cmd(name, md->name)) {
2604 if (md->get_value) {
2605 *pval = md->get_value(md, md->offset);
2606 } else {
2607 CPUState *env = mon_get_cpu();
2608 if (!env)
2609 return -2;
2610 ptr = (uint8_t *)env + md->offset;
2611 switch(md->type) {
2612 case MD_I32:
2613 *pval = *(int32_t *)ptr;
2614 break;
2615 case MD_TLONG:
2616 *pval = *(target_long *)ptr;
2617 break;
2618 default:
2619 *pval = 0;
2620 break;
2623 return 0;
2626 return -1;
2629 static void next(void)
2631 if (*pch != '\0') {
2632 pch++;
2633 while (qemu_isspace(*pch))
2634 pch++;
2638 static int64_t expr_sum(Monitor *mon);
2640 static int64_t expr_unary(Monitor *mon)
2642 int64_t n;
2643 char *p;
2644 int ret;
2646 switch(*pch) {
2647 case '+':
2648 next();
2649 n = expr_unary(mon);
2650 break;
2651 case '-':
2652 next();
2653 n = -expr_unary(mon);
2654 break;
2655 case '~':
2656 next();
2657 n = ~expr_unary(mon);
2658 break;
2659 case '(':
2660 next();
2661 n = expr_sum(mon);
2662 if (*pch != ')') {
2663 expr_error(mon, "')' expected");
2665 next();
2666 break;
2667 case '\'':
2668 pch++;
2669 if (*pch == '\0')
2670 expr_error(mon, "character constant expected");
2671 n = *pch;
2672 pch++;
2673 if (*pch != '\'')
2674 expr_error(mon, "missing terminating \' character");
2675 next();
2676 break;
2677 case '$':
2679 char buf[128], *q;
2680 target_long reg=0;
2682 pch++;
2683 q = buf;
2684 while ((*pch >= 'a' && *pch <= 'z') ||
2685 (*pch >= 'A' && *pch <= 'Z') ||
2686 (*pch >= '0' && *pch <= '9') ||
2687 *pch == '_' || *pch == '.') {
2688 if ((q - buf) < sizeof(buf) - 1)
2689 *q++ = *pch;
2690 pch++;
2692 while (qemu_isspace(*pch))
2693 pch++;
2694 *q = 0;
2695 ret = get_monitor_def(&reg, buf);
2696 if (ret == -1)
2697 expr_error(mon, "unknown register");
2698 else if (ret == -2)
2699 expr_error(mon, "no cpu defined");
2700 n = reg;
2702 break;
2703 case '\0':
2704 expr_error(mon, "unexpected end of expression");
2705 n = 0;
2706 break;
2707 default:
2708 #if TARGET_PHYS_ADDR_BITS > 32
2709 n = strtoull(pch, &p, 0);
2710 #else
2711 n = strtoul(pch, &p, 0);
2712 #endif
2713 if (pch == p) {
2714 expr_error(mon, "invalid char in expression");
2716 pch = p;
2717 while (qemu_isspace(*pch))
2718 pch++;
2719 break;
2721 return n;
2725 static int64_t expr_prod(Monitor *mon)
2727 int64_t val, val2;
2728 int op;
2730 val = expr_unary(mon);
2731 for(;;) {
2732 op = *pch;
2733 if (op != '*' && op != '/' && op != '%')
2734 break;
2735 next();
2736 val2 = expr_unary(mon);
2737 switch(op) {
2738 default:
2739 case '*':
2740 val *= val2;
2741 break;
2742 case '/':
2743 case '%':
2744 if (val2 == 0)
2745 expr_error(mon, "division by zero");
2746 if (op == '/')
2747 val /= val2;
2748 else
2749 val %= val2;
2750 break;
2753 return val;
2756 static int64_t expr_logic(Monitor *mon)
2758 int64_t val, val2;
2759 int op;
2761 val = expr_prod(mon);
2762 for(;;) {
2763 op = *pch;
2764 if (op != '&' && op != '|' && op != '^')
2765 break;
2766 next();
2767 val2 = expr_prod(mon);
2768 switch(op) {
2769 default:
2770 case '&':
2771 val &= val2;
2772 break;
2773 case '|':
2774 val |= val2;
2775 break;
2776 case '^':
2777 val ^= val2;
2778 break;
2781 return val;
2784 static int64_t expr_sum(Monitor *mon)
2786 int64_t val, val2;
2787 int op;
2789 val = expr_logic(mon);
2790 for(;;) {
2791 op = *pch;
2792 if (op != '+' && op != '-')
2793 break;
2794 next();
2795 val2 = expr_logic(mon);
2796 if (op == '+')
2797 val += val2;
2798 else
2799 val -= val2;
2801 return val;
2804 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2806 pch = *pp;
2807 if (setjmp(expr_env)) {
2808 *pp = pch;
2809 return -1;
2811 while (qemu_isspace(*pch))
2812 pch++;
2813 *pval = expr_sum(mon);
2814 *pp = pch;
2815 return 0;
2818 static int get_str(char *buf, int buf_size, const char **pp)
2820 const char *p;
2821 char *q;
2822 int c;
2824 q = buf;
2825 p = *pp;
2826 while (qemu_isspace(*p))
2827 p++;
2828 if (*p == '\0') {
2829 fail:
2830 *q = '\0';
2831 *pp = p;
2832 return -1;
2834 if (*p == '\"') {
2835 p++;
2836 while (*p != '\0' && *p != '\"') {
2837 if (*p == '\\') {
2838 p++;
2839 c = *p++;
2840 switch(c) {
2841 case 'n':
2842 c = '\n';
2843 break;
2844 case 'r':
2845 c = '\r';
2846 break;
2847 case '\\':
2848 case '\'':
2849 case '\"':
2850 break;
2851 default:
2852 qemu_printf("unsupported escape code: '\\%c'\n", c);
2853 goto fail;
2855 if ((q - buf) < buf_size - 1) {
2856 *q++ = c;
2858 } else {
2859 if ((q - buf) < buf_size - 1) {
2860 *q++ = *p;
2862 p++;
2865 if (*p != '\"') {
2866 qemu_printf("unterminated string\n");
2867 goto fail;
2869 p++;
2870 } else {
2871 while (*p != '\0' && !qemu_isspace(*p)) {
2872 if ((q - buf) < buf_size - 1) {
2873 *q++ = *p;
2875 p++;
2878 *q = '\0';
2879 *pp = p;
2880 return 0;
2884 * Store the command-name in cmdname, and return a pointer to
2885 * the remaining of the command string.
2887 static const char *get_command_name(const char *cmdline,
2888 char *cmdname, size_t nlen)
2890 size_t len;
2891 const char *p, *pstart;
2893 p = cmdline;
2894 while (qemu_isspace(*p))
2895 p++;
2896 if (*p == '\0')
2897 return NULL;
2898 pstart = p;
2899 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2900 p++;
2901 len = p - pstart;
2902 if (len > nlen - 1)
2903 len = nlen - 1;
2904 memcpy(cmdname, pstart, len);
2905 cmdname[len] = '\0';
2906 return p;
2910 * Read key of 'type' into 'key' and return the current
2911 * 'type' pointer.
2913 static char *key_get_info(const char *type, char **key)
2915 size_t len;
2916 char *p, *str;
2918 if (*type == ',')
2919 type++;
2921 p = strchr(type, ':');
2922 if (!p) {
2923 *key = NULL;
2924 return NULL;
2926 len = p - type;
2928 str = qemu_malloc(len + 1);
2929 memcpy(str, type, len);
2930 str[len] = '\0';
2932 *key = str;
2933 return ++p;
2936 static int default_fmt_format = 'x';
2937 static int default_fmt_size = 4;
2939 #define MAX_ARGS 16
2941 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2942 const char *cmdline,
2943 QDict *qdict)
2945 const char *p, *typestr;
2946 int c;
2947 const mon_cmd_t *cmd;
2948 char cmdname[256];
2949 char buf[1024];
2950 char *key;
2952 #ifdef DEBUG
2953 monitor_printf(mon, "command='%s'\n", cmdline);
2954 #endif
2956 /* extract the command name */
2957 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2958 if (!p)
2959 return NULL;
2961 /* find the command */
2962 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2963 if (compare_cmd(cmdname, cmd->name))
2964 break;
2967 if (cmd->name == NULL) {
2968 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2969 return NULL;
2972 /* parse the parameters */
2973 typestr = cmd->args_type;
2974 for(;;) {
2975 typestr = key_get_info(typestr, &key);
2976 if (!typestr)
2977 break;
2978 c = *typestr;
2979 typestr++;
2980 switch(c) {
2981 case 'F':
2982 case 'B':
2983 case 's':
2985 int ret;
2987 while (qemu_isspace(*p))
2988 p++;
2989 if (*typestr == '?') {
2990 typestr++;
2991 if (*p == '\0') {
2992 /* no optional string: NULL argument */
2993 break;
2996 ret = get_str(buf, sizeof(buf), &p);
2997 if (ret < 0) {
2998 switch(c) {
2999 case 'F':
3000 monitor_printf(mon, "%s: filename expected\n",
3001 cmdname);
3002 break;
3003 case 'B':
3004 monitor_printf(mon, "%s: block device name expected\n",
3005 cmdname);
3006 break;
3007 default:
3008 monitor_printf(mon, "%s: string expected\n", cmdname);
3009 break;
3011 goto fail;
3013 qdict_put(qdict, key, qstring_from_str(buf));
3015 break;
3016 case '/':
3018 int count, format, size;
3020 while (qemu_isspace(*p))
3021 p++;
3022 if (*p == '/') {
3023 /* format found */
3024 p++;
3025 count = 1;
3026 if (qemu_isdigit(*p)) {
3027 count = 0;
3028 while (qemu_isdigit(*p)) {
3029 count = count * 10 + (*p - '0');
3030 p++;
3033 size = -1;
3034 format = -1;
3035 for(;;) {
3036 switch(*p) {
3037 case 'o':
3038 case 'd':
3039 case 'u':
3040 case 'x':
3041 case 'i':
3042 case 'c':
3043 format = *p++;
3044 break;
3045 case 'b':
3046 size = 1;
3047 p++;
3048 break;
3049 case 'h':
3050 size = 2;
3051 p++;
3052 break;
3053 case 'w':
3054 size = 4;
3055 p++;
3056 break;
3057 case 'g':
3058 case 'L':
3059 size = 8;
3060 p++;
3061 break;
3062 default:
3063 goto next;
3066 next:
3067 if (*p != '\0' && !qemu_isspace(*p)) {
3068 monitor_printf(mon, "invalid char in format: '%c'\n",
3069 *p);
3070 goto fail;
3072 if (format < 0)
3073 format = default_fmt_format;
3074 if (format != 'i') {
3075 /* for 'i', not specifying a size gives -1 as size */
3076 if (size < 0)
3077 size = default_fmt_size;
3078 default_fmt_size = size;
3080 default_fmt_format = format;
3081 } else {
3082 count = 1;
3083 format = default_fmt_format;
3084 if (format != 'i') {
3085 size = default_fmt_size;
3086 } else {
3087 size = -1;
3090 qdict_put(qdict, "count", qint_from_int(count));
3091 qdict_put(qdict, "format", qint_from_int(format));
3092 qdict_put(qdict, "size", qint_from_int(size));
3094 break;
3095 case 'i':
3096 case 'l':
3098 int64_t val;
3100 while (qemu_isspace(*p))
3101 p++;
3102 if (*typestr == '?' || *typestr == '.') {
3103 if (*typestr == '?') {
3104 if (*p == '\0') {
3105 typestr++;
3106 break;
3108 } else {
3109 if (*p == '.') {
3110 p++;
3111 while (qemu_isspace(*p))
3112 p++;
3113 } else {
3114 typestr++;
3115 break;
3118 typestr++;
3120 if (get_expr(mon, &val, &p))
3121 goto fail;
3122 /* Check if 'i' is greater than 32-bit */
3123 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3124 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3125 monitor_printf(mon, "integer is for 32-bit values\n");
3126 goto fail;
3128 qdict_put(qdict, key, qint_from_int(val));
3130 break;
3131 case '-':
3133 int has_option;
3134 /* option */
3136 c = *typestr++;
3137 if (c == '\0')
3138 goto bad_type;
3139 while (qemu_isspace(*p))
3140 p++;
3141 has_option = 0;
3142 if (*p == '-') {
3143 p++;
3144 if (*p != c) {
3145 monitor_printf(mon, "%s: unsupported option -%c\n",
3146 cmdname, *p);
3147 goto fail;
3149 p++;
3150 has_option = 1;
3152 qdict_put(qdict, key, qint_from_int(has_option));
3154 break;
3155 default:
3156 bad_type:
3157 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3158 goto fail;
3160 qemu_free(key);
3161 key = NULL;
3163 /* check that all arguments were parsed */
3164 while (qemu_isspace(*p))
3165 p++;
3166 if (*p != '\0') {
3167 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3168 cmdname);
3169 goto fail;
3172 return cmd;
3174 fail:
3175 qemu_free(key);
3176 return NULL;
3179 static void monitor_handle_command(Monitor *mon, const char *cmdline)
3181 QDict *qdict;
3182 const mon_cmd_t *cmd;
3184 qdict = qdict_new();
3186 cmd = monitor_parse_command(mon, cmdline, qdict);
3187 if (!cmd)
3188 goto out;
3190 qemu_errors_to_mon(mon);
3192 if (monitor_handler_ported(cmd)) {
3193 QObject *data = NULL;
3195 cmd->mhandler.cmd_new(mon, qdict, &data);
3196 if (data)
3197 cmd->user_print(mon, data);
3199 qobject_decref(data);
3200 } else {
3201 cmd->mhandler.cmd(mon, qdict);
3204 qemu_errors_to_previous();
3206 out:
3207 QDECREF(qdict);
3210 static void cmd_completion(const char *name, const char *list)
3212 const char *p, *pstart;
3213 char cmd[128];
3214 int len;
3216 p = list;
3217 for(;;) {
3218 pstart = p;
3219 p = strchr(p, '|');
3220 if (!p)
3221 p = pstart + strlen(pstart);
3222 len = p - pstart;
3223 if (len > sizeof(cmd) - 2)
3224 len = sizeof(cmd) - 2;
3225 memcpy(cmd, pstart, len);
3226 cmd[len] = '\0';
3227 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3228 readline_add_completion(cur_mon->rs, cmd);
3230 if (*p == '\0')
3231 break;
3232 p++;
3236 static void file_completion(const char *input)
3238 DIR *ffs;
3239 struct dirent *d;
3240 char path[1024];
3241 char file[1024], file_prefix[1024];
3242 int input_path_len;
3243 const char *p;
3245 p = strrchr(input, '/');
3246 if (!p) {
3247 input_path_len = 0;
3248 pstrcpy(file_prefix, sizeof(file_prefix), input);
3249 pstrcpy(path, sizeof(path), ".");
3250 } else {
3251 input_path_len = p - input + 1;
3252 memcpy(path, input, input_path_len);
3253 if (input_path_len > sizeof(path) - 1)
3254 input_path_len = sizeof(path) - 1;
3255 path[input_path_len] = '\0';
3256 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3258 #ifdef DEBUG_COMPLETION
3259 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3260 input, path, file_prefix);
3261 #endif
3262 ffs = opendir(path);
3263 if (!ffs)
3264 return;
3265 for(;;) {
3266 struct stat sb;
3267 d = readdir(ffs);
3268 if (!d)
3269 break;
3270 if (strstart(d->d_name, file_prefix, NULL)) {
3271 memcpy(file, input, input_path_len);
3272 if (input_path_len < sizeof(file))
3273 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3274 d->d_name);
3275 /* stat the file to find out if it's a directory.
3276 * In that case add a slash to speed up typing long paths
3278 stat(file, &sb);
3279 if(S_ISDIR(sb.st_mode))
3280 pstrcat(file, sizeof(file), "/");
3281 readline_add_completion(cur_mon->rs, file);
3284 closedir(ffs);
3287 static void block_completion_it(void *opaque, BlockDriverState *bs)
3289 const char *name = bdrv_get_device_name(bs);
3290 const char *input = opaque;
3292 if (input[0] == '\0' ||
3293 !strncmp(name, (char *)input, strlen(input))) {
3294 readline_add_completion(cur_mon->rs, name);
3298 /* NOTE: this parser is an approximate form of the real command parser */
3299 static void parse_cmdline(const char *cmdline,
3300 int *pnb_args, char **args)
3302 const char *p;
3303 int nb_args, ret;
3304 char buf[1024];
3306 p = cmdline;
3307 nb_args = 0;
3308 for(;;) {
3309 while (qemu_isspace(*p))
3310 p++;
3311 if (*p == '\0')
3312 break;
3313 if (nb_args >= MAX_ARGS)
3314 break;
3315 ret = get_str(buf, sizeof(buf), &p);
3316 args[nb_args] = qemu_strdup(buf);
3317 nb_args++;
3318 if (ret < 0)
3319 break;
3321 *pnb_args = nb_args;
3324 static const char *next_arg_type(const char *typestr)
3326 const char *p = strchr(typestr, ':');
3327 return (p != NULL ? ++p : typestr);
3330 static void monitor_find_completion(const char *cmdline)
3332 const char *cmdname;
3333 char *args[MAX_ARGS];
3334 int nb_args, i, len;
3335 const char *ptype, *str;
3336 const mon_cmd_t *cmd;
3337 const KeyDef *key;
3339 parse_cmdline(cmdline, &nb_args, args);
3340 #ifdef DEBUG_COMPLETION
3341 for(i = 0; i < nb_args; i++) {
3342 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3344 #endif
3346 /* if the line ends with a space, it means we want to complete the
3347 next arg */
3348 len = strlen(cmdline);
3349 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3350 if (nb_args >= MAX_ARGS)
3351 return;
3352 args[nb_args++] = qemu_strdup("");
3354 if (nb_args <= 1) {
3355 /* command completion */
3356 if (nb_args == 0)
3357 cmdname = "";
3358 else
3359 cmdname = args[0];
3360 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3361 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3362 cmd_completion(cmdname, cmd->name);
3364 } else {
3365 /* find the command */
3366 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3367 if (compare_cmd(args[0], cmd->name))
3368 goto found;
3370 return;
3371 found:
3372 ptype = next_arg_type(cmd->args_type);
3373 for(i = 0; i < nb_args - 2; i++) {
3374 if (*ptype != '\0') {
3375 ptype = next_arg_type(ptype);
3376 while (*ptype == '?')
3377 ptype = next_arg_type(ptype);
3380 str = args[nb_args - 1];
3381 if (*ptype == '-' && ptype[1] != '\0') {
3382 ptype += 2;
3384 switch(*ptype) {
3385 case 'F':
3386 /* file completion */
3387 readline_set_completion_index(cur_mon->rs, strlen(str));
3388 file_completion(str);
3389 break;
3390 case 'B':
3391 /* block device name completion */
3392 readline_set_completion_index(cur_mon->rs, strlen(str));
3393 bdrv_iterate(block_completion_it, (void *)str);
3394 break;
3395 case 's':
3396 /* XXX: more generic ? */
3397 if (!strcmp(cmd->name, "info")) {
3398 readline_set_completion_index(cur_mon->rs, strlen(str));
3399 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3400 cmd_completion(str, cmd->name);
3402 } else if (!strcmp(cmd->name, "sendkey")) {
3403 char *sep = strrchr(str, '-');
3404 if (sep)
3405 str = sep + 1;
3406 readline_set_completion_index(cur_mon->rs, strlen(str));
3407 for(key = key_defs; key->name != NULL; key++) {
3408 cmd_completion(str, key->name);
3410 } else if (!strcmp(cmd->name, "help|?")) {
3411 readline_set_completion_index(cur_mon->rs, strlen(str));
3412 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3413 cmd_completion(str, cmd->name);
3416 break;
3417 default:
3418 break;
3421 for(i = 0; i < nb_args; i++)
3422 qemu_free(args[i]);
3425 static int monitor_can_read(void *opaque)
3427 Monitor *mon = opaque;
3429 return (mon->suspend_cnt == 0) ? 128 : 0;
3432 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3434 Monitor *old_mon = cur_mon;
3435 int i;
3437 cur_mon = opaque;
3439 if (cur_mon->rs) {
3440 for (i = 0; i < size; i++)
3441 readline_handle_byte(cur_mon->rs, buf[i]);
3442 } else {
3443 if (size == 0 || buf[size - 1] != 0)
3444 monitor_printf(cur_mon, "corrupted command\n");
3445 else
3446 monitor_handle_command(cur_mon, (char *)buf);
3449 cur_mon = old_mon;
3452 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3454 monitor_suspend(mon);
3455 monitor_handle_command(mon, cmdline);
3456 monitor_resume(mon);
3459 int monitor_suspend(Monitor *mon)
3461 if (!mon->rs)
3462 return -ENOTTY;
3463 mon->suspend_cnt++;
3464 return 0;
3467 void monitor_resume(Monitor *mon)
3469 if (!mon->rs)
3470 return;
3471 if (--mon->suspend_cnt == 0)
3472 readline_show_prompt(mon->rs);
3475 static void monitor_event(void *opaque, int event)
3477 Monitor *mon = opaque;
3479 switch (event) {
3480 case CHR_EVENT_MUX_IN:
3481 mon->mux_out = 0;
3482 if (mon->reset_seen) {
3483 readline_restart(mon->rs);
3484 monitor_resume(mon);
3485 monitor_flush(mon);
3486 } else {
3487 mon->suspend_cnt = 0;
3489 break;
3491 case CHR_EVENT_MUX_OUT:
3492 if (mon->reset_seen) {
3493 if (mon->suspend_cnt == 0) {
3494 monitor_printf(mon, "\n");
3496 monitor_flush(mon);
3497 monitor_suspend(mon);
3498 } else {
3499 mon->suspend_cnt++;
3501 mon->mux_out = 1;
3502 break;
3504 case CHR_EVENT_RESET:
3505 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3506 "information\n", QEMU_VERSION);
3507 if (!mon->mux_out) {
3508 readline_show_prompt(mon->rs);
3510 mon->reset_seen = 1;
3511 break;
3517 * Local variables:
3518 * c-indent-level: 4
3519 * c-basic-offset: 4
3520 * tab-width: 8
3521 * End:
3524 void monitor_init(CharDriverState *chr, int flags)
3526 static int is_first_init = 1;
3527 Monitor *mon;
3529 if (is_first_init) {
3530 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3531 is_first_init = 0;
3534 mon = qemu_mallocz(sizeof(*mon));
3536 mon->chr = chr;
3537 mon->flags = flags;
3538 if (flags & MONITOR_USE_READLINE) {
3539 mon->rs = readline_init(mon, monitor_find_completion);
3540 monitor_read_command(mon, 0);
3543 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3544 mon);
3546 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3547 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3548 cur_mon = mon;
3551 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3553 BlockDriverState *bs = opaque;
3554 int ret = 0;
3556 if (bdrv_set_key(bs, password) != 0) {
3557 monitor_printf(mon, "invalid password\n");
3558 ret = -EPERM;
3560 if (mon->password_completion_cb)
3561 mon->password_completion_cb(mon->password_opaque, ret);
3563 monitor_read_command(mon, 1);
3566 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3567 BlockDriverCompletionFunc *completion_cb,
3568 void *opaque)
3570 int err;
3572 if (!bdrv_key_required(bs)) {
3573 if (completion_cb)
3574 completion_cb(opaque, 0);
3575 return;
3578 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3579 bdrv_get_encrypted_filename(bs));
3581 mon->password_completion_cb = completion_cb;
3582 mon->password_opaque = opaque;
3584 err = monitor_read_password(mon, bdrv_password_cb, bs);
3586 if (err && completion_cb)
3587 completion_cb(opaque, err);
3590 typedef struct QemuErrorSink QemuErrorSink;
3591 struct QemuErrorSink {
3592 enum {
3593 ERR_SINK_FILE,
3594 ERR_SINK_MONITOR,
3595 } dest;
3596 union {
3597 FILE *fp;
3598 Monitor *mon;
3600 QemuErrorSink *previous;
3603 static QemuErrorSink *qemu_error_sink;
3605 void qemu_errors_to_file(FILE *fp)
3607 QemuErrorSink *sink;
3609 sink = qemu_mallocz(sizeof(*sink));
3610 sink->dest = ERR_SINK_FILE;
3611 sink->fp = fp;
3612 sink->previous = qemu_error_sink;
3613 qemu_error_sink = sink;
3616 void qemu_errors_to_mon(Monitor *mon)
3618 QemuErrorSink *sink;
3620 sink = qemu_mallocz(sizeof(*sink));
3621 sink->dest = ERR_SINK_MONITOR;
3622 sink->mon = mon;
3623 sink->previous = qemu_error_sink;
3624 qemu_error_sink = sink;
3627 void qemu_errors_to_previous(void)
3629 QemuErrorSink *sink;
3631 assert(qemu_error_sink != NULL);
3632 sink = qemu_error_sink;
3633 qemu_error_sink = sink->previous;
3634 qemu_free(sink);
3637 void qemu_error(const char *fmt, ...)
3639 va_list args;
3641 assert(qemu_error_sink != NULL);
3642 switch (qemu_error_sink->dest) {
3643 case ERR_SINK_FILE:
3644 va_start(args, fmt);
3645 vfprintf(qemu_error_sink->fp, fmt, args);
3646 va_end(args);
3647 break;
3648 case ERR_SINK_MONITOR:
3649 va_start(args, fmt);
3650 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3651 va_end(args);
3652 break;