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