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