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