monitor: Drop banner hiding (Jan Kiszka)
[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 "hw/hw.h"
25 #include "hw/usb.h"
26 #include "hw/pcmcia.h"
27 #include "hw/pc.h"
28 #include "hw/pci.h"
29 #include "gdbstub.h"
30 #include "net.h"
31 #include "qemu-char.h"
32 #include "sysemu.h"
33 #include "monitor.h"
34 #include "readline.h"
35 #include "console.h"
36 #include "block.h"
37 #include "audio/audio.h"
38 #include "disas.h"
39 #include "balloon.h"
40 #include <dirent.h>
41 #include "qemu-timer.h"
42 #include "migration.h"
43 #include "kvm.h"
44
45 //#define DEBUG
46 //#define DEBUG_COMPLETION
47
48 /*
49  * Supported types:
50  *
51  * 'F'          filename
52  * 'B'          block device name
53  * 's'          string (accept optional quote)
54  * 'i'          32 bit integer
55  * 'l'          target long (32 or 64 bit)
56  * '/'          optional gdb-like print format (like "/10x")
57  *
58  * '?'          optional type (for 'F', 's' and 'i')
59  *
60  */
61
62 typedef struct mon_cmd_t {
63     const char *name;
64     const char *args_type;
65     void *handler;
66     const char *params;
67     const char *help;
68 } mon_cmd_t;
69
70 struct Monitor {
71     CharDriverState *chr;
72     LIST_ENTRY(Monitor) entry;
73 };
74
75 static LIST_HEAD(mon_list, Monitor) mon_list;
76
77 static const mon_cmd_t mon_cmds[];
78 static const mon_cmd_t info_cmds[];
79
80 static uint8_t term_outbuf[1024];
81 static int term_outbuf_index;
82 static BlockDriverCompletionFunc *password_completion_cb;
83 static void *password_opaque;
84
85 Monitor *cur_mon = NULL;
86
87 static void monitor_start_input(void);
88
89 static CPUState *mon_cpu = NULL;
90
91 static void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
92                                   void *opaque)
93 {
94     readline_start("Password: ", 1, readline_func, opaque);
95 }
96
97 void monitor_flush(Monitor *mon)
98 {
99     Monitor *m;
100
101     if (term_outbuf_index > 0) {
102         LIST_FOREACH(m, &mon_list, entry) {
103             if (m->chr->focus == 0)
104                 qemu_chr_write(m->chr, term_outbuf, term_outbuf_index);
105         }
106         term_outbuf_index = 0;
107     }
108 }
109
110 /* flush at every end of line or if the buffer is full */
111 static void monitor_puts(Monitor *mon, const char *str)
112 {
113     char c;
114     for(;;) {
115         c = *str++;
116         if (c == '\0')
117             break;
118         if (c == '\n')
119             term_outbuf[term_outbuf_index++] = '\r';
120         term_outbuf[term_outbuf_index++] = c;
121         if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
122             c == '\n')
123             monitor_flush(mon);
124     }
125 }
126
127 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
128 {
129     char buf[4096];
130     vsnprintf(buf, sizeof(buf), fmt, ap);
131     monitor_puts(mon, buf);
132 }
133
134 void monitor_printf(Monitor *mon, const char *fmt, ...)
135 {
136     va_list ap;
137     va_start(ap, fmt);
138     monitor_vprintf(mon, fmt, ap);
139     va_end(ap);
140 }
141
142 void monitor_print_filename(Monitor *mon, const char *filename)
143 {
144     int i;
145
146     for (i = 0; filename[i]; i++) {
147         switch (filename[i]) {
148         case ' ':
149         case '"':
150         case '\\':
151             monitor_printf(mon, "\\%c", filename[i]);
152             break;
153         case '\t':
154             monitor_printf(mon, "\\t");
155             break;
156         case '\r':
157             monitor_printf(mon, "\\r");
158             break;
159         case '\n':
160             monitor_printf(mon, "\\n");
161             break;
162         default:
163             monitor_printf(mon, "%c", filename[i]);
164             break;
165         }
166     }
167 }
168
169 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
170 {
171     va_list ap;
172     va_start(ap, fmt);
173     monitor_vprintf((Monitor *)stream, fmt, ap);
174     va_end(ap);
175     return 0;
176 }
177
178 static int compare_cmd(const char *name, const char *list)
179 {
180     const char *p, *pstart;
181     int len;
182     len = strlen(name);
183     p = list;
184     for(;;) {
185         pstart = p;
186         p = strchr(p, '|');
187         if (!p)
188             p = pstart + strlen(pstart);
189         if ((p - pstart) == len && !memcmp(pstart, name, len))
190             return 1;
191         if (*p == '\0')
192             break;
193         p++;
194     }
195     return 0;
196 }
197
198 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
199                           const char *prefix, const char *name)
200 {
201     const mon_cmd_t *cmd;
202
203     for(cmd = cmds; cmd->name != NULL; cmd++) {
204         if (!name || !strcmp(name, cmd->name))
205             monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
206                            cmd->params, cmd->help);
207     }
208 }
209
210 static void help_cmd(Monitor *mon, const char *name)
211 {
212     if (name && !strcmp(name, "info")) {
213         help_cmd_dump(mon, info_cmds, "info ", NULL);
214     } else {
215         help_cmd_dump(mon, mon_cmds, "", name);
216         if (name && !strcmp(name, "log")) {
217             const CPULogItem *item;
218             monitor_printf(mon, "Log items (comma separated):\n");
219             monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
220             for(item = cpu_log_items; item->mask != 0; item++) {
221                 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
222             }
223         }
224     }
225 }
226
227 static void do_commit(Monitor *mon, const char *device)
228 {
229     int i, all_devices;
230
231     all_devices = !strcmp(device, "all");
232     for (i = 0; i < nb_drives; i++) {
233             if (all_devices ||
234                 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
235                 bdrv_commit(drives_table[i].bdrv);
236     }
237 }
238
239 static void do_info(Monitor *mon, const char *item)
240 {
241     const mon_cmd_t *cmd;
242     void (*handler)(Monitor *);
243
244     if (!item)
245         goto help;
246     for(cmd = info_cmds; cmd->name != NULL; cmd++) {
247         if (compare_cmd(item, cmd->name))
248             goto found;
249     }
250  help:
251     help_cmd(mon, "info");
252     return;
253  found:
254     handler = cmd->handler;
255     handler(mon);
256 }
257
258 static void do_info_version(Monitor *mon)
259 {
260     monitor_printf(mon, "%s\n", QEMU_VERSION);
261 }
262
263 static void do_info_name(Monitor *mon)
264 {
265     if (qemu_name)
266         monitor_printf(mon, "%s\n", qemu_name);
267 }
268
269 #if defined(TARGET_I386)
270 static void do_info_hpet(Monitor *mon)
271 {
272     monitor_printf(mon, "HPET is %s by QEMU\n",
273                    (no_hpet) ? "disabled" : "enabled");
274 }
275 #endif
276
277 static void do_info_uuid(Monitor *mon)
278 {
279     monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
280                    qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
281                    qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
282                    qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
283                    qemu_uuid[14], qemu_uuid[15]);
284 }
285
286 /* get the current CPU defined by the user */
287 static int mon_set_cpu(int cpu_index)
288 {
289     CPUState *env;
290
291     for(env = first_cpu; env != NULL; env = env->next_cpu) {
292         if (env->cpu_index == cpu_index) {
293             mon_cpu = env;
294             return 0;
295         }
296     }
297     return -1;
298 }
299
300 static CPUState *mon_get_cpu(void)
301 {
302     if (!mon_cpu) {
303         mon_set_cpu(0);
304     }
305     return mon_cpu;
306 }
307
308 static void do_info_registers(Monitor *mon)
309 {
310     CPUState *env;
311     env = mon_get_cpu();
312     if (!env)
313         return;
314 #ifdef TARGET_I386
315     cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
316                    X86_DUMP_FPU);
317 #else
318     cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
319                    0);
320 #endif
321 }
322
323 static void do_info_cpus(Monitor *mon)
324 {
325     CPUState *env;
326
327     /* just to set the default cpu if not already done */
328     mon_get_cpu();
329
330     for(env = first_cpu; env != NULL; env = env->next_cpu) {
331         monitor_printf(mon, "%c CPU #%d:",
332                        (env == mon_cpu) ? '*' : ' ',
333                        env->cpu_index);
334 #if defined(TARGET_I386)
335         monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
336                        env->eip + env->segs[R_CS].base);
337 #elif defined(TARGET_PPC)
338         monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
339 #elif defined(TARGET_SPARC)
340         monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
341                        env->pc, env->npc);
342 #elif defined(TARGET_MIPS)
343         monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
344 #endif
345         if (env->halted)
346             monitor_printf(mon, " (halted)");
347         monitor_printf(mon, "\n");
348     }
349 }
350
351 static void do_cpu_set(Monitor *mon, int index)
352 {
353     if (mon_set_cpu(index) < 0)
354         monitor_printf(mon, "Invalid CPU index\n");
355 }
356
357 static void do_info_jit(Monitor *mon)
358 {
359     dump_exec_info((FILE *)mon, monitor_fprintf);
360 }
361
362 static void do_info_history(Monitor *mon)
363 {
364     int i;
365     const char *str;
366
367     i = 0;
368     for(;;) {
369         str = readline_get_history(i);
370         if (!str)
371             break;
372         monitor_printf(mon, "%d: '%s'\n", i, str);
373         i++;
374     }
375 }
376
377 #if defined(TARGET_PPC)
378 /* XXX: not implemented in other targets */
379 static void do_info_cpu_stats(Monitor *mon)
380 {
381     CPUState *env;
382
383     env = mon_get_cpu();
384     cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
385 }
386 #endif
387
388 static void do_quit(Monitor *mon)
389 {
390     exit(0);
391 }
392
393 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
394 {
395     if (bdrv_is_inserted(bs)) {
396         if (!force) {
397             if (!bdrv_is_removable(bs)) {
398                 monitor_printf(mon, "device is not removable\n");
399                 return -1;
400             }
401             if (bdrv_is_locked(bs)) {
402                 monitor_printf(mon, "device is locked\n");
403                 return -1;
404             }
405         }
406         bdrv_close(bs);
407     }
408     return 0;
409 }
410
411 static void do_eject(Monitor *mon, int force, const char *filename)
412 {
413     BlockDriverState *bs;
414
415     bs = bdrv_find(filename);
416     if (!bs) {
417         monitor_printf(mon, "device not found\n");
418         return;
419     }
420     eject_device(mon, bs, force);
421 }
422
423 static void do_change_block(Monitor *mon, const char *device,
424                             const char *filename, const char *fmt)
425 {
426     BlockDriverState *bs;
427     BlockDriver *drv = NULL;
428
429     bs = bdrv_find(device);
430     if (!bs) {
431         monitor_printf(mon, "device not found\n");
432         return;
433     }
434     if (fmt) {
435         drv = bdrv_find_format(fmt);
436         if (!drv) {
437             monitor_printf(mon, "invalid format %s\n", fmt);
438             return;
439         }
440     }
441     if (eject_device(mon, bs, 0) < 0)
442         return;
443     bdrv_open2(bs, filename, 0, drv);
444     monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
445 }
446
447 static void change_vnc_password_cb(Monitor *mon, const char *password,
448                                    void *opaque)
449 {
450     if (vnc_display_password(NULL, password) < 0)
451         monitor_printf(mon, "could not set VNC server password\n");
452
453     monitor_start_input();
454 }
455
456 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
457 {
458     if (strcmp(target, "passwd") == 0 ||
459         strcmp(target, "password") == 0) {
460         if (arg) {
461             char password[9];
462             strncpy(password, arg, sizeof(password));
463             password[sizeof(password) - 1] = '\0';
464             change_vnc_password_cb(mon, password, NULL);
465         } else {
466             monitor_read_password(mon, change_vnc_password_cb, NULL);
467         }
468     } else {
469         if (vnc_display_open(NULL, target) < 0)
470             monitor_printf(mon, "could not start VNC server on %s\n", target);
471     }
472 }
473
474 static void do_change(Monitor *mon, const char *device, const char *target,
475                       const char *arg)
476 {
477     if (strcmp(device, "vnc") == 0) {
478         do_change_vnc(mon, target, arg);
479     } else {
480         do_change_block(mon, device, target, arg);
481     }
482 }
483
484 static void do_screen_dump(Monitor *mon, const char *filename)
485 {
486     vga_hw_screen_dump(filename);
487 }
488
489 static void do_logfile(Monitor *mon, const char *filename)
490 {
491     cpu_set_log_filename(filename);
492 }
493
494 static void do_log(Monitor *mon, const char *items)
495 {
496     int mask;
497
498     if (!strcmp(items, "none")) {
499         mask = 0;
500     } else {
501         mask = cpu_str_to_log_mask(items);
502         if (!mask) {
503             help_cmd(mon, "log");
504             return;
505         }
506     }
507     cpu_set_log(mask);
508 }
509
510 static void do_stop(Monitor *mon)
511 {
512     vm_stop(EXCP_INTERRUPT);
513 }
514
515 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
516
517 struct bdrv_iterate_context {
518     Monitor *mon;
519     int err;
520 };
521
522 static void do_cont(Monitor *mon)
523 {
524     struct bdrv_iterate_context context = { mon, 0 };
525
526     bdrv_iterate(encrypted_bdrv_it, &context);
527     /* only resume the vm if all keys are set and valid */
528     if (!context.err)
529         vm_start();
530 }
531
532 static void bdrv_key_cb(void *opaque, int err)
533 {
534     Monitor *mon = opaque;
535
536     /* another key was set successfully, retry to continue */
537     if (!err)
538         do_cont(mon);
539 }
540
541 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
542 {
543     struct bdrv_iterate_context *context = opaque;
544
545     if (!context->err && bdrv_key_required(bs)) {
546         context->err = -EBUSY;
547         monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
548                                     context->mon);
549     }
550 }
551
552 #ifdef CONFIG_GDBSTUB
553 static void do_gdbserver(Monitor *mon, const char *port)
554 {
555     if (!port)
556         port = DEFAULT_GDBSTUB_PORT;
557     if (gdbserver_start(port) < 0) {
558         monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
559                        port);
560     } else {
561         monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
562     }
563 }
564 #endif
565
566 static void monitor_printc(Monitor *mon, int c)
567 {
568     monitor_printf(mon, "'");
569     switch(c) {
570     case '\'':
571         monitor_printf(mon, "\\'");
572         break;
573     case '\\':
574         monitor_printf(mon, "\\\\");
575         break;
576     case '\n':
577         monitor_printf(mon, "\\n");
578         break;
579     case '\r':
580         monitor_printf(mon, "\\r");
581         break;
582     default:
583         if (c >= 32 && c <= 126) {
584             monitor_printf(mon, "%c", c);
585         } else {
586             monitor_printf(mon, "\\x%02x", c);
587         }
588         break;
589     }
590     monitor_printf(mon, "'");
591 }
592
593 static void memory_dump(Monitor *mon, int count, int format, int wsize,
594                         target_phys_addr_t addr, int is_physical)
595 {
596     CPUState *env;
597     int nb_per_line, l, line_size, i, max_digits, len;
598     uint8_t buf[16];
599     uint64_t v;
600
601     if (format == 'i') {
602         int flags;
603         flags = 0;
604         env = mon_get_cpu();
605         if (!env && !is_physical)
606             return;
607 #ifdef TARGET_I386
608         if (wsize == 2) {
609             flags = 1;
610         } else if (wsize == 4) {
611             flags = 0;
612         } else {
613             /* as default we use the current CS size */
614             flags = 0;
615             if (env) {
616 #ifdef TARGET_X86_64
617                 if ((env->efer & MSR_EFER_LMA) &&
618                     (env->segs[R_CS].flags & DESC_L_MASK))
619                     flags = 2;
620                 else
621 #endif
622                 if (!(env->segs[R_CS].flags & DESC_B_MASK))
623                     flags = 1;
624             }
625         }
626 #endif
627         monitor_disas(mon, env, addr, count, is_physical, flags);
628         return;
629     }
630
631     len = wsize * count;
632     if (wsize == 1)
633         line_size = 8;
634     else
635         line_size = 16;
636     nb_per_line = line_size / wsize;
637     max_digits = 0;
638
639     switch(format) {
640     case 'o':
641         max_digits = (wsize * 8 + 2) / 3;
642         break;
643     default:
644     case 'x':
645         max_digits = (wsize * 8) / 4;
646         break;
647     case 'u':
648     case 'd':
649         max_digits = (wsize * 8 * 10 + 32) / 33;
650         break;
651     case 'c':
652         wsize = 1;
653         break;
654     }
655
656     while (len > 0) {
657         if (is_physical)
658             monitor_printf(mon, TARGET_FMT_plx ":", addr);
659         else
660             monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
661         l = len;
662         if (l > line_size)
663             l = line_size;
664         if (is_physical) {
665             cpu_physical_memory_rw(addr, buf, l, 0);
666         } else {
667             env = mon_get_cpu();
668             if (!env)
669                 break;
670             if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
671                 monitor_printf(mon, " Cannot access memory\n");
672                 break;
673             }
674         }
675         i = 0;
676         while (i < l) {
677             switch(wsize) {
678             default:
679             case 1:
680                 v = ldub_raw(buf + i);
681                 break;
682             case 2:
683                 v = lduw_raw(buf + i);
684                 break;
685             case 4:
686                 v = (uint32_t)ldl_raw(buf + i);
687                 break;
688             case 8:
689                 v = ldq_raw(buf + i);
690                 break;
691             }
692             monitor_printf(mon, " ");
693             switch(format) {
694             case 'o':
695                 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
696                 break;
697             case 'x':
698                 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
699                 break;
700             case 'u':
701                 monitor_printf(mon, "%*" PRIu64, max_digits, v);
702                 break;
703             case 'd':
704                 monitor_printf(mon, "%*" PRId64, max_digits, v);
705                 break;
706             case 'c':
707                 monitor_printc(mon, v);
708                 break;
709             }
710             i += wsize;
711         }
712         monitor_printf(mon, "\n");
713         addr += l;
714         len -= l;
715     }
716 }
717
718 #if TARGET_LONG_BITS == 64
719 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
720 #else
721 #define GET_TLONG(h, l) (l)
722 #endif
723
724 static void do_memory_dump(Monitor *mon, int count, int format, int size,
725                            uint32_t addrh, uint32_t addrl)
726 {
727     target_long addr = GET_TLONG(addrh, addrl);
728     memory_dump(mon, count, format, size, addr, 0);
729 }
730
731 #if TARGET_PHYS_ADDR_BITS > 32
732 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
733 #else
734 #define GET_TPHYSADDR(h, l) (l)
735 #endif
736
737 static void do_physical_memory_dump(Monitor *mon, int count, int format,
738                                     int size, uint32_t addrh, uint32_t addrl)
739
740 {
741     target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
742     memory_dump(mon, count, format, size, addr, 1);
743 }
744
745 static void do_print(Monitor *mon, int count, int format, int size,
746                      unsigned int valh, unsigned int vall)
747 {
748     target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
749 #if TARGET_PHYS_ADDR_BITS == 32
750     switch(format) {
751     case 'o':
752         monitor_printf(mon, "%#o", val);
753         break;
754     case 'x':
755         monitor_printf(mon, "%#x", val);
756         break;
757     case 'u':
758         monitor_printf(mon, "%u", val);
759         break;
760     default:
761     case 'd':
762         monitor_printf(mon, "%d", val);
763         break;
764     case 'c':
765         monitor_printc(mon, val);
766         break;
767     }
768 #else
769     switch(format) {
770     case 'o':
771         monitor_printf(mon, "%#" PRIo64, val);
772         break;
773     case 'x':
774         monitor_printf(mon, "%#" PRIx64, val);
775         break;
776     case 'u':
777         monitor_printf(mon, "%" PRIu64, val);
778         break;
779     default:
780     case 'd':
781         monitor_printf(mon, "%" PRId64, val);
782         break;
783     case 'c':
784         monitor_printc(mon, val);
785         break;
786     }
787 #endif
788     monitor_printf(mon, "\n");
789 }
790
791 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
792                            uint32_t size, const char *filename)
793 {
794     FILE *f;
795     target_long addr = GET_TLONG(valh, vall);
796     uint32_t l;
797     CPUState *env;
798     uint8_t buf[1024];
799
800     env = mon_get_cpu();
801     if (!env)
802         return;
803
804     f = fopen(filename, "wb");
805     if (!f) {
806         monitor_printf(mon, "could not open '%s'\n", filename);
807         return;
808     }
809     while (size != 0) {
810         l = sizeof(buf);
811         if (l > size)
812             l = size;
813         cpu_memory_rw_debug(env, addr, buf, l, 0);
814         fwrite(buf, 1, l, f);
815         addr += l;
816         size -= l;
817     }
818     fclose(f);
819 }
820
821 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
822                                     unsigned int vall, uint32_t size,
823                                     const char *filename)
824 {
825     FILE *f;
826     uint32_t l;
827     uint8_t buf[1024];
828     target_phys_addr_t addr = GET_TPHYSADDR(valh, vall); 
829
830     f = fopen(filename, "wb");
831     if (!f) {
832         monitor_printf(mon, "could not open '%s'\n", filename);
833         return;
834     }
835     while (size != 0) {
836         l = sizeof(buf);
837         if (l > size)
838             l = size;
839         cpu_physical_memory_rw(addr, buf, l, 0);
840         fwrite(buf, 1, l, f);
841         fflush(f);
842         addr += l;
843         size -= l;
844     }
845     fclose(f);
846 }
847
848 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
849 {
850     uint32_t addr;
851     uint8_t buf[1];
852     uint16_t sum;
853
854     sum = 0;
855     for(addr = start; addr < (start + size); addr++) {
856         cpu_physical_memory_rw(addr, buf, 1, 0);
857         /* BSD sum algorithm ('sum' Unix command) */
858         sum = (sum >> 1) | (sum << 15);
859         sum += buf[0];
860     }
861     monitor_printf(mon, "%05d\n", sum);
862 }
863
864 typedef struct {
865     int keycode;
866     const char *name;
867 } KeyDef;
868
869 static const KeyDef key_defs[] = {
870     { 0x2a, "shift" },
871     { 0x36, "shift_r" },
872
873     { 0x38, "alt" },
874     { 0xb8, "alt_r" },
875     { 0x64, "altgr" },
876     { 0xe4, "altgr_r" },
877     { 0x1d, "ctrl" },
878     { 0x9d, "ctrl_r" },
879
880     { 0xdd, "menu" },
881
882     { 0x01, "esc" },
883
884     { 0x02, "1" },
885     { 0x03, "2" },
886     { 0x04, "3" },
887     { 0x05, "4" },
888     { 0x06, "5" },
889     { 0x07, "6" },
890     { 0x08, "7" },
891     { 0x09, "8" },
892     { 0x0a, "9" },
893     { 0x0b, "0" },
894     { 0x0c, "minus" },
895     { 0x0d, "equal" },
896     { 0x0e, "backspace" },
897
898     { 0x0f, "tab" },
899     { 0x10, "q" },
900     { 0x11, "w" },
901     { 0x12, "e" },
902     { 0x13, "r" },
903     { 0x14, "t" },
904     { 0x15, "y" },
905     { 0x16, "u" },
906     { 0x17, "i" },
907     { 0x18, "o" },
908     { 0x19, "p" },
909
910     { 0x1c, "ret" },
911
912     { 0x1e, "a" },
913     { 0x1f, "s" },
914     { 0x20, "d" },
915     { 0x21, "f" },
916     { 0x22, "g" },
917     { 0x23, "h" },
918     { 0x24, "j" },
919     { 0x25, "k" },
920     { 0x26, "l" },
921
922     { 0x2c, "z" },
923     { 0x2d, "x" },
924     { 0x2e, "c" },
925     { 0x2f, "v" },
926     { 0x30, "b" },
927     { 0x31, "n" },
928     { 0x32, "m" },
929     { 0x33, "comma" },
930     { 0x34, "dot" },
931     { 0x35, "slash" },
932
933     { 0x37, "asterisk" },
934
935     { 0x39, "spc" },
936     { 0x3a, "caps_lock" },
937     { 0x3b, "f1" },
938     { 0x3c, "f2" },
939     { 0x3d, "f3" },
940     { 0x3e, "f4" },
941     { 0x3f, "f5" },
942     { 0x40, "f6" },
943     { 0x41, "f7" },
944     { 0x42, "f8" },
945     { 0x43, "f9" },
946     { 0x44, "f10" },
947     { 0x45, "num_lock" },
948     { 0x46, "scroll_lock" },
949
950     { 0xb5, "kp_divide" },
951     { 0x37, "kp_multiply" },
952     { 0x4a, "kp_subtract" },
953     { 0x4e, "kp_add" },
954     { 0x9c, "kp_enter" },
955     { 0x53, "kp_decimal" },
956     { 0x54, "sysrq" },
957
958     { 0x52, "kp_0" },
959     { 0x4f, "kp_1" },
960     { 0x50, "kp_2" },
961     { 0x51, "kp_3" },
962     { 0x4b, "kp_4" },
963     { 0x4c, "kp_5" },
964     { 0x4d, "kp_6" },
965     { 0x47, "kp_7" },
966     { 0x48, "kp_8" },
967     { 0x49, "kp_9" },
968
969     { 0x56, "<" },
970
971     { 0x57, "f11" },
972     { 0x58, "f12" },
973
974     { 0xb7, "print" },
975
976     { 0xc7, "home" },
977     { 0xc9, "pgup" },
978     { 0xd1, "pgdn" },
979     { 0xcf, "end" },
980
981     { 0xcb, "left" },
982     { 0xc8, "up" },
983     { 0xd0, "down" },
984     { 0xcd, "right" },
985
986     { 0xd2, "insert" },
987     { 0xd3, "delete" },
988 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
989     { 0xf0, "stop" },
990     { 0xf1, "again" },
991     { 0xf2, "props" },
992     { 0xf3, "undo" },
993     { 0xf4, "front" },
994     { 0xf5, "copy" },
995     { 0xf6, "open" },
996     { 0xf7, "paste" },
997     { 0xf8, "find" },
998     { 0xf9, "cut" },
999     { 0xfa, "lf" },
1000     { 0xfb, "help" },
1001     { 0xfc, "meta_l" },
1002     { 0xfd, "meta_r" },
1003     { 0xfe, "compose" },
1004 #endif
1005     { 0, NULL },
1006 };
1007
1008 static int get_keycode(const char *key)
1009 {
1010     const KeyDef *p;
1011     char *endp;
1012     int ret;
1013
1014     for(p = key_defs; p->name != NULL; p++) {
1015         if (!strcmp(key, p->name))
1016             return p->keycode;
1017     }
1018     if (strstart(key, "0x", NULL)) {
1019         ret = strtoul(key, &endp, 0);
1020         if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1021             return ret;
1022     }
1023     return -1;
1024 }
1025
1026 #define MAX_KEYCODES 16
1027 static uint8_t keycodes[MAX_KEYCODES];
1028 static int nb_pending_keycodes;
1029 static QEMUTimer *key_timer;
1030
1031 static void release_keys(void *opaque)
1032 {
1033     int keycode;
1034
1035     while (nb_pending_keycodes > 0) {
1036         nb_pending_keycodes--;
1037         keycode = keycodes[nb_pending_keycodes];
1038         if (keycode & 0x80)
1039             kbd_put_keycode(0xe0);
1040         kbd_put_keycode(keycode | 0x80);
1041     }
1042 }
1043
1044 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1045                        int hold_time)
1046 {
1047     char keyname_buf[16];
1048     char *separator;
1049     int keyname_len, keycode, i;
1050
1051     if (nb_pending_keycodes > 0) {
1052         qemu_del_timer(key_timer);
1053         release_keys(NULL);
1054     }
1055     if (!has_hold_time)
1056         hold_time = 100;
1057     i = 0;
1058     while (1) {
1059         separator = strchr(string, '-');
1060         keyname_len = separator ? separator - string : strlen(string);
1061         if (keyname_len > 0) {
1062             pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1063             if (keyname_len > sizeof(keyname_buf) - 1) {
1064                 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1065                 return;
1066             }
1067             if (i == MAX_KEYCODES) {
1068                 monitor_printf(mon, "too many keys\n");
1069                 return;
1070             }
1071             keyname_buf[keyname_len] = 0;
1072             keycode = get_keycode(keyname_buf);
1073             if (keycode < 0) {
1074                 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1075                 return;
1076             }
1077             keycodes[i++] = keycode;
1078         }
1079         if (!separator)
1080             break;
1081         string = separator + 1;
1082     }
1083     nb_pending_keycodes = i;
1084     /* key down events */
1085     for (i = 0; i < nb_pending_keycodes; i++) {
1086         keycode = keycodes[i];
1087         if (keycode & 0x80)
1088             kbd_put_keycode(0xe0);
1089         kbd_put_keycode(keycode & 0x7f);
1090     }
1091     /* delayed key up events */
1092     qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1093                     muldiv64(ticks_per_sec, hold_time, 1000));
1094 }
1095
1096 static int mouse_button_state;
1097
1098 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1099                           const char *dz_str)
1100 {
1101     int dx, dy, dz;
1102     dx = strtol(dx_str, NULL, 0);
1103     dy = strtol(dy_str, NULL, 0);
1104     dz = 0;
1105     if (dz_str)
1106         dz = strtol(dz_str, NULL, 0);
1107     kbd_mouse_event(dx, dy, dz, mouse_button_state);
1108 }
1109
1110 static void do_mouse_button(Monitor *mon, int button_state)
1111 {
1112     mouse_button_state = button_state;
1113     kbd_mouse_event(0, 0, 0, mouse_button_state);
1114 }
1115
1116 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1117                            int addr, int has_index, int index)
1118 {
1119     uint32_t val;
1120     int suffix;
1121
1122     if (has_index) {
1123         cpu_outb(NULL, addr & 0xffff, index & 0xff);
1124         addr++;
1125     }
1126     addr &= 0xffff;
1127
1128     switch(size) {
1129     default:
1130     case 1:
1131         val = cpu_inb(NULL, addr);
1132         suffix = 'b';
1133         break;
1134     case 2:
1135         val = cpu_inw(NULL, addr);
1136         suffix = 'w';
1137         break;
1138     case 4:
1139         val = cpu_inl(NULL, addr);
1140         suffix = 'l';
1141         break;
1142     }
1143     monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1144                    suffix, addr, size * 2, val);
1145 }
1146
1147 /* boot_set handler */
1148 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1149 static void *boot_opaque;
1150
1151 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1152 {
1153     qemu_boot_set_handler = func;
1154     boot_opaque = opaque;
1155 }
1156
1157 static void do_boot_set(Monitor *mon, const char *bootdevice)
1158 {
1159     int res;
1160
1161     if (qemu_boot_set_handler)  {
1162         res = qemu_boot_set_handler(boot_opaque, bootdevice);
1163         if (res == 0)
1164             monitor_printf(mon, "boot device list now set to %s\n",
1165                            bootdevice);
1166         else
1167             monitor_printf(mon, "setting boot device list failed with "
1168                            "error %i\n", res);
1169     } else {
1170         monitor_printf(mon, "no function defined to set boot device list for "
1171                        "this architecture\n");
1172     }
1173 }
1174
1175 static void do_system_reset(Monitor *mon)
1176 {
1177     qemu_system_reset_request();
1178 }
1179
1180 static void do_system_powerdown(Monitor *mon)
1181 {
1182     qemu_system_powerdown_request();
1183 }
1184
1185 #if defined(TARGET_I386)
1186 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1187 {
1188     monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1189                    addr,
1190                    pte & mask,
1191                    pte & PG_GLOBAL_MASK ? 'G' : '-',
1192                    pte & PG_PSE_MASK ? 'P' : '-',
1193                    pte & PG_DIRTY_MASK ? 'D' : '-',
1194                    pte & PG_ACCESSED_MASK ? 'A' : '-',
1195                    pte & PG_PCD_MASK ? 'C' : '-',
1196                    pte & PG_PWT_MASK ? 'T' : '-',
1197                    pte & PG_USER_MASK ? 'U' : '-',
1198                    pte & PG_RW_MASK ? 'W' : '-');
1199 }
1200
1201 static void tlb_info(Monitor *mon)
1202 {
1203     CPUState *env;
1204     int l1, l2;
1205     uint32_t pgd, pde, pte;
1206
1207     env = mon_get_cpu();
1208     if (!env)
1209         return;
1210
1211     if (!(env->cr[0] & CR0_PG_MASK)) {
1212         monitor_printf(mon, "PG disabled\n");
1213         return;
1214     }
1215     pgd = env->cr[3] & ~0xfff;
1216     for(l1 = 0; l1 < 1024; l1++) {
1217         cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1218         pde = le32_to_cpu(pde);
1219         if (pde & PG_PRESENT_MASK) {
1220             if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1221                 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1222             } else {
1223                 for(l2 = 0; l2 < 1024; l2++) {
1224                     cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1225                                              (uint8_t *)&pte, 4);
1226                     pte = le32_to_cpu(pte);
1227                     if (pte & PG_PRESENT_MASK) {
1228                         print_pte(mon, (l1 << 22) + (l2 << 12),
1229                                   pte & ~PG_PSE_MASK,
1230                                   ~0xfff);
1231                     }
1232                 }
1233             }
1234         }
1235     }
1236 }
1237
1238 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1239                       uint32_t end, int prot)
1240 {
1241     int prot1;
1242     prot1 = *plast_prot;
1243     if (prot != prot1) {
1244         if (*pstart != -1) {
1245             monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1246                            *pstart, end, end - *pstart,
1247                            prot1 & PG_USER_MASK ? 'u' : '-',
1248                            'r',
1249                            prot1 & PG_RW_MASK ? 'w' : '-');
1250         }
1251         if (prot != 0)
1252             *pstart = end;
1253         else
1254             *pstart = -1;
1255         *plast_prot = prot;
1256     }
1257 }
1258
1259 static void mem_info(Monitor *mon)
1260 {
1261     CPUState *env;
1262     int l1, l2, prot, last_prot;
1263     uint32_t pgd, pde, pte, start, end;
1264
1265     env = mon_get_cpu();
1266     if (!env)
1267         return;
1268
1269     if (!(env->cr[0] & CR0_PG_MASK)) {
1270         monitor_printf(mon, "PG disabled\n");
1271         return;
1272     }
1273     pgd = env->cr[3] & ~0xfff;
1274     last_prot = 0;
1275     start = -1;
1276     for(l1 = 0; l1 < 1024; l1++) {
1277         cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1278         pde = le32_to_cpu(pde);
1279         end = l1 << 22;
1280         if (pde & PG_PRESENT_MASK) {
1281             if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1282                 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1283                 mem_print(mon, &start, &last_prot, end, prot);
1284             } else {
1285                 for(l2 = 0; l2 < 1024; l2++) {
1286                     cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1287                                              (uint8_t *)&pte, 4);
1288                     pte = le32_to_cpu(pte);
1289                     end = (l1 << 22) + (l2 << 12);
1290                     if (pte & PG_PRESENT_MASK) {
1291                         prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1292                     } else {
1293                         prot = 0;
1294                     }
1295                     mem_print(mon, &start, &last_prot, end, prot);
1296                 }
1297             }
1298         } else {
1299             prot = 0;
1300             mem_print(mon, &start, &last_prot, end, prot);
1301         }
1302     }
1303 }
1304 #endif
1305
1306 #if defined(TARGET_SH4)
1307
1308 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1309 {
1310     monitor_printf(mon, " tlb%i:\t"
1311                    "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1312                    "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1313                    "dirty=%hhu writethrough=%hhu\n",
1314                    idx,
1315                    tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1316                    tlb->v, tlb->sh, tlb->c, tlb->pr,
1317                    tlb->d, tlb->wt);
1318 }
1319
1320 static void tlb_info(Monitor *mon)
1321 {
1322     CPUState *env = mon_get_cpu();
1323     int i;
1324
1325     monitor_printf (mon, "ITLB:\n");
1326     for (i = 0 ; i < ITLB_SIZE ; i++)
1327         print_tlb (mon, i, &env->itlb[i]);
1328     monitor_printf (mon, "UTLB:\n");
1329     for (i = 0 ; i < UTLB_SIZE ; i++)
1330         print_tlb (mon, i, &env->utlb[i]);
1331 }
1332
1333 #endif
1334
1335 static void do_info_kqemu(Monitor *mon)
1336 {
1337 #ifdef USE_KQEMU
1338     CPUState *env;
1339     int val;
1340     val = 0;
1341     env = mon_get_cpu();
1342     if (!env) {
1343         monitor_printf(mon, "No cpu initialized yet");
1344         return;
1345     }
1346     val = env->kqemu_enabled;
1347     monitor_printf(mon, "kqemu support: ");
1348     switch(val) {
1349     default:
1350     case 0:
1351         monitor_printf(mon, "disabled\n");
1352         break;
1353     case 1:
1354         monitor_printf(mon, "enabled for user code\n");
1355         break;
1356     case 2:
1357         monitor_printf(mon, "enabled for user and kernel code\n");
1358         break;
1359     }
1360 #else
1361     monitor_printf(mon, "kqemu support: not compiled\n");
1362 #endif
1363 }
1364
1365 static void do_info_kvm(Monitor *mon)
1366 {
1367 #ifdef CONFIG_KVM
1368     monitor_printf(mon, "kvm support: ");
1369     if (kvm_enabled())
1370         monitor_printf(mon, "enabled\n");
1371     else
1372         monitor_printf(mon, "disabled\n");
1373 #else
1374     monitor_printf(mon, "kvm support: not compiled\n");
1375 #endif
1376 }
1377
1378 #ifdef CONFIG_PROFILER
1379
1380 int64_t kqemu_time;
1381 int64_t qemu_time;
1382 int64_t kqemu_exec_count;
1383 int64_t dev_time;
1384 int64_t kqemu_ret_int_count;
1385 int64_t kqemu_ret_excp_count;
1386 int64_t kqemu_ret_intr_count;
1387
1388 static void do_info_profile(Monitor *mon)
1389 {
1390     int64_t total;
1391     total = qemu_time;
1392     if (total == 0)
1393         total = 1;
1394     monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1395                    dev_time, dev_time / (double)ticks_per_sec);
1396     monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1397                    qemu_time, qemu_time / (double)ticks_per_sec);
1398     monitor_printf(mon, "kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%"
1399                         PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1400                         PRId64 "\n",
1401                    kqemu_time, kqemu_time / (double)ticks_per_sec,
1402                    kqemu_time / (double)total * 100.0,
1403                    kqemu_exec_count,
1404                    kqemu_ret_int_count,
1405                    kqemu_ret_excp_count,
1406                    kqemu_ret_intr_count);
1407     qemu_time = 0;
1408     kqemu_time = 0;
1409     kqemu_exec_count = 0;
1410     dev_time = 0;
1411     kqemu_ret_int_count = 0;
1412     kqemu_ret_excp_count = 0;
1413     kqemu_ret_intr_count = 0;
1414 #ifdef USE_KQEMU
1415     kqemu_record_dump();
1416 #endif
1417 }
1418 #else
1419 static void do_info_profile(Monitor *mon)
1420 {
1421     monitor_printf(mon, "Internal profiler not compiled\n");
1422 }
1423 #endif
1424
1425 /* Capture support */
1426 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1427
1428 static void do_info_capture(Monitor *mon)
1429 {
1430     int i;
1431     CaptureState *s;
1432
1433     for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1434         monitor_printf(mon, "[%d]: ", i);
1435         s->ops.info (s->opaque);
1436     }
1437 }
1438
1439 static void do_stop_capture(Monitor *mon, int n)
1440 {
1441     int i;
1442     CaptureState *s;
1443
1444     for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1445         if (i == n) {
1446             s->ops.destroy (s->opaque);
1447             LIST_REMOVE (s, entries);
1448             qemu_free (s);
1449             return;
1450         }
1451     }
1452 }
1453
1454 #ifdef HAS_AUDIO
1455 static void do_wav_capture(Monitor *mon, const char *path,
1456                            int has_freq, int freq,
1457                            int has_bits, int bits,
1458                            int has_channels, int nchannels)
1459 {
1460     CaptureState *s;
1461
1462     s = qemu_mallocz (sizeof (*s));
1463
1464     freq = has_freq ? freq : 44100;
1465     bits = has_bits ? bits : 16;
1466     nchannels = has_channels ? nchannels : 2;
1467
1468     if (wav_start_capture (s, path, freq, bits, nchannels)) {
1469         monitor_printf(mon, "Faied to add wave capture\n");
1470         qemu_free (s);
1471     }
1472     LIST_INSERT_HEAD (&capture_head, s, entries);
1473 }
1474 #endif
1475
1476 #if defined(TARGET_I386)
1477 static void do_inject_nmi(Monitor *mon, int cpu_index)
1478 {
1479     CPUState *env;
1480
1481     for (env = first_cpu; env != NULL; env = env->next_cpu)
1482         if (env->cpu_index == cpu_index) {
1483             cpu_interrupt(env, CPU_INTERRUPT_NMI);
1484             break;
1485         }
1486 }
1487 #endif
1488
1489 static void do_info_status(Monitor *mon)
1490 {
1491     if (vm_running)
1492        monitor_printf(mon, "VM status: running\n");
1493     else
1494        monitor_printf(mon, "VM status: paused\n");
1495 }
1496
1497
1498 static void do_balloon(Monitor *mon, int value)
1499 {
1500     ram_addr_t target = value;
1501     qemu_balloon(target << 20);
1502 }
1503
1504 static void do_info_balloon(Monitor *mon)
1505 {
1506     ram_addr_t actual;
1507
1508     actual = qemu_balloon_status();
1509     if (kvm_enabled() && !kvm_has_sync_mmu())
1510         monitor_printf(mon, "Using KVM without synchronous MMU, "
1511                        "ballooning disabled\n");
1512     else if (actual == 0)
1513         monitor_printf(mon, "Ballooning not activated in VM\n");
1514     else
1515         monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1516 }
1517
1518 /* Please update qemu-doc.texi when adding or changing commands */
1519 static const mon_cmd_t mon_cmds[] = {
1520     { "help|?", "s?", help_cmd,
1521       "[cmd]", "show the help" },
1522     { "commit", "s", do_commit,
1523       "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1524     { "info", "s?", do_info,
1525       "subcommand", "show various information about the system state" },
1526     { "q|quit", "", do_quit,
1527       "", "quit the emulator" },
1528     { "eject", "-fB", do_eject,
1529       "[-f] device", "eject a removable medium (use -f to force it)" },
1530     { "change", "BFs?", do_change,
1531       "device filename [format]", "change a removable medium, optional format" },
1532     { "screendump", "F", do_screen_dump,
1533       "filename", "save screen into PPM image 'filename'" },
1534     { "logfile", "F", do_logfile,
1535       "filename", "output logs to 'filename'" },
1536     { "log", "s", do_log,
1537       "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1538     { "savevm", "s?", do_savevm,
1539       "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1540     { "loadvm", "s", do_loadvm,
1541       "tag|id", "restore a VM snapshot from its tag or id" },
1542     { "delvm", "s", do_delvm,
1543       "tag|id", "delete a VM snapshot from its tag or id" },
1544     { "stop", "", do_stop,
1545       "", "stop emulation", },
1546     { "c|cont", "", do_cont,
1547       "", "resume emulation", },
1548 #ifdef CONFIG_GDBSTUB
1549     { "gdbserver", "s?", do_gdbserver,
1550       "[port]", "start gdbserver session (default port=1234)", },
1551 #endif
1552     { "x", "/l", do_memory_dump,
1553       "/fmt addr", "virtual memory dump starting at 'addr'", },
1554     { "xp", "/l", do_physical_memory_dump,
1555       "/fmt addr", "physical memory dump starting at 'addr'", },
1556     { "p|print", "/l", do_print,
1557       "/fmt expr", "print expression value (use $reg for CPU register access)", },
1558     { "i", "/ii.", do_ioport_read,
1559       "/fmt addr", "I/O port read" },
1560
1561     { "sendkey", "si?", do_sendkey,
1562       "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1563     { "system_reset", "", do_system_reset,
1564       "", "reset the system" },
1565     { "system_powerdown", "", do_system_powerdown,
1566       "", "send system power down event" },
1567     { "sum", "ii", do_sum,
1568       "addr size", "compute the checksum of a memory region" },
1569     { "usb_add", "s", do_usb_add,
1570       "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1571     { "usb_del", "s", do_usb_del,
1572       "device", "remove USB device 'bus.addr'" },
1573     { "cpu", "i", do_cpu_set,
1574       "index", "set the default CPU" },
1575     { "mouse_move", "sss?", do_mouse_move,
1576       "dx dy [dz]", "send mouse move events" },
1577     { "mouse_button", "i", do_mouse_button,
1578       "state", "change mouse button state (1=L, 2=M, 4=R)" },
1579     { "mouse_set", "i", do_mouse_set,
1580       "index", "set which mouse device receives events" },
1581 #ifdef HAS_AUDIO
1582     { "wavcapture", "si?i?i?", do_wav_capture,
1583       "path [frequency bits channels]",
1584       "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1585 #endif
1586     { "stopcapture", "i", do_stop_capture,
1587       "capture index", "stop capture" },
1588     { "memsave", "lis", do_memory_save,
1589       "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1590     { "pmemsave", "lis", do_physical_memory_save,
1591       "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1592     { "boot_set", "s", do_boot_set,
1593       "bootdevice", "define new values for the boot device list" },
1594 #if defined(TARGET_I386)
1595     { "nmi", "i", do_inject_nmi,
1596       "cpu", "inject an NMI on the given CPU", },
1597 #endif
1598     { "migrate", "-ds", do_migrate,
1599       "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1600     { "migrate_cancel", "", do_migrate_cancel,
1601       "", "cancel the current VM migration" },
1602     { "migrate_set_speed", "s", do_migrate_set_speed,
1603       "value", "set maximum speed (in bytes) for migrations" },
1604 #if defined(TARGET_I386)
1605     { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1606                                          "[file=file][,if=type][,bus=n]\n"
1607                                         "[,unit=m][,media=d][index=i]\n"
1608                                         "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1609                                         "[snapshot=on|off][,cache=on|off]",
1610                                         "add drive to PCI storage controller" },
1611     { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1612     { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1613     { "host_net_add", "ss", net_host_device_add,
1614       "[tap,user,socket,vde] options", "add host VLAN client" },
1615     { "host_net_remove", "is", net_host_device_remove,
1616       "vlan_id name", "remove host VLAN client" },
1617 #endif
1618     { "balloon", "i", do_balloon,
1619       "target", "request VM to change it's memory allocation (in MB)" },
1620     { "set_link", "ss", do_set_link,
1621       "name [up|down]", "change the link status of a network adapter" },
1622     { NULL, NULL, },
1623 };
1624
1625 /* Please update qemu-doc.texi when adding or changing commands */
1626 static const mon_cmd_t info_cmds[] = {
1627     { "version", "", do_info_version,
1628       "", "show the version of QEMU" },
1629     { "network", "", do_info_network,
1630       "", "show the network state" },
1631     { "chardev", "", qemu_chr_info,
1632       "", "show the character devices" },
1633     { "block", "", bdrv_info,
1634       "", "show the block devices" },
1635     { "blockstats", "", bdrv_info_stats,
1636       "", "show block device statistics" },
1637     { "registers", "", do_info_registers,
1638       "", "show the cpu registers" },
1639     { "cpus", "", do_info_cpus,
1640       "", "show infos for each CPU" },
1641     { "history", "", do_info_history,
1642       "", "show the command line history", },
1643     { "irq", "", irq_info,
1644       "", "show the interrupts statistics (if available)", },
1645     { "pic", "", pic_info,
1646       "", "show i8259 (PIC) state", },
1647     { "pci", "", pci_info,
1648       "", "show PCI info", },
1649 #if defined(TARGET_I386) || defined(TARGET_SH4)
1650     { "tlb", "", tlb_info,
1651       "", "show virtual to physical memory mappings", },
1652 #endif
1653 #if defined(TARGET_I386)
1654     { "mem", "", mem_info,
1655       "", "show the active virtual memory mappings", },
1656     { "hpet", "", do_info_hpet,
1657       "", "show state of HPET", },
1658 #endif
1659     { "jit", "", do_info_jit,
1660       "", "show dynamic compiler info", },
1661     { "kqemu", "", do_info_kqemu,
1662       "", "show KQEMU information", },
1663     { "kvm", "", do_info_kvm,
1664       "", "show KVM information", },
1665     { "usb", "", usb_info,
1666       "", "show guest USB devices", },
1667     { "usbhost", "", usb_host_info,
1668       "", "show host USB devices", },
1669     { "profile", "", do_info_profile,
1670       "", "show profiling information", },
1671     { "capture", "", do_info_capture,
1672       "", "show capture information" },
1673     { "snapshots", "", do_info_snapshots,
1674       "", "show the currently saved VM snapshots" },
1675     { "status", "", do_info_status,
1676       "", "show the current VM status (running|paused)" },
1677     { "pcmcia", "", pcmcia_info,
1678       "", "show guest PCMCIA status" },
1679     { "mice", "", do_info_mice,
1680       "", "show which guest mouse is receiving events" },
1681     { "vnc", "", do_info_vnc,
1682       "", "show the vnc server status"},
1683     { "name", "", do_info_name,
1684       "", "show the current VM name" },
1685     { "uuid", "", do_info_uuid,
1686       "", "show the current VM UUID" },
1687 #if defined(TARGET_PPC)
1688     { "cpustats", "", do_info_cpu_stats,
1689       "", "show CPU statistics", },
1690 #endif
1691 #if defined(CONFIG_SLIRP)
1692     { "slirp", "", do_info_slirp,
1693       "", "show SLIRP statistics", },
1694 #endif
1695     { "migrate", "", do_info_migrate, "", "show migration status" },
1696     { "balloon", "", do_info_balloon,
1697       "", "show balloon information" },
1698     { NULL, NULL, },
1699 };
1700
1701 /*******************************************************************/
1702
1703 static const char *pch;
1704 static jmp_buf expr_env;
1705
1706 #define MD_TLONG 0
1707 #define MD_I32   1
1708
1709 typedef struct MonitorDef {
1710     const char *name;
1711     int offset;
1712     target_long (*get_value)(const struct MonitorDef *md, int val);
1713     int type;
1714 } MonitorDef;
1715
1716 #if defined(TARGET_I386)
1717 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1718 {
1719     CPUState *env = mon_get_cpu();
1720     if (!env)
1721         return 0;
1722     return env->eip + env->segs[R_CS].base;
1723 }
1724 #endif
1725
1726 #if defined(TARGET_PPC)
1727 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1728 {
1729     CPUState *env = mon_get_cpu();
1730     unsigned int u;
1731     int i;
1732
1733     if (!env)
1734         return 0;
1735
1736     u = 0;
1737     for (i = 0; i < 8; i++)
1738         u |= env->crf[i] << (32 - (4 * i));
1739
1740     return u;
1741 }
1742
1743 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1744 {
1745     CPUState *env = mon_get_cpu();
1746     if (!env)
1747         return 0;
1748     return env->msr;
1749 }
1750
1751 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1752 {
1753     CPUState *env = mon_get_cpu();
1754     if (!env)
1755         return 0;
1756     return env->xer;
1757 }
1758
1759 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1760 {
1761     CPUState *env = mon_get_cpu();
1762     if (!env)
1763         return 0;
1764     return cpu_ppc_load_decr(env);
1765 }
1766
1767 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1768 {
1769     CPUState *env = mon_get_cpu();
1770     if (!env)
1771         return 0;
1772     return cpu_ppc_load_tbu(env);
1773 }
1774
1775 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1776 {
1777     CPUState *env = mon_get_cpu();
1778     if (!env)
1779         return 0;
1780     return cpu_ppc_load_tbl(env);
1781 }
1782 #endif
1783
1784 #if defined(TARGET_SPARC)
1785 #ifndef TARGET_SPARC64
1786 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1787 {
1788     CPUState *env = mon_get_cpu();
1789     if (!env)
1790         return 0;
1791     return GET_PSR(env);
1792 }
1793 #endif
1794
1795 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1796 {
1797     CPUState *env = mon_get_cpu();
1798     if (!env)
1799         return 0;
1800     return env->regwptr[val];
1801 }
1802 #endif
1803
1804 static const MonitorDef monitor_defs[] = {
1805 #ifdef TARGET_I386
1806
1807 #define SEG(name, seg) \
1808     { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1809     { name ".base", offsetof(CPUState, segs[seg].base) },\
1810     { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1811
1812     { "eax", offsetof(CPUState, regs[0]) },
1813     { "ecx", offsetof(CPUState, regs[1]) },
1814     { "edx", offsetof(CPUState, regs[2]) },
1815     { "ebx", offsetof(CPUState, regs[3]) },
1816     { "esp|sp", offsetof(CPUState, regs[4]) },
1817     { "ebp|fp", offsetof(CPUState, regs[5]) },
1818     { "esi", offsetof(CPUState, regs[6]) },
1819     { "edi", offsetof(CPUState, regs[7]) },
1820 #ifdef TARGET_X86_64
1821     { "r8", offsetof(CPUState, regs[8]) },
1822     { "r9", offsetof(CPUState, regs[9]) },
1823     { "r10", offsetof(CPUState, regs[10]) },
1824     { "r11", offsetof(CPUState, regs[11]) },
1825     { "r12", offsetof(CPUState, regs[12]) },
1826     { "r13", offsetof(CPUState, regs[13]) },
1827     { "r14", offsetof(CPUState, regs[14]) },
1828     { "r15", offsetof(CPUState, regs[15]) },
1829 #endif
1830     { "eflags", offsetof(CPUState, eflags) },
1831     { "eip", offsetof(CPUState, eip) },
1832     SEG("cs", R_CS)
1833     SEG("ds", R_DS)
1834     SEG("es", R_ES)
1835     SEG("ss", R_SS)
1836     SEG("fs", R_FS)
1837     SEG("gs", R_GS)
1838     { "pc", 0, monitor_get_pc, },
1839 #elif defined(TARGET_PPC)
1840     /* General purpose registers */
1841     { "r0", offsetof(CPUState, gpr[0]) },
1842     { "r1", offsetof(CPUState, gpr[1]) },
1843     { "r2", offsetof(CPUState, gpr[2]) },
1844     { "r3", offsetof(CPUState, gpr[3]) },
1845     { "r4", offsetof(CPUState, gpr[4]) },
1846     { "r5", offsetof(CPUState, gpr[5]) },
1847     { "r6", offsetof(CPUState, gpr[6]) },
1848     { "r7", offsetof(CPUState, gpr[7]) },
1849     { "r8", offsetof(CPUState, gpr[8]) },
1850     { "r9", offsetof(CPUState, gpr[9]) },
1851     { "r10", offsetof(CPUState, gpr[10]) },
1852     { "r11", offsetof(CPUState, gpr[11]) },
1853     { "r12", offsetof(CPUState, gpr[12]) },
1854     { "r13", offsetof(CPUState, gpr[13]) },
1855     { "r14", offsetof(CPUState, gpr[14]) },
1856     { "r15", offsetof(CPUState, gpr[15]) },
1857     { "r16", offsetof(CPUState, gpr[16]) },
1858     { "r17", offsetof(CPUState, gpr[17]) },
1859     { "r18", offsetof(CPUState, gpr[18]) },
1860     { "r19", offsetof(CPUState, gpr[19]) },
1861     { "r20", offsetof(CPUState, gpr[20]) },
1862     { "r21", offsetof(CPUState, gpr[21]) },
1863     { "r22", offsetof(CPUState, gpr[22]) },
1864     { "r23", offsetof(CPUState, gpr[23]) },
1865     { "r24", offsetof(CPUState, gpr[24]) },
1866     { "r25", offsetof(CPUState, gpr[25]) },
1867     { "r26", offsetof(CPUState, gpr[26]) },
1868     { "r27", offsetof(CPUState, gpr[27]) },
1869     { "r28", offsetof(CPUState, gpr[28]) },
1870     { "r29", offsetof(CPUState, gpr[29]) },
1871     { "r30", offsetof(CPUState, gpr[30]) },
1872     { "r31", offsetof(CPUState, gpr[31]) },
1873     /* Floating point registers */
1874     { "f0", offsetof(CPUState, fpr[0]) },
1875     { "f1", offsetof(CPUState, fpr[1]) },
1876     { "f2", offsetof(CPUState, fpr[2]) },
1877     { "f3", offsetof(CPUState, fpr[3]) },
1878     { "f4", offsetof(CPUState, fpr[4]) },
1879     { "f5", offsetof(CPUState, fpr[5]) },
1880     { "f6", offsetof(CPUState, fpr[6]) },
1881     { "f7", offsetof(CPUState, fpr[7]) },
1882     { "f8", offsetof(CPUState, fpr[8]) },
1883     { "f9", offsetof(CPUState, fpr[9]) },
1884     { "f10", offsetof(CPUState, fpr[10]) },
1885     { "f11", offsetof(CPUState, fpr[11]) },
1886     { "f12", offsetof(CPUState, fpr[12]) },
1887     { "f13", offsetof(CPUState, fpr[13]) },
1888     { "f14", offsetof(CPUState, fpr[14]) },
1889     { "f15", offsetof(CPUState, fpr[15]) },
1890     { "f16", offsetof(CPUState, fpr[16]) },
1891     { "f17", offsetof(CPUState, fpr[17]) },
1892     { "f18", offsetof(CPUState, fpr[18]) },
1893     { "f19", offsetof(CPUState, fpr[19]) },
1894     { "f20", offsetof(CPUState, fpr[20]) },
1895     { "f21", offsetof(CPUState, fpr[21]) },
1896     { "f22", offsetof(CPUState, fpr[22]) },
1897     { "f23", offsetof(CPUState, fpr[23]) },
1898     { "f24", offsetof(CPUState, fpr[24]) },
1899     { "f25", offsetof(CPUState, fpr[25]) },
1900     { "f26", offsetof(CPUState, fpr[26]) },
1901     { "f27", offsetof(CPUState, fpr[27]) },
1902     { "f28", offsetof(CPUState, fpr[28]) },
1903     { "f29", offsetof(CPUState, fpr[29]) },
1904     { "f30", offsetof(CPUState, fpr[30]) },
1905     { "f31", offsetof(CPUState, fpr[31]) },
1906     { "fpscr", offsetof(CPUState, fpscr) },
1907     /* Next instruction pointer */
1908     { "nip|pc", offsetof(CPUState, nip) },
1909     { "lr", offsetof(CPUState, lr) },
1910     { "ctr", offsetof(CPUState, ctr) },
1911     { "decr", 0, &monitor_get_decr, },
1912     { "ccr", 0, &monitor_get_ccr, },
1913     /* Machine state register */
1914     { "msr", 0, &monitor_get_msr, },
1915     { "xer", 0, &monitor_get_xer, },
1916     { "tbu", 0, &monitor_get_tbu, },
1917     { "tbl", 0, &monitor_get_tbl, },
1918 #if defined(TARGET_PPC64)
1919     /* Address space register */
1920     { "asr", offsetof(CPUState, asr) },
1921 #endif
1922     /* Segment registers */
1923     { "sdr1", offsetof(CPUState, sdr1) },
1924     { "sr0", offsetof(CPUState, sr[0]) },
1925     { "sr1", offsetof(CPUState, sr[1]) },
1926     { "sr2", offsetof(CPUState, sr[2]) },
1927     { "sr3", offsetof(CPUState, sr[3]) },
1928     { "sr4", offsetof(CPUState, sr[4]) },
1929     { "sr5", offsetof(CPUState, sr[5]) },
1930     { "sr6", offsetof(CPUState, sr[6]) },
1931     { "sr7", offsetof(CPUState, sr[7]) },
1932     { "sr8", offsetof(CPUState, sr[8]) },
1933     { "sr9", offsetof(CPUState, sr[9]) },
1934     { "sr10", offsetof(CPUState, sr[10]) },
1935     { "sr11", offsetof(CPUState, sr[11]) },
1936     { "sr12", offsetof(CPUState, sr[12]) },
1937     { "sr13", offsetof(CPUState, sr[13]) },
1938     { "sr14", offsetof(CPUState, sr[14]) },
1939     { "sr15", offsetof(CPUState, sr[15]) },
1940     /* Too lazy to put BATs and SPRs ... */
1941 #elif defined(TARGET_SPARC)
1942     { "g0", offsetof(CPUState, gregs[0]) },
1943     { "g1", offsetof(CPUState, gregs[1]) },
1944     { "g2", offsetof(CPUState, gregs[2]) },
1945     { "g3", offsetof(CPUState, gregs[3]) },
1946     { "g4", offsetof(CPUState, gregs[4]) },
1947     { "g5", offsetof(CPUState, gregs[5]) },
1948     { "g6", offsetof(CPUState, gregs[6]) },
1949     { "g7", offsetof(CPUState, gregs[7]) },
1950     { "o0", 0, monitor_get_reg },
1951     { "o1", 1, monitor_get_reg },
1952     { "o2", 2, monitor_get_reg },
1953     { "o3", 3, monitor_get_reg },
1954     { "o4", 4, monitor_get_reg },
1955     { "o5", 5, monitor_get_reg },
1956     { "o6", 6, monitor_get_reg },
1957     { "o7", 7, monitor_get_reg },
1958     { "l0", 8, monitor_get_reg },
1959     { "l1", 9, monitor_get_reg },
1960     { "l2", 10, monitor_get_reg },
1961     { "l3", 11, monitor_get_reg },
1962     { "l4", 12, monitor_get_reg },
1963     { "l5", 13, monitor_get_reg },
1964     { "l6", 14, monitor_get_reg },
1965     { "l7", 15, monitor_get_reg },
1966     { "i0", 16, monitor_get_reg },
1967     { "i1", 17, monitor_get_reg },
1968     { "i2", 18, monitor_get_reg },
1969     { "i3", 19, monitor_get_reg },
1970     { "i4", 20, monitor_get_reg },
1971     { "i5", 21, monitor_get_reg },
1972     { "i6", 22, monitor_get_reg },
1973     { "i7", 23, monitor_get_reg },
1974     { "pc", offsetof(CPUState, pc) },
1975     { "npc", offsetof(CPUState, npc) },
1976     { "y", offsetof(CPUState, y) },
1977 #ifndef TARGET_SPARC64
1978     { "psr", 0, &monitor_get_psr, },
1979     { "wim", offsetof(CPUState, wim) },
1980 #endif
1981     { "tbr", offsetof(CPUState, tbr) },
1982     { "fsr", offsetof(CPUState, fsr) },
1983     { "f0", offsetof(CPUState, fpr[0]) },
1984     { "f1", offsetof(CPUState, fpr[1]) },
1985     { "f2", offsetof(CPUState, fpr[2]) },
1986     { "f3", offsetof(CPUState, fpr[3]) },
1987     { "f4", offsetof(CPUState, fpr[4]) },
1988     { "f5", offsetof(CPUState, fpr[5]) },
1989     { "f6", offsetof(CPUState, fpr[6]) },
1990     { "f7", offsetof(CPUState, fpr[7]) },
1991     { "f8", offsetof(CPUState, fpr[8]) },
1992     { "f9", offsetof(CPUState, fpr[9]) },
1993     { "f10", offsetof(CPUState, fpr[10]) },
1994     { "f11", offsetof(CPUState, fpr[11]) },
1995     { "f12", offsetof(CPUState, fpr[12]) },
1996     { "f13", offsetof(CPUState, fpr[13]) },
1997     { "f14", offsetof(CPUState, fpr[14]) },
1998     { "f15", offsetof(CPUState, fpr[15]) },
1999     { "f16", offsetof(CPUState, fpr[16]) },
2000     { "f17", offsetof(CPUState, fpr[17]) },
2001     { "f18", offsetof(CPUState, fpr[18]) },
2002     { "f19", offsetof(CPUState, fpr[19]) },
2003     { "f20", offsetof(CPUState, fpr[20]) },
2004     { "f21", offsetof(CPUState, fpr[21]) },
2005     { "f22", offsetof(CPUState, fpr[22]) },
2006     { "f23", offsetof(CPUState, fpr[23]) },
2007     { "f24", offsetof(CPUState, fpr[24]) },
2008     { "f25", offsetof(CPUState, fpr[25]) },
2009     { "f26", offsetof(CPUState, fpr[26]) },
2010     { "f27", offsetof(CPUState, fpr[27]) },
2011     { "f28", offsetof(CPUState, fpr[28]) },
2012     { "f29", offsetof(CPUState, fpr[29]) },
2013     { "f30", offsetof(CPUState, fpr[30]) },
2014     { "f31", offsetof(CPUState, fpr[31]) },
2015 #ifdef TARGET_SPARC64
2016     { "f32", offsetof(CPUState, fpr[32]) },
2017     { "f34", offsetof(CPUState, fpr[34]) },
2018     { "f36", offsetof(CPUState, fpr[36]) },
2019     { "f38", offsetof(CPUState, fpr[38]) },
2020     { "f40", offsetof(CPUState, fpr[40]) },
2021     { "f42", offsetof(CPUState, fpr[42]) },
2022     { "f44", offsetof(CPUState, fpr[44]) },
2023     { "f46", offsetof(CPUState, fpr[46]) },
2024     { "f48", offsetof(CPUState, fpr[48]) },
2025     { "f50", offsetof(CPUState, fpr[50]) },
2026     { "f52", offsetof(CPUState, fpr[52]) },
2027     { "f54", offsetof(CPUState, fpr[54]) },
2028     { "f56", offsetof(CPUState, fpr[56]) },
2029     { "f58", offsetof(CPUState, fpr[58]) },
2030     { "f60", offsetof(CPUState, fpr[60]) },
2031     { "f62", offsetof(CPUState, fpr[62]) },
2032     { "asi", offsetof(CPUState, asi) },
2033     { "pstate", offsetof(CPUState, pstate) },
2034     { "cansave", offsetof(CPUState, cansave) },
2035     { "canrestore", offsetof(CPUState, canrestore) },
2036     { "otherwin", offsetof(CPUState, otherwin) },
2037     { "wstate", offsetof(CPUState, wstate) },
2038     { "cleanwin", offsetof(CPUState, cleanwin) },
2039     { "fprs", offsetof(CPUState, fprs) },
2040 #endif
2041 #endif
2042     { NULL },
2043 };
2044
2045 static void expr_error(Monitor *mon, const char *msg)
2046 {
2047     monitor_printf(mon, "%s\n", msg);
2048     longjmp(expr_env, 1);
2049 }
2050
2051 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2052 static int get_monitor_def(target_long *pval, const char *name)
2053 {
2054     const MonitorDef *md;
2055     void *ptr;
2056
2057     for(md = monitor_defs; md->name != NULL; md++) {
2058         if (compare_cmd(name, md->name)) {
2059             if (md->get_value) {
2060                 *pval = md->get_value(md, md->offset);
2061             } else {
2062                 CPUState *env = mon_get_cpu();
2063                 if (!env)
2064                     return -2;
2065                 ptr = (uint8_t *)env + md->offset;
2066                 switch(md->type) {
2067                 case MD_I32:
2068                     *pval = *(int32_t *)ptr;
2069                     break;
2070                 case MD_TLONG:
2071                     *pval = *(target_long *)ptr;
2072                     break;
2073                 default:
2074                     *pval = 0;
2075                     break;
2076                 }
2077             }
2078             return 0;
2079         }
2080     }
2081     return -1;
2082 }
2083
2084 static void next(void)
2085 {
2086     if (pch != '\0') {
2087         pch++;
2088         while (qemu_isspace(*pch))
2089             pch++;
2090     }
2091 }
2092
2093 static int64_t expr_sum(Monitor *mon);
2094
2095 static int64_t expr_unary(Monitor *mon)
2096 {
2097     int64_t n;
2098     char *p;
2099     int ret;
2100
2101     switch(*pch) {
2102     case '+':
2103         next();
2104         n = expr_unary(mon);
2105         break;
2106     case '-':
2107         next();
2108         n = -expr_unary(mon);
2109         break;
2110     case '~':
2111         next();
2112         n = ~expr_unary(mon);
2113         break;
2114     case '(':
2115         next();
2116         n = expr_sum(mon);
2117         if (*pch != ')') {
2118             expr_error(mon, "')' expected");
2119         }
2120         next();
2121         break;
2122     case '\'':
2123         pch++;
2124         if (*pch == '\0')
2125             expr_error(mon, "character constant expected");
2126         n = *pch;
2127         pch++;
2128         if (*pch != '\'')
2129             expr_error(mon, "missing terminating \' character");
2130         next();
2131         break;
2132     case '$':
2133         {
2134             char buf[128], *q;
2135             target_long reg=0;
2136
2137             pch++;
2138             q = buf;
2139             while ((*pch >= 'a' && *pch <= 'z') ||
2140                    (*pch >= 'A' && *pch <= 'Z') ||
2141                    (*pch >= '0' && *pch <= '9') ||
2142                    *pch == '_' || *pch == '.') {
2143                 if ((q - buf) < sizeof(buf) - 1)
2144                     *q++ = *pch;
2145                 pch++;
2146             }
2147             while (qemu_isspace(*pch))
2148                 pch++;
2149             *q = 0;
2150             ret = get_monitor_def(&reg, buf);
2151             if (ret == -1)
2152                 expr_error(mon, "unknown register");
2153             else if (ret == -2)
2154                 expr_error(mon, "no cpu defined");
2155             n = reg;
2156         }
2157         break;
2158     case '\0':
2159         expr_error(mon, "unexpected end of expression");
2160         n = 0;
2161         break;
2162     default:
2163 #if TARGET_PHYS_ADDR_BITS > 32
2164         n = strtoull(pch, &p, 0);
2165 #else
2166         n = strtoul(pch, &p, 0);
2167 #endif
2168         if (pch == p) {
2169             expr_error(mon, "invalid char in expression");
2170         }
2171         pch = p;
2172         while (qemu_isspace(*pch))
2173             pch++;
2174         break;
2175     }
2176     return n;
2177 }
2178
2179
2180 static int64_t expr_prod(Monitor *mon)
2181 {
2182     int64_t val, val2;
2183     int op;
2184
2185     val = expr_unary(mon);
2186     for(;;) {
2187         op = *pch;
2188         if (op != '*' && op != '/' && op != '%')
2189             break;
2190         next();
2191         val2 = expr_unary(mon);
2192         switch(op) {
2193         default:
2194         case '*':
2195             val *= val2;
2196             break;
2197         case '/':
2198         case '%':
2199             if (val2 == 0)
2200                 expr_error(mon, "division by zero");
2201             if (op == '/')
2202                 val /= val2;
2203             else
2204                 val %= val2;
2205             break;
2206         }
2207     }
2208     return val;
2209 }
2210
2211 static int64_t expr_logic(Monitor *mon)
2212 {
2213     int64_t val, val2;
2214     int op;
2215
2216     val = expr_prod(mon);
2217     for(;;) {
2218         op = *pch;
2219         if (op != '&' && op != '|' && op != '^')
2220             break;
2221         next();
2222         val2 = expr_prod(mon);
2223         switch(op) {
2224         default:
2225         case '&':
2226             val &= val2;
2227             break;
2228         case '|':
2229             val |= val2;
2230             break;
2231         case '^':
2232             val ^= val2;
2233             break;
2234         }
2235     }
2236     return val;
2237 }
2238
2239 static int64_t expr_sum(Monitor *mon)
2240 {
2241     int64_t val, val2;
2242     int op;
2243
2244     val = expr_logic(mon);
2245     for(;;) {
2246         op = *pch;
2247         if (op != '+' && op != '-')
2248             break;
2249         next();
2250         val2 = expr_logic(mon);
2251         if (op == '+')
2252             val += val2;
2253         else
2254             val -= val2;
2255     }
2256     return val;
2257 }
2258
2259 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2260 {
2261     pch = *pp;
2262     if (setjmp(expr_env)) {
2263         *pp = pch;
2264         return -1;
2265     }
2266     while (qemu_isspace(*pch))
2267         pch++;
2268     *pval = expr_sum(mon);
2269     *pp = pch;
2270     return 0;
2271 }
2272
2273 static int get_str(char *buf, int buf_size, const char **pp)
2274 {
2275     const char *p;
2276     char *q;
2277     int c;
2278
2279     q = buf;
2280     p = *pp;
2281     while (qemu_isspace(*p))
2282         p++;
2283     if (*p == '\0') {
2284     fail:
2285         *q = '\0';
2286         *pp = p;
2287         return -1;
2288     }
2289     if (*p == '\"') {
2290         p++;
2291         while (*p != '\0' && *p != '\"') {
2292             if (*p == '\\') {
2293                 p++;
2294                 c = *p++;
2295                 switch(c) {
2296                 case 'n':
2297                     c = '\n';
2298                     break;
2299                 case 'r':
2300                     c = '\r';
2301                     break;
2302                 case '\\':
2303                 case '\'':
2304                 case '\"':
2305                     break;
2306                 default:
2307                     qemu_printf("unsupported escape code: '\\%c'\n", c);
2308                     goto fail;
2309                 }
2310                 if ((q - buf) < buf_size - 1) {
2311                     *q++ = c;
2312                 }
2313             } else {
2314                 if ((q - buf) < buf_size - 1) {
2315                     *q++ = *p;
2316                 }
2317                 p++;
2318             }
2319         }
2320         if (*p != '\"') {
2321             qemu_printf("unterminated string\n");
2322             goto fail;
2323         }
2324         p++;
2325     } else {
2326         while (*p != '\0' && !qemu_isspace(*p)) {
2327             if ((q - buf) < buf_size - 1) {
2328                 *q++ = *p;
2329             }
2330             p++;
2331         }
2332     }
2333     *q = '\0';
2334     *pp = p;
2335     return 0;
2336 }
2337
2338 static int default_fmt_format = 'x';
2339 static int default_fmt_size = 4;
2340
2341 #define MAX_ARGS 16
2342
2343 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2344 {
2345     const char *p, *pstart, *typestr;
2346     char *q;
2347     int c, nb_args, len, i, has_arg;
2348     const mon_cmd_t *cmd;
2349     char cmdname[256];
2350     char buf[1024];
2351     void *str_allocated[MAX_ARGS];
2352     void *args[MAX_ARGS];
2353     void (*handler_0)(Monitor *mon);
2354     void (*handler_1)(Monitor *mon, void *arg0);
2355     void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2356     void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2357     void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2358                       void *arg3);
2359     void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2360                       void *arg3, void *arg4);
2361     void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2362                       void *arg3, void *arg4, void *arg5);
2363     void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2364                       void *arg3, void *arg4, void *arg5, void *arg6);
2365
2366 #ifdef DEBUG
2367     monitor_printf(mon, "command='%s'\n", cmdline);
2368 #endif
2369
2370     /* extract the command name */
2371     p = cmdline;
2372     q = cmdname;
2373     while (qemu_isspace(*p))
2374         p++;
2375     if (*p == '\0')
2376         return;
2377     pstart = p;
2378     while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2379         p++;
2380     len = p - pstart;
2381     if (len > sizeof(cmdname) - 1)
2382         len = sizeof(cmdname) - 1;
2383     memcpy(cmdname, pstart, len);
2384     cmdname[len] = '\0';
2385
2386     /* find the command */
2387     for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2388         if (compare_cmd(cmdname, cmd->name))
2389             goto found;
2390     }
2391     monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2392     return;
2393  found:
2394
2395     for(i = 0; i < MAX_ARGS; i++)
2396         str_allocated[i] = NULL;
2397
2398     /* parse the parameters */
2399     typestr = cmd->args_type;
2400     nb_args = 0;
2401     for(;;) {
2402         c = *typestr;
2403         if (c == '\0')
2404             break;
2405         typestr++;
2406         switch(c) {
2407         case 'F':
2408         case 'B':
2409         case 's':
2410             {
2411                 int ret;
2412                 char *str;
2413
2414                 while (qemu_isspace(*p))
2415                     p++;
2416                 if (*typestr == '?') {
2417                     typestr++;
2418                     if (*p == '\0') {
2419                         /* no optional string: NULL argument */
2420                         str = NULL;
2421                         goto add_str;
2422                     }
2423                 }
2424                 ret = get_str(buf, sizeof(buf), &p);
2425                 if (ret < 0) {
2426                     switch(c) {
2427                     case 'F':
2428                         monitor_printf(mon, "%s: filename expected\n",
2429                                        cmdname);
2430                         break;
2431                     case 'B':
2432                         monitor_printf(mon, "%s: block device name expected\n",
2433                                        cmdname);
2434                         break;
2435                     default:
2436                         monitor_printf(mon, "%s: string expected\n", cmdname);
2437                         break;
2438                     }
2439                     goto fail;
2440                 }
2441                 str = qemu_malloc(strlen(buf) + 1);
2442                 pstrcpy(str, sizeof(buf), buf);
2443                 str_allocated[nb_args] = str;
2444             add_str:
2445                 if (nb_args >= MAX_ARGS) {
2446                 error_args:
2447                     monitor_printf(mon, "%s: too many arguments\n", cmdname);
2448                     goto fail;
2449                 }
2450                 args[nb_args++] = str;
2451             }
2452             break;
2453         case '/':
2454             {
2455                 int count, format, size;
2456
2457                 while (qemu_isspace(*p))
2458                     p++;
2459                 if (*p == '/') {
2460                     /* format found */
2461                     p++;
2462                     count = 1;
2463                     if (qemu_isdigit(*p)) {
2464                         count = 0;
2465                         while (qemu_isdigit(*p)) {
2466                             count = count * 10 + (*p - '0');
2467                             p++;
2468                         }
2469                     }
2470                     size = -1;
2471                     format = -1;
2472                     for(;;) {
2473                         switch(*p) {
2474                         case 'o':
2475                         case 'd':
2476                         case 'u':
2477                         case 'x':
2478                         case 'i':
2479                         case 'c':
2480                             format = *p++;
2481                             break;
2482                         case 'b':
2483                             size = 1;
2484                             p++;
2485                             break;
2486                         case 'h':
2487                             size = 2;
2488                             p++;
2489                             break;
2490                         case 'w':
2491                             size = 4;
2492                             p++;
2493                             break;
2494                         case 'g':
2495                         case 'L':
2496                             size = 8;
2497                             p++;
2498                             break;
2499                         default:
2500                             goto next;
2501                         }
2502                     }
2503                 next:
2504                     if (*p != '\0' && !qemu_isspace(*p)) {
2505                         monitor_printf(mon, "invalid char in format: '%c'\n",
2506                                        *p);
2507                         goto fail;
2508                     }
2509                     if (format < 0)
2510                         format = default_fmt_format;
2511                     if (format != 'i') {
2512                         /* for 'i', not specifying a size gives -1 as size */
2513                         if (size < 0)
2514                             size = default_fmt_size;
2515                         default_fmt_size = size;
2516                     }
2517                     default_fmt_format = format;
2518                 } else {
2519                     count = 1;
2520                     format = default_fmt_format;
2521                     if (format != 'i') {
2522                         size = default_fmt_size;
2523                     } else {
2524                         size = -1;
2525                     }
2526                 }
2527                 if (nb_args + 3 > MAX_ARGS)
2528                     goto error_args;
2529                 args[nb_args++] = (void*)(long)count;
2530                 args[nb_args++] = (void*)(long)format;
2531                 args[nb_args++] = (void*)(long)size;
2532             }
2533             break;
2534         case 'i':
2535         case 'l':
2536             {
2537                 int64_t val;
2538
2539                 while (qemu_isspace(*p))
2540                     p++;
2541                 if (*typestr == '?' || *typestr == '.') {
2542                     if (*typestr == '?') {
2543                         if (*p == '\0')
2544                             has_arg = 0;
2545                         else
2546                             has_arg = 1;
2547                     } else {
2548                         if (*p == '.') {
2549                             p++;
2550                             while (qemu_isspace(*p))
2551                                 p++;
2552                             has_arg = 1;
2553                         } else {
2554                             has_arg = 0;
2555                         }
2556                     }
2557                     typestr++;
2558                     if (nb_args >= MAX_ARGS)
2559                         goto error_args;
2560                     args[nb_args++] = (void *)(long)has_arg;
2561                     if (!has_arg) {
2562                         if (nb_args >= MAX_ARGS)
2563                             goto error_args;
2564                         val = -1;
2565                         goto add_num;
2566                     }
2567                 }
2568                 if (get_expr(mon, &val, &p))
2569                     goto fail;
2570             add_num:
2571                 if (c == 'i') {
2572                     if (nb_args >= MAX_ARGS)
2573                         goto error_args;
2574                     args[nb_args++] = (void *)(long)val;
2575                 } else {
2576                     if ((nb_args + 1) >= MAX_ARGS)
2577                         goto error_args;
2578 #if TARGET_PHYS_ADDR_BITS > 32
2579                     args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2580 #else
2581                     args[nb_args++] = (void *)0;
2582 #endif
2583                     args[nb_args++] = (void *)(long)(val & 0xffffffff);
2584                 }
2585             }
2586             break;
2587         case '-':
2588             {
2589                 int has_option;
2590                 /* option */
2591
2592                 c = *typestr++;
2593                 if (c == '\0')
2594                     goto bad_type;
2595                 while (qemu_isspace(*p))
2596                     p++;
2597                 has_option = 0;
2598                 if (*p == '-') {
2599                     p++;
2600                     if (*p != c) {
2601                         monitor_printf(mon, "%s: unsupported option -%c\n",
2602                                        cmdname, *p);
2603                         goto fail;
2604                     }
2605                     p++;
2606                     has_option = 1;
2607                 }
2608                 if (nb_args >= MAX_ARGS)
2609                     goto error_args;
2610                 args[nb_args++] = (void *)(long)has_option;
2611             }
2612             break;
2613         default:
2614         bad_type:
2615             monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2616             goto fail;
2617         }
2618     }
2619     /* check that all arguments were parsed */
2620     while (qemu_isspace(*p))
2621         p++;
2622     if (*p != '\0') {
2623         monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2624                        cmdname);
2625         goto fail;
2626     }
2627
2628     switch(nb_args) {
2629     case 0:
2630         handler_0 = cmd->handler;
2631         handler_0(mon);
2632         break;
2633     case 1:
2634         handler_1 = cmd->handler;
2635         handler_1(mon, args[0]);
2636         break;
2637     case 2:
2638         handler_2 = cmd->handler;
2639         handler_2(mon, args[0], args[1]);
2640         break;
2641     case 3:
2642         handler_3 = cmd->handler;
2643         handler_3(mon, args[0], args[1], args[2]);
2644         break;
2645     case 4:
2646         handler_4 = cmd->handler;
2647         handler_4(mon, args[0], args[1], args[2], args[3]);
2648         break;
2649     case 5:
2650         handler_5 = cmd->handler;
2651         handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2652         break;
2653     case 6:
2654         handler_6 = cmd->handler;
2655         handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2656         break;
2657     case 7:
2658         handler_7 = cmd->handler;
2659         handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2660                   args[6]);
2661         break;
2662     default:
2663         monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2664         goto fail;
2665     }
2666  fail:
2667     for(i = 0; i < MAX_ARGS; i++)
2668         qemu_free(str_allocated[i]);
2669     return;
2670 }
2671
2672 static void cmd_completion(const char *name, const char *list)
2673 {
2674     const char *p, *pstart;
2675     char cmd[128];
2676     int len;
2677
2678     p = list;
2679     for(;;) {
2680         pstart = p;
2681         p = strchr(p, '|');
2682         if (!p)
2683             p = pstart + strlen(pstart);
2684         len = p - pstart;
2685         if (len > sizeof(cmd) - 2)
2686             len = sizeof(cmd) - 2;
2687         memcpy(cmd, pstart, len);
2688         cmd[len] = '\0';
2689         if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2690             readline_add_completion(cmd);
2691         }
2692         if (*p == '\0')
2693             break;
2694         p++;
2695     }
2696 }
2697
2698 static void file_completion(const char *input)
2699 {
2700     DIR *ffs;
2701     struct dirent *d;
2702     char path[1024];
2703     char file[1024], file_prefix[1024];
2704     int input_path_len;
2705     const char *p;
2706
2707     p = strrchr(input, '/');
2708     if (!p) {
2709         input_path_len = 0;
2710         pstrcpy(file_prefix, sizeof(file_prefix), input);
2711         pstrcpy(path, sizeof(path), ".");
2712     } else {
2713         input_path_len = p - input + 1;
2714         memcpy(path, input, input_path_len);
2715         if (input_path_len > sizeof(path) - 1)
2716             input_path_len = sizeof(path) - 1;
2717         path[input_path_len] = '\0';
2718         pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2719     }
2720 #ifdef DEBUG_COMPLETION
2721     monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2722                    input, path, file_prefix);
2723 #endif
2724     ffs = opendir(path);
2725     if (!ffs)
2726         return;
2727     for(;;) {
2728         struct stat sb;
2729         d = readdir(ffs);
2730         if (!d)
2731             break;
2732         if (strstart(d->d_name, file_prefix, NULL)) {
2733             memcpy(file, input, input_path_len);
2734             if (input_path_len < sizeof(file))
2735                 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2736                         d->d_name);
2737             /* stat the file to find out if it's a directory.
2738              * In that case add a slash to speed up typing long paths
2739              */
2740             stat(file, &sb);
2741             if(S_ISDIR(sb.st_mode))
2742                 pstrcat(file, sizeof(file), "/");
2743             readline_add_completion(file);
2744         }
2745     }
2746     closedir(ffs);
2747 }
2748
2749 static void block_completion_it(void *opaque, BlockDriverState *bs)
2750 {
2751     const char *name = bdrv_get_device_name(bs);
2752     const char *input = opaque;
2753
2754     if (input[0] == '\0' ||
2755         !strncmp(name, (char *)input, strlen(input))) {
2756         readline_add_completion(name);
2757     }
2758 }
2759
2760 /* NOTE: this parser is an approximate form of the real command parser */
2761 static void parse_cmdline(const char *cmdline,
2762                          int *pnb_args, char **args)
2763 {
2764     const char *p;
2765     int nb_args, ret;
2766     char buf[1024];
2767
2768     p = cmdline;
2769     nb_args = 0;
2770     for(;;) {
2771         while (qemu_isspace(*p))
2772             p++;
2773         if (*p == '\0')
2774             break;
2775         if (nb_args >= MAX_ARGS)
2776             break;
2777         ret = get_str(buf, sizeof(buf), &p);
2778         args[nb_args] = qemu_strdup(buf);
2779         nb_args++;
2780         if (ret < 0)
2781             break;
2782     }
2783     *pnb_args = nb_args;
2784 }
2785
2786 void readline_find_completion(const char *cmdline)
2787 {
2788     const char *cmdname;
2789     char *args[MAX_ARGS];
2790     int nb_args, i, len;
2791     const char *ptype, *str;
2792     const mon_cmd_t *cmd;
2793     const KeyDef *key;
2794
2795     parse_cmdline(cmdline, &nb_args, args);
2796 #ifdef DEBUG_COMPLETION
2797     for(i = 0; i < nb_args; i++) {
2798         monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2799     }
2800 #endif
2801
2802     /* if the line ends with a space, it means we want to complete the
2803        next arg */
2804     len = strlen(cmdline);
2805     if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2806         if (nb_args >= MAX_ARGS)
2807             return;
2808         args[nb_args++] = qemu_strdup("");
2809     }
2810     if (nb_args <= 1) {
2811         /* command completion */
2812         if (nb_args == 0)
2813             cmdname = "";
2814         else
2815             cmdname = args[0];
2816         readline_set_completion_index(strlen(cmdname));
2817         for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2818             cmd_completion(cmdname, cmd->name);
2819         }
2820     } else {
2821         /* find the command */
2822         for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2823             if (compare_cmd(args[0], cmd->name))
2824                 goto found;
2825         }
2826         return;
2827     found:
2828         ptype = cmd->args_type;
2829         for(i = 0; i < nb_args - 2; i++) {
2830             if (*ptype != '\0') {
2831                 ptype++;
2832                 while (*ptype == '?')
2833                     ptype++;
2834             }
2835         }
2836         str = args[nb_args - 1];
2837         switch(*ptype) {
2838         case 'F':
2839             /* file completion */
2840             readline_set_completion_index(strlen(str));
2841             file_completion(str);
2842             break;
2843         case 'B':
2844             /* block device name completion */
2845             readline_set_completion_index(strlen(str));
2846             bdrv_iterate(block_completion_it, (void *)str);
2847             break;
2848         case 's':
2849             /* XXX: more generic ? */
2850             if (!strcmp(cmd->name, "info")) {
2851                 readline_set_completion_index(strlen(str));
2852                 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2853                     cmd_completion(str, cmd->name);
2854                 }
2855             } else if (!strcmp(cmd->name, "sendkey")) {
2856                 readline_set_completion_index(strlen(str));
2857                 for(key = key_defs; key->name != NULL; key++) {
2858                     cmd_completion(str, key->name);
2859                 }
2860             }
2861             break;
2862         default:
2863             break;
2864         }
2865     }
2866     for(i = 0; i < nb_args; i++)
2867         qemu_free(args[i]);
2868 }
2869
2870 static int term_can_read(void *opaque)
2871 {
2872     return 128;
2873 }
2874
2875 static void term_read(void *opaque, const uint8_t *buf, int size)
2876 {
2877     int i;
2878
2879     for (i = 0; i < size; i++)
2880         readline_handle_byte(buf[i]);
2881 }
2882
2883 static int monitor_suspended;
2884
2885 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
2886 {
2887     monitor_handle_command(mon, cmdline);
2888     if (!monitor_suspended)
2889         readline_show_prompt();
2890     else
2891         monitor_suspended = 2;
2892 }
2893
2894 void monitor_suspend(Monitor *mon)
2895 {
2896     monitor_suspended = 1;
2897 }
2898
2899 void monitor_resume(Monitor *mon)
2900 {
2901     if (monitor_suspended == 2)
2902         monitor_start_input();
2903     monitor_suspended = 0;
2904 }
2905
2906 static void monitor_start_input(void)
2907 {
2908     readline_start("(qemu) ", 0, monitor_command_cb, NULL);
2909     readline_show_prompt();
2910 }
2911
2912 static void term_event(void *opaque, int event)
2913 {
2914     Monitor *mon = opaque;
2915
2916     if (event != CHR_EVENT_RESET)
2917         return;
2918
2919     monitor_printf(mon, "QEMU %s monitor - type 'help' for more information\n",
2920                    QEMU_VERSION);
2921     monitor_start_input();
2922 }
2923
2924 static int is_first_init = 1;
2925
2926 void monitor_init(CharDriverState *chr)
2927 {
2928     Monitor *mon;
2929
2930     if (is_first_init) {
2931         key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2932         is_first_init = 0;
2933     }
2934
2935     mon = qemu_mallocz(sizeof(*mon));
2936
2937     mon->chr = chr;
2938
2939     qemu_chr_add_handlers(chr, term_can_read, term_read, term_event, mon);
2940
2941     LIST_INSERT_HEAD(&mon_list, mon, entry);
2942     if (!cur_mon)
2943         cur_mon = mon;
2944
2945     readline_start("", 0, monitor_command_cb, NULL);
2946 }
2947
2948 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
2949 {
2950     BlockDriverState *bs = opaque;
2951     int ret = 0;
2952
2953     if (bdrv_set_key(bs, password) != 0) {
2954         monitor_printf(mon, "invalid password\n");
2955         ret = -EPERM;
2956     }
2957     if (password_completion_cb)
2958         password_completion_cb(password_opaque, ret);
2959
2960     monitor_start_input();
2961 }
2962
2963 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
2964                                  BlockDriverCompletionFunc *completion_cb,
2965                                  void *opaque)
2966 {
2967     if (!bdrv_key_required(bs)) {
2968         if (completion_cb)
2969             completion_cb(opaque, 0);
2970         return;
2971     }
2972
2973     monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2974                    bdrv_get_encrypted_filename(bs));
2975
2976     password_completion_cb = completion_cb;
2977     password_opaque = opaque;
2978
2979     monitor_read_password(mon, bdrv_password_cb, bs);
2980 }