blob: 8c9cc9b190e2131d8401b7018a78c1327e728bca [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"
35#include "qemu-char.h"
36#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000037#include "monitor.h"
38#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000039#include "console.h"
40#include "block.h"
41#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000042#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000043#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000044#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000045#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000046#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000047#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030048#include "qint.h"
49#include "qdict.h"
50#include "qstring.h"
ths6a5bd302007-12-03 17:05:38 +000051
bellard9dc39cb2004-03-14 21:38:27 +000052//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000053//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000054
bellard9307c4c2004-04-04 12:57:25 +000055/*
56 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000057 *
bellard9307c4c2004-04-04 12:57:25 +000058 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000059 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000060 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000061 * 'i' 32 bit integer
62 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000063 * '/' optional gdb-like print format (like "/10x")
64 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030065 * '?' optional type (for all types, except '/')
66 * '.' other form of optional type (for 'i' and 'l')
67 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000068 *
69 */
70
Anthony Liguoric227f092009-10-01 16:12:16 -050071typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +000072 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000073 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +000074 const char *params;
75 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -030076 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -030077 union {
78 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -030079 void (*info_new)(Monitor *mon, QObject **ret_data);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -030080 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -030081 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Luiz Capitulino910df892009-10-07 13:41:51 -030082 } mhandler;
Anthony Liguoric227f092009-10-01 16:12:16 -050083} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +000084
Mark McLoughlinf07918f2009-07-22 09:11:40 +010085/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -050086typedef struct mon_fd_t mon_fd_t;
87struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +010088 char *name;
89 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -050090 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +010091};
92
aliguori87127162009-03-05 23:01:29 +000093struct Monitor {
94 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +020095 int mux_out;
96 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +000097 int flags;
98 int suspend_cnt;
99 uint8_t outbuf[1024];
100 int outbuf_index;
101 ReadLineState *rs;
102 CPUState *mon_cpu;
103 BlockDriverCompletionFunc *password_completion_cb;
104 void *password_opaque;
Anthony Liguoric227f092009-10-01 16:12:16 -0500105 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000106 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000107};
108
Blue Swirl72cf2d42009-09-12 07:36:22 +0000109static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000110
Anthony Liguoric227f092009-10-01 16:12:16 -0500111static const mon_cmd_t mon_cmds[];
112static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000113
aliguori87127162009-03-05 23:01:29 +0000114Monitor *cur_mon = NULL;
aliguori376253e2009-03-05 23:01:23 +0000115
aliguori731b0362009-03-05 23:01:42 +0000116static void monitor_command_cb(Monitor *mon, const char *cmdline,
117 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000118
aliguori731b0362009-03-05 23:01:42 +0000119static void monitor_read_command(Monitor *mon, int show_prompt)
120{
121 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
122 if (show_prompt)
123 readline_show_prompt(mon->rs);
124}
bellard6a00d602005-11-21 23:25:50 +0000125
aliguoricde76ee2009-03-05 23:01:51 +0000126static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
127 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000128{
aliguoricde76ee2009-03-05 23:01:51 +0000129 if (mon->rs) {
130 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
131 /* prompt is printed on return from the command handler */
132 return 0;
133 } else {
134 monitor_printf(mon, "terminal does not support password prompting\n");
135 return -ENOTTY;
136 }
aliguoribb5fc202009-03-05 23:01:15 +0000137}
138
aliguori376253e2009-03-05 23:01:23 +0000139void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000140{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200141 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000142 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
143 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000144 }
145}
146
147/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000148static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000149{
ths60fe76f2007-12-16 03:02:09 +0000150 char c;
aliguori731b0362009-03-05 23:01:42 +0000151
152 if (!mon)
153 return;
154
bellard7e2515e2004-08-01 21:52:19 +0000155 for(;;) {
156 c = *str++;
157 if (c == '\0')
158 break;
bellard7ba12602006-07-14 20:26:42 +0000159 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000160 mon->outbuf[mon->outbuf_index++] = '\r';
161 mon->outbuf[mon->outbuf_index++] = c;
162 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
163 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000164 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000165 }
166}
167
aliguori376253e2009-03-05 23:01:23 +0000168void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000169{
170 char buf[4096];
171 vsnprintf(buf, sizeof(buf), fmt, ap);
aliguori376253e2009-03-05 23:01:23 +0000172 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000173}
174
aliguori376253e2009-03-05 23:01:23 +0000175void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000176{
177 va_list ap;
178 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000179 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000180 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000181}
182
aliguori376253e2009-03-05 23:01:23 +0000183void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000184{
185 int i;
186
187 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000188 switch (filename[i]) {
189 case ' ':
190 case '"':
191 case '\\':
192 monitor_printf(mon, "\\%c", filename[i]);
193 break;
194 case '\t':
195 monitor_printf(mon, "\\t");
196 break;
197 case '\r':
198 monitor_printf(mon, "\\r");
199 break;
200 case '\n':
201 monitor_printf(mon, "\\n");
202 break;
203 default:
204 monitor_printf(mon, "%c", filename[i]);
205 break;
206 }
thsfef30742006-12-22 14:11:32 +0000207 }
208}
209
bellard7fe48482004-10-09 18:08:01 +0000210static int monitor_fprintf(FILE *stream, const char *fmt, ...)
211{
212 va_list ap;
213 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000214 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000215 va_end(ap);
216 return 0;
217}
218
Luiz Capitulino13c74252009-10-07 13:41:55 -0300219static void monitor_user_noop(Monitor *mon, const QObject *data) { }
220
Luiz Capitulino13917be2009-10-07 13:41:54 -0300221static inline int monitor_handler_ported(const mon_cmd_t *cmd)
222{
223 return cmd->user_print != NULL;
224}
225
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300226static void monitor_print_qobject(Monitor *mon, const QObject *data)
227{
228 switch (qobject_type(data)) {
229 case QTYPE_QSTRING:
230 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
231 break;
232 case QTYPE_QINT:
233 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
234 break;
235 default:
236 monitor_printf(mon, "ERROR: unsupported type: %d",
237 qobject_type(data));
238 break;
239 }
240
241 monitor_puts(mon, "\n");
242}
243
bellard9dc39cb2004-03-14 21:38:27 +0000244static int compare_cmd(const char *name, const char *list)
245{
246 const char *p, *pstart;
247 int len;
248 len = strlen(name);
249 p = list;
250 for(;;) {
251 pstart = p;
252 p = strchr(p, '|');
253 if (!p)
254 p = pstart + strlen(pstart);
255 if ((p - pstart) == len && !memcmp(pstart, name, len))
256 return 1;
257 if (*p == '\0')
258 break;
259 p++;
260 }
261 return 0;
262}
263
Anthony Liguoric227f092009-10-01 16:12:16 -0500264static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000265 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000266{
Anthony Liguoric227f092009-10-01 16:12:16 -0500267 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000268
269 for(cmd = cmds; cmd->name != NULL; cmd++) {
270 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000271 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
272 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000273 }
274}
275
aliguori376253e2009-03-05 23:01:23 +0000276static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000277{
278 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000279 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000280 } else {
aliguori376253e2009-03-05 23:01:23 +0000281 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000282 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000283 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000284 monitor_printf(mon, "Log items (comma separated):\n");
285 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000286 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000287 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000288 }
289 }
bellard9dc39cb2004-03-14 21:38:27 +0000290 }
291}
292
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300293static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300294{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300295 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300296}
297
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300298static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000299{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200300 int all_devices;
301 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300302 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000303
bellard7954c732006-08-01 15:52:40 +0000304 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000305 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200306 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300307 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200308 continue;
309 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000310 }
311}
312
Luiz Capitulino13c74252009-10-07 13:41:55 -0300313static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000314{
Anthony Liguoric227f092009-10-01 16:12:16 -0500315 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300316 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000317
bellard9307c4c2004-04-04 12:57:25 +0000318 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000319 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300320
321 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000322 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300323 break;
bellard9dc39cb2004-03-14 21:38:27 +0000324 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300325
326 if (cmd->name == NULL)
327 goto help;
328
329 if (monitor_handler_ported(cmd)) {
330 cmd->mhandler.info_new(mon, ret_data);
331 if (*ret_data)
332 cmd->user_print(mon, *ret_data);
333 } else {
334 cmd->mhandler.info(mon);
335 }
336
bellard9dc39cb2004-03-14 21:38:27 +0000337 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300338
339help:
340 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000341}
342
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300343/**
344 * do_info_version(): Show QEMU version
345 */
346static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000347{
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300348 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
bellard9bc9d1c2004-10-10 15:15:51 +0000349}
350
aliguori376253e2009-03-05 23:01:23 +0000351static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000352{
353 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000354 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000355}
356
aurel32bf4f74c2008-12-18 22:42:34 +0000357#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000358static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000359{
aliguori376253e2009-03-05 23:01:23 +0000360 monitor_printf(mon, "HPET is %s by QEMU\n",
361 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000362}
aurel32bf4f74c2008-12-18 22:42:34 +0000363#endif
aliguori16b29ae2008-12-17 23:28:44 +0000364
aliguori376253e2009-03-05 23:01:23 +0000365static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000366{
aliguori376253e2009-03-05 23:01:23 +0000367 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
368 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
369 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
370 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
371 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000372}
373
bellard6a00d602005-11-21 23:25:50 +0000374/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000375static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000376{
377 CPUState *env;
378
379 for(env = first_cpu; env != NULL; env = env->next_cpu) {
380 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000381 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000382 return 0;
383 }
384 }
385 return -1;
386}
387
pbrook9596ebb2007-11-18 01:44:38 +0000388static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000389{
aliguori731b0362009-03-05 23:01:42 +0000390 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000391 mon_set_cpu(0);
392 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300393 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000394 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000395}
396
aliguori376253e2009-03-05 23:01:23 +0000397static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000398{
bellard6a00d602005-11-21 23:25:50 +0000399 CPUState *env;
400 env = mon_get_cpu();
401 if (!env)
402 return;
bellard9307c4c2004-04-04 12:57:25 +0000403#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000404 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000405 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000406#else
aliguori376253e2009-03-05 23:01:23 +0000407 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000408 0);
bellard9307c4c2004-04-04 12:57:25 +0000409#endif
410}
411
aliguori376253e2009-03-05 23:01:23 +0000412static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000413{
414 CPUState *env;
415
416 /* just to set the default cpu if not already done */
417 mon_get_cpu();
418
419 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300420 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000421 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000422 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000423 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000424#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000425 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
426 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000427#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000428 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000429#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000430 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
431 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000432#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000433 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000434#endif
thsead93602007-09-06 00:18:15 +0000435 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000436 monitor_printf(mon, " (halted)");
437 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000438 }
439}
440
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300441static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000442{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300443 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000444 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000445 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000446}
447
aliguori376253e2009-03-05 23:01:23 +0000448static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000449{
aliguori376253e2009-03-05 23:01:23 +0000450 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000451}
452
aliguori376253e2009-03-05 23:01:23 +0000453static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000454{
455 int i;
bellard7e2515e2004-08-01 21:52:19 +0000456 const char *str;
ths3b46e622007-09-17 08:09:54 +0000457
aliguoricde76ee2009-03-05 23:01:51 +0000458 if (!mon->rs)
459 return;
bellard7e2515e2004-08-01 21:52:19 +0000460 i = 0;
461 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000462 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000463 if (!str)
464 break;
aliguori376253e2009-03-05 23:01:23 +0000465 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000466 i++;
bellardaa455482004-04-04 13:07:25 +0000467 }
468}
469
j_mayer76a66252007-03-07 08:32:30 +0000470#if defined(TARGET_PPC)
471/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000472static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000473{
474 CPUState *env;
475
476 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000477 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000478}
479#endif
480
Luiz Capitulinob223f352009-10-07 13:41:56 -0300481/**
482 * do_quit(): Quit QEMU execution
483 */
484static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000485{
486 exit(0);
487}
488
aliguori376253e2009-03-05 23:01:23 +0000489static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000490{
491 if (bdrv_is_inserted(bs)) {
492 if (!force) {
493 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000494 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000495 return -1;
496 }
497 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000498 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000499 return -1;
500 }
501 }
502 bdrv_close(bs);
503 }
504 return 0;
505}
506
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300507static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000508{
509 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300510 int force = qdict_get_int(qdict, "force");
511 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000512
bellard9307c4c2004-04-04 12:57:25 +0000513 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000514 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000515 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000516 return;
517 }
aliguori376253e2009-03-05 23:01:23 +0000518 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000519}
520
aliguori376253e2009-03-05 23:01:23 +0000521static void do_change_block(Monitor *mon, const char *device,
522 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000523{
524 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000525 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000526
bellard9307c4c2004-04-04 12:57:25 +0000527 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000528 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000529 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000530 return;
531 }
aurel322ecea9b2008-06-18 22:10:01 +0000532 if (fmt) {
533 drv = bdrv_find_format(fmt);
534 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000535 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000536 return;
537 }
538 }
aliguori376253e2009-03-05 23:01:23 +0000539 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000540 return;
aurel322ecea9b2008-06-18 22:10:01 +0000541 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000542 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000543}
544
aliguori376253e2009-03-05 23:01:23 +0000545static void change_vnc_password_cb(Monitor *mon, const char *password,
546 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000547{
548 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000549 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000550
aliguori731b0362009-03-05 23:01:42 +0000551 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000552}
553
aliguori376253e2009-03-05 23:01:23 +0000554static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000555{
ths70848512007-08-25 01:37:05 +0000556 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000557 strcmp(target, "password") == 0) {
558 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000559 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000560 strncpy(password, arg, sizeof(password));
561 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000562 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000563 } else {
aliguori376253e2009-03-05 23:01:23 +0000564 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000565 }
ths70848512007-08-25 01:37:05 +0000566 } else {
aliguori28a76be2009-03-06 20:27:40 +0000567 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000568 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000569 }
thse25a5822007-08-25 01:36:20 +0000570}
571
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300572static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000573{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300574 const char *device = qdict_get_str(qdict, "device");
575 const char *target = qdict_get_str(qdict, "target");
576 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000577 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000578 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000579 } else {
aliguori28a76be2009-03-06 20:27:40 +0000580 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000581 }
582}
583
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300584static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000585{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300586 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000587}
588
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300589static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000590{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300591 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000592}
593
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300594static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000595{
596 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300597 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000598
bellard9307c4c2004-04-04 12:57:25 +0000599 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000600 mask = 0;
601 } else {
bellard9307c4c2004-04-04 12:57:25 +0000602 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000603 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000604 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000605 return;
606 }
607 }
608 cpu_set_log(mask);
609}
610
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300611static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000612{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300613 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000614 if (!option || !strcmp(option, "on")) {
615 singlestep = 1;
616 } else if (!strcmp(option, "off")) {
617 singlestep = 0;
618 } else {
619 monitor_printf(mon, "unexpected option %s\n", option);
620 }
621}
622
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300623/**
624 * do_stop(): Stop VM execution
625 */
626static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000627{
628 vm_stop(EXCP_INTERRUPT);
629}
630
aliguoribb5fc202009-03-05 23:01:15 +0000631static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000632
aliguori376253e2009-03-05 23:01:23 +0000633struct bdrv_iterate_context {
634 Monitor *mon;
635 int err;
636};
aliguoric0f4ce72009-03-05 23:01:01 +0000637
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300638/**
639 * do_cont(): Resume emulation.
640 */
641static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000642{
643 struct bdrv_iterate_context context = { mon, 0 };
644
645 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000646 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000647 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000648 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000649}
650
aliguoribb5fc202009-03-05 23:01:15 +0000651static void bdrv_key_cb(void *opaque, int err)
652{
aliguori376253e2009-03-05 23:01:23 +0000653 Monitor *mon = opaque;
654
aliguoribb5fc202009-03-05 23:01:15 +0000655 /* another key was set successfully, retry to continue */
656 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300657 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000658}
659
660static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
661{
aliguori376253e2009-03-05 23:01:23 +0000662 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000663
aliguori376253e2009-03-05 23:01:23 +0000664 if (!context->err && bdrv_key_required(bs)) {
665 context->err = -EBUSY;
666 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
667 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000668 }
669}
670
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300671static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000672{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300673 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000674 if (!device)
675 device = "tcp::" DEFAULT_GDBSTUB_PORT;
676 if (gdbserver_start(device) < 0) {
677 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
678 device);
679 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000680 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000681 } else {
aliguori59030a82009-04-05 18:43:41 +0000682 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
683 device);
bellard8a7ddc32004-03-31 19:00:16 +0000684 }
685}
686
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300687static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100688{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300689 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100690 if (select_watchdog_action(action) == -1) {
691 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
692 }
693}
694
aliguori376253e2009-03-05 23:01:23 +0000695static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000696{
aliguori376253e2009-03-05 23:01:23 +0000697 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000698 switch(c) {
699 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000700 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000701 break;
702 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000703 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000704 break;
705 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000706 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000707 break;
708 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000709 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000710 break;
711 default:
712 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000713 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000714 } else {
aliguori376253e2009-03-05 23:01:23 +0000715 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000716 }
717 break;
718 }
aliguori376253e2009-03-05 23:01:23 +0000719 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000720}
721
aliguori376253e2009-03-05 23:01:23 +0000722static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500723 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000724{
bellard6a00d602005-11-21 23:25:50 +0000725 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000726 int nb_per_line, l, line_size, i, max_digits, len;
727 uint8_t buf[16];
728 uint64_t v;
729
730 if (format == 'i') {
731 int flags;
732 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000733 env = mon_get_cpu();
734 if (!env && !is_physical)
735 return;
bellard9307c4c2004-04-04 12:57:25 +0000736#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000737 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000738 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000739 } else if (wsize == 4) {
740 flags = 0;
741 } else {
bellard6a15fd12006-04-12 21:07:07 +0000742 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000743 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000744 if (env) {
745#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000746 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000747 (env->segs[R_CS].flags & DESC_L_MASK))
748 flags = 2;
749 else
750#endif
751 if (!(env->segs[R_CS].flags & DESC_B_MASK))
752 flags = 1;
753 }
bellard4c27ba22004-04-25 18:05:08 +0000754 }
755#endif
aliguori376253e2009-03-05 23:01:23 +0000756 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000757 return;
758 }
759
760 len = wsize * count;
761 if (wsize == 1)
762 line_size = 8;
763 else
764 line_size = 16;
765 nb_per_line = line_size / wsize;
766 max_digits = 0;
767
768 switch(format) {
769 case 'o':
770 max_digits = (wsize * 8 + 2) / 3;
771 break;
772 default:
773 case 'x':
774 max_digits = (wsize * 8) / 4;
775 break;
776 case 'u':
777 case 'd':
778 max_digits = (wsize * 8 * 10 + 32) / 33;
779 break;
780 case 'c':
781 wsize = 1;
782 break;
783 }
784
785 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000786 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000787 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000788 else
aliguori376253e2009-03-05 23:01:23 +0000789 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000790 l = len;
791 if (l > line_size)
792 l = line_size;
793 if (is_physical) {
794 cpu_physical_memory_rw(addr, buf, l, 0);
795 } else {
bellard6a00d602005-11-21 23:25:50 +0000796 env = mon_get_cpu();
797 if (!env)
798 break;
aliguoric8f79b62008-08-18 14:00:20 +0000799 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000800 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000801 break;
802 }
bellard9307c4c2004-04-04 12:57:25 +0000803 }
ths5fafdf22007-09-16 21:08:06 +0000804 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000805 while (i < l) {
806 switch(wsize) {
807 default:
808 case 1:
809 v = ldub_raw(buf + i);
810 break;
811 case 2:
812 v = lduw_raw(buf + i);
813 break;
814 case 4:
bellard92a31b12005-02-10 22:00:52 +0000815 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000816 break;
817 case 8:
818 v = ldq_raw(buf + i);
819 break;
820 }
aliguori376253e2009-03-05 23:01:23 +0000821 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000822 switch(format) {
823 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000824 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000825 break;
826 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000827 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000828 break;
829 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000830 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000831 break;
832 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000833 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000834 break;
835 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000836 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000837 break;
838 }
839 i += wsize;
840 }
aliguori376253e2009-03-05 23:01:23 +0000841 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000842 addr += l;
843 len -= l;
844 }
845}
846
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300847static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000848{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300849 int count = qdict_get_int(qdict, "count");
850 int format = qdict_get_int(qdict, "format");
851 int size = qdict_get_int(qdict, "size");
852 target_long addr = qdict_get_int(qdict, "addr");
853
aliguori376253e2009-03-05 23:01:23 +0000854 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000855}
856
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300857static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000858{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300859 int count = qdict_get_int(qdict, "count");
860 int format = qdict_get_int(qdict, "format");
861 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500862 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300863
aliguori376253e2009-03-05 23:01:23 +0000864 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000865}
866
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300867static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000868{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300869 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500870 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300871
blueswir17743e582007-09-24 18:39:04 +0000872#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000873 switch(format) {
874 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000875 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000876 break;
877 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000878 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000879 break;
880 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000881 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000882 break;
883 default:
884 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000885 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000886 break;
887 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000888 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000889 break;
890 }
bellard92a31b12005-02-10 22:00:52 +0000891#else
892 switch(format) {
893 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000894 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000895 break;
896 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000897 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000898 break;
899 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000900 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000901 break;
902 default:
903 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000904 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000905 break;
906 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000907 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000908 break;
909 }
910#endif
aliguori376253e2009-03-05 23:01:23 +0000911 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000912}
913
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300914static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000915{
916 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300917 uint32_t size = qdict_get_int(qdict, "size");
918 const char *filename = qdict_get_str(qdict, "filename");
919 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000920 uint32_t l;
921 CPUState *env;
922 uint8_t buf[1024];
923
924 env = mon_get_cpu();
925 if (!env)
926 return;
927
928 f = fopen(filename, "wb");
929 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000930 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000931 return;
932 }
933 while (size != 0) {
934 l = sizeof(buf);
935 if (l > size)
936 l = size;
937 cpu_memory_rw_debug(env, addr, buf, l, 0);
938 fwrite(buf, 1, l, f);
939 addr += l;
940 size -= l;
941 }
942 fclose(f);
943}
944
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300945static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000946{
947 FILE *f;
948 uint32_t l;
949 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300950 uint32_t size = qdict_get_int(qdict, "size");
951 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -0500952 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000953
954 f = fopen(filename, "wb");
955 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000956 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000957 return;
958 }
959 while (size != 0) {
960 l = sizeof(buf);
961 if (l > size)
962 l = size;
963 cpu_physical_memory_rw(addr, buf, l, 0);
964 fwrite(buf, 1, l, f);
965 fflush(f);
966 addr += l;
967 size -= l;
968 }
969 fclose(f);
970}
971
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300972static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000973{
974 uint32_t addr;
975 uint8_t buf[1];
976 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300977 uint32_t start = qdict_get_int(qdict, "start");
978 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000979
980 sum = 0;
981 for(addr = start; addr < (start + size); addr++) {
982 cpu_physical_memory_rw(addr, buf, 1, 0);
983 /* BSD sum algorithm ('sum' Unix command) */
984 sum = (sum >> 1) | (sum << 15);
985 sum += buf[0];
986 }
aliguori376253e2009-03-05 23:01:23 +0000987 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000988}
989
bellarda3a91a32004-06-04 11:06:21 +0000990typedef struct {
991 int keycode;
992 const char *name;
993} KeyDef;
994
995static const KeyDef key_defs[] = {
996 { 0x2a, "shift" },
997 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000998
bellarda3a91a32004-06-04 11:06:21 +0000999 { 0x38, "alt" },
1000 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001001 { 0x64, "altgr" },
1002 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001003 { 0x1d, "ctrl" },
1004 { 0x9d, "ctrl_r" },
1005
1006 { 0xdd, "menu" },
1007
1008 { 0x01, "esc" },
1009
1010 { 0x02, "1" },
1011 { 0x03, "2" },
1012 { 0x04, "3" },
1013 { 0x05, "4" },
1014 { 0x06, "5" },
1015 { 0x07, "6" },
1016 { 0x08, "7" },
1017 { 0x09, "8" },
1018 { 0x0a, "9" },
1019 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001020 { 0x0c, "minus" },
1021 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001022 { 0x0e, "backspace" },
1023
1024 { 0x0f, "tab" },
1025 { 0x10, "q" },
1026 { 0x11, "w" },
1027 { 0x12, "e" },
1028 { 0x13, "r" },
1029 { 0x14, "t" },
1030 { 0x15, "y" },
1031 { 0x16, "u" },
1032 { 0x17, "i" },
1033 { 0x18, "o" },
1034 { 0x19, "p" },
1035
1036 { 0x1c, "ret" },
1037
1038 { 0x1e, "a" },
1039 { 0x1f, "s" },
1040 { 0x20, "d" },
1041 { 0x21, "f" },
1042 { 0x22, "g" },
1043 { 0x23, "h" },
1044 { 0x24, "j" },
1045 { 0x25, "k" },
1046 { 0x26, "l" },
1047
1048 { 0x2c, "z" },
1049 { 0x2d, "x" },
1050 { 0x2e, "c" },
1051 { 0x2f, "v" },
1052 { 0x30, "b" },
1053 { 0x31, "n" },
1054 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001055 { 0x33, "comma" },
1056 { 0x34, "dot" },
1057 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001058
balrog4d3b6f62008-02-10 16:33:14 +00001059 { 0x37, "asterisk" },
1060
bellarda3a91a32004-06-04 11:06:21 +00001061 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001062 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001063 { 0x3b, "f1" },
1064 { 0x3c, "f2" },
1065 { 0x3d, "f3" },
1066 { 0x3e, "f4" },
1067 { 0x3f, "f5" },
1068 { 0x40, "f6" },
1069 { 0x41, "f7" },
1070 { 0x42, "f8" },
1071 { 0x43, "f9" },
1072 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001073 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001074 { 0x46, "scroll_lock" },
1075
bellard64866c32006-05-07 18:03:31 +00001076 { 0xb5, "kp_divide" },
1077 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001078 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001079 { 0x4e, "kp_add" },
1080 { 0x9c, "kp_enter" },
1081 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001082 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001083
1084 { 0x52, "kp_0" },
1085 { 0x4f, "kp_1" },
1086 { 0x50, "kp_2" },
1087 { 0x51, "kp_3" },
1088 { 0x4b, "kp_4" },
1089 { 0x4c, "kp_5" },
1090 { 0x4d, "kp_6" },
1091 { 0x47, "kp_7" },
1092 { 0x48, "kp_8" },
1093 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001094
bellarda3a91a32004-06-04 11:06:21 +00001095 { 0x56, "<" },
1096
1097 { 0x57, "f11" },
1098 { 0x58, "f12" },
1099
1100 { 0xb7, "print" },
1101
1102 { 0xc7, "home" },
1103 { 0xc9, "pgup" },
1104 { 0xd1, "pgdn" },
1105 { 0xcf, "end" },
1106
1107 { 0xcb, "left" },
1108 { 0xc8, "up" },
1109 { 0xd0, "down" },
1110 { 0xcd, "right" },
1111
1112 { 0xd2, "insert" },
1113 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001114#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1115 { 0xf0, "stop" },
1116 { 0xf1, "again" },
1117 { 0xf2, "props" },
1118 { 0xf3, "undo" },
1119 { 0xf4, "front" },
1120 { 0xf5, "copy" },
1121 { 0xf6, "open" },
1122 { 0xf7, "paste" },
1123 { 0xf8, "find" },
1124 { 0xf9, "cut" },
1125 { 0xfa, "lf" },
1126 { 0xfb, "help" },
1127 { 0xfc, "meta_l" },
1128 { 0xfd, "meta_r" },
1129 { 0xfe, "compose" },
1130#endif
bellarda3a91a32004-06-04 11:06:21 +00001131 { 0, NULL },
1132};
1133
1134static int get_keycode(const char *key)
1135{
1136 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001137 char *endp;
1138 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001139
1140 for(p = key_defs; p->name != NULL; p++) {
1141 if (!strcmp(key, p->name))
1142 return p->keycode;
1143 }
bellard64866c32006-05-07 18:03:31 +00001144 if (strstart(key, "0x", NULL)) {
1145 ret = strtoul(key, &endp, 0);
1146 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1147 return ret;
1148 }
bellarda3a91a32004-06-04 11:06:21 +00001149 return -1;
1150}
1151
balrogc8256f92008-06-08 22:45:01 +00001152#define MAX_KEYCODES 16
1153static uint8_t keycodes[MAX_KEYCODES];
1154static int nb_pending_keycodes;
1155static QEMUTimer *key_timer;
1156
1157static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001158{
balrogc8256f92008-06-08 22:45:01 +00001159 int keycode;
1160
1161 while (nb_pending_keycodes > 0) {
1162 nb_pending_keycodes--;
1163 keycode = keycodes[nb_pending_keycodes];
1164 if (keycode & 0x80)
1165 kbd_put_keycode(0xe0);
1166 kbd_put_keycode(keycode | 0x80);
1167 }
1168}
1169
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001170static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001171{
balrog3401c0d2008-06-04 10:05:59 +00001172 char keyname_buf[16];
1173 char *separator;
1174 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001175 const char *string = qdict_get_str(qdict, "string");
1176 int has_hold_time = qdict_haskey(qdict, "hold_time");
1177 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001178
balrogc8256f92008-06-08 22:45:01 +00001179 if (nb_pending_keycodes > 0) {
1180 qemu_del_timer(key_timer);
1181 release_keys(NULL);
1182 }
1183 if (!has_hold_time)
1184 hold_time = 100;
1185 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001186 while (1) {
1187 separator = strchr(string, '-');
1188 keyname_len = separator ? separator - string : strlen(string);
1189 if (keyname_len > 0) {
1190 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1191 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001192 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001193 return;
bellarda3a91a32004-06-04 11:06:21 +00001194 }
balrogc8256f92008-06-08 22:45:01 +00001195 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001196 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001197 return;
1198 }
1199 keyname_buf[keyname_len] = 0;
1200 keycode = get_keycode(keyname_buf);
1201 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001202 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001203 return;
1204 }
balrogc8256f92008-06-08 22:45:01 +00001205 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001206 }
balrog3401c0d2008-06-04 10:05:59 +00001207 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001208 break;
balrog3401c0d2008-06-04 10:05:59 +00001209 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001210 }
balrogc8256f92008-06-08 22:45:01 +00001211 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001212 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001213 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001214 keycode = keycodes[i];
1215 if (keycode & 0x80)
1216 kbd_put_keycode(0xe0);
1217 kbd_put_keycode(keycode & 0x7f);
1218 }
balrogc8256f92008-06-08 22:45:01 +00001219 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001220 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001221 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001222}
1223
bellard13224a82006-07-14 22:03:35 +00001224static int mouse_button_state;
1225
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001226static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001227{
1228 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001229 const char *dx_str = qdict_get_str(qdict, "dx_str");
1230 const char *dy_str = qdict_get_str(qdict, "dy_str");
1231 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001232 dx = strtol(dx_str, NULL, 0);
1233 dy = strtol(dy_str, NULL, 0);
1234 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001235 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001236 dz = strtol(dz_str, NULL, 0);
1237 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1238}
1239
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001240static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001241{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001242 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001243 mouse_button_state = button_state;
1244 kbd_mouse_event(0, 0, 0, mouse_button_state);
1245}
1246
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001247static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001248{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001249 int size = qdict_get_int(qdict, "size");
1250 int addr = qdict_get_int(qdict, "addr");
1251 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001252 uint32_t val;
1253 int suffix;
1254
1255 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001256 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001257 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001258 addr++;
1259 }
1260 addr &= 0xffff;
1261
1262 switch(size) {
1263 default:
1264 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001265 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001266 suffix = 'b';
1267 break;
1268 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001269 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001270 suffix = 'w';
1271 break;
1272 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001273 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001274 suffix = 'l';
1275 break;
1276 }
aliguori376253e2009-03-05 23:01:23 +00001277 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1278 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001279}
bellarda3a91a32004-06-04 11:06:21 +00001280
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001282{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001283 int size = qdict_get_int(qdict, "size");
1284 int addr = qdict_get_int(qdict, "addr");
1285 int val = qdict_get_int(qdict, "val");
1286
Jan Kiszkaf1147842009-07-14 10:20:11 +02001287 addr &= IOPORTS_MASK;
1288
1289 switch (size) {
1290 default:
1291 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001292 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001293 break;
1294 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001295 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001296 break;
1297 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001298 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001299 break;
1300 }
1301}
1302
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001303static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001304{
1305 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001306 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001307
Jan Kiszka76e30d02009-07-02 00:19:02 +02001308 res = qemu_boot_set(bootdevice);
1309 if (res == 0) {
1310 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1311 } else if (res > 0) {
1312 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001313 } else {
aliguori376253e2009-03-05 23:01:23 +00001314 monitor_printf(mon, "no function defined to set boot device list for "
1315 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001316 }
1317}
1318
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001319/**
1320 * do_system_reset(): Issue a machine reset
1321 */
1322static void do_system_reset(Monitor *mon, const QDict *qdict,
1323 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001324{
1325 qemu_system_reset_request();
1326}
1327
Luiz Capitulino43076662009-10-07 13:41:59 -03001328/**
1329 * do_system_powerdown(): Issue a machine powerdown
1330 */
1331static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1332 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001333{
1334 qemu_system_powerdown_request();
1335}
1336
bellardb86bda52004-09-18 19:32:46 +00001337#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001338static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001339{
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1341 addr,
1342 pte & mask,
1343 pte & PG_GLOBAL_MASK ? 'G' : '-',
1344 pte & PG_PSE_MASK ? 'P' : '-',
1345 pte & PG_DIRTY_MASK ? 'D' : '-',
1346 pte & PG_ACCESSED_MASK ? 'A' : '-',
1347 pte & PG_PCD_MASK ? 'C' : '-',
1348 pte & PG_PWT_MASK ? 'T' : '-',
1349 pte & PG_USER_MASK ? 'U' : '-',
1350 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001351}
1352
aliguori376253e2009-03-05 23:01:23 +00001353static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001354{
bellard6a00d602005-11-21 23:25:50 +00001355 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001356 int l1, l2;
1357 uint32_t pgd, pde, pte;
1358
bellard6a00d602005-11-21 23:25:50 +00001359 env = mon_get_cpu();
1360 if (!env)
1361 return;
1362
bellardb86bda52004-09-18 19:32:46 +00001363 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001364 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001365 return;
1366 }
1367 pgd = env->cr[3] & ~0xfff;
1368 for(l1 = 0; l1 < 1024; l1++) {
1369 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1370 pde = le32_to_cpu(pde);
1371 if (pde & PG_PRESENT_MASK) {
1372 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001373 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001374 } else {
1375 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001376 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001377 (uint8_t *)&pte, 4);
1378 pte = le32_to_cpu(pte);
1379 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001380 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001381 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001382 ~0xfff);
1383 }
1384 }
1385 }
1386 }
1387 }
1388}
1389
aliguori376253e2009-03-05 23:01:23 +00001390static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001391 uint32_t end, int prot)
1392{
bellard9746b152004-11-11 18:30:24 +00001393 int prot1;
1394 prot1 = *plast_prot;
1395 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001396 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001397 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1398 *pstart, end, end - *pstart,
1399 prot1 & PG_USER_MASK ? 'u' : '-',
1400 'r',
1401 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001402 }
1403 if (prot != 0)
1404 *pstart = end;
1405 else
1406 *pstart = -1;
1407 *plast_prot = prot;
1408 }
1409}
1410
aliguori376253e2009-03-05 23:01:23 +00001411static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001412{
bellard6a00d602005-11-21 23:25:50 +00001413 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001414 int l1, l2, prot, last_prot;
1415 uint32_t pgd, pde, pte, start, end;
1416
bellard6a00d602005-11-21 23:25:50 +00001417 env = mon_get_cpu();
1418 if (!env)
1419 return;
1420
bellardb86bda52004-09-18 19:32:46 +00001421 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001422 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001423 return;
1424 }
1425 pgd = env->cr[3] & ~0xfff;
1426 last_prot = 0;
1427 start = -1;
1428 for(l1 = 0; l1 < 1024; l1++) {
1429 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1430 pde = le32_to_cpu(pde);
1431 end = l1 << 22;
1432 if (pde & PG_PRESENT_MASK) {
1433 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1434 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001435 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001436 } else {
1437 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001438 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001439 (uint8_t *)&pte, 4);
1440 pte = le32_to_cpu(pte);
1441 end = (l1 << 22) + (l2 << 12);
1442 if (pte & PG_PRESENT_MASK) {
1443 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1444 } else {
1445 prot = 0;
1446 }
aliguori376253e2009-03-05 23:01:23 +00001447 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001448 }
1449 }
1450 } else {
1451 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001452 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001453 }
1454 }
1455}
1456#endif
1457
aurel327c664e22009-03-03 06:12:22 +00001458#if defined(TARGET_SH4)
1459
aliguori376253e2009-03-05 23:01:23 +00001460static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001461{
aliguori376253e2009-03-05 23:01:23 +00001462 monitor_printf(mon, " tlb%i:\t"
1463 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1464 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1465 "dirty=%hhu writethrough=%hhu\n",
1466 idx,
1467 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1468 tlb->v, tlb->sh, tlb->c, tlb->pr,
1469 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001470}
1471
aliguori376253e2009-03-05 23:01:23 +00001472static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001473{
1474 CPUState *env = mon_get_cpu();
1475 int i;
1476
aliguori376253e2009-03-05 23:01:23 +00001477 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001478 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001479 print_tlb (mon, i, &env->itlb[i]);
1480 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001481 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001482 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001483}
1484
1485#endif
1486
aliguori376253e2009-03-05 23:01:23 +00001487static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001488{
1489#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001490 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001491 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001492 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001493 else
aliguori376253e2009-03-05 23:01:23 +00001494 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001495#else
aliguori376253e2009-03-05 23:01:23 +00001496 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001497#endif
1498}
1499
aliguori030ea372009-04-21 22:30:47 +00001500static void do_info_numa(Monitor *mon)
1501{
aliguorib28b6232009-04-22 20:20:29 +00001502 int i;
aliguori030ea372009-04-21 22:30:47 +00001503 CPUState *env;
1504
1505 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1506 for (i = 0; i < nb_numa_nodes; i++) {
1507 monitor_printf(mon, "node %d cpus:", i);
1508 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1509 if (env->numa_node == i) {
1510 monitor_printf(mon, " %d", env->cpu_index);
1511 }
1512 }
1513 monitor_printf(mon, "\n");
1514 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1515 node_mem[i] >> 20);
1516 }
1517}
1518
bellard5f1ce942006-02-08 22:40:15 +00001519#ifdef CONFIG_PROFILER
1520
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001521int64_t qemu_time;
1522int64_t dev_time;
1523
aliguori376253e2009-03-05 23:01:23 +00001524static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001525{
1526 int64_t total;
1527 total = qemu_time;
1528 if (total == 0)
1529 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001530 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001531 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001533 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001534 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001535 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001536}
1537#else
aliguori376253e2009-03-05 23:01:23 +00001538static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001539{
aliguori376253e2009-03-05 23:01:23 +00001540 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001541}
1542#endif
1543
bellardec36b692006-07-16 18:57:03 +00001544/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001545static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001546
aliguori376253e2009-03-05 23:01:23 +00001547static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001548{
1549 int i;
1550 CaptureState *s;
1551
1552 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001553 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001554 s->ops.info (s->opaque);
1555 }
1556}
1557
Blue Swirl23130862009-06-06 08:22:04 +00001558#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001559static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001560{
1561 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001562 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001563 CaptureState *s;
1564
1565 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1566 if (i == n) {
1567 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001568 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001569 qemu_free (s);
1570 return;
1571 }
1572 }
1573}
1574
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001575static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001576{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001577 const char *path = qdict_get_str(qdict, "path");
1578 int has_freq = qdict_haskey(qdict, "freq");
1579 int freq = qdict_get_try_int(qdict, "freq", -1);
1580 int has_bits = qdict_haskey(qdict, "bits");
1581 int bits = qdict_get_try_int(qdict, "bits", -1);
1582 int has_channels = qdict_haskey(qdict, "nchannels");
1583 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001584 CaptureState *s;
1585
1586 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001587
1588 freq = has_freq ? freq : 44100;
1589 bits = has_bits ? bits : 16;
1590 nchannels = has_channels ? nchannels : 2;
1591
1592 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001593 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001594 qemu_free (s);
1595 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001596 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001597}
1598#endif
1599
aurel32dc1c0b72008-04-27 23:52:12 +00001600#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001601static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001602{
1603 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001604 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001605
1606 for (env = first_cpu; env != NULL; env = env->next_cpu)
1607 if (env->cpu_index == cpu_index) {
1608 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1609 break;
1610 }
1611}
1612#endif
1613
aliguori376253e2009-03-05 23:01:23 +00001614static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001615{
aurel321b530a62009-04-05 20:08:59 +00001616 if (vm_running) {
1617 if (singlestep) {
1618 monitor_printf(mon, "VM status: running (single step mode)\n");
1619 } else {
1620 monitor_printf(mon, "VM status: running\n");
1621 }
1622 } else
aliguori376253e2009-03-05 23:01:23 +00001623 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001624}
1625
Luiz Capitulino83fb1de2009-10-07 13:42:01 -03001626/**
1627 * do_balloon(): Request VM to change its memory allocation
1628 */
1629static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguoridf751fa2008-12-04 20:19:35 +00001630{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001631 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001632 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001633 qemu_balloon(target << 20);
1634}
1635
aliguori376253e2009-03-05 23:01:23 +00001636static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001637{
Anthony Liguoric227f092009-10-01 16:12:16 -05001638 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001639
1640 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001641 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001642 monitor_printf(mon, "Using KVM without synchronous MMU, "
1643 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001644 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001645 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001646 else
aliguori376253e2009-03-05 23:01:23 +00001647 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001648}
1649
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001650static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001651{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001652 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001653
aliguori76655d62009-03-06 20:27:37 +00001654 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001655 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001656 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001657 return acl;
1658}
aliguori76655d62009-03-06 20:27:37 +00001659
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001660static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001661{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001662 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001663 qemu_acl *acl = find_acl(mon, aclname);
1664 qemu_acl_entry *entry;
1665 int i = 0;
1666
1667 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001668 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001669 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001670 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001671 i++;
1672 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001673 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001674 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001675 }
1676}
1677
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001678static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001679{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001680 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001681 qemu_acl *acl = find_acl(mon, aclname);
1682
1683 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001684 qemu_acl_reset(acl);
1685 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001686 }
1687}
aliguori76655d62009-03-06 20:27:37 +00001688
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001689static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001690{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001691 const char *aclname = qdict_get_str(qdict, "aclname");
1692 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001693 qemu_acl *acl = find_acl(mon, aclname);
1694
1695 if (acl) {
1696 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001697 acl->defaultDeny = 0;
1698 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001699 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001700 acl->defaultDeny = 1;
1701 monitor_printf(mon, "acl: policy set to 'deny'\n");
1702 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001703 monitor_printf(mon, "acl: unknown policy '%s', "
1704 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001705 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001706 }
1707}
aliguori76655d62009-03-06 20:27:37 +00001708
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001709static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001710{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001711 const char *aclname = qdict_get_str(qdict, "aclname");
1712 const char *match = qdict_get_str(qdict, "match");
1713 const char *policy = qdict_get_str(qdict, "policy");
1714 int has_index = qdict_haskey(qdict, "index");
1715 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001716 qemu_acl *acl = find_acl(mon, aclname);
1717 int deny, ret;
1718
1719 if (acl) {
1720 if (strcmp(policy, "allow") == 0) {
1721 deny = 0;
1722 } else if (strcmp(policy, "deny") == 0) {
1723 deny = 1;
1724 } else {
1725 monitor_printf(mon, "acl: unknown policy '%s', "
1726 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001727 return;
1728 }
aliguori28a76be2009-03-06 20:27:40 +00001729 if (has_index)
1730 ret = qemu_acl_insert(acl, deny, match, index);
1731 else
1732 ret = qemu_acl_append(acl, deny, match);
1733 if (ret < 0)
1734 monitor_printf(mon, "acl: unable to add acl entry\n");
1735 else
1736 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001737 }
1738}
aliguori76655d62009-03-06 20:27:37 +00001739
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001740static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001741{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001742 const char *aclname = qdict_get_str(qdict, "aclname");
1743 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001744 qemu_acl *acl = find_acl(mon, aclname);
1745 int ret;
aliguori76655d62009-03-06 20:27:37 +00001746
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001747 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001748 ret = qemu_acl_remove(acl, match);
1749 if (ret < 0)
1750 monitor_printf(mon, "acl: no matching acl entry\n");
1751 else
1752 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001753 }
1754}
1755
Huang Ying79c4f6b2009-06-23 10:05:14 +08001756#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001757static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001758{
1759 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001760 int cpu_index = qdict_get_int(qdict, "cpu_index");
1761 int bank = qdict_get_int(qdict, "bank");
1762 uint64_t status = qdict_get_int(qdict, "status");
1763 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1764 uint64_t addr = qdict_get_int(qdict, "addr");
1765 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001766
1767 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1768 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1769 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1770 break;
1771 }
1772}
1773#endif
1774
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001775static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001776{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001777 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001778 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001779 int fd;
1780
1781 fd = qemu_chr_get_msgfd(mon->chr);
1782 if (fd == -1) {
1783 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1784 return;
1785 }
1786
1787 if (qemu_isdigit(fdname[0])) {
1788 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1789 return;
1790 }
1791
1792 fd = dup(fd);
1793 if (fd == -1) {
1794 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1795 strerror(errno));
1796 return;
1797 }
1798
Blue Swirl72cf2d42009-09-12 07:36:22 +00001799 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001800 if (strcmp(monfd->name, fdname) != 0) {
1801 continue;
1802 }
1803
1804 close(monfd->fd);
1805 monfd->fd = fd;
1806 return;
1807 }
1808
Anthony Liguoric227f092009-10-01 16:12:16 -05001809 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001810 monfd->name = qemu_strdup(fdname);
1811 monfd->fd = fd;
1812
Blue Swirl72cf2d42009-09-12 07:36:22 +00001813 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001814}
1815
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001816static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001817{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001818 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001819 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001820
Blue Swirl72cf2d42009-09-12 07:36:22 +00001821 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001822 if (strcmp(monfd->name, fdname) != 0) {
1823 continue;
1824 }
1825
Blue Swirl72cf2d42009-09-12 07:36:22 +00001826 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001827 close(monfd->fd);
1828 qemu_free(monfd->name);
1829 qemu_free(monfd);
1830 return;
1831 }
1832
1833 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1834 fdname);
1835}
1836
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001837static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001838{
1839 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001840 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001841
1842 vm_stop(0);
1843
Juan Quintela05f24012009-08-20 19:42:22 +02001844 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001845 vm_start();
1846}
1847
Mark McLoughlin7768e042009-07-22 09:11:41 +01001848int monitor_get_fd(Monitor *mon, const char *fdname)
1849{
Anthony Liguoric227f092009-10-01 16:12:16 -05001850 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001851
Blue Swirl72cf2d42009-09-12 07:36:22 +00001852 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001853 int fd;
1854
1855 if (strcmp(monfd->name, fdname) != 0) {
1856 continue;
1857 }
1858
1859 fd = monfd->fd;
1860
1861 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001862 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001863 qemu_free(monfd->name);
1864 qemu_free(monfd);
1865
1866 return fd;
1867 }
1868
1869 return -1;
1870}
1871
Anthony Liguoric227f092009-10-01 16:12:16 -05001872static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001873#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001874 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001875};
1876
Blue Swirl23130862009-06-06 08:22:04 +00001877/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001878static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001879 {
1880 .name = "version",
1881 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001882 .params = "",
1883 .help = "show the version of QEMU",
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03001884 .user_print = monitor_print_qobject,
1885 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001886 },
1887 {
1888 .name = "network",
1889 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001890 .params = "",
1891 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001892 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001893 },
1894 {
1895 .name = "chardev",
1896 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001897 .params = "",
1898 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001899 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001900 },
1901 {
1902 .name = "block",
1903 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001904 .params = "",
1905 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001906 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001907 },
1908 {
1909 .name = "blockstats",
1910 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001911 .params = "",
1912 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03001913 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001914 },
1915 {
1916 .name = "registers",
1917 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001918 .params = "",
1919 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03001920 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001921 },
1922 {
1923 .name = "cpus",
1924 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001925 .params = "",
1926 .help = "show infos for each CPU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001927 .mhandler.info = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001928 },
1929 {
1930 .name = "history",
1931 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001932 .params = "",
1933 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03001934 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001935 },
1936 {
1937 .name = "irq",
1938 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001939 .params = "",
1940 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03001941 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001942 },
1943 {
1944 .name = "pic",
1945 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001946 .params = "",
1947 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001948 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001949 },
1950 {
1951 .name = "pci",
1952 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001953 .params = "",
1954 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001955 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001956 },
aurel327c664e22009-03-03 06:12:22 +00001957#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001958 {
1959 .name = "tlb",
1960 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001961 .params = "",
1962 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001963 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001964 },
aurel327c664e22009-03-03 06:12:22 +00001965#endif
1966#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001967 {
1968 .name = "mem",
1969 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001970 .params = "",
1971 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001972 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001973 },
1974 {
1975 .name = "hpet",
1976 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001977 .params = "",
1978 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03001979 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001980 },
bellardb86bda52004-09-18 19:32:46 +00001981#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001982 {
1983 .name = "jit",
1984 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001985 .params = "",
1986 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001987 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001988 },
1989 {
1990 .name = "kvm",
1991 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001992 .params = "",
1993 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001994 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001995 },
1996 {
1997 .name = "numa",
1998 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001999 .params = "",
2000 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002001 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002002 },
2003 {
2004 .name = "usb",
2005 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002006 .params = "",
2007 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002008 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002009 },
2010 {
2011 .name = "usbhost",
2012 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002013 .params = "",
2014 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002015 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002016 },
2017 {
2018 .name = "profile",
2019 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002020 .params = "",
2021 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002022 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002023 },
2024 {
2025 .name = "capture",
2026 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002027 .params = "",
2028 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002029 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002030 },
2031 {
2032 .name = "snapshots",
2033 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002034 .params = "",
2035 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002036 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002037 },
2038 {
2039 .name = "status",
2040 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002041 .params = "",
2042 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002043 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002044 },
2045 {
2046 .name = "pcmcia",
2047 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002048 .params = "",
2049 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002050 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002051 },
2052 {
2053 .name = "mice",
2054 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002055 .params = "",
2056 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002057 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002058 },
2059 {
2060 .name = "vnc",
2061 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002062 .params = "",
2063 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002064 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002065 },
2066 {
2067 .name = "name",
2068 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002069 .params = "",
2070 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002071 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002072 },
2073 {
2074 .name = "uuid",
2075 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002076 .params = "",
2077 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002078 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002079 },
j_mayer76a66252007-03-07 08:32:30 +00002080#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002081 {
2082 .name = "cpustats",
2083 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 .params = "",
2085 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002086 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002087 },
j_mayer76a66252007-03-07 08:32:30 +00002088#endif
blueswir131a60e22007-10-26 18:42:59 +00002089#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002090 {
2091 .name = "usernet",
2092 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002093 .params = "",
2094 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002095 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002096 },
blueswir131a60e22007-10-26 18:42:59 +00002097#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002098 {
2099 .name = "migrate",
2100 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002101 .params = "",
2102 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002103 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002104 },
2105 {
2106 .name = "balloon",
2107 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002108 .params = "",
2109 .help = "show balloon information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002110 .mhandler.info = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002111 },
2112 {
2113 .name = "qtree",
2114 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002115 .params = "",
2116 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002117 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002118 },
2119 {
2120 .name = "qdm",
2121 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002122 .params = "",
2123 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002124 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002125 },
2126 {
2127 .name = "roms",
2128 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002129 .params = "",
2130 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002131 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002132 },
2133 {
2134 .name = NULL,
2135 },
bellard9dc39cb2004-03-14 21:38:27 +00002136};
2137
bellard9307c4c2004-04-04 12:57:25 +00002138/*******************************************************************/
2139
2140static const char *pch;
2141static jmp_buf expr_env;
2142
bellard92a31b12005-02-10 22:00:52 +00002143#define MD_TLONG 0
2144#define MD_I32 1
2145
bellard9307c4c2004-04-04 12:57:25 +00002146typedef struct MonitorDef {
2147 const char *name;
2148 int offset;
blueswir18662d652008-10-02 18:32:44 +00002149 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002150 int type;
bellard9307c4c2004-04-04 12:57:25 +00002151} MonitorDef;
2152
bellard57206fd2004-04-25 18:54:52 +00002153#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002154static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002155{
bellard6a00d602005-11-21 23:25:50 +00002156 CPUState *env = mon_get_cpu();
2157 if (!env)
2158 return 0;
2159 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002160}
2161#endif
2162
bellarda541f292004-04-12 20:39:29 +00002163#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002164static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002165{
bellard6a00d602005-11-21 23:25:50 +00002166 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002167 unsigned int u;
2168 int i;
2169
bellard6a00d602005-11-21 23:25:50 +00002170 if (!env)
2171 return 0;
2172
bellarda541f292004-04-12 20:39:29 +00002173 u = 0;
2174 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002175 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002176
2177 return u;
2178}
2179
blueswir18662d652008-10-02 18:32:44 +00002180static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002181{
bellard6a00d602005-11-21 23:25:50 +00002182 CPUState *env = mon_get_cpu();
2183 if (!env)
2184 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002185 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002186}
2187
blueswir18662d652008-10-02 18:32:44 +00002188static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002189{
bellard6a00d602005-11-21 23:25:50 +00002190 CPUState *env = mon_get_cpu();
2191 if (!env)
2192 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002193 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002194}
bellard9fddaa02004-05-21 12:59:32 +00002195
blueswir18662d652008-10-02 18:32:44 +00002196static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002197{
bellard6a00d602005-11-21 23:25:50 +00002198 CPUState *env = mon_get_cpu();
2199 if (!env)
2200 return 0;
2201 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002202}
2203
blueswir18662d652008-10-02 18:32:44 +00002204static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002205{
bellard6a00d602005-11-21 23:25:50 +00002206 CPUState *env = mon_get_cpu();
2207 if (!env)
2208 return 0;
2209 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002210}
2211
blueswir18662d652008-10-02 18:32:44 +00002212static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002213{
bellard6a00d602005-11-21 23:25:50 +00002214 CPUState *env = mon_get_cpu();
2215 if (!env)
2216 return 0;
2217 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002218}
bellarda541f292004-04-12 20:39:29 +00002219#endif
2220
bellarde95c8d52004-09-30 22:22:08 +00002221#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002222#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002223static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002224{
bellard6a00d602005-11-21 23:25:50 +00002225 CPUState *env = mon_get_cpu();
2226 if (!env)
2227 return 0;
2228 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002229}
bellard7b936c02005-10-30 17:05:13 +00002230#endif
bellarde95c8d52004-09-30 22:22:08 +00002231
blueswir18662d652008-10-02 18:32:44 +00002232static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002233{
bellard6a00d602005-11-21 23:25:50 +00002234 CPUState *env = mon_get_cpu();
2235 if (!env)
2236 return 0;
2237 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002238}
2239#endif
2240
blueswir18662d652008-10-02 18:32:44 +00002241static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002242#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002243
2244#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002245 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002246 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002247 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002248
bellard9307c4c2004-04-04 12:57:25 +00002249 { "eax", offsetof(CPUState, regs[0]) },
2250 { "ecx", offsetof(CPUState, regs[1]) },
2251 { "edx", offsetof(CPUState, regs[2]) },
2252 { "ebx", offsetof(CPUState, regs[3]) },
2253 { "esp|sp", offsetof(CPUState, regs[4]) },
2254 { "ebp|fp", offsetof(CPUState, regs[5]) },
2255 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002256 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002257#ifdef TARGET_X86_64
2258 { "r8", offsetof(CPUState, regs[8]) },
2259 { "r9", offsetof(CPUState, regs[9]) },
2260 { "r10", offsetof(CPUState, regs[10]) },
2261 { "r11", offsetof(CPUState, regs[11]) },
2262 { "r12", offsetof(CPUState, regs[12]) },
2263 { "r13", offsetof(CPUState, regs[13]) },
2264 { "r14", offsetof(CPUState, regs[14]) },
2265 { "r15", offsetof(CPUState, regs[15]) },
2266#endif
bellard9307c4c2004-04-04 12:57:25 +00002267 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002268 { "eip", offsetof(CPUState, eip) },
2269 SEG("cs", R_CS)
2270 SEG("ds", R_DS)
2271 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002272 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002273 SEG("fs", R_FS)
2274 SEG("gs", R_GS)
2275 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002276#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002277 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002278 { "r0", offsetof(CPUState, gpr[0]) },
2279 { "r1", offsetof(CPUState, gpr[1]) },
2280 { "r2", offsetof(CPUState, gpr[2]) },
2281 { "r3", offsetof(CPUState, gpr[3]) },
2282 { "r4", offsetof(CPUState, gpr[4]) },
2283 { "r5", offsetof(CPUState, gpr[5]) },
2284 { "r6", offsetof(CPUState, gpr[6]) },
2285 { "r7", offsetof(CPUState, gpr[7]) },
2286 { "r8", offsetof(CPUState, gpr[8]) },
2287 { "r9", offsetof(CPUState, gpr[9]) },
2288 { "r10", offsetof(CPUState, gpr[10]) },
2289 { "r11", offsetof(CPUState, gpr[11]) },
2290 { "r12", offsetof(CPUState, gpr[12]) },
2291 { "r13", offsetof(CPUState, gpr[13]) },
2292 { "r14", offsetof(CPUState, gpr[14]) },
2293 { "r15", offsetof(CPUState, gpr[15]) },
2294 { "r16", offsetof(CPUState, gpr[16]) },
2295 { "r17", offsetof(CPUState, gpr[17]) },
2296 { "r18", offsetof(CPUState, gpr[18]) },
2297 { "r19", offsetof(CPUState, gpr[19]) },
2298 { "r20", offsetof(CPUState, gpr[20]) },
2299 { "r21", offsetof(CPUState, gpr[21]) },
2300 { "r22", offsetof(CPUState, gpr[22]) },
2301 { "r23", offsetof(CPUState, gpr[23]) },
2302 { "r24", offsetof(CPUState, gpr[24]) },
2303 { "r25", offsetof(CPUState, gpr[25]) },
2304 { "r26", offsetof(CPUState, gpr[26]) },
2305 { "r27", offsetof(CPUState, gpr[27]) },
2306 { "r28", offsetof(CPUState, gpr[28]) },
2307 { "r29", offsetof(CPUState, gpr[29]) },
2308 { "r30", offsetof(CPUState, gpr[30]) },
2309 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002310 /* Floating point registers */
2311 { "f0", offsetof(CPUState, fpr[0]) },
2312 { "f1", offsetof(CPUState, fpr[1]) },
2313 { "f2", offsetof(CPUState, fpr[2]) },
2314 { "f3", offsetof(CPUState, fpr[3]) },
2315 { "f4", offsetof(CPUState, fpr[4]) },
2316 { "f5", offsetof(CPUState, fpr[5]) },
2317 { "f6", offsetof(CPUState, fpr[6]) },
2318 { "f7", offsetof(CPUState, fpr[7]) },
2319 { "f8", offsetof(CPUState, fpr[8]) },
2320 { "f9", offsetof(CPUState, fpr[9]) },
2321 { "f10", offsetof(CPUState, fpr[10]) },
2322 { "f11", offsetof(CPUState, fpr[11]) },
2323 { "f12", offsetof(CPUState, fpr[12]) },
2324 { "f13", offsetof(CPUState, fpr[13]) },
2325 { "f14", offsetof(CPUState, fpr[14]) },
2326 { "f15", offsetof(CPUState, fpr[15]) },
2327 { "f16", offsetof(CPUState, fpr[16]) },
2328 { "f17", offsetof(CPUState, fpr[17]) },
2329 { "f18", offsetof(CPUState, fpr[18]) },
2330 { "f19", offsetof(CPUState, fpr[19]) },
2331 { "f20", offsetof(CPUState, fpr[20]) },
2332 { "f21", offsetof(CPUState, fpr[21]) },
2333 { "f22", offsetof(CPUState, fpr[22]) },
2334 { "f23", offsetof(CPUState, fpr[23]) },
2335 { "f24", offsetof(CPUState, fpr[24]) },
2336 { "f25", offsetof(CPUState, fpr[25]) },
2337 { "f26", offsetof(CPUState, fpr[26]) },
2338 { "f27", offsetof(CPUState, fpr[27]) },
2339 { "f28", offsetof(CPUState, fpr[28]) },
2340 { "f29", offsetof(CPUState, fpr[29]) },
2341 { "f30", offsetof(CPUState, fpr[30]) },
2342 { "f31", offsetof(CPUState, fpr[31]) },
2343 { "fpscr", offsetof(CPUState, fpscr) },
2344 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002345 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002346 { "lr", offsetof(CPUState, lr) },
2347 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002348 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002349 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002350 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002351 { "msr", 0, &monitor_get_msr, },
2352 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002353 { "tbu", 0, &monitor_get_tbu, },
2354 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002355#if defined(TARGET_PPC64)
2356 /* Address space register */
2357 { "asr", offsetof(CPUState, asr) },
2358#endif
2359 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002360 { "sdr1", offsetof(CPUState, sdr1) },
2361 { "sr0", offsetof(CPUState, sr[0]) },
2362 { "sr1", offsetof(CPUState, sr[1]) },
2363 { "sr2", offsetof(CPUState, sr[2]) },
2364 { "sr3", offsetof(CPUState, sr[3]) },
2365 { "sr4", offsetof(CPUState, sr[4]) },
2366 { "sr5", offsetof(CPUState, sr[5]) },
2367 { "sr6", offsetof(CPUState, sr[6]) },
2368 { "sr7", offsetof(CPUState, sr[7]) },
2369 { "sr8", offsetof(CPUState, sr[8]) },
2370 { "sr9", offsetof(CPUState, sr[9]) },
2371 { "sr10", offsetof(CPUState, sr[10]) },
2372 { "sr11", offsetof(CPUState, sr[11]) },
2373 { "sr12", offsetof(CPUState, sr[12]) },
2374 { "sr13", offsetof(CPUState, sr[13]) },
2375 { "sr14", offsetof(CPUState, sr[14]) },
2376 { "sr15", offsetof(CPUState, sr[15]) },
2377 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002378#elif defined(TARGET_SPARC)
2379 { "g0", offsetof(CPUState, gregs[0]) },
2380 { "g1", offsetof(CPUState, gregs[1]) },
2381 { "g2", offsetof(CPUState, gregs[2]) },
2382 { "g3", offsetof(CPUState, gregs[3]) },
2383 { "g4", offsetof(CPUState, gregs[4]) },
2384 { "g5", offsetof(CPUState, gregs[5]) },
2385 { "g6", offsetof(CPUState, gregs[6]) },
2386 { "g7", offsetof(CPUState, gregs[7]) },
2387 { "o0", 0, monitor_get_reg },
2388 { "o1", 1, monitor_get_reg },
2389 { "o2", 2, monitor_get_reg },
2390 { "o3", 3, monitor_get_reg },
2391 { "o4", 4, monitor_get_reg },
2392 { "o5", 5, monitor_get_reg },
2393 { "o6", 6, monitor_get_reg },
2394 { "o7", 7, monitor_get_reg },
2395 { "l0", 8, monitor_get_reg },
2396 { "l1", 9, monitor_get_reg },
2397 { "l2", 10, monitor_get_reg },
2398 { "l3", 11, monitor_get_reg },
2399 { "l4", 12, monitor_get_reg },
2400 { "l5", 13, monitor_get_reg },
2401 { "l6", 14, monitor_get_reg },
2402 { "l7", 15, monitor_get_reg },
2403 { "i0", 16, monitor_get_reg },
2404 { "i1", 17, monitor_get_reg },
2405 { "i2", 18, monitor_get_reg },
2406 { "i3", 19, monitor_get_reg },
2407 { "i4", 20, monitor_get_reg },
2408 { "i5", 21, monitor_get_reg },
2409 { "i6", 22, monitor_get_reg },
2410 { "i7", 23, monitor_get_reg },
2411 { "pc", offsetof(CPUState, pc) },
2412 { "npc", offsetof(CPUState, npc) },
2413 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002414#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002415 { "psr", 0, &monitor_get_psr, },
2416 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002417#endif
bellarde95c8d52004-09-30 22:22:08 +00002418 { "tbr", offsetof(CPUState, tbr) },
2419 { "fsr", offsetof(CPUState, fsr) },
2420 { "f0", offsetof(CPUState, fpr[0]) },
2421 { "f1", offsetof(CPUState, fpr[1]) },
2422 { "f2", offsetof(CPUState, fpr[2]) },
2423 { "f3", offsetof(CPUState, fpr[3]) },
2424 { "f4", offsetof(CPUState, fpr[4]) },
2425 { "f5", offsetof(CPUState, fpr[5]) },
2426 { "f6", offsetof(CPUState, fpr[6]) },
2427 { "f7", offsetof(CPUState, fpr[7]) },
2428 { "f8", offsetof(CPUState, fpr[8]) },
2429 { "f9", offsetof(CPUState, fpr[9]) },
2430 { "f10", offsetof(CPUState, fpr[10]) },
2431 { "f11", offsetof(CPUState, fpr[11]) },
2432 { "f12", offsetof(CPUState, fpr[12]) },
2433 { "f13", offsetof(CPUState, fpr[13]) },
2434 { "f14", offsetof(CPUState, fpr[14]) },
2435 { "f15", offsetof(CPUState, fpr[15]) },
2436 { "f16", offsetof(CPUState, fpr[16]) },
2437 { "f17", offsetof(CPUState, fpr[17]) },
2438 { "f18", offsetof(CPUState, fpr[18]) },
2439 { "f19", offsetof(CPUState, fpr[19]) },
2440 { "f20", offsetof(CPUState, fpr[20]) },
2441 { "f21", offsetof(CPUState, fpr[21]) },
2442 { "f22", offsetof(CPUState, fpr[22]) },
2443 { "f23", offsetof(CPUState, fpr[23]) },
2444 { "f24", offsetof(CPUState, fpr[24]) },
2445 { "f25", offsetof(CPUState, fpr[25]) },
2446 { "f26", offsetof(CPUState, fpr[26]) },
2447 { "f27", offsetof(CPUState, fpr[27]) },
2448 { "f28", offsetof(CPUState, fpr[28]) },
2449 { "f29", offsetof(CPUState, fpr[29]) },
2450 { "f30", offsetof(CPUState, fpr[30]) },
2451 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002452#ifdef TARGET_SPARC64
2453 { "f32", offsetof(CPUState, fpr[32]) },
2454 { "f34", offsetof(CPUState, fpr[34]) },
2455 { "f36", offsetof(CPUState, fpr[36]) },
2456 { "f38", offsetof(CPUState, fpr[38]) },
2457 { "f40", offsetof(CPUState, fpr[40]) },
2458 { "f42", offsetof(CPUState, fpr[42]) },
2459 { "f44", offsetof(CPUState, fpr[44]) },
2460 { "f46", offsetof(CPUState, fpr[46]) },
2461 { "f48", offsetof(CPUState, fpr[48]) },
2462 { "f50", offsetof(CPUState, fpr[50]) },
2463 { "f52", offsetof(CPUState, fpr[52]) },
2464 { "f54", offsetof(CPUState, fpr[54]) },
2465 { "f56", offsetof(CPUState, fpr[56]) },
2466 { "f58", offsetof(CPUState, fpr[58]) },
2467 { "f60", offsetof(CPUState, fpr[60]) },
2468 { "f62", offsetof(CPUState, fpr[62]) },
2469 { "asi", offsetof(CPUState, asi) },
2470 { "pstate", offsetof(CPUState, pstate) },
2471 { "cansave", offsetof(CPUState, cansave) },
2472 { "canrestore", offsetof(CPUState, canrestore) },
2473 { "otherwin", offsetof(CPUState, otherwin) },
2474 { "wstate", offsetof(CPUState, wstate) },
2475 { "cleanwin", offsetof(CPUState, cleanwin) },
2476 { "fprs", offsetof(CPUState, fprs) },
2477#endif
bellard9307c4c2004-04-04 12:57:25 +00002478#endif
2479 { NULL },
2480};
2481
aliguori376253e2009-03-05 23:01:23 +00002482static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002483{
aliguori376253e2009-03-05 23:01:23 +00002484 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002485 longjmp(expr_env, 1);
2486}
2487
bellard6a00d602005-11-21 23:25:50 +00002488/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002489static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002490{
blueswir18662d652008-10-02 18:32:44 +00002491 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002492 void *ptr;
2493
bellard9307c4c2004-04-04 12:57:25 +00002494 for(md = monitor_defs; md->name != NULL; md++) {
2495 if (compare_cmd(name, md->name)) {
2496 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002497 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002498 } else {
bellard6a00d602005-11-21 23:25:50 +00002499 CPUState *env = mon_get_cpu();
2500 if (!env)
2501 return -2;
2502 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002503 switch(md->type) {
2504 case MD_I32:
2505 *pval = *(int32_t *)ptr;
2506 break;
2507 case MD_TLONG:
2508 *pval = *(target_long *)ptr;
2509 break;
2510 default:
2511 *pval = 0;
2512 break;
2513 }
bellard9307c4c2004-04-04 12:57:25 +00002514 }
2515 return 0;
2516 }
2517 }
2518 return -1;
2519}
2520
2521static void next(void)
2522{
Blue Swirl660f11b2009-07-31 21:16:51 +00002523 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002524 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002525 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002526 pch++;
2527 }
2528}
2529
aliguori376253e2009-03-05 23:01:23 +00002530static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002531
aliguori376253e2009-03-05 23:01:23 +00002532static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002533{
blueswir1c2efc952007-09-25 17:28:42 +00002534 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002535 char *p;
bellard6a00d602005-11-21 23:25:50 +00002536 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002537
2538 switch(*pch) {
2539 case '+':
2540 next();
aliguori376253e2009-03-05 23:01:23 +00002541 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002542 break;
2543 case '-':
2544 next();
aliguori376253e2009-03-05 23:01:23 +00002545 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002546 break;
2547 case '~':
2548 next();
aliguori376253e2009-03-05 23:01:23 +00002549 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002550 break;
2551 case '(':
2552 next();
aliguori376253e2009-03-05 23:01:23 +00002553 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002554 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002555 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002556 }
2557 next();
2558 break;
bellard81d09122004-07-14 17:21:37 +00002559 case '\'':
2560 pch++;
2561 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002562 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002563 n = *pch;
2564 pch++;
2565 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002566 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002567 next();
2568 break;
bellard9307c4c2004-04-04 12:57:25 +00002569 case '$':
2570 {
2571 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002572 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002573
bellard9307c4c2004-04-04 12:57:25 +00002574 pch++;
2575 q = buf;
2576 while ((*pch >= 'a' && *pch <= 'z') ||
2577 (*pch >= 'A' && *pch <= 'Z') ||
2578 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002579 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002580 if ((q - buf) < sizeof(buf) - 1)
2581 *q++ = *pch;
2582 pch++;
2583 }
blueswir1cd390082008-11-16 13:53:32 +00002584 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002585 pch++;
2586 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002587 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002588 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002589 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002590 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002591 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002592 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002593 }
2594 break;
2595 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002596 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002597 n = 0;
2598 break;
2599 default:
blueswir17743e582007-09-24 18:39:04 +00002600#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002601 n = strtoull(pch, &p, 0);
2602#else
bellard9307c4c2004-04-04 12:57:25 +00002603 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002604#endif
bellard9307c4c2004-04-04 12:57:25 +00002605 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002606 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002607 }
2608 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002609 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002610 pch++;
2611 break;
2612 }
2613 return n;
2614}
2615
2616
aliguori376253e2009-03-05 23:01:23 +00002617static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002618{
blueswir1c2efc952007-09-25 17:28:42 +00002619 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002620 int op;
ths3b46e622007-09-17 08:09:54 +00002621
aliguori376253e2009-03-05 23:01:23 +00002622 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002623 for(;;) {
2624 op = *pch;
2625 if (op != '*' && op != '/' && op != '%')
2626 break;
2627 next();
aliguori376253e2009-03-05 23:01:23 +00002628 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002629 switch(op) {
2630 default:
2631 case '*':
2632 val *= val2;
2633 break;
2634 case '/':
2635 case '%':
ths5fafdf22007-09-16 21:08:06 +00002636 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002637 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002638 if (op == '/')
2639 val /= val2;
2640 else
2641 val %= val2;
2642 break;
2643 }
2644 }
2645 return val;
2646}
2647
aliguori376253e2009-03-05 23:01:23 +00002648static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002649{
blueswir1c2efc952007-09-25 17:28:42 +00002650 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002651 int op;
bellard9307c4c2004-04-04 12:57:25 +00002652
aliguori376253e2009-03-05 23:01:23 +00002653 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002654 for(;;) {
2655 op = *pch;
2656 if (op != '&' && op != '|' && op != '^')
2657 break;
2658 next();
aliguori376253e2009-03-05 23:01:23 +00002659 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002660 switch(op) {
2661 default:
2662 case '&':
2663 val &= val2;
2664 break;
2665 case '|':
2666 val |= val2;
2667 break;
2668 case '^':
2669 val ^= val2;
2670 break;
2671 }
2672 }
2673 return val;
2674}
2675
aliguori376253e2009-03-05 23:01:23 +00002676static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002677{
blueswir1c2efc952007-09-25 17:28:42 +00002678 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002679 int op;
bellard9307c4c2004-04-04 12:57:25 +00002680
aliguori376253e2009-03-05 23:01:23 +00002681 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002682 for(;;) {
2683 op = *pch;
2684 if (op != '+' && op != '-')
2685 break;
2686 next();
aliguori376253e2009-03-05 23:01:23 +00002687 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002688 if (op == '+')
2689 val += val2;
2690 else
2691 val -= val2;
2692 }
2693 return val;
2694}
2695
aliguori376253e2009-03-05 23:01:23 +00002696static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002697{
2698 pch = *pp;
2699 if (setjmp(expr_env)) {
2700 *pp = pch;
2701 return -1;
2702 }
blueswir1cd390082008-11-16 13:53:32 +00002703 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002704 pch++;
aliguori376253e2009-03-05 23:01:23 +00002705 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002706 *pp = pch;
2707 return 0;
2708}
2709
2710static int get_str(char *buf, int buf_size, const char **pp)
2711{
2712 const char *p;
2713 char *q;
2714 int c;
2715
bellard81d09122004-07-14 17:21:37 +00002716 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002717 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002718 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002719 p++;
2720 if (*p == '\0') {
2721 fail:
bellard81d09122004-07-14 17:21:37 +00002722 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002723 *pp = p;
2724 return -1;
2725 }
bellard9307c4c2004-04-04 12:57:25 +00002726 if (*p == '\"') {
2727 p++;
2728 while (*p != '\0' && *p != '\"') {
2729 if (*p == '\\') {
2730 p++;
2731 c = *p++;
2732 switch(c) {
2733 case 'n':
2734 c = '\n';
2735 break;
2736 case 'r':
2737 c = '\r';
2738 break;
2739 case '\\':
2740 case '\'':
2741 case '\"':
2742 break;
2743 default:
2744 qemu_printf("unsupported escape code: '\\%c'\n", c);
2745 goto fail;
2746 }
2747 if ((q - buf) < buf_size - 1) {
2748 *q++ = c;
2749 }
2750 } else {
2751 if ((q - buf) < buf_size - 1) {
2752 *q++ = *p;
2753 }
2754 p++;
2755 }
2756 }
2757 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002758 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002759 goto fail;
2760 }
2761 p++;
2762 } else {
blueswir1cd390082008-11-16 13:53:32 +00002763 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002764 if ((q - buf) < buf_size - 1) {
2765 *q++ = *p;
2766 }
2767 p++;
2768 }
bellard9307c4c2004-04-04 12:57:25 +00002769 }
bellard81d09122004-07-14 17:21:37 +00002770 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002771 *pp = p;
2772 return 0;
2773}
2774
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002775/*
2776 * Store the command-name in cmdname, and return a pointer to
2777 * the remaining of the command string.
2778 */
2779static const char *get_command_name(const char *cmdline,
2780 char *cmdname, size_t nlen)
2781{
2782 size_t len;
2783 const char *p, *pstart;
2784
2785 p = cmdline;
2786 while (qemu_isspace(*p))
2787 p++;
2788 if (*p == '\0')
2789 return NULL;
2790 pstart = p;
2791 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2792 p++;
2793 len = p - pstart;
2794 if (len > nlen - 1)
2795 len = nlen - 1;
2796 memcpy(cmdname, pstart, len);
2797 cmdname[len] = '\0';
2798 return p;
2799}
2800
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002801/**
2802 * Read key of 'type' into 'key' and return the current
2803 * 'type' pointer.
2804 */
2805static char *key_get_info(const char *type, char **key)
2806{
2807 size_t len;
2808 char *p, *str;
2809
2810 if (*type == ',')
2811 type++;
2812
2813 p = strchr(type, ':');
2814 if (!p) {
2815 *key = NULL;
2816 return NULL;
2817 }
2818 len = p - type;
2819
2820 str = qemu_malloc(len + 1);
2821 memcpy(str, type, len);
2822 str[len] = '\0';
2823
2824 *key = str;
2825 return ++p;
2826}
2827
bellard9307c4c2004-04-04 12:57:25 +00002828static int default_fmt_format = 'x';
2829static int default_fmt_size = 4;
2830
2831#define MAX_ARGS 16
2832
Anthony Liguoric227f092009-10-01 16:12:16 -05002833static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002834 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002835 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002836{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002837 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002838 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002839 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002840 char cmdname[256];
2841 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002842 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002843
2844#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002845 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002846#endif
ths3b46e622007-09-17 08:09:54 +00002847
bellard9307c4c2004-04-04 12:57:25 +00002848 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002849 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2850 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002851 return NULL;
ths3b46e622007-09-17 08:09:54 +00002852
bellard9307c4c2004-04-04 12:57:25 +00002853 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002854 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002855 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002856 break;
bellard9dc39cb2004-03-14 21:38:27 +00002857 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002858
2859 if (cmd->name == NULL) {
2860 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002861 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002862 }
bellard9307c4c2004-04-04 12:57:25 +00002863
bellard9307c4c2004-04-04 12:57:25 +00002864 /* parse the parameters */
2865 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002866 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002867 typestr = key_get_info(typestr, &key);
2868 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002869 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002870 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002871 typestr++;
2872 switch(c) {
2873 case 'F':
bellard81d09122004-07-14 17:21:37 +00002874 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002875 case 's':
2876 {
2877 int ret;
ths3b46e622007-09-17 08:09:54 +00002878
blueswir1cd390082008-11-16 13:53:32 +00002879 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002880 p++;
2881 if (*typestr == '?') {
2882 typestr++;
2883 if (*p == '\0') {
2884 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002885 break;
bellard9307c4c2004-04-04 12:57:25 +00002886 }
2887 }
2888 ret = get_str(buf, sizeof(buf), &p);
2889 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002890 switch(c) {
2891 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002892 monitor_printf(mon, "%s: filename expected\n",
2893 cmdname);
bellard81d09122004-07-14 17:21:37 +00002894 break;
2895 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002896 monitor_printf(mon, "%s: block device name expected\n",
2897 cmdname);
bellard81d09122004-07-14 17:21:37 +00002898 break;
2899 default:
aliguori376253e2009-03-05 23:01:23 +00002900 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002901 break;
2902 }
bellard9307c4c2004-04-04 12:57:25 +00002903 goto fail;
2904 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002905 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002906 }
2907 break;
2908 case '/':
2909 {
2910 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002911
blueswir1cd390082008-11-16 13:53:32 +00002912 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002913 p++;
2914 if (*p == '/') {
2915 /* format found */
2916 p++;
2917 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002918 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002919 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002920 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002921 count = count * 10 + (*p - '0');
2922 p++;
2923 }
2924 }
2925 size = -1;
2926 format = -1;
2927 for(;;) {
2928 switch(*p) {
2929 case 'o':
2930 case 'd':
2931 case 'u':
2932 case 'x':
2933 case 'i':
2934 case 'c':
2935 format = *p++;
2936 break;
2937 case 'b':
2938 size = 1;
2939 p++;
2940 break;
2941 case 'h':
2942 size = 2;
2943 p++;
2944 break;
2945 case 'w':
2946 size = 4;
2947 p++;
2948 break;
2949 case 'g':
2950 case 'L':
2951 size = 8;
2952 p++;
2953 break;
2954 default:
2955 goto next;
2956 }
2957 }
2958 next:
blueswir1cd390082008-11-16 13:53:32 +00002959 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002960 monitor_printf(mon, "invalid char in format: '%c'\n",
2961 *p);
bellard9307c4c2004-04-04 12:57:25 +00002962 goto fail;
2963 }
bellard9307c4c2004-04-04 12:57:25 +00002964 if (format < 0)
2965 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002966 if (format != 'i') {
2967 /* for 'i', not specifying a size gives -1 as size */
2968 if (size < 0)
2969 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002970 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002971 }
bellard9307c4c2004-04-04 12:57:25 +00002972 default_fmt_format = format;
2973 } else {
2974 count = 1;
2975 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002976 if (format != 'i') {
2977 size = default_fmt_size;
2978 } else {
2979 size = -1;
2980 }
bellard9307c4c2004-04-04 12:57:25 +00002981 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002982 qdict_put(qdict, "count", qint_from_int(count));
2983 qdict_put(qdict, "format", qint_from_int(format));
2984 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002985 }
2986 break;
2987 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002988 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002989 {
blueswir1c2efc952007-09-25 17:28:42 +00002990 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002991
blueswir1cd390082008-11-16 13:53:32 +00002992 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002993 p++;
bellard34405572004-06-08 00:55:58 +00002994 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002995 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002996 if (*p == '\0') {
2997 typestr++;
2998 break;
2999 }
bellard34405572004-06-08 00:55:58 +00003000 } else {
3001 if (*p == '.') {
3002 p++;
blueswir1cd390082008-11-16 13:53:32 +00003003 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003004 p++;
bellard34405572004-06-08 00:55:58 +00003005 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003006 typestr++;
3007 break;
bellard34405572004-06-08 00:55:58 +00003008 }
3009 }
bellard13224a82006-07-14 22:03:35 +00003010 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003011 }
aliguori376253e2009-03-05 23:01:23 +00003012 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003013 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003014 /* Check if 'i' is greater than 32-bit */
3015 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3016 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3017 monitor_printf(mon, "integer is for 32-bit values\n");
3018 goto fail;
3019 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003020 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003021 }
3022 break;
3023 case '-':
3024 {
3025 int has_option;
3026 /* option */
ths3b46e622007-09-17 08:09:54 +00003027
bellard9307c4c2004-04-04 12:57:25 +00003028 c = *typestr++;
3029 if (c == '\0')
3030 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003031 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003032 p++;
3033 has_option = 0;
3034 if (*p == '-') {
3035 p++;
3036 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00003037 monitor_printf(mon, "%s: unsupported option -%c\n",
3038 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00003039 goto fail;
3040 }
3041 p++;
3042 has_option = 1;
3043 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003044 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003045 }
3046 break;
3047 default:
3048 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003049 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003050 goto fail;
3051 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003052 qemu_free(key);
3053 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003054 }
3055 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003056 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003057 p++;
3058 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003059 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3060 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003061 goto fail;
3062 }
3063
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003064 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003065
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003066fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003067 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003068 return NULL;
3069}
3070
3071static void monitor_handle_command(Monitor *mon, const char *cmdline)
3072{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003073 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003074 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003075
3076 qdict = qdict_new();
3077
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003078 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003079 if (!cmd)
3080 goto out;
3081
3082 qemu_errors_to_mon(mon);
3083
3084 if (monitor_handler_ported(cmd)) {
3085 QObject *data = NULL;
3086
3087 cmd->mhandler.cmd_new(mon, qdict, &data);
3088 if (data)
3089 cmd->user_print(mon, data);
3090
3091 qobject_decref(data);
3092 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003093 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003094 }
3095
Luiz Capitulino13917be2009-10-07 13:41:54 -03003096 qemu_errors_to_previous();
3097
3098out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003099 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003100}
3101
bellard81d09122004-07-14 17:21:37 +00003102static void cmd_completion(const char *name, const char *list)
3103{
3104 const char *p, *pstart;
3105 char cmd[128];
3106 int len;
3107
3108 p = list;
3109 for(;;) {
3110 pstart = p;
3111 p = strchr(p, '|');
3112 if (!p)
3113 p = pstart + strlen(pstart);
3114 len = p - pstart;
3115 if (len > sizeof(cmd) - 2)
3116 len = sizeof(cmd) - 2;
3117 memcpy(cmd, pstart, len);
3118 cmd[len] = '\0';
3119 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003120 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003121 }
3122 if (*p == '\0')
3123 break;
3124 p++;
3125 }
3126}
3127
3128static void file_completion(const char *input)
3129{
3130 DIR *ffs;
3131 struct dirent *d;
3132 char path[1024];
3133 char file[1024], file_prefix[1024];
3134 int input_path_len;
3135 const char *p;
3136
ths5fafdf22007-09-16 21:08:06 +00003137 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003138 if (!p) {
3139 input_path_len = 0;
3140 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003141 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003142 } else {
3143 input_path_len = p - input + 1;
3144 memcpy(path, input, input_path_len);
3145 if (input_path_len > sizeof(path) - 1)
3146 input_path_len = sizeof(path) - 1;
3147 path[input_path_len] = '\0';
3148 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3149 }
3150#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003151 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3152 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003153#endif
3154 ffs = opendir(path);
3155 if (!ffs)
3156 return;
3157 for(;;) {
3158 struct stat sb;
3159 d = readdir(ffs);
3160 if (!d)
3161 break;
3162 if (strstart(d->d_name, file_prefix, NULL)) {
3163 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003164 if (input_path_len < sizeof(file))
3165 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3166 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003167 /* stat the file to find out if it's a directory.
3168 * In that case add a slash to speed up typing long paths
3169 */
3170 stat(file, &sb);
3171 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003172 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003173 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003174 }
3175 }
3176 closedir(ffs);
3177}
3178
aliguori51de9762009-03-05 23:00:43 +00003179static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003180{
aliguori51de9762009-03-05 23:00:43 +00003181 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003182 const char *input = opaque;
3183
3184 if (input[0] == '\0' ||
3185 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003186 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003187 }
3188}
3189
3190/* NOTE: this parser is an approximate form of the real command parser */
3191static void parse_cmdline(const char *cmdline,
3192 int *pnb_args, char **args)
3193{
3194 const char *p;
3195 int nb_args, ret;
3196 char buf[1024];
3197
3198 p = cmdline;
3199 nb_args = 0;
3200 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003201 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003202 p++;
3203 if (*p == '\0')
3204 break;
3205 if (nb_args >= MAX_ARGS)
3206 break;
3207 ret = get_str(buf, sizeof(buf), &p);
3208 args[nb_args] = qemu_strdup(buf);
3209 nb_args++;
3210 if (ret < 0)
3211 break;
3212 }
3213 *pnb_args = nb_args;
3214}
3215
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003216static const char *next_arg_type(const char *typestr)
3217{
3218 const char *p = strchr(typestr, ':');
3219 return (p != NULL ? ++p : typestr);
3220}
3221
aliguori4c36ba32009-03-05 23:01:37 +00003222static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003223{
3224 const char *cmdname;
3225 char *args[MAX_ARGS];
3226 int nb_args, i, len;
3227 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003228 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003229 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003230
3231 parse_cmdline(cmdline, &nb_args, args);
3232#ifdef DEBUG_COMPLETION
3233 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003234 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003235 }
3236#endif
3237
3238 /* if the line ends with a space, it means we want to complete the
3239 next arg */
3240 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003241 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003242 if (nb_args >= MAX_ARGS)
3243 return;
3244 args[nb_args++] = qemu_strdup("");
3245 }
3246 if (nb_args <= 1) {
3247 /* command completion */
3248 if (nb_args == 0)
3249 cmdname = "";
3250 else
3251 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003252 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003253 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003254 cmd_completion(cmdname, cmd->name);
3255 }
3256 } else {
3257 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003258 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003259 if (compare_cmd(args[0], cmd->name))
3260 goto found;
3261 }
3262 return;
3263 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003264 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003265 for(i = 0; i < nb_args - 2; i++) {
3266 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003267 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003268 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003269 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003270 }
3271 }
3272 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003273 if (*ptype == '-' && ptype[1] != '\0') {
3274 ptype += 2;
3275 }
bellard81d09122004-07-14 17:21:37 +00003276 switch(*ptype) {
3277 case 'F':
3278 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003279 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003280 file_completion(str);
3281 break;
3282 case 'B':
3283 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003284 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003285 bdrv_iterate(block_completion_it, (void *)str);
3286 break;
bellard7fe48482004-10-09 18:08:01 +00003287 case 's':
3288 /* XXX: more generic ? */
3289 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003290 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003291 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3292 cmd_completion(str, cmd->name);
3293 }
bellard64866c32006-05-07 18:03:31 +00003294 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003295 char *sep = strrchr(str, '-');
3296 if (sep)
3297 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003298 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003299 for(key = key_defs; key->name != NULL; key++) {
3300 cmd_completion(str, key->name);
3301 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003302 } else if (!strcmp(cmd->name, "help|?")) {
3303 readline_set_completion_index(cur_mon->rs, strlen(str));
3304 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3305 cmd_completion(str, cmd->name);
3306 }
bellard7fe48482004-10-09 18:08:01 +00003307 }
3308 break;
bellard81d09122004-07-14 17:21:37 +00003309 default:
3310 break;
3311 }
3312 }
3313 for(i = 0; i < nb_args; i++)
3314 qemu_free(args[i]);
3315}
3316
aliguori731b0362009-03-05 23:01:42 +00003317static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003318{
aliguori731b0362009-03-05 23:01:42 +00003319 Monitor *mon = opaque;
3320
3321 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003322}
3323
aliguori731b0362009-03-05 23:01:42 +00003324static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003325{
aliguori731b0362009-03-05 23:01:42 +00003326 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003327 int i;
aliguori376253e2009-03-05 23:01:23 +00003328
aliguori731b0362009-03-05 23:01:42 +00003329 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003330
aliguoricde76ee2009-03-05 23:01:51 +00003331 if (cur_mon->rs) {
3332 for (i = 0; i < size; i++)
3333 readline_handle_byte(cur_mon->rs, buf[i]);
3334 } else {
3335 if (size == 0 || buf[size - 1] != 0)
3336 monitor_printf(cur_mon, "corrupted command\n");
3337 else
3338 monitor_handle_command(cur_mon, (char *)buf);
3339 }
aliguori731b0362009-03-05 23:01:42 +00003340
3341 cur_mon = old_mon;
3342}
aliguorid8f44602008-10-06 13:52:44 +00003343
aliguori376253e2009-03-05 23:01:23 +00003344static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003345{
aliguori731b0362009-03-05 23:01:42 +00003346 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003347 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003348 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003349}
3350
aliguoricde76ee2009-03-05 23:01:51 +00003351int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003352{
aliguoricde76ee2009-03-05 23:01:51 +00003353 if (!mon->rs)
3354 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003355 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003356 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003357}
3358
aliguori376253e2009-03-05 23:01:23 +00003359void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003360{
aliguoricde76ee2009-03-05 23:01:51 +00003361 if (!mon->rs)
3362 return;
aliguori731b0362009-03-05 23:01:42 +00003363 if (--mon->suspend_cnt == 0)
3364 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003365}
3366
aliguori731b0362009-03-05 23:01:42 +00003367static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003368{
aliguori376253e2009-03-05 23:01:23 +00003369 Monitor *mon = opaque;
3370
aliguori2724b182009-03-05 23:01:47 +00003371 switch (event) {
3372 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003373 mon->mux_out = 0;
3374 if (mon->reset_seen) {
3375 readline_restart(mon->rs);
3376 monitor_resume(mon);
3377 monitor_flush(mon);
3378 } else {
3379 mon->suspend_cnt = 0;
3380 }
aliguori2724b182009-03-05 23:01:47 +00003381 break;
ths86e94de2007-01-05 22:01:59 +00003382
aliguori2724b182009-03-05 23:01:47 +00003383 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003384 if (mon->reset_seen) {
3385 if (mon->suspend_cnt == 0) {
3386 monitor_printf(mon, "\n");
3387 }
3388 monitor_flush(mon);
3389 monitor_suspend(mon);
3390 } else {
3391 mon->suspend_cnt++;
3392 }
3393 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003394 break;
3395
3396 case CHR_EVENT_RESET:
3397 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3398 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003399 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003400 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003401 }
3402 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003403 break;
3404 }
ths86e94de2007-01-05 22:01:59 +00003405}
3406
aliguori76655d62009-03-06 20:27:37 +00003407
3408/*
3409 * Local variables:
3410 * c-indent-level: 4
3411 * c-basic-offset: 4
3412 * tab-width: 8
3413 * End:
3414 */
3415
aliguori731b0362009-03-05 23:01:42 +00003416void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003417{
aliguori731b0362009-03-05 23:01:42 +00003418 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003419 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003420
3421 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003422 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003423 is_first_init = 0;
3424 }
aliguori87127162009-03-05 23:01:29 +00003425
3426 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003427
aliguori87127162009-03-05 23:01:29 +00003428 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003429 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003430 if (flags & MONITOR_USE_READLINE) {
3431 mon->rs = readline_init(mon, monitor_find_completion);
3432 monitor_read_command(mon, 0);
3433 }
aliguori87127162009-03-05 23:01:29 +00003434
aliguori731b0362009-03-05 23:01:42 +00003435 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3436 mon);
aliguori87127162009-03-05 23:01:29 +00003437
Blue Swirl72cf2d42009-09-12 07:36:22 +00003438 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003439 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003440 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003441}
3442
aliguori376253e2009-03-05 23:01:23 +00003443static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003444{
aliguoribb5fc202009-03-05 23:01:15 +00003445 BlockDriverState *bs = opaque;
3446 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003447
aliguoribb5fc202009-03-05 23:01:15 +00003448 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003449 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003450 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003451 }
aliguori731b0362009-03-05 23:01:42 +00003452 if (mon->password_completion_cb)
3453 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003454
aliguori731b0362009-03-05 23:01:42 +00003455 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003456}
aliguoric0f4ce72009-03-05 23:01:01 +00003457
aliguori376253e2009-03-05 23:01:23 +00003458void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003459 BlockDriverCompletionFunc *completion_cb,
3460 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003461{
aliguoricde76ee2009-03-05 23:01:51 +00003462 int err;
3463
aliguoribb5fc202009-03-05 23:01:15 +00003464 if (!bdrv_key_required(bs)) {
3465 if (completion_cb)
3466 completion_cb(opaque, 0);
3467 return;
3468 }
aliguoric0f4ce72009-03-05 23:01:01 +00003469
aliguori376253e2009-03-05 23:01:23 +00003470 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3471 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003472
aliguori731b0362009-03-05 23:01:42 +00003473 mon->password_completion_cb = completion_cb;
3474 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003475
aliguoricde76ee2009-03-05 23:01:51 +00003476 err = monitor_read_password(mon, bdrv_password_cb, bs);
3477
3478 if (err && completion_cb)
3479 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003480}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003481
3482typedef struct QemuErrorSink QemuErrorSink;
3483struct QemuErrorSink {
3484 enum {
3485 ERR_SINK_FILE,
3486 ERR_SINK_MONITOR,
3487 } dest;
3488 union {
3489 FILE *fp;
3490 Monitor *mon;
3491 };
3492 QemuErrorSink *previous;
3493};
3494
Blue Swirl528e93a2009-08-31 15:14:40 +00003495static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003496
3497void qemu_errors_to_file(FILE *fp)
3498{
3499 QemuErrorSink *sink;
3500
3501 sink = qemu_mallocz(sizeof(*sink));
3502 sink->dest = ERR_SINK_FILE;
3503 sink->fp = fp;
3504 sink->previous = qemu_error_sink;
3505 qemu_error_sink = sink;
3506}
3507
3508void qemu_errors_to_mon(Monitor *mon)
3509{
3510 QemuErrorSink *sink;
3511
3512 sink = qemu_mallocz(sizeof(*sink));
3513 sink->dest = ERR_SINK_MONITOR;
3514 sink->mon = mon;
3515 sink->previous = qemu_error_sink;
3516 qemu_error_sink = sink;
3517}
3518
3519void qemu_errors_to_previous(void)
3520{
3521 QemuErrorSink *sink;
3522
3523 assert(qemu_error_sink != NULL);
3524 sink = qemu_error_sink;
3525 qemu_error_sink = sink->previous;
3526 qemu_free(sink);
3527}
3528
3529void qemu_error(const char *fmt, ...)
3530{
3531 va_list args;
3532
3533 assert(qemu_error_sink != NULL);
3534 switch (qemu_error_sink->dest) {
3535 case ERR_SINK_FILE:
3536 va_start(args, fmt);
3537 vfprintf(qemu_error_sink->fp, fmt, args);
3538 va_end(args);
3539 break;
3540 case ERR_SINK_MONITOR:
3541 va_start(args, fmt);
3542 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3543 va_end(args);
3544 break;
3545 }
3546}