blob: eb641e3d00b1563e294a2ddb6137cada26a14f3f [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"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030050#include "qlist.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030051#include "qdict.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020052#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030053#include "qstring.h"
Luiz Capitulino8204a912009-11-18 23:05:31 -020054#include "qerror.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000059
bellard9dc39cb2004-03-14 21:38:27 +000060//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000061//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000062
bellard9307c4c2004-04-04 12:57:25 +000063/*
64 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000065 *
bellard9307c4c2004-04-04 12:57:25 +000066 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000067 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000068 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000069 * 'i' 32 bit integer
70 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000071 * '/' optional gdb-like print format (like "/10x")
72 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030073 * '?' optional type (for all types, except '/')
74 * '.' other form of optional type (for 'i' and 'l')
75 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000076 *
77 */
78
Anthony Liguoric227f092009-10-01 16:12:16 -050079typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000080 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000081 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000082 const char *params;
83 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030084 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030085 union {
86 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030087 void (*info_new)(Monitor *mon, QObject **ret_data);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030088 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030089 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -030090 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050091} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000092
Mark McLoughlinf07918f2009-07-22 09:11:40 +010093/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050094typedef struct mon_fd_t mon_fd_t;
95struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010096 char *name;
97 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050098 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010099};
100
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200101typedef struct MonitorControl {
102 QObject *id;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200103 int print_enabled;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200104 JSONMessageParser parser;
105} MonitorControl;
106
aliguori87127162009-03-05 23:01:29 +0000107struct Monitor {
108 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200109 int mux_out;
110 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000111 int flags;
112 int suspend_cnt;
113 uint8_t outbuf[1024];
114 int outbuf_index;
115 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200116 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000117 CPUState *mon_cpu;
118 BlockDriverCompletionFunc *password_completion_cb;
119 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200120 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500121 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000122 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000123};
124
Blue Swirl72cf2d42009-09-12 07:36:22 +0000125static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000126
Anthony Liguoric227f092009-10-01 16:12:16 -0500127static const mon_cmd_t mon_cmds[];
128static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000129
aliguori87127162009-03-05 23:01:29 +0000130Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000131
aliguori731b0362009-03-05 23:01:42 +0000132static void monitor_command_cb(Monitor *mon, const char *cmdline,
133 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000134
Luiz Capitulino418173c2009-11-26 22:58:51 -0200135/* Return true if in control mode, false otherwise */
136static inline int monitor_ctrl_mode(const Monitor *mon)
137{
138 return (mon->flags & MONITOR_USE_CONTROL);
139}
140
aliguori731b0362009-03-05 23:01:42 +0000141static void monitor_read_command(Monitor *mon, int show_prompt)
142{
143 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
144 if (show_prompt)
145 readline_show_prompt(mon->rs);
146}
bellard6a00d602005-11-21 23:25:50 +0000147
aliguoricde76ee2009-03-05 23:01:51 +0000148static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
149 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000150{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100151 if (monitor_ctrl_mode(mon)) {
152 qemu_error_new(QERR_MISSING_PARAMETER, "password");
153 return -EINVAL;
154 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000155 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
156 /* prompt is printed on return from the command handler */
157 return 0;
158 } else {
159 monitor_printf(mon, "terminal does not support password prompting\n");
160 return -ENOTTY;
161 }
aliguoribb5fc202009-03-05 23:01:15 +0000162}
163
aliguori376253e2009-03-05 23:01:23 +0000164void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000165{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200166 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000167 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
168 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000169 }
170}
171
172/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000173static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000174{
ths60fe76f2007-12-16 03:02:09 +0000175 char c;
aliguori731b0362009-03-05 23:01:42 +0000176
177 if (!mon)
178 return;
179
bellard7e2515e2004-08-01 21:52:19 +0000180 for(;;) {
181 c = *str++;
182 if (c == '\0')
183 break;
bellard7ba12602006-07-14 20:26:42 +0000184 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000185 mon->outbuf[mon->outbuf_index++] = '\r';
186 mon->outbuf[mon->outbuf_index++] = c;
187 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
188 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000189 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000190 }
191}
192
aliguori376253e2009-03-05 23:01:23 +0000193void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000194{
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200195 if (mon->mc && !mon->mc->print_enabled) {
196 qemu_error_new(QERR_UNDEFINED_ERROR);
197 } else {
198 char buf[4096];
199 vsnprintf(buf, sizeof(buf), fmt, ap);
200 monitor_puts(mon, buf);
201 }
bellard7e2515e2004-08-01 21:52:19 +0000202}
203
aliguori376253e2009-03-05 23:01:23 +0000204void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000205{
206 va_list ap;
207 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000208 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000209 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000210}
211
aliguori376253e2009-03-05 23:01:23 +0000212void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000213{
214 int i;
215
216 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000217 switch (filename[i]) {
218 case ' ':
219 case '"':
220 case '\\':
221 monitor_printf(mon, "\\%c", filename[i]);
222 break;
223 case '\t':
224 monitor_printf(mon, "\\t");
225 break;
226 case '\r':
227 monitor_printf(mon, "\\r");
228 break;
229 case '\n':
230 monitor_printf(mon, "\\n");
231 break;
232 default:
233 monitor_printf(mon, "%c", filename[i]);
234 break;
235 }
thsfef30742006-12-22 14:11:32 +0000236 }
237}
238
bellard7fe48482004-10-09 18:08:01 +0000239static int monitor_fprintf(FILE *stream, const char *fmt, ...)
240{
241 va_list ap;
242 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000243 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000244 va_end(ap);
245 return 0;
246}
247
Luiz Capitulino13c74252009-10-07 13:41:55 -0300248static void monitor_user_noop(Monitor *mon, const QObject *data) { }
249
Luiz Capitulino13917be2009-10-07 13:41:54 -0300250static inline int monitor_handler_ported(const mon_cmd_t *cmd)
251{
252 return cmd->user_print != NULL;
253}
254
Luiz Capitulino8204a912009-11-18 23:05:31 -0200255static inline int monitor_has_error(const Monitor *mon)
256{
257 return mon->error != NULL;
258}
259
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200260static void monitor_json_emitter(Monitor *mon, const QObject *data)
261{
262 QString *json;
263
264 json = qobject_to_json(data);
265 assert(json != NULL);
266
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200267 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200268 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200269 mon->mc->print_enabled = 0;
270
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200271 QDECREF(json);
272}
273
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200274static void monitor_protocol_emitter(Monitor *mon, QObject *data)
275{
276 QDict *qmp;
277
278 qmp = qdict_new();
279
280 if (!monitor_has_error(mon)) {
281 /* success response */
282 if (data) {
283 qobject_incref(data);
284 qdict_put_obj(qmp, "return", data);
285 } else {
286 qdict_put(qmp, "return", qstring_from_str("OK"));
287 }
288 } else {
289 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100290 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200291 qdict_put(qmp, "error", mon->error->error);
292 QINCREF(mon->error->error);
293 QDECREF(mon->error);
294 mon->error = NULL;
295 }
296
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200297 if (mon->mc->id) {
298 qdict_put_obj(qmp, "id", mon->mc->id);
299 mon->mc->id = NULL;
300 }
301
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200302 monitor_json_emitter(mon, QOBJECT(qmp));
303 QDECREF(qmp);
304}
305
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200306static void timestamp_put(QDict *qdict)
307{
308 int err;
309 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000310 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200311
Blue Swirld08d6f02009-12-04 18:06:39 +0000312 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200313 if (err < 0)
314 return;
315
316 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
317 "'microseconds': %" PRId64 " }",
318 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
319 assert(obj != NULL);
320
321 qdict_put_obj(qdict, "timestamp", obj);
322}
323
324/**
325 * monitor_protocol_event(): Generate a Monitor event
326 *
327 * Event-specific data can be emitted through the (optional) 'data' parameter.
328 */
329void monitor_protocol_event(MonitorEvent event, QObject *data)
330{
331 QDict *qmp;
332 const char *event_name;
333 Monitor *mon = cur_mon;
334
Blue Swirl242cd002009-12-04 18:05:45 +0000335 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200336
337 if (!monitor_ctrl_mode(mon))
338 return;
339
340 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000341 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200342 event_name = "DEBUG";
343 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000344 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200345 event_name = "SHUTDOWN";
346 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000347 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200348 event_name = "RESET";
349 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000350 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200351 event_name = "POWERDOWN";
352 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000353 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200354 event_name = "STOP";
355 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200356 default:
357 abort();
358 break;
359 }
360
361 qmp = qdict_new();
362 timestamp_put(qmp);
363 qdict_put(qmp, "event", qstring_from_str(event_name));
364 if (data)
365 qdict_put_obj(qmp, "data", data);
366
367 monitor_json_emitter(mon, QOBJECT(qmp));
368 QDECREF(qmp);
369}
370
bellard9dc39cb2004-03-14 21:38:27 +0000371static int compare_cmd(const char *name, const char *list)
372{
373 const char *p, *pstart;
374 int len;
375 len = strlen(name);
376 p = list;
377 for(;;) {
378 pstart = p;
379 p = strchr(p, '|');
380 if (!p)
381 p = pstart + strlen(pstart);
382 if ((p - pstart) == len && !memcmp(pstart, name, len))
383 return 1;
384 if (*p == '\0')
385 break;
386 p++;
387 }
388 return 0;
389}
390
Anthony Liguoric227f092009-10-01 16:12:16 -0500391static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000392 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000393{
Anthony Liguoric227f092009-10-01 16:12:16 -0500394 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000395
396 for(cmd = cmds; cmd->name != NULL; cmd++) {
397 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000398 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
399 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000400 }
401}
402
aliguori376253e2009-03-05 23:01:23 +0000403static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000404{
405 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000406 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000407 } else {
aliguori376253e2009-03-05 23:01:23 +0000408 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000409 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000410 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000411 monitor_printf(mon, "Log items (comma separated):\n");
412 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000413 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000414 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000415 }
416 }
bellard9dc39cb2004-03-14 21:38:27 +0000417 }
418}
419
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300420static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300421{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300422 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300423}
424
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300425static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000426{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200427 int all_devices;
428 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300429 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000430
bellard7954c732006-08-01 15:52:40 +0000431 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000432 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200433 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300434 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200435 continue;
436 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000437 }
438}
439
Luiz Capitulino13c74252009-10-07 13:41:55 -0300440static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000441{
Anthony Liguoric227f092009-10-01 16:12:16 -0500442 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300443 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000444
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200445 if (!item) {
446 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000447 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200448 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300449
450 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000451 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300452 break;
bellard9dc39cb2004-03-14 21:38:27 +0000453 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300454
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200455 if (cmd->name == NULL) {
456 if (monitor_ctrl_mode(mon)) {
457 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
458 return;
459 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300460 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200461 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300462
463 if (monitor_handler_ported(cmd)) {
464 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200465
466 if (!monitor_ctrl_mode(mon)) {
467 /*
468 * User Protocol function is called here, Monitor Protocol is
469 * handled by monitor_call_handler()
470 */
471 if (*ret_data)
472 cmd->user_print(mon, *ret_data);
473 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300474 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200475 if (monitor_ctrl_mode(mon)) {
476 /* handler not converted yet */
477 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
478 } else {
479 cmd->mhandler.info(mon);
480 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300481 }
482
bellard9dc39cb2004-03-14 21:38:27 +0000483 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300484
485help:
486 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000487}
488
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200489static void do_info_version_print(Monitor *mon, const QObject *data)
490{
491 QDict *qdict;
492
493 qdict = qobject_to_qdict(data);
494
495 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
496 qdict_get_str(qdict, "package"));
497}
498
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300499/**
500 * do_info_version(): Show QEMU version
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200501 *
502 * Return a QDict with the following information:
503 *
504 * - "qemu": QEMU's version
505 * - "package": package's version
506 *
507 * Example:
508 *
509 * { "qemu": "0.11.50", "package": "" }
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300510 */
511static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000512{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200513 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
514 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000515}
516
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200517static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000518{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200519 QDict *qdict;
520
521 qdict = qobject_to_qdict(data);
522 if (qdict_size(qdict) == 0) {
523 return;
524 }
525
526 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
527}
528
529/**
530 * do_info_name(): Show VM name
531 *
532 * Return a QDict with the following information:
533 *
534 * - "name": VM's name (optional)
535 *
536 * Example:
537 *
538 * { "name": "qemu-name" }
539 */
540static void do_info_name(Monitor *mon, QObject **ret_data)
541{
542 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
543 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000544}
545
Luiz Capitulino1a728672009-12-10 17:15:56 -0200546static QObject *get_cmd_dict(const char *name)
547{
548 const char *p;
549
550 /* Remove '|' from some commands */
551 p = strchr(name, '|');
552 if (p) {
553 p++;
554 } else {
555 p = name;
556 }
557
558 return qobject_from_jsonf("{ 'name': %s }", p);
559}
560
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200561/**
562 * do_info_commands(): List QMP available commands
563 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200564 * Each command is represented by a QDict, the returned QObject is a QList
565 * of all commands.
566 *
567 * The QDict contains:
568 *
569 * - "name": command's name
570 *
571 * Example:
572 *
573 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200574 */
575static void do_info_commands(Monitor *mon, QObject **ret_data)
576{
577 QList *cmd_list;
578 const mon_cmd_t *cmd;
579
580 cmd_list = qlist_new();
581
582 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
583 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200584 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200585 }
586 }
587
588 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
589 if (monitor_handler_ported(cmd)) {
590 char buf[128];
591 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200592 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200593 }
594 }
595
596 *ret_data = QOBJECT(cmd_list);
597}
598
aurel32bf4f74c2008-12-18 22:42:34 +0000599#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000600static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000601{
aliguori376253e2009-03-05 23:01:23 +0000602 monitor_printf(mon, "HPET is %s by QEMU\n",
603 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000604}
aurel32bf4f74c2008-12-18 22:42:34 +0000605#endif
aliguori16b29ae2008-12-17 23:28:44 +0000606
aliguori376253e2009-03-05 23:01:23 +0000607static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000608{
aliguori376253e2009-03-05 23:01:23 +0000609 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
610 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
611 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
612 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
613 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000614}
615
bellard6a00d602005-11-21 23:25:50 +0000616/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000617static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000618{
619 CPUState *env;
620
621 for(env = first_cpu; env != NULL; env = env->next_cpu) {
622 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000623 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000624 return 0;
625 }
626 }
627 return -1;
628}
629
pbrook9596ebb2007-11-18 01:44:38 +0000630static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000631{
aliguori731b0362009-03-05 23:01:42 +0000632 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000633 mon_set_cpu(0);
634 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300635 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000636 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000637}
638
aliguori376253e2009-03-05 23:01:23 +0000639static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000640{
bellard6a00d602005-11-21 23:25:50 +0000641 CPUState *env;
642 env = mon_get_cpu();
643 if (!env)
644 return;
bellard9307c4c2004-04-04 12:57:25 +0000645#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000646 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000647 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000648#else
aliguori376253e2009-03-05 23:01:23 +0000649 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000650 0);
bellard9307c4c2004-04-04 12:57:25 +0000651#endif
652}
653
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300654static void print_cpu_iter(QObject *obj, void *opaque)
655{
656 QDict *cpu;
657 int active = ' ';
658 Monitor *mon = opaque;
659
660 assert(qobject_type(obj) == QTYPE_QDICT);
661 cpu = qobject_to_qdict(obj);
662
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200663 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300664 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200665 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300666
667 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
668
669#if defined(TARGET_I386)
670 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
671 (target_ulong) qdict_get_int(cpu, "pc"));
672#elif defined(TARGET_PPC)
673 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
674 (target_long) qdict_get_int(cpu, "nip"));
675#elif defined(TARGET_SPARC)
676 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
677 (target_long) qdict_get_int(cpu, "pc"));
678 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
679 (target_long) qdict_get_int(cpu, "npc"));
680#elif defined(TARGET_MIPS)
681 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
682 (target_long) qdict_get_int(cpu, "PC"));
683#endif
684
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200685 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300686 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200687 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300688
689 monitor_printf(mon, "\n");
690}
691
692static void monitor_print_cpus(Monitor *mon, const QObject *data)
693{
694 QList *cpu_list;
695
696 assert(qobject_type(data) == QTYPE_QLIST);
697 cpu_list = qobject_to_qlist(data);
698 qlist_iter(cpu_list, print_cpu_iter, mon);
699}
700
701/**
702 * do_info_cpus(): Show CPU information
703 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200704 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300705 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200706 * - "cpu": CPU index
707 * - "current": true if this is the current CPU, false otherwise
708 * - "halted": true if the cpu is halted, false otherwise
709 * - Current program counter. The key's name depends on the architecture:
710 * "pc": i386/x86)64
711 * "nip": PPC
712 * "pc" and "npc": sparc
713 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300714 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200715 * Example:
716 *
717 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
718 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300719 */
720static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000721{
722 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300723 QList *cpu_list;
724
725 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000726
727 /* just to set the default cpu if not already done */
728 mon_get_cpu();
729
730 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200731 QDict *cpu;
732 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300733
Avi Kivity4c0960c2009-08-17 23:19:53 +0300734 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300735
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200736 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
737 env->cpu_index, env == mon->mon_cpu,
738 env->halted);
739 assert(obj != NULL);
740
741 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300742
bellard6a00d602005-11-21 23:25:50 +0000743#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300744 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000745#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300746 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000747#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300748 qdict_put(cpu, "pc", qint_from_int(env->pc));
749 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000750#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300751 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000752#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300753
754 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000755 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300756
757 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000758}
759
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300760static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000761{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300762 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000763 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000764 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000765}
766
aliguori376253e2009-03-05 23:01:23 +0000767static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000768{
aliguori376253e2009-03-05 23:01:23 +0000769 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000770}
771
aliguori376253e2009-03-05 23:01:23 +0000772static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000773{
774 int i;
bellard7e2515e2004-08-01 21:52:19 +0000775 const char *str;
ths3b46e622007-09-17 08:09:54 +0000776
aliguoricde76ee2009-03-05 23:01:51 +0000777 if (!mon->rs)
778 return;
bellard7e2515e2004-08-01 21:52:19 +0000779 i = 0;
780 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000781 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000782 if (!str)
783 break;
aliguori376253e2009-03-05 23:01:23 +0000784 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000785 i++;
bellardaa455482004-04-04 13:07:25 +0000786 }
787}
788
j_mayer76a66252007-03-07 08:32:30 +0000789#if defined(TARGET_PPC)
790/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000791static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000792{
793 CPUState *env;
794
795 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000796 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000797}
798#endif
799
Luiz Capitulinob223f352009-10-07 13:41:56 -0300800/**
801 * do_quit(): Quit QEMU execution
802 */
803static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000804{
805 exit(0);
806}
807
aliguori376253e2009-03-05 23:01:23 +0000808static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000809{
810 if (bdrv_is_inserted(bs)) {
811 if (!force) {
812 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100813 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
814 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000815 return -1;
816 }
817 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100818 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000819 return -1;
820 }
821 }
822 bdrv_close(bs);
823 }
824 return 0;
825}
826
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300827static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000828{
829 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300830 int force = qdict_get_int(qdict, "force");
831 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000832
bellard9307c4c2004-04-04 12:57:25 +0000833 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000834 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100835 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000836 return;
837 }
aliguori376253e2009-03-05 23:01:23 +0000838 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000839}
840
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -0200841static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
842 QObject **ret_data)
843{
844 BlockDriverState *bs;
845
846 bs = bdrv_find(qdict_get_str(qdict, "device"));
847 if (!bs) {
848 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
849 return;
850 }
851
852 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
853 qemu_error_new(QERR_INVALID_PASSWORD);
854 }
855}
856
aliguori376253e2009-03-05 23:01:23 +0000857static void do_change_block(Monitor *mon, const char *device,
858 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000859{
860 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000861 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000862
bellard9307c4c2004-04-04 12:57:25 +0000863 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000864 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100865 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +0000866 return;
867 }
aurel322ecea9b2008-06-18 22:10:01 +0000868 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100869 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000870 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100871 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000872 return;
873 }
874 }
aliguori376253e2009-03-05 23:01:23 +0000875 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000876 return;
aurel322ecea9b2008-06-18 22:10:01 +0000877 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000878 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000879}
880
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100881static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000882{
883 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100884 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +0000885
Markus Armbruster2895e072009-12-07 21:37:01 +0100886}
887
888static void change_vnc_password_cb(Monitor *mon, const char *password,
889 void *opaque)
890{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100891 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000892 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000893}
894
aliguori376253e2009-03-05 23:01:23 +0000895static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000896{
ths70848512007-08-25 01:37:05 +0000897 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000898 strcmp(target, "password") == 0) {
899 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000900 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000901 strncpy(password, arg, sizeof(password));
902 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100903 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000904 } else {
aliguori376253e2009-03-05 23:01:23 +0000905 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000906 }
ths70848512007-08-25 01:37:05 +0000907 } else {
aliguori28a76be2009-03-06 20:27:40 +0000908 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100909 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +0000910 }
thse25a5822007-08-25 01:36:20 +0000911}
912
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100913/**
914 * do_change(): Change a removable medium, or VNC configuration
915 */
916static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000917{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300918 const char *device = qdict_get_str(qdict, "device");
919 const char *target = qdict_get_str(qdict, "target");
920 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000921 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000922 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000923 } else {
aliguori28a76be2009-03-06 20:27:40 +0000924 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000925 }
926}
927
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300928static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000929{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300930 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000931}
932
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300933static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000934{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300935 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000936}
937
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300938static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000939{
940 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300941 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000942
bellard9307c4c2004-04-04 12:57:25 +0000943 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000944 mask = 0;
945 } else {
bellard9307c4c2004-04-04 12:57:25 +0000946 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000947 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000948 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000949 return;
950 }
951 }
952 cpu_set_log(mask);
953}
954
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300955static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000956{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300957 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000958 if (!option || !strcmp(option, "on")) {
959 singlestep = 1;
960 } else if (!strcmp(option, "off")) {
961 singlestep = 0;
962 } else {
963 monitor_printf(mon, "unexpected option %s\n", option);
964 }
965}
966
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300967/**
968 * do_stop(): Stop VM execution
969 */
970static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000971{
972 vm_stop(EXCP_INTERRUPT);
973}
974
aliguoribb5fc202009-03-05 23:01:15 +0000975static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000976
aliguori376253e2009-03-05 23:01:23 +0000977struct bdrv_iterate_context {
978 Monitor *mon;
979 int err;
980};
aliguoric0f4ce72009-03-05 23:01:01 +0000981
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300982/**
983 * do_cont(): Resume emulation.
984 */
985static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000986{
987 struct bdrv_iterate_context context = { mon, 0 };
988
989 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000990 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000991 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000992 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000993}
994
aliguoribb5fc202009-03-05 23:01:15 +0000995static void bdrv_key_cb(void *opaque, int err)
996{
aliguori376253e2009-03-05 23:01:23 +0000997 Monitor *mon = opaque;
998
aliguoribb5fc202009-03-05 23:01:15 +0000999 /* another key was set successfully, retry to continue */
1000 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001001 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001002}
1003
1004static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1005{
aliguori376253e2009-03-05 23:01:23 +00001006 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001007
aliguori376253e2009-03-05 23:01:23 +00001008 if (!context->err && bdrv_key_required(bs)) {
1009 context->err = -EBUSY;
1010 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1011 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001012 }
1013}
1014
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001015static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001016{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001017 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001018 if (!device)
1019 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1020 if (gdbserver_start(device) < 0) {
1021 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1022 device);
1023 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001024 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001025 } else {
aliguori59030a82009-04-05 18:43:41 +00001026 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1027 device);
bellard8a7ddc32004-03-31 19:00:16 +00001028 }
1029}
1030
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001031static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001032{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001033 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001034 if (select_watchdog_action(action) == -1) {
1035 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1036 }
1037}
1038
aliguori376253e2009-03-05 23:01:23 +00001039static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001040{
aliguori376253e2009-03-05 23:01:23 +00001041 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001042 switch(c) {
1043 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001044 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001045 break;
1046 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001047 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001048 break;
1049 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001050 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001051 break;
1052 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001053 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001054 break;
1055 default:
1056 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001057 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001058 } else {
aliguori376253e2009-03-05 23:01:23 +00001059 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001060 }
1061 break;
1062 }
aliguori376253e2009-03-05 23:01:23 +00001063 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001064}
1065
aliguori376253e2009-03-05 23:01:23 +00001066static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001067 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001068{
bellard6a00d602005-11-21 23:25:50 +00001069 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +00001070 int nb_per_line, l, line_size, i, max_digits, len;
1071 uint8_t buf[16];
1072 uint64_t v;
1073
1074 if (format == 'i') {
1075 int flags;
1076 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001077 env = mon_get_cpu();
1078 if (!env && !is_physical)
1079 return;
bellard9307c4c2004-04-04 12:57:25 +00001080#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001081 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001082 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001083 } else if (wsize == 4) {
1084 flags = 0;
1085 } else {
bellard6a15fd12006-04-12 21:07:07 +00001086 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001087 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001088 if (env) {
1089#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001090 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001091 (env->segs[R_CS].flags & DESC_L_MASK))
1092 flags = 2;
1093 else
1094#endif
1095 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1096 flags = 1;
1097 }
bellard4c27ba22004-04-25 18:05:08 +00001098 }
1099#endif
aliguori376253e2009-03-05 23:01:23 +00001100 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001101 return;
1102 }
1103
1104 len = wsize * count;
1105 if (wsize == 1)
1106 line_size = 8;
1107 else
1108 line_size = 16;
1109 nb_per_line = line_size / wsize;
1110 max_digits = 0;
1111
1112 switch(format) {
1113 case 'o':
1114 max_digits = (wsize * 8 + 2) / 3;
1115 break;
1116 default:
1117 case 'x':
1118 max_digits = (wsize * 8) / 4;
1119 break;
1120 case 'u':
1121 case 'd':
1122 max_digits = (wsize * 8 * 10 + 32) / 33;
1123 break;
1124 case 'c':
1125 wsize = 1;
1126 break;
1127 }
1128
1129 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001130 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001131 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001132 else
aliguori376253e2009-03-05 23:01:23 +00001133 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001134 l = len;
1135 if (l > line_size)
1136 l = line_size;
1137 if (is_physical) {
1138 cpu_physical_memory_rw(addr, buf, l, 0);
1139 } else {
bellard6a00d602005-11-21 23:25:50 +00001140 env = mon_get_cpu();
1141 if (!env)
1142 break;
aliguoric8f79b62008-08-18 14:00:20 +00001143 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001144 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001145 break;
1146 }
bellard9307c4c2004-04-04 12:57:25 +00001147 }
ths5fafdf22007-09-16 21:08:06 +00001148 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001149 while (i < l) {
1150 switch(wsize) {
1151 default:
1152 case 1:
1153 v = ldub_raw(buf + i);
1154 break;
1155 case 2:
1156 v = lduw_raw(buf + i);
1157 break;
1158 case 4:
bellard92a31b12005-02-10 22:00:52 +00001159 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001160 break;
1161 case 8:
1162 v = ldq_raw(buf + i);
1163 break;
1164 }
aliguori376253e2009-03-05 23:01:23 +00001165 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001166 switch(format) {
1167 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001168 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001169 break;
1170 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001171 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001172 break;
1173 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001174 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001175 break;
1176 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001177 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001178 break;
1179 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001180 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001181 break;
1182 }
1183 i += wsize;
1184 }
aliguori376253e2009-03-05 23:01:23 +00001185 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001186 addr += l;
1187 len -= l;
1188 }
1189}
1190
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001191static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001192{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001193 int count = qdict_get_int(qdict, "count");
1194 int format = qdict_get_int(qdict, "format");
1195 int size = qdict_get_int(qdict, "size");
1196 target_long addr = qdict_get_int(qdict, "addr");
1197
aliguori376253e2009-03-05 23:01:23 +00001198 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001199}
1200
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001201static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001202{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001203 int count = qdict_get_int(qdict, "count");
1204 int format = qdict_get_int(qdict, "format");
1205 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001206 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001207
aliguori376253e2009-03-05 23:01:23 +00001208 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001209}
1210
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001211static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001212{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001213 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001214 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001215
blueswir17743e582007-09-24 18:39:04 +00001216#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001217 switch(format) {
1218 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001220 break;
1221 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001222 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001223 break;
1224 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001225 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001226 break;
1227 default:
1228 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001229 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001230 break;
1231 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001232 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001233 break;
1234 }
bellard92a31b12005-02-10 22:00:52 +00001235#else
1236 switch(format) {
1237 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001238 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001239 break;
1240 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001241 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001242 break;
1243 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001244 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001245 break;
1246 default:
1247 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001249 break;
1250 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001251 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001252 break;
1253 }
1254#endif
aliguori376253e2009-03-05 23:01:23 +00001255 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001256}
1257
Luiz Capitulino57e09452009-10-16 12:23:43 -03001258static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001259{
1260 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001261 uint32_t size = qdict_get_int(qdict, "size");
1262 const char *filename = qdict_get_str(qdict, "filename");
1263 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001264 uint32_t l;
1265 CPUState *env;
1266 uint8_t buf[1024];
1267
1268 env = mon_get_cpu();
1269 if (!env)
1270 return;
1271
1272 f = fopen(filename, "wb");
1273 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001274 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001275 return;
1276 }
1277 while (size != 0) {
1278 l = sizeof(buf);
1279 if (l > size)
1280 l = size;
1281 cpu_memory_rw_debug(env, addr, buf, l, 0);
1282 fwrite(buf, 1, l, f);
1283 addr += l;
1284 size -= l;
1285 }
1286 fclose(f);
1287}
1288
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001289static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1290 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001291{
1292 FILE *f;
1293 uint32_t l;
1294 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001295 uint32_t size = qdict_get_int(qdict, "size");
1296 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001297 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001298
1299 f = fopen(filename, "wb");
1300 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001301 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001302 return;
1303 }
1304 while (size != 0) {
1305 l = sizeof(buf);
1306 if (l > size)
1307 l = size;
1308 cpu_physical_memory_rw(addr, buf, l, 0);
1309 fwrite(buf, 1, l, f);
1310 fflush(f);
1311 addr += l;
1312 size -= l;
1313 }
1314 fclose(f);
1315}
1316
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001317static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001318{
1319 uint32_t addr;
1320 uint8_t buf[1];
1321 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001322 uint32_t start = qdict_get_int(qdict, "start");
1323 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001324
1325 sum = 0;
1326 for(addr = start; addr < (start + size); addr++) {
1327 cpu_physical_memory_rw(addr, buf, 1, 0);
1328 /* BSD sum algorithm ('sum' Unix command) */
1329 sum = (sum >> 1) | (sum << 15);
1330 sum += buf[0];
1331 }
aliguori376253e2009-03-05 23:01:23 +00001332 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001333}
1334
bellarda3a91a32004-06-04 11:06:21 +00001335typedef struct {
1336 int keycode;
1337 const char *name;
1338} KeyDef;
1339
1340static const KeyDef key_defs[] = {
1341 { 0x2a, "shift" },
1342 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001343
bellarda3a91a32004-06-04 11:06:21 +00001344 { 0x38, "alt" },
1345 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001346 { 0x64, "altgr" },
1347 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001348 { 0x1d, "ctrl" },
1349 { 0x9d, "ctrl_r" },
1350
1351 { 0xdd, "menu" },
1352
1353 { 0x01, "esc" },
1354
1355 { 0x02, "1" },
1356 { 0x03, "2" },
1357 { 0x04, "3" },
1358 { 0x05, "4" },
1359 { 0x06, "5" },
1360 { 0x07, "6" },
1361 { 0x08, "7" },
1362 { 0x09, "8" },
1363 { 0x0a, "9" },
1364 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001365 { 0x0c, "minus" },
1366 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001367 { 0x0e, "backspace" },
1368
1369 { 0x0f, "tab" },
1370 { 0x10, "q" },
1371 { 0x11, "w" },
1372 { 0x12, "e" },
1373 { 0x13, "r" },
1374 { 0x14, "t" },
1375 { 0x15, "y" },
1376 { 0x16, "u" },
1377 { 0x17, "i" },
1378 { 0x18, "o" },
1379 { 0x19, "p" },
1380
1381 { 0x1c, "ret" },
1382
1383 { 0x1e, "a" },
1384 { 0x1f, "s" },
1385 { 0x20, "d" },
1386 { 0x21, "f" },
1387 { 0x22, "g" },
1388 { 0x23, "h" },
1389 { 0x24, "j" },
1390 { 0x25, "k" },
1391 { 0x26, "l" },
1392
1393 { 0x2c, "z" },
1394 { 0x2d, "x" },
1395 { 0x2e, "c" },
1396 { 0x2f, "v" },
1397 { 0x30, "b" },
1398 { 0x31, "n" },
1399 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001400 { 0x33, "comma" },
1401 { 0x34, "dot" },
1402 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001403
balrog4d3b6f62008-02-10 16:33:14 +00001404 { 0x37, "asterisk" },
1405
bellarda3a91a32004-06-04 11:06:21 +00001406 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001407 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001408 { 0x3b, "f1" },
1409 { 0x3c, "f2" },
1410 { 0x3d, "f3" },
1411 { 0x3e, "f4" },
1412 { 0x3f, "f5" },
1413 { 0x40, "f6" },
1414 { 0x41, "f7" },
1415 { 0x42, "f8" },
1416 { 0x43, "f9" },
1417 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001418 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001419 { 0x46, "scroll_lock" },
1420
bellard64866c32006-05-07 18:03:31 +00001421 { 0xb5, "kp_divide" },
1422 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001423 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001424 { 0x4e, "kp_add" },
1425 { 0x9c, "kp_enter" },
1426 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001427 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001428
1429 { 0x52, "kp_0" },
1430 { 0x4f, "kp_1" },
1431 { 0x50, "kp_2" },
1432 { 0x51, "kp_3" },
1433 { 0x4b, "kp_4" },
1434 { 0x4c, "kp_5" },
1435 { 0x4d, "kp_6" },
1436 { 0x47, "kp_7" },
1437 { 0x48, "kp_8" },
1438 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001439
bellarda3a91a32004-06-04 11:06:21 +00001440 { 0x56, "<" },
1441
1442 { 0x57, "f11" },
1443 { 0x58, "f12" },
1444
1445 { 0xb7, "print" },
1446
1447 { 0xc7, "home" },
1448 { 0xc9, "pgup" },
1449 { 0xd1, "pgdn" },
1450 { 0xcf, "end" },
1451
1452 { 0xcb, "left" },
1453 { 0xc8, "up" },
1454 { 0xd0, "down" },
1455 { 0xcd, "right" },
1456
1457 { 0xd2, "insert" },
1458 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001459#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1460 { 0xf0, "stop" },
1461 { 0xf1, "again" },
1462 { 0xf2, "props" },
1463 { 0xf3, "undo" },
1464 { 0xf4, "front" },
1465 { 0xf5, "copy" },
1466 { 0xf6, "open" },
1467 { 0xf7, "paste" },
1468 { 0xf8, "find" },
1469 { 0xf9, "cut" },
1470 { 0xfa, "lf" },
1471 { 0xfb, "help" },
1472 { 0xfc, "meta_l" },
1473 { 0xfd, "meta_r" },
1474 { 0xfe, "compose" },
1475#endif
bellarda3a91a32004-06-04 11:06:21 +00001476 { 0, NULL },
1477};
1478
1479static int get_keycode(const char *key)
1480{
1481 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001482 char *endp;
1483 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001484
1485 for(p = key_defs; p->name != NULL; p++) {
1486 if (!strcmp(key, p->name))
1487 return p->keycode;
1488 }
bellard64866c32006-05-07 18:03:31 +00001489 if (strstart(key, "0x", NULL)) {
1490 ret = strtoul(key, &endp, 0);
1491 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1492 return ret;
1493 }
bellarda3a91a32004-06-04 11:06:21 +00001494 return -1;
1495}
1496
balrogc8256f92008-06-08 22:45:01 +00001497#define MAX_KEYCODES 16
1498static uint8_t keycodes[MAX_KEYCODES];
1499static int nb_pending_keycodes;
1500static QEMUTimer *key_timer;
1501
1502static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001503{
balrogc8256f92008-06-08 22:45:01 +00001504 int keycode;
1505
1506 while (nb_pending_keycodes > 0) {
1507 nb_pending_keycodes--;
1508 keycode = keycodes[nb_pending_keycodes];
1509 if (keycode & 0x80)
1510 kbd_put_keycode(0xe0);
1511 kbd_put_keycode(keycode | 0x80);
1512 }
1513}
1514
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001515static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001516{
balrog3401c0d2008-06-04 10:05:59 +00001517 char keyname_buf[16];
1518 char *separator;
1519 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001520 const char *string = qdict_get_str(qdict, "string");
1521 int has_hold_time = qdict_haskey(qdict, "hold_time");
1522 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001523
balrogc8256f92008-06-08 22:45:01 +00001524 if (nb_pending_keycodes > 0) {
1525 qemu_del_timer(key_timer);
1526 release_keys(NULL);
1527 }
1528 if (!has_hold_time)
1529 hold_time = 100;
1530 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001531 while (1) {
1532 separator = strchr(string, '-');
1533 keyname_len = separator ? separator - string : strlen(string);
1534 if (keyname_len > 0) {
1535 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1536 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001537 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001538 return;
bellarda3a91a32004-06-04 11:06:21 +00001539 }
balrogc8256f92008-06-08 22:45:01 +00001540 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001541 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001542 return;
1543 }
1544 keyname_buf[keyname_len] = 0;
1545 keycode = get_keycode(keyname_buf);
1546 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001547 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001548 return;
1549 }
balrogc8256f92008-06-08 22:45:01 +00001550 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001551 }
balrog3401c0d2008-06-04 10:05:59 +00001552 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001553 break;
balrog3401c0d2008-06-04 10:05:59 +00001554 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001555 }
balrogc8256f92008-06-08 22:45:01 +00001556 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001557 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001558 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001559 keycode = keycodes[i];
1560 if (keycode & 0x80)
1561 kbd_put_keycode(0xe0);
1562 kbd_put_keycode(keycode & 0x7f);
1563 }
balrogc8256f92008-06-08 22:45:01 +00001564 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001565 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001566 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001567}
1568
bellard13224a82006-07-14 22:03:35 +00001569static int mouse_button_state;
1570
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001571static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001572{
1573 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001574 const char *dx_str = qdict_get_str(qdict, "dx_str");
1575 const char *dy_str = qdict_get_str(qdict, "dy_str");
1576 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001577 dx = strtol(dx_str, NULL, 0);
1578 dy = strtol(dy_str, NULL, 0);
1579 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001580 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001581 dz = strtol(dz_str, NULL, 0);
1582 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1583}
1584
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001585static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001586{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001587 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001588 mouse_button_state = button_state;
1589 kbd_mouse_event(0, 0, 0, mouse_button_state);
1590}
1591
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001592static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001593{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001594 int size = qdict_get_int(qdict, "size");
1595 int addr = qdict_get_int(qdict, "addr");
1596 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001597 uint32_t val;
1598 int suffix;
1599
1600 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001601 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001602 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001603 addr++;
1604 }
1605 addr &= 0xffff;
1606
1607 switch(size) {
1608 default:
1609 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001610 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001611 suffix = 'b';
1612 break;
1613 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001614 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001615 suffix = 'w';
1616 break;
1617 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001618 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001619 suffix = 'l';
1620 break;
1621 }
aliguori376253e2009-03-05 23:01:23 +00001622 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1623 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001624}
bellarda3a91a32004-06-04 11:06:21 +00001625
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001626static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001627{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001628 int size = qdict_get_int(qdict, "size");
1629 int addr = qdict_get_int(qdict, "addr");
1630 int val = qdict_get_int(qdict, "val");
1631
Jan Kiszkaf1147842009-07-14 10:20:11 +02001632 addr &= IOPORTS_MASK;
1633
1634 switch (size) {
1635 default:
1636 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001637 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001638 break;
1639 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001640 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001641 break;
1642 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001643 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001644 break;
1645 }
1646}
1647
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001648static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001649{
1650 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001651 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001652
Jan Kiszka76e30d02009-07-02 00:19:02 +02001653 res = qemu_boot_set(bootdevice);
1654 if (res == 0) {
1655 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1656 } else if (res > 0) {
1657 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001658 } else {
aliguori376253e2009-03-05 23:01:23 +00001659 monitor_printf(mon, "no function defined to set boot device list for "
1660 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001661 }
1662}
1663
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001664/**
1665 * do_system_reset(): Issue a machine reset
1666 */
1667static void do_system_reset(Monitor *mon, const QDict *qdict,
1668 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001669{
1670 qemu_system_reset_request();
1671}
1672
Luiz Capitulino43076662009-10-07 13:41:59 -03001673/**
1674 * do_system_powerdown(): Issue a machine powerdown
1675 */
1676static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1677 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001678{
1679 qemu_system_powerdown_request();
1680}
1681
bellardb86bda52004-09-18 19:32:46 +00001682#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001683static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001684{
aliguori376253e2009-03-05 23:01:23 +00001685 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1686 addr,
1687 pte & mask,
1688 pte & PG_GLOBAL_MASK ? 'G' : '-',
1689 pte & PG_PSE_MASK ? 'P' : '-',
1690 pte & PG_DIRTY_MASK ? 'D' : '-',
1691 pte & PG_ACCESSED_MASK ? 'A' : '-',
1692 pte & PG_PCD_MASK ? 'C' : '-',
1693 pte & PG_PWT_MASK ? 'T' : '-',
1694 pte & PG_USER_MASK ? 'U' : '-',
1695 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001696}
1697
aliguori376253e2009-03-05 23:01:23 +00001698static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001699{
bellard6a00d602005-11-21 23:25:50 +00001700 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001701 int l1, l2;
1702 uint32_t pgd, pde, pte;
1703
bellard6a00d602005-11-21 23:25:50 +00001704 env = mon_get_cpu();
1705 if (!env)
1706 return;
1707
bellardb86bda52004-09-18 19:32:46 +00001708 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001709 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001710 return;
1711 }
1712 pgd = env->cr[3] & ~0xfff;
1713 for(l1 = 0; l1 < 1024; l1++) {
1714 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1715 pde = le32_to_cpu(pde);
1716 if (pde & PG_PRESENT_MASK) {
1717 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001718 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001719 } else {
1720 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001721 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001722 (uint8_t *)&pte, 4);
1723 pte = le32_to_cpu(pte);
1724 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001725 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001726 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001727 ~0xfff);
1728 }
1729 }
1730 }
1731 }
1732 }
1733}
1734
aliguori376253e2009-03-05 23:01:23 +00001735static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001736 uint32_t end, int prot)
1737{
bellard9746b152004-11-11 18:30:24 +00001738 int prot1;
1739 prot1 = *plast_prot;
1740 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001741 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001742 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1743 *pstart, end, end - *pstart,
1744 prot1 & PG_USER_MASK ? 'u' : '-',
1745 'r',
1746 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001747 }
1748 if (prot != 0)
1749 *pstart = end;
1750 else
1751 *pstart = -1;
1752 *plast_prot = prot;
1753 }
1754}
1755
aliguori376253e2009-03-05 23:01:23 +00001756static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001757{
bellard6a00d602005-11-21 23:25:50 +00001758 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001759 int l1, l2, prot, last_prot;
1760 uint32_t pgd, pde, pte, start, end;
1761
bellard6a00d602005-11-21 23:25:50 +00001762 env = mon_get_cpu();
1763 if (!env)
1764 return;
1765
bellardb86bda52004-09-18 19:32:46 +00001766 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001767 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001768 return;
1769 }
1770 pgd = env->cr[3] & ~0xfff;
1771 last_prot = 0;
1772 start = -1;
1773 for(l1 = 0; l1 < 1024; l1++) {
1774 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1775 pde = le32_to_cpu(pde);
1776 end = l1 << 22;
1777 if (pde & PG_PRESENT_MASK) {
1778 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1779 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001780 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001781 } else {
1782 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001783 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001784 (uint8_t *)&pte, 4);
1785 pte = le32_to_cpu(pte);
1786 end = (l1 << 22) + (l2 << 12);
1787 if (pte & PG_PRESENT_MASK) {
1788 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1789 } else {
1790 prot = 0;
1791 }
aliguori376253e2009-03-05 23:01:23 +00001792 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001793 }
1794 }
1795 } else {
1796 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001797 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001798 }
1799 }
1800}
1801#endif
1802
aurel327c664e22009-03-03 06:12:22 +00001803#if defined(TARGET_SH4)
1804
aliguori376253e2009-03-05 23:01:23 +00001805static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001806{
aliguori376253e2009-03-05 23:01:23 +00001807 monitor_printf(mon, " tlb%i:\t"
1808 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1809 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1810 "dirty=%hhu writethrough=%hhu\n",
1811 idx,
1812 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1813 tlb->v, tlb->sh, tlb->c, tlb->pr,
1814 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001815}
1816
aliguori376253e2009-03-05 23:01:23 +00001817static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001818{
1819 CPUState *env = mon_get_cpu();
1820 int i;
1821
aliguori376253e2009-03-05 23:01:23 +00001822 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001823 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001824 print_tlb (mon, i, &env->itlb[i]);
1825 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001826 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001827 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001828}
1829
1830#endif
1831
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001832static void do_info_kvm_print(Monitor *mon, const QObject *data)
1833{
1834 QDict *qdict;
1835
1836 qdict = qobject_to_qdict(data);
1837
1838 monitor_printf(mon, "kvm support: ");
1839 if (qdict_get_bool(qdict, "present")) {
1840 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1841 "enabled" : "disabled");
1842 } else {
1843 monitor_printf(mon, "not compiled\n");
1844 }
1845}
1846
1847/**
1848 * do_info_kvm(): Show KVM information
1849 *
1850 * Return a QDict with the following information:
1851 *
1852 * - "enabled": true if KVM support is enabled, false otherwise
1853 * - "present": true if QEMU has KVM support, false otherwise
1854 *
1855 * Example:
1856 *
1857 * { "enabled": true, "present": true }
1858 */
1859static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001860{
1861#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001862 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1863 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001864#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001865 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001866#endif
1867}
1868
aliguori030ea372009-04-21 22:30:47 +00001869static void do_info_numa(Monitor *mon)
1870{
aliguorib28b6232009-04-22 20:20:29 +00001871 int i;
aliguori030ea372009-04-21 22:30:47 +00001872 CPUState *env;
1873
1874 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1875 for (i = 0; i < nb_numa_nodes; i++) {
1876 monitor_printf(mon, "node %d cpus:", i);
1877 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1878 if (env->numa_node == i) {
1879 monitor_printf(mon, " %d", env->cpu_index);
1880 }
1881 }
1882 monitor_printf(mon, "\n");
1883 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1884 node_mem[i] >> 20);
1885 }
1886}
1887
bellard5f1ce942006-02-08 22:40:15 +00001888#ifdef CONFIG_PROFILER
1889
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001890int64_t qemu_time;
1891int64_t dev_time;
1892
aliguori376253e2009-03-05 23:01:23 +00001893static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001894{
1895 int64_t total;
1896 total = qemu_time;
1897 if (total == 0)
1898 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001899 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001900 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001901 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001902 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001903 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001904 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001905}
1906#else
aliguori376253e2009-03-05 23:01:23 +00001907static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001908{
aliguori376253e2009-03-05 23:01:23 +00001909 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001910}
1911#endif
1912
bellardec36b692006-07-16 18:57:03 +00001913/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001914static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001915
aliguori376253e2009-03-05 23:01:23 +00001916static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001917{
1918 int i;
1919 CaptureState *s;
1920
1921 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001922 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001923 s->ops.info (s->opaque);
1924 }
1925}
1926
Blue Swirl23130862009-06-06 08:22:04 +00001927#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001928static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001929{
1930 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001931 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001932 CaptureState *s;
1933
1934 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1935 if (i == n) {
1936 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001937 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001938 qemu_free (s);
1939 return;
1940 }
1941 }
1942}
1943
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001944static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001945{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001946 const char *path = qdict_get_str(qdict, "path");
1947 int has_freq = qdict_haskey(qdict, "freq");
1948 int freq = qdict_get_try_int(qdict, "freq", -1);
1949 int has_bits = qdict_haskey(qdict, "bits");
1950 int bits = qdict_get_try_int(qdict, "bits", -1);
1951 int has_channels = qdict_haskey(qdict, "nchannels");
1952 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001953 CaptureState *s;
1954
1955 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001956
1957 freq = has_freq ? freq : 44100;
1958 bits = has_bits ? bits : 16;
1959 nchannels = has_channels ? nchannels : 2;
1960
1961 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001962 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001963 qemu_free (s);
1964 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001965 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001966}
1967#endif
1968
aurel32dc1c0b72008-04-27 23:52:12 +00001969#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001970static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001971{
1972 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001973 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001974
1975 for (env = first_cpu; env != NULL; env = env->next_cpu)
1976 if (env->cpu_index == cpu_index) {
1977 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1978 break;
1979 }
1980}
1981#endif
1982
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02001983static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00001984{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02001985 QDict *qdict;
1986
1987 qdict = qobject_to_qdict(data);
1988
1989 monitor_printf(mon, "VM status: ");
1990 if (qdict_get_bool(qdict, "running")) {
1991 monitor_printf(mon, "running");
1992 if (qdict_get_bool(qdict, "singlestep")) {
1993 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00001994 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02001995 } else {
1996 monitor_printf(mon, "paused");
1997 }
1998
1999 monitor_printf(mon, "\n");
2000}
2001
2002/**
2003 * do_info_status(): VM status
2004 *
2005 * Return a QDict with the following information:
2006 *
2007 * - "running": true if the VM is running, or false if it is paused
2008 * - "singlestep": true if the VM is in single step mode, false otherwise
2009 *
2010 * Example:
2011 *
2012 * { "running": true, "singlestep": false }
2013 */
2014static void do_info_status(Monitor *mon, QObject **ret_data)
2015{
2016 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2017 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002018}
2019
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03002020/**
2021 * do_balloon(): Request VM to change its memory allocation
2022 */
2023static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00002024{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002025 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05002026 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00002027 qemu_balloon(target << 20);
2028}
2029
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002030static void monitor_print_balloon(Monitor *mon, const QObject *data)
2031{
Luiz Capitulino7f179672009-12-10 17:15:55 -02002032 QDict *qdict;
2033
2034 qdict = qobject_to_qdict(data);
2035
2036 monitor_printf(mon, "balloon: actual=%" PRId64 "\n",
2037 qdict_get_int(qdict, "balloon") >> 20);
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002038}
2039
2040/**
2041 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02002042 *
2043 * Return a QDict with the following information:
2044 *
2045 * - "balloon": current balloon value in bytes
2046 *
2047 * Example:
2048 *
2049 * { "balloon": 1073741824 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002050 */
2051static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00002052{
Anthony Liguoric227f092009-10-01 16:12:16 -05002053 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00002054
2055 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00002056 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02002057 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00002058 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02002059 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00002060 else
Luiz Capitulino7f179672009-12-10 17:15:55 -02002061 *ret_data = qobject_from_jsonf("{ 'balloon': %" PRId64 "}",
2062 (int64_t) actual);
aliguoridf751fa2008-12-04 20:19:35 +00002063}
2064
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002065static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002066{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002067 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002068
aliguori76655d62009-03-06 20:27:37 +00002069 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002070 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002071 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002072 return acl;
2073}
aliguori76655d62009-03-06 20:27:37 +00002074
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002075static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002076{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002077 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002078 qemu_acl *acl = find_acl(mon, aclname);
2079 qemu_acl_entry *entry;
2080 int i = 0;
2081
2082 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002083 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002084 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002085 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002086 i++;
2087 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002088 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002089 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002090 }
2091}
2092
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002093static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002094{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002095 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002096 qemu_acl *acl = find_acl(mon, aclname);
2097
2098 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002099 qemu_acl_reset(acl);
2100 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002101 }
2102}
aliguori76655d62009-03-06 20:27:37 +00002103
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002104static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002105{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002106 const char *aclname = qdict_get_str(qdict, "aclname");
2107 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002108 qemu_acl *acl = find_acl(mon, aclname);
2109
2110 if (acl) {
2111 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002112 acl->defaultDeny = 0;
2113 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002114 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002115 acl->defaultDeny = 1;
2116 monitor_printf(mon, "acl: policy set to 'deny'\n");
2117 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002118 monitor_printf(mon, "acl: unknown policy '%s', "
2119 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002120 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002121 }
2122}
aliguori76655d62009-03-06 20:27:37 +00002123
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002124static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002125{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002126 const char *aclname = qdict_get_str(qdict, "aclname");
2127 const char *match = qdict_get_str(qdict, "match");
2128 const char *policy = qdict_get_str(qdict, "policy");
2129 int has_index = qdict_haskey(qdict, "index");
2130 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002131 qemu_acl *acl = find_acl(mon, aclname);
2132 int deny, ret;
2133
2134 if (acl) {
2135 if (strcmp(policy, "allow") == 0) {
2136 deny = 0;
2137 } else if (strcmp(policy, "deny") == 0) {
2138 deny = 1;
2139 } else {
2140 monitor_printf(mon, "acl: unknown policy '%s', "
2141 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002142 return;
2143 }
aliguori28a76be2009-03-06 20:27:40 +00002144 if (has_index)
2145 ret = qemu_acl_insert(acl, deny, match, index);
2146 else
2147 ret = qemu_acl_append(acl, deny, match);
2148 if (ret < 0)
2149 monitor_printf(mon, "acl: unable to add acl entry\n");
2150 else
2151 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002152 }
2153}
aliguori76655d62009-03-06 20:27:37 +00002154
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002155static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002156{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002157 const char *aclname = qdict_get_str(qdict, "aclname");
2158 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002159 qemu_acl *acl = find_acl(mon, aclname);
2160 int ret;
aliguori76655d62009-03-06 20:27:37 +00002161
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002162 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002163 ret = qemu_acl_remove(acl, match);
2164 if (ret < 0)
2165 monitor_printf(mon, "acl: no matching acl entry\n");
2166 else
2167 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002168 }
2169}
2170
Huang Ying79c4f6b2009-06-23 10:05:14 +08002171#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002172static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002173{
2174 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002175 int cpu_index = qdict_get_int(qdict, "cpu_index");
2176 int bank = qdict_get_int(qdict, "bank");
2177 uint64_t status = qdict_get_int(qdict, "status");
2178 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2179 uint64_t addr = qdict_get_int(qdict, "addr");
2180 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002181
2182 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2183 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2184 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2185 break;
2186 }
2187}
2188#endif
2189
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002190static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002191{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002192 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002193 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002194 int fd;
2195
2196 fd = qemu_chr_get_msgfd(mon->chr);
2197 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002198 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002199 return;
2200 }
2201
2202 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002203 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002204 return;
2205 }
2206
2207 fd = dup(fd);
2208 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002209 if (errno == EMFILE)
2210 qemu_error_new(QERR_TOO_MANY_FILES);
2211 else
2212 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002213 return;
2214 }
2215
Blue Swirl72cf2d42009-09-12 07:36:22 +00002216 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002217 if (strcmp(monfd->name, fdname) != 0) {
2218 continue;
2219 }
2220
2221 close(monfd->fd);
2222 monfd->fd = fd;
2223 return;
2224 }
2225
Anthony Liguoric227f092009-10-01 16:12:16 -05002226 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002227 monfd->name = qemu_strdup(fdname);
2228 monfd->fd = fd;
2229
Blue Swirl72cf2d42009-09-12 07:36:22 +00002230 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002231}
2232
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002233static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002234{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002235 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002236 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002237
Blue Swirl72cf2d42009-09-12 07:36:22 +00002238 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002239 if (strcmp(monfd->name, fdname) != 0) {
2240 continue;
2241 }
2242
Blue Swirl72cf2d42009-09-12 07:36:22 +00002243 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002244 close(monfd->fd);
2245 qemu_free(monfd->name);
2246 qemu_free(monfd);
2247 return;
2248 }
2249
Markus Armbruster063c1a02009-12-07 21:37:11 +01002250 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002251}
2252
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002253static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002254{
2255 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002256 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002257
2258 vm_stop(0);
2259
Juan Quintela05f24012009-08-20 19:42:22 +02002260 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002261 vm_start();
2262}
2263
Mark McLoughlin7768e042009-07-22 09:11:41 +01002264int monitor_get_fd(Monitor *mon, const char *fdname)
2265{
Anthony Liguoric227f092009-10-01 16:12:16 -05002266 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002267
Blue Swirl72cf2d42009-09-12 07:36:22 +00002268 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002269 int fd;
2270
2271 if (strcmp(monfd->name, fdname) != 0) {
2272 continue;
2273 }
2274
2275 fd = monfd->fd;
2276
2277 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002278 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002279 qemu_free(monfd->name);
2280 qemu_free(monfd);
2281
2282 return fd;
2283 }
2284
2285 return -1;
2286}
2287
Anthony Liguoric227f092009-10-01 16:12:16 -05002288static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002289#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002290 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002291};
2292
Blue Swirl23130862009-06-06 08:22:04 +00002293/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002294static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002295 {
2296 .name = "version",
2297 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002298 .params = "",
2299 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002300 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002301 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002302 },
2303 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002304 .name = "commands",
2305 .args_type = "",
2306 .params = "",
2307 .help = "list QMP available commands",
2308 .user_print = monitor_user_noop,
2309 .mhandler.info_new = do_info_commands,
2310 },
2311 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002312 .name = "network",
2313 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002314 .params = "",
2315 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002316 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002317 },
2318 {
2319 .name = "chardev",
2320 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002321 .params = "",
2322 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002323 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002324 },
2325 {
2326 .name = "block",
2327 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002328 .params = "",
2329 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002330 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002331 },
2332 {
2333 .name = "blockstats",
2334 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002335 .params = "",
2336 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002337 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002338 },
2339 {
2340 .name = "registers",
2341 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002342 .params = "",
2343 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002344 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002345 },
2346 {
2347 .name = "cpus",
2348 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002349 .params = "",
2350 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002351 .user_print = monitor_print_cpus,
2352 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002353 },
2354 {
2355 .name = "history",
2356 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002357 .params = "",
2358 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002359 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002360 },
2361 {
2362 .name = "irq",
2363 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002364 .params = "",
2365 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002366 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002367 },
2368 {
2369 .name = "pic",
2370 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002371 .params = "",
2372 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002373 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002374 },
2375 {
2376 .name = "pci",
2377 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002378 .params = "",
2379 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002380 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002381 },
aurel327c664e22009-03-03 06:12:22 +00002382#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002383 {
2384 .name = "tlb",
2385 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002386 .params = "",
2387 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002388 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002389 },
aurel327c664e22009-03-03 06:12:22 +00002390#endif
2391#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002392 {
2393 .name = "mem",
2394 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002395 .params = "",
2396 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002397 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002398 },
2399 {
2400 .name = "hpet",
2401 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002402 .params = "",
2403 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002404 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002405 },
bellardb86bda52004-09-18 19:32:46 +00002406#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002407 {
2408 .name = "jit",
2409 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002410 .params = "",
2411 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002412 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002413 },
2414 {
2415 .name = "kvm",
2416 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002417 .params = "",
2418 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002419 .user_print = do_info_kvm_print,
2420 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002421 },
2422 {
2423 .name = "numa",
2424 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002425 .params = "",
2426 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002427 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002428 },
2429 {
2430 .name = "usb",
2431 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002432 .params = "",
2433 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002434 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002435 },
2436 {
2437 .name = "usbhost",
2438 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002439 .params = "",
2440 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002441 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002442 },
2443 {
2444 .name = "profile",
2445 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002446 .params = "",
2447 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002448 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002449 },
2450 {
2451 .name = "capture",
2452 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002453 .params = "",
2454 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002455 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002456 },
2457 {
2458 .name = "snapshots",
2459 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002460 .params = "",
2461 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002462 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002463 },
2464 {
2465 .name = "status",
2466 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 .params = "",
2468 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002469 .user_print = do_info_status_print,
2470 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002471 },
2472 {
2473 .name = "pcmcia",
2474 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002475 .params = "",
2476 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002477 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002478 },
2479 {
2480 .name = "mice",
2481 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002482 .params = "",
2483 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002484 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002485 },
2486 {
2487 .name = "vnc",
2488 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002489 .params = "",
2490 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002491 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002492 },
2493 {
2494 .name = "name",
2495 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002496 .params = "",
2497 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002498 .user_print = do_info_name_print,
2499 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002500 },
2501 {
2502 .name = "uuid",
2503 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002504 .params = "",
2505 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002506 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002507 },
j_mayer76a66252007-03-07 08:32:30 +00002508#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002509 {
2510 .name = "cpustats",
2511 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002512 .params = "",
2513 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002514 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002515 },
j_mayer76a66252007-03-07 08:32:30 +00002516#endif
blueswir131a60e22007-10-26 18:42:59 +00002517#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002518 {
2519 .name = "usernet",
2520 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 .params = "",
2522 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002523 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 },
blueswir131a60e22007-10-26 18:42:59 +00002525#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002526 {
2527 .name = "migrate",
2528 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002529 .params = "",
2530 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002531 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002532 },
2533 {
2534 .name = "balloon",
2535 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 .params = "",
2537 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002538 .user_print = monitor_print_balloon,
2539 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002540 },
2541 {
2542 .name = "qtree",
2543 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002544 .params = "",
2545 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002546 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002547 },
2548 {
2549 .name = "qdm",
2550 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002551 .params = "",
2552 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002553 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 },
2555 {
2556 .name = "roms",
2557 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 .params = "",
2559 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002560 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 },
2562 {
2563 .name = NULL,
2564 },
bellard9dc39cb2004-03-14 21:38:27 +00002565};
2566
bellard9307c4c2004-04-04 12:57:25 +00002567/*******************************************************************/
2568
2569static const char *pch;
2570static jmp_buf expr_env;
2571
bellard92a31b12005-02-10 22:00:52 +00002572#define MD_TLONG 0
2573#define MD_I32 1
2574
bellard9307c4c2004-04-04 12:57:25 +00002575typedef struct MonitorDef {
2576 const char *name;
2577 int offset;
blueswir18662d652008-10-02 18:32:44 +00002578 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002579 int type;
bellard9307c4c2004-04-04 12:57:25 +00002580} MonitorDef;
2581
bellard57206fd2004-04-25 18:54:52 +00002582#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002583static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002584{
bellard6a00d602005-11-21 23:25:50 +00002585 CPUState *env = mon_get_cpu();
2586 if (!env)
2587 return 0;
2588 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002589}
2590#endif
2591
bellarda541f292004-04-12 20:39:29 +00002592#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002593static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002594{
bellard6a00d602005-11-21 23:25:50 +00002595 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002596 unsigned int u;
2597 int i;
2598
bellard6a00d602005-11-21 23:25:50 +00002599 if (!env)
2600 return 0;
2601
bellarda541f292004-04-12 20:39:29 +00002602 u = 0;
2603 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002604 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002605
2606 return u;
2607}
2608
blueswir18662d652008-10-02 18:32:44 +00002609static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002610{
bellard6a00d602005-11-21 23:25:50 +00002611 CPUState *env = mon_get_cpu();
2612 if (!env)
2613 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002614 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002615}
2616
blueswir18662d652008-10-02 18:32:44 +00002617static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002618{
bellard6a00d602005-11-21 23:25:50 +00002619 CPUState *env = mon_get_cpu();
2620 if (!env)
2621 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002622 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002623}
bellard9fddaa02004-05-21 12:59:32 +00002624
blueswir18662d652008-10-02 18:32:44 +00002625static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002626{
bellard6a00d602005-11-21 23:25:50 +00002627 CPUState *env = mon_get_cpu();
2628 if (!env)
2629 return 0;
2630 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002631}
2632
blueswir18662d652008-10-02 18:32:44 +00002633static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002634{
bellard6a00d602005-11-21 23:25:50 +00002635 CPUState *env = mon_get_cpu();
2636 if (!env)
2637 return 0;
2638 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002639}
2640
blueswir18662d652008-10-02 18:32:44 +00002641static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002642{
bellard6a00d602005-11-21 23:25:50 +00002643 CPUState *env = mon_get_cpu();
2644 if (!env)
2645 return 0;
2646 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002647}
bellarda541f292004-04-12 20:39:29 +00002648#endif
2649
bellarde95c8d52004-09-30 22:22:08 +00002650#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002651#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002652static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002653{
bellard6a00d602005-11-21 23:25:50 +00002654 CPUState *env = mon_get_cpu();
2655 if (!env)
2656 return 0;
2657 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002658}
bellard7b936c02005-10-30 17:05:13 +00002659#endif
bellarde95c8d52004-09-30 22:22:08 +00002660
blueswir18662d652008-10-02 18:32:44 +00002661static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002662{
bellard6a00d602005-11-21 23:25:50 +00002663 CPUState *env = mon_get_cpu();
2664 if (!env)
2665 return 0;
2666 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002667}
2668#endif
2669
blueswir18662d652008-10-02 18:32:44 +00002670static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002671#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002672
2673#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002674 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002675 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002676 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002677
bellard9307c4c2004-04-04 12:57:25 +00002678 { "eax", offsetof(CPUState, regs[0]) },
2679 { "ecx", offsetof(CPUState, regs[1]) },
2680 { "edx", offsetof(CPUState, regs[2]) },
2681 { "ebx", offsetof(CPUState, regs[3]) },
2682 { "esp|sp", offsetof(CPUState, regs[4]) },
2683 { "ebp|fp", offsetof(CPUState, regs[5]) },
2684 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002685 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002686#ifdef TARGET_X86_64
2687 { "r8", offsetof(CPUState, regs[8]) },
2688 { "r9", offsetof(CPUState, regs[9]) },
2689 { "r10", offsetof(CPUState, regs[10]) },
2690 { "r11", offsetof(CPUState, regs[11]) },
2691 { "r12", offsetof(CPUState, regs[12]) },
2692 { "r13", offsetof(CPUState, regs[13]) },
2693 { "r14", offsetof(CPUState, regs[14]) },
2694 { "r15", offsetof(CPUState, regs[15]) },
2695#endif
bellard9307c4c2004-04-04 12:57:25 +00002696 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002697 { "eip", offsetof(CPUState, eip) },
2698 SEG("cs", R_CS)
2699 SEG("ds", R_DS)
2700 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002701 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002702 SEG("fs", R_FS)
2703 SEG("gs", R_GS)
2704 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002705#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002706 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002707 { "r0", offsetof(CPUState, gpr[0]) },
2708 { "r1", offsetof(CPUState, gpr[1]) },
2709 { "r2", offsetof(CPUState, gpr[2]) },
2710 { "r3", offsetof(CPUState, gpr[3]) },
2711 { "r4", offsetof(CPUState, gpr[4]) },
2712 { "r5", offsetof(CPUState, gpr[5]) },
2713 { "r6", offsetof(CPUState, gpr[6]) },
2714 { "r7", offsetof(CPUState, gpr[7]) },
2715 { "r8", offsetof(CPUState, gpr[8]) },
2716 { "r9", offsetof(CPUState, gpr[9]) },
2717 { "r10", offsetof(CPUState, gpr[10]) },
2718 { "r11", offsetof(CPUState, gpr[11]) },
2719 { "r12", offsetof(CPUState, gpr[12]) },
2720 { "r13", offsetof(CPUState, gpr[13]) },
2721 { "r14", offsetof(CPUState, gpr[14]) },
2722 { "r15", offsetof(CPUState, gpr[15]) },
2723 { "r16", offsetof(CPUState, gpr[16]) },
2724 { "r17", offsetof(CPUState, gpr[17]) },
2725 { "r18", offsetof(CPUState, gpr[18]) },
2726 { "r19", offsetof(CPUState, gpr[19]) },
2727 { "r20", offsetof(CPUState, gpr[20]) },
2728 { "r21", offsetof(CPUState, gpr[21]) },
2729 { "r22", offsetof(CPUState, gpr[22]) },
2730 { "r23", offsetof(CPUState, gpr[23]) },
2731 { "r24", offsetof(CPUState, gpr[24]) },
2732 { "r25", offsetof(CPUState, gpr[25]) },
2733 { "r26", offsetof(CPUState, gpr[26]) },
2734 { "r27", offsetof(CPUState, gpr[27]) },
2735 { "r28", offsetof(CPUState, gpr[28]) },
2736 { "r29", offsetof(CPUState, gpr[29]) },
2737 { "r30", offsetof(CPUState, gpr[30]) },
2738 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002739 /* Floating point registers */
2740 { "f0", offsetof(CPUState, fpr[0]) },
2741 { "f1", offsetof(CPUState, fpr[1]) },
2742 { "f2", offsetof(CPUState, fpr[2]) },
2743 { "f3", offsetof(CPUState, fpr[3]) },
2744 { "f4", offsetof(CPUState, fpr[4]) },
2745 { "f5", offsetof(CPUState, fpr[5]) },
2746 { "f6", offsetof(CPUState, fpr[6]) },
2747 { "f7", offsetof(CPUState, fpr[7]) },
2748 { "f8", offsetof(CPUState, fpr[8]) },
2749 { "f9", offsetof(CPUState, fpr[9]) },
2750 { "f10", offsetof(CPUState, fpr[10]) },
2751 { "f11", offsetof(CPUState, fpr[11]) },
2752 { "f12", offsetof(CPUState, fpr[12]) },
2753 { "f13", offsetof(CPUState, fpr[13]) },
2754 { "f14", offsetof(CPUState, fpr[14]) },
2755 { "f15", offsetof(CPUState, fpr[15]) },
2756 { "f16", offsetof(CPUState, fpr[16]) },
2757 { "f17", offsetof(CPUState, fpr[17]) },
2758 { "f18", offsetof(CPUState, fpr[18]) },
2759 { "f19", offsetof(CPUState, fpr[19]) },
2760 { "f20", offsetof(CPUState, fpr[20]) },
2761 { "f21", offsetof(CPUState, fpr[21]) },
2762 { "f22", offsetof(CPUState, fpr[22]) },
2763 { "f23", offsetof(CPUState, fpr[23]) },
2764 { "f24", offsetof(CPUState, fpr[24]) },
2765 { "f25", offsetof(CPUState, fpr[25]) },
2766 { "f26", offsetof(CPUState, fpr[26]) },
2767 { "f27", offsetof(CPUState, fpr[27]) },
2768 { "f28", offsetof(CPUState, fpr[28]) },
2769 { "f29", offsetof(CPUState, fpr[29]) },
2770 { "f30", offsetof(CPUState, fpr[30]) },
2771 { "f31", offsetof(CPUState, fpr[31]) },
2772 { "fpscr", offsetof(CPUState, fpscr) },
2773 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002774 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002775 { "lr", offsetof(CPUState, lr) },
2776 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002777 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002778 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002779 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002780 { "msr", 0, &monitor_get_msr, },
2781 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002782 { "tbu", 0, &monitor_get_tbu, },
2783 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002784#if defined(TARGET_PPC64)
2785 /* Address space register */
2786 { "asr", offsetof(CPUState, asr) },
2787#endif
2788 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002789 { "sdr1", offsetof(CPUState, sdr1) },
2790 { "sr0", offsetof(CPUState, sr[0]) },
2791 { "sr1", offsetof(CPUState, sr[1]) },
2792 { "sr2", offsetof(CPUState, sr[2]) },
2793 { "sr3", offsetof(CPUState, sr[3]) },
2794 { "sr4", offsetof(CPUState, sr[4]) },
2795 { "sr5", offsetof(CPUState, sr[5]) },
2796 { "sr6", offsetof(CPUState, sr[6]) },
2797 { "sr7", offsetof(CPUState, sr[7]) },
2798 { "sr8", offsetof(CPUState, sr[8]) },
2799 { "sr9", offsetof(CPUState, sr[9]) },
2800 { "sr10", offsetof(CPUState, sr[10]) },
2801 { "sr11", offsetof(CPUState, sr[11]) },
2802 { "sr12", offsetof(CPUState, sr[12]) },
2803 { "sr13", offsetof(CPUState, sr[13]) },
2804 { "sr14", offsetof(CPUState, sr[14]) },
2805 { "sr15", offsetof(CPUState, sr[15]) },
2806 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002807#elif defined(TARGET_SPARC)
2808 { "g0", offsetof(CPUState, gregs[0]) },
2809 { "g1", offsetof(CPUState, gregs[1]) },
2810 { "g2", offsetof(CPUState, gregs[2]) },
2811 { "g3", offsetof(CPUState, gregs[3]) },
2812 { "g4", offsetof(CPUState, gregs[4]) },
2813 { "g5", offsetof(CPUState, gregs[5]) },
2814 { "g6", offsetof(CPUState, gregs[6]) },
2815 { "g7", offsetof(CPUState, gregs[7]) },
2816 { "o0", 0, monitor_get_reg },
2817 { "o1", 1, monitor_get_reg },
2818 { "o2", 2, monitor_get_reg },
2819 { "o3", 3, monitor_get_reg },
2820 { "o4", 4, monitor_get_reg },
2821 { "o5", 5, monitor_get_reg },
2822 { "o6", 6, monitor_get_reg },
2823 { "o7", 7, monitor_get_reg },
2824 { "l0", 8, monitor_get_reg },
2825 { "l1", 9, monitor_get_reg },
2826 { "l2", 10, monitor_get_reg },
2827 { "l3", 11, monitor_get_reg },
2828 { "l4", 12, monitor_get_reg },
2829 { "l5", 13, monitor_get_reg },
2830 { "l6", 14, monitor_get_reg },
2831 { "l7", 15, monitor_get_reg },
2832 { "i0", 16, monitor_get_reg },
2833 { "i1", 17, monitor_get_reg },
2834 { "i2", 18, monitor_get_reg },
2835 { "i3", 19, monitor_get_reg },
2836 { "i4", 20, monitor_get_reg },
2837 { "i5", 21, monitor_get_reg },
2838 { "i6", 22, monitor_get_reg },
2839 { "i7", 23, monitor_get_reg },
2840 { "pc", offsetof(CPUState, pc) },
2841 { "npc", offsetof(CPUState, npc) },
2842 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002843#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002844 { "psr", 0, &monitor_get_psr, },
2845 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002846#endif
bellarde95c8d52004-09-30 22:22:08 +00002847 { "tbr", offsetof(CPUState, tbr) },
2848 { "fsr", offsetof(CPUState, fsr) },
2849 { "f0", offsetof(CPUState, fpr[0]) },
2850 { "f1", offsetof(CPUState, fpr[1]) },
2851 { "f2", offsetof(CPUState, fpr[2]) },
2852 { "f3", offsetof(CPUState, fpr[3]) },
2853 { "f4", offsetof(CPUState, fpr[4]) },
2854 { "f5", offsetof(CPUState, fpr[5]) },
2855 { "f6", offsetof(CPUState, fpr[6]) },
2856 { "f7", offsetof(CPUState, fpr[7]) },
2857 { "f8", offsetof(CPUState, fpr[8]) },
2858 { "f9", offsetof(CPUState, fpr[9]) },
2859 { "f10", offsetof(CPUState, fpr[10]) },
2860 { "f11", offsetof(CPUState, fpr[11]) },
2861 { "f12", offsetof(CPUState, fpr[12]) },
2862 { "f13", offsetof(CPUState, fpr[13]) },
2863 { "f14", offsetof(CPUState, fpr[14]) },
2864 { "f15", offsetof(CPUState, fpr[15]) },
2865 { "f16", offsetof(CPUState, fpr[16]) },
2866 { "f17", offsetof(CPUState, fpr[17]) },
2867 { "f18", offsetof(CPUState, fpr[18]) },
2868 { "f19", offsetof(CPUState, fpr[19]) },
2869 { "f20", offsetof(CPUState, fpr[20]) },
2870 { "f21", offsetof(CPUState, fpr[21]) },
2871 { "f22", offsetof(CPUState, fpr[22]) },
2872 { "f23", offsetof(CPUState, fpr[23]) },
2873 { "f24", offsetof(CPUState, fpr[24]) },
2874 { "f25", offsetof(CPUState, fpr[25]) },
2875 { "f26", offsetof(CPUState, fpr[26]) },
2876 { "f27", offsetof(CPUState, fpr[27]) },
2877 { "f28", offsetof(CPUState, fpr[28]) },
2878 { "f29", offsetof(CPUState, fpr[29]) },
2879 { "f30", offsetof(CPUState, fpr[30]) },
2880 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002881#ifdef TARGET_SPARC64
2882 { "f32", offsetof(CPUState, fpr[32]) },
2883 { "f34", offsetof(CPUState, fpr[34]) },
2884 { "f36", offsetof(CPUState, fpr[36]) },
2885 { "f38", offsetof(CPUState, fpr[38]) },
2886 { "f40", offsetof(CPUState, fpr[40]) },
2887 { "f42", offsetof(CPUState, fpr[42]) },
2888 { "f44", offsetof(CPUState, fpr[44]) },
2889 { "f46", offsetof(CPUState, fpr[46]) },
2890 { "f48", offsetof(CPUState, fpr[48]) },
2891 { "f50", offsetof(CPUState, fpr[50]) },
2892 { "f52", offsetof(CPUState, fpr[52]) },
2893 { "f54", offsetof(CPUState, fpr[54]) },
2894 { "f56", offsetof(CPUState, fpr[56]) },
2895 { "f58", offsetof(CPUState, fpr[58]) },
2896 { "f60", offsetof(CPUState, fpr[60]) },
2897 { "f62", offsetof(CPUState, fpr[62]) },
2898 { "asi", offsetof(CPUState, asi) },
2899 { "pstate", offsetof(CPUState, pstate) },
2900 { "cansave", offsetof(CPUState, cansave) },
2901 { "canrestore", offsetof(CPUState, canrestore) },
2902 { "otherwin", offsetof(CPUState, otherwin) },
2903 { "wstate", offsetof(CPUState, wstate) },
2904 { "cleanwin", offsetof(CPUState, cleanwin) },
2905 { "fprs", offsetof(CPUState, fprs) },
2906#endif
bellard9307c4c2004-04-04 12:57:25 +00002907#endif
2908 { NULL },
2909};
2910
aliguori376253e2009-03-05 23:01:23 +00002911static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002912{
aliguori376253e2009-03-05 23:01:23 +00002913 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002914 longjmp(expr_env, 1);
2915}
2916
bellard6a00d602005-11-21 23:25:50 +00002917/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002918static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002919{
blueswir18662d652008-10-02 18:32:44 +00002920 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002921 void *ptr;
2922
bellard9307c4c2004-04-04 12:57:25 +00002923 for(md = monitor_defs; md->name != NULL; md++) {
2924 if (compare_cmd(name, md->name)) {
2925 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002926 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002927 } else {
bellard6a00d602005-11-21 23:25:50 +00002928 CPUState *env = mon_get_cpu();
2929 if (!env)
2930 return -2;
2931 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002932 switch(md->type) {
2933 case MD_I32:
2934 *pval = *(int32_t *)ptr;
2935 break;
2936 case MD_TLONG:
2937 *pval = *(target_long *)ptr;
2938 break;
2939 default:
2940 *pval = 0;
2941 break;
2942 }
bellard9307c4c2004-04-04 12:57:25 +00002943 }
2944 return 0;
2945 }
2946 }
2947 return -1;
2948}
2949
2950static void next(void)
2951{
Blue Swirl660f11b2009-07-31 21:16:51 +00002952 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002953 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002954 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002955 pch++;
2956 }
2957}
2958
aliguori376253e2009-03-05 23:01:23 +00002959static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002960
aliguori376253e2009-03-05 23:01:23 +00002961static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002962{
blueswir1c2efc952007-09-25 17:28:42 +00002963 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002964 char *p;
bellard6a00d602005-11-21 23:25:50 +00002965 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002966
2967 switch(*pch) {
2968 case '+':
2969 next();
aliguori376253e2009-03-05 23:01:23 +00002970 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002971 break;
2972 case '-':
2973 next();
aliguori376253e2009-03-05 23:01:23 +00002974 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002975 break;
2976 case '~':
2977 next();
aliguori376253e2009-03-05 23:01:23 +00002978 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002979 break;
2980 case '(':
2981 next();
aliguori376253e2009-03-05 23:01:23 +00002982 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002983 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002984 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002985 }
2986 next();
2987 break;
bellard81d09122004-07-14 17:21:37 +00002988 case '\'':
2989 pch++;
2990 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002991 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002992 n = *pch;
2993 pch++;
2994 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002995 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002996 next();
2997 break;
bellard9307c4c2004-04-04 12:57:25 +00002998 case '$':
2999 {
3000 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003001 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003002
bellard9307c4c2004-04-04 12:57:25 +00003003 pch++;
3004 q = buf;
3005 while ((*pch >= 'a' && *pch <= 'z') ||
3006 (*pch >= 'A' && *pch <= 'Z') ||
3007 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003008 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003009 if ((q - buf) < sizeof(buf) - 1)
3010 *q++ = *pch;
3011 pch++;
3012 }
blueswir1cd390082008-11-16 13:53:32 +00003013 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003014 pch++;
3015 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003016 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00003017 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00003018 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00003019 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00003020 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00003021 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003022 }
3023 break;
3024 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003025 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003026 n = 0;
3027 break;
3028 default:
blueswir17743e582007-09-24 18:39:04 +00003029#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003030 n = strtoull(pch, &p, 0);
3031#else
bellard9307c4c2004-04-04 12:57:25 +00003032 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003033#endif
bellard9307c4c2004-04-04 12:57:25 +00003034 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003035 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003036 }
3037 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003038 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003039 pch++;
3040 break;
3041 }
3042 return n;
3043}
3044
3045
aliguori376253e2009-03-05 23:01:23 +00003046static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003047{
blueswir1c2efc952007-09-25 17:28:42 +00003048 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003049 int op;
ths3b46e622007-09-17 08:09:54 +00003050
aliguori376253e2009-03-05 23:01:23 +00003051 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003052 for(;;) {
3053 op = *pch;
3054 if (op != '*' && op != '/' && op != '%')
3055 break;
3056 next();
aliguori376253e2009-03-05 23:01:23 +00003057 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003058 switch(op) {
3059 default:
3060 case '*':
3061 val *= val2;
3062 break;
3063 case '/':
3064 case '%':
ths5fafdf22007-09-16 21:08:06 +00003065 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003066 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003067 if (op == '/')
3068 val /= val2;
3069 else
3070 val %= val2;
3071 break;
3072 }
3073 }
3074 return val;
3075}
3076
aliguori376253e2009-03-05 23:01:23 +00003077static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003078{
blueswir1c2efc952007-09-25 17:28:42 +00003079 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003080 int op;
bellard9307c4c2004-04-04 12:57:25 +00003081
aliguori376253e2009-03-05 23:01:23 +00003082 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003083 for(;;) {
3084 op = *pch;
3085 if (op != '&' && op != '|' && op != '^')
3086 break;
3087 next();
aliguori376253e2009-03-05 23:01:23 +00003088 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003089 switch(op) {
3090 default:
3091 case '&':
3092 val &= val2;
3093 break;
3094 case '|':
3095 val |= val2;
3096 break;
3097 case '^':
3098 val ^= val2;
3099 break;
3100 }
3101 }
3102 return val;
3103}
3104
aliguori376253e2009-03-05 23:01:23 +00003105static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003106{
blueswir1c2efc952007-09-25 17:28:42 +00003107 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003108 int op;
bellard9307c4c2004-04-04 12:57:25 +00003109
aliguori376253e2009-03-05 23:01:23 +00003110 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003111 for(;;) {
3112 op = *pch;
3113 if (op != '+' && op != '-')
3114 break;
3115 next();
aliguori376253e2009-03-05 23:01:23 +00003116 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003117 if (op == '+')
3118 val += val2;
3119 else
3120 val -= val2;
3121 }
3122 return val;
3123}
3124
aliguori376253e2009-03-05 23:01:23 +00003125static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003126{
3127 pch = *pp;
3128 if (setjmp(expr_env)) {
3129 *pp = pch;
3130 return -1;
3131 }
blueswir1cd390082008-11-16 13:53:32 +00003132 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003133 pch++;
aliguori376253e2009-03-05 23:01:23 +00003134 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003135 *pp = pch;
3136 return 0;
3137}
3138
3139static int get_str(char *buf, int buf_size, const char **pp)
3140{
3141 const char *p;
3142 char *q;
3143 int c;
3144
bellard81d09122004-07-14 17:21:37 +00003145 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003146 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003147 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003148 p++;
3149 if (*p == '\0') {
3150 fail:
bellard81d09122004-07-14 17:21:37 +00003151 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003152 *pp = p;
3153 return -1;
3154 }
bellard9307c4c2004-04-04 12:57:25 +00003155 if (*p == '\"') {
3156 p++;
3157 while (*p != '\0' && *p != '\"') {
3158 if (*p == '\\') {
3159 p++;
3160 c = *p++;
3161 switch(c) {
3162 case 'n':
3163 c = '\n';
3164 break;
3165 case 'r':
3166 c = '\r';
3167 break;
3168 case '\\':
3169 case '\'':
3170 case '\"':
3171 break;
3172 default:
3173 qemu_printf("unsupported escape code: '\\%c'\n", c);
3174 goto fail;
3175 }
3176 if ((q - buf) < buf_size - 1) {
3177 *q++ = c;
3178 }
3179 } else {
3180 if ((q - buf) < buf_size - 1) {
3181 *q++ = *p;
3182 }
3183 p++;
3184 }
3185 }
3186 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003187 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003188 goto fail;
3189 }
3190 p++;
3191 } else {
blueswir1cd390082008-11-16 13:53:32 +00003192 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003193 if ((q - buf) < buf_size - 1) {
3194 *q++ = *p;
3195 }
3196 p++;
3197 }
bellard9307c4c2004-04-04 12:57:25 +00003198 }
bellard81d09122004-07-14 17:21:37 +00003199 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003200 *pp = p;
3201 return 0;
3202}
3203
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003204/*
3205 * Store the command-name in cmdname, and return a pointer to
3206 * the remaining of the command string.
3207 */
3208static const char *get_command_name(const char *cmdline,
3209 char *cmdname, size_t nlen)
3210{
3211 size_t len;
3212 const char *p, *pstart;
3213
3214 p = cmdline;
3215 while (qemu_isspace(*p))
3216 p++;
3217 if (*p == '\0')
3218 return NULL;
3219 pstart = p;
3220 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3221 p++;
3222 len = p - pstart;
3223 if (len > nlen - 1)
3224 len = nlen - 1;
3225 memcpy(cmdname, pstart, len);
3226 cmdname[len] = '\0';
3227 return p;
3228}
3229
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003230/**
3231 * Read key of 'type' into 'key' and return the current
3232 * 'type' pointer.
3233 */
3234static char *key_get_info(const char *type, char **key)
3235{
3236 size_t len;
3237 char *p, *str;
3238
3239 if (*type == ',')
3240 type++;
3241
3242 p = strchr(type, ':');
3243 if (!p) {
3244 *key = NULL;
3245 return NULL;
3246 }
3247 len = p - type;
3248
3249 str = qemu_malloc(len + 1);
3250 memcpy(str, type, len);
3251 str[len] = '\0';
3252
3253 *key = str;
3254 return ++p;
3255}
3256
bellard9307c4c2004-04-04 12:57:25 +00003257static int default_fmt_format = 'x';
3258static int default_fmt_size = 4;
3259
3260#define MAX_ARGS 16
3261
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003262static int is_valid_option(const char *c, const char *typestr)
3263{
3264 char option[3];
3265
3266 option[0] = '-';
3267 option[1] = *c;
3268 option[2] = '\0';
3269
3270 typestr = strstr(typestr, option);
3271 return (typestr != NULL);
3272}
3273
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003274static const mon_cmd_t *monitor_find_command(const char *cmdname)
3275{
3276 const mon_cmd_t *cmd;
3277
3278 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3279 if (compare_cmd(cmdname, cmd->name)) {
3280 return cmd;
3281 }
3282 }
3283
3284 return NULL;
3285}
3286
Anthony Liguoric227f092009-10-01 16:12:16 -05003287static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003288 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003289 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003290{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003291 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003292 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003293 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003294 char cmdname[256];
3295 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003296 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003297
3298#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003299 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003300#endif
ths3b46e622007-09-17 08:09:54 +00003301
bellard9307c4c2004-04-04 12:57:25 +00003302 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003303 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3304 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003305 return NULL;
ths3b46e622007-09-17 08:09:54 +00003306
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003307 cmd = monitor_find_command(cmdname);
3308 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003309 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003310 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003311 }
bellard9307c4c2004-04-04 12:57:25 +00003312
bellard9307c4c2004-04-04 12:57:25 +00003313 /* parse the parameters */
3314 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003315 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003316 typestr = key_get_info(typestr, &key);
3317 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003318 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003319 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003320 typestr++;
3321 switch(c) {
3322 case 'F':
bellard81d09122004-07-14 17:21:37 +00003323 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003324 case 's':
3325 {
3326 int ret;
ths3b46e622007-09-17 08:09:54 +00003327
blueswir1cd390082008-11-16 13:53:32 +00003328 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003329 p++;
3330 if (*typestr == '?') {
3331 typestr++;
3332 if (*p == '\0') {
3333 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003334 break;
bellard9307c4c2004-04-04 12:57:25 +00003335 }
3336 }
3337 ret = get_str(buf, sizeof(buf), &p);
3338 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003339 switch(c) {
3340 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003341 monitor_printf(mon, "%s: filename expected\n",
3342 cmdname);
bellard81d09122004-07-14 17:21:37 +00003343 break;
3344 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003345 monitor_printf(mon, "%s: block device name expected\n",
3346 cmdname);
bellard81d09122004-07-14 17:21:37 +00003347 break;
3348 default:
aliguori376253e2009-03-05 23:01:23 +00003349 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003350 break;
3351 }
bellard9307c4c2004-04-04 12:57:25 +00003352 goto fail;
3353 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003354 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003355 }
3356 break;
3357 case '/':
3358 {
3359 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003360
blueswir1cd390082008-11-16 13:53:32 +00003361 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003362 p++;
3363 if (*p == '/') {
3364 /* format found */
3365 p++;
3366 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003367 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003368 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003369 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003370 count = count * 10 + (*p - '0');
3371 p++;
3372 }
3373 }
3374 size = -1;
3375 format = -1;
3376 for(;;) {
3377 switch(*p) {
3378 case 'o':
3379 case 'd':
3380 case 'u':
3381 case 'x':
3382 case 'i':
3383 case 'c':
3384 format = *p++;
3385 break;
3386 case 'b':
3387 size = 1;
3388 p++;
3389 break;
3390 case 'h':
3391 size = 2;
3392 p++;
3393 break;
3394 case 'w':
3395 size = 4;
3396 p++;
3397 break;
3398 case 'g':
3399 case 'L':
3400 size = 8;
3401 p++;
3402 break;
3403 default:
3404 goto next;
3405 }
3406 }
3407 next:
blueswir1cd390082008-11-16 13:53:32 +00003408 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003409 monitor_printf(mon, "invalid char in format: '%c'\n",
3410 *p);
bellard9307c4c2004-04-04 12:57:25 +00003411 goto fail;
3412 }
bellard9307c4c2004-04-04 12:57:25 +00003413 if (format < 0)
3414 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003415 if (format != 'i') {
3416 /* for 'i', not specifying a size gives -1 as size */
3417 if (size < 0)
3418 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003419 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003420 }
bellard9307c4c2004-04-04 12:57:25 +00003421 default_fmt_format = format;
3422 } else {
3423 count = 1;
3424 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003425 if (format != 'i') {
3426 size = default_fmt_size;
3427 } else {
3428 size = -1;
3429 }
bellard9307c4c2004-04-04 12:57:25 +00003430 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003431 qdict_put(qdict, "count", qint_from_int(count));
3432 qdict_put(qdict, "format", qint_from_int(format));
3433 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003434 }
3435 break;
3436 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003437 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003438 {
blueswir1c2efc952007-09-25 17:28:42 +00003439 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003440
blueswir1cd390082008-11-16 13:53:32 +00003441 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003442 p++;
bellard34405572004-06-08 00:55:58 +00003443 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003444 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003445 if (*p == '\0') {
3446 typestr++;
3447 break;
3448 }
bellard34405572004-06-08 00:55:58 +00003449 } else {
3450 if (*p == '.') {
3451 p++;
blueswir1cd390082008-11-16 13:53:32 +00003452 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003453 p++;
bellard34405572004-06-08 00:55:58 +00003454 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003455 typestr++;
3456 break;
bellard34405572004-06-08 00:55:58 +00003457 }
3458 }
bellard13224a82006-07-14 22:03:35 +00003459 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003460 }
aliguori376253e2009-03-05 23:01:23 +00003461 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003462 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003463 /* Check if 'i' is greater than 32-bit */
3464 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3465 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3466 monitor_printf(mon, "integer is for 32-bit values\n");
3467 goto fail;
3468 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003469 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003470 }
3471 break;
3472 case '-':
3473 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003474 const char *tmp = p;
3475 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003476 /* option */
ths3b46e622007-09-17 08:09:54 +00003477
bellard9307c4c2004-04-04 12:57:25 +00003478 c = *typestr++;
3479 if (c == '\0')
3480 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003481 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003482 p++;
3483 has_option = 0;
3484 if (*p == '-') {
3485 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003486 if(c != *p) {
3487 if(!is_valid_option(p, typestr)) {
3488
3489 monitor_printf(mon, "%s: unsupported option -%c\n",
3490 cmdname, *p);
3491 goto fail;
3492 } else {
3493 skip_key = 1;
3494 }
bellard9307c4c2004-04-04 12:57:25 +00003495 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003496 if(skip_key) {
3497 p = tmp;
3498 } else {
3499 p++;
3500 has_option = 1;
3501 }
bellard9307c4c2004-04-04 12:57:25 +00003502 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003503 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003504 }
3505 break;
3506 default:
3507 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003508 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003509 goto fail;
3510 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003511 qemu_free(key);
3512 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003513 }
3514 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003515 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003516 p++;
3517 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003518 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3519 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003520 goto fail;
3521 }
3522
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003523 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003524
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003525fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003526 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003527 return NULL;
3528}
3529
Luiz Capitulino8204a912009-11-18 23:05:31 -02003530static void monitor_print_error(Monitor *mon)
3531{
3532 qerror_print(mon->error);
3533 QDECREF(mon->error);
3534 mon->error = NULL;
3535}
3536
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003537static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3538 const QDict *params)
3539{
3540 QObject *data = NULL;
3541
3542 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003543
3544 if (monitor_ctrl_mode(mon)) {
3545 /* Monitor Protocol */
3546 monitor_protocol_emitter(mon, data);
3547 } else {
3548 /* User Protocol */
3549 if (data)
3550 cmd->user_print(mon, data);
3551 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003552
3553 qobject_decref(data);
3554}
3555
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003556static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003557{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003558 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003559 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003560
3561 qdict = qdict_new();
3562
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003563 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003564 if (!cmd)
3565 goto out;
3566
3567 qemu_errors_to_mon(mon);
3568
3569 if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003570 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003571 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003572 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003573 }
3574
Luiz Capitulino8204a912009-11-18 23:05:31 -02003575 if (monitor_has_error(mon))
3576 monitor_print_error(mon);
3577
3578 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003579
3580out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003581 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003582}
3583
bellard81d09122004-07-14 17:21:37 +00003584static void cmd_completion(const char *name, const char *list)
3585{
3586 const char *p, *pstart;
3587 char cmd[128];
3588 int len;
3589
3590 p = list;
3591 for(;;) {
3592 pstart = p;
3593 p = strchr(p, '|');
3594 if (!p)
3595 p = pstart + strlen(pstart);
3596 len = p - pstart;
3597 if (len > sizeof(cmd) - 2)
3598 len = sizeof(cmd) - 2;
3599 memcpy(cmd, pstart, len);
3600 cmd[len] = '\0';
3601 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003602 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003603 }
3604 if (*p == '\0')
3605 break;
3606 p++;
3607 }
3608}
3609
3610static void file_completion(const char *input)
3611{
3612 DIR *ffs;
3613 struct dirent *d;
3614 char path[1024];
3615 char file[1024], file_prefix[1024];
3616 int input_path_len;
3617 const char *p;
3618
ths5fafdf22007-09-16 21:08:06 +00003619 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003620 if (!p) {
3621 input_path_len = 0;
3622 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003623 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003624 } else {
3625 input_path_len = p - input + 1;
3626 memcpy(path, input, input_path_len);
3627 if (input_path_len > sizeof(path) - 1)
3628 input_path_len = sizeof(path) - 1;
3629 path[input_path_len] = '\0';
3630 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3631 }
3632#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003633 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3634 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003635#endif
3636 ffs = opendir(path);
3637 if (!ffs)
3638 return;
3639 for(;;) {
3640 struct stat sb;
3641 d = readdir(ffs);
3642 if (!d)
3643 break;
3644 if (strstart(d->d_name, file_prefix, NULL)) {
3645 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003646 if (input_path_len < sizeof(file))
3647 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3648 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003649 /* stat the file to find out if it's a directory.
3650 * In that case add a slash to speed up typing long paths
3651 */
3652 stat(file, &sb);
3653 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003654 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003655 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003656 }
3657 }
3658 closedir(ffs);
3659}
3660
aliguori51de9762009-03-05 23:00:43 +00003661static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003662{
aliguori51de9762009-03-05 23:00:43 +00003663 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003664 const char *input = opaque;
3665
3666 if (input[0] == '\0' ||
3667 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003668 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003669 }
3670}
3671
3672/* NOTE: this parser is an approximate form of the real command parser */
3673static void parse_cmdline(const char *cmdline,
3674 int *pnb_args, char **args)
3675{
3676 const char *p;
3677 int nb_args, ret;
3678 char buf[1024];
3679
3680 p = cmdline;
3681 nb_args = 0;
3682 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003683 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003684 p++;
3685 if (*p == '\0')
3686 break;
3687 if (nb_args >= MAX_ARGS)
3688 break;
3689 ret = get_str(buf, sizeof(buf), &p);
3690 args[nb_args] = qemu_strdup(buf);
3691 nb_args++;
3692 if (ret < 0)
3693 break;
3694 }
3695 *pnb_args = nb_args;
3696}
3697
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003698static const char *next_arg_type(const char *typestr)
3699{
3700 const char *p = strchr(typestr, ':');
3701 return (p != NULL ? ++p : typestr);
3702}
3703
aliguori4c36ba32009-03-05 23:01:37 +00003704static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003705{
3706 const char *cmdname;
3707 char *args[MAX_ARGS];
3708 int nb_args, i, len;
3709 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003710 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003711 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003712
3713 parse_cmdline(cmdline, &nb_args, args);
3714#ifdef DEBUG_COMPLETION
3715 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003716 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003717 }
3718#endif
3719
3720 /* if the line ends with a space, it means we want to complete the
3721 next arg */
3722 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003723 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003724 if (nb_args >= MAX_ARGS)
3725 return;
3726 args[nb_args++] = qemu_strdup("");
3727 }
3728 if (nb_args <= 1) {
3729 /* command completion */
3730 if (nb_args == 0)
3731 cmdname = "";
3732 else
3733 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003734 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003735 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003736 cmd_completion(cmdname, cmd->name);
3737 }
3738 } else {
3739 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003740 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003741 if (compare_cmd(args[0], cmd->name))
3742 goto found;
3743 }
3744 return;
3745 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003746 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003747 for(i = 0; i < nb_args - 2; i++) {
3748 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003749 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003750 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003751 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003752 }
3753 }
3754 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003755 if (*ptype == '-' && ptype[1] != '\0') {
3756 ptype += 2;
3757 }
bellard81d09122004-07-14 17:21:37 +00003758 switch(*ptype) {
3759 case 'F':
3760 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003761 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003762 file_completion(str);
3763 break;
3764 case 'B':
3765 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003766 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003767 bdrv_iterate(block_completion_it, (void *)str);
3768 break;
bellard7fe48482004-10-09 18:08:01 +00003769 case 's':
3770 /* XXX: more generic ? */
3771 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003772 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003773 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3774 cmd_completion(str, cmd->name);
3775 }
bellard64866c32006-05-07 18:03:31 +00003776 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003777 char *sep = strrchr(str, '-');
3778 if (sep)
3779 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003780 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003781 for(key = key_defs; key->name != NULL; key++) {
3782 cmd_completion(str, key->name);
3783 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003784 } else if (!strcmp(cmd->name, "help|?")) {
3785 readline_set_completion_index(cur_mon->rs, strlen(str));
3786 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3787 cmd_completion(str, cmd->name);
3788 }
bellard7fe48482004-10-09 18:08:01 +00003789 }
3790 break;
bellard81d09122004-07-14 17:21:37 +00003791 default:
3792 break;
3793 }
3794 }
3795 for(i = 0; i < nb_args; i++)
3796 qemu_free(args[i]);
3797}
3798
aliguori731b0362009-03-05 23:01:42 +00003799static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003800{
aliguori731b0362009-03-05 23:01:42 +00003801 Monitor *mon = opaque;
3802
3803 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003804}
3805
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003806typedef struct CmdArgs {
3807 QString *name;
3808 int type;
3809 int flag;
3810 int optional;
3811} CmdArgs;
3812
3813static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
3814{
3815 if (!cmd_args->optional) {
3816 qemu_error_new(QERR_MISSING_PARAMETER, name);
3817 return -1;
3818 }
3819
3820 if (cmd_args->type == '-') {
3821 /* handlers expect a value, they need to be changed */
3822 qdict_put(args, name, qint_from_int(0));
3823 }
3824
3825 return 0;
3826}
3827
3828static int check_arg(const CmdArgs *cmd_args, QDict *args)
3829{
3830 QObject *value;
3831 const char *name;
3832
3833 name = qstring_get_str(cmd_args->name);
3834
3835 if (!args) {
3836 return check_opt(cmd_args, name, args);
3837 }
3838
3839 value = qdict_get(args, name);
3840 if (!value) {
3841 return check_opt(cmd_args, name, args);
3842 }
3843
3844 switch (cmd_args->type) {
3845 case 'F':
3846 case 'B':
3847 case 's':
3848 if (qobject_type(value) != QTYPE_QSTRING) {
3849 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
3850 return -1;
3851 }
3852 break;
3853 case '/': {
3854 int i;
3855 const char *keys[] = { "count", "format", "size", NULL };
3856
3857 for (i = 0; keys[i]; i++) {
3858 QObject *obj = qdict_get(args, keys[i]);
3859 if (!obj) {
3860 qemu_error_new(QERR_MISSING_PARAMETER, name);
3861 return -1;
3862 }
3863 if (qobject_type(obj) != QTYPE_QINT) {
3864 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
3865 return -1;
3866 }
3867 }
3868 break;
3869 }
3870 case 'i':
3871 case 'l':
3872 if (qobject_type(value) != QTYPE_QINT) {
3873 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
3874 return -1;
3875 }
3876 break;
3877 case '-':
3878 if (qobject_type(value) != QTYPE_QINT &&
3879 qobject_type(value) != QTYPE_QBOOL) {
3880 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
3881 return -1;
3882 }
3883 if (qobject_type(value) == QTYPE_QBOOL) {
3884 /* handlers expect a QInt, they need to be changed */
3885 qdict_put(args, name,
3886 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
3887 }
3888 break;
3889 default:
3890 /* impossible */
3891 abort();
3892 }
3893
3894 return 0;
3895}
3896
3897static void cmd_args_init(CmdArgs *cmd_args)
3898{
3899 cmd_args->name = qstring_new();
3900 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
3901}
3902
3903/*
3904 * This is not trivial, we have to parse Monitor command's argument
3905 * type syntax to be able to check the arguments provided by clients.
3906 *
3907 * In the near future we will be using an array for that and will be
3908 * able to drop all this parsing...
3909 */
3910static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
3911{
3912 int err;
3913 const char *p;
3914 CmdArgs cmd_args;
3915
Blue Swirldd5121b2009-12-04 20:52:02 +00003916 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003917 return (qdict_size(args) == 0 ? 0 : -1);
3918 }
3919
3920 err = 0;
3921 cmd_args_init(&cmd_args);
3922
3923 for (p = cmd->args_type;; p++) {
3924 if (*p == ':') {
3925 cmd_args.type = *++p;
3926 p++;
3927 if (cmd_args.type == '-') {
3928 cmd_args.flag = *p++;
3929 cmd_args.optional = 1;
3930 } else if (*p == '?') {
3931 cmd_args.optional = 1;
3932 p++;
3933 }
3934
3935 assert(*p == ',' || *p == '\0');
3936 err = check_arg(&cmd_args, args);
3937
3938 QDECREF(cmd_args.name);
3939 cmd_args_init(&cmd_args);
3940
3941 if (err < 0) {
3942 break;
3943 }
3944 } else {
3945 qstring_append_chr(cmd_args.name, *p);
3946 }
3947
3948 if (*p == '\0') {
3949 break;
3950 }
3951 }
3952
3953 QDECREF(cmd_args.name);
3954 return err;
3955}
3956
3957static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3958{
3959 int err;
3960 QObject *obj;
3961 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003962 const mon_cmd_t *cmd;
3963 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02003964 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003965
3966 args = NULL;
3967 qemu_errors_to_mon(mon);
3968
3969 obj = json_parser_parse(tokens, NULL);
3970 if (!obj) {
3971 // FIXME: should be triggered in json_parser_parse()
3972 qemu_error_new(QERR_JSON_PARSING);
3973 goto err_out;
3974 } else if (qobject_type(obj) != QTYPE_QDICT) {
3975 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
3976 qobject_decref(obj);
3977 goto err_out;
3978 }
3979
3980 input = qobject_to_qdict(obj);
3981
3982 mon->mc->id = qdict_get(input, "id");
3983 qobject_incref(mon->mc->id);
3984
3985 obj = qdict_get(input, "execute");
3986 if (!obj) {
3987 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
3988 goto err_input;
3989 } else if (qobject_type(obj) != QTYPE_QSTRING) {
3990 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
3991 goto err_input;
3992 }
3993
3994 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02003995
3996 /*
3997 * XXX: We need this special case until we get info handlers
3998 * converted into 'query-' commands
3999 */
4000 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004001 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4002 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004003 } else if (strstart(cmd_name, "query-", &info_item)) {
4004 cmd = monitor_find_command("info");
4005 qdict_put_obj(input, "arguments",
4006 qobject_from_jsonf("{ 'item': %s }", info_item));
4007 } else {
4008 cmd = monitor_find_command(cmd_name);
4009 if (!cmd) {
4010 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
4011 goto err_input;
4012 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004013 }
4014
4015 obj = qdict_get(input, "arguments");
4016 if (!obj) {
4017 args = qdict_new();
4018 } else {
4019 args = qobject_to_qdict(obj);
4020 QINCREF(args);
4021 }
4022
4023 QDECREF(input);
4024
4025 err = monitor_check_qmp_args(cmd, args);
4026 if (err < 0) {
4027 goto err_out;
4028 }
4029
4030 monitor_call_handler(mon, cmd, args);
4031 goto out;
4032
4033err_input:
4034 QDECREF(input);
4035err_out:
4036 monitor_protocol_emitter(mon, NULL);
4037out:
4038 QDECREF(args);
4039 qemu_errors_to_previous();
4040}
4041
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004042/**
4043 * monitor_control_read(): Read and handle QMP input
4044 */
4045static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4046{
4047 Monitor *old_mon = cur_mon;
4048
4049 cur_mon = opaque;
4050
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004051 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004052
4053 cur_mon = old_mon;
4054}
4055
aliguori731b0362009-03-05 23:01:42 +00004056static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004057{
aliguori731b0362009-03-05 23:01:42 +00004058 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004059 int i;
aliguori376253e2009-03-05 23:01:23 +00004060
aliguori731b0362009-03-05 23:01:42 +00004061 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004062
aliguoricde76ee2009-03-05 23:01:51 +00004063 if (cur_mon->rs) {
4064 for (i = 0; i < size; i++)
4065 readline_handle_byte(cur_mon->rs, buf[i]);
4066 } else {
4067 if (size == 0 || buf[size - 1] != 0)
4068 monitor_printf(cur_mon, "corrupted command\n");
4069 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004070 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004071 }
aliguori731b0362009-03-05 23:01:42 +00004072
4073 cur_mon = old_mon;
4074}
aliguorid8f44602008-10-06 13:52:44 +00004075
aliguori376253e2009-03-05 23:01:23 +00004076static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004077{
aliguori731b0362009-03-05 23:01:42 +00004078 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004079 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004080 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004081}
4082
aliguoricde76ee2009-03-05 23:01:51 +00004083int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004084{
aliguoricde76ee2009-03-05 23:01:51 +00004085 if (!mon->rs)
4086 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004087 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004088 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004089}
4090
aliguori376253e2009-03-05 23:01:23 +00004091void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004092{
aliguoricde76ee2009-03-05 23:01:51 +00004093 if (!mon->rs)
4094 return;
aliguori731b0362009-03-05 23:01:42 +00004095 if (--mon->suspend_cnt == 0)
4096 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004097}
4098
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004099/**
4100 * monitor_control_event(): Print QMP gretting
4101 */
4102static void monitor_control_event(void *opaque, int event)
4103{
4104 if (event == CHR_EVENT_OPENED) {
4105 QObject *data;
4106 Monitor *mon = opaque;
4107
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004108 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4109
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004110 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
4111 assert(data != NULL);
4112
4113 monitor_json_emitter(mon, data);
4114 qobject_decref(data);
4115 }
4116}
4117
aliguori731b0362009-03-05 23:01:42 +00004118static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004119{
aliguori376253e2009-03-05 23:01:23 +00004120 Monitor *mon = opaque;
4121
aliguori2724b182009-03-05 23:01:47 +00004122 switch (event) {
4123 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004124 mon->mux_out = 0;
4125 if (mon->reset_seen) {
4126 readline_restart(mon->rs);
4127 monitor_resume(mon);
4128 monitor_flush(mon);
4129 } else {
4130 mon->suspend_cnt = 0;
4131 }
aliguori2724b182009-03-05 23:01:47 +00004132 break;
ths86e94de2007-01-05 22:01:59 +00004133
aliguori2724b182009-03-05 23:01:47 +00004134 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004135 if (mon->reset_seen) {
4136 if (mon->suspend_cnt == 0) {
4137 monitor_printf(mon, "\n");
4138 }
4139 monitor_flush(mon);
4140 monitor_suspend(mon);
4141 } else {
4142 mon->suspend_cnt++;
4143 }
4144 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004145 break;
4146
Amit Shahb6b8df52009-10-07 18:31:16 +05304147 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004148 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4149 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004150 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004151 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004152 }
4153 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004154 break;
4155 }
ths86e94de2007-01-05 22:01:59 +00004156}
4157
aliguori76655d62009-03-06 20:27:37 +00004158
4159/*
4160 * Local variables:
4161 * c-indent-level: 4
4162 * c-basic-offset: 4
4163 * tab-width: 8
4164 * End:
4165 */
4166
aliguori731b0362009-03-05 23:01:42 +00004167void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004168{
aliguori731b0362009-03-05 23:01:42 +00004169 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004170 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004171
4172 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004173 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004174 is_first_init = 0;
4175 }
aliguori87127162009-03-05 23:01:29 +00004176
4177 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004178
aliguori87127162009-03-05 23:01:29 +00004179 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004180 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004181 if (flags & MONITOR_USE_READLINE) {
4182 mon->rs = readline_init(mon, monitor_find_completion);
4183 monitor_read_command(mon, 0);
4184 }
aliguori87127162009-03-05 23:01:29 +00004185
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004186 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004187 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004188 /* Control mode requires special handlers */
4189 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4190 monitor_control_event, mon);
4191 } else {
4192 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4193 monitor_event, mon);
4194 }
aliguori87127162009-03-05 23:01:29 +00004195
Blue Swirl72cf2d42009-09-12 07:36:22 +00004196 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004197 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004198 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004199}
4200
aliguori376253e2009-03-05 23:01:23 +00004201static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004202{
aliguoribb5fc202009-03-05 23:01:15 +00004203 BlockDriverState *bs = opaque;
4204 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004205
aliguoribb5fc202009-03-05 23:01:15 +00004206 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004207 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004208 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004209 }
aliguori731b0362009-03-05 23:01:42 +00004210 if (mon->password_completion_cb)
4211 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004212
aliguori731b0362009-03-05 23:01:42 +00004213 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004214}
aliguoric0f4ce72009-03-05 23:01:01 +00004215
aliguori376253e2009-03-05 23:01:23 +00004216void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004217 BlockDriverCompletionFunc *completion_cb,
4218 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004219{
aliguoricde76ee2009-03-05 23:01:51 +00004220 int err;
4221
aliguoribb5fc202009-03-05 23:01:15 +00004222 if (!bdrv_key_required(bs)) {
4223 if (completion_cb)
4224 completion_cb(opaque, 0);
4225 return;
4226 }
aliguoric0f4ce72009-03-05 23:01:01 +00004227
Luiz Capitulino94171e12009-12-07 21:37:00 +01004228 if (monitor_ctrl_mode(mon)) {
4229 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4230 return;
4231 }
4232
aliguori376253e2009-03-05 23:01:23 +00004233 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4234 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004235
aliguori731b0362009-03-05 23:01:42 +00004236 mon->password_completion_cb = completion_cb;
4237 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004238
aliguoricde76ee2009-03-05 23:01:51 +00004239 err = monitor_read_password(mon, bdrv_password_cb, bs);
4240
4241 if (err && completion_cb)
4242 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004243}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004244
4245typedef struct QemuErrorSink QemuErrorSink;
4246struct QemuErrorSink {
4247 enum {
4248 ERR_SINK_FILE,
4249 ERR_SINK_MONITOR,
4250 } dest;
4251 union {
4252 FILE *fp;
4253 Monitor *mon;
4254 };
4255 QemuErrorSink *previous;
4256};
4257
Blue Swirl528e93a2009-08-31 15:14:40 +00004258static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004259
4260void qemu_errors_to_file(FILE *fp)
4261{
4262 QemuErrorSink *sink;
4263
4264 sink = qemu_mallocz(sizeof(*sink));
4265 sink->dest = ERR_SINK_FILE;
4266 sink->fp = fp;
4267 sink->previous = qemu_error_sink;
4268 qemu_error_sink = sink;
4269}
4270
4271void qemu_errors_to_mon(Monitor *mon)
4272{
4273 QemuErrorSink *sink;
4274
4275 sink = qemu_mallocz(sizeof(*sink));
4276 sink->dest = ERR_SINK_MONITOR;
4277 sink->mon = mon;
4278 sink->previous = qemu_error_sink;
4279 qemu_error_sink = sink;
4280}
4281
4282void qemu_errors_to_previous(void)
4283{
4284 QemuErrorSink *sink;
4285
4286 assert(qemu_error_sink != NULL);
4287 sink = qemu_error_sink;
4288 qemu_error_sink = sink->previous;
4289 qemu_free(sink);
4290}
4291
4292void qemu_error(const char *fmt, ...)
4293{
4294 va_list args;
4295
4296 assert(qemu_error_sink != NULL);
4297 switch (qemu_error_sink->dest) {
4298 case ERR_SINK_FILE:
4299 va_start(args, fmt);
4300 vfprintf(qemu_error_sink->fp, fmt, args);
4301 va_end(args);
4302 break;
4303 case ERR_SINK_MONITOR:
4304 va_start(args, fmt);
4305 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4306 va_end(args);
4307 break;
4308 }
4309}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004310
4311void qemu_error_internal(const char *file, int linenr, const char *func,
4312 const char *fmt, ...)
4313{
4314 va_list va;
4315 QError *qerror;
4316
4317 assert(qemu_error_sink != NULL);
4318
4319 va_start(va, fmt);
4320 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4321 va_end(va);
4322
4323 switch (qemu_error_sink->dest) {
4324 case ERR_SINK_FILE:
4325 qerror_print(qerror);
4326 QDECREF(qerror);
4327 break;
4328 case ERR_SINK_MONITOR:
4329 assert(qemu_error_sink->mon->error == NULL);
4330 qemu_error_sink->mon->error = qerror;
4331 break;
4332 }
4333}