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