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