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