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