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