kqemu info
[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);
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 static void do_system_powerdown(void)
771 {
772     qemu_system_powerdown_request();
773 }
774
775 #if defined(TARGET_I386)
776 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
777 {
778     term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
779                 addr,
780                 pte & mask,
781                 pte & PG_GLOBAL_MASK ? 'G' : '-',
782                 pte & PG_PSE_MASK ? 'P' : '-',
783                 pte & PG_DIRTY_MASK ? 'D' : '-',
784                 pte & PG_ACCESSED_MASK ? 'A' : '-',
785                 pte & PG_PCD_MASK ? 'C' : '-',
786                 pte & PG_PWT_MASK ? 'T' : '-',
787                 pte & PG_USER_MASK ? 'U' : '-',
788                 pte & PG_RW_MASK ? 'W' : '-');
789 }
790
791 static void tlb_info(void)
792 {
793     CPUState *env = cpu_single_env;
794     int l1, l2;
795     uint32_t pgd, pde, pte;
796
797     if (!(env->cr[0] & CR0_PG_MASK)) {
798         term_printf("PG disabled\n");
799         return;
800     }
801     pgd = env->cr[3] & ~0xfff;
802     for(l1 = 0; l1 < 1024; l1++) {
803         cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
804         pde = le32_to_cpu(pde);
805         if (pde & PG_PRESENT_MASK) {
806             if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
807                 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
808             } else {
809                 for(l2 = 0; l2 < 1024; l2++) {
810                     cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
811                                              (uint8_t *)&pte, 4);
812                     pte = le32_to_cpu(pte);
813                     if (pte & PG_PRESENT_MASK) {
814                         print_pte((l1 << 22) + (l2 << 12), 
815                                   pte & ~PG_PSE_MASK, 
816                                   ~0xfff);
817                     }
818                 }
819             }
820         }
821     }
822 }
823
824 static void mem_print(uint32_t *pstart, int *plast_prot, 
825                       uint32_t end, int prot)
826 {
827     int prot1;
828     prot1 = *plast_prot;
829     if (prot != prot1) {
830         if (*pstart != -1) {
831             term_printf("%08x-%08x %08x %c%c%c\n",
832                         *pstart, end, end - *pstart, 
833                         prot1 & PG_USER_MASK ? 'u' : '-',
834                         'r',
835                         prot1 & PG_RW_MASK ? 'w' : '-');
836         }
837         if (prot != 0)
838             *pstart = end;
839         else
840             *pstart = -1;
841         *plast_prot = prot;
842     }
843 }
844
845 static void mem_info(void)
846 {
847     CPUState *env = cpu_single_env;
848     int l1, l2, prot, last_prot;
849     uint32_t pgd, pde, pte, start, end;
850
851     if (!(env->cr[0] & CR0_PG_MASK)) {
852         term_printf("PG disabled\n");
853         return;
854     }
855     pgd = env->cr[3] & ~0xfff;
856     last_prot = 0;
857     start = -1;
858     for(l1 = 0; l1 < 1024; l1++) {
859         cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
860         pde = le32_to_cpu(pde);
861         end = l1 << 22;
862         if (pde & PG_PRESENT_MASK) {
863             if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
864                 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
865                 mem_print(&start, &last_prot, end, prot);
866             } else {
867                 for(l2 = 0; l2 < 1024; l2++) {
868                     cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
869                                              (uint8_t *)&pte, 4);
870                     pte = le32_to_cpu(pte);
871                     end = (l1 << 22) + (l2 << 12);
872                     if (pte & PG_PRESENT_MASK) {
873                         prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
874                     } else {
875                         prot = 0;
876                     }
877                     mem_print(&start, &last_prot, end, prot);
878                 }
879             }
880         } else {
881             prot = 0;
882             mem_print(&start, &last_prot, end, prot);
883         }
884     }
885 }
886 #endif
887
888 static void do_info_kqemu(void)
889 {
890 #ifdef USE_KQEMU
891     int val;
892     val = 0;
893     if (cpu_single_env)
894         val = cpu_single_env->kqemu_enabled;
895     term_printf("kqemu is %s\n", val ? "enabled" : "disabled");
896 #else
897     term_printf("kqemu support is not compiled\n");
898 #endif
899
900
901
902 static term_cmd_t term_cmds[] = {
903     { "help|?", "s?", do_help, 
904       "[cmd]", "show the help" },
905     { "commit", "", do_commit, 
906       "", "commit changes to the disk images (if -snapshot is used)" },
907     { "info", "s?", do_info,
908       "subcommand", "show various information about the system state" },
909     { "q|quit", "", do_quit,
910       "", "quit the emulator" },
911     { "eject", "-fB", do_eject,
912       "[-f] device", "eject a removable media (use -f to force it)" },
913     { "change", "BF", do_change,
914       "device filename", "change a removable media" },
915     { "screendump", "F", do_screen_dump, 
916       "filename", "save screen into PPM image 'filename'" },
917     { "log", "s", do_log,
918       "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
919     { "savevm", "F", do_savevm,
920       "filename", "save the whole virtual machine state to 'filename'" }, 
921     { "loadvm", "F", do_loadvm,
922       "filename", "restore the whole virtual machine state from 'filename'" }, 
923     { "stop", "", do_stop, 
924       "", "stop emulation", },
925     { "c|cont", "", do_cont, 
926       "", "resume emulation", },
927 #ifdef CONFIG_GDBSTUB
928     { "gdbserver", "i?", do_gdbserver, 
929       "[port]", "start gdbserver session (default port=1234)", },
930 #endif
931     { "x", "/l", do_memory_dump, 
932       "/fmt addr", "virtual memory dump starting at 'addr'", },
933     { "xp", "/l", do_physical_memory_dump, 
934       "/fmt addr", "physical memory dump starting at 'addr'", },
935     { "p|print", "/l", do_print, 
936       "/fmt expr", "print expression value (use $reg for CPU register access)", },
937     { "i", "/ii.", do_ioport_read, 
938       "/fmt addr", "I/O port read" },
939
940     { "sendkey", "s", do_send_key, 
941       "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
942     { "system_reset", "", do_system_reset, 
943       "", "reset the system" },
944     { "system_powerdown", "", do_system_powerdown, 
945       "", "send system power down event" },
946     { "sum", "ii", do_sum, 
947       "addr size", "compute the checksum of a memory region" },
948     { NULL, NULL, }, 
949 };
950
951 static term_cmd_t info_cmds[] = {
952     { "version", "", do_info_version,
953       "", "show the version of qemu" },
954     { "network", "", do_info_network,
955       "", "show the network state" },
956     { "block", "", do_info_block,
957       "", "show the block devices" },
958     { "registers", "", do_info_registers,
959       "", "show the cpu registers" },
960     { "history", "", do_info_history,
961       "", "show the command line history", },
962     { "irq", "", irq_info,
963       "", "show the interrupts statistics (if available)", },
964     { "pic", "", pic_info,
965       "", "show i8259 (PIC) state", },
966     { "pci", "", pci_info,
967       "", "show PCI info", },
968 #if defined(TARGET_I386)
969     { "tlb", "", tlb_info,
970       "", "show virtual to physical memory mappings", },
971     { "mem", "", mem_info,
972       "", "show the active virtual memory mappings", },
973 #endif
974     { "jit", "", do_info_jit,
975       "", "show dynamic compiler info", },
976     { "kqemu", "", do_info_kqemu,
977       "", "show kqemu information", },
978     { NULL, NULL, },
979 };
980
981 /*******************************************************************/
982
983 static const char *pch;
984 static jmp_buf expr_env;
985
986 #define MD_TLONG 0
987 #define MD_I32   1
988
989 typedef struct MonitorDef {
990     const char *name;
991     int offset;
992     target_long (*get_value)(struct MonitorDef *md, int val);
993     int type;
994 } MonitorDef;
995
996 #if defined(TARGET_I386)
997 static target_long monitor_get_pc (struct MonitorDef *md, int val)
998 {
999     return cpu_single_env->eip + cpu_single_env->segs[R_CS].base;
1000 }
1001 #endif
1002
1003 #if defined(TARGET_PPC)
1004 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1005 {
1006     unsigned int u;
1007     int i;
1008
1009     u = 0;
1010     for (i = 0; i < 8; i++)
1011         u |= cpu_single_env->crf[i] << (32 - (4 * i));
1012
1013     return u;
1014 }
1015
1016 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1017 {
1018     return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
1019         (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
1020         (cpu_single_env->msr[MSR_EE] << MSR_EE) |
1021         (cpu_single_env->msr[MSR_PR] << MSR_PR) |
1022         (cpu_single_env->msr[MSR_FP] << MSR_FP) |
1023         (cpu_single_env->msr[MSR_ME] << MSR_ME) |
1024         (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
1025         (cpu_single_env->msr[MSR_SE] << MSR_SE) |
1026         (cpu_single_env->msr[MSR_BE] << MSR_BE) |
1027         (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
1028         (cpu_single_env->msr[MSR_IP] << MSR_IP) |
1029         (cpu_single_env->msr[MSR_IR] << MSR_IR) |
1030         (cpu_single_env->msr[MSR_DR] << MSR_DR) |
1031         (cpu_single_env->msr[MSR_RI] << MSR_RI) |
1032         (cpu_single_env->msr[MSR_LE] << MSR_LE);
1033 }
1034
1035 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1036 {
1037     return (cpu_single_env->xer[XER_SO] << XER_SO) |
1038         (cpu_single_env->xer[XER_OV] << XER_OV) |
1039         (cpu_single_env->xer[XER_CA] << XER_CA) |
1040         (cpu_single_env->xer[XER_BC] << XER_BC);
1041 }
1042
1043 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1044 {
1045     return cpu_ppc_load_decr(cpu_single_env);
1046 }
1047
1048 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1049 {
1050     return cpu_ppc_load_tbu(cpu_single_env);
1051 }
1052
1053 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1054 {
1055     return cpu_ppc_load_tbl(cpu_single_env);
1056 }
1057 #endif
1058
1059 #if defined(TARGET_SPARC)
1060 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1061 {
1062     return GET_PSR(cpu_single_env);
1063 }
1064
1065 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1066 {
1067     return cpu_single_env->regwptr[val];
1068 }
1069 #endif
1070
1071 static MonitorDef monitor_defs[] = {
1072 #ifdef TARGET_I386
1073
1074 #define SEG(name, seg) \
1075     { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1076     { name ".base", offsetof(CPUState, segs[seg].base) },\
1077     { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1078
1079     { "eax", offsetof(CPUState, regs[0]) },
1080     { "ecx", offsetof(CPUState, regs[1]) },
1081     { "edx", offsetof(CPUState, regs[2]) },
1082     { "ebx", offsetof(CPUState, regs[3]) },
1083     { "esp|sp", offsetof(CPUState, regs[4]) },
1084     { "ebp|fp", offsetof(CPUState, regs[5]) },
1085     { "esi", offsetof(CPUState, regs[6]) },
1086     { "edi", offsetof(CPUState, regs[7]) },
1087 #ifdef TARGET_X86_64
1088     { "r8", offsetof(CPUState, regs[8]) },
1089     { "r9", offsetof(CPUState, regs[9]) },
1090     { "r10", offsetof(CPUState, regs[10]) },
1091     { "r11", offsetof(CPUState, regs[11]) },
1092     { "r12", offsetof(CPUState, regs[12]) },
1093     { "r13", offsetof(CPUState, regs[13]) },
1094     { "r14", offsetof(CPUState, regs[14]) },
1095     { "r15", offsetof(CPUState, regs[15]) },
1096 #endif
1097     { "eflags", offsetof(CPUState, eflags) },
1098     { "eip", offsetof(CPUState, eip) },
1099     SEG("cs", R_CS)
1100     SEG("ds", R_DS)
1101     SEG("es", R_ES)
1102     SEG("ss", R_SS)
1103     SEG("fs", R_FS)
1104     SEG("gs", R_GS)
1105     { "pc", 0, monitor_get_pc, },
1106 #elif defined(TARGET_PPC)
1107     { "r0", offsetof(CPUState, gpr[0]) },
1108     { "r1", offsetof(CPUState, gpr[1]) },
1109     { "r2", offsetof(CPUState, gpr[2]) },
1110     { "r3", offsetof(CPUState, gpr[3]) },
1111     { "r4", offsetof(CPUState, gpr[4]) },
1112     { "r5", offsetof(CPUState, gpr[5]) },
1113     { "r6", offsetof(CPUState, gpr[6]) },
1114     { "r7", offsetof(CPUState, gpr[7]) },
1115     { "r8", offsetof(CPUState, gpr[8]) },
1116     { "r9", offsetof(CPUState, gpr[9]) },
1117     { "r10", offsetof(CPUState, gpr[10]) },
1118     { "r11", offsetof(CPUState, gpr[11]) },
1119     { "r12", offsetof(CPUState, gpr[12]) },
1120     { "r13", offsetof(CPUState, gpr[13]) },
1121     { "r14", offsetof(CPUState, gpr[14]) },
1122     { "r15", offsetof(CPUState, gpr[15]) },
1123     { "r16", offsetof(CPUState, gpr[16]) },
1124     { "r17", offsetof(CPUState, gpr[17]) },
1125     { "r18", offsetof(CPUState, gpr[18]) },
1126     { "r19", offsetof(CPUState, gpr[19]) },
1127     { "r20", offsetof(CPUState, gpr[20]) },
1128     { "r21", offsetof(CPUState, gpr[21]) },
1129     { "r22", offsetof(CPUState, gpr[22]) },
1130     { "r23", offsetof(CPUState, gpr[23]) },
1131     { "r24", offsetof(CPUState, gpr[24]) },
1132     { "r25", offsetof(CPUState, gpr[25]) },
1133     { "r26", offsetof(CPUState, gpr[26]) },
1134     { "r27", offsetof(CPUState, gpr[27]) },
1135     { "r28", offsetof(CPUState, gpr[28]) },
1136     { "r29", offsetof(CPUState, gpr[29]) },
1137     { "r30", offsetof(CPUState, gpr[30]) },
1138     { "r31", offsetof(CPUState, gpr[31]) },
1139     { "nip|pc", offsetof(CPUState, nip) },
1140     { "lr", offsetof(CPUState, lr) },
1141     { "ctr", offsetof(CPUState, ctr) },
1142     { "decr", 0, &monitor_get_decr, },
1143     { "ccr", 0, &monitor_get_ccr, },
1144     { "msr", 0, &monitor_get_msr, },
1145     { "xer", 0, &monitor_get_xer, },
1146     { "tbu", 0, &monitor_get_tbu, },
1147     { "tbl", 0, &monitor_get_tbl, },
1148     { "sdr1", offsetof(CPUState, sdr1) },
1149     { "sr0", offsetof(CPUState, sr[0]) },
1150     { "sr1", offsetof(CPUState, sr[1]) },
1151     { "sr2", offsetof(CPUState, sr[2]) },
1152     { "sr3", offsetof(CPUState, sr[3]) },
1153     { "sr4", offsetof(CPUState, sr[4]) },
1154     { "sr5", offsetof(CPUState, sr[5]) },
1155     { "sr6", offsetof(CPUState, sr[6]) },
1156     { "sr7", offsetof(CPUState, sr[7]) },
1157     { "sr8", offsetof(CPUState, sr[8]) },
1158     { "sr9", offsetof(CPUState, sr[9]) },
1159     { "sr10", offsetof(CPUState, sr[10]) },
1160     { "sr11", offsetof(CPUState, sr[11]) },
1161     { "sr12", offsetof(CPUState, sr[12]) },
1162     { "sr13", offsetof(CPUState, sr[13]) },
1163     { "sr14", offsetof(CPUState, sr[14]) },
1164     { "sr15", offsetof(CPUState, sr[15]) },
1165     /* Too lazy to put BATs and SPRs ... */
1166 #elif defined(TARGET_SPARC)
1167     { "g0", offsetof(CPUState, gregs[0]) },
1168     { "g1", offsetof(CPUState, gregs[1]) },
1169     { "g2", offsetof(CPUState, gregs[2]) },
1170     { "g3", offsetof(CPUState, gregs[3]) },
1171     { "g4", offsetof(CPUState, gregs[4]) },
1172     { "g5", offsetof(CPUState, gregs[5]) },
1173     { "g6", offsetof(CPUState, gregs[6]) },
1174     { "g7", offsetof(CPUState, gregs[7]) },
1175     { "o0", 0, monitor_get_reg },
1176     { "o1", 1, monitor_get_reg },
1177     { "o2", 2, monitor_get_reg },
1178     { "o3", 3, monitor_get_reg },
1179     { "o4", 4, monitor_get_reg },
1180     { "o5", 5, monitor_get_reg },
1181     { "o6", 6, monitor_get_reg },
1182     { "o7", 7, monitor_get_reg },
1183     { "l0", 8, monitor_get_reg },
1184     { "l1", 9, monitor_get_reg },
1185     { "l2", 10, monitor_get_reg },
1186     { "l3", 11, monitor_get_reg },
1187     { "l4", 12, monitor_get_reg },
1188     { "l5", 13, monitor_get_reg },
1189     { "l6", 14, monitor_get_reg },
1190     { "l7", 15, monitor_get_reg },
1191     { "i0", 16, monitor_get_reg },
1192     { "i1", 17, monitor_get_reg },
1193     { "i2", 18, monitor_get_reg },
1194     { "i3", 19, monitor_get_reg },
1195     { "i4", 20, monitor_get_reg },
1196     { "i5", 21, monitor_get_reg },
1197     { "i6", 22, monitor_get_reg },
1198     { "i7", 23, monitor_get_reg },
1199     { "pc", offsetof(CPUState, pc) },
1200     { "npc", offsetof(CPUState, npc) },
1201     { "y", offsetof(CPUState, y) },
1202     { "psr", 0, &monitor_get_psr, },
1203     { "wim", offsetof(CPUState, wim) },
1204     { "tbr", offsetof(CPUState, tbr) },
1205     { "fsr", offsetof(CPUState, fsr) },
1206     { "f0", offsetof(CPUState, fpr[0]) },
1207     { "f1", offsetof(CPUState, fpr[1]) },
1208     { "f2", offsetof(CPUState, fpr[2]) },
1209     { "f3", offsetof(CPUState, fpr[3]) },
1210     { "f4", offsetof(CPUState, fpr[4]) },
1211     { "f5", offsetof(CPUState, fpr[5]) },
1212     { "f6", offsetof(CPUState, fpr[6]) },
1213     { "f7", offsetof(CPUState, fpr[7]) },
1214     { "f8", offsetof(CPUState, fpr[8]) },
1215     { "f9", offsetof(CPUState, fpr[9]) },
1216     { "f10", offsetof(CPUState, fpr[10]) },
1217     { "f11", offsetof(CPUState, fpr[11]) },
1218     { "f12", offsetof(CPUState, fpr[12]) },
1219     { "f13", offsetof(CPUState, fpr[13]) },
1220     { "f14", offsetof(CPUState, fpr[14]) },
1221     { "f15", offsetof(CPUState, fpr[15]) },
1222     { "f16", offsetof(CPUState, fpr[16]) },
1223     { "f17", offsetof(CPUState, fpr[17]) },
1224     { "f18", offsetof(CPUState, fpr[18]) },
1225     { "f19", offsetof(CPUState, fpr[19]) },
1226     { "f20", offsetof(CPUState, fpr[20]) },
1227     { "f21", offsetof(CPUState, fpr[21]) },
1228     { "f22", offsetof(CPUState, fpr[22]) },
1229     { "f23", offsetof(CPUState, fpr[23]) },
1230     { "f24", offsetof(CPUState, fpr[24]) },
1231     { "f25", offsetof(CPUState, fpr[25]) },
1232     { "f26", offsetof(CPUState, fpr[26]) },
1233     { "f27", offsetof(CPUState, fpr[27]) },
1234     { "f28", offsetof(CPUState, fpr[28]) },
1235     { "f29", offsetof(CPUState, fpr[29]) },
1236     { "f30", offsetof(CPUState, fpr[30]) },
1237     { "f31", offsetof(CPUState, fpr[31]) },
1238 #endif
1239     { NULL },
1240 };
1241
1242 static void expr_error(const char *fmt) 
1243 {
1244     term_printf(fmt);
1245     term_printf("\n");
1246     longjmp(expr_env, 1);
1247 }
1248
1249 static int get_monitor_def(target_long *pval, const char *name)
1250 {
1251     MonitorDef *md;
1252     void *ptr;
1253
1254     for(md = monitor_defs; md->name != NULL; md++) {
1255         if (compare_cmd(name, md->name)) {
1256             if (md->get_value) {
1257                 *pval = md->get_value(md, md->offset);
1258             } else {
1259                 ptr = (uint8_t *)cpu_single_env + md->offset;
1260                 switch(md->type) {
1261                 case MD_I32:
1262                     *pval = *(int32_t *)ptr;
1263                     break;
1264                 case MD_TLONG:
1265                     *pval = *(target_long *)ptr;
1266                     break;
1267                 default:
1268                     *pval = 0;
1269                     break;
1270                 }
1271             }
1272             return 0;
1273         }
1274     }
1275     return -1;
1276 }
1277
1278 static void next(void)
1279 {
1280     if (pch != '\0') {
1281         pch++;
1282         while (isspace(*pch))
1283             pch++;
1284     }
1285 }
1286
1287 static target_long expr_sum(void);
1288
1289 static target_long expr_unary(void)
1290 {
1291     target_long n;
1292     char *p;
1293
1294     switch(*pch) {
1295     case '+':
1296         next();
1297         n = expr_unary();
1298         break;
1299     case '-':
1300         next();
1301         n = -expr_unary();
1302         break;
1303     case '~':
1304         next();
1305         n = ~expr_unary();
1306         break;
1307     case '(':
1308         next();
1309         n = expr_sum();
1310         if (*pch != ')') {
1311             expr_error("')' expected");
1312         }
1313         next();
1314         break;
1315     case '\'':
1316         pch++;
1317         if (*pch == '\0')
1318             expr_error("character constant expected");
1319         n = *pch;
1320         pch++;
1321         if (*pch != '\'')
1322             expr_error("missing terminating \' character");
1323         next();
1324         break;
1325     case '$':
1326         {
1327             char buf[128], *q;
1328             
1329             pch++;
1330             q = buf;
1331             while ((*pch >= 'a' && *pch <= 'z') ||
1332                    (*pch >= 'A' && *pch <= 'Z') ||
1333                    (*pch >= '0' && *pch <= '9') ||
1334                    *pch == '_' || *pch == '.') {
1335                 if ((q - buf) < sizeof(buf) - 1)
1336                     *q++ = *pch;
1337                 pch++;
1338             }
1339             while (isspace(*pch))
1340                 pch++;
1341             *q = 0;
1342             if (get_monitor_def(&n, buf))
1343                 expr_error("unknown register");
1344         }
1345         break;
1346     case '\0':
1347         expr_error("unexpected end of expression");
1348         n = 0;
1349         break;
1350     default:
1351         n = strtoul(pch, &p, 0);
1352         if (pch == p) {
1353             expr_error("invalid char in expression");
1354         }
1355         pch = p;
1356         while (isspace(*pch))
1357             pch++;
1358         break;
1359     }
1360     return n;
1361 }
1362
1363
1364 static target_long expr_prod(void)
1365 {
1366     target_long val, val2;
1367     int op;
1368     
1369     val = expr_unary();
1370     for(;;) {
1371         op = *pch;
1372         if (op != '*' && op != '/' && op != '%')
1373             break;
1374         next();
1375         val2 = expr_unary();
1376         switch(op) {
1377         default:
1378         case '*':
1379             val *= val2;
1380             break;
1381         case '/':
1382         case '%':
1383             if (val2 == 0) 
1384                 expr_error("division by zero");
1385             if (op == '/')
1386                 val /= val2;
1387             else
1388                 val %= val2;
1389             break;
1390         }
1391     }
1392     return val;
1393 }
1394
1395 static target_long expr_logic(void)
1396 {
1397     target_long val, val2;
1398     int op;
1399
1400     val = expr_prod();
1401     for(;;) {
1402         op = *pch;
1403         if (op != '&' && op != '|' && op != '^')
1404             break;
1405         next();
1406         val2 = expr_prod();
1407         switch(op) {
1408         default:
1409         case '&':
1410             val &= val2;
1411             break;
1412         case '|':
1413             val |= val2;
1414             break;
1415         case '^':
1416             val ^= val2;
1417             break;
1418         }
1419     }
1420     return val;
1421 }
1422
1423 static target_long expr_sum(void)
1424 {
1425     target_long val, val2;
1426     int op;
1427
1428     val = expr_logic();
1429     for(;;) {
1430         op = *pch;
1431         if (op != '+' && op != '-')
1432             break;
1433         next();
1434         val2 = expr_logic();
1435         if (op == '+')
1436             val += val2;
1437         else
1438             val -= val2;
1439     }
1440     return val;
1441 }
1442
1443 static int get_expr(target_long *pval, const char **pp)
1444 {
1445     pch = *pp;
1446     if (setjmp(expr_env)) {
1447         *pp = pch;
1448         return -1;
1449     }
1450     while (isspace(*pch))
1451         pch++;
1452     *pval = expr_sum();
1453     *pp = pch;
1454     return 0;
1455 }
1456
1457 static int get_str(char *buf, int buf_size, const char **pp)
1458 {
1459     const char *p;
1460     char *q;
1461     int c;
1462
1463     q = buf;
1464     p = *pp;
1465     while (isspace(*p))
1466         p++;
1467     if (*p == '\0') {
1468     fail:
1469         *q = '\0';
1470         *pp = p;
1471         return -1;
1472     }
1473     if (*p == '\"') {
1474         p++;
1475         while (*p != '\0' && *p != '\"') {
1476             if (*p == '\\') {
1477                 p++;
1478                 c = *p++;
1479                 switch(c) {
1480                 case 'n':
1481                     c = '\n';
1482                     break;
1483                 case 'r':
1484                     c = '\r';
1485                     break;
1486                 case '\\':
1487                 case '\'':
1488                 case '\"':
1489                     break;
1490                 default:
1491                     qemu_printf("unsupported escape code: '\\%c'\n", c);
1492                     goto fail;
1493                 }
1494                 if ((q - buf) < buf_size - 1) {
1495                     *q++ = c;
1496                 }
1497             } else {
1498                 if ((q - buf) < buf_size - 1) {
1499                     *q++ = *p;
1500                 }
1501                 p++;
1502             }
1503         }
1504         if (*p != '\"') {
1505             qemu_printf("unterminated string\n");
1506             goto fail;
1507         }
1508         p++;
1509     } else {
1510         while (*p != '\0' && !isspace(*p)) {
1511             if ((q - buf) < buf_size - 1) {
1512                 *q++ = *p;
1513             }
1514             p++;
1515         }
1516     }
1517     *q = '\0';
1518     *pp = p;
1519     return 0;
1520 }
1521
1522 static int default_fmt_format = 'x';
1523 static int default_fmt_size = 4;
1524
1525 #define MAX_ARGS 16
1526
1527 static void monitor_handle_command(const char *cmdline)
1528 {
1529     const char *p, *pstart, *typestr;
1530     char *q;
1531     int c, nb_args, len, i, has_arg;
1532     term_cmd_t *cmd;
1533     char cmdname[256];
1534     char buf[1024];
1535     void *str_allocated[MAX_ARGS];
1536     void *args[MAX_ARGS];
1537
1538 #ifdef DEBUG
1539     term_printf("command='%s'\n", cmdline);
1540 #endif
1541     
1542     /* extract the command name */
1543     p = cmdline;
1544     q = cmdname;
1545     while (isspace(*p))
1546         p++;
1547     if (*p == '\0')
1548         return;
1549     pstart = p;
1550     while (*p != '\0' && *p != '/' && !isspace(*p))
1551         p++;
1552     len = p - pstart;
1553     if (len > sizeof(cmdname) - 1)
1554         len = sizeof(cmdname) - 1;
1555     memcpy(cmdname, pstart, len);
1556     cmdname[len] = '\0';
1557     
1558     /* find the command */
1559     for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1560         if (compare_cmd(cmdname, cmd->name)) 
1561             goto found;
1562     }
1563     term_printf("unknown command: '%s'\n", cmdname);
1564     return;
1565  found:
1566
1567     for(i = 0; i < MAX_ARGS; i++)
1568         str_allocated[i] = NULL;
1569     
1570     /* parse the parameters */
1571     typestr = cmd->args_type;
1572     nb_args = 0;
1573     for(;;) {
1574         c = *typestr;
1575         if (c == '\0')
1576             break;
1577         typestr++;
1578         switch(c) {
1579         case 'F':
1580         case 'B':
1581         case 's':
1582             {
1583                 int ret;
1584                 char *str;
1585                 
1586                 while (isspace(*p)) 
1587                     p++;
1588                 if (*typestr == '?') {
1589                     typestr++;
1590                     if (*p == '\0') {
1591                         /* no optional string: NULL argument */
1592                         str = NULL;
1593                         goto add_str;
1594                     }
1595                 }
1596                 ret = get_str(buf, sizeof(buf), &p);
1597                 if (ret < 0) {
1598                     switch(c) {
1599                     case 'F':
1600                         term_printf("%s: filename expected\n", cmdname);
1601                         break;
1602                     case 'B':
1603                         term_printf("%s: block device name expected\n", cmdname);
1604                         break;
1605                     default:
1606                         term_printf("%s: string expected\n", cmdname);
1607                         break;
1608                     }
1609                     goto fail;
1610                 }
1611                 str = qemu_malloc(strlen(buf) + 1);
1612                 strcpy(str, buf);
1613                 str_allocated[nb_args] = str;
1614             add_str:
1615                 if (nb_args >= MAX_ARGS) {
1616                 error_args:
1617                     term_printf("%s: too many arguments\n", cmdname);
1618                     goto fail;
1619                 }
1620                 args[nb_args++] = str;
1621             }
1622             break;
1623         case '/':
1624             {
1625                 int count, format, size;
1626                 
1627                 while (isspace(*p))
1628                     p++;
1629                 if (*p == '/') {
1630                     /* format found */
1631                     p++;
1632                     count = 1;
1633                     if (isdigit(*p)) {
1634                         count = 0;
1635                         while (isdigit(*p)) {
1636                             count = count * 10 + (*p - '0');
1637                             p++;
1638                         }
1639                     }
1640                     size = -1;
1641                     format = -1;
1642                     for(;;) {
1643                         switch(*p) {
1644                         case 'o':
1645                         case 'd':
1646                         case 'u':
1647                         case 'x':
1648                         case 'i':
1649                         case 'c':
1650                             format = *p++;
1651                             break;
1652                         case 'b':
1653                             size = 1;
1654                             p++;
1655                             break;
1656                         case 'h':
1657                             size = 2;
1658                             p++;
1659                             break;
1660                         case 'w':
1661                             size = 4;
1662                             p++;
1663                             break;
1664                         case 'g':
1665                         case 'L':
1666                             size = 8;
1667                             p++;
1668                             break;
1669                         default:
1670                             goto next;
1671                         }
1672                     }
1673                 next:
1674                     if (*p != '\0' && !isspace(*p)) {
1675                         term_printf("invalid char in format: '%c'\n", *p);
1676                         goto fail;
1677                     }
1678                     if (format < 0)
1679                         format = default_fmt_format;
1680                     if (format != 'i') {
1681                         /* for 'i', not specifying a size gives -1 as size */
1682                         if (size < 0)
1683                             size = default_fmt_size;
1684                     }
1685                     default_fmt_size = size;
1686                     default_fmt_format = format;
1687                 } else {
1688                     count = 1;
1689                     format = default_fmt_format;
1690                     if (format != 'i') {
1691                         size = default_fmt_size;
1692                     } else {
1693                         size = -1;
1694                     }
1695                 }
1696                 if (nb_args + 3 > MAX_ARGS)
1697                     goto error_args;
1698                 args[nb_args++] = (void*)count;
1699                 args[nb_args++] = (void*)format;
1700                 args[nb_args++] = (void*)size;
1701             }
1702             break;
1703         case 'i':
1704         case 'l':
1705             {
1706                 target_long val;
1707                 while (isspace(*p)) 
1708                     p++;
1709                 if (*typestr == '?' || *typestr == '.') {
1710                     typestr++;
1711                     if (*typestr == '?') {
1712                         if (*p == '\0')
1713                             has_arg = 0;
1714                         else
1715                             has_arg = 1;
1716                     } else {
1717                         if (*p == '.') {
1718                             p++;
1719                             while (isspace(*p)) 
1720                                 p++;
1721                             has_arg = 1;
1722                         } else {
1723                             has_arg = 0;
1724                         }
1725                     }
1726                     if (nb_args >= MAX_ARGS)
1727                         goto error_args;
1728                     args[nb_args++] = (void *)has_arg;
1729                     if (!has_arg) {
1730                         if (nb_args >= MAX_ARGS)
1731                             goto error_args;
1732                         val = -1;
1733                         goto add_num;
1734                     }
1735                 }
1736                 if (get_expr(&val, &p))
1737                     goto fail;
1738             add_num:
1739                 if (c == 'i') {
1740                     if (nb_args >= MAX_ARGS)
1741                         goto error_args;
1742                     args[nb_args++] = (void *)(int)val;
1743                 } else {
1744                     if ((nb_args + 1) >= MAX_ARGS)
1745                         goto error_args;
1746 #if TARGET_LONG_BITS == 64
1747                     args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1748 #else
1749                     args[nb_args++] = (void *)0;
1750 #endif
1751                     args[nb_args++] = (void *)(int)(val & 0xffffffff);
1752                 }
1753             }
1754             break;
1755         case '-':
1756             {
1757                 int has_option;
1758                 /* option */
1759                 
1760                 c = *typestr++;
1761                 if (c == '\0')
1762                     goto bad_type;
1763                 while (isspace(*p)) 
1764                     p++;
1765                 has_option = 0;
1766                 if (*p == '-') {
1767                     p++;
1768                     if (*p != c) {
1769                         term_printf("%s: unsupported option -%c\n", 
1770                                     cmdname, *p);
1771                         goto fail;
1772                     }
1773                     p++;
1774                     has_option = 1;
1775                 }
1776                 if (nb_args >= MAX_ARGS)
1777                     goto error_args;
1778                 args[nb_args++] = (void *)has_option;
1779             }
1780             break;
1781         default:
1782         bad_type:
1783             term_printf("%s: unknown type '%c'\n", cmdname, c);
1784             goto fail;
1785         }
1786     }
1787     /* check that all arguments were parsed */
1788     while (isspace(*p))
1789         p++;
1790     if (*p != '\0') {
1791         term_printf("%s: extraneous characters at the end of line\n", 
1792                     cmdname);
1793         goto fail;
1794     }
1795
1796     switch(nb_args) {
1797     case 0:
1798         cmd->handler();
1799         break;
1800     case 1:
1801         cmd->handler(args[0]);
1802         break;
1803     case 2:
1804         cmd->handler(args[0], args[1]);
1805         break;
1806     case 3:
1807         cmd->handler(args[0], args[1], args[2]);
1808         break;
1809     case 4:
1810         cmd->handler(args[0], args[1], args[2], args[3]);
1811         break;
1812     case 5:
1813         cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1814         break;
1815     case 6:
1816         cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1817         break;
1818     default:
1819         term_printf("unsupported number of arguments: %d\n", nb_args);
1820         goto fail;
1821     }
1822  fail:
1823     for(i = 0; i < MAX_ARGS; i++)
1824         qemu_free(str_allocated[i]);
1825     return;
1826 }
1827
1828 static void cmd_completion(const char *name, const char *list)
1829 {
1830     const char *p, *pstart;
1831     char cmd[128];
1832     int len;
1833
1834     p = list;
1835     for(;;) {
1836         pstart = p;
1837         p = strchr(p, '|');
1838         if (!p)
1839             p = pstart + strlen(pstart);
1840         len = p - pstart;
1841         if (len > sizeof(cmd) - 2)
1842             len = sizeof(cmd) - 2;
1843         memcpy(cmd, pstart, len);
1844         cmd[len] = '\0';
1845         if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1846             add_completion(cmd);
1847         }
1848         if (*p == '\0')
1849             break;
1850         p++;
1851     }
1852 }
1853
1854 static void file_completion(const char *input)
1855 {
1856     DIR *ffs;
1857     struct dirent *d;
1858     char path[1024];
1859     char file[1024], file_prefix[1024];
1860     int input_path_len;
1861     const char *p;
1862
1863     p = strrchr(input, '/'); 
1864     if (!p) {
1865         input_path_len = 0;
1866         pstrcpy(file_prefix, sizeof(file_prefix), input);
1867         strcpy(path, ".");
1868     } else {
1869         input_path_len = p - input + 1;
1870         memcpy(path, input, input_path_len);
1871         if (input_path_len > sizeof(path) - 1)
1872             input_path_len = sizeof(path) - 1;
1873         path[input_path_len] = '\0';
1874         pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1875     }
1876 #ifdef DEBUG_COMPLETION
1877     term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1878 #endif
1879     ffs = opendir(path);
1880     if (!ffs)
1881         return;
1882     for(;;) {
1883         struct stat sb;
1884         d = readdir(ffs);
1885         if (!d)
1886             break;
1887         if (strstart(d->d_name, file_prefix, NULL)) {
1888             memcpy(file, input, input_path_len);
1889             strcpy(file + input_path_len, d->d_name);
1890             /* stat the file to find out if it's a directory.
1891              * In that case add a slash to speed up typing long paths
1892              */
1893             stat(file, &sb);
1894             if(S_ISDIR(sb.st_mode))
1895                 strcat(file, "/");
1896             add_completion(file);
1897         }
1898     }
1899     closedir(ffs);
1900 }
1901
1902 static void block_completion_it(void *opaque, const char *name)
1903 {
1904     const char *input = opaque;
1905
1906     if (input[0] == '\0' ||
1907         !strncmp(name, (char *)input, strlen(input))) {
1908         add_completion(name);
1909     }
1910 }
1911
1912 /* NOTE: this parser is an approximate form of the real command parser */
1913 static void parse_cmdline(const char *cmdline,
1914                          int *pnb_args, char **args)
1915 {
1916     const char *p;
1917     int nb_args, ret;
1918     char buf[1024];
1919
1920     p = cmdline;
1921     nb_args = 0;
1922     for(;;) {
1923         while (isspace(*p))
1924             p++;
1925         if (*p == '\0')
1926             break;
1927         if (nb_args >= MAX_ARGS)
1928             break;
1929         ret = get_str(buf, sizeof(buf), &p);
1930         args[nb_args] = qemu_strdup(buf);
1931         nb_args++;
1932         if (ret < 0)
1933             break;
1934     }
1935     *pnb_args = nb_args;
1936 }
1937
1938 void readline_find_completion(const char *cmdline)
1939 {
1940     const char *cmdname;
1941     char *args[MAX_ARGS];
1942     int nb_args, i, len;
1943     const char *ptype, *str;
1944     term_cmd_t *cmd;
1945
1946     parse_cmdline(cmdline, &nb_args, args);
1947 #ifdef DEBUG_COMPLETION
1948     for(i = 0; i < nb_args; i++) {
1949         term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1950     }
1951 #endif
1952
1953     /* if the line ends with a space, it means we want to complete the
1954        next arg */
1955     len = strlen(cmdline);
1956     if (len > 0 && isspace(cmdline[len - 1])) {
1957         if (nb_args >= MAX_ARGS)
1958             return;
1959         args[nb_args++] = qemu_strdup("");
1960     }
1961     if (nb_args <= 1) {
1962         /* command completion */
1963         if (nb_args == 0)
1964             cmdname = "";
1965         else
1966             cmdname = args[0];
1967         completion_index = strlen(cmdname);
1968         for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1969             cmd_completion(cmdname, cmd->name);
1970         }
1971     } else {
1972         /* find the command */
1973         for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1974             if (compare_cmd(args[0], cmd->name))
1975                 goto found;
1976         }
1977         return;
1978     found:
1979         ptype = cmd->args_type;
1980         for(i = 0; i < nb_args - 2; i++) {
1981             if (*ptype != '\0') {
1982                 ptype++;
1983                 while (*ptype == '?')
1984                     ptype++;
1985             }
1986         }
1987         str = args[nb_args - 1];
1988         switch(*ptype) {
1989         case 'F':
1990             /* file completion */
1991             completion_index = strlen(str);
1992             file_completion(str);
1993             break;
1994         case 'B':
1995             /* block device name completion */
1996             completion_index = strlen(str);
1997             bdrv_iterate(block_completion_it, (void *)str);
1998             break;
1999         case 's':
2000             /* XXX: more generic ? */
2001             if (!strcmp(cmd->name, "info")) {
2002                 completion_index = strlen(str);
2003                 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2004                     cmd_completion(str, cmd->name);
2005                 }
2006             }
2007             break;
2008         default:
2009             break;
2010         }
2011     }
2012     for(i = 0; i < nb_args; i++)
2013         qemu_free(args[i]);
2014 }
2015
2016 static int term_can_read(void *opaque)
2017 {
2018     return 128;
2019 }
2020
2021 static void term_read(void *opaque, const uint8_t *buf, int size)
2022 {
2023     int i;
2024     for(i = 0; i < size; i++)
2025         readline_handle_byte(buf[i]);
2026 }
2027
2028 static void monitor_start_input(void);
2029
2030 static void monitor_handle_command1(void *opaque, const char *cmdline)
2031 {
2032     monitor_handle_command(cmdline);
2033     monitor_start_input();
2034 }
2035
2036 static void monitor_start_input(void)
2037 {
2038     readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2039 }
2040
2041 void monitor_init(CharDriverState *hd, int show_banner)
2042 {
2043     monitor_hd = hd;
2044     if (show_banner) {
2045         term_printf("QEMU %s monitor - type 'help' for more information\n",
2046                     QEMU_VERSION);
2047     }
2048     qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2049     monitor_start_input();
2050 }
2051
2052 /* XXX: use threads ? */
2053 /* modal monitor readline */
2054 static int monitor_readline_started;
2055 static char *monitor_readline_buf;
2056 static int monitor_readline_buf_size;
2057
2058 static void monitor_readline_cb(void *opaque, const char *input)
2059 {
2060     pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2061     monitor_readline_started = 0;
2062 }
2063
2064 void monitor_readline(const char *prompt, int is_password,
2065                       char *buf, int buf_size)
2066 {
2067     if (is_password) {
2068         qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2069     }
2070     readline_start(prompt, is_password, monitor_readline_cb, NULL);
2071     monitor_readline_buf = buf;
2072     monitor_readline_buf_size = buf_size;
2073     monitor_readline_started = 1;
2074     while (monitor_readline_started) {
2075         main_loop_wait(10);
2076     }
2077 }