blob: ab15289a9c1e35598ed7708e88521109be71a769 [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 Capitulinob223f352009-10-07 13:41:56 -0300460/**
461 * do_quit(): Quit QEMU execution
462 */
463static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000464{
465 exit(0);
466}
467
aliguori376253e2009-03-05 23:01:23 +0000468static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
bellard9dc39cb2004-03-14 21:38:27 +0000469{
470 if (bdrv_is_inserted(bs)) {
471 if (!force) {
472 if (!bdrv_is_removable(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000473 monitor_printf(mon, "device is not removable\n");
bellard9dc39cb2004-03-14 21:38:27 +0000474 return -1;
475 }
476 if (bdrv_is_locked(bs)) {
aliguori376253e2009-03-05 23:01:23 +0000477 monitor_printf(mon, "device is locked\n");
bellard9dc39cb2004-03-14 21:38:27 +0000478 return -1;
479 }
480 }
481 bdrv_close(bs);
482 }
483 return 0;
484}
485
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300486static void do_eject(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000487{
488 BlockDriverState *bs;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300489 int force = qdict_get_int(qdict, "force");
490 const char *filename = qdict_get_str(qdict, "filename");
bellard9dc39cb2004-03-14 21:38:27 +0000491
bellard9307c4c2004-04-04 12:57:25 +0000492 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000493 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000494 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000495 return;
496 }
aliguori376253e2009-03-05 23:01:23 +0000497 eject_device(mon, bs, force);
bellard9dc39cb2004-03-14 21:38:27 +0000498}
499
aliguori376253e2009-03-05 23:01:23 +0000500static void do_change_block(Monitor *mon, const char *device,
501 const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000502{
503 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000504 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000505
bellard9307c4c2004-04-04 12:57:25 +0000506 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000507 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +0000508 monitor_printf(mon, "device not found\n");
bellard9dc39cb2004-03-14 21:38:27 +0000509 return;
510 }
aurel322ecea9b2008-06-18 22:10:01 +0000511 if (fmt) {
512 drv = bdrv_find_format(fmt);
513 if (!drv) {
aliguori376253e2009-03-05 23:01:23 +0000514 monitor_printf(mon, "invalid format %s\n", fmt);
aurel322ecea9b2008-06-18 22:10:01 +0000515 return;
516 }
517 }
aliguori376253e2009-03-05 23:01:23 +0000518 if (eject_device(mon, bs, 0) < 0)
bellard9dc39cb2004-03-14 21:38:27 +0000519 return;
aurel322ecea9b2008-06-18 22:10:01 +0000520 bdrv_open2(bs, filename, 0, drv);
aliguori376253e2009-03-05 23:01:23 +0000521 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000522}
523
aliguori376253e2009-03-05 23:01:23 +0000524static void change_vnc_password_cb(Monitor *mon, const char *password,
525 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000526{
527 if (vnc_display_password(NULL, password) < 0)
aliguori376253e2009-03-05 23:01:23 +0000528 monitor_printf(mon, "could not set VNC server password\n");
aliguoribb5fc202009-03-05 23:01:15 +0000529
aliguori731b0362009-03-05 23:01:42 +0000530 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000531}
532
aliguori376253e2009-03-05 23:01:23 +0000533static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000534{
ths70848512007-08-25 01:37:05 +0000535 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000536 strcmp(target, "password") == 0) {
537 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000538 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000539 strncpy(password, arg, sizeof(password));
540 password[sizeof(password) - 1] = '\0';
aliguori376253e2009-03-05 23:01:23 +0000541 change_vnc_password_cb(mon, password, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000542 } else {
aliguori376253e2009-03-05 23:01:23 +0000543 monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000544 }
ths70848512007-08-25 01:37:05 +0000545 } else {
aliguori28a76be2009-03-06 20:27:40 +0000546 if (vnc_display_open(NULL, target) < 0)
aliguori376253e2009-03-05 23:01:23 +0000547 monitor_printf(mon, "could not start VNC server on %s\n", target);
ths70848512007-08-25 01:37:05 +0000548 }
thse25a5822007-08-25 01:36:20 +0000549}
550
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300551static void do_change(Monitor *mon, const QDict *qdict)
thse25a5822007-08-25 01:36:20 +0000552{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300553 const char *device = qdict_get_str(qdict, "device");
554 const char *target = qdict_get_str(qdict, "target");
555 const char *arg = qdict_get_try_str(qdict, "arg");
thse25a5822007-08-25 01:36:20 +0000556 if (strcmp(device, "vnc") == 0) {
aliguori28a76be2009-03-06 20:27:40 +0000557 do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000558 } else {
aliguori28a76be2009-03-06 20:27:40 +0000559 do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000560 }
561}
562
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300563static void do_screen_dump(Monitor *mon, const QDict *qdict)
bellard59a983b2004-03-17 23:17:16 +0000564{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300565 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
bellard59a983b2004-03-17 23:17:16 +0000566}
567
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300568static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +0000569{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300570 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +0000571}
572
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300573static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +0000574{
575 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300576 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +0000577
bellard9307c4c2004-04-04 12:57:25 +0000578 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000579 mask = 0;
580 } else {
bellard9307c4c2004-04-04 12:57:25 +0000581 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000582 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +0000583 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +0000584 return;
585 }
586 }
587 cpu_set_log(mask);
588}
589
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300590static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +0000591{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300592 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +0000593 if (!option || !strcmp(option, "on")) {
594 singlestep = 1;
595 } else if (!strcmp(option, "off")) {
596 singlestep = 0;
597 } else {
598 monitor_printf(mon, "unexpected option %s\n", option);
599 }
600}
601
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -0300602/**
603 * do_stop(): Stop VM execution
604 */
605static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +0000606{
607 vm_stop(EXCP_INTERRUPT);
608}
609
aliguoribb5fc202009-03-05 23:01:15 +0000610static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000611
aliguori376253e2009-03-05 23:01:23 +0000612struct bdrv_iterate_context {
613 Monitor *mon;
614 int err;
615};
aliguoric0f4ce72009-03-05 23:01:01 +0000616
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300617static void do_cont(Monitor *mon, const QDict *qdict)
aliguori376253e2009-03-05 23:01:23 +0000618{
619 struct bdrv_iterate_context context = { mon, 0 };
620
621 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000622 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000623 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000624 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000625}
626
aliguoribb5fc202009-03-05 23:01:15 +0000627static void bdrv_key_cb(void *opaque, int err)
628{
aliguori376253e2009-03-05 23:01:23 +0000629 Monitor *mon = opaque;
630
aliguoribb5fc202009-03-05 23:01:15 +0000631 /* another key was set successfully, retry to continue */
632 if (!err)
Luiz Capitulinof96fc8a2009-08-28 15:27:12 -0300633 do_cont(mon, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000634}
635
636static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
637{
aliguori376253e2009-03-05 23:01:23 +0000638 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000639
aliguori376253e2009-03-05 23:01:23 +0000640 if (!context->err && bdrv_key_required(bs)) {
641 context->err = -EBUSY;
642 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
643 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000644 }
645}
646
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300647static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000648{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300649 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000650 if (!device)
651 device = "tcp::" DEFAULT_GDBSTUB_PORT;
652 if (gdbserver_start(device) < 0) {
653 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
654 device);
655 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000656 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000657 } else {
aliguori59030a82009-04-05 18:43:41 +0000658 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
659 device);
bellard8a7ddc32004-03-31 19:00:16 +0000660 }
661}
662
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300663static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100664{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300665 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100666 if (select_watchdog_action(action) == -1) {
667 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
668 }
669}
670
aliguori376253e2009-03-05 23:01:23 +0000671static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000672{
aliguori376253e2009-03-05 23:01:23 +0000673 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000674 switch(c) {
675 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000676 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000677 break;
678 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000679 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000680 break;
681 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000682 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000683 break;
684 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000685 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000686 break;
687 default:
688 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000689 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000690 } else {
aliguori376253e2009-03-05 23:01:23 +0000691 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000692 }
693 break;
694 }
aliguori376253e2009-03-05 23:01:23 +0000695 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000696}
697
aliguori376253e2009-03-05 23:01:23 +0000698static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500699 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000700{
bellard6a00d602005-11-21 23:25:50 +0000701 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000702 int nb_per_line, l, line_size, i, max_digits, len;
703 uint8_t buf[16];
704 uint64_t v;
705
706 if (format == 'i') {
707 int flags;
708 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000709 env = mon_get_cpu();
710 if (!env && !is_physical)
711 return;
bellard9307c4c2004-04-04 12:57:25 +0000712#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000713 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000714 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000715 } else if (wsize == 4) {
716 flags = 0;
717 } else {
bellard6a15fd12006-04-12 21:07:07 +0000718 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000719 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000720 if (env) {
721#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000722 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000723 (env->segs[R_CS].flags & DESC_L_MASK))
724 flags = 2;
725 else
726#endif
727 if (!(env->segs[R_CS].flags & DESC_B_MASK))
728 flags = 1;
729 }
bellard4c27ba22004-04-25 18:05:08 +0000730 }
731#endif
aliguori376253e2009-03-05 23:01:23 +0000732 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000733 return;
734 }
735
736 len = wsize * count;
737 if (wsize == 1)
738 line_size = 8;
739 else
740 line_size = 16;
741 nb_per_line = line_size / wsize;
742 max_digits = 0;
743
744 switch(format) {
745 case 'o':
746 max_digits = (wsize * 8 + 2) / 3;
747 break;
748 default:
749 case 'x':
750 max_digits = (wsize * 8) / 4;
751 break;
752 case 'u':
753 case 'd':
754 max_digits = (wsize * 8 * 10 + 32) / 33;
755 break;
756 case 'c':
757 wsize = 1;
758 break;
759 }
760
761 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000762 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000763 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000764 else
aliguori376253e2009-03-05 23:01:23 +0000765 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000766 l = len;
767 if (l > line_size)
768 l = line_size;
769 if (is_physical) {
770 cpu_physical_memory_rw(addr, buf, l, 0);
771 } else {
bellard6a00d602005-11-21 23:25:50 +0000772 env = mon_get_cpu();
773 if (!env)
774 break;
aliguoric8f79b62008-08-18 14:00:20 +0000775 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000776 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000777 break;
778 }
bellard9307c4c2004-04-04 12:57:25 +0000779 }
ths5fafdf22007-09-16 21:08:06 +0000780 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000781 while (i < l) {
782 switch(wsize) {
783 default:
784 case 1:
785 v = ldub_raw(buf + i);
786 break;
787 case 2:
788 v = lduw_raw(buf + i);
789 break;
790 case 4:
bellard92a31b12005-02-10 22:00:52 +0000791 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000792 break;
793 case 8:
794 v = ldq_raw(buf + i);
795 break;
796 }
aliguori376253e2009-03-05 23:01:23 +0000797 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000798 switch(format) {
799 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000800 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000801 break;
802 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000803 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000804 break;
805 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000806 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000807 break;
808 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000809 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000810 break;
811 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000812 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000813 break;
814 }
815 i += wsize;
816 }
aliguori376253e2009-03-05 23:01:23 +0000817 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000818 addr += l;
819 len -= l;
820 }
821}
822
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300823static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000824{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300825 int count = qdict_get_int(qdict, "count");
826 int format = qdict_get_int(qdict, "format");
827 int size = qdict_get_int(qdict, "size");
828 target_long addr = qdict_get_int(qdict, "addr");
829
aliguori376253e2009-03-05 23:01:23 +0000830 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000831}
832
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300833static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000834{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300835 int count = qdict_get_int(qdict, "count");
836 int format = qdict_get_int(qdict, "format");
837 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500838 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300839
aliguori376253e2009-03-05 23:01:23 +0000840 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000841}
842
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300843static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000844{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300845 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500846 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300847
blueswir17743e582007-09-24 18:39:04 +0000848#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000849 switch(format) {
850 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000851 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000852 break;
853 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000854 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000855 break;
856 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000857 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000858 break;
859 default:
860 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000861 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000862 break;
863 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000864 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000865 break;
866 }
bellard92a31b12005-02-10 22:00:52 +0000867#else
868 switch(format) {
869 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000870 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000871 break;
872 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000873 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000874 break;
875 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000876 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000877 break;
878 default:
879 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000880 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000881 break;
882 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000883 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000884 break;
885 }
886#endif
aliguori376253e2009-03-05 23:01:23 +0000887 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000888}
889
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300890static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000891{
892 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300893 uint32_t size = qdict_get_int(qdict, "size");
894 const char *filename = qdict_get_str(qdict, "filename");
895 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000896 uint32_t l;
897 CPUState *env;
898 uint8_t buf[1024];
899
900 env = mon_get_cpu();
901 if (!env)
902 return;
903
904 f = fopen(filename, "wb");
905 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000906 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000907 return;
908 }
909 while (size != 0) {
910 l = sizeof(buf);
911 if (l > size)
912 l = size;
913 cpu_memory_rw_debug(env, addr, buf, l, 0);
914 fwrite(buf, 1, l, f);
915 addr += l;
916 size -= l;
917 }
918 fclose(f);
919}
920
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300921static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000922{
923 FILE *f;
924 uint32_t l;
925 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300926 uint32_t size = qdict_get_int(qdict, "size");
927 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -0500928 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000929
930 f = fopen(filename, "wb");
931 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000932 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000933 return;
934 }
935 while (size != 0) {
936 l = sizeof(buf);
937 if (l > size)
938 l = size;
939 cpu_physical_memory_rw(addr, buf, l, 0);
940 fwrite(buf, 1, l, f);
941 fflush(f);
942 addr += l;
943 size -= l;
944 }
945 fclose(f);
946}
947
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300948static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000949{
950 uint32_t addr;
951 uint8_t buf[1];
952 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300953 uint32_t start = qdict_get_int(qdict, "start");
954 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000955
956 sum = 0;
957 for(addr = start; addr < (start + size); addr++) {
958 cpu_physical_memory_rw(addr, buf, 1, 0);
959 /* BSD sum algorithm ('sum' Unix command) */
960 sum = (sum >> 1) | (sum << 15);
961 sum += buf[0];
962 }
aliguori376253e2009-03-05 23:01:23 +0000963 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000964}
965
bellarda3a91a32004-06-04 11:06:21 +0000966typedef struct {
967 int keycode;
968 const char *name;
969} KeyDef;
970
971static const KeyDef key_defs[] = {
972 { 0x2a, "shift" },
973 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000974
bellarda3a91a32004-06-04 11:06:21 +0000975 { 0x38, "alt" },
976 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000977 { 0x64, "altgr" },
978 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000979 { 0x1d, "ctrl" },
980 { 0x9d, "ctrl_r" },
981
982 { 0xdd, "menu" },
983
984 { 0x01, "esc" },
985
986 { 0x02, "1" },
987 { 0x03, "2" },
988 { 0x04, "3" },
989 { 0x05, "4" },
990 { 0x06, "5" },
991 { 0x07, "6" },
992 { 0x08, "7" },
993 { 0x09, "8" },
994 { 0x0a, "9" },
995 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000996 { 0x0c, "minus" },
997 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000998 { 0x0e, "backspace" },
999
1000 { 0x0f, "tab" },
1001 { 0x10, "q" },
1002 { 0x11, "w" },
1003 { 0x12, "e" },
1004 { 0x13, "r" },
1005 { 0x14, "t" },
1006 { 0x15, "y" },
1007 { 0x16, "u" },
1008 { 0x17, "i" },
1009 { 0x18, "o" },
1010 { 0x19, "p" },
1011
1012 { 0x1c, "ret" },
1013
1014 { 0x1e, "a" },
1015 { 0x1f, "s" },
1016 { 0x20, "d" },
1017 { 0x21, "f" },
1018 { 0x22, "g" },
1019 { 0x23, "h" },
1020 { 0x24, "j" },
1021 { 0x25, "k" },
1022 { 0x26, "l" },
1023
1024 { 0x2c, "z" },
1025 { 0x2d, "x" },
1026 { 0x2e, "c" },
1027 { 0x2f, "v" },
1028 { 0x30, "b" },
1029 { 0x31, "n" },
1030 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001031 { 0x33, "comma" },
1032 { 0x34, "dot" },
1033 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001034
balrog4d3b6f62008-02-10 16:33:14 +00001035 { 0x37, "asterisk" },
1036
bellarda3a91a32004-06-04 11:06:21 +00001037 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001038 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001039 { 0x3b, "f1" },
1040 { 0x3c, "f2" },
1041 { 0x3d, "f3" },
1042 { 0x3e, "f4" },
1043 { 0x3f, "f5" },
1044 { 0x40, "f6" },
1045 { 0x41, "f7" },
1046 { 0x42, "f8" },
1047 { 0x43, "f9" },
1048 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001049 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001050 { 0x46, "scroll_lock" },
1051
bellard64866c32006-05-07 18:03:31 +00001052 { 0xb5, "kp_divide" },
1053 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001054 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001055 { 0x4e, "kp_add" },
1056 { 0x9c, "kp_enter" },
1057 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001058 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001059
1060 { 0x52, "kp_0" },
1061 { 0x4f, "kp_1" },
1062 { 0x50, "kp_2" },
1063 { 0x51, "kp_3" },
1064 { 0x4b, "kp_4" },
1065 { 0x4c, "kp_5" },
1066 { 0x4d, "kp_6" },
1067 { 0x47, "kp_7" },
1068 { 0x48, "kp_8" },
1069 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001070
bellarda3a91a32004-06-04 11:06:21 +00001071 { 0x56, "<" },
1072
1073 { 0x57, "f11" },
1074 { 0x58, "f12" },
1075
1076 { 0xb7, "print" },
1077
1078 { 0xc7, "home" },
1079 { 0xc9, "pgup" },
1080 { 0xd1, "pgdn" },
1081 { 0xcf, "end" },
1082
1083 { 0xcb, "left" },
1084 { 0xc8, "up" },
1085 { 0xd0, "down" },
1086 { 0xcd, "right" },
1087
1088 { 0xd2, "insert" },
1089 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001090#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1091 { 0xf0, "stop" },
1092 { 0xf1, "again" },
1093 { 0xf2, "props" },
1094 { 0xf3, "undo" },
1095 { 0xf4, "front" },
1096 { 0xf5, "copy" },
1097 { 0xf6, "open" },
1098 { 0xf7, "paste" },
1099 { 0xf8, "find" },
1100 { 0xf9, "cut" },
1101 { 0xfa, "lf" },
1102 { 0xfb, "help" },
1103 { 0xfc, "meta_l" },
1104 { 0xfd, "meta_r" },
1105 { 0xfe, "compose" },
1106#endif
bellarda3a91a32004-06-04 11:06:21 +00001107 { 0, NULL },
1108};
1109
1110static int get_keycode(const char *key)
1111{
1112 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001113 char *endp;
1114 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001115
1116 for(p = key_defs; p->name != NULL; p++) {
1117 if (!strcmp(key, p->name))
1118 return p->keycode;
1119 }
bellard64866c32006-05-07 18:03:31 +00001120 if (strstart(key, "0x", NULL)) {
1121 ret = strtoul(key, &endp, 0);
1122 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1123 return ret;
1124 }
bellarda3a91a32004-06-04 11:06:21 +00001125 return -1;
1126}
1127
balrogc8256f92008-06-08 22:45:01 +00001128#define MAX_KEYCODES 16
1129static uint8_t keycodes[MAX_KEYCODES];
1130static int nb_pending_keycodes;
1131static QEMUTimer *key_timer;
1132
1133static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001134{
balrogc8256f92008-06-08 22:45:01 +00001135 int keycode;
1136
1137 while (nb_pending_keycodes > 0) {
1138 nb_pending_keycodes--;
1139 keycode = keycodes[nb_pending_keycodes];
1140 if (keycode & 0x80)
1141 kbd_put_keycode(0xe0);
1142 kbd_put_keycode(keycode | 0x80);
1143 }
1144}
1145
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001146static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001147{
balrog3401c0d2008-06-04 10:05:59 +00001148 char keyname_buf[16];
1149 char *separator;
1150 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001151 const char *string = qdict_get_str(qdict, "string");
1152 int has_hold_time = qdict_haskey(qdict, "hold_time");
1153 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001154
balrogc8256f92008-06-08 22:45:01 +00001155 if (nb_pending_keycodes > 0) {
1156 qemu_del_timer(key_timer);
1157 release_keys(NULL);
1158 }
1159 if (!has_hold_time)
1160 hold_time = 100;
1161 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001162 while (1) {
1163 separator = strchr(string, '-');
1164 keyname_len = separator ? separator - string : strlen(string);
1165 if (keyname_len > 0) {
1166 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1167 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001168 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001169 return;
bellarda3a91a32004-06-04 11:06:21 +00001170 }
balrogc8256f92008-06-08 22:45:01 +00001171 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001173 return;
1174 }
1175 keyname_buf[keyname_len] = 0;
1176 keycode = get_keycode(keyname_buf);
1177 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001178 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001179 return;
1180 }
balrogc8256f92008-06-08 22:45:01 +00001181 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001182 }
balrog3401c0d2008-06-04 10:05:59 +00001183 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001184 break;
balrog3401c0d2008-06-04 10:05:59 +00001185 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001186 }
balrogc8256f92008-06-08 22:45:01 +00001187 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001188 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001189 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001190 keycode = keycodes[i];
1191 if (keycode & 0x80)
1192 kbd_put_keycode(0xe0);
1193 kbd_put_keycode(keycode & 0x7f);
1194 }
balrogc8256f92008-06-08 22:45:01 +00001195 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001196 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001197 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001198}
1199
bellard13224a82006-07-14 22:03:35 +00001200static int mouse_button_state;
1201
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001202static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001203{
1204 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001205 const char *dx_str = qdict_get_str(qdict, "dx_str");
1206 const char *dy_str = qdict_get_str(qdict, "dy_str");
1207 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001208 dx = strtol(dx_str, NULL, 0);
1209 dy = strtol(dy_str, NULL, 0);
1210 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001211 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001212 dz = strtol(dz_str, NULL, 0);
1213 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1214}
1215
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001216static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001217{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001218 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001219 mouse_button_state = button_state;
1220 kbd_mouse_event(0, 0, 0, mouse_button_state);
1221}
1222
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001223static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001224{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001225 int size = qdict_get_int(qdict, "size");
1226 int addr = qdict_get_int(qdict, "addr");
1227 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001228 uint32_t val;
1229 int suffix;
1230
1231 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001232 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001233 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001234 addr++;
1235 }
1236 addr &= 0xffff;
1237
1238 switch(size) {
1239 default:
1240 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001241 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001242 suffix = 'b';
1243 break;
1244 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001245 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001246 suffix = 'w';
1247 break;
1248 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001249 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001250 suffix = 'l';
1251 break;
1252 }
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1254 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001255}
bellarda3a91a32004-06-04 11:06:21 +00001256
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001257static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001258{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001259 int size = qdict_get_int(qdict, "size");
1260 int addr = qdict_get_int(qdict, "addr");
1261 int val = qdict_get_int(qdict, "val");
1262
Jan Kiszkaf1147842009-07-14 10:20:11 +02001263 addr &= IOPORTS_MASK;
1264
1265 switch (size) {
1266 default:
1267 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001268 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001269 break;
1270 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001271 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001272 break;
1273 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001274 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001275 break;
1276 }
1277}
1278
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001279static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001280{
1281 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001282 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001283
Jan Kiszka76e30d02009-07-02 00:19:02 +02001284 res = qemu_boot_set(bootdevice);
1285 if (res == 0) {
1286 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1287 } else if (res > 0) {
1288 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001289 } else {
aliguori376253e2009-03-05 23:01:23 +00001290 monitor_printf(mon, "no function defined to set boot device list for "
1291 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001292 }
1293}
1294
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001295/**
1296 * do_system_reset(): Issue a machine reset
1297 */
1298static void do_system_reset(Monitor *mon, const QDict *qdict,
1299 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001300{
1301 qemu_system_reset_request();
1302}
1303
Luiz Capitulino43076662009-10-07 13:41:59 -03001304/**
1305 * do_system_powerdown(): Issue a machine powerdown
1306 */
1307static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1308 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001309{
1310 qemu_system_powerdown_request();
1311}
1312
bellardb86bda52004-09-18 19:32:46 +00001313#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001314static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001315{
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1317 addr,
1318 pte & mask,
1319 pte & PG_GLOBAL_MASK ? 'G' : '-',
1320 pte & PG_PSE_MASK ? 'P' : '-',
1321 pte & PG_DIRTY_MASK ? 'D' : '-',
1322 pte & PG_ACCESSED_MASK ? 'A' : '-',
1323 pte & PG_PCD_MASK ? 'C' : '-',
1324 pte & PG_PWT_MASK ? 'T' : '-',
1325 pte & PG_USER_MASK ? 'U' : '-',
1326 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001327}
1328
aliguori376253e2009-03-05 23:01:23 +00001329static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001330{
bellard6a00d602005-11-21 23:25:50 +00001331 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001332 int l1, l2;
1333 uint32_t pgd, pde, pte;
1334
bellard6a00d602005-11-21 23:25:50 +00001335 env = mon_get_cpu();
1336 if (!env)
1337 return;
1338
bellardb86bda52004-09-18 19:32:46 +00001339 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001340 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001341 return;
1342 }
1343 pgd = env->cr[3] & ~0xfff;
1344 for(l1 = 0; l1 < 1024; l1++) {
1345 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1346 pde = le32_to_cpu(pde);
1347 if (pde & PG_PRESENT_MASK) {
1348 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001349 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001350 } else {
1351 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001352 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001353 (uint8_t *)&pte, 4);
1354 pte = le32_to_cpu(pte);
1355 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001356 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001357 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001358 ~0xfff);
1359 }
1360 }
1361 }
1362 }
1363 }
1364}
1365
aliguori376253e2009-03-05 23:01:23 +00001366static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001367 uint32_t end, int prot)
1368{
bellard9746b152004-11-11 18:30:24 +00001369 int prot1;
1370 prot1 = *plast_prot;
1371 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001372 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001373 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1374 *pstart, end, end - *pstart,
1375 prot1 & PG_USER_MASK ? 'u' : '-',
1376 'r',
1377 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001378 }
1379 if (prot != 0)
1380 *pstart = end;
1381 else
1382 *pstart = -1;
1383 *plast_prot = prot;
1384 }
1385}
1386
aliguori376253e2009-03-05 23:01:23 +00001387static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001388{
bellard6a00d602005-11-21 23:25:50 +00001389 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001390 int l1, l2, prot, last_prot;
1391 uint32_t pgd, pde, pte, start, end;
1392
bellard6a00d602005-11-21 23:25:50 +00001393 env = mon_get_cpu();
1394 if (!env)
1395 return;
1396
bellardb86bda52004-09-18 19:32:46 +00001397 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001399 return;
1400 }
1401 pgd = env->cr[3] & ~0xfff;
1402 last_prot = 0;
1403 start = -1;
1404 for(l1 = 0; l1 < 1024; l1++) {
1405 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1406 pde = le32_to_cpu(pde);
1407 end = l1 << 22;
1408 if (pde & PG_PRESENT_MASK) {
1409 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1410 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001411 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001412 } else {
1413 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001414 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001415 (uint8_t *)&pte, 4);
1416 pte = le32_to_cpu(pte);
1417 end = (l1 << 22) + (l2 << 12);
1418 if (pte & PG_PRESENT_MASK) {
1419 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1420 } else {
1421 prot = 0;
1422 }
aliguori376253e2009-03-05 23:01:23 +00001423 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001424 }
1425 }
1426 } else {
1427 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001428 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001429 }
1430 }
1431}
1432#endif
1433
aurel327c664e22009-03-03 06:12:22 +00001434#if defined(TARGET_SH4)
1435
aliguori376253e2009-03-05 23:01:23 +00001436static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001437{
aliguori376253e2009-03-05 23:01:23 +00001438 monitor_printf(mon, " tlb%i:\t"
1439 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1440 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1441 "dirty=%hhu writethrough=%hhu\n",
1442 idx,
1443 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1444 tlb->v, tlb->sh, tlb->c, tlb->pr,
1445 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001446}
1447
aliguori376253e2009-03-05 23:01:23 +00001448static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001449{
1450 CPUState *env = mon_get_cpu();
1451 int i;
1452
aliguori376253e2009-03-05 23:01:23 +00001453 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001454 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001455 print_tlb (mon, i, &env->itlb[i]);
1456 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001457 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001458 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001459}
1460
1461#endif
1462
aliguori376253e2009-03-05 23:01:23 +00001463static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001464{
1465#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001466 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001467 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001468 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001469 else
aliguori376253e2009-03-05 23:01:23 +00001470 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001471#else
aliguori376253e2009-03-05 23:01:23 +00001472 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001473#endif
1474}
1475
aliguori030ea372009-04-21 22:30:47 +00001476static void do_info_numa(Monitor *mon)
1477{
aliguorib28b6232009-04-22 20:20:29 +00001478 int i;
aliguori030ea372009-04-21 22:30:47 +00001479 CPUState *env;
1480
1481 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1482 for (i = 0; i < nb_numa_nodes; i++) {
1483 monitor_printf(mon, "node %d cpus:", i);
1484 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1485 if (env->numa_node == i) {
1486 monitor_printf(mon, " %d", env->cpu_index);
1487 }
1488 }
1489 monitor_printf(mon, "\n");
1490 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1491 node_mem[i] >> 20);
1492 }
1493}
1494
bellard5f1ce942006-02-08 22:40:15 +00001495#ifdef CONFIG_PROFILER
1496
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001497int64_t qemu_time;
1498int64_t dev_time;
1499
aliguori376253e2009-03-05 23:01:23 +00001500static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001501{
1502 int64_t total;
1503 total = qemu_time;
1504 if (total == 0)
1505 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001506 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001507 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001508 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001509 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001510 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001511 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001512}
1513#else
aliguori376253e2009-03-05 23:01:23 +00001514static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001515{
aliguori376253e2009-03-05 23:01:23 +00001516 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001517}
1518#endif
1519
bellardec36b692006-07-16 18:57:03 +00001520/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001521static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001522
aliguori376253e2009-03-05 23:01:23 +00001523static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001524{
1525 int i;
1526 CaptureState *s;
1527
1528 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001529 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001530 s->ops.info (s->opaque);
1531 }
1532}
1533
Blue Swirl23130862009-06-06 08:22:04 +00001534#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001535static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001536{
1537 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001538 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001539 CaptureState *s;
1540
1541 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1542 if (i == n) {
1543 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001544 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001545 qemu_free (s);
1546 return;
1547 }
1548 }
1549}
1550
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001551static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001552{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001553 const char *path = qdict_get_str(qdict, "path");
1554 int has_freq = qdict_haskey(qdict, "freq");
1555 int freq = qdict_get_try_int(qdict, "freq", -1);
1556 int has_bits = qdict_haskey(qdict, "bits");
1557 int bits = qdict_get_try_int(qdict, "bits", -1);
1558 int has_channels = qdict_haskey(qdict, "nchannels");
1559 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001560 CaptureState *s;
1561
1562 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001563
1564 freq = has_freq ? freq : 44100;
1565 bits = has_bits ? bits : 16;
1566 nchannels = has_channels ? nchannels : 2;
1567
1568 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001569 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001570 qemu_free (s);
1571 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001572 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001573}
1574#endif
1575
aurel32dc1c0b72008-04-27 23:52:12 +00001576#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001577static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001578{
1579 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001580 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001581
1582 for (env = first_cpu; env != NULL; env = env->next_cpu)
1583 if (env->cpu_index == cpu_index) {
1584 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1585 break;
1586 }
1587}
1588#endif
1589
aliguori376253e2009-03-05 23:01:23 +00001590static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001591{
aurel321b530a62009-04-05 20:08:59 +00001592 if (vm_running) {
1593 if (singlestep) {
1594 monitor_printf(mon, "VM status: running (single step mode)\n");
1595 } else {
1596 monitor_printf(mon, "VM status: running\n");
1597 }
1598 } else
aliguori376253e2009-03-05 23:01:23 +00001599 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001600}
1601
1602
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001603static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001604{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001605 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001606 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001607 qemu_balloon(target << 20);
1608}
1609
aliguori376253e2009-03-05 23:01:23 +00001610static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001611{
Anthony Liguoric227f092009-10-01 16:12:16 -05001612 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001613
1614 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001615 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001616 monitor_printf(mon, "Using KVM without synchronous MMU, "
1617 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001618 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001619 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001620 else
aliguori376253e2009-03-05 23:01:23 +00001621 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001622}
1623
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001624static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001625{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001626 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001627
aliguori76655d62009-03-06 20:27:37 +00001628 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001629 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001630 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001631 return acl;
1632}
aliguori76655d62009-03-06 20:27:37 +00001633
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001634static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001635{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001636 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001637 qemu_acl *acl = find_acl(mon, aclname);
1638 qemu_acl_entry *entry;
1639 int i = 0;
1640
1641 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001642 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001643 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001644 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001645 i++;
1646 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001647 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001648 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001649 }
1650}
1651
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001652static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001653{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001654 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001655 qemu_acl *acl = find_acl(mon, aclname);
1656
1657 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001658 qemu_acl_reset(acl);
1659 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001660 }
1661}
aliguori76655d62009-03-06 20:27:37 +00001662
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001663static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001664{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001665 const char *aclname = qdict_get_str(qdict, "aclname");
1666 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001667 qemu_acl *acl = find_acl(mon, aclname);
1668
1669 if (acl) {
1670 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001671 acl->defaultDeny = 0;
1672 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001673 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001674 acl->defaultDeny = 1;
1675 monitor_printf(mon, "acl: policy set to 'deny'\n");
1676 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001677 monitor_printf(mon, "acl: unknown policy '%s', "
1678 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001679 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001680 }
1681}
aliguori76655d62009-03-06 20:27:37 +00001682
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001683static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001684{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001685 const char *aclname = qdict_get_str(qdict, "aclname");
1686 const char *match = qdict_get_str(qdict, "match");
1687 const char *policy = qdict_get_str(qdict, "policy");
1688 int has_index = qdict_haskey(qdict, "index");
1689 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001690 qemu_acl *acl = find_acl(mon, aclname);
1691 int deny, ret;
1692
1693 if (acl) {
1694 if (strcmp(policy, "allow") == 0) {
1695 deny = 0;
1696 } else if (strcmp(policy, "deny") == 0) {
1697 deny = 1;
1698 } else {
1699 monitor_printf(mon, "acl: unknown policy '%s', "
1700 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001701 return;
1702 }
aliguori28a76be2009-03-06 20:27:40 +00001703 if (has_index)
1704 ret = qemu_acl_insert(acl, deny, match, index);
1705 else
1706 ret = qemu_acl_append(acl, deny, match);
1707 if (ret < 0)
1708 monitor_printf(mon, "acl: unable to add acl entry\n");
1709 else
1710 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001711 }
1712}
aliguori76655d62009-03-06 20:27:37 +00001713
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001714static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001715{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001716 const char *aclname = qdict_get_str(qdict, "aclname");
1717 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001718 qemu_acl *acl = find_acl(mon, aclname);
1719 int ret;
aliguori76655d62009-03-06 20:27:37 +00001720
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001721 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001722 ret = qemu_acl_remove(acl, match);
1723 if (ret < 0)
1724 monitor_printf(mon, "acl: no matching acl entry\n");
1725 else
1726 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001727 }
1728}
1729
Huang Ying79c4f6b2009-06-23 10:05:14 +08001730#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001731static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001732{
1733 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001734 int cpu_index = qdict_get_int(qdict, "cpu_index");
1735 int bank = qdict_get_int(qdict, "bank");
1736 uint64_t status = qdict_get_int(qdict, "status");
1737 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1738 uint64_t addr = qdict_get_int(qdict, "addr");
1739 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001740
1741 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1742 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1743 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1744 break;
1745 }
1746}
1747#endif
1748
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001749static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001750{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001751 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001752 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001753 int fd;
1754
1755 fd = qemu_chr_get_msgfd(mon->chr);
1756 if (fd == -1) {
1757 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1758 return;
1759 }
1760
1761 if (qemu_isdigit(fdname[0])) {
1762 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1763 return;
1764 }
1765
1766 fd = dup(fd);
1767 if (fd == -1) {
1768 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1769 strerror(errno));
1770 return;
1771 }
1772
Blue Swirl72cf2d42009-09-12 07:36:22 +00001773 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001774 if (strcmp(monfd->name, fdname) != 0) {
1775 continue;
1776 }
1777
1778 close(monfd->fd);
1779 monfd->fd = fd;
1780 return;
1781 }
1782
Anthony Liguoric227f092009-10-01 16:12:16 -05001783 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001784 monfd->name = qemu_strdup(fdname);
1785 monfd->fd = fd;
1786
Blue Swirl72cf2d42009-09-12 07:36:22 +00001787 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001788}
1789
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001790static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001791{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001792 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001793 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001794
Blue Swirl72cf2d42009-09-12 07:36:22 +00001795 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001796 if (strcmp(monfd->name, fdname) != 0) {
1797 continue;
1798 }
1799
Blue Swirl72cf2d42009-09-12 07:36:22 +00001800 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001801 close(monfd->fd);
1802 qemu_free(monfd->name);
1803 qemu_free(monfd);
1804 return;
1805 }
1806
1807 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1808 fdname);
1809}
1810
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001811static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001812{
1813 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001814 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001815
1816 vm_stop(0);
1817
Juan Quintela05f24012009-08-20 19:42:22 +02001818 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001819 vm_start();
1820}
1821
Mark McLoughlin7768e042009-07-22 09:11:41 +01001822int monitor_get_fd(Monitor *mon, const char *fdname)
1823{
Anthony Liguoric227f092009-10-01 16:12:16 -05001824 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001825
Blue Swirl72cf2d42009-09-12 07:36:22 +00001826 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001827 int fd;
1828
1829 if (strcmp(monfd->name, fdname) != 0) {
1830 continue;
1831 }
1832
1833 fd = monfd->fd;
1834
1835 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001836 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001837 qemu_free(monfd->name);
1838 qemu_free(monfd);
1839
1840 return fd;
1841 }
1842
1843 return -1;
1844}
1845
Anthony Liguoric227f092009-10-01 16:12:16 -05001846static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001847#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001848 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001849};
1850
Blue Swirl23130862009-06-06 08:22:04 +00001851/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001852static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001853 {
1854 .name = "version",
1855 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001856 .params = "",
1857 .help = "show the version of QEMU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001858 .mhandler.info = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001859 },
1860 {
1861 .name = "network",
1862 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001863 .params = "",
1864 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001865 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001866 },
1867 {
1868 .name = "chardev",
1869 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001870 .params = "",
1871 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001872 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001873 },
1874 {
1875 .name = "block",
1876 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001877 .params = "",
1878 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001879 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001880 },
1881 {
1882 .name = "blockstats",
1883 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001884 .params = "",
1885 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03001886 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001887 },
1888 {
1889 .name = "registers",
1890 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001891 .params = "",
1892 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03001893 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001894 },
1895 {
1896 .name = "cpus",
1897 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001898 .params = "",
1899 .help = "show infos for each CPU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001900 .mhandler.info = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001901 },
1902 {
1903 .name = "history",
1904 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001905 .params = "",
1906 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03001907 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001908 },
1909 {
1910 .name = "irq",
1911 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001912 .params = "",
1913 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03001914 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001915 },
1916 {
1917 .name = "pic",
1918 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001919 .params = "",
1920 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001921 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001922 },
1923 {
1924 .name = "pci",
1925 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001926 .params = "",
1927 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001928 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001929 },
aurel327c664e22009-03-03 06:12:22 +00001930#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001931 {
1932 .name = "tlb",
1933 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001934 .params = "",
1935 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001936 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001937 },
aurel327c664e22009-03-03 06:12:22 +00001938#endif
1939#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001940 {
1941 .name = "mem",
1942 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001943 .params = "",
1944 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001945 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001946 },
1947 {
1948 .name = "hpet",
1949 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001950 .params = "",
1951 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03001952 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001953 },
bellardb86bda52004-09-18 19:32:46 +00001954#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001955 {
1956 .name = "jit",
1957 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001958 .params = "",
1959 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001960 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001961 },
1962 {
1963 .name = "kvm",
1964 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001965 .params = "",
1966 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001967 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001968 },
1969 {
1970 .name = "numa",
1971 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001972 .params = "",
1973 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001974 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001975 },
1976 {
1977 .name = "usb",
1978 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001979 .params = "",
1980 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001981 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001982 },
1983 {
1984 .name = "usbhost",
1985 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001986 .params = "",
1987 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001988 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001989 },
1990 {
1991 .name = "profile",
1992 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001993 .params = "",
1994 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001995 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001996 },
1997 {
1998 .name = "capture",
1999 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002000 .params = "",
2001 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002002 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002003 },
2004 {
2005 .name = "snapshots",
2006 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002007 .params = "",
2008 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002009 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002010 },
2011 {
2012 .name = "status",
2013 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002014 .params = "",
2015 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002016 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002017 },
2018 {
2019 .name = "pcmcia",
2020 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002021 .params = "",
2022 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002023 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002024 },
2025 {
2026 .name = "mice",
2027 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002028 .params = "",
2029 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002030 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002031 },
2032 {
2033 .name = "vnc",
2034 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002035 .params = "",
2036 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002037 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002038 },
2039 {
2040 .name = "name",
2041 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002042 .params = "",
2043 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002044 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002045 },
2046 {
2047 .name = "uuid",
2048 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002049 .params = "",
2050 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002051 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002052 },
j_mayer76a66252007-03-07 08:32:30 +00002053#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002054 {
2055 .name = "cpustats",
2056 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002057 .params = "",
2058 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002059 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002060 },
j_mayer76a66252007-03-07 08:32:30 +00002061#endif
blueswir131a60e22007-10-26 18:42:59 +00002062#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002063 {
2064 .name = "usernet",
2065 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002066 .params = "",
2067 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002068 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002069 },
blueswir131a60e22007-10-26 18:42:59 +00002070#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002071 {
2072 .name = "migrate",
2073 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002074 .params = "",
2075 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002076 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002077 },
2078 {
2079 .name = "balloon",
2080 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002081 .params = "",
2082 .help = "show balloon information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002083 .mhandler.info = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 },
2085 {
2086 .name = "qtree",
2087 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002088 .params = "",
2089 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002090 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002091 },
2092 {
2093 .name = "qdm",
2094 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002095 .params = "",
2096 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002097 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002098 },
2099 {
2100 .name = "roms",
2101 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002102 .params = "",
2103 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002104 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002105 },
2106 {
2107 .name = NULL,
2108 },
bellard9dc39cb2004-03-14 21:38:27 +00002109};
2110
bellard9307c4c2004-04-04 12:57:25 +00002111/*******************************************************************/
2112
2113static const char *pch;
2114static jmp_buf expr_env;
2115
bellard92a31b12005-02-10 22:00:52 +00002116#define MD_TLONG 0
2117#define MD_I32 1
2118
bellard9307c4c2004-04-04 12:57:25 +00002119typedef struct MonitorDef {
2120 const char *name;
2121 int offset;
blueswir18662d652008-10-02 18:32:44 +00002122 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002123 int type;
bellard9307c4c2004-04-04 12:57:25 +00002124} MonitorDef;
2125
bellard57206fd2004-04-25 18:54:52 +00002126#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002127static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002128{
bellard6a00d602005-11-21 23:25:50 +00002129 CPUState *env = mon_get_cpu();
2130 if (!env)
2131 return 0;
2132 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002133}
2134#endif
2135
bellarda541f292004-04-12 20:39:29 +00002136#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002137static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002138{
bellard6a00d602005-11-21 23:25:50 +00002139 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002140 unsigned int u;
2141 int i;
2142
bellard6a00d602005-11-21 23:25:50 +00002143 if (!env)
2144 return 0;
2145
bellarda541f292004-04-12 20:39:29 +00002146 u = 0;
2147 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002148 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002149
2150 return u;
2151}
2152
blueswir18662d652008-10-02 18:32:44 +00002153static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002154{
bellard6a00d602005-11-21 23:25:50 +00002155 CPUState *env = mon_get_cpu();
2156 if (!env)
2157 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002158 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002159}
2160
blueswir18662d652008-10-02 18:32:44 +00002161static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002162{
bellard6a00d602005-11-21 23:25:50 +00002163 CPUState *env = mon_get_cpu();
2164 if (!env)
2165 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002166 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002167}
bellard9fddaa02004-05-21 12:59:32 +00002168
blueswir18662d652008-10-02 18:32:44 +00002169static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002170{
bellard6a00d602005-11-21 23:25:50 +00002171 CPUState *env = mon_get_cpu();
2172 if (!env)
2173 return 0;
2174 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002175}
2176
blueswir18662d652008-10-02 18:32:44 +00002177static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002178{
bellard6a00d602005-11-21 23:25:50 +00002179 CPUState *env = mon_get_cpu();
2180 if (!env)
2181 return 0;
2182 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002183}
2184
blueswir18662d652008-10-02 18:32:44 +00002185static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002186{
bellard6a00d602005-11-21 23:25:50 +00002187 CPUState *env = mon_get_cpu();
2188 if (!env)
2189 return 0;
2190 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002191}
bellarda541f292004-04-12 20:39:29 +00002192#endif
2193
bellarde95c8d52004-09-30 22:22:08 +00002194#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002195#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002196static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002197{
bellard6a00d602005-11-21 23:25:50 +00002198 CPUState *env = mon_get_cpu();
2199 if (!env)
2200 return 0;
2201 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002202}
bellard7b936c02005-10-30 17:05:13 +00002203#endif
bellarde95c8d52004-09-30 22:22:08 +00002204
blueswir18662d652008-10-02 18:32:44 +00002205static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002206{
bellard6a00d602005-11-21 23:25:50 +00002207 CPUState *env = mon_get_cpu();
2208 if (!env)
2209 return 0;
2210 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002211}
2212#endif
2213
blueswir18662d652008-10-02 18:32:44 +00002214static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002215#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002216
2217#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002218 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002219 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002220 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002221
bellard9307c4c2004-04-04 12:57:25 +00002222 { "eax", offsetof(CPUState, regs[0]) },
2223 { "ecx", offsetof(CPUState, regs[1]) },
2224 { "edx", offsetof(CPUState, regs[2]) },
2225 { "ebx", offsetof(CPUState, regs[3]) },
2226 { "esp|sp", offsetof(CPUState, regs[4]) },
2227 { "ebp|fp", offsetof(CPUState, regs[5]) },
2228 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002229 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002230#ifdef TARGET_X86_64
2231 { "r8", offsetof(CPUState, regs[8]) },
2232 { "r9", offsetof(CPUState, regs[9]) },
2233 { "r10", offsetof(CPUState, regs[10]) },
2234 { "r11", offsetof(CPUState, regs[11]) },
2235 { "r12", offsetof(CPUState, regs[12]) },
2236 { "r13", offsetof(CPUState, regs[13]) },
2237 { "r14", offsetof(CPUState, regs[14]) },
2238 { "r15", offsetof(CPUState, regs[15]) },
2239#endif
bellard9307c4c2004-04-04 12:57:25 +00002240 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002241 { "eip", offsetof(CPUState, eip) },
2242 SEG("cs", R_CS)
2243 SEG("ds", R_DS)
2244 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002245 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002246 SEG("fs", R_FS)
2247 SEG("gs", R_GS)
2248 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002249#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002250 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002251 { "r0", offsetof(CPUState, gpr[0]) },
2252 { "r1", offsetof(CPUState, gpr[1]) },
2253 { "r2", offsetof(CPUState, gpr[2]) },
2254 { "r3", offsetof(CPUState, gpr[3]) },
2255 { "r4", offsetof(CPUState, gpr[4]) },
2256 { "r5", offsetof(CPUState, gpr[5]) },
2257 { "r6", offsetof(CPUState, gpr[6]) },
2258 { "r7", offsetof(CPUState, gpr[7]) },
2259 { "r8", offsetof(CPUState, gpr[8]) },
2260 { "r9", offsetof(CPUState, gpr[9]) },
2261 { "r10", offsetof(CPUState, gpr[10]) },
2262 { "r11", offsetof(CPUState, gpr[11]) },
2263 { "r12", offsetof(CPUState, gpr[12]) },
2264 { "r13", offsetof(CPUState, gpr[13]) },
2265 { "r14", offsetof(CPUState, gpr[14]) },
2266 { "r15", offsetof(CPUState, gpr[15]) },
2267 { "r16", offsetof(CPUState, gpr[16]) },
2268 { "r17", offsetof(CPUState, gpr[17]) },
2269 { "r18", offsetof(CPUState, gpr[18]) },
2270 { "r19", offsetof(CPUState, gpr[19]) },
2271 { "r20", offsetof(CPUState, gpr[20]) },
2272 { "r21", offsetof(CPUState, gpr[21]) },
2273 { "r22", offsetof(CPUState, gpr[22]) },
2274 { "r23", offsetof(CPUState, gpr[23]) },
2275 { "r24", offsetof(CPUState, gpr[24]) },
2276 { "r25", offsetof(CPUState, gpr[25]) },
2277 { "r26", offsetof(CPUState, gpr[26]) },
2278 { "r27", offsetof(CPUState, gpr[27]) },
2279 { "r28", offsetof(CPUState, gpr[28]) },
2280 { "r29", offsetof(CPUState, gpr[29]) },
2281 { "r30", offsetof(CPUState, gpr[30]) },
2282 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002283 /* Floating point registers */
2284 { "f0", offsetof(CPUState, fpr[0]) },
2285 { "f1", offsetof(CPUState, fpr[1]) },
2286 { "f2", offsetof(CPUState, fpr[2]) },
2287 { "f3", offsetof(CPUState, fpr[3]) },
2288 { "f4", offsetof(CPUState, fpr[4]) },
2289 { "f5", offsetof(CPUState, fpr[5]) },
2290 { "f6", offsetof(CPUState, fpr[6]) },
2291 { "f7", offsetof(CPUState, fpr[7]) },
2292 { "f8", offsetof(CPUState, fpr[8]) },
2293 { "f9", offsetof(CPUState, fpr[9]) },
2294 { "f10", offsetof(CPUState, fpr[10]) },
2295 { "f11", offsetof(CPUState, fpr[11]) },
2296 { "f12", offsetof(CPUState, fpr[12]) },
2297 { "f13", offsetof(CPUState, fpr[13]) },
2298 { "f14", offsetof(CPUState, fpr[14]) },
2299 { "f15", offsetof(CPUState, fpr[15]) },
2300 { "f16", offsetof(CPUState, fpr[16]) },
2301 { "f17", offsetof(CPUState, fpr[17]) },
2302 { "f18", offsetof(CPUState, fpr[18]) },
2303 { "f19", offsetof(CPUState, fpr[19]) },
2304 { "f20", offsetof(CPUState, fpr[20]) },
2305 { "f21", offsetof(CPUState, fpr[21]) },
2306 { "f22", offsetof(CPUState, fpr[22]) },
2307 { "f23", offsetof(CPUState, fpr[23]) },
2308 { "f24", offsetof(CPUState, fpr[24]) },
2309 { "f25", offsetof(CPUState, fpr[25]) },
2310 { "f26", offsetof(CPUState, fpr[26]) },
2311 { "f27", offsetof(CPUState, fpr[27]) },
2312 { "f28", offsetof(CPUState, fpr[28]) },
2313 { "f29", offsetof(CPUState, fpr[29]) },
2314 { "f30", offsetof(CPUState, fpr[30]) },
2315 { "f31", offsetof(CPUState, fpr[31]) },
2316 { "fpscr", offsetof(CPUState, fpscr) },
2317 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002318 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002319 { "lr", offsetof(CPUState, lr) },
2320 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002321 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002322 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002323 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002324 { "msr", 0, &monitor_get_msr, },
2325 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002326 { "tbu", 0, &monitor_get_tbu, },
2327 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002328#if defined(TARGET_PPC64)
2329 /* Address space register */
2330 { "asr", offsetof(CPUState, asr) },
2331#endif
2332 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002333 { "sdr1", offsetof(CPUState, sdr1) },
2334 { "sr0", offsetof(CPUState, sr[0]) },
2335 { "sr1", offsetof(CPUState, sr[1]) },
2336 { "sr2", offsetof(CPUState, sr[2]) },
2337 { "sr3", offsetof(CPUState, sr[3]) },
2338 { "sr4", offsetof(CPUState, sr[4]) },
2339 { "sr5", offsetof(CPUState, sr[5]) },
2340 { "sr6", offsetof(CPUState, sr[6]) },
2341 { "sr7", offsetof(CPUState, sr[7]) },
2342 { "sr8", offsetof(CPUState, sr[8]) },
2343 { "sr9", offsetof(CPUState, sr[9]) },
2344 { "sr10", offsetof(CPUState, sr[10]) },
2345 { "sr11", offsetof(CPUState, sr[11]) },
2346 { "sr12", offsetof(CPUState, sr[12]) },
2347 { "sr13", offsetof(CPUState, sr[13]) },
2348 { "sr14", offsetof(CPUState, sr[14]) },
2349 { "sr15", offsetof(CPUState, sr[15]) },
2350 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002351#elif defined(TARGET_SPARC)
2352 { "g0", offsetof(CPUState, gregs[0]) },
2353 { "g1", offsetof(CPUState, gregs[1]) },
2354 { "g2", offsetof(CPUState, gregs[2]) },
2355 { "g3", offsetof(CPUState, gregs[3]) },
2356 { "g4", offsetof(CPUState, gregs[4]) },
2357 { "g5", offsetof(CPUState, gregs[5]) },
2358 { "g6", offsetof(CPUState, gregs[6]) },
2359 { "g7", offsetof(CPUState, gregs[7]) },
2360 { "o0", 0, monitor_get_reg },
2361 { "o1", 1, monitor_get_reg },
2362 { "o2", 2, monitor_get_reg },
2363 { "o3", 3, monitor_get_reg },
2364 { "o4", 4, monitor_get_reg },
2365 { "o5", 5, monitor_get_reg },
2366 { "o6", 6, monitor_get_reg },
2367 { "o7", 7, monitor_get_reg },
2368 { "l0", 8, monitor_get_reg },
2369 { "l1", 9, monitor_get_reg },
2370 { "l2", 10, monitor_get_reg },
2371 { "l3", 11, monitor_get_reg },
2372 { "l4", 12, monitor_get_reg },
2373 { "l5", 13, monitor_get_reg },
2374 { "l6", 14, monitor_get_reg },
2375 { "l7", 15, monitor_get_reg },
2376 { "i0", 16, monitor_get_reg },
2377 { "i1", 17, monitor_get_reg },
2378 { "i2", 18, monitor_get_reg },
2379 { "i3", 19, monitor_get_reg },
2380 { "i4", 20, monitor_get_reg },
2381 { "i5", 21, monitor_get_reg },
2382 { "i6", 22, monitor_get_reg },
2383 { "i7", 23, monitor_get_reg },
2384 { "pc", offsetof(CPUState, pc) },
2385 { "npc", offsetof(CPUState, npc) },
2386 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002387#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002388 { "psr", 0, &monitor_get_psr, },
2389 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002390#endif
bellarde95c8d52004-09-30 22:22:08 +00002391 { "tbr", offsetof(CPUState, tbr) },
2392 { "fsr", offsetof(CPUState, fsr) },
2393 { "f0", offsetof(CPUState, fpr[0]) },
2394 { "f1", offsetof(CPUState, fpr[1]) },
2395 { "f2", offsetof(CPUState, fpr[2]) },
2396 { "f3", offsetof(CPUState, fpr[3]) },
2397 { "f4", offsetof(CPUState, fpr[4]) },
2398 { "f5", offsetof(CPUState, fpr[5]) },
2399 { "f6", offsetof(CPUState, fpr[6]) },
2400 { "f7", offsetof(CPUState, fpr[7]) },
2401 { "f8", offsetof(CPUState, fpr[8]) },
2402 { "f9", offsetof(CPUState, fpr[9]) },
2403 { "f10", offsetof(CPUState, fpr[10]) },
2404 { "f11", offsetof(CPUState, fpr[11]) },
2405 { "f12", offsetof(CPUState, fpr[12]) },
2406 { "f13", offsetof(CPUState, fpr[13]) },
2407 { "f14", offsetof(CPUState, fpr[14]) },
2408 { "f15", offsetof(CPUState, fpr[15]) },
2409 { "f16", offsetof(CPUState, fpr[16]) },
2410 { "f17", offsetof(CPUState, fpr[17]) },
2411 { "f18", offsetof(CPUState, fpr[18]) },
2412 { "f19", offsetof(CPUState, fpr[19]) },
2413 { "f20", offsetof(CPUState, fpr[20]) },
2414 { "f21", offsetof(CPUState, fpr[21]) },
2415 { "f22", offsetof(CPUState, fpr[22]) },
2416 { "f23", offsetof(CPUState, fpr[23]) },
2417 { "f24", offsetof(CPUState, fpr[24]) },
2418 { "f25", offsetof(CPUState, fpr[25]) },
2419 { "f26", offsetof(CPUState, fpr[26]) },
2420 { "f27", offsetof(CPUState, fpr[27]) },
2421 { "f28", offsetof(CPUState, fpr[28]) },
2422 { "f29", offsetof(CPUState, fpr[29]) },
2423 { "f30", offsetof(CPUState, fpr[30]) },
2424 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002425#ifdef TARGET_SPARC64
2426 { "f32", offsetof(CPUState, fpr[32]) },
2427 { "f34", offsetof(CPUState, fpr[34]) },
2428 { "f36", offsetof(CPUState, fpr[36]) },
2429 { "f38", offsetof(CPUState, fpr[38]) },
2430 { "f40", offsetof(CPUState, fpr[40]) },
2431 { "f42", offsetof(CPUState, fpr[42]) },
2432 { "f44", offsetof(CPUState, fpr[44]) },
2433 { "f46", offsetof(CPUState, fpr[46]) },
2434 { "f48", offsetof(CPUState, fpr[48]) },
2435 { "f50", offsetof(CPUState, fpr[50]) },
2436 { "f52", offsetof(CPUState, fpr[52]) },
2437 { "f54", offsetof(CPUState, fpr[54]) },
2438 { "f56", offsetof(CPUState, fpr[56]) },
2439 { "f58", offsetof(CPUState, fpr[58]) },
2440 { "f60", offsetof(CPUState, fpr[60]) },
2441 { "f62", offsetof(CPUState, fpr[62]) },
2442 { "asi", offsetof(CPUState, asi) },
2443 { "pstate", offsetof(CPUState, pstate) },
2444 { "cansave", offsetof(CPUState, cansave) },
2445 { "canrestore", offsetof(CPUState, canrestore) },
2446 { "otherwin", offsetof(CPUState, otherwin) },
2447 { "wstate", offsetof(CPUState, wstate) },
2448 { "cleanwin", offsetof(CPUState, cleanwin) },
2449 { "fprs", offsetof(CPUState, fprs) },
2450#endif
bellard9307c4c2004-04-04 12:57:25 +00002451#endif
2452 { NULL },
2453};
2454
aliguori376253e2009-03-05 23:01:23 +00002455static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002456{
aliguori376253e2009-03-05 23:01:23 +00002457 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002458 longjmp(expr_env, 1);
2459}
2460
bellard6a00d602005-11-21 23:25:50 +00002461/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002462static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002463{
blueswir18662d652008-10-02 18:32:44 +00002464 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002465 void *ptr;
2466
bellard9307c4c2004-04-04 12:57:25 +00002467 for(md = monitor_defs; md->name != NULL; md++) {
2468 if (compare_cmd(name, md->name)) {
2469 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002470 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002471 } else {
bellard6a00d602005-11-21 23:25:50 +00002472 CPUState *env = mon_get_cpu();
2473 if (!env)
2474 return -2;
2475 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002476 switch(md->type) {
2477 case MD_I32:
2478 *pval = *(int32_t *)ptr;
2479 break;
2480 case MD_TLONG:
2481 *pval = *(target_long *)ptr;
2482 break;
2483 default:
2484 *pval = 0;
2485 break;
2486 }
bellard9307c4c2004-04-04 12:57:25 +00002487 }
2488 return 0;
2489 }
2490 }
2491 return -1;
2492}
2493
2494static void next(void)
2495{
Blue Swirl660f11b2009-07-31 21:16:51 +00002496 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002497 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002498 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002499 pch++;
2500 }
2501}
2502
aliguori376253e2009-03-05 23:01:23 +00002503static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002504
aliguori376253e2009-03-05 23:01:23 +00002505static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002506{
blueswir1c2efc952007-09-25 17:28:42 +00002507 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002508 char *p;
bellard6a00d602005-11-21 23:25:50 +00002509 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002510
2511 switch(*pch) {
2512 case '+':
2513 next();
aliguori376253e2009-03-05 23:01:23 +00002514 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002515 break;
2516 case '-':
2517 next();
aliguori376253e2009-03-05 23:01:23 +00002518 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002519 break;
2520 case '~':
2521 next();
aliguori376253e2009-03-05 23:01:23 +00002522 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002523 break;
2524 case '(':
2525 next();
aliguori376253e2009-03-05 23:01:23 +00002526 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002527 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002528 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002529 }
2530 next();
2531 break;
bellard81d09122004-07-14 17:21:37 +00002532 case '\'':
2533 pch++;
2534 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002535 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002536 n = *pch;
2537 pch++;
2538 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002539 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002540 next();
2541 break;
bellard9307c4c2004-04-04 12:57:25 +00002542 case '$':
2543 {
2544 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002545 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002546
bellard9307c4c2004-04-04 12:57:25 +00002547 pch++;
2548 q = buf;
2549 while ((*pch >= 'a' && *pch <= 'z') ||
2550 (*pch >= 'A' && *pch <= 'Z') ||
2551 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002552 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002553 if ((q - buf) < sizeof(buf) - 1)
2554 *q++ = *pch;
2555 pch++;
2556 }
blueswir1cd390082008-11-16 13:53:32 +00002557 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002558 pch++;
2559 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002560 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002561 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002562 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002563 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002564 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002565 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002566 }
2567 break;
2568 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002569 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002570 n = 0;
2571 break;
2572 default:
blueswir17743e582007-09-24 18:39:04 +00002573#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002574 n = strtoull(pch, &p, 0);
2575#else
bellard9307c4c2004-04-04 12:57:25 +00002576 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002577#endif
bellard9307c4c2004-04-04 12:57:25 +00002578 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002579 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002580 }
2581 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002582 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002583 pch++;
2584 break;
2585 }
2586 return n;
2587}
2588
2589
aliguori376253e2009-03-05 23:01:23 +00002590static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002591{
blueswir1c2efc952007-09-25 17:28:42 +00002592 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002593 int op;
ths3b46e622007-09-17 08:09:54 +00002594
aliguori376253e2009-03-05 23:01:23 +00002595 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002596 for(;;) {
2597 op = *pch;
2598 if (op != '*' && op != '/' && op != '%')
2599 break;
2600 next();
aliguori376253e2009-03-05 23:01:23 +00002601 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002602 switch(op) {
2603 default:
2604 case '*':
2605 val *= val2;
2606 break;
2607 case '/':
2608 case '%':
ths5fafdf22007-09-16 21:08:06 +00002609 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002610 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002611 if (op == '/')
2612 val /= val2;
2613 else
2614 val %= val2;
2615 break;
2616 }
2617 }
2618 return val;
2619}
2620
aliguori376253e2009-03-05 23:01:23 +00002621static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002622{
blueswir1c2efc952007-09-25 17:28:42 +00002623 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002624 int op;
bellard9307c4c2004-04-04 12:57:25 +00002625
aliguori376253e2009-03-05 23:01:23 +00002626 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002627 for(;;) {
2628 op = *pch;
2629 if (op != '&' && op != '|' && op != '^')
2630 break;
2631 next();
aliguori376253e2009-03-05 23:01:23 +00002632 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002633 switch(op) {
2634 default:
2635 case '&':
2636 val &= val2;
2637 break;
2638 case '|':
2639 val |= val2;
2640 break;
2641 case '^':
2642 val ^= val2;
2643 break;
2644 }
2645 }
2646 return val;
2647}
2648
aliguori376253e2009-03-05 23:01:23 +00002649static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002650{
blueswir1c2efc952007-09-25 17:28:42 +00002651 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002652 int op;
bellard9307c4c2004-04-04 12:57:25 +00002653
aliguori376253e2009-03-05 23:01:23 +00002654 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002655 for(;;) {
2656 op = *pch;
2657 if (op != '+' && op != '-')
2658 break;
2659 next();
aliguori376253e2009-03-05 23:01:23 +00002660 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002661 if (op == '+')
2662 val += val2;
2663 else
2664 val -= val2;
2665 }
2666 return val;
2667}
2668
aliguori376253e2009-03-05 23:01:23 +00002669static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002670{
2671 pch = *pp;
2672 if (setjmp(expr_env)) {
2673 *pp = pch;
2674 return -1;
2675 }
blueswir1cd390082008-11-16 13:53:32 +00002676 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002677 pch++;
aliguori376253e2009-03-05 23:01:23 +00002678 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002679 *pp = pch;
2680 return 0;
2681}
2682
2683static int get_str(char *buf, int buf_size, const char **pp)
2684{
2685 const char *p;
2686 char *q;
2687 int c;
2688
bellard81d09122004-07-14 17:21:37 +00002689 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002690 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002691 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002692 p++;
2693 if (*p == '\0') {
2694 fail:
bellard81d09122004-07-14 17:21:37 +00002695 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002696 *pp = p;
2697 return -1;
2698 }
bellard9307c4c2004-04-04 12:57:25 +00002699 if (*p == '\"') {
2700 p++;
2701 while (*p != '\0' && *p != '\"') {
2702 if (*p == '\\') {
2703 p++;
2704 c = *p++;
2705 switch(c) {
2706 case 'n':
2707 c = '\n';
2708 break;
2709 case 'r':
2710 c = '\r';
2711 break;
2712 case '\\':
2713 case '\'':
2714 case '\"':
2715 break;
2716 default:
2717 qemu_printf("unsupported escape code: '\\%c'\n", c);
2718 goto fail;
2719 }
2720 if ((q - buf) < buf_size - 1) {
2721 *q++ = c;
2722 }
2723 } else {
2724 if ((q - buf) < buf_size - 1) {
2725 *q++ = *p;
2726 }
2727 p++;
2728 }
2729 }
2730 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002731 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002732 goto fail;
2733 }
2734 p++;
2735 } else {
blueswir1cd390082008-11-16 13:53:32 +00002736 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002737 if ((q - buf) < buf_size - 1) {
2738 *q++ = *p;
2739 }
2740 p++;
2741 }
bellard9307c4c2004-04-04 12:57:25 +00002742 }
bellard81d09122004-07-14 17:21:37 +00002743 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002744 *pp = p;
2745 return 0;
2746}
2747
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002748/*
2749 * Store the command-name in cmdname, and return a pointer to
2750 * the remaining of the command string.
2751 */
2752static const char *get_command_name(const char *cmdline,
2753 char *cmdname, size_t nlen)
2754{
2755 size_t len;
2756 const char *p, *pstart;
2757
2758 p = cmdline;
2759 while (qemu_isspace(*p))
2760 p++;
2761 if (*p == '\0')
2762 return NULL;
2763 pstart = p;
2764 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2765 p++;
2766 len = p - pstart;
2767 if (len > nlen - 1)
2768 len = nlen - 1;
2769 memcpy(cmdname, pstart, len);
2770 cmdname[len] = '\0';
2771 return p;
2772}
2773
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002774/**
2775 * Read key of 'type' into 'key' and return the current
2776 * 'type' pointer.
2777 */
2778static char *key_get_info(const char *type, char **key)
2779{
2780 size_t len;
2781 char *p, *str;
2782
2783 if (*type == ',')
2784 type++;
2785
2786 p = strchr(type, ':');
2787 if (!p) {
2788 *key = NULL;
2789 return NULL;
2790 }
2791 len = p - type;
2792
2793 str = qemu_malloc(len + 1);
2794 memcpy(str, type, len);
2795 str[len] = '\0';
2796
2797 *key = str;
2798 return ++p;
2799}
2800
bellard9307c4c2004-04-04 12:57:25 +00002801static int default_fmt_format = 'x';
2802static int default_fmt_size = 4;
2803
2804#define MAX_ARGS 16
2805
Anthony Liguoric227f092009-10-01 16:12:16 -05002806static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002807 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002808 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002809{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002810 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002811 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002812 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002813 char cmdname[256];
2814 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002815 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002816
2817#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002818 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002819#endif
ths3b46e622007-09-17 08:09:54 +00002820
bellard9307c4c2004-04-04 12:57:25 +00002821 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002822 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2823 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002824 return NULL;
ths3b46e622007-09-17 08:09:54 +00002825
bellard9307c4c2004-04-04 12:57:25 +00002826 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002827 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002828 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002829 break;
bellard9dc39cb2004-03-14 21:38:27 +00002830 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002831
2832 if (cmd->name == NULL) {
2833 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002834 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002835 }
bellard9307c4c2004-04-04 12:57:25 +00002836
bellard9307c4c2004-04-04 12:57:25 +00002837 /* parse the parameters */
2838 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002839 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002840 typestr = key_get_info(typestr, &key);
2841 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002842 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002843 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002844 typestr++;
2845 switch(c) {
2846 case 'F':
bellard81d09122004-07-14 17:21:37 +00002847 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002848 case 's':
2849 {
2850 int ret;
ths3b46e622007-09-17 08:09:54 +00002851
blueswir1cd390082008-11-16 13:53:32 +00002852 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002853 p++;
2854 if (*typestr == '?') {
2855 typestr++;
2856 if (*p == '\0') {
2857 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002858 break;
bellard9307c4c2004-04-04 12:57:25 +00002859 }
2860 }
2861 ret = get_str(buf, sizeof(buf), &p);
2862 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002863 switch(c) {
2864 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002865 monitor_printf(mon, "%s: filename expected\n",
2866 cmdname);
bellard81d09122004-07-14 17:21:37 +00002867 break;
2868 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002869 monitor_printf(mon, "%s: block device name expected\n",
2870 cmdname);
bellard81d09122004-07-14 17:21:37 +00002871 break;
2872 default:
aliguori376253e2009-03-05 23:01:23 +00002873 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002874 break;
2875 }
bellard9307c4c2004-04-04 12:57:25 +00002876 goto fail;
2877 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002878 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002879 }
2880 break;
2881 case '/':
2882 {
2883 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002884
blueswir1cd390082008-11-16 13:53:32 +00002885 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002886 p++;
2887 if (*p == '/') {
2888 /* format found */
2889 p++;
2890 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002891 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002892 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002893 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002894 count = count * 10 + (*p - '0');
2895 p++;
2896 }
2897 }
2898 size = -1;
2899 format = -1;
2900 for(;;) {
2901 switch(*p) {
2902 case 'o':
2903 case 'd':
2904 case 'u':
2905 case 'x':
2906 case 'i':
2907 case 'c':
2908 format = *p++;
2909 break;
2910 case 'b':
2911 size = 1;
2912 p++;
2913 break;
2914 case 'h':
2915 size = 2;
2916 p++;
2917 break;
2918 case 'w':
2919 size = 4;
2920 p++;
2921 break;
2922 case 'g':
2923 case 'L':
2924 size = 8;
2925 p++;
2926 break;
2927 default:
2928 goto next;
2929 }
2930 }
2931 next:
blueswir1cd390082008-11-16 13:53:32 +00002932 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002933 monitor_printf(mon, "invalid char in format: '%c'\n",
2934 *p);
bellard9307c4c2004-04-04 12:57:25 +00002935 goto fail;
2936 }
bellard9307c4c2004-04-04 12:57:25 +00002937 if (format < 0)
2938 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002939 if (format != 'i') {
2940 /* for 'i', not specifying a size gives -1 as size */
2941 if (size < 0)
2942 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002943 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002944 }
bellard9307c4c2004-04-04 12:57:25 +00002945 default_fmt_format = format;
2946 } else {
2947 count = 1;
2948 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002949 if (format != 'i') {
2950 size = default_fmt_size;
2951 } else {
2952 size = -1;
2953 }
bellard9307c4c2004-04-04 12:57:25 +00002954 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002955 qdict_put(qdict, "count", qint_from_int(count));
2956 qdict_put(qdict, "format", qint_from_int(format));
2957 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002958 }
2959 break;
2960 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002961 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002962 {
blueswir1c2efc952007-09-25 17:28:42 +00002963 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002964
blueswir1cd390082008-11-16 13:53:32 +00002965 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002966 p++;
bellard34405572004-06-08 00:55:58 +00002967 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002968 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002969 if (*p == '\0') {
2970 typestr++;
2971 break;
2972 }
bellard34405572004-06-08 00:55:58 +00002973 } else {
2974 if (*p == '.') {
2975 p++;
blueswir1cd390082008-11-16 13:53:32 +00002976 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002977 p++;
bellard34405572004-06-08 00:55:58 +00002978 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002979 typestr++;
2980 break;
bellard34405572004-06-08 00:55:58 +00002981 }
2982 }
bellard13224a82006-07-14 22:03:35 +00002983 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002984 }
aliguori376253e2009-03-05 23:01:23 +00002985 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002986 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002987 /* Check if 'i' is greater than 32-bit */
2988 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2989 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2990 monitor_printf(mon, "integer is for 32-bit values\n");
2991 goto fail;
2992 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002993 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002994 }
2995 break;
2996 case '-':
2997 {
2998 int has_option;
2999 /* option */
ths3b46e622007-09-17 08:09:54 +00003000
bellard9307c4c2004-04-04 12:57:25 +00003001 c = *typestr++;
3002 if (c == '\0')
3003 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003004 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003005 p++;
3006 has_option = 0;
3007 if (*p == '-') {
3008 p++;
3009 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00003010 monitor_printf(mon, "%s: unsupported option -%c\n",
3011 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00003012 goto fail;
3013 }
3014 p++;
3015 has_option = 1;
3016 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003017 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003018 }
3019 break;
3020 default:
3021 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003022 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003023 goto fail;
3024 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003025 qemu_free(key);
3026 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003027 }
3028 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003029 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003030 p++;
3031 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003032 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3033 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003034 goto fail;
3035 }
3036
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003037 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003038
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003039fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003040 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003041 return NULL;
3042}
3043
3044static void monitor_handle_command(Monitor *mon, const char *cmdline)
3045{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003046 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003047 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003048
3049 qdict = qdict_new();
3050
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003051 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003052 if (!cmd)
3053 goto out;
3054
3055 qemu_errors_to_mon(mon);
3056
3057 if (monitor_handler_ported(cmd)) {
3058 QObject *data = NULL;
3059
3060 cmd->mhandler.cmd_new(mon, qdict, &data);
3061 if (data)
3062 cmd->user_print(mon, data);
3063
3064 qobject_decref(data);
3065 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003066 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003067 }
3068
Luiz Capitulino13917be2009-10-07 13:41:54 -03003069 qemu_errors_to_previous();
3070
3071out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003072 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003073}
3074
bellard81d09122004-07-14 17:21:37 +00003075static void cmd_completion(const char *name, const char *list)
3076{
3077 const char *p, *pstart;
3078 char cmd[128];
3079 int len;
3080
3081 p = list;
3082 for(;;) {
3083 pstart = p;
3084 p = strchr(p, '|');
3085 if (!p)
3086 p = pstart + strlen(pstart);
3087 len = p - pstart;
3088 if (len > sizeof(cmd) - 2)
3089 len = sizeof(cmd) - 2;
3090 memcpy(cmd, pstart, len);
3091 cmd[len] = '\0';
3092 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003093 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003094 }
3095 if (*p == '\0')
3096 break;
3097 p++;
3098 }
3099}
3100
3101static void file_completion(const char *input)
3102{
3103 DIR *ffs;
3104 struct dirent *d;
3105 char path[1024];
3106 char file[1024], file_prefix[1024];
3107 int input_path_len;
3108 const char *p;
3109
ths5fafdf22007-09-16 21:08:06 +00003110 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003111 if (!p) {
3112 input_path_len = 0;
3113 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003114 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003115 } else {
3116 input_path_len = p - input + 1;
3117 memcpy(path, input, input_path_len);
3118 if (input_path_len > sizeof(path) - 1)
3119 input_path_len = sizeof(path) - 1;
3120 path[input_path_len] = '\0';
3121 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3122 }
3123#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003124 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3125 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003126#endif
3127 ffs = opendir(path);
3128 if (!ffs)
3129 return;
3130 for(;;) {
3131 struct stat sb;
3132 d = readdir(ffs);
3133 if (!d)
3134 break;
3135 if (strstart(d->d_name, file_prefix, NULL)) {
3136 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003137 if (input_path_len < sizeof(file))
3138 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3139 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003140 /* stat the file to find out if it's a directory.
3141 * In that case add a slash to speed up typing long paths
3142 */
3143 stat(file, &sb);
3144 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003145 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003146 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003147 }
3148 }
3149 closedir(ffs);
3150}
3151
aliguori51de9762009-03-05 23:00:43 +00003152static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003153{
aliguori51de9762009-03-05 23:00:43 +00003154 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003155 const char *input = opaque;
3156
3157 if (input[0] == '\0' ||
3158 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003159 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003160 }
3161}
3162
3163/* NOTE: this parser is an approximate form of the real command parser */
3164static void parse_cmdline(const char *cmdline,
3165 int *pnb_args, char **args)
3166{
3167 const char *p;
3168 int nb_args, ret;
3169 char buf[1024];
3170
3171 p = cmdline;
3172 nb_args = 0;
3173 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003174 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003175 p++;
3176 if (*p == '\0')
3177 break;
3178 if (nb_args >= MAX_ARGS)
3179 break;
3180 ret = get_str(buf, sizeof(buf), &p);
3181 args[nb_args] = qemu_strdup(buf);
3182 nb_args++;
3183 if (ret < 0)
3184 break;
3185 }
3186 *pnb_args = nb_args;
3187}
3188
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003189static const char *next_arg_type(const char *typestr)
3190{
3191 const char *p = strchr(typestr, ':');
3192 return (p != NULL ? ++p : typestr);
3193}
3194
aliguori4c36ba32009-03-05 23:01:37 +00003195static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003196{
3197 const char *cmdname;
3198 char *args[MAX_ARGS];
3199 int nb_args, i, len;
3200 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003201 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003202 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003203
3204 parse_cmdline(cmdline, &nb_args, args);
3205#ifdef DEBUG_COMPLETION
3206 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003207 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003208 }
3209#endif
3210
3211 /* if the line ends with a space, it means we want to complete the
3212 next arg */
3213 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003214 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003215 if (nb_args >= MAX_ARGS)
3216 return;
3217 args[nb_args++] = qemu_strdup("");
3218 }
3219 if (nb_args <= 1) {
3220 /* command completion */
3221 if (nb_args == 0)
3222 cmdname = "";
3223 else
3224 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003225 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003226 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003227 cmd_completion(cmdname, cmd->name);
3228 }
3229 } else {
3230 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003231 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003232 if (compare_cmd(args[0], cmd->name))
3233 goto found;
3234 }
3235 return;
3236 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003237 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003238 for(i = 0; i < nb_args - 2; i++) {
3239 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003240 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003241 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003242 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003243 }
3244 }
3245 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003246 if (*ptype == '-' && ptype[1] != '\0') {
3247 ptype += 2;
3248 }
bellard81d09122004-07-14 17:21:37 +00003249 switch(*ptype) {
3250 case 'F':
3251 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003252 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003253 file_completion(str);
3254 break;
3255 case 'B':
3256 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003257 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003258 bdrv_iterate(block_completion_it, (void *)str);
3259 break;
bellard7fe48482004-10-09 18:08:01 +00003260 case 's':
3261 /* XXX: more generic ? */
3262 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003263 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003264 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3265 cmd_completion(str, cmd->name);
3266 }
bellard64866c32006-05-07 18:03:31 +00003267 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003268 char *sep = strrchr(str, '-');
3269 if (sep)
3270 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003271 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003272 for(key = key_defs; key->name != NULL; key++) {
3273 cmd_completion(str, key->name);
3274 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003275 } else if (!strcmp(cmd->name, "help|?")) {
3276 readline_set_completion_index(cur_mon->rs, strlen(str));
3277 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3278 cmd_completion(str, cmd->name);
3279 }
bellard7fe48482004-10-09 18:08:01 +00003280 }
3281 break;
bellard81d09122004-07-14 17:21:37 +00003282 default:
3283 break;
3284 }
3285 }
3286 for(i = 0; i < nb_args; i++)
3287 qemu_free(args[i]);
3288}
3289
aliguori731b0362009-03-05 23:01:42 +00003290static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003291{
aliguori731b0362009-03-05 23:01:42 +00003292 Monitor *mon = opaque;
3293
3294 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003295}
3296
aliguori731b0362009-03-05 23:01:42 +00003297static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003298{
aliguori731b0362009-03-05 23:01:42 +00003299 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003300 int i;
aliguori376253e2009-03-05 23:01:23 +00003301
aliguori731b0362009-03-05 23:01:42 +00003302 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003303
aliguoricde76ee2009-03-05 23:01:51 +00003304 if (cur_mon->rs) {
3305 for (i = 0; i < size; i++)
3306 readline_handle_byte(cur_mon->rs, buf[i]);
3307 } else {
3308 if (size == 0 || buf[size - 1] != 0)
3309 monitor_printf(cur_mon, "corrupted command\n");
3310 else
3311 monitor_handle_command(cur_mon, (char *)buf);
3312 }
aliguori731b0362009-03-05 23:01:42 +00003313
3314 cur_mon = old_mon;
3315}
aliguorid8f44602008-10-06 13:52:44 +00003316
aliguori376253e2009-03-05 23:01:23 +00003317static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003318{
aliguori731b0362009-03-05 23:01:42 +00003319 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003320 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003321 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003322}
3323
aliguoricde76ee2009-03-05 23:01:51 +00003324int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003325{
aliguoricde76ee2009-03-05 23:01:51 +00003326 if (!mon->rs)
3327 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003328 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003329 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003330}
3331
aliguori376253e2009-03-05 23:01:23 +00003332void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003333{
aliguoricde76ee2009-03-05 23:01:51 +00003334 if (!mon->rs)
3335 return;
aliguori731b0362009-03-05 23:01:42 +00003336 if (--mon->suspend_cnt == 0)
3337 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003338}
3339
aliguori731b0362009-03-05 23:01:42 +00003340static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003341{
aliguori376253e2009-03-05 23:01:23 +00003342 Monitor *mon = opaque;
3343
aliguori2724b182009-03-05 23:01:47 +00003344 switch (event) {
3345 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003346 mon->mux_out = 0;
3347 if (mon->reset_seen) {
3348 readline_restart(mon->rs);
3349 monitor_resume(mon);
3350 monitor_flush(mon);
3351 } else {
3352 mon->suspend_cnt = 0;
3353 }
aliguori2724b182009-03-05 23:01:47 +00003354 break;
ths86e94de2007-01-05 22:01:59 +00003355
aliguori2724b182009-03-05 23:01:47 +00003356 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003357 if (mon->reset_seen) {
3358 if (mon->suspend_cnt == 0) {
3359 monitor_printf(mon, "\n");
3360 }
3361 monitor_flush(mon);
3362 monitor_suspend(mon);
3363 } else {
3364 mon->suspend_cnt++;
3365 }
3366 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003367 break;
3368
3369 case CHR_EVENT_RESET:
3370 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3371 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003372 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003373 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003374 }
3375 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003376 break;
3377 }
ths86e94de2007-01-05 22:01:59 +00003378}
3379
aliguori76655d62009-03-06 20:27:37 +00003380
3381/*
3382 * Local variables:
3383 * c-indent-level: 4
3384 * c-basic-offset: 4
3385 * tab-width: 8
3386 * End:
3387 */
3388
aliguori731b0362009-03-05 23:01:42 +00003389void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003390{
aliguori731b0362009-03-05 23:01:42 +00003391 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003392 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003393
3394 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003395 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003396 is_first_init = 0;
3397 }
aliguori87127162009-03-05 23:01:29 +00003398
3399 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003400
aliguori87127162009-03-05 23:01:29 +00003401 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003402 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003403 if (flags & MONITOR_USE_READLINE) {
3404 mon->rs = readline_init(mon, monitor_find_completion);
3405 monitor_read_command(mon, 0);
3406 }
aliguori87127162009-03-05 23:01:29 +00003407
aliguori731b0362009-03-05 23:01:42 +00003408 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3409 mon);
aliguori87127162009-03-05 23:01:29 +00003410
Blue Swirl72cf2d42009-09-12 07:36:22 +00003411 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003412 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003413 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003414}
3415
aliguori376253e2009-03-05 23:01:23 +00003416static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003417{
aliguoribb5fc202009-03-05 23:01:15 +00003418 BlockDriverState *bs = opaque;
3419 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003420
aliguoribb5fc202009-03-05 23:01:15 +00003421 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003422 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003423 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003424 }
aliguori731b0362009-03-05 23:01:42 +00003425 if (mon->password_completion_cb)
3426 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003427
aliguori731b0362009-03-05 23:01:42 +00003428 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003429}
aliguoric0f4ce72009-03-05 23:01:01 +00003430
aliguori376253e2009-03-05 23:01:23 +00003431void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003432 BlockDriverCompletionFunc *completion_cb,
3433 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003434{
aliguoricde76ee2009-03-05 23:01:51 +00003435 int err;
3436
aliguoribb5fc202009-03-05 23:01:15 +00003437 if (!bdrv_key_required(bs)) {
3438 if (completion_cb)
3439 completion_cb(opaque, 0);
3440 return;
3441 }
aliguoric0f4ce72009-03-05 23:01:01 +00003442
aliguori376253e2009-03-05 23:01:23 +00003443 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3444 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003445
aliguori731b0362009-03-05 23:01:42 +00003446 mon->password_completion_cb = completion_cb;
3447 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003448
aliguoricde76ee2009-03-05 23:01:51 +00003449 err = monitor_read_password(mon, bdrv_password_cb, bs);
3450
3451 if (err && completion_cb)
3452 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003453}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003454
3455typedef struct QemuErrorSink QemuErrorSink;
3456struct QemuErrorSink {
3457 enum {
3458 ERR_SINK_FILE,
3459 ERR_SINK_MONITOR,
3460 } dest;
3461 union {
3462 FILE *fp;
3463 Monitor *mon;
3464 };
3465 QemuErrorSink *previous;
3466};
3467
Blue Swirl528e93a2009-08-31 15:14:40 +00003468static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003469
3470void qemu_errors_to_file(FILE *fp)
3471{
3472 QemuErrorSink *sink;
3473
3474 sink = qemu_mallocz(sizeof(*sink));
3475 sink->dest = ERR_SINK_FILE;
3476 sink->fp = fp;
3477 sink->previous = qemu_error_sink;
3478 qemu_error_sink = sink;
3479}
3480
3481void qemu_errors_to_mon(Monitor *mon)
3482{
3483 QemuErrorSink *sink;
3484
3485 sink = qemu_mallocz(sizeof(*sink));
3486 sink->dest = ERR_SINK_MONITOR;
3487 sink->mon = mon;
3488 sink->previous = qemu_error_sink;
3489 qemu_error_sink = sink;
3490}
3491
3492void qemu_errors_to_previous(void)
3493{
3494 QemuErrorSink *sink;
3495
3496 assert(qemu_error_sink != NULL);
3497 sink = qemu_error_sink;
3498 qemu_error_sink = sink->previous;
3499 qemu_free(sink);
3500}
3501
3502void qemu_error(const char *fmt, ...)
3503{
3504 va_list args;
3505
3506 assert(qemu_error_sink != NULL);
3507 switch (qemu_error_sink->dest) {
3508 case ERR_SINK_FILE:
3509 va_start(args, fmt);
3510 vfprintf(qemu_error_sink->fp, fmt, args);
3511 va_end(args);
3512 break;
3513 case ERR_SINK_MONITOR:
3514 va_start(args, fmt);
3515 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3516 va_end(args);
3517 break;
3518 }
3519}