blob: f62c34a94602d57d76c87bb2b9283b078df82961 [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
Markus Armbruster8631b602010-02-18 11:41:55 +0100192Monitor *cur_mon;
193Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000194
aliguori731b0362009-03-05 23:01:42 +0000195static void monitor_command_cb(Monitor *mon, const char *cmdline,
196 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000197
Luiz Capitulino09069b12010-02-04 18:10:06 -0200198static inline int qmp_cmd_mode(const Monitor *mon)
199{
200 return (mon->mc ? mon->mc->command_mode : 0);
201}
202
Luiz Capitulino418173c2009-11-26 22:58:51 -0200203/* Return true if in control mode, false otherwise */
204static inline int monitor_ctrl_mode(const Monitor *mon)
205{
206 return (mon->flags & MONITOR_USE_CONTROL);
207}
208
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100209/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
210int monitor_cur_is_qmp(void)
211{
212 return cur_mon && monitor_ctrl_mode(cur_mon);
213}
214
aliguori731b0362009-03-05 23:01:42 +0000215static void monitor_read_command(Monitor *mon, int show_prompt)
216{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200217 if (!mon->rs)
218 return;
219
aliguori731b0362009-03-05 23:01:42 +0000220 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
221 if (show_prompt)
222 readline_show_prompt(mon->rs);
223}
bellard6a00d602005-11-21 23:25:50 +0000224
aliguoricde76ee2009-03-05 23:01:51 +0000225static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
226 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000227{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100228 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100229 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100230 return -EINVAL;
231 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000232 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
233 /* prompt is printed on return from the command handler */
234 return 0;
235 } else {
236 monitor_printf(mon, "terminal does not support password prompting\n");
237 return -ENOTTY;
238 }
aliguoribb5fc202009-03-05 23:01:15 +0000239}
240
aliguori376253e2009-03-05 23:01:23 +0000241void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000242{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200243 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000244 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
245 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000246 }
247}
248
249/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000250static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000251{
ths60fe76f2007-12-16 03:02:09 +0000252 char c;
aliguori731b0362009-03-05 23:01:42 +0000253
bellard7e2515e2004-08-01 21:52:19 +0000254 for(;;) {
255 c = *str++;
256 if (c == '\0')
257 break;
bellard7ba12602006-07-14 20:26:42 +0000258 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000259 mon->outbuf[mon->outbuf_index++] = '\r';
260 mon->outbuf[mon->outbuf_index++] = c;
261 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
262 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000263 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000264 }
265}
266
aliguori376253e2009-03-05 23:01:23 +0000267void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000268{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200269 char buf[4096];
270
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200271 if (!mon)
272 return;
273
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200274 mon_print_count_inc(mon);
275
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200276 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200277 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200278 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200279
280 vsnprintf(buf, sizeof(buf), fmt, ap);
281 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000282}
283
aliguori376253e2009-03-05 23:01:23 +0000284void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000285{
286 va_list ap;
287 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000288 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000289 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000290}
291
aliguori376253e2009-03-05 23:01:23 +0000292void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000293{
294 int i;
295
296 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000297 switch (filename[i]) {
298 case ' ':
299 case '"':
300 case '\\':
301 monitor_printf(mon, "\\%c", filename[i]);
302 break;
303 case '\t':
304 monitor_printf(mon, "\\t");
305 break;
306 case '\r':
307 monitor_printf(mon, "\\r");
308 break;
309 case '\n':
310 monitor_printf(mon, "\\n");
311 break;
312 default:
313 monitor_printf(mon, "%c", filename[i]);
314 break;
315 }
thsfef30742006-12-22 14:11:32 +0000316 }
317}
318
bellard7fe48482004-10-09 18:08:01 +0000319static int monitor_fprintf(FILE *stream, const char *fmt, ...)
320{
321 va_list ap;
322 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000323 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000324 va_end(ap);
325 return 0;
326}
327
Luiz Capitulino13c74252009-10-07 13:41:55 -0300328static void monitor_user_noop(Monitor *mon, const QObject *data) { }
329
Luiz Capitulino13917be2009-10-07 13:41:54 -0300330static inline int monitor_handler_ported(const mon_cmd_t *cmd)
331{
332 return cmd->user_print != NULL;
333}
334
Adam Litke940cc302010-01-25 12:18:44 -0600335static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
336{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200337 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600338}
339
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200340static inline bool monitor_cmd_user_only(const mon_cmd_t *cmd)
341{
342 return (cmd->flags & MONITOR_CMD_USER_ONLY);
bellard9dc39cb2004-03-14 21:38:27 +0000343}
344
Luiz Capitulino8204a912009-11-18 23:05:31 -0200345static inline int monitor_has_error(const Monitor *mon)
346{
347 return mon->error != NULL;
348}
349
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200350static void monitor_json_emitter(Monitor *mon, const QObject *data)
351{
352 QString *json;
353
Daniel P. Berrange39eaab92010-06-07 15:42:31 +0100354 if (mon->flags & MONITOR_USE_PRETTY)
355 json = qobject_to_json_pretty(data);
356 else
357 json = qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200358 assert(json != NULL);
359
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200360 qstring_append_chr(json, '\n');
361 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200362
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200363 QDECREF(json);
364}
365
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200366static void monitor_protocol_emitter(Monitor *mon, QObject *data)
367{
368 QDict *qmp;
369
370 qmp = qdict_new();
371
372 if (!monitor_has_error(mon)) {
373 /* success response */
374 if (data) {
375 qobject_incref(data);
376 qdict_put_obj(qmp, "return", data);
377 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200378 /* return an empty QDict by default */
379 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200380 }
381 } else {
382 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100383 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200384 qdict_put(qmp, "error", mon->error->error);
385 QINCREF(mon->error->error);
386 QDECREF(mon->error);
387 mon->error = NULL;
388 }
389
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200390 if (mon->mc->id) {
391 qdict_put_obj(qmp, "id", mon->mc->id);
392 mon->mc->id = NULL;
393 }
394
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200395 monitor_json_emitter(mon, QOBJECT(qmp));
396 QDECREF(qmp);
397}
398
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200399static void timestamp_put(QDict *qdict)
400{
401 int err;
402 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000403 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200404
Blue Swirld08d6f02009-12-04 18:06:39 +0000405 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200406 if (err < 0)
407 return;
408
409 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
410 "'microseconds': %" PRId64 " }",
411 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200412 qdict_put_obj(qdict, "timestamp", obj);
413}
414
415/**
416 * monitor_protocol_event(): Generate a Monitor event
417 *
418 * Event-specific data can be emitted through the (optional) 'data' parameter.
419 */
420void monitor_protocol_event(MonitorEvent event, QObject *data)
421{
422 QDict *qmp;
423 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600424 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425
Blue Swirl242cd002009-12-04 18:05:45 +0000426 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200427
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200428 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000429 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200430 event_name = "SHUTDOWN";
431 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000432 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200433 event_name = "RESET";
434 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000435 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200436 event_name = "POWERDOWN";
437 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000438 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200439 event_name = "STOP";
440 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300441 case QEVENT_RESUME:
442 event_name = "RESUME";
443 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200444 case QEVENT_VNC_CONNECTED:
445 event_name = "VNC_CONNECTED";
446 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200447 case QEVENT_VNC_INITIALIZED:
448 event_name = "VNC_INITIALIZED";
449 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200450 case QEVENT_VNC_DISCONNECTED:
451 event_name = "VNC_DISCONNECTED";
452 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200453 case QEVENT_BLOCK_IO_ERROR:
454 event_name = "BLOCK_IO_ERROR";
455 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300456 case QEVENT_RTC_CHANGE:
457 event_name = "RTC_CHANGE";
458 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300459 case QEVENT_WATCHDOG:
460 event_name = "WATCHDOG";
461 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200462 default:
463 abort();
464 break;
465 }
466
467 qmp = qdict_new();
468 timestamp_put(qmp);
469 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200470 if (data) {
471 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200472 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200473 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200474
Adam Litkef039a562010-01-15 08:34:02 -0600475 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200476 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200477 monitor_json_emitter(mon, QOBJECT(qmp));
478 }
Adam Litkef039a562010-01-15 08:34:02 -0600479 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200480 QDECREF(qmp);
481}
482
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200483static int do_qmp_capabilities(Monitor *mon, const QDict *params,
484 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200485{
486 /* Will setup QMP capabilities in the future */
487 if (monitor_ctrl_mode(mon)) {
488 mon->mc->command_mode = 1;
489 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200490
491 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200492}
493
bellard9dc39cb2004-03-14 21:38:27 +0000494static int compare_cmd(const char *name, const char *list)
495{
496 const char *p, *pstart;
497 int len;
498 len = strlen(name);
499 p = list;
500 for(;;) {
501 pstart = p;
502 p = strchr(p, '|');
503 if (!p)
504 p = pstart + strlen(pstart);
505 if ((p - pstart) == len && !memcmp(pstart, name, len))
506 return 1;
507 if (*p == '\0')
508 break;
509 p++;
510 }
511 return 0;
512}
513
Anthony Liguoric227f092009-10-01 16:12:16 -0500514static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000515 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000516{
Anthony Liguoric227f092009-10-01 16:12:16 -0500517 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000518
519 for(cmd = cmds; cmd->name != NULL; cmd++) {
520 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000521 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
522 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000523 }
524}
525
aliguori376253e2009-03-05 23:01:23 +0000526static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000527{
528 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000529 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000530 } else {
aliguori376253e2009-03-05 23:01:23 +0000531 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000532 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000533 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000534 monitor_printf(mon, "Log items (comma separated):\n");
535 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000536 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000537 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000538 }
539 }
bellard9dc39cb2004-03-14 21:38:27 +0000540 }
541}
542
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300543static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300544{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300545 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300546}
547
Prerna Saxena22890ab2010-06-24 17:04:53 +0530548#ifdef CONFIG_SIMPLE_TRACE
549static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
550{
551 const char *tp_name = qdict_get_str(qdict, "name");
552 bool new_state = qdict_get_bool(qdict, "option");
553 st_change_trace_event_state(tp_name, new_state);
554}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100555
556static void do_trace_file(Monitor *mon, const QDict *qdict)
557{
558 const char *op = qdict_get_try_str(qdict, "op");
559 const char *arg = qdict_get_try_str(qdict, "arg");
560
561 if (!op) {
562 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
563 } else if (!strcmp(op, "on")) {
564 st_set_trace_file_enabled(true);
565 } else if (!strcmp(op, "off")) {
566 st_set_trace_file_enabled(false);
567 } else if (!strcmp(op, "flush")) {
568 st_flush_trace_buffer();
569 } else if (!strcmp(op, "set")) {
570 if (arg) {
571 st_set_trace_file(arg);
572 }
573 } else {
574 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
575 help_cmd(mon, "trace-file");
576 }
577}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530578#endif
579
Adam Litke940cc302010-01-25 12:18:44 -0600580static void user_monitor_complete(void *opaque, QObject *ret_data)
581{
582 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
583
584 if (ret_data) {
585 data->user_print(data->mon, ret_data);
586 }
587 monitor_resume(data->mon);
588 qemu_free(data);
589}
590
591static void qmp_monitor_complete(void *opaque, QObject *ret_data)
592{
593 monitor_protocol_emitter(opaque, ret_data);
594}
595
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300596static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
597 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600598{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300599 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600600}
601
602static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
603{
604 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
605}
606
607static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
608 const QDict *params)
609{
610 int ret;
611
612 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
613 cb_data->mon = mon;
614 cb_data->user_print = cmd->user_print;
615 monitor_suspend(mon);
616 ret = cmd->mhandler.cmd_async(mon, params,
617 user_monitor_complete, cb_data);
618 if (ret < 0) {
619 monitor_resume(mon);
620 qemu_free(cb_data);
621 }
622}
623
624static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
625{
626 int ret;
627
628 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
629 cb_data->mon = mon;
630 cb_data->user_print = cmd->user_print;
631 monitor_suspend(mon);
632 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
633 if (ret < 0) {
634 monitor_resume(mon);
635 qemu_free(cb_data);
636 }
637}
638
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300639static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000640{
Anthony Liguoric227f092009-10-01 16:12:16 -0500641 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300642 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000643
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200644 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000645 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200646 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300647
648 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000649 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300650 break;
bellard9dc39cb2004-03-14 21:38:27 +0000651 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300652
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200653 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300654 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200655 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300656
Adam Litke940cc302010-01-25 12:18:44 -0600657 if (monitor_handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300658 user_async_info_handler(mon, cmd);
Adam Litke940cc302010-01-25 12:18:44 -0600659 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300660 QObject *info_data = NULL;
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200661
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300662 cmd->mhandler.info_new(mon, &info_data);
663 if (info_data) {
664 cmd->user_print(mon, info_data);
665 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200666 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300667 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300668 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300669 }
670
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300671 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300672
673help:
674 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000675}
676
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200677static void do_info_version_print(Monitor *mon, const QObject *data)
678{
679 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300680 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200681
682 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300683 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200684
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300685 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
686 qdict_get_int(qemu, "major"),
687 qdict_get_int(qemu, "minor"),
688 qdict_get_int(qemu, "micro"),
689 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200690}
691
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300692static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000693{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300694 const char *version = QEMU_VERSION;
695 int major = 0, minor = 0, micro = 0;
696 char *tmp;
697
698 major = strtol(version, &tmp, 10);
699 tmp++;
700 minor = strtol(tmp, &tmp, 10);
701 tmp++;
702 micro = strtol(tmp, &tmp, 10);
703
704 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
705 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000706}
707
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200708static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000709{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200710 QDict *qdict;
711
712 qdict = qobject_to_qdict(data);
713 if (qdict_size(qdict) == 0) {
714 return;
715 }
716
717 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
718}
719
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200720static void do_info_name(Monitor *mon, QObject **ret_data)
721{
722 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
723 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000724}
725
Luiz Capitulino1a728672009-12-10 17:15:56 -0200726static QObject *get_cmd_dict(const char *name)
727{
728 const char *p;
729
730 /* Remove '|' from some commands */
731 p = strchr(name, '|');
732 if (p) {
733 p++;
734 } else {
735 p = name;
736 }
737
738 return qobject_from_jsonf("{ 'name': %s }", p);
739}
740
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200741static void do_info_commands(Monitor *mon, QObject **ret_data)
742{
743 QList *cmd_list;
744 const mon_cmd_t *cmd;
745
746 cmd_list = qlist_new();
747
748 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200749 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd) &&
750 !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200751 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200752 }
753 }
754
755 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200756 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd)) {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200757 char buf[128];
758 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200759 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200760 }
761 }
762
763 *ret_data = QOBJECT(cmd_list);
764}
765
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200766static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000767{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200768 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
769}
770
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200771static void do_info_uuid(Monitor *mon, QObject **ret_data)
772{
773 char uuid[64];
774
775 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000776 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
777 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
778 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
779 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200780 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000781}
782
bellard6a00d602005-11-21 23:25:50 +0000783/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000784static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000785{
786 CPUState *env;
787
788 for(env = first_cpu; env != NULL; env = env->next_cpu) {
789 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000790 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000791 return 0;
792 }
793 }
794 return -1;
795}
796
pbrook9596ebb2007-11-18 01:44:38 +0000797static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000798{
aliguori731b0362009-03-05 23:01:42 +0000799 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000800 mon_set_cpu(0);
801 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300802 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000803 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000804}
805
aliguori376253e2009-03-05 23:01:23 +0000806static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000807{
bellard6a00d602005-11-21 23:25:50 +0000808 CPUState *env;
809 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000810#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000811 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000812 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000813#else
aliguori376253e2009-03-05 23:01:23 +0000814 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000815 0);
bellard9307c4c2004-04-04 12:57:25 +0000816#endif
817}
818
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300819static void print_cpu_iter(QObject *obj, void *opaque)
820{
821 QDict *cpu;
822 int active = ' ';
823 Monitor *mon = opaque;
824
825 assert(qobject_type(obj) == QTYPE_QDICT);
826 cpu = qobject_to_qdict(obj);
827
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200828 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300829 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200830 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300831
832 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
833
834#if defined(TARGET_I386)
835 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
836 (target_ulong) qdict_get_int(cpu, "pc"));
837#elif defined(TARGET_PPC)
838 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
839 (target_long) qdict_get_int(cpu, "nip"));
840#elif defined(TARGET_SPARC)
841 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
842 (target_long) qdict_get_int(cpu, "pc"));
843 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
844 (target_long) qdict_get_int(cpu, "npc"));
845#elif defined(TARGET_MIPS)
846 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
847 (target_long) qdict_get_int(cpu, "PC"));
848#endif
849
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200850 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300851 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200852 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300853
854 monitor_printf(mon, "\n");
855}
856
857static void monitor_print_cpus(Monitor *mon, const QObject *data)
858{
859 QList *cpu_list;
860
861 assert(qobject_type(data) == QTYPE_QLIST);
862 cpu_list = qobject_to_qlist(data);
863 qlist_iter(cpu_list, print_cpu_iter, mon);
864}
865
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300866static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000867{
868 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300869 QList *cpu_list;
870
871 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000872
873 /* just to set the default cpu if not already done */
874 mon_get_cpu();
875
876 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877 QDict *cpu;
878 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879
Avi Kivity4c0960c2009-08-17 23:19:53 +0300880 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200882 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
883 env->cpu_index, env == mon->mon_cpu,
884 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200885
886 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300887
bellard6a00d602005-11-21 23:25:50 +0000888#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300889 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000890#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300891 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000892#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300893 qdict_put(cpu, "pc", qint_from_int(env->pc));
894 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000895#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300896 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000897#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300898
899 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000900 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300901
902 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000903}
904
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200905static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000906{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300907 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200908 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100909 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
910 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200911 return -1;
912 }
913 return 0;
bellard6a00d602005-11-21 23:25:50 +0000914}
915
aliguori376253e2009-03-05 23:01:23 +0000916static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000917{
aliguori376253e2009-03-05 23:01:23 +0000918 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000919}
920
aliguori376253e2009-03-05 23:01:23 +0000921static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000922{
923 int i;
bellard7e2515e2004-08-01 21:52:19 +0000924 const char *str;
ths3b46e622007-09-17 08:09:54 +0000925
aliguoricde76ee2009-03-05 23:01:51 +0000926 if (!mon->rs)
927 return;
bellard7e2515e2004-08-01 21:52:19 +0000928 i = 0;
929 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000930 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000931 if (!str)
932 break;
aliguori376253e2009-03-05 23:01:23 +0000933 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000934 i++;
bellardaa455482004-04-04 13:07:25 +0000935 }
936}
937
j_mayer76a66252007-03-07 08:32:30 +0000938#if defined(TARGET_PPC)
939/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000940static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000941{
942 CPUState *env;
943
944 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000945 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000946}
947#endif
948
Prerna Saxena22890ab2010-06-24 17:04:53 +0530949#if defined(CONFIG_SIMPLE_TRACE)
950static void do_info_trace(Monitor *mon)
951{
952 st_print_trace((FILE *)mon, &monitor_fprintf);
953}
954
955static void do_info_trace_events(Monitor *mon)
956{
957 st_print_trace_events((FILE *)mon, &monitor_fprintf);
958}
959#endif
960
Luiz Capitulinob223f352009-10-07 13:41:56 -0300961/**
962 * do_quit(): Quit QEMU execution
963 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200964static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000965{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300966 monitor_suspend(mon);
967 no_shutdown = 0;
968 qemu_system_shutdown_request();
969
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200970 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000971}
972
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200973static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000974{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200975 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100976 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200977 return -1;
978 }
aliguoribb5fc202009-03-05 23:01:15 +0000979
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200980 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100981}
982
983static void change_vnc_password_cb(Monitor *mon, const char *password,
984 void *opaque)
985{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100986 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000987 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000988}
989
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200990static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000991{
ths70848512007-08-25 01:37:05 +0000992 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000993 strcmp(target, "password") == 0) {
994 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000995 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000996 strncpy(password, arg, sizeof(password));
997 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200998 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000999 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001000 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001001 }
ths70848512007-08-25 01:37:05 +00001002 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001003 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001004 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001005 return -1;
1006 }
ths70848512007-08-25 01:37:05 +00001007 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001008
1009 return 0;
thse25a5822007-08-25 01:36:20 +00001010}
1011
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001012/**
1013 * do_change(): Change a removable medium, or VNC configuration
1014 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001015static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001016{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001017 const char *device = qdict_get_str(qdict, "device");
1018 const char *target = qdict_get_str(qdict, "target");
1019 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001020 int ret;
1021
thse25a5822007-08-25 01:36:20 +00001022 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001023 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001024 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001025 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001026 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027
1028 return ret;
thse25a5822007-08-25 01:36:20 +00001029}
1030
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001031static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001032{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001033 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001034 return 0;
bellard59a983b2004-03-17 23:17:16 +00001035}
1036
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001037static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001038{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001039 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001040}
1041
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001042static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001043{
1044 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001045 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001046
bellard9307c4c2004-04-04 12:57:25 +00001047 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001048 mask = 0;
1049 } else {
bellard9307c4c2004-04-04 12:57:25 +00001050 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001051 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001052 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001053 return;
1054 }
1055 }
1056 cpu_set_log(mask);
1057}
1058
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001059static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001060{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001061 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001062 if (!option || !strcmp(option, "on")) {
1063 singlestep = 1;
1064 } else if (!strcmp(option, "off")) {
1065 singlestep = 0;
1066 } else {
1067 monitor_printf(mon, "unexpected option %s\n", option);
1068 }
1069}
1070
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001071/**
1072 * do_stop(): Stop VM execution
1073 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001074static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001075{
1076 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001077 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001078}
1079
aliguoribb5fc202009-03-05 23:01:15 +00001080static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001081
aliguori376253e2009-03-05 23:01:23 +00001082struct bdrv_iterate_context {
1083 Monitor *mon;
1084 int err;
1085};
aliguoric0f4ce72009-03-05 23:01:01 +00001086
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001087/**
1088 * do_cont(): Resume emulation.
1089 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001090static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001091{
1092 struct bdrv_iterate_context context = { mon, 0 };
1093
Amit Shah8e848652010-07-27 15:49:19 +05301094 if (incoming_expected) {
1095 qerror_report(QERR_MIGRATION_EXPECTED);
1096 return -1;
1097 }
aliguori376253e2009-03-05 23:01:23 +00001098 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001099 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001100 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001101 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001102 return 0;
1103 } else {
1104 return -1;
1105 }
bellard8a7ddc32004-03-31 19:00:16 +00001106}
1107
aliguoribb5fc202009-03-05 23:01:15 +00001108static void bdrv_key_cb(void *opaque, int err)
1109{
aliguori376253e2009-03-05 23:01:23 +00001110 Monitor *mon = opaque;
1111
aliguoribb5fc202009-03-05 23:01:15 +00001112 /* another key was set successfully, retry to continue */
1113 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001114 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001115}
1116
1117static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1118{
aliguori376253e2009-03-05 23:01:23 +00001119 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001120
aliguori376253e2009-03-05 23:01:23 +00001121 if (!context->err && bdrv_key_required(bs)) {
1122 context->err = -EBUSY;
1123 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1124 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001125 }
1126}
1127
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001128static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001129{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001130 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001131 if (!device)
1132 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1133 if (gdbserver_start(device) < 0) {
1134 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1135 device);
1136 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001137 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001138 } else {
aliguori59030a82009-04-05 18:43:41 +00001139 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1140 device);
bellard8a7ddc32004-03-31 19:00:16 +00001141 }
1142}
1143
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001144static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001145{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001146 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001147 if (select_watchdog_action(action) == -1) {
1148 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1149 }
1150}
1151
aliguori376253e2009-03-05 23:01:23 +00001152static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001153{
aliguori376253e2009-03-05 23:01:23 +00001154 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001155 switch(c) {
1156 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001157 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001158 break;
1159 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001160 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001161 break;
1162 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001163 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001164 break;
1165 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001166 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001167 break;
1168 default:
1169 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001170 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001171 } else {
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001173 }
1174 break;
1175 }
aliguori376253e2009-03-05 23:01:23 +00001176 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001177}
1178
aliguori376253e2009-03-05 23:01:23 +00001179static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001180 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001181{
bellard6a00d602005-11-21 23:25:50 +00001182 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001183 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001184 uint8_t buf[16];
1185 uint64_t v;
1186
1187 if (format == 'i') {
1188 int flags;
1189 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001190 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001191#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001192 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001193 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001194 } else if (wsize == 4) {
1195 flags = 0;
1196 } else {
bellard6a15fd12006-04-12 21:07:07 +00001197 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001198 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001199 if (env) {
1200#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001201 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001202 (env->segs[R_CS].flags & DESC_L_MASK))
1203 flags = 2;
1204 else
1205#endif
1206 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1207 flags = 1;
1208 }
bellard4c27ba22004-04-25 18:05:08 +00001209 }
1210#endif
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001212 return;
1213 }
1214
1215 len = wsize * count;
1216 if (wsize == 1)
1217 line_size = 8;
1218 else
1219 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001220 max_digits = 0;
1221
1222 switch(format) {
1223 case 'o':
1224 max_digits = (wsize * 8 + 2) / 3;
1225 break;
1226 default:
1227 case 'x':
1228 max_digits = (wsize * 8) / 4;
1229 break;
1230 case 'u':
1231 case 'd':
1232 max_digits = (wsize * 8 * 10 + 32) / 33;
1233 break;
1234 case 'c':
1235 wsize = 1;
1236 break;
1237 }
1238
1239 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001240 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001241 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001242 else
aliguori376253e2009-03-05 23:01:23 +00001243 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001244 l = len;
1245 if (l > line_size)
1246 l = line_size;
1247 if (is_physical) {
1248 cpu_physical_memory_rw(addr, buf, l, 0);
1249 } else {
bellard6a00d602005-11-21 23:25:50 +00001250 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001251 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001252 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001253 break;
1254 }
bellard9307c4c2004-04-04 12:57:25 +00001255 }
ths5fafdf22007-09-16 21:08:06 +00001256 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001257 while (i < l) {
1258 switch(wsize) {
1259 default:
1260 case 1:
1261 v = ldub_raw(buf + i);
1262 break;
1263 case 2:
1264 v = lduw_raw(buf + i);
1265 break;
1266 case 4:
bellard92a31b12005-02-10 22:00:52 +00001267 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001268 break;
1269 case 8:
1270 v = ldq_raw(buf + i);
1271 break;
1272 }
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001274 switch(format) {
1275 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001277 break;
1278 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001280 break;
1281 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001282 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001283 break;
1284 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001285 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001286 break;
1287 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001288 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001289 break;
1290 }
1291 i += wsize;
1292 }
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001294 addr += l;
1295 len -= l;
1296 }
1297}
1298
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001299static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001300{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001301 int count = qdict_get_int(qdict, "count");
1302 int format = qdict_get_int(qdict, "format");
1303 int size = qdict_get_int(qdict, "size");
1304 target_long addr = qdict_get_int(qdict, "addr");
1305
aliguori376253e2009-03-05 23:01:23 +00001306 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001307}
1308
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001309static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001310{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001311 int count = qdict_get_int(qdict, "count");
1312 int format = qdict_get_int(qdict, "format");
1313 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001314 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001315
aliguori376253e2009-03-05 23:01:23 +00001316 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001317}
1318
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001319static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001320{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001321 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001322 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001323
blueswir17743e582007-09-24 18:39:04 +00001324#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001325 switch(format) {
1326 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001327 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001328 break;
1329 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001330 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001331 break;
1332 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001333 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001334 break;
1335 default:
1336 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001337 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001338 break;
1339 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001341 break;
1342 }
bellard92a31b12005-02-10 22:00:52 +00001343#else
1344 switch(format) {
1345 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001346 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001347 break;
1348 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001349 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001350 break;
1351 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001352 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001353 break;
1354 default:
1355 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001357 break;
1358 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001359 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001360 break;
1361 }
1362#endif
aliguori376253e2009-03-05 23:01:23 +00001363 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001364}
1365
Luiz Capitulino98696222010-02-10 23:49:58 -02001366static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001367{
1368 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001369 uint32_t size = qdict_get_int(qdict, "size");
1370 const char *filename = qdict_get_str(qdict, "filename");
1371 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001372 uint32_t l;
1373 CPUState *env;
1374 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001375 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001376
1377 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001378
1379 f = fopen(filename, "wb");
1380 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001381 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001382 return -1;
bellardb371dc52007-01-03 15:20:39 +00001383 }
1384 while (size != 0) {
1385 l = sizeof(buf);
1386 if (l > size)
1387 l = size;
1388 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001389 if (fwrite(buf, 1, l, f) != l) {
1390 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1391 goto exit;
1392 }
bellardb371dc52007-01-03 15:20:39 +00001393 addr += l;
1394 size -= l;
1395 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001396
1397 ret = 0;
1398
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001399exit:
bellardb371dc52007-01-03 15:20:39 +00001400 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001401 return ret;
bellardb371dc52007-01-03 15:20:39 +00001402}
1403
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001404static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001405 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001406{
1407 FILE *f;
1408 uint32_t l;
1409 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001410 uint32_t size = qdict_get_int(qdict, "size");
1411 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001412 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001413 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001414
1415 f = fopen(filename, "wb");
1416 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001417 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001418 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001419 }
1420 while (size != 0) {
1421 l = sizeof(buf);
1422 if (l > size)
1423 l = size;
1424 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001425 if (fwrite(buf, 1, l, f) != l) {
1426 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1427 goto exit;
1428 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001429 fflush(f);
1430 addr += l;
1431 size -= l;
1432 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001433
1434 ret = 0;
1435
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001436exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001437 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001438 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001439}
1440
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001441static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001442{
1443 uint32_t addr;
1444 uint8_t buf[1];
1445 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001446 uint32_t start = qdict_get_int(qdict, "start");
1447 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001448
1449 sum = 0;
1450 for(addr = start; addr < (start + size); addr++) {
1451 cpu_physical_memory_rw(addr, buf, 1, 0);
1452 /* BSD sum algorithm ('sum' Unix command) */
1453 sum = (sum >> 1) | (sum << 15);
1454 sum += buf[0];
1455 }
aliguori376253e2009-03-05 23:01:23 +00001456 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001457}
1458
bellarda3a91a32004-06-04 11:06:21 +00001459typedef struct {
1460 int keycode;
1461 const char *name;
1462} KeyDef;
1463
1464static const KeyDef key_defs[] = {
1465 { 0x2a, "shift" },
1466 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001467
bellarda3a91a32004-06-04 11:06:21 +00001468 { 0x38, "alt" },
1469 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001470 { 0x64, "altgr" },
1471 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001472 { 0x1d, "ctrl" },
1473 { 0x9d, "ctrl_r" },
1474
1475 { 0xdd, "menu" },
1476
1477 { 0x01, "esc" },
1478
1479 { 0x02, "1" },
1480 { 0x03, "2" },
1481 { 0x04, "3" },
1482 { 0x05, "4" },
1483 { 0x06, "5" },
1484 { 0x07, "6" },
1485 { 0x08, "7" },
1486 { 0x09, "8" },
1487 { 0x0a, "9" },
1488 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001489 { 0x0c, "minus" },
1490 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001491 { 0x0e, "backspace" },
1492
1493 { 0x0f, "tab" },
1494 { 0x10, "q" },
1495 { 0x11, "w" },
1496 { 0x12, "e" },
1497 { 0x13, "r" },
1498 { 0x14, "t" },
1499 { 0x15, "y" },
1500 { 0x16, "u" },
1501 { 0x17, "i" },
1502 { 0x18, "o" },
1503 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001504 { 0x1a, "bracket_left" },
1505 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001506 { 0x1c, "ret" },
1507
1508 { 0x1e, "a" },
1509 { 0x1f, "s" },
1510 { 0x20, "d" },
1511 { 0x21, "f" },
1512 { 0x22, "g" },
1513 { 0x23, "h" },
1514 { 0x24, "j" },
1515 { 0x25, "k" },
1516 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001517 { 0x27, "semicolon" },
1518 { 0x28, "apostrophe" },
1519 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001520
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001521 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001522 { 0x2c, "z" },
1523 { 0x2d, "x" },
1524 { 0x2e, "c" },
1525 { 0x2f, "v" },
1526 { 0x30, "b" },
1527 { 0x31, "n" },
1528 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001529 { 0x33, "comma" },
1530 { 0x34, "dot" },
1531 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001532
balrog4d3b6f62008-02-10 16:33:14 +00001533 { 0x37, "asterisk" },
1534
bellarda3a91a32004-06-04 11:06:21 +00001535 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001536 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001537 { 0x3b, "f1" },
1538 { 0x3c, "f2" },
1539 { 0x3d, "f3" },
1540 { 0x3e, "f4" },
1541 { 0x3f, "f5" },
1542 { 0x40, "f6" },
1543 { 0x41, "f7" },
1544 { 0x42, "f8" },
1545 { 0x43, "f9" },
1546 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001547 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001548 { 0x46, "scroll_lock" },
1549
bellard64866c32006-05-07 18:03:31 +00001550 { 0xb5, "kp_divide" },
1551 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001552 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001553 { 0x4e, "kp_add" },
1554 { 0x9c, "kp_enter" },
1555 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001556 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001557
1558 { 0x52, "kp_0" },
1559 { 0x4f, "kp_1" },
1560 { 0x50, "kp_2" },
1561 { 0x51, "kp_3" },
1562 { 0x4b, "kp_4" },
1563 { 0x4c, "kp_5" },
1564 { 0x4d, "kp_6" },
1565 { 0x47, "kp_7" },
1566 { 0x48, "kp_8" },
1567 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001568
bellarda3a91a32004-06-04 11:06:21 +00001569 { 0x56, "<" },
1570
1571 { 0x57, "f11" },
1572 { 0x58, "f12" },
1573
1574 { 0xb7, "print" },
1575
1576 { 0xc7, "home" },
1577 { 0xc9, "pgup" },
1578 { 0xd1, "pgdn" },
1579 { 0xcf, "end" },
1580
1581 { 0xcb, "left" },
1582 { 0xc8, "up" },
1583 { 0xd0, "down" },
1584 { 0xcd, "right" },
1585
1586 { 0xd2, "insert" },
1587 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001588#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1589 { 0xf0, "stop" },
1590 { 0xf1, "again" },
1591 { 0xf2, "props" },
1592 { 0xf3, "undo" },
1593 { 0xf4, "front" },
1594 { 0xf5, "copy" },
1595 { 0xf6, "open" },
1596 { 0xf7, "paste" },
1597 { 0xf8, "find" },
1598 { 0xf9, "cut" },
1599 { 0xfa, "lf" },
1600 { 0xfb, "help" },
1601 { 0xfc, "meta_l" },
1602 { 0xfd, "meta_r" },
1603 { 0xfe, "compose" },
1604#endif
bellarda3a91a32004-06-04 11:06:21 +00001605 { 0, NULL },
1606};
1607
1608static int get_keycode(const char *key)
1609{
1610 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001611 char *endp;
1612 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001613
1614 for(p = key_defs; p->name != NULL; p++) {
1615 if (!strcmp(key, p->name))
1616 return p->keycode;
1617 }
bellard64866c32006-05-07 18:03:31 +00001618 if (strstart(key, "0x", NULL)) {
1619 ret = strtoul(key, &endp, 0);
1620 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1621 return ret;
1622 }
bellarda3a91a32004-06-04 11:06:21 +00001623 return -1;
1624}
1625
balrogc8256f92008-06-08 22:45:01 +00001626#define MAX_KEYCODES 16
1627static uint8_t keycodes[MAX_KEYCODES];
1628static int nb_pending_keycodes;
1629static QEMUTimer *key_timer;
1630
1631static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001632{
balrogc8256f92008-06-08 22:45:01 +00001633 int keycode;
1634
1635 while (nb_pending_keycodes > 0) {
1636 nb_pending_keycodes--;
1637 keycode = keycodes[nb_pending_keycodes];
1638 if (keycode & 0x80)
1639 kbd_put_keycode(0xe0);
1640 kbd_put_keycode(keycode | 0x80);
1641 }
1642}
1643
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001644static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001645{
balrog3401c0d2008-06-04 10:05:59 +00001646 char keyname_buf[16];
1647 char *separator;
1648 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001649 const char *string = qdict_get_str(qdict, "string");
1650 int has_hold_time = qdict_haskey(qdict, "hold_time");
1651 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001652
balrogc8256f92008-06-08 22:45:01 +00001653 if (nb_pending_keycodes > 0) {
1654 qemu_del_timer(key_timer);
1655 release_keys(NULL);
1656 }
1657 if (!has_hold_time)
1658 hold_time = 100;
1659 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001660 while (1) {
1661 separator = strchr(string, '-');
1662 keyname_len = separator ? separator - string : strlen(string);
1663 if (keyname_len > 0) {
1664 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1665 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001666 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001667 return;
bellarda3a91a32004-06-04 11:06:21 +00001668 }
balrogc8256f92008-06-08 22:45:01 +00001669 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001670 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001671 return;
1672 }
1673 keyname_buf[keyname_len] = 0;
1674 keycode = get_keycode(keyname_buf);
1675 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001676 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001677 return;
1678 }
balrogc8256f92008-06-08 22:45:01 +00001679 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001680 }
balrog3401c0d2008-06-04 10:05:59 +00001681 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001682 break;
balrog3401c0d2008-06-04 10:05:59 +00001683 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001684 }
balrogc8256f92008-06-08 22:45:01 +00001685 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001686 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001687 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001688 keycode = keycodes[i];
1689 if (keycode & 0x80)
1690 kbd_put_keycode(0xe0);
1691 kbd_put_keycode(keycode & 0x7f);
1692 }
balrogc8256f92008-06-08 22:45:01 +00001693 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001694 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001695 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001696}
1697
bellard13224a82006-07-14 22:03:35 +00001698static int mouse_button_state;
1699
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001700static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001701{
1702 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001703 const char *dx_str = qdict_get_str(qdict, "dx_str");
1704 const char *dy_str = qdict_get_str(qdict, "dy_str");
1705 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001706 dx = strtol(dx_str, NULL, 0);
1707 dy = strtol(dy_str, NULL, 0);
1708 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001709 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001710 dz = strtol(dz_str, NULL, 0);
1711 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1712}
1713
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001714static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001715{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001716 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001717 mouse_button_state = button_state;
1718 kbd_mouse_event(0, 0, 0, mouse_button_state);
1719}
1720
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001721static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001722{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001723 int size = qdict_get_int(qdict, "size");
1724 int addr = qdict_get_int(qdict, "addr");
1725 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001726 uint32_t val;
1727 int suffix;
1728
1729 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001730 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001731 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001732 addr++;
1733 }
1734 addr &= 0xffff;
1735
1736 switch(size) {
1737 default:
1738 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001739 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001740 suffix = 'b';
1741 break;
1742 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001743 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001744 suffix = 'w';
1745 break;
1746 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001747 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001748 suffix = 'l';
1749 break;
1750 }
aliguori376253e2009-03-05 23:01:23 +00001751 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1752 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001753}
bellarda3a91a32004-06-04 11:06:21 +00001754
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001755static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001756{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001757 int size = qdict_get_int(qdict, "size");
1758 int addr = qdict_get_int(qdict, "addr");
1759 int val = qdict_get_int(qdict, "val");
1760
Jan Kiszkaf1147842009-07-14 10:20:11 +02001761 addr &= IOPORTS_MASK;
1762
1763 switch (size) {
1764 default:
1765 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001766 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001767 break;
1768 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001769 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001770 break;
1771 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001772 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001773 break;
1774 }
1775}
1776
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001777static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001778{
1779 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001780 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001781
Jan Kiszka76e30d02009-07-02 00:19:02 +02001782 res = qemu_boot_set(bootdevice);
1783 if (res == 0) {
1784 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1785 } else if (res > 0) {
1786 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001787 } else {
aliguori376253e2009-03-05 23:01:23 +00001788 monitor_printf(mon, "no function defined to set boot device list for "
1789 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001790 }
1791}
1792
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001793/**
1794 * do_system_reset(): Issue a machine reset
1795 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001796static int do_system_reset(Monitor *mon, const QDict *qdict,
1797 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001798{
1799 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001800 return 0;
bellarde4f90822004-06-20 12:35:44 +00001801}
1802
Luiz Capitulino43076662009-10-07 13:41:59 -03001803/**
1804 * do_system_powerdown(): Issue a machine powerdown
1805 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001806static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1807 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001808{
1809 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001810 return 0;
bellard34751872005-07-02 14:31:34 +00001811}
1812
bellardb86bda52004-09-18 19:32:46 +00001813#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001814static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001815{
aliguori376253e2009-03-05 23:01:23 +00001816 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1817 addr,
1818 pte & mask,
1819 pte & PG_GLOBAL_MASK ? 'G' : '-',
1820 pte & PG_PSE_MASK ? 'P' : '-',
1821 pte & PG_DIRTY_MASK ? 'D' : '-',
1822 pte & PG_ACCESSED_MASK ? 'A' : '-',
1823 pte & PG_PCD_MASK ? 'C' : '-',
1824 pte & PG_PWT_MASK ? 'T' : '-',
1825 pte & PG_USER_MASK ? 'U' : '-',
1826 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001827}
1828
aliguori376253e2009-03-05 23:01:23 +00001829static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001830{
bellard6a00d602005-11-21 23:25:50 +00001831 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001832 int l1, l2;
1833 uint32_t pgd, pde, pte;
1834
bellard6a00d602005-11-21 23:25:50 +00001835 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001836
bellardb86bda52004-09-18 19:32:46 +00001837 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001838 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001839 return;
1840 }
1841 pgd = env->cr[3] & ~0xfff;
1842 for(l1 = 0; l1 < 1024; l1++) {
1843 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1844 pde = le32_to_cpu(pde);
1845 if (pde & PG_PRESENT_MASK) {
1846 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001847 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001848 } else {
1849 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001850 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001851 (uint8_t *)&pte, 4);
1852 pte = le32_to_cpu(pte);
1853 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001854 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001855 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001856 ~0xfff);
1857 }
1858 }
1859 }
1860 }
1861 }
1862}
1863
aliguori376253e2009-03-05 23:01:23 +00001864static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001865 uint32_t end, int prot)
1866{
bellard9746b152004-11-11 18:30:24 +00001867 int prot1;
1868 prot1 = *plast_prot;
1869 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001870 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001871 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1872 *pstart, end, end - *pstart,
1873 prot1 & PG_USER_MASK ? 'u' : '-',
1874 'r',
1875 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001876 }
1877 if (prot != 0)
1878 *pstart = end;
1879 else
1880 *pstart = -1;
1881 *plast_prot = prot;
1882 }
1883}
1884
aliguori376253e2009-03-05 23:01:23 +00001885static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001886{
bellard6a00d602005-11-21 23:25:50 +00001887 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001888 int l1, l2, prot, last_prot;
1889 uint32_t pgd, pde, pte, start, end;
1890
bellard6a00d602005-11-21 23:25:50 +00001891 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001892
bellardb86bda52004-09-18 19:32:46 +00001893 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001894 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001895 return;
1896 }
1897 pgd = env->cr[3] & ~0xfff;
1898 last_prot = 0;
1899 start = -1;
1900 for(l1 = 0; l1 < 1024; l1++) {
1901 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1902 pde = le32_to_cpu(pde);
1903 end = l1 << 22;
1904 if (pde & PG_PRESENT_MASK) {
1905 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1906 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001907 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001908 } else {
1909 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001910 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001911 (uint8_t *)&pte, 4);
1912 pte = le32_to_cpu(pte);
1913 end = (l1 << 22) + (l2 << 12);
1914 if (pte & PG_PRESENT_MASK) {
1915 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1916 } else {
1917 prot = 0;
1918 }
aliguori376253e2009-03-05 23:01:23 +00001919 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001920 }
1921 }
1922 } else {
1923 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001924 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001925 }
1926 }
1927}
1928#endif
1929
aurel327c664e22009-03-03 06:12:22 +00001930#if defined(TARGET_SH4)
1931
aliguori376253e2009-03-05 23:01:23 +00001932static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001933{
aliguori376253e2009-03-05 23:01:23 +00001934 monitor_printf(mon, " tlb%i:\t"
1935 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1936 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1937 "dirty=%hhu writethrough=%hhu\n",
1938 idx,
1939 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1940 tlb->v, tlb->sh, tlb->c, tlb->pr,
1941 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001942}
1943
aliguori376253e2009-03-05 23:01:23 +00001944static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001945{
1946 CPUState *env = mon_get_cpu();
1947 int i;
1948
aliguori376253e2009-03-05 23:01:23 +00001949 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001950 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001951 print_tlb (mon, i, &env->itlb[i]);
1952 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001953 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001954 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001955}
1956
1957#endif
1958
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001959static void do_info_kvm_print(Monitor *mon, const QObject *data)
1960{
1961 QDict *qdict;
1962
1963 qdict = qobject_to_qdict(data);
1964
1965 monitor_printf(mon, "kvm support: ");
1966 if (qdict_get_bool(qdict, "present")) {
1967 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1968 "enabled" : "disabled");
1969 } else {
1970 monitor_printf(mon, "not compiled\n");
1971 }
1972}
1973
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001974static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001975{
1976#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001977 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1978 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001979#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001980 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001981#endif
1982}
1983
aliguori030ea372009-04-21 22:30:47 +00001984static void do_info_numa(Monitor *mon)
1985{
aliguorib28b6232009-04-22 20:20:29 +00001986 int i;
aliguori030ea372009-04-21 22:30:47 +00001987 CPUState *env;
1988
1989 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1990 for (i = 0; i < nb_numa_nodes; i++) {
1991 monitor_printf(mon, "node %d cpus:", i);
1992 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1993 if (env->numa_node == i) {
1994 monitor_printf(mon, " %d", env->cpu_index);
1995 }
1996 }
1997 monitor_printf(mon, "\n");
1998 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1999 node_mem[i] >> 20);
2000 }
2001}
2002
bellard5f1ce942006-02-08 22:40:15 +00002003#ifdef CONFIG_PROFILER
2004
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002005int64_t qemu_time;
2006int64_t dev_time;
2007
aliguori376253e2009-03-05 23:01:23 +00002008static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002009{
2010 int64_t total;
2011 total = qemu_time;
2012 if (total == 0)
2013 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002014 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002015 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002016 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002017 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002018 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002019 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002020}
2021#else
aliguori376253e2009-03-05 23:01:23 +00002022static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002023{
aliguori376253e2009-03-05 23:01:23 +00002024 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002025}
2026#endif
2027
bellardec36b692006-07-16 18:57:03 +00002028/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002029static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002030
aliguori376253e2009-03-05 23:01:23 +00002031static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002032{
2033 int i;
2034 CaptureState *s;
2035
2036 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002037 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002038 s->ops.info (s->opaque);
2039 }
2040}
2041
Blue Swirl23130862009-06-06 08:22:04 +00002042#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002043static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002044{
2045 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002046 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002047 CaptureState *s;
2048
2049 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2050 if (i == n) {
2051 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002052 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002053 qemu_free (s);
2054 return;
2055 }
2056 }
2057}
2058
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002059static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002060{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002061 const char *path = qdict_get_str(qdict, "path");
2062 int has_freq = qdict_haskey(qdict, "freq");
2063 int freq = qdict_get_try_int(qdict, "freq", -1);
2064 int has_bits = qdict_haskey(qdict, "bits");
2065 int bits = qdict_get_try_int(qdict, "bits", -1);
2066 int has_channels = qdict_haskey(qdict, "nchannels");
2067 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002068 CaptureState *s;
2069
2070 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002071
2072 freq = has_freq ? freq : 44100;
2073 bits = has_bits ? bits : 16;
2074 nchannels = has_channels ? nchannels : 2;
2075
2076 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002077 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002078 qemu_free (s);
2079 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002080 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002081}
2082#endif
2083
aurel32dc1c0b72008-04-27 23:52:12 +00002084#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002085static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002086{
2087 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002088 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002089
2090 for (env = first_cpu; env != NULL; env = env->next_cpu)
2091 if (env->cpu_index == cpu_index) {
2092 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2093 break;
2094 }
2095}
2096#endif
2097
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002098static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002099{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002100 QDict *qdict;
2101
2102 qdict = qobject_to_qdict(data);
2103
2104 monitor_printf(mon, "VM status: ");
2105 if (qdict_get_bool(qdict, "running")) {
2106 monitor_printf(mon, "running");
2107 if (qdict_get_bool(qdict, "singlestep")) {
2108 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002109 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002110 } else {
2111 monitor_printf(mon, "paused");
2112 }
2113
2114 monitor_printf(mon, "\n");
2115}
2116
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002117static void do_info_status(Monitor *mon, QObject **ret_data)
2118{
2119 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2120 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002121}
2122
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002123static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002124{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002125 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002126
aliguori76655d62009-03-06 20:27:37 +00002127 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002128 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002129 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002130 return acl;
2131}
aliguori76655d62009-03-06 20:27:37 +00002132
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002133static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002134{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002135 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002136 qemu_acl *acl = find_acl(mon, aclname);
2137 qemu_acl_entry *entry;
2138 int i = 0;
2139
2140 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002141 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002142 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002143 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002144 i++;
2145 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002146 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002147 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002148 }
2149}
2150
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002151static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002153 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002154 qemu_acl *acl = find_acl(mon, aclname);
2155
2156 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002157 qemu_acl_reset(acl);
2158 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002159 }
2160}
aliguori76655d62009-03-06 20:27:37 +00002161
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002162static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002163{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002164 const char *aclname = qdict_get_str(qdict, "aclname");
2165 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002166 qemu_acl *acl = find_acl(mon, aclname);
2167
2168 if (acl) {
2169 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002170 acl->defaultDeny = 0;
2171 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002172 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002173 acl->defaultDeny = 1;
2174 monitor_printf(mon, "acl: policy set to 'deny'\n");
2175 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002176 monitor_printf(mon, "acl: unknown policy '%s', "
2177 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002178 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002179 }
2180}
aliguori76655d62009-03-06 20:27:37 +00002181
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002182static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002183{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002184 const char *aclname = qdict_get_str(qdict, "aclname");
2185 const char *match = qdict_get_str(qdict, "match");
2186 const char *policy = qdict_get_str(qdict, "policy");
2187 int has_index = qdict_haskey(qdict, "index");
2188 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002189 qemu_acl *acl = find_acl(mon, aclname);
2190 int deny, ret;
2191
2192 if (acl) {
2193 if (strcmp(policy, "allow") == 0) {
2194 deny = 0;
2195 } else if (strcmp(policy, "deny") == 0) {
2196 deny = 1;
2197 } else {
2198 monitor_printf(mon, "acl: unknown policy '%s', "
2199 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002200 return;
2201 }
aliguori28a76be2009-03-06 20:27:40 +00002202 if (has_index)
2203 ret = qemu_acl_insert(acl, deny, match, index);
2204 else
2205 ret = qemu_acl_append(acl, deny, match);
2206 if (ret < 0)
2207 monitor_printf(mon, "acl: unable to add acl entry\n");
2208 else
2209 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002210 }
2211}
aliguori76655d62009-03-06 20:27:37 +00002212
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002213static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002214{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002215 const char *aclname = qdict_get_str(qdict, "aclname");
2216 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002217 qemu_acl *acl = find_acl(mon, aclname);
2218 int ret;
aliguori76655d62009-03-06 20:27:37 +00002219
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002220 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002221 ret = qemu_acl_remove(acl, match);
2222 if (ret < 0)
2223 monitor_printf(mon, "acl: no matching acl entry\n");
2224 else
2225 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002226 }
2227}
2228
Huang Ying79c4f6b2009-06-23 10:05:14 +08002229#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002230static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002231{
2232 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002233 int cpu_index = qdict_get_int(qdict, "cpu_index");
2234 int bank = qdict_get_int(qdict, "bank");
2235 uint64_t status = qdict_get_int(qdict, "status");
2236 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2237 uint64_t addr = qdict_get_int(qdict, "addr");
2238 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002239
2240 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2241 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2242 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2243 break;
2244 }
2245}
2246#endif
2247
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002248static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002249{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002250 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002251 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002252 int fd;
2253
2254 fd = qemu_chr_get_msgfd(mon->chr);
2255 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002256 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002257 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002258 }
2259
2260 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002261 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2262 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002263 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002264 }
2265
Blue Swirl72cf2d42009-09-12 07:36:22 +00002266 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002267 if (strcmp(monfd->name, fdname) != 0) {
2268 continue;
2269 }
2270
2271 close(monfd->fd);
2272 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002273 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002274 }
2275
Anthony Liguoric227f092009-10-01 16:12:16 -05002276 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002277 monfd->name = qemu_strdup(fdname);
2278 monfd->fd = fd;
2279
Blue Swirl72cf2d42009-09-12 07:36:22 +00002280 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002281 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002282}
2283
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002284static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002285{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002286 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002287 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002288
Blue Swirl72cf2d42009-09-12 07:36:22 +00002289 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002290 if (strcmp(monfd->name, fdname) != 0) {
2291 continue;
2292 }
2293
Blue Swirl72cf2d42009-09-12 07:36:22 +00002294 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002295 close(monfd->fd);
2296 qemu_free(monfd->name);
2297 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002298 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002299 }
2300
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002301 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002302 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002303}
2304
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002305static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002306{
2307 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002308 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002309
2310 vm_stop(0);
2311
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002312 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002313 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002314 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002315}
2316
Mark McLoughlin7768e042009-07-22 09:11:41 +01002317int monitor_get_fd(Monitor *mon, const char *fdname)
2318{
Anthony Liguoric227f092009-10-01 16:12:16 -05002319 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002320
Blue Swirl72cf2d42009-09-12 07:36:22 +00002321 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002322 int fd;
2323
2324 if (strcmp(monfd->name, fdname) != 0) {
2325 continue;
2326 }
2327
2328 fd = monfd->fd;
2329
2330 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002331 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002332 qemu_free(monfd->name);
2333 qemu_free(monfd);
2334
2335 return fd;
2336 }
2337
2338 return -1;
2339}
2340
Anthony Liguoric227f092009-10-01 16:12:16 -05002341static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002342#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002343 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002344};
2345
Blue Swirl23130862009-06-06 08:22:04 +00002346/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002347static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002348 {
2349 .name = "version",
2350 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002351 .params = "",
2352 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002353 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002354 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002355 },
2356 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002357 .name = "commands",
2358 .args_type = "",
2359 .params = "",
2360 .help = "list QMP available commands",
2361 .user_print = monitor_user_noop,
2362 .mhandler.info_new = do_info_commands,
2363 },
2364 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002365 .name = "network",
2366 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002367 .params = "",
2368 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002369 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002370 },
2371 {
2372 .name = "chardev",
2373 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002374 .params = "",
2375 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002376 .user_print = qemu_chr_info_print,
2377 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002378 },
2379 {
2380 .name = "block",
2381 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002382 .params = "",
2383 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002384 .user_print = bdrv_info_print,
2385 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002386 },
2387 {
2388 .name = "blockstats",
2389 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002390 .params = "",
2391 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002392 .user_print = bdrv_stats_print,
2393 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002394 },
2395 {
2396 .name = "registers",
2397 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 .params = "",
2399 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002400 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002401 },
2402 {
2403 .name = "cpus",
2404 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002405 .params = "",
2406 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002407 .user_print = monitor_print_cpus,
2408 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002409 },
2410 {
2411 .name = "history",
2412 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002413 .params = "",
2414 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002415 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002416 },
2417 {
2418 .name = "irq",
2419 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002420 .params = "",
2421 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002422 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002423 },
2424 {
2425 .name = "pic",
2426 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002427 .params = "",
2428 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002429 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002430 },
2431 {
2432 .name = "pci",
2433 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002434 .params = "",
2435 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002436 .user_print = do_pci_info_print,
2437 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002438 },
aurel327c664e22009-03-03 06:12:22 +00002439#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002440 {
2441 .name = "tlb",
2442 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002443 .params = "",
2444 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002445 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002446 },
aurel327c664e22009-03-03 06:12:22 +00002447#endif
2448#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002449 {
2450 .name = "mem",
2451 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002452 .params = "",
2453 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002454 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002455 },
bellardb86bda52004-09-18 19:32:46 +00002456#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002457 {
2458 .name = "jit",
2459 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002460 .params = "",
2461 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002462 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002463 },
2464 {
2465 .name = "kvm",
2466 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 .params = "",
2468 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002469 .user_print = do_info_kvm_print,
2470 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 },
2472 {
2473 .name = "numa",
2474 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002475 .params = "",
2476 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002477 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002478 },
2479 {
2480 .name = "usb",
2481 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002482 .params = "",
2483 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002484 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002485 },
2486 {
2487 .name = "usbhost",
2488 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002489 .params = "",
2490 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002491 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002492 },
2493 {
2494 .name = "profile",
2495 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002496 .params = "",
2497 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002498 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002499 },
2500 {
2501 .name = "capture",
2502 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002503 .params = "",
2504 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002505 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002506 },
2507 {
2508 .name = "snapshots",
2509 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002510 .params = "",
2511 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002512 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002513 },
2514 {
2515 .name = "status",
2516 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 .params = "",
2518 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002519 .user_print = do_info_status_print,
2520 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 },
2522 {
2523 .name = "pcmcia",
2524 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 .params = "",
2526 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002527 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002528 },
2529 {
2530 .name = "mice",
2531 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 .params = "",
2533 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002534 .user_print = do_info_mice_print,
2535 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 },
2537 {
2538 .name = "vnc",
2539 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 .params = "",
2541 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002542 .user_print = do_info_vnc_print,
2543 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 },
2545 {
2546 .name = "name",
2547 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 .params = "",
2549 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002550 .user_print = do_info_name_print,
2551 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 },
2553 {
2554 .name = "uuid",
2555 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 .params = "",
2557 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002558 .user_print = do_info_uuid_print,
2559 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 },
j_mayer76a66252007-03-07 08:32:30 +00002561#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002562 {
2563 .name = "cpustats",
2564 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 .params = "",
2566 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002567 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 },
j_mayer76a66252007-03-07 08:32:30 +00002569#endif
blueswir131a60e22007-10-26 18:42:59 +00002570#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 {
2572 .name = "usernet",
2573 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 .params = "",
2575 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002576 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 },
blueswir131a60e22007-10-26 18:42:59 +00002578#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 {
2580 .name = "migrate",
2581 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002582 .params = "",
2583 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002584 .user_print = do_info_migrate_print,
2585 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 },
2587 {
2588 .name = "balloon",
2589 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 .params = "",
2591 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002592 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002593 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002594 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 },
2596 {
2597 .name = "qtree",
2598 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 .params = "",
2600 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002601 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 },
2603 {
2604 .name = "qdm",
2605 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 .params = "",
2607 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002608 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 },
2610 {
2611 .name = "roms",
2612 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 .params = "",
2614 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002615 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002616 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302617#if defined(CONFIG_SIMPLE_TRACE)
2618 {
2619 .name = "trace",
2620 .args_type = "",
2621 .params = "",
2622 .help = "show current contents of trace buffer",
2623 .mhandler.info = do_info_trace,
2624 },
2625 {
2626 .name = "trace-events",
2627 .args_type = "",
2628 .params = "",
2629 .help = "show available trace-events & their state",
2630 .mhandler.info = do_info_trace_events,
2631 },
2632#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 {
2634 .name = NULL,
2635 },
bellard9dc39cb2004-03-14 21:38:27 +00002636};
2637
bellard9307c4c2004-04-04 12:57:25 +00002638/*******************************************************************/
2639
2640static const char *pch;
2641static jmp_buf expr_env;
2642
bellard92a31b12005-02-10 22:00:52 +00002643#define MD_TLONG 0
2644#define MD_I32 1
2645
bellard9307c4c2004-04-04 12:57:25 +00002646typedef struct MonitorDef {
2647 const char *name;
2648 int offset;
blueswir18662d652008-10-02 18:32:44 +00002649 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002650 int type;
bellard9307c4c2004-04-04 12:57:25 +00002651} MonitorDef;
2652
bellard57206fd2004-04-25 18:54:52 +00002653#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002654static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002655{
bellard6a00d602005-11-21 23:25:50 +00002656 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002657 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002658}
2659#endif
2660
bellarda541f292004-04-12 20:39:29 +00002661#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002662static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002663{
bellard6a00d602005-11-21 23:25:50 +00002664 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002665 unsigned int u;
2666 int i;
2667
2668 u = 0;
2669 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002670 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002671
2672 return u;
2673}
2674
blueswir18662d652008-10-02 18:32:44 +00002675static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002676{
bellard6a00d602005-11-21 23:25:50 +00002677 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002678 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002679}
2680
blueswir18662d652008-10-02 18:32:44 +00002681static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002682{
bellard6a00d602005-11-21 23:25:50 +00002683 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002684 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002685}
bellard9fddaa02004-05-21 12:59:32 +00002686
blueswir18662d652008-10-02 18:32:44 +00002687static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002688{
bellard6a00d602005-11-21 23:25:50 +00002689 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002690 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002691}
2692
blueswir18662d652008-10-02 18:32:44 +00002693static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002694{
bellard6a00d602005-11-21 23:25:50 +00002695 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002696 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002697}
2698
blueswir18662d652008-10-02 18:32:44 +00002699static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002700{
bellard6a00d602005-11-21 23:25:50 +00002701 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002702 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002703}
bellarda541f292004-04-12 20:39:29 +00002704#endif
2705
bellarde95c8d52004-09-30 22:22:08 +00002706#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002707#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002708static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002709{
bellard6a00d602005-11-21 23:25:50 +00002710 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002711
2712 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002713}
bellard7b936c02005-10-30 17:05:13 +00002714#endif
bellarde95c8d52004-09-30 22:22:08 +00002715
blueswir18662d652008-10-02 18:32:44 +00002716static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002717{
bellard6a00d602005-11-21 23:25:50 +00002718 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002719 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002720}
2721#endif
2722
blueswir18662d652008-10-02 18:32:44 +00002723static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002724#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002725
2726#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002727 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002728 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002729 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002730
bellard9307c4c2004-04-04 12:57:25 +00002731 { "eax", offsetof(CPUState, regs[0]) },
2732 { "ecx", offsetof(CPUState, regs[1]) },
2733 { "edx", offsetof(CPUState, regs[2]) },
2734 { "ebx", offsetof(CPUState, regs[3]) },
2735 { "esp|sp", offsetof(CPUState, regs[4]) },
2736 { "ebp|fp", offsetof(CPUState, regs[5]) },
2737 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002738 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002739#ifdef TARGET_X86_64
2740 { "r8", offsetof(CPUState, regs[8]) },
2741 { "r9", offsetof(CPUState, regs[9]) },
2742 { "r10", offsetof(CPUState, regs[10]) },
2743 { "r11", offsetof(CPUState, regs[11]) },
2744 { "r12", offsetof(CPUState, regs[12]) },
2745 { "r13", offsetof(CPUState, regs[13]) },
2746 { "r14", offsetof(CPUState, regs[14]) },
2747 { "r15", offsetof(CPUState, regs[15]) },
2748#endif
bellard9307c4c2004-04-04 12:57:25 +00002749 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002750 { "eip", offsetof(CPUState, eip) },
2751 SEG("cs", R_CS)
2752 SEG("ds", R_DS)
2753 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002754 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002755 SEG("fs", R_FS)
2756 SEG("gs", R_GS)
2757 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002758#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002759 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002760 { "r0", offsetof(CPUState, gpr[0]) },
2761 { "r1", offsetof(CPUState, gpr[1]) },
2762 { "r2", offsetof(CPUState, gpr[2]) },
2763 { "r3", offsetof(CPUState, gpr[3]) },
2764 { "r4", offsetof(CPUState, gpr[4]) },
2765 { "r5", offsetof(CPUState, gpr[5]) },
2766 { "r6", offsetof(CPUState, gpr[6]) },
2767 { "r7", offsetof(CPUState, gpr[7]) },
2768 { "r8", offsetof(CPUState, gpr[8]) },
2769 { "r9", offsetof(CPUState, gpr[9]) },
2770 { "r10", offsetof(CPUState, gpr[10]) },
2771 { "r11", offsetof(CPUState, gpr[11]) },
2772 { "r12", offsetof(CPUState, gpr[12]) },
2773 { "r13", offsetof(CPUState, gpr[13]) },
2774 { "r14", offsetof(CPUState, gpr[14]) },
2775 { "r15", offsetof(CPUState, gpr[15]) },
2776 { "r16", offsetof(CPUState, gpr[16]) },
2777 { "r17", offsetof(CPUState, gpr[17]) },
2778 { "r18", offsetof(CPUState, gpr[18]) },
2779 { "r19", offsetof(CPUState, gpr[19]) },
2780 { "r20", offsetof(CPUState, gpr[20]) },
2781 { "r21", offsetof(CPUState, gpr[21]) },
2782 { "r22", offsetof(CPUState, gpr[22]) },
2783 { "r23", offsetof(CPUState, gpr[23]) },
2784 { "r24", offsetof(CPUState, gpr[24]) },
2785 { "r25", offsetof(CPUState, gpr[25]) },
2786 { "r26", offsetof(CPUState, gpr[26]) },
2787 { "r27", offsetof(CPUState, gpr[27]) },
2788 { "r28", offsetof(CPUState, gpr[28]) },
2789 { "r29", offsetof(CPUState, gpr[29]) },
2790 { "r30", offsetof(CPUState, gpr[30]) },
2791 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002792 /* Floating point registers */
2793 { "f0", offsetof(CPUState, fpr[0]) },
2794 { "f1", offsetof(CPUState, fpr[1]) },
2795 { "f2", offsetof(CPUState, fpr[2]) },
2796 { "f3", offsetof(CPUState, fpr[3]) },
2797 { "f4", offsetof(CPUState, fpr[4]) },
2798 { "f5", offsetof(CPUState, fpr[5]) },
2799 { "f6", offsetof(CPUState, fpr[6]) },
2800 { "f7", offsetof(CPUState, fpr[7]) },
2801 { "f8", offsetof(CPUState, fpr[8]) },
2802 { "f9", offsetof(CPUState, fpr[9]) },
2803 { "f10", offsetof(CPUState, fpr[10]) },
2804 { "f11", offsetof(CPUState, fpr[11]) },
2805 { "f12", offsetof(CPUState, fpr[12]) },
2806 { "f13", offsetof(CPUState, fpr[13]) },
2807 { "f14", offsetof(CPUState, fpr[14]) },
2808 { "f15", offsetof(CPUState, fpr[15]) },
2809 { "f16", offsetof(CPUState, fpr[16]) },
2810 { "f17", offsetof(CPUState, fpr[17]) },
2811 { "f18", offsetof(CPUState, fpr[18]) },
2812 { "f19", offsetof(CPUState, fpr[19]) },
2813 { "f20", offsetof(CPUState, fpr[20]) },
2814 { "f21", offsetof(CPUState, fpr[21]) },
2815 { "f22", offsetof(CPUState, fpr[22]) },
2816 { "f23", offsetof(CPUState, fpr[23]) },
2817 { "f24", offsetof(CPUState, fpr[24]) },
2818 { "f25", offsetof(CPUState, fpr[25]) },
2819 { "f26", offsetof(CPUState, fpr[26]) },
2820 { "f27", offsetof(CPUState, fpr[27]) },
2821 { "f28", offsetof(CPUState, fpr[28]) },
2822 { "f29", offsetof(CPUState, fpr[29]) },
2823 { "f30", offsetof(CPUState, fpr[30]) },
2824 { "f31", offsetof(CPUState, fpr[31]) },
2825 { "fpscr", offsetof(CPUState, fpscr) },
2826 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002827 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002828 { "lr", offsetof(CPUState, lr) },
2829 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002830 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002831 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002832 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002833 { "msr", 0, &monitor_get_msr, },
2834 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002835 { "tbu", 0, &monitor_get_tbu, },
2836 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002837#if defined(TARGET_PPC64)
2838 /* Address space register */
2839 { "asr", offsetof(CPUState, asr) },
2840#endif
2841 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002842 { "sdr1", offsetof(CPUState, sdr1) },
2843 { "sr0", offsetof(CPUState, sr[0]) },
2844 { "sr1", offsetof(CPUState, sr[1]) },
2845 { "sr2", offsetof(CPUState, sr[2]) },
2846 { "sr3", offsetof(CPUState, sr[3]) },
2847 { "sr4", offsetof(CPUState, sr[4]) },
2848 { "sr5", offsetof(CPUState, sr[5]) },
2849 { "sr6", offsetof(CPUState, sr[6]) },
2850 { "sr7", offsetof(CPUState, sr[7]) },
2851 { "sr8", offsetof(CPUState, sr[8]) },
2852 { "sr9", offsetof(CPUState, sr[9]) },
2853 { "sr10", offsetof(CPUState, sr[10]) },
2854 { "sr11", offsetof(CPUState, sr[11]) },
2855 { "sr12", offsetof(CPUState, sr[12]) },
2856 { "sr13", offsetof(CPUState, sr[13]) },
2857 { "sr14", offsetof(CPUState, sr[14]) },
2858 { "sr15", offsetof(CPUState, sr[15]) },
2859 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002860#elif defined(TARGET_SPARC)
2861 { "g0", offsetof(CPUState, gregs[0]) },
2862 { "g1", offsetof(CPUState, gregs[1]) },
2863 { "g2", offsetof(CPUState, gregs[2]) },
2864 { "g3", offsetof(CPUState, gregs[3]) },
2865 { "g4", offsetof(CPUState, gregs[4]) },
2866 { "g5", offsetof(CPUState, gregs[5]) },
2867 { "g6", offsetof(CPUState, gregs[6]) },
2868 { "g7", offsetof(CPUState, gregs[7]) },
2869 { "o0", 0, monitor_get_reg },
2870 { "o1", 1, monitor_get_reg },
2871 { "o2", 2, monitor_get_reg },
2872 { "o3", 3, monitor_get_reg },
2873 { "o4", 4, monitor_get_reg },
2874 { "o5", 5, monitor_get_reg },
2875 { "o6", 6, monitor_get_reg },
2876 { "o7", 7, monitor_get_reg },
2877 { "l0", 8, monitor_get_reg },
2878 { "l1", 9, monitor_get_reg },
2879 { "l2", 10, monitor_get_reg },
2880 { "l3", 11, monitor_get_reg },
2881 { "l4", 12, monitor_get_reg },
2882 { "l5", 13, monitor_get_reg },
2883 { "l6", 14, monitor_get_reg },
2884 { "l7", 15, monitor_get_reg },
2885 { "i0", 16, monitor_get_reg },
2886 { "i1", 17, monitor_get_reg },
2887 { "i2", 18, monitor_get_reg },
2888 { "i3", 19, monitor_get_reg },
2889 { "i4", 20, monitor_get_reg },
2890 { "i5", 21, monitor_get_reg },
2891 { "i6", 22, monitor_get_reg },
2892 { "i7", 23, monitor_get_reg },
2893 { "pc", offsetof(CPUState, pc) },
2894 { "npc", offsetof(CPUState, npc) },
2895 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002896#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002897 { "psr", 0, &monitor_get_psr, },
2898 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002899#endif
bellarde95c8d52004-09-30 22:22:08 +00002900 { "tbr", offsetof(CPUState, tbr) },
2901 { "fsr", offsetof(CPUState, fsr) },
2902 { "f0", offsetof(CPUState, fpr[0]) },
2903 { "f1", offsetof(CPUState, fpr[1]) },
2904 { "f2", offsetof(CPUState, fpr[2]) },
2905 { "f3", offsetof(CPUState, fpr[3]) },
2906 { "f4", offsetof(CPUState, fpr[4]) },
2907 { "f5", offsetof(CPUState, fpr[5]) },
2908 { "f6", offsetof(CPUState, fpr[6]) },
2909 { "f7", offsetof(CPUState, fpr[7]) },
2910 { "f8", offsetof(CPUState, fpr[8]) },
2911 { "f9", offsetof(CPUState, fpr[9]) },
2912 { "f10", offsetof(CPUState, fpr[10]) },
2913 { "f11", offsetof(CPUState, fpr[11]) },
2914 { "f12", offsetof(CPUState, fpr[12]) },
2915 { "f13", offsetof(CPUState, fpr[13]) },
2916 { "f14", offsetof(CPUState, fpr[14]) },
2917 { "f15", offsetof(CPUState, fpr[15]) },
2918 { "f16", offsetof(CPUState, fpr[16]) },
2919 { "f17", offsetof(CPUState, fpr[17]) },
2920 { "f18", offsetof(CPUState, fpr[18]) },
2921 { "f19", offsetof(CPUState, fpr[19]) },
2922 { "f20", offsetof(CPUState, fpr[20]) },
2923 { "f21", offsetof(CPUState, fpr[21]) },
2924 { "f22", offsetof(CPUState, fpr[22]) },
2925 { "f23", offsetof(CPUState, fpr[23]) },
2926 { "f24", offsetof(CPUState, fpr[24]) },
2927 { "f25", offsetof(CPUState, fpr[25]) },
2928 { "f26", offsetof(CPUState, fpr[26]) },
2929 { "f27", offsetof(CPUState, fpr[27]) },
2930 { "f28", offsetof(CPUState, fpr[28]) },
2931 { "f29", offsetof(CPUState, fpr[29]) },
2932 { "f30", offsetof(CPUState, fpr[30]) },
2933 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002934#ifdef TARGET_SPARC64
2935 { "f32", offsetof(CPUState, fpr[32]) },
2936 { "f34", offsetof(CPUState, fpr[34]) },
2937 { "f36", offsetof(CPUState, fpr[36]) },
2938 { "f38", offsetof(CPUState, fpr[38]) },
2939 { "f40", offsetof(CPUState, fpr[40]) },
2940 { "f42", offsetof(CPUState, fpr[42]) },
2941 { "f44", offsetof(CPUState, fpr[44]) },
2942 { "f46", offsetof(CPUState, fpr[46]) },
2943 { "f48", offsetof(CPUState, fpr[48]) },
2944 { "f50", offsetof(CPUState, fpr[50]) },
2945 { "f52", offsetof(CPUState, fpr[52]) },
2946 { "f54", offsetof(CPUState, fpr[54]) },
2947 { "f56", offsetof(CPUState, fpr[56]) },
2948 { "f58", offsetof(CPUState, fpr[58]) },
2949 { "f60", offsetof(CPUState, fpr[60]) },
2950 { "f62", offsetof(CPUState, fpr[62]) },
2951 { "asi", offsetof(CPUState, asi) },
2952 { "pstate", offsetof(CPUState, pstate) },
2953 { "cansave", offsetof(CPUState, cansave) },
2954 { "canrestore", offsetof(CPUState, canrestore) },
2955 { "otherwin", offsetof(CPUState, otherwin) },
2956 { "wstate", offsetof(CPUState, wstate) },
2957 { "cleanwin", offsetof(CPUState, cleanwin) },
2958 { "fprs", offsetof(CPUState, fprs) },
2959#endif
bellard9307c4c2004-04-04 12:57:25 +00002960#endif
2961 { NULL },
2962};
2963
aliguori376253e2009-03-05 23:01:23 +00002964static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002965{
aliguori376253e2009-03-05 23:01:23 +00002966 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002967 longjmp(expr_env, 1);
2968}
2969
Markus Armbruster09b94182010-01-20 13:07:30 +01002970/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002971static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002972{
blueswir18662d652008-10-02 18:32:44 +00002973 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002974 void *ptr;
2975
bellard9307c4c2004-04-04 12:57:25 +00002976 for(md = monitor_defs; md->name != NULL; md++) {
2977 if (compare_cmd(name, md->name)) {
2978 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002979 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002980 } else {
bellard6a00d602005-11-21 23:25:50 +00002981 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002982 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002983 switch(md->type) {
2984 case MD_I32:
2985 *pval = *(int32_t *)ptr;
2986 break;
2987 case MD_TLONG:
2988 *pval = *(target_long *)ptr;
2989 break;
2990 default:
2991 *pval = 0;
2992 break;
2993 }
bellard9307c4c2004-04-04 12:57:25 +00002994 }
2995 return 0;
2996 }
2997 }
2998 return -1;
2999}
3000
3001static void next(void)
3002{
Blue Swirl660f11b2009-07-31 21:16:51 +00003003 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003004 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003005 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003006 pch++;
3007 }
3008}
3009
aliguori376253e2009-03-05 23:01:23 +00003010static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003011
aliguori376253e2009-03-05 23:01:23 +00003012static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003013{
blueswir1c2efc952007-09-25 17:28:42 +00003014 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003015 char *p;
bellard6a00d602005-11-21 23:25:50 +00003016 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003017
3018 switch(*pch) {
3019 case '+':
3020 next();
aliguori376253e2009-03-05 23:01:23 +00003021 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003022 break;
3023 case '-':
3024 next();
aliguori376253e2009-03-05 23:01:23 +00003025 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003026 break;
3027 case '~':
3028 next();
aliguori376253e2009-03-05 23:01:23 +00003029 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003030 break;
3031 case '(':
3032 next();
aliguori376253e2009-03-05 23:01:23 +00003033 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003034 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003035 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003036 }
3037 next();
3038 break;
bellard81d09122004-07-14 17:21:37 +00003039 case '\'':
3040 pch++;
3041 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003042 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003043 n = *pch;
3044 pch++;
3045 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003046 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003047 next();
3048 break;
bellard9307c4c2004-04-04 12:57:25 +00003049 case '$':
3050 {
3051 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003052 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003053
bellard9307c4c2004-04-04 12:57:25 +00003054 pch++;
3055 q = buf;
3056 while ((*pch >= 'a' && *pch <= 'z') ||
3057 (*pch >= 'A' && *pch <= 'Z') ||
3058 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003059 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003060 if ((q - buf) < sizeof(buf) - 1)
3061 *q++ = *pch;
3062 pch++;
3063 }
blueswir1cd390082008-11-16 13:53:32 +00003064 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003065 pch++;
3066 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003067 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003068 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003069 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003070 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003071 }
3072 break;
3073 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003074 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003075 n = 0;
3076 break;
3077 default:
blueswir17743e582007-09-24 18:39:04 +00003078#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003079 n = strtoull(pch, &p, 0);
3080#else
bellard9307c4c2004-04-04 12:57:25 +00003081 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003082#endif
bellard9307c4c2004-04-04 12:57:25 +00003083 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003084 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003085 }
3086 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003087 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003088 pch++;
3089 break;
3090 }
3091 return n;
3092}
3093
3094
aliguori376253e2009-03-05 23:01:23 +00003095static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003096{
blueswir1c2efc952007-09-25 17:28:42 +00003097 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003098 int op;
ths3b46e622007-09-17 08:09:54 +00003099
aliguori376253e2009-03-05 23:01:23 +00003100 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003101 for(;;) {
3102 op = *pch;
3103 if (op != '*' && op != '/' && op != '%')
3104 break;
3105 next();
aliguori376253e2009-03-05 23:01:23 +00003106 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003107 switch(op) {
3108 default:
3109 case '*':
3110 val *= val2;
3111 break;
3112 case '/':
3113 case '%':
ths5fafdf22007-09-16 21:08:06 +00003114 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003115 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003116 if (op == '/')
3117 val /= val2;
3118 else
3119 val %= val2;
3120 break;
3121 }
3122 }
3123 return val;
3124}
3125
aliguori376253e2009-03-05 23:01:23 +00003126static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003127{
blueswir1c2efc952007-09-25 17:28:42 +00003128 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003129 int op;
bellard9307c4c2004-04-04 12:57:25 +00003130
aliguori376253e2009-03-05 23:01:23 +00003131 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003132 for(;;) {
3133 op = *pch;
3134 if (op != '&' && op != '|' && op != '^')
3135 break;
3136 next();
aliguori376253e2009-03-05 23:01:23 +00003137 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003138 switch(op) {
3139 default:
3140 case '&':
3141 val &= val2;
3142 break;
3143 case '|':
3144 val |= val2;
3145 break;
3146 case '^':
3147 val ^= val2;
3148 break;
3149 }
3150 }
3151 return val;
3152}
3153
aliguori376253e2009-03-05 23:01:23 +00003154static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003155{
blueswir1c2efc952007-09-25 17:28:42 +00003156 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003157 int op;
bellard9307c4c2004-04-04 12:57:25 +00003158
aliguori376253e2009-03-05 23:01:23 +00003159 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003160 for(;;) {
3161 op = *pch;
3162 if (op != '+' && op != '-')
3163 break;
3164 next();
aliguori376253e2009-03-05 23:01:23 +00003165 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003166 if (op == '+')
3167 val += val2;
3168 else
3169 val -= val2;
3170 }
3171 return val;
3172}
3173
aliguori376253e2009-03-05 23:01:23 +00003174static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003175{
3176 pch = *pp;
3177 if (setjmp(expr_env)) {
3178 *pp = pch;
3179 return -1;
3180 }
blueswir1cd390082008-11-16 13:53:32 +00003181 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003182 pch++;
aliguori376253e2009-03-05 23:01:23 +00003183 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003184 *pp = pch;
3185 return 0;
3186}
3187
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003188static int get_double(Monitor *mon, double *pval, const char **pp)
3189{
3190 const char *p = *pp;
3191 char *tailp;
3192 double d;
3193
3194 d = strtod(p, &tailp);
3195 if (tailp == p) {
3196 monitor_printf(mon, "Number expected\n");
3197 return -1;
3198 }
3199 if (d != d || d - d != 0) {
3200 /* NaN or infinity */
3201 monitor_printf(mon, "Bad number\n");
3202 return -1;
3203 }
3204 *pval = d;
3205 *pp = tailp;
3206 return 0;
3207}
3208
bellard9307c4c2004-04-04 12:57:25 +00003209static int get_str(char *buf, int buf_size, const char **pp)
3210{
3211 const char *p;
3212 char *q;
3213 int c;
3214
bellard81d09122004-07-14 17:21:37 +00003215 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003216 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003217 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003218 p++;
3219 if (*p == '\0') {
3220 fail:
bellard81d09122004-07-14 17:21:37 +00003221 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003222 *pp = p;
3223 return -1;
3224 }
bellard9307c4c2004-04-04 12:57:25 +00003225 if (*p == '\"') {
3226 p++;
3227 while (*p != '\0' && *p != '\"') {
3228 if (*p == '\\') {
3229 p++;
3230 c = *p++;
3231 switch(c) {
3232 case 'n':
3233 c = '\n';
3234 break;
3235 case 'r':
3236 c = '\r';
3237 break;
3238 case '\\':
3239 case '\'':
3240 case '\"':
3241 break;
3242 default:
3243 qemu_printf("unsupported escape code: '\\%c'\n", c);
3244 goto fail;
3245 }
3246 if ((q - buf) < buf_size - 1) {
3247 *q++ = c;
3248 }
3249 } else {
3250 if ((q - buf) < buf_size - 1) {
3251 *q++ = *p;
3252 }
3253 p++;
3254 }
3255 }
3256 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003257 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003258 goto fail;
3259 }
3260 p++;
3261 } else {
blueswir1cd390082008-11-16 13:53:32 +00003262 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003263 if ((q - buf) < buf_size - 1) {
3264 *q++ = *p;
3265 }
3266 p++;
3267 }
bellard9307c4c2004-04-04 12:57:25 +00003268 }
bellard81d09122004-07-14 17:21:37 +00003269 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003270 *pp = p;
3271 return 0;
3272}
3273
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003274/*
3275 * Store the command-name in cmdname, and return a pointer to
3276 * the remaining of the command string.
3277 */
3278static const char *get_command_name(const char *cmdline,
3279 char *cmdname, size_t nlen)
3280{
3281 size_t len;
3282 const char *p, *pstart;
3283
3284 p = cmdline;
3285 while (qemu_isspace(*p))
3286 p++;
3287 if (*p == '\0')
3288 return NULL;
3289 pstart = p;
3290 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3291 p++;
3292 len = p - pstart;
3293 if (len > nlen - 1)
3294 len = nlen - 1;
3295 memcpy(cmdname, pstart, len);
3296 cmdname[len] = '\0';
3297 return p;
3298}
3299
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003300/**
3301 * Read key of 'type' into 'key' and return the current
3302 * 'type' pointer.
3303 */
3304static char *key_get_info(const char *type, char **key)
3305{
3306 size_t len;
3307 char *p, *str;
3308
3309 if (*type == ',')
3310 type++;
3311
3312 p = strchr(type, ':');
3313 if (!p) {
3314 *key = NULL;
3315 return NULL;
3316 }
3317 len = p - type;
3318
3319 str = qemu_malloc(len + 1);
3320 memcpy(str, type, len);
3321 str[len] = '\0';
3322
3323 *key = str;
3324 return ++p;
3325}
3326
bellard9307c4c2004-04-04 12:57:25 +00003327static int default_fmt_format = 'x';
3328static int default_fmt_size = 4;
3329
3330#define MAX_ARGS 16
3331
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003332static int is_valid_option(const char *c, const char *typestr)
3333{
3334 char option[3];
3335
3336 option[0] = '-';
3337 option[1] = *c;
3338 option[2] = '\0';
3339
3340 typestr = strstr(typestr, option);
3341 return (typestr != NULL);
3342}
3343
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003344static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3345 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003346{
3347 const mon_cmd_t *cmd;
3348
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003349 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003350 if (compare_cmd(cmdname, cmd->name)) {
3351 return cmd;
3352 }
3353 }
3354
3355 return NULL;
3356}
3357
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003358static const mon_cmd_t *monitor_find_command(const char *cmdname)
3359{
3360 return search_dispatch_table(mon_cmds, cmdname);
3361}
3362
Luiz Capitulino030db6e2010-09-10 16:03:38 -03003363static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3364{
3365 return search_dispatch_table(info_cmds, info_item);
3366}
3367
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003368static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3369{
3370 return search_dispatch_table(mon_cmds, cmdname);
3371}
3372
Anthony Liguoric227f092009-10-01 16:12:16 -05003373static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003374 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003375 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003376{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003377 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003378 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003379 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003380 char cmdname[256];
3381 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003382 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003383
3384#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003385 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003386#endif
ths3b46e622007-09-17 08:09:54 +00003387
bellard9307c4c2004-04-04 12:57:25 +00003388 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003389 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3390 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003391 return NULL;
ths3b46e622007-09-17 08:09:54 +00003392
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003393 cmd = monitor_find_command(cmdname);
3394 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003395 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003396 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003397 }
bellard9307c4c2004-04-04 12:57:25 +00003398
bellard9307c4c2004-04-04 12:57:25 +00003399 /* parse the parameters */
3400 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003401 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003402 typestr = key_get_info(typestr, &key);
3403 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003404 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003405 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003406 typestr++;
3407 switch(c) {
3408 case 'F':
bellard81d09122004-07-14 17:21:37 +00003409 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003410 case 's':
3411 {
3412 int ret;
ths3b46e622007-09-17 08:09:54 +00003413
blueswir1cd390082008-11-16 13:53:32 +00003414 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003415 p++;
3416 if (*typestr == '?') {
3417 typestr++;
3418 if (*p == '\0') {
3419 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003420 break;
bellard9307c4c2004-04-04 12:57:25 +00003421 }
3422 }
3423 ret = get_str(buf, sizeof(buf), &p);
3424 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003425 switch(c) {
3426 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003427 monitor_printf(mon, "%s: filename expected\n",
3428 cmdname);
bellard81d09122004-07-14 17:21:37 +00003429 break;
3430 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003431 monitor_printf(mon, "%s: block device name expected\n",
3432 cmdname);
bellard81d09122004-07-14 17:21:37 +00003433 break;
3434 default:
aliguori376253e2009-03-05 23:01:23 +00003435 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003436 break;
3437 }
bellard9307c4c2004-04-04 12:57:25 +00003438 goto fail;
3439 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003440 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003441 }
3442 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003443 case 'O':
3444 {
3445 QemuOptsList *opts_list;
3446 QemuOpts *opts;
3447
3448 opts_list = qemu_find_opts(key);
3449 if (!opts_list || opts_list->desc->name) {
3450 goto bad_type;
3451 }
3452 while (qemu_isspace(*p)) {
3453 p++;
3454 }
3455 if (!*p)
3456 break;
3457 if (get_str(buf, sizeof(buf), &p) < 0) {
3458 goto fail;
3459 }
3460 opts = qemu_opts_parse(opts_list, buf, 1);
3461 if (!opts) {
3462 goto fail;
3463 }
3464 qemu_opts_to_qdict(opts, qdict);
3465 qemu_opts_del(opts);
3466 }
3467 break;
bellard9307c4c2004-04-04 12:57:25 +00003468 case '/':
3469 {
3470 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003471
blueswir1cd390082008-11-16 13:53:32 +00003472 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003473 p++;
3474 if (*p == '/') {
3475 /* format found */
3476 p++;
3477 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003478 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003479 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003480 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003481 count = count * 10 + (*p - '0');
3482 p++;
3483 }
3484 }
3485 size = -1;
3486 format = -1;
3487 for(;;) {
3488 switch(*p) {
3489 case 'o':
3490 case 'd':
3491 case 'u':
3492 case 'x':
3493 case 'i':
3494 case 'c':
3495 format = *p++;
3496 break;
3497 case 'b':
3498 size = 1;
3499 p++;
3500 break;
3501 case 'h':
3502 size = 2;
3503 p++;
3504 break;
3505 case 'w':
3506 size = 4;
3507 p++;
3508 break;
3509 case 'g':
3510 case 'L':
3511 size = 8;
3512 p++;
3513 break;
3514 default:
3515 goto next;
3516 }
3517 }
3518 next:
blueswir1cd390082008-11-16 13:53:32 +00003519 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003520 monitor_printf(mon, "invalid char in format: '%c'\n",
3521 *p);
bellard9307c4c2004-04-04 12:57:25 +00003522 goto fail;
3523 }
bellard9307c4c2004-04-04 12:57:25 +00003524 if (format < 0)
3525 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003526 if (format != 'i') {
3527 /* for 'i', not specifying a size gives -1 as size */
3528 if (size < 0)
3529 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003530 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003531 }
bellard9307c4c2004-04-04 12:57:25 +00003532 default_fmt_format = format;
3533 } else {
3534 count = 1;
3535 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003536 if (format != 'i') {
3537 size = default_fmt_size;
3538 } else {
3539 size = -1;
3540 }
bellard9307c4c2004-04-04 12:57:25 +00003541 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003542 qdict_put(qdict, "count", qint_from_int(count));
3543 qdict_put(qdict, "format", qint_from_int(format));
3544 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003545 }
3546 break;
3547 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003548 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003549 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003550 {
blueswir1c2efc952007-09-25 17:28:42 +00003551 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003552
blueswir1cd390082008-11-16 13:53:32 +00003553 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003554 p++;
bellard34405572004-06-08 00:55:58 +00003555 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003556 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003557 if (*p == '\0') {
3558 typestr++;
3559 break;
3560 }
bellard34405572004-06-08 00:55:58 +00003561 } else {
3562 if (*p == '.') {
3563 p++;
blueswir1cd390082008-11-16 13:53:32 +00003564 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003565 p++;
bellard34405572004-06-08 00:55:58 +00003566 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003567 typestr++;
3568 break;
bellard34405572004-06-08 00:55:58 +00003569 }
3570 }
bellard13224a82006-07-14 22:03:35 +00003571 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003572 }
aliguori376253e2009-03-05 23:01:23 +00003573 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003574 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003575 /* Check if 'i' is greater than 32-bit */
3576 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3577 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3578 monitor_printf(mon, "integer is for 32-bit values\n");
3579 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003580 } else if (c == 'M') {
3581 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003582 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003583 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003584 }
3585 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003586 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003587 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003588 {
3589 double val;
3590
3591 while (qemu_isspace(*p))
3592 p++;
3593 if (*typestr == '?') {
3594 typestr++;
3595 if (*p == '\0') {
3596 break;
3597 }
3598 }
3599 if (get_double(mon, &val, &p) < 0) {
3600 goto fail;
3601 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003602 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003603 switch (*p) {
3604 case 'K': case 'k':
3605 val *= 1 << 10; p++; break;
3606 case 'M': case 'm':
3607 val *= 1 << 20; p++; break;
3608 case 'G': case 'g':
3609 val *= 1 << 30; p++; break;
3610 }
3611 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003612 if (c == 'T' && p[0] && p[1] == 's') {
3613 switch (*p) {
3614 case 'm':
3615 val /= 1e3; p += 2; break;
3616 case 'u':
3617 val /= 1e6; p += 2; break;
3618 case 'n':
3619 val /= 1e9; p += 2; break;
3620 }
3621 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003622 if (*p && !qemu_isspace(*p)) {
3623 monitor_printf(mon, "Unknown unit suffix\n");
3624 goto fail;
3625 }
3626 qdict_put(qdict, key, qfloat_from_double(val));
3627 }
3628 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003629 case 'b':
3630 {
3631 const char *beg;
3632 int val;
3633
3634 while (qemu_isspace(*p)) {
3635 p++;
3636 }
3637 beg = p;
3638 while (qemu_isgraph(*p)) {
3639 p++;
3640 }
3641 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3642 val = 1;
3643 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3644 val = 0;
3645 } else {
3646 monitor_printf(mon, "Expected 'on' or 'off'\n");
3647 goto fail;
3648 }
3649 qdict_put(qdict, key, qbool_from_int(val));
3650 }
3651 break;
bellard9307c4c2004-04-04 12:57:25 +00003652 case '-':
3653 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003654 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003655 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003656 /* option */
ths3b46e622007-09-17 08:09:54 +00003657
bellard9307c4c2004-04-04 12:57:25 +00003658 c = *typestr++;
3659 if (c == '\0')
3660 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003661 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003662 p++;
bellard9307c4c2004-04-04 12:57:25 +00003663 if (*p == '-') {
3664 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003665 if(c != *p) {
3666 if(!is_valid_option(p, typestr)) {
3667
3668 monitor_printf(mon, "%s: unsupported option -%c\n",
3669 cmdname, *p);
3670 goto fail;
3671 } else {
3672 skip_key = 1;
3673 }
bellard9307c4c2004-04-04 12:57:25 +00003674 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003675 if(skip_key) {
3676 p = tmp;
3677 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003678 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003679 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003680 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003681 }
bellard9307c4c2004-04-04 12:57:25 +00003682 }
bellard9307c4c2004-04-04 12:57:25 +00003683 }
3684 break;
3685 default:
3686 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003687 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003688 goto fail;
3689 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003690 qemu_free(key);
3691 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003692 }
3693 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003694 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003695 p++;
3696 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003697 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3698 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003699 goto fail;
3700 }
3701
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003702 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003703
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003704fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003705 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003706 return NULL;
3707}
3708
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003709void monitor_set_error(Monitor *mon, QError *qerror)
3710{
3711 /* report only the first error */
3712 if (!mon->error) {
3713 mon->error = qerror;
3714 } else {
3715 MON_DEBUG("Additional error report at %s:%d\n",
3716 qerror->file, qerror->linenr);
3717 QDECREF(qerror);
3718 }
3719}
3720
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003721static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3722{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003723 if (monitor_ctrl_mode(mon)) {
3724 if (ret && !monitor_has_error(mon)) {
3725 /*
3726 * If it returns failure, it must have passed on error.
3727 *
3728 * Action: Report an internal error to the client if in QMP.
3729 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003730 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003731 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3732 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003733 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003734
3735#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003736 if (!ret && monitor_has_error(mon)) {
3737 /*
3738 * If it returns success, it must not have passed an error.
3739 *
3740 * Action: Report the passed error to the client.
3741 */
3742 MON_DEBUG("command '%s' returned success but passed an error\n",
3743 cmd->name);
3744 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003745
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003746 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3747 /*
3748 * Handlers should not call Monitor print functions.
3749 *
3750 * Action: Ignore them in QMP.
3751 *
3752 * (XXX: we don't check any 'info' or 'query' command here
3753 * because the user print function _is_ called by do_info(), hence
3754 * we will trigger this check. This problem will go away when we
3755 * make 'query' commands real and kill do_info())
3756 */
3757 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3758 cmd->name, mon_print_count_get(mon));
3759 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003760#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003761 } else {
3762 assert(!monitor_has_error(mon));
3763 QDECREF(mon->error);
3764 mon->error = NULL;
3765 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003766}
3767
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003768static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3769 const QDict *params)
3770{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003771 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003772 QObject *data = NULL;
3773
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003774 mon_print_count_init(mon);
3775
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003776 ret = cmd->mhandler.cmd_new(mon, params, &data);
3777 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003778
Luiz Capitulinod4551292010-09-15 16:56:48 -03003779 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003780 /* Monitor Protocol */
3781 monitor_protocol_emitter(mon, data);
3782 } else {
3783 /* User Protocol */
3784 if (data)
3785 cmd->user_print(mon, data);
3786 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003787
3788 qobject_decref(data);
3789}
3790
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003791static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003792{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003793 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003794 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003795
3796 qdict = qdict_new();
3797
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003798 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003799 if (!cmd)
3800 goto out;
3801
Adam Litke940cc302010-01-25 12:18:44 -06003802 if (monitor_handler_is_async(cmd)) {
3803 user_async_cmd_handler(mon, cmd, qdict);
3804 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003805 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003806 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003807 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003808 }
3809
Luiz Capitulino13917be2009-10-07 13:41:54 -03003810out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003811 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003812}
3813
bellard81d09122004-07-14 17:21:37 +00003814static void cmd_completion(const char *name, const char *list)
3815{
3816 const char *p, *pstart;
3817 char cmd[128];
3818 int len;
3819
3820 p = list;
3821 for(;;) {
3822 pstart = p;
3823 p = strchr(p, '|');
3824 if (!p)
3825 p = pstart + strlen(pstart);
3826 len = p - pstart;
3827 if (len > sizeof(cmd) - 2)
3828 len = sizeof(cmd) - 2;
3829 memcpy(cmd, pstart, len);
3830 cmd[len] = '\0';
3831 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003832 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003833 }
3834 if (*p == '\0')
3835 break;
3836 p++;
3837 }
3838}
3839
3840static void file_completion(const char *input)
3841{
3842 DIR *ffs;
3843 struct dirent *d;
3844 char path[1024];
3845 char file[1024], file_prefix[1024];
3846 int input_path_len;
3847 const char *p;
3848
ths5fafdf22007-09-16 21:08:06 +00003849 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003850 if (!p) {
3851 input_path_len = 0;
3852 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003853 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003854 } else {
3855 input_path_len = p - input + 1;
3856 memcpy(path, input, input_path_len);
3857 if (input_path_len > sizeof(path) - 1)
3858 input_path_len = sizeof(path) - 1;
3859 path[input_path_len] = '\0';
3860 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3861 }
3862#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003863 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3864 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003865#endif
3866 ffs = opendir(path);
3867 if (!ffs)
3868 return;
3869 for(;;) {
3870 struct stat sb;
3871 d = readdir(ffs);
3872 if (!d)
3873 break;
3874 if (strstart(d->d_name, file_prefix, NULL)) {
3875 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003876 if (input_path_len < sizeof(file))
3877 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3878 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003879 /* stat the file to find out if it's a directory.
3880 * In that case add a slash to speed up typing long paths
3881 */
3882 stat(file, &sb);
3883 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003884 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003885 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003886 }
3887 }
3888 closedir(ffs);
3889}
3890
aliguori51de9762009-03-05 23:00:43 +00003891static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003892{
aliguori51de9762009-03-05 23:00:43 +00003893 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003894 const char *input = opaque;
3895
3896 if (input[0] == '\0' ||
3897 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003898 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003899 }
3900}
3901
3902/* NOTE: this parser is an approximate form of the real command parser */
3903static void parse_cmdline(const char *cmdline,
3904 int *pnb_args, char **args)
3905{
3906 const char *p;
3907 int nb_args, ret;
3908 char buf[1024];
3909
3910 p = cmdline;
3911 nb_args = 0;
3912 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003913 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003914 p++;
3915 if (*p == '\0')
3916 break;
3917 if (nb_args >= MAX_ARGS)
3918 break;
3919 ret = get_str(buf, sizeof(buf), &p);
3920 args[nb_args] = qemu_strdup(buf);
3921 nb_args++;
3922 if (ret < 0)
3923 break;
3924 }
3925 *pnb_args = nb_args;
3926}
3927
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003928static const char *next_arg_type(const char *typestr)
3929{
3930 const char *p = strchr(typestr, ':');
3931 return (p != NULL ? ++p : typestr);
3932}
3933
aliguori4c36ba32009-03-05 23:01:37 +00003934static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003935{
3936 const char *cmdname;
3937 char *args[MAX_ARGS];
3938 int nb_args, i, len;
3939 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003940 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003941 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003942
3943 parse_cmdline(cmdline, &nb_args, args);
3944#ifdef DEBUG_COMPLETION
3945 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003946 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003947 }
3948#endif
3949
3950 /* if the line ends with a space, it means we want to complete the
3951 next arg */
3952 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003953 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003954 if (nb_args >= MAX_ARGS) {
3955 goto cleanup;
3956 }
bellard81d09122004-07-14 17:21:37 +00003957 args[nb_args++] = qemu_strdup("");
3958 }
3959 if (nb_args <= 1) {
3960 /* command completion */
3961 if (nb_args == 0)
3962 cmdname = "";
3963 else
3964 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003965 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003966 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003967 cmd_completion(cmdname, cmd->name);
3968 }
3969 } else {
3970 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02003971 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3972 if (compare_cmd(args[0], cmd->name)) {
3973 break;
3974 }
bellard81d09122004-07-14 17:21:37 +00003975 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003976 if (!cmd->name) {
3977 goto cleanup;
3978 }
3979
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003980 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003981 for(i = 0; i < nb_args - 2; i++) {
3982 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003983 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003984 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003985 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003986 }
3987 }
3988 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003989 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003990 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003991 }
bellard81d09122004-07-14 17:21:37 +00003992 switch(*ptype) {
3993 case 'F':
3994 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003995 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003996 file_completion(str);
3997 break;
3998 case 'B':
3999 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004000 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004001 bdrv_iterate(block_completion_it, (void *)str);
4002 break;
bellard7fe48482004-10-09 18:08:01 +00004003 case 's':
4004 /* XXX: more generic ? */
4005 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004006 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004007 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4008 cmd_completion(str, cmd->name);
4009 }
bellard64866c32006-05-07 18:03:31 +00004010 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004011 char *sep = strrchr(str, '-');
4012 if (sep)
4013 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004014 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004015 for(key = key_defs; key->name != NULL; key++) {
4016 cmd_completion(str, key->name);
4017 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004018 } else if (!strcmp(cmd->name, "help|?")) {
4019 readline_set_completion_index(cur_mon->rs, strlen(str));
4020 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4021 cmd_completion(str, cmd->name);
4022 }
bellard7fe48482004-10-09 18:08:01 +00004023 }
4024 break;
bellard81d09122004-07-14 17:21:37 +00004025 default:
4026 break;
4027 }
4028 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004029
4030cleanup:
4031 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00004032 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004033 }
bellard81d09122004-07-14 17:21:37 +00004034}
4035
aliguori731b0362009-03-05 23:01:42 +00004036static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004037{
aliguori731b0362009-03-05 23:01:42 +00004038 Monitor *mon = opaque;
4039
Jan Kiszkac62313b2009-12-04 14:05:29 +01004040 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004041}
4042
Luiz Capitulino09069b12010-02-04 18:10:06 -02004043static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4044{
4045 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4046 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4047}
4048
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004049/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004050 * Argument validation rules:
4051 *
4052 * 1. The argument must exist in cmd_args qdict
4053 * 2. The argument type must be the expected one
4054 *
4055 * Special case: If the argument doesn't exist in cmd_args and
4056 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4057 * checking is skipped for it.
4058 */
4059static int check_client_args_type(const QDict *client_args,
4060 const QDict *cmd_args, int flags)
4061{
4062 const QDictEntry *ent;
4063
4064 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4065 QObject *obj;
4066 QString *arg_type;
4067 const QObject *client_arg = qdict_entry_value(ent);
4068 const char *client_arg_name = qdict_entry_key(ent);
4069
4070 obj = qdict_get(cmd_args, client_arg_name);
4071 if (!obj) {
4072 if (flags & QMP_ACCEPT_UNKNOWNS) {
4073 /* handler accepts unknowns */
4074 continue;
4075 }
4076 /* client arg doesn't exist */
4077 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4078 return -1;
4079 }
4080
4081 arg_type = qobject_to_qstring(obj);
4082 assert(arg_type != NULL);
4083
4084 /* check if argument's type is correct */
4085 switch (qstring_get_str(arg_type)[0]) {
4086 case 'F':
4087 case 'B':
4088 case 's':
4089 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4090 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4091 "string");
4092 return -1;
4093 }
4094 break;
4095 case 'i':
4096 case 'l':
4097 case 'M':
4098 if (qobject_type(client_arg) != QTYPE_QINT) {
4099 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4100 "int");
4101 return -1;
4102 }
4103 break;
4104 case 'f':
4105 case 'T':
4106 if (qobject_type(client_arg) != QTYPE_QINT &&
4107 qobject_type(client_arg) != QTYPE_QFLOAT) {
4108 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4109 "number");
4110 return -1;
4111 }
4112 break;
4113 case 'b':
4114 case '-':
4115 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4116 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4117 "bool");
4118 return -1;
4119 }
4120 break;
4121 case 'O':
4122 assert(flags & QMP_ACCEPT_UNKNOWNS);
4123 break;
4124 case '/':
4125 case '.':
4126 /*
4127 * These types are not supported by QMP and thus are not
4128 * handled here. Fall through.
4129 */
4130 default:
4131 abort();
4132 }
4133 }
4134
4135 return 0;
4136}
4137
4138/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004139 * - Check if the client has passed all mandatory args
4140 * - Set special flags for argument validation
4141 */
4142static int check_mandatory_args(const QDict *cmd_args,
4143 const QDict *client_args, int *flags)
4144{
4145 const QDictEntry *ent;
4146
4147 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4148 const char *cmd_arg_name = qdict_entry_key(ent);
4149 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4150 assert(type != NULL);
4151
4152 if (qstring_get_str(type)[0] == 'O') {
4153 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4154 *flags |= QMP_ACCEPT_UNKNOWNS;
4155 } else if (qstring_get_str(type)[0] != '-' &&
4156 qstring_get_str(type)[1] != '?' &&
4157 !qdict_haskey(client_args, cmd_arg_name)) {
4158 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4159 return -1;
4160 }
4161 }
4162
4163 return 0;
4164}
4165
4166static QDict *qdict_from_args_type(const char *args_type)
4167{
4168 int i;
4169 QDict *qdict;
4170 QString *key, *type, *cur_qs;
4171
4172 assert(args_type != NULL);
4173
4174 qdict = qdict_new();
4175
4176 if (args_type == NULL || args_type[0] == '\0') {
4177 /* no args, empty qdict */
4178 goto out;
4179 }
4180
4181 key = qstring_new();
4182 type = qstring_new();
4183
4184 cur_qs = key;
4185
4186 for (i = 0;; i++) {
4187 switch (args_type[i]) {
4188 case ',':
4189 case '\0':
4190 qdict_put(qdict, qstring_get_str(key), type);
4191 QDECREF(key);
4192 if (args_type[i] == '\0') {
4193 goto out;
4194 }
4195 type = qstring_new(); /* qdict has ref */
4196 cur_qs = key = qstring_new();
4197 break;
4198 case ':':
4199 cur_qs = type;
4200 break;
4201 default:
4202 qstring_append_chr(cur_qs, args_type[i]);
4203 break;
4204 }
4205 }
4206
4207out:
4208 return qdict;
4209}
4210
4211/*
4212 * Client argument checking rules:
4213 *
4214 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004215 * 2. Each argument provided by the client must be expected
4216 * 3. Each argument provided by the client must have the type expected
4217 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004218 */
4219static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4220{
4221 int flags, err;
4222 QDict *cmd_args;
4223
4224 cmd_args = qdict_from_args_type(cmd->args_type);
4225
4226 flags = 0;
4227 err = check_mandatory_args(cmd_args, client_args, &flags);
4228 if (err) {
4229 goto out;
4230 }
4231
Luiz Capitulino4af91932010-06-22 11:44:05 -03004232 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004233
4234out:
4235 QDECREF(cmd_args);
4236 return err;
4237}
4238
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004239/*
4240 * Input object checking rules
4241 *
4242 * 1. Input object must be a dict
4243 * 2. The "execute" key must exist
4244 * 3. The "execute" key must be a string
4245 * 4. If the "arguments" key exists, it must be a dict
4246 * 5. If the "id" key exists, it can be anything (ie. json-value)
4247 * 6. Any argument not listed above is considered invalid
4248 */
4249static QDict *qmp_check_input_obj(QObject *input_obj)
4250{
4251 const QDictEntry *ent;
4252 int has_exec_key = 0;
4253 QDict *input_dict;
4254
4255 if (qobject_type(input_obj) != QTYPE_QDICT) {
4256 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4257 return NULL;
4258 }
4259
4260 input_dict = qobject_to_qdict(input_obj);
4261
4262 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4263 const char *arg_name = qdict_entry_key(ent);
4264 const QObject *arg_obj = qdict_entry_value(ent);
4265
4266 if (!strcmp(arg_name, "execute")) {
4267 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4268 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4269 "string");
4270 return NULL;
4271 }
4272 has_exec_key = 1;
4273 } else if (!strcmp(arg_name, "arguments")) {
4274 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4275 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4276 "object");
4277 return NULL;
4278 }
4279 } else if (!strcmp(arg_name, "id")) {
4280 /* FIXME: check duplicated IDs for async commands */
4281 } else {
4282 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4283 return NULL;
4284 }
4285 }
4286
4287 if (!has_exec_key) {
4288 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4289 return NULL;
4290 }
4291
4292 return input_dict;
4293}
4294
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004295static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4296{
4297 QObject *ret_data = NULL;
4298
4299 if (monitor_handler_is_async(cmd)) {
4300 qmp_async_info_handler(mon, cmd);
4301 if (monitor_has_error(mon)) {
4302 monitor_protocol_emitter(mon, NULL);
4303 }
4304 } else {
4305 cmd->mhandler.info_new(mon, &ret_data);
4306 if (ret_data) {
4307 monitor_protocol_emitter(mon, ret_data);
4308 qobject_decref(ret_data);
4309 }
4310 }
4311}
4312
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004313static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4314{
4315 int err;
4316 QObject *obj;
4317 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004318 const mon_cmd_t *cmd;
4319 Monitor *mon = cur_mon;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004320 const char *cmd_name, *query_cmd;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004321
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004322 query_cmd = NULL;
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004323 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004324
4325 obj = json_parser_parse(tokens, NULL);
4326 if (!obj) {
4327 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004328 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004329 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004330 }
4331
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004332 input = qmp_check_input_obj(obj);
4333 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004334 qobject_decref(obj);
4335 goto err_out;
4336 }
4337
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004338 mon->mc->id = qdict_get(input, "id");
4339 qobject_incref(mon->mc->id);
4340
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004341 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004342 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004343 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004344 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004345 }
4346
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004347 /*
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004348 * XXX: We need this special case until QMP has its own dispatch table
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004349 */
4350 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004351 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004352 goto err_out;
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004353 } else if (strstart(cmd_name, "query-", &query_cmd)) {
4354 cmd = qmp_find_query_cmd(query_cmd);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004355 } else {
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03004356 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulino0fb88582010-09-15 10:56:17 -03004357 }
4358
4359 if (!cmd || !monitor_handler_ported(cmd) || monitor_cmd_user_only(cmd)) {
4360 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4361 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004362 }
4363
4364 obj = qdict_get(input, "arguments");
4365 if (!obj) {
4366 args = qdict_new();
4367 } else {
4368 args = qobject_to_qdict(obj);
4369 QINCREF(args);
4370 }
4371
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004372 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004373 if (err < 0) {
4374 goto err_out;
4375 }
4376
Luiz Capitulino030db6e2010-09-10 16:03:38 -03004377 if (query_cmd) {
4378 qmp_call_query_cmd(mon, cmd);
4379 } else if (monitor_handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004380 err = qmp_async_cmd_handler(mon, cmd, args);
4381 if (err) {
4382 /* emit the error response */
4383 goto err_out;
4384 }
Adam Litke940cc302010-01-25 12:18:44 -06004385 } else {
4386 monitor_call_handler(mon, cmd, args);
4387 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004388
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004389 goto out;
4390
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004391err_out:
4392 monitor_protocol_emitter(mon, NULL);
4393out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004394 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004395 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004396}
4397
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004398/**
4399 * monitor_control_read(): Read and handle QMP input
4400 */
4401static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4402{
4403 Monitor *old_mon = cur_mon;
4404
4405 cur_mon = opaque;
4406
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004407 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004408
4409 cur_mon = old_mon;
4410}
4411
aliguori731b0362009-03-05 23:01:42 +00004412static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004413{
aliguori731b0362009-03-05 23:01:42 +00004414 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004415 int i;
aliguori376253e2009-03-05 23:01:23 +00004416
aliguori731b0362009-03-05 23:01:42 +00004417 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004418
aliguoricde76ee2009-03-05 23:01:51 +00004419 if (cur_mon->rs) {
4420 for (i = 0; i < size; i++)
4421 readline_handle_byte(cur_mon->rs, buf[i]);
4422 } else {
4423 if (size == 0 || buf[size - 1] != 0)
4424 monitor_printf(cur_mon, "corrupted command\n");
4425 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004426 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004427 }
aliguori731b0362009-03-05 23:01:42 +00004428
4429 cur_mon = old_mon;
4430}
aliguorid8f44602008-10-06 13:52:44 +00004431
aliguori376253e2009-03-05 23:01:23 +00004432static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004433{
aliguori731b0362009-03-05 23:01:42 +00004434 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004435 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004436 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004437}
4438
aliguoricde76ee2009-03-05 23:01:51 +00004439int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004440{
aliguoricde76ee2009-03-05 23:01:51 +00004441 if (!mon->rs)
4442 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004443 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004444 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004445}
4446
aliguori376253e2009-03-05 23:01:23 +00004447void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004448{
aliguoricde76ee2009-03-05 23:01:51 +00004449 if (!mon->rs)
4450 return;
aliguori731b0362009-03-05 23:01:42 +00004451 if (--mon->suspend_cnt == 0)
4452 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004453}
4454
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004455static QObject *get_qmp_greeting(void)
4456{
4457 QObject *ver;
4458
4459 do_info_version(NULL, &ver);
4460 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4461}
4462
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004463/**
4464 * monitor_control_event(): Print QMP gretting
4465 */
4466static void monitor_control_event(void *opaque, int event)
4467{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004468 QObject *data;
4469 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004470
Luiz Capitulino47116d12010-02-08 17:01:30 -02004471 switch (event) {
4472 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004473 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004474 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004475 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004476 monitor_json_emitter(mon, data);
4477 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004478 break;
4479 case CHR_EVENT_CLOSED:
4480 json_message_parser_destroy(&mon->mc->parser);
4481 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004482 }
4483}
4484
aliguori731b0362009-03-05 23:01:42 +00004485static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004486{
aliguori376253e2009-03-05 23:01:23 +00004487 Monitor *mon = opaque;
4488
aliguori2724b182009-03-05 23:01:47 +00004489 switch (event) {
4490 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004491 mon->mux_out = 0;
4492 if (mon->reset_seen) {
4493 readline_restart(mon->rs);
4494 monitor_resume(mon);
4495 monitor_flush(mon);
4496 } else {
4497 mon->suspend_cnt = 0;
4498 }
aliguori2724b182009-03-05 23:01:47 +00004499 break;
ths86e94de2007-01-05 22:01:59 +00004500
aliguori2724b182009-03-05 23:01:47 +00004501 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004502 if (mon->reset_seen) {
4503 if (mon->suspend_cnt == 0) {
4504 monitor_printf(mon, "\n");
4505 }
4506 monitor_flush(mon);
4507 monitor_suspend(mon);
4508 } else {
4509 mon->suspend_cnt++;
4510 }
4511 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004512 break;
4513
Amit Shahb6b8df52009-10-07 18:31:16 +05304514 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004515 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4516 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004517 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004518 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004519 }
4520 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004521 break;
4522 }
ths86e94de2007-01-05 22:01:59 +00004523}
4524
aliguori76655d62009-03-06 20:27:37 +00004525
4526/*
4527 * Local variables:
4528 * c-indent-level: 4
4529 * c-basic-offset: 4
4530 * tab-width: 8
4531 * End:
4532 */
4533
aliguori731b0362009-03-05 23:01:42 +00004534void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004535{
aliguori731b0362009-03-05 23:01:42 +00004536 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004537 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004538
4539 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004540 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004541 is_first_init = 0;
4542 }
aliguori87127162009-03-05 23:01:29 +00004543
4544 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004545
aliguori87127162009-03-05 23:01:29 +00004546 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004547 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004548 if (flags & MONITOR_USE_READLINE) {
4549 mon->rs = readline_init(mon, monitor_find_completion);
4550 monitor_read_command(mon, 0);
4551 }
aliguori87127162009-03-05 23:01:29 +00004552
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004553 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004554 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004555 /* Control mode requires special handlers */
4556 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4557 monitor_control_event, mon);
4558 } else {
4559 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4560 monitor_event, mon);
4561 }
aliguori87127162009-03-05 23:01:29 +00004562
Blue Swirl72cf2d42009-09-12 07:36:22 +00004563 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004564 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4565 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004566}
4567
aliguori376253e2009-03-05 23:01:23 +00004568static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004569{
aliguoribb5fc202009-03-05 23:01:15 +00004570 BlockDriverState *bs = opaque;
4571 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004572
aliguoribb5fc202009-03-05 23:01:15 +00004573 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004574 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004575 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004576 }
aliguori731b0362009-03-05 23:01:42 +00004577 if (mon->password_completion_cb)
4578 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004579
aliguori731b0362009-03-05 23:01:42 +00004580 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004581}
aliguoric0f4ce72009-03-05 23:01:01 +00004582
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004583int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4584 BlockDriverCompletionFunc *completion_cb,
4585 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004586{
aliguoricde76ee2009-03-05 23:01:51 +00004587 int err;
4588
aliguoribb5fc202009-03-05 23:01:15 +00004589 if (!bdrv_key_required(bs)) {
4590 if (completion_cb)
4591 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004592 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004593 }
aliguoric0f4ce72009-03-05 23:01:01 +00004594
Luiz Capitulino94171e12009-12-07 21:37:00 +01004595 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004596 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004597 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004598 }
4599
aliguori376253e2009-03-05 23:01:23 +00004600 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4601 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004602
aliguori731b0362009-03-05 23:01:42 +00004603 mon->password_completion_cb = completion_cb;
4604 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004605
aliguoricde76ee2009-03-05 23:01:51 +00004606 err = monitor_read_password(mon, bdrv_password_cb, bs);
4607
4608 if (err && completion_cb)
4609 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004610
4611 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004612}