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