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