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