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
26 #include "hw/pcmcia.h"
31 #include "qemu-char.h"
35 #include "audio/audio.h"
40 //#define DEBUG_COMPLETION
43 #define offsetof(type, field) ((size_t) &((type *)0)->field)
50 * 'B' block device name
51 * 's' string (accept optional quote)
53 * 'l' target long (32 or 64 bit)
54 * '/' optional gdb-like print format (like "/10x")
56 * '?' optional type (for 'F', 's' and 'i')
60 typedef struct term_cmd_t {
62 const char *args_type;
69 static CharDriverState *monitor_hd[MAX_MON];
70 static int hide_banner;
72 static term_cmd_t term_cmds[];
73 static term_cmd_t info_cmds[];
75 static char term_outbuf[1024];
76 static int term_outbuf_index;
78 static void monitor_start_input(void);
80 CPUState *mon_cpu = NULL;
85 if (term_outbuf_index > 0) {
86 for (i = 0; i < MAX_MON; i++)
87 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
88 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
89 term_outbuf_index = 0;
93 /* flush at every end of line or if the buffer is full */
94 void term_puts(const char *str)
102 term_outbuf[term_outbuf_index++] = '\r';
103 term_outbuf[term_outbuf_index++] = c;
104 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
110 void term_vprintf(const char *fmt, va_list ap)
113 vsnprintf(buf, sizeof(buf), fmt, ap);
117 void term_printf(const char *fmt, ...)
121 term_vprintf(fmt, ap);
125 void term_print_filename(const char *filename)
129 for (i = 0; filename[i]; i++) {
130 switch (filename[i]) {
134 term_printf("\\%c", filename[i]);
146 term_printf("%c", filename[i]);
152 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
156 term_vprintf(fmt, ap);
161 static int compare_cmd(const char *name, const char *list)
163 const char *p, *pstart;
171 p = pstart + strlen(pstart);
172 if ((p - pstart) == len && !memcmp(pstart, name, len))
181 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
185 for(cmd = cmds; cmd->name != NULL; cmd++) {
186 if (!name || !strcmp(name, cmd->name))
187 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
191 static void help_cmd(const char *name)
193 if (name && !strcmp(name, "info")) {
194 help_cmd1(info_cmds, "info ", NULL);
196 help_cmd1(term_cmds, "", name);
197 if (name && !strcmp(name, "log")) {
199 term_printf("Log items (comma separated):\n");
200 term_printf("%-10s %s\n", "none", "remove all logs");
201 for(item = cpu_log_items; item->mask != 0; item++) {
202 term_printf("%-10s %s\n", item->name, item->help);
208 static void do_help(const char *name)
213 static void do_commit(const char *device)
217 all_devices = !strcmp(device, "all");
218 for (i = 0; i < nb_drives; i++) {
220 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
221 bdrv_commit(drives_table[i].bdrv);
225 static void do_info(const char *item)
231 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
232 if (compare_cmd(item, cmd->name))
242 static void do_info_version(void)
244 term_printf("%s\n", QEMU_VERSION);
247 static void do_info_name(void)
250 term_printf("%s\n", qemu_name);
253 static void do_info_block(void)
258 /* get the current CPU defined by the user */
259 static int mon_set_cpu(int cpu_index)
263 for(env = first_cpu; env != NULL; env = env->next_cpu) {
264 if (env->cpu_index == cpu_index) {
272 static CPUState *mon_get_cpu(void)
280 static void do_info_registers(void)
287 cpu_dump_state(env, NULL, monitor_fprintf,
290 cpu_dump_state(env, NULL, monitor_fprintf,
295 static void do_info_cpus(void)
299 /* just to set the default cpu if not already done */
302 for(env = first_cpu; env != NULL; env = env->next_cpu) {
303 term_printf("%c CPU #%d:",
304 (env == mon_cpu) ? '*' : ' ',
306 #if defined(TARGET_I386)
307 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
308 if (env->hflags & HF_HALTED_MASK)
309 term_printf(" (halted)");
310 #elif defined(TARGET_PPC)
311 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
313 term_printf(" (halted)");
314 #elif defined(TARGET_SPARC)
315 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
317 term_printf(" (halted)");
318 #elif defined(TARGET_MIPS)
319 term_printf(" PC=0x" TARGET_FMT_lx, env->PC[env->current_tc]);
321 term_printf(" (halted)");
327 static void do_cpu_set(int index)
329 if (mon_set_cpu(index) < 0)
330 term_printf("Invalid CPU index\n");
333 static void do_info_jit(void)
335 dump_exec_info(NULL, monitor_fprintf);
338 static void do_info_history (void)
345 str = readline_get_history(i);
348 term_printf("%d: '%s'\n", i, str);
353 #if defined(TARGET_PPC)
354 /* XXX: not implemented in other targets */
355 static void do_info_cpu_stats (void)
360 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
364 static void do_quit(void)
369 static int eject_device(BlockDriverState *bs, int force)
371 if (bdrv_is_inserted(bs)) {
373 if (!bdrv_is_removable(bs)) {
374 term_printf("device is not removable\n");
377 if (bdrv_is_locked(bs)) {
378 term_printf("device is locked\n");
387 static void do_eject(int force, const char *filename)
389 BlockDriverState *bs;
391 bs = bdrv_find(filename);
393 term_printf("device not found\n");
396 eject_device(bs, force);
399 static void do_change_block(const char *device, const char *filename)
401 BlockDriverState *bs;
403 bs = bdrv_find(device);
405 term_printf("device not found\n");
408 if (eject_device(bs, 0) < 0)
410 bdrv_open(bs, filename, 0);
411 qemu_key_check(bs, filename);
414 static void do_change_vnc(const char *target)
416 if (strcmp(target, "passwd") == 0 ||
417 strcmp(target, "password") == 0) {
419 monitor_readline("Password: ", 1, password, sizeof(password)-1);
420 password[sizeof(password)-1] = '\0';
421 if (vnc_display_password(NULL, password) < 0)
422 term_printf("could not set VNC server password\n");
424 if (vnc_display_open(NULL, target) < 0)
425 term_printf("could not start VNC server on %s\n", target);
429 static void do_change(const char *device, const char *target)
431 if (strcmp(device, "vnc") == 0) {
432 do_change_vnc(target);
434 do_change_block(device, target);
438 static void do_screen_dump(const char *filename)
440 vga_hw_screen_dump(filename);
443 static void do_logfile(const char *filename)
445 cpu_set_log_filename(filename);
448 static void do_log(const char *items)
452 if (!strcmp(items, "none")) {
455 mask = cpu_str_to_log_mask(items);
464 static void do_stop(void)
466 vm_stop(EXCP_INTERRUPT);
469 static void do_cont(void)
474 #ifdef CONFIG_GDBSTUB
475 static void do_gdbserver(const char *port)
478 port = DEFAULT_GDBSTUB_PORT;
479 if (gdbserver_start(port) < 0) {
480 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
482 qemu_printf("Waiting gdb connection on port '%s'\n", port);
487 static void term_printc(int c)
504 if (c >= 32 && c <= 126) {
505 term_printf("%c", c);
507 term_printf("\\x%02x", c);
514 static void memory_dump(int count, int format, int wsize,
515 target_phys_addr_t addr, int is_physical)
518 int nb_per_line, l, line_size, i, max_digits, len;
526 if (!env && !is_physical)
531 } else if (wsize == 4) {
534 /* as default we use the current CS size */
538 if ((env->efer & MSR_EFER_LMA) &&
539 (env->segs[R_CS].flags & DESC_L_MASK))
543 if (!(env->segs[R_CS].flags & DESC_B_MASK))
548 monitor_disas(env, addr, count, is_physical, flags);
557 nb_per_line = line_size / wsize;
562 max_digits = (wsize * 8 + 2) / 3;
566 max_digits = (wsize * 8) / 4;
570 max_digits = (wsize * 8 * 10 + 32) / 33;
579 term_printf(TARGET_FMT_plx ":", addr);
581 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
586 cpu_physical_memory_rw(addr, buf, l, 0);
591 cpu_memory_rw_debug(env, addr, buf, l, 0);
598 v = ldub_raw(buf + i);
601 v = lduw_raw(buf + i);
604 v = (uint32_t)ldl_raw(buf + i);
607 v = ldq_raw(buf + i);
613 term_printf("%#*" PRIo64, max_digits, v);
616 term_printf("0x%0*" PRIx64, max_digits, v);
619 term_printf("%*" PRIu64, max_digits, v);
622 term_printf("%*" PRId64, max_digits, v);
636 #if TARGET_LONG_BITS == 64
637 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
639 #define GET_TLONG(h, l) (l)
642 static void do_memory_dump(int count, int format, int size,
643 uint32_t addrh, uint32_t addrl)
645 target_long addr = GET_TLONG(addrh, addrl);
646 memory_dump(count, format, size, addr, 0);
649 #if TARGET_PHYS_ADDR_BITS > 32
650 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
652 #define GET_TPHYSADDR(h, l) (l)
655 static void do_physical_memory_dump(int count, int format, int size,
656 uint32_t addrh, uint32_t addrl)
659 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
660 memory_dump(count, format, size, addr, 1);
663 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
665 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
666 #if TARGET_PHYS_ADDR_BITS == 32
669 term_printf("%#o", val);
672 term_printf("%#x", val);
675 term_printf("%u", val);
679 term_printf("%d", val);
688 term_printf("%#" PRIo64, val);
691 term_printf("%#" PRIx64, val);
694 term_printf("%" PRIu64, val);
698 term_printf("%" PRId64, val);
708 static void do_memory_save(unsigned int valh, unsigned int vall,
709 uint32_t size, const char *filename)
712 target_long addr = GET_TLONG(valh, vall);
721 f = fopen(filename, "wb");
723 term_printf("could not open '%s'\n", filename);
730 cpu_memory_rw_debug(env, addr, buf, l, 0);
731 fwrite(buf, 1, l, f);
738 static void do_sum(uint32_t start, uint32_t size)
745 for(addr = start; addr < (start + size); addr++) {
746 cpu_physical_memory_rw(addr, buf, 1, 0);
747 /* BSD sum algorithm ('sum' Unix command) */
748 sum = (sum >> 1) | (sum << 15);
751 term_printf("%05d\n", sum);
759 static const KeyDef key_defs[] = {
784 { 0x0e, "backspace" },
819 { 0x3a, "caps_lock" },
830 { 0x45, "num_lock" },
831 { 0x46, "scroll_lock" },
833 { 0xb5, "kp_divide" },
834 { 0x37, "kp_multiply" },
835 { 0x4a, "kp_subtract" },
837 { 0x9c, "kp_enter" },
838 { 0x53, "kp_decimal" },
873 static int get_keycode(const char *key)
879 for(p = key_defs; p->name != NULL; p++) {
880 if (!strcmp(key, p->name))
883 if (strstart(key, "0x", NULL)) {
884 ret = strtoul(key, &endp, 0);
885 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
891 static void do_send_key(const char *string)
894 uint8_t keycodes[16];
896 int nb_keycodes, keycode, i;
902 while (*p != '\0' && *p != '-') {
903 if ((q - keybuf) < sizeof(keybuf) - 1) {
909 keycode = get_keycode(keybuf);
911 term_printf("unknown key: '%s'\n", keybuf);
914 keycodes[nb_keycodes++] = keycode;
919 /* key down events */
920 for(i = 0; i < nb_keycodes; i++) {
921 keycode = keycodes[i];
923 kbd_put_keycode(0xe0);
924 kbd_put_keycode(keycode & 0x7f);
927 for(i = nb_keycodes - 1; i >= 0; i--) {
928 keycode = keycodes[i];
930 kbd_put_keycode(0xe0);
931 kbd_put_keycode(keycode | 0x80);
935 static int mouse_button_state;
937 static void do_mouse_move(const char *dx_str, const char *dy_str,
941 dx = strtol(dx_str, NULL, 0);
942 dy = strtol(dy_str, NULL, 0);
945 dz = strtol(dz_str, NULL, 0);
946 kbd_mouse_event(dx, dy, dz, mouse_button_state);
949 static void do_mouse_button(int button_state)
951 mouse_button_state = button_state;
952 kbd_mouse_event(0, 0, 0, mouse_button_state);
955 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
961 cpu_outb(NULL, addr & 0xffff, index & 0xff);
969 val = cpu_inb(NULL, addr);
973 val = cpu_inw(NULL, addr);
977 val = cpu_inl(NULL, addr);
981 term_printf("port%c[0x%04x] = %#0*x\n",
982 suffix, addr, size * 2, val);
985 static void do_system_reset(void)
987 qemu_system_reset_request();
990 static void do_system_powerdown(void)
992 qemu_system_powerdown_request();
995 #if defined(TARGET_I386)
996 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
998 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1001 pte & PG_GLOBAL_MASK ? 'G' : '-',
1002 pte & PG_PSE_MASK ? 'P' : '-',
1003 pte & PG_DIRTY_MASK ? 'D' : '-',
1004 pte & PG_ACCESSED_MASK ? 'A' : '-',
1005 pte & PG_PCD_MASK ? 'C' : '-',
1006 pte & PG_PWT_MASK ? 'T' : '-',
1007 pte & PG_USER_MASK ? 'U' : '-',
1008 pte & PG_RW_MASK ? 'W' : '-');
1011 static void tlb_info(void)
1015 uint32_t pgd, pde, pte;
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;
1026 for(l1 = 0; l1 < 1024; l1++) {
1027 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1028 pde = le32_to_cpu(pde);
1029 if (pde & PG_PRESENT_MASK) {
1030 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1031 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
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 if (pte & PG_PRESENT_MASK) {
1038 print_pte((l1 << 22) + (l2 << 12),
1048 static void mem_print(uint32_t *pstart, int *plast_prot,
1049 uint32_t end, int prot)
1052 prot1 = *plast_prot;
1053 if (prot != prot1) {
1054 if (*pstart != -1) {
1055 term_printf("%08x-%08x %08x %c%c%c\n",
1056 *pstart, end, end - *pstart,
1057 prot1 & PG_USER_MASK ? 'u' : '-',
1059 prot1 & PG_RW_MASK ? 'w' : '-');
1069 static void mem_info(void)
1072 int l1, l2, prot, last_prot;
1073 uint32_t pgd, pde, pte, start, end;
1075 env = mon_get_cpu();
1079 if (!(env->cr[0] & CR0_PG_MASK)) {
1080 term_printf("PG disabled\n");
1083 pgd = env->cr[3] & ~0xfff;
1086 for(l1 = 0; l1 < 1024; l1++) {
1087 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1088 pde = le32_to_cpu(pde);
1090 if (pde & PG_PRESENT_MASK) {
1091 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1092 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1093 mem_print(&start, &last_prot, end, prot);
1095 for(l2 = 0; l2 < 1024; l2++) {
1096 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1097 (uint8_t *)&pte, 4);
1098 pte = le32_to_cpu(pte);
1099 end = (l1 << 22) + (l2 << 12);
1100 if (pte & PG_PRESENT_MASK) {
1101 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1105 mem_print(&start, &last_prot, end, prot);
1110 mem_print(&start, &last_prot, end, prot);
1116 static void do_info_kqemu(void)
1122 env = mon_get_cpu();
1124 term_printf("No cpu initialized yet");
1127 val = env->kqemu_enabled;
1128 term_printf("kqemu support: ");
1132 term_printf("disabled\n");
1135 term_printf("enabled for user code\n");
1138 term_printf("enabled for user and kernel code\n");
1142 term_printf("kqemu support: not compiled\n");
1146 #ifdef CONFIG_PROFILER
1150 int64_t kqemu_exec_count;
1152 int64_t kqemu_ret_int_count;
1153 int64_t kqemu_ret_excp_count;
1154 int64_t kqemu_ret_intr_count;
1156 static void do_info_profile(void)
1162 term_printf("async time %" PRId64 " (%0.3f)\n",
1163 dev_time, dev_time / (double)ticks_per_sec);
1164 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1165 qemu_time, qemu_time / (double)ticks_per_sec);
1166 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1167 kqemu_time, kqemu_time / (double)ticks_per_sec,
1168 kqemu_time / (double)total * 100.0,
1170 kqemu_ret_int_count,
1171 kqemu_ret_excp_count,
1172 kqemu_ret_intr_count);
1175 kqemu_exec_count = 0;
1177 kqemu_ret_int_count = 0;
1178 kqemu_ret_excp_count = 0;
1179 kqemu_ret_intr_count = 0;
1181 kqemu_record_dump();
1185 static void do_info_profile(void)
1187 term_printf("Internal profiler not compiled\n");
1191 /* Capture support */
1192 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1194 static void do_info_capture (void)
1199 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1200 term_printf ("[%d]: ", i);
1201 s->ops.info (s->opaque);
1205 static void do_stop_capture (int n)
1210 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1212 s->ops.destroy (s->opaque);
1213 LIST_REMOVE (s, entries);
1221 int wav_start_capture (CaptureState *s, const char *path, int freq,
1222 int bits, int nchannels);
1224 static void do_wav_capture (const char *path,
1225 int has_freq, int freq,
1226 int has_bits, int bits,
1227 int has_channels, int nchannels)
1231 s = qemu_mallocz (sizeof (*s));
1233 term_printf ("Not enough memory to add wave capture\n");
1237 freq = has_freq ? freq : 44100;
1238 bits = has_bits ? bits : 16;
1239 nchannels = has_channels ? nchannels : 2;
1241 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1242 term_printf ("Faied to add wave capture\n");
1245 LIST_INSERT_HEAD (&capture_head, s, entries);
1249 static term_cmd_t term_cmds[] = {
1250 { "help|?", "s?", do_help,
1251 "[cmd]", "show the help" },
1252 { "commit", "s", do_commit,
1253 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1254 { "info", "s?", do_info,
1255 "subcommand", "show various information about the system state" },
1256 { "q|quit", "", do_quit,
1257 "", "quit the emulator" },
1258 { "eject", "-fB", do_eject,
1259 "[-f] device", "eject a removable medium (use -f to force it)" },
1260 { "change", "BF", do_change,
1261 "device filename", "change a removable medium" },
1262 { "screendump", "F", do_screen_dump,
1263 "filename", "save screen into PPM image 'filename'" },
1264 { "logfile", "s", do_logfile,
1265 "filename", "output logs to 'filename'" },
1266 { "log", "s", do_log,
1267 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1268 { "savevm", "s?", do_savevm,
1269 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1270 { "loadvm", "s", do_loadvm,
1271 "tag|id", "restore a VM snapshot from its tag or id" },
1272 { "delvm", "s", do_delvm,
1273 "tag|id", "delete a VM snapshot from its tag or id" },
1274 { "stop", "", do_stop,
1275 "", "stop emulation", },
1276 { "c|cont", "", do_cont,
1277 "", "resume emulation", },
1278 #ifdef CONFIG_GDBSTUB
1279 { "gdbserver", "s?", do_gdbserver,
1280 "[port]", "start gdbserver session (default port=1234)", },
1282 { "x", "/l", do_memory_dump,
1283 "/fmt addr", "virtual memory dump starting at 'addr'", },
1284 { "xp", "/l", do_physical_memory_dump,
1285 "/fmt addr", "physical memory dump starting at 'addr'", },
1286 { "p|print", "/l", do_print,
1287 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1288 { "i", "/ii.", do_ioport_read,
1289 "/fmt addr", "I/O port read" },
1291 { "sendkey", "s", do_send_key,
1292 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1293 { "system_reset", "", do_system_reset,
1294 "", "reset the system" },
1295 { "system_powerdown", "", do_system_powerdown,
1296 "", "send system power down event" },
1297 { "sum", "ii", do_sum,
1298 "addr size", "compute the checksum of a memory region" },
1299 { "usb_add", "s", do_usb_add,
1300 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1301 { "usb_del", "s", do_usb_del,
1302 "device", "remove USB device 'bus.addr'" },
1303 { "cpu", "i", do_cpu_set,
1304 "index", "set the default CPU" },
1305 { "mouse_move", "sss?", do_mouse_move,
1306 "dx dy [dz]", "send mouse move events" },
1307 { "mouse_button", "i", do_mouse_button,
1308 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1309 { "mouse_set", "i", do_mouse_set,
1310 "index", "set which mouse device receives events" },
1312 { "wavcapture", "si?i?i?", do_wav_capture,
1313 "path [frequency bits channels]",
1314 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1316 { "stopcapture", "i", do_stop_capture,
1317 "capture index", "stop capture" },
1318 { "memsave", "lis", do_memory_save,
1319 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1323 static term_cmd_t info_cmds[] = {
1324 { "version", "", do_info_version,
1325 "", "show the version of qemu" },
1326 { "network", "", do_info_network,
1327 "", "show the network state" },
1328 { "block", "", do_info_block,
1329 "", "show the block devices" },
1330 { "registers", "", do_info_registers,
1331 "", "show the cpu registers" },
1332 { "cpus", "", do_info_cpus,
1333 "", "show infos for each CPU" },
1334 { "history", "", do_info_history,
1335 "", "show the command line history", },
1336 { "irq", "", irq_info,
1337 "", "show the interrupts statistics (if available)", },
1338 { "pic", "", pic_info,
1339 "", "show i8259 (PIC) state", },
1340 { "pci", "", pci_info,
1341 "", "show PCI info", },
1342 #if defined(TARGET_I386)
1343 { "tlb", "", tlb_info,
1344 "", "show virtual to physical memory mappings", },
1345 { "mem", "", mem_info,
1346 "", "show the active virtual memory mappings", },
1348 { "jit", "", do_info_jit,
1349 "", "show dynamic compiler info", },
1350 { "kqemu", "", do_info_kqemu,
1351 "", "show kqemu information", },
1352 { "usb", "", usb_info,
1353 "", "show guest USB devices", },
1354 { "usbhost", "", usb_host_info,
1355 "", "show host USB devices", },
1356 { "profile", "", do_info_profile,
1357 "", "show profiling information", },
1358 { "capture", "", do_info_capture,
1359 "", "show capture information" },
1360 { "snapshots", "", do_info_snapshots,
1361 "", "show the currently saved VM snapshots" },
1362 { "pcmcia", "", pcmcia_info,
1363 "", "show guest PCMCIA status" },
1364 { "mice", "", do_info_mice,
1365 "", "show which guest mouse is receiving events" },
1366 { "vnc", "", do_info_vnc,
1367 "", "show the vnc server status"},
1368 { "name", "", do_info_name,
1369 "", "show the current VM name" },
1370 #if defined(TARGET_PPC)
1371 { "cpustats", "", do_info_cpu_stats,
1372 "", "show CPU statistics", },
1374 #if defined(CONFIG_SLIRP)
1375 { "slirp", "", do_info_slirp,
1376 "", "show SLIRP statistics", },
1381 /*******************************************************************/
1383 static const char *pch;
1384 static jmp_buf expr_env;
1389 typedef struct MonitorDef {
1392 target_long (*get_value)(struct MonitorDef *md, int val);
1396 #if defined(TARGET_I386)
1397 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1399 CPUState *env = mon_get_cpu();
1402 return env->eip + env->segs[R_CS].base;
1406 #if defined(TARGET_PPC)
1407 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1409 CPUState *env = mon_get_cpu();
1417 for (i = 0; i < 8; i++)
1418 u |= env->crf[i] << (32 - (4 * i));
1423 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1425 CPUState *env = mon_get_cpu();
1431 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1433 CPUState *env = mon_get_cpu();
1436 return ppc_load_xer(env);
1439 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1441 CPUState *env = mon_get_cpu();
1444 return cpu_ppc_load_decr(env);
1447 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1449 CPUState *env = mon_get_cpu();
1452 return cpu_ppc_load_tbu(env);
1455 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1457 CPUState *env = mon_get_cpu();
1460 return cpu_ppc_load_tbl(env);
1464 #if defined(TARGET_SPARC)
1465 #ifndef TARGET_SPARC64
1466 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1468 CPUState *env = mon_get_cpu();
1471 return GET_PSR(env);
1475 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1477 CPUState *env = mon_get_cpu();
1480 return env->regwptr[val];
1484 static MonitorDef monitor_defs[] = {
1487 #define SEG(name, seg) \
1488 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1489 { name ".base", offsetof(CPUState, segs[seg].base) },\
1490 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1492 { "eax", offsetof(CPUState, regs[0]) },
1493 { "ecx", offsetof(CPUState, regs[1]) },
1494 { "edx", offsetof(CPUState, regs[2]) },
1495 { "ebx", offsetof(CPUState, regs[3]) },
1496 { "esp|sp", offsetof(CPUState, regs[4]) },
1497 { "ebp|fp", offsetof(CPUState, regs[5]) },
1498 { "esi", offsetof(CPUState, regs[6]) },
1499 { "edi", offsetof(CPUState, regs[7]) },
1500 #ifdef TARGET_X86_64
1501 { "r8", offsetof(CPUState, regs[8]) },
1502 { "r9", offsetof(CPUState, regs[9]) },
1503 { "r10", offsetof(CPUState, regs[10]) },
1504 { "r11", offsetof(CPUState, regs[11]) },
1505 { "r12", offsetof(CPUState, regs[12]) },
1506 { "r13", offsetof(CPUState, regs[13]) },
1507 { "r14", offsetof(CPUState, regs[14]) },
1508 { "r15", offsetof(CPUState, regs[15]) },
1510 { "eflags", offsetof(CPUState, eflags) },
1511 { "eip", offsetof(CPUState, eip) },
1518 { "pc", 0, monitor_get_pc, },
1519 #elif defined(TARGET_PPC)
1520 /* General purpose registers */
1521 { "r0", offsetof(CPUState, gpr[0]) },
1522 { "r1", offsetof(CPUState, gpr[1]) },
1523 { "r2", offsetof(CPUState, gpr[2]) },
1524 { "r3", offsetof(CPUState, gpr[3]) },
1525 { "r4", offsetof(CPUState, gpr[4]) },
1526 { "r5", offsetof(CPUState, gpr[5]) },
1527 { "r6", offsetof(CPUState, gpr[6]) },
1528 { "r7", offsetof(CPUState, gpr[7]) },
1529 { "r8", offsetof(CPUState, gpr[8]) },
1530 { "r9", offsetof(CPUState, gpr[9]) },
1531 { "r10", offsetof(CPUState, gpr[10]) },
1532 { "r11", offsetof(CPUState, gpr[11]) },
1533 { "r12", offsetof(CPUState, gpr[12]) },
1534 { "r13", offsetof(CPUState, gpr[13]) },
1535 { "r14", offsetof(CPUState, gpr[14]) },
1536 { "r15", offsetof(CPUState, gpr[15]) },
1537 { "r16", offsetof(CPUState, gpr[16]) },
1538 { "r17", offsetof(CPUState, gpr[17]) },
1539 { "r18", offsetof(CPUState, gpr[18]) },
1540 { "r19", offsetof(CPUState, gpr[19]) },
1541 { "r20", offsetof(CPUState, gpr[20]) },
1542 { "r21", offsetof(CPUState, gpr[21]) },
1543 { "r22", offsetof(CPUState, gpr[22]) },
1544 { "r23", offsetof(CPUState, gpr[23]) },
1545 { "r24", offsetof(CPUState, gpr[24]) },
1546 { "r25", offsetof(CPUState, gpr[25]) },
1547 { "r26", offsetof(CPUState, gpr[26]) },
1548 { "r27", offsetof(CPUState, gpr[27]) },
1549 { "r28", offsetof(CPUState, gpr[28]) },
1550 { "r29", offsetof(CPUState, gpr[29]) },
1551 { "r30", offsetof(CPUState, gpr[30]) },
1552 { "r31", offsetof(CPUState, gpr[31]) },
1553 /* Floating point registers */
1554 { "f0", offsetof(CPUState, fpr[0]) },
1555 { "f1", offsetof(CPUState, fpr[1]) },
1556 { "f2", offsetof(CPUState, fpr[2]) },
1557 { "f3", offsetof(CPUState, fpr[3]) },
1558 { "f4", offsetof(CPUState, fpr[4]) },
1559 { "f5", offsetof(CPUState, fpr[5]) },
1560 { "f6", offsetof(CPUState, fpr[6]) },
1561 { "f7", offsetof(CPUState, fpr[7]) },
1562 { "f8", offsetof(CPUState, fpr[8]) },
1563 { "f9", offsetof(CPUState, fpr[9]) },
1564 { "f10", offsetof(CPUState, fpr[10]) },
1565 { "f11", offsetof(CPUState, fpr[11]) },
1566 { "f12", offsetof(CPUState, fpr[12]) },
1567 { "f13", offsetof(CPUState, fpr[13]) },
1568 { "f14", offsetof(CPUState, fpr[14]) },
1569 { "f15", offsetof(CPUState, fpr[15]) },
1570 { "f16", offsetof(CPUState, fpr[16]) },
1571 { "f17", offsetof(CPUState, fpr[17]) },
1572 { "f18", offsetof(CPUState, fpr[18]) },
1573 { "f19", offsetof(CPUState, fpr[19]) },
1574 { "f20", offsetof(CPUState, fpr[20]) },
1575 { "f21", offsetof(CPUState, fpr[21]) },
1576 { "f22", offsetof(CPUState, fpr[22]) },
1577 { "f23", offsetof(CPUState, fpr[23]) },
1578 { "f24", offsetof(CPUState, fpr[24]) },
1579 { "f25", offsetof(CPUState, fpr[25]) },
1580 { "f26", offsetof(CPUState, fpr[26]) },
1581 { "f27", offsetof(CPUState, fpr[27]) },
1582 { "f28", offsetof(CPUState, fpr[28]) },
1583 { "f29", offsetof(CPUState, fpr[29]) },
1584 { "f30", offsetof(CPUState, fpr[30]) },
1585 { "f31", offsetof(CPUState, fpr[31]) },
1586 { "fpscr", offsetof(CPUState, fpscr) },
1587 /* Next instruction pointer */
1588 { "nip|pc", offsetof(CPUState, nip) },
1589 { "lr", offsetof(CPUState, lr) },
1590 { "ctr", offsetof(CPUState, ctr) },
1591 { "decr", 0, &monitor_get_decr, },
1592 { "ccr", 0, &monitor_get_ccr, },
1593 /* Machine state register */
1594 { "msr", 0, &monitor_get_msr, },
1595 { "xer", 0, &monitor_get_xer, },
1596 { "tbu", 0, &monitor_get_tbu, },
1597 { "tbl", 0, &monitor_get_tbl, },
1598 #if defined(TARGET_PPC64)
1599 /* Address space register */
1600 { "asr", offsetof(CPUState, asr) },
1602 /* Segment registers */
1603 { "sdr1", offsetof(CPUState, sdr1) },
1604 { "sr0", offsetof(CPUState, sr[0]) },
1605 { "sr1", offsetof(CPUState, sr[1]) },
1606 { "sr2", offsetof(CPUState, sr[2]) },
1607 { "sr3", offsetof(CPUState, sr[3]) },
1608 { "sr4", offsetof(CPUState, sr[4]) },
1609 { "sr5", offsetof(CPUState, sr[5]) },
1610 { "sr6", offsetof(CPUState, sr[6]) },
1611 { "sr7", offsetof(CPUState, sr[7]) },
1612 { "sr8", offsetof(CPUState, sr[8]) },
1613 { "sr9", offsetof(CPUState, sr[9]) },
1614 { "sr10", offsetof(CPUState, sr[10]) },
1615 { "sr11", offsetof(CPUState, sr[11]) },
1616 { "sr12", offsetof(CPUState, sr[12]) },
1617 { "sr13", offsetof(CPUState, sr[13]) },
1618 { "sr14", offsetof(CPUState, sr[14]) },
1619 { "sr15", offsetof(CPUState, sr[15]) },
1620 /* Too lazy to put BATs and SPRs ... */
1621 #elif defined(TARGET_SPARC)
1622 { "g0", offsetof(CPUState, gregs[0]) },
1623 { "g1", offsetof(CPUState, gregs[1]) },
1624 { "g2", offsetof(CPUState, gregs[2]) },
1625 { "g3", offsetof(CPUState, gregs[3]) },
1626 { "g4", offsetof(CPUState, gregs[4]) },
1627 { "g5", offsetof(CPUState, gregs[5]) },
1628 { "g6", offsetof(CPUState, gregs[6]) },
1629 { "g7", offsetof(CPUState, gregs[7]) },
1630 { "o0", 0, monitor_get_reg },
1631 { "o1", 1, monitor_get_reg },
1632 { "o2", 2, monitor_get_reg },
1633 { "o3", 3, monitor_get_reg },
1634 { "o4", 4, monitor_get_reg },
1635 { "o5", 5, monitor_get_reg },
1636 { "o6", 6, monitor_get_reg },
1637 { "o7", 7, monitor_get_reg },
1638 { "l0", 8, monitor_get_reg },
1639 { "l1", 9, monitor_get_reg },
1640 { "l2", 10, monitor_get_reg },
1641 { "l3", 11, monitor_get_reg },
1642 { "l4", 12, monitor_get_reg },
1643 { "l5", 13, monitor_get_reg },
1644 { "l6", 14, monitor_get_reg },
1645 { "l7", 15, monitor_get_reg },
1646 { "i0", 16, monitor_get_reg },
1647 { "i1", 17, monitor_get_reg },
1648 { "i2", 18, monitor_get_reg },
1649 { "i3", 19, monitor_get_reg },
1650 { "i4", 20, monitor_get_reg },
1651 { "i5", 21, monitor_get_reg },
1652 { "i6", 22, monitor_get_reg },
1653 { "i7", 23, monitor_get_reg },
1654 { "pc", offsetof(CPUState, pc) },
1655 { "npc", offsetof(CPUState, npc) },
1656 { "y", offsetof(CPUState, y) },
1657 #ifndef TARGET_SPARC64
1658 { "psr", 0, &monitor_get_psr, },
1659 { "wim", offsetof(CPUState, wim) },
1661 { "tbr", offsetof(CPUState, tbr) },
1662 { "fsr", offsetof(CPUState, fsr) },
1663 { "f0", offsetof(CPUState, fpr[0]) },
1664 { "f1", offsetof(CPUState, fpr[1]) },
1665 { "f2", offsetof(CPUState, fpr[2]) },
1666 { "f3", offsetof(CPUState, fpr[3]) },
1667 { "f4", offsetof(CPUState, fpr[4]) },
1668 { "f5", offsetof(CPUState, fpr[5]) },
1669 { "f6", offsetof(CPUState, fpr[6]) },
1670 { "f7", offsetof(CPUState, fpr[7]) },
1671 { "f8", offsetof(CPUState, fpr[8]) },
1672 { "f9", offsetof(CPUState, fpr[9]) },
1673 { "f10", offsetof(CPUState, fpr[10]) },
1674 { "f11", offsetof(CPUState, fpr[11]) },
1675 { "f12", offsetof(CPUState, fpr[12]) },
1676 { "f13", offsetof(CPUState, fpr[13]) },
1677 { "f14", offsetof(CPUState, fpr[14]) },
1678 { "f15", offsetof(CPUState, fpr[15]) },
1679 { "f16", offsetof(CPUState, fpr[16]) },
1680 { "f17", offsetof(CPUState, fpr[17]) },
1681 { "f18", offsetof(CPUState, fpr[18]) },
1682 { "f19", offsetof(CPUState, fpr[19]) },
1683 { "f20", offsetof(CPUState, fpr[20]) },
1684 { "f21", offsetof(CPUState, fpr[21]) },
1685 { "f22", offsetof(CPUState, fpr[22]) },
1686 { "f23", offsetof(CPUState, fpr[23]) },
1687 { "f24", offsetof(CPUState, fpr[24]) },
1688 { "f25", offsetof(CPUState, fpr[25]) },
1689 { "f26", offsetof(CPUState, fpr[26]) },
1690 { "f27", offsetof(CPUState, fpr[27]) },
1691 { "f28", offsetof(CPUState, fpr[28]) },
1692 { "f29", offsetof(CPUState, fpr[29]) },
1693 { "f30", offsetof(CPUState, fpr[30]) },
1694 { "f31", offsetof(CPUState, fpr[31]) },
1695 #ifdef TARGET_SPARC64
1696 { "f32", offsetof(CPUState, fpr[32]) },
1697 { "f34", offsetof(CPUState, fpr[34]) },
1698 { "f36", offsetof(CPUState, fpr[36]) },
1699 { "f38", offsetof(CPUState, fpr[38]) },
1700 { "f40", offsetof(CPUState, fpr[40]) },
1701 { "f42", offsetof(CPUState, fpr[42]) },
1702 { "f44", offsetof(CPUState, fpr[44]) },
1703 { "f46", offsetof(CPUState, fpr[46]) },
1704 { "f48", offsetof(CPUState, fpr[48]) },
1705 { "f50", offsetof(CPUState, fpr[50]) },
1706 { "f52", offsetof(CPUState, fpr[52]) },
1707 { "f54", offsetof(CPUState, fpr[54]) },
1708 { "f56", offsetof(CPUState, fpr[56]) },
1709 { "f58", offsetof(CPUState, fpr[58]) },
1710 { "f60", offsetof(CPUState, fpr[60]) },
1711 { "f62", offsetof(CPUState, fpr[62]) },
1712 { "asi", offsetof(CPUState, asi) },
1713 { "pstate", offsetof(CPUState, pstate) },
1714 { "cansave", offsetof(CPUState, cansave) },
1715 { "canrestore", offsetof(CPUState, canrestore) },
1716 { "otherwin", offsetof(CPUState, otherwin) },
1717 { "wstate", offsetof(CPUState, wstate) },
1718 { "cleanwin", offsetof(CPUState, cleanwin) },
1719 { "fprs", offsetof(CPUState, fprs) },
1725 static void expr_error(const char *fmt)
1729 longjmp(expr_env, 1);
1732 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1733 static int get_monitor_def(target_long *pval, const char *name)
1738 for(md = monitor_defs; md->name != NULL; md++) {
1739 if (compare_cmd(name, md->name)) {
1740 if (md->get_value) {
1741 *pval = md->get_value(md, md->offset);
1743 CPUState *env = mon_get_cpu();
1746 ptr = (uint8_t *)env + md->offset;
1749 *pval = *(int32_t *)ptr;
1752 *pval = *(target_long *)ptr;
1765 static void next(void)
1769 while (isspace(*pch))
1774 static int64_t expr_sum(void);
1776 static int64_t expr_unary(void)
1799 expr_error("')' expected");
1806 expr_error("character constant expected");
1810 expr_error("missing terminating \' character");
1820 while ((*pch >= 'a' && *pch <= 'z') ||
1821 (*pch >= 'A' && *pch <= 'Z') ||
1822 (*pch >= '0' && *pch <= '9') ||
1823 *pch == '_' || *pch == '.') {
1824 if ((q - buf) < sizeof(buf) - 1)
1828 while (isspace(*pch))
1831 ret = get_monitor_def(®, buf);
1833 expr_error("unknown register");
1835 expr_error("no cpu defined");
1840 expr_error("unexpected end of expression");
1844 #if TARGET_PHYS_ADDR_BITS > 32
1845 n = strtoull(pch, &p, 0);
1847 n = strtoul(pch, &p, 0);
1850 expr_error("invalid char in expression");
1853 while (isspace(*pch))
1861 static int64_t expr_prod(void)
1869 if (op != '*' && op != '/' && op != '%')
1872 val2 = expr_unary();
1881 expr_error("division by zero");
1892 static int64_t expr_logic(void)
1900 if (op != '&' && op != '|' && op != '^')
1920 static int64_t expr_sum(void)
1928 if (op != '+' && op != '-')
1931 val2 = expr_logic();
1940 static int get_expr(int64_t *pval, const char **pp)
1943 if (setjmp(expr_env)) {
1947 while (isspace(*pch))
1954 static int get_str(char *buf, int buf_size, const char **pp)
1972 while (*p != '\0' && *p != '\"') {
1988 qemu_printf("unsupported escape code: '\\%c'\n", c);
1991 if ((q - buf) < buf_size - 1) {
1995 if ((q - buf) < buf_size - 1) {
2002 qemu_printf("unterminated string\n");
2007 while (*p != '\0' && !isspace(*p)) {
2008 if ((q - buf) < buf_size - 1) {
2019 static int default_fmt_format = 'x';
2020 static int default_fmt_size = 4;
2024 static void monitor_handle_command(const char *cmdline)
2026 const char *p, *pstart, *typestr;
2028 int c, nb_args, len, i, has_arg;
2032 void *str_allocated[MAX_ARGS];
2033 void *args[MAX_ARGS];
2036 term_printf("command='%s'\n", cmdline);
2039 /* extract the command name */
2047 while (*p != '\0' && *p != '/' && !isspace(*p))
2050 if (len > sizeof(cmdname) - 1)
2051 len = sizeof(cmdname) - 1;
2052 memcpy(cmdname, pstart, len);
2053 cmdname[len] = '\0';
2055 /* find the command */
2056 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2057 if (compare_cmd(cmdname, cmd->name))
2060 term_printf("unknown command: '%s'\n", cmdname);
2064 for(i = 0; i < MAX_ARGS; i++)
2065 str_allocated[i] = NULL;
2067 /* parse the parameters */
2068 typestr = cmd->args_type;
2085 if (*typestr == '?') {
2088 /* no optional string: NULL argument */
2093 ret = get_str(buf, sizeof(buf), &p);
2097 term_printf("%s: filename expected\n", cmdname);
2100 term_printf("%s: block device name expected\n", cmdname);
2103 term_printf("%s: string expected\n", cmdname);
2108 str = qemu_malloc(strlen(buf) + 1);
2110 str_allocated[nb_args] = str;
2112 if (nb_args >= MAX_ARGS) {
2114 term_printf("%s: too many arguments\n", cmdname);
2117 args[nb_args++] = str;
2122 int count, format, size;
2132 while (isdigit(*p)) {
2133 count = count * 10 + (*p - '0');
2171 if (*p != '\0' && !isspace(*p)) {
2172 term_printf("invalid char in format: '%c'\n", *p);
2176 format = default_fmt_format;
2177 if (format != 'i') {
2178 /* for 'i', not specifying a size gives -1 as size */
2180 size = default_fmt_size;
2182 default_fmt_size = size;
2183 default_fmt_format = format;
2186 format = default_fmt_format;
2187 if (format != 'i') {
2188 size = default_fmt_size;
2193 if (nb_args + 3 > MAX_ARGS)
2195 args[nb_args++] = (void*)(long)count;
2196 args[nb_args++] = (void*)(long)format;
2197 args[nb_args++] = (void*)(long)size;
2207 if (*typestr == '?' || *typestr == '.') {
2208 if (*typestr == '?') {
2224 if (nb_args >= MAX_ARGS)
2226 args[nb_args++] = (void *)(long)has_arg;
2228 if (nb_args >= MAX_ARGS)
2234 if (get_expr(&val, &p))
2238 if (nb_args >= MAX_ARGS)
2240 args[nb_args++] = (void *)(long)val;
2242 if ((nb_args + 1) >= MAX_ARGS)
2244 #if TARGET_PHYS_ADDR_BITS > 32
2245 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2247 args[nb_args++] = (void *)0;
2249 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2267 term_printf("%s: unsupported option -%c\n",
2274 if (nb_args >= MAX_ARGS)
2276 args[nb_args++] = (void *)(long)has_option;
2281 term_printf("%s: unknown type '%c'\n", cmdname, c);
2285 /* check that all arguments were parsed */
2289 term_printf("%s: extraneous characters at the end of line\n",
2299 cmd->handler(args[0]);
2302 cmd->handler(args[0], args[1]);
2305 cmd->handler(args[0], args[1], args[2]);
2308 cmd->handler(args[0], args[1], args[2], args[3]);
2311 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2314 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2317 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2320 term_printf("unsupported number of arguments: %d\n", nb_args);
2324 for(i = 0; i < MAX_ARGS; i++)
2325 qemu_free(str_allocated[i]);
2329 static void cmd_completion(const char *name, const char *list)
2331 const char *p, *pstart;
2340 p = pstart + strlen(pstart);
2342 if (len > sizeof(cmd) - 2)
2343 len = sizeof(cmd) - 2;
2344 memcpy(cmd, pstart, len);
2346 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2347 add_completion(cmd);
2355 static void file_completion(const char *input)
2360 char file[1024], file_prefix[1024];
2364 p = strrchr(input, '/');
2367 pstrcpy(file_prefix, sizeof(file_prefix), input);
2370 input_path_len = p - input + 1;
2371 memcpy(path, input, input_path_len);
2372 if (input_path_len > sizeof(path) - 1)
2373 input_path_len = sizeof(path) - 1;
2374 path[input_path_len] = '\0';
2375 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2377 #ifdef DEBUG_COMPLETION
2378 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2380 ffs = opendir(path);
2388 if (strstart(d->d_name, file_prefix, NULL)) {
2389 memcpy(file, input, input_path_len);
2390 strcpy(file + input_path_len, d->d_name);
2391 /* stat the file to find out if it's a directory.
2392 * In that case add a slash to speed up typing long paths
2395 if(S_ISDIR(sb.st_mode))
2397 add_completion(file);
2403 static void block_completion_it(void *opaque, const char *name)
2405 const char *input = opaque;
2407 if (input[0] == '\0' ||
2408 !strncmp(name, (char *)input, strlen(input))) {
2409 add_completion(name);
2413 /* NOTE: this parser is an approximate form of the real command parser */
2414 static void parse_cmdline(const char *cmdline,
2415 int *pnb_args, char **args)
2428 if (nb_args >= MAX_ARGS)
2430 ret = get_str(buf, sizeof(buf), &p);
2431 args[nb_args] = qemu_strdup(buf);
2436 *pnb_args = nb_args;
2439 void readline_find_completion(const char *cmdline)
2441 const char *cmdname;
2442 char *args[MAX_ARGS];
2443 int nb_args, i, len;
2444 const char *ptype, *str;
2448 parse_cmdline(cmdline, &nb_args, args);
2449 #ifdef DEBUG_COMPLETION
2450 for(i = 0; i < nb_args; i++) {
2451 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2455 /* if the line ends with a space, it means we want to complete the
2457 len = strlen(cmdline);
2458 if (len > 0 && isspace(cmdline[len - 1])) {
2459 if (nb_args >= MAX_ARGS)
2461 args[nb_args++] = qemu_strdup("");
2464 /* command completion */
2469 completion_index = strlen(cmdname);
2470 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2471 cmd_completion(cmdname, cmd->name);
2474 /* find the command */
2475 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2476 if (compare_cmd(args[0], cmd->name))
2481 ptype = cmd->args_type;
2482 for(i = 0; i < nb_args - 2; i++) {
2483 if (*ptype != '\0') {
2485 while (*ptype == '?')
2489 str = args[nb_args - 1];
2492 /* file completion */
2493 completion_index = strlen(str);
2494 file_completion(str);
2497 /* block device name completion */
2498 completion_index = strlen(str);
2499 bdrv_iterate(block_completion_it, (void *)str);
2502 /* XXX: more generic ? */
2503 if (!strcmp(cmd->name, "info")) {
2504 completion_index = strlen(str);
2505 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2506 cmd_completion(str, cmd->name);
2508 } else if (!strcmp(cmd->name, "sendkey")) {
2509 completion_index = strlen(str);
2510 for(key = key_defs; key->name != NULL; key++) {
2511 cmd_completion(str, key->name);
2519 for(i = 0; i < nb_args; i++)
2523 static int term_can_read(void *opaque)
2528 static void term_read(void *opaque, const uint8_t *buf, int size)
2531 for(i = 0; i < size; i++)
2532 readline_handle_byte(buf[i]);
2535 static void monitor_start_input(void);
2537 static void monitor_handle_command1(void *opaque, const char *cmdline)
2539 monitor_handle_command(cmdline);
2540 monitor_start_input();
2543 static void monitor_start_input(void)
2545 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2548 static void term_event(void *opaque, int event)
2550 if (event != CHR_EVENT_RESET)
2554 term_printf("QEMU %s monitor - type 'help' for more information\n",
2556 monitor_start_input();
2559 static int is_first_init = 1;
2561 void monitor_init(CharDriverState *hd, int show_banner)
2565 if (is_first_init) {
2566 for (i = 0; i < MAX_MON; i++) {
2567 monitor_hd[i] = NULL;
2571 for (i = 0; i < MAX_MON; i++) {
2572 if (monitor_hd[i] == NULL) {
2578 hide_banner = !show_banner;
2580 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2582 readline_start("", 0, monitor_handle_command1, NULL);
2585 /* XXX: use threads ? */
2586 /* modal monitor readline */
2587 static int monitor_readline_started;
2588 static char *monitor_readline_buf;
2589 static int monitor_readline_buf_size;
2591 static void monitor_readline_cb(void *opaque, const char *input)
2593 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2594 monitor_readline_started = 0;
2597 void monitor_readline(const char *prompt, int is_password,
2598 char *buf, int buf_size)
2603 for (i = 0; i < MAX_MON; i++)
2604 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2605 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2607 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2608 monitor_readline_buf = buf;
2609 monitor_readline_buf_size = buf_size;
2610 monitor_readline_started = 1;
2611 while (monitor_readline_started) {