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