Statistics
| Branch: | Revision:

root / monitor.c @ f3c157c4

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