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;
59 static term_cmd_t term_cmds[];
60 static term_cmd_t info_cmds[];
62 static char term_outbuf[1024];
63 static int term_outbuf_index;
65 static void monitor_start_input(void);
67 CPUState *mon_cpu = NULL;
71 if (term_outbuf_index > 0) {
72 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
73 term_outbuf_index = 0;
77 /* flush at every end of line or if the buffer is full */
78 void term_puts(const char *str)
86 term_outbuf[term_outbuf_index++] = '\r';
87 term_outbuf[term_outbuf_index++] = c;
88 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
94 void term_vprintf(const char *fmt, va_list ap)
97 vsnprintf(buf, sizeof(buf), fmt, ap);
101 void term_printf(const char *fmt, ...)
105 term_vprintf(fmt, ap);
109 void term_print_filename(const char *filename)
113 for (i = 0; filename[i]; i++) {
114 switch (filename[i]) {
118 term_printf("\\%c", filename[i]);
130 term_printf("%c", filename[i]);
136 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
140 term_vprintf(fmt, ap);
145 static int compare_cmd(const char *name, const char *list)
147 const char *p, *pstart;
155 p = pstart + strlen(pstart);
156 if ((p - pstart) == len && !memcmp(pstart, name, len))
165 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
169 for(cmd = cmds; cmd->name != NULL; cmd++) {
170 if (!name || !strcmp(name, cmd->name))
171 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
175 static void help_cmd(const char *name)
177 if (name && !strcmp(name, "info")) {
178 help_cmd1(info_cmds, "info ", NULL);
180 help_cmd1(term_cmds, "", name);
181 if (name && !strcmp(name, "log")) {
183 term_printf("Log items (comma separated):\n");
184 term_printf("%-10s %s\n", "none", "remove all logs");
185 for(item = cpu_log_items; item->mask != 0; item++) {
186 term_printf("%-10s %s\n", item->name, item->help);
192 static void do_help(const char *name)
197 static void do_commit(const char *device)
201 all_devices = !strcmp(device, "all");
202 for (i = 0; i < MAX_DISKS; i++) {
205 !strcmp(bdrv_get_device_name(bs_table[i]), device))
206 bdrv_commit(bs_table[i]);
211 static void do_info(const char *item)
217 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
218 if (compare_cmd(item, cmd->name))
228 static void do_info_version(void)
230 term_printf("%s\n", QEMU_VERSION);
233 static void do_info_block(void)
238 /* get the current CPU defined by the user */
239 int mon_set_cpu(int cpu_index)
243 for(env = first_cpu; env != NULL; env = env->next_cpu) {
244 if (env->cpu_index == cpu_index) {
252 CPUState *mon_get_cpu(void)
260 static void do_info_registers(void)
267 cpu_dump_state(env, NULL, monitor_fprintf,
270 cpu_dump_state(env, NULL, monitor_fprintf,
275 static void do_info_cpus(void)
279 /* just to set the default cpu if not already done */
282 for(env = first_cpu; env != NULL; env = env->next_cpu) {
283 term_printf("%c CPU #%d:",
284 (env == mon_cpu) ? '*' : ' ',
286 #if defined(TARGET_I386)
287 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
288 if (env->hflags & HF_HALTED_MASK)
289 term_printf(" (halted)");
290 #elif defined(TARGET_PPC)
291 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
293 term_printf(" (halted)");
294 #elif defined(TARGET_SPARC)
295 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
297 term_printf(" (halted)");
303 static void do_cpu_set(int index)
305 if (mon_set_cpu(index) < 0)
306 term_printf("Invalid CPU index\n");
309 static void do_info_jit(void)
311 dump_exec_info(NULL, monitor_fprintf);
314 static void do_info_history (void)
321 str = readline_get_history(i);
324 term_printf("%d: '%s'\n", i, str);
329 static void do_quit(void)
334 static int eject_device(BlockDriverState *bs, int force)
336 if (bdrv_is_inserted(bs)) {
338 if (!bdrv_is_removable(bs)) {
339 term_printf("device is not removable\n");
342 if (bdrv_is_locked(bs)) {
343 term_printf("device is locked\n");
352 static void do_eject(int force, const char *filename)
354 BlockDriverState *bs;
356 bs = bdrv_find(filename);
358 term_printf("device not found\n");
361 eject_device(bs, force);
364 static void do_change(const char *device, const char *filename)
366 BlockDriverState *bs;
370 bs = bdrv_find(device);
372 term_printf("device not found\n");
375 if (eject_device(bs, 0) < 0)
377 bdrv_open(bs, filename, 0);
378 if (bdrv_is_encrypted(bs)) {
379 term_printf("%s is encrypted.\n", device);
380 for(i = 0; i < 3; i++) {
381 monitor_readline("Password: ", 1, password, sizeof(password));
382 if (bdrv_set_key(bs, password) == 0)
384 term_printf("invalid password\n");
389 static void do_screen_dump(const char *filename)
391 vga_hw_screen_dump(filename);
394 static void do_log(const char *items)
398 if (!strcmp(items, "none")) {
401 mask = cpu_str_to_log_mask(items);
410 static void do_stop(void)
412 vm_stop(EXCP_INTERRUPT);
415 static void do_cont(void)
420 #ifdef CONFIG_GDBSTUB
421 static void do_gdbserver(int has_port, int port)
424 port = DEFAULT_GDBSTUB_PORT;
425 if (gdbserver_start(port) < 0) {
426 qemu_printf("Could not open gdbserver socket on port %d\n", port);
428 qemu_printf("Waiting gdb connection on port %d\n", port);
433 static void term_printc(int c)
450 if (c >= 32 && c <= 126) {
451 term_printf("%c", c);
453 term_printf("\\x%02x", c);
460 static void memory_dump(int count, int format, int wsize,
461 target_ulong addr, int is_physical)
464 int nb_per_line, l, line_size, i, max_digits, len;
472 if (!env && !is_physical)
477 } else if (wsize == 4) {
480 /* as default we use the current CS size */
484 if ((env->efer & MSR_EFER_LMA) &&
485 (env->segs[R_CS].flags & DESC_L_MASK))
489 if (!(env->segs[R_CS].flags & DESC_B_MASK))
494 monitor_disas(env, addr, count, is_physical, flags);
503 nb_per_line = line_size / wsize;
508 max_digits = (wsize * 8 + 2) / 3;
512 max_digits = (wsize * 8) / 4;
516 max_digits = (wsize * 8 * 10 + 32) / 33;
524 term_printf(TARGET_FMT_lx ":", addr);
529 cpu_physical_memory_rw(addr, buf, l, 0);
534 cpu_memory_rw_debug(env, addr, buf, l, 0);
541 v = ldub_raw(buf + i);
544 v = lduw_raw(buf + i);
547 v = (uint32_t)ldl_raw(buf + i);
550 v = ldq_raw(buf + i);
556 term_printf("%#*" PRIo64, max_digits, v);
559 term_printf("0x%0*" PRIx64, max_digits, v);
562 term_printf("%*" PRIu64, max_digits, v);
565 term_printf("%*" PRId64, max_digits, v);
579 #if TARGET_LONG_BITS == 64
580 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
582 #define GET_TLONG(h, l) (l)
585 static void do_memory_dump(int count, int format, int size,
586 uint32_t addrh, uint32_t addrl)
588 target_long addr = GET_TLONG(addrh, addrl);
589 memory_dump(count, format, size, addr, 0);
592 static void do_physical_memory_dump(int count, int format, int size,
593 uint32_t addrh, uint32_t addrl)
596 target_long addr = GET_TLONG(addrh, addrl);
597 memory_dump(count, format, size, addr, 1);
600 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
602 target_long val = GET_TLONG(valh, vall);
603 #if TARGET_LONG_BITS == 32
606 term_printf("%#o", val);
609 term_printf("%#x", val);
612 term_printf("%u", val);
616 term_printf("%d", val);
625 term_printf("%#" PRIo64, val);
628 term_printf("%#" PRIx64, val);
631 term_printf("%" PRIu64, val);
635 term_printf("%" PRId64, val);
645 static void do_memory_save(unsigned int valh, unsigned int vall,
646 uint32_t size, const char *filename)
649 target_long addr = GET_TLONG(valh, vall);
658 f = fopen(filename, "wb");
660 term_printf("could not open '%s'\n", filename);
667 cpu_memory_rw_debug(env, addr, buf, l, 0);
668 fwrite(buf, 1, l, f);
675 static void do_sum(uint32_t start, uint32_t size)
682 for(addr = start; addr < (start + size); addr++) {
683 cpu_physical_memory_rw(addr, buf, 1, 0);
684 /* BSD sum algorithm ('sum' Unix command) */
685 sum = (sum >> 1) | (sum << 15);
688 term_printf("%05d\n", sum);
696 static const KeyDef key_defs[] = {
721 { 0x0e, "backspace" },
756 { 0x3a, "caps_lock" },
767 { 0x45, "num_lock" },
768 { 0x46, "scroll_lock" },
770 { 0xb5, "kp_divide" },
771 { 0x37, "kp_multiply" },
772 { 0x4a, "kp_substract" },
774 { 0x9c, "kp_enter" },
775 { 0x53, "kp_decimal" },
810 static int get_keycode(const char *key)
816 for(p = key_defs; p->name != NULL; p++) {
817 if (!strcmp(key, p->name))
820 if (strstart(key, "0x", NULL)) {
821 ret = strtoul(key, &endp, 0);
822 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
828 static void do_send_key(const char *string)
831 uint8_t keycodes[16];
833 int nb_keycodes, keycode, i;
839 while (*p != '\0' && *p != '-') {
840 if ((q - keybuf) < sizeof(keybuf) - 1) {
846 keycode = get_keycode(keybuf);
848 term_printf("unknown key: '%s'\n", keybuf);
851 keycodes[nb_keycodes++] = keycode;
856 /* key down events */
857 for(i = 0; i < nb_keycodes; i++) {
858 keycode = keycodes[i];
860 kbd_put_keycode(0xe0);
861 kbd_put_keycode(keycode & 0x7f);
864 for(i = nb_keycodes - 1; i >= 0; i--) {
865 keycode = keycodes[i];
867 kbd_put_keycode(0xe0);
868 kbd_put_keycode(keycode | 0x80);
872 static int mouse_button_state;
874 static void do_mouse_move(const char *dx_str, const char *dy_str,
878 dx = strtol(dx_str, NULL, 0);
879 dy = strtol(dy_str, NULL, 0);
882 dz = strtol(dz_str, NULL, 0);
883 kbd_mouse_event(dx, dy, dz, mouse_button_state);
886 static void do_mouse_button(int button_state)
888 mouse_button_state = button_state;
889 kbd_mouse_event(0, 0, 0, mouse_button_state);
892 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
898 cpu_outb(NULL, addr & 0xffff, index & 0xff);
906 val = cpu_inb(NULL, addr);
910 val = cpu_inw(NULL, addr);
914 val = cpu_inl(NULL, addr);
918 term_printf("port%c[0x%04x] = %#0*x\n",
919 suffix, addr, size * 2, val);
922 static void do_system_reset(void)
924 qemu_system_reset_request();
927 static void do_system_powerdown(void)
929 qemu_system_powerdown_request();
932 #if defined(TARGET_I386)
933 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
935 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
938 pte & PG_GLOBAL_MASK ? 'G' : '-',
939 pte & PG_PSE_MASK ? 'P' : '-',
940 pte & PG_DIRTY_MASK ? 'D' : '-',
941 pte & PG_ACCESSED_MASK ? 'A' : '-',
942 pte & PG_PCD_MASK ? 'C' : '-',
943 pte & PG_PWT_MASK ? 'T' : '-',
944 pte & PG_USER_MASK ? 'U' : '-',
945 pte & PG_RW_MASK ? 'W' : '-');
948 static void tlb_info(void)
952 uint32_t pgd, pde, pte;
958 if (!(env->cr[0] & CR0_PG_MASK)) {
959 term_printf("PG disabled\n");
962 pgd = env->cr[3] & ~0xfff;
963 for(l1 = 0; l1 < 1024; l1++) {
964 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
965 pde = le32_to_cpu(pde);
966 if (pde & PG_PRESENT_MASK) {
967 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
968 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
970 for(l2 = 0; l2 < 1024; l2++) {
971 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
973 pte = le32_to_cpu(pte);
974 if (pte & PG_PRESENT_MASK) {
975 print_pte((l1 << 22) + (l2 << 12),
985 static void mem_print(uint32_t *pstart, int *plast_prot,
986 uint32_t end, int prot)
992 term_printf("%08x-%08x %08x %c%c%c\n",
993 *pstart, end, end - *pstart,
994 prot1 & PG_USER_MASK ? 'u' : '-',
996 prot1 & PG_RW_MASK ? 'w' : '-');
1006 static void mem_info(void)
1009 int l1, l2, prot, last_prot;
1010 uint32_t pgd, pde, pte, start, end;
1012 env = mon_get_cpu();
1016 if (!(env->cr[0] & CR0_PG_MASK)) {
1017 term_printf("PG disabled\n");
1020 pgd = env->cr[3] & ~0xfff;
1023 for(l1 = 0; l1 < 1024; l1++) {
1024 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1025 pde = le32_to_cpu(pde);
1027 if (pde & PG_PRESENT_MASK) {
1028 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1029 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1030 mem_print(&start, &last_prot, end, prot);
1032 for(l2 = 0; l2 < 1024; l2++) {
1033 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1034 (uint8_t *)&pte, 4);
1035 pte = le32_to_cpu(pte);
1036 end = (l1 << 22) + (l2 << 12);
1037 if (pte & PG_PRESENT_MASK) {
1038 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1042 mem_print(&start, &last_prot, end, prot);
1047 mem_print(&start, &last_prot, end, prot);
1053 static void do_info_kqemu(void)
1059 env = mon_get_cpu();
1061 term_printf("No cpu initialized yet");
1064 val = env->kqemu_enabled;
1065 term_printf("kqemu support: ");
1069 term_printf("disabled\n");
1072 term_printf("enabled for user code\n");
1075 term_printf("enabled for user and kernel code\n");
1079 term_printf("kqemu support: not compiled\n");
1083 #ifdef CONFIG_PROFILER
1087 int64_t kqemu_exec_count;
1089 int64_t kqemu_ret_int_count;
1090 int64_t kqemu_ret_excp_count;
1091 int64_t kqemu_ret_intr_count;
1093 static void do_info_profile(void)
1099 term_printf("async time %" PRId64 " (%0.3f)\n",
1100 dev_time, dev_time / (double)ticks_per_sec);
1101 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1102 qemu_time, qemu_time / (double)ticks_per_sec);
1103 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1104 kqemu_time, kqemu_time / (double)ticks_per_sec,
1105 kqemu_time / (double)total * 100.0,
1107 kqemu_ret_int_count,
1108 kqemu_ret_excp_count,
1109 kqemu_ret_intr_count);
1112 kqemu_exec_count = 0;
1114 kqemu_ret_int_count = 0;
1115 kqemu_ret_excp_count = 0;
1116 kqemu_ret_intr_count = 0;
1118 kqemu_record_dump();
1122 static void do_info_profile(void)
1124 term_printf("Internal profiler not compiled\n");
1128 /* Capture support */
1129 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1131 static void do_info_capture (void)
1136 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1137 term_printf ("[%d]: ", i);
1138 s->ops.info (s->opaque);
1142 static void do_stop_capture (int n)
1147 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1149 s->ops.destroy (s->opaque);
1150 LIST_REMOVE (s, entries);
1158 int wav_start_capture (CaptureState *s, const char *path, int freq,
1159 int bits, int nchannels);
1161 static void do_wav_capture (const char *path,
1162 int has_freq, int freq,
1163 int has_bits, int bits,
1164 int has_channels, int nchannels)
1168 s = qemu_mallocz (sizeof (*s));
1170 term_printf ("Not enough memory to add wave capture\n");
1174 freq = has_freq ? freq : 44100;
1175 bits = has_bits ? bits : 16;
1176 nchannels = has_channels ? nchannels : 2;
1178 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1179 term_printf ("Faied to add wave capture\n");
1182 LIST_INSERT_HEAD (&capture_head, s, entries);
1186 static term_cmd_t term_cmds[] = {
1187 { "help|?", "s?", do_help,
1188 "[cmd]", "show the help" },
1189 { "commit", "s", do_commit,
1190 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1191 { "info", "s?", do_info,
1192 "subcommand", "show various information about the system state" },
1193 { "q|quit", "", do_quit,
1194 "", "quit the emulator" },
1195 { "eject", "-fB", do_eject,
1196 "[-f] device", "eject a removable media (use -f to force it)" },
1197 { "change", "BF", do_change,
1198 "device filename", "change a removable media" },
1199 { "screendump", "F", do_screen_dump,
1200 "filename", "save screen into PPM image 'filename'" },
1201 { "log", "s", do_log,
1202 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1203 { "savevm", "s?", do_savevm,
1204 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1205 { "loadvm", "s", do_loadvm,
1206 "tag|id", "restore a VM snapshot from its tag or id" },
1207 { "delvm", "s", do_delvm,
1208 "tag|id", "delete a VM snapshot from its tag or id" },
1209 { "stop", "", do_stop,
1210 "", "stop emulation", },
1211 { "c|cont", "", do_cont,
1212 "", "resume emulation", },
1213 #ifdef CONFIG_GDBSTUB
1214 { "gdbserver", "i?", do_gdbserver,
1215 "[port]", "start gdbserver session (default port=1234)", },
1217 { "x", "/l", do_memory_dump,
1218 "/fmt addr", "virtual memory dump starting at 'addr'", },
1219 { "xp", "/l", do_physical_memory_dump,
1220 "/fmt addr", "physical memory dump starting at 'addr'", },
1221 { "p|print", "/l", do_print,
1222 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1223 { "i", "/ii.", do_ioport_read,
1224 "/fmt addr", "I/O port read" },
1226 { "sendkey", "s", do_send_key,
1227 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1228 { "system_reset", "", do_system_reset,
1229 "", "reset the system" },
1230 { "system_powerdown", "", do_system_powerdown,
1231 "", "send system power down event" },
1232 { "sum", "ii", do_sum,
1233 "addr size", "compute the checksum of a memory region" },
1234 { "usb_add", "s", do_usb_add,
1235 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1236 { "usb_del", "s", do_usb_del,
1237 "device", "remove USB device 'bus.addr'" },
1238 { "cpu", "i", do_cpu_set,
1239 "index", "set the default CPU" },
1240 { "mouse_move", "sss?", do_mouse_move,
1241 "dx dy [dz]", "send mouse move events" },
1242 { "mouse_button", "i", do_mouse_button,
1243 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1244 { "mouse_set", "i", do_mouse_set,
1245 "index", "set which mouse device receives events" },
1247 { "wavcapture", "si?i?i?", do_wav_capture,
1248 "path [frequency bits channels]",
1249 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1251 { "stopcapture", "i", do_stop_capture,
1252 "capture index", "stop capture" },
1253 { "memsave", "lis", do_memory_save,
1254 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1258 static term_cmd_t info_cmds[] = {
1259 { "version", "", do_info_version,
1260 "", "show the version of qemu" },
1261 { "network", "", do_info_network,
1262 "", "show the network state" },
1263 { "block", "", do_info_block,
1264 "", "show the block devices" },
1265 { "registers", "", do_info_registers,
1266 "", "show the cpu registers" },
1267 { "cpus", "", do_info_cpus,
1268 "", "show infos for each CPU" },
1269 { "history", "", do_info_history,
1270 "", "show the command line history", },
1271 { "irq", "", irq_info,
1272 "", "show the interrupts statistics (if available)", },
1273 { "pic", "", pic_info,
1274 "", "show i8259 (PIC) state", },
1275 { "pci", "", pci_info,
1276 "", "show PCI info", },
1277 #if defined(TARGET_I386)
1278 { "tlb", "", tlb_info,
1279 "", "show virtual to physical memory mappings", },
1280 { "mem", "", mem_info,
1281 "", "show the active virtual memory mappings", },
1283 { "jit", "", do_info_jit,
1284 "", "show dynamic compiler info", },
1285 { "kqemu", "", do_info_kqemu,
1286 "", "show kqemu information", },
1287 { "usb", "", usb_info,
1288 "", "show guest USB devices", },
1289 { "usbhost", "", usb_host_info,
1290 "", "show host USB devices", },
1291 { "profile", "", do_info_profile,
1292 "", "show profiling information", },
1293 { "capture", "", do_info_capture,
1294 "", "show capture information" },
1295 { "snapshots", "", do_info_snapshots,
1296 "", "show the currently saved VM snapshots" },
1297 { "mice", "", do_info_mice,
1298 "", "show which guest mouse is receiving events" },
1302 /*******************************************************************/
1304 static const char *pch;
1305 static jmp_buf expr_env;
1310 typedef struct MonitorDef {
1313 target_long (*get_value)(struct MonitorDef *md, int val);
1317 #if defined(TARGET_I386)
1318 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1320 CPUState *env = mon_get_cpu();
1323 return env->eip + env->segs[R_CS].base;
1327 #if defined(TARGET_PPC)
1328 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1330 CPUState *env = mon_get_cpu();
1338 for (i = 0; i < 8; i++)
1339 u |= env->crf[i] << (32 - (4 * i));
1344 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1346 CPUState *env = mon_get_cpu();
1349 return (env->msr[MSR_POW] << MSR_POW) |
1350 (env->msr[MSR_ILE] << MSR_ILE) |
1351 (env->msr[MSR_EE] << MSR_EE) |
1352 (env->msr[MSR_PR] << MSR_PR) |
1353 (env->msr[MSR_FP] << MSR_FP) |
1354 (env->msr[MSR_ME] << MSR_ME) |
1355 (env->msr[MSR_FE0] << MSR_FE0) |
1356 (env->msr[MSR_SE] << MSR_SE) |
1357 (env->msr[MSR_BE] << MSR_BE) |
1358 (env->msr[MSR_FE1] << MSR_FE1) |
1359 (env->msr[MSR_IP] << MSR_IP) |
1360 (env->msr[MSR_IR] << MSR_IR) |
1361 (env->msr[MSR_DR] << MSR_DR) |
1362 (env->msr[MSR_RI] << MSR_RI) |
1363 (env->msr[MSR_LE] << MSR_LE);
1366 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1368 CPUState *env = mon_get_cpu();
1371 return (env->xer[XER_SO] << XER_SO) |
1372 (env->xer[XER_OV] << XER_OV) |
1373 (env->xer[XER_CA] << XER_CA) |
1374 (env->xer[XER_BC] << XER_BC);
1377 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1379 CPUState *env = mon_get_cpu();
1382 return cpu_ppc_load_decr(env);
1385 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1387 CPUState *env = mon_get_cpu();
1390 return cpu_ppc_load_tbu(env);
1393 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1395 CPUState *env = mon_get_cpu();
1398 return cpu_ppc_load_tbl(env);
1402 #if defined(TARGET_SPARC)
1403 #ifndef TARGET_SPARC64
1404 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1406 CPUState *env = mon_get_cpu();
1409 return GET_PSR(env);
1413 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1415 CPUState *env = mon_get_cpu();
1418 return env->regwptr[val];
1422 static MonitorDef monitor_defs[] = {
1425 #define SEG(name, seg) \
1426 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1427 { name ".base", offsetof(CPUState, segs[seg].base) },\
1428 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1430 { "eax", offsetof(CPUState, regs[0]) },
1431 { "ecx", offsetof(CPUState, regs[1]) },
1432 { "edx", offsetof(CPUState, regs[2]) },
1433 { "ebx", offsetof(CPUState, regs[3]) },
1434 { "esp|sp", offsetof(CPUState, regs[4]) },
1435 { "ebp|fp", offsetof(CPUState, regs[5]) },
1436 { "esi", offsetof(CPUState, regs[6]) },
1437 { "edi", offsetof(CPUState, regs[7]) },
1438 #ifdef TARGET_X86_64
1439 { "r8", offsetof(CPUState, regs[8]) },
1440 { "r9", offsetof(CPUState, regs[9]) },
1441 { "r10", offsetof(CPUState, regs[10]) },
1442 { "r11", offsetof(CPUState, regs[11]) },
1443 { "r12", offsetof(CPUState, regs[12]) },
1444 { "r13", offsetof(CPUState, regs[13]) },
1445 { "r14", offsetof(CPUState, regs[14]) },
1446 { "r15", offsetof(CPUState, regs[15]) },
1448 { "eflags", offsetof(CPUState, eflags) },
1449 { "eip", offsetof(CPUState, eip) },
1456 { "pc", 0, monitor_get_pc, },
1457 #elif defined(TARGET_PPC)
1458 { "r0", offsetof(CPUState, gpr[0]) },
1459 { "r1", offsetof(CPUState, gpr[1]) },
1460 { "r2", offsetof(CPUState, gpr[2]) },
1461 { "r3", offsetof(CPUState, gpr[3]) },
1462 { "r4", offsetof(CPUState, gpr[4]) },
1463 { "r5", offsetof(CPUState, gpr[5]) },
1464 { "r6", offsetof(CPUState, gpr[6]) },
1465 { "r7", offsetof(CPUState, gpr[7]) },
1466 { "r8", offsetof(CPUState, gpr[8]) },
1467 { "r9", offsetof(CPUState, gpr[9]) },
1468 { "r10", offsetof(CPUState, gpr[10]) },
1469 { "r11", offsetof(CPUState, gpr[11]) },
1470 { "r12", offsetof(CPUState, gpr[12]) },
1471 { "r13", offsetof(CPUState, gpr[13]) },
1472 { "r14", offsetof(CPUState, gpr[14]) },
1473 { "r15", offsetof(CPUState, gpr[15]) },
1474 { "r16", offsetof(CPUState, gpr[16]) },
1475 { "r17", offsetof(CPUState, gpr[17]) },
1476 { "r18", offsetof(CPUState, gpr[18]) },
1477 { "r19", offsetof(CPUState, gpr[19]) },
1478 { "r20", offsetof(CPUState, gpr[20]) },
1479 { "r21", offsetof(CPUState, gpr[21]) },
1480 { "r22", offsetof(CPUState, gpr[22]) },
1481 { "r23", offsetof(CPUState, gpr[23]) },
1482 { "r24", offsetof(CPUState, gpr[24]) },
1483 { "r25", offsetof(CPUState, gpr[25]) },
1484 { "r26", offsetof(CPUState, gpr[26]) },
1485 { "r27", offsetof(CPUState, gpr[27]) },
1486 { "r28", offsetof(CPUState, gpr[28]) },
1487 { "r29", offsetof(CPUState, gpr[29]) },
1488 { "r30", offsetof(CPUState, gpr[30]) },
1489 { "r31", offsetof(CPUState, gpr[31]) },
1490 { "nip|pc", offsetof(CPUState, nip) },
1491 { "lr", offsetof(CPUState, lr) },
1492 { "ctr", offsetof(CPUState, ctr) },
1493 { "decr", 0, &monitor_get_decr, },
1494 { "ccr", 0, &monitor_get_ccr, },
1495 { "msr", 0, &monitor_get_msr, },
1496 { "xer", 0, &monitor_get_xer, },
1497 { "tbu", 0, &monitor_get_tbu, },
1498 { "tbl", 0, &monitor_get_tbl, },
1499 { "sdr1", offsetof(CPUState, sdr1) },
1500 { "sr0", offsetof(CPUState, sr[0]) },
1501 { "sr1", offsetof(CPUState, sr[1]) },
1502 { "sr2", offsetof(CPUState, sr[2]) },
1503 { "sr3", offsetof(CPUState, sr[3]) },
1504 { "sr4", offsetof(CPUState, sr[4]) },
1505 { "sr5", offsetof(CPUState, sr[5]) },
1506 { "sr6", offsetof(CPUState, sr[6]) },
1507 { "sr7", offsetof(CPUState, sr[7]) },
1508 { "sr8", offsetof(CPUState, sr[8]) },
1509 { "sr9", offsetof(CPUState, sr[9]) },
1510 { "sr10", offsetof(CPUState, sr[10]) },
1511 { "sr11", offsetof(CPUState, sr[11]) },
1512 { "sr12", offsetof(CPUState, sr[12]) },
1513 { "sr13", offsetof(CPUState, sr[13]) },
1514 { "sr14", offsetof(CPUState, sr[14]) },
1515 { "sr15", offsetof(CPUState, sr[15]) },
1516 /* Too lazy to put BATs and SPRs ... */
1517 #elif defined(TARGET_SPARC)
1518 { "g0", offsetof(CPUState, gregs[0]) },
1519 { "g1", offsetof(CPUState, gregs[1]) },
1520 { "g2", offsetof(CPUState, gregs[2]) },
1521 { "g3", offsetof(CPUState, gregs[3]) },
1522 { "g4", offsetof(CPUState, gregs[4]) },
1523 { "g5", offsetof(CPUState, gregs[5]) },
1524 { "g6", offsetof(CPUState, gregs[6]) },
1525 { "g7", offsetof(CPUState, gregs[7]) },
1526 { "o0", 0, monitor_get_reg },
1527 { "o1", 1, monitor_get_reg },
1528 { "o2", 2, monitor_get_reg },
1529 { "o3", 3, monitor_get_reg },
1530 { "o4", 4, monitor_get_reg },
1531 { "o5", 5, monitor_get_reg },
1532 { "o6", 6, monitor_get_reg },
1533 { "o7", 7, monitor_get_reg },
1534 { "l0", 8, monitor_get_reg },
1535 { "l1", 9, monitor_get_reg },
1536 { "l2", 10, monitor_get_reg },
1537 { "l3", 11, monitor_get_reg },
1538 { "l4", 12, monitor_get_reg },
1539 { "l5", 13, monitor_get_reg },
1540 { "l6", 14, monitor_get_reg },
1541 { "l7", 15, monitor_get_reg },
1542 { "i0", 16, monitor_get_reg },
1543 { "i1", 17, monitor_get_reg },
1544 { "i2", 18, monitor_get_reg },
1545 { "i3", 19, monitor_get_reg },
1546 { "i4", 20, monitor_get_reg },
1547 { "i5", 21, monitor_get_reg },
1548 { "i6", 22, monitor_get_reg },
1549 { "i7", 23, monitor_get_reg },
1550 { "pc", offsetof(CPUState, pc) },
1551 { "npc", offsetof(CPUState, npc) },
1552 { "y", offsetof(CPUState, y) },
1553 #ifndef TARGET_SPARC64
1554 { "psr", 0, &monitor_get_psr, },
1555 { "wim", offsetof(CPUState, wim) },
1557 { "tbr", offsetof(CPUState, tbr) },
1558 { "fsr", offsetof(CPUState, fsr) },
1559 { "f0", offsetof(CPUState, fpr[0]) },
1560 { "f1", offsetof(CPUState, fpr[1]) },
1561 { "f2", offsetof(CPUState, fpr[2]) },
1562 { "f3", offsetof(CPUState, fpr[3]) },
1563 { "f4", offsetof(CPUState, fpr[4]) },
1564 { "f5", offsetof(CPUState, fpr[5]) },
1565 { "f6", offsetof(CPUState, fpr[6]) },
1566 { "f7", offsetof(CPUState, fpr[7]) },
1567 { "f8", offsetof(CPUState, fpr[8]) },
1568 { "f9", offsetof(CPUState, fpr[9]) },
1569 { "f10", offsetof(CPUState, fpr[10]) },
1570 { "f11", offsetof(CPUState, fpr[11]) },
1571 { "f12", offsetof(CPUState, fpr[12]) },
1572 { "f13", offsetof(CPUState, fpr[13]) },
1573 { "f14", offsetof(CPUState, fpr[14]) },
1574 { "f15", offsetof(CPUState, fpr[15]) },
1575 { "f16", offsetof(CPUState, fpr[16]) },
1576 { "f17", offsetof(CPUState, fpr[17]) },
1577 { "f18", offsetof(CPUState, fpr[18]) },
1578 { "f19", offsetof(CPUState, fpr[19]) },
1579 { "f20", offsetof(CPUState, fpr[20]) },
1580 { "f21", offsetof(CPUState, fpr[21]) },
1581 { "f22", offsetof(CPUState, fpr[22]) },
1582 { "f23", offsetof(CPUState, fpr[23]) },
1583 { "f24", offsetof(CPUState, fpr[24]) },
1584 { "f25", offsetof(CPUState, fpr[25]) },
1585 { "f26", offsetof(CPUState, fpr[26]) },
1586 { "f27", offsetof(CPUState, fpr[27]) },
1587 { "f28", offsetof(CPUState, fpr[28]) },
1588 { "f29", offsetof(CPUState, fpr[29]) },
1589 { "f30", offsetof(CPUState, fpr[30]) },
1590 { "f31", offsetof(CPUState, fpr[31]) },
1591 #ifdef TARGET_SPARC64
1592 { "f32", offsetof(CPUState, fpr[32]) },
1593 { "f34", offsetof(CPUState, fpr[34]) },
1594 { "f36", offsetof(CPUState, fpr[36]) },
1595 { "f38", offsetof(CPUState, fpr[38]) },
1596 { "f40", offsetof(CPUState, fpr[40]) },
1597 { "f42", offsetof(CPUState, fpr[42]) },
1598 { "f44", offsetof(CPUState, fpr[44]) },
1599 { "f46", offsetof(CPUState, fpr[46]) },
1600 { "f48", offsetof(CPUState, fpr[48]) },
1601 { "f50", offsetof(CPUState, fpr[50]) },
1602 { "f52", offsetof(CPUState, fpr[52]) },
1603 { "f54", offsetof(CPUState, fpr[54]) },
1604 { "f56", offsetof(CPUState, fpr[56]) },
1605 { "f58", offsetof(CPUState, fpr[58]) },
1606 { "f60", offsetof(CPUState, fpr[60]) },
1607 { "f62", offsetof(CPUState, fpr[62]) },
1608 { "asi", offsetof(CPUState, asi) },
1609 { "pstate", offsetof(CPUState, pstate) },
1610 { "cansave", offsetof(CPUState, cansave) },
1611 { "canrestore", offsetof(CPUState, canrestore) },
1612 { "otherwin", offsetof(CPUState, otherwin) },
1613 { "wstate", offsetof(CPUState, wstate) },
1614 { "cleanwin", offsetof(CPUState, cleanwin) },
1615 { "fprs", offsetof(CPUState, fprs) },
1621 static void expr_error(const char *fmt)
1625 longjmp(expr_env, 1);
1628 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1629 static int get_monitor_def(target_long *pval, const char *name)
1634 for(md = monitor_defs; md->name != NULL; md++) {
1635 if (compare_cmd(name, md->name)) {
1636 if (md->get_value) {
1637 *pval = md->get_value(md, md->offset);
1639 CPUState *env = mon_get_cpu();
1642 ptr = (uint8_t *)env + md->offset;
1645 *pval = *(int32_t *)ptr;
1648 *pval = *(target_long *)ptr;
1661 static void next(void)
1665 while (isspace(*pch))
1670 static target_long expr_sum(void);
1672 static target_long expr_unary(void)
1695 expr_error("')' expected");
1702 expr_error("character constant expected");
1706 expr_error("missing terminating \' character");
1715 while ((*pch >= 'a' && *pch <= 'z') ||
1716 (*pch >= 'A' && *pch <= 'Z') ||
1717 (*pch >= '0' && *pch <= '9') ||
1718 *pch == '_' || *pch == '.') {
1719 if ((q - buf) < sizeof(buf) - 1)
1723 while (isspace(*pch))
1726 ret = get_monitor_def(&n, buf);
1728 expr_error("unknown register");
1730 expr_error("no cpu defined");
1734 expr_error("unexpected end of expression");
1738 #if TARGET_LONG_BITS == 64
1739 n = strtoull(pch, &p, 0);
1741 n = strtoul(pch, &p, 0);
1744 expr_error("invalid char in expression");
1747 while (isspace(*pch))
1755 static target_long expr_prod(void)
1757 target_long val, val2;
1763 if (op != '*' && op != '/' && op != '%')
1766 val2 = expr_unary();
1775 expr_error("division by zero");
1786 static target_long expr_logic(void)
1788 target_long val, val2;
1794 if (op != '&' && op != '|' && op != '^')
1814 static target_long expr_sum(void)
1816 target_long val, val2;
1822 if (op != '+' && op != '-')
1825 val2 = expr_logic();
1834 static int get_expr(target_long *pval, const char **pp)
1837 if (setjmp(expr_env)) {
1841 while (isspace(*pch))
1848 static int get_str(char *buf, int buf_size, const char **pp)
1866 while (*p != '\0' && *p != '\"') {
1882 qemu_printf("unsupported escape code: '\\%c'\n", c);
1885 if ((q - buf) < buf_size - 1) {
1889 if ((q - buf) < buf_size - 1) {
1896 qemu_printf("unterminated string\n");
1901 while (*p != '\0' && !isspace(*p)) {
1902 if ((q - buf) < buf_size - 1) {
1913 static int default_fmt_format = 'x';
1914 static int default_fmt_size = 4;
1918 static void monitor_handle_command(const char *cmdline)
1920 const char *p, *pstart, *typestr;
1922 int c, nb_args, len, i, has_arg;
1926 void *str_allocated[MAX_ARGS];
1927 void *args[MAX_ARGS];
1930 term_printf("command='%s'\n", cmdline);
1933 /* extract the command name */
1941 while (*p != '\0' && *p != '/' && !isspace(*p))
1944 if (len > sizeof(cmdname) - 1)
1945 len = sizeof(cmdname) - 1;
1946 memcpy(cmdname, pstart, len);
1947 cmdname[len] = '\0';
1949 /* find the command */
1950 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1951 if (compare_cmd(cmdname, cmd->name))
1954 term_printf("unknown command: '%s'\n", cmdname);
1958 for(i = 0; i < MAX_ARGS; i++)
1959 str_allocated[i] = NULL;
1961 /* parse the parameters */
1962 typestr = cmd->args_type;
1979 if (*typestr == '?') {
1982 /* no optional string: NULL argument */
1987 ret = get_str(buf, sizeof(buf), &p);
1991 term_printf("%s: filename expected\n", cmdname);
1994 term_printf("%s: block device name expected\n", cmdname);
1997 term_printf("%s: string expected\n", cmdname);
2002 str = qemu_malloc(strlen(buf) + 1);
2004 str_allocated[nb_args] = str;
2006 if (nb_args >= MAX_ARGS) {
2008 term_printf("%s: too many arguments\n", cmdname);
2011 args[nb_args++] = str;
2016 int count, format, size;
2026 while (isdigit(*p)) {
2027 count = count * 10 + (*p - '0');
2065 if (*p != '\0' && !isspace(*p)) {
2066 term_printf("invalid char in format: '%c'\n", *p);
2070 format = default_fmt_format;
2071 if (format != 'i') {
2072 /* for 'i', not specifying a size gives -1 as size */
2074 size = default_fmt_size;
2076 default_fmt_size = size;
2077 default_fmt_format = format;
2080 format = default_fmt_format;
2081 if (format != 'i') {
2082 size = default_fmt_size;
2087 if (nb_args + 3 > MAX_ARGS)
2089 args[nb_args++] = (void*)count;
2090 args[nb_args++] = (void*)format;
2091 args[nb_args++] = (void*)size;
2100 if (*typestr == '?' || *typestr == '.') {
2101 if (*typestr == '?') {
2117 if (nb_args >= MAX_ARGS)
2119 args[nb_args++] = (void *)has_arg;
2121 if (nb_args >= MAX_ARGS)
2127 if (get_expr(&val, &p))
2131 if (nb_args >= MAX_ARGS)
2133 args[nb_args++] = (void *)(int)val;
2135 if ((nb_args + 1) >= MAX_ARGS)
2137 #if TARGET_LONG_BITS == 64
2138 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2140 args[nb_args++] = (void *)0;
2142 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2160 term_printf("%s: unsupported option -%c\n",
2167 if (nb_args >= MAX_ARGS)
2169 args[nb_args++] = (void *)has_option;
2174 term_printf("%s: unknown type '%c'\n", cmdname, c);
2178 /* check that all arguments were parsed */
2182 term_printf("%s: extraneous characters at the end of line\n",
2192 cmd->handler(args[0]);
2195 cmd->handler(args[0], args[1]);
2198 cmd->handler(args[0], args[1], args[2]);
2201 cmd->handler(args[0], args[1], args[2], args[3]);
2204 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2207 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2210 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2213 term_printf("unsupported number of arguments: %d\n", nb_args);
2217 for(i = 0; i < MAX_ARGS; i++)
2218 qemu_free(str_allocated[i]);
2222 static void cmd_completion(const char *name, const char *list)
2224 const char *p, *pstart;
2233 p = pstart + strlen(pstart);
2235 if (len > sizeof(cmd) - 2)
2236 len = sizeof(cmd) - 2;
2237 memcpy(cmd, pstart, len);
2239 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2240 add_completion(cmd);
2248 static void file_completion(const char *input)
2253 char file[1024], file_prefix[1024];
2257 p = strrchr(input, '/');
2260 pstrcpy(file_prefix, sizeof(file_prefix), input);
2263 input_path_len = p - input + 1;
2264 memcpy(path, input, input_path_len);
2265 if (input_path_len > sizeof(path) - 1)
2266 input_path_len = sizeof(path) - 1;
2267 path[input_path_len] = '\0';
2268 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2270 #ifdef DEBUG_COMPLETION
2271 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2273 ffs = opendir(path);
2281 if (strstart(d->d_name, file_prefix, NULL)) {
2282 memcpy(file, input, input_path_len);
2283 strcpy(file + input_path_len, d->d_name);
2284 /* stat the file to find out if it's a directory.
2285 * In that case add a slash to speed up typing long paths
2288 if(S_ISDIR(sb.st_mode))
2290 add_completion(file);
2296 static void block_completion_it(void *opaque, const char *name)
2298 const char *input = opaque;
2300 if (input[0] == '\0' ||
2301 !strncmp(name, (char *)input, strlen(input))) {
2302 add_completion(name);
2306 /* NOTE: this parser is an approximate form of the real command parser */
2307 static void parse_cmdline(const char *cmdline,
2308 int *pnb_args, char **args)
2321 if (nb_args >= MAX_ARGS)
2323 ret = get_str(buf, sizeof(buf), &p);
2324 args[nb_args] = qemu_strdup(buf);
2329 *pnb_args = nb_args;
2332 void readline_find_completion(const char *cmdline)
2334 const char *cmdname;
2335 char *args[MAX_ARGS];
2336 int nb_args, i, len;
2337 const char *ptype, *str;
2341 parse_cmdline(cmdline, &nb_args, args);
2342 #ifdef DEBUG_COMPLETION
2343 for(i = 0; i < nb_args; i++) {
2344 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2348 /* if the line ends with a space, it means we want to complete the
2350 len = strlen(cmdline);
2351 if (len > 0 && isspace(cmdline[len - 1])) {
2352 if (nb_args >= MAX_ARGS)
2354 args[nb_args++] = qemu_strdup("");
2357 /* command completion */
2362 completion_index = strlen(cmdname);
2363 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2364 cmd_completion(cmdname, cmd->name);
2367 /* find the command */
2368 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2369 if (compare_cmd(args[0], cmd->name))
2374 ptype = cmd->args_type;
2375 for(i = 0; i < nb_args - 2; i++) {
2376 if (*ptype != '\0') {
2378 while (*ptype == '?')
2382 str = args[nb_args - 1];
2385 /* file completion */
2386 completion_index = strlen(str);
2387 file_completion(str);
2390 /* block device name completion */
2391 completion_index = strlen(str);
2392 bdrv_iterate(block_completion_it, (void *)str);
2395 /* XXX: more generic ? */
2396 if (!strcmp(cmd->name, "info")) {
2397 completion_index = strlen(str);
2398 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2399 cmd_completion(str, cmd->name);
2401 } else if (!strcmp(cmd->name, "sendkey")) {
2402 completion_index = strlen(str);
2403 for(key = key_defs; key->name != NULL; key++) {
2404 cmd_completion(str, key->name);
2412 for(i = 0; i < nb_args; i++)
2416 static int term_can_read(void *opaque)
2421 static void term_read(void *opaque, const uint8_t *buf, int size)
2424 for(i = 0; i < size; i++)
2425 readline_handle_byte(buf[i]);
2428 static void monitor_start_input(void);
2430 static void monitor_handle_command1(void *opaque, const char *cmdline)
2432 monitor_handle_command(cmdline);
2433 monitor_start_input();
2436 static void monitor_start_input(void)
2438 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2441 void monitor_init(CharDriverState *hd, int show_banner)
2445 term_printf("QEMU %s monitor - type 'help' for more information\n",
2448 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2449 monitor_start_input();
2452 /* XXX: use threads ? */
2453 /* modal monitor readline */
2454 static int monitor_readline_started;
2455 static char *monitor_readline_buf;
2456 static int monitor_readline_buf_size;
2458 static void monitor_readline_cb(void *opaque, const char *input)
2460 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2461 monitor_readline_started = 0;
2464 void monitor_readline(const char *prompt, int is_password,
2465 char *buf, int buf_size)
2468 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2470 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2471 monitor_readline_buf = buf;
2472 monitor_readline_buf_size = buf_size;
2473 monitor_readline_started = 1;
2474 while (monitor_readline_started) {