Statistics
| Branch: | Revision:

root / monitor.c @ 0bbc47bb

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