blob: e602480ebb39bb15aae749d09fd5d9089074ef79 [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
354 json = qobject_to_json(data);
355 assert(json != NULL);
356
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200357 qstring_append_chr(json, '\n');
358 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200359
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200360 QDECREF(json);
361}
362
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200363static void monitor_protocol_emitter(Monitor *mon, QObject *data)
364{
365 QDict *qmp;
366
367 qmp = qdict_new();
368
369 if (!monitor_has_error(mon)) {
370 /* success response */
371 if (data) {
372 qobject_incref(data);
373 qdict_put_obj(qmp, "return", data);
374 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200375 /* return an empty QDict by default */
376 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200377 }
378 } else {
379 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100380 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200381 qdict_put(qmp, "error", mon->error->error);
382 QINCREF(mon->error->error);
383 QDECREF(mon->error);
384 mon->error = NULL;
385 }
386
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200387 if (mon->mc->id) {
388 qdict_put_obj(qmp, "id", mon->mc->id);
389 mon->mc->id = NULL;
390 }
391
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200392 monitor_json_emitter(mon, QOBJECT(qmp));
393 QDECREF(qmp);
394}
395
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200396static void timestamp_put(QDict *qdict)
397{
398 int err;
399 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000400 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200401
Blue Swirld08d6f02009-12-04 18:06:39 +0000402 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200403 if (err < 0)
404 return;
405
406 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
407 "'microseconds': %" PRId64 " }",
408 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200409 qdict_put_obj(qdict, "timestamp", obj);
410}
411
412/**
413 * monitor_protocol_event(): Generate a Monitor event
414 *
415 * Event-specific data can be emitted through the (optional) 'data' parameter.
416 */
417void monitor_protocol_event(MonitorEvent event, QObject *data)
418{
419 QDict *qmp;
420 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600421 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200422
Blue Swirl242cd002009-12-04 18:05:45 +0000423 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200424
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000426 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200427 event_name = "SHUTDOWN";
428 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000429 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200430 event_name = "RESET";
431 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000432 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200433 event_name = "POWERDOWN";
434 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000435 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200436 event_name = "STOP";
437 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300438 case QEVENT_RESUME:
439 event_name = "RESUME";
440 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200441 case QEVENT_VNC_CONNECTED:
442 event_name = "VNC_CONNECTED";
443 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200444 case QEVENT_VNC_INITIALIZED:
445 event_name = "VNC_INITIALIZED";
446 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200447 case QEVENT_VNC_DISCONNECTED:
448 event_name = "VNC_DISCONNECTED";
449 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200450 case QEVENT_BLOCK_IO_ERROR:
451 event_name = "BLOCK_IO_ERROR";
452 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300453 case QEVENT_RTC_CHANGE:
454 event_name = "RTC_CHANGE";
455 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300456 case QEVENT_WATCHDOG:
457 event_name = "WATCHDOG";
458 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200459 default:
460 abort();
461 break;
462 }
463
464 qmp = qdict_new();
465 timestamp_put(qmp);
466 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200467 if (data) {
468 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200469 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200470 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200471
Adam Litkef039a562010-01-15 08:34:02 -0600472 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200473 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200474 monitor_json_emitter(mon, QOBJECT(qmp));
475 }
Adam Litkef039a562010-01-15 08:34:02 -0600476 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200477 QDECREF(qmp);
478}
479
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200480static int do_qmp_capabilities(Monitor *mon, const QDict *params,
481 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200482{
483 /* Will setup QMP capabilities in the future */
484 if (monitor_ctrl_mode(mon)) {
485 mon->mc->command_mode = 1;
486 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200487
488 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200489}
490
bellard9dc39cb2004-03-14 21:38:27 +0000491static int compare_cmd(const char *name, const char *list)
492{
493 const char *p, *pstart;
494 int len;
495 len = strlen(name);
496 p = list;
497 for(;;) {
498 pstart = p;
499 p = strchr(p, '|');
500 if (!p)
501 p = pstart + strlen(pstart);
502 if ((p - pstart) == len && !memcmp(pstart, name, len))
503 return 1;
504 if (*p == '\0')
505 break;
506 p++;
507 }
508 return 0;
509}
510
Anthony Liguoric227f092009-10-01 16:12:16 -0500511static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000512 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000513{
Anthony Liguoric227f092009-10-01 16:12:16 -0500514 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000515
516 for(cmd = cmds; cmd->name != NULL; cmd++) {
517 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000518 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
519 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000520 }
521}
522
aliguori376253e2009-03-05 23:01:23 +0000523static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000524{
525 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000526 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000527 } else {
aliguori376253e2009-03-05 23:01:23 +0000528 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000529 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000530 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000531 monitor_printf(mon, "Log items (comma separated):\n");
532 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000533 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000534 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000535 }
536 }
bellard9dc39cb2004-03-14 21:38:27 +0000537 }
538}
539
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300540static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300541{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300542 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300543}
544
Prerna Saxena22890ab2010-06-24 17:04:53 +0530545#ifdef CONFIG_SIMPLE_TRACE
546static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
547{
548 const char *tp_name = qdict_get_str(qdict, "name");
549 bool new_state = qdict_get_bool(qdict, "option");
550 st_change_trace_event_state(tp_name, new_state);
551}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100552
553static void do_trace_file(Monitor *mon, const QDict *qdict)
554{
555 const char *op = qdict_get_try_str(qdict, "op");
556 const char *arg = qdict_get_try_str(qdict, "arg");
557
558 if (!op) {
559 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
560 } else if (!strcmp(op, "on")) {
561 st_set_trace_file_enabled(true);
562 } else if (!strcmp(op, "off")) {
563 st_set_trace_file_enabled(false);
564 } else if (!strcmp(op, "flush")) {
565 st_flush_trace_buffer();
566 } else if (!strcmp(op, "set")) {
567 if (arg) {
568 st_set_trace_file(arg);
569 }
570 } else {
571 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
572 help_cmd(mon, "trace-file");
573 }
574}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530575#endif
576
Adam Litke940cc302010-01-25 12:18:44 -0600577static void user_monitor_complete(void *opaque, QObject *ret_data)
578{
579 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
580
581 if (ret_data) {
582 data->user_print(data->mon, ret_data);
583 }
584 monitor_resume(data->mon);
585 qemu_free(data);
586}
587
588static void qmp_monitor_complete(void *opaque, QObject *ret_data)
589{
590 monitor_protocol_emitter(opaque, ret_data);
591}
592
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300593static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
594 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600595{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300596 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600597}
598
599static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
600{
601 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
602}
603
604static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
605 const QDict *params)
606{
607 int ret;
608
609 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
610 cb_data->mon = mon;
611 cb_data->user_print = cmd->user_print;
612 monitor_suspend(mon);
613 ret = cmd->mhandler.cmd_async(mon, params,
614 user_monitor_complete, cb_data);
615 if (ret < 0) {
616 monitor_resume(mon);
617 qemu_free(cb_data);
618 }
619}
620
621static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
622{
623 int ret;
624
625 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
626 cb_data->mon = mon;
627 cb_data->user_print = cmd->user_print;
628 monitor_suspend(mon);
629 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
630 if (ret < 0) {
631 monitor_resume(mon);
632 qemu_free(cb_data);
633 }
634}
635
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200636static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000637{
Anthony Liguoric227f092009-10-01 16:12:16 -0500638 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300639 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000640
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200641 if (!item) {
642 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000643 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200644 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300645
646 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000647 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300648 break;
bellard9dc39cb2004-03-14 21:38:27 +0000649 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300650
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200651 if (cmd->name == NULL) {
652 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100653 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200654 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200655 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300656 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200657 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300658
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200659 if (monitor_ctrl_mode(mon) && monitor_cmd_user_only(cmd)) {
660 qerror_report(QERR_COMMAND_NOT_FOUND, item);
661 return -1;
662 }
663
Adam Litke940cc302010-01-25 12:18:44 -0600664 if (monitor_handler_is_async(cmd)) {
665 if (monitor_ctrl_mode(mon)) {
666 qmp_async_info_handler(mon, cmd);
667 } else {
668 user_async_info_handler(mon, cmd);
669 }
670 /*
671 * Indicate that this command is asynchronous and will not return any
672 * data (not even empty). Instead, the data will be returned via a
673 * completion callback.
674 */
675 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
676 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300677 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200678
679 if (!monitor_ctrl_mode(mon)) {
680 /*
681 * User Protocol function is called here, Monitor Protocol is
682 * handled by monitor_call_handler()
683 */
684 if (*ret_data)
685 cmd->user_print(mon, *ret_data);
686 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300687 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200688 if (monitor_ctrl_mode(mon)) {
689 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100690 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200691 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200692 } else {
693 cmd->mhandler.info(mon);
694 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300695 }
696
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200697 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300698
699help:
700 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200701 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000702}
703
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200704static void do_info_version_print(Monitor *mon, const QObject *data)
705{
706 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300707 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200708
709 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300710 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200711
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300712 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
713 qdict_get_int(qemu, "major"),
714 qdict_get_int(qemu, "minor"),
715 qdict_get_int(qemu, "micro"),
716 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200717}
718
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300719static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000720{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300721 const char *version = QEMU_VERSION;
722 int major = 0, minor = 0, micro = 0;
723 char *tmp;
724
725 major = strtol(version, &tmp, 10);
726 tmp++;
727 minor = strtol(tmp, &tmp, 10);
728 tmp++;
729 micro = strtol(tmp, &tmp, 10);
730
731 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
732 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000733}
734
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200735static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000736{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200737 QDict *qdict;
738
739 qdict = qobject_to_qdict(data);
740 if (qdict_size(qdict) == 0) {
741 return;
742 }
743
744 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
745}
746
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200747static void do_info_name(Monitor *mon, QObject **ret_data)
748{
749 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
750 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000751}
752
Luiz Capitulino1a728672009-12-10 17:15:56 -0200753static QObject *get_cmd_dict(const char *name)
754{
755 const char *p;
756
757 /* Remove '|' from some commands */
758 p = strchr(name, '|');
759 if (p) {
760 p++;
761 } else {
762 p = name;
763 }
764
765 return qobject_from_jsonf("{ 'name': %s }", p);
766}
767
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200768static void do_info_commands(Monitor *mon, QObject **ret_data)
769{
770 QList *cmd_list;
771 const mon_cmd_t *cmd;
772
773 cmd_list = qlist_new();
774
775 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200776 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd) &&
777 !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200778 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200779 }
780 }
781
782 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200783 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd)) {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200784 char buf[128];
785 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200786 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200787 }
788 }
789
790 *ret_data = QOBJECT(cmd_list);
791}
792
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200793static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000794{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200795 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
796}
797
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200798static void do_info_uuid(Monitor *mon, QObject **ret_data)
799{
800 char uuid[64];
801
802 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000803 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
804 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
805 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
806 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200807 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000808}
809
bellard6a00d602005-11-21 23:25:50 +0000810/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000811static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000812{
813 CPUState *env;
814
815 for(env = first_cpu; env != NULL; env = env->next_cpu) {
816 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000817 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000818 return 0;
819 }
820 }
821 return -1;
822}
823
pbrook9596ebb2007-11-18 01:44:38 +0000824static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000825{
aliguori731b0362009-03-05 23:01:42 +0000826 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000827 mon_set_cpu(0);
828 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300829 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000830 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000831}
832
aliguori376253e2009-03-05 23:01:23 +0000833static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000834{
bellard6a00d602005-11-21 23:25:50 +0000835 CPUState *env;
836 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000837#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000838 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000839 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000840#else
aliguori376253e2009-03-05 23:01:23 +0000841 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000842 0);
bellard9307c4c2004-04-04 12:57:25 +0000843#endif
844}
845
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300846static void print_cpu_iter(QObject *obj, void *opaque)
847{
848 QDict *cpu;
849 int active = ' ';
850 Monitor *mon = opaque;
851
852 assert(qobject_type(obj) == QTYPE_QDICT);
853 cpu = qobject_to_qdict(obj);
854
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200855 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300856 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200857 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300858
859 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
860
861#if defined(TARGET_I386)
862 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
863 (target_ulong) qdict_get_int(cpu, "pc"));
864#elif defined(TARGET_PPC)
865 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
866 (target_long) qdict_get_int(cpu, "nip"));
867#elif defined(TARGET_SPARC)
868 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
869 (target_long) qdict_get_int(cpu, "pc"));
870 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
871 (target_long) qdict_get_int(cpu, "npc"));
872#elif defined(TARGET_MIPS)
873 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
874 (target_long) qdict_get_int(cpu, "PC"));
875#endif
876
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300878 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200879 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300880
881 monitor_printf(mon, "\n");
882}
883
884static void monitor_print_cpus(Monitor *mon, const QObject *data)
885{
886 QList *cpu_list;
887
888 assert(qobject_type(data) == QTYPE_QLIST);
889 cpu_list = qobject_to_qlist(data);
890 qlist_iter(cpu_list, print_cpu_iter, mon);
891}
892
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300893static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000894{
895 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300896 QList *cpu_list;
897
898 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000899
900 /* just to set the default cpu if not already done */
901 mon_get_cpu();
902
903 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200904 QDict *cpu;
905 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300906
Avi Kivity4c0960c2009-08-17 23:19:53 +0300907 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300908
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200909 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
910 env->cpu_index, env == mon->mon_cpu,
911 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200912
913 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300914
bellard6a00d602005-11-21 23:25:50 +0000915#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300916 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000917#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300918 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000919#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300920 qdict_put(cpu, "pc", qint_from_int(env->pc));
921 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000922#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300923 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000924#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300925
926 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000927 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300928
929 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000930}
931
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200932static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000933{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300934 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200935 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100936 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
937 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200938 return -1;
939 }
940 return 0;
bellard6a00d602005-11-21 23:25:50 +0000941}
942
aliguori376253e2009-03-05 23:01:23 +0000943static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000944{
aliguori376253e2009-03-05 23:01:23 +0000945 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000946}
947
aliguori376253e2009-03-05 23:01:23 +0000948static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000949{
950 int i;
bellard7e2515e2004-08-01 21:52:19 +0000951 const char *str;
ths3b46e622007-09-17 08:09:54 +0000952
aliguoricde76ee2009-03-05 23:01:51 +0000953 if (!mon->rs)
954 return;
bellard7e2515e2004-08-01 21:52:19 +0000955 i = 0;
956 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000957 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000958 if (!str)
959 break;
aliguori376253e2009-03-05 23:01:23 +0000960 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000961 i++;
bellardaa455482004-04-04 13:07:25 +0000962 }
963}
964
j_mayer76a66252007-03-07 08:32:30 +0000965#if defined(TARGET_PPC)
966/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000967static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000968{
969 CPUState *env;
970
971 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000972 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000973}
974#endif
975
Prerna Saxena22890ab2010-06-24 17:04:53 +0530976#if defined(CONFIG_SIMPLE_TRACE)
977static void do_info_trace(Monitor *mon)
978{
979 st_print_trace((FILE *)mon, &monitor_fprintf);
980}
981
982static void do_info_trace_events(Monitor *mon)
983{
984 st_print_trace_events((FILE *)mon, &monitor_fprintf);
985}
986#endif
987
Luiz Capitulinob223f352009-10-07 13:41:56 -0300988/**
989 * do_quit(): Quit QEMU execution
990 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200991static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000992{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300993 monitor_suspend(mon);
994 no_shutdown = 0;
995 qemu_system_shutdown_request();
996
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200997 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000998}
999
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001000static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001001{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001002 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001003 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001004 return -1;
1005 }
aliguoribb5fc202009-03-05 23:01:15 +00001006
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001007 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001008}
1009
1010static void change_vnc_password_cb(Monitor *mon, const char *password,
1011 void *opaque)
1012{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001013 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001014 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001015}
1016
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001017static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001018{
ths70848512007-08-25 01:37:05 +00001019 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001020 strcmp(target, "password") == 0) {
1021 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001022 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001023 strncpy(password, arg, sizeof(password));
1024 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001025 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001026 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001027 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001028 }
ths70848512007-08-25 01:37:05 +00001029 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001030 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001031 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001032 return -1;
1033 }
ths70848512007-08-25 01:37:05 +00001034 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001035
1036 return 0;
thse25a5822007-08-25 01:36:20 +00001037}
1038
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001039/**
1040 * do_change(): Change a removable medium, or VNC configuration
1041 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001042static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001043{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001044 const char *device = qdict_get_str(qdict, "device");
1045 const char *target = qdict_get_str(qdict, "target");
1046 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001047 int ret;
1048
thse25a5822007-08-25 01:36:20 +00001049 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001050 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001051 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001052 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001053 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001054
1055 return ret;
thse25a5822007-08-25 01:36:20 +00001056}
1057
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001058static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001059{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001060 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001061 return 0;
bellard59a983b2004-03-17 23:17:16 +00001062}
1063
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001064static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001065{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001066 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001067}
1068
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001069static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001070{
1071 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001072 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001073
bellard9307c4c2004-04-04 12:57:25 +00001074 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001075 mask = 0;
1076 } else {
bellard9307c4c2004-04-04 12:57:25 +00001077 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001078 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001079 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001080 return;
1081 }
1082 }
1083 cpu_set_log(mask);
1084}
1085
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001086static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001087{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001088 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001089 if (!option || !strcmp(option, "on")) {
1090 singlestep = 1;
1091 } else if (!strcmp(option, "off")) {
1092 singlestep = 0;
1093 } else {
1094 monitor_printf(mon, "unexpected option %s\n", option);
1095 }
1096}
1097
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001098/**
1099 * do_stop(): Stop VM execution
1100 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001101static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001102{
1103 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001104 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001105}
1106
aliguoribb5fc202009-03-05 23:01:15 +00001107static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001108
aliguori376253e2009-03-05 23:01:23 +00001109struct bdrv_iterate_context {
1110 Monitor *mon;
1111 int err;
1112};
aliguoric0f4ce72009-03-05 23:01:01 +00001113
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001114/**
1115 * do_cont(): Resume emulation.
1116 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001117static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001118{
1119 struct bdrv_iterate_context context = { mon, 0 };
1120
Amit Shah8e848652010-07-27 15:49:19 +05301121 if (incoming_expected) {
1122 qerror_report(QERR_MIGRATION_EXPECTED);
1123 return -1;
1124 }
aliguori376253e2009-03-05 23:01:23 +00001125 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001126 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001127 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001128 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001129 return 0;
1130 } else {
1131 return -1;
1132 }
bellard8a7ddc32004-03-31 19:00:16 +00001133}
1134
aliguoribb5fc202009-03-05 23:01:15 +00001135static void bdrv_key_cb(void *opaque, int err)
1136{
aliguori376253e2009-03-05 23:01:23 +00001137 Monitor *mon = opaque;
1138
aliguoribb5fc202009-03-05 23:01:15 +00001139 /* another key was set successfully, retry to continue */
1140 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001141 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001142}
1143
1144static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1145{
aliguori376253e2009-03-05 23:01:23 +00001146 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001147
aliguori376253e2009-03-05 23:01:23 +00001148 if (!context->err && bdrv_key_required(bs)) {
1149 context->err = -EBUSY;
1150 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1151 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001152 }
1153}
1154
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001155static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001156{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001157 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001158 if (!device)
1159 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1160 if (gdbserver_start(device) < 0) {
1161 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1162 device);
1163 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001164 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001165 } else {
aliguori59030a82009-04-05 18:43:41 +00001166 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1167 device);
bellard8a7ddc32004-03-31 19:00:16 +00001168 }
1169}
1170
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001171static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001172{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001173 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001174 if (select_watchdog_action(action) == -1) {
1175 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1176 }
1177}
1178
aliguori376253e2009-03-05 23:01:23 +00001179static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001180{
aliguori376253e2009-03-05 23:01:23 +00001181 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001182 switch(c) {
1183 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001184 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001185 break;
1186 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001187 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001188 break;
1189 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001190 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001191 break;
1192 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001194 break;
1195 default:
1196 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001197 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001198 } else {
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001200 }
1201 break;
1202 }
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001204}
1205
aliguori376253e2009-03-05 23:01:23 +00001206static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001207 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001208{
bellard6a00d602005-11-21 23:25:50 +00001209 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001210 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001211 uint8_t buf[16];
1212 uint64_t v;
1213
1214 if (format == 'i') {
1215 int flags;
1216 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001217 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001218#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001219 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001220 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001221 } else if (wsize == 4) {
1222 flags = 0;
1223 } else {
bellard6a15fd12006-04-12 21:07:07 +00001224 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001225 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001226 if (env) {
1227#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001228 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001229 (env->segs[R_CS].flags & DESC_L_MASK))
1230 flags = 2;
1231 else
1232#endif
1233 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1234 flags = 1;
1235 }
bellard4c27ba22004-04-25 18:05:08 +00001236 }
1237#endif
aliguori376253e2009-03-05 23:01:23 +00001238 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001239 return;
1240 }
1241
1242 len = wsize * count;
1243 if (wsize == 1)
1244 line_size = 8;
1245 else
1246 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001247 max_digits = 0;
1248
1249 switch(format) {
1250 case 'o':
1251 max_digits = (wsize * 8 + 2) / 3;
1252 break;
1253 default:
1254 case 'x':
1255 max_digits = (wsize * 8) / 4;
1256 break;
1257 case 'u':
1258 case 'd':
1259 max_digits = (wsize * 8 * 10 + 32) / 33;
1260 break;
1261 case 'c':
1262 wsize = 1;
1263 break;
1264 }
1265
1266 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001267 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001269 else
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001271 l = len;
1272 if (l > line_size)
1273 l = line_size;
1274 if (is_physical) {
1275 cpu_physical_memory_rw(addr, buf, l, 0);
1276 } else {
bellard6a00d602005-11-21 23:25:50 +00001277 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001278 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001280 break;
1281 }
bellard9307c4c2004-04-04 12:57:25 +00001282 }
ths5fafdf22007-09-16 21:08:06 +00001283 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001284 while (i < l) {
1285 switch(wsize) {
1286 default:
1287 case 1:
1288 v = ldub_raw(buf + i);
1289 break;
1290 case 2:
1291 v = lduw_raw(buf + i);
1292 break;
1293 case 4:
bellard92a31b12005-02-10 22:00:52 +00001294 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001295 break;
1296 case 8:
1297 v = ldq_raw(buf + i);
1298 break;
1299 }
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001301 switch(format) {
1302 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001303 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001304 break;
1305 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001307 break;
1308 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001309 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001310 break;
1311 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001312 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001313 break;
1314 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001315 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001316 break;
1317 }
1318 i += wsize;
1319 }
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001321 addr += l;
1322 len -= l;
1323 }
1324}
1325
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001326static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001327{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001328 int count = qdict_get_int(qdict, "count");
1329 int format = qdict_get_int(qdict, "format");
1330 int size = qdict_get_int(qdict, "size");
1331 target_long addr = qdict_get_int(qdict, "addr");
1332
aliguori376253e2009-03-05 23:01:23 +00001333 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001334}
1335
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001336static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001337{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001338 int count = qdict_get_int(qdict, "count");
1339 int format = qdict_get_int(qdict, "format");
1340 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001341 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001342
aliguori376253e2009-03-05 23:01:23 +00001343 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001344}
1345
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001346static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001347{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001348 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001349 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001350
blueswir17743e582007-09-24 18:39:04 +00001351#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001352 switch(format) {
1353 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001354 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001355 break;
1356 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001357 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001358 break;
1359 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001360 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001361 break;
1362 default:
1363 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001365 break;
1366 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001368 break;
1369 }
bellard92a31b12005-02-10 22:00:52 +00001370#else
1371 switch(format) {
1372 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001373 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001374 break;
1375 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001376 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001377 break;
1378 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001379 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001380 break;
1381 default:
1382 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001383 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001384 break;
1385 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001387 break;
1388 }
1389#endif
aliguori376253e2009-03-05 23:01:23 +00001390 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001391}
1392
Luiz Capitulino98696222010-02-10 23:49:58 -02001393static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001394{
1395 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001396 uint32_t size = qdict_get_int(qdict, "size");
1397 const char *filename = qdict_get_str(qdict, "filename");
1398 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001399 uint32_t l;
1400 CPUState *env;
1401 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001402 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001403
1404 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001405
1406 f = fopen(filename, "wb");
1407 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001408 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001409 return -1;
bellardb371dc52007-01-03 15:20:39 +00001410 }
1411 while (size != 0) {
1412 l = sizeof(buf);
1413 if (l > size)
1414 l = size;
1415 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001416 if (fwrite(buf, 1, l, f) != l) {
1417 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1418 goto exit;
1419 }
bellardb371dc52007-01-03 15:20:39 +00001420 addr += l;
1421 size -= l;
1422 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001423
1424 ret = 0;
1425
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001426exit:
bellardb371dc52007-01-03 15:20:39 +00001427 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001428 return ret;
bellardb371dc52007-01-03 15:20:39 +00001429}
1430
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001431static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001432 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001433{
1434 FILE *f;
1435 uint32_t l;
1436 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001437 uint32_t size = qdict_get_int(qdict, "size");
1438 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001439 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001440 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001441
1442 f = fopen(filename, "wb");
1443 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001444 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001445 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001446 }
1447 while (size != 0) {
1448 l = sizeof(buf);
1449 if (l > size)
1450 l = size;
1451 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001452 if (fwrite(buf, 1, l, f) != l) {
1453 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1454 goto exit;
1455 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001456 fflush(f);
1457 addr += l;
1458 size -= l;
1459 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001460
1461 ret = 0;
1462
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001463exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001464 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001465 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001466}
1467
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001468static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001469{
1470 uint32_t addr;
1471 uint8_t buf[1];
1472 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001473 uint32_t start = qdict_get_int(qdict, "start");
1474 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001475
1476 sum = 0;
1477 for(addr = start; addr < (start + size); addr++) {
1478 cpu_physical_memory_rw(addr, buf, 1, 0);
1479 /* BSD sum algorithm ('sum' Unix command) */
1480 sum = (sum >> 1) | (sum << 15);
1481 sum += buf[0];
1482 }
aliguori376253e2009-03-05 23:01:23 +00001483 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001484}
1485
bellarda3a91a32004-06-04 11:06:21 +00001486typedef struct {
1487 int keycode;
1488 const char *name;
1489} KeyDef;
1490
1491static const KeyDef key_defs[] = {
1492 { 0x2a, "shift" },
1493 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001494
bellarda3a91a32004-06-04 11:06:21 +00001495 { 0x38, "alt" },
1496 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001497 { 0x64, "altgr" },
1498 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001499 { 0x1d, "ctrl" },
1500 { 0x9d, "ctrl_r" },
1501
1502 { 0xdd, "menu" },
1503
1504 { 0x01, "esc" },
1505
1506 { 0x02, "1" },
1507 { 0x03, "2" },
1508 { 0x04, "3" },
1509 { 0x05, "4" },
1510 { 0x06, "5" },
1511 { 0x07, "6" },
1512 { 0x08, "7" },
1513 { 0x09, "8" },
1514 { 0x0a, "9" },
1515 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001516 { 0x0c, "minus" },
1517 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001518 { 0x0e, "backspace" },
1519
1520 { 0x0f, "tab" },
1521 { 0x10, "q" },
1522 { 0x11, "w" },
1523 { 0x12, "e" },
1524 { 0x13, "r" },
1525 { 0x14, "t" },
1526 { 0x15, "y" },
1527 { 0x16, "u" },
1528 { 0x17, "i" },
1529 { 0x18, "o" },
1530 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001531 { 0x1a, "bracket_left" },
1532 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001533 { 0x1c, "ret" },
1534
1535 { 0x1e, "a" },
1536 { 0x1f, "s" },
1537 { 0x20, "d" },
1538 { 0x21, "f" },
1539 { 0x22, "g" },
1540 { 0x23, "h" },
1541 { 0x24, "j" },
1542 { 0x25, "k" },
1543 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001544 { 0x27, "semicolon" },
1545 { 0x28, "apostrophe" },
1546 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001547
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001548 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001549 { 0x2c, "z" },
1550 { 0x2d, "x" },
1551 { 0x2e, "c" },
1552 { 0x2f, "v" },
1553 { 0x30, "b" },
1554 { 0x31, "n" },
1555 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001556 { 0x33, "comma" },
1557 { 0x34, "dot" },
1558 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001559
balrog4d3b6f62008-02-10 16:33:14 +00001560 { 0x37, "asterisk" },
1561
bellarda3a91a32004-06-04 11:06:21 +00001562 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001563 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001564 { 0x3b, "f1" },
1565 { 0x3c, "f2" },
1566 { 0x3d, "f3" },
1567 { 0x3e, "f4" },
1568 { 0x3f, "f5" },
1569 { 0x40, "f6" },
1570 { 0x41, "f7" },
1571 { 0x42, "f8" },
1572 { 0x43, "f9" },
1573 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001574 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001575 { 0x46, "scroll_lock" },
1576
bellard64866c32006-05-07 18:03:31 +00001577 { 0xb5, "kp_divide" },
1578 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001579 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001580 { 0x4e, "kp_add" },
1581 { 0x9c, "kp_enter" },
1582 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001583 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001584
1585 { 0x52, "kp_0" },
1586 { 0x4f, "kp_1" },
1587 { 0x50, "kp_2" },
1588 { 0x51, "kp_3" },
1589 { 0x4b, "kp_4" },
1590 { 0x4c, "kp_5" },
1591 { 0x4d, "kp_6" },
1592 { 0x47, "kp_7" },
1593 { 0x48, "kp_8" },
1594 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001595
bellarda3a91a32004-06-04 11:06:21 +00001596 { 0x56, "<" },
1597
1598 { 0x57, "f11" },
1599 { 0x58, "f12" },
1600
1601 { 0xb7, "print" },
1602
1603 { 0xc7, "home" },
1604 { 0xc9, "pgup" },
1605 { 0xd1, "pgdn" },
1606 { 0xcf, "end" },
1607
1608 { 0xcb, "left" },
1609 { 0xc8, "up" },
1610 { 0xd0, "down" },
1611 { 0xcd, "right" },
1612
1613 { 0xd2, "insert" },
1614 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001615#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1616 { 0xf0, "stop" },
1617 { 0xf1, "again" },
1618 { 0xf2, "props" },
1619 { 0xf3, "undo" },
1620 { 0xf4, "front" },
1621 { 0xf5, "copy" },
1622 { 0xf6, "open" },
1623 { 0xf7, "paste" },
1624 { 0xf8, "find" },
1625 { 0xf9, "cut" },
1626 { 0xfa, "lf" },
1627 { 0xfb, "help" },
1628 { 0xfc, "meta_l" },
1629 { 0xfd, "meta_r" },
1630 { 0xfe, "compose" },
1631#endif
bellarda3a91a32004-06-04 11:06:21 +00001632 { 0, NULL },
1633};
1634
1635static int get_keycode(const char *key)
1636{
1637 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001638 char *endp;
1639 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001640
1641 for(p = key_defs; p->name != NULL; p++) {
1642 if (!strcmp(key, p->name))
1643 return p->keycode;
1644 }
bellard64866c32006-05-07 18:03:31 +00001645 if (strstart(key, "0x", NULL)) {
1646 ret = strtoul(key, &endp, 0);
1647 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1648 return ret;
1649 }
bellarda3a91a32004-06-04 11:06:21 +00001650 return -1;
1651}
1652
balrogc8256f92008-06-08 22:45:01 +00001653#define MAX_KEYCODES 16
1654static uint8_t keycodes[MAX_KEYCODES];
1655static int nb_pending_keycodes;
1656static QEMUTimer *key_timer;
1657
1658static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001659{
balrogc8256f92008-06-08 22:45:01 +00001660 int keycode;
1661
1662 while (nb_pending_keycodes > 0) {
1663 nb_pending_keycodes--;
1664 keycode = keycodes[nb_pending_keycodes];
1665 if (keycode & 0x80)
1666 kbd_put_keycode(0xe0);
1667 kbd_put_keycode(keycode | 0x80);
1668 }
1669}
1670
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001671static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001672{
balrog3401c0d2008-06-04 10:05:59 +00001673 char keyname_buf[16];
1674 char *separator;
1675 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001676 const char *string = qdict_get_str(qdict, "string");
1677 int has_hold_time = qdict_haskey(qdict, "hold_time");
1678 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001679
balrogc8256f92008-06-08 22:45:01 +00001680 if (nb_pending_keycodes > 0) {
1681 qemu_del_timer(key_timer);
1682 release_keys(NULL);
1683 }
1684 if (!has_hold_time)
1685 hold_time = 100;
1686 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001687 while (1) {
1688 separator = strchr(string, '-');
1689 keyname_len = separator ? separator - string : strlen(string);
1690 if (keyname_len > 0) {
1691 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1692 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001693 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001694 return;
bellarda3a91a32004-06-04 11:06:21 +00001695 }
balrogc8256f92008-06-08 22:45:01 +00001696 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001697 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001698 return;
1699 }
1700 keyname_buf[keyname_len] = 0;
1701 keycode = get_keycode(keyname_buf);
1702 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001703 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001704 return;
1705 }
balrogc8256f92008-06-08 22:45:01 +00001706 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001707 }
balrog3401c0d2008-06-04 10:05:59 +00001708 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001709 break;
balrog3401c0d2008-06-04 10:05:59 +00001710 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001711 }
balrogc8256f92008-06-08 22:45:01 +00001712 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001713 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001714 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001715 keycode = keycodes[i];
1716 if (keycode & 0x80)
1717 kbd_put_keycode(0xe0);
1718 kbd_put_keycode(keycode & 0x7f);
1719 }
balrogc8256f92008-06-08 22:45:01 +00001720 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001721 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001722 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001723}
1724
bellard13224a82006-07-14 22:03:35 +00001725static int mouse_button_state;
1726
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001727static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001728{
1729 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001730 const char *dx_str = qdict_get_str(qdict, "dx_str");
1731 const char *dy_str = qdict_get_str(qdict, "dy_str");
1732 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001733 dx = strtol(dx_str, NULL, 0);
1734 dy = strtol(dy_str, NULL, 0);
1735 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001736 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001737 dz = strtol(dz_str, NULL, 0);
1738 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1739}
1740
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001741static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001742{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001743 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001744 mouse_button_state = button_state;
1745 kbd_mouse_event(0, 0, 0, mouse_button_state);
1746}
1747
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001748static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001749{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001750 int size = qdict_get_int(qdict, "size");
1751 int addr = qdict_get_int(qdict, "addr");
1752 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001753 uint32_t val;
1754 int suffix;
1755
1756 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001757 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001758 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001759 addr++;
1760 }
1761 addr &= 0xffff;
1762
1763 switch(size) {
1764 default:
1765 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001766 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001767 suffix = 'b';
1768 break;
1769 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001770 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001771 suffix = 'w';
1772 break;
1773 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001774 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001775 suffix = 'l';
1776 break;
1777 }
aliguori376253e2009-03-05 23:01:23 +00001778 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1779 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001780}
bellarda3a91a32004-06-04 11:06:21 +00001781
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001782static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001783{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001784 int size = qdict_get_int(qdict, "size");
1785 int addr = qdict_get_int(qdict, "addr");
1786 int val = qdict_get_int(qdict, "val");
1787
Jan Kiszkaf1147842009-07-14 10:20:11 +02001788 addr &= IOPORTS_MASK;
1789
1790 switch (size) {
1791 default:
1792 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001793 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001794 break;
1795 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001796 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001797 break;
1798 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001799 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001800 break;
1801 }
1802}
1803
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001804static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001805{
1806 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001807 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001808
Jan Kiszka76e30d02009-07-02 00:19:02 +02001809 res = qemu_boot_set(bootdevice);
1810 if (res == 0) {
1811 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1812 } else if (res > 0) {
1813 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001814 } else {
aliguori376253e2009-03-05 23:01:23 +00001815 monitor_printf(mon, "no function defined to set boot device list for "
1816 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001817 }
1818}
1819
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001820/**
1821 * do_system_reset(): Issue a machine reset
1822 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001823static int do_system_reset(Monitor *mon, const QDict *qdict,
1824 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001825{
1826 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001827 return 0;
bellarde4f90822004-06-20 12:35:44 +00001828}
1829
Luiz Capitulino43076662009-10-07 13:41:59 -03001830/**
1831 * do_system_powerdown(): Issue a machine powerdown
1832 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001833static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1834 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001835{
1836 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001837 return 0;
bellard34751872005-07-02 14:31:34 +00001838}
1839
bellardb86bda52004-09-18 19:32:46 +00001840#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001841static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001842{
aliguori376253e2009-03-05 23:01:23 +00001843 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1844 addr,
1845 pte & mask,
1846 pte & PG_GLOBAL_MASK ? 'G' : '-',
1847 pte & PG_PSE_MASK ? 'P' : '-',
1848 pte & PG_DIRTY_MASK ? 'D' : '-',
1849 pte & PG_ACCESSED_MASK ? 'A' : '-',
1850 pte & PG_PCD_MASK ? 'C' : '-',
1851 pte & PG_PWT_MASK ? 'T' : '-',
1852 pte & PG_USER_MASK ? 'U' : '-',
1853 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001854}
1855
aliguori376253e2009-03-05 23:01:23 +00001856static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001857{
bellard6a00d602005-11-21 23:25:50 +00001858 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001859 int l1, l2;
1860 uint32_t pgd, pde, pte;
1861
bellard6a00d602005-11-21 23:25:50 +00001862 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001863
bellardb86bda52004-09-18 19:32:46 +00001864 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001865 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001866 return;
1867 }
1868 pgd = env->cr[3] & ~0xfff;
1869 for(l1 = 0; l1 < 1024; l1++) {
1870 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1871 pde = le32_to_cpu(pde);
1872 if (pde & PG_PRESENT_MASK) {
1873 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001874 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001875 } else {
1876 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001877 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001878 (uint8_t *)&pte, 4);
1879 pte = le32_to_cpu(pte);
1880 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001881 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001882 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001883 ~0xfff);
1884 }
1885 }
1886 }
1887 }
1888 }
1889}
1890
aliguori376253e2009-03-05 23:01:23 +00001891static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001892 uint32_t end, int prot)
1893{
bellard9746b152004-11-11 18:30:24 +00001894 int prot1;
1895 prot1 = *plast_prot;
1896 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001897 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001898 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1899 *pstart, end, end - *pstart,
1900 prot1 & PG_USER_MASK ? 'u' : '-',
1901 'r',
1902 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001903 }
1904 if (prot != 0)
1905 *pstart = end;
1906 else
1907 *pstart = -1;
1908 *plast_prot = prot;
1909 }
1910}
1911
aliguori376253e2009-03-05 23:01:23 +00001912static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001913{
bellard6a00d602005-11-21 23:25:50 +00001914 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001915 int l1, l2, prot, last_prot;
1916 uint32_t pgd, pde, pte, start, end;
1917
bellard6a00d602005-11-21 23:25:50 +00001918 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001919
bellardb86bda52004-09-18 19:32:46 +00001920 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001921 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001922 return;
1923 }
1924 pgd = env->cr[3] & ~0xfff;
1925 last_prot = 0;
1926 start = -1;
1927 for(l1 = 0; l1 < 1024; l1++) {
1928 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1929 pde = le32_to_cpu(pde);
1930 end = l1 << 22;
1931 if (pde & PG_PRESENT_MASK) {
1932 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1933 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001934 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001935 } else {
1936 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001937 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001938 (uint8_t *)&pte, 4);
1939 pte = le32_to_cpu(pte);
1940 end = (l1 << 22) + (l2 << 12);
1941 if (pte & PG_PRESENT_MASK) {
1942 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1943 } else {
1944 prot = 0;
1945 }
aliguori376253e2009-03-05 23:01:23 +00001946 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001947 }
1948 }
1949 } else {
1950 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001951 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001952 }
1953 }
1954}
1955#endif
1956
aurel327c664e22009-03-03 06:12:22 +00001957#if defined(TARGET_SH4)
1958
aliguori376253e2009-03-05 23:01:23 +00001959static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001960{
aliguori376253e2009-03-05 23:01:23 +00001961 monitor_printf(mon, " tlb%i:\t"
1962 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1963 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1964 "dirty=%hhu writethrough=%hhu\n",
1965 idx,
1966 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1967 tlb->v, tlb->sh, tlb->c, tlb->pr,
1968 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001969}
1970
aliguori376253e2009-03-05 23:01:23 +00001971static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001972{
1973 CPUState *env = mon_get_cpu();
1974 int i;
1975
aliguori376253e2009-03-05 23:01:23 +00001976 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001977 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001978 print_tlb (mon, i, &env->itlb[i]);
1979 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001980 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001981 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001982}
1983
1984#endif
1985
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001986static void do_info_kvm_print(Monitor *mon, const QObject *data)
1987{
1988 QDict *qdict;
1989
1990 qdict = qobject_to_qdict(data);
1991
1992 monitor_printf(mon, "kvm support: ");
1993 if (qdict_get_bool(qdict, "present")) {
1994 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1995 "enabled" : "disabled");
1996 } else {
1997 monitor_printf(mon, "not compiled\n");
1998 }
1999}
2000
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002001static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002002{
2003#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002004 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2005 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002006#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002007 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002008#endif
2009}
2010
aliguori030ea372009-04-21 22:30:47 +00002011static void do_info_numa(Monitor *mon)
2012{
aliguorib28b6232009-04-22 20:20:29 +00002013 int i;
aliguori030ea372009-04-21 22:30:47 +00002014 CPUState *env;
2015
2016 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2017 for (i = 0; i < nb_numa_nodes; i++) {
2018 monitor_printf(mon, "node %d cpus:", i);
2019 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2020 if (env->numa_node == i) {
2021 monitor_printf(mon, " %d", env->cpu_index);
2022 }
2023 }
2024 monitor_printf(mon, "\n");
2025 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2026 node_mem[i] >> 20);
2027 }
2028}
2029
bellard5f1ce942006-02-08 22:40:15 +00002030#ifdef CONFIG_PROFILER
2031
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002032int64_t qemu_time;
2033int64_t dev_time;
2034
aliguori376253e2009-03-05 23:01:23 +00002035static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002036{
2037 int64_t total;
2038 total = qemu_time;
2039 if (total == 0)
2040 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002041 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002042 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002043 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002044 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002045 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002046 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002047}
2048#else
aliguori376253e2009-03-05 23:01:23 +00002049static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002050{
aliguori376253e2009-03-05 23:01:23 +00002051 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002052}
2053#endif
2054
bellardec36b692006-07-16 18:57:03 +00002055/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002056static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002057
aliguori376253e2009-03-05 23:01:23 +00002058static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002059{
2060 int i;
2061 CaptureState *s;
2062
2063 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002064 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002065 s->ops.info (s->opaque);
2066 }
2067}
2068
Blue Swirl23130862009-06-06 08:22:04 +00002069#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002070static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002071{
2072 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002073 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002074 CaptureState *s;
2075
2076 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2077 if (i == n) {
2078 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002079 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002080 qemu_free (s);
2081 return;
2082 }
2083 }
2084}
2085
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002086static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002087{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002088 const char *path = qdict_get_str(qdict, "path");
2089 int has_freq = qdict_haskey(qdict, "freq");
2090 int freq = qdict_get_try_int(qdict, "freq", -1);
2091 int has_bits = qdict_haskey(qdict, "bits");
2092 int bits = qdict_get_try_int(qdict, "bits", -1);
2093 int has_channels = qdict_haskey(qdict, "nchannels");
2094 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002095 CaptureState *s;
2096
2097 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002098
2099 freq = has_freq ? freq : 44100;
2100 bits = has_bits ? bits : 16;
2101 nchannels = has_channels ? nchannels : 2;
2102
2103 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002104 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002105 qemu_free (s);
2106 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002107 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002108}
2109#endif
2110
aurel32dc1c0b72008-04-27 23:52:12 +00002111#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002112static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002113{
2114 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002115 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002116
2117 for (env = first_cpu; env != NULL; env = env->next_cpu)
2118 if (env->cpu_index == cpu_index) {
2119 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2120 break;
2121 }
2122}
2123#endif
2124
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002125static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002126{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002127 QDict *qdict;
2128
2129 qdict = qobject_to_qdict(data);
2130
2131 monitor_printf(mon, "VM status: ");
2132 if (qdict_get_bool(qdict, "running")) {
2133 monitor_printf(mon, "running");
2134 if (qdict_get_bool(qdict, "singlestep")) {
2135 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002136 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002137 } else {
2138 monitor_printf(mon, "paused");
2139 }
2140
2141 monitor_printf(mon, "\n");
2142}
2143
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002144static void do_info_status(Monitor *mon, QObject **ret_data)
2145{
2146 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2147 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002148}
2149
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002150static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002151{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002153
aliguori76655d62009-03-06 20:27:37 +00002154 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002155 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002156 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002157 return acl;
2158}
aliguori76655d62009-03-06 20:27:37 +00002159
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002160static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002161{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002162 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002163 qemu_acl *acl = find_acl(mon, aclname);
2164 qemu_acl_entry *entry;
2165 int i = 0;
2166
2167 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002168 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002169 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002170 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002171 i++;
2172 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002173 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002174 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002175 }
2176}
2177
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002178static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002179{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002180 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002181 qemu_acl *acl = find_acl(mon, aclname);
2182
2183 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002184 qemu_acl_reset(acl);
2185 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002186 }
2187}
aliguori76655d62009-03-06 20:27:37 +00002188
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002189static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002190{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002191 const char *aclname = qdict_get_str(qdict, "aclname");
2192 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002193 qemu_acl *acl = find_acl(mon, aclname);
2194
2195 if (acl) {
2196 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002197 acl->defaultDeny = 0;
2198 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002199 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002200 acl->defaultDeny = 1;
2201 monitor_printf(mon, "acl: policy set to 'deny'\n");
2202 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002203 monitor_printf(mon, "acl: unknown policy '%s', "
2204 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002205 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002206 }
2207}
aliguori76655d62009-03-06 20:27:37 +00002208
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002209static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002210{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002211 const char *aclname = qdict_get_str(qdict, "aclname");
2212 const char *match = qdict_get_str(qdict, "match");
2213 const char *policy = qdict_get_str(qdict, "policy");
2214 int has_index = qdict_haskey(qdict, "index");
2215 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002216 qemu_acl *acl = find_acl(mon, aclname);
2217 int deny, ret;
2218
2219 if (acl) {
2220 if (strcmp(policy, "allow") == 0) {
2221 deny = 0;
2222 } else if (strcmp(policy, "deny") == 0) {
2223 deny = 1;
2224 } else {
2225 monitor_printf(mon, "acl: unknown policy '%s', "
2226 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002227 return;
2228 }
aliguori28a76be2009-03-06 20:27:40 +00002229 if (has_index)
2230 ret = qemu_acl_insert(acl, deny, match, index);
2231 else
2232 ret = qemu_acl_append(acl, deny, match);
2233 if (ret < 0)
2234 monitor_printf(mon, "acl: unable to add acl entry\n");
2235 else
2236 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002237 }
2238}
aliguori76655d62009-03-06 20:27:37 +00002239
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002240static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002241{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002242 const char *aclname = qdict_get_str(qdict, "aclname");
2243 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002244 qemu_acl *acl = find_acl(mon, aclname);
2245 int ret;
aliguori76655d62009-03-06 20:27:37 +00002246
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002247 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002248 ret = qemu_acl_remove(acl, match);
2249 if (ret < 0)
2250 monitor_printf(mon, "acl: no matching acl entry\n");
2251 else
2252 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002253 }
2254}
2255
Huang Ying79c4f6b2009-06-23 10:05:14 +08002256#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002257static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002258{
2259 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002260 int cpu_index = qdict_get_int(qdict, "cpu_index");
2261 int bank = qdict_get_int(qdict, "bank");
2262 uint64_t status = qdict_get_int(qdict, "status");
2263 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2264 uint64_t addr = qdict_get_int(qdict, "addr");
2265 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002266
2267 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2268 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2269 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2270 break;
2271 }
2272}
2273#endif
2274
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002275static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002276{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002277 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002278 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002279 int fd;
2280
2281 fd = qemu_chr_get_msgfd(mon->chr);
2282 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002283 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002284 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002285 }
2286
2287 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002288 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2289 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002290 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002291 }
2292
Blue Swirl72cf2d42009-09-12 07:36:22 +00002293 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002294 if (strcmp(monfd->name, fdname) != 0) {
2295 continue;
2296 }
2297
2298 close(monfd->fd);
2299 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002300 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002301 }
2302
Anthony Liguoric227f092009-10-01 16:12:16 -05002303 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002304 monfd->name = qemu_strdup(fdname);
2305 monfd->fd = fd;
2306
Blue Swirl72cf2d42009-09-12 07:36:22 +00002307 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002308 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002309}
2310
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002311static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002312{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002313 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002314 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002315
Blue Swirl72cf2d42009-09-12 07:36:22 +00002316 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002317 if (strcmp(monfd->name, fdname) != 0) {
2318 continue;
2319 }
2320
Blue Swirl72cf2d42009-09-12 07:36:22 +00002321 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002322 close(monfd->fd);
2323 qemu_free(monfd->name);
2324 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002325 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002326 }
2327
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002328 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002329 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002330}
2331
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002332static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002333{
2334 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002335 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002336
2337 vm_stop(0);
2338
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002339 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002340 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002341 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002342}
2343
Mark McLoughlin7768e042009-07-22 09:11:41 +01002344int monitor_get_fd(Monitor *mon, const char *fdname)
2345{
Anthony Liguoric227f092009-10-01 16:12:16 -05002346 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002347
Blue Swirl72cf2d42009-09-12 07:36:22 +00002348 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002349 int fd;
2350
2351 if (strcmp(monfd->name, fdname) != 0) {
2352 continue;
2353 }
2354
2355 fd = monfd->fd;
2356
2357 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002358 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002359 qemu_free(monfd->name);
2360 qemu_free(monfd);
2361
2362 return fd;
2363 }
2364
2365 return -1;
2366}
2367
Anthony Liguoric227f092009-10-01 16:12:16 -05002368static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002369#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002370 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002371};
2372
Blue Swirl23130862009-06-06 08:22:04 +00002373/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002374static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002375 {
2376 .name = "version",
2377 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002378 .params = "",
2379 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002380 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002381 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002382 },
2383 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002384 .name = "commands",
2385 .args_type = "",
2386 .params = "",
2387 .help = "list QMP available commands",
2388 .user_print = monitor_user_noop,
2389 .mhandler.info_new = do_info_commands,
2390 },
2391 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002392 .name = "network",
2393 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002394 .params = "",
2395 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002396 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002397 },
2398 {
2399 .name = "chardev",
2400 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002401 .params = "",
2402 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002403 .user_print = qemu_chr_info_print,
2404 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002405 },
2406 {
2407 .name = "block",
2408 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002409 .params = "",
2410 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002411 .user_print = bdrv_info_print,
2412 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002413 },
2414 {
2415 .name = "blockstats",
2416 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002417 .params = "",
2418 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002419 .user_print = bdrv_stats_print,
2420 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002421 },
2422 {
2423 .name = "registers",
2424 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002425 .params = "",
2426 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002427 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002428 },
2429 {
2430 .name = "cpus",
2431 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002432 .params = "",
2433 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002434 .user_print = monitor_print_cpus,
2435 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002436 },
2437 {
2438 .name = "history",
2439 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002440 .params = "",
2441 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002442 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002443 },
2444 {
2445 .name = "irq",
2446 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002447 .params = "",
2448 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002449 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002450 },
2451 {
2452 .name = "pic",
2453 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002454 .params = "",
2455 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002456 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002457 },
2458 {
2459 .name = "pci",
2460 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002461 .params = "",
2462 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002463 .user_print = do_pci_info_print,
2464 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002465 },
aurel327c664e22009-03-03 06:12:22 +00002466#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 {
2468 .name = "tlb",
2469 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002470 .params = "",
2471 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002472 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002473 },
aurel327c664e22009-03-03 06:12:22 +00002474#endif
2475#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002476 {
2477 .name = "mem",
2478 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002479 .params = "",
2480 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002481 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002482 },
bellardb86bda52004-09-18 19:32:46 +00002483#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002484 {
2485 .name = "jit",
2486 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002487 .params = "",
2488 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002489 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002490 },
2491 {
2492 .name = "kvm",
2493 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002494 .params = "",
2495 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002496 .user_print = do_info_kvm_print,
2497 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002498 },
2499 {
2500 .name = "numa",
2501 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002502 .params = "",
2503 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002504 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002505 },
2506 {
2507 .name = "usb",
2508 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002509 .params = "",
2510 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002511 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 },
2513 {
2514 .name = "usbhost",
2515 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002516 .params = "",
2517 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002518 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002519 },
2520 {
2521 .name = "profile",
2522 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002523 .params = "",
2524 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002525 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 },
2527 {
2528 .name = "capture",
2529 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 .params = "",
2531 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002532 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 },
2534 {
2535 .name = "snapshots",
2536 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002537 .params = "",
2538 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002539 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
2541 {
2542 .name = "status",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002546 .user_print = do_info_status_print,
2547 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002548 },
2549 {
2550 .name = "pcmcia",
2551 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002552 .params = "",
2553 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002554 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002555 },
2556 {
2557 .name = "mice",
2558 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002559 .params = "",
2560 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002561 .user_print = do_info_mice_print,
2562 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002563 },
2564 {
2565 .name = "vnc",
2566 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002567 .params = "",
2568 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002569 .user_print = do_info_vnc_print,
2570 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 },
2572 {
2573 .name = "name",
2574 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 .params = "",
2576 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002577 .user_print = do_info_name_print,
2578 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 },
2580 {
2581 .name = "uuid",
2582 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 .params = "",
2584 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002585 .user_print = do_info_uuid_print,
2586 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002587 },
j_mayer76a66252007-03-07 08:32:30 +00002588#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 {
2590 .name = "cpustats",
2591 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002592 .params = "",
2593 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002594 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002595 },
j_mayer76a66252007-03-07 08:32:30 +00002596#endif
blueswir131a60e22007-10-26 18:42:59 +00002597#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 {
2599 .name = "usernet",
2600 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002601 .params = "",
2602 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002603 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002604 },
blueswir131a60e22007-10-26 18:42:59 +00002605#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 {
2607 .name = "migrate",
2608 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 .params = "",
2610 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002611 .user_print = do_info_migrate_print,
2612 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
2614 {
2615 .name = "balloon",
2616 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 .params = "",
2618 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002619 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002620 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002621 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 },
2623 {
2624 .name = "qtree",
2625 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002626 .params = "",
2627 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002628 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 },
2630 {
2631 .name = "qdm",
2632 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 .params = "",
2634 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002635 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 },
2637 {
2638 .name = "roms",
2639 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 .params = "",
2641 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002642 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002643 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302644#if defined(CONFIG_SIMPLE_TRACE)
2645 {
2646 .name = "trace",
2647 .args_type = "",
2648 .params = "",
2649 .help = "show current contents of trace buffer",
2650 .mhandler.info = do_info_trace,
2651 },
2652 {
2653 .name = "trace-events",
2654 .args_type = "",
2655 .params = "",
2656 .help = "show available trace-events & their state",
2657 .mhandler.info = do_info_trace_events,
2658 },
2659#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 {
2661 .name = NULL,
2662 },
bellard9dc39cb2004-03-14 21:38:27 +00002663};
2664
bellard9307c4c2004-04-04 12:57:25 +00002665/*******************************************************************/
2666
2667static const char *pch;
2668static jmp_buf expr_env;
2669
bellard92a31b12005-02-10 22:00:52 +00002670#define MD_TLONG 0
2671#define MD_I32 1
2672
bellard9307c4c2004-04-04 12:57:25 +00002673typedef struct MonitorDef {
2674 const char *name;
2675 int offset;
blueswir18662d652008-10-02 18:32:44 +00002676 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002677 int type;
bellard9307c4c2004-04-04 12:57:25 +00002678} MonitorDef;
2679
bellard57206fd2004-04-25 18:54:52 +00002680#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002681static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002682{
bellard6a00d602005-11-21 23:25:50 +00002683 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002684 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002685}
2686#endif
2687
bellarda541f292004-04-12 20:39:29 +00002688#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002689static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002690{
bellard6a00d602005-11-21 23:25:50 +00002691 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002692 unsigned int u;
2693 int i;
2694
2695 u = 0;
2696 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002697 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002698
2699 return u;
2700}
2701
blueswir18662d652008-10-02 18:32:44 +00002702static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002703{
bellard6a00d602005-11-21 23:25:50 +00002704 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002705 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002706}
2707
blueswir18662d652008-10-02 18:32:44 +00002708static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002709{
bellard6a00d602005-11-21 23:25:50 +00002710 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002711 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002712}
bellard9fddaa02004-05-21 12:59:32 +00002713
blueswir18662d652008-10-02 18:32:44 +00002714static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002715{
bellard6a00d602005-11-21 23:25:50 +00002716 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002717 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002718}
2719
blueswir18662d652008-10-02 18:32:44 +00002720static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002721{
bellard6a00d602005-11-21 23:25:50 +00002722 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002723 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002724}
2725
blueswir18662d652008-10-02 18:32:44 +00002726static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002727{
bellard6a00d602005-11-21 23:25:50 +00002728 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002729 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002730}
bellarda541f292004-04-12 20:39:29 +00002731#endif
2732
bellarde95c8d52004-09-30 22:22:08 +00002733#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002734#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002735static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002736{
bellard6a00d602005-11-21 23:25:50 +00002737 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002738
2739 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002740}
bellard7b936c02005-10-30 17:05:13 +00002741#endif
bellarde95c8d52004-09-30 22:22:08 +00002742
blueswir18662d652008-10-02 18:32:44 +00002743static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002744{
bellard6a00d602005-11-21 23:25:50 +00002745 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002746 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002747}
2748#endif
2749
blueswir18662d652008-10-02 18:32:44 +00002750static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002751#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002752
2753#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002754 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002755 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002756 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002757
bellard9307c4c2004-04-04 12:57:25 +00002758 { "eax", offsetof(CPUState, regs[0]) },
2759 { "ecx", offsetof(CPUState, regs[1]) },
2760 { "edx", offsetof(CPUState, regs[2]) },
2761 { "ebx", offsetof(CPUState, regs[3]) },
2762 { "esp|sp", offsetof(CPUState, regs[4]) },
2763 { "ebp|fp", offsetof(CPUState, regs[5]) },
2764 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002765 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002766#ifdef TARGET_X86_64
2767 { "r8", offsetof(CPUState, regs[8]) },
2768 { "r9", offsetof(CPUState, regs[9]) },
2769 { "r10", offsetof(CPUState, regs[10]) },
2770 { "r11", offsetof(CPUState, regs[11]) },
2771 { "r12", offsetof(CPUState, regs[12]) },
2772 { "r13", offsetof(CPUState, regs[13]) },
2773 { "r14", offsetof(CPUState, regs[14]) },
2774 { "r15", offsetof(CPUState, regs[15]) },
2775#endif
bellard9307c4c2004-04-04 12:57:25 +00002776 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002777 { "eip", offsetof(CPUState, eip) },
2778 SEG("cs", R_CS)
2779 SEG("ds", R_DS)
2780 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002781 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002782 SEG("fs", R_FS)
2783 SEG("gs", R_GS)
2784 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002785#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002786 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002787 { "r0", offsetof(CPUState, gpr[0]) },
2788 { "r1", offsetof(CPUState, gpr[1]) },
2789 { "r2", offsetof(CPUState, gpr[2]) },
2790 { "r3", offsetof(CPUState, gpr[3]) },
2791 { "r4", offsetof(CPUState, gpr[4]) },
2792 { "r5", offsetof(CPUState, gpr[5]) },
2793 { "r6", offsetof(CPUState, gpr[6]) },
2794 { "r7", offsetof(CPUState, gpr[7]) },
2795 { "r8", offsetof(CPUState, gpr[8]) },
2796 { "r9", offsetof(CPUState, gpr[9]) },
2797 { "r10", offsetof(CPUState, gpr[10]) },
2798 { "r11", offsetof(CPUState, gpr[11]) },
2799 { "r12", offsetof(CPUState, gpr[12]) },
2800 { "r13", offsetof(CPUState, gpr[13]) },
2801 { "r14", offsetof(CPUState, gpr[14]) },
2802 { "r15", offsetof(CPUState, gpr[15]) },
2803 { "r16", offsetof(CPUState, gpr[16]) },
2804 { "r17", offsetof(CPUState, gpr[17]) },
2805 { "r18", offsetof(CPUState, gpr[18]) },
2806 { "r19", offsetof(CPUState, gpr[19]) },
2807 { "r20", offsetof(CPUState, gpr[20]) },
2808 { "r21", offsetof(CPUState, gpr[21]) },
2809 { "r22", offsetof(CPUState, gpr[22]) },
2810 { "r23", offsetof(CPUState, gpr[23]) },
2811 { "r24", offsetof(CPUState, gpr[24]) },
2812 { "r25", offsetof(CPUState, gpr[25]) },
2813 { "r26", offsetof(CPUState, gpr[26]) },
2814 { "r27", offsetof(CPUState, gpr[27]) },
2815 { "r28", offsetof(CPUState, gpr[28]) },
2816 { "r29", offsetof(CPUState, gpr[29]) },
2817 { "r30", offsetof(CPUState, gpr[30]) },
2818 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002819 /* Floating point registers */
2820 { "f0", offsetof(CPUState, fpr[0]) },
2821 { "f1", offsetof(CPUState, fpr[1]) },
2822 { "f2", offsetof(CPUState, fpr[2]) },
2823 { "f3", offsetof(CPUState, fpr[3]) },
2824 { "f4", offsetof(CPUState, fpr[4]) },
2825 { "f5", offsetof(CPUState, fpr[5]) },
2826 { "f6", offsetof(CPUState, fpr[6]) },
2827 { "f7", offsetof(CPUState, fpr[7]) },
2828 { "f8", offsetof(CPUState, fpr[8]) },
2829 { "f9", offsetof(CPUState, fpr[9]) },
2830 { "f10", offsetof(CPUState, fpr[10]) },
2831 { "f11", offsetof(CPUState, fpr[11]) },
2832 { "f12", offsetof(CPUState, fpr[12]) },
2833 { "f13", offsetof(CPUState, fpr[13]) },
2834 { "f14", offsetof(CPUState, fpr[14]) },
2835 { "f15", offsetof(CPUState, fpr[15]) },
2836 { "f16", offsetof(CPUState, fpr[16]) },
2837 { "f17", offsetof(CPUState, fpr[17]) },
2838 { "f18", offsetof(CPUState, fpr[18]) },
2839 { "f19", offsetof(CPUState, fpr[19]) },
2840 { "f20", offsetof(CPUState, fpr[20]) },
2841 { "f21", offsetof(CPUState, fpr[21]) },
2842 { "f22", offsetof(CPUState, fpr[22]) },
2843 { "f23", offsetof(CPUState, fpr[23]) },
2844 { "f24", offsetof(CPUState, fpr[24]) },
2845 { "f25", offsetof(CPUState, fpr[25]) },
2846 { "f26", offsetof(CPUState, fpr[26]) },
2847 { "f27", offsetof(CPUState, fpr[27]) },
2848 { "f28", offsetof(CPUState, fpr[28]) },
2849 { "f29", offsetof(CPUState, fpr[29]) },
2850 { "f30", offsetof(CPUState, fpr[30]) },
2851 { "f31", offsetof(CPUState, fpr[31]) },
2852 { "fpscr", offsetof(CPUState, fpscr) },
2853 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002854 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002855 { "lr", offsetof(CPUState, lr) },
2856 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002857 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002858 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002859 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002860 { "msr", 0, &monitor_get_msr, },
2861 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002862 { "tbu", 0, &monitor_get_tbu, },
2863 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002864#if defined(TARGET_PPC64)
2865 /* Address space register */
2866 { "asr", offsetof(CPUState, asr) },
2867#endif
2868 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002869 { "sdr1", offsetof(CPUState, sdr1) },
2870 { "sr0", offsetof(CPUState, sr[0]) },
2871 { "sr1", offsetof(CPUState, sr[1]) },
2872 { "sr2", offsetof(CPUState, sr[2]) },
2873 { "sr3", offsetof(CPUState, sr[3]) },
2874 { "sr4", offsetof(CPUState, sr[4]) },
2875 { "sr5", offsetof(CPUState, sr[5]) },
2876 { "sr6", offsetof(CPUState, sr[6]) },
2877 { "sr7", offsetof(CPUState, sr[7]) },
2878 { "sr8", offsetof(CPUState, sr[8]) },
2879 { "sr9", offsetof(CPUState, sr[9]) },
2880 { "sr10", offsetof(CPUState, sr[10]) },
2881 { "sr11", offsetof(CPUState, sr[11]) },
2882 { "sr12", offsetof(CPUState, sr[12]) },
2883 { "sr13", offsetof(CPUState, sr[13]) },
2884 { "sr14", offsetof(CPUState, sr[14]) },
2885 { "sr15", offsetof(CPUState, sr[15]) },
2886 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002887#elif defined(TARGET_SPARC)
2888 { "g0", offsetof(CPUState, gregs[0]) },
2889 { "g1", offsetof(CPUState, gregs[1]) },
2890 { "g2", offsetof(CPUState, gregs[2]) },
2891 { "g3", offsetof(CPUState, gregs[3]) },
2892 { "g4", offsetof(CPUState, gregs[4]) },
2893 { "g5", offsetof(CPUState, gregs[5]) },
2894 { "g6", offsetof(CPUState, gregs[6]) },
2895 { "g7", offsetof(CPUState, gregs[7]) },
2896 { "o0", 0, monitor_get_reg },
2897 { "o1", 1, monitor_get_reg },
2898 { "o2", 2, monitor_get_reg },
2899 { "o3", 3, monitor_get_reg },
2900 { "o4", 4, monitor_get_reg },
2901 { "o5", 5, monitor_get_reg },
2902 { "o6", 6, monitor_get_reg },
2903 { "o7", 7, monitor_get_reg },
2904 { "l0", 8, monitor_get_reg },
2905 { "l1", 9, monitor_get_reg },
2906 { "l2", 10, monitor_get_reg },
2907 { "l3", 11, monitor_get_reg },
2908 { "l4", 12, monitor_get_reg },
2909 { "l5", 13, monitor_get_reg },
2910 { "l6", 14, monitor_get_reg },
2911 { "l7", 15, monitor_get_reg },
2912 { "i0", 16, monitor_get_reg },
2913 { "i1", 17, monitor_get_reg },
2914 { "i2", 18, monitor_get_reg },
2915 { "i3", 19, monitor_get_reg },
2916 { "i4", 20, monitor_get_reg },
2917 { "i5", 21, monitor_get_reg },
2918 { "i6", 22, monitor_get_reg },
2919 { "i7", 23, monitor_get_reg },
2920 { "pc", offsetof(CPUState, pc) },
2921 { "npc", offsetof(CPUState, npc) },
2922 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002923#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002924 { "psr", 0, &monitor_get_psr, },
2925 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002926#endif
bellarde95c8d52004-09-30 22:22:08 +00002927 { "tbr", offsetof(CPUState, tbr) },
2928 { "fsr", offsetof(CPUState, fsr) },
2929 { "f0", offsetof(CPUState, fpr[0]) },
2930 { "f1", offsetof(CPUState, fpr[1]) },
2931 { "f2", offsetof(CPUState, fpr[2]) },
2932 { "f3", offsetof(CPUState, fpr[3]) },
2933 { "f4", offsetof(CPUState, fpr[4]) },
2934 { "f5", offsetof(CPUState, fpr[5]) },
2935 { "f6", offsetof(CPUState, fpr[6]) },
2936 { "f7", offsetof(CPUState, fpr[7]) },
2937 { "f8", offsetof(CPUState, fpr[8]) },
2938 { "f9", offsetof(CPUState, fpr[9]) },
2939 { "f10", offsetof(CPUState, fpr[10]) },
2940 { "f11", offsetof(CPUState, fpr[11]) },
2941 { "f12", offsetof(CPUState, fpr[12]) },
2942 { "f13", offsetof(CPUState, fpr[13]) },
2943 { "f14", offsetof(CPUState, fpr[14]) },
2944 { "f15", offsetof(CPUState, fpr[15]) },
2945 { "f16", offsetof(CPUState, fpr[16]) },
2946 { "f17", offsetof(CPUState, fpr[17]) },
2947 { "f18", offsetof(CPUState, fpr[18]) },
2948 { "f19", offsetof(CPUState, fpr[19]) },
2949 { "f20", offsetof(CPUState, fpr[20]) },
2950 { "f21", offsetof(CPUState, fpr[21]) },
2951 { "f22", offsetof(CPUState, fpr[22]) },
2952 { "f23", offsetof(CPUState, fpr[23]) },
2953 { "f24", offsetof(CPUState, fpr[24]) },
2954 { "f25", offsetof(CPUState, fpr[25]) },
2955 { "f26", offsetof(CPUState, fpr[26]) },
2956 { "f27", offsetof(CPUState, fpr[27]) },
2957 { "f28", offsetof(CPUState, fpr[28]) },
2958 { "f29", offsetof(CPUState, fpr[29]) },
2959 { "f30", offsetof(CPUState, fpr[30]) },
2960 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002961#ifdef TARGET_SPARC64
2962 { "f32", offsetof(CPUState, fpr[32]) },
2963 { "f34", offsetof(CPUState, fpr[34]) },
2964 { "f36", offsetof(CPUState, fpr[36]) },
2965 { "f38", offsetof(CPUState, fpr[38]) },
2966 { "f40", offsetof(CPUState, fpr[40]) },
2967 { "f42", offsetof(CPUState, fpr[42]) },
2968 { "f44", offsetof(CPUState, fpr[44]) },
2969 { "f46", offsetof(CPUState, fpr[46]) },
2970 { "f48", offsetof(CPUState, fpr[48]) },
2971 { "f50", offsetof(CPUState, fpr[50]) },
2972 { "f52", offsetof(CPUState, fpr[52]) },
2973 { "f54", offsetof(CPUState, fpr[54]) },
2974 { "f56", offsetof(CPUState, fpr[56]) },
2975 { "f58", offsetof(CPUState, fpr[58]) },
2976 { "f60", offsetof(CPUState, fpr[60]) },
2977 { "f62", offsetof(CPUState, fpr[62]) },
2978 { "asi", offsetof(CPUState, asi) },
2979 { "pstate", offsetof(CPUState, pstate) },
2980 { "cansave", offsetof(CPUState, cansave) },
2981 { "canrestore", offsetof(CPUState, canrestore) },
2982 { "otherwin", offsetof(CPUState, otherwin) },
2983 { "wstate", offsetof(CPUState, wstate) },
2984 { "cleanwin", offsetof(CPUState, cleanwin) },
2985 { "fprs", offsetof(CPUState, fprs) },
2986#endif
bellard9307c4c2004-04-04 12:57:25 +00002987#endif
2988 { NULL },
2989};
2990
aliguori376253e2009-03-05 23:01:23 +00002991static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002992{
aliguori376253e2009-03-05 23:01:23 +00002993 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002994 longjmp(expr_env, 1);
2995}
2996
Markus Armbruster09b94182010-01-20 13:07:30 +01002997/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002998static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002999{
blueswir18662d652008-10-02 18:32:44 +00003000 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003001 void *ptr;
3002
bellard9307c4c2004-04-04 12:57:25 +00003003 for(md = monitor_defs; md->name != NULL; md++) {
3004 if (compare_cmd(name, md->name)) {
3005 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003006 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003007 } else {
bellard6a00d602005-11-21 23:25:50 +00003008 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003009 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003010 switch(md->type) {
3011 case MD_I32:
3012 *pval = *(int32_t *)ptr;
3013 break;
3014 case MD_TLONG:
3015 *pval = *(target_long *)ptr;
3016 break;
3017 default:
3018 *pval = 0;
3019 break;
3020 }
bellard9307c4c2004-04-04 12:57:25 +00003021 }
3022 return 0;
3023 }
3024 }
3025 return -1;
3026}
3027
3028static void next(void)
3029{
Blue Swirl660f11b2009-07-31 21:16:51 +00003030 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003031 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003032 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003033 pch++;
3034 }
3035}
3036
aliguori376253e2009-03-05 23:01:23 +00003037static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003038
aliguori376253e2009-03-05 23:01:23 +00003039static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003040{
blueswir1c2efc952007-09-25 17:28:42 +00003041 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003042 char *p;
bellard6a00d602005-11-21 23:25:50 +00003043 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003044
3045 switch(*pch) {
3046 case '+':
3047 next();
aliguori376253e2009-03-05 23:01:23 +00003048 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003049 break;
3050 case '-':
3051 next();
aliguori376253e2009-03-05 23:01:23 +00003052 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003053 break;
3054 case '~':
3055 next();
aliguori376253e2009-03-05 23:01:23 +00003056 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003057 break;
3058 case '(':
3059 next();
aliguori376253e2009-03-05 23:01:23 +00003060 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003061 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003062 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003063 }
3064 next();
3065 break;
bellard81d09122004-07-14 17:21:37 +00003066 case '\'':
3067 pch++;
3068 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003069 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003070 n = *pch;
3071 pch++;
3072 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003073 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003074 next();
3075 break;
bellard9307c4c2004-04-04 12:57:25 +00003076 case '$':
3077 {
3078 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003079 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003080
bellard9307c4c2004-04-04 12:57:25 +00003081 pch++;
3082 q = buf;
3083 while ((*pch >= 'a' && *pch <= 'z') ||
3084 (*pch >= 'A' && *pch <= 'Z') ||
3085 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003086 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003087 if ((q - buf) < sizeof(buf) - 1)
3088 *q++ = *pch;
3089 pch++;
3090 }
blueswir1cd390082008-11-16 13:53:32 +00003091 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003092 pch++;
3093 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003094 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003095 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003096 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003097 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003098 }
3099 break;
3100 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003101 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003102 n = 0;
3103 break;
3104 default:
blueswir17743e582007-09-24 18:39:04 +00003105#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003106 n = strtoull(pch, &p, 0);
3107#else
bellard9307c4c2004-04-04 12:57:25 +00003108 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003109#endif
bellard9307c4c2004-04-04 12:57:25 +00003110 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003111 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003112 }
3113 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003114 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003115 pch++;
3116 break;
3117 }
3118 return n;
3119}
3120
3121
aliguori376253e2009-03-05 23:01:23 +00003122static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003123{
blueswir1c2efc952007-09-25 17:28:42 +00003124 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003125 int op;
ths3b46e622007-09-17 08:09:54 +00003126
aliguori376253e2009-03-05 23:01:23 +00003127 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003128 for(;;) {
3129 op = *pch;
3130 if (op != '*' && op != '/' && op != '%')
3131 break;
3132 next();
aliguori376253e2009-03-05 23:01:23 +00003133 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003134 switch(op) {
3135 default:
3136 case '*':
3137 val *= val2;
3138 break;
3139 case '/':
3140 case '%':
ths5fafdf22007-09-16 21:08:06 +00003141 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003142 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003143 if (op == '/')
3144 val /= val2;
3145 else
3146 val %= val2;
3147 break;
3148 }
3149 }
3150 return val;
3151}
3152
aliguori376253e2009-03-05 23:01:23 +00003153static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003154{
blueswir1c2efc952007-09-25 17:28:42 +00003155 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003156 int op;
bellard9307c4c2004-04-04 12:57:25 +00003157
aliguori376253e2009-03-05 23:01:23 +00003158 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003159 for(;;) {
3160 op = *pch;
3161 if (op != '&' && op != '|' && op != '^')
3162 break;
3163 next();
aliguori376253e2009-03-05 23:01:23 +00003164 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003165 switch(op) {
3166 default:
3167 case '&':
3168 val &= val2;
3169 break;
3170 case '|':
3171 val |= val2;
3172 break;
3173 case '^':
3174 val ^= val2;
3175 break;
3176 }
3177 }
3178 return val;
3179}
3180
aliguori376253e2009-03-05 23:01:23 +00003181static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003182{
blueswir1c2efc952007-09-25 17:28:42 +00003183 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003184 int op;
bellard9307c4c2004-04-04 12:57:25 +00003185
aliguori376253e2009-03-05 23:01:23 +00003186 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003187 for(;;) {
3188 op = *pch;
3189 if (op != '+' && op != '-')
3190 break;
3191 next();
aliguori376253e2009-03-05 23:01:23 +00003192 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003193 if (op == '+')
3194 val += val2;
3195 else
3196 val -= val2;
3197 }
3198 return val;
3199}
3200
aliguori376253e2009-03-05 23:01:23 +00003201static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003202{
3203 pch = *pp;
3204 if (setjmp(expr_env)) {
3205 *pp = pch;
3206 return -1;
3207 }
blueswir1cd390082008-11-16 13:53:32 +00003208 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003209 pch++;
aliguori376253e2009-03-05 23:01:23 +00003210 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003211 *pp = pch;
3212 return 0;
3213}
3214
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003215static int get_double(Monitor *mon, double *pval, const char **pp)
3216{
3217 const char *p = *pp;
3218 char *tailp;
3219 double d;
3220
3221 d = strtod(p, &tailp);
3222 if (tailp == p) {
3223 monitor_printf(mon, "Number expected\n");
3224 return -1;
3225 }
3226 if (d != d || d - d != 0) {
3227 /* NaN or infinity */
3228 monitor_printf(mon, "Bad number\n");
3229 return -1;
3230 }
3231 *pval = d;
3232 *pp = tailp;
3233 return 0;
3234}
3235
bellard9307c4c2004-04-04 12:57:25 +00003236static int get_str(char *buf, int buf_size, const char **pp)
3237{
3238 const char *p;
3239 char *q;
3240 int c;
3241
bellard81d09122004-07-14 17:21:37 +00003242 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003243 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003244 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003245 p++;
3246 if (*p == '\0') {
3247 fail:
bellard81d09122004-07-14 17:21:37 +00003248 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003249 *pp = p;
3250 return -1;
3251 }
bellard9307c4c2004-04-04 12:57:25 +00003252 if (*p == '\"') {
3253 p++;
3254 while (*p != '\0' && *p != '\"') {
3255 if (*p == '\\') {
3256 p++;
3257 c = *p++;
3258 switch(c) {
3259 case 'n':
3260 c = '\n';
3261 break;
3262 case 'r':
3263 c = '\r';
3264 break;
3265 case '\\':
3266 case '\'':
3267 case '\"':
3268 break;
3269 default:
3270 qemu_printf("unsupported escape code: '\\%c'\n", c);
3271 goto fail;
3272 }
3273 if ((q - buf) < buf_size - 1) {
3274 *q++ = c;
3275 }
3276 } else {
3277 if ((q - buf) < buf_size - 1) {
3278 *q++ = *p;
3279 }
3280 p++;
3281 }
3282 }
3283 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003284 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003285 goto fail;
3286 }
3287 p++;
3288 } else {
blueswir1cd390082008-11-16 13:53:32 +00003289 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003290 if ((q - buf) < buf_size - 1) {
3291 *q++ = *p;
3292 }
3293 p++;
3294 }
bellard9307c4c2004-04-04 12:57:25 +00003295 }
bellard81d09122004-07-14 17:21:37 +00003296 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003297 *pp = p;
3298 return 0;
3299}
3300
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003301/*
3302 * Store the command-name in cmdname, and return a pointer to
3303 * the remaining of the command string.
3304 */
3305static const char *get_command_name(const char *cmdline,
3306 char *cmdname, size_t nlen)
3307{
3308 size_t len;
3309 const char *p, *pstart;
3310
3311 p = cmdline;
3312 while (qemu_isspace(*p))
3313 p++;
3314 if (*p == '\0')
3315 return NULL;
3316 pstart = p;
3317 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3318 p++;
3319 len = p - pstart;
3320 if (len > nlen - 1)
3321 len = nlen - 1;
3322 memcpy(cmdname, pstart, len);
3323 cmdname[len] = '\0';
3324 return p;
3325}
3326
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003327/**
3328 * Read key of 'type' into 'key' and return the current
3329 * 'type' pointer.
3330 */
3331static char *key_get_info(const char *type, char **key)
3332{
3333 size_t len;
3334 char *p, *str;
3335
3336 if (*type == ',')
3337 type++;
3338
3339 p = strchr(type, ':');
3340 if (!p) {
3341 *key = NULL;
3342 return NULL;
3343 }
3344 len = p - type;
3345
3346 str = qemu_malloc(len + 1);
3347 memcpy(str, type, len);
3348 str[len] = '\0';
3349
3350 *key = str;
3351 return ++p;
3352}
3353
bellard9307c4c2004-04-04 12:57:25 +00003354static int default_fmt_format = 'x';
3355static int default_fmt_size = 4;
3356
3357#define MAX_ARGS 16
3358
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003359static int is_valid_option(const char *c, const char *typestr)
3360{
3361 char option[3];
3362
3363 option[0] = '-';
3364 option[1] = *c;
3365 option[2] = '\0';
3366
3367 typestr = strstr(typestr, option);
3368 return (typestr != NULL);
3369}
3370
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003371static const mon_cmd_t *monitor_find_command(const char *cmdname)
3372{
3373 const mon_cmd_t *cmd;
3374
3375 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3376 if (compare_cmd(cmdname, cmd->name)) {
3377 return cmd;
3378 }
3379 }
3380
3381 return NULL;
3382}
3383
Anthony Liguoric227f092009-10-01 16:12:16 -05003384static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003385 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003386 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003387{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003388 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003389 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003390 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003391 char cmdname[256];
3392 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003393 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003394
3395#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003396 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003397#endif
ths3b46e622007-09-17 08:09:54 +00003398
bellard9307c4c2004-04-04 12:57:25 +00003399 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003400 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3401 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003402 return NULL;
ths3b46e622007-09-17 08:09:54 +00003403
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003404 cmd = monitor_find_command(cmdname);
3405 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003406 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003407 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003408 }
bellard9307c4c2004-04-04 12:57:25 +00003409
bellard9307c4c2004-04-04 12:57:25 +00003410 /* parse the parameters */
3411 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003412 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003413 typestr = key_get_info(typestr, &key);
3414 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003415 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003416 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003417 typestr++;
3418 switch(c) {
3419 case 'F':
bellard81d09122004-07-14 17:21:37 +00003420 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003421 case 's':
3422 {
3423 int ret;
ths3b46e622007-09-17 08:09:54 +00003424
blueswir1cd390082008-11-16 13:53:32 +00003425 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003426 p++;
3427 if (*typestr == '?') {
3428 typestr++;
3429 if (*p == '\0') {
3430 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003431 break;
bellard9307c4c2004-04-04 12:57:25 +00003432 }
3433 }
3434 ret = get_str(buf, sizeof(buf), &p);
3435 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003436 switch(c) {
3437 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003438 monitor_printf(mon, "%s: filename expected\n",
3439 cmdname);
bellard81d09122004-07-14 17:21:37 +00003440 break;
3441 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003442 monitor_printf(mon, "%s: block device name expected\n",
3443 cmdname);
bellard81d09122004-07-14 17:21:37 +00003444 break;
3445 default:
aliguori376253e2009-03-05 23:01:23 +00003446 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003447 break;
3448 }
bellard9307c4c2004-04-04 12:57:25 +00003449 goto fail;
3450 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003451 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003452 }
3453 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003454 case 'O':
3455 {
3456 QemuOptsList *opts_list;
3457 QemuOpts *opts;
3458
3459 opts_list = qemu_find_opts(key);
3460 if (!opts_list || opts_list->desc->name) {
3461 goto bad_type;
3462 }
3463 while (qemu_isspace(*p)) {
3464 p++;
3465 }
3466 if (!*p)
3467 break;
3468 if (get_str(buf, sizeof(buf), &p) < 0) {
3469 goto fail;
3470 }
3471 opts = qemu_opts_parse(opts_list, buf, 1);
3472 if (!opts) {
3473 goto fail;
3474 }
3475 qemu_opts_to_qdict(opts, qdict);
3476 qemu_opts_del(opts);
3477 }
3478 break;
bellard9307c4c2004-04-04 12:57:25 +00003479 case '/':
3480 {
3481 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003482
blueswir1cd390082008-11-16 13:53:32 +00003483 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003484 p++;
3485 if (*p == '/') {
3486 /* format found */
3487 p++;
3488 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003489 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003490 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003491 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003492 count = count * 10 + (*p - '0');
3493 p++;
3494 }
3495 }
3496 size = -1;
3497 format = -1;
3498 for(;;) {
3499 switch(*p) {
3500 case 'o':
3501 case 'd':
3502 case 'u':
3503 case 'x':
3504 case 'i':
3505 case 'c':
3506 format = *p++;
3507 break;
3508 case 'b':
3509 size = 1;
3510 p++;
3511 break;
3512 case 'h':
3513 size = 2;
3514 p++;
3515 break;
3516 case 'w':
3517 size = 4;
3518 p++;
3519 break;
3520 case 'g':
3521 case 'L':
3522 size = 8;
3523 p++;
3524 break;
3525 default:
3526 goto next;
3527 }
3528 }
3529 next:
blueswir1cd390082008-11-16 13:53:32 +00003530 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003531 monitor_printf(mon, "invalid char in format: '%c'\n",
3532 *p);
bellard9307c4c2004-04-04 12:57:25 +00003533 goto fail;
3534 }
bellard9307c4c2004-04-04 12:57:25 +00003535 if (format < 0)
3536 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003537 if (format != 'i') {
3538 /* for 'i', not specifying a size gives -1 as size */
3539 if (size < 0)
3540 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003541 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003542 }
bellard9307c4c2004-04-04 12:57:25 +00003543 default_fmt_format = format;
3544 } else {
3545 count = 1;
3546 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003547 if (format != 'i') {
3548 size = default_fmt_size;
3549 } else {
3550 size = -1;
3551 }
bellard9307c4c2004-04-04 12:57:25 +00003552 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003553 qdict_put(qdict, "count", qint_from_int(count));
3554 qdict_put(qdict, "format", qint_from_int(format));
3555 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003556 }
3557 break;
3558 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003559 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003560 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003561 {
blueswir1c2efc952007-09-25 17:28:42 +00003562 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003563
blueswir1cd390082008-11-16 13:53:32 +00003564 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003565 p++;
bellard34405572004-06-08 00:55:58 +00003566 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003567 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003568 if (*p == '\0') {
3569 typestr++;
3570 break;
3571 }
bellard34405572004-06-08 00:55:58 +00003572 } else {
3573 if (*p == '.') {
3574 p++;
blueswir1cd390082008-11-16 13:53:32 +00003575 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003576 p++;
bellard34405572004-06-08 00:55:58 +00003577 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003578 typestr++;
3579 break;
bellard34405572004-06-08 00:55:58 +00003580 }
3581 }
bellard13224a82006-07-14 22:03:35 +00003582 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003583 }
aliguori376253e2009-03-05 23:01:23 +00003584 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003585 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003586 /* Check if 'i' is greater than 32-bit */
3587 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3588 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3589 monitor_printf(mon, "integer is for 32-bit values\n");
3590 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003591 } else if (c == 'M') {
3592 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003593 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003594 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003595 }
3596 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003597 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003598 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003599 {
3600 double val;
3601
3602 while (qemu_isspace(*p))
3603 p++;
3604 if (*typestr == '?') {
3605 typestr++;
3606 if (*p == '\0') {
3607 break;
3608 }
3609 }
3610 if (get_double(mon, &val, &p) < 0) {
3611 goto fail;
3612 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003613 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003614 switch (*p) {
3615 case 'K': case 'k':
3616 val *= 1 << 10; p++; break;
3617 case 'M': case 'm':
3618 val *= 1 << 20; p++; break;
3619 case 'G': case 'g':
3620 val *= 1 << 30; p++; break;
3621 }
3622 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003623 if (c == 'T' && p[0] && p[1] == 's') {
3624 switch (*p) {
3625 case 'm':
3626 val /= 1e3; p += 2; break;
3627 case 'u':
3628 val /= 1e6; p += 2; break;
3629 case 'n':
3630 val /= 1e9; p += 2; break;
3631 }
3632 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003633 if (*p && !qemu_isspace(*p)) {
3634 monitor_printf(mon, "Unknown unit suffix\n");
3635 goto fail;
3636 }
3637 qdict_put(qdict, key, qfloat_from_double(val));
3638 }
3639 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003640 case 'b':
3641 {
3642 const char *beg;
3643 int val;
3644
3645 while (qemu_isspace(*p)) {
3646 p++;
3647 }
3648 beg = p;
3649 while (qemu_isgraph(*p)) {
3650 p++;
3651 }
3652 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3653 val = 1;
3654 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3655 val = 0;
3656 } else {
3657 monitor_printf(mon, "Expected 'on' or 'off'\n");
3658 goto fail;
3659 }
3660 qdict_put(qdict, key, qbool_from_int(val));
3661 }
3662 break;
bellard9307c4c2004-04-04 12:57:25 +00003663 case '-':
3664 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003665 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003666 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003667 /* option */
ths3b46e622007-09-17 08:09:54 +00003668
bellard9307c4c2004-04-04 12:57:25 +00003669 c = *typestr++;
3670 if (c == '\0')
3671 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003672 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003673 p++;
bellard9307c4c2004-04-04 12:57:25 +00003674 if (*p == '-') {
3675 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003676 if(c != *p) {
3677 if(!is_valid_option(p, typestr)) {
3678
3679 monitor_printf(mon, "%s: unsupported option -%c\n",
3680 cmdname, *p);
3681 goto fail;
3682 } else {
3683 skip_key = 1;
3684 }
bellard9307c4c2004-04-04 12:57:25 +00003685 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003686 if(skip_key) {
3687 p = tmp;
3688 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003689 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003690 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003691 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003692 }
bellard9307c4c2004-04-04 12:57:25 +00003693 }
bellard9307c4c2004-04-04 12:57:25 +00003694 }
3695 break;
3696 default:
3697 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003698 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003699 goto fail;
3700 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003701 qemu_free(key);
3702 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003703 }
3704 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003705 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003706 p++;
3707 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003708 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3709 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003710 goto fail;
3711 }
3712
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003713 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003714
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003715fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003716 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003717 return NULL;
3718}
3719
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003720void monitor_set_error(Monitor *mon, QError *qerror)
3721{
3722 /* report only the first error */
3723 if (!mon->error) {
3724 mon->error = qerror;
3725 } else {
3726 MON_DEBUG("Additional error report at %s:%d\n",
3727 qerror->file, qerror->linenr);
3728 QDECREF(qerror);
3729 }
3730}
3731
Adam Litke940cc302010-01-25 12:18:44 -06003732static int is_async_return(const QObject *data)
3733{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003734 if (data && qobject_type(data) == QTYPE_QDICT) {
3735 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3736 }
3737
3738 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003739}
3740
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003741static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3742{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003743 if (monitor_ctrl_mode(mon)) {
3744 if (ret && !monitor_has_error(mon)) {
3745 /*
3746 * If it returns failure, it must have passed on error.
3747 *
3748 * Action: Report an internal error to the client if in QMP.
3749 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003750 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003751 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3752 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003753 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003754
3755#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003756 if (!ret && monitor_has_error(mon)) {
3757 /*
3758 * If it returns success, it must not have passed an error.
3759 *
3760 * Action: Report the passed error to the client.
3761 */
3762 MON_DEBUG("command '%s' returned success but passed an error\n",
3763 cmd->name);
3764 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003765
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003766 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3767 /*
3768 * Handlers should not call Monitor print functions.
3769 *
3770 * Action: Ignore them in QMP.
3771 *
3772 * (XXX: we don't check any 'info' or 'query' command here
3773 * because the user print function _is_ called by do_info(), hence
3774 * we will trigger this check. This problem will go away when we
3775 * make 'query' commands real and kill do_info())
3776 */
3777 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3778 cmd->name, mon_print_count_get(mon));
3779 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003780#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003781 } else {
3782 assert(!monitor_has_error(mon));
3783 QDECREF(mon->error);
3784 mon->error = NULL;
3785 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003786}
3787
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003788static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3789 const QDict *params)
3790{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003791 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003792 QObject *data = NULL;
3793
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003794 mon_print_count_init(mon);
3795
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003796 ret = cmd->mhandler.cmd_new(mon, params, &data);
3797 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003798
Adam Litke940cc302010-01-25 12:18:44 -06003799 if (is_async_return(data)) {
3800 /*
3801 * Asynchronous commands have no initial return data but they can
3802 * generate errors. Data is returned via the async completion handler.
3803 */
3804 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3805 monitor_protocol_emitter(mon, NULL);
3806 }
3807 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003808 /* Monitor Protocol */
3809 monitor_protocol_emitter(mon, data);
3810 } else {
3811 /* User Protocol */
3812 if (data)
3813 cmd->user_print(mon, data);
3814 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003815
3816 qobject_decref(data);
3817}
3818
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003819static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003820{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003821 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003822 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003823
3824 qdict = qdict_new();
3825
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003826 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003827 if (!cmd)
3828 goto out;
3829
Adam Litke940cc302010-01-25 12:18:44 -06003830 if (monitor_handler_is_async(cmd)) {
3831 user_async_cmd_handler(mon, cmd, qdict);
3832 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003833 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003834 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003835 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003836 }
3837
Luiz Capitulino13917be2009-10-07 13:41:54 -03003838out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003839 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003840}
3841
bellard81d09122004-07-14 17:21:37 +00003842static void cmd_completion(const char *name, const char *list)
3843{
3844 const char *p, *pstart;
3845 char cmd[128];
3846 int len;
3847
3848 p = list;
3849 for(;;) {
3850 pstart = p;
3851 p = strchr(p, '|');
3852 if (!p)
3853 p = pstart + strlen(pstart);
3854 len = p - pstart;
3855 if (len > sizeof(cmd) - 2)
3856 len = sizeof(cmd) - 2;
3857 memcpy(cmd, pstart, len);
3858 cmd[len] = '\0';
3859 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003860 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003861 }
3862 if (*p == '\0')
3863 break;
3864 p++;
3865 }
3866}
3867
3868static void file_completion(const char *input)
3869{
3870 DIR *ffs;
3871 struct dirent *d;
3872 char path[1024];
3873 char file[1024], file_prefix[1024];
3874 int input_path_len;
3875 const char *p;
3876
ths5fafdf22007-09-16 21:08:06 +00003877 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003878 if (!p) {
3879 input_path_len = 0;
3880 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003881 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003882 } else {
3883 input_path_len = p - input + 1;
3884 memcpy(path, input, input_path_len);
3885 if (input_path_len > sizeof(path) - 1)
3886 input_path_len = sizeof(path) - 1;
3887 path[input_path_len] = '\0';
3888 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3889 }
3890#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003891 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3892 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003893#endif
3894 ffs = opendir(path);
3895 if (!ffs)
3896 return;
3897 for(;;) {
3898 struct stat sb;
3899 d = readdir(ffs);
3900 if (!d)
3901 break;
3902 if (strstart(d->d_name, file_prefix, NULL)) {
3903 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003904 if (input_path_len < sizeof(file))
3905 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3906 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003907 /* stat the file to find out if it's a directory.
3908 * In that case add a slash to speed up typing long paths
3909 */
3910 stat(file, &sb);
3911 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003912 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003913 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003914 }
3915 }
3916 closedir(ffs);
3917}
3918
aliguori51de9762009-03-05 23:00:43 +00003919static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003920{
aliguori51de9762009-03-05 23:00:43 +00003921 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003922 const char *input = opaque;
3923
3924 if (input[0] == '\0' ||
3925 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003926 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003927 }
3928}
3929
3930/* NOTE: this parser is an approximate form of the real command parser */
3931static void parse_cmdline(const char *cmdline,
3932 int *pnb_args, char **args)
3933{
3934 const char *p;
3935 int nb_args, ret;
3936 char buf[1024];
3937
3938 p = cmdline;
3939 nb_args = 0;
3940 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003941 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003942 p++;
3943 if (*p == '\0')
3944 break;
3945 if (nb_args >= MAX_ARGS)
3946 break;
3947 ret = get_str(buf, sizeof(buf), &p);
3948 args[nb_args] = qemu_strdup(buf);
3949 nb_args++;
3950 if (ret < 0)
3951 break;
3952 }
3953 *pnb_args = nb_args;
3954}
3955
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003956static const char *next_arg_type(const char *typestr)
3957{
3958 const char *p = strchr(typestr, ':');
3959 return (p != NULL ? ++p : typestr);
3960}
3961
aliguori4c36ba32009-03-05 23:01:37 +00003962static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003963{
3964 const char *cmdname;
3965 char *args[MAX_ARGS];
3966 int nb_args, i, len;
3967 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003968 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003969 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003970
3971 parse_cmdline(cmdline, &nb_args, args);
3972#ifdef DEBUG_COMPLETION
3973 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003974 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003975 }
3976#endif
3977
3978 /* if the line ends with a space, it means we want to complete the
3979 next arg */
3980 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003981 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003982 if (nb_args >= MAX_ARGS) {
3983 goto cleanup;
3984 }
bellard81d09122004-07-14 17:21:37 +00003985 args[nb_args++] = qemu_strdup("");
3986 }
3987 if (nb_args <= 1) {
3988 /* command completion */
3989 if (nb_args == 0)
3990 cmdname = "";
3991 else
3992 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003993 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003994 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003995 cmd_completion(cmdname, cmd->name);
3996 }
3997 } else {
3998 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02003999 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4000 if (compare_cmd(args[0], cmd->name)) {
4001 break;
4002 }
bellard81d09122004-07-14 17:21:37 +00004003 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004004 if (!cmd->name) {
4005 goto cleanup;
4006 }
4007
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004008 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004009 for(i = 0; i < nb_args - 2; i++) {
4010 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004011 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004012 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004013 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004014 }
4015 }
4016 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004017 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004018 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004019 }
bellard81d09122004-07-14 17:21:37 +00004020 switch(*ptype) {
4021 case 'F':
4022 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004023 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004024 file_completion(str);
4025 break;
4026 case 'B':
4027 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004028 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004029 bdrv_iterate(block_completion_it, (void *)str);
4030 break;
bellard7fe48482004-10-09 18:08:01 +00004031 case 's':
4032 /* XXX: more generic ? */
4033 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004034 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004035 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4036 cmd_completion(str, cmd->name);
4037 }
bellard64866c32006-05-07 18:03:31 +00004038 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004039 char *sep = strrchr(str, '-');
4040 if (sep)
4041 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004042 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004043 for(key = key_defs; key->name != NULL; key++) {
4044 cmd_completion(str, key->name);
4045 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004046 } else if (!strcmp(cmd->name, "help|?")) {
4047 readline_set_completion_index(cur_mon->rs, strlen(str));
4048 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4049 cmd_completion(str, cmd->name);
4050 }
bellard7fe48482004-10-09 18:08:01 +00004051 }
4052 break;
bellard81d09122004-07-14 17:21:37 +00004053 default:
4054 break;
4055 }
4056 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004057
4058cleanup:
4059 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00004060 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004061 }
bellard81d09122004-07-14 17:21:37 +00004062}
4063
aliguori731b0362009-03-05 23:01:42 +00004064static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004065{
aliguori731b0362009-03-05 23:01:42 +00004066 Monitor *mon = opaque;
4067
Jan Kiszkac62313b2009-12-04 14:05:29 +01004068 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004069}
4070
Luiz Capitulino09069b12010-02-04 18:10:06 -02004071static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4072{
4073 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4074 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4075}
4076
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004077/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004078 * Argument validation rules:
4079 *
4080 * 1. The argument must exist in cmd_args qdict
4081 * 2. The argument type must be the expected one
4082 *
4083 * Special case: If the argument doesn't exist in cmd_args and
4084 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4085 * checking is skipped for it.
4086 */
4087static int check_client_args_type(const QDict *client_args,
4088 const QDict *cmd_args, int flags)
4089{
4090 const QDictEntry *ent;
4091
4092 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4093 QObject *obj;
4094 QString *arg_type;
4095 const QObject *client_arg = qdict_entry_value(ent);
4096 const char *client_arg_name = qdict_entry_key(ent);
4097
4098 obj = qdict_get(cmd_args, client_arg_name);
4099 if (!obj) {
4100 if (flags & QMP_ACCEPT_UNKNOWNS) {
4101 /* handler accepts unknowns */
4102 continue;
4103 }
4104 /* client arg doesn't exist */
4105 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4106 return -1;
4107 }
4108
4109 arg_type = qobject_to_qstring(obj);
4110 assert(arg_type != NULL);
4111
4112 /* check if argument's type is correct */
4113 switch (qstring_get_str(arg_type)[0]) {
4114 case 'F':
4115 case 'B':
4116 case 's':
4117 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4118 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4119 "string");
4120 return -1;
4121 }
4122 break;
4123 case 'i':
4124 case 'l':
4125 case 'M':
4126 if (qobject_type(client_arg) != QTYPE_QINT) {
4127 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4128 "int");
4129 return -1;
4130 }
4131 break;
4132 case 'f':
4133 case 'T':
4134 if (qobject_type(client_arg) != QTYPE_QINT &&
4135 qobject_type(client_arg) != QTYPE_QFLOAT) {
4136 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4137 "number");
4138 return -1;
4139 }
4140 break;
4141 case 'b':
4142 case '-':
4143 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4144 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4145 "bool");
4146 return -1;
4147 }
4148 break;
4149 case 'O':
4150 assert(flags & QMP_ACCEPT_UNKNOWNS);
4151 break;
4152 case '/':
4153 case '.':
4154 /*
4155 * These types are not supported by QMP and thus are not
4156 * handled here. Fall through.
4157 */
4158 default:
4159 abort();
4160 }
4161 }
4162
4163 return 0;
4164}
4165
4166/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004167 * - Check if the client has passed all mandatory args
4168 * - Set special flags for argument validation
4169 */
4170static int check_mandatory_args(const QDict *cmd_args,
4171 const QDict *client_args, int *flags)
4172{
4173 const QDictEntry *ent;
4174
4175 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4176 const char *cmd_arg_name = qdict_entry_key(ent);
4177 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4178 assert(type != NULL);
4179
4180 if (qstring_get_str(type)[0] == 'O') {
4181 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4182 *flags |= QMP_ACCEPT_UNKNOWNS;
4183 } else if (qstring_get_str(type)[0] != '-' &&
4184 qstring_get_str(type)[1] != '?' &&
4185 !qdict_haskey(client_args, cmd_arg_name)) {
4186 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4187 return -1;
4188 }
4189 }
4190
4191 return 0;
4192}
4193
4194static QDict *qdict_from_args_type(const char *args_type)
4195{
4196 int i;
4197 QDict *qdict;
4198 QString *key, *type, *cur_qs;
4199
4200 assert(args_type != NULL);
4201
4202 qdict = qdict_new();
4203
4204 if (args_type == NULL || args_type[0] == '\0') {
4205 /* no args, empty qdict */
4206 goto out;
4207 }
4208
4209 key = qstring_new();
4210 type = qstring_new();
4211
4212 cur_qs = key;
4213
4214 for (i = 0;; i++) {
4215 switch (args_type[i]) {
4216 case ',':
4217 case '\0':
4218 qdict_put(qdict, qstring_get_str(key), type);
4219 QDECREF(key);
4220 if (args_type[i] == '\0') {
4221 goto out;
4222 }
4223 type = qstring_new(); /* qdict has ref */
4224 cur_qs = key = qstring_new();
4225 break;
4226 case ':':
4227 cur_qs = type;
4228 break;
4229 default:
4230 qstring_append_chr(cur_qs, args_type[i]);
4231 break;
4232 }
4233 }
4234
4235out:
4236 return qdict;
4237}
4238
4239/*
4240 * Client argument checking rules:
4241 *
4242 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004243 * 2. Each argument provided by the client must be expected
4244 * 3. Each argument provided by the client must have the type expected
4245 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004246 */
4247static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4248{
4249 int flags, err;
4250 QDict *cmd_args;
4251
4252 cmd_args = qdict_from_args_type(cmd->args_type);
4253
4254 flags = 0;
4255 err = check_mandatory_args(cmd_args, client_args, &flags);
4256 if (err) {
4257 goto out;
4258 }
4259
Luiz Capitulino4af91932010-06-22 11:44:05 -03004260 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004261
4262out:
4263 QDECREF(cmd_args);
4264 return err;
4265}
4266
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004267/*
4268 * Input object checking rules
4269 *
4270 * 1. Input object must be a dict
4271 * 2. The "execute" key must exist
4272 * 3. The "execute" key must be a string
4273 * 4. If the "arguments" key exists, it must be a dict
4274 * 5. If the "id" key exists, it can be anything (ie. json-value)
4275 * 6. Any argument not listed above is considered invalid
4276 */
4277static QDict *qmp_check_input_obj(QObject *input_obj)
4278{
4279 const QDictEntry *ent;
4280 int has_exec_key = 0;
4281 QDict *input_dict;
4282
4283 if (qobject_type(input_obj) != QTYPE_QDICT) {
4284 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4285 return NULL;
4286 }
4287
4288 input_dict = qobject_to_qdict(input_obj);
4289
4290 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4291 const char *arg_name = qdict_entry_key(ent);
4292 const QObject *arg_obj = qdict_entry_value(ent);
4293
4294 if (!strcmp(arg_name, "execute")) {
4295 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4296 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4297 "string");
4298 return NULL;
4299 }
4300 has_exec_key = 1;
4301 } else if (!strcmp(arg_name, "arguments")) {
4302 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4303 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4304 "object");
4305 return NULL;
4306 }
4307 } else if (!strcmp(arg_name, "id")) {
4308 /* FIXME: check duplicated IDs for async commands */
4309 } else {
4310 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4311 return NULL;
4312 }
4313 }
4314
4315 if (!has_exec_key) {
4316 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4317 return NULL;
4318 }
4319
4320 return input_dict;
4321}
4322
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004323static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4324{
4325 int err;
4326 QObject *obj;
4327 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004328 const mon_cmd_t *cmd;
4329 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004330 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004331
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004332 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004333
4334 obj = json_parser_parse(tokens, NULL);
4335 if (!obj) {
4336 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004337 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004338 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004339 }
4340
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004341 input = qmp_check_input_obj(obj);
4342 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004343 qobject_decref(obj);
4344 goto err_out;
4345 }
4346
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004347 mon->mc->id = qdict_get(input, "id");
4348 qobject_incref(mon->mc->id);
4349
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004350 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004351 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004352 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004353 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004354 }
4355
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004356 /*
4357 * XXX: We need this special case until we get info handlers
4358 * converted into 'query-' commands
4359 */
4360 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004361 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004362 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004363 } else if (strstart(cmd_name, "query-", &info_item)) {
4364 cmd = monitor_find_command("info");
4365 qdict_put_obj(input, "arguments",
4366 qobject_from_jsonf("{ 'item': %s }", info_item));
4367 } else {
4368 cmd = monitor_find_command(cmd_name);
Jan Kiszkaa6c4d362010-06-28 18:27:47 +02004369 if (!cmd || !monitor_handler_ported(cmd)
4370 || monitor_cmd_user_only(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004371 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004372 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004373 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004374 }
4375
4376 obj = qdict_get(input, "arguments");
4377 if (!obj) {
4378 args = qdict_new();
4379 } else {
4380 args = qobject_to_qdict(obj);
4381 QINCREF(args);
4382 }
4383
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004384 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004385 if (err < 0) {
4386 goto err_out;
4387 }
4388
Adam Litke940cc302010-01-25 12:18:44 -06004389 if (monitor_handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004390 err = qmp_async_cmd_handler(mon, cmd, args);
4391 if (err) {
4392 /* emit the error response */
4393 goto err_out;
4394 }
Adam Litke940cc302010-01-25 12:18:44 -06004395 } else {
4396 monitor_call_handler(mon, cmd, args);
4397 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004398
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004399 goto out;
4400
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004401err_out:
4402 monitor_protocol_emitter(mon, NULL);
4403out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004404 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004405 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004406}
4407
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004408/**
4409 * monitor_control_read(): Read and handle QMP input
4410 */
4411static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4412{
4413 Monitor *old_mon = cur_mon;
4414
4415 cur_mon = opaque;
4416
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004417 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004418
4419 cur_mon = old_mon;
4420}
4421
aliguori731b0362009-03-05 23:01:42 +00004422static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004423{
aliguori731b0362009-03-05 23:01:42 +00004424 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004425 int i;
aliguori376253e2009-03-05 23:01:23 +00004426
aliguori731b0362009-03-05 23:01:42 +00004427 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004428
aliguoricde76ee2009-03-05 23:01:51 +00004429 if (cur_mon->rs) {
4430 for (i = 0; i < size; i++)
4431 readline_handle_byte(cur_mon->rs, buf[i]);
4432 } else {
4433 if (size == 0 || buf[size - 1] != 0)
4434 monitor_printf(cur_mon, "corrupted command\n");
4435 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004436 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004437 }
aliguori731b0362009-03-05 23:01:42 +00004438
4439 cur_mon = old_mon;
4440}
aliguorid8f44602008-10-06 13:52:44 +00004441
aliguori376253e2009-03-05 23:01:23 +00004442static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004443{
aliguori731b0362009-03-05 23:01:42 +00004444 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004445 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004446 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004447}
4448
aliguoricde76ee2009-03-05 23:01:51 +00004449int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004450{
aliguoricde76ee2009-03-05 23:01:51 +00004451 if (!mon->rs)
4452 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004453 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004454 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004455}
4456
aliguori376253e2009-03-05 23:01:23 +00004457void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004458{
aliguoricde76ee2009-03-05 23:01:51 +00004459 if (!mon->rs)
4460 return;
aliguori731b0362009-03-05 23:01:42 +00004461 if (--mon->suspend_cnt == 0)
4462 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004463}
4464
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004465static QObject *get_qmp_greeting(void)
4466{
4467 QObject *ver;
4468
4469 do_info_version(NULL, &ver);
4470 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4471}
4472
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004473/**
4474 * monitor_control_event(): Print QMP gretting
4475 */
4476static void monitor_control_event(void *opaque, int event)
4477{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004478 QObject *data;
4479 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004480
Luiz Capitulino47116d12010-02-08 17:01:30 -02004481 switch (event) {
4482 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004483 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004484 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004485 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004486 monitor_json_emitter(mon, data);
4487 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004488 break;
4489 case CHR_EVENT_CLOSED:
4490 json_message_parser_destroy(&mon->mc->parser);
4491 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004492 }
4493}
4494
aliguori731b0362009-03-05 23:01:42 +00004495static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004496{
aliguori376253e2009-03-05 23:01:23 +00004497 Monitor *mon = opaque;
4498
aliguori2724b182009-03-05 23:01:47 +00004499 switch (event) {
4500 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004501 mon->mux_out = 0;
4502 if (mon->reset_seen) {
4503 readline_restart(mon->rs);
4504 monitor_resume(mon);
4505 monitor_flush(mon);
4506 } else {
4507 mon->suspend_cnt = 0;
4508 }
aliguori2724b182009-03-05 23:01:47 +00004509 break;
ths86e94de2007-01-05 22:01:59 +00004510
aliguori2724b182009-03-05 23:01:47 +00004511 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004512 if (mon->reset_seen) {
4513 if (mon->suspend_cnt == 0) {
4514 monitor_printf(mon, "\n");
4515 }
4516 monitor_flush(mon);
4517 monitor_suspend(mon);
4518 } else {
4519 mon->suspend_cnt++;
4520 }
4521 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004522 break;
4523
Amit Shahb6b8df52009-10-07 18:31:16 +05304524 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004525 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4526 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004527 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004528 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004529 }
4530 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004531 break;
4532 }
ths86e94de2007-01-05 22:01:59 +00004533}
4534
aliguori76655d62009-03-06 20:27:37 +00004535
4536/*
4537 * Local variables:
4538 * c-indent-level: 4
4539 * c-basic-offset: 4
4540 * tab-width: 8
4541 * End:
4542 */
4543
aliguori731b0362009-03-05 23:01:42 +00004544void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004545{
aliguori731b0362009-03-05 23:01:42 +00004546 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004547 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004548
4549 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004550 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004551 is_first_init = 0;
4552 }
aliguori87127162009-03-05 23:01:29 +00004553
4554 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004555
aliguori87127162009-03-05 23:01:29 +00004556 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004557 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004558 if (flags & MONITOR_USE_READLINE) {
4559 mon->rs = readline_init(mon, monitor_find_completion);
4560 monitor_read_command(mon, 0);
4561 }
aliguori87127162009-03-05 23:01:29 +00004562
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004563 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004564 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004565 /* Control mode requires special handlers */
4566 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4567 monitor_control_event, mon);
4568 } else {
4569 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4570 monitor_event, mon);
4571 }
aliguori87127162009-03-05 23:01:29 +00004572
Blue Swirl72cf2d42009-09-12 07:36:22 +00004573 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004574 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4575 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004576}
4577
aliguori376253e2009-03-05 23:01:23 +00004578static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004579{
aliguoribb5fc202009-03-05 23:01:15 +00004580 BlockDriverState *bs = opaque;
4581 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004582
aliguoribb5fc202009-03-05 23:01:15 +00004583 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004584 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004585 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004586 }
aliguori731b0362009-03-05 23:01:42 +00004587 if (mon->password_completion_cb)
4588 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004589
aliguori731b0362009-03-05 23:01:42 +00004590 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004591}
aliguoric0f4ce72009-03-05 23:01:01 +00004592
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004593int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4594 BlockDriverCompletionFunc *completion_cb,
4595 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004596{
aliguoricde76ee2009-03-05 23:01:51 +00004597 int err;
4598
aliguoribb5fc202009-03-05 23:01:15 +00004599 if (!bdrv_key_required(bs)) {
4600 if (completion_cb)
4601 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004602 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004603 }
aliguoric0f4ce72009-03-05 23:01:01 +00004604
Luiz Capitulino94171e12009-12-07 21:37:00 +01004605 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004606 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004607 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004608 }
4609
aliguori376253e2009-03-05 23:01:23 +00004610 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4611 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004612
aliguori731b0362009-03-05 23:01:42 +00004613 mon->password_completion_cb = completion_cb;
4614 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004615
aliguoricde76ee2009-03-05 23:01:51 +00004616 err = monitor_read_password(mon, bdrv_password_cb, bs);
4617
4618 if (err && completion_cb)
4619 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004620
4621 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004622}