blob: f0ec84d52f4f16d5d13a2cdf04c55444bc581cf8 [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"
41#include "block.h"
42#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 Capitulinof7188bb2009-08-28 15:27:10 -030052#include "qdict.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020055#include "qerror.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020056#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020057#include "json-streamer.h"
58#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000059#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000060
bellard9dc39cb2004-03-14 21:38:27 +000061//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000062//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000063
bellard9307c4c2004-04-04 12:57:25 +000064/*
65 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000066 *
bellard9307c4c2004-04-04 12:57:25 +000067 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000068 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000069 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010070 * 'O' option string of the form NAME=VALUE,...
71 * parsed according to QemuOptsList given by its name
72 * Example: 'device:O' uses qemu_device_opts.
73 * Restriction: only lists with empty desc are supported
74 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000075 * 'i' 32 bit integer
76 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010077 * 'M' just like 'l', except in user mode the value is
78 * multiplied by 2^20 (think Mebibyte)
Markus Armbruster3350a4d2010-01-25 14:23:03 +010079 * 'b' double
80 * user mode accepts an optional G, g, M, m, K, k suffix,
81 * which multiplies the value by 2^30 for suffixes G and
82 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010083 * 'T' double
84 * user mode accepts an optional ms, us, ns suffix,
85 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000086 * '/' optional gdb-like print format (like "/10x")
87 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030088 * '?' optional type (for all types, except '/')
89 * '.' other form of optional type (for 'i' and 'l')
90 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000091 *
92 */
93
Adam Litke940cc302010-01-25 12:18:44 -060094typedef struct MonitorCompletionData MonitorCompletionData;
95struct MonitorCompletionData {
96 Monitor *mon;
97 void (*user_print)(Monitor *mon, const QObject *data);
98};
99
Anthony Liguoric227f092009-10-01 16:12:16 -0500100typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000101 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000102 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000103 const char *params;
104 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300105 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300106 union {
107 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300108 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600109 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300110 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200111 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600112 int (*cmd_async)(Monitor *mon, const QDict *params,
113 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300114 } mhandler;
Adam Litke940cc302010-01-25 12:18:44 -0600115 int async;
Anthony Liguoric227f092009-10-01 16:12:16 -0500116} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000117
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100118/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500119typedef struct mon_fd_t mon_fd_t;
120struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100121 char *name;
122 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500123 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100124};
125
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200126typedef struct MonitorControl {
127 QObject *id;
128 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200129 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200130} MonitorControl;
131
aliguori87127162009-03-05 23:01:29 +0000132struct Monitor {
133 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200134 int mux_out;
135 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000136 int flags;
137 int suspend_cnt;
138 uint8_t outbuf[1024];
139 int outbuf_index;
140 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200141 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000142 CPUState *mon_cpu;
143 BlockDriverCompletionFunc *password_completion_cb;
144 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200145#ifdef CONFIG_DEBUG_MONITOR
146 int print_calls_nr;
147#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200148 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500149 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000150 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000151};
152
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200153#ifdef CONFIG_DEBUG_MONITOR
154#define MON_DEBUG(fmt, ...) do { \
155 fprintf(stderr, "Monitor: "); \
156 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200157
158static inline void mon_print_count_inc(Monitor *mon)
159{
160 mon->print_calls_nr++;
161}
162
163static inline void mon_print_count_init(Monitor *mon)
164{
165 mon->print_calls_nr = 0;
166}
167
168static inline int mon_print_count_get(const Monitor *mon)
169{
170 return mon->print_calls_nr;
171}
172
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200173#else /* !CONFIG_DEBUG_MONITOR */
174#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200175static inline void mon_print_count_inc(Monitor *mon) { }
176static inline void mon_print_count_init(Monitor *mon) { }
177static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200178#endif /* CONFIG_DEBUG_MONITOR */
179
Blue Swirl72cf2d42009-09-12 07:36:22 +0000180static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000181
Anthony Liguoric227f092009-10-01 16:12:16 -0500182static const mon_cmd_t mon_cmds[];
183static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000184
Markus Armbruster8631b602010-02-18 11:41:55 +0100185Monitor *cur_mon;
186Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000187
aliguori731b0362009-03-05 23:01:42 +0000188static void monitor_command_cb(Monitor *mon, const char *cmdline,
189 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000190
Luiz Capitulino09069b12010-02-04 18:10:06 -0200191static inline int qmp_cmd_mode(const Monitor *mon)
192{
193 return (mon->mc ? mon->mc->command_mode : 0);
194}
195
Luiz Capitulino418173c2009-11-26 22:58:51 -0200196/* Return true if in control mode, false otherwise */
197static inline int monitor_ctrl_mode(const Monitor *mon)
198{
199 return (mon->flags & MONITOR_USE_CONTROL);
200}
201
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100202/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
203int monitor_cur_is_qmp(void)
204{
205 return cur_mon && monitor_ctrl_mode(cur_mon);
206}
207
aliguori731b0362009-03-05 23:01:42 +0000208static void monitor_read_command(Monitor *mon, int show_prompt)
209{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200210 if (!mon->rs)
211 return;
212
aliguori731b0362009-03-05 23:01:42 +0000213 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
214 if (show_prompt)
215 readline_show_prompt(mon->rs);
216}
bellard6a00d602005-11-21 23:25:50 +0000217
aliguoricde76ee2009-03-05 23:01:51 +0000218static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
219 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000220{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100221 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100222 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100223 return -EINVAL;
224 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000225 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
226 /* prompt is printed on return from the command handler */
227 return 0;
228 } else {
229 monitor_printf(mon, "terminal does not support password prompting\n");
230 return -ENOTTY;
231 }
aliguoribb5fc202009-03-05 23:01:15 +0000232}
233
aliguori376253e2009-03-05 23:01:23 +0000234void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000235{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200236 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000237 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
238 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000239 }
240}
241
242/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000243static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000244{
ths60fe76f2007-12-16 03:02:09 +0000245 char c;
aliguori731b0362009-03-05 23:01:42 +0000246
bellard7e2515e2004-08-01 21:52:19 +0000247 for(;;) {
248 c = *str++;
249 if (c == '\0')
250 break;
bellard7ba12602006-07-14 20:26:42 +0000251 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000252 mon->outbuf[mon->outbuf_index++] = '\r';
253 mon->outbuf[mon->outbuf_index++] = c;
254 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
255 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000256 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000257 }
258}
259
aliguori376253e2009-03-05 23:01:23 +0000260void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000261{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200262 char buf[4096];
263
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200264 if (!mon)
265 return;
266
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200267 mon_print_count_inc(mon);
268
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200269 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200270 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200271 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200272
273 vsnprintf(buf, sizeof(buf), fmt, ap);
274 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000275}
276
aliguori376253e2009-03-05 23:01:23 +0000277void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000278{
279 va_list ap;
280 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000281 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000282 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000283}
284
aliguori376253e2009-03-05 23:01:23 +0000285void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000286{
287 int i;
288
289 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000290 switch (filename[i]) {
291 case ' ':
292 case '"':
293 case '\\':
294 monitor_printf(mon, "\\%c", filename[i]);
295 break;
296 case '\t':
297 monitor_printf(mon, "\\t");
298 break;
299 case '\r':
300 monitor_printf(mon, "\\r");
301 break;
302 case '\n':
303 monitor_printf(mon, "\\n");
304 break;
305 default:
306 monitor_printf(mon, "%c", filename[i]);
307 break;
308 }
thsfef30742006-12-22 14:11:32 +0000309 }
310}
311
bellard7fe48482004-10-09 18:08:01 +0000312static int monitor_fprintf(FILE *stream, const char *fmt, ...)
313{
314 va_list ap;
315 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000316 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000317 va_end(ap);
318 return 0;
319}
320
Luiz Capitulino13c74252009-10-07 13:41:55 -0300321static void monitor_user_noop(Monitor *mon, const QObject *data) { }
322
Luiz Capitulino13917be2009-10-07 13:41:54 -0300323static inline int monitor_handler_ported(const mon_cmd_t *cmd)
324{
325 return cmd->user_print != NULL;
326}
327
Adam Litke940cc302010-01-25 12:18:44 -0600328static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
329{
330 return cmd->async != 0;
331}
332
Luiz Capitulino8204a912009-11-18 23:05:31 -0200333static inline int monitor_has_error(const Monitor *mon)
334{
335 return mon->error != NULL;
336}
337
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200338static void monitor_json_emitter(Monitor *mon, const QObject *data)
339{
340 QString *json;
341
342 json = qobject_to_json(data);
343 assert(json != NULL);
344
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200345 qstring_append_chr(json, '\n');
346 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200347
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200348 QDECREF(json);
349}
350
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200351static void monitor_protocol_emitter(Monitor *mon, QObject *data)
352{
353 QDict *qmp;
354
355 qmp = qdict_new();
356
357 if (!monitor_has_error(mon)) {
358 /* success response */
359 if (data) {
360 qobject_incref(data);
361 qdict_put_obj(qmp, "return", data);
362 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200363 /* return an empty QDict by default */
364 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200365 }
366 } else {
367 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100368 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200369 qdict_put(qmp, "error", mon->error->error);
370 QINCREF(mon->error->error);
371 QDECREF(mon->error);
372 mon->error = NULL;
373 }
374
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200375 if (mon->mc->id) {
376 qdict_put_obj(qmp, "id", mon->mc->id);
377 mon->mc->id = NULL;
378 }
379
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200380 monitor_json_emitter(mon, QOBJECT(qmp));
381 QDECREF(qmp);
382}
383
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200384static void timestamp_put(QDict *qdict)
385{
386 int err;
387 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000388 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200389
Blue Swirld08d6f02009-12-04 18:06:39 +0000390 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200391 if (err < 0)
392 return;
393
394 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
395 "'microseconds': %" PRId64 " }",
396 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200397 qdict_put_obj(qdict, "timestamp", obj);
398}
399
400/**
401 * monitor_protocol_event(): Generate a Monitor event
402 *
403 * Event-specific data can be emitted through the (optional) 'data' parameter.
404 */
405void monitor_protocol_event(MonitorEvent event, QObject *data)
406{
407 QDict *qmp;
408 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600409 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200410
Blue Swirl242cd002009-12-04 18:05:45 +0000411 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200412
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200413 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000414 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200415 event_name = "SHUTDOWN";
416 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000417 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200418 event_name = "RESET";
419 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000420 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200421 event_name = "POWERDOWN";
422 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000423 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200424 event_name = "STOP";
425 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200426 case QEVENT_VNC_CONNECTED:
427 event_name = "VNC_CONNECTED";
428 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200429 case QEVENT_VNC_INITIALIZED:
430 event_name = "VNC_INITIALIZED";
431 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200432 case QEVENT_VNC_DISCONNECTED:
433 event_name = "VNC_DISCONNECTED";
434 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200435 case QEVENT_BLOCK_IO_ERROR:
436 event_name = "BLOCK_IO_ERROR";
437 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300438 case QEVENT_RTC_CHANGE:
439 event_name = "RTC_CHANGE";
440 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300441 case QEVENT_WATCHDOG:
442 event_name = "WATCHDOG";
443 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200444 default:
445 abort();
446 break;
447 }
448
449 qmp = qdict_new();
450 timestamp_put(qmp);
451 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200452 if (data) {
453 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200454 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200455 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200456
Adam Litkef039a562010-01-15 08:34:02 -0600457 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200458 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200459 monitor_json_emitter(mon, QOBJECT(qmp));
460 }
Adam Litkef039a562010-01-15 08:34:02 -0600461 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200462 QDECREF(qmp);
463}
464
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200465static int do_qmp_capabilities(Monitor *mon, const QDict *params,
466 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200467{
468 /* Will setup QMP capabilities in the future */
469 if (monitor_ctrl_mode(mon)) {
470 mon->mc->command_mode = 1;
471 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200472
473 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200474}
475
bellard9dc39cb2004-03-14 21:38:27 +0000476static int compare_cmd(const char *name, const char *list)
477{
478 const char *p, *pstart;
479 int len;
480 len = strlen(name);
481 p = list;
482 for(;;) {
483 pstart = p;
484 p = strchr(p, '|');
485 if (!p)
486 p = pstart + strlen(pstart);
487 if ((p - pstart) == len && !memcmp(pstart, name, len))
488 return 1;
489 if (*p == '\0')
490 break;
491 p++;
492 }
493 return 0;
494}
495
Anthony Liguoric227f092009-10-01 16:12:16 -0500496static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000497 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000498{
Anthony Liguoric227f092009-10-01 16:12:16 -0500499 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000500
501 for(cmd = cmds; cmd->name != NULL; cmd++) {
502 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000503 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
504 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000505 }
506}
507
aliguori376253e2009-03-05 23:01:23 +0000508static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000509{
510 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000511 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000512 } else {
aliguori376253e2009-03-05 23:01:23 +0000513 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000514 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000515 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000516 monitor_printf(mon, "Log items (comma separated):\n");
517 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000518 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000519 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000520 }
521 }
bellard9dc39cb2004-03-14 21:38:27 +0000522 }
523}
524
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300525static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300526{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300527 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300528}
529
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300530static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000531{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200532 int all_devices;
533 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300534 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000535
bellard7954c732006-08-01 15:52:40 +0000536 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000537 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200538 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300539 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200540 continue;
541 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000542 }
543}
544
Adam Litke940cc302010-01-25 12:18:44 -0600545static void user_monitor_complete(void *opaque, QObject *ret_data)
546{
547 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
548
549 if (ret_data) {
550 data->user_print(data->mon, ret_data);
551 }
552 monitor_resume(data->mon);
553 qemu_free(data);
554}
555
556static void qmp_monitor_complete(void *opaque, QObject *ret_data)
557{
558 monitor_protocol_emitter(opaque, ret_data);
559}
560
561static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
562 const QDict *params)
563{
564 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
565}
566
567static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
568{
569 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
570}
571
572static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
573 const QDict *params)
574{
575 int ret;
576
577 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
578 cb_data->mon = mon;
579 cb_data->user_print = cmd->user_print;
580 monitor_suspend(mon);
581 ret = cmd->mhandler.cmd_async(mon, params,
582 user_monitor_complete, cb_data);
583 if (ret < 0) {
584 monitor_resume(mon);
585 qemu_free(cb_data);
586 }
587}
588
589static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
590{
591 int ret;
592
593 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
594 cb_data->mon = mon;
595 cb_data->user_print = cmd->user_print;
596 monitor_suspend(mon);
597 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
598 if (ret < 0) {
599 monitor_resume(mon);
600 qemu_free(cb_data);
601 }
602}
603
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200604static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000605{
Anthony Liguoric227f092009-10-01 16:12:16 -0500606 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300607 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000608
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200609 if (!item) {
610 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000611 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200612 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300613
614 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000615 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300616 break;
bellard9dc39cb2004-03-14 21:38:27 +0000617 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300618
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200619 if (cmd->name == NULL) {
620 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100621 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200622 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200623 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300624 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200625 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300626
Adam Litke940cc302010-01-25 12:18:44 -0600627 if (monitor_handler_is_async(cmd)) {
628 if (monitor_ctrl_mode(mon)) {
629 qmp_async_info_handler(mon, cmd);
630 } else {
631 user_async_info_handler(mon, cmd);
632 }
633 /*
634 * Indicate that this command is asynchronous and will not return any
635 * data (not even empty). Instead, the data will be returned via a
636 * completion callback.
637 */
638 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
639 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300640 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200641
642 if (!monitor_ctrl_mode(mon)) {
643 /*
644 * User Protocol function is called here, Monitor Protocol is
645 * handled by monitor_call_handler()
646 */
647 if (*ret_data)
648 cmd->user_print(mon, *ret_data);
649 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300650 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200651 if (monitor_ctrl_mode(mon)) {
652 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100653 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200654 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200655 } else {
656 cmd->mhandler.info(mon);
657 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300658 }
659
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200660 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300661
662help:
663 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200664 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000665}
666
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200667static void do_info_version_print(Monitor *mon, const QObject *data)
668{
669 QDict *qdict;
670
671 qdict = qobject_to_qdict(data);
672
673 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
674 qdict_get_str(qdict, "package"));
675}
676
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300677/**
678 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200679 *
680 * Return a QDict with the following information:
681 *
682 * - "qemu": QEMU's version
683 * - "package": package's version
684 *
685 * Example:
686 *
687 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300688 */
689static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000690{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200691 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
692 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000693}
694
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200695static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000696{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200697 QDict *qdict;
698
699 qdict = qobject_to_qdict(data);
700 if (qdict_size(qdict) == 0) {
701 return;
702 }
703
704 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
705}
706
707/**
708 * do_info_name(): Show VM name
709 *
710 * Return a QDict with the following information:
711 *
712 * - "name": VM's name (optional)
713 *
714 * Example:
715 *
716 * { "name": "qemu-name" }
717 */
718static void do_info_name(Monitor *mon, QObject **ret_data)
719{
720 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
721 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000722}
723
Luiz Capitulino1a728672009-12-10 17:15:56 -0200724static QObject *get_cmd_dict(const char *name)
725{
726 const char *p;
727
728 /* Remove '|' from some commands */
729 p = strchr(name, '|');
730 if (p) {
731 p++;
732 } else {
733 p = name;
734 }
735
736 return qobject_from_jsonf("{ 'name': %s }", p);
737}
738
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200739/**
740 * do_info_commands(): List QMP available commands
741 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200742 * Each command is represented by a QDict, the returned QObject is a QList
743 * of all commands.
744 *
745 * The QDict contains:
746 *
747 * - "name": command's name
748 *
749 * Example:
750 *
751 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200752 */
753static void do_info_commands(Monitor *mon, QObject **ret_data)
754{
755 QList *cmd_list;
756 const mon_cmd_t *cmd;
757
758 cmd_list = qlist_new();
759
760 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
761 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200762 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200763 }
764 }
765
766 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
767 if (monitor_handler_ported(cmd)) {
768 char buf[128];
769 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200770 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200771 }
772 }
773
774 *ret_data = QOBJECT(cmd_list);
775}
776
aurel32bf4f74c2008-12-18 22:42:34 +0000777#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200778static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000779{
aliguori376253e2009-03-05 23:01:23 +0000780 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200781 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
782 "enabled" : "disabled");
783}
784
785/**
786 * do_info_hpet(): Show HPET state
787 *
788 * Return a QDict with the following information:
789 *
790 * - "enabled": true if hpet if enabled, false otherwise
791 *
792 * Example:
793 *
794 * { "enabled": true }
795 */
796static void do_info_hpet(Monitor *mon, QObject **ret_data)
797{
798 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000799}
aurel32bf4f74c2008-12-18 22:42:34 +0000800#endif
aliguori16b29ae2008-12-17 23:28:44 +0000801
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200802static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000803{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200804 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
805}
806
807/**
808 * do_info_uuid(): Show VM UUID
809 *
810 * Return a QDict with the following information:
811 *
812 * - "UUID": Universally Unique Identifier
813 *
814 * Example:
815 *
816 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
817 */
818static void do_info_uuid(Monitor *mon, QObject **ret_data)
819{
820 char uuid[64];
821
822 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000823 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
824 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
825 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
826 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200827 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000828}
829
bellard6a00d602005-11-21 23:25:50 +0000830/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000831static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000832{
833 CPUState *env;
834
835 for(env = first_cpu; env != NULL; env = env->next_cpu) {
836 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000837 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000838 return 0;
839 }
840 }
841 return -1;
842}
843
pbrook9596ebb2007-11-18 01:44:38 +0000844static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000845{
aliguori731b0362009-03-05 23:01:42 +0000846 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000847 mon_set_cpu(0);
848 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300849 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000850 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000851}
852
aliguori376253e2009-03-05 23:01:23 +0000853static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000854{
bellard6a00d602005-11-21 23:25:50 +0000855 CPUState *env;
856 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000857#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000858 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000859 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000860#else
aliguori376253e2009-03-05 23:01:23 +0000861 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000862 0);
bellard9307c4c2004-04-04 12:57:25 +0000863#endif
864}
865
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300866static void print_cpu_iter(QObject *obj, void *opaque)
867{
868 QDict *cpu;
869 int active = ' ';
870 Monitor *mon = opaque;
871
872 assert(qobject_type(obj) == QTYPE_QDICT);
873 cpu = qobject_to_qdict(obj);
874
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200875 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300876 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300878
879 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
880
881#if defined(TARGET_I386)
882 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
883 (target_ulong) qdict_get_int(cpu, "pc"));
884#elif defined(TARGET_PPC)
885 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
886 (target_long) qdict_get_int(cpu, "nip"));
887#elif defined(TARGET_SPARC)
888 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
889 (target_long) qdict_get_int(cpu, "pc"));
890 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
891 (target_long) qdict_get_int(cpu, "npc"));
892#elif defined(TARGET_MIPS)
893 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
894 (target_long) qdict_get_int(cpu, "PC"));
895#endif
896
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200897 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300898 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200899 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300900
901 monitor_printf(mon, "\n");
902}
903
904static void monitor_print_cpus(Monitor *mon, const QObject *data)
905{
906 QList *cpu_list;
907
908 assert(qobject_type(data) == QTYPE_QLIST);
909 cpu_list = qobject_to_qlist(data);
910 qlist_iter(cpu_list, print_cpu_iter, mon);
911}
912
913/**
914 * do_info_cpus(): Show CPU information
915 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200916 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300917 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200918 * - "cpu": CPU index
919 * - "current": true if this is the current CPU, false otherwise
920 * - "halted": true if the cpu is halted, false otherwise
921 * - Current program counter. The key's name depends on the architecture:
922 * "pc": i386/x86)64
923 * "nip": PPC
924 * "pc" and "npc": sparc
925 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300926 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200927 * Example:
928 *
929 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
930 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300931 */
932static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000933{
934 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300935 QList *cpu_list;
936
937 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000938
939 /* just to set the default cpu if not already done */
940 mon_get_cpu();
941
942 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200943 QDict *cpu;
944 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300945
Avi Kivity4c0960c2009-08-17 23:19:53 +0300946 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300947
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200948 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
949 env->cpu_index, env == mon->mon_cpu,
950 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200951
952 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300953
bellard6a00d602005-11-21 23:25:50 +0000954#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300955 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000956#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300957 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000958#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300959 qdict_put(cpu, "pc", qint_from_int(env->pc));
960 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000961#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300962 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000963#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300964
965 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000966 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300967
968 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000969}
970
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200971static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000972{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300973 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200974 if (mon_set_cpu(index) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100975 qerror_report(QERR_INVALID_PARAMETER, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200976 return -1;
977 }
978 return 0;
bellard6a00d602005-11-21 23:25:50 +0000979}
980
aliguori376253e2009-03-05 23:01:23 +0000981static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000982{
aliguori376253e2009-03-05 23:01:23 +0000983 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000984}
985
aliguori376253e2009-03-05 23:01:23 +0000986static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000987{
988 int i;
bellard7e2515e2004-08-01 21:52:19 +0000989 const char *str;
ths3b46e622007-09-17 08:09:54 +0000990
aliguoricde76ee2009-03-05 23:01:51 +0000991 if (!mon->rs)
992 return;
bellard7e2515e2004-08-01 21:52:19 +0000993 i = 0;
994 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000995 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000996 if (!str)
997 break;
aliguori376253e2009-03-05 23:01:23 +0000998 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000999 i++;
bellardaa455482004-04-04 13:07:25 +00001000 }
1001}
1002
j_mayer76a66252007-03-07 08:32:30 +00001003#if defined(TARGET_PPC)
1004/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +00001005static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +00001006{
1007 CPUState *env;
1008
1009 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +00001010 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001011}
1012#endif
1013
Luiz Capitulinob223f352009-10-07 13:41:56 -03001014/**
1015 * do_quit(): Quit QEMU execution
1016 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001017static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001018{
1019 exit(0);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001020 return 0;
bellard9dc39cb2004-03-14 21:38:27 +00001021}
1022
aliguori376253e2009-03-05 23:01:23 +00001023static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +00001024{
1025 if (bdrv_is_inserted(bs)) {
1026 if (!force) {
1027 if (!bdrv_is_removable(bs)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001028 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +01001029 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +00001030 return -1;
1031 }
1032 if (bdrv_is_locked(bs)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001033 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +00001034 return -1;
1035 }
1036 }
1037 bdrv_close(bs);
1038 }
1039 return 0;
1040}
1041
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001042static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001043{
1044 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001045 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -02001046 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +00001047
bellard9307c4c2004-04-04 12:57:25 +00001048 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +00001049 if (!bs) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001050 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001051 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001052 }
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001053 return eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001054}
1055
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001056static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001057 QObject **ret_data)
1058{
1059 BlockDriverState *bs;
1060
1061 bs = bdrv_find(qdict_get_str(qdict, "device"));
1062 if (!bs) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001063 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001064 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001065 }
1066
1067 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001068 qerror_report(QERR_INVALID_PASSWORD);
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001069 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001070 }
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001071
1072 return 0;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001073}
1074
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001075static int do_change_block(Monitor *mon, const char *device,
1076 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001077{
1078 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001079 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001080
bellard9307c4c2004-04-04 12:57:25 +00001081 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001082 if (!bs) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001083 qerror_report(QERR_DEVICE_NOT_FOUND, device);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001084 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001085 }
aurel322ecea9b2008-06-18 22:10:01 +00001086 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001087 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001088 if (!drv) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001089 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001090 return -1;
aurel322ecea9b2008-06-18 22:10:01 +00001091 }
1092 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001093 if (eject_device(mon, bs, 0) < 0) {
1094 return -1;
1095 }
1096 if (bdrv_open2(bs, filename, BDRV_O_RDWR, drv) < 0) {
1097 return -1;
1098 }
1099 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001100}
1101
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001102static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001103{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001104 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001105 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001106 return -1;
1107 }
aliguoribb5fc202009-03-05 23:01:15 +00001108
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001109 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001110}
1111
1112static void change_vnc_password_cb(Monitor *mon, const char *password,
1113 void *opaque)
1114{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001115 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001116 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001117}
1118
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001119static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001120{
ths70848512007-08-25 01:37:05 +00001121 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001122 strcmp(target, "password") == 0) {
1123 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001124 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001125 strncpy(password, arg, sizeof(password));
1126 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001127 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001128 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001129 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001130 }
ths70848512007-08-25 01:37:05 +00001131 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001132 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001133 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001134 return -1;
1135 }
ths70848512007-08-25 01:37:05 +00001136 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001137
1138 return 0;
thse25a5822007-08-25 01:36:20 +00001139}
1140
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001141/**
1142 * do_change(): Change a removable medium, or VNC configuration
1143 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001144static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001145{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001146 const char *device = qdict_get_str(qdict, "device");
1147 const char *target = qdict_get_str(qdict, "target");
1148 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001149 int ret;
1150
thse25a5822007-08-25 01:36:20 +00001151 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001152 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001153 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001154 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001155 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001156
1157 return ret;
thse25a5822007-08-25 01:36:20 +00001158}
1159
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001160static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001161{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001162 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001163}
1164
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001165static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001166{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001167 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001168}
1169
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001170static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001171{
1172 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001173 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001174
bellard9307c4c2004-04-04 12:57:25 +00001175 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001176 mask = 0;
1177 } else {
bellard9307c4c2004-04-04 12:57:25 +00001178 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001179 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001180 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001181 return;
1182 }
1183 }
1184 cpu_set_log(mask);
1185}
1186
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001187static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001188{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001189 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001190 if (!option || !strcmp(option, "on")) {
1191 singlestep = 1;
1192 } else if (!strcmp(option, "off")) {
1193 singlestep = 0;
1194 } else {
1195 monitor_printf(mon, "unexpected option %s\n", option);
1196 }
1197}
1198
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001199/**
1200 * do_stop(): Stop VM execution
1201 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001202static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001203{
1204 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001205 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001206}
1207
aliguoribb5fc202009-03-05 23:01:15 +00001208static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001209
aliguori376253e2009-03-05 23:01:23 +00001210struct bdrv_iterate_context {
1211 Monitor *mon;
1212 int err;
1213};
aliguoric0f4ce72009-03-05 23:01:01 +00001214
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001215/**
1216 * do_cont(): Resume emulation.
1217 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001218static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001219{
1220 struct bdrv_iterate_context context = { mon, 0 };
1221
1222 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001223 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001224 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001225 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001226 return 0;
1227 } else {
1228 return -1;
1229 }
bellard8a7ddc32004-03-31 19:00:16 +00001230}
1231
aliguoribb5fc202009-03-05 23:01:15 +00001232static void bdrv_key_cb(void *opaque, int err)
1233{
aliguori376253e2009-03-05 23:01:23 +00001234 Monitor *mon = opaque;
1235
aliguoribb5fc202009-03-05 23:01:15 +00001236 /* another key was set successfully, retry to continue */
1237 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001238 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001239}
1240
1241static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1242{
aliguori376253e2009-03-05 23:01:23 +00001243 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001244
aliguori376253e2009-03-05 23:01:23 +00001245 if (!context->err && bdrv_key_required(bs)) {
1246 context->err = -EBUSY;
1247 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1248 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001249 }
1250}
1251
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001252static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001253{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001254 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001255 if (!device)
1256 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1257 if (gdbserver_start(device) < 0) {
1258 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1259 device);
1260 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001261 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001262 } else {
aliguori59030a82009-04-05 18:43:41 +00001263 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1264 device);
bellard8a7ddc32004-03-31 19:00:16 +00001265 }
1266}
1267
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001268static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001269{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001270 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001271 if (select_watchdog_action(action) == -1) {
1272 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1273 }
1274}
1275
aliguori376253e2009-03-05 23:01:23 +00001276static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001277{
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001279 switch(c) {
1280 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001281 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001282 break;
1283 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001284 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001285 break;
1286 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001290 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 default:
1293 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001294 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001295 } else {
aliguori376253e2009-03-05 23:01:23 +00001296 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001297 }
1298 break;
1299 }
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001301}
1302
aliguori376253e2009-03-05 23:01:23 +00001303static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001304 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001305{
bellard6a00d602005-11-21 23:25:50 +00001306 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001307 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001308 uint8_t buf[16];
1309 uint64_t v;
1310
1311 if (format == 'i') {
1312 int flags;
1313 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001314 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001315#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001316 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001317 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001318 } else if (wsize == 4) {
1319 flags = 0;
1320 } else {
bellard6a15fd12006-04-12 21:07:07 +00001321 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001322 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001323 if (env) {
1324#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001325 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001326 (env->segs[R_CS].flags & DESC_L_MASK))
1327 flags = 2;
1328 else
1329#endif
1330 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1331 flags = 1;
1332 }
bellard4c27ba22004-04-25 18:05:08 +00001333 }
1334#endif
aliguori376253e2009-03-05 23:01:23 +00001335 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001336 return;
1337 }
1338
1339 len = wsize * count;
1340 if (wsize == 1)
1341 line_size = 8;
1342 else
1343 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001344 max_digits = 0;
1345
1346 switch(format) {
1347 case 'o':
1348 max_digits = (wsize * 8 + 2) / 3;
1349 break;
1350 default:
1351 case 'x':
1352 max_digits = (wsize * 8) / 4;
1353 break;
1354 case 'u':
1355 case 'd':
1356 max_digits = (wsize * 8 * 10 + 32) / 33;
1357 break;
1358 case 'c':
1359 wsize = 1;
1360 break;
1361 }
1362
1363 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001364 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001366 else
aliguori376253e2009-03-05 23:01:23 +00001367 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001368 l = len;
1369 if (l > line_size)
1370 l = line_size;
1371 if (is_physical) {
1372 cpu_physical_memory_rw(addr, buf, l, 0);
1373 } else {
bellard6a00d602005-11-21 23:25:50 +00001374 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001375 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001376 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001377 break;
1378 }
bellard9307c4c2004-04-04 12:57:25 +00001379 }
ths5fafdf22007-09-16 21:08:06 +00001380 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001381 while (i < l) {
1382 switch(wsize) {
1383 default:
1384 case 1:
1385 v = ldub_raw(buf + i);
1386 break;
1387 case 2:
1388 v = lduw_raw(buf + i);
1389 break;
1390 case 4:
bellard92a31b12005-02-10 22:00:52 +00001391 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001392 break;
1393 case 8:
1394 v = ldq_raw(buf + i);
1395 break;
1396 }
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001398 switch(format) {
1399 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001400 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001401 break;
1402 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001403 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001404 break;
1405 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001407 break;
1408 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001409 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001410 break;
1411 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001412 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001413 break;
1414 }
1415 i += wsize;
1416 }
aliguori376253e2009-03-05 23:01:23 +00001417 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001418 addr += l;
1419 len -= l;
1420 }
1421}
1422
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001423static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001424{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001425 int count = qdict_get_int(qdict, "count");
1426 int format = qdict_get_int(qdict, "format");
1427 int size = qdict_get_int(qdict, "size");
1428 target_long addr = qdict_get_int(qdict, "addr");
1429
aliguori376253e2009-03-05 23:01:23 +00001430 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001431}
1432
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001433static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001434{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001435 int count = qdict_get_int(qdict, "count");
1436 int format = qdict_get_int(qdict, "format");
1437 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001438 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001439
aliguori376253e2009-03-05 23:01:23 +00001440 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001441}
1442
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001443static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001444{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001445 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001446 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001447
blueswir17743e582007-09-24 18:39:04 +00001448#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001449 switch(format) {
1450 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001451 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001452 break;
1453 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001454 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001455 break;
1456 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001457 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001458 break;
1459 default:
1460 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001461 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001462 break;
1463 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001464 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001465 break;
1466 }
bellard92a31b12005-02-10 22:00:52 +00001467#else
1468 switch(format) {
1469 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001470 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001471 break;
1472 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001474 break;
1475 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001476 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001477 break;
1478 default:
1479 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001480 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001481 break;
1482 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001483 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001484 break;
1485 }
1486#endif
aliguori376253e2009-03-05 23:01:23 +00001487 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001488}
1489
Luiz Capitulino98696222010-02-10 23:49:58 -02001490static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001491{
1492 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001493 uint32_t size = qdict_get_int(qdict, "size");
1494 const char *filename = qdict_get_str(qdict, "filename");
1495 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001496 uint32_t l;
1497 CPUState *env;
1498 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001499 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001500
1501 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001502
1503 f = fopen(filename, "wb");
1504 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001505 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001506 return -1;
bellardb371dc52007-01-03 15:20:39 +00001507 }
1508 while (size != 0) {
1509 l = sizeof(buf);
1510 if (l > size)
1511 l = size;
1512 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001513 if (fwrite(buf, 1, l, f) != l) {
1514 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1515 goto exit;
1516 }
bellardb371dc52007-01-03 15:20:39 +00001517 addr += l;
1518 size -= l;
1519 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001520
1521 ret = 0;
1522
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001523exit:
bellardb371dc52007-01-03 15:20:39 +00001524 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001525 return ret;
bellardb371dc52007-01-03 15:20:39 +00001526}
1527
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001528static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001529 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001530{
1531 FILE *f;
1532 uint32_t l;
1533 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001534 uint32_t size = qdict_get_int(qdict, "size");
1535 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001536 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001537 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001538
1539 f = fopen(filename, "wb");
1540 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001541 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001542 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001543 }
1544 while (size != 0) {
1545 l = sizeof(buf);
1546 if (l > size)
1547 l = size;
1548 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001549 if (fwrite(buf, 1, l, f) != l) {
1550 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1551 goto exit;
1552 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001553 fflush(f);
1554 addr += l;
1555 size -= l;
1556 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001557
1558 ret = 0;
1559
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001560exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001561 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001562 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001563}
1564
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001565static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001566{
1567 uint32_t addr;
1568 uint8_t buf[1];
1569 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001570 uint32_t start = qdict_get_int(qdict, "start");
1571 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001572
1573 sum = 0;
1574 for(addr = start; addr < (start + size); addr++) {
1575 cpu_physical_memory_rw(addr, buf, 1, 0);
1576 /* BSD sum algorithm ('sum' Unix command) */
1577 sum = (sum >> 1) | (sum << 15);
1578 sum += buf[0];
1579 }
aliguori376253e2009-03-05 23:01:23 +00001580 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001581}
1582
bellarda3a91a32004-06-04 11:06:21 +00001583typedef struct {
1584 int keycode;
1585 const char *name;
1586} KeyDef;
1587
1588static const KeyDef key_defs[] = {
1589 { 0x2a, "shift" },
1590 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001591
bellarda3a91a32004-06-04 11:06:21 +00001592 { 0x38, "alt" },
1593 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001594 { 0x64, "altgr" },
1595 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001596 { 0x1d, "ctrl" },
1597 { 0x9d, "ctrl_r" },
1598
1599 { 0xdd, "menu" },
1600
1601 { 0x01, "esc" },
1602
1603 { 0x02, "1" },
1604 { 0x03, "2" },
1605 { 0x04, "3" },
1606 { 0x05, "4" },
1607 { 0x06, "5" },
1608 { 0x07, "6" },
1609 { 0x08, "7" },
1610 { 0x09, "8" },
1611 { 0x0a, "9" },
1612 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001613 { 0x0c, "minus" },
1614 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001615 { 0x0e, "backspace" },
1616
1617 { 0x0f, "tab" },
1618 { 0x10, "q" },
1619 { 0x11, "w" },
1620 { 0x12, "e" },
1621 { 0x13, "r" },
1622 { 0x14, "t" },
1623 { 0x15, "y" },
1624 { 0x16, "u" },
1625 { 0x17, "i" },
1626 { 0x18, "o" },
1627 { 0x19, "p" },
1628
1629 { 0x1c, "ret" },
1630
1631 { 0x1e, "a" },
1632 { 0x1f, "s" },
1633 { 0x20, "d" },
1634 { 0x21, "f" },
1635 { 0x22, "g" },
1636 { 0x23, "h" },
1637 { 0x24, "j" },
1638 { 0x25, "k" },
1639 { 0x26, "l" },
1640
1641 { 0x2c, "z" },
1642 { 0x2d, "x" },
1643 { 0x2e, "c" },
1644 { 0x2f, "v" },
1645 { 0x30, "b" },
1646 { 0x31, "n" },
1647 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001648 { 0x33, "comma" },
1649 { 0x34, "dot" },
1650 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001651
balrog4d3b6f62008-02-10 16:33:14 +00001652 { 0x37, "asterisk" },
1653
bellarda3a91a32004-06-04 11:06:21 +00001654 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001655 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001656 { 0x3b, "f1" },
1657 { 0x3c, "f2" },
1658 { 0x3d, "f3" },
1659 { 0x3e, "f4" },
1660 { 0x3f, "f5" },
1661 { 0x40, "f6" },
1662 { 0x41, "f7" },
1663 { 0x42, "f8" },
1664 { 0x43, "f9" },
1665 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001666 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001667 { 0x46, "scroll_lock" },
1668
bellard64866c32006-05-07 18:03:31 +00001669 { 0xb5, "kp_divide" },
1670 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001671 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001672 { 0x4e, "kp_add" },
1673 { 0x9c, "kp_enter" },
1674 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001675 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001676
1677 { 0x52, "kp_0" },
1678 { 0x4f, "kp_1" },
1679 { 0x50, "kp_2" },
1680 { 0x51, "kp_3" },
1681 { 0x4b, "kp_4" },
1682 { 0x4c, "kp_5" },
1683 { 0x4d, "kp_6" },
1684 { 0x47, "kp_7" },
1685 { 0x48, "kp_8" },
1686 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001687
bellarda3a91a32004-06-04 11:06:21 +00001688 { 0x56, "<" },
1689
1690 { 0x57, "f11" },
1691 { 0x58, "f12" },
1692
1693 { 0xb7, "print" },
1694
1695 { 0xc7, "home" },
1696 { 0xc9, "pgup" },
1697 { 0xd1, "pgdn" },
1698 { 0xcf, "end" },
1699
1700 { 0xcb, "left" },
1701 { 0xc8, "up" },
1702 { 0xd0, "down" },
1703 { 0xcd, "right" },
1704
1705 { 0xd2, "insert" },
1706 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001707#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1708 { 0xf0, "stop" },
1709 { 0xf1, "again" },
1710 { 0xf2, "props" },
1711 { 0xf3, "undo" },
1712 { 0xf4, "front" },
1713 { 0xf5, "copy" },
1714 { 0xf6, "open" },
1715 { 0xf7, "paste" },
1716 { 0xf8, "find" },
1717 { 0xf9, "cut" },
1718 { 0xfa, "lf" },
1719 { 0xfb, "help" },
1720 { 0xfc, "meta_l" },
1721 { 0xfd, "meta_r" },
1722 { 0xfe, "compose" },
1723#endif
bellarda3a91a32004-06-04 11:06:21 +00001724 { 0, NULL },
1725};
1726
1727static int get_keycode(const char *key)
1728{
1729 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001730 char *endp;
1731 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001732
1733 for(p = key_defs; p->name != NULL; p++) {
1734 if (!strcmp(key, p->name))
1735 return p->keycode;
1736 }
bellard64866c32006-05-07 18:03:31 +00001737 if (strstart(key, "0x", NULL)) {
1738 ret = strtoul(key, &endp, 0);
1739 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1740 return ret;
1741 }
bellarda3a91a32004-06-04 11:06:21 +00001742 return -1;
1743}
1744
balrogc8256f92008-06-08 22:45:01 +00001745#define MAX_KEYCODES 16
1746static uint8_t keycodes[MAX_KEYCODES];
1747static int nb_pending_keycodes;
1748static QEMUTimer *key_timer;
1749
1750static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001751{
balrogc8256f92008-06-08 22:45:01 +00001752 int keycode;
1753
1754 while (nb_pending_keycodes > 0) {
1755 nb_pending_keycodes--;
1756 keycode = keycodes[nb_pending_keycodes];
1757 if (keycode & 0x80)
1758 kbd_put_keycode(0xe0);
1759 kbd_put_keycode(keycode | 0x80);
1760 }
1761}
1762
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001763static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001764{
balrog3401c0d2008-06-04 10:05:59 +00001765 char keyname_buf[16];
1766 char *separator;
1767 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001768 const char *string = qdict_get_str(qdict, "string");
1769 int has_hold_time = qdict_haskey(qdict, "hold_time");
1770 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001771
balrogc8256f92008-06-08 22:45:01 +00001772 if (nb_pending_keycodes > 0) {
1773 qemu_del_timer(key_timer);
1774 release_keys(NULL);
1775 }
1776 if (!has_hold_time)
1777 hold_time = 100;
1778 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001779 while (1) {
1780 separator = strchr(string, '-');
1781 keyname_len = separator ? separator - string : strlen(string);
1782 if (keyname_len > 0) {
1783 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1784 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001785 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001786 return;
bellarda3a91a32004-06-04 11:06:21 +00001787 }
balrogc8256f92008-06-08 22:45:01 +00001788 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001789 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001790 return;
1791 }
1792 keyname_buf[keyname_len] = 0;
1793 keycode = get_keycode(keyname_buf);
1794 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001795 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001796 return;
1797 }
balrogc8256f92008-06-08 22:45:01 +00001798 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001799 }
balrog3401c0d2008-06-04 10:05:59 +00001800 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001801 break;
balrog3401c0d2008-06-04 10:05:59 +00001802 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001803 }
balrogc8256f92008-06-08 22:45:01 +00001804 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001805 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001806 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001807 keycode = keycodes[i];
1808 if (keycode & 0x80)
1809 kbd_put_keycode(0xe0);
1810 kbd_put_keycode(keycode & 0x7f);
1811 }
balrogc8256f92008-06-08 22:45:01 +00001812 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001813 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001814 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001815}
1816
bellard13224a82006-07-14 22:03:35 +00001817static int mouse_button_state;
1818
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001819static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001820{
1821 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001822 const char *dx_str = qdict_get_str(qdict, "dx_str");
1823 const char *dy_str = qdict_get_str(qdict, "dy_str");
1824 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001825 dx = strtol(dx_str, NULL, 0);
1826 dy = strtol(dy_str, NULL, 0);
1827 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001828 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001829 dz = strtol(dz_str, NULL, 0);
1830 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1831}
1832
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001833static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001834{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001835 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001836 mouse_button_state = button_state;
1837 kbd_mouse_event(0, 0, 0, mouse_button_state);
1838}
1839
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001840static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001841{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001842 int size = qdict_get_int(qdict, "size");
1843 int addr = qdict_get_int(qdict, "addr");
1844 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001845 uint32_t val;
1846 int suffix;
1847
1848 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001849 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001850 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001851 addr++;
1852 }
1853 addr &= 0xffff;
1854
1855 switch(size) {
1856 default:
1857 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001858 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001859 suffix = 'b';
1860 break;
1861 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001862 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001863 suffix = 'w';
1864 break;
1865 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001866 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001867 suffix = 'l';
1868 break;
1869 }
aliguori376253e2009-03-05 23:01:23 +00001870 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1871 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001872}
bellarda3a91a32004-06-04 11:06:21 +00001873
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001874static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001875{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001876 int size = qdict_get_int(qdict, "size");
1877 int addr = qdict_get_int(qdict, "addr");
1878 int val = qdict_get_int(qdict, "val");
1879
Jan Kiszkaf1147842009-07-14 10:20:11 +02001880 addr &= IOPORTS_MASK;
1881
1882 switch (size) {
1883 default:
1884 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001885 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001886 break;
1887 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001888 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001889 break;
1890 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001891 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001892 break;
1893 }
1894}
1895
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001896static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001897{
1898 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001899 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001900
Jan Kiszka76e30d02009-07-02 00:19:02 +02001901 res = qemu_boot_set(bootdevice);
1902 if (res == 0) {
1903 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1904 } else if (res > 0) {
1905 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001906 } else {
aliguori376253e2009-03-05 23:01:23 +00001907 monitor_printf(mon, "no function defined to set boot device list for "
1908 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001909 }
1910}
1911
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001912/**
1913 * do_system_reset(): Issue a machine reset
1914 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001915static int do_system_reset(Monitor *mon, const QDict *qdict,
1916 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001917{
1918 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001919 return 0;
bellarde4f90822004-06-20 12:35:44 +00001920}
1921
Luiz Capitulino43076662009-10-07 13:41:59 -03001922/**
1923 * do_system_powerdown(): Issue a machine powerdown
1924 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001925static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1926 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001927{
1928 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001929 return 0;
bellard34751872005-07-02 14:31:34 +00001930}
1931
bellardb86bda52004-09-18 19:32:46 +00001932#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001933static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001934{
aliguori376253e2009-03-05 23:01:23 +00001935 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1936 addr,
1937 pte & mask,
1938 pte & PG_GLOBAL_MASK ? 'G' : '-',
1939 pte & PG_PSE_MASK ? 'P' : '-',
1940 pte & PG_DIRTY_MASK ? 'D' : '-',
1941 pte & PG_ACCESSED_MASK ? 'A' : '-',
1942 pte & PG_PCD_MASK ? 'C' : '-',
1943 pte & PG_PWT_MASK ? 'T' : '-',
1944 pte & PG_USER_MASK ? 'U' : '-',
1945 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001946}
1947
aliguori376253e2009-03-05 23:01:23 +00001948static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001949{
bellard6a00d602005-11-21 23:25:50 +00001950 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001951 int l1, l2;
1952 uint32_t pgd, pde, pte;
1953
bellard6a00d602005-11-21 23:25:50 +00001954 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001955
bellardb86bda52004-09-18 19:32:46 +00001956 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001957 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001958 return;
1959 }
1960 pgd = env->cr[3] & ~0xfff;
1961 for(l1 = 0; l1 < 1024; l1++) {
1962 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1963 pde = le32_to_cpu(pde);
1964 if (pde & PG_PRESENT_MASK) {
1965 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001966 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001967 } else {
1968 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001969 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001970 (uint8_t *)&pte, 4);
1971 pte = le32_to_cpu(pte);
1972 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001973 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001974 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001975 ~0xfff);
1976 }
1977 }
1978 }
1979 }
1980 }
1981}
1982
aliguori376253e2009-03-05 23:01:23 +00001983static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001984 uint32_t end, int prot)
1985{
bellard9746b152004-11-11 18:30:24 +00001986 int prot1;
1987 prot1 = *plast_prot;
1988 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001989 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001990 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1991 *pstart, end, end - *pstart,
1992 prot1 & PG_USER_MASK ? 'u' : '-',
1993 'r',
1994 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001995 }
1996 if (prot != 0)
1997 *pstart = end;
1998 else
1999 *pstart = -1;
2000 *plast_prot = prot;
2001 }
2002}
2003
aliguori376253e2009-03-05 23:01:23 +00002004static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00002005{
bellard6a00d602005-11-21 23:25:50 +00002006 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00002007 int l1, l2, prot, last_prot;
2008 uint32_t pgd, pde, pte, start, end;
2009
bellard6a00d602005-11-21 23:25:50 +00002010 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002011
bellardb86bda52004-09-18 19:32:46 +00002012 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00002013 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00002014 return;
2015 }
2016 pgd = env->cr[3] & ~0xfff;
2017 last_prot = 0;
2018 start = -1;
2019 for(l1 = 0; l1 < 1024; l1++) {
2020 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
2021 pde = le32_to_cpu(pde);
2022 end = l1 << 22;
2023 if (pde & PG_PRESENT_MASK) {
2024 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2025 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002026 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002027 } else {
2028 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00002029 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00002030 (uint8_t *)&pte, 4);
2031 pte = le32_to_cpu(pte);
2032 end = (l1 << 22) + (l2 << 12);
2033 if (pte & PG_PRESENT_MASK) {
2034 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2035 } else {
2036 prot = 0;
2037 }
aliguori376253e2009-03-05 23:01:23 +00002038 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002039 }
2040 }
2041 } else {
2042 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002043 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002044 }
2045 }
2046}
2047#endif
2048
aurel327c664e22009-03-03 06:12:22 +00002049#if defined(TARGET_SH4)
2050
aliguori376253e2009-03-05 23:01:23 +00002051static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002052{
aliguori376253e2009-03-05 23:01:23 +00002053 monitor_printf(mon, " tlb%i:\t"
2054 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2055 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2056 "dirty=%hhu writethrough=%hhu\n",
2057 idx,
2058 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2059 tlb->v, tlb->sh, tlb->c, tlb->pr,
2060 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002061}
2062
aliguori376253e2009-03-05 23:01:23 +00002063static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002064{
2065 CPUState *env = mon_get_cpu();
2066 int i;
2067
aliguori376253e2009-03-05 23:01:23 +00002068 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002069 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002070 print_tlb (mon, i, &env->itlb[i]);
2071 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002072 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002073 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002074}
2075
2076#endif
2077
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002078static void do_info_kvm_print(Monitor *mon, const QObject *data)
2079{
2080 QDict *qdict;
2081
2082 qdict = qobject_to_qdict(data);
2083
2084 monitor_printf(mon, "kvm support: ");
2085 if (qdict_get_bool(qdict, "present")) {
2086 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2087 "enabled" : "disabled");
2088 } else {
2089 monitor_printf(mon, "not compiled\n");
2090 }
2091}
2092
2093/**
2094 * do_info_kvm(): Show KVM information
2095 *
2096 * Return a QDict with the following information:
2097 *
2098 * - "enabled": true if KVM support is enabled, false otherwise
2099 * - "present": true if QEMU has KVM support, false otherwise
2100 *
2101 * Example:
2102 *
2103 * { "enabled": true, "present": true }
2104 */
2105static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002106{
2107#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002108 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2109 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002110#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002111 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002112#endif
2113}
2114
aliguori030ea372009-04-21 22:30:47 +00002115static void do_info_numa(Monitor *mon)
2116{
aliguorib28b6232009-04-22 20:20:29 +00002117 int i;
aliguori030ea372009-04-21 22:30:47 +00002118 CPUState *env;
2119
2120 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2121 for (i = 0; i < nb_numa_nodes; i++) {
2122 monitor_printf(mon, "node %d cpus:", i);
2123 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2124 if (env->numa_node == i) {
2125 monitor_printf(mon, " %d", env->cpu_index);
2126 }
2127 }
2128 monitor_printf(mon, "\n");
2129 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2130 node_mem[i] >> 20);
2131 }
2132}
2133
bellard5f1ce942006-02-08 22:40:15 +00002134#ifdef CONFIG_PROFILER
2135
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002136int64_t qemu_time;
2137int64_t dev_time;
2138
aliguori376253e2009-03-05 23:01:23 +00002139static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002140{
2141 int64_t total;
2142 total = qemu_time;
2143 if (total == 0)
2144 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002145 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002146 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002147 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002148 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002149 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002150 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002151}
2152#else
aliguori376253e2009-03-05 23:01:23 +00002153static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002154{
aliguori376253e2009-03-05 23:01:23 +00002155 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002156}
2157#endif
2158
bellardec36b692006-07-16 18:57:03 +00002159/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002160static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002161
aliguori376253e2009-03-05 23:01:23 +00002162static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002163{
2164 int i;
2165 CaptureState *s;
2166
2167 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002168 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002169 s->ops.info (s->opaque);
2170 }
2171}
2172
Blue Swirl23130862009-06-06 08:22:04 +00002173#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002174static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002175{
2176 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002177 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002178 CaptureState *s;
2179
2180 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2181 if (i == n) {
2182 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002183 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002184 qemu_free (s);
2185 return;
2186 }
2187 }
2188}
2189
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002190static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002191{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002192 const char *path = qdict_get_str(qdict, "path");
2193 int has_freq = qdict_haskey(qdict, "freq");
2194 int freq = qdict_get_try_int(qdict, "freq", -1);
2195 int has_bits = qdict_haskey(qdict, "bits");
2196 int bits = qdict_get_try_int(qdict, "bits", -1);
2197 int has_channels = qdict_haskey(qdict, "nchannels");
2198 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002199 CaptureState *s;
2200
2201 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002202
2203 freq = has_freq ? freq : 44100;
2204 bits = has_bits ? bits : 16;
2205 nchannels = has_channels ? nchannels : 2;
2206
2207 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002208 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002209 qemu_free (s);
2210 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002211 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002212}
2213#endif
2214
aurel32dc1c0b72008-04-27 23:52:12 +00002215#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002216static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002217{
2218 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002219 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002220
2221 for (env = first_cpu; env != NULL; env = env->next_cpu)
2222 if (env->cpu_index == cpu_index) {
2223 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2224 break;
2225 }
2226}
2227#endif
2228
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002229static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002230{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002231 QDict *qdict;
2232
2233 qdict = qobject_to_qdict(data);
2234
2235 monitor_printf(mon, "VM status: ");
2236 if (qdict_get_bool(qdict, "running")) {
2237 monitor_printf(mon, "running");
2238 if (qdict_get_bool(qdict, "singlestep")) {
2239 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002240 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002241 } else {
2242 monitor_printf(mon, "paused");
2243 }
2244
2245 monitor_printf(mon, "\n");
2246}
2247
2248/**
2249 * do_info_status(): VM status
2250 *
2251 * Return a QDict with the following information:
2252 *
2253 * - "running": true if the VM is running, or false if it is paused
2254 * - "singlestep": true if the VM is in single step mode, false otherwise
2255 *
2256 * Example:
2257 *
2258 * { "running": true, "singlestep": false }
2259 */
2260static void do_info_status(Monitor *mon, QObject **ret_data)
2261{
2262 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2263 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002264}
2265
Adam Litke625a5be2010-01-26 14:17:35 -06002266static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002267{
Adam Litke625a5be2010-01-26 14:17:35 -06002268 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002269
Adam Litke625a5be2010-01-26 14:17:35 -06002270 if (strcmp(key, "actual"))
2271 monitor_printf(mon, ",%s=%" PRId64, key,
2272 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002273}
2274
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002275static void monitor_print_balloon(Monitor *mon, const QObject *data)
2276{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002277 QDict *qdict;
2278
2279 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002280 if (!qdict_haskey(qdict, "actual"))
2281 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002282
Adam Litke625a5be2010-01-26 14:17:35 -06002283 monitor_printf(mon, "balloon: actual=%" PRId64,
2284 qdict_get_int(qdict, "actual") >> 20);
2285 qdict_iter(qdict, print_balloon_stat, mon);
2286 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002287}
2288
2289/**
2290 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002291 *
Adam Litke625a5be2010-01-26 14:17:35 -06002292 * Make an asynchronous request for balloon info. When the request completes
2293 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002294 *
Adam Litke625a5be2010-01-26 14:17:35 -06002295 * - "actual": current balloon value in bytes
2296 * The following fields may or may not be present:
2297 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2298 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2299 * - "major_page_faults": Number of major faults
2300 * - "minor_page_faults": Number of minor faults
2301 * - "free_mem": Total amount of free and unused memory (bytes)
2302 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002303 *
2304 * Example:
2305 *
Adam Litke625a5be2010-01-26 14:17:35 -06002306 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2307 * "major_page_faults": 142, "minor_page_faults": 239245,
2308 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002309 */
Adam Litke625a5be2010-01-26 14:17:35 -06002310static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002311{
Adam Litke625a5be2010-01-26 14:17:35 -06002312 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002313
Adam Litke625a5be2010-01-26 14:17:35 -06002314 if (kvm_enabled() && !kvm_has_sync_mmu()) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002315 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002316 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002317 }
Adam Litke625a5be2010-01-26 14:17:35 -06002318
2319 ret = qemu_balloon_status(cb, opaque);
2320 if (!ret) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002321 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002322 return -1;
2323 }
2324
2325 return 0;
2326}
2327
2328/**
2329 * do_balloon(): Request VM to change its memory allocation
2330 */
2331static int do_balloon(Monitor *mon, const QDict *params,
2332 MonitorCompletion cb, void *opaque)
2333{
2334 int ret;
2335
2336 if (kvm_enabled() && !kvm_has_sync_mmu()) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002337 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002338 return -1;
2339 }
2340
2341 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2342 if (ret == 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002343 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002344 return -1;
2345 }
2346
Adam Litke93d67ee2010-02-22 10:51:20 -06002347 cb(opaque, NULL);
Adam Litke625a5be2010-01-26 14:17:35 -06002348 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002349}
2350
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002351static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002352{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002353 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002354
aliguori76655d62009-03-06 20:27:37 +00002355 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002356 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002357 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002358 return acl;
2359}
aliguori76655d62009-03-06 20:27:37 +00002360
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002361static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002362{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002363 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002364 qemu_acl *acl = find_acl(mon, aclname);
2365 qemu_acl_entry *entry;
2366 int i = 0;
2367
2368 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002369 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002370 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002371 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002372 i++;
2373 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002374 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002375 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002376 }
2377}
2378
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002379static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002380{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002381 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002382 qemu_acl *acl = find_acl(mon, aclname);
2383
2384 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002385 qemu_acl_reset(acl);
2386 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002387 }
2388}
aliguori76655d62009-03-06 20:27:37 +00002389
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002390static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002391{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002392 const char *aclname = qdict_get_str(qdict, "aclname");
2393 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002394 qemu_acl *acl = find_acl(mon, aclname);
2395
2396 if (acl) {
2397 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002398 acl->defaultDeny = 0;
2399 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002400 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002401 acl->defaultDeny = 1;
2402 monitor_printf(mon, "acl: policy set to 'deny'\n");
2403 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002404 monitor_printf(mon, "acl: unknown policy '%s', "
2405 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002406 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002407 }
2408}
aliguori76655d62009-03-06 20:27:37 +00002409
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002410static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002411{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002412 const char *aclname = qdict_get_str(qdict, "aclname");
2413 const char *match = qdict_get_str(qdict, "match");
2414 const char *policy = qdict_get_str(qdict, "policy");
2415 int has_index = qdict_haskey(qdict, "index");
2416 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002417 qemu_acl *acl = find_acl(mon, aclname);
2418 int deny, ret;
2419
2420 if (acl) {
2421 if (strcmp(policy, "allow") == 0) {
2422 deny = 0;
2423 } else if (strcmp(policy, "deny") == 0) {
2424 deny = 1;
2425 } else {
2426 monitor_printf(mon, "acl: unknown policy '%s', "
2427 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002428 return;
2429 }
aliguori28a76be2009-03-06 20:27:40 +00002430 if (has_index)
2431 ret = qemu_acl_insert(acl, deny, match, index);
2432 else
2433 ret = qemu_acl_append(acl, deny, match);
2434 if (ret < 0)
2435 monitor_printf(mon, "acl: unable to add acl entry\n");
2436 else
2437 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002438 }
2439}
aliguori76655d62009-03-06 20:27:37 +00002440
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002441static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002442{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002443 const char *aclname = qdict_get_str(qdict, "aclname");
2444 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002445 qemu_acl *acl = find_acl(mon, aclname);
2446 int ret;
aliguori76655d62009-03-06 20:27:37 +00002447
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002448 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002449 ret = qemu_acl_remove(acl, match);
2450 if (ret < 0)
2451 monitor_printf(mon, "acl: no matching acl entry\n");
2452 else
2453 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002454 }
2455}
2456
Huang Ying79c4f6b2009-06-23 10:05:14 +08002457#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002458static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002459{
2460 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002461 int cpu_index = qdict_get_int(qdict, "cpu_index");
2462 int bank = qdict_get_int(qdict, "bank");
2463 uint64_t status = qdict_get_int(qdict, "status");
2464 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2465 uint64_t addr = qdict_get_int(qdict, "addr");
2466 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002467
2468 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2469 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2470 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2471 break;
2472 }
2473}
2474#endif
2475
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002476static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002477{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002478 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002479 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002480 int fd;
2481
2482 fd = qemu_chr_get_msgfd(mon->chr);
2483 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002484 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002485 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002486 }
2487
2488 if (qemu_isdigit(fdname[0])) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002489 qerror_report(QERR_INVALID_PARAMETER, "fdname");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002490 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002491 }
2492
2493 fd = dup(fd);
2494 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002495 if (errno == EMFILE)
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002496 qerror_report(QERR_TOO_MANY_FILES);
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002497 else
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002498 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002499 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002500 }
2501
Blue Swirl72cf2d42009-09-12 07:36:22 +00002502 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002503 if (strcmp(monfd->name, fdname) != 0) {
2504 continue;
2505 }
2506
2507 close(monfd->fd);
2508 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002509 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002510 }
2511
Anthony Liguoric227f092009-10-01 16:12:16 -05002512 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002513 monfd->name = qemu_strdup(fdname);
2514 monfd->fd = fd;
2515
Blue Swirl72cf2d42009-09-12 07:36:22 +00002516 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002517 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002518}
2519
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002520static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002521{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002522 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002523 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002524
Blue Swirl72cf2d42009-09-12 07:36:22 +00002525 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002526 if (strcmp(monfd->name, fdname) != 0) {
2527 continue;
2528 }
2529
Blue Swirl72cf2d42009-09-12 07:36:22 +00002530 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002531 close(monfd->fd);
2532 qemu_free(monfd->name);
2533 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002534 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002535 }
2536
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002537 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002538 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002539}
2540
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002541static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002542{
2543 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002544 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002545
2546 vm_stop(0);
2547
Markus Armbruster03cd4652010-02-17 16:24:10 +01002548 if (load_vmstate(name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002549 vm_start();
2550}
2551
Mark McLoughlin7768e042009-07-22 09:11:41 +01002552int monitor_get_fd(Monitor *mon, const char *fdname)
2553{
Anthony Liguoric227f092009-10-01 16:12:16 -05002554 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002555
Blue Swirl72cf2d42009-09-12 07:36:22 +00002556 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002557 int fd;
2558
2559 if (strcmp(monfd->name, fdname) != 0) {
2560 continue;
2561 }
2562
2563 fd = monfd->fd;
2564
2565 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002566 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002567 qemu_free(monfd->name);
2568 qemu_free(monfd);
2569
2570 return fd;
2571 }
2572
2573 return -1;
2574}
2575
Anthony Liguoric227f092009-10-01 16:12:16 -05002576static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002577#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002578 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002579};
2580
Blue Swirl23130862009-06-06 08:22:04 +00002581/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002582static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 {
2584 .name = "version",
2585 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 .params = "",
2587 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002588 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002589 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 },
2591 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002592 .name = "commands",
2593 .args_type = "",
2594 .params = "",
2595 .help = "list QMP available commands",
2596 .user_print = monitor_user_noop,
2597 .mhandler.info_new = do_info_commands,
2598 },
2599 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002600 .name = "network",
2601 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 .params = "",
2603 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002604 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002605 },
2606 {
2607 .name = "chardev",
2608 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002609 .params = "",
2610 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002611 .user_print = qemu_chr_info_print,
2612 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002613 },
2614 {
2615 .name = "block",
2616 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 .params = "",
2618 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002619 .user_print = bdrv_info_print,
2620 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 },
2622 {
2623 .name = "blockstats",
2624 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 .params = "",
2626 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002627 .user_print = bdrv_stats_print,
2628 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 },
2630 {
2631 .name = "registers",
2632 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 .params = "",
2634 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002635 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002636 },
2637 {
2638 .name = "cpus",
2639 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 .params = "",
2641 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002642 .user_print = monitor_print_cpus,
2643 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 },
2645 {
2646 .name = "history",
2647 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002648 .params = "",
2649 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002650 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002651 },
2652 {
2653 .name = "irq",
2654 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002655 .params = "",
2656 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002657 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 },
2659 {
2660 .name = "pic",
2661 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 .params = "",
2663 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002664 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002665 },
2666 {
2667 .name = "pci",
2668 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002669 .params = "",
2670 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002671 .user_print = do_pci_info_print,
2672 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 },
aurel327c664e22009-03-03 06:12:22 +00002674#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 {
2676 .name = "tlb",
2677 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002678 .params = "",
2679 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002680 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 },
aurel327c664e22009-03-03 06:12:22 +00002682#endif
2683#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 {
2685 .name = "mem",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002689 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 },
2691 {
2692 .name = "hpet",
2693 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002694 .params = "",
2695 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002696 .user_print = do_info_hpet_print,
2697 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
bellardb86bda52004-09-18 19:32:46 +00002699#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002700 {
2701 .name = "jit",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002705 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002706 },
2707 {
2708 .name = "kvm",
2709 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 .params = "",
2711 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002712 .user_print = do_info_kvm_print,
2713 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 },
2715 {
2716 .name = "numa",
2717 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 .params = "",
2719 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002720 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 },
2722 {
2723 .name = "usb",
2724 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 .params = "",
2726 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002727 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 },
2729 {
2730 .name = "usbhost",
2731 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002732 .params = "",
2733 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002734 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 },
2736 {
2737 .name = "profile",
2738 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002739 .params = "",
2740 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002741 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 },
2743 {
2744 .name = "capture",
2745 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002746 .params = "",
2747 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002748 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 },
2750 {
2751 .name = "snapshots",
2752 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 .params = "",
2754 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002755 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002756 },
2757 {
2758 .name = "status",
2759 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 .params = "",
2761 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002762 .user_print = do_info_status_print,
2763 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 },
2765 {
2766 .name = "pcmcia",
2767 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 .params = "",
2769 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002770 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 },
2772 {
2773 .name = "mice",
2774 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002775 .params = "",
2776 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002777 .user_print = do_info_mice_print,
2778 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002779 },
2780 {
2781 .name = "vnc",
2782 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002783 .params = "",
2784 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002785 .user_print = do_info_vnc_print,
2786 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002787 },
2788 {
2789 .name = "name",
2790 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002791 .params = "",
2792 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002793 .user_print = do_info_name_print,
2794 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 },
2796 {
2797 .name = "uuid",
2798 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .params = "",
2800 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002801 .user_print = do_info_uuid_print,
2802 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 },
j_mayer76a66252007-03-07 08:32:30 +00002804#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002805 {
2806 .name = "cpustats",
2807 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002808 .params = "",
2809 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002810 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 },
j_mayer76a66252007-03-07 08:32:30 +00002812#endif
blueswir131a60e22007-10-26 18:42:59 +00002813#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002814 {
2815 .name = "usernet",
2816 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002817 .params = "",
2818 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002819 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 },
blueswir131a60e22007-10-26 18:42:59 +00002821#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 {
2823 .name = "migrate",
2824 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002825 .params = "",
2826 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002827 .user_print = do_info_migrate_print,
2828 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002829 },
2830 {
2831 .name = "balloon",
2832 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002833 .params = "",
2834 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002835 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002836 .mhandler.info_async = do_info_balloon,
2837 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 },
2839 {
2840 .name = "qtree",
2841 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002842 .params = "",
2843 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002844 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 },
2846 {
2847 .name = "qdm",
2848 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002849 .params = "",
2850 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002851 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002852 },
2853 {
2854 .name = "roms",
2855 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002856 .params = "",
2857 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002858 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002859 },
2860 {
2861 .name = NULL,
2862 },
bellard9dc39cb2004-03-14 21:38:27 +00002863};
2864
bellard9307c4c2004-04-04 12:57:25 +00002865/*******************************************************************/
2866
2867static const char *pch;
2868static jmp_buf expr_env;
2869
bellard92a31b12005-02-10 22:00:52 +00002870#define MD_TLONG 0
2871#define MD_I32 1
2872
bellard9307c4c2004-04-04 12:57:25 +00002873typedef struct MonitorDef {
2874 const char *name;
2875 int offset;
blueswir18662d652008-10-02 18:32:44 +00002876 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002877 int type;
bellard9307c4c2004-04-04 12:57:25 +00002878} MonitorDef;
2879
bellard57206fd2004-04-25 18:54:52 +00002880#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002881static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002882{
bellard6a00d602005-11-21 23:25:50 +00002883 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002884 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002885}
2886#endif
2887
bellarda541f292004-04-12 20:39:29 +00002888#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002889static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002890{
bellard6a00d602005-11-21 23:25:50 +00002891 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002892 unsigned int u;
2893 int i;
2894
2895 u = 0;
2896 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002897 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002898
2899 return u;
2900}
2901
blueswir18662d652008-10-02 18:32:44 +00002902static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002903{
bellard6a00d602005-11-21 23:25:50 +00002904 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002905 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002906}
2907
blueswir18662d652008-10-02 18:32:44 +00002908static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002909{
bellard6a00d602005-11-21 23:25:50 +00002910 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002911 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002912}
bellard9fddaa02004-05-21 12:59:32 +00002913
blueswir18662d652008-10-02 18:32:44 +00002914static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002915{
bellard6a00d602005-11-21 23:25:50 +00002916 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002917 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002918}
2919
blueswir18662d652008-10-02 18:32:44 +00002920static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002921{
bellard6a00d602005-11-21 23:25:50 +00002922 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002923 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002924}
2925
blueswir18662d652008-10-02 18:32:44 +00002926static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002927{
bellard6a00d602005-11-21 23:25:50 +00002928 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002929 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002930}
bellarda541f292004-04-12 20:39:29 +00002931#endif
2932
bellarde95c8d52004-09-30 22:22:08 +00002933#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002934#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002935static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002936{
bellard6a00d602005-11-21 23:25:50 +00002937 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002938 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002939}
bellard7b936c02005-10-30 17:05:13 +00002940#endif
bellarde95c8d52004-09-30 22:22:08 +00002941
blueswir18662d652008-10-02 18:32:44 +00002942static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002943{
bellard6a00d602005-11-21 23:25:50 +00002944 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002945 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002946}
2947#endif
2948
blueswir18662d652008-10-02 18:32:44 +00002949static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002950#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002951
2952#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002953 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002954 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002955 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002956
bellard9307c4c2004-04-04 12:57:25 +00002957 { "eax", offsetof(CPUState, regs[0]) },
2958 { "ecx", offsetof(CPUState, regs[1]) },
2959 { "edx", offsetof(CPUState, regs[2]) },
2960 { "ebx", offsetof(CPUState, regs[3]) },
2961 { "esp|sp", offsetof(CPUState, regs[4]) },
2962 { "ebp|fp", offsetof(CPUState, regs[5]) },
2963 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002964 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002965#ifdef TARGET_X86_64
2966 { "r8", offsetof(CPUState, regs[8]) },
2967 { "r9", offsetof(CPUState, regs[9]) },
2968 { "r10", offsetof(CPUState, regs[10]) },
2969 { "r11", offsetof(CPUState, regs[11]) },
2970 { "r12", offsetof(CPUState, regs[12]) },
2971 { "r13", offsetof(CPUState, regs[13]) },
2972 { "r14", offsetof(CPUState, regs[14]) },
2973 { "r15", offsetof(CPUState, regs[15]) },
2974#endif
bellard9307c4c2004-04-04 12:57:25 +00002975 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002976 { "eip", offsetof(CPUState, eip) },
2977 SEG("cs", R_CS)
2978 SEG("ds", R_DS)
2979 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002980 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002981 SEG("fs", R_FS)
2982 SEG("gs", R_GS)
2983 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002984#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002985 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002986 { "r0", offsetof(CPUState, gpr[0]) },
2987 { "r1", offsetof(CPUState, gpr[1]) },
2988 { "r2", offsetof(CPUState, gpr[2]) },
2989 { "r3", offsetof(CPUState, gpr[3]) },
2990 { "r4", offsetof(CPUState, gpr[4]) },
2991 { "r5", offsetof(CPUState, gpr[5]) },
2992 { "r6", offsetof(CPUState, gpr[6]) },
2993 { "r7", offsetof(CPUState, gpr[7]) },
2994 { "r8", offsetof(CPUState, gpr[8]) },
2995 { "r9", offsetof(CPUState, gpr[9]) },
2996 { "r10", offsetof(CPUState, gpr[10]) },
2997 { "r11", offsetof(CPUState, gpr[11]) },
2998 { "r12", offsetof(CPUState, gpr[12]) },
2999 { "r13", offsetof(CPUState, gpr[13]) },
3000 { "r14", offsetof(CPUState, gpr[14]) },
3001 { "r15", offsetof(CPUState, gpr[15]) },
3002 { "r16", offsetof(CPUState, gpr[16]) },
3003 { "r17", offsetof(CPUState, gpr[17]) },
3004 { "r18", offsetof(CPUState, gpr[18]) },
3005 { "r19", offsetof(CPUState, gpr[19]) },
3006 { "r20", offsetof(CPUState, gpr[20]) },
3007 { "r21", offsetof(CPUState, gpr[21]) },
3008 { "r22", offsetof(CPUState, gpr[22]) },
3009 { "r23", offsetof(CPUState, gpr[23]) },
3010 { "r24", offsetof(CPUState, gpr[24]) },
3011 { "r25", offsetof(CPUState, gpr[25]) },
3012 { "r26", offsetof(CPUState, gpr[26]) },
3013 { "r27", offsetof(CPUState, gpr[27]) },
3014 { "r28", offsetof(CPUState, gpr[28]) },
3015 { "r29", offsetof(CPUState, gpr[29]) },
3016 { "r30", offsetof(CPUState, gpr[30]) },
3017 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003018 /* Floating point registers */
3019 { "f0", offsetof(CPUState, fpr[0]) },
3020 { "f1", offsetof(CPUState, fpr[1]) },
3021 { "f2", offsetof(CPUState, fpr[2]) },
3022 { "f3", offsetof(CPUState, fpr[3]) },
3023 { "f4", offsetof(CPUState, fpr[4]) },
3024 { "f5", offsetof(CPUState, fpr[5]) },
3025 { "f6", offsetof(CPUState, fpr[6]) },
3026 { "f7", offsetof(CPUState, fpr[7]) },
3027 { "f8", offsetof(CPUState, fpr[8]) },
3028 { "f9", offsetof(CPUState, fpr[9]) },
3029 { "f10", offsetof(CPUState, fpr[10]) },
3030 { "f11", offsetof(CPUState, fpr[11]) },
3031 { "f12", offsetof(CPUState, fpr[12]) },
3032 { "f13", offsetof(CPUState, fpr[13]) },
3033 { "f14", offsetof(CPUState, fpr[14]) },
3034 { "f15", offsetof(CPUState, fpr[15]) },
3035 { "f16", offsetof(CPUState, fpr[16]) },
3036 { "f17", offsetof(CPUState, fpr[17]) },
3037 { "f18", offsetof(CPUState, fpr[18]) },
3038 { "f19", offsetof(CPUState, fpr[19]) },
3039 { "f20", offsetof(CPUState, fpr[20]) },
3040 { "f21", offsetof(CPUState, fpr[21]) },
3041 { "f22", offsetof(CPUState, fpr[22]) },
3042 { "f23", offsetof(CPUState, fpr[23]) },
3043 { "f24", offsetof(CPUState, fpr[24]) },
3044 { "f25", offsetof(CPUState, fpr[25]) },
3045 { "f26", offsetof(CPUState, fpr[26]) },
3046 { "f27", offsetof(CPUState, fpr[27]) },
3047 { "f28", offsetof(CPUState, fpr[28]) },
3048 { "f29", offsetof(CPUState, fpr[29]) },
3049 { "f30", offsetof(CPUState, fpr[30]) },
3050 { "f31", offsetof(CPUState, fpr[31]) },
3051 { "fpscr", offsetof(CPUState, fpscr) },
3052 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003053 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003054 { "lr", offsetof(CPUState, lr) },
3055 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003056 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003057 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003058 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003059 { "msr", 0, &monitor_get_msr, },
3060 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003061 { "tbu", 0, &monitor_get_tbu, },
3062 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003063#if defined(TARGET_PPC64)
3064 /* Address space register */
3065 { "asr", offsetof(CPUState, asr) },
3066#endif
3067 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00003068 { "sdr1", offsetof(CPUState, sdr1) },
3069 { "sr0", offsetof(CPUState, sr[0]) },
3070 { "sr1", offsetof(CPUState, sr[1]) },
3071 { "sr2", offsetof(CPUState, sr[2]) },
3072 { "sr3", offsetof(CPUState, sr[3]) },
3073 { "sr4", offsetof(CPUState, sr[4]) },
3074 { "sr5", offsetof(CPUState, sr[5]) },
3075 { "sr6", offsetof(CPUState, sr[6]) },
3076 { "sr7", offsetof(CPUState, sr[7]) },
3077 { "sr8", offsetof(CPUState, sr[8]) },
3078 { "sr9", offsetof(CPUState, sr[9]) },
3079 { "sr10", offsetof(CPUState, sr[10]) },
3080 { "sr11", offsetof(CPUState, sr[11]) },
3081 { "sr12", offsetof(CPUState, sr[12]) },
3082 { "sr13", offsetof(CPUState, sr[13]) },
3083 { "sr14", offsetof(CPUState, sr[14]) },
3084 { "sr15", offsetof(CPUState, sr[15]) },
3085 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003086#elif defined(TARGET_SPARC)
3087 { "g0", offsetof(CPUState, gregs[0]) },
3088 { "g1", offsetof(CPUState, gregs[1]) },
3089 { "g2", offsetof(CPUState, gregs[2]) },
3090 { "g3", offsetof(CPUState, gregs[3]) },
3091 { "g4", offsetof(CPUState, gregs[4]) },
3092 { "g5", offsetof(CPUState, gregs[5]) },
3093 { "g6", offsetof(CPUState, gregs[6]) },
3094 { "g7", offsetof(CPUState, gregs[7]) },
3095 { "o0", 0, monitor_get_reg },
3096 { "o1", 1, monitor_get_reg },
3097 { "o2", 2, monitor_get_reg },
3098 { "o3", 3, monitor_get_reg },
3099 { "o4", 4, monitor_get_reg },
3100 { "o5", 5, monitor_get_reg },
3101 { "o6", 6, monitor_get_reg },
3102 { "o7", 7, monitor_get_reg },
3103 { "l0", 8, monitor_get_reg },
3104 { "l1", 9, monitor_get_reg },
3105 { "l2", 10, monitor_get_reg },
3106 { "l3", 11, monitor_get_reg },
3107 { "l4", 12, monitor_get_reg },
3108 { "l5", 13, monitor_get_reg },
3109 { "l6", 14, monitor_get_reg },
3110 { "l7", 15, monitor_get_reg },
3111 { "i0", 16, monitor_get_reg },
3112 { "i1", 17, monitor_get_reg },
3113 { "i2", 18, monitor_get_reg },
3114 { "i3", 19, monitor_get_reg },
3115 { "i4", 20, monitor_get_reg },
3116 { "i5", 21, monitor_get_reg },
3117 { "i6", 22, monitor_get_reg },
3118 { "i7", 23, monitor_get_reg },
3119 { "pc", offsetof(CPUState, pc) },
3120 { "npc", offsetof(CPUState, npc) },
3121 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003122#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003123 { "psr", 0, &monitor_get_psr, },
3124 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003125#endif
bellarde95c8d52004-09-30 22:22:08 +00003126 { "tbr", offsetof(CPUState, tbr) },
3127 { "fsr", offsetof(CPUState, fsr) },
3128 { "f0", offsetof(CPUState, fpr[0]) },
3129 { "f1", offsetof(CPUState, fpr[1]) },
3130 { "f2", offsetof(CPUState, fpr[2]) },
3131 { "f3", offsetof(CPUState, fpr[3]) },
3132 { "f4", offsetof(CPUState, fpr[4]) },
3133 { "f5", offsetof(CPUState, fpr[5]) },
3134 { "f6", offsetof(CPUState, fpr[6]) },
3135 { "f7", offsetof(CPUState, fpr[7]) },
3136 { "f8", offsetof(CPUState, fpr[8]) },
3137 { "f9", offsetof(CPUState, fpr[9]) },
3138 { "f10", offsetof(CPUState, fpr[10]) },
3139 { "f11", offsetof(CPUState, fpr[11]) },
3140 { "f12", offsetof(CPUState, fpr[12]) },
3141 { "f13", offsetof(CPUState, fpr[13]) },
3142 { "f14", offsetof(CPUState, fpr[14]) },
3143 { "f15", offsetof(CPUState, fpr[15]) },
3144 { "f16", offsetof(CPUState, fpr[16]) },
3145 { "f17", offsetof(CPUState, fpr[17]) },
3146 { "f18", offsetof(CPUState, fpr[18]) },
3147 { "f19", offsetof(CPUState, fpr[19]) },
3148 { "f20", offsetof(CPUState, fpr[20]) },
3149 { "f21", offsetof(CPUState, fpr[21]) },
3150 { "f22", offsetof(CPUState, fpr[22]) },
3151 { "f23", offsetof(CPUState, fpr[23]) },
3152 { "f24", offsetof(CPUState, fpr[24]) },
3153 { "f25", offsetof(CPUState, fpr[25]) },
3154 { "f26", offsetof(CPUState, fpr[26]) },
3155 { "f27", offsetof(CPUState, fpr[27]) },
3156 { "f28", offsetof(CPUState, fpr[28]) },
3157 { "f29", offsetof(CPUState, fpr[29]) },
3158 { "f30", offsetof(CPUState, fpr[30]) },
3159 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003160#ifdef TARGET_SPARC64
3161 { "f32", offsetof(CPUState, fpr[32]) },
3162 { "f34", offsetof(CPUState, fpr[34]) },
3163 { "f36", offsetof(CPUState, fpr[36]) },
3164 { "f38", offsetof(CPUState, fpr[38]) },
3165 { "f40", offsetof(CPUState, fpr[40]) },
3166 { "f42", offsetof(CPUState, fpr[42]) },
3167 { "f44", offsetof(CPUState, fpr[44]) },
3168 { "f46", offsetof(CPUState, fpr[46]) },
3169 { "f48", offsetof(CPUState, fpr[48]) },
3170 { "f50", offsetof(CPUState, fpr[50]) },
3171 { "f52", offsetof(CPUState, fpr[52]) },
3172 { "f54", offsetof(CPUState, fpr[54]) },
3173 { "f56", offsetof(CPUState, fpr[56]) },
3174 { "f58", offsetof(CPUState, fpr[58]) },
3175 { "f60", offsetof(CPUState, fpr[60]) },
3176 { "f62", offsetof(CPUState, fpr[62]) },
3177 { "asi", offsetof(CPUState, asi) },
3178 { "pstate", offsetof(CPUState, pstate) },
3179 { "cansave", offsetof(CPUState, cansave) },
3180 { "canrestore", offsetof(CPUState, canrestore) },
3181 { "otherwin", offsetof(CPUState, otherwin) },
3182 { "wstate", offsetof(CPUState, wstate) },
3183 { "cleanwin", offsetof(CPUState, cleanwin) },
3184 { "fprs", offsetof(CPUState, fprs) },
3185#endif
bellard9307c4c2004-04-04 12:57:25 +00003186#endif
3187 { NULL },
3188};
3189
aliguori376253e2009-03-05 23:01:23 +00003190static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003191{
aliguori376253e2009-03-05 23:01:23 +00003192 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003193 longjmp(expr_env, 1);
3194}
3195
Markus Armbruster09b94182010-01-20 13:07:30 +01003196/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003197static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003198{
blueswir18662d652008-10-02 18:32:44 +00003199 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003200 void *ptr;
3201
bellard9307c4c2004-04-04 12:57:25 +00003202 for(md = monitor_defs; md->name != NULL; md++) {
3203 if (compare_cmd(name, md->name)) {
3204 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003205 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003206 } else {
bellard6a00d602005-11-21 23:25:50 +00003207 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003208 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003209 switch(md->type) {
3210 case MD_I32:
3211 *pval = *(int32_t *)ptr;
3212 break;
3213 case MD_TLONG:
3214 *pval = *(target_long *)ptr;
3215 break;
3216 default:
3217 *pval = 0;
3218 break;
3219 }
bellard9307c4c2004-04-04 12:57:25 +00003220 }
3221 return 0;
3222 }
3223 }
3224 return -1;
3225}
3226
3227static void next(void)
3228{
Blue Swirl660f11b2009-07-31 21:16:51 +00003229 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003230 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003231 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003232 pch++;
3233 }
3234}
3235
aliguori376253e2009-03-05 23:01:23 +00003236static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003237
aliguori376253e2009-03-05 23:01:23 +00003238static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003239{
blueswir1c2efc952007-09-25 17:28:42 +00003240 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003241 char *p;
bellard6a00d602005-11-21 23:25:50 +00003242 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003243
3244 switch(*pch) {
3245 case '+':
3246 next();
aliguori376253e2009-03-05 23:01:23 +00003247 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003248 break;
3249 case '-':
3250 next();
aliguori376253e2009-03-05 23:01:23 +00003251 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003252 break;
3253 case '~':
3254 next();
aliguori376253e2009-03-05 23:01:23 +00003255 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003256 break;
3257 case '(':
3258 next();
aliguori376253e2009-03-05 23:01:23 +00003259 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003260 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003261 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003262 }
3263 next();
3264 break;
bellard81d09122004-07-14 17:21:37 +00003265 case '\'':
3266 pch++;
3267 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003268 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003269 n = *pch;
3270 pch++;
3271 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003272 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003273 next();
3274 break;
bellard9307c4c2004-04-04 12:57:25 +00003275 case '$':
3276 {
3277 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003278 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003279
bellard9307c4c2004-04-04 12:57:25 +00003280 pch++;
3281 q = buf;
3282 while ((*pch >= 'a' && *pch <= 'z') ||
3283 (*pch >= 'A' && *pch <= 'Z') ||
3284 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003285 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003286 if ((q - buf) < sizeof(buf) - 1)
3287 *q++ = *pch;
3288 pch++;
3289 }
blueswir1cd390082008-11-16 13:53:32 +00003290 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003291 pch++;
3292 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003293 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003294 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003295 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003296 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003297 }
3298 break;
3299 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003300 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003301 n = 0;
3302 break;
3303 default:
blueswir17743e582007-09-24 18:39:04 +00003304#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003305 n = strtoull(pch, &p, 0);
3306#else
bellard9307c4c2004-04-04 12:57:25 +00003307 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003308#endif
bellard9307c4c2004-04-04 12:57:25 +00003309 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003310 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003311 }
3312 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003313 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003314 pch++;
3315 break;
3316 }
3317 return n;
3318}
3319
3320
aliguori376253e2009-03-05 23:01:23 +00003321static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003322{
blueswir1c2efc952007-09-25 17:28:42 +00003323 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003324 int op;
ths3b46e622007-09-17 08:09:54 +00003325
aliguori376253e2009-03-05 23:01:23 +00003326 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003327 for(;;) {
3328 op = *pch;
3329 if (op != '*' && op != '/' && op != '%')
3330 break;
3331 next();
aliguori376253e2009-03-05 23:01:23 +00003332 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003333 switch(op) {
3334 default:
3335 case '*':
3336 val *= val2;
3337 break;
3338 case '/':
3339 case '%':
ths5fafdf22007-09-16 21:08:06 +00003340 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003341 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003342 if (op == '/')
3343 val /= val2;
3344 else
3345 val %= val2;
3346 break;
3347 }
3348 }
3349 return val;
3350}
3351
aliguori376253e2009-03-05 23:01:23 +00003352static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003353{
blueswir1c2efc952007-09-25 17:28:42 +00003354 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003355 int op;
bellard9307c4c2004-04-04 12:57:25 +00003356
aliguori376253e2009-03-05 23:01:23 +00003357 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003358 for(;;) {
3359 op = *pch;
3360 if (op != '&' && op != '|' && op != '^')
3361 break;
3362 next();
aliguori376253e2009-03-05 23:01:23 +00003363 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003364 switch(op) {
3365 default:
3366 case '&':
3367 val &= val2;
3368 break;
3369 case '|':
3370 val |= val2;
3371 break;
3372 case '^':
3373 val ^= val2;
3374 break;
3375 }
3376 }
3377 return val;
3378}
3379
aliguori376253e2009-03-05 23:01:23 +00003380static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003381{
blueswir1c2efc952007-09-25 17:28:42 +00003382 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003383 int op;
bellard9307c4c2004-04-04 12:57:25 +00003384
aliguori376253e2009-03-05 23:01:23 +00003385 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003386 for(;;) {
3387 op = *pch;
3388 if (op != '+' && op != '-')
3389 break;
3390 next();
aliguori376253e2009-03-05 23:01:23 +00003391 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003392 if (op == '+')
3393 val += val2;
3394 else
3395 val -= val2;
3396 }
3397 return val;
3398}
3399
aliguori376253e2009-03-05 23:01:23 +00003400static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003401{
3402 pch = *pp;
3403 if (setjmp(expr_env)) {
3404 *pp = pch;
3405 return -1;
3406 }
blueswir1cd390082008-11-16 13:53:32 +00003407 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003408 pch++;
aliguori376253e2009-03-05 23:01:23 +00003409 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003410 *pp = pch;
3411 return 0;
3412}
3413
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003414static int get_double(Monitor *mon, double *pval, const char **pp)
3415{
3416 const char *p = *pp;
3417 char *tailp;
3418 double d;
3419
3420 d = strtod(p, &tailp);
3421 if (tailp == p) {
3422 monitor_printf(mon, "Number expected\n");
3423 return -1;
3424 }
3425 if (d != d || d - d != 0) {
3426 /* NaN or infinity */
3427 monitor_printf(mon, "Bad number\n");
3428 return -1;
3429 }
3430 *pval = d;
3431 *pp = tailp;
3432 return 0;
3433}
3434
bellard9307c4c2004-04-04 12:57:25 +00003435static int get_str(char *buf, int buf_size, const char **pp)
3436{
3437 const char *p;
3438 char *q;
3439 int c;
3440
bellard81d09122004-07-14 17:21:37 +00003441 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003442 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003443 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003444 p++;
3445 if (*p == '\0') {
3446 fail:
bellard81d09122004-07-14 17:21:37 +00003447 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003448 *pp = p;
3449 return -1;
3450 }
bellard9307c4c2004-04-04 12:57:25 +00003451 if (*p == '\"') {
3452 p++;
3453 while (*p != '\0' && *p != '\"') {
3454 if (*p == '\\') {
3455 p++;
3456 c = *p++;
3457 switch(c) {
3458 case 'n':
3459 c = '\n';
3460 break;
3461 case 'r':
3462 c = '\r';
3463 break;
3464 case '\\':
3465 case '\'':
3466 case '\"':
3467 break;
3468 default:
3469 qemu_printf("unsupported escape code: '\\%c'\n", c);
3470 goto fail;
3471 }
3472 if ((q - buf) < buf_size - 1) {
3473 *q++ = c;
3474 }
3475 } else {
3476 if ((q - buf) < buf_size - 1) {
3477 *q++ = *p;
3478 }
3479 p++;
3480 }
3481 }
3482 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003483 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003484 goto fail;
3485 }
3486 p++;
3487 } else {
blueswir1cd390082008-11-16 13:53:32 +00003488 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003489 if ((q - buf) < buf_size - 1) {
3490 *q++ = *p;
3491 }
3492 p++;
3493 }
bellard9307c4c2004-04-04 12:57:25 +00003494 }
bellard81d09122004-07-14 17:21:37 +00003495 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003496 *pp = p;
3497 return 0;
3498}
3499
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003500/*
3501 * Store the command-name in cmdname, and return a pointer to
3502 * the remaining of the command string.
3503 */
3504static const char *get_command_name(const char *cmdline,
3505 char *cmdname, size_t nlen)
3506{
3507 size_t len;
3508 const char *p, *pstart;
3509
3510 p = cmdline;
3511 while (qemu_isspace(*p))
3512 p++;
3513 if (*p == '\0')
3514 return NULL;
3515 pstart = p;
3516 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3517 p++;
3518 len = p - pstart;
3519 if (len > nlen - 1)
3520 len = nlen - 1;
3521 memcpy(cmdname, pstart, len);
3522 cmdname[len] = '\0';
3523 return p;
3524}
3525
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003526/**
3527 * Read key of 'type' into 'key' and return the current
3528 * 'type' pointer.
3529 */
3530static char *key_get_info(const char *type, char **key)
3531{
3532 size_t len;
3533 char *p, *str;
3534
3535 if (*type == ',')
3536 type++;
3537
3538 p = strchr(type, ':');
3539 if (!p) {
3540 *key = NULL;
3541 return NULL;
3542 }
3543 len = p - type;
3544
3545 str = qemu_malloc(len + 1);
3546 memcpy(str, type, len);
3547 str[len] = '\0';
3548
3549 *key = str;
3550 return ++p;
3551}
3552
bellard9307c4c2004-04-04 12:57:25 +00003553static int default_fmt_format = 'x';
3554static int default_fmt_size = 4;
3555
3556#define MAX_ARGS 16
3557
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003558static int is_valid_option(const char *c, const char *typestr)
3559{
3560 char option[3];
3561
3562 option[0] = '-';
3563 option[1] = *c;
3564 option[2] = '\0';
3565
3566 typestr = strstr(typestr, option);
3567 return (typestr != NULL);
3568}
3569
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003570static const mon_cmd_t *monitor_find_command(const char *cmdname)
3571{
3572 const mon_cmd_t *cmd;
3573
3574 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3575 if (compare_cmd(cmdname, cmd->name)) {
3576 return cmd;
3577 }
3578 }
3579
3580 return NULL;
3581}
3582
Anthony Liguoric227f092009-10-01 16:12:16 -05003583static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003584 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003585 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003586{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003587 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003588 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003589 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003590 char cmdname[256];
3591 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003592 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003593
3594#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003595 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003596#endif
ths3b46e622007-09-17 08:09:54 +00003597
bellard9307c4c2004-04-04 12:57:25 +00003598 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003599 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3600 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003601 return NULL;
ths3b46e622007-09-17 08:09:54 +00003602
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003603 cmd = monitor_find_command(cmdname);
3604 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003605 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003606 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003607 }
bellard9307c4c2004-04-04 12:57:25 +00003608
bellard9307c4c2004-04-04 12:57:25 +00003609 /* parse the parameters */
3610 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003611 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003612 typestr = key_get_info(typestr, &key);
3613 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003614 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003615 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003616 typestr++;
3617 switch(c) {
3618 case 'F':
bellard81d09122004-07-14 17:21:37 +00003619 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003620 case 's':
3621 {
3622 int ret;
ths3b46e622007-09-17 08:09:54 +00003623
blueswir1cd390082008-11-16 13:53:32 +00003624 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003625 p++;
3626 if (*typestr == '?') {
3627 typestr++;
3628 if (*p == '\0') {
3629 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003630 break;
bellard9307c4c2004-04-04 12:57:25 +00003631 }
3632 }
3633 ret = get_str(buf, sizeof(buf), &p);
3634 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003635 switch(c) {
3636 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003637 monitor_printf(mon, "%s: filename expected\n",
3638 cmdname);
bellard81d09122004-07-14 17:21:37 +00003639 break;
3640 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003641 monitor_printf(mon, "%s: block device name expected\n",
3642 cmdname);
bellard81d09122004-07-14 17:21:37 +00003643 break;
3644 default:
aliguori376253e2009-03-05 23:01:23 +00003645 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003646 break;
3647 }
bellard9307c4c2004-04-04 12:57:25 +00003648 goto fail;
3649 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003650 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003651 }
3652 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003653 case 'O':
3654 {
3655 QemuOptsList *opts_list;
3656 QemuOpts *opts;
3657
3658 opts_list = qemu_find_opts(key);
3659 if (!opts_list || opts_list->desc->name) {
3660 goto bad_type;
3661 }
3662 while (qemu_isspace(*p)) {
3663 p++;
3664 }
3665 if (!*p)
3666 break;
3667 if (get_str(buf, sizeof(buf), &p) < 0) {
3668 goto fail;
3669 }
3670 opts = qemu_opts_parse(opts_list, buf, 1);
3671 if (!opts) {
3672 goto fail;
3673 }
3674 qemu_opts_to_qdict(opts, qdict);
3675 qemu_opts_del(opts);
3676 }
3677 break;
bellard9307c4c2004-04-04 12:57:25 +00003678 case '/':
3679 {
3680 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003681
blueswir1cd390082008-11-16 13:53:32 +00003682 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003683 p++;
3684 if (*p == '/') {
3685 /* format found */
3686 p++;
3687 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003688 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003689 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003690 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003691 count = count * 10 + (*p - '0');
3692 p++;
3693 }
3694 }
3695 size = -1;
3696 format = -1;
3697 for(;;) {
3698 switch(*p) {
3699 case 'o':
3700 case 'd':
3701 case 'u':
3702 case 'x':
3703 case 'i':
3704 case 'c':
3705 format = *p++;
3706 break;
3707 case 'b':
3708 size = 1;
3709 p++;
3710 break;
3711 case 'h':
3712 size = 2;
3713 p++;
3714 break;
3715 case 'w':
3716 size = 4;
3717 p++;
3718 break;
3719 case 'g':
3720 case 'L':
3721 size = 8;
3722 p++;
3723 break;
3724 default:
3725 goto next;
3726 }
3727 }
3728 next:
blueswir1cd390082008-11-16 13:53:32 +00003729 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003730 monitor_printf(mon, "invalid char in format: '%c'\n",
3731 *p);
bellard9307c4c2004-04-04 12:57:25 +00003732 goto fail;
3733 }
bellard9307c4c2004-04-04 12:57:25 +00003734 if (format < 0)
3735 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003736 if (format != 'i') {
3737 /* for 'i', not specifying a size gives -1 as size */
3738 if (size < 0)
3739 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003740 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003741 }
bellard9307c4c2004-04-04 12:57:25 +00003742 default_fmt_format = format;
3743 } else {
3744 count = 1;
3745 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003746 if (format != 'i') {
3747 size = default_fmt_size;
3748 } else {
3749 size = -1;
3750 }
bellard9307c4c2004-04-04 12:57:25 +00003751 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003752 qdict_put(qdict, "count", qint_from_int(count));
3753 qdict_put(qdict, "format", qint_from_int(format));
3754 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003755 }
3756 break;
3757 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003758 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003759 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003760 {
blueswir1c2efc952007-09-25 17:28:42 +00003761 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003762
blueswir1cd390082008-11-16 13:53:32 +00003763 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003764 p++;
bellard34405572004-06-08 00:55:58 +00003765 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003766 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003767 if (*p == '\0') {
3768 typestr++;
3769 break;
3770 }
bellard34405572004-06-08 00:55:58 +00003771 } else {
3772 if (*p == '.') {
3773 p++;
blueswir1cd390082008-11-16 13:53:32 +00003774 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003775 p++;
bellard34405572004-06-08 00:55:58 +00003776 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003777 typestr++;
3778 break;
bellard34405572004-06-08 00:55:58 +00003779 }
3780 }
bellard13224a82006-07-14 22:03:35 +00003781 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003782 }
aliguori376253e2009-03-05 23:01:23 +00003783 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003784 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003785 /* Check if 'i' is greater than 32-bit */
3786 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3787 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3788 monitor_printf(mon, "integer is for 32-bit values\n");
3789 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003790 } else if (c == 'M') {
3791 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003792 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003793 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003794 }
3795 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003796 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003797 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003798 {
3799 double val;
3800
3801 while (qemu_isspace(*p))
3802 p++;
3803 if (*typestr == '?') {
3804 typestr++;
3805 if (*p == '\0') {
3806 break;
3807 }
3808 }
3809 if (get_double(mon, &val, &p) < 0) {
3810 goto fail;
3811 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003812 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003813 switch (*p) {
3814 case 'K': case 'k':
3815 val *= 1 << 10; p++; break;
3816 case 'M': case 'm':
3817 val *= 1 << 20; p++; break;
3818 case 'G': case 'g':
3819 val *= 1 << 30; p++; break;
3820 }
3821 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003822 if (c == 'T' && p[0] && p[1] == 's') {
3823 switch (*p) {
3824 case 'm':
3825 val /= 1e3; p += 2; break;
3826 case 'u':
3827 val /= 1e6; p += 2; break;
3828 case 'n':
3829 val /= 1e9; p += 2; break;
3830 }
3831 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003832 if (*p && !qemu_isspace(*p)) {
3833 monitor_printf(mon, "Unknown unit suffix\n");
3834 goto fail;
3835 }
3836 qdict_put(qdict, key, qfloat_from_double(val));
3837 }
3838 break;
bellard9307c4c2004-04-04 12:57:25 +00003839 case '-':
3840 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003841 const char *tmp = p;
3842 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003843 /* option */
ths3b46e622007-09-17 08:09:54 +00003844
bellard9307c4c2004-04-04 12:57:25 +00003845 c = *typestr++;
3846 if (c == '\0')
3847 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003848 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003849 p++;
3850 has_option = 0;
3851 if (*p == '-') {
3852 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003853 if(c != *p) {
3854 if(!is_valid_option(p, typestr)) {
3855
3856 monitor_printf(mon, "%s: unsupported option -%c\n",
3857 cmdname, *p);
3858 goto fail;
3859 } else {
3860 skip_key = 1;
3861 }
bellard9307c4c2004-04-04 12:57:25 +00003862 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003863 if(skip_key) {
3864 p = tmp;
3865 } else {
3866 p++;
3867 has_option = 1;
3868 }
bellard9307c4c2004-04-04 12:57:25 +00003869 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003870 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003871 }
3872 break;
3873 default:
3874 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003875 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003876 goto fail;
3877 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003878 qemu_free(key);
3879 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003880 }
3881 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003882 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003883 p++;
3884 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003885 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3886 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003887 goto fail;
3888 }
3889
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003890 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003891
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003892fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003893 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003894 return NULL;
3895}
3896
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003897void monitor_set_error(Monitor *mon, QError *qerror)
3898{
3899 /* report only the first error */
3900 if (!mon->error) {
3901 mon->error = qerror;
3902 } else {
3903 MON_DEBUG("Additional error report at %s:%d\n",
3904 qerror->file, qerror->linenr);
3905 QDECREF(qerror);
3906 }
3907}
3908
Adam Litke940cc302010-01-25 12:18:44 -06003909static int is_async_return(const QObject *data)
3910{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003911 if (data && qobject_type(data) == QTYPE_QDICT) {
3912 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3913 }
3914
3915 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003916}
3917
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003918static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3919{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003920 if (monitor_ctrl_mode(mon)) {
3921 if (ret && !monitor_has_error(mon)) {
3922 /*
3923 * If it returns failure, it must have passed on error.
3924 *
3925 * Action: Report an internal error to the client if in QMP.
3926 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003927 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003928 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3929 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003930 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003931
3932#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003933 if (!ret && monitor_has_error(mon)) {
3934 /*
3935 * If it returns success, it must not have passed an error.
3936 *
3937 * Action: Report the passed error to the client.
3938 */
3939 MON_DEBUG("command '%s' returned success but passed an error\n",
3940 cmd->name);
3941 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003942
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003943 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3944 /*
3945 * Handlers should not call Monitor print functions.
3946 *
3947 * Action: Ignore them in QMP.
3948 *
3949 * (XXX: we don't check any 'info' or 'query' command here
3950 * because the user print function _is_ called by do_info(), hence
3951 * we will trigger this check. This problem will go away when we
3952 * make 'query' commands real and kill do_info())
3953 */
3954 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3955 cmd->name, mon_print_count_get(mon));
3956 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003957#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003958 } else {
3959 assert(!monitor_has_error(mon));
3960 QDECREF(mon->error);
3961 mon->error = NULL;
3962 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003963}
3964
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003965static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3966 const QDict *params)
3967{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003968 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003969 QObject *data = NULL;
3970
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003971 mon_print_count_init(mon);
3972
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003973 ret = cmd->mhandler.cmd_new(mon, params, &data);
3974 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003975
Adam Litke940cc302010-01-25 12:18:44 -06003976 if (is_async_return(data)) {
3977 /*
3978 * Asynchronous commands have no initial return data but they can
3979 * generate errors. Data is returned via the async completion handler.
3980 */
3981 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3982 monitor_protocol_emitter(mon, NULL);
3983 }
3984 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003985 /* Monitor Protocol */
3986 monitor_protocol_emitter(mon, data);
3987 } else {
3988 /* User Protocol */
3989 if (data)
3990 cmd->user_print(mon, data);
3991 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003992
3993 qobject_decref(data);
3994}
3995
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003996static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003997{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003998 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003999 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004000
4001 qdict = qdict_new();
4002
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004003 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004004 if (!cmd)
4005 goto out;
4006
Adam Litke940cc302010-01-25 12:18:44 -06004007 if (monitor_handler_is_async(cmd)) {
4008 user_async_cmd_handler(mon, cmd, qdict);
4009 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02004010 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004011 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004012 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004013 }
4014
Luiz Capitulino13917be2009-10-07 13:41:54 -03004015out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004016 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004017}
4018
bellard81d09122004-07-14 17:21:37 +00004019static void cmd_completion(const char *name, const char *list)
4020{
4021 const char *p, *pstart;
4022 char cmd[128];
4023 int len;
4024
4025 p = list;
4026 for(;;) {
4027 pstart = p;
4028 p = strchr(p, '|');
4029 if (!p)
4030 p = pstart + strlen(pstart);
4031 len = p - pstart;
4032 if (len > sizeof(cmd) - 2)
4033 len = sizeof(cmd) - 2;
4034 memcpy(cmd, pstart, len);
4035 cmd[len] = '\0';
4036 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004037 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004038 }
4039 if (*p == '\0')
4040 break;
4041 p++;
4042 }
4043}
4044
4045static void file_completion(const char *input)
4046{
4047 DIR *ffs;
4048 struct dirent *d;
4049 char path[1024];
4050 char file[1024], file_prefix[1024];
4051 int input_path_len;
4052 const char *p;
4053
ths5fafdf22007-09-16 21:08:06 +00004054 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004055 if (!p) {
4056 input_path_len = 0;
4057 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004058 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004059 } else {
4060 input_path_len = p - input + 1;
4061 memcpy(path, input, input_path_len);
4062 if (input_path_len > sizeof(path) - 1)
4063 input_path_len = sizeof(path) - 1;
4064 path[input_path_len] = '\0';
4065 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4066 }
4067#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004068 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4069 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004070#endif
4071 ffs = opendir(path);
4072 if (!ffs)
4073 return;
4074 for(;;) {
4075 struct stat sb;
4076 d = readdir(ffs);
4077 if (!d)
4078 break;
4079 if (strstart(d->d_name, file_prefix, NULL)) {
4080 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004081 if (input_path_len < sizeof(file))
4082 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4083 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004084 /* stat the file to find out if it's a directory.
4085 * In that case add a slash to speed up typing long paths
4086 */
4087 stat(file, &sb);
4088 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004089 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004090 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004091 }
4092 }
4093 closedir(ffs);
4094}
4095
aliguori51de9762009-03-05 23:00:43 +00004096static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004097{
aliguori51de9762009-03-05 23:00:43 +00004098 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004099 const char *input = opaque;
4100
4101 if (input[0] == '\0' ||
4102 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004103 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004104 }
4105}
4106
4107/* NOTE: this parser is an approximate form of the real command parser */
4108static void parse_cmdline(const char *cmdline,
4109 int *pnb_args, char **args)
4110{
4111 const char *p;
4112 int nb_args, ret;
4113 char buf[1024];
4114
4115 p = cmdline;
4116 nb_args = 0;
4117 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004118 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004119 p++;
4120 if (*p == '\0')
4121 break;
4122 if (nb_args >= MAX_ARGS)
4123 break;
4124 ret = get_str(buf, sizeof(buf), &p);
4125 args[nb_args] = qemu_strdup(buf);
4126 nb_args++;
4127 if (ret < 0)
4128 break;
4129 }
4130 *pnb_args = nb_args;
4131}
4132
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004133static const char *next_arg_type(const char *typestr)
4134{
4135 const char *p = strchr(typestr, ':');
4136 return (p != NULL ? ++p : typestr);
4137}
4138
aliguori4c36ba32009-03-05 23:01:37 +00004139static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004140{
4141 const char *cmdname;
4142 char *args[MAX_ARGS];
4143 int nb_args, i, len;
4144 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004145 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004146 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004147
4148 parse_cmdline(cmdline, &nb_args, args);
4149#ifdef DEBUG_COMPLETION
4150 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004151 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004152 }
4153#endif
4154
4155 /* if the line ends with a space, it means we want to complete the
4156 next arg */
4157 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004158 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004159 if (nb_args >= MAX_ARGS)
4160 return;
4161 args[nb_args++] = qemu_strdup("");
4162 }
4163 if (nb_args <= 1) {
4164 /* command completion */
4165 if (nb_args == 0)
4166 cmdname = "";
4167 else
4168 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004169 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004170 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004171 cmd_completion(cmdname, cmd->name);
4172 }
4173 } else {
4174 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004175 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004176 if (compare_cmd(args[0], cmd->name))
4177 goto found;
4178 }
4179 return;
4180 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004181 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004182 for(i = 0; i < nb_args - 2; i++) {
4183 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004184 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004185 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004186 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004187 }
4188 }
4189 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004190 if (*ptype == '-' && ptype[1] != '\0') {
4191 ptype += 2;
4192 }
bellard81d09122004-07-14 17:21:37 +00004193 switch(*ptype) {
4194 case 'F':
4195 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004196 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004197 file_completion(str);
4198 break;
4199 case 'B':
4200 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004201 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004202 bdrv_iterate(block_completion_it, (void *)str);
4203 break;
bellard7fe48482004-10-09 18:08:01 +00004204 case 's':
4205 /* XXX: more generic ? */
4206 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004207 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004208 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4209 cmd_completion(str, cmd->name);
4210 }
bellard64866c32006-05-07 18:03:31 +00004211 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004212 char *sep = strrchr(str, '-');
4213 if (sep)
4214 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004215 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004216 for(key = key_defs; key->name != NULL; key++) {
4217 cmd_completion(str, key->name);
4218 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004219 } else if (!strcmp(cmd->name, "help|?")) {
4220 readline_set_completion_index(cur_mon->rs, strlen(str));
4221 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4222 cmd_completion(str, cmd->name);
4223 }
bellard7fe48482004-10-09 18:08:01 +00004224 }
4225 break;
bellard81d09122004-07-14 17:21:37 +00004226 default:
4227 break;
4228 }
4229 }
4230 for(i = 0; i < nb_args; i++)
4231 qemu_free(args[i]);
4232}
4233
aliguori731b0362009-03-05 23:01:42 +00004234static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004235{
aliguori731b0362009-03-05 23:01:42 +00004236 Monitor *mon = opaque;
4237
Jan Kiszkac62313b2009-12-04 14:05:29 +01004238 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004239}
4240
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004241typedef struct CmdArgs {
4242 QString *name;
4243 int type;
4244 int flag;
4245 int optional;
4246} CmdArgs;
4247
4248static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4249{
4250 if (!cmd_args->optional) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004251 qerror_report(QERR_MISSING_PARAMETER, name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004252 return -1;
4253 }
4254
4255 if (cmd_args->type == '-') {
4256 /* handlers expect a value, they need to be changed */
4257 qdict_put(args, name, qint_from_int(0));
4258 }
4259
4260 return 0;
4261}
4262
4263static int check_arg(const CmdArgs *cmd_args, QDict *args)
4264{
4265 QObject *value;
4266 const char *name;
4267
4268 name = qstring_get_str(cmd_args->name);
4269
4270 if (!args) {
4271 return check_opt(cmd_args, name, args);
4272 }
4273
4274 value = qdict_get(args, name);
4275 if (!value) {
4276 return check_opt(cmd_args, name, args);
4277 }
4278
4279 switch (cmd_args->type) {
4280 case 'F':
4281 case 'B':
4282 case 's':
4283 if (qobject_type(value) != QTYPE_QSTRING) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004284 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "string");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004285 return -1;
4286 }
4287 break;
4288 case '/': {
4289 int i;
4290 const char *keys[] = { "count", "format", "size", NULL };
4291
4292 for (i = 0; keys[i]; i++) {
4293 QObject *obj = qdict_get(args, keys[i]);
4294 if (!obj) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004295 qerror_report(QERR_MISSING_PARAMETER, name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004296 return -1;
4297 }
4298 if (qobject_type(obj) != QTYPE_QINT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004299 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004300 return -1;
4301 }
4302 }
4303 break;
4304 }
4305 case 'i':
4306 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004307 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004308 if (qobject_type(value) != QTYPE_QINT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004309 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004310 return -1;
4311 }
4312 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004313 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004314 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004315 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004316 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "number");
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004317 return -1;
4318 }
4319 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004320 case '-':
4321 if (qobject_type(value) != QTYPE_QINT &&
4322 qobject_type(value) != QTYPE_QBOOL) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004323 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "bool");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004324 return -1;
4325 }
4326 if (qobject_type(value) == QTYPE_QBOOL) {
4327 /* handlers expect a QInt, they need to be changed */
4328 qdict_put(args, name,
4329 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4330 }
4331 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01004332 case 'O':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004333 default:
4334 /* impossible */
4335 abort();
4336 }
4337
4338 return 0;
4339}
4340
4341static void cmd_args_init(CmdArgs *cmd_args)
4342{
4343 cmd_args->name = qstring_new();
4344 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4345}
4346
Markus Armbruster361127d2010-02-10 20:24:35 +01004347static int check_opts(QemuOptsList *opts_list, QDict *args)
4348{
4349 assert(!opts_list->desc->name);
4350 return 0;
4351}
4352
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004353/*
4354 * This is not trivial, we have to parse Monitor command's argument
4355 * type syntax to be able to check the arguments provided by clients.
4356 *
4357 * In the near future we will be using an array for that and will be
4358 * able to drop all this parsing...
4359 */
4360static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4361{
4362 int err;
4363 const char *p;
4364 CmdArgs cmd_args;
Markus Armbruster361127d2010-02-10 20:24:35 +01004365 QemuOptsList *opts_list;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004366
Blue Swirldd5121b2009-12-04 20:52:02 +00004367 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004368 return (qdict_size(args) == 0 ? 0 : -1);
4369 }
4370
4371 err = 0;
4372 cmd_args_init(&cmd_args);
Markus Armbruster361127d2010-02-10 20:24:35 +01004373 opts_list = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004374
4375 for (p = cmd->args_type;; p++) {
4376 if (*p == ':') {
4377 cmd_args.type = *++p;
4378 p++;
4379 if (cmd_args.type == '-') {
4380 cmd_args.flag = *p++;
4381 cmd_args.optional = 1;
Markus Armbruster361127d2010-02-10 20:24:35 +01004382 } else if (cmd_args.type == 'O') {
4383 opts_list = qemu_find_opts(qstring_get_str(cmd_args.name));
4384 assert(opts_list);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004385 } else if (*p == '?') {
4386 cmd_args.optional = 1;
4387 p++;
4388 }
4389
4390 assert(*p == ',' || *p == '\0');
Markus Armbruster361127d2010-02-10 20:24:35 +01004391 if (opts_list) {
4392 err = check_opts(opts_list, args);
4393 opts_list = NULL;
4394 } else {
4395 err = check_arg(&cmd_args, args);
4396 QDECREF(cmd_args.name);
4397 cmd_args_init(&cmd_args);
4398 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004399
4400 if (err < 0) {
4401 break;
4402 }
4403 } else {
4404 qstring_append_chr(cmd_args.name, *p);
4405 }
4406
4407 if (*p == '\0') {
4408 break;
4409 }
4410 }
4411
4412 QDECREF(cmd_args.name);
4413 return err;
4414}
4415
Luiz Capitulino09069b12010-02-04 18:10:06 -02004416static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4417{
4418 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4419 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4420}
4421
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004422static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4423{
4424 int err;
4425 QObject *obj;
4426 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004427 const mon_cmd_t *cmd;
4428 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004429 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004430
4431 args = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004432
4433 obj = json_parser_parse(tokens, NULL);
4434 if (!obj) {
4435 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004436 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004437 goto err_out;
4438 } else if (qobject_type(obj) != QTYPE_QDICT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004439 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004440 qobject_decref(obj);
4441 goto err_out;
4442 }
4443
4444 input = qobject_to_qdict(obj);
4445
4446 mon->mc->id = qdict_get(input, "id");
4447 qobject_incref(mon->mc->id);
4448
4449 obj = qdict_get(input, "execute");
4450 if (!obj) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004451 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004452 goto err_input;
4453 } else if (qobject_type(obj) != QTYPE_QSTRING) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004454 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "string");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004455 goto err_input;
4456 }
4457
4458 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004459
Luiz Capitulino09069b12010-02-04 18:10:06 -02004460 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004461 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004462 goto err_input;
4463 }
4464
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004465 /*
4466 * XXX: We need this special case until we get info handlers
4467 * converted into 'query-' commands
4468 */
4469 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004470 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004471 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004472 } else if (strstart(cmd_name, "query-", &info_item)) {
4473 cmd = monitor_find_command("info");
4474 qdict_put_obj(input, "arguments",
4475 qobject_from_jsonf("{ 'item': %s }", info_item));
4476 } else {
4477 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004478 if (!cmd || !monitor_handler_ported(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004479 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004480 goto err_input;
4481 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004482 }
4483
4484 obj = qdict_get(input, "arguments");
4485 if (!obj) {
4486 args = qdict_new();
4487 } else {
4488 args = qobject_to_qdict(obj);
4489 QINCREF(args);
4490 }
4491
4492 QDECREF(input);
4493
4494 err = monitor_check_qmp_args(cmd, args);
4495 if (err < 0) {
4496 goto err_out;
4497 }
4498
Adam Litke940cc302010-01-25 12:18:44 -06004499 if (monitor_handler_is_async(cmd)) {
4500 qmp_async_cmd_handler(mon, cmd, args);
4501 } else {
4502 monitor_call_handler(mon, cmd, args);
4503 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004504 goto out;
4505
4506err_input:
4507 QDECREF(input);
4508err_out:
4509 monitor_protocol_emitter(mon, NULL);
4510out:
4511 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004512}
4513
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004514/**
4515 * monitor_control_read(): Read and handle QMP input
4516 */
4517static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4518{
4519 Monitor *old_mon = cur_mon;
4520
4521 cur_mon = opaque;
4522
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004523 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004524
4525 cur_mon = old_mon;
4526}
4527
aliguori731b0362009-03-05 23:01:42 +00004528static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004529{
aliguori731b0362009-03-05 23:01:42 +00004530 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004531 int i;
aliguori376253e2009-03-05 23:01:23 +00004532
aliguori731b0362009-03-05 23:01:42 +00004533 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004534
aliguoricde76ee2009-03-05 23:01:51 +00004535 if (cur_mon->rs) {
4536 for (i = 0; i < size; i++)
4537 readline_handle_byte(cur_mon->rs, buf[i]);
4538 } else {
4539 if (size == 0 || buf[size - 1] != 0)
4540 monitor_printf(cur_mon, "corrupted command\n");
4541 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004542 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004543 }
aliguori731b0362009-03-05 23:01:42 +00004544
4545 cur_mon = old_mon;
4546}
aliguorid8f44602008-10-06 13:52:44 +00004547
aliguori376253e2009-03-05 23:01:23 +00004548static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004549{
aliguori731b0362009-03-05 23:01:42 +00004550 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004551 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004552 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004553}
4554
aliguoricde76ee2009-03-05 23:01:51 +00004555int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004556{
aliguoricde76ee2009-03-05 23:01:51 +00004557 if (!mon->rs)
4558 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004559 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004560 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004561}
4562
aliguori376253e2009-03-05 23:01:23 +00004563void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004564{
aliguoricde76ee2009-03-05 23:01:51 +00004565 if (!mon->rs)
4566 return;
aliguori731b0362009-03-05 23:01:42 +00004567 if (--mon->suspend_cnt == 0)
4568 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004569}
4570
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004571static QObject *get_qmp_greeting(void)
4572{
4573 QObject *ver;
4574
4575 do_info_version(NULL, &ver);
4576 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4577}
4578
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004579/**
4580 * monitor_control_event(): Print QMP gretting
4581 */
4582static void monitor_control_event(void *opaque, int event)
4583{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004584 QObject *data;
4585 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004586
Luiz Capitulino47116d12010-02-08 17:01:30 -02004587 switch (event) {
4588 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004589 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004590 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004591 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004592 monitor_json_emitter(mon, data);
4593 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004594 break;
4595 case CHR_EVENT_CLOSED:
4596 json_message_parser_destroy(&mon->mc->parser);
4597 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004598 }
4599}
4600
aliguori731b0362009-03-05 23:01:42 +00004601static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004602{
aliguori376253e2009-03-05 23:01:23 +00004603 Monitor *mon = opaque;
4604
aliguori2724b182009-03-05 23:01:47 +00004605 switch (event) {
4606 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004607 mon->mux_out = 0;
4608 if (mon->reset_seen) {
4609 readline_restart(mon->rs);
4610 monitor_resume(mon);
4611 monitor_flush(mon);
4612 } else {
4613 mon->suspend_cnt = 0;
4614 }
aliguori2724b182009-03-05 23:01:47 +00004615 break;
ths86e94de2007-01-05 22:01:59 +00004616
aliguori2724b182009-03-05 23:01:47 +00004617 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004618 if (mon->reset_seen) {
4619 if (mon->suspend_cnt == 0) {
4620 monitor_printf(mon, "\n");
4621 }
4622 monitor_flush(mon);
4623 monitor_suspend(mon);
4624 } else {
4625 mon->suspend_cnt++;
4626 }
4627 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004628 break;
4629
Amit Shahb6b8df52009-10-07 18:31:16 +05304630 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004631 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4632 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004633 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004634 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004635 }
4636 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004637 break;
4638 }
ths86e94de2007-01-05 22:01:59 +00004639}
4640
aliguori76655d62009-03-06 20:27:37 +00004641
4642/*
4643 * Local variables:
4644 * c-indent-level: 4
4645 * c-basic-offset: 4
4646 * tab-width: 8
4647 * End:
4648 */
4649
aliguori731b0362009-03-05 23:01:42 +00004650void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004651{
aliguori731b0362009-03-05 23:01:42 +00004652 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004653 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004654
4655 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004656 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004657 is_first_init = 0;
4658 }
aliguori87127162009-03-05 23:01:29 +00004659
4660 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004661
aliguori87127162009-03-05 23:01:29 +00004662 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004663 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004664 if (flags & MONITOR_USE_READLINE) {
4665 mon->rs = readline_init(mon, monitor_find_completion);
4666 monitor_read_command(mon, 0);
4667 }
aliguori87127162009-03-05 23:01:29 +00004668
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004669 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004670 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004671 /* Control mode requires special handlers */
4672 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4673 monitor_control_event, mon);
4674 } else {
4675 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4676 monitor_event, mon);
4677 }
aliguori87127162009-03-05 23:01:29 +00004678
Blue Swirl72cf2d42009-09-12 07:36:22 +00004679 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004680 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4681 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004682}
4683
aliguori376253e2009-03-05 23:01:23 +00004684static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004685{
aliguoribb5fc202009-03-05 23:01:15 +00004686 BlockDriverState *bs = opaque;
4687 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004688
aliguoribb5fc202009-03-05 23:01:15 +00004689 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004690 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004691 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004692 }
aliguori731b0362009-03-05 23:01:42 +00004693 if (mon->password_completion_cb)
4694 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004695
aliguori731b0362009-03-05 23:01:42 +00004696 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004697}
aliguoric0f4ce72009-03-05 23:01:01 +00004698
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004699int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4700 BlockDriverCompletionFunc *completion_cb,
4701 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004702{
aliguoricde76ee2009-03-05 23:01:51 +00004703 int err;
4704
aliguoribb5fc202009-03-05 23:01:15 +00004705 if (!bdrv_key_required(bs)) {
4706 if (completion_cb)
4707 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004708 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004709 }
aliguoric0f4ce72009-03-05 23:01:01 +00004710
Luiz Capitulino94171e12009-12-07 21:37:00 +01004711 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004712 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004713 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004714 }
4715
aliguori376253e2009-03-05 23:01:23 +00004716 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4717 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004718
aliguori731b0362009-03-05 23:01:42 +00004719 mon->password_completion_cb = completion_cb;
4720 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004721
aliguoricde76ee2009-03-05 23:01:51 +00004722 err = monitor_read_password(mon, bdrv_password_cb, bs);
4723
4724 if (err && completion_cb)
4725 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004726
4727 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004728}