Statistics
| Branch: | Revision:

root / monitor.c @ c80d259e

History | View | Annotate | Download (91.9 kB)

1 9dc39cba bellard
/*
2 9dc39cba bellard
 * QEMU monitor
3 5fafdf24 ths
 *
4 9dc39cba bellard
 * Copyright (c) 2003-2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 9dc39cba bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 9dc39cba bellard
 * of this software and associated documentation files (the "Software"), to deal
8 9dc39cba bellard
 * in the Software without restriction, including without limitation the rights
9 9dc39cba bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 9dc39cba bellard
 * copies of the Software, and to permit persons to whom the Software is
11 9dc39cba bellard
 * furnished to do so, subject to the following conditions:
12 9dc39cba bellard
 *
13 9dc39cba bellard
 * The above copyright notice and this permission notice shall be included in
14 9dc39cba bellard
 * all copies or substantial portions of the Software.
15 9dc39cba bellard
 *
16 9dc39cba bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 9dc39cba bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 9dc39cba bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 9dc39cba bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 9dc39cba bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 9dc39cba bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 9dc39cba bellard
 * THE SOFTWARE.
23 9dc39cba bellard
 */
24 511d2b14 blueswir1
#include <dirent.h>
25 87ecb68b pbrook
#include "hw/hw.h"
26 cae4956e Gerd Hoffmann
#include "hw/qdev.h"
27 87ecb68b pbrook
#include "hw/usb.h"
28 87ecb68b pbrook
#include "hw/pcmcia.h"
29 87ecb68b pbrook
#include "hw/pc.h"
30 87ecb68b pbrook
#include "hw/pci.h"
31 9dd986cc Richard W.M. Jones
#include "hw/watchdog.h"
32 45a50b16 Gerd Hoffmann
#include "hw/loader.h"
33 87ecb68b pbrook
#include "gdbstub.h"
34 87ecb68b pbrook
#include "net.h"
35 87ecb68b pbrook
#include "qemu-char.h"
36 87ecb68b pbrook
#include "sysemu.h"
37 376253ec aliguori
#include "monitor.h"
38 376253ec aliguori
#include "readline.h"
39 87ecb68b pbrook
#include "console.h"
40 87ecb68b pbrook
#include "block.h"
41 87ecb68b pbrook
#include "audio/audio.h"
42 9307c4c1 bellard
#include "disas.h"
43 df751fa8 aliguori
#include "balloon.h"
44 c8256f9d balrog
#include "qemu-timer.h"
45 5bb7910a aliguori
#include "migration.h"
46 7ba1e619 aliguori
#include "kvm.h"
47 76655d6d aliguori
#include "acl.h"
48 f7188bbe Luiz Capitulino
#include "qint.h"
49 f7188bbe Luiz Capitulino
#include "qdict.h"
50 f7188bbe Luiz Capitulino
#include "qstring.h"
51 6a5bd307 ths
52 9dc39cba bellard
//#define DEBUG
53 81d0912d bellard
//#define DEBUG_COMPLETION
54 9dc39cba bellard
55 9307c4c1 bellard
/*
56 9307c4c1 bellard
 * Supported types:
57 5fafdf24 ths
 *
58 9307c4c1 bellard
 * 'F'          filename
59 81d0912d bellard
 * 'B'          block device name
60 9307c4c1 bellard
 * 's'          string (accept optional quote)
61 92a31b1f bellard
 * 'i'          32 bit integer
62 92a31b1f bellard
 * 'l'          target long (32 or 64 bit)
63 9307c4c1 bellard
 * '/'          optional gdb-like print format (like "/10x")
64 9307c4c1 bellard
 *
65 fb46660e Luiz Capitulino
 * '?'          optional type (for all types, except '/')
66 fb46660e Luiz Capitulino
 * '.'          other form of optional type (for 'i' and 'l')
67 fb46660e Luiz Capitulino
 * '-'          optional parameter (eg. '-f')
68 9307c4c1 bellard
 *
69 9307c4c1 bellard
 */
70 9307c4c1 bellard
71 c227f099 Anthony Liguori
typedef struct mon_cmd_t {
72 9dc39cba bellard
    const char *name;
73 9307c4c1 bellard
    const char *args_type;
74 9dc39cba bellard
    const char *params;
75 9dc39cba bellard
    const char *help;
76 a2876f59 Luiz Capitulino
    void (*user_print)(Monitor *mon, const QObject *data);
77 910df89d Luiz Capitulino
    union {
78 910df89d Luiz Capitulino
        void (*info)(Monitor *mon);
79 13c7425e Luiz Capitulino
        void (*info_new)(Monitor *mon, QObject **ret_data);
80 af4ce882 Luiz Capitulino
        void (*cmd)(Monitor *mon, const QDict *qdict);
81 13917bee Luiz Capitulino
        void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
82 910df89d Luiz Capitulino
    } mhandler;
83 c227f099 Anthony Liguori
} mon_cmd_t;
84 9dc39cba bellard
85 f07918fd Mark McLoughlin
/* file descriptors passed via SCM_RIGHTS */
86 c227f099 Anthony Liguori
typedef struct mon_fd_t mon_fd_t;
87 c227f099 Anthony Liguori
struct mon_fd_t {
88 f07918fd Mark McLoughlin
    char *name;
89 f07918fd Mark McLoughlin
    int fd;
90 c227f099 Anthony Liguori
    QLIST_ENTRY(mon_fd_t) next;
91 f07918fd Mark McLoughlin
};
92 f07918fd Mark McLoughlin
93 87127161 aliguori
struct Monitor {
94 87127161 aliguori
    CharDriverState *chr;
95 a7aec5da Gerd Hoffmann
    int mux_out;
96 a7aec5da Gerd Hoffmann
    int reset_seen;
97 731b0364 aliguori
    int flags;
98 731b0364 aliguori
    int suspend_cnt;
99 731b0364 aliguori
    uint8_t outbuf[1024];
100 731b0364 aliguori
    int outbuf_index;
101 731b0364 aliguori
    ReadLineState *rs;
102 731b0364 aliguori
    CPUState *mon_cpu;
103 731b0364 aliguori
    BlockDriverCompletionFunc *password_completion_cb;
104 731b0364 aliguori
    void *password_opaque;
105 c227f099 Anthony Liguori
    QLIST_HEAD(,mon_fd_t) fds;
106 72cf2d4f Blue Swirl
    QLIST_ENTRY(Monitor) entry;
107 87127161 aliguori
};
108 87127161 aliguori
109 72cf2d4f Blue Swirl
static QLIST_HEAD(mon_list, Monitor) mon_list;
110 7e2515e8 bellard
111 c227f099 Anthony Liguori
static const mon_cmd_t mon_cmds[];
112 c227f099 Anthony Liguori
static const mon_cmd_t info_cmds[];
113 9dc39cba bellard
114 87127161 aliguori
Monitor *cur_mon = NULL;
115 376253ec aliguori
116 731b0364 aliguori
static void monitor_command_cb(Monitor *mon, const char *cmdline,
117 731b0364 aliguori
                               void *opaque);
118 83ab7950 aliguori
119 731b0364 aliguori
static void monitor_read_command(Monitor *mon, int show_prompt)
120 731b0364 aliguori
{
121 731b0364 aliguori
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
122 731b0364 aliguori
    if (show_prompt)
123 731b0364 aliguori
        readline_show_prompt(mon->rs);
124 731b0364 aliguori
}
125 6a00d601 bellard
126 cde76ee1 aliguori
static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
127 cde76ee1 aliguori
                                 void *opaque)
128 bb5fc20f aliguori
{
129 cde76ee1 aliguori
    if (mon->rs) {
130 cde76ee1 aliguori
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
131 cde76ee1 aliguori
        /* prompt is printed on return from the command handler */
132 cde76ee1 aliguori
        return 0;
133 cde76ee1 aliguori
    } else {
134 cde76ee1 aliguori
        monitor_printf(mon, "terminal does not support password prompting\n");
135 cde76ee1 aliguori
        return -ENOTTY;
136 cde76ee1 aliguori
    }
137 bb5fc20f aliguori
}
138 bb5fc20f aliguori
139 376253ec aliguori
void monitor_flush(Monitor *mon)
140 7e2515e8 bellard
{
141 a7aec5da Gerd Hoffmann
    if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
142 731b0364 aliguori
        qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
143 731b0364 aliguori
        mon->outbuf_index = 0;
144 7e2515e8 bellard
    }
145 7e2515e8 bellard
}
146 7e2515e8 bellard
147 7e2515e8 bellard
/* flush at every end of line or if the buffer is full */
148 376253ec aliguori
static void monitor_puts(Monitor *mon, const char *str)
149 7e2515e8 bellard
{
150 60fe76f3 ths
    char c;
151 731b0364 aliguori
152 731b0364 aliguori
    if (!mon)
153 731b0364 aliguori
        return;
154 731b0364 aliguori
155 7e2515e8 bellard
    for(;;) {
156 7e2515e8 bellard
        c = *str++;
157 7e2515e8 bellard
        if (c == '\0')
158 7e2515e8 bellard
            break;
159 7ba1260a bellard
        if (c == '\n')
160 731b0364 aliguori
            mon->outbuf[mon->outbuf_index++] = '\r';
161 731b0364 aliguori
        mon->outbuf[mon->outbuf_index++] = c;
162 731b0364 aliguori
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
163 731b0364 aliguori
            || c == '\n')
164 376253ec aliguori
            monitor_flush(mon);
165 7e2515e8 bellard
    }
166 7e2515e8 bellard
}
167 7e2515e8 bellard
168 376253ec aliguori
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
169 9dc39cba bellard
{
170 81d0912d bellard
    char buf[4096];
171 81d0912d bellard
    vsnprintf(buf, sizeof(buf), fmt, ap);
172 376253ec aliguori
    monitor_puts(mon, buf);
173 9dc39cba bellard
}
174 9dc39cba bellard
175 376253ec aliguori
void monitor_printf(Monitor *mon, const char *fmt, ...)
176 9dc39cba bellard
{
177 7e2515e8 bellard
    va_list ap;
178 7e2515e8 bellard
    va_start(ap, fmt);
179 376253ec aliguori
    monitor_vprintf(mon, fmt, ap);
180 7e2515e8 bellard
    va_end(ap);
181 9dc39cba bellard
}
182 9dc39cba bellard
183 376253ec aliguori
void monitor_print_filename(Monitor *mon, const char *filename)
184 fef30743 ths
{
185 fef30743 ths
    int i;
186 fef30743 ths
187 fef30743 ths
    for (i = 0; filename[i]; i++) {
188 28a76be8 aliguori
        switch (filename[i]) {
189 28a76be8 aliguori
        case ' ':
190 28a76be8 aliguori
        case '"':
191 28a76be8 aliguori
        case '\\':
192 28a76be8 aliguori
            monitor_printf(mon, "\\%c", filename[i]);
193 28a76be8 aliguori
            break;
194 28a76be8 aliguori
        case '\t':
195 28a76be8 aliguori
            monitor_printf(mon, "\\t");
196 28a76be8 aliguori
            break;
197 28a76be8 aliguori
        case '\r':
198 28a76be8 aliguori
            monitor_printf(mon, "\\r");
199 28a76be8 aliguori
            break;
200 28a76be8 aliguori
        case '\n':
201 28a76be8 aliguori
            monitor_printf(mon, "\\n");
202 28a76be8 aliguori
            break;
203 28a76be8 aliguori
        default:
204 28a76be8 aliguori
            monitor_printf(mon, "%c", filename[i]);
205 28a76be8 aliguori
            break;
206 28a76be8 aliguori
        }
207 fef30743 ths
    }
208 fef30743 ths
}
209 fef30743 ths
210 7fe48483 bellard
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
211 7fe48483 bellard
{
212 7fe48483 bellard
    va_list ap;
213 7fe48483 bellard
    va_start(ap, fmt);
214 376253ec aliguori
    monitor_vprintf((Monitor *)stream, fmt, ap);
215 7fe48483 bellard
    va_end(ap);
216 7fe48483 bellard
    return 0;
217 7fe48483 bellard
}
218 7fe48483 bellard
219 13c7425e Luiz Capitulino
static void monitor_user_noop(Monitor *mon, const QObject *data) { }
220 13c7425e Luiz Capitulino
221 13917bee Luiz Capitulino
static inline int monitor_handler_ported(const mon_cmd_t *cmd)
222 13917bee Luiz Capitulino
{
223 13917bee Luiz Capitulino
    return cmd->user_print != NULL;
224 13917bee Luiz Capitulino
}
225 13917bee Luiz Capitulino
226 9dc39cba bellard
static int compare_cmd(const char *name, const char *list)
227 9dc39cba bellard
{
228 9dc39cba bellard
    const char *p, *pstart;
229 9dc39cba bellard
    int len;
230 9dc39cba bellard
    len = strlen(name);
231 9dc39cba bellard
    p = list;
232 9dc39cba bellard
    for(;;) {
233 9dc39cba bellard
        pstart = p;
234 9dc39cba bellard
        p = strchr(p, '|');
235 9dc39cba bellard
        if (!p)
236 9dc39cba bellard
            p = pstart + strlen(pstart);
237 9dc39cba bellard
        if ((p - pstart) == len && !memcmp(pstart, name, len))
238 9dc39cba bellard
            return 1;
239 9dc39cba bellard
        if (*p == '\0')
240 9dc39cba bellard
            break;
241 9dc39cba bellard
        p++;
242 9dc39cba bellard
    }
243 9dc39cba bellard
    return 0;
244 9dc39cba bellard
}
245 9dc39cba bellard
246 c227f099 Anthony Liguori
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
247 376253ec aliguori
                          const char *prefix, const char *name)
248 9dc39cba bellard
{
249 c227f099 Anthony Liguori
    const mon_cmd_t *cmd;
250 9dc39cba bellard
251 9dc39cba bellard
    for(cmd = cmds; cmd->name != NULL; cmd++) {
252 9dc39cba bellard
        if (!name || !strcmp(name, cmd->name))
253 376253ec aliguori
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
254 376253ec aliguori
                           cmd->params, cmd->help);
255 9dc39cba bellard
    }
256 9dc39cba bellard
}
257 9dc39cba bellard
258 376253ec aliguori
static void help_cmd(Monitor *mon, const char *name)
259 9dc39cba bellard
{
260 9dc39cba bellard
    if (name && !strcmp(name, "info")) {
261 376253ec aliguori
        help_cmd_dump(mon, info_cmds, "info ", NULL);
262 9dc39cba bellard
    } else {
263 376253ec aliguori
        help_cmd_dump(mon, mon_cmds, "", name);
264 f193c797 bellard
        if (name && !strcmp(name, "log")) {
265 8662d656 blueswir1
            const CPULogItem *item;
266 376253ec aliguori
            monitor_printf(mon, "Log items (comma separated):\n");
267 376253ec aliguori
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
268 f193c797 bellard
            for(item = cpu_log_items; item->mask != 0; item++) {
269 376253ec aliguori
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
270 f193c797 bellard
            }
271 f193c797 bellard
        }
272 9dc39cba bellard
    }
273 9dc39cba bellard
}
274 9dc39cba bellard
275 d54908a5 Luiz Capitulino
static void do_help_cmd(Monitor *mon, const QDict *qdict)
276 38183186 Luiz Capitulino
{
277 d54908a5 Luiz Capitulino
    help_cmd(mon, qdict_get_try_str(qdict, "name"));
278 38183186 Luiz Capitulino
}
279 38183186 Luiz Capitulino
280 d54908a5 Luiz Capitulino
static void do_commit(Monitor *mon, const QDict *qdict)
281 9dc39cba bellard
{
282 751c6a17 Gerd Hoffmann
    int all_devices;
283 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
284 d54908a5 Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
285 2dc7b602 balrog
286 7954c734 bellard
    all_devices = !strcmp(device, "all");
287 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(dinfo, &drives, next) {
288 751c6a17 Gerd Hoffmann
        if (!all_devices)
289 73006d2a Luiz Capitulino
            if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
290 751c6a17 Gerd Hoffmann
                continue;
291 751c6a17 Gerd Hoffmann
        bdrv_commit(dinfo->bdrv);
292 9dc39cba bellard
    }
293 9dc39cba bellard
}
294 9dc39cba bellard
295 13c7425e Luiz Capitulino
static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
296 9dc39cba bellard
{
297 c227f099 Anthony Liguori
    const mon_cmd_t *cmd;
298 d54908a5 Luiz Capitulino
    const char *item = qdict_get_try_str(qdict, "item");
299 9dc39cba bellard
300 9307c4c1 bellard
    if (!item)
301 9dc39cba bellard
        goto help;
302 13c7425e Luiz Capitulino
303 13c7425e Luiz Capitulino
    for (cmd = info_cmds; cmd->name != NULL; cmd++) {
304 5fafdf24 ths
        if (compare_cmd(item, cmd->name))
305 13c7425e Luiz Capitulino
            break;
306 9dc39cba bellard
    }
307 13c7425e Luiz Capitulino
308 13c7425e Luiz Capitulino
    if (cmd->name == NULL)
309 13c7425e Luiz Capitulino
        goto help;
310 13c7425e Luiz Capitulino
311 13c7425e Luiz Capitulino
    if (monitor_handler_ported(cmd)) {
312 13c7425e Luiz Capitulino
        cmd->mhandler.info_new(mon, ret_data);
313 13c7425e Luiz Capitulino
        if (*ret_data)
314 13c7425e Luiz Capitulino
            cmd->user_print(mon, *ret_data);
315 13c7425e Luiz Capitulino
    } else {
316 13c7425e Luiz Capitulino
        cmd->mhandler.info(mon);
317 13c7425e Luiz Capitulino
    }
318 13c7425e Luiz Capitulino
319 9dc39cba bellard
    return;
320 13c7425e Luiz Capitulino
321 13c7425e Luiz Capitulino
help:
322 13c7425e Luiz Capitulino
    help_cmd(mon, "info");
323 9dc39cba bellard
}
324 9dc39cba bellard
325 376253ec aliguori
static void do_info_version(Monitor *mon)
326 9bc9d1c7 bellard
{
327 4a19f1ec pbrook
    monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
328 9bc9d1c7 bellard
}
329 9bc9d1c7 bellard
330 376253ec aliguori
static void do_info_name(Monitor *mon)
331 c35734b2 ths
{
332 c35734b2 ths
    if (qemu_name)
333 376253ec aliguori
        monitor_printf(mon, "%s\n", qemu_name);
334 c35734b2 ths
}
335 c35734b2 ths
336 bf4f74c0 aurel32
#if defined(TARGET_I386)
337 376253ec aliguori
static void do_info_hpet(Monitor *mon)
338 16b29ae1 aliguori
{
339 376253ec aliguori
    monitor_printf(mon, "HPET is %s by QEMU\n",
340 376253ec aliguori
                   (no_hpet) ? "disabled" : "enabled");
341 16b29ae1 aliguori
}
342 bf4f74c0 aurel32
#endif
343 16b29ae1 aliguori
344 376253ec aliguori
static void do_info_uuid(Monitor *mon)
345 a36e69dd ths
{
346 376253ec aliguori
    monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
347 376253ec aliguori
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
348 376253ec aliguori
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
349 376253ec aliguori
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
350 376253ec aliguori
                   qemu_uuid[14], qemu_uuid[15]);
351 a36e69dd ths
}
352 a36e69dd ths
353 6a00d601 bellard
/* get the current CPU defined by the user */
354 9596ebb7 pbrook
static int mon_set_cpu(int cpu_index)
355 6a00d601 bellard
{
356 6a00d601 bellard
    CPUState *env;
357 6a00d601 bellard
358 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
359 6a00d601 bellard
        if (env->cpu_index == cpu_index) {
360 731b0364 aliguori
            cur_mon->mon_cpu = env;
361 6a00d601 bellard
            return 0;
362 6a00d601 bellard
        }
363 6a00d601 bellard
    }
364 6a00d601 bellard
    return -1;
365 6a00d601 bellard
}
366 6a00d601 bellard
367 9596ebb7 pbrook
static CPUState *mon_get_cpu(void)
368 6a00d601 bellard
{
369 731b0364 aliguori
    if (!cur_mon->mon_cpu) {
370 6a00d601 bellard
        mon_set_cpu(0);
371 6a00d601 bellard
    }
372 4c0960c0 Avi Kivity
    cpu_synchronize_state(cur_mon->mon_cpu);
373 731b0364 aliguori
    return cur_mon->mon_cpu;
374 6a00d601 bellard
}
375 6a00d601 bellard
376 376253ec aliguori
static void do_info_registers(Monitor *mon)
377 9307c4c1 bellard
{
378 6a00d601 bellard
    CPUState *env;
379 6a00d601 bellard
    env = mon_get_cpu();
380 6a00d601 bellard
    if (!env)
381 6a00d601 bellard
        return;
382 9307c4c1 bellard
#ifdef TARGET_I386
383 376253ec aliguori
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
384 d24b15a8 bellard
                   X86_DUMP_FPU);
385 9307c4c1 bellard
#else
386 376253ec aliguori
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
387 7fe48483 bellard
                   0);
388 9307c4c1 bellard
#endif
389 9307c4c1 bellard
}
390 9307c4c1 bellard
391 376253ec aliguori
static void do_info_cpus(Monitor *mon)
392 6a00d601 bellard
{
393 6a00d601 bellard
    CPUState *env;
394 6a00d601 bellard
395 6a00d601 bellard
    /* just to set the default cpu if not already done */
396 6a00d601 bellard
    mon_get_cpu();
397 6a00d601 bellard
398 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
399 4c0960c0 Avi Kivity
        cpu_synchronize_state(env);
400 376253ec aliguori
        monitor_printf(mon, "%c CPU #%d:",
401 731b0364 aliguori
                       (env == mon->mon_cpu) ? '*' : ' ',
402 376253ec aliguori
                       env->cpu_index);
403 6a00d601 bellard
#if defined(TARGET_I386)
404 376253ec aliguori
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
405 376253ec aliguori
                       env->eip + env->segs[R_CS].base);
406 e80e1cc4 bellard
#elif defined(TARGET_PPC)
407 376253ec aliguori
        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
408 ba3c64fb bellard
#elif defined(TARGET_SPARC)
409 376253ec aliguori
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
410 376253ec aliguori
                       env->pc, env->npc);
411 ead9360e ths
#elif defined(TARGET_MIPS)
412 376253ec aliguori
        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
413 ce5232c5 bellard
#endif
414 ead9360e ths
        if (env->halted)
415 376253ec aliguori
            monitor_printf(mon, " (halted)");
416 376253ec aliguori
        monitor_printf(mon, "\n");
417 6a00d601 bellard
    }
418 6a00d601 bellard
}
419 6a00d601 bellard
420 d54908a5 Luiz Capitulino
static void do_cpu_set(Monitor *mon, const QDict *qdict)
421 6a00d601 bellard
{
422 d54908a5 Luiz Capitulino
    int index = qdict_get_int(qdict, "index");
423 6a00d601 bellard
    if (mon_set_cpu(index) < 0)
424 376253ec aliguori
        monitor_printf(mon, "Invalid CPU index\n");
425 6a00d601 bellard
}
426 6a00d601 bellard
427 376253ec aliguori
static void do_info_jit(Monitor *mon)
428 e3db7226 bellard
{
429 376253ec aliguori
    dump_exec_info((FILE *)mon, monitor_fprintf);
430 e3db7226 bellard
}
431 e3db7226 bellard
432 376253ec aliguori
static void do_info_history(Monitor *mon)
433 aa455485 bellard
{
434 aa455485 bellard
    int i;
435 7e2515e8 bellard
    const char *str;
436 3b46e624 ths
437 cde76ee1 aliguori
    if (!mon->rs)
438 cde76ee1 aliguori
        return;
439 7e2515e8 bellard
    i = 0;
440 7e2515e8 bellard
    for(;;) {
441 731b0364 aliguori
        str = readline_get_history(mon->rs, i);
442 7e2515e8 bellard
        if (!str)
443 7e2515e8 bellard
            break;
444 376253ec aliguori
        monitor_printf(mon, "%d: '%s'\n", i, str);
445 8e3a9fd2 bellard
        i++;
446 aa455485 bellard
    }
447 aa455485 bellard
}
448 aa455485 bellard
449 76a66253 j_mayer
#if defined(TARGET_PPC)
450 76a66253 j_mayer
/* XXX: not implemented in other targets */
451 376253ec aliguori
static void do_info_cpu_stats(Monitor *mon)
452 76a66253 j_mayer
{
453 76a66253 j_mayer
    CPUState *env;
454 76a66253 j_mayer
455 76a66253 j_mayer
    env = mon_get_cpu();
456 376253ec aliguori
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
457 76a66253 j_mayer
}
458 76a66253 j_mayer
#endif
459 76a66253 j_mayer
460 b223f35f Luiz Capitulino
/**
461 b223f35f Luiz Capitulino
 * do_quit(): Quit QEMU execution
462 b223f35f Luiz Capitulino
 */
463 b223f35f Luiz Capitulino
static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
464 9dc39cba bellard
{
465 9dc39cba bellard
    exit(0);
466 9dc39cba bellard
}
467 9dc39cba bellard
468 376253ec aliguori
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
469 9dc39cba bellard
{
470 9dc39cba bellard
    if (bdrv_is_inserted(bs)) {
471 9dc39cba bellard
        if (!force) {
472 9dc39cba bellard
            if (!bdrv_is_removable(bs)) {
473 376253ec aliguori
                monitor_printf(mon, "device is not removable\n");
474 9dc39cba bellard
                return -1;
475 9dc39cba bellard
            }
476 9dc39cba bellard
            if (bdrv_is_locked(bs)) {
477 376253ec aliguori
                monitor_printf(mon, "device is locked\n");
478 9dc39cba bellard
                return -1;
479 9dc39cba bellard
            }
480 9dc39cba bellard
        }
481 9dc39cba bellard
        bdrv_close(bs);
482 9dc39cba bellard
    }
483 9dc39cba bellard
    return 0;
484 9dc39cba bellard
}
485 9dc39cba bellard
486 f18c16de Luiz Capitulino
static void do_eject(Monitor *mon, const QDict *qdict)
487 9dc39cba bellard
{
488 9dc39cba bellard
    BlockDriverState *bs;
489 f18c16de Luiz Capitulino
    int force = qdict_get_int(qdict, "force");
490 f18c16de Luiz Capitulino
    const char *filename = qdict_get_str(qdict, "filename");
491 9dc39cba bellard
492 9307c4c1 bellard
    bs = bdrv_find(filename);
493 9dc39cba bellard
    if (!bs) {
494 376253ec aliguori
        monitor_printf(mon, "device not found\n");
495 9dc39cba bellard
        return;
496 9dc39cba bellard
    }
497 376253ec aliguori
    eject_device(mon, bs, force);
498 9dc39cba bellard
}
499 9dc39cba bellard
500 376253ec aliguori
static void do_change_block(Monitor *mon, const char *device,
501 376253ec aliguori
                            const char *filename, const char *fmt)
502 9dc39cba bellard
{
503 9dc39cba bellard
    BlockDriverState *bs;
504 2ecea9b8 aurel32
    BlockDriver *drv = NULL;
505 9dc39cba bellard
506 9307c4c1 bellard
    bs = bdrv_find(device);
507 9dc39cba bellard
    if (!bs) {
508 376253ec aliguori
        monitor_printf(mon, "device not found\n");
509 9dc39cba bellard
        return;
510 9dc39cba bellard
    }
511 2ecea9b8 aurel32
    if (fmt) {
512 2ecea9b8 aurel32
        drv = bdrv_find_format(fmt);
513 2ecea9b8 aurel32
        if (!drv) {
514 376253ec aliguori
            monitor_printf(mon, "invalid format %s\n", fmt);
515 2ecea9b8 aurel32
            return;
516 2ecea9b8 aurel32
        }
517 2ecea9b8 aurel32
    }
518 376253ec aliguori
    if (eject_device(mon, bs, 0) < 0)
519 9dc39cba bellard
        return;
520 2ecea9b8 aurel32
    bdrv_open2(bs, filename, 0, drv);
521 376253ec aliguori
    monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
522 bb5fc20f aliguori
}
523 bb5fc20f aliguori
524 376253ec aliguori
static void change_vnc_password_cb(Monitor *mon, const char *password,
525 376253ec aliguori
                                   void *opaque)
526 bb5fc20f aliguori
{
527 bb5fc20f aliguori
    if (vnc_display_password(NULL, password) < 0)
528 376253ec aliguori
        monitor_printf(mon, "could not set VNC server password\n");
529 bb5fc20f aliguori
530 731b0364 aliguori
    monitor_read_command(mon, 1);
531 9dc39cba bellard
}
532 9dc39cba bellard
533 376253ec aliguori
static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
534 e25a5822 ths
{
535 70848515 ths
    if (strcmp(target, "passwd") == 0 ||
536 28a76be8 aliguori
        strcmp(target, "password") == 0) {
537 28a76be8 aliguori
        if (arg) {
538 bb5fc20f aliguori
            char password[9];
539 28a76be8 aliguori
            strncpy(password, arg, sizeof(password));
540 28a76be8 aliguori
            password[sizeof(password) - 1] = '\0';
541 376253ec aliguori
            change_vnc_password_cb(mon, password, NULL);
542 bb5fc20f aliguori
        } else {
543 376253ec aliguori
            monitor_read_password(mon, change_vnc_password_cb, NULL);
544 bb5fc20f aliguori
        }
545 70848515 ths
    } else {
546 28a76be8 aliguori
        if (vnc_display_open(NULL, target) < 0)
547 376253ec aliguori
            monitor_printf(mon, "could not start VNC server on %s\n", target);
548 70848515 ths
    }
549 e25a5822 ths
}
550 e25a5822 ths
551 1d4daa91 Luiz Capitulino
static void do_change(Monitor *mon, const QDict *qdict)
552 e25a5822 ths
{
553 1d4daa91 Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
554 1d4daa91 Luiz Capitulino
    const char *target = qdict_get_str(qdict, "target");
555 1d4daa91 Luiz Capitulino
    const char *arg = qdict_get_try_str(qdict, "arg");
556 e25a5822 ths
    if (strcmp(device, "vnc") == 0) {
557 28a76be8 aliguori
        do_change_vnc(mon, target, arg);
558 e25a5822 ths
    } else {
559 28a76be8 aliguori
        do_change_block(mon, device, target, arg);
560 e25a5822 ths
    }
561 e25a5822 ths
}
562 e25a5822 ths
563 d54908a5 Luiz Capitulino
static void do_screen_dump(Monitor *mon, const QDict *qdict)
564 59a983b9 bellard
{
565 d54908a5 Luiz Capitulino
    vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
566 59a983b9 bellard
}
567 59a983b9 bellard
568 d54908a5 Luiz Capitulino
static void do_logfile(Monitor *mon, const QDict *qdict)
569 e735b91c pbrook
{
570 d54908a5 Luiz Capitulino
    cpu_set_log_filename(qdict_get_str(qdict, "filename"));
571 e735b91c pbrook
}
572 e735b91c pbrook
573 d54908a5 Luiz Capitulino
static void do_log(Monitor *mon, const QDict *qdict)
574 f193c797 bellard
{
575 f193c797 bellard
    int mask;
576 d54908a5 Luiz Capitulino
    const char *items = qdict_get_str(qdict, "items");
577 3b46e624 ths
578 9307c4c1 bellard
    if (!strcmp(items, "none")) {
579 f193c797 bellard
        mask = 0;
580 f193c797 bellard
    } else {
581 9307c4c1 bellard
        mask = cpu_str_to_log_mask(items);
582 f193c797 bellard
        if (!mask) {
583 376253ec aliguori
            help_cmd(mon, "log");
584 f193c797 bellard
            return;
585 f193c797 bellard
        }
586 f193c797 bellard
    }
587 f193c797 bellard
    cpu_set_log(mask);
588 f193c797 bellard
}
589 f193c797 bellard
590 d54908a5 Luiz Capitulino
static void do_singlestep(Monitor *mon, const QDict *qdict)
591 1b530a6d aurel32
{
592 d54908a5 Luiz Capitulino
    const char *option = qdict_get_try_str(qdict, "option");
593 1b530a6d aurel32
    if (!option || !strcmp(option, "on")) {
594 1b530a6d aurel32
        singlestep = 1;
595 1b530a6d aurel32
    } else if (!strcmp(option, "off")) {
596 1b530a6d aurel32
        singlestep = 0;
597 1b530a6d aurel32
    } else {
598 1b530a6d aurel32
        monitor_printf(mon, "unexpected option %s\n", option);
599 1b530a6d aurel32
    }
600 1b530a6d aurel32
}
601 1b530a6d aurel32
602 e0c97bde Luiz Capitulino
/**
603 e0c97bde Luiz Capitulino
 * do_stop(): Stop VM execution
604 e0c97bde Luiz Capitulino
 */
605 e0c97bde Luiz Capitulino
static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
606 8a7ddc38 bellard
{
607 8a7ddc38 bellard
    vm_stop(EXCP_INTERRUPT);
608 8a7ddc38 bellard
}
609 8a7ddc38 bellard
610 bb5fc20f aliguori
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
611 c0f4ce77 aliguori
612 376253ec aliguori
struct bdrv_iterate_context {
613 376253ec aliguori
    Monitor *mon;
614 376253ec aliguori
    int err;
615 376253ec aliguori
};
616 376253ec aliguori
617 f96fc8a0 Luiz Capitulino
static void do_cont(Monitor *mon, const QDict *qdict)
618 8a7ddc38 bellard
{
619 376253ec aliguori
    struct bdrv_iterate_context context = { mon, 0 };
620 c0f4ce77 aliguori
621 376253ec aliguori
    bdrv_iterate(encrypted_bdrv_it, &context);
622 c0f4ce77 aliguori
    /* only resume the vm if all keys are set and valid */
623 376253ec aliguori
    if (!context.err)
624 c0f4ce77 aliguori
        vm_start();
625 8a7ddc38 bellard
}
626 8a7ddc38 bellard
627 bb5fc20f aliguori
static void bdrv_key_cb(void *opaque, int err)
628 bb5fc20f aliguori
{
629 376253ec aliguori
    Monitor *mon = opaque;
630 376253ec aliguori
631 bb5fc20f aliguori
    /* another key was set successfully, retry to continue */
632 bb5fc20f aliguori
    if (!err)
633 f96fc8a0 Luiz Capitulino
        do_cont(mon, NULL);
634 bb5fc20f aliguori
}
635 bb5fc20f aliguori
636 bb5fc20f aliguori
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
637 bb5fc20f aliguori
{
638 376253ec aliguori
    struct bdrv_iterate_context *context = opaque;
639 bb5fc20f aliguori
640 376253ec aliguori
    if (!context->err && bdrv_key_required(bs)) {
641 376253ec aliguori
        context->err = -EBUSY;
642 376253ec aliguori
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
643 376253ec aliguori
                                    context->mon);
644 bb5fc20f aliguori
    }
645 bb5fc20f aliguori
}
646 bb5fc20f aliguori
647 d54908a5 Luiz Capitulino
static void do_gdbserver(Monitor *mon, const QDict *qdict)
648 59030a8c aliguori
{
649 d54908a5 Luiz Capitulino
    const char *device = qdict_get_try_str(qdict, "device");
650 59030a8c aliguori
    if (!device)
651 59030a8c aliguori
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
652 59030a8c aliguori
    if (gdbserver_start(device) < 0) {
653 59030a8c aliguori
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
654 59030a8c aliguori
                       device);
655 59030a8c aliguori
    } else if (strcmp(device, "none") == 0) {
656 36556b20 aliguori
        monitor_printf(mon, "Disabled gdbserver\n");
657 8a7ddc38 bellard
    } else {
658 59030a8c aliguori
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
659 59030a8c aliguori
                       device);
660 8a7ddc38 bellard
    }
661 8a7ddc38 bellard
}
662 8a7ddc38 bellard
663 d54908a5 Luiz Capitulino
static void do_watchdog_action(Monitor *mon, const QDict *qdict)
664 9dd986cc Richard W.M. Jones
{
665 d54908a5 Luiz Capitulino
    const char *action = qdict_get_str(qdict, "action");
666 9dd986cc Richard W.M. Jones
    if (select_watchdog_action(action) == -1) {
667 9dd986cc Richard W.M. Jones
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
668 9dd986cc Richard W.M. Jones
    }
669 9dd986cc Richard W.M. Jones
}
670 9dd986cc Richard W.M. Jones
671 376253ec aliguori
static void monitor_printc(Monitor *mon, int c)
672 9307c4c1 bellard
{
673 376253ec aliguori
    monitor_printf(mon, "'");
674 9307c4c1 bellard
    switch(c) {
675 9307c4c1 bellard
    case '\'':
676 376253ec aliguori
        monitor_printf(mon, "\\'");
677 9307c4c1 bellard
        break;
678 9307c4c1 bellard
    case '\\':
679 376253ec aliguori
        monitor_printf(mon, "\\\\");
680 9307c4c1 bellard
        break;
681 9307c4c1 bellard
    case '\n':
682 376253ec aliguori
        monitor_printf(mon, "\\n");
683 9307c4c1 bellard
        break;
684 9307c4c1 bellard
    case '\r':
685 376253ec aliguori
        monitor_printf(mon, "\\r");
686 9307c4c1 bellard
        break;
687 9307c4c1 bellard
    default:
688 9307c4c1 bellard
        if (c >= 32 && c <= 126) {
689 376253ec aliguori
            monitor_printf(mon, "%c", c);
690 9307c4c1 bellard
        } else {
691 376253ec aliguori
            monitor_printf(mon, "\\x%02x", c);
692 9307c4c1 bellard
        }
693 9307c4c1 bellard
        break;
694 9307c4c1 bellard
    }
695 376253ec aliguori
    monitor_printf(mon, "'");
696 9307c4c1 bellard
}
697 9307c4c1 bellard
698 376253ec aliguori
static void memory_dump(Monitor *mon, int count, int format, int wsize,
699 c227f099 Anthony Liguori
                        target_phys_addr_t addr, int is_physical)
700 9307c4c1 bellard
{
701 6a00d601 bellard
    CPUState *env;
702 9307c4c1 bellard
    int nb_per_line, l, line_size, i, max_digits, len;
703 9307c4c1 bellard
    uint8_t buf[16];
704 9307c4c1 bellard
    uint64_t v;
705 9307c4c1 bellard
706 9307c4c1 bellard
    if (format == 'i') {
707 9307c4c1 bellard
        int flags;
708 9307c4c1 bellard
        flags = 0;
709 6a00d601 bellard
        env = mon_get_cpu();
710 6a00d601 bellard
        if (!env && !is_physical)
711 6a00d601 bellard
            return;
712 9307c4c1 bellard
#ifdef TARGET_I386
713 4c27ba27 bellard
        if (wsize == 2) {
714 9307c4c1 bellard
            flags = 1;
715 4c27ba27 bellard
        } else if (wsize == 4) {
716 4c27ba27 bellard
            flags = 0;
717 4c27ba27 bellard
        } else {
718 6a15fd12 bellard
            /* as default we use the current CS size */
719 4c27ba27 bellard
            flags = 0;
720 6a15fd12 bellard
            if (env) {
721 6a15fd12 bellard
#ifdef TARGET_X86_64
722 5fafdf24 ths
                if ((env->efer & MSR_EFER_LMA) &&
723 6a15fd12 bellard
                    (env->segs[R_CS].flags & DESC_L_MASK))
724 6a15fd12 bellard
                    flags = 2;
725 6a15fd12 bellard
                else
726 6a15fd12 bellard
#endif
727 6a15fd12 bellard
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
728 6a15fd12 bellard
                    flags = 1;
729 6a15fd12 bellard
            }
730 4c27ba27 bellard
        }
731 4c27ba27 bellard
#endif
732 376253ec aliguori
        monitor_disas(mon, env, addr, count, is_physical, flags);
733 9307c4c1 bellard
        return;
734 9307c4c1 bellard
    }
735 9307c4c1 bellard
736 9307c4c1 bellard
    len = wsize * count;
737 9307c4c1 bellard
    if (wsize == 1)
738 9307c4c1 bellard
        line_size = 8;
739 9307c4c1 bellard
    else
740 9307c4c1 bellard
        line_size = 16;
741 9307c4c1 bellard
    nb_per_line = line_size / wsize;
742 9307c4c1 bellard
    max_digits = 0;
743 9307c4c1 bellard
744 9307c4c1 bellard
    switch(format) {
745 9307c4c1 bellard
    case 'o':
746 9307c4c1 bellard
        max_digits = (wsize * 8 + 2) / 3;
747 9307c4c1 bellard
        break;
748 9307c4c1 bellard
    default:
749 9307c4c1 bellard
    case 'x':
750 9307c4c1 bellard
        max_digits = (wsize * 8) / 4;
751 9307c4c1 bellard
        break;
752 9307c4c1 bellard
    case 'u':
753 9307c4c1 bellard
    case 'd':
754 9307c4c1 bellard
        max_digits = (wsize * 8 * 10 + 32) / 33;
755 9307c4c1 bellard
        break;
756 9307c4c1 bellard
    case 'c':
757 9307c4c1 bellard
        wsize = 1;
758 9307c4c1 bellard
        break;
759 9307c4c1 bellard
    }
760 9307c4c1 bellard
761 9307c4c1 bellard
    while (len > 0) {
762 7743e588 blueswir1
        if (is_physical)
763 376253ec aliguori
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
764 7743e588 blueswir1
        else
765 376253ec aliguori
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
766 9307c4c1 bellard
        l = len;
767 9307c4c1 bellard
        if (l > line_size)
768 9307c4c1 bellard
            l = line_size;
769 9307c4c1 bellard
        if (is_physical) {
770 9307c4c1 bellard
            cpu_physical_memory_rw(addr, buf, l, 0);
771 9307c4c1 bellard
        } else {
772 6a00d601 bellard
            env = mon_get_cpu();
773 6a00d601 bellard
            if (!env)
774 6a00d601 bellard
                break;
775 c8f79b67 aliguori
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
776 376253ec aliguori
                monitor_printf(mon, " Cannot access memory\n");
777 c8f79b67 aliguori
                break;
778 c8f79b67 aliguori
            }
779 9307c4c1 bellard
        }
780 5fafdf24 ths
        i = 0;
781 9307c4c1 bellard
        while (i < l) {
782 9307c4c1 bellard
            switch(wsize) {
783 9307c4c1 bellard
            default:
784 9307c4c1 bellard
            case 1:
785 9307c4c1 bellard
                v = ldub_raw(buf + i);
786 9307c4c1 bellard
                break;
787 9307c4c1 bellard
            case 2:
788 9307c4c1 bellard
                v = lduw_raw(buf + i);
789 9307c4c1 bellard
                break;
790 9307c4c1 bellard
            case 4:
791 92a31b1f bellard
                v = (uint32_t)ldl_raw(buf + i);
792 9307c4c1 bellard
                break;
793 9307c4c1 bellard
            case 8:
794 9307c4c1 bellard
                v = ldq_raw(buf + i);
795 9307c4c1 bellard
                break;
796 9307c4c1 bellard
            }
797 376253ec aliguori
            monitor_printf(mon, " ");
798 9307c4c1 bellard
            switch(format) {
799 9307c4c1 bellard
            case 'o':
800 376253ec aliguori
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
801 9307c4c1 bellard
                break;
802 9307c4c1 bellard
            case 'x':
803 376253ec aliguori
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
804 9307c4c1 bellard
                break;
805 9307c4c1 bellard
            case 'u':
806 376253ec aliguori
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
807 9307c4c1 bellard
                break;
808 9307c4c1 bellard
            case 'd':
809 376253ec aliguori
                monitor_printf(mon, "%*" PRId64, max_digits, v);
810 9307c4c1 bellard
                break;
811 9307c4c1 bellard
            case 'c':
812 376253ec aliguori
                monitor_printc(mon, v);
813 9307c4c1 bellard
                break;
814 9307c4c1 bellard
            }
815 9307c4c1 bellard
            i += wsize;
816 9307c4c1 bellard
        }
817 376253ec aliguori
        monitor_printf(mon, "\n");
818 9307c4c1 bellard
        addr += l;
819 9307c4c1 bellard
        len -= l;
820 9307c4c1 bellard
    }
821 9307c4c1 bellard
}
822 9307c4c1 bellard
823 1bd1442e Luiz Capitulino
static void do_memory_dump(Monitor *mon, const QDict *qdict)
824 9307c4c1 bellard
{
825 1bd1442e Luiz Capitulino
    int count = qdict_get_int(qdict, "count");
826 1bd1442e Luiz Capitulino
    int format = qdict_get_int(qdict, "format");
827 1bd1442e Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
828 1bd1442e Luiz Capitulino
    target_long addr = qdict_get_int(qdict, "addr");
829 1bd1442e Luiz Capitulino
830 376253ec aliguori
    memory_dump(mon, count, format, size, addr, 0);
831 9307c4c1 bellard
}
832 9307c4c1 bellard
833 1bd1442e Luiz Capitulino
static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
834 9307c4c1 bellard
{
835 1bd1442e Luiz Capitulino
    int count = qdict_get_int(qdict, "count");
836 1bd1442e Luiz Capitulino
    int format = qdict_get_int(qdict, "format");
837 1bd1442e Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
838 c227f099 Anthony Liguori
    target_phys_addr_t addr = qdict_get_int(qdict, "addr");
839 1bd1442e Luiz Capitulino
840 376253ec aliguori
    memory_dump(mon, count, format, size, addr, 1);
841 9307c4c1 bellard
}
842 9307c4c1 bellard
843 1bd1442e Luiz Capitulino
static void do_print(Monitor *mon, const QDict *qdict)
844 9307c4c1 bellard
{
845 1bd1442e Luiz Capitulino
    int format = qdict_get_int(qdict, "format");
846 c227f099 Anthony Liguori
    target_phys_addr_t val = qdict_get_int(qdict, "val");
847 1bd1442e Luiz Capitulino
848 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS == 32
849 9307c4c1 bellard
    switch(format) {
850 9307c4c1 bellard
    case 'o':
851 376253ec aliguori
        monitor_printf(mon, "%#o", val);
852 9307c4c1 bellard
        break;
853 9307c4c1 bellard
    case 'x':
854 376253ec aliguori
        monitor_printf(mon, "%#x", val);
855 9307c4c1 bellard
        break;
856 9307c4c1 bellard
    case 'u':
857 376253ec aliguori
        monitor_printf(mon, "%u", val);
858 9307c4c1 bellard
        break;
859 9307c4c1 bellard
    default:
860 9307c4c1 bellard
    case 'd':
861 376253ec aliguori
        monitor_printf(mon, "%d", val);
862 9307c4c1 bellard
        break;
863 9307c4c1 bellard
    case 'c':
864 376253ec aliguori
        monitor_printc(mon, val);
865 9307c4c1 bellard
        break;
866 9307c4c1 bellard
    }
867 92a31b1f bellard
#else
868 92a31b1f bellard
    switch(format) {
869 92a31b1f bellard
    case 'o':
870 376253ec aliguori
        monitor_printf(mon, "%#" PRIo64, val);
871 92a31b1f bellard
        break;
872 92a31b1f bellard
    case 'x':
873 376253ec aliguori
        monitor_printf(mon, "%#" PRIx64, val);
874 92a31b1f bellard
        break;
875 92a31b1f bellard
    case 'u':
876 376253ec aliguori
        monitor_printf(mon, "%" PRIu64, val);
877 92a31b1f bellard
        break;
878 92a31b1f bellard
    default:
879 92a31b1f bellard
    case 'd':
880 376253ec aliguori
        monitor_printf(mon, "%" PRId64, val);
881 92a31b1f bellard
        break;
882 92a31b1f bellard
    case 'c':
883 376253ec aliguori
        monitor_printc(mon, val);
884 92a31b1f bellard
        break;
885 92a31b1f bellard
    }
886 92a31b1f bellard
#endif
887 376253ec aliguori
    monitor_printf(mon, "\n");
888 9307c4c1 bellard
}
889 9307c4c1 bellard
890 afe67ef2 Luiz Capitulino
static void do_memory_save(Monitor *mon, const QDict *qdict)
891 b371dc59 bellard
{
892 b371dc59 bellard
    FILE *f;
893 afe67ef2 Luiz Capitulino
    uint32_t size = qdict_get_int(qdict, "size");
894 afe67ef2 Luiz Capitulino
    const char *filename = qdict_get_str(qdict, "filename");
895 afe67ef2 Luiz Capitulino
    target_long addr = qdict_get_int(qdict, "val");
896 b371dc59 bellard
    uint32_t l;
897 b371dc59 bellard
    CPUState *env;
898 b371dc59 bellard
    uint8_t buf[1024];
899 b371dc59 bellard
900 b371dc59 bellard
    env = mon_get_cpu();
901 b371dc59 bellard
    if (!env)
902 b371dc59 bellard
        return;
903 b371dc59 bellard
904 b371dc59 bellard
    f = fopen(filename, "wb");
905 b371dc59 bellard
    if (!f) {
906 376253ec aliguori
        monitor_printf(mon, "could not open '%s'\n", filename);
907 b371dc59 bellard
        return;
908 b371dc59 bellard
    }
909 b371dc59 bellard
    while (size != 0) {
910 b371dc59 bellard
        l = sizeof(buf);
911 b371dc59 bellard
        if (l > size)
912 b371dc59 bellard
            l = size;
913 b371dc59 bellard
        cpu_memory_rw_debug(env, addr, buf, l, 0);
914 b371dc59 bellard
        fwrite(buf, 1, l, f);
915 b371dc59 bellard
        addr += l;
916 b371dc59 bellard
        size -= l;
917 b371dc59 bellard
    }
918 b371dc59 bellard
    fclose(f);
919 b371dc59 bellard
}
920 b371dc59 bellard
921 afe67ef2 Luiz Capitulino
static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
922 a8bdf7a6 aurel32
{
923 a8bdf7a6 aurel32
    FILE *f;
924 a8bdf7a6 aurel32
    uint32_t l;
925 a8bdf7a6 aurel32
    uint8_t buf[1024];
926 afe67ef2 Luiz Capitulino
    uint32_t size = qdict_get_int(qdict, "size");
927 afe67ef2 Luiz Capitulino
    const char *filename = qdict_get_str(qdict, "filename");
928 c227f099 Anthony Liguori
    target_phys_addr_t addr = qdict_get_int(qdict, "val");
929 a8bdf7a6 aurel32
930 a8bdf7a6 aurel32
    f = fopen(filename, "wb");
931 a8bdf7a6 aurel32
    if (!f) {
932 376253ec aliguori
        monitor_printf(mon, "could not open '%s'\n", filename);
933 a8bdf7a6 aurel32
        return;
934 a8bdf7a6 aurel32
    }
935 a8bdf7a6 aurel32
    while (size != 0) {
936 a8bdf7a6 aurel32
        l = sizeof(buf);
937 a8bdf7a6 aurel32
        if (l > size)
938 a8bdf7a6 aurel32
            l = size;
939 a8bdf7a6 aurel32
        cpu_physical_memory_rw(addr, buf, l, 0);
940 a8bdf7a6 aurel32
        fwrite(buf, 1, l, f);
941 a8bdf7a6 aurel32
        fflush(f);
942 a8bdf7a6 aurel32
        addr += l;
943 a8bdf7a6 aurel32
        size -= l;
944 a8bdf7a6 aurel32
    }
945 a8bdf7a6 aurel32
    fclose(f);
946 a8bdf7a6 aurel32
}
947 a8bdf7a6 aurel32
948 f18c16de Luiz Capitulino
static void do_sum(Monitor *mon, const QDict *qdict)
949 e4cf1adc bellard
{
950 e4cf1adc bellard
    uint32_t addr;
951 e4cf1adc bellard
    uint8_t buf[1];
952 e4cf1adc bellard
    uint16_t sum;
953 f18c16de Luiz Capitulino
    uint32_t start = qdict_get_int(qdict, "start");
954 f18c16de Luiz Capitulino
    uint32_t size = qdict_get_int(qdict, "size");
955 e4cf1adc bellard
956 e4cf1adc bellard
    sum = 0;
957 e4cf1adc bellard
    for(addr = start; addr < (start + size); addr++) {
958 e4cf1adc bellard
        cpu_physical_memory_rw(addr, buf, 1, 0);
959 e4cf1adc bellard
        /* BSD sum algorithm ('sum' Unix command) */
960 e4cf1adc bellard
        sum = (sum >> 1) | (sum << 15);
961 e4cf1adc bellard
        sum += buf[0];
962 e4cf1adc bellard
    }
963 376253ec aliguori
    monitor_printf(mon, "%05d\n", sum);
964 e4cf1adc bellard
}
965 e4cf1adc bellard
966 a3a91a35 bellard
typedef struct {
967 a3a91a35 bellard
    int keycode;
968 a3a91a35 bellard
    const char *name;
969 a3a91a35 bellard
} KeyDef;
970 a3a91a35 bellard
971 a3a91a35 bellard
static const KeyDef key_defs[] = {
972 a3a91a35 bellard
    { 0x2a, "shift" },
973 a3a91a35 bellard
    { 0x36, "shift_r" },
974 3b46e624 ths
975 a3a91a35 bellard
    { 0x38, "alt" },
976 a3a91a35 bellard
    { 0xb8, "alt_r" },
977 2ba27c7f ths
    { 0x64, "altgr" },
978 2ba27c7f ths
    { 0xe4, "altgr_r" },
979 a3a91a35 bellard
    { 0x1d, "ctrl" },
980 a3a91a35 bellard
    { 0x9d, "ctrl_r" },
981 a3a91a35 bellard
982 a3a91a35 bellard
    { 0xdd, "menu" },
983 a3a91a35 bellard
984 a3a91a35 bellard
    { 0x01, "esc" },
985 a3a91a35 bellard
986 a3a91a35 bellard
    { 0x02, "1" },
987 a3a91a35 bellard
    { 0x03, "2" },
988 a3a91a35 bellard
    { 0x04, "3" },
989 a3a91a35 bellard
    { 0x05, "4" },
990 a3a91a35 bellard
    { 0x06, "5" },
991 a3a91a35 bellard
    { 0x07, "6" },
992 a3a91a35 bellard
    { 0x08, "7" },
993 a3a91a35 bellard
    { 0x09, "8" },
994 a3a91a35 bellard
    { 0x0a, "9" },
995 a3a91a35 bellard
    { 0x0b, "0" },
996 64866c3d bellard
    { 0x0c, "minus" },
997 64866c3d bellard
    { 0x0d, "equal" },
998 a3a91a35 bellard
    { 0x0e, "backspace" },
999 a3a91a35 bellard
1000 a3a91a35 bellard
    { 0x0f, "tab" },
1001 a3a91a35 bellard
    { 0x10, "q" },
1002 a3a91a35 bellard
    { 0x11, "w" },
1003 a3a91a35 bellard
    { 0x12, "e" },
1004 a3a91a35 bellard
    { 0x13, "r" },
1005 a3a91a35 bellard
    { 0x14, "t" },
1006 a3a91a35 bellard
    { 0x15, "y" },
1007 a3a91a35 bellard
    { 0x16, "u" },
1008 a3a91a35 bellard
    { 0x17, "i" },
1009 a3a91a35 bellard
    { 0x18, "o" },
1010 a3a91a35 bellard
    { 0x19, "p" },
1011 a3a91a35 bellard
1012 a3a91a35 bellard
    { 0x1c, "ret" },
1013 a3a91a35 bellard
1014 a3a91a35 bellard
    { 0x1e, "a" },
1015 a3a91a35 bellard
    { 0x1f, "s" },
1016 a3a91a35 bellard
    { 0x20, "d" },
1017 a3a91a35 bellard
    { 0x21, "f" },
1018 a3a91a35 bellard
    { 0x22, "g" },
1019 a3a91a35 bellard
    { 0x23, "h" },
1020 a3a91a35 bellard
    { 0x24, "j" },
1021 a3a91a35 bellard
    { 0x25, "k" },
1022 a3a91a35 bellard
    { 0x26, "l" },
1023 a3a91a35 bellard
1024 a3a91a35 bellard
    { 0x2c, "z" },
1025 a3a91a35 bellard
    { 0x2d, "x" },
1026 a3a91a35 bellard
    { 0x2e, "c" },
1027 a3a91a35 bellard
    { 0x2f, "v" },
1028 a3a91a35 bellard
    { 0x30, "b" },
1029 a3a91a35 bellard
    { 0x31, "n" },
1030 a3a91a35 bellard
    { 0x32, "m" },
1031 9155fc45 aurel32
    { 0x33, "comma" },
1032 9155fc45 aurel32
    { 0x34, "dot" },
1033 9155fc45 aurel32
    { 0x35, "slash" },
1034 3b46e624 ths
1035 4d3b6f6e balrog
    { 0x37, "asterisk" },
1036 4d3b6f6e balrog
1037 a3a91a35 bellard
    { 0x39, "spc" },
1038 00ffa62a bellard
    { 0x3a, "caps_lock" },
1039 a3a91a35 bellard
    { 0x3b, "f1" },
1040 a3a91a35 bellard
    { 0x3c, "f2" },
1041 a3a91a35 bellard
    { 0x3d, "f3" },
1042 a3a91a35 bellard
    { 0x3e, "f4" },
1043 a3a91a35 bellard
    { 0x3f, "f5" },
1044 a3a91a35 bellard
    { 0x40, "f6" },
1045 a3a91a35 bellard
    { 0x41, "f7" },
1046 a3a91a35 bellard
    { 0x42, "f8" },
1047 a3a91a35 bellard
    { 0x43, "f9" },
1048 a3a91a35 bellard
    { 0x44, "f10" },
1049 00ffa62a bellard
    { 0x45, "num_lock" },
1050 a3a91a35 bellard
    { 0x46, "scroll_lock" },
1051 a3a91a35 bellard
1052 64866c3d bellard
    { 0xb5, "kp_divide" },
1053 64866c3d bellard
    { 0x37, "kp_multiply" },
1054 0cfec834 ths
    { 0x4a, "kp_subtract" },
1055 64866c3d bellard
    { 0x4e, "kp_add" },
1056 64866c3d bellard
    { 0x9c, "kp_enter" },
1057 64866c3d bellard
    { 0x53, "kp_decimal" },
1058 f2289cb6 balrog
    { 0x54, "sysrq" },
1059 64866c3d bellard
1060 64866c3d bellard
    { 0x52, "kp_0" },
1061 64866c3d bellard
    { 0x4f, "kp_1" },
1062 64866c3d bellard
    { 0x50, "kp_2" },
1063 64866c3d bellard
    { 0x51, "kp_3" },
1064 64866c3d bellard
    { 0x4b, "kp_4" },
1065 64866c3d bellard
    { 0x4c, "kp_5" },
1066 64866c3d bellard
    { 0x4d, "kp_6" },
1067 64866c3d bellard
    { 0x47, "kp_7" },
1068 64866c3d bellard
    { 0x48, "kp_8" },
1069 64866c3d bellard
    { 0x49, "kp_9" },
1070 3b46e624 ths
1071 a3a91a35 bellard
    { 0x56, "<" },
1072 a3a91a35 bellard
1073 a3a91a35 bellard
    { 0x57, "f11" },
1074 a3a91a35 bellard
    { 0x58, "f12" },
1075 a3a91a35 bellard
1076 a3a91a35 bellard
    { 0xb7, "print" },
1077 a3a91a35 bellard
1078 a3a91a35 bellard
    { 0xc7, "home" },
1079 a3a91a35 bellard
    { 0xc9, "pgup" },
1080 a3a91a35 bellard
    { 0xd1, "pgdn" },
1081 a3a91a35 bellard
    { 0xcf, "end" },
1082 a3a91a35 bellard
1083 a3a91a35 bellard
    { 0xcb, "left" },
1084 a3a91a35 bellard
    { 0xc8, "up" },
1085 a3a91a35 bellard
    { 0xd0, "down" },
1086 a3a91a35 bellard
    { 0xcd, "right" },
1087 a3a91a35 bellard
1088 a3a91a35 bellard
    { 0xd2, "insert" },
1089 a3a91a35 bellard
    { 0xd3, "delete" },
1090 c0b5b109 blueswir1
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1091 c0b5b109 blueswir1
    { 0xf0, "stop" },
1092 c0b5b109 blueswir1
    { 0xf1, "again" },
1093 c0b5b109 blueswir1
    { 0xf2, "props" },
1094 c0b5b109 blueswir1
    { 0xf3, "undo" },
1095 c0b5b109 blueswir1
    { 0xf4, "front" },
1096 c0b5b109 blueswir1
    { 0xf5, "copy" },
1097 c0b5b109 blueswir1
    { 0xf6, "open" },
1098 c0b5b109 blueswir1
    { 0xf7, "paste" },
1099 c0b5b109 blueswir1
    { 0xf8, "find" },
1100 c0b5b109 blueswir1
    { 0xf9, "cut" },
1101 c0b5b109 blueswir1
    { 0xfa, "lf" },
1102 c0b5b109 blueswir1
    { 0xfb, "help" },
1103 c0b5b109 blueswir1
    { 0xfc, "meta_l" },
1104 c0b5b109 blueswir1
    { 0xfd, "meta_r" },
1105 c0b5b109 blueswir1
    { 0xfe, "compose" },
1106 c0b5b109 blueswir1
#endif
1107 a3a91a35 bellard
    { 0, NULL },
1108 a3a91a35 bellard
};
1109 a3a91a35 bellard
1110 a3a91a35 bellard
static int get_keycode(const char *key)
1111 a3a91a35 bellard
{
1112 a3a91a35 bellard
    const KeyDef *p;
1113 64866c3d bellard
    char *endp;
1114 64866c3d bellard
    int ret;
1115 a3a91a35 bellard
1116 a3a91a35 bellard
    for(p = key_defs; p->name != NULL; p++) {
1117 a3a91a35 bellard
        if (!strcmp(key, p->name))
1118 a3a91a35 bellard
            return p->keycode;
1119 a3a91a35 bellard
    }
1120 64866c3d bellard
    if (strstart(key, "0x", NULL)) {
1121 64866c3d bellard
        ret = strtoul(key, &endp, 0);
1122 64866c3d bellard
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1123 64866c3d bellard
            return ret;
1124 64866c3d bellard
    }
1125 a3a91a35 bellard
    return -1;
1126 a3a91a35 bellard
}
1127 a3a91a35 bellard
1128 c8256f9d balrog
#define MAX_KEYCODES 16
1129 c8256f9d balrog
static uint8_t keycodes[MAX_KEYCODES];
1130 c8256f9d balrog
static int nb_pending_keycodes;
1131 c8256f9d balrog
static QEMUTimer *key_timer;
1132 c8256f9d balrog
1133 c8256f9d balrog
static void release_keys(void *opaque)
1134 c8256f9d balrog
{
1135 c8256f9d balrog
    int keycode;
1136 c8256f9d balrog
1137 c8256f9d balrog
    while (nb_pending_keycodes > 0) {
1138 c8256f9d balrog
        nb_pending_keycodes--;
1139 c8256f9d balrog
        keycode = keycodes[nb_pending_keycodes];
1140 c8256f9d balrog
        if (keycode & 0x80)
1141 c8256f9d balrog
            kbd_put_keycode(0xe0);
1142 c8256f9d balrog
        kbd_put_keycode(keycode | 0x80);
1143 c8256f9d balrog
    }
1144 c8256f9d balrog
}
1145 c8256f9d balrog
1146 1d4daa91 Luiz Capitulino
static void do_sendkey(Monitor *mon, const QDict *qdict)
1147 a3a91a35 bellard
{
1148 3401c0d9 balrog
    char keyname_buf[16];
1149 3401c0d9 balrog
    char *separator;
1150 3401c0d9 balrog
    int keyname_len, keycode, i;
1151 1d4daa91 Luiz Capitulino
    const char *string = qdict_get_str(qdict, "string");
1152 1d4daa91 Luiz Capitulino
    int has_hold_time = qdict_haskey(qdict, "hold_time");
1153 1d4daa91 Luiz Capitulino
    int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1154 3401c0d9 balrog
1155 c8256f9d balrog
    if (nb_pending_keycodes > 0) {
1156 c8256f9d balrog
        qemu_del_timer(key_timer);
1157 c8256f9d balrog
        release_keys(NULL);
1158 c8256f9d balrog
    }
1159 c8256f9d balrog
    if (!has_hold_time)
1160 c8256f9d balrog
        hold_time = 100;
1161 c8256f9d balrog
    i = 0;
1162 3401c0d9 balrog
    while (1) {
1163 3401c0d9 balrog
        separator = strchr(string, '-');
1164 3401c0d9 balrog
        keyname_len = separator ? separator - string : strlen(string);
1165 3401c0d9 balrog
        if (keyname_len > 0) {
1166 3401c0d9 balrog
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1167 3401c0d9 balrog
            if (keyname_len > sizeof(keyname_buf) - 1) {
1168 376253ec aliguori
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1169 3401c0d9 balrog
                return;
1170 a3a91a35 bellard
            }
1171 c8256f9d balrog
            if (i == MAX_KEYCODES) {
1172 376253ec aliguori
                monitor_printf(mon, "too many keys\n");
1173 3401c0d9 balrog
                return;
1174 3401c0d9 balrog
            }
1175 3401c0d9 balrog
            keyname_buf[keyname_len] = 0;
1176 3401c0d9 balrog
            keycode = get_keycode(keyname_buf);
1177 3401c0d9 balrog
            if (keycode < 0) {
1178 376253ec aliguori
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1179 3401c0d9 balrog
                return;
1180 3401c0d9 balrog
            }
1181 c8256f9d balrog
            keycodes[i++] = keycode;
1182 a3a91a35 bellard
        }
1183 3401c0d9 balrog
        if (!separator)
1184 a3a91a35 bellard
            break;
1185 3401c0d9 balrog
        string = separator + 1;
1186 a3a91a35 bellard
    }
1187 c8256f9d balrog
    nb_pending_keycodes = i;
1188 a3a91a35 bellard
    /* key down events */
1189 c8256f9d balrog
    for (i = 0; i < nb_pending_keycodes; i++) {
1190 a3a91a35 bellard
        keycode = keycodes[i];
1191 a3a91a35 bellard
        if (keycode & 0x80)
1192 a3a91a35 bellard
            kbd_put_keycode(0xe0);
1193 a3a91a35 bellard
        kbd_put_keycode(keycode & 0x7f);
1194 a3a91a35 bellard
    }
1195 c8256f9d balrog
    /* delayed key up events */
1196 f227f17d balrog
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1197 6ee093c9 Juan Quintela
                   muldiv64(get_ticks_per_sec(), hold_time, 1000));
1198 a3a91a35 bellard
}
1199 a3a91a35 bellard
1200 13224a87 bellard
static int mouse_button_state;
1201 13224a87 bellard
1202 1d4daa91 Luiz Capitulino
static void do_mouse_move(Monitor *mon, const QDict *qdict)
1203 13224a87 bellard
{
1204 13224a87 bellard
    int dx, dy, dz;
1205 1d4daa91 Luiz Capitulino
    const char *dx_str = qdict_get_str(qdict, "dx_str");
1206 1d4daa91 Luiz Capitulino
    const char *dy_str = qdict_get_str(qdict, "dy_str");
1207 1d4daa91 Luiz Capitulino
    const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1208 13224a87 bellard
    dx = strtol(dx_str, NULL, 0);
1209 13224a87 bellard
    dy = strtol(dy_str, NULL, 0);
1210 13224a87 bellard
    dz = 0;
1211 5fafdf24 ths
    if (dz_str)
1212 13224a87 bellard
        dz = strtol(dz_str, NULL, 0);
1213 13224a87 bellard
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1214 13224a87 bellard
}
1215 13224a87 bellard
1216 d54908a5 Luiz Capitulino
static void do_mouse_button(Monitor *mon, const QDict *qdict)
1217 13224a87 bellard
{
1218 d54908a5 Luiz Capitulino
    int button_state = qdict_get_int(qdict, "button_state");
1219 13224a87 bellard
    mouse_button_state = button_state;
1220 13224a87 bellard
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1221 13224a87 bellard
}
1222 13224a87 bellard
1223 aa93e39c Luiz Capitulino
static void do_ioport_read(Monitor *mon, const QDict *qdict)
1224 3440557b bellard
{
1225 aa93e39c Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
1226 aa93e39c Luiz Capitulino
    int addr = qdict_get_int(qdict, "addr");
1227 aa93e39c Luiz Capitulino
    int has_index = qdict_haskey(qdict, "index");
1228 3440557b bellard
    uint32_t val;
1229 3440557b bellard
    int suffix;
1230 3440557b bellard
1231 3440557b bellard
    if (has_index) {
1232 aa93e39c Luiz Capitulino
        int index = qdict_get_int(qdict, "index");
1233 afcea8cb Blue Swirl
        cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1234 3440557b bellard
        addr++;
1235 3440557b bellard
    }
1236 3440557b bellard
    addr &= 0xffff;
1237 3440557b bellard
1238 3440557b bellard
    switch(size) {
1239 3440557b bellard
    default:
1240 3440557b bellard
    case 1:
1241 afcea8cb Blue Swirl
        val = cpu_inb(addr);
1242 3440557b bellard
        suffix = 'b';
1243 3440557b bellard
        break;
1244 3440557b bellard
    case 2:
1245 afcea8cb Blue Swirl
        val = cpu_inw(addr);
1246 3440557b bellard
        suffix = 'w';
1247 3440557b bellard
        break;
1248 3440557b bellard
    case 4:
1249 afcea8cb Blue Swirl
        val = cpu_inl(addr);
1250 3440557b bellard
        suffix = 'l';
1251 3440557b bellard
        break;
1252 3440557b bellard
    }
1253 376253ec aliguori
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1254 376253ec aliguori
                   suffix, addr, size * 2, val);
1255 3440557b bellard
}
1256 a3a91a35 bellard
1257 1bd1442e Luiz Capitulino
static void do_ioport_write(Monitor *mon, const QDict *qdict)
1258 f114784f Jan Kiszka
{
1259 1bd1442e Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
1260 1bd1442e Luiz Capitulino
    int addr = qdict_get_int(qdict, "addr");
1261 1bd1442e Luiz Capitulino
    int val = qdict_get_int(qdict, "val");
1262 1bd1442e Luiz Capitulino
1263 f114784f Jan Kiszka
    addr &= IOPORTS_MASK;
1264 f114784f Jan Kiszka
1265 f114784f Jan Kiszka
    switch (size) {
1266 f114784f Jan Kiszka
    default:
1267 f114784f Jan Kiszka
    case 1:
1268 afcea8cb Blue Swirl
        cpu_outb(addr, val);
1269 f114784f Jan Kiszka
        break;
1270 f114784f Jan Kiszka
    case 2:
1271 afcea8cb Blue Swirl
        cpu_outw(addr, val);
1272 f114784f Jan Kiszka
        break;
1273 f114784f Jan Kiszka
    case 4:
1274 afcea8cb Blue Swirl
        cpu_outl(addr, val);
1275 f114784f Jan Kiszka
        break;
1276 f114784f Jan Kiszka
    }
1277 f114784f Jan Kiszka
}
1278 f114784f Jan Kiszka
1279 d54908a5 Luiz Capitulino
static void do_boot_set(Monitor *mon, const QDict *qdict)
1280 0ecdffbb aurel32
{
1281 0ecdffbb aurel32
    int res;
1282 d54908a5 Luiz Capitulino
    const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1283 0ecdffbb aurel32
1284 76e30d0f Jan Kiszka
    res = qemu_boot_set(bootdevice);
1285 76e30d0f Jan Kiszka
    if (res == 0) {
1286 76e30d0f Jan Kiszka
        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1287 76e30d0f Jan Kiszka
    } else if (res > 0) {
1288 76e30d0f Jan Kiszka
        monitor_printf(mon, "setting boot device list failed\n");
1289 0ecdffbb aurel32
    } else {
1290 376253ec aliguori
        monitor_printf(mon, "no function defined to set boot device list for "
1291 376253ec aliguori
                       "this architecture\n");
1292 0ecdffbb aurel32
    }
1293 0ecdffbb aurel32
}
1294 0ecdffbb aurel32
1295 c80d259e Luiz Capitulino
/**
1296 c80d259e Luiz Capitulino
 * do_system_reset(): Issue a machine reset
1297 c80d259e Luiz Capitulino
 */
1298 c80d259e Luiz Capitulino
static void do_system_reset(Monitor *mon, const QDict *qdict,
1299 c80d259e Luiz Capitulino
                            QObject **ret_data)
1300 e4f9082b bellard
{
1301 e4f9082b bellard
    qemu_system_reset_request();
1302 e4f9082b bellard
}
1303 e4f9082b bellard
1304 f96fc8a0 Luiz Capitulino
static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1305 3475187d bellard
{
1306 3475187d bellard
    qemu_system_powerdown_request();
1307 3475187d bellard
}
1308 3475187d bellard
1309 b86bda5b bellard
#if defined(TARGET_I386)
1310 376253ec aliguori
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1311 b86bda5b bellard
{
1312 376253ec aliguori
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1313 376253ec aliguori
                   addr,
1314 376253ec aliguori
                   pte & mask,
1315 376253ec aliguori
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
1316 376253ec aliguori
                   pte & PG_PSE_MASK ? 'P' : '-',
1317 376253ec aliguori
                   pte & PG_DIRTY_MASK ? 'D' : '-',
1318 376253ec aliguori
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
1319 376253ec aliguori
                   pte & PG_PCD_MASK ? 'C' : '-',
1320 376253ec aliguori
                   pte & PG_PWT_MASK ? 'T' : '-',
1321 376253ec aliguori
                   pte & PG_USER_MASK ? 'U' : '-',
1322 376253ec aliguori
                   pte & PG_RW_MASK ? 'W' : '-');
1323 b86bda5b bellard
}
1324 b86bda5b bellard
1325 376253ec aliguori
static void tlb_info(Monitor *mon)
1326 b86bda5b bellard
{
1327 6a00d601 bellard
    CPUState *env;
1328 b86bda5b bellard
    int l1, l2;
1329 b86bda5b bellard
    uint32_t pgd, pde, pte;
1330 b86bda5b bellard
1331 6a00d601 bellard
    env = mon_get_cpu();
1332 6a00d601 bellard
    if (!env)
1333 6a00d601 bellard
        return;
1334 6a00d601 bellard
1335 b86bda5b bellard
    if (!(env->cr[0] & CR0_PG_MASK)) {
1336 376253ec aliguori
        monitor_printf(mon, "PG disabled\n");
1337 b86bda5b bellard
        return;
1338 b86bda5b bellard
    }
1339 b86bda5b bellard
    pgd = env->cr[3] & ~0xfff;
1340 b86bda5b bellard
    for(l1 = 0; l1 < 1024; l1++) {
1341 b86bda5b bellard
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1342 b86bda5b bellard
        pde = le32_to_cpu(pde);
1343 b86bda5b bellard
        if (pde & PG_PRESENT_MASK) {
1344 b86bda5b bellard
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1345 376253ec aliguori
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1346 b86bda5b bellard
            } else {
1347 b86bda5b bellard
                for(l2 = 0; l2 < 1024; l2++) {
1348 5fafdf24 ths
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1349 b86bda5b bellard
                                             (uint8_t *)&pte, 4);
1350 b86bda5b bellard
                    pte = le32_to_cpu(pte);
1351 b86bda5b bellard
                    if (pte & PG_PRESENT_MASK) {
1352 376253ec aliguori
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1353 5fafdf24 ths
                                  pte & ~PG_PSE_MASK,
1354 b86bda5b bellard
                                  ~0xfff);
1355 b86bda5b bellard
                    }
1356 b86bda5b bellard
                }
1357 b86bda5b bellard
            }
1358 b86bda5b bellard
        }
1359 b86bda5b bellard
    }
1360 b86bda5b bellard
}
1361 b86bda5b bellard
1362 376253ec aliguori
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1363 b86bda5b bellard
                      uint32_t end, int prot)
1364 b86bda5b bellard
{
1365 9746b15b bellard
    int prot1;
1366 9746b15b bellard
    prot1 = *plast_prot;
1367 9746b15b bellard
    if (prot != prot1) {
1368 b86bda5b bellard
        if (*pstart != -1) {
1369 376253ec aliguori
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1370 376253ec aliguori
                           *pstart, end, end - *pstart,
1371 376253ec aliguori
                           prot1 & PG_USER_MASK ? 'u' : '-',
1372 376253ec aliguori
                           'r',
1373 376253ec aliguori
                           prot1 & PG_RW_MASK ? 'w' : '-');
1374 b86bda5b bellard
        }
1375 b86bda5b bellard
        if (prot != 0)
1376 b86bda5b bellard
            *pstart = end;
1377 b86bda5b bellard
        else
1378 b86bda5b bellard
            *pstart = -1;
1379 b86bda5b bellard
        *plast_prot = prot;
1380 b86bda5b bellard
    }
1381 b86bda5b bellard
}
1382 b86bda5b bellard
1383 376253ec aliguori
static void mem_info(Monitor *mon)
1384 b86bda5b bellard
{
1385 6a00d601 bellard
    CPUState *env;
1386 b86bda5b bellard
    int l1, l2, prot, last_prot;
1387 b86bda5b bellard
    uint32_t pgd, pde, pte, start, end;
1388 b86bda5b bellard
1389 6a00d601 bellard
    env = mon_get_cpu();
1390 6a00d601 bellard
    if (!env)
1391 6a00d601 bellard
        return;
1392 6a00d601 bellard
1393 b86bda5b bellard
    if (!(env->cr[0] & CR0_PG_MASK)) {
1394 376253ec aliguori
        monitor_printf(mon, "PG disabled\n");
1395 b86bda5b bellard
        return;
1396 b86bda5b bellard
    }
1397 b86bda5b bellard
    pgd = env->cr[3] & ~0xfff;
1398 b86bda5b bellard
    last_prot = 0;
1399 b86bda5b bellard
    start = -1;
1400 b86bda5b bellard
    for(l1 = 0; l1 < 1024; l1++) {
1401 b86bda5b bellard
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1402 b86bda5b bellard
        pde = le32_to_cpu(pde);
1403 b86bda5b bellard
        end = l1 << 22;
1404 b86bda5b bellard
        if (pde & PG_PRESENT_MASK) {
1405 b86bda5b bellard
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1406 b86bda5b bellard
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1407 376253ec aliguori
                mem_print(mon, &start, &last_prot, end, prot);
1408 b86bda5b bellard
            } else {
1409 b86bda5b bellard
                for(l2 = 0; l2 < 1024; l2++) {
1410 5fafdf24 ths
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1411 b86bda5b bellard
                                             (uint8_t *)&pte, 4);
1412 b86bda5b bellard
                    pte = le32_to_cpu(pte);
1413 b86bda5b bellard
                    end = (l1 << 22) + (l2 << 12);
1414 b86bda5b bellard
                    if (pte & PG_PRESENT_MASK) {
1415 b86bda5b bellard
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1416 b86bda5b bellard
                    } else {
1417 b86bda5b bellard
                        prot = 0;
1418 b86bda5b bellard
                    }
1419 376253ec aliguori
                    mem_print(mon, &start, &last_prot, end, prot);
1420 b86bda5b bellard
                }
1421 b86bda5b bellard
            }
1422 b86bda5b bellard
        } else {
1423 b86bda5b bellard
            prot = 0;
1424 376253ec aliguori
            mem_print(mon, &start, &last_prot, end, prot);
1425 b86bda5b bellard
        }
1426 b86bda5b bellard
    }
1427 b86bda5b bellard
}
1428 b86bda5b bellard
#endif
1429 b86bda5b bellard
1430 7c664e2f aurel32
#if defined(TARGET_SH4)
1431 7c664e2f aurel32
1432 376253ec aliguori
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1433 7c664e2f aurel32
{
1434 376253ec aliguori
    monitor_printf(mon, " tlb%i:\t"
1435 376253ec aliguori
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1436 376253ec aliguori
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1437 376253ec aliguori
                   "dirty=%hhu writethrough=%hhu\n",
1438 376253ec aliguori
                   idx,
1439 376253ec aliguori
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1440 376253ec aliguori
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
1441 376253ec aliguori
                   tlb->d, tlb->wt);
1442 7c664e2f aurel32
}
1443 7c664e2f aurel32
1444 376253ec aliguori
static void tlb_info(Monitor *mon)
1445 7c664e2f aurel32
{
1446 7c664e2f aurel32
    CPUState *env = mon_get_cpu();
1447 7c664e2f aurel32
    int i;
1448 7c664e2f aurel32
1449 376253ec aliguori
    monitor_printf (mon, "ITLB:\n");
1450 7c664e2f aurel32
    for (i = 0 ; i < ITLB_SIZE ; i++)
1451 376253ec aliguori
        print_tlb (mon, i, &env->itlb[i]);
1452 376253ec aliguori
    monitor_printf (mon, "UTLB:\n");
1453 7c664e2f aurel32
    for (i = 0 ; i < UTLB_SIZE ; i++)
1454 376253ec aliguori
        print_tlb (mon, i, &env->utlb[i]);
1455 7c664e2f aurel32
}
1456 7c664e2f aurel32
1457 7c664e2f aurel32
#endif
1458 7c664e2f aurel32
1459 376253ec aliguori
static void do_info_kvm(Monitor *mon)
1460 7ba1e619 aliguori
{
1461 7ba1e619 aliguori
#ifdef CONFIG_KVM
1462 376253ec aliguori
    monitor_printf(mon, "kvm support: ");
1463 7ba1e619 aliguori
    if (kvm_enabled())
1464 376253ec aliguori
        monitor_printf(mon, "enabled\n");
1465 7ba1e619 aliguori
    else
1466 376253ec aliguori
        monitor_printf(mon, "disabled\n");
1467 7ba1e619 aliguori
#else
1468 376253ec aliguori
    monitor_printf(mon, "kvm support: not compiled\n");
1469 7ba1e619 aliguori
#endif
1470 7ba1e619 aliguori
}
1471 7ba1e619 aliguori
1472 030ea37b aliguori
static void do_info_numa(Monitor *mon)
1473 030ea37b aliguori
{
1474 b28b6230 aliguori
    int i;
1475 030ea37b aliguori
    CPUState *env;
1476 030ea37b aliguori
1477 030ea37b aliguori
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1478 030ea37b aliguori
    for (i = 0; i < nb_numa_nodes; i++) {
1479 030ea37b aliguori
        monitor_printf(mon, "node %d cpus:", i);
1480 030ea37b aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1481 030ea37b aliguori
            if (env->numa_node == i) {
1482 030ea37b aliguori
                monitor_printf(mon, " %d", env->cpu_index);
1483 030ea37b aliguori
            }
1484 030ea37b aliguori
        }
1485 030ea37b aliguori
        monitor_printf(mon, "\n");
1486 030ea37b aliguori
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1487 030ea37b aliguori
            node_mem[i] >> 20);
1488 030ea37b aliguori
    }
1489 030ea37b aliguori
}
1490 030ea37b aliguori
1491 5f1ce948 bellard
#ifdef CONFIG_PROFILER
1492 5f1ce948 bellard
1493 e9a6625e Aurelien Jarno
int64_t qemu_time;
1494 e9a6625e Aurelien Jarno
int64_t dev_time;
1495 e9a6625e Aurelien Jarno
1496 376253ec aliguori
static void do_info_profile(Monitor *mon)
1497 5f1ce948 bellard
{
1498 5f1ce948 bellard
    int64_t total;
1499 5f1ce948 bellard
    total = qemu_time;
1500 5f1ce948 bellard
    if (total == 0)
1501 5f1ce948 bellard
        total = 1;
1502 376253ec aliguori
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1503 6ee093c9 Juan Quintela
                   dev_time, dev_time / (double)get_ticks_per_sec());
1504 376253ec aliguori
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1505 6ee093c9 Juan Quintela
                   qemu_time, qemu_time / (double)get_ticks_per_sec());
1506 5f1ce948 bellard
    qemu_time = 0;
1507 5f1ce948 bellard
    dev_time = 0;
1508 5f1ce948 bellard
}
1509 5f1ce948 bellard
#else
1510 376253ec aliguori
static void do_info_profile(Monitor *mon)
1511 5f1ce948 bellard
{
1512 376253ec aliguori
    monitor_printf(mon, "Internal profiler not compiled\n");
1513 5f1ce948 bellard
}
1514 5f1ce948 bellard
#endif
1515 5f1ce948 bellard
1516 ec36b695 bellard
/* Capture support */
1517 72cf2d4f Blue Swirl
static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1518 ec36b695 bellard
1519 376253ec aliguori
static void do_info_capture(Monitor *mon)
1520 ec36b695 bellard
{
1521 ec36b695 bellard
    int i;
1522 ec36b695 bellard
    CaptureState *s;
1523 ec36b695 bellard
1524 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1525 376253ec aliguori
        monitor_printf(mon, "[%d]: ", i);
1526 ec36b695 bellard
        s->ops.info (s->opaque);
1527 ec36b695 bellard
    }
1528 ec36b695 bellard
}
1529 ec36b695 bellard
1530 2313086a Blue Swirl
#ifdef HAS_AUDIO
1531 d54908a5 Luiz Capitulino
static void do_stop_capture(Monitor *mon, const QDict *qdict)
1532 ec36b695 bellard
{
1533 ec36b695 bellard
    int i;
1534 d54908a5 Luiz Capitulino
    int n = qdict_get_int(qdict, "n");
1535 ec36b695 bellard
    CaptureState *s;
1536 ec36b695 bellard
1537 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1538 ec36b695 bellard
        if (i == n) {
1539 ec36b695 bellard
            s->ops.destroy (s->opaque);
1540 72cf2d4f Blue Swirl
            QLIST_REMOVE (s, entries);
1541 ec36b695 bellard
            qemu_free (s);
1542 ec36b695 bellard
            return;
1543 ec36b695 bellard
        }
1544 ec36b695 bellard
    }
1545 ec36b695 bellard
}
1546 ec36b695 bellard
1547 c1925484 Luiz Capitulino
static void do_wav_capture(Monitor *mon, const QDict *qdict)
1548 c1925484 Luiz Capitulino
{
1549 c1925484 Luiz Capitulino
    const char *path = qdict_get_str(qdict, "path");
1550 c1925484 Luiz Capitulino
    int has_freq = qdict_haskey(qdict, "freq");
1551 c1925484 Luiz Capitulino
    int freq = qdict_get_try_int(qdict, "freq", -1);
1552 c1925484 Luiz Capitulino
    int has_bits = qdict_haskey(qdict, "bits");
1553 c1925484 Luiz Capitulino
    int bits = qdict_get_try_int(qdict, "bits", -1);
1554 c1925484 Luiz Capitulino
    int has_channels = qdict_haskey(qdict, "nchannels");
1555 c1925484 Luiz Capitulino
    int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1556 ec36b695 bellard
    CaptureState *s;
1557 ec36b695 bellard
1558 ec36b695 bellard
    s = qemu_mallocz (sizeof (*s));
1559 ec36b695 bellard
1560 ec36b695 bellard
    freq = has_freq ? freq : 44100;
1561 ec36b695 bellard
    bits = has_bits ? bits : 16;
1562 ec36b695 bellard
    nchannels = has_channels ? nchannels : 2;
1563 ec36b695 bellard
1564 ec36b695 bellard
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1565 376253ec aliguori
        monitor_printf(mon, "Faied to add wave capture\n");
1566 ec36b695 bellard
        qemu_free (s);
1567 ec36b695 bellard
    }
1568 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD (&capture_head, s, entries);
1569 ec36b695 bellard
}
1570 ec36b695 bellard
#endif
1571 ec36b695 bellard
1572 dc1c0b74 aurel32
#if defined(TARGET_I386)
1573 d54908a5 Luiz Capitulino
static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1574 dc1c0b74 aurel32
{
1575 dc1c0b74 aurel32
    CPUState *env;
1576 d54908a5 Luiz Capitulino
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1577 dc1c0b74 aurel32
1578 dc1c0b74 aurel32
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1579 dc1c0b74 aurel32
        if (env->cpu_index == cpu_index) {
1580 dc1c0b74 aurel32
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1581 dc1c0b74 aurel32
            break;
1582 dc1c0b74 aurel32
        }
1583 dc1c0b74 aurel32
}
1584 dc1c0b74 aurel32
#endif
1585 dc1c0b74 aurel32
1586 376253ec aliguori
static void do_info_status(Monitor *mon)
1587 6f9c5ee7 aurel32
{
1588 1b530a6d aurel32
    if (vm_running) {
1589 1b530a6d aurel32
        if (singlestep) {
1590 1b530a6d aurel32
            monitor_printf(mon, "VM status: running (single step mode)\n");
1591 1b530a6d aurel32
        } else {
1592 1b530a6d aurel32
            monitor_printf(mon, "VM status: running\n");
1593 1b530a6d aurel32
        }
1594 1b530a6d aurel32
    } else
1595 376253ec aliguori
       monitor_printf(mon, "VM status: paused\n");
1596 6f9c5ee7 aurel32
}
1597 6f9c5ee7 aurel32
1598 6f9c5ee7 aurel32
1599 d54908a5 Luiz Capitulino
static void do_balloon(Monitor *mon, const QDict *qdict)
1600 df751fa8 aliguori
{
1601 d54908a5 Luiz Capitulino
    int value = qdict_get_int(qdict, "value");
1602 c227f099 Anthony Liguori
    ram_addr_t target = value;
1603 df751fa8 aliguori
    qemu_balloon(target << 20);
1604 df751fa8 aliguori
}
1605 df751fa8 aliguori
1606 376253ec aliguori
static void do_info_balloon(Monitor *mon)
1607 df751fa8 aliguori
{
1608 c227f099 Anthony Liguori
    ram_addr_t actual;
1609 df751fa8 aliguori
1610 df751fa8 aliguori
    actual = qemu_balloon_status();
1611 bd322087 aliguori
    if (kvm_enabled() && !kvm_has_sync_mmu())
1612 376253ec aliguori
        monitor_printf(mon, "Using KVM without synchronous MMU, "
1613 376253ec aliguori
                       "ballooning disabled\n");
1614 bd322087 aliguori
    else if (actual == 0)
1615 376253ec aliguori
        monitor_printf(mon, "Ballooning not activated in VM\n");
1616 df751fa8 aliguori
    else
1617 376253ec aliguori
        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1618 df751fa8 aliguori
}
1619 df751fa8 aliguori
1620 15dfcd45 Jan Kiszka
static qemu_acl *find_acl(Monitor *mon, const char *name)
1621 76655d6d aliguori
{
1622 15dfcd45 Jan Kiszka
    qemu_acl *acl = qemu_acl_find(name);
1623 76655d6d aliguori
1624 76655d6d aliguori
    if (!acl) {
1625 15dfcd45 Jan Kiszka
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
1626 76655d6d aliguori
    }
1627 15dfcd45 Jan Kiszka
    return acl;
1628 15dfcd45 Jan Kiszka
}
1629 15dfcd45 Jan Kiszka
1630 d54908a5 Luiz Capitulino
static void do_acl_show(Monitor *mon, const QDict *qdict)
1631 15dfcd45 Jan Kiszka
{
1632 d54908a5 Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
1633 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1634 15dfcd45 Jan Kiszka
    qemu_acl_entry *entry;
1635 15dfcd45 Jan Kiszka
    int i = 0;
1636 76655d6d aliguori
1637 15dfcd45 Jan Kiszka
    if (acl) {
1638 28a76be8 aliguori
        monitor_printf(mon, "policy: %s\n",
1639 76655d6d aliguori
                       acl->defaultDeny ? "deny" : "allow");
1640 72cf2d4f Blue Swirl
        QTAILQ_FOREACH(entry, &acl->entries, next) {
1641 28a76be8 aliguori
            i++;
1642 28a76be8 aliguori
            monitor_printf(mon, "%d: %s %s\n", i,
1643 15dfcd45 Jan Kiszka
                           entry->deny ? "deny" : "allow", entry->match);
1644 28a76be8 aliguori
        }
1645 15dfcd45 Jan Kiszka
    }
1646 15dfcd45 Jan Kiszka
}
1647 15dfcd45 Jan Kiszka
1648 d54908a5 Luiz Capitulino
static void do_acl_reset(Monitor *mon, const QDict *qdict)
1649 15dfcd45 Jan Kiszka
{
1650 d54908a5 Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
1651 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1652 15dfcd45 Jan Kiszka
1653 15dfcd45 Jan Kiszka
    if (acl) {
1654 28a76be8 aliguori
        qemu_acl_reset(acl);
1655 28a76be8 aliguori
        monitor_printf(mon, "acl: removed all rules\n");
1656 15dfcd45 Jan Kiszka
    }
1657 15dfcd45 Jan Kiszka
}
1658 15dfcd45 Jan Kiszka
1659 f18c16de Luiz Capitulino
static void do_acl_policy(Monitor *mon, const QDict *qdict)
1660 15dfcd45 Jan Kiszka
{
1661 f18c16de Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
1662 f18c16de Luiz Capitulino
    const char *policy = qdict_get_str(qdict, "policy");
1663 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1664 28a76be8 aliguori
1665 15dfcd45 Jan Kiszka
    if (acl) {
1666 15dfcd45 Jan Kiszka
        if (strcmp(policy, "allow") == 0) {
1667 28a76be8 aliguori
            acl->defaultDeny = 0;
1668 28a76be8 aliguori
            monitor_printf(mon, "acl: policy set to 'allow'\n");
1669 15dfcd45 Jan Kiszka
        } else if (strcmp(policy, "deny") == 0) {
1670 28a76be8 aliguori
            acl->defaultDeny = 1;
1671 28a76be8 aliguori
            monitor_printf(mon, "acl: policy set to 'deny'\n");
1672 28a76be8 aliguori
        } else {
1673 15dfcd45 Jan Kiszka
            monitor_printf(mon, "acl: unknown policy '%s', "
1674 15dfcd45 Jan Kiszka
                           "expected 'deny' or 'allow'\n", policy);
1675 28a76be8 aliguori
        }
1676 15dfcd45 Jan Kiszka
    }
1677 15dfcd45 Jan Kiszka
}
1678 28a76be8 aliguori
1679 1bd1442e Luiz Capitulino
static void do_acl_add(Monitor *mon, const QDict *qdict)
1680 15dfcd45 Jan Kiszka
{
1681 1bd1442e Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
1682 1bd1442e Luiz Capitulino
    const char *match = qdict_get_str(qdict, "match");
1683 1bd1442e Luiz Capitulino
    const char *policy = qdict_get_str(qdict, "policy");
1684 1bd1442e Luiz Capitulino
    int has_index = qdict_haskey(qdict, "index");
1685 1bd1442e Luiz Capitulino
    int index = qdict_get_try_int(qdict, "index", -1);
1686 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1687 15dfcd45 Jan Kiszka
    int deny, ret;
1688 15dfcd45 Jan Kiszka
1689 15dfcd45 Jan Kiszka
    if (acl) {
1690 15dfcd45 Jan Kiszka
        if (strcmp(policy, "allow") == 0) {
1691 15dfcd45 Jan Kiszka
            deny = 0;
1692 15dfcd45 Jan Kiszka
        } else if (strcmp(policy, "deny") == 0) {
1693 15dfcd45 Jan Kiszka
            deny = 1;
1694 15dfcd45 Jan Kiszka
        } else {
1695 15dfcd45 Jan Kiszka
            monitor_printf(mon, "acl: unknown policy '%s', "
1696 15dfcd45 Jan Kiszka
                           "expected 'deny' or 'allow'\n", policy);
1697 28a76be8 aliguori
            return;
1698 28a76be8 aliguori
        }
1699 28a76be8 aliguori
        if (has_index)
1700 28a76be8 aliguori
            ret = qemu_acl_insert(acl, deny, match, index);
1701 28a76be8 aliguori
        else
1702 28a76be8 aliguori
            ret = qemu_acl_append(acl, deny, match);
1703 28a76be8 aliguori
        if (ret < 0)
1704 28a76be8 aliguori
            monitor_printf(mon, "acl: unable to add acl entry\n");
1705 28a76be8 aliguori
        else
1706 28a76be8 aliguori
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
1707 15dfcd45 Jan Kiszka
    }
1708 15dfcd45 Jan Kiszka
}
1709 28a76be8 aliguori
1710 f18c16de Luiz Capitulino
static void do_acl_remove(Monitor *mon, const QDict *qdict)
1711 15dfcd45 Jan Kiszka
{
1712 f18c16de Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
1713 f18c16de Luiz Capitulino
    const char *match = qdict_get_str(qdict, "match");
1714 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1715 15dfcd45 Jan Kiszka
    int ret;
1716 28a76be8 aliguori
1717 15dfcd45 Jan Kiszka
    if (acl) {
1718 28a76be8 aliguori
        ret = qemu_acl_remove(acl, match);
1719 28a76be8 aliguori
        if (ret < 0)
1720 28a76be8 aliguori
            monitor_printf(mon, "acl: no matching acl entry\n");
1721 28a76be8 aliguori
        else
1722 28a76be8 aliguori
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1723 76655d6d aliguori
    }
1724 76655d6d aliguori
}
1725 76655d6d aliguori
1726 79c4f6b0 Huang Ying
#if defined(TARGET_I386)
1727 37b7ad48 Luiz Capitulino
static void do_inject_mce(Monitor *mon, const QDict *qdict)
1728 79c4f6b0 Huang Ying
{
1729 79c4f6b0 Huang Ying
    CPUState *cenv;
1730 37b7ad48 Luiz Capitulino
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1731 37b7ad48 Luiz Capitulino
    int bank = qdict_get_int(qdict, "bank");
1732 37b7ad48 Luiz Capitulino
    uint64_t status = qdict_get_int(qdict, "status");
1733 37b7ad48 Luiz Capitulino
    uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1734 37b7ad48 Luiz Capitulino
    uint64_t addr = qdict_get_int(qdict, "addr");
1735 37b7ad48 Luiz Capitulino
    uint64_t misc = qdict_get_int(qdict, "misc");
1736 79c4f6b0 Huang Ying
1737 79c4f6b0 Huang Ying
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1738 79c4f6b0 Huang Ying
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1739 79c4f6b0 Huang Ying
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1740 79c4f6b0 Huang Ying
            break;
1741 79c4f6b0 Huang Ying
        }
1742 79c4f6b0 Huang Ying
}
1743 79c4f6b0 Huang Ying
#endif
1744 79c4f6b0 Huang Ying
1745 d54908a5 Luiz Capitulino
static void do_getfd(Monitor *mon, const QDict *qdict)
1746 f07918fd Mark McLoughlin
{
1747 d54908a5 Luiz Capitulino
    const char *fdname = qdict_get_str(qdict, "fdname");
1748 c227f099 Anthony Liguori
    mon_fd_t *monfd;
1749 f07918fd Mark McLoughlin
    int fd;
1750 f07918fd Mark McLoughlin
1751 f07918fd Mark McLoughlin
    fd = qemu_chr_get_msgfd(mon->chr);
1752 f07918fd Mark McLoughlin
    if (fd == -1) {
1753 f07918fd Mark McLoughlin
        monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1754 f07918fd Mark McLoughlin
        return;
1755 f07918fd Mark McLoughlin
    }
1756 f07918fd Mark McLoughlin
1757 f07918fd Mark McLoughlin
    if (qemu_isdigit(fdname[0])) {
1758 f07918fd Mark McLoughlin
        monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1759 f07918fd Mark McLoughlin
        return;
1760 f07918fd Mark McLoughlin
    }
1761 f07918fd Mark McLoughlin
1762 f07918fd Mark McLoughlin
    fd = dup(fd);
1763 f07918fd Mark McLoughlin
    if (fd == -1) {
1764 f07918fd Mark McLoughlin
        monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1765 f07918fd Mark McLoughlin
                       strerror(errno));
1766 f07918fd Mark McLoughlin
        return;
1767 f07918fd Mark McLoughlin
    }
1768 f07918fd Mark McLoughlin
1769 72cf2d4f Blue Swirl
    QLIST_FOREACH(monfd, &mon->fds, next) {
1770 f07918fd Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
1771 f07918fd Mark McLoughlin
            continue;
1772 f07918fd Mark McLoughlin
        }
1773 f07918fd Mark McLoughlin
1774 f07918fd Mark McLoughlin
        close(monfd->fd);
1775 f07918fd Mark McLoughlin
        monfd->fd = fd;
1776 f07918fd Mark McLoughlin
        return;
1777 f07918fd Mark McLoughlin
    }
1778 f07918fd Mark McLoughlin
1779 c227f099 Anthony Liguori
    monfd = qemu_mallocz(sizeof(mon_fd_t));
1780 f07918fd Mark McLoughlin
    monfd->name = qemu_strdup(fdname);
1781 f07918fd Mark McLoughlin
    monfd->fd = fd;
1782 f07918fd Mark McLoughlin
1783 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1784 f07918fd Mark McLoughlin
}
1785 f07918fd Mark McLoughlin
1786 d54908a5 Luiz Capitulino
static void do_closefd(Monitor *mon, const QDict *qdict)
1787 f07918fd Mark McLoughlin
{
1788 d54908a5 Luiz Capitulino
    const char *fdname = qdict_get_str(qdict, "fdname");
1789 c227f099 Anthony Liguori
    mon_fd_t *monfd;
1790 f07918fd Mark McLoughlin
1791 72cf2d4f Blue Swirl
    QLIST_FOREACH(monfd, &mon->fds, next) {
1792 f07918fd Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
1793 f07918fd Mark McLoughlin
            continue;
1794 f07918fd Mark McLoughlin
        }
1795 f07918fd Mark McLoughlin
1796 72cf2d4f Blue Swirl
        QLIST_REMOVE(monfd, next);
1797 f07918fd Mark McLoughlin
        close(monfd->fd);
1798 f07918fd Mark McLoughlin
        qemu_free(monfd->name);
1799 f07918fd Mark McLoughlin
        qemu_free(monfd);
1800 f07918fd Mark McLoughlin
        return;
1801 f07918fd Mark McLoughlin
    }
1802 f07918fd Mark McLoughlin
1803 f07918fd Mark McLoughlin
    monitor_printf(mon, "Failed to find file descriptor named %s\n",
1804 f07918fd Mark McLoughlin
                   fdname);
1805 f07918fd Mark McLoughlin
}
1806 f07918fd Mark McLoughlin
1807 d54908a5 Luiz Capitulino
static void do_loadvm(Monitor *mon, const QDict *qdict)
1808 c8d41b2c Juan Quintela
{
1809 c8d41b2c Juan Quintela
    int saved_vm_running  = vm_running;
1810 d54908a5 Luiz Capitulino
    const char *name = qdict_get_str(qdict, "name");
1811 c8d41b2c Juan Quintela
1812 c8d41b2c Juan Quintela
    vm_stop(0);
1813 c8d41b2c Juan Quintela
1814 05f2401e Juan Quintela
    if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1815 c8d41b2c Juan Quintela
        vm_start();
1816 c8d41b2c Juan Quintela
}
1817 c8d41b2c Juan Quintela
1818 7768e04c Mark McLoughlin
int monitor_get_fd(Monitor *mon, const char *fdname)
1819 7768e04c Mark McLoughlin
{
1820 c227f099 Anthony Liguori
    mon_fd_t *monfd;
1821 7768e04c Mark McLoughlin
1822 72cf2d4f Blue Swirl
    QLIST_FOREACH(monfd, &mon->fds, next) {
1823 7768e04c Mark McLoughlin
        int fd;
1824 7768e04c Mark McLoughlin
1825 7768e04c Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
1826 7768e04c Mark McLoughlin
            continue;
1827 7768e04c Mark McLoughlin
        }
1828 7768e04c Mark McLoughlin
1829 7768e04c Mark McLoughlin
        fd = monfd->fd;
1830 7768e04c Mark McLoughlin
1831 7768e04c Mark McLoughlin
        /* caller takes ownership of fd */
1832 72cf2d4f Blue Swirl
        QLIST_REMOVE(monfd, next);
1833 7768e04c Mark McLoughlin
        qemu_free(monfd->name);
1834 7768e04c Mark McLoughlin
        qemu_free(monfd);
1835 7768e04c Mark McLoughlin
1836 7768e04c Mark McLoughlin
        return fd;
1837 7768e04c Mark McLoughlin
    }
1838 7768e04c Mark McLoughlin
1839 7768e04c Mark McLoughlin
    return -1;
1840 7768e04c Mark McLoughlin
}
1841 7768e04c Mark McLoughlin
1842 c227f099 Anthony Liguori
static const mon_cmd_t mon_cmds[] = {
1843 2313086a Blue Swirl
#include "qemu-monitor.h"
1844 5fafdf24 ths
    { NULL, NULL, },
1845 9dc39cba bellard
};
1846 9dc39cba bellard
1847 2313086a Blue Swirl
/* Please update qemu-monitor.hx when adding or changing commands */
1848 c227f099 Anthony Liguori
static const mon_cmd_t info_cmds[] = {
1849 d7f9b689 Luiz Capitulino
    {
1850 d7f9b689 Luiz Capitulino
        .name       = "version",
1851 d7f9b689 Luiz Capitulino
        .args_type  = "",
1852 d7f9b689 Luiz Capitulino
        .params     = "",
1853 d7f9b689 Luiz Capitulino
        .help       = "show the version of QEMU",
1854 910df89d Luiz Capitulino
        .mhandler.info = do_info_version,
1855 d7f9b689 Luiz Capitulino
    },
1856 d7f9b689 Luiz Capitulino
    {
1857 d7f9b689 Luiz Capitulino
        .name       = "network",
1858 d7f9b689 Luiz Capitulino
        .args_type  = "",
1859 d7f9b689 Luiz Capitulino
        .params     = "",
1860 d7f9b689 Luiz Capitulino
        .help       = "show the network state",
1861 910df89d Luiz Capitulino
        .mhandler.info = do_info_network,
1862 d7f9b689 Luiz Capitulino
    },
1863 d7f9b689 Luiz Capitulino
    {
1864 d7f9b689 Luiz Capitulino
        .name       = "chardev",
1865 d7f9b689 Luiz Capitulino
        .args_type  = "",
1866 d7f9b689 Luiz Capitulino
        .params     = "",
1867 d7f9b689 Luiz Capitulino
        .help       = "show the character devices",
1868 910df89d Luiz Capitulino
        .mhandler.info = qemu_chr_info,
1869 d7f9b689 Luiz Capitulino
    },
1870 d7f9b689 Luiz Capitulino
    {
1871 d7f9b689 Luiz Capitulino
        .name       = "block",
1872 d7f9b689 Luiz Capitulino
        .args_type  = "",
1873 d7f9b689 Luiz Capitulino
        .params     = "",
1874 d7f9b689 Luiz Capitulino
        .help       = "show the block devices",
1875 910df89d Luiz Capitulino
        .mhandler.info = bdrv_info,
1876 d7f9b689 Luiz Capitulino
    },
1877 d7f9b689 Luiz Capitulino
    {
1878 d7f9b689 Luiz Capitulino
        .name       = "blockstats",
1879 d7f9b689 Luiz Capitulino
        .args_type  = "",
1880 d7f9b689 Luiz Capitulino
        .params     = "",
1881 d7f9b689 Luiz Capitulino
        .help       = "show block device statistics",
1882 910df89d Luiz Capitulino
        .mhandler.info = bdrv_info_stats,
1883 d7f9b689 Luiz Capitulino
    },
1884 d7f9b689 Luiz Capitulino
    {
1885 d7f9b689 Luiz Capitulino
        .name       = "registers",
1886 d7f9b689 Luiz Capitulino
        .args_type  = "",
1887 d7f9b689 Luiz Capitulino
        .params     = "",
1888 d7f9b689 Luiz Capitulino
        .help       = "show the cpu registers",
1889 910df89d Luiz Capitulino
        .mhandler.info = do_info_registers,
1890 d7f9b689 Luiz Capitulino
    },
1891 d7f9b689 Luiz Capitulino
    {
1892 d7f9b689 Luiz Capitulino
        .name       = "cpus",
1893 d7f9b689 Luiz Capitulino
        .args_type  = "",
1894 d7f9b689 Luiz Capitulino
        .params     = "",
1895 d7f9b689 Luiz Capitulino
        .help       = "show infos for each CPU",
1896 910df89d Luiz Capitulino
        .mhandler.info = do_info_cpus,
1897 d7f9b689 Luiz Capitulino
    },
1898 d7f9b689 Luiz Capitulino
    {
1899 d7f9b689 Luiz Capitulino
        .name       = "history",
1900 d7f9b689 Luiz Capitulino
        .args_type  = "",
1901 d7f9b689 Luiz Capitulino
        .params     = "",
1902 d7f9b689 Luiz Capitulino
        .help       = "show the command line history",
1903 910df89d Luiz Capitulino
        .mhandler.info = do_info_history,
1904 d7f9b689 Luiz Capitulino
    },
1905 d7f9b689 Luiz Capitulino
    {
1906 d7f9b689 Luiz Capitulino
        .name       = "irq",
1907 d7f9b689 Luiz Capitulino
        .args_type  = "",
1908 d7f9b689 Luiz Capitulino
        .params     = "",
1909 d7f9b689 Luiz Capitulino
        .help       = "show the interrupts statistics (if available)",
1910 910df89d Luiz Capitulino
        .mhandler.info = irq_info,
1911 d7f9b689 Luiz Capitulino
    },
1912 d7f9b689 Luiz Capitulino
    {
1913 d7f9b689 Luiz Capitulino
        .name       = "pic",
1914 d7f9b689 Luiz Capitulino
        .args_type  = "",
1915 d7f9b689 Luiz Capitulino
        .params     = "",
1916 d7f9b689 Luiz Capitulino
        .help       = "show i8259 (PIC) state",
1917 910df89d Luiz Capitulino
        .mhandler.info = pic_info,
1918 d7f9b689 Luiz Capitulino
    },
1919 d7f9b689 Luiz Capitulino
    {
1920 d7f9b689 Luiz Capitulino
        .name       = "pci",
1921 d7f9b689 Luiz Capitulino
        .args_type  = "",
1922 d7f9b689 Luiz Capitulino
        .params     = "",
1923 d7f9b689 Luiz Capitulino
        .help       = "show PCI info",
1924 910df89d Luiz Capitulino
        .mhandler.info = pci_info,
1925 d7f9b689 Luiz Capitulino
    },
1926 7c664e2f aurel32
#if defined(TARGET_I386) || defined(TARGET_SH4)
1927 d7f9b689 Luiz Capitulino
    {
1928 d7f9b689 Luiz Capitulino
        .name       = "tlb",
1929 d7f9b689 Luiz Capitulino
        .args_type  = "",
1930 d7f9b689 Luiz Capitulino
        .params     = "",
1931 d7f9b689 Luiz Capitulino
        .help       = "show virtual to physical memory mappings",
1932 910df89d Luiz Capitulino
        .mhandler.info = tlb_info,
1933 d7f9b689 Luiz Capitulino
    },
1934 7c664e2f aurel32
#endif
1935 7c664e2f aurel32
#if defined(TARGET_I386)
1936 d7f9b689 Luiz Capitulino
    {
1937 d7f9b689 Luiz Capitulino
        .name       = "mem",
1938 d7f9b689 Luiz Capitulino
        .args_type  = "",
1939 d7f9b689 Luiz Capitulino
        .params     = "",
1940 d7f9b689 Luiz Capitulino
        .help       = "show the active virtual memory mappings",
1941 910df89d Luiz Capitulino
        .mhandler.info = mem_info,
1942 d7f9b689 Luiz Capitulino
    },
1943 d7f9b689 Luiz Capitulino
    {
1944 d7f9b689 Luiz Capitulino
        .name       = "hpet",
1945 d7f9b689 Luiz Capitulino
        .args_type  = "",
1946 d7f9b689 Luiz Capitulino
        .params     = "",
1947 d7f9b689 Luiz Capitulino
        .help       = "show state of HPET",
1948 910df89d Luiz Capitulino
        .mhandler.info = do_info_hpet,
1949 d7f9b689 Luiz Capitulino
    },
1950 b86bda5b bellard
#endif
1951 d7f9b689 Luiz Capitulino
    {
1952 d7f9b689 Luiz Capitulino
        .name       = "jit",
1953 d7f9b689 Luiz Capitulino
        .args_type  = "",
1954 d7f9b689 Luiz Capitulino
        .params     = "",
1955 d7f9b689 Luiz Capitulino
        .help       = "show dynamic compiler info",
1956 910df89d Luiz Capitulino
        .mhandler.info = do_info_jit,
1957 d7f9b689 Luiz Capitulino
    },
1958 d7f9b689 Luiz Capitulino
    {
1959 d7f9b689 Luiz Capitulino
        .name       = "kvm",
1960 d7f9b689 Luiz Capitulino
        .args_type  = "",
1961 d7f9b689 Luiz Capitulino
        .params     = "",
1962 d7f9b689 Luiz Capitulino
        .help       = "show KVM information",
1963 910df89d Luiz Capitulino
        .mhandler.info = do_info_kvm,
1964 d7f9b689 Luiz Capitulino
    },
1965 d7f9b689 Luiz Capitulino
    {
1966 d7f9b689 Luiz Capitulino
        .name       = "numa",
1967 d7f9b689 Luiz Capitulino
        .args_type  = "",
1968 d7f9b689 Luiz Capitulino
        .params     = "",
1969 d7f9b689 Luiz Capitulino
        .help       = "show NUMA information",
1970 910df89d Luiz Capitulino
        .mhandler.info = do_info_numa,
1971 d7f9b689 Luiz Capitulino
    },
1972 d7f9b689 Luiz Capitulino
    {
1973 d7f9b689 Luiz Capitulino
        .name       = "usb",
1974 d7f9b689 Luiz Capitulino
        .args_type  = "",
1975 d7f9b689 Luiz Capitulino
        .params     = "",
1976 d7f9b689 Luiz Capitulino
        .help       = "show guest USB devices",
1977 910df89d Luiz Capitulino
        .mhandler.info = usb_info,
1978 d7f9b689 Luiz Capitulino
    },
1979 d7f9b689 Luiz Capitulino
    {
1980 d7f9b689 Luiz Capitulino
        .name       = "usbhost",
1981 d7f9b689 Luiz Capitulino
        .args_type  = "",
1982 d7f9b689 Luiz Capitulino
        .params     = "",
1983 d7f9b689 Luiz Capitulino
        .help       = "show host USB devices",
1984 910df89d Luiz Capitulino
        .mhandler.info = usb_host_info,
1985 d7f9b689 Luiz Capitulino
    },
1986 d7f9b689 Luiz Capitulino
    {
1987 d7f9b689 Luiz Capitulino
        .name       = "profile",
1988 d7f9b689 Luiz Capitulino
        .args_type  = "",
1989 d7f9b689 Luiz Capitulino
        .params     = "",
1990 d7f9b689 Luiz Capitulino
        .help       = "show profiling information",
1991 910df89d Luiz Capitulino
        .mhandler.info = do_info_profile,
1992 d7f9b689 Luiz Capitulino
    },
1993 d7f9b689 Luiz Capitulino
    {
1994 d7f9b689 Luiz Capitulino
        .name       = "capture",
1995 d7f9b689 Luiz Capitulino
        .args_type  = "",
1996 d7f9b689 Luiz Capitulino
        .params     = "",
1997 d7f9b689 Luiz Capitulino
        .help       = "show capture information",
1998 910df89d Luiz Capitulino
        .mhandler.info = do_info_capture,
1999 d7f9b689 Luiz Capitulino
    },
2000 d7f9b689 Luiz Capitulino
    {
2001 d7f9b689 Luiz Capitulino
        .name       = "snapshots",
2002 d7f9b689 Luiz Capitulino
        .args_type  = "",
2003 d7f9b689 Luiz Capitulino
        .params     = "",
2004 d7f9b689 Luiz Capitulino
        .help       = "show the currently saved VM snapshots",
2005 910df89d Luiz Capitulino
        .mhandler.info = do_info_snapshots,
2006 d7f9b689 Luiz Capitulino
    },
2007 d7f9b689 Luiz Capitulino
    {
2008 d7f9b689 Luiz Capitulino
        .name       = "status",
2009 d7f9b689 Luiz Capitulino
        .args_type  = "",
2010 d7f9b689 Luiz Capitulino
        .params     = "",
2011 d7f9b689 Luiz Capitulino
        .help       = "show the current VM status (running|paused)",
2012 910df89d Luiz Capitulino
        .mhandler.info = do_info_status,
2013 d7f9b689 Luiz Capitulino
    },
2014 d7f9b689 Luiz Capitulino
    {
2015 d7f9b689 Luiz Capitulino
        .name       = "pcmcia",
2016 d7f9b689 Luiz Capitulino
        .args_type  = "",
2017 d7f9b689 Luiz Capitulino
        .params     = "",
2018 d7f9b689 Luiz Capitulino
        .help       = "show guest PCMCIA status",
2019 910df89d Luiz Capitulino
        .mhandler.info = pcmcia_info,
2020 d7f9b689 Luiz Capitulino
    },
2021 d7f9b689 Luiz Capitulino
    {
2022 d7f9b689 Luiz Capitulino
        .name       = "mice",
2023 d7f9b689 Luiz Capitulino
        .args_type  = "",
2024 d7f9b689 Luiz Capitulino
        .params     = "",
2025 d7f9b689 Luiz Capitulino
        .help       = "show which guest mouse is receiving events",
2026 910df89d Luiz Capitulino
        .mhandler.info = do_info_mice,
2027 d7f9b689 Luiz Capitulino
    },
2028 d7f9b689 Luiz Capitulino
    {
2029 d7f9b689 Luiz Capitulino
        .name       = "vnc",
2030 d7f9b689 Luiz Capitulino
        .args_type  = "",
2031 d7f9b689 Luiz Capitulino
        .params     = "",
2032 d7f9b689 Luiz Capitulino
        .help       = "show the vnc server status",
2033 910df89d Luiz Capitulino
        .mhandler.info = do_info_vnc,
2034 d7f9b689 Luiz Capitulino
    },
2035 d7f9b689 Luiz Capitulino
    {
2036 d7f9b689 Luiz Capitulino
        .name       = "name",
2037 d7f9b689 Luiz Capitulino
        .args_type  = "",
2038 d7f9b689 Luiz Capitulino
        .params     = "",
2039 d7f9b689 Luiz Capitulino
        .help       = "show the current VM name",
2040 910df89d Luiz Capitulino
        .mhandler.info = do_info_name,
2041 d7f9b689 Luiz Capitulino
    },
2042 d7f9b689 Luiz Capitulino
    {
2043 d7f9b689 Luiz Capitulino
        .name       = "uuid",
2044 d7f9b689 Luiz Capitulino
        .args_type  = "",
2045 d7f9b689 Luiz Capitulino
        .params     = "",
2046 d7f9b689 Luiz Capitulino
        .help       = "show the current VM UUID",
2047 910df89d Luiz Capitulino
        .mhandler.info = do_info_uuid,
2048 d7f9b689 Luiz Capitulino
    },
2049 76a66253 j_mayer
#if defined(TARGET_PPC)
2050 d7f9b689 Luiz Capitulino
    {
2051 d7f9b689 Luiz Capitulino
        .name       = "cpustats",
2052 d7f9b689 Luiz Capitulino
        .args_type  = "",
2053 d7f9b689 Luiz Capitulino
        .params     = "",
2054 d7f9b689 Luiz Capitulino
        .help       = "show CPU statistics",
2055 910df89d Luiz Capitulino
        .mhandler.info = do_info_cpu_stats,
2056 d7f9b689 Luiz Capitulino
    },
2057 76a66253 j_mayer
#endif
2058 31a60e22 blueswir1
#if defined(CONFIG_SLIRP)
2059 d7f9b689 Luiz Capitulino
    {
2060 d7f9b689 Luiz Capitulino
        .name       = "usernet",
2061 d7f9b689 Luiz Capitulino
        .args_type  = "",
2062 d7f9b689 Luiz Capitulino
        .params     = "",
2063 d7f9b689 Luiz Capitulino
        .help       = "show user network stack connection states",
2064 910df89d Luiz Capitulino
        .mhandler.info = do_info_usernet,
2065 d7f9b689 Luiz Capitulino
    },
2066 31a60e22 blueswir1
#endif
2067 d7f9b689 Luiz Capitulino
    {
2068 d7f9b689 Luiz Capitulino
        .name       = "migrate",
2069 d7f9b689 Luiz Capitulino
        .args_type  = "",
2070 d7f9b689 Luiz Capitulino
        .params     = "",
2071 d7f9b689 Luiz Capitulino
        .help       = "show migration status",
2072 910df89d Luiz Capitulino
        .mhandler.info = do_info_migrate,
2073 d7f9b689 Luiz Capitulino
    },
2074 d7f9b689 Luiz Capitulino
    {
2075 d7f9b689 Luiz Capitulino
        .name       = "balloon",
2076 d7f9b689 Luiz Capitulino
        .args_type  = "",
2077 d7f9b689 Luiz Capitulino
        .params     = "",
2078 d7f9b689 Luiz Capitulino
        .help       = "show balloon information",
2079 910df89d Luiz Capitulino
        .mhandler.info = do_info_balloon,
2080 d7f9b689 Luiz Capitulino
    },
2081 d7f9b689 Luiz Capitulino
    {
2082 d7f9b689 Luiz Capitulino
        .name       = "qtree",
2083 d7f9b689 Luiz Capitulino
        .args_type  = "",
2084 d7f9b689 Luiz Capitulino
        .params     = "",
2085 d7f9b689 Luiz Capitulino
        .help       = "show device tree",
2086 910df89d Luiz Capitulino
        .mhandler.info = do_info_qtree,
2087 d7f9b689 Luiz Capitulino
    },
2088 d7f9b689 Luiz Capitulino
    {
2089 d7f9b689 Luiz Capitulino
        .name       = "qdm",
2090 d7f9b689 Luiz Capitulino
        .args_type  = "",
2091 d7f9b689 Luiz Capitulino
        .params     = "",
2092 d7f9b689 Luiz Capitulino
        .help       = "show qdev device model list",
2093 910df89d Luiz Capitulino
        .mhandler.info = do_info_qdm,
2094 d7f9b689 Luiz Capitulino
    },
2095 d7f9b689 Luiz Capitulino
    {
2096 d7f9b689 Luiz Capitulino
        .name       = "roms",
2097 d7f9b689 Luiz Capitulino
        .args_type  = "",
2098 d7f9b689 Luiz Capitulino
        .params     = "",
2099 d7f9b689 Luiz Capitulino
        .help       = "show roms",
2100 910df89d Luiz Capitulino
        .mhandler.info = do_info_roms,
2101 d7f9b689 Luiz Capitulino
    },
2102 d7f9b689 Luiz Capitulino
    {
2103 d7f9b689 Luiz Capitulino
        .name       = NULL,
2104 d7f9b689 Luiz Capitulino
    },
2105 9dc39cba bellard
};
2106 9dc39cba bellard
2107 9307c4c1 bellard
/*******************************************************************/
2108 9307c4c1 bellard
2109 9307c4c1 bellard
static const char *pch;
2110 9307c4c1 bellard
static jmp_buf expr_env;
2111 9307c4c1 bellard
2112 92a31b1f bellard
#define MD_TLONG 0
2113 92a31b1f bellard
#define MD_I32   1
2114 92a31b1f bellard
2115 9307c4c1 bellard
typedef struct MonitorDef {
2116 9307c4c1 bellard
    const char *name;
2117 9307c4c1 bellard
    int offset;
2118 8662d656 blueswir1
    target_long (*get_value)(const struct MonitorDef *md, int val);
2119 92a31b1f bellard
    int type;
2120 9307c4c1 bellard
} MonitorDef;
2121 9307c4c1 bellard
2122 57206fd4 bellard
#if defined(TARGET_I386)
2123 8662d656 blueswir1
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2124 57206fd4 bellard
{
2125 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2126 6a00d601 bellard
    if (!env)
2127 6a00d601 bellard
        return 0;
2128 6a00d601 bellard
    return env->eip + env->segs[R_CS].base;
2129 57206fd4 bellard
}
2130 57206fd4 bellard
#endif
2131 57206fd4 bellard
2132 a541f297 bellard
#if defined(TARGET_PPC)
2133 8662d656 blueswir1
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2134 a541f297 bellard
{
2135 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2136 a541f297 bellard
    unsigned int u;
2137 a541f297 bellard
    int i;
2138 a541f297 bellard
2139 6a00d601 bellard
    if (!env)
2140 6a00d601 bellard
        return 0;
2141 6a00d601 bellard
2142 a541f297 bellard
    u = 0;
2143 a541f297 bellard
    for (i = 0; i < 8; i++)
2144 28a76be8 aliguori
        u |= env->crf[i] << (32 - (4 * i));
2145 a541f297 bellard
2146 a541f297 bellard
    return u;
2147 a541f297 bellard
}
2148 a541f297 bellard
2149 8662d656 blueswir1
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2150 a541f297 bellard
{
2151 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2152 6a00d601 bellard
    if (!env)
2153 6a00d601 bellard
        return 0;
2154 0411a972 j_mayer
    return env->msr;
2155 a541f297 bellard
}
2156 a541f297 bellard
2157 8662d656 blueswir1
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2158 a541f297 bellard
{
2159 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2160 6a00d601 bellard
    if (!env)
2161 6a00d601 bellard
        return 0;
2162 3d7b417e aurel32
    return env->xer;
2163 a541f297 bellard
}
2164 9fddaa0c bellard
2165 8662d656 blueswir1
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2166 9fddaa0c bellard
{
2167 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2168 6a00d601 bellard
    if (!env)
2169 6a00d601 bellard
        return 0;
2170 6a00d601 bellard
    return cpu_ppc_load_decr(env);
2171 9fddaa0c bellard
}
2172 9fddaa0c bellard
2173 8662d656 blueswir1
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2174 9fddaa0c bellard
{
2175 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2176 6a00d601 bellard
    if (!env)
2177 6a00d601 bellard
        return 0;
2178 6a00d601 bellard
    return cpu_ppc_load_tbu(env);
2179 9fddaa0c bellard
}
2180 9fddaa0c bellard
2181 8662d656 blueswir1
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2182 9fddaa0c bellard
{
2183 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2184 6a00d601 bellard
    if (!env)
2185 6a00d601 bellard
        return 0;
2186 6a00d601 bellard
    return cpu_ppc_load_tbl(env);
2187 9fddaa0c bellard
}
2188 a541f297 bellard
#endif
2189 a541f297 bellard
2190 e95c8d51 bellard
#if defined(TARGET_SPARC)
2191 7b936c0c bellard
#ifndef TARGET_SPARC64
2192 8662d656 blueswir1
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2193 e95c8d51 bellard
{
2194 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2195 6a00d601 bellard
    if (!env)
2196 6a00d601 bellard
        return 0;
2197 6a00d601 bellard
    return GET_PSR(env);
2198 e95c8d51 bellard
}
2199 7b936c0c bellard
#endif
2200 e95c8d51 bellard
2201 8662d656 blueswir1
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2202 e95c8d51 bellard
{
2203 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2204 6a00d601 bellard
    if (!env)
2205 6a00d601 bellard
        return 0;
2206 6a00d601 bellard
    return env->regwptr[val];
2207 e95c8d51 bellard
}
2208 e95c8d51 bellard
#endif
2209 e95c8d51 bellard
2210 8662d656 blueswir1
static const MonitorDef monitor_defs[] = {
2211 9307c4c1 bellard
#ifdef TARGET_I386
2212 57206fd4 bellard
2213 57206fd4 bellard
#define SEG(name, seg) \
2214 92a31b1f bellard
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2215 57206fd4 bellard
    { name ".base", offsetof(CPUState, segs[seg].base) },\
2216 92a31b1f bellard
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2217 57206fd4 bellard
2218 9307c4c1 bellard
    { "eax", offsetof(CPUState, regs[0]) },
2219 9307c4c1 bellard
    { "ecx", offsetof(CPUState, regs[1]) },
2220 9307c4c1 bellard
    { "edx", offsetof(CPUState, regs[2]) },
2221 9307c4c1 bellard
    { "ebx", offsetof(CPUState, regs[3]) },
2222 9307c4c1 bellard
    { "esp|sp", offsetof(CPUState, regs[4]) },
2223 9307c4c1 bellard
    { "ebp|fp", offsetof(CPUState, regs[5]) },
2224 9307c4c1 bellard
    { "esi", offsetof(CPUState, regs[6]) },
2225 01038d2a bellard
    { "edi", offsetof(CPUState, regs[7]) },
2226 92a31b1f bellard
#ifdef TARGET_X86_64
2227 92a31b1f bellard
    { "r8", offsetof(CPUState, regs[8]) },
2228 92a31b1f bellard
    { "r9", offsetof(CPUState, regs[9]) },
2229 92a31b1f bellard
    { "r10", offsetof(CPUState, regs[10]) },
2230 92a31b1f bellard
    { "r11", offsetof(CPUState, regs[11]) },
2231 92a31b1f bellard
    { "r12", offsetof(CPUState, regs[12]) },
2232 92a31b1f bellard
    { "r13", offsetof(CPUState, regs[13]) },
2233 92a31b1f bellard
    { "r14", offsetof(CPUState, regs[14]) },
2234 92a31b1f bellard
    { "r15", offsetof(CPUState, regs[15]) },
2235 92a31b1f bellard
#endif
2236 9307c4c1 bellard
    { "eflags", offsetof(CPUState, eflags) },
2237 57206fd4 bellard
    { "eip", offsetof(CPUState, eip) },
2238 57206fd4 bellard
    SEG("cs", R_CS)
2239 57206fd4 bellard
    SEG("ds", R_DS)
2240 57206fd4 bellard
    SEG("es", R_ES)
2241 01038d2a bellard
    SEG("ss", R_SS)
2242 57206fd4 bellard
    SEG("fs", R_FS)
2243 57206fd4 bellard
    SEG("gs", R_GS)
2244 57206fd4 bellard
    { "pc", 0, monitor_get_pc, },
2245 a541f297 bellard
#elif defined(TARGET_PPC)
2246 ff937dba j_mayer
    /* General purpose registers */
2247 a541f297 bellard
    { "r0", offsetof(CPUState, gpr[0]) },
2248 a541f297 bellard
    { "r1", offsetof(CPUState, gpr[1]) },
2249 a541f297 bellard
    { "r2", offsetof(CPUState, gpr[2]) },
2250 a541f297 bellard
    { "r3", offsetof(CPUState, gpr[3]) },
2251 a541f297 bellard
    { "r4", offsetof(CPUState, gpr[4]) },
2252 a541f297 bellard
    { "r5", offsetof(CPUState, gpr[5]) },
2253 a541f297 bellard
    { "r6", offsetof(CPUState, gpr[6]) },
2254 a541f297 bellard
    { "r7", offsetof(CPUState, gpr[7]) },
2255 a541f297 bellard
    { "r8", offsetof(CPUState, gpr[8]) },
2256 a541f297 bellard
    { "r9", offsetof(CPUState, gpr[9]) },
2257 a541f297 bellard
    { "r10", offsetof(CPUState, gpr[10]) },
2258 a541f297 bellard
    { "r11", offsetof(CPUState, gpr[11]) },
2259 a541f297 bellard
    { "r12", offsetof(CPUState, gpr[12]) },
2260 a541f297 bellard
    { "r13", offsetof(CPUState, gpr[13]) },
2261 a541f297 bellard
    { "r14", offsetof(CPUState, gpr[14]) },
2262 a541f297 bellard
    { "r15", offsetof(CPUState, gpr[15]) },
2263 a541f297 bellard
    { "r16", offsetof(CPUState, gpr[16]) },
2264 a541f297 bellard
    { "r17", offsetof(CPUState, gpr[17]) },
2265 a541f297 bellard
    { "r18", offsetof(CPUState, gpr[18]) },
2266 a541f297 bellard
    { "r19", offsetof(CPUState, gpr[19]) },
2267 a541f297 bellard
    { "r20", offsetof(CPUState, gpr[20]) },
2268 a541f297 bellard
    { "r21", offsetof(CPUState, gpr[21]) },
2269 a541f297 bellard
    { "r22", offsetof(CPUState, gpr[22]) },
2270 a541f297 bellard
    { "r23", offsetof(CPUState, gpr[23]) },
2271 a541f297 bellard
    { "r24", offsetof(CPUState, gpr[24]) },
2272 a541f297 bellard
    { "r25", offsetof(CPUState, gpr[25]) },
2273 a541f297 bellard
    { "r26", offsetof(CPUState, gpr[26]) },
2274 a541f297 bellard
    { "r27", offsetof(CPUState, gpr[27]) },
2275 a541f297 bellard
    { "r28", offsetof(CPUState, gpr[28]) },
2276 a541f297 bellard
    { "r29", offsetof(CPUState, gpr[29]) },
2277 a541f297 bellard
    { "r30", offsetof(CPUState, gpr[30]) },
2278 a541f297 bellard
    { "r31", offsetof(CPUState, gpr[31]) },
2279 ff937dba j_mayer
    /* Floating point registers */
2280 ff937dba j_mayer
    { "f0", offsetof(CPUState, fpr[0]) },
2281 ff937dba j_mayer
    { "f1", offsetof(CPUState, fpr[1]) },
2282 ff937dba j_mayer
    { "f2", offsetof(CPUState, fpr[2]) },
2283 ff937dba j_mayer
    { "f3", offsetof(CPUState, fpr[3]) },
2284 ff937dba j_mayer
    { "f4", offsetof(CPUState, fpr[4]) },
2285 ff937dba j_mayer
    { "f5", offsetof(CPUState, fpr[5]) },
2286 ff937dba j_mayer
    { "f6", offsetof(CPUState, fpr[6]) },
2287 ff937dba j_mayer
    { "f7", offsetof(CPUState, fpr[7]) },
2288 ff937dba j_mayer
    { "f8", offsetof(CPUState, fpr[8]) },
2289 ff937dba j_mayer
    { "f9", offsetof(CPUState, fpr[9]) },
2290 ff937dba j_mayer
    { "f10", offsetof(CPUState, fpr[10]) },
2291 ff937dba j_mayer
    { "f11", offsetof(CPUState, fpr[11]) },
2292 ff937dba j_mayer
    { "f12", offsetof(CPUState, fpr[12]) },
2293 ff937dba j_mayer
    { "f13", offsetof(CPUState, fpr[13]) },
2294 ff937dba j_mayer
    { "f14", offsetof(CPUState, fpr[14]) },
2295 ff937dba j_mayer
    { "f15", offsetof(CPUState, fpr[15]) },
2296 ff937dba j_mayer
    { "f16", offsetof(CPUState, fpr[16]) },
2297 ff937dba j_mayer
    { "f17", offsetof(CPUState, fpr[17]) },
2298 ff937dba j_mayer
    { "f18", offsetof(CPUState, fpr[18]) },
2299 ff937dba j_mayer
    { "f19", offsetof(CPUState, fpr[19]) },
2300 ff937dba j_mayer
    { "f20", offsetof(CPUState, fpr[20]) },
2301 ff937dba j_mayer
    { "f21", offsetof(CPUState, fpr[21]) },
2302 ff937dba j_mayer
    { "f22", offsetof(CPUState, fpr[22]) },
2303 ff937dba j_mayer
    { "f23", offsetof(CPUState, fpr[23]) },
2304 ff937dba j_mayer
    { "f24", offsetof(CPUState, fpr[24]) },
2305 ff937dba j_mayer
    { "f25", offsetof(CPUState, fpr[25]) },
2306 ff937dba j_mayer
    { "f26", offsetof(CPUState, fpr[26]) },
2307 ff937dba j_mayer
    { "f27", offsetof(CPUState, fpr[27]) },
2308 ff937dba j_mayer
    { "f28", offsetof(CPUState, fpr[28]) },
2309 ff937dba j_mayer
    { "f29", offsetof(CPUState, fpr[29]) },
2310 ff937dba j_mayer
    { "f30", offsetof(CPUState, fpr[30]) },
2311 ff937dba j_mayer
    { "f31", offsetof(CPUState, fpr[31]) },
2312 ff937dba j_mayer
    { "fpscr", offsetof(CPUState, fpscr) },
2313 ff937dba j_mayer
    /* Next instruction pointer */
2314 57206fd4 bellard
    { "nip|pc", offsetof(CPUState, nip) },
2315 a541f297 bellard
    { "lr", offsetof(CPUState, lr) },
2316 a541f297 bellard
    { "ctr", offsetof(CPUState, ctr) },
2317 9fddaa0c bellard
    { "decr", 0, &monitor_get_decr, },
2318 a541f297 bellard
    { "ccr", 0, &monitor_get_ccr, },
2319 ff937dba j_mayer
    /* Machine state register */
2320 a541f297 bellard
    { "msr", 0, &monitor_get_msr, },
2321 a541f297 bellard
    { "xer", 0, &monitor_get_xer, },
2322 9fddaa0c bellard
    { "tbu", 0, &monitor_get_tbu, },
2323 9fddaa0c bellard
    { "tbl", 0, &monitor_get_tbl, },
2324 ff937dba j_mayer
#if defined(TARGET_PPC64)
2325 ff937dba j_mayer
    /* Address space register */
2326 ff937dba j_mayer
    { "asr", offsetof(CPUState, asr) },
2327 ff937dba j_mayer
#endif
2328 ff937dba j_mayer
    /* Segment registers */
2329 a541f297 bellard
    { "sdr1", offsetof(CPUState, sdr1) },
2330 a541f297 bellard
    { "sr0", offsetof(CPUState, sr[0]) },
2331 a541f297 bellard
    { "sr1", offsetof(CPUState, sr[1]) },
2332 a541f297 bellard
    { "sr2", offsetof(CPUState, sr[2]) },
2333 a541f297 bellard
    { "sr3", offsetof(CPUState, sr[3]) },
2334 a541f297 bellard
    { "sr4", offsetof(CPUState, sr[4]) },
2335 a541f297 bellard
    { "sr5", offsetof(CPUState, sr[5]) },
2336 a541f297 bellard
    { "sr6", offsetof(CPUState, sr[6]) },
2337 a541f297 bellard
    { "sr7", offsetof(CPUState, sr[7]) },
2338 a541f297 bellard
    { "sr8", offsetof(CPUState, sr[8]) },
2339 a541f297 bellard
    { "sr9", offsetof(CPUState, sr[9]) },
2340 a541f297 bellard
    { "sr10", offsetof(CPUState, sr[10]) },
2341 a541f297 bellard
    { "sr11", offsetof(CPUState, sr[11]) },
2342 a541f297 bellard
    { "sr12", offsetof(CPUState, sr[12]) },
2343 a541f297 bellard
    { "sr13", offsetof(CPUState, sr[13]) },
2344 a541f297 bellard
    { "sr14", offsetof(CPUState, sr[14]) },
2345 a541f297 bellard
    { "sr15", offsetof(CPUState, sr[15]) },
2346 a541f297 bellard
    /* Too lazy to put BATs and SPRs ... */
2347 e95c8d51 bellard
#elif defined(TARGET_SPARC)
2348 e95c8d51 bellard
    { "g0", offsetof(CPUState, gregs[0]) },
2349 e95c8d51 bellard
    { "g1", offsetof(CPUState, gregs[1]) },
2350 e95c8d51 bellard
    { "g2", offsetof(CPUState, gregs[2]) },
2351 e95c8d51 bellard
    { "g3", offsetof(CPUState, gregs[3]) },
2352 e95c8d51 bellard
    { "g4", offsetof(CPUState, gregs[4]) },
2353 e95c8d51 bellard
    { "g5", offsetof(CPUState, gregs[5]) },
2354 e95c8d51 bellard
    { "g6", offsetof(CPUState, gregs[6]) },
2355 e95c8d51 bellard
    { "g7", offsetof(CPUState, gregs[7]) },
2356 e95c8d51 bellard
    { "o0", 0, monitor_get_reg },
2357 e95c8d51 bellard
    { "o1", 1, monitor_get_reg },
2358 e95c8d51 bellard
    { "o2", 2, monitor_get_reg },
2359 e95c8d51 bellard
    { "o3", 3, monitor_get_reg },
2360 e95c8d51 bellard
    { "o4", 4, monitor_get_reg },
2361 e95c8d51 bellard
    { "o5", 5, monitor_get_reg },
2362 e95c8d51 bellard
    { "o6", 6, monitor_get_reg },
2363 e95c8d51 bellard
    { "o7", 7, monitor_get_reg },
2364 e95c8d51 bellard
    { "l0", 8, monitor_get_reg },
2365 e95c8d51 bellard
    { "l1", 9, monitor_get_reg },
2366 e95c8d51 bellard
    { "l2", 10, monitor_get_reg },
2367 e95c8d51 bellard
    { "l3", 11, monitor_get_reg },
2368 e95c8d51 bellard
    { "l4", 12, monitor_get_reg },
2369 e95c8d51 bellard
    { "l5", 13, monitor_get_reg },
2370 e95c8d51 bellard
    { "l6", 14, monitor_get_reg },
2371 e95c8d51 bellard
    { "l7", 15, monitor_get_reg },
2372 e95c8d51 bellard
    { "i0", 16, monitor_get_reg },
2373 e95c8d51 bellard
    { "i1", 17, monitor_get_reg },
2374 e95c8d51 bellard
    { "i2", 18, monitor_get_reg },
2375 e95c8d51 bellard
    { "i3", 19, monitor_get_reg },
2376 e95c8d51 bellard
    { "i4", 20, monitor_get_reg },
2377 e95c8d51 bellard
    { "i5", 21, monitor_get_reg },
2378 e95c8d51 bellard
    { "i6", 22, monitor_get_reg },
2379 e95c8d51 bellard
    { "i7", 23, monitor_get_reg },
2380 e95c8d51 bellard
    { "pc", offsetof(CPUState, pc) },
2381 e95c8d51 bellard
    { "npc", offsetof(CPUState, npc) },
2382 e95c8d51 bellard
    { "y", offsetof(CPUState, y) },
2383 7b936c0c bellard
#ifndef TARGET_SPARC64
2384 e95c8d51 bellard
    { "psr", 0, &monitor_get_psr, },
2385 e95c8d51 bellard
    { "wim", offsetof(CPUState, wim) },
2386 7b936c0c bellard
#endif
2387 e95c8d51 bellard
    { "tbr", offsetof(CPUState, tbr) },
2388 e95c8d51 bellard
    { "fsr", offsetof(CPUState, fsr) },
2389 e95c8d51 bellard
    { "f0", offsetof(CPUState, fpr[0]) },
2390 e95c8d51 bellard
    { "f1", offsetof(CPUState, fpr[1]) },
2391 e95c8d51 bellard
    { "f2", offsetof(CPUState, fpr[2]) },
2392 e95c8d51 bellard
    { "f3", offsetof(CPUState, fpr[3]) },
2393 e95c8d51 bellard
    { "f4", offsetof(CPUState, fpr[4]) },
2394 e95c8d51 bellard
    { "f5", offsetof(CPUState, fpr[5]) },
2395 e95c8d51 bellard
    { "f6", offsetof(CPUState, fpr[6]) },
2396 e95c8d51 bellard
    { "f7", offsetof(CPUState, fpr[7]) },
2397 e95c8d51 bellard
    { "f8", offsetof(CPUState, fpr[8]) },
2398 e95c8d51 bellard
    { "f9", offsetof(CPUState, fpr[9]) },
2399 e95c8d51 bellard
    { "f10", offsetof(CPUState, fpr[10]) },
2400 e95c8d51 bellard
    { "f11", offsetof(CPUState, fpr[11]) },
2401 e95c8d51 bellard
    { "f12", offsetof(CPUState, fpr[12]) },
2402 e95c8d51 bellard
    { "f13", offsetof(CPUState, fpr[13]) },
2403 e95c8d51 bellard
    { "f14", offsetof(CPUState, fpr[14]) },
2404 e95c8d51 bellard
    { "f15", offsetof(CPUState, fpr[15]) },
2405 e95c8d51 bellard
    { "f16", offsetof(CPUState, fpr[16]) },
2406 e95c8d51 bellard
    { "f17", offsetof(CPUState, fpr[17]) },
2407 e95c8d51 bellard
    { "f18", offsetof(CPUState, fpr[18]) },
2408 e95c8d51 bellard
    { "f19", offsetof(CPUState, fpr[19]) },
2409 e95c8d51 bellard
    { "f20", offsetof(CPUState, fpr[20]) },
2410 e95c8d51 bellard
    { "f21", offsetof(CPUState, fpr[21]) },
2411 e95c8d51 bellard
    { "f22", offsetof(CPUState, fpr[22]) },
2412 e95c8d51 bellard
    { "f23", offsetof(CPUState, fpr[23]) },
2413 e95c8d51 bellard
    { "f24", offsetof(CPUState, fpr[24]) },
2414 e95c8d51 bellard
    { "f25", offsetof(CPUState, fpr[25]) },
2415 e95c8d51 bellard
    { "f26", offsetof(CPUState, fpr[26]) },
2416 e95c8d51 bellard
    { "f27", offsetof(CPUState, fpr[27]) },
2417 e95c8d51 bellard
    { "f28", offsetof(CPUState, fpr[28]) },
2418 e95c8d51 bellard
    { "f29", offsetof(CPUState, fpr[29]) },
2419 e95c8d51 bellard
    { "f30", offsetof(CPUState, fpr[30]) },
2420 e95c8d51 bellard
    { "f31", offsetof(CPUState, fpr[31]) },
2421 7b936c0c bellard
#ifdef TARGET_SPARC64
2422 7b936c0c bellard
    { "f32", offsetof(CPUState, fpr[32]) },
2423 7b936c0c bellard
    { "f34", offsetof(CPUState, fpr[34]) },
2424 7b936c0c bellard
    { "f36", offsetof(CPUState, fpr[36]) },
2425 7b936c0c bellard
    { "f38", offsetof(CPUState, fpr[38]) },
2426 7b936c0c bellard
    { "f40", offsetof(CPUState, fpr[40]) },
2427 7b936c0c bellard
    { "f42", offsetof(CPUState, fpr[42]) },
2428 7b936c0c bellard
    { "f44", offsetof(CPUState, fpr[44]) },
2429 7b936c0c bellard
    { "f46", offsetof(CPUState, fpr[46]) },
2430 7b936c0c bellard
    { "f48", offsetof(CPUState, fpr[48]) },
2431 7b936c0c bellard
    { "f50", offsetof(CPUState, fpr[50]) },
2432 7b936c0c bellard
    { "f52", offsetof(CPUState, fpr[52]) },
2433 7b936c0c bellard
    { "f54", offsetof(CPUState, fpr[54]) },
2434 7b936c0c bellard
    { "f56", offsetof(CPUState, fpr[56]) },
2435 7b936c0c bellard
    { "f58", offsetof(CPUState, fpr[58]) },
2436 7b936c0c bellard
    { "f60", offsetof(CPUState, fpr[60]) },
2437 7b936c0c bellard
    { "f62", offsetof(CPUState, fpr[62]) },
2438 7b936c0c bellard
    { "asi", offsetof(CPUState, asi) },
2439 7b936c0c bellard
    { "pstate", offsetof(CPUState, pstate) },
2440 7b936c0c bellard
    { "cansave", offsetof(CPUState, cansave) },
2441 7b936c0c bellard
    { "canrestore", offsetof(CPUState, canrestore) },
2442 7b936c0c bellard
    { "otherwin", offsetof(CPUState, otherwin) },
2443 7b936c0c bellard
    { "wstate", offsetof(CPUState, wstate) },
2444 7b936c0c bellard
    { "cleanwin", offsetof(CPUState, cleanwin) },
2445 7b936c0c bellard
    { "fprs", offsetof(CPUState, fprs) },
2446 7b936c0c bellard
#endif
2447 9307c4c1 bellard
#endif
2448 9307c4c1 bellard
    { NULL },
2449 9307c4c1 bellard
};
2450 9307c4c1 bellard
2451 376253ec aliguori
static void expr_error(Monitor *mon, const char *msg)
2452 9dc39cba bellard
{
2453 376253ec aliguori
    monitor_printf(mon, "%s\n", msg);
2454 9307c4c1 bellard
    longjmp(expr_env, 1);
2455 9307c4c1 bellard
}
2456 9307c4c1 bellard
2457 6a00d601 bellard
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2458 92a31b1f bellard
static int get_monitor_def(target_long *pval, const char *name)
2459 9307c4c1 bellard
{
2460 8662d656 blueswir1
    const MonitorDef *md;
2461 92a31b1f bellard
    void *ptr;
2462 92a31b1f bellard
2463 9307c4c1 bellard
    for(md = monitor_defs; md->name != NULL; md++) {
2464 9307c4c1 bellard
        if (compare_cmd(name, md->name)) {
2465 9307c4c1 bellard
            if (md->get_value) {
2466 e95c8d51 bellard
                *pval = md->get_value(md, md->offset);
2467 9307c4c1 bellard
            } else {
2468 6a00d601 bellard
                CPUState *env = mon_get_cpu();
2469 6a00d601 bellard
                if (!env)
2470 6a00d601 bellard
                    return -2;
2471 6a00d601 bellard
                ptr = (uint8_t *)env + md->offset;
2472 92a31b1f bellard
                switch(md->type) {
2473 92a31b1f bellard
                case MD_I32:
2474 92a31b1f bellard
                    *pval = *(int32_t *)ptr;
2475 92a31b1f bellard
                    break;
2476 92a31b1f bellard
                case MD_TLONG:
2477 92a31b1f bellard
                    *pval = *(target_long *)ptr;
2478 92a31b1f bellard
                    break;
2479 92a31b1f bellard
                default:
2480 92a31b1f bellard
                    *pval = 0;
2481 92a31b1f bellard
                    break;
2482 92a31b1f bellard
                }
2483 9307c4c1 bellard
            }
2484 9307c4c1 bellard
            return 0;
2485 9307c4c1 bellard
        }
2486 9307c4c1 bellard
    }
2487 9307c4c1 bellard
    return -1;
2488 9307c4c1 bellard
}
2489 9307c4c1 bellard
2490 9307c4c1 bellard
static void next(void)
2491 9307c4c1 bellard
{
2492 660f11be Blue Swirl
    if (*pch != '\0') {
2493 9307c4c1 bellard
        pch++;
2494 cd390083 blueswir1
        while (qemu_isspace(*pch))
2495 9307c4c1 bellard
            pch++;
2496 9307c4c1 bellard
    }
2497 9307c4c1 bellard
}
2498 9307c4c1 bellard
2499 376253ec aliguori
static int64_t expr_sum(Monitor *mon);
2500 9307c4c1 bellard
2501 376253ec aliguori
static int64_t expr_unary(Monitor *mon)
2502 9307c4c1 bellard
{
2503 c2efc95d blueswir1
    int64_t n;
2504 9307c4c1 bellard
    char *p;
2505 6a00d601 bellard
    int ret;
2506 9307c4c1 bellard
2507 9307c4c1 bellard
    switch(*pch) {
2508 9307c4c1 bellard
    case '+':
2509 9307c4c1 bellard
        next();
2510 376253ec aliguori
        n = expr_unary(mon);
2511 9307c4c1 bellard
        break;
2512 9307c4c1 bellard
    case '-':
2513 9307c4c1 bellard
        next();
2514 376253ec aliguori
        n = -expr_unary(mon);
2515 9307c4c1 bellard
        break;
2516 9307c4c1 bellard
    case '~':
2517 9307c4c1 bellard
        next();
2518 376253ec aliguori
        n = ~expr_unary(mon);
2519 9307c4c1 bellard
        break;
2520 9307c4c1 bellard
    case '(':
2521 9307c4c1 bellard
        next();
2522 376253ec aliguori
        n = expr_sum(mon);
2523 9307c4c1 bellard
        if (*pch != ')') {
2524 376253ec aliguori
            expr_error(mon, "')' expected");
2525 9307c4c1 bellard
        }
2526 9307c4c1 bellard
        next();
2527 9307c4c1 bellard
        break;
2528 81d0912d bellard
    case '\'':
2529 81d0912d bellard
        pch++;
2530 81d0912d bellard
        if (*pch == '\0')
2531 376253ec aliguori
            expr_error(mon, "character constant expected");
2532 81d0912d bellard
        n = *pch;
2533 81d0912d bellard
        pch++;
2534 81d0912d bellard
        if (*pch != '\'')
2535 376253ec aliguori
            expr_error(mon, "missing terminating \' character");
2536 81d0912d bellard
        next();
2537 81d0912d bellard
        break;
2538 9307c4c1 bellard
    case '$':
2539 9307c4c1 bellard
        {
2540 9307c4c1 bellard
            char buf[128], *q;
2541 69b34976 ths
            target_long reg=0;
2542 3b46e624 ths
2543 9307c4c1 bellard
            pch++;
2544 9307c4c1 bellard
            q = buf;
2545 9307c4c1 bellard
            while ((*pch >= 'a' && *pch <= 'z') ||
2546 9307c4c1 bellard
                   (*pch >= 'A' && *pch <= 'Z') ||
2547 9307c4c1 bellard
                   (*pch >= '0' && *pch <= '9') ||
2548 57206fd4 bellard
                   *pch == '_' || *pch == '.') {
2549 9307c4c1 bellard
                if ((q - buf) < sizeof(buf) - 1)
2550 9307c4c1 bellard
                    *q++ = *pch;
2551 9307c4c1 bellard
                pch++;
2552 9307c4c1 bellard
            }
2553 cd390083 blueswir1
            while (qemu_isspace(*pch))
2554 9307c4c1 bellard
                pch++;
2555 9307c4c1 bellard
            *q = 0;
2556 7743e588 blueswir1
            ret = get_monitor_def(&reg, buf);
2557 6a00d601 bellard
            if (ret == -1)
2558 376253ec aliguori
                expr_error(mon, "unknown register");
2559 5fafdf24 ths
            else if (ret == -2)
2560 376253ec aliguori
                expr_error(mon, "no cpu defined");
2561 7743e588 blueswir1
            n = reg;
2562 9307c4c1 bellard
        }
2563 9307c4c1 bellard
        break;
2564 9307c4c1 bellard
    case '\0':
2565 376253ec aliguori
        expr_error(mon, "unexpected end of expression");
2566 9307c4c1 bellard
        n = 0;
2567 9307c4c1 bellard
        break;
2568 9307c4c1 bellard
    default:
2569 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS > 32
2570 4f4fbf77 bellard
        n = strtoull(pch, &p, 0);
2571 4f4fbf77 bellard
#else
2572 9307c4c1 bellard
        n = strtoul(pch, &p, 0);
2573 4f4fbf77 bellard
#endif
2574 9307c4c1 bellard
        if (pch == p) {
2575 376253ec aliguori
            expr_error(mon, "invalid char in expression");
2576 9307c4c1 bellard
        }
2577 9307c4c1 bellard
        pch = p;
2578 cd390083 blueswir1
        while (qemu_isspace(*pch))
2579 9307c4c1 bellard
            pch++;
2580 9307c4c1 bellard
        break;
2581 9307c4c1 bellard
    }
2582 9307c4c1 bellard
    return n;
2583 9307c4c1 bellard
}
2584 9307c4c1 bellard
2585 9307c4c1 bellard
2586 376253ec aliguori
static int64_t expr_prod(Monitor *mon)
2587 9307c4c1 bellard
{
2588 c2efc95d blueswir1
    int64_t val, val2;
2589 92a31b1f bellard
    int op;
2590 3b46e624 ths
2591 376253ec aliguori
    val = expr_unary(mon);
2592 9307c4c1 bellard
    for(;;) {
2593 9307c4c1 bellard
        op = *pch;
2594 9307c4c1 bellard
        if (op != '*' && op != '/' && op != '%')
2595 9307c4c1 bellard
            break;
2596 9307c4c1 bellard
        next();
2597 376253ec aliguori
        val2 = expr_unary(mon);
2598 9307c4c1 bellard
        switch(op) {
2599 9307c4c1 bellard
        default:
2600 9307c4c1 bellard
        case '*':
2601 9307c4c1 bellard
            val *= val2;
2602 9307c4c1 bellard
            break;
2603 9307c4c1 bellard
        case '/':
2604 9307c4c1 bellard
        case '%':
2605 5fafdf24 ths
            if (val2 == 0)
2606 376253ec aliguori
                expr_error(mon, "division by zero");
2607 9307c4c1 bellard
            if (op == '/')
2608 9307c4c1 bellard
                val /= val2;
2609 9307c4c1 bellard
            else
2610 9307c4c1 bellard
                val %= val2;
2611 9307c4c1 bellard
            break;
2612 9307c4c1 bellard
        }
2613 9307c4c1 bellard
    }
2614 9307c4c1 bellard
    return val;
2615 9307c4c1 bellard
}
2616 9307c4c1 bellard
2617 376253ec aliguori
static int64_t expr_logic(Monitor *mon)
2618 9307c4c1 bellard
{
2619 c2efc95d blueswir1
    int64_t val, val2;
2620 92a31b1f bellard
    int op;
2621 9307c4c1 bellard
2622 376253ec aliguori
    val = expr_prod(mon);
2623 9307c4c1 bellard
    for(;;) {
2624 9307c4c1 bellard
        op = *pch;
2625 9307c4c1 bellard
        if (op != '&' && op != '|' && op != '^')
2626 9307c4c1 bellard
            break;
2627 9307c4c1 bellard
        next();
2628 376253ec aliguori
        val2 = expr_prod(mon);
2629 9307c4c1 bellard
        switch(op) {
2630 9307c4c1 bellard
        default:
2631 9307c4c1 bellard
        case '&':
2632 9307c4c1 bellard
            val &= val2;
2633 9307c4c1 bellard
            break;
2634 9307c4c1 bellard
        case '|':
2635 9307c4c1 bellard
            val |= val2;
2636 9307c4c1 bellard
            break;
2637 9307c4c1 bellard
        case '^':
2638 9307c4c1 bellard
            val ^= val2;
2639 9307c4c1 bellard
            break;
2640 9307c4c1 bellard
        }
2641 9307c4c1 bellard
    }
2642 9307c4c1 bellard
    return val;
2643 9307c4c1 bellard
}
2644 9307c4c1 bellard
2645 376253ec aliguori
static int64_t expr_sum(Monitor *mon)
2646 9307c4c1 bellard
{
2647 c2efc95d blueswir1
    int64_t val, val2;
2648 92a31b1f bellard
    int op;
2649 9307c4c1 bellard
2650 376253ec aliguori
    val = expr_logic(mon);
2651 9307c4c1 bellard
    for(;;) {
2652 9307c4c1 bellard
        op = *pch;
2653 9307c4c1 bellard
        if (op != '+' && op != '-')
2654 9307c4c1 bellard
            break;
2655 9307c4c1 bellard
        next();
2656 376253ec aliguori
        val2 = expr_logic(mon);
2657 9307c4c1 bellard
        if (op == '+')
2658 9307c4c1 bellard
            val += val2;
2659 9307c4c1 bellard
        else
2660 9307c4c1 bellard
            val -= val2;
2661 9307c4c1 bellard
    }
2662 9307c4c1 bellard
    return val;
2663 9307c4c1 bellard
}
2664 9307c4c1 bellard
2665 376253ec aliguori
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2666 9307c4c1 bellard
{
2667 9307c4c1 bellard
    pch = *pp;
2668 9307c4c1 bellard
    if (setjmp(expr_env)) {
2669 9307c4c1 bellard
        *pp = pch;
2670 9307c4c1 bellard
        return -1;
2671 9307c4c1 bellard
    }
2672 cd390083 blueswir1
    while (qemu_isspace(*pch))
2673 9307c4c1 bellard
        pch++;
2674 376253ec aliguori
    *pval = expr_sum(mon);
2675 9307c4c1 bellard
    *pp = pch;
2676 9307c4c1 bellard
    return 0;
2677 9307c4c1 bellard
}
2678 9307c4c1 bellard
2679 9307c4c1 bellard
static int get_str(char *buf, int buf_size, const char **pp)
2680 9307c4c1 bellard
{
2681 9307c4c1 bellard
    const char *p;
2682 9307c4c1 bellard
    char *q;
2683 9307c4c1 bellard
    int c;
2684 9307c4c1 bellard
2685 81d0912d bellard
    q = buf;
2686 9307c4c1 bellard
    p = *pp;
2687 cd390083 blueswir1
    while (qemu_isspace(*p))
2688 9307c4c1 bellard
        p++;
2689 9307c4c1 bellard
    if (*p == '\0') {
2690 9307c4c1 bellard
    fail:
2691 81d0912d bellard
        *q = '\0';
2692 9307c4c1 bellard
        *pp = p;
2693 9307c4c1 bellard
        return -1;
2694 9307c4c1 bellard
    }
2695 9307c4c1 bellard
    if (*p == '\"') {
2696 9307c4c1 bellard
        p++;
2697 9307c4c1 bellard
        while (*p != '\0' && *p != '\"') {
2698 9307c4c1 bellard
            if (*p == '\\') {
2699 9307c4c1 bellard
                p++;
2700 9307c4c1 bellard
                c = *p++;
2701 9307c4c1 bellard
                switch(c) {
2702 9307c4c1 bellard
                case 'n':
2703 9307c4c1 bellard
                    c = '\n';
2704 9307c4c1 bellard
                    break;
2705 9307c4c1 bellard
                case 'r':
2706 9307c4c1 bellard
                    c = '\r';
2707 9307c4c1 bellard
                    break;
2708 9307c4c1 bellard
                case '\\':
2709 9307c4c1 bellard
                case '\'':
2710 9307c4c1 bellard
                case '\"':
2711 9307c4c1 bellard
                    break;
2712 9307c4c1 bellard
                default:
2713 9307c4c1 bellard
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2714 9307c4c1 bellard
                    goto fail;
2715 9307c4c1 bellard
                }
2716 9307c4c1 bellard
                if ((q - buf) < buf_size - 1) {
2717 9307c4c1 bellard
                    *q++ = c;
2718 9307c4c1 bellard
                }
2719 9307c4c1 bellard
            } else {
2720 9307c4c1 bellard
                if ((q - buf) < buf_size - 1) {
2721 9307c4c1 bellard
                    *q++ = *p;
2722 9307c4c1 bellard
                }
2723 9307c4c1 bellard
                p++;
2724 9307c4c1 bellard
            }
2725 9307c4c1 bellard
        }
2726 9307c4c1 bellard
        if (*p != '\"') {
2727 5b60212f bellard
            qemu_printf("unterminated string\n");
2728 9307c4c1 bellard
            goto fail;
2729 9307c4c1 bellard
        }
2730 9307c4c1 bellard
        p++;
2731 9307c4c1 bellard
    } else {
2732 cd390083 blueswir1
        while (*p != '\0' && !qemu_isspace(*p)) {
2733 9307c4c1 bellard
            if ((q - buf) < buf_size - 1) {
2734 9307c4c1 bellard
                *q++ = *p;
2735 9307c4c1 bellard
            }
2736 9307c4c1 bellard
            p++;
2737 9307c4c1 bellard
        }
2738 9307c4c1 bellard
    }
2739 81d0912d bellard
    *q = '\0';
2740 9307c4c1 bellard
    *pp = p;
2741 9307c4c1 bellard
    return 0;
2742 9307c4c1 bellard
}
2743 9307c4c1 bellard
2744 4590fd80 Luiz Capitulino
/*
2745 4590fd80 Luiz Capitulino
 * Store the command-name in cmdname, and return a pointer to
2746 4590fd80 Luiz Capitulino
 * the remaining of the command string.
2747 4590fd80 Luiz Capitulino
 */
2748 4590fd80 Luiz Capitulino
static const char *get_command_name(const char *cmdline,
2749 4590fd80 Luiz Capitulino
                                    char *cmdname, size_t nlen)
2750 4590fd80 Luiz Capitulino
{
2751 4590fd80 Luiz Capitulino
    size_t len;
2752 4590fd80 Luiz Capitulino
    const char *p, *pstart;
2753 4590fd80 Luiz Capitulino
2754 4590fd80 Luiz Capitulino
    p = cmdline;
2755 4590fd80 Luiz Capitulino
    while (qemu_isspace(*p))
2756 4590fd80 Luiz Capitulino
        p++;
2757 4590fd80 Luiz Capitulino
    if (*p == '\0')
2758 4590fd80 Luiz Capitulino
        return NULL;
2759 4590fd80 Luiz Capitulino
    pstart = p;
2760 4590fd80 Luiz Capitulino
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2761 4590fd80 Luiz Capitulino
        p++;
2762 4590fd80 Luiz Capitulino
    len = p - pstart;
2763 4590fd80 Luiz Capitulino
    if (len > nlen - 1)
2764 4590fd80 Luiz Capitulino
        len = nlen - 1;
2765 4590fd80 Luiz Capitulino
    memcpy(cmdname, pstart, len);
2766 4590fd80 Luiz Capitulino
    cmdname[len] = '\0';
2767 4590fd80 Luiz Capitulino
    return p;
2768 4590fd80 Luiz Capitulino
}
2769 4590fd80 Luiz Capitulino
2770 4d76d2ba Luiz Capitulino
/**
2771 4d76d2ba Luiz Capitulino
 * Read key of 'type' into 'key' and return the current
2772 4d76d2ba Luiz Capitulino
 * 'type' pointer.
2773 4d76d2ba Luiz Capitulino
 */
2774 4d76d2ba Luiz Capitulino
static char *key_get_info(const char *type, char **key)
2775 4d76d2ba Luiz Capitulino
{
2776 4d76d2ba Luiz Capitulino
    size_t len;
2777 4d76d2ba Luiz Capitulino
    char *p, *str;
2778 4d76d2ba Luiz Capitulino
2779 4d76d2ba Luiz Capitulino
    if (*type == ',')
2780 4d76d2ba Luiz Capitulino
        type++;
2781 4d76d2ba Luiz Capitulino
2782 4d76d2ba Luiz Capitulino
    p = strchr(type, ':');
2783 4d76d2ba Luiz Capitulino
    if (!p) {
2784 4d76d2ba Luiz Capitulino
        *key = NULL;
2785 4d76d2ba Luiz Capitulino
        return NULL;
2786 4d76d2ba Luiz Capitulino
    }
2787 4d76d2ba Luiz Capitulino
    len = p - type;
2788 4d76d2ba Luiz Capitulino
2789 4d76d2ba Luiz Capitulino
    str = qemu_malloc(len + 1);
2790 4d76d2ba Luiz Capitulino
    memcpy(str, type, len);
2791 4d76d2ba Luiz Capitulino
    str[len] = '\0';
2792 4d76d2ba Luiz Capitulino
2793 4d76d2ba Luiz Capitulino
    *key = str;
2794 4d76d2ba Luiz Capitulino
    return ++p;
2795 4d76d2ba Luiz Capitulino
}
2796 4d76d2ba Luiz Capitulino
2797 9307c4c1 bellard
static int default_fmt_format = 'x';
2798 9307c4c1 bellard
static int default_fmt_size = 4;
2799 9307c4c1 bellard
2800 9307c4c1 bellard
#define MAX_ARGS 16
2801 9307c4c1 bellard
2802 c227f099 Anthony Liguori
static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2803 55f81d96 Luiz Capitulino
                                              const char *cmdline,
2804 55f81d96 Luiz Capitulino
                                              QDict *qdict)
2805 9307c4c1 bellard
{
2806 4590fd80 Luiz Capitulino
    const char *p, *typestr;
2807 53773581 Luiz Capitulino
    int c;
2808 c227f099 Anthony Liguori
    const mon_cmd_t *cmd;
2809 9307c4c1 bellard
    char cmdname[256];
2810 9307c4c1 bellard
    char buf[1024];
2811 4d76d2ba Luiz Capitulino
    char *key;
2812 9dc39cba bellard
2813 9dc39cba bellard
#ifdef DEBUG
2814 376253ec aliguori
    monitor_printf(mon, "command='%s'\n", cmdline);
2815 9dc39cba bellard
#endif
2816 3b46e624 ths
2817 9307c4c1 bellard
    /* extract the command name */
2818 4590fd80 Luiz Capitulino
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2819 4590fd80 Luiz Capitulino
    if (!p)
2820 55f81d96 Luiz Capitulino
        return NULL;
2821 3b46e624 ths
2822 9307c4c1 bellard
    /* find the command */
2823 376253ec aliguori
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2824 5fafdf24 ths
        if (compare_cmd(cmdname, cmd->name))
2825 d91d9bf6 Luiz Capitulino
            break;
2826 d91d9bf6 Luiz Capitulino
    }
2827 d91d9bf6 Luiz Capitulino
2828 d91d9bf6 Luiz Capitulino
    if (cmd->name == NULL) {
2829 d91d9bf6 Luiz Capitulino
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2830 55f81d96 Luiz Capitulino
        return NULL;
2831 9307c4c1 bellard
    }
2832 9307c4c1 bellard
2833 9307c4c1 bellard
    /* parse the parameters */
2834 9307c4c1 bellard
    typestr = cmd->args_type;
2835 9dc39cba bellard
    for(;;) {
2836 4d76d2ba Luiz Capitulino
        typestr = key_get_info(typestr, &key);
2837 4d76d2ba Luiz Capitulino
        if (!typestr)
2838 9dc39cba bellard
            break;
2839 4d76d2ba Luiz Capitulino
        c = *typestr;
2840 9307c4c1 bellard
        typestr++;
2841 9307c4c1 bellard
        switch(c) {
2842 9307c4c1 bellard
        case 'F':
2843 81d0912d bellard
        case 'B':
2844 9307c4c1 bellard
        case 's':
2845 9307c4c1 bellard
            {
2846 9307c4c1 bellard
                int ret;
2847 3b46e624 ths
2848 cd390083 blueswir1
                while (qemu_isspace(*p))
2849 9307c4c1 bellard
                    p++;
2850 9307c4c1 bellard
                if (*typestr == '?') {
2851 9307c4c1 bellard
                    typestr++;
2852 9307c4c1 bellard
                    if (*p == '\0') {
2853 9307c4c1 bellard
                        /* no optional string: NULL argument */
2854 53773581 Luiz Capitulino
                        break;
2855 9307c4c1 bellard
                    }
2856 9307c4c1 bellard
                }
2857 9307c4c1 bellard
                ret = get_str(buf, sizeof(buf), &p);
2858 9307c4c1 bellard
                if (ret < 0) {
2859 81d0912d bellard
                    switch(c) {
2860 81d0912d bellard
                    case 'F':
2861 376253ec aliguori
                        monitor_printf(mon, "%s: filename expected\n",
2862 376253ec aliguori
                                       cmdname);
2863 81d0912d bellard
                        break;
2864 81d0912d bellard
                    case 'B':
2865 376253ec aliguori
                        monitor_printf(mon, "%s: block device name expected\n",
2866 376253ec aliguori
                                       cmdname);
2867 81d0912d bellard
                        break;
2868 81d0912d bellard
                    default:
2869 376253ec aliguori
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2870 81d0912d bellard
                        break;
2871 81d0912d bellard
                    }
2872 9307c4c1 bellard
                    goto fail;
2873 9307c4c1 bellard
                }
2874 53773581 Luiz Capitulino
                qdict_put(qdict, key, qstring_from_str(buf));
2875 9307c4c1 bellard
            }
2876 9dc39cba bellard
            break;
2877 9307c4c1 bellard
        case '/':
2878 9307c4c1 bellard
            {
2879 9307c4c1 bellard
                int count, format, size;
2880 3b46e624 ths
2881 cd390083 blueswir1
                while (qemu_isspace(*p))
2882 9307c4c1 bellard
                    p++;
2883 9307c4c1 bellard
                if (*p == '/') {
2884 9307c4c1 bellard
                    /* format found */
2885 9307c4c1 bellard
                    p++;
2886 9307c4c1 bellard
                    count = 1;
2887 cd390083 blueswir1
                    if (qemu_isdigit(*p)) {
2888 9307c4c1 bellard
                        count = 0;
2889 cd390083 blueswir1
                        while (qemu_isdigit(*p)) {
2890 9307c4c1 bellard
                            count = count * 10 + (*p - '0');
2891 9307c4c1 bellard
                            p++;
2892 9307c4c1 bellard
                        }
2893 9307c4c1 bellard
                    }
2894 9307c4c1 bellard
                    size = -1;
2895 9307c4c1 bellard
                    format = -1;
2896 9307c4c1 bellard
                    for(;;) {
2897 9307c4c1 bellard
                        switch(*p) {
2898 9307c4c1 bellard
                        case 'o':
2899 9307c4c1 bellard
                        case 'd':
2900 9307c4c1 bellard
                        case 'u':
2901 9307c4c1 bellard
                        case 'x':
2902 9307c4c1 bellard
                        case 'i':
2903 9307c4c1 bellard
                        case 'c':
2904 9307c4c1 bellard
                            format = *p++;
2905 9307c4c1 bellard
                            break;
2906 9307c4c1 bellard
                        case 'b':
2907 9307c4c1 bellard
                            size = 1;
2908 9307c4c1 bellard
                            p++;
2909 9307c4c1 bellard
                            break;
2910 9307c4c1 bellard
                        case 'h':
2911 9307c4c1 bellard
                            size = 2;
2912 9307c4c1 bellard
                            p++;
2913 9307c4c1 bellard
                            break;
2914 9307c4c1 bellard
                        case 'w':
2915 9307c4c1 bellard
                            size = 4;
2916 9307c4c1 bellard
                            p++;
2917 9307c4c1 bellard
                            break;
2918 9307c4c1 bellard
                        case 'g':
2919 9307c4c1 bellard
                        case 'L':
2920 9307c4c1 bellard
                            size = 8;
2921 9307c4c1 bellard
                            p++;
2922 9307c4c1 bellard
                            break;
2923 9307c4c1 bellard
                        default:
2924 9307c4c1 bellard
                            goto next;
2925 9307c4c1 bellard
                        }
2926 9307c4c1 bellard
                    }
2927 9307c4c1 bellard
                next:
2928 cd390083 blueswir1
                    if (*p != '\0' && !qemu_isspace(*p)) {
2929 376253ec aliguori
                        monitor_printf(mon, "invalid char in format: '%c'\n",
2930 376253ec aliguori
                                       *p);
2931 9307c4c1 bellard
                        goto fail;
2932 9307c4c1 bellard
                    }
2933 9307c4c1 bellard
                    if (format < 0)
2934 9307c4c1 bellard
                        format = default_fmt_format;
2935 4c27ba27 bellard
                    if (format != 'i') {
2936 4c27ba27 bellard
                        /* for 'i', not specifying a size gives -1 as size */
2937 4c27ba27 bellard
                        if (size < 0)
2938 4c27ba27 bellard
                            size = default_fmt_size;
2939 e90f009b aurel32
                        default_fmt_size = size;
2940 4c27ba27 bellard
                    }
2941 9307c4c1 bellard
                    default_fmt_format = format;
2942 9307c4c1 bellard
                } else {
2943 9307c4c1 bellard
                    count = 1;
2944 9307c4c1 bellard
                    format = default_fmt_format;
2945 4c27ba27 bellard
                    if (format != 'i') {
2946 4c27ba27 bellard
                        size = default_fmt_size;
2947 4c27ba27 bellard
                    } else {
2948 4c27ba27 bellard
                        size = -1;
2949 4c27ba27 bellard
                    }
2950 9307c4c1 bellard
                }
2951 f7188bbe Luiz Capitulino
                qdict_put(qdict, "count", qint_from_int(count));
2952 f7188bbe Luiz Capitulino
                qdict_put(qdict, "format", qint_from_int(format));
2953 f7188bbe Luiz Capitulino
                qdict_put(qdict, "size", qint_from_int(size));
2954 9307c4c1 bellard
            }
2955 9dc39cba bellard
            break;
2956 9307c4c1 bellard
        case 'i':
2957 92a31b1f bellard
        case 'l':
2958 9307c4c1 bellard
            {
2959 c2efc95d blueswir1
                int64_t val;
2960 7743e588 blueswir1
2961 cd390083 blueswir1
                while (qemu_isspace(*p))
2962 9307c4c1 bellard
                    p++;
2963 3440557b bellard
                if (*typestr == '?' || *typestr == '.') {
2964 3440557b bellard
                    if (*typestr == '?') {
2965 53773581 Luiz Capitulino
                        if (*p == '\0') {
2966 53773581 Luiz Capitulino
                            typestr++;
2967 53773581 Luiz Capitulino
                            break;
2968 53773581 Luiz Capitulino
                        }
2969 3440557b bellard
                    } else {
2970 3440557b bellard
                        if (*p == '.') {
2971 3440557b bellard
                            p++;
2972 cd390083 blueswir1
                            while (qemu_isspace(*p))
2973 3440557b bellard
                                p++;
2974 3440557b bellard
                        } else {
2975 53773581 Luiz Capitulino
                            typestr++;
2976 53773581 Luiz Capitulino
                            break;
2977 3440557b bellard
                        }
2978 3440557b bellard
                    }
2979 13224a87 bellard
                    typestr++;
2980 9307c4c1 bellard
                }
2981 376253ec aliguori
                if (get_expr(mon, &val, &p))
2982 9307c4c1 bellard
                    goto fail;
2983 675ebef9 Luiz Capitulino
                /* Check if 'i' is greater than 32-bit */
2984 675ebef9 Luiz Capitulino
                if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2985 675ebef9 Luiz Capitulino
                    monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2986 675ebef9 Luiz Capitulino
                    monitor_printf(mon, "integer is for 32-bit values\n");
2987 675ebef9 Luiz Capitulino
                    goto fail;
2988 675ebef9 Luiz Capitulino
                }
2989 53773581 Luiz Capitulino
                qdict_put(qdict, key, qint_from_int(val));
2990 9307c4c1 bellard
            }
2991 9307c4c1 bellard
            break;
2992 9307c4c1 bellard
        case '-':
2993 9307c4c1 bellard
            {
2994 9307c4c1 bellard
                int has_option;
2995 9307c4c1 bellard
                /* option */
2996 3b46e624 ths
2997 9307c4c1 bellard
                c = *typestr++;
2998 9307c4c1 bellard
                if (c == '\0')
2999 9307c4c1 bellard
                    goto bad_type;
3000 cd390083 blueswir1
                while (qemu_isspace(*p))
3001 9307c4c1 bellard
                    p++;
3002 9307c4c1 bellard
                has_option = 0;
3003 9307c4c1 bellard
                if (*p == '-') {
3004 9307c4c1 bellard
                    p++;
3005 9307c4c1 bellard
                    if (*p != c) {
3006 376253ec aliguori
                        monitor_printf(mon, "%s: unsupported option -%c\n",
3007 376253ec aliguori
                                       cmdname, *p);
3008 9307c4c1 bellard
                        goto fail;
3009 9307c4c1 bellard
                    }
3010 9307c4c1 bellard
                    p++;
3011 9307c4c1 bellard
                    has_option = 1;
3012 9307c4c1 bellard
                }
3013 f7188bbe Luiz Capitulino
                qdict_put(qdict, key, qint_from_int(has_option));
3014 9307c4c1 bellard
            }
3015 9307c4c1 bellard
            break;
3016 9307c4c1 bellard
        default:
3017 9307c4c1 bellard
        bad_type:
3018 376253ec aliguori
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3019 9307c4c1 bellard
            goto fail;
3020 9307c4c1 bellard
        }
3021 4d76d2ba Luiz Capitulino
        qemu_free(key);
3022 4d76d2ba Luiz Capitulino
        key = NULL;
3023 9dc39cba bellard
    }
3024 9307c4c1 bellard
    /* check that all arguments were parsed */
3025 cd390083 blueswir1
    while (qemu_isspace(*p))
3026 9307c4c1 bellard
        p++;
3027 9307c4c1 bellard
    if (*p != '\0') {
3028 376253ec aliguori
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3029 376253ec aliguori
                       cmdname);
3030 9307c4c1 bellard
        goto fail;
3031 9dc39cba bellard
    }
3032 9307c4c1 bellard
3033 55f81d96 Luiz Capitulino
    return cmd;
3034 ac7531ec Gerd Hoffmann
3035 55f81d96 Luiz Capitulino
fail:
3036 4d76d2ba Luiz Capitulino
    qemu_free(key);
3037 55f81d96 Luiz Capitulino
    return NULL;
3038 55f81d96 Luiz Capitulino
}
3039 55f81d96 Luiz Capitulino
3040 55f81d96 Luiz Capitulino
static void monitor_handle_command(Monitor *mon, const char *cmdline)
3041 55f81d96 Luiz Capitulino
{
3042 55f81d96 Luiz Capitulino
    QDict *qdict;
3043 c227f099 Anthony Liguori
    const mon_cmd_t *cmd;
3044 55f81d96 Luiz Capitulino
3045 55f81d96 Luiz Capitulino
    qdict = qdict_new();
3046 55f81d96 Luiz Capitulino
3047 590fb3b7 Luiz Capitulino
    cmd = monitor_parse_command(mon, cmdline, qdict);
3048 13917bee Luiz Capitulino
    if (!cmd)
3049 13917bee Luiz Capitulino
        goto out;
3050 13917bee Luiz Capitulino
3051 13917bee Luiz Capitulino
    qemu_errors_to_mon(mon);
3052 13917bee Luiz Capitulino
3053 13917bee Luiz Capitulino
    if (monitor_handler_ported(cmd)) {
3054 13917bee Luiz Capitulino
        QObject *data = NULL;
3055 13917bee Luiz Capitulino
3056 13917bee Luiz Capitulino
        cmd->mhandler.cmd_new(mon, qdict, &data);
3057 13917bee Luiz Capitulino
        if (data)
3058 13917bee Luiz Capitulino
            cmd->user_print(mon, data);
3059 13917bee Luiz Capitulino
3060 13917bee Luiz Capitulino
        qobject_decref(data);
3061 13917bee Luiz Capitulino
    } else {
3062 af4ce882 Luiz Capitulino
        cmd->mhandler.cmd(mon, qdict);
3063 55f81d96 Luiz Capitulino
    }
3064 55f81d96 Luiz Capitulino
3065 13917bee Luiz Capitulino
   qemu_errors_to_previous();
3066 13917bee Luiz Capitulino
3067 13917bee Luiz Capitulino
out:
3068 f7188bbe Luiz Capitulino
    QDECREF(qdict);
3069 9dc39cba bellard
}
3070 9dc39cba bellard
3071 81d0912d bellard
static void cmd_completion(const char *name, const char *list)
3072 81d0912d bellard
{
3073 81d0912d bellard
    const char *p, *pstart;
3074 81d0912d bellard
    char cmd[128];
3075 81d0912d bellard
    int len;
3076 81d0912d bellard
3077 81d0912d bellard
    p = list;
3078 81d0912d bellard
    for(;;) {
3079 81d0912d bellard
        pstart = p;
3080 81d0912d bellard
        p = strchr(p, '|');
3081 81d0912d bellard
        if (!p)
3082 81d0912d bellard
            p = pstart + strlen(pstart);
3083 81d0912d bellard
        len = p - pstart;
3084 81d0912d bellard
        if (len > sizeof(cmd) - 2)
3085 81d0912d bellard
            len = sizeof(cmd) - 2;
3086 81d0912d bellard
        memcpy(cmd, pstart, len);
3087 81d0912d bellard
        cmd[len] = '\0';
3088 81d0912d bellard
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3089 731b0364 aliguori
            readline_add_completion(cur_mon->rs, cmd);
3090 81d0912d bellard
        }
3091 81d0912d bellard
        if (*p == '\0')
3092 81d0912d bellard
            break;
3093 81d0912d bellard
        p++;
3094 81d0912d bellard
    }
3095 81d0912d bellard
}
3096 81d0912d bellard
3097 81d0912d bellard
static void file_completion(const char *input)
3098 81d0912d bellard
{
3099 81d0912d bellard
    DIR *ffs;
3100 81d0912d bellard
    struct dirent *d;
3101 81d0912d bellard
    char path[1024];
3102 81d0912d bellard
    char file[1024], file_prefix[1024];
3103 81d0912d bellard
    int input_path_len;
3104 81d0912d bellard
    const char *p;
3105 81d0912d bellard
3106 5fafdf24 ths
    p = strrchr(input, '/');
3107 81d0912d bellard
    if (!p) {
3108 81d0912d bellard
        input_path_len = 0;
3109 81d0912d bellard
        pstrcpy(file_prefix, sizeof(file_prefix), input);
3110 363a37d5 blueswir1
        pstrcpy(path, sizeof(path), ".");
3111 81d0912d bellard
    } else {
3112 81d0912d bellard
        input_path_len = p - input + 1;
3113 81d0912d bellard
        memcpy(path, input, input_path_len);
3114 81d0912d bellard
        if (input_path_len > sizeof(path) - 1)
3115 81d0912d bellard
            input_path_len = sizeof(path) - 1;
3116 81d0912d bellard
        path[input_path_len] = '\0';
3117 81d0912d bellard
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3118 81d0912d bellard
    }
3119 81d0912d bellard
#ifdef DEBUG_COMPLETION
3120 376253ec aliguori
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3121 376253ec aliguori
                   input, path, file_prefix);
3122 81d0912d bellard
#endif
3123 81d0912d bellard
    ffs = opendir(path);
3124 81d0912d bellard
    if (!ffs)
3125 81d0912d bellard
        return;
3126 81d0912d bellard
    for(;;) {
3127 81d0912d bellard
        struct stat sb;
3128 81d0912d bellard
        d = readdir(ffs);
3129 81d0912d bellard
        if (!d)
3130 81d0912d bellard
            break;
3131 81d0912d bellard
        if (strstart(d->d_name, file_prefix, NULL)) {
3132 81d0912d bellard
            memcpy(file, input, input_path_len);
3133 363a37d5 blueswir1
            if (input_path_len < sizeof(file))
3134 363a37d5 blueswir1
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3135 363a37d5 blueswir1
                        d->d_name);
3136 81d0912d bellard
            /* stat the file to find out if it's a directory.
3137 81d0912d bellard
             * In that case add a slash to speed up typing long paths
3138 81d0912d bellard
             */
3139 81d0912d bellard
            stat(file, &sb);
3140 81d0912d bellard
            if(S_ISDIR(sb.st_mode))
3141 363a37d5 blueswir1
                pstrcat(file, sizeof(file), "/");
3142 731b0364 aliguori
            readline_add_completion(cur_mon->rs, file);
3143 81d0912d bellard
        }
3144 81d0912d bellard
    }
3145 81d0912d bellard
    closedir(ffs);
3146 81d0912d bellard
}
3147 81d0912d bellard
3148 51de9760 aliguori
static void block_completion_it(void *opaque, BlockDriverState *bs)
3149 81d0912d bellard
{
3150 51de9760 aliguori
    const char *name = bdrv_get_device_name(bs);
3151 81d0912d bellard
    const char *input = opaque;
3152 81d0912d bellard
3153 81d0912d bellard
    if (input[0] == '\0' ||
3154 81d0912d bellard
        !strncmp(name, (char *)input, strlen(input))) {
3155 731b0364 aliguori
        readline_add_completion(cur_mon->rs, name);
3156 81d0912d bellard
    }
3157 81d0912d bellard
}
3158 81d0912d bellard
3159 81d0912d bellard
/* NOTE: this parser is an approximate form of the real command parser */
3160 81d0912d bellard
static void parse_cmdline(const char *cmdline,
3161 81d0912d bellard
                         int *pnb_args, char **args)
3162 81d0912d bellard
{
3163 81d0912d bellard
    const char *p;
3164 81d0912d bellard
    int nb_args, ret;
3165 81d0912d bellard
    char buf[1024];
3166 81d0912d bellard
3167 81d0912d bellard
    p = cmdline;
3168 81d0912d bellard
    nb_args = 0;
3169 81d0912d bellard
    for(;;) {
3170 cd390083 blueswir1
        while (qemu_isspace(*p))
3171 81d0912d bellard
            p++;
3172 81d0912d bellard
        if (*p == '\0')
3173 81d0912d bellard
            break;
3174 81d0912d bellard
        if (nb_args >= MAX_ARGS)
3175 81d0912d bellard
            break;
3176 81d0912d bellard
        ret = get_str(buf, sizeof(buf), &p);
3177 81d0912d bellard
        args[nb_args] = qemu_strdup(buf);
3178 81d0912d bellard
        nb_args++;
3179 81d0912d bellard
        if (ret < 0)
3180 81d0912d bellard
            break;
3181 81d0912d bellard
    }
3182 81d0912d bellard
    *pnb_args = nb_args;
3183 81d0912d bellard
}
3184 81d0912d bellard
3185 4d76d2ba Luiz Capitulino
static const char *next_arg_type(const char *typestr)
3186 4d76d2ba Luiz Capitulino
{
3187 4d76d2ba Luiz Capitulino
    const char *p = strchr(typestr, ':');
3188 4d76d2ba Luiz Capitulino
    return (p != NULL ? ++p : typestr);
3189 4d76d2ba Luiz Capitulino
}
3190 4d76d2ba Luiz Capitulino
3191 4c36ba32 aliguori
static void monitor_find_completion(const char *cmdline)
3192 81d0912d bellard
{
3193 81d0912d bellard
    const char *cmdname;
3194 81d0912d bellard
    char *args[MAX_ARGS];
3195 81d0912d bellard
    int nb_args, i, len;
3196 81d0912d bellard
    const char *ptype, *str;
3197 c227f099 Anthony Liguori
    const mon_cmd_t *cmd;
3198 64866c3d bellard
    const KeyDef *key;
3199 81d0912d bellard
3200 81d0912d bellard
    parse_cmdline(cmdline, &nb_args, args);
3201 81d0912d bellard
#ifdef DEBUG_COMPLETION
3202 81d0912d bellard
    for(i = 0; i < nb_args; i++) {
3203 376253ec aliguori
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3204 81d0912d bellard
    }
3205 81d0912d bellard
#endif
3206 81d0912d bellard
3207 81d0912d bellard
    /* if the line ends with a space, it means we want to complete the
3208 81d0912d bellard
       next arg */
3209 81d0912d bellard
    len = strlen(cmdline);
3210 cd390083 blueswir1
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3211 81d0912d bellard
        if (nb_args >= MAX_ARGS)
3212 81d0912d bellard
            return;
3213 81d0912d bellard
        args[nb_args++] = qemu_strdup("");
3214 81d0912d bellard
    }
3215 81d0912d bellard
    if (nb_args <= 1) {
3216 81d0912d bellard
        /* command completion */
3217 81d0912d bellard
        if (nb_args == 0)
3218 81d0912d bellard
            cmdname = "";
3219 81d0912d bellard
        else
3220 81d0912d bellard
            cmdname = args[0];
3221 731b0364 aliguori
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3222 376253ec aliguori
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3223 81d0912d bellard
            cmd_completion(cmdname, cmd->name);
3224 81d0912d bellard
        }
3225 81d0912d bellard
    } else {
3226 81d0912d bellard
        /* find the command */
3227 376253ec aliguori
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3228 81d0912d bellard
            if (compare_cmd(args[0], cmd->name))
3229 81d0912d bellard
                goto found;
3230 81d0912d bellard
        }
3231 81d0912d bellard
        return;
3232 81d0912d bellard
    found:
3233 4d76d2ba Luiz Capitulino
        ptype = next_arg_type(cmd->args_type);
3234 81d0912d bellard
        for(i = 0; i < nb_args - 2; i++) {
3235 81d0912d bellard
            if (*ptype != '\0') {
3236 4d76d2ba Luiz Capitulino
                ptype = next_arg_type(ptype);
3237 81d0912d bellard
                while (*ptype == '?')
3238 4d76d2ba Luiz Capitulino
                    ptype = next_arg_type(ptype);
3239 81d0912d bellard
            }
3240 81d0912d bellard
        }
3241 81d0912d bellard
        str = args[nb_args - 1];
3242 2a1704a7 Blue Swirl
        if (*ptype == '-' && ptype[1] != '\0') {
3243 2a1704a7 Blue Swirl
            ptype += 2;
3244 2a1704a7 Blue Swirl
        }
3245 81d0912d bellard
        switch(*ptype) {
3246 81d0912d bellard
        case 'F':
3247 81d0912d bellard
            /* file completion */
3248 731b0364 aliguori
            readline_set_completion_index(cur_mon->rs, strlen(str));
3249 81d0912d bellard
            file_completion(str);
3250 81d0912d bellard
            break;
3251 81d0912d bellard
        case 'B':
3252 81d0912d bellard
            /* block device name completion */
3253 731b0364 aliguori
            readline_set_completion_index(cur_mon->rs, strlen(str));
3254 81d0912d bellard
            bdrv_iterate(block_completion_it, (void *)str);
3255 81d0912d bellard
            break;
3256 7fe48483 bellard
        case 's':
3257 7fe48483 bellard
            /* XXX: more generic ? */
3258 7fe48483 bellard
            if (!strcmp(cmd->name, "info")) {
3259 731b0364 aliguori
                readline_set_completion_index(cur_mon->rs, strlen(str));
3260 7fe48483 bellard
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3261 7fe48483 bellard
                    cmd_completion(str, cmd->name);
3262 7fe48483 bellard
                }
3263 64866c3d bellard
            } else if (!strcmp(cmd->name, "sendkey")) {
3264 e600d1ef blueswir1
                char *sep = strrchr(str, '-');
3265 e600d1ef blueswir1
                if (sep)
3266 e600d1ef blueswir1
                    str = sep + 1;
3267 731b0364 aliguori
                readline_set_completion_index(cur_mon->rs, strlen(str));
3268 64866c3d bellard
                for(key = key_defs; key->name != NULL; key++) {
3269 64866c3d bellard
                    cmd_completion(str, key->name);
3270 64866c3d bellard
                }
3271 f3353c6b Jan Kiszka
            } else if (!strcmp(cmd->name, "help|?")) {
3272 f3353c6b Jan Kiszka
                readline_set_completion_index(cur_mon->rs, strlen(str));
3273 f3353c6b Jan Kiszka
                for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3274 f3353c6b Jan Kiszka
                    cmd_completion(str, cmd->name);
3275 f3353c6b Jan Kiszka
                }
3276 7fe48483 bellard
            }
3277 7fe48483 bellard
            break;
3278 81d0912d bellard
        default:
3279 81d0912d bellard
            break;
3280 81d0912d bellard
        }
3281 81d0912d bellard
    }
3282 81d0912d bellard
    for(i = 0; i < nb_args; i++)
3283 81d0912d bellard
        qemu_free(args[i]);
3284 81d0912d bellard
}
3285 81d0912d bellard
3286 731b0364 aliguori
static int monitor_can_read(void *opaque)
3287 9dc39cba bellard
{
3288 731b0364 aliguori
    Monitor *mon = opaque;
3289 731b0364 aliguori
3290 731b0364 aliguori
    return (mon->suspend_cnt == 0) ? 128 : 0;
3291 9dc39cba bellard
}
3292 9dc39cba bellard
3293 731b0364 aliguori
static void monitor_read(void *opaque, const uint8_t *buf, int size)
3294 9dc39cba bellard
{
3295 731b0364 aliguori
    Monitor *old_mon = cur_mon;
3296 7e2515e8 bellard
    int i;
3297 376253ec aliguori
3298 731b0364 aliguori
    cur_mon = opaque;
3299 731b0364 aliguori
3300 cde76ee1 aliguori
    if (cur_mon->rs) {
3301 cde76ee1 aliguori
        for (i = 0; i < size; i++)
3302 cde76ee1 aliguori
            readline_handle_byte(cur_mon->rs, buf[i]);
3303 cde76ee1 aliguori
    } else {
3304 cde76ee1 aliguori
        if (size == 0 || buf[size - 1] != 0)
3305 cde76ee1 aliguori
            monitor_printf(cur_mon, "corrupted command\n");
3306 cde76ee1 aliguori
        else
3307 cde76ee1 aliguori
            monitor_handle_command(cur_mon, (char *)buf);
3308 cde76ee1 aliguori
    }
3309 9dc39cba bellard
3310 731b0364 aliguori
    cur_mon = old_mon;
3311 731b0364 aliguori
}
3312 d8f44609 aliguori
3313 376253ec aliguori
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3314 aa455485 bellard
{
3315 731b0364 aliguori
    monitor_suspend(mon);
3316 376253ec aliguori
    monitor_handle_command(mon, cmdline);
3317 731b0364 aliguori
    monitor_resume(mon);
3318 d8f44609 aliguori
}
3319 d8f44609 aliguori
3320 cde76ee1 aliguori
int monitor_suspend(Monitor *mon)
3321 d8f44609 aliguori
{
3322 cde76ee1 aliguori
    if (!mon->rs)
3323 cde76ee1 aliguori
        return -ENOTTY;
3324 731b0364 aliguori
    mon->suspend_cnt++;
3325 cde76ee1 aliguori
    return 0;
3326 d8f44609 aliguori
}
3327 d8f44609 aliguori
3328 376253ec aliguori
void monitor_resume(Monitor *mon)
3329 d8f44609 aliguori
{
3330 cde76ee1 aliguori
    if (!mon->rs)
3331 cde76ee1 aliguori
        return;
3332 731b0364 aliguori
    if (--mon->suspend_cnt == 0)
3333 731b0364 aliguori
        readline_show_prompt(mon->rs);
3334 aa455485 bellard
}
3335 aa455485 bellard
3336 731b0364 aliguori
static void monitor_event(void *opaque, int event)
3337 86e94dea ths
{
3338 376253ec aliguori
    Monitor *mon = opaque;
3339 376253ec aliguori
3340 2724b180 aliguori
    switch (event) {
3341 2724b180 aliguori
    case CHR_EVENT_MUX_IN:
3342 a7aec5da Gerd Hoffmann
        mon->mux_out = 0;
3343 a7aec5da Gerd Hoffmann
        if (mon->reset_seen) {
3344 a7aec5da Gerd Hoffmann
            readline_restart(mon->rs);
3345 a7aec5da Gerd Hoffmann
            monitor_resume(mon);
3346 a7aec5da Gerd Hoffmann
            monitor_flush(mon);
3347 a7aec5da Gerd Hoffmann
        } else {
3348 a7aec5da Gerd Hoffmann
            mon->suspend_cnt = 0;
3349 a7aec5da Gerd Hoffmann
        }
3350 2724b180 aliguori
        break;
3351 2724b180 aliguori
3352 2724b180 aliguori
    case CHR_EVENT_MUX_OUT:
3353 a7aec5da Gerd Hoffmann
        if (mon->reset_seen) {
3354 a7aec5da Gerd Hoffmann
            if (mon->suspend_cnt == 0) {
3355 a7aec5da Gerd Hoffmann
                monitor_printf(mon, "\n");
3356 a7aec5da Gerd Hoffmann
            }
3357 a7aec5da Gerd Hoffmann
            monitor_flush(mon);
3358 a7aec5da Gerd Hoffmann
            monitor_suspend(mon);
3359 a7aec5da Gerd Hoffmann
        } else {
3360 a7aec5da Gerd Hoffmann
            mon->suspend_cnt++;
3361 a7aec5da Gerd Hoffmann
        }
3362 a7aec5da Gerd Hoffmann
        mon->mux_out = 1;
3363 2724b180 aliguori
        break;
3364 86e94dea ths
3365 2724b180 aliguori
    case CHR_EVENT_RESET:
3366 2724b180 aliguori
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3367 2724b180 aliguori
                       "information\n", QEMU_VERSION);
3368 a7aec5da Gerd Hoffmann
        if (!mon->mux_out) {
3369 2724b180 aliguori
            readline_show_prompt(mon->rs);
3370 a7aec5da Gerd Hoffmann
        }
3371 a7aec5da Gerd Hoffmann
        mon->reset_seen = 1;
3372 2724b180 aliguori
        break;
3373 2724b180 aliguori
    }
3374 86e94dea ths
}
3375 86e94dea ths
3376 76655d6d aliguori
3377 76655d6d aliguori
/*
3378 76655d6d aliguori
 * Local variables:
3379 76655d6d aliguori
 *  c-indent-level: 4
3380 76655d6d aliguori
 *  c-basic-offset: 4
3381 76655d6d aliguori
 *  tab-width: 8
3382 76655d6d aliguori
 * End:
3383 76655d6d aliguori
 */
3384 76655d6d aliguori
3385 731b0364 aliguori
void monitor_init(CharDriverState *chr, int flags)
3386 aa455485 bellard
{
3387 731b0364 aliguori
    static int is_first_init = 1;
3388 87127161 aliguori
    Monitor *mon;
3389 20d8a3ed ths
3390 20d8a3ed ths
    if (is_first_init) {
3391 c8256f9d balrog
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3392 20d8a3ed ths
        is_first_init = 0;
3393 20d8a3ed ths
    }
3394 87127161 aliguori
3395 87127161 aliguori
    mon = qemu_mallocz(sizeof(*mon));
3396 20d8a3ed ths
3397 87127161 aliguori
    mon->chr = chr;
3398 731b0364 aliguori
    mon->flags = flags;
3399 cde76ee1 aliguori
    if (flags & MONITOR_USE_READLINE) {
3400 cde76ee1 aliguori
        mon->rs = readline_init(mon, monitor_find_completion);
3401 cde76ee1 aliguori
        monitor_read_command(mon, 0);
3402 cde76ee1 aliguori
    }
3403 87127161 aliguori
3404 731b0364 aliguori
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3405 731b0364 aliguori
                          mon);
3406 87127161 aliguori
3407 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&mon_list, mon, entry);
3408 731b0364 aliguori
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3409 87127161 aliguori
        cur_mon = mon;
3410 aa455485 bellard
}
3411 aa455485 bellard
3412 376253ec aliguori
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3413 81d0912d bellard
{
3414 bb5fc20f aliguori
    BlockDriverState *bs = opaque;
3415 bb5fc20f aliguori
    int ret = 0;
3416 81d0912d bellard
3417 bb5fc20f aliguori
    if (bdrv_set_key(bs, password) != 0) {
3418 376253ec aliguori
        monitor_printf(mon, "invalid password\n");
3419 bb5fc20f aliguori
        ret = -EPERM;
3420 9dc39cba bellard
    }
3421 731b0364 aliguori
    if (mon->password_completion_cb)
3422 731b0364 aliguori
        mon->password_completion_cb(mon->password_opaque, ret);
3423 bb5fc20f aliguori
3424 731b0364 aliguori
    monitor_read_command(mon, 1);
3425 9dc39cba bellard
}
3426 c0f4ce77 aliguori
3427 376253ec aliguori
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3428 bb5fc20f aliguori
                                 BlockDriverCompletionFunc *completion_cb,
3429 bb5fc20f aliguori
                                 void *opaque)
3430 c0f4ce77 aliguori
{
3431 cde76ee1 aliguori
    int err;
3432 cde76ee1 aliguori
3433 bb5fc20f aliguori
    if (!bdrv_key_required(bs)) {
3434 bb5fc20f aliguori
        if (completion_cb)
3435 bb5fc20f aliguori
            completion_cb(opaque, 0);
3436 bb5fc20f aliguori
        return;
3437 bb5fc20f aliguori
    }
3438 c0f4ce77 aliguori
3439 376253ec aliguori
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3440 376253ec aliguori
                   bdrv_get_encrypted_filename(bs));
3441 bb5fc20f aliguori
3442 731b0364 aliguori
    mon->password_completion_cb = completion_cb;
3443 731b0364 aliguori
    mon->password_opaque = opaque;
3444 bb5fc20f aliguori
3445 cde76ee1 aliguori
    err = monitor_read_password(mon, bdrv_password_cb, bs);
3446 cde76ee1 aliguori
3447 cde76ee1 aliguori
    if (err && completion_cb)
3448 cde76ee1 aliguori
        completion_cb(opaque, err);
3449 c0f4ce77 aliguori
}
3450 ac7531ec Gerd Hoffmann
3451 ac7531ec Gerd Hoffmann
typedef struct QemuErrorSink QemuErrorSink;
3452 ac7531ec Gerd Hoffmann
struct QemuErrorSink {
3453 ac7531ec Gerd Hoffmann
    enum {
3454 ac7531ec Gerd Hoffmann
        ERR_SINK_FILE,
3455 ac7531ec Gerd Hoffmann
        ERR_SINK_MONITOR,
3456 ac7531ec Gerd Hoffmann
    } dest;
3457 ac7531ec Gerd Hoffmann
    union {
3458 ac7531ec Gerd Hoffmann
        FILE    *fp;
3459 ac7531ec Gerd Hoffmann
        Monitor *mon;
3460 ac7531ec Gerd Hoffmann
    };
3461 ac7531ec Gerd Hoffmann
    QemuErrorSink *previous;
3462 ac7531ec Gerd Hoffmann
};
3463 ac7531ec Gerd Hoffmann
3464 528e93a9 Blue Swirl
static QemuErrorSink *qemu_error_sink;
3465 ac7531ec Gerd Hoffmann
3466 ac7531ec Gerd Hoffmann
void qemu_errors_to_file(FILE *fp)
3467 ac7531ec Gerd Hoffmann
{
3468 ac7531ec Gerd Hoffmann
    QemuErrorSink *sink;
3469 ac7531ec Gerd Hoffmann
3470 ac7531ec Gerd Hoffmann
    sink = qemu_mallocz(sizeof(*sink));
3471 ac7531ec Gerd Hoffmann
    sink->dest = ERR_SINK_FILE;
3472 ac7531ec Gerd Hoffmann
    sink->fp = fp;
3473 ac7531ec Gerd Hoffmann
    sink->previous = qemu_error_sink;
3474 ac7531ec Gerd Hoffmann
    qemu_error_sink = sink;
3475 ac7531ec Gerd Hoffmann
}
3476 ac7531ec Gerd Hoffmann
3477 ac7531ec Gerd Hoffmann
void qemu_errors_to_mon(Monitor *mon)
3478 ac7531ec Gerd Hoffmann
{
3479 ac7531ec Gerd Hoffmann
    QemuErrorSink *sink;
3480 ac7531ec Gerd Hoffmann
3481 ac7531ec Gerd Hoffmann
    sink = qemu_mallocz(sizeof(*sink));
3482 ac7531ec Gerd Hoffmann
    sink->dest = ERR_SINK_MONITOR;
3483 ac7531ec Gerd Hoffmann
    sink->mon = mon;
3484 ac7531ec Gerd Hoffmann
    sink->previous = qemu_error_sink;
3485 ac7531ec Gerd Hoffmann
    qemu_error_sink = sink;
3486 ac7531ec Gerd Hoffmann
}
3487 ac7531ec Gerd Hoffmann
3488 ac7531ec Gerd Hoffmann
void qemu_errors_to_previous(void)
3489 ac7531ec Gerd Hoffmann
{
3490 ac7531ec Gerd Hoffmann
    QemuErrorSink *sink;
3491 ac7531ec Gerd Hoffmann
3492 ac7531ec Gerd Hoffmann
    assert(qemu_error_sink != NULL);
3493 ac7531ec Gerd Hoffmann
    sink = qemu_error_sink;
3494 ac7531ec Gerd Hoffmann
    qemu_error_sink = sink->previous;
3495 ac7531ec Gerd Hoffmann
    qemu_free(sink);
3496 ac7531ec Gerd Hoffmann
}
3497 ac7531ec Gerd Hoffmann
3498 ac7531ec Gerd Hoffmann
void qemu_error(const char *fmt, ...)
3499 ac7531ec Gerd Hoffmann
{
3500 ac7531ec Gerd Hoffmann
    va_list args;
3501 ac7531ec Gerd Hoffmann
3502 ac7531ec Gerd Hoffmann
    assert(qemu_error_sink != NULL);
3503 ac7531ec Gerd Hoffmann
    switch (qemu_error_sink->dest) {
3504 ac7531ec Gerd Hoffmann
    case ERR_SINK_FILE:
3505 ac7531ec Gerd Hoffmann
        va_start(args, fmt);
3506 ac7531ec Gerd Hoffmann
        vfprintf(qemu_error_sink->fp, fmt, args);
3507 ac7531ec Gerd Hoffmann
        va_end(args);
3508 ac7531ec Gerd Hoffmann
        break;
3509 ac7531ec Gerd Hoffmann
    case ERR_SINK_MONITOR:
3510 ac7531ec Gerd Hoffmann
        va_start(args, fmt);
3511 ac7531ec Gerd Hoffmann
        monitor_vprintf(qemu_error_sink->mon, fmt, args);
3512 ac7531ec Gerd Hoffmann
        va_end(args);
3513 ac7531ec Gerd Hoffmann
        break;
3514 ac7531ec Gerd Hoffmann
    }
3515 ac7531ec Gerd Hoffmann
}