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