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;
58 static CharDriverState *monitor_hd[MAX_MON];
59 static int hide_banner;
61 static term_cmd_t term_cmds[];
62 static term_cmd_t info_cmds[];
64 static char term_outbuf[1024];
65 static int term_outbuf_index;
67 static void monitor_start_input(void);
69 CPUState *mon_cpu = NULL;
74 if (term_outbuf_index > 0) {
75 for (i = 0; i < MAX_MON; i++)
76 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
77 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
78 term_outbuf_index = 0;
82 /* flush at every end of line or if the buffer is full */
83 void term_puts(const char *str)
91 term_outbuf[term_outbuf_index++] = '\r';
92 term_outbuf[term_outbuf_index++] = c;
93 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
99 void term_vprintf(const char *fmt, va_list ap)
102 vsnprintf(buf, sizeof(buf), fmt, ap);
106 void term_printf(const char *fmt, ...)
110 term_vprintf(fmt, ap);
114 void term_print_filename(const char *filename)
118 for (i = 0; filename[i]; i++) {
119 switch (filename[i]) {
123 term_printf("\\%c", filename[i]);
135 term_printf("%c", filename[i]);
141 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
145 term_vprintf(fmt, ap);
150 static int compare_cmd(const char *name, const char *list)
152 const char *p, *pstart;
160 p = pstart + strlen(pstart);
161 if ((p - pstart) == len && !memcmp(pstart, name, len))
170 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
174 for(cmd = cmds; cmd->name != NULL; cmd++) {
175 if (!name || !strcmp(name, cmd->name))
176 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
180 static void help_cmd(const char *name)
182 if (name && !strcmp(name, "info")) {
183 help_cmd1(info_cmds, "info ", NULL);
185 help_cmd1(term_cmds, "", name);
186 if (name && !strcmp(name, "log")) {
188 term_printf("Log items (comma separated):\n");
189 term_printf("%-10s %s\n", "none", "remove all logs");
190 for(item = cpu_log_items; item->mask != 0; item++) {
191 term_printf("%-10s %s\n", item->name, item->help);
197 static void do_help(const char *name)
202 static void do_commit(const char *device)
206 all_devices = !strcmp(device, "all");
207 for (i = 0; i < MAX_DISKS; i++) {
210 !strcmp(bdrv_get_device_name(bs_table[i]), device))
211 bdrv_commit(bs_table[i]);
216 static void do_info(const char *item)
222 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
223 if (compare_cmd(item, cmd->name))
233 static void do_info_version(void)
235 term_printf("%s\n", QEMU_VERSION);
238 static void do_info_block(void)
243 /* get the current CPU defined by the user */
244 int mon_set_cpu(int cpu_index)
248 for(env = first_cpu; env != NULL; env = env->next_cpu) {
249 if (env->cpu_index == cpu_index) {
257 CPUState *mon_get_cpu(void)
265 static void do_info_registers(void)
272 cpu_dump_state(env, NULL, monitor_fprintf,
275 cpu_dump_state(env, NULL, monitor_fprintf,
280 static void do_info_cpus(void)
284 /* just to set the default cpu if not already done */
287 for(env = first_cpu; env != NULL; env = env->next_cpu) {
288 term_printf("%c CPU #%d:",
289 (env == mon_cpu) ? '*' : ' ',
291 #if defined(TARGET_I386)
292 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
293 if (env->hflags & HF_HALTED_MASK)
294 term_printf(" (halted)");
295 #elif defined(TARGET_PPC)
296 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
298 term_printf(" (halted)");
299 #elif defined(TARGET_SPARC)
300 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
302 term_printf(" (halted)");
308 static void do_cpu_set(int index)
310 if (mon_set_cpu(index) < 0)
311 term_printf("Invalid CPU index\n");
314 static void do_info_jit(void)
316 dump_exec_info(NULL, monitor_fprintf);
319 static void do_info_history (void)
326 str = readline_get_history(i);
329 term_printf("%d: '%s'\n", i, str);
334 static void do_quit(void)
339 static int eject_device(BlockDriverState *bs, int force)
341 if (bdrv_is_inserted(bs)) {
343 if (!bdrv_is_removable(bs)) {
344 term_printf("device is not removable\n");
347 if (bdrv_is_locked(bs)) {
348 term_printf("device is locked\n");
357 static void do_eject(int force, const char *filename)
359 BlockDriverState *bs;
361 bs = bdrv_find(filename);
363 term_printf("device not found\n");
366 eject_device(bs, force);
369 static void do_change(const char *device, const char *filename)
371 BlockDriverState *bs;
375 bs = bdrv_find(device);
377 term_printf("device not found\n");
380 if (eject_device(bs, 0) < 0)
382 bdrv_open(bs, filename, 0);
383 if (bdrv_is_encrypted(bs)) {
384 term_printf("%s is encrypted.\n", device);
385 for(i = 0; i < 3; i++) {
386 monitor_readline("Password: ", 1, password, sizeof(password));
387 if (bdrv_set_key(bs, password) == 0)
389 term_printf("invalid password\n");
394 static void do_screen_dump(const char *filename)
396 vga_hw_screen_dump(filename);
399 static void do_log(const char *items)
403 if (!strcmp(items, "none")) {
406 mask = cpu_str_to_log_mask(items);
415 static void do_stop(void)
417 vm_stop(EXCP_INTERRUPT);
420 static void do_cont(void)
425 #ifdef CONFIG_GDBSTUB
426 static void do_gdbserver(int has_port, int port)
429 port = DEFAULT_GDBSTUB_PORT;
430 if (gdbserver_start_port(port) < 0) {
431 qemu_printf("Could not open gdbserver socket on port %d\n", port);
433 qemu_printf("Waiting gdb connection on port %d\n", port);
438 static void term_printc(int c)
455 if (c >= 32 && c <= 126) {
456 term_printf("%c", c);
458 term_printf("\\x%02x", c);
465 static void memory_dump(int count, int format, int wsize,
466 target_ulong addr, int is_physical)
469 int nb_per_line, l, line_size, i, max_digits, len;
477 if (!env && !is_physical)
482 } else if (wsize == 4) {
485 /* as default we use the current CS size */
489 if ((env->efer & MSR_EFER_LMA) &&
490 (env->segs[R_CS].flags & DESC_L_MASK))
494 if (!(env->segs[R_CS].flags & DESC_B_MASK))
499 monitor_disas(env, addr, count, is_physical, flags);
508 nb_per_line = line_size / wsize;
513 max_digits = (wsize * 8 + 2) / 3;
517 max_digits = (wsize * 8) / 4;
521 max_digits = (wsize * 8 * 10 + 32) / 33;
529 term_printf(TARGET_FMT_lx ":", addr);
534 cpu_physical_memory_rw(addr, buf, l, 0);
539 cpu_memory_rw_debug(env, addr, buf, l, 0);
546 v = ldub_raw(buf + i);
549 v = lduw_raw(buf + i);
552 v = (uint32_t)ldl_raw(buf + i);
555 v = ldq_raw(buf + i);
561 term_printf("%#*" PRIo64, max_digits, v);
564 term_printf("0x%0*" PRIx64, max_digits, v);
567 term_printf("%*" PRIu64, max_digits, v);
570 term_printf("%*" PRId64, max_digits, v);
584 #if TARGET_LONG_BITS == 64
585 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
587 #define GET_TLONG(h, l) (l)
590 static void do_memory_dump(int count, int format, int size,
591 uint32_t addrh, uint32_t addrl)
593 target_long addr = GET_TLONG(addrh, addrl);
594 memory_dump(count, format, size, addr, 0);
597 static void do_physical_memory_dump(int count, int format, int size,
598 uint32_t addrh, uint32_t addrl)
601 target_long addr = GET_TLONG(addrh, addrl);
602 memory_dump(count, format, size, addr, 1);
605 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
607 target_long val = GET_TLONG(valh, vall);
608 #if TARGET_LONG_BITS == 32
611 term_printf("%#o", val);
614 term_printf("%#x", val);
617 term_printf("%u", val);
621 term_printf("%d", val);
630 term_printf("%#" PRIo64, val);
633 term_printf("%#" PRIx64, val);
636 term_printf("%" PRIu64, val);
640 term_printf("%" PRId64, val);
650 static void do_memory_save(unsigned int valh, unsigned int vall,
651 uint32_t size, const char *filename)
654 target_long addr = GET_TLONG(valh, vall);
663 f = fopen(filename, "wb");
665 term_printf("could not open '%s'\n", filename);
672 cpu_memory_rw_debug(env, addr, buf, l, 0);
673 fwrite(buf, 1, l, f);
680 static void do_sum(uint32_t start, uint32_t size)
687 for(addr = start; addr < (start + size); addr++) {
688 cpu_physical_memory_rw(addr, buf, 1, 0);
689 /* BSD sum algorithm ('sum' Unix command) */
690 sum = (sum >> 1) | (sum << 15);
693 term_printf("%05d\n", sum);
701 static const KeyDef key_defs[] = {
726 { 0x0e, "backspace" },
761 { 0x3a, "caps_lock" },
772 { 0x45, "num_lock" },
773 { 0x46, "scroll_lock" },
775 { 0xb5, "kp_divide" },
776 { 0x37, "kp_multiply" },
777 { 0x4a, "kp_substract" },
779 { 0x9c, "kp_enter" },
780 { 0x53, "kp_decimal" },
815 static int get_keycode(const char *key)
821 for(p = key_defs; p->name != NULL; p++) {
822 if (!strcmp(key, p->name))
825 if (strstart(key, "0x", NULL)) {
826 ret = strtoul(key, &endp, 0);
827 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
833 static void do_send_key(const char *string)
836 uint8_t keycodes[16];
838 int nb_keycodes, keycode, i;
844 while (*p != '\0' && *p != '-') {
845 if ((q - keybuf) < sizeof(keybuf) - 1) {
851 keycode = get_keycode(keybuf);
853 term_printf("unknown key: '%s'\n", keybuf);
856 keycodes[nb_keycodes++] = keycode;
861 /* key down events */
862 for(i = 0; i < nb_keycodes; i++) {
863 keycode = keycodes[i];
865 kbd_put_keycode(0xe0);
866 kbd_put_keycode(keycode & 0x7f);
869 for(i = nb_keycodes - 1; i >= 0; i--) {
870 keycode = keycodes[i];
872 kbd_put_keycode(0xe0);
873 kbd_put_keycode(keycode | 0x80);
877 static int mouse_button_state;
879 static void do_mouse_move(const char *dx_str, const char *dy_str,
883 dx = strtol(dx_str, NULL, 0);
884 dy = strtol(dy_str, NULL, 0);
887 dz = strtol(dz_str, NULL, 0);
888 kbd_mouse_event(dx, dy, dz, mouse_button_state);
891 static void do_mouse_button(int button_state)
893 mouse_button_state = button_state;
894 kbd_mouse_event(0, 0, 0, mouse_button_state);
897 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
903 cpu_outb(NULL, addr & 0xffff, index & 0xff);
911 val = cpu_inb(NULL, addr);
915 val = cpu_inw(NULL, addr);
919 val = cpu_inl(NULL, addr);
923 term_printf("port%c[0x%04x] = %#0*x\n",
924 suffix, addr, size * 2, val);
927 static void do_system_reset(void)
929 qemu_system_reset_request();
932 static void do_system_powerdown(void)
934 qemu_system_powerdown_request();
937 #if defined(TARGET_I386)
938 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
940 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
943 pte & PG_GLOBAL_MASK ? 'G' : '-',
944 pte & PG_PSE_MASK ? 'P' : '-',
945 pte & PG_DIRTY_MASK ? 'D' : '-',
946 pte & PG_ACCESSED_MASK ? 'A' : '-',
947 pte & PG_PCD_MASK ? 'C' : '-',
948 pte & PG_PWT_MASK ? 'T' : '-',
949 pte & PG_USER_MASK ? 'U' : '-',
950 pte & PG_RW_MASK ? 'W' : '-');
953 static void tlb_info(void)
957 uint32_t pgd, pde, pte;
963 if (!(env->cr[0] & CR0_PG_MASK)) {
964 term_printf("PG disabled\n");
967 pgd = env->cr[3] & ~0xfff;
968 for(l1 = 0; l1 < 1024; l1++) {
969 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
970 pde = le32_to_cpu(pde);
971 if (pde & PG_PRESENT_MASK) {
972 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
973 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
975 for(l2 = 0; l2 < 1024; l2++) {
976 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
978 pte = le32_to_cpu(pte);
979 if (pte & PG_PRESENT_MASK) {
980 print_pte((l1 << 22) + (l2 << 12),
990 static void mem_print(uint32_t *pstart, int *plast_prot,
991 uint32_t end, int prot)
997 term_printf("%08x-%08x %08x %c%c%c\n",
998 *pstart, end, end - *pstart,
999 prot1 & PG_USER_MASK ? 'u' : '-',
1001 prot1 & PG_RW_MASK ? 'w' : '-');
1011 static void mem_info(void)
1014 int l1, l2, prot, last_prot;
1015 uint32_t pgd, pde, pte, start, end;
1017 env = mon_get_cpu();
1021 if (!(env->cr[0] & CR0_PG_MASK)) {
1022 term_printf("PG disabled\n");
1025 pgd = env->cr[3] & ~0xfff;
1028 for(l1 = 0; l1 < 1024; l1++) {
1029 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1030 pde = le32_to_cpu(pde);
1032 if (pde & PG_PRESENT_MASK) {
1033 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1034 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1035 mem_print(&start, &last_prot, end, prot);
1037 for(l2 = 0; l2 < 1024; l2++) {
1038 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1039 (uint8_t *)&pte, 4);
1040 pte = le32_to_cpu(pte);
1041 end = (l1 << 22) + (l2 << 12);
1042 if (pte & PG_PRESENT_MASK) {
1043 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1047 mem_print(&start, &last_prot, end, prot);
1052 mem_print(&start, &last_prot, end, prot);
1058 static void do_info_kqemu(void)
1064 env = mon_get_cpu();
1066 term_printf("No cpu initialized yet");
1069 val = env->kqemu_enabled;
1070 term_printf("kqemu support: ");
1074 term_printf("disabled\n");
1077 term_printf("enabled for user code\n");
1080 term_printf("enabled for user and kernel code\n");
1084 term_printf("kqemu support: not compiled\n");
1088 #ifdef CONFIG_PROFILER
1092 int64_t kqemu_exec_count;
1094 int64_t kqemu_ret_int_count;
1095 int64_t kqemu_ret_excp_count;
1096 int64_t kqemu_ret_intr_count;
1098 static void do_info_profile(void)
1104 term_printf("async time %" PRId64 " (%0.3f)\n",
1105 dev_time, dev_time / (double)ticks_per_sec);
1106 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1107 qemu_time, qemu_time / (double)ticks_per_sec);
1108 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1109 kqemu_time, kqemu_time / (double)ticks_per_sec,
1110 kqemu_time / (double)total * 100.0,
1112 kqemu_ret_int_count,
1113 kqemu_ret_excp_count,
1114 kqemu_ret_intr_count);
1117 kqemu_exec_count = 0;
1119 kqemu_ret_int_count = 0;
1120 kqemu_ret_excp_count = 0;
1121 kqemu_ret_intr_count = 0;
1123 kqemu_record_dump();
1127 static void do_info_profile(void)
1129 term_printf("Internal profiler not compiled\n");
1133 /* Capture support */
1134 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1136 static void do_info_capture (void)
1141 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1142 term_printf ("[%d]: ", i);
1143 s->ops.info (s->opaque);
1147 static void do_stop_capture (int n)
1152 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1154 s->ops.destroy (s->opaque);
1155 LIST_REMOVE (s, entries);
1163 int wav_start_capture (CaptureState *s, const char *path, int freq,
1164 int bits, int nchannels);
1166 static void do_wav_capture (const char *path,
1167 int has_freq, int freq,
1168 int has_bits, int bits,
1169 int has_channels, int nchannels)
1173 s = qemu_mallocz (sizeof (*s));
1175 term_printf ("Not enough memory to add wave capture\n");
1179 freq = has_freq ? freq : 44100;
1180 bits = has_bits ? bits : 16;
1181 nchannels = has_channels ? nchannels : 2;
1183 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1184 term_printf ("Faied to add wave capture\n");
1187 LIST_INSERT_HEAD (&capture_head, s, entries);
1191 static term_cmd_t term_cmds[] = {
1192 { "help|?", "s?", do_help,
1193 "[cmd]", "show the help" },
1194 { "commit", "s", do_commit,
1195 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1196 { "info", "s?", do_info,
1197 "subcommand", "show various information about the system state" },
1198 { "q|quit", "", do_quit,
1199 "", "quit the emulator" },
1200 { "eject", "-fB", do_eject,
1201 "[-f] device", "eject a removable media (use -f to force it)" },
1202 { "change", "BF", do_change,
1203 "device filename", "change a removable media" },
1204 { "screendump", "F", do_screen_dump,
1205 "filename", "save screen into PPM image 'filename'" },
1206 { "log", "s", do_log,
1207 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1208 { "savevm", "s?", do_savevm,
1209 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1210 { "loadvm", "s", do_loadvm,
1211 "tag|id", "restore a VM snapshot from its tag or id" },
1212 { "delvm", "s", do_delvm,
1213 "tag|id", "delete a VM snapshot from its tag or id" },
1214 { "stop", "", do_stop,
1215 "", "stop emulation", },
1216 { "c|cont", "", do_cont,
1217 "", "resume emulation", },
1218 #ifdef CONFIG_GDBSTUB
1219 { "gdbserver", "i?", do_gdbserver,
1220 "[port]", "start gdbserver session (default port=1234)", },
1222 { "x", "/l", do_memory_dump,
1223 "/fmt addr", "virtual memory dump starting at 'addr'", },
1224 { "xp", "/l", do_physical_memory_dump,
1225 "/fmt addr", "physical memory dump starting at 'addr'", },
1226 { "p|print", "/l", do_print,
1227 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1228 { "i", "/ii.", do_ioport_read,
1229 "/fmt addr", "I/O port read" },
1231 { "sendkey", "s", do_send_key,
1232 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1233 { "system_reset", "", do_system_reset,
1234 "", "reset the system" },
1235 { "system_powerdown", "", do_system_powerdown,
1236 "", "send system power down event" },
1237 { "sum", "ii", do_sum,
1238 "addr size", "compute the checksum of a memory region" },
1239 { "usb_add", "s", do_usb_add,
1240 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1241 { "usb_del", "s", do_usb_del,
1242 "device", "remove USB device 'bus.addr'" },
1243 { "cpu", "i", do_cpu_set,
1244 "index", "set the default CPU" },
1245 { "mouse_move", "sss?", do_mouse_move,
1246 "dx dy [dz]", "send mouse move events" },
1247 { "mouse_button", "i", do_mouse_button,
1248 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1249 { "mouse_set", "i", do_mouse_set,
1250 "index", "set which mouse device receives events" },
1252 { "wavcapture", "si?i?i?", do_wav_capture,
1253 "path [frequency bits channels]",
1254 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1256 { "stopcapture", "i", do_stop_capture,
1257 "capture index", "stop capture" },
1258 { "memsave", "lis", do_memory_save,
1259 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1263 static term_cmd_t info_cmds[] = {
1264 { "version", "", do_info_version,
1265 "", "show the version of qemu" },
1266 { "network", "", do_info_network,
1267 "", "show the network state" },
1268 { "block", "", do_info_block,
1269 "", "show the block devices" },
1270 { "registers", "", do_info_registers,
1271 "", "show the cpu registers" },
1272 { "cpus", "", do_info_cpus,
1273 "", "show infos for each CPU" },
1274 { "history", "", do_info_history,
1275 "", "show the command line history", },
1276 { "irq", "", irq_info,
1277 "", "show the interrupts statistics (if available)", },
1278 { "pic", "", pic_info,
1279 "", "show i8259 (PIC) state", },
1280 { "pci", "", pci_info,
1281 "", "show PCI info", },
1282 #if defined(TARGET_I386)
1283 { "tlb", "", tlb_info,
1284 "", "show virtual to physical memory mappings", },
1285 { "mem", "", mem_info,
1286 "", "show the active virtual memory mappings", },
1288 { "jit", "", do_info_jit,
1289 "", "show dynamic compiler info", },
1290 { "kqemu", "", do_info_kqemu,
1291 "", "show kqemu information", },
1292 { "usb", "", usb_info,
1293 "", "show guest USB devices", },
1294 { "usbhost", "", usb_host_info,
1295 "", "show host USB devices", },
1296 { "profile", "", do_info_profile,
1297 "", "show profiling information", },
1298 { "capture", "", do_info_capture,
1299 "", "show capture information" },
1300 { "snapshots", "", do_info_snapshots,
1301 "", "show the currently saved VM snapshots" },
1302 { "mice", "", do_info_mice,
1303 "", "show which guest mouse is receiving events" },
1304 { "vnc", "", do_info_vnc,
1305 "", "show the vnc server status"},
1309 /*******************************************************************/
1311 static const char *pch;
1312 static jmp_buf expr_env;
1317 typedef struct MonitorDef {
1320 target_long (*get_value)(struct MonitorDef *md, int val);
1324 #if defined(TARGET_I386)
1325 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1327 CPUState *env = mon_get_cpu();
1330 return env->eip + env->segs[R_CS].base;
1334 #if defined(TARGET_PPC)
1335 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1337 CPUState *env = mon_get_cpu();
1345 for (i = 0; i < 8; i++)
1346 u |= env->crf[i] << (32 - (4 * i));
1351 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1353 CPUState *env = mon_get_cpu();
1356 return (env->msr[MSR_POW] << MSR_POW) |
1357 (env->msr[MSR_ILE] << MSR_ILE) |
1358 (env->msr[MSR_EE] << MSR_EE) |
1359 (env->msr[MSR_PR] << MSR_PR) |
1360 (env->msr[MSR_FP] << MSR_FP) |
1361 (env->msr[MSR_ME] << MSR_ME) |
1362 (env->msr[MSR_FE0] << MSR_FE0) |
1363 (env->msr[MSR_SE] << MSR_SE) |
1364 (env->msr[MSR_BE] << MSR_BE) |
1365 (env->msr[MSR_FE1] << MSR_FE1) |
1366 (env->msr[MSR_IP] << MSR_IP) |
1367 (env->msr[MSR_IR] << MSR_IR) |
1368 (env->msr[MSR_DR] << MSR_DR) |
1369 (env->msr[MSR_RI] << MSR_RI) |
1370 (env->msr[MSR_LE] << MSR_LE);
1373 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1375 CPUState *env = mon_get_cpu();
1378 return (env->xer[XER_SO] << XER_SO) |
1379 (env->xer[XER_OV] << XER_OV) |
1380 (env->xer[XER_CA] << XER_CA) |
1381 (env->xer[XER_BC] << XER_BC);
1384 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1386 CPUState *env = mon_get_cpu();
1389 return cpu_ppc_load_decr(env);
1392 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1394 CPUState *env = mon_get_cpu();
1397 return cpu_ppc_load_tbu(env);
1400 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1402 CPUState *env = mon_get_cpu();
1405 return cpu_ppc_load_tbl(env);
1409 #if defined(TARGET_SPARC)
1410 #ifndef TARGET_SPARC64
1411 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1413 CPUState *env = mon_get_cpu();
1416 return GET_PSR(env);
1420 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1422 CPUState *env = mon_get_cpu();
1425 return env->regwptr[val];
1429 static MonitorDef monitor_defs[] = {
1432 #define SEG(name, seg) \
1433 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1434 { name ".base", offsetof(CPUState, segs[seg].base) },\
1435 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1437 { "eax", offsetof(CPUState, regs[0]) },
1438 { "ecx", offsetof(CPUState, regs[1]) },
1439 { "edx", offsetof(CPUState, regs[2]) },
1440 { "ebx", offsetof(CPUState, regs[3]) },
1441 { "esp|sp", offsetof(CPUState, regs[4]) },
1442 { "ebp|fp", offsetof(CPUState, regs[5]) },
1443 { "esi", offsetof(CPUState, regs[6]) },
1444 { "edi", offsetof(CPUState, regs[7]) },
1445 #ifdef TARGET_X86_64
1446 { "r8", offsetof(CPUState, regs[8]) },
1447 { "r9", offsetof(CPUState, regs[9]) },
1448 { "r10", offsetof(CPUState, regs[10]) },
1449 { "r11", offsetof(CPUState, regs[11]) },
1450 { "r12", offsetof(CPUState, regs[12]) },
1451 { "r13", offsetof(CPUState, regs[13]) },
1452 { "r14", offsetof(CPUState, regs[14]) },
1453 { "r15", offsetof(CPUState, regs[15]) },
1455 { "eflags", offsetof(CPUState, eflags) },
1456 { "eip", offsetof(CPUState, eip) },
1463 { "pc", 0, monitor_get_pc, },
1464 #elif defined(TARGET_PPC)
1465 { "r0", offsetof(CPUState, gpr[0]) },
1466 { "r1", offsetof(CPUState, gpr[1]) },
1467 { "r2", offsetof(CPUState, gpr[2]) },
1468 { "r3", offsetof(CPUState, gpr[3]) },
1469 { "r4", offsetof(CPUState, gpr[4]) },
1470 { "r5", offsetof(CPUState, gpr[5]) },
1471 { "r6", offsetof(CPUState, gpr[6]) },
1472 { "r7", offsetof(CPUState, gpr[7]) },
1473 { "r8", offsetof(CPUState, gpr[8]) },
1474 { "r9", offsetof(CPUState, gpr[9]) },
1475 { "r10", offsetof(CPUState, gpr[10]) },
1476 { "r11", offsetof(CPUState, gpr[11]) },
1477 { "r12", offsetof(CPUState, gpr[12]) },
1478 { "r13", offsetof(CPUState, gpr[13]) },
1479 { "r14", offsetof(CPUState, gpr[14]) },
1480 { "r15", offsetof(CPUState, gpr[15]) },
1481 { "r16", offsetof(CPUState, gpr[16]) },
1482 { "r17", offsetof(CPUState, gpr[17]) },
1483 { "r18", offsetof(CPUState, gpr[18]) },
1484 { "r19", offsetof(CPUState, gpr[19]) },
1485 { "r20", offsetof(CPUState, gpr[20]) },
1486 { "r21", offsetof(CPUState, gpr[21]) },
1487 { "r22", offsetof(CPUState, gpr[22]) },
1488 { "r23", offsetof(CPUState, gpr[23]) },
1489 { "r24", offsetof(CPUState, gpr[24]) },
1490 { "r25", offsetof(CPUState, gpr[25]) },
1491 { "r26", offsetof(CPUState, gpr[26]) },
1492 { "r27", offsetof(CPUState, gpr[27]) },
1493 { "r28", offsetof(CPUState, gpr[28]) },
1494 { "r29", offsetof(CPUState, gpr[29]) },
1495 { "r30", offsetof(CPUState, gpr[30]) },
1496 { "r31", offsetof(CPUState, gpr[31]) },
1497 { "nip|pc", offsetof(CPUState, nip) },
1498 { "lr", offsetof(CPUState, lr) },
1499 { "ctr", offsetof(CPUState, ctr) },
1500 { "decr", 0, &monitor_get_decr, },
1501 { "ccr", 0, &monitor_get_ccr, },
1502 { "msr", 0, &monitor_get_msr, },
1503 { "xer", 0, &monitor_get_xer, },
1504 { "tbu", 0, &monitor_get_tbu, },
1505 { "tbl", 0, &monitor_get_tbl, },
1506 { "sdr1", offsetof(CPUState, sdr1) },
1507 { "sr0", offsetof(CPUState, sr[0]) },
1508 { "sr1", offsetof(CPUState, sr[1]) },
1509 { "sr2", offsetof(CPUState, sr[2]) },
1510 { "sr3", offsetof(CPUState, sr[3]) },
1511 { "sr4", offsetof(CPUState, sr[4]) },
1512 { "sr5", offsetof(CPUState, sr[5]) },
1513 { "sr6", offsetof(CPUState, sr[6]) },
1514 { "sr7", offsetof(CPUState, sr[7]) },
1515 { "sr8", offsetof(CPUState, sr[8]) },
1516 { "sr9", offsetof(CPUState, sr[9]) },
1517 { "sr10", offsetof(CPUState, sr[10]) },
1518 { "sr11", offsetof(CPUState, sr[11]) },
1519 { "sr12", offsetof(CPUState, sr[12]) },
1520 { "sr13", offsetof(CPUState, sr[13]) },
1521 { "sr14", offsetof(CPUState, sr[14]) },
1522 { "sr15", offsetof(CPUState, sr[15]) },
1523 /* Too lazy to put BATs and SPRs ... */
1524 #elif defined(TARGET_SPARC)
1525 { "g0", offsetof(CPUState, gregs[0]) },
1526 { "g1", offsetof(CPUState, gregs[1]) },
1527 { "g2", offsetof(CPUState, gregs[2]) },
1528 { "g3", offsetof(CPUState, gregs[3]) },
1529 { "g4", offsetof(CPUState, gregs[4]) },
1530 { "g5", offsetof(CPUState, gregs[5]) },
1531 { "g6", offsetof(CPUState, gregs[6]) },
1532 { "g7", offsetof(CPUState, gregs[7]) },
1533 { "o0", 0, monitor_get_reg },
1534 { "o1", 1, monitor_get_reg },
1535 { "o2", 2, monitor_get_reg },
1536 { "o3", 3, monitor_get_reg },
1537 { "o4", 4, monitor_get_reg },
1538 { "o5", 5, monitor_get_reg },
1539 { "o6", 6, monitor_get_reg },
1540 { "o7", 7, monitor_get_reg },
1541 { "l0", 8, monitor_get_reg },
1542 { "l1", 9, monitor_get_reg },
1543 { "l2", 10, monitor_get_reg },
1544 { "l3", 11, monitor_get_reg },
1545 { "l4", 12, monitor_get_reg },
1546 { "l5", 13, monitor_get_reg },
1547 { "l6", 14, monitor_get_reg },
1548 { "l7", 15, monitor_get_reg },
1549 { "i0", 16, monitor_get_reg },
1550 { "i1", 17, monitor_get_reg },
1551 { "i2", 18, monitor_get_reg },
1552 { "i3", 19, monitor_get_reg },
1553 { "i4", 20, monitor_get_reg },
1554 { "i5", 21, monitor_get_reg },
1555 { "i6", 22, monitor_get_reg },
1556 { "i7", 23, monitor_get_reg },
1557 { "pc", offsetof(CPUState, pc) },
1558 { "npc", offsetof(CPUState, npc) },
1559 { "y", offsetof(CPUState, y) },
1560 #ifndef TARGET_SPARC64
1561 { "psr", 0, &monitor_get_psr, },
1562 { "wim", offsetof(CPUState, wim) },
1564 { "tbr", offsetof(CPUState, tbr) },
1565 { "fsr", offsetof(CPUState, fsr) },
1566 { "f0", offsetof(CPUState, fpr[0]) },
1567 { "f1", offsetof(CPUState, fpr[1]) },
1568 { "f2", offsetof(CPUState, fpr[2]) },
1569 { "f3", offsetof(CPUState, fpr[3]) },
1570 { "f4", offsetof(CPUState, fpr[4]) },
1571 { "f5", offsetof(CPUState, fpr[5]) },
1572 { "f6", offsetof(CPUState, fpr[6]) },
1573 { "f7", offsetof(CPUState, fpr[7]) },
1574 { "f8", offsetof(CPUState, fpr[8]) },
1575 { "f9", offsetof(CPUState, fpr[9]) },
1576 { "f10", offsetof(CPUState, fpr[10]) },
1577 { "f11", offsetof(CPUState, fpr[11]) },
1578 { "f12", offsetof(CPUState, fpr[12]) },
1579 { "f13", offsetof(CPUState, fpr[13]) },
1580 { "f14", offsetof(CPUState, fpr[14]) },
1581 { "f15", offsetof(CPUState, fpr[15]) },
1582 { "f16", offsetof(CPUState, fpr[16]) },
1583 { "f17", offsetof(CPUState, fpr[17]) },
1584 { "f18", offsetof(CPUState, fpr[18]) },
1585 { "f19", offsetof(CPUState, fpr[19]) },
1586 { "f20", offsetof(CPUState, fpr[20]) },
1587 { "f21", offsetof(CPUState, fpr[21]) },
1588 { "f22", offsetof(CPUState, fpr[22]) },
1589 { "f23", offsetof(CPUState, fpr[23]) },
1590 { "f24", offsetof(CPUState, fpr[24]) },
1591 { "f25", offsetof(CPUState, fpr[25]) },
1592 { "f26", offsetof(CPUState, fpr[26]) },
1593 { "f27", offsetof(CPUState, fpr[27]) },
1594 { "f28", offsetof(CPUState, fpr[28]) },
1595 { "f29", offsetof(CPUState, fpr[29]) },
1596 { "f30", offsetof(CPUState, fpr[30]) },
1597 { "f31", offsetof(CPUState, fpr[31]) },
1598 #ifdef TARGET_SPARC64
1599 { "f32", offsetof(CPUState, fpr[32]) },
1600 { "f34", offsetof(CPUState, fpr[34]) },
1601 { "f36", offsetof(CPUState, fpr[36]) },
1602 { "f38", offsetof(CPUState, fpr[38]) },
1603 { "f40", offsetof(CPUState, fpr[40]) },
1604 { "f42", offsetof(CPUState, fpr[42]) },
1605 { "f44", offsetof(CPUState, fpr[44]) },
1606 { "f46", offsetof(CPUState, fpr[46]) },
1607 { "f48", offsetof(CPUState, fpr[48]) },
1608 { "f50", offsetof(CPUState, fpr[50]) },
1609 { "f52", offsetof(CPUState, fpr[52]) },
1610 { "f54", offsetof(CPUState, fpr[54]) },
1611 { "f56", offsetof(CPUState, fpr[56]) },
1612 { "f58", offsetof(CPUState, fpr[58]) },
1613 { "f60", offsetof(CPUState, fpr[60]) },
1614 { "f62", offsetof(CPUState, fpr[62]) },
1615 { "asi", offsetof(CPUState, asi) },
1616 { "pstate", offsetof(CPUState, pstate) },
1617 { "cansave", offsetof(CPUState, cansave) },
1618 { "canrestore", offsetof(CPUState, canrestore) },
1619 { "otherwin", offsetof(CPUState, otherwin) },
1620 { "wstate", offsetof(CPUState, wstate) },
1621 { "cleanwin", offsetof(CPUState, cleanwin) },
1622 { "fprs", offsetof(CPUState, fprs) },
1628 static void expr_error(const char *fmt)
1632 longjmp(expr_env, 1);
1635 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1636 static int get_monitor_def(target_long *pval, const char *name)
1641 for(md = monitor_defs; md->name != NULL; md++) {
1642 if (compare_cmd(name, md->name)) {
1643 if (md->get_value) {
1644 *pval = md->get_value(md, md->offset);
1646 CPUState *env = mon_get_cpu();
1649 ptr = (uint8_t *)env + md->offset;
1652 *pval = *(int32_t *)ptr;
1655 *pval = *(target_long *)ptr;
1668 static void next(void)
1672 while (isspace(*pch))
1677 static target_long expr_sum(void);
1679 static target_long expr_unary(void)
1702 expr_error("')' expected");
1709 expr_error("character constant expected");
1713 expr_error("missing terminating \' character");
1722 while ((*pch >= 'a' && *pch <= 'z') ||
1723 (*pch >= 'A' && *pch <= 'Z') ||
1724 (*pch >= '0' && *pch <= '9') ||
1725 *pch == '_' || *pch == '.') {
1726 if ((q - buf) < sizeof(buf) - 1)
1730 while (isspace(*pch))
1733 ret = get_monitor_def(&n, buf);
1735 expr_error("unknown register");
1737 expr_error("no cpu defined");
1741 expr_error("unexpected end of expression");
1745 #if TARGET_LONG_BITS == 64
1746 n = strtoull(pch, &p, 0);
1748 n = strtoul(pch, &p, 0);
1751 expr_error("invalid char in expression");
1754 while (isspace(*pch))
1762 static target_long expr_prod(void)
1764 target_long val, val2;
1770 if (op != '*' && op != '/' && op != '%')
1773 val2 = expr_unary();
1782 expr_error("division by zero");
1793 static target_long expr_logic(void)
1795 target_long val, val2;
1801 if (op != '&' && op != '|' && op != '^')
1821 static target_long expr_sum(void)
1823 target_long val, val2;
1829 if (op != '+' && op != '-')
1832 val2 = expr_logic();
1841 static int get_expr(target_long *pval, const char **pp)
1844 if (setjmp(expr_env)) {
1848 while (isspace(*pch))
1855 static int get_str(char *buf, int buf_size, const char **pp)
1873 while (*p != '\0' && *p != '\"') {
1889 qemu_printf("unsupported escape code: '\\%c'\n", c);
1892 if ((q - buf) < buf_size - 1) {
1896 if ((q - buf) < buf_size - 1) {
1903 qemu_printf("unterminated string\n");
1908 while (*p != '\0' && !isspace(*p)) {
1909 if ((q - buf) < buf_size - 1) {
1920 static int default_fmt_format = 'x';
1921 static int default_fmt_size = 4;
1925 static void monitor_handle_command(const char *cmdline)
1927 const char *p, *pstart, *typestr;
1929 int c, nb_args, len, i, has_arg;
1933 void *str_allocated[MAX_ARGS];
1934 void *args[MAX_ARGS];
1937 term_printf("command='%s'\n", cmdline);
1940 /* extract the command name */
1948 while (*p != '\0' && *p != '/' && !isspace(*p))
1951 if (len > sizeof(cmdname) - 1)
1952 len = sizeof(cmdname) - 1;
1953 memcpy(cmdname, pstart, len);
1954 cmdname[len] = '\0';
1956 /* find the command */
1957 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1958 if (compare_cmd(cmdname, cmd->name))
1961 term_printf("unknown command: '%s'\n", cmdname);
1965 for(i = 0; i < MAX_ARGS; i++)
1966 str_allocated[i] = NULL;
1968 /* parse the parameters */
1969 typestr = cmd->args_type;
1986 if (*typestr == '?') {
1989 /* no optional string: NULL argument */
1994 ret = get_str(buf, sizeof(buf), &p);
1998 term_printf("%s: filename expected\n", cmdname);
2001 term_printf("%s: block device name expected\n", cmdname);
2004 term_printf("%s: string expected\n", cmdname);
2009 str = qemu_malloc(strlen(buf) + 1);
2011 str_allocated[nb_args] = str;
2013 if (nb_args >= MAX_ARGS) {
2015 term_printf("%s: too many arguments\n", cmdname);
2018 args[nb_args++] = str;
2023 int count, format, size;
2033 while (isdigit(*p)) {
2034 count = count * 10 + (*p - '0');
2072 if (*p != '\0' && !isspace(*p)) {
2073 term_printf("invalid char in format: '%c'\n", *p);
2077 format = default_fmt_format;
2078 if (format != 'i') {
2079 /* for 'i', not specifying a size gives -1 as size */
2081 size = default_fmt_size;
2083 default_fmt_size = size;
2084 default_fmt_format = format;
2087 format = default_fmt_format;
2088 if (format != 'i') {
2089 size = default_fmt_size;
2094 if (nb_args + 3 > MAX_ARGS)
2096 args[nb_args++] = (void*)count;
2097 args[nb_args++] = (void*)format;
2098 args[nb_args++] = (void*)size;
2107 if (*typestr == '?' || *typestr == '.') {
2108 if (*typestr == '?') {
2124 if (nb_args >= MAX_ARGS)
2126 args[nb_args++] = (void *)has_arg;
2128 if (nb_args >= MAX_ARGS)
2134 if (get_expr(&val, &p))
2138 if (nb_args >= MAX_ARGS)
2140 args[nb_args++] = (void *)(int)val;
2142 if ((nb_args + 1) >= MAX_ARGS)
2144 #if TARGET_LONG_BITS == 64
2145 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2147 args[nb_args++] = (void *)0;
2149 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2167 term_printf("%s: unsupported option -%c\n",
2174 if (nb_args >= MAX_ARGS)
2176 args[nb_args++] = (void *)has_option;
2181 term_printf("%s: unknown type '%c'\n", cmdname, c);
2185 /* check that all arguments were parsed */
2189 term_printf("%s: extraneous characters at the end of line\n",
2199 cmd->handler(args[0]);
2202 cmd->handler(args[0], args[1]);
2205 cmd->handler(args[0], args[1], args[2]);
2208 cmd->handler(args[0], args[1], args[2], args[3]);
2211 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2214 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2217 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2220 term_printf("unsupported number of arguments: %d\n", nb_args);
2224 for(i = 0; i < MAX_ARGS; i++)
2225 qemu_free(str_allocated[i]);
2229 static void cmd_completion(const char *name, const char *list)
2231 const char *p, *pstart;
2240 p = pstart + strlen(pstart);
2242 if (len > sizeof(cmd) - 2)
2243 len = sizeof(cmd) - 2;
2244 memcpy(cmd, pstart, len);
2246 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2247 add_completion(cmd);
2255 static void file_completion(const char *input)
2260 char file[1024], file_prefix[1024];
2264 p = strrchr(input, '/');
2267 pstrcpy(file_prefix, sizeof(file_prefix), input);
2270 input_path_len = p - input + 1;
2271 memcpy(path, input, input_path_len);
2272 if (input_path_len > sizeof(path) - 1)
2273 input_path_len = sizeof(path) - 1;
2274 path[input_path_len] = '\0';
2275 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2277 #ifdef DEBUG_COMPLETION
2278 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2280 ffs = opendir(path);
2288 if (strstart(d->d_name, file_prefix, NULL)) {
2289 memcpy(file, input, input_path_len);
2290 strcpy(file + input_path_len, d->d_name);
2291 /* stat the file to find out if it's a directory.
2292 * In that case add a slash to speed up typing long paths
2295 if(S_ISDIR(sb.st_mode))
2297 add_completion(file);
2303 static void block_completion_it(void *opaque, const char *name)
2305 const char *input = opaque;
2307 if (input[0] == '\0' ||
2308 !strncmp(name, (char *)input, strlen(input))) {
2309 add_completion(name);
2313 /* NOTE: this parser is an approximate form of the real command parser */
2314 static void parse_cmdline(const char *cmdline,
2315 int *pnb_args, char **args)
2328 if (nb_args >= MAX_ARGS)
2330 ret = get_str(buf, sizeof(buf), &p);
2331 args[nb_args] = qemu_strdup(buf);
2336 *pnb_args = nb_args;
2339 void readline_find_completion(const char *cmdline)
2341 const char *cmdname;
2342 char *args[MAX_ARGS];
2343 int nb_args, i, len;
2344 const char *ptype, *str;
2348 parse_cmdline(cmdline, &nb_args, args);
2349 #ifdef DEBUG_COMPLETION
2350 for(i = 0; i < nb_args; i++) {
2351 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2355 /* if the line ends with a space, it means we want to complete the
2357 len = strlen(cmdline);
2358 if (len > 0 && isspace(cmdline[len - 1])) {
2359 if (nb_args >= MAX_ARGS)
2361 args[nb_args++] = qemu_strdup("");
2364 /* command completion */
2369 completion_index = strlen(cmdname);
2370 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2371 cmd_completion(cmdname, cmd->name);
2374 /* find the command */
2375 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2376 if (compare_cmd(args[0], cmd->name))
2381 ptype = cmd->args_type;
2382 for(i = 0; i < nb_args - 2; i++) {
2383 if (*ptype != '\0') {
2385 while (*ptype == '?')
2389 str = args[nb_args - 1];
2392 /* file completion */
2393 completion_index = strlen(str);
2394 file_completion(str);
2397 /* block device name completion */
2398 completion_index = strlen(str);
2399 bdrv_iterate(block_completion_it, (void *)str);
2402 /* XXX: more generic ? */
2403 if (!strcmp(cmd->name, "info")) {
2404 completion_index = strlen(str);
2405 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2406 cmd_completion(str, cmd->name);
2408 } else if (!strcmp(cmd->name, "sendkey")) {
2409 completion_index = strlen(str);
2410 for(key = key_defs; key->name != NULL; key++) {
2411 cmd_completion(str, key->name);
2419 for(i = 0; i < nb_args; i++)
2423 static int term_can_read(void *opaque)
2428 static void term_read(void *opaque, const uint8_t *buf, int size)
2431 for(i = 0; i < size; i++)
2432 readline_handle_byte(buf[i]);
2435 static void monitor_start_input(void);
2437 static void monitor_handle_command1(void *opaque, const char *cmdline)
2439 monitor_handle_command(cmdline);
2440 monitor_start_input();
2443 static void monitor_start_input(void)
2445 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2448 static void term_event(void *opaque, int event)
2450 if (event != CHR_EVENT_RESET)
2454 term_printf("QEMU %s monitor - type 'help' for more information\n",
2456 monitor_start_input();
2459 static int is_first_init = 1;
2461 void monitor_init(CharDriverState *hd, int show_banner)
2465 if (is_first_init) {
2466 for (i = 0; i < MAX_MON; i++) {
2467 monitor_hd[i] = NULL;
2471 for (i = 0; i < MAX_MON; i++) {
2472 if (monitor_hd[i] == NULL) {
2478 hide_banner = !show_banner;
2480 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2483 /* XXX: use threads ? */
2484 /* modal monitor readline */
2485 static int monitor_readline_started;
2486 static char *monitor_readline_buf;
2487 static int monitor_readline_buf_size;
2489 static void monitor_readline_cb(void *opaque, const char *input)
2491 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2492 monitor_readline_started = 0;
2495 void monitor_readline(const char *prompt, int is_password,
2496 char *buf, int buf_size)
2501 for (i = 0; i < MAX_MON; i++)
2502 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2503 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2505 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2506 monitor_readline_buf = buf;
2507 monitor_readline_buf_size = buf_size;
2508 monitor_readline_started = 1;
2509 while (monitor_readline_started) {