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