]> git.proxmox.com Git - mirror_qemu.git/blame - monitor.c
monitor: code move for parse_cmdline()
[mirror_qemu.git] / monitor.c
CommitLineData
9dc39cba
FB
1/*
2 * QEMU monitor
5fafdf24 3 *
9dc39cba 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
9dc39cba
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
511d2b14 24#include <dirent.h>
87ecb68b 25#include "hw/hw.h"
b4a42f81 26#include "monitor/qdev.h"
87ecb68b
PB
27#include "hw/usb.h"
28#include "hw/pcmcia.h"
0d09e41a 29#include "hw/i386/pc.h"
a2cb15b0 30#include "hw/pci/pci.h"
0d09e41a 31#include "sysemu/watchdog.h"
45a50b16 32#include "hw/loader.h"
022c62cb 33#include "exec/gdbstub.h"
1422e32d 34#include "net/net.h"
68ac40d2 35#include "net/slirp.h"
dccfcd0e 36#include "sysemu/char.h"
7572150c 37#include "ui/qemu-spice.h"
9c17d615 38#include "sysemu/sysemu.h"
83c9089e
PB
39#include "monitor/monitor.h"
40#include "monitor/readline.h"
28ecbaee 41#include "ui/console.h"
9c17d615 42#include "sysemu/blockdev.h"
87ecb68b 43#include "audio/audio.h"
76cad711 44#include "disas/disas.h"
9c17d615 45#include "sysemu/balloon.h"
1de7afc9 46#include "qemu/timer.h"
caf71f86 47#include "migration/migration.h"
9c17d615 48#include "sysemu/kvm.h"
1de7afc9 49#include "qemu/acl.h"
bdee56f5 50#include "sysemu/tpm.h"
7b1b5d19
PB
51#include "qapi/qmp/qint.h"
52#include "qapi/qmp/qfloat.h"
53#include "qapi/qmp/qlist.h"
54#include "qapi/qmp/qbool.h"
55#include "qapi/qmp/qstring.h"
56#include "qapi/qmp/qjson.h"
57#include "qapi/qmp/json-streamer.h"
58#include "qapi/qmp/json-parser.h"
1de7afc9 59#include "qemu/osdep.h"
2b41f10e 60#include "cpu.h"
89bd820a 61#include "trace.h"
31965ae2 62#include "trace/control.h"
6d8a764e 63#ifdef CONFIG_TRACE_SIMPLE
31965ae2 64#include "trace/simple.h"
22890ab5 65#endif
022c62cb 66#include "exec/memory.h"
48a32bed
AL
67#include "qmp-commands.h"
68#include "hmp.h"
1de7afc9 69#include "qemu/thread.h"
6a5bd307 70
661f1929
JK
71/* for pic/irq_info */
72#if defined(TARGET_SPARC)
0d09e41a 73#include "hw/sparc/sun4m.h"
661f1929 74#endif
0d09e41a 75#include "hw/lm32/lm32_pic.h"
661f1929 76
9dc39cba 77//#define DEBUG
81d0912d 78//#define DEBUG_COMPLETION
9dc39cba 79
9307c4c1
FB
80/*
81 * Supported types:
5fafdf24 82 *
9307c4c1 83 * 'F' filename
81d0912d 84 * 'B' block device name
9307c4c1 85 * 's' string (accept optional quote)
361127df
MA
86 * 'O' option string of the form NAME=VALUE,...
87 * parsed according to QemuOptsList given by its name
88 * Example: 'device:O' uses qemu_device_opts.
89 * Restriction: only lists with empty desc are supported
90 * TODO lift the restriction
92a31b1f
FB
91 * 'i' 32 bit integer
92 * 'l' target long (32 or 64 bit)
91162849
LC
93 * 'M' Non-negative target long (32 or 64 bit), in user mode the
94 * value is multiplied by 2^20 (think Mebibyte)
dbc0c67f 95 * 'o' octets (aka bytes)
5e00984a
KW
96 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
97 * K, k suffix, which multiplies the value by 2^60 for suffixes E
98 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
99 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
fccfb11e
MA
100 * 'T' double
101 * user mode accepts an optional ms, us, ns suffix,
102 * which divides the value by 1e3, 1e6, 1e9, respectively
9307c4c1
FB
103 * '/' optional gdb-like print format (like "/10x")
104 *
fb46660e
LC
105 * '?' optional type (for all types, except '/')
106 * '.' other form of optional type (for 'i' and 'l')
942cd1f2
MA
107 * 'b' boolean
108 * user mode accepts "on" or "off"
fb46660e 109 * '-' optional parameter (eg. '-f')
9307c4c1
FB
110 *
111 */
112
940cc30d
AL
113typedef struct MonitorCompletionData MonitorCompletionData;
114struct MonitorCompletionData {
115 Monitor *mon;
116 void (*user_print)(Monitor *mon, const QObject *data);
117};
118
c227f099 119typedef struct mon_cmd_t {
9dc39cba 120 const char *name;
9307c4c1 121 const char *args_type;
9dc39cba
FB
122 const char *params;
123 const char *help;
a2876f59 124 void (*user_print)(Monitor *mon, const QObject *data);
910df89d 125 union {
af4ce882 126 void (*cmd)(Monitor *mon, const QDict *qdict);
261394db 127 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
940cc30d
AL
128 int (*cmd_async)(Monitor *mon, const QDict *params,
129 MonitorCompletion *cb, void *opaque);
910df89d 130 } mhandler;
8ac470c1 131 int flags;
5f3d335f
WX
132 /* @sub_table is a list of 2nd level of commands. If it do not exist,
133 * mhandler should be used. If it exist, sub_table[?].mhandler should be
134 * used, and mhandler of 1st level plays the role of help function.
135 */
136 struct mon_cmd_t *sub_table;
c227f099 137} mon_cmd_t;
9dc39cba 138
f07918fd 139/* file descriptors passed via SCM_RIGHTS */
c227f099
AL
140typedef struct mon_fd_t mon_fd_t;
141struct mon_fd_t {
f07918fd
MM
142 char *name;
143 int fd;
c227f099 144 QLIST_ENTRY(mon_fd_t) next;
f07918fd
MM
145};
146
ba1c048a
CB
147/* file descriptor associated with a file descriptor set */
148typedef struct MonFdsetFd MonFdsetFd;
149struct MonFdsetFd {
150 int fd;
151 bool removed;
152 char *opaque;
153 QLIST_ENTRY(MonFdsetFd) next;
154};
155
156/* file descriptor set containing fds passed via SCM_RIGHTS */
157typedef struct MonFdset MonFdset;
158struct MonFdset {
159 int64_t id;
160 QLIST_HEAD(, MonFdsetFd) fds;
adb696f3 161 QLIST_HEAD(, MonFdsetFd) dup_fds;
ba1c048a
CB
162 QLIST_ENTRY(MonFdset) next;
163};
164
5fa737a4
LC
165typedef struct MonitorControl {
166 QObject *id;
167 JSONMessageParser parser;
4a7e1190 168 int command_mode;
5fa737a4
LC
169} MonitorControl;
170
afeecec2
DB
171/*
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
174 * instance.
175 */
176typedef struct MonitorEventState {
177 MonitorEvent event; /* Event being tracked */
178 int64_t rate; /* Period over which to throttle. 0 to disable */
179 int64_t last; /* Time at which event was last emitted */
180 QEMUTimer *timer; /* Timer for handling delayed events */
181 QObject *data; /* Event pending delayed dispatch */
182} MonitorEventState;
183
87127161
AL
184struct Monitor {
185 CharDriverState *chr;
a7aec5da
GH
186 int mux_out;
187 int reset_seen;
731b0364
AL
188 int flags;
189 int suspend_cnt;
48c043d0 190 bool skip_flush;
e1f2641b 191 QString *outbuf;
293d2a00 192 guint watch;
731b0364 193 ReadLineState *rs;
5fa737a4 194 MonitorControl *mc;
cb446eca 195 CPUState *mon_cpu;
731b0364
AL
196 BlockDriverCompletionFunc *password_completion_cb;
197 void *password_opaque;
7717239d 198 mon_cmd_t *cmd_table;
8204a918 199 QError *error;
c227f099 200 QLIST_HEAD(,mon_fd_t) fds;
72cf2d4f 201 QLIST_ENTRY(Monitor) entry;
87127161
AL
202};
203
2dbc8db0
LC
204/* QMP checker flags */
205#define QMP_ACCEPT_UNKNOWNS 1
206
72cf2d4f 207static QLIST_HEAD(mon_list, Monitor) mon_list;
ba1c048a 208static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
efb87c16 209static int mon_refcount;
7e2515e8 210
816f8925
WX
211static mon_cmd_t mon_cmds[];
212static mon_cmd_t info_cmds[];
9dc39cba 213
f36b4afb
LC
214static const mon_cmd_t qmp_cmds[];
215
8631b608
MA
216Monitor *cur_mon;
217Monitor *default_mon;
376253ec 218
731b0364
AL
219static void monitor_command_cb(Monitor *mon, const char *cmdline,
220 void *opaque);
83ab7950 221
09069b19
LC
222static inline int qmp_cmd_mode(const Monitor *mon)
223{
224 return (mon->mc ? mon->mc->command_mode : 0);
225}
226
418173c7
LC
227/* Return true if in control mode, false otherwise */
228static inline int monitor_ctrl_mode(const Monitor *mon)
229{
230 return (mon->flags & MONITOR_USE_CONTROL);
231}
232
6620d3ce
MA
233/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
234int monitor_cur_is_qmp(void)
235{
236 return cur_mon && monitor_ctrl_mode(cur_mon);
237}
238
7060b478 239void monitor_read_command(Monitor *mon, int show_prompt)
731b0364 240{
183e6e52
LC
241 if (!mon->rs)
242 return;
243
731b0364
AL
244 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
245 if (show_prompt)
246 readline_show_prompt(mon->rs);
247}
6a00d601 248
7060b478
AL
249int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
250 void *opaque)
bb5fc20f 251{
94171e11 252 if (monitor_ctrl_mode(mon)) {
ab5b027e 253 qerror_report(QERR_MISSING_PARAMETER, "password");
94171e11
LC
254 return -EINVAL;
255 } else if (mon->rs) {
cde76ee1
AL
256 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
257 /* prompt is printed on return from the command handler */
258 return 0;
259 } else {
260 monitor_printf(mon, "terminal does not support password prompting\n");
261 return -ENOTTY;
262 }
bb5fc20f
AL
263}
264
f628926b
GH
265static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
266 void *opaque)
267{
293d2a00
LE
268 Monitor *mon = opaque;
269
270 mon->watch = 0;
271 monitor_flush(mon);
f628926b
GH
272 return FALSE;
273}
274
376253ec 275void monitor_flush(Monitor *mon)
7e2515e8 276{
f628926b 277 int rc;
e1f2641b
LC
278 size_t len;
279 const char *buf;
280
48c043d0
LC
281 if (mon->skip_flush) {
282 return;
283 }
284
e1f2641b
LC
285 buf = qstring_get_str(mon->outbuf);
286 len = qstring_get_length(mon->outbuf);
f628926b 287
a4cc73d6 288 if (len && !mon->mux_out) {
e1f2641b
LC
289 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
290 if (rc == len) {
f628926b 291 /* all flushed */
e1f2641b
LC
292 QDECREF(mon->outbuf);
293 mon->outbuf = qstring_new();
f628926b
GH
294 return;
295 }
296 if (rc > 0) {
297 /* partinal write */
e1f2641b
LC
298 QString *tmp = qstring_from_str(buf + rc);
299 QDECREF(mon->outbuf);
300 mon->outbuf = tmp;
f628926b 301 }
293d2a00
LE
302 if (mon->watch == 0) {
303 mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
304 monitor_unblocked, mon);
305 }
7e2515e8
FB
306 }
307}
308
e1f2641b 309/* flush at every end of line */
376253ec 310static void monitor_puts(Monitor *mon, const char *str)
7e2515e8 311{
60fe76f3 312 char c;
731b0364 313
7e2515e8
FB
314 for(;;) {
315 c = *str++;
316 if (c == '\0')
317 break;
e1f2641b
LC
318 if (c == '\n') {
319 qstring_append_chr(mon->outbuf, '\r');
320 }
321 qstring_append_chr(mon->outbuf, c);
322 if (c == '\n') {
376253ec 323 monitor_flush(mon);
e1f2641b 324 }
7e2515e8
FB
325 }
326}
327
376253ec 328void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
9dc39cba 329{
e1f2641b 330 char *buf;
b8b08266 331
2daa1191
LC
332 if (!mon)
333 return;
334
b8b08266 335 if (monitor_ctrl_mode(mon)) {
b8b08266 336 return;
4a29a85d 337 }
b8b08266 338
e1f2641b 339 buf = g_strdup_vprintf(fmt, ap);
b8b08266 340 monitor_puts(mon, buf);
e1f2641b 341 g_free(buf);
9dc39cba
FB
342}
343
376253ec 344void monitor_printf(Monitor *mon, const char *fmt, ...)
9dc39cba 345{
7e2515e8
FB
346 va_list ap;
347 va_start(ap, fmt);
376253ec 348 monitor_vprintf(mon, fmt, ap);
7e2515e8 349 va_end(ap);
9dc39cba
FB
350}
351
376253ec 352void monitor_print_filename(Monitor *mon, const char *filename)
fef30743
TS
353{
354 int i;
355
356 for (i = 0; filename[i]; i++) {
28a76be8
AL
357 switch (filename[i]) {
358 case ' ':
359 case '"':
360 case '\\':
361 monitor_printf(mon, "\\%c", filename[i]);
362 break;
363 case '\t':
364 monitor_printf(mon, "\\t");
365 break;
366 case '\r':
367 monitor_printf(mon, "\\r");
368 break;
369 case '\n':
370 monitor_printf(mon, "\\n");
371 break;
372 default:
373 monitor_printf(mon, "%c", filename[i]);
374 break;
375 }
fef30743
TS
376 }
377}
378
8b7968f7
SW
379static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
380 const char *fmt, ...)
7fe48483
FB
381{
382 va_list ap;
383 va_start(ap, fmt);
376253ec 384 monitor_vprintf((Monitor *)stream, fmt, ap);
7fe48483
FB
385 va_end(ap);
386 return 0;
387}
388
13c7425e
LC
389static void monitor_user_noop(Monitor *mon, const QObject *data) { }
390
9e80721e 391static inline int handler_is_qobject(const mon_cmd_t *cmd)
13917bee
LC
392{
393 return cmd->user_print != NULL;
394}
395
4903de0c 396static inline bool handler_is_async(const mon_cmd_t *cmd)
940cc30d 397{
8ac470c1 398 return cmd->flags & MONITOR_CMD_ASYNC;
940cc30d
AL
399}
400
8204a918
LC
401static inline int monitor_has_error(const Monitor *mon)
402{
403 return mon->error != NULL;
404}
405
9b57c02e
LC
406static void monitor_json_emitter(Monitor *mon, const QObject *data)
407{
408 QString *json;
409
83a27d4d
LC
410 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
411 qobject_to_json(data);
9b57c02e
LC
412 assert(json != NULL);
413
b8b08266
LC
414 qstring_append_chr(json, '\n');
415 monitor_puts(mon, qstring_get_str(json));
4a29a85d 416
9b57c02e
LC
417 QDECREF(json);
418}
419
de253f14
LC
420static QDict *build_qmp_error_dict(const QError *err)
421{
422 QObject *obj;
423
424 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
425 ErrorClass_lookup[err->err_class],
426 qerror_human(err));
427
428 return qobject_to_qdict(obj);
429}
430
25b422eb
LC
431static void monitor_protocol_emitter(Monitor *mon, QObject *data)
432{
433 QDict *qmp;
434
89bd820a
SH
435 trace_monitor_protocol_emitter(mon);
436
25b422eb
LC
437 if (!monitor_has_error(mon)) {
438 /* success response */
de253f14 439 qmp = qdict_new();
25b422eb
LC
440 if (data) {
441 qobject_incref(data);
442 qdict_put_obj(qmp, "return", data);
443 } else {
0abc6579
LC
444 /* return an empty QDict by default */
445 qdict_put(qmp, "return", qdict_new());
25b422eb
LC
446 }
447 } else {
448 /* error response */
de253f14 449 qmp = build_qmp_error_dict(mon->error);
25b422eb
LC
450 QDECREF(mon->error);
451 mon->error = NULL;
452 }
453
5fa737a4
LC
454 if (mon->mc->id) {
455 qdict_put_obj(qmp, "id", mon->mc->id);
456 mon->mc->id = NULL;
457 }
458
25b422eb
LC
459 monitor_json_emitter(mon, QOBJECT(qmp));
460 QDECREF(qmp);
461}
462
0d1ea871
LC
463static void timestamp_put(QDict *qdict)
464{
465 int err;
466 QObject *obj;
d08d6f04 467 qemu_timeval tv;
0d1ea871 468
d08d6f04 469 err = qemu_gettimeofday(&tv);
0d1ea871
LC
470 if (err < 0)
471 return;
472
473 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
474 "'microseconds': %" PRId64 " }",
475 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
0d1ea871
LC
476 qdict_put_obj(qdict, "timestamp", obj);
477}
478
4860853d
DB
479
480static const char *monitor_event_names[] = {
481 [QEVENT_SHUTDOWN] = "SHUTDOWN",
482 [QEVENT_RESET] = "RESET",
483 [QEVENT_POWERDOWN] = "POWERDOWN",
484 [QEVENT_STOP] = "STOP",
485 [QEVENT_RESUME] = "RESUME",
486 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
487 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
488 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
489 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
490 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
491 [QEVENT_WATCHDOG] = "WATCHDOG",
492 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
493 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
494 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
495 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
496 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
32c81a4a 497 [QEVENT_BLOCK_JOB_ERROR] = "BLOCK_JOB_ERROR",
a66a2a36 498 [QEVENT_BLOCK_JOB_READY] = "BLOCK_JOB_READY",
0402a5d6 499 [QEVENT_DEVICE_DELETED] = "DEVICE_DELETED",
4860853d 500 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
b1be4280 501 [QEVENT_NIC_RX_FILTER_CHANGED] = "NIC_RX_FILTER_CHANGED",
4860853d 502 [QEVENT_SUSPEND] = "SUSPEND",
25df49f6 503 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
4860853d 504 [QEVENT_WAKEUP] = "WAKEUP",
973603a8 505 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
2fdd16e2 506 [QEVENT_SPICE_MIGRATE_COMPLETED] = "SPICE_MIGRATE_COMPLETED",
c401a8a5 507 [QEVENT_GUEST_PANICKED] = "GUEST_PANICKED",
4860853d
DB
508};
509QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
510
afeecec2
DB
511MonitorEventState monitor_event_state[QEVENT_MAX];
512QemuMutex monitor_event_state_lock;
513
514/*
515 * Emits the event to every monitor instance
516 */
517static void
518monitor_protocol_event_emit(MonitorEvent event,
519 QObject *data)
520{
521 Monitor *mon;
522
523 trace_monitor_protocol_event_emit(event, data);
524 QLIST_FOREACH(mon, &mon_list, entry) {
525 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
526 monitor_json_emitter(mon, data);
527 }
528 }
529}
530
531
532/*
533 * Queue a new event for emission to Monitor instances,
534 * applying any rate limiting if required.
535 */
536static void
537monitor_protocol_event_queue(MonitorEvent event,
538 QObject *data)
539{
540 MonitorEventState *evstate;
bc72ad67 541 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
afeecec2
DB
542 assert(event < QEVENT_MAX);
543
544 qemu_mutex_lock(&monitor_event_state_lock);
545 evstate = &(monitor_event_state[event]);
546 trace_monitor_protocol_event_queue(event,
547 data,
548 evstate->rate,
549 evstate->last,
550 now);
551
552 /* Rate limit of 0 indicates no throttling */
553 if (!evstate->rate) {
554 monitor_protocol_event_emit(event, data);
555 evstate->last = now;
556 } else {
557 int64_t delta = now - evstate->last;
558 if (evstate->data ||
559 delta < evstate->rate) {
560 /* If there's an existing event pending, replace
561 * it with the new event, otherwise schedule a
562 * timer for delayed emission
563 */
564 if (evstate->data) {
565 qobject_decref(evstate->data);
566 } else {
567 int64_t then = evstate->last + evstate->rate;
bc72ad67 568 timer_mod_ns(evstate->timer, then);
afeecec2
DB
569 }
570 evstate->data = data;
571 qobject_incref(evstate->data);
572 } else {
573 monitor_protocol_event_emit(event, data);
574 evstate->last = now;
575 }
576 }
577 qemu_mutex_unlock(&monitor_event_state_lock);
578}
579
580
581/*
582 * The callback invoked by QemuTimer when a delayed
583 * event is ready to be emitted
584 */
585static void monitor_protocol_event_handler(void *opaque)
586{
587 MonitorEventState *evstate = opaque;
bc72ad67 588 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
afeecec2
DB
589
590 qemu_mutex_lock(&monitor_event_state_lock);
591
592 trace_monitor_protocol_event_handler(evstate->event,
593 evstate->data,
594 evstate->last,
595 now);
596 if (evstate->data) {
597 monitor_protocol_event_emit(evstate->event, evstate->data);
598 qobject_decref(evstate->data);
599 evstate->data = NULL;
600 }
601 evstate->last = now;
602 qemu_mutex_unlock(&monitor_event_state_lock);
603}
604
605
606/*
607 * @event: the event ID to be limited
608 * @rate: the rate limit in milliseconds
609 *
610 * Sets a rate limit on a particular event, so no
611 * more than 1 event will be emitted within @rate
612 * milliseconds
613 */
614static void
615monitor_protocol_event_throttle(MonitorEvent event,
616 int64_t rate)
617{
618 MonitorEventState *evstate;
619 assert(event < QEVENT_MAX);
620
621 evstate = &(monitor_event_state[event]);
622
623 trace_monitor_protocol_event_throttle(event, rate);
624 evstate->event = event;
625 evstate->rate = rate * SCALE_MS;
bc72ad67 626 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
afeecec2
DB
627 SCALE_MS,
628 monitor_protocol_event_handler,
629 evstate);
630 evstate->last = 0;
631 evstate->data = NULL;
632}
633
634
635/* Global, one-time initializer to configure the rate limiting
636 * and initialize state */
637static void monitor_protocol_event_init(void)
638{
639 qemu_mutex_init(&monitor_event_state_lock);
640 /* Limit RTC & BALLOON events to 1 per second */
641 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
642 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
643 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
644}
645
0d1ea871
LC
646/**
647 * monitor_protocol_event(): Generate a Monitor event
648 *
649 * Event-specific data can be emitted through the (optional) 'data' parameter.
650 */
651void monitor_protocol_event(MonitorEvent event, QObject *data)
652{
653 QDict *qmp;
654 const char *event_name;
0d1ea871 655
242cd003 656 assert(event < QEVENT_MAX);
0d1ea871 657
4860853d
DB
658 event_name = monitor_event_names[event];
659 assert(event_name != NULL);
0d1ea871
LC
660
661 qmp = qdict_new();
662 timestamp_put(qmp);
663 qdict_put(qmp, "event", qstring_from_str(event_name));
3d72f9a2
LC
664 if (data) {
665 qobject_incref(data);
0d1ea871 666 qdict_put_obj(qmp, "data", data);
3d72f9a2 667 }
0d1ea871 668
afeecec2
DB
669 trace_monitor_protocol_event(event, event_name, qmp);
670 monitor_protocol_event_queue(event, QOBJECT(qmp));
0d1ea871
LC
671 QDECREF(qmp);
672}
673
ef4b7eee
LC
674static int do_qmp_capabilities(Monitor *mon, const QDict *params,
675 QObject **ret_data)
4a7e1190
LC
676{
677 /* Will setup QMP capabilities in the future */
678 if (monitor_ctrl_mode(mon)) {
679 mon->mc->command_mode = 1;
680 }
ef4b7eee
LC
681
682 return 0;
4a7e1190
LC
683}
684
0268d97c
LC
685static void handle_user_command(Monitor *mon, const char *cmdline);
686
b01fe89e
WX
687static void monitor_data_init(Monitor *mon)
688{
689 memset(mon, 0, sizeof(Monitor));
690 mon->outbuf = qstring_new();
7717239d
WX
691 /* Use *mon_cmds by default. */
692 mon->cmd_table = mon_cmds;
b01fe89e
WX
693}
694
695static void monitor_data_destroy(Monitor *mon)
696{
697 QDECREF(mon->outbuf);
698}
699
d51a67b4
LC
700char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
701 int64_t cpu_index, Error **errp)
0268d97c 702{
d51a67b4 703 char *output = NULL;
0268d97c 704 Monitor *old_mon, hmp;
0268d97c 705
b01fe89e 706 monitor_data_init(&hmp);
48c043d0 707 hmp.skip_flush = true;
0268d97c
LC
708
709 old_mon = cur_mon;
710 cur_mon = &hmp;
711
d51a67b4
LC
712 if (has_cpu_index) {
713 int ret = monitor_set_cpu(cpu_index);
0268d97c
LC
714 if (ret < 0) {
715 cur_mon = old_mon;
d51a67b4
LC
716 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
717 "a CPU number");
0268d97c
LC
718 goto out;
719 }
720 }
721
d51a67b4 722 handle_user_command(&hmp, command_line);
0268d97c
LC
723 cur_mon = old_mon;
724
48c043d0
LC
725 if (qstring_get_length(hmp.outbuf) > 0) {
726 output = g_strdup(qstring_get_str(hmp.outbuf));
d51a67b4
LC
727 } else {
728 output = g_strdup("");
0268d97c
LC
729 }
730
731out:
b01fe89e 732 monitor_data_destroy(&hmp);
d51a67b4 733 return output;
0268d97c
LC
734}
735
9dc39cba
FB
736static int compare_cmd(const char *name, const char *list)
737{
738 const char *p, *pstart;
739 int len;
740 len = strlen(name);
741 p = list;
742 for(;;) {
743 pstart = p;
744 p = strchr(p, '|');
745 if (!p)
746 p = pstart + strlen(pstart);
747 if ((p - pstart) == len && !memcmp(pstart, name, len))
748 return 1;
749 if (*p == '\0')
750 break;
751 p++;
752 }
753 return 0;
754}
755
f5438c05
WX
756static int get_str(char *buf, int buf_size, const char **pp)
757{
758 const char *p;
759 char *q;
760 int c;
761
762 q = buf;
763 p = *pp;
764 while (qemu_isspace(*p)) {
765 p++;
766 }
767 if (*p == '\0') {
768 fail:
769 *q = '\0';
770 *pp = p;
771 return -1;
772 }
773 if (*p == '\"') {
774 p++;
775 while (*p != '\0' && *p != '\"') {
776 if (*p == '\\') {
777 p++;
778 c = *p++;
779 switch (c) {
780 case 'n':
781 c = '\n';
782 break;
783 case 'r':
784 c = '\r';
785 break;
786 case '\\':
787 case '\'':
788 case '\"':
789 break;
790 default:
791 qemu_printf("unsupported escape code: '\\%c'\n", c);
792 goto fail;
793 }
794 if ((q - buf) < buf_size - 1) {
795 *q++ = c;
796 }
797 } else {
798 if ((q - buf) < buf_size - 1) {
799 *q++ = *p;
800 }
801 p++;
802 }
803 }
804 if (*p != '\"') {
805 qemu_printf("unterminated string\n");
806 goto fail;
807 }
808 p++;
809 } else {
810 while (*p != '\0' && !qemu_isspace(*p)) {
811 if ((q - buf) < buf_size - 1) {
812 *q++ = *p;
813 }
814 p++;
815 }
816 }
817 *q = '\0';
818 *pp = p;
819 return 0;
820}
821
822#define MAX_ARGS 16
823
824/* NOTE: this parser is an approximate form of the real command parser */
825static void parse_cmdline(const char *cmdline,
826 int *pnb_args, char **args)
827{
828 const char *p;
829 int nb_args, ret;
830 char buf[1024];
831
832 p = cmdline;
833 nb_args = 0;
834 for (;;) {
835 while (qemu_isspace(*p)) {
836 p++;
837 }
838 if (*p == '\0') {
839 break;
840 }
841 if (nb_args >= MAX_ARGS) {
842 break;
843 }
844 ret = get_str(buf, sizeof(buf), &p);
845 args[nb_args] = g_strdup(buf);
846 nb_args++;
847 if (ret < 0) {
848 break;
849 }
850 }
851 *pnb_args = nb_args;
852}
853
c227f099 854static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
376253ec 855 const char *prefix, const char *name)
9dc39cba 856{
c227f099 857 const mon_cmd_t *cmd;
9dc39cba
FB
858
859 for(cmd = cmds; cmd->name != NULL; cmd++) {
860 if (!name || !strcmp(name, cmd->name))
376253ec
AL
861 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
862 cmd->params, cmd->help);
9dc39cba
FB
863 }
864}
865
376253ec 866static void help_cmd(Monitor *mon, const char *name)
9dc39cba
FB
867{
868 if (name && !strcmp(name, "info")) {
376253ec 869 help_cmd_dump(mon, info_cmds, "info ", NULL);
9dc39cba 870 } else {
7717239d 871 help_cmd_dump(mon, mon->cmd_table, "", name);
f193c797 872 if (name && !strcmp(name, "log")) {
38dad9e5 873 const QEMULogItem *item;
376253ec
AL
874 monitor_printf(mon, "Log items (comma separated):\n");
875 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
38dad9e5 876 for (item = qemu_log_items; item->mask != 0; item++) {
376253ec 877 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
f193c797
FB
878 }
879 }
9dc39cba
FB
880 }
881}
882
d54908a5 883static void do_help_cmd(Monitor *mon, const QDict *qdict)
38183186 884{
d54908a5 885 help_cmd(mon, qdict_get_try_str(qdict, "name"));
38183186
LC
886}
887
fc764105 888static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
22890ab5
PS
889{
890 const char *tp_name = qdict_get_str(qdict, "name");
891 bool new_state = qdict_get_bool(qdict, "option");
f871d689 892
ca285c3f
LV
893 bool found = false;
894 TraceEvent *ev = NULL;
895 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
896 found = true;
897 if (!trace_event_get_state_static(ev)) {
898 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
899 } else {
900 trace_event_set_state_dynamic(ev, new_state);
901 }
902 }
903 if (!trace_event_is_pattern(tp_name) && !found) {
f871d689
BS
904 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
905 }
22890ab5 906}
c5ceb523 907
c45a8168 908#ifdef CONFIG_TRACE_SIMPLE
c5ceb523
SH
909static void do_trace_file(Monitor *mon, const QDict *qdict)
910{
911 const char *op = qdict_get_try_str(qdict, "op");
912 const char *arg = qdict_get_try_str(qdict, "arg");
913
914 if (!op) {
915 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
916 } else if (!strcmp(op, "on")) {
917 st_set_trace_file_enabled(true);
918 } else if (!strcmp(op, "off")) {
919 st_set_trace_file_enabled(false);
920 } else if (!strcmp(op, "flush")) {
921 st_flush_trace_buffer();
922 } else if (!strcmp(op, "set")) {
923 if (arg) {
924 st_set_trace_file(arg);
925 }
926 } else {
927 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
928 help_cmd(mon, "trace-file");
929 }
930}
22890ab5
PS
931#endif
932
940cc30d
AL
933static void user_monitor_complete(void *opaque, QObject *ret_data)
934{
935 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
936
937 if (ret_data) {
938 data->user_print(data->mon, ret_data);
939 }
940 monitor_resume(data->mon);
7267c094 941 g_free(data);
940cc30d
AL
942}
943
944static void qmp_monitor_complete(void *opaque, QObject *ret_data)
945{
946 monitor_protocol_emitter(opaque, ret_data);
947}
948
5af7bbae
LC
949static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
950 const QDict *params)
940cc30d 951{
5af7bbae 952 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
940cc30d
AL
953}
954
940cc30d
AL
955static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
956 const QDict *params)
957{
958 int ret;
959
7267c094 960 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
940cc30d
AL
961 cb_data->mon = mon;
962 cb_data->user_print = cmd->user_print;
963 monitor_suspend(mon);
964 ret = cmd->mhandler.cmd_async(mon, params,
965 user_monitor_complete, cb_data);
966 if (ret < 0) {
967 monitor_resume(mon);
7267c094 968 g_free(cb_data);
940cc30d
AL
969 }
970}
971
84c44613 972static void do_info_help(Monitor *mon, const QDict *qdict)
9dc39cba 973{
13c7425e 974 help_cmd(mon, "info");
9dc39cba
FB
975}
976
aa9b79bc 977CommandInfoList *qmp_query_commands(Error **errp)
e3bba9d0 978{
aa9b79bc 979 CommandInfoList *info, *cmd_list = NULL;
e3bba9d0
LC
980 const mon_cmd_t *cmd;
981
f36b4afb 982 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
40e5a01d
LC
983 info = g_malloc0(sizeof(*info));
984 info->value = g_malloc0(sizeof(*info->value));
985 info->value->name = g_strdup(cmd->name);
e3bba9d0 986
aa9b79bc
LC
987 info->next = cmd_list;
988 cmd_list = info;
e3bba9d0
LC
989 }
990
aa9b79bc 991 return cmd_list;
a36e69dd
TS
992}
993
4860853d
DB
994EventInfoList *qmp_query_events(Error **errp)
995{
996 EventInfoList *info, *ev_list = NULL;
997 MonitorEvent e;
998
999 for (e = 0 ; e < QEVENT_MAX ; e++) {
1000 const char *event_name = monitor_event_names[e];
1001 assert(event_name != NULL);
1002 info = g_malloc0(sizeof(*info));
1003 info->value = g_malloc0(sizeof(*info->value));
1004 info->value->name = g_strdup(event_name);
1005
1006 info->next = ev_list;
1007 ev_list = info;
1008 }
1009
1010 return ev_list;
1011}
1012
b025c8b4
LC
1013/* set the current CPU defined by the user */
1014int monitor_set_cpu(int cpu_index)
6a00d601 1015{
55e5c285 1016 CPUState *cpu;
6a00d601 1017
1c8bb3cc
AF
1018 cpu = qemu_get_cpu(cpu_index);
1019 if (cpu == NULL) {
1020 return -1;
6a00d601 1021 }
cb446eca 1022 cur_mon->mon_cpu = cpu;
1c8bb3cc 1023 return 0;
6a00d601
FB
1024}
1025
9349b4f9 1026static CPUArchState *mon_get_cpu(void)
6a00d601 1027{
731b0364 1028 if (!cur_mon->mon_cpu) {
b025c8b4 1029 monitor_set_cpu(0);
6a00d601 1030 }
4c0960c0 1031 cpu_synchronize_state(cur_mon->mon_cpu);
cb446eca 1032 return cur_mon->mon_cpu->env_ptr;
6a00d601
FB
1033}
1034
99b7796f
LC
1035int monitor_get_cpu_index(void)
1036{
55e5c285
AF
1037 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1038 return cpu->cpu_index;
99b7796f
LC
1039}
1040
84f2d0ea 1041static void do_info_registers(Monitor *mon, const QDict *qdict)
9307c4c1 1042{
878096ee 1043 CPUState *cpu;
9349b4f9 1044 CPUArchState *env;
6a00d601 1045 env = mon_get_cpu();
878096ee
AF
1046 cpu = ENV_GET_CPU(env);
1047 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
9307c4c1
FB
1048}
1049
84f2d0ea 1050static void do_info_jit(Monitor *mon, const QDict *qdict)
e3db7226 1051{
376253ec 1052 dump_exec_info((FILE *)mon, monitor_fprintf);
e3db7226
FB
1053}
1054
84f2d0ea 1055static void do_info_history(Monitor *mon, const QDict *qdict)
aa455485
FB
1056{
1057 int i;
7e2515e8 1058 const char *str;
3b46e624 1059
cde76ee1
AL
1060 if (!mon->rs)
1061 return;
7e2515e8
FB
1062 i = 0;
1063 for(;;) {
731b0364 1064 str = readline_get_history(mon->rs, i);
7e2515e8
FB
1065 if (!str)
1066 break;
376253ec 1067 monitor_printf(mon, "%d: '%s'\n", i, str);
8e3a9fd2 1068 i++;
aa455485
FB
1069 }
1070}
1071
84f2d0ea 1072static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
76a66253 1073{
878096ee 1074 CPUState *cpu;
9349b4f9 1075 CPUArchState *env;
76a66253
JM
1076
1077 env = mon_get_cpu();
878096ee
AF
1078 cpu = ENV_GET_CPU(env);
1079 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
76a66253 1080}
76a66253 1081
84f2d0ea 1082static void do_trace_print_events(Monitor *mon, const QDict *qdict)
22890ab5 1083{
fc764105 1084 trace_print_events((FILE *)mon, &monitor_fprintf);
22890ab5 1085}
22890ab5 1086
edc5cb1a
YH
1087static int client_migrate_info(Monitor *mon, const QDict *qdict,
1088 MonitorCompletion cb, void *opaque)
e866e239
GH
1089{
1090 const char *protocol = qdict_get_str(qdict, "protocol");
1091 const char *hostname = qdict_get_str(qdict, "hostname");
1092 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1093 int port = qdict_get_try_int(qdict, "port", -1);
1094 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1095 int ret;
1096
1097 if (strcmp(protocol, "spice") == 0) {
1098 if (!using_spice) {
1099 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1100 return -1;
1101 }
1102
6ec5dae5
YH
1103 if (port == -1 && tls_port == -1) {
1104 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1105 return -1;
1106 }
1107
edc5cb1a
YH
1108 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1109 cb, opaque);
e866e239
GH
1110 if (ret != 0) {
1111 qerror_report(QERR_UNDEFINED_ERROR);
1112 return -1;
1113 }
1114 return 0;
1115 }
1116
1117 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1118 return -1;
1119}
1120
d54908a5 1121static void do_logfile(Monitor *mon, const QDict *qdict)
e735b91c 1122{
9a7e5424 1123 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
e735b91c
PB
1124}
1125
d54908a5 1126static void do_log(Monitor *mon, const QDict *qdict)
f193c797
FB
1127{
1128 int mask;
d54908a5 1129 const char *items = qdict_get_str(qdict, "items");
3b46e624 1130
9307c4c1 1131 if (!strcmp(items, "none")) {
f193c797
FB
1132 mask = 0;
1133 } else {
4fde1eba 1134 mask = qemu_str_to_log_mask(items);
f193c797 1135 if (!mask) {
376253ec 1136 help_cmd(mon, "log");
f193c797
FB
1137 return;
1138 }
1139 }
24537a01 1140 qemu_set_log(mask);
f193c797
FB
1141}
1142
d54908a5 1143static void do_singlestep(Monitor *mon, const QDict *qdict)
1b530a6d 1144{
d54908a5 1145 const char *option = qdict_get_try_str(qdict, "option");
1b530a6d
AJ
1146 if (!option || !strcmp(option, "on")) {
1147 singlestep = 1;
1148 } else if (!strcmp(option, "off")) {
1149 singlestep = 0;
1150 } else {
1151 monitor_printf(mon, "unexpected option %s\n", option);
1152 }
1153}
1154
d54908a5 1155static void do_gdbserver(Monitor *mon, const QDict *qdict)
59030a8c 1156{
d54908a5 1157 const char *device = qdict_get_try_str(qdict, "device");
59030a8c
AL
1158 if (!device)
1159 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1160 if (gdbserver_start(device) < 0) {
1161 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1162 device);
1163 } else if (strcmp(device, "none") == 0) {
36556b20 1164 monitor_printf(mon, "Disabled gdbserver\n");
8a7ddc38 1165 } else {
59030a8c
AL
1166 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1167 device);
8a7ddc38
FB
1168 }
1169}
1170
d54908a5 1171static void do_watchdog_action(Monitor *mon, const QDict *qdict)
9dd986cc 1172{
d54908a5 1173 const char *action = qdict_get_str(qdict, "action");
9dd986cc
RJ
1174 if (select_watchdog_action(action) == -1) {
1175 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1176 }
1177}
1178
376253ec 1179static void monitor_printc(Monitor *mon, int c)
9307c4c1 1180{
376253ec 1181 monitor_printf(mon, "'");
9307c4c1
FB
1182 switch(c) {
1183 case '\'':
376253ec 1184 monitor_printf(mon, "\\'");
9307c4c1
FB
1185 break;
1186 case '\\':
376253ec 1187 monitor_printf(mon, "\\\\");
9307c4c1
FB
1188 break;
1189 case '\n':
376253ec 1190 monitor_printf(mon, "\\n");
9307c4c1
FB
1191 break;
1192 case '\r':
376253ec 1193 monitor_printf(mon, "\\r");
9307c4c1
FB
1194 break;
1195 default:
1196 if (c >= 32 && c <= 126) {
376253ec 1197 monitor_printf(mon, "%c", c);
9307c4c1 1198 } else {
376253ec 1199 monitor_printf(mon, "\\x%02x", c);
9307c4c1
FB
1200 }
1201 break;
1202 }
376253ec 1203 monitor_printf(mon, "'");
9307c4c1
FB
1204}
1205
376253ec 1206static void memory_dump(Monitor *mon, int count, int format, int wsize,
a8170e5e 1207 hwaddr addr, int is_physical)
9307c4c1 1208{
9349b4f9 1209 CPUArchState *env;
23842aab 1210 int l, line_size, i, max_digits, len;
9307c4c1
FB
1211 uint8_t buf[16];
1212 uint64_t v;
1213
1214 if (format == 'i') {
1215 int flags;
1216 flags = 0;
6a00d601 1217 env = mon_get_cpu();
9307c4c1 1218#ifdef TARGET_I386
4c27ba27 1219 if (wsize == 2) {
9307c4c1 1220 flags = 1;
4c27ba27
FB
1221 } else if (wsize == 4) {
1222 flags = 0;
1223 } else {
6a15fd12 1224 /* as default we use the current CS size */
4c27ba27 1225 flags = 0;
6a15fd12
FB
1226 if (env) {
1227#ifdef TARGET_X86_64
5fafdf24 1228 if ((env->efer & MSR_EFER_LMA) &&
6a15fd12
FB
1229 (env->segs[R_CS].flags & DESC_L_MASK))
1230 flags = 2;
1231 else
1232#endif
1233 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1234 flags = 1;
1235 }
4c27ba27
FB
1236 }
1237#endif
376253ec 1238 monitor_disas(mon, env, addr, count, is_physical, flags);
9307c4c1
FB
1239 return;
1240 }
1241
1242 len = wsize * count;
1243 if (wsize == 1)
1244 line_size = 8;
1245 else
1246 line_size = 16;
9307c4c1
FB
1247 max_digits = 0;
1248
1249 switch(format) {
1250 case 'o':
1251 max_digits = (wsize * 8 + 2) / 3;
1252 break;
1253 default:
1254 case 'x':
1255 max_digits = (wsize * 8) / 4;
1256 break;
1257 case 'u':
1258 case 'd':
1259 max_digits = (wsize * 8 * 10 + 32) / 33;
1260 break;
1261 case 'c':
1262 wsize = 1;
1263 break;
1264 }
1265
1266 while (len > 0) {
7743e588 1267 if (is_physical)
376253ec 1268 monitor_printf(mon, TARGET_FMT_plx ":", addr);
7743e588 1269 else
376253ec 1270 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
9307c4c1
FB
1271 l = len;
1272 if (l > line_size)
1273 l = line_size;
1274 if (is_physical) {
54f7b4a3 1275 cpu_physical_memory_read(addr, buf, l);
9307c4c1 1276 } else {
6a00d601 1277 env = mon_get_cpu();
f17ec444 1278 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
376253ec 1279 monitor_printf(mon, " Cannot access memory\n");
c8f79b67
AL
1280 break;
1281 }
9307c4c1 1282 }
5fafdf24 1283 i = 0;
9307c4c1
FB
1284 while (i < l) {
1285 switch(wsize) {
1286 default:
1287 case 1:
1288 v = ldub_raw(buf + i);
1289 break;
1290 case 2:
1291 v = lduw_raw(buf + i);
1292 break;
1293 case 4:
92a31b1f 1294 v = (uint32_t)ldl_raw(buf + i);
9307c4c1
FB
1295 break;
1296 case 8:
1297 v = ldq_raw(buf + i);
1298 break;
1299 }
376253ec 1300 monitor_printf(mon, " ");
9307c4c1
FB
1301 switch(format) {
1302 case 'o':
376253ec 1303 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
9307c4c1
FB
1304 break;
1305 case 'x':
376253ec 1306 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
9307c4c1
FB
1307 break;
1308 case 'u':
376253ec 1309 monitor_printf(mon, "%*" PRIu64, max_digits, v);
9307c4c1
FB
1310 break;
1311 case 'd':
376253ec 1312 monitor_printf(mon, "%*" PRId64, max_digits, v);
9307c4c1
FB
1313 break;
1314 case 'c':
376253ec 1315 monitor_printc(mon, v);
9307c4c1
FB
1316 break;
1317 }
1318 i += wsize;
1319 }
376253ec 1320 monitor_printf(mon, "\n");
9307c4c1
FB
1321 addr += l;
1322 len -= l;
1323 }
1324}
1325
1bd1442e 1326static void do_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 1327{
1bd1442e
LC
1328 int count = qdict_get_int(qdict, "count");
1329 int format = qdict_get_int(qdict, "format");
1330 int size = qdict_get_int(qdict, "size");
1331 target_long addr = qdict_get_int(qdict, "addr");
1332
376253ec 1333 memory_dump(mon, count, format, size, addr, 0);
9307c4c1
FB
1334}
1335
1bd1442e 1336static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 1337{
1bd1442e
LC
1338 int count = qdict_get_int(qdict, "count");
1339 int format = qdict_get_int(qdict, "format");
1340 int size = qdict_get_int(qdict, "size");
a8170e5e 1341 hwaddr addr = qdict_get_int(qdict, "addr");
1bd1442e 1342
376253ec 1343 memory_dump(mon, count, format, size, addr, 1);
9307c4c1
FB
1344}
1345
1bd1442e 1346static void do_print(Monitor *mon, const QDict *qdict)
9307c4c1 1347{
1bd1442e 1348 int format = qdict_get_int(qdict, "format");
a8170e5e 1349 hwaddr val = qdict_get_int(qdict, "val");
1bd1442e 1350
9307c4c1
FB
1351 switch(format) {
1352 case 'o':
a8170e5e 1353 monitor_printf(mon, "%#" HWADDR_PRIo, val);
9307c4c1
FB
1354 break;
1355 case 'x':
a8170e5e 1356 monitor_printf(mon, "%#" HWADDR_PRIx, val);
9307c4c1
FB
1357 break;
1358 case 'u':
a8170e5e 1359 monitor_printf(mon, "%" HWADDR_PRIu, val);
9307c4c1
FB
1360 break;
1361 default:
1362 case 'd':
a8170e5e 1363 monitor_printf(mon, "%" HWADDR_PRId, val);
9307c4c1
FB
1364 break;
1365 case 'c':
376253ec 1366 monitor_printc(mon, val);
9307c4c1
FB
1367 break;
1368 }
376253ec 1369 monitor_printf(mon, "\n");
9307c4c1
FB
1370}
1371
f18c16de 1372static void do_sum(Monitor *mon, const QDict *qdict)
e4cf1adc
FB
1373{
1374 uint32_t addr;
e4cf1adc 1375 uint16_t sum;
f18c16de
LC
1376 uint32_t start = qdict_get_int(qdict, "start");
1377 uint32_t size = qdict_get_int(qdict, "size");
e4cf1adc
FB
1378
1379 sum = 0;
1380 for(addr = start; addr < (start + size); addr++) {
54f7b4a3 1381 uint8_t val = ldub_phys(addr);
e4cf1adc
FB
1382 /* BSD sum algorithm ('sum' Unix command) */
1383 sum = (sum >> 1) | (sum << 15);
54f7b4a3 1384 sum += val;
e4cf1adc 1385 }
376253ec 1386 monitor_printf(mon, "%05d\n", sum);
e4cf1adc
FB
1387}
1388
13224a87
FB
1389static int mouse_button_state;
1390
1d4daa91 1391static void do_mouse_move(Monitor *mon, const QDict *qdict)
13224a87
FB
1392{
1393 int dx, dy, dz;
1d4daa91
LC
1394 const char *dx_str = qdict_get_str(qdict, "dx_str");
1395 const char *dy_str = qdict_get_str(qdict, "dy_str");
1396 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
13224a87
FB
1397 dx = strtol(dx_str, NULL, 0);
1398 dy = strtol(dy_str, NULL, 0);
1399 dz = 0;
5fafdf24 1400 if (dz_str)
13224a87
FB
1401 dz = strtol(dz_str, NULL, 0);
1402 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1403}
1404
d54908a5 1405static void do_mouse_button(Monitor *mon, const QDict *qdict)
13224a87 1406{
d54908a5 1407 int button_state = qdict_get_int(qdict, "button_state");
13224a87
FB
1408 mouse_button_state = button_state;
1409 kbd_mouse_event(0, 0, 0, mouse_button_state);
1410}
1411
aa93e39c 1412static void do_ioport_read(Monitor *mon, const QDict *qdict)
3440557b 1413{
aa93e39c
LC
1414 int size = qdict_get_int(qdict, "size");
1415 int addr = qdict_get_int(qdict, "addr");
1416 int has_index = qdict_haskey(qdict, "index");
3440557b
FB
1417 uint32_t val;
1418 int suffix;
1419
1420 if (has_index) {
aa93e39c 1421 int index = qdict_get_int(qdict, "index");
afcea8cb 1422 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
3440557b
FB
1423 addr++;
1424 }
1425 addr &= 0xffff;
1426
1427 switch(size) {
1428 default:
1429 case 1:
afcea8cb 1430 val = cpu_inb(addr);
3440557b
FB
1431 suffix = 'b';
1432 break;
1433 case 2:
afcea8cb 1434 val = cpu_inw(addr);
3440557b
FB
1435 suffix = 'w';
1436 break;
1437 case 4:
afcea8cb 1438 val = cpu_inl(addr);
3440557b
FB
1439 suffix = 'l';
1440 break;
1441 }
376253ec
AL
1442 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1443 suffix, addr, size * 2, val);
3440557b 1444}
a3a91a35 1445
1bd1442e 1446static void do_ioport_write(Monitor *mon, const QDict *qdict)
f114784f 1447{
1bd1442e
LC
1448 int size = qdict_get_int(qdict, "size");
1449 int addr = qdict_get_int(qdict, "addr");
1450 int val = qdict_get_int(qdict, "val");
1451
f114784f
JK
1452 addr &= IOPORTS_MASK;
1453
1454 switch (size) {
1455 default:
1456 case 1:
afcea8cb 1457 cpu_outb(addr, val);
f114784f
JK
1458 break;
1459 case 2:
afcea8cb 1460 cpu_outw(addr, val);
f114784f
JK
1461 break;
1462 case 4:
afcea8cb 1463 cpu_outl(addr, val);
f114784f
JK
1464 break;
1465 }
1466}
1467
d54908a5 1468static void do_boot_set(Monitor *mon, const QDict *qdict)
0ecdffbb
AJ
1469{
1470 int res;
d54908a5 1471 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
0ecdffbb 1472
76e30d0f
JK
1473 res = qemu_boot_set(bootdevice);
1474 if (res == 0) {
1475 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1476 } else if (res > 0) {
1477 monitor_printf(mon, "setting boot device list failed\n");
0ecdffbb 1478 } else {
376253ec
AL
1479 monitor_printf(mon, "no function defined to set boot device list for "
1480 "this architecture\n");
0ecdffbb
AJ
1481 }
1482}
1483
b86bda5b 1484#if defined(TARGET_I386)
a8170e5e
AK
1485static void print_pte(Monitor *mon, hwaddr addr,
1486 hwaddr pte,
1487 hwaddr mask)
b86bda5b 1488{
d65aaf37
BS
1489#ifdef TARGET_X86_64
1490 if (addr & (1ULL << 47)) {
1491 addr |= -1LL << 48;
1492 }
1493#endif
1494 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1495 " %c%c%c%c%c%c%c%c%c\n",
376253ec
AL
1496 addr,
1497 pte & mask,
d65aaf37 1498 pte & PG_NX_MASK ? 'X' : '-',
376253ec
AL
1499 pte & PG_GLOBAL_MASK ? 'G' : '-',
1500 pte & PG_PSE_MASK ? 'P' : '-',
1501 pte & PG_DIRTY_MASK ? 'D' : '-',
1502 pte & PG_ACCESSED_MASK ? 'A' : '-',
1503 pte & PG_PCD_MASK ? 'C' : '-',
1504 pte & PG_PWT_MASK ? 'T' : '-',
1505 pte & PG_USER_MASK ? 'U' : '-',
1506 pte & PG_RW_MASK ? 'W' : '-');
b86bda5b
FB
1507}
1508
9349b4f9 1509static void tlb_info_32(Monitor *mon, CPUArchState *env)
b86bda5b 1510{
94ac5cd2 1511 unsigned int l1, l2;
b86bda5b
FB
1512 uint32_t pgd, pde, pte;
1513
b86bda5b
FB
1514 pgd = env->cr[3] & ~0xfff;
1515 for(l1 = 0; l1 < 1024; l1++) {
b8b79323 1516 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
b86bda5b
FB
1517 pde = le32_to_cpu(pde);
1518 if (pde & PG_PRESENT_MASK) {
1519 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
d65aaf37
BS
1520 /* 4M pages */
1521 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
b86bda5b
FB
1522 } else {
1523 for(l2 = 0; l2 < 1024; l2++) {
b8b79323 1524 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
b86bda5b
FB
1525 pte = le32_to_cpu(pte);
1526 if (pte & PG_PRESENT_MASK) {
376253ec 1527 print_pte(mon, (l1 << 22) + (l2 << 12),
5fafdf24 1528 pte & ~PG_PSE_MASK,
b86bda5b
FB
1529 ~0xfff);
1530 }
1531 }
1532 }
1533 }
1534 }
1535}
1536
9349b4f9 1537static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
d65aaf37 1538{
94ac5cd2 1539 unsigned int l1, l2, l3;
d65aaf37
BS
1540 uint64_t pdpe, pde, pte;
1541 uint64_t pdp_addr, pd_addr, pt_addr;
1542
1543 pdp_addr = env->cr[3] & ~0x1f;
1544 for (l1 = 0; l1 < 4; l1++) {
b8b79323 1545 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
d65aaf37
BS
1546 pdpe = le64_to_cpu(pdpe);
1547 if (pdpe & PG_PRESENT_MASK) {
1548 pd_addr = pdpe & 0x3fffffffff000ULL;
1549 for (l2 = 0; l2 < 512; l2++) {
b8b79323 1550 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
d65aaf37
BS
1551 pde = le64_to_cpu(pde);
1552 if (pde & PG_PRESENT_MASK) {
1553 if (pde & PG_PSE_MASK) {
1554 /* 2M pages with PAE, CR4.PSE is ignored */
1555 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
a8170e5e 1556 ~((hwaddr)(1 << 20) - 1));
d65aaf37
BS
1557 } else {
1558 pt_addr = pde & 0x3fffffffff000ULL;
1559 for (l3 = 0; l3 < 512; l3++) {
b8b79323 1560 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
d65aaf37
BS
1561 pte = le64_to_cpu(pte);
1562 if (pte & PG_PRESENT_MASK) {
1563 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1564 + (l3 << 12),
1565 pte & ~PG_PSE_MASK,
a8170e5e 1566 ~(hwaddr)0xfff);
d65aaf37
BS
1567 }
1568 }
1569 }
1570 }
1571 }
1572 }
1573 }
1574}
1575
1576#ifdef TARGET_X86_64
9349b4f9 1577static void tlb_info_64(Monitor *mon, CPUArchState *env)
d65aaf37
BS
1578{
1579 uint64_t l1, l2, l3, l4;
1580 uint64_t pml4e, pdpe, pde, pte;
1581 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1582
1583 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1584 for (l1 = 0; l1 < 512; l1++) {
b8b79323 1585 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
d65aaf37
BS
1586 pml4e = le64_to_cpu(pml4e);
1587 if (pml4e & PG_PRESENT_MASK) {
1588 pdp_addr = pml4e & 0x3fffffffff000ULL;
1589 for (l2 = 0; l2 < 512; l2++) {
b8b79323 1590 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
d65aaf37
BS
1591 pdpe = le64_to_cpu(pdpe);
1592 if (pdpe & PG_PRESENT_MASK) {
1593 if (pdpe & PG_PSE_MASK) {
1594 /* 1G pages, CR4.PSE is ignored */
1595 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1596 0x3ffffc0000000ULL);
1597 } else {
1598 pd_addr = pdpe & 0x3fffffffff000ULL;
1599 for (l3 = 0; l3 < 512; l3++) {
b8b79323 1600 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
d65aaf37
BS
1601 pde = le64_to_cpu(pde);
1602 if (pde & PG_PRESENT_MASK) {
1603 if (pde & PG_PSE_MASK) {
1604 /* 2M pages, CR4.PSE is ignored */
1605 print_pte(mon, (l1 << 39) + (l2 << 30) +
1606 (l3 << 21), pde,
1607 0x3ffffffe00000ULL);
1608 } else {
1609 pt_addr = pde & 0x3fffffffff000ULL;
1610 for (l4 = 0; l4 < 512; l4++) {
1611 cpu_physical_memory_read(pt_addr
1612 + l4 * 8,
b8b79323 1613 &pte, 8);
d65aaf37
BS
1614 pte = le64_to_cpu(pte);
1615 if (pte & PG_PRESENT_MASK) {
1616 print_pte(mon, (l1 << 39) +
1617 (l2 << 30) +
1618 (l3 << 21) + (l4 << 12),
1619 pte & ~PG_PSE_MASK,
1620 0x3fffffffff000ULL);
1621 }
1622 }
1623 }
1624 }
1625 }
1626 }
1627 }
1628 }
1629 }
1630 }
1631}
1632#endif
1633
84f2d0ea 1634static void tlb_info(Monitor *mon, const QDict *qdict)
d65aaf37 1635{
9349b4f9 1636 CPUArchState *env;
d65aaf37
BS
1637
1638 env = mon_get_cpu();
1639
1640 if (!(env->cr[0] & CR0_PG_MASK)) {
1641 monitor_printf(mon, "PG disabled\n");
1642 return;
1643 }
1644 if (env->cr[4] & CR4_PAE_MASK) {
1645#ifdef TARGET_X86_64
1646 if (env->hflags & HF_LMA_MASK) {
1647 tlb_info_64(mon, env);
1648 } else
1649#endif
1650 {
1651 tlb_info_pae32(mon, env);
1652 }
1653 } else {
1654 tlb_info_32(mon, env);
1655 }
1656}
1657
a8170e5e 1658static void mem_print(Monitor *mon, hwaddr *pstart,
1b3cba6e 1659 int *plast_prot,
a8170e5e 1660 hwaddr end, int prot)
b86bda5b 1661{
9746b15b
FB
1662 int prot1;
1663 prot1 = *plast_prot;
1664 if (prot != prot1) {
b86bda5b 1665 if (*pstart != -1) {
1b3cba6e
BS
1666 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1667 TARGET_FMT_plx " %c%c%c\n",
376253ec
AL
1668 *pstart, end, end - *pstart,
1669 prot1 & PG_USER_MASK ? 'u' : '-',
1670 'r',
1671 prot1 & PG_RW_MASK ? 'w' : '-');
b86bda5b
FB
1672 }
1673 if (prot != 0)
1674 *pstart = end;
1675 else
1676 *pstart = -1;
1677 *plast_prot = prot;
1678 }
1679}
1680
9349b4f9 1681static void mem_info_32(Monitor *mon, CPUArchState *env)
b86bda5b 1682{
b49ca72d
AC
1683 unsigned int l1, l2;
1684 int prot, last_prot;
1b3cba6e 1685 uint32_t pgd, pde, pte;
a8170e5e 1686 hwaddr start, end;
6a00d601 1687
b86bda5b
FB
1688 pgd = env->cr[3] & ~0xfff;
1689 last_prot = 0;
1690 start = -1;
1691 for(l1 = 0; l1 < 1024; l1++) {
b8b79323 1692 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
b86bda5b
FB
1693 pde = le32_to_cpu(pde);
1694 end = l1 << 22;
1695 if (pde & PG_PRESENT_MASK) {
1696 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1697 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
376253ec 1698 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
1699 } else {
1700 for(l2 = 0; l2 < 1024; l2++) {
b8b79323 1701 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
b86bda5b
FB
1702 pte = le32_to_cpu(pte);
1703 end = (l1 << 22) + (l2 << 12);
1704 if (pte & PG_PRESENT_MASK) {
c76c8416
AC
1705 prot = pte & pde &
1706 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
b86bda5b
FB
1707 } else {
1708 prot = 0;
1709 }
376253ec 1710 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
1711 }
1712 }
1713 } else {
1714 prot = 0;
376253ec 1715 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
1716 }
1717 }
8a94b8ca 1718 /* Flush last range */
a8170e5e 1719 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
b86bda5b 1720}
1b3cba6e 1721
9349b4f9 1722static void mem_info_pae32(Monitor *mon, CPUArchState *env)
1b3cba6e 1723{
b49ca72d
AC
1724 unsigned int l1, l2, l3;
1725 int prot, last_prot;
1b3cba6e
BS
1726 uint64_t pdpe, pde, pte;
1727 uint64_t pdp_addr, pd_addr, pt_addr;
a8170e5e 1728 hwaddr start, end;
1b3cba6e
BS
1729
1730 pdp_addr = env->cr[3] & ~0x1f;
1731 last_prot = 0;
1732 start = -1;
1733 for (l1 = 0; l1 < 4; l1++) {
b8b79323 1734 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1b3cba6e
BS
1735 pdpe = le64_to_cpu(pdpe);
1736 end = l1 << 30;
1737 if (pdpe & PG_PRESENT_MASK) {
1738 pd_addr = pdpe & 0x3fffffffff000ULL;
1739 for (l2 = 0; l2 < 512; l2++) {
b8b79323 1740 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1b3cba6e
BS
1741 pde = le64_to_cpu(pde);
1742 end = (l1 << 30) + (l2 << 21);
1743 if (pde & PG_PRESENT_MASK) {
1744 if (pde & PG_PSE_MASK) {
1745 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1746 PG_PRESENT_MASK);
1747 mem_print(mon, &start, &last_prot, end, prot);
1748 } else {
1749 pt_addr = pde & 0x3fffffffff000ULL;
1750 for (l3 = 0; l3 < 512; l3++) {
b8b79323 1751 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1b3cba6e
BS
1752 pte = le64_to_cpu(pte);
1753 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1754 if (pte & PG_PRESENT_MASK) {
c76c8416
AC
1755 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1756 PG_PRESENT_MASK);
1b3cba6e
BS
1757 } else {
1758 prot = 0;
1759 }
1760 mem_print(mon, &start, &last_prot, end, prot);
1761 }
1762 }
1763 } else {
1764 prot = 0;
1765 mem_print(mon, &start, &last_prot, end, prot);
1766 }
1767 }
1768 } else {
1769 prot = 0;
1770 mem_print(mon, &start, &last_prot, end, prot);
1771 }
1772 }
8a94b8ca 1773 /* Flush last range */
a8170e5e 1774 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1b3cba6e
BS
1775}
1776
1777
1778#ifdef TARGET_X86_64
9349b4f9 1779static void mem_info_64(Monitor *mon, CPUArchState *env)
1b3cba6e
BS
1780{
1781 int prot, last_prot;
1782 uint64_t l1, l2, l3, l4;
1783 uint64_t pml4e, pdpe, pde, pte;
1784 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1785
1786 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1787 last_prot = 0;
1788 start = -1;
1789 for (l1 = 0; l1 < 512; l1++) {
b8b79323 1790 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1b3cba6e
BS
1791 pml4e = le64_to_cpu(pml4e);
1792 end = l1 << 39;
1793 if (pml4e & PG_PRESENT_MASK) {
1794 pdp_addr = pml4e & 0x3fffffffff000ULL;
1795 for (l2 = 0; l2 < 512; l2++) {
b8b79323 1796 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1b3cba6e
BS
1797 pdpe = le64_to_cpu(pdpe);
1798 end = (l1 << 39) + (l2 << 30);
1799 if (pdpe & PG_PRESENT_MASK) {
1800 if (pdpe & PG_PSE_MASK) {
2d5b5074
BS
1801 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1802 PG_PRESENT_MASK);
c76c8416 1803 prot &= pml4e;
1b3cba6e
BS
1804 mem_print(mon, &start, &last_prot, end, prot);
1805 } else {
1806 pd_addr = pdpe & 0x3fffffffff000ULL;
1807 for (l3 = 0; l3 < 512; l3++) {
b8b79323 1808 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1b3cba6e
BS
1809 pde = le64_to_cpu(pde);
1810 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1811 if (pde & PG_PRESENT_MASK) {
1812 if (pde & PG_PSE_MASK) {
1813 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1814 PG_PRESENT_MASK);
c76c8416 1815 prot &= pml4e & pdpe;
1b3cba6e
BS
1816 mem_print(mon, &start, &last_prot, end, prot);
1817 } else {
1818 pt_addr = pde & 0x3fffffffff000ULL;
1819 for (l4 = 0; l4 < 512; l4++) {
1820 cpu_physical_memory_read(pt_addr
1821 + l4 * 8,
b8b79323 1822 &pte, 8);
1b3cba6e
BS
1823 pte = le64_to_cpu(pte);
1824 end = (l1 << 39) + (l2 << 30) +
1825 (l3 << 21) + (l4 << 12);
1826 if (pte & PG_PRESENT_MASK) {
1827 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1828 PG_PRESENT_MASK);
c76c8416 1829 prot &= pml4e & pdpe & pde;
1b3cba6e
BS
1830 } else {
1831 prot = 0;
1832 }
1833 mem_print(mon, &start, &last_prot, end, prot);
1834 }
1835 }
1836 } else {
1837 prot = 0;
1838 mem_print(mon, &start, &last_prot, end, prot);
1839 }
1840 }
1841 }
1842 } else {
1843 prot = 0;
1844 mem_print(mon, &start, &last_prot, end, prot);
1845 }
1846 }
1847 } else {
1848 prot = 0;
1849 mem_print(mon, &start, &last_prot, end, prot);
1850 }
1851 }
8a94b8ca 1852 /* Flush last range */
a8170e5e 1853 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
1b3cba6e
BS
1854}
1855#endif
1856
84f2d0ea 1857static void mem_info(Monitor *mon, const QDict *qdict)
1b3cba6e 1858{
9349b4f9 1859 CPUArchState *env;
1b3cba6e
BS
1860
1861 env = mon_get_cpu();
1862
1863 if (!(env->cr[0] & CR0_PG_MASK)) {
1864 monitor_printf(mon, "PG disabled\n");
1865 return;
1866 }
1867 if (env->cr[4] & CR4_PAE_MASK) {
1868#ifdef TARGET_X86_64
1869 if (env->hflags & HF_LMA_MASK) {
1870 mem_info_64(mon, env);
1871 } else
1872#endif
1873 {
1874 mem_info_pae32(mon, env);
1875 }
1876 } else {
1877 mem_info_32(mon, env);
1878 }
1879}
b86bda5b
FB
1880#endif
1881
7c664e2f
AJ
1882#if defined(TARGET_SH4)
1883
376253ec 1884static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
7c664e2f 1885{
376253ec
AL
1886 monitor_printf(mon, " tlb%i:\t"
1887 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1888 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1889 "dirty=%hhu writethrough=%hhu\n",
1890 idx,
1891 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1892 tlb->v, tlb->sh, tlb->c, tlb->pr,
1893 tlb->d, tlb->wt);
7c664e2f
AJ
1894}
1895
84f2d0ea 1896static void tlb_info(Monitor *mon, const QDict *qdict)
7c664e2f 1897{
9349b4f9 1898 CPUArchState *env = mon_get_cpu();
7c664e2f
AJ
1899 int i;
1900
376253ec 1901 monitor_printf (mon, "ITLB:\n");
7c664e2f 1902 for (i = 0 ; i < ITLB_SIZE ; i++)
376253ec
AL
1903 print_tlb (mon, i, &env->itlb[i]);
1904 monitor_printf (mon, "UTLB:\n");
7c664e2f 1905 for (i = 0 ; i < UTLB_SIZE ; i++)
376253ec 1906 print_tlb (mon, i, &env->utlb[i]);
7c664e2f
AJ
1907}
1908
1909#endif
1910
692f737c 1911#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
84f2d0ea 1912static void tlb_info(Monitor *mon, const QDict *qdict)
d41160a3 1913{
9349b4f9 1914 CPUArchState *env1 = mon_get_cpu();
d41160a3
BS
1915
1916 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1917}
1918#endif
1919
84f2d0ea 1920static void do_info_mtree(Monitor *mon, const QDict *qdict)
314e2987
BS
1921{
1922 mtree_info((fprintf_function)monitor_printf, mon);
1923}
1924
84f2d0ea 1925static void do_info_numa(Monitor *mon, const QDict *qdict)
030ea37b 1926{
b28b6230 1927 int i;
1b1ed8dc 1928 CPUState *cpu;
030ea37b
AL
1929
1930 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1931 for (i = 0; i < nb_numa_nodes; i++) {
1932 monitor_printf(mon, "node %d cpus:", i);
182735ef 1933 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1b1ed8dc 1934 if (cpu->numa_node == i) {
55e5c285 1935 monitor_printf(mon, " %d", cpu->cpu_index);
030ea37b
AL
1936 }
1937 }
1938 monitor_printf(mon, "\n");
1939 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1940 node_mem[i] >> 20);
1941 }
1942}
1943
5f1ce948
FB
1944#ifdef CONFIG_PROFILER
1945
e9a6625e
AJ
1946int64_t qemu_time;
1947int64_t dev_time;
1948
84f2d0ea 1949static void do_info_profile(Monitor *mon, const QDict *qdict)
5f1ce948
FB
1950{
1951 int64_t total;
1952 total = qemu_time;
1953 if (total == 0)
1954 total = 1;
376253ec 1955 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
6ee093c9 1956 dev_time, dev_time / (double)get_ticks_per_sec());
376253ec 1957 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
6ee093c9 1958 qemu_time, qemu_time / (double)get_ticks_per_sec());
5f1ce948 1959 qemu_time = 0;
5f1ce948 1960 dev_time = 0;
5f1ce948
FB
1961}
1962#else
84f2d0ea 1963static void do_info_profile(Monitor *mon, const QDict *qdict)
5f1ce948 1964{
376253ec 1965 monitor_printf(mon, "Internal profiler not compiled\n");
5f1ce948
FB
1966}
1967#endif
1968
ec36b695 1969/* Capture support */
72cf2d4f 1970static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
ec36b695 1971
84f2d0ea 1972static void do_info_capture(Monitor *mon, const QDict *qdict)
ec36b695
FB
1973{
1974 int i;
1975 CaptureState *s;
1976
1977 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
376253ec 1978 monitor_printf(mon, "[%d]: ", i);
ec36b695
FB
1979 s->ops.info (s->opaque);
1980 }
1981}
1982
d54908a5 1983static void do_stop_capture(Monitor *mon, const QDict *qdict)
ec36b695
FB
1984{
1985 int i;
d54908a5 1986 int n = qdict_get_int(qdict, "n");
ec36b695
FB
1987 CaptureState *s;
1988
1989 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1990 if (i == n) {
1991 s->ops.destroy (s->opaque);
72cf2d4f 1992 QLIST_REMOVE (s, entries);
7267c094 1993 g_free (s);
ec36b695
FB
1994 return;
1995 }
1996 }
1997}
1998
c1925484
LC
1999static void do_wav_capture(Monitor *mon, const QDict *qdict)
2000{
2001 const char *path = qdict_get_str(qdict, "path");
2002 int has_freq = qdict_haskey(qdict, "freq");
2003 int freq = qdict_get_try_int(qdict, "freq", -1);
2004 int has_bits = qdict_haskey(qdict, "bits");
2005 int bits = qdict_get_try_int(qdict, "bits", -1);
2006 int has_channels = qdict_haskey(qdict, "nchannels");
2007 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
ec36b695
FB
2008 CaptureState *s;
2009
7267c094 2010 s = g_malloc0 (sizeof (*s));
ec36b695
FB
2011
2012 freq = has_freq ? freq : 44100;
2013 bits = has_bits ? bits : 16;
2014 nchannels = has_channels ? nchannels : 2;
2015
2016 if (wav_start_capture (s, path, freq, bits, nchannels)) {
d00b2618 2017 monitor_printf(mon, "Failed to add wave capture\n");
7267c094 2018 g_free (s);
d00b2618 2019 return;
ec36b695 2020 }
72cf2d4f 2021 QLIST_INSERT_HEAD (&capture_head, s, entries);
ec36b695 2022}
ec36b695 2023
15dfcd45 2024static qemu_acl *find_acl(Monitor *mon, const char *name)
76655d6d 2025{
15dfcd45 2026 qemu_acl *acl = qemu_acl_find(name);
76655d6d 2027
76655d6d 2028 if (!acl) {
15dfcd45 2029 monitor_printf(mon, "acl: unknown list '%s'\n", name);
76655d6d 2030 }
15dfcd45
JK
2031 return acl;
2032}
2033
d54908a5 2034static void do_acl_show(Monitor *mon, const QDict *qdict)
15dfcd45 2035{
d54908a5 2036 const char *aclname = qdict_get_str(qdict, "aclname");
15dfcd45
JK
2037 qemu_acl *acl = find_acl(mon, aclname);
2038 qemu_acl_entry *entry;
2039 int i = 0;
76655d6d 2040
15dfcd45 2041 if (acl) {
28a76be8 2042 monitor_printf(mon, "policy: %s\n",
76655d6d 2043 acl->defaultDeny ? "deny" : "allow");
72cf2d4f 2044 QTAILQ_FOREACH(entry, &acl->entries, next) {
28a76be8
AL
2045 i++;
2046 monitor_printf(mon, "%d: %s %s\n", i,
15dfcd45 2047 entry->deny ? "deny" : "allow", entry->match);
28a76be8 2048 }
15dfcd45
JK
2049 }
2050}
2051
d54908a5 2052static void do_acl_reset(Monitor *mon, const QDict *qdict)
15dfcd45 2053{
d54908a5 2054 const char *aclname = qdict_get_str(qdict, "aclname");
15dfcd45
JK
2055 qemu_acl *acl = find_acl(mon, aclname);
2056
2057 if (acl) {
28a76be8
AL
2058 qemu_acl_reset(acl);
2059 monitor_printf(mon, "acl: removed all rules\n");
15dfcd45
JK
2060 }
2061}
2062
f18c16de 2063static void do_acl_policy(Monitor *mon, const QDict *qdict)
15dfcd45 2064{
f18c16de
LC
2065 const char *aclname = qdict_get_str(qdict, "aclname");
2066 const char *policy = qdict_get_str(qdict, "policy");
15dfcd45 2067 qemu_acl *acl = find_acl(mon, aclname);
28a76be8 2068
15dfcd45
JK
2069 if (acl) {
2070 if (strcmp(policy, "allow") == 0) {
28a76be8
AL
2071 acl->defaultDeny = 0;
2072 monitor_printf(mon, "acl: policy set to 'allow'\n");
15dfcd45 2073 } else if (strcmp(policy, "deny") == 0) {
28a76be8
AL
2074 acl->defaultDeny = 1;
2075 monitor_printf(mon, "acl: policy set to 'deny'\n");
2076 } else {
15dfcd45
JK
2077 monitor_printf(mon, "acl: unknown policy '%s', "
2078 "expected 'deny' or 'allow'\n", policy);
28a76be8 2079 }
15dfcd45
JK
2080 }
2081}
28a76be8 2082
1bd1442e 2083static void do_acl_add(Monitor *mon, const QDict *qdict)
15dfcd45 2084{
1bd1442e
LC
2085 const char *aclname = qdict_get_str(qdict, "aclname");
2086 const char *match = qdict_get_str(qdict, "match");
2087 const char *policy = qdict_get_str(qdict, "policy");
2088 int has_index = qdict_haskey(qdict, "index");
2089 int index = qdict_get_try_int(qdict, "index", -1);
15dfcd45
JK
2090 qemu_acl *acl = find_acl(mon, aclname);
2091 int deny, ret;
2092
2093 if (acl) {
2094 if (strcmp(policy, "allow") == 0) {
2095 deny = 0;
2096 } else if (strcmp(policy, "deny") == 0) {
2097 deny = 1;
2098 } else {
2099 monitor_printf(mon, "acl: unknown policy '%s', "
2100 "expected 'deny' or 'allow'\n", policy);
28a76be8
AL
2101 return;
2102 }
28a76be8
AL
2103 if (has_index)
2104 ret = qemu_acl_insert(acl, deny, match, index);
2105 else
2106 ret = qemu_acl_append(acl, deny, match);
2107 if (ret < 0)
2108 monitor_printf(mon, "acl: unable to add acl entry\n");
2109 else
2110 monitor_printf(mon, "acl: added rule at position %d\n", ret);
15dfcd45
JK
2111 }
2112}
28a76be8 2113
f18c16de 2114static void do_acl_remove(Monitor *mon, const QDict *qdict)
15dfcd45 2115{
f18c16de
LC
2116 const char *aclname = qdict_get_str(qdict, "aclname");
2117 const char *match = qdict_get_str(qdict, "match");
15dfcd45
JK
2118 qemu_acl *acl = find_acl(mon, aclname);
2119 int ret;
28a76be8 2120
15dfcd45 2121 if (acl) {
28a76be8
AL
2122 ret = qemu_acl_remove(acl, match);
2123 if (ret < 0)
2124 monitor_printf(mon, "acl: no matching acl entry\n");
2125 else
2126 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
76655d6d
AL
2127 }
2128}
2129
79c4f6b0 2130#if defined(TARGET_I386)
37b7ad48 2131static void do_inject_mce(Monitor *mon, const QDict *qdict)
79c4f6b0 2132{
8c5cf3b6 2133 X86CPU *cpu;
55e5c285 2134 CPUState *cs;
37b7ad48
LC
2135 int cpu_index = qdict_get_int(qdict, "cpu_index");
2136 int bank = qdict_get_int(qdict, "bank");
2137 uint64_t status = qdict_get_int(qdict, "status");
2138 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2139 uint64_t addr = qdict_get_int(qdict, "addr");
2140 uint64_t misc = qdict_get_int(qdict, "misc");
747461c7 2141 int flags = MCE_INJECT_UNCOND_AO;
79c4f6b0 2142
747461c7
JK
2143 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2144 flags |= MCE_INJECT_BROADCAST;
2145 }
c51a944b
AF
2146 cs = qemu_get_cpu(cpu_index);
2147 if (cs != NULL) {
2148 cpu = X86_CPU(cs);
2149 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2150 flags);
31ce5e0c 2151 }
79c4f6b0
HY
2152}
2153#endif
2154
208c9d1b 2155void qmp_getfd(const char *fdname, Error **errp)
f07918fd 2156{
c227f099 2157 mon_fd_t *monfd;
f07918fd
MM
2158 int fd;
2159
208c9d1b 2160 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
f07918fd 2161 if (fd == -1) {
208c9d1b
CB
2162 error_set(errp, QERR_FD_NOT_SUPPLIED);
2163 return;
f07918fd
MM
2164 }
2165
2166 if (qemu_isdigit(fdname[0])) {
208c9d1b
CB
2167 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2168 "a name not starting with a digit");
2169 return;
f07918fd
MM
2170 }
2171
208c9d1b 2172 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
2173 if (strcmp(monfd->name, fdname) != 0) {
2174 continue;
2175 }
2176
2177 close(monfd->fd);
2178 monfd->fd = fd;
208c9d1b 2179 return;
f07918fd
MM
2180 }
2181
7267c094
AL
2182 monfd = g_malloc0(sizeof(mon_fd_t));
2183 monfd->name = g_strdup(fdname);
f07918fd
MM
2184 monfd->fd = fd;
2185
208c9d1b 2186 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
f07918fd
MM
2187}
2188
208c9d1b 2189void qmp_closefd(const char *fdname, Error **errp)
f07918fd 2190{
c227f099 2191 mon_fd_t *monfd;
f07918fd 2192
208c9d1b 2193 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
f07918fd
MM
2194 if (strcmp(monfd->name, fdname) != 0) {
2195 continue;
2196 }
2197
72cf2d4f 2198 QLIST_REMOVE(monfd, next);
f07918fd 2199 close(monfd->fd);
7267c094
AL
2200 g_free(monfd->name);
2201 g_free(monfd);
208c9d1b 2202 return;
f07918fd
MM
2203 }
2204
208c9d1b 2205 error_set(errp, QERR_FD_NOT_FOUND, fdname);
f07918fd
MM
2206}
2207
d54908a5 2208static void do_loadvm(Monitor *mon, const QDict *qdict)
c8d41b2c 2209{
1354869c 2210 int saved_vm_running = runstate_is_running();
d54908a5 2211 const char *name = qdict_get_str(qdict, "name");
c8d41b2c 2212
0461d5a6 2213 vm_stop(RUN_STATE_RESTORE_VM);
c8d41b2c 2214
f0aa7a8b 2215 if (load_vmstate(name) == 0 && saved_vm_running) {
c8d41b2c 2216 vm_start();
f0aa7a8b 2217 }
c8d41b2c
JQ
2218}
2219
a9940fc4 2220int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
7768e04c 2221{
c227f099 2222 mon_fd_t *monfd;
7768e04c 2223
72cf2d4f 2224 QLIST_FOREACH(monfd, &mon->fds, next) {
7768e04c
MM
2225 int fd;
2226
2227 if (strcmp(monfd->name, fdname) != 0) {
2228 continue;
2229 }
2230
2231 fd = monfd->fd;
2232
2233 /* caller takes ownership of fd */
72cf2d4f 2234 QLIST_REMOVE(monfd, next);
7267c094
AL
2235 g_free(monfd->name);
2236 g_free(monfd);
7768e04c
MM
2237
2238 return fd;
2239 }
2240
a9940fc4 2241 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
7768e04c
MM
2242 return -1;
2243}
2244
ba1c048a
CB
2245static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2246{
2247 MonFdsetFd *mon_fdset_fd;
2248 MonFdsetFd *mon_fdset_fd_next;
2249
2250 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
ebe52b59
CB
2251 if ((mon_fdset_fd->removed ||
2252 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2253 runstate_is_running()) {
ba1c048a
CB
2254 close(mon_fdset_fd->fd);
2255 g_free(mon_fdset_fd->opaque);
2256 QLIST_REMOVE(mon_fdset_fd, next);
2257 g_free(mon_fdset_fd);
2258 }
2259 }
2260
adb696f3 2261 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
ba1c048a
CB
2262 QLIST_REMOVE(mon_fdset, next);
2263 g_free(mon_fdset);
2264 }
2265}
2266
efb87c16
CB
2267static void monitor_fdsets_cleanup(void)
2268{
2269 MonFdset *mon_fdset;
2270 MonFdset *mon_fdset_next;
2271
2272 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2273 monitor_fdset_cleanup(mon_fdset);
2274 }
2275}
2276
ba1c048a
CB
2277AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2278 const char *opaque, Error **errp)
2279{
2280 int fd;
2281 Monitor *mon = cur_mon;
ba1c048a
CB
2282 AddfdInfo *fdinfo;
2283
2284 fd = qemu_chr_fe_get_msgfd(mon->chr);
2285 if (fd == -1) {
2286 error_set(errp, QERR_FD_NOT_SUPPLIED);
2287 goto error;
2288 }
2289
e446f70d
CB
2290 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2291 has_opaque, opaque, errp);
2292 if (fdinfo) {
2293 return fdinfo;
ba1c048a 2294 }
ba1c048a
CB
2295
2296error:
2297 if (fd != -1) {
2298 close(fd);
2299 }
2300 return NULL;
2301}
2302
2303void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2304{
2305 MonFdset *mon_fdset;
2306 MonFdsetFd *mon_fdset_fd;
2307 char fd_str[60];
2308
2309 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2310 if (mon_fdset->id != fdset_id) {
2311 continue;
2312 }
2313 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2314 if (has_fd) {
2315 if (mon_fdset_fd->fd != fd) {
2316 continue;
2317 }
2318 mon_fdset_fd->removed = true;
2319 break;
2320 } else {
2321 mon_fdset_fd->removed = true;
2322 }
2323 }
2324 if (has_fd && !mon_fdset_fd) {
2325 goto error;
2326 }
2327 monitor_fdset_cleanup(mon_fdset);
2328 return;
2329 }
2330
2331error:
2332 if (has_fd) {
2333 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2334 fdset_id, fd);
2335 } else {
2336 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2337 }
2338 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2339}
2340
2341FdsetInfoList *qmp_query_fdsets(Error **errp)
2342{
2343 MonFdset *mon_fdset;
2344 MonFdsetFd *mon_fdset_fd;
2345 FdsetInfoList *fdset_list = NULL;
2346
2347 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2348 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2349 FdsetFdInfoList *fdsetfd_list = NULL;
2350
2351 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2352 fdset_info->value->fdset_id = mon_fdset->id;
2353
2354 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2355 FdsetFdInfoList *fdsetfd_info;
2356
2357 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2358 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2359 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2360 if (mon_fdset_fd->opaque) {
2361 fdsetfd_info->value->has_opaque = true;
2362 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2363 } else {
2364 fdsetfd_info->value->has_opaque = false;
2365 }
2366
2367 fdsetfd_info->next = fdsetfd_list;
2368 fdsetfd_list = fdsetfd_info;
2369 }
2370
2371 fdset_info->value->fds = fdsetfd_list;
2372
2373 fdset_info->next = fdset_list;
2374 fdset_list = fdset_info;
2375 }
2376
2377 return fdset_list;
2378}
2379
e446f70d
CB
2380AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2381 bool has_opaque, const char *opaque,
2382 Error **errp)
2383{
2384 MonFdset *mon_fdset = NULL;
2385 MonFdsetFd *mon_fdset_fd;
2386 AddfdInfo *fdinfo;
2387
2388 if (has_fdset_id) {
2389 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2390 /* Break if match found or match impossible due to ordering by ID */
2391 if (fdset_id <= mon_fdset->id) {
2392 if (fdset_id < mon_fdset->id) {
2393 mon_fdset = NULL;
2394 }
2395 break;
2396 }
2397 }
2398 }
2399
2400 if (mon_fdset == NULL) {
2401 int64_t fdset_id_prev = -1;
2402 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2403
2404 if (has_fdset_id) {
2405 if (fdset_id < 0) {
2406 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2407 "a non-negative value");
2408 return NULL;
2409 }
2410 /* Use specified fdset ID */
2411 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2412 mon_fdset_cur = mon_fdset;
2413 if (fdset_id < mon_fdset_cur->id) {
2414 break;
2415 }
2416 }
2417 } else {
2418 /* Use first available fdset ID */
2419 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2420 mon_fdset_cur = mon_fdset;
2421 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2422 fdset_id_prev = mon_fdset_cur->id;
2423 continue;
2424 }
2425 break;
2426 }
2427 }
2428
2429 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2430 if (has_fdset_id) {
2431 mon_fdset->id = fdset_id;
2432 } else {
2433 mon_fdset->id = fdset_id_prev + 1;
2434 }
2435
2436 /* The fdset list is ordered by fdset ID */
2437 if (!mon_fdset_cur) {
2438 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2439 } else if (mon_fdset->id < mon_fdset_cur->id) {
2440 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2441 } else {
2442 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2443 }
2444 }
2445
2446 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2447 mon_fdset_fd->fd = fd;
2448 mon_fdset_fd->removed = false;
2449 if (has_opaque) {
2450 mon_fdset_fd->opaque = g_strdup(opaque);
2451 }
2452 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2453
2454 fdinfo = g_malloc0(sizeof(*fdinfo));
2455 fdinfo->fdset_id = mon_fdset->id;
2456 fdinfo->fd = mon_fdset_fd->fd;
2457
2458 return fdinfo;
2459}
2460
adb696f3
CB
2461int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2462{
b2dc64c3 2463#ifndef _WIN32
adb696f3
CB
2464 MonFdset *mon_fdset;
2465 MonFdsetFd *mon_fdset_fd;
2466 int mon_fd_flags;
2467
adb696f3
CB
2468 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2469 if (mon_fdset->id != fdset_id) {
2470 continue;
2471 }
2472 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2473 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2474 if (mon_fd_flags == -1) {
2475 return -1;
2476 }
2477
2478 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2479 return mon_fdset_fd->fd;
2480 }
2481 }
2482 errno = EACCES;
2483 return -1;
2484 }
2485#endif
2486
2487 errno = ENOENT;
2488 return -1;
2489}
2490
2491int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2492{
2493 MonFdset *mon_fdset;
2494 MonFdsetFd *mon_fdset_fd_dup;
2495
2496 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2497 if (mon_fdset->id != fdset_id) {
2498 continue;
2499 }
2500 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2501 if (mon_fdset_fd_dup->fd == dup_fd) {
2502 return -1;
2503 }
2504 }
2505 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2506 mon_fdset_fd_dup->fd = dup_fd;
2507 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2508 return 0;
2509 }
2510 return -1;
2511}
2512
2513static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2514{
2515 MonFdset *mon_fdset;
2516 MonFdsetFd *mon_fdset_fd_dup;
2517
2518 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2519 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2520 if (mon_fdset_fd_dup->fd == dup_fd) {
2521 if (remove) {
2522 QLIST_REMOVE(mon_fdset_fd_dup, next);
2523 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2524 monitor_fdset_cleanup(mon_fdset);
2525 }
2526 }
2527 return mon_fdset->id;
2528 }
2529 }
2530 }
2531 return -1;
2532}
2533
2534int monitor_fdset_dup_fd_find(int dup_fd)
2535{
2536 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2537}
2538
2539int monitor_fdset_dup_fd_remove(int dup_fd)
2540{
2541 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2542}
2543
a96ed02f
NB
2544int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2545{
2546 int fd;
a9940fc4 2547 Error *local_err = NULL;
a96ed02f
NB
2548
2549 if (!qemu_isdigit(fdname[0]) && mon) {
2550
a9940fc4 2551 fd = monitor_get_fd(mon, fdname, &local_err);
a96ed02f 2552 if (fd == -1) {
a9940fc4
PB
2553 qerror_report_err(local_err);
2554 error_free(local_err);
a96ed02f
NB
2555 return -1;
2556 }
2557 } else {
2558 fd = qemu_parse_fd(fdname);
2559 }
2560
2561 return fd;
2562}
2563
acd0a093 2564/* Please update hmp-commands.hx when adding or changing commands */
816f8925 2565static mon_cmd_t info_cmds[] = {
d7f9b689
LC
2566 {
2567 .name = "version",
2568 .args_type = "",
d7f9b689
LC
2569 .params = "",
2570 .help = "show the version of QEMU",
5f11cb00 2571 .mhandler.cmd = hmp_info_version,
d7f9b689
LC
2572 },
2573 {
2574 .name = "network",
2575 .args_type = "",
d7f9b689
LC
2576 .params = "",
2577 .help = "show the network state",
5f11cb00 2578 .mhandler.cmd = do_info_network,
d7f9b689
LC
2579 },
2580 {
2581 .name = "chardev",
2582 .args_type = "",
d7f9b689
LC
2583 .params = "",
2584 .help = "show the character devices",
5f11cb00 2585 .mhandler.cmd = hmp_info_chardev,
d7f9b689
LC
2586 },
2587 {
2588 .name = "block",
e73fe2b4
WX
2589 .args_type = "verbose:-v,device:B?",
2590 .params = "[-v] [device]",
2591 .help = "show info of one block device or all block devices "
2592 "(and details of images with -v option)",
5f11cb00 2593 .mhandler.cmd = hmp_info_block,
d7f9b689
LC
2594 },
2595 {
2596 .name = "blockstats",
2597 .args_type = "",
d7f9b689
LC
2598 .params = "",
2599 .help = "show block device statistics",
5f11cb00 2600 .mhandler.cmd = hmp_info_blockstats,
d7f9b689 2601 },
fb5458cd
SH
2602 {
2603 .name = "block-jobs",
2604 .args_type = "",
2605 .params = "",
2606 .help = "show progress of ongoing block device operations",
5f11cb00 2607 .mhandler.cmd = hmp_info_block_jobs,
fb5458cd 2608 },
d7f9b689
LC
2609 {
2610 .name = "registers",
2611 .args_type = "",
d7f9b689
LC
2612 .params = "",
2613 .help = "show the cpu registers",
5f11cb00 2614 .mhandler.cmd = do_info_registers,
d7f9b689
LC
2615 },
2616 {
2617 .name = "cpus",
2618 .args_type = "",
d7f9b689
LC
2619 .params = "",
2620 .help = "show infos for each CPU",
5f11cb00 2621 .mhandler.cmd = hmp_info_cpus,
d7f9b689
LC
2622 },
2623 {
2624 .name = "history",
2625 .args_type = "",
d7f9b689
LC
2626 .params = "",
2627 .help = "show the command line history",
5f11cb00 2628 .mhandler.cmd = do_info_history,
d7f9b689 2629 },
661f1929
JK
2630#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2631 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
d7f9b689
LC
2632 {
2633 .name = "irq",
2634 .args_type = "",
d7f9b689
LC
2635 .params = "",
2636 .help = "show the interrupts statistics (if available)",
661f1929 2637#ifdef TARGET_SPARC
5f11cb00 2638 .mhandler.cmd = sun4m_irq_info,
661f1929 2639#elif defined(TARGET_LM32)
5f11cb00 2640 .mhandler.cmd = lm32_irq_info,
661f1929 2641#else
5f11cb00 2642 .mhandler.cmd = irq_info,
661f1929 2643#endif
d7f9b689
LC
2644 },
2645 {
2646 .name = "pic",
2647 .args_type = "",
d7f9b689
LC
2648 .params = "",
2649 .help = "show i8259 (PIC) state",
661f1929 2650#ifdef TARGET_SPARC
5f11cb00 2651 .mhandler.cmd = sun4m_pic_info,
661f1929 2652#elif defined(TARGET_LM32)
5f11cb00 2653 .mhandler.cmd = lm32_do_pic_info,
661f1929 2654#else
5f11cb00 2655 .mhandler.cmd = pic_info,
661f1929 2656#endif
d7f9b689 2657 },
661f1929 2658#endif
d7f9b689
LC
2659 {
2660 .name = "pci",
2661 .args_type = "",
d7f9b689
LC
2662 .params = "",
2663 .help = "show PCI info",
5f11cb00 2664 .mhandler.cmd = hmp_info_pci,
d7f9b689 2665 },
bebabbc7 2666#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
692f737c 2667 defined(TARGET_PPC) || defined(TARGET_XTENSA)
d7f9b689
LC
2668 {
2669 .name = "tlb",
2670 .args_type = "",
d7f9b689
LC
2671 .params = "",
2672 .help = "show virtual to physical memory mappings",
5f11cb00 2673 .mhandler.cmd = tlb_info,
d7f9b689 2674 },
7c664e2f
AJ
2675#endif
2676#if defined(TARGET_I386)
d7f9b689
LC
2677 {
2678 .name = "mem",
2679 .args_type = "",
d7f9b689
LC
2680 .params = "",
2681 .help = "show the active virtual memory mappings",
5f11cb00 2682 .mhandler.cmd = mem_info,
d7f9b689 2683 },
b86bda5b 2684#endif
314e2987
BS
2685 {
2686 .name = "mtree",
2687 .args_type = "",
2688 .params = "",
2689 .help = "show memory tree",
5f11cb00 2690 .mhandler.cmd = do_info_mtree,
314e2987 2691 },
d7f9b689
LC
2692 {
2693 .name = "jit",
2694 .args_type = "",
d7f9b689
LC
2695 .params = "",
2696 .help = "show dynamic compiler info",
5f11cb00 2697 .mhandler.cmd = do_info_jit,
d7f9b689
LC
2698 },
2699 {
2700 .name = "kvm",
2701 .args_type = "",
d7f9b689
LC
2702 .params = "",
2703 .help = "show KVM information",
5f11cb00 2704 .mhandler.cmd = hmp_info_kvm,
d7f9b689
LC
2705 },
2706 {
2707 .name = "numa",
2708 .args_type = "",
d7f9b689
LC
2709 .params = "",
2710 .help = "show NUMA information",
5f11cb00 2711 .mhandler.cmd = do_info_numa,
d7f9b689
LC
2712 },
2713 {
2714 .name = "usb",
2715 .args_type = "",
d7f9b689
LC
2716 .params = "",
2717 .help = "show guest USB devices",
5f11cb00 2718 .mhandler.cmd = usb_info,
d7f9b689
LC
2719 },
2720 {
2721 .name = "usbhost",
2722 .args_type = "",
d7f9b689
LC
2723 .params = "",
2724 .help = "show host USB devices",
5f11cb00 2725 .mhandler.cmd = usb_host_info,
d7f9b689
LC
2726 },
2727 {
2728 .name = "profile",
2729 .args_type = "",
d7f9b689
LC
2730 .params = "",
2731 .help = "show profiling information",
5f11cb00 2732 .mhandler.cmd = do_info_profile,
d7f9b689
LC
2733 },
2734 {
2735 .name = "capture",
2736 .args_type = "",
d7f9b689
LC
2737 .params = "",
2738 .help = "show capture information",
5f11cb00 2739 .mhandler.cmd = do_info_capture,
d7f9b689
LC
2740 },
2741 {
2742 .name = "snapshots",
2743 .args_type = "",
d7f9b689
LC
2744 .params = "",
2745 .help = "show the currently saved VM snapshots",
5f11cb00 2746 .mhandler.cmd = do_info_snapshots,
d7f9b689
LC
2747 },
2748 {
2749 .name = "status",
2750 .args_type = "",
d7f9b689
LC
2751 .params = "",
2752 .help = "show the current VM status (running|paused)",
5f11cb00 2753 .mhandler.cmd = hmp_info_status,
d7f9b689
LC
2754 },
2755 {
2756 .name = "pcmcia",
2757 .args_type = "",
d7f9b689
LC
2758 .params = "",
2759 .help = "show guest PCMCIA status",
5f11cb00 2760 .mhandler.cmd = pcmcia_info,
d7f9b689
LC
2761 },
2762 {
2763 .name = "mice",
2764 .args_type = "",
d7f9b689
LC
2765 .params = "",
2766 .help = "show which guest mouse is receiving events",
5f11cb00 2767 .mhandler.cmd = hmp_info_mice,
d7f9b689
LC
2768 },
2769 {
2770 .name = "vnc",
2771 .args_type = "",
d7f9b689
LC
2772 .params = "",
2773 .help = "show the vnc server status",
5f11cb00 2774 .mhandler.cmd = hmp_info_vnc,
d7f9b689 2775 },
cb42a870
GH
2776#if defined(CONFIG_SPICE)
2777 {
2778 .name = "spice",
2779 .args_type = "",
2780 .params = "",
2781 .help = "show the spice server status",
5f11cb00 2782 .mhandler.cmd = hmp_info_spice,
cb42a870
GH
2783 },
2784#endif
d7f9b689
LC
2785 {
2786 .name = "name",
2787 .args_type = "",
d7f9b689
LC
2788 .params = "",
2789 .help = "show the current VM name",
5f11cb00 2790 .mhandler.cmd = hmp_info_name,
d7f9b689
LC
2791 },
2792 {
2793 .name = "uuid",
2794 .args_type = "",
d7f9b689
LC
2795 .params = "",
2796 .help = "show the current VM UUID",
5f11cb00 2797 .mhandler.cmd = hmp_info_uuid,
d7f9b689 2798 },
d7f9b689
LC
2799 {
2800 .name = "cpustats",
2801 .args_type = "",
d7f9b689
LC
2802 .params = "",
2803 .help = "show CPU statistics",
5f11cb00 2804 .mhandler.cmd = do_info_cpu_stats,
d7f9b689 2805 },
31a60e22 2806#if defined(CONFIG_SLIRP)
d7f9b689
LC
2807 {
2808 .name = "usernet",
2809 .args_type = "",
d7f9b689
LC
2810 .params = "",
2811 .help = "show user network stack connection states",
5f11cb00 2812 .mhandler.cmd = do_info_usernet,
d7f9b689 2813 },
31a60e22 2814#endif
d7f9b689
LC
2815 {
2816 .name = "migrate",
2817 .args_type = "",
d7f9b689
LC
2818 .params = "",
2819 .help = "show migration status",
5f11cb00 2820 .mhandler.cmd = hmp_info_migrate,
d7f9b689 2821 },
bbf6da32
OW
2822 {
2823 .name = "migrate_capabilities",
2824 .args_type = "",
2825 .params = "",
2826 .help = "show current migration capabilities",
5f11cb00 2827 .mhandler.cmd = hmp_info_migrate_capabilities,
bbf6da32 2828 },
9e1ba4cc
OW
2829 {
2830 .name = "migrate_cache_size",
2831 .args_type = "",
2832 .params = "",
2833 .help = "show current migration xbzrle cache size",
5f11cb00 2834 .mhandler.cmd = hmp_info_migrate_cache_size,
9e1ba4cc 2835 },
d7f9b689
LC
2836 {
2837 .name = "balloon",
2838 .args_type = "",
d7f9b689
LC
2839 .params = "",
2840 .help = "show balloon information",
5f11cb00 2841 .mhandler.cmd = hmp_info_balloon,
d7f9b689
LC
2842 },
2843 {
2844 .name = "qtree",
2845 .args_type = "",
d7f9b689
LC
2846 .params = "",
2847 .help = "show device tree",
5f11cb00 2848 .mhandler.cmd = do_info_qtree,
d7f9b689
LC
2849 },
2850 {
2851 .name = "qdm",
2852 .args_type = "",
d7f9b689
LC
2853 .params = "",
2854 .help = "show qdev device model list",
5f11cb00 2855 .mhandler.cmd = do_info_qdm,
d7f9b689
LC
2856 },
2857 {
2858 .name = "roms",
2859 .args_type = "",
d7f9b689
LC
2860 .params = "",
2861 .help = "show roms",
5f11cb00 2862 .mhandler.cmd = do_info_roms,
d7f9b689 2863 },
22890ab5
PS
2864 {
2865 .name = "trace-events",
2866 .args_type = "",
2867 .params = "",
2868 .help = "show available trace-events & their state",
5f11cb00 2869 .mhandler.cmd = do_trace_print_events,
22890ab5 2870 },
d1a0cf73
SB
2871 {
2872 .name = "tpm",
2873 .args_type = "",
2874 .params = "",
2875 .help = "show the TPM device",
2876 .mhandler.cmd = hmp_info_tpm,
2877 },
d7f9b689
LC
2878 {
2879 .name = NULL,
2880 },
9dc39cba
FB
2881};
2882
a13ced59
WX
2883/* mon_cmds and info_cmds would be sorted at runtime */
2884static mon_cmd_t mon_cmds[] = {
2885#include "hmp-commands.h"
2886 { NULL, NULL, },
2887};
2888
f36b4afb 2889static const mon_cmd_t qmp_cmds[] = {
e3193601 2890#include "qmp-commands-old.h"
f36b4afb
LC
2891 { /* NULL */ },
2892};
2893
9307c4c1
FB
2894/*******************************************************************/
2895
2896static const char *pch;
6ab7e546 2897static sigjmp_buf expr_env;
9307c4c1 2898
92a31b1f
FB
2899#define MD_TLONG 0
2900#define MD_I32 1
2901
9307c4c1
FB
2902typedef struct MonitorDef {
2903 const char *name;
2904 int offset;
8662d656 2905 target_long (*get_value)(const struct MonitorDef *md, int val);
92a31b1f 2906 int type;
9307c4c1
FB
2907} MonitorDef;
2908
57206fd4 2909#if defined(TARGET_I386)
8662d656 2910static target_long monitor_get_pc (const struct MonitorDef *md, int val)
57206fd4 2911{
9349b4f9 2912 CPUArchState *env = mon_get_cpu();
6a00d601 2913 return env->eip + env->segs[R_CS].base;
57206fd4
FB
2914}
2915#endif
2916
a541f297 2917#if defined(TARGET_PPC)
8662d656 2918static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
a541f297 2919{
9349b4f9 2920 CPUArchState *env = mon_get_cpu();
a541f297
FB
2921 unsigned int u;
2922 int i;
2923
2924 u = 0;
2925 for (i = 0; i < 8; i++)
28a76be8 2926 u |= env->crf[i] << (32 - (4 * i));
a541f297
FB
2927
2928 return u;
2929}
2930
8662d656 2931static target_long monitor_get_msr (const struct MonitorDef *md, int val)
a541f297 2932{
9349b4f9 2933 CPUArchState *env = mon_get_cpu();
0411a972 2934 return env->msr;
a541f297
FB
2935}
2936
8662d656 2937static target_long monitor_get_xer (const struct MonitorDef *md, int val)
a541f297 2938{
9349b4f9 2939 CPUArchState *env = mon_get_cpu();
3d7b417e 2940 return env->xer;
a541f297 2941}
9fddaa0c 2942
8662d656 2943static target_long monitor_get_decr (const struct MonitorDef *md, int val)
9fddaa0c 2944{
9349b4f9 2945 CPUArchState *env = mon_get_cpu();
6a00d601 2946 return cpu_ppc_load_decr(env);
9fddaa0c
FB
2947}
2948
8662d656 2949static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
9fddaa0c 2950{
9349b4f9 2951 CPUArchState *env = mon_get_cpu();
6a00d601 2952 return cpu_ppc_load_tbu(env);
9fddaa0c
FB
2953}
2954
8662d656 2955static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
9fddaa0c 2956{
9349b4f9 2957 CPUArchState *env = mon_get_cpu();
6a00d601 2958 return cpu_ppc_load_tbl(env);
9fddaa0c 2959}
a541f297
FB
2960#endif
2961
e95c8d51 2962#if defined(TARGET_SPARC)
7b936c0c 2963#ifndef TARGET_SPARC64
8662d656 2964static target_long monitor_get_psr (const struct MonitorDef *md, int val)
e95c8d51 2965{
9349b4f9 2966 CPUArchState *env = mon_get_cpu();
5a834bb4
BS
2967
2968 return cpu_get_psr(env);
e95c8d51 2969}
7b936c0c 2970#endif
e95c8d51 2971
8662d656 2972static target_long monitor_get_reg(const struct MonitorDef *md, int val)
e95c8d51 2973{
9349b4f9 2974 CPUArchState *env = mon_get_cpu();
6a00d601 2975 return env->regwptr[val];
e95c8d51
FB
2976}
2977#endif
2978
8662d656 2979static const MonitorDef monitor_defs[] = {
9307c4c1 2980#ifdef TARGET_I386
57206fd4
FB
2981
2982#define SEG(name, seg) \
e59d167f
AF
2983 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2984 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2985 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
2986
2987 { "eax", offsetof(CPUX86State, regs[0]) },
2988 { "ecx", offsetof(CPUX86State, regs[1]) },
2989 { "edx", offsetof(CPUX86State, regs[2]) },
2990 { "ebx", offsetof(CPUX86State, regs[3]) },
2991 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2992 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2993 { "esi", offsetof(CPUX86State, regs[6]) },
2994 { "edi", offsetof(CPUX86State, regs[7]) },
92a31b1f 2995#ifdef TARGET_X86_64
e59d167f
AF
2996 { "r8", offsetof(CPUX86State, regs[8]) },
2997 { "r9", offsetof(CPUX86State, regs[9]) },
2998 { "r10", offsetof(CPUX86State, regs[10]) },
2999 { "r11", offsetof(CPUX86State, regs[11]) },
3000 { "r12", offsetof(CPUX86State, regs[12]) },
3001 { "r13", offsetof(CPUX86State, regs[13]) },
3002 { "r14", offsetof(CPUX86State, regs[14]) },
3003 { "r15", offsetof(CPUX86State, regs[15]) },
92a31b1f 3004#endif
e59d167f
AF
3005 { "eflags", offsetof(CPUX86State, eflags) },
3006 { "eip", offsetof(CPUX86State, eip) },
57206fd4
FB
3007 SEG("cs", R_CS)
3008 SEG("ds", R_DS)
3009 SEG("es", R_ES)
01038d2a 3010 SEG("ss", R_SS)
57206fd4
FB
3011 SEG("fs", R_FS)
3012 SEG("gs", R_GS)
3013 { "pc", 0, monitor_get_pc, },
a541f297 3014#elif defined(TARGET_PPC)
ff937dba 3015 /* General purpose registers */
e59d167f
AF
3016 { "r0", offsetof(CPUPPCState, gpr[0]) },
3017 { "r1", offsetof(CPUPPCState, gpr[1]) },
3018 { "r2", offsetof(CPUPPCState, gpr[2]) },
3019 { "r3", offsetof(CPUPPCState, gpr[3]) },
3020 { "r4", offsetof(CPUPPCState, gpr[4]) },
3021 { "r5", offsetof(CPUPPCState, gpr[5]) },
3022 { "r6", offsetof(CPUPPCState, gpr[6]) },
3023 { "r7", offsetof(CPUPPCState, gpr[7]) },
3024 { "r8", offsetof(CPUPPCState, gpr[8]) },
3025 { "r9", offsetof(CPUPPCState, gpr[9]) },
3026 { "r10", offsetof(CPUPPCState, gpr[10]) },
3027 { "r11", offsetof(CPUPPCState, gpr[11]) },
3028 { "r12", offsetof(CPUPPCState, gpr[12]) },
3029 { "r13", offsetof(CPUPPCState, gpr[13]) },
3030 { "r14", offsetof(CPUPPCState, gpr[14]) },
3031 { "r15", offsetof(CPUPPCState, gpr[15]) },
3032 { "r16", offsetof(CPUPPCState, gpr[16]) },
3033 { "r17", offsetof(CPUPPCState, gpr[17]) },
3034 { "r18", offsetof(CPUPPCState, gpr[18]) },
3035 { "r19", offsetof(CPUPPCState, gpr[19]) },
3036 { "r20", offsetof(CPUPPCState, gpr[20]) },
3037 { "r21", offsetof(CPUPPCState, gpr[21]) },
3038 { "r22", offsetof(CPUPPCState, gpr[22]) },
3039 { "r23", offsetof(CPUPPCState, gpr[23]) },
3040 { "r24", offsetof(CPUPPCState, gpr[24]) },
3041 { "r25", offsetof(CPUPPCState, gpr[25]) },
3042 { "r26", offsetof(CPUPPCState, gpr[26]) },
3043 { "r27", offsetof(CPUPPCState, gpr[27]) },
3044 { "r28", offsetof(CPUPPCState, gpr[28]) },
3045 { "r29", offsetof(CPUPPCState, gpr[29]) },
3046 { "r30", offsetof(CPUPPCState, gpr[30]) },
3047 { "r31", offsetof(CPUPPCState, gpr[31]) },
ff937dba 3048 /* Floating point registers */
e59d167f
AF
3049 { "f0", offsetof(CPUPPCState, fpr[0]) },
3050 { "f1", offsetof(CPUPPCState, fpr[1]) },
3051 { "f2", offsetof(CPUPPCState, fpr[2]) },
3052 { "f3", offsetof(CPUPPCState, fpr[3]) },
3053 { "f4", offsetof(CPUPPCState, fpr[4]) },
3054 { "f5", offsetof(CPUPPCState, fpr[5]) },
3055 { "f6", offsetof(CPUPPCState, fpr[6]) },
3056 { "f7", offsetof(CPUPPCState, fpr[7]) },
3057 { "f8", offsetof(CPUPPCState, fpr[8]) },
3058 { "f9", offsetof(CPUPPCState, fpr[9]) },
3059 { "f10", offsetof(CPUPPCState, fpr[10]) },
3060 { "f11", offsetof(CPUPPCState, fpr[11]) },
3061 { "f12", offsetof(CPUPPCState, fpr[12]) },
3062 { "f13", offsetof(CPUPPCState, fpr[13]) },
3063 { "f14", offsetof(CPUPPCState, fpr[14]) },
3064 { "f15", offsetof(CPUPPCState, fpr[15]) },
3065 { "f16", offsetof(CPUPPCState, fpr[16]) },
3066 { "f17", offsetof(CPUPPCState, fpr[17]) },
3067 { "f18", offsetof(CPUPPCState, fpr[18]) },
3068 { "f19", offsetof(CPUPPCState, fpr[19]) },
3069 { "f20", offsetof(CPUPPCState, fpr[20]) },
3070 { "f21", offsetof(CPUPPCState, fpr[21]) },
3071 { "f22", offsetof(CPUPPCState, fpr[22]) },
3072 { "f23", offsetof(CPUPPCState, fpr[23]) },
3073 { "f24", offsetof(CPUPPCState, fpr[24]) },
3074 { "f25", offsetof(CPUPPCState, fpr[25]) },
3075 { "f26", offsetof(CPUPPCState, fpr[26]) },
3076 { "f27", offsetof(CPUPPCState, fpr[27]) },
3077 { "f28", offsetof(CPUPPCState, fpr[28]) },
3078 { "f29", offsetof(CPUPPCState, fpr[29]) },
3079 { "f30", offsetof(CPUPPCState, fpr[30]) },
3080 { "f31", offsetof(CPUPPCState, fpr[31]) },
3081 { "fpscr", offsetof(CPUPPCState, fpscr) },
ff937dba 3082 /* Next instruction pointer */
e59d167f
AF
3083 { "nip|pc", offsetof(CPUPPCState, nip) },
3084 { "lr", offsetof(CPUPPCState, lr) },
3085 { "ctr", offsetof(CPUPPCState, ctr) },
9fddaa0c 3086 { "decr", 0, &monitor_get_decr, },
a541f297 3087 { "ccr", 0, &monitor_get_ccr, },
ff937dba 3088 /* Machine state register */
a541f297
FB
3089 { "msr", 0, &monitor_get_msr, },
3090 { "xer", 0, &monitor_get_xer, },
9fddaa0c
FB
3091 { "tbu", 0, &monitor_get_tbu, },
3092 { "tbl", 0, &monitor_get_tbl, },
ff937dba 3093 /* Segment registers */
e59d167f
AF
3094 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3095 { "sr0", offsetof(CPUPPCState, sr[0]) },
3096 { "sr1", offsetof(CPUPPCState, sr[1]) },
3097 { "sr2", offsetof(CPUPPCState, sr[2]) },
3098 { "sr3", offsetof(CPUPPCState, sr[3]) },
3099 { "sr4", offsetof(CPUPPCState, sr[4]) },
3100 { "sr5", offsetof(CPUPPCState, sr[5]) },
3101 { "sr6", offsetof(CPUPPCState, sr[6]) },
3102 { "sr7", offsetof(CPUPPCState, sr[7]) },
3103 { "sr8", offsetof(CPUPPCState, sr[8]) },
3104 { "sr9", offsetof(CPUPPCState, sr[9]) },
3105 { "sr10", offsetof(CPUPPCState, sr[10]) },
3106 { "sr11", offsetof(CPUPPCState, sr[11]) },
3107 { "sr12", offsetof(CPUPPCState, sr[12]) },
3108 { "sr13", offsetof(CPUPPCState, sr[13]) },
3109 { "sr14", offsetof(CPUPPCState, sr[14]) },
3110 { "sr15", offsetof(CPUPPCState, sr[15]) },
90dc8812 3111 /* Too lazy to put BATs... */
e59d167f
AF
3112 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
3113
3114 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3115 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3116 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3117 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3118 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3119 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3120 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3121 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3122 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3123 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3124 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3125 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3126 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3127 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3128 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3129 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3130 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3131 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3132 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3133 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3134 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3135 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3136 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3137 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3138 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3139 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3140 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3141 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3142 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3143 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3144 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3145 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3146 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3147 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3148 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3149 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3150 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3151 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3152 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3153 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3154 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3155 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3156 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3157 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3158 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3159 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3160 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3161 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3162 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3163 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3164 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3165 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3166 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3167 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3168 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3169 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3170 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3171 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3172 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3173 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3174 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3175 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3176 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3177 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3178 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3179 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
90dc8812 3180
e95c8d51 3181#elif defined(TARGET_SPARC)
e59d167f
AF
3182 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3183 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3184 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3185 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3186 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3187 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3188 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3189 { "g7", offsetof(CPUSPARCState, gregs[7]) },
e95c8d51
FB
3190 { "o0", 0, monitor_get_reg },
3191 { "o1", 1, monitor_get_reg },
3192 { "o2", 2, monitor_get_reg },
3193 { "o3", 3, monitor_get_reg },
3194 { "o4", 4, monitor_get_reg },
3195 { "o5", 5, monitor_get_reg },
3196 { "o6", 6, monitor_get_reg },
3197 { "o7", 7, monitor_get_reg },
3198 { "l0", 8, monitor_get_reg },
3199 { "l1", 9, monitor_get_reg },
3200 { "l2", 10, monitor_get_reg },
3201 { "l3", 11, monitor_get_reg },
3202 { "l4", 12, monitor_get_reg },
3203 { "l5", 13, monitor_get_reg },
3204 { "l6", 14, monitor_get_reg },
3205 { "l7", 15, monitor_get_reg },
3206 { "i0", 16, monitor_get_reg },
3207 { "i1", 17, monitor_get_reg },
3208 { "i2", 18, monitor_get_reg },
3209 { "i3", 19, monitor_get_reg },
3210 { "i4", 20, monitor_get_reg },
3211 { "i5", 21, monitor_get_reg },
3212 { "i6", 22, monitor_get_reg },
3213 { "i7", 23, monitor_get_reg },
e59d167f
AF
3214 { "pc", offsetof(CPUSPARCState, pc) },
3215 { "npc", offsetof(CPUSPARCState, npc) },
3216 { "y", offsetof(CPUSPARCState, y) },
7b936c0c 3217#ifndef TARGET_SPARC64
e95c8d51 3218 { "psr", 0, &monitor_get_psr, },
e59d167f 3219 { "wim", offsetof(CPUSPARCState, wim) },
7b936c0c 3220#endif
e59d167f
AF
3221 { "tbr", offsetof(CPUSPARCState, tbr) },
3222 { "fsr", offsetof(CPUSPARCState, fsr) },
3223 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3224 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3225 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3226 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3227 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3228 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3229 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3230 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3231 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3232 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3233 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3234 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3235 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3236 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3237 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3238 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3239 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3240 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3241 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3242 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3243 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3244 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3245 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3246 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3247 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3248 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3249 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3250 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3251 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3252 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3253 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3254 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
7b936c0c 3255#ifdef TARGET_SPARC64
e59d167f
AF
3256 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3257 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3258 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3259 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3260 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3261 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3262 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3263 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3264 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3265 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3266 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3267 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3268 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3269 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3270 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3271 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3272 { "asi", offsetof(CPUSPARCState, asi) },
3273 { "pstate", offsetof(CPUSPARCState, pstate) },
3274 { "cansave", offsetof(CPUSPARCState, cansave) },
3275 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3276 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3277 { "wstate", offsetof(CPUSPARCState, wstate) },
3278 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3279 { "fprs", offsetof(CPUSPARCState, fprs) },
7b936c0c 3280#endif
9307c4c1
FB
3281#endif
3282 { NULL },
3283};
3284
9c3175cc
SW
3285static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3286expr_error(Monitor *mon, const char *fmt, ...)
9dc39cba 3287{
277acfe8
FZ
3288 va_list ap;
3289 va_start(ap, fmt);
3290 monitor_vprintf(mon, fmt, ap);
3291 monitor_printf(mon, "\n");
3292 va_end(ap);
6ab7e546 3293 siglongjmp(expr_env, 1);
9307c4c1
FB
3294}
3295
09b9418c 3296/* return 0 if OK, -1 if not found */
92a31b1f 3297static int get_monitor_def(target_long *pval, const char *name)
9307c4c1 3298{
8662d656 3299 const MonitorDef *md;
92a31b1f
FB
3300 void *ptr;
3301
9307c4c1
FB
3302 for(md = monitor_defs; md->name != NULL; md++) {
3303 if (compare_cmd(name, md->name)) {
3304 if (md->get_value) {
e95c8d51 3305 *pval = md->get_value(md, md->offset);
9307c4c1 3306 } else {
9349b4f9 3307 CPUArchState *env = mon_get_cpu();
6a00d601 3308 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
3309 switch(md->type) {
3310 case MD_I32:
3311 *pval = *(int32_t *)ptr;
3312 break;
3313 case MD_TLONG:
3314 *pval = *(target_long *)ptr;
3315 break;
3316 default:
3317 *pval = 0;
3318 break;
3319 }
9307c4c1
FB
3320 }
3321 return 0;
3322 }
3323 }
3324 return -1;
3325}
3326
3327static void next(void)
3328{
660f11be 3329 if (*pch != '\0') {
9307c4c1 3330 pch++;
cd390083 3331 while (qemu_isspace(*pch))
9307c4c1
FB
3332 pch++;
3333 }
3334}
3335
376253ec 3336static int64_t expr_sum(Monitor *mon);
9307c4c1 3337
376253ec 3338static int64_t expr_unary(Monitor *mon)
9307c4c1 3339{
c2efc95d 3340 int64_t n;
9307c4c1 3341 char *p;
6a00d601 3342 int ret;
9307c4c1
FB
3343
3344 switch(*pch) {
3345 case '+':
3346 next();
376253ec 3347 n = expr_unary(mon);
9307c4c1
FB
3348 break;
3349 case '-':
3350 next();
376253ec 3351 n = -expr_unary(mon);
9307c4c1
FB
3352 break;
3353 case '~':
3354 next();
376253ec 3355 n = ~expr_unary(mon);
9307c4c1
FB
3356 break;
3357 case '(':
3358 next();
376253ec 3359 n = expr_sum(mon);
9307c4c1 3360 if (*pch != ')') {
376253ec 3361 expr_error(mon, "')' expected");
9307c4c1
FB
3362 }
3363 next();
3364 break;
81d0912d
FB
3365 case '\'':
3366 pch++;
3367 if (*pch == '\0')
376253ec 3368 expr_error(mon, "character constant expected");
81d0912d
FB
3369 n = *pch;
3370 pch++;
3371 if (*pch != '\'')
376253ec 3372 expr_error(mon, "missing terminating \' character");
81d0912d
FB
3373 next();
3374 break;
9307c4c1
FB
3375 case '$':
3376 {
3377 char buf[128], *q;
69b34976 3378 target_long reg=0;
3b46e624 3379
9307c4c1
FB
3380 pch++;
3381 q = buf;
3382 while ((*pch >= 'a' && *pch <= 'z') ||
3383 (*pch >= 'A' && *pch <= 'Z') ||
3384 (*pch >= '0' && *pch <= '9') ||
57206fd4 3385 *pch == '_' || *pch == '.') {
9307c4c1
FB
3386 if ((q - buf) < sizeof(buf) - 1)
3387 *q++ = *pch;
3388 pch++;
3389 }
cd390083 3390 while (qemu_isspace(*pch))
9307c4c1
FB
3391 pch++;
3392 *q = 0;
7743e588 3393 ret = get_monitor_def(&reg, buf);
09b9418c 3394 if (ret < 0)
376253ec 3395 expr_error(mon, "unknown register");
7743e588 3396 n = reg;
9307c4c1
FB
3397 }
3398 break;
3399 case '\0':
376253ec 3400 expr_error(mon, "unexpected end of expression");
9307c4c1
FB
3401 n = 0;
3402 break;
3403 default:
6b0e33be 3404 errno = 0;
4f4fbf77 3405 n = strtoull(pch, &p, 0);
6b0e33be
LC
3406 if (errno == ERANGE) {
3407 expr_error(mon, "number too large");
3408 }
9307c4c1 3409 if (pch == p) {
277acfe8 3410 expr_error(mon, "invalid char '%c' in expression", *p);
9307c4c1
FB
3411 }
3412 pch = p;
cd390083 3413 while (qemu_isspace(*pch))
9307c4c1
FB
3414 pch++;
3415 break;
3416 }
3417 return n;
3418}
3419
3420
376253ec 3421static int64_t expr_prod(Monitor *mon)
9307c4c1 3422{
c2efc95d 3423 int64_t val, val2;
92a31b1f 3424 int op;
3b46e624 3425
376253ec 3426 val = expr_unary(mon);
9307c4c1
FB
3427 for(;;) {
3428 op = *pch;
3429 if (op != '*' && op != '/' && op != '%')
3430 break;
3431 next();
376253ec 3432 val2 = expr_unary(mon);
9307c4c1
FB
3433 switch(op) {
3434 default:
3435 case '*':
3436 val *= val2;
3437 break;
3438 case '/':
3439 case '%':
5fafdf24 3440 if (val2 == 0)
376253ec 3441 expr_error(mon, "division by zero");
9307c4c1
FB
3442 if (op == '/')
3443 val /= val2;
3444 else
3445 val %= val2;
3446 break;
3447 }
3448 }
3449 return val;
3450}
3451
376253ec 3452static int64_t expr_logic(Monitor *mon)
9307c4c1 3453{
c2efc95d 3454 int64_t val, val2;
92a31b1f 3455 int op;
9307c4c1 3456
376253ec 3457 val = expr_prod(mon);
9307c4c1
FB
3458 for(;;) {
3459 op = *pch;
3460 if (op != '&' && op != '|' && op != '^')
3461 break;
3462 next();
376253ec 3463 val2 = expr_prod(mon);
9307c4c1
FB
3464 switch(op) {
3465 default:
3466 case '&':
3467 val &= val2;
3468 break;
3469 case '|':
3470 val |= val2;
3471 break;
3472 case '^':
3473 val ^= val2;
3474 break;
3475 }
3476 }
3477 return val;
3478}
3479
376253ec 3480static int64_t expr_sum(Monitor *mon)
9307c4c1 3481{
c2efc95d 3482 int64_t val, val2;
92a31b1f 3483 int op;
9307c4c1 3484
376253ec 3485 val = expr_logic(mon);
9307c4c1
FB
3486 for(;;) {
3487 op = *pch;
3488 if (op != '+' && op != '-')
3489 break;
3490 next();
376253ec 3491 val2 = expr_logic(mon);
9307c4c1
FB
3492 if (op == '+')
3493 val += val2;
3494 else
3495 val -= val2;
3496 }
3497 return val;
3498}
3499
376253ec 3500static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
9307c4c1
FB
3501{
3502 pch = *pp;
6ab7e546 3503 if (sigsetjmp(expr_env, 0)) {
9307c4c1
FB
3504 *pp = pch;
3505 return -1;
3506 }
cd390083 3507 while (qemu_isspace(*pch))
9307c4c1 3508 pch++;
376253ec 3509 *pval = expr_sum(mon);
9307c4c1
FB
3510 *pp = pch;
3511 return 0;
3512}
3513
3350a4dd
MA
3514static int get_double(Monitor *mon, double *pval, const char **pp)
3515{
3516 const char *p = *pp;
3517 char *tailp;
3518 double d;
3519
3520 d = strtod(p, &tailp);
3521 if (tailp == p) {
3522 monitor_printf(mon, "Number expected\n");
3523 return -1;
3524 }
3525 if (d != d || d - d != 0) {
3526 /* NaN or infinity */
3527 monitor_printf(mon, "Bad number\n");
3528 return -1;
3529 }
3530 *pval = d;
3531 *pp = tailp;
3532 return 0;
3533}
3534
4590fd80
LC
3535/*
3536 * Store the command-name in cmdname, and return a pointer to
3537 * the remaining of the command string.
3538 */
3539static const char *get_command_name(const char *cmdline,
3540 char *cmdname, size_t nlen)
3541{
3542 size_t len;
3543 const char *p, *pstart;
3544
3545 p = cmdline;
3546 while (qemu_isspace(*p))
3547 p++;
3548 if (*p == '\0')
3549 return NULL;
3550 pstart = p;
3551 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3552 p++;
3553 len = p - pstart;
3554 if (len > nlen - 1)
3555 len = nlen - 1;
3556 memcpy(cmdname, pstart, len);
3557 cmdname[len] = '\0';
3558 return p;
3559}
3560
4d76d2ba
LC
3561/**
3562 * Read key of 'type' into 'key' and return the current
3563 * 'type' pointer.
3564 */
3565static char *key_get_info(const char *type, char **key)
3566{
3567 size_t len;
3568 char *p, *str;
3569
3570 if (*type == ',')
3571 type++;
3572
3573 p = strchr(type, ':');
3574 if (!p) {
3575 *key = NULL;
3576 return NULL;
3577 }
3578 len = p - type;
3579
7267c094 3580 str = g_malloc(len + 1);
4d76d2ba
LC
3581 memcpy(str, type, len);
3582 str[len] = '\0';
3583
3584 *key = str;
3585 return ++p;
3586}
3587
9307c4c1
FB
3588static int default_fmt_format = 'x';
3589static int default_fmt_size = 4;
3590
fbc3d96c
LS
3591static int is_valid_option(const char *c, const char *typestr)
3592{
3593 char option[3];
3594
3595 option[0] = '-';
3596 option[1] = *c;
3597 option[2] = '\0';
3598
3599 typestr = strstr(typestr, option);
3600 return (typestr != NULL);
3601}
3602
945c5ac8
LC
3603static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3604 const char *cmdname)
7fd669a1
LC
3605{
3606 const mon_cmd_t *cmd;
3607
945c5ac8 3608 for (cmd = disp_table; cmd->name != NULL; cmd++) {
7fd669a1
LC
3609 if (compare_cmd(cmdname, cmd->name)) {
3610 return cmd;
3611 }
3612 }
3613
3614 return NULL;
3615}
3616
bead3ce1
LC
3617static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3618{
f36b4afb 3619 return search_dispatch_table(qmp_cmds, cmdname);
bead3ce1
LC
3620}
3621
5f3d335f
WX
3622/*
3623 * Parse @cmdline according to command table @table.
3624 * If @cmdline is blank, return NULL.
3625 * If it can't be parsed, report to @mon, and return NULL.
3626 * Else, insert command arguments into @qdict, and return the command.
085d8134
PM
3627 * If a sub-command table exists, and if @cmdline contains an additional string
3628 * for a sub-command, this function will try to search the sub-command table.
3629 * If no additional string for a sub-command is present, this function will
3630 * return the command found in @table.
5f3d335f
WX
3631 * Do not assume the returned command points into @table! It doesn't
3632 * when the command is a sub-command.
3633 */
c227f099 3634static const mon_cmd_t *monitor_parse_command(Monitor *mon,
55f81d96 3635 const char *cmdline,
5f3d335f
WX
3636 int start,
3637 mon_cmd_t *table,
55f81d96 3638 QDict *qdict)
9307c4c1 3639{
4590fd80 3640 const char *p, *typestr;
53773581 3641 int c;
c227f099 3642 const mon_cmd_t *cmd;
9307c4c1
FB
3643 char cmdname[256];
3644 char buf[1024];
4d76d2ba 3645 char *key;
9dc39cba
FB
3646
3647#ifdef DEBUG
5f3d335f 3648 monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start);
9dc39cba 3649#endif
3b46e624 3650
9307c4c1 3651 /* extract the command name */
5f3d335f 3652 p = get_command_name(cmdline + start, cmdname, sizeof(cmdname));
4590fd80 3653 if (!p)
55f81d96 3654 return NULL;
3b46e624 3655
5f3d335f 3656 cmd = search_dispatch_table(table, cmdname);
7fd669a1 3657 if (!cmd) {
5f3d335f
WX
3658 monitor_printf(mon, "unknown command: '%.*s'\n",
3659 (int)(p - cmdline), cmdline);
55f81d96 3660 return NULL;
9307c4c1 3661 }
9307c4c1 3662
5f3d335f
WX
3663 /* filter out following useless space */
3664 while (qemu_isspace(*p)) {
3665 p++;
3666 }
3667 /* search sub command */
3668 if (cmd->sub_table != NULL) {
3669 /* check if user set additional command */
3670 if (*p == '\0') {
3671 return cmd;
3672 }
3673 return monitor_parse_command(mon, cmdline, p - cmdline,
3674 cmd->sub_table, qdict);
3675 }
3676
9307c4c1
FB
3677 /* parse the parameters */
3678 typestr = cmd->args_type;
9dc39cba 3679 for(;;) {
4d76d2ba
LC
3680 typestr = key_get_info(typestr, &key);
3681 if (!typestr)
9dc39cba 3682 break;
4d76d2ba 3683 c = *typestr;
9307c4c1
FB
3684 typestr++;
3685 switch(c) {
3686 case 'F':
81d0912d 3687 case 'B':
9307c4c1
FB
3688 case 's':
3689 {
3690 int ret;
3b46e624 3691
cd390083 3692 while (qemu_isspace(*p))
9307c4c1
FB
3693 p++;
3694 if (*typestr == '?') {
3695 typestr++;
3696 if (*p == '\0') {
3697 /* no optional string: NULL argument */
53773581 3698 break;
9307c4c1
FB
3699 }
3700 }
3701 ret = get_str(buf, sizeof(buf), &p);
3702 if (ret < 0) {
81d0912d
FB
3703 switch(c) {
3704 case 'F':
376253ec
AL
3705 monitor_printf(mon, "%s: filename expected\n",
3706 cmdname);
81d0912d
FB
3707 break;
3708 case 'B':
376253ec
AL
3709 monitor_printf(mon, "%s: block device name expected\n",
3710 cmdname);
81d0912d
FB
3711 break;
3712 default:
376253ec 3713 monitor_printf(mon, "%s: string expected\n", cmdname);
81d0912d
FB
3714 break;
3715 }
9307c4c1
FB
3716 goto fail;
3717 }
53773581 3718 qdict_put(qdict, key, qstring_from_str(buf));
9307c4c1 3719 }
9dc39cba 3720 break;
361127df
MA
3721 case 'O':
3722 {
3723 QemuOptsList *opts_list;
3724 QemuOpts *opts;
3725
3726 opts_list = qemu_find_opts(key);
3727 if (!opts_list || opts_list->desc->name) {
3728 goto bad_type;
3729 }
3730 while (qemu_isspace(*p)) {
3731 p++;
3732 }
3733 if (!*p)
3734 break;
3735 if (get_str(buf, sizeof(buf), &p) < 0) {
3736 goto fail;
3737 }
3738 opts = qemu_opts_parse(opts_list, buf, 1);
3739 if (!opts) {
3740 goto fail;
3741 }
3742 qemu_opts_to_qdict(opts, qdict);
3743 qemu_opts_del(opts);
3744 }
3745 break;
9307c4c1
FB
3746 case '/':
3747 {
3748 int count, format, size;
3b46e624 3749
cd390083 3750 while (qemu_isspace(*p))
9307c4c1
FB
3751 p++;
3752 if (*p == '/') {
3753 /* format found */
3754 p++;
3755 count = 1;
cd390083 3756 if (qemu_isdigit(*p)) {
9307c4c1 3757 count = 0;
cd390083 3758 while (qemu_isdigit(*p)) {
9307c4c1
FB
3759 count = count * 10 + (*p - '0');
3760 p++;
3761 }
3762 }
3763 size = -1;
3764 format = -1;
3765 for(;;) {
3766 switch(*p) {
3767 case 'o':
3768 case 'd':
3769 case 'u':
3770 case 'x':
3771 case 'i':
3772 case 'c':
3773 format = *p++;
3774 break;
3775 case 'b':
3776 size = 1;
3777 p++;
3778 break;
3779 case 'h':
3780 size = 2;
3781 p++;
3782 break;
3783 case 'w':
3784 size = 4;
3785 p++;
3786 break;
3787 case 'g':
3788 case 'L':
3789 size = 8;
3790 p++;
3791 break;
3792 default:
3793 goto next;
3794 }
3795 }
3796 next:
cd390083 3797 if (*p != '\0' && !qemu_isspace(*p)) {
376253ec
AL
3798 monitor_printf(mon, "invalid char in format: '%c'\n",
3799 *p);
9307c4c1
FB
3800 goto fail;
3801 }
9307c4c1
FB
3802 if (format < 0)
3803 format = default_fmt_format;
4c27ba27
FB
3804 if (format != 'i') {
3805 /* for 'i', not specifying a size gives -1 as size */
3806 if (size < 0)
3807 size = default_fmt_size;
e90f009b 3808 default_fmt_size = size;
4c27ba27 3809 }
9307c4c1
FB
3810 default_fmt_format = format;
3811 } else {
3812 count = 1;
3813 format = default_fmt_format;
4c27ba27
FB
3814 if (format != 'i') {
3815 size = default_fmt_size;
3816 } else {
3817 size = -1;
3818 }
9307c4c1 3819 }
f7188bbe
LC
3820 qdict_put(qdict, "count", qint_from_int(count));
3821 qdict_put(qdict, "format", qint_from_int(format));
3822 qdict_put(qdict, "size", qint_from_int(size));
9307c4c1 3823 }
9dc39cba 3824 break;
9307c4c1 3825 case 'i':
92a31b1f 3826 case 'l':
b6e098d7 3827 case 'M':
9307c4c1 3828 {
c2efc95d 3829 int64_t val;
7743e588 3830
cd390083 3831 while (qemu_isspace(*p))
9307c4c1 3832 p++;
3440557b 3833 if (*typestr == '?' || *typestr == '.') {
3440557b 3834 if (*typestr == '?') {
53773581
LC
3835 if (*p == '\0') {
3836 typestr++;
3837 break;
3838 }
3440557b
FB
3839 } else {
3840 if (*p == '.') {
3841 p++;
cd390083 3842 while (qemu_isspace(*p))
3440557b 3843 p++;
3440557b 3844 } else {
53773581
LC
3845 typestr++;
3846 break;
3440557b
FB
3847 }
3848 }
13224a87 3849 typestr++;
9307c4c1 3850 }
376253ec 3851 if (get_expr(mon, &val, &p))
9307c4c1 3852 goto fail;
675ebef9
LC
3853 /* Check if 'i' is greater than 32-bit */
3854 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3855 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3856 monitor_printf(mon, "integer is for 32-bit values\n");
3857 goto fail;
b6e098d7 3858 } else if (c == 'M') {
91162849
LC
3859 if (val < 0) {
3860 monitor_printf(mon, "enter a positive value\n");
3861 goto fail;
3862 }
b6e098d7 3863 val <<= 20;
675ebef9 3864 }
53773581 3865 qdict_put(qdict, key, qint_from_int(val));
9307c4c1
FB
3866 }
3867 break;
dbc0c67f
JS
3868 case 'o':
3869 {
70b4f4bb 3870 int64_t val;
dbc0c67f
JS
3871 char *end;
3872
3873 while (qemu_isspace(*p)) {
3874 p++;
3875 }
3876 if (*typestr == '?') {
3877 typestr++;
3878 if (*p == '\0') {
3879 break;
3880 }
3881 }
3882 val = strtosz(p, &end);
3883 if (val < 0) {
3884 monitor_printf(mon, "invalid size\n");
3885 goto fail;
3886 }
3887 qdict_put(qdict, key, qint_from_int(val));
3888 p = end;
3889 }
3890 break;
fccfb11e 3891 case 'T':
3350a4dd
MA
3892 {
3893 double val;
3894
3895 while (qemu_isspace(*p))
3896 p++;
3897 if (*typestr == '?') {
3898 typestr++;
3899 if (*p == '\0') {
3900 break;
3901 }
3902 }
3903 if (get_double(mon, &val, &p) < 0) {
3904 goto fail;
3905 }
07de3e60 3906 if (p[0] && p[1] == 's') {
fccfb11e
MA
3907 switch (*p) {
3908 case 'm':
3909 val /= 1e3; p += 2; break;
3910 case 'u':
3911 val /= 1e6; p += 2; break;
3912 case 'n':
3913 val /= 1e9; p += 2; break;
3914 }
3915 }
3350a4dd
MA
3916 if (*p && !qemu_isspace(*p)) {
3917 monitor_printf(mon, "Unknown unit suffix\n");
3918 goto fail;
3919 }
3920 qdict_put(qdict, key, qfloat_from_double(val));
3921 }
3922 break;
942cd1f2
MA
3923 case 'b':
3924 {
3925 const char *beg;
3926 int val;
3927
3928 while (qemu_isspace(*p)) {
3929 p++;
3930 }
3931 beg = p;
3932 while (qemu_isgraph(*p)) {
3933 p++;
3934 }
3935 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3936 val = 1;
3937 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3938 val = 0;
3939 } else {
3940 monitor_printf(mon, "Expected 'on' or 'off'\n");
3941 goto fail;
3942 }
3943 qdict_put(qdict, key, qbool_from_int(val));
3944 }
3945 break;
9307c4c1
FB
3946 case '-':
3947 {
fbc3d96c 3948 const char *tmp = p;
eb159d13 3949 int skip_key = 0;
9307c4c1 3950 /* option */
3b46e624 3951
9307c4c1
FB
3952 c = *typestr++;
3953 if (c == '\0')
3954 goto bad_type;
cd390083 3955 while (qemu_isspace(*p))
9307c4c1 3956 p++;
9307c4c1
FB
3957 if (*p == '-') {
3958 p++;
fbc3d96c
LS
3959 if(c != *p) {
3960 if(!is_valid_option(p, typestr)) {
3961
3962 monitor_printf(mon, "%s: unsupported option -%c\n",
3963 cmdname, *p);
3964 goto fail;
3965 } else {
3966 skip_key = 1;
3967 }
3968 }
3969 if(skip_key) {
3970 p = tmp;
3971 } else {
eb159d13 3972 /* has option */
fbc3d96c 3973 p++;
eb159d13 3974 qdict_put(qdict, key, qbool_from_int(1));
9307c4c1 3975 }
9307c4c1 3976 }
9307c4c1
FB
3977 }
3978 break;
3979 default:
3980 bad_type:
376253ec 3981 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
9307c4c1
FB
3982 goto fail;
3983 }
7267c094 3984 g_free(key);
4d76d2ba 3985 key = NULL;
9dc39cba 3986 }
9307c4c1 3987 /* check that all arguments were parsed */
cd390083 3988 while (qemu_isspace(*p))
9307c4c1
FB
3989 p++;
3990 if (*p != '\0') {
376253ec
AL
3991 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3992 cmdname);
9307c4c1 3993 goto fail;
9dc39cba 3994 }
9307c4c1 3995
55f81d96 3996 return cmd;
ac7531ec 3997
55f81d96 3998fail:
7267c094 3999 g_free(key);
55f81d96
LC
4000 return NULL;
4001}
4002
d6f46833
MA
4003void monitor_set_error(Monitor *mon, QError *qerror)
4004{
4005 /* report only the first error */
4006 if (!mon->error) {
4007 mon->error = qerror;
4008 } else {
d6f46833
MA
4009 QDECREF(qerror);
4010 }
4011}
4012
bb89c2e9
LC
4013static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4014{
6d441430
LC
4015 if (ret && !monitor_has_error(mon)) {
4016 /*
4017 * If it returns failure, it must have passed on error.
4018 *
4019 * Action: Report an internal error to the client if in QMP.
4020 */
4021 qerror_report(QERR_UNDEFINED_ERROR);
6d441430 4022 }
bb89c2e9
LC
4023}
4024
f3c157c4 4025static void handle_user_command(Monitor *mon, const char *cmdline)
55f81d96 4026{
55f81d96 4027 QDict *qdict;
c227f099 4028 const mon_cmd_t *cmd;
55f81d96
LC
4029
4030 qdict = qdict_new();
4031
7717239d 4032 cmd = monitor_parse_command(mon, cmdline, 0, mon->cmd_table, qdict);
13917bee
LC
4033 if (!cmd)
4034 goto out;
4035
4903de0c 4036 if (handler_is_async(cmd)) {
940cc30d 4037 user_async_cmd_handler(mon, cmd, qdict);
9e80721e 4038 } else if (handler_is_qobject(cmd)) {
de79ba6f
LC
4039 QObject *data = NULL;
4040
4041 /* XXX: ignores the error code */
4042 cmd->mhandler.cmd_new(mon, qdict, &data);
4043 assert(!monitor_has_error(mon));
4044 if (data) {
4045 cmd->user_print(mon, data);
4046 qobject_decref(data);
4047 }
13917bee 4048 } else {
af4ce882 4049 cmd->mhandler.cmd(mon, qdict);
55f81d96
LC
4050 }
4051
13917bee 4052out:
f7188bbe 4053 QDECREF(qdict);
9dc39cba
FB
4054}
4055
cd5c6bba 4056static void cmd_completion(Monitor *mon, const char *name, const char *list)
81d0912d
FB
4057{
4058 const char *p, *pstart;
4059 char cmd[128];
4060 int len;
4061
4062 p = list;
4063 for(;;) {
4064 pstart = p;
4065 p = strchr(p, '|');
4066 if (!p)
4067 p = pstart + strlen(pstart);
4068 len = p - pstart;
4069 if (len > sizeof(cmd) - 2)
4070 len = sizeof(cmd) - 2;
4071 memcpy(cmd, pstart, len);
4072 cmd[len] = '\0';
4073 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
cd5c6bba 4074 readline_add_completion(mon->rs, cmd);
81d0912d
FB
4075 }
4076 if (*p == '\0')
4077 break;
4078 p++;
4079 }
4080}
4081
cb8f68b1 4082static void file_completion(Monitor *mon, const char *input)
81d0912d
FB
4083{
4084 DIR *ffs;
4085 struct dirent *d;
4086 char path[1024];
4087 char file[1024], file_prefix[1024];
4088 int input_path_len;
4089 const char *p;
4090
5fafdf24 4091 p = strrchr(input, '/');
81d0912d
FB
4092 if (!p) {
4093 input_path_len = 0;
4094 pstrcpy(file_prefix, sizeof(file_prefix), input);
363a37d5 4095 pstrcpy(path, sizeof(path), ".");
81d0912d
FB
4096 } else {
4097 input_path_len = p - input + 1;
4098 memcpy(path, input, input_path_len);
4099 if (input_path_len > sizeof(path) - 1)
4100 input_path_len = sizeof(path) - 1;
4101 path[input_path_len] = '\0';
4102 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4103 }
4104#ifdef DEBUG_COMPLETION
cb8f68b1 4105 monitor_printf(mon, "input='%s' path='%s' prefix='%s'\n",
376253ec 4106 input, path, file_prefix);
81d0912d
FB
4107#endif
4108 ffs = opendir(path);
4109 if (!ffs)
4110 return;
4111 for(;;) {
4112 struct stat sb;
4113 d = readdir(ffs);
4114 if (!d)
4115 break;
46c7fc18
KK
4116
4117 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4118 continue;
4119 }
4120
81d0912d
FB
4121 if (strstart(d->d_name, file_prefix, NULL)) {
4122 memcpy(file, input, input_path_len);
363a37d5
BS
4123 if (input_path_len < sizeof(file))
4124 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4125 d->d_name);
81d0912d
FB
4126 /* stat the file to find out if it's a directory.
4127 * In that case add a slash to speed up typing long paths
4128 */
c951d9a6 4129 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
363a37d5 4130 pstrcat(file, sizeof(file), "/");
c951d9a6 4131 }
cb8f68b1 4132 readline_add_completion(mon->rs, file);
81d0912d
FB
4133 }
4134 }
4135 closedir(ffs);
4136}
4137
599a926a
WX
4138typedef struct MonitorBlockComplete {
4139 Monitor *mon;
4140 const char *input;
4141} MonitorBlockComplete;
4142
51de9760 4143static void block_completion_it(void *opaque, BlockDriverState *bs)
81d0912d 4144{
51de9760 4145 const char *name = bdrv_get_device_name(bs);
599a926a
WX
4146 MonitorBlockComplete *mbc = opaque;
4147 Monitor *mon = mbc->mon;
4148 const char *input = mbc->input;
81d0912d
FB
4149
4150 if (input[0] == '\0' ||
4151 !strncmp(name, (char *)input, strlen(input))) {
599a926a 4152 readline_add_completion(mon->rs, name);
81d0912d
FB
4153 }
4154}
4155
4d76d2ba
LC
4156static const char *next_arg_type(const char *typestr)
4157{
4158 const char *p = strchr(typestr, ':');
4159 return (p != NULL ? ++p : typestr);
4160}
4161
d2674b2c
WX
4162static void monitor_find_completion(Monitor *mon,
4163 const char *cmdline)
81d0912d
FB
4164{
4165 const char *cmdname;
4166 char *args[MAX_ARGS];
4167 int nb_args, i, len;
4168 const char *ptype, *str;
c227f099 4169 const mon_cmd_t *cmd;
599a926a 4170 MonitorBlockComplete mbs;
81d0912d
FB
4171
4172 parse_cmdline(cmdline, &nb_args, args);
4173#ifdef DEBUG_COMPLETION
d2674b2c
WX
4174 for (i = 0; i < nb_args; i++) {
4175 monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
81d0912d
FB
4176 }
4177#endif
4178
4179 /* if the line ends with a space, it means we want to complete the
4180 next arg */
4181 len = strlen(cmdline);
cd390083 4182 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
03a63484
JK
4183 if (nb_args >= MAX_ARGS) {
4184 goto cleanup;
4185 }
7267c094 4186 args[nb_args++] = g_strdup("");
81d0912d
FB
4187 }
4188 if (nb_args <= 1) {
4189 /* command completion */
4190 if (nb_args == 0)
4191 cmdname = "";
4192 else
4193 cmdname = args[0];
d2674b2c 4194 readline_set_completion_index(mon->rs, strlen(cmdname));
7717239d 4195 for (cmd = mon->cmd_table; cmd->name != NULL; cmd++) {
cd5c6bba 4196 cmd_completion(mon, cmdname, cmd->name);
81d0912d
FB
4197 }
4198 } else {
4199 /* find the command */
7717239d 4200 for (cmd = mon->cmd_table; cmd->name != NULL; cmd++) {
03a63484
JK
4201 if (compare_cmd(args[0], cmd->name)) {
4202 break;
4203 }
81d0912d 4204 }
03a63484
JK
4205 if (!cmd->name) {
4206 goto cleanup;
4207 }
4208
4d76d2ba 4209 ptype = next_arg_type(cmd->args_type);
81d0912d
FB
4210 for(i = 0; i < nb_args - 2; i++) {
4211 if (*ptype != '\0') {
4d76d2ba 4212 ptype = next_arg_type(ptype);
81d0912d 4213 while (*ptype == '?')
4d76d2ba 4214 ptype = next_arg_type(ptype);
81d0912d
FB
4215 }
4216 }
4217 str = args[nb_args - 1];
2a1704a7 4218 if (*ptype == '-' && ptype[1] != '\0') {
3b6dbf27 4219 ptype = next_arg_type(ptype);
2a1704a7 4220 }
81d0912d
FB
4221 switch(*ptype) {
4222 case 'F':
4223 /* file completion */
d2674b2c 4224 readline_set_completion_index(mon->rs, strlen(str));
cb8f68b1 4225 file_completion(mon, str);
81d0912d
FB
4226 break;
4227 case 'B':
4228 /* block device name completion */
599a926a
WX
4229 mbs.mon = mon;
4230 mbs.input = str;
4231 readline_set_completion_index(mon->rs, strlen(str));
4232 bdrv_iterate(block_completion_it, &mbs);
81d0912d 4233 break;
7fe48483
FB
4234 case 's':
4235 /* XXX: more generic ? */
4236 if (!strcmp(cmd->name, "info")) {
d2674b2c 4237 readline_set_completion_index(mon->rs, strlen(str));
7fe48483 4238 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
cd5c6bba 4239 cmd_completion(mon, str, cmd->name);
7fe48483 4240 }
64866c3d 4241 } else if (!strcmp(cmd->name, "sendkey")) {
e600d1ef
BS
4242 char *sep = strrchr(str, '-');
4243 if (sep)
4244 str = sep + 1;
d2674b2c 4245 readline_set_completion_index(mon->rs, strlen(str));
1048c88f 4246 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
cd5c6bba 4247 cmd_completion(mon, str, QKeyCode_lookup[i]);
64866c3d 4248 }
f3353c6b 4249 } else if (!strcmp(cmd->name, "help|?")) {
d2674b2c 4250 readline_set_completion_index(mon->rs, strlen(str));
7717239d 4251 for (cmd = mon->cmd_table; cmd->name != NULL; cmd++) {
cd5c6bba 4252 cmd_completion(mon, str, cmd->name);
f3353c6b 4253 }
7fe48483
FB
4254 }
4255 break;
81d0912d
FB
4256 default:
4257 break;
4258 }
4259 }
03a63484
JK
4260
4261cleanup:
4262 for (i = 0; i < nb_args; i++) {
7267c094 4263 g_free(args[i]);
03a63484 4264 }
81d0912d
FB
4265}
4266
731b0364 4267static int monitor_can_read(void *opaque)
9dc39cba 4268{
731b0364
AL
4269 Monitor *mon = opaque;
4270
c62313bb 4271 return (mon->suspend_cnt == 0) ? 1 : 0;
9dc39cba
FB
4272}
4273
09069b19 4274static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
5fa737a4 4275{
09069b19
LC
4276 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4277 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
5fa737a4
LC
4278}
4279
4af9193a
LC
4280/*
4281 * Argument validation rules:
4282 *
4283 * 1. The argument must exist in cmd_args qdict
4284 * 2. The argument type must be the expected one
4285 *
4286 * Special case: If the argument doesn't exist in cmd_args and
4287 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4288 * checking is skipped for it.
4289 */
4290static int check_client_args_type(const QDict *client_args,
4291 const QDict *cmd_args, int flags)
5fa737a4 4292{
4af9193a 4293 const QDictEntry *ent;
5fa737a4 4294
4af9193a
LC
4295 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4296 QObject *obj;
4297 QString *arg_type;
4298 const QObject *client_arg = qdict_entry_value(ent);
4299 const char *client_arg_name = qdict_entry_key(ent);
4300
4301 obj = qdict_get(cmd_args, client_arg_name);
4302 if (!obj) {
4303 if (flags & QMP_ACCEPT_UNKNOWNS) {
4304 /* handler accepts unknowns */
4305 continue;
4306 }
4307 /* client arg doesn't exist */
4308 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4309 return -1;
4310 }
5fa737a4 4311
4af9193a
LC
4312 arg_type = qobject_to_qstring(obj);
4313 assert(arg_type != NULL);
5fa737a4 4314
4af9193a
LC
4315 /* check if argument's type is correct */
4316 switch (qstring_get_str(arg_type)[0]) {
5fa737a4
LC
4317 case 'F':
4318 case 'B':
4319 case 's':
4af9193a
LC
4320 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4321 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4322 "string");
5fa737a4
LC
4323 return -1;
4324 }
4af9193a 4325 break;
5fa737a4
LC
4326 case 'i':
4327 case 'l':
b6e098d7 4328 case 'M':
dbc0c67f 4329 case 'o':
4af9193a
LC
4330 if (qobject_type(client_arg) != QTYPE_QINT) {
4331 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4332 "int");
4333 return -1;
5fa737a4
LC
4334 }
4335 break;
fccfb11e 4336 case 'T':
4af9193a
LC
4337 if (qobject_type(client_arg) != QTYPE_QINT &&
4338 qobject_type(client_arg) != QTYPE_QFLOAT) {
4339 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4340 "number");
4341 return -1;
3350a4dd
MA
4342 }
4343 break;
942cd1f2 4344 case 'b':
5fa737a4 4345 case '-':
4af9193a
LC
4346 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4347 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4348 "bool");
4349 return -1;
5fa737a4
LC
4350 }
4351 break;
361127df 4352 case 'O':
4af9193a
LC
4353 assert(flags & QMP_ACCEPT_UNKNOWNS);
4354 break;
b9f8978c
PB
4355 case 'q':
4356 /* Any QObject can be passed. */
4357 break;
4af9193a
LC
4358 case '/':
4359 case '.':
4360 /*
4361 * These types are not supported by QMP and thus are not
4362 * handled here. Fall through.
4363 */
5fa737a4 4364 default:
5fa737a4 4365 abort();
4af9193a
LC
4366 }
4367 }
4368
4369 return 0;
4370}
4371
2dbc8db0
LC
4372/*
4373 * - Check if the client has passed all mandatory args
4374 * - Set special flags for argument validation
4375 */
4376static int check_mandatory_args(const QDict *cmd_args,
4377 const QDict *client_args, int *flags)
4378{
4379 const QDictEntry *ent;
4380
4381 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4382 const char *cmd_arg_name = qdict_entry_key(ent);
4383 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4384 assert(type != NULL);
4385
4386 if (qstring_get_str(type)[0] == 'O') {
4387 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4388 *flags |= QMP_ACCEPT_UNKNOWNS;
4389 } else if (qstring_get_str(type)[0] != '-' &&
4390 qstring_get_str(type)[1] != '?' &&
4391 !qdict_haskey(client_args, cmd_arg_name)) {
4392 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4393 return -1;
4394 }
5fa737a4
LC
4395 }
4396
4397 return 0;
4398}
4399
2dbc8db0 4400static QDict *qdict_from_args_type(const char *args_type)
5fa737a4 4401{
2dbc8db0
LC
4402 int i;
4403 QDict *qdict;
4404 QString *key, *type, *cur_qs;
4405
4406 assert(args_type != NULL);
4407
4408 qdict = qdict_new();
4409
4410 if (args_type == NULL || args_type[0] == '\0') {
4411 /* no args, empty qdict */
4412 goto out;
4413 }
4414
4415 key = qstring_new();
4416 type = qstring_new();
4417
4418 cur_qs = key;
4419
4420 for (i = 0;; i++) {
4421 switch (args_type[i]) {
4422 case ',':
4423 case '\0':
4424 qdict_put(qdict, qstring_get_str(key), type);
4425 QDECREF(key);
4426 if (args_type[i] == '\0') {
4427 goto out;
4428 }
4429 type = qstring_new(); /* qdict has ref */
4430 cur_qs = key = qstring_new();
4431 break;
4432 case ':':
4433 cur_qs = type;
4434 break;
4435 default:
4436 qstring_append_chr(cur_qs, args_type[i]);
4437 break;
4438 }
4439 }
4440
4441out:
4442 return qdict;
5fa737a4
LC
4443}
4444
2dbc8db0
LC
4445/*
4446 * Client argument checking rules:
4447 *
4448 * 1. Client must provide all mandatory arguments
4af9193a
LC
4449 * 2. Each argument provided by the client must be expected
4450 * 3. Each argument provided by the client must have the type expected
4451 * by the command
2dbc8db0
LC
4452 */
4453static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
361127df 4454{
2dbc8db0
LC
4455 int flags, err;
4456 QDict *cmd_args;
4457
4458 cmd_args = qdict_from_args_type(cmd->args_type);
4459
4460 flags = 0;
4461 err = check_mandatory_args(cmd_args, client_args, &flags);
4462 if (err) {
4463 goto out;
4464 }
4465
4af9193a 4466 err = check_client_args_type(client_args, cmd_args, flags);
2dbc8db0
LC
4467
4468out:
4469 QDECREF(cmd_args);
4470 return err;
361127df
MA
4471}
4472
5fa737a4 4473/*
c917c8f3 4474 * Input object checking rules
5fa737a4 4475 *
c917c8f3
LC
4476 * 1. Input object must be a dict
4477 * 2. The "execute" key must exist
4478 * 3. The "execute" key must be a string
4479 * 4. If the "arguments" key exists, it must be a dict
4480 * 5. If the "id" key exists, it can be anything (ie. json-value)
4481 * 6. Any argument not listed above is considered invalid
5fa737a4 4482 */
c917c8f3 4483static QDict *qmp_check_input_obj(QObject *input_obj)
5fa737a4 4484{
c917c8f3
LC
4485 const QDictEntry *ent;
4486 int has_exec_key = 0;
4487 QDict *input_dict;
5fa737a4 4488
c917c8f3
LC
4489 if (qobject_type(input_obj) != QTYPE_QDICT) {
4490 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4491 return NULL;
5fa737a4
LC
4492 }
4493
c917c8f3 4494 input_dict = qobject_to_qdict(input_obj);
5fa737a4 4495
c917c8f3
LC
4496 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4497 const char *arg_name = qdict_entry_key(ent);
4498 const QObject *arg_obj = qdict_entry_value(ent);
5fa737a4 4499
c917c8f3
LC
4500 if (!strcmp(arg_name, "execute")) {
4501 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4502 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4503 "string");
4504 return NULL;
361127df 4505 }
c917c8f3
LC
4506 has_exec_key = 1;
4507 } else if (!strcmp(arg_name, "arguments")) {
4508 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4509 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4510 "object");
4511 return NULL;
5fa737a4 4512 }
c917c8f3
LC
4513 } else if (!strcmp(arg_name, "id")) {
4514 /* FIXME: check duplicated IDs for async commands */
5fa737a4 4515 } else {
c917c8f3
LC
4516 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4517 return NULL;
5fa737a4
LC
4518 }
4519 }
4520
c917c8f3
LC
4521 if (!has_exec_key) {
4522 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4523 return NULL;
4524 }
5fa737a4 4525
c917c8f3 4526 return input_dict;
09069b19
LC
4527}
4528
fc29df75
LC
4529static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4530 const QDict *params)
4531{
4532 int ret;
4533 QObject *data = NULL;
4534
fc29df75
LC
4535 ret = cmd->mhandler.cmd_new(mon, params, &data);
4536 handler_audit(mon, cmd, ret);
4537 monitor_protocol_emitter(mon, data);
4538 qobject_decref(data);
4539}
4540
5fa737a4
LC
4541static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4542{
4543 int err;
4544 QObject *obj;
4545 QDict *input, *args;
5fa737a4 4546 const mon_cmd_t *cmd;
40e5a01d 4547 const char *cmd_name;
5fa737a4
LC
4548 Monitor *mon = cur_mon;
4549
e4940c60 4550 args = input = NULL;
5fa737a4
LC
4551
4552 obj = json_parser_parse(tokens, NULL);
4553 if (!obj) {
4554 // FIXME: should be triggered in json_parser_parse()
ab5b027e 4555 qerror_report(QERR_JSON_PARSING);
5fa737a4 4556 goto err_out;
5fa737a4
LC
4557 }
4558
c917c8f3
LC
4559 input = qmp_check_input_obj(obj);
4560 if (!input) {
5fa737a4
LC
4561 qobject_decref(obj);
4562 goto err_out;
4563 }
4564
5fa737a4
LC
4565 mon->mc->id = qdict_get(input, "id");
4566 qobject_incref(mon->mc->id);
4567
0bbab46d 4568 cmd_name = qdict_get_str(input, "execute");
89bd820a 4569 trace_handle_qmp_command(mon, cmd_name);
09069b19 4570 if (invalid_qmp_mode(mon, cmd_name)) {
ab5b027e 4571 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
e4940c60 4572 goto err_out;
09069b19
LC
4573 }
4574
e3193601 4575 cmd = qmp_find_cmd(cmd_name);
d1249eaa 4576 if (!cmd) {
ab5b027e 4577 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
e4940c60 4578 goto err_out;
5fa737a4
LC
4579 }
4580
4581 obj = qdict_get(input, "arguments");
4582 if (!obj) {
4583 args = qdict_new();
4584 } else {
4585 args = qobject_to_qdict(obj);
4586 QINCREF(args);
4587 }
4588
2dbc8db0 4589 err = qmp_check_client_args(cmd, args);
5fa737a4
LC
4590 if (err < 0) {
4591 goto err_out;
4592 }
4593
40e5a01d 4594 if (handler_is_async(cmd)) {
5af7bbae
LC
4595 err = qmp_async_cmd_handler(mon, cmd, args);
4596 if (err) {
4597 /* emit the error response */
4598 goto err_out;
4599 }
940cc30d 4600 } else {
fc29df75 4601 qmp_call_cmd(mon, cmd, args);
940cc30d 4602 }
e4940c60 4603
5fa737a4
LC
4604 goto out;
4605
5fa737a4
LC
4606err_out:
4607 monitor_protocol_emitter(mon, NULL);
4608out:
e4940c60 4609 QDECREF(input);
5fa737a4 4610 QDECREF(args);
5fa737a4
LC
4611}
4612
9b57c02e
LC
4613/**
4614 * monitor_control_read(): Read and handle QMP input
4615 */
4616static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4617{
4618 Monitor *old_mon = cur_mon;
4619
4620 cur_mon = opaque;
4621
5fa737a4 4622 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
9b57c02e
LC
4623
4624 cur_mon = old_mon;
4625}
4626
731b0364 4627static void monitor_read(void *opaque, const uint8_t *buf, int size)
9dc39cba 4628{
731b0364 4629 Monitor *old_mon = cur_mon;
7e2515e8 4630 int i;
376253ec 4631
731b0364
AL
4632 cur_mon = opaque;
4633
cde76ee1
AL
4634 if (cur_mon->rs) {
4635 for (i = 0; i < size; i++)
4636 readline_handle_byte(cur_mon->rs, buf[i]);
4637 } else {
4638 if (size == 0 || buf[size - 1] != 0)
4639 monitor_printf(cur_mon, "corrupted command\n");
4640 else
f3c157c4 4641 handle_user_command(cur_mon, (char *)buf);
cde76ee1 4642 }
9dc39cba 4643
731b0364
AL
4644 cur_mon = old_mon;
4645}
d8f44609 4646
376253ec 4647static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
aa455485 4648{
731b0364 4649 monitor_suspend(mon);
f3c157c4 4650 handle_user_command(mon, cmdline);
731b0364 4651 monitor_resume(mon);
d8f44609
AL
4652}
4653
cde76ee1 4654int monitor_suspend(Monitor *mon)
d8f44609 4655{
cde76ee1
AL
4656 if (!mon->rs)
4657 return -ENOTTY;
731b0364 4658 mon->suspend_cnt++;
cde76ee1 4659 return 0;
d8f44609
AL
4660}
4661
376253ec 4662void monitor_resume(Monitor *mon)
d8f44609 4663{
cde76ee1
AL
4664 if (!mon->rs)
4665 return;
731b0364
AL
4666 if (--mon->suspend_cnt == 0)
4667 readline_show_prompt(mon->rs);
aa455485
FB
4668}
4669
ca9567e2
LC
4670static QObject *get_qmp_greeting(void)
4671{
b9c15f16 4672 QObject *ver = NULL;
ca9567e2 4673
b9c15f16 4674 qmp_marshal_input_query_version(NULL, NULL, &ver);
ca9567e2
LC
4675 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4676}
4677
9b57c02e
LC
4678/**
4679 * monitor_control_event(): Print QMP gretting
4680 */
4681static void monitor_control_event(void *opaque, int event)
4682{
47116d1c
LC
4683 QObject *data;
4684 Monitor *mon = opaque;
9b57c02e 4685
47116d1c
LC
4686 switch (event) {
4687 case CHR_EVENT_OPENED:
4a7e1190 4688 mon->mc->command_mode = 0;
ca9567e2 4689 data = get_qmp_greeting();
9b57c02e
LC
4690 monitor_json_emitter(mon, data);
4691 qobject_decref(data);
efb87c16 4692 mon_refcount++;
47116d1c
LC
4693 break;
4694 case CHR_EVENT_CLOSED:
4695 json_message_parser_destroy(&mon->mc->parser);
58617a79 4696 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
efb87c16
CB
4697 mon_refcount--;
4698 monitor_fdsets_cleanup();
47116d1c 4699 break;
9b57c02e
LC
4700 }
4701}
4702
731b0364 4703static void monitor_event(void *opaque, int event)
86e94dea 4704{
376253ec
AL
4705 Monitor *mon = opaque;
4706
2724b180
AL
4707 switch (event) {
4708 case CHR_EVENT_MUX_IN:
a7aec5da
GH
4709 mon->mux_out = 0;
4710 if (mon->reset_seen) {
4711 readline_restart(mon->rs);
4712 monitor_resume(mon);
4713 monitor_flush(mon);
4714 } else {
4715 mon->suspend_cnt = 0;
4716 }
2724b180
AL
4717 break;
4718
4719 case CHR_EVENT_MUX_OUT:
a7aec5da
GH
4720 if (mon->reset_seen) {
4721 if (mon->suspend_cnt == 0) {
4722 monitor_printf(mon, "\n");
4723 }
4724 monitor_flush(mon);
4725 monitor_suspend(mon);
4726 } else {
4727 mon->suspend_cnt++;
4728 }
4729 mon->mux_out = 1;
2724b180 4730 break;
86e94dea 4731
b6b8df56 4732 case CHR_EVENT_OPENED:
2724b180
AL
4733 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4734 "information\n", QEMU_VERSION);
a7aec5da 4735 if (!mon->mux_out) {
2724b180 4736 readline_show_prompt(mon->rs);
a7aec5da
GH
4737 }
4738 mon->reset_seen = 1;
efb87c16
CB
4739 mon_refcount++;
4740 break;
4741
4742 case CHR_EVENT_CLOSED:
4743 mon_refcount--;
4744 monitor_fdsets_cleanup();
2724b180
AL
4745 break;
4746 }
86e94dea
TS
4747}
4748
816f8925
WX
4749static int
4750compare_mon_cmd(const void *a, const void *b)
4751{
4752 return strcmp(((const mon_cmd_t *)a)->name,
4753 ((const mon_cmd_t *)b)->name);
4754}
4755
4756static void sortcmdlist(void)
4757{
4758 int array_num;
4759 int elem_size = sizeof(mon_cmd_t);
4760
4761 array_num = sizeof(mon_cmds)/elem_size-1;
4762 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4763
4764 array_num = sizeof(info_cmds)/elem_size-1;
4765 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4766}
4767
76655d6d
AL
4768
4769/*
4770 * Local variables:
4771 * c-indent-level: 4
4772 * c-basic-offset: 4
4773 * tab-width: 8
4774 * End:
4775 */
4776
731b0364 4777void monitor_init(CharDriverState *chr, int flags)
aa455485 4778{
731b0364 4779 static int is_first_init = 1;
87127161 4780 Monitor *mon;
20d8a3ed
TS
4781
4782 if (is_first_init) {
afeecec2 4783 monitor_protocol_event_init();
d038317c 4784 sortcmdlist();
20d8a3ed
TS
4785 is_first_init = 0;
4786 }
87127161 4787
b01fe89e
WX
4788 mon = g_malloc(sizeof(*mon));
4789 monitor_data_init(mon);
20d8a3ed 4790
87127161 4791 mon->chr = chr;
731b0364 4792 mon->flags = flags;
cde76ee1
AL
4793 if (flags & MONITOR_USE_READLINE) {
4794 mon->rs = readline_init(mon, monitor_find_completion);
4795 monitor_read_command(mon, 0);
4796 }
87127161 4797
9b57c02e 4798 if (monitor_ctrl_mode(mon)) {
7267c094 4799 mon->mc = g_malloc0(sizeof(MonitorControl));
9b57c02e
LC
4800 /* Control mode requires special handlers */
4801 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4802 monitor_control_event, mon);
15f31519 4803 qemu_chr_fe_set_echo(chr, true);
26efaca3
AL
4804
4805 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
9b57c02e
LC
4806 } else {
4807 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4808 monitor_event, mon);
4809 }
87127161 4810
72cf2d4f 4811 QLIST_INSERT_HEAD(&mon_list, mon, entry);
8631b608
MA
4812 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4813 default_mon = mon;
aa455485
FB
4814}
4815
376253ec 4816static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
81d0912d 4817{
bb5fc20f
AL
4818 BlockDriverState *bs = opaque;
4819 int ret = 0;
81d0912d 4820
bb5fc20f 4821 if (bdrv_set_key(bs, password) != 0) {
376253ec 4822 monitor_printf(mon, "invalid password\n");
bb5fc20f 4823 ret = -EPERM;
9dc39cba 4824 }
731b0364
AL
4825 if (mon->password_completion_cb)
4826 mon->password_completion_cb(mon->password_opaque, ret);
bb5fc20f 4827
731b0364 4828 monitor_read_command(mon, 1);
9dc39cba 4829}
c0f4ce77 4830
7060b478
AL
4831ReadLineState *monitor_get_rs(Monitor *mon)
4832{
4833 return mon->rs;
4834}
4835
0bbc47bb
LC
4836int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4837 BlockDriverCompletionFunc *completion_cb,
4838 void *opaque)
c0f4ce77 4839{
cde76ee1
AL
4840 int err;
4841
bb5fc20f
AL
4842 if (!bdrv_key_required(bs)) {
4843 if (completion_cb)
4844 completion_cb(opaque, 0);
0bbc47bb 4845 return 0;
bb5fc20f 4846 }
c0f4ce77 4847
94171e11 4848 if (monitor_ctrl_mode(mon)) {
903a8814
LC
4849 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4850 bdrv_get_encrypted_filename(bs));
0bbc47bb 4851 return -1;
94171e11
LC
4852 }
4853
376253ec
AL
4854 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4855 bdrv_get_encrypted_filename(bs));
bb5fc20f 4856
731b0364
AL
4857 mon->password_completion_cb = completion_cb;
4858 mon->password_opaque = opaque;
bb5fc20f 4859
cde76ee1
AL
4860 err = monitor_read_password(mon, bdrv_password_cb, bs);
4861
4862 if (err && completion_cb)
4863 completion_cb(opaque, err);
0bbc47bb
LC
4864
4865 return err;
c0f4ce77 4866}
e42e818b
LC
4867
4868int monitor_read_block_device_key(Monitor *mon, const char *device,
4869 BlockDriverCompletionFunc *completion_cb,
4870 void *opaque)
4871{
4872 BlockDriverState *bs;
4873
4874 bs = bdrv_find(device);
4875 if (!bs) {
4876 monitor_printf(mon, "Device not found %s\n", device);
4877 return -1;
4878 }
4879
4880 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
4881}
4d454574
PB
4882
4883QemuOptsList qemu_mon_opts = {
4884 .name = "mon",
4885 .implied_opt_name = "chardev",
4886 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4887 .desc = {
4888 {
4889 .name = "mode",
4890 .type = QEMU_OPT_STRING,
4891 },{
4892 .name = "chardev",
4893 .type = QEMU_OPT_STRING,
4894 },{
4895 .name = "default",
4896 .type = QEMU_OPT_BOOL,
4897 },{
4898 .name = "pretty",
4899 .type = QEMU_OPT_BOOL,
4900 },
4901 { /* end of list */ }
4902 },
4903};