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