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