]> git.proxmox.com Git - qemu.git/blame - monitor.c
qemu-option: Rename find_list() to qemu_find_opts() & external linkage
[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"
cae4956e 26#include "hw/qdev.h"
87ecb68b
PB
27#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
9dd986cc 31#include "hw/watchdog.h"
45a50b16 32#include "hw/loader.h"
87ecb68b
PB
33#include "gdbstub.h"
34#include "net.h"
68ac40d2 35#include "net/slirp.h"
87ecb68b
PB
36#include "qemu-char.h"
37#include "sysemu.h"
376253ec
AL
38#include "monitor.h"
39#include "readline.h"
87ecb68b
PB
40#include "console.h"
41#include "block.h"
42#include "audio/audio.h"
9307c4c1 43#include "disas.h"
df751fa8 44#include "balloon.h"
c8256f9d 45#include "qemu-timer.h"
5bb7910a 46#include "migration.h"
7ba1e619 47#include "kvm.h"
76655d6d 48#include "acl.h"
f7188bbe 49#include "qint.h"
3350a4dd 50#include "qfloat.h"
8f3cec0b 51#include "qlist.h"
f7188bbe 52#include "qdict.h"
5fa737a4 53#include "qbool.h"
f7188bbe 54#include "qstring.h"
8204a918 55#include "qerror.h"
9b57c02e 56#include "qjson.h"
5fa737a4
LC
57#include "json-streamer.h"
58#include "json-parser.h"
d08d6f04 59#include "osdep.h"
6a5bd307 60
9dc39cba 61//#define DEBUG
81d0912d 62//#define DEBUG_COMPLETION
9dc39cba 63
9307c4c1
FB
64/*
65 * Supported types:
5fafdf24 66 *
9307c4c1 67 * 'F' filename
81d0912d 68 * 'B' block device name
9307c4c1 69 * 's' string (accept optional quote)
92a31b1f
FB
70 * 'i' 32 bit integer
71 * 'l' target long (32 or 64 bit)
9fec543f
MA
72 * 'M' just like 'l', except in user mode the value is
73 * multiplied by 2^20 (think Mebibyte)
3350a4dd
MA
74 * 'b' double
75 * user mode accepts an optional G, g, M, m, K, k suffix,
76 * which multiplies the value by 2^30 for suffixes G and
77 * g, 2^20 for M and m, 2^10 for K and k
fccfb11e
MA
78 * 'T' double
79 * user mode accepts an optional ms, us, ns suffix,
80 * which divides the value by 1e3, 1e6, 1e9, respectively
9307c4c1
FB
81 * '/' optional gdb-like print format (like "/10x")
82 *
fb46660e
LC
83 * '?' optional type (for all types, except '/')
84 * '.' other form of optional type (for 'i' and 'l')
85 * '-' optional parameter (eg. '-f')
9307c4c1
FB
86 *
87 */
88
940cc30d
AL
89typedef struct MonitorCompletionData MonitorCompletionData;
90struct MonitorCompletionData {
91 Monitor *mon;
92 void (*user_print)(Monitor *mon, const QObject *data);
93};
94
c227f099 95typedef struct mon_cmd_t {
9dc39cba 96 const char *name;
9307c4c1 97 const char *args_type;
9dc39cba
FB
98 const char *params;
99 const char *help;
a2876f59 100 void (*user_print)(Monitor *mon, const QObject *data);
910df89d
LC
101 union {
102 void (*info)(Monitor *mon);
13c7425e 103 void (*info_new)(Monitor *mon, QObject **ret_data);
940cc30d 104 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
af4ce882 105 void (*cmd)(Monitor *mon, const QDict *qdict);
261394db 106 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
940cc30d
AL
107 int (*cmd_async)(Monitor *mon, const QDict *params,
108 MonitorCompletion *cb, void *opaque);
910df89d 109 } mhandler;
940cc30d 110 int async;
c227f099 111} mon_cmd_t;
9dc39cba 112
f07918fd 113/* file descriptors passed via SCM_RIGHTS */
c227f099
AL
114typedef struct mon_fd_t mon_fd_t;
115struct mon_fd_t {
f07918fd
MM
116 char *name;
117 int fd;
c227f099 118 QLIST_ENTRY(mon_fd_t) next;
f07918fd
MM
119};
120
5fa737a4
LC
121typedef struct MonitorControl {
122 QObject *id;
123 JSONMessageParser parser;
4a7e1190 124 int command_mode;
5fa737a4
LC
125} MonitorControl;
126
87127161
AL
127struct Monitor {
128 CharDriverState *chr;
a7aec5da
GH
129 int mux_out;
130 int reset_seen;
731b0364
AL
131 int flags;
132 int suspend_cnt;
133 uint8_t outbuf[1024];
134 int outbuf_index;
135 ReadLineState *rs;
5fa737a4 136 MonitorControl *mc;
731b0364
AL
137 CPUState *mon_cpu;
138 BlockDriverCompletionFunc *password_completion_cb;
139 void *password_opaque;
10e4f606
LC
140#ifdef CONFIG_DEBUG_MONITOR
141 int print_calls_nr;
142#endif
8204a918 143 QError *error;
c227f099 144 QLIST_HEAD(,mon_fd_t) fds;
72cf2d4f 145 QLIST_ENTRY(Monitor) entry;
87127161
AL
146};
147
b4475aa2
LC
148#ifdef CONFIG_DEBUG_MONITOR
149#define MON_DEBUG(fmt, ...) do { \
150 fprintf(stderr, "Monitor: "); \
151 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
10e4f606
LC
152
153static inline void mon_print_count_inc(Monitor *mon)
154{
155 mon->print_calls_nr++;
156}
157
158static inline void mon_print_count_init(Monitor *mon)
159{
160 mon->print_calls_nr = 0;
161}
162
163static inline int mon_print_count_get(const Monitor *mon)
164{
165 return mon->print_calls_nr;
166}
167
b4475aa2
LC
168#else /* !CONFIG_DEBUG_MONITOR */
169#define MON_DEBUG(fmt, ...) do { } while (0)
10e4f606
LC
170static inline void mon_print_count_inc(Monitor *mon) { }
171static inline void mon_print_count_init(Monitor *mon) { }
172static inline int mon_print_count_get(const Monitor *mon) { return 0; }
b4475aa2
LC
173#endif /* CONFIG_DEBUG_MONITOR */
174
72cf2d4f 175static QLIST_HEAD(mon_list, Monitor) mon_list;
7e2515e8 176
c227f099
AL
177static const mon_cmd_t mon_cmds[];
178static const mon_cmd_t info_cmds[];
9dc39cba 179
8631b608
MA
180Monitor *cur_mon;
181Monitor *default_mon;
376253ec 182
731b0364
AL
183static void monitor_command_cb(Monitor *mon, const char *cmdline,
184 void *opaque);
83ab7950 185
09069b19
LC
186static inline int qmp_cmd_mode(const Monitor *mon)
187{
188 return (mon->mc ? mon->mc->command_mode : 0);
189}
190
418173c7
LC
191/* Return true if in control mode, false otherwise */
192static inline int monitor_ctrl_mode(const Monitor *mon)
193{
194 return (mon->flags & MONITOR_USE_CONTROL);
195}
196
6620d3ce
MA
197/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
198int monitor_cur_is_qmp(void)
199{
200 return cur_mon && monitor_ctrl_mode(cur_mon);
201}
202
731b0364
AL
203static void monitor_read_command(Monitor *mon, int show_prompt)
204{
183e6e52
LC
205 if (!mon->rs)
206 return;
207
731b0364
AL
208 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
209 if (show_prompt)
210 readline_show_prompt(mon->rs);
211}
6a00d601 212
cde76ee1
AL
213static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
214 void *opaque)
bb5fc20f 215{
94171e11 216 if (monitor_ctrl_mode(mon)) {
ab5b027e 217 qerror_report(QERR_MISSING_PARAMETER, "password");
94171e11
LC
218 return -EINVAL;
219 } else if (mon->rs) {
cde76ee1
AL
220 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
221 /* prompt is printed on return from the command handler */
222 return 0;
223 } else {
224 monitor_printf(mon, "terminal does not support password prompting\n");
225 return -ENOTTY;
226 }
bb5fc20f
AL
227}
228
376253ec 229void monitor_flush(Monitor *mon)
7e2515e8 230{
a7aec5da 231 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
731b0364
AL
232 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
233 mon->outbuf_index = 0;
7e2515e8
FB
234 }
235}
236
237/* flush at every end of line or if the buffer is full */
376253ec 238static void monitor_puts(Monitor *mon, const char *str)
7e2515e8 239{
60fe76f3 240 char c;
731b0364 241
7e2515e8
FB
242 for(;;) {
243 c = *str++;
244 if (c == '\0')
245 break;
7ba1260a 246 if (c == '\n')
731b0364
AL
247 mon->outbuf[mon->outbuf_index++] = '\r';
248 mon->outbuf[mon->outbuf_index++] = c;
249 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
250 || c == '\n')
376253ec 251 monitor_flush(mon);
7e2515e8
FB
252 }
253}
254
376253ec 255void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
9dc39cba 256{
b8b08266
LC
257 char buf[4096];
258
2daa1191
LC
259 if (!mon)
260 return;
261
10e4f606
LC
262 mon_print_count_inc(mon);
263
b8b08266 264 if (monitor_ctrl_mode(mon)) {
b8b08266 265 return;
4a29a85d 266 }
b8b08266
LC
267
268 vsnprintf(buf, sizeof(buf), fmt, ap);
269 monitor_puts(mon, buf);
9dc39cba
FB
270}
271
376253ec 272void monitor_printf(Monitor *mon, const char *fmt, ...)
9dc39cba 273{
7e2515e8
FB
274 va_list ap;
275 va_start(ap, fmt);
376253ec 276 monitor_vprintf(mon, fmt, ap);
7e2515e8 277 va_end(ap);
9dc39cba
FB
278}
279
376253ec 280void monitor_print_filename(Monitor *mon, const char *filename)
fef30743
TS
281{
282 int i;
283
284 for (i = 0; filename[i]; i++) {
28a76be8
AL
285 switch (filename[i]) {
286 case ' ':
287 case '"':
288 case '\\':
289 monitor_printf(mon, "\\%c", filename[i]);
290 break;
291 case '\t':
292 monitor_printf(mon, "\\t");
293 break;
294 case '\r':
295 monitor_printf(mon, "\\r");
296 break;
297 case '\n':
298 monitor_printf(mon, "\\n");
299 break;
300 default:
301 monitor_printf(mon, "%c", filename[i]);
302 break;
303 }
fef30743
TS
304 }
305}
306
7fe48483
FB
307static int monitor_fprintf(FILE *stream, const char *fmt, ...)
308{
309 va_list ap;
310 va_start(ap, fmt);
376253ec 311 monitor_vprintf((Monitor *)stream, fmt, ap);
7fe48483
FB
312 va_end(ap);
313 return 0;
314}
315
13c7425e
LC
316static void monitor_user_noop(Monitor *mon, const QObject *data) { }
317
13917bee
LC
318static inline int monitor_handler_ported(const mon_cmd_t *cmd)
319{
320 return cmd->user_print != NULL;
321}
322
940cc30d
AL
323static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
324{
325 return cmd->async != 0;
326}
327
8204a918
LC
328static inline int monitor_has_error(const Monitor *mon)
329{
330 return mon->error != NULL;
331}
332
9b57c02e
LC
333static void monitor_json_emitter(Monitor *mon, const QObject *data)
334{
335 QString *json;
336
337 json = qobject_to_json(data);
338 assert(json != NULL);
339
b8b08266
LC
340 qstring_append_chr(json, '\n');
341 monitor_puts(mon, qstring_get_str(json));
4a29a85d 342
9b57c02e
LC
343 QDECREF(json);
344}
345
25b422eb
LC
346static void monitor_protocol_emitter(Monitor *mon, QObject *data)
347{
348 QDict *qmp;
349
350 qmp = qdict_new();
351
352 if (!monitor_has_error(mon)) {
353 /* success response */
354 if (data) {
355 qobject_incref(data);
356 qdict_put_obj(qmp, "return", data);
357 } else {
0abc6579
LC
358 /* return an empty QDict by default */
359 qdict_put(qmp, "return", qdict_new());
25b422eb
LC
360 }
361 } else {
362 /* error response */
77e595e7 363 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
25b422eb
LC
364 qdict_put(qmp, "error", mon->error->error);
365 QINCREF(mon->error->error);
366 QDECREF(mon->error);
367 mon->error = NULL;
368 }
369
5fa737a4
LC
370 if (mon->mc->id) {
371 qdict_put_obj(qmp, "id", mon->mc->id);
372 mon->mc->id = NULL;
373 }
374
25b422eb
LC
375 monitor_json_emitter(mon, QOBJECT(qmp));
376 QDECREF(qmp);
377}
378
0d1ea871
LC
379static void timestamp_put(QDict *qdict)
380{
381 int err;
382 QObject *obj;
d08d6f04 383 qemu_timeval tv;
0d1ea871 384
d08d6f04 385 err = qemu_gettimeofday(&tv);
0d1ea871
LC
386 if (err < 0)
387 return;
388
389 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
390 "'microseconds': %" PRId64 " }",
391 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
0d1ea871
LC
392 qdict_put_obj(qdict, "timestamp", obj);
393}
394
395/**
396 * monitor_protocol_event(): Generate a Monitor event
397 *
398 * Event-specific data can be emitted through the (optional) 'data' parameter.
399 */
400void monitor_protocol_event(MonitorEvent event, QObject *data)
401{
402 QDict *qmp;
403 const char *event_name;
f039a563 404 Monitor *mon;
0d1ea871 405
242cd003 406 assert(event < QEVENT_MAX);
0d1ea871 407
0d1ea871 408 switch (event) {
242cd003 409 case QEVENT_SHUTDOWN:
b1a15e7e
LC
410 event_name = "SHUTDOWN";
411 break;
242cd003 412 case QEVENT_RESET:
b1a15e7e
LC
413 event_name = "RESET";
414 break;
242cd003 415 case QEVENT_POWERDOWN:
b1a15e7e
LC
416 event_name = "POWERDOWN";
417 break;
242cd003 418 case QEVENT_STOP:
b1a15e7e
LC
419 event_name = "STOP";
420 break;
586153d9
LC
421 case QEVENT_VNC_CONNECTED:
422 event_name = "VNC_CONNECTED";
423 break;
0d2ed46a
LC
424 case QEVENT_VNC_INITIALIZED:
425 event_name = "VNC_INITIALIZED";
426 break;
0d72f3d3
LC
427 case QEVENT_VNC_DISCONNECTED:
428 event_name = "VNC_DISCONNECTED";
429 break;
aa1db6ed
LC
430 case QEVENT_BLOCK_IO_ERROR:
431 event_name = "BLOCK_IO_ERROR";
432 break;
80cd3478
LC
433 case QEVENT_RTC_CHANGE:
434 event_name = "RTC_CHANGE";
435 break;
9eedeb3b
LC
436 case QEVENT_WATCHDOG:
437 event_name = "WATCHDOG";
438 break;
0d1ea871
LC
439 default:
440 abort();
441 break;
442 }
443
444 qmp = qdict_new();
445 timestamp_put(qmp);
446 qdict_put(qmp, "event", qstring_from_str(event_name));
3d72f9a2
LC
447 if (data) {
448 qobject_incref(data);
0d1ea871 449 qdict_put_obj(qmp, "data", data);
3d72f9a2 450 }
0d1ea871 451
f039a563 452 QLIST_FOREACH(mon, &mon_list, entry) {
09069b19 453 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
23fabed1
LC
454 monitor_json_emitter(mon, QOBJECT(qmp));
455 }
f039a563 456 }
0d1ea871
LC
457 QDECREF(qmp);
458}
459
ef4b7eee
LC
460static int do_qmp_capabilities(Monitor *mon, const QDict *params,
461 QObject **ret_data)
4a7e1190
LC
462{
463 /* Will setup QMP capabilities in the future */
464 if (monitor_ctrl_mode(mon)) {
465 mon->mc->command_mode = 1;
466 }
ef4b7eee
LC
467
468 return 0;
4a7e1190
LC
469}
470
9dc39cba
FB
471static int compare_cmd(const char *name, const char *list)
472{
473 const char *p, *pstart;
474 int len;
475 len = strlen(name);
476 p = list;
477 for(;;) {
478 pstart = p;
479 p = strchr(p, '|');
480 if (!p)
481 p = pstart + strlen(pstart);
482 if ((p - pstart) == len && !memcmp(pstart, name, len))
483 return 1;
484 if (*p == '\0')
485 break;
486 p++;
487 }
488 return 0;
489}
490
c227f099 491static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
376253ec 492 const char *prefix, const char *name)
9dc39cba 493{
c227f099 494 const mon_cmd_t *cmd;
9dc39cba
FB
495
496 for(cmd = cmds; cmd->name != NULL; cmd++) {
497 if (!name || !strcmp(name, cmd->name))
376253ec
AL
498 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
499 cmd->params, cmd->help);
9dc39cba
FB
500 }
501}
502
376253ec 503static void help_cmd(Monitor *mon, const char *name)
9dc39cba
FB
504{
505 if (name && !strcmp(name, "info")) {
376253ec 506 help_cmd_dump(mon, info_cmds, "info ", NULL);
9dc39cba 507 } else {
376253ec 508 help_cmd_dump(mon, mon_cmds, "", name);
f193c797 509 if (name && !strcmp(name, "log")) {
8662d656 510 const CPULogItem *item;
376253ec
AL
511 monitor_printf(mon, "Log items (comma separated):\n");
512 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
f193c797 513 for(item = cpu_log_items; item->mask != 0; item++) {
376253ec 514 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
f193c797
FB
515 }
516 }
9dc39cba
FB
517 }
518}
519
d54908a5 520static void do_help_cmd(Monitor *mon, const QDict *qdict)
38183186 521{
d54908a5 522 help_cmd(mon, qdict_get_try_str(qdict, "name"));
38183186
LC
523}
524
d54908a5 525static void do_commit(Monitor *mon, const QDict *qdict)
9dc39cba 526{
751c6a17
GH
527 int all_devices;
528 DriveInfo *dinfo;
d54908a5 529 const char *device = qdict_get_str(qdict, "device");
2dc7b602 530
7954c734 531 all_devices = !strcmp(device, "all");
72cf2d4f 532 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 533 if (!all_devices)
73006d2a 534 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
751c6a17
GH
535 continue;
536 bdrv_commit(dinfo->bdrv);
9dc39cba
FB
537 }
538}
539
940cc30d
AL
540static void user_monitor_complete(void *opaque, QObject *ret_data)
541{
542 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
543
544 if (ret_data) {
545 data->user_print(data->mon, ret_data);
546 }
547 monitor_resume(data->mon);
548 qemu_free(data);
549}
550
551static void qmp_monitor_complete(void *opaque, QObject *ret_data)
552{
553 monitor_protocol_emitter(opaque, ret_data);
554}
555
556static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
557 const QDict *params)
558{
559 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
560}
561
562static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
563{
564 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
565}
566
567static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
568 const QDict *params)
569{
570 int ret;
571
572 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
573 cb_data->mon = mon;
574 cb_data->user_print = cmd->user_print;
575 monitor_suspend(mon);
576 ret = cmd->mhandler.cmd_async(mon, params,
577 user_monitor_complete, cb_data);
578 if (ret < 0) {
579 monitor_resume(mon);
580 qemu_free(cb_data);
581 }
582}
583
584static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
585{
586 int ret;
587
588 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
589 cb_data->mon = mon;
590 cb_data->user_print = cmd->user_print;
591 monitor_suspend(mon);
592 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
593 if (ret < 0) {
594 monitor_resume(mon);
595 qemu_free(cb_data);
596 }
597}
598
4fdc94b4 599static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
9dc39cba 600{
c227f099 601 const mon_cmd_t *cmd;
d54908a5 602 const char *item = qdict_get_try_str(qdict, "item");
9dc39cba 603
956f1a0d
LC
604 if (!item) {
605 assert(monitor_ctrl_mode(mon) == 0);
9dc39cba 606 goto help;
956f1a0d 607 }
13c7425e
LC
608
609 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
5fafdf24 610 if (compare_cmd(item, cmd->name))
13c7425e 611 break;
9dc39cba 612 }
13c7425e 613
956f1a0d
LC
614 if (cmd->name == NULL) {
615 if (monitor_ctrl_mode(mon)) {
ab5b027e 616 qerror_report(QERR_COMMAND_NOT_FOUND, item);
4fdc94b4 617 return -1;
956f1a0d 618 }
13c7425e 619 goto help;
956f1a0d 620 }
13c7425e 621
940cc30d
AL
622 if (monitor_handler_is_async(cmd)) {
623 if (monitor_ctrl_mode(mon)) {
624 qmp_async_info_handler(mon, cmd);
625 } else {
626 user_async_info_handler(mon, cmd);
627 }
628 /*
629 * Indicate that this command is asynchronous and will not return any
630 * data (not even empty). Instead, the data will be returned via a
631 * completion callback.
632 */
633 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
634 } else if (monitor_handler_ported(cmd)) {
13c7425e 635 cmd->mhandler.info_new(mon, ret_data);
25b422eb
LC
636
637 if (!monitor_ctrl_mode(mon)) {
638 /*
639 * User Protocol function is called here, Monitor Protocol is
640 * handled by monitor_call_handler()
641 */
642 if (*ret_data)
643 cmd->user_print(mon, *ret_data);
644 }
13c7425e 645 } else {
956f1a0d
LC
646 if (monitor_ctrl_mode(mon)) {
647 /* handler not converted yet */
ab5b027e 648 qerror_report(QERR_COMMAND_NOT_FOUND, item);
4fdc94b4 649 return -1;
956f1a0d
LC
650 } else {
651 cmd->mhandler.info(mon);
652 }
13c7425e
LC
653 }
654
4fdc94b4 655 return 0;
13c7425e
LC
656
657help:
658 help_cmd(mon, "info");
4fdc94b4 659 return 0;
9dc39cba
FB
660}
661
45e914cf
LC
662static void do_info_version_print(Monitor *mon, const QObject *data)
663{
664 QDict *qdict;
665
666 qdict = qobject_to_qdict(data);
667
668 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
669 qdict_get_str(qdict, "package"));
670}
671
ab2d3187
LC
672/**
673 * do_info_version(): Show QEMU version
45e914cf
LC
674 *
675 * Return a QDict with the following information:
676 *
677 * - "qemu": QEMU's version
678 * - "package": package's version
679 *
680 * Example:
681 *
682 * { "qemu": "0.11.50", "package": "" }
ab2d3187
LC
683 */
684static void do_info_version(Monitor *mon, QObject **ret_data)
9bc9d1c7 685{
45e914cf
LC
686 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
687 QEMU_VERSION, QEMU_PKGVERSION);
9bc9d1c7
FB
688}
689
e05486cb 690static void do_info_name_print(Monitor *mon, const QObject *data)
c35734b2 691{
e05486cb
LC
692 QDict *qdict;
693
694 qdict = qobject_to_qdict(data);
695 if (qdict_size(qdict) == 0) {
696 return;
697 }
698
699 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
700}
701
702/**
703 * do_info_name(): Show VM name
704 *
705 * Return a QDict with the following information:
706 *
707 * - "name": VM's name (optional)
708 *
709 * Example:
710 *
711 * { "name": "qemu-name" }
712 */
713static void do_info_name(Monitor *mon, QObject **ret_data)
714{
715 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
716 qobject_from_jsonf("{}");
c35734b2
TS
717}
718
1a728677
LC
719static QObject *get_cmd_dict(const char *name)
720{
721 const char *p;
722
723 /* Remove '|' from some commands */
724 p = strchr(name, '|');
725 if (p) {
726 p++;
727 } else {
728 p = name;
729 }
730
731 return qobject_from_jsonf("{ 'name': %s }", p);
732}
733
e3bba9d0
LC
734/**
735 * do_info_commands(): List QMP available commands
736 *
1a728677
LC
737 * Each command is represented by a QDict, the returned QObject is a QList
738 * of all commands.
739 *
740 * The QDict contains:
741 *
742 * - "name": command's name
743 *
744 * Example:
745 *
746 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
e3bba9d0
LC
747 */
748static void do_info_commands(Monitor *mon, QObject **ret_data)
749{
750 QList *cmd_list;
751 const mon_cmd_t *cmd;
752
753 cmd_list = qlist_new();
754
755 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
756 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
1a728677 757 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
e3bba9d0
LC
758 }
759 }
760
761 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
762 if (monitor_handler_ported(cmd)) {
763 char buf[128];
764 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
1a728677 765 qlist_append_obj(cmd_list, get_cmd_dict(buf));
e3bba9d0
LC
766 }
767 }
768
769 *ret_data = QOBJECT(cmd_list);
770}
771
bf4f74c0 772#if defined(TARGET_I386)
14f0720d 773static void do_info_hpet_print(Monitor *mon, const QObject *data)
16b29ae1 774{
376253ec 775 monitor_printf(mon, "HPET is %s by QEMU\n",
14f0720d
LC
776 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
777 "enabled" : "disabled");
778}
779
780/**
781 * do_info_hpet(): Show HPET state
782 *
783 * Return a QDict with the following information:
784 *
785 * - "enabled": true if hpet if enabled, false otherwise
786 *
787 * Example:
788 *
789 * { "enabled": true }
790 */
791static void do_info_hpet(Monitor *mon, QObject **ret_data)
792{
793 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
16b29ae1 794}
bf4f74c0 795#endif
16b29ae1 796
9603ceba 797static void do_info_uuid_print(Monitor *mon, const QObject *data)
a36e69dd 798{
9603ceba
LC
799 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
800}
801
802/**
803 * do_info_uuid(): Show VM UUID
804 *
805 * Return a QDict with the following information:
806 *
807 * - "UUID": Universally Unique Identifier
808 *
809 * Example:
810 *
811 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
812 */
813static void do_info_uuid(Monitor *mon, QObject **ret_data)
814{
815 char uuid[64];
816
817 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
376253ec
AL
818 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
819 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
820 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
821 qemu_uuid[14], qemu_uuid[15]);
9603ceba 822 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
a36e69dd
TS
823}
824
6a00d601 825/* get the current CPU defined by the user */
9596ebb7 826static int mon_set_cpu(int cpu_index)
6a00d601
FB
827{
828 CPUState *env;
829
830 for(env = first_cpu; env != NULL; env = env->next_cpu) {
831 if (env->cpu_index == cpu_index) {
731b0364 832 cur_mon->mon_cpu = env;
6a00d601
FB
833 return 0;
834 }
835 }
836 return -1;
837}
838
9596ebb7 839static CPUState *mon_get_cpu(void)
6a00d601 840{
731b0364 841 if (!cur_mon->mon_cpu) {
6a00d601
FB
842 mon_set_cpu(0);
843 }
4c0960c0 844 cpu_synchronize_state(cur_mon->mon_cpu);
731b0364 845 return cur_mon->mon_cpu;
6a00d601
FB
846}
847
376253ec 848static void do_info_registers(Monitor *mon)
9307c4c1 849{
6a00d601
FB
850 CPUState *env;
851 env = mon_get_cpu();
9307c4c1 852#ifdef TARGET_I386
376253ec 853 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
d24b15a8 854 X86_DUMP_FPU);
9307c4c1 855#else
376253ec 856 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
7fe48483 857 0);
9307c4c1
FB
858#endif
859}
860
8f3cec0b
LC
861static void print_cpu_iter(QObject *obj, void *opaque)
862{
863 QDict *cpu;
864 int active = ' ';
865 Monitor *mon = opaque;
866
867 assert(qobject_type(obj) == QTYPE_QDICT);
868 cpu = qobject_to_qdict(obj);
869
55483ad6 870 if (qdict_get_bool(cpu, "current")) {
8f3cec0b 871 active = '*';
55483ad6 872 }
8f3cec0b
LC
873
874 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
875
876#if defined(TARGET_I386)
877 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
878 (target_ulong) qdict_get_int(cpu, "pc"));
879#elif defined(TARGET_PPC)
880 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
881 (target_long) qdict_get_int(cpu, "nip"));
882#elif defined(TARGET_SPARC)
883 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
884 (target_long) qdict_get_int(cpu, "pc"));
885 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
886 (target_long) qdict_get_int(cpu, "npc"));
887#elif defined(TARGET_MIPS)
888 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
889 (target_long) qdict_get_int(cpu, "PC"));
890#endif
891
55483ad6 892 if (qdict_get_bool(cpu, "halted")) {
8f3cec0b 893 monitor_printf(mon, " (halted)");
55483ad6 894 }
8f3cec0b
LC
895
896 monitor_printf(mon, "\n");
897}
898
899static void monitor_print_cpus(Monitor *mon, const QObject *data)
900{
901 QList *cpu_list;
902
903 assert(qobject_type(data) == QTYPE_QLIST);
904 cpu_list = qobject_to_qlist(data);
905 qlist_iter(cpu_list, print_cpu_iter, mon);
906}
907
908/**
909 * do_info_cpus(): Show CPU information
910 *
55483ad6 911 * Return a QList. Each CPU is represented by a QDict, which contains:
8f3cec0b 912 *
55483ad6
LC
913 * - "cpu": CPU index
914 * - "current": true if this is the current CPU, false otherwise
915 * - "halted": true if the cpu is halted, false otherwise
916 * - Current program counter. The key's name depends on the architecture:
917 * "pc": i386/x86)64
918 * "nip": PPC
919 * "pc" and "npc": sparc
920 * "PC": mips
8f3cec0b 921 *
55483ad6
LC
922 * Example:
923 *
924 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
925 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
8f3cec0b
LC
926 */
927static void do_info_cpus(Monitor *mon, QObject **ret_data)
6a00d601
FB
928{
929 CPUState *env;
8f3cec0b
LC
930 QList *cpu_list;
931
932 cpu_list = qlist_new();
6a00d601
FB
933
934 /* just to set the default cpu if not already done */
935 mon_get_cpu();
936
937 for(env = first_cpu; env != NULL; env = env->next_cpu) {
55483ad6
LC
938 QDict *cpu;
939 QObject *obj;
8f3cec0b 940
4c0960c0 941 cpu_synchronize_state(env);
8f3cec0b 942
55483ad6
LC
943 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
944 env->cpu_index, env == mon->mon_cpu,
945 env->halted);
55483ad6
LC
946
947 cpu = qobject_to_qdict(obj);
8f3cec0b 948
6a00d601 949#if defined(TARGET_I386)
8f3cec0b 950 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
e80e1cc4 951#elif defined(TARGET_PPC)
8f3cec0b 952 qdict_put(cpu, "nip", qint_from_int(env->nip));
ba3c64fb 953#elif defined(TARGET_SPARC)
8f3cec0b
LC
954 qdict_put(cpu, "pc", qint_from_int(env->pc));
955 qdict_put(cpu, "npc", qint_from_int(env->npc));
ead9360e 956#elif defined(TARGET_MIPS)
8f3cec0b 957 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
ce5232c5 958#endif
8f3cec0b
LC
959
960 qlist_append(cpu_list, cpu);
6a00d601 961 }
8f3cec0b
LC
962
963 *ret_data = QOBJECT(cpu_list);
6a00d601
FB
964}
965
584cbdb5 966static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
6a00d601 967{
d54908a5 968 int index = qdict_get_int(qdict, "index");
584cbdb5 969 if (mon_set_cpu(index) < 0) {
ab5b027e 970 qerror_report(QERR_INVALID_PARAMETER, "index");
584cbdb5
LC
971 return -1;
972 }
973 return 0;
6a00d601
FB
974}
975
376253ec 976static void do_info_jit(Monitor *mon)
e3db7226 977{
376253ec 978 dump_exec_info((FILE *)mon, monitor_fprintf);
e3db7226
FB
979}
980
376253ec 981static void do_info_history(Monitor *mon)
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
76a66253
JM
998#if defined(TARGET_PPC)
999/* XXX: not implemented in other targets */
376253ec 1000static void do_info_cpu_stats(Monitor *mon)
76a66253
JM
1001{
1002 CPUState *env;
1003
1004 env = mon_get_cpu();
376253ec 1005 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
76a66253
JM
1006}
1007#endif
1008
b223f35f
LC
1009/**
1010 * do_quit(): Quit QEMU execution
1011 */
ef4b7eee 1012static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
9dc39cba
FB
1013{
1014 exit(0);
ef4b7eee 1015 return 0;
9dc39cba
FB
1016}
1017
376253ec 1018static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
9dc39cba
FB
1019{
1020 if (bdrv_is_inserted(bs)) {
1021 if (!force) {
1022 if (!bdrv_is_removable(bs)) {
ab5b027e 1023 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
2c2a6bb8 1024 bdrv_get_device_name(bs));
9dc39cba
FB
1025 return -1;
1026 }
1027 if (bdrv_is_locked(bs)) {
ab5b027e 1028 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
9dc39cba
FB
1029 return -1;
1030 }
1031 }
1032 bdrv_close(bs);
1033 }
1034 return 0;
1035}
1036
9b9d4d9c 1037static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
9dc39cba
FB
1038{
1039 BlockDriverState *bs;
f18c16de 1040 int force = qdict_get_int(qdict, "force");
78d714e0 1041 const char *filename = qdict_get_str(qdict, "device");
9dc39cba 1042
9307c4c1 1043 bs = bdrv_find(filename);
9dc39cba 1044 if (!bs) {
ab5b027e 1045 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
9b9d4d9c 1046 return -1;
9dc39cba 1047 }
9b9d4d9c 1048 return eject_device(mon, bs, force);
9dc39cba
FB
1049}
1050
ba85d351 1051static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
a3a55a2e
LC
1052 QObject **ret_data)
1053{
1054 BlockDriverState *bs;
1055
1056 bs = bdrv_find(qdict_get_str(qdict, "device"));
1057 if (!bs) {
ab5b027e 1058 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
ba85d351 1059 return -1;
a3a55a2e
LC
1060 }
1061
1062 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
ab5b027e 1063 qerror_report(QERR_INVALID_PASSWORD);
ba85d351 1064 return -1;
a3a55a2e 1065 }
ba85d351
LC
1066
1067 return 0;
a3a55a2e
LC
1068}
1069
0bbc47bb
LC
1070static int do_change_block(Monitor *mon, const char *device,
1071 const char *filename, const char *fmt)
9dc39cba
FB
1072{
1073 BlockDriverState *bs;
2ecea9b8 1074 BlockDriver *drv = NULL;
9dc39cba 1075
9307c4c1 1076 bs = bdrv_find(device);
9dc39cba 1077 if (!bs) {
ab5b027e 1078 qerror_report(QERR_DEVICE_NOT_FOUND, device);
0bbc47bb 1079 return -1;
9dc39cba 1080 }
2ecea9b8 1081 if (fmt) {
eb852011 1082 drv = bdrv_find_whitelisted_format(fmt);
2ecea9b8 1083 if (!drv) {
ab5b027e 1084 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
0bbc47bb 1085 return -1;
2ecea9b8
AJ
1086 }
1087 }
0bbc47bb
LC
1088 if (eject_device(mon, bs, 0) < 0) {
1089 return -1;
1090 }
1091 if (bdrv_open2(bs, filename, BDRV_O_RDWR, drv) < 0) {
1092 return -1;
1093 }
1094 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
bb5fc20f
AL
1095}
1096
0bbc47bb 1097static int change_vnc_password(const char *password)
bb5fc20f 1098{
0bbc47bb 1099 if (vnc_display_password(NULL, password) < 0) {
ab5b027e 1100 qerror_report(QERR_SET_PASSWD_FAILED);
0bbc47bb
LC
1101 return -1;
1102 }
bb5fc20f 1103
0bbc47bb 1104 return 0;
2895e075
MA
1105}
1106
1107static void change_vnc_password_cb(Monitor *mon, const char *password,
1108 void *opaque)
1109{
ec3b82af 1110 change_vnc_password(password);
731b0364 1111 monitor_read_command(mon, 1);
9dc39cba
FB
1112}
1113
0bbc47bb 1114static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
e25a5822 1115{
70848515 1116 if (strcmp(target, "passwd") == 0 ||
28a76be8
AL
1117 strcmp(target, "password") == 0) {
1118 if (arg) {
bb5fc20f 1119 char password[9];
28a76be8
AL
1120 strncpy(password, arg, sizeof(password));
1121 password[sizeof(password) - 1] = '\0';
0bbc47bb 1122 return change_vnc_password(password);
bb5fc20f 1123 } else {
0bbc47bb 1124 return monitor_read_password(mon, change_vnc_password_cb, NULL);
bb5fc20f 1125 }
70848515 1126 } else {
0bbc47bb 1127 if (vnc_display_open(NULL, target) < 0) {
ab5b027e 1128 qerror_report(QERR_VNC_SERVER_FAILED, target);
0bbc47bb
LC
1129 return -1;
1130 }
70848515 1131 }
0bbc47bb
LC
1132
1133 return 0;
e25a5822
TS
1134}
1135
ec3b82af
MA
1136/**
1137 * do_change(): Change a removable medium, or VNC configuration
1138 */
0bbc47bb 1139static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
e25a5822 1140{
1d4daa91
LC
1141 const char *device = qdict_get_str(qdict, "device");
1142 const char *target = qdict_get_str(qdict, "target");
1143 const char *arg = qdict_get_try_str(qdict, "arg");
0bbc47bb
LC
1144 int ret;
1145
e25a5822 1146 if (strcmp(device, "vnc") == 0) {
0bbc47bb 1147 ret = do_change_vnc(mon, target, arg);
e25a5822 1148 } else {
0bbc47bb 1149 ret = do_change_block(mon, device, target, arg);
e25a5822 1150 }
0bbc47bb
LC
1151
1152 return ret;
e25a5822
TS
1153}
1154
d54908a5 1155static void do_screen_dump(Monitor *mon, const QDict *qdict)
59a983b9 1156{
d54908a5 1157 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
59a983b9
FB
1158}
1159
d54908a5 1160static void do_logfile(Monitor *mon, const QDict *qdict)
e735b91c 1161{
d54908a5 1162 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
e735b91c
PB
1163}
1164
d54908a5 1165static void do_log(Monitor *mon, const QDict *qdict)
f193c797
FB
1166{
1167 int mask;
d54908a5 1168 const char *items = qdict_get_str(qdict, "items");
3b46e624 1169
9307c4c1 1170 if (!strcmp(items, "none")) {
f193c797
FB
1171 mask = 0;
1172 } else {
9307c4c1 1173 mask = cpu_str_to_log_mask(items);
f193c797 1174 if (!mask) {
376253ec 1175 help_cmd(mon, "log");
f193c797
FB
1176 return;
1177 }
1178 }
1179 cpu_set_log(mask);
1180}
1181
d54908a5 1182static void do_singlestep(Monitor *mon, const QDict *qdict)
1b530a6d 1183{
d54908a5 1184 const char *option = qdict_get_try_str(qdict, "option");
1b530a6d
AJ
1185 if (!option || !strcmp(option, "on")) {
1186 singlestep = 1;
1187 } else if (!strcmp(option, "off")) {
1188 singlestep = 0;
1189 } else {
1190 monitor_printf(mon, "unexpected option %s\n", option);
1191 }
1192}
1193
e0c97bde
LC
1194/**
1195 * do_stop(): Stop VM execution
1196 */
ef4b7eee 1197static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
8a7ddc38
FB
1198{
1199 vm_stop(EXCP_INTERRUPT);
ef4b7eee 1200 return 0;
8a7ddc38
FB
1201}
1202
bb5fc20f 1203static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
c0f4ce77 1204
376253ec
AL
1205struct bdrv_iterate_context {
1206 Monitor *mon;
1207 int err;
1208};
1209
a1f896a0
LC
1210/**
1211 * do_cont(): Resume emulation.
1212 */
d5a7b38f 1213static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
8a7ddc38 1214{
376253ec 1215 struct bdrv_iterate_context context = { mon, 0 };
c0f4ce77 1216
376253ec 1217 bdrv_iterate(encrypted_bdrv_it, &context);
c0f4ce77 1218 /* only resume the vm if all keys are set and valid */
d5a7b38f 1219 if (!context.err) {
c0f4ce77 1220 vm_start();
d5a7b38f
LC
1221 return 0;
1222 } else {
1223 return -1;
1224 }
8a7ddc38
FB
1225}
1226
bb5fc20f
AL
1227static void bdrv_key_cb(void *opaque, int err)
1228{
376253ec
AL
1229 Monitor *mon = opaque;
1230
bb5fc20f
AL
1231 /* another key was set successfully, retry to continue */
1232 if (!err)
a1f896a0 1233 do_cont(mon, NULL, NULL);
bb5fc20f
AL
1234}
1235
1236static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1237{
376253ec 1238 struct bdrv_iterate_context *context = opaque;
bb5fc20f 1239
376253ec
AL
1240 if (!context->err && bdrv_key_required(bs)) {
1241 context->err = -EBUSY;
1242 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1243 context->mon);
bb5fc20f
AL
1244 }
1245}
1246
d54908a5 1247static void do_gdbserver(Monitor *mon, const QDict *qdict)
59030a8c 1248{
d54908a5 1249 const char *device = qdict_get_try_str(qdict, "device");
59030a8c
AL
1250 if (!device)
1251 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1252 if (gdbserver_start(device) < 0) {
1253 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1254 device);
1255 } else if (strcmp(device, "none") == 0) {
36556b20 1256 monitor_printf(mon, "Disabled gdbserver\n");
8a7ddc38 1257 } else {
59030a8c
AL
1258 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1259 device);
8a7ddc38
FB
1260 }
1261}
1262
d54908a5 1263static void do_watchdog_action(Monitor *mon, const QDict *qdict)
9dd986cc 1264{
d54908a5 1265 const char *action = qdict_get_str(qdict, "action");
9dd986cc
RJ
1266 if (select_watchdog_action(action) == -1) {
1267 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1268 }
1269}
1270
376253ec 1271static void monitor_printc(Monitor *mon, int c)
9307c4c1 1272{
376253ec 1273 monitor_printf(mon, "'");
9307c4c1
FB
1274 switch(c) {
1275 case '\'':
376253ec 1276 monitor_printf(mon, "\\'");
9307c4c1
FB
1277 break;
1278 case '\\':
376253ec 1279 monitor_printf(mon, "\\\\");
9307c4c1
FB
1280 break;
1281 case '\n':
376253ec 1282 monitor_printf(mon, "\\n");
9307c4c1
FB
1283 break;
1284 case '\r':
376253ec 1285 monitor_printf(mon, "\\r");
9307c4c1
FB
1286 break;
1287 default:
1288 if (c >= 32 && c <= 126) {
376253ec 1289 monitor_printf(mon, "%c", c);
9307c4c1 1290 } else {
376253ec 1291 monitor_printf(mon, "\\x%02x", c);
9307c4c1
FB
1292 }
1293 break;
1294 }
376253ec 1295 monitor_printf(mon, "'");
9307c4c1
FB
1296}
1297
376253ec 1298static void memory_dump(Monitor *mon, int count, int format, int wsize,
c227f099 1299 target_phys_addr_t addr, int is_physical)
9307c4c1 1300{
6a00d601 1301 CPUState *env;
23842aab 1302 int l, line_size, i, max_digits, len;
9307c4c1
FB
1303 uint8_t buf[16];
1304 uint64_t v;
1305
1306 if (format == 'i') {
1307 int flags;
1308 flags = 0;
6a00d601 1309 env = mon_get_cpu();
9307c4c1 1310#ifdef TARGET_I386
4c27ba27 1311 if (wsize == 2) {
9307c4c1 1312 flags = 1;
4c27ba27
FB
1313 } else if (wsize == 4) {
1314 flags = 0;
1315 } else {
6a15fd12 1316 /* as default we use the current CS size */
4c27ba27 1317 flags = 0;
6a15fd12
FB
1318 if (env) {
1319#ifdef TARGET_X86_64
5fafdf24 1320 if ((env->efer & MSR_EFER_LMA) &&
6a15fd12
FB
1321 (env->segs[R_CS].flags & DESC_L_MASK))
1322 flags = 2;
1323 else
1324#endif
1325 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1326 flags = 1;
1327 }
4c27ba27
FB
1328 }
1329#endif
376253ec 1330 monitor_disas(mon, env, addr, count, is_physical, flags);
9307c4c1
FB
1331 return;
1332 }
1333
1334 len = wsize * count;
1335 if (wsize == 1)
1336 line_size = 8;
1337 else
1338 line_size = 16;
9307c4c1
FB
1339 max_digits = 0;
1340
1341 switch(format) {
1342 case 'o':
1343 max_digits = (wsize * 8 + 2) / 3;
1344 break;
1345 default:
1346 case 'x':
1347 max_digits = (wsize * 8) / 4;
1348 break;
1349 case 'u':
1350 case 'd':
1351 max_digits = (wsize * 8 * 10 + 32) / 33;
1352 break;
1353 case 'c':
1354 wsize = 1;
1355 break;
1356 }
1357
1358 while (len > 0) {
7743e588 1359 if (is_physical)
376253ec 1360 monitor_printf(mon, TARGET_FMT_plx ":", addr);
7743e588 1361 else
376253ec 1362 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
9307c4c1
FB
1363 l = len;
1364 if (l > line_size)
1365 l = line_size;
1366 if (is_physical) {
1367 cpu_physical_memory_rw(addr, buf, l, 0);
1368 } else {
6a00d601 1369 env = mon_get_cpu();
c8f79b67 1370 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
376253ec 1371 monitor_printf(mon, " Cannot access memory\n");
c8f79b67
AL
1372 break;
1373 }
9307c4c1 1374 }
5fafdf24 1375 i = 0;
9307c4c1
FB
1376 while (i < l) {
1377 switch(wsize) {
1378 default:
1379 case 1:
1380 v = ldub_raw(buf + i);
1381 break;
1382 case 2:
1383 v = lduw_raw(buf + i);
1384 break;
1385 case 4:
92a31b1f 1386 v = (uint32_t)ldl_raw(buf + i);
9307c4c1
FB
1387 break;
1388 case 8:
1389 v = ldq_raw(buf + i);
1390 break;
1391 }
376253ec 1392 monitor_printf(mon, " ");
9307c4c1
FB
1393 switch(format) {
1394 case 'o':
376253ec 1395 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
9307c4c1
FB
1396 break;
1397 case 'x':
376253ec 1398 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
9307c4c1
FB
1399 break;
1400 case 'u':
376253ec 1401 monitor_printf(mon, "%*" PRIu64, max_digits, v);
9307c4c1
FB
1402 break;
1403 case 'd':
376253ec 1404 monitor_printf(mon, "%*" PRId64, max_digits, v);
9307c4c1
FB
1405 break;
1406 case 'c':
376253ec 1407 monitor_printc(mon, v);
9307c4c1
FB
1408 break;
1409 }
1410 i += wsize;
1411 }
376253ec 1412 monitor_printf(mon, "\n");
9307c4c1
FB
1413 addr += l;
1414 len -= l;
1415 }
1416}
1417
1bd1442e 1418static void do_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 1419{
1bd1442e
LC
1420 int count = qdict_get_int(qdict, "count");
1421 int format = qdict_get_int(qdict, "format");
1422 int size = qdict_get_int(qdict, "size");
1423 target_long addr = qdict_get_int(qdict, "addr");
1424
376253ec 1425 memory_dump(mon, count, format, size, addr, 0);
9307c4c1
FB
1426}
1427
1bd1442e 1428static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
9307c4c1 1429{
1bd1442e
LC
1430 int count = qdict_get_int(qdict, "count");
1431 int format = qdict_get_int(qdict, "format");
1432 int size = qdict_get_int(qdict, "size");
c227f099 1433 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
1bd1442e 1434
376253ec 1435 memory_dump(mon, count, format, size, addr, 1);
9307c4c1
FB
1436}
1437
1bd1442e 1438static void do_print(Monitor *mon, const QDict *qdict)
9307c4c1 1439{
1bd1442e 1440 int format = qdict_get_int(qdict, "format");
c227f099 1441 target_phys_addr_t val = qdict_get_int(qdict, "val");
1bd1442e 1442
7743e588 1443#if TARGET_PHYS_ADDR_BITS == 32
9307c4c1
FB
1444 switch(format) {
1445 case 'o':
376253ec 1446 monitor_printf(mon, "%#o", val);
9307c4c1
FB
1447 break;
1448 case 'x':
376253ec 1449 monitor_printf(mon, "%#x", val);
9307c4c1
FB
1450 break;
1451 case 'u':
376253ec 1452 monitor_printf(mon, "%u", val);
9307c4c1
FB
1453 break;
1454 default:
1455 case 'd':
376253ec 1456 monitor_printf(mon, "%d", val);
9307c4c1
FB
1457 break;
1458 case 'c':
376253ec 1459 monitor_printc(mon, val);
9307c4c1
FB
1460 break;
1461 }
92a31b1f
FB
1462#else
1463 switch(format) {
1464 case 'o':
376253ec 1465 monitor_printf(mon, "%#" PRIo64, val);
92a31b1f
FB
1466 break;
1467 case 'x':
376253ec 1468 monitor_printf(mon, "%#" PRIx64, val);
92a31b1f
FB
1469 break;
1470 case 'u':
376253ec 1471 monitor_printf(mon, "%" PRIu64, val);
92a31b1f
FB
1472 break;
1473 default:
1474 case 'd':
376253ec 1475 monitor_printf(mon, "%" PRId64, val);
92a31b1f
FB
1476 break;
1477 case 'c':
376253ec 1478 monitor_printc(mon, val);
92a31b1f
FB
1479 break;
1480 }
1481#endif
376253ec 1482 monitor_printf(mon, "\n");
9307c4c1
FB
1483}
1484
9869622e 1485static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
b371dc59
FB
1486{
1487 FILE *f;
afe67ef2
LC
1488 uint32_t size = qdict_get_int(qdict, "size");
1489 const char *filename = qdict_get_str(qdict, "filename");
1490 target_long addr = qdict_get_int(qdict, "val");
b371dc59
FB
1491 uint32_t l;
1492 CPUState *env;
1493 uint8_t buf[1024];
9869622e 1494 int ret = -1;
b371dc59
FB
1495
1496 env = mon_get_cpu();
b371dc59
FB
1497
1498 f = fopen(filename, "wb");
1499 if (!f) {
ab5b027e 1500 qerror_report(QERR_OPEN_FILE_FAILED, filename);
9869622e 1501 return -1;
b371dc59
FB
1502 }
1503 while (size != 0) {
1504 l = sizeof(buf);
1505 if (l > size)
1506 l = size;
1507 cpu_memory_rw_debug(env, addr, buf, l, 0);
27e3ddd3
KS
1508 if (fwrite(buf, 1, l, f) != l) {
1509 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1510 goto exit;
1511 }
b371dc59
FB
1512 addr += l;
1513 size -= l;
1514 }
9869622e
LC
1515
1516 ret = 0;
1517
27e3ddd3 1518exit:
b371dc59 1519 fclose(f);
9869622e 1520 return ret;
b371dc59
FB
1521}
1522
fe38a32a 1523static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
18f5a8bf 1524 QObject **ret_data)
a8bdf7a6
AJ
1525{
1526 FILE *f;
1527 uint32_t l;
1528 uint8_t buf[1024];
afe67ef2
LC
1529 uint32_t size = qdict_get_int(qdict, "size");
1530 const char *filename = qdict_get_str(qdict, "filename");
c227f099 1531 target_phys_addr_t addr = qdict_get_int(qdict, "val");
fe38a32a 1532 int ret = -1;
a8bdf7a6
AJ
1533
1534 f = fopen(filename, "wb");
1535 if (!f) {
ab5b027e 1536 qerror_report(QERR_OPEN_FILE_FAILED, filename);
fe38a32a 1537 return -1;
a8bdf7a6
AJ
1538 }
1539 while (size != 0) {
1540 l = sizeof(buf);
1541 if (l > size)
1542 l = size;
1543 cpu_physical_memory_rw(addr, buf, l, 0);
27e3ddd3
KS
1544 if (fwrite(buf, 1, l, f) != l) {
1545 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1546 goto exit;
1547 }
a8bdf7a6
AJ
1548 fflush(f);
1549 addr += l;
1550 size -= l;
1551 }
fe38a32a
LC
1552
1553 ret = 0;
1554
27e3ddd3 1555exit:
a8bdf7a6 1556 fclose(f);
fe38a32a 1557 return ret;
a8bdf7a6
AJ
1558}
1559
f18c16de 1560static void do_sum(Monitor *mon, const QDict *qdict)
e4cf1adc
FB
1561{
1562 uint32_t addr;
1563 uint8_t buf[1];
1564 uint16_t sum;
f18c16de
LC
1565 uint32_t start = qdict_get_int(qdict, "start");
1566 uint32_t size = qdict_get_int(qdict, "size");
e4cf1adc
FB
1567
1568 sum = 0;
1569 for(addr = start; addr < (start + size); addr++) {
1570 cpu_physical_memory_rw(addr, buf, 1, 0);
1571 /* BSD sum algorithm ('sum' Unix command) */
1572 sum = (sum >> 1) | (sum << 15);
1573 sum += buf[0];
1574 }
376253ec 1575 monitor_printf(mon, "%05d\n", sum);
e4cf1adc
FB
1576}
1577
a3a91a35
FB
1578typedef struct {
1579 int keycode;
1580 const char *name;
1581} KeyDef;
1582
1583static const KeyDef key_defs[] = {
1584 { 0x2a, "shift" },
1585 { 0x36, "shift_r" },
3b46e624 1586
a3a91a35
FB
1587 { 0x38, "alt" },
1588 { 0xb8, "alt_r" },
2ba27c7f
TS
1589 { 0x64, "altgr" },
1590 { 0xe4, "altgr_r" },
a3a91a35
FB
1591 { 0x1d, "ctrl" },
1592 { 0x9d, "ctrl_r" },
1593
1594 { 0xdd, "menu" },
1595
1596 { 0x01, "esc" },
1597
1598 { 0x02, "1" },
1599 { 0x03, "2" },
1600 { 0x04, "3" },
1601 { 0x05, "4" },
1602 { 0x06, "5" },
1603 { 0x07, "6" },
1604 { 0x08, "7" },
1605 { 0x09, "8" },
1606 { 0x0a, "9" },
1607 { 0x0b, "0" },
64866c3d
FB
1608 { 0x0c, "minus" },
1609 { 0x0d, "equal" },
a3a91a35
FB
1610 { 0x0e, "backspace" },
1611
1612 { 0x0f, "tab" },
1613 { 0x10, "q" },
1614 { 0x11, "w" },
1615 { 0x12, "e" },
1616 { 0x13, "r" },
1617 { 0x14, "t" },
1618 { 0x15, "y" },
1619 { 0x16, "u" },
1620 { 0x17, "i" },
1621 { 0x18, "o" },
1622 { 0x19, "p" },
1623
1624 { 0x1c, "ret" },
1625
1626 { 0x1e, "a" },
1627 { 0x1f, "s" },
1628 { 0x20, "d" },
1629 { 0x21, "f" },
1630 { 0x22, "g" },
1631 { 0x23, "h" },
1632 { 0x24, "j" },
1633 { 0x25, "k" },
1634 { 0x26, "l" },
1635
1636 { 0x2c, "z" },
1637 { 0x2d, "x" },
1638 { 0x2e, "c" },
1639 { 0x2f, "v" },
1640 { 0x30, "b" },
1641 { 0x31, "n" },
1642 { 0x32, "m" },
9155fc45
AJ
1643 { 0x33, "comma" },
1644 { 0x34, "dot" },
1645 { 0x35, "slash" },
3b46e624 1646
4d3b6f6e
AZ
1647 { 0x37, "asterisk" },
1648
a3a91a35 1649 { 0x39, "spc" },
00ffa62a 1650 { 0x3a, "caps_lock" },
a3a91a35
FB
1651 { 0x3b, "f1" },
1652 { 0x3c, "f2" },
1653 { 0x3d, "f3" },
1654 { 0x3e, "f4" },
1655 { 0x3f, "f5" },
1656 { 0x40, "f6" },
1657 { 0x41, "f7" },
1658 { 0x42, "f8" },
1659 { 0x43, "f9" },
1660 { 0x44, "f10" },
00ffa62a 1661 { 0x45, "num_lock" },
a3a91a35
FB
1662 { 0x46, "scroll_lock" },
1663
64866c3d
FB
1664 { 0xb5, "kp_divide" },
1665 { 0x37, "kp_multiply" },
0cfec834 1666 { 0x4a, "kp_subtract" },
64866c3d
FB
1667 { 0x4e, "kp_add" },
1668 { 0x9c, "kp_enter" },
1669 { 0x53, "kp_decimal" },
f2289cb6 1670 { 0x54, "sysrq" },
64866c3d
FB
1671
1672 { 0x52, "kp_0" },
1673 { 0x4f, "kp_1" },
1674 { 0x50, "kp_2" },
1675 { 0x51, "kp_3" },
1676 { 0x4b, "kp_4" },
1677 { 0x4c, "kp_5" },
1678 { 0x4d, "kp_6" },
1679 { 0x47, "kp_7" },
1680 { 0x48, "kp_8" },
1681 { 0x49, "kp_9" },
3b46e624 1682
a3a91a35
FB
1683 { 0x56, "<" },
1684
1685 { 0x57, "f11" },
1686 { 0x58, "f12" },
1687
1688 { 0xb7, "print" },
1689
1690 { 0xc7, "home" },
1691 { 0xc9, "pgup" },
1692 { 0xd1, "pgdn" },
1693 { 0xcf, "end" },
1694
1695 { 0xcb, "left" },
1696 { 0xc8, "up" },
1697 { 0xd0, "down" },
1698 { 0xcd, "right" },
1699
1700 { 0xd2, "insert" },
1701 { 0xd3, "delete" },
c0b5b109
BS
1702#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1703 { 0xf0, "stop" },
1704 { 0xf1, "again" },
1705 { 0xf2, "props" },
1706 { 0xf3, "undo" },
1707 { 0xf4, "front" },
1708 { 0xf5, "copy" },
1709 { 0xf6, "open" },
1710 { 0xf7, "paste" },
1711 { 0xf8, "find" },
1712 { 0xf9, "cut" },
1713 { 0xfa, "lf" },
1714 { 0xfb, "help" },
1715 { 0xfc, "meta_l" },
1716 { 0xfd, "meta_r" },
1717 { 0xfe, "compose" },
1718#endif
a3a91a35
FB
1719 { 0, NULL },
1720};
1721
1722static int get_keycode(const char *key)
1723{
1724 const KeyDef *p;
64866c3d
FB
1725 char *endp;
1726 int ret;
a3a91a35
FB
1727
1728 for(p = key_defs; p->name != NULL; p++) {
1729 if (!strcmp(key, p->name))
1730 return p->keycode;
1731 }
64866c3d
FB
1732 if (strstart(key, "0x", NULL)) {
1733 ret = strtoul(key, &endp, 0);
1734 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1735 return ret;
1736 }
a3a91a35
FB
1737 return -1;
1738}
1739
c8256f9d
AZ
1740#define MAX_KEYCODES 16
1741static uint8_t keycodes[MAX_KEYCODES];
1742static int nb_pending_keycodes;
1743static QEMUTimer *key_timer;
1744
1745static void release_keys(void *opaque)
1746{
1747 int keycode;
1748
1749 while (nb_pending_keycodes > 0) {
1750 nb_pending_keycodes--;
1751 keycode = keycodes[nb_pending_keycodes];
1752 if (keycode & 0x80)
1753 kbd_put_keycode(0xe0);
1754 kbd_put_keycode(keycode | 0x80);
1755 }
1756}
1757
1d4daa91 1758static void do_sendkey(Monitor *mon, const QDict *qdict)
a3a91a35 1759{
3401c0d9
AZ
1760 char keyname_buf[16];
1761 char *separator;
1762 int keyname_len, keycode, i;
1d4daa91
LC
1763 const char *string = qdict_get_str(qdict, "string");
1764 int has_hold_time = qdict_haskey(qdict, "hold_time");
1765 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
3401c0d9 1766
c8256f9d
AZ
1767 if (nb_pending_keycodes > 0) {
1768 qemu_del_timer(key_timer);
1769 release_keys(NULL);
1770 }
1771 if (!has_hold_time)
1772 hold_time = 100;
1773 i = 0;
3401c0d9
AZ
1774 while (1) {
1775 separator = strchr(string, '-');
1776 keyname_len = separator ? separator - string : strlen(string);
1777 if (keyname_len > 0) {
1778 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1779 if (keyname_len > sizeof(keyname_buf) - 1) {
376253ec 1780 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
3401c0d9 1781 return;
a3a91a35 1782 }
c8256f9d 1783 if (i == MAX_KEYCODES) {
376253ec 1784 monitor_printf(mon, "too many keys\n");
3401c0d9
AZ
1785 return;
1786 }
1787 keyname_buf[keyname_len] = 0;
1788 keycode = get_keycode(keyname_buf);
1789 if (keycode < 0) {
376253ec 1790 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
3401c0d9
AZ
1791 return;
1792 }
c8256f9d 1793 keycodes[i++] = keycode;
a3a91a35 1794 }
3401c0d9 1795 if (!separator)
a3a91a35 1796 break;
3401c0d9 1797 string = separator + 1;
a3a91a35 1798 }
c8256f9d 1799 nb_pending_keycodes = i;
a3a91a35 1800 /* key down events */
c8256f9d 1801 for (i = 0; i < nb_pending_keycodes; i++) {
a3a91a35
FB
1802 keycode = keycodes[i];
1803 if (keycode & 0x80)
1804 kbd_put_keycode(0xe0);
1805 kbd_put_keycode(keycode & 0x7f);
1806 }
c8256f9d 1807 /* delayed key up events */
f227f17d 1808 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
6ee093c9 1809 muldiv64(get_ticks_per_sec(), hold_time, 1000));
a3a91a35
FB
1810}
1811
13224a87
FB
1812static int mouse_button_state;
1813
1d4daa91 1814static void do_mouse_move(Monitor *mon, const QDict *qdict)
13224a87
FB
1815{
1816 int dx, dy, dz;
1d4daa91
LC
1817 const char *dx_str = qdict_get_str(qdict, "dx_str");
1818 const char *dy_str = qdict_get_str(qdict, "dy_str");
1819 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
13224a87
FB
1820 dx = strtol(dx_str, NULL, 0);
1821 dy = strtol(dy_str, NULL, 0);
1822 dz = 0;
5fafdf24 1823 if (dz_str)
13224a87
FB
1824 dz = strtol(dz_str, NULL, 0);
1825 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1826}
1827
d54908a5 1828static void do_mouse_button(Monitor *mon, const QDict *qdict)
13224a87 1829{
d54908a5 1830 int button_state = qdict_get_int(qdict, "button_state");
13224a87
FB
1831 mouse_button_state = button_state;
1832 kbd_mouse_event(0, 0, 0, mouse_button_state);
1833}
1834
aa93e39c 1835static void do_ioport_read(Monitor *mon, const QDict *qdict)
3440557b 1836{
aa93e39c
LC
1837 int size = qdict_get_int(qdict, "size");
1838 int addr = qdict_get_int(qdict, "addr");
1839 int has_index = qdict_haskey(qdict, "index");
3440557b
FB
1840 uint32_t val;
1841 int suffix;
1842
1843 if (has_index) {
aa93e39c 1844 int index = qdict_get_int(qdict, "index");
afcea8cb 1845 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
3440557b
FB
1846 addr++;
1847 }
1848 addr &= 0xffff;
1849
1850 switch(size) {
1851 default:
1852 case 1:
afcea8cb 1853 val = cpu_inb(addr);
3440557b
FB
1854 suffix = 'b';
1855 break;
1856 case 2:
afcea8cb 1857 val = cpu_inw(addr);
3440557b
FB
1858 suffix = 'w';
1859 break;
1860 case 4:
afcea8cb 1861 val = cpu_inl(addr);
3440557b
FB
1862 suffix = 'l';
1863 break;
1864 }
376253ec
AL
1865 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1866 suffix, addr, size * 2, val);
3440557b 1867}
a3a91a35 1868
1bd1442e 1869static void do_ioport_write(Monitor *mon, const QDict *qdict)
f114784f 1870{
1bd1442e
LC
1871 int size = qdict_get_int(qdict, "size");
1872 int addr = qdict_get_int(qdict, "addr");
1873 int val = qdict_get_int(qdict, "val");
1874
f114784f
JK
1875 addr &= IOPORTS_MASK;
1876
1877 switch (size) {
1878 default:
1879 case 1:
afcea8cb 1880 cpu_outb(addr, val);
f114784f
JK
1881 break;
1882 case 2:
afcea8cb 1883 cpu_outw(addr, val);
f114784f
JK
1884 break;
1885 case 4:
afcea8cb 1886 cpu_outl(addr, val);
f114784f
JK
1887 break;
1888 }
1889}
1890
d54908a5 1891static void do_boot_set(Monitor *mon, const QDict *qdict)
0ecdffbb
AJ
1892{
1893 int res;
d54908a5 1894 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
0ecdffbb 1895
76e30d0f
JK
1896 res = qemu_boot_set(bootdevice);
1897 if (res == 0) {
1898 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1899 } else if (res > 0) {
1900 monitor_printf(mon, "setting boot device list failed\n");
0ecdffbb 1901 } else {
376253ec
AL
1902 monitor_printf(mon, "no function defined to set boot device list for "
1903 "this architecture\n");
0ecdffbb
AJ
1904 }
1905}
1906
c80d259e
LC
1907/**
1908 * do_system_reset(): Issue a machine reset
1909 */
ef4b7eee
LC
1910static int do_system_reset(Monitor *mon, const QDict *qdict,
1911 QObject **ret_data)
e4f9082b
FB
1912{
1913 qemu_system_reset_request();
ef4b7eee 1914 return 0;
e4f9082b
FB
1915}
1916
43076664
LC
1917/**
1918 * do_system_powerdown(): Issue a machine powerdown
1919 */
ef4b7eee
LC
1920static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1921 QObject **ret_data)
3475187d
FB
1922{
1923 qemu_system_powerdown_request();
ef4b7eee 1924 return 0;
3475187d
FB
1925}
1926
b86bda5b 1927#if defined(TARGET_I386)
376253ec 1928static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
b86bda5b 1929{
376253ec
AL
1930 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1931 addr,
1932 pte & mask,
1933 pte & PG_GLOBAL_MASK ? 'G' : '-',
1934 pte & PG_PSE_MASK ? 'P' : '-',
1935 pte & PG_DIRTY_MASK ? 'D' : '-',
1936 pte & PG_ACCESSED_MASK ? 'A' : '-',
1937 pte & PG_PCD_MASK ? 'C' : '-',
1938 pte & PG_PWT_MASK ? 'T' : '-',
1939 pte & PG_USER_MASK ? 'U' : '-',
1940 pte & PG_RW_MASK ? 'W' : '-');
b86bda5b
FB
1941}
1942
376253ec 1943static void tlb_info(Monitor *mon)
b86bda5b 1944{
6a00d601 1945 CPUState *env;
b86bda5b
FB
1946 int l1, l2;
1947 uint32_t pgd, pde, pte;
1948
6a00d601 1949 env = mon_get_cpu();
6a00d601 1950
b86bda5b 1951 if (!(env->cr[0] & CR0_PG_MASK)) {
376253ec 1952 monitor_printf(mon, "PG disabled\n");
b86bda5b
FB
1953 return;
1954 }
1955 pgd = env->cr[3] & ~0xfff;
1956 for(l1 = 0; l1 < 1024; l1++) {
1957 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1958 pde = le32_to_cpu(pde);
1959 if (pde & PG_PRESENT_MASK) {
1960 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
376253ec 1961 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
b86bda5b
FB
1962 } else {
1963 for(l2 = 0; l2 < 1024; l2++) {
5fafdf24 1964 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
b86bda5b
FB
1965 (uint8_t *)&pte, 4);
1966 pte = le32_to_cpu(pte);
1967 if (pte & PG_PRESENT_MASK) {
376253ec 1968 print_pte(mon, (l1 << 22) + (l2 << 12),
5fafdf24 1969 pte & ~PG_PSE_MASK,
b86bda5b
FB
1970 ~0xfff);
1971 }
1972 }
1973 }
1974 }
1975 }
1976}
1977
376253ec 1978static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
b86bda5b
FB
1979 uint32_t end, int prot)
1980{
9746b15b
FB
1981 int prot1;
1982 prot1 = *plast_prot;
1983 if (prot != prot1) {
b86bda5b 1984 if (*pstart != -1) {
376253ec
AL
1985 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1986 *pstart, end, end - *pstart,
1987 prot1 & PG_USER_MASK ? 'u' : '-',
1988 'r',
1989 prot1 & PG_RW_MASK ? 'w' : '-');
b86bda5b
FB
1990 }
1991 if (prot != 0)
1992 *pstart = end;
1993 else
1994 *pstart = -1;
1995 *plast_prot = prot;
1996 }
1997}
1998
376253ec 1999static void mem_info(Monitor *mon)
b86bda5b 2000{
6a00d601 2001 CPUState *env;
b86bda5b
FB
2002 int l1, l2, prot, last_prot;
2003 uint32_t pgd, pde, pte, start, end;
2004
6a00d601 2005 env = mon_get_cpu();
6a00d601 2006
b86bda5b 2007 if (!(env->cr[0] & CR0_PG_MASK)) {
376253ec 2008 monitor_printf(mon, "PG disabled\n");
b86bda5b
FB
2009 return;
2010 }
2011 pgd = env->cr[3] & ~0xfff;
2012 last_prot = 0;
2013 start = -1;
2014 for(l1 = 0; l1 < 1024; l1++) {
2015 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
2016 pde = le32_to_cpu(pde);
2017 end = l1 << 22;
2018 if (pde & PG_PRESENT_MASK) {
2019 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2020 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
376253ec 2021 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
2022 } else {
2023 for(l2 = 0; l2 < 1024; l2++) {
5fafdf24 2024 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
b86bda5b
FB
2025 (uint8_t *)&pte, 4);
2026 pte = le32_to_cpu(pte);
2027 end = (l1 << 22) + (l2 << 12);
2028 if (pte & PG_PRESENT_MASK) {
2029 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2030 } else {
2031 prot = 0;
2032 }
376253ec 2033 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
2034 }
2035 }
2036 } else {
2037 prot = 0;
376253ec 2038 mem_print(mon, &start, &last_prot, end, prot);
b86bda5b
FB
2039 }
2040 }
2041}
2042#endif
2043
7c664e2f
AJ
2044#if defined(TARGET_SH4)
2045
376253ec 2046static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
7c664e2f 2047{
376253ec
AL
2048 monitor_printf(mon, " tlb%i:\t"
2049 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2050 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2051 "dirty=%hhu writethrough=%hhu\n",
2052 idx,
2053 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2054 tlb->v, tlb->sh, tlb->c, tlb->pr,
2055 tlb->d, tlb->wt);
7c664e2f
AJ
2056}
2057
376253ec 2058static void tlb_info(Monitor *mon)
7c664e2f
AJ
2059{
2060 CPUState *env = mon_get_cpu();
2061 int i;
2062
376253ec 2063 monitor_printf (mon, "ITLB:\n");
7c664e2f 2064 for (i = 0 ; i < ITLB_SIZE ; i++)
376253ec
AL
2065 print_tlb (mon, i, &env->itlb[i]);
2066 monitor_printf (mon, "UTLB:\n");
7c664e2f 2067 for (i = 0 ; i < UTLB_SIZE ; i++)
376253ec 2068 print_tlb (mon, i, &env->utlb[i]);
7c664e2f
AJ
2069}
2070
2071#endif
2072
2af5ba71 2073static void do_info_kvm_print(Monitor *mon, const QObject *data)
7ba1e619 2074{
2af5ba71
LC
2075 QDict *qdict;
2076
2077 qdict = qobject_to_qdict(data);
2078
376253ec 2079 monitor_printf(mon, "kvm support: ");
2af5ba71
LC
2080 if (qdict_get_bool(qdict, "present")) {
2081 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2082 "enabled" : "disabled");
2083 } else {
2084 monitor_printf(mon, "not compiled\n");
2085 }
2086}
2087
2088/**
2089 * do_info_kvm(): Show KVM information
2090 *
2091 * Return a QDict with the following information:
2092 *
2093 * - "enabled": true if KVM support is enabled, false otherwise
2094 * - "present": true if QEMU has KVM support, false otherwise
2095 *
2096 * Example:
2097 *
2098 * { "enabled": true, "present": true }
2099 */
2100static void do_info_kvm(Monitor *mon, QObject **ret_data)
2101{
2102#ifdef CONFIG_KVM
2103 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2104 kvm_enabled());
7ba1e619 2105#else
2af5ba71 2106 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
7ba1e619
AL
2107#endif
2108}
2109
030ea37b
AL
2110static void do_info_numa(Monitor *mon)
2111{
b28b6230 2112 int i;
030ea37b
AL
2113 CPUState *env;
2114
2115 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2116 for (i = 0; i < nb_numa_nodes; i++) {
2117 monitor_printf(mon, "node %d cpus:", i);
2118 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2119 if (env->numa_node == i) {
2120 monitor_printf(mon, " %d", env->cpu_index);
2121 }
2122 }
2123 monitor_printf(mon, "\n");
2124 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2125 node_mem[i] >> 20);
2126 }
2127}
2128
5f1ce948
FB
2129#ifdef CONFIG_PROFILER
2130
e9a6625e
AJ
2131int64_t qemu_time;
2132int64_t dev_time;
2133
376253ec 2134static void do_info_profile(Monitor *mon)
5f1ce948
FB
2135{
2136 int64_t total;
2137 total = qemu_time;
2138 if (total == 0)
2139 total = 1;
376253ec 2140 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
6ee093c9 2141 dev_time, dev_time / (double)get_ticks_per_sec());
376253ec 2142 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
6ee093c9 2143 qemu_time, qemu_time / (double)get_ticks_per_sec());
5f1ce948 2144 qemu_time = 0;
5f1ce948 2145 dev_time = 0;
5f1ce948
FB
2146}
2147#else
376253ec 2148static void do_info_profile(Monitor *mon)
5f1ce948 2149{
376253ec 2150 monitor_printf(mon, "Internal profiler not compiled\n");
5f1ce948
FB
2151}
2152#endif
2153
ec36b695 2154/* Capture support */
72cf2d4f 2155static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
ec36b695 2156
376253ec 2157static void do_info_capture(Monitor *mon)
ec36b695
FB
2158{
2159 int i;
2160 CaptureState *s;
2161
2162 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
376253ec 2163 monitor_printf(mon, "[%d]: ", i);
ec36b695
FB
2164 s->ops.info (s->opaque);
2165 }
2166}
2167
2313086a 2168#ifdef HAS_AUDIO
d54908a5 2169static void do_stop_capture(Monitor *mon, const QDict *qdict)
ec36b695
FB
2170{
2171 int i;
d54908a5 2172 int n = qdict_get_int(qdict, "n");
ec36b695
FB
2173 CaptureState *s;
2174
2175 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2176 if (i == n) {
2177 s->ops.destroy (s->opaque);
72cf2d4f 2178 QLIST_REMOVE (s, entries);
ec36b695
FB
2179 qemu_free (s);
2180 return;
2181 }
2182 }
2183}
2184
c1925484
LC
2185static void do_wav_capture(Monitor *mon, const QDict *qdict)
2186{
2187 const char *path = qdict_get_str(qdict, "path");
2188 int has_freq = qdict_haskey(qdict, "freq");
2189 int freq = qdict_get_try_int(qdict, "freq", -1);
2190 int has_bits = qdict_haskey(qdict, "bits");
2191 int bits = qdict_get_try_int(qdict, "bits", -1);
2192 int has_channels = qdict_haskey(qdict, "nchannels");
2193 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
ec36b695
FB
2194 CaptureState *s;
2195
2196 s = qemu_mallocz (sizeof (*s));
ec36b695
FB
2197
2198 freq = has_freq ? freq : 44100;
2199 bits = has_bits ? bits : 16;
2200 nchannels = has_channels ? nchannels : 2;
2201
2202 if (wav_start_capture (s, path, freq, bits, nchannels)) {
376253ec 2203 monitor_printf(mon, "Faied to add wave capture\n");
ec36b695
FB
2204 qemu_free (s);
2205 }
72cf2d4f 2206 QLIST_INSERT_HEAD (&capture_head, s, entries);
ec36b695
FB
2207}
2208#endif
2209
dc1c0b74 2210#if defined(TARGET_I386)
d54908a5 2211static void do_inject_nmi(Monitor *mon, const QDict *qdict)
dc1c0b74
AJ
2212{
2213 CPUState *env;
d54908a5 2214 int cpu_index = qdict_get_int(qdict, "cpu_index");
dc1c0b74
AJ
2215
2216 for (env = first_cpu; env != NULL; env = env->next_cpu)
2217 if (env->cpu_index == cpu_index) {
2218 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2219 break;
2220 }
2221}
2222#endif
2223
c0e8520e 2224static void do_info_status_print(Monitor *mon, const QObject *data)
6f9c5ee7 2225{
c0e8520e
LC
2226 QDict *qdict;
2227
2228 qdict = qobject_to_qdict(data);
2229
2230 monitor_printf(mon, "VM status: ");
2231 if (qdict_get_bool(qdict, "running")) {
2232 monitor_printf(mon, "running");
2233 if (qdict_get_bool(qdict, "singlestep")) {
2234 monitor_printf(mon, " (single step mode)");
1b530a6d 2235 }
c0e8520e
LC
2236 } else {
2237 monitor_printf(mon, "paused");
2238 }
2239
2240 monitor_printf(mon, "\n");
2241}
2242
2243/**
2244 * do_info_status(): VM status
2245 *
2246 * Return a QDict with the following information:
2247 *
2248 * - "running": true if the VM is running, or false if it is paused
2249 * - "singlestep": true if the VM is in single step mode, false otherwise
2250 *
2251 * Example:
2252 *
2253 * { "running": true, "singlestep": false }
2254 */
2255static void do_info_status(Monitor *mon, QObject **ret_data)
2256{
2257 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2258 vm_running, singlestep);
6f9c5ee7
AJ
2259}
2260
625a5bef 2261static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
cfdf2c40 2262{
625a5bef 2263 Monitor *mon = opaque;
cfdf2c40 2264
625a5bef
AL
2265 if (strcmp(key, "actual"))
2266 monitor_printf(mon, ",%s=%" PRId64, key,
2267 qint_get_int(qobject_to_qint(obj)));
df751fa8
AL
2268}
2269
cc1d9c70
LC
2270static void monitor_print_balloon(Monitor *mon, const QObject *data)
2271{
7f179671
LC
2272 QDict *qdict;
2273
2274 qdict = qobject_to_qdict(data);
625a5bef
AL
2275 if (!qdict_haskey(qdict, "actual"))
2276 return;
7f179671 2277
625a5bef
AL
2278 monitor_printf(mon, "balloon: actual=%" PRId64,
2279 qdict_get_int(qdict, "actual") >> 20);
2280 qdict_iter(qdict, print_balloon_stat, mon);
2281 monitor_printf(mon, "\n");
cc1d9c70
LC
2282}
2283
2284/**
2285 * do_info_balloon(): Balloon information
7f179671 2286 *
625a5bef
AL
2287 * Make an asynchronous request for balloon info. When the request completes
2288 * a QDict will be returned according to the following specification:
7f179671 2289 *
625a5bef
AL
2290 * - "actual": current balloon value in bytes
2291 * The following fields may or may not be present:
2292 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2293 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2294 * - "major_page_faults": Number of major faults
2295 * - "minor_page_faults": Number of minor faults
2296 * - "free_mem": Total amount of free and unused memory (bytes)
2297 * - "total_mem": Total amount of available memory (bytes)
7f179671
LC
2298 *
2299 * Example:
2300 *
625a5bef
AL
2301 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2302 * "major_page_faults": 142, "minor_page_faults": 239245,
2303 * "free_mem": 1014185984, "total_mem": 1044668416 }
2304 */
2305static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
2306{
2307 int ret;
2308
2309 if (kvm_enabled() && !kvm_has_sync_mmu()) {
ab5b027e 2310 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
625a5bef
AL
2311 return -1;
2312 }
2313
2314 ret = qemu_balloon_status(cb, opaque);
2315 if (!ret) {
ab5b027e 2316 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
625a5bef
AL
2317 return -1;
2318 }
2319
2320 return 0;
2321}
2322
2323/**
2324 * do_balloon(): Request VM to change its memory allocation
cc1d9c70 2325 */
625a5bef
AL
2326static int do_balloon(Monitor *mon, const QDict *params,
2327 MonitorCompletion cb, void *opaque)
df751fa8 2328{
625a5bef 2329 int ret;
df751fa8 2330
625a5bef 2331 if (kvm_enabled() && !kvm_has_sync_mmu()) {
ab5b027e 2332 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
625a5bef 2333 return -1;
cfdf2c40 2334 }
625a5bef
AL
2335
2336 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2337 if (ret == 0) {
ab5b027e 2338 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
625a5bef
AL
2339 return -1;
2340 }
2341
93d67ee6 2342 cb(opaque, NULL);
625a5bef 2343 return 0;
df751fa8
AL
2344}
2345
15dfcd45 2346static qemu_acl *find_acl(Monitor *mon, const char *name)
76655d6d 2347{
15dfcd45 2348 qemu_acl *acl = qemu_acl_find(name);
76655d6d 2349
76655d6d 2350 if (!acl) {
15dfcd45 2351 monitor_printf(mon, "acl: unknown list '%s'\n", name);
76655d6d 2352 }
15dfcd45
JK
2353 return acl;
2354}
2355
d54908a5 2356static void do_acl_show(Monitor *mon, const QDict *qdict)
15dfcd45 2357{
d54908a5 2358 const char *aclname = qdict_get_str(qdict, "aclname");
15dfcd45
JK
2359 qemu_acl *acl = find_acl(mon, aclname);
2360 qemu_acl_entry *entry;
2361 int i = 0;
76655d6d 2362
15dfcd45 2363 if (acl) {
28a76be8 2364 monitor_printf(mon, "policy: %s\n",
76655d6d 2365 acl->defaultDeny ? "deny" : "allow");
72cf2d4f 2366 QTAILQ_FOREACH(entry, &acl->entries, next) {
28a76be8
AL
2367 i++;
2368 monitor_printf(mon, "%d: %s %s\n", i,
15dfcd45 2369 entry->deny ? "deny" : "allow", entry->match);
28a76be8 2370 }
15dfcd45
JK
2371 }
2372}
2373
d54908a5 2374static void do_acl_reset(Monitor *mon, const QDict *qdict)
15dfcd45 2375{
d54908a5 2376 const char *aclname = qdict_get_str(qdict, "aclname");
15dfcd45
JK
2377 qemu_acl *acl = find_acl(mon, aclname);
2378
2379 if (acl) {
28a76be8
AL
2380 qemu_acl_reset(acl);
2381 monitor_printf(mon, "acl: removed all rules\n");
15dfcd45
JK
2382 }
2383}
2384
f18c16de 2385static void do_acl_policy(Monitor *mon, const QDict *qdict)
15dfcd45 2386{
f18c16de
LC
2387 const char *aclname = qdict_get_str(qdict, "aclname");
2388 const char *policy = qdict_get_str(qdict, "policy");
15dfcd45 2389 qemu_acl *acl = find_acl(mon, aclname);
28a76be8 2390
15dfcd45
JK
2391 if (acl) {
2392 if (strcmp(policy, "allow") == 0) {
28a76be8
AL
2393 acl->defaultDeny = 0;
2394 monitor_printf(mon, "acl: policy set to 'allow'\n");
15dfcd45 2395 } else if (strcmp(policy, "deny") == 0) {
28a76be8
AL
2396 acl->defaultDeny = 1;
2397 monitor_printf(mon, "acl: policy set to 'deny'\n");
2398 } else {
15dfcd45
JK
2399 monitor_printf(mon, "acl: unknown policy '%s', "
2400 "expected 'deny' or 'allow'\n", policy);
28a76be8 2401 }
15dfcd45
JK
2402 }
2403}
28a76be8 2404
1bd1442e 2405static void do_acl_add(Monitor *mon, const QDict *qdict)
15dfcd45 2406{
1bd1442e
LC
2407 const char *aclname = qdict_get_str(qdict, "aclname");
2408 const char *match = qdict_get_str(qdict, "match");
2409 const char *policy = qdict_get_str(qdict, "policy");
2410 int has_index = qdict_haskey(qdict, "index");
2411 int index = qdict_get_try_int(qdict, "index", -1);
15dfcd45
JK
2412 qemu_acl *acl = find_acl(mon, aclname);
2413 int deny, ret;
2414
2415 if (acl) {
2416 if (strcmp(policy, "allow") == 0) {
2417 deny = 0;
2418 } else if (strcmp(policy, "deny") == 0) {
2419 deny = 1;
2420 } else {
2421 monitor_printf(mon, "acl: unknown policy '%s', "
2422 "expected 'deny' or 'allow'\n", policy);
28a76be8
AL
2423 return;
2424 }
28a76be8
AL
2425 if (has_index)
2426 ret = qemu_acl_insert(acl, deny, match, index);
2427 else
2428 ret = qemu_acl_append(acl, deny, match);
2429 if (ret < 0)
2430 monitor_printf(mon, "acl: unable to add acl entry\n");
2431 else
2432 monitor_printf(mon, "acl: added rule at position %d\n", ret);
15dfcd45
JK
2433 }
2434}
28a76be8 2435
f18c16de 2436static void do_acl_remove(Monitor *mon, const QDict *qdict)
15dfcd45 2437{
f18c16de
LC
2438 const char *aclname = qdict_get_str(qdict, "aclname");
2439 const char *match = qdict_get_str(qdict, "match");
15dfcd45
JK
2440 qemu_acl *acl = find_acl(mon, aclname);
2441 int ret;
28a76be8 2442
15dfcd45 2443 if (acl) {
28a76be8
AL
2444 ret = qemu_acl_remove(acl, match);
2445 if (ret < 0)
2446 monitor_printf(mon, "acl: no matching acl entry\n");
2447 else
2448 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
76655d6d
AL
2449 }
2450}
2451
79c4f6b0 2452#if defined(TARGET_I386)
37b7ad48 2453static void do_inject_mce(Monitor *mon, const QDict *qdict)
79c4f6b0
HY
2454{
2455 CPUState *cenv;
37b7ad48
LC
2456 int cpu_index = qdict_get_int(qdict, "cpu_index");
2457 int bank = qdict_get_int(qdict, "bank");
2458 uint64_t status = qdict_get_int(qdict, "status");
2459 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2460 uint64_t addr = qdict_get_int(qdict, "addr");
2461 uint64_t misc = qdict_get_int(qdict, "misc");
79c4f6b0
HY
2462
2463 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2464 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2465 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2466 break;
2467 }
2468}
2469#endif
2470
6ad3ebd2 2471static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
f07918fd 2472{
d54908a5 2473 const char *fdname = qdict_get_str(qdict, "fdname");
c227f099 2474 mon_fd_t *monfd;
f07918fd
MM
2475 int fd;
2476
2477 fd = qemu_chr_get_msgfd(mon->chr);
2478 if (fd == -1) {
ab5b027e 2479 qerror_report(QERR_FD_NOT_SUPPLIED);
6ad3ebd2 2480 return -1;
f07918fd
MM
2481 }
2482
2483 if (qemu_isdigit(fdname[0])) {
ab5b027e 2484 qerror_report(QERR_INVALID_PARAMETER, "fdname");
6ad3ebd2 2485 return -1;
f07918fd
MM
2486 }
2487
2488 fd = dup(fd);
2489 if (fd == -1) {
7cdfcfe1 2490 if (errno == EMFILE)
ab5b027e 2491 qerror_report(QERR_TOO_MANY_FILES);
7cdfcfe1 2492 else
ab5b027e 2493 qerror_report(QERR_UNDEFINED_ERROR);
6ad3ebd2 2494 return -1;
f07918fd
MM
2495 }
2496
72cf2d4f 2497 QLIST_FOREACH(monfd, &mon->fds, next) {
f07918fd
MM
2498 if (strcmp(monfd->name, fdname) != 0) {
2499 continue;
2500 }
2501
2502 close(monfd->fd);
2503 monfd->fd = fd;
6ad3ebd2 2504 return 0;
f07918fd
MM
2505 }
2506
c227f099 2507 monfd = qemu_mallocz(sizeof(mon_fd_t));
f07918fd
MM
2508 monfd->name = qemu_strdup(fdname);
2509 monfd->fd = fd;
2510
72cf2d4f 2511 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
6ad3ebd2 2512 return 0;
f07918fd
MM
2513}
2514
aeb91c1e 2515static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
f07918fd 2516{
d54908a5 2517 const char *fdname = qdict_get_str(qdict, "fdname");
c227f099 2518 mon_fd_t *monfd;
f07918fd 2519
72cf2d4f 2520 QLIST_FOREACH(monfd, &mon->fds, next) {
f07918fd
MM
2521 if (strcmp(monfd->name, fdname) != 0) {
2522 continue;
2523 }
2524
72cf2d4f 2525 QLIST_REMOVE(monfd, next);
f07918fd
MM
2526 close(monfd->fd);
2527 qemu_free(monfd->name);
2528 qemu_free(monfd);
aeb91c1e 2529 return 0;
f07918fd
MM
2530 }
2531
ab5b027e 2532 qerror_report(QERR_FD_NOT_FOUND, fdname);
aeb91c1e 2533 return -1;
f07918fd
MM
2534}
2535
d54908a5 2536static void do_loadvm(Monitor *mon, const QDict *qdict)
c8d41b2c
JQ
2537{
2538 int saved_vm_running = vm_running;
d54908a5 2539 const char *name = qdict_get_str(qdict, "name");
c8d41b2c
JQ
2540
2541 vm_stop(0);
2542
03cd4655 2543 if (load_vmstate(name) >= 0 && saved_vm_running)
c8d41b2c
JQ
2544 vm_start();
2545}
2546
7768e04c
MM
2547int monitor_get_fd(Monitor *mon, const char *fdname)
2548{
c227f099 2549 mon_fd_t *monfd;
7768e04c 2550
72cf2d4f 2551 QLIST_FOREACH(monfd, &mon->fds, next) {
7768e04c
MM
2552 int fd;
2553
2554 if (strcmp(monfd->name, fdname) != 0) {
2555 continue;
2556 }
2557
2558 fd = monfd->fd;
2559
2560 /* caller takes ownership of fd */
72cf2d4f 2561 QLIST_REMOVE(monfd, next);
7768e04c
MM
2562 qemu_free(monfd->name);
2563 qemu_free(monfd);
2564
2565 return fd;
2566 }
2567
2568 return -1;
2569}
2570
c227f099 2571static const mon_cmd_t mon_cmds[] = {
2313086a 2572#include "qemu-monitor.h"
5fafdf24 2573 { NULL, NULL, },
9dc39cba
FB
2574};
2575
2313086a 2576/* Please update qemu-monitor.hx when adding or changing commands */
c227f099 2577static const mon_cmd_t info_cmds[] = {
d7f9b689
LC
2578 {
2579 .name = "version",
2580 .args_type = "",
d7f9b689
LC
2581 .params = "",
2582 .help = "show the version of QEMU",
45e914cf 2583 .user_print = do_info_version_print,
ab2d3187 2584 .mhandler.info_new = do_info_version,
d7f9b689 2585 },
e3bba9d0
LC
2586 {
2587 .name = "commands",
2588 .args_type = "",
2589 .params = "",
2590 .help = "list QMP available commands",
2591 .user_print = monitor_user_noop,
2592 .mhandler.info_new = do_info_commands,
2593 },
d7f9b689
LC
2594 {
2595 .name = "network",
2596 .args_type = "",
d7f9b689
LC
2597 .params = "",
2598 .help = "show the network state",
910df89d 2599 .mhandler.info = do_info_network,
d7f9b689
LC
2600 },
2601 {
2602 .name = "chardev",
2603 .args_type = "",
d7f9b689
LC
2604 .params = "",
2605 .help = "show the character devices",
588b3832
LC
2606 .user_print = qemu_chr_info_print,
2607 .mhandler.info_new = qemu_chr_info,
d7f9b689
LC
2608 },
2609 {
2610 .name = "block",
2611 .args_type = "",
d7f9b689
LC
2612 .params = "",
2613 .help = "show the block devices",
d15e5465
LC
2614 .user_print = bdrv_info_print,
2615 .mhandler.info_new = bdrv_info,
d7f9b689
LC
2616 },
2617 {
2618 .name = "blockstats",
2619 .args_type = "",
d7f9b689
LC
2620 .params = "",
2621 .help = "show block device statistics",
218a536a
LC
2622 .user_print = bdrv_stats_print,
2623 .mhandler.info_new = bdrv_info_stats,
d7f9b689
LC
2624 },
2625 {
2626 .name = "registers",
2627 .args_type = "",
d7f9b689
LC
2628 .params = "",
2629 .help = "show the cpu registers",
910df89d 2630 .mhandler.info = do_info_registers,
d7f9b689
LC
2631 },
2632 {
2633 .name = "cpus",
2634 .args_type = "",
d7f9b689
LC
2635 .params = "",
2636 .help = "show infos for each CPU",
8f3cec0b
LC
2637 .user_print = monitor_print_cpus,
2638 .mhandler.info_new = do_info_cpus,
d7f9b689
LC
2639 },
2640 {
2641 .name = "history",
2642 .args_type = "",
d7f9b689
LC
2643 .params = "",
2644 .help = "show the command line history",
910df89d 2645 .mhandler.info = do_info_history,
d7f9b689
LC
2646 },
2647 {
2648 .name = "irq",
2649 .args_type = "",
d7f9b689
LC
2650 .params = "",
2651 .help = "show the interrupts statistics (if available)",
910df89d 2652 .mhandler.info = irq_info,
d7f9b689
LC
2653 },
2654 {
2655 .name = "pic",
2656 .args_type = "",
d7f9b689
LC
2657 .params = "",
2658 .help = "show i8259 (PIC) state",
910df89d 2659 .mhandler.info = pic_info,
d7f9b689
LC
2660 },
2661 {
2662 .name = "pci",
2663 .args_type = "",
d7f9b689
LC
2664 .params = "",
2665 .help = "show PCI info",
163c8a59
LC
2666 .user_print = do_pci_info_print,
2667 .mhandler.info_new = do_pci_info,
d7f9b689 2668 },
7c664e2f 2669#if defined(TARGET_I386) || defined(TARGET_SH4)
d7f9b689
LC
2670 {
2671 .name = "tlb",
2672 .args_type = "",
d7f9b689
LC
2673 .params = "",
2674 .help = "show virtual to physical memory mappings",
910df89d 2675 .mhandler.info = tlb_info,
d7f9b689 2676 },
7c664e2f
AJ
2677#endif
2678#if defined(TARGET_I386)
d7f9b689
LC
2679 {
2680 .name = "mem",
2681 .args_type = "",
d7f9b689
LC
2682 .params = "",
2683 .help = "show the active virtual memory mappings",
910df89d 2684 .mhandler.info = mem_info,
d7f9b689
LC
2685 },
2686 {
2687 .name = "hpet",
2688 .args_type = "",
d7f9b689
LC
2689 .params = "",
2690 .help = "show state of HPET",
14f0720d
LC
2691 .user_print = do_info_hpet_print,
2692 .mhandler.info_new = do_info_hpet,
d7f9b689 2693 },
b86bda5b 2694#endif
d7f9b689
LC
2695 {
2696 .name = "jit",
2697 .args_type = "",
d7f9b689
LC
2698 .params = "",
2699 .help = "show dynamic compiler info",
910df89d 2700 .mhandler.info = do_info_jit,
d7f9b689
LC
2701 },
2702 {
2703 .name = "kvm",
2704 .args_type = "",
d7f9b689
LC
2705 .params = "",
2706 .help = "show KVM information",
2af5ba71
LC
2707 .user_print = do_info_kvm_print,
2708 .mhandler.info_new = do_info_kvm,
d7f9b689
LC
2709 },
2710 {
2711 .name = "numa",
2712 .args_type = "",
d7f9b689
LC
2713 .params = "",
2714 .help = "show NUMA information",
910df89d 2715 .mhandler.info = do_info_numa,
d7f9b689
LC
2716 },
2717 {
2718 .name = "usb",
2719 .args_type = "",
d7f9b689
LC
2720 .params = "",
2721 .help = "show guest USB devices",
910df89d 2722 .mhandler.info = usb_info,
d7f9b689
LC
2723 },
2724 {
2725 .name = "usbhost",
2726 .args_type = "",
d7f9b689
LC
2727 .params = "",
2728 .help = "show host USB devices",
910df89d 2729 .mhandler.info = usb_host_info,
d7f9b689
LC
2730 },
2731 {
2732 .name = "profile",
2733 .args_type = "",
d7f9b689
LC
2734 .params = "",
2735 .help = "show profiling information",
910df89d 2736 .mhandler.info = do_info_profile,
d7f9b689
LC
2737 },
2738 {
2739 .name = "capture",
2740 .args_type = "",
d7f9b689
LC
2741 .params = "",
2742 .help = "show capture information",
910df89d 2743 .mhandler.info = do_info_capture,
d7f9b689
LC
2744 },
2745 {
2746 .name = "snapshots",
2747 .args_type = "",
d7f9b689
LC
2748 .params = "",
2749 .help = "show the currently saved VM snapshots",
910df89d 2750 .mhandler.info = do_info_snapshots,
d7f9b689
LC
2751 },
2752 {
2753 .name = "status",
2754 .args_type = "",
d7f9b689
LC
2755 .params = "",
2756 .help = "show the current VM status (running|paused)",
c0e8520e
LC
2757 .user_print = do_info_status_print,
2758 .mhandler.info_new = do_info_status,
d7f9b689
LC
2759 },
2760 {
2761 .name = "pcmcia",
2762 .args_type = "",
d7f9b689
LC
2763 .params = "",
2764 .help = "show guest PCMCIA status",
910df89d 2765 .mhandler.info = pcmcia_info,
d7f9b689
LC
2766 },
2767 {
2768 .name = "mice",
2769 .args_type = "",
d7f9b689
LC
2770 .params = "",
2771 .help = "show which guest mouse is receiving events",
e78c48ec
LC
2772 .user_print = do_info_mice_print,
2773 .mhandler.info_new = do_info_mice,
d7f9b689
LC
2774 },
2775 {
2776 .name = "vnc",
2777 .args_type = "",
d7f9b689
LC
2778 .params = "",
2779 .help = "show the vnc server status",
d96fd29c
LC
2780 .user_print = do_info_vnc_print,
2781 .mhandler.info_new = do_info_vnc,
d7f9b689
LC
2782 },
2783 {
2784 .name = "name",
2785 .args_type = "",
d7f9b689
LC
2786 .params = "",
2787 .help = "show the current VM name",
e05486cb
LC
2788 .user_print = do_info_name_print,
2789 .mhandler.info_new = do_info_name,
d7f9b689
LC
2790 },
2791 {
2792 .name = "uuid",
2793 .args_type = "",
d7f9b689
LC
2794 .params = "",
2795 .help = "show the current VM UUID",
9603ceba
LC
2796 .user_print = do_info_uuid_print,
2797 .mhandler.info_new = do_info_uuid,
d7f9b689 2798 },
76a66253 2799#if defined(TARGET_PPC)
d7f9b689
LC
2800 {
2801 .name = "cpustats",
2802 .args_type = "",
d7f9b689
LC
2803 .params = "",
2804 .help = "show CPU statistics",
910df89d 2805 .mhandler.info = do_info_cpu_stats,
d7f9b689 2806 },
76a66253 2807#endif
31a60e22 2808#if defined(CONFIG_SLIRP)
d7f9b689
LC
2809 {
2810 .name = "usernet",
2811 .args_type = "",
d7f9b689
LC
2812 .params = "",
2813 .help = "show user network stack connection states",
910df89d 2814 .mhandler.info = do_info_usernet,
d7f9b689 2815 },
31a60e22 2816#endif
d7f9b689
LC
2817 {
2818 .name = "migrate",
2819 .args_type = "",
d7f9b689
LC
2820 .params = "",
2821 .help = "show migration status",
c86a6683
LC
2822 .user_print = do_info_migrate_print,
2823 .mhandler.info_new = do_info_migrate,
d7f9b689
LC
2824 },
2825 {
2826 .name = "balloon",
2827 .args_type = "",
d7f9b689
LC
2828 .params = "",
2829 .help = "show balloon information",
cc1d9c70 2830 .user_print = monitor_print_balloon,
625a5bef
AL
2831 .mhandler.info_async = do_info_balloon,
2832 .async = 1,
d7f9b689
LC
2833 },
2834 {
2835 .name = "qtree",
2836 .args_type = "",
d7f9b689
LC
2837 .params = "",
2838 .help = "show device tree",
910df89d 2839 .mhandler.info = do_info_qtree,
d7f9b689
LC
2840 },
2841 {
2842 .name = "qdm",
2843 .args_type = "",
d7f9b689
LC
2844 .params = "",
2845 .help = "show qdev device model list",
910df89d 2846 .mhandler.info = do_info_qdm,
d7f9b689
LC
2847 },
2848 {
2849 .name = "roms",
2850 .args_type = "",
d7f9b689
LC
2851 .params = "",
2852 .help = "show roms",
910df89d 2853 .mhandler.info = do_info_roms,
d7f9b689
LC
2854 },
2855 {
2856 .name = NULL,
2857 },
9dc39cba
FB
2858};
2859
9307c4c1
FB
2860/*******************************************************************/
2861
2862static const char *pch;
2863static jmp_buf expr_env;
2864
92a31b1f
FB
2865#define MD_TLONG 0
2866#define MD_I32 1
2867
9307c4c1
FB
2868typedef struct MonitorDef {
2869 const char *name;
2870 int offset;
8662d656 2871 target_long (*get_value)(const struct MonitorDef *md, int val);
92a31b1f 2872 int type;
9307c4c1
FB
2873} MonitorDef;
2874
57206fd4 2875#if defined(TARGET_I386)
8662d656 2876static target_long monitor_get_pc (const struct MonitorDef *md, int val)
57206fd4 2877{
6a00d601 2878 CPUState *env = mon_get_cpu();
6a00d601 2879 return env->eip + env->segs[R_CS].base;
57206fd4
FB
2880}
2881#endif
2882
a541f297 2883#if defined(TARGET_PPC)
8662d656 2884static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
a541f297 2885{
6a00d601 2886 CPUState *env = mon_get_cpu();
a541f297
FB
2887 unsigned int u;
2888 int i;
2889
2890 u = 0;
2891 for (i = 0; i < 8; i++)
28a76be8 2892 u |= env->crf[i] << (32 - (4 * i));
a541f297
FB
2893
2894 return u;
2895}
2896
8662d656 2897static target_long monitor_get_msr (const struct MonitorDef *md, int val)
a541f297 2898{
6a00d601 2899 CPUState *env = mon_get_cpu();
0411a972 2900 return env->msr;
a541f297
FB
2901}
2902
8662d656 2903static target_long monitor_get_xer (const struct MonitorDef *md, int val)
a541f297 2904{
6a00d601 2905 CPUState *env = mon_get_cpu();
3d7b417e 2906 return env->xer;
a541f297 2907}
9fddaa0c 2908
8662d656 2909static target_long monitor_get_decr (const struct MonitorDef *md, int val)
9fddaa0c 2910{
6a00d601 2911 CPUState *env = mon_get_cpu();
6a00d601 2912 return cpu_ppc_load_decr(env);
9fddaa0c
FB
2913}
2914
8662d656 2915static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
9fddaa0c 2916{
6a00d601 2917 CPUState *env = mon_get_cpu();
6a00d601 2918 return cpu_ppc_load_tbu(env);
9fddaa0c
FB
2919}
2920
8662d656 2921static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
9fddaa0c 2922{
6a00d601 2923 CPUState *env = mon_get_cpu();
6a00d601 2924 return cpu_ppc_load_tbl(env);
9fddaa0c 2925}
a541f297
FB
2926#endif
2927
e95c8d51 2928#if defined(TARGET_SPARC)
7b936c0c 2929#ifndef TARGET_SPARC64
8662d656 2930static target_long monitor_get_psr (const struct MonitorDef *md, int val)
e95c8d51 2931{
6a00d601 2932 CPUState *env = mon_get_cpu();
6a00d601 2933 return GET_PSR(env);
e95c8d51 2934}
7b936c0c 2935#endif
e95c8d51 2936
8662d656 2937static target_long monitor_get_reg(const struct MonitorDef *md, int val)
e95c8d51 2938{
6a00d601 2939 CPUState *env = mon_get_cpu();
6a00d601 2940 return env->regwptr[val];
e95c8d51
FB
2941}
2942#endif
2943
8662d656 2944static const MonitorDef monitor_defs[] = {
9307c4c1 2945#ifdef TARGET_I386
57206fd4
FB
2946
2947#define SEG(name, seg) \
92a31b1f 2948 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
57206fd4 2949 { name ".base", offsetof(CPUState, segs[seg].base) },\
92a31b1f 2950 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
57206fd4 2951
9307c4c1
FB
2952 { "eax", offsetof(CPUState, regs[0]) },
2953 { "ecx", offsetof(CPUState, regs[1]) },
2954 { "edx", offsetof(CPUState, regs[2]) },
2955 { "ebx", offsetof(CPUState, regs[3]) },
2956 { "esp|sp", offsetof(CPUState, regs[4]) },
2957 { "ebp|fp", offsetof(CPUState, regs[5]) },
2958 { "esi", offsetof(CPUState, regs[6]) },
01038d2a 2959 { "edi", offsetof(CPUState, regs[7]) },
92a31b1f
FB
2960#ifdef TARGET_X86_64
2961 { "r8", offsetof(CPUState, regs[8]) },
2962 { "r9", offsetof(CPUState, regs[9]) },
2963 { "r10", offsetof(CPUState, regs[10]) },
2964 { "r11", offsetof(CPUState, regs[11]) },
2965 { "r12", offsetof(CPUState, regs[12]) },
2966 { "r13", offsetof(CPUState, regs[13]) },
2967 { "r14", offsetof(CPUState, regs[14]) },
2968 { "r15", offsetof(CPUState, regs[15]) },
2969#endif
9307c4c1 2970 { "eflags", offsetof(CPUState, eflags) },
57206fd4
FB
2971 { "eip", offsetof(CPUState, eip) },
2972 SEG("cs", R_CS)
2973 SEG("ds", R_DS)
2974 SEG("es", R_ES)
01038d2a 2975 SEG("ss", R_SS)
57206fd4
FB
2976 SEG("fs", R_FS)
2977 SEG("gs", R_GS)
2978 { "pc", 0, monitor_get_pc, },
a541f297 2979#elif defined(TARGET_PPC)
ff937dba 2980 /* General purpose registers */
a541f297
FB
2981 { "r0", offsetof(CPUState, gpr[0]) },
2982 { "r1", offsetof(CPUState, gpr[1]) },
2983 { "r2", offsetof(CPUState, gpr[2]) },
2984 { "r3", offsetof(CPUState, gpr[3]) },
2985 { "r4", offsetof(CPUState, gpr[4]) },
2986 { "r5", offsetof(CPUState, gpr[5]) },
2987 { "r6", offsetof(CPUState, gpr[6]) },
2988 { "r7", offsetof(CPUState, gpr[7]) },
2989 { "r8", offsetof(CPUState, gpr[8]) },
2990 { "r9", offsetof(CPUState, gpr[9]) },
2991 { "r10", offsetof(CPUState, gpr[10]) },
2992 { "r11", offsetof(CPUState, gpr[11]) },
2993 { "r12", offsetof(CPUState, gpr[12]) },
2994 { "r13", offsetof(CPUState, gpr[13]) },
2995 { "r14", offsetof(CPUState, gpr[14]) },
2996 { "r15", offsetof(CPUState, gpr[15]) },
2997 { "r16", offsetof(CPUState, gpr[16]) },
2998 { "r17", offsetof(CPUState, gpr[17]) },
2999 { "r18", offsetof(CPUState, gpr[18]) },
3000 { "r19", offsetof(CPUState, gpr[19]) },
3001 { "r20", offsetof(CPUState, gpr[20]) },
3002 { "r21", offsetof(CPUState, gpr[21]) },
3003 { "r22", offsetof(CPUState, gpr[22]) },
3004 { "r23", offsetof(CPUState, gpr[23]) },
3005 { "r24", offsetof(CPUState, gpr[24]) },
3006 { "r25", offsetof(CPUState, gpr[25]) },
3007 { "r26", offsetof(CPUState, gpr[26]) },
3008 { "r27", offsetof(CPUState, gpr[27]) },
3009 { "r28", offsetof(CPUState, gpr[28]) },
3010 { "r29", offsetof(CPUState, gpr[29]) },
3011 { "r30", offsetof(CPUState, gpr[30]) },
3012 { "r31", offsetof(CPUState, gpr[31]) },
ff937dba
JM
3013 /* Floating point registers */
3014 { "f0", offsetof(CPUState, fpr[0]) },
3015 { "f1", offsetof(CPUState, fpr[1]) },
3016 { "f2", offsetof(CPUState, fpr[2]) },
3017 { "f3", offsetof(CPUState, fpr[3]) },
3018 { "f4", offsetof(CPUState, fpr[4]) },
3019 { "f5", offsetof(CPUState, fpr[5]) },
3020 { "f6", offsetof(CPUState, fpr[6]) },
3021 { "f7", offsetof(CPUState, fpr[7]) },
3022 { "f8", offsetof(CPUState, fpr[8]) },
3023 { "f9", offsetof(CPUState, fpr[9]) },
3024 { "f10", offsetof(CPUState, fpr[10]) },
3025 { "f11", offsetof(CPUState, fpr[11]) },
3026 { "f12", offsetof(CPUState, fpr[12]) },
3027 { "f13", offsetof(CPUState, fpr[13]) },
3028 { "f14", offsetof(CPUState, fpr[14]) },
3029 { "f15", offsetof(CPUState, fpr[15]) },
3030 { "f16", offsetof(CPUState, fpr[16]) },
3031 { "f17", offsetof(CPUState, fpr[17]) },
3032 { "f18", offsetof(CPUState, fpr[18]) },
3033 { "f19", offsetof(CPUState, fpr[19]) },
3034 { "f20", offsetof(CPUState, fpr[20]) },
3035 { "f21", offsetof(CPUState, fpr[21]) },
3036 { "f22", offsetof(CPUState, fpr[22]) },
3037 { "f23", offsetof(CPUState, fpr[23]) },
3038 { "f24", offsetof(CPUState, fpr[24]) },
3039 { "f25", offsetof(CPUState, fpr[25]) },
3040 { "f26", offsetof(CPUState, fpr[26]) },
3041 { "f27", offsetof(CPUState, fpr[27]) },
3042 { "f28", offsetof(CPUState, fpr[28]) },
3043 { "f29", offsetof(CPUState, fpr[29]) },
3044 { "f30", offsetof(CPUState, fpr[30]) },
3045 { "f31", offsetof(CPUState, fpr[31]) },
3046 { "fpscr", offsetof(CPUState, fpscr) },
3047 /* Next instruction pointer */
57206fd4 3048 { "nip|pc", offsetof(CPUState, nip) },
a541f297
FB
3049 { "lr", offsetof(CPUState, lr) },
3050 { "ctr", offsetof(CPUState, ctr) },
9fddaa0c 3051 { "decr", 0, &monitor_get_decr, },
a541f297 3052 { "ccr", 0, &monitor_get_ccr, },
ff937dba 3053 /* Machine state register */
a541f297
FB
3054 { "msr", 0, &monitor_get_msr, },
3055 { "xer", 0, &monitor_get_xer, },
9fddaa0c
FB
3056 { "tbu", 0, &monitor_get_tbu, },
3057 { "tbl", 0, &monitor_get_tbl, },
ff937dba
JM
3058#if defined(TARGET_PPC64)
3059 /* Address space register */
3060 { "asr", offsetof(CPUState, asr) },
3061#endif
3062 /* Segment registers */
a541f297
FB
3063 { "sdr1", offsetof(CPUState, sdr1) },
3064 { "sr0", offsetof(CPUState, sr[0]) },
3065 { "sr1", offsetof(CPUState, sr[1]) },
3066 { "sr2", offsetof(CPUState, sr[2]) },
3067 { "sr3", offsetof(CPUState, sr[3]) },
3068 { "sr4", offsetof(CPUState, sr[4]) },
3069 { "sr5", offsetof(CPUState, sr[5]) },
3070 { "sr6", offsetof(CPUState, sr[6]) },
3071 { "sr7", offsetof(CPUState, sr[7]) },
3072 { "sr8", offsetof(CPUState, sr[8]) },
3073 { "sr9", offsetof(CPUState, sr[9]) },
3074 { "sr10", offsetof(CPUState, sr[10]) },
3075 { "sr11", offsetof(CPUState, sr[11]) },
3076 { "sr12", offsetof(CPUState, sr[12]) },
3077 { "sr13", offsetof(CPUState, sr[13]) },
3078 { "sr14", offsetof(CPUState, sr[14]) },
3079 { "sr15", offsetof(CPUState, sr[15]) },
3080 /* Too lazy to put BATs and SPRs ... */
e95c8d51
FB
3081#elif defined(TARGET_SPARC)
3082 { "g0", offsetof(CPUState, gregs[0]) },
3083 { "g1", offsetof(CPUState, gregs[1]) },
3084 { "g2", offsetof(CPUState, gregs[2]) },
3085 { "g3", offsetof(CPUState, gregs[3]) },
3086 { "g4", offsetof(CPUState, gregs[4]) },
3087 { "g5", offsetof(CPUState, gregs[5]) },
3088 { "g6", offsetof(CPUState, gregs[6]) },
3089 { "g7", offsetof(CPUState, gregs[7]) },
3090 { "o0", 0, monitor_get_reg },
3091 { "o1", 1, monitor_get_reg },
3092 { "o2", 2, monitor_get_reg },
3093 { "o3", 3, monitor_get_reg },
3094 { "o4", 4, monitor_get_reg },
3095 { "o5", 5, monitor_get_reg },
3096 { "o6", 6, monitor_get_reg },
3097 { "o7", 7, monitor_get_reg },
3098 { "l0", 8, monitor_get_reg },
3099 { "l1", 9, monitor_get_reg },
3100 { "l2", 10, monitor_get_reg },
3101 { "l3", 11, monitor_get_reg },
3102 { "l4", 12, monitor_get_reg },
3103 { "l5", 13, monitor_get_reg },
3104 { "l6", 14, monitor_get_reg },
3105 { "l7", 15, monitor_get_reg },
3106 { "i0", 16, monitor_get_reg },
3107 { "i1", 17, monitor_get_reg },
3108 { "i2", 18, monitor_get_reg },
3109 { "i3", 19, monitor_get_reg },
3110 { "i4", 20, monitor_get_reg },
3111 { "i5", 21, monitor_get_reg },
3112 { "i6", 22, monitor_get_reg },
3113 { "i7", 23, monitor_get_reg },
3114 { "pc", offsetof(CPUState, pc) },
3115 { "npc", offsetof(CPUState, npc) },
3116 { "y", offsetof(CPUState, y) },
7b936c0c 3117#ifndef TARGET_SPARC64
e95c8d51
FB
3118 { "psr", 0, &monitor_get_psr, },
3119 { "wim", offsetof(CPUState, wim) },
7b936c0c 3120#endif
e95c8d51
FB
3121 { "tbr", offsetof(CPUState, tbr) },
3122 { "fsr", offsetof(CPUState, fsr) },
3123 { "f0", offsetof(CPUState, fpr[0]) },
3124 { "f1", offsetof(CPUState, fpr[1]) },
3125 { "f2", offsetof(CPUState, fpr[2]) },
3126 { "f3", offsetof(CPUState, fpr[3]) },
3127 { "f4", offsetof(CPUState, fpr[4]) },
3128 { "f5", offsetof(CPUState, fpr[5]) },
3129 { "f6", offsetof(CPUState, fpr[6]) },
3130 { "f7", offsetof(CPUState, fpr[7]) },
3131 { "f8", offsetof(CPUState, fpr[8]) },
3132 { "f9", offsetof(CPUState, fpr[9]) },
3133 { "f10", offsetof(CPUState, fpr[10]) },
3134 { "f11", offsetof(CPUState, fpr[11]) },
3135 { "f12", offsetof(CPUState, fpr[12]) },
3136 { "f13", offsetof(CPUState, fpr[13]) },
3137 { "f14", offsetof(CPUState, fpr[14]) },
3138 { "f15", offsetof(CPUState, fpr[15]) },
3139 { "f16", offsetof(CPUState, fpr[16]) },
3140 { "f17", offsetof(CPUState, fpr[17]) },
3141 { "f18", offsetof(CPUState, fpr[18]) },
3142 { "f19", offsetof(CPUState, fpr[19]) },
3143 { "f20", offsetof(CPUState, fpr[20]) },
3144 { "f21", offsetof(CPUState, fpr[21]) },
3145 { "f22", offsetof(CPUState, fpr[22]) },
3146 { "f23", offsetof(CPUState, fpr[23]) },
3147 { "f24", offsetof(CPUState, fpr[24]) },
3148 { "f25", offsetof(CPUState, fpr[25]) },
3149 { "f26", offsetof(CPUState, fpr[26]) },
3150 { "f27", offsetof(CPUState, fpr[27]) },
3151 { "f28", offsetof(CPUState, fpr[28]) },
3152 { "f29", offsetof(CPUState, fpr[29]) },
3153 { "f30", offsetof(CPUState, fpr[30]) },
3154 { "f31", offsetof(CPUState, fpr[31]) },
7b936c0c
FB
3155#ifdef TARGET_SPARC64
3156 { "f32", offsetof(CPUState, fpr[32]) },
3157 { "f34", offsetof(CPUState, fpr[34]) },
3158 { "f36", offsetof(CPUState, fpr[36]) },
3159 { "f38", offsetof(CPUState, fpr[38]) },
3160 { "f40", offsetof(CPUState, fpr[40]) },
3161 { "f42", offsetof(CPUState, fpr[42]) },
3162 { "f44", offsetof(CPUState, fpr[44]) },
3163 { "f46", offsetof(CPUState, fpr[46]) },
3164 { "f48", offsetof(CPUState, fpr[48]) },
3165 { "f50", offsetof(CPUState, fpr[50]) },
3166 { "f52", offsetof(CPUState, fpr[52]) },
3167 { "f54", offsetof(CPUState, fpr[54]) },
3168 { "f56", offsetof(CPUState, fpr[56]) },
3169 { "f58", offsetof(CPUState, fpr[58]) },
3170 { "f60", offsetof(CPUState, fpr[60]) },
3171 { "f62", offsetof(CPUState, fpr[62]) },
3172 { "asi", offsetof(CPUState, asi) },
3173 { "pstate", offsetof(CPUState, pstate) },
3174 { "cansave", offsetof(CPUState, cansave) },
3175 { "canrestore", offsetof(CPUState, canrestore) },
3176 { "otherwin", offsetof(CPUState, otherwin) },
3177 { "wstate", offsetof(CPUState, wstate) },
3178 { "cleanwin", offsetof(CPUState, cleanwin) },
3179 { "fprs", offsetof(CPUState, fprs) },
3180#endif
9307c4c1
FB
3181#endif
3182 { NULL },
3183};
3184
376253ec 3185static void expr_error(Monitor *mon, const char *msg)
9dc39cba 3186{
376253ec 3187 monitor_printf(mon, "%s\n", msg);
9307c4c1
FB
3188 longjmp(expr_env, 1);
3189}
3190
09b9418c 3191/* return 0 if OK, -1 if not found */
92a31b1f 3192static int get_monitor_def(target_long *pval, const char *name)
9307c4c1 3193{
8662d656 3194 const MonitorDef *md;
92a31b1f
FB
3195 void *ptr;
3196
9307c4c1
FB
3197 for(md = monitor_defs; md->name != NULL; md++) {
3198 if (compare_cmd(name, md->name)) {
3199 if (md->get_value) {
e95c8d51 3200 *pval = md->get_value(md, md->offset);
9307c4c1 3201 } else {
6a00d601 3202 CPUState *env = mon_get_cpu();
6a00d601 3203 ptr = (uint8_t *)env + md->offset;
92a31b1f
FB
3204 switch(md->type) {
3205 case MD_I32:
3206 *pval = *(int32_t *)ptr;
3207 break;
3208 case MD_TLONG:
3209 *pval = *(target_long *)ptr;
3210 break;
3211 default:
3212 *pval = 0;
3213 break;
3214 }
9307c4c1
FB
3215 }
3216 return 0;
3217 }
3218 }
3219 return -1;
3220}
3221
3222static void next(void)
3223{
660f11be 3224 if (*pch != '\0') {
9307c4c1 3225 pch++;
cd390083 3226 while (qemu_isspace(*pch))
9307c4c1
FB
3227 pch++;
3228 }
3229}
3230
376253ec 3231static int64_t expr_sum(Monitor *mon);
9307c4c1 3232
376253ec 3233static int64_t expr_unary(Monitor *mon)
9307c4c1 3234{
c2efc95d 3235 int64_t n;
9307c4c1 3236 char *p;
6a00d601 3237 int ret;
9307c4c1
FB
3238
3239 switch(*pch) {
3240 case '+':
3241 next();
376253ec 3242 n = expr_unary(mon);
9307c4c1
FB
3243 break;
3244 case '-':
3245 next();
376253ec 3246 n = -expr_unary(mon);
9307c4c1
FB
3247 break;
3248 case '~':
3249 next();
376253ec 3250 n = ~expr_unary(mon);
9307c4c1
FB
3251 break;
3252 case '(':
3253 next();
376253ec 3254 n = expr_sum(mon);
9307c4c1 3255 if (*pch != ')') {
376253ec 3256 expr_error(mon, "')' expected");
9307c4c1
FB
3257 }
3258 next();
3259 break;
81d0912d
FB
3260 case '\'':
3261 pch++;
3262 if (*pch == '\0')
376253ec 3263 expr_error(mon, "character constant expected");
81d0912d
FB
3264 n = *pch;
3265 pch++;
3266 if (*pch != '\'')
376253ec 3267 expr_error(mon, "missing terminating \' character");
81d0912d
FB
3268 next();
3269 break;
9307c4c1
FB
3270 case '$':
3271 {
3272 char buf[128], *q;
69b34976 3273 target_long reg=0;
3b46e624 3274
9307c4c1
FB
3275 pch++;
3276 q = buf;
3277 while ((*pch >= 'a' && *pch <= 'z') ||
3278 (*pch >= 'A' && *pch <= 'Z') ||
3279 (*pch >= '0' && *pch <= '9') ||
57206fd4 3280 *pch == '_' || *pch == '.') {
9307c4c1
FB
3281 if ((q - buf) < sizeof(buf) - 1)
3282 *q++ = *pch;
3283 pch++;
3284 }
cd390083 3285 while (qemu_isspace(*pch))
9307c4c1
FB
3286 pch++;
3287 *q = 0;
7743e588 3288 ret = get_monitor_def(&reg, buf);
09b9418c 3289 if (ret < 0)
376253ec 3290 expr_error(mon, "unknown register");
7743e588 3291 n = reg;
9307c4c1
FB
3292 }
3293 break;
3294 case '\0':
376253ec 3295 expr_error(mon, "unexpected end of expression");
9307c4c1
FB
3296 n = 0;
3297 break;
3298 default:
7743e588 3299#if TARGET_PHYS_ADDR_BITS > 32
4f4fbf77
FB
3300 n = strtoull(pch, &p, 0);
3301#else
9307c4c1 3302 n = strtoul(pch, &p, 0);
4f4fbf77 3303#endif
9307c4c1 3304 if (pch == p) {
376253ec 3305 expr_error(mon, "invalid char in expression");
9307c4c1
FB
3306 }
3307 pch = p;
cd390083 3308 while (qemu_isspace(*pch))
9307c4c1
FB
3309 pch++;
3310 break;
3311 }
3312 return n;
3313}
3314
3315
376253ec 3316static int64_t expr_prod(Monitor *mon)
9307c4c1 3317{
c2efc95d 3318 int64_t val, val2;
92a31b1f 3319 int op;
3b46e624 3320
376253ec 3321 val = expr_unary(mon);
9307c4c1
FB
3322 for(;;) {
3323 op = *pch;
3324 if (op != '*' && op != '/' && op != '%')
3325 break;
3326 next();
376253ec 3327 val2 = expr_unary(mon);
9307c4c1
FB
3328 switch(op) {
3329 default:
3330 case '*':
3331 val *= val2;
3332 break;
3333 case '/':
3334 case '%':
5fafdf24 3335 if (val2 == 0)
376253ec 3336 expr_error(mon, "division by zero");
9307c4c1
FB
3337 if (op == '/')
3338 val /= val2;
3339 else
3340 val %= val2;
3341 break;
3342 }
3343 }
3344 return val;
3345}
3346
376253ec 3347static int64_t expr_logic(Monitor *mon)
9307c4c1 3348{
c2efc95d 3349 int64_t val, val2;
92a31b1f 3350 int op;
9307c4c1 3351
376253ec 3352 val = expr_prod(mon);
9307c4c1
FB
3353 for(;;) {
3354 op = *pch;
3355 if (op != '&' && op != '|' && op != '^')
3356 break;
3357 next();
376253ec 3358 val2 = expr_prod(mon);
9307c4c1
FB
3359 switch(op) {
3360 default:
3361 case '&':
3362 val &= val2;
3363 break;
3364 case '|':
3365 val |= val2;
3366 break;
3367 case '^':
3368 val ^= val2;
3369 break;
3370 }
3371 }
3372 return val;
3373}
3374
376253ec 3375static int64_t expr_sum(Monitor *mon)
9307c4c1 3376{
c2efc95d 3377 int64_t val, val2;
92a31b1f 3378 int op;
9307c4c1 3379
376253ec 3380 val = expr_logic(mon);
9307c4c1
FB
3381 for(;;) {
3382 op = *pch;
3383 if (op != '+' && op != '-')
3384 break;
3385 next();
376253ec 3386 val2 = expr_logic(mon);
9307c4c1
FB
3387 if (op == '+')
3388 val += val2;
3389 else
3390 val -= val2;
3391 }
3392 return val;
3393}
3394
376253ec 3395static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
9307c4c1
FB
3396{
3397 pch = *pp;
3398 if (setjmp(expr_env)) {
3399 *pp = pch;
3400 return -1;
3401 }
cd390083 3402 while (qemu_isspace(*pch))
9307c4c1 3403 pch++;
376253ec 3404 *pval = expr_sum(mon);
9307c4c1
FB
3405 *pp = pch;
3406 return 0;
3407}
3408
3350a4dd
MA
3409static int get_double(Monitor *mon, double *pval, const char **pp)
3410{
3411 const char *p = *pp;
3412 char *tailp;
3413 double d;
3414
3415 d = strtod(p, &tailp);
3416 if (tailp == p) {
3417 monitor_printf(mon, "Number expected\n");
3418 return -1;
3419 }
3420 if (d != d || d - d != 0) {
3421 /* NaN or infinity */
3422 monitor_printf(mon, "Bad number\n");
3423 return -1;
3424 }
3425 *pval = d;
3426 *pp = tailp;
3427 return 0;
3428}
3429
9307c4c1
FB
3430static int get_str(char *buf, int buf_size, const char **pp)
3431{
3432 const char *p;
3433 char *q;
3434 int c;
3435
81d0912d 3436 q = buf;
9307c4c1 3437 p = *pp;
cd390083 3438 while (qemu_isspace(*p))
9307c4c1
FB
3439 p++;
3440 if (*p == '\0') {
3441 fail:
81d0912d 3442 *q = '\0';
9307c4c1
FB
3443 *pp = p;
3444 return -1;
3445 }
9307c4c1
FB
3446 if (*p == '\"') {
3447 p++;
3448 while (*p != '\0' && *p != '\"') {
3449 if (*p == '\\') {
3450 p++;
3451 c = *p++;
3452 switch(c) {
3453 case 'n':
3454 c = '\n';
3455 break;
3456 case 'r':
3457 c = '\r';
3458 break;
3459 case '\\':
3460 case '\'':
3461 case '\"':
3462 break;
3463 default:
3464 qemu_printf("unsupported escape code: '\\%c'\n", c);
3465 goto fail;
3466 }
3467 if ((q - buf) < buf_size - 1) {
3468 *q++ = c;
3469 }
3470 } else {
3471 if ((q - buf) < buf_size - 1) {
3472 *q++ = *p;
3473 }
3474 p++;
3475 }
3476 }
3477 if (*p != '\"') {
5b60212f 3478 qemu_printf("unterminated string\n");
9307c4c1
FB
3479 goto fail;
3480 }
3481 p++;
3482 } else {
cd390083 3483 while (*p != '\0' && !qemu_isspace(*p)) {
9307c4c1
FB
3484 if ((q - buf) < buf_size - 1) {
3485 *q++ = *p;
3486 }
3487 p++;
3488 }
9307c4c1 3489 }
81d0912d 3490 *q = '\0';
9307c4c1
FB
3491 *pp = p;
3492 return 0;
3493}
3494
4590fd80
LC
3495/*
3496 * Store the command-name in cmdname, and return a pointer to
3497 * the remaining of the command string.
3498 */
3499static const char *get_command_name(const char *cmdline,
3500 char *cmdname, size_t nlen)
3501{
3502 size_t len;
3503 const char *p, *pstart;
3504
3505 p = cmdline;
3506 while (qemu_isspace(*p))
3507 p++;
3508 if (*p == '\0')
3509 return NULL;
3510 pstart = p;
3511 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3512 p++;
3513 len = p - pstart;
3514 if (len > nlen - 1)
3515 len = nlen - 1;
3516 memcpy(cmdname, pstart, len);
3517 cmdname[len] = '\0';
3518 return p;
3519}
3520
4d76d2ba
LC
3521/**
3522 * Read key of 'type' into 'key' and return the current
3523 * 'type' pointer.
3524 */
3525static char *key_get_info(const char *type, char **key)
3526{
3527 size_t len;
3528 char *p, *str;
3529
3530 if (*type == ',')
3531 type++;
3532
3533 p = strchr(type, ':');
3534 if (!p) {
3535 *key = NULL;
3536 return NULL;
3537 }
3538 len = p - type;
3539
3540 str = qemu_malloc(len + 1);
3541 memcpy(str, type, len);
3542 str[len] = '\0';
3543
3544 *key = str;
3545 return ++p;
3546}
3547
9307c4c1
FB
3548static int default_fmt_format = 'x';
3549static int default_fmt_size = 4;
3550
3551#define MAX_ARGS 16
3552
fbc3d96c 3553static int is_valid_option(const char *c, const char *typestr)
3554{
3555 char option[3];
3556
3557 option[0] = '-';
3558 option[1] = *c;
3559 option[2] = '\0';
3560
3561 typestr = strstr(typestr, option);
3562 return (typestr != NULL);
3563}
3564
7fd669a1
LC
3565static const mon_cmd_t *monitor_find_command(const char *cmdname)
3566{
3567 const mon_cmd_t *cmd;
3568
3569 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3570 if (compare_cmd(cmdname, cmd->name)) {
3571 return cmd;
3572 }
3573 }
3574
3575 return NULL;
3576}
3577
c227f099 3578static const mon_cmd_t *monitor_parse_command(Monitor *mon,
55f81d96 3579 const char *cmdline,
55f81d96 3580 QDict *qdict)
9307c4c1 3581{
4590fd80 3582 const char *p, *typestr;
53773581 3583 int c;
c227f099 3584 const mon_cmd_t *cmd;
9307c4c1
FB
3585 char cmdname[256];
3586 char buf[1024];
4d76d2ba 3587 char *key;
9dc39cba
FB
3588
3589#ifdef DEBUG
376253ec 3590 monitor_printf(mon, "command='%s'\n", cmdline);
9dc39cba 3591#endif
3b46e624 3592
9307c4c1 3593 /* extract the command name */
4590fd80
LC
3594 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3595 if (!p)
55f81d96 3596 return NULL;
3b46e624 3597
7fd669a1
LC
3598 cmd = monitor_find_command(cmdname);
3599 if (!cmd) {
d91d9bf6 3600 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
55f81d96 3601 return NULL;
9307c4c1 3602 }
9307c4c1 3603
9307c4c1
FB
3604 /* parse the parameters */
3605 typestr = cmd->args_type;
9dc39cba 3606 for(;;) {
4d76d2ba
LC
3607 typestr = key_get_info(typestr, &key);
3608 if (!typestr)
9dc39cba 3609 break;
4d76d2ba 3610 c = *typestr;
9307c4c1
FB
3611 typestr++;
3612 switch(c) {
3613 case 'F':
81d0912d 3614 case 'B':
9307c4c1
FB
3615 case 's':
3616 {
3617 int ret;
3b46e624 3618
cd390083 3619 while (qemu_isspace(*p))
9307c4c1
FB
3620 p++;
3621 if (*typestr == '?') {
3622 typestr++;
3623 if (*p == '\0') {
3624 /* no optional string: NULL argument */
53773581 3625 break;
9307c4c1
FB
3626 }
3627 }
3628 ret = get_str(buf, sizeof(buf), &p);
3629 if (ret < 0) {
81d0912d
FB
3630 switch(c) {
3631 case 'F':
376253ec
AL
3632 monitor_printf(mon, "%s: filename expected\n",
3633 cmdname);
81d0912d
FB
3634 break;
3635 case 'B':
376253ec
AL
3636 monitor_printf(mon, "%s: block device name expected\n",
3637 cmdname);
81d0912d
FB
3638 break;
3639 default:
376253ec 3640 monitor_printf(mon, "%s: string expected\n", cmdname);
81d0912d
FB
3641 break;
3642 }
9307c4c1
FB
3643 goto fail;
3644 }
53773581 3645 qdict_put(qdict, key, qstring_from_str(buf));
9307c4c1 3646 }
9dc39cba 3647 break;
9307c4c1
FB
3648 case '/':
3649 {
3650 int count, format, size;
3b46e624 3651
cd390083 3652 while (qemu_isspace(*p))
9307c4c1
FB
3653 p++;
3654 if (*p == '/') {
3655 /* format found */
3656 p++;
3657 count = 1;
cd390083 3658 if (qemu_isdigit(*p)) {
9307c4c1 3659 count = 0;
cd390083 3660 while (qemu_isdigit(*p)) {
9307c4c1
FB
3661 count = count * 10 + (*p - '0');
3662 p++;
3663 }
3664 }
3665 size = -1;
3666 format = -1;
3667 for(;;) {
3668 switch(*p) {
3669 case 'o':
3670 case 'd':
3671 case 'u':
3672 case 'x':
3673 case 'i':
3674 case 'c':
3675 format = *p++;
3676 break;
3677 case 'b':
3678 size = 1;
3679 p++;
3680 break;
3681 case 'h':
3682 size = 2;
3683 p++;
3684 break;
3685 case 'w':
3686 size = 4;
3687 p++;
3688 break;
3689 case 'g':
3690 case 'L':
3691 size = 8;
3692 p++;
3693 break;
3694 default:
3695 goto next;
3696 }
3697 }
3698 next:
cd390083 3699 if (*p != '\0' && !qemu_isspace(*p)) {
376253ec
AL
3700 monitor_printf(mon, "invalid char in format: '%c'\n",
3701 *p);
9307c4c1
FB
3702 goto fail;
3703 }
9307c4c1
FB
3704 if (format < 0)
3705 format = default_fmt_format;
4c27ba27
FB
3706 if (format != 'i') {
3707 /* for 'i', not specifying a size gives -1 as size */
3708 if (size < 0)
3709 size = default_fmt_size;
e90f009b 3710 default_fmt_size = size;
4c27ba27 3711 }
9307c4c1
FB
3712 default_fmt_format = format;
3713 } else {
3714 count = 1;
3715 format = default_fmt_format;
4c27ba27
FB
3716 if (format != 'i') {
3717 size = default_fmt_size;
3718 } else {
3719 size = -1;
3720 }
9307c4c1 3721 }
f7188bbe
LC
3722 qdict_put(qdict, "count", qint_from_int(count));
3723 qdict_put(qdict, "format", qint_from_int(format));
3724 qdict_put(qdict, "size", qint_from_int(size));
9307c4c1 3725 }
9dc39cba 3726 break;
9307c4c1 3727 case 'i':
92a31b1f 3728 case 'l':
b6e098d7 3729 case 'M':
9307c4c1 3730 {
c2efc95d 3731 int64_t val;
7743e588 3732
cd390083 3733 while (qemu_isspace(*p))
9307c4c1 3734 p++;
3440557b 3735 if (*typestr == '?' || *typestr == '.') {
3440557b 3736 if (*typestr == '?') {
53773581
LC
3737 if (*p == '\0') {
3738 typestr++;
3739 break;
3740 }
3440557b
FB
3741 } else {
3742 if (*p == '.') {
3743 p++;
cd390083 3744 while (qemu_isspace(*p))
3440557b 3745 p++;
3440557b 3746 } else {
53773581
LC
3747 typestr++;
3748 break;
3440557b
FB
3749 }
3750 }
13224a87 3751 typestr++;
9307c4c1 3752 }
376253ec 3753 if (get_expr(mon, &val, &p))
9307c4c1 3754 goto fail;
675ebef9
LC
3755 /* Check if 'i' is greater than 32-bit */
3756 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3757 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3758 monitor_printf(mon, "integer is for 32-bit values\n");
3759 goto fail;
b6e098d7
LC
3760 } else if (c == 'M') {
3761 val <<= 20;
675ebef9 3762 }
53773581 3763 qdict_put(qdict, key, qint_from_int(val));
9307c4c1
FB
3764 }
3765 break;
3350a4dd 3766 case 'b':
fccfb11e 3767 case 'T':
3350a4dd
MA
3768 {
3769 double val;
3770
3771 while (qemu_isspace(*p))
3772 p++;
3773 if (*typestr == '?') {
3774 typestr++;
3775 if (*p == '\0') {
3776 break;
3777 }
3778 }
3779 if (get_double(mon, &val, &p) < 0) {
3780 goto fail;
3781 }
fccfb11e 3782 if (c == 'b' && *p) {
3350a4dd
MA
3783 switch (*p) {
3784 case 'K': case 'k':
3785 val *= 1 << 10; p++; break;
3786 case 'M': case 'm':
3787 val *= 1 << 20; p++; break;
3788 case 'G': case 'g':
3789 val *= 1 << 30; p++; break;
3790 }
3791 }
fccfb11e
MA
3792 if (c == 'T' && p[0] && p[1] == 's') {
3793 switch (*p) {
3794 case 'm':
3795 val /= 1e3; p += 2; break;
3796 case 'u':
3797 val /= 1e6; p += 2; break;
3798 case 'n':
3799 val /= 1e9; p += 2; break;
3800 }
3801 }
3350a4dd
MA
3802 if (*p && !qemu_isspace(*p)) {
3803 monitor_printf(mon, "Unknown unit suffix\n");
3804 goto fail;
3805 }
3806 qdict_put(qdict, key, qfloat_from_double(val));
3807 }
3808 break;
9307c4c1
FB
3809 case '-':
3810 {
fbc3d96c 3811 const char *tmp = p;
3812 int has_option, skip_key = 0;
9307c4c1 3813 /* option */
3b46e624 3814
9307c4c1
FB
3815 c = *typestr++;
3816 if (c == '\0')
3817 goto bad_type;
cd390083 3818 while (qemu_isspace(*p))
9307c4c1
FB
3819 p++;
3820 has_option = 0;
3821 if (*p == '-') {
3822 p++;
fbc3d96c 3823 if(c != *p) {
3824 if(!is_valid_option(p, typestr)) {
3825
3826 monitor_printf(mon, "%s: unsupported option -%c\n",
3827 cmdname, *p);
3828 goto fail;
3829 } else {
3830 skip_key = 1;
3831 }
3832 }
3833 if(skip_key) {
3834 p = tmp;
3835 } else {
3836 p++;
3837 has_option = 1;
9307c4c1 3838 }
9307c4c1 3839 }
f7188bbe 3840 qdict_put(qdict, key, qint_from_int(has_option));
9307c4c1
FB
3841 }
3842 break;
3843 default:
3844 bad_type:
376253ec 3845 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
9307c4c1
FB
3846 goto fail;
3847 }
4d76d2ba
LC
3848 qemu_free(key);
3849 key = NULL;
9dc39cba 3850 }
9307c4c1 3851 /* check that all arguments were parsed */
cd390083 3852 while (qemu_isspace(*p))
9307c4c1
FB
3853 p++;
3854 if (*p != '\0') {
376253ec
AL
3855 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3856 cmdname);
9307c4c1 3857 goto fail;
9dc39cba 3858 }
9307c4c1 3859
55f81d96 3860 return cmd;
ac7531ec 3861
55f81d96 3862fail:
4d76d2ba 3863 qemu_free(key);
55f81d96
LC
3864 return NULL;
3865}
3866
d6f46833
MA
3867void monitor_set_error(Monitor *mon, QError *qerror)
3868{
3869 /* report only the first error */
3870 if (!mon->error) {
3871 mon->error = qerror;
3872 } else {
3873 MON_DEBUG("Additional error report at %s:%d\n",
3874 qerror->file, qerror->linenr);
3875 QDECREF(qerror);
3876 }
3877}
3878
940cc30d
AL
3879static int is_async_return(const QObject *data)
3880{
82617d7c
LC
3881 if (data && qobject_type(data) == QTYPE_QDICT) {
3882 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3883 }
3884
3885 return 0;
940cc30d
AL
3886}
3887
bb89c2e9
LC
3888static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3889{
cde0fc75
MA
3890 if (monitor_ctrl_mode(mon)) {
3891 if (ret && !monitor_has_error(mon)) {
3892 /*
3893 * If it returns failure, it must have passed on error.
3894 *
3895 * Action: Report an internal error to the client if in QMP.
3896 */
ab5b027e 3897 qerror_report(QERR_UNDEFINED_ERROR);
cde0fc75
MA
3898 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3899 cmd->name);
bb89c2e9 3900 }
bb89c2e9
LC
3901
3902#ifdef CONFIG_DEBUG_MONITOR
cde0fc75
MA
3903 if (!ret && monitor_has_error(mon)) {
3904 /*
3905 * If it returns success, it must not have passed an error.
3906 *
3907 * Action: Report the passed error to the client.
3908 */
3909 MON_DEBUG("command '%s' returned success but passed an error\n",
3910 cmd->name);
3911 }
10e4f606 3912
cde0fc75
MA
3913 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3914 /*
3915 * Handlers should not call Monitor print functions.
3916 *
3917 * Action: Ignore them in QMP.
3918 *
3919 * (XXX: we don't check any 'info' or 'query' command here
3920 * because the user print function _is_ called by do_info(), hence
3921 * we will trigger this check. This problem will go away when we
3922 * make 'query' commands real and kill do_info())
3923 */
3924 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3925 cmd->name, mon_print_count_get(mon));
3926 }
bb89c2e9 3927#endif
cde0fc75
MA
3928 } else {
3929 assert(!monitor_has_error(mon));
3930 QDECREF(mon->error);
3931 mon->error = NULL;
3932 }
bb89c2e9
LC
3933}
3934
99e2fc16
LC
3935static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3936 const QDict *params)
3937{
bb89c2e9 3938 int ret;
99e2fc16
LC
3939 QObject *data = NULL;
3940
10e4f606
LC
3941 mon_print_count_init(mon);
3942
bb89c2e9
LC
3943 ret = cmd->mhandler.cmd_new(mon, params, &data);
3944 handler_audit(mon, cmd, ret);
25b422eb 3945
940cc30d
AL
3946 if (is_async_return(data)) {
3947 /*
3948 * Asynchronous commands have no initial return data but they can
3949 * generate errors. Data is returned via the async completion handler.
3950 */
3951 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3952 monitor_protocol_emitter(mon, NULL);
3953 }
3954 } else if (monitor_ctrl_mode(mon)) {
25b422eb
LC
3955 /* Monitor Protocol */
3956 monitor_protocol_emitter(mon, data);
3957 } else {
3958 /* User Protocol */
3959 if (data)
3960 cmd->user_print(mon, data);
3961 }
99e2fc16
LC
3962
3963 qobject_decref(data);
3964}
3965
f3c157c4 3966static void handle_user_command(Monitor *mon, const char *cmdline)
55f81d96 3967{
55f81d96 3968 QDict *qdict;
c227f099 3969 const mon_cmd_t *cmd;
55f81d96
LC
3970
3971 qdict = qdict_new();
3972
590fb3b7 3973 cmd = monitor_parse_command(mon, cmdline, qdict);
13917bee
LC
3974 if (!cmd)
3975 goto out;
3976
940cc30d
AL
3977 if (monitor_handler_is_async(cmd)) {
3978 user_async_cmd_handler(mon, cmd, qdict);
3979 } else if (monitor_handler_ported(cmd)) {
99e2fc16 3980 monitor_call_handler(mon, cmd, qdict);
13917bee 3981 } else {
af4ce882 3982 cmd->mhandler.cmd(mon, qdict);
55f81d96
LC
3983 }
3984
13917bee 3985out:
f7188bbe 3986 QDECREF(qdict);
9dc39cba
FB
3987}
3988
81d0912d
FB
3989static void cmd_completion(const char *name, const char *list)
3990{
3991 const char *p, *pstart;
3992 char cmd[128];
3993 int len;
3994
3995 p = list;
3996 for(;;) {
3997 pstart = p;
3998 p = strchr(p, '|');
3999 if (!p)
4000 p = pstart + strlen(pstart);
4001 len = p - pstart;
4002 if (len > sizeof(cmd) - 2)
4003 len = sizeof(cmd) - 2;
4004 memcpy(cmd, pstart, len);
4005 cmd[len] = '\0';
4006 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
731b0364 4007 readline_add_completion(cur_mon->rs, cmd);
81d0912d
FB
4008 }
4009 if (*p == '\0')
4010 break;
4011 p++;
4012 }
4013}
4014
4015static void file_completion(const char *input)
4016{
4017 DIR *ffs;
4018 struct dirent *d;
4019 char path[1024];
4020 char file[1024], file_prefix[1024];
4021 int input_path_len;
4022 const char *p;
4023
5fafdf24 4024 p = strrchr(input, '/');
81d0912d
FB
4025 if (!p) {
4026 input_path_len = 0;
4027 pstrcpy(file_prefix, sizeof(file_prefix), input);
363a37d5 4028 pstrcpy(path, sizeof(path), ".");
81d0912d
FB
4029 } else {
4030 input_path_len = p - input + 1;
4031 memcpy(path, input, input_path_len);
4032 if (input_path_len > sizeof(path) - 1)
4033 input_path_len = sizeof(path) - 1;
4034 path[input_path_len] = '\0';
4035 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4036 }
4037#ifdef DEBUG_COMPLETION
376253ec
AL
4038 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4039 input, path, file_prefix);
81d0912d
FB
4040#endif
4041 ffs = opendir(path);
4042 if (!ffs)
4043 return;
4044 for(;;) {
4045 struct stat sb;
4046 d = readdir(ffs);
4047 if (!d)
4048 break;
4049 if (strstart(d->d_name, file_prefix, NULL)) {
4050 memcpy(file, input, input_path_len);
363a37d5
BS
4051 if (input_path_len < sizeof(file))
4052 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4053 d->d_name);
81d0912d
FB
4054 /* stat the file to find out if it's a directory.
4055 * In that case add a slash to speed up typing long paths
4056 */
4057 stat(file, &sb);
4058 if(S_ISDIR(sb.st_mode))
363a37d5 4059 pstrcat(file, sizeof(file), "/");
731b0364 4060 readline_add_completion(cur_mon->rs, file);
81d0912d
FB
4061 }
4062 }
4063 closedir(ffs);
4064}
4065
51de9760 4066static void block_completion_it(void *opaque, BlockDriverState *bs)
81d0912d 4067{
51de9760 4068 const char *name = bdrv_get_device_name(bs);
81d0912d
FB
4069 const char *input = opaque;
4070
4071 if (input[0] == '\0' ||
4072 !strncmp(name, (char *)input, strlen(input))) {
731b0364 4073 readline_add_completion(cur_mon->rs, name);
81d0912d
FB
4074 }
4075}
4076
4077/* NOTE: this parser is an approximate form of the real command parser */
4078static void parse_cmdline(const char *cmdline,
4079 int *pnb_args, char **args)
4080{
4081 const char *p;
4082 int nb_args, ret;
4083 char buf[1024];
4084
4085 p = cmdline;
4086 nb_args = 0;
4087 for(;;) {
cd390083 4088 while (qemu_isspace(*p))
81d0912d
FB
4089 p++;
4090 if (*p == '\0')
4091 break;
4092 if (nb_args >= MAX_ARGS)
4093 break;
4094 ret = get_str(buf, sizeof(buf), &p);
4095 args[nb_args] = qemu_strdup(buf);
4096 nb_args++;
4097 if (ret < 0)
4098 break;
4099 }
4100 *pnb_args = nb_args;
4101}
4102
4d76d2ba
LC
4103static const char *next_arg_type(const char *typestr)
4104{
4105 const char *p = strchr(typestr, ':');
4106 return (p != NULL ? ++p : typestr);
4107}
4108
4c36ba32 4109static void monitor_find_completion(const char *cmdline)
81d0912d
FB
4110{
4111 const char *cmdname;
4112 char *args[MAX_ARGS];
4113 int nb_args, i, len;
4114 const char *ptype, *str;
c227f099 4115 const mon_cmd_t *cmd;
64866c3d 4116 const KeyDef *key;
81d0912d
FB
4117
4118 parse_cmdline(cmdline, &nb_args, args);
4119#ifdef DEBUG_COMPLETION
4120 for(i = 0; i < nb_args; i++) {
376253ec 4121 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
81d0912d
FB
4122 }
4123#endif
4124
4125 /* if the line ends with a space, it means we want to complete the
4126 next arg */
4127 len = strlen(cmdline);
cd390083 4128 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
81d0912d
FB
4129 if (nb_args >= MAX_ARGS)
4130 return;
4131 args[nb_args++] = qemu_strdup("");
4132 }
4133 if (nb_args <= 1) {
4134 /* command completion */
4135 if (nb_args == 0)
4136 cmdname = "";
4137 else
4138 cmdname = args[0];
731b0364 4139 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
376253ec 4140 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
81d0912d
FB
4141 cmd_completion(cmdname, cmd->name);
4142 }
4143 } else {
4144 /* find the command */
376253ec 4145 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
81d0912d
FB
4146 if (compare_cmd(args[0], cmd->name))
4147 goto found;
4148 }
4149 return;
4150 found:
4d76d2ba 4151 ptype = next_arg_type(cmd->args_type);
81d0912d
FB
4152 for(i = 0; i < nb_args - 2; i++) {
4153 if (*ptype != '\0') {
4d76d2ba 4154 ptype = next_arg_type(ptype);
81d0912d 4155 while (*ptype == '?')
4d76d2ba 4156 ptype = next_arg_type(ptype);
81d0912d
FB
4157 }
4158 }
4159 str = args[nb_args - 1];
2a1704a7
BS
4160 if (*ptype == '-' && ptype[1] != '\0') {
4161 ptype += 2;
4162 }
81d0912d
FB
4163 switch(*ptype) {
4164 case 'F':
4165 /* file completion */
731b0364 4166 readline_set_completion_index(cur_mon->rs, strlen(str));
81d0912d
FB
4167 file_completion(str);
4168 break;
4169 case 'B':
4170 /* block device name completion */
731b0364 4171 readline_set_completion_index(cur_mon->rs, strlen(str));
81d0912d
FB
4172 bdrv_iterate(block_completion_it, (void *)str);
4173 break;
7fe48483
FB
4174 case 's':
4175 /* XXX: more generic ? */
4176 if (!strcmp(cmd->name, "info")) {
731b0364 4177 readline_set_completion_index(cur_mon->rs, strlen(str));
7fe48483
FB
4178 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4179 cmd_completion(str, cmd->name);
4180 }
64866c3d 4181 } else if (!strcmp(cmd->name, "sendkey")) {
e600d1ef
BS
4182 char *sep = strrchr(str, '-');
4183 if (sep)
4184 str = sep + 1;
731b0364 4185 readline_set_completion_index(cur_mon->rs, strlen(str));
64866c3d
FB
4186 for(key = key_defs; key->name != NULL; key++) {
4187 cmd_completion(str, key->name);
4188 }
f3353c6b
JK
4189 } else if (!strcmp(cmd->name, "help|?")) {
4190 readline_set_completion_index(cur_mon->rs, strlen(str));
4191 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4192 cmd_completion(str, cmd->name);
4193 }
7fe48483
FB
4194 }
4195 break;
81d0912d
FB
4196 default:
4197 break;
4198 }
4199 }
4200 for(i = 0; i < nb_args; i++)
4201 qemu_free(args[i]);
4202}
4203
731b0364 4204static int monitor_can_read(void *opaque)
9dc39cba 4205{
731b0364
AL
4206 Monitor *mon = opaque;
4207
c62313bb 4208 return (mon->suspend_cnt == 0) ? 1 : 0;
9dc39cba
FB
4209}
4210
5fa737a4
LC
4211typedef struct CmdArgs {
4212 QString *name;
4213 int type;
4214 int flag;
4215 int optional;
4216} CmdArgs;
4217
4218static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4219{
4220 if (!cmd_args->optional) {
ab5b027e 4221 qerror_report(QERR_MISSING_PARAMETER, name);
5fa737a4
LC
4222 return -1;
4223 }
4224
4225 if (cmd_args->type == '-') {
4226 /* handlers expect a value, they need to be changed */
4227 qdict_put(args, name, qint_from_int(0));
4228 }
4229
4230 return 0;
4231}
4232
4233static int check_arg(const CmdArgs *cmd_args, QDict *args)
4234{
4235 QObject *value;
4236 const char *name;
4237
4238 name = qstring_get_str(cmd_args->name);
4239
4240 if (!args) {
4241 return check_opt(cmd_args, name, args);
4242 }
4243
4244 value = qdict_get(args, name);
4245 if (!value) {
4246 return check_opt(cmd_args, name, args);
4247 }
4248
4249 switch (cmd_args->type) {
4250 case 'F':
4251 case 'B':
4252 case 's':
4253 if (qobject_type(value) != QTYPE_QSTRING) {
ab5b027e 4254 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "string");
5fa737a4
LC
4255 return -1;
4256 }
4257 break;
4258 case '/': {
4259 int i;
4260 const char *keys[] = { "count", "format", "size", NULL };
4261
4262 for (i = 0; keys[i]; i++) {
4263 QObject *obj = qdict_get(args, keys[i]);
4264 if (!obj) {
ab5b027e 4265 qerror_report(QERR_MISSING_PARAMETER, name);
5fa737a4
LC
4266 return -1;
4267 }
4268 if (qobject_type(obj) != QTYPE_QINT) {
ab5b027e 4269 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
5fa737a4
LC
4270 return -1;
4271 }
4272 }
4273 break;
4274 }
4275 case 'i':
4276 case 'l':
b6e098d7 4277 case 'M':
5fa737a4 4278 if (qobject_type(value) != QTYPE_QINT) {
ab5b027e 4279 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
5fa737a4
LC
4280 return -1;
4281 }
4282 break;
3350a4dd 4283 case 'b':
fccfb11e 4284 case 'T':
3350a4dd 4285 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
ab5b027e 4286 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "number");
3350a4dd
MA
4287 return -1;
4288 }
4289 break;
5fa737a4
LC
4290 case '-':
4291 if (qobject_type(value) != QTYPE_QINT &&
4292 qobject_type(value) != QTYPE_QBOOL) {
ab5b027e 4293 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "bool");
5fa737a4
LC
4294 return -1;
4295 }
4296 if (qobject_type(value) == QTYPE_QBOOL) {
4297 /* handlers expect a QInt, they need to be changed */
4298 qdict_put(args, name,
4299 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4300 }
4301 break;
4302 default:
4303 /* impossible */
4304 abort();
4305 }
4306
4307 return 0;
4308}
4309
4310static void cmd_args_init(CmdArgs *cmd_args)
4311{
4312 cmd_args->name = qstring_new();
4313 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4314}
4315
4316/*
4317 * This is not trivial, we have to parse Monitor command's argument
4318 * type syntax to be able to check the arguments provided by clients.
4319 *
4320 * In the near future we will be using an array for that and will be
4321 * able to drop all this parsing...
4322 */
4323static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4324{
4325 int err;
4326 const char *p;
4327 CmdArgs cmd_args;
4328
dd5121bd 4329 if (cmd->args_type == NULL) {
5fa737a4
LC
4330 return (qdict_size(args) == 0 ? 0 : -1);
4331 }
4332
4333 err = 0;
4334 cmd_args_init(&cmd_args);
4335
4336 for (p = cmd->args_type;; p++) {
4337 if (*p == ':') {
4338 cmd_args.type = *++p;
4339 p++;
4340 if (cmd_args.type == '-') {
4341 cmd_args.flag = *p++;
4342 cmd_args.optional = 1;
4343 } else if (*p == '?') {
4344 cmd_args.optional = 1;
4345 p++;
4346 }
4347
4348 assert(*p == ',' || *p == '\0');
4349 err = check_arg(&cmd_args, args);
4350
4351 QDECREF(cmd_args.name);
4352 cmd_args_init(&cmd_args);
4353
4354 if (err < 0) {
4355 break;
4356 }
4357 } else {
4358 qstring_append_chr(cmd_args.name, *p);
4359 }
4360
4361 if (*p == '\0') {
4362 break;
4363 }
4364 }
4365
4366 QDECREF(cmd_args.name);
4367 return err;
4368}
4369
09069b19
LC
4370static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4371{
4372 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4373 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4374}
4375
5fa737a4
LC
4376static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4377{
4378 int err;
4379 QObject *obj;
4380 QDict *input, *args;
5fa737a4
LC
4381 const mon_cmd_t *cmd;
4382 Monitor *mon = cur_mon;
5e23f480 4383 const char *cmd_name, *info_item;
5fa737a4
LC
4384
4385 args = NULL;
5fa737a4
LC
4386
4387 obj = json_parser_parse(tokens, NULL);
4388 if (!obj) {
4389 // FIXME: should be triggered in json_parser_parse()
ab5b027e 4390 qerror_report(QERR_JSON_PARSING);
5fa737a4
LC
4391 goto err_out;
4392 } else if (qobject_type(obj) != QTYPE_QDICT) {
ab5b027e 4393 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
5fa737a4
LC
4394 qobject_decref(obj);
4395 goto err_out;
4396 }
4397
4398 input = qobject_to_qdict(obj);
4399
4400 mon->mc->id = qdict_get(input, "id");
4401 qobject_incref(mon->mc->id);
4402
4403 obj = qdict_get(input, "execute");
4404 if (!obj) {
ab5b027e 4405 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
5fa737a4
LC
4406 goto err_input;
4407 } else if (qobject_type(obj) != QTYPE_QSTRING) {
ab5b027e 4408 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "string");
5fa737a4
LC
4409 goto err_input;
4410 }
4411
4412 cmd_name = qstring_get_str(qobject_to_qstring(obj));
5e23f480 4413
09069b19 4414 if (invalid_qmp_mode(mon, cmd_name)) {
ab5b027e 4415 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
09069b19
LC
4416 goto err_input;
4417 }
4418
5e23f480
LC
4419 /*
4420 * XXX: We need this special case until we get info handlers
4421 * converted into 'query-' commands
4422 */
4423 if (compare_cmd(cmd_name, "info")) {
ab5b027e 4424 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
5fa737a4 4425 goto err_input;
5e23f480
LC
4426 } else if (strstart(cmd_name, "query-", &info_item)) {
4427 cmd = monitor_find_command("info");
4428 qdict_put_obj(input, "arguments",
4429 qobject_from_jsonf("{ 'item': %s }", info_item));
4430 } else {
4431 cmd = monitor_find_command(cmd_name);
43e713ce 4432 if (!cmd || !monitor_handler_ported(cmd)) {
ab5b027e 4433 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
5e23f480
LC
4434 goto err_input;
4435 }
5fa737a4
LC
4436 }
4437
4438 obj = qdict_get(input, "arguments");
4439 if (!obj) {
4440 args = qdict_new();
4441 } else {
4442 args = qobject_to_qdict(obj);
4443 QINCREF(args);
4444 }
4445
4446 QDECREF(input);
4447
4448 err = monitor_check_qmp_args(cmd, args);
4449 if (err < 0) {
4450 goto err_out;
4451 }
4452
940cc30d
AL
4453 if (monitor_handler_is_async(cmd)) {
4454 qmp_async_cmd_handler(mon, cmd, args);
4455 } else {
4456 monitor_call_handler(mon, cmd, args);
4457 }
5fa737a4
LC
4458 goto out;
4459
4460err_input:
4461 QDECREF(input);
4462err_out:
4463 monitor_protocol_emitter(mon, NULL);
4464out:
4465 QDECREF(args);
5fa737a4
LC
4466}
4467
9b57c02e
LC
4468/**
4469 * monitor_control_read(): Read and handle QMP input
4470 */
4471static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4472{
4473 Monitor *old_mon = cur_mon;
4474
4475 cur_mon = opaque;
4476
5fa737a4 4477 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
9b57c02e
LC
4478
4479 cur_mon = old_mon;
4480}
4481
731b0364 4482static void monitor_read(void *opaque, const uint8_t *buf, int size)
9dc39cba 4483{
731b0364 4484 Monitor *old_mon = cur_mon;
7e2515e8 4485 int i;
376253ec 4486
731b0364
AL
4487 cur_mon = opaque;
4488
cde76ee1
AL
4489 if (cur_mon->rs) {
4490 for (i = 0; i < size; i++)
4491 readline_handle_byte(cur_mon->rs, buf[i]);
4492 } else {
4493 if (size == 0 || buf[size - 1] != 0)
4494 monitor_printf(cur_mon, "corrupted command\n");
4495 else
f3c157c4 4496 handle_user_command(cur_mon, (char *)buf);
cde76ee1 4497 }
9dc39cba 4498
731b0364
AL
4499 cur_mon = old_mon;
4500}
d8f44609 4501
376253ec 4502static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
aa455485 4503{
731b0364 4504 monitor_suspend(mon);
f3c157c4 4505 handle_user_command(mon, cmdline);
731b0364 4506 monitor_resume(mon);
d8f44609
AL
4507}
4508
cde76ee1 4509int monitor_suspend(Monitor *mon)
d8f44609 4510{
cde76ee1
AL
4511 if (!mon->rs)
4512 return -ENOTTY;
731b0364 4513 mon->suspend_cnt++;
cde76ee1 4514 return 0;
d8f44609
AL
4515}
4516
376253ec 4517void monitor_resume(Monitor *mon)
d8f44609 4518{
cde76ee1
AL
4519 if (!mon->rs)
4520 return;
731b0364
AL
4521 if (--mon->suspend_cnt == 0)
4522 readline_show_prompt(mon->rs);
aa455485
FB
4523}
4524
ca9567e2
LC
4525static QObject *get_qmp_greeting(void)
4526{
4527 QObject *ver;
4528
4529 do_info_version(NULL, &ver);
4530 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4531}
4532
9b57c02e
LC
4533/**
4534 * monitor_control_event(): Print QMP gretting
4535 */
4536static void monitor_control_event(void *opaque, int event)
4537{
47116d1c
LC
4538 QObject *data;
4539 Monitor *mon = opaque;
9b57c02e 4540
47116d1c
LC
4541 switch (event) {
4542 case CHR_EVENT_OPENED:
4a7e1190 4543 mon->mc->command_mode = 0;
5fa737a4 4544 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
ca9567e2 4545 data = get_qmp_greeting();
9b57c02e
LC
4546 monitor_json_emitter(mon, data);
4547 qobject_decref(data);
47116d1c
LC
4548 break;
4549 case CHR_EVENT_CLOSED:
4550 json_message_parser_destroy(&mon->mc->parser);
4551 break;
9b57c02e
LC
4552 }
4553}
4554
731b0364 4555static void monitor_event(void *opaque, int event)
86e94dea 4556{
376253ec
AL
4557 Monitor *mon = opaque;
4558
2724b180
AL
4559 switch (event) {
4560 case CHR_EVENT_MUX_IN:
a7aec5da
GH
4561 mon->mux_out = 0;
4562 if (mon->reset_seen) {
4563 readline_restart(mon->rs);
4564 monitor_resume(mon);
4565 monitor_flush(mon);
4566 } else {
4567 mon->suspend_cnt = 0;
4568 }
2724b180
AL
4569 break;
4570
4571 case CHR_EVENT_MUX_OUT:
a7aec5da
GH
4572 if (mon->reset_seen) {
4573 if (mon->suspend_cnt == 0) {
4574 monitor_printf(mon, "\n");
4575 }
4576 monitor_flush(mon);
4577 monitor_suspend(mon);
4578 } else {
4579 mon->suspend_cnt++;
4580 }
4581 mon->mux_out = 1;
2724b180 4582 break;
86e94dea 4583
b6b8df56 4584 case CHR_EVENT_OPENED:
2724b180
AL
4585 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4586 "information\n", QEMU_VERSION);
a7aec5da 4587 if (!mon->mux_out) {
2724b180 4588 readline_show_prompt(mon->rs);
a7aec5da
GH
4589 }
4590 mon->reset_seen = 1;
2724b180
AL
4591 break;
4592 }
86e94dea
TS
4593}
4594
76655d6d
AL
4595
4596/*
4597 * Local variables:
4598 * c-indent-level: 4
4599 * c-basic-offset: 4
4600 * tab-width: 8
4601 * End:
4602 */
4603
731b0364 4604void monitor_init(CharDriverState *chr, int flags)
aa455485 4605{
731b0364 4606 static int is_first_init = 1;
87127161 4607 Monitor *mon;
20d8a3ed
TS
4608
4609 if (is_first_init) {
c8256f9d 4610 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
20d8a3ed
TS
4611 is_first_init = 0;
4612 }
87127161
AL
4613
4614 mon = qemu_mallocz(sizeof(*mon));
20d8a3ed 4615
87127161 4616 mon->chr = chr;
731b0364 4617 mon->flags = flags;
cde76ee1
AL
4618 if (flags & MONITOR_USE_READLINE) {
4619 mon->rs = readline_init(mon, monitor_find_completion);
4620 monitor_read_command(mon, 0);
4621 }
87127161 4622
9b57c02e 4623 if (monitor_ctrl_mode(mon)) {
5fa737a4 4624 mon->mc = qemu_mallocz(sizeof(MonitorControl));
9b57c02e
LC
4625 /* Control mode requires special handlers */
4626 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4627 monitor_control_event, mon);
4628 } else {
4629 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4630 monitor_event, mon);
4631 }
87127161 4632
72cf2d4f 4633 QLIST_INSERT_HEAD(&mon_list, mon, entry);
8631b608
MA
4634 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4635 default_mon = mon;
aa455485
FB
4636}
4637
376253ec 4638static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
81d0912d 4639{
bb5fc20f
AL
4640 BlockDriverState *bs = opaque;
4641 int ret = 0;
81d0912d 4642
bb5fc20f 4643 if (bdrv_set_key(bs, password) != 0) {
376253ec 4644 monitor_printf(mon, "invalid password\n");
bb5fc20f 4645 ret = -EPERM;
9dc39cba 4646 }
731b0364
AL
4647 if (mon->password_completion_cb)
4648 mon->password_completion_cb(mon->password_opaque, ret);
bb5fc20f 4649
731b0364 4650 monitor_read_command(mon, 1);
9dc39cba 4651}
c0f4ce77 4652
0bbc47bb
LC
4653int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4654 BlockDriverCompletionFunc *completion_cb,
4655 void *opaque)
c0f4ce77 4656{
cde76ee1
AL
4657 int err;
4658
bb5fc20f
AL
4659 if (!bdrv_key_required(bs)) {
4660 if (completion_cb)
4661 completion_cb(opaque, 0);
0bbc47bb 4662 return 0;
bb5fc20f 4663 }
c0f4ce77 4664
94171e11 4665 if (monitor_ctrl_mode(mon)) {
ab5b027e 4666 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
0bbc47bb 4667 return -1;
94171e11
LC
4668 }
4669
376253ec
AL
4670 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4671 bdrv_get_encrypted_filename(bs));
bb5fc20f 4672
731b0364
AL
4673 mon->password_completion_cb = completion_cb;
4674 mon->password_opaque = opaque;
bb5fc20f 4675
cde76ee1
AL
4676 err = monitor_read_password(mon, bdrv_password_cb, bs);
4677
4678 if (err && completion_cb)
4679 completion_cb(opaque, err);
0bbc47bb
LC
4680
4681 return err;
c0f4ce77 4682}