Statistics
| Branch: | Revision:

root / monitor.c @ 3350a4dd

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