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