blob: cc1b501cfe7e44353121bd37e602504767066256 [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
bellard9dc39cb2004-03-14 21:38:27 +0000226static int compare_cmd(const char *name, const char *list)
227{
228 const char *p, *pstart;
229 int len;
230 len = strlen(name);
231 p = list;
232 for(;;) {
233 pstart = p;
234 p = strchr(p, '|');
235 if (!p)
236 p = pstart + strlen(pstart);
237 if ((p - pstart) == len && !memcmp(pstart, name, len))
238 return 1;
239 if (*p == '\0')
240 break;
241 p++;
242 }
243 return 0;
244}
245
Anthony Liguoric227f092009-10-01 16:12:16 -0500246static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000247 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000248{
Anthony Liguoric227f092009-10-01 16:12:16 -0500249 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000250
251 for(cmd = cmds; cmd->name != NULL; cmd++) {
252 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000253 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
254 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000255 }
256}
257
aliguori376253e2009-03-05 23:01:23 +0000258static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000259{
260 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000261 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000262 } else {
aliguori376253e2009-03-05 23:01:23 +0000263 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000264 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000265 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000266 monitor_printf(mon, "Log items (comma separated):\n");
267 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000268 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000269 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000270 }
271 }
bellard9dc39cb2004-03-14 21:38:27 +0000272 }
273}
274
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300275static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300276{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300277 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300278}
279
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300280static void do_commit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000281{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200282 int all_devices;
283 DriveInfo *dinfo;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300284 const char *device = qdict_get_str(qdict, "device");
balrog2dc7b602007-05-24 18:53:22 +0000285
bellard7954c732006-08-01 15:52:40 +0000286 all_devices = !strcmp(device, "all");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000287 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200288 if (!all_devices)
Luiz Capitulino73006d22009-07-31 15:15:41 -0300289 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200290 continue;
291 bdrv_commit(dinfo->bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000292 }
293}
294
Luiz Capitulino13c74252009-10-07 13:41:55 -0300295static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000296{
Anthony Liguoric227f092009-10-01 16:12:16 -0500297 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300298 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000299
bellard9307c4c2004-04-04 12:57:25 +0000300 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000301 goto help;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300302
303 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000304 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300305 break;
bellard9dc39cb2004-03-14 21:38:27 +0000306 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300307
308 if (cmd->name == NULL)
309 goto help;
310
311 if (monitor_handler_ported(cmd)) {
312 cmd->mhandler.info_new(mon, ret_data);
313 if (*ret_data)
314 cmd->user_print(mon, *ret_data);
315 } else {
316 cmd->mhandler.info(mon);
317 }
318
bellard9dc39cb2004-03-14 21:38:27 +0000319 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300320
321help:
322 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000323}
324
aliguori376253e2009-03-05 23:01:23 +0000325static void do_info_version(Monitor *mon)
bellard9bc9d1c2004-10-10 15:15:51 +0000326{
pbrook4a19f1e2009-04-07 23:17:49 +0000327 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000328}
329
aliguori376253e2009-03-05 23:01:23 +0000330static void do_info_name(Monitor *mon)
thsc35734b2007-03-19 15:17:08 +0000331{
332 if (qemu_name)
aliguori376253e2009-03-05 23:01:23 +0000333 monitor_printf(mon, "%s\n", qemu_name);
thsc35734b2007-03-19 15:17:08 +0000334}
335
aurel32bf4f74c2008-12-18 22:42:34 +0000336#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000337static void do_info_hpet(Monitor *mon)
aliguori16b29ae2008-12-17 23:28:44 +0000338{
aliguori376253e2009-03-05 23:01:23 +0000339 monitor_printf(mon, "HPET is %s by QEMU\n",
340 (no_hpet) ? "disabled" : "enabled");
aliguori16b29ae2008-12-17 23:28:44 +0000341}
aurel32bf4f74c2008-12-18 22:42:34 +0000342#endif
aliguori16b29ae2008-12-17 23:28:44 +0000343
aliguori376253e2009-03-05 23:01:23 +0000344static void do_info_uuid(Monitor *mon)
blueswir1f1f23ad2008-09-18 18:30:20 +0000345{
aliguori376253e2009-03-05 23:01:23 +0000346 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
347 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
348 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
349 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
350 qemu_uuid[14], qemu_uuid[15]);
thsa36e69d2007-12-02 05:18:19 +0000351}
352
bellard6a00d602005-11-21 23:25:50 +0000353/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000354static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000355{
356 CPUState *env;
357
358 for(env = first_cpu; env != NULL; env = env->next_cpu) {
359 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000360 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000361 return 0;
362 }
363 }
364 return -1;
365}
366
pbrook9596ebb2007-11-18 01:44:38 +0000367static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000368{
aliguori731b0362009-03-05 23:01:42 +0000369 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000370 mon_set_cpu(0);
371 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300372 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000373 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000374}
375
aliguori376253e2009-03-05 23:01:23 +0000376static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000377{
bellard6a00d602005-11-21 23:25:50 +0000378 CPUState *env;
379 env = mon_get_cpu();
380 if (!env)
381 return;
bellard9307c4c2004-04-04 12:57:25 +0000382#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000383 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000384 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000385#else
aliguori376253e2009-03-05 23:01:23 +0000386 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000387 0);
bellard9307c4c2004-04-04 12:57:25 +0000388#endif
389}
390
aliguori376253e2009-03-05 23:01:23 +0000391static void do_info_cpus(Monitor *mon)
bellard6a00d602005-11-21 23:25:50 +0000392{
393 CPUState *env;
394
395 /* just to set the default cpu if not already done */
396 mon_get_cpu();
397
398 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Avi Kivity4c0960c2009-08-17 23:19:53 +0300399 cpu_synchronize_state(env);
aliguori376253e2009-03-05 23:01:23 +0000400 monitor_printf(mon, "%c CPU #%d:",
aliguori731b0362009-03-05 23:01:42 +0000401 (env == mon->mon_cpu) ? '*' : ' ',
aliguori376253e2009-03-05 23:01:23 +0000402 env->cpu_index);
bellard6a00d602005-11-21 23:25:50 +0000403#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +0000404 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
405 env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000406#elif defined(TARGET_PPC)
aliguori376253e2009-03-05 23:01:23 +0000407 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000408#elif defined(TARGET_SPARC)
aliguori376253e2009-03-05 23:01:23 +0000409 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
410 env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000411#elif defined(TARGET_MIPS)
aliguori376253e2009-03-05 23:01:23 +0000412 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000413#endif
thsead93602007-09-06 00:18:15 +0000414 if (env->halted)
aliguori376253e2009-03-05 23:01:23 +0000415 monitor_printf(mon, " (halted)");
416 monitor_printf(mon, "\n");
bellard6a00d602005-11-21 23:25:50 +0000417 }
418}
419
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300420static void do_cpu_set(Monitor *mon, const QDict *qdict)
bellard6a00d602005-11-21 23:25:50 +0000421{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300422 int index = qdict_get_int(qdict, "index");
bellard6a00d602005-11-21 23:25:50 +0000423 if (mon_set_cpu(index) < 0)
aliguori376253e2009-03-05 23:01:23 +0000424 monitor_printf(mon, "Invalid CPU index\n");
bellard6a00d602005-11-21 23:25:50 +0000425}
426
aliguori376253e2009-03-05 23:01:23 +0000427static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000428{
aliguori376253e2009-03-05 23:01:23 +0000429 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000430}
431
aliguori376253e2009-03-05 23:01:23 +0000432static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000433{
434 int i;
bellard7e2515e2004-08-01 21:52:19 +0000435 const char *str;
ths3b46e622007-09-17 08:09:54 +0000436
aliguoricde76ee2009-03-05 23:01:51 +0000437 if (!mon->rs)
438 return;
bellard7e2515e2004-08-01 21:52:19 +0000439 i = 0;
440 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000441 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000442 if (!str)
443 break;
aliguori376253e2009-03-05 23:01:23 +0000444 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000445 i++;
bellardaa455482004-04-04 13:07:25 +0000446 }
447}
448
j_mayer76a66252007-03-07 08:32:30 +0000449#if defined(TARGET_PPC)
450/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000451static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000452{
453 CPUState *env;
454
455 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000456 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000457}
458#endif
459
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300460static void do_quit(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000461{
462 exit(0);
463}
464
aliguori376253e2009-03-05 23:01:23 +0000465static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000466{
467 if (bdrv_is_inserted(bs)) {
468 if (!force) {
469 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000470 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000471 return -1;
472 }
473 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000474 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000475 return -1;
476 }
477 }
478 bdrv_close(bs);
479 }
480 return 0;
481}
482
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300483static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000484{
485 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300486 int force = qdict_get_int(qdict, "force");
487 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000488
bellard9307c4c2004-04-04 12:57:25 +0000489 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000490 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000491 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000492 return;
493 }
aliguori376253e2009-03-05 23:01:23 +0000494 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000495}
496
aliguori376253e2009-03-05 23:01:23 +0000497static void do_change_block(Monitor *mon, const char *device,
498 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000499{
500 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000501 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000502
bellard9307c4c2004-04-04 12:57:25 +0000503 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000504 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000505 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000506 return;
507 }
aurel322ecea9b2008-06-18 22:10:01 +0000508 if (fmt) {
509 drv = bdrv_find_format(fmt);
510 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000511 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000512 return;
513 }
514 }
aliguori376253e2009-03-05 23:01:23 +0000515 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000516 return;
aurel322ecea9b2008-06-18 22:10:01 +0000517 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000518 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000519}
520
aliguori376253e2009-03-05 23:01:23 +0000521static void change_vnc_password_cb(Monitor *mon, const char *password,
522 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000523{
524 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000525 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000526
aliguori731b0362009-03-05 23:01:42 +0000527 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000528}
529
aliguori376253e2009-03-05 23:01:23 +0000530static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000531{
ths70848512007-08-25 01:37:05 +0000532 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000533 strcmp(target, "password") == 0) {
534 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000535 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000536 strncpy(password, arg, sizeof(password));
537 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000538 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000539 } else {
aliguori376253e2009-03-05 23:01:23 +0000540 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000541 }
ths70848512007-08-25 01:37:05 +0000542 } else {
aliguori28a76be2009-03-06 20:27:40 +0000543 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000544 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000545 }
thse25a5822007-08-25 01:36:20 +0000546}
547
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300548static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000549{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300550 const char *device = qdict_get_str(qdict, "device");
551 const char *target = qdict_get_str(qdict, "target");
552 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000553 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000554 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000555 } else {
aliguori28a76be2009-03-06 20:27:40 +0000556 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000557 }
558}
559
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300560static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000561{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300562 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000563}
564
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300565static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000566{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300567 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000568}
569
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300570static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000571{
572 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300573 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000574
bellard9307c4c2004-04-04 12:57:25 +0000575 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000576 mask = 0;
577 } else {
bellard9307c4c2004-04-04 12:57:25 +0000578 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000579 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000580 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000581 return;
582 }
583 }
584 cpu_set_log(mask);
585}
586
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300587static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000588{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300589 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000590 if (!option || !strcmp(option, "on")) {
591 singlestep = 1;
592 } else if (!strcmp(option, "off")) {
593 singlestep = 0;
594 } else {
595 monitor_printf(mon, "unexpected option %s\n", option);
596 }
597}
598
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300599static void do_stop(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000600{
601 vm_stop(EXCP_INTERRUPT);
602}
603
aliguoribb5fc202009-03-05 23:01:15 +0000604static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000605
aliguori376253e2009-03-05 23:01:23 +0000606struct bdrv_iterate_context {
607 Monitor *mon;
608 int err;
609};
aliguoric0f4ce72009-03-05 23:01:01 +0000610
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300611static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000612{
613 struct bdrv_iterate_context context = { mon, 0 };
614
615 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000616 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000617 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000618 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000619}
620
aliguoribb5fc202009-03-05 23:01:15 +0000621static void bdrv_key_cb(void *opaque, int err)
622{
aliguori376253e2009-03-05 23:01:23 +0000623 Monitor *mon = opaque;
624
aliguoribb5fc202009-03-05 23:01:15 +0000625 /* another key was set successfully, retry to continue */
626 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300627 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000628}
629
630static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
631{
aliguori376253e2009-03-05 23:01:23 +0000632 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000633
aliguori376253e2009-03-05 23:01:23 +0000634 if (!context->err && bdrv_key_required(bs)) {
635 context->err = -EBUSY;
636 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
637 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000638 }
639}
640
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300641static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000642{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300643 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000644 if (!device)
645 device = "tcp::" DEFAULT_GDBSTUB_PORT;
646 if (gdbserver_start(device) < 0) {
647 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
648 device);
649 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000650 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000651 } else {
aliguori59030a82009-04-05 18:43:41 +0000652 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
653 device);
bellard8a7ddc32004-03-31 19:00:16 +0000654 }
655}
656
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300657static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100658{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300659 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100660 if (select_watchdog_action(action) == -1) {
661 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
662 }
663}
664
aliguori376253e2009-03-05 23:01:23 +0000665static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000666{
aliguori376253e2009-03-05 23:01:23 +0000667 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000668 switch(c) {
669 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000670 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000671 break;
672 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000673 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000674 break;
675 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000676 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000677 break;
678 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000679 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000680 break;
681 default:
682 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000683 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000684 } else {
aliguori376253e2009-03-05 23:01:23 +0000685 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000686 }
687 break;
688 }
aliguori376253e2009-03-05 23:01:23 +0000689 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000690}
691
aliguori376253e2009-03-05 23:01:23 +0000692static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500693 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000694{
bellard6a00d602005-11-21 23:25:50 +0000695 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000696 int nb_per_line, l, line_size, i, max_digits, len;
697 uint8_t buf[16];
698 uint64_t v;
699
700 if (format == 'i') {
701 int flags;
702 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000703 env = mon_get_cpu();
704 if (!env && !is_physical)
705 return;
bellard9307c4c2004-04-04 12:57:25 +0000706#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000707 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000708 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000709 } else if (wsize == 4) {
710 flags = 0;
711 } else {
bellard6a15fd12006-04-12 21:07:07 +0000712 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000713 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000714 if (env) {
715#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000716 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000717 (env->segs[R_CS].flags & DESC_L_MASK))
718 flags = 2;
719 else
720#endif
721 if (!(env->segs[R_CS].flags & DESC_B_MASK))
722 flags = 1;
723 }
bellard4c27ba22004-04-25 18:05:08 +0000724 }
725#endif
aliguori376253e2009-03-05 23:01:23 +0000726 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000727 return;
728 }
729
730 len = wsize * count;
731 if (wsize == 1)
732 line_size = 8;
733 else
734 line_size = 16;
735 nb_per_line = line_size / wsize;
736 max_digits = 0;
737
738 switch(format) {
739 case 'o':
740 max_digits = (wsize * 8 + 2) / 3;
741 break;
742 default:
743 case 'x':
744 max_digits = (wsize * 8) / 4;
745 break;
746 case 'u':
747 case 'd':
748 max_digits = (wsize * 8 * 10 + 32) / 33;
749 break;
750 case 'c':
751 wsize = 1;
752 break;
753 }
754
755 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000756 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000757 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000758 else
aliguori376253e2009-03-05 23:01:23 +0000759 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000760 l = len;
761 if (l > line_size)
762 l = line_size;
763 if (is_physical) {
764 cpu_physical_memory_rw(addr, buf, l, 0);
765 } else {
bellard6a00d602005-11-21 23:25:50 +0000766 env = mon_get_cpu();
767 if (!env)
768 break;
aliguoric8f79b62008-08-18 14:00:20 +0000769 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000770 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000771 break;
772 }
bellard9307c4c2004-04-04 12:57:25 +0000773 }
ths5fafdf22007-09-16 21:08:06 +0000774 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000775 while (i < l) {
776 switch(wsize) {
777 default:
778 case 1:
779 v = ldub_raw(buf + i);
780 break;
781 case 2:
782 v = lduw_raw(buf + i);
783 break;
784 case 4:
bellard92a31b12005-02-10 22:00:52 +0000785 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000786 break;
787 case 8:
788 v = ldq_raw(buf + i);
789 break;
790 }
aliguori376253e2009-03-05 23:01:23 +0000791 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000792 switch(format) {
793 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000794 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000795 break;
796 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000797 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000798 break;
799 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000800 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000801 break;
802 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000803 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000804 break;
805 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000806 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000807 break;
808 }
809 i += wsize;
810 }
aliguori376253e2009-03-05 23:01:23 +0000811 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000812 addr += l;
813 len -= l;
814 }
815}
816
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300817static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000818{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300819 int count = qdict_get_int(qdict, "count");
820 int format = qdict_get_int(qdict, "format");
821 int size = qdict_get_int(qdict, "size");
822 target_long addr = qdict_get_int(qdict, "addr");
823
aliguori376253e2009-03-05 23:01:23 +0000824 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000825}
826
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300827static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000828{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300829 int count = qdict_get_int(qdict, "count");
830 int format = qdict_get_int(qdict, "format");
831 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500832 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300833
aliguori376253e2009-03-05 23:01:23 +0000834 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000835}
836
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300837static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000838{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300839 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500840 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300841
blueswir17743e582007-09-24 18:39:04 +0000842#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000843 switch(format) {
844 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000845 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000846 break;
847 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000848 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000849 break;
850 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000851 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000852 break;
853 default:
854 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000855 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000856 break;
857 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000858 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000859 break;
860 }
bellard92a31b12005-02-10 22:00:52 +0000861#else
862 switch(format) {
863 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000864 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000865 break;
866 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000867 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000868 break;
869 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000870 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000871 break;
872 default:
873 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000874 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000875 break;
876 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000877 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000878 break;
879 }
880#endif
aliguori376253e2009-03-05 23:01:23 +0000881 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000882}
883
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300884static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000885{
886 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300887 uint32_t size = qdict_get_int(qdict, "size");
888 const char *filename = qdict_get_str(qdict, "filename");
889 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000890 uint32_t l;
891 CPUState *env;
892 uint8_t buf[1024];
893
894 env = mon_get_cpu();
895 if (!env)
896 return;
897
898 f = fopen(filename, "wb");
899 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000900 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000901 return;
902 }
903 while (size != 0) {
904 l = sizeof(buf);
905 if (l > size)
906 l = size;
907 cpu_memory_rw_debug(env, addr, buf, l, 0);
908 fwrite(buf, 1, l, f);
909 addr += l;
910 size -= l;
911 }
912 fclose(f);
913}
914
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300915static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000916{
917 FILE *f;
918 uint32_t l;
919 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300920 uint32_t size = qdict_get_int(qdict, "size");
921 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -0500922 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000923
924 f = fopen(filename, "wb");
925 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000926 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000927 return;
928 }
929 while (size != 0) {
930 l = sizeof(buf);
931 if (l > size)
932 l = size;
933 cpu_physical_memory_rw(addr, buf, l, 0);
934 fwrite(buf, 1, l, f);
935 fflush(f);
936 addr += l;
937 size -= l;
938 }
939 fclose(f);
940}
941
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300942static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000943{
944 uint32_t addr;
945 uint8_t buf[1];
946 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300947 uint32_t start = qdict_get_int(qdict, "start");
948 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000949
950 sum = 0;
951 for(addr = start; addr < (start + size); addr++) {
952 cpu_physical_memory_rw(addr, buf, 1, 0);
953 /* BSD sum algorithm ('sum' Unix command) */
954 sum = (sum >> 1) | (sum << 15);
955 sum += buf[0];
956 }
aliguori376253e2009-03-05 23:01:23 +0000957 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000958}
959
bellarda3a91a32004-06-04 11:06:21 +0000960typedef struct {
961 int keycode;
962 const char *name;
963} KeyDef;
964
965static const KeyDef key_defs[] = {
966 { 0x2a, "shift" },
967 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000968
bellarda3a91a32004-06-04 11:06:21 +0000969 { 0x38, "alt" },
970 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000971 { 0x64, "altgr" },
972 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000973 { 0x1d, "ctrl" },
974 { 0x9d, "ctrl_r" },
975
976 { 0xdd, "menu" },
977
978 { 0x01, "esc" },
979
980 { 0x02, "1" },
981 { 0x03, "2" },
982 { 0x04, "3" },
983 { 0x05, "4" },
984 { 0x06, "5" },
985 { 0x07, "6" },
986 { 0x08, "7" },
987 { 0x09, "8" },
988 { 0x0a, "9" },
989 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000990 { 0x0c, "minus" },
991 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000992 { 0x0e, "backspace" },
993
994 { 0x0f, "tab" },
995 { 0x10, "q" },
996 { 0x11, "w" },
997 { 0x12, "e" },
998 { 0x13, "r" },
999 { 0x14, "t" },
1000 { 0x15, "y" },
1001 { 0x16, "u" },
1002 { 0x17, "i" },
1003 { 0x18, "o" },
1004 { 0x19, "p" },
1005
1006 { 0x1c, "ret" },
1007
1008 { 0x1e, "a" },
1009 { 0x1f, "s" },
1010 { 0x20, "d" },
1011 { 0x21, "f" },
1012 { 0x22, "g" },
1013 { 0x23, "h" },
1014 { 0x24, "j" },
1015 { 0x25, "k" },
1016 { 0x26, "l" },
1017
1018 { 0x2c, "z" },
1019 { 0x2d, "x" },
1020 { 0x2e, "c" },
1021 { 0x2f, "v" },
1022 { 0x30, "b" },
1023 { 0x31, "n" },
1024 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001025 { 0x33, "comma" },
1026 { 0x34, "dot" },
1027 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001028
balrog4d3b6f62008-02-10 16:33:14 +00001029 { 0x37, "asterisk" },
1030
bellarda3a91a32004-06-04 11:06:21 +00001031 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001032 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001033 { 0x3b, "f1" },
1034 { 0x3c, "f2" },
1035 { 0x3d, "f3" },
1036 { 0x3e, "f4" },
1037 { 0x3f, "f5" },
1038 { 0x40, "f6" },
1039 { 0x41, "f7" },
1040 { 0x42, "f8" },
1041 { 0x43, "f9" },
1042 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001043 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001044 { 0x46, "scroll_lock" },
1045
bellard64866c32006-05-07 18:03:31 +00001046 { 0xb5, "kp_divide" },
1047 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001048 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001049 { 0x4e, "kp_add" },
1050 { 0x9c, "kp_enter" },
1051 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001052 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001053
1054 { 0x52, "kp_0" },
1055 { 0x4f, "kp_1" },
1056 { 0x50, "kp_2" },
1057 { 0x51, "kp_3" },
1058 { 0x4b, "kp_4" },
1059 { 0x4c, "kp_5" },
1060 { 0x4d, "kp_6" },
1061 { 0x47, "kp_7" },
1062 { 0x48, "kp_8" },
1063 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001064
bellarda3a91a32004-06-04 11:06:21 +00001065 { 0x56, "<" },
1066
1067 { 0x57, "f11" },
1068 { 0x58, "f12" },
1069
1070 { 0xb7, "print" },
1071
1072 { 0xc7, "home" },
1073 { 0xc9, "pgup" },
1074 { 0xd1, "pgdn" },
1075 { 0xcf, "end" },
1076
1077 { 0xcb, "left" },
1078 { 0xc8, "up" },
1079 { 0xd0, "down" },
1080 { 0xcd, "right" },
1081
1082 { 0xd2, "insert" },
1083 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001084#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1085 { 0xf0, "stop" },
1086 { 0xf1, "again" },
1087 { 0xf2, "props" },
1088 { 0xf3, "undo" },
1089 { 0xf4, "front" },
1090 { 0xf5, "copy" },
1091 { 0xf6, "open" },
1092 { 0xf7, "paste" },
1093 { 0xf8, "find" },
1094 { 0xf9, "cut" },
1095 { 0xfa, "lf" },
1096 { 0xfb, "help" },
1097 { 0xfc, "meta_l" },
1098 { 0xfd, "meta_r" },
1099 { 0xfe, "compose" },
1100#endif
bellarda3a91a32004-06-04 11:06:21 +00001101 { 0, NULL },
1102};
1103
1104static int get_keycode(const char *key)
1105{
1106 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001107 char *endp;
1108 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001109
1110 for(p = key_defs; p->name != NULL; p++) {
1111 if (!strcmp(key, p->name))
1112 return p->keycode;
1113 }
bellard64866c32006-05-07 18:03:31 +00001114 if (strstart(key, "0x", NULL)) {
1115 ret = strtoul(key, &endp, 0);
1116 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1117 return ret;
1118 }
bellarda3a91a32004-06-04 11:06:21 +00001119 return -1;
1120}
1121
balrogc8256f92008-06-08 22:45:01 +00001122#define MAX_KEYCODES 16
1123static uint8_t keycodes[MAX_KEYCODES];
1124static int nb_pending_keycodes;
1125static QEMUTimer *key_timer;
1126
1127static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001128{
balrogc8256f92008-06-08 22:45:01 +00001129 int keycode;
1130
1131 while (nb_pending_keycodes > 0) {
1132 nb_pending_keycodes--;
1133 keycode = keycodes[nb_pending_keycodes];
1134 if (keycode & 0x80)
1135 kbd_put_keycode(0xe0);
1136 kbd_put_keycode(keycode | 0x80);
1137 }
1138}
1139
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001140static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001141{
balrog3401c0d2008-06-04 10:05:59 +00001142 char keyname_buf[16];
1143 char *separator;
1144 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001145 const char *string = qdict_get_str(qdict, "string");
1146 int has_hold_time = qdict_haskey(qdict, "hold_time");
1147 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001148
balrogc8256f92008-06-08 22:45:01 +00001149 if (nb_pending_keycodes > 0) {
1150 qemu_del_timer(key_timer);
1151 release_keys(NULL);
1152 }
1153 if (!has_hold_time)
1154 hold_time = 100;
1155 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001156 while (1) {
1157 separator = strchr(string, '-');
1158 keyname_len = separator ? separator - string : strlen(string);
1159 if (keyname_len > 0) {
1160 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1161 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001162 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001163 return;
bellarda3a91a32004-06-04 11:06:21 +00001164 }
balrogc8256f92008-06-08 22:45:01 +00001165 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001166 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001167 return;
1168 }
1169 keyname_buf[keyname_len] = 0;
1170 keycode = get_keycode(keyname_buf);
1171 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001173 return;
1174 }
balrogc8256f92008-06-08 22:45:01 +00001175 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001176 }
balrog3401c0d2008-06-04 10:05:59 +00001177 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001178 break;
balrog3401c0d2008-06-04 10:05:59 +00001179 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001180 }
balrogc8256f92008-06-08 22:45:01 +00001181 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001182 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001183 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001184 keycode = keycodes[i];
1185 if (keycode & 0x80)
1186 kbd_put_keycode(0xe0);
1187 kbd_put_keycode(keycode & 0x7f);
1188 }
balrogc8256f92008-06-08 22:45:01 +00001189 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001190 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001191 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001192}
1193
bellard13224a82006-07-14 22:03:35 +00001194static int mouse_button_state;
1195
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001196static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001197{
1198 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001199 const char *dx_str = qdict_get_str(qdict, "dx_str");
1200 const char *dy_str = qdict_get_str(qdict, "dy_str");
1201 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001202 dx = strtol(dx_str, NULL, 0);
1203 dy = strtol(dy_str, NULL, 0);
1204 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001205 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001206 dz = strtol(dz_str, NULL, 0);
1207 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1208}
1209
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001210static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001211{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001212 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001213 mouse_button_state = button_state;
1214 kbd_mouse_event(0, 0, 0, mouse_button_state);
1215}
1216
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001217static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001218{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001219 int size = qdict_get_int(qdict, "size");
1220 int addr = qdict_get_int(qdict, "addr");
1221 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001222 uint32_t val;
1223 int suffix;
1224
1225 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001226 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001227 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001228 addr++;
1229 }
1230 addr &= 0xffff;
1231
1232 switch(size) {
1233 default:
1234 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001235 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001236 suffix = 'b';
1237 break;
1238 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001239 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001240 suffix = 'w';
1241 break;
1242 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001243 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001244 suffix = 'l';
1245 break;
1246 }
aliguori376253e2009-03-05 23:01:23 +00001247 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1248 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001249}
bellarda3a91a32004-06-04 11:06:21 +00001250
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001251static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001252{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001253 int size = qdict_get_int(qdict, "size");
1254 int addr = qdict_get_int(qdict, "addr");
1255 int val = qdict_get_int(qdict, "val");
1256
Jan Kiszkaf1147842009-07-14 10:20:11 +02001257 addr &= IOPORTS_MASK;
1258
1259 switch (size) {
1260 default:
1261 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001262 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001263 break;
1264 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001265 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001266 break;
1267 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001268 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001269 break;
1270 }
1271}
1272
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001273static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001274{
1275 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001276 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001277
Jan Kiszka76e30d02009-07-02 00:19:02 +02001278 res = qemu_boot_set(bootdevice);
1279 if (res == 0) {
1280 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1281 } else if (res > 0) {
1282 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001283 } else {
aliguori376253e2009-03-05 23:01:23 +00001284 monitor_printf(mon, "no function defined to set boot device list for "
1285 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001286 }
1287}
1288
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001289static void do_system_reset(Monitor *mon, const QDict *qdict)
bellarde4f90822004-06-20 12:35:44 +00001290{
1291 qemu_system_reset_request();
1292}
1293
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -03001294static void do_system_powerdown(Monitor *mon, const QDict *qdict)
bellard34751872005-07-02 14:31:34 +00001295{
1296 qemu_system_powerdown_request();
1297}
1298
bellardb86bda52004-09-18 19:32:46 +00001299#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001300static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001301{
aliguori376253e2009-03-05 23:01:23 +00001302 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1303 addr,
1304 pte & mask,
1305 pte & PG_GLOBAL_MASK ? 'G' : '-',
1306 pte & PG_PSE_MASK ? 'P' : '-',
1307 pte & PG_DIRTY_MASK ? 'D' : '-',
1308 pte & PG_ACCESSED_MASK ? 'A' : '-',
1309 pte & PG_PCD_MASK ? 'C' : '-',
1310 pte & PG_PWT_MASK ? 'T' : '-',
1311 pte & PG_USER_MASK ? 'U' : '-',
1312 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001313}
1314
aliguori376253e2009-03-05 23:01:23 +00001315static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001316{
bellard6a00d602005-11-21 23:25:50 +00001317 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001318 int l1, l2;
1319 uint32_t pgd, pde, pte;
1320
bellard6a00d602005-11-21 23:25:50 +00001321 env = mon_get_cpu();
1322 if (!env)
1323 return;
1324
bellardb86bda52004-09-18 19:32:46 +00001325 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001326 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001327 return;
1328 }
1329 pgd = env->cr[3] & ~0xfff;
1330 for(l1 = 0; l1 < 1024; l1++) {
1331 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1332 pde = le32_to_cpu(pde);
1333 if (pde & PG_PRESENT_MASK) {
1334 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001335 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001336 } else {
1337 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001338 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001339 (uint8_t *)&pte, 4);
1340 pte = le32_to_cpu(pte);
1341 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001342 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001343 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001344 ~0xfff);
1345 }
1346 }
1347 }
1348 }
1349 }
1350}
1351
aliguori376253e2009-03-05 23:01:23 +00001352static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001353 uint32_t end, int prot)
1354{
bellard9746b152004-11-11 18:30:24 +00001355 int prot1;
1356 prot1 = *plast_prot;
1357 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001358 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001359 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1360 *pstart, end, end - *pstart,
1361 prot1 & PG_USER_MASK ? 'u' : '-',
1362 'r',
1363 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001364 }
1365 if (prot != 0)
1366 *pstart = end;
1367 else
1368 *pstart = -1;
1369 *plast_prot = prot;
1370 }
1371}
1372
aliguori376253e2009-03-05 23:01:23 +00001373static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001374{
bellard6a00d602005-11-21 23:25:50 +00001375 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001376 int l1, l2, prot, last_prot;
1377 uint32_t pgd, pde, pte, start, end;
1378
bellard6a00d602005-11-21 23:25:50 +00001379 env = mon_get_cpu();
1380 if (!env)
1381 return;
1382
bellardb86bda52004-09-18 19:32:46 +00001383 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001385 return;
1386 }
1387 pgd = env->cr[3] & ~0xfff;
1388 last_prot = 0;
1389 start = -1;
1390 for(l1 = 0; l1 < 1024; l1++) {
1391 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1392 pde = le32_to_cpu(pde);
1393 end = l1 << 22;
1394 if (pde & PG_PRESENT_MASK) {
1395 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1396 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001397 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001398 } else {
1399 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001400 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001401 (uint8_t *)&pte, 4);
1402 pte = le32_to_cpu(pte);
1403 end = (l1 << 22) + (l2 << 12);
1404 if (pte & PG_PRESENT_MASK) {
1405 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1406 } else {
1407 prot = 0;
1408 }
aliguori376253e2009-03-05 23:01:23 +00001409 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001410 }
1411 }
1412 } else {
1413 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001414 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001415 }
1416 }
1417}
1418#endif
1419
aurel327c664e22009-03-03 06:12:22 +00001420#if defined(TARGET_SH4)
1421
aliguori376253e2009-03-05 23:01:23 +00001422static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001423{
aliguori376253e2009-03-05 23:01:23 +00001424 monitor_printf(mon, " tlb%i:\t"
1425 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1426 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1427 "dirty=%hhu writethrough=%hhu\n",
1428 idx,
1429 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1430 tlb->v, tlb->sh, tlb->c, tlb->pr,
1431 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001432}
1433
aliguori376253e2009-03-05 23:01:23 +00001434static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001435{
1436 CPUState *env = mon_get_cpu();
1437 int i;
1438
aliguori376253e2009-03-05 23:01:23 +00001439 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001440 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001441 print_tlb (mon, i, &env->itlb[i]);
1442 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001443 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001444 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001445}
1446
1447#endif
1448
aliguori376253e2009-03-05 23:01:23 +00001449static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001450{
1451#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001452 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001453 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001454 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001455 else
aliguori376253e2009-03-05 23:01:23 +00001456 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001457#else
aliguori376253e2009-03-05 23:01:23 +00001458 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001459#endif
1460}
1461
aliguori030ea372009-04-21 22:30:47 +00001462static void do_info_numa(Monitor *mon)
1463{
aliguorib28b6232009-04-22 20:20:29 +00001464 int i;
aliguori030ea372009-04-21 22:30:47 +00001465 CPUState *env;
1466
1467 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1468 for (i = 0; i < nb_numa_nodes; i++) {
1469 monitor_printf(mon, "node %d cpus:", i);
1470 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1471 if (env->numa_node == i) {
1472 monitor_printf(mon, " %d", env->cpu_index);
1473 }
1474 }
1475 monitor_printf(mon, "\n");
1476 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1477 node_mem[i] >> 20);
1478 }
1479}
1480
bellard5f1ce942006-02-08 22:40:15 +00001481#ifdef CONFIG_PROFILER
1482
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001483int64_t qemu_time;
1484int64_t dev_time;
1485
aliguori376253e2009-03-05 23:01:23 +00001486static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001487{
1488 int64_t total;
1489 total = qemu_time;
1490 if (total == 0)
1491 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001492 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001493 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001494 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001495 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001496 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001497 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001498}
1499#else
aliguori376253e2009-03-05 23:01:23 +00001500static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001501{
aliguori376253e2009-03-05 23:01:23 +00001502 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001503}
1504#endif
1505
bellardec36b692006-07-16 18:57:03 +00001506/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001507static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001508
aliguori376253e2009-03-05 23:01:23 +00001509static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001510{
1511 int i;
1512 CaptureState *s;
1513
1514 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001515 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001516 s->ops.info (s->opaque);
1517 }
1518}
1519
Blue Swirl23130862009-06-06 08:22:04 +00001520#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001521static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001522{
1523 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001524 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001525 CaptureState *s;
1526
1527 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1528 if (i == n) {
1529 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001530 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001531 qemu_free (s);
1532 return;
1533 }
1534 }
1535}
1536
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001537static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001538{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001539 const char *path = qdict_get_str(qdict, "path");
1540 int has_freq = qdict_haskey(qdict, "freq");
1541 int freq = qdict_get_try_int(qdict, "freq", -1);
1542 int has_bits = qdict_haskey(qdict, "bits");
1543 int bits = qdict_get_try_int(qdict, "bits", -1);
1544 int has_channels = qdict_haskey(qdict, "nchannels");
1545 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001546 CaptureState *s;
1547
1548 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001549
1550 freq = has_freq ? freq : 44100;
1551 bits = has_bits ? bits : 16;
1552 nchannels = has_channels ? nchannels : 2;
1553
1554 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001555 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001556 qemu_free (s);
1557 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001558 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001559}
1560#endif
1561
aurel32dc1c0b72008-04-27 23:52:12 +00001562#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001563static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001564{
1565 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001566 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001567
1568 for (env = first_cpu; env != NULL; env = env->next_cpu)
1569 if (env->cpu_index == cpu_index) {
1570 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1571 break;
1572 }
1573}
1574#endif
1575
aliguori376253e2009-03-05 23:01:23 +00001576static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001577{
aurel321b530a62009-04-05 20:08:59 +00001578 if (vm_running) {
1579 if (singlestep) {
1580 monitor_printf(mon, "VM status: running (single step mode)\n");
1581 } else {
1582 monitor_printf(mon, "VM status: running\n");
1583 }
1584 } else
aliguori376253e2009-03-05 23:01:23 +00001585 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001586}
1587
1588
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001589static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001590{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001591 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001592 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001593 qemu_balloon(target << 20);
1594}
1595
aliguori376253e2009-03-05 23:01:23 +00001596static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001597{
Anthony Liguoric227f092009-10-01 16:12:16 -05001598 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001599
1600 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001601 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001602 monitor_printf(mon, "Using KVM without synchronous MMU, "
1603 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001604 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001605 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001606 else
aliguori376253e2009-03-05 23:01:23 +00001607 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001608}
1609
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001610static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001611{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001612 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001613
aliguori76655d62009-03-06 20:27:37 +00001614 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001615 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001616 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001617 return acl;
1618}
aliguori76655d62009-03-06 20:27:37 +00001619
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001620static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001621{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001622 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001623 qemu_acl *acl = find_acl(mon, aclname);
1624 qemu_acl_entry *entry;
1625 int i = 0;
1626
1627 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001628 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001629 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001630 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001631 i++;
1632 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001633 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001634 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001635 }
1636}
1637
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001638static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001639{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001640 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001641 qemu_acl *acl = find_acl(mon, aclname);
1642
1643 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001644 qemu_acl_reset(acl);
1645 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001646 }
1647}
aliguori76655d62009-03-06 20:27:37 +00001648
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001649static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001650{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001651 const char *aclname = qdict_get_str(qdict, "aclname");
1652 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653 qemu_acl *acl = find_acl(mon, aclname);
1654
1655 if (acl) {
1656 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001657 acl->defaultDeny = 0;
1658 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001659 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001660 acl->defaultDeny = 1;
1661 monitor_printf(mon, "acl: policy set to 'deny'\n");
1662 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001663 monitor_printf(mon, "acl: unknown policy '%s', "
1664 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001665 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001666 }
1667}
aliguori76655d62009-03-06 20:27:37 +00001668
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001669static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001670{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001671 const char *aclname = qdict_get_str(qdict, "aclname");
1672 const char *match = qdict_get_str(qdict, "match");
1673 const char *policy = qdict_get_str(qdict, "policy");
1674 int has_index = qdict_haskey(qdict, "index");
1675 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001676 qemu_acl *acl = find_acl(mon, aclname);
1677 int deny, ret;
1678
1679 if (acl) {
1680 if (strcmp(policy, "allow") == 0) {
1681 deny = 0;
1682 } else if (strcmp(policy, "deny") == 0) {
1683 deny = 1;
1684 } else {
1685 monitor_printf(mon, "acl: unknown policy '%s', "
1686 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001687 return;
1688 }
aliguori28a76be2009-03-06 20:27:40 +00001689 if (has_index)
1690 ret = qemu_acl_insert(acl, deny, match, index);
1691 else
1692 ret = qemu_acl_append(acl, deny, match);
1693 if (ret < 0)
1694 monitor_printf(mon, "acl: unable to add acl entry\n");
1695 else
1696 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001697 }
1698}
aliguori76655d62009-03-06 20:27:37 +00001699
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001700static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001701{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001702 const char *aclname = qdict_get_str(qdict, "aclname");
1703 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001704 qemu_acl *acl = find_acl(mon, aclname);
1705 int ret;
aliguori76655d62009-03-06 20:27:37 +00001706
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001707 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001708 ret = qemu_acl_remove(acl, match);
1709 if (ret < 0)
1710 monitor_printf(mon, "acl: no matching acl entry\n");
1711 else
1712 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001713 }
1714}
1715
Huang Ying79c4f6b2009-06-23 10:05:14 +08001716#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001717static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001718{
1719 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001720 int cpu_index = qdict_get_int(qdict, "cpu_index");
1721 int bank = qdict_get_int(qdict, "bank");
1722 uint64_t status = qdict_get_int(qdict, "status");
1723 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1724 uint64_t addr = qdict_get_int(qdict, "addr");
1725 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001726
1727 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1728 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1729 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1730 break;
1731 }
1732}
1733#endif
1734
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001735static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001736{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001737 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001738 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001739 int fd;
1740
1741 fd = qemu_chr_get_msgfd(mon->chr);
1742 if (fd == -1) {
1743 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1744 return;
1745 }
1746
1747 if (qemu_isdigit(fdname[0])) {
1748 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1749 return;
1750 }
1751
1752 fd = dup(fd);
1753 if (fd == -1) {
1754 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1755 strerror(errno));
1756 return;
1757 }
1758
Blue Swirl72cf2d42009-09-12 07:36:22 +00001759 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001760 if (strcmp(monfd->name, fdname) != 0) {
1761 continue;
1762 }
1763
1764 close(monfd->fd);
1765 monfd->fd = fd;
1766 return;
1767 }
1768
Anthony Liguoric227f092009-10-01 16:12:16 -05001769 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001770 monfd->name = qemu_strdup(fdname);
1771 monfd->fd = fd;
1772
Blue Swirl72cf2d42009-09-12 07:36:22 +00001773 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001774}
1775
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001776static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001777{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001778 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001779 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001780
Blue Swirl72cf2d42009-09-12 07:36:22 +00001781 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001782 if (strcmp(monfd->name, fdname) != 0) {
1783 continue;
1784 }
1785
Blue Swirl72cf2d42009-09-12 07:36:22 +00001786 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001787 close(monfd->fd);
1788 qemu_free(monfd->name);
1789 qemu_free(monfd);
1790 return;
1791 }
1792
1793 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1794 fdname);
1795}
1796
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001797static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001798{
1799 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001800 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001801
1802 vm_stop(0);
1803
Juan Quintela05f24012009-08-20 19:42:22 +02001804 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001805 vm_start();
1806}
1807
Mark McLoughlin7768e042009-07-22 09:11:41 +01001808int monitor_get_fd(Monitor *mon, const char *fdname)
1809{
Anthony Liguoric227f092009-10-01 16:12:16 -05001810 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001811
Blue Swirl72cf2d42009-09-12 07:36:22 +00001812 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001813 int fd;
1814
1815 if (strcmp(monfd->name, fdname) != 0) {
1816 continue;
1817 }
1818
1819 fd = monfd->fd;
1820
1821 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001822 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001823 qemu_free(monfd->name);
1824 qemu_free(monfd);
1825
1826 return fd;
1827 }
1828
1829 return -1;
1830}
1831
Anthony Liguoric227f092009-10-01 16:12:16 -05001832static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001833#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001834 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001835};
1836
Blue Swirl23130862009-06-06 08:22:04 +00001837/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001838static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001839 {
1840 .name = "version",
1841 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001842 .params = "",
1843 .help = "show the version of QEMU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001844 .mhandler.info = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001845 },
1846 {
1847 .name = "network",
1848 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001849 .params = "",
1850 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001851 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001852 },
1853 {
1854 .name = "chardev",
1855 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001856 .params = "",
1857 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001858 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001859 },
1860 {
1861 .name = "block",
1862 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001863 .params = "",
1864 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001865 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001866 },
1867 {
1868 .name = "blockstats",
1869 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001870 .params = "",
1871 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03001872 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001873 },
1874 {
1875 .name = "registers",
1876 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001877 .params = "",
1878 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03001879 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001880 },
1881 {
1882 .name = "cpus",
1883 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001884 .params = "",
1885 .help = "show infos for each CPU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001886 .mhandler.info = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001887 },
1888 {
1889 .name = "history",
1890 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001891 .params = "",
1892 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03001893 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001894 },
1895 {
1896 .name = "irq",
1897 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001898 .params = "",
1899 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03001900 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001901 },
1902 {
1903 .name = "pic",
1904 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001905 .params = "",
1906 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001907 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001908 },
1909 {
1910 .name = "pci",
1911 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001912 .params = "",
1913 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001914 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001915 },
aurel327c664e22009-03-03 06:12:22 +00001916#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001917 {
1918 .name = "tlb",
1919 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001920 .params = "",
1921 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001922 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001923 },
aurel327c664e22009-03-03 06:12:22 +00001924#endif
1925#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001926 {
1927 .name = "mem",
1928 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001929 .params = "",
1930 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001931 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001932 },
1933 {
1934 .name = "hpet",
1935 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001936 .params = "",
1937 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03001938 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001939 },
bellardb86bda52004-09-18 19:32:46 +00001940#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001941 {
1942 .name = "jit",
1943 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001944 .params = "",
1945 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001946 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001947 },
1948 {
1949 .name = "kvm",
1950 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001951 .params = "",
1952 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001953 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001954 },
1955 {
1956 .name = "numa",
1957 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001958 .params = "",
1959 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001960 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001961 },
1962 {
1963 .name = "usb",
1964 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001965 .params = "",
1966 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001967 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001968 },
1969 {
1970 .name = "usbhost",
1971 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001972 .params = "",
1973 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001974 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001975 },
1976 {
1977 .name = "profile",
1978 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001979 .params = "",
1980 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001981 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001982 },
1983 {
1984 .name = "capture",
1985 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001986 .params = "",
1987 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001988 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001989 },
1990 {
1991 .name = "snapshots",
1992 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001993 .params = "",
1994 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03001995 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001996 },
1997 {
1998 .name = "status",
1999 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002000 .params = "",
2001 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002002 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002003 },
2004 {
2005 .name = "pcmcia",
2006 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002007 .params = "",
2008 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002009 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002010 },
2011 {
2012 .name = "mice",
2013 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002014 .params = "",
2015 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002016 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002017 },
2018 {
2019 .name = "vnc",
2020 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002021 .params = "",
2022 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002023 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002024 },
2025 {
2026 .name = "name",
2027 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002028 .params = "",
2029 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002030 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002031 },
2032 {
2033 .name = "uuid",
2034 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002035 .params = "",
2036 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002037 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002038 },
j_mayer76a66252007-03-07 08:32:30 +00002039#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002040 {
2041 .name = "cpustats",
2042 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002043 .params = "",
2044 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002045 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002046 },
j_mayer76a66252007-03-07 08:32:30 +00002047#endif
blueswir131a60e22007-10-26 18:42:59 +00002048#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002049 {
2050 .name = "usernet",
2051 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002052 .params = "",
2053 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002054 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002055 },
blueswir131a60e22007-10-26 18:42:59 +00002056#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002057 {
2058 .name = "migrate",
2059 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002060 .params = "",
2061 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002062 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002063 },
2064 {
2065 .name = "balloon",
2066 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002067 .params = "",
2068 .help = "show balloon information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002069 .mhandler.info = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002070 },
2071 {
2072 .name = "qtree",
2073 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002074 .params = "",
2075 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002076 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002077 },
2078 {
2079 .name = "qdm",
2080 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002081 .params = "",
2082 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002083 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 },
2085 {
2086 .name = "roms",
2087 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002088 .params = "",
2089 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002090 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002091 },
2092 {
2093 .name = NULL,
2094 },
bellard9dc39cb2004-03-14 21:38:27 +00002095};
2096
bellard9307c4c2004-04-04 12:57:25 +00002097/*******************************************************************/
2098
2099static const char *pch;
2100static jmp_buf expr_env;
2101
bellard92a31b12005-02-10 22:00:52 +00002102#define MD_TLONG 0
2103#define MD_I32 1
2104
bellard9307c4c2004-04-04 12:57:25 +00002105typedef struct MonitorDef {
2106 const char *name;
2107 int offset;
blueswir18662d652008-10-02 18:32:44 +00002108 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002109 int type;
bellard9307c4c2004-04-04 12:57:25 +00002110} MonitorDef;
2111
bellard57206fd2004-04-25 18:54:52 +00002112#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002113static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002114{
bellard6a00d602005-11-21 23:25:50 +00002115 CPUState *env = mon_get_cpu();
2116 if (!env)
2117 return 0;
2118 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002119}
2120#endif
2121
bellarda541f292004-04-12 20:39:29 +00002122#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002123static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002124{
bellard6a00d602005-11-21 23:25:50 +00002125 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002126 unsigned int u;
2127 int i;
2128
bellard6a00d602005-11-21 23:25:50 +00002129 if (!env)
2130 return 0;
2131
bellarda541f292004-04-12 20:39:29 +00002132 u = 0;
2133 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002134 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002135
2136 return u;
2137}
2138
blueswir18662d652008-10-02 18:32:44 +00002139static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002140{
bellard6a00d602005-11-21 23:25:50 +00002141 CPUState *env = mon_get_cpu();
2142 if (!env)
2143 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002144 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002145}
2146
blueswir18662d652008-10-02 18:32:44 +00002147static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002148{
bellard6a00d602005-11-21 23:25:50 +00002149 CPUState *env = mon_get_cpu();
2150 if (!env)
2151 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002152 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002153}
bellard9fddaa02004-05-21 12:59:32 +00002154
blueswir18662d652008-10-02 18:32:44 +00002155static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002156{
bellard6a00d602005-11-21 23:25:50 +00002157 CPUState *env = mon_get_cpu();
2158 if (!env)
2159 return 0;
2160 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002161}
2162
blueswir18662d652008-10-02 18:32:44 +00002163static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002164{
bellard6a00d602005-11-21 23:25:50 +00002165 CPUState *env = mon_get_cpu();
2166 if (!env)
2167 return 0;
2168 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002169}
2170
blueswir18662d652008-10-02 18:32:44 +00002171static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002172{
bellard6a00d602005-11-21 23:25:50 +00002173 CPUState *env = mon_get_cpu();
2174 if (!env)
2175 return 0;
2176 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002177}
bellarda541f292004-04-12 20:39:29 +00002178#endif
2179
bellarde95c8d52004-09-30 22:22:08 +00002180#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002181#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002182static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002183{
bellard6a00d602005-11-21 23:25:50 +00002184 CPUState *env = mon_get_cpu();
2185 if (!env)
2186 return 0;
2187 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002188}
bellard7b936c02005-10-30 17:05:13 +00002189#endif
bellarde95c8d52004-09-30 22:22:08 +00002190
blueswir18662d652008-10-02 18:32:44 +00002191static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002192{
bellard6a00d602005-11-21 23:25:50 +00002193 CPUState *env = mon_get_cpu();
2194 if (!env)
2195 return 0;
2196 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002197}
2198#endif
2199
blueswir18662d652008-10-02 18:32:44 +00002200static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002201#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002202
2203#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002204 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002205 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002206 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002207
bellard9307c4c2004-04-04 12:57:25 +00002208 { "eax", offsetof(CPUState, regs[0]) },
2209 { "ecx", offsetof(CPUState, regs[1]) },
2210 { "edx", offsetof(CPUState, regs[2]) },
2211 { "ebx", offsetof(CPUState, regs[3]) },
2212 { "esp|sp", offsetof(CPUState, regs[4]) },
2213 { "ebp|fp", offsetof(CPUState, regs[5]) },
2214 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002215 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002216#ifdef TARGET_X86_64
2217 { "r8", offsetof(CPUState, regs[8]) },
2218 { "r9", offsetof(CPUState, regs[9]) },
2219 { "r10", offsetof(CPUState, regs[10]) },
2220 { "r11", offsetof(CPUState, regs[11]) },
2221 { "r12", offsetof(CPUState, regs[12]) },
2222 { "r13", offsetof(CPUState, regs[13]) },
2223 { "r14", offsetof(CPUState, regs[14]) },
2224 { "r15", offsetof(CPUState, regs[15]) },
2225#endif
bellard9307c4c2004-04-04 12:57:25 +00002226 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002227 { "eip", offsetof(CPUState, eip) },
2228 SEG("cs", R_CS)
2229 SEG("ds", R_DS)
2230 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002231 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002232 SEG("fs", R_FS)
2233 SEG("gs", R_GS)
2234 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002235#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002236 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002237 { "r0", offsetof(CPUState, gpr[0]) },
2238 { "r1", offsetof(CPUState, gpr[1]) },
2239 { "r2", offsetof(CPUState, gpr[2]) },
2240 { "r3", offsetof(CPUState, gpr[3]) },
2241 { "r4", offsetof(CPUState, gpr[4]) },
2242 { "r5", offsetof(CPUState, gpr[5]) },
2243 { "r6", offsetof(CPUState, gpr[6]) },
2244 { "r7", offsetof(CPUState, gpr[7]) },
2245 { "r8", offsetof(CPUState, gpr[8]) },
2246 { "r9", offsetof(CPUState, gpr[9]) },
2247 { "r10", offsetof(CPUState, gpr[10]) },
2248 { "r11", offsetof(CPUState, gpr[11]) },
2249 { "r12", offsetof(CPUState, gpr[12]) },
2250 { "r13", offsetof(CPUState, gpr[13]) },
2251 { "r14", offsetof(CPUState, gpr[14]) },
2252 { "r15", offsetof(CPUState, gpr[15]) },
2253 { "r16", offsetof(CPUState, gpr[16]) },
2254 { "r17", offsetof(CPUState, gpr[17]) },
2255 { "r18", offsetof(CPUState, gpr[18]) },
2256 { "r19", offsetof(CPUState, gpr[19]) },
2257 { "r20", offsetof(CPUState, gpr[20]) },
2258 { "r21", offsetof(CPUState, gpr[21]) },
2259 { "r22", offsetof(CPUState, gpr[22]) },
2260 { "r23", offsetof(CPUState, gpr[23]) },
2261 { "r24", offsetof(CPUState, gpr[24]) },
2262 { "r25", offsetof(CPUState, gpr[25]) },
2263 { "r26", offsetof(CPUState, gpr[26]) },
2264 { "r27", offsetof(CPUState, gpr[27]) },
2265 { "r28", offsetof(CPUState, gpr[28]) },
2266 { "r29", offsetof(CPUState, gpr[29]) },
2267 { "r30", offsetof(CPUState, gpr[30]) },
2268 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002269 /* Floating point registers */
2270 { "f0", offsetof(CPUState, fpr[0]) },
2271 { "f1", offsetof(CPUState, fpr[1]) },
2272 { "f2", offsetof(CPUState, fpr[2]) },
2273 { "f3", offsetof(CPUState, fpr[3]) },
2274 { "f4", offsetof(CPUState, fpr[4]) },
2275 { "f5", offsetof(CPUState, fpr[5]) },
2276 { "f6", offsetof(CPUState, fpr[6]) },
2277 { "f7", offsetof(CPUState, fpr[7]) },
2278 { "f8", offsetof(CPUState, fpr[8]) },
2279 { "f9", offsetof(CPUState, fpr[9]) },
2280 { "f10", offsetof(CPUState, fpr[10]) },
2281 { "f11", offsetof(CPUState, fpr[11]) },
2282 { "f12", offsetof(CPUState, fpr[12]) },
2283 { "f13", offsetof(CPUState, fpr[13]) },
2284 { "f14", offsetof(CPUState, fpr[14]) },
2285 { "f15", offsetof(CPUState, fpr[15]) },
2286 { "f16", offsetof(CPUState, fpr[16]) },
2287 { "f17", offsetof(CPUState, fpr[17]) },
2288 { "f18", offsetof(CPUState, fpr[18]) },
2289 { "f19", offsetof(CPUState, fpr[19]) },
2290 { "f20", offsetof(CPUState, fpr[20]) },
2291 { "f21", offsetof(CPUState, fpr[21]) },
2292 { "f22", offsetof(CPUState, fpr[22]) },
2293 { "f23", offsetof(CPUState, fpr[23]) },
2294 { "f24", offsetof(CPUState, fpr[24]) },
2295 { "f25", offsetof(CPUState, fpr[25]) },
2296 { "f26", offsetof(CPUState, fpr[26]) },
2297 { "f27", offsetof(CPUState, fpr[27]) },
2298 { "f28", offsetof(CPUState, fpr[28]) },
2299 { "f29", offsetof(CPUState, fpr[29]) },
2300 { "f30", offsetof(CPUState, fpr[30]) },
2301 { "f31", offsetof(CPUState, fpr[31]) },
2302 { "fpscr", offsetof(CPUState, fpscr) },
2303 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002304 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002305 { "lr", offsetof(CPUState, lr) },
2306 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002307 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002308 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002309 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002310 { "msr", 0, &monitor_get_msr, },
2311 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002312 { "tbu", 0, &monitor_get_tbu, },
2313 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002314#if defined(TARGET_PPC64)
2315 /* Address space register */
2316 { "asr", offsetof(CPUState, asr) },
2317#endif
2318 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002319 { "sdr1", offsetof(CPUState, sdr1) },
2320 { "sr0", offsetof(CPUState, sr[0]) },
2321 { "sr1", offsetof(CPUState, sr[1]) },
2322 { "sr2", offsetof(CPUState, sr[2]) },
2323 { "sr3", offsetof(CPUState, sr[3]) },
2324 { "sr4", offsetof(CPUState, sr[4]) },
2325 { "sr5", offsetof(CPUState, sr[5]) },
2326 { "sr6", offsetof(CPUState, sr[6]) },
2327 { "sr7", offsetof(CPUState, sr[7]) },
2328 { "sr8", offsetof(CPUState, sr[8]) },
2329 { "sr9", offsetof(CPUState, sr[9]) },
2330 { "sr10", offsetof(CPUState, sr[10]) },
2331 { "sr11", offsetof(CPUState, sr[11]) },
2332 { "sr12", offsetof(CPUState, sr[12]) },
2333 { "sr13", offsetof(CPUState, sr[13]) },
2334 { "sr14", offsetof(CPUState, sr[14]) },
2335 { "sr15", offsetof(CPUState, sr[15]) },
2336 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002337#elif defined(TARGET_SPARC)
2338 { "g0", offsetof(CPUState, gregs[0]) },
2339 { "g1", offsetof(CPUState, gregs[1]) },
2340 { "g2", offsetof(CPUState, gregs[2]) },
2341 { "g3", offsetof(CPUState, gregs[3]) },
2342 { "g4", offsetof(CPUState, gregs[4]) },
2343 { "g5", offsetof(CPUState, gregs[5]) },
2344 { "g6", offsetof(CPUState, gregs[6]) },
2345 { "g7", offsetof(CPUState, gregs[7]) },
2346 { "o0", 0, monitor_get_reg },
2347 { "o1", 1, monitor_get_reg },
2348 { "o2", 2, monitor_get_reg },
2349 { "o3", 3, monitor_get_reg },
2350 { "o4", 4, monitor_get_reg },
2351 { "o5", 5, monitor_get_reg },
2352 { "o6", 6, monitor_get_reg },
2353 { "o7", 7, monitor_get_reg },
2354 { "l0", 8, monitor_get_reg },
2355 { "l1", 9, monitor_get_reg },
2356 { "l2", 10, monitor_get_reg },
2357 { "l3", 11, monitor_get_reg },
2358 { "l4", 12, monitor_get_reg },
2359 { "l5", 13, monitor_get_reg },
2360 { "l6", 14, monitor_get_reg },
2361 { "l7", 15, monitor_get_reg },
2362 { "i0", 16, monitor_get_reg },
2363 { "i1", 17, monitor_get_reg },
2364 { "i2", 18, monitor_get_reg },
2365 { "i3", 19, monitor_get_reg },
2366 { "i4", 20, monitor_get_reg },
2367 { "i5", 21, monitor_get_reg },
2368 { "i6", 22, monitor_get_reg },
2369 { "i7", 23, monitor_get_reg },
2370 { "pc", offsetof(CPUState, pc) },
2371 { "npc", offsetof(CPUState, npc) },
2372 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002373#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002374 { "psr", 0, &monitor_get_psr, },
2375 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002376#endif
bellarde95c8d52004-09-30 22:22:08 +00002377 { "tbr", offsetof(CPUState, tbr) },
2378 { "fsr", offsetof(CPUState, fsr) },
2379 { "f0", offsetof(CPUState, fpr[0]) },
2380 { "f1", offsetof(CPUState, fpr[1]) },
2381 { "f2", offsetof(CPUState, fpr[2]) },
2382 { "f3", offsetof(CPUState, fpr[3]) },
2383 { "f4", offsetof(CPUState, fpr[4]) },
2384 { "f5", offsetof(CPUState, fpr[5]) },
2385 { "f6", offsetof(CPUState, fpr[6]) },
2386 { "f7", offsetof(CPUState, fpr[7]) },
2387 { "f8", offsetof(CPUState, fpr[8]) },
2388 { "f9", offsetof(CPUState, fpr[9]) },
2389 { "f10", offsetof(CPUState, fpr[10]) },
2390 { "f11", offsetof(CPUState, fpr[11]) },
2391 { "f12", offsetof(CPUState, fpr[12]) },
2392 { "f13", offsetof(CPUState, fpr[13]) },
2393 { "f14", offsetof(CPUState, fpr[14]) },
2394 { "f15", offsetof(CPUState, fpr[15]) },
2395 { "f16", offsetof(CPUState, fpr[16]) },
2396 { "f17", offsetof(CPUState, fpr[17]) },
2397 { "f18", offsetof(CPUState, fpr[18]) },
2398 { "f19", offsetof(CPUState, fpr[19]) },
2399 { "f20", offsetof(CPUState, fpr[20]) },
2400 { "f21", offsetof(CPUState, fpr[21]) },
2401 { "f22", offsetof(CPUState, fpr[22]) },
2402 { "f23", offsetof(CPUState, fpr[23]) },
2403 { "f24", offsetof(CPUState, fpr[24]) },
2404 { "f25", offsetof(CPUState, fpr[25]) },
2405 { "f26", offsetof(CPUState, fpr[26]) },
2406 { "f27", offsetof(CPUState, fpr[27]) },
2407 { "f28", offsetof(CPUState, fpr[28]) },
2408 { "f29", offsetof(CPUState, fpr[29]) },
2409 { "f30", offsetof(CPUState, fpr[30]) },
2410 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002411#ifdef TARGET_SPARC64
2412 { "f32", offsetof(CPUState, fpr[32]) },
2413 { "f34", offsetof(CPUState, fpr[34]) },
2414 { "f36", offsetof(CPUState, fpr[36]) },
2415 { "f38", offsetof(CPUState, fpr[38]) },
2416 { "f40", offsetof(CPUState, fpr[40]) },
2417 { "f42", offsetof(CPUState, fpr[42]) },
2418 { "f44", offsetof(CPUState, fpr[44]) },
2419 { "f46", offsetof(CPUState, fpr[46]) },
2420 { "f48", offsetof(CPUState, fpr[48]) },
2421 { "f50", offsetof(CPUState, fpr[50]) },
2422 { "f52", offsetof(CPUState, fpr[52]) },
2423 { "f54", offsetof(CPUState, fpr[54]) },
2424 { "f56", offsetof(CPUState, fpr[56]) },
2425 { "f58", offsetof(CPUState, fpr[58]) },
2426 { "f60", offsetof(CPUState, fpr[60]) },
2427 { "f62", offsetof(CPUState, fpr[62]) },
2428 { "asi", offsetof(CPUState, asi) },
2429 { "pstate", offsetof(CPUState, pstate) },
2430 { "cansave", offsetof(CPUState, cansave) },
2431 { "canrestore", offsetof(CPUState, canrestore) },
2432 { "otherwin", offsetof(CPUState, otherwin) },
2433 { "wstate", offsetof(CPUState, wstate) },
2434 { "cleanwin", offsetof(CPUState, cleanwin) },
2435 { "fprs", offsetof(CPUState, fprs) },
2436#endif
bellard9307c4c2004-04-04 12:57:25 +00002437#endif
2438 { NULL },
2439};
2440
aliguori376253e2009-03-05 23:01:23 +00002441static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002442{
aliguori376253e2009-03-05 23:01:23 +00002443 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002444 longjmp(expr_env, 1);
2445}
2446
bellard6a00d602005-11-21 23:25:50 +00002447/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002448static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002449{
blueswir18662d652008-10-02 18:32:44 +00002450 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002451 void *ptr;
2452
bellard9307c4c2004-04-04 12:57:25 +00002453 for(md = monitor_defs; md->name != NULL; md++) {
2454 if (compare_cmd(name, md->name)) {
2455 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002456 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002457 } else {
bellard6a00d602005-11-21 23:25:50 +00002458 CPUState *env = mon_get_cpu();
2459 if (!env)
2460 return -2;
2461 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002462 switch(md->type) {
2463 case MD_I32:
2464 *pval = *(int32_t *)ptr;
2465 break;
2466 case MD_TLONG:
2467 *pval = *(target_long *)ptr;
2468 break;
2469 default:
2470 *pval = 0;
2471 break;
2472 }
bellard9307c4c2004-04-04 12:57:25 +00002473 }
2474 return 0;
2475 }
2476 }
2477 return -1;
2478}
2479
2480static void next(void)
2481{
Blue Swirl660f11b2009-07-31 21:16:51 +00002482 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002483 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002484 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002485 pch++;
2486 }
2487}
2488
aliguori376253e2009-03-05 23:01:23 +00002489static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002490
aliguori376253e2009-03-05 23:01:23 +00002491static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002492{
blueswir1c2efc952007-09-25 17:28:42 +00002493 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002494 char *p;
bellard6a00d602005-11-21 23:25:50 +00002495 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002496
2497 switch(*pch) {
2498 case '+':
2499 next();
aliguori376253e2009-03-05 23:01:23 +00002500 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002501 break;
2502 case '-':
2503 next();
aliguori376253e2009-03-05 23:01:23 +00002504 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002505 break;
2506 case '~':
2507 next();
aliguori376253e2009-03-05 23:01:23 +00002508 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002509 break;
2510 case '(':
2511 next();
aliguori376253e2009-03-05 23:01:23 +00002512 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002513 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002514 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002515 }
2516 next();
2517 break;
bellard81d09122004-07-14 17:21:37 +00002518 case '\'':
2519 pch++;
2520 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002521 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002522 n = *pch;
2523 pch++;
2524 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002525 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002526 next();
2527 break;
bellard9307c4c2004-04-04 12:57:25 +00002528 case '$':
2529 {
2530 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002531 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002532
bellard9307c4c2004-04-04 12:57:25 +00002533 pch++;
2534 q = buf;
2535 while ((*pch >= 'a' && *pch <= 'z') ||
2536 (*pch >= 'A' && *pch <= 'Z') ||
2537 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002538 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002539 if ((q - buf) < sizeof(buf) - 1)
2540 *q++ = *pch;
2541 pch++;
2542 }
blueswir1cd390082008-11-16 13:53:32 +00002543 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002544 pch++;
2545 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002546 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002547 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002548 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002549 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002550 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002551 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002552 }
2553 break;
2554 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002555 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002556 n = 0;
2557 break;
2558 default:
blueswir17743e582007-09-24 18:39:04 +00002559#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002560 n = strtoull(pch, &p, 0);
2561#else
bellard9307c4c2004-04-04 12:57:25 +00002562 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002563#endif
bellard9307c4c2004-04-04 12:57:25 +00002564 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002565 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002566 }
2567 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002568 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002569 pch++;
2570 break;
2571 }
2572 return n;
2573}
2574
2575
aliguori376253e2009-03-05 23:01:23 +00002576static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002577{
blueswir1c2efc952007-09-25 17:28:42 +00002578 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002579 int op;
ths3b46e622007-09-17 08:09:54 +00002580
aliguori376253e2009-03-05 23:01:23 +00002581 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002582 for(;;) {
2583 op = *pch;
2584 if (op != '*' && op != '/' && op != '%')
2585 break;
2586 next();
aliguori376253e2009-03-05 23:01:23 +00002587 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002588 switch(op) {
2589 default:
2590 case '*':
2591 val *= val2;
2592 break;
2593 case '/':
2594 case '%':
ths5fafdf22007-09-16 21:08:06 +00002595 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002596 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002597 if (op == '/')
2598 val /= val2;
2599 else
2600 val %= val2;
2601 break;
2602 }
2603 }
2604 return val;
2605}
2606
aliguori376253e2009-03-05 23:01:23 +00002607static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002608{
blueswir1c2efc952007-09-25 17:28:42 +00002609 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002610 int op;
bellard9307c4c2004-04-04 12:57:25 +00002611
aliguori376253e2009-03-05 23:01:23 +00002612 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002613 for(;;) {
2614 op = *pch;
2615 if (op != '&' && op != '|' && op != '^')
2616 break;
2617 next();
aliguori376253e2009-03-05 23:01:23 +00002618 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002619 switch(op) {
2620 default:
2621 case '&':
2622 val &= val2;
2623 break;
2624 case '|':
2625 val |= val2;
2626 break;
2627 case '^':
2628 val ^= val2;
2629 break;
2630 }
2631 }
2632 return val;
2633}
2634
aliguori376253e2009-03-05 23:01:23 +00002635static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002636{
blueswir1c2efc952007-09-25 17:28:42 +00002637 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002638 int op;
bellard9307c4c2004-04-04 12:57:25 +00002639
aliguori376253e2009-03-05 23:01:23 +00002640 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002641 for(;;) {
2642 op = *pch;
2643 if (op != '+' && op != '-')
2644 break;
2645 next();
aliguori376253e2009-03-05 23:01:23 +00002646 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002647 if (op == '+')
2648 val += val2;
2649 else
2650 val -= val2;
2651 }
2652 return val;
2653}
2654
aliguori376253e2009-03-05 23:01:23 +00002655static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002656{
2657 pch = *pp;
2658 if (setjmp(expr_env)) {
2659 *pp = pch;
2660 return -1;
2661 }
blueswir1cd390082008-11-16 13:53:32 +00002662 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002663 pch++;
aliguori376253e2009-03-05 23:01:23 +00002664 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002665 *pp = pch;
2666 return 0;
2667}
2668
2669static int get_str(char *buf, int buf_size, const char **pp)
2670{
2671 const char *p;
2672 char *q;
2673 int c;
2674
bellard81d09122004-07-14 17:21:37 +00002675 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002676 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002677 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002678 p++;
2679 if (*p == '\0') {
2680 fail:
bellard81d09122004-07-14 17:21:37 +00002681 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002682 *pp = p;
2683 return -1;
2684 }
bellard9307c4c2004-04-04 12:57:25 +00002685 if (*p == '\"') {
2686 p++;
2687 while (*p != '\0' && *p != '\"') {
2688 if (*p == '\\') {
2689 p++;
2690 c = *p++;
2691 switch(c) {
2692 case 'n':
2693 c = '\n';
2694 break;
2695 case 'r':
2696 c = '\r';
2697 break;
2698 case '\\':
2699 case '\'':
2700 case '\"':
2701 break;
2702 default:
2703 qemu_printf("unsupported escape code: '\\%c'\n", c);
2704 goto fail;
2705 }
2706 if ((q - buf) < buf_size - 1) {
2707 *q++ = c;
2708 }
2709 } else {
2710 if ((q - buf) < buf_size - 1) {
2711 *q++ = *p;
2712 }
2713 p++;
2714 }
2715 }
2716 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002717 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002718 goto fail;
2719 }
2720 p++;
2721 } else {
blueswir1cd390082008-11-16 13:53:32 +00002722 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002723 if ((q - buf) < buf_size - 1) {
2724 *q++ = *p;
2725 }
2726 p++;
2727 }
bellard9307c4c2004-04-04 12:57:25 +00002728 }
bellard81d09122004-07-14 17:21:37 +00002729 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002730 *pp = p;
2731 return 0;
2732}
2733
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002734/*
2735 * Store the command-name in cmdname, and return a pointer to
2736 * the remaining of the command string.
2737 */
2738static const char *get_command_name(const char *cmdline,
2739 char *cmdname, size_t nlen)
2740{
2741 size_t len;
2742 const char *p, *pstart;
2743
2744 p = cmdline;
2745 while (qemu_isspace(*p))
2746 p++;
2747 if (*p == '\0')
2748 return NULL;
2749 pstart = p;
2750 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2751 p++;
2752 len = p - pstart;
2753 if (len > nlen - 1)
2754 len = nlen - 1;
2755 memcpy(cmdname, pstart, len);
2756 cmdname[len] = '\0';
2757 return p;
2758}
2759
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002760/**
2761 * Read key of 'type' into 'key' and return the current
2762 * 'type' pointer.
2763 */
2764static char *key_get_info(const char *type, char **key)
2765{
2766 size_t len;
2767 char *p, *str;
2768
2769 if (*type == ',')
2770 type++;
2771
2772 p = strchr(type, ':');
2773 if (!p) {
2774 *key = NULL;
2775 return NULL;
2776 }
2777 len = p - type;
2778
2779 str = qemu_malloc(len + 1);
2780 memcpy(str, type, len);
2781 str[len] = '\0';
2782
2783 *key = str;
2784 return ++p;
2785}
2786
bellard9307c4c2004-04-04 12:57:25 +00002787static int default_fmt_format = 'x';
2788static int default_fmt_size = 4;
2789
2790#define MAX_ARGS 16
2791
Anthony Liguoric227f092009-10-01 16:12:16 -05002792static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002793 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002794 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002795{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002796 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002797 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002798 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002799 char cmdname[256];
2800 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002801 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002802
2803#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002804 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002805#endif
ths3b46e622007-09-17 08:09:54 +00002806
bellard9307c4c2004-04-04 12:57:25 +00002807 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002808 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2809 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002810 return NULL;
ths3b46e622007-09-17 08:09:54 +00002811
bellard9307c4c2004-04-04 12:57:25 +00002812 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002813 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002814 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002815 break;
bellard9dc39cb2004-03-14 21:38:27 +00002816 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002817
2818 if (cmd->name == NULL) {
2819 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002820 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002821 }
bellard9307c4c2004-04-04 12:57:25 +00002822
bellard9307c4c2004-04-04 12:57:25 +00002823 /* parse the parameters */
2824 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002825 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002826 typestr = key_get_info(typestr, &key);
2827 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002828 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002829 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002830 typestr++;
2831 switch(c) {
2832 case 'F':
bellard81d09122004-07-14 17:21:37 +00002833 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002834 case 's':
2835 {
2836 int ret;
ths3b46e622007-09-17 08:09:54 +00002837
blueswir1cd390082008-11-16 13:53:32 +00002838 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002839 p++;
2840 if (*typestr == '?') {
2841 typestr++;
2842 if (*p == '\0') {
2843 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002844 break;
bellard9307c4c2004-04-04 12:57:25 +00002845 }
2846 }
2847 ret = get_str(buf, sizeof(buf), &p);
2848 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002849 switch(c) {
2850 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002851 monitor_printf(mon, "%s: filename expected\n",
2852 cmdname);
bellard81d09122004-07-14 17:21:37 +00002853 break;
2854 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002855 monitor_printf(mon, "%s: block device name expected\n",
2856 cmdname);
bellard81d09122004-07-14 17:21:37 +00002857 break;
2858 default:
aliguori376253e2009-03-05 23:01:23 +00002859 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002860 break;
2861 }
bellard9307c4c2004-04-04 12:57:25 +00002862 goto fail;
2863 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002864 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002865 }
2866 break;
2867 case '/':
2868 {
2869 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002870
blueswir1cd390082008-11-16 13:53:32 +00002871 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002872 p++;
2873 if (*p == '/') {
2874 /* format found */
2875 p++;
2876 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002877 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002878 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002879 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002880 count = count * 10 + (*p - '0');
2881 p++;
2882 }
2883 }
2884 size = -1;
2885 format = -1;
2886 for(;;) {
2887 switch(*p) {
2888 case 'o':
2889 case 'd':
2890 case 'u':
2891 case 'x':
2892 case 'i':
2893 case 'c':
2894 format = *p++;
2895 break;
2896 case 'b':
2897 size = 1;
2898 p++;
2899 break;
2900 case 'h':
2901 size = 2;
2902 p++;
2903 break;
2904 case 'w':
2905 size = 4;
2906 p++;
2907 break;
2908 case 'g':
2909 case 'L':
2910 size = 8;
2911 p++;
2912 break;
2913 default:
2914 goto next;
2915 }
2916 }
2917 next:
blueswir1cd390082008-11-16 13:53:32 +00002918 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002919 monitor_printf(mon, "invalid char in format: '%c'\n",
2920 *p);
bellard9307c4c2004-04-04 12:57:25 +00002921 goto fail;
2922 }
bellard9307c4c2004-04-04 12:57:25 +00002923 if (format < 0)
2924 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002925 if (format != 'i') {
2926 /* for 'i', not specifying a size gives -1 as size */
2927 if (size < 0)
2928 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002929 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002930 }
bellard9307c4c2004-04-04 12:57:25 +00002931 default_fmt_format = format;
2932 } else {
2933 count = 1;
2934 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002935 if (format != 'i') {
2936 size = default_fmt_size;
2937 } else {
2938 size = -1;
2939 }
bellard9307c4c2004-04-04 12:57:25 +00002940 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002941 qdict_put(qdict, "count", qint_from_int(count));
2942 qdict_put(qdict, "format", qint_from_int(format));
2943 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002944 }
2945 break;
2946 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002947 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002948 {
blueswir1c2efc952007-09-25 17:28:42 +00002949 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002950
blueswir1cd390082008-11-16 13:53:32 +00002951 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002952 p++;
bellard34405572004-06-08 00:55:58 +00002953 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002954 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002955 if (*p == '\0') {
2956 typestr++;
2957 break;
2958 }
bellard34405572004-06-08 00:55:58 +00002959 } else {
2960 if (*p == '.') {
2961 p++;
blueswir1cd390082008-11-16 13:53:32 +00002962 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002963 p++;
bellard34405572004-06-08 00:55:58 +00002964 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002965 typestr++;
2966 break;
bellard34405572004-06-08 00:55:58 +00002967 }
2968 }
bellard13224a82006-07-14 22:03:35 +00002969 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002970 }
aliguori376253e2009-03-05 23:01:23 +00002971 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002972 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002973 /* Check if 'i' is greater than 32-bit */
2974 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2975 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2976 monitor_printf(mon, "integer is for 32-bit values\n");
2977 goto fail;
2978 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002979 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002980 }
2981 break;
2982 case '-':
2983 {
2984 int has_option;
2985 /* option */
ths3b46e622007-09-17 08:09:54 +00002986
bellard9307c4c2004-04-04 12:57:25 +00002987 c = *typestr++;
2988 if (c == '\0')
2989 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002990 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002991 p++;
2992 has_option = 0;
2993 if (*p == '-') {
2994 p++;
2995 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00002996 monitor_printf(mon, "%s: unsupported option -%c\n",
2997 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00002998 goto fail;
2999 }
3000 p++;
3001 has_option = 1;
3002 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003003 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003004 }
3005 break;
3006 default:
3007 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003008 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003009 goto fail;
3010 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003011 qemu_free(key);
3012 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003013 }
3014 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003015 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003016 p++;
3017 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003018 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3019 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003020 goto fail;
3021 }
3022
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003023 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003024
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003025fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003026 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003027 return NULL;
3028}
3029
3030static void monitor_handle_command(Monitor *mon, const char *cmdline)
3031{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003032 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003033 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003034
3035 qdict = qdict_new();
3036
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003037 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003038 if (!cmd)
3039 goto out;
3040
3041 qemu_errors_to_mon(mon);
3042
3043 if (monitor_handler_ported(cmd)) {
3044 QObject *data = NULL;
3045
3046 cmd->mhandler.cmd_new(mon, qdict, &data);
3047 if (data)
3048 cmd->user_print(mon, data);
3049
3050 qobject_decref(data);
3051 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003052 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003053 }
3054
Luiz Capitulino13917be2009-10-07 13:41:54 -03003055 qemu_errors_to_previous();
3056
3057out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003058 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003059}
3060
bellard81d09122004-07-14 17:21:37 +00003061static void cmd_completion(const char *name, const char *list)
3062{
3063 const char *p, *pstart;
3064 char cmd[128];
3065 int len;
3066
3067 p = list;
3068 for(;;) {
3069 pstart = p;
3070 p = strchr(p, '|');
3071 if (!p)
3072 p = pstart + strlen(pstart);
3073 len = p - pstart;
3074 if (len > sizeof(cmd) - 2)
3075 len = sizeof(cmd) - 2;
3076 memcpy(cmd, pstart, len);
3077 cmd[len] = '\0';
3078 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003079 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003080 }
3081 if (*p == '\0')
3082 break;
3083 p++;
3084 }
3085}
3086
3087static void file_completion(const char *input)
3088{
3089 DIR *ffs;
3090 struct dirent *d;
3091 char path[1024];
3092 char file[1024], file_prefix[1024];
3093 int input_path_len;
3094 const char *p;
3095
ths5fafdf22007-09-16 21:08:06 +00003096 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003097 if (!p) {
3098 input_path_len = 0;
3099 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003100 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003101 } else {
3102 input_path_len = p - input + 1;
3103 memcpy(path, input, input_path_len);
3104 if (input_path_len > sizeof(path) - 1)
3105 input_path_len = sizeof(path) - 1;
3106 path[input_path_len] = '\0';
3107 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3108 }
3109#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003110 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3111 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003112#endif
3113 ffs = opendir(path);
3114 if (!ffs)
3115 return;
3116 for(;;) {
3117 struct stat sb;
3118 d = readdir(ffs);
3119 if (!d)
3120 break;
3121 if (strstart(d->d_name, file_prefix, NULL)) {
3122 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003123 if (input_path_len < sizeof(file))
3124 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3125 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003126 /* stat the file to find out if it's a directory.
3127 * In that case add a slash to speed up typing long paths
3128 */
3129 stat(file, &sb);
3130 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003131 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003132 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003133 }
3134 }
3135 closedir(ffs);
3136}
3137
aliguori51de9762009-03-05 23:00:43 +00003138static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003139{
aliguori51de9762009-03-05 23:00:43 +00003140 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003141 const char *input = opaque;
3142
3143 if (input[0] == '\0' ||
3144 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003145 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003146 }
3147}
3148
3149/* NOTE: this parser is an approximate form of the real command parser */
3150static void parse_cmdline(const char *cmdline,
3151 int *pnb_args, char **args)
3152{
3153 const char *p;
3154 int nb_args, ret;
3155 char buf[1024];
3156
3157 p = cmdline;
3158 nb_args = 0;
3159 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003160 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003161 p++;
3162 if (*p == '\0')
3163 break;
3164 if (nb_args >= MAX_ARGS)
3165 break;
3166 ret = get_str(buf, sizeof(buf), &p);
3167 args[nb_args] = qemu_strdup(buf);
3168 nb_args++;
3169 if (ret < 0)
3170 break;
3171 }
3172 *pnb_args = nb_args;
3173}
3174
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003175static const char *next_arg_type(const char *typestr)
3176{
3177 const char *p = strchr(typestr, ':');
3178 return (p != NULL ? ++p : typestr);
3179}
3180
aliguori4c36ba32009-03-05 23:01:37 +00003181static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003182{
3183 const char *cmdname;
3184 char *args[MAX_ARGS];
3185 int nb_args, i, len;
3186 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003187 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003188 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003189
3190 parse_cmdline(cmdline, &nb_args, args);
3191#ifdef DEBUG_COMPLETION
3192 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003193 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003194 }
3195#endif
3196
3197 /* if the line ends with a space, it means we want to complete the
3198 next arg */
3199 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003200 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003201 if (nb_args >= MAX_ARGS)
3202 return;
3203 args[nb_args++] = qemu_strdup("");
3204 }
3205 if (nb_args <= 1) {
3206 /* command completion */
3207 if (nb_args == 0)
3208 cmdname = "";
3209 else
3210 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003211 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003212 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003213 cmd_completion(cmdname, cmd->name);
3214 }
3215 } else {
3216 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003217 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003218 if (compare_cmd(args[0], cmd->name))
3219 goto found;
3220 }
3221 return;
3222 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003223 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003224 for(i = 0; i < nb_args - 2; i++) {
3225 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003226 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003227 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003228 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003229 }
3230 }
3231 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003232 if (*ptype == '-' && ptype[1] != '\0') {
3233 ptype += 2;
3234 }
bellard81d09122004-07-14 17:21:37 +00003235 switch(*ptype) {
3236 case 'F':
3237 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003238 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003239 file_completion(str);
3240 break;
3241 case 'B':
3242 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003243 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003244 bdrv_iterate(block_completion_it, (void *)str);
3245 break;
bellard7fe48482004-10-09 18:08:01 +00003246 case 's':
3247 /* XXX: more generic ? */
3248 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003249 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003250 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3251 cmd_completion(str, cmd->name);
3252 }
bellard64866c32006-05-07 18:03:31 +00003253 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003254 char *sep = strrchr(str, '-');
3255 if (sep)
3256 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003257 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003258 for(key = key_defs; key->name != NULL; key++) {
3259 cmd_completion(str, key->name);
3260 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003261 } else if (!strcmp(cmd->name, "help|?")) {
3262 readline_set_completion_index(cur_mon->rs, strlen(str));
3263 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3264 cmd_completion(str, cmd->name);
3265 }
bellard7fe48482004-10-09 18:08:01 +00003266 }
3267 break;
bellard81d09122004-07-14 17:21:37 +00003268 default:
3269 break;
3270 }
3271 }
3272 for(i = 0; i < nb_args; i++)
3273 qemu_free(args[i]);
3274}
3275
aliguori731b0362009-03-05 23:01:42 +00003276static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003277{
aliguori731b0362009-03-05 23:01:42 +00003278 Monitor *mon = opaque;
3279
3280 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003281}
3282
aliguori731b0362009-03-05 23:01:42 +00003283static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003284{
aliguori731b0362009-03-05 23:01:42 +00003285 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003286 int i;
aliguori376253e2009-03-05 23:01:23 +00003287
aliguori731b0362009-03-05 23:01:42 +00003288 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003289
aliguoricde76ee2009-03-05 23:01:51 +00003290 if (cur_mon->rs) {
3291 for (i = 0; i < size; i++)
3292 readline_handle_byte(cur_mon->rs, buf[i]);
3293 } else {
3294 if (size == 0 || buf[size - 1] != 0)
3295 monitor_printf(cur_mon, "corrupted command\n");
3296 else
3297 monitor_handle_command(cur_mon, (char *)buf);
3298 }
aliguori731b0362009-03-05 23:01:42 +00003299
3300 cur_mon = old_mon;
3301}
aliguorid8f44602008-10-06 13:52:44 +00003302
aliguori376253e2009-03-05 23:01:23 +00003303static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003304{
aliguori731b0362009-03-05 23:01:42 +00003305 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003306 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003307 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003308}
3309
aliguoricde76ee2009-03-05 23:01:51 +00003310int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003311{
aliguoricde76ee2009-03-05 23:01:51 +00003312 if (!mon->rs)
3313 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003314 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003315 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003316}
3317
aliguori376253e2009-03-05 23:01:23 +00003318void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003319{
aliguoricde76ee2009-03-05 23:01:51 +00003320 if (!mon->rs)
3321 return;
aliguori731b0362009-03-05 23:01:42 +00003322 if (--mon->suspend_cnt == 0)
3323 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003324}
3325
aliguori731b0362009-03-05 23:01:42 +00003326static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003327{
aliguori376253e2009-03-05 23:01:23 +00003328 Monitor *mon = opaque;
3329
aliguori2724b182009-03-05 23:01:47 +00003330 switch (event) {
3331 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003332 mon->mux_out = 0;
3333 if (mon->reset_seen) {
3334 readline_restart(mon->rs);
3335 monitor_resume(mon);
3336 monitor_flush(mon);
3337 } else {
3338 mon->suspend_cnt = 0;
3339 }
aliguori2724b182009-03-05 23:01:47 +00003340 break;
ths86e94de2007-01-05 22:01:59 +00003341
aliguori2724b182009-03-05 23:01:47 +00003342 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003343 if (mon->reset_seen) {
3344 if (mon->suspend_cnt == 0) {
3345 monitor_printf(mon, "\n");
3346 }
3347 monitor_flush(mon);
3348 monitor_suspend(mon);
3349 } else {
3350 mon->suspend_cnt++;
3351 }
3352 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003353 break;
3354
3355 case CHR_EVENT_RESET:
3356 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3357 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003358 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003359 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003360 }
3361 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003362 break;
3363 }
ths86e94de2007-01-05 22:01:59 +00003364}
3365
aliguori76655d62009-03-06 20:27:37 +00003366
3367/*
3368 * Local variables:
3369 * c-indent-level: 4
3370 * c-basic-offset: 4
3371 * tab-width: 8
3372 * End:
3373 */
3374
aliguori731b0362009-03-05 23:01:42 +00003375void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003376{
aliguori731b0362009-03-05 23:01:42 +00003377 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003378 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003379
3380 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003381 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003382 is_first_init = 0;
3383 }
aliguori87127162009-03-05 23:01:29 +00003384
3385 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003386
aliguori87127162009-03-05 23:01:29 +00003387 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003388 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003389 if (flags & MONITOR_USE_READLINE) {
3390 mon->rs = readline_init(mon, monitor_find_completion);
3391 monitor_read_command(mon, 0);
3392 }
aliguori87127162009-03-05 23:01:29 +00003393
aliguori731b0362009-03-05 23:01:42 +00003394 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3395 mon);
aliguori87127162009-03-05 23:01:29 +00003396
Blue Swirl72cf2d42009-09-12 07:36:22 +00003397 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003398 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003399 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003400}
3401
aliguori376253e2009-03-05 23:01:23 +00003402static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003403{
aliguoribb5fc202009-03-05 23:01:15 +00003404 BlockDriverState *bs = opaque;
3405 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003406
aliguoribb5fc202009-03-05 23:01:15 +00003407 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003408 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003409 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003410 }
aliguori731b0362009-03-05 23:01:42 +00003411 if (mon->password_completion_cb)
3412 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003413
aliguori731b0362009-03-05 23:01:42 +00003414 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003415}
aliguoric0f4ce72009-03-05 23:01:01 +00003416
aliguori376253e2009-03-05 23:01:23 +00003417void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003418 BlockDriverCompletionFunc *completion_cb,
3419 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003420{
aliguoricde76ee2009-03-05 23:01:51 +00003421 int err;
3422
aliguoribb5fc202009-03-05 23:01:15 +00003423 if (!bdrv_key_required(bs)) {
3424 if (completion_cb)
3425 completion_cb(opaque, 0);
3426 return;
3427 }
aliguoric0f4ce72009-03-05 23:01:01 +00003428
aliguori376253e2009-03-05 23:01:23 +00003429 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3430 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003431
aliguori731b0362009-03-05 23:01:42 +00003432 mon->password_completion_cb = completion_cb;
3433 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003434
aliguoricde76ee2009-03-05 23:01:51 +00003435 err = monitor_read_password(mon, bdrv_password_cb, bs);
3436
3437 if (err && completion_cb)
3438 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003439}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003440
3441typedef struct QemuErrorSink QemuErrorSink;
3442struct QemuErrorSink {
3443 enum {
3444 ERR_SINK_FILE,
3445 ERR_SINK_MONITOR,
3446 } dest;
3447 union {
3448 FILE *fp;
3449 Monitor *mon;
3450 };
3451 QemuErrorSink *previous;
3452};
3453
Blue Swirl528e93a2009-08-31 15:14:40 +00003454static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003455
3456void qemu_errors_to_file(FILE *fp)
3457{
3458 QemuErrorSink *sink;
3459
3460 sink = qemu_mallocz(sizeof(*sink));
3461 sink->dest = ERR_SINK_FILE;
3462 sink->fp = fp;
3463 sink->previous = qemu_error_sink;
3464 qemu_error_sink = sink;
3465}
3466
3467void qemu_errors_to_mon(Monitor *mon)
3468{
3469 QemuErrorSink *sink;
3470
3471 sink = qemu_mallocz(sizeof(*sink));
3472 sink->dest = ERR_SINK_MONITOR;
3473 sink->mon = mon;
3474 sink->previous = qemu_error_sink;
3475 qemu_error_sink = sink;
3476}
3477
3478void qemu_errors_to_previous(void)
3479{
3480 QemuErrorSink *sink;
3481
3482 assert(qemu_error_sink != NULL);
3483 sink = qemu_error_sink;
3484 qemu_error_sink = sink->previous;
3485 qemu_free(sink);
3486}
3487
3488void qemu_error(const char *fmt, ...)
3489{
3490 va_list args;
3491
3492 assert(qemu_error_sink != NULL);
3493 switch (qemu_error_sink->dest) {
3494 case ERR_SINK_FILE:
3495 va_start(args, fmt);
3496 vfprintf(qemu_error_sink->fp, fmt, args);
3497 va_end(args);
3498 break;
3499 case ERR_SINK_MONITOR:
3500 va_start(args, fmt);
3501 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3502 va_end(args);
3503 break;
3504 }
3505}