Statistics
| Branch: | Revision:

root / monitor.c @ d5a7b38f

History | View | Annotate | Download (122.2 kB)

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