Statistics
| Branch: | Revision:

root / monitor.c @ 72cf2d4f

History | View | Annotate | Download (87 kB)

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