]> git.proxmox.com Git - qemu.git/blame - monitor.c
ACPI suspend type field is 3 bits long
[qemu.git] / monitor.c
CommitLineData
9dc39cba
FB
1/*
2 * QEMU monitor
5fafdf24 3 *
9dc39cba 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
9dc39cba
FB
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 */
87ecb68b
PB
24#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
33#include "console.h"
34#include "block.h"
35#include "audio/audio.h"
9307c4c1 36#include "disas.h"
81d0912d 37#include <dirent.h>
c8256f9d 38#include "qemu-timer.h"
6a5bd307 39
9dc39cba 40//#define DEBUG
81d0912d 41//#define DEBUG_COMPLETION
9dc39cba 42
9307c4c1
FB
43#ifndef offsetof
44#define offsetof(type, field) ((size_t) &((type *)0)->field)
45#endif
46
9307c4c1
FB
47/*
48 * Supported types:
5fafdf24 49 *
9307c4c1 50 * 'F' filename
81d0912d 51 * 'B' block device name
9307c4c1 52 * 's' string (accept optional quote)
92a31b1f
FB
53 * 'i' 32 bit integer
54 * 'l' target long (32 or 64 bit)
9307c4c1
FB
55 * '/' optional gdb-like print format (like "/10x")
56 *
57 * '?' optional type (for 'F', 's' and 'i')
58 *
59 */
60
9dc39cba
FB
61typedef struct term_cmd_t {
62 const char *name;
9307c4c1 63 const char *args_type;
a5f1b965 64 void *handler;
9dc39cba
FB
65 const char *params;
66 const char *help;
67} term_cmd_t;
68
20d8a3ed
TS
69#define MAX_MON 4
70static CharDriverState *monitor_hd[MAX_MON];
86e94dea 71static int hide_banner;
7e2515e8 72
9dc39cba
FB
73static term_cmd_t term_cmds[];
74static term_cmd_t info_cmds[];
75
60fe76f3 76static uint8_t term_outbuf[1024];
7e2515e8 77static int term_outbuf_index;
81d0912d 78
6a00d601
FB
79CPUState *mon_cpu = NULL;
80
7e2515e8
FB
81void term_flush(void)
82{
20d8a3ed 83 int i;
7e2515e8 84 if (term_outbuf_index > 0) {
20d8a3ed
TS
85 for (i = 0; i < MAX_MON; i++)
86 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
87 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
7e2515e8
FB
88 term_outbuf_index = 0;
89 }
90}
91
92/* flush at every end of line or if the buffer is full */
93void term_puts(const char *str)
94{
60fe76f3 95 char c;
7e2515e8
FB
96 for(;;) {
97 c = *str++;
98 if (c == '\0')
99 break;
7ba1260a
FB
100 if (c == '\n')
101 term_outbuf[term_outbuf_index++] = '\r';
7e2515e8 102 term_outbuf[term_outbuf_index++] = c;
7ba1260a 103 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
7e2515e8
FB
104 c == '\n')
105 term_flush();
106 }
107}
108
109void term_vprintf(const char *fmt, va_list ap)
9dc39cba 110{
81d0912d 111 char buf[4096];
81d0912d 112 vsnprintf(buf, sizeof(buf), fmt, ap);
7e2515e8 113 term_puts(buf);
9dc39cba
FB
114}
115
7e2515e8 116void term_printf(const char *fmt, ...)
9dc39cba 117{
7e2515e8
FB
118 va_list ap;
119 va_start(ap, fmt);
120 term_vprintf(fmt, ap);
121 va_end(ap);
9dc39cba
FB
122}
123
fef30743
TS
124void term_print_filename(const char *filename)
125{
126 int i;
127
128 for (i = 0; filename[i]; i++) {
129 switch (filename[i]) {
130 case ' ':
131 case '"':
132 case '\\':
133 term_printf("\\%c", filename[i]);
134 break;
135 case '\t':
136 term_printf("\\t");
137 break;
138 case '\r':
139 term_printf("\\r");
140 break;
141 case '\n':
142 term_printf("\\n");
143 break;
144 default:
145 term_printf("%c", filename[i]);
146 break;
147 }
148 }
149}
150
7fe48483
FB
151static int monitor_fprintf(FILE *stream, const char *fmt, ...)
152{
153 va_list ap;
154 va_start(ap, fmt);
155 term_vprintf(fmt, ap);
156 va_end(ap);
157 return 0;
158}
159
9dc39cba
FB
160static int compare_cmd(const char *name, const char *list)
161{
162 const char *p, *pstart;
163 int len;
164 len = strlen(name);
165 p = list;
166 for(;;) {
167 pstart = p;
168 p = strchr(p, '|');
169 if (!p)
170 p = pstart + strlen(pstart);
171 if ((p - pstart) == len && !memcmp(pstart, name, len))
172 return 1;
173 if (*p == '\0')
174 break;
175 p++;
176 }
177 return 0;
178}
179
180static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
181{
182 term_cmd_t *cmd;
183
184 for(cmd = cmds; cmd->name != NULL; cmd++) {
185 if (!name || !strcmp(name, cmd->name))
186 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
187 }
188}
189
190static void help_cmd(const char *name)
191{
192 if (name && !strcmp(name, "info")) {
193 help_cmd1(info_cmds, "info ", NULL);
194 } else {
195 help_cmd1(term_cmds, "", name);
f193c797
FB
196 if (name && !strcmp(name, "log")) {
197 CPULogItem *item;
198 term_printf("Log items (comma separated):\n");
199 term_printf("%-10s %s\n", "none", "remove all logs");
200 for(item = cpu_log_items; item->mask != 0; item++) {
201 term_printf("%-10s %s\n", item->name, item->help);
202 }
203 }
9dc39cba
FB
204 }
205}
206
9307c4c1 207static void do_help(const char *name)
9dc39cba 208{
9307c4c1 209 help_cmd(name);
9dc39cba
FB
210}
211
7954c734 212static void do_commit(const char *device)
9dc39cba 213{
7954c734 214 int i, all_devices;
2dc7b602 215
7954c734 216 all_devices = !strcmp(device, "all");
e4bcb14c 217 for (i = 0; i < nb_drives; i++) {
5fafdf24 218 if (all_devices ||
e4bcb14c
TS
219 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
220 bdrv_commit(drives_table[i].bdrv);
9dc39cba
FB
221 }
222}
223
9307c4c1 224static void do_info(const char *item)
9dc39cba
FB
225{
226 term_cmd_t *cmd;
a5f1b965 227 void (*handler)(void);
9dc39cba 228
9307c4c1 229 if (!item)
9dc39cba 230 goto help;
9dc39cba 231 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
5fafdf24 232 if (compare_cmd(item, cmd->name))
9dc39cba
FB
233 goto found;
234 }
235 help:
9307c4c1 236 help_cmd("info");
9dc39cba
FB
237 return;
238 found:
a5f1b965
BS
239 handler = cmd->handler;
240 handler();
9dc39cba
FB
241}
242
9bc9d1c7
FB
243static void do_info_version(void)
244{
245 term_printf("%s\n", QEMU_VERSION);
246}
247
c35734b2
TS
248static void do_info_name(void)
249{
250 if (qemu_name)
251 term_printf("%s\n", qemu_name);
252}
253
9307c4c1 254static void do_info_block(void)
9dc39cba
FB
255{
256 bdrv_info();
257}
258
a36e69dd
TS
259static void do_info_blockstats(void)
260{
261 bdrv_info_stats();
262}
263
6a00d601 264/* get the current CPU defined by the user */
9596ebb7 265static int mon_set_cpu(int cpu_index)
6a00d601
FB
266{
267 CPUState *env;
268
269 for(env = first_cpu; env != NULL; env = env->next_cpu) {
270 if (env->cpu_index == cpu_index) {
271 mon_cpu = env;
272 return 0;
273 }
274 }
275 return -1;
276}
277
9596ebb7 278static CPUState *mon_get_cpu(void)
6a00d601
FB
279{
280 if (!mon_cpu) {
281 mon_set_cpu(0);
282 }
283 return mon_cpu;
284}
285
9307c4c1
FB
286static void do_info_registers(void)
287{
6a00d601
FB
288 CPUState *env;
289 env = mon_get_cpu();
290 if (!env)
291 return;
9307c4c1 292#ifdef TARGET_I386
6a00d601 293 cpu_dump_state(env, NULL, monitor_fprintf,
d24b15a8 294 X86_DUMP_FPU);
9307c4c1 295#else
5fafdf24 296 cpu_dump_state(env, NULL, monitor_fprintf,
7fe48483 297 0);
9307c4c1
FB
298#endif
299}
300
6a00d601
FB
301static void do_info_cpus(void)
302{
303 CPUState *env;
304
305 /* just to set the default cpu if not already done */
306 mon_get_cpu();
307
308 for(env = first_cpu; env != NULL; env = env->next_cpu) {
5fafdf24 309 term_printf("%c CPU #%d:",
6a00d601
FB
310 (env == mon_cpu) ? '*' : ' ',
311 env->cpu_index);
312#if defined(TARGET_I386)
313 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
e80e1cc4
FB
314#elif defined(TARGET_PPC)
315 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
ba3c64fb
FB
316#elif defined(TARGET_SPARC)
317 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
ead9360e 318#elif defined(TARGET_MIPS)
b5dc7732 319 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
ce5232c5 320#endif
ead9360e
TS
321 if (env->halted)
322 term_printf(" (halted)");
6a00d601
FB
323 term_printf("\n");
324 }
325}
326
327static void do_cpu_set(int index)
328{
329 if (mon_set_cpu(index) < 0)
330 term_printf("Invalid CPU index\n");
331}
332
e3db7226
FB
333static void do_info_jit(void)
334{
335 dump_exec_info(NULL, monitor_fprintf);
336}
337
aa455485
FB
338static void do_info_history (void)
339{
340 int i;
7e2515e8 341 const char *str;
3b46e624 342
7e2515e8
FB
343 i = 0;
344 for(;;) {
345 str = readline_get_history(i);
346 if (!str)
347 break;
348 term_printf("%d: '%s'\n", i, str);
8e3a9fd2 349 i++;
aa455485
FB
350 }
351}
352
76a66253
JM
353#if defined(TARGET_PPC)
354/* XXX: not implemented in other targets */
355static void do_info_cpu_stats (void)
356{
357 CPUState *env;
358
359 env = mon_get_cpu();
360 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
361}
362#endif
363
9307c4c1 364static void do_quit(void)
9dc39cba
FB
365{
366 exit(0);
367}
368
369static int eject_device(BlockDriverState *bs, int force)
370{
371 if (bdrv_is_inserted(bs)) {
372 if (!force) {
373 if (!bdrv_is_removable(bs)) {
374 term_printf("device is not removable\n");
375 return -1;
376 }
377 if (bdrv_is_locked(bs)) {
378 term_printf("device is locked\n");
379 return -1;
380 }
381 }
382 bdrv_close(bs);
383 }
384 return 0;
385}
386
9307c4c1 387static void do_eject(int force, const char *filename)
9dc39cba
FB
388{
389 BlockDriverState *bs;
9dc39cba 390
9307c4c1 391 bs = bdrv_find(filename);
9dc39cba
FB
392 if (!bs) {
393 term_printf("device not found\n");
394 return;
395 }
396 eject_device(bs, force);
397}
398
2ecea9b8 399static void do_change_block(const char *device, const char *filename, const char *fmt)
9dc39cba
FB
400{
401 BlockDriverState *bs;
2ecea9b8 402 BlockDriver *drv = NULL;
9dc39cba 403
9307c4c1 404 bs = bdrv_find(device);
9dc39cba
FB
405 if (!bs) {
406 term_printf("device not found\n");
407 return;
408 }
2ecea9b8
AJ
409 if (fmt) {
410 drv = bdrv_find_format(fmt);
411 if (!drv) {
412 term_printf("invalid format %s\n", fmt);
413 return;
414 }
415 }
9dc39cba
FB
416 if (eject_device(bs, 0) < 0)
417 return;
2ecea9b8 418 bdrv_open2(bs, filename, 0, drv);
2bac6019 419 qemu_key_check(bs, filename);
9dc39cba
FB
420}
421
e25a5822
TS
422static void do_change_vnc(const char *target)
423{
70848515
TS
424 if (strcmp(target, "passwd") == 0 ||
425 strcmp(target, "password") == 0) {
426 char password[9];
427 monitor_readline("Password: ", 1, password, sizeof(password)-1);
428 password[sizeof(password)-1] = '\0';
429 if (vnc_display_password(NULL, password) < 0)
430 term_printf("could not set VNC server password\n");
431 } else {
432 if (vnc_display_open(NULL, target) < 0)
433 term_printf("could not start VNC server on %s\n", target);
434 }
e25a5822
TS
435}
436
2ecea9b8 437static void do_change(const char *device, const char *target, const char *fmt)
e25a5822
TS
438{
439 if (strcmp(device, "vnc") == 0) {
440 do_change_vnc(target);
441 } else {
2ecea9b8 442 do_change_block(device, target, fmt);
e25a5822
TS
443 }
444}
445
9307c4c1 446static void do_screen_dump(const char *filename)
59a983b9 447{
95219897 448 vga_hw_screen_dump(filename);
59a983b9
FB
449}
450
e735b91c
PB
451static void do_logfile(const char *filename)
452{
453 cpu_set_log_filename(filename);
454}
455
9307c4c1 456static void do_log(const char *items)
f193c797
FB
457{
458 int mask;
3b46e624 459
9307c4c1 460 if (!strcmp(items, "none")) {
f193c797
FB
461 mask = 0;
462 } else {
9307c4c1 463 mask = cpu_str_to_log_mask(items);
f193c797 464 if (!mask) {
9307c4c1 465 help_cmd("log");
f193c797
FB
466 return;
467 }
468 }
469 cpu_set_log(mask);
470}
471
9307c4c1 472static void do_stop(void)
8a7ddc38
FB
473{
474 vm_stop(EXCP_INTERRUPT);
475}
476
9307c4c1 477static void do_cont(void)
8a7ddc38
FB
478{
479 vm_start();
480}
481
67b915a5 482#ifdef CONFIG_GDBSTUB
cfc3475a 483static void do_gdbserver(const char *port)
8a7ddc38 484{
cfc3475a 485 if (!port)
9307c4c1 486 port = DEFAULT_GDBSTUB_PORT;
cfc3475a
PB
487 if (gdbserver_start(port) < 0) {
488 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
8a7ddc38 489 } else {
cfc3475a 490 qemu_printf("Waiting gdb connection on port '%s'\n", port);
8a7ddc38
FB
491 }
492}
67b915a5 493#endif
8a7ddc38 494
9307c4c1
FB
495static void term_printc(int c)
496{
497 term_printf("'");
498 switch(c) {
499 case '\'':
500 term_printf("\\'");
501 break;
502 case '\\':
503 term_printf("\\\\");
504 break;
505 case '\n':
506 term_printf("\\n");
507 break;
508 case '\r':
509 term_printf("\\r");
510 break;
511 default:
512 if (c >= 32 && c <= 126) {
513 term_printf("%c", c);
514 } else {
515 term_printf("\\x%02x", c);
516 }
517 break;
518 }
519 term_printf("'");
520}
521
5fafdf24 522static void memory_dump(int count, int format, int wsize,
7743e588 523 target_phys_addr_t addr, int is_physical)
9307c4c1 524{
6a00d601 525 CPUState *env;
9307c4c1
FB
526 int nb_per_line, l, line_size, i, max_digits, len;
527 uint8_t buf[16];
528 uint64_t v;
529
530 if (format == 'i') {
531 int flags;
532 flags = 0;
6a00d601
FB
533 env = mon_get_cpu();
534 if (!env && !is_physical)
535 return;
9307c4c1 536#ifdef TARGET_I386
4c27ba27 537 if (wsize == 2) {
9307c4c1 538 flags = 1;
4c27ba27
FB
539 } else if (wsize == 4) {
540 flags = 0;
541 } else {
6a15fd12 542 /* as default we use the current CS size */
4c27ba27 543 flags = 0;
6a15fd12
FB
544 if (env) {
545#ifdef TARGET_X86_64
5fafdf24 546 if ((env->efer & MSR_EFER_LMA) &&
6a15fd12
FB
547 (env->segs[R_CS].flags & DESC_L_MASK))
548 flags = 2;
549 else
550#endif
551 if (!(env->segs[R_CS].flags & DESC_B_MASK))
552 flags = 1;
553 }
4c27ba27
FB
554 }
555#endif
6a00d601 556 monitor_disas(env, addr, count, is_physical, flags);
9307c4c1
FB
557 return;
558 }
559
560 len = wsize * count;
561 if (wsize == 1)
562 line_size = 8;
563 else
564 line_size = 16;
565 nb_per_line = line_size / wsize;
566 max_digits = 0;
567
568 switch(format) {
569 case 'o':
570 max_digits = (wsize * 8 + 2) / 3;
571 break;
572 default:
573 case 'x':
574 max_digits = (wsize * 8) / 4;
575 break;
576 case 'u':
577 case 'd':
578 max_digits = (wsize * 8 * 10 + 32) / 33;
579 break;
580 case 'c':
581 wsize = 1;
582 break;
583 }
584
585 while (len > 0) {
7743e588
BS
586 if (is_physical)
587 term_printf(TARGET_FMT_plx ":", addr);
588 else
589 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
9307c4c1
FB
590 l = len;
591 if (l > line_size)
592 l = line_size;
593 if (is_physical) {
594 cpu_physical_memory_rw(addr, buf, l, 0);
595 } else {
6a00d601
FB
596 env = mon_get_cpu();
597 if (!env)
598 break;
c8f79b67
AL
599 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
600 term_printf(" Cannot access memory\n");
601 break;
602 }
9307c4c1 603 }
5fafdf24 604 i = 0;
9307c4c1
FB
605 while (i < l) {
606 switch(wsize) {
607 default:
608 case 1:
609 v = ldub_raw(buf + i);
610 break;
611 case 2:
612 v = lduw_raw(buf + i);
613 break;
614 case 4:
92a31b1f 615 v = (uint32_t)ldl_raw(buf + i);
9307c4c1
FB
616 break;
617 case 8:
618 v = ldq_raw(buf + i);
619 break;
620 }
621 term_printf(" ");
622 switch(format) {
623 case 'o':
26a76461 624 term_printf("%#*" PRIo64, max_digits, v);
9307c4c1
FB
625 break;
626 case 'x':
26a76461 627 term_printf("0x%0*" PRIx64, max_digits, v);
9307c4c1
FB
628 break;
629 case 'u':
26a76461 630 term_printf("%*" PRIu64, max_digits, v);
9307c4c1
FB
631 break;
632 case 'd':
26a76461 633 term_printf("%*" PRId64, max_digits, v);
9307c4c1
FB
634 break;
635 case 'c':
636 term_printc(v);
637 break;
638 }
639 i += wsize;
640 }
641 term_printf("\n");
642 addr += l;
643 len -= l;
644 }
645}
646
92a31b1f
FB
647#if TARGET_LONG_BITS == 64
648#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
649#else
650#define GET_TLONG(h, l) (l)
651#endif
652
5fafdf24 653static void do_memory_dump(int count, int format, int size,
92a31b1f 654 uint32_t addrh, uint32_t addrl)
9307c4c1 655{
92a31b1f 656 target_long addr = GET_TLONG(addrh, addrl);
9307c4c1
FB
657 memory_dump(count, format, size, addr, 0);
658}
659
7743e588
BS
660#if TARGET_PHYS_ADDR_BITS > 32
661#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
662#else
663#define GET_TPHYSADDR(h, l) (l)
664#endif
665
92a31b1f
FB
666static void do_physical_memory_dump(int count, int format, int size,
667 uint32_t addrh, uint32_t addrl)
668
9307c4c1 669{
7743e588 670 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
9307c4c1
FB
671 memory_dump(count, format, size, addr, 1);
672}
673
92a31b1f 674static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
9307c4c1 675{
7743e588
BS
676 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
677#if TARGET_PHYS_ADDR_BITS == 32
9307c4c1
FB
678 switch(format) {
679 case 'o':
680 term_printf("%#o", val);
681 break;
682 case 'x':
683 term_printf("%#x", val);
684 break;
685 case 'u':
686 term_printf("%u", val);
687 break;
688 default:
689 case 'd':
690 term_printf("%d", val);
691 break;
692 case 'c':
693 term_printc(val);
694 break;
695 }
92a31b1f
FB
696#else
697 switch(format) {
698 case 'o':
26a76461 699 term_printf("%#" PRIo64, val);
92a31b1f
FB
700 break;
701 case 'x':
26a76461 702 term_printf("%#" PRIx64, val);
92a31b1f
FB
703 break;
704 case 'u':
26a76461 705 term_printf("%" PRIu64, val);
92a31b1f
FB
706 break;
707 default:
708 case 'd':
26a76461 709 term_printf("%" PRId64, val);
92a31b1f
FB
710 break;
711 case 'c':
712 term_printc(val);
713 break;
714 }
715#endif
9307c4c1
FB
716 term_printf("\n");
717}
718
5fafdf24 719static void do_memory_save(unsigned int valh, unsigned int vall,
b371dc59
FB
720 uint32_t size, const char *filename)
721{
722 FILE *f;
723 target_long addr = GET_TLONG(valh, vall);
724 uint32_t l;
725 CPUState *env;
726 uint8_t buf[1024];
727
728 env = mon_get_cpu();
729 if (!env)
730 return;
731
732 f = fopen(filename, "wb");
733 if (!f) {
734 term_printf("could not open '%s'\n", filename);
735 return;
736 }
737 while (size != 0) {
738 l = sizeof(buf);
739 if (l > size)
740 l = size;
741 cpu_memory_rw_debug(env, addr, buf, l, 0);
742 fwrite(buf, 1, l, f);
743 addr += l;
744 size -= l;
745 }
746 fclose(f);
747}
748
a8bdf7a6
AJ
749static void do_physical_memory_save(unsigned int valh, unsigned int vall,
750 uint32_t size, const char *filename)
751{
752 FILE *f;
753 uint32_t l;
754 uint8_t buf[1024];
339dea27 755 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
a8bdf7a6
AJ
756
757 f = fopen(filename, "wb");
758 if (!f) {
759 term_printf("could not open '%s'\n", filename);
760 return;
761 }
762 while (size != 0) {
763 l = sizeof(buf);
764 if (l > size)
765 l = size;
766 cpu_physical_memory_rw(addr, buf, l, 0);
767 fwrite(buf, 1, l, f);
768 fflush(f);
769 addr += l;
770 size -= l;
771 }
772 fclose(f);
773}
774
e4cf1adc
FB
775static void do_sum(uint32_t start, uint32_t size)
776{
777 uint32_t addr;
778 uint8_t buf[1];
779 uint16_t sum;
780
781 sum = 0;
782 for(addr = start; addr < (start + size); addr++) {
783 cpu_physical_memory_rw(addr, buf, 1, 0);
784 /* BSD sum algorithm ('sum' Unix command) */
785 sum = (sum >> 1) | (sum << 15);
786 sum += buf[0];
787 }
788 term_printf("%05d\n", sum);
789}
790
a3a91a35
FB
791typedef struct {
792 int keycode;
793 const char *name;
794} KeyDef;
795
796static const KeyDef key_defs[] = {
797 { 0x2a, "shift" },
798 { 0x36, "shift_r" },
3b46e624 799
a3a91a35
FB
800 { 0x38, "alt" },
801 { 0xb8, "alt_r" },
2ba27c7f
TS
802 { 0x64, "altgr" },
803 { 0xe4, "altgr_r" },
a3a91a35
FB
804 { 0x1d, "ctrl" },
805 { 0x9d, "ctrl_r" },
806
807 { 0xdd, "menu" },
808
809 { 0x01, "esc" },
810
811 { 0x02, "1" },
812 { 0x03, "2" },
813 { 0x04, "3" },
814 { 0x05, "4" },
815 { 0x06, "5" },
816 { 0x07, "6" },
817 { 0x08, "7" },
818 { 0x09, "8" },
819 { 0x0a, "9" },
820 { 0x0b, "0" },
64866c3d
FB
821 { 0x0c, "minus" },
822 { 0x0d, "equal" },
a3a91a35
FB
823 { 0x0e, "backspace" },
824
825 { 0x0f, "tab" },
826 { 0x10, "q" },
827 { 0x11, "w" },
828 { 0x12, "e" },
829 { 0x13, "r" },
830 { 0x14, "t" },
831 { 0x15, "y" },
832 { 0x16, "u" },
833 { 0x17, "i" },
834 { 0x18, "o" },
835 { 0x19, "p" },
836
837 { 0x1c, "ret" },
838
839 { 0x1e, "a" },
840 { 0x1f, "s" },
841 { 0x20, "d" },
842 { 0x21, "f" },
843 { 0x22, "g" },
844 { 0x23, "h" },
845 { 0x24, "j" },
846 { 0x25, "k" },
847 { 0x26, "l" },
848
849 { 0x2c, "z" },
850 { 0x2d, "x" },
851 { 0x2e, "c" },
852 { 0x2f, "v" },
853 { 0x30, "b" },
854 { 0x31, "n" },
855 { 0x32, "m" },
3b46e624 856
4d3b6f6e
AZ
857 { 0x37, "asterisk" },
858
a3a91a35 859 { 0x39, "spc" },
00ffa62a 860 { 0x3a, "caps_lock" },
a3a91a35
FB
861 { 0x3b, "f1" },
862 { 0x3c, "f2" },
863 { 0x3d, "f3" },
864 { 0x3e, "f4" },
865 { 0x3f, "f5" },
866 { 0x40, "f6" },
867 { 0x41, "f7" },
868 { 0x42, "f8" },
869 { 0x43, "f9" },
870 { 0x44, "f10" },
00ffa62a 871 { 0x45, "num_lock" },
a3a91a35
FB
872 { 0x46, "scroll_lock" },
873
64866c3d
FB
874 { 0xb5, "kp_divide" },
875 { 0x37, "kp_multiply" },
0cfec834 876 { 0x4a, "kp_subtract" },
64866c3d
FB
877 { 0x4e, "kp_add" },
878 { 0x9c, "kp_enter" },
879 { 0x53, "kp_decimal" },
f2289cb6 880 { 0x54, "sysrq" },
64866c3d
FB
881
882 { 0x52, "kp_0" },
883 { 0x4f, "kp_1" },
884 { 0x50, "kp_2" },
885 { 0x51, "kp_3" },
886 { 0x4b, "kp_4" },
887 { 0x4c, "kp_5" },
888 { 0x4d, "kp_6" },
889 { 0x47, "kp_7" },
890 { 0x48, "kp_8" },
891 { 0x49, "kp_9" },
3b46e624 892
a3a91a35
FB
893 { 0x56, "<" },
894
895 { 0x57, "f11" },
896 { 0x58, "f12" },
897
898 { 0xb7, "print" },
899
900 { 0xc7, "home" },
901 { 0xc9, "pgup" },
902 { 0xd1, "pgdn" },
903 { 0xcf, "end" },
904
905 { 0xcb, "left" },
906 { 0xc8, "up" },
907 { 0xd0, "down" },
908 { 0xcd, "right" },
909
910 { 0xd2, "insert" },
911 { 0xd3, "delete" },
c0b5b109
BS
912#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
913 { 0xf0, "stop" },
914 { 0xf1, "again" },
915 { 0xf2, "props" },
916 { 0xf3, "undo" },
917 { 0xf4, "front" },
918 { 0xf5, "copy" },
919 { 0xf6, "open" },
920 { 0xf7, "paste" },
921 { 0xf8, "find" },
922 { 0xf9, "cut" },
923 { 0xfa, "lf" },
924 { 0xfb, "help" },
925 { 0xfc, "meta_l" },
926 { 0xfd, "meta_r" },
927 { 0xfe, "compose" },
928#endif
a3a91a35
FB
929 { 0, NULL },
930};
931
932static int get_keycode(const char *key)
933{
934 const KeyDef *p;
64866c3d
FB
935 char *endp;
936 int ret;
a3a91a35
FB
937
938 for(p = key_defs; p->name != NULL; p++) {
939 if (!strcmp(key, p->name))
940 return p->keycode;
941 }
64866c3d
FB
942 if (strstart(key, "0x", NULL)) {
943 ret = strtoul(key, &endp, 0);
944 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
945 return ret;
946 }
a3a91a35
FB
947 return -1;
948}
949
c8256f9d
AZ
950#define MAX_KEYCODES 16
951static uint8_t keycodes[MAX_KEYCODES];
952static int nb_pending_keycodes;
953static QEMUTimer *key_timer;
954
955static void release_keys(void *opaque)
956{
957 int keycode;
958
959 while (nb_pending_keycodes > 0) {
960 nb_pending_keycodes--;
961 keycode = keycodes[nb_pending_keycodes];
962 if (keycode & 0x80)
963 kbd_put_keycode(0xe0);
964 kbd_put_keycode(keycode | 0x80);
965 }
966}
967
968static void do_sendkey(const char *string, int has_hold_time, int hold_time)
a3a91a35 969{
3401c0d9
AZ
970 char keyname_buf[16];
971 char *separator;
972 int keyname_len, keycode, i;
973
c8256f9d
AZ
974 if (nb_pending_keycodes > 0) {
975 qemu_del_timer(key_timer);
976 release_keys(NULL);
977 }
978 if (!has_hold_time)
979 hold_time = 100;
980 i = 0;
3401c0d9
AZ
981 while (1) {
982 separator = strchr(string, '-');
983 keyname_len = separator ? separator - string : strlen(string);
984 if (keyname_len > 0) {
985 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
986 if (keyname_len > sizeof(keyname_buf) - 1) {
987 term_printf("invalid key: '%s...'\n", keyname_buf);
988 return;
a3a91a35 989 }
c8256f9d 990 if (i == MAX_KEYCODES) {
3401c0d9
AZ
991 term_printf("too many keys\n");
992 return;
993 }
994 keyname_buf[keyname_len] = 0;
995 keycode = get_keycode(keyname_buf);
996 if (keycode < 0) {
997 term_printf("unknown key: '%s'\n", keyname_buf);
998 return;
999 }
c8256f9d 1000 keycodes[i++] = keycode;
a3a91a35 1001 }
3401c0d9 1002 if (!separator)
a3a91a35 1003 break;
3401c0d9 1004 string = separator + 1;
a3a91a35 1005 }
c8256f9d 1006 nb_pending_keycodes = i;
a3a91a35 1007 /* key down events */
c8256f9d 1008 for (i = 0; i < nb_pending_keycodes; i++) {
a3a91a35
FB
1009 keycode = keycodes[i];
1010 if (keycode & 0x80)
1011 kbd_put_keycode(0xe0);
1012 kbd_put_keycode(keycode & 0x7f);
1013 }
c8256f9d 1014 /* delayed key up events */
f227f17d
AZ
1015 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1016 muldiv64(ticks_per_sec, hold_time, 1000));
a3a91a35
FB
1017}
1018
13224a87
FB
1019static int mouse_button_state;
1020
5fafdf24 1021static void do_mouse_move(const char *dx_str, const char *dy_str,
13224a87
FB
1022 const char *dz_str)
1023{
1024 int dx, dy, dz;
1025 dx = strtol(dx_str, NULL, 0);
1026 dy = strtol(dy_str, NULL, 0);
1027 dz = 0;
5fafdf24 1028 if (dz_str)
13224a87
FB
1029 dz = strtol(dz_str, NULL, 0);
1030 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1031}
1032
1033static void do_mouse_button(int button_state)
1034{
1035 mouse_button_state = button_state;
1036 kbd_mouse_event(0, 0, 0, mouse_button_state);
1037}
1038
3440557b
FB
1039static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1040{
1041 uint32_t val;
1042 int suffix;
1043
1044 if (has_index) {
1045 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1046 addr++;
1047 }
1048 addr &= 0xffff;
1049
1050 switch(size) {
1051 default:
1052 case 1:
1053 val = cpu_inb(NULL, addr);
1054 suffix = 'b';
1055 break;
1056 case 2:
1057 val = cpu_inw(NULL, addr);
1058 suffix = 'w';
1059 break;
1060 case 4:
1061 val = cpu_inl(NULL, addr);
1062 suffix = 'l';
1063 break;
1064 }
1065 term_printf("port%c[0x%04x] = %#0*x\n",
1066 suffix, addr, size * 2, val);
1067}
a3a91a35 1068
3b4366de
BS
1069/* boot_set handler */
1070static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1071static void *boot_opaque;
1072
1073void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1074{
1075 qemu_boot_set_handler = func;
1076 boot_opaque = opaque;
1077}
1078
0ecdffbb
AJ
1079static void do_boot_set(const char *bootdevice)
1080{
1081 int res;
1082
1083 if (qemu_boot_set_handler) {
3b4366de 1084 res = qemu_boot_set_handler(boot_opaque, bootdevice);
0ecdffbb
AJ
1085 if (res == 0)
1086 term_printf("boot device list now set to %s\n", bootdevice);
1087 else
1088 term_printf("setting boot device list failed with error %i\n", res);
1089 } else {
1090 term_printf("no function defined to set boot device list for this architecture\n");
1091 }
1092}
1093
e4f9082b
FB
1094static void do_system_reset(void)
1095{
1096 qemu_system_reset_request();
1097}
1098
3475187d
FB
1099static void do_system_powerdown(void)
1100{
1101 qemu_system_powerdown_request();
1102}
1103
b86bda5b
FB
1104#if defined(TARGET_I386)
1105static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1106{
5fafdf24 1107 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
b86bda5b
FB
1108 addr,
1109 pte & mask,
1110 pte & PG_GLOBAL_MASK ? 'G' : '-',
1111 pte & PG_PSE_MASK ? 'P' : '-',
1112 pte & PG_DIRTY_MASK ? 'D' : '-',
1113 pte & PG_ACCESSED_MASK ? 'A' : '-',
1114 pte & PG_PCD_MASK ? 'C' : '-',
1115 pte & PG_PWT_MASK ? 'T' : '-',
1116 pte & PG_USER_MASK ? 'U' : '-',
1117 pte & PG_RW_MASK ? 'W' : '-');
1118}
1119
1120static void tlb_info(void)
1121{
6a00d601 1122 CPUState *env;
b86bda5b
FB
1123 int l1, l2;
1124 uint32_t pgd, pde, pte;
1125
6a00d601
FB
1126 env = mon_get_cpu();
1127 if (!env)
1128 return;
1129
b86bda5b
FB
1130 if (!(env->cr[0] & CR0_PG_MASK)) {
1131 term_printf("PG disabled\n");
1132 return;
1133 }
1134 pgd = env->cr[3] & ~0xfff;
1135 for(l1 = 0; l1 < 1024; l1++) {
1136 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1137 pde = le32_to_cpu(pde);
1138 if (pde & PG_PRESENT_MASK) {
1139 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1140 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1141 } else {
1142 for(l2 = 0; l2 < 1024; l2++) {
5fafdf24 1143 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
b86bda5b
FB
1144 (uint8_t *)&pte, 4);
1145 pte = le32_to_cpu(pte);
1146 if (pte & PG_PRESENT_MASK) {
5fafdf24
TS
1147 print_pte((l1 << 22) + (l2 << 12),
1148 pte & ~PG_PSE_MASK,
b86bda5b
FB
1149 ~0xfff);
1150 }
1151 }
1152 }
1153 }
1154 }
1155}
1156
5fafdf24 1157static void mem_print(uint32_t *pstart, int *plast_prot,
b86bda5b
FB
1158 uint32_t end, int prot)
1159{
9746b15b
FB
1160 int prot1;
1161 prot1 = *plast_prot;
1162 if (prot != prot1) {
b86bda5b
FB
1163 if (*pstart != -1) {
1164 term_printf("%08x-%08x %08x %c%c%c\n",
5fafdf24 1165 *pstart, end, end - *pstart,
9746b15b 1166 prot1 & PG_USER_MASK ? 'u' : '-',
b86bda5b 1167 'r',
9746b15b 1168 prot1 & PG_RW_MASK ? 'w' : '-');
b86bda5b
FB
1169 }
1170 if (prot != 0)
1171 *pstart = end;
1172 else
1173 *pstart = -1;
1174 *plast_prot = prot;
1175 }
1176}
1177
1178static void mem_info(void)
1179{
6a00d601 1180 CPUState *env;
b86bda5b
FB
1181 int l1, l2, prot, last_prot;
1182 uint32_t pgd, pde, pte, start, end;
1183
6a00d601
FB
1184 env = mon_get_cpu();
1185 if (!env)
1186 return;
1187
b86bda5b
FB
1188 if (!(env->cr[0] & CR0_PG_MASK)) {
1189 term_printf("PG disabled\n");
1190 return;
1191 }
1192 pgd = env->cr[3] & ~0xfff;
1193 last_prot = 0;
1194 start = -1;
1195 for(l1 = 0; l1 < 1024; l1++) {
1196 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1197 pde = le32_to_cpu(pde);
1198 end = l1 << 22;
1199 if (pde & PG_PRESENT_MASK) {
1200 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1201 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1202 mem_print(&start, &last_prot, end, prot);
1203 } else {
1204 for(l2 = 0; l2 < 1024; l2++) {
5fafdf24 1205 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
b86bda5b
FB
1206 (uint8_t *)&pte, 4);
1207 pte = le32_to_cpu(pte);
1208 end = (l1 << 22) + (l2 << 12);
1209 if (pte & PG_PRESENT_MASK) {
1210 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1211 } else {
1212 prot = 0;
1213 }
1214 mem_print(&start, &last_prot, end, prot);
1215 }
1216 }
1217 } else {
1218 prot = 0;
1219 mem_print(&start, &last_prot, end, prot);
1220 }
1221 }
1222}
1223#endif
1224
0f4c6415
FB
1225static void do_info_kqemu(void)
1226{
1227#ifdef USE_KQEMU
6a00d601 1228 CPUState *env;
0f4c6415
FB
1229 int val;
1230 val = 0;
6a00d601
FB
1231 env = mon_get_cpu();
1232 if (!env) {
1233 term_printf("No cpu initialized yet");
1234 return;
1235 }
1236 val = env->kqemu_enabled;
5f1ce948
FB
1237 term_printf("kqemu support: ");
1238 switch(val) {
1239 default:
1240 case 0:
1241 term_printf("disabled\n");
1242 break;
1243 case 1:
1244 term_printf("enabled for user code\n");
1245 break;
1246 case 2:
1247 term_printf("enabled for user and kernel code\n");
1248 break;
1249 }
0f4c6415 1250#else
5f1ce948 1251 term_printf("kqemu support: not compiled\n");
0f4c6415 1252#endif
5fafdf24 1253}
0f4c6415 1254
5f1ce948
FB
1255#ifdef CONFIG_PROFILER
1256
1257int64_t kqemu_time;
1258int64_t qemu_time;
1259int64_t kqemu_exec_count;
1260int64_t dev_time;
1261int64_t kqemu_ret_int_count;
1262int64_t kqemu_ret_excp_count;
1263int64_t kqemu_ret_intr_count;
1264
1265static void do_info_profile(void)
1266{
1267 int64_t total;
1268 total = qemu_time;
1269 if (total == 0)
1270 total = 1;
26a76461 1271 term_printf("async time %" PRId64 " (%0.3f)\n",
5f1ce948 1272 dev_time, dev_time / (double)ticks_per_sec);
26a76461 1273 term_printf("qemu time %" PRId64 " (%0.3f)\n",
5f1ce948 1274 qemu_time, qemu_time / (double)ticks_per_sec);
26a76461 1275 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
5f1ce948
FB
1276 kqemu_time, kqemu_time / (double)ticks_per_sec,
1277 kqemu_time / (double)total * 100.0,
1278 kqemu_exec_count,
1279 kqemu_ret_int_count,
1280 kqemu_ret_excp_count,
1281 kqemu_ret_intr_count);
1282 qemu_time = 0;
1283 kqemu_time = 0;
1284 kqemu_exec_count = 0;
1285 dev_time = 0;
1286 kqemu_ret_int_count = 0;
1287 kqemu_ret_excp_count = 0;
1288 kqemu_ret_intr_count = 0;
1289#ifdef USE_KQEMU
1290 kqemu_record_dump();
1291#endif
1292}
1293#else
1294static void do_info_profile(void)
1295{
1296 term_printf("Internal profiler not compiled\n");
1297}
1298#endif
1299
ec36b695
FB
1300/* Capture support */
1301static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1302
1303static void do_info_capture (void)
1304{
1305 int i;
1306 CaptureState *s;
1307
1308 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1309 term_printf ("[%d]: ", i);
1310 s->ops.info (s->opaque);
1311 }
1312}
1313
1314static void do_stop_capture (int n)
1315{
1316 int i;
1317 CaptureState *s;
1318
1319 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1320 if (i == n) {
1321 s->ops.destroy (s->opaque);
1322 LIST_REMOVE (s, entries);
1323 qemu_free (s);
1324 return;
1325 }
1326 }
1327}
1328
1329#ifdef HAS_AUDIO
1330int wav_start_capture (CaptureState *s, const char *path, int freq,
1331 int bits, int nchannels);
1332
1333static void do_wav_capture (const char *path,
1334 int has_freq, int freq,
1335 int has_bits, int bits,
1336 int has_channels, int nchannels)
1337{
1338 CaptureState *s;
1339
1340 s = qemu_mallocz (sizeof (*s));
1341 if (!s) {
1342 term_printf ("Not enough memory to add wave capture\n");
1343 return;
1344 }
1345
1346 freq = has_freq ? freq : 44100;
1347 bits = has_bits ? bits : 16;
1348 nchannels = has_channels ? nchannels : 2;
1349
1350 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1351 term_printf ("Faied to add wave capture\n");
1352 qemu_free (s);
1353 }
1354 LIST_INSERT_HEAD (&capture_head, s, entries);
1355}
1356#endif
1357
dc1c0b74
AJ
1358#if defined(TARGET_I386)
1359static void do_inject_nmi(int cpu_index)
1360{
1361 CPUState *env;
1362
1363 for (env = first_cpu; env != NULL; env = env->next_cpu)
1364 if (env->cpu_index == cpu_index) {
1365 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1366 break;
1367 }
1368}
1369#endif
1370
9dc39cba 1371static term_cmd_t term_cmds[] = {
5fafdf24 1372 { "help|?", "s?", do_help,
9dc39cba 1373 "[cmd]", "show the help" },
5fafdf24 1374 { "commit", "s", do_commit,
7954c734 1375 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
9307c4c1 1376 { "info", "s?", do_info,
9dc39cba 1377 "subcommand", "show various information about the system state" },
9307c4c1 1378 { "q|quit", "", do_quit,
9dc39cba 1379 "", "quit the emulator" },
81d0912d 1380 { "eject", "-fB", do_eject,
e598752a 1381 "[-f] device", "eject a removable medium (use -f to force it)" },
2ecea9b8
AJ
1382 { "change", "BFs?", do_change,
1383 "device filename [format]", "change a removable medium, optional format" },
5fafdf24 1384 { "screendump", "F", do_screen_dump,
59a983b9 1385 "filename", "save screen into PPM image 'filename'" },
788228c0 1386 { "logfile", "F", do_logfile,
e735b91c 1387 "filename", "output logs to 'filename'" },
9307c4c1 1388 { "log", "s", do_log,
5fafdf24 1389 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
faea38e7 1390 { "savevm", "s?", do_savevm,
5fafdf24 1391 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
faea38e7 1392 { "loadvm", "s", do_loadvm,
5fafdf24 1393 "tag|id", "restore a VM snapshot from its tag or id" },
faea38e7 1394 { "delvm", "s", do_delvm,
5fafdf24
TS
1395 "tag|id", "delete a VM snapshot from its tag or id" },
1396 { "stop", "", do_stop,
9307c4c1 1397 "", "stop emulation", },
5fafdf24 1398 { "c|cont", "", do_cont,
9307c4c1 1399 "", "resume emulation", },
67b915a5 1400#ifdef CONFIG_GDBSTUB
5fafdf24 1401 { "gdbserver", "s?", do_gdbserver,
9307c4c1 1402 "[port]", "start gdbserver session (default port=1234)", },
67b915a5 1403#endif
5fafdf24 1404 { "x", "/l", do_memory_dump,
9307c4c1 1405 "/fmt addr", "virtual memory dump starting at 'addr'", },
5fafdf24 1406 { "xp", "/l", do_physical_memory_dump,
9307c4c1 1407 "/fmt addr", "physical memory dump starting at 'addr'", },
5fafdf24 1408 { "p|print", "/l", do_print,
9307c4c1 1409 "/fmt expr", "print expression value (use $reg for CPU register access)", },
5fafdf24 1410 { "i", "/ii.", do_ioport_read,
3440557b
FB
1411 "/fmt addr", "I/O port read" },
1412
c8256f9d
AZ
1413 { "sendkey", "si?", do_sendkey,
1414 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
5fafdf24 1415 { "system_reset", "", do_system_reset,
e4f9082b 1416 "", "reset the system" },
5fafdf24 1417 { "system_powerdown", "", do_system_powerdown,
3475187d 1418 "", "send system power down event" },
5fafdf24 1419 { "sum", "ii", do_sum,
e4cf1adc 1420 "addr size", "compute the checksum of a memory region" },
a594cfbf
FB
1421 { "usb_add", "s", do_usb_add,
1422 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1423 { "usb_del", "s", do_usb_del,
1424 "device", "remove USB device 'bus.addr'" },
5fafdf24 1425 { "cpu", "i", do_cpu_set,
6a00d601 1426 "index", "set the default CPU" },
5fafdf24 1427 { "mouse_move", "sss?", do_mouse_move,
13224a87 1428 "dx dy [dz]", "send mouse move events" },
5fafdf24 1429 { "mouse_button", "i", do_mouse_button,
13224a87 1430 "state", "change mouse button state (1=L, 2=M, 4=R)" },
455204eb
TS
1431 { "mouse_set", "i", do_mouse_set,
1432 "index", "set which mouse device receives events" },
ec36b695
FB
1433#ifdef HAS_AUDIO
1434 { "wavcapture", "si?i?i?", do_wav_capture,
1435 "path [frequency bits channels]",
1436 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1437#endif
1438 { "stopcapture", "i", do_stop_capture,
1439 "capture index", "stop capture" },
5fafdf24 1440 { "memsave", "lis", do_memory_save,
b371dc59 1441 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
a8bdf7a6
AJ
1442 { "pmemsave", "lis", do_physical_memory_save,
1443 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
0ecdffbb
AJ
1444 { "boot_set", "s", do_boot_set,
1445 "bootdevice", "define new values for the boot device list" },
dc1c0b74
AJ
1446#if defined(TARGET_I386)
1447 { "nmi", "i", do_inject_nmi,
1448 "cpu", "inject an NMI on the given CPU", },
1449#endif
5fafdf24 1450 { NULL, NULL, },
9dc39cba
FB
1451};
1452
1453static term_cmd_t info_cmds[] = {
9bc9d1c7
FB
1454 { "version", "", do_info_version,
1455 "", "show the version of qemu" },
9307c4c1 1456 { "network", "", do_info_network,
9dc39cba 1457 "", "show the network state" },
9307c4c1 1458 { "block", "", do_info_block,
9dc39cba 1459 "", "show the block devices" },
a36e69dd
TS
1460 { "blockstats", "", do_info_blockstats,
1461 "", "show block device statistics" },
9307c4c1
FB
1462 { "registers", "", do_info_registers,
1463 "", "show the cpu registers" },
6a00d601
FB
1464 { "cpus", "", do_info_cpus,
1465 "", "show infos for each CPU" },
aa455485
FB
1466 { "history", "", do_info_history,
1467 "", "show the command line history", },
4a0fb71e
FB
1468 { "irq", "", irq_info,
1469 "", "show the interrupts statistics (if available)", },
4c27ba27
FB
1470 { "pic", "", pic_info,
1471 "", "show i8259 (PIC) state", },
86e0c048
FB
1472 { "pci", "", pci_info,
1473 "", "show PCI info", },
b86bda5b
FB
1474#if defined(TARGET_I386)
1475 { "tlb", "", tlb_info,
1476 "", "show virtual to physical memory mappings", },
1477 { "mem", "", mem_info,
1478 "", "show the active virtual memory mappings", },
1479#endif
e3db7226
FB
1480 { "jit", "", do_info_jit,
1481 "", "show dynamic compiler info", },
0f4c6415
FB
1482 { "kqemu", "", do_info_kqemu,
1483 "", "show kqemu information", },
a594cfbf
FB
1484 { "usb", "", usb_info,
1485 "", "show guest USB devices", },
1486 { "usbhost", "", usb_host_info,
1487 "", "show host USB devices", },
5f1ce948
FB
1488 { "profile", "", do_info_profile,
1489 "", "show profiling information", },
ec36b695 1490 { "capture", "", do_info_capture,
17100159 1491 "", "show capture information" },
faea38e7 1492 { "snapshots", "", do_info_snapshots,
17100159 1493 "", "show the currently saved VM snapshots" },
201a51fc
AZ
1494 { "pcmcia", "", pcmcia_info,
1495 "", "show guest PCMCIA status" },
455204eb
TS
1496 { "mice", "", do_info_mice,
1497 "", "show which guest mouse is receiving events" },
a9ce8590
FB
1498 { "vnc", "", do_info_vnc,
1499 "", "show the vnc server status"},
c35734b2
TS
1500 { "name", "", do_info_name,
1501 "", "show the current VM name" },
76a66253
JM
1502#if defined(TARGET_PPC)
1503 { "cpustats", "", do_info_cpu_stats,
1504 "", "show CPU statistics", },
1505#endif
31a60e22
BS
1506#if defined(CONFIG_SLIRP)
1507 { "slirp", "", do_info_slirp,
1508 "", "show SLIRP statistics", },
1509#endif
9dc39cba
FB
1510 { NULL, NULL, },
1511};
1512
9307c4c1
FB
1513/*******************************************************************/
1514
1515static const char *pch;
1516static jmp_buf expr_env;
1517
92a31b1f
FB
1518#define MD_TLONG 0
1519#define MD_I32 1
1520
9307c4c1
FB
1521typedef struct MonitorDef {
1522 const char *name;
1523 int offset;
92a31b1f
FB
1524 target_long (*get_value)(struct MonitorDef *md, int val);
1525 int type;
9307c4c1
FB
1526} MonitorDef;
1527
57206fd4 1528#if defined(TARGET_I386)
92a31b1f 1529static target_long monitor_get_pc (struct MonitorDef *md, int val)
57206fd4 1530{
6a00d601
FB
1531 CPUState *env = mon_get_cpu();
1532 if (!env)
1533 return 0;
1534 return env->eip + env->segs[R_CS].base;
57206fd4
FB
1535}
1536#endif
1537
a541f297 1538#if defined(TARGET_PPC)
92a31b1f 1539static target_long monitor_get_ccr (struct MonitorDef *md, int val)
a541f297 1540{
6a00d601 1541 CPUState *env = mon_get_cpu();
a541f297
FB
1542 unsigned int u;
1543 int i;
1544
6a00d601
FB
1545 if (!env)
1546 return 0;
1547
a541f297
FB
1548 u = 0;
1549 for (i = 0; i < 8; i++)
6a00d601 1550 u |= env->crf[i] << (32 - (4 * i));
a541f297
FB
1551
1552 return u;
1553}
1554
92a31b1f 1555static target_long monitor_get_msr (struct MonitorDef *md, int val)
a541f297 1556{
6a00d601
FB
1557 CPUState *env = mon_get_cpu();
1558 if (!env)
1559 return 0;
0411a972 1560 return env->msr;
a541f297
FB
1561}
1562
92a31b1f 1563static target_long monitor_get_xer (struct MonitorDef *md, int val)
a541f297 1564{
6a00d601
FB
1565 CPUState *env = mon_get_cpu();
1566 if (!env)
1567 return 0;
ff937dba 1568 return ppc_load_xer(env);
a541f297 1569}
9fddaa0c 1570
92a31b1f 1571static target_long monitor_get_decr (struct MonitorDef *md, int val)
9fddaa0c 1572{
6a00d601
FB
1573 CPUState *env = mon_get_cpu();
1574 if (!env)
1575 return 0;
1576 return cpu_ppc_load_decr(env);
9fddaa0c
FB
1577}
1578
92a31b1f 1579static target_long monitor_get_tbu (struct MonitorDef *md, int val)
9fddaa0c 1580{
6a00d601
FB
1581 CPUState *env = mon_get_cpu();
1582 if (!env)
1583 return 0;
1584 return cpu_ppc_load_tbu(env);
9fddaa0c
FB
1585}
1586
92a31b1f 1587static target_long monitor_get_tbl (struct MonitorDef *md, int val)
9fddaa0c 1588{
6a00d601
FB
1589 CPUState *env = mon_get_cpu();
1590 if (!env)
1591 return 0;
1592 return cpu_ppc_load_tbl(env);
9fddaa0c 1593}
a541f297
FB
1594#endif
1595
e95c8d51 1596#if defined(TARGET_SPARC)
7b936c0c 1597#ifndef TARGET_SPARC64
92a31b1f 1598static target_long monitor_get_psr (struct MonitorDef *md, int val)
e95c8d51 1599{
6a00d601
FB
1600 CPUState *env = mon_get_cpu();
1601 if (!env)
1602 return 0;
1603 return GET_PSR(env);
e95c8d51 1604}
7b936c0c 1605#endif
e95c8d51 1606
92a31b1f 1607static target_long monitor_get_reg(struct MonitorDef *md, int val)
e95c8d51 1608{
6a00d601
FB
1609 CPUState *env = mon_get_cpu();
1610 if (!env)
1611 return 0;
1612 return env->regwptr[val];
e95c8d51
FB
1613}
1614#endif
1615
9307c4c1
FB
1616static MonitorDef monitor_defs[] = {
1617#ifdef TARGET_I386
57206fd4
FB
1618
1619#define SEG(name, seg) \
92a31b1f 1620 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
57206fd4 1621 { name ".base", offsetof(CPUState, segs[seg].base) },\
92a31b1f 1622 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
57206fd4 1623
9307c4c1
FB
1624 { "eax", offsetof(CPUState, regs[0]) },
1625 { "ecx", offsetof(CPUState, regs[1]) },
1626 { "edx", offsetof(CPUState, regs[2]) },
1627 { "ebx", offsetof(CPUState, regs[3]) },
1628 { "esp|sp", offsetof(CPUState, regs[4]) },
1629 { "ebp|fp", offsetof(CPUState, regs[5]) },
1630 { "esi", offsetof(CPUState, regs[6]) },
01038d2a 1631 { "edi", offsetof(CPUState, regs[7]) },
92a31b1f
FB
1632#ifdef TARGET_X86_64
1633 { "r8", offsetof(CPUState, regs[8]) },
1634 { "r9", offsetof(CPUState, regs[9]) },
1635 { "r10", offsetof(CPUState, regs[10]) },
1636 { "r11", offsetof(CPUState, regs[11]) },
1637 { "r12", offsetof(CPUState, regs[12]) },
1638 { "r13", offsetof(CPUState, regs[13]) },
1639 { "r14", offsetof(CPUState, regs[14]) },
1640 { "r15", offsetof(CPUState, regs[15]) },
1641#endif
9307c4c1 1642 { "eflags", offsetof(CPUState, eflags) },
57206fd4
FB
1643 { "eip", offsetof(CPUState, eip) },
1644 SEG("cs", R_CS)
1645 SEG("ds", R_DS)
1646 SEG("es", R_ES)
01038d2a 1647 SEG("ss", R_SS)
57206fd4
FB
1648 SEG("fs", R_FS)
1649 SEG("gs", R_GS)
1650 { "pc", 0, monitor_get_pc, },
a541f297 1651#elif defined(TARGET_PPC)
ff937dba 1652 /* General purpose registers */
a541f297
FB
1653 { "r0", offsetof(CPUState, gpr[0]) },
1654 { "r1", offsetof(CPUState, gpr[1]) },
1655 { "r2", offsetof(CPUState, gpr[2]) },
1656 { "r3", offsetof(CPUState, gpr[3]) },
1657 { "r4", offsetof(CPUState, gpr[4]) },
1658 { "r5", offsetof(CPUState, gpr[5]) },
1659 { "r6", offsetof(CPUState, gpr[6]) },
1660 { "r7", offsetof(CPUState, gpr[7]) },
1661 { "r8", offsetof(CPUState, gpr[8]) },
1662 { "r9", offsetof(CPUState, gpr[9]) },
1663 { "r10", offsetof(CPUState, gpr[10]) },
1664 { "r11", offsetof(CPUState, gpr[11]) },
1665 { "r12", offsetof(CPUState, gpr[12]) },
1666 { "r13", offsetof(CPUState, gpr[13]) },
1667 { "r14", offsetof(CPUState, gpr[14]) },
1668 { "r15", offsetof(CPUState, gpr[15]) },
1669 { "r16", offsetof(CPUState, gpr[16]) },
1670 { "r17", offsetof(CPUState, gpr[17]) },
1671 { "r18", offsetof(CPUState, gpr[18]) },
1672 { "r19", offsetof(CPUState, gpr[19]) },
1673 { "r20", offsetof(CPUState, gpr[20]) },
1674 { "r21", offsetof(CPUState, gpr[21]) },
1675 { "r22", offsetof(CPUState, gpr[22]) },
1676 { "r23", offsetof(CPUState, gpr[23]) },
1677 { "r24", offsetof(CPUState, gpr[24]) },
1678 { "r25", offsetof(CPUState, gpr[25]) },
1679 { "r26", offsetof(CPUState, gpr[26]) },
1680 { "r27", offsetof(CPUState, gpr[27]) },
1681 { "r28", offsetof(CPUState, gpr[28]) },
1682 { "r29", offsetof(CPUState, gpr[29]) },
1683 { "r30", offsetof(CPUState, gpr[30]) },
1684 { "r31", offsetof(CPUState, gpr[31]) },
ff937dba
JM
1685 /* Floating point registers */
1686 { "f0", offsetof(CPUState, fpr[0]) },
1687 { "f1", offsetof(CPUState, fpr[1]) },
1688 { "f2", offsetof(CPUState, fpr[2]) },
1689 { "f3", offsetof(CPUState, fpr[3]) },
1690 { "f4", offsetof(CPUState, fpr[4]) },
1691 { "f5", offsetof(CPUState, fpr[5]) },
1692 { "f6", offsetof(CPUState, fpr[6]) },
1693 { "f7", offsetof(CPUState, fpr[7]) },
1694 { "f8", offsetof(CPUState, fpr[8]) },
1695 { "f9", offsetof(CPUState, fpr[9]) },
1696 { "f10", offsetof(CPUState, fpr[10]) },
1697 { "f11", offsetof(CPUState, fpr[11]) },
1698 { "f12", offsetof(CPUState, fpr[12]) },
1699 { "f13", offsetof(CPUState, fpr[13]) },
1700 { "f14", offsetof(CPUState, fpr[14]) },
1701 { "f15", offsetof(CPUState, fpr[15]) },
1702 { "f16", offsetof(CPUState, fpr[16]) },
1703 { "f17", offsetof(CPUState, fpr[17]) },
1704 { "f18", offsetof(CPUState, fpr[18]) },
1705 { "f19", offsetof(CPUState, fpr[19]) },
1706 { "f20", offsetof(CPUState, fpr[20]) },
1707 { "f21", offsetof(CPUState, fpr[21]) },
1708 { "f22", offsetof(CPUState, fpr[22]) },
1709 { "f23", offsetof(CPUState, fpr[23]) },
1710 { "f24", offsetof(CPUState, fpr[24]) },
1711 { "f25", offsetof(CPUState, fpr[25]) },
1712 { "f26", offsetof(CPUState, fpr[26]) },
1713 { "f27", offsetof(CPUState, fpr[27]) },
1714 { "f28", offsetof(CPUState, fpr[28]) },
1715 { "f29", offsetof(CPUState, fpr[29]) },
1716 { "f30", offsetof(CPUState, fpr[30]) },
1717 { "f31", offsetof(CPUState, fpr[31]) },
1718 { "fpscr", offsetof(CPUState, fpscr) },
1719 /* Next instruction pointer */
57206fd4 1720 { "nip|pc", offsetof(CPUState, nip) },
a541f297
FB
1721 { "lr", offsetof(CPUState, lr) },
1722 { "ctr", offsetof(CPUState, ctr) },
9fddaa0c 1723 { "decr", 0, &monitor_get_decr, },
a541f297 1724 { "ccr", 0, &monitor_get_ccr, },
ff937dba 1725 /* Machine state register */
a541f297
FB
1726 { "msr", 0, &monitor_get_msr, },
1727 { "xer", 0, &monitor_get_xer, },
9fddaa0c
FB
1728 { "tbu", 0, &monitor_get_tbu, },
1729 { "tbl", 0, &monitor_get_tbl, },
ff937dba
JM
1730#if defined(TARGET_PPC64)
1731 /* Address space register */
1732 { "asr", offsetof(CPUState, asr) },
1733#endif
1734 /* Segment registers */
a541f297
FB
1735 { "sdr1", offsetof(CPUState, sdr1) },
1736 { "sr0", offsetof(CPUState, sr[0]) },
1737 { "sr1", offsetof(CPUState, sr[1]) },
1738 { "sr2", offsetof(CPUState, sr[2]) },
1739 { "sr3", offsetof(CPUState, sr[3]) },
1740 { "sr4", offsetof(CPUState, sr[4]) },
1741 { "sr5", offsetof(CPUState, sr[5]) },
1742 { "sr6", offsetof(CPUState, sr[6]) },
1743 { "sr7", offsetof(CPUState, sr[7]) },
1744 { "sr8", offsetof(CPUState, sr[8]) },
1745 { "sr9", offsetof(CPUState, sr[9]) },
1746 { "sr10", offsetof(CPUState, sr[10]) },
1747 { "sr11", offsetof(CPUState, sr[11]) },
1748 { "sr12", offsetof(CPUState, sr[12]) },
1749 { "sr13", offsetof(CPUState, sr[13]) },
1750 { "sr14", offsetof(CPUState, sr[14]) },
1751 { "sr15", offsetof(CPUState, sr[15]) },
1752 /* Too lazy to put BATs and SPRs ... */
e95c8d51
FB
1753#elif defined(TARGET_SPARC)
1754 { "g0", offsetof(CPUState, gregs[0]) },
1755 { "g1", offsetof(CPUState, gregs[1]) },
1756 { "g2", offsetof(CPUState, gregs[2]) },
1757 { "g3", offsetof(CPUState, gregs[3]) },
1758 { "g4", offsetof(CPUState, gregs[4]) },
1759 { "g5", offsetof(CPUState, gregs[5]) },
1760 { "g6", offsetof(CPUState, gregs[6]) },
1761 { "g7", offsetof(CPUState, gregs[7]) },
1762 { "o0", 0, monitor_get_reg },
1763 { "o1", 1, monitor_get_reg },
1764 { "o2", 2, monitor_get_reg },
1765 { "o3", 3, monitor_get_reg },
1766 { "o4", 4, monitor_get_reg },
1767 { "o5", 5, monitor_get_reg },
1768 { "o6", 6, monitor_get_reg },
1769 { "o7", 7, monitor_get_reg },
1770 { "l0", 8, monitor_get_reg },
1771 { "l1", 9, monitor_get_reg },
1772 { "l2", 10, monitor_get_reg },
1773 { "l3", 11, monitor_get_reg },
1774 { "l4", 12, monitor_get_reg },
1775 { "l5", 13, monitor_get_reg },
1776 { "l6", 14, monitor_get_reg },
1777 { "l7", 15, monitor_get_reg },
1778 { "i0", 16, monitor_get_reg },
1779 { "i1", 17, monitor_get_reg },
1780 { "i2", 18, monitor_get_reg },
1781 { "i3", 19, monitor_get_reg },
1782 { "i4", 20, monitor_get_reg },
1783 { "i5", 21, monitor_get_reg },
1784 { "i6", 22, monitor_get_reg },
1785 { "i7", 23, monitor_get_reg },
1786 { "pc", offsetof(CPUState, pc) },
1787 { "npc", offsetof(CPUState, npc) },
1788 { "y", offsetof(CPUState, y) },
7b936c0c 1789#ifndef TARGET_SPARC64
e95c8d51
FB
1790 { "psr", 0, &monitor_get_psr, },
1791 { "wim", offsetof(CPUState, wim) },
7b936c0c 1792#endif
e95c8d51
FB
1793 { "tbr", offsetof(CPUState, tbr) },
1794 { "fsr", offsetof(CPUState, fsr) },
1795 { "f0", offsetof(CPUState, fpr[0]) },
1796 { "f1", offsetof(CPUState, fpr[1]) },
1797 { "f2", offsetof(CPUState, fpr[2]) },
1798 { "f3", offsetof(CPUState, fpr[3]) },
1799 { "f4", offsetof(CPUState, fpr[4]) },
1800 { "f5", offsetof(CPUState, fpr[5]) },
1801 { "f6", offsetof(CPUState, fpr[6]) },
1802 { "f7", offsetof(CPUState, fpr[7]) },
1803 { "f8", offsetof(CPUState, fpr[8]) },
1804 { "f9", offsetof(CPUState, fpr[9]) },
1805 { "f10", offsetof(CPUState, fpr[10]) },
1806 { "f11", offsetof(CPUState, fpr[11]) },
1807 { "f12", offsetof(CPUState, fpr[12]) },
1808 { "f13", offsetof(CPUState, fpr[13]) },
1809 { "f14", offsetof(CPUState, fpr[14]) },
1810 { "f15", offsetof(CPUState, fpr[15]) },
1811 { "f16", offsetof(CPUState, fpr[16]) },
1812 { "f17", offsetof(CPUState, fpr[17]) },
1813 { "f18", offsetof(CPUState, fpr[18]) },
1814 { "f19", offsetof(CPUState, fpr[19]) },
1815 { "f20", offsetof(CPUState, fpr[20]) },
1816 { "f21", offsetof(CPUState, fpr[21]) },
1817 { "f22", offsetof(CPUState, fpr[22]) },
1818 { "f23", offsetof(CPUState, fpr[23]) },
1819 { "f24", offsetof(CPUState, fpr[24]) },
1820 { "f25", offsetof(CPUState, fpr[25]) },
1821 { "f26", offsetof(CPUState, fpr[26]) },
1822 { "f27", offsetof(CPUState, fpr[27]) },
1823 { "f28", offsetof(CPUState, fpr[28]) },
1824 { "f29", offsetof(CPUState, fpr[29]) },
1825 { "f30", offsetof(CPUState, fpr[30]) },
1826 { "f31", offsetof(CPUState, fpr[31]) },
7b936c0c
FB
1827#ifdef TARGET_SPARC64
1828 { "f32", offsetof(CPUState, fpr[32]) },
1829 { "f34", offsetof(CPUState, fpr[34]) },
1830 { "f36", offsetof(CPUState, fpr[36]) },
1831 { "f38", offsetof(CPUState, fpr[38]) },
1832 { "f40", offsetof(CPUState, fpr[40]) },
1833 { "f42", offsetof(CPUState, fpr[42]) },
1834 { "f44", offsetof(CPUState, fpr[44]) },
1835 { "f46", offsetof(CPUState, fpr[46]) },
1836 { "f48", offsetof(CPUState, fpr[48]) },
1837 { "f50", offsetof(CPUState, fpr[50]) },
1838 { "f52", offsetof(CPUState, fpr[52]) },
1839 { "f54", offsetof(CPUState, fpr[54]) },
1840 { "f56", offsetof(CPUState, fpr[56]) },
1841 { "f58", offsetof(CPUState, fpr[58]) },
1842 { "f60", offsetof(CPUState, fpr[60]) },
1843 { "f62", offsetof(CPUState, fpr[62]) },
1844 { "asi", offsetof(CPUState, asi) },
1845 { "pstate", offsetof(CPUState, pstate) },
1846 { "cansave", offsetof(CPUState, cansave) },
1847 { "canrestore", offsetof(CPUState, canrestore) },
1848 { "otherwin", offsetof(CPUState, otherwin) },
1849 { "wstate", offsetof(CPUState, wstate) },
1850 { "cleanwin", offsetof(CPUState, cleanwin) },
1851 { "fprs", offsetof(CPUState, fprs) },
1852#endif
9307c4c1
FB
1853#endif
1854 { NULL },
1855};
1856
5fafdf24 1857static void expr_error(const char *fmt)
9dc39cba 1858{
9307c4c1
FB
1859 term_printf(fmt);
1860 term_printf("\n");
1861 longjmp(expr_env, 1);
1862}
1863
6a00d601 1864/* return 0 if OK, -1 if not found, -2 if no CPU defined */
92a31b1f 1865static int get_monitor_def(target_long *pval, const char *name)
9307c4c1
FB
1866{
1867 MonitorDef *md;
92a31b1f
FB
1868 void *ptr;
1869
9307c4c1
FB
1870 for(md = monitor_defs; md->name != NULL; md++) {
1871 if (compare_cmd(name, md->name)) {
1872 if (md->get_value) {
e95c8d51 1873 *pval = md->get_value(md, md->offset);
9307c4c1 1874 } else {
6a00d601
FB
1875 CPUState *env = mon_get_cpu();
1876 if (!env)
1877 return -2;
1878 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
1879 switch(md->type) {
1880 case MD_I32:
1881 *pval = *(int32_t *)ptr;
1882 break;
1883 case MD_TLONG:
1884 *pval = *(target_long *)ptr;
1885 break;
1886 default:
1887 *pval = 0;
1888 break;
1889 }
9307c4c1
FB
1890 }
1891 return 0;
1892 }
1893 }
1894 return -1;
1895}
1896
1897static void next(void)
1898{
1899 if (pch != '\0') {
1900 pch++;
1901 while (isspace(*pch))
1902 pch++;
1903 }
1904}
1905
c2efc95d 1906static int64_t expr_sum(void);
9307c4c1 1907
c2efc95d 1908static int64_t expr_unary(void)
9307c4c1 1909{
c2efc95d 1910 int64_t n;
9307c4c1 1911 char *p;
6a00d601 1912 int ret;
9307c4c1
FB
1913
1914 switch(*pch) {
1915 case '+':
1916 next();
1917 n = expr_unary();
1918 break;
1919 case '-':
1920 next();
1921 n = -expr_unary();
1922 break;
1923 case '~':
1924 next();
1925 n = ~expr_unary();
1926 break;
1927 case '(':
1928 next();
1929 n = expr_sum();
1930 if (*pch != ')') {
1931 expr_error("')' expected");
1932 }
1933 next();
1934 break;
81d0912d
FB
1935 case '\'':
1936 pch++;
1937 if (*pch == '\0')
1938 expr_error("character constant expected");
1939 n = *pch;
1940 pch++;
1941 if (*pch != '\'')
1942 expr_error("missing terminating \' character");
1943 next();
1944 break;
9307c4c1
FB
1945 case '$':
1946 {
1947 char buf[128], *q;
69b34976 1948 target_long reg=0;
3b46e624 1949
9307c4c1
FB
1950 pch++;
1951 q = buf;
1952 while ((*pch >= 'a' && *pch <= 'z') ||
1953 (*pch >= 'A' && *pch <= 'Z') ||
1954 (*pch >= '0' && *pch <= '9') ||
57206fd4 1955 *pch == '_' || *pch == '.') {
9307c4c1
FB
1956 if ((q - buf) < sizeof(buf) - 1)
1957 *q++ = *pch;
1958 pch++;
1959 }
1960 while (isspace(*pch))
1961 pch++;
1962 *q = 0;
7743e588 1963 ret = get_monitor_def(&reg, buf);
6a00d601 1964 if (ret == -1)
9307c4c1 1965 expr_error("unknown register");
5fafdf24 1966 else if (ret == -2)
6a00d601 1967 expr_error("no cpu defined");
7743e588 1968 n = reg;
9307c4c1
FB
1969 }
1970 break;
1971 case '\0':
1972 expr_error("unexpected end of expression");
1973 n = 0;
1974 break;
1975 default:
7743e588 1976#if TARGET_PHYS_ADDR_BITS > 32
4f4fbf77
FB
1977 n = strtoull(pch, &p, 0);
1978#else
9307c4c1 1979 n = strtoul(pch, &p, 0);
4f4fbf77 1980#endif
9307c4c1
FB
1981 if (pch == p) {
1982 expr_error("invalid char in expression");
1983 }
1984 pch = p;
1985 while (isspace(*pch))
1986 pch++;
1987 break;
1988 }
1989 return n;
1990}
1991
1992
c2efc95d 1993static int64_t expr_prod(void)
9307c4c1 1994{
c2efc95d 1995 int64_t val, val2;
92a31b1f 1996 int op;
3b46e624 1997
9307c4c1
FB
1998 val = expr_unary();
1999 for(;;) {
2000 op = *pch;
2001 if (op != '*' && op != '/' && op != '%')
2002 break;
2003 next();
2004 val2 = expr_unary();
2005 switch(op) {
2006 default:
2007 case '*':
2008 val *= val2;
2009 break;
2010 case '/':
2011 case '%':
5fafdf24 2012 if (val2 == 0)
5b60212f 2013 expr_error("division by zero");
9307c4c1
FB
2014 if (op == '/')
2015 val /= val2;
2016 else
2017 val %= val2;
2018 break;
2019 }
2020 }
2021 return val;
2022}
2023
c2efc95d 2024static int64_t expr_logic(void)
9307c4c1 2025{
c2efc95d 2026 int64_t val, val2;
92a31b1f 2027 int op;
9307c4c1
FB
2028
2029 val = expr_prod();
2030 for(;;) {
2031 op = *pch;
2032 if (op != '&' && op != '|' && op != '^')
2033 break;
2034 next();
2035 val2 = expr_prod();
2036 switch(op) {
2037 default:
2038 case '&':
2039 val &= val2;
2040 break;
2041 case '|':
2042 val |= val2;
2043 break;
2044 case '^':
2045 val ^= val2;
2046 break;
2047 }
2048 }
2049 return val;
2050}
2051
c2efc95d 2052static int64_t expr_sum(void)
9307c4c1 2053{
c2efc95d 2054 int64_t val, val2;
92a31b1f 2055 int op;
9307c4c1
FB
2056
2057 val = expr_logic();
2058 for(;;) {
2059 op = *pch;
2060 if (op != '+' && op != '-')
2061 break;
2062 next();
2063 val2 = expr_logic();
2064 if (op == '+')
2065 val += val2;
2066 else
2067 val -= val2;
2068 }
2069 return val;
2070}
2071
c2efc95d 2072static int get_expr(int64_t *pval, const char **pp)
9307c4c1
FB
2073{
2074 pch = *pp;
2075 if (setjmp(expr_env)) {
2076 *pp = pch;
2077 return -1;
2078 }
2079 while (isspace(*pch))
2080 pch++;
2081 *pval = expr_sum();
2082 *pp = pch;
2083 return 0;
2084}
2085
2086static int get_str(char *buf, int buf_size, const char **pp)
2087{
2088 const char *p;
2089 char *q;
2090 int c;
2091
81d0912d 2092 q = buf;
9307c4c1
FB
2093 p = *pp;
2094 while (isspace(*p))
2095 p++;
2096 if (*p == '\0') {
2097 fail:
81d0912d 2098 *q = '\0';
9307c4c1
FB
2099 *pp = p;
2100 return -1;
2101 }
9307c4c1
FB
2102 if (*p == '\"') {
2103 p++;
2104 while (*p != '\0' && *p != '\"') {
2105 if (*p == '\\') {
2106 p++;
2107 c = *p++;
2108 switch(c) {
2109 case 'n':
2110 c = '\n';
2111 break;
2112 case 'r':
2113 c = '\r';
2114 break;
2115 case '\\':
2116 case '\'':
2117 case '\"':
2118 break;
2119 default:
2120 qemu_printf("unsupported escape code: '\\%c'\n", c);
2121 goto fail;
2122 }
2123 if ((q - buf) < buf_size - 1) {
2124 *q++ = c;
2125 }
2126 } else {
2127 if ((q - buf) < buf_size - 1) {
2128 *q++ = *p;
2129 }
2130 p++;
2131 }
2132 }
2133 if (*p != '\"') {
5b60212f 2134 qemu_printf("unterminated string\n");
9307c4c1
FB
2135 goto fail;
2136 }
2137 p++;
2138 } else {
2139 while (*p != '\0' && !isspace(*p)) {
2140 if ((q - buf) < buf_size - 1) {
2141 *q++ = *p;
2142 }
2143 p++;
2144 }
9307c4c1 2145 }
81d0912d 2146 *q = '\0';
9307c4c1
FB
2147 *pp = p;
2148 return 0;
2149}
2150
2151static int default_fmt_format = 'x';
2152static int default_fmt_size = 4;
2153
2154#define MAX_ARGS 16
2155
7e2515e8 2156static void monitor_handle_command(const char *cmdline)
9307c4c1
FB
2157{
2158 const char *p, *pstart, *typestr;
2159 char *q;
2160 int c, nb_args, len, i, has_arg;
9dc39cba 2161 term_cmd_t *cmd;
9307c4c1
FB
2162 char cmdname[256];
2163 char buf[1024];
2164 void *str_allocated[MAX_ARGS];
2165 void *args[MAX_ARGS];
a5f1b965
BS
2166 void (*handler_0)(void);
2167 void (*handler_1)(void *arg0);
2168 void (*handler_2)(void *arg0, void *arg1);
2169 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2170 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2171 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2172 void *arg4);
2173 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2174 void *arg4, void *arg5);
2175 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2176 void *arg4, void *arg5, void *arg6);
9dc39cba
FB
2177
2178#ifdef DEBUG
2179 term_printf("command='%s'\n", cmdline);
2180#endif
3b46e624 2181
9307c4c1 2182 /* extract the command name */
9dc39cba 2183 p = cmdline;
9307c4c1
FB
2184 q = cmdname;
2185 while (isspace(*p))
2186 p++;
2187 if (*p == '\0')
2188 return;
2189 pstart = p;
2190 while (*p != '\0' && *p != '/' && !isspace(*p))
2191 p++;
2192 len = p - pstart;
2193 if (len > sizeof(cmdname) - 1)
2194 len = sizeof(cmdname) - 1;
2195 memcpy(cmdname, pstart, len);
2196 cmdname[len] = '\0';
3b46e624 2197
9307c4c1
FB
2198 /* find the command */
2199 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
5fafdf24 2200 if (compare_cmd(cmdname, cmd->name))
9307c4c1
FB
2201 goto found;
2202 }
2203 term_printf("unknown command: '%s'\n", cmdname);
2204 return;
2205 found:
2206
2207 for(i = 0; i < MAX_ARGS; i++)
2208 str_allocated[i] = NULL;
3b46e624 2209
9307c4c1
FB
2210 /* parse the parameters */
2211 typestr = cmd->args_type;
2212 nb_args = 0;
9dc39cba 2213 for(;;) {
9307c4c1
FB
2214 c = *typestr;
2215 if (c == '\0')
9dc39cba 2216 break;
9307c4c1
FB
2217 typestr++;
2218 switch(c) {
2219 case 'F':
81d0912d 2220 case 'B':
9307c4c1
FB
2221 case 's':
2222 {
2223 int ret;
2224 char *str;
3b46e624 2225
5fafdf24 2226 while (isspace(*p))
9307c4c1
FB
2227 p++;
2228 if (*typestr == '?') {
2229 typestr++;
2230 if (*p == '\0') {
2231 /* no optional string: NULL argument */
2232 str = NULL;
2233 goto add_str;
2234 }
2235 }
2236 ret = get_str(buf, sizeof(buf), &p);
2237 if (ret < 0) {
81d0912d
FB
2238 switch(c) {
2239 case 'F':
9307c4c1 2240 term_printf("%s: filename expected\n", cmdname);
81d0912d
FB
2241 break;
2242 case 'B':
2243 term_printf("%s: block device name expected\n", cmdname);
2244 break;
2245 default:
9307c4c1 2246 term_printf("%s: string expected\n", cmdname);
81d0912d
FB
2247 break;
2248 }
9307c4c1
FB
2249 goto fail;
2250 }
2251 str = qemu_malloc(strlen(buf) + 1);
2252 strcpy(str, buf);
2253 str_allocated[nb_args] = str;
2254 add_str:
2255 if (nb_args >= MAX_ARGS) {
2256 error_args:
2257 term_printf("%s: too many arguments\n", cmdname);
2258 goto fail;
2259 }
2260 args[nb_args++] = str;
2261 }
9dc39cba 2262 break;
9307c4c1
FB
2263 case '/':
2264 {
2265 int count, format, size;
3b46e624 2266
9307c4c1
FB
2267 while (isspace(*p))
2268 p++;
2269 if (*p == '/') {
2270 /* format found */
2271 p++;
2272 count = 1;
2273 if (isdigit(*p)) {
2274 count = 0;
2275 while (isdigit(*p)) {
2276 count = count * 10 + (*p - '0');
2277 p++;
2278 }
2279 }
2280 size = -1;
2281 format = -1;
2282 for(;;) {
2283 switch(*p) {
2284 case 'o':
2285 case 'd':
2286 case 'u':
2287 case 'x':
2288 case 'i':
2289 case 'c':
2290 format = *p++;
2291 break;
2292 case 'b':
2293 size = 1;
2294 p++;
2295 break;
2296 case 'h':
2297 size = 2;
2298 p++;
2299 break;
2300 case 'w':
2301 size = 4;
2302 p++;
2303 break;
2304 case 'g':
2305 case 'L':
2306 size = 8;
2307 p++;
2308 break;
2309 default:
2310 goto next;
2311 }
2312 }
2313 next:
2314 if (*p != '\0' && !isspace(*p)) {
2315 term_printf("invalid char in format: '%c'\n", *p);
2316 goto fail;
2317 }
9307c4c1
FB
2318 if (format < 0)
2319 format = default_fmt_format;
4c27ba27
FB
2320 if (format != 'i') {
2321 /* for 'i', not specifying a size gives -1 as size */
2322 if (size < 0)
2323 size = default_fmt_size;
2324 }
9307c4c1
FB
2325 default_fmt_size = size;
2326 default_fmt_format = format;
2327 } else {
2328 count = 1;
2329 format = default_fmt_format;
4c27ba27
FB
2330 if (format != 'i') {
2331 size = default_fmt_size;
2332 } else {
2333 size = -1;
2334 }
9307c4c1
FB
2335 }
2336 if (nb_args + 3 > MAX_ARGS)
2337 goto error_args;
1c5bf3bf
JM
2338 args[nb_args++] = (void*)(long)count;
2339 args[nb_args++] = (void*)(long)format;
2340 args[nb_args++] = (void*)(long)size;
9307c4c1 2341 }
9dc39cba 2342 break;
9307c4c1 2343 case 'i':
92a31b1f 2344 case 'l':
9307c4c1 2345 {
c2efc95d 2346 int64_t val;
7743e588 2347
5fafdf24 2348 while (isspace(*p))
9307c4c1 2349 p++;
3440557b 2350 if (*typestr == '?' || *typestr == '.') {
3440557b
FB
2351 if (*typestr == '?') {
2352 if (*p == '\0')
2353 has_arg = 0;
2354 else
2355 has_arg = 1;
2356 } else {
2357 if (*p == '.') {
2358 p++;
5fafdf24 2359 while (isspace(*p))
3440557b
FB
2360 p++;
2361 has_arg = 1;
2362 } else {
2363 has_arg = 0;
2364 }
2365 }
13224a87 2366 typestr++;
9307c4c1
FB
2367 if (nb_args >= MAX_ARGS)
2368 goto error_args;
1c5bf3bf 2369 args[nb_args++] = (void *)(long)has_arg;
9307c4c1
FB
2370 if (!has_arg) {
2371 if (nb_args >= MAX_ARGS)
2372 goto error_args;
2373 val = -1;
2374 goto add_num;
2375 }
2376 }
2377 if (get_expr(&val, &p))
2378 goto fail;
2379 add_num:
92a31b1f
FB
2380 if (c == 'i') {
2381 if (nb_args >= MAX_ARGS)
2382 goto error_args;
1c5bf3bf 2383 args[nb_args++] = (void *)(long)val;
92a31b1f
FB
2384 } else {
2385 if ((nb_args + 1) >= MAX_ARGS)
2386 goto error_args;
7743e588 2387#if TARGET_PHYS_ADDR_BITS > 32
1c5bf3bf 2388 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
92a31b1f
FB
2389#else
2390 args[nb_args++] = (void *)0;
2391#endif
1c5bf3bf 2392 args[nb_args++] = (void *)(long)(val & 0xffffffff);
92a31b1f 2393 }
9307c4c1
FB
2394 }
2395 break;
2396 case '-':
2397 {
2398 int has_option;
2399 /* option */
3b46e624 2400
9307c4c1
FB
2401 c = *typestr++;
2402 if (c == '\0')
2403 goto bad_type;
5fafdf24 2404 while (isspace(*p))
9307c4c1
FB
2405 p++;
2406 has_option = 0;
2407 if (*p == '-') {
2408 p++;
2409 if (*p != c) {
5fafdf24 2410 term_printf("%s: unsupported option -%c\n",
9307c4c1
FB
2411 cmdname, *p);
2412 goto fail;
2413 }
2414 p++;
2415 has_option = 1;
2416 }
2417 if (nb_args >= MAX_ARGS)
2418 goto error_args;
1c5bf3bf 2419 args[nb_args++] = (void *)(long)has_option;
9307c4c1
FB
2420 }
2421 break;
2422 default:
2423 bad_type:
2424 term_printf("%s: unknown type '%c'\n", cmdname, c);
2425 goto fail;
2426 }
9dc39cba 2427 }
9307c4c1
FB
2428 /* check that all arguments were parsed */
2429 while (isspace(*p))
2430 p++;
2431 if (*p != '\0') {
5fafdf24 2432 term_printf("%s: extraneous characters at the end of line\n",
9307c4c1
FB
2433 cmdname);
2434 goto fail;
9dc39cba 2435 }
9307c4c1
FB
2436
2437 switch(nb_args) {
2438 case 0:
a5f1b965
BS
2439 handler_0 = cmd->handler;
2440 handler_0();
9307c4c1
FB
2441 break;
2442 case 1:
a5f1b965
BS
2443 handler_1 = cmd->handler;
2444 handler_1(args[0]);
9307c4c1
FB
2445 break;
2446 case 2:
a5f1b965
BS
2447 handler_2 = cmd->handler;
2448 handler_2(args[0], args[1]);
9307c4c1
FB
2449 break;
2450 case 3:
a5f1b965
BS
2451 handler_3 = cmd->handler;
2452 handler_3(args[0], args[1], args[2]);
9307c4c1
FB
2453 break;
2454 case 4:
a5f1b965
BS
2455 handler_4 = cmd->handler;
2456 handler_4(args[0], args[1], args[2], args[3]);
9307c4c1
FB
2457 break;
2458 case 5:
a5f1b965
BS
2459 handler_5 = cmd->handler;
2460 handler_5(args[0], args[1], args[2], args[3], args[4]);
9307c4c1 2461 break;
3440557b 2462 case 6:
a5f1b965
BS
2463 handler_6 = cmd->handler;
2464 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
3440557b 2465 break;
ec36b695 2466 case 7:
a5f1b965
BS
2467 handler_7 = cmd->handler;
2468 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
ec36b695 2469 break;
9307c4c1
FB
2470 default:
2471 term_printf("unsupported number of arguments: %d\n", nb_args);
2472 goto fail;
9dc39cba 2473 }
9307c4c1
FB
2474 fail:
2475 for(i = 0; i < MAX_ARGS; i++)
2476 qemu_free(str_allocated[i]);
9dc39cba 2477 return;
9dc39cba
FB
2478}
2479
81d0912d
FB
2480static void cmd_completion(const char *name, const char *list)
2481{
2482 const char *p, *pstart;
2483 char cmd[128];
2484 int len;
2485
2486 p = list;
2487 for(;;) {
2488 pstart = p;
2489 p = strchr(p, '|');
2490 if (!p)
2491 p = pstart + strlen(pstart);
2492 len = p - pstart;
2493 if (len > sizeof(cmd) - 2)
2494 len = sizeof(cmd) - 2;
2495 memcpy(cmd, pstart, len);
2496 cmd[len] = '\0';
2497 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2498 add_completion(cmd);
2499 }
2500 if (*p == '\0')
2501 break;
2502 p++;
2503 }
2504}
2505
2506static void file_completion(const char *input)
2507{
2508 DIR *ffs;
2509 struct dirent *d;
2510 char path[1024];
2511 char file[1024], file_prefix[1024];
2512 int input_path_len;
2513 const char *p;
2514
5fafdf24 2515 p = strrchr(input, '/');
81d0912d
FB
2516 if (!p) {
2517 input_path_len = 0;
2518 pstrcpy(file_prefix, sizeof(file_prefix), input);
2519 strcpy(path, ".");
2520 } else {
2521 input_path_len = p - input + 1;
2522 memcpy(path, input, input_path_len);
2523 if (input_path_len > sizeof(path) - 1)
2524 input_path_len = sizeof(path) - 1;
2525 path[input_path_len] = '\0';
2526 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2527 }
2528#ifdef DEBUG_COMPLETION
2529 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2530#endif
2531 ffs = opendir(path);
2532 if (!ffs)
2533 return;
2534 for(;;) {
2535 struct stat sb;
2536 d = readdir(ffs);
2537 if (!d)
2538 break;
2539 if (strstart(d->d_name, file_prefix, NULL)) {
2540 memcpy(file, input, input_path_len);
2541 strcpy(file + input_path_len, d->d_name);
2542 /* stat the file to find out if it's a directory.
2543 * In that case add a slash to speed up typing long paths
2544 */
2545 stat(file, &sb);
2546 if(S_ISDIR(sb.st_mode))
2547 strcat(file, "/");
2548 add_completion(file);
2549 }
2550 }
2551 closedir(ffs);
2552}
2553
2554static void block_completion_it(void *opaque, const char *name)
2555{
2556 const char *input = opaque;
2557
2558 if (input[0] == '\0' ||
2559 !strncmp(name, (char *)input, strlen(input))) {
2560 add_completion(name);
2561 }
2562}
2563
2564/* NOTE: this parser is an approximate form of the real command parser */
2565static void parse_cmdline(const char *cmdline,
2566 int *pnb_args, char **args)
2567{
2568 const char *p;
2569 int nb_args, ret;
2570 char buf[1024];
2571
2572 p = cmdline;
2573 nb_args = 0;
2574 for(;;) {
2575 while (isspace(*p))
2576 p++;
2577 if (*p == '\0')
2578 break;
2579 if (nb_args >= MAX_ARGS)
2580 break;
2581 ret = get_str(buf, sizeof(buf), &p);
2582 args[nb_args] = qemu_strdup(buf);
2583 nb_args++;
2584 if (ret < 0)
2585 break;
2586 }
2587 *pnb_args = nb_args;
2588}
2589
7e2515e8 2590void readline_find_completion(const char *cmdline)
81d0912d
FB
2591{
2592 const char *cmdname;
2593 char *args[MAX_ARGS];
2594 int nb_args, i, len;
2595 const char *ptype, *str;
2596 term_cmd_t *cmd;
64866c3d 2597 const KeyDef *key;
81d0912d
FB
2598
2599 parse_cmdline(cmdline, &nb_args, args);
2600#ifdef DEBUG_COMPLETION
2601 for(i = 0; i < nb_args; i++) {
2602 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2603 }
2604#endif
2605
2606 /* if the line ends with a space, it means we want to complete the
2607 next arg */
2608 len = strlen(cmdline);
2609 if (len > 0 && isspace(cmdline[len - 1])) {
2610 if (nb_args >= MAX_ARGS)
2611 return;
2612 args[nb_args++] = qemu_strdup("");
2613 }
2614 if (nb_args <= 1) {
2615 /* command completion */
2616 if (nb_args == 0)
2617 cmdname = "";
2618 else
2619 cmdname = args[0];
2620 completion_index = strlen(cmdname);
2621 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2622 cmd_completion(cmdname, cmd->name);
2623 }
2624 } else {
2625 /* find the command */
2626 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2627 if (compare_cmd(args[0], cmd->name))
2628 goto found;
2629 }
2630 return;
2631 found:
2632 ptype = cmd->args_type;
2633 for(i = 0; i < nb_args - 2; i++) {
2634 if (*ptype != '\0') {
2635 ptype++;
2636 while (*ptype == '?')
2637 ptype++;
2638 }
2639 }
2640 str = args[nb_args - 1];
2641 switch(*ptype) {
2642 case 'F':
2643 /* file completion */
2644 completion_index = strlen(str);
2645 file_completion(str);
2646 break;
2647 case 'B':
2648 /* block device name completion */
2649 completion_index = strlen(str);
2650 bdrv_iterate(block_completion_it, (void *)str);
2651 break;
7fe48483
FB
2652 case 's':
2653 /* XXX: more generic ? */
2654 if (!strcmp(cmd->name, "info")) {
2655 completion_index = strlen(str);
2656 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2657 cmd_completion(str, cmd->name);
2658 }
64866c3d
FB
2659 } else if (!strcmp(cmd->name, "sendkey")) {
2660 completion_index = strlen(str);
2661 for(key = key_defs; key->name != NULL; key++) {
2662 cmd_completion(str, key->name);
2663 }
7fe48483
FB
2664 }
2665 break;
81d0912d
FB
2666 default:
2667 break;
2668 }
2669 }
2670 for(i = 0; i < nb_args; i++)
2671 qemu_free(args[i]);
2672}
2673
7e2515e8 2674static int term_can_read(void *opaque)
9dc39cba 2675{
7e2515e8 2676 return 128;
9dc39cba
FB
2677}
2678
7e2515e8 2679static void term_read(void *opaque, const uint8_t *buf, int size)
9dc39cba 2680{
7e2515e8
FB
2681 int i;
2682 for(i = 0; i < size; i++)
2683 readline_handle_byte(buf[i]);
9dc39cba
FB
2684}
2685
7e2515e8 2686static void monitor_handle_command1(void *opaque, const char *cmdline)
aa455485 2687{
7e2515e8
FB
2688 monitor_handle_command(cmdline);
2689 monitor_start_input();
aa455485
FB
2690}
2691
396f9297 2692void monitor_start_input(void)
aa455485 2693{
7e2515e8 2694 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
aa455485
FB
2695}
2696
86e94dea
TS
2697static void term_event(void *opaque, int event)
2698{
2699 if (event != CHR_EVENT_RESET)
2700 return;
2701
2702 if (!hide_banner)
2703 term_printf("QEMU %s monitor - type 'help' for more information\n",
2704 QEMU_VERSION);
2705 monitor_start_input();
2706}
2707
20d8a3ed
TS
2708static int is_first_init = 1;
2709
7e2515e8 2710void monitor_init(CharDriverState *hd, int show_banner)
aa455485 2711{
20d8a3ed
TS
2712 int i;
2713
2714 if (is_first_init) {
c8256f9d
AZ
2715 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2716 if (!key_timer)
2717 return;
20d8a3ed
TS
2718 for (i = 0; i < MAX_MON; i++) {
2719 monitor_hd[i] = NULL;
2720 }
2721 is_first_init = 0;
2722 }
2723 for (i = 0; i < MAX_MON; i++) {
2724 if (monitor_hd[i] == NULL) {
2725 monitor_hd[i] = hd;
2726 break;
2727 }
2728 }
2729
86e94dea
TS
2730 hide_banner = !show_banner;
2731
e5b0bc44 2732 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
aa455485
FB
2733}
2734
7e2515e8
FB
2735/* XXX: use threads ? */
2736/* modal monitor readline */
2737static int monitor_readline_started;
2738static char *monitor_readline_buf;
2739static int monitor_readline_buf_size;
81d0912d 2740
7e2515e8 2741static void monitor_readline_cb(void *opaque, const char *input)
81d0912d 2742{
7e2515e8
FB
2743 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2744 monitor_readline_started = 0;
81d0912d
FB
2745}
2746
7e2515e8
FB
2747void monitor_readline(const char *prompt, int is_password,
2748 char *buf, int buf_size)
9dc39cba 2749{
20d8a3ed 2750 int i;
bc0129d9 2751 int old_focus[MAX_MON];
20d8a3ed 2752
7e2515e8 2753 if (is_password) {
bc0129d9
AL
2754 for (i = 0; i < MAX_MON; i++) {
2755 old_focus[i] = 0;
2756 if (monitor_hd[i]) {
2757 old_focus[i] = monitor_hd[i]->focus;
2758 monitor_hd[i]->focus = 0;
20d8a3ed 2759 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
bc0129d9
AL
2760 }
2761 }
9dc39cba 2762 }
bc0129d9 2763
7e2515e8
FB
2764 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2765 monitor_readline_buf = buf;
2766 monitor_readline_buf_size = buf_size;
2767 monitor_readline_started = 1;
2768 while (monitor_readline_started) {
2769 main_loop_wait(10);
9dc39cba 2770 }
bc0129d9
AL
2771 /* restore original focus */
2772 if (is_password) {
2773 for (i = 0; i < MAX_MON; i++)
2774 if (old_focus[i])
2775 monitor_hd[i]->focus = old_focus[i];
2776 }
9dc39cba 2777}