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