blob: 7959504595addab2fc8b1fb3d7f0a0f112680b9b [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * 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 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
37#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000038#include "monitor.h"
39#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020041#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000042#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000043#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000045#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000046#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000047#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000048#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030049#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010050#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030051#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020052#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030053#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020054#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020055#include "json-streamer.h"
56#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000057#include "osdep.h"
Paolo Bonzinia88790a2010-06-29 09:58:51 +020058#include "exec-all.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053059#ifdef CONFIG_SIMPLE_TRACE
60#include "trace.h"
61#endif
ths6a5bd302007-12-03 17:05:38 +000062
bellard9dc39cb2004-03-14 21:38:27 +000063//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000064//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000065
bellard9307c4c2004-04-04 12:57:25 +000066/*
67 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000068 *
bellard9307c4c2004-04-04 12:57:25 +000069 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000070 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000071 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010072 * 'O' option string of the form NAME=VALUE,...
73 * parsed according to QemuOptsList given by its name
74 * Example: 'device:O' uses qemu_device_opts.
75 * Restriction: only lists with empty desc are supported
76 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000077 * 'i' 32 bit integer
78 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010079 * 'M' just like 'l', except in user mode the value is
80 * multiplied by 2^20 (think Mebibyte)
Markus Armbrusteree9545d2010-03-26 09:07:08 +010081 * 'f' double
Markus Armbruster3350a4d2010-01-25 14:23:03 +010082 * user mode accepts an optional G, g, M, m, K, k suffix,
83 * which multiplies the value by 2^30 for suffixes G and
84 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010085 * 'T' double
86 * user mode accepts an optional ms, us, ns suffix,
87 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000088 * '/' optional gdb-like print format (like "/10x")
89 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030090 * '?' optional type (for all types, except '/')
91 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +010092 * 'b' boolean
93 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -030094 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000095 *
96 */
97
Adam Litke940cc302010-01-25 12:18:44 -060098typedef struct MonitorCompletionData MonitorCompletionData;
99struct MonitorCompletionData {
100 Monitor *mon;
101 void (*user_print)(Monitor *mon, const QObject *data);
102};
103
Anthony Liguoric227f092009-10-01 16:12:16 -0500104typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000105 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000106 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000107 const char *params;
108 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300109 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300110 union {
111 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300112 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600113 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300114 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200115 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600116 int (*cmd_async)(Monitor *mon, const QDict *params,
117 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300118 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200119 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500120} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000121
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100122/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500123typedef struct mon_fd_t mon_fd_t;
124struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100125 char *name;
126 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500127 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100128};
129
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200130typedef struct MonitorControl {
131 QObject *id;
132 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200133 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200134} MonitorControl;
135
aliguori87127162009-03-05 23:01:29 +0000136struct Monitor {
137 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200138 int mux_out;
139 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000140 int flags;
141 int suspend_cnt;
142 uint8_t outbuf[1024];
143 int outbuf_index;
144 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200145 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000146 CPUState *mon_cpu;
147 BlockDriverCompletionFunc *password_completion_cb;
148 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200149#ifdef CONFIG_DEBUG_MONITOR
150 int print_calls_nr;
151#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200152 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500153 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000154 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000155};
156
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200157#ifdef CONFIG_DEBUG_MONITOR
158#define MON_DEBUG(fmt, ...) do { \
159 fprintf(stderr, "Monitor: "); \
160 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200161
162static inline void mon_print_count_inc(Monitor *mon)
163{
164 mon->print_calls_nr++;
165}
166
167static inline void mon_print_count_init(Monitor *mon)
168{
169 mon->print_calls_nr = 0;
170}
171
172static inline int mon_print_count_get(const Monitor *mon)
173{
174 return mon->print_calls_nr;
175}
176
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200177#else /* !CONFIG_DEBUG_MONITOR */
178#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200179static inline void mon_print_count_inc(Monitor *mon) { }
180static inline void mon_print_count_init(Monitor *mon) { }
181static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200182#endif /* CONFIG_DEBUG_MONITOR */
183
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300184/* QMP checker flags */
185#define QMP_ACCEPT_UNKNOWNS 1
186
Blue Swirl72cf2d42009-09-12 07:36:22 +0000187static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000188
Anthony Liguoric227f092009-10-01 16:12:16 -0500189static const mon_cmd_t mon_cmds[];
190static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000191
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300192static const mon_cmd_t qmp_cmds[];
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300193static const mon_cmd_t qmp_query_cmds[];
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300194
Markus Armbruster8631b602010-02-18 11:41:55 +0100195Monitor *cur_mon;
196Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000197
aliguori731b0362009-03-05 23:01:42 +0000198static void monitor_command_cb(Monitor *mon, const char *cmdline,
199 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000200
Luiz Capitulino09069b12010-02-04 18:10:06 -0200201static inline int qmp_cmd_mode(const Monitor *mon)
202{
203 return (mon->mc ? mon->mc->command_mode : 0);
204}
205
Luiz Capitulino418173c2009-11-26 22:58:51 -0200206/* Return true if in control mode, false otherwise */
207static inline int monitor_ctrl_mode(const Monitor *mon)
208{
209 return (mon->flags & MONITOR_USE_CONTROL);
210}
211
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100212/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
213int monitor_cur_is_qmp(void)
214{
215 return cur_mon && monitor_ctrl_mode(cur_mon);
216}
217
aliguori731b0362009-03-05 23:01:42 +0000218static void monitor_read_command(Monitor *mon, int show_prompt)
219{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200220 if (!mon->rs)
221 return;
222
aliguori731b0362009-03-05 23:01:42 +0000223 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
224 if (show_prompt)
225 readline_show_prompt(mon->rs);
226}
bellard6a00d602005-11-21 23:25:50 +0000227
aliguoricde76ee2009-03-05 23:01:51 +0000228static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
229 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000230{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100231 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100232 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100233 return -EINVAL;
234 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000235 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
236 /* prompt is printed on return from the command handler */
237 return 0;
238 } else {
239 monitor_printf(mon, "terminal does not support password prompting\n");
240 return -ENOTTY;
241 }
aliguoribb5fc202009-03-05 23:01:15 +0000242}
243
aliguori376253e2009-03-05 23:01:23 +0000244void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000245{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200246 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000247 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
248 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000249 }
250}
251
252/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000253static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000254{
ths60fe76f2007-12-16 03:02:09 +0000255 char c;
aliguori731b0362009-03-05 23:01:42 +0000256
bellard7e2515e2004-08-01 21:52:19 +0000257 for(;;) {
258 c = *str++;
259 if (c == '\0')
260 break;
bellard7ba12602006-07-14 20:26:42 +0000261 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000262 mon->outbuf[mon->outbuf_index++] = '\r';
263 mon->outbuf[mon->outbuf_index++] = c;
264 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
265 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000266 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000267 }
268}
269
aliguori376253e2009-03-05 23:01:23 +0000270void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000271{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200272 char buf[4096];
273
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200274 if (!mon)
275 return;
276
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200277 mon_print_count_inc(mon);
278
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200279 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200280 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200281 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200282
283 vsnprintf(buf, sizeof(buf), fmt, ap);
284 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000285}
286
aliguori376253e2009-03-05 23:01:23 +0000287void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000288{
289 va_list ap;
290 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000291 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000292 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000293}
294
aliguori376253e2009-03-05 23:01:23 +0000295void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000296{
297 int i;
298
299 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000300 switch (filename[i]) {
301 case ' ':
302 case '"':
303 case '\\':
304 monitor_printf(mon, "\\%c", filename[i]);
305 break;
306 case '\t':
307 monitor_printf(mon, "\\t");
308 break;
309 case '\r':
310 monitor_printf(mon, "\\r");
311 break;
312 case '\n':
313 monitor_printf(mon, "\\n");
314 break;
315 default:
316 monitor_printf(mon, "%c", filename[i]);
317 break;
318 }
thsfef30742006-12-22 14:11:32 +0000319 }
320}
321
bellard7fe48482004-10-09 18:08:01 +0000322static int monitor_fprintf(FILE *stream, const char *fmt, ...)
323{
324 va_list ap;
325 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000326 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000327 va_end(ap);
328 return 0;
329}
330
Luiz Capitulino13c74252009-10-07 13:41:55 -0300331static void monitor_user_noop(Monitor *mon, const QObject *data) { }
332
Luiz Capitulino9e807212010-09-16 10:58:59 -0300333static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300334{
335 return cmd->user_print != NULL;
336}
337
Luiz Capitulino4903de02010-09-16 11:01:32 -0300338static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600339{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200340 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600341}
342
Luiz Capitulino8204a912009-11-18 23:05:31 -0200343static inline int monitor_has_error(const Monitor *mon)
344{
345 return mon->error != NULL;
346}
347
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200348static void monitor_json_emitter(Monitor *mon, const QObject *data)
349{
350 QString *json;
351
Daniel P. Berrange39eaab92010-06-07 15:42:31 +0100352 if (mon->flags & MONITOR_USE_PRETTY)
353 json = qobject_to_json_pretty(data);
354 else
355 json = qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200356 assert(json != NULL);
357
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 qstring_append_chr(json, '\n');
359 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200360
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200361 QDECREF(json);
362}
363
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200364static void monitor_protocol_emitter(Monitor *mon, QObject *data)
365{
366 QDict *qmp;
367
368 qmp = qdict_new();
369
370 if (!monitor_has_error(mon)) {
371 /* success response */
372 if (data) {
373 qobject_incref(data);
374 qdict_put_obj(qmp, "return", data);
375 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200376 /* return an empty QDict by default */
377 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200378 }
379 } else {
380 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100381 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200382 qdict_put(qmp, "error", mon->error->error);
383 QINCREF(mon->error->error);
384 QDECREF(mon->error);
385 mon->error = NULL;
386 }
387
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200388 if (mon->mc->id) {
389 qdict_put_obj(qmp, "id", mon->mc->id);
390 mon->mc->id = NULL;
391 }
392
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200393 monitor_json_emitter(mon, QOBJECT(qmp));
394 QDECREF(qmp);
395}
396
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200397static void timestamp_put(QDict *qdict)
398{
399 int err;
400 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000401 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200402
Blue Swirld08d6f02009-12-04 18:06:39 +0000403 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200404 if (err < 0)
405 return;
406
407 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
408 "'microseconds': %" PRId64 " }",
409 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410 qdict_put_obj(qdict, "timestamp", obj);
411}
412
413/**
414 * monitor_protocol_event(): Generate a Monitor event
415 *
416 * Event-specific data can be emitted through the (optional) 'data' parameter.
417 */
418void monitor_protocol_event(MonitorEvent event, QObject *data)
419{
420 QDict *qmp;
421 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600422 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200423
Blue Swirl242cd002009-12-04 18:05:45 +0000424 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200426 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000427 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200428 event_name = "SHUTDOWN";
429 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000430 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200431 event_name = "RESET";
432 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000433 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200434 event_name = "POWERDOWN";
435 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000436 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200437 event_name = "STOP";
438 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300439 case QEVENT_RESUME:
440 event_name = "RESUME";
441 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200442 case QEVENT_VNC_CONNECTED:
443 event_name = "VNC_CONNECTED";
444 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200445 case QEVENT_VNC_INITIALIZED:
446 event_name = "VNC_INITIALIZED";
447 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200448 case QEVENT_VNC_DISCONNECTED:
449 event_name = "VNC_DISCONNECTED";
450 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200451 case QEVENT_BLOCK_IO_ERROR:
452 event_name = "BLOCK_IO_ERROR";
453 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300454 case QEVENT_RTC_CHANGE:
455 event_name = "RTC_CHANGE";
456 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300457 case QEVENT_WATCHDOG:
458 event_name = "WATCHDOG";
459 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200460 default:
461 abort();
462 break;
463 }
464
465 qmp = qdict_new();
466 timestamp_put(qmp);
467 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200468 if (data) {
469 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200470 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200471 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200472
Adam Litkef039a562010-01-15 08:34:02 -0600473 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200474 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200475 monitor_json_emitter(mon, QOBJECT(qmp));
476 }
Adam Litkef039a562010-01-15 08:34:02 -0600477 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200478 QDECREF(qmp);
479}
480
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200481static int do_qmp_capabilities(Monitor *mon, const QDict *params,
482 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200483{
484 /* Will setup QMP capabilities in the future */
485 if (monitor_ctrl_mode(mon)) {
486 mon->mc->command_mode = 1;
487 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200488
489 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200490}
491
bellard9dc39cb2004-03-14 21:38:27 +0000492static int compare_cmd(const char *name, const char *list)
493{
494 const char *p, *pstart;
495 int len;
496 len = strlen(name);
497 p = list;
498 for(;;) {
499 pstart = p;
500 p = strchr(p, '|');
501 if (!p)
502 p = pstart + strlen(pstart);
503 if ((p - pstart) == len && !memcmp(pstart, name, len))
504 return 1;
505 if (*p == '\0')
506 break;
507 p++;
508 }
509 return 0;
510}
511
Anthony Liguoric227f092009-10-01 16:12:16 -0500512static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000513 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000514{
Anthony Liguoric227f092009-10-01 16:12:16 -0500515 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000516
517 for(cmd = cmds; cmd->name != NULL; cmd++) {
518 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000519 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
520 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000521 }
522}
523
aliguori376253e2009-03-05 23:01:23 +0000524static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000525{
526 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000527 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000528 } else {
aliguori376253e2009-03-05 23:01:23 +0000529 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000530 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000531 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000532 monitor_printf(mon, "Log items (comma separated):\n");
533 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000534 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000535 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000536 }
537 }
bellard9dc39cb2004-03-14 21:38:27 +0000538 }
539}
540
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300541static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300542{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300543 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300544}
545
Prerna Saxena22890ab2010-06-24 17:04:53 +0530546#ifdef CONFIG_SIMPLE_TRACE
547static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
548{
549 const char *tp_name = qdict_get_str(qdict, "name");
550 bool new_state = qdict_get_bool(qdict, "option");
551 st_change_trace_event_state(tp_name, new_state);
552}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100553
554static void do_trace_file(Monitor *mon, const QDict *qdict)
555{
556 const char *op = qdict_get_try_str(qdict, "op");
557 const char *arg = qdict_get_try_str(qdict, "arg");
558
559 if (!op) {
560 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
561 } else if (!strcmp(op, "on")) {
562 st_set_trace_file_enabled(true);
563 } else if (!strcmp(op, "off")) {
564 st_set_trace_file_enabled(false);
565 } else if (!strcmp(op, "flush")) {
566 st_flush_trace_buffer();
567 } else if (!strcmp(op, "set")) {
568 if (arg) {
569 st_set_trace_file(arg);
570 }
571 } else {
572 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
573 help_cmd(mon, "trace-file");
574 }
575}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530576#endif
577
Adam Litke940cc302010-01-25 12:18:44 -0600578static void user_monitor_complete(void *opaque, QObject *ret_data)
579{
580 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
581
582 if (ret_data) {
583 data->user_print(data->mon, ret_data);
584 }
585 monitor_resume(data->mon);
586 qemu_free(data);
587}
588
589static void qmp_monitor_complete(void *opaque, QObject *ret_data)
590{
591 monitor_protocol_emitter(opaque, ret_data);
592}
593
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300594static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
595 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600596{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300597 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600598}
599
600static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
601{
602 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
603}
604
605static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
606 const QDict *params)
607{
608 int ret;
609
610 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
611 cb_data->mon = mon;
612 cb_data->user_print = cmd->user_print;
613 monitor_suspend(mon);
614 ret = cmd->mhandler.cmd_async(mon, params,
615 user_monitor_complete, cb_data);
616 if (ret < 0) {
617 monitor_resume(mon);
618 qemu_free(cb_data);
619 }
620}
621
622static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
623{
624 int ret;
625
626 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
627 cb_data->mon = mon;
628 cb_data->user_print = cmd->user_print;
629 monitor_suspend(mon);
630 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
631 if (ret < 0) {
632 monitor_resume(mon);
633 qemu_free(cb_data);
634 }
635}
636
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300637static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000638{
Anthony Liguoric227f092009-10-01 16:12:16 -0500639 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300640 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000641
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200642 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000643 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200644 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300645
646 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000647 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300648 break;
bellard9dc39cb2004-03-14 21:38:27 +0000649 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300650
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200651 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300652 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200653 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300654
Luiz Capitulino4903de02010-09-16 11:01:32 -0300655 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300656 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300657 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300658 QObject *info_data = NULL;
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200659
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300660 cmd->mhandler.info_new(mon, &info_data);
661 if (info_data) {
662 cmd->user_print(mon, info_data);
663 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200664 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300665 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300666 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300667 }
668
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300669 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300670
671help:
672 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000673}
674
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200675static void do_info_version_print(Monitor *mon, const QObject *data)
676{
677 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300678 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200679
680 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300681 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200682
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300683 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
684 qdict_get_int(qemu, "major"),
685 qdict_get_int(qemu, "minor"),
686 qdict_get_int(qemu, "micro"),
687 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200688}
689
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300690static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000691{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300692 const char *version = QEMU_VERSION;
693 int major = 0, minor = 0, micro = 0;
694 char *tmp;
695
696 major = strtol(version, &tmp, 10);
697 tmp++;
698 minor = strtol(tmp, &tmp, 10);
699 tmp++;
700 micro = strtol(tmp, &tmp, 10);
701
702 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
703 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000704}
705
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200706static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000707{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200708 QDict *qdict;
709
710 qdict = qobject_to_qdict(data);
711 if (qdict_size(qdict) == 0) {
712 return;
713 }
714
715 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
716}
717
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200718static void do_info_name(Monitor *mon, QObject **ret_data)
719{
720 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
721 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000722}
723
Luiz Capitulino1a728672009-12-10 17:15:56 -0200724static QObject *get_cmd_dict(const char *name)
725{
726 const char *p;
727
728 /* Remove '|' from some commands */
729 p = strchr(name, '|');
730 if (p) {
731 p++;
732 } else {
733 p = name;
734 }
735
736 return qobject_from_jsonf("{ 'name': %s }", p);
737}
738
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200739static void do_info_commands(Monitor *mon, QObject **ret_data)
740{
741 QList *cmd_list;
742 const mon_cmd_t *cmd;
743
744 cmd_list = qlist_new();
745
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300746 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300747 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200748 }
749
Luiz Capitulino3e12a752010-09-15 17:20:33 -0300750 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino2e061a72010-09-16 10:36:54 -0300751 char buf[128];
752 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
753 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200754 }
755
756 *ret_data = QOBJECT(cmd_list);
757}
758
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200759static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000760{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200761 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
762}
763
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200764static void do_info_uuid(Monitor *mon, QObject **ret_data)
765{
766 char uuid[64];
767
768 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000769 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
770 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
771 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
772 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200773 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000774}
775
bellard6a00d602005-11-21 23:25:50 +0000776/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000777static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000778{
779 CPUState *env;
780
781 for(env = first_cpu; env != NULL; env = env->next_cpu) {
782 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000783 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000784 return 0;
785 }
786 }
787 return -1;
788}
789
pbrook9596ebb2007-11-18 01:44:38 +0000790static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000791{
aliguori731b0362009-03-05 23:01:42 +0000792 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000793 mon_set_cpu(0);
794 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300795 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000796 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000797}
798
aliguori376253e2009-03-05 23:01:23 +0000799static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000800{
bellard6a00d602005-11-21 23:25:50 +0000801 CPUState *env;
802 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000803#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000804 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000805 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000806#else
aliguori376253e2009-03-05 23:01:23 +0000807 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000808 0);
bellard9307c4c2004-04-04 12:57:25 +0000809#endif
810}
811
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300812static void print_cpu_iter(QObject *obj, void *opaque)
813{
814 QDict *cpu;
815 int active = ' ';
816 Monitor *mon = opaque;
817
818 assert(qobject_type(obj) == QTYPE_QDICT);
819 cpu = qobject_to_qdict(obj);
820
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200821 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300822 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200823 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300824
825 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
826
827#if defined(TARGET_I386)
828 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
829 (target_ulong) qdict_get_int(cpu, "pc"));
830#elif defined(TARGET_PPC)
831 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
832 (target_long) qdict_get_int(cpu, "nip"));
833#elif defined(TARGET_SPARC)
834 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
835 (target_long) qdict_get_int(cpu, "pc"));
836 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
837 (target_long) qdict_get_int(cpu, "npc"));
838#elif defined(TARGET_MIPS)
839 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
840 (target_long) qdict_get_int(cpu, "PC"));
841#endif
842
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200843 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300844 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200845 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300846
847 monitor_printf(mon, "\n");
848}
849
850static void monitor_print_cpus(Monitor *mon, const QObject *data)
851{
852 QList *cpu_list;
853
854 assert(qobject_type(data) == QTYPE_QLIST);
855 cpu_list = qobject_to_qlist(data);
856 qlist_iter(cpu_list, print_cpu_iter, mon);
857}
858
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300859static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000860{
861 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300862 QList *cpu_list;
863
864 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000865
866 /* just to set the default cpu if not already done */
867 mon_get_cpu();
868
869 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200870 QDict *cpu;
871 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300872
Avi Kivity4c0960c2009-08-17 23:19:53 +0300873 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300874
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200875 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
876 env->cpu_index, env == mon->mon_cpu,
877 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200878
879 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880
bellard6a00d602005-11-21 23:25:50 +0000881#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300882 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000883#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300884 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000885#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300886 qdict_put(cpu, "pc", qint_from_int(env->pc));
887 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000888#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300889 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000890#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300891
892 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000893 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300894
895 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000896}
897
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200898static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000899{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300900 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200901 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100902 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
903 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200904 return -1;
905 }
906 return 0;
bellard6a00d602005-11-21 23:25:50 +0000907}
908
aliguori376253e2009-03-05 23:01:23 +0000909static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000910{
aliguori376253e2009-03-05 23:01:23 +0000911 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000912}
913
aliguori376253e2009-03-05 23:01:23 +0000914static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000915{
916 int i;
bellard7e2515e2004-08-01 21:52:19 +0000917 const char *str;
ths3b46e622007-09-17 08:09:54 +0000918
aliguoricde76ee2009-03-05 23:01:51 +0000919 if (!mon->rs)
920 return;
bellard7e2515e2004-08-01 21:52:19 +0000921 i = 0;
922 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000923 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000924 if (!str)
925 break;
aliguori376253e2009-03-05 23:01:23 +0000926 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000927 i++;
bellardaa455482004-04-04 13:07:25 +0000928 }
929}
930
j_mayer76a66252007-03-07 08:32:30 +0000931#if defined(TARGET_PPC)
932/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000933static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000934{
935 CPUState *env;
936
937 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000938 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000939}
940#endif
941
Prerna Saxena22890ab2010-06-24 17:04:53 +0530942#if defined(CONFIG_SIMPLE_TRACE)
943static void do_info_trace(Monitor *mon)
944{
945 st_print_trace((FILE *)mon, &monitor_fprintf);
946}
947
948static void do_info_trace_events(Monitor *mon)
949{
950 st_print_trace_events((FILE *)mon, &monitor_fprintf);
951}
952#endif
953
Luiz Capitulinob223f352009-10-07 13:41:56 -0300954/**
955 * do_quit(): Quit QEMU execution
956 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200957static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000958{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300959 monitor_suspend(mon);
960 no_shutdown = 0;
961 qemu_system_shutdown_request();
962
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200963 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000964}
965
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200966static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000967{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200968 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100969 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200970 return -1;
971 }
aliguoribb5fc202009-03-05 23:01:15 +0000972
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200973 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100974}
975
976static void change_vnc_password_cb(Monitor *mon, const char *password,
977 void *opaque)
978{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100979 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000980 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000981}
982
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200983static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000984{
ths70848512007-08-25 01:37:05 +0000985 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000986 strcmp(target, "password") == 0) {
987 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000988 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000989 strncpy(password, arg, sizeof(password));
990 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200991 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000992 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200993 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000994 }
ths70848512007-08-25 01:37:05 +0000995 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200996 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100997 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200998 return -1;
999 }
ths70848512007-08-25 01:37:05 +00001000 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001001
1002 return 0;
thse25a5822007-08-25 01:36:20 +00001003}
1004
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001005/**
1006 * do_change(): Change a removable medium, or VNC configuration
1007 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001008static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001009{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001010 const char *device = qdict_get_str(qdict, "device");
1011 const char *target = qdict_get_str(qdict, "target");
1012 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001013 int ret;
1014
thse25a5822007-08-25 01:36:20 +00001015 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001016 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001017 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001018 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001019 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001020
1021 return ret;
thse25a5822007-08-25 01:36:20 +00001022}
1023
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001024static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001025{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001026 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001027 return 0;
bellard59a983b2004-03-17 23:17:16 +00001028}
1029
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001030static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001031{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001032 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001033}
1034
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001035static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001036{
1037 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001038 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001039
bellard9307c4c2004-04-04 12:57:25 +00001040 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001041 mask = 0;
1042 } else {
bellard9307c4c2004-04-04 12:57:25 +00001043 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001044 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001045 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001046 return;
1047 }
1048 }
1049 cpu_set_log(mask);
1050}
1051
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001052static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001053{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001054 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001055 if (!option || !strcmp(option, "on")) {
1056 singlestep = 1;
1057 } else if (!strcmp(option, "off")) {
1058 singlestep = 0;
1059 } else {
1060 monitor_printf(mon, "unexpected option %s\n", option);
1061 }
1062}
1063
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001064/**
1065 * do_stop(): Stop VM execution
1066 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001067static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001068{
1069 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001070 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001071}
1072
aliguoribb5fc202009-03-05 23:01:15 +00001073static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001074
aliguori376253e2009-03-05 23:01:23 +00001075struct bdrv_iterate_context {
1076 Monitor *mon;
1077 int err;
1078};
aliguoric0f4ce72009-03-05 23:01:01 +00001079
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001080/**
1081 * do_cont(): Resume emulation.
1082 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001083static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001084{
1085 struct bdrv_iterate_context context = { mon, 0 };
1086
Amit Shah8e848652010-07-27 15:49:19 +05301087 if (incoming_expected) {
1088 qerror_report(QERR_MIGRATION_EXPECTED);
1089 return -1;
1090 }
aliguori376253e2009-03-05 23:01:23 +00001091 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001092 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001093 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001094 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001095 return 0;
1096 } else {
1097 return -1;
1098 }
bellard8a7ddc32004-03-31 19:00:16 +00001099}
1100
aliguoribb5fc202009-03-05 23:01:15 +00001101static void bdrv_key_cb(void *opaque, int err)
1102{
aliguori376253e2009-03-05 23:01:23 +00001103 Monitor *mon = opaque;
1104
aliguoribb5fc202009-03-05 23:01:15 +00001105 /* another key was set successfully, retry to continue */
1106 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001107 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001108}
1109
1110static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1111{
aliguori376253e2009-03-05 23:01:23 +00001112 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001113
aliguori376253e2009-03-05 23:01:23 +00001114 if (!context->err && bdrv_key_required(bs)) {
1115 context->err = -EBUSY;
1116 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1117 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001118 }
1119}
1120
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001121static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001122{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001123 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001124 if (!device)
1125 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1126 if (gdbserver_start(device) < 0) {
1127 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1128 device);
1129 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001130 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001131 } else {
aliguori59030a82009-04-05 18:43:41 +00001132 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1133 device);
bellard8a7ddc32004-03-31 19:00:16 +00001134 }
1135}
1136
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001137static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001138{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001139 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001140 if (select_watchdog_action(action) == -1) {
1141 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1142 }
1143}
1144
aliguori376253e2009-03-05 23:01:23 +00001145static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001146{
aliguori376253e2009-03-05 23:01:23 +00001147 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001148 switch(c) {
1149 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001150 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001151 break;
1152 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001153 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001154 break;
1155 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001156 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001157 break;
1158 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001159 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001160 break;
1161 default:
1162 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001163 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001164 } else {
aliguori376253e2009-03-05 23:01:23 +00001165 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001166 }
1167 break;
1168 }
aliguori376253e2009-03-05 23:01:23 +00001169 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001170}
1171
aliguori376253e2009-03-05 23:01:23 +00001172static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001173 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001174{
bellard6a00d602005-11-21 23:25:50 +00001175 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001176 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001177 uint8_t buf[16];
1178 uint64_t v;
1179
1180 if (format == 'i') {
1181 int flags;
1182 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001183 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001184#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001185 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001186 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001187 } else if (wsize == 4) {
1188 flags = 0;
1189 } else {
bellard6a15fd12006-04-12 21:07:07 +00001190 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001191 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001192 if (env) {
1193#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001194 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001195 (env->segs[R_CS].flags & DESC_L_MASK))
1196 flags = 2;
1197 else
1198#endif
1199 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1200 flags = 1;
1201 }
bellard4c27ba22004-04-25 18:05:08 +00001202 }
1203#endif
aliguori376253e2009-03-05 23:01:23 +00001204 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001205 return;
1206 }
1207
1208 len = wsize * count;
1209 if (wsize == 1)
1210 line_size = 8;
1211 else
1212 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001213 max_digits = 0;
1214
1215 switch(format) {
1216 case 'o':
1217 max_digits = (wsize * 8 + 2) / 3;
1218 break;
1219 default:
1220 case 'x':
1221 max_digits = (wsize * 8) / 4;
1222 break;
1223 case 'u':
1224 case 'd':
1225 max_digits = (wsize * 8 * 10 + 32) / 33;
1226 break;
1227 case 'c':
1228 wsize = 1;
1229 break;
1230 }
1231
1232 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001233 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001235 else
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001237 l = len;
1238 if (l > line_size)
1239 l = line_size;
1240 if (is_physical) {
1241 cpu_physical_memory_rw(addr, buf, l, 0);
1242 } else {
bellard6a00d602005-11-21 23:25:50 +00001243 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001244 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001246 break;
1247 }
bellard9307c4c2004-04-04 12:57:25 +00001248 }
ths5fafdf22007-09-16 21:08:06 +00001249 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001250 while (i < l) {
1251 switch(wsize) {
1252 default:
1253 case 1:
1254 v = ldub_raw(buf + i);
1255 break;
1256 case 2:
1257 v = lduw_raw(buf + i);
1258 break;
1259 case 4:
bellard92a31b12005-02-10 22:00:52 +00001260 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001261 break;
1262 case 8:
1263 v = ldq_raw(buf + i);
1264 break;
1265 }
aliguori376253e2009-03-05 23:01:23 +00001266 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001267 switch(format) {
1268 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001270 break;
1271 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001273 break;
1274 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001275 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001276 break;
1277 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001279 break;
1280 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001281 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 }
1284 i += wsize;
1285 }
aliguori376253e2009-03-05 23:01:23 +00001286 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001287 addr += l;
1288 len -= l;
1289 }
1290}
1291
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001292static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001293{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001294 int count = qdict_get_int(qdict, "count");
1295 int format = qdict_get_int(qdict, "format");
1296 int size = qdict_get_int(qdict, "size");
1297 target_long addr = qdict_get_int(qdict, "addr");
1298
aliguori376253e2009-03-05 23:01:23 +00001299 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001300}
1301
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001302static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001303{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001304 int count = qdict_get_int(qdict, "count");
1305 int format = qdict_get_int(qdict, "format");
1306 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001307 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001308
aliguori376253e2009-03-05 23:01:23 +00001309 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001310}
1311
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001312static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001313{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001314 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001315 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001316
blueswir17743e582007-09-24 18:39:04 +00001317#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001318 switch(format) {
1319 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001321 break;
1322 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001324 break;
1325 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001327 break;
1328 default:
1329 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001330 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001331 break;
1332 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001333 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001334 break;
1335 }
bellard92a31b12005-02-10 22:00:52 +00001336#else
1337 switch(format) {
1338 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001339 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001340 break;
1341 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001342 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001343 break;
1344 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001345 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001346 break;
1347 default:
1348 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001349 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001350 break;
1351 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001353 break;
1354 }
1355#endif
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001357}
1358
Luiz Capitulino98696222010-02-10 23:49:58 -02001359static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001360{
1361 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001362 uint32_t size = qdict_get_int(qdict, "size");
1363 const char *filename = qdict_get_str(qdict, "filename");
1364 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001365 uint32_t l;
1366 CPUState *env;
1367 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001368 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001369
1370 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001371
1372 f = fopen(filename, "wb");
1373 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001374 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001375 return -1;
bellardb371dc52007-01-03 15:20:39 +00001376 }
1377 while (size != 0) {
1378 l = sizeof(buf);
1379 if (l > size)
1380 l = size;
1381 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001382 if (fwrite(buf, 1, l, f) != l) {
1383 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1384 goto exit;
1385 }
bellardb371dc52007-01-03 15:20:39 +00001386 addr += l;
1387 size -= l;
1388 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001389
1390 ret = 0;
1391
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001392exit:
bellardb371dc52007-01-03 15:20:39 +00001393 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001394 return ret;
bellardb371dc52007-01-03 15:20:39 +00001395}
1396
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001397static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001398 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001399{
1400 FILE *f;
1401 uint32_t l;
1402 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001403 uint32_t size = qdict_get_int(qdict, "size");
1404 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001405 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001406 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001407
1408 f = fopen(filename, "wb");
1409 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001410 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001411 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001412 }
1413 while (size != 0) {
1414 l = sizeof(buf);
1415 if (l > size)
1416 l = size;
1417 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001418 if (fwrite(buf, 1, l, f) != l) {
1419 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1420 goto exit;
1421 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001422 fflush(f);
1423 addr += l;
1424 size -= l;
1425 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001426
1427 ret = 0;
1428
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001429exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001430 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001431 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001432}
1433
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001434static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001435{
1436 uint32_t addr;
1437 uint8_t buf[1];
1438 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001439 uint32_t start = qdict_get_int(qdict, "start");
1440 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001441
1442 sum = 0;
1443 for(addr = start; addr < (start + size); addr++) {
1444 cpu_physical_memory_rw(addr, buf, 1, 0);
1445 /* BSD sum algorithm ('sum' Unix command) */
1446 sum = (sum >> 1) | (sum << 15);
1447 sum += buf[0];
1448 }
aliguori376253e2009-03-05 23:01:23 +00001449 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001450}
1451
bellarda3a91a32004-06-04 11:06:21 +00001452typedef struct {
1453 int keycode;
1454 const char *name;
1455} KeyDef;
1456
1457static const KeyDef key_defs[] = {
1458 { 0x2a, "shift" },
1459 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001460
bellarda3a91a32004-06-04 11:06:21 +00001461 { 0x38, "alt" },
1462 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001463 { 0x64, "altgr" },
1464 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001465 { 0x1d, "ctrl" },
1466 { 0x9d, "ctrl_r" },
1467
1468 { 0xdd, "menu" },
1469
1470 { 0x01, "esc" },
1471
1472 { 0x02, "1" },
1473 { 0x03, "2" },
1474 { 0x04, "3" },
1475 { 0x05, "4" },
1476 { 0x06, "5" },
1477 { 0x07, "6" },
1478 { 0x08, "7" },
1479 { 0x09, "8" },
1480 { 0x0a, "9" },
1481 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001482 { 0x0c, "minus" },
1483 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001484 { 0x0e, "backspace" },
1485
1486 { 0x0f, "tab" },
1487 { 0x10, "q" },
1488 { 0x11, "w" },
1489 { 0x12, "e" },
1490 { 0x13, "r" },
1491 { 0x14, "t" },
1492 { 0x15, "y" },
1493 { 0x16, "u" },
1494 { 0x17, "i" },
1495 { 0x18, "o" },
1496 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001497 { 0x1a, "bracket_left" },
1498 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001499 { 0x1c, "ret" },
1500
1501 { 0x1e, "a" },
1502 { 0x1f, "s" },
1503 { 0x20, "d" },
1504 { 0x21, "f" },
1505 { 0x22, "g" },
1506 { 0x23, "h" },
1507 { 0x24, "j" },
1508 { 0x25, "k" },
1509 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001510 { 0x27, "semicolon" },
1511 { 0x28, "apostrophe" },
1512 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001513
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001514 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001515 { 0x2c, "z" },
1516 { 0x2d, "x" },
1517 { 0x2e, "c" },
1518 { 0x2f, "v" },
1519 { 0x30, "b" },
1520 { 0x31, "n" },
1521 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001522 { 0x33, "comma" },
1523 { 0x34, "dot" },
1524 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001525
balrog4d3b6f62008-02-10 16:33:14 +00001526 { 0x37, "asterisk" },
1527
bellarda3a91a32004-06-04 11:06:21 +00001528 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001529 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001530 { 0x3b, "f1" },
1531 { 0x3c, "f2" },
1532 { 0x3d, "f3" },
1533 { 0x3e, "f4" },
1534 { 0x3f, "f5" },
1535 { 0x40, "f6" },
1536 { 0x41, "f7" },
1537 { 0x42, "f8" },
1538 { 0x43, "f9" },
1539 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001540 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001541 { 0x46, "scroll_lock" },
1542
bellard64866c32006-05-07 18:03:31 +00001543 { 0xb5, "kp_divide" },
1544 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001545 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001546 { 0x4e, "kp_add" },
1547 { 0x9c, "kp_enter" },
1548 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001549 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001550
1551 { 0x52, "kp_0" },
1552 { 0x4f, "kp_1" },
1553 { 0x50, "kp_2" },
1554 { 0x51, "kp_3" },
1555 { 0x4b, "kp_4" },
1556 { 0x4c, "kp_5" },
1557 { 0x4d, "kp_6" },
1558 { 0x47, "kp_7" },
1559 { 0x48, "kp_8" },
1560 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001561
bellarda3a91a32004-06-04 11:06:21 +00001562 { 0x56, "<" },
1563
1564 { 0x57, "f11" },
1565 { 0x58, "f12" },
1566
1567 { 0xb7, "print" },
1568
1569 { 0xc7, "home" },
1570 { 0xc9, "pgup" },
1571 { 0xd1, "pgdn" },
1572 { 0xcf, "end" },
1573
1574 { 0xcb, "left" },
1575 { 0xc8, "up" },
1576 { 0xd0, "down" },
1577 { 0xcd, "right" },
1578
1579 { 0xd2, "insert" },
1580 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001581#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1582 { 0xf0, "stop" },
1583 { 0xf1, "again" },
1584 { 0xf2, "props" },
1585 { 0xf3, "undo" },
1586 { 0xf4, "front" },
1587 { 0xf5, "copy" },
1588 { 0xf6, "open" },
1589 { 0xf7, "paste" },
1590 { 0xf8, "find" },
1591 { 0xf9, "cut" },
1592 { 0xfa, "lf" },
1593 { 0xfb, "help" },
1594 { 0xfc, "meta_l" },
1595 { 0xfd, "meta_r" },
1596 { 0xfe, "compose" },
1597#endif
bellarda3a91a32004-06-04 11:06:21 +00001598 { 0, NULL },
1599};
1600
1601static int get_keycode(const char *key)
1602{
1603 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001604 char *endp;
1605 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001606
1607 for(p = key_defs; p->name != NULL; p++) {
1608 if (!strcmp(key, p->name))
1609 return p->keycode;
1610 }
bellard64866c32006-05-07 18:03:31 +00001611 if (strstart(key, "0x", NULL)) {
1612 ret = strtoul(key, &endp, 0);
1613 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1614 return ret;
1615 }
bellarda3a91a32004-06-04 11:06:21 +00001616 return -1;
1617}
1618
balrogc8256f92008-06-08 22:45:01 +00001619#define MAX_KEYCODES 16
1620static uint8_t keycodes[MAX_KEYCODES];
1621static int nb_pending_keycodes;
1622static QEMUTimer *key_timer;
1623
1624static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001625{
balrogc8256f92008-06-08 22:45:01 +00001626 int keycode;
1627
1628 while (nb_pending_keycodes > 0) {
1629 nb_pending_keycodes--;
1630 keycode = keycodes[nb_pending_keycodes];
1631 if (keycode & 0x80)
1632 kbd_put_keycode(0xe0);
1633 kbd_put_keycode(keycode | 0x80);
1634 }
1635}
1636
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001637static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001638{
balrog3401c0d2008-06-04 10:05:59 +00001639 char keyname_buf[16];
1640 char *separator;
1641 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001642 const char *string = qdict_get_str(qdict, "string");
1643 int has_hold_time = qdict_haskey(qdict, "hold_time");
1644 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001645
balrogc8256f92008-06-08 22:45:01 +00001646 if (nb_pending_keycodes > 0) {
1647 qemu_del_timer(key_timer);
1648 release_keys(NULL);
1649 }
1650 if (!has_hold_time)
1651 hold_time = 100;
1652 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001653 while (1) {
1654 separator = strchr(string, '-');
1655 keyname_len = separator ? separator - string : strlen(string);
1656 if (keyname_len > 0) {
1657 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1658 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001659 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001660 return;
bellarda3a91a32004-06-04 11:06:21 +00001661 }
balrogc8256f92008-06-08 22:45:01 +00001662 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001663 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001664 return;
1665 }
1666 keyname_buf[keyname_len] = 0;
1667 keycode = get_keycode(keyname_buf);
1668 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001669 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001670 return;
1671 }
balrogc8256f92008-06-08 22:45:01 +00001672 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001673 }
balrog3401c0d2008-06-04 10:05:59 +00001674 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001675 break;
balrog3401c0d2008-06-04 10:05:59 +00001676 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001677 }
balrogc8256f92008-06-08 22:45:01 +00001678 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001679 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001680 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001681 keycode = keycodes[i];
1682 if (keycode & 0x80)
1683 kbd_put_keycode(0xe0);
1684 kbd_put_keycode(keycode & 0x7f);
1685 }
balrogc8256f92008-06-08 22:45:01 +00001686 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001687 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001688 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001689}
1690
bellard13224a82006-07-14 22:03:35 +00001691static int mouse_button_state;
1692
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001693static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001694{
1695 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001696 const char *dx_str = qdict_get_str(qdict, "dx_str");
1697 const char *dy_str = qdict_get_str(qdict, "dy_str");
1698 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001699 dx = strtol(dx_str, NULL, 0);
1700 dy = strtol(dy_str, NULL, 0);
1701 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001702 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001703 dz = strtol(dz_str, NULL, 0);
1704 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1705}
1706
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001707static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001708{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001709 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001710 mouse_button_state = button_state;
1711 kbd_mouse_event(0, 0, 0, mouse_button_state);
1712}
1713
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001714static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001715{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001716 int size = qdict_get_int(qdict, "size");
1717 int addr = qdict_get_int(qdict, "addr");
1718 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001719 uint32_t val;
1720 int suffix;
1721
1722 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001723 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001724 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001725 addr++;
1726 }
1727 addr &= 0xffff;
1728
1729 switch(size) {
1730 default:
1731 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001732 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001733 suffix = 'b';
1734 break;
1735 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001736 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001737 suffix = 'w';
1738 break;
1739 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001740 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001741 suffix = 'l';
1742 break;
1743 }
aliguori376253e2009-03-05 23:01:23 +00001744 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1745 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001746}
bellarda3a91a32004-06-04 11:06:21 +00001747
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001748static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001749{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001750 int size = qdict_get_int(qdict, "size");
1751 int addr = qdict_get_int(qdict, "addr");
1752 int val = qdict_get_int(qdict, "val");
1753
Jan Kiszkaf1147842009-07-14 10:20:11 +02001754 addr &= IOPORTS_MASK;
1755
1756 switch (size) {
1757 default:
1758 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001759 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001760 break;
1761 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001762 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001763 break;
1764 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001765 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001766 break;
1767 }
1768}
1769
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001770static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001771{
1772 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001773 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001774
Jan Kiszka76e30d02009-07-02 00:19:02 +02001775 res = qemu_boot_set(bootdevice);
1776 if (res == 0) {
1777 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1778 } else if (res > 0) {
1779 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001780 } else {
aliguori376253e2009-03-05 23:01:23 +00001781 monitor_printf(mon, "no function defined to set boot device list for "
1782 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001783 }
1784}
1785
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001786/**
1787 * do_system_reset(): Issue a machine reset
1788 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001789static int do_system_reset(Monitor *mon, const QDict *qdict,
1790 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001791{
1792 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001793 return 0;
bellarde4f90822004-06-20 12:35:44 +00001794}
1795
Luiz Capitulino43076662009-10-07 13:41:59 -03001796/**
1797 * do_system_powerdown(): Issue a machine powerdown
1798 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001799static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1800 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001801{
1802 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001803 return 0;
bellard34751872005-07-02 14:31:34 +00001804}
1805
bellardb86bda52004-09-18 19:32:46 +00001806#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001807static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001808{
aliguori376253e2009-03-05 23:01:23 +00001809 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1810 addr,
1811 pte & mask,
1812 pte & PG_GLOBAL_MASK ? 'G' : '-',
1813 pte & PG_PSE_MASK ? 'P' : '-',
1814 pte & PG_DIRTY_MASK ? 'D' : '-',
1815 pte & PG_ACCESSED_MASK ? 'A' : '-',
1816 pte & PG_PCD_MASK ? 'C' : '-',
1817 pte & PG_PWT_MASK ? 'T' : '-',
1818 pte & PG_USER_MASK ? 'U' : '-',
1819 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001820}
1821
aliguori376253e2009-03-05 23:01:23 +00001822static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001823{
bellard6a00d602005-11-21 23:25:50 +00001824 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001825 int l1, l2;
1826 uint32_t pgd, pde, pte;
1827
bellard6a00d602005-11-21 23:25:50 +00001828 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001829
bellardb86bda52004-09-18 19:32:46 +00001830 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001831 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001832 return;
1833 }
1834 pgd = env->cr[3] & ~0xfff;
1835 for(l1 = 0; l1 < 1024; l1++) {
1836 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1837 pde = le32_to_cpu(pde);
1838 if (pde & PG_PRESENT_MASK) {
1839 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001840 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001841 } else {
1842 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001843 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001844 (uint8_t *)&pte, 4);
1845 pte = le32_to_cpu(pte);
1846 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001847 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001848 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001849 ~0xfff);
1850 }
1851 }
1852 }
1853 }
1854 }
1855}
1856
aliguori376253e2009-03-05 23:01:23 +00001857static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001858 uint32_t end, int prot)
1859{
bellard9746b152004-11-11 18:30:24 +00001860 int prot1;
1861 prot1 = *plast_prot;
1862 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001863 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001864 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1865 *pstart, end, end - *pstart,
1866 prot1 & PG_USER_MASK ? 'u' : '-',
1867 'r',
1868 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001869 }
1870 if (prot != 0)
1871 *pstart = end;
1872 else
1873 *pstart = -1;
1874 *plast_prot = prot;
1875 }
1876}
1877
aliguori376253e2009-03-05 23:01:23 +00001878static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001879{
bellard6a00d602005-11-21 23:25:50 +00001880 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001881 int l1, l2, prot, last_prot;
1882 uint32_t pgd, pde, pte, start, end;
1883
bellard6a00d602005-11-21 23:25:50 +00001884 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001885
bellardb86bda52004-09-18 19:32:46 +00001886 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001887 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001888 return;
1889 }
1890 pgd = env->cr[3] & ~0xfff;
1891 last_prot = 0;
1892 start = -1;
1893 for(l1 = 0; l1 < 1024; l1++) {
1894 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1895 pde = le32_to_cpu(pde);
1896 end = l1 << 22;
1897 if (pde & PG_PRESENT_MASK) {
1898 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1899 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001900 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001901 } else {
1902 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001903 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001904 (uint8_t *)&pte, 4);
1905 pte = le32_to_cpu(pte);
1906 end = (l1 << 22) + (l2 << 12);
1907 if (pte & PG_PRESENT_MASK) {
1908 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1909 } else {
1910 prot = 0;
1911 }
aliguori376253e2009-03-05 23:01:23 +00001912 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001913 }
1914 }
1915 } else {
1916 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001917 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001918 }
1919 }
1920}
1921#endif
1922
aurel327c664e22009-03-03 06:12:22 +00001923#if defined(TARGET_SH4)
1924
aliguori376253e2009-03-05 23:01:23 +00001925static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001926{
aliguori376253e2009-03-05 23:01:23 +00001927 monitor_printf(mon, " tlb%i:\t"
1928 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1929 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1930 "dirty=%hhu writethrough=%hhu\n",
1931 idx,
1932 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1933 tlb->v, tlb->sh, tlb->c, tlb->pr,
1934 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001935}
1936
aliguori376253e2009-03-05 23:01:23 +00001937static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001938{
1939 CPUState *env = mon_get_cpu();
1940 int i;
1941
aliguori376253e2009-03-05 23:01:23 +00001942 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001943 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001944 print_tlb (mon, i, &env->itlb[i]);
1945 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001946 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001947 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001948}
1949
1950#endif
1951
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001952static void do_info_kvm_print(Monitor *mon, const QObject *data)
1953{
1954 QDict *qdict;
1955
1956 qdict = qobject_to_qdict(data);
1957
1958 monitor_printf(mon, "kvm support: ");
1959 if (qdict_get_bool(qdict, "present")) {
1960 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1961 "enabled" : "disabled");
1962 } else {
1963 monitor_printf(mon, "not compiled\n");
1964 }
1965}
1966
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001967static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001968{
1969#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001970 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1971 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001972#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001973 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001974#endif
1975}
1976
aliguori030ea372009-04-21 22:30:47 +00001977static void do_info_numa(Monitor *mon)
1978{
aliguorib28b6232009-04-22 20:20:29 +00001979 int i;
aliguori030ea372009-04-21 22:30:47 +00001980 CPUState *env;
1981
1982 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1983 for (i = 0; i < nb_numa_nodes; i++) {
1984 monitor_printf(mon, "node %d cpus:", i);
1985 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1986 if (env->numa_node == i) {
1987 monitor_printf(mon, " %d", env->cpu_index);
1988 }
1989 }
1990 monitor_printf(mon, "\n");
1991 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1992 node_mem[i] >> 20);
1993 }
1994}
1995
bellard5f1ce942006-02-08 22:40:15 +00001996#ifdef CONFIG_PROFILER
1997
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001998int64_t qemu_time;
1999int64_t dev_time;
2000
aliguori376253e2009-03-05 23:01:23 +00002001static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002002{
2003 int64_t total;
2004 total = qemu_time;
2005 if (total == 0)
2006 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002007 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002008 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002009 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002010 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002011 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002012 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002013}
2014#else
aliguori376253e2009-03-05 23:01:23 +00002015static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002016{
aliguori376253e2009-03-05 23:01:23 +00002017 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002018}
2019#endif
2020
bellardec36b692006-07-16 18:57:03 +00002021/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002022static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002023
aliguori376253e2009-03-05 23:01:23 +00002024static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002025{
2026 int i;
2027 CaptureState *s;
2028
2029 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002030 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002031 s->ops.info (s->opaque);
2032 }
2033}
2034
Blue Swirl23130862009-06-06 08:22:04 +00002035#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002036static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002037{
2038 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002039 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002040 CaptureState *s;
2041
2042 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2043 if (i == n) {
2044 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002045 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002046 qemu_free (s);
2047 return;
2048 }
2049 }
2050}
2051
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002052static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002053{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002054 const char *path = qdict_get_str(qdict, "path");
2055 int has_freq = qdict_haskey(qdict, "freq");
2056 int freq = qdict_get_try_int(qdict, "freq", -1);
2057 int has_bits = qdict_haskey(qdict, "bits");
2058 int bits = qdict_get_try_int(qdict, "bits", -1);
2059 int has_channels = qdict_haskey(qdict, "nchannels");
2060 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002061 CaptureState *s;
2062
2063 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002064
2065 freq = has_freq ? freq : 44100;
2066 bits = has_bits ? bits : 16;
2067 nchannels = has_channels ? nchannels : 2;
2068
2069 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002070 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002071 qemu_free (s);
2072 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002073 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002074}
2075#endif
2076
aurel32dc1c0b72008-04-27 23:52:12 +00002077#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002078static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002079{
2080 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002081 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002082
2083 for (env = first_cpu; env != NULL; env = env->next_cpu)
2084 if (env->cpu_index == cpu_index) {
2085 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2086 break;
2087 }
2088}
2089#endif
2090
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002091static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002092{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002093 QDict *qdict;
2094
2095 qdict = qobject_to_qdict(data);
2096
2097 monitor_printf(mon, "VM status: ");
2098 if (qdict_get_bool(qdict, "running")) {
2099 monitor_printf(mon, "running");
2100 if (qdict_get_bool(qdict, "singlestep")) {
2101 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002102 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002103 } else {
2104 monitor_printf(mon, "paused");
2105 }
2106
2107 monitor_printf(mon, "\n");
2108}
2109
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002110static void do_info_status(Monitor *mon, QObject **ret_data)
2111{
2112 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2113 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002114}
2115
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002116static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002117{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002118 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002119
aliguori76655d62009-03-06 20:27:37 +00002120 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002121 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002122 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002123 return acl;
2124}
aliguori76655d62009-03-06 20:27:37 +00002125
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002126static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002127{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002128 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002129 qemu_acl *acl = find_acl(mon, aclname);
2130 qemu_acl_entry *entry;
2131 int i = 0;
2132
2133 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002134 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002135 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002136 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002137 i++;
2138 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002140 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002141 }
2142}
2143
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002144static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002145{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002146 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002147 qemu_acl *acl = find_acl(mon, aclname);
2148
2149 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002150 qemu_acl_reset(acl);
2151 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 }
2153}
aliguori76655d62009-03-06 20:27:37 +00002154
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002155static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002156{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002157 const char *aclname = qdict_get_str(qdict, "aclname");
2158 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002159 qemu_acl *acl = find_acl(mon, aclname);
2160
2161 if (acl) {
2162 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002163 acl->defaultDeny = 0;
2164 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002165 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002166 acl->defaultDeny = 1;
2167 monitor_printf(mon, "acl: policy set to 'deny'\n");
2168 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002169 monitor_printf(mon, "acl: unknown policy '%s', "
2170 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002171 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002172 }
2173}
aliguori76655d62009-03-06 20:27:37 +00002174
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002175static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002176{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002177 const char *aclname = qdict_get_str(qdict, "aclname");
2178 const char *match = qdict_get_str(qdict, "match");
2179 const char *policy = qdict_get_str(qdict, "policy");
2180 int has_index = qdict_haskey(qdict, "index");
2181 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002182 qemu_acl *acl = find_acl(mon, aclname);
2183 int deny, ret;
2184
2185 if (acl) {
2186 if (strcmp(policy, "allow") == 0) {
2187 deny = 0;
2188 } else if (strcmp(policy, "deny") == 0) {
2189 deny = 1;
2190 } else {
2191 monitor_printf(mon, "acl: unknown policy '%s', "
2192 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002193 return;
2194 }
aliguori28a76be2009-03-06 20:27:40 +00002195 if (has_index)
2196 ret = qemu_acl_insert(acl, deny, match, index);
2197 else
2198 ret = qemu_acl_append(acl, deny, match);
2199 if (ret < 0)
2200 monitor_printf(mon, "acl: unable to add acl entry\n");
2201 else
2202 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002203 }
2204}
aliguori76655d62009-03-06 20:27:37 +00002205
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002206static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002207{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002208 const char *aclname = qdict_get_str(qdict, "aclname");
2209 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002210 qemu_acl *acl = find_acl(mon, aclname);
2211 int ret;
aliguori76655d62009-03-06 20:27:37 +00002212
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002213 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002214 ret = qemu_acl_remove(acl, match);
2215 if (ret < 0)
2216 monitor_printf(mon, "acl: no matching acl entry\n");
2217 else
2218 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002219 }
2220}
2221
Huang Ying79c4f6b2009-06-23 10:05:14 +08002222#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002223static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002224{
2225 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002226 int cpu_index = qdict_get_int(qdict, "cpu_index");
2227 int bank = qdict_get_int(qdict, "bank");
2228 uint64_t status = qdict_get_int(qdict, "status");
2229 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2230 uint64_t addr = qdict_get_int(qdict, "addr");
2231 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002232
2233 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2234 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2235 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2236 break;
2237 }
2238}
2239#endif
2240
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002241static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002242{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002243 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002244 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002245 int fd;
2246
2247 fd = qemu_chr_get_msgfd(mon->chr);
2248 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002249 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002250 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002251 }
2252
2253 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002254 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2255 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002256 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002257 }
2258
Blue Swirl72cf2d42009-09-12 07:36:22 +00002259 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002260 if (strcmp(monfd->name, fdname) != 0) {
2261 continue;
2262 }
2263
2264 close(monfd->fd);
2265 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002266 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002267 }
2268
Anthony Liguoric227f092009-10-01 16:12:16 -05002269 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002270 monfd->name = qemu_strdup(fdname);
2271 monfd->fd = fd;
2272
Blue Swirl72cf2d42009-09-12 07:36:22 +00002273 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002274 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002275}
2276
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002277static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002278{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002279 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002280 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002281
Blue Swirl72cf2d42009-09-12 07:36:22 +00002282 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002283 if (strcmp(monfd->name, fdname) != 0) {
2284 continue;
2285 }
2286
Blue Swirl72cf2d42009-09-12 07:36:22 +00002287 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002288 close(monfd->fd);
2289 qemu_free(monfd->name);
2290 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002291 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002292 }
2293
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002294 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002295 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002296}
2297
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002298static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002299{
2300 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002301 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002302
2303 vm_stop(0);
2304
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002305 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002306 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002307 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002308}
2309
Mark McLoughlin7768e042009-07-22 09:11:41 +01002310int monitor_get_fd(Monitor *mon, const char *fdname)
2311{
Anthony Liguoric227f092009-10-01 16:12:16 -05002312 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002313
Blue Swirl72cf2d42009-09-12 07:36:22 +00002314 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002315 int fd;
2316
2317 if (strcmp(monfd->name, fdname) != 0) {
2318 continue;
2319 }
2320
2321 fd = monfd->fd;
2322
2323 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002324 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002325 qemu_free(monfd->name);
2326 qemu_free(monfd);
2327
2328 return fd;
2329 }
2330
2331 return -1;
2332}
2333
Anthony Liguoric227f092009-10-01 16:12:16 -05002334static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002335#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002336 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002337};
2338
Blue Swirl23130862009-06-06 08:22:04 +00002339/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002340static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002341 {
2342 .name = "version",
2343 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002344 .params = "",
2345 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002346 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002347 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002348 },
2349 {
2350 .name = "network",
2351 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002352 .params = "",
2353 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002354 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002355 },
2356 {
2357 .name = "chardev",
2358 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002359 .params = "",
2360 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002361 .user_print = qemu_chr_info_print,
2362 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002363 },
2364 {
2365 .name = "block",
2366 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002367 .params = "",
2368 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002369 .user_print = bdrv_info_print,
2370 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002371 },
2372 {
2373 .name = "blockstats",
2374 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002375 .params = "",
2376 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002377 .user_print = bdrv_stats_print,
2378 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002379 },
2380 {
2381 .name = "registers",
2382 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002383 .params = "",
2384 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002385 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002386 },
2387 {
2388 .name = "cpus",
2389 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002390 .params = "",
2391 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002392 .user_print = monitor_print_cpus,
2393 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002394 },
2395 {
2396 .name = "history",
2397 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 .params = "",
2399 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002400 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002401 },
2402 {
2403 .name = "irq",
2404 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002405 .params = "",
2406 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002407 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002408 },
2409 {
2410 .name = "pic",
2411 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002412 .params = "",
2413 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002414 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002415 },
2416 {
2417 .name = "pci",
2418 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002419 .params = "",
2420 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002421 .user_print = do_pci_info_print,
2422 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002423 },
aurel327c664e22009-03-03 06:12:22 +00002424#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002425 {
2426 .name = "tlb",
2427 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002428 .params = "",
2429 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002430 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002431 },
aurel327c664e22009-03-03 06:12:22 +00002432#endif
2433#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002434 {
2435 .name = "mem",
2436 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002437 .params = "",
2438 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002439 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002440 },
bellardb86bda52004-09-18 19:32:46 +00002441#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002442 {
2443 .name = "jit",
2444 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002445 .params = "",
2446 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002447 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002448 },
2449 {
2450 .name = "kvm",
2451 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002452 .params = "",
2453 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002454 .user_print = do_info_kvm_print,
2455 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002456 },
2457 {
2458 .name = "numa",
2459 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002460 .params = "",
2461 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002462 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002463 },
2464 {
2465 .name = "usb",
2466 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 .params = "",
2468 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002469 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002470 },
2471 {
2472 .name = "usbhost",
2473 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002474 .params = "",
2475 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002476 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002477 },
2478 {
2479 .name = "profile",
2480 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002481 .params = "",
2482 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002483 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002484 },
2485 {
2486 .name = "capture",
2487 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002488 .params = "",
2489 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002490 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002491 },
2492 {
2493 .name = "snapshots",
2494 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002495 .params = "",
2496 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002497 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002498 },
2499 {
2500 .name = "status",
2501 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002502 .params = "",
2503 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002504 .user_print = do_info_status_print,
2505 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002506 },
2507 {
2508 .name = "pcmcia",
2509 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002510 .params = "",
2511 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002512 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002513 },
2514 {
2515 .name = "mice",
2516 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 .params = "",
2518 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002519 .user_print = do_info_mice_print,
2520 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 },
2522 {
2523 .name = "vnc",
2524 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 .params = "",
2526 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002527 .user_print = do_info_vnc_print,
2528 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 },
2530 {
2531 .name = "name",
2532 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 .params = "",
2534 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002535 .user_print = do_info_name_print,
2536 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 },
2538 {
2539 .name = "uuid",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002543 .user_print = do_info_uuid_print,
2544 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 },
j_mayer76a66252007-03-07 08:32:30 +00002546#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 {
2548 .name = "cpustats",
2549 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002550 .params = "",
2551 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002552 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002553 },
j_mayer76a66252007-03-07 08:32:30 +00002554#endif
blueswir131a60e22007-10-26 18:42:59 +00002555#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 {
2557 .name = "usernet",
2558 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 .params = "",
2560 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002561 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 },
blueswir131a60e22007-10-26 18:42:59 +00002563#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 {
2565 .name = "migrate",
2566 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 .params = "",
2568 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002569 .user_print = do_info_migrate_print,
2570 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 },
2572 {
2573 .name = "balloon",
2574 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 .params = "",
2576 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002577 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002578 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002579 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
2582 .name = "qtree",
2583 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 .params = "",
2585 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002586 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 },
2588 {
2589 .name = "qdm",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002593 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
2595 {
2596 .name = "roms",
2597 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 .params = "",
2599 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002600 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302602#if defined(CONFIG_SIMPLE_TRACE)
2603 {
2604 .name = "trace",
2605 .args_type = "",
2606 .params = "",
2607 .help = "show current contents of trace buffer",
2608 .mhandler.info = do_info_trace,
2609 },
2610 {
2611 .name = "trace-events",
2612 .args_type = "",
2613 .params = "",
2614 .help = "show available trace-events & their state",
2615 .mhandler.info = do_info_trace_events,
2616 },
2617#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 {
2619 .name = NULL,
2620 },
bellard9dc39cb2004-03-14 21:38:27 +00002621};
2622
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002623static const mon_cmd_t qmp_cmds[] = {
2624#include "qmp-commands.h"
2625 { /* NULL */ },
2626};
2627
Luiz Capitulino3e12a752010-09-15 17:20:33 -03002628static const mon_cmd_t qmp_query_cmds[] = {
2629 {
2630 .name = "version",
2631 .args_type = "",
2632 .params = "",
2633 .help = "show the version of QEMU",
2634 .user_print = do_info_version_print,
2635 .mhandler.info_new = do_info_version,
2636 },
2637 {
2638 .name = "commands",
2639 .args_type = "",
2640 .params = "",
2641 .help = "list QMP available commands",
2642 .user_print = monitor_user_noop,
2643 .mhandler.info_new = do_info_commands,
2644 },
2645 {
2646 .name = "chardev",
2647 .args_type = "",
2648 .params = "",
2649 .help = "show the character devices",
2650 .user_print = qemu_chr_info_print,
2651 .mhandler.info_new = qemu_chr_info,
2652 },
2653 {
2654 .name = "block",
2655 .args_type = "",
2656 .params = "",
2657 .help = "show the block devices",
2658 .user_print = bdrv_info_print,
2659 .mhandler.info_new = bdrv_info,
2660 },
2661 {
2662 .name = "blockstats",
2663 .args_type = "",
2664 .params = "",
2665 .help = "show block device statistics",
2666 .user_print = bdrv_stats_print,
2667 .mhandler.info_new = bdrv_info_stats,
2668 },
2669 {
2670 .name = "cpus",
2671 .args_type = "",
2672 .params = "",
2673 .help = "show infos for each CPU",
2674 .user_print = monitor_print_cpus,
2675 .mhandler.info_new = do_info_cpus,
2676 },
2677 {
2678 .name = "pci",
2679 .args_type = "",
2680 .params = "",
2681 .help = "show PCI info",
2682 .user_print = do_pci_info_print,
2683 .mhandler.info_new = do_pci_info,
2684 },
2685 {
2686 .name = "kvm",
2687 .args_type = "",
2688 .params = "",
2689 .help = "show KVM information",
2690 .user_print = do_info_kvm_print,
2691 .mhandler.info_new = do_info_kvm,
2692 },
2693 {
2694 .name = "status",
2695 .args_type = "",
2696 .params = "",
2697 .help = "show the current VM status (running|paused)",
2698 .user_print = do_info_status_print,
2699 .mhandler.info_new = do_info_status,
2700 },
2701 {
2702 .name = "mice",
2703 .args_type = "",
2704 .params = "",
2705 .help = "show which guest mouse is receiving events",
2706 .user_print = do_info_mice_print,
2707 .mhandler.info_new = do_info_mice,
2708 },
2709 {
2710 .name = "vnc",
2711 .args_type = "",
2712 .params = "",
2713 .help = "show the vnc server status",
2714 .user_print = do_info_vnc_print,
2715 .mhandler.info_new = do_info_vnc,
2716 },
2717 {
2718 .name = "name",
2719 .args_type = "",
2720 .params = "",
2721 .help = "show the current VM name",
2722 .user_print = do_info_name_print,
2723 .mhandler.info_new = do_info_name,
2724 },
2725 {
2726 .name = "uuid",
2727 .args_type = "",
2728 .params = "",
2729 .help = "show the current VM UUID",
2730 .user_print = do_info_uuid_print,
2731 .mhandler.info_new = do_info_uuid,
2732 },
2733 {
2734 .name = "migrate",
2735 .args_type = "",
2736 .params = "",
2737 .help = "show migration status",
2738 .user_print = do_info_migrate_print,
2739 .mhandler.info_new = do_info_migrate,
2740 },
2741 {
2742 .name = "balloon",
2743 .args_type = "",
2744 .params = "",
2745 .help = "show balloon information",
2746 .user_print = monitor_print_balloon,
2747 .mhandler.info_async = do_info_balloon,
2748 .flags = MONITOR_CMD_ASYNC,
2749 },
2750 { /* NULL */ },
2751};
2752
bellard9307c4c2004-04-04 12:57:25 +00002753/*******************************************************************/
2754
2755static const char *pch;
2756static jmp_buf expr_env;
2757
bellard92a31b12005-02-10 22:00:52 +00002758#define MD_TLONG 0
2759#define MD_I32 1
2760
bellard9307c4c2004-04-04 12:57:25 +00002761typedef struct MonitorDef {
2762 const char *name;
2763 int offset;
blueswir18662d652008-10-02 18:32:44 +00002764 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002765 int type;
bellard9307c4c2004-04-04 12:57:25 +00002766} MonitorDef;
2767
bellard57206fd2004-04-25 18:54:52 +00002768#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002769static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002770{
bellard6a00d602005-11-21 23:25:50 +00002771 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002772 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002773}
2774#endif
2775
bellarda541f292004-04-12 20:39:29 +00002776#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002777static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002778{
bellard6a00d602005-11-21 23:25:50 +00002779 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002780 unsigned int u;
2781 int i;
2782
2783 u = 0;
2784 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002785 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002786
2787 return u;
2788}
2789
blueswir18662d652008-10-02 18:32:44 +00002790static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002791{
bellard6a00d602005-11-21 23:25:50 +00002792 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002793 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002794}
2795
blueswir18662d652008-10-02 18:32:44 +00002796static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002797{
bellard6a00d602005-11-21 23:25:50 +00002798 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002799 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002800}
bellard9fddaa02004-05-21 12:59:32 +00002801
blueswir18662d652008-10-02 18:32:44 +00002802static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002803{
bellard6a00d602005-11-21 23:25:50 +00002804 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002805 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002806}
2807
blueswir18662d652008-10-02 18:32:44 +00002808static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002809{
bellard6a00d602005-11-21 23:25:50 +00002810 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002811 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002812}
2813
blueswir18662d652008-10-02 18:32:44 +00002814static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002815{
bellard6a00d602005-11-21 23:25:50 +00002816 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002817 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002818}
bellarda541f292004-04-12 20:39:29 +00002819#endif
2820
bellarde95c8d52004-09-30 22:22:08 +00002821#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002822#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002823static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002824{
bellard6a00d602005-11-21 23:25:50 +00002825 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002826
2827 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002828}
bellard7b936c02005-10-30 17:05:13 +00002829#endif
bellarde95c8d52004-09-30 22:22:08 +00002830
blueswir18662d652008-10-02 18:32:44 +00002831static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002832{
bellard6a00d602005-11-21 23:25:50 +00002833 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002834 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002835}
2836#endif
2837
blueswir18662d652008-10-02 18:32:44 +00002838static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002839#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002840
2841#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002842 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002843 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002844 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002845
bellard9307c4c2004-04-04 12:57:25 +00002846 { "eax", offsetof(CPUState, regs[0]) },
2847 { "ecx", offsetof(CPUState, regs[1]) },
2848 { "edx", offsetof(CPUState, regs[2]) },
2849 { "ebx", offsetof(CPUState, regs[3]) },
2850 { "esp|sp", offsetof(CPUState, regs[4]) },
2851 { "ebp|fp", offsetof(CPUState, regs[5]) },
2852 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002853 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002854#ifdef TARGET_X86_64
2855 { "r8", offsetof(CPUState, regs[8]) },
2856 { "r9", offsetof(CPUState, regs[9]) },
2857 { "r10", offsetof(CPUState, regs[10]) },
2858 { "r11", offsetof(CPUState, regs[11]) },
2859 { "r12", offsetof(CPUState, regs[12]) },
2860 { "r13", offsetof(CPUState, regs[13]) },
2861 { "r14", offsetof(CPUState, regs[14]) },
2862 { "r15", offsetof(CPUState, regs[15]) },
2863#endif
bellard9307c4c2004-04-04 12:57:25 +00002864 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002865 { "eip", offsetof(CPUState, eip) },
2866 SEG("cs", R_CS)
2867 SEG("ds", R_DS)
2868 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002869 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002870 SEG("fs", R_FS)
2871 SEG("gs", R_GS)
2872 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002873#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002874 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002875 { "r0", offsetof(CPUState, gpr[0]) },
2876 { "r1", offsetof(CPUState, gpr[1]) },
2877 { "r2", offsetof(CPUState, gpr[2]) },
2878 { "r3", offsetof(CPUState, gpr[3]) },
2879 { "r4", offsetof(CPUState, gpr[4]) },
2880 { "r5", offsetof(CPUState, gpr[5]) },
2881 { "r6", offsetof(CPUState, gpr[6]) },
2882 { "r7", offsetof(CPUState, gpr[7]) },
2883 { "r8", offsetof(CPUState, gpr[8]) },
2884 { "r9", offsetof(CPUState, gpr[9]) },
2885 { "r10", offsetof(CPUState, gpr[10]) },
2886 { "r11", offsetof(CPUState, gpr[11]) },
2887 { "r12", offsetof(CPUState, gpr[12]) },
2888 { "r13", offsetof(CPUState, gpr[13]) },
2889 { "r14", offsetof(CPUState, gpr[14]) },
2890 { "r15", offsetof(CPUState, gpr[15]) },
2891 { "r16", offsetof(CPUState, gpr[16]) },
2892 { "r17", offsetof(CPUState, gpr[17]) },
2893 { "r18", offsetof(CPUState, gpr[18]) },
2894 { "r19", offsetof(CPUState, gpr[19]) },
2895 { "r20", offsetof(CPUState, gpr[20]) },
2896 { "r21", offsetof(CPUState, gpr[21]) },
2897 { "r22", offsetof(CPUState, gpr[22]) },
2898 { "r23", offsetof(CPUState, gpr[23]) },
2899 { "r24", offsetof(CPUState, gpr[24]) },
2900 { "r25", offsetof(CPUState, gpr[25]) },
2901 { "r26", offsetof(CPUState, gpr[26]) },
2902 { "r27", offsetof(CPUState, gpr[27]) },
2903 { "r28", offsetof(CPUState, gpr[28]) },
2904 { "r29", offsetof(CPUState, gpr[29]) },
2905 { "r30", offsetof(CPUState, gpr[30]) },
2906 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002907 /* Floating point registers */
2908 { "f0", offsetof(CPUState, fpr[0]) },
2909 { "f1", offsetof(CPUState, fpr[1]) },
2910 { "f2", offsetof(CPUState, fpr[2]) },
2911 { "f3", offsetof(CPUState, fpr[3]) },
2912 { "f4", offsetof(CPUState, fpr[4]) },
2913 { "f5", offsetof(CPUState, fpr[5]) },
2914 { "f6", offsetof(CPUState, fpr[6]) },
2915 { "f7", offsetof(CPUState, fpr[7]) },
2916 { "f8", offsetof(CPUState, fpr[8]) },
2917 { "f9", offsetof(CPUState, fpr[9]) },
2918 { "f10", offsetof(CPUState, fpr[10]) },
2919 { "f11", offsetof(CPUState, fpr[11]) },
2920 { "f12", offsetof(CPUState, fpr[12]) },
2921 { "f13", offsetof(CPUState, fpr[13]) },
2922 { "f14", offsetof(CPUState, fpr[14]) },
2923 { "f15", offsetof(CPUState, fpr[15]) },
2924 { "f16", offsetof(CPUState, fpr[16]) },
2925 { "f17", offsetof(CPUState, fpr[17]) },
2926 { "f18", offsetof(CPUState, fpr[18]) },
2927 { "f19", offsetof(CPUState, fpr[19]) },
2928 { "f20", offsetof(CPUState, fpr[20]) },
2929 { "f21", offsetof(CPUState, fpr[21]) },
2930 { "f22", offsetof(CPUState, fpr[22]) },
2931 { "f23", offsetof(CPUState, fpr[23]) },
2932 { "f24", offsetof(CPUState, fpr[24]) },
2933 { "f25", offsetof(CPUState, fpr[25]) },
2934 { "f26", offsetof(CPUState, fpr[26]) },
2935 { "f27", offsetof(CPUState, fpr[27]) },
2936 { "f28", offsetof(CPUState, fpr[28]) },
2937 { "f29", offsetof(CPUState, fpr[29]) },
2938 { "f30", offsetof(CPUState, fpr[30]) },
2939 { "f31", offsetof(CPUState, fpr[31]) },
2940 { "fpscr", offsetof(CPUState, fpscr) },
2941 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002942 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002943 { "lr", offsetof(CPUState, lr) },
2944 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002945 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002946 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002947 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002948 { "msr", 0, &monitor_get_msr, },
2949 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002950 { "tbu", 0, &monitor_get_tbu, },
2951 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002952#if defined(TARGET_PPC64)
2953 /* Address space register */
2954 { "asr", offsetof(CPUState, asr) },
2955#endif
2956 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002957 { "sdr1", offsetof(CPUState, sdr1) },
2958 { "sr0", offsetof(CPUState, sr[0]) },
2959 { "sr1", offsetof(CPUState, sr[1]) },
2960 { "sr2", offsetof(CPUState, sr[2]) },
2961 { "sr3", offsetof(CPUState, sr[3]) },
2962 { "sr4", offsetof(CPUState, sr[4]) },
2963 { "sr5", offsetof(CPUState, sr[5]) },
2964 { "sr6", offsetof(CPUState, sr[6]) },
2965 { "sr7", offsetof(CPUState, sr[7]) },
2966 { "sr8", offsetof(CPUState, sr[8]) },
2967 { "sr9", offsetof(CPUState, sr[9]) },
2968 { "sr10", offsetof(CPUState, sr[10]) },
2969 { "sr11", offsetof(CPUState, sr[11]) },
2970 { "sr12", offsetof(CPUState, sr[12]) },
2971 { "sr13", offsetof(CPUState, sr[13]) },
2972 { "sr14", offsetof(CPUState, sr[14]) },
2973 { "sr15", offsetof(CPUState, sr[15]) },
2974 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002975#elif defined(TARGET_SPARC)
2976 { "g0", offsetof(CPUState, gregs[0]) },
2977 { "g1", offsetof(CPUState, gregs[1]) },
2978 { "g2", offsetof(CPUState, gregs[2]) },
2979 { "g3", offsetof(CPUState, gregs[3]) },
2980 { "g4", offsetof(CPUState, gregs[4]) },
2981 { "g5", offsetof(CPUState, gregs[5]) },
2982 { "g6", offsetof(CPUState, gregs[6]) },
2983 { "g7", offsetof(CPUState, gregs[7]) },
2984 { "o0", 0, monitor_get_reg },
2985 { "o1", 1, monitor_get_reg },
2986 { "o2", 2, monitor_get_reg },
2987 { "o3", 3, monitor_get_reg },
2988 { "o4", 4, monitor_get_reg },
2989 { "o5", 5, monitor_get_reg },
2990 { "o6", 6, monitor_get_reg },
2991 { "o7", 7, monitor_get_reg },
2992 { "l0", 8, monitor_get_reg },
2993 { "l1", 9, monitor_get_reg },
2994 { "l2", 10, monitor_get_reg },
2995 { "l3", 11, monitor_get_reg },
2996 { "l4", 12, monitor_get_reg },
2997 { "l5", 13, monitor_get_reg },
2998 { "l6", 14, monitor_get_reg },
2999 { "l7", 15, monitor_get_reg },
3000 { "i0", 16, monitor_get_reg },
3001 { "i1", 17, monitor_get_reg },
3002 { "i2", 18, monitor_get_reg },
3003 { "i3", 19, monitor_get_reg },
3004 { "i4", 20, monitor_get_reg },
3005 { "i5", 21, monitor_get_reg },
3006 { "i6", 22, monitor_get_reg },
3007 { "i7", 23, monitor_get_reg },
3008 { "pc", offsetof(CPUState, pc) },
3009 { "npc", offsetof(CPUState, npc) },
3010 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003011#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003012 { "psr", 0, &monitor_get_psr, },
3013 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003014#endif
bellarde95c8d52004-09-30 22:22:08 +00003015 { "tbr", offsetof(CPUState, tbr) },
3016 { "fsr", offsetof(CPUState, fsr) },
3017 { "f0", offsetof(CPUState, fpr[0]) },
3018 { "f1", offsetof(CPUState, fpr[1]) },
3019 { "f2", offsetof(CPUState, fpr[2]) },
3020 { "f3", offsetof(CPUState, fpr[3]) },
3021 { "f4", offsetof(CPUState, fpr[4]) },
3022 { "f5", offsetof(CPUState, fpr[5]) },
3023 { "f6", offsetof(CPUState, fpr[6]) },
3024 { "f7", offsetof(CPUState, fpr[7]) },
3025 { "f8", offsetof(CPUState, fpr[8]) },
3026 { "f9", offsetof(CPUState, fpr[9]) },
3027 { "f10", offsetof(CPUState, fpr[10]) },
3028 { "f11", offsetof(CPUState, fpr[11]) },
3029 { "f12", offsetof(CPUState, fpr[12]) },
3030 { "f13", offsetof(CPUState, fpr[13]) },
3031 { "f14", offsetof(CPUState, fpr[14]) },
3032 { "f15", offsetof(CPUState, fpr[15]) },
3033 { "f16", offsetof(CPUState, fpr[16]) },
3034 { "f17", offsetof(CPUState, fpr[17]) },
3035 { "f18", offsetof(CPUState, fpr[18]) },
3036 { "f19", offsetof(CPUState, fpr[19]) },
3037 { "f20", offsetof(CPUState, fpr[20]) },
3038 { "f21", offsetof(CPUState, fpr[21]) },
3039 { "f22", offsetof(CPUState, fpr[22]) },
3040 { "f23", offsetof(CPUState, fpr[23]) },
3041 { "f24", offsetof(CPUState, fpr[24]) },
3042 { "f25", offsetof(CPUState, fpr[25]) },
3043 { "f26", offsetof(CPUState, fpr[26]) },
3044 { "f27", offsetof(CPUState, fpr[27]) },
3045 { "f28", offsetof(CPUState, fpr[28]) },
3046 { "f29", offsetof(CPUState, fpr[29]) },
3047 { "f30", offsetof(CPUState, fpr[30]) },
3048 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003049#ifdef TARGET_SPARC64
3050 { "f32", offsetof(CPUState, fpr[32]) },
3051 { "f34", offsetof(CPUState, fpr[34]) },
3052 { "f36", offsetof(CPUState, fpr[36]) },
3053 { "f38", offsetof(CPUState, fpr[38]) },
3054 { "f40", offsetof(CPUState, fpr[40]) },
3055 { "f42", offsetof(CPUState, fpr[42]) },
3056 { "f44", offsetof(CPUState, fpr[44]) },
3057 { "f46", offsetof(CPUState, fpr[46]) },
3058 { "f48", offsetof(CPUState, fpr[48]) },
3059 { "f50", offsetof(CPUState, fpr[50]) },
3060 { "f52", offsetof(CPUState, fpr[52]) },
3061 { "f54", offsetof(CPUState, fpr[54]) },
3062 { "f56", offsetof(CPUState, fpr[56]) },
3063 { "f58", offsetof(CPUState, fpr[58]) },
3064 { "f60", offsetof(CPUState, fpr[60]) },
3065 { "f62", offsetof(CPUState, fpr[62]) },
3066 { "asi", offsetof(CPUState, asi) },
3067 { "pstate", offsetof(CPUState, pstate) },
3068 { "cansave", offsetof(CPUState, cansave) },
3069 { "canrestore", offsetof(CPUState, canrestore) },
3070 { "otherwin", offsetof(CPUState, otherwin) },
3071 { "wstate", offsetof(CPUState, wstate) },
3072 { "cleanwin", offsetof(CPUState, cleanwin) },
3073 { "fprs", offsetof(CPUState, fprs) },
3074#endif
bellard9307c4c2004-04-04 12:57:25 +00003075#endif
3076 { NULL },
3077};
3078
aliguori376253e2009-03-05 23:01:23 +00003079static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003080{
aliguori376253e2009-03-05 23:01:23 +00003081 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003082 longjmp(expr_env, 1);
3083}
3084
Markus Armbruster09b94182010-01-20 13:07:30 +01003085/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003086static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003087{
blueswir18662d652008-10-02 18:32:44 +00003088 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003089 void *ptr;
3090
bellard9307c4c2004-04-04 12:57:25 +00003091 for(md = monitor_defs; md->name != NULL; md++) {
3092 if (compare_cmd(name, md->name)) {
3093 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003094 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003095 } else {
bellard6a00d602005-11-21 23:25:50 +00003096 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003097 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003098 switch(md->type) {
3099 case MD_I32:
3100 *pval = *(int32_t *)ptr;
3101 break;
3102 case MD_TLONG:
3103 *pval = *(target_long *)ptr;
3104 break;
3105 default:
3106 *pval = 0;
3107 break;
3108 }
bellard9307c4c2004-04-04 12:57:25 +00003109 }
3110 return 0;
3111 }
3112 }
3113 return -1;
3114}
3115
3116static void next(void)
3117{
Blue Swirl660f11b2009-07-31 21:16:51 +00003118 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003119 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003120 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003121 pch++;
3122 }
3123}
3124
aliguori376253e2009-03-05 23:01:23 +00003125static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003126
aliguori376253e2009-03-05 23:01:23 +00003127static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003128{
blueswir1c2efc952007-09-25 17:28:42 +00003129 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003130 char *p;
bellard6a00d602005-11-21 23:25:50 +00003131 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003132
3133 switch(*pch) {
3134 case '+':
3135 next();
aliguori376253e2009-03-05 23:01:23 +00003136 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003137 break;
3138 case '-':
3139 next();
aliguori376253e2009-03-05 23:01:23 +00003140 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003141 break;
3142 case '~':
3143 next();
aliguori376253e2009-03-05 23:01:23 +00003144 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003145 break;
3146 case '(':
3147 next();
aliguori376253e2009-03-05 23:01:23 +00003148 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003149 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003150 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003151 }
3152 next();
3153 break;
bellard81d09122004-07-14 17:21:37 +00003154 case '\'':
3155 pch++;
3156 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003157 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003158 n = *pch;
3159 pch++;
3160 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003161 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003162 next();
3163 break;
bellard9307c4c2004-04-04 12:57:25 +00003164 case '$':
3165 {
3166 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003167 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003168
bellard9307c4c2004-04-04 12:57:25 +00003169 pch++;
3170 q = buf;
3171 while ((*pch >= 'a' && *pch <= 'z') ||
3172 (*pch >= 'A' && *pch <= 'Z') ||
3173 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003174 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003175 if ((q - buf) < sizeof(buf) - 1)
3176 *q++ = *pch;
3177 pch++;
3178 }
blueswir1cd390082008-11-16 13:53:32 +00003179 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003180 pch++;
3181 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003182 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003183 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003184 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003185 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003186 }
3187 break;
3188 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003189 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003190 n = 0;
3191 break;
3192 default:
blueswir17743e582007-09-24 18:39:04 +00003193#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003194 n = strtoull(pch, &p, 0);
3195#else
bellard9307c4c2004-04-04 12:57:25 +00003196 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003197#endif
bellard9307c4c2004-04-04 12:57:25 +00003198 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003199 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003200 }
3201 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003202 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003203 pch++;
3204 break;
3205 }
3206 return n;
3207}
3208
3209
aliguori376253e2009-03-05 23:01:23 +00003210static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003211{
blueswir1c2efc952007-09-25 17:28:42 +00003212 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003213 int op;
ths3b46e622007-09-17 08:09:54 +00003214
aliguori376253e2009-03-05 23:01:23 +00003215 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003216 for(;;) {
3217 op = *pch;
3218 if (op != '*' && op != '/' && op != '%')
3219 break;
3220 next();
aliguori376253e2009-03-05 23:01:23 +00003221 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003222 switch(op) {
3223 default:
3224 case '*':
3225 val *= val2;
3226 break;
3227 case '/':
3228 case '%':
ths5fafdf22007-09-16 21:08:06 +00003229 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003230 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003231 if (op == '/')
3232 val /= val2;
3233 else
3234 val %= val2;
3235 break;
3236 }
3237 }
3238 return val;
3239}
3240
aliguori376253e2009-03-05 23:01:23 +00003241static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003242{
blueswir1c2efc952007-09-25 17:28:42 +00003243 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003244 int op;
bellard9307c4c2004-04-04 12:57:25 +00003245
aliguori376253e2009-03-05 23:01:23 +00003246 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003247 for(;;) {
3248 op = *pch;
3249 if (op != '&' && op != '|' && op != '^')
3250 break;
3251 next();
aliguori376253e2009-03-05 23:01:23 +00003252 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003253 switch(op) {
3254 default:
3255 case '&':
3256 val &= val2;
3257 break;
3258 case '|':
3259 val |= val2;
3260 break;
3261 case '^':
3262 val ^= val2;
3263 break;
3264 }
3265 }
3266 return val;
3267}
3268
aliguori376253e2009-03-05 23:01:23 +00003269static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003270{
blueswir1c2efc952007-09-25 17:28:42 +00003271 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003272 int op;
bellard9307c4c2004-04-04 12:57:25 +00003273
aliguori376253e2009-03-05 23:01:23 +00003274 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003275 for(;;) {
3276 op = *pch;
3277 if (op != '+' && op != '-')
3278 break;
3279 next();
aliguori376253e2009-03-05 23:01:23 +00003280 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003281 if (op == '+')
3282 val += val2;
3283 else
3284 val -= val2;
3285 }
3286 return val;
3287}
3288
aliguori376253e2009-03-05 23:01:23 +00003289static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003290{
3291 pch = *pp;
3292 if (setjmp(expr_env)) {
3293 *pp = pch;
3294 return -1;
3295 }
blueswir1cd390082008-11-16 13:53:32 +00003296 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003297 pch++;
aliguori376253e2009-03-05 23:01:23 +00003298 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003299 *pp = pch;
3300 return 0;
3301}
3302
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003303static int get_double(Monitor *mon, double *pval, const char **pp)
3304{
3305 const char *p = *pp;
3306 char *tailp;
3307 double d;
3308
3309 d = strtod(p, &tailp);
3310 if (tailp == p) {
3311 monitor_printf(mon, "Number expected\n");
3312 return -1;
3313 }
3314 if (d != d || d - d != 0) {
3315 /* NaN or infinity */
3316 monitor_printf(mon, "Bad number\n");
3317 return -1;
3318 }
3319 *pval = d;
3320 *pp = tailp;
3321 return 0;
3322}
3323
bellard9307c4c2004-04-04 12:57:25 +00003324static int get_str(char *buf, int buf_size, const char **pp)
3325{
3326 const char *p;
3327 char *q;
3328 int c;
3329
bellard81d09122004-07-14 17:21:37 +00003330 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003331 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003332 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003333 p++;
3334 if (*p == '\0') {
3335 fail:
bellard81d09122004-07-14 17:21:37 +00003336 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003337 *pp = p;
3338 return -1;
3339 }
bellard9307c4c2004-04-04 12:57:25 +00003340 if (*p == '\"') {
3341 p++;
3342 while (*p != '\0' && *p != '\"') {
3343 if (*p == '\\') {
3344 p++;
3345 c = *p++;
3346 switch(c) {
3347 case 'n':
3348 c = '\n';
3349 break;
3350 case 'r':
3351 c = '\r';
3352 break;
3353 case '\\':
3354 case '\'':
3355 case '\"':
3356 break;
3357 default:
3358 qemu_printf("unsupported escape code: '\\%c'\n", c);
3359 goto fail;
3360 }
3361 if ((q - buf) < buf_size - 1) {
3362 *q++ = c;
3363 }
3364 } else {
3365 if ((q - buf) < buf_size - 1) {
3366 *q++ = *p;
3367 }
3368 p++;
3369 }
3370 }
3371 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003372 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003373 goto fail;
3374 }
3375 p++;
3376 } else {
blueswir1cd390082008-11-16 13:53:32 +00003377 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003378 if ((q - buf) < buf_size - 1) {
3379 *q++ = *p;
3380 }
3381 p++;
3382 }
bellard9307c4c2004-04-04 12:57:25 +00003383 }
bellard81d09122004-07-14 17:21:37 +00003384 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003385 *pp = p;
3386 return 0;
3387}
3388
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003389/*
3390 * Store the command-name in cmdname, and return a pointer to
3391 * the remaining of the command string.
3392 */
3393static const char *get_command_name(const char *cmdline,
3394 char *cmdname, size_t nlen)
3395{
3396 size_t len;
3397 const char *p, *pstart;
3398
3399 p = cmdline;
3400 while (qemu_isspace(*p))
3401 p++;
3402 if (*p == '\0')
3403 return NULL;
3404 pstart = p;
3405 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3406 p++;
3407 len = p - pstart;
3408 if (len > nlen - 1)
3409 len = nlen - 1;
3410 memcpy(cmdname, pstart, len);
3411 cmdname[len] = '\0';
3412 return p;
3413}
3414
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003415/**
3416 * Read key of 'type' into 'key' and return the current
3417 * 'type' pointer.
3418 */
3419static char *key_get_info(const char *type, char **key)
3420{
3421 size_t len;
3422 char *p, *str;
3423
3424 if (*type == ',')
3425 type++;
3426
3427 p = strchr(type, ':');
3428 if (!p) {
3429 *key = NULL;
3430 return NULL;
3431 }
3432 len = p - type;
3433
3434 str = qemu_malloc(len + 1);
3435 memcpy(str, type, len);
3436 str[len] = '\0';
3437
3438 *key = str;
3439 return ++p;
3440}
3441
bellard9307c4c2004-04-04 12:57:25 +00003442static int default_fmt_format = 'x';
3443static int default_fmt_size = 4;
3444
3445#define MAX_ARGS 16
3446
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003447static int is_valid_option(const char *c, const char *typestr)
3448{
3449 char option[3];
3450
3451 option[0] = '-';
3452 option[1] = *c;
3453 option[2] = '\0';
3454
3455 typestr = strstr(typestr, option);
3456 return (typestr != NULL);
3457}
3458
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003459static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3460 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003461{
3462 const mon_cmd_t *cmd;
3463
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003464 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003465 if (compare_cmd(cmdname, cmd->name)) {
3466 return cmd;
3467 }
3468 }
3469
3470 return NULL;
3471}
3472
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003473static const mon_cmd_t *monitor_find_command(const char *cmdname)
3474{
3475 return search_dispatch_table(mon_cmds, cmdname);
3476}
3477
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003478static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3479{
Luiz Capitulino3e12a752010-09-15 17:20:33 -03003480 return search_dispatch_table(qmp_query_cmds, info_item);
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003481}
3482
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003483static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3484{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003485 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003486}
3487
Anthony Liguoric227f092009-10-01 16:12:16 -05003488static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003489 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003490 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003491{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003492 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003493 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003494 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003495 char cmdname[256];
3496 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003497 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003498
3499#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003500 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003501#endif
ths3b46e622007-09-17 08:09:54 +00003502
bellard9307c4c2004-04-04 12:57:25 +00003503 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003504 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3505 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003506 return NULL;
ths3b46e622007-09-17 08:09:54 +00003507
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003508 cmd = monitor_find_command(cmdname);
3509 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003510 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003511 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003512 }
bellard9307c4c2004-04-04 12:57:25 +00003513
bellard9307c4c2004-04-04 12:57:25 +00003514 /* parse the parameters */
3515 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003516 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003517 typestr = key_get_info(typestr, &key);
3518 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003519 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003520 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003521 typestr++;
3522 switch(c) {
3523 case 'F':
bellard81d09122004-07-14 17:21:37 +00003524 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003525 case 's':
3526 {
3527 int ret;
ths3b46e622007-09-17 08:09:54 +00003528
blueswir1cd390082008-11-16 13:53:32 +00003529 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003530 p++;
3531 if (*typestr == '?') {
3532 typestr++;
3533 if (*p == '\0') {
3534 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003535 break;
bellard9307c4c2004-04-04 12:57:25 +00003536 }
3537 }
3538 ret = get_str(buf, sizeof(buf), &p);
3539 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003540 switch(c) {
3541 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003542 monitor_printf(mon, "%s: filename expected\n",
3543 cmdname);
bellard81d09122004-07-14 17:21:37 +00003544 break;
3545 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003546 monitor_printf(mon, "%s: block device name expected\n",
3547 cmdname);
bellard81d09122004-07-14 17:21:37 +00003548 break;
3549 default:
aliguori376253e2009-03-05 23:01:23 +00003550 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003551 break;
3552 }
bellard9307c4c2004-04-04 12:57:25 +00003553 goto fail;
3554 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003555 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003556 }
3557 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003558 case 'O':
3559 {
3560 QemuOptsList *opts_list;
3561 QemuOpts *opts;
3562
3563 opts_list = qemu_find_opts(key);
3564 if (!opts_list || opts_list->desc->name) {
3565 goto bad_type;
3566 }
3567 while (qemu_isspace(*p)) {
3568 p++;
3569 }
3570 if (!*p)
3571 break;
3572 if (get_str(buf, sizeof(buf), &p) < 0) {
3573 goto fail;
3574 }
3575 opts = qemu_opts_parse(opts_list, buf, 1);
3576 if (!opts) {
3577 goto fail;
3578 }
3579 qemu_opts_to_qdict(opts, qdict);
3580 qemu_opts_del(opts);
3581 }
3582 break;
bellard9307c4c2004-04-04 12:57:25 +00003583 case '/':
3584 {
3585 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003586
blueswir1cd390082008-11-16 13:53:32 +00003587 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003588 p++;
3589 if (*p == '/') {
3590 /* format found */
3591 p++;
3592 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003593 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003594 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003595 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003596 count = count * 10 + (*p - '0');
3597 p++;
3598 }
3599 }
3600 size = -1;
3601 format = -1;
3602 for(;;) {
3603 switch(*p) {
3604 case 'o':
3605 case 'd':
3606 case 'u':
3607 case 'x':
3608 case 'i':
3609 case 'c':
3610 format = *p++;
3611 break;
3612 case 'b':
3613 size = 1;
3614 p++;
3615 break;
3616 case 'h':
3617 size = 2;
3618 p++;
3619 break;
3620 case 'w':
3621 size = 4;
3622 p++;
3623 break;
3624 case 'g':
3625 case 'L':
3626 size = 8;
3627 p++;
3628 break;
3629 default:
3630 goto next;
3631 }
3632 }
3633 next:
blueswir1cd390082008-11-16 13:53:32 +00003634 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003635 monitor_printf(mon, "invalid char in format: '%c'\n",
3636 *p);
bellard9307c4c2004-04-04 12:57:25 +00003637 goto fail;
3638 }
bellard9307c4c2004-04-04 12:57:25 +00003639 if (format < 0)
3640 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003641 if (format != 'i') {
3642 /* for 'i', not specifying a size gives -1 as size */
3643 if (size < 0)
3644 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003645 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003646 }
bellard9307c4c2004-04-04 12:57:25 +00003647 default_fmt_format = format;
3648 } else {
3649 count = 1;
3650 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003651 if (format != 'i') {
3652 size = default_fmt_size;
3653 } else {
3654 size = -1;
3655 }
bellard9307c4c2004-04-04 12:57:25 +00003656 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003657 qdict_put(qdict, "count", qint_from_int(count));
3658 qdict_put(qdict, "format", qint_from_int(format));
3659 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003660 }
3661 break;
3662 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003663 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003664 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003665 {
blueswir1c2efc952007-09-25 17:28:42 +00003666 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003667
blueswir1cd390082008-11-16 13:53:32 +00003668 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003669 p++;
bellard34405572004-06-08 00:55:58 +00003670 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003671 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003672 if (*p == '\0') {
3673 typestr++;
3674 break;
3675 }
bellard34405572004-06-08 00:55:58 +00003676 } else {
3677 if (*p == '.') {
3678 p++;
blueswir1cd390082008-11-16 13:53:32 +00003679 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003680 p++;
bellard34405572004-06-08 00:55:58 +00003681 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003682 typestr++;
3683 break;
bellard34405572004-06-08 00:55:58 +00003684 }
3685 }
bellard13224a82006-07-14 22:03:35 +00003686 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003687 }
aliguori376253e2009-03-05 23:01:23 +00003688 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003689 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003690 /* Check if 'i' is greater than 32-bit */
3691 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3692 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3693 monitor_printf(mon, "integer is for 32-bit values\n");
3694 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003695 } else if (c == 'M') {
3696 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003697 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003698 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003699 }
3700 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003701 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003702 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003703 {
3704 double val;
3705
3706 while (qemu_isspace(*p))
3707 p++;
3708 if (*typestr == '?') {
3709 typestr++;
3710 if (*p == '\0') {
3711 break;
3712 }
3713 }
3714 if (get_double(mon, &val, &p) < 0) {
3715 goto fail;
3716 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003717 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003718 switch (*p) {
3719 case 'K': case 'k':
3720 val *= 1 << 10; p++; break;
3721 case 'M': case 'm':
3722 val *= 1 << 20; p++; break;
3723 case 'G': case 'g':
3724 val *= 1 << 30; p++; break;
3725 }
3726 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003727 if (c == 'T' && p[0] && p[1] == 's') {
3728 switch (*p) {
3729 case 'm':
3730 val /= 1e3; p += 2; break;
3731 case 'u':
3732 val /= 1e6; p += 2; break;
3733 case 'n':
3734 val /= 1e9; p += 2; break;
3735 }
3736 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003737 if (*p && !qemu_isspace(*p)) {
3738 monitor_printf(mon, "Unknown unit suffix\n");
3739 goto fail;
3740 }
3741 qdict_put(qdict, key, qfloat_from_double(val));
3742 }
3743 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003744 case 'b':
3745 {
3746 const char *beg;
3747 int val;
3748
3749 while (qemu_isspace(*p)) {
3750 p++;
3751 }
3752 beg = p;
3753 while (qemu_isgraph(*p)) {
3754 p++;
3755 }
3756 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3757 val = 1;
3758 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3759 val = 0;
3760 } else {
3761 monitor_printf(mon, "Expected 'on' or 'off'\n");
3762 goto fail;
3763 }
3764 qdict_put(qdict, key, qbool_from_int(val));
3765 }
3766 break;
bellard9307c4c2004-04-04 12:57:25 +00003767 case '-':
3768 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003769 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003770 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003771 /* option */
ths3b46e622007-09-17 08:09:54 +00003772
bellard9307c4c2004-04-04 12:57:25 +00003773 c = *typestr++;
3774 if (c == '\0')
3775 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003776 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003777 p++;
bellard9307c4c2004-04-04 12:57:25 +00003778 if (*p == '-') {
3779 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003780 if(c != *p) {
3781 if(!is_valid_option(p, typestr)) {
3782
3783 monitor_printf(mon, "%s: unsupported option -%c\n",
3784 cmdname, *p);
3785 goto fail;
3786 } else {
3787 skip_key = 1;
3788 }
bellard9307c4c2004-04-04 12:57:25 +00003789 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003790 if(skip_key) {
3791 p = tmp;
3792 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003793 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003794 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003795 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003796 }
bellard9307c4c2004-04-04 12:57:25 +00003797 }
bellard9307c4c2004-04-04 12:57:25 +00003798 }
3799 break;
3800 default:
3801 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003802 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003803 goto fail;
3804 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003805 qemu_free(key);
3806 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003807 }
3808 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003809 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003810 p++;
3811 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003812 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3813 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003814 goto fail;
3815 }
3816
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003817 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003818
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003819fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003820 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003821 return NULL;
3822}
3823
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003824void monitor_set_error(Monitor *mon, QError *qerror)
3825{
3826 /* report only the first error */
3827 if (!mon->error) {
3828 mon->error = qerror;
3829 } else {
3830 MON_DEBUG("Additional error report at %s:%d\n",
3831 qerror->file, qerror->linenr);
3832 QDECREF(qerror);
3833 }
3834}
3835
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003836static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3837{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003838 if (monitor_ctrl_mode(mon)) {
3839 if (ret && !monitor_has_error(mon)) {
3840 /*
3841 * If it returns failure, it must have passed on error.
3842 *
3843 * Action: Report an internal error to the client if in QMP.
3844 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003845 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003846 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3847 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003848 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003849
3850#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003851 if (!ret && monitor_has_error(mon)) {
3852 /*
3853 * If it returns success, it must not have passed an error.
3854 *
3855 * Action: Report the passed error to the client.
3856 */
3857 MON_DEBUG("command '%s' returned success but passed an error\n",
3858 cmd->name);
3859 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003860
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003861 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3862 /*
3863 * Handlers should not call Monitor print functions.
3864 *
3865 * Action: Ignore them in QMP.
3866 *
3867 * (XXX: we don't check any 'info' or 'query' command here
3868 * because the user print function _is_ called by do_info(), hence
3869 * we will trigger this check. This problem will go away when we
3870 * make 'query' commands real and kill do_info())
3871 */
3872 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3873 cmd->name, mon_print_count_get(mon));
3874 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003875#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003876 } else {
3877 assert(!monitor_has_error(mon));
3878 QDECREF(mon->error);
3879 mon->error = NULL;
3880 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003881}
3882
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003883static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3884 const QDict *params)
3885{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003886 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003887 QObject *data = NULL;
3888
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003889 mon_print_count_init(mon);
3890
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003891 ret = cmd->mhandler.cmd_new(mon, params, &data);
3892 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003893
Luiz Capitulinod4551292010-09-15 16:56:48 -03003894 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003895 /* Monitor Protocol */
3896 monitor_protocol_emitter(mon, data);
3897 } else {
3898 /* User Protocol */
3899 if (data)
3900 cmd->user_print(mon, data);
3901 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003902
3903 qobject_decref(data);
3904}
3905
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003906static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003907{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003908 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003909 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003910
3911 qdict = qdict_new();
3912
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003913 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003914 if (!cmd)
3915 goto out;
3916
Luiz Capitulino4903de02010-09-16 11:01:32 -03003917 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06003918 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03003919 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003920 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003921 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003922 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003923 }
3924
Luiz Capitulino13917be2009-10-07 13:41:54 -03003925out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003926 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003927}
3928
bellard81d09122004-07-14 17:21:37 +00003929static void cmd_completion(const char *name, const char *list)
3930{
3931 const char *p, *pstart;
3932 char cmd[128];
3933 int len;
3934
3935 p = list;
3936 for(;;) {
3937 pstart = p;
3938 p = strchr(p, '|');
3939 if (!p)
3940 p = pstart + strlen(pstart);
3941 len = p - pstart;
3942 if (len > sizeof(cmd) - 2)
3943 len = sizeof(cmd) - 2;
3944 memcpy(cmd, pstart, len);
3945 cmd[len] = '\0';
3946 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003947 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003948 }
3949 if (*p == '\0')
3950 break;
3951 p++;
3952 }
3953}
3954
3955static void file_completion(const char *input)
3956{
3957 DIR *ffs;
3958 struct dirent *d;
3959 char path[1024];
3960 char file[1024], file_prefix[1024];
3961 int input_path_len;
3962 const char *p;
3963
ths5fafdf22007-09-16 21:08:06 +00003964 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003965 if (!p) {
3966 input_path_len = 0;
3967 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003968 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003969 } else {
3970 input_path_len = p - input + 1;
3971 memcpy(path, input, input_path_len);
3972 if (input_path_len > sizeof(path) - 1)
3973 input_path_len = sizeof(path) - 1;
3974 path[input_path_len] = '\0';
3975 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3976 }
3977#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003978 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3979 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003980#endif
3981 ffs = opendir(path);
3982 if (!ffs)
3983 return;
3984 for(;;) {
3985 struct stat sb;
3986 d = readdir(ffs);
3987 if (!d)
3988 break;
3989 if (strstart(d->d_name, file_prefix, NULL)) {
3990 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003991 if (input_path_len < sizeof(file))
3992 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3993 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003994 /* stat the file to find out if it's a directory.
3995 * In that case add a slash to speed up typing long paths
3996 */
3997 stat(file, &sb);
3998 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003999 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004000 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004001 }
4002 }
4003 closedir(ffs);
4004}
4005
aliguori51de9762009-03-05 23:00:43 +00004006static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004007{
aliguori51de9762009-03-05 23:00:43 +00004008 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004009 const char *input = opaque;
4010
4011 if (input[0] == '\0' ||
4012 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004013 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004014 }
4015}
4016
4017/* NOTE: this parser is an approximate form of the real command parser */
4018static void parse_cmdline(const char *cmdline,
4019 int *pnb_args, char **args)
4020{
4021 const char *p;
4022 int nb_args, ret;
4023 char buf[1024];
4024
4025 p = cmdline;
4026 nb_args = 0;
4027 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004028 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004029 p++;
4030 if (*p == '\0')
4031 break;
4032 if (nb_args >= MAX_ARGS)
4033 break;
4034 ret = get_str(buf, sizeof(buf), &p);
4035 args[nb_args] = qemu_strdup(buf);
4036 nb_args++;
4037 if (ret < 0)
4038 break;
4039 }
4040 *pnb_args = nb_args;
4041}
4042
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004043static const char *next_arg_type(const char *typestr)
4044{
4045 const char *p = strchr(typestr, ':');
4046 return (p != NULL ? ++p : typestr);
4047}
4048
aliguori4c36ba32009-03-05 23:01:37 +00004049static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004050{
4051 const char *cmdname;
4052 char *args[MAX_ARGS];
4053 int nb_args, i, len;
4054 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004055 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004056 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004057
4058 parse_cmdline(cmdline, &nb_args, args);
4059#ifdef DEBUG_COMPLETION
4060 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004061 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004062 }
4063#endif
4064
4065 /* if the line ends with a space, it means we want to complete the
4066 next arg */
4067 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004068 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004069 if (nb_args >= MAX_ARGS) {
4070 goto cleanup;
4071 }
bellard81d09122004-07-14 17:21:37 +00004072 args[nb_args++] = qemu_strdup("");
4073 }
4074 if (nb_args <= 1) {
4075 /* command completion */
4076 if (nb_args == 0)
4077 cmdname = "";
4078 else
4079 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004080 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004081 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004082 cmd_completion(cmdname, cmd->name);
4083 }
4084 } else {
4085 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004086 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4087 if (compare_cmd(args[0], cmd->name)) {
4088 break;
4089 }
bellard81d09122004-07-14 17:21:37 +00004090 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004091 if (!cmd->name) {
4092 goto cleanup;
4093 }
4094
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004095 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004096 for(i = 0; i < nb_args - 2; i++) {
4097 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004098 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004099 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004100 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004101 }
4102 }
4103 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004104 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004105 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004106 }
bellard81d09122004-07-14 17:21:37 +00004107 switch(*ptype) {
4108 case 'F':
4109 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004110 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004111 file_completion(str);
4112 break;
4113 case 'B':
4114 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004115 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004116 bdrv_iterate(block_completion_it, (void *)str);
4117 break;
bellard7fe48482004-10-09 18:08:01 +00004118 case 's':
4119 /* XXX: more generic ? */
4120 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004121 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004122 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4123 cmd_completion(str, cmd->name);
4124 }
bellard64866c32006-05-07 18:03:31 +00004125 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004126 char *sep = strrchr(str, '-');
4127 if (sep)
4128 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004129 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004130 for(key = key_defs; key->name != NULL; key++) {
4131 cmd_completion(str, key->name);
4132 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004133 } else if (!strcmp(cmd->name, "help|?")) {
4134 readline_set_completion_index(cur_mon->rs, strlen(str));
4135 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4136 cmd_completion(str, cmd->name);
4137 }
bellard7fe48482004-10-09 18:08:01 +00004138 }
4139 break;
bellard81d09122004-07-14 17:21:37 +00004140 default:
4141 break;
4142 }
4143 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004144
4145cleanup:
4146 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00004147 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004148 }
bellard81d09122004-07-14 17:21:37 +00004149}
4150
aliguori731b0362009-03-05 23:01:42 +00004151static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004152{
aliguori731b0362009-03-05 23:01:42 +00004153 Monitor *mon = opaque;
4154
Jan Kiszkac62313b2009-12-04 14:05:29 +01004155 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004156}
4157
Luiz Capitulino09069b12010-02-04 18:10:06 -02004158static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4159{
4160 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4161 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4162}
4163
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004164/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004165 * Argument validation rules:
4166 *
4167 * 1. The argument must exist in cmd_args qdict
4168 * 2. The argument type must be the expected one
4169 *
4170 * Special case: If the argument doesn't exist in cmd_args and
4171 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4172 * checking is skipped for it.
4173 */
4174static int check_client_args_type(const QDict *client_args,
4175 const QDict *cmd_args, int flags)
4176{
4177 const QDictEntry *ent;
4178
4179 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4180 QObject *obj;
4181 QString *arg_type;
4182 const QObject *client_arg = qdict_entry_value(ent);
4183 const char *client_arg_name = qdict_entry_key(ent);
4184
4185 obj = qdict_get(cmd_args, client_arg_name);
4186 if (!obj) {
4187 if (flags & QMP_ACCEPT_UNKNOWNS) {
4188 /* handler accepts unknowns */
4189 continue;
4190 }
4191 /* client arg doesn't exist */
4192 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4193 return -1;
4194 }
4195
4196 arg_type = qobject_to_qstring(obj);
4197 assert(arg_type != NULL);
4198
4199 /* check if argument's type is correct */
4200 switch (qstring_get_str(arg_type)[0]) {
4201 case 'F':
4202 case 'B':
4203 case 's':
4204 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4205 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4206 "string");
4207 return -1;
4208 }
4209 break;
4210 case 'i':
4211 case 'l':
4212 case 'M':
4213 if (qobject_type(client_arg) != QTYPE_QINT) {
4214 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4215 "int");
4216 return -1;
4217 }
4218 break;
4219 case 'f':
4220 case 'T':
4221 if (qobject_type(client_arg) != QTYPE_QINT &&
4222 qobject_type(client_arg) != QTYPE_QFLOAT) {
4223 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4224 "number");
4225 return -1;
4226 }
4227 break;
4228 case 'b':
4229 case '-':
4230 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4231 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4232 "bool");
4233 return -1;
4234 }
4235 break;
4236 case 'O':
4237 assert(flags & QMP_ACCEPT_UNKNOWNS);
4238 break;
4239 case '/':
4240 case '.':
4241 /*
4242 * These types are not supported by QMP and thus are not
4243 * handled here. Fall through.
4244 */
4245 default:
4246 abort();
4247 }
4248 }
4249
4250 return 0;
4251}
4252
4253/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004254 * - Check if the client has passed all mandatory args
4255 * - Set special flags for argument validation
4256 */
4257static int check_mandatory_args(const QDict *cmd_args,
4258 const QDict *client_args, int *flags)
4259{
4260 const QDictEntry *ent;
4261
4262 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4263 const char *cmd_arg_name = qdict_entry_key(ent);
4264 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4265 assert(type != NULL);
4266
4267 if (qstring_get_str(type)[0] == 'O') {
4268 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4269 *flags |= QMP_ACCEPT_UNKNOWNS;
4270 } else if (qstring_get_str(type)[0] != '-' &&
4271 qstring_get_str(type)[1] != '?' &&
4272 !qdict_haskey(client_args, cmd_arg_name)) {
4273 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4274 return -1;
4275 }
4276 }
4277
4278 return 0;
4279}
4280
4281static QDict *qdict_from_args_type(const char *args_type)
4282{
4283 int i;
4284 QDict *qdict;
4285 QString *key, *type, *cur_qs;
4286
4287 assert(args_type != NULL);
4288
4289 qdict = qdict_new();
4290
4291 if (args_type == NULL || args_type[0] == '\0') {
4292 /* no args, empty qdict */
4293 goto out;
4294 }
4295
4296 key = qstring_new();
4297 type = qstring_new();
4298
4299 cur_qs = key;
4300
4301 for (i = 0;; i++) {
4302 switch (args_type[i]) {
4303 case ',':
4304 case '\0':
4305 qdict_put(qdict, qstring_get_str(key), type);
4306 QDECREF(key);
4307 if (args_type[i] == '\0') {
4308 goto out;
4309 }
4310 type = qstring_new(); /* qdict has ref */
4311 cur_qs = key = qstring_new();
4312 break;
4313 case ':':
4314 cur_qs = type;
4315 break;
4316 default:
4317 qstring_append_chr(cur_qs, args_type[i]);
4318 break;
4319 }
4320 }
4321
4322out:
4323 return qdict;
4324}
4325
4326/*
4327 * Client argument checking rules:
4328 *
4329 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004330 * 2. Each argument provided by the client must be expected
4331 * 3. Each argument provided by the client must have the type expected
4332 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004333 */
4334static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4335{
4336 int flags, err;
4337 QDict *cmd_args;
4338
4339 cmd_args = qdict_from_args_type(cmd->args_type);
4340
4341 flags = 0;
4342 err = check_mandatory_args(cmd_args, client_args, &flags);
4343 if (err) {
4344 goto out;
4345 }
4346
Luiz Capitulino4af91932010-06-22 11:44:05 -03004347 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004348
4349out:
4350 QDECREF(cmd_args);
4351 return err;
4352}
4353
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004354/*
4355 * Input object checking rules
4356 *
4357 * 1. Input object must be a dict
4358 * 2. The "execute" key must exist
4359 * 3. The "execute" key must be a string
4360 * 4. If the "arguments" key exists, it must be a dict
4361 * 5. If the "id" key exists, it can be anything (ie. json-value)
4362 * 6. Any argument not listed above is considered invalid
4363 */
4364static QDict *qmp_check_input_obj(QObject *input_obj)
4365{
4366 const QDictEntry *ent;
4367 int has_exec_key = 0;
4368 QDict *input_dict;
4369
4370 if (qobject_type(input_obj) != QTYPE_QDICT) {
4371 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4372 return NULL;
4373 }
4374
4375 input_dict = qobject_to_qdict(input_obj);
4376
4377 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4378 const char *arg_name = qdict_entry_key(ent);
4379 const QObject *arg_obj = qdict_entry_value(ent);
4380
4381 if (!strcmp(arg_name, "execute")) {
4382 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4383 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4384 "string");
4385 return NULL;
4386 }
4387 has_exec_key = 1;
4388 } else if (!strcmp(arg_name, "arguments")) {
4389 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4390 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4391 "object");
4392 return NULL;
4393 }
4394 } else if (!strcmp(arg_name, "id")) {
4395 /* FIXME: check duplicated IDs for async commands */
4396 } else {
4397 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4398 return NULL;
4399 }
4400 }
4401
4402 if (!has_exec_key) {
4403 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4404 return NULL;
4405 }
4406
4407 return input_dict;
4408}
4409
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004410static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4411{
4412 QObject *ret_data = NULL;
4413
Luiz Capitulino4903de02010-09-16 11:01:32 -03004414 if (handler_is_async(cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004415 qmp_async_info_handler(mon, cmd);
4416 if (monitor_has_error(mon)) {
4417 monitor_protocol_emitter(mon, NULL);
4418 }
4419 } else {
4420 cmd->mhandler.info_new(mon, &ret_data);
4421 if (ret_data) {
4422 monitor_protocol_emitter(mon, ret_data);
4423 qobject_decref(ret_data);
4424 }
4425 }
4426}
4427
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004428static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4429{
4430 int err;
4431 QObject *obj;
4432 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004433 const mon_cmd_t *cmd;
4434 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004435 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004436
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004437 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004438 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004439
4440 obj = json_parser_parse(tokens, NULL);
4441 if (!obj) {
4442 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004443 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004444 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004445 }
4446
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004447 input = qmp_check_input_obj(obj);
4448 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004449 qobject_decref(obj);
4450 goto err_out;
4451 }
4452
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004453 mon->mc->id = qdict_get(input, "id");
4454 qobject_incref(mon->mc->id);
4455
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004456 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004457 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004458 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004459 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004460 }
4461
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004462 if (strstart(cmd_name, "query-", &query_cmd)) {
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004463 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004464 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004465 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004466 }
4467
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004468 if (!cmd) {
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004469 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4470 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004471 }
4472
4473 obj = qdict_get(input, "arguments");
4474 if (!obj) {
4475 args = qdict_new();
4476 } else {
4477 args = qobject_to_qdict(obj);
4478 QINCREF(args);
4479 }
4480
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004481 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004482 if (err < 0) {
4483 goto err_out;
4484 }
4485
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004486 if (query_cmd) {
4487 qmp_call_query_cmd(mon, cmd);
Luiz Capitulino4903de02010-09-16 11:01:32 -03004488 } else if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004489 err = qmp_async_cmd_handler(mon, cmd, args);
4490 if (err) {
4491 /* emit the error response */
4492 goto err_out;
4493 }
Adam Litke940cc302010-01-25 12:18:44 -06004494 } else {
4495 monitor_call_handler(mon, cmd, args);
4496 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004497
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004498 goto out;
4499
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004500err_out:
4501 monitor_protocol_emitter(mon, NULL);
4502out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004503 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004504 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004505}
4506
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004507/**
4508 * monitor_control_read(): Read and handle QMP input
4509 */
4510static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4511{
4512 Monitor *old_mon = cur_mon;
4513
4514 cur_mon = opaque;
4515
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004516 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004517
4518 cur_mon = old_mon;
4519}
4520
aliguori731b0362009-03-05 23:01:42 +00004521static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004522{
aliguori731b0362009-03-05 23:01:42 +00004523 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004524 int i;
aliguori376253e2009-03-05 23:01:23 +00004525
aliguori731b0362009-03-05 23:01:42 +00004526 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004527
aliguoricde76ee2009-03-05 23:01:51 +00004528 if (cur_mon->rs) {
4529 for (i = 0; i < size; i++)
4530 readline_handle_byte(cur_mon->rs, buf[i]);
4531 } else {
4532 if (size == 0 || buf[size - 1] != 0)
4533 monitor_printf(cur_mon, "corrupted command\n");
4534 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004535 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004536 }
aliguori731b0362009-03-05 23:01:42 +00004537
4538 cur_mon = old_mon;
4539}
aliguorid8f44602008-10-06 13:52:44 +00004540
aliguori376253e2009-03-05 23:01:23 +00004541static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004542{
aliguori731b0362009-03-05 23:01:42 +00004543 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004544 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004545 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004546}
4547
aliguoricde76ee2009-03-05 23:01:51 +00004548int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004549{
aliguoricde76ee2009-03-05 23:01:51 +00004550 if (!mon->rs)
4551 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004552 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004553 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004554}
4555
aliguori376253e2009-03-05 23:01:23 +00004556void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004557{
aliguoricde76ee2009-03-05 23:01:51 +00004558 if (!mon->rs)
4559 return;
aliguori731b0362009-03-05 23:01:42 +00004560 if (--mon->suspend_cnt == 0)
4561 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004562}
4563
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004564static QObject *get_qmp_greeting(void)
4565{
4566 QObject *ver;
4567
4568 do_info_version(NULL, &ver);
4569 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4570}
4571
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004572/**
4573 * monitor_control_event(): Print QMP gretting
4574 */
4575static void monitor_control_event(void *opaque, int event)
4576{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004577 QObject *data;
4578 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004579
Luiz Capitulino47116d12010-02-08 17:01:30 -02004580 switch (event) {
4581 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004582 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004583 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004584 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004585 monitor_json_emitter(mon, data);
4586 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004587 break;
4588 case CHR_EVENT_CLOSED:
4589 json_message_parser_destroy(&mon->mc->parser);
4590 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004591 }
4592}
4593
aliguori731b0362009-03-05 23:01:42 +00004594static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004595{
aliguori376253e2009-03-05 23:01:23 +00004596 Monitor *mon = opaque;
4597
aliguori2724b182009-03-05 23:01:47 +00004598 switch (event) {
4599 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004600 mon->mux_out = 0;
4601 if (mon->reset_seen) {
4602 readline_restart(mon->rs);
4603 monitor_resume(mon);
4604 monitor_flush(mon);
4605 } else {
4606 mon->suspend_cnt = 0;
4607 }
aliguori2724b182009-03-05 23:01:47 +00004608 break;
ths86e94de2007-01-05 22:01:59 +00004609
aliguori2724b182009-03-05 23:01:47 +00004610 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004611 if (mon->reset_seen) {
4612 if (mon->suspend_cnt == 0) {
4613 monitor_printf(mon, "\n");
4614 }
4615 monitor_flush(mon);
4616 monitor_suspend(mon);
4617 } else {
4618 mon->suspend_cnt++;
4619 }
4620 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004621 break;
4622
Amit Shahb6b8df52009-10-07 18:31:16 +05304623 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004624 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4625 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004626 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004627 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004628 }
4629 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004630 break;
4631 }
ths86e94de2007-01-05 22:01:59 +00004632}
4633
aliguori76655d62009-03-06 20:27:37 +00004634
4635/*
4636 * Local variables:
4637 * c-indent-level: 4
4638 * c-basic-offset: 4
4639 * tab-width: 8
4640 * End:
4641 */
4642
aliguori731b0362009-03-05 23:01:42 +00004643void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004644{
aliguori731b0362009-03-05 23:01:42 +00004645 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004646 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004647
4648 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004649 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004650 is_first_init = 0;
4651 }
aliguori87127162009-03-05 23:01:29 +00004652
4653 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004654
aliguori87127162009-03-05 23:01:29 +00004655 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004656 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004657 if (flags & MONITOR_USE_READLINE) {
4658 mon->rs = readline_init(mon, monitor_find_completion);
4659 monitor_read_command(mon, 0);
4660 }
aliguori87127162009-03-05 23:01:29 +00004661
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004662 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004663 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004664 /* Control mode requires special handlers */
4665 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4666 monitor_control_event, mon);
4667 } else {
4668 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4669 monitor_event, mon);
4670 }
aliguori87127162009-03-05 23:01:29 +00004671
Blue Swirl72cf2d42009-09-12 07:36:22 +00004672 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004673 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4674 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004675}
4676
aliguori376253e2009-03-05 23:01:23 +00004677static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004678{
aliguoribb5fc202009-03-05 23:01:15 +00004679 BlockDriverState *bs = opaque;
4680 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004681
aliguoribb5fc202009-03-05 23:01:15 +00004682 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004683 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004684 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004685 }
aliguori731b0362009-03-05 23:01:42 +00004686 if (mon->password_completion_cb)
4687 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004688
aliguori731b0362009-03-05 23:01:42 +00004689 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004690}
aliguoric0f4ce72009-03-05 23:01:01 +00004691
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004692int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4693 BlockDriverCompletionFunc *completion_cb,
4694 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004695{
aliguoricde76ee2009-03-05 23:01:51 +00004696 int err;
4697
aliguoribb5fc202009-03-05 23:01:15 +00004698 if (!bdrv_key_required(bs)) {
4699 if (completion_cb)
4700 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004701 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004702 }
aliguoric0f4ce72009-03-05 23:01:01 +00004703
Luiz Capitulino94171e12009-12-07 21:37:00 +01004704 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004705 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004706 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004707 }
4708
aliguori376253e2009-03-05 23:01:23 +00004709 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4710 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004711
aliguori731b0362009-03-05 23:01:42 +00004712 mon->password_completion_cb = completion_cb;
4713 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004714
aliguoricde76ee2009-03-05 23:01:51 +00004715 err = monitor_read_password(mon, bdrv_password_cb, bs);
4716
4717 if (err && completion_cb)
4718 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004719
4720 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004721}