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
29 //#define DEBUG_COMPLETION
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
39 * 'B' block device name
40 * 's' string (accept optional quote)
42 * 'l' target long (32 or 64 bit)
43 * '/' optional gdb-like print format (like "/10x")
45 * '?' optional type (for 'F', 's' and 'i')
49 typedef struct term_cmd_t {
51 const char *args_type;
57 static CharDriverState *monitor_hd;
58 static int hide_banner;
60 static term_cmd_t term_cmds[];
61 static term_cmd_t info_cmds[];
63 static char term_outbuf[1024];
64 static int term_outbuf_index;
66 static void monitor_start_input(void);
68 CPUState *mon_cpu = NULL;
72 if (term_outbuf_index > 0) {
73 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
74 term_outbuf_index = 0;
78 /* flush at every end of line or if the buffer is full */
79 void term_puts(const char *str)
87 term_outbuf[term_outbuf_index++] = '\r';
88 term_outbuf[term_outbuf_index++] = c;
89 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
95 void term_vprintf(const char *fmt, va_list ap)
98 vsnprintf(buf, sizeof(buf), fmt, ap);
102 void term_printf(const char *fmt, ...)
106 term_vprintf(fmt, ap);
110 void term_print_filename(const char *filename)
114 for (i = 0; filename[i]; i++) {
115 switch (filename[i]) {
119 term_printf("\\%c", filename[i]);
131 term_printf("%c", filename[i]);
137 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
141 term_vprintf(fmt, ap);
146 static int compare_cmd(const char *name, const char *list)
148 const char *p, *pstart;
156 p = pstart + strlen(pstart);
157 if ((p - pstart) == len && !memcmp(pstart, name, len))
166 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
170 for(cmd = cmds; cmd->name != NULL; cmd++) {
171 if (!name || !strcmp(name, cmd->name))
172 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
176 static void help_cmd(const char *name)
178 if (name && !strcmp(name, "info")) {
179 help_cmd1(info_cmds, "info ", NULL);
181 help_cmd1(term_cmds, "", name);
182 if (name && !strcmp(name, "log")) {
184 term_printf("Log items (comma separated):\n");
185 term_printf("%-10s %s\n", "none", "remove all logs");
186 for(item = cpu_log_items; item->mask != 0; item++) {
187 term_printf("%-10s %s\n", item->name, item->help);
193 static void do_help(const char *name)
198 static void do_commit(const char *device)
202 all_devices = !strcmp(device, "all");
203 for (i = 0; i < MAX_DISKS; i++) {
206 !strcmp(bdrv_get_device_name(bs_table[i]), device))
207 bdrv_commit(bs_table[i]);
212 static void do_info(const char *item)
218 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
219 if (compare_cmd(item, cmd->name))
229 static void do_info_version(void)
231 term_printf("%s\n", QEMU_VERSION);
234 static void do_info_block(void)
239 /* get the current CPU defined by the user */
240 int mon_set_cpu(int cpu_index)
244 for(env = first_cpu; env != NULL; env = env->next_cpu) {
245 if (env->cpu_index == cpu_index) {
253 CPUState *mon_get_cpu(void)
261 static void do_info_registers(void)
268 cpu_dump_state(env, NULL, monitor_fprintf,
271 cpu_dump_state(env, NULL, monitor_fprintf,
276 static void do_info_cpus(void)
280 /* just to set the default cpu if not already done */
283 for(env = first_cpu; env != NULL; env = env->next_cpu) {
284 term_printf("%c CPU #%d:",
285 (env == mon_cpu) ? '*' : ' ',
287 #if defined(TARGET_I386)
288 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
289 if (env->hflags & HF_HALTED_MASK)
290 term_printf(" (halted)");
291 #elif defined(TARGET_PPC)
292 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
294 term_printf(" (halted)");
295 #elif defined(TARGET_SPARC)
296 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
298 term_printf(" (halted)");
304 static void do_cpu_set(int index)
306 if (mon_set_cpu(index) < 0)
307 term_printf("Invalid CPU index\n");
310 static void do_info_jit(void)
312 dump_exec_info(NULL, monitor_fprintf);
315 static void do_info_history (void)
322 str = readline_get_history(i);
325 term_printf("%d: '%s'\n", i, str);
330 static void do_quit(void)
335 static int eject_device(BlockDriverState *bs, int force)
337 if (bdrv_is_inserted(bs)) {
339 if (!bdrv_is_removable(bs)) {
340 term_printf("device is not removable\n");
343 if (bdrv_is_locked(bs)) {
344 term_printf("device is locked\n");
353 static void do_eject(int force, const char *filename)
355 BlockDriverState *bs;
357 bs = bdrv_find(filename);
359 term_printf("device not found\n");
362 eject_device(bs, force);
365 static void do_change(const char *device, const char *filename)
367 BlockDriverState *bs;
371 bs = bdrv_find(device);
373 term_printf("device not found\n");
376 if (eject_device(bs, 0) < 0)
378 bdrv_open(bs, filename, 0);
379 if (bdrv_is_encrypted(bs)) {
380 term_printf("%s is encrypted.\n", device);
381 for(i = 0; i < 3; i++) {
382 monitor_readline("Password: ", 1, password, sizeof(password));
383 if (bdrv_set_key(bs, password) == 0)
385 term_printf("invalid password\n");
390 static void do_screen_dump(const char *filename)
392 vga_hw_screen_dump(filename);
395 static void do_log(const char *items)
399 if (!strcmp(items, "none")) {
402 mask = cpu_str_to_log_mask(items);
411 static void do_stop(void)
413 vm_stop(EXCP_INTERRUPT);
416 static void do_cont(void)
421 #ifdef CONFIG_GDBSTUB
422 static void do_gdbserver(int has_port, int port)
425 port = DEFAULT_GDBSTUB_PORT;
426 if (gdbserver_start(port) < 0) {
427 qemu_printf("Could not open gdbserver socket on port %d\n", port);
429 qemu_printf("Waiting gdb connection on port %d\n", port);
434 static void term_printc(int c)
451 if (c >= 32 && c <= 126) {
452 term_printf("%c", c);
454 term_printf("\\x%02x", c);
461 static void memory_dump(int count, int format, int wsize,
462 target_ulong addr, int is_physical)
465 int nb_per_line, l, line_size, i, max_digits, len;
473 if (!env && !is_physical)
478 } else if (wsize == 4) {
481 /* as default we use the current CS size */
485 if ((env->efer & MSR_EFER_LMA) &&
486 (env->segs[R_CS].flags & DESC_L_MASK))
490 if (!(env->segs[R_CS].flags & DESC_B_MASK))
495 monitor_disas(env, addr, count, is_physical, flags);
504 nb_per_line = line_size / wsize;
509 max_digits = (wsize * 8 + 2) / 3;
513 max_digits = (wsize * 8) / 4;
517 max_digits = (wsize * 8 * 10 + 32) / 33;
525 term_printf(TARGET_FMT_lx ":", addr);
530 cpu_physical_memory_rw(addr, buf, l, 0);
535 cpu_memory_rw_debug(env, addr, buf, l, 0);
542 v = ldub_raw(buf + i);
545 v = lduw_raw(buf + i);
548 v = (uint32_t)ldl_raw(buf + i);
551 v = ldq_raw(buf + i);
557 term_printf("%#*" PRIo64, max_digits, v);
560 term_printf("0x%0*" PRIx64, max_digits, v);
563 term_printf("%*" PRIu64, max_digits, v);
566 term_printf("%*" PRId64, max_digits, v);
580 #if TARGET_LONG_BITS == 64
581 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
583 #define GET_TLONG(h, l) (l)
586 static void do_memory_dump(int count, int format, int size,
587 uint32_t addrh, uint32_t addrl)
589 target_long addr = GET_TLONG(addrh, addrl);
590 memory_dump(count, format, size, addr, 0);
593 static void do_physical_memory_dump(int count, int format, int size,
594 uint32_t addrh, uint32_t addrl)
597 target_long addr = GET_TLONG(addrh, addrl);
598 memory_dump(count, format, size, addr, 1);
601 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
603 target_long val = GET_TLONG(valh, vall);
604 #if TARGET_LONG_BITS == 32
607 term_printf("%#o", val);
610 term_printf("%#x", val);
613 term_printf("%u", val);
617 term_printf("%d", val);
626 term_printf("%#" PRIo64, val);
629 term_printf("%#" PRIx64, val);
632 term_printf("%" PRIu64, val);
636 term_printf("%" PRId64, val);
646 static void do_memory_save(unsigned int valh, unsigned int vall,
647 uint32_t size, const char *filename)
650 target_long addr = GET_TLONG(valh, vall);
659 f = fopen(filename, "wb");
661 term_printf("could not open '%s'\n", filename);
668 cpu_memory_rw_debug(env, addr, buf, l, 0);
669 fwrite(buf, 1, l, f);
676 static void do_sum(uint32_t start, uint32_t size)
683 for(addr = start; addr < (start + size); addr++) {
684 cpu_physical_memory_rw(addr, buf, 1, 0);
685 /* BSD sum algorithm ('sum' Unix command) */
686 sum = (sum >> 1) | (sum << 15);
689 term_printf("%05d\n", sum);
697 static const KeyDef key_defs[] = {
722 { 0x0e, "backspace" },
757 { 0x3a, "caps_lock" },
768 { 0x45, "num_lock" },
769 { 0x46, "scroll_lock" },
771 { 0xb5, "kp_divide" },
772 { 0x37, "kp_multiply" },
773 { 0x4a, "kp_substract" },
775 { 0x9c, "kp_enter" },
776 { 0x53, "kp_decimal" },
811 static int get_keycode(const char *key)
817 for(p = key_defs; p->name != NULL; p++) {
818 if (!strcmp(key, p->name))
821 if (strstart(key, "0x", NULL)) {
822 ret = strtoul(key, &endp, 0);
823 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
829 static void do_send_key(const char *string)
832 uint8_t keycodes[16];
834 int nb_keycodes, keycode, i;
840 while (*p != '\0' && *p != '-') {
841 if ((q - keybuf) < sizeof(keybuf) - 1) {
847 keycode = get_keycode(keybuf);
849 term_printf("unknown key: '%s'\n", keybuf);
852 keycodes[nb_keycodes++] = keycode;
857 /* key down events */
858 for(i = 0; i < nb_keycodes; i++) {
859 keycode = keycodes[i];
861 kbd_put_keycode(0xe0);
862 kbd_put_keycode(keycode & 0x7f);
865 for(i = nb_keycodes - 1; i >= 0; i--) {
866 keycode = keycodes[i];
868 kbd_put_keycode(0xe0);
869 kbd_put_keycode(keycode | 0x80);
873 static int mouse_button_state;
875 static void do_mouse_move(const char *dx_str, const char *dy_str,
879 dx = strtol(dx_str, NULL, 0);
880 dy = strtol(dy_str, NULL, 0);
883 dz = strtol(dz_str, NULL, 0);
884 kbd_mouse_event(dx, dy, dz, mouse_button_state);
887 static void do_mouse_button(int button_state)
889 mouse_button_state = button_state;
890 kbd_mouse_event(0, 0, 0, mouse_button_state);
893 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
899 cpu_outb(NULL, addr & 0xffff, index & 0xff);
907 val = cpu_inb(NULL, addr);
911 val = cpu_inw(NULL, addr);
915 val = cpu_inl(NULL, addr);
919 term_printf("port%c[0x%04x] = %#0*x\n",
920 suffix, addr, size * 2, val);
923 static void do_system_reset(void)
925 qemu_system_reset_request();
928 static void do_system_powerdown(void)
930 qemu_system_powerdown_request();
933 #if defined(TARGET_I386)
934 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
936 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
939 pte & PG_GLOBAL_MASK ? 'G' : '-',
940 pte & PG_PSE_MASK ? 'P' : '-',
941 pte & PG_DIRTY_MASK ? 'D' : '-',
942 pte & PG_ACCESSED_MASK ? 'A' : '-',
943 pte & PG_PCD_MASK ? 'C' : '-',
944 pte & PG_PWT_MASK ? 'T' : '-',
945 pte & PG_USER_MASK ? 'U' : '-',
946 pte & PG_RW_MASK ? 'W' : '-');
949 static void tlb_info(void)
953 uint32_t pgd, pde, pte;
959 if (!(env->cr[0] & CR0_PG_MASK)) {
960 term_printf("PG disabled\n");
963 pgd = env->cr[3] & ~0xfff;
964 for(l1 = 0; l1 < 1024; l1++) {
965 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
966 pde = le32_to_cpu(pde);
967 if (pde & PG_PRESENT_MASK) {
968 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
969 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
971 for(l2 = 0; l2 < 1024; l2++) {
972 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
974 pte = le32_to_cpu(pte);
975 if (pte & PG_PRESENT_MASK) {
976 print_pte((l1 << 22) + (l2 << 12),
986 static void mem_print(uint32_t *pstart, int *plast_prot,
987 uint32_t end, int prot)
993 term_printf("%08x-%08x %08x %c%c%c\n",
994 *pstart, end, end - *pstart,
995 prot1 & PG_USER_MASK ? 'u' : '-',
997 prot1 & PG_RW_MASK ? 'w' : '-');
1007 static void mem_info(void)
1010 int l1, l2, prot, last_prot;
1011 uint32_t pgd, pde, pte, start, end;
1013 env = mon_get_cpu();
1017 if (!(env->cr[0] & CR0_PG_MASK)) {
1018 term_printf("PG disabled\n");
1021 pgd = env->cr[3] & ~0xfff;
1024 for(l1 = 0; l1 < 1024; l1++) {
1025 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1026 pde = le32_to_cpu(pde);
1028 if (pde & PG_PRESENT_MASK) {
1029 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1030 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1031 mem_print(&start, &last_prot, end, prot);
1033 for(l2 = 0; l2 < 1024; l2++) {
1034 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1035 (uint8_t *)&pte, 4);
1036 pte = le32_to_cpu(pte);
1037 end = (l1 << 22) + (l2 << 12);
1038 if (pte & PG_PRESENT_MASK) {
1039 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1043 mem_print(&start, &last_prot, end, prot);
1048 mem_print(&start, &last_prot, end, prot);
1054 static void do_info_kqemu(void)
1060 env = mon_get_cpu();
1062 term_printf("No cpu initialized yet");
1065 val = env->kqemu_enabled;
1066 term_printf("kqemu support: ");
1070 term_printf("disabled\n");
1073 term_printf("enabled for user code\n");
1076 term_printf("enabled for user and kernel code\n");
1080 term_printf("kqemu support: not compiled\n");
1084 #ifdef CONFIG_PROFILER
1088 int64_t kqemu_exec_count;
1090 int64_t kqemu_ret_int_count;
1091 int64_t kqemu_ret_excp_count;
1092 int64_t kqemu_ret_intr_count;
1094 static void do_info_profile(void)
1100 term_printf("async time %" PRId64 " (%0.3f)\n",
1101 dev_time, dev_time / (double)ticks_per_sec);
1102 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1103 qemu_time, qemu_time / (double)ticks_per_sec);
1104 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1105 kqemu_time, kqemu_time / (double)ticks_per_sec,
1106 kqemu_time / (double)total * 100.0,
1108 kqemu_ret_int_count,
1109 kqemu_ret_excp_count,
1110 kqemu_ret_intr_count);
1113 kqemu_exec_count = 0;
1115 kqemu_ret_int_count = 0;
1116 kqemu_ret_excp_count = 0;
1117 kqemu_ret_intr_count = 0;
1119 kqemu_record_dump();
1123 static void do_info_profile(void)
1125 term_printf("Internal profiler not compiled\n");
1129 /* Capture support */
1130 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1132 static void do_info_capture (void)
1137 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1138 term_printf ("[%d]: ", i);
1139 s->ops.info (s->opaque);
1143 static void do_stop_capture (int n)
1148 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1150 s->ops.destroy (s->opaque);
1151 LIST_REMOVE (s, entries);
1159 int wav_start_capture (CaptureState *s, const char *path, int freq,
1160 int bits, int nchannels);
1162 static void do_wav_capture (const char *path,
1163 int has_freq, int freq,
1164 int has_bits, int bits,
1165 int has_channels, int nchannels)
1169 s = qemu_mallocz (sizeof (*s));
1171 term_printf ("Not enough memory to add wave capture\n");
1175 freq = has_freq ? freq : 44100;
1176 bits = has_bits ? bits : 16;
1177 nchannels = has_channels ? nchannels : 2;
1179 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1180 term_printf ("Faied to add wave capture\n");
1183 LIST_INSERT_HEAD (&capture_head, s, entries);
1187 static term_cmd_t term_cmds[] = {
1188 { "help|?", "s?", do_help,
1189 "[cmd]", "show the help" },
1190 { "commit", "s", do_commit,
1191 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1192 { "info", "s?", do_info,
1193 "subcommand", "show various information about the system state" },
1194 { "q|quit", "", do_quit,
1195 "", "quit the emulator" },
1196 { "eject", "-fB", do_eject,
1197 "[-f] device", "eject a removable media (use -f to force it)" },
1198 { "change", "BF", do_change,
1199 "device filename", "change a removable media" },
1200 { "screendump", "F", do_screen_dump,
1201 "filename", "save screen into PPM image 'filename'" },
1202 { "log", "s", do_log,
1203 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1204 { "savevm", "s?", do_savevm,
1205 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1206 { "loadvm", "s", do_loadvm,
1207 "tag|id", "restore a VM snapshot from its tag or id" },
1208 { "delvm", "s", do_delvm,
1209 "tag|id", "delete a VM snapshot from its tag or id" },
1210 { "stop", "", do_stop,
1211 "", "stop emulation", },
1212 { "c|cont", "", do_cont,
1213 "", "resume emulation", },
1214 #ifdef CONFIG_GDBSTUB
1215 { "gdbserver", "i?", do_gdbserver,
1216 "[port]", "start gdbserver session (default port=1234)", },
1218 { "x", "/l", do_memory_dump,
1219 "/fmt addr", "virtual memory dump starting at 'addr'", },
1220 { "xp", "/l", do_physical_memory_dump,
1221 "/fmt addr", "physical memory dump starting at 'addr'", },
1222 { "p|print", "/l", do_print,
1223 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1224 { "i", "/ii.", do_ioport_read,
1225 "/fmt addr", "I/O port read" },
1227 { "sendkey", "s", do_send_key,
1228 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1229 { "system_reset", "", do_system_reset,
1230 "", "reset the system" },
1231 { "system_powerdown", "", do_system_powerdown,
1232 "", "send system power down event" },
1233 { "sum", "ii", do_sum,
1234 "addr size", "compute the checksum of a memory region" },
1235 { "usb_add", "s", do_usb_add,
1236 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1237 { "usb_del", "s", do_usb_del,
1238 "device", "remove USB device 'bus.addr'" },
1239 { "cpu", "i", do_cpu_set,
1240 "index", "set the default CPU" },
1241 { "mouse_move", "sss?", do_mouse_move,
1242 "dx dy [dz]", "send mouse move events" },
1243 { "mouse_button", "i", do_mouse_button,
1244 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1245 { "mouse_set", "i", do_mouse_set,
1246 "index", "set which mouse device receives events" },
1248 { "wavcapture", "si?i?i?", do_wav_capture,
1249 "path [frequency bits channels]",
1250 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1252 { "stopcapture", "i", do_stop_capture,
1253 "capture index", "stop capture" },
1254 { "memsave", "lis", do_memory_save,
1255 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1259 static term_cmd_t info_cmds[] = {
1260 { "version", "", do_info_version,
1261 "", "show the version of qemu" },
1262 { "network", "", do_info_network,
1263 "", "show the network state" },
1264 { "block", "", do_info_block,
1265 "", "show the block devices" },
1266 { "registers", "", do_info_registers,
1267 "", "show the cpu registers" },
1268 { "cpus", "", do_info_cpus,
1269 "", "show infos for each CPU" },
1270 { "history", "", do_info_history,
1271 "", "show the command line history", },
1272 { "irq", "", irq_info,
1273 "", "show the interrupts statistics (if available)", },
1274 { "pic", "", pic_info,
1275 "", "show i8259 (PIC) state", },
1276 { "pci", "", pci_info,
1277 "", "show PCI info", },
1278 #if defined(TARGET_I386)
1279 { "tlb", "", tlb_info,
1280 "", "show virtual to physical memory mappings", },
1281 { "mem", "", mem_info,
1282 "", "show the active virtual memory mappings", },
1284 { "jit", "", do_info_jit,
1285 "", "show dynamic compiler info", },
1286 { "kqemu", "", do_info_kqemu,
1287 "", "show kqemu information", },
1288 { "usb", "", usb_info,
1289 "", "show guest USB devices", },
1290 { "usbhost", "", usb_host_info,
1291 "", "show host USB devices", },
1292 { "profile", "", do_info_profile,
1293 "", "show profiling information", },
1294 { "capture", "", do_info_capture,
1295 "", "show capture information" },
1296 { "snapshots", "", do_info_snapshots,
1297 "", "show the currently saved VM snapshots" },
1298 { "mice", "", do_info_mice,
1299 "", "show which guest mouse is receiving events" },
1303 /*******************************************************************/
1305 static const char *pch;
1306 static jmp_buf expr_env;
1311 typedef struct MonitorDef {
1314 target_long (*get_value)(struct MonitorDef *md, int val);
1318 #if defined(TARGET_I386)
1319 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1321 CPUState *env = mon_get_cpu();
1324 return env->eip + env->segs[R_CS].base;
1328 #if defined(TARGET_PPC)
1329 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1331 CPUState *env = mon_get_cpu();
1339 for (i = 0; i < 8; i++)
1340 u |= env->crf[i] << (32 - (4 * i));
1345 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1347 CPUState *env = mon_get_cpu();
1350 return (env->msr[MSR_POW] << MSR_POW) |
1351 (env->msr[MSR_ILE] << MSR_ILE) |
1352 (env->msr[MSR_EE] << MSR_EE) |
1353 (env->msr[MSR_PR] << MSR_PR) |
1354 (env->msr[MSR_FP] << MSR_FP) |
1355 (env->msr[MSR_ME] << MSR_ME) |
1356 (env->msr[MSR_FE0] << MSR_FE0) |
1357 (env->msr[MSR_SE] << MSR_SE) |
1358 (env->msr[MSR_BE] << MSR_BE) |
1359 (env->msr[MSR_FE1] << MSR_FE1) |
1360 (env->msr[MSR_IP] << MSR_IP) |
1361 (env->msr[MSR_IR] << MSR_IR) |
1362 (env->msr[MSR_DR] << MSR_DR) |
1363 (env->msr[MSR_RI] << MSR_RI) |
1364 (env->msr[MSR_LE] << MSR_LE);
1367 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1369 CPUState *env = mon_get_cpu();
1372 return (env->xer[XER_SO] << XER_SO) |
1373 (env->xer[XER_OV] << XER_OV) |
1374 (env->xer[XER_CA] << XER_CA) |
1375 (env->xer[XER_BC] << XER_BC);
1378 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1380 CPUState *env = mon_get_cpu();
1383 return cpu_ppc_load_decr(env);
1386 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1388 CPUState *env = mon_get_cpu();
1391 return cpu_ppc_load_tbu(env);
1394 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1396 CPUState *env = mon_get_cpu();
1399 return cpu_ppc_load_tbl(env);
1403 #if defined(TARGET_SPARC)
1404 #ifndef TARGET_SPARC64
1405 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1407 CPUState *env = mon_get_cpu();
1410 return GET_PSR(env);
1414 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1416 CPUState *env = mon_get_cpu();
1419 return env->regwptr[val];
1423 static MonitorDef monitor_defs[] = {
1426 #define SEG(name, seg) \
1427 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1428 { name ".base", offsetof(CPUState, segs[seg].base) },\
1429 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1431 { "eax", offsetof(CPUState, regs[0]) },
1432 { "ecx", offsetof(CPUState, regs[1]) },
1433 { "edx", offsetof(CPUState, regs[2]) },
1434 { "ebx", offsetof(CPUState, regs[3]) },
1435 { "esp|sp", offsetof(CPUState, regs[4]) },
1436 { "ebp|fp", offsetof(CPUState, regs[5]) },
1437 { "esi", offsetof(CPUState, regs[6]) },
1438 { "edi", offsetof(CPUState, regs[7]) },
1439 #ifdef TARGET_X86_64
1440 { "r8", offsetof(CPUState, regs[8]) },
1441 { "r9", offsetof(CPUState, regs[9]) },
1442 { "r10", offsetof(CPUState, regs[10]) },
1443 { "r11", offsetof(CPUState, regs[11]) },
1444 { "r12", offsetof(CPUState, regs[12]) },
1445 { "r13", offsetof(CPUState, regs[13]) },
1446 { "r14", offsetof(CPUState, regs[14]) },
1447 { "r15", offsetof(CPUState, regs[15]) },
1449 { "eflags", offsetof(CPUState, eflags) },
1450 { "eip", offsetof(CPUState, eip) },
1457 { "pc", 0, monitor_get_pc, },
1458 #elif defined(TARGET_PPC)
1459 { "r0", offsetof(CPUState, gpr[0]) },
1460 { "r1", offsetof(CPUState, gpr[1]) },
1461 { "r2", offsetof(CPUState, gpr[2]) },
1462 { "r3", offsetof(CPUState, gpr[3]) },
1463 { "r4", offsetof(CPUState, gpr[4]) },
1464 { "r5", offsetof(CPUState, gpr[5]) },
1465 { "r6", offsetof(CPUState, gpr[6]) },
1466 { "r7", offsetof(CPUState, gpr[7]) },
1467 { "r8", offsetof(CPUState, gpr[8]) },
1468 { "r9", offsetof(CPUState, gpr[9]) },
1469 { "r10", offsetof(CPUState, gpr[10]) },
1470 { "r11", offsetof(CPUState, gpr[11]) },
1471 { "r12", offsetof(CPUState, gpr[12]) },
1472 { "r13", offsetof(CPUState, gpr[13]) },
1473 { "r14", offsetof(CPUState, gpr[14]) },
1474 { "r15", offsetof(CPUState, gpr[15]) },
1475 { "r16", offsetof(CPUState, gpr[16]) },
1476 { "r17", offsetof(CPUState, gpr[17]) },
1477 { "r18", offsetof(CPUState, gpr[18]) },
1478 { "r19", offsetof(CPUState, gpr[19]) },
1479 { "r20", offsetof(CPUState, gpr[20]) },
1480 { "r21", offsetof(CPUState, gpr[21]) },
1481 { "r22", offsetof(CPUState, gpr[22]) },
1482 { "r23", offsetof(CPUState, gpr[23]) },
1483 { "r24", offsetof(CPUState, gpr[24]) },
1484 { "r25", offsetof(CPUState, gpr[25]) },
1485 { "r26", offsetof(CPUState, gpr[26]) },
1486 { "r27", offsetof(CPUState, gpr[27]) },
1487 { "r28", offsetof(CPUState, gpr[28]) },
1488 { "r29", offsetof(CPUState, gpr[29]) },
1489 { "r30", offsetof(CPUState, gpr[30]) },
1490 { "r31", offsetof(CPUState, gpr[31]) },
1491 { "nip|pc", offsetof(CPUState, nip) },
1492 { "lr", offsetof(CPUState, lr) },
1493 { "ctr", offsetof(CPUState, ctr) },
1494 { "decr", 0, &monitor_get_decr, },
1495 { "ccr", 0, &monitor_get_ccr, },
1496 { "msr", 0, &monitor_get_msr, },
1497 { "xer", 0, &monitor_get_xer, },
1498 { "tbu", 0, &monitor_get_tbu, },
1499 { "tbl", 0, &monitor_get_tbl, },
1500 { "sdr1", offsetof(CPUState, sdr1) },
1501 { "sr0", offsetof(CPUState, sr[0]) },
1502 { "sr1", offsetof(CPUState, sr[1]) },
1503 { "sr2", offsetof(CPUState, sr[2]) },
1504 { "sr3", offsetof(CPUState, sr[3]) },
1505 { "sr4", offsetof(CPUState, sr[4]) },
1506 { "sr5", offsetof(CPUState, sr[5]) },
1507 { "sr6", offsetof(CPUState, sr[6]) },
1508 { "sr7", offsetof(CPUState, sr[7]) },
1509 { "sr8", offsetof(CPUState, sr[8]) },
1510 { "sr9", offsetof(CPUState, sr[9]) },
1511 { "sr10", offsetof(CPUState, sr[10]) },
1512 { "sr11", offsetof(CPUState, sr[11]) },
1513 { "sr12", offsetof(CPUState, sr[12]) },
1514 { "sr13", offsetof(CPUState, sr[13]) },
1515 { "sr14", offsetof(CPUState, sr[14]) },
1516 { "sr15", offsetof(CPUState, sr[15]) },
1517 /* Too lazy to put BATs and SPRs ... */
1518 #elif defined(TARGET_SPARC)
1519 { "g0", offsetof(CPUState, gregs[0]) },
1520 { "g1", offsetof(CPUState, gregs[1]) },
1521 { "g2", offsetof(CPUState, gregs[2]) },
1522 { "g3", offsetof(CPUState, gregs[3]) },
1523 { "g4", offsetof(CPUState, gregs[4]) },
1524 { "g5", offsetof(CPUState, gregs[5]) },
1525 { "g6", offsetof(CPUState, gregs[6]) },
1526 { "g7", offsetof(CPUState, gregs[7]) },
1527 { "o0", 0, monitor_get_reg },
1528 { "o1", 1, monitor_get_reg },
1529 { "o2", 2, monitor_get_reg },
1530 { "o3", 3, monitor_get_reg },
1531 { "o4", 4, monitor_get_reg },
1532 { "o5", 5, monitor_get_reg },
1533 { "o6", 6, monitor_get_reg },
1534 { "o7", 7, monitor_get_reg },
1535 { "l0", 8, monitor_get_reg },
1536 { "l1", 9, monitor_get_reg },
1537 { "l2", 10, monitor_get_reg },
1538 { "l3", 11, monitor_get_reg },
1539 { "l4", 12, monitor_get_reg },
1540 { "l5", 13, monitor_get_reg },
1541 { "l6", 14, monitor_get_reg },
1542 { "l7", 15, monitor_get_reg },
1543 { "i0", 16, monitor_get_reg },
1544 { "i1", 17, monitor_get_reg },
1545 { "i2", 18, monitor_get_reg },
1546 { "i3", 19, monitor_get_reg },
1547 { "i4", 20, monitor_get_reg },
1548 { "i5", 21, monitor_get_reg },
1549 { "i6", 22, monitor_get_reg },
1550 { "i7", 23, monitor_get_reg },
1551 { "pc", offsetof(CPUState, pc) },
1552 { "npc", offsetof(CPUState, npc) },
1553 { "y", offsetof(CPUState, y) },
1554 #ifndef TARGET_SPARC64
1555 { "psr", 0, &monitor_get_psr, },
1556 { "wim", offsetof(CPUState, wim) },
1558 { "tbr", offsetof(CPUState, tbr) },
1559 { "fsr", offsetof(CPUState, fsr) },
1560 { "f0", offsetof(CPUState, fpr[0]) },
1561 { "f1", offsetof(CPUState, fpr[1]) },
1562 { "f2", offsetof(CPUState, fpr[2]) },
1563 { "f3", offsetof(CPUState, fpr[3]) },
1564 { "f4", offsetof(CPUState, fpr[4]) },
1565 { "f5", offsetof(CPUState, fpr[5]) },
1566 { "f6", offsetof(CPUState, fpr[6]) },
1567 { "f7", offsetof(CPUState, fpr[7]) },
1568 { "f8", offsetof(CPUState, fpr[8]) },
1569 { "f9", offsetof(CPUState, fpr[9]) },
1570 { "f10", offsetof(CPUState, fpr[10]) },
1571 { "f11", offsetof(CPUState, fpr[11]) },
1572 { "f12", offsetof(CPUState, fpr[12]) },
1573 { "f13", offsetof(CPUState, fpr[13]) },
1574 { "f14", offsetof(CPUState, fpr[14]) },
1575 { "f15", offsetof(CPUState, fpr[15]) },
1576 { "f16", offsetof(CPUState, fpr[16]) },
1577 { "f17", offsetof(CPUState, fpr[17]) },
1578 { "f18", offsetof(CPUState, fpr[18]) },
1579 { "f19", offsetof(CPUState, fpr[19]) },
1580 { "f20", offsetof(CPUState, fpr[20]) },
1581 { "f21", offsetof(CPUState, fpr[21]) },
1582 { "f22", offsetof(CPUState, fpr[22]) },
1583 { "f23", offsetof(CPUState, fpr[23]) },
1584 { "f24", offsetof(CPUState, fpr[24]) },
1585 { "f25", offsetof(CPUState, fpr[25]) },
1586 { "f26", offsetof(CPUState, fpr[26]) },
1587 { "f27", offsetof(CPUState, fpr[27]) },
1588 { "f28", offsetof(CPUState, fpr[28]) },
1589 { "f29", offsetof(CPUState, fpr[29]) },
1590 { "f30", offsetof(CPUState, fpr[30]) },
1591 { "f31", offsetof(CPUState, fpr[31]) },
1592 #ifdef TARGET_SPARC64
1593 { "f32", offsetof(CPUState, fpr[32]) },
1594 { "f34", offsetof(CPUState, fpr[34]) },
1595 { "f36", offsetof(CPUState, fpr[36]) },
1596 { "f38", offsetof(CPUState, fpr[38]) },
1597 { "f40", offsetof(CPUState, fpr[40]) },
1598 { "f42", offsetof(CPUState, fpr[42]) },
1599 { "f44", offsetof(CPUState, fpr[44]) },
1600 { "f46", offsetof(CPUState, fpr[46]) },
1601 { "f48", offsetof(CPUState, fpr[48]) },
1602 { "f50", offsetof(CPUState, fpr[50]) },
1603 { "f52", offsetof(CPUState, fpr[52]) },
1604 { "f54", offsetof(CPUState, fpr[54]) },
1605 { "f56", offsetof(CPUState, fpr[56]) },
1606 { "f58", offsetof(CPUState, fpr[58]) },
1607 { "f60", offsetof(CPUState, fpr[60]) },
1608 { "f62", offsetof(CPUState, fpr[62]) },
1609 { "asi", offsetof(CPUState, asi) },
1610 { "pstate", offsetof(CPUState, pstate) },
1611 { "cansave", offsetof(CPUState, cansave) },
1612 { "canrestore", offsetof(CPUState, canrestore) },
1613 { "otherwin", offsetof(CPUState, otherwin) },
1614 { "wstate", offsetof(CPUState, wstate) },
1615 { "cleanwin", offsetof(CPUState, cleanwin) },
1616 { "fprs", offsetof(CPUState, fprs) },
1622 static void expr_error(const char *fmt)
1626 longjmp(expr_env, 1);
1629 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1630 static int get_monitor_def(target_long *pval, const char *name)
1635 for(md = monitor_defs; md->name != NULL; md++) {
1636 if (compare_cmd(name, md->name)) {
1637 if (md->get_value) {
1638 *pval = md->get_value(md, md->offset);
1640 CPUState *env = mon_get_cpu();
1643 ptr = (uint8_t *)env + md->offset;
1646 *pval = *(int32_t *)ptr;
1649 *pval = *(target_long *)ptr;
1662 static void next(void)
1666 while (isspace(*pch))
1671 static target_long expr_sum(void);
1673 static target_long expr_unary(void)
1696 expr_error("')' expected");
1703 expr_error("character constant expected");
1707 expr_error("missing terminating \' character");
1716 while ((*pch >= 'a' && *pch <= 'z') ||
1717 (*pch >= 'A' && *pch <= 'Z') ||
1718 (*pch >= '0' && *pch <= '9') ||
1719 *pch == '_' || *pch == '.') {
1720 if ((q - buf) < sizeof(buf) - 1)
1724 while (isspace(*pch))
1727 ret = get_monitor_def(&n, buf);
1729 expr_error("unknown register");
1731 expr_error("no cpu defined");
1735 expr_error("unexpected end of expression");
1739 #if TARGET_LONG_BITS == 64
1740 n = strtoull(pch, &p, 0);
1742 n = strtoul(pch, &p, 0);
1745 expr_error("invalid char in expression");
1748 while (isspace(*pch))
1756 static target_long expr_prod(void)
1758 target_long val, val2;
1764 if (op != '*' && op != '/' && op != '%')
1767 val2 = expr_unary();
1776 expr_error("division by zero");
1787 static target_long expr_logic(void)
1789 target_long val, val2;
1795 if (op != '&' && op != '|' && op != '^')
1815 static target_long expr_sum(void)
1817 target_long val, val2;
1823 if (op != '+' && op != '-')
1826 val2 = expr_logic();
1835 static int get_expr(target_long *pval, const char **pp)
1838 if (setjmp(expr_env)) {
1842 while (isspace(*pch))
1849 static int get_str(char *buf, int buf_size, const char **pp)
1867 while (*p != '\0' && *p != '\"') {
1883 qemu_printf("unsupported escape code: '\\%c'\n", c);
1886 if ((q - buf) < buf_size - 1) {
1890 if ((q - buf) < buf_size - 1) {
1897 qemu_printf("unterminated string\n");
1902 while (*p != '\0' && !isspace(*p)) {
1903 if ((q - buf) < buf_size - 1) {
1914 static int default_fmt_format = 'x';
1915 static int default_fmt_size = 4;
1919 static void monitor_handle_command(const char *cmdline)
1921 const char *p, *pstart, *typestr;
1923 int c, nb_args, len, i, has_arg;
1927 void *str_allocated[MAX_ARGS];
1928 void *args[MAX_ARGS];
1931 term_printf("command='%s'\n", cmdline);
1934 /* extract the command name */
1942 while (*p != '\0' && *p != '/' && !isspace(*p))
1945 if (len > sizeof(cmdname) - 1)
1946 len = sizeof(cmdname) - 1;
1947 memcpy(cmdname, pstart, len);
1948 cmdname[len] = '\0';
1950 /* find the command */
1951 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1952 if (compare_cmd(cmdname, cmd->name))
1955 term_printf("unknown command: '%s'\n", cmdname);
1959 for(i = 0; i < MAX_ARGS; i++)
1960 str_allocated[i] = NULL;
1962 /* parse the parameters */
1963 typestr = cmd->args_type;
1980 if (*typestr == '?') {
1983 /* no optional string: NULL argument */
1988 ret = get_str(buf, sizeof(buf), &p);
1992 term_printf("%s: filename expected\n", cmdname);
1995 term_printf("%s: block device name expected\n", cmdname);
1998 term_printf("%s: string expected\n", cmdname);
2003 str = qemu_malloc(strlen(buf) + 1);
2005 str_allocated[nb_args] = str;
2007 if (nb_args >= MAX_ARGS) {
2009 term_printf("%s: too many arguments\n", cmdname);
2012 args[nb_args++] = str;
2017 int count, format, size;
2027 while (isdigit(*p)) {
2028 count = count * 10 + (*p - '0');
2066 if (*p != '\0' && !isspace(*p)) {
2067 term_printf("invalid char in format: '%c'\n", *p);
2071 format = default_fmt_format;
2072 if (format != 'i') {
2073 /* for 'i', not specifying a size gives -1 as size */
2075 size = default_fmt_size;
2077 default_fmt_size = size;
2078 default_fmt_format = format;
2081 format = default_fmt_format;
2082 if (format != 'i') {
2083 size = default_fmt_size;
2088 if (nb_args + 3 > MAX_ARGS)
2090 args[nb_args++] = (void*)count;
2091 args[nb_args++] = (void*)format;
2092 args[nb_args++] = (void*)size;
2101 if (*typestr == '?' || *typestr == '.') {
2102 if (*typestr == '?') {
2118 if (nb_args >= MAX_ARGS)
2120 args[nb_args++] = (void *)has_arg;
2122 if (nb_args >= MAX_ARGS)
2128 if (get_expr(&val, &p))
2132 if (nb_args >= MAX_ARGS)
2134 args[nb_args++] = (void *)(int)val;
2136 if ((nb_args + 1) >= MAX_ARGS)
2138 #if TARGET_LONG_BITS == 64
2139 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2141 args[nb_args++] = (void *)0;
2143 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2161 term_printf("%s: unsupported option -%c\n",
2168 if (nb_args >= MAX_ARGS)
2170 args[nb_args++] = (void *)has_option;
2175 term_printf("%s: unknown type '%c'\n", cmdname, c);
2179 /* check that all arguments were parsed */
2183 term_printf("%s: extraneous characters at the end of line\n",
2193 cmd->handler(args[0]);
2196 cmd->handler(args[0], args[1]);
2199 cmd->handler(args[0], args[1], args[2]);
2202 cmd->handler(args[0], args[1], args[2], args[3]);
2205 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2208 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2211 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2214 term_printf("unsupported number of arguments: %d\n", nb_args);
2218 for(i = 0; i < MAX_ARGS; i++)
2219 qemu_free(str_allocated[i]);
2223 static void cmd_completion(const char *name, const char *list)
2225 const char *p, *pstart;
2234 p = pstart + strlen(pstart);
2236 if (len > sizeof(cmd) - 2)
2237 len = sizeof(cmd) - 2;
2238 memcpy(cmd, pstart, len);
2240 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2241 add_completion(cmd);
2249 static void file_completion(const char *input)
2254 char file[1024], file_prefix[1024];
2258 p = strrchr(input, '/');
2261 pstrcpy(file_prefix, sizeof(file_prefix), input);
2264 input_path_len = p - input + 1;
2265 memcpy(path, input, input_path_len);
2266 if (input_path_len > sizeof(path) - 1)
2267 input_path_len = sizeof(path) - 1;
2268 path[input_path_len] = '\0';
2269 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2271 #ifdef DEBUG_COMPLETION
2272 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2274 ffs = opendir(path);
2282 if (strstart(d->d_name, file_prefix, NULL)) {
2283 memcpy(file, input, input_path_len);
2284 strcpy(file + input_path_len, d->d_name);
2285 /* stat the file to find out if it's a directory.
2286 * In that case add a slash to speed up typing long paths
2289 if(S_ISDIR(sb.st_mode))
2291 add_completion(file);
2297 static void block_completion_it(void *opaque, const char *name)
2299 const char *input = opaque;
2301 if (input[0] == '\0' ||
2302 !strncmp(name, (char *)input, strlen(input))) {
2303 add_completion(name);
2307 /* NOTE: this parser is an approximate form of the real command parser */
2308 static void parse_cmdline(const char *cmdline,
2309 int *pnb_args, char **args)
2322 if (nb_args >= MAX_ARGS)
2324 ret = get_str(buf, sizeof(buf), &p);
2325 args[nb_args] = qemu_strdup(buf);
2330 *pnb_args = nb_args;
2333 void readline_find_completion(const char *cmdline)
2335 const char *cmdname;
2336 char *args[MAX_ARGS];
2337 int nb_args, i, len;
2338 const char *ptype, *str;
2342 parse_cmdline(cmdline, &nb_args, args);
2343 #ifdef DEBUG_COMPLETION
2344 for(i = 0; i < nb_args; i++) {
2345 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2349 /* if the line ends with a space, it means we want to complete the
2351 len = strlen(cmdline);
2352 if (len > 0 && isspace(cmdline[len - 1])) {
2353 if (nb_args >= MAX_ARGS)
2355 args[nb_args++] = qemu_strdup("");
2358 /* command completion */
2363 completion_index = strlen(cmdname);
2364 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2365 cmd_completion(cmdname, cmd->name);
2368 /* find the command */
2369 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2370 if (compare_cmd(args[0], cmd->name))
2375 ptype = cmd->args_type;
2376 for(i = 0; i < nb_args - 2; i++) {
2377 if (*ptype != '\0') {
2379 while (*ptype == '?')
2383 str = args[nb_args - 1];
2386 /* file completion */
2387 completion_index = strlen(str);
2388 file_completion(str);
2391 /* block device name completion */
2392 completion_index = strlen(str);
2393 bdrv_iterate(block_completion_it, (void *)str);
2396 /* XXX: more generic ? */
2397 if (!strcmp(cmd->name, "info")) {
2398 completion_index = strlen(str);
2399 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2400 cmd_completion(str, cmd->name);
2402 } else if (!strcmp(cmd->name, "sendkey")) {
2403 completion_index = strlen(str);
2404 for(key = key_defs; key->name != NULL; key++) {
2405 cmd_completion(str, key->name);
2413 for(i = 0; i < nb_args; i++)
2417 static int term_can_read(void *opaque)
2422 static void term_read(void *opaque, const uint8_t *buf, int size)
2425 for(i = 0; i < size; i++)
2426 readline_handle_byte(buf[i]);
2429 static void monitor_start_input(void);
2431 static void monitor_handle_command1(void *opaque, const char *cmdline)
2433 monitor_handle_command(cmdline);
2434 monitor_start_input();
2437 static void monitor_start_input(void)
2439 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2442 static void term_event(void *opaque, int event)
2444 if (event != CHR_EVENT_RESET)
2448 term_printf("QEMU %s monitor - type 'help' for more information\n",
2450 monitor_start_input();
2453 void monitor_init(CharDriverState *hd, int show_banner)
2456 hide_banner = !show_banner;
2458 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2461 /* XXX: use threads ? */
2462 /* modal monitor readline */
2463 static int monitor_readline_started;
2464 static char *monitor_readline_buf;
2465 static int monitor_readline_buf_size;
2467 static void monitor_readline_cb(void *opaque, const char *input)
2469 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2470 monitor_readline_started = 0;
2473 void monitor_readline(const char *prompt, int is_password,
2474 char *buf, int buf_size)
2477 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2479 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2480 monitor_readline_buf = buf;
2481 monitor_readline_buf_size = buf_size;
2482 monitor_readline_started = 1;
2483 while (monitor_readline_started) {