blob: d393ad52719ab3d1056c910ce7041bb54ebfa4a1 [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 Capitulinoa1f896a2009-10-07 13:42:00 -0300617/**
618 * do_cont(): Resume emulation.
619 */
620static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +0000621{
622 struct bdrv_iterate_context context = { mon, 0 };
623
624 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +0000625 /* only resume the vm if all keys are set and valid */
aliguori376253e2009-03-05 23:01:23 +0000626 if (!context.err)
aliguoric0f4ce72009-03-05 23:01:01 +0000627 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000628}
629
aliguoribb5fc202009-03-05 23:01:15 +0000630static void bdrv_key_cb(void *opaque, int err)
631{
aliguori376253e2009-03-05 23:01:23 +0000632 Monitor *mon = opaque;
633
aliguoribb5fc202009-03-05 23:01:15 +0000634 /* another key was set successfully, retry to continue */
635 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -0300636 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000637}
638
639static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
640{
aliguori376253e2009-03-05 23:01:23 +0000641 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +0000642
aliguori376253e2009-03-05 23:01:23 +0000643 if (!context->err && bdrv_key_required(bs)) {
644 context->err = -EBUSY;
645 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
646 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +0000647 }
648}
649
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300650static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +0000651{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300652 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +0000653 if (!device)
654 device = "tcp::" DEFAULT_GDBSTUB_PORT;
655 if (gdbserver_start(device) < 0) {
656 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
657 device);
658 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +0000659 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +0000660 } else {
aliguori59030a82009-04-05 18:43:41 +0000661 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
662 device);
bellard8a7ddc32004-03-31 19:00:16 +0000663 }
664}
665
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300666static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100667{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300668 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100669 if (select_watchdog_action(action) == -1) {
670 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
671 }
672}
673
aliguori376253e2009-03-05 23:01:23 +0000674static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +0000675{
aliguori376253e2009-03-05 23:01:23 +0000676 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000677 switch(c) {
678 case '\'':
aliguori376253e2009-03-05 23:01:23 +0000679 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +0000680 break;
681 case '\\':
aliguori376253e2009-03-05 23:01:23 +0000682 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +0000683 break;
684 case '\n':
aliguori376253e2009-03-05 23:01:23 +0000685 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +0000686 break;
687 case '\r':
aliguori376253e2009-03-05 23:01:23 +0000688 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +0000689 break;
690 default:
691 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +0000692 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +0000693 } else {
aliguori376253e2009-03-05 23:01:23 +0000694 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +0000695 }
696 break;
697 }
aliguori376253e2009-03-05 23:01:23 +0000698 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +0000699}
700
aliguori376253e2009-03-05 23:01:23 +0000701static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -0500702 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000703{
bellard6a00d602005-11-21 23:25:50 +0000704 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000705 int nb_per_line, l, line_size, i, max_digits, len;
706 uint8_t buf[16];
707 uint64_t v;
708
709 if (format == 'i') {
710 int flags;
711 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000712 env = mon_get_cpu();
713 if (!env && !is_physical)
714 return;
bellard9307c4c2004-04-04 12:57:25 +0000715#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000716 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000717 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000718 } else if (wsize == 4) {
719 flags = 0;
720 } else {
bellard6a15fd12006-04-12 21:07:07 +0000721 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000722 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000723 if (env) {
724#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000725 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000726 (env->segs[R_CS].flags & DESC_L_MASK))
727 flags = 2;
728 else
729#endif
730 if (!(env->segs[R_CS].flags & DESC_B_MASK))
731 flags = 1;
732 }
bellard4c27ba22004-04-25 18:05:08 +0000733 }
734#endif
aliguori376253e2009-03-05 23:01:23 +0000735 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000736 return;
737 }
738
739 len = wsize * count;
740 if (wsize == 1)
741 line_size = 8;
742 else
743 line_size = 16;
744 nb_per_line = line_size / wsize;
745 max_digits = 0;
746
747 switch(format) {
748 case 'o':
749 max_digits = (wsize * 8 + 2) / 3;
750 break;
751 default:
752 case 'x':
753 max_digits = (wsize * 8) / 4;
754 break;
755 case 'u':
756 case 'd':
757 max_digits = (wsize * 8 * 10 + 32) / 33;
758 break;
759 case 'c':
760 wsize = 1;
761 break;
762 }
763
764 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000765 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +0000766 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +0000767 else
aliguori376253e2009-03-05 23:01:23 +0000768 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000769 l = len;
770 if (l > line_size)
771 l = line_size;
772 if (is_physical) {
773 cpu_physical_memory_rw(addr, buf, l, 0);
774 } else {
bellard6a00d602005-11-21 23:25:50 +0000775 env = mon_get_cpu();
776 if (!env)
777 break;
aliguoric8f79b62008-08-18 14:00:20 +0000778 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +0000779 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +0000780 break;
781 }
bellard9307c4c2004-04-04 12:57:25 +0000782 }
ths5fafdf22007-09-16 21:08:06 +0000783 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000784 while (i < l) {
785 switch(wsize) {
786 default:
787 case 1:
788 v = ldub_raw(buf + i);
789 break;
790 case 2:
791 v = lduw_raw(buf + i);
792 break;
793 case 4:
bellard92a31b12005-02-10 22:00:52 +0000794 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000795 break;
796 case 8:
797 v = ldq_raw(buf + i);
798 break;
799 }
aliguori376253e2009-03-05 23:01:23 +0000800 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +0000801 switch(format) {
802 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000803 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000804 break;
805 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000806 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000807 break;
808 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000809 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000810 break;
811 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000812 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000813 break;
814 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000815 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +0000816 break;
817 }
818 i += wsize;
819 }
aliguori376253e2009-03-05 23:01:23 +0000820 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000821 addr += l;
822 len -= l;
823 }
824}
825
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300826static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000827{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300828 int count = qdict_get_int(qdict, "count");
829 int format = qdict_get_int(qdict, "format");
830 int size = qdict_get_int(qdict, "size");
831 target_long addr = qdict_get_int(qdict, "addr");
832
aliguori376253e2009-03-05 23:01:23 +0000833 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +0000834}
835
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300836static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000837{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300838 int count = qdict_get_int(qdict, "count");
839 int format = qdict_get_int(qdict, "format");
840 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -0500841 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300842
aliguori376253e2009-03-05 23:01:23 +0000843 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +0000844}
845
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300846static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +0000847{
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300848 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -0500849 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -0300850
blueswir17743e582007-09-24 18:39:04 +0000851#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000852 switch(format) {
853 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000854 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +0000855 break;
856 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000857 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +0000858 break;
859 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000860 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +0000861 break;
862 default:
863 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000864 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +0000865 break;
866 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000867 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +0000868 break;
869 }
bellard92a31b12005-02-10 22:00:52 +0000870#else
871 switch(format) {
872 case 'o':
aliguori376253e2009-03-05 23:01:23 +0000873 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000874 break;
875 case 'x':
aliguori376253e2009-03-05 23:01:23 +0000876 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000877 break;
878 case 'u':
aliguori376253e2009-03-05 23:01:23 +0000879 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000880 break;
881 default:
882 case 'd':
aliguori376253e2009-03-05 23:01:23 +0000883 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000884 break;
885 case 'c':
aliguori376253e2009-03-05 23:01:23 +0000886 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +0000887 break;
888 }
889#endif
aliguori376253e2009-03-05 23:01:23 +0000890 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +0000891}
892
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300893static void do_memory_save(Monitor *mon, const QDict *qdict)
bellardb371dc52007-01-03 15:20:39 +0000894{
895 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300896 uint32_t size = qdict_get_int(qdict, "size");
897 const char *filename = qdict_get_str(qdict, "filename");
898 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +0000899 uint32_t l;
900 CPUState *env;
901 uint8_t buf[1024];
902
903 env = mon_get_cpu();
904 if (!env)
905 return;
906
907 f = fopen(filename, "wb");
908 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_printf(mon, "could not open '%s'\n", filename);
bellardb371dc52007-01-03 15:20:39 +0000910 return;
911 }
912 while (size != 0) {
913 l = sizeof(buf);
914 if (l > size)
915 l = size;
916 cpu_memory_rw_debug(env, addr, buf, l, 0);
917 fwrite(buf, 1, l, f);
918 addr += l;
919 size -= l;
920 }
921 fclose(f);
922}
923
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300924static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
aurel32a8bdf7a2008-04-11 21:36:14 +0000925{
926 FILE *f;
927 uint32_t l;
928 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -0300929 uint32_t size = qdict_get_int(qdict, "size");
930 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -0500931 target_phys_addr_t addr = qdict_get_int(qdict, "val");
aurel32a8bdf7a2008-04-11 21:36:14 +0000932
933 f = fopen(filename, "wb");
934 if (!f) {
aliguori376253e2009-03-05 23:01:23 +0000935 monitor_printf(mon, "could not open '%s'\n", filename);
aurel32a8bdf7a2008-04-11 21:36:14 +0000936 return;
937 }
938 while (size != 0) {
939 l = sizeof(buf);
940 if (l > size)
941 l = size;
942 cpu_physical_memory_rw(addr, buf, l, 0);
943 fwrite(buf, 1, l, f);
944 fflush(f);
945 addr += l;
946 size -= l;
947 }
948 fclose(f);
949}
950
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300951static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +0000952{
953 uint32_t addr;
954 uint8_t buf[1];
955 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300956 uint32_t start = qdict_get_int(qdict, "start");
957 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +0000958
959 sum = 0;
960 for(addr = start; addr < (start + size); addr++) {
961 cpu_physical_memory_rw(addr, buf, 1, 0);
962 /* BSD sum algorithm ('sum' Unix command) */
963 sum = (sum >> 1) | (sum << 15);
964 sum += buf[0];
965 }
aliguori376253e2009-03-05 23:01:23 +0000966 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +0000967}
968
bellarda3a91a32004-06-04 11:06:21 +0000969typedef struct {
970 int keycode;
971 const char *name;
972} KeyDef;
973
974static const KeyDef key_defs[] = {
975 { 0x2a, "shift" },
976 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000977
bellarda3a91a32004-06-04 11:06:21 +0000978 { 0x38, "alt" },
979 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000980 { 0x64, "altgr" },
981 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000982 { 0x1d, "ctrl" },
983 { 0x9d, "ctrl_r" },
984
985 { 0xdd, "menu" },
986
987 { 0x01, "esc" },
988
989 { 0x02, "1" },
990 { 0x03, "2" },
991 { 0x04, "3" },
992 { 0x05, "4" },
993 { 0x06, "5" },
994 { 0x07, "6" },
995 { 0x08, "7" },
996 { 0x09, "8" },
997 { 0x0a, "9" },
998 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000999 { 0x0c, "minus" },
1000 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001001 { 0x0e, "backspace" },
1002
1003 { 0x0f, "tab" },
1004 { 0x10, "q" },
1005 { 0x11, "w" },
1006 { 0x12, "e" },
1007 { 0x13, "r" },
1008 { 0x14, "t" },
1009 { 0x15, "y" },
1010 { 0x16, "u" },
1011 { 0x17, "i" },
1012 { 0x18, "o" },
1013 { 0x19, "p" },
1014
1015 { 0x1c, "ret" },
1016
1017 { 0x1e, "a" },
1018 { 0x1f, "s" },
1019 { 0x20, "d" },
1020 { 0x21, "f" },
1021 { 0x22, "g" },
1022 { 0x23, "h" },
1023 { 0x24, "j" },
1024 { 0x25, "k" },
1025 { 0x26, "l" },
1026
1027 { 0x2c, "z" },
1028 { 0x2d, "x" },
1029 { 0x2e, "c" },
1030 { 0x2f, "v" },
1031 { 0x30, "b" },
1032 { 0x31, "n" },
1033 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001034 { 0x33, "comma" },
1035 { 0x34, "dot" },
1036 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001037
balrog4d3b6f62008-02-10 16:33:14 +00001038 { 0x37, "asterisk" },
1039
bellarda3a91a32004-06-04 11:06:21 +00001040 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001041 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001042 { 0x3b, "f1" },
1043 { 0x3c, "f2" },
1044 { 0x3d, "f3" },
1045 { 0x3e, "f4" },
1046 { 0x3f, "f5" },
1047 { 0x40, "f6" },
1048 { 0x41, "f7" },
1049 { 0x42, "f8" },
1050 { 0x43, "f9" },
1051 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001052 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001053 { 0x46, "scroll_lock" },
1054
bellard64866c32006-05-07 18:03:31 +00001055 { 0xb5, "kp_divide" },
1056 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001057 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001058 { 0x4e, "kp_add" },
1059 { 0x9c, "kp_enter" },
1060 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001061 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001062
1063 { 0x52, "kp_0" },
1064 { 0x4f, "kp_1" },
1065 { 0x50, "kp_2" },
1066 { 0x51, "kp_3" },
1067 { 0x4b, "kp_4" },
1068 { 0x4c, "kp_5" },
1069 { 0x4d, "kp_6" },
1070 { 0x47, "kp_7" },
1071 { 0x48, "kp_8" },
1072 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001073
bellarda3a91a32004-06-04 11:06:21 +00001074 { 0x56, "<" },
1075
1076 { 0x57, "f11" },
1077 { 0x58, "f12" },
1078
1079 { 0xb7, "print" },
1080
1081 { 0xc7, "home" },
1082 { 0xc9, "pgup" },
1083 { 0xd1, "pgdn" },
1084 { 0xcf, "end" },
1085
1086 { 0xcb, "left" },
1087 { 0xc8, "up" },
1088 { 0xd0, "down" },
1089 { 0xcd, "right" },
1090
1091 { 0xd2, "insert" },
1092 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001093#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1094 { 0xf0, "stop" },
1095 { 0xf1, "again" },
1096 { 0xf2, "props" },
1097 { 0xf3, "undo" },
1098 { 0xf4, "front" },
1099 { 0xf5, "copy" },
1100 { 0xf6, "open" },
1101 { 0xf7, "paste" },
1102 { 0xf8, "find" },
1103 { 0xf9, "cut" },
1104 { 0xfa, "lf" },
1105 { 0xfb, "help" },
1106 { 0xfc, "meta_l" },
1107 { 0xfd, "meta_r" },
1108 { 0xfe, "compose" },
1109#endif
bellarda3a91a32004-06-04 11:06:21 +00001110 { 0, NULL },
1111};
1112
1113static int get_keycode(const char *key)
1114{
1115 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001116 char *endp;
1117 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001118
1119 for(p = key_defs; p->name != NULL; p++) {
1120 if (!strcmp(key, p->name))
1121 return p->keycode;
1122 }
bellard64866c32006-05-07 18:03:31 +00001123 if (strstart(key, "0x", NULL)) {
1124 ret = strtoul(key, &endp, 0);
1125 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1126 return ret;
1127 }
bellarda3a91a32004-06-04 11:06:21 +00001128 return -1;
1129}
1130
balrogc8256f92008-06-08 22:45:01 +00001131#define MAX_KEYCODES 16
1132static uint8_t keycodes[MAX_KEYCODES];
1133static int nb_pending_keycodes;
1134static QEMUTimer *key_timer;
1135
1136static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001137{
balrogc8256f92008-06-08 22:45:01 +00001138 int keycode;
1139
1140 while (nb_pending_keycodes > 0) {
1141 nb_pending_keycodes--;
1142 keycode = keycodes[nb_pending_keycodes];
1143 if (keycode & 0x80)
1144 kbd_put_keycode(0xe0);
1145 kbd_put_keycode(keycode | 0x80);
1146 }
1147}
1148
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001149static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001150{
balrog3401c0d2008-06-04 10:05:59 +00001151 char keyname_buf[16];
1152 char *separator;
1153 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001154 const char *string = qdict_get_str(qdict, "string");
1155 int has_hold_time = qdict_haskey(qdict, "hold_time");
1156 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001157
balrogc8256f92008-06-08 22:45:01 +00001158 if (nb_pending_keycodes > 0) {
1159 qemu_del_timer(key_timer);
1160 release_keys(NULL);
1161 }
1162 if (!has_hold_time)
1163 hold_time = 100;
1164 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001165 while (1) {
1166 separator = strchr(string, '-');
1167 keyname_len = separator ? separator - string : strlen(string);
1168 if (keyname_len > 0) {
1169 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1170 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001171 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001172 return;
bellarda3a91a32004-06-04 11:06:21 +00001173 }
balrogc8256f92008-06-08 22:45:01 +00001174 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001175 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001176 return;
1177 }
1178 keyname_buf[keyname_len] = 0;
1179 keycode = get_keycode(keyname_buf);
1180 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001181 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001182 return;
1183 }
balrogc8256f92008-06-08 22:45:01 +00001184 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001185 }
balrog3401c0d2008-06-04 10:05:59 +00001186 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001187 break;
balrog3401c0d2008-06-04 10:05:59 +00001188 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001189 }
balrogc8256f92008-06-08 22:45:01 +00001190 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001191 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001192 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001193 keycode = keycodes[i];
1194 if (keycode & 0x80)
1195 kbd_put_keycode(0xe0);
1196 kbd_put_keycode(keycode & 0x7f);
1197 }
balrogc8256f92008-06-08 22:45:01 +00001198 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001199 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001200 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001201}
1202
bellard13224a82006-07-14 22:03:35 +00001203static int mouse_button_state;
1204
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001205static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001206{
1207 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001208 const char *dx_str = qdict_get_str(qdict, "dx_str");
1209 const char *dy_str = qdict_get_str(qdict, "dy_str");
1210 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001211 dx = strtol(dx_str, NULL, 0);
1212 dy = strtol(dy_str, NULL, 0);
1213 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001214 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001215 dz = strtol(dz_str, NULL, 0);
1216 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1217}
1218
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001219static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001220{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001221 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001222 mouse_button_state = button_state;
1223 kbd_mouse_event(0, 0, 0, mouse_button_state);
1224}
1225
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001226static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001227{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001228 int size = qdict_get_int(qdict, "size");
1229 int addr = qdict_get_int(qdict, "addr");
1230 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001231 uint32_t val;
1232 int suffix;
1233
1234 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001235 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001236 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001237 addr++;
1238 }
1239 addr &= 0xffff;
1240
1241 switch(size) {
1242 default:
1243 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001244 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001245 suffix = 'b';
1246 break;
1247 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001248 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001249 suffix = 'w';
1250 break;
1251 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001252 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001253 suffix = 'l';
1254 break;
1255 }
aliguori376253e2009-03-05 23:01:23 +00001256 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1257 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001258}
bellarda3a91a32004-06-04 11:06:21 +00001259
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001260static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001261{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001262 int size = qdict_get_int(qdict, "size");
1263 int addr = qdict_get_int(qdict, "addr");
1264 int val = qdict_get_int(qdict, "val");
1265
Jan Kiszkaf1147842009-07-14 10:20:11 +02001266 addr &= IOPORTS_MASK;
1267
1268 switch (size) {
1269 default:
1270 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001271 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001272 break;
1273 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001274 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001275 break;
1276 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001277 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001278 break;
1279 }
1280}
1281
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001282static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001283{
1284 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001285 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001286
Jan Kiszka76e30d02009-07-02 00:19:02 +02001287 res = qemu_boot_set(bootdevice);
1288 if (res == 0) {
1289 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1290 } else if (res > 0) {
1291 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001292 } else {
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, "no function defined to set boot device list for "
1294 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001295 }
1296}
1297
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001298/**
1299 * do_system_reset(): Issue a machine reset
1300 */
1301static void do_system_reset(Monitor *mon, const QDict *qdict,
1302 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001303{
1304 qemu_system_reset_request();
1305}
1306
Luiz Capitulino43076662009-10-07 13:41:59 -03001307/**
1308 * do_system_powerdown(): Issue a machine powerdown
1309 */
1310static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1311 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001312{
1313 qemu_system_powerdown_request();
1314}
1315
bellardb86bda52004-09-18 19:32:46 +00001316#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001317static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001318{
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1320 addr,
1321 pte & mask,
1322 pte & PG_GLOBAL_MASK ? 'G' : '-',
1323 pte & PG_PSE_MASK ? 'P' : '-',
1324 pte & PG_DIRTY_MASK ? 'D' : '-',
1325 pte & PG_ACCESSED_MASK ? 'A' : '-',
1326 pte & PG_PCD_MASK ? 'C' : '-',
1327 pte & PG_PWT_MASK ? 'T' : '-',
1328 pte & PG_USER_MASK ? 'U' : '-',
1329 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001330}
1331
aliguori376253e2009-03-05 23:01:23 +00001332static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001333{
bellard6a00d602005-11-21 23:25:50 +00001334 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001335 int l1, l2;
1336 uint32_t pgd, pde, pte;
1337
bellard6a00d602005-11-21 23:25:50 +00001338 env = mon_get_cpu();
1339 if (!env)
1340 return;
1341
bellardb86bda52004-09-18 19:32:46 +00001342 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001343 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001344 return;
1345 }
1346 pgd = env->cr[3] & ~0xfff;
1347 for(l1 = 0; l1 < 1024; l1++) {
1348 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1349 pde = le32_to_cpu(pde);
1350 if (pde & PG_PRESENT_MASK) {
1351 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001352 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001353 } else {
1354 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001355 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001356 (uint8_t *)&pte, 4);
1357 pte = le32_to_cpu(pte);
1358 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001359 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001360 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001361 ~0xfff);
1362 }
1363 }
1364 }
1365 }
1366 }
1367}
1368
aliguori376253e2009-03-05 23:01:23 +00001369static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001370 uint32_t end, int prot)
1371{
bellard9746b152004-11-11 18:30:24 +00001372 int prot1;
1373 prot1 = *plast_prot;
1374 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001375 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001376 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1377 *pstart, end, end - *pstart,
1378 prot1 & PG_USER_MASK ? 'u' : '-',
1379 'r',
1380 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001381 }
1382 if (prot != 0)
1383 *pstart = end;
1384 else
1385 *pstart = -1;
1386 *plast_prot = prot;
1387 }
1388}
1389
aliguori376253e2009-03-05 23:01:23 +00001390static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001391{
bellard6a00d602005-11-21 23:25:50 +00001392 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001393 int l1, l2, prot, last_prot;
1394 uint32_t pgd, pde, pte, start, end;
1395
bellard6a00d602005-11-21 23:25:50 +00001396 env = mon_get_cpu();
1397 if (!env)
1398 return;
1399
bellardb86bda52004-09-18 19:32:46 +00001400 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001401 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001402 return;
1403 }
1404 pgd = env->cr[3] & ~0xfff;
1405 last_prot = 0;
1406 start = -1;
1407 for(l1 = 0; l1 < 1024; l1++) {
1408 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1409 pde = le32_to_cpu(pde);
1410 end = l1 << 22;
1411 if (pde & PG_PRESENT_MASK) {
1412 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1413 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001414 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001415 } else {
1416 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001417 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001418 (uint8_t *)&pte, 4);
1419 pte = le32_to_cpu(pte);
1420 end = (l1 << 22) + (l2 << 12);
1421 if (pte & PG_PRESENT_MASK) {
1422 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1423 } else {
1424 prot = 0;
1425 }
aliguori376253e2009-03-05 23:01:23 +00001426 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001427 }
1428 }
1429 } else {
1430 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001431 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001432 }
1433 }
1434}
1435#endif
1436
aurel327c664e22009-03-03 06:12:22 +00001437#if defined(TARGET_SH4)
1438
aliguori376253e2009-03-05 23:01:23 +00001439static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001440{
aliguori376253e2009-03-05 23:01:23 +00001441 monitor_printf(mon, " tlb%i:\t"
1442 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1443 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1444 "dirty=%hhu writethrough=%hhu\n",
1445 idx,
1446 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1447 tlb->v, tlb->sh, tlb->c, tlb->pr,
1448 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001449}
1450
aliguori376253e2009-03-05 23:01:23 +00001451static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001452{
1453 CPUState *env = mon_get_cpu();
1454 int i;
1455
aliguori376253e2009-03-05 23:01:23 +00001456 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001457 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001458 print_tlb (mon, i, &env->itlb[i]);
1459 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001460 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001461 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001462}
1463
1464#endif
1465
aliguori376253e2009-03-05 23:01:23 +00001466static void do_info_kvm(Monitor *mon)
aliguori7ba1e612008-11-05 16:04:33 +00001467{
1468#ifdef CONFIG_KVM
aliguori376253e2009-03-05 23:01:23 +00001469 monitor_printf(mon, "kvm support: ");
aliguori7ba1e612008-11-05 16:04:33 +00001470 if (kvm_enabled())
aliguori376253e2009-03-05 23:01:23 +00001471 monitor_printf(mon, "enabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001472 else
aliguori376253e2009-03-05 23:01:23 +00001473 monitor_printf(mon, "disabled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001474#else
aliguori376253e2009-03-05 23:01:23 +00001475 monitor_printf(mon, "kvm support: not compiled\n");
aliguori7ba1e612008-11-05 16:04:33 +00001476#endif
1477}
1478
aliguori030ea372009-04-21 22:30:47 +00001479static void do_info_numa(Monitor *mon)
1480{
aliguorib28b6232009-04-22 20:20:29 +00001481 int i;
aliguori030ea372009-04-21 22:30:47 +00001482 CPUState *env;
1483
1484 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1485 for (i = 0; i < nb_numa_nodes; i++) {
1486 monitor_printf(mon, "node %d cpus:", i);
1487 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1488 if (env->numa_node == i) {
1489 monitor_printf(mon, " %d", env->cpu_index);
1490 }
1491 }
1492 monitor_printf(mon, "\n");
1493 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1494 node_mem[i] >> 20);
1495 }
1496}
1497
bellard5f1ce942006-02-08 22:40:15 +00001498#ifdef CONFIG_PROFILER
1499
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001500int64_t qemu_time;
1501int64_t dev_time;
1502
aliguori376253e2009-03-05 23:01:23 +00001503static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001504{
1505 int64_t total;
1506 total = qemu_time;
1507 if (total == 0)
1508 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001510 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001511 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001512 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001513 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001514 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001515}
1516#else
aliguori376253e2009-03-05 23:01:23 +00001517static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001518{
aliguori376253e2009-03-05 23:01:23 +00001519 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001520}
1521#endif
1522
bellardec36b692006-07-16 18:57:03 +00001523/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001524static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001525
aliguori376253e2009-03-05 23:01:23 +00001526static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001527{
1528 int i;
1529 CaptureState *s;
1530
1531 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001532 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001533 s->ops.info (s->opaque);
1534 }
1535}
1536
Blue Swirl23130862009-06-06 08:22:04 +00001537#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001538static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001539{
1540 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001541 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00001542 CaptureState *s;
1543
1544 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1545 if (i == n) {
1546 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001547 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00001548 qemu_free (s);
1549 return;
1550 }
1551 }
1552}
1553
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001554static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00001555{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03001556 const char *path = qdict_get_str(qdict, "path");
1557 int has_freq = qdict_haskey(qdict, "freq");
1558 int freq = qdict_get_try_int(qdict, "freq", -1);
1559 int has_bits = qdict_haskey(qdict, "bits");
1560 int bits = qdict_get_try_int(qdict, "bits", -1);
1561 int has_channels = qdict_haskey(qdict, "nchannels");
1562 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00001563 CaptureState *s;
1564
1565 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001566
1567 freq = has_freq ? freq : 44100;
1568 bits = has_bits ? bits : 16;
1569 nchannels = has_channels ? nchannels : 2;
1570
1571 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00001572 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00001573 qemu_free (s);
1574 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001575 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00001576}
1577#endif
1578
aurel32dc1c0b72008-04-27 23:52:12 +00001579#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001580static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00001581{
1582 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001583 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00001584
1585 for (env = first_cpu; env != NULL; env = env->next_cpu)
1586 if (env->cpu_index == cpu_index) {
1587 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1588 break;
1589 }
1590}
1591#endif
1592
aliguori376253e2009-03-05 23:01:23 +00001593static void do_info_status(Monitor *mon)
aurel326f9c5ee2008-12-18 22:43:56 +00001594{
aurel321b530a62009-04-05 20:08:59 +00001595 if (vm_running) {
1596 if (singlestep) {
1597 monitor_printf(mon, "VM status: running (single step mode)\n");
1598 } else {
1599 monitor_printf(mon, "VM status: running\n");
1600 }
1601 } else
aliguori376253e2009-03-05 23:01:23 +00001602 monitor_printf(mon, "VM status: paused\n");
aurel326f9c5ee2008-12-18 22:43:56 +00001603}
1604
1605
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001606static void do_balloon(Monitor *mon, const QDict *qdict)
aliguoridf751fa2008-12-04 20:19:35 +00001607{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001608 int value = qdict_get_int(qdict, "value");
Anthony Liguoric227f092009-10-01 16:12:16 -05001609 ram_addr_t target = value;
aliguoridf751fa2008-12-04 20:19:35 +00001610 qemu_balloon(target << 20);
1611}
1612
aliguori376253e2009-03-05 23:01:23 +00001613static void do_info_balloon(Monitor *mon)
aliguoridf751fa2008-12-04 20:19:35 +00001614{
Anthony Liguoric227f092009-10-01 16:12:16 -05001615 ram_addr_t actual;
aliguoridf751fa2008-12-04 20:19:35 +00001616
1617 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001618 if (kvm_enabled() && !kvm_has_sync_mmu())
aliguori376253e2009-03-05 23:01:23 +00001619 monitor_printf(mon, "Using KVM without synchronous MMU, "
1620 "ballooning disabled\n");
aliguoribd322082008-12-04 20:33:06 +00001621 else if (actual == 0)
aliguori376253e2009-03-05 23:01:23 +00001622 monitor_printf(mon, "Ballooning not activated in VM\n");
aliguoridf751fa2008-12-04 20:19:35 +00001623 else
aliguori376253e2009-03-05 23:01:23 +00001624 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
aliguoridf751fa2008-12-04 20:19:35 +00001625}
1626
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001627static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00001628{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001629 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00001630
aliguori76655d62009-03-06 20:27:37 +00001631 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001632 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00001633 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001634 return acl;
1635}
aliguori76655d62009-03-06 20:27:37 +00001636
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001637static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001638{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001639 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001640 qemu_acl *acl = find_acl(mon, aclname);
1641 qemu_acl_entry *entry;
1642 int i = 0;
1643
1644 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001645 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00001646 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001647 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00001648 i++;
1649 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001650 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00001651 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001652 }
1653}
1654
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001655static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001656{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001657 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001658 qemu_acl *acl = find_acl(mon, aclname);
1659
1660 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001661 qemu_acl_reset(acl);
1662 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001663 }
1664}
aliguori76655d62009-03-06 20:27:37 +00001665
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001666static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001667{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001668 const char *aclname = qdict_get_str(qdict, "aclname");
1669 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001670 qemu_acl *acl = find_acl(mon, aclname);
1671
1672 if (acl) {
1673 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001674 acl->defaultDeny = 0;
1675 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001676 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00001677 acl->defaultDeny = 1;
1678 monitor_printf(mon, "acl: policy set to 'deny'\n");
1679 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001680 monitor_printf(mon, "acl: unknown policy '%s', "
1681 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001682 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001683 }
1684}
aliguori76655d62009-03-06 20:27:37 +00001685
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001686static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001687{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001688 const char *aclname = qdict_get_str(qdict, "aclname");
1689 const char *match = qdict_get_str(qdict, "match");
1690 const char *policy = qdict_get_str(qdict, "policy");
1691 int has_index = qdict_haskey(qdict, "index");
1692 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001693 qemu_acl *acl = find_acl(mon, aclname);
1694 int deny, ret;
1695
1696 if (acl) {
1697 if (strcmp(policy, "allow") == 0) {
1698 deny = 0;
1699 } else if (strcmp(policy, "deny") == 0) {
1700 deny = 1;
1701 } else {
1702 monitor_printf(mon, "acl: unknown policy '%s', "
1703 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00001704 return;
1705 }
aliguori28a76be2009-03-06 20:27:40 +00001706 if (has_index)
1707 ret = qemu_acl_insert(acl, deny, match, index);
1708 else
1709 ret = qemu_acl_append(acl, deny, match);
1710 if (ret < 0)
1711 monitor_printf(mon, "acl: unable to add acl entry\n");
1712 else
1713 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001714 }
1715}
aliguori76655d62009-03-06 20:27:37 +00001716
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001717static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001718{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001719 const char *aclname = qdict_get_str(qdict, "aclname");
1720 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001721 qemu_acl *acl = find_acl(mon, aclname);
1722 int ret;
aliguori76655d62009-03-06 20:27:37 +00001723
Jan Kiszka15dfcd42009-06-25 08:22:08 +02001724 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00001725 ret = qemu_acl_remove(acl, match);
1726 if (ret < 0)
1727 monitor_printf(mon, "acl: no matching acl entry\n");
1728 else
1729 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00001730 }
1731}
1732
Huang Ying79c4f6b2009-06-23 10:05:14 +08001733#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001734static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08001735{
1736 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03001737 int cpu_index = qdict_get_int(qdict, "cpu_index");
1738 int bank = qdict_get_int(qdict, "bank");
1739 uint64_t status = qdict_get_int(qdict, "status");
1740 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1741 uint64_t addr = qdict_get_int(qdict, "addr");
1742 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08001743
1744 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1745 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1746 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1747 break;
1748 }
1749}
1750#endif
1751
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001752static void do_getfd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001753{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001754 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001755 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001756 int fd;
1757
1758 fd = qemu_chr_get_msgfd(mon->chr);
1759 if (fd == -1) {
1760 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1761 return;
1762 }
1763
1764 if (qemu_isdigit(fdname[0])) {
1765 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1766 return;
1767 }
1768
1769 fd = dup(fd);
1770 if (fd == -1) {
1771 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1772 strerror(errno));
1773 return;
1774 }
1775
Blue Swirl72cf2d42009-09-12 07:36:22 +00001776 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001777 if (strcmp(monfd->name, fdname) != 0) {
1778 continue;
1779 }
1780
1781 close(monfd->fd);
1782 monfd->fd = fd;
1783 return;
1784 }
1785
Anthony Liguoric227f092009-10-01 16:12:16 -05001786 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001787 monfd->name = qemu_strdup(fdname);
1788 monfd->fd = fd;
1789
Blue Swirl72cf2d42009-09-12 07:36:22 +00001790 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001791}
1792
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001793static void do_closefd(Monitor *mon, const QDict *qdict)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001794{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001795 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05001796 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001797
Blue Swirl72cf2d42009-09-12 07:36:22 +00001798 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001799 if (strcmp(monfd->name, fdname) != 0) {
1800 continue;
1801 }
1802
Blue Swirl72cf2d42009-09-12 07:36:22 +00001803 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01001804 close(monfd->fd);
1805 qemu_free(monfd->name);
1806 qemu_free(monfd);
1807 return;
1808 }
1809
1810 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1811 fdname);
1812}
1813
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001814static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001815{
1816 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001817 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02001818
1819 vm_stop(0);
1820
Juan Quintela05f24012009-08-20 19:42:22 +02001821 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02001822 vm_start();
1823}
1824
Mark McLoughlin7768e042009-07-22 09:11:41 +01001825int monitor_get_fd(Monitor *mon, const char *fdname)
1826{
Anthony Liguoric227f092009-10-01 16:12:16 -05001827 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01001828
Blue Swirl72cf2d42009-09-12 07:36:22 +00001829 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01001830 int fd;
1831
1832 if (strcmp(monfd->name, fdname) != 0) {
1833 continue;
1834 }
1835
1836 fd = monfd->fd;
1837
1838 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001839 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01001840 qemu_free(monfd->name);
1841 qemu_free(monfd);
1842
1843 return fd;
1844 }
1845
1846 return -1;
1847}
1848
Anthony Liguoric227f092009-10-01 16:12:16 -05001849static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00001850#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00001851 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001852};
1853
Blue Swirl23130862009-06-06 08:22:04 +00001854/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05001855static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001856 {
1857 .name = "version",
1858 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001859 .params = "",
1860 .help = "show the version of QEMU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001861 .mhandler.info = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001862 },
1863 {
1864 .name = "network",
1865 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001866 .params = "",
1867 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001868 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001869 },
1870 {
1871 .name = "chardev",
1872 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001873 .params = "",
1874 .help = "show the character devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001875 .mhandler.info = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001876 },
1877 {
1878 .name = "block",
1879 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001880 .params = "",
1881 .help = "show the block devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001882 .mhandler.info = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001883 },
1884 {
1885 .name = "blockstats",
1886 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001887 .params = "",
1888 .help = "show block device statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03001889 .mhandler.info = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001890 },
1891 {
1892 .name = "registers",
1893 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001894 .params = "",
1895 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03001896 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001897 },
1898 {
1899 .name = "cpus",
1900 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001901 .params = "",
1902 .help = "show infos for each CPU",
Luiz Capitulino910df892009-10-07 13:41:51 -03001903 .mhandler.info = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001904 },
1905 {
1906 .name = "history",
1907 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001908 .params = "",
1909 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03001910 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001911 },
1912 {
1913 .name = "irq",
1914 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001915 .params = "",
1916 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03001917 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001918 },
1919 {
1920 .name = "pic",
1921 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001922 .params = "",
1923 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03001924 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001925 },
1926 {
1927 .name = "pci",
1928 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001929 .params = "",
1930 .help = "show PCI info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001931 .mhandler.info = pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001932 },
aurel327c664e22009-03-03 06:12:22 +00001933#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001934 {
1935 .name = "tlb",
1936 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001937 .params = "",
1938 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001939 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001940 },
aurel327c664e22009-03-03 06:12:22 +00001941#endif
1942#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001943 {
1944 .name = "mem",
1945 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001946 .params = "",
1947 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03001948 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001949 },
1950 {
1951 .name = "hpet",
1952 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001953 .params = "",
1954 .help = "show state of HPET",
Luiz Capitulino910df892009-10-07 13:41:51 -03001955 .mhandler.info = do_info_hpet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001956 },
bellardb86bda52004-09-18 19:32:46 +00001957#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001958 {
1959 .name = "jit",
1960 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001961 .params = "",
1962 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03001963 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001964 },
1965 {
1966 .name = "kvm",
1967 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001968 .params = "",
1969 .help = "show KVM information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001970 .mhandler.info = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001971 },
1972 {
1973 .name = "numa",
1974 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001975 .params = "",
1976 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001977 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001978 },
1979 {
1980 .name = "usb",
1981 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001982 .params = "",
1983 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001984 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001985 },
1986 {
1987 .name = "usbhost",
1988 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001989 .params = "",
1990 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03001991 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001992 },
1993 {
1994 .name = "profile",
1995 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001996 .params = "",
1997 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03001998 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03001999 },
2000 {
2001 .name = "capture",
2002 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002003 .params = "",
2004 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002005 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002006 },
2007 {
2008 .name = "snapshots",
2009 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002010 .params = "",
2011 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002012 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002013 },
2014 {
2015 .name = "status",
2016 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002017 .params = "",
2018 .help = "show the current VM status (running|paused)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002019 .mhandler.info = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002020 },
2021 {
2022 .name = "pcmcia",
2023 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002024 .params = "",
2025 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002026 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002027 },
2028 {
2029 .name = "mice",
2030 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002031 .params = "",
2032 .help = "show which guest mouse is receiving events",
Luiz Capitulino910df892009-10-07 13:41:51 -03002033 .mhandler.info = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002034 },
2035 {
2036 .name = "vnc",
2037 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002038 .params = "",
2039 .help = "show the vnc server status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002040 .mhandler.info = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002041 },
2042 {
2043 .name = "name",
2044 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002045 .params = "",
2046 .help = "show the current VM name",
Luiz Capitulino910df892009-10-07 13:41:51 -03002047 .mhandler.info = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002048 },
2049 {
2050 .name = "uuid",
2051 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002052 .params = "",
2053 .help = "show the current VM UUID",
Luiz Capitulino910df892009-10-07 13:41:51 -03002054 .mhandler.info = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002055 },
j_mayer76a66252007-03-07 08:32:30 +00002056#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002057 {
2058 .name = "cpustats",
2059 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002060 .params = "",
2061 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002062 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002063 },
j_mayer76a66252007-03-07 08:32:30 +00002064#endif
blueswir131a60e22007-10-26 18:42:59 +00002065#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002066 {
2067 .name = "usernet",
2068 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002069 .params = "",
2070 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002071 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002072 },
blueswir131a60e22007-10-26 18:42:59 +00002073#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002074 {
2075 .name = "migrate",
2076 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002077 .params = "",
2078 .help = "show migration status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002079 .mhandler.info = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002080 },
2081 {
2082 .name = "balloon",
2083 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002084 .params = "",
2085 .help = "show balloon information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002086 .mhandler.info = do_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002087 },
2088 {
2089 .name = "qtree",
2090 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002091 .params = "",
2092 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002093 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002094 },
2095 {
2096 .name = "qdm",
2097 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002098 .params = "",
2099 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002100 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002101 },
2102 {
2103 .name = "roms",
2104 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002105 .params = "",
2106 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002107 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002108 },
2109 {
2110 .name = NULL,
2111 },
bellard9dc39cb2004-03-14 21:38:27 +00002112};
2113
bellard9307c4c2004-04-04 12:57:25 +00002114/*******************************************************************/
2115
2116static const char *pch;
2117static jmp_buf expr_env;
2118
bellard92a31b12005-02-10 22:00:52 +00002119#define MD_TLONG 0
2120#define MD_I32 1
2121
bellard9307c4c2004-04-04 12:57:25 +00002122typedef struct MonitorDef {
2123 const char *name;
2124 int offset;
blueswir18662d652008-10-02 18:32:44 +00002125 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002126 int type;
bellard9307c4c2004-04-04 12:57:25 +00002127} MonitorDef;
2128
bellard57206fd2004-04-25 18:54:52 +00002129#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002130static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002131{
bellard6a00d602005-11-21 23:25:50 +00002132 CPUState *env = mon_get_cpu();
2133 if (!env)
2134 return 0;
2135 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002136}
2137#endif
2138
bellarda541f292004-04-12 20:39:29 +00002139#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002140static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002141{
bellard6a00d602005-11-21 23:25:50 +00002142 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002143 unsigned int u;
2144 int i;
2145
bellard6a00d602005-11-21 23:25:50 +00002146 if (!env)
2147 return 0;
2148
bellarda541f292004-04-12 20:39:29 +00002149 u = 0;
2150 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002151 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002152
2153 return u;
2154}
2155
blueswir18662d652008-10-02 18:32:44 +00002156static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002157{
bellard6a00d602005-11-21 23:25:50 +00002158 CPUState *env = mon_get_cpu();
2159 if (!env)
2160 return 0;
j_mayer0411a972007-10-25 21:35:50 +00002161 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002162}
2163
blueswir18662d652008-10-02 18:32:44 +00002164static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002165{
bellard6a00d602005-11-21 23:25:50 +00002166 CPUState *env = mon_get_cpu();
2167 if (!env)
2168 return 0;
aurel323d7b4172008-10-21 11:28:46 +00002169 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002170}
bellard9fddaa02004-05-21 12:59:32 +00002171
blueswir18662d652008-10-02 18:32:44 +00002172static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002173{
bellard6a00d602005-11-21 23:25:50 +00002174 CPUState *env = mon_get_cpu();
2175 if (!env)
2176 return 0;
2177 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002178}
2179
blueswir18662d652008-10-02 18:32:44 +00002180static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002181{
bellard6a00d602005-11-21 23:25:50 +00002182 CPUState *env = mon_get_cpu();
2183 if (!env)
2184 return 0;
2185 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002186}
2187
blueswir18662d652008-10-02 18:32:44 +00002188static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002189{
bellard6a00d602005-11-21 23:25:50 +00002190 CPUState *env = mon_get_cpu();
2191 if (!env)
2192 return 0;
2193 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002194}
bellarda541f292004-04-12 20:39:29 +00002195#endif
2196
bellarde95c8d52004-09-30 22:22:08 +00002197#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002198#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002199static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002200{
bellard6a00d602005-11-21 23:25:50 +00002201 CPUState *env = mon_get_cpu();
2202 if (!env)
2203 return 0;
2204 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00002205}
bellard7b936c02005-10-30 17:05:13 +00002206#endif
bellarde95c8d52004-09-30 22:22:08 +00002207
blueswir18662d652008-10-02 18:32:44 +00002208static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002209{
bellard6a00d602005-11-21 23:25:50 +00002210 CPUState *env = mon_get_cpu();
2211 if (!env)
2212 return 0;
2213 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002214}
2215#endif
2216
blueswir18662d652008-10-02 18:32:44 +00002217static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002218#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002219
2220#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002221 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002222 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002223 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002224
bellard9307c4c2004-04-04 12:57:25 +00002225 { "eax", offsetof(CPUState, regs[0]) },
2226 { "ecx", offsetof(CPUState, regs[1]) },
2227 { "edx", offsetof(CPUState, regs[2]) },
2228 { "ebx", offsetof(CPUState, regs[3]) },
2229 { "esp|sp", offsetof(CPUState, regs[4]) },
2230 { "ebp|fp", offsetof(CPUState, regs[5]) },
2231 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002232 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002233#ifdef TARGET_X86_64
2234 { "r8", offsetof(CPUState, regs[8]) },
2235 { "r9", offsetof(CPUState, regs[9]) },
2236 { "r10", offsetof(CPUState, regs[10]) },
2237 { "r11", offsetof(CPUState, regs[11]) },
2238 { "r12", offsetof(CPUState, regs[12]) },
2239 { "r13", offsetof(CPUState, regs[13]) },
2240 { "r14", offsetof(CPUState, regs[14]) },
2241 { "r15", offsetof(CPUState, regs[15]) },
2242#endif
bellard9307c4c2004-04-04 12:57:25 +00002243 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002244 { "eip", offsetof(CPUState, eip) },
2245 SEG("cs", R_CS)
2246 SEG("ds", R_DS)
2247 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002248 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002249 SEG("fs", R_FS)
2250 SEG("gs", R_GS)
2251 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002252#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002253 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002254 { "r0", offsetof(CPUState, gpr[0]) },
2255 { "r1", offsetof(CPUState, gpr[1]) },
2256 { "r2", offsetof(CPUState, gpr[2]) },
2257 { "r3", offsetof(CPUState, gpr[3]) },
2258 { "r4", offsetof(CPUState, gpr[4]) },
2259 { "r5", offsetof(CPUState, gpr[5]) },
2260 { "r6", offsetof(CPUState, gpr[6]) },
2261 { "r7", offsetof(CPUState, gpr[7]) },
2262 { "r8", offsetof(CPUState, gpr[8]) },
2263 { "r9", offsetof(CPUState, gpr[9]) },
2264 { "r10", offsetof(CPUState, gpr[10]) },
2265 { "r11", offsetof(CPUState, gpr[11]) },
2266 { "r12", offsetof(CPUState, gpr[12]) },
2267 { "r13", offsetof(CPUState, gpr[13]) },
2268 { "r14", offsetof(CPUState, gpr[14]) },
2269 { "r15", offsetof(CPUState, gpr[15]) },
2270 { "r16", offsetof(CPUState, gpr[16]) },
2271 { "r17", offsetof(CPUState, gpr[17]) },
2272 { "r18", offsetof(CPUState, gpr[18]) },
2273 { "r19", offsetof(CPUState, gpr[19]) },
2274 { "r20", offsetof(CPUState, gpr[20]) },
2275 { "r21", offsetof(CPUState, gpr[21]) },
2276 { "r22", offsetof(CPUState, gpr[22]) },
2277 { "r23", offsetof(CPUState, gpr[23]) },
2278 { "r24", offsetof(CPUState, gpr[24]) },
2279 { "r25", offsetof(CPUState, gpr[25]) },
2280 { "r26", offsetof(CPUState, gpr[26]) },
2281 { "r27", offsetof(CPUState, gpr[27]) },
2282 { "r28", offsetof(CPUState, gpr[28]) },
2283 { "r29", offsetof(CPUState, gpr[29]) },
2284 { "r30", offsetof(CPUState, gpr[30]) },
2285 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002286 /* Floating point registers */
2287 { "f0", offsetof(CPUState, fpr[0]) },
2288 { "f1", offsetof(CPUState, fpr[1]) },
2289 { "f2", offsetof(CPUState, fpr[2]) },
2290 { "f3", offsetof(CPUState, fpr[3]) },
2291 { "f4", offsetof(CPUState, fpr[4]) },
2292 { "f5", offsetof(CPUState, fpr[5]) },
2293 { "f6", offsetof(CPUState, fpr[6]) },
2294 { "f7", offsetof(CPUState, fpr[7]) },
2295 { "f8", offsetof(CPUState, fpr[8]) },
2296 { "f9", offsetof(CPUState, fpr[9]) },
2297 { "f10", offsetof(CPUState, fpr[10]) },
2298 { "f11", offsetof(CPUState, fpr[11]) },
2299 { "f12", offsetof(CPUState, fpr[12]) },
2300 { "f13", offsetof(CPUState, fpr[13]) },
2301 { "f14", offsetof(CPUState, fpr[14]) },
2302 { "f15", offsetof(CPUState, fpr[15]) },
2303 { "f16", offsetof(CPUState, fpr[16]) },
2304 { "f17", offsetof(CPUState, fpr[17]) },
2305 { "f18", offsetof(CPUState, fpr[18]) },
2306 { "f19", offsetof(CPUState, fpr[19]) },
2307 { "f20", offsetof(CPUState, fpr[20]) },
2308 { "f21", offsetof(CPUState, fpr[21]) },
2309 { "f22", offsetof(CPUState, fpr[22]) },
2310 { "f23", offsetof(CPUState, fpr[23]) },
2311 { "f24", offsetof(CPUState, fpr[24]) },
2312 { "f25", offsetof(CPUState, fpr[25]) },
2313 { "f26", offsetof(CPUState, fpr[26]) },
2314 { "f27", offsetof(CPUState, fpr[27]) },
2315 { "f28", offsetof(CPUState, fpr[28]) },
2316 { "f29", offsetof(CPUState, fpr[29]) },
2317 { "f30", offsetof(CPUState, fpr[30]) },
2318 { "f31", offsetof(CPUState, fpr[31]) },
2319 { "fpscr", offsetof(CPUState, fpscr) },
2320 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002321 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002322 { "lr", offsetof(CPUState, lr) },
2323 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002324 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002325 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002326 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002327 { "msr", 0, &monitor_get_msr, },
2328 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002329 { "tbu", 0, &monitor_get_tbu, },
2330 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002331#if defined(TARGET_PPC64)
2332 /* Address space register */
2333 { "asr", offsetof(CPUState, asr) },
2334#endif
2335 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002336 { "sdr1", offsetof(CPUState, sdr1) },
2337 { "sr0", offsetof(CPUState, sr[0]) },
2338 { "sr1", offsetof(CPUState, sr[1]) },
2339 { "sr2", offsetof(CPUState, sr[2]) },
2340 { "sr3", offsetof(CPUState, sr[3]) },
2341 { "sr4", offsetof(CPUState, sr[4]) },
2342 { "sr5", offsetof(CPUState, sr[5]) },
2343 { "sr6", offsetof(CPUState, sr[6]) },
2344 { "sr7", offsetof(CPUState, sr[7]) },
2345 { "sr8", offsetof(CPUState, sr[8]) },
2346 { "sr9", offsetof(CPUState, sr[9]) },
2347 { "sr10", offsetof(CPUState, sr[10]) },
2348 { "sr11", offsetof(CPUState, sr[11]) },
2349 { "sr12", offsetof(CPUState, sr[12]) },
2350 { "sr13", offsetof(CPUState, sr[13]) },
2351 { "sr14", offsetof(CPUState, sr[14]) },
2352 { "sr15", offsetof(CPUState, sr[15]) },
2353 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002354#elif defined(TARGET_SPARC)
2355 { "g0", offsetof(CPUState, gregs[0]) },
2356 { "g1", offsetof(CPUState, gregs[1]) },
2357 { "g2", offsetof(CPUState, gregs[2]) },
2358 { "g3", offsetof(CPUState, gregs[3]) },
2359 { "g4", offsetof(CPUState, gregs[4]) },
2360 { "g5", offsetof(CPUState, gregs[5]) },
2361 { "g6", offsetof(CPUState, gregs[6]) },
2362 { "g7", offsetof(CPUState, gregs[7]) },
2363 { "o0", 0, monitor_get_reg },
2364 { "o1", 1, monitor_get_reg },
2365 { "o2", 2, monitor_get_reg },
2366 { "o3", 3, monitor_get_reg },
2367 { "o4", 4, monitor_get_reg },
2368 { "o5", 5, monitor_get_reg },
2369 { "o6", 6, monitor_get_reg },
2370 { "o7", 7, monitor_get_reg },
2371 { "l0", 8, monitor_get_reg },
2372 { "l1", 9, monitor_get_reg },
2373 { "l2", 10, monitor_get_reg },
2374 { "l3", 11, monitor_get_reg },
2375 { "l4", 12, monitor_get_reg },
2376 { "l5", 13, monitor_get_reg },
2377 { "l6", 14, monitor_get_reg },
2378 { "l7", 15, monitor_get_reg },
2379 { "i0", 16, monitor_get_reg },
2380 { "i1", 17, monitor_get_reg },
2381 { "i2", 18, monitor_get_reg },
2382 { "i3", 19, monitor_get_reg },
2383 { "i4", 20, monitor_get_reg },
2384 { "i5", 21, monitor_get_reg },
2385 { "i6", 22, monitor_get_reg },
2386 { "i7", 23, monitor_get_reg },
2387 { "pc", offsetof(CPUState, pc) },
2388 { "npc", offsetof(CPUState, npc) },
2389 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002390#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002391 { "psr", 0, &monitor_get_psr, },
2392 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002393#endif
bellarde95c8d52004-09-30 22:22:08 +00002394 { "tbr", offsetof(CPUState, tbr) },
2395 { "fsr", offsetof(CPUState, fsr) },
2396 { "f0", offsetof(CPUState, fpr[0]) },
2397 { "f1", offsetof(CPUState, fpr[1]) },
2398 { "f2", offsetof(CPUState, fpr[2]) },
2399 { "f3", offsetof(CPUState, fpr[3]) },
2400 { "f4", offsetof(CPUState, fpr[4]) },
2401 { "f5", offsetof(CPUState, fpr[5]) },
2402 { "f6", offsetof(CPUState, fpr[6]) },
2403 { "f7", offsetof(CPUState, fpr[7]) },
2404 { "f8", offsetof(CPUState, fpr[8]) },
2405 { "f9", offsetof(CPUState, fpr[9]) },
2406 { "f10", offsetof(CPUState, fpr[10]) },
2407 { "f11", offsetof(CPUState, fpr[11]) },
2408 { "f12", offsetof(CPUState, fpr[12]) },
2409 { "f13", offsetof(CPUState, fpr[13]) },
2410 { "f14", offsetof(CPUState, fpr[14]) },
2411 { "f15", offsetof(CPUState, fpr[15]) },
2412 { "f16", offsetof(CPUState, fpr[16]) },
2413 { "f17", offsetof(CPUState, fpr[17]) },
2414 { "f18", offsetof(CPUState, fpr[18]) },
2415 { "f19", offsetof(CPUState, fpr[19]) },
2416 { "f20", offsetof(CPUState, fpr[20]) },
2417 { "f21", offsetof(CPUState, fpr[21]) },
2418 { "f22", offsetof(CPUState, fpr[22]) },
2419 { "f23", offsetof(CPUState, fpr[23]) },
2420 { "f24", offsetof(CPUState, fpr[24]) },
2421 { "f25", offsetof(CPUState, fpr[25]) },
2422 { "f26", offsetof(CPUState, fpr[26]) },
2423 { "f27", offsetof(CPUState, fpr[27]) },
2424 { "f28", offsetof(CPUState, fpr[28]) },
2425 { "f29", offsetof(CPUState, fpr[29]) },
2426 { "f30", offsetof(CPUState, fpr[30]) },
2427 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002428#ifdef TARGET_SPARC64
2429 { "f32", offsetof(CPUState, fpr[32]) },
2430 { "f34", offsetof(CPUState, fpr[34]) },
2431 { "f36", offsetof(CPUState, fpr[36]) },
2432 { "f38", offsetof(CPUState, fpr[38]) },
2433 { "f40", offsetof(CPUState, fpr[40]) },
2434 { "f42", offsetof(CPUState, fpr[42]) },
2435 { "f44", offsetof(CPUState, fpr[44]) },
2436 { "f46", offsetof(CPUState, fpr[46]) },
2437 { "f48", offsetof(CPUState, fpr[48]) },
2438 { "f50", offsetof(CPUState, fpr[50]) },
2439 { "f52", offsetof(CPUState, fpr[52]) },
2440 { "f54", offsetof(CPUState, fpr[54]) },
2441 { "f56", offsetof(CPUState, fpr[56]) },
2442 { "f58", offsetof(CPUState, fpr[58]) },
2443 { "f60", offsetof(CPUState, fpr[60]) },
2444 { "f62", offsetof(CPUState, fpr[62]) },
2445 { "asi", offsetof(CPUState, asi) },
2446 { "pstate", offsetof(CPUState, pstate) },
2447 { "cansave", offsetof(CPUState, cansave) },
2448 { "canrestore", offsetof(CPUState, canrestore) },
2449 { "otherwin", offsetof(CPUState, otherwin) },
2450 { "wstate", offsetof(CPUState, wstate) },
2451 { "cleanwin", offsetof(CPUState, cleanwin) },
2452 { "fprs", offsetof(CPUState, fprs) },
2453#endif
bellard9307c4c2004-04-04 12:57:25 +00002454#endif
2455 { NULL },
2456};
2457
aliguori376253e2009-03-05 23:01:23 +00002458static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002459{
aliguori376253e2009-03-05 23:01:23 +00002460 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002461 longjmp(expr_env, 1);
2462}
2463
bellard6a00d602005-11-21 23:25:50 +00002464/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002465static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002466{
blueswir18662d652008-10-02 18:32:44 +00002467 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002468 void *ptr;
2469
bellard9307c4c2004-04-04 12:57:25 +00002470 for(md = monitor_defs; md->name != NULL; md++) {
2471 if (compare_cmd(name, md->name)) {
2472 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002473 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002474 } else {
bellard6a00d602005-11-21 23:25:50 +00002475 CPUState *env = mon_get_cpu();
2476 if (!env)
2477 return -2;
2478 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002479 switch(md->type) {
2480 case MD_I32:
2481 *pval = *(int32_t *)ptr;
2482 break;
2483 case MD_TLONG:
2484 *pval = *(target_long *)ptr;
2485 break;
2486 default:
2487 *pval = 0;
2488 break;
2489 }
bellard9307c4c2004-04-04 12:57:25 +00002490 }
2491 return 0;
2492 }
2493 }
2494 return -1;
2495}
2496
2497static void next(void)
2498{
Blue Swirl660f11b2009-07-31 21:16:51 +00002499 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002500 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002501 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002502 pch++;
2503 }
2504}
2505
aliguori376253e2009-03-05 23:01:23 +00002506static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002507
aliguori376253e2009-03-05 23:01:23 +00002508static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002509{
blueswir1c2efc952007-09-25 17:28:42 +00002510 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002511 char *p;
bellard6a00d602005-11-21 23:25:50 +00002512 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002513
2514 switch(*pch) {
2515 case '+':
2516 next();
aliguori376253e2009-03-05 23:01:23 +00002517 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002518 break;
2519 case '-':
2520 next();
aliguori376253e2009-03-05 23:01:23 +00002521 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002522 break;
2523 case '~':
2524 next();
aliguori376253e2009-03-05 23:01:23 +00002525 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002526 break;
2527 case '(':
2528 next();
aliguori376253e2009-03-05 23:01:23 +00002529 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002530 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002531 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002532 }
2533 next();
2534 break;
bellard81d09122004-07-14 17:21:37 +00002535 case '\'':
2536 pch++;
2537 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002538 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002539 n = *pch;
2540 pch++;
2541 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002542 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002543 next();
2544 break;
bellard9307c4c2004-04-04 12:57:25 +00002545 case '$':
2546 {
2547 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002548 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002549
bellard9307c4c2004-04-04 12:57:25 +00002550 pch++;
2551 q = buf;
2552 while ((*pch >= 'a' && *pch <= 'z') ||
2553 (*pch >= 'A' && *pch <= 'Z') ||
2554 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002555 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002556 if ((q - buf) < sizeof(buf) - 1)
2557 *q++ = *pch;
2558 pch++;
2559 }
blueswir1cd390082008-11-16 13:53:32 +00002560 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002561 pch++;
2562 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002563 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002564 if (ret == -1)
aliguori376253e2009-03-05 23:01:23 +00002565 expr_error(mon, "unknown register");
ths5fafdf22007-09-16 21:08:06 +00002566 else if (ret == -2)
aliguori376253e2009-03-05 23:01:23 +00002567 expr_error(mon, "no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002568 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002569 }
2570 break;
2571 case '\0':
aliguori376253e2009-03-05 23:01:23 +00002572 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00002573 n = 0;
2574 break;
2575 default:
blueswir17743e582007-09-24 18:39:04 +00002576#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002577 n = strtoull(pch, &p, 0);
2578#else
bellard9307c4c2004-04-04 12:57:25 +00002579 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002580#endif
bellard9307c4c2004-04-04 12:57:25 +00002581 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00002582 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00002583 }
2584 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002585 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002586 pch++;
2587 break;
2588 }
2589 return n;
2590}
2591
2592
aliguori376253e2009-03-05 23:01:23 +00002593static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002594{
blueswir1c2efc952007-09-25 17:28:42 +00002595 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002596 int op;
ths3b46e622007-09-17 08:09:54 +00002597
aliguori376253e2009-03-05 23:01:23 +00002598 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002599 for(;;) {
2600 op = *pch;
2601 if (op != '*' && op != '/' && op != '%')
2602 break;
2603 next();
aliguori376253e2009-03-05 23:01:23 +00002604 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002605 switch(op) {
2606 default:
2607 case '*':
2608 val *= val2;
2609 break;
2610 case '/':
2611 case '%':
ths5fafdf22007-09-16 21:08:06 +00002612 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00002613 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002614 if (op == '/')
2615 val /= val2;
2616 else
2617 val %= val2;
2618 break;
2619 }
2620 }
2621 return val;
2622}
2623
aliguori376253e2009-03-05 23:01:23 +00002624static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002625{
blueswir1c2efc952007-09-25 17:28:42 +00002626 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002627 int op;
bellard9307c4c2004-04-04 12:57:25 +00002628
aliguori376253e2009-03-05 23:01:23 +00002629 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002630 for(;;) {
2631 op = *pch;
2632 if (op != '&' && op != '|' && op != '^')
2633 break;
2634 next();
aliguori376253e2009-03-05 23:01:23 +00002635 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00002636 switch(op) {
2637 default:
2638 case '&':
2639 val &= val2;
2640 break;
2641 case '|':
2642 val |= val2;
2643 break;
2644 case '^':
2645 val ^= val2;
2646 break;
2647 }
2648 }
2649 return val;
2650}
2651
aliguori376253e2009-03-05 23:01:23 +00002652static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002653{
blueswir1c2efc952007-09-25 17:28:42 +00002654 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002655 int op;
bellard9307c4c2004-04-04 12:57:25 +00002656
aliguori376253e2009-03-05 23:01:23 +00002657 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002658 for(;;) {
2659 op = *pch;
2660 if (op != '+' && op != '-')
2661 break;
2662 next();
aliguori376253e2009-03-05 23:01:23 +00002663 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00002664 if (op == '+')
2665 val += val2;
2666 else
2667 val -= val2;
2668 }
2669 return val;
2670}
2671
aliguori376253e2009-03-05 23:01:23 +00002672static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002673{
2674 pch = *pp;
2675 if (setjmp(expr_env)) {
2676 *pp = pch;
2677 return -1;
2678 }
blueswir1cd390082008-11-16 13:53:32 +00002679 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002680 pch++;
aliguori376253e2009-03-05 23:01:23 +00002681 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002682 *pp = pch;
2683 return 0;
2684}
2685
2686static int get_str(char *buf, int buf_size, const char **pp)
2687{
2688 const char *p;
2689 char *q;
2690 int c;
2691
bellard81d09122004-07-14 17:21:37 +00002692 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002693 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002694 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002695 p++;
2696 if (*p == '\0') {
2697 fail:
bellard81d09122004-07-14 17:21:37 +00002698 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002699 *pp = p;
2700 return -1;
2701 }
bellard9307c4c2004-04-04 12:57:25 +00002702 if (*p == '\"') {
2703 p++;
2704 while (*p != '\0' && *p != '\"') {
2705 if (*p == '\\') {
2706 p++;
2707 c = *p++;
2708 switch(c) {
2709 case 'n':
2710 c = '\n';
2711 break;
2712 case 'r':
2713 c = '\r';
2714 break;
2715 case '\\':
2716 case '\'':
2717 case '\"':
2718 break;
2719 default:
2720 qemu_printf("unsupported escape code: '\\%c'\n", c);
2721 goto fail;
2722 }
2723 if ((q - buf) < buf_size - 1) {
2724 *q++ = c;
2725 }
2726 } else {
2727 if ((q - buf) < buf_size - 1) {
2728 *q++ = *p;
2729 }
2730 p++;
2731 }
2732 }
2733 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002734 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002735 goto fail;
2736 }
2737 p++;
2738 } else {
blueswir1cd390082008-11-16 13:53:32 +00002739 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002740 if ((q - buf) < buf_size - 1) {
2741 *q++ = *p;
2742 }
2743 p++;
2744 }
bellard9307c4c2004-04-04 12:57:25 +00002745 }
bellard81d09122004-07-14 17:21:37 +00002746 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002747 *pp = p;
2748 return 0;
2749}
2750
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002751/*
2752 * Store the command-name in cmdname, and return a pointer to
2753 * the remaining of the command string.
2754 */
2755static const char *get_command_name(const char *cmdline,
2756 char *cmdname, size_t nlen)
2757{
2758 size_t len;
2759 const char *p, *pstart;
2760
2761 p = cmdline;
2762 while (qemu_isspace(*p))
2763 p++;
2764 if (*p == '\0')
2765 return NULL;
2766 pstart = p;
2767 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2768 p++;
2769 len = p - pstart;
2770 if (len > nlen - 1)
2771 len = nlen - 1;
2772 memcpy(cmdname, pstart, len);
2773 cmdname[len] = '\0';
2774 return p;
2775}
2776
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002777/**
2778 * Read key of 'type' into 'key' and return the current
2779 * 'type' pointer.
2780 */
2781static char *key_get_info(const char *type, char **key)
2782{
2783 size_t len;
2784 char *p, *str;
2785
2786 if (*type == ',')
2787 type++;
2788
2789 p = strchr(type, ':');
2790 if (!p) {
2791 *key = NULL;
2792 return NULL;
2793 }
2794 len = p - type;
2795
2796 str = qemu_malloc(len + 1);
2797 memcpy(str, type, len);
2798 str[len] = '\0';
2799
2800 *key = str;
2801 return ++p;
2802}
2803
bellard9307c4c2004-04-04 12:57:25 +00002804static int default_fmt_format = 'x';
2805static int default_fmt_size = 4;
2806
2807#define MAX_ARGS 16
2808
Anthony Liguoric227f092009-10-01 16:12:16 -05002809static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002810 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002811 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00002812{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002813 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03002814 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05002815 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002816 char cmdname[256];
2817 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002818 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00002819
2820#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00002821 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00002822#endif
ths3b46e622007-09-17 08:09:54 +00002823
bellard9307c4c2004-04-04 12:57:25 +00002824 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03002825 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2826 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002827 return NULL;
ths3b46e622007-09-17 08:09:54 +00002828
bellard9307c4c2004-04-04 12:57:25 +00002829 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00002830 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002831 if (compare_cmd(cmdname, cmd->name))
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002832 break;
bellard9dc39cb2004-03-14 21:38:27 +00002833 }
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002834
2835 if (cmd->name == NULL) {
2836 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03002837 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03002838 }
bellard9307c4c2004-04-04 12:57:25 +00002839
bellard9307c4c2004-04-04 12:57:25 +00002840 /* parse the parameters */
2841 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00002842 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002843 typestr = key_get_info(typestr, &key);
2844 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00002845 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03002846 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00002847 typestr++;
2848 switch(c) {
2849 case 'F':
bellard81d09122004-07-14 17:21:37 +00002850 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002851 case 's':
2852 {
2853 int ret;
ths3b46e622007-09-17 08:09:54 +00002854
blueswir1cd390082008-11-16 13:53:32 +00002855 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002856 p++;
2857 if (*typestr == '?') {
2858 typestr++;
2859 if (*p == '\0') {
2860 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03002861 break;
bellard9307c4c2004-04-04 12:57:25 +00002862 }
2863 }
2864 ret = get_str(buf, sizeof(buf), &p);
2865 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002866 switch(c) {
2867 case 'F':
aliguori376253e2009-03-05 23:01:23 +00002868 monitor_printf(mon, "%s: filename expected\n",
2869 cmdname);
bellard81d09122004-07-14 17:21:37 +00002870 break;
2871 case 'B':
aliguori376253e2009-03-05 23:01:23 +00002872 monitor_printf(mon, "%s: block device name expected\n",
2873 cmdname);
bellard81d09122004-07-14 17:21:37 +00002874 break;
2875 default:
aliguori376253e2009-03-05 23:01:23 +00002876 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002877 break;
2878 }
bellard9307c4c2004-04-04 12:57:25 +00002879 goto fail;
2880 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002881 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00002882 }
2883 break;
2884 case '/':
2885 {
2886 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002887
blueswir1cd390082008-11-16 13:53:32 +00002888 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002889 p++;
2890 if (*p == '/') {
2891 /* format found */
2892 p++;
2893 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002894 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002895 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002896 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002897 count = count * 10 + (*p - '0');
2898 p++;
2899 }
2900 }
2901 size = -1;
2902 format = -1;
2903 for(;;) {
2904 switch(*p) {
2905 case 'o':
2906 case 'd':
2907 case 'u':
2908 case 'x':
2909 case 'i':
2910 case 'c':
2911 format = *p++;
2912 break;
2913 case 'b':
2914 size = 1;
2915 p++;
2916 break;
2917 case 'h':
2918 size = 2;
2919 p++;
2920 break;
2921 case 'w':
2922 size = 4;
2923 p++;
2924 break;
2925 case 'g':
2926 case 'L':
2927 size = 8;
2928 p++;
2929 break;
2930 default:
2931 goto next;
2932 }
2933 }
2934 next:
blueswir1cd390082008-11-16 13:53:32 +00002935 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00002936 monitor_printf(mon, "invalid char in format: '%c'\n",
2937 *p);
bellard9307c4c2004-04-04 12:57:25 +00002938 goto fail;
2939 }
bellard9307c4c2004-04-04 12:57:25 +00002940 if (format < 0)
2941 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002942 if (format != 'i') {
2943 /* for 'i', not specifying a size gives -1 as size */
2944 if (size < 0)
2945 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002946 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002947 }
bellard9307c4c2004-04-04 12:57:25 +00002948 default_fmt_format = format;
2949 } else {
2950 count = 1;
2951 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002952 if (format != 'i') {
2953 size = default_fmt_size;
2954 } else {
2955 size = -1;
2956 }
bellard9307c4c2004-04-04 12:57:25 +00002957 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03002958 qdict_put(qdict, "count", qint_from_int(count));
2959 qdict_put(qdict, "format", qint_from_int(format));
2960 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00002961 }
2962 break;
2963 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002964 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002965 {
blueswir1c2efc952007-09-25 17:28:42 +00002966 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002967
blueswir1cd390082008-11-16 13:53:32 +00002968 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002969 p++;
bellard34405572004-06-08 00:55:58 +00002970 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002971 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03002972 if (*p == '\0') {
2973 typestr++;
2974 break;
2975 }
bellard34405572004-06-08 00:55:58 +00002976 } else {
2977 if (*p == '.') {
2978 p++;
blueswir1cd390082008-11-16 13:53:32 +00002979 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002980 p++;
bellard34405572004-06-08 00:55:58 +00002981 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03002982 typestr++;
2983 break;
bellard34405572004-06-08 00:55:58 +00002984 }
2985 }
bellard13224a82006-07-14 22:03:35 +00002986 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002987 }
aliguori376253e2009-03-05 23:01:23 +00002988 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00002989 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03002990 /* Check if 'i' is greater than 32-bit */
2991 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2992 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2993 monitor_printf(mon, "integer is for 32-bit values\n");
2994 goto fail;
2995 }
Luiz Capitulino53773582009-08-28 15:27:25 -03002996 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00002997 }
2998 break;
2999 case '-':
3000 {
3001 int has_option;
3002 /* option */
ths3b46e622007-09-17 08:09:54 +00003003
bellard9307c4c2004-04-04 12:57:25 +00003004 c = *typestr++;
3005 if (c == '\0')
3006 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003007 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003008 p++;
3009 has_option = 0;
3010 if (*p == '-') {
3011 p++;
3012 if (*p != c) {
aliguori376253e2009-03-05 23:01:23 +00003013 monitor_printf(mon, "%s: unsupported option -%c\n",
3014 cmdname, *p);
bellard9307c4c2004-04-04 12:57:25 +00003015 goto fail;
3016 }
3017 p++;
3018 has_option = 1;
3019 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003020 qdict_put(qdict, key, qint_from_int(has_option));
bellard9307c4c2004-04-04 12:57:25 +00003021 }
3022 break;
3023 default:
3024 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003025 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003026 goto fail;
3027 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003028 qemu_free(key);
3029 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003030 }
3031 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003032 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003033 p++;
3034 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003035 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3036 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003037 goto fail;
3038 }
3039
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003040 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003041
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003042fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003043 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003044 return NULL;
3045}
3046
3047static void monitor_handle_command(Monitor *mon, const char *cmdline)
3048{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003049 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003050 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003051
3052 qdict = qdict_new();
3053
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003054 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003055 if (!cmd)
3056 goto out;
3057
3058 qemu_errors_to_mon(mon);
3059
3060 if (monitor_handler_ported(cmd)) {
3061 QObject *data = NULL;
3062
3063 cmd->mhandler.cmd_new(mon, qdict, &data);
3064 if (data)
3065 cmd->user_print(mon, data);
3066
3067 qobject_decref(data);
3068 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003069 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003070 }
3071
Luiz Capitulino13917be2009-10-07 13:41:54 -03003072 qemu_errors_to_previous();
3073
3074out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003075 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003076}
3077
bellard81d09122004-07-14 17:21:37 +00003078static void cmd_completion(const char *name, const char *list)
3079{
3080 const char *p, *pstart;
3081 char cmd[128];
3082 int len;
3083
3084 p = list;
3085 for(;;) {
3086 pstart = p;
3087 p = strchr(p, '|');
3088 if (!p)
3089 p = pstart + strlen(pstart);
3090 len = p - pstart;
3091 if (len > sizeof(cmd) - 2)
3092 len = sizeof(cmd) - 2;
3093 memcpy(cmd, pstart, len);
3094 cmd[len] = '\0';
3095 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003096 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003097 }
3098 if (*p == '\0')
3099 break;
3100 p++;
3101 }
3102}
3103
3104static void file_completion(const char *input)
3105{
3106 DIR *ffs;
3107 struct dirent *d;
3108 char path[1024];
3109 char file[1024], file_prefix[1024];
3110 int input_path_len;
3111 const char *p;
3112
ths5fafdf22007-09-16 21:08:06 +00003113 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003114 if (!p) {
3115 input_path_len = 0;
3116 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003117 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003118 } else {
3119 input_path_len = p - input + 1;
3120 memcpy(path, input, input_path_len);
3121 if (input_path_len > sizeof(path) - 1)
3122 input_path_len = sizeof(path) - 1;
3123 path[input_path_len] = '\0';
3124 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3125 }
3126#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003127 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3128 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003129#endif
3130 ffs = opendir(path);
3131 if (!ffs)
3132 return;
3133 for(;;) {
3134 struct stat sb;
3135 d = readdir(ffs);
3136 if (!d)
3137 break;
3138 if (strstart(d->d_name, file_prefix, NULL)) {
3139 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003140 if (input_path_len < sizeof(file))
3141 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3142 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003143 /* stat the file to find out if it's a directory.
3144 * In that case add a slash to speed up typing long paths
3145 */
3146 stat(file, &sb);
3147 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003148 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003149 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003150 }
3151 }
3152 closedir(ffs);
3153}
3154
aliguori51de9762009-03-05 23:00:43 +00003155static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003156{
aliguori51de9762009-03-05 23:00:43 +00003157 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003158 const char *input = opaque;
3159
3160 if (input[0] == '\0' ||
3161 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003162 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003163 }
3164}
3165
3166/* NOTE: this parser is an approximate form of the real command parser */
3167static void parse_cmdline(const char *cmdline,
3168 int *pnb_args, char **args)
3169{
3170 const char *p;
3171 int nb_args, ret;
3172 char buf[1024];
3173
3174 p = cmdline;
3175 nb_args = 0;
3176 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003177 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003178 p++;
3179 if (*p == '\0')
3180 break;
3181 if (nb_args >= MAX_ARGS)
3182 break;
3183 ret = get_str(buf, sizeof(buf), &p);
3184 args[nb_args] = qemu_strdup(buf);
3185 nb_args++;
3186 if (ret < 0)
3187 break;
3188 }
3189 *pnb_args = nb_args;
3190}
3191
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003192static const char *next_arg_type(const char *typestr)
3193{
3194 const char *p = strchr(typestr, ':');
3195 return (p != NULL ? ++p : typestr);
3196}
3197
aliguori4c36ba32009-03-05 23:01:37 +00003198static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003199{
3200 const char *cmdname;
3201 char *args[MAX_ARGS];
3202 int nb_args, i, len;
3203 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003204 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003205 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003206
3207 parse_cmdline(cmdline, &nb_args, args);
3208#ifdef DEBUG_COMPLETION
3209 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003210 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003211 }
3212#endif
3213
3214 /* if the line ends with a space, it means we want to complete the
3215 next arg */
3216 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003217 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00003218 if (nb_args >= MAX_ARGS)
3219 return;
3220 args[nb_args++] = qemu_strdup("");
3221 }
3222 if (nb_args <= 1) {
3223 /* command completion */
3224 if (nb_args == 0)
3225 cmdname = "";
3226 else
3227 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003228 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003229 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003230 cmd_completion(cmdname, cmd->name);
3231 }
3232 } else {
3233 /* find the command */
aliguori376253e2009-03-05 23:01:23 +00003234 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003235 if (compare_cmd(args[0], cmd->name))
3236 goto found;
3237 }
3238 return;
3239 found:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003240 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003241 for(i = 0; i < nb_args - 2; i++) {
3242 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003243 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003244 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003245 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003246 }
3247 }
3248 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003249 if (*ptype == '-' && ptype[1] != '\0') {
3250 ptype += 2;
3251 }
bellard81d09122004-07-14 17:21:37 +00003252 switch(*ptype) {
3253 case 'F':
3254 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003255 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003256 file_completion(str);
3257 break;
3258 case 'B':
3259 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003260 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003261 bdrv_iterate(block_completion_it, (void *)str);
3262 break;
bellard7fe48482004-10-09 18:08:01 +00003263 case 's':
3264 /* XXX: more generic ? */
3265 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003266 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003267 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3268 cmd_completion(str, cmd->name);
3269 }
bellard64866c32006-05-07 18:03:31 +00003270 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003271 char *sep = strrchr(str, '-');
3272 if (sep)
3273 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003274 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003275 for(key = key_defs; key->name != NULL; key++) {
3276 cmd_completion(str, key->name);
3277 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003278 } else if (!strcmp(cmd->name, "help|?")) {
3279 readline_set_completion_index(cur_mon->rs, strlen(str));
3280 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3281 cmd_completion(str, cmd->name);
3282 }
bellard7fe48482004-10-09 18:08:01 +00003283 }
3284 break;
bellard81d09122004-07-14 17:21:37 +00003285 default:
3286 break;
3287 }
3288 }
3289 for(i = 0; i < nb_args; i++)
3290 qemu_free(args[i]);
3291}
3292
aliguori731b0362009-03-05 23:01:42 +00003293static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003294{
aliguori731b0362009-03-05 23:01:42 +00003295 Monitor *mon = opaque;
3296
3297 return (mon->suspend_cnt == 0) ? 128 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003298}
3299
aliguori731b0362009-03-05 23:01:42 +00003300static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00003301{
aliguori731b0362009-03-05 23:01:42 +00003302 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00003303 int i;
aliguori376253e2009-03-05 23:01:23 +00003304
aliguori731b0362009-03-05 23:01:42 +00003305 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00003306
aliguoricde76ee2009-03-05 23:01:51 +00003307 if (cur_mon->rs) {
3308 for (i = 0; i < size; i++)
3309 readline_handle_byte(cur_mon->rs, buf[i]);
3310 } else {
3311 if (size == 0 || buf[size - 1] != 0)
3312 monitor_printf(cur_mon, "corrupted command\n");
3313 else
3314 monitor_handle_command(cur_mon, (char *)buf);
3315 }
aliguori731b0362009-03-05 23:01:42 +00003316
3317 cur_mon = old_mon;
3318}
aliguorid8f44602008-10-06 13:52:44 +00003319
aliguori376253e2009-03-05 23:01:23 +00003320static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003321{
aliguori731b0362009-03-05 23:01:42 +00003322 monitor_suspend(mon);
aliguori376253e2009-03-05 23:01:23 +00003323 monitor_handle_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00003324 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00003325}
3326
aliguoricde76ee2009-03-05 23:01:51 +00003327int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003328{
aliguoricde76ee2009-03-05 23:01:51 +00003329 if (!mon->rs)
3330 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00003331 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00003332 return 0;
aliguorid8f44602008-10-06 13:52:44 +00003333}
3334
aliguori376253e2009-03-05 23:01:23 +00003335void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00003336{
aliguoricde76ee2009-03-05 23:01:51 +00003337 if (!mon->rs)
3338 return;
aliguori731b0362009-03-05 23:01:42 +00003339 if (--mon->suspend_cnt == 0)
3340 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00003341}
3342
aliguori731b0362009-03-05 23:01:42 +00003343static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00003344{
aliguori376253e2009-03-05 23:01:23 +00003345 Monitor *mon = opaque;
3346
aliguori2724b182009-03-05 23:01:47 +00003347 switch (event) {
3348 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003349 mon->mux_out = 0;
3350 if (mon->reset_seen) {
3351 readline_restart(mon->rs);
3352 monitor_resume(mon);
3353 monitor_flush(mon);
3354 } else {
3355 mon->suspend_cnt = 0;
3356 }
aliguori2724b182009-03-05 23:01:47 +00003357 break;
ths86e94de2007-01-05 22:01:59 +00003358
aliguori2724b182009-03-05 23:01:47 +00003359 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003360 if (mon->reset_seen) {
3361 if (mon->suspend_cnt == 0) {
3362 monitor_printf(mon, "\n");
3363 }
3364 monitor_flush(mon);
3365 monitor_suspend(mon);
3366 } else {
3367 mon->suspend_cnt++;
3368 }
3369 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00003370 break;
3371
3372 case CHR_EVENT_RESET:
3373 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3374 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003375 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00003376 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02003377 }
3378 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00003379 break;
3380 }
ths86e94de2007-01-05 22:01:59 +00003381}
3382
aliguori76655d62009-03-06 20:27:37 +00003383
3384/*
3385 * Local variables:
3386 * c-indent-level: 4
3387 * c-basic-offset: 4
3388 * tab-width: 8
3389 * End:
3390 */
3391
aliguori731b0362009-03-05 23:01:42 +00003392void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00003393{
aliguori731b0362009-03-05 23:01:42 +00003394 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00003395 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00003396
3397 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00003398 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00003399 is_first_init = 0;
3400 }
aliguori87127162009-03-05 23:01:29 +00003401
3402 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00003403
aliguori87127162009-03-05 23:01:29 +00003404 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00003405 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00003406 if (flags & MONITOR_USE_READLINE) {
3407 mon->rs = readline_init(mon, monitor_find_completion);
3408 monitor_read_command(mon, 0);
3409 }
aliguori87127162009-03-05 23:01:29 +00003410
aliguori731b0362009-03-05 23:01:42 +00003411 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3412 mon);
aliguori87127162009-03-05 23:01:29 +00003413
Blue Swirl72cf2d42009-09-12 07:36:22 +00003414 QLIST_INSERT_HEAD(&mon_list, mon, entry);
aliguori731b0362009-03-05 23:01:42 +00003415 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
aliguori87127162009-03-05 23:01:29 +00003416 cur_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00003417}
3418
aliguori376253e2009-03-05 23:01:23 +00003419static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00003420{
aliguoribb5fc202009-03-05 23:01:15 +00003421 BlockDriverState *bs = opaque;
3422 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00003423
aliguoribb5fc202009-03-05 23:01:15 +00003424 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00003425 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00003426 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00003427 }
aliguori731b0362009-03-05 23:01:42 +00003428 if (mon->password_completion_cb)
3429 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00003430
aliguori731b0362009-03-05 23:01:42 +00003431 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00003432}
aliguoric0f4ce72009-03-05 23:01:01 +00003433
aliguori376253e2009-03-05 23:01:23 +00003434void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
aliguoribb5fc202009-03-05 23:01:15 +00003435 BlockDriverCompletionFunc *completion_cb,
3436 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00003437{
aliguoricde76ee2009-03-05 23:01:51 +00003438 int err;
3439
aliguoribb5fc202009-03-05 23:01:15 +00003440 if (!bdrv_key_required(bs)) {
3441 if (completion_cb)
3442 completion_cb(opaque, 0);
3443 return;
3444 }
aliguoric0f4ce72009-03-05 23:01:01 +00003445
aliguori376253e2009-03-05 23:01:23 +00003446 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3447 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00003448
aliguori731b0362009-03-05 23:01:42 +00003449 mon->password_completion_cb = completion_cb;
3450 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00003451
aliguoricde76ee2009-03-05 23:01:51 +00003452 err = monitor_read_password(mon, bdrv_password_cb, bs);
3453
3454 if (err && completion_cb)
3455 completion_cb(opaque, err);
aliguoric0f4ce72009-03-05 23:01:01 +00003456}
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003457
3458typedef struct QemuErrorSink QemuErrorSink;
3459struct QemuErrorSink {
3460 enum {
3461 ERR_SINK_FILE,
3462 ERR_SINK_MONITOR,
3463 } dest;
3464 union {
3465 FILE *fp;
3466 Monitor *mon;
3467 };
3468 QemuErrorSink *previous;
3469};
3470
Blue Swirl528e93a2009-08-31 15:14:40 +00003471static QemuErrorSink *qemu_error_sink;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003472
3473void qemu_errors_to_file(FILE *fp)
3474{
3475 QemuErrorSink *sink;
3476
3477 sink = qemu_mallocz(sizeof(*sink));
3478 sink->dest = ERR_SINK_FILE;
3479 sink->fp = fp;
3480 sink->previous = qemu_error_sink;
3481 qemu_error_sink = sink;
3482}
3483
3484void qemu_errors_to_mon(Monitor *mon)
3485{
3486 QemuErrorSink *sink;
3487
3488 sink = qemu_mallocz(sizeof(*sink));
3489 sink->dest = ERR_SINK_MONITOR;
3490 sink->mon = mon;
3491 sink->previous = qemu_error_sink;
3492 qemu_error_sink = sink;
3493}
3494
3495void qemu_errors_to_previous(void)
3496{
3497 QemuErrorSink *sink;
3498
3499 assert(qemu_error_sink != NULL);
3500 sink = qemu_error_sink;
3501 qemu_error_sink = sink->previous;
3502 qemu_free(sink);
3503}
3504
3505void qemu_error(const char *fmt, ...)
3506{
3507 va_list args;
3508
3509 assert(qemu_error_sink != NULL);
3510 switch (qemu_error_sink->dest) {
3511 case ERR_SINK_FILE:
3512 va_start(args, fmt);
3513 vfprintf(qemu_error_sink->fp, fmt, args);
3514 va_end(args);
3515 break;
3516 case ERR_SINK_MONITOR:
3517 va_start(args, fmt);
3518 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3519 va_end(args);
3520 break;
3521 }
3522}