Statistics
| Branch: | Revision:

root / monitor.c @ 13917bee

History | View | Annotate | Download (91.3 kB)

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