Statistics
| Branch: | Revision:

root / monitor.c @ 94a943ef

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