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