blob: 377ab378c297a79c5563075104515643ac392b0d [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
Stefan Weil8b7968f2010-09-23 21:28:05 +0200319static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
320 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000321{
322 va_list ap;
323 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000324 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000325 va_end(ap);
326 return 0;
327}
328
Luiz Capitulino13c74252009-10-07 13:41:55 -0300329static void monitor_user_noop(Monitor *mon, const QObject *data) { }
330
Luiz Capitulino13917be2009-10-07 13:41:54 -0300331static inline int monitor_handler_ported(const mon_cmd_t *cmd)
332{
333 return cmd->user_print != NULL;
334}
335
Adam Litke940cc302010-01-25 12:18:44 -0600336static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
337{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200338 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600339}
340
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200341static inline bool monitor_cmd_user_only(const mon_cmd_t *cmd)
342{
343 return (cmd->flags & MONITOR_CMD_USER_ONLY);
bellard9dc39cb2004-03-14 21:38:27 +0000344}
345
Luiz Capitulino8204a912009-11-18 23:05:31 -0200346static inline int monitor_has_error(const Monitor *mon)
347{
348 return mon->error != NULL;
349}
350
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200351static void monitor_json_emitter(Monitor *mon, const QObject *data)
352{
353 QString *json;
354
355 json = qobject_to_json(data);
356 assert(json != NULL);
357
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200358 qstring_append_chr(json, '\n');
359 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200360
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200361 QDECREF(json);
362}
363
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200364static void monitor_protocol_emitter(Monitor *mon, QObject *data)
365{
366 QDict *qmp;
367
368 qmp = qdict_new();
369
370 if (!monitor_has_error(mon)) {
371 /* success response */
372 if (data) {
373 qobject_incref(data);
374 qdict_put_obj(qmp, "return", data);
375 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200376 /* return an empty QDict by default */
377 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200378 }
379 } else {
380 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100381 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200382 qdict_put(qmp, "error", mon->error->error);
383 QINCREF(mon->error->error);
384 QDECREF(mon->error);
385 mon->error = NULL;
386 }
387
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200388 if (mon->mc->id) {
389 qdict_put_obj(qmp, "id", mon->mc->id);
390 mon->mc->id = NULL;
391 }
392
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200393 monitor_json_emitter(mon, QOBJECT(qmp));
394 QDECREF(qmp);
395}
396
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200397static void timestamp_put(QDict *qdict)
398{
399 int err;
400 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000401 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200402
Blue Swirld08d6f02009-12-04 18:06:39 +0000403 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200404 if (err < 0)
405 return;
406
407 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
408 "'microseconds': %" PRId64 " }",
409 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410 qdict_put_obj(qdict, "timestamp", obj);
411}
412
413/**
414 * monitor_protocol_event(): Generate a Monitor event
415 *
416 * Event-specific data can be emitted through the (optional) 'data' parameter.
417 */
418void monitor_protocol_event(MonitorEvent event, QObject *data)
419{
420 QDict *qmp;
421 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600422 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200423
Blue Swirl242cd002009-12-04 18:05:45 +0000424 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200426 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000427 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200428 event_name = "SHUTDOWN";
429 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000430 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200431 event_name = "RESET";
432 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000433 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200434 event_name = "POWERDOWN";
435 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000436 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200437 event_name = "STOP";
438 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300439 case QEVENT_RESUME:
440 event_name = "RESUME";
441 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200442 case QEVENT_VNC_CONNECTED:
443 event_name = "VNC_CONNECTED";
444 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200445 case QEVENT_VNC_INITIALIZED:
446 event_name = "VNC_INITIALIZED";
447 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200448 case QEVENT_VNC_DISCONNECTED:
449 event_name = "VNC_DISCONNECTED";
450 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200451 case QEVENT_BLOCK_IO_ERROR:
452 event_name = "BLOCK_IO_ERROR";
453 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300454 case QEVENT_RTC_CHANGE:
455 event_name = "RTC_CHANGE";
456 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300457 case QEVENT_WATCHDOG:
458 event_name = "WATCHDOG";
459 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200460 default:
461 abort();
462 break;
463 }
464
465 qmp = qdict_new();
466 timestamp_put(qmp);
467 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200468 if (data) {
469 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200470 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200471 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200472
Adam Litkef039a562010-01-15 08:34:02 -0600473 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200474 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200475 monitor_json_emitter(mon, QOBJECT(qmp));
476 }
Adam Litkef039a562010-01-15 08:34:02 -0600477 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200478 QDECREF(qmp);
479}
480
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200481static int do_qmp_capabilities(Monitor *mon, const QDict *params,
482 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200483{
484 /* Will setup QMP capabilities in the future */
485 if (monitor_ctrl_mode(mon)) {
486 mon->mc->command_mode = 1;
487 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200488
489 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200490}
491
bellard9dc39cb2004-03-14 21:38:27 +0000492static int compare_cmd(const char *name, const char *list)
493{
494 const char *p, *pstart;
495 int len;
496 len = strlen(name);
497 p = list;
498 for(;;) {
499 pstart = p;
500 p = strchr(p, '|');
501 if (!p)
502 p = pstart + strlen(pstart);
503 if ((p - pstart) == len && !memcmp(pstart, name, len))
504 return 1;
505 if (*p == '\0')
506 break;
507 p++;
508 }
509 return 0;
510}
511
Anthony Liguoric227f092009-10-01 16:12:16 -0500512static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000513 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000514{
Anthony Liguoric227f092009-10-01 16:12:16 -0500515 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000516
517 for(cmd = cmds; cmd->name != NULL; cmd++) {
518 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000519 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
520 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000521 }
522}
523
aliguori376253e2009-03-05 23:01:23 +0000524static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000525{
526 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000527 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000528 } else {
aliguori376253e2009-03-05 23:01:23 +0000529 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000530 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000531 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000532 monitor_printf(mon, "Log items (comma separated):\n");
533 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000534 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000535 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000536 }
537 }
bellard9dc39cb2004-03-14 21:38:27 +0000538 }
539}
540
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300541static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300542{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300543 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300544}
545
Prerna Saxena22890ab2010-06-24 17:04:53 +0530546#ifdef CONFIG_SIMPLE_TRACE
547static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
548{
549 const char *tp_name = qdict_get_str(qdict, "name");
550 bool new_state = qdict_get_bool(qdict, "option");
551 st_change_trace_event_state(tp_name, new_state);
552}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100553
554static void do_trace_file(Monitor *mon, const QDict *qdict)
555{
556 const char *op = qdict_get_try_str(qdict, "op");
557 const char *arg = qdict_get_try_str(qdict, "arg");
558
559 if (!op) {
560 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
561 } else if (!strcmp(op, "on")) {
562 st_set_trace_file_enabled(true);
563 } else if (!strcmp(op, "off")) {
564 st_set_trace_file_enabled(false);
565 } else if (!strcmp(op, "flush")) {
566 st_flush_trace_buffer();
567 } else if (!strcmp(op, "set")) {
568 if (arg) {
569 st_set_trace_file(arg);
570 }
571 } else {
572 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
573 help_cmd(mon, "trace-file");
574 }
575}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530576#endif
577
Adam Litke940cc302010-01-25 12:18:44 -0600578static void user_monitor_complete(void *opaque, QObject *ret_data)
579{
580 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
581
582 if (ret_data) {
583 data->user_print(data->mon, ret_data);
584 }
585 monitor_resume(data->mon);
586 qemu_free(data);
587}
588
589static void qmp_monitor_complete(void *opaque, QObject *ret_data)
590{
591 monitor_protocol_emitter(opaque, ret_data);
592}
593
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300594static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
595 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600596{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300597 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600598}
599
600static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
601{
602 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
603}
604
605static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
606 const QDict *params)
607{
608 int ret;
609
610 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
611 cb_data->mon = mon;
612 cb_data->user_print = cmd->user_print;
613 monitor_suspend(mon);
614 ret = cmd->mhandler.cmd_async(mon, params,
615 user_monitor_complete, cb_data);
616 if (ret < 0) {
617 monitor_resume(mon);
618 qemu_free(cb_data);
619 }
620}
621
622static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
623{
624 int ret;
625
626 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
627 cb_data->mon = mon;
628 cb_data->user_print = cmd->user_print;
629 monitor_suspend(mon);
630 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
631 if (ret < 0) {
632 monitor_resume(mon);
633 qemu_free(cb_data);
634 }
635}
636
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200637static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000638{
Anthony Liguoric227f092009-10-01 16:12:16 -0500639 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300640 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000641
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200642 if (!item) {
643 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000644 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200645 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300646
647 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000648 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300649 break;
bellard9dc39cb2004-03-14 21:38:27 +0000650 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300651
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200652 if (cmd->name == NULL) {
653 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100654 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200655 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200656 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300657 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200658 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300659
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200660 if (monitor_ctrl_mode(mon) && monitor_cmd_user_only(cmd)) {
661 qerror_report(QERR_COMMAND_NOT_FOUND, item);
662 return -1;
663 }
664
Adam Litke940cc302010-01-25 12:18:44 -0600665 if (monitor_handler_is_async(cmd)) {
666 if (monitor_ctrl_mode(mon)) {
667 qmp_async_info_handler(mon, cmd);
668 } else {
669 user_async_info_handler(mon, cmd);
670 }
671 /*
672 * Indicate that this command is asynchronous and will not return any
673 * data (not even empty). Instead, the data will be returned via a
674 * completion callback.
675 */
676 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
677 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300678 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200679
680 if (!monitor_ctrl_mode(mon)) {
681 /*
682 * User Protocol function is called here, Monitor Protocol is
683 * handled by monitor_call_handler()
684 */
685 if (*ret_data)
686 cmd->user_print(mon, *ret_data);
687 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300688 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200689 if (monitor_ctrl_mode(mon)) {
690 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100691 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200692 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200693 } else {
694 cmd->mhandler.info(mon);
695 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300696 }
697
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200698 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300699
700help:
701 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200702 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000703}
704
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200705static void do_info_version_print(Monitor *mon, const QObject *data)
706{
707 QDict *qdict;
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300708 QDict *qemu;
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200709
710 qdict = qobject_to_qdict(data);
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300711 qemu = qdict_get_qdict(qdict, "qemu");
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200712
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300713 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
714 qdict_get_int(qemu, "major"),
715 qdict_get_int(qemu, "minor"),
716 qdict_get_int(qemu, "micro"),
717 qdict_get_str(qdict, "package"));
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200718}
719
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300720static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000721{
Miguel Di Ciurcio Filho0ec02912010-08-20 16:42:30 -0300722 const char *version = QEMU_VERSION;
723 int major = 0, minor = 0, micro = 0;
724 char *tmp;
725
726 major = strtol(version, &tmp, 10);
727 tmp++;
728 minor = strtol(tmp, &tmp, 10);
729 tmp++;
730 micro = strtol(tmp, &tmp, 10);
731
732 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
733 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000734}
735
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200736static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000737{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200738 QDict *qdict;
739
740 qdict = qobject_to_qdict(data);
741 if (qdict_size(qdict) == 0) {
742 return;
743 }
744
745 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
746}
747
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200748static void do_info_name(Monitor *mon, QObject **ret_data)
749{
750 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
751 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000752}
753
Luiz Capitulino1a728672009-12-10 17:15:56 -0200754static QObject *get_cmd_dict(const char *name)
755{
756 const char *p;
757
758 /* Remove '|' from some commands */
759 p = strchr(name, '|');
760 if (p) {
761 p++;
762 } else {
763 p = name;
764 }
765
766 return qobject_from_jsonf("{ 'name': %s }", p);
767}
768
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200769static void do_info_commands(Monitor *mon, QObject **ret_data)
770{
771 QList *cmd_list;
772 const mon_cmd_t *cmd;
773
774 cmd_list = qlist_new();
775
776 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200777 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd) &&
778 !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200779 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200780 }
781 }
782
783 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200784 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd)) {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200785 char buf[128];
786 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200787 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200788 }
789 }
790
791 *ret_data = QOBJECT(cmd_list);
792}
793
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200794static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000795{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200796 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
797}
798
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200799static void do_info_uuid(Monitor *mon, QObject **ret_data)
800{
801 char uuid[64];
802
803 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000804 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
805 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
806 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
807 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200808 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000809}
810
bellard6a00d602005-11-21 23:25:50 +0000811/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000812static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000813{
814 CPUState *env;
815
816 for(env = first_cpu; env != NULL; env = env->next_cpu) {
817 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000818 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000819 return 0;
820 }
821 }
822 return -1;
823}
824
pbrook9596ebb2007-11-18 01:44:38 +0000825static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000826{
aliguori731b0362009-03-05 23:01:42 +0000827 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000828 mon_set_cpu(0);
829 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300830 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000831 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000832}
833
aliguori376253e2009-03-05 23:01:23 +0000834static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000835{
bellard6a00d602005-11-21 23:25:50 +0000836 CPUState *env;
837 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000838#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000839 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000840 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000841#else
aliguori376253e2009-03-05 23:01:23 +0000842 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000843 0);
bellard9307c4c2004-04-04 12:57:25 +0000844#endif
845}
846
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300847static void print_cpu_iter(QObject *obj, void *opaque)
848{
849 QDict *cpu;
850 int active = ' ';
851 Monitor *mon = opaque;
852
853 assert(qobject_type(obj) == QTYPE_QDICT);
854 cpu = qobject_to_qdict(obj);
855
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200856 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300857 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200858 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300859
860 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
861
862#if defined(TARGET_I386)
863 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
864 (target_ulong) qdict_get_int(cpu, "pc"));
865#elif defined(TARGET_PPC)
866 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
867 (target_long) qdict_get_int(cpu, "nip"));
868#elif defined(TARGET_SPARC)
869 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
870 (target_long) qdict_get_int(cpu, "pc"));
871 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
872 (target_long) qdict_get_int(cpu, "npc"));
873#elif defined(TARGET_MIPS)
874 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
875 (target_long) qdict_get_int(cpu, "PC"));
876#endif
877
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200878 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200880 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881
882 monitor_printf(mon, "\n");
883}
884
885static void monitor_print_cpus(Monitor *mon, const QObject *data)
886{
887 QList *cpu_list;
888
889 assert(qobject_type(data) == QTYPE_QLIST);
890 cpu_list = qobject_to_qlist(data);
891 qlist_iter(cpu_list, print_cpu_iter, mon);
892}
893
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300894static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000895{
896 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300897 QList *cpu_list;
898
899 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000900
901 /* just to set the default cpu if not already done */
902 mon_get_cpu();
903
904 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200905 QDict *cpu;
906 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300907
Avi Kivity4c0960c2009-08-17 23:19:53 +0300908 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300909
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200910 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
911 env->cpu_index, env == mon->mon_cpu,
912 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200913
914 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300915
bellard6a00d602005-11-21 23:25:50 +0000916#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300917 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000918#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300919 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000920#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300921 qdict_put(cpu, "pc", qint_from_int(env->pc));
922 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000923#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300924 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000925#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300926
927 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000928 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300929
930 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000931}
932
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200933static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000934{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300935 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200936 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100937 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
938 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200939 return -1;
940 }
941 return 0;
bellard6a00d602005-11-21 23:25:50 +0000942}
943
aliguori376253e2009-03-05 23:01:23 +0000944static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000945{
aliguori376253e2009-03-05 23:01:23 +0000946 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000947}
948
aliguori376253e2009-03-05 23:01:23 +0000949static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000950{
951 int i;
bellard7e2515e2004-08-01 21:52:19 +0000952 const char *str;
ths3b46e622007-09-17 08:09:54 +0000953
aliguoricde76ee2009-03-05 23:01:51 +0000954 if (!mon->rs)
955 return;
bellard7e2515e2004-08-01 21:52:19 +0000956 i = 0;
957 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000958 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000959 if (!str)
960 break;
aliguori376253e2009-03-05 23:01:23 +0000961 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000962 i++;
bellardaa455482004-04-04 13:07:25 +0000963 }
964}
965
j_mayer76a66252007-03-07 08:32:30 +0000966#if defined(TARGET_PPC)
967/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000968static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000969{
970 CPUState *env;
971
972 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000973 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000974}
975#endif
976
Prerna Saxena22890ab2010-06-24 17:04:53 +0530977#if defined(CONFIG_SIMPLE_TRACE)
978static void do_info_trace(Monitor *mon)
979{
980 st_print_trace((FILE *)mon, &monitor_fprintf);
981}
982
983static void do_info_trace_events(Monitor *mon)
984{
985 st_print_trace_events((FILE *)mon, &monitor_fprintf);
986}
987#endif
988
Luiz Capitulinob223f352009-10-07 13:41:56 -0300989/**
990 * do_quit(): Quit QEMU execution
991 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200992static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000993{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300994 monitor_suspend(mon);
995 no_shutdown = 0;
996 qemu_system_shutdown_request();
997
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200998 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000999}
1000
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001001static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001002{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001003 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001004 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001005 return -1;
1006 }
aliguoribb5fc202009-03-05 23:01:15 +00001007
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001008 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001009}
1010
1011static void change_vnc_password_cb(Monitor *mon, const char *password,
1012 void *opaque)
1013{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001014 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001015 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001016}
1017
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001018static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001019{
ths70848512007-08-25 01:37:05 +00001020 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001021 strcmp(target, "password") == 0) {
1022 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001023 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001024 strncpy(password, arg, sizeof(password));
1025 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001026 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001027 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001028 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001029 }
ths70848512007-08-25 01:37:05 +00001030 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001031 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001032 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001033 return -1;
1034 }
ths70848512007-08-25 01:37:05 +00001035 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001036
1037 return 0;
thse25a5822007-08-25 01:36:20 +00001038}
1039
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001040/**
1041 * do_change(): Change a removable medium, or VNC configuration
1042 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001043static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001044{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001045 const char *device = qdict_get_str(qdict, "device");
1046 const char *target = qdict_get_str(qdict, "target");
1047 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001048 int ret;
1049
thse25a5822007-08-25 01:36:20 +00001050 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001051 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001052 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001053 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001054 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001055
1056 return ret;
thse25a5822007-08-25 01:36:20 +00001057}
1058
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001059static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001060{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001061 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001062 return 0;
bellard59a983b2004-03-17 23:17:16 +00001063}
1064
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001065static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001066{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001067 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001068}
1069
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001070static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001071{
1072 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001073 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001074
bellard9307c4c2004-04-04 12:57:25 +00001075 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001076 mask = 0;
1077 } else {
bellard9307c4c2004-04-04 12:57:25 +00001078 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001079 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001080 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001081 return;
1082 }
1083 }
1084 cpu_set_log(mask);
1085}
1086
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001087static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001088{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001089 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001090 if (!option || !strcmp(option, "on")) {
1091 singlestep = 1;
1092 } else if (!strcmp(option, "off")) {
1093 singlestep = 0;
1094 } else {
1095 monitor_printf(mon, "unexpected option %s\n", option);
1096 }
1097}
1098
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001099/**
1100 * do_stop(): Stop VM execution
1101 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001102static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001103{
1104 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001105 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001106}
1107
aliguoribb5fc202009-03-05 23:01:15 +00001108static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001109
aliguori376253e2009-03-05 23:01:23 +00001110struct bdrv_iterate_context {
1111 Monitor *mon;
1112 int err;
1113};
aliguoric0f4ce72009-03-05 23:01:01 +00001114
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001115/**
1116 * do_cont(): Resume emulation.
1117 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001118static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001119{
1120 struct bdrv_iterate_context context = { mon, 0 };
1121
Amit Shah8e848652010-07-27 15:49:19 +05301122 if (incoming_expected) {
1123 qerror_report(QERR_MIGRATION_EXPECTED);
1124 return -1;
1125 }
aliguori376253e2009-03-05 23:01:23 +00001126 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001127 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001128 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001129 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001130 return 0;
1131 } else {
1132 return -1;
1133 }
bellard8a7ddc32004-03-31 19:00:16 +00001134}
1135
aliguoribb5fc202009-03-05 23:01:15 +00001136static void bdrv_key_cb(void *opaque, int err)
1137{
aliguori376253e2009-03-05 23:01:23 +00001138 Monitor *mon = opaque;
1139
aliguoribb5fc202009-03-05 23:01:15 +00001140 /* another key was set successfully, retry to continue */
1141 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001142 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001143}
1144
1145static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1146{
aliguori376253e2009-03-05 23:01:23 +00001147 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001148
aliguori376253e2009-03-05 23:01:23 +00001149 if (!context->err && bdrv_key_required(bs)) {
1150 context->err = -EBUSY;
1151 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1152 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001153 }
1154}
1155
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001156static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001157{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001158 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001159 if (!device)
1160 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1161 if (gdbserver_start(device) < 0) {
1162 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1163 device);
1164 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001165 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001166 } else {
aliguori59030a82009-04-05 18:43:41 +00001167 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1168 device);
bellard8a7ddc32004-03-31 19:00:16 +00001169 }
1170}
1171
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001172static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001173{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001174 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001175 if (select_watchdog_action(action) == -1) {
1176 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1177 }
1178}
1179
aliguori376253e2009-03-05 23:01:23 +00001180static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001181{
aliguori376253e2009-03-05 23:01:23 +00001182 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001183 switch(c) {
1184 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001185 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001186 break;
1187 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001188 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001189 break;
1190 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001191 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001192 break;
1193 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001194 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001195 break;
1196 default:
1197 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001198 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001199 } else {
aliguori376253e2009-03-05 23:01:23 +00001200 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001201 }
1202 break;
1203 }
aliguori376253e2009-03-05 23:01:23 +00001204 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001205}
1206
aliguori376253e2009-03-05 23:01:23 +00001207static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001208 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001209{
bellard6a00d602005-11-21 23:25:50 +00001210 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001211 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001212 uint8_t buf[16];
1213 uint64_t v;
1214
1215 if (format == 'i') {
1216 int flags;
1217 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001218 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001219#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001220 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001221 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001222 } else if (wsize == 4) {
1223 flags = 0;
1224 } else {
bellard6a15fd12006-04-12 21:07:07 +00001225 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001226 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001227 if (env) {
1228#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001229 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001230 (env->segs[R_CS].flags & DESC_L_MASK))
1231 flags = 2;
1232 else
1233#endif
1234 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1235 flags = 1;
1236 }
bellard4c27ba22004-04-25 18:05:08 +00001237 }
1238#endif
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001240 return;
1241 }
1242
1243 len = wsize * count;
1244 if (wsize == 1)
1245 line_size = 8;
1246 else
1247 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001248 max_digits = 0;
1249
1250 switch(format) {
1251 case 'o':
1252 max_digits = (wsize * 8 + 2) / 3;
1253 break;
1254 default:
1255 case 'x':
1256 max_digits = (wsize * 8) / 4;
1257 break;
1258 case 'u':
1259 case 'd':
1260 max_digits = (wsize * 8 * 10 + 32) / 33;
1261 break;
1262 case 'c':
1263 wsize = 1;
1264 break;
1265 }
1266
1267 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001268 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001270 else
aliguori376253e2009-03-05 23:01:23 +00001271 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001272 l = len;
1273 if (l > line_size)
1274 l = line_size;
1275 if (is_physical) {
1276 cpu_physical_memory_rw(addr, buf, l, 0);
1277 } else {
bellard6a00d602005-11-21 23:25:50 +00001278 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001279 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001280 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001281 break;
1282 }
bellard9307c4c2004-04-04 12:57:25 +00001283 }
ths5fafdf22007-09-16 21:08:06 +00001284 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001285 while (i < l) {
1286 switch(wsize) {
1287 default:
1288 case 1:
1289 v = ldub_raw(buf + i);
1290 break;
1291 case 2:
1292 v = lduw_raw(buf + i);
1293 break;
1294 case 4:
bellard92a31b12005-02-10 22:00:52 +00001295 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001296 break;
1297 case 8:
1298 v = ldq_raw(buf + i);
1299 break;
1300 }
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001302 switch(format) {
1303 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001304 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001305 break;
1306 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001308 break;
1309 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001310 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001311 break;
1312 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001313 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001314 break;
1315 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001317 break;
1318 }
1319 i += wsize;
1320 }
aliguori376253e2009-03-05 23:01:23 +00001321 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001322 addr += l;
1323 len -= l;
1324 }
1325}
1326
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001327static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001328{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001329 int count = qdict_get_int(qdict, "count");
1330 int format = qdict_get_int(qdict, "format");
1331 int size = qdict_get_int(qdict, "size");
1332 target_long addr = qdict_get_int(qdict, "addr");
1333
aliguori376253e2009-03-05 23:01:23 +00001334 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001335}
1336
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001337static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001338{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001339 int count = qdict_get_int(qdict, "count");
1340 int format = qdict_get_int(qdict, "format");
1341 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001342 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001343
aliguori376253e2009-03-05 23:01:23 +00001344 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001345}
1346
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001347static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001348{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001349 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001350 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001351
blueswir17743e582007-09-24 18:39:04 +00001352#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001353 switch(format) {
1354 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001355 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001356 break;
1357 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001358 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001359 break;
1360 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001361 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001362 break;
1363 default:
1364 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001366 break;
1367 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001369 break;
1370 }
bellard92a31b12005-02-10 22:00:52 +00001371#else
1372 switch(format) {
1373 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001374 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001375 break;
1376 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001377 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001378 break;
1379 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001380 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001381 break;
1382 default:
1383 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001385 break;
1386 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001388 break;
1389 }
1390#endif
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001392}
1393
Luiz Capitulino98696222010-02-10 23:49:58 -02001394static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001395{
1396 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001397 uint32_t size = qdict_get_int(qdict, "size");
1398 const char *filename = qdict_get_str(qdict, "filename");
1399 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001400 uint32_t l;
1401 CPUState *env;
1402 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001403 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001404
1405 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001406
1407 f = fopen(filename, "wb");
1408 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001409 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001410 return -1;
bellardb371dc52007-01-03 15:20:39 +00001411 }
1412 while (size != 0) {
1413 l = sizeof(buf);
1414 if (l > size)
1415 l = size;
1416 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001417 if (fwrite(buf, 1, l, f) != l) {
1418 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1419 goto exit;
1420 }
bellardb371dc52007-01-03 15:20:39 +00001421 addr += l;
1422 size -= l;
1423 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001424
1425 ret = 0;
1426
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001427exit:
bellardb371dc52007-01-03 15:20:39 +00001428 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001429 return ret;
bellardb371dc52007-01-03 15:20:39 +00001430}
1431
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001432static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001433 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001434{
1435 FILE *f;
1436 uint32_t l;
1437 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001438 uint32_t size = qdict_get_int(qdict, "size");
1439 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001440 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001441 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001442
1443 f = fopen(filename, "wb");
1444 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001445 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001446 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001447 }
1448 while (size != 0) {
1449 l = sizeof(buf);
1450 if (l > size)
1451 l = size;
1452 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001453 if (fwrite(buf, 1, l, f) != l) {
1454 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1455 goto exit;
1456 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001457 fflush(f);
1458 addr += l;
1459 size -= l;
1460 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001461
1462 ret = 0;
1463
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001464exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001465 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001466 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001467}
1468
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001469static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001470{
1471 uint32_t addr;
1472 uint8_t buf[1];
1473 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001474 uint32_t start = qdict_get_int(qdict, "start");
1475 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001476
1477 sum = 0;
1478 for(addr = start; addr < (start + size); addr++) {
1479 cpu_physical_memory_rw(addr, buf, 1, 0);
1480 /* BSD sum algorithm ('sum' Unix command) */
1481 sum = (sum >> 1) | (sum << 15);
1482 sum += buf[0];
1483 }
aliguori376253e2009-03-05 23:01:23 +00001484 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001485}
1486
bellarda3a91a32004-06-04 11:06:21 +00001487typedef struct {
1488 int keycode;
1489 const char *name;
1490} KeyDef;
1491
1492static const KeyDef key_defs[] = {
1493 { 0x2a, "shift" },
1494 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001495
bellarda3a91a32004-06-04 11:06:21 +00001496 { 0x38, "alt" },
1497 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001498 { 0x64, "altgr" },
1499 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001500 { 0x1d, "ctrl" },
1501 { 0x9d, "ctrl_r" },
1502
1503 { 0xdd, "menu" },
1504
1505 { 0x01, "esc" },
1506
1507 { 0x02, "1" },
1508 { 0x03, "2" },
1509 { 0x04, "3" },
1510 { 0x05, "4" },
1511 { 0x06, "5" },
1512 { 0x07, "6" },
1513 { 0x08, "7" },
1514 { 0x09, "8" },
1515 { 0x0a, "9" },
1516 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001517 { 0x0c, "minus" },
1518 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001519 { 0x0e, "backspace" },
1520
1521 { 0x0f, "tab" },
1522 { 0x10, "q" },
1523 { 0x11, "w" },
1524 { 0x12, "e" },
1525 { 0x13, "r" },
1526 { 0x14, "t" },
1527 { 0x15, "y" },
1528 { 0x16, "u" },
1529 { 0x17, "i" },
1530 { 0x18, "o" },
1531 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001532 { 0x1a, "bracket_left" },
1533 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001534 { 0x1c, "ret" },
1535
1536 { 0x1e, "a" },
1537 { 0x1f, "s" },
1538 { 0x20, "d" },
1539 { 0x21, "f" },
1540 { 0x22, "g" },
1541 { 0x23, "h" },
1542 { 0x24, "j" },
1543 { 0x25, "k" },
1544 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001545 { 0x27, "semicolon" },
1546 { 0x28, "apostrophe" },
1547 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001548
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001549 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001550 { 0x2c, "z" },
1551 { 0x2d, "x" },
1552 { 0x2e, "c" },
1553 { 0x2f, "v" },
1554 { 0x30, "b" },
1555 { 0x31, "n" },
1556 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001557 { 0x33, "comma" },
1558 { 0x34, "dot" },
1559 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001560
balrog4d3b6f62008-02-10 16:33:14 +00001561 { 0x37, "asterisk" },
1562
bellarda3a91a32004-06-04 11:06:21 +00001563 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001564 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001565 { 0x3b, "f1" },
1566 { 0x3c, "f2" },
1567 { 0x3d, "f3" },
1568 { 0x3e, "f4" },
1569 { 0x3f, "f5" },
1570 { 0x40, "f6" },
1571 { 0x41, "f7" },
1572 { 0x42, "f8" },
1573 { 0x43, "f9" },
1574 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001575 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001576 { 0x46, "scroll_lock" },
1577
bellard64866c32006-05-07 18:03:31 +00001578 { 0xb5, "kp_divide" },
1579 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001580 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001581 { 0x4e, "kp_add" },
1582 { 0x9c, "kp_enter" },
1583 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001584 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001585
1586 { 0x52, "kp_0" },
1587 { 0x4f, "kp_1" },
1588 { 0x50, "kp_2" },
1589 { 0x51, "kp_3" },
1590 { 0x4b, "kp_4" },
1591 { 0x4c, "kp_5" },
1592 { 0x4d, "kp_6" },
1593 { 0x47, "kp_7" },
1594 { 0x48, "kp_8" },
1595 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001596
bellarda3a91a32004-06-04 11:06:21 +00001597 { 0x56, "<" },
1598
1599 { 0x57, "f11" },
1600 { 0x58, "f12" },
1601
1602 { 0xb7, "print" },
1603
1604 { 0xc7, "home" },
1605 { 0xc9, "pgup" },
1606 { 0xd1, "pgdn" },
1607 { 0xcf, "end" },
1608
1609 { 0xcb, "left" },
1610 { 0xc8, "up" },
1611 { 0xd0, "down" },
1612 { 0xcd, "right" },
1613
1614 { 0xd2, "insert" },
1615 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001616#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1617 { 0xf0, "stop" },
1618 { 0xf1, "again" },
1619 { 0xf2, "props" },
1620 { 0xf3, "undo" },
1621 { 0xf4, "front" },
1622 { 0xf5, "copy" },
1623 { 0xf6, "open" },
1624 { 0xf7, "paste" },
1625 { 0xf8, "find" },
1626 { 0xf9, "cut" },
1627 { 0xfa, "lf" },
1628 { 0xfb, "help" },
1629 { 0xfc, "meta_l" },
1630 { 0xfd, "meta_r" },
1631 { 0xfe, "compose" },
1632#endif
bellarda3a91a32004-06-04 11:06:21 +00001633 { 0, NULL },
1634};
1635
1636static int get_keycode(const char *key)
1637{
1638 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001639 char *endp;
1640 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001641
1642 for(p = key_defs; p->name != NULL; p++) {
1643 if (!strcmp(key, p->name))
1644 return p->keycode;
1645 }
bellard64866c32006-05-07 18:03:31 +00001646 if (strstart(key, "0x", NULL)) {
1647 ret = strtoul(key, &endp, 0);
1648 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1649 return ret;
1650 }
bellarda3a91a32004-06-04 11:06:21 +00001651 return -1;
1652}
1653
balrogc8256f92008-06-08 22:45:01 +00001654#define MAX_KEYCODES 16
1655static uint8_t keycodes[MAX_KEYCODES];
1656static int nb_pending_keycodes;
1657static QEMUTimer *key_timer;
1658
1659static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001660{
balrogc8256f92008-06-08 22:45:01 +00001661 int keycode;
1662
1663 while (nb_pending_keycodes > 0) {
1664 nb_pending_keycodes--;
1665 keycode = keycodes[nb_pending_keycodes];
1666 if (keycode & 0x80)
1667 kbd_put_keycode(0xe0);
1668 kbd_put_keycode(keycode | 0x80);
1669 }
1670}
1671
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001672static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001673{
balrog3401c0d2008-06-04 10:05:59 +00001674 char keyname_buf[16];
1675 char *separator;
1676 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001677 const char *string = qdict_get_str(qdict, "string");
1678 int has_hold_time = qdict_haskey(qdict, "hold_time");
1679 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001680
balrogc8256f92008-06-08 22:45:01 +00001681 if (nb_pending_keycodes > 0) {
1682 qemu_del_timer(key_timer);
1683 release_keys(NULL);
1684 }
1685 if (!has_hold_time)
1686 hold_time = 100;
1687 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001688 while (1) {
1689 separator = strchr(string, '-');
1690 keyname_len = separator ? separator - string : strlen(string);
1691 if (keyname_len > 0) {
1692 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1693 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001694 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001695 return;
bellarda3a91a32004-06-04 11:06:21 +00001696 }
balrogc8256f92008-06-08 22:45:01 +00001697 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001698 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001699 return;
1700 }
1701 keyname_buf[keyname_len] = 0;
1702 keycode = get_keycode(keyname_buf);
1703 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001704 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001705 return;
1706 }
balrogc8256f92008-06-08 22:45:01 +00001707 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001708 }
balrog3401c0d2008-06-04 10:05:59 +00001709 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001710 break;
balrog3401c0d2008-06-04 10:05:59 +00001711 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001712 }
balrogc8256f92008-06-08 22:45:01 +00001713 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001714 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001715 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001716 keycode = keycodes[i];
1717 if (keycode & 0x80)
1718 kbd_put_keycode(0xe0);
1719 kbd_put_keycode(keycode & 0x7f);
1720 }
balrogc8256f92008-06-08 22:45:01 +00001721 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001722 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001723 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001724}
1725
bellard13224a82006-07-14 22:03:35 +00001726static int mouse_button_state;
1727
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001728static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001729{
1730 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001731 const char *dx_str = qdict_get_str(qdict, "dx_str");
1732 const char *dy_str = qdict_get_str(qdict, "dy_str");
1733 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001734 dx = strtol(dx_str, NULL, 0);
1735 dy = strtol(dy_str, NULL, 0);
1736 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001737 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001738 dz = strtol(dz_str, NULL, 0);
1739 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1740}
1741
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001742static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001743{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001744 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001745 mouse_button_state = button_state;
1746 kbd_mouse_event(0, 0, 0, mouse_button_state);
1747}
1748
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001749static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001750{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001751 int size = qdict_get_int(qdict, "size");
1752 int addr = qdict_get_int(qdict, "addr");
1753 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001754 uint32_t val;
1755 int suffix;
1756
1757 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001758 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001759 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001760 addr++;
1761 }
1762 addr &= 0xffff;
1763
1764 switch(size) {
1765 default:
1766 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001767 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001768 suffix = 'b';
1769 break;
1770 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001771 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001772 suffix = 'w';
1773 break;
1774 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001775 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001776 suffix = 'l';
1777 break;
1778 }
aliguori376253e2009-03-05 23:01:23 +00001779 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1780 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001781}
bellarda3a91a32004-06-04 11:06:21 +00001782
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001783static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001784{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001785 int size = qdict_get_int(qdict, "size");
1786 int addr = qdict_get_int(qdict, "addr");
1787 int val = qdict_get_int(qdict, "val");
1788
Jan Kiszkaf1147842009-07-14 10:20:11 +02001789 addr &= IOPORTS_MASK;
1790
1791 switch (size) {
1792 default:
1793 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001794 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001795 break;
1796 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001797 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001798 break;
1799 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001800 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001801 break;
1802 }
1803}
1804
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001805static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001806{
1807 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001808 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001809
Jan Kiszka76e30d02009-07-02 00:19:02 +02001810 res = qemu_boot_set(bootdevice);
1811 if (res == 0) {
1812 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1813 } else if (res > 0) {
1814 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001815 } else {
aliguori376253e2009-03-05 23:01:23 +00001816 monitor_printf(mon, "no function defined to set boot device list for "
1817 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001818 }
1819}
1820
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001821/**
1822 * do_system_reset(): Issue a machine reset
1823 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001824static int do_system_reset(Monitor *mon, const QDict *qdict,
1825 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001826{
1827 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001828 return 0;
bellarde4f90822004-06-20 12:35:44 +00001829}
1830
Luiz Capitulino43076662009-10-07 13:41:59 -03001831/**
1832 * do_system_powerdown(): Issue a machine powerdown
1833 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001834static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1835 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001836{
1837 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001838 return 0;
bellard34751872005-07-02 14:31:34 +00001839}
1840
bellardb86bda52004-09-18 19:32:46 +00001841#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001842static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001843{
aliguori376253e2009-03-05 23:01:23 +00001844 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1845 addr,
1846 pte & mask,
1847 pte & PG_GLOBAL_MASK ? 'G' : '-',
1848 pte & PG_PSE_MASK ? 'P' : '-',
1849 pte & PG_DIRTY_MASK ? 'D' : '-',
1850 pte & PG_ACCESSED_MASK ? 'A' : '-',
1851 pte & PG_PCD_MASK ? 'C' : '-',
1852 pte & PG_PWT_MASK ? 'T' : '-',
1853 pte & PG_USER_MASK ? 'U' : '-',
1854 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001855}
1856
aliguori376253e2009-03-05 23:01:23 +00001857static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001858{
bellard6a00d602005-11-21 23:25:50 +00001859 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001860 int l1, l2;
1861 uint32_t pgd, pde, pte;
1862
bellard6a00d602005-11-21 23:25:50 +00001863 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001864
bellardb86bda52004-09-18 19:32:46 +00001865 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001866 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001867 return;
1868 }
1869 pgd = env->cr[3] & ~0xfff;
1870 for(l1 = 0; l1 < 1024; l1++) {
1871 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1872 pde = le32_to_cpu(pde);
1873 if (pde & PG_PRESENT_MASK) {
1874 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001875 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001876 } else {
1877 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001878 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001879 (uint8_t *)&pte, 4);
1880 pte = le32_to_cpu(pte);
1881 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001882 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001883 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001884 ~0xfff);
1885 }
1886 }
1887 }
1888 }
1889 }
1890}
1891
aliguori376253e2009-03-05 23:01:23 +00001892static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001893 uint32_t end, int prot)
1894{
bellard9746b152004-11-11 18:30:24 +00001895 int prot1;
1896 prot1 = *plast_prot;
1897 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001898 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001899 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1900 *pstart, end, end - *pstart,
1901 prot1 & PG_USER_MASK ? 'u' : '-',
1902 'r',
1903 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001904 }
1905 if (prot != 0)
1906 *pstart = end;
1907 else
1908 *pstart = -1;
1909 *plast_prot = prot;
1910 }
1911}
1912
aliguori376253e2009-03-05 23:01:23 +00001913static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001914{
bellard6a00d602005-11-21 23:25:50 +00001915 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001916 int l1, l2, prot, last_prot;
1917 uint32_t pgd, pde, pte, start, end;
1918
bellard6a00d602005-11-21 23:25:50 +00001919 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001920
bellardb86bda52004-09-18 19:32:46 +00001921 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001922 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001923 return;
1924 }
1925 pgd = env->cr[3] & ~0xfff;
1926 last_prot = 0;
1927 start = -1;
1928 for(l1 = 0; l1 < 1024; l1++) {
1929 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1930 pde = le32_to_cpu(pde);
1931 end = l1 << 22;
1932 if (pde & PG_PRESENT_MASK) {
1933 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1934 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001935 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001936 } else {
1937 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001938 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001939 (uint8_t *)&pte, 4);
1940 pte = le32_to_cpu(pte);
1941 end = (l1 << 22) + (l2 << 12);
1942 if (pte & PG_PRESENT_MASK) {
1943 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1944 } else {
1945 prot = 0;
1946 }
aliguori376253e2009-03-05 23:01:23 +00001947 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001948 }
1949 }
1950 } else {
1951 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001952 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001953 }
1954 }
1955}
1956#endif
1957
aurel327c664e22009-03-03 06:12:22 +00001958#if defined(TARGET_SH4)
1959
aliguori376253e2009-03-05 23:01:23 +00001960static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001961{
aliguori376253e2009-03-05 23:01:23 +00001962 monitor_printf(mon, " tlb%i:\t"
1963 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1964 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1965 "dirty=%hhu writethrough=%hhu\n",
1966 idx,
1967 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1968 tlb->v, tlb->sh, tlb->c, tlb->pr,
1969 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001970}
1971
aliguori376253e2009-03-05 23:01:23 +00001972static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001973{
1974 CPUState *env = mon_get_cpu();
1975 int i;
1976
aliguori376253e2009-03-05 23:01:23 +00001977 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001978 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001979 print_tlb (mon, i, &env->itlb[i]);
1980 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001981 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001982 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001983}
1984
1985#endif
1986
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001987static void do_info_kvm_print(Monitor *mon, const QObject *data)
1988{
1989 QDict *qdict;
1990
1991 qdict = qobject_to_qdict(data);
1992
1993 monitor_printf(mon, "kvm support: ");
1994 if (qdict_get_bool(qdict, "present")) {
1995 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1996 "enabled" : "disabled");
1997 } else {
1998 monitor_printf(mon, "not compiled\n");
1999 }
2000}
2001
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002002static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002003{
2004#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002005 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2006 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002007#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002008 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002009#endif
2010}
2011
aliguori030ea372009-04-21 22:30:47 +00002012static void do_info_numa(Monitor *mon)
2013{
aliguorib28b6232009-04-22 20:20:29 +00002014 int i;
aliguori030ea372009-04-21 22:30:47 +00002015 CPUState *env;
2016
2017 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2018 for (i = 0; i < nb_numa_nodes; i++) {
2019 monitor_printf(mon, "node %d cpus:", i);
2020 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2021 if (env->numa_node == i) {
2022 monitor_printf(mon, " %d", env->cpu_index);
2023 }
2024 }
2025 monitor_printf(mon, "\n");
2026 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2027 node_mem[i] >> 20);
2028 }
2029}
2030
bellard5f1ce942006-02-08 22:40:15 +00002031#ifdef CONFIG_PROFILER
2032
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002033int64_t qemu_time;
2034int64_t dev_time;
2035
aliguori376253e2009-03-05 23:01:23 +00002036static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002037{
2038 int64_t total;
2039 total = qemu_time;
2040 if (total == 0)
2041 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002042 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002043 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002044 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002045 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002046 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002047 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002048}
2049#else
aliguori376253e2009-03-05 23:01:23 +00002050static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002051{
aliguori376253e2009-03-05 23:01:23 +00002052 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002053}
2054#endif
2055
bellardec36b692006-07-16 18:57:03 +00002056/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002057static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002058
aliguori376253e2009-03-05 23:01:23 +00002059static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002060{
2061 int i;
2062 CaptureState *s;
2063
2064 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002065 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002066 s->ops.info (s->opaque);
2067 }
2068}
2069
Blue Swirl23130862009-06-06 08:22:04 +00002070#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002071static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002072{
2073 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002074 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002075 CaptureState *s;
2076
2077 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2078 if (i == n) {
2079 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002080 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002081 qemu_free (s);
2082 return;
2083 }
2084 }
2085}
2086
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002087static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002088{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002089 const char *path = qdict_get_str(qdict, "path");
2090 int has_freq = qdict_haskey(qdict, "freq");
2091 int freq = qdict_get_try_int(qdict, "freq", -1);
2092 int has_bits = qdict_haskey(qdict, "bits");
2093 int bits = qdict_get_try_int(qdict, "bits", -1);
2094 int has_channels = qdict_haskey(qdict, "nchannels");
2095 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002096 CaptureState *s;
2097
2098 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002099
2100 freq = has_freq ? freq : 44100;
2101 bits = has_bits ? bits : 16;
2102 nchannels = has_channels ? nchannels : 2;
2103
2104 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002105 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002106 qemu_free (s);
2107 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002108 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002109}
2110#endif
2111
aurel32dc1c0b72008-04-27 23:52:12 +00002112#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002113static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002114{
2115 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002116 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002117
2118 for (env = first_cpu; env != NULL; env = env->next_cpu)
2119 if (env->cpu_index == cpu_index) {
2120 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2121 break;
2122 }
2123}
2124#endif
2125
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002126static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002127{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002128 QDict *qdict;
2129
2130 qdict = qobject_to_qdict(data);
2131
2132 monitor_printf(mon, "VM status: ");
2133 if (qdict_get_bool(qdict, "running")) {
2134 monitor_printf(mon, "running");
2135 if (qdict_get_bool(qdict, "singlestep")) {
2136 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002137 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002138 } else {
2139 monitor_printf(mon, "paused");
2140 }
2141
2142 monitor_printf(mon, "\n");
2143}
2144
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002145static void do_info_status(Monitor *mon, QObject **ret_data)
2146{
2147 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2148 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002149}
2150
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002151static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002152{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002153 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002154
aliguori76655d62009-03-06 20:27:37 +00002155 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002156 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002157 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002158 return acl;
2159}
aliguori76655d62009-03-06 20:27:37 +00002160
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002161static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002162{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002163 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002164 qemu_acl *acl = find_acl(mon, aclname);
2165 qemu_acl_entry *entry;
2166 int i = 0;
2167
2168 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002169 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002170 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002171 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002172 i++;
2173 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002174 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002175 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002176 }
2177}
2178
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002179static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002181 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002182 qemu_acl *acl = find_acl(mon, aclname);
2183
2184 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002185 qemu_acl_reset(acl);
2186 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002187 }
2188}
aliguori76655d62009-03-06 20:27:37 +00002189
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002190static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002191{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002192 const char *aclname = qdict_get_str(qdict, "aclname");
2193 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002194 qemu_acl *acl = find_acl(mon, aclname);
2195
2196 if (acl) {
2197 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002198 acl->defaultDeny = 0;
2199 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002200 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002201 acl->defaultDeny = 1;
2202 monitor_printf(mon, "acl: policy set to 'deny'\n");
2203 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002204 monitor_printf(mon, "acl: unknown policy '%s', "
2205 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002206 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002207 }
2208}
aliguori76655d62009-03-06 20:27:37 +00002209
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002210static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002211{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002212 const char *aclname = qdict_get_str(qdict, "aclname");
2213 const char *match = qdict_get_str(qdict, "match");
2214 const char *policy = qdict_get_str(qdict, "policy");
2215 int has_index = qdict_haskey(qdict, "index");
2216 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002217 qemu_acl *acl = find_acl(mon, aclname);
2218 int deny, ret;
2219
2220 if (acl) {
2221 if (strcmp(policy, "allow") == 0) {
2222 deny = 0;
2223 } else if (strcmp(policy, "deny") == 0) {
2224 deny = 1;
2225 } else {
2226 monitor_printf(mon, "acl: unknown policy '%s', "
2227 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002228 return;
2229 }
aliguori28a76be2009-03-06 20:27:40 +00002230 if (has_index)
2231 ret = qemu_acl_insert(acl, deny, match, index);
2232 else
2233 ret = qemu_acl_append(acl, deny, match);
2234 if (ret < 0)
2235 monitor_printf(mon, "acl: unable to add acl entry\n");
2236 else
2237 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002238 }
2239}
aliguori76655d62009-03-06 20:27:37 +00002240
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002241static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002242{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002243 const char *aclname = qdict_get_str(qdict, "aclname");
2244 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002245 qemu_acl *acl = find_acl(mon, aclname);
2246 int ret;
aliguori76655d62009-03-06 20:27:37 +00002247
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002248 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002249 ret = qemu_acl_remove(acl, match);
2250 if (ret < 0)
2251 monitor_printf(mon, "acl: no matching acl entry\n");
2252 else
2253 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002254 }
2255}
2256
Huang Ying79c4f6b2009-06-23 10:05:14 +08002257#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002258static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002259{
2260 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002261 int cpu_index = qdict_get_int(qdict, "cpu_index");
2262 int bank = qdict_get_int(qdict, "bank");
2263 uint64_t status = qdict_get_int(qdict, "status");
2264 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2265 uint64_t addr = qdict_get_int(qdict, "addr");
2266 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002267
2268 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2269 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2270 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2271 break;
2272 }
2273}
2274#endif
2275
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002276static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002277{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002278 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002279 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002280 int fd;
2281
2282 fd = qemu_chr_get_msgfd(mon->chr);
2283 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002284 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002285 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002286 }
2287
2288 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002289 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2290 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002291 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002292 }
2293
Blue Swirl72cf2d42009-09-12 07:36:22 +00002294 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002295 if (strcmp(monfd->name, fdname) != 0) {
2296 continue;
2297 }
2298
2299 close(monfd->fd);
2300 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002301 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002302 }
2303
Anthony Liguoric227f092009-10-01 16:12:16 -05002304 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002305 monfd->name = qemu_strdup(fdname);
2306 monfd->fd = fd;
2307
Blue Swirl72cf2d42009-09-12 07:36:22 +00002308 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002309 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002310}
2311
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002312static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002313{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002314 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002315 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002316
Blue Swirl72cf2d42009-09-12 07:36:22 +00002317 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002318 if (strcmp(monfd->name, fdname) != 0) {
2319 continue;
2320 }
2321
Blue Swirl72cf2d42009-09-12 07:36:22 +00002322 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002323 close(monfd->fd);
2324 qemu_free(monfd->name);
2325 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002326 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002327 }
2328
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002329 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002330 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002331}
2332
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002333static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002334{
2335 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002336 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002337
2338 vm_stop(0);
2339
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002340 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002341 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002342 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002343}
2344
Mark McLoughlin7768e042009-07-22 09:11:41 +01002345int monitor_get_fd(Monitor *mon, const char *fdname)
2346{
Anthony Liguoric227f092009-10-01 16:12:16 -05002347 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002348
Blue Swirl72cf2d42009-09-12 07:36:22 +00002349 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002350 int fd;
2351
2352 if (strcmp(monfd->name, fdname) != 0) {
2353 continue;
2354 }
2355
2356 fd = monfd->fd;
2357
2358 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002359 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002360 qemu_free(monfd->name);
2361 qemu_free(monfd);
2362
2363 return fd;
2364 }
2365
2366 return -1;
2367}
2368
Anthony Liguoric227f092009-10-01 16:12:16 -05002369static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002370#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002371 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002372};
2373
Blue Swirl23130862009-06-06 08:22:04 +00002374/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002375static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002376 {
2377 .name = "version",
2378 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002379 .params = "",
2380 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002381 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002382 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002383 },
2384 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002385 .name = "commands",
2386 .args_type = "",
2387 .params = "",
2388 .help = "list QMP available commands",
2389 .user_print = monitor_user_noop,
2390 .mhandler.info_new = do_info_commands,
2391 },
2392 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002393 .name = "network",
2394 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002395 .params = "",
2396 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002397 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 },
2399 {
2400 .name = "chardev",
2401 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002402 .params = "",
2403 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002404 .user_print = qemu_chr_info_print,
2405 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002406 },
2407 {
2408 .name = "block",
2409 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002410 .params = "",
2411 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002412 .user_print = bdrv_info_print,
2413 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002414 },
2415 {
2416 .name = "blockstats",
2417 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002418 .params = "",
2419 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002420 .user_print = bdrv_stats_print,
2421 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002422 },
2423 {
2424 .name = "registers",
2425 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002426 .params = "",
2427 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002428 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002429 },
2430 {
2431 .name = "cpus",
2432 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002433 .params = "",
2434 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002435 .user_print = monitor_print_cpus,
2436 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002437 },
2438 {
2439 .name = "history",
2440 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002441 .params = "",
2442 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002443 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002444 },
2445 {
2446 .name = "irq",
2447 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002448 .params = "",
2449 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002450 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002451 },
2452 {
2453 .name = "pic",
2454 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002455 .params = "",
2456 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002457 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002458 },
2459 {
2460 .name = "pci",
2461 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002462 .params = "",
2463 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002464 .user_print = do_pci_info_print,
2465 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002466 },
aurel327c664e22009-03-03 06:12:22 +00002467#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002468 {
2469 .name = "tlb",
2470 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 .params = "",
2472 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002473 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002474 },
aurel327c664e22009-03-03 06:12:22 +00002475#endif
2476#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002477 {
2478 .name = "mem",
2479 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002480 .params = "",
2481 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002482 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002483 },
bellardb86bda52004-09-18 19:32:46 +00002484#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002485 {
2486 .name = "jit",
2487 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002488 .params = "",
2489 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002490 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002491 },
2492 {
2493 .name = "kvm",
2494 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002495 .params = "",
2496 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002497 .user_print = do_info_kvm_print,
2498 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002499 },
2500 {
2501 .name = "numa",
2502 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002503 .params = "",
2504 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002505 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002506 },
2507 {
2508 .name = "usb",
2509 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002510 .params = "",
2511 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002512 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002513 },
2514 {
2515 .name = "usbhost",
2516 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 .params = "",
2518 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002519 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002520 },
2521 {
2522 .name = "profile",
2523 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 .params = "",
2525 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002526 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 },
2528 {
2529 .name = "capture",
2530 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002531 .params = "",
2532 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002533 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002534 },
2535 {
2536 .name = "snapshots",
2537 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 .params = "",
2539 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002540 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 },
2542 {
2543 .name = "status",
2544 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 .params = "",
2546 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002547 .user_print = do_info_status_print,
2548 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002549 },
2550 {
2551 .name = "pcmcia",
2552 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002553 .params = "",
2554 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002555 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002556 },
2557 {
2558 .name = "mice",
2559 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002560 .params = "",
2561 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002562 .user_print = do_info_mice_print,
2563 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002564 },
2565 {
2566 .name = "vnc",
2567 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 .params = "",
2569 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002570 .user_print = do_info_vnc_print,
2571 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 },
2573 {
2574 .name = "name",
2575 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002576 .params = "",
2577 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002578 .user_print = do_info_name_print,
2579 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 },
2581 {
2582 .name = "uuid",
2583 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002584 .params = "",
2585 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002586 .user_print = do_info_uuid_print,
2587 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002588 },
j_mayer76a66252007-03-07 08:32:30 +00002589#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 {
2591 .name = "cpustats",
2592 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002593 .params = "",
2594 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002595 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 },
j_mayer76a66252007-03-07 08:32:30 +00002597#endif
blueswir131a60e22007-10-26 18:42:59 +00002598#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 {
2600 .name = "usernet",
2601 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 .params = "",
2603 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002604 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 },
blueswir131a60e22007-10-26 18:42:59 +00002606#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002607 {
2608 .name = "migrate",
2609 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 .params = "",
2611 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002612 .user_print = do_info_migrate_print,
2613 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 },
2615 {
2616 .name = "balloon",
2617 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 .params = "",
2619 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002620 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002621 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002622 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002623 },
2624 {
2625 .name = "qtree",
2626 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002627 .params = "",
2628 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002629 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002630 },
2631 {
2632 .name = "qdm",
2633 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002634 .params = "",
2635 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002636 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002637 },
2638 {
2639 .name = "roms",
2640 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002641 .params = "",
2642 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002643 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302645#if defined(CONFIG_SIMPLE_TRACE)
2646 {
2647 .name = "trace",
2648 .args_type = "",
2649 .params = "",
2650 .help = "show current contents of trace buffer",
2651 .mhandler.info = do_info_trace,
2652 },
2653 {
2654 .name = "trace-events",
2655 .args_type = "",
2656 .params = "",
2657 .help = "show available trace-events & their state",
2658 .mhandler.info = do_info_trace_events,
2659 },
2660#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002661 {
2662 .name = NULL,
2663 },
bellard9dc39cb2004-03-14 21:38:27 +00002664};
2665
bellard9307c4c2004-04-04 12:57:25 +00002666/*******************************************************************/
2667
2668static const char *pch;
2669static jmp_buf expr_env;
2670
bellard92a31b12005-02-10 22:00:52 +00002671#define MD_TLONG 0
2672#define MD_I32 1
2673
bellard9307c4c2004-04-04 12:57:25 +00002674typedef struct MonitorDef {
2675 const char *name;
2676 int offset;
blueswir18662d652008-10-02 18:32:44 +00002677 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002678 int type;
bellard9307c4c2004-04-04 12:57:25 +00002679} MonitorDef;
2680
bellard57206fd2004-04-25 18:54:52 +00002681#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002682static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002683{
bellard6a00d602005-11-21 23:25:50 +00002684 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002685 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002686}
2687#endif
2688
bellarda541f292004-04-12 20:39:29 +00002689#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002690static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002691{
bellard6a00d602005-11-21 23:25:50 +00002692 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002693 unsigned int u;
2694 int i;
2695
2696 u = 0;
2697 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002698 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002699
2700 return u;
2701}
2702
blueswir18662d652008-10-02 18:32:44 +00002703static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002704{
bellard6a00d602005-11-21 23:25:50 +00002705 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002706 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002707}
2708
blueswir18662d652008-10-02 18:32:44 +00002709static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002710{
bellard6a00d602005-11-21 23:25:50 +00002711 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002712 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002713}
bellard9fddaa02004-05-21 12:59:32 +00002714
blueswir18662d652008-10-02 18:32:44 +00002715static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002716{
bellard6a00d602005-11-21 23:25:50 +00002717 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002718 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002719}
2720
blueswir18662d652008-10-02 18:32:44 +00002721static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002722{
bellard6a00d602005-11-21 23:25:50 +00002723 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002724 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002725}
2726
blueswir18662d652008-10-02 18:32:44 +00002727static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002728{
bellard6a00d602005-11-21 23:25:50 +00002729 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002730 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002731}
bellarda541f292004-04-12 20:39:29 +00002732#endif
2733
bellarde95c8d52004-09-30 22:22:08 +00002734#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002735#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002736static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002737{
bellard6a00d602005-11-21 23:25:50 +00002738 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002739
2740 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002741}
bellard7b936c02005-10-30 17:05:13 +00002742#endif
bellarde95c8d52004-09-30 22:22:08 +00002743
blueswir18662d652008-10-02 18:32:44 +00002744static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002745{
bellard6a00d602005-11-21 23:25:50 +00002746 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002747 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002748}
2749#endif
2750
blueswir18662d652008-10-02 18:32:44 +00002751static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002752#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002753
2754#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002755 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002756 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002757 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002758
bellard9307c4c2004-04-04 12:57:25 +00002759 { "eax", offsetof(CPUState, regs[0]) },
2760 { "ecx", offsetof(CPUState, regs[1]) },
2761 { "edx", offsetof(CPUState, regs[2]) },
2762 { "ebx", offsetof(CPUState, regs[3]) },
2763 { "esp|sp", offsetof(CPUState, regs[4]) },
2764 { "ebp|fp", offsetof(CPUState, regs[5]) },
2765 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002766 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002767#ifdef TARGET_X86_64
2768 { "r8", offsetof(CPUState, regs[8]) },
2769 { "r9", offsetof(CPUState, regs[9]) },
2770 { "r10", offsetof(CPUState, regs[10]) },
2771 { "r11", offsetof(CPUState, regs[11]) },
2772 { "r12", offsetof(CPUState, regs[12]) },
2773 { "r13", offsetof(CPUState, regs[13]) },
2774 { "r14", offsetof(CPUState, regs[14]) },
2775 { "r15", offsetof(CPUState, regs[15]) },
2776#endif
bellard9307c4c2004-04-04 12:57:25 +00002777 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002778 { "eip", offsetof(CPUState, eip) },
2779 SEG("cs", R_CS)
2780 SEG("ds", R_DS)
2781 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002782 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002783 SEG("fs", R_FS)
2784 SEG("gs", R_GS)
2785 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002786#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002787 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002788 { "r0", offsetof(CPUState, gpr[0]) },
2789 { "r1", offsetof(CPUState, gpr[1]) },
2790 { "r2", offsetof(CPUState, gpr[2]) },
2791 { "r3", offsetof(CPUState, gpr[3]) },
2792 { "r4", offsetof(CPUState, gpr[4]) },
2793 { "r5", offsetof(CPUState, gpr[5]) },
2794 { "r6", offsetof(CPUState, gpr[6]) },
2795 { "r7", offsetof(CPUState, gpr[7]) },
2796 { "r8", offsetof(CPUState, gpr[8]) },
2797 { "r9", offsetof(CPUState, gpr[9]) },
2798 { "r10", offsetof(CPUState, gpr[10]) },
2799 { "r11", offsetof(CPUState, gpr[11]) },
2800 { "r12", offsetof(CPUState, gpr[12]) },
2801 { "r13", offsetof(CPUState, gpr[13]) },
2802 { "r14", offsetof(CPUState, gpr[14]) },
2803 { "r15", offsetof(CPUState, gpr[15]) },
2804 { "r16", offsetof(CPUState, gpr[16]) },
2805 { "r17", offsetof(CPUState, gpr[17]) },
2806 { "r18", offsetof(CPUState, gpr[18]) },
2807 { "r19", offsetof(CPUState, gpr[19]) },
2808 { "r20", offsetof(CPUState, gpr[20]) },
2809 { "r21", offsetof(CPUState, gpr[21]) },
2810 { "r22", offsetof(CPUState, gpr[22]) },
2811 { "r23", offsetof(CPUState, gpr[23]) },
2812 { "r24", offsetof(CPUState, gpr[24]) },
2813 { "r25", offsetof(CPUState, gpr[25]) },
2814 { "r26", offsetof(CPUState, gpr[26]) },
2815 { "r27", offsetof(CPUState, gpr[27]) },
2816 { "r28", offsetof(CPUState, gpr[28]) },
2817 { "r29", offsetof(CPUState, gpr[29]) },
2818 { "r30", offsetof(CPUState, gpr[30]) },
2819 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002820 /* Floating point registers */
2821 { "f0", offsetof(CPUState, fpr[0]) },
2822 { "f1", offsetof(CPUState, fpr[1]) },
2823 { "f2", offsetof(CPUState, fpr[2]) },
2824 { "f3", offsetof(CPUState, fpr[3]) },
2825 { "f4", offsetof(CPUState, fpr[4]) },
2826 { "f5", offsetof(CPUState, fpr[5]) },
2827 { "f6", offsetof(CPUState, fpr[6]) },
2828 { "f7", offsetof(CPUState, fpr[7]) },
2829 { "f8", offsetof(CPUState, fpr[8]) },
2830 { "f9", offsetof(CPUState, fpr[9]) },
2831 { "f10", offsetof(CPUState, fpr[10]) },
2832 { "f11", offsetof(CPUState, fpr[11]) },
2833 { "f12", offsetof(CPUState, fpr[12]) },
2834 { "f13", offsetof(CPUState, fpr[13]) },
2835 { "f14", offsetof(CPUState, fpr[14]) },
2836 { "f15", offsetof(CPUState, fpr[15]) },
2837 { "f16", offsetof(CPUState, fpr[16]) },
2838 { "f17", offsetof(CPUState, fpr[17]) },
2839 { "f18", offsetof(CPUState, fpr[18]) },
2840 { "f19", offsetof(CPUState, fpr[19]) },
2841 { "f20", offsetof(CPUState, fpr[20]) },
2842 { "f21", offsetof(CPUState, fpr[21]) },
2843 { "f22", offsetof(CPUState, fpr[22]) },
2844 { "f23", offsetof(CPUState, fpr[23]) },
2845 { "f24", offsetof(CPUState, fpr[24]) },
2846 { "f25", offsetof(CPUState, fpr[25]) },
2847 { "f26", offsetof(CPUState, fpr[26]) },
2848 { "f27", offsetof(CPUState, fpr[27]) },
2849 { "f28", offsetof(CPUState, fpr[28]) },
2850 { "f29", offsetof(CPUState, fpr[29]) },
2851 { "f30", offsetof(CPUState, fpr[30]) },
2852 { "f31", offsetof(CPUState, fpr[31]) },
2853 { "fpscr", offsetof(CPUState, fpscr) },
2854 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002855 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002856 { "lr", offsetof(CPUState, lr) },
2857 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002858 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002859 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002860 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002861 { "msr", 0, &monitor_get_msr, },
2862 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002863 { "tbu", 0, &monitor_get_tbu, },
2864 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002865#if defined(TARGET_PPC64)
2866 /* Address space register */
2867 { "asr", offsetof(CPUState, asr) },
2868#endif
2869 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002870 { "sdr1", offsetof(CPUState, sdr1) },
2871 { "sr0", offsetof(CPUState, sr[0]) },
2872 { "sr1", offsetof(CPUState, sr[1]) },
2873 { "sr2", offsetof(CPUState, sr[2]) },
2874 { "sr3", offsetof(CPUState, sr[3]) },
2875 { "sr4", offsetof(CPUState, sr[4]) },
2876 { "sr5", offsetof(CPUState, sr[5]) },
2877 { "sr6", offsetof(CPUState, sr[6]) },
2878 { "sr7", offsetof(CPUState, sr[7]) },
2879 { "sr8", offsetof(CPUState, sr[8]) },
2880 { "sr9", offsetof(CPUState, sr[9]) },
2881 { "sr10", offsetof(CPUState, sr[10]) },
2882 { "sr11", offsetof(CPUState, sr[11]) },
2883 { "sr12", offsetof(CPUState, sr[12]) },
2884 { "sr13", offsetof(CPUState, sr[13]) },
2885 { "sr14", offsetof(CPUState, sr[14]) },
2886 { "sr15", offsetof(CPUState, sr[15]) },
2887 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002888#elif defined(TARGET_SPARC)
2889 { "g0", offsetof(CPUState, gregs[0]) },
2890 { "g1", offsetof(CPUState, gregs[1]) },
2891 { "g2", offsetof(CPUState, gregs[2]) },
2892 { "g3", offsetof(CPUState, gregs[3]) },
2893 { "g4", offsetof(CPUState, gregs[4]) },
2894 { "g5", offsetof(CPUState, gregs[5]) },
2895 { "g6", offsetof(CPUState, gregs[6]) },
2896 { "g7", offsetof(CPUState, gregs[7]) },
2897 { "o0", 0, monitor_get_reg },
2898 { "o1", 1, monitor_get_reg },
2899 { "o2", 2, monitor_get_reg },
2900 { "o3", 3, monitor_get_reg },
2901 { "o4", 4, monitor_get_reg },
2902 { "o5", 5, monitor_get_reg },
2903 { "o6", 6, monitor_get_reg },
2904 { "o7", 7, monitor_get_reg },
2905 { "l0", 8, monitor_get_reg },
2906 { "l1", 9, monitor_get_reg },
2907 { "l2", 10, monitor_get_reg },
2908 { "l3", 11, monitor_get_reg },
2909 { "l4", 12, monitor_get_reg },
2910 { "l5", 13, monitor_get_reg },
2911 { "l6", 14, monitor_get_reg },
2912 { "l7", 15, monitor_get_reg },
2913 { "i0", 16, monitor_get_reg },
2914 { "i1", 17, monitor_get_reg },
2915 { "i2", 18, monitor_get_reg },
2916 { "i3", 19, monitor_get_reg },
2917 { "i4", 20, monitor_get_reg },
2918 { "i5", 21, monitor_get_reg },
2919 { "i6", 22, monitor_get_reg },
2920 { "i7", 23, monitor_get_reg },
2921 { "pc", offsetof(CPUState, pc) },
2922 { "npc", offsetof(CPUState, npc) },
2923 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002924#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002925 { "psr", 0, &monitor_get_psr, },
2926 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002927#endif
bellarde95c8d52004-09-30 22:22:08 +00002928 { "tbr", offsetof(CPUState, tbr) },
2929 { "fsr", offsetof(CPUState, fsr) },
2930 { "f0", offsetof(CPUState, fpr[0]) },
2931 { "f1", offsetof(CPUState, fpr[1]) },
2932 { "f2", offsetof(CPUState, fpr[2]) },
2933 { "f3", offsetof(CPUState, fpr[3]) },
2934 { "f4", offsetof(CPUState, fpr[4]) },
2935 { "f5", offsetof(CPUState, fpr[5]) },
2936 { "f6", offsetof(CPUState, fpr[6]) },
2937 { "f7", offsetof(CPUState, fpr[7]) },
2938 { "f8", offsetof(CPUState, fpr[8]) },
2939 { "f9", offsetof(CPUState, fpr[9]) },
2940 { "f10", offsetof(CPUState, fpr[10]) },
2941 { "f11", offsetof(CPUState, fpr[11]) },
2942 { "f12", offsetof(CPUState, fpr[12]) },
2943 { "f13", offsetof(CPUState, fpr[13]) },
2944 { "f14", offsetof(CPUState, fpr[14]) },
2945 { "f15", offsetof(CPUState, fpr[15]) },
2946 { "f16", offsetof(CPUState, fpr[16]) },
2947 { "f17", offsetof(CPUState, fpr[17]) },
2948 { "f18", offsetof(CPUState, fpr[18]) },
2949 { "f19", offsetof(CPUState, fpr[19]) },
2950 { "f20", offsetof(CPUState, fpr[20]) },
2951 { "f21", offsetof(CPUState, fpr[21]) },
2952 { "f22", offsetof(CPUState, fpr[22]) },
2953 { "f23", offsetof(CPUState, fpr[23]) },
2954 { "f24", offsetof(CPUState, fpr[24]) },
2955 { "f25", offsetof(CPUState, fpr[25]) },
2956 { "f26", offsetof(CPUState, fpr[26]) },
2957 { "f27", offsetof(CPUState, fpr[27]) },
2958 { "f28", offsetof(CPUState, fpr[28]) },
2959 { "f29", offsetof(CPUState, fpr[29]) },
2960 { "f30", offsetof(CPUState, fpr[30]) },
2961 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002962#ifdef TARGET_SPARC64
2963 { "f32", offsetof(CPUState, fpr[32]) },
2964 { "f34", offsetof(CPUState, fpr[34]) },
2965 { "f36", offsetof(CPUState, fpr[36]) },
2966 { "f38", offsetof(CPUState, fpr[38]) },
2967 { "f40", offsetof(CPUState, fpr[40]) },
2968 { "f42", offsetof(CPUState, fpr[42]) },
2969 { "f44", offsetof(CPUState, fpr[44]) },
2970 { "f46", offsetof(CPUState, fpr[46]) },
2971 { "f48", offsetof(CPUState, fpr[48]) },
2972 { "f50", offsetof(CPUState, fpr[50]) },
2973 { "f52", offsetof(CPUState, fpr[52]) },
2974 { "f54", offsetof(CPUState, fpr[54]) },
2975 { "f56", offsetof(CPUState, fpr[56]) },
2976 { "f58", offsetof(CPUState, fpr[58]) },
2977 { "f60", offsetof(CPUState, fpr[60]) },
2978 { "f62", offsetof(CPUState, fpr[62]) },
2979 { "asi", offsetof(CPUState, asi) },
2980 { "pstate", offsetof(CPUState, pstate) },
2981 { "cansave", offsetof(CPUState, cansave) },
2982 { "canrestore", offsetof(CPUState, canrestore) },
2983 { "otherwin", offsetof(CPUState, otherwin) },
2984 { "wstate", offsetof(CPUState, wstate) },
2985 { "cleanwin", offsetof(CPUState, cleanwin) },
2986 { "fprs", offsetof(CPUState, fprs) },
2987#endif
bellard9307c4c2004-04-04 12:57:25 +00002988#endif
2989 { NULL },
2990};
2991
aliguori376253e2009-03-05 23:01:23 +00002992static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002993{
aliguori376253e2009-03-05 23:01:23 +00002994 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002995 longjmp(expr_env, 1);
2996}
2997
Markus Armbruster09b94182010-01-20 13:07:30 +01002998/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002999static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003000{
blueswir18662d652008-10-02 18:32:44 +00003001 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003002 void *ptr;
3003
bellard9307c4c2004-04-04 12:57:25 +00003004 for(md = monitor_defs; md->name != NULL; md++) {
3005 if (compare_cmd(name, md->name)) {
3006 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003007 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003008 } else {
bellard6a00d602005-11-21 23:25:50 +00003009 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003010 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003011 switch(md->type) {
3012 case MD_I32:
3013 *pval = *(int32_t *)ptr;
3014 break;
3015 case MD_TLONG:
3016 *pval = *(target_long *)ptr;
3017 break;
3018 default:
3019 *pval = 0;
3020 break;
3021 }
bellard9307c4c2004-04-04 12:57:25 +00003022 }
3023 return 0;
3024 }
3025 }
3026 return -1;
3027}
3028
3029static void next(void)
3030{
Blue Swirl660f11b2009-07-31 21:16:51 +00003031 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003032 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003033 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003034 pch++;
3035 }
3036}
3037
aliguori376253e2009-03-05 23:01:23 +00003038static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003039
aliguori376253e2009-03-05 23:01:23 +00003040static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003041{
blueswir1c2efc952007-09-25 17:28:42 +00003042 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003043 char *p;
bellard6a00d602005-11-21 23:25:50 +00003044 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003045
3046 switch(*pch) {
3047 case '+':
3048 next();
aliguori376253e2009-03-05 23:01:23 +00003049 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003050 break;
3051 case '-':
3052 next();
aliguori376253e2009-03-05 23:01:23 +00003053 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003054 break;
3055 case '~':
3056 next();
aliguori376253e2009-03-05 23:01:23 +00003057 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003058 break;
3059 case '(':
3060 next();
aliguori376253e2009-03-05 23:01:23 +00003061 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003062 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003063 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003064 }
3065 next();
3066 break;
bellard81d09122004-07-14 17:21:37 +00003067 case '\'':
3068 pch++;
3069 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003070 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003071 n = *pch;
3072 pch++;
3073 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003074 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003075 next();
3076 break;
bellard9307c4c2004-04-04 12:57:25 +00003077 case '$':
3078 {
3079 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003080 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003081
bellard9307c4c2004-04-04 12:57:25 +00003082 pch++;
3083 q = buf;
3084 while ((*pch >= 'a' && *pch <= 'z') ||
3085 (*pch >= 'A' && *pch <= 'Z') ||
3086 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003087 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003088 if ((q - buf) < sizeof(buf) - 1)
3089 *q++ = *pch;
3090 pch++;
3091 }
blueswir1cd390082008-11-16 13:53:32 +00003092 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003093 pch++;
3094 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003095 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003096 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003097 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003098 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003099 }
3100 break;
3101 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003102 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003103 n = 0;
3104 break;
3105 default:
blueswir17743e582007-09-24 18:39:04 +00003106#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003107 n = strtoull(pch, &p, 0);
3108#else
bellard9307c4c2004-04-04 12:57:25 +00003109 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003110#endif
bellard9307c4c2004-04-04 12:57:25 +00003111 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003112 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003113 }
3114 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003115 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003116 pch++;
3117 break;
3118 }
3119 return n;
3120}
3121
3122
aliguori376253e2009-03-05 23:01:23 +00003123static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003124{
blueswir1c2efc952007-09-25 17:28:42 +00003125 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003126 int op;
ths3b46e622007-09-17 08:09:54 +00003127
aliguori376253e2009-03-05 23:01:23 +00003128 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003129 for(;;) {
3130 op = *pch;
3131 if (op != '*' && op != '/' && op != '%')
3132 break;
3133 next();
aliguori376253e2009-03-05 23:01:23 +00003134 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003135 switch(op) {
3136 default:
3137 case '*':
3138 val *= val2;
3139 break;
3140 case '/':
3141 case '%':
ths5fafdf22007-09-16 21:08:06 +00003142 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003143 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003144 if (op == '/')
3145 val /= val2;
3146 else
3147 val %= val2;
3148 break;
3149 }
3150 }
3151 return val;
3152}
3153
aliguori376253e2009-03-05 23:01:23 +00003154static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003155{
blueswir1c2efc952007-09-25 17:28:42 +00003156 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003157 int op;
bellard9307c4c2004-04-04 12:57:25 +00003158
aliguori376253e2009-03-05 23:01:23 +00003159 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003160 for(;;) {
3161 op = *pch;
3162 if (op != '&' && op != '|' && op != '^')
3163 break;
3164 next();
aliguori376253e2009-03-05 23:01:23 +00003165 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003166 switch(op) {
3167 default:
3168 case '&':
3169 val &= val2;
3170 break;
3171 case '|':
3172 val |= val2;
3173 break;
3174 case '^':
3175 val ^= val2;
3176 break;
3177 }
3178 }
3179 return val;
3180}
3181
aliguori376253e2009-03-05 23:01:23 +00003182static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003183{
blueswir1c2efc952007-09-25 17:28:42 +00003184 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003185 int op;
bellard9307c4c2004-04-04 12:57:25 +00003186
aliguori376253e2009-03-05 23:01:23 +00003187 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003188 for(;;) {
3189 op = *pch;
3190 if (op != '+' && op != '-')
3191 break;
3192 next();
aliguori376253e2009-03-05 23:01:23 +00003193 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003194 if (op == '+')
3195 val += val2;
3196 else
3197 val -= val2;
3198 }
3199 return val;
3200}
3201
aliguori376253e2009-03-05 23:01:23 +00003202static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003203{
3204 pch = *pp;
3205 if (setjmp(expr_env)) {
3206 *pp = pch;
3207 return -1;
3208 }
blueswir1cd390082008-11-16 13:53:32 +00003209 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003210 pch++;
aliguori376253e2009-03-05 23:01:23 +00003211 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003212 *pp = pch;
3213 return 0;
3214}
3215
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003216static int get_double(Monitor *mon, double *pval, const char **pp)
3217{
3218 const char *p = *pp;
3219 char *tailp;
3220 double d;
3221
3222 d = strtod(p, &tailp);
3223 if (tailp == p) {
3224 monitor_printf(mon, "Number expected\n");
3225 return -1;
3226 }
3227 if (d != d || d - d != 0) {
3228 /* NaN or infinity */
3229 monitor_printf(mon, "Bad number\n");
3230 return -1;
3231 }
3232 *pval = d;
3233 *pp = tailp;
3234 return 0;
3235}
3236
bellard9307c4c2004-04-04 12:57:25 +00003237static int get_str(char *buf, int buf_size, const char **pp)
3238{
3239 const char *p;
3240 char *q;
3241 int c;
3242
bellard81d09122004-07-14 17:21:37 +00003243 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003244 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003245 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003246 p++;
3247 if (*p == '\0') {
3248 fail:
bellard81d09122004-07-14 17:21:37 +00003249 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003250 *pp = p;
3251 return -1;
3252 }
bellard9307c4c2004-04-04 12:57:25 +00003253 if (*p == '\"') {
3254 p++;
3255 while (*p != '\0' && *p != '\"') {
3256 if (*p == '\\') {
3257 p++;
3258 c = *p++;
3259 switch(c) {
3260 case 'n':
3261 c = '\n';
3262 break;
3263 case 'r':
3264 c = '\r';
3265 break;
3266 case '\\':
3267 case '\'':
3268 case '\"':
3269 break;
3270 default:
3271 qemu_printf("unsupported escape code: '\\%c'\n", c);
3272 goto fail;
3273 }
3274 if ((q - buf) < buf_size - 1) {
3275 *q++ = c;
3276 }
3277 } else {
3278 if ((q - buf) < buf_size - 1) {
3279 *q++ = *p;
3280 }
3281 p++;
3282 }
3283 }
3284 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003285 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003286 goto fail;
3287 }
3288 p++;
3289 } else {
blueswir1cd390082008-11-16 13:53:32 +00003290 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003291 if ((q - buf) < buf_size - 1) {
3292 *q++ = *p;
3293 }
3294 p++;
3295 }
bellard9307c4c2004-04-04 12:57:25 +00003296 }
bellard81d09122004-07-14 17:21:37 +00003297 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003298 *pp = p;
3299 return 0;
3300}
3301
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003302/*
3303 * Store the command-name in cmdname, and return a pointer to
3304 * the remaining of the command string.
3305 */
3306static const char *get_command_name(const char *cmdline,
3307 char *cmdname, size_t nlen)
3308{
3309 size_t len;
3310 const char *p, *pstart;
3311
3312 p = cmdline;
3313 while (qemu_isspace(*p))
3314 p++;
3315 if (*p == '\0')
3316 return NULL;
3317 pstart = p;
3318 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3319 p++;
3320 len = p - pstart;
3321 if (len > nlen - 1)
3322 len = nlen - 1;
3323 memcpy(cmdname, pstart, len);
3324 cmdname[len] = '\0';
3325 return p;
3326}
3327
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003328/**
3329 * Read key of 'type' into 'key' and return the current
3330 * 'type' pointer.
3331 */
3332static char *key_get_info(const char *type, char **key)
3333{
3334 size_t len;
3335 char *p, *str;
3336
3337 if (*type == ',')
3338 type++;
3339
3340 p = strchr(type, ':');
3341 if (!p) {
3342 *key = NULL;
3343 return NULL;
3344 }
3345 len = p - type;
3346
3347 str = qemu_malloc(len + 1);
3348 memcpy(str, type, len);
3349 str[len] = '\0';
3350
3351 *key = str;
3352 return ++p;
3353}
3354
bellard9307c4c2004-04-04 12:57:25 +00003355static int default_fmt_format = 'x';
3356static int default_fmt_size = 4;
3357
3358#define MAX_ARGS 16
3359
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003360static int is_valid_option(const char *c, const char *typestr)
3361{
3362 char option[3];
3363
3364 option[0] = '-';
3365 option[1] = *c;
3366 option[2] = '\0';
3367
3368 typestr = strstr(typestr, option);
3369 return (typestr != NULL);
3370}
3371
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003372static const mon_cmd_t *monitor_find_command(const char *cmdname)
3373{
3374 const mon_cmd_t *cmd;
3375
3376 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3377 if (compare_cmd(cmdname, cmd->name)) {
3378 return cmd;
3379 }
3380 }
3381
3382 return NULL;
3383}
3384
Anthony Liguoric227f092009-10-01 16:12:16 -05003385static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003386 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003387 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003388{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003389 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003390 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003391 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003392 char cmdname[256];
3393 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003394 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003395
3396#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003397 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003398#endif
ths3b46e622007-09-17 08:09:54 +00003399
bellard9307c4c2004-04-04 12:57:25 +00003400 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003401 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3402 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003403 return NULL;
ths3b46e622007-09-17 08:09:54 +00003404
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003405 cmd = monitor_find_command(cmdname);
3406 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003407 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003408 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003409 }
bellard9307c4c2004-04-04 12:57:25 +00003410
bellard9307c4c2004-04-04 12:57:25 +00003411 /* parse the parameters */
3412 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003413 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003414 typestr = key_get_info(typestr, &key);
3415 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003416 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003417 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003418 typestr++;
3419 switch(c) {
3420 case 'F':
bellard81d09122004-07-14 17:21:37 +00003421 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003422 case 's':
3423 {
3424 int ret;
ths3b46e622007-09-17 08:09:54 +00003425
blueswir1cd390082008-11-16 13:53:32 +00003426 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003427 p++;
3428 if (*typestr == '?') {
3429 typestr++;
3430 if (*p == '\0') {
3431 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003432 break;
bellard9307c4c2004-04-04 12:57:25 +00003433 }
3434 }
3435 ret = get_str(buf, sizeof(buf), &p);
3436 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003437 switch(c) {
3438 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003439 monitor_printf(mon, "%s: filename expected\n",
3440 cmdname);
bellard81d09122004-07-14 17:21:37 +00003441 break;
3442 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003443 monitor_printf(mon, "%s: block device name expected\n",
3444 cmdname);
bellard81d09122004-07-14 17:21:37 +00003445 break;
3446 default:
aliguori376253e2009-03-05 23:01:23 +00003447 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003448 break;
3449 }
bellard9307c4c2004-04-04 12:57:25 +00003450 goto fail;
3451 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003452 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003453 }
3454 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003455 case 'O':
3456 {
3457 QemuOptsList *opts_list;
3458 QemuOpts *opts;
3459
3460 opts_list = qemu_find_opts(key);
3461 if (!opts_list || opts_list->desc->name) {
3462 goto bad_type;
3463 }
3464 while (qemu_isspace(*p)) {
3465 p++;
3466 }
3467 if (!*p)
3468 break;
3469 if (get_str(buf, sizeof(buf), &p) < 0) {
3470 goto fail;
3471 }
3472 opts = qemu_opts_parse(opts_list, buf, 1);
3473 if (!opts) {
3474 goto fail;
3475 }
3476 qemu_opts_to_qdict(opts, qdict);
3477 qemu_opts_del(opts);
3478 }
3479 break;
bellard9307c4c2004-04-04 12:57:25 +00003480 case '/':
3481 {
3482 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003483
blueswir1cd390082008-11-16 13:53:32 +00003484 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003485 p++;
3486 if (*p == '/') {
3487 /* format found */
3488 p++;
3489 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003490 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003491 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003492 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003493 count = count * 10 + (*p - '0');
3494 p++;
3495 }
3496 }
3497 size = -1;
3498 format = -1;
3499 for(;;) {
3500 switch(*p) {
3501 case 'o':
3502 case 'd':
3503 case 'u':
3504 case 'x':
3505 case 'i':
3506 case 'c':
3507 format = *p++;
3508 break;
3509 case 'b':
3510 size = 1;
3511 p++;
3512 break;
3513 case 'h':
3514 size = 2;
3515 p++;
3516 break;
3517 case 'w':
3518 size = 4;
3519 p++;
3520 break;
3521 case 'g':
3522 case 'L':
3523 size = 8;
3524 p++;
3525 break;
3526 default:
3527 goto next;
3528 }
3529 }
3530 next:
blueswir1cd390082008-11-16 13:53:32 +00003531 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003532 monitor_printf(mon, "invalid char in format: '%c'\n",
3533 *p);
bellard9307c4c2004-04-04 12:57:25 +00003534 goto fail;
3535 }
bellard9307c4c2004-04-04 12:57:25 +00003536 if (format < 0)
3537 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003538 if (format != 'i') {
3539 /* for 'i', not specifying a size gives -1 as size */
3540 if (size < 0)
3541 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003542 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003543 }
bellard9307c4c2004-04-04 12:57:25 +00003544 default_fmt_format = format;
3545 } else {
3546 count = 1;
3547 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003548 if (format != 'i') {
3549 size = default_fmt_size;
3550 } else {
3551 size = -1;
3552 }
bellard9307c4c2004-04-04 12:57:25 +00003553 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003554 qdict_put(qdict, "count", qint_from_int(count));
3555 qdict_put(qdict, "format", qint_from_int(format));
3556 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003557 }
3558 break;
3559 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003560 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003561 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003562 {
blueswir1c2efc952007-09-25 17:28:42 +00003563 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003564
blueswir1cd390082008-11-16 13:53:32 +00003565 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003566 p++;
bellard34405572004-06-08 00:55:58 +00003567 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003568 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003569 if (*p == '\0') {
3570 typestr++;
3571 break;
3572 }
bellard34405572004-06-08 00:55:58 +00003573 } else {
3574 if (*p == '.') {
3575 p++;
blueswir1cd390082008-11-16 13:53:32 +00003576 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003577 p++;
bellard34405572004-06-08 00:55:58 +00003578 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003579 typestr++;
3580 break;
bellard34405572004-06-08 00:55:58 +00003581 }
3582 }
bellard13224a82006-07-14 22:03:35 +00003583 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003584 }
aliguori376253e2009-03-05 23:01:23 +00003585 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003586 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003587 /* Check if 'i' is greater than 32-bit */
3588 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3589 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3590 monitor_printf(mon, "integer is for 32-bit values\n");
3591 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003592 } else if (c == 'M') {
3593 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003594 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003595 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003596 }
3597 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003598 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003599 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003600 {
3601 double val;
3602
3603 while (qemu_isspace(*p))
3604 p++;
3605 if (*typestr == '?') {
3606 typestr++;
3607 if (*p == '\0') {
3608 break;
3609 }
3610 }
3611 if (get_double(mon, &val, &p) < 0) {
3612 goto fail;
3613 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003614 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003615 switch (*p) {
3616 case 'K': case 'k':
3617 val *= 1 << 10; p++; break;
3618 case 'M': case 'm':
3619 val *= 1 << 20; p++; break;
3620 case 'G': case 'g':
3621 val *= 1 << 30; p++; break;
3622 }
3623 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003624 if (c == 'T' && p[0] && p[1] == 's') {
3625 switch (*p) {
3626 case 'm':
3627 val /= 1e3; p += 2; break;
3628 case 'u':
3629 val /= 1e6; p += 2; break;
3630 case 'n':
3631 val /= 1e9; p += 2; break;
3632 }
3633 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003634 if (*p && !qemu_isspace(*p)) {
3635 monitor_printf(mon, "Unknown unit suffix\n");
3636 goto fail;
3637 }
3638 qdict_put(qdict, key, qfloat_from_double(val));
3639 }
3640 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003641 case 'b':
3642 {
3643 const char *beg;
3644 int val;
3645
3646 while (qemu_isspace(*p)) {
3647 p++;
3648 }
3649 beg = p;
3650 while (qemu_isgraph(*p)) {
3651 p++;
3652 }
3653 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3654 val = 1;
3655 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3656 val = 0;
3657 } else {
3658 monitor_printf(mon, "Expected 'on' or 'off'\n");
3659 goto fail;
3660 }
3661 qdict_put(qdict, key, qbool_from_int(val));
3662 }
3663 break;
bellard9307c4c2004-04-04 12:57:25 +00003664 case '-':
3665 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003666 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003667 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003668 /* option */
ths3b46e622007-09-17 08:09:54 +00003669
bellard9307c4c2004-04-04 12:57:25 +00003670 c = *typestr++;
3671 if (c == '\0')
3672 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003673 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003674 p++;
bellard9307c4c2004-04-04 12:57:25 +00003675 if (*p == '-') {
3676 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003677 if(c != *p) {
3678 if(!is_valid_option(p, typestr)) {
3679
3680 monitor_printf(mon, "%s: unsupported option -%c\n",
3681 cmdname, *p);
3682 goto fail;
3683 } else {
3684 skip_key = 1;
3685 }
bellard9307c4c2004-04-04 12:57:25 +00003686 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003687 if(skip_key) {
3688 p = tmp;
3689 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003690 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003691 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003692 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003693 }
bellard9307c4c2004-04-04 12:57:25 +00003694 }
bellard9307c4c2004-04-04 12:57:25 +00003695 }
3696 break;
3697 default:
3698 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003699 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003700 goto fail;
3701 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003702 qemu_free(key);
3703 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003704 }
3705 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003706 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003707 p++;
3708 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003709 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3710 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003711 goto fail;
3712 }
3713
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003714 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003715
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003716fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003717 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003718 return NULL;
3719}
3720
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003721void monitor_set_error(Monitor *mon, QError *qerror)
3722{
3723 /* report only the first error */
3724 if (!mon->error) {
3725 mon->error = qerror;
3726 } else {
3727 MON_DEBUG("Additional error report at %s:%d\n",
3728 qerror->file, qerror->linenr);
3729 QDECREF(qerror);
3730 }
3731}
3732
Adam Litke940cc302010-01-25 12:18:44 -06003733static int is_async_return(const QObject *data)
3734{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003735 if (data && qobject_type(data) == QTYPE_QDICT) {
3736 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3737 }
3738
3739 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003740}
3741
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003742static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3743{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003744 if (monitor_ctrl_mode(mon)) {
3745 if (ret && !monitor_has_error(mon)) {
3746 /*
3747 * If it returns failure, it must have passed on error.
3748 *
3749 * Action: Report an internal error to the client if in QMP.
3750 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003751 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003752 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3753 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003754 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003755
3756#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003757 if (!ret && monitor_has_error(mon)) {
3758 /*
3759 * If it returns success, it must not have passed an error.
3760 *
3761 * Action: Report the passed error to the client.
3762 */
3763 MON_DEBUG("command '%s' returned success but passed an error\n",
3764 cmd->name);
3765 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003766
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003767 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3768 /*
3769 * Handlers should not call Monitor print functions.
3770 *
3771 * Action: Ignore them in QMP.
3772 *
3773 * (XXX: we don't check any 'info' or 'query' command here
3774 * because the user print function _is_ called by do_info(), hence
3775 * we will trigger this check. This problem will go away when we
3776 * make 'query' commands real and kill do_info())
3777 */
3778 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3779 cmd->name, mon_print_count_get(mon));
3780 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003781#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003782 } else {
3783 assert(!monitor_has_error(mon));
3784 QDECREF(mon->error);
3785 mon->error = NULL;
3786 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003787}
3788
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003789static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3790 const QDict *params)
3791{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003792 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003793 QObject *data = NULL;
3794
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003795 mon_print_count_init(mon);
3796
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003797 ret = cmd->mhandler.cmd_new(mon, params, &data);
3798 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003799
Adam Litke940cc302010-01-25 12:18:44 -06003800 if (is_async_return(data)) {
3801 /*
3802 * Asynchronous commands have no initial return data but they can
3803 * generate errors. Data is returned via the async completion handler.
3804 */
3805 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3806 monitor_protocol_emitter(mon, NULL);
3807 }
3808 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003809 /* Monitor Protocol */
3810 monitor_protocol_emitter(mon, data);
3811 } else {
3812 /* User Protocol */
3813 if (data)
3814 cmd->user_print(mon, data);
3815 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003816
3817 qobject_decref(data);
3818}
3819
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003820static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003821{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003822 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003823 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003824
3825 qdict = qdict_new();
3826
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003827 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003828 if (!cmd)
3829 goto out;
3830
Adam Litke940cc302010-01-25 12:18:44 -06003831 if (monitor_handler_is_async(cmd)) {
3832 user_async_cmd_handler(mon, cmd, qdict);
3833 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003834 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003835 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003836 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003837 }
3838
Luiz Capitulino13917be2009-10-07 13:41:54 -03003839out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003840 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003841}
3842
bellard81d09122004-07-14 17:21:37 +00003843static void cmd_completion(const char *name, const char *list)
3844{
3845 const char *p, *pstart;
3846 char cmd[128];
3847 int len;
3848
3849 p = list;
3850 for(;;) {
3851 pstart = p;
3852 p = strchr(p, '|');
3853 if (!p)
3854 p = pstart + strlen(pstart);
3855 len = p - pstart;
3856 if (len > sizeof(cmd) - 2)
3857 len = sizeof(cmd) - 2;
3858 memcpy(cmd, pstart, len);
3859 cmd[len] = '\0';
3860 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003861 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003862 }
3863 if (*p == '\0')
3864 break;
3865 p++;
3866 }
3867}
3868
3869static void file_completion(const char *input)
3870{
3871 DIR *ffs;
3872 struct dirent *d;
3873 char path[1024];
3874 char file[1024], file_prefix[1024];
3875 int input_path_len;
3876 const char *p;
3877
ths5fafdf22007-09-16 21:08:06 +00003878 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003879 if (!p) {
3880 input_path_len = 0;
3881 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003882 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003883 } else {
3884 input_path_len = p - input + 1;
3885 memcpy(path, input, input_path_len);
3886 if (input_path_len > sizeof(path) - 1)
3887 input_path_len = sizeof(path) - 1;
3888 path[input_path_len] = '\0';
3889 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3890 }
3891#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003892 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3893 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003894#endif
3895 ffs = opendir(path);
3896 if (!ffs)
3897 return;
3898 for(;;) {
3899 struct stat sb;
3900 d = readdir(ffs);
3901 if (!d)
3902 break;
3903 if (strstart(d->d_name, file_prefix, NULL)) {
3904 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003905 if (input_path_len < sizeof(file))
3906 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3907 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003908 /* stat the file to find out if it's a directory.
3909 * In that case add a slash to speed up typing long paths
3910 */
3911 stat(file, &sb);
3912 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003913 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003914 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003915 }
3916 }
3917 closedir(ffs);
3918}
3919
aliguori51de9762009-03-05 23:00:43 +00003920static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003921{
aliguori51de9762009-03-05 23:00:43 +00003922 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003923 const char *input = opaque;
3924
3925 if (input[0] == '\0' ||
3926 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003927 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003928 }
3929}
3930
3931/* NOTE: this parser is an approximate form of the real command parser */
3932static void parse_cmdline(const char *cmdline,
3933 int *pnb_args, char **args)
3934{
3935 const char *p;
3936 int nb_args, ret;
3937 char buf[1024];
3938
3939 p = cmdline;
3940 nb_args = 0;
3941 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003942 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003943 p++;
3944 if (*p == '\0')
3945 break;
3946 if (nb_args >= MAX_ARGS)
3947 break;
3948 ret = get_str(buf, sizeof(buf), &p);
3949 args[nb_args] = qemu_strdup(buf);
3950 nb_args++;
3951 if (ret < 0)
3952 break;
3953 }
3954 *pnb_args = nb_args;
3955}
3956
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003957static const char *next_arg_type(const char *typestr)
3958{
3959 const char *p = strchr(typestr, ':');
3960 return (p != NULL ? ++p : typestr);
3961}
3962
aliguori4c36ba32009-03-05 23:01:37 +00003963static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003964{
3965 const char *cmdname;
3966 char *args[MAX_ARGS];
3967 int nb_args, i, len;
3968 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003969 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003970 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003971
3972 parse_cmdline(cmdline, &nb_args, args);
3973#ifdef DEBUG_COMPLETION
3974 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003975 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003976 }
3977#endif
3978
3979 /* if the line ends with a space, it means we want to complete the
3980 next arg */
3981 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003982 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003983 if (nb_args >= MAX_ARGS) {
3984 goto cleanup;
3985 }
bellard81d09122004-07-14 17:21:37 +00003986 args[nb_args++] = qemu_strdup("");
3987 }
3988 if (nb_args <= 1) {
3989 /* command completion */
3990 if (nb_args == 0)
3991 cmdname = "";
3992 else
3993 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003994 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003995 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003996 cmd_completion(cmdname, cmd->name);
3997 }
3998 } else {
3999 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004000 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4001 if (compare_cmd(args[0], cmd->name)) {
4002 break;
4003 }
bellard81d09122004-07-14 17:21:37 +00004004 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004005 if (!cmd->name) {
4006 goto cleanup;
4007 }
4008
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004009 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004010 for(i = 0; i < nb_args - 2; i++) {
4011 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004012 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004013 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004014 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004015 }
4016 }
4017 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004018 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004019 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004020 }
bellard81d09122004-07-14 17:21:37 +00004021 switch(*ptype) {
4022 case 'F':
4023 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004024 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004025 file_completion(str);
4026 break;
4027 case 'B':
4028 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004029 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004030 bdrv_iterate(block_completion_it, (void *)str);
4031 break;
bellard7fe48482004-10-09 18:08:01 +00004032 case 's':
4033 /* XXX: more generic ? */
4034 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004035 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004036 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4037 cmd_completion(str, cmd->name);
4038 }
bellard64866c32006-05-07 18:03:31 +00004039 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004040 char *sep = strrchr(str, '-');
4041 if (sep)
4042 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004043 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004044 for(key = key_defs; key->name != NULL; key++) {
4045 cmd_completion(str, key->name);
4046 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004047 } else if (!strcmp(cmd->name, "help|?")) {
4048 readline_set_completion_index(cur_mon->rs, strlen(str));
4049 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4050 cmd_completion(str, cmd->name);
4051 }
bellard7fe48482004-10-09 18:08:01 +00004052 }
4053 break;
bellard81d09122004-07-14 17:21:37 +00004054 default:
4055 break;
4056 }
4057 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004058
4059cleanup:
4060 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00004061 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004062 }
bellard81d09122004-07-14 17:21:37 +00004063}
4064
aliguori731b0362009-03-05 23:01:42 +00004065static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004066{
aliguori731b0362009-03-05 23:01:42 +00004067 Monitor *mon = opaque;
4068
Jan Kiszkac62313b2009-12-04 14:05:29 +01004069 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004070}
4071
Luiz Capitulino09069b12010-02-04 18:10:06 -02004072static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4073{
4074 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4075 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4076}
4077
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004078/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004079 * Argument validation rules:
4080 *
4081 * 1. The argument must exist in cmd_args qdict
4082 * 2. The argument type must be the expected one
4083 *
4084 * Special case: If the argument doesn't exist in cmd_args and
4085 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4086 * checking is skipped for it.
4087 */
4088static int check_client_args_type(const QDict *client_args,
4089 const QDict *cmd_args, int flags)
4090{
4091 const QDictEntry *ent;
4092
4093 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4094 QObject *obj;
4095 QString *arg_type;
4096 const QObject *client_arg = qdict_entry_value(ent);
4097 const char *client_arg_name = qdict_entry_key(ent);
4098
4099 obj = qdict_get(cmd_args, client_arg_name);
4100 if (!obj) {
4101 if (flags & QMP_ACCEPT_UNKNOWNS) {
4102 /* handler accepts unknowns */
4103 continue;
4104 }
4105 /* client arg doesn't exist */
4106 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4107 return -1;
4108 }
4109
4110 arg_type = qobject_to_qstring(obj);
4111 assert(arg_type != NULL);
4112
4113 /* check if argument's type is correct */
4114 switch (qstring_get_str(arg_type)[0]) {
4115 case 'F':
4116 case 'B':
4117 case 's':
4118 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4119 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4120 "string");
4121 return -1;
4122 }
4123 break;
4124 case 'i':
4125 case 'l':
4126 case 'M':
4127 if (qobject_type(client_arg) != QTYPE_QINT) {
4128 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4129 "int");
4130 return -1;
4131 }
4132 break;
4133 case 'f':
4134 case 'T':
4135 if (qobject_type(client_arg) != QTYPE_QINT &&
4136 qobject_type(client_arg) != QTYPE_QFLOAT) {
4137 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4138 "number");
4139 return -1;
4140 }
4141 break;
4142 case 'b':
4143 case '-':
4144 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4145 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4146 "bool");
4147 return -1;
4148 }
4149 break;
4150 case 'O':
4151 assert(flags & QMP_ACCEPT_UNKNOWNS);
4152 break;
4153 case '/':
4154 case '.':
4155 /*
4156 * These types are not supported by QMP and thus are not
4157 * handled here. Fall through.
4158 */
4159 default:
4160 abort();
4161 }
4162 }
4163
4164 return 0;
4165}
4166
4167/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004168 * - Check if the client has passed all mandatory args
4169 * - Set special flags for argument validation
4170 */
4171static int check_mandatory_args(const QDict *cmd_args,
4172 const QDict *client_args, int *flags)
4173{
4174 const QDictEntry *ent;
4175
4176 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4177 const char *cmd_arg_name = qdict_entry_key(ent);
4178 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4179 assert(type != NULL);
4180
4181 if (qstring_get_str(type)[0] == 'O') {
4182 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4183 *flags |= QMP_ACCEPT_UNKNOWNS;
4184 } else if (qstring_get_str(type)[0] != '-' &&
4185 qstring_get_str(type)[1] != '?' &&
4186 !qdict_haskey(client_args, cmd_arg_name)) {
4187 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4188 return -1;
4189 }
4190 }
4191
4192 return 0;
4193}
4194
4195static QDict *qdict_from_args_type(const char *args_type)
4196{
4197 int i;
4198 QDict *qdict;
4199 QString *key, *type, *cur_qs;
4200
4201 assert(args_type != NULL);
4202
4203 qdict = qdict_new();
4204
4205 if (args_type == NULL || args_type[0] == '\0') {
4206 /* no args, empty qdict */
4207 goto out;
4208 }
4209
4210 key = qstring_new();
4211 type = qstring_new();
4212
4213 cur_qs = key;
4214
4215 for (i = 0;; i++) {
4216 switch (args_type[i]) {
4217 case ',':
4218 case '\0':
4219 qdict_put(qdict, qstring_get_str(key), type);
4220 QDECREF(key);
4221 if (args_type[i] == '\0') {
4222 goto out;
4223 }
4224 type = qstring_new(); /* qdict has ref */
4225 cur_qs = key = qstring_new();
4226 break;
4227 case ':':
4228 cur_qs = type;
4229 break;
4230 default:
4231 qstring_append_chr(cur_qs, args_type[i]);
4232 break;
4233 }
4234 }
4235
4236out:
4237 return qdict;
4238}
4239
4240/*
4241 * Client argument checking rules:
4242 *
4243 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004244 * 2. Each argument provided by the client must be expected
4245 * 3. Each argument provided by the client must have the type expected
4246 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004247 */
4248static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4249{
4250 int flags, err;
4251 QDict *cmd_args;
4252
4253 cmd_args = qdict_from_args_type(cmd->args_type);
4254
4255 flags = 0;
4256 err = check_mandatory_args(cmd_args, client_args, &flags);
4257 if (err) {
4258 goto out;
4259 }
4260
Luiz Capitulino4af91932010-06-22 11:44:05 -03004261 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004262
4263out:
4264 QDECREF(cmd_args);
4265 return err;
4266}
4267
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004268/*
4269 * Input object checking rules
4270 *
4271 * 1. Input object must be a dict
4272 * 2. The "execute" key must exist
4273 * 3. The "execute" key must be a string
4274 * 4. If the "arguments" key exists, it must be a dict
4275 * 5. If the "id" key exists, it can be anything (ie. json-value)
4276 * 6. Any argument not listed above is considered invalid
4277 */
4278static QDict *qmp_check_input_obj(QObject *input_obj)
4279{
4280 const QDictEntry *ent;
4281 int has_exec_key = 0;
4282 QDict *input_dict;
4283
4284 if (qobject_type(input_obj) != QTYPE_QDICT) {
4285 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4286 return NULL;
4287 }
4288
4289 input_dict = qobject_to_qdict(input_obj);
4290
4291 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4292 const char *arg_name = qdict_entry_key(ent);
4293 const QObject *arg_obj = qdict_entry_value(ent);
4294
4295 if (!strcmp(arg_name, "execute")) {
4296 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4297 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4298 "string");
4299 return NULL;
4300 }
4301 has_exec_key = 1;
4302 } else if (!strcmp(arg_name, "arguments")) {
4303 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4304 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4305 "object");
4306 return NULL;
4307 }
4308 } else if (!strcmp(arg_name, "id")) {
4309 /* FIXME: check duplicated IDs for async commands */
4310 } else {
4311 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4312 return NULL;
4313 }
4314 }
4315
4316 if (!has_exec_key) {
4317 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4318 return NULL;
4319 }
4320
4321 return input_dict;
4322}
4323
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004324static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4325{
4326 int err;
4327 QObject *obj;
4328 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004329 const mon_cmd_t *cmd;
4330 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004331 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004332
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004333 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004334
4335 obj = json_parser_parse(tokens, NULL);
4336 if (!obj) {
4337 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004338 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004339 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004340 }
4341
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004342 input = qmp_check_input_obj(obj);
4343 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004344 qobject_decref(obj);
4345 goto err_out;
4346 }
4347
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004348 mon->mc->id = qdict_get(input, "id");
4349 qobject_incref(mon->mc->id);
4350
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004351 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004352 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004353 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004354 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004355 }
4356
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004357 /*
4358 * XXX: We need this special case until we get info handlers
4359 * converted into 'query-' commands
4360 */
4361 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004362 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004363 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004364 } else if (strstart(cmd_name, "query-", &info_item)) {
4365 cmd = monitor_find_command("info");
4366 qdict_put_obj(input, "arguments",
4367 qobject_from_jsonf("{ 'item': %s }", info_item));
4368 } else {
4369 cmd = monitor_find_command(cmd_name);
Jan Kiszkaa6c4d362010-06-28 18:27:47 +02004370 if (!cmd || !monitor_handler_ported(cmd)
4371 || monitor_cmd_user_only(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004372 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004373 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004374 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004375 }
4376
4377 obj = qdict_get(input, "arguments");
4378 if (!obj) {
4379 args = qdict_new();
4380 } else {
4381 args = qobject_to_qdict(obj);
4382 QINCREF(args);
4383 }
4384
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004385 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004386 if (err < 0) {
4387 goto err_out;
4388 }
4389
Adam Litke940cc302010-01-25 12:18:44 -06004390 if (monitor_handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004391 err = qmp_async_cmd_handler(mon, cmd, args);
4392 if (err) {
4393 /* emit the error response */
4394 goto err_out;
4395 }
Adam Litke940cc302010-01-25 12:18:44 -06004396 } else {
4397 monitor_call_handler(mon, cmd, args);
4398 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004399
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004400 goto out;
4401
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004402err_out:
4403 monitor_protocol_emitter(mon, NULL);
4404out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004405 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004406 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004407}
4408
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004409/**
4410 * monitor_control_read(): Read and handle QMP input
4411 */
4412static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4413{
4414 Monitor *old_mon = cur_mon;
4415
4416 cur_mon = opaque;
4417
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004418 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004419
4420 cur_mon = old_mon;
4421}
4422
aliguori731b0362009-03-05 23:01:42 +00004423static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004424{
aliguori731b0362009-03-05 23:01:42 +00004425 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004426 int i;
aliguori376253e2009-03-05 23:01:23 +00004427
aliguori731b0362009-03-05 23:01:42 +00004428 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004429
aliguoricde76ee2009-03-05 23:01:51 +00004430 if (cur_mon->rs) {
4431 for (i = 0; i < size; i++)
4432 readline_handle_byte(cur_mon->rs, buf[i]);
4433 } else {
4434 if (size == 0 || buf[size - 1] != 0)
4435 monitor_printf(cur_mon, "corrupted command\n");
4436 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004437 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004438 }
aliguori731b0362009-03-05 23:01:42 +00004439
4440 cur_mon = old_mon;
4441}
aliguorid8f44602008-10-06 13:52:44 +00004442
aliguori376253e2009-03-05 23:01:23 +00004443static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004444{
aliguori731b0362009-03-05 23:01:42 +00004445 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004446 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004447 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004448}
4449
aliguoricde76ee2009-03-05 23:01:51 +00004450int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004451{
aliguoricde76ee2009-03-05 23:01:51 +00004452 if (!mon->rs)
4453 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004454 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004455 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004456}
4457
aliguori376253e2009-03-05 23:01:23 +00004458void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004459{
aliguoricde76ee2009-03-05 23:01:51 +00004460 if (!mon->rs)
4461 return;
aliguori731b0362009-03-05 23:01:42 +00004462 if (--mon->suspend_cnt == 0)
4463 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004464}
4465
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004466static QObject *get_qmp_greeting(void)
4467{
4468 QObject *ver;
4469
4470 do_info_version(NULL, &ver);
4471 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4472}
4473
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004474/**
4475 * monitor_control_event(): Print QMP gretting
4476 */
4477static void monitor_control_event(void *opaque, int event)
4478{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004479 QObject *data;
4480 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004481
Luiz Capitulino47116d12010-02-08 17:01:30 -02004482 switch (event) {
4483 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004484 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004485 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004486 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004487 monitor_json_emitter(mon, data);
4488 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004489 break;
4490 case CHR_EVENT_CLOSED:
4491 json_message_parser_destroy(&mon->mc->parser);
4492 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004493 }
4494}
4495
aliguori731b0362009-03-05 23:01:42 +00004496static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004497{
aliguori376253e2009-03-05 23:01:23 +00004498 Monitor *mon = opaque;
4499
aliguori2724b182009-03-05 23:01:47 +00004500 switch (event) {
4501 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004502 mon->mux_out = 0;
4503 if (mon->reset_seen) {
4504 readline_restart(mon->rs);
4505 monitor_resume(mon);
4506 monitor_flush(mon);
4507 } else {
4508 mon->suspend_cnt = 0;
4509 }
aliguori2724b182009-03-05 23:01:47 +00004510 break;
ths86e94de2007-01-05 22:01:59 +00004511
aliguori2724b182009-03-05 23:01:47 +00004512 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004513 if (mon->reset_seen) {
4514 if (mon->suspend_cnt == 0) {
4515 monitor_printf(mon, "\n");
4516 }
4517 monitor_flush(mon);
4518 monitor_suspend(mon);
4519 } else {
4520 mon->suspend_cnt++;
4521 }
4522 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004523 break;
4524
Amit Shahb6b8df52009-10-07 18:31:16 +05304525 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004526 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4527 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004528 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004529 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004530 }
4531 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004532 break;
4533 }
ths86e94de2007-01-05 22:01:59 +00004534}
4535
aliguori76655d62009-03-06 20:27:37 +00004536
4537/*
4538 * Local variables:
4539 * c-indent-level: 4
4540 * c-basic-offset: 4
4541 * tab-width: 8
4542 * End:
4543 */
4544
aliguori731b0362009-03-05 23:01:42 +00004545void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004546{
aliguori731b0362009-03-05 23:01:42 +00004547 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004548 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004549
4550 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004551 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004552 is_first_init = 0;
4553 }
aliguori87127162009-03-05 23:01:29 +00004554
4555 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004556
aliguori87127162009-03-05 23:01:29 +00004557 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004558 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004559 if (flags & MONITOR_USE_READLINE) {
4560 mon->rs = readline_init(mon, monitor_find_completion);
4561 monitor_read_command(mon, 0);
4562 }
aliguori87127162009-03-05 23:01:29 +00004563
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004564 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004565 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004566 /* Control mode requires special handlers */
4567 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4568 monitor_control_event, mon);
4569 } else {
4570 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4571 monitor_event, mon);
4572 }
aliguori87127162009-03-05 23:01:29 +00004573
Blue Swirl72cf2d42009-09-12 07:36:22 +00004574 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004575 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4576 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004577}
4578
aliguori376253e2009-03-05 23:01:23 +00004579static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004580{
aliguoribb5fc202009-03-05 23:01:15 +00004581 BlockDriverState *bs = opaque;
4582 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004583
aliguoribb5fc202009-03-05 23:01:15 +00004584 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004585 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004586 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004587 }
aliguori731b0362009-03-05 23:01:42 +00004588 if (mon->password_completion_cb)
4589 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004590
aliguori731b0362009-03-05 23:01:42 +00004591 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004592}
aliguoric0f4ce72009-03-05 23:01:01 +00004593
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004594int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4595 BlockDriverCompletionFunc *completion_cb,
4596 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004597{
aliguoricde76ee2009-03-05 23:01:51 +00004598 int err;
4599
aliguoribb5fc202009-03-05 23:01:15 +00004600 if (!bdrv_key_required(bs)) {
4601 if (completion_cb)
4602 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004603 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004604 }
aliguoric0f4ce72009-03-05 23:01:01 +00004605
Luiz Capitulino94171e12009-12-07 21:37:00 +01004606 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004607 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004608 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004609 }
4610
aliguori376253e2009-03-05 23:01:23 +00004611 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4612 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004613
aliguori731b0362009-03-05 23:01:42 +00004614 mon->password_completion_cb = completion_cb;
4615 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004616
aliguoricde76ee2009-03-05 23:01:51 +00004617 err = monitor_read_password(mon, bdrv_password_cb, bs);
4618
4619 if (err && completion_cb)
4620 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004621
4622 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004623}