]> git.proxmox.com Git - qemu.git/blame - monitor.c
monitor: Convert do_info_version() 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
376253ec 1636static void do_info_balloon(Monitor *mon)
df751fa8 1637{
c227f099 1638 ram_addr_t actual;
df751fa8
AL
1639
1640 actual = qemu_balloon_status();
bd322087 1641 if (kvm_enabled() && !kvm_has_sync_mmu())
376253ec
AL
1642 monitor_printf(mon, "Using KVM without synchronous MMU, "
1643 "ballooning disabled\n");
bd322087 1644 else if (actual == 0)
376253ec 1645 monitor_printf(mon, "Ballooning not activated in VM\n");
df751fa8 1646 else
376253ec 1647 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
df751fa8
AL
1648}
1649
15dfcd45 1650static qemu_acl *find_acl(Monitor *mon, const char *name)
76655d6d 1651{
15dfcd45 1652 qemu_acl *acl = qemu_acl_find(name);
76655d6d 1653
76655d6d 1654 if (!acl) {
15dfcd45 1655 monitor_printf(mon, "acl: unknown list '%s'\n", name);
76655d6d 1656 }
15dfcd45
JK
1657 return acl;
1658}
1659
d54908a5 1660static void do_acl_show(Monitor *mon, const QDict *qdict)
15dfcd45 1661{
d54908a5 1662 const char *aclname = qdict_get_str(qdict, "aclname");
15dfcd45
JK
1663 qemu_acl *acl = find_acl(mon, aclname);
1664 qemu_acl_entry *entry;
1665 int i = 0;
76655d6d 1666
15dfcd45 1667 if (acl) {
28a76be8 1668 monitor_printf(mon, "policy: %s\n",
76655d6d 1669 acl->defaultDeny ? "deny" : "allow");
72cf2d4f 1670 QTAILQ_FOREACH(entry, &acl->entries, next) {
28a76be8
AL
1671 i++;
1672 monitor_printf(mon, "%d: %s %s\n", i,
15dfcd45 1673 entry->deny ? "deny" : "allow", entry->match);
28a76be8 1674 }
15dfcd45
JK
1675 }
1676}
1677
d54908a5 1678static void do_acl_reset(Monitor *mon, const QDict *qdict)
15dfcd45 1679{
d54908a5 1680 const char *aclname = qdict_get_str(qdict, "aclname");
15dfcd45
JK
1681 qemu_acl *acl = find_acl(mon, aclname);
1682
1683 if (acl) {
28a76be8
AL
1684 qemu_acl_reset(acl);
1685 monitor_printf(mon, "acl: removed all rules\n");
15dfcd45
JK
1686 }
1687}
1688
f18c16de 1689static void do_acl_policy(Monitor *mon, const QDict *qdict)
15dfcd45 1690{
f18c16de
LC
1691 const char *aclname = qdict_get_str(qdict, "aclname");
1692 const char *policy = qdict_get_str(qdict, "policy");
15dfcd45 1693 qemu_acl *acl = find_acl(mon, aclname);
28a76be8 1694
15dfcd45
JK
1695 if (acl) {
1696 if (strcmp(policy, "allow") == 0) {
28a76be8
AL
1697 acl->defaultDeny = 0;
1698 monitor_printf(mon, "acl: policy set to 'allow'\n");
15dfcd45 1699 } else if (strcmp(policy, "deny") == 0) {
28a76be8
AL
1700 acl->defaultDeny = 1;
1701 monitor_printf(mon, "acl: policy set to 'deny'\n");
1702 } else {
15dfcd45
JK
1703 monitor_printf(mon, "acl: unknown policy '%s', "
1704 "expected 'deny' or 'allow'\n", policy);
28a76be8 1705 }
15dfcd45
JK
1706 }
1707}
28a76be8 1708
1bd1442e 1709static void do_acl_add(Monitor *mon, const QDict *qdict)
15dfcd45 1710{
1bd1442e
LC
1711 const char *aclname = qdict_get_str(qdict, "aclname");
1712 const char *match = qdict_get_str(qdict, "match");
1713 const char *policy = qdict_get_str(qdict, "policy");
1714 int has_index = qdict_haskey(qdict, "index");
1715 int index = qdict_get_try_int(qdict, "index", -1);
15dfcd45
JK
1716 qemu_acl *acl = find_acl(mon, aclname);
1717 int deny, ret;
1718
1719 if (acl) {
1720 if (strcmp(policy, "allow") == 0) {
1721 deny = 0;
1722 } else if (strcmp(policy, "deny") == 0) {
1723 deny = 1;
1724 } else {
1725 monitor_printf(mon, "acl: unknown policy '%s', "
1726 "expected 'deny' or 'allow'\n", policy);
28a76be8
AL
1727 return;
1728 }
28a76be8
AL
1729 if (has_index)
1730 ret = qemu_acl_insert(acl, deny, match, index);
1731 else
1732 ret = qemu_acl_append(acl, deny, match);
1733 if (ret < 0)
1734 monitor_printf(mon, "acl: unable to add acl entry\n");
1735 else
1736 monitor_printf(mon, "acl: added rule at position %d\n", ret);
15dfcd45
JK
1737 }
1738}
28a76be8 1739
f18c16de 1740static void do_acl_remove(Monitor *mon, const QDict *qdict)
15dfcd45 1741{
f18c16de
LC
1742 const char *aclname = qdict_get_str(qdict, "aclname");
1743 const char *match = qdict_get_str(qdict, "match");
15dfcd45
JK
1744 qemu_acl *acl = find_acl(mon, aclname);
1745 int ret;
28a76be8 1746
15dfcd45 1747 if (acl) {
28a76be8
AL
1748 ret = qemu_acl_remove(acl, match);
1749 if (ret < 0)
1750 monitor_printf(mon, "acl: no matching acl entry\n");
1751 else
1752 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
76655d6d
AL
1753 }
1754}
1755
79c4f6b0 1756#if defined(TARGET_I386)
37b7ad48 1757static void do_inject_mce(Monitor *mon, const QDict *qdict)
79c4f6b0
HY
1758{
1759 CPUState *cenv;
37b7ad48
LC
1760 int cpu_index = qdict_get_int(qdict, "cpu_index");
1761 int bank = qdict_get_int(qdict, "bank");
1762 uint64_t status = qdict_get_int(qdict, "status");
1763 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1764 uint64_t addr = qdict_get_int(qdict, "addr");
1765 uint64_t misc = qdict_get_int(qdict, "misc");
79c4f6b0
HY
1766
1767 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1768 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1769 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1770 break;
1771 }
1772}
1773#endif
1774
d54908a5 1775static void do_getfd(Monitor *mon, const QDict *qdict)
f07918fd 1776{
d54908a5 1777 const char *fdname = qdict_get_str(qdict, "fdname");
c227f099 1778 mon_fd_t *monfd;
f07918fd
MM
1779 int fd;
1780
1781 fd = qemu_chr_get_msgfd(mon->chr);
1782 if (fd == -1) {
1783 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1784 return;
1785 }
1786
1787 if (qemu_isdigit(fdname[0])) {
1788 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1789 return;
1790 }
1791
1792 fd = dup(fd);
1793 if (fd == -1) {
1794 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1795 strerror(errno));
1796 return;
1797 }
1798
72cf2d4f 1799 QLIST_FOREACH(monfd, &mon->fds, next) {
f07918fd
MM
1800 if (strcmp(monfd->name, fdname) != 0) {
1801 continue;
1802 }
1803
1804 close(monfd->fd);
1805 monfd->fd = fd;
1806 return;
1807 }
1808
c227f099 1809 monfd = qemu_mallocz(sizeof(mon_fd_t));
f07918fd
MM
1810 monfd->name = qemu_strdup(fdname);
1811 monfd->fd = fd;
1812
72cf2d4f 1813 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
f07918fd
MM
1814}
1815
d54908a5 1816static void do_closefd(Monitor *mon, const QDict *qdict)
f07918fd 1817{
d54908a5 1818 const char *fdname = qdict_get_str(qdict, "fdname");
c227f099 1819 mon_fd_t *monfd;
f07918fd 1820
72cf2d4f 1821 QLIST_FOREACH(monfd, &mon->fds, next) {
f07918fd
MM
1822 if (strcmp(monfd->name, fdname) != 0) {
1823 continue;
1824 }
1825
72cf2d4f 1826 QLIST_REMOVE(monfd, next);
f07918fd
MM
1827 close(monfd->fd);
1828 qemu_free(monfd->name);
1829 qemu_free(monfd);
1830 return;
1831 }
1832
1833 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1834 fdname);
1835}
1836
d54908a5 1837static void do_loadvm(Monitor *mon, const QDict *qdict)
c8d41b2c
JQ
1838{
1839 int saved_vm_running = vm_running;
d54908a5 1840 const char *name = qdict_get_str(qdict, "name");
c8d41b2c
JQ
1841
1842 vm_stop(0);
1843
05f2401e 1844 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
c8d41b2c
JQ
1845 vm_start();
1846}
1847
7768e04c
MM
1848int monitor_get_fd(Monitor *mon, const char *fdname)
1849{
c227f099 1850 mon_fd_t *monfd;
7768e04c 1851
72cf2d4f 1852 QLIST_FOREACH(monfd, &mon->fds, next) {
7768e04c
MM
1853 int fd;
1854
1855 if (strcmp(monfd->name, fdname) != 0) {
1856 continue;
1857 }
1858
1859 fd = monfd->fd;
1860
1861 /* caller takes ownership of fd */
72cf2d4f 1862 QLIST_REMOVE(monfd, next);
7768e04c
MM
1863 qemu_free(monfd->name);
1864 qemu_free(monfd);
1865
1866 return fd;
1867 }
1868
1869 return -1;
1870}
1871
c227f099 1872static const mon_cmd_t mon_cmds[] = {
2313086a 1873#include "qemu-monitor.h"
5fafdf24 1874 { NULL, NULL, },
9dc39cba
FB
1875};
1876
2313086a 1877/* Please update qemu-monitor.hx when adding or changing commands */
c227f099 1878static const mon_cmd_t info_cmds[] = {
d7f9b689
LC
1879 {
1880 .name = "version",
1881 .args_type = "",
d7f9b689
LC
1882 .params = "",
1883 .help = "show the version of QEMU",
ab2d3187
LC
1884 .user_print = monitor_print_qobject,
1885 .mhandler.info_new = do_info_version,
d7f9b689
LC
1886 },
1887 {
1888 .name = "network",
1889 .args_type = "",
d7f9b689
LC
1890 .params = "",
1891 .help = "show the network state",
910df89d 1892 .mhandler.info = do_info_network,
d7f9b689
LC
1893 },
1894 {
1895 .name = "chardev",
1896 .args_type = "",
d7f9b689
LC
1897 .params = "",
1898 .help = "show the character devices",
910df89d 1899 .mhandler.info = qemu_chr_info,
d7f9b689
LC
1900 },
1901 {
1902 .name = "block",
1903 .args_type = "",
d7f9b689
LC
1904 .params = "",
1905 .help = "show the block devices",
910df89d 1906 .mhandler.info = bdrv_info,
d7f9b689
LC
1907 },
1908 {
1909 .name = "blockstats",
1910 .args_type = "",
d7f9b689
LC
1911 .params = "",
1912 .help = "show block device statistics",
910df89d 1913 .mhandler.info = bdrv_info_stats,
d7f9b689
LC
1914 },
1915 {
1916 .name = "registers",
1917 .args_type = "",
d7f9b689
LC
1918 .params = "",
1919 .help = "show the cpu registers",
910df89d 1920 .mhandler.info = do_info_registers,
d7f9b689
LC
1921 },
1922 {
1923 .name = "cpus",
1924 .args_type = "",
d7f9b689
LC
1925 .params = "",
1926 .help = "show infos for each CPU",
910df89d 1927 .mhandler.info = do_info_cpus,
d7f9b689
LC
1928 },
1929 {
1930 .name = "history",
1931 .args_type = "",
d7f9b689
LC
1932 .params = "",
1933 .help = "show the command line history",
910df89d 1934 .mhandler.info = do_info_history,
d7f9b689
LC
1935 },
1936 {
1937 .name = "irq",
1938 .args_type = "",
d7f9b689
LC
1939 .params = "",
1940 .help = "show the interrupts statistics (if available)",
910df89d 1941 .mhandler.info = irq_info,
d7f9b689
LC
1942 },
1943 {
1944 .name = "pic",
1945 .args_type = "",
d7f9b689
LC
1946 .params = "",
1947 .help = "show i8259 (PIC) state",
910df89d 1948 .mhandler.info = pic_info,
d7f9b689
LC
1949 },
1950 {
1951 .name = "pci",
1952 .args_type = "",
d7f9b689
LC
1953 .params = "",
1954 .help = "show PCI info",
910df89d 1955 .mhandler.info = pci_info,
d7f9b689 1956 },
7c664e2f 1957#if defined(TARGET_I386) || defined(TARGET_SH4)
d7f9b689
LC
1958 {
1959 .name = "tlb",
1960 .args_type = "",
d7f9b689
LC
1961 .params = "",
1962 .help = "show virtual to physical memory mappings",
910df89d 1963 .mhandler.info = tlb_info,
d7f9b689 1964 },
7c664e2f
AJ
1965#endif
1966#if defined(TARGET_I386)
d7f9b689
LC
1967 {
1968 .name = "mem",
1969 .args_type = "",
d7f9b689
LC
1970 .params = "",
1971 .help = "show the active virtual memory mappings",
910df89d 1972 .mhandler.info = mem_info,
d7f9b689
LC
1973 },
1974 {
1975 .name = "hpet",
1976 .args_type = "",
d7f9b689
LC
1977 .params = "",
1978 .help = "show state of HPET",
910df89d 1979 .mhandler.info = do_info_hpet,
d7f9b689 1980 },
b86bda5b 1981#endif
d7f9b689
LC
1982 {
1983 .name = "jit",
1984 .args_type = "",
d7f9b689
LC
1985 .params = "",
1986 .help = "show dynamic compiler info",
910df89d 1987 .mhandler.info = do_info_jit,
d7f9b689
LC
1988 },
1989 {
1990 .name = "kvm",
1991 .args_type = "",
d7f9b689
LC
1992 .params = "",
1993 .help = "show KVM information",
910df89d 1994 .mhandler.info = do_info_kvm,
d7f9b689
LC
1995 },
1996 {
1997 .name = "numa",
1998 .args_type = "",
d7f9b689
LC
1999 .params = "",
2000 .help = "show NUMA information",
910df89d 2001 .mhandler.info = do_info_numa,
d7f9b689
LC
2002 },
2003 {
2004 .name = "usb",
2005 .args_type = "",
d7f9b689
LC
2006 .params = "",
2007 .help = "show guest USB devices",
910df89d 2008 .mhandler.info = usb_info,
d7f9b689
LC
2009 },
2010 {
2011 .name = "usbhost",
2012 .args_type = "",
d7f9b689
LC
2013 .params = "",
2014 .help = "show host USB devices",
910df89d 2015 .mhandler.info = usb_host_info,
d7f9b689
LC
2016 },
2017 {
2018 .name = "profile",
2019 .args_type = "",
d7f9b689
LC
2020 .params = "",
2021 .help = "show profiling information",
910df89d 2022 .mhandler.info = do_info_profile,
d7f9b689
LC
2023 },
2024 {
2025 .name = "capture",
2026 .args_type = "",
d7f9b689
LC
2027 .params = "",
2028 .help = "show capture information",
910df89d 2029 .mhandler.info = do_info_capture,
d7f9b689
LC
2030 },
2031 {
2032 .name = "snapshots",
2033 .args_type = "",
d7f9b689
LC
2034 .params = "",
2035 .help = "show the currently saved VM snapshots",
910df89d 2036 .mhandler.info = do_info_snapshots,
d7f9b689
LC
2037 },
2038 {
2039 .name = "status",
2040 .args_type = "",
d7f9b689
LC
2041 .params = "",
2042 .help = "show the current VM status (running|paused)",
910df89d 2043 .mhandler.info = do_info_status,
d7f9b689
LC
2044 },
2045 {
2046 .name = "pcmcia",
2047 .args_type = "",
d7f9b689
LC
2048 .params = "",
2049 .help = "show guest PCMCIA status",
910df89d 2050 .mhandler.info = pcmcia_info,
d7f9b689
LC
2051 },
2052 {
2053 .name = "mice",
2054 .args_type = "",
d7f9b689
LC
2055 .params = "",
2056 .help = "show which guest mouse is receiving events",
910df89d 2057 .mhandler.info = do_info_mice,
d7f9b689
LC
2058 },
2059 {
2060 .name = "vnc",
2061 .args_type = "",
d7f9b689
LC
2062 .params = "",
2063 .help = "show the vnc server status",
910df89d 2064 .mhandler.info = do_info_vnc,
d7f9b689
LC
2065 },
2066 {
2067 .name = "name",
2068 .args_type = "",
d7f9b689
LC
2069 .params = "",
2070 .help = "show the current VM name",
910df89d 2071 .mhandler.info = do_info_name,
d7f9b689
LC
2072 },
2073 {
2074 .name = "uuid",
2075 .args_type = "",
d7f9b689
LC
2076 .params = "",
2077 .help = "show the current VM UUID",
910df89d 2078 .mhandler.info = do_info_uuid,
d7f9b689 2079 },
76a66253 2080#if defined(TARGET_PPC)
d7f9b689
LC
2081 {
2082 .name = "cpustats",
2083 .args_type = "",
d7f9b689
LC
2084 .params = "",
2085 .help = "show CPU statistics",
910df89d 2086 .mhandler.info = do_info_cpu_stats,
d7f9b689 2087 },
76a66253 2088#endif
31a60e22 2089#if defined(CONFIG_SLIRP)
d7f9b689
LC
2090 {
2091 .name = "usernet",
2092 .args_type = "",
d7f9b689
LC
2093 .params = "",
2094 .help = "show user network stack connection states",
910df89d 2095 .mhandler.info = do_info_usernet,
d7f9b689 2096 },
31a60e22 2097#endif
d7f9b689
LC
2098 {
2099 .name = "migrate",
2100 .args_type = "",
d7f9b689
LC
2101 .params = "",
2102 .help = "show migration status",
910df89d 2103 .mhandler.info = do_info_migrate,
d7f9b689
LC
2104 },
2105 {
2106 .name = "balloon",
2107 .args_type = "",
d7f9b689
LC
2108 .params = "",
2109 .help = "show balloon information",
910df89d 2110 .mhandler.info = do_info_balloon,
d7f9b689
LC
2111 },
2112 {
2113 .name = "qtree",
2114 .args_type = "",
d7f9b689
LC
2115 .params = "",
2116 .help = "show device tree",
910df89d 2117 .mhandler.info = do_info_qtree,
d7f9b689
LC
2118 },
2119 {
2120 .name = "qdm",
2121 .args_type = "",
d7f9b689
LC
2122 .params = "",
2123 .help = "show qdev device model list",
910df89d 2124 .mhandler.info = do_info_qdm,
d7f9b689
LC
2125 },
2126 {
2127 .name = "roms",
2128 .args_type = "",
d7f9b689
LC
2129 .params = "",
2130 .help = "show roms",
910df89d 2131 .mhandler.info = do_info_roms,
d7f9b689
LC
2132 },
2133 {
2134 .name = NULL,
2135 },
9dc39cba
FB
2136};
2137
9307c4c1
FB
2138/*******************************************************************/
2139
2140static const char *pch;
2141static jmp_buf expr_env;
2142
92a31b1f
FB
2143#define MD_TLONG 0
2144#define MD_I32 1
2145
9307c4c1
FB
2146typedef struct MonitorDef {
2147 const char *name;
2148 int offset;
8662d656 2149 target_long (*get_value)(const struct MonitorDef *md, int val);
92a31b1f 2150 int type;
9307c4c1
FB
2151} MonitorDef;
2152
57206fd4 2153#if defined(TARGET_I386)
8662d656 2154static target_long monitor_get_pc (const struct MonitorDef *md, int val)
57206fd4 2155{
6a00d601
FB
2156 CPUState *env = mon_get_cpu();
2157 if (!env)
2158 return 0;
2159 return env->eip + env->segs[R_CS].base;
57206fd4
FB
2160}
2161#endif
2162
a541f297 2163#if defined(TARGET_PPC)
8662d656 2164static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
a541f297 2165{
6a00d601 2166 CPUState *env = mon_get_cpu();
a541f297
FB
2167 unsigned int u;
2168 int i;
2169
6a00d601
FB
2170 if (!env)
2171 return 0;
2172
a541f297
FB
2173 u = 0;
2174 for (i = 0; i < 8; i++)
28a76be8 2175 u |= env->crf[i] << (32 - (4 * i));
a541f297
FB
2176
2177 return u;
2178}
2179
8662d656 2180static target_long monitor_get_msr (const struct MonitorDef *md, int val)
a541f297 2181{
6a00d601
FB
2182 CPUState *env = mon_get_cpu();
2183 if (!env)
2184 return 0;
0411a972 2185 return env->msr;
a541f297
FB
2186}
2187
8662d656 2188static target_long monitor_get_xer (const struct MonitorDef *md, int val)
a541f297 2189{
6a00d601
FB
2190 CPUState *env = mon_get_cpu();
2191 if (!env)
2192 return 0;
3d7b417e 2193 return env->xer;
a541f297 2194}
9fddaa0c 2195
8662d656 2196static target_long monitor_get_decr (const struct MonitorDef *md, int val)
9fddaa0c 2197{
6a00d601
FB
2198 CPUState *env = mon_get_cpu();
2199 if (!env)
2200 return 0;
2201 return cpu_ppc_load_decr(env);
9fddaa0c
FB
2202}
2203
8662d656 2204static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
9fddaa0c 2205{
6a00d601
FB
2206 CPUState *env = mon_get_cpu();
2207 if (!env)
2208 return 0;
2209 return cpu_ppc_load_tbu(env);
9fddaa0c
FB
2210}
2211
8662d656 2212static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
9fddaa0c 2213{
6a00d601
FB
2214 CPUState *env = mon_get_cpu();
2215 if (!env)
2216 return 0;
2217 return cpu_ppc_load_tbl(env);
9fddaa0c 2218}
a541f297
FB
2219#endif
2220
e95c8d51 2221#if defined(TARGET_SPARC)
7b936c0c 2222#ifndef TARGET_SPARC64
8662d656 2223static target_long monitor_get_psr (const struct MonitorDef *md, int val)
e95c8d51 2224{
6a00d601
FB
2225 CPUState *env = mon_get_cpu();
2226 if (!env)
2227 return 0;
2228 return GET_PSR(env);
e95c8d51 2229}
7b936c0c 2230#endif
e95c8d51 2231
8662d656 2232static target_long monitor_get_reg(const struct MonitorDef *md, int val)
e95c8d51 2233{
6a00d601
FB
2234 CPUState *env = mon_get_cpu();
2235 if (!env)
2236 return 0;
2237 return env->regwptr[val];
e95c8d51
FB
2238}
2239#endif
2240
8662d656 2241static const MonitorDef monitor_defs[] = {
9307c4c1 2242#ifdef TARGET_I386
57206fd4
FB
2243
2244#define SEG(name, seg) \
92a31b1f 2245 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
57206fd4 2246 { name ".base", offsetof(CPUState, segs[seg].base) },\
92a31b1f 2247 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
57206fd4 2248
9307c4c1
FB
2249 { "eax", offsetof(CPUState, regs[0]) },
2250 { "ecx", offsetof(CPUState, regs[1]) },
2251 { "edx", offsetof(CPUState, regs[2]) },
2252 { "ebx", offsetof(CPUState, regs[3]) },
2253 { "esp|sp", offsetof(CPUState, regs[4]) },
2254 { "ebp|fp", offsetof(CPUState, regs[5]) },
2255 { "esi", offsetof(CPUState, regs[6]) },
01038d2a 2256 { "edi", offsetof(CPUState, regs[7]) },
92a31b1f
FB
2257#ifdef TARGET_X86_64
2258 { "r8", offsetof(CPUState, regs[8]) },
2259 { "r9", offsetof(CPUState, regs[9]) },
2260 { "r10", offsetof(CPUState, regs[10]) },
2261 { "r11", offsetof(CPUState, regs[11]) },
2262 { "r12", offsetof(CPUState, regs[12]) },
2263 { "r13", offsetof(CPUState, regs[13]) },
2264 { "r14", offsetof(CPUState, regs[14]) },
2265 { "r15", offsetof(CPUState, regs[15]) },
2266#endif
9307c4c1 2267 { "eflags", offsetof(CPUState, eflags) },
57206fd4
FB
2268 { "eip", offsetof(CPUState, eip) },
2269 SEG("cs", R_CS)
2270 SEG("ds", R_DS)
2271 SEG("es", R_ES)
01038d2a 2272 SEG("ss", R_SS)
57206fd4
FB
2273 SEG("fs", R_FS)
2274 SEG("gs", R_GS)
2275 { "pc", 0, monitor_get_pc, },
a541f297 2276#elif defined(TARGET_PPC)
ff937dba 2277 /* General purpose registers */
a541f297
FB
2278 { "r0", offsetof(CPUState, gpr[0]) },
2279 { "r1", offsetof(CPUState, gpr[1]) },
2280 { "r2", offsetof(CPUState, gpr[2]) },
2281 { "r3", offsetof(CPUState, gpr[3]) },
2282 { "r4", offsetof(CPUState, gpr[4]) },
2283 { "r5", offsetof(CPUState, gpr[5]) },
2284 { "r6", offsetof(CPUState, gpr[6]) },
2285 { "r7", offsetof(CPUState, gpr[7]) },
2286 { "r8", offsetof(CPUState, gpr[8]) },
2287 { "r9", offsetof(CPUState, gpr[9]) },
2288 { "r10", offsetof(CPUState, gpr[10]) },
2289 { "r11", offsetof(CPUState, gpr[11]) },
2290 { "r12", offsetof(CPUState, gpr[12]) },
2291 { "r13", offsetof(CPUState, gpr[13]) },
2292 { "r14", offsetof(CPUState, gpr[14]) },
2293 { "r15", offsetof(CPUState, gpr[15]) },
2294 { "r16", offsetof(CPUState, gpr[16]) },
2295 { "r17", offsetof(CPUState, gpr[17]) },
2296 { "r18", offsetof(CPUState, gpr[18]) },
2297 { "r19", offsetof(CPUState, gpr[19]) },
2298 { "r20", offsetof(CPUState, gpr[20]) },
2299 { "r21", offsetof(CPUState, gpr[21]) },
2300 { "r22", offsetof(CPUState, gpr[22]) },
2301 { "r23", offsetof(CPUState, gpr[23]) },
2302 { "r24", offsetof(CPUState, gpr[24]) },
2303 { "r25", offsetof(CPUState, gpr[25]) },
2304 { "r26", offsetof(CPUState, gpr[26]) },
2305 { "r27", offsetof(CPUState, gpr[27]) },
2306 { "r28", offsetof(CPUState, gpr[28]) },
2307 { "r29", offsetof(CPUState, gpr[29]) },
2308 { "r30", offsetof(CPUState, gpr[30]) },
2309 { "r31", offsetof(CPUState, gpr[31]) },
ff937dba
JM
2310 /* Floating point registers */
2311 { "f0", offsetof(CPUState, fpr[0]) },
2312 { "f1", offsetof(CPUState, fpr[1]) },
2313 { "f2", offsetof(CPUState, fpr[2]) },
2314 { "f3", offsetof(CPUState, fpr[3]) },
2315 { "f4", offsetof(CPUState, fpr[4]) },
2316 { "f5", offsetof(CPUState, fpr[5]) },
2317 { "f6", offsetof(CPUState, fpr[6]) },
2318 { "f7", offsetof(CPUState, fpr[7]) },
2319 { "f8", offsetof(CPUState, fpr[8]) },
2320 { "f9", offsetof(CPUState, fpr[9]) },
2321 { "f10", offsetof(CPUState, fpr[10]) },
2322 { "f11", offsetof(CPUState, fpr[11]) },
2323 { "f12", offsetof(CPUState, fpr[12]) },
2324 { "f13", offsetof(CPUState, fpr[13]) },
2325 { "f14", offsetof(CPUState, fpr[14]) },
2326 { "f15", offsetof(CPUState, fpr[15]) },
2327 { "f16", offsetof(CPUState, fpr[16]) },
2328 { "f17", offsetof(CPUState, fpr[17]) },
2329 { "f18", offsetof(CPUState, fpr[18]) },
2330 { "f19", offsetof(CPUState, fpr[19]) },
2331 { "f20", offsetof(CPUState, fpr[20]) },
2332 { "f21", offsetof(CPUState, fpr[21]) },
2333 { "f22", offsetof(CPUState, fpr[22]) },
2334 { "f23", offsetof(CPUState, fpr[23]) },
2335 { "f24", offsetof(CPUState, fpr[24]) },
2336 { "f25", offsetof(CPUState, fpr[25]) },
2337 { "f26", offsetof(CPUState, fpr[26]) },
2338 { "f27", offsetof(CPUState, fpr[27]) },
2339 { "f28", offsetof(CPUState, fpr[28]) },
2340 { "f29", offsetof(CPUState, fpr[29]) },
2341 { "f30", offsetof(CPUState, fpr[30]) },
2342 { "f31", offsetof(CPUState, fpr[31]) },
2343 { "fpscr", offsetof(CPUState, fpscr) },
2344 /* Next instruction pointer */
57206fd4 2345 { "nip|pc", offsetof(CPUState, nip) },
a541f297
FB
2346 { "lr", offsetof(CPUState, lr) },
2347 { "ctr", offsetof(CPUState, ctr) },
9fddaa0c 2348 { "decr", 0, &monitor_get_decr, },
a541f297 2349 { "ccr", 0, &monitor_get_ccr, },
ff937dba 2350 /* Machine state register */
a541f297
FB
2351 { "msr", 0, &monitor_get_msr, },
2352 { "xer", 0, &monitor_get_xer, },
9fddaa0c
FB
2353 { "tbu", 0, &monitor_get_tbu, },
2354 { "tbl", 0, &monitor_get_tbl, },
ff937dba
JM
2355#if defined(TARGET_PPC64)
2356 /* Address space register */
2357 { "asr", offsetof(CPUState, asr) },
2358#endif
2359 /* Segment registers */
a541f297
FB
2360 { "sdr1", offsetof(CPUState, sdr1) },
2361 { "sr0", offsetof(CPUState, sr[0]) },
2362 { "sr1", offsetof(CPUState, sr[1]) },
2363 { "sr2", offsetof(CPUState, sr[2]) },
2364 { "sr3", offsetof(CPUState, sr[3]) },
2365 { "sr4", offsetof(CPUState, sr[4]) },
2366 { "sr5", offsetof(CPUState, sr[5]) },
2367 { "sr6", offsetof(CPUState, sr[6]) },
2368 { "sr7", offsetof(CPUState, sr[7]) },
2369 { "sr8", offsetof(CPUState, sr[8]) },
2370 { "sr9", offsetof(CPUState, sr[9]) },
2371 { "sr10", offsetof(CPUState, sr[10]) },
2372 { "sr11", offsetof(CPUState, sr[11]) },
2373 { "sr12", offsetof(CPUState, sr[12]) },
2374 { "sr13", offsetof(CPUState, sr[13]) },
2375 { "sr14", offsetof(CPUState, sr[14]) },
2376 { "sr15", offsetof(CPUState, sr[15]) },
2377 /* Too lazy to put BATs and SPRs ... */
e95c8d51
FB
2378#elif defined(TARGET_SPARC)
2379 { "g0", offsetof(CPUState, gregs[0]) },
2380 { "g1", offsetof(CPUState, gregs[1]) },
2381 { "g2", offsetof(CPUState, gregs[2]) },
2382 { "g3", offsetof(CPUState, gregs[3]) },
2383 { "g4", offsetof(CPUState, gregs[4]) },
2384 { "g5", offsetof(CPUState, gregs[5]) },
2385 { "g6", offsetof(CPUState, gregs[6]) },
2386 { "g7", offsetof(CPUState, gregs[7]) },
2387 { "o0", 0, monitor_get_reg },
2388 { "o1", 1, monitor_get_reg },
2389 { "o2", 2, monitor_get_reg },
2390 { "o3", 3, monitor_get_reg },
2391 { "o4", 4, monitor_get_reg },
2392 { "o5", 5, monitor_get_reg },
2393 { "o6", 6, monitor_get_reg },
2394 { "o7", 7, monitor_get_reg },
2395 { "l0", 8, monitor_get_reg },
2396 { "l1", 9, monitor_get_reg },
2397 { "l2", 10, monitor_get_reg },
2398 { "l3", 11, monitor_get_reg },
2399 { "l4", 12, monitor_get_reg },
2400 { "l5", 13, monitor_get_reg },
2401 { "l6", 14, monitor_get_reg },
2402 { "l7", 15, monitor_get_reg },
2403 { "i0", 16, monitor_get_reg },
2404 { "i1", 17, monitor_get_reg },
2405 { "i2", 18, monitor_get_reg },
2406 { "i3", 19, monitor_get_reg },
2407 { "i4", 20, monitor_get_reg },
2408 { "i5", 21, monitor_get_reg },
2409 { "i6", 22, monitor_get_reg },
2410 { "i7", 23, monitor_get_reg },
2411 { "pc", offsetof(CPUState, pc) },
2412 { "npc", offsetof(CPUState, npc) },
2413 { "y", offsetof(CPUState, y) },
7b936c0c 2414#ifndef TARGET_SPARC64
e95c8d51
FB
2415 { "psr", 0, &monitor_get_psr, },
2416 { "wim", offsetof(CPUState, wim) },
7b936c0c 2417#endif
e95c8d51
FB
2418 { "tbr", offsetof(CPUState, tbr) },
2419 { "fsr", offsetof(CPUState, fsr) },
2420 { "f0", offsetof(CPUState, fpr[0]) },
2421 { "f1", offsetof(CPUState, fpr[1]) },
2422 { "f2", offsetof(CPUState, fpr[2]) },
2423 { "f3", offsetof(CPUState, fpr[3]) },
2424 { "f4", offsetof(CPUState, fpr[4]) },
2425 { "f5", offsetof(CPUState, fpr[5]) },
2426 { "f6", offsetof(CPUState, fpr[6]) },
2427 { "f7", offsetof(CPUState, fpr[7]) },
2428 { "f8", offsetof(CPUState, fpr[8]) },
2429 { "f9", offsetof(CPUState, fpr[9]) },
2430 { "f10", offsetof(CPUState, fpr[10]) },
2431 { "f11", offsetof(CPUState, fpr[11]) },
2432 { "f12", offsetof(CPUState, fpr[12]) },
2433 { "f13", offsetof(CPUState, fpr[13]) },
2434 { "f14", offsetof(CPUState, fpr[14]) },
2435 { "f15", offsetof(CPUState, fpr[15]) },
2436 { "f16", offsetof(CPUState, fpr[16]) },
2437 { "f17", offsetof(CPUState, fpr[17]) },
2438 { "f18", offsetof(CPUState, fpr[18]) },
2439 { "f19", offsetof(CPUState, fpr[19]) },
2440 { "f20", offsetof(CPUState, fpr[20]) },
2441 { "f21", offsetof(CPUState, fpr[21]) },
2442 { "f22", offsetof(CPUState, fpr[22]) },
2443 { "f23", offsetof(CPUState, fpr[23]) },
2444 { "f24", offsetof(CPUState, fpr[24]) },
2445 { "f25", offsetof(CPUState, fpr[25]) },
2446 { "f26", offsetof(CPUState, fpr[26]) },
2447 { "f27", offsetof(CPUState, fpr[27]) },
2448 { "f28", offsetof(CPUState, fpr[28]) },
2449 { "f29", offsetof(CPUState, fpr[29]) },
2450 { "f30", offsetof(CPUState, fpr[30]) },
2451 { "f31", offsetof(CPUState, fpr[31]) },
7b936c0c
FB
2452#ifdef TARGET_SPARC64
2453 { "f32", offsetof(CPUState, fpr[32]) },
2454 { "f34", offsetof(CPUState, fpr[34]) },
2455 { "f36", offsetof(CPUState, fpr[36]) },
2456 { "f38", offsetof(CPUState, fpr[38]) },
2457 { "f40", offsetof(CPUState, fpr[40]) },
2458 { "f42", offsetof(CPUState, fpr[42]) },
2459 { "f44", offsetof(CPUState, fpr[44]) },
2460 { "f46", offsetof(CPUState, fpr[46]) },
2461 { "f48", offsetof(CPUState, fpr[48]) },
2462 { "f50", offsetof(CPUState, fpr[50]) },
2463 { "f52", offsetof(CPUState, fpr[52]) },
2464 { "f54", offsetof(CPUState, fpr[54]) },
2465 { "f56", offsetof(CPUState, fpr[56]) },
2466 { "f58", offsetof(CPUState, fpr[58]) },
2467 { "f60", offsetof(CPUState, fpr[60]) },
2468 { "f62", offsetof(CPUState, fpr[62]) },
2469 { "asi", offsetof(CPUState, asi) },
2470 { "pstate", offsetof(CPUState, pstate) },
2471 { "cansave", offsetof(CPUState, cansave) },
2472 { "canrestore", offsetof(CPUState, canrestore) },
2473 { "otherwin", offsetof(CPUState, otherwin) },
2474 { "wstate", offsetof(CPUState, wstate) },
2475 { "cleanwin", offsetof(CPUState, cleanwin) },
2476 { "fprs", offsetof(CPUState, fprs) },
2477#endif
9307c4c1
FB
2478#endif
2479 { NULL },
2480};
2481
376253ec 2482static void expr_error(Monitor *mon, const char *msg)
9dc39cba 2483{
376253ec 2484 monitor_printf(mon, "%s\n", msg);
9307c4c1
FB
2485 longjmp(expr_env, 1);
2486}
2487
6a00d601 2488/* return 0 if OK, -1 if not found, -2 if no CPU defined */
92a31b1f 2489static int get_monitor_def(target_long *pval, const char *name)
9307c4c1 2490{
8662d656 2491 const MonitorDef *md;
92a31b1f
FB
2492 void *ptr;
2493
9307c4c1
FB
2494 for(md = monitor_defs; md->name != NULL; md++) {
2495 if (compare_cmd(name, md->name)) {
2496 if (md->get_value) {
e95c8d51 2497 *pval = md->get_value(md, md->offset);
9307c4c1 2498 } else {
6a00d601
FB
2499 CPUState *env = mon_get_cpu();
2500 if (!env)
2501 return -2;
2502 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
2503 switch(md->type) {
2504 case MD_I32:
2505 *pval = *(int32_t *)ptr;
2506 break;
2507 case MD_TLONG:
2508 *pval = *(target_long *)ptr;
2509 break;
2510 default:
2511 *pval = 0;
2512 break;
2513 }
9307c4c1
FB
2514 }
2515 return 0;
2516 }
2517 }
2518 return -1;
2519}
2520
2521static void next(void)
2522{
660f11be 2523 if (*pch != '\0') {
9307c4c1 2524 pch++;
cd390083 2525 while (qemu_isspace(*pch))
9307c4c1
FB
2526 pch++;
2527 }
2528}
2529
376253ec 2530static int64_t expr_sum(Monitor *mon);
9307c4c1 2531
376253ec 2532static int64_t expr_unary(Monitor *mon)
9307c4c1 2533{
c2efc95d 2534 int64_t n;
9307c4c1 2535 char *p;
6a00d601 2536 int ret;
9307c4c1
FB
2537
2538 switch(*pch) {
2539 case '+':
2540 next();
376253ec 2541 n = expr_unary(mon);
9307c4c1
FB
2542 break;
2543 case '-':
2544 next();
376253ec 2545 n = -expr_unary(mon);
9307c4c1
FB
2546 break;
2547 case '~':
2548 next();
376253ec 2549 n = ~expr_unary(mon);
9307c4c1
FB
2550 break;
2551 case '(':
2552 next();
376253ec 2553 n = expr_sum(mon);
9307c4c1 2554 if (*pch != ')') {
376253ec 2555 expr_error(mon, "')' expected");
9307c4c1
FB
2556 }
2557 next();
2558 break;
81d0912d
FB
2559 case '\'':
2560 pch++;
2561 if (*pch == '\0')
376253ec 2562 expr_error(mon, "character constant expected");
81d0912d
FB
2563 n = *pch;
2564 pch++;
2565 if (*pch != '\'')
376253ec 2566 expr_error(mon, "missing terminating \' character");
81d0912d
FB
2567 next();
2568 break;
9307c4c1
FB
2569 case '$':
2570 {
2571 char buf[128], *q;
69b34976 2572 target_long reg=0;
3b46e624 2573
9307c4c1
FB
2574 pch++;
2575 q = buf;
2576 while ((*pch >= 'a' && *pch <= 'z') ||
2577 (*pch >= 'A' && *pch <= 'Z') ||
2578 (*pch >= '0' && *pch <= '9') ||
57206fd4 2579 *pch == '_' || *pch == '.') {
9307c4c1
FB
2580 if ((q - buf) < sizeof(buf) - 1)
2581 *q++ = *pch;
2582 pch++;
2583 }
cd390083 2584 while (qemu_isspace(*pch))
9307c4c1
FB
2585 pch++;
2586 *q = 0;
7743e588 2587 ret = get_monitor_def(&reg, buf);
6a00d601 2588 if (ret == -1)
376253ec 2589 expr_error(mon, "unknown register");
5fafdf24 2590 else if (ret == -2)
376253ec 2591 expr_error(mon, "no cpu defined");
7743e588 2592 n = reg;
9307c4c1
FB
2593 }
2594 break;
2595 case '\0':
376253ec 2596 expr_error(mon, "unexpected end of expression");
9307c4c1
FB
2597 n = 0;
2598 break;
2599 default:
7743e588 2600#if TARGET_PHYS_ADDR_BITS > 32
4f4fbf77
FB
2601 n = strtoull(pch, &p, 0);
2602#else
9307c4c1 2603 n = strtoul(pch, &p, 0);
4f4fbf77 2604#endif
9307c4c1 2605 if (pch == p) {
376253ec 2606 expr_error(mon, "invalid char in expression");
9307c4c1
FB
2607 }
2608 pch = p;
cd390083 2609 while (qemu_isspace(*pch))
9307c4c1
FB
2610 pch++;
2611 break;
2612 }
2613 return n;
2614}
2615
2616
376253ec 2617static int64_t expr_prod(Monitor *mon)
9307c4c1 2618{
c2efc95d 2619 int64_t val, val2;
92a31b1f 2620 int op;
3b46e624 2621
376253ec 2622 val = expr_unary(mon);
9307c4c1
FB
2623 for(;;) {
2624 op = *pch;
2625 if (op != '*' && op != '/' && op != '%')
2626 break;
2627 next();
376253ec 2628 val2 = expr_unary(mon);
9307c4c1
FB
2629 switch(op) {
2630 default:
2631 case '*':
2632 val *= val2;
2633 break;
2634 case '/':
2635 case '%':
5fafdf24 2636 if (val2 == 0)
376253ec 2637 expr_error(mon, "division by zero");
9307c4c1
FB
2638 if (op == '/')
2639 val /= val2;
2640 else
2641 val %= val2;
2642 break;
2643 }
2644 }
2645 return val;
2646}
2647
376253ec 2648static int64_t expr_logic(Monitor *mon)
9307c4c1 2649{
c2efc95d 2650 int64_t val, val2;
92a31b1f 2651 int op;
9307c4c1 2652
376253ec 2653 val = expr_prod(mon);
9307c4c1
FB
2654 for(;;) {
2655 op = *pch;
2656 if (op != '&' && op != '|' && op != '^')
2657 break;
2658 next();
376253ec 2659 val2 = expr_prod(mon);
9307c4c1
FB
2660 switch(op) {
2661 default:
2662 case '&':
2663 val &= val2;
2664 break;
2665 case '|':
2666 val |= val2;
2667 break;
2668 case '^':
2669 val ^= val2;
2670 break;
2671 }
2672 }
2673 return val;
2674}
2675
376253ec 2676static int64_t expr_sum(Monitor *mon)
9307c4c1 2677{
c2efc95d 2678 int64_t val, val2;
92a31b1f 2679 int op;
9307c4c1 2680
376253ec 2681 val = expr_logic(mon);
9307c4c1
FB
2682 for(;;) {
2683 op = *pch;
2684 if (op != '+' && op != '-')
2685 break;
2686 next();
376253ec 2687 val2 = expr_logic(mon);
9307c4c1
FB
2688 if (op == '+')
2689 val += val2;
2690 else
2691 val -= val2;
2692 }
2693 return val;
2694}
2695
376253ec 2696static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
9307c4c1
FB
2697{
2698 pch = *pp;
2699 if (setjmp(expr_env)) {
2700 *pp = pch;
2701 return -1;
2702 }
cd390083 2703 while (qemu_isspace(*pch))
9307c4c1 2704 pch++;
376253ec 2705 *pval = expr_sum(mon);
9307c4c1
FB
2706 *pp = pch;
2707 return 0;
2708}
2709
2710static int get_str(char *buf, int buf_size, const char **pp)
2711{
2712 const char *p;
2713 char *q;
2714 int c;
2715
81d0912d 2716 q = buf;
9307c4c1 2717 p = *pp;
cd390083 2718 while (qemu_isspace(*p))
9307c4c1
FB
2719 p++;
2720 if (*p == '\0') {
2721 fail:
81d0912d 2722 *q = '\0';
9307c4c1
FB
2723 *pp = p;
2724 return -1;
2725 }
9307c4c1
FB
2726 if (*p == '\"') {
2727 p++;
2728 while (*p != '\0' && *p != '\"') {
2729 if (*p == '\\') {
2730 p++;
2731 c = *p++;
2732 switch(c) {
2733 case 'n':
2734 c = '\n';
2735 break;
2736 case 'r':
2737 c = '\r';
2738 break;
2739 case '\\':
2740 case '\'':
2741 case '\"':
2742 break;
2743 default:
2744 qemu_printf("unsupported escape code: '\\%c'\n", c);
2745 goto fail;
2746 }
2747 if ((q - buf) < buf_size - 1) {
2748 *q++ = c;
2749 }
2750 } else {
2751 if ((q - buf) < buf_size - 1) {
2752 *q++ = *p;
2753 }
2754 p++;
2755 }
2756 }
2757 if (*p != '\"') {
5b60212f 2758 qemu_printf("unterminated string\n");
9307c4c1
FB
2759 goto fail;
2760 }
2761 p++;
2762 } else {
cd390083 2763 while (*p != '\0' && !qemu_isspace(*p)) {
9307c4c1
FB
2764 if ((q - buf) < buf_size - 1) {
2765 *q++ = *p;
2766 }
2767 p++;
2768 }
9307c4c1 2769 }
81d0912d 2770 *q = '\0';
9307c4c1
FB
2771 *pp = p;
2772 return 0;
2773}
2774
4590fd80
LC
2775/*
2776 * Store the command-name in cmdname, and return a pointer to
2777 * the remaining of the command string.
2778 */
2779static const char *get_command_name(const char *cmdline,
2780 char *cmdname, size_t nlen)
2781{
2782 size_t len;
2783 const char *p, *pstart;
2784
2785 p = cmdline;
2786 while (qemu_isspace(*p))
2787 p++;
2788 if (*p == '\0')
2789 return NULL;
2790 pstart = p;
2791 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2792 p++;
2793 len = p - pstart;
2794 if (len > nlen - 1)
2795 len = nlen - 1;
2796 memcpy(cmdname, pstart, len);
2797 cmdname[len] = '\0';
2798 return p;
2799}
2800
4d76d2ba
LC
2801/**
2802 * Read key of 'type' into 'key' and return the current
2803 * 'type' pointer.
2804 */
2805static char *key_get_info(const char *type, char **key)
2806{
2807 size_t len;
2808 char *p, *str;
2809
2810 if (*type == ',')
2811 type++;
2812
2813 p = strchr(type, ':');
2814 if (!p) {
2815 *key = NULL;
2816 return NULL;
2817 }
2818 len = p - type;
2819
2820 str = qemu_malloc(len + 1);
2821 memcpy(str, type, len);
2822 str[len] = '\0';
2823
2824 *key = str;
2825 return ++p;
2826}
2827
9307c4c1
FB
2828static int default_fmt_format = 'x';
2829static int default_fmt_size = 4;
2830
2831#define MAX_ARGS 16
2832
c227f099 2833static const mon_cmd_t *monitor_parse_command(Monitor *mon,
55f81d96 2834 const char *cmdline,
55f81d96 2835 QDict *qdict)
9307c4c1 2836{
4590fd80 2837 const char *p, *typestr;
53773581 2838 int c;
c227f099 2839 const mon_cmd_t *cmd;
9307c4c1
FB
2840 char cmdname[256];
2841 char buf[1024];
4d76d2ba 2842 char *key;
9dc39cba
FB
2843
2844#ifdef DEBUG
376253ec 2845 monitor_printf(mon, "command='%s'\n", cmdline);
9dc39cba 2846#endif
3b46e624 2847
9307c4c1 2848 /* extract the command name */
4590fd80
LC
2849 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2850 if (!p)
55f81d96 2851 return NULL;
3b46e624 2852
9307c4c1 2853 /* find the command */
376253ec 2854 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
5fafdf24 2855 if (compare_cmd(cmdname, cmd->name))
d91d9bf6
LC
2856 break;
2857 }
2858
2859 if (cmd->name == NULL) {
2860 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
55f81d96 2861 return NULL;
9307c4c1 2862 }
9307c4c1 2863
9307c4c1
FB
2864 /* parse the parameters */
2865 typestr = cmd->args_type;
9dc39cba 2866 for(;;) {
4d76d2ba
LC
2867 typestr = key_get_info(typestr, &key);
2868 if (!typestr)
9dc39cba 2869 break;
4d76d2ba 2870 c = *typestr;
9307c4c1
FB
2871 typestr++;
2872 switch(c) {
2873 case 'F':
81d0912d 2874 case 'B':
9307c4c1
FB
2875 case 's':
2876 {
2877 int ret;
3b46e624 2878
cd390083 2879 while (qemu_isspace(*p))
9307c4c1
FB
2880 p++;
2881 if (*typestr == '?') {
2882 typestr++;
2883 if (*p == '\0') {
2884 /* no optional string: NULL argument */
53773581 2885 break;
9307c4c1
FB
2886 }
2887 }
2888 ret = get_str(buf, sizeof(buf), &p);
2889 if (ret < 0) {
81d0912d
FB
2890 switch(c) {
2891 case 'F':
376253ec
AL
2892 monitor_printf(mon, "%s: filename expected\n",
2893 cmdname);
81d0912d
FB
2894 break;
2895 case 'B':
376253ec
AL
2896 monitor_printf(mon, "%s: block device name expected\n",
2897 cmdname);
81d0912d
FB
2898 break;
2899 default:
376253ec 2900 monitor_printf(mon, "%s: string expected\n", cmdname);
81d0912d
FB
2901 break;
2902 }
9307c4c1
FB
2903 goto fail;
2904 }
53773581 2905 qdict_put(qdict, key, qstring_from_str(buf));
9307c4c1 2906 }
9dc39cba 2907 break;
9307c4c1
FB
2908 case '/':
2909 {
2910 int count, format, size;
3b46e624 2911
cd390083 2912 while (qemu_isspace(*p))
9307c4c1
FB
2913 p++;
2914 if (*p == '/') {
2915 /* format found */
2916 p++;
2917 count = 1;
cd390083 2918 if (qemu_isdigit(*p)) {
9307c4c1 2919 count = 0;
cd390083 2920 while (qemu_isdigit(*p)) {
9307c4c1
FB
2921 count = count * 10 + (*p - '0');
2922 p++;
2923 }
2924 }
2925 size = -1;
2926 format = -1;
2927 for(;;) {
2928 switch(*p) {
2929 case 'o':
2930 case 'd':
2931 case 'u':
2932 case 'x':
2933 case 'i':
2934 case 'c':
2935 format = *p++;
2936 break;
2937 case 'b':
2938 size = 1;
2939 p++;
2940 break;
2941 case 'h':
2942 size = 2;
2943 p++;
2944 break;
2945 case 'w':
2946 size = 4;
2947 p++;
2948 break;
2949 case 'g':
2950 case 'L':
2951 size = 8;
2952 p++;
2953 break;
2954 default:
2955 goto next;
2956 }
2957 }
2958 next:
cd390083 2959 if (*p != '\0' && !qemu_isspace(*p)) {
376253ec
AL
2960 monitor_printf(mon, "invalid char in format: '%c'\n",
2961 *p);
9307c4c1
FB
2962 goto fail;
2963 }
9307c4c1
FB
2964 if (format < 0)
2965 format = default_fmt_format;
4c27ba27
FB
2966 if (format != 'i') {
2967 /* for 'i', not specifying a size gives -1 as size */
2968 if (size < 0)
2969 size = default_fmt_size;
e90f009b 2970 default_fmt_size = size;
4c27ba27 2971 }
9307c4c1
FB
2972 default_fmt_format = format;
2973 } else {
2974 count = 1;
2975 format = default_fmt_format;
4c27ba27
FB
2976 if (format != 'i') {
2977 size = default_fmt_size;
2978 } else {
2979 size = -1;
2980 }
9307c4c1 2981 }
f7188bbe
LC
2982 qdict_put(qdict, "count", qint_from_int(count));
2983 qdict_put(qdict, "format", qint_from_int(format));
2984 qdict_put(qdict, "size", qint_from_int(size));
9307c4c1 2985 }
9dc39cba 2986 break;
9307c4c1 2987 case 'i':
92a31b1f 2988 case 'l':
9307c4c1 2989 {
c2efc95d 2990 int64_t val;
7743e588 2991
cd390083 2992 while (qemu_isspace(*p))
9307c4c1 2993 p++;
3440557b 2994 if (*typestr == '?' || *typestr == '.') {
3440557b 2995 if (*typestr == '?') {
53773581
LC
2996 if (*p == '\0') {
2997 typestr++;
2998 break;
2999 }
3440557b
FB
3000 } else {
3001 if (*p == '.') {
3002 p++;
cd390083 3003 while (qemu_isspace(*p))
3440557b 3004 p++;
3440557b 3005 } else {
53773581
LC
3006 typestr++;
3007 break;
3440557b
FB
3008 }
3009 }
13224a87 3010 typestr++;
9307c4c1 3011 }
376253ec 3012 if (get_expr(mon, &val, &p))
9307c4c1 3013 goto fail;
675ebef9
LC
3014 /* Check if 'i' is greater than 32-bit */
3015 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3016 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3017 monitor_printf(mon, "integer is for 32-bit values\n");
3018 goto fail;
3019 }
53773581 3020 qdict_put(qdict, key, qint_from_int(val));
9307c4c1
FB
3021 }
3022 break;
3023 case '-':
3024 {
3025 int has_option;
3026 /* option */
3b46e624 3027
9307c4c1
FB
3028 c = *typestr++;
3029 if (c == '\0')
3030 goto bad_type;
cd390083 3031 while (qemu_isspace(*p))
9307c4c1
FB
3032 p++;
3033 has_option = 0;
3034 if (*p == '-') {
3035 p++;
3036 if (*p != c) {
376253ec
AL
3037 monitor_printf(mon, "%s: unsupported option -%c\n",
3038 cmdname, *p);
9307c4c1
FB
3039 goto fail;
3040 }
3041 p++;
3042 has_option = 1;
3043 }
f7188bbe 3044 qdict_put(qdict, key, qint_from_int(has_option));
9307c4c1
FB
3045 }
3046 break;
3047 default:
3048 bad_type:
376253ec 3049 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
9307c4c1
FB
3050 goto fail;
3051 }
4d76d2ba
LC
3052 qemu_free(key);
3053 key = NULL;
9dc39cba 3054 }
9307c4c1 3055 /* check that all arguments were parsed */
cd390083 3056 while (qemu_isspace(*p))
9307c4c1
FB
3057 p++;
3058 if (*p != '\0') {
376253ec
AL
3059 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3060 cmdname);
9307c4c1 3061 goto fail;
9dc39cba 3062 }
9307c4c1 3063
55f81d96 3064 return cmd;
ac7531ec 3065
55f81d96 3066fail:
4d76d2ba 3067 qemu_free(key);
55f81d96
LC
3068 return NULL;
3069}
3070
3071static void monitor_handle_command(Monitor *mon, const char *cmdline)
3072{
55f81d96 3073 QDict *qdict;
c227f099 3074 const mon_cmd_t *cmd;
55f81d96
LC
3075
3076 qdict = qdict_new();
3077
590fb3b7 3078 cmd = monitor_parse_command(mon, cmdline, qdict);
13917bee
LC
3079 if (!cmd)
3080 goto out;
3081
3082 qemu_errors_to_mon(mon);
3083
3084 if (monitor_handler_ported(cmd)) {
3085 QObject *data = NULL;
3086
3087 cmd->mhandler.cmd_new(mon, qdict, &data);
3088 if (data)
3089 cmd->user_print(mon, data);
3090
3091 qobject_decref(data);
3092 } else {
af4ce882 3093 cmd->mhandler.cmd(mon, qdict);
55f81d96
LC
3094 }
3095
13917bee
LC
3096 qemu_errors_to_previous();
3097
3098out:
f7188bbe 3099 QDECREF(qdict);
9dc39cba
FB
3100}
3101
81d0912d
FB
3102static void cmd_completion(const char *name, const char *list)
3103{
3104 const char *p, *pstart;
3105 char cmd[128];
3106 int len;
3107
3108 p = list;
3109 for(;;) {
3110 pstart = p;
3111 p = strchr(p, '|');
3112 if (!p)
3113 p = pstart + strlen(pstart);
3114 len = p - pstart;
3115 if (len > sizeof(cmd) - 2)
3116 len = sizeof(cmd) - 2;
3117 memcpy(cmd, pstart, len);
3118 cmd[len] = '\0';
3119 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
731b0364 3120 readline_add_completion(cur_mon->rs, cmd);
81d0912d
FB
3121 }
3122 if (*p == '\0')
3123 break;
3124 p++;
3125 }
3126}
3127
3128static void file_completion(const char *input)
3129{
3130 DIR *ffs;
3131 struct dirent *d;
3132 char path[1024];
3133 char file[1024], file_prefix[1024];
3134 int input_path_len;
3135 const char *p;
3136
5fafdf24 3137 p = strrchr(input, '/');
81d0912d
FB
3138 if (!p) {
3139 input_path_len = 0;
3140 pstrcpy(file_prefix, sizeof(file_prefix), input);
363a37d5 3141 pstrcpy(path, sizeof(path), ".");
81d0912d
FB
3142 } else {
3143 input_path_len = p - input + 1;
3144 memcpy(path, input, input_path_len);
3145 if (input_path_len > sizeof(path) - 1)
3146 input_path_len = sizeof(path) - 1;
3147 path[input_path_len] = '\0';
3148 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3149 }
3150#ifdef DEBUG_COMPLETION
376253ec
AL
3151 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3152 input, path, file_prefix);
81d0912d
FB
3153#endif
3154 ffs = opendir(path);
3155 if (!ffs)
3156 return;
3157 for(;;) {
3158 struct stat sb;
3159 d = readdir(ffs);
3160 if (!d)
3161 break;
3162 if (strstart(d->d_name, file_prefix, NULL)) {
3163 memcpy(file, input, input_path_len);
363a37d5
BS
3164 if (input_path_len < sizeof(file))
3165 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3166 d->d_name);
81d0912d
FB
3167 /* stat the file to find out if it's a directory.
3168 * In that case add a slash to speed up typing long paths
3169 */
3170 stat(file, &sb);
3171 if(S_ISDIR(sb.st_mode))
363a37d5 3172 pstrcat(file, sizeof(file), "/");
731b0364 3173 readline_add_completion(cur_mon->rs, file);
81d0912d
FB
3174 }
3175 }
3176 closedir(ffs);
3177}
3178
51de9760 3179static void block_completion_it(void *opaque, BlockDriverState *bs)
81d0912d 3180{
51de9760 3181 const char *name = bdrv_get_device_name(bs);
81d0912d
FB
3182 const char *input = opaque;
3183
3184 if (input[0] == '\0' ||
3185 !strncmp(name, (char *)input, strlen(input))) {
731b0364 3186 readline_add_completion(cur_mon->rs, name);
81d0912d
FB
3187 }
3188}
3189
3190/* NOTE: this parser is an approximate form of the real command parser */
3191static void parse_cmdline(const char *cmdline,
3192 int *pnb_args, char **args)
3193{
3194 const char *p;
3195 int nb_args, ret;
3196 char buf[1024];
3197
3198 p = cmdline;
3199 nb_args = 0;
3200 for(;;) {
cd390083 3201 while (qemu_isspace(*p))
81d0912d
FB
3202 p++;
3203 if (*p == '\0')
3204 break;
3205 if (nb_args >= MAX_ARGS)
3206 break;
3207 ret = get_str(buf, sizeof(buf), &p);
3208 args[nb_args] = qemu_strdup(buf);
3209 nb_args++;
3210 if (ret < 0)
3211 break;
3212 }
3213 *pnb_args = nb_args;
3214}
3215
4d76d2ba
LC
3216static const char *next_arg_type(const char *typestr)
3217{
3218 const char *p = strchr(typestr, ':');
3219 return (p != NULL ? ++p : typestr);
3220}
3221
4c36ba32 3222static void monitor_find_completion(const char *cmdline)
81d0912d
FB
3223{
3224 const char *cmdname;
3225 char *args[MAX_ARGS];
3226 int nb_args, i, len;
3227 const char *ptype, *str;
c227f099 3228 const mon_cmd_t *cmd;
64866c3d 3229 const KeyDef *key;
81d0912d
FB
3230
3231 parse_cmdline(cmdline, &nb_args, args);
3232#ifdef DEBUG_COMPLETION
3233 for(i = 0; i < nb_args; i++) {
376253ec 3234 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
81d0912d
FB
3235 }
3236#endif
3237
3238 /* if the line ends with a space, it means we want to complete the
3239 next arg */
3240 len = strlen(cmdline);
cd390083 3241 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
81d0912d
FB
3242 if (nb_args >= MAX_ARGS)
3243 return;
3244 args[nb_args++] = qemu_strdup("");
3245 }
3246 if (nb_args <= 1) {
3247 /* command completion */
3248 if (nb_args == 0)
3249 cmdname = "";
3250 else
3251 cmdname = args[0];
731b0364 3252 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
376253ec 3253 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
81d0912d
FB
3254 cmd_completion(cmdname, cmd->name);
3255 }
3256 } else {
3257 /* find the command */
376253ec 3258 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
81d0912d
FB
3259 if (compare_cmd(args[0], cmd->name))
3260 goto found;
3261 }
3262 return;
3263 found:
4d76d2ba 3264 ptype = next_arg_type(cmd->args_type);
81d0912d
FB
3265 for(i = 0; i < nb_args - 2; i++) {
3266 if (*ptype != '\0') {
4d76d2ba 3267 ptype = next_arg_type(ptype);
81d0912d 3268 while (*ptype == '?')
4d76d2ba 3269 ptype = next_arg_type(ptype);
81d0912d
FB
3270 }
3271 }
3272 str = args[nb_args - 1];
2a1704a7
BS
3273 if (*ptype == '-' && ptype[1] != '\0') {
3274 ptype += 2;
3275 }
81d0912d
FB
3276 switch(*ptype) {
3277 case 'F':
3278 /* file completion */
731b0364 3279 readline_set_completion_index(cur_mon->rs, strlen(str));
81d0912d
FB
3280 file_completion(str);
3281 break;
3282 case 'B':
3283 /* block device name completion */
731b0364 3284 readline_set_completion_index(cur_mon->rs, strlen(str));
81d0912d
FB
3285 bdrv_iterate(block_completion_it, (void *)str);
3286 break;
7fe48483
FB
3287 case 's':
3288 /* XXX: more generic ? */
3289 if (!strcmp(cmd->name, "info")) {
731b0364 3290 readline_set_completion_index(cur_mon->rs, strlen(str));
7fe48483
FB
3291 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3292 cmd_completion(str, cmd->name);
3293 }
64866c3d 3294 } else if (!strcmp(cmd->name, "sendkey")) {
e600d1ef
BS
3295 char *sep = strrchr(str, '-');
3296 if (sep)
3297 str = sep + 1;
731b0364 3298 readline_set_completion_index(cur_mon->rs, strlen(str));
64866c3d
FB
3299 for(key = key_defs; key->name != NULL; key++) {
3300 cmd_completion(str, key->name);
3301 }
f3353c6b
JK
3302 } else if (!strcmp(cmd->name, "help|?")) {
3303 readline_set_completion_index(cur_mon->rs, strlen(str));
3304 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3305 cmd_completion(str, cmd->name);
3306 }
7fe48483
FB
3307 }
3308 break;
81d0912d
FB
3309 default:
3310 break;
3311 }
3312 }
3313 for(i = 0; i < nb_args; i++)
3314 qemu_free(args[i]);
3315}
3316
731b0364 3317static int monitor_can_read(void *opaque)
9dc39cba 3318{
731b0364
AL
3319 Monitor *mon = opaque;
3320
3321 return (mon->suspend_cnt == 0) ? 128 : 0;
9dc39cba
FB
3322}
3323
731b0364 3324static void monitor_read(void *opaque, const uint8_t *buf, int size)
9dc39cba 3325{
731b0364 3326 Monitor *old_mon = cur_mon;
7e2515e8 3327 int i;
376253ec 3328
731b0364
AL
3329 cur_mon = opaque;
3330
cde76ee1
AL
3331 if (cur_mon->rs) {
3332 for (i = 0; i < size; i++)
3333 readline_handle_byte(cur_mon->rs, buf[i]);
3334 } else {
3335 if (size == 0 || buf[size - 1] != 0)
3336 monitor_printf(cur_mon, "corrupted command\n");
3337 else
3338 monitor_handle_command(cur_mon, (char *)buf);
3339 }
9dc39cba 3340
731b0364
AL
3341 cur_mon = old_mon;
3342}
d8f44609 3343
376253ec 3344static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
aa455485 3345{
731b0364 3346 monitor_suspend(mon);
376253ec 3347 monitor_handle_command(mon, cmdline);
731b0364 3348 monitor_resume(mon);
d8f44609
AL
3349}
3350
cde76ee1 3351int monitor_suspend(Monitor *mon)
d8f44609 3352{
cde76ee1
AL
3353 if (!mon->rs)
3354 return -ENOTTY;
731b0364 3355 mon->suspend_cnt++;
cde76ee1 3356 return 0;
d8f44609
AL
3357}
3358
376253ec 3359void monitor_resume(Monitor *mon)
d8f44609 3360{
cde76ee1
AL
3361 if (!mon->rs)
3362 return;
731b0364
AL
3363 if (--mon->suspend_cnt == 0)
3364 readline_show_prompt(mon->rs);
aa455485
FB
3365}
3366
731b0364 3367static void monitor_event(void *opaque, int event)
86e94dea 3368{
376253ec
AL
3369 Monitor *mon = opaque;
3370
2724b180
AL
3371 switch (event) {
3372 case CHR_EVENT_MUX_IN:
a7aec5da
GH
3373 mon->mux_out = 0;
3374 if (mon->reset_seen) {
3375 readline_restart(mon->rs);
3376 monitor_resume(mon);
3377 monitor_flush(mon);
3378 } else {
3379 mon->suspend_cnt = 0;
3380 }
2724b180
AL
3381 break;
3382
3383 case CHR_EVENT_MUX_OUT:
a7aec5da
GH
3384 if (mon->reset_seen) {
3385 if (mon->suspend_cnt == 0) {
3386 monitor_printf(mon, "\n");
3387 }
3388 monitor_flush(mon);
3389 monitor_suspend(mon);
3390 } else {
3391 mon->suspend_cnt++;
3392 }
3393 mon->mux_out = 1;
2724b180 3394 break;
86e94dea 3395
2724b180
AL
3396 case CHR_EVENT_RESET:
3397 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3398 "information\n", QEMU_VERSION);
a7aec5da 3399 if (!mon->mux_out) {
2724b180 3400 readline_show_prompt(mon->rs);
a7aec5da
GH
3401 }
3402 mon->reset_seen = 1;
2724b180
AL
3403 break;
3404 }
86e94dea
TS
3405}
3406
76655d6d
AL
3407
3408/*
3409 * Local variables:
3410 * c-indent-level: 4
3411 * c-basic-offset: 4
3412 * tab-width: 8
3413 * End:
3414 */
3415
731b0364 3416void monitor_init(CharDriverState *chr, int flags)
aa455485 3417{
731b0364 3418 static int is_first_init = 1;
87127161 3419 Monitor *mon;
20d8a3ed
TS
3420
3421 if (is_first_init) {
c8256f9d 3422 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
20d8a3ed
TS
3423 is_first_init = 0;
3424 }
87127161
AL
3425
3426 mon = qemu_mallocz(sizeof(*mon));
20d8a3ed 3427
87127161 3428 mon->chr = chr;
731b0364 3429 mon->flags = flags;
cde76ee1
AL
3430 if (flags & MONITOR_USE_READLINE) {
3431 mon->rs = readline_init(mon, monitor_find_completion);
3432 monitor_read_command(mon, 0);
3433 }
87127161 3434
731b0364
AL
3435 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3436 mon);
87127161 3437
72cf2d4f 3438 QLIST_INSERT_HEAD(&mon_list, mon, entry);
731b0364 3439 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
87127161 3440 cur_mon = mon;
aa455485
FB
3441}
3442
376253ec 3443static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
81d0912d 3444{
bb5fc20f
AL
3445 BlockDriverState *bs = opaque;
3446 int ret = 0;
81d0912d 3447
bb5fc20f 3448 if (bdrv_set_key(bs, password) != 0) {
376253ec 3449 monitor_printf(mon, "invalid password\n");
bb5fc20f 3450 ret = -EPERM;
9dc39cba 3451 }
731b0364
AL
3452 if (mon->password_completion_cb)
3453 mon->password_completion_cb(mon->password_opaque, ret);
bb5fc20f 3454
731b0364 3455 monitor_read_command(mon, 1);
9dc39cba 3456}
c0f4ce77 3457
376253ec 3458void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
bb5fc20f
AL
3459 BlockDriverCompletionFunc *completion_cb,
3460 void *opaque)
c0f4ce77 3461{
cde76ee1
AL
3462 int err;
3463
bb5fc20f
AL
3464 if (!bdrv_key_required(bs)) {
3465 if (completion_cb)
3466 completion_cb(opaque, 0);
3467 return;
3468 }
c0f4ce77 3469
376253ec
AL
3470 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3471 bdrv_get_encrypted_filename(bs));
bb5fc20f 3472
731b0364
AL
3473 mon->password_completion_cb = completion_cb;
3474 mon->password_opaque = opaque;
bb5fc20f 3475
cde76ee1
AL
3476 err = monitor_read_password(mon, bdrv_password_cb, bs);
3477
3478 if (err && completion_cb)
3479 completion_cb(opaque, err);
c0f4ce77 3480}
ac7531ec
GH
3481
3482typedef struct QemuErrorSink QemuErrorSink;
3483struct QemuErrorSink {
3484 enum {
3485 ERR_SINK_FILE,
3486 ERR_SINK_MONITOR,
3487 } dest;
3488 union {
3489 FILE *fp;
3490 Monitor *mon;
3491 };
3492 QemuErrorSink *previous;
3493};
3494
528e93a9 3495static QemuErrorSink *qemu_error_sink;
ac7531ec
GH
3496
3497void qemu_errors_to_file(FILE *fp)
3498{
3499 QemuErrorSink *sink;
3500
3501 sink = qemu_mallocz(sizeof(*sink));
3502 sink->dest = ERR_SINK_FILE;
3503 sink->fp = fp;
3504 sink->previous = qemu_error_sink;
3505 qemu_error_sink = sink;
3506}
3507
3508void qemu_errors_to_mon(Monitor *mon)
3509{
3510 QemuErrorSink *sink;
3511
3512 sink = qemu_mallocz(sizeof(*sink));
3513 sink->dest = ERR_SINK_MONITOR;
3514 sink->mon = mon;
3515 sink->previous = qemu_error_sink;
3516 qemu_error_sink = sink;
3517}
3518
3519void qemu_errors_to_previous(void)
3520{
3521 QemuErrorSink *sink;
3522
3523 assert(qemu_error_sink != NULL);
3524 sink = qemu_error_sink;
3525 qemu_error_sink = sink->previous;
3526 qemu_free(sink);
3527}
3528
3529void qemu_error(const char *fmt, ...)
3530{
3531 va_list args;
3532
3533 assert(qemu_error_sink != NULL);
3534 switch (qemu_error_sink->dest) {
3535 case ERR_SINK_FILE:
3536 va_start(args, fmt);
3537 vfprintf(qemu_error_sink->fp, fmt, args);
3538 va_end(args);
3539 break;
3540 case ERR_SINK_MONITOR:
3541 va_start(args, fmt);
3542 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3543 va_end(args);
3544 break;
3545 }
3546}