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