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