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