blob: 4b6184b7656115c473095659b05991b1e12a1fd5 [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)
bellard92a31b12005-02-10 22:00:52 +000070 * 'i' 32 bit integer
71 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010072 * 'M' just like 'l', except in user mode the value is
73 * multiplied by 2^20 (think Mebibyte)
Markus Armbruster3350a4d2010-01-25 14:23:03 +010074 * 'b' double
75 * user mode accepts an optional G, g, M, m, K, k suffix,
76 * which multiplies the value by 2^30 for suffixes G and
77 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010078 * 'T' double
79 * user mode accepts an optional ms, us, ns suffix,
80 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000081 * '/' optional gdb-like print format (like "/10x")
82 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030083 * '?' optional type (for all types, except '/')
84 * '.' other form of optional type (for 'i' and 'l')
85 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000086 *
87 */
88
Adam Litke940cc302010-01-25 12:18:44 -060089typedef struct MonitorCompletionData MonitorCompletionData;
90struct MonitorCompletionData {
91 Monitor *mon;
92 void (*user_print)(Monitor *mon, const QObject *data);
93};
94
Anthony Liguoric227f092009-10-01 16:12:16 -050095typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000096 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000097 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000098 const char *params;
99 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300100 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300101 union {
102 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300103 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600104 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300105 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200106 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600107 int (*cmd_async)(Monitor *mon, const QDict *params,
108 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300109 } mhandler;
Adam Litke940cc302010-01-25 12:18:44 -0600110 int async;
Anthony Liguoric227f092009-10-01 16:12:16 -0500111} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000112
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100113/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500114typedef struct mon_fd_t mon_fd_t;
115struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100116 char *name;
117 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500118 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100119};
120
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200121typedef struct MonitorControl {
122 QObject *id;
123 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200124 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200125} MonitorControl;
126
aliguori87127162009-03-05 23:01:29 +0000127struct Monitor {
128 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200129 int mux_out;
130 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000131 int flags;
132 int suspend_cnt;
133 uint8_t outbuf[1024];
134 int outbuf_index;
135 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200136 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000137 CPUState *mon_cpu;
138 BlockDriverCompletionFunc *password_completion_cb;
139 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200140#ifdef CONFIG_DEBUG_MONITOR
141 int print_calls_nr;
142#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200143 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500144 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000145 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000146};
147
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200148#ifdef CONFIG_DEBUG_MONITOR
149#define MON_DEBUG(fmt, ...) do { \
150 fprintf(stderr, "Monitor: "); \
151 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200152
153static inline void mon_print_count_inc(Monitor *mon)
154{
155 mon->print_calls_nr++;
156}
157
158static inline void mon_print_count_init(Monitor *mon)
159{
160 mon->print_calls_nr = 0;
161}
162
163static inline int mon_print_count_get(const Monitor *mon)
164{
165 return mon->print_calls_nr;
166}
167
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200168#else /* !CONFIG_DEBUG_MONITOR */
169#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200170static inline void mon_print_count_inc(Monitor *mon) { }
171static inline void mon_print_count_init(Monitor *mon) { }
172static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200173#endif /* CONFIG_DEBUG_MONITOR */
174
Blue Swirl72cf2d42009-09-12 07:36:22 +0000175static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000176
Anthony Liguoric227f092009-10-01 16:12:16 -0500177static const mon_cmd_t mon_cmds[];
178static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000179
Markus Armbruster8631b602010-02-18 11:41:55 +0100180Monitor *cur_mon;
181Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000182
aliguori731b0362009-03-05 23:01:42 +0000183static void monitor_command_cb(Monitor *mon, const char *cmdline,
184 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000185
Luiz Capitulino09069b12010-02-04 18:10:06 -0200186static inline int qmp_cmd_mode(const Monitor *mon)
187{
188 return (mon->mc ? mon->mc->command_mode : 0);
189}
190
Luiz Capitulino418173c2009-11-26 22:58:51 -0200191/* Return true if in control mode, false otherwise */
192static inline int monitor_ctrl_mode(const Monitor *mon)
193{
194 return (mon->flags & MONITOR_USE_CONTROL);
195}
196
aliguori731b0362009-03-05 23:01:42 +0000197static void monitor_read_command(Monitor *mon, int show_prompt)
198{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200199 if (!mon->rs)
200 return;
201
aliguori731b0362009-03-05 23:01:42 +0000202 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
203 if (show_prompt)
204 readline_show_prompt(mon->rs);
205}
bellard6a00d602005-11-21 23:25:50 +0000206
aliguoricde76ee2009-03-05 23:01:51 +0000207static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
208 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000209{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100210 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100211 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100212 return -EINVAL;
213 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000214 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
215 /* prompt is printed on return from the command handler */
216 return 0;
217 } else {
218 monitor_printf(mon, "terminal does not support password prompting\n");
219 return -ENOTTY;
220 }
aliguoribb5fc202009-03-05 23:01:15 +0000221}
222
aliguori376253e2009-03-05 23:01:23 +0000223void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000224{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200225 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000226 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
227 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000228 }
229}
230
231/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000232static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000233{
ths60fe76f2007-12-16 03:02:09 +0000234 char c;
aliguori731b0362009-03-05 23:01:42 +0000235
bellard7e2515e2004-08-01 21:52:19 +0000236 for(;;) {
237 c = *str++;
238 if (c == '\0')
239 break;
bellard7ba12602006-07-14 20:26:42 +0000240 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000241 mon->outbuf[mon->outbuf_index++] = '\r';
242 mon->outbuf[mon->outbuf_index++] = c;
243 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
244 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000245 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000246 }
247}
248
aliguori376253e2009-03-05 23:01:23 +0000249void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000250{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200251 char buf[4096];
252
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200253 if (!mon)
254 return;
255
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200256 mon_print_count_inc(mon);
257
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200258 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200259 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200260 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200261
262 vsnprintf(buf, sizeof(buf), fmt, ap);
263 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000264}
265
aliguori376253e2009-03-05 23:01:23 +0000266void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000267{
268 va_list ap;
269 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000270 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000271 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000272}
273
aliguori376253e2009-03-05 23:01:23 +0000274void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000275{
276 int i;
277
278 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000279 switch (filename[i]) {
280 case ' ':
281 case '"':
282 case '\\':
283 monitor_printf(mon, "\\%c", filename[i]);
284 break;
285 case '\t':
286 monitor_printf(mon, "\\t");
287 break;
288 case '\r':
289 monitor_printf(mon, "\\r");
290 break;
291 case '\n':
292 monitor_printf(mon, "\\n");
293 break;
294 default:
295 monitor_printf(mon, "%c", filename[i]);
296 break;
297 }
thsfef30742006-12-22 14:11:32 +0000298 }
299}
300
bellard7fe48482004-10-09 18:08:01 +0000301static int monitor_fprintf(FILE *stream, const char *fmt, ...)
302{
303 va_list ap;
304 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000305 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000306 va_end(ap);
307 return 0;
308}
309
Luiz Capitulino13c74252009-10-07 13:41:55 -0300310static void monitor_user_noop(Monitor *mon, const QObject *data) { }
311
Luiz Capitulino13917be2009-10-07 13:41:54 -0300312static inline int monitor_handler_ported(const mon_cmd_t *cmd)
313{
314 return cmd->user_print != NULL;
315}
316
Adam Litke940cc302010-01-25 12:18:44 -0600317static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
318{
319 return cmd->async != 0;
320}
321
Luiz Capitulino8204a912009-11-18 23:05:31 -0200322static inline int monitor_has_error(const Monitor *mon)
323{
324 return mon->error != NULL;
325}
326
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200327static void monitor_json_emitter(Monitor *mon, const QObject *data)
328{
329 QString *json;
330
331 json = qobject_to_json(data);
332 assert(json != NULL);
333
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200334 qstring_append_chr(json, '\n');
335 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200336
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200337 QDECREF(json);
338}
339
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200340static void monitor_protocol_emitter(Monitor *mon, QObject *data)
341{
342 QDict *qmp;
343
344 qmp = qdict_new();
345
346 if (!monitor_has_error(mon)) {
347 /* success response */
348 if (data) {
349 qobject_incref(data);
350 qdict_put_obj(qmp, "return", data);
351 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200352 /* return an empty QDict by default */
353 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200354 }
355 } else {
356 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100357 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200358 qdict_put(qmp, "error", mon->error->error);
359 QINCREF(mon->error->error);
360 QDECREF(mon->error);
361 mon->error = NULL;
362 }
363
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200364 if (mon->mc->id) {
365 qdict_put_obj(qmp, "id", mon->mc->id);
366 mon->mc->id = NULL;
367 }
368
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200369 monitor_json_emitter(mon, QOBJECT(qmp));
370 QDECREF(qmp);
371}
372
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200373static void timestamp_put(QDict *qdict)
374{
375 int err;
376 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000377 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200378
Blue Swirld08d6f02009-12-04 18:06:39 +0000379 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200380 if (err < 0)
381 return;
382
383 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
384 "'microseconds': %" PRId64 " }",
385 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200386 qdict_put_obj(qdict, "timestamp", obj);
387}
388
389/**
390 * monitor_protocol_event(): Generate a Monitor event
391 *
392 * Event-specific data can be emitted through the (optional) 'data' parameter.
393 */
394void monitor_protocol_event(MonitorEvent event, QObject *data)
395{
396 QDict *qmp;
397 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600398 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200399
Blue Swirl242cd002009-12-04 18:05:45 +0000400 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200401
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200402 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000403 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200404 event_name = "SHUTDOWN";
405 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000406 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200407 event_name = "RESET";
408 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000409 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200410 event_name = "POWERDOWN";
411 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000412 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200413 event_name = "STOP";
414 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200415 case QEVENT_VNC_CONNECTED:
416 event_name = "VNC_CONNECTED";
417 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200418 case QEVENT_VNC_INITIALIZED:
419 event_name = "VNC_INITIALIZED";
420 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200421 case QEVENT_VNC_DISCONNECTED:
422 event_name = "VNC_DISCONNECTED";
423 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200424 case QEVENT_BLOCK_IO_ERROR:
425 event_name = "BLOCK_IO_ERROR";
426 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300427 case QEVENT_RTC_CHANGE:
428 event_name = "RTC_CHANGE";
429 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300430 case QEVENT_WATCHDOG:
431 event_name = "WATCHDOG";
432 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200433 default:
434 abort();
435 break;
436 }
437
438 qmp = qdict_new();
439 timestamp_put(qmp);
440 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200441 if (data) {
442 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200443 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200444 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200445
Adam Litkef039a562010-01-15 08:34:02 -0600446 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200447 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200448 monitor_json_emitter(mon, QOBJECT(qmp));
449 }
Adam Litkef039a562010-01-15 08:34:02 -0600450 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200451 QDECREF(qmp);
452}
453
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200454static int do_qmp_capabilities(Monitor *mon, const QDict *params,
455 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200456{
457 /* Will setup QMP capabilities in the future */
458 if (monitor_ctrl_mode(mon)) {
459 mon->mc->command_mode = 1;
460 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200461
462 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200463}
464
bellard9dc39cb2004-03-14 21:38:27 +0000465static int compare_cmd(const char *name, const char *list)
466{
467 const char *p, *pstart;
468 int len;
469 len = strlen(name);
470 p = list;
471 for(;;) {
472 pstart = p;
473 p = strchr(p, '|');
474 if (!p)
475 p = pstart + strlen(pstart);
476 if ((p - pstart) == len && !memcmp(pstart, name, len))
477 return 1;
478 if (*p == '\0')
479 break;
480 p++;
481 }
482 return 0;
483}
484
Anthony Liguoric227f092009-10-01 16:12:16 -0500485static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000486 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000487{
Anthony Liguoric227f092009-10-01 16:12:16 -0500488 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000489
490 for(cmd = cmds; cmd->name != NULL; cmd++) {
491 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000492 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
493 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000494 }
495}
496
aliguori376253e2009-03-05 23:01:23 +0000497static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000498{
499 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000500 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000501 } else {
aliguori376253e2009-03-05 23:01:23 +0000502 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000503 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000504 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000505 monitor_printf(mon, "Log items (comma separated):\n");
506 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000507 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000508 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000509 }
510 }
bellard9dc39cb2004-03-14 21:38:27 +0000511 }
512}
513
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300514static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300515{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300516 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300517}
518
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300519static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000520{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200521 int all_devices;
522 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300523 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000524
bellard7954c732006-08-01 15:52:40 +0000525 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000526 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200527 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300528 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200529 continue;
530 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000531 }
532}
533
Adam Litke940cc302010-01-25 12:18:44 -0600534static void user_monitor_complete(void *opaque, QObject *ret_data)
535{
536 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
537
538 if (ret_data) {
539 data->user_print(data->mon, ret_data);
540 }
541 monitor_resume(data->mon);
542 qemu_free(data);
543}
544
545static void qmp_monitor_complete(void *opaque, QObject *ret_data)
546{
547 monitor_protocol_emitter(opaque, ret_data);
548}
549
550static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
551 const QDict *params)
552{
553 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
554}
555
556static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
557{
558 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
559}
560
561static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
562 const QDict *params)
563{
564 int ret;
565
566 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
567 cb_data->mon = mon;
568 cb_data->user_print = cmd->user_print;
569 monitor_suspend(mon);
570 ret = cmd->mhandler.cmd_async(mon, params,
571 user_monitor_complete, cb_data);
572 if (ret < 0) {
573 monitor_resume(mon);
574 qemu_free(cb_data);
575 }
576}
577
578static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
579{
580 int ret;
581
582 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
583 cb_data->mon = mon;
584 cb_data->user_print = cmd->user_print;
585 monitor_suspend(mon);
586 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
587 if (ret < 0) {
588 monitor_resume(mon);
589 qemu_free(cb_data);
590 }
591}
592
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200593static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000594{
Anthony Liguoric227f092009-10-01 16:12:16 -0500595 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300596 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000597
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200598 if (!item) {
599 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000600 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200601 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300602
603 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000604 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300605 break;
bellard9dc39cb2004-03-14 21:38:27 +0000606 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300607
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200608 if (cmd->name == NULL) {
609 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100610 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200611 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200612 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300613 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200614 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300615
Adam Litke940cc302010-01-25 12:18:44 -0600616 if (monitor_handler_is_async(cmd)) {
617 if (monitor_ctrl_mode(mon)) {
618 qmp_async_info_handler(mon, cmd);
619 } else {
620 user_async_info_handler(mon, cmd);
621 }
622 /*
623 * Indicate that this command is asynchronous and will not return any
624 * data (not even empty). Instead, the data will be returned via a
625 * completion callback.
626 */
627 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
628 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300629 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200630
631 if (!monitor_ctrl_mode(mon)) {
632 /*
633 * User Protocol function is called here, Monitor Protocol is
634 * handled by monitor_call_handler()
635 */
636 if (*ret_data)
637 cmd->user_print(mon, *ret_data);
638 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300639 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200640 if (monitor_ctrl_mode(mon)) {
641 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100642 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200643 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200644 } else {
645 cmd->mhandler.info(mon);
646 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300647 }
648
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200649 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300650
651help:
652 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200653 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000654}
655
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200656static void do_info_version_print(Monitor *mon, const QObject *data)
657{
658 QDict *qdict;
659
660 qdict = qobject_to_qdict(data);
661
662 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
663 qdict_get_str(qdict, "package"));
664}
665
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300666/**
667 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200668 *
669 * Return a QDict with the following information:
670 *
671 * - "qemu": QEMU's version
672 * - "package": package's version
673 *
674 * Example:
675 *
676 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300677 */
678static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000679{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200680 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
681 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000682}
683
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200684static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000685{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200686 QDict *qdict;
687
688 qdict = qobject_to_qdict(data);
689 if (qdict_size(qdict) == 0) {
690 return;
691 }
692
693 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
694}
695
696/**
697 * do_info_name(): Show VM name
698 *
699 * Return a QDict with the following information:
700 *
701 * - "name": VM's name (optional)
702 *
703 * Example:
704 *
705 * { "name": "qemu-name" }
706 */
707static void do_info_name(Monitor *mon, QObject **ret_data)
708{
709 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
710 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000711}
712
Luiz Capitulino1a728672009-12-10 17:15:56 -0200713static QObject *get_cmd_dict(const char *name)
714{
715 const char *p;
716
717 /* Remove '|' from some commands */
718 p = strchr(name, '|');
719 if (p) {
720 p++;
721 } else {
722 p = name;
723 }
724
725 return qobject_from_jsonf("{ 'name': %s }", p);
726}
727
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200728/**
729 * do_info_commands(): List QMP available commands
730 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200731 * Each command is represented by a QDict, the returned QObject is a QList
732 * of all commands.
733 *
734 * The QDict contains:
735 *
736 * - "name": command's name
737 *
738 * Example:
739 *
740 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200741 */
742static void do_info_commands(Monitor *mon, QObject **ret_data)
743{
744 QList *cmd_list;
745 const mon_cmd_t *cmd;
746
747 cmd_list = qlist_new();
748
749 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
750 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200751 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200752 }
753 }
754
755 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
756 if (monitor_handler_ported(cmd)) {
757 char buf[128];
758 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200759 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200760 }
761 }
762
763 *ret_data = QOBJECT(cmd_list);
764}
765
aurel32bf4f74c2008-12-18 22:42:34 +0000766#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200767static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000768{
aliguori376253e2009-03-05 23:01:23 +0000769 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200770 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
771 "enabled" : "disabled");
772}
773
774/**
775 * do_info_hpet(): Show HPET state
776 *
777 * Return a QDict with the following information:
778 *
779 * - "enabled": true if hpet if enabled, false otherwise
780 *
781 * Example:
782 *
783 * { "enabled": true }
784 */
785static void do_info_hpet(Monitor *mon, QObject **ret_data)
786{
787 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000788}
aurel32bf4f74c2008-12-18 22:42:34 +0000789#endif
aliguori16b29ae2008-12-17 23:28:44 +0000790
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200791static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000792{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200793 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
794}
795
796/**
797 * do_info_uuid(): Show VM UUID
798 *
799 * Return a QDict with the following information:
800 *
801 * - "UUID": Universally Unique Identifier
802 *
803 * Example:
804 *
805 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
806 */
807static void do_info_uuid(Monitor *mon, QObject **ret_data)
808{
809 char uuid[64];
810
811 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000812 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
813 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
814 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
815 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200816 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000817}
818
bellard6a00d602005-11-21 23:25:50 +0000819/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000820static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000821{
822 CPUState *env;
823
824 for(env = first_cpu; env != NULL; env = env->next_cpu) {
825 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000826 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000827 return 0;
828 }
829 }
830 return -1;
831}
832
pbrook9596ebb2007-11-18 01:44:38 +0000833static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000834{
aliguori731b0362009-03-05 23:01:42 +0000835 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000836 mon_set_cpu(0);
837 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300838 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000839 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000840}
841
aliguori376253e2009-03-05 23:01:23 +0000842static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000843{
bellard6a00d602005-11-21 23:25:50 +0000844 CPUState *env;
845 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000846#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000847 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000848 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000849#else
aliguori376253e2009-03-05 23:01:23 +0000850 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000851 0);
bellard9307c4c2004-04-04 12:57:25 +0000852#endif
853}
854
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300855static void print_cpu_iter(QObject *obj, void *opaque)
856{
857 QDict *cpu;
858 int active = ' ';
859 Monitor *mon = opaque;
860
861 assert(qobject_type(obj) == QTYPE_QDICT);
862 cpu = qobject_to_qdict(obj);
863
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200864 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300865 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200866 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300867
868 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
869
870#if defined(TARGET_I386)
871 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
872 (target_ulong) qdict_get_int(cpu, "pc"));
873#elif defined(TARGET_PPC)
874 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
875 (target_long) qdict_get_int(cpu, "nip"));
876#elif defined(TARGET_SPARC)
877 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
878 (target_long) qdict_get_int(cpu, "pc"));
879 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
880 (target_long) qdict_get_int(cpu, "npc"));
881#elif defined(TARGET_MIPS)
882 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
883 (target_long) qdict_get_int(cpu, "PC"));
884#endif
885
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200886 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300887 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200888 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300889
890 monitor_printf(mon, "\n");
891}
892
893static void monitor_print_cpus(Monitor *mon, const QObject *data)
894{
895 QList *cpu_list;
896
897 assert(qobject_type(data) == QTYPE_QLIST);
898 cpu_list = qobject_to_qlist(data);
899 qlist_iter(cpu_list, print_cpu_iter, mon);
900}
901
902/**
903 * do_info_cpus(): Show CPU information
904 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200905 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300906 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200907 * - "cpu": CPU index
908 * - "current": true if this is the current CPU, false otherwise
909 * - "halted": true if the cpu is halted, false otherwise
910 * - Current program counter. The key's name depends on the architecture:
911 * "pc": i386/x86)64
912 * "nip": PPC
913 * "pc" and "npc": sparc
914 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300915 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200916 * Example:
917 *
918 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
919 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300920 */
921static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000922{
923 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300924 QList *cpu_list;
925
926 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000927
928 /* just to set the default cpu if not already done */
929 mon_get_cpu();
930
931 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200932 QDict *cpu;
933 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300934
Avi Kivity4c0960c2009-08-17 23:19:53 +0300935 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300936
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200937 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
938 env->cpu_index, env == mon->mon_cpu,
939 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200940
941 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300942
bellard6a00d602005-11-21 23:25:50 +0000943#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300944 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000945#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300946 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000947#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300948 qdict_put(cpu, "pc", qint_from_int(env->pc));
949 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000950#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300951 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000952#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300953
954 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000955 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300956
957 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000958}
959
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200960static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000961{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300962 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200963 if (mon_set_cpu(index) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100964 qerror_report(QERR_INVALID_PARAMETER, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200965 return -1;
966 }
967 return 0;
bellard6a00d602005-11-21 23:25:50 +0000968}
969
aliguori376253e2009-03-05 23:01:23 +0000970static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000971{
aliguori376253e2009-03-05 23:01:23 +0000972 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000973}
974
aliguori376253e2009-03-05 23:01:23 +0000975static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000976{
977 int i;
bellard7e2515e2004-08-01 21:52:19 +0000978 const char *str;
ths3b46e622007-09-17 08:09:54 +0000979
aliguoricde76ee2009-03-05 23:01:51 +0000980 if (!mon->rs)
981 return;
bellard7e2515e2004-08-01 21:52:19 +0000982 i = 0;
983 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000984 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000985 if (!str)
986 break;
aliguori376253e2009-03-05 23:01:23 +0000987 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000988 i++;
bellardaa455482004-04-04 13:07:25 +0000989 }
990}
991
j_mayer76a66252007-03-07 08:32:30 +0000992#if defined(TARGET_PPC)
993/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000994static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000995{
996 CPUState *env;
997
998 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000999 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +00001000}
1001#endif
1002
Luiz Capitulinob223f352009-10-07 13:41:56 -03001003/**
1004 * do_quit(): Quit QEMU execution
1005 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001006static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001007{
1008 exit(0);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001009 return 0;
bellard9dc39cb2004-03-14 21:38:27 +00001010}
1011
aliguori376253e2009-03-05 23:01:23 +00001012static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +00001013{
1014 if (bdrv_is_inserted(bs)) {
1015 if (!force) {
1016 if (!bdrv_is_removable(bs)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001017 qerror_report(QERR_DEVICE_NOT_REMOVABLE,
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +01001018 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +00001019 return -1;
1020 }
1021 if (bdrv_is_locked(bs)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001022 qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +00001023 return -1;
1024 }
1025 }
1026 bdrv_close(bs);
1027 }
1028 return 0;
1029}
1030
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001031static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +00001032{
1033 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001034 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -02001035 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +00001036
bellard9307c4c2004-04-04 12:57:25 +00001037 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +00001038 if (!bs) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001039 qerror_report(QERR_DEVICE_NOT_FOUND, filename);
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001040 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001041 }
Luiz Capitulino9b9d4d92010-02-10 23:49:50 -02001042 return eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +00001043}
1044
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001045static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001046 QObject **ret_data)
1047{
1048 BlockDriverState *bs;
1049
1050 bs = bdrv_find(qdict_get_str(qdict, "device"));
1051 if (!bs) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001052 qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001053 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001054 }
1055
1056 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001057 qerror_report(QERR_INVALID_PASSWORD);
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001058 return -1;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001059 }
Luiz Capitulinoba85d352010-02-10 23:49:52 -02001060
1061 return 0;
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -02001062}
1063
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001064static int do_change_block(Monitor *mon, const char *device,
1065 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001066{
1067 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001068 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001069
bellard9307c4c2004-04-04 12:57:25 +00001070 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001071 if (!bs) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001072 qerror_report(QERR_DEVICE_NOT_FOUND, device);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001073 return -1;
bellard9dc39cb2004-03-14 21:38:27 +00001074 }
aurel322ecea9b2008-06-18 22:10:01 +00001075 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001076 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001077 if (!drv) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001078 qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001079 return -1;
aurel322ecea9b2008-06-18 22:10:01 +00001080 }
1081 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001082 if (eject_device(mon, bs, 0) < 0) {
1083 return -1;
1084 }
1085 if (bdrv_open2(bs, filename, BDRV_O_RDWR, drv) < 0) {
1086 return -1;
1087 }
1088 return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001089}
1090
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001091static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001092{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001093 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001094 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001095 return -1;
1096 }
aliguoribb5fc202009-03-05 23:01:15 +00001097
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001098 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +01001099}
1100
1101static void change_vnc_password_cb(Monitor *mon, const char *password,
1102 void *opaque)
1103{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001104 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001105 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001106}
1107
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001108static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001109{
ths70848512007-08-25 01:37:05 +00001110 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001111 strcmp(target, "password") == 0) {
1112 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001113 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001114 strncpy(password, arg, sizeof(password));
1115 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001116 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001117 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001118 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001119 }
ths70848512007-08-25 01:37:05 +00001120 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001121 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001122 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001123 return -1;
1124 }
ths70848512007-08-25 01:37:05 +00001125 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001126
1127 return 0;
thse25a5822007-08-25 01:36:20 +00001128}
1129
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001130/**
1131 * do_change(): Change a removable medium, or VNC configuration
1132 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001133static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001134{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001135 const char *device = qdict_get_str(qdict, "device");
1136 const char *target = qdict_get_str(qdict, "target");
1137 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001138 int ret;
1139
thse25a5822007-08-25 01:36:20 +00001140 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001141 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001142 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001143 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001144 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02001145
1146 return ret;
thse25a5822007-08-25 01:36:20 +00001147}
1148
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001149static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001150{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001151 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001152}
1153
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001154static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001155{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001156 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001157}
1158
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001159static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001160{
1161 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001162 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001163
bellard9307c4c2004-04-04 12:57:25 +00001164 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001165 mask = 0;
1166 } else {
bellard9307c4c2004-04-04 12:57:25 +00001167 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001168 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001169 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001170 return;
1171 }
1172 }
1173 cpu_set_log(mask);
1174}
1175
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001176static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001177{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001178 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001179 if (!option || !strcmp(option, "on")) {
1180 singlestep = 1;
1181 } else if (!strcmp(option, "off")) {
1182 singlestep = 0;
1183 } else {
1184 monitor_printf(mon, "unexpected option %s\n", option);
1185 }
1186}
1187
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001188/**
1189 * do_stop(): Stop VM execution
1190 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001191static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001192{
1193 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001194 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001195}
1196
aliguoribb5fc202009-03-05 23:01:15 +00001197static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001198
aliguori376253e2009-03-05 23:01:23 +00001199struct bdrv_iterate_context {
1200 Monitor *mon;
1201 int err;
1202};
aliguoric0f4ce72009-03-05 23:01:01 +00001203
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001204/**
1205 * do_cont(): Resume emulation.
1206 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001207static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001208{
1209 struct bdrv_iterate_context context = { mon, 0 };
1210
1211 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001212 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001213 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001214 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001215 return 0;
1216 } else {
1217 return -1;
1218 }
bellard8a7ddc32004-03-31 19:00:16 +00001219}
1220
aliguoribb5fc202009-03-05 23:01:15 +00001221static void bdrv_key_cb(void *opaque, int err)
1222{
aliguori376253e2009-03-05 23:01:23 +00001223 Monitor *mon = opaque;
1224
aliguoribb5fc202009-03-05 23:01:15 +00001225 /* another key was set successfully, retry to continue */
1226 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001227 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001228}
1229
1230static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1231{
aliguori376253e2009-03-05 23:01:23 +00001232 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001233
aliguori376253e2009-03-05 23:01:23 +00001234 if (!context->err && bdrv_key_required(bs)) {
1235 context->err = -EBUSY;
1236 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1237 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001238 }
1239}
1240
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001241static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001242{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001243 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001244 if (!device)
1245 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1246 if (gdbserver_start(device) < 0) {
1247 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1248 device);
1249 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001250 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001251 } else {
aliguori59030a82009-04-05 18:43:41 +00001252 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1253 device);
bellard8a7ddc32004-03-31 19:00:16 +00001254 }
1255}
1256
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001257static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001258{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001259 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001260 if (select_watchdog_action(action) == -1) {
1261 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1262 }
1263}
1264
aliguori376253e2009-03-05 23:01:23 +00001265static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001266{
aliguori376253e2009-03-05 23:01:23 +00001267 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001268 switch(c) {
1269 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001271 break;
1272 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001273 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001274 break;
1275 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001277 break;
1278 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001279 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001280 break;
1281 default:
1282 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001283 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001284 } else {
aliguori376253e2009-03-05 23:01:23 +00001285 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001286 }
1287 break;
1288 }
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001290}
1291
aliguori376253e2009-03-05 23:01:23 +00001292static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001293 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001294{
bellard6a00d602005-11-21 23:25:50 +00001295 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001296 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001297 uint8_t buf[16];
1298 uint64_t v;
1299
1300 if (format == 'i') {
1301 int flags;
1302 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001303 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001304#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001305 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001306 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001307 } else if (wsize == 4) {
1308 flags = 0;
1309 } else {
bellard6a15fd12006-04-12 21:07:07 +00001310 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001311 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001312 if (env) {
1313#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001314 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001315 (env->segs[R_CS].flags & DESC_L_MASK))
1316 flags = 2;
1317 else
1318#endif
1319 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1320 flags = 1;
1321 }
bellard4c27ba22004-04-25 18:05:08 +00001322 }
1323#endif
aliguori376253e2009-03-05 23:01:23 +00001324 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001325 return;
1326 }
1327
1328 len = wsize * count;
1329 if (wsize == 1)
1330 line_size = 8;
1331 else
1332 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001333 max_digits = 0;
1334
1335 switch(format) {
1336 case 'o':
1337 max_digits = (wsize * 8 + 2) / 3;
1338 break;
1339 default:
1340 case 'x':
1341 max_digits = (wsize * 8) / 4;
1342 break;
1343 case 'u':
1344 case 'd':
1345 max_digits = (wsize * 8 * 10 + 32) / 33;
1346 break;
1347 case 'c':
1348 wsize = 1;
1349 break;
1350 }
1351
1352 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001353 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001354 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001355 else
aliguori376253e2009-03-05 23:01:23 +00001356 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001357 l = len;
1358 if (l > line_size)
1359 l = line_size;
1360 if (is_physical) {
1361 cpu_physical_memory_rw(addr, buf, l, 0);
1362 } else {
bellard6a00d602005-11-21 23:25:50 +00001363 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001364 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001366 break;
1367 }
bellard9307c4c2004-04-04 12:57:25 +00001368 }
ths5fafdf22007-09-16 21:08:06 +00001369 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001370 while (i < l) {
1371 switch(wsize) {
1372 default:
1373 case 1:
1374 v = ldub_raw(buf + i);
1375 break;
1376 case 2:
1377 v = lduw_raw(buf + i);
1378 break;
1379 case 4:
bellard92a31b12005-02-10 22:00:52 +00001380 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001381 break;
1382 case 8:
1383 v = ldq_raw(buf + i);
1384 break;
1385 }
aliguori376253e2009-03-05 23:01:23 +00001386 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001387 switch(format) {
1388 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001389 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001390 break;
1391 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001392 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001393 break;
1394 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001395 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001396 break;
1397 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001399 break;
1400 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001402 break;
1403 }
1404 i += wsize;
1405 }
aliguori376253e2009-03-05 23:01:23 +00001406 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001407 addr += l;
1408 len -= l;
1409 }
1410}
1411
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001412static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001413{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001414 int count = qdict_get_int(qdict, "count");
1415 int format = qdict_get_int(qdict, "format");
1416 int size = qdict_get_int(qdict, "size");
1417 target_long addr = qdict_get_int(qdict, "addr");
1418
aliguori376253e2009-03-05 23:01:23 +00001419 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001420}
1421
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001422static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001423{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001424 int count = qdict_get_int(qdict, "count");
1425 int format = qdict_get_int(qdict, "format");
1426 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001427 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001428
aliguori376253e2009-03-05 23:01:23 +00001429 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001430}
1431
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001432static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001433{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001434 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001435 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001436
blueswir17743e582007-09-24 18:39:04 +00001437#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001438 switch(format) {
1439 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001440 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001441 break;
1442 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001443 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001444 break;
1445 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001446 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001447 break;
1448 default:
1449 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001450 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001451 break;
1452 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001453 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001454 break;
1455 }
bellard92a31b12005-02-10 22:00:52 +00001456#else
1457 switch(format) {
1458 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001459 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001460 break;
1461 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001462 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001463 break;
1464 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001465 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001466 break;
1467 default:
1468 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001470 break;
1471 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001472 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001473 break;
1474 }
1475#endif
aliguori376253e2009-03-05 23:01:23 +00001476 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001477}
1478
Luiz Capitulino98696222010-02-10 23:49:58 -02001479static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001480{
1481 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001482 uint32_t size = qdict_get_int(qdict, "size");
1483 const char *filename = qdict_get_str(qdict, "filename");
1484 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001485 uint32_t l;
1486 CPUState *env;
1487 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001488 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001489
1490 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001491
1492 f = fopen(filename, "wb");
1493 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001494 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001495 return -1;
bellardb371dc52007-01-03 15:20:39 +00001496 }
1497 while (size != 0) {
1498 l = sizeof(buf);
1499 if (l > size)
1500 l = size;
1501 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001502 if (fwrite(buf, 1, l, f) != l) {
1503 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1504 goto exit;
1505 }
bellardb371dc52007-01-03 15:20:39 +00001506 addr += l;
1507 size -= l;
1508 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001509
1510 ret = 0;
1511
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001512exit:
bellardb371dc52007-01-03 15:20:39 +00001513 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001514 return ret;
bellardb371dc52007-01-03 15:20:39 +00001515}
1516
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001517static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001518 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001519{
1520 FILE *f;
1521 uint32_t l;
1522 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001523 uint32_t size = qdict_get_int(qdict, "size");
1524 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001525 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001526 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001527
1528 f = fopen(filename, "wb");
1529 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001530 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001531 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001532 }
1533 while (size != 0) {
1534 l = sizeof(buf);
1535 if (l > size)
1536 l = size;
1537 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001538 if (fwrite(buf, 1, l, f) != l) {
1539 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1540 goto exit;
1541 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001542 fflush(f);
1543 addr += l;
1544 size -= l;
1545 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001546
1547 ret = 0;
1548
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001549exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001550 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001551 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001552}
1553
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001554static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001555{
1556 uint32_t addr;
1557 uint8_t buf[1];
1558 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001559 uint32_t start = qdict_get_int(qdict, "start");
1560 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001561
1562 sum = 0;
1563 for(addr = start; addr < (start + size); addr++) {
1564 cpu_physical_memory_rw(addr, buf, 1, 0);
1565 /* BSD sum algorithm ('sum' Unix command) */
1566 sum = (sum >> 1) | (sum << 15);
1567 sum += buf[0];
1568 }
aliguori376253e2009-03-05 23:01:23 +00001569 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001570}
1571
bellarda3a91a32004-06-04 11:06:21 +00001572typedef struct {
1573 int keycode;
1574 const char *name;
1575} KeyDef;
1576
1577static const KeyDef key_defs[] = {
1578 { 0x2a, "shift" },
1579 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001580
bellarda3a91a32004-06-04 11:06:21 +00001581 { 0x38, "alt" },
1582 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001583 { 0x64, "altgr" },
1584 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001585 { 0x1d, "ctrl" },
1586 { 0x9d, "ctrl_r" },
1587
1588 { 0xdd, "menu" },
1589
1590 { 0x01, "esc" },
1591
1592 { 0x02, "1" },
1593 { 0x03, "2" },
1594 { 0x04, "3" },
1595 { 0x05, "4" },
1596 { 0x06, "5" },
1597 { 0x07, "6" },
1598 { 0x08, "7" },
1599 { 0x09, "8" },
1600 { 0x0a, "9" },
1601 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001602 { 0x0c, "minus" },
1603 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001604 { 0x0e, "backspace" },
1605
1606 { 0x0f, "tab" },
1607 { 0x10, "q" },
1608 { 0x11, "w" },
1609 { 0x12, "e" },
1610 { 0x13, "r" },
1611 { 0x14, "t" },
1612 { 0x15, "y" },
1613 { 0x16, "u" },
1614 { 0x17, "i" },
1615 { 0x18, "o" },
1616 { 0x19, "p" },
1617
1618 { 0x1c, "ret" },
1619
1620 { 0x1e, "a" },
1621 { 0x1f, "s" },
1622 { 0x20, "d" },
1623 { 0x21, "f" },
1624 { 0x22, "g" },
1625 { 0x23, "h" },
1626 { 0x24, "j" },
1627 { 0x25, "k" },
1628 { 0x26, "l" },
1629
1630 { 0x2c, "z" },
1631 { 0x2d, "x" },
1632 { 0x2e, "c" },
1633 { 0x2f, "v" },
1634 { 0x30, "b" },
1635 { 0x31, "n" },
1636 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001637 { 0x33, "comma" },
1638 { 0x34, "dot" },
1639 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001640
balrog4d3b6f62008-02-10 16:33:14 +00001641 { 0x37, "asterisk" },
1642
bellarda3a91a32004-06-04 11:06:21 +00001643 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001644 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001645 { 0x3b, "f1" },
1646 { 0x3c, "f2" },
1647 { 0x3d, "f3" },
1648 { 0x3e, "f4" },
1649 { 0x3f, "f5" },
1650 { 0x40, "f6" },
1651 { 0x41, "f7" },
1652 { 0x42, "f8" },
1653 { 0x43, "f9" },
1654 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001655 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001656 { 0x46, "scroll_lock" },
1657
bellard64866c32006-05-07 18:03:31 +00001658 { 0xb5, "kp_divide" },
1659 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001660 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001661 { 0x4e, "kp_add" },
1662 { 0x9c, "kp_enter" },
1663 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001664 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001665
1666 { 0x52, "kp_0" },
1667 { 0x4f, "kp_1" },
1668 { 0x50, "kp_2" },
1669 { 0x51, "kp_3" },
1670 { 0x4b, "kp_4" },
1671 { 0x4c, "kp_5" },
1672 { 0x4d, "kp_6" },
1673 { 0x47, "kp_7" },
1674 { 0x48, "kp_8" },
1675 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001676
bellarda3a91a32004-06-04 11:06:21 +00001677 { 0x56, "<" },
1678
1679 { 0x57, "f11" },
1680 { 0x58, "f12" },
1681
1682 { 0xb7, "print" },
1683
1684 { 0xc7, "home" },
1685 { 0xc9, "pgup" },
1686 { 0xd1, "pgdn" },
1687 { 0xcf, "end" },
1688
1689 { 0xcb, "left" },
1690 { 0xc8, "up" },
1691 { 0xd0, "down" },
1692 { 0xcd, "right" },
1693
1694 { 0xd2, "insert" },
1695 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001696#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1697 { 0xf0, "stop" },
1698 { 0xf1, "again" },
1699 { 0xf2, "props" },
1700 { 0xf3, "undo" },
1701 { 0xf4, "front" },
1702 { 0xf5, "copy" },
1703 { 0xf6, "open" },
1704 { 0xf7, "paste" },
1705 { 0xf8, "find" },
1706 { 0xf9, "cut" },
1707 { 0xfa, "lf" },
1708 { 0xfb, "help" },
1709 { 0xfc, "meta_l" },
1710 { 0xfd, "meta_r" },
1711 { 0xfe, "compose" },
1712#endif
bellarda3a91a32004-06-04 11:06:21 +00001713 { 0, NULL },
1714};
1715
1716static int get_keycode(const char *key)
1717{
1718 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001719 char *endp;
1720 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001721
1722 for(p = key_defs; p->name != NULL; p++) {
1723 if (!strcmp(key, p->name))
1724 return p->keycode;
1725 }
bellard64866c32006-05-07 18:03:31 +00001726 if (strstart(key, "0x", NULL)) {
1727 ret = strtoul(key, &endp, 0);
1728 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1729 return ret;
1730 }
bellarda3a91a32004-06-04 11:06:21 +00001731 return -1;
1732}
1733
balrogc8256f92008-06-08 22:45:01 +00001734#define MAX_KEYCODES 16
1735static uint8_t keycodes[MAX_KEYCODES];
1736static int nb_pending_keycodes;
1737static QEMUTimer *key_timer;
1738
1739static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001740{
balrogc8256f92008-06-08 22:45:01 +00001741 int keycode;
1742
1743 while (nb_pending_keycodes > 0) {
1744 nb_pending_keycodes--;
1745 keycode = keycodes[nb_pending_keycodes];
1746 if (keycode & 0x80)
1747 kbd_put_keycode(0xe0);
1748 kbd_put_keycode(keycode | 0x80);
1749 }
1750}
1751
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001752static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001753{
balrog3401c0d2008-06-04 10:05:59 +00001754 char keyname_buf[16];
1755 char *separator;
1756 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001757 const char *string = qdict_get_str(qdict, "string");
1758 int has_hold_time = qdict_haskey(qdict, "hold_time");
1759 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001760
balrogc8256f92008-06-08 22:45:01 +00001761 if (nb_pending_keycodes > 0) {
1762 qemu_del_timer(key_timer);
1763 release_keys(NULL);
1764 }
1765 if (!has_hold_time)
1766 hold_time = 100;
1767 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001768 while (1) {
1769 separator = strchr(string, '-');
1770 keyname_len = separator ? separator - string : strlen(string);
1771 if (keyname_len > 0) {
1772 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1773 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001774 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001775 return;
bellarda3a91a32004-06-04 11:06:21 +00001776 }
balrogc8256f92008-06-08 22:45:01 +00001777 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001778 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001779 return;
1780 }
1781 keyname_buf[keyname_len] = 0;
1782 keycode = get_keycode(keyname_buf);
1783 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001784 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001785 return;
1786 }
balrogc8256f92008-06-08 22:45:01 +00001787 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001788 }
balrog3401c0d2008-06-04 10:05:59 +00001789 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001790 break;
balrog3401c0d2008-06-04 10:05:59 +00001791 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001792 }
balrogc8256f92008-06-08 22:45:01 +00001793 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001794 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001795 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001796 keycode = keycodes[i];
1797 if (keycode & 0x80)
1798 kbd_put_keycode(0xe0);
1799 kbd_put_keycode(keycode & 0x7f);
1800 }
balrogc8256f92008-06-08 22:45:01 +00001801 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001802 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001803 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001804}
1805
bellard13224a82006-07-14 22:03:35 +00001806static int mouse_button_state;
1807
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001808static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001809{
1810 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001811 const char *dx_str = qdict_get_str(qdict, "dx_str");
1812 const char *dy_str = qdict_get_str(qdict, "dy_str");
1813 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001814 dx = strtol(dx_str, NULL, 0);
1815 dy = strtol(dy_str, NULL, 0);
1816 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001817 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001818 dz = strtol(dz_str, NULL, 0);
1819 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1820}
1821
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001822static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001823{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001824 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001825 mouse_button_state = button_state;
1826 kbd_mouse_event(0, 0, 0, mouse_button_state);
1827}
1828
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001829static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001830{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001831 int size = qdict_get_int(qdict, "size");
1832 int addr = qdict_get_int(qdict, "addr");
1833 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001834 uint32_t val;
1835 int suffix;
1836
1837 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001838 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001839 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001840 addr++;
1841 }
1842 addr &= 0xffff;
1843
1844 switch(size) {
1845 default:
1846 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001847 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001848 suffix = 'b';
1849 break;
1850 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001851 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001852 suffix = 'w';
1853 break;
1854 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001855 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001856 suffix = 'l';
1857 break;
1858 }
aliguori376253e2009-03-05 23:01:23 +00001859 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1860 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001861}
bellarda3a91a32004-06-04 11:06:21 +00001862
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001863static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001864{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001865 int size = qdict_get_int(qdict, "size");
1866 int addr = qdict_get_int(qdict, "addr");
1867 int val = qdict_get_int(qdict, "val");
1868
Jan Kiszkaf1147842009-07-14 10:20:11 +02001869 addr &= IOPORTS_MASK;
1870
1871 switch (size) {
1872 default:
1873 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001874 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001875 break;
1876 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001877 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001878 break;
1879 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001880 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001881 break;
1882 }
1883}
1884
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001885static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001886{
1887 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001888 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001889
Jan Kiszka76e30d02009-07-02 00:19:02 +02001890 res = qemu_boot_set(bootdevice);
1891 if (res == 0) {
1892 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1893 } else if (res > 0) {
1894 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001895 } else {
aliguori376253e2009-03-05 23:01:23 +00001896 monitor_printf(mon, "no function defined to set boot device list for "
1897 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001898 }
1899}
1900
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001901/**
1902 * do_system_reset(): Issue a machine reset
1903 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001904static int do_system_reset(Monitor *mon, const QDict *qdict,
1905 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001906{
1907 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001908 return 0;
bellarde4f90822004-06-20 12:35:44 +00001909}
1910
Luiz Capitulino43076662009-10-07 13:41:59 -03001911/**
1912 * do_system_powerdown(): Issue a machine powerdown
1913 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001914static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1915 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001916{
1917 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001918 return 0;
bellard34751872005-07-02 14:31:34 +00001919}
1920
bellardb86bda52004-09-18 19:32:46 +00001921#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001922static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001923{
aliguori376253e2009-03-05 23:01:23 +00001924 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1925 addr,
1926 pte & mask,
1927 pte & PG_GLOBAL_MASK ? 'G' : '-',
1928 pte & PG_PSE_MASK ? 'P' : '-',
1929 pte & PG_DIRTY_MASK ? 'D' : '-',
1930 pte & PG_ACCESSED_MASK ? 'A' : '-',
1931 pte & PG_PCD_MASK ? 'C' : '-',
1932 pte & PG_PWT_MASK ? 'T' : '-',
1933 pte & PG_USER_MASK ? 'U' : '-',
1934 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001935}
1936
aliguori376253e2009-03-05 23:01:23 +00001937static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001938{
bellard6a00d602005-11-21 23:25:50 +00001939 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001940 int l1, l2;
1941 uint32_t pgd, pde, pte;
1942
bellard6a00d602005-11-21 23:25:50 +00001943 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001944
bellardb86bda52004-09-18 19:32:46 +00001945 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001946 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001947 return;
1948 }
1949 pgd = env->cr[3] & ~0xfff;
1950 for(l1 = 0; l1 < 1024; l1++) {
1951 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1952 pde = le32_to_cpu(pde);
1953 if (pde & PG_PRESENT_MASK) {
1954 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001955 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001956 } else {
1957 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001958 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001959 (uint8_t *)&pte, 4);
1960 pte = le32_to_cpu(pte);
1961 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001962 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001963 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001964 ~0xfff);
1965 }
1966 }
1967 }
1968 }
1969 }
1970}
1971
aliguori376253e2009-03-05 23:01:23 +00001972static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001973 uint32_t end, int prot)
1974{
bellard9746b152004-11-11 18:30:24 +00001975 int prot1;
1976 prot1 = *plast_prot;
1977 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001978 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001979 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1980 *pstart, end, end - *pstart,
1981 prot1 & PG_USER_MASK ? 'u' : '-',
1982 'r',
1983 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001984 }
1985 if (prot != 0)
1986 *pstart = end;
1987 else
1988 *pstart = -1;
1989 *plast_prot = prot;
1990 }
1991}
1992
aliguori376253e2009-03-05 23:01:23 +00001993static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001994{
bellard6a00d602005-11-21 23:25:50 +00001995 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001996 int l1, l2, prot, last_prot;
1997 uint32_t pgd, pde, pte, start, end;
1998
bellard6a00d602005-11-21 23:25:50 +00001999 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002000
bellardb86bda52004-09-18 19:32:46 +00002001 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00002002 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00002003 return;
2004 }
2005 pgd = env->cr[3] & ~0xfff;
2006 last_prot = 0;
2007 start = -1;
2008 for(l1 = 0; l1 < 1024; l1++) {
2009 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
2010 pde = le32_to_cpu(pde);
2011 end = l1 << 22;
2012 if (pde & PG_PRESENT_MASK) {
2013 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2014 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002015 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002016 } else {
2017 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00002018 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00002019 (uint8_t *)&pte, 4);
2020 pte = le32_to_cpu(pte);
2021 end = (l1 << 22) + (l2 << 12);
2022 if (pte & PG_PRESENT_MASK) {
2023 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2024 } else {
2025 prot = 0;
2026 }
aliguori376253e2009-03-05 23:01:23 +00002027 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002028 }
2029 }
2030 } else {
2031 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002032 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002033 }
2034 }
2035}
2036#endif
2037
aurel327c664e22009-03-03 06:12:22 +00002038#if defined(TARGET_SH4)
2039
aliguori376253e2009-03-05 23:01:23 +00002040static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002041{
aliguori376253e2009-03-05 23:01:23 +00002042 monitor_printf(mon, " tlb%i:\t"
2043 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2044 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2045 "dirty=%hhu writethrough=%hhu\n",
2046 idx,
2047 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2048 tlb->v, tlb->sh, tlb->c, tlb->pr,
2049 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002050}
2051
aliguori376253e2009-03-05 23:01:23 +00002052static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002053{
2054 CPUState *env = mon_get_cpu();
2055 int i;
2056
aliguori376253e2009-03-05 23:01:23 +00002057 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002058 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002059 print_tlb (mon, i, &env->itlb[i]);
2060 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002061 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002062 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002063}
2064
2065#endif
2066
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002067static void do_info_kvm_print(Monitor *mon, const QObject *data)
2068{
2069 QDict *qdict;
2070
2071 qdict = qobject_to_qdict(data);
2072
2073 monitor_printf(mon, "kvm support: ");
2074 if (qdict_get_bool(qdict, "present")) {
2075 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2076 "enabled" : "disabled");
2077 } else {
2078 monitor_printf(mon, "not compiled\n");
2079 }
2080}
2081
2082/**
2083 * do_info_kvm(): Show KVM information
2084 *
2085 * Return a QDict with the following information:
2086 *
2087 * - "enabled": true if KVM support is enabled, false otherwise
2088 * - "present": true if QEMU has KVM support, false otherwise
2089 *
2090 * Example:
2091 *
2092 * { "enabled": true, "present": true }
2093 */
2094static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002095{
2096#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002097 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2098 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002099#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002100 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002101#endif
2102}
2103
aliguori030ea372009-04-21 22:30:47 +00002104static void do_info_numa(Monitor *mon)
2105{
aliguorib28b6232009-04-22 20:20:29 +00002106 int i;
aliguori030ea372009-04-21 22:30:47 +00002107 CPUState *env;
2108
2109 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2110 for (i = 0; i < nb_numa_nodes; i++) {
2111 monitor_printf(mon, "node %d cpus:", i);
2112 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2113 if (env->numa_node == i) {
2114 monitor_printf(mon, " %d", env->cpu_index);
2115 }
2116 }
2117 monitor_printf(mon, "\n");
2118 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2119 node_mem[i] >> 20);
2120 }
2121}
2122
bellard5f1ce942006-02-08 22:40:15 +00002123#ifdef CONFIG_PROFILER
2124
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002125int64_t qemu_time;
2126int64_t dev_time;
2127
aliguori376253e2009-03-05 23:01:23 +00002128static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002129{
2130 int64_t total;
2131 total = qemu_time;
2132 if (total == 0)
2133 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002134 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002135 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002136 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002137 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002138 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002139 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002140}
2141#else
aliguori376253e2009-03-05 23:01:23 +00002142static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002143{
aliguori376253e2009-03-05 23:01:23 +00002144 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002145}
2146#endif
2147
bellardec36b692006-07-16 18:57:03 +00002148/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002149static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002150
aliguori376253e2009-03-05 23:01:23 +00002151static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002152{
2153 int i;
2154 CaptureState *s;
2155
2156 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002157 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002158 s->ops.info (s->opaque);
2159 }
2160}
2161
Blue Swirl23130862009-06-06 08:22:04 +00002162#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002163static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002164{
2165 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002166 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002167 CaptureState *s;
2168
2169 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2170 if (i == n) {
2171 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002172 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002173 qemu_free (s);
2174 return;
2175 }
2176 }
2177}
2178
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002179static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002180{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002181 const char *path = qdict_get_str(qdict, "path");
2182 int has_freq = qdict_haskey(qdict, "freq");
2183 int freq = qdict_get_try_int(qdict, "freq", -1);
2184 int has_bits = qdict_haskey(qdict, "bits");
2185 int bits = qdict_get_try_int(qdict, "bits", -1);
2186 int has_channels = qdict_haskey(qdict, "nchannels");
2187 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002188 CaptureState *s;
2189
2190 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002191
2192 freq = has_freq ? freq : 44100;
2193 bits = has_bits ? bits : 16;
2194 nchannels = has_channels ? nchannels : 2;
2195
2196 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002197 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002198 qemu_free (s);
2199 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002200 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002201}
2202#endif
2203
aurel32dc1c0b72008-04-27 23:52:12 +00002204#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002205static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002206{
2207 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002208 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002209
2210 for (env = first_cpu; env != NULL; env = env->next_cpu)
2211 if (env->cpu_index == cpu_index) {
2212 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2213 break;
2214 }
2215}
2216#endif
2217
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002218static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002219{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002220 QDict *qdict;
2221
2222 qdict = qobject_to_qdict(data);
2223
2224 monitor_printf(mon, "VM status: ");
2225 if (qdict_get_bool(qdict, "running")) {
2226 monitor_printf(mon, "running");
2227 if (qdict_get_bool(qdict, "singlestep")) {
2228 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002229 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002230 } else {
2231 monitor_printf(mon, "paused");
2232 }
2233
2234 monitor_printf(mon, "\n");
2235}
2236
2237/**
2238 * do_info_status(): VM status
2239 *
2240 * Return a QDict with the following information:
2241 *
2242 * - "running": true if the VM is running, or false if it is paused
2243 * - "singlestep": true if the VM is in single step mode, false otherwise
2244 *
2245 * Example:
2246 *
2247 * { "running": true, "singlestep": false }
2248 */
2249static void do_info_status(Monitor *mon, QObject **ret_data)
2250{
2251 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2252 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002253}
2254
Adam Litke625a5be2010-01-26 14:17:35 -06002255static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002256{
Adam Litke625a5be2010-01-26 14:17:35 -06002257 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002258
Adam Litke625a5be2010-01-26 14:17:35 -06002259 if (strcmp(key, "actual"))
2260 monitor_printf(mon, ",%s=%" PRId64, key,
2261 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002262}
2263
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002264static void monitor_print_balloon(Monitor *mon, const QObject *data)
2265{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002266 QDict *qdict;
2267
2268 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002269 if (!qdict_haskey(qdict, "actual"))
2270 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002271
Adam Litke625a5be2010-01-26 14:17:35 -06002272 monitor_printf(mon, "balloon: actual=%" PRId64,
2273 qdict_get_int(qdict, "actual") >> 20);
2274 qdict_iter(qdict, print_balloon_stat, mon);
2275 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002276}
2277
2278/**
2279 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002280 *
Adam Litke625a5be2010-01-26 14:17:35 -06002281 * Make an asynchronous request for balloon info. When the request completes
2282 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002283 *
Adam Litke625a5be2010-01-26 14:17:35 -06002284 * - "actual": current balloon value in bytes
2285 * The following fields may or may not be present:
2286 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2287 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2288 * - "major_page_faults": Number of major faults
2289 * - "minor_page_faults": Number of minor faults
2290 * - "free_mem": Total amount of free and unused memory (bytes)
2291 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002292 *
2293 * Example:
2294 *
Adam Litke625a5be2010-01-26 14:17:35 -06002295 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2296 * "major_page_faults": 142, "minor_page_faults": 239245,
2297 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002298 */
Adam Litke625a5be2010-01-26 14:17:35 -06002299static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002300{
Adam Litke625a5be2010-01-26 14:17:35 -06002301 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002302
Adam Litke625a5be2010-01-26 14:17:35 -06002303 if (kvm_enabled() && !kvm_has_sync_mmu()) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002304 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002305 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002306 }
Adam Litke625a5be2010-01-26 14:17:35 -06002307
2308 ret = qemu_balloon_status(cb, opaque);
2309 if (!ret) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002310 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002311 return -1;
2312 }
2313
2314 return 0;
2315}
2316
2317/**
2318 * do_balloon(): Request VM to change its memory allocation
2319 */
2320static int do_balloon(Monitor *mon, const QDict *params,
2321 MonitorCompletion cb, void *opaque)
2322{
2323 int ret;
2324
2325 if (kvm_enabled() && !kvm_has_sync_mmu()) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002326 qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002327 return -1;
2328 }
2329
2330 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2331 if (ret == 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002332 qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
Adam Litke625a5be2010-01-26 14:17:35 -06002333 return -1;
2334 }
2335
Adam Litke93d67ee2010-02-22 10:51:20 -06002336 cb(opaque, NULL);
Adam Litke625a5be2010-01-26 14:17:35 -06002337 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002338}
2339
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002340static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002341{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002342 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002343
aliguori76655d62009-03-06 20:27:37 +00002344 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002345 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002346 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002347 return acl;
2348}
aliguori76655d62009-03-06 20:27:37 +00002349
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002350static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002351{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002352 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002353 qemu_acl *acl = find_acl(mon, aclname);
2354 qemu_acl_entry *entry;
2355 int i = 0;
2356
2357 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002358 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002359 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002360 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002361 i++;
2362 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002363 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002364 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002365 }
2366}
2367
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002368static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002369{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002370 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002371 qemu_acl *acl = find_acl(mon, aclname);
2372
2373 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002374 qemu_acl_reset(acl);
2375 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002376 }
2377}
aliguori76655d62009-03-06 20:27:37 +00002378
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002379static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002380{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002381 const char *aclname = qdict_get_str(qdict, "aclname");
2382 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002383 qemu_acl *acl = find_acl(mon, aclname);
2384
2385 if (acl) {
2386 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002387 acl->defaultDeny = 0;
2388 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002389 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002390 acl->defaultDeny = 1;
2391 monitor_printf(mon, "acl: policy set to 'deny'\n");
2392 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002393 monitor_printf(mon, "acl: unknown policy '%s', "
2394 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002395 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002396 }
2397}
aliguori76655d62009-03-06 20:27:37 +00002398
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002399static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002400{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002401 const char *aclname = qdict_get_str(qdict, "aclname");
2402 const char *match = qdict_get_str(qdict, "match");
2403 const char *policy = qdict_get_str(qdict, "policy");
2404 int has_index = qdict_haskey(qdict, "index");
2405 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002406 qemu_acl *acl = find_acl(mon, aclname);
2407 int deny, ret;
2408
2409 if (acl) {
2410 if (strcmp(policy, "allow") == 0) {
2411 deny = 0;
2412 } else if (strcmp(policy, "deny") == 0) {
2413 deny = 1;
2414 } else {
2415 monitor_printf(mon, "acl: unknown policy '%s', "
2416 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002417 return;
2418 }
aliguori28a76be2009-03-06 20:27:40 +00002419 if (has_index)
2420 ret = qemu_acl_insert(acl, deny, match, index);
2421 else
2422 ret = qemu_acl_append(acl, deny, match);
2423 if (ret < 0)
2424 monitor_printf(mon, "acl: unable to add acl entry\n");
2425 else
2426 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002427 }
2428}
aliguori76655d62009-03-06 20:27:37 +00002429
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002430static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002431{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002432 const char *aclname = qdict_get_str(qdict, "aclname");
2433 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002434 qemu_acl *acl = find_acl(mon, aclname);
2435 int ret;
aliguori76655d62009-03-06 20:27:37 +00002436
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002437 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002438 ret = qemu_acl_remove(acl, match);
2439 if (ret < 0)
2440 monitor_printf(mon, "acl: no matching acl entry\n");
2441 else
2442 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002443 }
2444}
2445
Huang Ying79c4f6b2009-06-23 10:05:14 +08002446#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002447static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002448{
2449 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002450 int cpu_index = qdict_get_int(qdict, "cpu_index");
2451 int bank = qdict_get_int(qdict, "bank");
2452 uint64_t status = qdict_get_int(qdict, "status");
2453 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2454 uint64_t addr = qdict_get_int(qdict, "addr");
2455 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002456
2457 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2458 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2459 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2460 break;
2461 }
2462}
2463#endif
2464
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002465static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002466{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002467 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002468 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002469 int fd;
2470
2471 fd = qemu_chr_get_msgfd(mon->chr);
2472 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002473 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002474 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002475 }
2476
2477 if (qemu_isdigit(fdname[0])) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002478 qerror_report(QERR_INVALID_PARAMETER, "fdname");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002479 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002480 }
2481
2482 fd = dup(fd);
2483 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002484 if (errno == EMFILE)
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002485 qerror_report(QERR_TOO_MANY_FILES);
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002486 else
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002487 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002488 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002489 }
2490
Blue Swirl72cf2d42009-09-12 07:36:22 +00002491 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002492 if (strcmp(monfd->name, fdname) != 0) {
2493 continue;
2494 }
2495
2496 close(monfd->fd);
2497 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002498 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002499 }
2500
Anthony Liguoric227f092009-10-01 16:12:16 -05002501 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002502 monfd->name = qemu_strdup(fdname);
2503 monfd->fd = fd;
2504
Blue Swirl72cf2d42009-09-12 07:36:22 +00002505 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002506 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002507}
2508
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002509static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002510{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002511 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002512 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002513
Blue Swirl72cf2d42009-09-12 07:36:22 +00002514 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002515 if (strcmp(monfd->name, fdname) != 0) {
2516 continue;
2517 }
2518
Blue Swirl72cf2d42009-09-12 07:36:22 +00002519 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002520 close(monfd->fd);
2521 qemu_free(monfd->name);
2522 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002523 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002524 }
2525
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002526 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002527 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002528}
2529
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002530static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002531{
2532 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002533 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002534
2535 vm_stop(0);
2536
Markus Armbruster03cd4652010-02-17 16:24:10 +01002537 if (load_vmstate(name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002538 vm_start();
2539}
2540
Mark McLoughlin7768e042009-07-22 09:11:41 +01002541int monitor_get_fd(Monitor *mon, const char *fdname)
2542{
Anthony Liguoric227f092009-10-01 16:12:16 -05002543 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002544
Blue Swirl72cf2d42009-09-12 07:36:22 +00002545 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002546 int fd;
2547
2548 if (strcmp(monfd->name, fdname) != 0) {
2549 continue;
2550 }
2551
2552 fd = monfd->fd;
2553
2554 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002555 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002556 qemu_free(monfd->name);
2557 qemu_free(monfd);
2558
2559 return fd;
2560 }
2561
2562 return -1;
2563}
2564
Anthony Liguoric227f092009-10-01 16:12:16 -05002565static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002566#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002567 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002568};
2569
Blue Swirl23130862009-06-06 08:22:04 +00002570/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002571static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 {
2573 .name = "version",
2574 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 .params = "",
2576 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002577 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002578 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002579 },
2580 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002581 .name = "commands",
2582 .args_type = "",
2583 .params = "",
2584 .help = "list QMP available commands",
2585 .user_print = monitor_user_noop,
2586 .mhandler.info_new = do_info_commands,
2587 },
2588 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002589 .name = "network",
2590 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002591 .params = "",
2592 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002593 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
2595 {
2596 .name = "chardev",
2597 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002598 .params = "",
2599 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002600 .user_print = qemu_chr_info_print,
2601 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 },
2603 {
2604 .name = "block",
2605 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 .params = "",
2607 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002608 .user_print = bdrv_info_print,
2609 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 },
2611 {
2612 .name = "blockstats",
2613 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 .params = "",
2615 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002616 .user_print = bdrv_stats_print,
2617 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002618 },
2619 {
2620 .name = "registers",
2621 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002622 .params = "",
2623 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002624 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002625 },
2626 {
2627 .name = "cpus",
2628 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 .params = "",
2630 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002631 .user_print = monitor_print_cpus,
2632 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002633 },
2634 {
2635 .name = "history",
2636 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002637 .params = "",
2638 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002639 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002640 },
2641 {
2642 .name = "irq",
2643 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002644 .params = "",
2645 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002646 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002647 },
2648 {
2649 .name = "pic",
2650 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002651 .params = "",
2652 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002653 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002654 },
2655 {
2656 .name = "pci",
2657 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002658 .params = "",
2659 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002660 .user_print = do_pci_info_print,
2661 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002662 },
aurel327c664e22009-03-03 06:12:22 +00002663#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 {
2665 .name = "tlb",
2666 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 .params = "",
2668 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002669 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002670 },
aurel327c664e22009-03-03 06:12:22 +00002671#endif
2672#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002673 {
2674 .name = "mem",
2675 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002676 .params = "",
2677 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002678 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 },
2680 {
2681 .name = "hpet",
2682 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 .params = "",
2684 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002685 .user_print = do_info_hpet_print,
2686 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 },
bellardb86bda52004-09-18 19:32:46 +00002688#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002689 {
2690 .name = "jit",
2691 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002692 .params = "",
2693 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002694 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 },
2696 {
2697 .name = "kvm",
2698 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 .params = "",
2700 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002701 .user_print = do_info_kvm_print,
2702 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 },
2704 {
2705 .name = "numa",
2706 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002707 .params = "",
2708 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002709 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 },
2711 {
2712 .name = "usb",
2713 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 .params = "",
2715 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002716 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 },
2718 {
2719 .name = "usbhost",
2720 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 .params = "",
2722 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002723 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002724 },
2725 {
2726 .name = "profile",
2727 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002728 .params = "",
2729 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002730 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 },
2732 {
2733 .name = "capture",
2734 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 .params = "",
2736 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002737 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 },
2739 {
2740 .name = "snapshots",
2741 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 .params = "",
2743 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002744 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 },
2746 {
2747 .name = "status",
2748 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002749 .params = "",
2750 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002751 .user_print = do_info_status_print,
2752 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
2754 {
2755 .name = "pcmcia",
2756 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 .params = "",
2758 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002759 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
2761 {
2762 .name = "mice",
2763 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 .params = "",
2765 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002766 .user_print = do_info_mice_print,
2767 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 },
2769 {
2770 .name = "vnc",
2771 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002772 .params = "",
2773 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002774 .user_print = do_info_vnc_print,
2775 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 },
2777 {
2778 .name = "name",
2779 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002780 .params = "",
2781 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002782 .user_print = do_info_name_print,
2783 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002784 },
2785 {
2786 .name = "uuid",
2787 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 .params = "",
2789 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002790 .user_print = do_info_uuid_print,
2791 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 },
j_mayer76a66252007-03-07 08:32:30 +00002793#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002794 {
2795 .name = "cpustats",
2796 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002797 .params = "",
2798 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002799 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002800 },
j_mayer76a66252007-03-07 08:32:30 +00002801#endif
blueswir131a60e22007-10-26 18:42:59 +00002802#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002803 {
2804 .name = "usernet",
2805 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 .params = "",
2807 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002808 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 },
blueswir131a60e22007-10-26 18:42:59 +00002810#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002811 {
2812 .name = "migrate",
2813 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002814 .params = "",
2815 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002816 .user_print = do_info_migrate_print,
2817 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002818 },
2819 {
2820 .name = "balloon",
2821 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002822 .params = "",
2823 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002824 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002825 .mhandler.info_async = do_info_balloon,
2826 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 },
2828 {
2829 .name = "qtree",
2830 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002831 .params = "",
2832 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002833 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 },
2835 {
2836 .name = "qdm",
2837 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002838 .params = "",
2839 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002840 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 },
2842 {
2843 .name = "roms",
2844 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002845 .params = "",
2846 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002847 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 },
2849 {
2850 .name = NULL,
2851 },
bellard9dc39cb2004-03-14 21:38:27 +00002852};
2853
bellard9307c4c2004-04-04 12:57:25 +00002854/*******************************************************************/
2855
2856static const char *pch;
2857static jmp_buf expr_env;
2858
bellard92a31b12005-02-10 22:00:52 +00002859#define MD_TLONG 0
2860#define MD_I32 1
2861
bellard9307c4c2004-04-04 12:57:25 +00002862typedef struct MonitorDef {
2863 const char *name;
2864 int offset;
blueswir18662d652008-10-02 18:32:44 +00002865 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002866 int type;
bellard9307c4c2004-04-04 12:57:25 +00002867} MonitorDef;
2868
bellard57206fd2004-04-25 18:54:52 +00002869#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002870static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002871{
bellard6a00d602005-11-21 23:25:50 +00002872 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002873 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002874}
2875#endif
2876
bellarda541f292004-04-12 20:39:29 +00002877#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002878static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002879{
bellard6a00d602005-11-21 23:25:50 +00002880 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002881 unsigned int u;
2882 int i;
2883
2884 u = 0;
2885 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002886 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002887
2888 return u;
2889}
2890
blueswir18662d652008-10-02 18:32:44 +00002891static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002892{
bellard6a00d602005-11-21 23:25:50 +00002893 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002894 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002895}
2896
blueswir18662d652008-10-02 18:32:44 +00002897static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002898{
bellard6a00d602005-11-21 23:25:50 +00002899 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002900 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002901}
bellard9fddaa02004-05-21 12:59:32 +00002902
blueswir18662d652008-10-02 18:32:44 +00002903static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002904{
bellard6a00d602005-11-21 23:25:50 +00002905 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002906 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002907}
2908
blueswir18662d652008-10-02 18:32:44 +00002909static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002910{
bellard6a00d602005-11-21 23:25:50 +00002911 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002912 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002913}
2914
blueswir18662d652008-10-02 18:32:44 +00002915static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002916{
bellard6a00d602005-11-21 23:25:50 +00002917 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002918 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002919}
bellarda541f292004-04-12 20:39:29 +00002920#endif
2921
bellarde95c8d52004-09-30 22:22:08 +00002922#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002923#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002924static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002925{
bellard6a00d602005-11-21 23:25:50 +00002926 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002927 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002928}
bellard7b936c02005-10-30 17:05:13 +00002929#endif
bellarde95c8d52004-09-30 22:22:08 +00002930
blueswir18662d652008-10-02 18:32:44 +00002931static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002932{
bellard6a00d602005-11-21 23:25:50 +00002933 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002934 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002935}
2936#endif
2937
blueswir18662d652008-10-02 18:32:44 +00002938static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002939#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002940
2941#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002942 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002943 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002944 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002945
bellard9307c4c2004-04-04 12:57:25 +00002946 { "eax", offsetof(CPUState, regs[0]) },
2947 { "ecx", offsetof(CPUState, regs[1]) },
2948 { "edx", offsetof(CPUState, regs[2]) },
2949 { "ebx", offsetof(CPUState, regs[3]) },
2950 { "esp|sp", offsetof(CPUState, regs[4]) },
2951 { "ebp|fp", offsetof(CPUState, regs[5]) },
2952 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002953 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002954#ifdef TARGET_X86_64
2955 { "r8", offsetof(CPUState, regs[8]) },
2956 { "r9", offsetof(CPUState, regs[9]) },
2957 { "r10", offsetof(CPUState, regs[10]) },
2958 { "r11", offsetof(CPUState, regs[11]) },
2959 { "r12", offsetof(CPUState, regs[12]) },
2960 { "r13", offsetof(CPUState, regs[13]) },
2961 { "r14", offsetof(CPUState, regs[14]) },
2962 { "r15", offsetof(CPUState, regs[15]) },
2963#endif
bellard9307c4c2004-04-04 12:57:25 +00002964 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002965 { "eip", offsetof(CPUState, eip) },
2966 SEG("cs", R_CS)
2967 SEG("ds", R_DS)
2968 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002969 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002970 SEG("fs", R_FS)
2971 SEG("gs", R_GS)
2972 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002973#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002974 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002975 { "r0", offsetof(CPUState, gpr[0]) },
2976 { "r1", offsetof(CPUState, gpr[1]) },
2977 { "r2", offsetof(CPUState, gpr[2]) },
2978 { "r3", offsetof(CPUState, gpr[3]) },
2979 { "r4", offsetof(CPUState, gpr[4]) },
2980 { "r5", offsetof(CPUState, gpr[5]) },
2981 { "r6", offsetof(CPUState, gpr[6]) },
2982 { "r7", offsetof(CPUState, gpr[7]) },
2983 { "r8", offsetof(CPUState, gpr[8]) },
2984 { "r9", offsetof(CPUState, gpr[9]) },
2985 { "r10", offsetof(CPUState, gpr[10]) },
2986 { "r11", offsetof(CPUState, gpr[11]) },
2987 { "r12", offsetof(CPUState, gpr[12]) },
2988 { "r13", offsetof(CPUState, gpr[13]) },
2989 { "r14", offsetof(CPUState, gpr[14]) },
2990 { "r15", offsetof(CPUState, gpr[15]) },
2991 { "r16", offsetof(CPUState, gpr[16]) },
2992 { "r17", offsetof(CPUState, gpr[17]) },
2993 { "r18", offsetof(CPUState, gpr[18]) },
2994 { "r19", offsetof(CPUState, gpr[19]) },
2995 { "r20", offsetof(CPUState, gpr[20]) },
2996 { "r21", offsetof(CPUState, gpr[21]) },
2997 { "r22", offsetof(CPUState, gpr[22]) },
2998 { "r23", offsetof(CPUState, gpr[23]) },
2999 { "r24", offsetof(CPUState, gpr[24]) },
3000 { "r25", offsetof(CPUState, gpr[25]) },
3001 { "r26", offsetof(CPUState, gpr[26]) },
3002 { "r27", offsetof(CPUState, gpr[27]) },
3003 { "r28", offsetof(CPUState, gpr[28]) },
3004 { "r29", offsetof(CPUState, gpr[29]) },
3005 { "r30", offsetof(CPUState, gpr[30]) },
3006 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003007 /* Floating point registers */
3008 { "f0", offsetof(CPUState, fpr[0]) },
3009 { "f1", offsetof(CPUState, fpr[1]) },
3010 { "f2", offsetof(CPUState, fpr[2]) },
3011 { "f3", offsetof(CPUState, fpr[3]) },
3012 { "f4", offsetof(CPUState, fpr[4]) },
3013 { "f5", offsetof(CPUState, fpr[5]) },
3014 { "f6", offsetof(CPUState, fpr[6]) },
3015 { "f7", offsetof(CPUState, fpr[7]) },
3016 { "f8", offsetof(CPUState, fpr[8]) },
3017 { "f9", offsetof(CPUState, fpr[9]) },
3018 { "f10", offsetof(CPUState, fpr[10]) },
3019 { "f11", offsetof(CPUState, fpr[11]) },
3020 { "f12", offsetof(CPUState, fpr[12]) },
3021 { "f13", offsetof(CPUState, fpr[13]) },
3022 { "f14", offsetof(CPUState, fpr[14]) },
3023 { "f15", offsetof(CPUState, fpr[15]) },
3024 { "f16", offsetof(CPUState, fpr[16]) },
3025 { "f17", offsetof(CPUState, fpr[17]) },
3026 { "f18", offsetof(CPUState, fpr[18]) },
3027 { "f19", offsetof(CPUState, fpr[19]) },
3028 { "f20", offsetof(CPUState, fpr[20]) },
3029 { "f21", offsetof(CPUState, fpr[21]) },
3030 { "f22", offsetof(CPUState, fpr[22]) },
3031 { "f23", offsetof(CPUState, fpr[23]) },
3032 { "f24", offsetof(CPUState, fpr[24]) },
3033 { "f25", offsetof(CPUState, fpr[25]) },
3034 { "f26", offsetof(CPUState, fpr[26]) },
3035 { "f27", offsetof(CPUState, fpr[27]) },
3036 { "f28", offsetof(CPUState, fpr[28]) },
3037 { "f29", offsetof(CPUState, fpr[29]) },
3038 { "f30", offsetof(CPUState, fpr[30]) },
3039 { "f31", offsetof(CPUState, fpr[31]) },
3040 { "fpscr", offsetof(CPUState, fpscr) },
3041 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003042 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003043 { "lr", offsetof(CPUState, lr) },
3044 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003045 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003046 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003047 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003048 { "msr", 0, &monitor_get_msr, },
3049 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003050 { "tbu", 0, &monitor_get_tbu, },
3051 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003052#if defined(TARGET_PPC64)
3053 /* Address space register */
3054 { "asr", offsetof(CPUState, asr) },
3055#endif
3056 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00003057 { "sdr1", offsetof(CPUState, sdr1) },
3058 { "sr0", offsetof(CPUState, sr[0]) },
3059 { "sr1", offsetof(CPUState, sr[1]) },
3060 { "sr2", offsetof(CPUState, sr[2]) },
3061 { "sr3", offsetof(CPUState, sr[3]) },
3062 { "sr4", offsetof(CPUState, sr[4]) },
3063 { "sr5", offsetof(CPUState, sr[5]) },
3064 { "sr6", offsetof(CPUState, sr[6]) },
3065 { "sr7", offsetof(CPUState, sr[7]) },
3066 { "sr8", offsetof(CPUState, sr[8]) },
3067 { "sr9", offsetof(CPUState, sr[9]) },
3068 { "sr10", offsetof(CPUState, sr[10]) },
3069 { "sr11", offsetof(CPUState, sr[11]) },
3070 { "sr12", offsetof(CPUState, sr[12]) },
3071 { "sr13", offsetof(CPUState, sr[13]) },
3072 { "sr14", offsetof(CPUState, sr[14]) },
3073 { "sr15", offsetof(CPUState, sr[15]) },
3074 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00003075#elif defined(TARGET_SPARC)
3076 { "g0", offsetof(CPUState, gregs[0]) },
3077 { "g1", offsetof(CPUState, gregs[1]) },
3078 { "g2", offsetof(CPUState, gregs[2]) },
3079 { "g3", offsetof(CPUState, gregs[3]) },
3080 { "g4", offsetof(CPUState, gregs[4]) },
3081 { "g5", offsetof(CPUState, gregs[5]) },
3082 { "g6", offsetof(CPUState, gregs[6]) },
3083 { "g7", offsetof(CPUState, gregs[7]) },
3084 { "o0", 0, monitor_get_reg },
3085 { "o1", 1, monitor_get_reg },
3086 { "o2", 2, monitor_get_reg },
3087 { "o3", 3, monitor_get_reg },
3088 { "o4", 4, monitor_get_reg },
3089 { "o5", 5, monitor_get_reg },
3090 { "o6", 6, monitor_get_reg },
3091 { "o7", 7, monitor_get_reg },
3092 { "l0", 8, monitor_get_reg },
3093 { "l1", 9, monitor_get_reg },
3094 { "l2", 10, monitor_get_reg },
3095 { "l3", 11, monitor_get_reg },
3096 { "l4", 12, monitor_get_reg },
3097 { "l5", 13, monitor_get_reg },
3098 { "l6", 14, monitor_get_reg },
3099 { "l7", 15, monitor_get_reg },
3100 { "i0", 16, monitor_get_reg },
3101 { "i1", 17, monitor_get_reg },
3102 { "i2", 18, monitor_get_reg },
3103 { "i3", 19, monitor_get_reg },
3104 { "i4", 20, monitor_get_reg },
3105 { "i5", 21, monitor_get_reg },
3106 { "i6", 22, monitor_get_reg },
3107 { "i7", 23, monitor_get_reg },
3108 { "pc", offsetof(CPUState, pc) },
3109 { "npc", offsetof(CPUState, npc) },
3110 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003111#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003112 { "psr", 0, &monitor_get_psr, },
3113 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003114#endif
bellarde95c8d52004-09-30 22:22:08 +00003115 { "tbr", offsetof(CPUState, tbr) },
3116 { "fsr", offsetof(CPUState, fsr) },
3117 { "f0", offsetof(CPUState, fpr[0]) },
3118 { "f1", offsetof(CPUState, fpr[1]) },
3119 { "f2", offsetof(CPUState, fpr[2]) },
3120 { "f3", offsetof(CPUState, fpr[3]) },
3121 { "f4", offsetof(CPUState, fpr[4]) },
3122 { "f5", offsetof(CPUState, fpr[5]) },
3123 { "f6", offsetof(CPUState, fpr[6]) },
3124 { "f7", offsetof(CPUState, fpr[7]) },
3125 { "f8", offsetof(CPUState, fpr[8]) },
3126 { "f9", offsetof(CPUState, fpr[9]) },
3127 { "f10", offsetof(CPUState, fpr[10]) },
3128 { "f11", offsetof(CPUState, fpr[11]) },
3129 { "f12", offsetof(CPUState, fpr[12]) },
3130 { "f13", offsetof(CPUState, fpr[13]) },
3131 { "f14", offsetof(CPUState, fpr[14]) },
3132 { "f15", offsetof(CPUState, fpr[15]) },
3133 { "f16", offsetof(CPUState, fpr[16]) },
3134 { "f17", offsetof(CPUState, fpr[17]) },
3135 { "f18", offsetof(CPUState, fpr[18]) },
3136 { "f19", offsetof(CPUState, fpr[19]) },
3137 { "f20", offsetof(CPUState, fpr[20]) },
3138 { "f21", offsetof(CPUState, fpr[21]) },
3139 { "f22", offsetof(CPUState, fpr[22]) },
3140 { "f23", offsetof(CPUState, fpr[23]) },
3141 { "f24", offsetof(CPUState, fpr[24]) },
3142 { "f25", offsetof(CPUState, fpr[25]) },
3143 { "f26", offsetof(CPUState, fpr[26]) },
3144 { "f27", offsetof(CPUState, fpr[27]) },
3145 { "f28", offsetof(CPUState, fpr[28]) },
3146 { "f29", offsetof(CPUState, fpr[29]) },
3147 { "f30", offsetof(CPUState, fpr[30]) },
3148 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003149#ifdef TARGET_SPARC64
3150 { "f32", offsetof(CPUState, fpr[32]) },
3151 { "f34", offsetof(CPUState, fpr[34]) },
3152 { "f36", offsetof(CPUState, fpr[36]) },
3153 { "f38", offsetof(CPUState, fpr[38]) },
3154 { "f40", offsetof(CPUState, fpr[40]) },
3155 { "f42", offsetof(CPUState, fpr[42]) },
3156 { "f44", offsetof(CPUState, fpr[44]) },
3157 { "f46", offsetof(CPUState, fpr[46]) },
3158 { "f48", offsetof(CPUState, fpr[48]) },
3159 { "f50", offsetof(CPUState, fpr[50]) },
3160 { "f52", offsetof(CPUState, fpr[52]) },
3161 { "f54", offsetof(CPUState, fpr[54]) },
3162 { "f56", offsetof(CPUState, fpr[56]) },
3163 { "f58", offsetof(CPUState, fpr[58]) },
3164 { "f60", offsetof(CPUState, fpr[60]) },
3165 { "f62", offsetof(CPUState, fpr[62]) },
3166 { "asi", offsetof(CPUState, asi) },
3167 { "pstate", offsetof(CPUState, pstate) },
3168 { "cansave", offsetof(CPUState, cansave) },
3169 { "canrestore", offsetof(CPUState, canrestore) },
3170 { "otherwin", offsetof(CPUState, otherwin) },
3171 { "wstate", offsetof(CPUState, wstate) },
3172 { "cleanwin", offsetof(CPUState, cleanwin) },
3173 { "fprs", offsetof(CPUState, fprs) },
3174#endif
bellard9307c4c2004-04-04 12:57:25 +00003175#endif
3176 { NULL },
3177};
3178
aliguori376253e2009-03-05 23:01:23 +00003179static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003180{
aliguori376253e2009-03-05 23:01:23 +00003181 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003182 longjmp(expr_env, 1);
3183}
3184
Markus Armbruster09b94182010-01-20 13:07:30 +01003185/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003186static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003187{
blueswir18662d652008-10-02 18:32:44 +00003188 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003189 void *ptr;
3190
bellard9307c4c2004-04-04 12:57:25 +00003191 for(md = monitor_defs; md->name != NULL; md++) {
3192 if (compare_cmd(name, md->name)) {
3193 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003194 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003195 } else {
bellard6a00d602005-11-21 23:25:50 +00003196 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003197 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003198 switch(md->type) {
3199 case MD_I32:
3200 *pval = *(int32_t *)ptr;
3201 break;
3202 case MD_TLONG:
3203 *pval = *(target_long *)ptr;
3204 break;
3205 default:
3206 *pval = 0;
3207 break;
3208 }
bellard9307c4c2004-04-04 12:57:25 +00003209 }
3210 return 0;
3211 }
3212 }
3213 return -1;
3214}
3215
3216static void next(void)
3217{
Blue Swirl660f11b2009-07-31 21:16:51 +00003218 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003219 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003220 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003221 pch++;
3222 }
3223}
3224
aliguori376253e2009-03-05 23:01:23 +00003225static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003226
aliguori376253e2009-03-05 23:01:23 +00003227static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003228{
blueswir1c2efc952007-09-25 17:28:42 +00003229 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003230 char *p;
bellard6a00d602005-11-21 23:25:50 +00003231 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003232
3233 switch(*pch) {
3234 case '+':
3235 next();
aliguori376253e2009-03-05 23:01:23 +00003236 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003237 break;
3238 case '-':
3239 next();
aliguori376253e2009-03-05 23:01:23 +00003240 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003241 break;
3242 case '~':
3243 next();
aliguori376253e2009-03-05 23:01:23 +00003244 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003245 break;
3246 case '(':
3247 next();
aliguori376253e2009-03-05 23:01:23 +00003248 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003249 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003250 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003251 }
3252 next();
3253 break;
bellard81d09122004-07-14 17:21:37 +00003254 case '\'':
3255 pch++;
3256 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003257 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003258 n = *pch;
3259 pch++;
3260 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003261 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003262 next();
3263 break;
bellard9307c4c2004-04-04 12:57:25 +00003264 case '$':
3265 {
3266 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003267 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003268
bellard9307c4c2004-04-04 12:57:25 +00003269 pch++;
3270 q = buf;
3271 while ((*pch >= 'a' && *pch <= 'z') ||
3272 (*pch >= 'A' && *pch <= 'Z') ||
3273 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003274 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003275 if ((q - buf) < sizeof(buf) - 1)
3276 *q++ = *pch;
3277 pch++;
3278 }
blueswir1cd390082008-11-16 13:53:32 +00003279 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003280 pch++;
3281 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003282 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003283 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003284 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003285 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003286 }
3287 break;
3288 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003289 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003290 n = 0;
3291 break;
3292 default:
blueswir17743e582007-09-24 18:39:04 +00003293#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003294 n = strtoull(pch, &p, 0);
3295#else
bellard9307c4c2004-04-04 12:57:25 +00003296 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003297#endif
bellard9307c4c2004-04-04 12:57:25 +00003298 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003299 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003300 }
3301 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003302 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003303 pch++;
3304 break;
3305 }
3306 return n;
3307}
3308
3309
aliguori376253e2009-03-05 23:01:23 +00003310static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003311{
blueswir1c2efc952007-09-25 17:28:42 +00003312 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003313 int op;
ths3b46e622007-09-17 08:09:54 +00003314
aliguori376253e2009-03-05 23:01:23 +00003315 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003316 for(;;) {
3317 op = *pch;
3318 if (op != '*' && op != '/' && op != '%')
3319 break;
3320 next();
aliguori376253e2009-03-05 23:01:23 +00003321 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003322 switch(op) {
3323 default:
3324 case '*':
3325 val *= val2;
3326 break;
3327 case '/':
3328 case '%':
ths5fafdf22007-09-16 21:08:06 +00003329 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003330 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003331 if (op == '/')
3332 val /= val2;
3333 else
3334 val %= val2;
3335 break;
3336 }
3337 }
3338 return val;
3339}
3340
aliguori376253e2009-03-05 23:01:23 +00003341static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003342{
blueswir1c2efc952007-09-25 17:28:42 +00003343 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003344 int op;
bellard9307c4c2004-04-04 12:57:25 +00003345
aliguori376253e2009-03-05 23:01:23 +00003346 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003347 for(;;) {
3348 op = *pch;
3349 if (op != '&' && op != '|' && op != '^')
3350 break;
3351 next();
aliguori376253e2009-03-05 23:01:23 +00003352 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003353 switch(op) {
3354 default:
3355 case '&':
3356 val &= val2;
3357 break;
3358 case '|':
3359 val |= val2;
3360 break;
3361 case '^':
3362 val ^= val2;
3363 break;
3364 }
3365 }
3366 return val;
3367}
3368
aliguori376253e2009-03-05 23:01:23 +00003369static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003370{
blueswir1c2efc952007-09-25 17:28:42 +00003371 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003372 int op;
bellard9307c4c2004-04-04 12:57:25 +00003373
aliguori376253e2009-03-05 23:01:23 +00003374 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003375 for(;;) {
3376 op = *pch;
3377 if (op != '+' && op != '-')
3378 break;
3379 next();
aliguori376253e2009-03-05 23:01:23 +00003380 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003381 if (op == '+')
3382 val += val2;
3383 else
3384 val -= val2;
3385 }
3386 return val;
3387}
3388
aliguori376253e2009-03-05 23:01:23 +00003389static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003390{
3391 pch = *pp;
3392 if (setjmp(expr_env)) {
3393 *pp = pch;
3394 return -1;
3395 }
blueswir1cd390082008-11-16 13:53:32 +00003396 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003397 pch++;
aliguori376253e2009-03-05 23:01:23 +00003398 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003399 *pp = pch;
3400 return 0;
3401}
3402
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003403static int get_double(Monitor *mon, double *pval, const char **pp)
3404{
3405 const char *p = *pp;
3406 char *tailp;
3407 double d;
3408
3409 d = strtod(p, &tailp);
3410 if (tailp == p) {
3411 monitor_printf(mon, "Number expected\n");
3412 return -1;
3413 }
3414 if (d != d || d - d != 0) {
3415 /* NaN or infinity */
3416 monitor_printf(mon, "Bad number\n");
3417 return -1;
3418 }
3419 *pval = d;
3420 *pp = tailp;
3421 return 0;
3422}
3423
bellard9307c4c2004-04-04 12:57:25 +00003424static int get_str(char *buf, int buf_size, const char **pp)
3425{
3426 const char *p;
3427 char *q;
3428 int c;
3429
bellard81d09122004-07-14 17:21:37 +00003430 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003431 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003432 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003433 p++;
3434 if (*p == '\0') {
3435 fail:
bellard81d09122004-07-14 17:21:37 +00003436 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003437 *pp = p;
3438 return -1;
3439 }
bellard9307c4c2004-04-04 12:57:25 +00003440 if (*p == '\"') {
3441 p++;
3442 while (*p != '\0' && *p != '\"') {
3443 if (*p == '\\') {
3444 p++;
3445 c = *p++;
3446 switch(c) {
3447 case 'n':
3448 c = '\n';
3449 break;
3450 case 'r':
3451 c = '\r';
3452 break;
3453 case '\\':
3454 case '\'':
3455 case '\"':
3456 break;
3457 default:
3458 qemu_printf("unsupported escape code: '\\%c'\n", c);
3459 goto fail;
3460 }
3461 if ((q - buf) < buf_size - 1) {
3462 *q++ = c;
3463 }
3464 } else {
3465 if ((q - buf) < buf_size - 1) {
3466 *q++ = *p;
3467 }
3468 p++;
3469 }
3470 }
3471 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003472 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003473 goto fail;
3474 }
3475 p++;
3476 } else {
blueswir1cd390082008-11-16 13:53:32 +00003477 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003478 if ((q - buf) < buf_size - 1) {
3479 *q++ = *p;
3480 }
3481 p++;
3482 }
bellard9307c4c2004-04-04 12:57:25 +00003483 }
bellard81d09122004-07-14 17:21:37 +00003484 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003485 *pp = p;
3486 return 0;
3487}
3488
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003489/*
3490 * Store the command-name in cmdname, and return a pointer to
3491 * the remaining of the command string.
3492 */
3493static const char *get_command_name(const char *cmdline,
3494 char *cmdname, size_t nlen)
3495{
3496 size_t len;
3497 const char *p, *pstart;
3498
3499 p = cmdline;
3500 while (qemu_isspace(*p))
3501 p++;
3502 if (*p == '\0')
3503 return NULL;
3504 pstart = p;
3505 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3506 p++;
3507 len = p - pstart;
3508 if (len > nlen - 1)
3509 len = nlen - 1;
3510 memcpy(cmdname, pstart, len);
3511 cmdname[len] = '\0';
3512 return p;
3513}
3514
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003515/**
3516 * Read key of 'type' into 'key' and return the current
3517 * 'type' pointer.
3518 */
3519static char *key_get_info(const char *type, char **key)
3520{
3521 size_t len;
3522 char *p, *str;
3523
3524 if (*type == ',')
3525 type++;
3526
3527 p = strchr(type, ':');
3528 if (!p) {
3529 *key = NULL;
3530 return NULL;
3531 }
3532 len = p - type;
3533
3534 str = qemu_malloc(len + 1);
3535 memcpy(str, type, len);
3536 str[len] = '\0';
3537
3538 *key = str;
3539 return ++p;
3540}
3541
bellard9307c4c2004-04-04 12:57:25 +00003542static int default_fmt_format = 'x';
3543static int default_fmt_size = 4;
3544
3545#define MAX_ARGS 16
3546
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003547static int is_valid_option(const char *c, const char *typestr)
3548{
3549 char option[3];
3550
3551 option[0] = '-';
3552 option[1] = *c;
3553 option[2] = '\0';
3554
3555 typestr = strstr(typestr, option);
3556 return (typestr != NULL);
3557}
3558
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003559static const mon_cmd_t *monitor_find_command(const char *cmdname)
3560{
3561 const mon_cmd_t *cmd;
3562
3563 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3564 if (compare_cmd(cmdname, cmd->name)) {
3565 return cmd;
3566 }
3567 }
3568
3569 return NULL;
3570}
3571
Anthony Liguoric227f092009-10-01 16:12:16 -05003572static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003573 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003574 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003575{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003576 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003577 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003578 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003579 char cmdname[256];
3580 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003581 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003582
3583#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003584 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003585#endif
ths3b46e622007-09-17 08:09:54 +00003586
bellard9307c4c2004-04-04 12:57:25 +00003587 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003588 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3589 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003590 return NULL;
ths3b46e622007-09-17 08:09:54 +00003591
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003592 cmd = monitor_find_command(cmdname);
3593 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003594 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003595 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003596 }
bellard9307c4c2004-04-04 12:57:25 +00003597
bellard9307c4c2004-04-04 12:57:25 +00003598 /* parse the parameters */
3599 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003600 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003601 typestr = key_get_info(typestr, &key);
3602 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003603 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003604 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003605 typestr++;
3606 switch(c) {
3607 case 'F':
bellard81d09122004-07-14 17:21:37 +00003608 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003609 case 's':
3610 {
3611 int ret;
ths3b46e622007-09-17 08:09:54 +00003612
blueswir1cd390082008-11-16 13:53:32 +00003613 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003614 p++;
3615 if (*typestr == '?') {
3616 typestr++;
3617 if (*p == '\0') {
3618 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003619 break;
bellard9307c4c2004-04-04 12:57:25 +00003620 }
3621 }
3622 ret = get_str(buf, sizeof(buf), &p);
3623 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003624 switch(c) {
3625 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003626 monitor_printf(mon, "%s: filename expected\n",
3627 cmdname);
bellard81d09122004-07-14 17:21:37 +00003628 break;
3629 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003630 monitor_printf(mon, "%s: block device name expected\n",
3631 cmdname);
bellard81d09122004-07-14 17:21:37 +00003632 break;
3633 default:
aliguori376253e2009-03-05 23:01:23 +00003634 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003635 break;
3636 }
bellard9307c4c2004-04-04 12:57:25 +00003637 goto fail;
3638 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003639 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003640 }
3641 break;
3642 case '/':
3643 {
3644 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003645
blueswir1cd390082008-11-16 13:53:32 +00003646 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003647 p++;
3648 if (*p == '/') {
3649 /* format found */
3650 p++;
3651 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003652 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003653 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003654 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003655 count = count * 10 + (*p - '0');
3656 p++;
3657 }
3658 }
3659 size = -1;
3660 format = -1;
3661 for(;;) {
3662 switch(*p) {
3663 case 'o':
3664 case 'd':
3665 case 'u':
3666 case 'x':
3667 case 'i':
3668 case 'c':
3669 format = *p++;
3670 break;
3671 case 'b':
3672 size = 1;
3673 p++;
3674 break;
3675 case 'h':
3676 size = 2;
3677 p++;
3678 break;
3679 case 'w':
3680 size = 4;
3681 p++;
3682 break;
3683 case 'g':
3684 case 'L':
3685 size = 8;
3686 p++;
3687 break;
3688 default:
3689 goto next;
3690 }
3691 }
3692 next:
blueswir1cd390082008-11-16 13:53:32 +00003693 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003694 monitor_printf(mon, "invalid char in format: '%c'\n",
3695 *p);
bellard9307c4c2004-04-04 12:57:25 +00003696 goto fail;
3697 }
bellard9307c4c2004-04-04 12:57:25 +00003698 if (format < 0)
3699 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003700 if (format != 'i') {
3701 /* for 'i', not specifying a size gives -1 as size */
3702 if (size < 0)
3703 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003704 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003705 }
bellard9307c4c2004-04-04 12:57:25 +00003706 default_fmt_format = format;
3707 } else {
3708 count = 1;
3709 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003710 if (format != 'i') {
3711 size = default_fmt_size;
3712 } else {
3713 size = -1;
3714 }
bellard9307c4c2004-04-04 12:57:25 +00003715 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003716 qdict_put(qdict, "count", qint_from_int(count));
3717 qdict_put(qdict, "format", qint_from_int(format));
3718 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003719 }
3720 break;
3721 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003722 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003723 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003724 {
blueswir1c2efc952007-09-25 17:28:42 +00003725 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003726
blueswir1cd390082008-11-16 13:53:32 +00003727 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003728 p++;
bellard34405572004-06-08 00:55:58 +00003729 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003730 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003731 if (*p == '\0') {
3732 typestr++;
3733 break;
3734 }
bellard34405572004-06-08 00:55:58 +00003735 } else {
3736 if (*p == '.') {
3737 p++;
blueswir1cd390082008-11-16 13:53:32 +00003738 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003739 p++;
bellard34405572004-06-08 00:55:58 +00003740 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003741 typestr++;
3742 break;
bellard34405572004-06-08 00:55:58 +00003743 }
3744 }
bellard13224a82006-07-14 22:03:35 +00003745 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003746 }
aliguori376253e2009-03-05 23:01:23 +00003747 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003748 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003749 /* Check if 'i' is greater than 32-bit */
3750 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3751 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3752 monitor_printf(mon, "integer is for 32-bit values\n");
3753 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003754 } else if (c == 'M') {
3755 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003756 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003757 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003758 }
3759 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003760 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003761 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003762 {
3763 double val;
3764
3765 while (qemu_isspace(*p))
3766 p++;
3767 if (*typestr == '?') {
3768 typestr++;
3769 if (*p == '\0') {
3770 break;
3771 }
3772 }
3773 if (get_double(mon, &val, &p) < 0) {
3774 goto fail;
3775 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003776 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003777 switch (*p) {
3778 case 'K': case 'k':
3779 val *= 1 << 10; p++; break;
3780 case 'M': case 'm':
3781 val *= 1 << 20; p++; break;
3782 case 'G': case 'g':
3783 val *= 1 << 30; p++; break;
3784 }
3785 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003786 if (c == 'T' && p[0] && p[1] == 's') {
3787 switch (*p) {
3788 case 'm':
3789 val /= 1e3; p += 2; break;
3790 case 'u':
3791 val /= 1e6; p += 2; break;
3792 case 'n':
3793 val /= 1e9; p += 2; break;
3794 }
3795 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003796 if (*p && !qemu_isspace(*p)) {
3797 monitor_printf(mon, "Unknown unit suffix\n");
3798 goto fail;
3799 }
3800 qdict_put(qdict, key, qfloat_from_double(val));
3801 }
3802 break;
bellard9307c4c2004-04-04 12:57:25 +00003803 case '-':
3804 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003805 const char *tmp = p;
3806 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003807 /* option */
ths3b46e622007-09-17 08:09:54 +00003808
bellard9307c4c2004-04-04 12:57:25 +00003809 c = *typestr++;
3810 if (c == '\0')
3811 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003812 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003813 p++;
3814 has_option = 0;
3815 if (*p == '-') {
3816 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003817 if(c != *p) {
3818 if(!is_valid_option(p, typestr)) {
3819
3820 monitor_printf(mon, "%s: unsupported option -%c\n",
3821 cmdname, *p);
3822 goto fail;
3823 } else {
3824 skip_key = 1;
3825 }
bellard9307c4c2004-04-04 12:57:25 +00003826 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003827 if(skip_key) {
3828 p = tmp;
3829 } else {
3830 p++;
3831 has_option = 1;
3832 }
bellard9307c4c2004-04-04 12:57:25 +00003833 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003834 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003835 }
3836 break;
3837 default:
3838 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003839 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003840 goto fail;
3841 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003842 qemu_free(key);
3843 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003844 }
3845 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003846 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003847 p++;
3848 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003849 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3850 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003851 goto fail;
3852 }
3853
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003854 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003855
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003856fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003857 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003858 return NULL;
3859}
3860
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003861void monitor_set_error(Monitor *mon, QError *qerror)
3862{
3863 /* report only the first error */
3864 if (!mon->error) {
3865 mon->error = qerror;
3866 } else {
3867 MON_DEBUG("Additional error report at %s:%d\n",
3868 qerror->file, qerror->linenr);
3869 QDECREF(qerror);
3870 }
3871}
3872
Luiz Capitulino8204a912009-11-18 23:05:31 -02003873static void monitor_print_error(Monitor *mon)
3874{
3875 qerror_print(mon->error);
3876 QDECREF(mon->error);
3877 mon->error = NULL;
3878}
3879
Adam Litke940cc302010-01-25 12:18:44 -06003880static int is_async_return(const QObject *data)
3881{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003882 if (data && qobject_type(data) == QTYPE_QDICT) {
3883 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3884 }
3885
3886 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003887}
3888
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003889static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3890{
3891 if (ret && !monitor_has_error(mon)) {
3892 /*
3893 * If it returns failure, it must have passed on error.
3894 *
3895 * Action: Report an internal error to the client if in QMP.
3896 */
3897 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003898 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003899 }
3900 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3901 cmd->name);
3902 }
3903
3904#ifdef CONFIG_DEBUG_MONITOR
3905 if (!ret && monitor_has_error(mon)) {
3906 /*
3907 * If it returns success, it must not have passed an error.
3908 *
3909 * Action: Report the passed error to the client.
3910 */
3911 MON_DEBUG("command '%s' returned success but passed an error\n",
3912 cmd->name);
3913 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003914
3915 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3916 /*
3917 * Handlers should not call Monitor print functions.
3918 *
3919 * Action: Ignore them in QMP.
3920 *
3921 * (XXX: we don't check any 'info' or 'query' command here
3922 * because the user print function _is_ called by do_info(), hence
3923 * we will trigger this check. This problem will go away when we
3924 * make 'query' commands real and kill do_info())
3925 */
3926 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3927 cmd->name, mon_print_count_get(mon));
3928 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003929#endif
3930}
3931
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003932static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3933 const QDict *params)
3934{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003935 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003936 QObject *data = NULL;
3937
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003938 mon_print_count_init(mon);
3939
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003940 ret = cmd->mhandler.cmd_new(mon, params, &data);
3941 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003942
Adam Litke940cc302010-01-25 12:18:44 -06003943 if (is_async_return(data)) {
3944 /*
3945 * Asynchronous commands have no initial return data but they can
3946 * generate errors. Data is returned via the async completion handler.
3947 */
3948 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3949 monitor_protocol_emitter(mon, NULL);
3950 }
3951 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003952 /* Monitor Protocol */
3953 monitor_protocol_emitter(mon, data);
3954 } else {
3955 /* User Protocol */
3956 if (data)
3957 cmd->user_print(mon, data);
3958 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003959
3960 qobject_decref(data);
3961}
3962
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003963static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003964{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003965 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003966 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003967
3968 qdict = qdict_new();
3969
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003970 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003971 if (!cmd)
3972 goto out;
3973
Adam Litke940cc302010-01-25 12:18:44 -06003974 if (monitor_handler_is_async(cmd)) {
3975 user_async_cmd_handler(mon, cmd, qdict);
3976 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003977 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003978 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003979 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003980 }
3981
Luiz Capitulino8204a912009-11-18 23:05:31 -02003982 if (monitor_has_error(mon))
3983 monitor_print_error(mon);
3984
Luiz Capitulino13917be2009-10-07 13:41:54 -03003985out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003986 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003987}
3988
bellard81d09122004-07-14 17:21:37 +00003989static void cmd_completion(const char *name, const char *list)
3990{
3991 const char *p, *pstart;
3992 char cmd[128];
3993 int len;
3994
3995 p = list;
3996 for(;;) {
3997 pstart = p;
3998 p = strchr(p, '|');
3999 if (!p)
4000 p = pstart + strlen(pstart);
4001 len = p - pstart;
4002 if (len > sizeof(cmd) - 2)
4003 len = sizeof(cmd) - 2;
4004 memcpy(cmd, pstart, len);
4005 cmd[len] = '\0';
4006 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004007 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004008 }
4009 if (*p == '\0')
4010 break;
4011 p++;
4012 }
4013}
4014
4015static void file_completion(const char *input)
4016{
4017 DIR *ffs;
4018 struct dirent *d;
4019 char path[1024];
4020 char file[1024], file_prefix[1024];
4021 int input_path_len;
4022 const char *p;
4023
ths5fafdf22007-09-16 21:08:06 +00004024 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004025 if (!p) {
4026 input_path_len = 0;
4027 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004028 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004029 } else {
4030 input_path_len = p - input + 1;
4031 memcpy(path, input, input_path_len);
4032 if (input_path_len > sizeof(path) - 1)
4033 input_path_len = sizeof(path) - 1;
4034 path[input_path_len] = '\0';
4035 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4036 }
4037#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004038 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4039 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004040#endif
4041 ffs = opendir(path);
4042 if (!ffs)
4043 return;
4044 for(;;) {
4045 struct stat sb;
4046 d = readdir(ffs);
4047 if (!d)
4048 break;
4049 if (strstart(d->d_name, file_prefix, NULL)) {
4050 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004051 if (input_path_len < sizeof(file))
4052 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4053 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004054 /* stat the file to find out if it's a directory.
4055 * In that case add a slash to speed up typing long paths
4056 */
4057 stat(file, &sb);
4058 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004059 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004060 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004061 }
4062 }
4063 closedir(ffs);
4064}
4065
aliguori51de9762009-03-05 23:00:43 +00004066static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004067{
aliguori51de9762009-03-05 23:00:43 +00004068 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004069 const char *input = opaque;
4070
4071 if (input[0] == '\0' ||
4072 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004073 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004074 }
4075}
4076
4077/* NOTE: this parser is an approximate form of the real command parser */
4078static void parse_cmdline(const char *cmdline,
4079 int *pnb_args, char **args)
4080{
4081 const char *p;
4082 int nb_args, ret;
4083 char buf[1024];
4084
4085 p = cmdline;
4086 nb_args = 0;
4087 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004088 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004089 p++;
4090 if (*p == '\0')
4091 break;
4092 if (nb_args >= MAX_ARGS)
4093 break;
4094 ret = get_str(buf, sizeof(buf), &p);
4095 args[nb_args] = qemu_strdup(buf);
4096 nb_args++;
4097 if (ret < 0)
4098 break;
4099 }
4100 *pnb_args = nb_args;
4101}
4102
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004103static const char *next_arg_type(const char *typestr)
4104{
4105 const char *p = strchr(typestr, ':');
4106 return (p != NULL ? ++p : typestr);
4107}
4108
aliguori4c36ba32009-03-05 23:01:37 +00004109static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004110{
4111 const char *cmdname;
4112 char *args[MAX_ARGS];
4113 int nb_args, i, len;
4114 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004115 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004116 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004117
4118 parse_cmdline(cmdline, &nb_args, args);
4119#ifdef DEBUG_COMPLETION
4120 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004121 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004122 }
4123#endif
4124
4125 /* if the line ends with a space, it means we want to complete the
4126 next arg */
4127 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004128 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00004129 if (nb_args >= MAX_ARGS)
4130 return;
4131 args[nb_args++] = qemu_strdup("");
4132 }
4133 if (nb_args <= 1) {
4134 /* command completion */
4135 if (nb_args == 0)
4136 cmdname = "";
4137 else
4138 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004139 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004140 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004141 cmd_completion(cmdname, cmd->name);
4142 }
4143 } else {
4144 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00004145 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004146 if (compare_cmd(args[0], cmd->name))
4147 goto found;
4148 }
4149 return;
4150 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004151 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004152 for(i = 0; i < nb_args - 2; i++) {
4153 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004154 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004155 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004156 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004157 }
4158 }
4159 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004160 if (*ptype == '-' && ptype[1] != '\0') {
4161 ptype += 2;
4162 }
bellard81d09122004-07-14 17:21:37 +00004163 switch(*ptype) {
4164 case 'F':
4165 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004166 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004167 file_completion(str);
4168 break;
4169 case 'B':
4170 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004171 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004172 bdrv_iterate(block_completion_it, (void *)str);
4173 break;
bellard7fe48482004-10-09 18:08:01 +00004174 case 's':
4175 /* XXX: more generic ? */
4176 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004177 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004178 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4179 cmd_completion(str, cmd->name);
4180 }
bellard64866c32006-05-07 18:03:31 +00004181 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004182 char *sep = strrchr(str, '-');
4183 if (sep)
4184 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004185 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004186 for(key = key_defs; key->name != NULL; key++) {
4187 cmd_completion(str, key->name);
4188 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004189 } else if (!strcmp(cmd->name, "help|?")) {
4190 readline_set_completion_index(cur_mon->rs, strlen(str));
4191 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4192 cmd_completion(str, cmd->name);
4193 }
bellard7fe48482004-10-09 18:08:01 +00004194 }
4195 break;
bellard81d09122004-07-14 17:21:37 +00004196 default:
4197 break;
4198 }
4199 }
4200 for(i = 0; i < nb_args; i++)
4201 qemu_free(args[i]);
4202}
4203
aliguori731b0362009-03-05 23:01:42 +00004204static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004205{
aliguori731b0362009-03-05 23:01:42 +00004206 Monitor *mon = opaque;
4207
Jan Kiszkac62313b2009-12-04 14:05:29 +01004208 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004209}
4210
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004211typedef struct CmdArgs {
4212 QString *name;
4213 int type;
4214 int flag;
4215 int optional;
4216} CmdArgs;
4217
4218static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4219{
4220 if (!cmd_args->optional) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004221 qerror_report(QERR_MISSING_PARAMETER, name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004222 return -1;
4223 }
4224
4225 if (cmd_args->type == '-') {
4226 /* handlers expect a value, they need to be changed */
4227 qdict_put(args, name, qint_from_int(0));
4228 }
4229
4230 return 0;
4231}
4232
4233static int check_arg(const CmdArgs *cmd_args, QDict *args)
4234{
4235 QObject *value;
4236 const char *name;
4237
4238 name = qstring_get_str(cmd_args->name);
4239
4240 if (!args) {
4241 return check_opt(cmd_args, name, args);
4242 }
4243
4244 value = qdict_get(args, name);
4245 if (!value) {
4246 return check_opt(cmd_args, name, args);
4247 }
4248
4249 switch (cmd_args->type) {
4250 case 'F':
4251 case 'B':
4252 case 's':
4253 if (qobject_type(value) != QTYPE_QSTRING) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004254 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "string");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004255 return -1;
4256 }
4257 break;
4258 case '/': {
4259 int i;
4260 const char *keys[] = { "count", "format", "size", NULL };
4261
4262 for (i = 0; keys[i]; i++) {
4263 QObject *obj = qdict_get(args, keys[i]);
4264 if (!obj) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004265 qerror_report(QERR_MISSING_PARAMETER, name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004266 return -1;
4267 }
4268 if (qobject_type(obj) != QTYPE_QINT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004269 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004270 return -1;
4271 }
4272 }
4273 break;
4274 }
4275 case 'i':
4276 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004277 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004278 if (qobject_type(value) != QTYPE_QINT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004279 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "int");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004280 return -1;
4281 }
4282 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004283 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004284 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004285 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004286 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "number");
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004287 return -1;
4288 }
4289 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004290 case '-':
4291 if (qobject_type(value) != QTYPE_QINT &&
4292 qobject_type(value) != QTYPE_QBOOL) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004293 qerror_report(QERR_INVALID_PARAMETER_TYPE, name, "bool");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004294 return -1;
4295 }
4296 if (qobject_type(value) == QTYPE_QBOOL) {
4297 /* handlers expect a QInt, they need to be changed */
4298 qdict_put(args, name,
4299 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4300 }
4301 break;
4302 default:
4303 /* impossible */
4304 abort();
4305 }
4306
4307 return 0;
4308}
4309
4310static void cmd_args_init(CmdArgs *cmd_args)
4311{
4312 cmd_args->name = qstring_new();
4313 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4314}
4315
4316/*
4317 * This is not trivial, we have to parse Monitor command's argument
4318 * type syntax to be able to check the arguments provided by clients.
4319 *
4320 * In the near future we will be using an array for that and will be
4321 * able to drop all this parsing...
4322 */
4323static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4324{
4325 int err;
4326 const char *p;
4327 CmdArgs cmd_args;
4328
Blue Swirldd5121b2009-12-04 20:52:02 +00004329 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004330 return (qdict_size(args) == 0 ? 0 : -1);
4331 }
4332
4333 err = 0;
4334 cmd_args_init(&cmd_args);
4335
4336 for (p = cmd->args_type;; p++) {
4337 if (*p == ':') {
4338 cmd_args.type = *++p;
4339 p++;
4340 if (cmd_args.type == '-') {
4341 cmd_args.flag = *p++;
4342 cmd_args.optional = 1;
4343 } else if (*p == '?') {
4344 cmd_args.optional = 1;
4345 p++;
4346 }
4347
4348 assert(*p == ',' || *p == '\0');
4349 err = check_arg(&cmd_args, args);
4350
4351 QDECREF(cmd_args.name);
4352 cmd_args_init(&cmd_args);
4353
4354 if (err < 0) {
4355 break;
4356 }
4357 } else {
4358 qstring_append_chr(cmd_args.name, *p);
4359 }
4360
4361 if (*p == '\0') {
4362 break;
4363 }
4364 }
4365
4366 QDECREF(cmd_args.name);
4367 return err;
4368}
4369
Luiz Capitulino09069b12010-02-04 18:10:06 -02004370static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4371{
4372 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4373 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4374}
4375
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004376static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4377{
4378 int err;
4379 QObject *obj;
4380 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004381 const mon_cmd_t *cmd;
4382 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004383 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004384
4385 args = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004386
4387 obj = json_parser_parse(tokens, NULL);
4388 if (!obj) {
4389 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004390 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004391 goto err_out;
4392 } else if (qobject_type(obj) != QTYPE_QDICT) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004393 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004394 qobject_decref(obj);
4395 goto err_out;
4396 }
4397
4398 input = qobject_to_qdict(obj);
4399
4400 mon->mc->id = qdict_get(input, "id");
4401 qobject_incref(mon->mc->id);
4402
4403 obj = qdict_get(input, "execute");
4404 if (!obj) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004405 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004406 goto err_input;
4407 } else if (qobject_type(obj) != QTYPE_QSTRING) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004408 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "string");
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004409 goto err_input;
4410 }
4411
4412 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004413
Luiz Capitulino09069b12010-02-04 18:10:06 -02004414 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004415 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004416 goto err_input;
4417 }
4418
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004419 /*
4420 * XXX: We need this special case until we get info handlers
4421 * converted into 'query-' commands
4422 */
4423 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004424 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004425 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004426 } else if (strstart(cmd_name, "query-", &info_item)) {
4427 cmd = monitor_find_command("info");
4428 qdict_put_obj(input, "arguments",
4429 qobject_from_jsonf("{ 'item': %s }", info_item));
4430 } else {
4431 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004432 if (!cmd || !monitor_handler_ported(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004433 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004434 goto err_input;
4435 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004436 }
4437
4438 obj = qdict_get(input, "arguments");
4439 if (!obj) {
4440 args = qdict_new();
4441 } else {
4442 args = qobject_to_qdict(obj);
4443 QINCREF(args);
4444 }
4445
4446 QDECREF(input);
4447
4448 err = monitor_check_qmp_args(cmd, args);
4449 if (err < 0) {
4450 goto err_out;
4451 }
4452
Adam Litke940cc302010-01-25 12:18:44 -06004453 if (monitor_handler_is_async(cmd)) {
4454 qmp_async_cmd_handler(mon, cmd, args);
4455 } else {
4456 monitor_call_handler(mon, cmd, args);
4457 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004458 goto out;
4459
4460err_input:
4461 QDECREF(input);
4462err_out:
4463 monitor_protocol_emitter(mon, NULL);
4464out:
4465 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004466}
4467
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004468/**
4469 * monitor_control_read(): Read and handle QMP input
4470 */
4471static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4472{
4473 Monitor *old_mon = cur_mon;
4474
4475 cur_mon = opaque;
4476
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004477 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004478
4479 cur_mon = old_mon;
4480}
4481
aliguori731b0362009-03-05 23:01:42 +00004482static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004483{
aliguori731b0362009-03-05 23:01:42 +00004484 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004485 int i;
aliguori376253e2009-03-05 23:01:23 +00004486
aliguori731b0362009-03-05 23:01:42 +00004487 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004488
aliguoricde76ee2009-03-05 23:01:51 +00004489 if (cur_mon->rs) {
4490 for (i = 0; i < size; i++)
4491 readline_handle_byte(cur_mon->rs, buf[i]);
4492 } else {
4493 if (size == 0 || buf[size - 1] != 0)
4494 monitor_printf(cur_mon, "corrupted command\n");
4495 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004496 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004497 }
aliguori731b0362009-03-05 23:01:42 +00004498
4499 cur_mon = old_mon;
4500}
aliguorid8f44602008-10-06 13:52:44 +00004501
aliguori376253e2009-03-05 23:01:23 +00004502static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004503{
aliguori731b0362009-03-05 23:01:42 +00004504 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004505 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004506 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004507}
4508
aliguoricde76ee2009-03-05 23:01:51 +00004509int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004510{
aliguoricde76ee2009-03-05 23:01:51 +00004511 if (!mon->rs)
4512 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004513 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004514 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004515}
4516
aliguori376253e2009-03-05 23:01:23 +00004517void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004518{
aliguoricde76ee2009-03-05 23:01:51 +00004519 if (!mon->rs)
4520 return;
aliguori731b0362009-03-05 23:01:42 +00004521 if (--mon->suspend_cnt == 0)
4522 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004523}
4524
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004525static QObject *get_qmp_greeting(void)
4526{
4527 QObject *ver;
4528
4529 do_info_version(NULL, &ver);
4530 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4531}
4532
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004533/**
4534 * monitor_control_event(): Print QMP gretting
4535 */
4536static void monitor_control_event(void *opaque, int event)
4537{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004538 QObject *data;
4539 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004540
Luiz Capitulino47116d12010-02-08 17:01:30 -02004541 switch (event) {
4542 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004543 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004544 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004545 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004546 monitor_json_emitter(mon, data);
4547 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004548 break;
4549 case CHR_EVENT_CLOSED:
4550 json_message_parser_destroy(&mon->mc->parser);
4551 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004552 }
4553}
4554
aliguori731b0362009-03-05 23:01:42 +00004555static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004556{
aliguori376253e2009-03-05 23:01:23 +00004557 Monitor *mon = opaque;
4558
aliguori2724b182009-03-05 23:01:47 +00004559 switch (event) {
4560 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004561 mon->mux_out = 0;
4562 if (mon->reset_seen) {
4563 readline_restart(mon->rs);
4564 monitor_resume(mon);
4565 monitor_flush(mon);
4566 } else {
4567 mon->suspend_cnt = 0;
4568 }
aliguori2724b182009-03-05 23:01:47 +00004569 break;
ths86e94de2007-01-05 22:01:59 +00004570
aliguori2724b182009-03-05 23:01:47 +00004571 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004572 if (mon->reset_seen) {
4573 if (mon->suspend_cnt == 0) {
4574 monitor_printf(mon, "\n");
4575 }
4576 monitor_flush(mon);
4577 monitor_suspend(mon);
4578 } else {
4579 mon->suspend_cnt++;
4580 }
4581 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004582 break;
4583
Amit Shahb6b8df52009-10-07 18:31:16 +05304584 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004585 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4586 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004587 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004588 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004589 }
4590 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004591 break;
4592 }
ths86e94de2007-01-05 22:01:59 +00004593}
4594
aliguori76655d62009-03-06 20:27:37 +00004595
4596/*
4597 * Local variables:
4598 * c-indent-level: 4
4599 * c-basic-offset: 4
4600 * tab-width: 8
4601 * End:
4602 */
4603
aliguori731b0362009-03-05 23:01:42 +00004604void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004605{
aliguori731b0362009-03-05 23:01:42 +00004606 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004607 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004608
4609 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004610 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004611 is_first_init = 0;
4612 }
aliguori87127162009-03-05 23:01:29 +00004613
4614 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004615
aliguori87127162009-03-05 23:01:29 +00004616 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004617 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004618 if (flags & MONITOR_USE_READLINE) {
4619 mon->rs = readline_init(mon, monitor_find_completion);
4620 monitor_read_command(mon, 0);
4621 }
aliguori87127162009-03-05 23:01:29 +00004622
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004623 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004624 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004625 /* Control mode requires special handlers */
4626 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4627 monitor_control_event, mon);
4628 } else {
4629 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4630 monitor_event, mon);
4631 }
aliguori87127162009-03-05 23:01:29 +00004632
Blue Swirl72cf2d42009-09-12 07:36:22 +00004633 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004634 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4635 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004636}
4637
aliguori376253e2009-03-05 23:01:23 +00004638static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004639{
aliguoribb5fc202009-03-05 23:01:15 +00004640 BlockDriverState *bs = opaque;
4641 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004642
aliguoribb5fc202009-03-05 23:01:15 +00004643 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004644 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004645 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004646 }
aliguori731b0362009-03-05 23:01:42 +00004647 if (mon->password_completion_cb)
4648 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004649
aliguori731b0362009-03-05 23:01:42 +00004650 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004651}
aliguoric0f4ce72009-03-05 23:01:01 +00004652
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004653int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4654 BlockDriverCompletionFunc *completion_cb,
4655 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004656{
aliguoricde76ee2009-03-05 23:01:51 +00004657 int err;
4658
aliguoribb5fc202009-03-05 23:01:15 +00004659 if (!bdrv_key_required(bs)) {
4660 if (completion_cb)
4661 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004662 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004663 }
aliguoric0f4ce72009-03-05 23:01:01 +00004664
Luiz Capitulino94171e12009-12-07 21:37:00 +01004665 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004666 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004667 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004668 }
4669
aliguori376253e2009-03-05 23:01:23 +00004670 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4671 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004672
aliguori731b0362009-03-05 23:01:42 +00004673 mon->password_completion_cb = completion_cb;
4674 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004675
aliguoricde76ee2009-03-05 23:01:51 +00004676 err = monitor_read_password(mon, bdrv_password_cb, bs);
4677
4678 if (err && completion_cb)
4679 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004680
4681 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004682}