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