blob: ef33cf4402b6cb116ba01a7773f1374121a7da31 [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 Capitulino13917be2009-10-07 13:41:54 -0300106 void (*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;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200123 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200124 JSONMessageParser parser;
125} 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 Capitulino8204a912009-11-18 23:05:31 -0200140 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500141 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000142 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000143};
144
Blue Swirl72cf2d42009-09-12 07:36:22 +0000145static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000146
Anthony Liguoric227f092009-10-01 16:12:16 -0500147static const mon_cmd_t mon_cmds[];
148static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000149
aliguori87127162009-03-05 23:01:29 +0000150Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000151
aliguori731b0362009-03-05 23:01:42 +0000152static void monitor_command_cb(Monitor *mon, const char *cmdline,
153 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000154
Luiz Capitulino418173c2009-11-26 22:58:51 -0200155/* Return true if in control mode, false otherwise */
156static inline int monitor_ctrl_mode(const Monitor *mon)
157{
158 return (mon->flags & MONITOR_USE_CONTROL);
159}
160
aliguori731b0362009-03-05 23:01:42 +0000161static void monitor_read_command(Monitor *mon, int show_prompt)
162{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200163 if (!mon->rs)
164 return;
165
aliguori731b0362009-03-05 23:01:42 +0000166 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
167 if (show_prompt)
168 readline_show_prompt(mon->rs);
169}
bellard6a00d602005-11-21 23:25:50 +0000170
aliguoricde76ee2009-03-05 23:01:51 +0000171static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
172 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000173{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100174 if (monitor_ctrl_mode(mon)) {
175 qemu_error_new(QERR_MISSING_PARAMETER, "password");
176 return -EINVAL;
177 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000178 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
179 /* prompt is printed on return from the command handler */
180 return 0;
181 } else {
182 monitor_printf(mon, "terminal does not support password prompting\n");
183 return -ENOTTY;
184 }
aliguoribb5fc202009-03-05 23:01:15 +0000185}
186
aliguori376253e2009-03-05 23:01:23 +0000187void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000188{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200189 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000190 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
191 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000192 }
193}
194
195/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000196static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000197{
ths60fe76f2007-12-16 03:02:09 +0000198 char c;
aliguori731b0362009-03-05 23:01:42 +0000199
bellard7e2515e2004-08-01 21:52:19 +0000200 for(;;) {
201 c = *str++;
202 if (c == '\0')
203 break;
bellard7ba12602006-07-14 20:26:42 +0000204 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000205 mon->outbuf[mon->outbuf_index++] = '\r';
206 mon->outbuf[mon->outbuf_index++] = c;
207 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
208 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000209 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000210 }
211}
212
aliguori376253e2009-03-05 23:01:23 +0000213void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000214{
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200215 if (!mon)
216 return;
217
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200218 if (mon->mc && !mon->mc->print_enabled) {
219 qemu_error_new(QERR_UNDEFINED_ERROR);
220 } else {
221 char buf[4096];
222 vsnprintf(buf, sizeof(buf), fmt, ap);
223 monitor_puts(mon, buf);
224 }
bellard7e2515e2004-08-01 21:52:19 +0000225}
226
aliguori376253e2009-03-05 23:01:23 +0000227void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000228{
229 va_list ap;
230 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000231 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000232 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000233}
234
aliguori376253e2009-03-05 23:01:23 +0000235void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000236{
237 int i;
238
239 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000240 switch (filename[i]) {
241 case ' ':
242 case '"':
243 case '\\':
244 monitor_printf(mon, "\\%c", filename[i]);
245 break;
246 case '\t':
247 monitor_printf(mon, "\\t");
248 break;
249 case '\r':
250 monitor_printf(mon, "\\r");
251 break;
252 case '\n':
253 monitor_printf(mon, "\\n");
254 break;
255 default:
256 monitor_printf(mon, "%c", filename[i]);
257 break;
258 }
thsfef30742006-12-22 14:11:32 +0000259 }
260}
261
bellard7fe48482004-10-09 18:08:01 +0000262static int monitor_fprintf(FILE *stream, const char *fmt, ...)
263{
264 va_list ap;
265 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000266 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000267 va_end(ap);
268 return 0;
269}
270
Luiz Capitulino13c74252009-10-07 13:41:55 -0300271static void monitor_user_noop(Monitor *mon, const QObject *data) { }
272
Luiz Capitulino13917be2009-10-07 13:41:54 -0300273static inline int monitor_handler_ported(const mon_cmd_t *cmd)
274{
275 return cmd->user_print != NULL;
276}
277
Adam Litke940cc302010-01-25 12:18:44 -0600278static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
279{
280 return cmd->async != 0;
281}
282
Luiz Capitulino8204a912009-11-18 23:05:31 -0200283static inline int monitor_has_error(const Monitor *mon)
284{
285 return mon->error != NULL;
286}
287
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200288static void monitor_json_emitter(Monitor *mon, const QObject *data)
289{
290 QString *json;
291
292 json = qobject_to_json(data);
293 assert(json != NULL);
294
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200295 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200296 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200297 mon->mc->print_enabled = 0;
298
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200299 QDECREF(json);
300}
301
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200302static void monitor_protocol_emitter(Monitor *mon, QObject *data)
303{
304 QDict *qmp;
305
306 qmp = qdict_new();
307
308 if (!monitor_has_error(mon)) {
309 /* success response */
310 if (data) {
311 qobject_incref(data);
312 qdict_put_obj(qmp, "return", data);
313 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200314 /* return an empty QDict by default */
315 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200316 }
317 } else {
318 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100319 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200320 qdict_put(qmp, "error", mon->error->error);
321 QINCREF(mon->error->error);
322 QDECREF(mon->error);
323 mon->error = NULL;
324 }
325
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200326 if (mon->mc->id) {
327 qdict_put_obj(qmp, "id", mon->mc->id);
328 mon->mc->id = NULL;
329 }
330
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200331 monitor_json_emitter(mon, QOBJECT(qmp));
332 QDECREF(qmp);
333}
334
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200335static void timestamp_put(QDict *qdict)
336{
337 int err;
338 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000339 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200340
Blue Swirld08d6f02009-12-04 18:06:39 +0000341 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200342 if (err < 0)
343 return;
344
345 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
346 "'microseconds': %" PRId64 " }",
347 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
348 assert(obj != NULL);
349
350 qdict_put_obj(qdict, "timestamp", obj);
351}
352
353/**
354 * monitor_protocol_event(): Generate a Monitor event
355 *
356 * Event-specific data can be emitted through the (optional) 'data' parameter.
357 */
358void monitor_protocol_event(MonitorEvent event, QObject *data)
359{
360 QDict *qmp;
361 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600362 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200363
Blue Swirl242cd002009-12-04 18:05:45 +0000364 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200365
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200366 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000367 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200368 event_name = "DEBUG";
369 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000370 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200371 event_name = "SHUTDOWN";
372 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000373 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200374 event_name = "RESET";
375 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000376 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200377 event_name = "POWERDOWN";
378 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000379 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200380 event_name = "STOP";
381 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200382 case QEVENT_VNC_CONNECTED:
383 event_name = "VNC_CONNECTED";
384 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200385 case QEVENT_VNC_INITIALIZED:
386 event_name = "VNC_INITIALIZED";
387 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200388 case QEVENT_VNC_DISCONNECTED:
389 event_name = "VNC_DISCONNECTED";
390 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200391 default:
392 abort();
393 break;
394 }
395
396 qmp = qdict_new();
397 timestamp_put(qmp);
398 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200399 if (data) {
400 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200401 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200402 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200403
Adam Litkef039a562010-01-15 08:34:02 -0600404 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200405 if (monitor_ctrl_mode(mon)) {
406 monitor_json_emitter(mon, QOBJECT(qmp));
407 }
Adam Litkef039a562010-01-15 08:34:02 -0600408 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200409 QDECREF(qmp);
410}
411
bellard9dc39cb2004-03-14 21:38:27 +0000412static int compare_cmd(const char *name, const char *list)
413{
414 const char *p, *pstart;
415 int len;
416 len = strlen(name);
417 p = list;
418 for(;;) {
419 pstart = p;
420 p = strchr(p, '|');
421 if (!p)
422 p = pstart + strlen(pstart);
423 if ((p - pstart) == len && !memcmp(pstart, name, len))
424 return 1;
425 if (*p == '\0')
426 break;
427 p++;
428 }
429 return 0;
430}
431
Anthony Liguoric227f092009-10-01 16:12:16 -0500432static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000433 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000434{
Anthony Liguoric227f092009-10-01 16:12:16 -0500435 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000436
437 for(cmd = cmds; cmd->name != NULL; cmd++) {
438 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000439 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
440 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000441 }
442}
443
aliguori376253e2009-03-05 23:01:23 +0000444static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000445{
446 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000447 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000448 } else {
aliguori376253e2009-03-05 23:01:23 +0000449 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000450 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000451 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000452 monitor_printf(mon, "Log items (comma separated):\n");
453 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000454 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000455 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000456 }
457 }
bellard9dc39cb2004-03-14 21:38:27 +0000458 }
459}
460
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300461static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300462{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300463 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300464}
465
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300466static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000467{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200468 int all_devices;
469 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300470 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000471
bellard7954c732006-08-01 15:52:40 +0000472 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000473 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200474 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300475 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200476 continue;
477 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000478 }
479}
480
Adam Litke940cc302010-01-25 12:18:44 -0600481static void user_monitor_complete(void *opaque, QObject *ret_data)
482{
483 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
484
485 if (ret_data) {
486 data->user_print(data->mon, ret_data);
487 }
488 monitor_resume(data->mon);
489 qemu_free(data);
490}
491
492static void qmp_monitor_complete(void *opaque, QObject *ret_data)
493{
494 monitor_protocol_emitter(opaque, ret_data);
495}
496
497static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
498 const QDict *params)
499{
500 cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
501}
502
503static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
504{
505 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
506}
507
508static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
509 const QDict *params)
510{
511 int ret;
512
513 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
514 cb_data->mon = mon;
515 cb_data->user_print = cmd->user_print;
516 monitor_suspend(mon);
517 ret = cmd->mhandler.cmd_async(mon, params,
518 user_monitor_complete, cb_data);
519 if (ret < 0) {
520 monitor_resume(mon);
521 qemu_free(cb_data);
522 }
523}
524
525static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
526{
527 int ret;
528
529 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
530 cb_data->mon = mon;
531 cb_data->user_print = cmd->user_print;
532 monitor_suspend(mon);
533 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
534 if (ret < 0) {
535 monitor_resume(mon);
536 qemu_free(cb_data);
537 }
538}
539
Luiz Capitulino13c74252009-10-07 13:41:55 -0300540static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000541{
Anthony Liguoric227f092009-10-01 16:12:16 -0500542 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300543 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000544
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200545 if (!item) {
546 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000547 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200548 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300549
550 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000551 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300552 break;
bellard9dc39cb2004-03-14 21:38:27 +0000553 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300554
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200555 if (cmd->name == NULL) {
556 if (monitor_ctrl_mode(mon)) {
557 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
558 return;
559 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300560 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200561 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300562
Adam Litke940cc302010-01-25 12:18:44 -0600563 if (monitor_handler_is_async(cmd)) {
564 if (monitor_ctrl_mode(mon)) {
565 qmp_async_info_handler(mon, cmd);
566 } else {
567 user_async_info_handler(mon, cmd);
568 }
569 /*
570 * Indicate that this command is asynchronous and will not return any
571 * data (not even empty). Instead, the data will be returned via a
572 * completion callback.
573 */
574 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
575 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300576 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200577
578 if (!monitor_ctrl_mode(mon)) {
579 /*
580 * User Protocol function is called here, Monitor Protocol is
581 * handled by monitor_call_handler()
582 */
583 if (*ret_data)
584 cmd->user_print(mon, *ret_data);
585 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300586 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200587 if (monitor_ctrl_mode(mon)) {
588 /* handler not converted yet */
589 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
590 } else {
591 cmd->mhandler.info(mon);
592 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300593 }
594
bellard9dc39cb2004-03-14 21:38:27 +0000595 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300596
597help:
598 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000599}
600
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200601static void do_info_version_print(Monitor *mon, const QObject *data)
602{
603 QDict *qdict;
604
605 qdict = qobject_to_qdict(data);
606
607 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
608 qdict_get_str(qdict, "package"));
609}
610
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300611/**
612 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200613 *
614 * Return a QDict with the following information:
615 *
616 * - "qemu": QEMU's version
617 * - "package": package's version
618 *
619 * Example:
620 *
621 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300622 */
623static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000624{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200625 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
626 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000627}
628
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200629static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000630{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200631 QDict *qdict;
632
633 qdict = qobject_to_qdict(data);
634 if (qdict_size(qdict) == 0) {
635 return;
636 }
637
638 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
639}
640
641/**
642 * do_info_name(): Show VM name
643 *
644 * Return a QDict with the following information:
645 *
646 * - "name": VM's name (optional)
647 *
648 * Example:
649 *
650 * { "name": "qemu-name" }
651 */
652static void do_info_name(Monitor *mon, QObject **ret_data)
653{
654 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
655 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000656}
657
Luiz Capitulino1a728672009-12-10 17:15:56 -0200658static QObject *get_cmd_dict(const char *name)
659{
660 const char *p;
661
662 /* Remove '|' from some commands */
663 p = strchr(name, '|');
664 if (p) {
665 p++;
666 } else {
667 p = name;
668 }
669
670 return qobject_from_jsonf("{ 'name': %s }", p);
671}
672
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200673/**
674 * do_info_commands(): List QMP available commands
675 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200676 * Each command is represented by a QDict, the returned QObject is a QList
677 * of all commands.
678 *
679 * The QDict contains:
680 *
681 * - "name": command's name
682 *
683 * Example:
684 *
685 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200686 */
687static void do_info_commands(Monitor *mon, QObject **ret_data)
688{
689 QList *cmd_list;
690 const mon_cmd_t *cmd;
691
692 cmd_list = qlist_new();
693
694 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
695 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200696 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200697 }
698 }
699
700 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
701 if (monitor_handler_ported(cmd)) {
702 char buf[128];
703 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200704 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200705 }
706 }
707
708 *ret_data = QOBJECT(cmd_list);
709}
710
aurel32bf4f74c2008-12-18 22:42:34 +0000711#if defined(TARGET_I386)
Luiz Capitulino14f07202009-12-10 17:16:02 -0200712static void do_info_hpet_print(Monitor *mon, const QObject *data)
aliguori16b29ae2008-12-17 23:28:44 +0000713{
aliguori376253e2009-03-05 23:01:23 +0000714 monitor_printf(mon, "HPET is %s by QEMU\n",
Luiz Capitulino14f07202009-12-10 17:16:02 -0200715 qdict_get_bool(qobject_to_qdict(data), "enabled") ?
716 "enabled" : "disabled");
717}
718
719/**
720 * do_info_hpet(): Show HPET state
721 *
722 * Return a QDict with the following information:
723 *
724 * - "enabled": true if hpet if enabled, false otherwise
725 *
726 * Example:
727 *
728 * { "enabled": true }
729 */
730static void do_info_hpet(Monitor *mon, QObject **ret_data)
731{
732 *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
aliguori16b29ae2008-12-17 23:28:44 +0000733}
aurel32bf4f74c2008-12-18 22:42:34 +0000734#endif
aliguori16b29ae2008-12-17 23:28:44 +0000735
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200736static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000737{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200738 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
739}
740
741/**
742 * do_info_uuid(): Show VM UUID
743 *
744 * Return a QDict with the following information:
745 *
746 * - "UUID": Universally Unique Identifier
747 *
748 * Example:
749 *
750 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
751 */
752static void do_info_uuid(Monitor *mon, QObject **ret_data)
753{
754 char uuid[64];
755
756 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000757 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
758 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
759 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
760 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200761 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000762}
763
bellard6a00d602005-11-21 23:25:50 +0000764/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000765static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000766{
767 CPUState *env;
768
769 for(env = first_cpu; env != NULL; env = env->next_cpu) {
770 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000771 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000772 return 0;
773 }
774 }
775 return -1;
776}
777
pbrook9596ebb2007-11-18 01:44:38 +0000778static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000779{
aliguori731b0362009-03-05 23:01:42 +0000780 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000781 mon_set_cpu(0);
782 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300783 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000784 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000785}
786
aliguori376253e2009-03-05 23:01:23 +0000787static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000788{
bellard6a00d602005-11-21 23:25:50 +0000789 CPUState *env;
790 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000791#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000792 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000793 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000794#else
aliguori376253e2009-03-05 23:01:23 +0000795 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000796 0);
bellard9307c4c2004-04-04 12:57:25 +0000797#endif
798}
799
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300800static void print_cpu_iter(QObject *obj, void *opaque)
801{
802 QDict *cpu;
803 int active = ' ';
804 Monitor *mon = opaque;
805
806 assert(qobject_type(obj) == QTYPE_QDICT);
807 cpu = qobject_to_qdict(obj);
808
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200809 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300810 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200811 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300812
813 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
814
815#if defined(TARGET_I386)
816 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
817 (target_ulong) qdict_get_int(cpu, "pc"));
818#elif defined(TARGET_PPC)
819 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
820 (target_long) qdict_get_int(cpu, "nip"));
821#elif defined(TARGET_SPARC)
822 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
823 (target_long) qdict_get_int(cpu, "pc"));
824 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
825 (target_long) qdict_get_int(cpu, "npc"));
826#elif defined(TARGET_MIPS)
827 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
828 (target_long) qdict_get_int(cpu, "PC"));
829#endif
830
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200831 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300832 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200833 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300834
835 monitor_printf(mon, "\n");
836}
837
838static void monitor_print_cpus(Monitor *mon, const QObject *data)
839{
840 QList *cpu_list;
841
842 assert(qobject_type(data) == QTYPE_QLIST);
843 cpu_list = qobject_to_qlist(data);
844 qlist_iter(cpu_list, print_cpu_iter, mon);
845}
846
847/**
848 * do_info_cpus(): Show CPU information
849 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200850 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300851 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200852 * - "cpu": CPU index
853 * - "current": true if this is the current CPU, false otherwise
854 * - "halted": true if the cpu is halted, false otherwise
855 * - Current program counter. The key's name depends on the architecture:
856 * "pc": i386/x86)64
857 * "nip": PPC
858 * "pc" and "npc": sparc
859 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300860 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200861 * Example:
862 *
863 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
864 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300865 */
866static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000867{
868 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300869 QList *cpu_list;
870
871 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000872
873 /* just to set the default cpu if not already done */
874 mon_get_cpu();
875
876 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200877 QDict *cpu;
878 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300879
Avi Kivity4c0960c2009-08-17 23:19:53 +0300880 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300881
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200882 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
883 env->cpu_index, env == mon->mon_cpu,
884 env->halted);
885 assert(obj != NULL);
886
887 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300888
bellard6a00d602005-11-21 23:25:50 +0000889#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300890 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000891#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300892 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000893#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300894 qdict_put(cpu, "pc", qint_from_int(env->pc));
895 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000896#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300897 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000898#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300899
900 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000901 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300902
903 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000904}
905
Markus Armbruster81a1b452010-01-20 13:07:35 +0100906static void do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000907{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300908 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000909 if (mon_set_cpu(index) < 0)
Markus Armbruster81a1b452010-01-20 13:07:35 +0100910 qemu_error_new(QERR_INVALID_CPU_INDEX);
bellard6a00d602005-11-21 23:25:50 +0000911}
912
aliguori376253e2009-03-05 23:01:23 +0000913static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000914{
aliguori376253e2009-03-05 23:01:23 +0000915 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000916}
917
aliguori376253e2009-03-05 23:01:23 +0000918static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000919{
920 int i;
bellard7e2515e2004-08-01 21:52:19 +0000921 const char *str;
ths3b46e622007-09-17 08:09:54 +0000922
aliguoricde76ee2009-03-05 23:01:51 +0000923 if (!mon->rs)
924 return;
bellard7e2515e2004-08-01 21:52:19 +0000925 i = 0;
926 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000927 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000928 if (!str)
929 break;
aliguori376253e2009-03-05 23:01:23 +0000930 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000931 i++;
bellardaa455482004-04-04 13:07:25 +0000932 }
933}
934
j_mayer76a66252007-03-07 08:32:30 +0000935#if defined(TARGET_PPC)
936/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000937static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000938{
939 CPUState *env;
940
941 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000942 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000943}
944#endif
945
Luiz Capitulinob223f352009-10-07 13:41:56 -0300946/**
947 * do_quit(): Quit QEMU execution
948 */
949static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000950{
951 exit(0);
952}
953
aliguori376253e2009-03-05 23:01:23 +0000954static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000955{
956 if (bdrv_is_inserted(bs)) {
957 if (!force) {
958 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100959 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
960 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000961 return -1;
962 }
963 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100964 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000965 return -1;
966 }
967 }
968 bdrv_close(bs);
969 }
970 return 0;
971}
972
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300973static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000974{
975 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300976 int force = qdict_get_int(qdict, "force");
Luiz Capitulino78d714e2009-12-14 18:53:21 -0200977 const char *filename = qdict_get_str(qdict, "device");
bellard9dc39cb2004-03-14 21:38:27 +0000978
bellard9307c4c2004-04-04 12:57:25 +0000979 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000980 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100981 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000982 return;
983 }
aliguori376253e2009-03-05 23:01:23 +0000984 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000985}
986
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -0200987static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
988 QObject **ret_data)
989{
990 BlockDriverState *bs;
991
992 bs = bdrv_find(qdict_get_str(qdict, "device"));
993 if (!bs) {
994 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
995 return;
996 }
997
998 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
999 qemu_error_new(QERR_INVALID_PASSWORD);
1000 }
1001}
1002
aliguori376253e2009-03-05 23:01:23 +00001003static void do_change_block(Monitor *mon, const char *device,
1004 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001005{
1006 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +00001007 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +00001008
bellard9307c4c2004-04-04 12:57:25 +00001009 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +00001010 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001011 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +00001012 return;
1013 }
aurel322ecea9b2008-06-18 22:10:01 +00001014 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +01001015 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001016 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001017 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +00001018 return;
1019 }
1020 }
aliguori376253e2009-03-05 23:01:23 +00001021 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +00001022 return;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001023 bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
aliguori376253e2009-03-05 23:01:23 +00001024 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001025}
1026
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001027static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +00001028{
1029 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001030 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +00001031
Markus Armbruster2895e072009-12-07 21:37:01 +01001032}
1033
1034static void change_vnc_password_cb(Monitor *mon, const char *password,
1035 void *opaque)
1036{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001037 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +00001038 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00001039}
1040
aliguori376253e2009-03-05 23:01:23 +00001041static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +00001042{
ths70848512007-08-25 01:37:05 +00001043 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +00001044 strcmp(target, "password") == 0) {
1045 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +00001046 char password[9];
aliguori28a76be2009-03-06 20:27:40 +00001047 strncpy(password, arg, sizeof(password));
1048 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001049 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +00001050 } else {
aliguori376253e2009-03-05 23:01:23 +00001051 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001052 }
ths70848512007-08-25 01:37:05 +00001053 } else {
aliguori28a76be2009-03-06 20:27:40 +00001054 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001055 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +00001056 }
thse25a5822007-08-25 01:36:20 +00001057}
1058
Markus Armbrusterec3b82a2009-12-07 21:37:09 +01001059/**
1060 * do_change(): Change a removable medium, or VNC configuration
1061 */
1062static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +00001063{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001064 const char *device = qdict_get_str(qdict, "device");
1065 const char *target = qdict_get_str(qdict, "target");
1066 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +00001067 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001068 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +00001069 } else {
aliguori28a76be2009-03-06 20:27:40 +00001070 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +00001071 }
1072}
1073
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001074static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +00001075{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001076 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +00001077}
1078
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001079static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001080{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001081 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001082}
1083
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001084static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001085{
1086 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001087 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001088
bellard9307c4c2004-04-04 12:57:25 +00001089 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001090 mask = 0;
1091 } else {
bellard9307c4c2004-04-04 12:57:25 +00001092 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001093 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001094 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001095 return;
1096 }
1097 }
1098 cpu_set_log(mask);
1099}
1100
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001101static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001102{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001103 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001104 if (!option || !strcmp(option, "on")) {
1105 singlestep = 1;
1106 } else if (!strcmp(option, "off")) {
1107 singlestep = 0;
1108 } else {
1109 monitor_printf(mon, "unexpected option %s\n", option);
1110 }
1111}
1112
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001113/**
1114 * do_stop(): Stop VM execution
1115 */
1116static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001117{
1118 vm_stop(EXCP_INTERRUPT);
1119}
1120
aliguoribb5fc202009-03-05 23:01:15 +00001121static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001122
aliguori376253e2009-03-05 23:01:23 +00001123struct bdrv_iterate_context {
1124 Monitor *mon;
1125 int err;
1126};
aliguoric0f4ce72009-03-05 23:01:01 +00001127
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001128/**
1129 * do_cont(): Resume emulation.
1130 */
1131static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001132{
1133 struct bdrv_iterate_context context = { mon, 0 };
1134
1135 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001136 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +00001137 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +00001138 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +00001139}
1140
aliguoribb5fc202009-03-05 23:01:15 +00001141static void bdrv_key_cb(void *opaque, int err)
1142{
aliguori376253e2009-03-05 23:01:23 +00001143 Monitor *mon = opaque;
1144
aliguoribb5fc202009-03-05 23:01:15 +00001145 /* another key was set successfully, retry to continue */
1146 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001147 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001148}
1149
1150static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1151{
aliguori376253e2009-03-05 23:01:23 +00001152 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001153
aliguori376253e2009-03-05 23:01:23 +00001154 if (!context->err && bdrv_key_required(bs)) {
1155 context->err = -EBUSY;
1156 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1157 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001158 }
1159}
1160
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001161static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001162{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001163 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001164 if (!device)
1165 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1166 if (gdbserver_start(device) < 0) {
1167 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1168 device);
1169 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001170 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001171 } else {
aliguori59030a82009-04-05 18:43:41 +00001172 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1173 device);
bellard8a7ddc32004-03-31 19:00:16 +00001174 }
1175}
1176
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001177static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001178{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001179 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001180 if (select_watchdog_action(action) == -1) {
1181 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1182 }
1183}
1184
aliguori376253e2009-03-05 23:01:23 +00001185static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001186{
aliguori376253e2009-03-05 23:01:23 +00001187 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001188 switch(c) {
1189 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001190 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001191 break;
1192 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001193 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001194 break;
1195 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001196 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001197 break;
1198 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001199 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001200 break;
1201 default:
1202 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001204 } else {
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001206 }
1207 break;
1208 }
aliguori376253e2009-03-05 23:01:23 +00001209 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001210}
1211
aliguori376253e2009-03-05 23:01:23 +00001212static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001213 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001214{
bellard6a00d602005-11-21 23:25:50 +00001215 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001216 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001217 uint8_t buf[16];
1218 uint64_t v;
1219
1220 if (format == 'i') {
1221 int flags;
1222 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001223 env = mon_get_cpu();
Markus Armbruster09b94182010-01-20 13:07:30 +01001224 if (!is_physical)
bellard6a00d602005-11-21 23:25:50 +00001225 return;
bellard9307c4c2004-04-04 12:57:25 +00001226#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001227 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001228 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001229 } else if (wsize == 4) {
1230 flags = 0;
1231 } else {
bellard6a15fd12006-04-12 21:07:07 +00001232 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001233 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001234 if (env) {
1235#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001236 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001237 (env->segs[R_CS].flags & DESC_L_MASK))
1238 flags = 2;
1239 else
1240#endif
1241 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1242 flags = 1;
1243 }
bellard4c27ba22004-04-25 18:05:08 +00001244 }
1245#endif
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001247 return;
1248 }
1249
1250 len = wsize * count;
1251 if (wsize == 1)
1252 line_size = 8;
1253 else
1254 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001255 max_digits = 0;
1256
1257 switch(format) {
1258 case 'o':
1259 max_digits = (wsize * 8 + 2) / 3;
1260 break;
1261 default:
1262 case 'x':
1263 max_digits = (wsize * 8) / 4;
1264 break;
1265 case 'u':
1266 case 'd':
1267 max_digits = (wsize * 8 * 10 + 32) / 33;
1268 break;
1269 case 'c':
1270 wsize = 1;
1271 break;
1272 }
1273
1274 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001275 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001277 else
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001279 l = len;
1280 if (l > line_size)
1281 l = line_size;
1282 if (is_physical) {
1283 cpu_physical_memory_rw(addr, buf, l, 0);
1284 } else {
bellard6a00d602005-11-21 23:25:50 +00001285 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001286 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001288 break;
1289 }
bellard9307c4c2004-04-04 12:57:25 +00001290 }
ths5fafdf22007-09-16 21:08:06 +00001291 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001292 while (i < l) {
1293 switch(wsize) {
1294 default:
1295 case 1:
1296 v = ldub_raw(buf + i);
1297 break;
1298 case 2:
1299 v = lduw_raw(buf + i);
1300 break;
1301 case 4:
bellard92a31b12005-02-10 22:00:52 +00001302 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 case 8:
1305 v = ldq_raw(buf + i);
1306 break;
1307 }
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001309 switch(format) {
1310 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001312 break;
1313 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001314 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001315 break;
1316 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001318 break;
1319 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001321 break;
1322 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001324 break;
1325 }
1326 i += wsize;
1327 }
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001329 addr += l;
1330 len -= l;
1331 }
1332}
1333
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001334static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001335{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001336 int count = qdict_get_int(qdict, "count");
1337 int format = qdict_get_int(qdict, "format");
1338 int size = qdict_get_int(qdict, "size");
1339 target_long addr = qdict_get_int(qdict, "addr");
1340
aliguori376253e2009-03-05 23:01:23 +00001341 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001342}
1343
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001344static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001345{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001346 int count = qdict_get_int(qdict, "count");
1347 int format = qdict_get_int(qdict, "format");
1348 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001349 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001350
aliguori376253e2009-03-05 23:01:23 +00001351 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001352}
1353
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001354static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001355{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001356 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001357 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001358
blueswir17743e582007-09-24 18:39:04 +00001359#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001360 switch(format) {
1361 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001362 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001363 break;
1364 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001366 break;
1367 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001369 break;
1370 default:
1371 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001372 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001373 break;
1374 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001375 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001376 break;
1377 }
bellard92a31b12005-02-10 22:00:52 +00001378#else
1379 switch(format) {
1380 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001382 break;
1383 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001385 break;
1386 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001388 break;
1389 default:
1390 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001392 break;
1393 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001395 break;
1396 }
1397#endif
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001399}
1400
Luiz Capitulino57e09452009-10-16 12:23:43 -03001401static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001402{
1403 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001404 uint32_t size = qdict_get_int(qdict, "size");
1405 const char *filename = qdict_get_str(qdict, "filename");
1406 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001407 uint32_t l;
1408 CPUState *env;
1409 uint8_t buf[1024];
1410
1411 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001412
1413 f = fopen(filename, "wb");
1414 if (!f) {
Markus Armbrusterc34ed282010-01-20 13:07:32 +01001415 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
bellardb371dc52007-01-03 15:20:39 +00001416 return;
1417 }
1418 while (size != 0) {
1419 l = sizeof(buf);
1420 if (l > size)
1421 l = size;
1422 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001423 if (fwrite(buf, 1, l, f) != l) {
1424 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1425 goto exit;
1426 }
bellardb371dc52007-01-03 15:20:39 +00001427 addr += l;
1428 size -= l;
1429 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001430exit:
bellardb371dc52007-01-03 15:20:39 +00001431 fclose(f);
1432}
1433
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001434static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1435 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001436{
1437 FILE *f;
1438 uint32_t l;
1439 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001440 uint32_t size = qdict_get_int(qdict, "size");
1441 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001442 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001443
1444 f = fopen(filename, "wb");
1445 if (!f) {
Markus Armbruster95fada82010-01-20 13:07:33 +01001446 qemu_error_new(QERR_OPEN_FILE_FAILED, filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001447 return;
1448 }
1449 while (size != 0) {
1450 l = sizeof(buf);
1451 if (l > size)
1452 l = size;
1453 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001454 if (fwrite(buf, 1, l, f) != l) {
1455 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1456 goto exit;
1457 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001458 fflush(f);
1459 addr += l;
1460 size -= l;
1461 }
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001462exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001463 fclose(f);
1464}
1465
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001466static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001467{
1468 uint32_t addr;
1469 uint8_t buf[1];
1470 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001471 uint32_t start = qdict_get_int(qdict, "start");
1472 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001473
1474 sum = 0;
1475 for(addr = start; addr < (start + size); addr++) {
1476 cpu_physical_memory_rw(addr, buf, 1, 0);
1477 /* BSD sum algorithm ('sum' Unix command) */
1478 sum = (sum >> 1) | (sum << 15);
1479 sum += buf[0];
1480 }
aliguori376253e2009-03-05 23:01:23 +00001481 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001482}
1483
bellarda3a91a32004-06-04 11:06:21 +00001484typedef struct {
1485 int keycode;
1486 const char *name;
1487} KeyDef;
1488
1489static const KeyDef key_defs[] = {
1490 { 0x2a, "shift" },
1491 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001492
bellarda3a91a32004-06-04 11:06:21 +00001493 { 0x38, "alt" },
1494 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001495 { 0x64, "altgr" },
1496 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001497 { 0x1d, "ctrl" },
1498 { 0x9d, "ctrl_r" },
1499
1500 { 0xdd, "menu" },
1501
1502 { 0x01, "esc" },
1503
1504 { 0x02, "1" },
1505 { 0x03, "2" },
1506 { 0x04, "3" },
1507 { 0x05, "4" },
1508 { 0x06, "5" },
1509 { 0x07, "6" },
1510 { 0x08, "7" },
1511 { 0x09, "8" },
1512 { 0x0a, "9" },
1513 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001514 { 0x0c, "minus" },
1515 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001516 { 0x0e, "backspace" },
1517
1518 { 0x0f, "tab" },
1519 { 0x10, "q" },
1520 { 0x11, "w" },
1521 { 0x12, "e" },
1522 { 0x13, "r" },
1523 { 0x14, "t" },
1524 { 0x15, "y" },
1525 { 0x16, "u" },
1526 { 0x17, "i" },
1527 { 0x18, "o" },
1528 { 0x19, "p" },
1529
1530 { 0x1c, "ret" },
1531
1532 { 0x1e, "a" },
1533 { 0x1f, "s" },
1534 { 0x20, "d" },
1535 { 0x21, "f" },
1536 { 0x22, "g" },
1537 { 0x23, "h" },
1538 { 0x24, "j" },
1539 { 0x25, "k" },
1540 { 0x26, "l" },
1541
1542 { 0x2c, "z" },
1543 { 0x2d, "x" },
1544 { 0x2e, "c" },
1545 { 0x2f, "v" },
1546 { 0x30, "b" },
1547 { 0x31, "n" },
1548 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001549 { 0x33, "comma" },
1550 { 0x34, "dot" },
1551 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001552
balrog4d3b6f62008-02-10 16:33:14 +00001553 { 0x37, "asterisk" },
1554
bellarda3a91a32004-06-04 11:06:21 +00001555 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001556 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001557 { 0x3b, "f1" },
1558 { 0x3c, "f2" },
1559 { 0x3d, "f3" },
1560 { 0x3e, "f4" },
1561 { 0x3f, "f5" },
1562 { 0x40, "f6" },
1563 { 0x41, "f7" },
1564 { 0x42, "f8" },
1565 { 0x43, "f9" },
1566 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001567 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001568 { 0x46, "scroll_lock" },
1569
bellard64866c32006-05-07 18:03:31 +00001570 { 0xb5, "kp_divide" },
1571 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001572 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001573 { 0x4e, "kp_add" },
1574 { 0x9c, "kp_enter" },
1575 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001576 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001577
1578 { 0x52, "kp_0" },
1579 { 0x4f, "kp_1" },
1580 { 0x50, "kp_2" },
1581 { 0x51, "kp_3" },
1582 { 0x4b, "kp_4" },
1583 { 0x4c, "kp_5" },
1584 { 0x4d, "kp_6" },
1585 { 0x47, "kp_7" },
1586 { 0x48, "kp_8" },
1587 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001588
bellarda3a91a32004-06-04 11:06:21 +00001589 { 0x56, "<" },
1590
1591 { 0x57, "f11" },
1592 { 0x58, "f12" },
1593
1594 { 0xb7, "print" },
1595
1596 { 0xc7, "home" },
1597 { 0xc9, "pgup" },
1598 { 0xd1, "pgdn" },
1599 { 0xcf, "end" },
1600
1601 { 0xcb, "left" },
1602 { 0xc8, "up" },
1603 { 0xd0, "down" },
1604 { 0xcd, "right" },
1605
1606 { 0xd2, "insert" },
1607 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001608#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1609 { 0xf0, "stop" },
1610 { 0xf1, "again" },
1611 { 0xf2, "props" },
1612 { 0xf3, "undo" },
1613 { 0xf4, "front" },
1614 { 0xf5, "copy" },
1615 { 0xf6, "open" },
1616 { 0xf7, "paste" },
1617 { 0xf8, "find" },
1618 { 0xf9, "cut" },
1619 { 0xfa, "lf" },
1620 { 0xfb, "help" },
1621 { 0xfc, "meta_l" },
1622 { 0xfd, "meta_r" },
1623 { 0xfe, "compose" },
1624#endif
bellarda3a91a32004-06-04 11:06:21 +00001625 { 0, NULL },
1626};
1627
1628static int get_keycode(const char *key)
1629{
1630 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001631 char *endp;
1632 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001633
1634 for(p = key_defs; p->name != NULL; p++) {
1635 if (!strcmp(key, p->name))
1636 return p->keycode;
1637 }
bellard64866c32006-05-07 18:03:31 +00001638 if (strstart(key, "0x", NULL)) {
1639 ret = strtoul(key, &endp, 0);
1640 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1641 return ret;
1642 }
bellarda3a91a32004-06-04 11:06:21 +00001643 return -1;
1644}
1645
balrogc8256f92008-06-08 22:45:01 +00001646#define MAX_KEYCODES 16
1647static uint8_t keycodes[MAX_KEYCODES];
1648static int nb_pending_keycodes;
1649static QEMUTimer *key_timer;
1650
1651static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001652{
balrogc8256f92008-06-08 22:45:01 +00001653 int keycode;
1654
1655 while (nb_pending_keycodes > 0) {
1656 nb_pending_keycodes--;
1657 keycode = keycodes[nb_pending_keycodes];
1658 if (keycode & 0x80)
1659 kbd_put_keycode(0xe0);
1660 kbd_put_keycode(keycode | 0x80);
1661 }
1662}
1663
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001664static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001665{
balrog3401c0d2008-06-04 10:05:59 +00001666 char keyname_buf[16];
1667 char *separator;
1668 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001669 const char *string = qdict_get_str(qdict, "string");
1670 int has_hold_time = qdict_haskey(qdict, "hold_time");
1671 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001672
balrogc8256f92008-06-08 22:45:01 +00001673 if (nb_pending_keycodes > 0) {
1674 qemu_del_timer(key_timer);
1675 release_keys(NULL);
1676 }
1677 if (!has_hold_time)
1678 hold_time = 100;
1679 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001680 while (1) {
1681 separator = strchr(string, '-');
1682 keyname_len = separator ? separator - string : strlen(string);
1683 if (keyname_len > 0) {
1684 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1685 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001686 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001687 return;
bellarda3a91a32004-06-04 11:06:21 +00001688 }
balrogc8256f92008-06-08 22:45:01 +00001689 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001690 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001691 return;
1692 }
1693 keyname_buf[keyname_len] = 0;
1694 keycode = get_keycode(keyname_buf);
1695 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001696 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001697 return;
1698 }
balrogc8256f92008-06-08 22:45:01 +00001699 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001700 }
balrog3401c0d2008-06-04 10:05:59 +00001701 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001702 break;
balrog3401c0d2008-06-04 10:05:59 +00001703 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001704 }
balrogc8256f92008-06-08 22:45:01 +00001705 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001706 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001707 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001708 keycode = keycodes[i];
1709 if (keycode & 0x80)
1710 kbd_put_keycode(0xe0);
1711 kbd_put_keycode(keycode & 0x7f);
1712 }
balrogc8256f92008-06-08 22:45:01 +00001713 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001714 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001715 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001716}
1717
bellard13224a82006-07-14 22:03:35 +00001718static int mouse_button_state;
1719
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001720static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001721{
1722 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001723 const char *dx_str = qdict_get_str(qdict, "dx_str");
1724 const char *dy_str = qdict_get_str(qdict, "dy_str");
1725 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001726 dx = strtol(dx_str, NULL, 0);
1727 dy = strtol(dy_str, NULL, 0);
1728 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001729 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001730 dz = strtol(dz_str, NULL, 0);
1731 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1732}
1733
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001734static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001735{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001736 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001737 mouse_button_state = button_state;
1738 kbd_mouse_event(0, 0, 0, mouse_button_state);
1739}
1740
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001741static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001742{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001743 int size = qdict_get_int(qdict, "size");
1744 int addr = qdict_get_int(qdict, "addr");
1745 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001746 uint32_t val;
1747 int suffix;
1748
1749 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001750 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001751 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001752 addr++;
1753 }
1754 addr &= 0xffff;
1755
1756 switch(size) {
1757 default:
1758 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001759 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001760 suffix = 'b';
1761 break;
1762 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001763 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001764 suffix = 'w';
1765 break;
1766 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001767 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001768 suffix = 'l';
1769 break;
1770 }
aliguori376253e2009-03-05 23:01:23 +00001771 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1772 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001773}
bellarda3a91a32004-06-04 11:06:21 +00001774
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001775static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001776{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001777 int size = qdict_get_int(qdict, "size");
1778 int addr = qdict_get_int(qdict, "addr");
1779 int val = qdict_get_int(qdict, "val");
1780
Jan Kiszkaf1147842009-07-14 10:20:11 +02001781 addr &= IOPORTS_MASK;
1782
1783 switch (size) {
1784 default:
1785 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001786 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001787 break;
1788 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001789 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001790 break;
1791 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001792 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001793 break;
1794 }
1795}
1796
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001797static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001798{
1799 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001800 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001801
Jan Kiszka76e30d02009-07-02 00:19:02 +02001802 res = qemu_boot_set(bootdevice);
1803 if (res == 0) {
1804 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1805 } else if (res > 0) {
1806 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001807 } else {
aliguori376253e2009-03-05 23:01:23 +00001808 monitor_printf(mon, "no function defined to set boot device list for "
1809 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001810 }
1811}
1812
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001813/**
1814 * do_system_reset(): Issue a machine reset
1815 */
1816static void do_system_reset(Monitor *mon, const QDict *qdict,
1817 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001818{
1819 qemu_system_reset_request();
1820}
1821
Luiz Capitulino43076662009-10-07 13:41:59 -03001822/**
1823 * do_system_powerdown(): Issue a machine powerdown
1824 */
1825static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1826 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001827{
1828 qemu_system_powerdown_request();
1829}
1830
bellardb86bda52004-09-18 19:32:46 +00001831#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001832static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001833{
aliguori376253e2009-03-05 23:01:23 +00001834 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1835 addr,
1836 pte & mask,
1837 pte & PG_GLOBAL_MASK ? 'G' : '-',
1838 pte & PG_PSE_MASK ? 'P' : '-',
1839 pte & PG_DIRTY_MASK ? 'D' : '-',
1840 pte & PG_ACCESSED_MASK ? 'A' : '-',
1841 pte & PG_PCD_MASK ? 'C' : '-',
1842 pte & PG_PWT_MASK ? 'T' : '-',
1843 pte & PG_USER_MASK ? 'U' : '-',
1844 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001845}
1846
aliguori376253e2009-03-05 23:01:23 +00001847static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001848{
bellard6a00d602005-11-21 23:25:50 +00001849 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001850 int l1, l2;
1851 uint32_t pgd, pde, pte;
1852
bellard6a00d602005-11-21 23:25:50 +00001853 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001854
bellardb86bda52004-09-18 19:32:46 +00001855 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001856 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001857 return;
1858 }
1859 pgd = env->cr[3] & ~0xfff;
1860 for(l1 = 0; l1 < 1024; l1++) {
1861 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1862 pde = le32_to_cpu(pde);
1863 if (pde & PG_PRESENT_MASK) {
1864 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001865 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001866 } else {
1867 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001868 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001869 (uint8_t *)&pte, 4);
1870 pte = le32_to_cpu(pte);
1871 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001872 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001873 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001874 ~0xfff);
1875 }
1876 }
1877 }
1878 }
1879 }
1880}
1881
aliguori376253e2009-03-05 23:01:23 +00001882static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001883 uint32_t end, int prot)
1884{
bellard9746b152004-11-11 18:30:24 +00001885 int prot1;
1886 prot1 = *plast_prot;
1887 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001888 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001889 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1890 *pstart, end, end - *pstart,
1891 prot1 & PG_USER_MASK ? 'u' : '-',
1892 'r',
1893 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001894 }
1895 if (prot != 0)
1896 *pstart = end;
1897 else
1898 *pstart = -1;
1899 *plast_prot = prot;
1900 }
1901}
1902
aliguori376253e2009-03-05 23:01:23 +00001903static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001904{
bellard6a00d602005-11-21 23:25:50 +00001905 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001906 int l1, l2, prot, last_prot;
1907 uint32_t pgd, pde, pte, start, end;
1908
bellard6a00d602005-11-21 23:25:50 +00001909 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001910
bellardb86bda52004-09-18 19:32:46 +00001911 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001912 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001913 return;
1914 }
1915 pgd = env->cr[3] & ~0xfff;
1916 last_prot = 0;
1917 start = -1;
1918 for(l1 = 0; l1 < 1024; l1++) {
1919 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1920 pde = le32_to_cpu(pde);
1921 end = l1 << 22;
1922 if (pde & PG_PRESENT_MASK) {
1923 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1924 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001925 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001926 } else {
1927 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001928 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001929 (uint8_t *)&pte, 4);
1930 pte = le32_to_cpu(pte);
1931 end = (l1 << 22) + (l2 << 12);
1932 if (pte & PG_PRESENT_MASK) {
1933 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1934 } else {
1935 prot = 0;
1936 }
aliguori376253e2009-03-05 23:01:23 +00001937 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001938 }
1939 }
1940 } else {
1941 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001942 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001943 }
1944 }
1945}
1946#endif
1947
aurel327c664e22009-03-03 06:12:22 +00001948#if defined(TARGET_SH4)
1949
aliguori376253e2009-03-05 23:01:23 +00001950static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001951{
aliguori376253e2009-03-05 23:01:23 +00001952 monitor_printf(mon, " tlb%i:\t"
1953 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1954 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1955 "dirty=%hhu writethrough=%hhu\n",
1956 idx,
1957 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1958 tlb->v, tlb->sh, tlb->c, tlb->pr,
1959 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001960}
1961
aliguori376253e2009-03-05 23:01:23 +00001962static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001963{
1964 CPUState *env = mon_get_cpu();
1965 int i;
1966
aliguori376253e2009-03-05 23:01:23 +00001967 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001968 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001969 print_tlb (mon, i, &env->itlb[i]);
1970 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001971 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001972 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001973}
1974
1975#endif
1976
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001977static void do_info_kvm_print(Monitor *mon, const QObject *data)
1978{
1979 QDict *qdict;
1980
1981 qdict = qobject_to_qdict(data);
1982
1983 monitor_printf(mon, "kvm support: ");
1984 if (qdict_get_bool(qdict, "present")) {
1985 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1986 "enabled" : "disabled");
1987 } else {
1988 monitor_printf(mon, "not compiled\n");
1989 }
1990}
1991
1992/**
1993 * do_info_kvm(): Show KVM information
1994 *
1995 * Return a QDict with the following information:
1996 *
1997 * - "enabled": true if KVM support is enabled, false otherwise
1998 * - "present": true if QEMU has KVM support, false otherwise
1999 *
2000 * Example:
2001 *
2002 * { "enabled": true, "present": true }
2003 */
2004static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00002005{
2006#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002007 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2008 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00002009#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002010 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00002011#endif
2012}
2013
aliguori030ea372009-04-21 22:30:47 +00002014static void do_info_numa(Monitor *mon)
2015{
aliguorib28b6232009-04-22 20:20:29 +00002016 int i;
aliguori030ea372009-04-21 22:30:47 +00002017 CPUState *env;
2018
2019 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2020 for (i = 0; i < nb_numa_nodes; i++) {
2021 monitor_printf(mon, "node %d cpus:", i);
2022 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2023 if (env->numa_node == i) {
2024 monitor_printf(mon, " %d", env->cpu_index);
2025 }
2026 }
2027 monitor_printf(mon, "\n");
2028 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2029 node_mem[i] >> 20);
2030 }
2031}
2032
bellard5f1ce942006-02-08 22:40:15 +00002033#ifdef CONFIG_PROFILER
2034
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002035int64_t qemu_time;
2036int64_t dev_time;
2037
aliguori376253e2009-03-05 23:01:23 +00002038static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002039{
2040 int64_t total;
2041 total = qemu_time;
2042 if (total == 0)
2043 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002044 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002045 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002046 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002047 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002048 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002049 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002050}
2051#else
aliguori376253e2009-03-05 23:01:23 +00002052static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002053{
aliguori376253e2009-03-05 23:01:23 +00002054 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002055}
2056#endif
2057
bellardec36b692006-07-16 18:57:03 +00002058/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002059static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002060
aliguori376253e2009-03-05 23:01:23 +00002061static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002062{
2063 int i;
2064 CaptureState *s;
2065
2066 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002067 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002068 s->ops.info (s->opaque);
2069 }
2070}
2071
Blue Swirl23130862009-06-06 08:22:04 +00002072#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002073static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002074{
2075 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002076 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002077 CaptureState *s;
2078
2079 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2080 if (i == n) {
2081 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002082 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002083 qemu_free (s);
2084 return;
2085 }
2086 }
2087}
2088
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002089static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002090{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002091 const char *path = qdict_get_str(qdict, "path");
2092 int has_freq = qdict_haskey(qdict, "freq");
2093 int freq = qdict_get_try_int(qdict, "freq", -1);
2094 int has_bits = qdict_haskey(qdict, "bits");
2095 int bits = qdict_get_try_int(qdict, "bits", -1);
2096 int has_channels = qdict_haskey(qdict, "nchannels");
2097 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002098 CaptureState *s;
2099
2100 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002101
2102 freq = has_freq ? freq : 44100;
2103 bits = has_bits ? bits : 16;
2104 nchannels = has_channels ? nchannels : 2;
2105
2106 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002107 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002108 qemu_free (s);
2109 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002110 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002111}
2112#endif
2113
aurel32dc1c0b72008-04-27 23:52:12 +00002114#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002115static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002116{
2117 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002118 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002119
2120 for (env = first_cpu; env != NULL; env = env->next_cpu)
2121 if (env->cpu_index == cpu_index) {
2122 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2123 break;
2124 }
2125}
2126#endif
2127
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002128static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002129{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002130 QDict *qdict;
2131
2132 qdict = qobject_to_qdict(data);
2133
2134 monitor_printf(mon, "VM status: ");
2135 if (qdict_get_bool(qdict, "running")) {
2136 monitor_printf(mon, "running");
2137 if (qdict_get_bool(qdict, "singlestep")) {
2138 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002139 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002140 } else {
2141 monitor_printf(mon, "paused");
2142 }
2143
2144 monitor_printf(mon, "\n");
2145}
2146
2147/**
2148 * do_info_status(): VM status
2149 *
2150 * Return a QDict with the following information:
2151 *
2152 * - "running": true if the VM is running, or false if it is paused
2153 * - "singlestep": true if the VM is in single step mode, false otherwise
2154 *
2155 * Example:
2156 *
2157 * { "running": true, "singlestep": false }
2158 */
2159static void do_info_status(Monitor *mon, QObject **ret_data)
2160{
2161 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2162 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002163}
2164
Adam Litke625a5be2010-01-26 14:17:35 -06002165static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002166{
Adam Litke625a5be2010-01-26 14:17:35 -06002167 Monitor *mon = opaque;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002168
Adam Litke625a5be2010-01-26 14:17:35 -06002169 if (strcmp(key, "actual"))
2170 monitor_printf(mon, ",%s=%" PRId64, key,
2171 qint_get_int(qobject_to_qint(obj)));
aliguoridf751fa2008-12-04 20:19:35 +00002172}
2173
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002174static void monitor_print_balloon(Monitor *mon, const QObject *data)
2175{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002176 QDict *qdict;
2177
2178 qdict = qobject_to_qdict(data);
Adam Litke625a5be2010-01-26 14:17:35 -06002179 if (!qdict_haskey(qdict, "actual"))
2180 return;
Luiz Capitulino7f179672009-12-10 17:15:55 -02002181
Adam Litke625a5be2010-01-26 14:17:35 -06002182 monitor_printf(mon, "balloon: actual=%" PRId64,
2183 qdict_get_int(qdict, "actual") >> 20);
2184 qdict_iter(qdict, print_balloon_stat, mon);
2185 monitor_printf(mon, "\n");
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002186}
2187
2188/**
2189 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002190 *
Adam Litke625a5be2010-01-26 14:17:35 -06002191 * Make an asynchronous request for balloon info. When the request completes
2192 * a QDict will be returned according to the following specification:
Luiz Capitulino7f179672009-12-10 17:15:55 -02002193 *
Adam Litke625a5be2010-01-26 14:17:35 -06002194 * - "actual": current balloon value in bytes
2195 * The following fields may or may not be present:
2196 * - "mem_swapped_in": Amount of memory swapped in (bytes)
2197 * - "mem_swapped_out": Amount of memory swapped out (bytes)
2198 * - "major_page_faults": Number of major faults
2199 * - "minor_page_faults": Number of minor faults
2200 * - "free_mem": Total amount of free and unused memory (bytes)
2201 * - "total_mem": Total amount of available memory (bytes)
Luiz Capitulino7f179672009-12-10 17:15:55 -02002202 *
2203 * Example:
2204 *
Adam Litke625a5be2010-01-26 14:17:35 -06002205 * { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
2206 * "major_page_faults": 142, "minor_page_faults": 239245,
2207 * "free_mem": 1014185984, "total_mem": 1044668416 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002208 */
Adam Litke625a5be2010-01-26 14:17:35 -06002209static int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +00002210{
Adam Litke625a5be2010-01-26 14:17:35 -06002211 int ret;
aliguoridf751fa2008-12-04 20:19:35 +00002212
Adam Litke625a5be2010-01-26 14:17:35 -06002213 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2214 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2215 return -1;
Luiz Capitulinocfdf2c42009-12-14 18:53:22 -02002216 }
Adam Litke625a5be2010-01-26 14:17:35 -06002217
2218 ret = qemu_balloon_status(cb, opaque);
2219 if (!ret) {
2220 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2221 return -1;
2222 }
2223
2224 return 0;
2225}
2226
2227/**
2228 * do_balloon(): Request VM to change its memory allocation
2229 */
2230static int do_balloon(Monitor *mon, const QDict *params,
2231 MonitorCompletion cb, void *opaque)
2232{
2233 int ret;
2234
2235 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2236 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
2237 return -1;
2238 }
2239
2240 ret = qemu_balloon(qdict_get_int(params, "value"), cb, opaque);
2241 if (ret == 0) {
2242 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
2243 return -1;
2244 }
2245
2246 return 0;
aliguoridf751fa2008-12-04 20:19:35 +00002247}
2248
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002249static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002250{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002251 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002252
aliguori76655d62009-03-06 20:27:37 +00002253 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002254 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002255 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002256 return acl;
2257}
aliguori76655d62009-03-06 20:27:37 +00002258
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002259static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002260{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002261 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002262 qemu_acl *acl = find_acl(mon, aclname);
2263 qemu_acl_entry *entry;
2264 int i = 0;
2265
2266 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002267 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002268 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002269 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002270 i++;
2271 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002272 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002273 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002274 }
2275}
2276
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002277static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002278{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002279 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002280 qemu_acl *acl = find_acl(mon, aclname);
2281
2282 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002283 qemu_acl_reset(acl);
2284 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002285 }
2286}
aliguori76655d62009-03-06 20:27:37 +00002287
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002288static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002289{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002290 const char *aclname = qdict_get_str(qdict, "aclname");
2291 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002292 qemu_acl *acl = find_acl(mon, aclname);
2293
2294 if (acl) {
2295 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002296 acl->defaultDeny = 0;
2297 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002298 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002299 acl->defaultDeny = 1;
2300 monitor_printf(mon, "acl: policy set to 'deny'\n");
2301 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002302 monitor_printf(mon, "acl: unknown policy '%s', "
2303 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002304 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002305 }
2306}
aliguori76655d62009-03-06 20:27:37 +00002307
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002308static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002309{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002310 const char *aclname = qdict_get_str(qdict, "aclname");
2311 const char *match = qdict_get_str(qdict, "match");
2312 const char *policy = qdict_get_str(qdict, "policy");
2313 int has_index = qdict_haskey(qdict, "index");
2314 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002315 qemu_acl *acl = find_acl(mon, aclname);
2316 int deny, ret;
2317
2318 if (acl) {
2319 if (strcmp(policy, "allow") == 0) {
2320 deny = 0;
2321 } else if (strcmp(policy, "deny") == 0) {
2322 deny = 1;
2323 } else {
2324 monitor_printf(mon, "acl: unknown policy '%s', "
2325 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002326 return;
2327 }
aliguori28a76be2009-03-06 20:27:40 +00002328 if (has_index)
2329 ret = qemu_acl_insert(acl, deny, match, index);
2330 else
2331 ret = qemu_acl_append(acl, deny, match);
2332 if (ret < 0)
2333 monitor_printf(mon, "acl: unable to add acl entry\n");
2334 else
2335 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002336 }
2337}
aliguori76655d62009-03-06 20:27:37 +00002338
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002339static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002340{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002341 const char *aclname = qdict_get_str(qdict, "aclname");
2342 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002343 qemu_acl *acl = find_acl(mon, aclname);
2344 int ret;
aliguori76655d62009-03-06 20:27:37 +00002345
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002346 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002347 ret = qemu_acl_remove(acl, match);
2348 if (ret < 0)
2349 monitor_printf(mon, "acl: no matching acl entry\n");
2350 else
2351 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002352 }
2353}
2354
Huang Ying79c4f6b2009-06-23 10:05:14 +08002355#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002356static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002357{
2358 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002359 int cpu_index = qdict_get_int(qdict, "cpu_index");
2360 int bank = qdict_get_int(qdict, "bank");
2361 uint64_t status = qdict_get_int(qdict, "status");
2362 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2363 uint64_t addr = qdict_get_int(qdict, "addr");
2364 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002365
2366 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2367 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2368 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2369 break;
2370 }
2371}
2372#endif
2373
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002374static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002375{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002376 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002377 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002378 int fd;
2379
2380 fd = qemu_chr_get_msgfd(mon->chr);
2381 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002382 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002383 return;
2384 }
2385
2386 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002387 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002388 return;
2389 }
2390
2391 fd = dup(fd);
2392 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002393 if (errno == EMFILE)
2394 qemu_error_new(QERR_TOO_MANY_FILES);
2395 else
2396 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002397 return;
2398 }
2399
Blue Swirl72cf2d42009-09-12 07:36:22 +00002400 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002401 if (strcmp(monfd->name, fdname) != 0) {
2402 continue;
2403 }
2404
2405 close(monfd->fd);
2406 monfd->fd = fd;
2407 return;
2408 }
2409
Anthony Liguoric227f092009-10-01 16:12:16 -05002410 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002411 monfd->name = qemu_strdup(fdname);
2412 monfd->fd = fd;
2413
Blue Swirl72cf2d42009-09-12 07:36:22 +00002414 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002415}
2416
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002417static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002418{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002419 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002420 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002421
Blue Swirl72cf2d42009-09-12 07:36:22 +00002422 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002423 if (strcmp(monfd->name, fdname) != 0) {
2424 continue;
2425 }
2426
Blue Swirl72cf2d42009-09-12 07:36:22 +00002427 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002428 close(monfd->fd);
2429 qemu_free(monfd->name);
2430 qemu_free(monfd);
2431 return;
2432 }
2433
Markus Armbruster063c1a02009-12-07 21:37:11 +01002434 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002435}
2436
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002437static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002438{
2439 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002440 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002441
2442 vm_stop(0);
2443
Juan Quintela05f24012009-08-20 19:42:22 +02002444 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002445 vm_start();
2446}
2447
Mark McLoughlin7768e042009-07-22 09:11:41 +01002448int monitor_get_fd(Monitor *mon, const char *fdname)
2449{
Anthony Liguoric227f092009-10-01 16:12:16 -05002450 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002451
Blue Swirl72cf2d42009-09-12 07:36:22 +00002452 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002453 int fd;
2454
2455 if (strcmp(monfd->name, fdname) != 0) {
2456 continue;
2457 }
2458
2459 fd = monfd->fd;
2460
2461 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002462 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002463 qemu_free(monfd->name);
2464 qemu_free(monfd);
2465
2466 return fd;
2467 }
2468
2469 return -1;
2470}
2471
Anthony Liguoric227f092009-10-01 16:12:16 -05002472static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002473#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002474 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002475};
2476
Blue Swirl23130862009-06-06 08:22:04 +00002477/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002478static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002479 {
2480 .name = "version",
2481 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002482 .params = "",
2483 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002484 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002485 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002486 },
2487 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002488 .name = "commands",
2489 .args_type = "",
2490 .params = "",
2491 .help = "list QMP available commands",
2492 .user_print = monitor_user_noop,
2493 .mhandler.info_new = do_info_commands,
2494 },
2495 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002496 .name = "network",
2497 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002498 .params = "",
2499 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002500 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002501 },
2502 {
2503 .name = "chardev",
2504 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002505 .params = "",
2506 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002507 .user_print = qemu_chr_info_print,
2508 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002509 },
2510 {
2511 .name = "block",
2512 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002513 .params = "",
2514 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002515 .user_print = bdrv_info_print,
2516 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002517 },
2518 {
2519 .name = "blockstats",
2520 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 .params = "",
2522 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002523 .user_print = bdrv_stats_print,
2524 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002525 },
2526 {
2527 .name = "registers",
2528 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 .params = "",
2530 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002531 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
2534 .name = "cpus",
2535 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 .params = "",
2537 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002538 .user_print = monitor_print_cpus,
2539 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
2541 {
2542 .name = "history",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002546 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 },
2548 {
2549 .name = "irq",
2550 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 .params = "",
2552 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002553 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 },
2555 {
2556 .name = "pic",
2557 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 .params = "",
2559 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002560 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 },
2562 {
2563 .name = "pci",
2564 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 .params = "",
2566 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002567 .user_print = do_pci_info_print,
2568 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002569 },
aurel327c664e22009-03-03 06:12:22 +00002570#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002571 {
2572 .name = "tlb",
2573 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002574 .params = "",
2575 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002576 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002577 },
aurel327c664e22009-03-03 06:12:22 +00002578#endif
2579#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002580 {
2581 .name = "mem",
2582 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002583 .params = "",
2584 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002585 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002586 },
2587 {
2588 .name = "hpet",
2589 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002590 .params = "",
2591 .help = "show state of HPET",
Luiz Capitulino14f07202009-12-10 17:16:02 -02002592 .user_print = do_info_hpet_print,
2593 .mhandler.info_new = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002594 },
bellardb86bda52004-09-18 19:32:46 +00002595#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002596 {
2597 .name = "jit",
2598 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002599 .params = "",
2600 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002601 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002602 },
2603 {
2604 .name = "kvm",
2605 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002606 .params = "",
2607 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002608 .user_print = do_info_kvm_print,
2609 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002610 },
2611 {
2612 .name = "numa",
2613 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002614 .params = "",
2615 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002616 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002617 },
2618 {
2619 .name = "usb",
2620 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002621 .params = "",
2622 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002623 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002624 },
2625 {
2626 .name = "usbhost",
2627 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002628 .params = "",
2629 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002630 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002631 },
2632 {
2633 .name = "profile",
2634 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 .params = "",
2636 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002637 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002638 },
2639 {
2640 .name = "capture",
2641 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 .params = "",
2643 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002644 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002645 },
2646 {
2647 .name = "snapshots",
2648 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002649 .params = "",
2650 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002651 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002652 },
2653 {
2654 .name = "status",
2655 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 .params = "",
2657 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002658 .user_print = do_info_status_print,
2659 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 },
2661 {
2662 .name = "pcmcia",
2663 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002664 .params = "",
2665 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002666 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 },
2668 {
2669 .name = "mice",
2670 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002671 .params = "",
2672 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002673 .user_print = do_info_mice_print,
2674 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002675 },
2676 {
2677 .name = "vnc",
2678 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002679 .params = "",
2680 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002681 .user_print = do_info_vnc_print,
2682 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002683 },
2684 {
2685 .name = "name",
2686 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 .params = "",
2688 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002689 .user_print = do_info_name_print,
2690 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002691 },
2692 {
2693 .name = "uuid",
2694 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 .params = "",
2696 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002697 .user_print = do_info_uuid_print,
2698 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 },
j_mayer76a66252007-03-07 08:32:30 +00002700#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002701 {
2702 .name = "cpustats",
2703 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002704 .params = "",
2705 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002706 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002707 },
j_mayer76a66252007-03-07 08:32:30 +00002708#endif
blueswir131a60e22007-10-26 18:42:59 +00002709#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002710 {
2711 .name = "usernet",
2712 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002713 .params = "",
2714 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002715 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002716 },
blueswir131a60e22007-10-26 18:42:59 +00002717#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 {
2719 .name = "migrate",
2720 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002721 .params = "",
2722 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002723 .user_print = do_info_migrate_print,
2724 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002725 },
2726 {
2727 .name = "balloon",
2728 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 .params = "",
2730 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002731 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002732 .mhandler.info_async = do_info_balloon,
2733 .async = 1,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002734 },
2735 {
2736 .name = "qtree",
2737 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 .params = "",
2739 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002740 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002741 },
2742 {
2743 .name = "qdm",
2744 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 .params = "",
2746 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002747 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 },
2749 {
2750 .name = "roms",
2751 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002752 .params = "",
2753 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002754 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002755 },
2756 {
2757 .name = NULL,
2758 },
bellard9dc39cb2004-03-14 21:38:27 +00002759};
2760
bellard9307c4c2004-04-04 12:57:25 +00002761/*******************************************************************/
2762
2763static const char *pch;
2764static jmp_buf expr_env;
2765
bellard92a31b12005-02-10 22:00:52 +00002766#define MD_TLONG 0
2767#define MD_I32 1
2768
bellard9307c4c2004-04-04 12:57:25 +00002769typedef struct MonitorDef {
2770 const char *name;
2771 int offset;
blueswir18662d652008-10-02 18:32:44 +00002772 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002773 int type;
bellard9307c4c2004-04-04 12:57:25 +00002774} MonitorDef;
2775
bellard57206fd2004-04-25 18:54:52 +00002776#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002777static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002778{
bellard6a00d602005-11-21 23:25:50 +00002779 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002780 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002781}
2782#endif
2783
bellarda541f292004-04-12 20:39:29 +00002784#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002785static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002786{
bellard6a00d602005-11-21 23:25:50 +00002787 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002788 unsigned int u;
2789 int i;
2790
2791 u = 0;
2792 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002793 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002794
2795 return u;
2796}
2797
blueswir18662d652008-10-02 18:32:44 +00002798static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002799{
bellard6a00d602005-11-21 23:25:50 +00002800 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002801 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002802}
2803
blueswir18662d652008-10-02 18:32:44 +00002804static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002805{
bellard6a00d602005-11-21 23:25:50 +00002806 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002807 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002808}
bellard9fddaa02004-05-21 12:59:32 +00002809
blueswir18662d652008-10-02 18:32:44 +00002810static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002811{
bellard6a00d602005-11-21 23:25:50 +00002812 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002813 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002814}
2815
blueswir18662d652008-10-02 18:32:44 +00002816static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002817{
bellard6a00d602005-11-21 23:25:50 +00002818 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002819 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002820}
2821
blueswir18662d652008-10-02 18:32:44 +00002822static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002823{
bellard6a00d602005-11-21 23:25:50 +00002824 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002825 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002826}
bellarda541f292004-04-12 20:39:29 +00002827#endif
2828
bellarde95c8d52004-09-30 22:22:08 +00002829#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002830#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002831static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002832{
bellard6a00d602005-11-21 23:25:50 +00002833 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002834 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002835}
bellard7b936c02005-10-30 17:05:13 +00002836#endif
bellarde95c8d52004-09-30 22:22:08 +00002837
blueswir18662d652008-10-02 18:32:44 +00002838static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002839{
bellard6a00d602005-11-21 23:25:50 +00002840 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002841 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002842}
2843#endif
2844
blueswir18662d652008-10-02 18:32:44 +00002845static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002846#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002847
2848#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002849 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002850 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002851 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002852
bellard9307c4c2004-04-04 12:57:25 +00002853 { "eax", offsetof(CPUState, regs[0]) },
2854 { "ecx", offsetof(CPUState, regs[1]) },
2855 { "edx", offsetof(CPUState, regs[2]) },
2856 { "ebx", offsetof(CPUState, regs[3]) },
2857 { "esp|sp", offsetof(CPUState, regs[4]) },
2858 { "ebp|fp", offsetof(CPUState, regs[5]) },
2859 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002860 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002861#ifdef TARGET_X86_64
2862 { "r8", offsetof(CPUState, regs[8]) },
2863 { "r9", offsetof(CPUState, regs[9]) },
2864 { "r10", offsetof(CPUState, regs[10]) },
2865 { "r11", offsetof(CPUState, regs[11]) },
2866 { "r12", offsetof(CPUState, regs[12]) },
2867 { "r13", offsetof(CPUState, regs[13]) },
2868 { "r14", offsetof(CPUState, regs[14]) },
2869 { "r15", offsetof(CPUState, regs[15]) },
2870#endif
bellard9307c4c2004-04-04 12:57:25 +00002871 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002872 { "eip", offsetof(CPUState, eip) },
2873 SEG("cs", R_CS)
2874 SEG("ds", R_DS)
2875 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002876 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002877 SEG("fs", R_FS)
2878 SEG("gs", R_GS)
2879 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002880#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002881 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002882 { "r0", offsetof(CPUState, gpr[0]) },
2883 { "r1", offsetof(CPUState, gpr[1]) },
2884 { "r2", offsetof(CPUState, gpr[2]) },
2885 { "r3", offsetof(CPUState, gpr[3]) },
2886 { "r4", offsetof(CPUState, gpr[4]) },
2887 { "r5", offsetof(CPUState, gpr[5]) },
2888 { "r6", offsetof(CPUState, gpr[6]) },
2889 { "r7", offsetof(CPUState, gpr[7]) },
2890 { "r8", offsetof(CPUState, gpr[8]) },
2891 { "r9", offsetof(CPUState, gpr[9]) },
2892 { "r10", offsetof(CPUState, gpr[10]) },
2893 { "r11", offsetof(CPUState, gpr[11]) },
2894 { "r12", offsetof(CPUState, gpr[12]) },
2895 { "r13", offsetof(CPUState, gpr[13]) },
2896 { "r14", offsetof(CPUState, gpr[14]) },
2897 { "r15", offsetof(CPUState, gpr[15]) },
2898 { "r16", offsetof(CPUState, gpr[16]) },
2899 { "r17", offsetof(CPUState, gpr[17]) },
2900 { "r18", offsetof(CPUState, gpr[18]) },
2901 { "r19", offsetof(CPUState, gpr[19]) },
2902 { "r20", offsetof(CPUState, gpr[20]) },
2903 { "r21", offsetof(CPUState, gpr[21]) },
2904 { "r22", offsetof(CPUState, gpr[22]) },
2905 { "r23", offsetof(CPUState, gpr[23]) },
2906 { "r24", offsetof(CPUState, gpr[24]) },
2907 { "r25", offsetof(CPUState, gpr[25]) },
2908 { "r26", offsetof(CPUState, gpr[26]) },
2909 { "r27", offsetof(CPUState, gpr[27]) },
2910 { "r28", offsetof(CPUState, gpr[28]) },
2911 { "r29", offsetof(CPUState, gpr[29]) },
2912 { "r30", offsetof(CPUState, gpr[30]) },
2913 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002914 /* Floating point registers */
2915 { "f0", offsetof(CPUState, fpr[0]) },
2916 { "f1", offsetof(CPUState, fpr[1]) },
2917 { "f2", offsetof(CPUState, fpr[2]) },
2918 { "f3", offsetof(CPUState, fpr[3]) },
2919 { "f4", offsetof(CPUState, fpr[4]) },
2920 { "f5", offsetof(CPUState, fpr[5]) },
2921 { "f6", offsetof(CPUState, fpr[6]) },
2922 { "f7", offsetof(CPUState, fpr[7]) },
2923 { "f8", offsetof(CPUState, fpr[8]) },
2924 { "f9", offsetof(CPUState, fpr[9]) },
2925 { "f10", offsetof(CPUState, fpr[10]) },
2926 { "f11", offsetof(CPUState, fpr[11]) },
2927 { "f12", offsetof(CPUState, fpr[12]) },
2928 { "f13", offsetof(CPUState, fpr[13]) },
2929 { "f14", offsetof(CPUState, fpr[14]) },
2930 { "f15", offsetof(CPUState, fpr[15]) },
2931 { "f16", offsetof(CPUState, fpr[16]) },
2932 { "f17", offsetof(CPUState, fpr[17]) },
2933 { "f18", offsetof(CPUState, fpr[18]) },
2934 { "f19", offsetof(CPUState, fpr[19]) },
2935 { "f20", offsetof(CPUState, fpr[20]) },
2936 { "f21", offsetof(CPUState, fpr[21]) },
2937 { "f22", offsetof(CPUState, fpr[22]) },
2938 { "f23", offsetof(CPUState, fpr[23]) },
2939 { "f24", offsetof(CPUState, fpr[24]) },
2940 { "f25", offsetof(CPUState, fpr[25]) },
2941 { "f26", offsetof(CPUState, fpr[26]) },
2942 { "f27", offsetof(CPUState, fpr[27]) },
2943 { "f28", offsetof(CPUState, fpr[28]) },
2944 { "f29", offsetof(CPUState, fpr[29]) },
2945 { "f30", offsetof(CPUState, fpr[30]) },
2946 { "f31", offsetof(CPUState, fpr[31]) },
2947 { "fpscr", offsetof(CPUState, fpscr) },
2948 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002949 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002950 { "lr", offsetof(CPUState, lr) },
2951 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002952 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002953 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002954 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002955 { "msr", 0, &monitor_get_msr, },
2956 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002957 { "tbu", 0, &monitor_get_tbu, },
2958 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002959#if defined(TARGET_PPC64)
2960 /* Address space register */
2961 { "asr", offsetof(CPUState, asr) },
2962#endif
2963 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002964 { "sdr1", offsetof(CPUState, sdr1) },
2965 { "sr0", offsetof(CPUState, sr[0]) },
2966 { "sr1", offsetof(CPUState, sr[1]) },
2967 { "sr2", offsetof(CPUState, sr[2]) },
2968 { "sr3", offsetof(CPUState, sr[3]) },
2969 { "sr4", offsetof(CPUState, sr[4]) },
2970 { "sr5", offsetof(CPUState, sr[5]) },
2971 { "sr6", offsetof(CPUState, sr[6]) },
2972 { "sr7", offsetof(CPUState, sr[7]) },
2973 { "sr8", offsetof(CPUState, sr[8]) },
2974 { "sr9", offsetof(CPUState, sr[9]) },
2975 { "sr10", offsetof(CPUState, sr[10]) },
2976 { "sr11", offsetof(CPUState, sr[11]) },
2977 { "sr12", offsetof(CPUState, sr[12]) },
2978 { "sr13", offsetof(CPUState, sr[13]) },
2979 { "sr14", offsetof(CPUState, sr[14]) },
2980 { "sr15", offsetof(CPUState, sr[15]) },
2981 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002982#elif defined(TARGET_SPARC)
2983 { "g0", offsetof(CPUState, gregs[0]) },
2984 { "g1", offsetof(CPUState, gregs[1]) },
2985 { "g2", offsetof(CPUState, gregs[2]) },
2986 { "g3", offsetof(CPUState, gregs[3]) },
2987 { "g4", offsetof(CPUState, gregs[4]) },
2988 { "g5", offsetof(CPUState, gregs[5]) },
2989 { "g6", offsetof(CPUState, gregs[6]) },
2990 { "g7", offsetof(CPUState, gregs[7]) },
2991 { "o0", 0, monitor_get_reg },
2992 { "o1", 1, monitor_get_reg },
2993 { "o2", 2, monitor_get_reg },
2994 { "o3", 3, monitor_get_reg },
2995 { "o4", 4, monitor_get_reg },
2996 { "o5", 5, monitor_get_reg },
2997 { "o6", 6, monitor_get_reg },
2998 { "o7", 7, monitor_get_reg },
2999 { "l0", 8, monitor_get_reg },
3000 { "l1", 9, monitor_get_reg },
3001 { "l2", 10, monitor_get_reg },
3002 { "l3", 11, monitor_get_reg },
3003 { "l4", 12, monitor_get_reg },
3004 { "l5", 13, monitor_get_reg },
3005 { "l6", 14, monitor_get_reg },
3006 { "l7", 15, monitor_get_reg },
3007 { "i0", 16, monitor_get_reg },
3008 { "i1", 17, monitor_get_reg },
3009 { "i2", 18, monitor_get_reg },
3010 { "i3", 19, monitor_get_reg },
3011 { "i4", 20, monitor_get_reg },
3012 { "i5", 21, monitor_get_reg },
3013 { "i6", 22, monitor_get_reg },
3014 { "i7", 23, monitor_get_reg },
3015 { "pc", offsetof(CPUState, pc) },
3016 { "npc", offsetof(CPUState, npc) },
3017 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003018#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003019 { "psr", 0, &monitor_get_psr, },
3020 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003021#endif
bellarde95c8d52004-09-30 22:22:08 +00003022 { "tbr", offsetof(CPUState, tbr) },
3023 { "fsr", offsetof(CPUState, fsr) },
3024 { "f0", offsetof(CPUState, fpr[0]) },
3025 { "f1", offsetof(CPUState, fpr[1]) },
3026 { "f2", offsetof(CPUState, fpr[2]) },
3027 { "f3", offsetof(CPUState, fpr[3]) },
3028 { "f4", offsetof(CPUState, fpr[4]) },
3029 { "f5", offsetof(CPUState, fpr[5]) },
3030 { "f6", offsetof(CPUState, fpr[6]) },
3031 { "f7", offsetof(CPUState, fpr[7]) },
3032 { "f8", offsetof(CPUState, fpr[8]) },
3033 { "f9", offsetof(CPUState, fpr[9]) },
3034 { "f10", offsetof(CPUState, fpr[10]) },
3035 { "f11", offsetof(CPUState, fpr[11]) },
3036 { "f12", offsetof(CPUState, fpr[12]) },
3037 { "f13", offsetof(CPUState, fpr[13]) },
3038 { "f14", offsetof(CPUState, fpr[14]) },
3039 { "f15", offsetof(CPUState, fpr[15]) },
3040 { "f16", offsetof(CPUState, fpr[16]) },
3041 { "f17", offsetof(CPUState, fpr[17]) },
3042 { "f18", offsetof(CPUState, fpr[18]) },
3043 { "f19", offsetof(CPUState, fpr[19]) },
3044 { "f20", offsetof(CPUState, fpr[20]) },
3045 { "f21", offsetof(CPUState, fpr[21]) },
3046 { "f22", offsetof(CPUState, fpr[22]) },
3047 { "f23", offsetof(CPUState, fpr[23]) },
3048 { "f24", offsetof(CPUState, fpr[24]) },
3049 { "f25", offsetof(CPUState, fpr[25]) },
3050 { "f26", offsetof(CPUState, fpr[26]) },
3051 { "f27", offsetof(CPUState, fpr[27]) },
3052 { "f28", offsetof(CPUState, fpr[28]) },
3053 { "f29", offsetof(CPUState, fpr[29]) },
3054 { "f30", offsetof(CPUState, fpr[30]) },
3055 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003056#ifdef TARGET_SPARC64
3057 { "f32", offsetof(CPUState, fpr[32]) },
3058 { "f34", offsetof(CPUState, fpr[34]) },
3059 { "f36", offsetof(CPUState, fpr[36]) },
3060 { "f38", offsetof(CPUState, fpr[38]) },
3061 { "f40", offsetof(CPUState, fpr[40]) },
3062 { "f42", offsetof(CPUState, fpr[42]) },
3063 { "f44", offsetof(CPUState, fpr[44]) },
3064 { "f46", offsetof(CPUState, fpr[46]) },
3065 { "f48", offsetof(CPUState, fpr[48]) },
3066 { "f50", offsetof(CPUState, fpr[50]) },
3067 { "f52", offsetof(CPUState, fpr[52]) },
3068 { "f54", offsetof(CPUState, fpr[54]) },
3069 { "f56", offsetof(CPUState, fpr[56]) },
3070 { "f58", offsetof(CPUState, fpr[58]) },
3071 { "f60", offsetof(CPUState, fpr[60]) },
3072 { "f62", offsetof(CPUState, fpr[62]) },
3073 { "asi", offsetof(CPUState, asi) },
3074 { "pstate", offsetof(CPUState, pstate) },
3075 { "cansave", offsetof(CPUState, cansave) },
3076 { "canrestore", offsetof(CPUState, canrestore) },
3077 { "otherwin", offsetof(CPUState, otherwin) },
3078 { "wstate", offsetof(CPUState, wstate) },
3079 { "cleanwin", offsetof(CPUState, cleanwin) },
3080 { "fprs", offsetof(CPUState, fprs) },
3081#endif
bellard9307c4c2004-04-04 12:57:25 +00003082#endif
3083 { NULL },
3084};
3085
aliguori376253e2009-03-05 23:01:23 +00003086static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003087{
aliguori376253e2009-03-05 23:01:23 +00003088 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003089 longjmp(expr_env, 1);
3090}
3091
Markus Armbruster09b94182010-01-20 13:07:30 +01003092/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003093static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003094{
blueswir18662d652008-10-02 18:32:44 +00003095 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003096 void *ptr;
3097
bellard9307c4c2004-04-04 12:57:25 +00003098 for(md = monitor_defs; md->name != NULL; md++) {
3099 if (compare_cmd(name, md->name)) {
3100 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003101 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003102 } else {
bellard6a00d602005-11-21 23:25:50 +00003103 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003104 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003105 switch(md->type) {
3106 case MD_I32:
3107 *pval = *(int32_t *)ptr;
3108 break;
3109 case MD_TLONG:
3110 *pval = *(target_long *)ptr;
3111 break;
3112 default:
3113 *pval = 0;
3114 break;
3115 }
bellard9307c4c2004-04-04 12:57:25 +00003116 }
3117 return 0;
3118 }
3119 }
3120 return -1;
3121}
3122
3123static void next(void)
3124{
Blue Swirl660f11b2009-07-31 21:16:51 +00003125 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003126 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003127 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003128 pch++;
3129 }
3130}
3131
aliguori376253e2009-03-05 23:01:23 +00003132static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003133
aliguori376253e2009-03-05 23:01:23 +00003134static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003135{
blueswir1c2efc952007-09-25 17:28:42 +00003136 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003137 char *p;
bellard6a00d602005-11-21 23:25:50 +00003138 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003139
3140 switch(*pch) {
3141 case '+':
3142 next();
aliguori376253e2009-03-05 23:01:23 +00003143 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003144 break;
3145 case '-':
3146 next();
aliguori376253e2009-03-05 23:01:23 +00003147 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003148 break;
3149 case '~':
3150 next();
aliguori376253e2009-03-05 23:01:23 +00003151 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003152 break;
3153 case '(':
3154 next();
aliguori376253e2009-03-05 23:01:23 +00003155 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003156 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003157 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003158 }
3159 next();
3160 break;
bellard81d09122004-07-14 17:21:37 +00003161 case '\'':
3162 pch++;
3163 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003164 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003165 n = *pch;
3166 pch++;
3167 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003168 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003169 next();
3170 break;
bellard9307c4c2004-04-04 12:57:25 +00003171 case '$':
3172 {
3173 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003174 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003175
bellard9307c4c2004-04-04 12:57:25 +00003176 pch++;
3177 q = buf;
3178 while ((*pch >= 'a' && *pch <= 'z') ||
3179 (*pch >= 'A' && *pch <= 'Z') ||
3180 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003181 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003182 if ((q - buf) < sizeof(buf) - 1)
3183 *q++ = *pch;
3184 pch++;
3185 }
blueswir1cd390082008-11-16 13:53:32 +00003186 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003187 pch++;
3188 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003189 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003190 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003191 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003192 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003193 }
3194 break;
3195 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003196 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003197 n = 0;
3198 break;
3199 default:
blueswir17743e582007-09-24 18:39:04 +00003200#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003201 n = strtoull(pch, &p, 0);
3202#else
bellard9307c4c2004-04-04 12:57:25 +00003203 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003204#endif
bellard9307c4c2004-04-04 12:57:25 +00003205 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003206 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003207 }
3208 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003209 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003210 pch++;
3211 break;
3212 }
3213 return n;
3214}
3215
3216
aliguori376253e2009-03-05 23:01:23 +00003217static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003218{
blueswir1c2efc952007-09-25 17:28:42 +00003219 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003220 int op;
ths3b46e622007-09-17 08:09:54 +00003221
aliguori376253e2009-03-05 23:01:23 +00003222 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003223 for(;;) {
3224 op = *pch;
3225 if (op != '*' && op != '/' && op != '%')
3226 break;
3227 next();
aliguori376253e2009-03-05 23:01:23 +00003228 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003229 switch(op) {
3230 default:
3231 case '*':
3232 val *= val2;
3233 break;
3234 case '/':
3235 case '%':
ths5fafdf22007-09-16 21:08:06 +00003236 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003237 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003238 if (op == '/')
3239 val /= val2;
3240 else
3241 val %= val2;
3242 break;
3243 }
3244 }
3245 return val;
3246}
3247
aliguori376253e2009-03-05 23:01:23 +00003248static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003249{
blueswir1c2efc952007-09-25 17:28:42 +00003250 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003251 int op;
bellard9307c4c2004-04-04 12:57:25 +00003252
aliguori376253e2009-03-05 23:01:23 +00003253 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003254 for(;;) {
3255 op = *pch;
3256 if (op != '&' && op != '|' && op != '^')
3257 break;
3258 next();
aliguori376253e2009-03-05 23:01:23 +00003259 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003260 switch(op) {
3261 default:
3262 case '&':
3263 val &= val2;
3264 break;
3265 case '|':
3266 val |= val2;
3267 break;
3268 case '^':
3269 val ^= val2;
3270 break;
3271 }
3272 }
3273 return val;
3274}
3275
aliguori376253e2009-03-05 23:01:23 +00003276static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003277{
blueswir1c2efc952007-09-25 17:28:42 +00003278 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003279 int op;
bellard9307c4c2004-04-04 12:57:25 +00003280
aliguori376253e2009-03-05 23:01:23 +00003281 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003282 for(;;) {
3283 op = *pch;
3284 if (op != '+' && op != '-')
3285 break;
3286 next();
aliguori376253e2009-03-05 23:01:23 +00003287 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003288 if (op == '+')
3289 val += val2;
3290 else
3291 val -= val2;
3292 }
3293 return val;
3294}
3295
aliguori376253e2009-03-05 23:01:23 +00003296static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003297{
3298 pch = *pp;
3299 if (setjmp(expr_env)) {
3300 *pp = pch;
3301 return -1;
3302 }
blueswir1cd390082008-11-16 13:53:32 +00003303 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003304 pch++;
aliguori376253e2009-03-05 23:01:23 +00003305 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003306 *pp = pch;
3307 return 0;
3308}
3309
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003310static int get_double(Monitor *mon, double *pval, const char **pp)
3311{
3312 const char *p = *pp;
3313 char *tailp;
3314 double d;
3315
3316 d = strtod(p, &tailp);
3317 if (tailp == p) {
3318 monitor_printf(mon, "Number expected\n");
3319 return -1;
3320 }
3321 if (d != d || d - d != 0) {
3322 /* NaN or infinity */
3323 monitor_printf(mon, "Bad number\n");
3324 return -1;
3325 }
3326 *pval = d;
3327 *pp = tailp;
3328 return 0;
3329}
3330
bellard9307c4c2004-04-04 12:57:25 +00003331static int get_str(char *buf, int buf_size, const char **pp)
3332{
3333 const char *p;
3334 char *q;
3335 int c;
3336
bellard81d09122004-07-14 17:21:37 +00003337 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003338 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003339 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003340 p++;
3341 if (*p == '\0') {
3342 fail:
bellard81d09122004-07-14 17:21:37 +00003343 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003344 *pp = p;
3345 return -1;
3346 }
bellard9307c4c2004-04-04 12:57:25 +00003347 if (*p == '\"') {
3348 p++;
3349 while (*p != '\0' && *p != '\"') {
3350 if (*p == '\\') {
3351 p++;
3352 c = *p++;
3353 switch(c) {
3354 case 'n':
3355 c = '\n';
3356 break;
3357 case 'r':
3358 c = '\r';
3359 break;
3360 case '\\':
3361 case '\'':
3362 case '\"':
3363 break;
3364 default:
3365 qemu_printf("unsupported escape code: '\\%c'\n", c);
3366 goto fail;
3367 }
3368 if ((q - buf) < buf_size - 1) {
3369 *q++ = c;
3370 }
3371 } else {
3372 if ((q - buf) < buf_size - 1) {
3373 *q++ = *p;
3374 }
3375 p++;
3376 }
3377 }
3378 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003379 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003380 goto fail;
3381 }
3382 p++;
3383 } else {
blueswir1cd390082008-11-16 13:53:32 +00003384 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003385 if ((q - buf) < buf_size - 1) {
3386 *q++ = *p;
3387 }
3388 p++;
3389 }
bellard9307c4c2004-04-04 12:57:25 +00003390 }
bellard81d09122004-07-14 17:21:37 +00003391 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003392 *pp = p;
3393 return 0;
3394}
3395
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003396/*
3397 * Store the command-name in cmdname, and return a pointer to
3398 * the remaining of the command string.
3399 */
3400static const char *get_command_name(const char *cmdline,
3401 char *cmdname, size_t nlen)
3402{
3403 size_t len;
3404 const char *p, *pstart;
3405
3406 p = cmdline;
3407 while (qemu_isspace(*p))
3408 p++;
3409 if (*p == '\0')
3410 return NULL;
3411 pstart = p;
3412 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3413 p++;
3414 len = p - pstart;
3415 if (len > nlen - 1)
3416 len = nlen - 1;
3417 memcpy(cmdname, pstart, len);
3418 cmdname[len] = '\0';
3419 return p;
3420}
3421
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003422/**
3423 * Read key of 'type' into 'key' and return the current
3424 * 'type' pointer.
3425 */
3426static char *key_get_info(const char *type, char **key)
3427{
3428 size_t len;
3429 char *p, *str;
3430
3431 if (*type == ',')
3432 type++;
3433
3434 p = strchr(type, ':');
3435 if (!p) {
3436 *key = NULL;
3437 return NULL;
3438 }
3439 len = p - type;
3440
3441 str = qemu_malloc(len + 1);
3442 memcpy(str, type, len);
3443 str[len] = '\0';
3444
3445 *key = str;
3446 return ++p;
3447}
3448
bellard9307c4c2004-04-04 12:57:25 +00003449static int default_fmt_format = 'x';
3450static int default_fmt_size = 4;
3451
3452#define MAX_ARGS 16
3453
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003454static int is_valid_option(const char *c, const char *typestr)
3455{
3456 char option[3];
3457
3458 option[0] = '-';
3459 option[1] = *c;
3460 option[2] = '\0';
3461
3462 typestr = strstr(typestr, option);
3463 return (typestr != NULL);
3464}
3465
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003466static const mon_cmd_t *monitor_find_command(const char *cmdname)
3467{
3468 const mon_cmd_t *cmd;
3469
3470 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3471 if (compare_cmd(cmdname, cmd->name)) {
3472 return cmd;
3473 }
3474 }
3475
3476 return NULL;
3477}
3478
Anthony Liguoric227f092009-10-01 16:12:16 -05003479static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003480 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003481 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003482{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003483 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003484 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003485 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003486 char cmdname[256];
3487 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003488 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003489
3490#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003491 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003492#endif
ths3b46e622007-09-17 08:09:54 +00003493
bellard9307c4c2004-04-04 12:57:25 +00003494 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003495 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3496 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003497 return NULL;
ths3b46e622007-09-17 08:09:54 +00003498
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003499 cmd = monitor_find_command(cmdname);
3500 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003501 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003502 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003503 }
bellard9307c4c2004-04-04 12:57:25 +00003504
bellard9307c4c2004-04-04 12:57:25 +00003505 /* parse the parameters */
3506 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003507 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003508 typestr = key_get_info(typestr, &key);
3509 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003510 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003511 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003512 typestr++;
3513 switch(c) {
3514 case 'F':
bellard81d09122004-07-14 17:21:37 +00003515 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003516 case 's':
3517 {
3518 int ret;
ths3b46e622007-09-17 08:09:54 +00003519
blueswir1cd390082008-11-16 13:53:32 +00003520 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003521 p++;
3522 if (*typestr == '?') {
3523 typestr++;
3524 if (*p == '\0') {
3525 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003526 break;
bellard9307c4c2004-04-04 12:57:25 +00003527 }
3528 }
3529 ret = get_str(buf, sizeof(buf), &p);
3530 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003531 switch(c) {
3532 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003533 monitor_printf(mon, "%s: filename expected\n",
3534 cmdname);
bellard81d09122004-07-14 17:21:37 +00003535 break;
3536 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003537 monitor_printf(mon, "%s: block device name expected\n",
3538 cmdname);
bellard81d09122004-07-14 17:21:37 +00003539 break;
3540 default:
aliguori376253e2009-03-05 23:01:23 +00003541 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003542 break;
3543 }
bellard9307c4c2004-04-04 12:57:25 +00003544 goto fail;
3545 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003546 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003547 }
3548 break;
3549 case '/':
3550 {
3551 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003552
blueswir1cd390082008-11-16 13:53:32 +00003553 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003554 p++;
3555 if (*p == '/') {
3556 /* format found */
3557 p++;
3558 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003559 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003560 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003561 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003562 count = count * 10 + (*p - '0');
3563 p++;
3564 }
3565 }
3566 size = -1;
3567 format = -1;
3568 for(;;) {
3569 switch(*p) {
3570 case 'o':
3571 case 'd':
3572 case 'u':
3573 case 'x':
3574 case 'i':
3575 case 'c':
3576 format = *p++;
3577 break;
3578 case 'b':
3579 size = 1;
3580 p++;
3581 break;
3582 case 'h':
3583 size = 2;
3584 p++;
3585 break;
3586 case 'w':
3587 size = 4;
3588 p++;
3589 break;
3590 case 'g':
3591 case 'L':
3592 size = 8;
3593 p++;
3594 break;
3595 default:
3596 goto next;
3597 }
3598 }
3599 next:
blueswir1cd390082008-11-16 13:53:32 +00003600 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003601 monitor_printf(mon, "invalid char in format: '%c'\n",
3602 *p);
bellard9307c4c2004-04-04 12:57:25 +00003603 goto fail;
3604 }
bellard9307c4c2004-04-04 12:57:25 +00003605 if (format < 0)
3606 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003607 if (format != 'i') {
3608 /* for 'i', not specifying a size gives -1 as size */
3609 if (size < 0)
3610 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003611 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003612 }
bellard9307c4c2004-04-04 12:57:25 +00003613 default_fmt_format = format;
3614 } else {
3615 count = 1;
3616 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003617 if (format != 'i') {
3618 size = default_fmt_size;
3619 } else {
3620 size = -1;
3621 }
bellard9307c4c2004-04-04 12:57:25 +00003622 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003623 qdict_put(qdict, "count", qint_from_int(count));
3624 qdict_put(qdict, "format", qint_from_int(format));
3625 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003626 }
3627 break;
3628 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003629 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003630 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003631 {
blueswir1c2efc952007-09-25 17:28:42 +00003632 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003633
blueswir1cd390082008-11-16 13:53:32 +00003634 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003635 p++;
bellard34405572004-06-08 00:55:58 +00003636 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003637 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003638 if (*p == '\0') {
3639 typestr++;
3640 break;
3641 }
bellard34405572004-06-08 00:55:58 +00003642 } else {
3643 if (*p == '.') {
3644 p++;
blueswir1cd390082008-11-16 13:53:32 +00003645 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003646 p++;
bellard34405572004-06-08 00:55:58 +00003647 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003648 typestr++;
3649 break;
bellard34405572004-06-08 00:55:58 +00003650 }
3651 }
bellard13224a82006-07-14 22:03:35 +00003652 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003653 }
aliguori376253e2009-03-05 23:01:23 +00003654 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003655 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003656 /* Check if 'i' is greater than 32-bit */
3657 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3658 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3659 monitor_printf(mon, "integer is for 32-bit values\n");
3660 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003661 } else if (c == 'M') {
3662 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003663 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003664 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003665 }
3666 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003667 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003668 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003669 {
3670 double val;
3671
3672 while (qemu_isspace(*p))
3673 p++;
3674 if (*typestr == '?') {
3675 typestr++;
3676 if (*p == '\0') {
3677 break;
3678 }
3679 }
3680 if (get_double(mon, &val, &p) < 0) {
3681 goto fail;
3682 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003683 if (c == 'b' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003684 switch (*p) {
3685 case 'K': case 'k':
3686 val *= 1 << 10; p++; break;
3687 case 'M': case 'm':
3688 val *= 1 << 20; p++; break;
3689 case 'G': case 'g':
3690 val *= 1 << 30; p++; break;
3691 }
3692 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003693 if (c == 'T' && p[0] && p[1] == 's') {
3694 switch (*p) {
3695 case 'm':
3696 val /= 1e3; p += 2; break;
3697 case 'u':
3698 val /= 1e6; p += 2; break;
3699 case 'n':
3700 val /= 1e9; p += 2; break;
3701 }
3702 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003703 if (*p && !qemu_isspace(*p)) {
3704 monitor_printf(mon, "Unknown unit suffix\n");
3705 goto fail;
3706 }
3707 qdict_put(qdict, key, qfloat_from_double(val));
3708 }
3709 break;
bellard9307c4c2004-04-04 12:57:25 +00003710 case '-':
3711 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003712 const char *tmp = p;
3713 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003714 /* option */
ths3b46e622007-09-17 08:09:54 +00003715
bellard9307c4c2004-04-04 12:57:25 +00003716 c = *typestr++;
3717 if (c == '\0')
3718 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003719 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003720 p++;
3721 has_option = 0;
3722 if (*p == '-') {
3723 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003724 if(c != *p) {
3725 if(!is_valid_option(p, typestr)) {
3726
3727 monitor_printf(mon, "%s: unsupported option -%c\n",
3728 cmdname, *p);
3729 goto fail;
3730 } else {
3731 skip_key = 1;
3732 }
bellard9307c4c2004-04-04 12:57:25 +00003733 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003734 if(skip_key) {
3735 p = tmp;
3736 } else {
3737 p++;
3738 has_option = 1;
3739 }
bellard9307c4c2004-04-04 12:57:25 +00003740 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003741 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003742 }
3743 break;
3744 default:
3745 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003746 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003747 goto fail;
3748 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003749 qemu_free(key);
3750 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003751 }
3752 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003753 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003754 p++;
3755 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003756 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3757 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003758 goto fail;
3759 }
3760
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003761 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003762
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003763fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003764 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003765 return NULL;
3766}
3767
Luiz Capitulino8204a912009-11-18 23:05:31 -02003768static void monitor_print_error(Monitor *mon)
3769{
3770 qerror_print(mon->error);
3771 QDECREF(mon->error);
3772 mon->error = NULL;
3773}
3774
Adam Litke940cc302010-01-25 12:18:44 -06003775static int is_async_return(const QObject *data)
3776{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003777 if (data && qobject_type(data) == QTYPE_QDICT) {
3778 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3779 }
3780
3781 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003782}
3783
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003784static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3785 const QDict *params)
3786{
3787 QObject *data = NULL;
3788
3789 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003790
Adam Litke940cc302010-01-25 12:18:44 -06003791 if (is_async_return(data)) {
3792 /*
3793 * Asynchronous commands have no initial return data but they can
3794 * generate errors. Data is returned via the async completion handler.
3795 */
3796 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3797 monitor_protocol_emitter(mon, NULL);
3798 }
3799 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003800 /* Monitor Protocol */
3801 monitor_protocol_emitter(mon, data);
3802 } else {
3803 /* User Protocol */
3804 if (data)
3805 cmd->user_print(mon, data);
3806 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003807
3808 qobject_decref(data);
3809}
3810
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003811static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003812{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003813 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003814 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003815
3816 qdict = qdict_new();
3817
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003818 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003819 if (!cmd)
3820 goto out;
3821
3822 qemu_errors_to_mon(mon);
3823
Adam Litke940cc302010-01-25 12:18:44 -06003824 if (monitor_handler_is_async(cmd)) {
3825 user_async_cmd_handler(mon, cmd, qdict);
3826 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003827 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003828 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003829 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003830 }
3831
Luiz Capitulino8204a912009-11-18 23:05:31 -02003832 if (monitor_has_error(mon))
3833 monitor_print_error(mon);
3834
3835 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003836
3837out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003838 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003839}
3840
bellard81d09122004-07-14 17:21:37 +00003841static void cmd_completion(const char *name, const char *list)
3842{
3843 const char *p, *pstart;
3844 char cmd[128];
3845 int len;
3846
3847 p = list;
3848 for(;;) {
3849 pstart = p;
3850 p = strchr(p, '|');
3851 if (!p)
3852 p = pstart + strlen(pstart);
3853 len = p - pstart;
3854 if (len > sizeof(cmd) - 2)
3855 len = sizeof(cmd) - 2;
3856 memcpy(cmd, pstart, len);
3857 cmd[len] = '\0';
3858 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003859 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003860 }
3861 if (*p == '\0')
3862 break;
3863 p++;
3864 }
3865}
3866
3867static void file_completion(const char *input)
3868{
3869 DIR *ffs;
3870 struct dirent *d;
3871 char path[1024];
3872 char file[1024], file_prefix[1024];
3873 int input_path_len;
3874 const char *p;
3875
ths5fafdf22007-09-16 21:08:06 +00003876 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003877 if (!p) {
3878 input_path_len = 0;
3879 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003880 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003881 } else {
3882 input_path_len = p - input + 1;
3883 memcpy(path, input, input_path_len);
3884 if (input_path_len > sizeof(path) - 1)
3885 input_path_len = sizeof(path) - 1;
3886 path[input_path_len] = '\0';
3887 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3888 }
3889#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003890 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3891 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003892#endif
3893 ffs = opendir(path);
3894 if (!ffs)
3895 return;
3896 for(;;) {
3897 struct stat sb;
3898 d = readdir(ffs);
3899 if (!d)
3900 break;
3901 if (strstart(d->d_name, file_prefix, NULL)) {
3902 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003903 if (input_path_len < sizeof(file))
3904 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3905 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003906 /* stat the file to find out if it's a directory.
3907 * In that case add a slash to speed up typing long paths
3908 */
3909 stat(file, &sb);
3910 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003911 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003912 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003913 }
3914 }
3915 closedir(ffs);
3916}
3917
aliguori51de9762009-03-05 23:00:43 +00003918static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003919{
aliguori51de9762009-03-05 23:00:43 +00003920 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003921 const char *input = opaque;
3922
3923 if (input[0] == '\0' ||
3924 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003925 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003926 }
3927}
3928
3929/* NOTE: this parser is an approximate form of the real command parser */
3930static void parse_cmdline(const char *cmdline,
3931 int *pnb_args, char **args)
3932{
3933 const char *p;
3934 int nb_args, ret;
3935 char buf[1024];
3936
3937 p = cmdline;
3938 nb_args = 0;
3939 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003940 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003941 p++;
3942 if (*p == '\0')
3943 break;
3944 if (nb_args >= MAX_ARGS)
3945 break;
3946 ret = get_str(buf, sizeof(buf), &p);
3947 args[nb_args] = qemu_strdup(buf);
3948 nb_args++;
3949 if (ret < 0)
3950 break;
3951 }
3952 *pnb_args = nb_args;
3953}
3954
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003955static const char *next_arg_type(const char *typestr)
3956{
3957 const char *p = strchr(typestr, ':');
3958 return (p != NULL ? ++p : typestr);
3959}
3960
aliguori4c36ba32009-03-05 23:01:37 +00003961static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003962{
3963 const char *cmdname;
3964 char *args[MAX_ARGS];
3965 int nb_args, i, len;
3966 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003967 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003968 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003969
3970 parse_cmdline(cmdline, &nb_args, args);
3971#ifdef DEBUG_COMPLETION
3972 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003973 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003974 }
3975#endif
3976
3977 /* if the line ends with a space, it means we want to complete the
3978 next arg */
3979 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003980 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003981 if (nb_args >= MAX_ARGS)
3982 return;
3983 args[nb_args++] = qemu_strdup("");
3984 }
3985 if (nb_args <= 1) {
3986 /* command completion */
3987 if (nb_args == 0)
3988 cmdname = "";
3989 else
3990 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003991 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003992 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003993 cmd_completion(cmdname, cmd->name);
3994 }
3995 } else {
3996 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003997 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003998 if (compare_cmd(args[0], cmd->name))
3999 goto found;
4000 }
4001 return;
4002 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004003 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004004 for(i = 0; i < nb_args - 2; i++) {
4005 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004006 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004007 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004008 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004009 }
4010 }
4011 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004012 if (*ptype == '-' && ptype[1] != '\0') {
4013 ptype += 2;
4014 }
bellard81d09122004-07-14 17:21:37 +00004015 switch(*ptype) {
4016 case 'F':
4017 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004018 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004019 file_completion(str);
4020 break;
4021 case 'B':
4022 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004023 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004024 bdrv_iterate(block_completion_it, (void *)str);
4025 break;
bellard7fe48482004-10-09 18:08:01 +00004026 case 's':
4027 /* XXX: more generic ? */
4028 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004029 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004030 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4031 cmd_completion(str, cmd->name);
4032 }
bellard64866c32006-05-07 18:03:31 +00004033 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004034 char *sep = strrchr(str, '-');
4035 if (sep)
4036 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004037 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004038 for(key = key_defs; key->name != NULL; key++) {
4039 cmd_completion(str, key->name);
4040 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004041 } else if (!strcmp(cmd->name, "help|?")) {
4042 readline_set_completion_index(cur_mon->rs, strlen(str));
4043 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4044 cmd_completion(str, cmd->name);
4045 }
bellard7fe48482004-10-09 18:08:01 +00004046 }
4047 break;
bellard81d09122004-07-14 17:21:37 +00004048 default:
4049 break;
4050 }
4051 }
4052 for(i = 0; i < nb_args; i++)
4053 qemu_free(args[i]);
4054}
4055
aliguori731b0362009-03-05 23:01:42 +00004056static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004057{
aliguori731b0362009-03-05 23:01:42 +00004058 Monitor *mon = opaque;
4059
Jan Kiszkac62313b2009-12-04 14:05:29 +01004060 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004061}
4062
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004063typedef struct CmdArgs {
4064 QString *name;
4065 int type;
4066 int flag;
4067 int optional;
4068} CmdArgs;
4069
4070static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
4071{
4072 if (!cmd_args->optional) {
4073 qemu_error_new(QERR_MISSING_PARAMETER, name);
4074 return -1;
4075 }
4076
4077 if (cmd_args->type == '-') {
4078 /* handlers expect a value, they need to be changed */
4079 qdict_put(args, name, qint_from_int(0));
4080 }
4081
4082 return 0;
4083}
4084
4085static int check_arg(const CmdArgs *cmd_args, QDict *args)
4086{
4087 QObject *value;
4088 const char *name;
4089
4090 name = qstring_get_str(cmd_args->name);
4091
4092 if (!args) {
4093 return check_opt(cmd_args, name, args);
4094 }
4095
4096 value = qdict_get(args, name);
4097 if (!value) {
4098 return check_opt(cmd_args, name, args);
4099 }
4100
4101 switch (cmd_args->type) {
4102 case 'F':
4103 case 'B':
4104 case 's':
4105 if (qobject_type(value) != QTYPE_QSTRING) {
4106 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
4107 return -1;
4108 }
4109 break;
4110 case '/': {
4111 int i;
4112 const char *keys[] = { "count", "format", "size", NULL };
4113
4114 for (i = 0; keys[i]; i++) {
4115 QObject *obj = qdict_get(args, keys[i]);
4116 if (!obj) {
4117 qemu_error_new(QERR_MISSING_PARAMETER, name);
4118 return -1;
4119 }
4120 if (qobject_type(obj) != QTYPE_QINT) {
4121 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4122 return -1;
4123 }
4124 }
4125 break;
4126 }
4127 case 'i':
4128 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02004129 case 'M':
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004130 if (qobject_type(value) != QTYPE_QINT) {
4131 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
4132 return -1;
4133 }
4134 break;
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004135 case 'b':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004136 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004137 if (qobject_type(value) != QTYPE_QINT && qobject_type(value) != QTYPE_QFLOAT) {
4138 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "number");
4139 return -1;
4140 }
4141 break;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004142 case '-':
4143 if (qobject_type(value) != QTYPE_QINT &&
4144 qobject_type(value) != QTYPE_QBOOL) {
4145 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
4146 return -1;
4147 }
4148 if (qobject_type(value) == QTYPE_QBOOL) {
4149 /* handlers expect a QInt, they need to be changed */
4150 qdict_put(args, name,
4151 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
4152 }
4153 break;
4154 default:
4155 /* impossible */
4156 abort();
4157 }
4158
4159 return 0;
4160}
4161
4162static void cmd_args_init(CmdArgs *cmd_args)
4163{
4164 cmd_args->name = qstring_new();
4165 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
4166}
4167
4168/*
4169 * This is not trivial, we have to parse Monitor command's argument
4170 * type syntax to be able to check the arguments provided by clients.
4171 *
4172 * In the near future we will be using an array for that and will be
4173 * able to drop all this parsing...
4174 */
4175static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
4176{
4177 int err;
4178 const char *p;
4179 CmdArgs cmd_args;
4180
Blue Swirldd5121b2009-12-04 20:52:02 +00004181 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004182 return (qdict_size(args) == 0 ? 0 : -1);
4183 }
4184
4185 err = 0;
4186 cmd_args_init(&cmd_args);
4187
4188 for (p = cmd->args_type;; p++) {
4189 if (*p == ':') {
4190 cmd_args.type = *++p;
4191 p++;
4192 if (cmd_args.type == '-') {
4193 cmd_args.flag = *p++;
4194 cmd_args.optional = 1;
4195 } else if (*p == '?') {
4196 cmd_args.optional = 1;
4197 p++;
4198 }
4199
4200 assert(*p == ',' || *p == '\0');
4201 err = check_arg(&cmd_args, args);
4202
4203 QDECREF(cmd_args.name);
4204 cmd_args_init(&cmd_args);
4205
4206 if (err < 0) {
4207 break;
4208 }
4209 } else {
4210 qstring_append_chr(cmd_args.name, *p);
4211 }
4212
4213 if (*p == '\0') {
4214 break;
4215 }
4216 }
4217
4218 QDECREF(cmd_args.name);
4219 return err;
4220}
4221
4222static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4223{
4224 int err;
4225 QObject *obj;
4226 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004227 const mon_cmd_t *cmd;
4228 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004229 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004230
4231 args = NULL;
4232 qemu_errors_to_mon(mon);
4233
4234 obj = json_parser_parse(tokens, NULL);
4235 if (!obj) {
4236 // FIXME: should be triggered in json_parser_parse()
4237 qemu_error_new(QERR_JSON_PARSING);
4238 goto err_out;
4239 } else if (qobject_type(obj) != QTYPE_QDICT) {
4240 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
4241 qobject_decref(obj);
4242 goto err_out;
4243 }
4244
4245 input = qobject_to_qdict(obj);
4246
4247 mon->mc->id = qdict_get(input, "id");
4248 qobject_incref(mon->mc->id);
4249
4250 obj = qdict_get(input, "execute");
4251 if (!obj) {
4252 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4253 goto err_input;
4254 } else if (qobject_type(obj) != QTYPE_QSTRING) {
4255 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
4256 goto err_input;
4257 }
4258
4259 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004260
4261 /*
4262 * XXX: We need this special case until we get info handlers
4263 * converted into 'query-' commands
4264 */
4265 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004266 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4267 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004268 } else if (strstart(cmd_name, "query-", &info_item)) {
4269 cmd = monitor_find_command("info");
4270 qdict_put_obj(input, "arguments",
4271 qobject_from_jsonf("{ 'item': %s }", info_item));
4272 } else {
4273 cmd = monitor_find_command(cmd_name);
Luiz Capitulino43e713c2009-12-18 13:24:59 -02004274 if (!cmd || !monitor_handler_ported(cmd)) {
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004275 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4276 goto err_input;
4277 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004278 }
4279
4280 obj = qdict_get(input, "arguments");
4281 if (!obj) {
4282 args = qdict_new();
4283 } else {
4284 args = qobject_to_qdict(obj);
4285 QINCREF(args);
4286 }
4287
4288 QDECREF(input);
4289
4290 err = monitor_check_qmp_args(cmd, args);
4291 if (err < 0) {
4292 goto err_out;
4293 }
4294
Adam Litke940cc302010-01-25 12:18:44 -06004295 if (monitor_handler_is_async(cmd)) {
4296 qmp_async_cmd_handler(mon, cmd, args);
4297 } else {
4298 monitor_call_handler(mon, cmd, args);
4299 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004300 goto out;
4301
4302err_input:
4303 QDECREF(input);
4304err_out:
4305 monitor_protocol_emitter(mon, NULL);
4306out:
4307 QDECREF(args);
4308 qemu_errors_to_previous();
4309}
4310
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004311/**
4312 * monitor_control_read(): Read and handle QMP input
4313 */
4314static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4315{
4316 Monitor *old_mon = cur_mon;
4317
4318 cur_mon = opaque;
4319
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004320 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004321
4322 cur_mon = old_mon;
4323}
4324
aliguori731b0362009-03-05 23:01:42 +00004325static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004326{
aliguori731b0362009-03-05 23:01:42 +00004327 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004328 int i;
aliguori376253e2009-03-05 23:01:23 +00004329
aliguori731b0362009-03-05 23:01:42 +00004330 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004331
aliguoricde76ee2009-03-05 23:01:51 +00004332 if (cur_mon->rs) {
4333 for (i = 0; i < size; i++)
4334 readline_handle_byte(cur_mon->rs, buf[i]);
4335 } else {
4336 if (size == 0 || buf[size - 1] != 0)
4337 monitor_printf(cur_mon, "corrupted command\n");
4338 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004339 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004340 }
aliguori731b0362009-03-05 23:01:42 +00004341
4342 cur_mon = old_mon;
4343}
aliguorid8f44602008-10-06 13:52:44 +00004344
aliguori376253e2009-03-05 23:01:23 +00004345static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004346{
aliguori731b0362009-03-05 23:01:42 +00004347 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004348 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004349 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004350}
4351
aliguoricde76ee2009-03-05 23:01:51 +00004352int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004353{
aliguoricde76ee2009-03-05 23:01:51 +00004354 if (!mon->rs)
4355 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004356 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004357 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004358}
4359
aliguori376253e2009-03-05 23:01:23 +00004360void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004361{
aliguoricde76ee2009-03-05 23:01:51 +00004362 if (!mon->rs)
4363 return;
aliguori731b0362009-03-05 23:01:42 +00004364 if (--mon->suspend_cnt == 0)
4365 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004366}
4367
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004368/**
4369 * monitor_control_event(): Print QMP gretting
4370 */
4371static void monitor_control_event(void *opaque, int event)
4372{
4373 if (event == CHR_EVENT_OPENED) {
4374 QObject *data;
4375 Monitor *mon = opaque;
4376
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004377 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4378
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004379 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
4380 assert(data != NULL);
4381
4382 monitor_json_emitter(mon, data);
4383 qobject_decref(data);
4384 }
4385}
4386
aliguori731b0362009-03-05 23:01:42 +00004387static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004388{
aliguori376253e2009-03-05 23:01:23 +00004389 Monitor *mon = opaque;
4390
aliguori2724b182009-03-05 23:01:47 +00004391 switch (event) {
4392 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004393 mon->mux_out = 0;
4394 if (mon->reset_seen) {
4395 readline_restart(mon->rs);
4396 monitor_resume(mon);
4397 monitor_flush(mon);
4398 } else {
4399 mon->suspend_cnt = 0;
4400 }
aliguori2724b182009-03-05 23:01:47 +00004401 break;
ths86e94de2007-01-05 22:01:59 +00004402
aliguori2724b182009-03-05 23:01:47 +00004403 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004404 if (mon->reset_seen) {
4405 if (mon->suspend_cnt == 0) {
4406 monitor_printf(mon, "\n");
4407 }
4408 monitor_flush(mon);
4409 monitor_suspend(mon);
4410 } else {
4411 mon->suspend_cnt++;
4412 }
4413 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004414 break;
4415
Amit Shahb6b8df52009-10-07 18:31:16 +05304416 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004417 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4418 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004419 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004420 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004421 }
4422 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004423 break;
4424 }
ths86e94de2007-01-05 22:01:59 +00004425}
4426
aliguori76655d62009-03-06 20:27:37 +00004427
4428/*
4429 * Local variables:
4430 * c-indent-level: 4
4431 * c-basic-offset: 4
4432 * tab-width: 8
4433 * End:
4434 */
4435
aliguori731b0362009-03-05 23:01:42 +00004436void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004437{
aliguori731b0362009-03-05 23:01:42 +00004438 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004439 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004440
4441 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004442 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004443 is_first_init = 0;
4444 }
aliguori87127162009-03-05 23:01:29 +00004445
4446 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004447
aliguori87127162009-03-05 23:01:29 +00004448 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004449 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004450 if (flags & MONITOR_USE_READLINE) {
4451 mon->rs = readline_init(mon, monitor_find_completion);
4452 monitor_read_command(mon, 0);
4453 }
aliguori87127162009-03-05 23:01:29 +00004454
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004455 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004456 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004457 /* Control mode requires special handlers */
4458 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4459 monitor_control_event, mon);
4460 } else {
4461 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4462 monitor_event, mon);
4463 }
aliguori87127162009-03-05 23:01:29 +00004464
Blue Swirl72cf2d42009-09-12 07:36:22 +00004465 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004466 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004467 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004468}
4469
aliguori376253e2009-03-05 23:01:23 +00004470static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004471{
aliguoribb5fc202009-03-05 23:01:15 +00004472 BlockDriverState *bs = opaque;
4473 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004474
aliguoribb5fc202009-03-05 23:01:15 +00004475 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004476 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004477 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004478 }
aliguori731b0362009-03-05 23:01:42 +00004479 if (mon->password_completion_cb)
4480 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004481
aliguori731b0362009-03-05 23:01:42 +00004482 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004483}
aliguoric0f4ce72009-03-05 23:01:01 +00004484
aliguori376253e2009-03-05 23:01:23 +00004485void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004486 BlockDriverCompletionFunc *completion_cb,
4487 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004488{
aliguoricde76ee2009-03-05 23:01:51 +00004489 int err;
4490
aliguoribb5fc202009-03-05 23:01:15 +00004491 if (!bdrv_key_required(bs)) {
4492 if (completion_cb)
4493 completion_cb(opaque, 0);
4494 return;
4495 }
aliguoric0f4ce72009-03-05 23:01:01 +00004496
Luiz Capitulino94171e12009-12-07 21:37:00 +01004497 if (monitor_ctrl_mode(mon)) {
4498 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4499 return;
4500 }
4501
aliguori376253e2009-03-05 23:01:23 +00004502 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4503 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004504
aliguori731b0362009-03-05 23:01:42 +00004505 mon->password_completion_cb = completion_cb;
4506 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004507
aliguoricde76ee2009-03-05 23:01:51 +00004508 err = monitor_read_password(mon, bdrv_password_cb, bs);
4509
4510 if (err && completion_cb)
4511 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004512}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004513
4514typedef struct QemuErrorSink QemuErrorSink;
4515struct QemuErrorSink {
4516 enum {
4517 ERR_SINK_FILE,
4518 ERR_SINK_MONITOR,
4519 } dest;
4520 union {
4521 FILE *fp;
4522 Monitor *mon;
4523 };
4524 QemuErrorSink *previous;
4525};
4526
Blue Swirl528e93a2009-08-31 15:14:40 +00004527static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004528
4529void qemu_errors_to_file(FILE *fp)
4530{
4531 QemuErrorSink *sink;
4532
4533 sink = qemu_mallocz(sizeof(*sink));
4534 sink->dest = ERR_SINK_FILE;
4535 sink->fp = fp;
4536 sink->previous = qemu_error_sink;
4537 qemu_error_sink = sink;
4538}
4539
4540void qemu_errors_to_mon(Monitor *mon)
4541{
4542 QemuErrorSink *sink;
4543
4544 sink = qemu_mallocz(sizeof(*sink));
4545 sink->dest = ERR_SINK_MONITOR;
4546 sink->mon = mon;
4547 sink->previous = qemu_error_sink;
4548 qemu_error_sink = sink;
4549}
4550
4551void qemu_errors_to_previous(void)
4552{
4553 QemuErrorSink *sink;
4554
4555 assert(qemu_error_sink != NULL);
4556 sink = qemu_error_sink;
4557 qemu_error_sink = sink->previous;
4558 qemu_free(sink);
4559}
4560
4561void qemu_error(const char *fmt, ...)
4562{
4563 va_list args;
4564
4565 assert(qemu_error_sink != NULL);
4566 switch (qemu_error_sink->dest) {
4567 case ERR_SINK_FILE:
4568 va_start(args, fmt);
4569 vfprintf(qemu_error_sink->fp, fmt, args);
4570 va_end(args);
4571 break;
4572 case ERR_SINK_MONITOR:
4573 va_start(args, fmt);
4574 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4575 va_end(args);
4576 break;
4577 }
4578}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004579
4580void qemu_error_internal(const char *file, int linenr, const char *func,
4581 const char *fmt, ...)
4582{
4583 va_list va;
4584 QError *qerror;
4585
4586 assert(qemu_error_sink != NULL);
4587
4588 va_start(va, fmt);
4589 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4590 va_end(va);
4591
4592 switch (qemu_error_sink->dest) {
4593 case ERR_SINK_FILE:
4594 qerror_print(qerror);
4595 QDECREF(qerror);
4596 break;
4597 case ERR_SINK_MONITOR:
4598 assert(qemu_error_sink->mon->error == NULL);
4599 qemu_error_sink->mon->error = qerror;
4600 break;
4601 }
4602}