Statistics
| Branch: | Revision:

root / monitor.c @ d49f626e

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