Statistics
| Branch: | Revision:

root / monitor.c @ d7f9b689

History | View | Annotate | Download (90.8 kB)

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