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