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