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