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