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