Statistics
| Branch: | Revision:

root / monitor.c @ 751c6a17

History | View | Annotate | Download (87.1 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 751c6a17 Gerd Hoffmann
            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 d154615d aliguori
    cpu_synchronize_state(cur_mon->mon_cpu, 0);
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 d154615d aliguori
        cpu_synchronize_state(env, 0);
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_kqemu(Monitor *mon)
1394 0f4c6415 bellard
{
1395 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
1396 6a00d601 bellard
    CPUState *env;
1397 0f4c6415 bellard
    int val;
1398 0f4c6415 bellard
    val = 0;
1399 6a00d601 bellard
    env = mon_get_cpu();
1400 6a00d601 bellard
    if (!env) {
1401 376253ec aliguori
        monitor_printf(mon, "No cpu initialized yet");
1402 6a00d601 bellard
        return;
1403 6a00d601 bellard
    }
1404 6a00d601 bellard
    val = env->kqemu_enabled;
1405 376253ec aliguori
    monitor_printf(mon, "kqemu support: ");
1406 5f1ce948 bellard
    switch(val) {
1407 5f1ce948 bellard
    default:
1408 5f1ce948 bellard
    case 0:
1409 376253ec aliguori
        monitor_printf(mon, "disabled\n");
1410 5f1ce948 bellard
        break;
1411 5f1ce948 bellard
    case 1:
1412 376253ec aliguori
        monitor_printf(mon, "enabled for user code\n");
1413 5f1ce948 bellard
        break;
1414 5f1ce948 bellard
    case 2:
1415 376253ec aliguori
        monitor_printf(mon, "enabled for user and kernel code\n");
1416 5f1ce948 bellard
        break;
1417 5f1ce948 bellard
    }
1418 0f4c6415 bellard
#else
1419 376253ec aliguori
    monitor_printf(mon, "kqemu support: not compiled\n");
1420 0f4c6415 bellard
#endif
1421 5fafdf24 ths
}
1422 0f4c6415 bellard
1423 376253ec aliguori
static void do_info_kvm(Monitor *mon)
1424 7ba1e619 aliguori
{
1425 7ba1e619 aliguori
#ifdef CONFIG_KVM
1426 376253ec aliguori
    monitor_printf(mon, "kvm support: ");
1427 7ba1e619 aliguori
    if (kvm_enabled())
1428 376253ec aliguori
        monitor_printf(mon, "enabled\n");
1429 7ba1e619 aliguori
    else
1430 376253ec aliguori
        monitor_printf(mon, "disabled\n");
1431 7ba1e619 aliguori
#else
1432 376253ec aliguori
    monitor_printf(mon, "kvm support: not compiled\n");
1433 7ba1e619 aliguori
#endif
1434 7ba1e619 aliguori
}
1435 7ba1e619 aliguori
1436 030ea37b aliguori
static void do_info_numa(Monitor *mon)
1437 030ea37b aliguori
{
1438 b28b6230 aliguori
    int i;
1439 030ea37b aliguori
    CPUState *env;
1440 030ea37b aliguori
1441 030ea37b aliguori
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1442 030ea37b aliguori
    for (i = 0; i < nb_numa_nodes; i++) {
1443 030ea37b aliguori
        monitor_printf(mon, "node %d cpus:", i);
1444 030ea37b aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1445 030ea37b aliguori
            if (env->numa_node == i) {
1446 030ea37b aliguori
                monitor_printf(mon, " %d", env->cpu_index);
1447 030ea37b aliguori
            }
1448 030ea37b aliguori
        }
1449 030ea37b aliguori
        monitor_printf(mon, "\n");
1450 030ea37b aliguori
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1451 030ea37b aliguori
            node_mem[i] >> 20);
1452 030ea37b aliguori
    }
1453 030ea37b aliguori
}
1454 030ea37b aliguori
1455 5f1ce948 bellard
#ifdef CONFIG_PROFILER
1456 5f1ce948 bellard
1457 5f1ce948 bellard
int64_t kqemu_time;
1458 5f1ce948 bellard
int64_t qemu_time;
1459 5f1ce948 bellard
int64_t kqemu_exec_count;
1460 5f1ce948 bellard
int64_t dev_time;
1461 5f1ce948 bellard
int64_t kqemu_ret_int_count;
1462 5f1ce948 bellard
int64_t kqemu_ret_excp_count;
1463 5f1ce948 bellard
int64_t kqemu_ret_intr_count;
1464 5f1ce948 bellard
1465 376253ec aliguori
static void do_info_profile(Monitor *mon)
1466 5f1ce948 bellard
{
1467 5f1ce948 bellard
    int64_t total;
1468 5f1ce948 bellard
    total = qemu_time;
1469 5f1ce948 bellard
    if (total == 0)
1470 5f1ce948 bellard
        total = 1;
1471 376253ec aliguori
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1472 376253ec aliguori
                   dev_time, dev_time / (double)ticks_per_sec);
1473 376253ec aliguori
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1474 376253ec aliguori
                   qemu_time, qemu_time / (double)ticks_per_sec);
1475 376253ec aliguori
    monitor_printf(mon, "kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%"
1476 376253ec aliguori
                        PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1477 376253ec aliguori
                        PRId64 "\n",
1478 376253ec aliguori
                   kqemu_time, kqemu_time / (double)ticks_per_sec,
1479 376253ec aliguori
                   kqemu_time / (double)total * 100.0,
1480 376253ec aliguori
                   kqemu_exec_count,
1481 376253ec aliguori
                   kqemu_ret_int_count,
1482 376253ec aliguori
                   kqemu_ret_excp_count,
1483 376253ec aliguori
                   kqemu_ret_intr_count);
1484 5f1ce948 bellard
    qemu_time = 0;
1485 5f1ce948 bellard
    kqemu_time = 0;
1486 5f1ce948 bellard
    kqemu_exec_count = 0;
1487 5f1ce948 bellard
    dev_time = 0;
1488 5f1ce948 bellard
    kqemu_ret_int_count = 0;
1489 5f1ce948 bellard
    kqemu_ret_excp_count = 0;
1490 5f1ce948 bellard
    kqemu_ret_intr_count = 0;
1491 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
1492 5f1ce948 bellard
    kqemu_record_dump();
1493 5f1ce948 bellard
#endif
1494 5f1ce948 bellard
}
1495 5f1ce948 bellard
#else
1496 376253ec aliguori
static void do_info_profile(Monitor *mon)
1497 5f1ce948 bellard
{
1498 376253ec aliguori
    monitor_printf(mon, "Internal profiler not compiled\n");
1499 5f1ce948 bellard
}
1500 5f1ce948 bellard
#endif
1501 5f1ce948 bellard
1502 ec36b695 bellard
/* Capture support */
1503 ec36b695 bellard
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1504 ec36b695 bellard
1505 376253ec aliguori
static void do_info_capture(Monitor *mon)
1506 ec36b695 bellard
{
1507 ec36b695 bellard
    int i;
1508 ec36b695 bellard
    CaptureState *s;
1509 ec36b695 bellard
1510 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1511 376253ec aliguori
        monitor_printf(mon, "[%d]: ", i);
1512 ec36b695 bellard
        s->ops.info (s->opaque);
1513 ec36b695 bellard
    }
1514 ec36b695 bellard
}
1515 ec36b695 bellard
1516 2313086a Blue Swirl
#ifdef HAS_AUDIO
1517 376253ec aliguori
static void do_stop_capture(Monitor *mon, int n)
1518 ec36b695 bellard
{
1519 ec36b695 bellard
    int i;
1520 ec36b695 bellard
    CaptureState *s;
1521 ec36b695 bellard
1522 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1523 ec36b695 bellard
        if (i == n) {
1524 ec36b695 bellard
            s->ops.destroy (s->opaque);
1525 ec36b695 bellard
            LIST_REMOVE (s, entries);
1526 ec36b695 bellard
            qemu_free (s);
1527 ec36b695 bellard
            return;
1528 ec36b695 bellard
        }
1529 ec36b695 bellard
    }
1530 ec36b695 bellard
}
1531 ec36b695 bellard
1532 376253ec aliguori
static void do_wav_capture(Monitor *mon, const char *path,
1533 376253ec aliguori
                           int has_freq, int freq,
1534 376253ec aliguori
                           int has_bits, int bits,
1535 376253ec aliguori
                           int has_channels, int nchannels)
1536 ec36b695 bellard
{
1537 ec36b695 bellard
    CaptureState *s;
1538 ec36b695 bellard
1539 ec36b695 bellard
    s = qemu_mallocz (sizeof (*s));
1540 ec36b695 bellard
1541 ec36b695 bellard
    freq = has_freq ? freq : 44100;
1542 ec36b695 bellard
    bits = has_bits ? bits : 16;
1543 ec36b695 bellard
    nchannels = has_channels ? nchannels : 2;
1544 ec36b695 bellard
1545 ec36b695 bellard
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1546 376253ec aliguori
        monitor_printf(mon, "Faied to add wave capture\n");
1547 ec36b695 bellard
        qemu_free (s);
1548 ec36b695 bellard
    }
1549 ec36b695 bellard
    LIST_INSERT_HEAD (&capture_head, s, entries);
1550 ec36b695 bellard
}
1551 ec36b695 bellard
#endif
1552 ec36b695 bellard
1553 dc1c0b74 aurel32
#if defined(TARGET_I386)
1554 376253ec aliguori
static void do_inject_nmi(Monitor *mon, int cpu_index)
1555 dc1c0b74 aurel32
{
1556 dc1c0b74 aurel32
    CPUState *env;
1557 dc1c0b74 aurel32
1558 dc1c0b74 aurel32
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1559 dc1c0b74 aurel32
        if (env->cpu_index == cpu_index) {
1560 dc1c0b74 aurel32
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1561 dc1c0b74 aurel32
            break;
1562 dc1c0b74 aurel32
        }
1563 dc1c0b74 aurel32
}
1564 dc1c0b74 aurel32
#endif
1565 dc1c0b74 aurel32
1566 376253ec aliguori
static void do_info_status(Monitor *mon)
1567 6f9c5ee7 aurel32
{
1568 1b530a6d aurel32
    if (vm_running) {
1569 1b530a6d aurel32
        if (singlestep) {
1570 1b530a6d aurel32
            monitor_printf(mon, "VM status: running (single step mode)\n");
1571 1b530a6d aurel32
        } else {
1572 1b530a6d aurel32
            monitor_printf(mon, "VM status: running\n");
1573 1b530a6d aurel32
        }
1574 1b530a6d aurel32
    } else
1575 376253ec aliguori
       monitor_printf(mon, "VM status: paused\n");
1576 6f9c5ee7 aurel32
}
1577 6f9c5ee7 aurel32
1578 6f9c5ee7 aurel32
1579 376253ec aliguori
static void do_balloon(Monitor *mon, int value)
1580 df751fa8 aliguori
{
1581 df751fa8 aliguori
    ram_addr_t target = value;
1582 df751fa8 aliguori
    qemu_balloon(target << 20);
1583 df751fa8 aliguori
}
1584 df751fa8 aliguori
1585 376253ec aliguori
static void do_info_balloon(Monitor *mon)
1586 df751fa8 aliguori
{
1587 df751fa8 aliguori
    ram_addr_t actual;
1588 df751fa8 aliguori
1589 df751fa8 aliguori
    actual = qemu_balloon_status();
1590 bd322087 aliguori
    if (kvm_enabled() && !kvm_has_sync_mmu())
1591 376253ec aliguori
        monitor_printf(mon, "Using KVM without synchronous MMU, "
1592 376253ec aliguori
                       "ballooning disabled\n");
1593 bd322087 aliguori
    else if (actual == 0)
1594 376253ec aliguori
        monitor_printf(mon, "Ballooning not activated in VM\n");
1595 df751fa8 aliguori
    else
1596 376253ec aliguori
        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1597 df751fa8 aliguori
}
1598 df751fa8 aliguori
1599 15dfcd45 Jan Kiszka
static qemu_acl *find_acl(Monitor *mon, const char *name)
1600 76655d6d aliguori
{
1601 15dfcd45 Jan Kiszka
    qemu_acl *acl = qemu_acl_find(name);
1602 76655d6d aliguori
1603 76655d6d aliguori
    if (!acl) {
1604 15dfcd45 Jan Kiszka
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
1605 76655d6d aliguori
    }
1606 15dfcd45 Jan Kiszka
    return acl;
1607 15dfcd45 Jan Kiszka
}
1608 15dfcd45 Jan Kiszka
1609 15dfcd45 Jan Kiszka
static void do_acl_show(Monitor *mon, const char *aclname)
1610 15dfcd45 Jan Kiszka
{
1611 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1612 15dfcd45 Jan Kiszka
    qemu_acl_entry *entry;
1613 15dfcd45 Jan Kiszka
    int i = 0;
1614 76655d6d aliguori
1615 15dfcd45 Jan Kiszka
    if (acl) {
1616 28a76be8 aliguori
        monitor_printf(mon, "policy: %s\n",
1617 76655d6d aliguori
                       acl->defaultDeny ? "deny" : "allow");
1618 28a76be8 aliguori
        TAILQ_FOREACH(entry, &acl->entries, next) {
1619 28a76be8 aliguori
            i++;
1620 28a76be8 aliguori
            monitor_printf(mon, "%d: %s %s\n", i,
1621 15dfcd45 Jan Kiszka
                           entry->deny ? "deny" : "allow", entry->match);
1622 28a76be8 aliguori
        }
1623 15dfcd45 Jan Kiszka
    }
1624 15dfcd45 Jan Kiszka
}
1625 15dfcd45 Jan Kiszka
1626 15dfcd45 Jan Kiszka
static void do_acl_reset(Monitor *mon, const char *aclname)
1627 15dfcd45 Jan Kiszka
{
1628 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1629 15dfcd45 Jan Kiszka
1630 15dfcd45 Jan Kiszka
    if (acl) {
1631 28a76be8 aliguori
        qemu_acl_reset(acl);
1632 28a76be8 aliguori
        monitor_printf(mon, "acl: removed all rules\n");
1633 15dfcd45 Jan Kiszka
    }
1634 15dfcd45 Jan Kiszka
}
1635 15dfcd45 Jan Kiszka
1636 15dfcd45 Jan Kiszka
static void do_acl_policy(Monitor *mon, const char *aclname,
1637 15dfcd45 Jan Kiszka
                          const char *policy)
1638 15dfcd45 Jan Kiszka
{
1639 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1640 28a76be8 aliguori
1641 15dfcd45 Jan Kiszka
    if (acl) {
1642 15dfcd45 Jan Kiszka
        if (strcmp(policy, "allow") == 0) {
1643 28a76be8 aliguori
            acl->defaultDeny = 0;
1644 28a76be8 aliguori
            monitor_printf(mon, "acl: policy set to 'allow'\n");
1645 15dfcd45 Jan Kiszka
        } else if (strcmp(policy, "deny") == 0) {
1646 28a76be8 aliguori
            acl->defaultDeny = 1;
1647 28a76be8 aliguori
            monitor_printf(mon, "acl: policy set to 'deny'\n");
1648 28a76be8 aliguori
        } else {
1649 15dfcd45 Jan Kiszka
            monitor_printf(mon, "acl: unknown policy '%s', "
1650 15dfcd45 Jan Kiszka
                           "expected 'deny' or 'allow'\n", policy);
1651 28a76be8 aliguori
        }
1652 15dfcd45 Jan Kiszka
    }
1653 15dfcd45 Jan Kiszka
}
1654 28a76be8 aliguori
1655 15dfcd45 Jan Kiszka
static void do_acl_add(Monitor *mon, const char *aclname,
1656 15dfcd45 Jan Kiszka
                       const char *match, const char *policy,
1657 15dfcd45 Jan Kiszka
                       int has_index, int index)
1658 15dfcd45 Jan Kiszka
{
1659 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1660 15dfcd45 Jan Kiszka
    int deny, ret;
1661 15dfcd45 Jan Kiszka
1662 15dfcd45 Jan Kiszka
    if (acl) {
1663 15dfcd45 Jan Kiszka
        if (strcmp(policy, "allow") == 0) {
1664 15dfcd45 Jan Kiszka
            deny = 0;
1665 15dfcd45 Jan Kiszka
        } else if (strcmp(policy, "deny") == 0) {
1666 15dfcd45 Jan Kiszka
            deny = 1;
1667 15dfcd45 Jan Kiszka
        } else {
1668 15dfcd45 Jan Kiszka
            monitor_printf(mon, "acl: unknown policy '%s', "
1669 15dfcd45 Jan Kiszka
                           "expected 'deny' or 'allow'\n", policy);
1670 28a76be8 aliguori
            return;
1671 28a76be8 aliguori
        }
1672 28a76be8 aliguori
        if (has_index)
1673 28a76be8 aliguori
            ret = qemu_acl_insert(acl, deny, match, index);
1674 28a76be8 aliguori
        else
1675 28a76be8 aliguori
            ret = qemu_acl_append(acl, deny, match);
1676 28a76be8 aliguori
        if (ret < 0)
1677 28a76be8 aliguori
            monitor_printf(mon, "acl: unable to add acl entry\n");
1678 28a76be8 aliguori
        else
1679 28a76be8 aliguori
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
1680 15dfcd45 Jan Kiszka
    }
1681 15dfcd45 Jan Kiszka
}
1682 28a76be8 aliguori
1683 15dfcd45 Jan Kiszka
static void do_acl_remove(Monitor *mon, const char *aclname, const char *match)
1684 15dfcd45 Jan Kiszka
{
1685 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
1686 15dfcd45 Jan Kiszka
    int ret;
1687 28a76be8 aliguori
1688 15dfcd45 Jan Kiszka
    if (acl) {
1689 28a76be8 aliguori
        ret = qemu_acl_remove(acl, match);
1690 28a76be8 aliguori
        if (ret < 0)
1691 28a76be8 aliguori
            monitor_printf(mon, "acl: no matching acl entry\n");
1692 28a76be8 aliguori
        else
1693 28a76be8 aliguori
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1694 76655d6d aliguori
    }
1695 76655d6d aliguori
}
1696 76655d6d aliguori
1697 79c4f6b0 Huang Ying
#if defined(TARGET_I386)
1698 79c4f6b0 Huang Ying
static void do_inject_mce(Monitor *mon,
1699 79c4f6b0 Huang Ying
                          int cpu_index, int bank,
1700 79c4f6b0 Huang Ying
                          unsigned status_hi, unsigned status_lo,
1701 79c4f6b0 Huang Ying
                          unsigned mcg_status_hi, unsigned mcg_status_lo,
1702 79c4f6b0 Huang Ying
                          unsigned addr_hi, unsigned addr_lo,
1703 79c4f6b0 Huang Ying
                          unsigned misc_hi, unsigned misc_lo)
1704 79c4f6b0 Huang Ying
{
1705 79c4f6b0 Huang Ying
    CPUState *cenv;
1706 79c4f6b0 Huang Ying
    uint64_t status = ((uint64_t)status_hi << 32) | status_lo;
1707 79c4f6b0 Huang Ying
    uint64_t mcg_status = ((uint64_t)mcg_status_hi << 32) | mcg_status_lo;
1708 79c4f6b0 Huang Ying
    uint64_t addr = ((uint64_t)addr_hi << 32) | addr_lo;
1709 79c4f6b0 Huang Ying
    uint64_t misc = ((uint64_t)misc_hi << 32) | misc_lo;
1710 79c4f6b0 Huang Ying
1711 79c4f6b0 Huang Ying
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1712 79c4f6b0 Huang Ying
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1713 79c4f6b0 Huang Ying
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1714 79c4f6b0 Huang Ying
            break;
1715 79c4f6b0 Huang Ying
        }
1716 79c4f6b0 Huang Ying
}
1717 79c4f6b0 Huang Ying
#endif
1718 79c4f6b0 Huang Ying
1719 f07918fd Mark McLoughlin
static void do_getfd(Monitor *mon, const char *fdname)
1720 f07918fd Mark McLoughlin
{
1721 f07918fd Mark McLoughlin
    mon_fd_t *monfd;
1722 f07918fd Mark McLoughlin
    int fd;
1723 f07918fd Mark McLoughlin
1724 f07918fd Mark McLoughlin
    fd = qemu_chr_get_msgfd(mon->chr);
1725 f07918fd Mark McLoughlin
    if (fd == -1) {
1726 f07918fd Mark McLoughlin
        monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1727 f07918fd Mark McLoughlin
        return;
1728 f07918fd Mark McLoughlin
    }
1729 f07918fd Mark McLoughlin
1730 f07918fd Mark McLoughlin
    if (qemu_isdigit(fdname[0])) {
1731 f07918fd Mark McLoughlin
        monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1732 f07918fd Mark McLoughlin
        return;
1733 f07918fd Mark McLoughlin
    }
1734 f07918fd Mark McLoughlin
1735 f07918fd Mark McLoughlin
    fd = dup(fd);
1736 f07918fd Mark McLoughlin
    if (fd == -1) {
1737 f07918fd Mark McLoughlin
        monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1738 f07918fd Mark McLoughlin
                       strerror(errno));
1739 f07918fd Mark McLoughlin
        return;
1740 f07918fd Mark McLoughlin
    }
1741 f07918fd Mark McLoughlin
1742 f07918fd Mark McLoughlin
    LIST_FOREACH(monfd, &mon->fds, next) {
1743 f07918fd Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
1744 f07918fd Mark McLoughlin
            continue;
1745 f07918fd Mark McLoughlin
        }
1746 f07918fd Mark McLoughlin
1747 f07918fd Mark McLoughlin
        close(monfd->fd);
1748 f07918fd Mark McLoughlin
        monfd->fd = fd;
1749 f07918fd Mark McLoughlin
        return;
1750 f07918fd Mark McLoughlin
    }
1751 f07918fd Mark McLoughlin
1752 f07918fd Mark McLoughlin
    monfd = qemu_mallocz(sizeof(mon_fd_t));
1753 f07918fd Mark McLoughlin
    monfd->name = qemu_strdup(fdname);
1754 f07918fd Mark McLoughlin
    monfd->fd = fd;
1755 f07918fd Mark McLoughlin
1756 f07918fd Mark McLoughlin
    LIST_INSERT_HEAD(&mon->fds, monfd, next);
1757 f07918fd Mark McLoughlin
}
1758 f07918fd Mark McLoughlin
1759 f07918fd Mark McLoughlin
static void do_closefd(Monitor *mon, const char *fdname)
1760 f07918fd Mark McLoughlin
{
1761 f07918fd Mark McLoughlin
    mon_fd_t *monfd;
1762 f07918fd Mark McLoughlin
1763 f07918fd Mark McLoughlin
    LIST_FOREACH(monfd, &mon->fds, next) {
1764 f07918fd Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
1765 f07918fd Mark McLoughlin
            continue;
1766 f07918fd Mark McLoughlin
        }
1767 f07918fd Mark McLoughlin
1768 f07918fd Mark McLoughlin
        LIST_REMOVE(monfd, next);
1769 f07918fd Mark McLoughlin
        close(monfd->fd);
1770 f07918fd Mark McLoughlin
        qemu_free(monfd->name);
1771 f07918fd Mark McLoughlin
        qemu_free(monfd);
1772 f07918fd Mark McLoughlin
        return;
1773 f07918fd Mark McLoughlin
    }
1774 f07918fd Mark McLoughlin
1775 f07918fd Mark McLoughlin
    monitor_printf(mon, "Failed to find file descriptor named %s\n",
1776 f07918fd Mark McLoughlin
                   fdname);
1777 f07918fd Mark McLoughlin
}
1778 f07918fd Mark McLoughlin
1779 7768e04c Mark McLoughlin
int monitor_get_fd(Monitor *mon, const char *fdname)
1780 7768e04c Mark McLoughlin
{
1781 7768e04c Mark McLoughlin
    mon_fd_t *monfd;
1782 7768e04c Mark McLoughlin
1783 7768e04c Mark McLoughlin
    LIST_FOREACH(monfd, &mon->fds, next) {
1784 7768e04c Mark McLoughlin
        int fd;
1785 7768e04c Mark McLoughlin
1786 7768e04c Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
1787 7768e04c Mark McLoughlin
            continue;
1788 7768e04c Mark McLoughlin
        }
1789 7768e04c Mark McLoughlin
1790 7768e04c Mark McLoughlin
        fd = monfd->fd;
1791 7768e04c Mark McLoughlin
1792 7768e04c Mark McLoughlin
        /* caller takes ownership of fd */
1793 7768e04c Mark McLoughlin
        LIST_REMOVE(monfd, next);
1794 7768e04c Mark McLoughlin
        qemu_free(monfd->name);
1795 7768e04c Mark McLoughlin
        qemu_free(monfd);
1796 7768e04c Mark McLoughlin
1797 7768e04c Mark McLoughlin
        return fd;
1798 7768e04c Mark McLoughlin
    }
1799 7768e04c Mark McLoughlin
1800 7768e04c Mark McLoughlin
    return -1;
1801 7768e04c Mark McLoughlin
}
1802 7768e04c Mark McLoughlin
1803 376253ec aliguori
static const mon_cmd_t mon_cmds[] = {
1804 2313086a Blue Swirl
#include "qemu-monitor.h"
1805 5fafdf24 ths
    { NULL, NULL, },
1806 9dc39cba bellard
};
1807 9dc39cba bellard
1808 2313086a Blue Swirl
/* Please update qemu-monitor.hx when adding or changing commands */
1809 376253ec aliguori
static const mon_cmd_t info_cmds[] = {
1810 9bc9d1c7 bellard
    { "version", "", do_info_version,
1811 d2c639d6 blueswir1
      "", "show the version of QEMU" },
1812 9307c4c1 bellard
    { "network", "", do_info_network,
1813 9dc39cba bellard
      "", "show the network state" },
1814 5ccfae10 aliguori
    { "chardev", "", qemu_chr_info,
1815 5ccfae10 aliguori
      "", "show the character devices" },
1816 376253ec aliguori
    { "block", "", bdrv_info,
1817 9dc39cba bellard
      "", "show the block devices" },
1818 376253ec aliguori
    { "blockstats", "", bdrv_info_stats,
1819 a36e69dd ths
      "", "show block device statistics" },
1820 9307c4c1 bellard
    { "registers", "", do_info_registers,
1821 9307c4c1 bellard
      "", "show the cpu registers" },
1822 6a00d601 bellard
    { "cpus", "", do_info_cpus,
1823 6a00d601 bellard
      "", "show infos for each CPU" },
1824 aa455485 bellard
    { "history", "", do_info_history,
1825 aa455485 bellard
      "", "show the command line history", },
1826 4a0fb71e bellard
    { "irq", "", irq_info,
1827 4a0fb71e bellard
      "", "show the interrupts statistics (if available)", },
1828 4c27ba27 bellard
    { "pic", "", pic_info,
1829 4c27ba27 bellard
      "", "show i8259 (PIC) state", },
1830 86e0c048 bellard
    { "pci", "", pci_info,
1831 86e0c048 bellard
      "", "show PCI info", },
1832 7c664e2f aurel32
#if defined(TARGET_I386) || defined(TARGET_SH4)
1833 b86bda5b bellard
    { "tlb", "", tlb_info,
1834 b86bda5b bellard
      "", "show virtual to physical memory mappings", },
1835 7c664e2f aurel32
#endif
1836 7c664e2f aurel32
#if defined(TARGET_I386)
1837 b86bda5b bellard
    { "mem", "", mem_info,
1838 b86bda5b bellard
      "", "show the active virtual memory mappings", },
1839 16b29ae1 aliguori
    { "hpet", "", do_info_hpet,
1840 16b29ae1 aliguori
      "", "show state of HPET", },
1841 b86bda5b bellard
#endif
1842 e3db7226 bellard
    { "jit", "", do_info_jit,
1843 e3db7226 bellard
      "", "show dynamic compiler info", },
1844 0f4c6415 bellard
    { "kqemu", "", do_info_kqemu,
1845 d2c639d6 blueswir1
      "", "show KQEMU information", },
1846 7ba1e619 aliguori
    { "kvm", "", do_info_kvm,
1847 d2c639d6 blueswir1
      "", "show KVM information", },
1848 030ea37b aliguori
    { "numa", "", do_info_numa,
1849 030ea37b aliguori
      "", "show NUMA information", },
1850 a594cfbf bellard
    { "usb", "", usb_info,
1851 a594cfbf bellard
      "", "show guest USB devices", },
1852 a594cfbf bellard
    { "usbhost", "", usb_host_info,
1853 a594cfbf bellard
      "", "show host USB devices", },
1854 5f1ce948 bellard
    { "profile", "", do_info_profile,
1855 5f1ce948 bellard
      "", "show profiling information", },
1856 ec36b695 bellard
    { "capture", "", do_info_capture,
1857 17100159 bellard
      "", "show capture information" },
1858 faea38e7 bellard
    { "snapshots", "", do_info_snapshots,
1859 17100159 bellard
      "", "show the currently saved VM snapshots" },
1860 6f9c5ee7 aurel32
    { "status", "", do_info_status,
1861 6f9c5ee7 aurel32
      "", "show the current VM status (running|paused)" },
1862 201a51fc balrog
    { "pcmcia", "", pcmcia_info,
1863 201a51fc balrog
      "", "show guest PCMCIA status" },
1864 455204eb ths
    { "mice", "", do_info_mice,
1865 455204eb ths
      "", "show which guest mouse is receiving events" },
1866 a9ce8590 bellard
    { "vnc", "", do_info_vnc,
1867 a9ce8590 bellard
      "", "show the vnc server status"},
1868 c35734b2 ths
    { "name", "", do_info_name,
1869 c35734b2 ths
      "", "show the current VM name" },
1870 f1f23ad5 blueswir1
    { "uuid", "", do_info_uuid,
1871 f1f23ad5 blueswir1
      "", "show the current VM UUID" },
1872 76a66253 j_mayer
#if defined(TARGET_PPC)
1873 76a66253 j_mayer
    { "cpustats", "", do_info_cpu_stats,
1874 76a66253 j_mayer
      "", "show CPU statistics", },
1875 76a66253 j_mayer
#endif
1876 31a60e22 blueswir1
#if defined(CONFIG_SLIRP)
1877 6dbe553f Jan Kiszka
    { "usernet", "", do_info_usernet,
1878 6dbe553f Jan Kiszka
      "", "show user network stack connection states", },
1879 31a60e22 blueswir1
#endif
1880 5bb7910a aliguori
    { "migrate", "", do_info_migrate, "", "show migration status" },
1881 df751fa8 aliguori
    { "balloon", "", do_info_balloon,
1882 df751fa8 aliguori
      "", "show balloon information" },
1883 cae4956e Gerd Hoffmann
    { "qtree", "", do_info_qtree,
1884 cae4956e Gerd Hoffmann
      "", "show device tree" },
1885 9dc39cba bellard
    { NULL, NULL, },
1886 9dc39cba bellard
};
1887 9dc39cba bellard
1888 9307c4c1 bellard
/*******************************************************************/
1889 9307c4c1 bellard
1890 9307c4c1 bellard
static const char *pch;
1891 9307c4c1 bellard
static jmp_buf expr_env;
1892 9307c4c1 bellard
1893 92a31b1f bellard
#define MD_TLONG 0
1894 92a31b1f bellard
#define MD_I32   1
1895 92a31b1f bellard
1896 9307c4c1 bellard
typedef struct MonitorDef {
1897 9307c4c1 bellard
    const char *name;
1898 9307c4c1 bellard
    int offset;
1899 8662d656 blueswir1
    target_long (*get_value)(const struct MonitorDef *md, int val);
1900 92a31b1f bellard
    int type;
1901 9307c4c1 bellard
} MonitorDef;
1902 9307c4c1 bellard
1903 57206fd4 bellard
#if defined(TARGET_I386)
1904 8662d656 blueswir1
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1905 57206fd4 bellard
{
1906 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1907 6a00d601 bellard
    if (!env)
1908 6a00d601 bellard
        return 0;
1909 6a00d601 bellard
    return env->eip + env->segs[R_CS].base;
1910 57206fd4 bellard
}
1911 57206fd4 bellard
#endif
1912 57206fd4 bellard
1913 a541f297 bellard
#if defined(TARGET_PPC)
1914 8662d656 blueswir1
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1915 a541f297 bellard
{
1916 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1917 a541f297 bellard
    unsigned int u;
1918 a541f297 bellard
    int i;
1919 a541f297 bellard
1920 6a00d601 bellard
    if (!env)
1921 6a00d601 bellard
        return 0;
1922 6a00d601 bellard
1923 a541f297 bellard
    u = 0;
1924 a541f297 bellard
    for (i = 0; i < 8; i++)
1925 28a76be8 aliguori
        u |= env->crf[i] << (32 - (4 * i));
1926 a541f297 bellard
1927 a541f297 bellard
    return u;
1928 a541f297 bellard
}
1929 a541f297 bellard
1930 8662d656 blueswir1
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1931 a541f297 bellard
{
1932 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1933 6a00d601 bellard
    if (!env)
1934 6a00d601 bellard
        return 0;
1935 0411a972 j_mayer
    return env->msr;
1936 a541f297 bellard
}
1937 a541f297 bellard
1938 8662d656 blueswir1
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1939 a541f297 bellard
{
1940 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1941 6a00d601 bellard
    if (!env)
1942 6a00d601 bellard
        return 0;
1943 3d7b417e aurel32
    return env->xer;
1944 a541f297 bellard
}
1945 9fddaa0c bellard
1946 8662d656 blueswir1
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1947 9fddaa0c bellard
{
1948 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1949 6a00d601 bellard
    if (!env)
1950 6a00d601 bellard
        return 0;
1951 6a00d601 bellard
    return cpu_ppc_load_decr(env);
1952 9fddaa0c bellard
}
1953 9fddaa0c bellard
1954 8662d656 blueswir1
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1955 9fddaa0c bellard
{
1956 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1957 6a00d601 bellard
    if (!env)
1958 6a00d601 bellard
        return 0;
1959 6a00d601 bellard
    return cpu_ppc_load_tbu(env);
1960 9fddaa0c bellard
}
1961 9fddaa0c bellard
1962 8662d656 blueswir1
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1963 9fddaa0c bellard
{
1964 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1965 6a00d601 bellard
    if (!env)
1966 6a00d601 bellard
        return 0;
1967 6a00d601 bellard
    return cpu_ppc_load_tbl(env);
1968 9fddaa0c bellard
}
1969 a541f297 bellard
#endif
1970 a541f297 bellard
1971 e95c8d51 bellard
#if defined(TARGET_SPARC)
1972 7b936c0c bellard
#ifndef TARGET_SPARC64
1973 8662d656 blueswir1
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1974 e95c8d51 bellard
{
1975 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1976 6a00d601 bellard
    if (!env)
1977 6a00d601 bellard
        return 0;
1978 6a00d601 bellard
    return GET_PSR(env);
1979 e95c8d51 bellard
}
1980 7b936c0c bellard
#endif
1981 e95c8d51 bellard
1982 8662d656 blueswir1
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1983 e95c8d51 bellard
{
1984 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1985 6a00d601 bellard
    if (!env)
1986 6a00d601 bellard
        return 0;
1987 6a00d601 bellard
    return env->regwptr[val];
1988 e95c8d51 bellard
}
1989 e95c8d51 bellard
#endif
1990 e95c8d51 bellard
1991 8662d656 blueswir1
static const MonitorDef monitor_defs[] = {
1992 9307c4c1 bellard
#ifdef TARGET_I386
1993 57206fd4 bellard
1994 57206fd4 bellard
#define SEG(name, seg) \
1995 92a31b1f bellard
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1996 57206fd4 bellard
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1997 92a31b1f bellard
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1998 57206fd4 bellard
1999 9307c4c1 bellard
    { "eax", offsetof(CPUState, regs[0]) },
2000 9307c4c1 bellard
    { "ecx", offsetof(CPUState, regs[1]) },
2001 9307c4c1 bellard
    { "edx", offsetof(CPUState, regs[2]) },
2002 9307c4c1 bellard
    { "ebx", offsetof(CPUState, regs[3]) },
2003 9307c4c1 bellard
    { "esp|sp", offsetof(CPUState, regs[4]) },
2004 9307c4c1 bellard
    { "ebp|fp", offsetof(CPUState, regs[5]) },
2005 9307c4c1 bellard
    { "esi", offsetof(CPUState, regs[6]) },
2006 01038d2a bellard
    { "edi", offsetof(CPUState, regs[7]) },
2007 92a31b1f bellard
#ifdef TARGET_X86_64
2008 92a31b1f bellard
    { "r8", offsetof(CPUState, regs[8]) },
2009 92a31b1f bellard
    { "r9", offsetof(CPUState, regs[9]) },
2010 92a31b1f bellard
    { "r10", offsetof(CPUState, regs[10]) },
2011 92a31b1f bellard
    { "r11", offsetof(CPUState, regs[11]) },
2012 92a31b1f bellard
    { "r12", offsetof(CPUState, regs[12]) },
2013 92a31b1f bellard
    { "r13", offsetof(CPUState, regs[13]) },
2014 92a31b1f bellard
    { "r14", offsetof(CPUState, regs[14]) },
2015 92a31b1f bellard
    { "r15", offsetof(CPUState, regs[15]) },
2016 92a31b1f bellard
#endif
2017 9307c4c1 bellard
    { "eflags", offsetof(CPUState, eflags) },
2018 57206fd4 bellard
    { "eip", offsetof(CPUState, eip) },
2019 57206fd4 bellard
    SEG("cs", R_CS)
2020 57206fd4 bellard
    SEG("ds", R_DS)
2021 57206fd4 bellard
    SEG("es", R_ES)
2022 01038d2a bellard
    SEG("ss", R_SS)
2023 57206fd4 bellard
    SEG("fs", R_FS)
2024 57206fd4 bellard
    SEG("gs", R_GS)
2025 57206fd4 bellard
    { "pc", 0, monitor_get_pc, },
2026 a541f297 bellard
#elif defined(TARGET_PPC)
2027 ff937dba j_mayer
    /* General purpose registers */
2028 a541f297 bellard
    { "r0", offsetof(CPUState, gpr[0]) },
2029 a541f297 bellard
    { "r1", offsetof(CPUState, gpr[1]) },
2030 a541f297 bellard
    { "r2", offsetof(CPUState, gpr[2]) },
2031 a541f297 bellard
    { "r3", offsetof(CPUState, gpr[3]) },
2032 a541f297 bellard
    { "r4", offsetof(CPUState, gpr[4]) },
2033 a541f297 bellard
    { "r5", offsetof(CPUState, gpr[5]) },
2034 a541f297 bellard
    { "r6", offsetof(CPUState, gpr[6]) },
2035 a541f297 bellard
    { "r7", offsetof(CPUState, gpr[7]) },
2036 a541f297 bellard
    { "r8", offsetof(CPUState, gpr[8]) },
2037 a541f297 bellard
    { "r9", offsetof(CPUState, gpr[9]) },
2038 a541f297 bellard
    { "r10", offsetof(CPUState, gpr[10]) },
2039 a541f297 bellard
    { "r11", offsetof(CPUState, gpr[11]) },
2040 a541f297 bellard
    { "r12", offsetof(CPUState, gpr[12]) },
2041 a541f297 bellard
    { "r13", offsetof(CPUState, gpr[13]) },
2042 a541f297 bellard
    { "r14", offsetof(CPUState, gpr[14]) },
2043 a541f297 bellard
    { "r15", offsetof(CPUState, gpr[15]) },
2044 a541f297 bellard
    { "r16", offsetof(CPUState, gpr[16]) },
2045 a541f297 bellard
    { "r17", offsetof(CPUState, gpr[17]) },
2046 a541f297 bellard
    { "r18", offsetof(CPUState, gpr[18]) },
2047 a541f297 bellard
    { "r19", offsetof(CPUState, gpr[19]) },
2048 a541f297 bellard
    { "r20", offsetof(CPUState, gpr[20]) },
2049 a541f297 bellard
    { "r21", offsetof(CPUState, gpr[21]) },
2050 a541f297 bellard
    { "r22", offsetof(CPUState, gpr[22]) },
2051 a541f297 bellard
    { "r23", offsetof(CPUState, gpr[23]) },
2052 a541f297 bellard
    { "r24", offsetof(CPUState, gpr[24]) },
2053 a541f297 bellard
    { "r25", offsetof(CPUState, gpr[25]) },
2054 a541f297 bellard
    { "r26", offsetof(CPUState, gpr[26]) },
2055 a541f297 bellard
    { "r27", offsetof(CPUState, gpr[27]) },
2056 a541f297 bellard
    { "r28", offsetof(CPUState, gpr[28]) },
2057 a541f297 bellard
    { "r29", offsetof(CPUState, gpr[29]) },
2058 a541f297 bellard
    { "r30", offsetof(CPUState, gpr[30]) },
2059 a541f297 bellard
    { "r31", offsetof(CPUState, gpr[31]) },
2060 ff937dba j_mayer
    /* Floating point registers */
2061 ff937dba j_mayer
    { "f0", offsetof(CPUState, fpr[0]) },
2062 ff937dba j_mayer
    { "f1", offsetof(CPUState, fpr[1]) },
2063 ff937dba j_mayer
    { "f2", offsetof(CPUState, fpr[2]) },
2064 ff937dba j_mayer
    { "f3", offsetof(CPUState, fpr[3]) },
2065 ff937dba j_mayer
    { "f4", offsetof(CPUState, fpr[4]) },
2066 ff937dba j_mayer
    { "f5", offsetof(CPUState, fpr[5]) },
2067 ff937dba j_mayer
    { "f6", offsetof(CPUState, fpr[6]) },
2068 ff937dba j_mayer
    { "f7", offsetof(CPUState, fpr[7]) },
2069 ff937dba j_mayer
    { "f8", offsetof(CPUState, fpr[8]) },
2070 ff937dba j_mayer
    { "f9", offsetof(CPUState, fpr[9]) },
2071 ff937dba j_mayer
    { "f10", offsetof(CPUState, fpr[10]) },
2072 ff937dba j_mayer
    { "f11", offsetof(CPUState, fpr[11]) },
2073 ff937dba j_mayer
    { "f12", offsetof(CPUState, fpr[12]) },
2074 ff937dba j_mayer
    { "f13", offsetof(CPUState, fpr[13]) },
2075 ff937dba j_mayer
    { "f14", offsetof(CPUState, fpr[14]) },
2076 ff937dba j_mayer
    { "f15", offsetof(CPUState, fpr[15]) },
2077 ff937dba j_mayer
    { "f16", offsetof(CPUState, fpr[16]) },
2078 ff937dba j_mayer
    { "f17", offsetof(CPUState, fpr[17]) },
2079 ff937dba j_mayer
    { "f18", offsetof(CPUState, fpr[18]) },
2080 ff937dba j_mayer
    { "f19", offsetof(CPUState, fpr[19]) },
2081 ff937dba j_mayer
    { "f20", offsetof(CPUState, fpr[20]) },
2082 ff937dba j_mayer
    { "f21", offsetof(CPUState, fpr[21]) },
2083 ff937dba j_mayer
    { "f22", offsetof(CPUState, fpr[22]) },
2084 ff937dba j_mayer
    { "f23", offsetof(CPUState, fpr[23]) },
2085 ff937dba j_mayer
    { "f24", offsetof(CPUState, fpr[24]) },
2086 ff937dba j_mayer
    { "f25", offsetof(CPUState, fpr[25]) },
2087 ff937dba j_mayer
    { "f26", offsetof(CPUState, fpr[26]) },
2088 ff937dba j_mayer
    { "f27", offsetof(CPUState, fpr[27]) },
2089 ff937dba j_mayer
    { "f28", offsetof(CPUState, fpr[28]) },
2090 ff937dba j_mayer
    { "f29", offsetof(CPUState, fpr[29]) },
2091 ff937dba j_mayer
    { "f30", offsetof(CPUState, fpr[30]) },
2092 ff937dba j_mayer
    { "f31", offsetof(CPUState, fpr[31]) },
2093 ff937dba j_mayer
    { "fpscr", offsetof(CPUState, fpscr) },
2094 ff937dba j_mayer
    /* Next instruction pointer */
2095 57206fd4 bellard
    { "nip|pc", offsetof(CPUState, nip) },
2096 a541f297 bellard
    { "lr", offsetof(CPUState, lr) },
2097 a541f297 bellard
    { "ctr", offsetof(CPUState, ctr) },
2098 9fddaa0c bellard
    { "decr", 0, &monitor_get_decr, },
2099 a541f297 bellard
    { "ccr", 0, &monitor_get_ccr, },
2100 ff937dba j_mayer
    /* Machine state register */
2101 a541f297 bellard
    { "msr", 0, &monitor_get_msr, },
2102 a541f297 bellard
    { "xer", 0, &monitor_get_xer, },
2103 9fddaa0c bellard
    { "tbu", 0, &monitor_get_tbu, },
2104 9fddaa0c bellard
    { "tbl", 0, &monitor_get_tbl, },
2105 ff937dba j_mayer
#if defined(TARGET_PPC64)
2106 ff937dba j_mayer
    /* Address space register */
2107 ff937dba j_mayer
    { "asr", offsetof(CPUState, asr) },
2108 ff937dba j_mayer
#endif
2109 ff937dba j_mayer
    /* Segment registers */
2110 a541f297 bellard
    { "sdr1", offsetof(CPUState, sdr1) },
2111 a541f297 bellard
    { "sr0", offsetof(CPUState, sr[0]) },
2112 a541f297 bellard
    { "sr1", offsetof(CPUState, sr[1]) },
2113 a541f297 bellard
    { "sr2", offsetof(CPUState, sr[2]) },
2114 a541f297 bellard
    { "sr3", offsetof(CPUState, sr[3]) },
2115 a541f297 bellard
    { "sr4", offsetof(CPUState, sr[4]) },
2116 a541f297 bellard
    { "sr5", offsetof(CPUState, sr[5]) },
2117 a541f297 bellard
    { "sr6", offsetof(CPUState, sr[6]) },
2118 a541f297 bellard
    { "sr7", offsetof(CPUState, sr[7]) },
2119 a541f297 bellard
    { "sr8", offsetof(CPUState, sr[8]) },
2120 a541f297 bellard
    { "sr9", offsetof(CPUState, sr[9]) },
2121 a541f297 bellard
    { "sr10", offsetof(CPUState, sr[10]) },
2122 a541f297 bellard
    { "sr11", offsetof(CPUState, sr[11]) },
2123 a541f297 bellard
    { "sr12", offsetof(CPUState, sr[12]) },
2124 a541f297 bellard
    { "sr13", offsetof(CPUState, sr[13]) },
2125 a541f297 bellard
    { "sr14", offsetof(CPUState, sr[14]) },
2126 a541f297 bellard
    { "sr15", offsetof(CPUState, sr[15]) },
2127 a541f297 bellard
    /* Too lazy to put BATs and SPRs ... */
2128 e95c8d51 bellard
#elif defined(TARGET_SPARC)
2129 e95c8d51 bellard
    { "g0", offsetof(CPUState, gregs[0]) },
2130 e95c8d51 bellard
    { "g1", offsetof(CPUState, gregs[1]) },
2131 e95c8d51 bellard
    { "g2", offsetof(CPUState, gregs[2]) },
2132 e95c8d51 bellard
    { "g3", offsetof(CPUState, gregs[3]) },
2133 e95c8d51 bellard
    { "g4", offsetof(CPUState, gregs[4]) },
2134 e95c8d51 bellard
    { "g5", offsetof(CPUState, gregs[5]) },
2135 e95c8d51 bellard
    { "g6", offsetof(CPUState, gregs[6]) },
2136 e95c8d51 bellard
    { "g7", offsetof(CPUState, gregs[7]) },
2137 e95c8d51 bellard
    { "o0", 0, monitor_get_reg },
2138 e95c8d51 bellard
    { "o1", 1, monitor_get_reg },
2139 e95c8d51 bellard
    { "o2", 2, monitor_get_reg },
2140 e95c8d51 bellard
    { "o3", 3, monitor_get_reg },
2141 e95c8d51 bellard
    { "o4", 4, monitor_get_reg },
2142 e95c8d51 bellard
    { "o5", 5, monitor_get_reg },
2143 e95c8d51 bellard
    { "o6", 6, monitor_get_reg },
2144 e95c8d51 bellard
    { "o7", 7, monitor_get_reg },
2145 e95c8d51 bellard
    { "l0", 8, monitor_get_reg },
2146 e95c8d51 bellard
    { "l1", 9, monitor_get_reg },
2147 e95c8d51 bellard
    { "l2", 10, monitor_get_reg },
2148 e95c8d51 bellard
    { "l3", 11, monitor_get_reg },
2149 e95c8d51 bellard
    { "l4", 12, monitor_get_reg },
2150 e95c8d51 bellard
    { "l5", 13, monitor_get_reg },
2151 e95c8d51 bellard
    { "l6", 14, monitor_get_reg },
2152 e95c8d51 bellard
    { "l7", 15, monitor_get_reg },
2153 e95c8d51 bellard
    { "i0", 16, monitor_get_reg },
2154 e95c8d51 bellard
    { "i1", 17, monitor_get_reg },
2155 e95c8d51 bellard
    { "i2", 18, monitor_get_reg },
2156 e95c8d51 bellard
    { "i3", 19, monitor_get_reg },
2157 e95c8d51 bellard
    { "i4", 20, monitor_get_reg },
2158 e95c8d51 bellard
    { "i5", 21, monitor_get_reg },
2159 e95c8d51 bellard
    { "i6", 22, monitor_get_reg },
2160 e95c8d51 bellard
    { "i7", 23, monitor_get_reg },
2161 e95c8d51 bellard
    { "pc", offsetof(CPUState, pc) },
2162 e95c8d51 bellard
    { "npc", offsetof(CPUState, npc) },
2163 e95c8d51 bellard
    { "y", offsetof(CPUState, y) },
2164 7b936c0c bellard
#ifndef TARGET_SPARC64
2165 e95c8d51 bellard
    { "psr", 0, &monitor_get_psr, },
2166 e95c8d51 bellard
    { "wim", offsetof(CPUState, wim) },
2167 7b936c0c bellard
#endif
2168 e95c8d51 bellard
    { "tbr", offsetof(CPUState, tbr) },
2169 e95c8d51 bellard
    { "fsr", offsetof(CPUState, fsr) },
2170 e95c8d51 bellard
    { "f0", offsetof(CPUState, fpr[0]) },
2171 e95c8d51 bellard
    { "f1", offsetof(CPUState, fpr[1]) },
2172 e95c8d51 bellard
    { "f2", offsetof(CPUState, fpr[2]) },
2173 e95c8d51 bellard
    { "f3", offsetof(CPUState, fpr[3]) },
2174 e95c8d51 bellard
    { "f4", offsetof(CPUState, fpr[4]) },
2175 e95c8d51 bellard
    { "f5", offsetof(CPUState, fpr[5]) },
2176 e95c8d51 bellard
    { "f6", offsetof(CPUState, fpr[6]) },
2177 e95c8d51 bellard
    { "f7", offsetof(CPUState, fpr[7]) },
2178 e95c8d51 bellard
    { "f8", offsetof(CPUState, fpr[8]) },
2179 e95c8d51 bellard
    { "f9", offsetof(CPUState, fpr[9]) },
2180 e95c8d51 bellard
    { "f10", offsetof(CPUState, fpr[10]) },
2181 e95c8d51 bellard
    { "f11", offsetof(CPUState, fpr[11]) },
2182 e95c8d51 bellard
    { "f12", offsetof(CPUState, fpr[12]) },
2183 e95c8d51 bellard
    { "f13", offsetof(CPUState, fpr[13]) },
2184 e95c8d51 bellard
    { "f14", offsetof(CPUState, fpr[14]) },
2185 e95c8d51 bellard
    { "f15", offsetof(CPUState, fpr[15]) },
2186 e95c8d51 bellard
    { "f16", offsetof(CPUState, fpr[16]) },
2187 e95c8d51 bellard
    { "f17", offsetof(CPUState, fpr[17]) },
2188 e95c8d51 bellard
    { "f18", offsetof(CPUState, fpr[18]) },
2189 e95c8d51 bellard
    { "f19", offsetof(CPUState, fpr[19]) },
2190 e95c8d51 bellard
    { "f20", offsetof(CPUState, fpr[20]) },
2191 e95c8d51 bellard
    { "f21", offsetof(CPUState, fpr[21]) },
2192 e95c8d51 bellard
    { "f22", offsetof(CPUState, fpr[22]) },
2193 e95c8d51 bellard
    { "f23", offsetof(CPUState, fpr[23]) },
2194 e95c8d51 bellard
    { "f24", offsetof(CPUState, fpr[24]) },
2195 e95c8d51 bellard
    { "f25", offsetof(CPUState, fpr[25]) },
2196 e95c8d51 bellard
    { "f26", offsetof(CPUState, fpr[26]) },
2197 e95c8d51 bellard
    { "f27", offsetof(CPUState, fpr[27]) },
2198 e95c8d51 bellard
    { "f28", offsetof(CPUState, fpr[28]) },
2199 e95c8d51 bellard
    { "f29", offsetof(CPUState, fpr[29]) },
2200 e95c8d51 bellard
    { "f30", offsetof(CPUState, fpr[30]) },
2201 e95c8d51 bellard
    { "f31", offsetof(CPUState, fpr[31]) },
2202 7b936c0c bellard
#ifdef TARGET_SPARC64
2203 7b936c0c bellard
    { "f32", offsetof(CPUState, fpr[32]) },
2204 7b936c0c bellard
    { "f34", offsetof(CPUState, fpr[34]) },
2205 7b936c0c bellard
    { "f36", offsetof(CPUState, fpr[36]) },
2206 7b936c0c bellard
    { "f38", offsetof(CPUState, fpr[38]) },
2207 7b936c0c bellard
    { "f40", offsetof(CPUState, fpr[40]) },
2208 7b936c0c bellard
    { "f42", offsetof(CPUState, fpr[42]) },
2209 7b936c0c bellard
    { "f44", offsetof(CPUState, fpr[44]) },
2210 7b936c0c bellard
    { "f46", offsetof(CPUState, fpr[46]) },
2211 7b936c0c bellard
    { "f48", offsetof(CPUState, fpr[48]) },
2212 7b936c0c bellard
    { "f50", offsetof(CPUState, fpr[50]) },
2213 7b936c0c bellard
    { "f52", offsetof(CPUState, fpr[52]) },
2214 7b936c0c bellard
    { "f54", offsetof(CPUState, fpr[54]) },
2215 7b936c0c bellard
    { "f56", offsetof(CPUState, fpr[56]) },
2216 7b936c0c bellard
    { "f58", offsetof(CPUState, fpr[58]) },
2217 7b936c0c bellard
    { "f60", offsetof(CPUState, fpr[60]) },
2218 7b936c0c bellard
    { "f62", offsetof(CPUState, fpr[62]) },
2219 7b936c0c bellard
    { "asi", offsetof(CPUState, asi) },
2220 7b936c0c bellard
    { "pstate", offsetof(CPUState, pstate) },
2221 7b936c0c bellard
    { "cansave", offsetof(CPUState, cansave) },
2222 7b936c0c bellard
    { "canrestore", offsetof(CPUState, canrestore) },
2223 7b936c0c bellard
    { "otherwin", offsetof(CPUState, otherwin) },
2224 7b936c0c bellard
    { "wstate", offsetof(CPUState, wstate) },
2225 7b936c0c bellard
    { "cleanwin", offsetof(CPUState, cleanwin) },
2226 7b936c0c bellard
    { "fprs", offsetof(CPUState, fprs) },
2227 7b936c0c bellard
#endif
2228 9307c4c1 bellard
#endif
2229 9307c4c1 bellard
    { NULL },
2230 9307c4c1 bellard
};
2231 9307c4c1 bellard
2232 376253ec aliguori
static void expr_error(Monitor *mon, const char *msg)
2233 9dc39cba bellard
{
2234 376253ec aliguori
    monitor_printf(mon, "%s\n", msg);
2235 9307c4c1 bellard
    longjmp(expr_env, 1);
2236 9307c4c1 bellard
}
2237 9307c4c1 bellard
2238 6a00d601 bellard
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2239 92a31b1f bellard
static int get_monitor_def(target_long *pval, const char *name)
2240 9307c4c1 bellard
{
2241 8662d656 blueswir1
    const MonitorDef *md;
2242 92a31b1f bellard
    void *ptr;
2243 92a31b1f bellard
2244 9307c4c1 bellard
    for(md = monitor_defs; md->name != NULL; md++) {
2245 9307c4c1 bellard
        if (compare_cmd(name, md->name)) {
2246 9307c4c1 bellard
            if (md->get_value) {
2247 e95c8d51 bellard
                *pval = md->get_value(md, md->offset);
2248 9307c4c1 bellard
            } else {
2249 6a00d601 bellard
                CPUState *env = mon_get_cpu();
2250 6a00d601 bellard
                if (!env)
2251 6a00d601 bellard
                    return -2;
2252 6a00d601 bellard
                ptr = (uint8_t *)env + md->offset;
2253 92a31b1f bellard
                switch(md->type) {
2254 92a31b1f bellard
                case MD_I32:
2255 92a31b1f bellard
                    *pval = *(int32_t *)ptr;
2256 92a31b1f bellard
                    break;
2257 92a31b1f bellard
                case MD_TLONG:
2258 92a31b1f bellard
                    *pval = *(target_long *)ptr;
2259 92a31b1f bellard
                    break;
2260 92a31b1f bellard
                default:
2261 92a31b1f bellard
                    *pval = 0;
2262 92a31b1f bellard
                    break;
2263 92a31b1f bellard
                }
2264 9307c4c1 bellard
            }
2265 9307c4c1 bellard
            return 0;
2266 9307c4c1 bellard
        }
2267 9307c4c1 bellard
    }
2268 9307c4c1 bellard
    return -1;
2269 9307c4c1 bellard
}
2270 9307c4c1 bellard
2271 9307c4c1 bellard
static void next(void)
2272 9307c4c1 bellard
{
2273 9307c4c1 bellard
    if (pch != '\0') {
2274 9307c4c1 bellard
        pch++;
2275 cd390083 blueswir1
        while (qemu_isspace(*pch))
2276 9307c4c1 bellard
            pch++;
2277 9307c4c1 bellard
    }
2278 9307c4c1 bellard
}
2279 9307c4c1 bellard
2280 376253ec aliguori
static int64_t expr_sum(Monitor *mon);
2281 9307c4c1 bellard
2282 376253ec aliguori
static int64_t expr_unary(Monitor *mon)
2283 9307c4c1 bellard
{
2284 c2efc95d blueswir1
    int64_t n;
2285 9307c4c1 bellard
    char *p;
2286 6a00d601 bellard
    int ret;
2287 9307c4c1 bellard
2288 9307c4c1 bellard
    switch(*pch) {
2289 9307c4c1 bellard
    case '+':
2290 9307c4c1 bellard
        next();
2291 376253ec aliguori
        n = expr_unary(mon);
2292 9307c4c1 bellard
        break;
2293 9307c4c1 bellard
    case '-':
2294 9307c4c1 bellard
        next();
2295 376253ec aliguori
        n = -expr_unary(mon);
2296 9307c4c1 bellard
        break;
2297 9307c4c1 bellard
    case '~':
2298 9307c4c1 bellard
        next();
2299 376253ec aliguori
        n = ~expr_unary(mon);
2300 9307c4c1 bellard
        break;
2301 9307c4c1 bellard
    case '(':
2302 9307c4c1 bellard
        next();
2303 376253ec aliguori
        n = expr_sum(mon);
2304 9307c4c1 bellard
        if (*pch != ')') {
2305 376253ec aliguori
            expr_error(mon, "')' expected");
2306 9307c4c1 bellard
        }
2307 9307c4c1 bellard
        next();
2308 9307c4c1 bellard
        break;
2309 81d0912d bellard
    case '\'':
2310 81d0912d bellard
        pch++;
2311 81d0912d bellard
        if (*pch == '\0')
2312 376253ec aliguori
            expr_error(mon, "character constant expected");
2313 81d0912d bellard
        n = *pch;
2314 81d0912d bellard
        pch++;
2315 81d0912d bellard
        if (*pch != '\'')
2316 376253ec aliguori
            expr_error(mon, "missing terminating \' character");
2317 81d0912d bellard
        next();
2318 81d0912d bellard
        break;
2319 9307c4c1 bellard
    case '$':
2320 9307c4c1 bellard
        {
2321 9307c4c1 bellard
            char buf[128], *q;
2322 69b34976 ths
            target_long reg=0;
2323 3b46e624 ths
2324 9307c4c1 bellard
            pch++;
2325 9307c4c1 bellard
            q = buf;
2326 9307c4c1 bellard
            while ((*pch >= 'a' && *pch <= 'z') ||
2327 9307c4c1 bellard
                   (*pch >= 'A' && *pch <= 'Z') ||
2328 9307c4c1 bellard
                   (*pch >= '0' && *pch <= '9') ||
2329 57206fd4 bellard
                   *pch == '_' || *pch == '.') {
2330 9307c4c1 bellard
                if ((q - buf) < sizeof(buf) - 1)
2331 9307c4c1 bellard
                    *q++ = *pch;
2332 9307c4c1 bellard
                pch++;
2333 9307c4c1 bellard
            }
2334 cd390083 blueswir1
            while (qemu_isspace(*pch))
2335 9307c4c1 bellard
                pch++;
2336 9307c4c1 bellard
            *q = 0;
2337 7743e588 blueswir1
            ret = get_monitor_def(&reg, buf);
2338 6a00d601 bellard
            if (ret == -1)
2339 376253ec aliguori
                expr_error(mon, "unknown register");
2340 5fafdf24 ths
            else if (ret == -2)
2341 376253ec aliguori
                expr_error(mon, "no cpu defined");
2342 7743e588 blueswir1
            n = reg;
2343 9307c4c1 bellard
        }
2344 9307c4c1 bellard
        break;
2345 9307c4c1 bellard
    case '\0':
2346 376253ec aliguori
        expr_error(mon, "unexpected end of expression");
2347 9307c4c1 bellard
        n = 0;
2348 9307c4c1 bellard
        break;
2349 9307c4c1 bellard
    default:
2350 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS > 32
2351 4f4fbf77 bellard
        n = strtoull(pch, &p, 0);
2352 4f4fbf77 bellard
#else
2353 9307c4c1 bellard
        n = strtoul(pch, &p, 0);
2354 4f4fbf77 bellard
#endif
2355 9307c4c1 bellard
        if (pch == p) {
2356 376253ec aliguori
            expr_error(mon, "invalid char in expression");
2357 9307c4c1 bellard
        }
2358 9307c4c1 bellard
        pch = p;
2359 cd390083 blueswir1
        while (qemu_isspace(*pch))
2360 9307c4c1 bellard
            pch++;
2361 9307c4c1 bellard
        break;
2362 9307c4c1 bellard
    }
2363 9307c4c1 bellard
    return n;
2364 9307c4c1 bellard
}
2365 9307c4c1 bellard
2366 9307c4c1 bellard
2367 376253ec aliguori
static int64_t expr_prod(Monitor *mon)
2368 9307c4c1 bellard
{
2369 c2efc95d blueswir1
    int64_t val, val2;
2370 92a31b1f bellard
    int op;
2371 3b46e624 ths
2372 376253ec aliguori
    val = expr_unary(mon);
2373 9307c4c1 bellard
    for(;;) {
2374 9307c4c1 bellard
        op = *pch;
2375 9307c4c1 bellard
        if (op != '*' && op != '/' && op != '%')
2376 9307c4c1 bellard
            break;
2377 9307c4c1 bellard
        next();
2378 376253ec aliguori
        val2 = expr_unary(mon);
2379 9307c4c1 bellard
        switch(op) {
2380 9307c4c1 bellard
        default:
2381 9307c4c1 bellard
        case '*':
2382 9307c4c1 bellard
            val *= val2;
2383 9307c4c1 bellard
            break;
2384 9307c4c1 bellard
        case '/':
2385 9307c4c1 bellard
        case '%':
2386 5fafdf24 ths
            if (val2 == 0)
2387 376253ec aliguori
                expr_error(mon, "division by zero");
2388 9307c4c1 bellard
            if (op == '/')
2389 9307c4c1 bellard
                val /= val2;
2390 9307c4c1 bellard
            else
2391 9307c4c1 bellard
                val %= val2;
2392 9307c4c1 bellard
            break;
2393 9307c4c1 bellard
        }
2394 9307c4c1 bellard
    }
2395 9307c4c1 bellard
    return val;
2396 9307c4c1 bellard
}
2397 9307c4c1 bellard
2398 376253ec aliguori
static int64_t expr_logic(Monitor *mon)
2399 9307c4c1 bellard
{
2400 c2efc95d blueswir1
    int64_t val, val2;
2401 92a31b1f bellard
    int op;
2402 9307c4c1 bellard
2403 376253ec aliguori
    val = expr_prod(mon);
2404 9307c4c1 bellard
    for(;;) {
2405 9307c4c1 bellard
        op = *pch;
2406 9307c4c1 bellard
        if (op != '&' && op != '|' && op != '^')
2407 9307c4c1 bellard
            break;
2408 9307c4c1 bellard
        next();
2409 376253ec aliguori
        val2 = expr_prod(mon);
2410 9307c4c1 bellard
        switch(op) {
2411 9307c4c1 bellard
        default:
2412 9307c4c1 bellard
        case '&':
2413 9307c4c1 bellard
            val &= val2;
2414 9307c4c1 bellard
            break;
2415 9307c4c1 bellard
        case '|':
2416 9307c4c1 bellard
            val |= val2;
2417 9307c4c1 bellard
            break;
2418 9307c4c1 bellard
        case '^':
2419 9307c4c1 bellard
            val ^= val2;
2420 9307c4c1 bellard
            break;
2421 9307c4c1 bellard
        }
2422 9307c4c1 bellard
    }
2423 9307c4c1 bellard
    return val;
2424 9307c4c1 bellard
}
2425 9307c4c1 bellard
2426 376253ec aliguori
static int64_t expr_sum(Monitor *mon)
2427 9307c4c1 bellard
{
2428 c2efc95d blueswir1
    int64_t val, val2;
2429 92a31b1f bellard
    int op;
2430 9307c4c1 bellard
2431 376253ec aliguori
    val = expr_logic(mon);
2432 9307c4c1 bellard
    for(;;) {
2433 9307c4c1 bellard
        op = *pch;
2434 9307c4c1 bellard
        if (op != '+' && op != '-')
2435 9307c4c1 bellard
            break;
2436 9307c4c1 bellard
        next();
2437 376253ec aliguori
        val2 = expr_logic(mon);
2438 9307c4c1 bellard
        if (op == '+')
2439 9307c4c1 bellard
            val += val2;
2440 9307c4c1 bellard
        else
2441 9307c4c1 bellard
            val -= val2;
2442 9307c4c1 bellard
    }
2443 9307c4c1 bellard
    return val;
2444 9307c4c1 bellard
}
2445 9307c4c1 bellard
2446 376253ec aliguori
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2447 9307c4c1 bellard
{
2448 9307c4c1 bellard
    pch = *pp;
2449 9307c4c1 bellard
    if (setjmp(expr_env)) {
2450 9307c4c1 bellard
        *pp = pch;
2451 9307c4c1 bellard
        return -1;
2452 9307c4c1 bellard
    }
2453 cd390083 blueswir1
    while (qemu_isspace(*pch))
2454 9307c4c1 bellard
        pch++;
2455 376253ec aliguori
    *pval = expr_sum(mon);
2456 9307c4c1 bellard
    *pp = pch;
2457 9307c4c1 bellard
    return 0;
2458 9307c4c1 bellard
}
2459 9307c4c1 bellard
2460 9307c4c1 bellard
static int get_str(char *buf, int buf_size, const char **pp)
2461 9307c4c1 bellard
{
2462 9307c4c1 bellard
    const char *p;
2463 9307c4c1 bellard
    char *q;
2464 9307c4c1 bellard
    int c;
2465 9307c4c1 bellard
2466 81d0912d bellard
    q = buf;
2467 9307c4c1 bellard
    p = *pp;
2468 cd390083 blueswir1
    while (qemu_isspace(*p))
2469 9307c4c1 bellard
        p++;
2470 9307c4c1 bellard
    if (*p == '\0') {
2471 9307c4c1 bellard
    fail:
2472 81d0912d bellard
        *q = '\0';
2473 9307c4c1 bellard
        *pp = p;
2474 9307c4c1 bellard
        return -1;
2475 9307c4c1 bellard
    }
2476 9307c4c1 bellard
    if (*p == '\"') {
2477 9307c4c1 bellard
        p++;
2478 9307c4c1 bellard
        while (*p != '\0' && *p != '\"') {
2479 9307c4c1 bellard
            if (*p == '\\') {
2480 9307c4c1 bellard
                p++;
2481 9307c4c1 bellard
                c = *p++;
2482 9307c4c1 bellard
                switch(c) {
2483 9307c4c1 bellard
                case 'n':
2484 9307c4c1 bellard
                    c = '\n';
2485 9307c4c1 bellard
                    break;
2486 9307c4c1 bellard
                case 'r':
2487 9307c4c1 bellard
                    c = '\r';
2488 9307c4c1 bellard
                    break;
2489 9307c4c1 bellard
                case '\\':
2490 9307c4c1 bellard
                case '\'':
2491 9307c4c1 bellard
                case '\"':
2492 9307c4c1 bellard
                    break;
2493 9307c4c1 bellard
                default:
2494 9307c4c1 bellard
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2495 9307c4c1 bellard
                    goto fail;
2496 9307c4c1 bellard
                }
2497 9307c4c1 bellard
                if ((q - buf) < buf_size - 1) {
2498 9307c4c1 bellard
                    *q++ = c;
2499 9307c4c1 bellard
                }
2500 9307c4c1 bellard
            } else {
2501 9307c4c1 bellard
                if ((q - buf) < buf_size - 1) {
2502 9307c4c1 bellard
                    *q++ = *p;
2503 9307c4c1 bellard
                }
2504 9307c4c1 bellard
                p++;
2505 9307c4c1 bellard
            }
2506 9307c4c1 bellard
        }
2507 9307c4c1 bellard
        if (*p != '\"') {
2508 5b60212f bellard
            qemu_printf("unterminated string\n");
2509 9307c4c1 bellard
            goto fail;
2510 9307c4c1 bellard
        }
2511 9307c4c1 bellard
        p++;
2512 9307c4c1 bellard
    } else {
2513 cd390083 blueswir1
        while (*p != '\0' && !qemu_isspace(*p)) {
2514 9307c4c1 bellard
            if ((q - buf) < buf_size - 1) {
2515 9307c4c1 bellard
                *q++ = *p;
2516 9307c4c1 bellard
            }
2517 9307c4c1 bellard
            p++;
2518 9307c4c1 bellard
        }
2519 9307c4c1 bellard
    }
2520 81d0912d bellard
    *q = '\0';
2521 9307c4c1 bellard
    *pp = p;
2522 9307c4c1 bellard
    return 0;
2523 9307c4c1 bellard
}
2524 9307c4c1 bellard
2525 4590fd80 Luiz Capitulino
/*
2526 4590fd80 Luiz Capitulino
 * Store the command-name in cmdname, and return a pointer to
2527 4590fd80 Luiz Capitulino
 * the remaining of the command string.
2528 4590fd80 Luiz Capitulino
 */
2529 4590fd80 Luiz Capitulino
static const char *get_command_name(const char *cmdline,
2530 4590fd80 Luiz Capitulino
                                    char *cmdname, size_t nlen)
2531 4590fd80 Luiz Capitulino
{
2532 4590fd80 Luiz Capitulino
    size_t len;
2533 4590fd80 Luiz Capitulino
    const char *p, *pstart;
2534 4590fd80 Luiz Capitulino
2535 4590fd80 Luiz Capitulino
    p = cmdline;
2536 4590fd80 Luiz Capitulino
    while (qemu_isspace(*p))
2537 4590fd80 Luiz Capitulino
        p++;
2538 4590fd80 Luiz Capitulino
    if (*p == '\0')
2539 4590fd80 Luiz Capitulino
        return NULL;
2540 4590fd80 Luiz Capitulino
    pstart = p;
2541 4590fd80 Luiz Capitulino
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2542 4590fd80 Luiz Capitulino
        p++;
2543 4590fd80 Luiz Capitulino
    len = p - pstart;
2544 4590fd80 Luiz Capitulino
    if (len > nlen - 1)
2545 4590fd80 Luiz Capitulino
        len = nlen - 1;
2546 4590fd80 Luiz Capitulino
    memcpy(cmdname, pstart, len);
2547 4590fd80 Luiz Capitulino
    cmdname[len] = '\0';
2548 4590fd80 Luiz Capitulino
    return p;
2549 4590fd80 Luiz Capitulino
}
2550 4590fd80 Luiz Capitulino
2551 9307c4c1 bellard
static int default_fmt_format = 'x';
2552 9307c4c1 bellard
static int default_fmt_size = 4;
2553 9307c4c1 bellard
2554 9307c4c1 bellard
#define MAX_ARGS 16
2555 9307c4c1 bellard
2556 376253ec aliguori
static void monitor_handle_command(Monitor *mon, const char *cmdline)
2557 9307c4c1 bellard
{
2558 4590fd80 Luiz Capitulino
    const char *p, *typestr;
2559 4590fd80 Luiz Capitulino
    int c, nb_args, i, has_arg;
2560 376253ec aliguori
    const mon_cmd_t *cmd;
2561 9307c4c1 bellard
    char cmdname[256];
2562 9307c4c1 bellard
    char buf[1024];
2563 9307c4c1 bellard
    void *str_allocated[MAX_ARGS];
2564 9307c4c1 bellard
    void *args[MAX_ARGS];
2565 376253ec aliguori
    void (*handler_0)(Monitor *mon);
2566 376253ec aliguori
    void (*handler_1)(Monitor *mon, void *arg0);
2567 376253ec aliguori
    void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2568 376253ec aliguori
    void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2569 376253ec aliguori
    void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2570 376253ec aliguori
                      void *arg3);
2571 376253ec aliguori
    void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2572 376253ec aliguori
                      void *arg3, void *arg4);
2573 376253ec aliguori
    void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2574 376253ec aliguori
                      void *arg3, void *arg4, void *arg5);
2575 376253ec aliguori
    void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2576 376253ec aliguori
                      void *arg3, void *arg4, void *arg5, void *arg6);
2577 79c4f6b0 Huang Ying
    void (*handler_8)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2578 79c4f6b0 Huang Ying
                      void *arg3, void *arg4, void *arg5, void *arg6,
2579 79c4f6b0 Huang Ying
                      void *arg7);
2580 79c4f6b0 Huang Ying
    void (*handler_9)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2581 79c4f6b0 Huang Ying
                      void *arg3, void *arg4, void *arg5, void *arg6,
2582 79c4f6b0 Huang Ying
                      void *arg7, void *arg8);
2583 79c4f6b0 Huang Ying
    void (*handler_10)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2584 79c4f6b0 Huang Ying
                       void *arg3, void *arg4, void *arg5, void *arg6,
2585 79c4f6b0 Huang Ying
                       void *arg7, void *arg8, void *arg9);
2586 9dc39cba bellard
2587 9dc39cba bellard
#ifdef DEBUG
2588 376253ec aliguori
    monitor_printf(mon, "command='%s'\n", cmdline);
2589 9dc39cba bellard
#endif
2590 3b46e624 ths
2591 9307c4c1 bellard
    /* extract the command name */
2592 4590fd80 Luiz Capitulino
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2593 4590fd80 Luiz Capitulino
    if (!p)
2594 9307c4c1 bellard
        return;
2595 3b46e624 ths
2596 9307c4c1 bellard
    /* find the command */
2597 376253ec aliguori
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2598 5fafdf24 ths
        if (compare_cmd(cmdname, cmd->name))
2599 d91d9bf6 Luiz Capitulino
            break;
2600 d91d9bf6 Luiz Capitulino
    }
2601 d91d9bf6 Luiz Capitulino
2602 d91d9bf6 Luiz Capitulino
    if (cmd->name == NULL) {
2603 d91d9bf6 Luiz Capitulino
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2604 d91d9bf6 Luiz Capitulino
        return;
2605 9307c4c1 bellard
    }
2606 9307c4c1 bellard
2607 9307c4c1 bellard
    for(i = 0; i < MAX_ARGS; i++)
2608 9307c4c1 bellard
        str_allocated[i] = NULL;
2609 3b46e624 ths
2610 9307c4c1 bellard
    /* parse the parameters */
2611 9307c4c1 bellard
    typestr = cmd->args_type;
2612 9307c4c1 bellard
    nb_args = 0;
2613 9dc39cba bellard
    for(;;) {
2614 9307c4c1 bellard
        c = *typestr;
2615 9307c4c1 bellard
        if (c == '\0')
2616 9dc39cba bellard
            break;
2617 9307c4c1 bellard
        typestr++;
2618 9307c4c1 bellard
        switch(c) {
2619 9307c4c1 bellard
        case 'F':
2620 81d0912d bellard
        case 'B':
2621 9307c4c1 bellard
        case 's':
2622 9307c4c1 bellard
            {
2623 9307c4c1 bellard
                int ret;
2624 9307c4c1 bellard
                char *str;
2625 3b46e624 ths
2626 cd390083 blueswir1
                while (qemu_isspace(*p))
2627 9307c4c1 bellard
                    p++;
2628 9307c4c1 bellard
                if (*typestr == '?') {
2629 9307c4c1 bellard
                    typestr++;
2630 9307c4c1 bellard
                    if (*p == '\0') {
2631 9307c4c1 bellard
                        /* no optional string: NULL argument */
2632 9307c4c1 bellard
                        str = NULL;
2633 9307c4c1 bellard
                        goto add_str;
2634 9307c4c1 bellard
                    }
2635 9307c4c1 bellard
                }
2636 9307c4c1 bellard
                ret = get_str(buf, sizeof(buf), &p);
2637 9307c4c1 bellard
                if (ret < 0) {
2638 81d0912d bellard
                    switch(c) {
2639 81d0912d bellard
                    case 'F':
2640 376253ec aliguori
                        monitor_printf(mon, "%s: filename expected\n",
2641 376253ec aliguori
                                       cmdname);
2642 81d0912d bellard
                        break;
2643 81d0912d bellard
                    case 'B':
2644 376253ec aliguori
                        monitor_printf(mon, "%s: block device name expected\n",
2645 376253ec aliguori
                                       cmdname);
2646 81d0912d bellard
                        break;
2647 81d0912d bellard
                    default:
2648 376253ec aliguori
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2649 81d0912d bellard
                        break;
2650 81d0912d bellard
                    }
2651 9307c4c1 bellard
                    goto fail;
2652 9307c4c1 bellard
                }
2653 9307c4c1 bellard
                str = qemu_malloc(strlen(buf) + 1);
2654 363a37d5 blueswir1
                pstrcpy(str, sizeof(buf), buf);
2655 9307c4c1 bellard
                str_allocated[nb_args] = str;
2656 9307c4c1 bellard
            add_str:
2657 9307c4c1 bellard
                if (nb_args >= MAX_ARGS) {
2658 9307c4c1 bellard
                error_args:
2659 376253ec aliguori
                    monitor_printf(mon, "%s: too many arguments\n", cmdname);
2660 9307c4c1 bellard
                    goto fail;
2661 9307c4c1 bellard
                }
2662 9307c4c1 bellard
                args[nb_args++] = str;
2663 9307c4c1 bellard
            }
2664 9dc39cba bellard
            break;
2665 9307c4c1 bellard
        case '/':
2666 9307c4c1 bellard
            {
2667 9307c4c1 bellard
                int count, format, size;
2668 3b46e624 ths
2669 cd390083 blueswir1
                while (qemu_isspace(*p))
2670 9307c4c1 bellard
                    p++;
2671 9307c4c1 bellard
                if (*p == '/') {
2672 9307c4c1 bellard
                    /* format found */
2673 9307c4c1 bellard
                    p++;
2674 9307c4c1 bellard
                    count = 1;
2675 cd390083 blueswir1
                    if (qemu_isdigit(*p)) {
2676 9307c4c1 bellard
                        count = 0;
2677 cd390083 blueswir1
                        while (qemu_isdigit(*p)) {
2678 9307c4c1 bellard
                            count = count * 10 + (*p - '0');
2679 9307c4c1 bellard
                            p++;
2680 9307c4c1 bellard
                        }
2681 9307c4c1 bellard
                    }
2682 9307c4c1 bellard
                    size = -1;
2683 9307c4c1 bellard
                    format = -1;
2684 9307c4c1 bellard
                    for(;;) {
2685 9307c4c1 bellard
                        switch(*p) {
2686 9307c4c1 bellard
                        case 'o':
2687 9307c4c1 bellard
                        case 'd':
2688 9307c4c1 bellard
                        case 'u':
2689 9307c4c1 bellard
                        case 'x':
2690 9307c4c1 bellard
                        case 'i':
2691 9307c4c1 bellard
                        case 'c':
2692 9307c4c1 bellard
                            format = *p++;
2693 9307c4c1 bellard
                            break;
2694 9307c4c1 bellard
                        case 'b':
2695 9307c4c1 bellard
                            size = 1;
2696 9307c4c1 bellard
                            p++;
2697 9307c4c1 bellard
                            break;
2698 9307c4c1 bellard
                        case 'h':
2699 9307c4c1 bellard
                            size = 2;
2700 9307c4c1 bellard
                            p++;
2701 9307c4c1 bellard
                            break;
2702 9307c4c1 bellard
                        case 'w':
2703 9307c4c1 bellard
                            size = 4;
2704 9307c4c1 bellard
                            p++;
2705 9307c4c1 bellard
                            break;
2706 9307c4c1 bellard
                        case 'g':
2707 9307c4c1 bellard
                        case 'L':
2708 9307c4c1 bellard
                            size = 8;
2709 9307c4c1 bellard
                            p++;
2710 9307c4c1 bellard
                            break;
2711 9307c4c1 bellard
                        default:
2712 9307c4c1 bellard
                            goto next;
2713 9307c4c1 bellard
                        }
2714 9307c4c1 bellard
                    }
2715 9307c4c1 bellard
                next:
2716 cd390083 blueswir1
                    if (*p != '\0' && !qemu_isspace(*p)) {
2717 376253ec aliguori
                        monitor_printf(mon, "invalid char in format: '%c'\n",
2718 376253ec aliguori
                                       *p);
2719 9307c4c1 bellard
                        goto fail;
2720 9307c4c1 bellard
                    }
2721 9307c4c1 bellard
                    if (format < 0)
2722 9307c4c1 bellard
                        format = default_fmt_format;
2723 4c27ba27 bellard
                    if (format != 'i') {
2724 4c27ba27 bellard
                        /* for 'i', not specifying a size gives -1 as size */
2725 4c27ba27 bellard
                        if (size < 0)
2726 4c27ba27 bellard
                            size = default_fmt_size;
2727 e90f009b aurel32
                        default_fmt_size = size;
2728 4c27ba27 bellard
                    }
2729 9307c4c1 bellard
                    default_fmt_format = format;
2730 9307c4c1 bellard
                } else {
2731 9307c4c1 bellard
                    count = 1;
2732 9307c4c1 bellard
                    format = default_fmt_format;
2733 4c27ba27 bellard
                    if (format != 'i') {
2734 4c27ba27 bellard
                        size = default_fmt_size;
2735 4c27ba27 bellard
                    } else {
2736 4c27ba27 bellard
                        size = -1;
2737 4c27ba27 bellard
                    }
2738 9307c4c1 bellard
                }
2739 9307c4c1 bellard
                if (nb_args + 3 > MAX_ARGS)
2740 9307c4c1 bellard
                    goto error_args;
2741 1c5bf3bf j_mayer
                args[nb_args++] = (void*)(long)count;
2742 1c5bf3bf j_mayer
                args[nb_args++] = (void*)(long)format;
2743 1c5bf3bf j_mayer
                args[nb_args++] = (void*)(long)size;
2744 9307c4c1 bellard
            }
2745 9dc39cba bellard
            break;
2746 9307c4c1 bellard
        case 'i':
2747 92a31b1f bellard
        case 'l':
2748 9307c4c1 bellard
            {
2749 c2efc95d blueswir1
                int64_t val;
2750 7743e588 blueswir1
2751 cd390083 blueswir1
                while (qemu_isspace(*p))
2752 9307c4c1 bellard
                    p++;
2753 3440557b bellard
                if (*typestr == '?' || *typestr == '.') {
2754 3440557b bellard
                    if (*typestr == '?') {
2755 3440557b bellard
                        if (*p == '\0')
2756 3440557b bellard
                            has_arg = 0;
2757 3440557b bellard
                        else
2758 3440557b bellard
                            has_arg = 1;
2759 3440557b bellard
                    } else {
2760 3440557b bellard
                        if (*p == '.') {
2761 3440557b bellard
                            p++;
2762 cd390083 blueswir1
                            while (qemu_isspace(*p))
2763 3440557b bellard
                                p++;
2764 3440557b bellard
                            has_arg = 1;
2765 3440557b bellard
                        } else {
2766 3440557b bellard
                            has_arg = 0;
2767 3440557b bellard
                        }
2768 3440557b bellard
                    }
2769 13224a87 bellard
                    typestr++;
2770 9307c4c1 bellard
                    if (nb_args >= MAX_ARGS)
2771 9307c4c1 bellard
                        goto error_args;
2772 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)has_arg;
2773 9307c4c1 bellard
                    if (!has_arg) {
2774 9307c4c1 bellard
                        if (nb_args >= MAX_ARGS)
2775 9307c4c1 bellard
                            goto error_args;
2776 9307c4c1 bellard
                        val = -1;
2777 9307c4c1 bellard
                        goto add_num;
2778 9307c4c1 bellard
                    }
2779 9307c4c1 bellard
                }
2780 376253ec aliguori
                if (get_expr(mon, &val, &p))
2781 9307c4c1 bellard
                    goto fail;
2782 9307c4c1 bellard
            add_num:
2783 92a31b1f bellard
                if (c == 'i') {
2784 92a31b1f bellard
                    if (nb_args >= MAX_ARGS)
2785 92a31b1f bellard
                        goto error_args;
2786 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)val;
2787 92a31b1f bellard
                } else {
2788 92a31b1f bellard
                    if ((nb_args + 1) >= MAX_ARGS)
2789 92a31b1f bellard
                        goto error_args;
2790 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS > 32
2791 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2792 92a31b1f bellard
#else
2793 92a31b1f bellard
                    args[nb_args++] = (void *)0;
2794 92a31b1f bellard
#endif
2795 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)(val & 0xffffffff);
2796 92a31b1f bellard
                }
2797 9307c4c1 bellard
            }
2798 9307c4c1 bellard
            break;
2799 9307c4c1 bellard
        case '-':
2800 9307c4c1 bellard
            {
2801 9307c4c1 bellard
                int has_option;
2802 9307c4c1 bellard
                /* option */
2803 3b46e624 ths
2804 9307c4c1 bellard
                c = *typestr++;
2805 9307c4c1 bellard
                if (c == '\0')
2806 9307c4c1 bellard
                    goto bad_type;
2807 cd390083 blueswir1
                while (qemu_isspace(*p))
2808 9307c4c1 bellard
                    p++;
2809 9307c4c1 bellard
                has_option = 0;
2810 9307c4c1 bellard
                if (*p == '-') {
2811 9307c4c1 bellard
                    p++;
2812 9307c4c1 bellard
                    if (*p != c) {
2813 376253ec aliguori
                        monitor_printf(mon, "%s: unsupported option -%c\n",
2814 376253ec aliguori
                                       cmdname, *p);
2815 9307c4c1 bellard
                        goto fail;
2816 9307c4c1 bellard
                    }
2817 9307c4c1 bellard
                    p++;
2818 9307c4c1 bellard
                    has_option = 1;
2819 9307c4c1 bellard
                }
2820 9307c4c1 bellard
                if (nb_args >= MAX_ARGS)
2821 9307c4c1 bellard
                    goto error_args;
2822 1c5bf3bf j_mayer
                args[nb_args++] = (void *)(long)has_option;
2823 9307c4c1 bellard
            }
2824 9307c4c1 bellard
            break;
2825 9307c4c1 bellard
        default:
2826 9307c4c1 bellard
        bad_type:
2827 376253ec aliguori
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2828 9307c4c1 bellard
            goto fail;
2829 9307c4c1 bellard
        }
2830 9dc39cba bellard
    }
2831 9307c4c1 bellard
    /* check that all arguments were parsed */
2832 cd390083 blueswir1
    while (qemu_isspace(*p))
2833 9307c4c1 bellard
        p++;
2834 9307c4c1 bellard
    if (*p != '\0') {
2835 376253ec aliguori
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2836 376253ec aliguori
                       cmdname);
2837 9307c4c1 bellard
        goto fail;
2838 9dc39cba bellard
    }
2839 9307c4c1 bellard
2840 9307c4c1 bellard
    switch(nb_args) {
2841 9307c4c1 bellard
    case 0:
2842 a5f1b965 blueswir1
        handler_0 = cmd->handler;
2843 376253ec aliguori
        handler_0(mon);
2844 9307c4c1 bellard
        break;
2845 9307c4c1 bellard
    case 1:
2846 a5f1b965 blueswir1
        handler_1 = cmd->handler;
2847 376253ec aliguori
        handler_1(mon, args[0]);
2848 9307c4c1 bellard
        break;
2849 9307c4c1 bellard
    case 2:
2850 a5f1b965 blueswir1
        handler_2 = cmd->handler;
2851 376253ec aliguori
        handler_2(mon, args[0], args[1]);
2852 9307c4c1 bellard
        break;
2853 9307c4c1 bellard
    case 3:
2854 a5f1b965 blueswir1
        handler_3 = cmd->handler;
2855 376253ec aliguori
        handler_3(mon, args[0], args[1], args[2]);
2856 9307c4c1 bellard
        break;
2857 9307c4c1 bellard
    case 4:
2858 a5f1b965 blueswir1
        handler_4 = cmd->handler;
2859 376253ec aliguori
        handler_4(mon, args[0], args[1], args[2], args[3]);
2860 9307c4c1 bellard
        break;
2861 9307c4c1 bellard
    case 5:
2862 a5f1b965 blueswir1
        handler_5 = cmd->handler;
2863 376253ec aliguori
        handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2864 9307c4c1 bellard
        break;
2865 3440557b bellard
    case 6:
2866 a5f1b965 blueswir1
        handler_6 = cmd->handler;
2867 376253ec aliguori
        handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2868 3440557b bellard
        break;
2869 ec36b695 bellard
    case 7:
2870 a5f1b965 blueswir1
        handler_7 = cmd->handler;
2871 376253ec aliguori
        handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2872 376253ec aliguori
                  args[6]);
2873 ec36b695 bellard
        break;
2874 79c4f6b0 Huang Ying
    case 8:
2875 79c4f6b0 Huang Ying
        handler_8 = cmd->handler;
2876 79c4f6b0 Huang Ying
        handler_8(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2877 79c4f6b0 Huang Ying
                  args[6], args[7]);
2878 79c4f6b0 Huang Ying
        break;
2879 79c4f6b0 Huang Ying
    case 9:
2880 79c4f6b0 Huang Ying
        handler_9 = cmd->handler;
2881 79c4f6b0 Huang Ying
        handler_9(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2882 79c4f6b0 Huang Ying
                  args[6], args[7], args[8]);
2883 79c4f6b0 Huang Ying
        break;
2884 79c4f6b0 Huang Ying
    case 10:
2885 79c4f6b0 Huang Ying
        handler_10 = cmd->handler;
2886 79c4f6b0 Huang Ying
        handler_10(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2887 79c4f6b0 Huang Ying
                   args[6], args[7], args[8], args[9]);
2888 79c4f6b0 Huang Ying
        break;
2889 9307c4c1 bellard
    default:
2890 376253ec aliguori
        monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2891 9307c4c1 bellard
        goto fail;
2892 9dc39cba bellard
    }
2893 9307c4c1 bellard
 fail:
2894 9307c4c1 bellard
    for(i = 0; i < MAX_ARGS; i++)
2895 9307c4c1 bellard
        qemu_free(str_allocated[i]);
2896 9dc39cba bellard
}
2897 9dc39cba bellard
2898 81d0912d bellard
static void cmd_completion(const char *name, const char *list)
2899 81d0912d bellard
{
2900 81d0912d bellard
    const char *p, *pstart;
2901 81d0912d bellard
    char cmd[128];
2902 81d0912d bellard
    int len;
2903 81d0912d bellard
2904 81d0912d bellard
    p = list;
2905 81d0912d bellard
    for(;;) {
2906 81d0912d bellard
        pstart = p;
2907 81d0912d bellard
        p = strchr(p, '|');
2908 81d0912d bellard
        if (!p)
2909 81d0912d bellard
            p = pstart + strlen(pstart);
2910 81d0912d bellard
        len = p - pstart;
2911 81d0912d bellard
        if (len > sizeof(cmd) - 2)
2912 81d0912d bellard
            len = sizeof(cmd) - 2;
2913 81d0912d bellard
        memcpy(cmd, pstart, len);
2914 81d0912d bellard
        cmd[len] = '\0';
2915 81d0912d bellard
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2916 731b0364 aliguori
            readline_add_completion(cur_mon->rs, cmd);
2917 81d0912d bellard
        }
2918 81d0912d bellard
        if (*p == '\0')
2919 81d0912d bellard
            break;
2920 81d0912d bellard
        p++;
2921 81d0912d bellard
    }
2922 81d0912d bellard
}
2923 81d0912d bellard
2924 81d0912d bellard
static void file_completion(const char *input)
2925 81d0912d bellard
{
2926 81d0912d bellard
    DIR *ffs;
2927 81d0912d bellard
    struct dirent *d;
2928 81d0912d bellard
    char path[1024];
2929 81d0912d bellard
    char file[1024], file_prefix[1024];
2930 81d0912d bellard
    int input_path_len;
2931 81d0912d bellard
    const char *p;
2932 81d0912d bellard
2933 5fafdf24 ths
    p = strrchr(input, '/');
2934 81d0912d bellard
    if (!p) {
2935 81d0912d bellard
        input_path_len = 0;
2936 81d0912d bellard
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2937 363a37d5 blueswir1
        pstrcpy(path, sizeof(path), ".");
2938 81d0912d bellard
    } else {
2939 81d0912d bellard
        input_path_len = p - input + 1;
2940 81d0912d bellard
        memcpy(path, input, input_path_len);
2941 81d0912d bellard
        if (input_path_len > sizeof(path) - 1)
2942 81d0912d bellard
            input_path_len = sizeof(path) - 1;
2943 81d0912d bellard
        path[input_path_len] = '\0';
2944 81d0912d bellard
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2945 81d0912d bellard
    }
2946 81d0912d bellard
#ifdef DEBUG_COMPLETION
2947 376253ec aliguori
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2948 376253ec aliguori
                   input, path, file_prefix);
2949 81d0912d bellard
#endif
2950 81d0912d bellard
    ffs = opendir(path);
2951 81d0912d bellard
    if (!ffs)
2952 81d0912d bellard
        return;
2953 81d0912d bellard
    for(;;) {
2954 81d0912d bellard
        struct stat sb;
2955 81d0912d bellard
        d = readdir(ffs);
2956 81d0912d bellard
        if (!d)
2957 81d0912d bellard
            break;
2958 81d0912d bellard
        if (strstart(d->d_name, file_prefix, NULL)) {
2959 81d0912d bellard
            memcpy(file, input, input_path_len);
2960 363a37d5 blueswir1
            if (input_path_len < sizeof(file))
2961 363a37d5 blueswir1
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2962 363a37d5 blueswir1
                        d->d_name);
2963 81d0912d bellard
            /* stat the file to find out if it's a directory.
2964 81d0912d bellard
             * In that case add a slash to speed up typing long paths
2965 81d0912d bellard
             */
2966 81d0912d bellard
            stat(file, &sb);
2967 81d0912d bellard
            if(S_ISDIR(sb.st_mode))
2968 363a37d5 blueswir1
                pstrcat(file, sizeof(file), "/");
2969 731b0364 aliguori
            readline_add_completion(cur_mon->rs, file);
2970 81d0912d bellard
        }
2971 81d0912d bellard
    }
2972 81d0912d bellard
    closedir(ffs);
2973 81d0912d bellard
}
2974 81d0912d bellard
2975 51de9760 aliguori
static void block_completion_it(void *opaque, BlockDriverState *bs)
2976 81d0912d bellard
{
2977 51de9760 aliguori
    const char *name = bdrv_get_device_name(bs);
2978 81d0912d bellard
    const char *input = opaque;
2979 81d0912d bellard
2980 81d0912d bellard
    if (input[0] == '\0' ||
2981 81d0912d bellard
        !strncmp(name, (char *)input, strlen(input))) {
2982 731b0364 aliguori
        readline_add_completion(cur_mon->rs, name);
2983 81d0912d bellard
    }
2984 81d0912d bellard
}
2985 81d0912d bellard
2986 81d0912d bellard
/* NOTE: this parser is an approximate form of the real command parser */
2987 81d0912d bellard
static void parse_cmdline(const char *cmdline,
2988 81d0912d bellard
                         int *pnb_args, char **args)
2989 81d0912d bellard
{
2990 81d0912d bellard
    const char *p;
2991 81d0912d bellard
    int nb_args, ret;
2992 81d0912d bellard
    char buf[1024];
2993 81d0912d bellard
2994 81d0912d bellard
    p = cmdline;
2995 81d0912d bellard
    nb_args = 0;
2996 81d0912d bellard
    for(;;) {
2997 cd390083 blueswir1
        while (qemu_isspace(*p))
2998 81d0912d bellard
            p++;
2999 81d0912d bellard
        if (*p == '\0')
3000 81d0912d bellard
            break;
3001 81d0912d bellard
        if (nb_args >= MAX_ARGS)
3002 81d0912d bellard
            break;
3003 81d0912d bellard
        ret = get_str(buf, sizeof(buf), &p);
3004 81d0912d bellard
        args[nb_args] = qemu_strdup(buf);
3005 81d0912d bellard
        nb_args++;
3006 81d0912d bellard
        if (ret < 0)
3007 81d0912d bellard
            break;
3008 81d0912d bellard
    }
3009 81d0912d bellard
    *pnb_args = nb_args;
3010 81d0912d bellard
}
3011 81d0912d bellard
3012 4c36ba32 aliguori
static void monitor_find_completion(const char *cmdline)
3013 81d0912d bellard
{
3014 81d0912d bellard
    const char *cmdname;
3015 81d0912d bellard
    char *args[MAX_ARGS];
3016 81d0912d bellard
    int nb_args, i, len;
3017 81d0912d bellard
    const char *ptype, *str;
3018 376253ec aliguori
    const mon_cmd_t *cmd;
3019 64866c3d bellard
    const KeyDef *key;
3020 81d0912d bellard
3021 81d0912d bellard
    parse_cmdline(cmdline, &nb_args, args);
3022 81d0912d bellard
#ifdef DEBUG_COMPLETION
3023 81d0912d bellard
    for(i = 0; i < nb_args; i++) {
3024 376253ec aliguori
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3025 81d0912d bellard
    }
3026 81d0912d bellard
#endif
3027 81d0912d bellard
3028 81d0912d bellard
    /* if the line ends with a space, it means we want to complete the
3029 81d0912d bellard
       next arg */
3030 81d0912d bellard
    len = strlen(cmdline);
3031 cd390083 blueswir1
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3032 81d0912d bellard
        if (nb_args >= MAX_ARGS)
3033 81d0912d bellard
            return;
3034 81d0912d bellard
        args[nb_args++] = qemu_strdup("");
3035 81d0912d bellard
    }
3036 81d0912d bellard
    if (nb_args <= 1) {
3037 81d0912d bellard
        /* command completion */
3038 81d0912d bellard
        if (nb_args == 0)
3039 81d0912d bellard
            cmdname = "";
3040 81d0912d bellard
        else
3041 81d0912d bellard
            cmdname = args[0];
3042 731b0364 aliguori
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3043 376253ec aliguori
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3044 81d0912d bellard
            cmd_completion(cmdname, cmd->name);
3045 81d0912d bellard
        }
3046 81d0912d bellard
    } else {
3047 81d0912d bellard
        /* find the command */
3048 376253ec aliguori
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3049 81d0912d bellard
            if (compare_cmd(args[0], cmd->name))
3050 81d0912d bellard
                goto found;
3051 81d0912d bellard
        }
3052 81d0912d bellard
        return;
3053 81d0912d bellard
    found:
3054 81d0912d bellard
        ptype = cmd->args_type;
3055 81d0912d bellard
        for(i = 0; i < nb_args - 2; i++) {
3056 81d0912d bellard
            if (*ptype != '\0') {
3057 81d0912d bellard
                ptype++;
3058 81d0912d bellard
                while (*ptype == '?')
3059 81d0912d bellard
                    ptype++;
3060 81d0912d bellard
            }
3061 81d0912d bellard
        }
3062 81d0912d bellard
        str = args[nb_args - 1];
3063 81d0912d bellard
        switch(*ptype) {
3064 81d0912d bellard
        case 'F':
3065 81d0912d bellard
            /* file completion */
3066 731b0364 aliguori
            readline_set_completion_index(cur_mon->rs, strlen(str));
3067 81d0912d bellard
            file_completion(str);
3068 81d0912d bellard
            break;
3069 81d0912d bellard
        case 'B':
3070 81d0912d bellard
            /* block device name completion */
3071 731b0364 aliguori
            readline_set_completion_index(cur_mon->rs, strlen(str));
3072 81d0912d bellard
            bdrv_iterate(block_completion_it, (void *)str);
3073 81d0912d bellard
            break;
3074 7fe48483 bellard
        case 's':
3075 7fe48483 bellard
            /* XXX: more generic ? */
3076 7fe48483 bellard
            if (!strcmp(cmd->name, "info")) {
3077 731b0364 aliguori
                readline_set_completion_index(cur_mon->rs, strlen(str));
3078 7fe48483 bellard
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3079 7fe48483 bellard
                    cmd_completion(str, cmd->name);
3080 7fe48483 bellard
                }
3081 64866c3d bellard
            } else if (!strcmp(cmd->name, "sendkey")) {
3082 e600d1ef blueswir1
                char *sep = strrchr(str, '-');
3083 e600d1ef blueswir1
                if (sep)
3084 e600d1ef blueswir1
                    str = sep + 1;
3085 731b0364 aliguori
                readline_set_completion_index(cur_mon->rs, strlen(str));
3086 64866c3d bellard
                for(key = key_defs; key->name != NULL; key++) {
3087 64866c3d bellard
                    cmd_completion(str, key->name);
3088 64866c3d bellard
                }
3089 f3353c6b Jan Kiszka
            } else if (!strcmp(cmd->name, "help|?")) {
3090 f3353c6b Jan Kiszka
                readline_set_completion_index(cur_mon->rs, strlen(str));
3091 f3353c6b Jan Kiszka
                for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3092 f3353c6b Jan Kiszka
                    cmd_completion(str, cmd->name);
3093 f3353c6b Jan Kiszka
                }
3094 7fe48483 bellard
            }
3095 7fe48483 bellard
            break;
3096 81d0912d bellard
        default:
3097 81d0912d bellard
            break;
3098 81d0912d bellard
        }
3099 81d0912d bellard
    }
3100 81d0912d bellard
    for(i = 0; i < nb_args; i++)
3101 81d0912d bellard
        qemu_free(args[i]);
3102 81d0912d bellard
}
3103 81d0912d bellard
3104 731b0364 aliguori
static int monitor_can_read(void *opaque)
3105 9dc39cba bellard
{
3106 731b0364 aliguori
    Monitor *mon = opaque;
3107 731b0364 aliguori
3108 731b0364 aliguori
    return (mon->suspend_cnt == 0) ? 128 : 0;
3109 9dc39cba bellard
}
3110 9dc39cba bellard
3111 731b0364 aliguori
static void monitor_read(void *opaque, const uint8_t *buf, int size)
3112 9dc39cba bellard
{
3113 731b0364 aliguori
    Monitor *old_mon = cur_mon;
3114 7e2515e8 bellard
    int i;
3115 376253ec aliguori
3116 731b0364 aliguori
    cur_mon = opaque;
3117 731b0364 aliguori
3118 cde76ee1 aliguori
    if (cur_mon->rs) {
3119 cde76ee1 aliguori
        for (i = 0; i < size; i++)
3120 cde76ee1 aliguori
            readline_handle_byte(cur_mon->rs, buf[i]);
3121 cde76ee1 aliguori
    } else {
3122 cde76ee1 aliguori
        if (size == 0 || buf[size - 1] != 0)
3123 cde76ee1 aliguori
            monitor_printf(cur_mon, "corrupted command\n");
3124 cde76ee1 aliguori
        else
3125 cde76ee1 aliguori
            monitor_handle_command(cur_mon, (char *)buf);
3126 cde76ee1 aliguori
    }
3127 9dc39cba bellard
3128 731b0364 aliguori
    cur_mon = old_mon;
3129 731b0364 aliguori
}
3130 d8f44609 aliguori
3131 376253ec aliguori
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3132 aa455485 bellard
{
3133 731b0364 aliguori
    monitor_suspend(mon);
3134 376253ec aliguori
    monitor_handle_command(mon, cmdline);
3135 731b0364 aliguori
    monitor_resume(mon);
3136 d8f44609 aliguori
}
3137 d8f44609 aliguori
3138 cde76ee1 aliguori
int monitor_suspend(Monitor *mon)
3139 d8f44609 aliguori
{
3140 cde76ee1 aliguori
    if (!mon->rs)
3141 cde76ee1 aliguori
        return -ENOTTY;
3142 731b0364 aliguori
    mon->suspend_cnt++;
3143 cde76ee1 aliguori
    return 0;
3144 d8f44609 aliguori
}
3145 d8f44609 aliguori
3146 376253ec aliguori
void monitor_resume(Monitor *mon)
3147 d8f44609 aliguori
{
3148 cde76ee1 aliguori
    if (!mon->rs)
3149 cde76ee1 aliguori
        return;
3150 731b0364 aliguori
    if (--mon->suspend_cnt == 0)
3151 731b0364 aliguori
        readline_show_prompt(mon->rs);
3152 aa455485 bellard
}
3153 aa455485 bellard
3154 731b0364 aliguori
static void monitor_event(void *opaque, int event)
3155 86e94dea ths
{
3156 376253ec aliguori
    Monitor *mon = opaque;
3157 376253ec aliguori
3158 2724b180 aliguori
    switch (event) {
3159 2724b180 aliguori
    case CHR_EVENT_MUX_IN:
3160 2724b180 aliguori
        readline_restart(mon->rs);
3161 2724b180 aliguori
        monitor_resume(mon);
3162 2724b180 aliguori
        monitor_flush(mon);
3163 2724b180 aliguori
        break;
3164 2724b180 aliguori
3165 2724b180 aliguori
    case CHR_EVENT_MUX_OUT:
3166 2724b180 aliguori
        if (mon->suspend_cnt == 0)
3167 2724b180 aliguori
            monitor_printf(mon, "\n");
3168 2724b180 aliguori
        monitor_flush(mon);
3169 2724b180 aliguori
        monitor_suspend(mon);
3170 2724b180 aliguori
        break;
3171 86e94dea ths
3172 2724b180 aliguori
    case CHR_EVENT_RESET:
3173 2724b180 aliguori
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3174 2724b180 aliguori
                       "information\n", QEMU_VERSION);
3175 2724b180 aliguori
        if (mon->chr->focus == 0)
3176 2724b180 aliguori
            readline_show_prompt(mon->rs);
3177 2724b180 aliguori
        break;
3178 2724b180 aliguori
    }
3179 86e94dea ths
}
3180 86e94dea ths
3181 76655d6d aliguori
3182 76655d6d aliguori
/*
3183 76655d6d aliguori
 * Local variables:
3184 76655d6d aliguori
 *  c-indent-level: 4
3185 76655d6d aliguori
 *  c-basic-offset: 4
3186 76655d6d aliguori
 *  tab-width: 8
3187 76655d6d aliguori
 * End:
3188 76655d6d aliguori
 */
3189 76655d6d aliguori
3190 731b0364 aliguori
void monitor_init(CharDriverState *chr, int flags)
3191 aa455485 bellard
{
3192 731b0364 aliguori
    static int is_first_init = 1;
3193 87127161 aliguori
    Monitor *mon;
3194 20d8a3ed ths
3195 20d8a3ed ths
    if (is_first_init) {
3196 c8256f9d balrog
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3197 20d8a3ed ths
        is_first_init = 0;
3198 20d8a3ed ths
    }
3199 87127161 aliguori
3200 87127161 aliguori
    mon = qemu_mallocz(sizeof(*mon));
3201 20d8a3ed ths
3202 87127161 aliguori
    mon->chr = chr;
3203 731b0364 aliguori
    mon->flags = flags;
3204 2724b180 aliguori
    if (mon->chr->focus != 0)
3205 2724b180 aliguori
        mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3206 cde76ee1 aliguori
    if (flags & MONITOR_USE_READLINE) {
3207 cde76ee1 aliguori
        mon->rs = readline_init(mon, monitor_find_completion);
3208 cde76ee1 aliguori
        monitor_read_command(mon, 0);
3209 cde76ee1 aliguori
    }
3210 87127161 aliguori
3211 731b0364 aliguori
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3212 731b0364 aliguori
                          mon);
3213 87127161 aliguori
3214 87127161 aliguori
    LIST_INSERT_HEAD(&mon_list, mon, entry);
3215 731b0364 aliguori
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3216 87127161 aliguori
        cur_mon = mon;
3217 aa455485 bellard
}
3218 aa455485 bellard
3219 376253ec aliguori
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3220 81d0912d bellard
{
3221 bb5fc20f aliguori
    BlockDriverState *bs = opaque;
3222 bb5fc20f aliguori
    int ret = 0;
3223 81d0912d bellard
3224 bb5fc20f aliguori
    if (bdrv_set_key(bs, password) != 0) {
3225 376253ec aliguori
        monitor_printf(mon, "invalid password\n");
3226 bb5fc20f aliguori
        ret = -EPERM;
3227 9dc39cba bellard
    }
3228 731b0364 aliguori
    if (mon->password_completion_cb)
3229 731b0364 aliguori
        mon->password_completion_cb(mon->password_opaque, ret);
3230 bb5fc20f aliguori
3231 731b0364 aliguori
    monitor_read_command(mon, 1);
3232 9dc39cba bellard
}
3233 c0f4ce77 aliguori
3234 376253ec aliguori
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3235 bb5fc20f aliguori
                                 BlockDriverCompletionFunc *completion_cb,
3236 bb5fc20f aliguori
                                 void *opaque)
3237 c0f4ce77 aliguori
{
3238 cde76ee1 aliguori
    int err;
3239 cde76ee1 aliguori
3240 bb5fc20f aliguori
    if (!bdrv_key_required(bs)) {
3241 bb5fc20f aliguori
        if (completion_cb)
3242 bb5fc20f aliguori
            completion_cb(opaque, 0);
3243 bb5fc20f aliguori
        return;
3244 bb5fc20f aliguori
    }
3245 c0f4ce77 aliguori
3246 376253ec aliguori
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3247 376253ec aliguori
                   bdrv_get_encrypted_filename(bs));
3248 bb5fc20f aliguori
3249 731b0364 aliguori
    mon->password_completion_cb = completion_cb;
3250 731b0364 aliguori
    mon->password_opaque = opaque;
3251 bb5fc20f aliguori
3252 cde76ee1 aliguori
    err = monitor_read_password(mon, bdrv_password_cb, bs);
3253 cde76ee1 aliguori
3254 cde76ee1 aliguori
    if (err && completion_cb)
3255 cde76ee1 aliguori
        completion_cb(opaque, err);
3256 c0f4ce77 aliguori
}