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