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