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