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