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