]> Git Repo - qemu.git/blob - monitor.c
added mouse event generation
[qemu.git] / monitor.c
1 /*
2  * QEMU monitor
3  * 
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  * 
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
27
28 //#define DEBUG
29 //#define DEBUG_COMPLETION
30
31 #ifndef offsetof
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
33 #endif
34
35 /*
36  * Supported types:
37  * 
38  * 'F'          filename
39  * 'B'          block device name
40  * 's'          string (accept optional quote)
41  * 'i'          32 bit integer
42  * 'l'          target long (32 or 64 bit)
43  * '/'          optional gdb-like print format (like "/10x")
44  *
45  * '?'          optional type (for 'F', 's' and 'i')
46  *
47  */
48
49 typedef struct term_cmd_t {
50     const char *name;
51     const char *args_type;
52     void (*handler)();
53     const char *params;
54     const char *help;
55 } term_cmd_t;
56
57 static CharDriverState *monitor_hd;
58
59 static term_cmd_t term_cmds[];
60 static term_cmd_t info_cmds[];
61
62 static char term_outbuf[1024];
63 static int term_outbuf_index;
64
65 static void monitor_start_input(void);
66
67 CPUState *mon_cpu = NULL;
68
69 void term_flush(void)
70 {
71     if (term_outbuf_index > 0) {
72         qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
73         term_outbuf_index = 0;
74     }
75 }
76
77 /* flush at every end of line or if the buffer is full */
78 void term_puts(const char *str)
79 {
80     int c;
81     for(;;) {
82         c = *str++;
83         if (c == '\0')
84             break;
85         if (c == '\n')
86             term_outbuf[term_outbuf_index++] = '\r';
87         term_outbuf[term_outbuf_index++] = c;
88         if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
89             c == '\n')
90             term_flush();
91     }
92 }
93
94 void term_vprintf(const char *fmt, va_list ap)
95 {
96     char buf[4096];
97     vsnprintf(buf, sizeof(buf), fmt, ap);
98     term_puts(buf);
99 }
100
101 void term_printf(const char *fmt, ...)
102 {
103     va_list ap;
104     va_start(ap, fmt);
105     term_vprintf(fmt, ap);
106     va_end(ap);
107 }
108
109 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
110 {
111     va_list ap;
112     va_start(ap, fmt);
113     term_vprintf(fmt, ap);
114     va_end(ap);
115     return 0;
116 }
117
118 static int compare_cmd(const char *name, const char *list)
119 {
120     const char *p, *pstart;
121     int len;
122     len = strlen(name);
123     p = list;
124     for(;;) {
125         pstart = p;
126         p = strchr(p, '|');
127         if (!p)
128             p = pstart + strlen(pstart);
129         if ((p - pstart) == len && !memcmp(pstart, name, len))
130             return 1;
131         if (*p == '\0')
132             break;
133         p++;
134     }
135     return 0;
136 }
137
138 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
139 {
140     term_cmd_t *cmd;
141
142     for(cmd = cmds; cmd->name != NULL; cmd++) {
143         if (!name || !strcmp(name, cmd->name))
144             term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
145     }
146 }
147
148 static void help_cmd(const char *name)
149 {
150     if (name && !strcmp(name, "info")) {
151         help_cmd1(info_cmds, "info ", NULL);
152     } else {
153         help_cmd1(term_cmds, "", name);
154         if (name && !strcmp(name, "log")) {
155             CPULogItem *item;
156             term_printf("Log items (comma separated):\n");
157             term_printf("%-10s %s\n", "none", "remove all logs");
158             for(item = cpu_log_items; item->mask != 0; item++) {
159                 term_printf("%-10s %s\n", item->name, item->help);
160             }
161         }
162     }
163 }
164
165 static void do_help(const char *name)
166 {
167     help_cmd(name);
168 }
169
170 static void do_commit(void)
171 {
172     int i;
173
174     for (i = 0; i < MAX_DISKS; i++) {
175         if (bs_table[i]) {
176             bdrv_commit(bs_table[i]);
177         }
178     }
179 }
180
181 static void do_info(const char *item)
182 {
183     term_cmd_t *cmd;
184
185     if (!item)
186         goto help;
187     for(cmd = info_cmds; cmd->name != NULL; cmd++) {
188         if (compare_cmd(item, cmd->name)) 
189             goto found;
190     }
191  help:
192     help_cmd("info");
193     return;
194  found:
195     cmd->handler();
196 }
197
198 static void do_info_version(void)
199 {
200   term_printf("%s\n", QEMU_VERSION);
201 }
202
203 static void do_info_block(void)
204 {
205     bdrv_info();
206 }
207
208 /* get the current CPU defined by the user */
209 int mon_set_cpu(int cpu_index)
210 {
211     CPUState *env;
212
213     for(env = first_cpu; env != NULL; env = env->next_cpu) {
214         if (env->cpu_index == cpu_index) {
215             mon_cpu = env;
216             return 0;
217         }
218     }
219     return -1;
220 }
221
222 CPUState *mon_get_cpu(void)
223 {
224     if (!mon_cpu) {
225         mon_set_cpu(0);
226     }
227     return mon_cpu;
228 }
229
230 static void do_info_registers(void)
231 {
232     CPUState *env;
233     env = mon_get_cpu();
234     if (!env)
235         return;
236 #ifdef TARGET_I386
237     cpu_dump_state(env, NULL, monitor_fprintf,
238                    X86_DUMP_FPU);
239 #else
240     cpu_dump_state(env, NULL, monitor_fprintf, 
241                    0);
242 #endif
243 }
244
245 static void do_info_cpus(void)
246 {
247     CPUState *env;
248
249     /* just to set the default cpu if not already done */
250     mon_get_cpu();
251
252     for(env = first_cpu; env != NULL; env = env->next_cpu) {
253         term_printf("%c CPU #%d:", 
254                     (env == mon_cpu) ? '*' : ' ',
255                     env->cpu_index);
256 #if defined(TARGET_I386)
257         term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
258         if (env->hflags & HF_HALTED_MASK)
259             term_printf(" (halted)");
260 #elif defined(TARGET_PPC)
261         term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
262         if (env->halted)
263             term_printf(" (halted)");
264 #elif defined(TARGET_SPARC)
265         term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
266         if (env->halted)
267             term_printf(" (halted)");
268 #endif
269         term_printf("\n");
270     }
271 }
272
273 static void do_cpu_set(int index)
274 {
275     if (mon_set_cpu(index) < 0)
276         term_printf("Invalid CPU index\n");
277 }
278
279 static void do_info_jit(void)
280 {
281     dump_exec_info(NULL, monitor_fprintf);
282 }
283
284 static void do_info_history (void)
285 {
286     int i;
287     const char *str;
288     
289     i = 0;
290     for(;;) {
291         str = readline_get_history(i);
292         if (!str)
293             break;
294         term_printf("%d: '%s'\n", i, str);
295         i++;
296     }
297 }
298
299 static void do_quit(void)
300 {
301     exit(0);
302 }
303
304 static int eject_device(BlockDriverState *bs, int force)
305 {
306     if (bdrv_is_inserted(bs)) {
307         if (!force) {
308             if (!bdrv_is_removable(bs)) {
309                 term_printf("device is not removable\n");
310                 return -1;
311             }
312             if (bdrv_is_locked(bs)) {
313                 term_printf("device is locked\n");
314                 return -1;
315             }
316         }
317         bdrv_close(bs);
318     }
319     return 0;
320 }
321
322 static void do_eject(int force, const char *filename)
323 {
324     BlockDriverState *bs;
325
326     bs = bdrv_find(filename);
327     if (!bs) {
328         term_printf("device not found\n");
329         return;
330     }
331     eject_device(bs, force);
332 }
333
334 static void do_change(const char *device, const char *filename)
335 {
336     BlockDriverState *bs;
337     int i;
338     char password[256];
339
340     bs = bdrv_find(device);
341     if (!bs) {
342         term_printf("device not found\n");
343         return;
344     }
345     if (eject_device(bs, 0) < 0)
346         return;
347     bdrv_open(bs, filename, 0);
348     if (bdrv_is_encrypted(bs)) {
349         term_printf("%s is encrypted.\n", device);
350         for(i = 0; i < 3; i++) {
351             monitor_readline("Password: ", 1, password, sizeof(password));
352             if (bdrv_set_key(bs, password) == 0)
353                 break;
354             term_printf("invalid password\n");
355         }
356     }
357 }
358
359 static void do_screen_dump(const char *filename)
360 {
361     vga_hw_screen_dump(filename);
362 }
363
364 static void do_log(const char *items)
365 {
366     int mask;
367     
368     if (!strcmp(items, "none")) {
369         mask = 0;
370     } else {
371         mask = cpu_str_to_log_mask(items);
372         if (!mask) {
373             help_cmd("log");
374             return;
375         }
376     }
377     cpu_set_log(mask);
378 }
379
380 static void do_savevm(const char *filename)
381 {
382     if (qemu_savevm(filename) < 0)
383         term_printf("I/O error when saving VM to '%s'\n", filename);
384 }
385
386 static void do_loadvm(const char *filename)
387 {
388     if (qemu_loadvm(filename) < 0) 
389         term_printf("I/O error when loading VM from '%s'\n", filename);
390 }
391
392 static void do_stop(void)
393 {
394     vm_stop(EXCP_INTERRUPT);
395 }
396
397 static void do_cont(void)
398 {
399     vm_start();
400 }
401
402 #ifdef CONFIG_GDBSTUB
403 static void do_gdbserver(int has_port, int port)
404 {
405     if (!has_port)
406         port = DEFAULT_GDBSTUB_PORT;
407     if (gdbserver_start(port) < 0) {
408         qemu_printf("Could not open gdbserver socket on port %d\n", port);
409     } else {
410         qemu_printf("Waiting gdb connection on port %d\n", port);
411     }
412 }
413 #endif
414
415 static void term_printc(int c)
416 {
417     term_printf("'");
418     switch(c) {
419     case '\'':
420         term_printf("\\'");
421         break;
422     case '\\':
423         term_printf("\\\\");
424         break;
425     case '\n':
426         term_printf("\\n");
427         break;
428     case '\r':
429         term_printf("\\r");
430         break;
431     default:
432         if (c >= 32 && c <= 126) {
433             term_printf("%c", c);
434         } else {
435             term_printf("\\x%02x", c);
436         }
437         break;
438     }
439     term_printf("'");
440 }
441
442 static void memory_dump(int count, int format, int wsize, 
443                         target_ulong addr, int is_physical)
444 {
445     CPUState *env;
446     int nb_per_line, l, line_size, i, max_digits, len;
447     uint8_t buf[16];
448     uint64_t v;
449
450     if (format == 'i') {
451         int flags;
452         flags = 0;
453         env = mon_get_cpu();
454         if (!env && !is_physical)
455             return;
456 #ifdef TARGET_I386
457         if (wsize == 2) {
458             flags = 1;
459         } else if (wsize == 4) {
460             flags = 0;
461         } else {
462             /* as default we use the current CS size */
463             flags = 0;
464             if (env) {
465 #ifdef TARGET_X86_64
466                 if ((env->efer & MSR_EFER_LMA) && 
467                     (env->segs[R_CS].flags & DESC_L_MASK))
468                     flags = 2;
469                 else
470 #endif
471                 if (!(env->segs[R_CS].flags & DESC_B_MASK))
472                     flags = 1;
473             }
474         }
475 #endif
476         monitor_disas(env, addr, count, is_physical, flags);
477         return;
478     }
479
480     len = wsize * count;
481     if (wsize == 1)
482         line_size = 8;
483     else
484         line_size = 16;
485     nb_per_line = line_size / wsize;
486     max_digits = 0;
487
488     switch(format) {
489     case 'o':
490         max_digits = (wsize * 8 + 2) / 3;
491         break;
492     default:
493     case 'x':
494         max_digits = (wsize * 8) / 4;
495         break;
496     case 'u':
497     case 'd':
498         max_digits = (wsize * 8 * 10 + 32) / 33;
499         break;
500     case 'c':
501         wsize = 1;
502         break;
503     }
504
505     while (len > 0) {
506         term_printf(TARGET_FMT_lx ":", addr);
507         l = len;
508         if (l > line_size)
509             l = line_size;
510         if (is_physical) {
511             cpu_physical_memory_rw(addr, buf, l, 0);
512         } else {
513             env = mon_get_cpu();
514             if (!env)
515                 break;
516             cpu_memory_rw_debug(env, addr, buf, l, 0);
517         }
518         i = 0; 
519         while (i < l) {
520             switch(wsize) {
521             default:
522             case 1:
523                 v = ldub_raw(buf + i);
524                 break;
525             case 2:
526                 v = lduw_raw(buf + i);
527                 break;
528             case 4:
529                 v = (uint32_t)ldl_raw(buf + i);
530                 break;
531             case 8:
532                 v = ldq_raw(buf + i);
533                 break;
534             }
535             term_printf(" ");
536             switch(format) {
537             case 'o':
538                 term_printf("%#*" PRIo64, max_digits, v);
539                 break;
540             case 'x':
541                 term_printf("0x%0*" PRIx64, max_digits, v);
542                 break;
543             case 'u':
544                 term_printf("%*" PRIu64, max_digits, v);
545                 break;
546             case 'd':
547                 term_printf("%*" PRId64, max_digits, v);
548                 break;
549             case 'c':
550                 term_printc(v);
551                 break;
552             }
553             i += wsize;
554         }
555         term_printf("\n");
556         addr += l;
557         len -= l;
558     }
559 }
560
561 #if TARGET_LONG_BITS == 64
562 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
563 #else
564 #define GET_TLONG(h, l) (l)
565 #endif
566
567 static void do_memory_dump(int count, int format, int size, 
568                            uint32_t addrh, uint32_t addrl)
569 {
570     target_long addr = GET_TLONG(addrh, addrl);
571     memory_dump(count, format, size, addr, 0);
572 }
573
574 static void do_physical_memory_dump(int count, int format, int size,
575                                     uint32_t addrh, uint32_t addrl)
576
577 {
578     target_long addr = GET_TLONG(addrh, addrl);
579     memory_dump(count, format, size, addr, 1);
580 }
581
582 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
583 {
584     target_long val = GET_TLONG(valh, vall);
585 #if TARGET_LONG_BITS == 32
586     switch(format) {
587     case 'o':
588         term_printf("%#o", val);
589         break;
590     case 'x':
591         term_printf("%#x", val);
592         break;
593     case 'u':
594         term_printf("%u", val);
595         break;
596     default:
597     case 'd':
598         term_printf("%d", val);
599         break;
600     case 'c':
601         term_printc(val);
602         break;
603     }
604 #else
605     switch(format) {
606     case 'o':
607         term_printf("%#" PRIo64, val);
608         break;
609     case 'x':
610         term_printf("%#" PRIx64, val);
611         break;
612     case 'u':
613         term_printf("%" PRIu64, val);
614         break;
615     default:
616     case 'd':
617         term_printf("%" PRId64, val);
618         break;
619     case 'c':
620         term_printc(val);
621         break;
622     }
623 #endif
624     term_printf("\n");
625 }
626
627 static void do_sum(uint32_t start, uint32_t size)
628 {
629     uint32_t addr;
630     uint8_t buf[1];
631     uint16_t sum;
632
633     sum = 0;
634     for(addr = start; addr < (start + size); addr++) {
635         cpu_physical_memory_rw(addr, buf, 1, 0);
636         /* BSD sum algorithm ('sum' Unix command) */
637         sum = (sum >> 1) | (sum << 15);
638         sum += buf[0];
639     }
640     term_printf("%05d\n", sum);
641 }
642
643 typedef struct {
644     int keycode;
645     const char *name;
646 } KeyDef;
647
648 static const KeyDef key_defs[] = {
649     { 0x2a, "shift" },
650     { 0x36, "shift_r" },
651     
652     { 0x38, "alt" },
653     { 0xb8, "alt_r" },
654     { 0x1d, "ctrl" },
655     { 0x9d, "ctrl_r" },
656
657     { 0xdd, "menu" },
658
659     { 0x01, "esc" },
660
661     { 0x02, "1" },
662     { 0x03, "2" },
663     { 0x04, "3" },
664     { 0x05, "4" },
665     { 0x06, "5" },
666     { 0x07, "6" },
667     { 0x08, "7" },
668     { 0x09, "8" },
669     { 0x0a, "9" },
670     { 0x0b, "0" },
671     { 0x0c, "minus" },
672     { 0x0d, "equal" },
673     { 0x0e, "backspace" },
674
675     { 0x0f, "tab" },
676     { 0x10, "q" },
677     { 0x11, "w" },
678     { 0x12, "e" },
679     { 0x13, "r" },
680     { 0x14, "t" },
681     { 0x15, "y" },
682     { 0x16, "u" },
683     { 0x17, "i" },
684     { 0x18, "o" },
685     { 0x19, "p" },
686
687     { 0x1c, "ret" },
688
689     { 0x1e, "a" },
690     { 0x1f, "s" },
691     { 0x20, "d" },
692     { 0x21, "f" },
693     { 0x22, "g" },
694     { 0x23, "h" },
695     { 0x24, "j" },
696     { 0x25, "k" },
697     { 0x26, "l" },
698
699     { 0x2c, "z" },
700     { 0x2d, "x" },
701     { 0x2e, "c" },
702     { 0x2f, "v" },
703     { 0x30, "b" },
704     { 0x31, "n" },
705     { 0x32, "m" },
706     
707     { 0x39, "spc" },
708     { 0x3a, "caps_lock" },
709     { 0x3b, "f1" },
710     { 0x3c, "f2" },
711     { 0x3d, "f3" },
712     { 0x3e, "f4" },
713     { 0x3f, "f5" },
714     { 0x40, "f6" },
715     { 0x41, "f7" },
716     { 0x42, "f8" },
717     { 0x43, "f9" },
718     { 0x44, "f10" },
719     { 0x45, "num_lock" },
720     { 0x46, "scroll_lock" },
721
722     { 0xb5, "kp_divide" },
723     { 0x37, "kp_multiply" },
724     { 0x4a, "kp_substract" },
725     { 0x4e, "kp_add" },
726     { 0x9c, "kp_enter" },
727     { 0x53, "kp_decimal" },
728
729     { 0x52, "kp_0" },
730     { 0x4f, "kp_1" },
731     { 0x50, "kp_2" },
732     { 0x51, "kp_3" },
733     { 0x4b, "kp_4" },
734     { 0x4c, "kp_5" },
735     { 0x4d, "kp_6" },
736     { 0x47, "kp_7" },
737     { 0x48, "kp_8" },
738     { 0x49, "kp_9" },
739     
740     { 0x56, "<" },
741
742     { 0x57, "f11" },
743     { 0x58, "f12" },
744
745     { 0xb7, "print" },
746
747     { 0xc7, "home" },
748     { 0xc9, "pgup" },
749     { 0xd1, "pgdn" },
750     { 0xcf, "end" },
751
752     { 0xcb, "left" },
753     { 0xc8, "up" },
754     { 0xd0, "down" },
755     { 0xcd, "right" },
756
757     { 0xd2, "insert" },
758     { 0xd3, "delete" },
759     { 0, NULL },
760 };
761
762 static int get_keycode(const char *key)
763 {
764     const KeyDef *p;
765     char *endp;
766     int ret;
767
768     for(p = key_defs; p->name != NULL; p++) {
769         if (!strcmp(key, p->name))
770             return p->keycode;
771     }
772     if (strstart(key, "0x", NULL)) {
773         ret = strtoul(key, &endp, 0);
774         if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
775             return ret;
776     }
777     return -1;
778 }
779
780 static void do_send_key(const char *string)
781 {
782     char keybuf[16], *q;
783     uint8_t keycodes[16];
784     const char *p;
785     int nb_keycodes, keycode, i;
786     
787     nb_keycodes = 0;
788     p = string;
789     while (*p != '\0') {
790         q = keybuf;
791         while (*p != '\0' && *p != '-') {
792             if ((q - keybuf) < sizeof(keybuf) - 1) {
793                 *q++ = *p;
794             }
795             p++;
796         }
797         *q = '\0';
798         keycode = get_keycode(keybuf);
799         if (keycode < 0) {
800             term_printf("unknown key: '%s'\n", keybuf);
801             return;
802         }
803         keycodes[nb_keycodes++] = keycode;
804         if (*p == '\0')
805             break;
806         p++;
807     }
808     /* key down events */
809     for(i = 0; i < nb_keycodes; i++) {
810         keycode = keycodes[i];
811         if (keycode & 0x80)
812             kbd_put_keycode(0xe0);
813         kbd_put_keycode(keycode & 0x7f);
814     }
815     /* key up events */
816     for(i = nb_keycodes - 1; i >= 0; i--) {
817         keycode = keycodes[i];
818         if (keycode & 0x80)
819             kbd_put_keycode(0xe0);
820         kbd_put_keycode(keycode | 0x80);
821     }
822 }
823
824 static int mouse_button_state;
825
826 static void do_mouse_move(const char *dx_str, const char *dy_str, 
827                           const char *dz_str)
828 {
829     int dx, dy, dz;
830     dx = strtol(dx_str, NULL, 0);
831     dy = strtol(dy_str, NULL, 0);
832     dz = 0;
833     if (dz_str) 
834         dz = strtol(dz_str, NULL, 0);
835     kbd_mouse_event(dx, dy, dz, mouse_button_state);
836 }
837
838 static void do_mouse_button(int button_state)
839 {
840     mouse_button_state = button_state;
841     kbd_mouse_event(0, 0, 0, mouse_button_state);
842 }
843
844 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
845 {
846     uint32_t val;
847     int suffix;
848
849     if (has_index) {
850         cpu_outb(NULL, addr & 0xffff, index & 0xff);
851         addr++;
852     }
853     addr &= 0xffff;
854
855     switch(size) {
856     default:
857     case 1:
858         val = cpu_inb(NULL, addr);
859         suffix = 'b';
860         break;
861     case 2:
862         val = cpu_inw(NULL, addr);
863         suffix = 'w';
864         break;
865     case 4:
866         val = cpu_inl(NULL, addr);
867         suffix = 'l';
868         break;
869     }
870     term_printf("port%c[0x%04x] = %#0*x\n",
871                 suffix, addr, size * 2, val);
872 }
873
874 static void do_system_reset(void)
875 {
876     qemu_system_reset_request();
877 }
878
879 static void do_system_powerdown(void)
880 {
881     qemu_system_powerdown_request();
882 }
883
884 #if defined(TARGET_I386)
885 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
886 {
887     term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
888                 addr,
889                 pte & mask,
890                 pte & PG_GLOBAL_MASK ? 'G' : '-',
891                 pte & PG_PSE_MASK ? 'P' : '-',
892                 pte & PG_DIRTY_MASK ? 'D' : '-',
893                 pte & PG_ACCESSED_MASK ? 'A' : '-',
894                 pte & PG_PCD_MASK ? 'C' : '-',
895                 pte & PG_PWT_MASK ? 'T' : '-',
896                 pte & PG_USER_MASK ? 'U' : '-',
897                 pte & PG_RW_MASK ? 'W' : '-');
898 }
899
900 static void tlb_info(void)
901 {
902     CPUState *env;
903     int l1, l2;
904     uint32_t pgd, pde, pte;
905
906     env = mon_get_cpu();
907     if (!env)
908         return;
909
910     if (!(env->cr[0] & CR0_PG_MASK)) {
911         term_printf("PG disabled\n");
912         return;
913     }
914     pgd = env->cr[3] & ~0xfff;
915     for(l1 = 0; l1 < 1024; l1++) {
916         cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
917         pde = le32_to_cpu(pde);
918         if (pde & PG_PRESENT_MASK) {
919             if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
920                 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
921             } else {
922                 for(l2 = 0; l2 < 1024; l2++) {
923                     cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
924                                              (uint8_t *)&pte, 4);
925                     pte = le32_to_cpu(pte);
926                     if (pte & PG_PRESENT_MASK) {
927                         print_pte((l1 << 22) + (l2 << 12), 
928                                   pte & ~PG_PSE_MASK, 
929                                   ~0xfff);
930                     }
931                 }
932             }
933         }
934     }
935 }
936
937 static void mem_print(uint32_t *pstart, int *plast_prot, 
938                       uint32_t end, int prot)
939 {
940     int prot1;
941     prot1 = *plast_prot;
942     if (prot != prot1) {
943         if (*pstart != -1) {
944             term_printf("%08x-%08x %08x %c%c%c\n",
945                         *pstart, end, end - *pstart, 
946                         prot1 & PG_USER_MASK ? 'u' : '-',
947                         'r',
948                         prot1 & PG_RW_MASK ? 'w' : '-');
949         }
950         if (prot != 0)
951             *pstart = end;
952         else
953             *pstart = -1;
954         *plast_prot = prot;
955     }
956 }
957
958 static void mem_info(void)
959 {
960     CPUState *env;
961     int l1, l2, prot, last_prot;
962     uint32_t pgd, pde, pte, start, end;
963
964     env = mon_get_cpu();
965     if (!env)
966         return;
967
968     if (!(env->cr[0] & CR0_PG_MASK)) {
969         term_printf("PG disabled\n");
970         return;
971     }
972     pgd = env->cr[3] & ~0xfff;
973     last_prot = 0;
974     start = -1;
975     for(l1 = 0; l1 < 1024; l1++) {
976         cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
977         pde = le32_to_cpu(pde);
978         end = l1 << 22;
979         if (pde & PG_PRESENT_MASK) {
980             if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
981                 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
982                 mem_print(&start, &last_prot, end, prot);
983             } else {
984                 for(l2 = 0; l2 < 1024; l2++) {
985                     cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
986                                              (uint8_t *)&pte, 4);
987                     pte = le32_to_cpu(pte);
988                     end = (l1 << 22) + (l2 << 12);
989                     if (pte & PG_PRESENT_MASK) {
990                         prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
991                     } else {
992                         prot = 0;
993                     }
994                     mem_print(&start, &last_prot, end, prot);
995                 }
996             }
997         } else {
998             prot = 0;
999             mem_print(&start, &last_prot, end, prot);
1000         }
1001     }
1002 }
1003 #endif
1004
1005 static void do_info_kqemu(void)
1006 {
1007 #ifdef USE_KQEMU
1008     CPUState *env;
1009     int val;
1010     val = 0;
1011     env = mon_get_cpu();
1012     if (!env) {
1013         term_printf("No cpu initialized yet");
1014         return;
1015     }
1016     val = env->kqemu_enabled;
1017     term_printf("kqemu support: ");
1018     switch(val) {
1019     default:
1020     case 0:
1021         term_printf("disabled\n");
1022         break;
1023     case 1:
1024         term_printf("enabled for user code\n");
1025         break;
1026     case 2:
1027         term_printf("enabled for user and kernel code\n");
1028         break;
1029     }
1030 #else
1031     term_printf("kqemu support: not compiled\n");
1032 #endif
1033
1034
1035 #ifdef CONFIG_PROFILER
1036
1037 int64_t kqemu_time;
1038 int64_t qemu_time;
1039 int64_t kqemu_exec_count;
1040 int64_t dev_time;
1041 int64_t kqemu_ret_int_count;
1042 int64_t kqemu_ret_excp_count;
1043 int64_t kqemu_ret_intr_count;
1044
1045 static void do_info_profile(void)
1046 {
1047     int64_t total;
1048     total = qemu_time;
1049     if (total == 0)
1050         total = 1;
1051     term_printf("async time  %" PRId64 " (%0.3f)\n",
1052                 dev_time, dev_time / (double)ticks_per_sec);
1053     term_printf("qemu time   %" PRId64 " (%0.3f)\n",
1054                 qemu_time, qemu_time / (double)ticks_per_sec);
1055     term_printf("kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1056                 kqemu_time, kqemu_time / (double)ticks_per_sec,
1057                 kqemu_time / (double)total * 100.0,
1058                 kqemu_exec_count,
1059                 kqemu_ret_int_count,
1060                 kqemu_ret_excp_count,
1061                 kqemu_ret_intr_count);
1062     qemu_time = 0;
1063     kqemu_time = 0;
1064     kqemu_exec_count = 0;
1065     dev_time = 0;
1066     kqemu_ret_int_count = 0;
1067     kqemu_ret_excp_count = 0;
1068     kqemu_ret_intr_count = 0;
1069 #ifdef USE_KQEMU
1070     kqemu_record_dump();
1071 #endif
1072 }
1073 #else
1074 static void do_info_profile(void)
1075 {
1076     term_printf("Internal profiler not compiled\n");
1077 }
1078 #endif
1079
1080 static term_cmd_t term_cmds[] = {
1081     { "help|?", "s?", do_help, 
1082       "[cmd]", "show the help" },
1083     { "commit", "", do_commit, 
1084       "", "commit changes to the disk images (if -snapshot is used)" },
1085     { "info", "s?", do_info,
1086       "subcommand", "show various information about the system state" },
1087     { "q|quit", "", do_quit,
1088       "", "quit the emulator" },
1089     { "eject", "-fB", do_eject,
1090       "[-f] device", "eject a removable media (use -f to force it)" },
1091     { "change", "BF", do_change,
1092       "device filename", "change a removable media" },
1093     { "screendump", "F", do_screen_dump, 
1094       "filename", "save screen into PPM image 'filename'" },
1095     { "log", "s", do_log,
1096       "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
1097     { "savevm", "F", do_savevm,
1098       "filename", "save the whole virtual machine state to 'filename'" }, 
1099     { "loadvm", "F", do_loadvm,
1100       "filename", "restore the whole virtual machine state from 'filename'" }, 
1101     { "stop", "", do_stop, 
1102       "", "stop emulation", },
1103     { "c|cont", "", do_cont, 
1104       "", "resume emulation", },
1105 #ifdef CONFIG_GDBSTUB
1106     { "gdbserver", "i?", do_gdbserver, 
1107       "[port]", "start gdbserver session (default port=1234)", },
1108 #endif
1109     { "x", "/l", do_memory_dump, 
1110       "/fmt addr", "virtual memory dump starting at 'addr'", },
1111     { "xp", "/l", do_physical_memory_dump, 
1112       "/fmt addr", "physical memory dump starting at 'addr'", },
1113     { "p|print", "/l", do_print, 
1114       "/fmt expr", "print expression value (use $reg for CPU register access)", },
1115     { "i", "/ii.", do_ioport_read, 
1116       "/fmt addr", "I/O port read" },
1117
1118     { "sendkey", "s", do_send_key, 
1119       "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1120     { "system_reset", "", do_system_reset, 
1121       "", "reset the system" },
1122     { "system_powerdown", "", do_system_powerdown, 
1123       "", "send system power down event" },
1124     { "sum", "ii", do_sum, 
1125       "addr size", "compute the checksum of a memory region" },
1126     { "usb_add", "s", do_usb_add,
1127       "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1128     { "usb_del", "s", do_usb_del,
1129       "device", "remove USB device 'bus.addr'" },
1130     { "cpu", "i", do_cpu_set, 
1131       "index", "set the default CPU" },
1132     { "mouse_move", "sss?", do_mouse_move, 
1133       "dx dy [dz]", "send mouse move events" },
1134     { "mouse_button", "i", do_mouse_button, 
1135       "state", "change mouse button state (1=L, 2=M, 4=R)" },
1136     { NULL, NULL, }, 
1137 };
1138
1139 static term_cmd_t info_cmds[] = {
1140     { "version", "", do_info_version,
1141       "", "show the version of qemu" },
1142     { "network", "", do_info_network,
1143       "", "show the network state" },
1144     { "block", "", do_info_block,
1145       "", "show the block devices" },
1146     { "registers", "", do_info_registers,
1147       "", "show the cpu registers" },
1148     { "cpus", "", do_info_cpus,
1149       "", "show infos for each CPU" },
1150     { "history", "", do_info_history,
1151       "", "show the command line history", },
1152     { "irq", "", irq_info,
1153       "", "show the interrupts statistics (if available)", },
1154     { "pic", "", pic_info,
1155       "", "show i8259 (PIC) state", },
1156     { "pci", "", pci_info,
1157       "", "show PCI info", },
1158 #if defined(TARGET_I386)
1159     { "tlb", "", tlb_info,
1160       "", "show virtual to physical memory mappings", },
1161     { "mem", "", mem_info,
1162       "", "show the active virtual memory mappings", },
1163 #endif
1164     { "jit", "", do_info_jit,
1165       "", "show dynamic compiler info", },
1166     { "kqemu", "", do_info_kqemu,
1167       "", "show kqemu information", },
1168     { "usb", "", usb_info,
1169       "", "show guest USB devices", },
1170     { "usbhost", "", usb_host_info,
1171       "", "show host USB devices", },
1172     { "profile", "", do_info_profile,
1173       "", "show profiling information", },
1174     { NULL, NULL, },
1175 };
1176
1177 /*******************************************************************/
1178
1179 static const char *pch;
1180 static jmp_buf expr_env;
1181
1182 #define MD_TLONG 0
1183 #define MD_I32   1
1184
1185 typedef struct MonitorDef {
1186     const char *name;
1187     int offset;
1188     target_long (*get_value)(struct MonitorDef *md, int val);
1189     int type;
1190 } MonitorDef;
1191
1192 #if defined(TARGET_I386)
1193 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1194 {
1195     CPUState *env = mon_get_cpu();
1196     if (!env)
1197         return 0;
1198     return env->eip + env->segs[R_CS].base;
1199 }
1200 #endif
1201
1202 #if defined(TARGET_PPC)
1203 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1204 {
1205     CPUState *env = mon_get_cpu();
1206     unsigned int u;
1207     int i;
1208
1209     if (!env)
1210         return 0;
1211
1212     u = 0;
1213     for (i = 0; i < 8; i++)
1214         u |= env->crf[i] << (32 - (4 * i));
1215
1216     return u;
1217 }
1218
1219 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1220 {
1221     CPUState *env = mon_get_cpu();
1222     if (!env)
1223         return 0;
1224     return (env->msr[MSR_POW] << MSR_POW) |
1225         (env->msr[MSR_ILE] << MSR_ILE) |
1226         (env->msr[MSR_EE] << MSR_EE) |
1227         (env->msr[MSR_PR] << MSR_PR) |
1228         (env->msr[MSR_FP] << MSR_FP) |
1229         (env->msr[MSR_ME] << MSR_ME) |
1230         (env->msr[MSR_FE0] << MSR_FE0) |
1231         (env->msr[MSR_SE] << MSR_SE) |
1232         (env->msr[MSR_BE] << MSR_BE) |
1233         (env->msr[MSR_FE1] << MSR_FE1) |
1234         (env->msr[MSR_IP] << MSR_IP) |
1235         (env->msr[MSR_IR] << MSR_IR) |
1236         (env->msr[MSR_DR] << MSR_DR) |
1237         (env->msr[MSR_RI] << MSR_RI) |
1238         (env->msr[MSR_LE] << MSR_LE);
1239 }
1240
1241 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1242 {
1243     CPUState *env = mon_get_cpu();
1244     if (!env)
1245         return 0;
1246     return (env->xer[XER_SO] << XER_SO) |
1247         (env->xer[XER_OV] << XER_OV) |
1248         (env->xer[XER_CA] << XER_CA) |
1249         (env->xer[XER_BC] << XER_BC);
1250 }
1251
1252 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1253 {
1254     CPUState *env = mon_get_cpu();
1255     if (!env)
1256         return 0;
1257     return cpu_ppc_load_decr(env);
1258 }
1259
1260 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1261 {
1262     CPUState *env = mon_get_cpu();
1263     if (!env)
1264         return 0;
1265     return cpu_ppc_load_tbu(env);
1266 }
1267
1268 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1269 {
1270     CPUState *env = mon_get_cpu();
1271     if (!env)
1272         return 0;
1273     return cpu_ppc_load_tbl(env);
1274 }
1275 #endif
1276
1277 #if defined(TARGET_SPARC)
1278 #ifndef TARGET_SPARC64
1279 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1280 {
1281     CPUState *env = mon_get_cpu();
1282     if (!env)
1283         return 0;
1284     return GET_PSR(env);
1285 }
1286 #endif
1287
1288 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1289 {
1290     CPUState *env = mon_get_cpu();
1291     if (!env)
1292         return 0;
1293     return env->regwptr[val];
1294 }
1295 #endif
1296
1297 static MonitorDef monitor_defs[] = {
1298 #ifdef TARGET_I386
1299
1300 #define SEG(name, seg) \
1301     { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1302     { name ".base", offsetof(CPUState, segs[seg].base) },\
1303     { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1304
1305     { "eax", offsetof(CPUState, regs[0]) },
1306     { "ecx", offsetof(CPUState, regs[1]) },
1307     { "edx", offsetof(CPUState, regs[2]) },
1308     { "ebx", offsetof(CPUState, regs[3]) },
1309     { "esp|sp", offsetof(CPUState, regs[4]) },
1310     { "ebp|fp", offsetof(CPUState, regs[5]) },
1311     { "esi", offsetof(CPUState, regs[6]) },
1312     { "edi", offsetof(CPUState, regs[7]) },
1313 #ifdef TARGET_X86_64
1314     { "r8", offsetof(CPUState, regs[8]) },
1315     { "r9", offsetof(CPUState, regs[9]) },
1316     { "r10", offsetof(CPUState, regs[10]) },
1317     { "r11", offsetof(CPUState, regs[11]) },
1318     { "r12", offsetof(CPUState, regs[12]) },
1319     { "r13", offsetof(CPUState, regs[13]) },
1320     { "r14", offsetof(CPUState, regs[14]) },
1321     { "r15", offsetof(CPUState, regs[15]) },
1322 #endif
1323     { "eflags", offsetof(CPUState, eflags) },
1324     { "eip", offsetof(CPUState, eip) },
1325     SEG("cs", R_CS)
1326     SEG("ds", R_DS)
1327     SEG("es", R_ES)
1328     SEG("ss", R_SS)
1329     SEG("fs", R_FS)
1330     SEG("gs", R_GS)
1331     { "pc", 0, monitor_get_pc, },
1332 #elif defined(TARGET_PPC)
1333     { "r0", offsetof(CPUState, gpr[0]) },
1334     { "r1", offsetof(CPUState, gpr[1]) },
1335     { "r2", offsetof(CPUState, gpr[2]) },
1336     { "r3", offsetof(CPUState, gpr[3]) },
1337     { "r4", offsetof(CPUState, gpr[4]) },
1338     { "r5", offsetof(CPUState, gpr[5]) },
1339     { "r6", offsetof(CPUState, gpr[6]) },
1340     { "r7", offsetof(CPUState, gpr[7]) },
1341     { "r8", offsetof(CPUState, gpr[8]) },
1342     { "r9", offsetof(CPUState, gpr[9]) },
1343     { "r10", offsetof(CPUState, gpr[10]) },
1344     { "r11", offsetof(CPUState, gpr[11]) },
1345     { "r12", offsetof(CPUState, gpr[12]) },
1346     { "r13", offsetof(CPUState, gpr[13]) },
1347     { "r14", offsetof(CPUState, gpr[14]) },
1348     { "r15", offsetof(CPUState, gpr[15]) },
1349     { "r16", offsetof(CPUState, gpr[16]) },
1350     { "r17", offsetof(CPUState, gpr[17]) },
1351     { "r18", offsetof(CPUState, gpr[18]) },
1352     { "r19", offsetof(CPUState, gpr[19]) },
1353     { "r20", offsetof(CPUState, gpr[20]) },
1354     { "r21", offsetof(CPUState, gpr[21]) },
1355     { "r22", offsetof(CPUState, gpr[22]) },
1356     { "r23", offsetof(CPUState, gpr[23]) },
1357     { "r24", offsetof(CPUState, gpr[24]) },
1358     { "r25", offsetof(CPUState, gpr[25]) },
1359     { "r26", offsetof(CPUState, gpr[26]) },
1360     { "r27", offsetof(CPUState, gpr[27]) },
1361     { "r28", offsetof(CPUState, gpr[28]) },
1362     { "r29", offsetof(CPUState, gpr[29]) },
1363     { "r30", offsetof(CPUState, gpr[30]) },
1364     { "r31", offsetof(CPUState, gpr[31]) },
1365     { "nip|pc", offsetof(CPUState, nip) },
1366     { "lr", offsetof(CPUState, lr) },
1367     { "ctr", offsetof(CPUState, ctr) },
1368     { "decr", 0, &monitor_get_decr, },
1369     { "ccr", 0, &monitor_get_ccr, },
1370     { "msr", 0, &monitor_get_msr, },
1371     { "xer", 0, &monitor_get_xer, },
1372     { "tbu", 0, &monitor_get_tbu, },
1373     { "tbl", 0, &monitor_get_tbl, },
1374     { "sdr1", offsetof(CPUState, sdr1) },
1375     { "sr0", offsetof(CPUState, sr[0]) },
1376     { "sr1", offsetof(CPUState, sr[1]) },
1377     { "sr2", offsetof(CPUState, sr[2]) },
1378     { "sr3", offsetof(CPUState, sr[3]) },
1379     { "sr4", offsetof(CPUState, sr[4]) },
1380     { "sr5", offsetof(CPUState, sr[5]) },
1381     { "sr6", offsetof(CPUState, sr[6]) },
1382     { "sr7", offsetof(CPUState, sr[7]) },
1383     { "sr8", offsetof(CPUState, sr[8]) },
1384     { "sr9", offsetof(CPUState, sr[9]) },
1385     { "sr10", offsetof(CPUState, sr[10]) },
1386     { "sr11", offsetof(CPUState, sr[11]) },
1387     { "sr12", offsetof(CPUState, sr[12]) },
1388     { "sr13", offsetof(CPUState, sr[13]) },
1389     { "sr14", offsetof(CPUState, sr[14]) },
1390     { "sr15", offsetof(CPUState, sr[15]) },
1391     /* Too lazy to put BATs and SPRs ... */
1392 #elif defined(TARGET_SPARC)
1393     { "g0", offsetof(CPUState, gregs[0]) },
1394     { "g1", offsetof(CPUState, gregs[1]) },
1395     { "g2", offsetof(CPUState, gregs[2]) },
1396     { "g3", offsetof(CPUState, gregs[3]) },
1397     { "g4", offsetof(CPUState, gregs[4]) },
1398     { "g5", offsetof(CPUState, gregs[5]) },
1399     { "g6", offsetof(CPUState, gregs[6]) },
1400     { "g7", offsetof(CPUState, gregs[7]) },
1401     { "o0", 0, monitor_get_reg },
1402     { "o1", 1, monitor_get_reg },
1403     { "o2", 2, monitor_get_reg },
1404     { "o3", 3, monitor_get_reg },
1405     { "o4", 4, monitor_get_reg },
1406     { "o5", 5, monitor_get_reg },
1407     { "o6", 6, monitor_get_reg },
1408     { "o7", 7, monitor_get_reg },
1409     { "l0", 8, monitor_get_reg },
1410     { "l1", 9, monitor_get_reg },
1411     { "l2", 10, monitor_get_reg },
1412     { "l3", 11, monitor_get_reg },
1413     { "l4", 12, monitor_get_reg },
1414     { "l5", 13, monitor_get_reg },
1415     { "l6", 14, monitor_get_reg },
1416     { "l7", 15, monitor_get_reg },
1417     { "i0", 16, monitor_get_reg },
1418     { "i1", 17, monitor_get_reg },
1419     { "i2", 18, monitor_get_reg },
1420     { "i3", 19, monitor_get_reg },
1421     { "i4", 20, monitor_get_reg },
1422     { "i5", 21, monitor_get_reg },
1423     { "i6", 22, monitor_get_reg },
1424     { "i7", 23, monitor_get_reg },
1425     { "pc", offsetof(CPUState, pc) },
1426     { "npc", offsetof(CPUState, npc) },
1427     { "y", offsetof(CPUState, y) },
1428 #ifndef TARGET_SPARC64
1429     { "psr", 0, &monitor_get_psr, },
1430     { "wim", offsetof(CPUState, wim) },
1431 #endif
1432     { "tbr", offsetof(CPUState, tbr) },
1433     { "fsr", offsetof(CPUState, fsr) },
1434     { "f0", offsetof(CPUState, fpr[0]) },
1435     { "f1", offsetof(CPUState, fpr[1]) },
1436     { "f2", offsetof(CPUState, fpr[2]) },
1437     { "f3", offsetof(CPUState, fpr[3]) },
1438     { "f4", offsetof(CPUState, fpr[4]) },
1439     { "f5", offsetof(CPUState, fpr[5]) },
1440     { "f6", offsetof(CPUState, fpr[6]) },
1441     { "f7", offsetof(CPUState, fpr[7]) },
1442     { "f8", offsetof(CPUState, fpr[8]) },
1443     { "f9", offsetof(CPUState, fpr[9]) },
1444     { "f10", offsetof(CPUState, fpr[10]) },
1445     { "f11", offsetof(CPUState, fpr[11]) },
1446     { "f12", offsetof(CPUState, fpr[12]) },
1447     { "f13", offsetof(CPUState, fpr[13]) },
1448     { "f14", offsetof(CPUState, fpr[14]) },
1449     { "f15", offsetof(CPUState, fpr[15]) },
1450     { "f16", offsetof(CPUState, fpr[16]) },
1451     { "f17", offsetof(CPUState, fpr[17]) },
1452     { "f18", offsetof(CPUState, fpr[18]) },
1453     { "f19", offsetof(CPUState, fpr[19]) },
1454     { "f20", offsetof(CPUState, fpr[20]) },
1455     { "f21", offsetof(CPUState, fpr[21]) },
1456     { "f22", offsetof(CPUState, fpr[22]) },
1457     { "f23", offsetof(CPUState, fpr[23]) },
1458     { "f24", offsetof(CPUState, fpr[24]) },
1459     { "f25", offsetof(CPUState, fpr[25]) },
1460     { "f26", offsetof(CPUState, fpr[26]) },
1461     { "f27", offsetof(CPUState, fpr[27]) },
1462     { "f28", offsetof(CPUState, fpr[28]) },
1463     { "f29", offsetof(CPUState, fpr[29]) },
1464     { "f30", offsetof(CPUState, fpr[30]) },
1465     { "f31", offsetof(CPUState, fpr[31]) },
1466 #ifdef TARGET_SPARC64
1467     { "f32", offsetof(CPUState, fpr[32]) },
1468     { "f34", offsetof(CPUState, fpr[34]) },
1469     { "f36", offsetof(CPUState, fpr[36]) },
1470     { "f38", offsetof(CPUState, fpr[38]) },
1471     { "f40", offsetof(CPUState, fpr[40]) },
1472     { "f42", offsetof(CPUState, fpr[42]) },
1473     { "f44", offsetof(CPUState, fpr[44]) },
1474     { "f46", offsetof(CPUState, fpr[46]) },
1475     { "f48", offsetof(CPUState, fpr[48]) },
1476     { "f50", offsetof(CPUState, fpr[50]) },
1477     { "f52", offsetof(CPUState, fpr[52]) },
1478     { "f54", offsetof(CPUState, fpr[54]) },
1479     { "f56", offsetof(CPUState, fpr[56]) },
1480     { "f58", offsetof(CPUState, fpr[58]) },
1481     { "f60", offsetof(CPUState, fpr[60]) },
1482     { "f62", offsetof(CPUState, fpr[62]) },
1483     { "asi", offsetof(CPUState, asi) },
1484     { "pstate", offsetof(CPUState, pstate) },
1485     { "cansave", offsetof(CPUState, cansave) },
1486     { "canrestore", offsetof(CPUState, canrestore) },
1487     { "otherwin", offsetof(CPUState, otherwin) },
1488     { "wstate", offsetof(CPUState, wstate) },
1489     { "cleanwin", offsetof(CPUState, cleanwin) },
1490     { "fprs", offsetof(CPUState, fprs) },
1491 #endif
1492 #endif
1493     { NULL },
1494 };
1495
1496 static void expr_error(const char *fmt) 
1497 {
1498     term_printf(fmt);
1499     term_printf("\n");
1500     longjmp(expr_env, 1);
1501 }
1502
1503 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1504 static int get_monitor_def(target_long *pval, const char *name)
1505 {
1506     MonitorDef *md;
1507     void *ptr;
1508
1509     for(md = monitor_defs; md->name != NULL; md++) {
1510         if (compare_cmd(name, md->name)) {
1511             if (md->get_value) {
1512                 *pval = md->get_value(md, md->offset);
1513             } else {
1514                 CPUState *env = mon_get_cpu();
1515                 if (!env)
1516                     return -2;
1517                 ptr = (uint8_t *)env + md->offset;
1518                 switch(md->type) {
1519                 case MD_I32:
1520                     *pval = *(int32_t *)ptr;
1521                     break;
1522                 case MD_TLONG:
1523                     *pval = *(target_long *)ptr;
1524                     break;
1525                 default:
1526                     *pval = 0;
1527                     break;
1528                 }
1529             }
1530             return 0;
1531         }
1532     }
1533     return -1;
1534 }
1535
1536 static void next(void)
1537 {
1538     if (pch != '\0') {
1539         pch++;
1540         while (isspace(*pch))
1541             pch++;
1542     }
1543 }
1544
1545 static target_long expr_sum(void);
1546
1547 static target_long expr_unary(void)
1548 {
1549     target_long n;
1550     char *p;
1551     int ret;
1552
1553     switch(*pch) {
1554     case '+':
1555         next();
1556         n = expr_unary();
1557         break;
1558     case '-':
1559         next();
1560         n = -expr_unary();
1561         break;
1562     case '~':
1563         next();
1564         n = ~expr_unary();
1565         break;
1566     case '(':
1567         next();
1568         n = expr_sum();
1569         if (*pch != ')') {
1570             expr_error("')' expected");
1571         }
1572         next();
1573         break;
1574     case '\'':
1575         pch++;
1576         if (*pch == '\0')
1577             expr_error("character constant expected");
1578         n = *pch;
1579         pch++;
1580         if (*pch != '\'')
1581             expr_error("missing terminating \' character");
1582         next();
1583         break;
1584     case '$':
1585         {
1586             char buf[128], *q;
1587             
1588             pch++;
1589             q = buf;
1590             while ((*pch >= 'a' && *pch <= 'z') ||
1591                    (*pch >= 'A' && *pch <= 'Z') ||
1592                    (*pch >= '0' && *pch <= '9') ||
1593                    *pch == '_' || *pch == '.') {
1594                 if ((q - buf) < sizeof(buf) - 1)
1595                     *q++ = *pch;
1596                 pch++;
1597             }
1598             while (isspace(*pch))
1599                 pch++;
1600             *q = 0;
1601             ret = get_monitor_def(&n, buf);
1602             if (ret == -1)
1603                 expr_error("unknown register");
1604             else if (ret == -2) 
1605                 expr_error("no cpu defined");
1606         }
1607         break;
1608     case '\0':
1609         expr_error("unexpected end of expression");
1610         n = 0;
1611         break;
1612     default:
1613 #if TARGET_LONG_BITS == 64
1614         n = strtoull(pch, &p, 0);
1615 #else
1616         n = strtoul(pch, &p, 0);
1617 #endif
1618         if (pch == p) {
1619             expr_error("invalid char in expression");
1620         }
1621         pch = p;
1622         while (isspace(*pch))
1623             pch++;
1624         break;
1625     }
1626     return n;
1627 }
1628
1629
1630 static target_long expr_prod(void)
1631 {
1632     target_long val, val2;
1633     int op;
1634     
1635     val = expr_unary();
1636     for(;;) {
1637         op = *pch;
1638         if (op != '*' && op != '/' && op != '%')
1639             break;
1640         next();
1641         val2 = expr_unary();
1642         switch(op) {
1643         default:
1644         case '*':
1645             val *= val2;
1646             break;
1647         case '/':
1648         case '%':
1649             if (val2 == 0) 
1650                 expr_error("division by zero");
1651             if (op == '/')
1652                 val /= val2;
1653             else
1654                 val %= val2;
1655             break;
1656         }
1657     }
1658     return val;
1659 }
1660
1661 static target_long expr_logic(void)
1662 {
1663     target_long val, val2;
1664     int op;
1665
1666     val = expr_prod();
1667     for(;;) {
1668         op = *pch;
1669         if (op != '&' && op != '|' && op != '^')
1670             break;
1671         next();
1672         val2 = expr_prod();
1673         switch(op) {
1674         default:
1675         case '&':
1676             val &= val2;
1677             break;
1678         case '|':
1679             val |= val2;
1680             break;
1681         case '^':
1682             val ^= val2;
1683             break;
1684         }
1685     }
1686     return val;
1687 }
1688
1689 static target_long expr_sum(void)
1690 {
1691     target_long val, val2;
1692     int op;
1693
1694     val = expr_logic();
1695     for(;;) {
1696         op = *pch;
1697         if (op != '+' && op != '-')
1698             break;
1699         next();
1700         val2 = expr_logic();
1701         if (op == '+')
1702             val += val2;
1703         else
1704             val -= val2;
1705     }
1706     return val;
1707 }
1708
1709 static int get_expr(target_long *pval, const char **pp)
1710 {
1711     pch = *pp;
1712     if (setjmp(expr_env)) {
1713         *pp = pch;
1714         return -1;
1715     }
1716     while (isspace(*pch))
1717         pch++;
1718     *pval = expr_sum();
1719     *pp = pch;
1720     return 0;
1721 }
1722
1723 static int get_str(char *buf, int buf_size, const char **pp)
1724 {
1725     const char *p;
1726     char *q;
1727     int c;
1728
1729     q = buf;
1730     p = *pp;
1731     while (isspace(*p))
1732         p++;
1733     if (*p == '\0') {
1734     fail:
1735         *q = '\0';
1736         *pp = p;
1737         return -1;
1738     }
1739     if (*p == '\"') {
1740         p++;
1741         while (*p != '\0' && *p != '\"') {
1742             if (*p == '\\') {
1743                 p++;
1744                 c = *p++;
1745                 switch(c) {
1746                 case 'n':
1747                     c = '\n';
1748                     break;
1749                 case 'r':
1750                     c = '\r';
1751                     break;
1752                 case '\\':
1753                 case '\'':
1754                 case '\"':
1755                     break;
1756                 default:
1757                     qemu_printf("unsupported escape code: '\\%c'\n", c);
1758                     goto fail;
1759                 }
1760                 if ((q - buf) < buf_size - 1) {
1761                     *q++ = c;
1762                 }
1763             } else {
1764                 if ((q - buf) < buf_size - 1) {
1765                     *q++ = *p;
1766                 }
1767                 p++;
1768             }
1769         }
1770         if (*p != '\"') {
1771             qemu_printf("unterminated string\n");
1772             goto fail;
1773         }
1774         p++;
1775     } else {
1776         while (*p != '\0' && !isspace(*p)) {
1777             if ((q - buf) < buf_size - 1) {
1778                 *q++ = *p;
1779             }
1780             p++;
1781         }
1782     }
1783     *q = '\0';
1784     *pp = p;
1785     return 0;
1786 }
1787
1788 static int default_fmt_format = 'x';
1789 static int default_fmt_size = 4;
1790
1791 #define MAX_ARGS 16
1792
1793 static void monitor_handle_command(const char *cmdline)
1794 {
1795     const char *p, *pstart, *typestr;
1796     char *q;
1797     int c, nb_args, len, i, has_arg;
1798     term_cmd_t *cmd;
1799     char cmdname[256];
1800     char buf[1024];
1801     void *str_allocated[MAX_ARGS];
1802     void *args[MAX_ARGS];
1803
1804 #ifdef DEBUG
1805     term_printf("command='%s'\n", cmdline);
1806 #endif
1807     
1808     /* extract the command name */
1809     p = cmdline;
1810     q = cmdname;
1811     while (isspace(*p))
1812         p++;
1813     if (*p == '\0')
1814         return;
1815     pstart = p;
1816     while (*p != '\0' && *p != '/' && !isspace(*p))
1817         p++;
1818     len = p - pstart;
1819     if (len > sizeof(cmdname) - 1)
1820         len = sizeof(cmdname) - 1;
1821     memcpy(cmdname, pstart, len);
1822     cmdname[len] = '\0';
1823     
1824     /* find the command */
1825     for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1826         if (compare_cmd(cmdname, cmd->name)) 
1827             goto found;
1828     }
1829     term_printf("unknown command: '%s'\n", cmdname);
1830     return;
1831  found:
1832
1833     for(i = 0; i < MAX_ARGS; i++)
1834         str_allocated[i] = NULL;
1835     
1836     /* parse the parameters */
1837     typestr = cmd->args_type;
1838     nb_args = 0;
1839     for(;;) {
1840         c = *typestr;
1841         if (c == '\0')
1842             break;
1843         typestr++;
1844         switch(c) {
1845         case 'F':
1846         case 'B':
1847         case 's':
1848             {
1849                 int ret;
1850                 char *str;
1851                 
1852                 while (isspace(*p)) 
1853                     p++;
1854                 if (*typestr == '?') {
1855                     typestr++;
1856                     if (*p == '\0') {
1857                         /* no optional string: NULL argument */
1858                         str = NULL;
1859                         goto add_str;
1860                     }
1861                 }
1862                 ret = get_str(buf, sizeof(buf), &p);
1863                 if (ret < 0) {
1864                     switch(c) {
1865                     case 'F':
1866                         term_printf("%s: filename expected\n", cmdname);
1867                         break;
1868                     case 'B':
1869                         term_printf("%s: block device name expected\n", cmdname);
1870                         break;
1871                     default:
1872                         term_printf("%s: string expected\n", cmdname);
1873                         break;
1874                     }
1875                     goto fail;
1876                 }
1877                 str = qemu_malloc(strlen(buf) + 1);
1878                 strcpy(str, buf);
1879                 str_allocated[nb_args] = str;
1880             add_str:
1881                 if (nb_args >= MAX_ARGS) {
1882                 error_args:
1883                     term_printf("%s: too many arguments\n", cmdname);
1884                     goto fail;
1885                 }
1886                 args[nb_args++] = str;
1887             }
1888             break;
1889         case '/':
1890             {
1891                 int count, format, size;
1892                 
1893                 while (isspace(*p))
1894                     p++;
1895                 if (*p == '/') {
1896                     /* format found */
1897                     p++;
1898                     count = 1;
1899                     if (isdigit(*p)) {
1900                         count = 0;
1901                         while (isdigit(*p)) {
1902                             count = count * 10 + (*p - '0');
1903                             p++;
1904                         }
1905                     }
1906                     size = -1;
1907                     format = -1;
1908                     for(;;) {
1909                         switch(*p) {
1910                         case 'o':
1911                         case 'd':
1912                         case 'u':
1913                         case 'x':
1914                         case 'i':
1915                         case 'c':
1916                             format = *p++;
1917                             break;
1918                         case 'b':
1919                             size = 1;
1920                             p++;
1921                             break;
1922                         case 'h':
1923                             size = 2;
1924                             p++;
1925                             break;
1926                         case 'w':
1927                             size = 4;
1928                             p++;
1929                             break;
1930                         case 'g':
1931                         case 'L':
1932                             size = 8;
1933                             p++;
1934                             break;
1935                         default:
1936                             goto next;
1937                         }
1938                     }
1939                 next:
1940                     if (*p != '\0' && !isspace(*p)) {
1941                         term_printf("invalid char in format: '%c'\n", *p);
1942                         goto fail;
1943                     }
1944                     if (format < 0)
1945                         format = default_fmt_format;
1946                     if (format != 'i') {
1947                         /* for 'i', not specifying a size gives -1 as size */
1948                         if (size < 0)
1949                             size = default_fmt_size;
1950                     }
1951                     default_fmt_size = size;
1952                     default_fmt_format = format;
1953                 } else {
1954                     count = 1;
1955                     format = default_fmt_format;
1956                     if (format != 'i') {
1957                         size = default_fmt_size;
1958                     } else {
1959                         size = -1;
1960                     }
1961                 }
1962                 if (nb_args + 3 > MAX_ARGS)
1963                     goto error_args;
1964                 args[nb_args++] = (void*)count;
1965                 args[nb_args++] = (void*)format;
1966                 args[nb_args++] = (void*)size;
1967             }
1968             break;
1969         case 'i':
1970         case 'l':
1971             {
1972                 target_long val;
1973                 while (isspace(*p)) 
1974                     p++;
1975                 if (*typestr == '?' || *typestr == '.') {
1976                     if (*typestr == '?') {
1977                         if (*p == '\0')
1978                             has_arg = 0;
1979                         else
1980                             has_arg = 1;
1981                     } else {
1982                         if (*p == '.') {
1983                             p++;
1984                             while (isspace(*p)) 
1985                                 p++;
1986                             has_arg = 1;
1987                         } else {
1988                             has_arg = 0;
1989                         }
1990                     }
1991                     typestr++;
1992                     if (nb_args >= MAX_ARGS)
1993                         goto error_args;
1994                     args[nb_args++] = (void *)has_arg;
1995                     if (!has_arg) {
1996                         if (nb_args >= MAX_ARGS)
1997                             goto error_args;
1998                         val = -1;
1999                         goto add_num;
2000                     }
2001                 }
2002                 if (get_expr(&val, &p))
2003                     goto fail;
2004             add_num:
2005                 if (c == 'i') {
2006                     if (nb_args >= MAX_ARGS)
2007                         goto error_args;
2008                     args[nb_args++] = (void *)(int)val;
2009                 } else {
2010                     if ((nb_args + 1) >= MAX_ARGS)
2011                         goto error_args;
2012 #if TARGET_LONG_BITS == 64
2013                     args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2014 #else
2015                     args[nb_args++] = (void *)0;
2016 #endif
2017                     args[nb_args++] = (void *)(int)(val & 0xffffffff);
2018                 }
2019             }
2020             break;
2021         case '-':
2022             {
2023                 int has_option;
2024                 /* option */
2025                 
2026                 c = *typestr++;
2027                 if (c == '\0')
2028                     goto bad_type;
2029                 while (isspace(*p)) 
2030                     p++;
2031                 has_option = 0;
2032                 if (*p == '-') {
2033                     p++;
2034                     if (*p != c) {
2035                         term_printf("%s: unsupported option -%c\n", 
2036                                     cmdname, *p);
2037                         goto fail;
2038                     }
2039                     p++;
2040                     has_option = 1;
2041                 }
2042                 if (nb_args >= MAX_ARGS)
2043                     goto error_args;
2044                 args[nb_args++] = (void *)has_option;
2045             }
2046             break;
2047         default:
2048         bad_type:
2049             term_printf("%s: unknown type '%c'\n", cmdname, c);
2050             goto fail;
2051         }
2052     }
2053     /* check that all arguments were parsed */
2054     while (isspace(*p))
2055         p++;
2056     if (*p != '\0') {
2057         term_printf("%s: extraneous characters at the end of line\n", 
2058                     cmdname);
2059         goto fail;
2060     }
2061
2062     switch(nb_args) {
2063     case 0:
2064         cmd->handler();
2065         break;
2066     case 1:
2067         cmd->handler(args[0]);
2068         break;
2069     case 2:
2070         cmd->handler(args[0], args[1]);
2071         break;
2072     case 3:
2073         cmd->handler(args[0], args[1], args[2]);
2074         break;
2075     case 4:
2076         cmd->handler(args[0], args[1], args[2], args[3]);
2077         break;
2078     case 5:
2079         cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2080         break;
2081     case 6:
2082         cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2083         break;
2084     default:
2085         term_printf("unsupported number of arguments: %d\n", nb_args);
2086         goto fail;
2087     }
2088  fail:
2089     for(i = 0; i < MAX_ARGS; i++)
2090         qemu_free(str_allocated[i]);
2091     return;
2092 }
2093
2094 static void cmd_completion(const char *name, const char *list)
2095 {
2096     const char *p, *pstart;
2097     char cmd[128];
2098     int len;
2099
2100     p = list;
2101     for(;;) {
2102         pstart = p;
2103         p = strchr(p, '|');
2104         if (!p)
2105             p = pstart + strlen(pstart);
2106         len = p - pstart;
2107         if (len > sizeof(cmd) - 2)
2108             len = sizeof(cmd) - 2;
2109         memcpy(cmd, pstart, len);
2110         cmd[len] = '\0';
2111         if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2112             add_completion(cmd);
2113         }
2114         if (*p == '\0')
2115             break;
2116         p++;
2117     }
2118 }
2119
2120 static void file_completion(const char *input)
2121 {
2122     DIR *ffs;
2123     struct dirent *d;
2124     char path[1024];
2125     char file[1024], file_prefix[1024];
2126     int input_path_len;
2127     const char *p;
2128
2129     p = strrchr(input, '/'); 
2130     if (!p) {
2131         input_path_len = 0;
2132         pstrcpy(file_prefix, sizeof(file_prefix), input);
2133         strcpy(path, ".");
2134     } else {
2135         input_path_len = p - input + 1;
2136         memcpy(path, input, input_path_len);
2137         if (input_path_len > sizeof(path) - 1)
2138             input_path_len = sizeof(path) - 1;
2139         path[input_path_len] = '\0';
2140         pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2141     }
2142 #ifdef DEBUG_COMPLETION
2143     term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2144 #endif
2145     ffs = opendir(path);
2146     if (!ffs)
2147         return;
2148     for(;;) {
2149         struct stat sb;
2150         d = readdir(ffs);
2151         if (!d)
2152             break;
2153         if (strstart(d->d_name, file_prefix, NULL)) {
2154             memcpy(file, input, input_path_len);
2155             strcpy(file + input_path_len, d->d_name);
2156             /* stat the file to find out if it's a directory.
2157              * In that case add a slash to speed up typing long paths
2158              */
2159             stat(file, &sb);
2160             if(S_ISDIR(sb.st_mode))
2161                 strcat(file, "/");
2162             add_completion(file);
2163         }
2164     }
2165     closedir(ffs);
2166 }
2167
2168 static void block_completion_it(void *opaque, const char *name)
2169 {
2170     const char *input = opaque;
2171
2172     if (input[0] == '\0' ||
2173         !strncmp(name, (char *)input, strlen(input))) {
2174         add_completion(name);
2175     }
2176 }
2177
2178 /* NOTE: this parser is an approximate form of the real command parser */
2179 static void parse_cmdline(const char *cmdline,
2180                          int *pnb_args, char **args)
2181 {
2182     const char *p;
2183     int nb_args, ret;
2184     char buf[1024];
2185
2186     p = cmdline;
2187     nb_args = 0;
2188     for(;;) {
2189         while (isspace(*p))
2190             p++;
2191         if (*p == '\0')
2192             break;
2193         if (nb_args >= MAX_ARGS)
2194             break;
2195         ret = get_str(buf, sizeof(buf), &p);
2196         args[nb_args] = qemu_strdup(buf);
2197         nb_args++;
2198         if (ret < 0)
2199             break;
2200     }
2201     *pnb_args = nb_args;
2202 }
2203
2204 void readline_find_completion(const char *cmdline)
2205 {
2206     const char *cmdname;
2207     char *args[MAX_ARGS];
2208     int nb_args, i, len;
2209     const char *ptype, *str;
2210     term_cmd_t *cmd;
2211     const KeyDef *key;
2212
2213     parse_cmdline(cmdline, &nb_args, args);
2214 #ifdef DEBUG_COMPLETION
2215     for(i = 0; i < nb_args; i++) {
2216         term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2217     }
2218 #endif
2219
2220     /* if the line ends with a space, it means we want to complete the
2221        next arg */
2222     len = strlen(cmdline);
2223     if (len > 0 && isspace(cmdline[len - 1])) {
2224         if (nb_args >= MAX_ARGS)
2225             return;
2226         args[nb_args++] = qemu_strdup("");
2227     }
2228     if (nb_args <= 1) {
2229         /* command completion */
2230         if (nb_args == 0)
2231             cmdname = "";
2232         else
2233             cmdname = args[0];
2234         completion_index = strlen(cmdname);
2235         for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2236             cmd_completion(cmdname, cmd->name);
2237         }
2238     } else {
2239         /* find the command */
2240         for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2241             if (compare_cmd(args[0], cmd->name))
2242                 goto found;
2243         }
2244         return;
2245     found:
2246         ptype = cmd->args_type;
2247         for(i = 0; i < nb_args - 2; i++) {
2248             if (*ptype != '\0') {
2249                 ptype++;
2250                 while (*ptype == '?')
2251                     ptype++;
2252             }
2253         }
2254         str = args[nb_args - 1];
2255         switch(*ptype) {
2256         case 'F':
2257             /* file completion */
2258             completion_index = strlen(str);
2259             file_completion(str);
2260             break;
2261         case 'B':
2262             /* block device name completion */
2263             completion_index = strlen(str);
2264             bdrv_iterate(block_completion_it, (void *)str);
2265             break;
2266         case 's':
2267             /* XXX: more generic ? */
2268             if (!strcmp(cmd->name, "info")) {
2269                 completion_index = strlen(str);
2270                 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2271                     cmd_completion(str, cmd->name);
2272                 }
2273             } else if (!strcmp(cmd->name, "sendkey")) {
2274                 completion_index = strlen(str);
2275                 for(key = key_defs; key->name != NULL; key++) {
2276                     cmd_completion(str, key->name);
2277                 }
2278             }
2279             break;
2280         default:
2281             break;
2282         }
2283     }
2284     for(i = 0; i < nb_args; i++)
2285         qemu_free(args[i]);
2286 }
2287
2288 static int term_can_read(void *opaque)
2289 {
2290     return 128;
2291 }
2292
2293 static void term_read(void *opaque, const uint8_t *buf, int size)
2294 {
2295     int i;
2296     for(i = 0; i < size; i++)
2297         readline_handle_byte(buf[i]);
2298 }
2299
2300 static void monitor_start_input(void);
2301
2302 static void monitor_handle_command1(void *opaque, const char *cmdline)
2303 {
2304     monitor_handle_command(cmdline);
2305     monitor_start_input();
2306 }
2307
2308 static void monitor_start_input(void)
2309 {
2310     readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2311 }
2312
2313 void monitor_init(CharDriverState *hd, int show_banner)
2314 {
2315     monitor_hd = hd;
2316     if (show_banner) {
2317         term_printf("QEMU %s monitor - type 'help' for more information\n",
2318                     QEMU_VERSION);
2319     }
2320     qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2321     monitor_start_input();
2322 }
2323
2324 /* XXX: use threads ? */
2325 /* modal monitor readline */
2326 static int monitor_readline_started;
2327 static char *monitor_readline_buf;
2328 static int monitor_readline_buf_size;
2329
2330 static void monitor_readline_cb(void *opaque, const char *input)
2331 {
2332     pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2333     monitor_readline_started = 0;
2334 }
2335
2336 void monitor_readline(const char *prompt, int is_password,
2337                       char *buf, int buf_size)
2338 {
2339     if (is_password) {
2340         qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2341     }
2342     readline_start(prompt, is_password, monitor_readline_cb, NULL);
2343     monitor_readline_buf = buf;
2344     monitor_readline_buf_size = buf_size;
2345     monitor_readline_started = 1;
2346     while (monitor_readline_started) {
2347         main_loop_wait(10);
2348     }
2349 }
This page took 0.155528 seconds and 4 git commands to generate.