Statistics
| Branch: | Revision:

root / monitor.c @ 1e5b9d2f

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