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