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