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