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