]> git.proxmox.com Git - qemu.git/blame - monitor.c
update
[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{
955 return (0<<28) | (4<<24) | cpu_single_env->psr \
956 | (cpu_single_env->psrs? PSR_S : 0) \
957 | (cpu_single_env->psrs? PSR_PS : 0) \
958 | (cpu_single_env->psret? PSR_ET : 0) \
959 | cpu_single_env->cwp;
960}
961
962static int monitor_get_reg(struct MonitorDef *md, int val)
963{
964 return cpu_single_env->regwptr[val];
965}
966#endif
967
9307c4c1
FB
968static MonitorDef monitor_defs[] = {
969#ifdef TARGET_I386
57206fd4
FB
970
971#define SEG(name, seg) \
972 { name, offsetof(CPUState, segs[seg].selector) },\
973 { name ".base", offsetof(CPUState, segs[seg].base) },\
974 { name ".limit", offsetof(CPUState, segs[seg].limit) },
975
9307c4c1
FB
976 { "eax", offsetof(CPUState, regs[0]) },
977 { "ecx", offsetof(CPUState, regs[1]) },
978 { "edx", offsetof(CPUState, regs[2]) },
979 { "ebx", offsetof(CPUState, regs[3]) },
980 { "esp|sp", offsetof(CPUState, regs[4]) },
981 { "ebp|fp", offsetof(CPUState, regs[5]) },
982 { "esi", offsetof(CPUState, regs[6]) },
01038d2a 983 { "edi", offsetof(CPUState, regs[7]) },
9307c4c1 984 { "eflags", offsetof(CPUState, eflags) },
57206fd4
FB
985 { "eip", offsetof(CPUState, eip) },
986 SEG("cs", R_CS)
987 SEG("ds", R_DS)
988 SEG("es", R_ES)
01038d2a 989 SEG("ss", R_SS)
57206fd4
FB
990 SEG("fs", R_FS)
991 SEG("gs", R_GS)
992 { "pc", 0, monitor_get_pc, },
a541f297
FB
993#elif defined(TARGET_PPC)
994 { "r0", offsetof(CPUState, gpr[0]) },
995 { "r1", offsetof(CPUState, gpr[1]) },
996 { "r2", offsetof(CPUState, gpr[2]) },
997 { "r3", offsetof(CPUState, gpr[3]) },
998 { "r4", offsetof(CPUState, gpr[4]) },
999 { "r5", offsetof(CPUState, gpr[5]) },
1000 { "r6", offsetof(CPUState, gpr[6]) },
1001 { "r7", offsetof(CPUState, gpr[7]) },
1002 { "r8", offsetof(CPUState, gpr[8]) },
1003 { "r9", offsetof(CPUState, gpr[9]) },
1004 { "r10", offsetof(CPUState, gpr[10]) },
1005 { "r11", offsetof(CPUState, gpr[11]) },
1006 { "r12", offsetof(CPUState, gpr[12]) },
1007 { "r13", offsetof(CPUState, gpr[13]) },
1008 { "r14", offsetof(CPUState, gpr[14]) },
1009 { "r15", offsetof(CPUState, gpr[15]) },
1010 { "r16", offsetof(CPUState, gpr[16]) },
1011 { "r17", offsetof(CPUState, gpr[17]) },
1012 { "r18", offsetof(CPUState, gpr[18]) },
1013 { "r19", offsetof(CPUState, gpr[19]) },
1014 { "r20", offsetof(CPUState, gpr[20]) },
1015 { "r21", offsetof(CPUState, gpr[21]) },
1016 { "r22", offsetof(CPUState, gpr[22]) },
1017 { "r23", offsetof(CPUState, gpr[23]) },
1018 { "r24", offsetof(CPUState, gpr[24]) },
1019 { "r25", offsetof(CPUState, gpr[25]) },
1020 { "r26", offsetof(CPUState, gpr[26]) },
1021 { "r27", offsetof(CPUState, gpr[27]) },
1022 { "r28", offsetof(CPUState, gpr[28]) },
1023 { "r29", offsetof(CPUState, gpr[29]) },
1024 { "r30", offsetof(CPUState, gpr[30]) },
1025 { "r31", offsetof(CPUState, gpr[31]) },
57206fd4 1026 { "nip|pc", offsetof(CPUState, nip) },
a541f297
FB
1027 { "lr", offsetof(CPUState, lr) },
1028 { "ctr", offsetof(CPUState, ctr) },
9fddaa0c 1029 { "decr", 0, &monitor_get_decr, },
a541f297
FB
1030 { "ccr", 0, &monitor_get_ccr, },
1031 { "msr", 0, &monitor_get_msr, },
1032 { "xer", 0, &monitor_get_xer, },
9fddaa0c
FB
1033 { "tbu", 0, &monitor_get_tbu, },
1034 { "tbl", 0, &monitor_get_tbl, },
a541f297
FB
1035 { "sdr1", offsetof(CPUState, sdr1) },
1036 { "sr0", offsetof(CPUState, sr[0]) },
1037 { "sr1", offsetof(CPUState, sr[1]) },
1038 { "sr2", offsetof(CPUState, sr[2]) },
1039 { "sr3", offsetof(CPUState, sr[3]) },
1040 { "sr4", offsetof(CPUState, sr[4]) },
1041 { "sr5", offsetof(CPUState, sr[5]) },
1042 { "sr6", offsetof(CPUState, sr[6]) },
1043 { "sr7", offsetof(CPUState, sr[7]) },
1044 { "sr8", offsetof(CPUState, sr[8]) },
1045 { "sr9", offsetof(CPUState, sr[9]) },
1046 { "sr10", offsetof(CPUState, sr[10]) },
1047 { "sr11", offsetof(CPUState, sr[11]) },
1048 { "sr12", offsetof(CPUState, sr[12]) },
1049 { "sr13", offsetof(CPUState, sr[13]) },
1050 { "sr14", offsetof(CPUState, sr[14]) },
1051 { "sr15", offsetof(CPUState, sr[15]) },
1052 /* Too lazy to put BATs and SPRs ... */
e95c8d51
FB
1053#elif defined(TARGET_SPARC)
1054 { "g0", offsetof(CPUState, gregs[0]) },
1055 { "g1", offsetof(CPUState, gregs[1]) },
1056 { "g2", offsetof(CPUState, gregs[2]) },
1057 { "g3", offsetof(CPUState, gregs[3]) },
1058 { "g4", offsetof(CPUState, gregs[4]) },
1059 { "g5", offsetof(CPUState, gregs[5]) },
1060 { "g6", offsetof(CPUState, gregs[6]) },
1061 { "g7", offsetof(CPUState, gregs[7]) },
1062 { "o0", 0, monitor_get_reg },
1063 { "o1", 1, monitor_get_reg },
1064 { "o2", 2, monitor_get_reg },
1065 { "o3", 3, monitor_get_reg },
1066 { "o4", 4, monitor_get_reg },
1067 { "o5", 5, monitor_get_reg },
1068 { "o6", 6, monitor_get_reg },
1069 { "o7", 7, monitor_get_reg },
1070 { "l0", 8, monitor_get_reg },
1071 { "l1", 9, monitor_get_reg },
1072 { "l2", 10, monitor_get_reg },
1073 { "l3", 11, monitor_get_reg },
1074 { "l4", 12, monitor_get_reg },
1075 { "l5", 13, monitor_get_reg },
1076 { "l6", 14, monitor_get_reg },
1077 { "l7", 15, monitor_get_reg },
1078 { "i0", 16, monitor_get_reg },
1079 { "i1", 17, monitor_get_reg },
1080 { "i2", 18, monitor_get_reg },
1081 { "i3", 19, monitor_get_reg },
1082 { "i4", 20, monitor_get_reg },
1083 { "i5", 21, monitor_get_reg },
1084 { "i6", 22, monitor_get_reg },
1085 { "i7", 23, monitor_get_reg },
1086 { "pc", offsetof(CPUState, pc) },
1087 { "npc", offsetof(CPUState, npc) },
1088 { "y", offsetof(CPUState, y) },
1089 { "psr", 0, &monitor_get_psr, },
1090 { "wim", offsetof(CPUState, wim) },
1091 { "tbr", offsetof(CPUState, tbr) },
1092 { "fsr", offsetof(CPUState, fsr) },
1093 { "f0", offsetof(CPUState, fpr[0]) },
1094 { "f1", offsetof(CPUState, fpr[1]) },
1095 { "f2", offsetof(CPUState, fpr[2]) },
1096 { "f3", offsetof(CPUState, fpr[3]) },
1097 { "f4", offsetof(CPUState, fpr[4]) },
1098 { "f5", offsetof(CPUState, fpr[5]) },
1099 { "f6", offsetof(CPUState, fpr[6]) },
1100 { "f7", offsetof(CPUState, fpr[7]) },
1101 { "f8", offsetof(CPUState, fpr[8]) },
1102 { "f9", offsetof(CPUState, fpr[9]) },
1103 { "f10", offsetof(CPUState, fpr[10]) },
1104 { "f11", offsetof(CPUState, fpr[11]) },
1105 { "f12", offsetof(CPUState, fpr[12]) },
1106 { "f13", offsetof(CPUState, fpr[13]) },
1107 { "f14", offsetof(CPUState, fpr[14]) },
1108 { "f15", offsetof(CPUState, fpr[15]) },
1109 { "f16", offsetof(CPUState, fpr[16]) },
1110 { "f17", offsetof(CPUState, fpr[17]) },
1111 { "f18", offsetof(CPUState, fpr[18]) },
1112 { "f19", offsetof(CPUState, fpr[19]) },
1113 { "f20", offsetof(CPUState, fpr[20]) },
1114 { "f21", offsetof(CPUState, fpr[21]) },
1115 { "f22", offsetof(CPUState, fpr[22]) },
1116 { "f23", offsetof(CPUState, fpr[23]) },
1117 { "f24", offsetof(CPUState, fpr[24]) },
1118 { "f25", offsetof(CPUState, fpr[25]) },
1119 { "f26", offsetof(CPUState, fpr[26]) },
1120 { "f27", offsetof(CPUState, fpr[27]) },
1121 { "f28", offsetof(CPUState, fpr[28]) },
1122 { "f29", offsetof(CPUState, fpr[29]) },
1123 { "f30", offsetof(CPUState, fpr[30]) },
1124 { "f31", offsetof(CPUState, fpr[31]) },
9307c4c1
FB
1125#endif
1126 { NULL },
1127};
1128
1129static void expr_error(const char *fmt)
9dc39cba 1130{
9307c4c1
FB
1131 term_printf(fmt);
1132 term_printf("\n");
1133 longjmp(expr_env, 1);
1134}
1135
1136static int get_monitor_def(int *pval, const char *name)
1137{
1138 MonitorDef *md;
1139 for(md = monitor_defs; md->name != NULL; md++) {
1140 if (compare_cmd(name, md->name)) {
1141 if (md->get_value) {
e95c8d51 1142 *pval = md->get_value(md, md->offset);
9307c4c1
FB
1143 } else {
1144 *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
1145 }
1146 return 0;
1147 }
1148 }
1149 return -1;
1150}
1151
1152static void next(void)
1153{
1154 if (pch != '\0') {
1155 pch++;
1156 while (isspace(*pch))
1157 pch++;
1158 }
1159}
1160
1161static int expr_sum(void);
1162
1163static int expr_unary(void)
1164{
1165 int n;
1166 char *p;
1167
1168 switch(*pch) {
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_unary();
1180 break;
1181 case '(':
1182 next();
1183 n = expr_sum();
1184 if (*pch != ')') {
1185 expr_error("')' expected");
1186 }
1187 next();
1188 break;
81d0912d
FB
1189 case '\'':
1190 pch++;
1191 if (*pch == '\0')
1192 expr_error("character constant expected");
1193 n = *pch;
1194 pch++;
1195 if (*pch != '\'')
1196 expr_error("missing terminating \' character");
1197 next();
1198 break;
9307c4c1
FB
1199 case '$':
1200 {
1201 char buf[128], *q;
1202
1203 pch++;
1204 q = buf;
1205 while ((*pch >= 'a' && *pch <= 'z') ||
1206 (*pch >= 'A' && *pch <= 'Z') ||
1207 (*pch >= '0' && *pch <= '9') ||
57206fd4 1208 *pch == '_' || *pch == '.') {
9307c4c1
FB
1209 if ((q - buf) < sizeof(buf) - 1)
1210 *q++ = *pch;
1211 pch++;
1212 }
1213 while (isspace(*pch))
1214 pch++;
1215 *q = 0;
1216 if (get_monitor_def(&n, buf))
1217 expr_error("unknown register");
1218 }
1219 break;
1220 case '\0':
1221 expr_error("unexpected end of expression");
1222 n = 0;
1223 break;
1224 default:
1225 n = strtoul(pch, &p, 0);
1226 if (pch == p) {
1227 expr_error("invalid char in expression");
1228 }
1229 pch = p;
1230 while (isspace(*pch))
1231 pch++;
1232 break;
1233 }
1234 return n;
1235}
1236
1237
1238static int expr_prod(void)
1239{
1240 int val, val2, op;
1241
1242 val = expr_unary();
1243 for(;;) {
1244 op = *pch;
1245 if (op != '*' && op != '/' && op != '%')
1246 break;
1247 next();
1248 val2 = expr_unary();
1249 switch(op) {
1250 default:
1251 case '*':
1252 val *= val2;
1253 break;
1254 case '/':
1255 case '%':
1256 if (val2 == 0)
5b60212f 1257 expr_error("division by zero");
9307c4c1
FB
1258 if (op == '/')
1259 val /= val2;
1260 else
1261 val %= val2;
1262 break;
1263 }
1264 }
1265 return val;
1266}
1267
1268static int expr_logic(void)
1269{
1270 int val, val2, op;
1271
1272 val = expr_prod();
1273 for(;;) {
1274 op = *pch;
1275 if (op != '&' && op != '|' && op != '^')
1276 break;
1277 next();
1278 val2 = expr_prod();
1279 switch(op) {
1280 default:
1281 case '&':
1282 val &= val2;
1283 break;
1284 case '|':
1285 val |= val2;
1286 break;
1287 case '^':
1288 val ^= val2;
1289 break;
1290 }
1291 }
1292 return val;
1293}
1294
1295static int expr_sum(void)
1296{
1297 int val, val2, op;
1298
1299 val = expr_logic();
1300 for(;;) {
1301 op = *pch;
1302 if (op != '+' && op != '-')
1303 break;
1304 next();
1305 val2 = expr_logic();
1306 if (op == '+')
1307 val += val2;
1308 else
1309 val -= val2;
1310 }
1311 return val;
1312}
1313
1314static int get_expr(int *pval, const char **pp)
1315{
1316 pch = *pp;
1317 if (setjmp(expr_env)) {
1318 *pp = pch;
1319 return -1;
1320 }
1321 while (isspace(*pch))
1322 pch++;
1323 *pval = expr_sum();
1324 *pp = pch;
1325 return 0;
1326}
1327
1328static int get_str(char *buf, int buf_size, const char **pp)
1329{
1330 const char *p;
1331 char *q;
1332 int c;
1333
81d0912d 1334 q = buf;
9307c4c1
FB
1335 p = *pp;
1336 while (isspace(*p))
1337 p++;
1338 if (*p == '\0') {
1339 fail:
81d0912d 1340 *q = '\0';
9307c4c1
FB
1341 *pp = p;
1342 return -1;
1343 }
9307c4c1
FB
1344 if (*p == '\"') {
1345 p++;
1346 while (*p != '\0' && *p != '\"') {
1347 if (*p == '\\') {
1348 p++;
1349 c = *p++;
1350 switch(c) {
1351 case 'n':
1352 c = '\n';
1353 break;
1354 case 'r':
1355 c = '\r';
1356 break;
1357 case '\\':
1358 case '\'':
1359 case '\"':
1360 break;
1361 default:
1362 qemu_printf("unsupported escape code: '\\%c'\n", c);
1363 goto fail;
1364 }
1365 if ((q - buf) < buf_size - 1) {
1366 *q++ = c;
1367 }
1368 } else {
1369 if ((q - buf) < buf_size - 1) {
1370 *q++ = *p;
1371 }
1372 p++;
1373 }
1374 }
1375 if (*p != '\"') {
5b60212f 1376 qemu_printf("unterminated string\n");
9307c4c1
FB
1377 goto fail;
1378 }
1379 p++;
1380 } else {
1381 while (*p != '\0' && !isspace(*p)) {
1382 if ((q - buf) < buf_size - 1) {
1383 *q++ = *p;
1384 }
1385 p++;
1386 }
9307c4c1 1387 }
81d0912d 1388 *q = '\0';
9307c4c1
FB
1389 *pp = p;
1390 return 0;
1391}
1392
1393static int default_fmt_format = 'x';
1394static int default_fmt_size = 4;
1395
1396#define MAX_ARGS 16
1397
7e2515e8 1398static void monitor_handle_command(const char *cmdline)
9307c4c1
FB
1399{
1400 const char *p, *pstart, *typestr;
1401 char *q;
1402 int c, nb_args, len, i, has_arg;
9dc39cba 1403 term_cmd_t *cmd;
9307c4c1
FB
1404 char cmdname[256];
1405 char buf[1024];
1406 void *str_allocated[MAX_ARGS];
1407 void *args[MAX_ARGS];
9dc39cba
FB
1408
1409#ifdef DEBUG
1410 term_printf("command='%s'\n", cmdline);
1411#endif
1412
9307c4c1 1413 /* extract the command name */
9dc39cba 1414 p = cmdline;
9307c4c1
FB
1415 q = cmdname;
1416 while (isspace(*p))
1417 p++;
1418 if (*p == '\0')
1419 return;
1420 pstart = p;
1421 while (*p != '\0' && *p != '/' && !isspace(*p))
1422 p++;
1423 len = p - pstart;
1424 if (len > sizeof(cmdname) - 1)
1425 len = sizeof(cmdname) - 1;
1426 memcpy(cmdname, pstart, len);
1427 cmdname[len] = '\0';
1428
1429 /* find the command */
1430 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1431 if (compare_cmd(cmdname, cmd->name))
1432 goto found;
1433 }
1434 term_printf("unknown command: '%s'\n", cmdname);
1435 return;
1436 found:
1437
1438 for(i = 0; i < MAX_ARGS; i++)
1439 str_allocated[i] = NULL;
1440
1441 /* parse the parameters */
1442 typestr = cmd->args_type;
1443 nb_args = 0;
9dc39cba 1444 for(;;) {
9307c4c1
FB
1445 c = *typestr;
1446 if (c == '\0')
9dc39cba 1447 break;
9307c4c1
FB
1448 typestr++;
1449 switch(c) {
1450 case 'F':
81d0912d 1451 case 'B':
9307c4c1
FB
1452 case 's':
1453 {
1454 int ret;
1455 char *str;
1456
1457 while (isspace(*p))
1458 p++;
1459 if (*typestr == '?') {
1460 typestr++;
1461 if (*p == '\0') {
1462 /* no optional string: NULL argument */
1463 str = NULL;
1464 goto add_str;
1465 }
1466 }
1467 ret = get_str(buf, sizeof(buf), &p);
1468 if (ret < 0) {
81d0912d
FB
1469 switch(c) {
1470 case 'F':
9307c4c1 1471 term_printf("%s: filename expected\n", cmdname);
81d0912d
FB
1472 break;
1473 case 'B':
1474 term_printf("%s: block device name expected\n", cmdname);
1475 break;
1476 default:
9307c4c1 1477 term_printf("%s: string expected\n", cmdname);
81d0912d
FB
1478 break;
1479 }
9307c4c1
FB
1480 goto fail;
1481 }
1482 str = qemu_malloc(strlen(buf) + 1);
1483 strcpy(str, buf);
1484 str_allocated[nb_args] = str;
1485 add_str:
1486 if (nb_args >= MAX_ARGS) {
1487 error_args:
1488 term_printf("%s: too many arguments\n", cmdname);
1489 goto fail;
1490 }
1491 args[nb_args++] = str;
1492 }
9dc39cba 1493 break;
9307c4c1
FB
1494 case '/':
1495 {
1496 int count, format, size;
1497
1498 while (isspace(*p))
1499 p++;
1500 if (*p == '/') {
1501 /* format found */
1502 p++;
1503 count = 1;
1504 if (isdigit(*p)) {
1505 count = 0;
1506 while (isdigit(*p)) {
1507 count = count * 10 + (*p - '0');
1508 p++;
1509 }
1510 }
1511 size = -1;
1512 format = -1;
1513 for(;;) {
1514 switch(*p) {
1515 case 'o':
1516 case 'd':
1517 case 'u':
1518 case 'x':
1519 case 'i':
1520 case 'c':
1521 format = *p++;
1522 break;
1523 case 'b':
1524 size = 1;
1525 p++;
1526 break;
1527 case 'h':
1528 size = 2;
1529 p++;
1530 break;
1531 case 'w':
1532 size = 4;
1533 p++;
1534 break;
1535 case 'g':
1536 case 'L':
1537 size = 8;
1538 p++;
1539 break;
1540 default:
1541 goto next;
1542 }
1543 }
1544 next:
1545 if (*p != '\0' && !isspace(*p)) {
1546 term_printf("invalid char in format: '%c'\n", *p);
1547 goto fail;
1548 }
9307c4c1
FB
1549 if (format < 0)
1550 format = default_fmt_format;
4c27ba27
FB
1551 if (format != 'i') {
1552 /* for 'i', not specifying a size gives -1 as size */
1553 if (size < 0)
1554 size = default_fmt_size;
1555 }
9307c4c1
FB
1556 default_fmt_size = size;
1557 default_fmt_format = format;
1558 } else {
1559 count = 1;
1560 format = default_fmt_format;
4c27ba27
FB
1561 if (format != 'i') {
1562 size = default_fmt_size;
1563 } else {
1564 size = -1;
1565 }
9307c4c1
FB
1566 }
1567 if (nb_args + 3 > MAX_ARGS)
1568 goto error_args;
1569 args[nb_args++] = (void*)count;
1570 args[nb_args++] = (void*)format;
1571 args[nb_args++] = (void*)size;
1572 }
9dc39cba 1573 break;
9307c4c1
FB
1574 case 'i':
1575 {
1576 int val;
1577 while (isspace(*p))
1578 p++;
3440557b 1579 if (*typestr == '?' || *typestr == '.') {
9307c4c1 1580 typestr++;
3440557b
FB
1581 if (*typestr == '?') {
1582 if (*p == '\0')
1583 has_arg = 0;
1584 else
1585 has_arg = 1;
1586 } else {
1587 if (*p == '.') {
1588 p++;
1589 while (isspace(*p))
1590 p++;
1591 has_arg = 1;
1592 } else {
1593 has_arg = 0;
1594 }
1595 }
9307c4c1
FB
1596 if (nb_args >= MAX_ARGS)
1597 goto error_args;
1598 args[nb_args++] = (void *)has_arg;
1599 if (!has_arg) {
1600 if (nb_args >= MAX_ARGS)
1601 goto error_args;
1602 val = -1;
1603 goto add_num;
1604 }
1605 }
1606 if (get_expr(&val, &p))
1607 goto fail;
1608 add_num:
1609 if (nb_args >= MAX_ARGS)
1610 goto error_args;
1611 args[nb_args++] = (void *)val;
1612 }
1613 break;
1614 case '-':
1615 {
1616 int has_option;
1617 /* option */
1618
1619 c = *typestr++;
1620 if (c == '\0')
1621 goto bad_type;
1622 while (isspace(*p))
1623 p++;
1624 has_option = 0;
1625 if (*p == '-') {
1626 p++;
1627 if (*p != c) {
1628 term_printf("%s: unsupported option -%c\n",
1629 cmdname, *p);
1630 goto fail;
1631 }
1632 p++;
1633 has_option = 1;
1634 }
1635 if (nb_args >= MAX_ARGS)
1636 goto error_args;
1637 args[nb_args++] = (void *)has_option;
1638 }
1639 break;
1640 default:
1641 bad_type:
1642 term_printf("%s: unknown type '%c'\n", cmdname, c);
1643 goto fail;
1644 }
9dc39cba 1645 }
9307c4c1
FB
1646 /* check that all arguments were parsed */
1647 while (isspace(*p))
1648 p++;
1649 if (*p != '\0') {
1650 term_printf("%s: extraneous characters at the end of line\n",
1651 cmdname);
1652 goto fail;
9dc39cba 1653 }
9307c4c1
FB
1654
1655 switch(nb_args) {
1656 case 0:
1657 cmd->handler();
1658 break;
1659 case 1:
1660 cmd->handler(args[0]);
1661 break;
1662 case 2:
1663 cmd->handler(args[0], args[1]);
1664 break;
1665 case 3:
1666 cmd->handler(args[0], args[1], args[2]);
1667 break;
1668 case 4:
1669 cmd->handler(args[0], args[1], args[2], args[3]);
1670 break;
1671 case 5:
1672 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1673 break;
3440557b
FB
1674 case 6:
1675 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1676 break;
9307c4c1
FB
1677 default:
1678 term_printf("unsupported number of arguments: %d\n", nb_args);
1679 goto fail;
9dc39cba 1680 }
9307c4c1
FB
1681 fail:
1682 for(i = 0; i < MAX_ARGS; i++)
1683 qemu_free(str_allocated[i]);
9dc39cba 1684 return;
9dc39cba
FB
1685}
1686
81d0912d
FB
1687static void cmd_completion(const char *name, const char *list)
1688{
1689 const char *p, *pstart;
1690 char cmd[128];
1691 int len;
1692
1693 p = list;
1694 for(;;) {
1695 pstart = p;
1696 p = strchr(p, '|');
1697 if (!p)
1698 p = pstart + strlen(pstart);
1699 len = p - pstart;
1700 if (len > sizeof(cmd) - 2)
1701 len = sizeof(cmd) - 2;
1702 memcpy(cmd, pstart, len);
1703 cmd[len] = '\0';
1704 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1705 add_completion(cmd);
1706 }
1707 if (*p == '\0')
1708 break;
1709 p++;
1710 }
1711}
1712
1713static void file_completion(const char *input)
1714{
1715 DIR *ffs;
1716 struct dirent *d;
1717 char path[1024];
1718 char file[1024], file_prefix[1024];
1719 int input_path_len;
1720 const char *p;
1721
1722 p = strrchr(input, '/');
1723 if (!p) {
1724 input_path_len = 0;
1725 pstrcpy(file_prefix, sizeof(file_prefix), input);
1726 strcpy(path, ".");
1727 } else {
1728 input_path_len = p - input + 1;
1729 memcpy(path, input, input_path_len);
1730 if (input_path_len > sizeof(path) - 1)
1731 input_path_len = sizeof(path) - 1;
1732 path[input_path_len] = '\0';
1733 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1734 }
1735#ifdef DEBUG_COMPLETION
1736 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1737#endif
1738 ffs = opendir(path);
1739 if (!ffs)
1740 return;
1741 for(;;) {
1742 struct stat sb;
1743 d = readdir(ffs);
1744 if (!d)
1745 break;
1746 if (strstart(d->d_name, file_prefix, NULL)) {
1747 memcpy(file, input, input_path_len);
1748 strcpy(file + input_path_len, d->d_name);
1749 /* stat the file to find out if it's a directory.
1750 * In that case add a slash to speed up typing long paths
1751 */
1752 stat(file, &sb);
1753 if(S_ISDIR(sb.st_mode))
1754 strcat(file, "/");
1755 add_completion(file);
1756 }
1757 }
1758 closedir(ffs);
1759}
1760
1761static void block_completion_it(void *opaque, const char *name)
1762{
1763 const char *input = opaque;
1764
1765 if (input[0] == '\0' ||
1766 !strncmp(name, (char *)input, strlen(input))) {
1767 add_completion(name);
1768 }
1769}
1770
1771/* NOTE: this parser is an approximate form of the real command parser */
1772static void parse_cmdline(const char *cmdline,
1773 int *pnb_args, char **args)
1774{
1775 const char *p;
1776 int nb_args, ret;
1777 char buf[1024];
1778
1779 p = cmdline;
1780 nb_args = 0;
1781 for(;;) {
1782 while (isspace(*p))
1783 p++;
1784 if (*p == '\0')
1785 break;
1786 if (nb_args >= MAX_ARGS)
1787 break;
1788 ret = get_str(buf, sizeof(buf), &p);
1789 args[nb_args] = qemu_strdup(buf);
1790 nb_args++;
1791 if (ret < 0)
1792 break;
1793 }
1794 *pnb_args = nb_args;
1795}
1796
7e2515e8 1797void readline_find_completion(const char *cmdline)
81d0912d
FB
1798{
1799 const char *cmdname;
1800 char *args[MAX_ARGS];
1801 int nb_args, i, len;
1802 const char *ptype, *str;
1803 term_cmd_t *cmd;
1804
1805 parse_cmdline(cmdline, &nb_args, args);
1806#ifdef DEBUG_COMPLETION
1807 for(i = 0; i < nb_args; i++) {
1808 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1809 }
1810#endif
1811
1812 /* if the line ends with a space, it means we want to complete the
1813 next arg */
1814 len = strlen(cmdline);
1815 if (len > 0 && isspace(cmdline[len - 1])) {
1816 if (nb_args >= MAX_ARGS)
1817 return;
1818 args[nb_args++] = qemu_strdup("");
1819 }
1820 if (nb_args <= 1) {
1821 /* command completion */
1822 if (nb_args == 0)
1823 cmdname = "";
1824 else
1825 cmdname = args[0];
1826 completion_index = strlen(cmdname);
1827 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1828 cmd_completion(cmdname, cmd->name);
1829 }
1830 } else {
1831 /* find the command */
1832 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1833 if (compare_cmd(args[0], cmd->name))
1834 goto found;
1835 }
1836 return;
1837 found:
1838 ptype = cmd->args_type;
1839 for(i = 0; i < nb_args - 2; i++) {
1840 if (*ptype != '\0') {
1841 ptype++;
1842 while (*ptype == '?')
1843 ptype++;
1844 }
1845 }
1846 str = args[nb_args - 1];
1847 switch(*ptype) {
1848 case 'F':
1849 /* file completion */
1850 completion_index = strlen(str);
1851 file_completion(str);
1852 break;
1853 case 'B':
1854 /* block device name completion */
1855 completion_index = strlen(str);
1856 bdrv_iterate(block_completion_it, (void *)str);
1857 break;
1858 default:
1859 break;
1860 }
1861 }
1862 for(i = 0; i < nb_args; i++)
1863 qemu_free(args[i]);
1864}
1865
7e2515e8 1866static int term_can_read(void *opaque)
9dc39cba 1867{
7e2515e8 1868 return 128;
9dc39cba
FB
1869}
1870
7e2515e8 1871static void term_read(void *opaque, const uint8_t *buf, int size)
9dc39cba 1872{
7e2515e8
FB
1873 int i;
1874 for(i = 0; i < size; i++)
1875 readline_handle_byte(buf[i]);
9dc39cba
FB
1876}
1877
7e2515e8 1878static void monitor_start_input(void);
9dc39cba 1879
7e2515e8 1880static void monitor_handle_command1(void *opaque, const char *cmdline)
aa455485 1881{
7e2515e8
FB
1882 monitor_handle_command(cmdline);
1883 monitor_start_input();
aa455485
FB
1884}
1885
7e2515e8 1886static void monitor_start_input(void)
aa455485 1887{
7e2515e8 1888 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
aa455485
FB
1889}
1890
7e2515e8 1891void monitor_init(CharDriverState *hd, int show_banner)
aa455485 1892{
7e2515e8
FB
1893 monitor_hd = hd;
1894 if (show_banner) {
1895 term_printf("QEMU %s monitor - type 'help' for more information\n",
1896 QEMU_VERSION);
aa455485 1897 }
7e2515e8
FB
1898 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
1899 monitor_start_input();
aa455485
FB
1900}
1901
7e2515e8
FB
1902/* XXX: use threads ? */
1903/* modal monitor readline */
1904static int monitor_readline_started;
1905static char *monitor_readline_buf;
1906static int monitor_readline_buf_size;
81d0912d 1907
7e2515e8 1908static void monitor_readline_cb(void *opaque, const char *input)
81d0912d 1909{
7e2515e8
FB
1910 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
1911 monitor_readline_started = 0;
81d0912d
FB
1912}
1913
7e2515e8
FB
1914void monitor_readline(const char *prompt, int is_password,
1915 char *buf, int buf_size)
9dc39cba 1916{
7e2515e8
FB
1917 if (is_password) {
1918 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
9dc39cba 1919 }
7e2515e8
FB
1920 readline_start(prompt, is_password, monitor_readline_cb, NULL);
1921 monitor_readline_buf = buf;
1922 monitor_readline_buf_size = buf_size;
1923 monitor_readline_started = 1;
1924 while (monitor_readline_started) {
1925 main_loop_wait(10);
9dc39cba 1926 }
9dc39cba 1927}