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