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