Statistics
| Branch: | Revision:

root / monitor.c @ 5d6c37fb

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