Statistics
| Branch: | Revision:

root / monitor.c @ 83fb1de2

History | View | Annotate | Download (92.1 kB)

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