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