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