Statistics
| Branch: | Revision:

root / monitor.c @ fb46660e

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