Statistics
| Branch: | Revision:

root / monitor.c @ 20420430

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