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