blob: 31a2afaa15337c7a49b4a51832b24b2a9516aaef [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 Capitulinoab2d3182009-10-07 13:42:02 -0300260static void monitor_print_qobject(Monitor *mon, const QObject *data)
261{
262 switch (qobject_type(data)) {
263 case QTYPE_QSTRING:
264 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
265 break;
266 case QTYPE_QINT:
267 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
268 break;
269 default:
270 monitor_printf(mon, "ERROR: unsupported type: %d",
271 qobject_type(data));
272 break;
273 }
274
275 monitor_puts(mon, "\n");
276}
277
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200278static void monitor_json_emitter(Monitor *mon, const QObject *data)
279{
280 QString *json;
281
282 json = qobject_to_json(data);
283 assert(json != NULL);
284
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200285 mon->mc->print_enabled = 1;
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200286 monitor_printf(mon, "%s\n", qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200287 mon->mc->print_enabled = 0;
288
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200289 QDECREF(json);
290}
291
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200292static void monitor_protocol_emitter(Monitor *mon, QObject *data)
293{
294 QDict *qmp;
295
296 qmp = qdict_new();
297
298 if (!monitor_has_error(mon)) {
299 /* success response */
300 if (data) {
301 qobject_incref(data);
302 qdict_put_obj(qmp, "return", data);
303 } else {
304 qdict_put(qmp, "return", qstring_from_str("OK"));
305 }
306 } else {
307 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100308 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200309 qdict_put(qmp, "error", mon->error->error);
310 QINCREF(mon->error->error);
311 QDECREF(mon->error);
312 mon->error = NULL;
313 }
314
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200315 if (mon->mc->id) {
316 qdict_put_obj(qmp, "id", mon->mc->id);
317 mon->mc->id = NULL;
318 }
319
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200320 monitor_json_emitter(mon, QOBJECT(qmp));
321 QDECREF(qmp);
322}
323
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200324static void timestamp_put(QDict *qdict)
325{
326 int err;
327 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000328 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200329
Blue Swirld08d6f02009-12-04 18:06:39 +0000330 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200331 if (err < 0)
332 return;
333
334 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
335 "'microseconds': %" PRId64 " }",
336 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
337 assert(obj != NULL);
338
339 qdict_put_obj(qdict, "timestamp", obj);
340}
341
342/**
343 * monitor_protocol_event(): Generate a Monitor event
344 *
345 * Event-specific data can be emitted through the (optional) 'data' parameter.
346 */
347void monitor_protocol_event(MonitorEvent event, QObject *data)
348{
349 QDict *qmp;
350 const char *event_name;
351 Monitor *mon = cur_mon;
352
Blue Swirl242cd002009-12-04 18:05:45 +0000353 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200354
355 if (!monitor_ctrl_mode(mon))
356 return;
357
358 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000359 case QEVENT_DEBUG:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200360 event_name = "DEBUG";
361 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000362 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200363 event_name = "SHUTDOWN";
364 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000365 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200366 event_name = "RESET";
367 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000368 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200369 event_name = "POWERDOWN";
370 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000371 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200372 event_name = "STOP";
373 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200374 default:
375 abort();
376 break;
377 }
378
379 qmp = qdict_new();
380 timestamp_put(qmp);
381 qdict_put(qmp, "event", qstring_from_str(event_name));
382 if (data)
383 qdict_put_obj(qmp, "data", data);
384
385 monitor_json_emitter(mon, QOBJECT(qmp));
386 QDECREF(qmp);
387}
388
bellard9dc39cb2004-03-14 21:38:27 +0000389static int compare_cmd(const char *name, const char *list)
390{
391 const char *p, *pstart;
392 int len;
393 len = strlen(name);
394 p = list;
395 for(;;) {
396 pstart = p;
397 p = strchr(p, '|');
398 if (!p)
399 p = pstart + strlen(pstart);
400 if ((p - pstart) == len && !memcmp(pstart, name, len))
401 return 1;
402 if (*p == '\0')
403 break;
404 p++;
405 }
406 return 0;
407}
408
Anthony Liguoric227f092009-10-01 16:12:16 -0500409static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000410 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000411{
Anthony Liguoric227f092009-10-01 16:12:16 -0500412 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000413
414 for(cmd = cmds; cmd->name != NULL; cmd++) {
415 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000416 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
417 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000418 }
419}
420
aliguori376253e2009-03-05 23:01:23 +0000421static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000422{
423 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000424 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000425 } else {
aliguori376253e2009-03-05 23:01:23 +0000426 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000427 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000428 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000429 monitor_printf(mon, "Log items (comma separated):\n");
430 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000431 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000432 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000433 }
434 }
bellard9dc39cb2004-03-14 21:38:27 +0000435 }
436}
437
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300438static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300439{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300440 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300441}
442
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300443static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000444{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200445 int all_devices;
446 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300447 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000448
bellard7954c732006-08-01 15:52:40 +0000449 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000450 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200451 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300452 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200453 continue;
454 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000455 }
456}
457
Luiz Capitulino13c74252009-10-07 13:41:55 -0300458static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000459{
Anthony Liguoric227f092009-10-01 16:12:16 -0500460 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300461 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000462
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200463 if (!item) {
464 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000465 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200466 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300467
468 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000469 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300470 break;
bellard9dc39cb2004-03-14 21:38:27 +0000471 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300472
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200473 if (cmd->name == NULL) {
474 if (monitor_ctrl_mode(mon)) {
475 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
476 return;
477 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300478 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200479 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300480
481 if (monitor_handler_ported(cmd)) {
482 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200483
484 if (!monitor_ctrl_mode(mon)) {
485 /*
486 * User Protocol function is called here, Monitor Protocol is
487 * handled by monitor_call_handler()
488 */
489 if (*ret_data)
490 cmd->user_print(mon, *ret_data);
491 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300492 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200493 if (monitor_ctrl_mode(mon)) {
494 /* handler not converted yet */
495 qemu_error_new(QERR_COMMAND_NOT_FOUND, item);
496 } else {
497 cmd->mhandler.info(mon);
498 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300499 }
500
bellard9dc39cb2004-03-14 21:38:27 +0000501 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300502
503help:
504 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000505}
506
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300507/**
508 * do_info_version(): Show QEMU version
509 */
510static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000511{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300512 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000513}
514
aliguori376253e2009-03-05 23:01:23 +0000515static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000516{
517 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000518 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000519}
520
Luiz Capitulino1a728672009-12-10 17:15:56 -0200521static QObject *get_cmd_dict(const char *name)
522{
523 const char *p;
524
525 /* Remove '|' from some commands */
526 p = strchr(name, '|');
527 if (p) {
528 p++;
529 } else {
530 p = name;
531 }
532
533 return qobject_from_jsonf("{ 'name': %s }", p);
534}
535
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200536/**
537 * do_info_commands(): List QMP available commands
538 *
Luiz Capitulino1a728672009-12-10 17:15:56 -0200539 * Each command is represented by a QDict, the returned QObject is a QList
540 * of all commands.
541 *
542 * The QDict contains:
543 *
544 * - "name": command's name
545 *
546 * Example:
547 *
548 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200549 */
550static void do_info_commands(Monitor *mon, QObject **ret_data)
551{
552 QList *cmd_list;
553 const mon_cmd_t *cmd;
554
555 cmd_list = qlist_new();
556
557 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
558 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200559 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200560 }
561 }
562
563 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
564 if (monitor_handler_ported(cmd)) {
565 char buf[128];
566 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200567 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200568 }
569 }
570
571 *ret_data = QOBJECT(cmd_list);
572}
573
aurel32bf4f74c2008-12-18 22:42:34 +0000574#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000575static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000576{
aliguori376253e2009-03-05 23:01:23 +0000577 monitor_printf(mon, "HPET is %s by QEMU\n",
578 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000579}
aurel32bf4f74c2008-12-18 22:42:34 +0000580#endif
aliguori16b29ae2008-12-17 23:28:44 +0000581
aliguori376253e2009-03-05 23:01:23 +0000582static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000583{
aliguori376253e2009-03-05 23:01:23 +0000584 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
585 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
586 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
587 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
588 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000589}
590
bellard6a00d602005-11-21 23:25:50 +0000591/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000592static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000593{
594 CPUState *env;
595
596 for(env = first_cpu; env != NULL; env = env->next_cpu) {
597 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000598 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000599 return 0;
600 }
601 }
602 return -1;
603}
604
pbrook9596ebb2007-11-18 01:44:38 +0000605static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000606{
aliguori731b0362009-03-05 23:01:42 +0000607 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000608 mon_set_cpu(0);
609 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300610 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000611 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000612}
613
aliguori376253e2009-03-05 23:01:23 +0000614static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000615{
bellard6a00d602005-11-21 23:25:50 +0000616 CPUState *env;
617 env = mon_get_cpu();
618 if (!env)
619 return;
bellard9307c4c2004-04-04 12:57:25 +0000620#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000621 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000622 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000623#else
aliguori376253e2009-03-05 23:01:23 +0000624 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000625 0);
bellard9307c4c2004-04-04 12:57:25 +0000626#endif
627}
628
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300629static void print_cpu_iter(QObject *obj, void *opaque)
630{
631 QDict *cpu;
632 int active = ' ';
633 Monitor *mon = opaque;
634
635 assert(qobject_type(obj) == QTYPE_QDICT);
636 cpu = qobject_to_qdict(obj);
637
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200638 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300639 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200640 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300641
642 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
643
644#if defined(TARGET_I386)
645 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
646 (target_ulong) qdict_get_int(cpu, "pc"));
647#elif defined(TARGET_PPC)
648 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
649 (target_long) qdict_get_int(cpu, "nip"));
650#elif defined(TARGET_SPARC)
651 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
652 (target_long) qdict_get_int(cpu, "pc"));
653 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
654 (target_long) qdict_get_int(cpu, "npc"));
655#elif defined(TARGET_MIPS)
656 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
657 (target_long) qdict_get_int(cpu, "PC"));
658#endif
659
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200660 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300661 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200662 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300663
664 monitor_printf(mon, "\n");
665}
666
667static void monitor_print_cpus(Monitor *mon, const QObject *data)
668{
669 QList *cpu_list;
670
671 assert(qobject_type(data) == QTYPE_QLIST);
672 cpu_list = qobject_to_qlist(data);
673 qlist_iter(cpu_list, print_cpu_iter, mon);
674}
675
676/**
677 * do_info_cpus(): Show CPU information
678 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200679 * Return a QList. Each CPU is represented by a QDict, which contains:
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300680 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200681 * - "cpu": CPU index
682 * - "current": true if this is the current CPU, false otherwise
683 * - "halted": true if the cpu is halted, false otherwise
684 * - Current program counter. The key's name depends on the architecture:
685 * "pc": i386/x86)64
686 * "nip": PPC
687 * "pc" and "npc": sparc
688 * "PC": mips
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300689 *
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200690 * Example:
691 *
692 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
693 * { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300694 */
695static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000696{
697 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300698 QList *cpu_list;
699
700 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000701
702 /* just to set the default cpu if not already done */
703 mon_get_cpu();
704
705 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200706 QDict *cpu;
707 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300708
Avi Kivity4c0960c2009-08-17 23:19:53 +0300709 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300710
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200711 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
712 env->cpu_index, env == mon->mon_cpu,
713 env->halted);
714 assert(obj != NULL);
715
716 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300717
bellard6a00d602005-11-21 23:25:50 +0000718#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300719 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000720#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300721 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000722#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300723 qdict_put(cpu, "pc", qint_from_int(env->pc));
724 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000725#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300726 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000727#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300728
729 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000730 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300731
732 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000733}
734
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300735static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000736{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300737 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000738 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000739 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000740}
741
aliguori376253e2009-03-05 23:01:23 +0000742static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000743{
aliguori376253e2009-03-05 23:01:23 +0000744 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000745}
746
aliguori376253e2009-03-05 23:01:23 +0000747static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000748{
749 int i;
bellard7e2515e2004-08-01 21:52:19 +0000750 const char *str;
ths3b46e622007-09-17 08:09:54 +0000751
aliguoricde76ee2009-03-05 23:01:51 +0000752 if (!mon->rs)
753 return;
bellard7e2515e2004-08-01 21:52:19 +0000754 i = 0;
755 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000756 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000757 if (!str)
758 break;
aliguori376253e2009-03-05 23:01:23 +0000759 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000760 i++;
bellardaa455482004-04-04 13:07:25 +0000761 }
762}
763
j_mayer76a66252007-03-07 08:32:30 +0000764#if defined(TARGET_PPC)
765/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000766static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000767{
768 CPUState *env;
769
770 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000771 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000772}
773#endif
774
Luiz Capitulinob223f352009-10-07 13:41:56 -0300775/**
776 * do_quit(): Quit QEMU execution
777 */
778static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000779{
780 exit(0);
781}
782
aliguori376253e2009-03-05 23:01:23 +0000783static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000784{
785 if (bdrv_is_inserted(bs)) {
786 if (!force) {
787 if (!bdrv_is_removable(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100788 qemu_error_new(QERR_DEVICE_NOT_REMOVABLE,
789 bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000790 return -1;
791 }
792 if (bdrv_is_locked(bs)) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100793 qemu_error_new(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
bellard9dc39cb2004-03-14 21:38:27 +0000794 return -1;
795 }
796 }
797 bdrv_close(bs);
798 }
799 return 0;
800}
801
Luiz Capitulinoe1c923a2009-10-16 12:23:49 -0300802static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000803{
804 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300805 int force = qdict_get_int(qdict, "force");
806 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000807
bellard9307c4c2004-04-04 12:57:25 +0000808 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000809 if (!bs) {
Markus Armbruster2c2a6bb2009-12-07 21:37:05 +0100810 qemu_error_new(QERR_DEVICE_NOT_FOUND, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000811 return;
812 }
aliguori376253e2009-03-05 23:01:23 +0000813 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000814}
815
Luiz Capitulinoa3a55a22009-12-04 15:24:09 -0200816static void do_block_set_passwd(Monitor *mon, const QDict *qdict,
817 QObject **ret_data)
818{
819 BlockDriverState *bs;
820
821 bs = bdrv_find(qdict_get_str(qdict, "device"));
822 if (!bs) {
823 qemu_error_new(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
824 return;
825 }
826
827 if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
828 qemu_error_new(QERR_INVALID_PASSWORD);
829 }
830}
831
aliguori376253e2009-03-05 23:01:23 +0000832static void do_change_block(Monitor *mon, const char *device,
833 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000834{
835 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000836 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000837
bellard9307c4c2004-04-04 12:57:25 +0000838 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000839 if (!bs) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100840 qemu_error_new(QERR_DEVICE_NOT_FOUND, device);
bellard9dc39cb2004-03-14 21:38:27 +0000841 return;
842 }
aurel322ecea9b2008-06-18 22:10:01 +0000843 if (fmt) {
Markus Armbrustereb852012009-10-27 18:41:44 +0100844 drv = bdrv_find_whitelisted_format(fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000845 if (!drv) {
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100846 qemu_error_new(QERR_INVALID_BLOCK_FORMAT, fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000847 return;
848 }
849 }
aliguori376253e2009-03-05 23:01:23 +0000850 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000851 return;
aurel322ecea9b2008-06-18 22:10:01 +0000852 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000853 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000854}
855
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100856static void change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000857{
858 if (vnc_display_password(NULL, password) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100859 qemu_error_new(QERR_SET_PASSWD_FAILED);
aliguoribb5fc202009-03-05 23:01:15 +0000860
Markus Armbruster2895e072009-12-07 21:37:01 +0100861}
862
863static void change_vnc_password_cb(Monitor *mon, const char *password,
864 void *opaque)
865{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100866 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000867 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000868}
869
aliguori376253e2009-03-05 23:01:23 +0000870static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000871{
ths70848512007-08-25 01:37:05 +0000872 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000873 strcmp(target, "password") == 0) {
874 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000875 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000876 strncpy(password, arg, sizeof(password));
877 password[sizeof(password) - 1] = '\0';
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100878 change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000879 } else {
aliguori376253e2009-03-05 23:01:23 +0000880 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000881 }
ths70848512007-08-25 01:37:05 +0000882 } else {
aliguori28a76be2009-03-06 20:27:40 +0000883 if (vnc_display_open(NULL, target) < 0)
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100884 qemu_error_new(QERR_VNC_SERVER_FAILED, target);
ths70848512007-08-25 01:37:05 +0000885 }
thse25a5822007-08-25 01:36:20 +0000886}
887
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100888/**
889 * do_change(): Change a removable medium, or VNC configuration
890 */
891static void do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000892{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300893 const char *device = qdict_get_str(qdict, "device");
894 const char *target = qdict_get_str(qdict, "target");
895 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000896 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000897 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000898 } else {
aliguori28a76be2009-03-06 20:27:40 +0000899 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000900 }
901}
902
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300903static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000904{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300905 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000906}
907
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300908static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000909{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300910 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000911}
912
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300913static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000914{
915 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300916 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000917
bellard9307c4c2004-04-04 12:57:25 +0000918 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000919 mask = 0;
920 } else {
bellard9307c4c2004-04-04 12:57:25 +0000921 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000922 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000923 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000924 return;
925 }
926 }
927 cpu_set_log(mask);
928}
929
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300930static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000931{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300932 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000933 if (!option || !strcmp(option, "on")) {
934 singlestep = 1;
935 } else if (!strcmp(option, "off")) {
936 singlestep = 0;
937 } else {
938 monitor_printf(mon, "unexpected option %s\n", option);
939 }
940}
941
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300942/**
943 * do_stop(): Stop VM execution
944 */
945static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000946{
947 vm_stop(EXCP_INTERRUPT);
948}
949
aliguoribb5fc202009-03-05 23:01:15 +0000950static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000951
aliguori376253e2009-03-05 23:01:23 +0000952struct bdrv_iterate_context {
953 Monitor *mon;
954 int err;
955};
aliguoric0f4ce72009-03-05 23:01:01 +0000956
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300957/**
958 * do_cont(): Resume emulation.
959 */
960static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000961{
962 struct bdrv_iterate_context context = { mon, 0 };
963
964 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000965 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000966 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000967 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000968}
969
aliguoribb5fc202009-03-05 23:01:15 +0000970static void bdrv_key_cb(void *opaque, int err)
971{
aliguori376253e2009-03-05 23:01:23 +0000972 Monitor *mon = opaque;
973
aliguoribb5fc202009-03-05 23:01:15 +0000974 /* another key was set successfully, retry to continue */
975 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300976 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000977}
978
979static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
980{
aliguori376253e2009-03-05 23:01:23 +0000981 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000982
aliguori376253e2009-03-05 23:01:23 +0000983 if (!context->err && bdrv_key_required(bs)) {
984 context->err = -EBUSY;
985 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
986 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000987 }
988}
989
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300990static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000991{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300992 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000993 if (!device)
994 device = "tcp::" DEFAULT_GDBSTUB_PORT;
995 if (gdbserver_start(device) < 0) {
996 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
997 device);
998 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000999 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001000 } else {
aliguori59030a82009-04-05 18:43:41 +00001001 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1002 device);
bellard8a7ddc32004-03-31 19:00:16 +00001003 }
1004}
1005
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001006static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001007{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001008 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001009 if (select_watchdog_action(action) == -1) {
1010 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1011 }
1012}
1013
aliguori376253e2009-03-05 23:01:23 +00001014static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001015{
aliguori376253e2009-03-05 23:01:23 +00001016 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001017 switch(c) {
1018 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001019 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001020 break;
1021 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001022 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001023 break;
1024 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001025 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001026 break;
1027 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001028 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001029 break;
1030 default:
1031 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001032 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001033 } else {
aliguori376253e2009-03-05 23:01:23 +00001034 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001035 }
1036 break;
1037 }
aliguori376253e2009-03-05 23:01:23 +00001038 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001039}
1040
aliguori376253e2009-03-05 23:01:23 +00001041static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001042 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001043{
bellard6a00d602005-11-21 23:25:50 +00001044 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +00001045 int nb_per_line, l, line_size, i, max_digits, len;
1046 uint8_t buf[16];
1047 uint64_t v;
1048
1049 if (format == 'i') {
1050 int flags;
1051 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001052 env = mon_get_cpu();
1053 if (!env && !is_physical)
1054 return;
bellard9307c4c2004-04-04 12:57:25 +00001055#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001056 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001057 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001058 } else if (wsize == 4) {
1059 flags = 0;
1060 } else {
bellard6a15fd12006-04-12 21:07:07 +00001061 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001062 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001063 if (env) {
1064#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001065 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001066 (env->segs[R_CS].flags & DESC_L_MASK))
1067 flags = 2;
1068 else
1069#endif
1070 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1071 flags = 1;
1072 }
bellard4c27ba22004-04-25 18:05:08 +00001073 }
1074#endif
aliguori376253e2009-03-05 23:01:23 +00001075 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001076 return;
1077 }
1078
1079 len = wsize * count;
1080 if (wsize == 1)
1081 line_size = 8;
1082 else
1083 line_size = 16;
1084 nb_per_line = line_size / wsize;
1085 max_digits = 0;
1086
1087 switch(format) {
1088 case 'o':
1089 max_digits = (wsize * 8 + 2) / 3;
1090 break;
1091 default:
1092 case 'x':
1093 max_digits = (wsize * 8) / 4;
1094 break;
1095 case 'u':
1096 case 'd':
1097 max_digits = (wsize * 8 * 10 + 32) / 33;
1098 break;
1099 case 'c':
1100 wsize = 1;
1101 break;
1102 }
1103
1104 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001105 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001106 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001107 else
aliguori376253e2009-03-05 23:01:23 +00001108 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001109 l = len;
1110 if (l > line_size)
1111 l = line_size;
1112 if (is_physical) {
1113 cpu_physical_memory_rw(addr, buf, l, 0);
1114 } else {
bellard6a00d602005-11-21 23:25:50 +00001115 env = mon_get_cpu();
1116 if (!env)
1117 break;
aliguoric8f79b62008-08-18 14:00:20 +00001118 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001119 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001120 break;
1121 }
bellard9307c4c2004-04-04 12:57:25 +00001122 }
ths5fafdf22007-09-16 21:08:06 +00001123 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001124 while (i < l) {
1125 switch(wsize) {
1126 default:
1127 case 1:
1128 v = ldub_raw(buf + i);
1129 break;
1130 case 2:
1131 v = lduw_raw(buf + i);
1132 break;
1133 case 4:
bellard92a31b12005-02-10 22:00:52 +00001134 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001135 break;
1136 case 8:
1137 v = ldq_raw(buf + i);
1138 break;
1139 }
aliguori376253e2009-03-05 23:01:23 +00001140 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001141 switch(format) {
1142 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001143 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001144 break;
1145 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001146 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001147 break;
1148 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001149 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001150 break;
1151 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001152 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001153 break;
1154 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001155 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001156 break;
1157 }
1158 i += wsize;
1159 }
aliguori376253e2009-03-05 23:01:23 +00001160 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001161 addr += l;
1162 len -= l;
1163 }
1164}
1165
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001166static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001167{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001168 int count = qdict_get_int(qdict, "count");
1169 int format = qdict_get_int(qdict, "format");
1170 int size = qdict_get_int(qdict, "size");
1171 target_long addr = qdict_get_int(qdict, "addr");
1172
aliguori376253e2009-03-05 23:01:23 +00001173 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001174}
1175
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001176static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001177{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001178 int count = qdict_get_int(qdict, "count");
1179 int format = qdict_get_int(qdict, "format");
1180 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001181 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001182
aliguori376253e2009-03-05 23:01:23 +00001183 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001184}
1185
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001186static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001187{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001188 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001189 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001190
blueswir17743e582007-09-24 18:39:04 +00001191#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001192 switch(format) {
1193 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001194 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001195 break;
1196 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001197 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001198 break;
1199 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001200 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001201 break;
1202 default:
1203 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001204 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001205 break;
1206 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001208 break;
1209 }
bellard92a31b12005-02-10 22:00:52 +00001210#else
1211 switch(format) {
1212 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001213 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001214 break;
1215 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001216 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001217 break;
1218 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001219 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001220 break;
1221 default:
1222 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001224 break;
1225 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001226 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001227 break;
1228 }
1229#endif
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001231}
1232
Luiz Capitulino57e09452009-10-16 12:23:43 -03001233static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001234{
1235 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001236 uint32_t size = qdict_get_int(qdict, "size");
1237 const char *filename = qdict_get_str(qdict, "filename");
1238 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001239 uint32_t l;
1240 CPUState *env;
1241 uint8_t buf[1024];
1242
1243 env = mon_get_cpu();
1244 if (!env)
1245 return;
1246
1247 f = fopen(filename, "wb");
1248 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001249 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +00001250 return;
1251 }
1252 while (size != 0) {
1253 l = sizeof(buf);
1254 if (l > size)
1255 l = size;
1256 cpu_memory_rw_debug(env, addr, buf, l, 0);
1257 fwrite(buf, 1, l, f);
1258 addr += l;
1259 size -= l;
1260 }
1261 fclose(f);
1262}
1263
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001264static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1265 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001266{
1267 FILE *f;
1268 uint32_t l;
1269 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001270 uint32_t size = qdict_get_int(qdict, "size");
1271 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001272 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +00001273
1274 f = fopen(filename, "wb");
1275 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +00001277 return;
1278 }
1279 while (size != 0) {
1280 l = sizeof(buf);
1281 if (l > size)
1282 l = size;
1283 cpu_physical_memory_rw(addr, buf, l, 0);
1284 fwrite(buf, 1, l, f);
1285 fflush(f);
1286 addr += l;
1287 size -= l;
1288 }
1289 fclose(f);
1290}
1291
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001292static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001293{
1294 uint32_t addr;
1295 uint8_t buf[1];
1296 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001297 uint32_t start = qdict_get_int(qdict, "start");
1298 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001299
1300 sum = 0;
1301 for(addr = start; addr < (start + size); addr++) {
1302 cpu_physical_memory_rw(addr, buf, 1, 0);
1303 /* BSD sum algorithm ('sum' Unix command) */
1304 sum = (sum >> 1) | (sum << 15);
1305 sum += buf[0];
1306 }
aliguori376253e2009-03-05 23:01:23 +00001307 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001308}
1309
bellarda3a91a32004-06-04 11:06:21 +00001310typedef struct {
1311 int keycode;
1312 const char *name;
1313} KeyDef;
1314
1315static const KeyDef key_defs[] = {
1316 { 0x2a, "shift" },
1317 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001318
bellarda3a91a32004-06-04 11:06:21 +00001319 { 0x38, "alt" },
1320 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001321 { 0x64, "altgr" },
1322 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001323 { 0x1d, "ctrl" },
1324 { 0x9d, "ctrl_r" },
1325
1326 { 0xdd, "menu" },
1327
1328 { 0x01, "esc" },
1329
1330 { 0x02, "1" },
1331 { 0x03, "2" },
1332 { 0x04, "3" },
1333 { 0x05, "4" },
1334 { 0x06, "5" },
1335 { 0x07, "6" },
1336 { 0x08, "7" },
1337 { 0x09, "8" },
1338 { 0x0a, "9" },
1339 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001340 { 0x0c, "minus" },
1341 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001342 { 0x0e, "backspace" },
1343
1344 { 0x0f, "tab" },
1345 { 0x10, "q" },
1346 { 0x11, "w" },
1347 { 0x12, "e" },
1348 { 0x13, "r" },
1349 { 0x14, "t" },
1350 { 0x15, "y" },
1351 { 0x16, "u" },
1352 { 0x17, "i" },
1353 { 0x18, "o" },
1354 { 0x19, "p" },
1355
1356 { 0x1c, "ret" },
1357
1358 { 0x1e, "a" },
1359 { 0x1f, "s" },
1360 { 0x20, "d" },
1361 { 0x21, "f" },
1362 { 0x22, "g" },
1363 { 0x23, "h" },
1364 { 0x24, "j" },
1365 { 0x25, "k" },
1366 { 0x26, "l" },
1367
1368 { 0x2c, "z" },
1369 { 0x2d, "x" },
1370 { 0x2e, "c" },
1371 { 0x2f, "v" },
1372 { 0x30, "b" },
1373 { 0x31, "n" },
1374 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001375 { 0x33, "comma" },
1376 { 0x34, "dot" },
1377 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001378
balrog4d3b6f62008-02-10 16:33:14 +00001379 { 0x37, "asterisk" },
1380
bellarda3a91a32004-06-04 11:06:21 +00001381 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001382 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001383 { 0x3b, "f1" },
1384 { 0x3c, "f2" },
1385 { 0x3d, "f3" },
1386 { 0x3e, "f4" },
1387 { 0x3f, "f5" },
1388 { 0x40, "f6" },
1389 { 0x41, "f7" },
1390 { 0x42, "f8" },
1391 { 0x43, "f9" },
1392 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001393 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001394 { 0x46, "scroll_lock" },
1395
bellard64866c32006-05-07 18:03:31 +00001396 { 0xb5, "kp_divide" },
1397 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001398 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001399 { 0x4e, "kp_add" },
1400 { 0x9c, "kp_enter" },
1401 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001402 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001403
1404 { 0x52, "kp_0" },
1405 { 0x4f, "kp_1" },
1406 { 0x50, "kp_2" },
1407 { 0x51, "kp_3" },
1408 { 0x4b, "kp_4" },
1409 { 0x4c, "kp_5" },
1410 { 0x4d, "kp_6" },
1411 { 0x47, "kp_7" },
1412 { 0x48, "kp_8" },
1413 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001414
bellarda3a91a32004-06-04 11:06:21 +00001415 { 0x56, "<" },
1416
1417 { 0x57, "f11" },
1418 { 0x58, "f12" },
1419
1420 { 0xb7, "print" },
1421
1422 { 0xc7, "home" },
1423 { 0xc9, "pgup" },
1424 { 0xd1, "pgdn" },
1425 { 0xcf, "end" },
1426
1427 { 0xcb, "left" },
1428 { 0xc8, "up" },
1429 { 0xd0, "down" },
1430 { 0xcd, "right" },
1431
1432 { 0xd2, "insert" },
1433 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001434#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1435 { 0xf0, "stop" },
1436 { 0xf1, "again" },
1437 { 0xf2, "props" },
1438 { 0xf3, "undo" },
1439 { 0xf4, "front" },
1440 { 0xf5, "copy" },
1441 { 0xf6, "open" },
1442 { 0xf7, "paste" },
1443 { 0xf8, "find" },
1444 { 0xf9, "cut" },
1445 { 0xfa, "lf" },
1446 { 0xfb, "help" },
1447 { 0xfc, "meta_l" },
1448 { 0xfd, "meta_r" },
1449 { 0xfe, "compose" },
1450#endif
bellarda3a91a32004-06-04 11:06:21 +00001451 { 0, NULL },
1452};
1453
1454static int get_keycode(const char *key)
1455{
1456 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001457 char *endp;
1458 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001459
1460 for(p = key_defs; p->name != NULL; p++) {
1461 if (!strcmp(key, p->name))
1462 return p->keycode;
1463 }
bellard64866c32006-05-07 18:03:31 +00001464 if (strstart(key, "0x", NULL)) {
1465 ret = strtoul(key, &endp, 0);
1466 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1467 return ret;
1468 }
bellarda3a91a32004-06-04 11:06:21 +00001469 return -1;
1470}
1471
balrogc8256f92008-06-08 22:45:01 +00001472#define MAX_KEYCODES 16
1473static uint8_t keycodes[MAX_KEYCODES];
1474static int nb_pending_keycodes;
1475static QEMUTimer *key_timer;
1476
1477static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001478{
balrogc8256f92008-06-08 22:45:01 +00001479 int keycode;
1480
1481 while (nb_pending_keycodes > 0) {
1482 nb_pending_keycodes--;
1483 keycode = keycodes[nb_pending_keycodes];
1484 if (keycode & 0x80)
1485 kbd_put_keycode(0xe0);
1486 kbd_put_keycode(keycode | 0x80);
1487 }
1488}
1489
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001490static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001491{
balrog3401c0d2008-06-04 10:05:59 +00001492 char keyname_buf[16];
1493 char *separator;
1494 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001495 const char *string = qdict_get_str(qdict, "string");
1496 int has_hold_time = qdict_haskey(qdict, "hold_time");
1497 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001498
balrogc8256f92008-06-08 22:45:01 +00001499 if (nb_pending_keycodes > 0) {
1500 qemu_del_timer(key_timer);
1501 release_keys(NULL);
1502 }
1503 if (!has_hold_time)
1504 hold_time = 100;
1505 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001506 while (1) {
1507 separator = strchr(string, '-');
1508 keyname_len = separator ? separator - string : strlen(string);
1509 if (keyname_len > 0) {
1510 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1511 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001512 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001513 return;
bellarda3a91a32004-06-04 11:06:21 +00001514 }
balrogc8256f92008-06-08 22:45:01 +00001515 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001516 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001517 return;
1518 }
1519 keyname_buf[keyname_len] = 0;
1520 keycode = get_keycode(keyname_buf);
1521 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001522 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001523 return;
1524 }
balrogc8256f92008-06-08 22:45:01 +00001525 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001526 }
balrog3401c0d2008-06-04 10:05:59 +00001527 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001528 break;
balrog3401c0d2008-06-04 10:05:59 +00001529 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001530 }
balrogc8256f92008-06-08 22:45:01 +00001531 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001532 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001533 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001534 keycode = keycodes[i];
1535 if (keycode & 0x80)
1536 kbd_put_keycode(0xe0);
1537 kbd_put_keycode(keycode & 0x7f);
1538 }
balrogc8256f92008-06-08 22:45:01 +00001539 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001540 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001541 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001542}
1543
bellard13224a82006-07-14 22:03:35 +00001544static int mouse_button_state;
1545
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001546static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001547{
1548 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001549 const char *dx_str = qdict_get_str(qdict, "dx_str");
1550 const char *dy_str = qdict_get_str(qdict, "dy_str");
1551 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001552 dx = strtol(dx_str, NULL, 0);
1553 dy = strtol(dy_str, NULL, 0);
1554 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001555 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001556 dz = strtol(dz_str, NULL, 0);
1557 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1558}
1559
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001560static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001561{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001562 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001563 mouse_button_state = button_state;
1564 kbd_mouse_event(0, 0, 0, mouse_button_state);
1565}
1566
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001567static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001568{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001569 int size = qdict_get_int(qdict, "size");
1570 int addr = qdict_get_int(qdict, "addr");
1571 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001572 uint32_t val;
1573 int suffix;
1574
1575 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001576 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001577 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001578 addr++;
1579 }
1580 addr &= 0xffff;
1581
1582 switch(size) {
1583 default:
1584 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001585 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001586 suffix = 'b';
1587 break;
1588 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001589 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001590 suffix = 'w';
1591 break;
1592 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001593 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001594 suffix = 'l';
1595 break;
1596 }
aliguori376253e2009-03-05 23:01:23 +00001597 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1598 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001599}
bellarda3a91a32004-06-04 11:06:21 +00001600
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001601static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001602{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001603 int size = qdict_get_int(qdict, "size");
1604 int addr = qdict_get_int(qdict, "addr");
1605 int val = qdict_get_int(qdict, "val");
1606
Jan Kiszkaf1147842009-07-14 10:20:11 +02001607 addr &= IOPORTS_MASK;
1608
1609 switch (size) {
1610 default:
1611 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001612 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001613 break;
1614 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001615 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001616 break;
1617 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001618 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001619 break;
1620 }
1621}
1622
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001623static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001624{
1625 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001626 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001627
Jan Kiszka76e30d02009-07-02 00:19:02 +02001628 res = qemu_boot_set(bootdevice);
1629 if (res == 0) {
1630 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1631 } else if (res > 0) {
1632 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001633 } else {
aliguori376253e2009-03-05 23:01:23 +00001634 monitor_printf(mon, "no function defined to set boot device list for "
1635 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001636 }
1637}
1638
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001639/**
1640 * do_system_reset(): Issue a machine reset
1641 */
1642static void do_system_reset(Monitor *mon, const QDict *qdict,
1643 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001644{
1645 qemu_system_reset_request();
1646}
1647
Luiz Capitulino43076662009-10-07 13:41:59 -03001648/**
1649 * do_system_powerdown(): Issue a machine powerdown
1650 */
1651static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1652 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001653{
1654 qemu_system_powerdown_request();
1655}
1656
bellardb86bda52004-09-18 19:32:46 +00001657#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001658static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001659{
aliguori376253e2009-03-05 23:01:23 +00001660 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1661 addr,
1662 pte & mask,
1663 pte & PG_GLOBAL_MASK ? 'G' : '-',
1664 pte & PG_PSE_MASK ? 'P' : '-',
1665 pte & PG_DIRTY_MASK ? 'D' : '-',
1666 pte & PG_ACCESSED_MASK ? 'A' : '-',
1667 pte & PG_PCD_MASK ? 'C' : '-',
1668 pte & PG_PWT_MASK ? 'T' : '-',
1669 pte & PG_USER_MASK ? 'U' : '-',
1670 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001671}
1672
aliguori376253e2009-03-05 23:01:23 +00001673static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001674{
bellard6a00d602005-11-21 23:25:50 +00001675 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001676 int l1, l2;
1677 uint32_t pgd, pde, pte;
1678
bellard6a00d602005-11-21 23:25:50 +00001679 env = mon_get_cpu();
1680 if (!env)
1681 return;
1682
bellardb86bda52004-09-18 19:32:46 +00001683 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001684 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001685 return;
1686 }
1687 pgd = env->cr[3] & ~0xfff;
1688 for(l1 = 0; l1 < 1024; l1++) {
1689 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1690 pde = le32_to_cpu(pde);
1691 if (pde & PG_PRESENT_MASK) {
1692 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001693 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001694 } else {
1695 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001696 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001697 (uint8_t *)&pte, 4);
1698 pte = le32_to_cpu(pte);
1699 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001700 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001701 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001702 ~0xfff);
1703 }
1704 }
1705 }
1706 }
1707 }
1708}
1709
aliguori376253e2009-03-05 23:01:23 +00001710static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001711 uint32_t end, int prot)
1712{
bellard9746b152004-11-11 18:30:24 +00001713 int prot1;
1714 prot1 = *plast_prot;
1715 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001716 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001717 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1718 *pstart, end, end - *pstart,
1719 prot1 & PG_USER_MASK ? 'u' : '-',
1720 'r',
1721 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001722 }
1723 if (prot != 0)
1724 *pstart = end;
1725 else
1726 *pstart = -1;
1727 *plast_prot = prot;
1728 }
1729}
1730
aliguori376253e2009-03-05 23:01:23 +00001731static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001732{
bellard6a00d602005-11-21 23:25:50 +00001733 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001734 int l1, l2, prot, last_prot;
1735 uint32_t pgd, pde, pte, start, end;
1736
bellard6a00d602005-11-21 23:25:50 +00001737 env = mon_get_cpu();
1738 if (!env)
1739 return;
1740
bellardb86bda52004-09-18 19:32:46 +00001741 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001742 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001743 return;
1744 }
1745 pgd = env->cr[3] & ~0xfff;
1746 last_prot = 0;
1747 start = -1;
1748 for(l1 = 0; l1 < 1024; l1++) {
1749 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1750 pde = le32_to_cpu(pde);
1751 end = l1 << 22;
1752 if (pde & PG_PRESENT_MASK) {
1753 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1754 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001755 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001756 } else {
1757 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001758 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001759 (uint8_t *)&pte, 4);
1760 pte = le32_to_cpu(pte);
1761 end = (l1 << 22) + (l2 << 12);
1762 if (pte & PG_PRESENT_MASK) {
1763 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1764 } else {
1765 prot = 0;
1766 }
aliguori376253e2009-03-05 23:01:23 +00001767 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001768 }
1769 }
1770 } else {
1771 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001772 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001773 }
1774 }
1775}
1776#endif
1777
aurel327c664e22009-03-03 06:12:22 +00001778#if defined(TARGET_SH4)
1779
aliguori376253e2009-03-05 23:01:23 +00001780static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001781{
aliguori376253e2009-03-05 23:01:23 +00001782 monitor_printf(mon, " tlb%i:\t"
1783 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1784 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1785 "dirty=%hhu writethrough=%hhu\n",
1786 idx,
1787 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1788 tlb->v, tlb->sh, tlb->c, tlb->pr,
1789 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001790}
1791
aliguori376253e2009-03-05 23:01:23 +00001792static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001793{
1794 CPUState *env = mon_get_cpu();
1795 int i;
1796
aliguori376253e2009-03-05 23:01:23 +00001797 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001798 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001799 print_tlb (mon, i, &env->itlb[i]);
1800 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001801 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001802 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001803}
1804
1805#endif
1806
aliguori376253e2009-03-05 23:01:23 +00001807static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001808{
1809#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001810 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001811 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001812 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001813 else
aliguori376253e2009-03-05 23:01:23 +00001814 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001815#else
aliguori376253e2009-03-05 23:01:23 +00001816 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001817#endif
1818}
1819
aliguori030ea372009-04-21 22:30:47 +00001820static void do_info_numa(Monitor *mon)
1821{
aliguorib28b6232009-04-22 20:20:29 +00001822 int i;
aliguori030ea372009-04-21 22:30:47 +00001823 CPUState *env;
1824
1825 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1826 for (i = 0; i < nb_numa_nodes; i++) {
1827 monitor_printf(mon, "node %d cpus:", i);
1828 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1829 if (env->numa_node == i) {
1830 monitor_printf(mon, " %d", env->cpu_index);
1831 }
1832 }
1833 monitor_printf(mon, "\n");
1834 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1835 node_mem[i] >> 20);
1836 }
1837}
1838
bellard5f1ce942006-02-08 22:40:15 +00001839#ifdef CONFIG_PROFILER
1840
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001841int64_t qemu_time;
1842int64_t dev_time;
1843
aliguori376253e2009-03-05 23:01:23 +00001844static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001845{
1846 int64_t total;
1847 total = qemu_time;
1848 if (total == 0)
1849 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001850 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001851 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001852 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001853 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001854 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001855 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001856}
1857#else
aliguori376253e2009-03-05 23:01:23 +00001858static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001859{
aliguori376253e2009-03-05 23:01:23 +00001860 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001861}
1862#endif
1863
bellardec36b692006-07-16 18:57:03 +00001864/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001865static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001866
aliguori376253e2009-03-05 23:01:23 +00001867static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001868{
1869 int i;
1870 CaptureState *s;
1871
1872 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001873 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001874 s->ops.info (s->opaque);
1875 }
1876}
1877
Blue Swirl23130862009-06-06 08:22:04 +00001878#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001879static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001880{
1881 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001882 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001883 CaptureState *s;
1884
1885 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1886 if (i == n) {
1887 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001888 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001889 qemu_free (s);
1890 return;
1891 }
1892 }
1893}
1894
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001895static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001896{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001897 const char *path = qdict_get_str(qdict, "path");
1898 int has_freq = qdict_haskey(qdict, "freq");
1899 int freq = qdict_get_try_int(qdict, "freq", -1);
1900 int has_bits = qdict_haskey(qdict, "bits");
1901 int bits = qdict_get_try_int(qdict, "bits", -1);
1902 int has_channels = qdict_haskey(qdict, "nchannels");
1903 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001904 CaptureState *s;
1905
1906 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001907
1908 freq = has_freq ? freq : 44100;
1909 bits = has_bits ? bits : 16;
1910 nchannels = has_channels ? nchannels : 2;
1911
1912 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001913 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001914 qemu_free (s);
1915 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001916 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001917}
1918#endif
1919
aurel32dc1c0b72008-04-27 23:52:12 +00001920#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001921static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001922{
1923 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001924 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001925
1926 for (env = first_cpu; env != NULL; env = env->next_cpu)
1927 if (env->cpu_index == cpu_index) {
1928 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1929 break;
1930 }
1931}
1932#endif
1933
aliguori376253e2009-03-05 23:01:23 +00001934static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001935{
aurel321b530a62009-04-05 20:08:59 +00001936 if (vm_running) {
1937 if (singlestep) {
1938 monitor_printf(mon, "VM status: running (single step mode)\n");
1939 } else {
1940 monitor_printf(mon, "VM status: running\n");
1941 }
1942 } else
aliguori376253e2009-03-05 23:01:23 +00001943 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001944}
1945
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001946/**
1947 * do_balloon(): Request VM to change its memory allocation
1948 */
1949static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001950{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001951 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001952 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001953 qemu_balloon(target << 20);
1954}
1955
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001956static void monitor_print_balloon(Monitor *mon, const QObject *data)
1957{
Luiz Capitulino7f179672009-12-10 17:15:55 -02001958 QDict *qdict;
1959
1960 qdict = qobject_to_qdict(data);
1961
1962 monitor_printf(mon, "balloon: actual=%" PRId64 "\n",
1963 qdict_get_int(qdict, "balloon") >> 20);
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001964}
1965
1966/**
1967 * do_info_balloon(): Balloon information
Luiz Capitulino7f179672009-12-10 17:15:55 -02001968 *
1969 * Return a QDict with the following information:
1970 *
1971 * - "balloon": current balloon value in bytes
1972 *
1973 * Example:
1974 *
1975 * { "balloon": 1073741824 }
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03001976 */
1977static void do_info_balloon(Monitor *mon, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001978{
Anthony Liguoric227f092009-10-01 16:12:16 -05001979 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001980
1981 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001982 if (kvm_enabled() && !kvm_has_sync_mmu())
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001983 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
aliguoribd322082008-12-04 20:33:06 +00001984 else if (actual == 0)
Luiz Capitulino5d6c37f2009-11-18 23:05:36 -02001985 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
aliguoridf751fa2008-12-04 20:19:35 +00001986 else
Luiz Capitulino7f179672009-12-10 17:15:55 -02001987 *ret_data = qobject_from_jsonf("{ 'balloon': %" PRId64 "}",
1988 (int64_t) actual);
aliguoridf751fa2008-12-04 20:19:35 +00001989}
1990
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001991static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001992{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001993 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001994
aliguori76655d62009-03-06 20:27:37 +00001995 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001996 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001997 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001998 return acl;
1999}
aliguori76655d62009-03-06 20:27:37 +00002000
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002001static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002002{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002003 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002004 qemu_acl *acl = find_acl(mon, aclname);
2005 qemu_acl_entry *entry;
2006 int i = 0;
2007
2008 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002009 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002010 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002011 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002012 i++;
2013 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002014 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002015 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002016 }
2017}
2018
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002019static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002020{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002021 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002022 qemu_acl *acl = find_acl(mon, aclname);
2023
2024 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002025 qemu_acl_reset(acl);
2026 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002027 }
2028}
aliguori76655d62009-03-06 20:27:37 +00002029
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002030static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002031{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002032 const char *aclname = qdict_get_str(qdict, "aclname");
2033 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002034 qemu_acl *acl = find_acl(mon, aclname);
2035
2036 if (acl) {
2037 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002038 acl->defaultDeny = 0;
2039 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002040 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002041 acl->defaultDeny = 1;
2042 monitor_printf(mon, "acl: policy set to 'deny'\n");
2043 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002044 monitor_printf(mon, "acl: unknown policy '%s', "
2045 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002046 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002047 }
2048}
aliguori76655d62009-03-06 20:27:37 +00002049
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002050static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002051{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002052 const char *aclname = qdict_get_str(qdict, "aclname");
2053 const char *match = qdict_get_str(qdict, "match");
2054 const char *policy = qdict_get_str(qdict, "policy");
2055 int has_index = qdict_haskey(qdict, "index");
2056 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002057 qemu_acl *acl = find_acl(mon, aclname);
2058 int deny, ret;
2059
2060 if (acl) {
2061 if (strcmp(policy, "allow") == 0) {
2062 deny = 0;
2063 } else if (strcmp(policy, "deny") == 0) {
2064 deny = 1;
2065 } else {
2066 monitor_printf(mon, "acl: unknown policy '%s', "
2067 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002068 return;
2069 }
aliguori28a76be2009-03-06 20:27:40 +00002070 if (has_index)
2071 ret = qemu_acl_insert(acl, deny, match, index);
2072 else
2073 ret = qemu_acl_append(acl, deny, match);
2074 if (ret < 0)
2075 monitor_printf(mon, "acl: unable to add acl entry\n");
2076 else
2077 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002078 }
2079}
aliguori76655d62009-03-06 20:27:37 +00002080
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002081static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002082{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002083 const char *aclname = qdict_get_str(qdict, "aclname");
2084 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002085 qemu_acl *acl = find_acl(mon, aclname);
2086 int ret;
aliguori76655d62009-03-06 20:27:37 +00002087
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002088 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002089 ret = qemu_acl_remove(acl, match);
2090 if (ret < 0)
2091 monitor_printf(mon, "acl: no matching acl entry\n");
2092 else
2093 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002094 }
2095}
2096
Huang Ying79c4f6b2009-06-23 10:05:14 +08002097#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002098static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002099{
2100 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002101 int cpu_index = qdict_get_int(qdict, "cpu_index");
2102 int bank = qdict_get_int(qdict, "bank");
2103 uint64_t status = qdict_get_int(qdict, "status");
2104 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2105 uint64_t addr = qdict_get_int(qdict, "addr");
2106 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002107
2108 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2109 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2110 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2111 break;
2112 }
2113}
2114#endif
2115
Luiz Capitulinof0d60002009-10-16 12:23:50 -03002116static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002117{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002118 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002119 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002120 int fd;
2121
2122 fd = qemu_chr_get_msgfd(mon->chr);
2123 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002124 qemu_error_new(QERR_FD_NOT_SUPPLIED);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002125 return;
2126 }
2127
2128 if (qemu_isdigit(fdname[0])) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002129 qemu_error_new(QERR_INVALID_PARAMETER, "fdname");
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002130 return;
2131 }
2132
2133 fd = dup(fd);
2134 if (fd == -1) {
Markus Armbruster7cdfcfe2009-12-07 21:37:15 +01002135 if (errno == EMFILE)
2136 qemu_error_new(QERR_TOO_MANY_FILES);
2137 else
2138 qemu_error_new(QERR_UNDEFINED_ERROR);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002139 return;
2140 }
2141
Blue Swirl72cf2d42009-09-12 07:36:22 +00002142 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002143 if (strcmp(monfd->name, fdname) != 0) {
2144 continue;
2145 }
2146
2147 close(monfd->fd);
2148 monfd->fd = fd;
2149 return;
2150 }
2151
Anthony Liguoric227f092009-10-01 16:12:16 -05002152 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002153 monfd->name = qemu_strdup(fdname);
2154 monfd->fd = fd;
2155
Blue Swirl72cf2d42009-09-12 07:36:22 +00002156 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002157}
2158
Luiz Capitulino18f3a512009-10-16 12:23:51 -03002159static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002160{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002161 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002162 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002163
Blue Swirl72cf2d42009-09-12 07:36:22 +00002164 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002165 if (strcmp(monfd->name, fdname) != 0) {
2166 continue;
2167 }
2168
Blue Swirl72cf2d42009-09-12 07:36:22 +00002169 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002170 close(monfd->fd);
2171 qemu_free(monfd->name);
2172 qemu_free(monfd);
2173 return;
2174 }
2175
Markus Armbruster063c1a02009-12-07 21:37:11 +01002176 qemu_error_new(QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002177}
2178
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002179static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002180{
2181 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002182 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002183
2184 vm_stop(0);
2185
Juan Quintela05f24012009-08-20 19:42:22 +02002186 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002187 vm_start();
2188}
2189
Mark McLoughlin7768e042009-07-22 09:11:41 +01002190int monitor_get_fd(Monitor *mon, const char *fdname)
2191{
Anthony Liguoric227f092009-10-01 16:12:16 -05002192 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002193
Blue Swirl72cf2d42009-09-12 07:36:22 +00002194 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002195 int fd;
2196
2197 if (strcmp(monfd->name, fdname) != 0) {
2198 continue;
2199 }
2200
2201 fd = monfd->fd;
2202
2203 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002204 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002205 qemu_free(monfd->name);
2206 qemu_free(monfd);
2207
2208 return fd;
2209 }
2210
2211 return -1;
2212}
2213
Anthony Liguoric227f092009-10-01 16:12:16 -05002214static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002215#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002216 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002217};
2218
Blue Swirl23130862009-06-06 08:22:04 +00002219/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002220static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002221 {
2222 .name = "version",
2223 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002224 .params = "",
2225 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002226 .user_print = monitor_print_qobject,
2227 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002228 },
2229 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002230 .name = "commands",
2231 .args_type = "",
2232 .params = "",
2233 .help = "list QMP available commands",
2234 .user_print = monitor_user_noop,
2235 .mhandler.info_new = do_info_commands,
2236 },
2237 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002238 .name = "network",
2239 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002240 .params = "",
2241 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002242 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002243 },
2244 {
2245 .name = "chardev",
2246 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002247 .params = "",
2248 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002249 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002250 },
2251 {
2252 .name = "block",
2253 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002254 .params = "",
2255 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002256 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002257 },
2258 {
2259 .name = "blockstats",
2260 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002261 .params = "",
2262 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002263 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002264 },
2265 {
2266 .name = "registers",
2267 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002268 .params = "",
2269 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002270 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002271 },
2272 {
2273 .name = "cpus",
2274 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002275 .params = "",
2276 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002277 .user_print = monitor_print_cpus,
2278 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002279 },
2280 {
2281 .name = "history",
2282 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002283 .params = "",
2284 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002285 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002286 },
2287 {
2288 .name = "irq",
2289 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002290 .params = "",
2291 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002292 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002293 },
2294 {
2295 .name = "pic",
2296 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002297 .params = "",
2298 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002299 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002300 },
2301 {
2302 .name = "pci",
2303 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002304 .params = "",
2305 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002306 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002307 },
aurel327c664e22009-03-03 06:12:22 +00002308#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002309 {
2310 .name = "tlb",
2311 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002312 .params = "",
2313 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002314 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002315 },
aurel327c664e22009-03-03 06:12:22 +00002316#endif
2317#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002318 {
2319 .name = "mem",
2320 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002321 .params = "",
2322 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002323 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002324 },
2325 {
2326 .name = "hpet",
2327 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002328 .params = "",
2329 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03002330 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002331 },
bellardb86bda52004-09-18 19:32:46 +00002332#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002333 {
2334 .name = "jit",
2335 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002336 .params = "",
2337 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002338 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002339 },
2340 {
2341 .name = "kvm",
2342 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002343 .params = "",
2344 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002345 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002346 },
2347 {
2348 .name = "numa",
2349 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002350 .params = "",
2351 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002352 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002353 },
2354 {
2355 .name = "usb",
2356 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002357 .params = "",
2358 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002359 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002360 },
2361 {
2362 .name = "usbhost",
2363 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002364 .params = "",
2365 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002366 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002367 },
2368 {
2369 .name = "profile",
2370 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002371 .params = "",
2372 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002373 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002374 },
2375 {
2376 .name = "capture",
2377 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002378 .params = "",
2379 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002380 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002381 },
2382 {
2383 .name = "snapshots",
2384 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002385 .params = "",
2386 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002387 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002388 },
2389 {
2390 .name = "status",
2391 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002392 .params = "",
2393 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002394 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002395 },
2396 {
2397 .name = "pcmcia",
2398 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002399 .params = "",
2400 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002401 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002402 },
2403 {
2404 .name = "mice",
2405 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002406 .params = "",
2407 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002408 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002409 },
2410 {
2411 .name = "vnc",
2412 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002413 .params = "",
2414 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002415 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002416 },
2417 {
2418 .name = "name",
2419 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002420 .params = "",
2421 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002422 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002423 },
2424 {
2425 .name = "uuid",
2426 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002427 .params = "",
2428 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002429 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002430 },
j_mayer76a66252007-03-07 08:32:30 +00002431#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002432 {
2433 .name = "cpustats",
2434 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002435 .params = "",
2436 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002437 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002438 },
j_mayer76a66252007-03-07 08:32:30 +00002439#endif
blueswir131a60e22007-10-26 18:42:59 +00002440#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002441 {
2442 .name = "usernet",
2443 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002444 .params = "",
2445 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002446 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002447 },
blueswir131a60e22007-10-26 18:42:59 +00002448#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002449 {
2450 .name = "migrate",
2451 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002452 .params = "",
2453 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002454 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002455 },
2456 {
2457 .name = "balloon",
2458 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002459 .params = "",
2460 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002461 .user_print = monitor_print_balloon,
2462 .mhandler.info_new = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002463 },
2464 {
2465 .name = "qtree",
2466 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002467 .params = "",
2468 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002469 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002470 },
2471 {
2472 .name = "qdm",
2473 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002474 .params = "",
2475 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002476 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002477 },
2478 {
2479 .name = "roms",
2480 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002481 .params = "",
2482 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002483 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002484 },
2485 {
2486 .name = NULL,
2487 },
bellard9dc39cb2004-03-14 21:38:27 +00002488};
2489
bellard9307c4c2004-04-04 12:57:25 +00002490/*******************************************************************/
2491
2492static const char *pch;
2493static jmp_buf expr_env;
2494
bellard92a31b12005-02-10 22:00:52 +00002495#define MD_TLONG 0
2496#define MD_I32 1
2497
bellard9307c4c2004-04-04 12:57:25 +00002498typedef struct MonitorDef {
2499 const char *name;
2500 int offset;
blueswir18662d652008-10-02 18:32:44 +00002501 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002502 int type;
bellard9307c4c2004-04-04 12:57:25 +00002503} MonitorDef;
2504
bellard57206fd2004-04-25 18:54:52 +00002505#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002506static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002507{
bellard6a00d602005-11-21 23:25:50 +00002508 CPUState *env = mon_get_cpu();
2509 if (!env)
2510 return 0;
2511 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002512}
2513#endif
2514
bellarda541f292004-04-12 20:39:29 +00002515#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002516static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002517{
bellard6a00d602005-11-21 23:25:50 +00002518 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002519 unsigned int u;
2520 int i;
2521
bellard6a00d602005-11-21 23:25:50 +00002522 if (!env)
2523 return 0;
2524
bellarda541f292004-04-12 20:39:29 +00002525 u = 0;
2526 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002527 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002528
2529 return u;
2530}
2531
blueswir18662d652008-10-02 18:32:44 +00002532static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002533{
bellard6a00d602005-11-21 23:25:50 +00002534 CPUState *env = mon_get_cpu();
2535 if (!env)
2536 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002537 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002538}
2539
blueswir18662d652008-10-02 18:32:44 +00002540static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002541{
bellard6a00d602005-11-21 23:25:50 +00002542 CPUState *env = mon_get_cpu();
2543 if (!env)
2544 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002545 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002546}
bellard9fddaa02004-05-21 12:59:32 +00002547
blueswir18662d652008-10-02 18:32:44 +00002548static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002549{
bellard6a00d602005-11-21 23:25:50 +00002550 CPUState *env = mon_get_cpu();
2551 if (!env)
2552 return 0;
2553 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002554}
2555
blueswir18662d652008-10-02 18:32:44 +00002556static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002557{
bellard6a00d602005-11-21 23:25:50 +00002558 CPUState *env = mon_get_cpu();
2559 if (!env)
2560 return 0;
2561 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002562}
2563
blueswir18662d652008-10-02 18:32:44 +00002564static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002565{
bellard6a00d602005-11-21 23:25:50 +00002566 CPUState *env = mon_get_cpu();
2567 if (!env)
2568 return 0;
2569 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002570}
bellarda541f292004-04-12 20:39:29 +00002571#endif
2572
bellarde95c8d52004-09-30 22:22:08 +00002573#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002574#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002575static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002576{
bellard6a00d602005-11-21 23:25:50 +00002577 CPUState *env = mon_get_cpu();
2578 if (!env)
2579 return 0;
2580 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002581}
bellard7b936c02005-10-30 17:05:13 +00002582#endif
bellarde95c8d52004-09-30 22:22:08 +00002583
blueswir18662d652008-10-02 18:32:44 +00002584static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002585{
bellard6a00d602005-11-21 23:25:50 +00002586 CPUState *env = mon_get_cpu();
2587 if (!env)
2588 return 0;
2589 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002590}
2591#endif
2592
blueswir18662d652008-10-02 18:32:44 +00002593static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002594#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002595
2596#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002597 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002598 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002599 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002600
bellard9307c4c2004-04-04 12:57:25 +00002601 { "eax", offsetof(CPUState, regs[0]) },
2602 { "ecx", offsetof(CPUState, regs[1]) },
2603 { "edx", offsetof(CPUState, regs[2]) },
2604 { "ebx", offsetof(CPUState, regs[3]) },
2605 { "esp|sp", offsetof(CPUState, regs[4]) },
2606 { "ebp|fp", offsetof(CPUState, regs[5]) },
2607 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002608 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002609#ifdef TARGET_X86_64
2610 { "r8", offsetof(CPUState, regs[8]) },
2611 { "r9", offsetof(CPUState, regs[9]) },
2612 { "r10", offsetof(CPUState, regs[10]) },
2613 { "r11", offsetof(CPUState, regs[11]) },
2614 { "r12", offsetof(CPUState, regs[12]) },
2615 { "r13", offsetof(CPUState, regs[13]) },
2616 { "r14", offsetof(CPUState, regs[14]) },
2617 { "r15", offsetof(CPUState, regs[15]) },
2618#endif
bellard9307c4c2004-04-04 12:57:25 +00002619 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002620 { "eip", offsetof(CPUState, eip) },
2621 SEG("cs", R_CS)
2622 SEG("ds", R_DS)
2623 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002624 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002625 SEG("fs", R_FS)
2626 SEG("gs", R_GS)
2627 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002628#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002629 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002630 { "r0", offsetof(CPUState, gpr[0]) },
2631 { "r1", offsetof(CPUState, gpr[1]) },
2632 { "r2", offsetof(CPUState, gpr[2]) },
2633 { "r3", offsetof(CPUState, gpr[3]) },
2634 { "r4", offsetof(CPUState, gpr[4]) },
2635 { "r5", offsetof(CPUState, gpr[5]) },
2636 { "r6", offsetof(CPUState, gpr[6]) },
2637 { "r7", offsetof(CPUState, gpr[7]) },
2638 { "r8", offsetof(CPUState, gpr[8]) },
2639 { "r9", offsetof(CPUState, gpr[9]) },
2640 { "r10", offsetof(CPUState, gpr[10]) },
2641 { "r11", offsetof(CPUState, gpr[11]) },
2642 { "r12", offsetof(CPUState, gpr[12]) },
2643 { "r13", offsetof(CPUState, gpr[13]) },
2644 { "r14", offsetof(CPUState, gpr[14]) },
2645 { "r15", offsetof(CPUState, gpr[15]) },
2646 { "r16", offsetof(CPUState, gpr[16]) },
2647 { "r17", offsetof(CPUState, gpr[17]) },
2648 { "r18", offsetof(CPUState, gpr[18]) },
2649 { "r19", offsetof(CPUState, gpr[19]) },
2650 { "r20", offsetof(CPUState, gpr[20]) },
2651 { "r21", offsetof(CPUState, gpr[21]) },
2652 { "r22", offsetof(CPUState, gpr[22]) },
2653 { "r23", offsetof(CPUState, gpr[23]) },
2654 { "r24", offsetof(CPUState, gpr[24]) },
2655 { "r25", offsetof(CPUState, gpr[25]) },
2656 { "r26", offsetof(CPUState, gpr[26]) },
2657 { "r27", offsetof(CPUState, gpr[27]) },
2658 { "r28", offsetof(CPUState, gpr[28]) },
2659 { "r29", offsetof(CPUState, gpr[29]) },
2660 { "r30", offsetof(CPUState, gpr[30]) },
2661 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002662 /* Floating point registers */
2663 { "f0", offsetof(CPUState, fpr[0]) },
2664 { "f1", offsetof(CPUState, fpr[1]) },
2665 { "f2", offsetof(CPUState, fpr[2]) },
2666 { "f3", offsetof(CPUState, fpr[3]) },
2667 { "f4", offsetof(CPUState, fpr[4]) },
2668 { "f5", offsetof(CPUState, fpr[5]) },
2669 { "f6", offsetof(CPUState, fpr[6]) },
2670 { "f7", offsetof(CPUState, fpr[7]) },
2671 { "f8", offsetof(CPUState, fpr[8]) },
2672 { "f9", offsetof(CPUState, fpr[9]) },
2673 { "f10", offsetof(CPUState, fpr[10]) },
2674 { "f11", offsetof(CPUState, fpr[11]) },
2675 { "f12", offsetof(CPUState, fpr[12]) },
2676 { "f13", offsetof(CPUState, fpr[13]) },
2677 { "f14", offsetof(CPUState, fpr[14]) },
2678 { "f15", offsetof(CPUState, fpr[15]) },
2679 { "f16", offsetof(CPUState, fpr[16]) },
2680 { "f17", offsetof(CPUState, fpr[17]) },
2681 { "f18", offsetof(CPUState, fpr[18]) },
2682 { "f19", offsetof(CPUState, fpr[19]) },
2683 { "f20", offsetof(CPUState, fpr[20]) },
2684 { "f21", offsetof(CPUState, fpr[21]) },
2685 { "f22", offsetof(CPUState, fpr[22]) },
2686 { "f23", offsetof(CPUState, fpr[23]) },
2687 { "f24", offsetof(CPUState, fpr[24]) },
2688 { "f25", offsetof(CPUState, fpr[25]) },
2689 { "f26", offsetof(CPUState, fpr[26]) },
2690 { "f27", offsetof(CPUState, fpr[27]) },
2691 { "f28", offsetof(CPUState, fpr[28]) },
2692 { "f29", offsetof(CPUState, fpr[29]) },
2693 { "f30", offsetof(CPUState, fpr[30]) },
2694 { "f31", offsetof(CPUState, fpr[31]) },
2695 { "fpscr", offsetof(CPUState, fpscr) },
2696 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002697 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002698 { "lr", offsetof(CPUState, lr) },
2699 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002700 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002701 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002702 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002703 { "msr", 0, &monitor_get_msr, },
2704 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002705 { "tbu", 0, &monitor_get_tbu, },
2706 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002707#if defined(TARGET_PPC64)
2708 /* Address space register */
2709 { "asr", offsetof(CPUState, asr) },
2710#endif
2711 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002712 { "sdr1", offsetof(CPUState, sdr1) },
2713 { "sr0", offsetof(CPUState, sr[0]) },
2714 { "sr1", offsetof(CPUState, sr[1]) },
2715 { "sr2", offsetof(CPUState, sr[2]) },
2716 { "sr3", offsetof(CPUState, sr[3]) },
2717 { "sr4", offsetof(CPUState, sr[4]) },
2718 { "sr5", offsetof(CPUState, sr[5]) },
2719 { "sr6", offsetof(CPUState, sr[6]) },
2720 { "sr7", offsetof(CPUState, sr[7]) },
2721 { "sr8", offsetof(CPUState, sr[8]) },
2722 { "sr9", offsetof(CPUState, sr[9]) },
2723 { "sr10", offsetof(CPUState, sr[10]) },
2724 { "sr11", offsetof(CPUState, sr[11]) },
2725 { "sr12", offsetof(CPUState, sr[12]) },
2726 { "sr13", offsetof(CPUState, sr[13]) },
2727 { "sr14", offsetof(CPUState, sr[14]) },
2728 { "sr15", offsetof(CPUState, sr[15]) },
2729 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002730#elif defined(TARGET_SPARC)
2731 { "g0", offsetof(CPUState, gregs[0]) },
2732 { "g1", offsetof(CPUState, gregs[1]) },
2733 { "g2", offsetof(CPUState, gregs[2]) },
2734 { "g3", offsetof(CPUState, gregs[3]) },
2735 { "g4", offsetof(CPUState, gregs[4]) },
2736 { "g5", offsetof(CPUState, gregs[5]) },
2737 { "g6", offsetof(CPUState, gregs[6]) },
2738 { "g7", offsetof(CPUState, gregs[7]) },
2739 { "o0", 0, monitor_get_reg },
2740 { "o1", 1, monitor_get_reg },
2741 { "o2", 2, monitor_get_reg },
2742 { "o3", 3, monitor_get_reg },
2743 { "o4", 4, monitor_get_reg },
2744 { "o5", 5, monitor_get_reg },
2745 { "o6", 6, monitor_get_reg },
2746 { "o7", 7, monitor_get_reg },
2747 { "l0", 8, monitor_get_reg },
2748 { "l1", 9, monitor_get_reg },
2749 { "l2", 10, monitor_get_reg },
2750 { "l3", 11, monitor_get_reg },
2751 { "l4", 12, monitor_get_reg },
2752 { "l5", 13, monitor_get_reg },
2753 { "l6", 14, monitor_get_reg },
2754 { "l7", 15, monitor_get_reg },
2755 { "i0", 16, monitor_get_reg },
2756 { "i1", 17, monitor_get_reg },
2757 { "i2", 18, monitor_get_reg },
2758 { "i3", 19, monitor_get_reg },
2759 { "i4", 20, monitor_get_reg },
2760 { "i5", 21, monitor_get_reg },
2761 { "i6", 22, monitor_get_reg },
2762 { "i7", 23, monitor_get_reg },
2763 { "pc", offsetof(CPUState, pc) },
2764 { "npc", offsetof(CPUState, npc) },
2765 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002766#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002767 { "psr", 0, &monitor_get_psr, },
2768 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002769#endif
bellarde95c8d52004-09-30 22:22:08 +00002770 { "tbr", offsetof(CPUState, tbr) },
2771 { "fsr", offsetof(CPUState, fsr) },
2772 { "f0", offsetof(CPUState, fpr[0]) },
2773 { "f1", offsetof(CPUState, fpr[1]) },
2774 { "f2", offsetof(CPUState, fpr[2]) },
2775 { "f3", offsetof(CPUState, fpr[3]) },
2776 { "f4", offsetof(CPUState, fpr[4]) },
2777 { "f5", offsetof(CPUState, fpr[5]) },
2778 { "f6", offsetof(CPUState, fpr[6]) },
2779 { "f7", offsetof(CPUState, fpr[7]) },
2780 { "f8", offsetof(CPUState, fpr[8]) },
2781 { "f9", offsetof(CPUState, fpr[9]) },
2782 { "f10", offsetof(CPUState, fpr[10]) },
2783 { "f11", offsetof(CPUState, fpr[11]) },
2784 { "f12", offsetof(CPUState, fpr[12]) },
2785 { "f13", offsetof(CPUState, fpr[13]) },
2786 { "f14", offsetof(CPUState, fpr[14]) },
2787 { "f15", offsetof(CPUState, fpr[15]) },
2788 { "f16", offsetof(CPUState, fpr[16]) },
2789 { "f17", offsetof(CPUState, fpr[17]) },
2790 { "f18", offsetof(CPUState, fpr[18]) },
2791 { "f19", offsetof(CPUState, fpr[19]) },
2792 { "f20", offsetof(CPUState, fpr[20]) },
2793 { "f21", offsetof(CPUState, fpr[21]) },
2794 { "f22", offsetof(CPUState, fpr[22]) },
2795 { "f23", offsetof(CPUState, fpr[23]) },
2796 { "f24", offsetof(CPUState, fpr[24]) },
2797 { "f25", offsetof(CPUState, fpr[25]) },
2798 { "f26", offsetof(CPUState, fpr[26]) },
2799 { "f27", offsetof(CPUState, fpr[27]) },
2800 { "f28", offsetof(CPUState, fpr[28]) },
2801 { "f29", offsetof(CPUState, fpr[29]) },
2802 { "f30", offsetof(CPUState, fpr[30]) },
2803 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002804#ifdef TARGET_SPARC64
2805 { "f32", offsetof(CPUState, fpr[32]) },
2806 { "f34", offsetof(CPUState, fpr[34]) },
2807 { "f36", offsetof(CPUState, fpr[36]) },
2808 { "f38", offsetof(CPUState, fpr[38]) },
2809 { "f40", offsetof(CPUState, fpr[40]) },
2810 { "f42", offsetof(CPUState, fpr[42]) },
2811 { "f44", offsetof(CPUState, fpr[44]) },
2812 { "f46", offsetof(CPUState, fpr[46]) },
2813 { "f48", offsetof(CPUState, fpr[48]) },
2814 { "f50", offsetof(CPUState, fpr[50]) },
2815 { "f52", offsetof(CPUState, fpr[52]) },
2816 { "f54", offsetof(CPUState, fpr[54]) },
2817 { "f56", offsetof(CPUState, fpr[56]) },
2818 { "f58", offsetof(CPUState, fpr[58]) },
2819 { "f60", offsetof(CPUState, fpr[60]) },
2820 { "f62", offsetof(CPUState, fpr[62]) },
2821 { "asi", offsetof(CPUState, asi) },
2822 { "pstate", offsetof(CPUState, pstate) },
2823 { "cansave", offsetof(CPUState, cansave) },
2824 { "canrestore", offsetof(CPUState, canrestore) },
2825 { "otherwin", offsetof(CPUState, otherwin) },
2826 { "wstate", offsetof(CPUState, wstate) },
2827 { "cleanwin", offsetof(CPUState, cleanwin) },
2828 { "fprs", offsetof(CPUState, fprs) },
2829#endif
bellard9307c4c2004-04-04 12:57:25 +00002830#endif
2831 { NULL },
2832};
2833
aliguori376253e2009-03-05 23:01:23 +00002834static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002835{
aliguori376253e2009-03-05 23:01:23 +00002836 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002837 longjmp(expr_env, 1);
2838}
2839
bellard6a00d602005-11-21 23:25:50 +00002840/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002841static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002842{
blueswir18662d652008-10-02 18:32:44 +00002843 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002844 void *ptr;
2845
bellard9307c4c2004-04-04 12:57:25 +00002846 for(md = monitor_defs; md->name != NULL; md++) {
2847 if (compare_cmd(name, md->name)) {
2848 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002849 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002850 } else {
bellard6a00d602005-11-21 23:25:50 +00002851 CPUState *env = mon_get_cpu();
2852 if (!env)
2853 return -2;
2854 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002855 switch(md->type) {
2856 case MD_I32:
2857 *pval = *(int32_t *)ptr;
2858 break;
2859 case MD_TLONG:
2860 *pval = *(target_long *)ptr;
2861 break;
2862 default:
2863 *pval = 0;
2864 break;
2865 }
bellard9307c4c2004-04-04 12:57:25 +00002866 }
2867 return 0;
2868 }
2869 }
2870 return -1;
2871}
2872
2873static void next(void)
2874{
Blue Swirl660f11b2009-07-31 21:16:51 +00002875 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002876 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002877 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002878 pch++;
2879 }
2880}
2881
aliguori376253e2009-03-05 23:01:23 +00002882static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002883
aliguori376253e2009-03-05 23:01:23 +00002884static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002885{
blueswir1c2efc952007-09-25 17:28:42 +00002886 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002887 char *p;
bellard6a00d602005-11-21 23:25:50 +00002888 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002889
2890 switch(*pch) {
2891 case '+':
2892 next();
aliguori376253e2009-03-05 23:01:23 +00002893 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002894 break;
2895 case '-':
2896 next();
aliguori376253e2009-03-05 23:01:23 +00002897 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002898 break;
2899 case '~':
2900 next();
aliguori376253e2009-03-05 23:01:23 +00002901 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002902 break;
2903 case '(':
2904 next();
aliguori376253e2009-03-05 23:01:23 +00002905 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002906 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002907 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002908 }
2909 next();
2910 break;
bellard81d09122004-07-14 17:21:37 +00002911 case '\'':
2912 pch++;
2913 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002914 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002915 n = *pch;
2916 pch++;
2917 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002918 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002919 next();
2920 break;
bellard9307c4c2004-04-04 12:57:25 +00002921 case '$':
2922 {
2923 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002924 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002925
bellard9307c4c2004-04-04 12:57:25 +00002926 pch++;
2927 q = buf;
2928 while ((*pch >= 'a' && *pch <= 'z') ||
2929 (*pch >= 'A' && *pch <= 'Z') ||
2930 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002931 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002932 if ((q - buf) < sizeof(buf) - 1)
2933 *q++ = *pch;
2934 pch++;
2935 }
blueswir1cd390082008-11-16 13:53:32 +00002936 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002937 pch++;
2938 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002939 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002940 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002941 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002942 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002943 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002944 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002945 }
2946 break;
2947 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002948 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002949 n = 0;
2950 break;
2951 default:
blueswir17743e582007-09-24 18:39:04 +00002952#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002953 n = strtoull(pch, &p, 0);
2954#else
bellard9307c4c2004-04-04 12:57:25 +00002955 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002956#endif
bellard9307c4c2004-04-04 12:57:25 +00002957 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002958 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002959 }
2960 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002961 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002962 pch++;
2963 break;
2964 }
2965 return n;
2966}
2967
2968
aliguori376253e2009-03-05 23:01:23 +00002969static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002970{
blueswir1c2efc952007-09-25 17:28:42 +00002971 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002972 int op;
ths3b46e622007-09-17 08:09:54 +00002973
aliguori376253e2009-03-05 23:01:23 +00002974 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002975 for(;;) {
2976 op = *pch;
2977 if (op != '*' && op != '/' && op != '%')
2978 break;
2979 next();
aliguori376253e2009-03-05 23:01:23 +00002980 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002981 switch(op) {
2982 default:
2983 case '*':
2984 val *= val2;
2985 break;
2986 case '/':
2987 case '%':
ths5fafdf22007-09-16 21:08:06 +00002988 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002989 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002990 if (op == '/')
2991 val /= val2;
2992 else
2993 val %= val2;
2994 break;
2995 }
2996 }
2997 return val;
2998}
2999
aliguori376253e2009-03-05 23:01:23 +00003000static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003001{
blueswir1c2efc952007-09-25 17:28:42 +00003002 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003003 int op;
bellard9307c4c2004-04-04 12:57:25 +00003004
aliguori376253e2009-03-05 23:01:23 +00003005 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003006 for(;;) {
3007 op = *pch;
3008 if (op != '&' && op != '|' && op != '^')
3009 break;
3010 next();
aliguori376253e2009-03-05 23:01:23 +00003011 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003012 switch(op) {
3013 default:
3014 case '&':
3015 val &= val2;
3016 break;
3017 case '|':
3018 val |= val2;
3019 break;
3020 case '^':
3021 val ^= val2;
3022 break;
3023 }
3024 }
3025 return val;
3026}
3027
aliguori376253e2009-03-05 23:01:23 +00003028static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003029{
blueswir1c2efc952007-09-25 17:28:42 +00003030 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003031 int op;
bellard9307c4c2004-04-04 12:57:25 +00003032
aliguori376253e2009-03-05 23:01:23 +00003033 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003034 for(;;) {
3035 op = *pch;
3036 if (op != '+' && op != '-')
3037 break;
3038 next();
aliguori376253e2009-03-05 23:01:23 +00003039 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003040 if (op == '+')
3041 val += val2;
3042 else
3043 val -= val2;
3044 }
3045 return val;
3046}
3047
aliguori376253e2009-03-05 23:01:23 +00003048static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003049{
3050 pch = *pp;
3051 if (setjmp(expr_env)) {
3052 *pp = pch;
3053 return -1;
3054 }
blueswir1cd390082008-11-16 13:53:32 +00003055 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003056 pch++;
aliguori376253e2009-03-05 23:01:23 +00003057 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003058 *pp = pch;
3059 return 0;
3060}
3061
3062static int get_str(char *buf, int buf_size, const char **pp)
3063{
3064 const char *p;
3065 char *q;
3066 int c;
3067
bellard81d09122004-07-14 17:21:37 +00003068 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003069 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003070 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003071 p++;
3072 if (*p == '\0') {
3073 fail:
bellard81d09122004-07-14 17:21:37 +00003074 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003075 *pp = p;
3076 return -1;
3077 }
bellard9307c4c2004-04-04 12:57:25 +00003078 if (*p == '\"') {
3079 p++;
3080 while (*p != '\0' && *p != '\"') {
3081 if (*p == '\\') {
3082 p++;
3083 c = *p++;
3084 switch(c) {
3085 case 'n':
3086 c = '\n';
3087 break;
3088 case 'r':
3089 c = '\r';
3090 break;
3091 case '\\':
3092 case '\'':
3093 case '\"':
3094 break;
3095 default:
3096 qemu_printf("unsupported escape code: '\\%c'\n", c);
3097 goto fail;
3098 }
3099 if ((q - buf) < buf_size - 1) {
3100 *q++ = c;
3101 }
3102 } else {
3103 if ((q - buf) < buf_size - 1) {
3104 *q++ = *p;
3105 }
3106 p++;
3107 }
3108 }
3109 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003110 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003111 goto fail;
3112 }
3113 p++;
3114 } else {
blueswir1cd390082008-11-16 13:53:32 +00003115 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003116 if ((q - buf) < buf_size - 1) {
3117 *q++ = *p;
3118 }
3119 p++;
3120 }
bellard9307c4c2004-04-04 12:57:25 +00003121 }
bellard81d09122004-07-14 17:21:37 +00003122 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003123 *pp = p;
3124 return 0;
3125}
3126
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003127/*
3128 * Store the command-name in cmdname, and return a pointer to
3129 * the remaining of the command string.
3130 */
3131static const char *get_command_name(const char *cmdline,
3132 char *cmdname, size_t nlen)
3133{
3134 size_t len;
3135 const char *p, *pstart;
3136
3137 p = cmdline;
3138 while (qemu_isspace(*p))
3139 p++;
3140 if (*p == '\0')
3141 return NULL;
3142 pstart = p;
3143 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3144 p++;
3145 len = p - pstart;
3146 if (len > nlen - 1)
3147 len = nlen - 1;
3148 memcpy(cmdname, pstart, len);
3149 cmdname[len] = '\0';
3150 return p;
3151}
3152
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003153/**
3154 * Read key of 'type' into 'key' and return the current
3155 * 'type' pointer.
3156 */
3157static char *key_get_info(const char *type, char **key)
3158{
3159 size_t len;
3160 char *p, *str;
3161
3162 if (*type == ',')
3163 type++;
3164
3165 p = strchr(type, ':');
3166 if (!p) {
3167 *key = NULL;
3168 return NULL;
3169 }
3170 len = p - type;
3171
3172 str = qemu_malloc(len + 1);
3173 memcpy(str, type, len);
3174 str[len] = '\0';
3175
3176 *key = str;
3177 return ++p;
3178}
3179
bellard9307c4c2004-04-04 12:57:25 +00003180static int default_fmt_format = 'x';
3181static int default_fmt_size = 4;
3182
3183#define MAX_ARGS 16
3184
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003185static int is_valid_option(const char *c, const char *typestr)
3186{
3187 char option[3];
3188
3189 option[0] = '-';
3190 option[1] = *c;
3191 option[2] = '\0';
3192
3193 typestr = strstr(typestr, option);
3194 return (typestr != NULL);
3195}
3196
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003197static const mon_cmd_t *monitor_find_command(const char *cmdname)
3198{
3199 const mon_cmd_t *cmd;
3200
3201 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3202 if (compare_cmd(cmdname, cmd->name)) {
3203 return cmd;
3204 }
3205 }
3206
3207 return NULL;
3208}
3209
Anthony Liguoric227f092009-10-01 16:12:16 -05003210static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003211 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003212 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003213{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003214 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003215 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003216 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003217 char cmdname[256];
3218 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003219 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003220
3221#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003222 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003223#endif
ths3b46e622007-09-17 08:09:54 +00003224
bellard9307c4c2004-04-04 12:57:25 +00003225 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003226 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3227 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003228 return NULL;
ths3b46e622007-09-17 08:09:54 +00003229
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003230 cmd = monitor_find_command(cmdname);
3231 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003232 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003233 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003234 }
bellard9307c4c2004-04-04 12:57:25 +00003235
bellard9307c4c2004-04-04 12:57:25 +00003236 /* parse the parameters */
3237 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003238 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003239 typestr = key_get_info(typestr, &key);
3240 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003241 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003242 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003243 typestr++;
3244 switch(c) {
3245 case 'F':
bellard81d09122004-07-14 17:21:37 +00003246 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003247 case 's':
3248 {
3249 int ret;
ths3b46e622007-09-17 08:09:54 +00003250
blueswir1cd390082008-11-16 13:53:32 +00003251 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003252 p++;
3253 if (*typestr == '?') {
3254 typestr++;
3255 if (*p == '\0') {
3256 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003257 break;
bellard9307c4c2004-04-04 12:57:25 +00003258 }
3259 }
3260 ret = get_str(buf, sizeof(buf), &p);
3261 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003262 switch(c) {
3263 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003264 monitor_printf(mon, "%s: filename expected\n",
3265 cmdname);
bellard81d09122004-07-14 17:21:37 +00003266 break;
3267 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003268 monitor_printf(mon, "%s: block device name expected\n",
3269 cmdname);
bellard81d09122004-07-14 17:21:37 +00003270 break;
3271 default:
aliguori376253e2009-03-05 23:01:23 +00003272 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003273 break;
3274 }
bellard9307c4c2004-04-04 12:57:25 +00003275 goto fail;
3276 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003277 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003278 }
3279 break;
3280 case '/':
3281 {
3282 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003283
blueswir1cd390082008-11-16 13:53:32 +00003284 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003285 p++;
3286 if (*p == '/') {
3287 /* format found */
3288 p++;
3289 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003290 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003291 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003292 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003293 count = count * 10 + (*p - '0');
3294 p++;
3295 }
3296 }
3297 size = -1;
3298 format = -1;
3299 for(;;) {
3300 switch(*p) {
3301 case 'o':
3302 case 'd':
3303 case 'u':
3304 case 'x':
3305 case 'i':
3306 case 'c':
3307 format = *p++;
3308 break;
3309 case 'b':
3310 size = 1;
3311 p++;
3312 break;
3313 case 'h':
3314 size = 2;
3315 p++;
3316 break;
3317 case 'w':
3318 size = 4;
3319 p++;
3320 break;
3321 case 'g':
3322 case 'L':
3323 size = 8;
3324 p++;
3325 break;
3326 default:
3327 goto next;
3328 }
3329 }
3330 next:
blueswir1cd390082008-11-16 13:53:32 +00003331 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003332 monitor_printf(mon, "invalid char in format: '%c'\n",
3333 *p);
bellard9307c4c2004-04-04 12:57:25 +00003334 goto fail;
3335 }
bellard9307c4c2004-04-04 12:57:25 +00003336 if (format < 0)
3337 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003338 if (format != 'i') {
3339 /* for 'i', not specifying a size gives -1 as size */
3340 if (size < 0)
3341 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003342 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003343 }
bellard9307c4c2004-04-04 12:57:25 +00003344 default_fmt_format = format;
3345 } else {
3346 count = 1;
3347 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003348 if (format != 'i') {
3349 size = default_fmt_size;
3350 } else {
3351 size = -1;
3352 }
bellard9307c4c2004-04-04 12:57:25 +00003353 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003354 qdict_put(qdict, "count", qint_from_int(count));
3355 qdict_put(qdict, "format", qint_from_int(format));
3356 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003357 }
3358 break;
3359 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003360 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00003361 {
blueswir1c2efc952007-09-25 17:28:42 +00003362 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003363
blueswir1cd390082008-11-16 13:53:32 +00003364 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003365 p++;
bellard34405572004-06-08 00:55:58 +00003366 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003367 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003368 if (*p == '\0') {
3369 typestr++;
3370 break;
3371 }
bellard34405572004-06-08 00:55:58 +00003372 } else {
3373 if (*p == '.') {
3374 p++;
blueswir1cd390082008-11-16 13:53:32 +00003375 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003376 p++;
bellard34405572004-06-08 00:55:58 +00003377 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003378 typestr++;
3379 break;
bellard34405572004-06-08 00:55:58 +00003380 }
3381 }
bellard13224a82006-07-14 22:03:35 +00003382 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003383 }
aliguori376253e2009-03-05 23:01:23 +00003384 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003385 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003386 /* Check if 'i' is greater than 32-bit */
3387 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3388 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3389 monitor_printf(mon, "integer is for 32-bit values\n");
3390 goto fail;
3391 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003392 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003393 }
3394 break;
3395 case '-':
3396 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003397 const char *tmp = p;
3398 int has_option, skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003399 /* option */
ths3b46e622007-09-17 08:09:54 +00003400
bellard9307c4c2004-04-04 12:57:25 +00003401 c = *typestr++;
3402 if (c == '\0')
3403 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003404 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003405 p++;
3406 has_option = 0;
3407 if (*p == '-') {
3408 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003409 if(c != *p) {
3410 if(!is_valid_option(p, typestr)) {
3411
3412 monitor_printf(mon, "%s: unsupported option -%c\n",
3413 cmdname, *p);
3414 goto fail;
3415 } else {
3416 skip_key = 1;
3417 }
bellard9307c4c2004-04-04 12:57:25 +00003418 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003419 if(skip_key) {
3420 p = tmp;
3421 } else {
3422 p++;
3423 has_option = 1;
3424 }
bellard9307c4c2004-04-04 12:57:25 +00003425 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003426 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003427 }
3428 break;
3429 default:
3430 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003431 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003432 goto fail;
3433 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003434 qemu_free(key);
3435 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003436 }
3437 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003438 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003439 p++;
3440 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003441 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3442 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003443 goto fail;
3444 }
3445
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003446 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003447
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003448fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003449 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003450 return NULL;
3451}
3452
Luiz Capitulino8204a912009-11-18 23:05:31 -02003453static void monitor_print_error(Monitor *mon)
3454{
3455 qerror_print(mon->error);
3456 QDECREF(mon->error);
3457 mon->error = NULL;
3458}
3459
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003460static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3461 const QDict *params)
3462{
3463 QObject *data = NULL;
3464
3465 cmd->mhandler.cmd_new(mon, params, &data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003466
3467 if (monitor_ctrl_mode(mon)) {
3468 /* Monitor Protocol */
3469 monitor_protocol_emitter(mon, data);
3470 } else {
3471 /* User Protocol */
3472 if (data)
3473 cmd->user_print(mon, data);
3474 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003475
3476 qobject_decref(data);
3477}
3478
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003479static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003480{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003481 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003482 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003483
3484 qdict = qdict_new();
3485
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003486 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003487 if (!cmd)
3488 goto out;
3489
3490 qemu_errors_to_mon(mon);
3491
3492 if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003493 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003494 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003495 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003496 }
3497
Luiz Capitulino8204a912009-11-18 23:05:31 -02003498 if (monitor_has_error(mon))
3499 monitor_print_error(mon);
3500
3501 qemu_errors_to_previous();
Luiz Capitulino13917be2009-10-07 13:41:54 -03003502
3503out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003504 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003505}
3506
bellard81d09122004-07-14 17:21:37 +00003507static void cmd_completion(const char *name, const char *list)
3508{
3509 const char *p, *pstart;
3510 char cmd[128];
3511 int len;
3512
3513 p = list;
3514 for(;;) {
3515 pstart = p;
3516 p = strchr(p, '|');
3517 if (!p)
3518 p = pstart + strlen(pstart);
3519 len = p - pstart;
3520 if (len > sizeof(cmd) - 2)
3521 len = sizeof(cmd) - 2;
3522 memcpy(cmd, pstart, len);
3523 cmd[len] = '\0';
3524 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003525 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003526 }
3527 if (*p == '\0')
3528 break;
3529 p++;
3530 }
3531}
3532
3533static void file_completion(const char *input)
3534{
3535 DIR *ffs;
3536 struct dirent *d;
3537 char path[1024];
3538 char file[1024], file_prefix[1024];
3539 int input_path_len;
3540 const char *p;
3541
ths5fafdf22007-09-16 21:08:06 +00003542 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003543 if (!p) {
3544 input_path_len = 0;
3545 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003546 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003547 } else {
3548 input_path_len = p - input + 1;
3549 memcpy(path, input, input_path_len);
3550 if (input_path_len > sizeof(path) - 1)
3551 input_path_len = sizeof(path) - 1;
3552 path[input_path_len] = '\0';
3553 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3554 }
3555#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003556 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3557 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003558#endif
3559 ffs = opendir(path);
3560 if (!ffs)
3561 return;
3562 for(;;) {
3563 struct stat sb;
3564 d = readdir(ffs);
3565 if (!d)
3566 break;
3567 if (strstart(d->d_name, file_prefix, NULL)) {
3568 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003569 if (input_path_len < sizeof(file))
3570 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3571 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003572 /* stat the file to find out if it's a directory.
3573 * In that case add a slash to speed up typing long paths
3574 */
3575 stat(file, &sb);
3576 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003577 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003578 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003579 }
3580 }
3581 closedir(ffs);
3582}
3583
aliguori51de9762009-03-05 23:00:43 +00003584static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003585{
aliguori51de9762009-03-05 23:00:43 +00003586 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003587 const char *input = opaque;
3588
3589 if (input[0] == '\0' ||
3590 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003591 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003592 }
3593}
3594
3595/* NOTE: this parser is an approximate form of the real command parser */
3596static void parse_cmdline(const char *cmdline,
3597 int *pnb_args, char **args)
3598{
3599 const char *p;
3600 int nb_args, ret;
3601 char buf[1024];
3602
3603 p = cmdline;
3604 nb_args = 0;
3605 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003606 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003607 p++;
3608 if (*p == '\0')
3609 break;
3610 if (nb_args >= MAX_ARGS)
3611 break;
3612 ret = get_str(buf, sizeof(buf), &p);
3613 args[nb_args] = qemu_strdup(buf);
3614 nb_args++;
3615 if (ret < 0)
3616 break;
3617 }
3618 *pnb_args = nb_args;
3619}
3620
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003621static const char *next_arg_type(const char *typestr)
3622{
3623 const char *p = strchr(typestr, ':');
3624 return (p != NULL ? ++p : typestr);
3625}
3626
aliguori4c36ba32009-03-05 23:01:37 +00003627static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003628{
3629 const char *cmdname;
3630 char *args[MAX_ARGS];
3631 int nb_args, i, len;
3632 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003633 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003634 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003635
3636 parse_cmdline(cmdline, &nb_args, args);
3637#ifdef DEBUG_COMPLETION
3638 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003639 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003640 }
3641#endif
3642
3643 /* if the line ends with a space, it means we want to complete the
3644 next arg */
3645 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003646 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003647 if (nb_args >= MAX_ARGS)
3648 return;
3649 args[nb_args++] = qemu_strdup("");
3650 }
3651 if (nb_args <= 1) {
3652 /* command completion */
3653 if (nb_args == 0)
3654 cmdname = "";
3655 else
3656 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003657 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003658 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003659 cmd_completion(cmdname, cmd->name);
3660 }
3661 } else {
3662 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003663 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003664 if (compare_cmd(args[0], cmd->name))
3665 goto found;
3666 }
3667 return;
3668 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003669 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003670 for(i = 0; i < nb_args - 2; i++) {
3671 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003672 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003673 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003674 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003675 }
3676 }
3677 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003678 if (*ptype == '-' && ptype[1] != '\0') {
3679 ptype += 2;
3680 }
bellard81d09122004-07-14 17:21:37 +00003681 switch(*ptype) {
3682 case 'F':
3683 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003684 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003685 file_completion(str);
3686 break;
3687 case 'B':
3688 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003689 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003690 bdrv_iterate(block_completion_it, (void *)str);
3691 break;
bellard7fe48482004-10-09 18:08:01 +00003692 case 's':
3693 /* XXX: more generic ? */
3694 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003695 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003696 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3697 cmd_completion(str, cmd->name);
3698 }
bellard64866c32006-05-07 18:03:31 +00003699 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003700 char *sep = strrchr(str, '-');
3701 if (sep)
3702 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003703 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003704 for(key = key_defs; key->name != NULL; key++) {
3705 cmd_completion(str, key->name);
3706 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003707 } else if (!strcmp(cmd->name, "help|?")) {
3708 readline_set_completion_index(cur_mon->rs, strlen(str));
3709 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3710 cmd_completion(str, cmd->name);
3711 }
bellard7fe48482004-10-09 18:08:01 +00003712 }
3713 break;
bellard81d09122004-07-14 17:21:37 +00003714 default:
3715 break;
3716 }
3717 }
3718 for(i = 0; i < nb_args; i++)
3719 qemu_free(args[i]);
3720}
3721
aliguori731b0362009-03-05 23:01:42 +00003722static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003723{
aliguori731b0362009-03-05 23:01:42 +00003724 Monitor *mon = opaque;
3725
3726 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003727}
3728
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003729typedef struct CmdArgs {
3730 QString *name;
3731 int type;
3732 int flag;
3733 int optional;
3734} CmdArgs;
3735
3736static int check_opt(const CmdArgs *cmd_args, const char *name, QDict *args)
3737{
3738 if (!cmd_args->optional) {
3739 qemu_error_new(QERR_MISSING_PARAMETER, name);
3740 return -1;
3741 }
3742
3743 if (cmd_args->type == '-') {
3744 /* handlers expect a value, they need to be changed */
3745 qdict_put(args, name, qint_from_int(0));
3746 }
3747
3748 return 0;
3749}
3750
3751static int check_arg(const CmdArgs *cmd_args, QDict *args)
3752{
3753 QObject *value;
3754 const char *name;
3755
3756 name = qstring_get_str(cmd_args->name);
3757
3758 if (!args) {
3759 return check_opt(cmd_args, name, args);
3760 }
3761
3762 value = qdict_get(args, name);
3763 if (!value) {
3764 return check_opt(cmd_args, name, args);
3765 }
3766
3767 switch (cmd_args->type) {
3768 case 'F':
3769 case 'B':
3770 case 's':
3771 if (qobject_type(value) != QTYPE_QSTRING) {
3772 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "string");
3773 return -1;
3774 }
3775 break;
3776 case '/': {
3777 int i;
3778 const char *keys[] = { "count", "format", "size", NULL };
3779
3780 for (i = 0; keys[i]; i++) {
3781 QObject *obj = qdict_get(args, keys[i]);
3782 if (!obj) {
3783 qemu_error_new(QERR_MISSING_PARAMETER, name);
3784 return -1;
3785 }
3786 if (qobject_type(obj) != QTYPE_QINT) {
3787 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
3788 return -1;
3789 }
3790 }
3791 break;
3792 }
3793 case 'i':
3794 case 'l':
3795 if (qobject_type(value) != QTYPE_QINT) {
3796 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "int");
3797 return -1;
3798 }
3799 break;
3800 case '-':
3801 if (qobject_type(value) != QTYPE_QINT &&
3802 qobject_type(value) != QTYPE_QBOOL) {
3803 qemu_error_new(QERR_INVALID_PARAMETER_TYPE, name, "bool");
3804 return -1;
3805 }
3806 if (qobject_type(value) == QTYPE_QBOOL) {
3807 /* handlers expect a QInt, they need to be changed */
3808 qdict_put(args, name,
3809 qint_from_int(qbool_get_int(qobject_to_qbool(value))));
3810 }
3811 break;
3812 default:
3813 /* impossible */
3814 abort();
3815 }
3816
3817 return 0;
3818}
3819
3820static void cmd_args_init(CmdArgs *cmd_args)
3821{
3822 cmd_args->name = qstring_new();
3823 cmd_args->type = cmd_args->flag = cmd_args->optional = 0;
3824}
3825
3826/*
3827 * This is not trivial, we have to parse Monitor command's argument
3828 * type syntax to be able to check the arguments provided by clients.
3829 *
3830 * In the near future we will be using an array for that and will be
3831 * able to drop all this parsing...
3832 */
3833static int monitor_check_qmp_args(const mon_cmd_t *cmd, QDict *args)
3834{
3835 int err;
3836 const char *p;
3837 CmdArgs cmd_args;
3838
Blue Swirldd5121b2009-12-04 20:52:02 +00003839 if (cmd->args_type == NULL) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003840 return (qdict_size(args) == 0 ? 0 : -1);
3841 }
3842
3843 err = 0;
3844 cmd_args_init(&cmd_args);
3845
3846 for (p = cmd->args_type;; p++) {
3847 if (*p == ':') {
3848 cmd_args.type = *++p;
3849 p++;
3850 if (cmd_args.type == '-') {
3851 cmd_args.flag = *p++;
3852 cmd_args.optional = 1;
3853 } else if (*p == '?') {
3854 cmd_args.optional = 1;
3855 p++;
3856 }
3857
3858 assert(*p == ',' || *p == '\0');
3859 err = check_arg(&cmd_args, args);
3860
3861 QDECREF(cmd_args.name);
3862 cmd_args_init(&cmd_args);
3863
3864 if (err < 0) {
3865 break;
3866 }
3867 } else {
3868 qstring_append_chr(cmd_args.name, *p);
3869 }
3870
3871 if (*p == '\0') {
3872 break;
3873 }
3874 }
3875
3876 QDECREF(cmd_args.name);
3877 return err;
3878}
3879
3880static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3881{
3882 int err;
3883 QObject *obj;
3884 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003885 const mon_cmd_t *cmd;
3886 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02003887 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003888
3889 args = NULL;
3890 qemu_errors_to_mon(mon);
3891
3892 obj = json_parser_parse(tokens, NULL);
3893 if (!obj) {
3894 // FIXME: should be triggered in json_parser_parse()
3895 qemu_error_new(QERR_JSON_PARSING);
3896 goto err_out;
3897 } else if (qobject_type(obj) != QTYPE_QDICT) {
3898 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "object");
3899 qobject_decref(obj);
3900 goto err_out;
3901 }
3902
3903 input = qobject_to_qdict(obj);
3904
3905 mon->mc->id = qdict_get(input, "id");
3906 qobject_incref(mon->mc->id);
3907
3908 obj = qdict_get(input, "execute");
3909 if (!obj) {
3910 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "execute");
3911 goto err_input;
3912 } else if (qobject_type(obj) != QTYPE_QSTRING) {
3913 qemu_error_new(QERR_QMP_BAD_INPUT_OBJECT, "string");
3914 goto err_input;
3915 }
3916
3917 cmd_name = qstring_get_str(qobject_to_qstring(obj));
Luiz Capitulino5e23f482009-11-26 22:59:02 -02003918
3919 /*
3920 * XXX: We need this special case until we get info handlers
3921 * converted into 'query-' commands
3922 */
3923 if (compare_cmd(cmd_name, "info")) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003924 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
3925 goto err_input;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02003926 } else if (strstart(cmd_name, "query-", &info_item)) {
3927 cmd = monitor_find_command("info");
3928 qdict_put_obj(input, "arguments",
3929 qobject_from_jsonf("{ 'item': %s }", info_item));
3930 } else {
3931 cmd = monitor_find_command(cmd_name);
3932 if (!cmd) {
3933 qemu_error_new(QERR_COMMAND_NOT_FOUND, cmd_name);
3934 goto err_input;
3935 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003936 }
3937
3938 obj = qdict_get(input, "arguments");
3939 if (!obj) {
3940 args = qdict_new();
3941 } else {
3942 args = qobject_to_qdict(obj);
3943 QINCREF(args);
3944 }
3945
3946 QDECREF(input);
3947
3948 err = monitor_check_qmp_args(cmd, args);
3949 if (err < 0) {
3950 goto err_out;
3951 }
3952
3953 monitor_call_handler(mon, cmd, args);
3954 goto out;
3955
3956err_input:
3957 QDECREF(input);
3958err_out:
3959 monitor_protocol_emitter(mon, NULL);
3960out:
3961 QDECREF(args);
3962 qemu_errors_to_previous();
3963}
3964
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003965/**
3966 * monitor_control_read(): Read and handle QMP input
3967 */
3968static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
3969{
3970 Monitor *old_mon = cur_mon;
3971
3972 cur_mon = opaque;
3973
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02003974 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02003975
3976 cur_mon = old_mon;
3977}
3978
aliguori731b0362009-03-05 23:01:42 +00003979static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003980{
aliguori731b0362009-03-05 23:01:42 +00003981 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003982 int i;
aliguori376253e2009-03-05 23:01:23 +00003983
aliguori731b0362009-03-05 23:01:42 +00003984 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003985
aliguoricde76ee2009-03-05 23:01:51 +00003986 if (cur_mon->rs) {
3987 for (i = 0; i < size; i++)
3988 readline_handle_byte(cur_mon->rs, buf[i]);
3989 } else {
3990 if (size == 0 || buf[size - 1] != 0)
3991 monitor_printf(cur_mon, "corrupted command\n");
3992 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003993 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00003994 }
aliguori731b0362009-03-05 23:01:42 +00003995
3996 cur_mon = old_mon;
3997}
aliguorid8f44602008-10-06 13:52:44 +00003998
aliguori376253e2009-03-05 23:01:23 +00003999static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004000{
aliguori731b0362009-03-05 23:01:42 +00004001 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004002 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004003 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004004}
4005
aliguoricde76ee2009-03-05 23:01:51 +00004006int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004007{
aliguoricde76ee2009-03-05 23:01:51 +00004008 if (!mon->rs)
4009 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004010 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004011 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004012}
4013
aliguori376253e2009-03-05 23:01:23 +00004014void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004015{
aliguoricde76ee2009-03-05 23:01:51 +00004016 if (!mon->rs)
4017 return;
aliguori731b0362009-03-05 23:01:42 +00004018 if (--mon->suspend_cnt == 0)
4019 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004020}
4021
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004022/**
4023 * monitor_control_event(): Print QMP gretting
4024 */
4025static void monitor_control_event(void *opaque, int event)
4026{
4027 if (event == CHR_EVENT_OPENED) {
4028 QObject *data;
4029 Monitor *mon = opaque;
4030
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004031 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4032
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004033 data = qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
4034 assert(data != NULL);
4035
4036 monitor_json_emitter(mon, data);
4037 qobject_decref(data);
4038 }
4039}
4040
aliguori731b0362009-03-05 23:01:42 +00004041static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004042{
aliguori376253e2009-03-05 23:01:23 +00004043 Monitor *mon = opaque;
4044
aliguori2724b182009-03-05 23:01:47 +00004045 switch (event) {
4046 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004047 mon->mux_out = 0;
4048 if (mon->reset_seen) {
4049 readline_restart(mon->rs);
4050 monitor_resume(mon);
4051 monitor_flush(mon);
4052 } else {
4053 mon->suspend_cnt = 0;
4054 }
aliguori2724b182009-03-05 23:01:47 +00004055 break;
ths86e94de2007-01-05 22:01:59 +00004056
aliguori2724b182009-03-05 23:01:47 +00004057 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004058 if (mon->reset_seen) {
4059 if (mon->suspend_cnt == 0) {
4060 monitor_printf(mon, "\n");
4061 }
4062 monitor_flush(mon);
4063 monitor_suspend(mon);
4064 } else {
4065 mon->suspend_cnt++;
4066 }
4067 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004068 break;
4069
Amit Shahb6b8df52009-10-07 18:31:16 +05304070 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004071 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4072 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004073 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004074 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004075 }
4076 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004077 break;
4078 }
ths86e94de2007-01-05 22:01:59 +00004079}
4080
aliguori76655d62009-03-06 20:27:37 +00004081
4082/*
4083 * Local variables:
4084 * c-indent-level: 4
4085 * c-basic-offset: 4
4086 * tab-width: 8
4087 * End:
4088 */
4089
aliguori731b0362009-03-05 23:01:42 +00004090void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004091{
aliguori731b0362009-03-05 23:01:42 +00004092 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004093 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004094
4095 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004096 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004097 is_first_init = 0;
4098 }
aliguori87127162009-03-05 23:01:29 +00004099
4100 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004101
aliguori87127162009-03-05 23:01:29 +00004102 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004103 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004104 if (flags & MONITOR_USE_READLINE) {
4105 mon->rs = readline_init(mon, monitor_find_completion);
4106 monitor_read_command(mon, 0);
4107 }
aliguori87127162009-03-05 23:01:29 +00004108
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004109 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004110 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004111 /* Control mode requires special handlers */
4112 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4113 monitor_control_event, mon);
4114 } else {
4115 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4116 monitor_event, mon);
4117 }
aliguori87127162009-03-05 23:01:29 +00004118
Blue Swirl72cf2d42009-09-12 07:36:22 +00004119 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00004120 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00004121 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004122}
4123
aliguori376253e2009-03-05 23:01:23 +00004124static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004125{
aliguoribb5fc202009-03-05 23:01:15 +00004126 BlockDriverState *bs = opaque;
4127 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004128
aliguoribb5fc202009-03-05 23:01:15 +00004129 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004130 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004131 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004132 }
aliguori731b0362009-03-05 23:01:42 +00004133 if (mon->password_completion_cb)
4134 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004135
aliguori731b0362009-03-05 23:01:42 +00004136 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004137}
aliguoric0f4ce72009-03-05 23:01:01 +00004138
aliguori376253e2009-03-05 23:01:23 +00004139void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00004140 BlockDriverCompletionFunc *completion_cb,
4141 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004142{
aliguoricde76ee2009-03-05 23:01:51 +00004143 int err;
4144
aliguoribb5fc202009-03-05 23:01:15 +00004145 if (!bdrv_key_required(bs)) {
4146 if (completion_cb)
4147 completion_cb(opaque, 0);
4148 return;
4149 }
aliguoric0f4ce72009-03-05 23:01:01 +00004150
Luiz Capitulino94171e12009-12-07 21:37:00 +01004151 if (monitor_ctrl_mode(mon)) {
4152 qemu_error_new(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4153 return;
4154 }
4155
aliguori376253e2009-03-05 23:01:23 +00004156 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4157 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004158
aliguori731b0362009-03-05 23:01:42 +00004159 mon->password_completion_cb = completion_cb;
4160 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004161
aliguoricde76ee2009-03-05 23:01:51 +00004162 err = monitor_read_password(mon, bdrv_password_cb, bs);
4163
4164 if (err && completion_cb)
4165 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00004166}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004167
4168typedef struct QemuErrorSink QemuErrorSink;
4169struct QemuErrorSink {
4170 enum {
4171 ERR_SINK_FILE,
4172 ERR_SINK_MONITOR,
4173 } dest;
4174 union {
4175 FILE *fp;
4176 Monitor *mon;
4177 };
4178 QemuErrorSink *previous;
4179};
4180
Blue Swirl528e93a2009-08-31 15:14:40 +00004181static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004182
4183void qemu_errors_to_file(FILE *fp)
4184{
4185 QemuErrorSink *sink;
4186
4187 sink = qemu_mallocz(sizeof(*sink));
4188 sink->dest = ERR_SINK_FILE;
4189 sink->fp = fp;
4190 sink->previous = qemu_error_sink;
4191 qemu_error_sink = sink;
4192}
4193
4194void qemu_errors_to_mon(Monitor *mon)
4195{
4196 QemuErrorSink *sink;
4197
4198 sink = qemu_mallocz(sizeof(*sink));
4199 sink->dest = ERR_SINK_MONITOR;
4200 sink->mon = mon;
4201 sink->previous = qemu_error_sink;
4202 qemu_error_sink = sink;
4203}
4204
4205void qemu_errors_to_previous(void)
4206{
4207 QemuErrorSink *sink;
4208
4209 assert(qemu_error_sink != NULL);
4210 sink = qemu_error_sink;
4211 qemu_error_sink = sink->previous;
4212 qemu_free(sink);
4213}
4214
4215void qemu_error(const char *fmt, ...)
4216{
4217 va_list args;
4218
4219 assert(qemu_error_sink != NULL);
4220 switch (qemu_error_sink->dest) {
4221 case ERR_SINK_FILE:
4222 va_start(args, fmt);
4223 vfprintf(qemu_error_sink->fp, fmt, args);
4224 va_end(args);
4225 break;
4226 case ERR_SINK_MONITOR:
4227 va_start(args, fmt);
4228 monitor_vprintf(qemu_error_sink->mon, fmt, args);
4229 va_end(args);
4230 break;
4231 }
4232}
Luiz Capitulino8204a912009-11-18 23:05:31 -02004233
4234void qemu_error_internal(const char *file, int linenr, const char *func,
4235 const char *fmt, ...)
4236{
4237 va_list va;
4238 QError *qerror;
4239
4240 assert(qemu_error_sink != NULL);
4241
4242 va_start(va, fmt);
4243 qerror = qerror_from_info(file, linenr, func, fmt, &va);
4244 va_end(va);
4245
4246 switch (qemu_error_sink->dest) {
4247 case ERR_SINK_FILE:
4248 qerror_print(qerror);
4249 QDECREF(qerror);
4250 break;
4251 case ERR_SINK_MONITOR:
4252 assert(qemu_error_sink->mon->error == NULL);
4253 qemu_error_sink->mon->error = qerror;
4254 break;
4255 }
4256}