Statistics
| Branch: | Revision:

root / monitor.c @ 0f89cc7b

History | View | Annotate | Download (123.8 kB)

1 9dc39cba bellard
/*
2 9dc39cba bellard
 * QEMU monitor
3 5fafdf24 ths
 *
4 9dc39cba bellard
 * Copyright (c) 2003-2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 9dc39cba bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 9dc39cba bellard
 * of this software and associated documentation files (the "Software"), to deal
8 9dc39cba bellard
 * in the Software without restriction, including without limitation the rights
9 9dc39cba bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 9dc39cba bellard
 * copies of the Software, and to permit persons to whom the Software is
11 9dc39cba bellard
 * furnished to do so, subject to the following conditions:
12 9dc39cba bellard
 *
13 9dc39cba bellard
 * The above copyright notice and this permission notice shall be included in
14 9dc39cba bellard
 * all copies or substantial portions of the Software.
15 9dc39cba bellard
 *
16 9dc39cba bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 9dc39cba bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 9dc39cba bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 9dc39cba bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 9dc39cba bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 9dc39cba bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 9dc39cba bellard
 * THE SOFTWARE.
23 9dc39cba bellard
 */
24 511d2b14 blueswir1
#include <dirent.h>
25 87ecb68b pbrook
#include "hw/hw.h"
26 cae4956e Gerd Hoffmann
#include "hw/qdev.h"
27 87ecb68b pbrook
#include "hw/usb.h"
28 87ecb68b pbrook
#include "hw/pcmcia.h"
29 87ecb68b pbrook
#include "hw/pc.h"
30 87ecb68b pbrook
#include "hw/pci.h"
31 9dd986cc Richard W.M. Jones
#include "hw/watchdog.h"
32 45a50b16 Gerd Hoffmann
#include "hw/loader.h"
33 87ecb68b pbrook
#include "gdbstub.h"
34 87ecb68b pbrook
#include "net.h"
35 68ac40d2 Mark McLoughlin
#include "net/slirp.h"
36 87ecb68b pbrook
#include "qemu-char.h"
37 87ecb68b pbrook
#include "sysemu.h"
38 376253ec aliguori
#include "monitor.h"
39 376253ec aliguori
#include "readline.h"
40 87ecb68b pbrook
#include "console.h"
41 87ecb68b pbrook
#include "block.h"
42 87ecb68b pbrook
#include "audio/audio.h"
43 9307c4c1 bellard
#include "disas.h"
44 df751fa8 aliguori
#include "balloon.h"
45 c8256f9d balrog
#include "qemu-timer.h"
46 5bb7910a aliguori
#include "migration.h"
47 7ba1e619 aliguori
#include "kvm.h"
48 76655d6d aliguori
#include "acl.h"
49 f7188bbe Luiz Capitulino
#include "qint.h"
50 3350a4dd Markus Armbruster
#include "qfloat.h"
51 8f3cec0b Luiz Capitulino
#include "qlist.h"
52 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 6ed2c484 Luiz Capitulino
        case QEVENT_RESUME:
427 6ed2c484 Luiz Capitulino
            event_name = "RESUME";
428 6ed2c484 Luiz Capitulino
            break;
429 586153d9 Luiz Capitulino
        case QEVENT_VNC_CONNECTED:
430 586153d9 Luiz Capitulino
            event_name = "VNC_CONNECTED";
431 586153d9 Luiz Capitulino
            break;
432 0d2ed46a Luiz Capitulino
        case QEVENT_VNC_INITIALIZED:
433 0d2ed46a Luiz Capitulino
            event_name = "VNC_INITIALIZED";
434 0d2ed46a Luiz Capitulino
            break;
435 0d72f3d3 Luiz Capitulino
        case QEVENT_VNC_DISCONNECTED:
436 0d72f3d3 Luiz Capitulino
            event_name = "VNC_DISCONNECTED";
437 0d72f3d3 Luiz Capitulino
            break;
438 aa1db6ed Luiz Capitulino
        case QEVENT_BLOCK_IO_ERROR:
439 aa1db6ed Luiz Capitulino
            event_name = "BLOCK_IO_ERROR";
440 aa1db6ed Luiz Capitulino
            break;
441 80cd3478 Luiz Capitulino
        case QEVENT_RTC_CHANGE:
442 80cd3478 Luiz Capitulino
            event_name = "RTC_CHANGE";
443 80cd3478 Luiz Capitulino
            break;
444 9eedeb3b Luiz Capitulino
        case QEVENT_WATCHDOG:
445 9eedeb3b Luiz Capitulino
            event_name = "WATCHDOG";
446 9eedeb3b Luiz Capitulino
            break;
447 0d1ea871 Luiz Capitulino
        default:
448 0d1ea871 Luiz Capitulino
            abort();
449 0d1ea871 Luiz Capitulino
            break;
450 0d1ea871 Luiz Capitulino
    }
451 0d1ea871 Luiz Capitulino
452 0d1ea871 Luiz Capitulino
    qmp = qdict_new();
453 0d1ea871 Luiz Capitulino
    timestamp_put(qmp);
454 0d1ea871 Luiz Capitulino
    qdict_put(qmp, "event", qstring_from_str(event_name));
455 3d72f9a2 Luiz Capitulino
    if (data) {
456 3d72f9a2 Luiz Capitulino
        qobject_incref(data);
457 0d1ea871 Luiz Capitulino
        qdict_put_obj(qmp, "data", data);
458 3d72f9a2 Luiz Capitulino
    }
459 0d1ea871 Luiz Capitulino
460 f039a563 Adam Litke
    QLIST_FOREACH(mon, &mon_list, entry) {
461 09069b19 Luiz Capitulino
        if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
462 23fabed1 Luiz Capitulino
            monitor_json_emitter(mon, QOBJECT(qmp));
463 23fabed1 Luiz Capitulino
        }
464 f039a563 Adam Litke
    }
465 0d1ea871 Luiz Capitulino
    QDECREF(qmp);
466 0d1ea871 Luiz Capitulino
}
467 0d1ea871 Luiz Capitulino
468 ef4b7eee Luiz Capitulino
static int do_qmp_capabilities(Monitor *mon, const QDict *params,
469 ef4b7eee Luiz Capitulino
                               QObject **ret_data)
470 4a7e1190 Luiz Capitulino
{
471 4a7e1190 Luiz Capitulino
    /* Will setup QMP capabilities in the future */
472 4a7e1190 Luiz Capitulino
    if (monitor_ctrl_mode(mon)) {
473 4a7e1190 Luiz Capitulino
        mon->mc->command_mode = 1;
474 4a7e1190 Luiz Capitulino
    }
475 ef4b7eee Luiz Capitulino
476 ef4b7eee Luiz Capitulino
    return 0;
477 4a7e1190 Luiz Capitulino
}
478 4a7e1190 Luiz Capitulino
479 9dc39cba bellard
static int compare_cmd(const char *name, const char *list)
480 9dc39cba bellard
{
481 9dc39cba bellard
    const char *p, *pstart;
482 9dc39cba bellard
    int len;
483 9dc39cba bellard
    len = strlen(name);
484 9dc39cba bellard
    p = list;
485 9dc39cba bellard
    for(;;) {
486 9dc39cba bellard
        pstart = p;
487 9dc39cba bellard
        p = strchr(p, '|');
488 9dc39cba bellard
        if (!p)
489 9dc39cba bellard
            p = pstart + strlen(pstart);
490 9dc39cba bellard
        if ((p - pstart) == len && !memcmp(pstart, name, len))
491 9dc39cba bellard
            return 1;
492 9dc39cba bellard
        if (*p == '\0')
493 9dc39cba bellard
            break;
494 9dc39cba bellard
        p++;
495 9dc39cba bellard
    }
496 9dc39cba bellard
    return 0;
497 9dc39cba bellard
}
498 9dc39cba bellard
499 c227f099 Anthony Liguori
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
500 376253ec aliguori
                          const char *prefix, const char *name)
501 9dc39cba bellard
{
502 c227f099 Anthony Liguori
    const mon_cmd_t *cmd;
503 9dc39cba bellard
504 9dc39cba bellard
    for(cmd = cmds; cmd->name != NULL; cmd++) {
505 9dc39cba bellard
        if (!name || !strcmp(name, cmd->name))
506 376253ec aliguori
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
507 376253ec aliguori
                           cmd->params, cmd->help);
508 9dc39cba bellard
    }
509 9dc39cba bellard
}
510 9dc39cba bellard
511 376253ec aliguori
static void help_cmd(Monitor *mon, const char *name)
512 9dc39cba bellard
{
513 9dc39cba bellard
    if (name && !strcmp(name, "info")) {
514 376253ec aliguori
        help_cmd_dump(mon, info_cmds, "info ", NULL);
515 9dc39cba bellard
    } else {
516 376253ec aliguori
        help_cmd_dump(mon, mon_cmds, "", name);
517 f193c797 bellard
        if (name && !strcmp(name, "log")) {
518 8662d656 blueswir1
            const CPULogItem *item;
519 376253ec aliguori
            monitor_printf(mon, "Log items (comma separated):\n");
520 376253ec aliguori
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
521 f193c797 bellard
            for(item = cpu_log_items; item->mask != 0; item++) {
522 376253ec aliguori
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
523 f193c797 bellard
            }
524 f193c797 bellard
        }
525 9dc39cba bellard
    }
526 9dc39cba bellard
}
527 9dc39cba bellard
528 d54908a5 Luiz Capitulino
static void do_help_cmd(Monitor *mon, const QDict *qdict)
529 38183186 Luiz Capitulino
{
530 d54908a5 Luiz Capitulino
    help_cmd(mon, qdict_get_try_str(qdict, "name"));
531 38183186 Luiz Capitulino
}
532 38183186 Luiz Capitulino
533 d54908a5 Luiz Capitulino
static void do_commit(Monitor *mon, const QDict *qdict)
534 9dc39cba bellard
{
535 751c6a17 Gerd Hoffmann
    int all_devices;
536 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
537 d54908a5 Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
538 2dc7b602 balrog
539 7954c734 bellard
    all_devices = !strcmp(device, "all");
540 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(dinfo, &drives, next) {
541 751c6a17 Gerd Hoffmann
        if (!all_devices)
542 73006d2a Luiz Capitulino
            if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
543 751c6a17 Gerd Hoffmann
                continue;
544 751c6a17 Gerd Hoffmann
        bdrv_commit(dinfo->bdrv);
545 9dc39cba bellard
    }
546 9dc39cba bellard
}
547 9dc39cba bellard
548 940cc30d Adam Litke
static void user_monitor_complete(void *opaque, QObject *ret_data)
549 940cc30d Adam Litke
{
550 940cc30d Adam Litke
    MonitorCompletionData *data = (MonitorCompletionData *)opaque; 
551 940cc30d Adam Litke
552 940cc30d Adam Litke
    if (ret_data) {
553 940cc30d Adam Litke
        data->user_print(data->mon, ret_data);
554 940cc30d Adam Litke
    }
555 940cc30d Adam Litke
    monitor_resume(data->mon);
556 940cc30d Adam Litke
    qemu_free(data);
557 940cc30d Adam Litke
}
558 940cc30d Adam Litke
559 940cc30d Adam Litke
static void qmp_monitor_complete(void *opaque, QObject *ret_data)
560 940cc30d Adam Litke
{
561 940cc30d Adam Litke
    monitor_protocol_emitter(opaque, ret_data);
562 940cc30d Adam Litke
}
563 940cc30d Adam Litke
564 940cc30d Adam Litke
static void qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
565 940cc30d Adam Litke
                                  const QDict *params)
566 940cc30d Adam Litke
{
567 940cc30d Adam Litke
    cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
568 940cc30d Adam Litke
}
569 940cc30d Adam Litke
570 940cc30d Adam Litke
static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
571 940cc30d Adam Litke
{
572 940cc30d Adam Litke
    cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
573 940cc30d Adam Litke
}
574 940cc30d Adam Litke
575 940cc30d Adam Litke
static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
576 940cc30d Adam Litke
                                   const QDict *params)
577 940cc30d Adam Litke
{
578 940cc30d Adam Litke
    int ret;
579 940cc30d Adam Litke
580 940cc30d Adam Litke
    MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
581 940cc30d Adam Litke
    cb_data->mon = mon;
582 940cc30d Adam Litke
    cb_data->user_print = cmd->user_print;
583 940cc30d Adam Litke
    monitor_suspend(mon);
584 940cc30d Adam Litke
    ret = cmd->mhandler.cmd_async(mon, params,
585 940cc30d Adam Litke
                                  user_monitor_complete, cb_data);
586 940cc30d Adam Litke
    if (ret < 0) {
587 940cc30d Adam Litke
        monitor_resume(mon);
588 940cc30d Adam Litke
        qemu_free(cb_data);
589 940cc30d Adam Litke
    }
590 940cc30d Adam Litke
}
591 940cc30d Adam Litke
592 940cc30d Adam Litke
static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
593 940cc30d Adam Litke
{
594 940cc30d Adam Litke
    int ret;
595 940cc30d Adam Litke
596 940cc30d Adam Litke
    MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
597 940cc30d Adam Litke
    cb_data->mon = mon;
598 940cc30d Adam Litke
    cb_data->user_print = cmd->user_print;
599 940cc30d Adam Litke
    monitor_suspend(mon);
600 940cc30d Adam Litke
    ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
601 940cc30d Adam Litke
    if (ret < 0) {
602 940cc30d Adam Litke
        monitor_resume(mon);
603 940cc30d Adam Litke
        qemu_free(cb_data);
604 940cc30d Adam Litke
    }
605 940cc30d Adam Litke
}
606 940cc30d Adam Litke
607 4fdc94b4 Luiz Capitulino
static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
608 9dc39cba bellard
{
609 c227f099 Anthony Liguori
    const mon_cmd_t *cmd;
610 d54908a5 Luiz Capitulino
    const char *item = qdict_get_try_str(qdict, "item");
611 9dc39cba bellard
612 956f1a0d Luiz Capitulino
    if (!item) {
613 956f1a0d Luiz Capitulino
        assert(monitor_ctrl_mode(mon) == 0);
614 9dc39cba bellard
        goto help;
615 956f1a0d Luiz Capitulino
    }
616 13c7425e Luiz Capitulino
617 13c7425e Luiz Capitulino
    for (cmd = info_cmds; cmd->name != NULL; cmd++) {
618 5fafdf24 ths
        if (compare_cmd(item, cmd->name))
619 13c7425e Luiz Capitulino
            break;
620 9dc39cba bellard
    }
621 13c7425e Luiz Capitulino
622 956f1a0d Luiz Capitulino
    if (cmd->name == NULL) {
623 956f1a0d Luiz Capitulino
        if (monitor_ctrl_mode(mon)) {
624 ab5b027e Markus Armbruster
            qerror_report(QERR_COMMAND_NOT_FOUND, item);
625 4fdc94b4 Luiz Capitulino
            return -1;
626 956f1a0d Luiz Capitulino
        }
627 13c7425e Luiz Capitulino
        goto help;
628 956f1a0d Luiz Capitulino
    }
629 13c7425e Luiz Capitulino
630 940cc30d Adam Litke
    if (monitor_handler_is_async(cmd)) {
631 940cc30d Adam Litke
        if (monitor_ctrl_mode(mon)) {
632 940cc30d Adam Litke
            qmp_async_info_handler(mon, cmd);
633 940cc30d Adam Litke
        } else {
634 940cc30d Adam Litke
            user_async_info_handler(mon, cmd);
635 940cc30d Adam Litke
        }
636 940cc30d Adam Litke
        /*
637 940cc30d Adam Litke
         * Indicate that this command is asynchronous and will not return any
638 940cc30d Adam Litke
         * data (not even empty).  Instead, the data will be returned via a
639 940cc30d Adam Litke
         * completion callback.
640 940cc30d Adam Litke
         */
641 940cc30d Adam Litke
        *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
642 940cc30d Adam Litke
    } else if (monitor_handler_ported(cmd)) {
643 13c7425e Luiz Capitulino
        cmd->mhandler.info_new(mon, ret_data);
644 25b422eb Luiz Capitulino
645 25b422eb Luiz Capitulino
        if (!monitor_ctrl_mode(mon)) {
646 25b422eb Luiz Capitulino
            /*
647 25b422eb Luiz Capitulino
             * User Protocol function is called here, Monitor Protocol is
648 25b422eb Luiz Capitulino
             * handled by monitor_call_handler()
649 25b422eb Luiz Capitulino
             */
650 25b422eb Luiz Capitulino
            if (*ret_data)
651 25b422eb Luiz Capitulino
                cmd->user_print(mon, *ret_data);
652 25b422eb Luiz Capitulino
        }
653 13c7425e Luiz Capitulino
    } else {
654 956f1a0d Luiz Capitulino
        if (monitor_ctrl_mode(mon)) {
655 956f1a0d Luiz Capitulino
            /* handler not converted yet */
656 ab5b027e Markus Armbruster
            qerror_report(QERR_COMMAND_NOT_FOUND, item);
657 4fdc94b4 Luiz Capitulino
            return -1;
658 956f1a0d Luiz Capitulino
        } else {
659 956f1a0d Luiz Capitulino
            cmd->mhandler.info(mon);
660 956f1a0d Luiz Capitulino
        }
661 13c7425e Luiz Capitulino
    }
662 13c7425e Luiz Capitulino
663 4fdc94b4 Luiz Capitulino
    return 0;
664 13c7425e Luiz Capitulino
665 13c7425e Luiz Capitulino
help:
666 13c7425e Luiz Capitulino
    help_cmd(mon, "info");
667 4fdc94b4 Luiz Capitulino
    return 0;
668 9dc39cba bellard
}
669 9dc39cba bellard
670 45e914cf Luiz Capitulino
static void do_info_version_print(Monitor *mon, const QObject *data)
671 45e914cf Luiz Capitulino
{
672 45e914cf Luiz Capitulino
    QDict *qdict;
673 45e914cf Luiz Capitulino
674 45e914cf Luiz Capitulino
    qdict = qobject_to_qdict(data);
675 45e914cf Luiz Capitulino
676 45e914cf Luiz Capitulino
    monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
677 45e914cf Luiz Capitulino
                                  qdict_get_str(qdict, "package"));
678 45e914cf Luiz Capitulino
}
679 45e914cf Luiz Capitulino
680 ab2d3187 Luiz Capitulino
/**
681 ab2d3187 Luiz Capitulino
 * do_info_version(): Show QEMU version
682 45e914cf Luiz Capitulino
 *
683 45e914cf Luiz Capitulino
 * Return a QDict with the following information:
684 45e914cf Luiz Capitulino
 *
685 45e914cf Luiz Capitulino
 * - "qemu": QEMU's version
686 45e914cf Luiz Capitulino
 * - "package": package's version
687 45e914cf Luiz Capitulino
 *
688 45e914cf Luiz Capitulino
 * Example:
689 45e914cf Luiz Capitulino
 *
690 45e914cf Luiz Capitulino
 * { "qemu": "0.11.50", "package": "" }
691 ab2d3187 Luiz Capitulino
 */
692 ab2d3187 Luiz Capitulino
static void do_info_version(Monitor *mon, QObject **ret_data)
693 9bc9d1c7 bellard
{
694 45e914cf Luiz Capitulino
    *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
695 45e914cf Luiz Capitulino
                                   QEMU_VERSION, QEMU_PKGVERSION);
696 9bc9d1c7 bellard
}
697 9bc9d1c7 bellard
698 e05486cb Luiz Capitulino
static void do_info_name_print(Monitor *mon, const QObject *data)
699 c35734b2 ths
{
700 e05486cb Luiz Capitulino
    QDict *qdict;
701 e05486cb Luiz Capitulino
702 e05486cb Luiz Capitulino
    qdict = qobject_to_qdict(data);
703 e05486cb Luiz Capitulino
    if (qdict_size(qdict) == 0) {
704 e05486cb Luiz Capitulino
        return;
705 e05486cb Luiz Capitulino
    }
706 e05486cb Luiz Capitulino
707 e05486cb Luiz Capitulino
    monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
708 e05486cb Luiz Capitulino
}
709 e05486cb Luiz Capitulino
710 e05486cb Luiz Capitulino
/**
711 e05486cb Luiz Capitulino
 * do_info_name(): Show VM name
712 e05486cb Luiz Capitulino
 *
713 e05486cb Luiz Capitulino
 * Return a QDict with the following information:
714 e05486cb Luiz Capitulino
 *
715 e05486cb Luiz Capitulino
 * - "name": VM's name (optional)
716 e05486cb Luiz Capitulino
 *
717 e05486cb Luiz Capitulino
 * Example:
718 e05486cb Luiz Capitulino
 *
719 e05486cb Luiz Capitulino
 * { "name": "qemu-name" }
720 e05486cb Luiz Capitulino
 */
721 e05486cb Luiz Capitulino
static void do_info_name(Monitor *mon, QObject **ret_data)
722 e05486cb Luiz Capitulino
{
723 e05486cb Luiz Capitulino
    *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
724 e05486cb Luiz Capitulino
                            qobject_from_jsonf("{}");
725 c35734b2 ths
}
726 c35734b2 ths
727 1a728677 Luiz Capitulino
static QObject *get_cmd_dict(const char *name)
728 1a728677 Luiz Capitulino
{
729 1a728677 Luiz Capitulino
    const char *p;
730 1a728677 Luiz Capitulino
731 1a728677 Luiz Capitulino
    /* Remove '|' from some commands */
732 1a728677 Luiz Capitulino
    p = strchr(name, '|');
733 1a728677 Luiz Capitulino
    if (p) {
734 1a728677 Luiz Capitulino
        p++;
735 1a728677 Luiz Capitulino
    } else {
736 1a728677 Luiz Capitulino
        p = name;
737 1a728677 Luiz Capitulino
    }
738 1a728677 Luiz Capitulino
739 1a728677 Luiz Capitulino
    return qobject_from_jsonf("{ 'name': %s }", p);
740 1a728677 Luiz Capitulino
}
741 1a728677 Luiz Capitulino
742 e3bba9d0 Luiz Capitulino
/**
743 e3bba9d0 Luiz Capitulino
 * do_info_commands(): List QMP available commands
744 e3bba9d0 Luiz Capitulino
 *
745 1a728677 Luiz Capitulino
 * Each command is represented by a QDict, the returned QObject is a QList
746 1a728677 Luiz Capitulino
 * of all commands.
747 1a728677 Luiz Capitulino
 *
748 1a728677 Luiz Capitulino
 * The QDict contains:
749 1a728677 Luiz Capitulino
 *
750 1a728677 Luiz Capitulino
 * - "name": command's name
751 1a728677 Luiz Capitulino
 *
752 1a728677 Luiz Capitulino
 * Example:
753 1a728677 Luiz Capitulino
 *
754 1a728677 Luiz Capitulino
 * { [ { "name": "query-balloon" }, { "name": "system_powerdown" } ] }
755 e3bba9d0 Luiz Capitulino
 */
756 e3bba9d0 Luiz Capitulino
static void do_info_commands(Monitor *mon, QObject **ret_data)
757 e3bba9d0 Luiz Capitulino
{
758 e3bba9d0 Luiz Capitulino
    QList *cmd_list;
759 e3bba9d0 Luiz Capitulino
    const mon_cmd_t *cmd;
760 e3bba9d0 Luiz Capitulino
761 e3bba9d0 Luiz Capitulino
    cmd_list = qlist_new();
762 e3bba9d0 Luiz Capitulino
763 e3bba9d0 Luiz Capitulino
    for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
764 e3bba9d0 Luiz Capitulino
        if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
765 1a728677 Luiz Capitulino
            qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
766 e3bba9d0 Luiz Capitulino
        }
767 e3bba9d0 Luiz Capitulino
    }
768 e3bba9d0 Luiz Capitulino
769 e3bba9d0 Luiz Capitulino
    for (cmd = info_cmds; cmd->name != NULL; cmd++) {
770 e3bba9d0 Luiz Capitulino
        if (monitor_handler_ported(cmd)) {
771 e3bba9d0 Luiz Capitulino
            char buf[128];
772 e3bba9d0 Luiz Capitulino
            snprintf(buf, sizeof(buf), "query-%s", cmd->name);
773 1a728677 Luiz Capitulino
            qlist_append_obj(cmd_list, get_cmd_dict(buf));
774 e3bba9d0 Luiz Capitulino
        }
775 e3bba9d0 Luiz Capitulino
    }
776 e3bba9d0 Luiz Capitulino
777 e3bba9d0 Luiz Capitulino
    *ret_data = QOBJECT(cmd_list);
778 e3bba9d0 Luiz Capitulino
}
779 e3bba9d0 Luiz Capitulino
780 bf4f74c0 aurel32
#if defined(TARGET_I386)
781 14f0720d Luiz Capitulino
static void do_info_hpet_print(Monitor *mon, const QObject *data)
782 16b29ae1 aliguori
{
783 376253ec aliguori
    monitor_printf(mon, "HPET is %s by QEMU\n",
784 14f0720d Luiz Capitulino
                   qdict_get_bool(qobject_to_qdict(data), "enabled") ?
785 14f0720d Luiz Capitulino
                   "enabled" : "disabled");
786 14f0720d Luiz Capitulino
}
787 14f0720d Luiz Capitulino
788 14f0720d Luiz Capitulino
/**
789 14f0720d Luiz Capitulino
 * do_info_hpet(): Show HPET state
790 14f0720d Luiz Capitulino
 *
791 14f0720d Luiz Capitulino
 * Return a QDict with the following information:
792 14f0720d Luiz Capitulino
 *
793 14f0720d Luiz Capitulino
 * - "enabled": true if hpet if enabled, false otherwise
794 14f0720d Luiz Capitulino
 *
795 14f0720d Luiz Capitulino
 * Example:
796 14f0720d Luiz Capitulino
 *
797 14f0720d Luiz Capitulino
 * { "enabled": true }
798 14f0720d Luiz Capitulino
 */
799 14f0720d Luiz Capitulino
static void do_info_hpet(Monitor *mon, QObject **ret_data)
800 14f0720d Luiz Capitulino
{
801 14f0720d Luiz Capitulino
    *ret_data = qobject_from_jsonf("{ 'enabled': %i }", !no_hpet);
802 16b29ae1 aliguori
}
803 bf4f74c0 aurel32
#endif
804 16b29ae1 aliguori
805 9603ceba Luiz Capitulino
static void do_info_uuid_print(Monitor *mon, const QObject *data)
806 a36e69dd ths
{
807 9603ceba Luiz Capitulino
    monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
808 9603ceba Luiz Capitulino
}
809 9603ceba Luiz Capitulino
810 9603ceba Luiz Capitulino
/**
811 9603ceba Luiz Capitulino
 * do_info_uuid(): Show VM UUID
812 9603ceba Luiz Capitulino
 *
813 9603ceba Luiz Capitulino
 * Return a QDict with the following information:
814 9603ceba Luiz Capitulino
 *
815 9603ceba Luiz Capitulino
 * - "UUID": Universally Unique Identifier
816 9603ceba Luiz Capitulino
 *
817 9603ceba Luiz Capitulino
 * Example:
818 9603ceba Luiz Capitulino
 *
819 9603ceba Luiz Capitulino
 * { "UUID": "550e8400-e29b-41d4-a716-446655440000" }
820 9603ceba Luiz Capitulino
 */
821 9603ceba Luiz Capitulino
static void do_info_uuid(Monitor *mon, QObject **ret_data)
822 9603ceba Luiz Capitulino
{
823 9603ceba Luiz Capitulino
    char uuid[64];
824 9603ceba Luiz Capitulino
825 9603ceba Luiz Capitulino
    snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
826 376253ec aliguori
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
827 376253ec aliguori
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
828 376253ec aliguori
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
829 376253ec aliguori
                   qemu_uuid[14], qemu_uuid[15]);
830 9603ceba Luiz Capitulino
    *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
831 a36e69dd ths
}
832 a36e69dd ths
833 6a00d601 bellard
/* get the current CPU defined by the user */
834 9596ebb7 pbrook
static int mon_set_cpu(int cpu_index)
835 6a00d601 bellard
{
836 6a00d601 bellard
    CPUState *env;
837 6a00d601 bellard
838 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
839 6a00d601 bellard
        if (env->cpu_index == cpu_index) {
840 731b0364 aliguori
            cur_mon->mon_cpu = env;
841 6a00d601 bellard
            return 0;
842 6a00d601 bellard
        }
843 6a00d601 bellard
    }
844 6a00d601 bellard
    return -1;
845 6a00d601 bellard
}
846 6a00d601 bellard
847 9596ebb7 pbrook
static CPUState *mon_get_cpu(void)
848 6a00d601 bellard
{
849 731b0364 aliguori
    if (!cur_mon->mon_cpu) {
850 6a00d601 bellard
        mon_set_cpu(0);
851 6a00d601 bellard
    }
852 4c0960c0 Avi Kivity
    cpu_synchronize_state(cur_mon->mon_cpu);
853 731b0364 aliguori
    return cur_mon->mon_cpu;
854 6a00d601 bellard
}
855 6a00d601 bellard
856 376253ec aliguori
static void do_info_registers(Monitor *mon)
857 9307c4c1 bellard
{
858 6a00d601 bellard
    CPUState *env;
859 6a00d601 bellard
    env = mon_get_cpu();
860 9307c4c1 bellard
#ifdef TARGET_I386
861 376253ec aliguori
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
862 d24b15a8 bellard
                   X86_DUMP_FPU);
863 9307c4c1 bellard
#else
864 376253ec aliguori
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
865 7fe48483 bellard
                   0);
866 9307c4c1 bellard
#endif
867 9307c4c1 bellard
}
868 9307c4c1 bellard
869 8f3cec0b Luiz Capitulino
static void print_cpu_iter(QObject *obj, void *opaque)
870 8f3cec0b Luiz Capitulino
{
871 8f3cec0b Luiz Capitulino
    QDict *cpu;
872 8f3cec0b Luiz Capitulino
    int active = ' ';
873 8f3cec0b Luiz Capitulino
    Monitor *mon = opaque;
874 8f3cec0b Luiz Capitulino
875 8f3cec0b Luiz Capitulino
    assert(qobject_type(obj) == QTYPE_QDICT);
876 8f3cec0b Luiz Capitulino
    cpu = qobject_to_qdict(obj);
877 8f3cec0b Luiz Capitulino
878 55483ad6 Luiz Capitulino
    if (qdict_get_bool(cpu, "current")) {
879 8f3cec0b Luiz Capitulino
        active = '*';
880 55483ad6 Luiz Capitulino
    }
881 8f3cec0b Luiz Capitulino
882 8f3cec0b Luiz Capitulino
    monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
883 8f3cec0b Luiz Capitulino
884 8f3cec0b Luiz Capitulino
#if defined(TARGET_I386)
885 8f3cec0b Luiz Capitulino
    monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
886 8f3cec0b Luiz Capitulino
                   (target_ulong) qdict_get_int(cpu, "pc"));
887 8f3cec0b Luiz Capitulino
#elif defined(TARGET_PPC)
888 8f3cec0b Luiz Capitulino
    monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
889 8f3cec0b Luiz Capitulino
                   (target_long) qdict_get_int(cpu, "nip"));
890 8f3cec0b Luiz Capitulino
#elif defined(TARGET_SPARC)
891 8f3cec0b Luiz Capitulino
    monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
892 8f3cec0b Luiz Capitulino
                   (target_long) qdict_get_int(cpu, "pc"));
893 8f3cec0b Luiz Capitulino
    monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
894 8f3cec0b Luiz Capitulino
                   (target_long) qdict_get_int(cpu, "npc"));
895 8f3cec0b Luiz Capitulino
#elif defined(TARGET_MIPS)
896 8f3cec0b Luiz Capitulino
    monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
897 8f3cec0b Luiz Capitulino
                   (target_long) qdict_get_int(cpu, "PC"));
898 8f3cec0b Luiz Capitulino
#endif
899 8f3cec0b Luiz Capitulino
900 55483ad6 Luiz Capitulino
    if (qdict_get_bool(cpu, "halted")) {
901 8f3cec0b Luiz Capitulino
        monitor_printf(mon, " (halted)");
902 55483ad6 Luiz Capitulino
    }
903 8f3cec0b Luiz Capitulino
904 8f3cec0b Luiz Capitulino
    monitor_printf(mon, "\n");
905 8f3cec0b Luiz Capitulino
}
906 8f3cec0b Luiz Capitulino
907 8f3cec0b Luiz Capitulino
static void monitor_print_cpus(Monitor *mon, const QObject *data)
908 8f3cec0b Luiz Capitulino
{
909 8f3cec0b Luiz Capitulino
    QList *cpu_list;
910 8f3cec0b Luiz Capitulino
911 8f3cec0b Luiz Capitulino
    assert(qobject_type(data) == QTYPE_QLIST);
912 8f3cec0b Luiz Capitulino
    cpu_list = qobject_to_qlist(data);
913 8f3cec0b Luiz Capitulino
    qlist_iter(cpu_list, print_cpu_iter, mon);
914 8f3cec0b Luiz Capitulino
}
915 8f3cec0b Luiz Capitulino
916 8f3cec0b Luiz Capitulino
/**
917 8f3cec0b Luiz Capitulino
 * do_info_cpus(): Show CPU information
918 8f3cec0b Luiz Capitulino
 *
919 55483ad6 Luiz Capitulino
 * Return a QList. Each CPU is represented by a QDict, which contains:
920 8f3cec0b Luiz Capitulino
 *
921 55483ad6 Luiz Capitulino
 * - "cpu": CPU index
922 55483ad6 Luiz Capitulino
 * - "current": true if this is the current CPU, false otherwise
923 55483ad6 Luiz Capitulino
 * - "halted": true if the cpu is halted, false otherwise
924 55483ad6 Luiz Capitulino
 * - Current program counter. The key's name depends on the architecture:
925 55483ad6 Luiz Capitulino
 *      "pc": i386/x86)64
926 55483ad6 Luiz Capitulino
 *      "nip": PPC
927 55483ad6 Luiz Capitulino
 *      "pc" and "npc": sparc
928 55483ad6 Luiz Capitulino
 *      "PC": mips
929 8f3cec0b Luiz Capitulino
 *
930 55483ad6 Luiz Capitulino
 * Example:
931 55483ad6 Luiz Capitulino
 *
932 55483ad6 Luiz Capitulino
 * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 },
933 55483ad6 Luiz Capitulino
 *   { "CPU": 1, "current": false, "halted": true, "pc": 7108165 } ]
934 8f3cec0b Luiz Capitulino
 */
935 8f3cec0b Luiz Capitulino
static void do_info_cpus(Monitor *mon, QObject **ret_data)
936 6a00d601 bellard
{
937 6a00d601 bellard
    CPUState *env;
938 8f3cec0b Luiz Capitulino
    QList *cpu_list;
939 8f3cec0b Luiz Capitulino
940 8f3cec0b Luiz Capitulino
    cpu_list = qlist_new();
941 6a00d601 bellard
942 6a00d601 bellard
    /* just to set the default cpu if not already done */
943 6a00d601 bellard
    mon_get_cpu();
944 6a00d601 bellard
945 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
946 55483ad6 Luiz Capitulino
        QDict *cpu;
947 55483ad6 Luiz Capitulino
        QObject *obj;
948 8f3cec0b Luiz Capitulino
949 4c0960c0 Avi Kivity
        cpu_synchronize_state(env);
950 8f3cec0b Luiz Capitulino
951 55483ad6 Luiz Capitulino
        obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
952 55483ad6 Luiz Capitulino
                                 env->cpu_index, env == mon->mon_cpu,
953 55483ad6 Luiz Capitulino
                                 env->halted);
954 55483ad6 Luiz Capitulino
955 55483ad6 Luiz Capitulino
        cpu = qobject_to_qdict(obj);
956 8f3cec0b Luiz Capitulino
957 6a00d601 bellard
#if defined(TARGET_I386)
958 8f3cec0b Luiz Capitulino
        qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
959 e80e1cc4 bellard
#elif defined(TARGET_PPC)
960 8f3cec0b Luiz Capitulino
        qdict_put(cpu, "nip", qint_from_int(env->nip));
961 ba3c64fb bellard
#elif defined(TARGET_SPARC)
962 8f3cec0b Luiz Capitulino
        qdict_put(cpu, "pc", qint_from_int(env->pc));
963 8f3cec0b Luiz Capitulino
        qdict_put(cpu, "npc", qint_from_int(env->npc));
964 ead9360e ths
#elif defined(TARGET_MIPS)
965 8f3cec0b Luiz Capitulino
        qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
966 ce5232c5 bellard
#endif
967 8f3cec0b Luiz Capitulino
968 8f3cec0b Luiz Capitulino
        qlist_append(cpu_list, cpu);
969 6a00d601 bellard
    }
970 8f3cec0b Luiz Capitulino
971 8f3cec0b Luiz Capitulino
    *ret_data = QOBJECT(cpu_list);
972 6a00d601 bellard
}
973 6a00d601 bellard
974 584cbdb5 Luiz Capitulino
static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
975 6a00d601 bellard
{
976 d54908a5 Luiz Capitulino
    int index = qdict_get_int(qdict, "index");
977 584cbdb5 Luiz Capitulino
    if (mon_set_cpu(index) < 0) {
978 e17ba87c Markus Armbruster
        qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
979 e17ba87c Markus Armbruster
                      "a CPU number");
980 584cbdb5 Luiz Capitulino
        return -1;
981 584cbdb5 Luiz Capitulino
    }
982 584cbdb5 Luiz Capitulino
    return 0;
983 6a00d601 bellard
}
984 6a00d601 bellard
985 376253ec aliguori
static void do_info_jit(Monitor *mon)
986 e3db7226 bellard
{
987 376253ec aliguori
    dump_exec_info((FILE *)mon, monitor_fprintf);
988 e3db7226 bellard
}
989 e3db7226 bellard
990 376253ec aliguori
static void do_info_history(Monitor *mon)
991 aa455485 bellard
{
992 aa455485 bellard
    int i;
993 7e2515e8 bellard
    const char *str;
994 3b46e624 ths
995 cde76ee1 aliguori
    if (!mon->rs)
996 cde76ee1 aliguori
        return;
997 7e2515e8 bellard
    i = 0;
998 7e2515e8 bellard
    for(;;) {
999 731b0364 aliguori
        str = readline_get_history(mon->rs, i);
1000 7e2515e8 bellard
        if (!str)
1001 7e2515e8 bellard
            break;
1002 376253ec aliguori
        monitor_printf(mon, "%d: '%s'\n", i, str);
1003 8e3a9fd2 bellard
        i++;
1004 aa455485 bellard
    }
1005 aa455485 bellard
}
1006 aa455485 bellard
1007 76a66253 j_mayer
#if defined(TARGET_PPC)
1008 76a66253 j_mayer
/* XXX: not implemented in other targets */
1009 376253ec aliguori
static void do_info_cpu_stats(Monitor *mon)
1010 76a66253 j_mayer
{
1011 76a66253 j_mayer
    CPUState *env;
1012 76a66253 j_mayer
1013 76a66253 j_mayer
    env = mon_get_cpu();
1014 376253ec aliguori
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
1015 76a66253 j_mayer
}
1016 76a66253 j_mayer
#endif
1017 76a66253 j_mayer
1018 b223f35f Luiz Capitulino
/**
1019 b223f35f Luiz Capitulino
 * do_quit(): Quit QEMU execution
1020 b223f35f Luiz Capitulino
 */
1021 ef4b7eee Luiz Capitulino
static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
1022 9dc39cba bellard
{
1023 39b59d26 Luiz Capitulino
    monitor_suspend(mon);
1024 39b59d26 Luiz Capitulino
    no_shutdown = 0;
1025 39b59d26 Luiz Capitulino
    qemu_system_shutdown_request();
1026 39b59d26 Luiz Capitulino
1027 ef4b7eee Luiz Capitulino
    return 0;
1028 9dc39cba bellard
}
1029 9dc39cba bellard
1030 376253ec aliguori
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
1031 9dc39cba bellard
{
1032 9dc39cba bellard
    if (bdrv_is_inserted(bs)) {
1033 9dc39cba bellard
        if (!force) {
1034 9dc39cba bellard
            if (!bdrv_is_removable(bs)) {
1035 ab5b027e Markus Armbruster
                qerror_report(QERR_DEVICE_NOT_REMOVABLE,
1036 2c2a6bb8 Markus Armbruster
                               bdrv_get_device_name(bs));
1037 9dc39cba bellard
                return -1;
1038 9dc39cba bellard
            }
1039 9dc39cba bellard
            if (bdrv_is_locked(bs)) {
1040 ab5b027e Markus Armbruster
                qerror_report(QERR_DEVICE_LOCKED, bdrv_get_device_name(bs));
1041 9dc39cba bellard
                return -1;
1042 9dc39cba bellard
            }
1043 9dc39cba bellard
        }
1044 9dc39cba bellard
        bdrv_close(bs);
1045 9dc39cba bellard
    }
1046 9dc39cba bellard
    return 0;
1047 9dc39cba bellard
}
1048 9dc39cba bellard
1049 9b9d4d9c Luiz Capitulino
static int do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
1050 9dc39cba bellard
{
1051 9dc39cba bellard
    BlockDriverState *bs;
1052 f18c16de Luiz Capitulino
    int force = qdict_get_int(qdict, "force");
1053 78d714e0 Luiz Capitulino
    const char *filename = qdict_get_str(qdict, "device");
1054 9dc39cba bellard
1055 9307c4c1 bellard
    bs = bdrv_find(filename);
1056 9dc39cba bellard
    if (!bs) {
1057 ab5b027e Markus Armbruster
        qerror_report(QERR_DEVICE_NOT_FOUND, filename);
1058 9b9d4d9c Luiz Capitulino
        return -1;
1059 9dc39cba bellard
    }
1060 9b9d4d9c Luiz Capitulino
    return eject_device(mon, bs, force);
1061 9dc39cba bellard
}
1062 9dc39cba bellard
1063 ba85d351 Luiz Capitulino
static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
1064 a3a55a2e Luiz Capitulino
                                QObject **ret_data)
1065 a3a55a2e Luiz Capitulino
{
1066 a3a55a2e Luiz Capitulino
    BlockDriverState *bs;
1067 fd04a2ae Shahar Havivi
    int err;
1068 a3a55a2e Luiz Capitulino
1069 a3a55a2e Luiz Capitulino
    bs = bdrv_find(qdict_get_str(qdict, "device"));
1070 a3a55a2e Luiz Capitulino
    if (!bs) {
1071 ab5b027e Markus Armbruster
        qerror_report(QERR_DEVICE_NOT_FOUND, qdict_get_str(qdict, "device"));
1072 ba85d351 Luiz Capitulino
        return -1;
1073 a3a55a2e Luiz Capitulino
    }
1074 a3a55a2e Luiz Capitulino
1075 fd04a2ae Shahar Havivi
    err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
1076 fd04a2ae Shahar Havivi
    if (err == -EINVAL) {
1077 fd04a2ae Shahar Havivi
        qerror_report(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
1078 fd04a2ae Shahar Havivi
        return -1;
1079 fd04a2ae Shahar Havivi
    } else if (err < 0) {
1080 ab5b027e Markus Armbruster
        qerror_report(QERR_INVALID_PASSWORD);
1081 ba85d351 Luiz Capitulino
        return -1;
1082 a3a55a2e Luiz Capitulino
    }
1083 ba85d351 Luiz Capitulino
1084 ba85d351 Luiz Capitulino
    return 0;
1085 a3a55a2e Luiz Capitulino
}
1086 a3a55a2e Luiz Capitulino
1087 0bbc47bb Luiz Capitulino
static int do_change_block(Monitor *mon, const char *device,
1088 0bbc47bb Luiz Capitulino
                           const char *filename, const char *fmt)
1089 9dc39cba bellard
{
1090 9dc39cba bellard
    BlockDriverState *bs;
1091 2ecea9b8 aurel32
    BlockDriver *drv = NULL;
1092 cb4e5f8e Naphtali Sprei
    int bdrv_flags;
1093 9dc39cba bellard
1094 9307c4c1 bellard
    bs = bdrv_find(device);
1095 9dc39cba bellard
    if (!bs) {
1096 ab5b027e Markus Armbruster
        qerror_report(QERR_DEVICE_NOT_FOUND, device);
1097 0bbc47bb Luiz Capitulino
        return -1;
1098 9dc39cba bellard
    }
1099 2ecea9b8 aurel32
    if (fmt) {
1100 eb852011 Markus Armbruster
        drv = bdrv_find_whitelisted_format(fmt);
1101 2ecea9b8 aurel32
        if (!drv) {
1102 ab5b027e Markus Armbruster
            qerror_report(QERR_INVALID_BLOCK_FORMAT, fmt);
1103 0bbc47bb Luiz Capitulino
            return -1;
1104 2ecea9b8 aurel32
        }
1105 2ecea9b8 aurel32
    }
1106 0bbc47bb Luiz Capitulino
    if (eject_device(mon, bs, 0) < 0) {
1107 0bbc47bb Luiz Capitulino
        return -1;
1108 0bbc47bb Luiz Capitulino
    }
1109 cb4e5f8e Naphtali Sprei
    bdrv_flags = bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM ? 0 : BDRV_O_RDWR;
1110 cb4e5f8e Naphtali Sprei
    if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
1111 07a5d95a Ryan Harper
        qerror_report(QERR_OPEN_FILE_FAILED, filename);
1112 0bbc47bb Luiz Capitulino
        return -1;
1113 0bbc47bb Luiz Capitulino
    }
1114 0bbc47bb Luiz Capitulino
    return monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
1115 bb5fc20f aliguori
}
1116 bb5fc20f aliguori
1117 0bbc47bb Luiz Capitulino
static int change_vnc_password(const char *password)
1118 bb5fc20f aliguori
{
1119 0bbc47bb Luiz Capitulino
    if (vnc_display_password(NULL, password) < 0) {
1120 ab5b027e Markus Armbruster
        qerror_report(QERR_SET_PASSWD_FAILED);
1121 0bbc47bb Luiz Capitulino
        return -1;
1122 0bbc47bb Luiz Capitulino
    }
1123 bb5fc20f aliguori
1124 0bbc47bb Luiz Capitulino
    return 0;
1125 2895e075 Markus Armbruster
}
1126 2895e075 Markus Armbruster
1127 2895e075 Markus Armbruster
static void change_vnc_password_cb(Monitor *mon, const char *password,
1128 2895e075 Markus Armbruster
                                   void *opaque)
1129 2895e075 Markus Armbruster
{
1130 ec3b82af Markus Armbruster
    change_vnc_password(password);
1131 731b0364 aliguori
    monitor_read_command(mon, 1);
1132 9dc39cba bellard
}
1133 9dc39cba bellard
1134 0bbc47bb Luiz Capitulino
static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1135 e25a5822 ths
{
1136 70848515 ths
    if (strcmp(target, "passwd") == 0 ||
1137 28a76be8 aliguori
        strcmp(target, "password") == 0) {
1138 28a76be8 aliguori
        if (arg) {
1139 bb5fc20f aliguori
            char password[9];
1140 28a76be8 aliguori
            strncpy(password, arg, sizeof(password));
1141 28a76be8 aliguori
            password[sizeof(password) - 1] = '\0';
1142 0bbc47bb Luiz Capitulino
            return change_vnc_password(password);
1143 bb5fc20f aliguori
        } else {
1144 0bbc47bb Luiz Capitulino
            return monitor_read_password(mon, change_vnc_password_cb, NULL);
1145 bb5fc20f aliguori
        }
1146 70848515 ths
    } else {
1147 0bbc47bb Luiz Capitulino
        if (vnc_display_open(NULL, target) < 0) {
1148 ab5b027e Markus Armbruster
            qerror_report(QERR_VNC_SERVER_FAILED, target);
1149 0bbc47bb Luiz Capitulino
            return -1;
1150 0bbc47bb Luiz Capitulino
        }
1151 70848515 ths
    }
1152 0bbc47bb Luiz Capitulino
1153 0bbc47bb Luiz Capitulino
    return 0;
1154 e25a5822 ths
}
1155 e25a5822 ths
1156 ec3b82af Markus Armbruster
/**
1157 ec3b82af Markus Armbruster
 * do_change(): Change a removable medium, or VNC configuration
1158 ec3b82af Markus Armbruster
 */
1159 0bbc47bb Luiz Capitulino
static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
1160 e25a5822 ths
{
1161 1d4daa91 Luiz Capitulino
    const char *device = qdict_get_str(qdict, "device");
1162 1d4daa91 Luiz Capitulino
    const char *target = qdict_get_str(qdict, "target");
1163 1d4daa91 Luiz Capitulino
    const char *arg = qdict_get_try_str(qdict, "arg");
1164 0bbc47bb Luiz Capitulino
    int ret;
1165 0bbc47bb Luiz Capitulino
1166 e25a5822 ths
    if (strcmp(device, "vnc") == 0) {
1167 0bbc47bb Luiz Capitulino
        ret = do_change_vnc(mon, target, arg);
1168 e25a5822 ths
    } else {
1169 0bbc47bb Luiz Capitulino
        ret = do_change_block(mon, device, target, arg);
1170 e25a5822 ths
    }
1171 0bbc47bb Luiz Capitulino
1172 0bbc47bb Luiz Capitulino
    return ret;
1173 e25a5822 ths
}
1174 e25a5822 ths
1175 f1dc58e0 Luiz Capitulino
static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
1176 59a983b9 bellard
{
1177 d54908a5 Luiz Capitulino
    vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
1178 f1dc58e0 Luiz Capitulino
    return 0;
1179 59a983b9 bellard
}
1180 59a983b9 bellard
1181 d54908a5 Luiz Capitulino
static void do_logfile(Monitor *mon, const QDict *qdict)
1182 e735b91c pbrook
{
1183 d54908a5 Luiz Capitulino
    cpu_set_log_filename(qdict_get_str(qdict, "filename"));
1184 e735b91c pbrook
}
1185 e735b91c pbrook
1186 d54908a5 Luiz Capitulino
static void do_log(Monitor *mon, const QDict *qdict)
1187 f193c797 bellard
{
1188 f193c797 bellard
    int mask;
1189 d54908a5 Luiz Capitulino
    const char *items = qdict_get_str(qdict, "items");
1190 3b46e624 ths
1191 9307c4c1 bellard
    if (!strcmp(items, "none")) {
1192 f193c797 bellard
        mask = 0;
1193 f193c797 bellard
    } else {
1194 9307c4c1 bellard
        mask = cpu_str_to_log_mask(items);
1195 f193c797 bellard
        if (!mask) {
1196 376253ec aliguori
            help_cmd(mon, "log");
1197 f193c797 bellard
            return;
1198 f193c797 bellard
        }
1199 f193c797 bellard
    }
1200 f193c797 bellard
    cpu_set_log(mask);
1201 f193c797 bellard
}
1202 f193c797 bellard
1203 d54908a5 Luiz Capitulino
static void do_singlestep(Monitor *mon, const QDict *qdict)
1204 1b530a6d aurel32
{
1205 d54908a5 Luiz Capitulino
    const char *option = qdict_get_try_str(qdict, "option");
1206 1b530a6d aurel32
    if (!option || !strcmp(option, "on")) {
1207 1b530a6d aurel32
        singlestep = 1;
1208 1b530a6d aurel32
    } else if (!strcmp(option, "off")) {
1209 1b530a6d aurel32
        singlestep = 0;
1210 1b530a6d aurel32
    } else {
1211 1b530a6d aurel32
        monitor_printf(mon, "unexpected option %s\n", option);
1212 1b530a6d aurel32
    }
1213 1b530a6d aurel32
}
1214 1b530a6d aurel32
1215 e0c97bde Luiz Capitulino
/**
1216 e0c97bde Luiz Capitulino
 * do_stop(): Stop VM execution
1217 e0c97bde Luiz Capitulino
 */
1218 ef4b7eee Luiz Capitulino
static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
1219 8a7ddc38 bellard
{
1220 8a7ddc38 bellard
    vm_stop(EXCP_INTERRUPT);
1221 ef4b7eee Luiz Capitulino
    return 0;
1222 8a7ddc38 bellard
}
1223 8a7ddc38 bellard
1224 bb5fc20f aliguori
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
1225 c0f4ce77 aliguori
1226 376253ec aliguori
struct bdrv_iterate_context {
1227 376253ec aliguori
    Monitor *mon;
1228 376253ec aliguori
    int err;
1229 376253ec aliguori
};
1230 376253ec aliguori
1231 a1f896a0 Luiz Capitulino
/**
1232 a1f896a0 Luiz Capitulino
 * do_cont(): Resume emulation.
1233 a1f896a0 Luiz Capitulino
 */
1234 d5a7b38f Luiz Capitulino
static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
1235 8a7ddc38 bellard
{
1236 376253ec aliguori
    struct bdrv_iterate_context context = { mon, 0 };
1237 c0f4ce77 aliguori
1238 376253ec aliguori
    bdrv_iterate(encrypted_bdrv_it, &context);
1239 c0f4ce77 aliguori
    /* only resume the vm if all keys are set and valid */
1240 d5a7b38f Luiz Capitulino
    if (!context.err) {
1241 c0f4ce77 aliguori
        vm_start();
1242 d5a7b38f Luiz Capitulino
        return 0;
1243 d5a7b38f Luiz Capitulino
    } else {
1244 d5a7b38f Luiz Capitulino
        return -1;
1245 d5a7b38f Luiz Capitulino
    }
1246 8a7ddc38 bellard
}
1247 8a7ddc38 bellard
1248 bb5fc20f aliguori
static void bdrv_key_cb(void *opaque, int err)
1249 bb5fc20f aliguori
{
1250 376253ec aliguori
    Monitor *mon = opaque;
1251 376253ec aliguori
1252 bb5fc20f aliguori
    /* another key was set successfully, retry to continue */
1253 bb5fc20f aliguori
    if (!err)
1254 a1f896a0 Luiz Capitulino
        do_cont(mon, NULL, NULL);
1255 bb5fc20f aliguori
}
1256 bb5fc20f aliguori
1257 bb5fc20f aliguori
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1258 bb5fc20f aliguori
{
1259 376253ec aliguori
    struct bdrv_iterate_context *context = opaque;
1260 bb5fc20f aliguori
1261 376253ec aliguori
    if (!context->err && bdrv_key_required(bs)) {
1262 376253ec aliguori
        context->err = -EBUSY;
1263 376253ec aliguori
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1264 376253ec aliguori
                                    context->mon);
1265 bb5fc20f aliguori
    }
1266 bb5fc20f aliguori
}
1267 bb5fc20f aliguori
1268 d54908a5 Luiz Capitulino
static void do_gdbserver(Monitor *mon, const QDict *qdict)
1269 59030a8c aliguori
{
1270 d54908a5 Luiz Capitulino
    const char *device = qdict_get_try_str(qdict, "device");
1271 59030a8c aliguori
    if (!device)
1272 59030a8c aliguori
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
1273 59030a8c aliguori
    if (gdbserver_start(device) < 0) {
1274 59030a8c aliguori
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1275 59030a8c aliguori
                       device);
1276 59030a8c aliguori
    } else if (strcmp(device, "none") == 0) {
1277 36556b20 aliguori
        monitor_printf(mon, "Disabled gdbserver\n");
1278 8a7ddc38 bellard
    } else {
1279 59030a8c aliguori
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1280 59030a8c aliguori
                       device);
1281 8a7ddc38 bellard
    }
1282 8a7ddc38 bellard
}
1283 8a7ddc38 bellard
1284 d54908a5 Luiz Capitulino
static void do_watchdog_action(Monitor *mon, const QDict *qdict)
1285 9dd986cc Richard W.M. Jones
{
1286 d54908a5 Luiz Capitulino
    const char *action = qdict_get_str(qdict, "action");
1287 9dd986cc Richard W.M. Jones
    if (select_watchdog_action(action) == -1) {
1288 9dd986cc Richard W.M. Jones
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1289 9dd986cc Richard W.M. Jones
    }
1290 9dd986cc Richard W.M. Jones
}
1291 9dd986cc Richard W.M. Jones
1292 376253ec aliguori
static void monitor_printc(Monitor *mon, int c)
1293 9307c4c1 bellard
{
1294 376253ec aliguori
    monitor_printf(mon, "'");
1295 9307c4c1 bellard
    switch(c) {
1296 9307c4c1 bellard
    case '\'':
1297 376253ec aliguori
        monitor_printf(mon, "\\'");
1298 9307c4c1 bellard
        break;
1299 9307c4c1 bellard
    case '\\':
1300 376253ec aliguori
        monitor_printf(mon, "\\\\");
1301 9307c4c1 bellard
        break;
1302 9307c4c1 bellard
    case '\n':
1303 376253ec aliguori
        monitor_printf(mon, "\\n");
1304 9307c4c1 bellard
        break;
1305 9307c4c1 bellard
    case '\r':
1306 376253ec aliguori
        monitor_printf(mon, "\\r");
1307 9307c4c1 bellard
        break;
1308 9307c4c1 bellard
    default:
1309 9307c4c1 bellard
        if (c >= 32 && c <= 126) {
1310 376253ec aliguori
            monitor_printf(mon, "%c", c);
1311 9307c4c1 bellard
        } else {
1312 376253ec aliguori
            monitor_printf(mon, "\\x%02x", c);
1313 9307c4c1 bellard
        }
1314 9307c4c1 bellard
        break;
1315 9307c4c1 bellard
    }
1316 376253ec aliguori
    monitor_printf(mon, "'");
1317 9307c4c1 bellard
}
1318 9307c4c1 bellard
1319 376253ec aliguori
static void memory_dump(Monitor *mon, int count, int format, int wsize,
1320 c227f099 Anthony Liguori
                        target_phys_addr_t addr, int is_physical)
1321 9307c4c1 bellard
{
1322 6a00d601 bellard
    CPUState *env;
1323 23842aab Blue Swirl
    int l, line_size, i, max_digits, len;
1324 9307c4c1 bellard
    uint8_t buf[16];
1325 9307c4c1 bellard
    uint64_t v;
1326 9307c4c1 bellard
1327 9307c4c1 bellard
    if (format == 'i') {
1328 9307c4c1 bellard
        int flags;
1329 9307c4c1 bellard
        flags = 0;
1330 6a00d601 bellard
        env = mon_get_cpu();
1331 9307c4c1 bellard
#ifdef TARGET_I386
1332 4c27ba27 bellard
        if (wsize == 2) {
1333 9307c4c1 bellard
            flags = 1;
1334 4c27ba27 bellard
        } else if (wsize == 4) {
1335 4c27ba27 bellard
            flags = 0;
1336 4c27ba27 bellard
        } else {
1337 6a15fd12 bellard
            /* as default we use the current CS size */
1338 4c27ba27 bellard
            flags = 0;
1339 6a15fd12 bellard
            if (env) {
1340 6a15fd12 bellard
#ifdef TARGET_X86_64
1341 5fafdf24 ths
                if ((env->efer & MSR_EFER_LMA) &&
1342 6a15fd12 bellard
                    (env->segs[R_CS].flags & DESC_L_MASK))
1343 6a15fd12 bellard
                    flags = 2;
1344 6a15fd12 bellard
                else
1345 6a15fd12 bellard
#endif
1346 6a15fd12 bellard
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
1347 6a15fd12 bellard
                    flags = 1;
1348 6a15fd12 bellard
            }
1349 4c27ba27 bellard
        }
1350 4c27ba27 bellard
#endif
1351 376253ec aliguori
        monitor_disas(mon, env, addr, count, is_physical, flags);
1352 9307c4c1 bellard
        return;
1353 9307c4c1 bellard
    }
1354 9307c4c1 bellard
1355 9307c4c1 bellard
    len = wsize * count;
1356 9307c4c1 bellard
    if (wsize == 1)
1357 9307c4c1 bellard
        line_size = 8;
1358 9307c4c1 bellard
    else
1359 9307c4c1 bellard
        line_size = 16;
1360 9307c4c1 bellard
    max_digits = 0;
1361 9307c4c1 bellard
1362 9307c4c1 bellard
    switch(format) {
1363 9307c4c1 bellard
    case 'o':
1364 9307c4c1 bellard
        max_digits = (wsize * 8 + 2) / 3;
1365 9307c4c1 bellard
        break;
1366 9307c4c1 bellard
    default:
1367 9307c4c1 bellard
    case 'x':
1368 9307c4c1 bellard
        max_digits = (wsize * 8) / 4;
1369 9307c4c1 bellard
        break;
1370 9307c4c1 bellard
    case 'u':
1371 9307c4c1 bellard
    case 'd':
1372 9307c4c1 bellard
        max_digits = (wsize * 8 * 10 + 32) / 33;
1373 9307c4c1 bellard
        break;
1374 9307c4c1 bellard
    case 'c':
1375 9307c4c1 bellard
        wsize = 1;
1376 9307c4c1 bellard
        break;
1377 9307c4c1 bellard
    }
1378 9307c4c1 bellard
1379 9307c4c1 bellard
    while (len > 0) {
1380 7743e588 blueswir1
        if (is_physical)
1381 376253ec aliguori
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
1382 7743e588 blueswir1
        else
1383 376253ec aliguori
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1384 9307c4c1 bellard
        l = len;
1385 9307c4c1 bellard
        if (l > line_size)
1386 9307c4c1 bellard
            l = line_size;
1387 9307c4c1 bellard
        if (is_physical) {
1388 9307c4c1 bellard
            cpu_physical_memory_rw(addr, buf, l, 0);
1389 9307c4c1 bellard
        } else {
1390 6a00d601 bellard
            env = mon_get_cpu();
1391 c8f79b67 aliguori
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
1392 376253ec aliguori
                monitor_printf(mon, " Cannot access memory\n");
1393 c8f79b67 aliguori
                break;
1394 c8f79b67 aliguori
            }
1395 9307c4c1 bellard
        }
1396 5fafdf24 ths
        i = 0;
1397 9307c4c1 bellard
        while (i < l) {
1398 9307c4c1 bellard
            switch(wsize) {
1399 9307c4c1 bellard
            default:
1400 9307c4c1 bellard
            case 1:
1401 9307c4c1 bellard
                v = ldub_raw(buf + i);
1402 9307c4c1 bellard
                break;
1403 9307c4c1 bellard
            case 2:
1404 9307c4c1 bellard
                v = lduw_raw(buf + i);
1405 9307c4c1 bellard
                break;
1406 9307c4c1 bellard
            case 4:
1407 92a31b1f bellard
                v = (uint32_t)ldl_raw(buf + i);
1408 9307c4c1 bellard
                break;
1409 9307c4c1 bellard
            case 8:
1410 9307c4c1 bellard
                v = ldq_raw(buf + i);
1411 9307c4c1 bellard
                break;
1412 9307c4c1 bellard
            }
1413 376253ec aliguori
            monitor_printf(mon, " ");
1414 9307c4c1 bellard
            switch(format) {
1415 9307c4c1 bellard
            case 'o':
1416 376253ec aliguori
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1417 9307c4c1 bellard
                break;
1418 9307c4c1 bellard
            case 'x':
1419 376253ec aliguori
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1420 9307c4c1 bellard
                break;
1421 9307c4c1 bellard
            case 'u':
1422 376253ec aliguori
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
1423 9307c4c1 bellard
                break;
1424 9307c4c1 bellard
            case 'd':
1425 376253ec aliguori
                monitor_printf(mon, "%*" PRId64, max_digits, v);
1426 9307c4c1 bellard
                break;
1427 9307c4c1 bellard
            case 'c':
1428 376253ec aliguori
                monitor_printc(mon, v);
1429 9307c4c1 bellard
                break;
1430 9307c4c1 bellard
            }
1431 9307c4c1 bellard
            i += wsize;
1432 9307c4c1 bellard
        }
1433 376253ec aliguori
        monitor_printf(mon, "\n");
1434 9307c4c1 bellard
        addr += l;
1435 9307c4c1 bellard
        len -= l;
1436 9307c4c1 bellard
    }
1437 9307c4c1 bellard
}
1438 9307c4c1 bellard
1439 1bd1442e Luiz Capitulino
static void do_memory_dump(Monitor *mon, const QDict *qdict)
1440 9307c4c1 bellard
{
1441 1bd1442e Luiz Capitulino
    int count = qdict_get_int(qdict, "count");
1442 1bd1442e Luiz Capitulino
    int format = qdict_get_int(qdict, "format");
1443 1bd1442e Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
1444 1bd1442e Luiz Capitulino
    target_long addr = qdict_get_int(qdict, "addr");
1445 1bd1442e Luiz Capitulino
1446 376253ec aliguori
    memory_dump(mon, count, format, size, addr, 0);
1447 9307c4c1 bellard
}
1448 9307c4c1 bellard
1449 1bd1442e Luiz Capitulino
static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1450 9307c4c1 bellard
{
1451 1bd1442e Luiz Capitulino
    int count = qdict_get_int(qdict, "count");
1452 1bd1442e Luiz Capitulino
    int format = qdict_get_int(qdict, "format");
1453 1bd1442e Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
1454 c227f099 Anthony Liguori
    target_phys_addr_t addr = qdict_get_int(qdict, "addr");
1455 1bd1442e Luiz Capitulino
1456 376253ec aliguori
    memory_dump(mon, count, format, size, addr, 1);
1457 9307c4c1 bellard
}
1458 9307c4c1 bellard
1459 1bd1442e Luiz Capitulino
static void do_print(Monitor *mon, const QDict *qdict)
1460 9307c4c1 bellard
{
1461 1bd1442e Luiz Capitulino
    int format = qdict_get_int(qdict, "format");
1462 c227f099 Anthony Liguori
    target_phys_addr_t val = qdict_get_int(qdict, "val");
1463 1bd1442e Luiz Capitulino
1464 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS == 32
1465 9307c4c1 bellard
    switch(format) {
1466 9307c4c1 bellard
    case 'o':
1467 376253ec aliguori
        monitor_printf(mon, "%#o", val);
1468 9307c4c1 bellard
        break;
1469 9307c4c1 bellard
    case 'x':
1470 376253ec aliguori
        monitor_printf(mon, "%#x", val);
1471 9307c4c1 bellard
        break;
1472 9307c4c1 bellard
    case 'u':
1473 376253ec aliguori
        monitor_printf(mon, "%u", val);
1474 9307c4c1 bellard
        break;
1475 9307c4c1 bellard
    default:
1476 9307c4c1 bellard
    case 'd':
1477 376253ec aliguori
        monitor_printf(mon, "%d", val);
1478 9307c4c1 bellard
        break;
1479 9307c4c1 bellard
    case 'c':
1480 376253ec aliguori
        monitor_printc(mon, val);
1481 9307c4c1 bellard
        break;
1482 9307c4c1 bellard
    }
1483 92a31b1f bellard
#else
1484 92a31b1f bellard
    switch(format) {
1485 92a31b1f bellard
    case 'o':
1486 376253ec aliguori
        monitor_printf(mon, "%#" PRIo64, val);
1487 92a31b1f bellard
        break;
1488 92a31b1f bellard
    case 'x':
1489 376253ec aliguori
        monitor_printf(mon, "%#" PRIx64, val);
1490 92a31b1f bellard
        break;
1491 92a31b1f bellard
    case 'u':
1492 376253ec aliguori
        monitor_printf(mon, "%" PRIu64, val);
1493 92a31b1f bellard
        break;
1494 92a31b1f bellard
    default:
1495 92a31b1f bellard
    case 'd':
1496 376253ec aliguori
        monitor_printf(mon, "%" PRId64, val);
1497 92a31b1f bellard
        break;
1498 92a31b1f bellard
    case 'c':
1499 376253ec aliguori
        monitor_printc(mon, val);
1500 92a31b1f bellard
        break;
1501 92a31b1f bellard
    }
1502 92a31b1f bellard
#endif
1503 376253ec aliguori
    monitor_printf(mon, "\n");
1504 9307c4c1 bellard
}
1505 9307c4c1 bellard
1506 9869622e Luiz Capitulino
static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
1507 b371dc59 bellard
{
1508 b371dc59 bellard
    FILE *f;
1509 afe67ef2 Luiz Capitulino
    uint32_t size = qdict_get_int(qdict, "size");
1510 afe67ef2 Luiz Capitulino
    const char *filename = qdict_get_str(qdict, "filename");
1511 afe67ef2 Luiz Capitulino
    target_long addr = qdict_get_int(qdict, "val");
1512 b371dc59 bellard
    uint32_t l;
1513 b371dc59 bellard
    CPUState *env;
1514 b371dc59 bellard
    uint8_t buf[1024];
1515 9869622e Luiz Capitulino
    int ret = -1;
1516 b371dc59 bellard
1517 b371dc59 bellard
    env = mon_get_cpu();
1518 b371dc59 bellard
1519 b371dc59 bellard
    f = fopen(filename, "wb");
1520 b371dc59 bellard
    if (!f) {
1521 ab5b027e Markus Armbruster
        qerror_report(QERR_OPEN_FILE_FAILED, filename);
1522 9869622e Luiz Capitulino
        return -1;
1523 b371dc59 bellard
    }
1524 b371dc59 bellard
    while (size != 0) {
1525 b371dc59 bellard
        l = sizeof(buf);
1526 b371dc59 bellard
        if (l > size)
1527 b371dc59 bellard
            l = size;
1528 b371dc59 bellard
        cpu_memory_rw_debug(env, addr, buf, l, 0);
1529 27e3ddd3 Kirill A. Shutemov
        if (fwrite(buf, 1, l, f) != l) {
1530 27e3ddd3 Kirill A. Shutemov
            monitor_printf(mon, "fwrite() error in do_memory_save\n");
1531 27e3ddd3 Kirill A. Shutemov
            goto exit;
1532 27e3ddd3 Kirill A. Shutemov
        }
1533 b371dc59 bellard
        addr += l;
1534 b371dc59 bellard
        size -= l;
1535 b371dc59 bellard
    }
1536 9869622e Luiz Capitulino
1537 9869622e Luiz Capitulino
    ret = 0;
1538 9869622e Luiz Capitulino
1539 27e3ddd3 Kirill A. Shutemov
exit:
1540 b371dc59 bellard
    fclose(f);
1541 9869622e Luiz Capitulino
    return ret;
1542 b371dc59 bellard
}
1543 b371dc59 bellard
1544 fe38a32a Luiz Capitulino
static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
1545 18f5a8bf Luiz Capitulino
                                    QObject **ret_data)
1546 a8bdf7a6 aurel32
{
1547 a8bdf7a6 aurel32
    FILE *f;
1548 a8bdf7a6 aurel32
    uint32_t l;
1549 a8bdf7a6 aurel32
    uint8_t buf[1024];
1550 afe67ef2 Luiz Capitulino
    uint32_t size = qdict_get_int(qdict, "size");
1551 afe67ef2 Luiz Capitulino
    const char *filename = qdict_get_str(qdict, "filename");
1552 c227f099 Anthony Liguori
    target_phys_addr_t addr = qdict_get_int(qdict, "val");
1553 fe38a32a Luiz Capitulino
    int ret = -1;
1554 a8bdf7a6 aurel32
1555 a8bdf7a6 aurel32
    f = fopen(filename, "wb");
1556 a8bdf7a6 aurel32
    if (!f) {
1557 ab5b027e Markus Armbruster
        qerror_report(QERR_OPEN_FILE_FAILED, filename);
1558 fe38a32a Luiz Capitulino
        return -1;
1559 a8bdf7a6 aurel32
    }
1560 a8bdf7a6 aurel32
    while (size != 0) {
1561 a8bdf7a6 aurel32
        l = sizeof(buf);
1562 a8bdf7a6 aurel32
        if (l > size)
1563 a8bdf7a6 aurel32
            l = size;
1564 a8bdf7a6 aurel32
        cpu_physical_memory_rw(addr, buf, l, 0);
1565 27e3ddd3 Kirill A. Shutemov
        if (fwrite(buf, 1, l, f) != l) {
1566 27e3ddd3 Kirill A. Shutemov
            monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1567 27e3ddd3 Kirill A. Shutemov
            goto exit;
1568 27e3ddd3 Kirill A. Shutemov
        }
1569 a8bdf7a6 aurel32
        fflush(f);
1570 a8bdf7a6 aurel32
        addr += l;
1571 a8bdf7a6 aurel32
        size -= l;
1572 a8bdf7a6 aurel32
    }
1573 fe38a32a Luiz Capitulino
1574 fe38a32a Luiz Capitulino
    ret = 0;
1575 fe38a32a Luiz Capitulino
1576 27e3ddd3 Kirill A. Shutemov
exit:
1577 a8bdf7a6 aurel32
    fclose(f);
1578 fe38a32a Luiz Capitulino
    return ret;
1579 a8bdf7a6 aurel32
}
1580 a8bdf7a6 aurel32
1581 f18c16de Luiz Capitulino
static void do_sum(Monitor *mon, const QDict *qdict)
1582 e4cf1adc bellard
{
1583 e4cf1adc bellard
    uint32_t addr;
1584 e4cf1adc bellard
    uint8_t buf[1];
1585 e4cf1adc bellard
    uint16_t sum;
1586 f18c16de Luiz Capitulino
    uint32_t start = qdict_get_int(qdict, "start");
1587 f18c16de Luiz Capitulino
    uint32_t size = qdict_get_int(qdict, "size");
1588 e4cf1adc bellard
1589 e4cf1adc bellard
    sum = 0;
1590 e4cf1adc bellard
    for(addr = start; addr < (start + size); addr++) {
1591 e4cf1adc bellard
        cpu_physical_memory_rw(addr, buf, 1, 0);
1592 e4cf1adc bellard
        /* BSD sum algorithm ('sum' Unix command) */
1593 e4cf1adc bellard
        sum = (sum >> 1) | (sum << 15);
1594 e4cf1adc bellard
        sum += buf[0];
1595 e4cf1adc bellard
    }
1596 376253ec aliguori
    monitor_printf(mon, "%05d\n", sum);
1597 e4cf1adc bellard
}
1598 e4cf1adc bellard
1599 a3a91a35 bellard
typedef struct {
1600 a3a91a35 bellard
    int keycode;
1601 a3a91a35 bellard
    const char *name;
1602 a3a91a35 bellard
} KeyDef;
1603 a3a91a35 bellard
1604 a3a91a35 bellard
static const KeyDef key_defs[] = {
1605 a3a91a35 bellard
    { 0x2a, "shift" },
1606 a3a91a35 bellard
    { 0x36, "shift_r" },
1607 3b46e624 ths
1608 a3a91a35 bellard
    { 0x38, "alt" },
1609 a3a91a35 bellard
    { 0xb8, "alt_r" },
1610 2ba27c7f ths
    { 0x64, "altgr" },
1611 2ba27c7f ths
    { 0xe4, "altgr_r" },
1612 a3a91a35 bellard
    { 0x1d, "ctrl" },
1613 a3a91a35 bellard
    { 0x9d, "ctrl_r" },
1614 a3a91a35 bellard
1615 a3a91a35 bellard
    { 0xdd, "menu" },
1616 a3a91a35 bellard
1617 a3a91a35 bellard
    { 0x01, "esc" },
1618 a3a91a35 bellard
1619 a3a91a35 bellard
    { 0x02, "1" },
1620 a3a91a35 bellard
    { 0x03, "2" },
1621 a3a91a35 bellard
    { 0x04, "3" },
1622 a3a91a35 bellard
    { 0x05, "4" },
1623 a3a91a35 bellard
    { 0x06, "5" },
1624 a3a91a35 bellard
    { 0x07, "6" },
1625 a3a91a35 bellard
    { 0x08, "7" },
1626 a3a91a35 bellard
    { 0x09, "8" },
1627 a3a91a35 bellard
    { 0x0a, "9" },
1628 a3a91a35 bellard
    { 0x0b, "0" },
1629 64866c3d bellard
    { 0x0c, "minus" },
1630 64866c3d bellard
    { 0x0d, "equal" },
1631 a3a91a35 bellard
    { 0x0e, "backspace" },
1632 a3a91a35 bellard
1633 a3a91a35 bellard
    { 0x0f, "tab" },
1634 a3a91a35 bellard
    { 0x10, "q" },
1635 a3a91a35 bellard
    { 0x11, "w" },
1636 a3a91a35 bellard
    { 0x12, "e" },
1637 a3a91a35 bellard
    { 0x13, "r" },
1638 a3a91a35 bellard
    { 0x14, "t" },
1639 a3a91a35 bellard
    { 0x15, "y" },
1640 a3a91a35 bellard
    { 0x16, "u" },
1641 a3a91a35 bellard
    { 0x17, "i" },
1642 a3a91a35 bellard
    { 0x18, "o" },
1643 a3a91a35 bellard
    { 0x19, "p" },
1644 a3a91a35 bellard
1645 a3a91a35 bellard
    { 0x1c, "ret" },
1646 a3a91a35 bellard
1647 a3a91a35 bellard
    { 0x1e, "a" },
1648 a3a91a35 bellard
    { 0x1f, "s" },
1649 a3a91a35 bellard
    { 0x20, "d" },
1650 a3a91a35 bellard
    { 0x21, "f" },
1651 a3a91a35 bellard
    { 0x22, "g" },
1652 a3a91a35 bellard
    { 0x23, "h" },
1653 a3a91a35 bellard
    { 0x24, "j" },
1654 a3a91a35 bellard
    { 0x25, "k" },
1655 a3a91a35 bellard
    { 0x26, "l" },
1656 a3a91a35 bellard
1657 a3a91a35 bellard
    { 0x2c, "z" },
1658 a3a91a35 bellard
    { 0x2d, "x" },
1659 a3a91a35 bellard
    { 0x2e, "c" },
1660 a3a91a35 bellard
    { 0x2f, "v" },
1661 a3a91a35 bellard
    { 0x30, "b" },
1662 a3a91a35 bellard
    { 0x31, "n" },
1663 a3a91a35 bellard
    { 0x32, "m" },
1664 9155fc45 aurel32
    { 0x33, "comma" },
1665 9155fc45 aurel32
    { 0x34, "dot" },
1666 9155fc45 aurel32
    { 0x35, "slash" },
1667 3b46e624 ths
1668 4d3b6f6e balrog
    { 0x37, "asterisk" },
1669 4d3b6f6e balrog
1670 a3a91a35 bellard
    { 0x39, "spc" },
1671 00ffa62a bellard
    { 0x3a, "caps_lock" },
1672 a3a91a35 bellard
    { 0x3b, "f1" },
1673 a3a91a35 bellard
    { 0x3c, "f2" },
1674 a3a91a35 bellard
    { 0x3d, "f3" },
1675 a3a91a35 bellard
    { 0x3e, "f4" },
1676 a3a91a35 bellard
    { 0x3f, "f5" },
1677 a3a91a35 bellard
    { 0x40, "f6" },
1678 a3a91a35 bellard
    { 0x41, "f7" },
1679 a3a91a35 bellard
    { 0x42, "f8" },
1680 a3a91a35 bellard
    { 0x43, "f9" },
1681 a3a91a35 bellard
    { 0x44, "f10" },
1682 00ffa62a bellard
    { 0x45, "num_lock" },
1683 a3a91a35 bellard
    { 0x46, "scroll_lock" },
1684 a3a91a35 bellard
1685 64866c3d bellard
    { 0xb5, "kp_divide" },
1686 64866c3d bellard
    { 0x37, "kp_multiply" },
1687 0cfec834 ths
    { 0x4a, "kp_subtract" },
1688 64866c3d bellard
    { 0x4e, "kp_add" },
1689 64866c3d bellard
    { 0x9c, "kp_enter" },
1690 64866c3d bellard
    { 0x53, "kp_decimal" },
1691 f2289cb6 balrog
    { 0x54, "sysrq" },
1692 64866c3d bellard
1693 64866c3d bellard
    { 0x52, "kp_0" },
1694 64866c3d bellard
    { 0x4f, "kp_1" },
1695 64866c3d bellard
    { 0x50, "kp_2" },
1696 64866c3d bellard
    { 0x51, "kp_3" },
1697 64866c3d bellard
    { 0x4b, "kp_4" },
1698 64866c3d bellard
    { 0x4c, "kp_5" },
1699 64866c3d bellard
    { 0x4d, "kp_6" },
1700 64866c3d bellard
    { 0x47, "kp_7" },
1701 64866c3d bellard
    { 0x48, "kp_8" },
1702 64866c3d bellard
    { 0x49, "kp_9" },
1703 3b46e624 ths
1704 a3a91a35 bellard
    { 0x56, "<" },
1705 a3a91a35 bellard
1706 a3a91a35 bellard
    { 0x57, "f11" },
1707 a3a91a35 bellard
    { 0x58, "f12" },
1708 a3a91a35 bellard
1709 a3a91a35 bellard
    { 0xb7, "print" },
1710 a3a91a35 bellard
1711 a3a91a35 bellard
    { 0xc7, "home" },
1712 a3a91a35 bellard
    { 0xc9, "pgup" },
1713 a3a91a35 bellard
    { 0xd1, "pgdn" },
1714 a3a91a35 bellard
    { 0xcf, "end" },
1715 a3a91a35 bellard
1716 a3a91a35 bellard
    { 0xcb, "left" },
1717 a3a91a35 bellard
    { 0xc8, "up" },
1718 a3a91a35 bellard
    { 0xd0, "down" },
1719 a3a91a35 bellard
    { 0xcd, "right" },
1720 a3a91a35 bellard
1721 a3a91a35 bellard
    { 0xd2, "insert" },
1722 a3a91a35 bellard
    { 0xd3, "delete" },
1723 c0b5b109 blueswir1
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1724 c0b5b109 blueswir1
    { 0xf0, "stop" },
1725 c0b5b109 blueswir1
    { 0xf1, "again" },
1726 c0b5b109 blueswir1
    { 0xf2, "props" },
1727 c0b5b109 blueswir1
    { 0xf3, "undo" },
1728 c0b5b109 blueswir1
    { 0xf4, "front" },
1729 c0b5b109 blueswir1
    { 0xf5, "copy" },
1730 c0b5b109 blueswir1
    { 0xf6, "open" },
1731 c0b5b109 blueswir1
    { 0xf7, "paste" },
1732 c0b5b109 blueswir1
    { 0xf8, "find" },
1733 c0b5b109 blueswir1
    { 0xf9, "cut" },
1734 c0b5b109 blueswir1
    { 0xfa, "lf" },
1735 c0b5b109 blueswir1
    { 0xfb, "help" },
1736 c0b5b109 blueswir1
    { 0xfc, "meta_l" },
1737 c0b5b109 blueswir1
    { 0xfd, "meta_r" },
1738 c0b5b109 blueswir1
    { 0xfe, "compose" },
1739 c0b5b109 blueswir1
#endif
1740 a3a91a35 bellard
    { 0, NULL },
1741 a3a91a35 bellard
};
1742 a3a91a35 bellard
1743 a3a91a35 bellard
static int get_keycode(const char *key)
1744 a3a91a35 bellard
{
1745 a3a91a35 bellard
    const KeyDef *p;
1746 64866c3d bellard
    char *endp;
1747 64866c3d bellard
    int ret;
1748 a3a91a35 bellard
1749 a3a91a35 bellard
    for(p = key_defs; p->name != NULL; p++) {
1750 a3a91a35 bellard
        if (!strcmp(key, p->name))
1751 a3a91a35 bellard
            return p->keycode;
1752 a3a91a35 bellard
    }
1753 64866c3d bellard
    if (strstart(key, "0x", NULL)) {
1754 64866c3d bellard
        ret = strtoul(key, &endp, 0);
1755 64866c3d bellard
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1756 64866c3d bellard
            return ret;
1757 64866c3d bellard
    }
1758 a3a91a35 bellard
    return -1;
1759 a3a91a35 bellard
}
1760 a3a91a35 bellard
1761 c8256f9d balrog
#define MAX_KEYCODES 16
1762 c8256f9d balrog
static uint8_t keycodes[MAX_KEYCODES];
1763 c8256f9d balrog
static int nb_pending_keycodes;
1764 c8256f9d balrog
static QEMUTimer *key_timer;
1765 c8256f9d balrog
1766 c8256f9d balrog
static void release_keys(void *opaque)
1767 c8256f9d balrog
{
1768 c8256f9d balrog
    int keycode;
1769 c8256f9d balrog
1770 c8256f9d balrog
    while (nb_pending_keycodes > 0) {
1771 c8256f9d balrog
        nb_pending_keycodes--;
1772 c8256f9d balrog
        keycode = keycodes[nb_pending_keycodes];
1773 c8256f9d balrog
        if (keycode & 0x80)
1774 c8256f9d balrog
            kbd_put_keycode(0xe0);
1775 c8256f9d balrog
        kbd_put_keycode(keycode | 0x80);
1776 c8256f9d balrog
    }
1777 c8256f9d balrog
}
1778 c8256f9d balrog
1779 1d4daa91 Luiz Capitulino
static void do_sendkey(Monitor *mon, const QDict *qdict)
1780 a3a91a35 bellard
{
1781 3401c0d9 balrog
    char keyname_buf[16];
1782 3401c0d9 balrog
    char *separator;
1783 3401c0d9 balrog
    int keyname_len, keycode, i;
1784 1d4daa91 Luiz Capitulino
    const char *string = qdict_get_str(qdict, "string");
1785 1d4daa91 Luiz Capitulino
    int has_hold_time = qdict_haskey(qdict, "hold_time");
1786 1d4daa91 Luiz Capitulino
    int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1787 3401c0d9 balrog
1788 c8256f9d balrog
    if (nb_pending_keycodes > 0) {
1789 c8256f9d balrog
        qemu_del_timer(key_timer);
1790 c8256f9d balrog
        release_keys(NULL);
1791 c8256f9d balrog
    }
1792 c8256f9d balrog
    if (!has_hold_time)
1793 c8256f9d balrog
        hold_time = 100;
1794 c8256f9d balrog
    i = 0;
1795 3401c0d9 balrog
    while (1) {
1796 3401c0d9 balrog
        separator = strchr(string, '-');
1797 3401c0d9 balrog
        keyname_len = separator ? separator - string : strlen(string);
1798 3401c0d9 balrog
        if (keyname_len > 0) {
1799 3401c0d9 balrog
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1800 3401c0d9 balrog
            if (keyname_len > sizeof(keyname_buf) - 1) {
1801 376253ec aliguori
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1802 3401c0d9 balrog
                return;
1803 a3a91a35 bellard
            }
1804 c8256f9d balrog
            if (i == MAX_KEYCODES) {
1805 376253ec aliguori
                monitor_printf(mon, "too many keys\n");
1806 3401c0d9 balrog
                return;
1807 3401c0d9 balrog
            }
1808 3401c0d9 balrog
            keyname_buf[keyname_len] = 0;
1809 3401c0d9 balrog
            keycode = get_keycode(keyname_buf);
1810 3401c0d9 balrog
            if (keycode < 0) {
1811 376253ec aliguori
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1812 3401c0d9 balrog
                return;
1813 3401c0d9 balrog
            }
1814 c8256f9d balrog
            keycodes[i++] = keycode;
1815 a3a91a35 bellard
        }
1816 3401c0d9 balrog
        if (!separator)
1817 a3a91a35 bellard
            break;
1818 3401c0d9 balrog
        string = separator + 1;
1819 a3a91a35 bellard
    }
1820 c8256f9d balrog
    nb_pending_keycodes = i;
1821 a3a91a35 bellard
    /* key down events */
1822 c8256f9d balrog
    for (i = 0; i < nb_pending_keycodes; i++) {
1823 a3a91a35 bellard
        keycode = keycodes[i];
1824 a3a91a35 bellard
        if (keycode & 0x80)
1825 a3a91a35 bellard
            kbd_put_keycode(0xe0);
1826 a3a91a35 bellard
        kbd_put_keycode(keycode & 0x7f);
1827 a3a91a35 bellard
    }
1828 c8256f9d balrog
    /* delayed key up events */
1829 f227f17d balrog
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1830 6ee093c9 Juan Quintela
                   muldiv64(get_ticks_per_sec(), hold_time, 1000));
1831 a3a91a35 bellard
}
1832 a3a91a35 bellard
1833 13224a87 bellard
static int mouse_button_state;
1834 13224a87 bellard
1835 1d4daa91 Luiz Capitulino
static void do_mouse_move(Monitor *mon, const QDict *qdict)
1836 13224a87 bellard
{
1837 13224a87 bellard
    int dx, dy, dz;
1838 1d4daa91 Luiz Capitulino
    const char *dx_str = qdict_get_str(qdict, "dx_str");
1839 1d4daa91 Luiz Capitulino
    const char *dy_str = qdict_get_str(qdict, "dy_str");
1840 1d4daa91 Luiz Capitulino
    const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1841 13224a87 bellard
    dx = strtol(dx_str, NULL, 0);
1842 13224a87 bellard
    dy = strtol(dy_str, NULL, 0);
1843 13224a87 bellard
    dz = 0;
1844 5fafdf24 ths
    if (dz_str)
1845 13224a87 bellard
        dz = strtol(dz_str, NULL, 0);
1846 13224a87 bellard
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1847 13224a87 bellard
}
1848 13224a87 bellard
1849 d54908a5 Luiz Capitulino
static void do_mouse_button(Monitor *mon, const QDict *qdict)
1850 13224a87 bellard
{
1851 d54908a5 Luiz Capitulino
    int button_state = qdict_get_int(qdict, "button_state");
1852 13224a87 bellard
    mouse_button_state = button_state;
1853 13224a87 bellard
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1854 13224a87 bellard
}
1855 13224a87 bellard
1856 aa93e39c Luiz Capitulino
static void do_ioport_read(Monitor *mon, const QDict *qdict)
1857 3440557b bellard
{
1858 aa93e39c Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
1859 aa93e39c Luiz Capitulino
    int addr = qdict_get_int(qdict, "addr");
1860 aa93e39c Luiz Capitulino
    int has_index = qdict_haskey(qdict, "index");
1861 3440557b bellard
    uint32_t val;
1862 3440557b bellard
    int suffix;
1863 3440557b bellard
1864 3440557b bellard
    if (has_index) {
1865 aa93e39c Luiz Capitulino
        int index = qdict_get_int(qdict, "index");
1866 afcea8cb Blue Swirl
        cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1867 3440557b bellard
        addr++;
1868 3440557b bellard
    }
1869 3440557b bellard
    addr &= 0xffff;
1870 3440557b bellard
1871 3440557b bellard
    switch(size) {
1872 3440557b bellard
    default:
1873 3440557b bellard
    case 1:
1874 afcea8cb Blue Swirl
        val = cpu_inb(addr);
1875 3440557b bellard
        suffix = 'b';
1876 3440557b bellard
        break;
1877 3440557b bellard
    case 2:
1878 afcea8cb Blue Swirl
        val = cpu_inw(addr);
1879 3440557b bellard
        suffix = 'w';
1880 3440557b bellard
        break;
1881 3440557b bellard
    case 4:
1882 afcea8cb Blue Swirl
        val = cpu_inl(addr);
1883 3440557b bellard
        suffix = 'l';
1884 3440557b bellard
        break;
1885 3440557b bellard
    }
1886 376253ec aliguori
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1887 376253ec aliguori
                   suffix, addr, size * 2, val);
1888 3440557b bellard
}
1889 a3a91a35 bellard
1890 1bd1442e Luiz Capitulino
static void do_ioport_write(Monitor *mon, const QDict *qdict)
1891 f114784f Jan Kiszka
{
1892 1bd1442e Luiz Capitulino
    int size = qdict_get_int(qdict, "size");
1893 1bd1442e Luiz Capitulino
    int addr = qdict_get_int(qdict, "addr");
1894 1bd1442e Luiz Capitulino
    int val = qdict_get_int(qdict, "val");
1895 1bd1442e Luiz Capitulino
1896 f114784f Jan Kiszka
    addr &= IOPORTS_MASK;
1897 f114784f Jan Kiszka
1898 f114784f Jan Kiszka
    switch (size) {
1899 f114784f Jan Kiszka
    default:
1900 f114784f Jan Kiszka
    case 1:
1901 afcea8cb Blue Swirl
        cpu_outb(addr, val);
1902 f114784f Jan Kiszka
        break;
1903 f114784f Jan Kiszka
    case 2:
1904 afcea8cb Blue Swirl
        cpu_outw(addr, val);
1905 f114784f Jan Kiszka
        break;
1906 f114784f Jan Kiszka
    case 4:
1907 afcea8cb Blue Swirl
        cpu_outl(addr, val);
1908 f114784f Jan Kiszka
        break;
1909 f114784f Jan Kiszka
    }
1910 f114784f Jan Kiszka
}
1911 f114784f Jan Kiszka
1912 d54908a5 Luiz Capitulino
static void do_boot_set(Monitor *mon, const QDict *qdict)
1913 0ecdffbb aurel32
{
1914 0ecdffbb aurel32
    int res;
1915 d54908a5 Luiz Capitulino
    const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1916 0ecdffbb aurel32
1917 76e30d0f Jan Kiszka
    res = qemu_boot_set(bootdevice);
1918 76e30d0f Jan Kiszka
    if (res == 0) {
1919 76e30d0f Jan Kiszka
        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1920 76e30d0f Jan Kiszka
    } else if (res > 0) {
1921 76e30d0f Jan Kiszka
        monitor_printf(mon, "setting boot device list failed\n");
1922 0ecdffbb aurel32
    } else {
1923 376253ec aliguori
        monitor_printf(mon, "no function defined to set boot device list for "
1924 376253ec aliguori
                       "this architecture\n");
1925 0ecdffbb aurel32
    }
1926 0ecdffbb aurel32
}
1927 0ecdffbb aurel32
1928 c80d259e Luiz Capitulino
/**
1929 c80d259e Luiz Capitulino
 * do_system_reset(): Issue a machine reset
1930 c80d259e Luiz Capitulino
 */
1931 ef4b7eee Luiz Capitulino
static int do_system_reset(Monitor *mon, const QDict *qdict,
1932 ef4b7eee Luiz Capitulino
                           QObject **ret_data)
1933 e4f9082b bellard
{
1934 e4f9082b bellard
    qemu_system_reset_request();
1935 ef4b7eee Luiz Capitulino
    return 0;
1936 e4f9082b bellard
}
1937 e4f9082b bellard
1938 43076664 Luiz Capitulino
/**
1939 43076664 Luiz Capitulino
 * do_system_powerdown(): Issue a machine powerdown
1940 43076664 Luiz Capitulino
 */
1941 ef4b7eee Luiz Capitulino
static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1942 ef4b7eee Luiz Capitulino
                               QObject **ret_data)
1943 3475187d bellard
{
1944 3475187d bellard
    qemu_system_powerdown_request();
1945 ef4b7eee Luiz Capitulino
    return 0;
1946 3475187d bellard
}
1947 3475187d bellard
1948 b86bda5b bellard
#if defined(TARGET_I386)
1949 376253ec aliguori
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1950 b86bda5b bellard
{
1951 376253ec aliguori
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1952 376253ec aliguori
                   addr,
1953 376253ec aliguori
                   pte & mask,
1954 376253ec aliguori
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
1955 376253ec aliguori
                   pte & PG_PSE_MASK ? 'P' : '-',
1956 376253ec aliguori
                   pte & PG_DIRTY_MASK ? 'D' : '-',
1957 376253ec aliguori
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
1958 376253ec aliguori
                   pte & PG_PCD_MASK ? 'C' : '-',
1959 376253ec aliguori
                   pte & PG_PWT_MASK ? 'T' : '-',
1960 376253ec aliguori
                   pte & PG_USER_MASK ? 'U' : '-',
1961 376253ec aliguori
                   pte & PG_RW_MASK ? 'W' : '-');
1962 b86bda5b bellard
}
1963 b86bda5b bellard
1964 376253ec aliguori
static void tlb_info(Monitor *mon)
1965 b86bda5b bellard
{
1966 6a00d601 bellard
    CPUState *env;
1967 b86bda5b bellard
    int l1, l2;
1968 b86bda5b bellard
    uint32_t pgd, pde, pte;
1969 b86bda5b bellard
1970 6a00d601 bellard
    env = mon_get_cpu();
1971 6a00d601 bellard
1972 b86bda5b bellard
    if (!(env->cr[0] & CR0_PG_MASK)) {
1973 376253ec aliguori
        monitor_printf(mon, "PG disabled\n");
1974 b86bda5b bellard
        return;
1975 b86bda5b bellard
    }
1976 b86bda5b bellard
    pgd = env->cr[3] & ~0xfff;
1977 b86bda5b bellard
    for(l1 = 0; l1 < 1024; l1++) {
1978 b86bda5b bellard
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1979 b86bda5b bellard
        pde = le32_to_cpu(pde);
1980 b86bda5b bellard
        if (pde & PG_PRESENT_MASK) {
1981 b86bda5b bellard
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1982 376253ec aliguori
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1983 b86bda5b bellard
            } else {
1984 b86bda5b bellard
                for(l2 = 0; l2 < 1024; l2++) {
1985 5fafdf24 ths
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1986 b86bda5b bellard
                                             (uint8_t *)&pte, 4);
1987 b86bda5b bellard
                    pte = le32_to_cpu(pte);
1988 b86bda5b bellard
                    if (pte & PG_PRESENT_MASK) {
1989 376253ec aliguori
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1990 5fafdf24 ths
                                  pte & ~PG_PSE_MASK,
1991 b86bda5b bellard
                                  ~0xfff);
1992 b86bda5b bellard
                    }
1993 b86bda5b bellard
                }
1994 b86bda5b bellard
            }
1995 b86bda5b bellard
        }
1996 b86bda5b bellard
    }
1997 b86bda5b bellard
}
1998 b86bda5b bellard
1999 376253ec aliguori
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
2000 b86bda5b bellard
                      uint32_t end, int prot)
2001 b86bda5b bellard
{
2002 9746b15b bellard
    int prot1;
2003 9746b15b bellard
    prot1 = *plast_prot;
2004 9746b15b bellard
    if (prot != prot1) {
2005 b86bda5b bellard
        if (*pstart != -1) {
2006 376253ec aliguori
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
2007 376253ec aliguori
                           *pstart, end, end - *pstart,
2008 376253ec aliguori
                           prot1 & PG_USER_MASK ? 'u' : '-',
2009 376253ec aliguori
                           'r',
2010 376253ec aliguori
                           prot1 & PG_RW_MASK ? 'w' : '-');
2011 b86bda5b bellard
        }
2012 b86bda5b bellard
        if (prot != 0)
2013 b86bda5b bellard
            *pstart = end;
2014 b86bda5b bellard
        else
2015 b86bda5b bellard
            *pstart = -1;
2016 b86bda5b bellard
        *plast_prot = prot;
2017 b86bda5b bellard
    }
2018 b86bda5b bellard
}
2019 b86bda5b bellard
2020 376253ec aliguori
static void mem_info(Monitor *mon)
2021 b86bda5b bellard
{
2022 6a00d601 bellard
    CPUState *env;
2023 b86bda5b bellard
    int l1, l2, prot, last_prot;
2024 b86bda5b bellard
    uint32_t pgd, pde, pte, start, end;
2025 b86bda5b bellard
2026 6a00d601 bellard
    env = mon_get_cpu();
2027 6a00d601 bellard
2028 b86bda5b bellard
    if (!(env->cr[0] & CR0_PG_MASK)) {
2029 376253ec aliguori
        monitor_printf(mon, "PG disabled\n");
2030 b86bda5b bellard
        return;
2031 b86bda5b bellard
    }
2032 b86bda5b bellard
    pgd = env->cr[3] & ~0xfff;
2033 b86bda5b bellard
    last_prot = 0;
2034 b86bda5b bellard
    start = -1;
2035 b86bda5b bellard
    for(l1 = 0; l1 < 1024; l1++) {
2036 b86bda5b bellard
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
2037 b86bda5b bellard
        pde = le32_to_cpu(pde);
2038 b86bda5b bellard
        end = l1 << 22;
2039 b86bda5b bellard
        if (pde & PG_PRESENT_MASK) {
2040 b86bda5b bellard
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2041 b86bda5b bellard
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2042 376253ec aliguori
                mem_print(mon, &start, &last_prot, end, prot);
2043 b86bda5b bellard
            } else {
2044 b86bda5b bellard
                for(l2 = 0; l2 < 1024; l2++) {
2045 5fafdf24 ths
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
2046 b86bda5b bellard
                                             (uint8_t *)&pte, 4);
2047 b86bda5b bellard
                    pte = le32_to_cpu(pte);
2048 b86bda5b bellard
                    end = (l1 << 22) + (l2 << 12);
2049 b86bda5b bellard
                    if (pte & PG_PRESENT_MASK) {
2050 b86bda5b bellard
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2051 b86bda5b bellard
                    } else {
2052 b86bda5b bellard
                        prot = 0;
2053 b86bda5b bellard
                    }
2054 376253ec aliguori
                    mem_print(mon, &start, &last_prot, end, prot);
2055 b86bda5b bellard
                }
2056 b86bda5b bellard
            }
2057 b86bda5b bellard
        } else {
2058 b86bda5b bellard
            prot = 0;
2059 376253ec aliguori
            mem_print(mon, &start, &last_prot, end, prot);
2060 b86bda5b bellard
        }
2061 b86bda5b bellard
    }
2062 b86bda5b bellard
}
2063 b86bda5b bellard
#endif
2064 b86bda5b bellard
2065 7c664e2f aurel32
#if defined(TARGET_SH4)
2066 7c664e2f aurel32
2067 376253ec aliguori
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
2068 7c664e2f aurel32
{
2069 376253ec aliguori
    monitor_printf(mon, " tlb%i:\t"
2070 376253ec aliguori
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2071 376253ec aliguori
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2072 376253ec aliguori
                   "dirty=%hhu writethrough=%hhu\n",
2073 376253ec aliguori
                   idx,
2074 376253ec aliguori
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2075 376253ec aliguori
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
2076 376253ec aliguori
                   tlb->d, tlb->wt);
2077 7c664e2f aurel32
}
2078 7c664e2f aurel32
2079 376253ec aliguori
static void tlb_info(Monitor *mon)
2080 7c664e2f aurel32
{
2081 7c664e2f aurel32
    CPUState *env = mon_get_cpu();
2082 7c664e2f aurel32
    int i;
2083 7c664e2f aurel32
2084 376253ec aliguori
    monitor_printf (mon, "ITLB:\n");
2085 7c664e2f aurel32
    for (i = 0 ; i < ITLB_SIZE ; i++)
2086 376253ec aliguori
        print_tlb (mon, i, &env->itlb[i]);
2087 376253ec aliguori
    monitor_printf (mon, "UTLB:\n");
2088 7c664e2f aurel32
    for (i = 0 ; i < UTLB_SIZE ; i++)
2089 376253ec aliguori
        print_tlb (mon, i, &env->utlb[i]);
2090 7c664e2f aurel32
}
2091 7c664e2f aurel32
2092 7c664e2f aurel32
#endif
2093 7c664e2f aurel32
2094 2af5ba71 Luiz Capitulino
static void do_info_kvm_print(Monitor *mon, const QObject *data)
2095 7ba1e619 aliguori
{
2096 2af5ba71 Luiz Capitulino
    QDict *qdict;
2097 2af5ba71 Luiz Capitulino
2098 2af5ba71 Luiz Capitulino
    qdict = qobject_to_qdict(data);
2099 2af5ba71 Luiz Capitulino
2100 376253ec aliguori
    monitor_printf(mon, "kvm support: ");
2101 2af5ba71 Luiz Capitulino
    if (qdict_get_bool(qdict, "present")) {
2102 2af5ba71 Luiz Capitulino
        monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
2103 2af5ba71 Luiz Capitulino
                                    "enabled" : "disabled");
2104 2af5ba71 Luiz Capitulino
    } else {
2105 2af5ba71 Luiz Capitulino
        monitor_printf(mon, "not compiled\n");
2106 2af5ba71 Luiz Capitulino
    }
2107 2af5ba71 Luiz Capitulino
}
2108 2af5ba71 Luiz Capitulino
2109 2af5ba71 Luiz Capitulino
/**
2110 2af5ba71 Luiz Capitulino
 * do_info_kvm(): Show KVM information
2111 2af5ba71 Luiz Capitulino
 *
2112 2af5ba71 Luiz Capitulino
 * Return a QDict with the following information:
2113 2af5ba71 Luiz Capitulino
 *
2114 2af5ba71 Luiz Capitulino
 * - "enabled": true if KVM support is enabled, false otherwise
2115 2af5ba71 Luiz Capitulino
 * - "present": true if QEMU has KVM support, false otherwise
2116 2af5ba71 Luiz Capitulino
 *
2117 2af5ba71 Luiz Capitulino
 * Example:
2118 2af5ba71 Luiz Capitulino
 *
2119 2af5ba71 Luiz Capitulino
 * { "enabled": true, "present": true }
2120 2af5ba71 Luiz Capitulino
 */
2121 2af5ba71 Luiz Capitulino
static void do_info_kvm(Monitor *mon, QObject **ret_data)
2122 2af5ba71 Luiz Capitulino
{
2123 2af5ba71 Luiz Capitulino
#ifdef CONFIG_KVM
2124 2af5ba71 Luiz Capitulino
    *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2125 2af5ba71 Luiz Capitulino
                                   kvm_enabled());
2126 7ba1e619 aliguori
#else
2127 2af5ba71 Luiz Capitulino
    *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2128 7ba1e619 aliguori
#endif
2129 7ba1e619 aliguori
}
2130 7ba1e619 aliguori
2131 030ea37b aliguori
static void do_info_numa(Monitor *mon)
2132 030ea37b aliguori
{
2133 b28b6230 aliguori
    int i;
2134 030ea37b aliguori
    CPUState *env;
2135 030ea37b aliguori
2136 030ea37b aliguori
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2137 030ea37b aliguori
    for (i = 0; i < nb_numa_nodes; i++) {
2138 030ea37b aliguori
        monitor_printf(mon, "node %d cpus:", i);
2139 030ea37b aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
2140 030ea37b aliguori
            if (env->numa_node == i) {
2141 030ea37b aliguori
                monitor_printf(mon, " %d", env->cpu_index);
2142 030ea37b aliguori
            }
2143 030ea37b aliguori
        }
2144 030ea37b aliguori
        monitor_printf(mon, "\n");
2145 030ea37b aliguori
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2146 030ea37b aliguori
            node_mem[i] >> 20);
2147 030ea37b aliguori
    }
2148 030ea37b aliguori
}
2149 030ea37b aliguori
2150 5f1ce948 bellard
#ifdef CONFIG_PROFILER
2151 5f1ce948 bellard
2152 e9a6625e Aurelien Jarno
int64_t qemu_time;
2153 e9a6625e Aurelien Jarno
int64_t dev_time;
2154 e9a6625e Aurelien Jarno
2155 376253ec aliguori
static void do_info_profile(Monitor *mon)
2156 5f1ce948 bellard
{
2157 5f1ce948 bellard
    int64_t total;
2158 5f1ce948 bellard
    total = qemu_time;
2159 5f1ce948 bellard
    if (total == 0)
2160 5f1ce948 bellard
        total = 1;
2161 376253ec aliguori
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
2162 6ee093c9 Juan Quintela
                   dev_time, dev_time / (double)get_ticks_per_sec());
2163 376253ec aliguori
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
2164 6ee093c9 Juan Quintela
                   qemu_time, qemu_time / (double)get_ticks_per_sec());
2165 5f1ce948 bellard
    qemu_time = 0;
2166 5f1ce948 bellard
    dev_time = 0;
2167 5f1ce948 bellard
}
2168 5f1ce948 bellard
#else
2169 376253ec aliguori
static void do_info_profile(Monitor *mon)
2170 5f1ce948 bellard
{
2171 376253ec aliguori
    monitor_printf(mon, "Internal profiler not compiled\n");
2172 5f1ce948 bellard
}
2173 5f1ce948 bellard
#endif
2174 5f1ce948 bellard
2175 ec36b695 bellard
/* Capture support */
2176 72cf2d4f Blue Swirl
static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
2177 ec36b695 bellard
2178 376253ec aliguori
static void do_info_capture(Monitor *mon)
2179 ec36b695 bellard
{
2180 ec36b695 bellard
    int i;
2181 ec36b695 bellard
    CaptureState *s;
2182 ec36b695 bellard
2183 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2184 376253ec aliguori
        monitor_printf(mon, "[%d]: ", i);
2185 ec36b695 bellard
        s->ops.info (s->opaque);
2186 ec36b695 bellard
    }
2187 ec36b695 bellard
}
2188 ec36b695 bellard
2189 2313086a Blue Swirl
#ifdef HAS_AUDIO
2190 d54908a5 Luiz Capitulino
static void do_stop_capture(Monitor *mon, const QDict *qdict)
2191 ec36b695 bellard
{
2192 ec36b695 bellard
    int i;
2193 d54908a5 Luiz Capitulino
    int n = qdict_get_int(qdict, "n");
2194 ec36b695 bellard
    CaptureState *s;
2195 ec36b695 bellard
2196 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2197 ec36b695 bellard
        if (i == n) {
2198 ec36b695 bellard
            s->ops.destroy (s->opaque);
2199 72cf2d4f Blue Swirl
            QLIST_REMOVE (s, entries);
2200 ec36b695 bellard
            qemu_free (s);
2201 ec36b695 bellard
            return;
2202 ec36b695 bellard
        }
2203 ec36b695 bellard
    }
2204 ec36b695 bellard
}
2205 ec36b695 bellard
2206 c1925484 Luiz Capitulino
static void do_wav_capture(Monitor *mon, const QDict *qdict)
2207 c1925484 Luiz Capitulino
{
2208 c1925484 Luiz Capitulino
    const char *path = qdict_get_str(qdict, "path");
2209 c1925484 Luiz Capitulino
    int has_freq = qdict_haskey(qdict, "freq");
2210 c1925484 Luiz Capitulino
    int freq = qdict_get_try_int(qdict, "freq", -1);
2211 c1925484 Luiz Capitulino
    int has_bits = qdict_haskey(qdict, "bits");
2212 c1925484 Luiz Capitulino
    int bits = qdict_get_try_int(qdict, "bits", -1);
2213 c1925484 Luiz Capitulino
    int has_channels = qdict_haskey(qdict, "nchannels");
2214 c1925484 Luiz Capitulino
    int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2215 ec36b695 bellard
    CaptureState *s;
2216 ec36b695 bellard
2217 ec36b695 bellard
    s = qemu_mallocz (sizeof (*s));
2218 ec36b695 bellard
2219 ec36b695 bellard
    freq = has_freq ? freq : 44100;
2220 ec36b695 bellard
    bits = has_bits ? bits : 16;
2221 ec36b695 bellard
    nchannels = has_channels ? nchannels : 2;
2222 ec36b695 bellard
2223 ec36b695 bellard
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
2224 376253ec aliguori
        monitor_printf(mon, "Faied to add wave capture\n");
2225 ec36b695 bellard
        qemu_free (s);
2226 ec36b695 bellard
    }
2227 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD (&capture_head, s, entries);
2228 ec36b695 bellard
}
2229 ec36b695 bellard
#endif
2230 ec36b695 bellard
2231 dc1c0b74 aurel32
#if defined(TARGET_I386)
2232 d54908a5 Luiz Capitulino
static void do_inject_nmi(Monitor *mon, const QDict *qdict)
2233 dc1c0b74 aurel32
{
2234 dc1c0b74 aurel32
    CPUState *env;
2235 d54908a5 Luiz Capitulino
    int cpu_index = qdict_get_int(qdict, "cpu_index");
2236 dc1c0b74 aurel32
2237 dc1c0b74 aurel32
    for (env = first_cpu; env != NULL; env = env->next_cpu)
2238 dc1c0b74 aurel32
        if (env->cpu_index == cpu_index) {
2239 dc1c0b74 aurel32
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
2240 dc1c0b74 aurel32
            break;
2241 dc1c0b74 aurel32
        }
2242 dc1c0b74 aurel32
}
2243 dc1c0b74 aurel32
#endif
2244 dc1c0b74 aurel32
2245 c0e8520e Luiz Capitulino
static void do_info_status_print(Monitor *mon, const QObject *data)
2246 6f9c5ee7 aurel32
{
2247 c0e8520e Luiz Capitulino
    QDict *qdict;
2248 c0e8520e Luiz Capitulino
2249 c0e8520e Luiz Capitulino
    qdict = qobject_to_qdict(data);
2250 c0e8520e Luiz Capitulino
2251 c0e8520e Luiz Capitulino
    monitor_printf(mon, "VM status: ");
2252 c0e8520e Luiz Capitulino
    if (qdict_get_bool(qdict, "running")) {
2253 c0e8520e Luiz Capitulino
        monitor_printf(mon, "running");
2254 c0e8520e Luiz Capitulino
        if (qdict_get_bool(qdict, "singlestep")) {
2255 c0e8520e Luiz Capitulino
            monitor_printf(mon, " (single step mode)");
2256 1b530a6d aurel32
        }
2257 c0e8520e Luiz Capitulino
    } else {
2258 c0e8520e Luiz Capitulino
        monitor_printf(mon, "paused");
2259 c0e8520e Luiz Capitulino
    }
2260 c0e8520e Luiz Capitulino
2261 c0e8520e Luiz Capitulino
    monitor_printf(mon, "\n");
2262 c0e8520e Luiz Capitulino
}
2263 c0e8520e Luiz Capitulino
2264 c0e8520e Luiz Capitulino
/**
2265 c0e8520e Luiz Capitulino
 * do_info_status(): VM status
2266 c0e8520e Luiz Capitulino
 *
2267 c0e8520e Luiz Capitulino
 * Return a QDict with the following information:
2268 c0e8520e Luiz Capitulino
 *
2269 c0e8520e Luiz Capitulino
 * - "running": true if the VM is running, or false if it is paused
2270 c0e8520e Luiz Capitulino
 * - "singlestep": true if the VM is in single step mode, false otherwise
2271 c0e8520e Luiz Capitulino
 *
2272 c0e8520e Luiz Capitulino
 * Example:
2273 c0e8520e Luiz Capitulino
 *
2274 c0e8520e Luiz Capitulino
 * { "running": true, "singlestep": false }
2275 c0e8520e Luiz Capitulino
 */
2276 c0e8520e Luiz Capitulino
static void do_info_status(Monitor *mon, QObject **ret_data)
2277 c0e8520e Luiz Capitulino
{
2278 c0e8520e Luiz Capitulino
    *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2279 c0e8520e Luiz Capitulino
                                    vm_running, singlestep);
2280 6f9c5ee7 aurel32
}
2281 6f9c5ee7 aurel32
2282 15dfcd45 Jan Kiszka
static qemu_acl *find_acl(Monitor *mon, const char *name)
2283 76655d6d aliguori
{
2284 15dfcd45 Jan Kiszka
    qemu_acl *acl = qemu_acl_find(name);
2285 76655d6d aliguori
2286 76655d6d aliguori
    if (!acl) {
2287 15dfcd45 Jan Kiszka
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
2288 76655d6d aliguori
    }
2289 15dfcd45 Jan Kiszka
    return acl;
2290 15dfcd45 Jan Kiszka
}
2291 15dfcd45 Jan Kiszka
2292 d54908a5 Luiz Capitulino
static void do_acl_show(Monitor *mon, const QDict *qdict)
2293 15dfcd45 Jan Kiszka
{
2294 d54908a5 Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
2295 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
2296 15dfcd45 Jan Kiszka
    qemu_acl_entry *entry;
2297 15dfcd45 Jan Kiszka
    int i = 0;
2298 76655d6d aliguori
2299 15dfcd45 Jan Kiszka
    if (acl) {
2300 28a76be8 aliguori
        monitor_printf(mon, "policy: %s\n",
2301 76655d6d aliguori
                       acl->defaultDeny ? "deny" : "allow");
2302 72cf2d4f Blue Swirl
        QTAILQ_FOREACH(entry, &acl->entries, next) {
2303 28a76be8 aliguori
            i++;
2304 28a76be8 aliguori
            monitor_printf(mon, "%d: %s %s\n", i,
2305 15dfcd45 Jan Kiszka
                           entry->deny ? "deny" : "allow", entry->match);
2306 28a76be8 aliguori
        }
2307 15dfcd45 Jan Kiszka
    }
2308 15dfcd45 Jan Kiszka
}
2309 15dfcd45 Jan Kiszka
2310 d54908a5 Luiz Capitulino
static void do_acl_reset(Monitor *mon, const QDict *qdict)
2311 15dfcd45 Jan Kiszka
{
2312 d54908a5 Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
2313 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
2314 15dfcd45 Jan Kiszka
2315 15dfcd45 Jan Kiszka
    if (acl) {
2316 28a76be8 aliguori
        qemu_acl_reset(acl);
2317 28a76be8 aliguori
        monitor_printf(mon, "acl: removed all rules\n");
2318 15dfcd45 Jan Kiszka
    }
2319 15dfcd45 Jan Kiszka
}
2320 15dfcd45 Jan Kiszka
2321 f18c16de Luiz Capitulino
static void do_acl_policy(Monitor *mon, const QDict *qdict)
2322 15dfcd45 Jan Kiszka
{
2323 f18c16de Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
2324 f18c16de Luiz Capitulino
    const char *policy = qdict_get_str(qdict, "policy");
2325 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
2326 28a76be8 aliguori
2327 15dfcd45 Jan Kiszka
    if (acl) {
2328 15dfcd45 Jan Kiszka
        if (strcmp(policy, "allow") == 0) {
2329 28a76be8 aliguori
            acl->defaultDeny = 0;
2330 28a76be8 aliguori
            monitor_printf(mon, "acl: policy set to 'allow'\n");
2331 15dfcd45 Jan Kiszka
        } else if (strcmp(policy, "deny") == 0) {
2332 28a76be8 aliguori
            acl->defaultDeny = 1;
2333 28a76be8 aliguori
            monitor_printf(mon, "acl: policy set to 'deny'\n");
2334 28a76be8 aliguori
        } else {
2335 15dfcd45 Jan Kiszka
            monitor_printf(mon, "acl: unknown policy '%s', "
2336 15dfcd45 Jan Kiszka
                           "expected 'deny' or 'allow'\n", policy);
2337 28a76be8 aliguori
        }
2338 15dfcd45 Jan Kiszka
    }
2339 15dfcd45 Jan Kiszka
}
2340 28a76be8 aliguori
2341 1bd1442e Luiz Capitulino
static void do_acl_add(Monitor *mon, const QDict *qdict)
2342 15dfcd45 Jan Kiszka
{
2343 1bd1442e Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
2344 1bd1442e Luiz Capitulino
    const char *match = qdict_get_str(qdict, "match");
2345 1bd1442e Luiz Capitulino
    const char *policy = qdict_get_str(qdict, "policy");
2346 1bd1442e Luiz Capitulino
    int has_index = qdict_haskey(qdict, "index");
2347 1bd1442e Luiz Capitulino
    int index = qdict_get_try_int(qdict, "index", -1);
2348 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
2349 15dfcd45 Jan Kiszka
    int deny, ret;
2350 15dfcd45 Jan Kiszka
2351 15dfcd45 Jan Kiszka
    if (acl) {
2352 15dfcd45 Jan Kiszka
        if (strcmp(policy, "allow") == 0) {
2353 15dfcd45 Jan Kiszka
            deny = 0;
2354 15dfcd45 Jan Kiszka
        } else if (strcmp(policy, "deny") == 0) {
2355 15dfcd45 Jan Kiszka
            deny = 1;
2356 15dfcd45 Jan Kiszka
        } else {
2357 15dfcd45 Jan Kiszka
            monitor_printf(mon, "acl: unknown policy '%s', "
2358 15dfcd45 Jan Kiszka
                           "expected 'deny' or 'allow'\n", policy);
2359 28a76be8 aliguori
            return;
2360 28a76be8 aliguori
        }
2361 28a76be8 aliguori
        if (has_index)
2362 28a76be8 aliguori
            ret = qemu_acl_insert(acl, deny, match, index);
2363 28a76be8 aliguori
        else
2364 28a76be8 aliguori
            ret = qemu_acl_append(acl, deny, match);
2365 28a76be8 aliguori
        if (ret < 0)
2366 28a76be8 aliguori
            monitor_printf(mon, "acl: unable to add acl entry\n");
2367 28a76be8 aliguori
        else
2368 28a76be8 aliguori
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
2369 15dfcd45 Jan Kiszka
    }
2370 15dfcd45 Jan Kiszka
}
2371 28a76be8 aliguori
2372 f18c16de Luiz Capitulino
static void do_acl_remove(Monitor *mon, const QDict *qdict)
2373 15dfcd45 Jan Kiszka
{
2374 f18c16de Luiz Capitulino
    const char *aclname = qdict_get_str(qdict, "aclname");
2375 f18c16de Luiz Capitulino
    const char *match = qdict_get_str(qdict, "match");
2376 15dfcd45 Jan Kiszka
    qemu_acl *acl = find_acl(mon, aclname);
2377 15dfcd45 Jan Kiszka
    int ret;
2378 28a76be8 aliguori
2379 15dfcd45 Jan Kiszka
    if (acl) {
2380 28a76be8 aliguori
        ret = qemu_acl_remove(acl, match);
2381 28a76be8 aliguori
        if (ret < 0)
2382 28a76be8 aliguori
            monitor_printf(mon, "acl: no matching acl entry\n");
2383 28a76be8 aliguori
        else
2384 28a76be8 aliguori
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2385 76655d6d aliguori
    }
2386 76655d6d aliguori
}
2387 76655d6d aliguori
2388 79c4f6b0 Huang Ying
#if defined(TARGET_I386)
2389 37b7ad48 Luiz Capitulino
static void do_inject_mce(Monitor *mon, const QDict *qdict)
2390 79c4f6b0 Huang Ying
{
2391 79c4f6b0 Huang Ying
    CPUState *cenv;
2392 37b7ad48 Luiz Capitulino
    int cpu_index = qdict_get_int(qdict, "cpu_index");
2393 37b7ad48 Luiz Capitulino
    int bank = qdict_get_int(qdict, "bank");
2394 37b7ad48 Luiz Capitulino
    uint64_t status = qdict_get_int(qdict, "status");
2395 37b7ad48 Luiz Capitulino
    uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2396 37b7ad48 Luiz Capitulino
    uint64_t addr = qdict_get_int(qdict, "addr");
2397 37b7ad48 Luiz Capitulino
    uint64_t misc = qdict_get_int(qdict, "misc");
2398 79c4f6b0 Huang Ying
2399 79c4f6b0 Huang Ying
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2400 79c4f6b0 Huang Ying
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2401 79c4f6b0 Huang Ying
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2402 79c4f6b0 Huang Ying
            break;
2403 79c4f6b0 Huang Ying
        }
2404 79c4f6b0 Huang Ying
}
2405 79c4f6b0 Huang Ying
#endif
2406 79c4f6b0 Huang Ying
2407 6ad3ebd2 Luiz Capitulino
static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2408 f07918fd Mark McLoughlin
{
2409 d54908a5 Luiz Capitulino
    const char *fdname = qdict_get_str(qdict, "fdname");
2410 c227f099 Anthony Liguori
    mon_fd_t *monfd;
2411 f07918fd Mark McLoughlin
    int fd;
2412 f07918fd Mark McLoughlin
2413 f07918fd Mark McLoughlin
    fd = qemu_chr_get_msgfd(mon->chr);
2414 f07918fd Mark McLoughlin
    if (fd == -1) {
2415 ab5b027e Markus Armbruster
        qerror_report(QERR_FD_NOT_SUPPLIED);
2416 6ad3ebd2 Luiz Capitulino
        return -1;
2417 f07918fd Mark McLoughlin
    }
2418 f07918fd Mark McLoughlin
2419 f07918fd Mark McLoughlin
    if (qemu_isdigit(fdname[0])) {
2420 e17ba87c Markus Armbruster
        qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2421 e17ba87c Markus Armbruster
                      "a name not starting with a digit");
2422 6ad3ebd2 Luiz Capitulino
        return -1;
2423 f07918fd Mark McLoughlin
    }
2424 f07918fd Mark McLoughlin
2425 72cf2d4f Blue Swirl
    QLIST_FOREACH(monfd, &mon->fds, next) {
2426 f07918fd Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
2427 f07918fd Mark McLoughlin
            continue;
2428 f07918fd Mark McLoughlin
        }
2429 f07918fd Mark McLoughlin
2430 f07918fd Mark McLoughlin
        close(monfd->fd);
2431 f07918fd Mark McLoughlin
        monfd->fd = fd;
2432 6ad3ebd2 Luiz Capitulino
        return 0;
2433 f07918fd Mark McLoughlin
    }
2434 f07918fd Mark McLoughlin
2435 c227f099 Anthony Liguori
    monfd = qemu_mallocz(sizeof(mon_fd_t));
2436 f07918fd Mark McLoughlin
    monfd->name = qemu_strdup(fdname);
2437 f07918fd Mark McLoughlin
    monfd->fd = fd;
2438 f07918fd Mark McLoughlin
2439 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&mon->fds, monfd, next);
2440 6ad3ebd2 Luiz Capitulino
    return 0;
2441 f07918fd Mark McLoughlin
}
2442 f07918fd Mark McLoughlin
2443 aeb91c1e Luiz Capitulino
static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2444 f07918fd Mark McLoughlin
{
2445 d54908a5 Luiz Capitulino
    const char *fdname = qdict_get_str(qdict, "fdname");
2446 c227f099 Anthony Liguori
    mon_fd_t *monfd;
2447 f07918fd Mark McLoughlin
2448 72cf2d4f Blue Swirl
    QLIST_FOREACH(monfd, &mon->fds, next) {
2449 f07918fd Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
2450 f07918fd Mark McLoughlin
            continue;
2451 f07918fd Mark McLoughlin
        }
2452 f07918fd Mark McLoughlin
2453 72cf2d4f Blue Swirl
        QLIST_REMOVE(monfd, next);
2454 f07918fd Mark McLoughlin
        close(monfd->fd);
2455 f07918fd Mark McLoughlin
        qemu_free(monfd->name);
2456 f07918fd Mark McLoughlin
        qemu_free(monfd);
2457 aeb91c1e Luiz Capitulino
        return 0;
2458 f07918fd Mark McLoughlin
    }
2459 f07918fd Mark McLoughlin
2460 ab5b027e Markus Armbruster
    qerror_report(QERR_FD_NOT_FOUND, fdname);
2461 aeb91c1e Luiz Capitulino
    return -1;
2462 f07918fd Mark McLoughlin
}
2463 f07918fd Mark McLoughlin
2464 d54908a5 Luiz Capitulino
static void do_loadvm(Monitor *mon, const QDict *qdict)
2465 c8d41b2c Juan Quintela
{
2466 c8d41b2c Juan Quintela
    int saved_vm_running  = vm_running;
2467 d54908a5 Luiz Capitulino
    const char *name = qdict_get_str(qdict, "name");
2468 c8d41b2c Juan Quintela
2469 c8d41b2c Juan Quintela
    vm_stop(0);
2470 c8d41b2c Juan Quintela
2471 03cd4655 Markus Armbruster
    if (load_vmstate(name) >= 0 && saved_vm_running)
2472 c8d41b2c Juan Quintela
        vm_start();
2473 c8d41b2c Juan Quintela
}
2474 c8d41b2c Juan Quintela
2475 7768e04c Mark McLoughlin
int monitor_get_fd(Monitor *mon, const char *fdname)
2476 7768e04c Mark McLoughlin
{
2477 c227f099 Anthony Liguori
    mon_fd_t *monfd;
2478 7768e04c Mark McLoughlin
2479 72cf2d4f Blue Swirl
    QLIST_FOREACH(monfd, &mon->fds, next) {
2480 7768e04c Mark McLoughlin
        int fd;
2481 7768e04c Mark McLoughlin
2482 7768e04c Mark McLoughlin
        if (strcmp(monfd->name, fdname) != 0) {
2483 7768e04c Mark McLoughlin
            continue;
2484 7768e04c Mark McLoughlin
        }
2485 7768e04c Mark McLoughlin
2486 7768e04c Mark McLoughlin
        fd = monfd->fd;
2487 7768e04c Mark McLoughlin
2488 7768e04c Mark McLoughlin
        /* caller takes ownership of fd */
2489 72cf2d4f Blue Swirl
        QLIST_REMOVE(monfd, next);
2490 7768e04c Mark McLoughlin
        qemu_free(monfd->name);
2491 7768e04c Mark McLoughlin
        qemu_free(monfd);
2492 7768e04c Mark McLoughlin
2493 7768e04c Mark McLoughlin
        return fd;
2494 7768e04c Mark McLoughlin
    }
2495 7768e04c Mark McLoughlin
2496 7768e04c Mark McLoughlin
    return -1;
2497 7768e04c Mark McLoughlin
}
2498 7768e04c Mark McLoughlin
2499 c227f099 Anthony Liguori
static const mon_cmd_t mon_cmds[] = {
2500 2313086a Blue Swirl
#include "qemu-monitor.h"
2501 5fafdf24 ths
    { NULL, NULL, },
2502 9dc39cba bellard
};
2503 9dc39cba bellard
2504 2313086a Blue Swirl
/* Please update qemu-monitor.hx when adding or changing commands */
2505 c227f099 Anthony Liguori
static const mon_cmd_t info_cmds[] = {
2506 d7f9b689 Luiz Capitulino
    {
2507 d7f9b689 Luiz Capitulino
        .name       = "version",
2508 d7f9b689 Luiz Capitulino
        .args_type  = "",
2509 d7f9b689 Luiz Capitulino
        .params     = "",
2510 d7f9b689 Luiz Capitulino
        .help       = "show the version of QEMU",
2511 45e914cf Luiz Capitulino
        .user_print = do_info_version_print,
2512 ab2d3187 Luiz Capitulino
        .mhandler.info_new = do_info_version,
2513 d7f9b689 Luiz Capitulino
    },
2514 d7f9b689 Luiz Capitulino
    {
2515 e3bba9d0 Luiz Capitulino
        .name       = "commands",
2516 e3bba9d0 Luiz Capitulino
        .args_type  = "",
2517 e3bba9d0 Luiz Capitulino
        .params     = "",
2518 e3bba9d0 Luiz Capitulino
        .help       = "list QMP available commands",
2519 e3bba9d0 Luiz Capitulino
        .user_print = monitor_user_noop,
2520 e3bba9d0 Luiz Capitulino
        .mhandler.info_new = do_info_commands,
2521 e3bba9d0 Luiz Capitulino
    },
2522 e3bba9d0 Luiz Capitulino
    {
2523 d7f9b689 Luiz Capitulino
        .name       = "network",
2524 d7f9b689 Luiz Capitulino
        .args_type  = "",
2525 d7f9b689 Luiz Capitulino
        .params     = "",
2526 d7f9b689 Luiz Capitulino
        .help       = "show the network state",
2527 910df89d Luiz Capitulino
        .mhandler.info = do_info_network,
2528 d7f9b689 Luiz Capitulino
    },
2529 d7f9b689 Luiz Capitulino
    {
2530 d7f9b689 Luiz Capitulino
        .name       = "chardev",
2531 d7f9b689 Luiz Capitulino
        .args_type  = "",
2532 d7f9b689 Luiz Capitulino
        .params     = "",
2533 d7f9b689 Luiz Capitulino
        .help       = "show the character devices",
2534 588b3832 Luiz Capitulino
        .user_print = qemu_chr_info_print,
2535 588b3832 Luiz Capitulino
        .mhandler.info_new = qemu_chr_info,
2536 d7f9b689 Luiz Capitulino
    },
2537 d7f9b689 Luiz Capitulino
    {
2538 d7f9b689 Luiz Capitulino
        .name       = "block",
2539 d7f9b689 Luiz Capitulino
        .args_type  = "",
2540 d7f9b689 Luiz Capitulino
        .params     = "",
2541 d7f9b689 Luiz Capitulino
        .help       = "show the block devices",
2542 d15e5465 Luiz Capitulino
        .user_print = bdrv_info_print,
2543 d15e5465 Luiz Capitulino
        .mhandler.info_new = bdrv_info,
2544 d7f9b689 Luiz Capitulino
    },
2545 d7f9b689 Luiz Capitulino
    {
2546 d7f9b689 Luiz Capitulino
        .name       = "blockstats",
2547 d7f9b689 Luiz Capitulino
        .args_type  = "",
2548 d7f9b689 Luiz Capitulino
        .params     = "",
2549 d7f9b689 Luiz Capitulino
        .help       = "show block device statistics",
2550 218a536a Luiz Capitulino
        .user_print = bdrv_stats_print,
2551 218a536a Luiz Capitulino
        .mhandler.info_new = bdrv_info_stats,
2552 d7f9b689 Luiz Capitulino
    },
2553 d7f9b689 Luiz Capitulino
    {
2554 d7f9b689 Luiz Capitulino
        .name       = "registers",
2555 d7f9b689 Luiz Capitulino
        .args_type  = "",
2556 d7f9b689 Luiz Capitulino
        .params     = "",
2557 d7f9b689 Luiz Capitulino
        .help       = "show the cpu registers",
2558 910df89d Luiz Capitulino
        .mhandler.info = do_info_registers,
2559 d7f9b689 Luiz Capitulino
    },
2560 d7f9b689 Luiz Capitulino
    {
2561 d7f9b689 Luiz Capitulino
        .name       = "cpus",
2562 d7f9b689 Luiz Capitulino
        .args_type  = "",
2563 d7f9b689 Luiz Capitulino
        .params     = "",
2564 d7f9b689 Luiz Capitulino
        .help       = "show infos for each CPU",
2565 8f3cec0b Luiz Capitulino
        .user_print = monitor_print_cpus,
2566 8f3cec0b Luiz Capitulino
        .mhandler.info_new = do_info_cpus,
2567 d7f9b689 Luiz Capitulino
    },
2568 d7f9b689 Luiz Capitulino
    {
2569 d7f9b689 Luiz Capitulino
        .name       = "history",
2570 d7f9b689 Luiz Capitulino
        .args_type  = "",
2571 d7f9b689 Luiz Capitulino
        .params     = "",
2572 d7f9b689 Luiz Capitulino
        .help       = "show the command line history",
2573 910df89d Luiz Capitulino
        .mhandler.info = do_info_history,
2574 d7f9b689 Luiz Capitulino
    },
2575 d7f9b689 Luiz Capitulino
    {
2576 d7f9b689 Luiz Capitulino
        .name       = "irq",
2577 d7f9b689 Luiz Capitulino
        .args_type  = "",
2578 d7f9b689 Luiz Capitulino
        .params     = "",
2579 d7f9b689 Luiz Capitulino
        .help       = "show the interrupts statistics (if available)",
2580 910df89d Luiz Capitulino
        .mhandler.info = irq_info,
2581 d7f9b689 Luiz Capitulino
    },
2582 d7f9b689 Luiz Capitulino
    {
2583 d7f9b689 Luiz Capitulino
        .name       = "pic",
2584 d7f9b689 Luiz Capitulino
        .args_type  = "",
2585 d7f9b689 Luiz Capitulino
        .params     = "",
2586 d7f9b689 Luiz Capitulino
        .help       = "show i8259 (PIC) state",
2587 910df89d Luiz Capitulino
        .mhandler.info = pic_info,
2588 d7f9b689 Luiz Capitulino
    },
2589 d7f9b689 Luiz Capitulino
    {
2590 d7f9b689 Luiz Capitulino
        .name       = "pci",
2591 d7f9b689 Luiz Capitulino
        .args_type  = "",
2592 d7f9b689 Luiz Capitulino
        .params     = "",
2593 d7f9b689 Luiz Capitulino
        .help       = "show PCI info",
2594 163c8a59 Luiz Capitulino
        .user_print = do_pci_info_print,
2595 163c8a59 Luiz Capitulino
        .mhandler.info_new = do_pci_info,
2596 d7f9b689 Luiz Capitulino
    },
2597 7c664e2f aurel32
#if defined(TARGET_I386) || defined(TARGET_SH4)
2598 d7f9b689 Luiz Capitulino
    {
2599 d7f9b689 Luiz Capitulino
        .name       = "tlb",
2600 d7f9b689 Luiz Capitulino
        .args_type  = "",
2601 d7f9b689 Luiz Capitulino
        .params     = "",
2602 d7f9b689 Luiz Capitulino
        .help       = "show virtual to physical memory mappings",
2603 910df89d Luiz Capitulino
        .mhandler.info = tlb_info,
2604 d7f9b689 Luiz Capitulino
    },
2605 7c664e2f aurel32
#endif
2606 7c664e2f aurel32
#if defined(TARGET_I386)
2607 d7f9b689 Luiz Capitulino
    {
2608 d7f9b689 Luiz Capitulino
        .name       = "mem",
2609 d7f9b689 Luiz Capitulino
        .args_type  = "",
2610 d7f9b689 Luiz Capitulino
        .params     = "",
2611 d7f9b689 Luiz Capitulino
        .help       = "show the active virtual memory mappings",
2612 910df89d Luiz Capitulino
        .mhandler.info = mem_info,
2613 d7f9b689 Luiz Capitulino
    },
2614 d7f9b689 Luiz Capitulino
    {
2615 d7f9b689 Luiz Capitulino
        .name       = "hpet",
2616 d7f9b689 Luiz Capitulino
        .args_type  = "",
2617 d7f9b689 Luiz Capitulino
        .params     = "",
2618 d7f9b689 Luiz Capitulino
        .help       = "show state of HPET",
2619 14f0720d Luiz Capitulino
        .user_print = do_info_hpet_print,
2620 14f0720d Luiz Capitulino
        .mhandler.info_new = do_info_hpet,
2621 d7f9b689 Luiz Capitulino
    },
2622 b86bda5b bellard
#endif
2623 d7f9b689 Luiz Capitulino
    {
2624 d7f9b689 Luiz Capitulino
        .name       = "jit",
2625 d7f9b689 Luiz Capitulino
        .args_type  = "",
2626 d7f9b689 Luiz Capitulino
        .params     = "",
2627 d7f9b689 Luiz Capitulino
        .help       = "show dynamic compiler info",
2628 910df89d Luiz Capitulino
        .mhandler.info = do_info_jit,
2629 d7f9b689 Luiz Capitulino
    },
2630 d7f9b689 Luiz Capitulino
    {
2631 d7f9b689 Luiz Capitulino
        .name       = "kvm",
2632 d7f9b689 Luiz Capitulino
        .args_type  = "",
2633 d7f9b689 Luiz Capitulino
        .params     = "",
2634 d7f9b689 Luiz Capitulino
        .help       = "show KVM information",
2635 2af5ba71 Luiz Capitulino
        .user_print = do_info_kvm_print,
2636 2af5ba71 Luiz Capitulino
        .mhandler.info_new = do_info_kvm,
2637 d7f9b689 Luiz Capitulino
    },
2638 d7f9b689 Luiz Capitulino
    {
2639 d7f9b689 Luiz Capitulino
        .name       = "numa",
2640 d7f9b689 Luiz Capitulino
        .args_type  = "",
2641 d7f9b689 Luiz Capitulino
        .params     = "",
2642 d7f9b689 Luiz Capitulino
        .help       = "show NUMA information",
2643 910df89d Luiz Capitulino
        .mhandler.info = do_info_numa,
2644 d7f9b689 Luiz Capitulino
    },
2645 d7f9b689 Luiz Capitulino
    {
2646 d7f9b689 Luiz Capitulino
        .name       = "usb",
2647 d7f9b689 Luiz Capitulino
        .args_type  = "",
2648 d7f9b689 Luiz Capitulino
        .params     = "",
2649 d7f9b689 Luiz Capitulino
        .help       = "show guest USB devices",
2650 910df89d Luiz Capitulino
        .mhandler.info = usb_info,
2651 d7f9b689 Luiz Capitulino
    },
2652 d7f9b689 Luiz Capitulino
    {
2653 d7f9b689 Luiz Capitulino
        .name       = "usbhost",
2654 d7f9b689 Luiz Capitulino
        .args_type  = "",
2655 d7f9b689 Luiz Capitulino
        .params     = "",
2656 d7f9b689 Luiz Capitulino
        .help       = "show host USB devices",
2657 910df89d Luiz Capitulino
        .mhandler.info = usb_host_info,
2658 d7f9b689 Luiz Capitulino
    },
2659 d7f9b689 Luiz Capitulino
    {
2660 d7f9b689 Luiz Capitulino
        .name       = "profile",
2661 d7f9b689 Luiz Capitulino
        .args_type  = "",
2662 d7f9b689 Luiz Capitulino
        .params     = "",
2663 d7f9b689 Luiz Capitulino
        .help       = "show profiling information",
2664 910df89d Luiz Capitulino
        .mhandler.info = do_info_profile,
2665 d7f9b689 Luiz Capitulino
    },
2666 d7f9b689 Luiz Capitulino
    {
2667 d7f9b689 Luiz Capitulino
        .name       = "capture",
2668 d7f9b689 Luiz Capitulino
        .args_type  = "",
2669 d7f9b689 Luiz Capitulino
        .params     = "",
2670 d7f9b689 Luiz Capitulino
        .help       = "show capture information",
2671 910df89d Luiz Capitulino
        .mhandler.info = do_info_capture,
2672 d7f9b689 Luiz Capitulino
    },
2673 d7f9b689 Luiz Capitulino
    {
2674 d7f9b689 Luiz Capitulino
        .name       = "snapshots",
2675 d7f9b689 Luiz Capitulino
        .args_type  = "",
2676 d7f9b689 Luiz Capitulino
        .params     = "",
2677 d7f9b689 Luiz Capitulino
        .help       = "show the currently saved VM snapshots",
2678 910df89d Luiz Capitulino
        .mhandler.info = do_info_snapshots,
2679 d7f9b689 Luiz Capitulino
    },
2680 d7f9b689 Luiz Capitulino
    {
2681 d7f9b689 Luiz Capitulino
        .name       = "status",
2682 d7f9b689 Luiz Capitulino
        .args_type  = "",
2683 d7f9b689 Luiz Capitulino
        .params     = "",
2684 d7f9b689 Luiz Capitulino
        .help       = "show the current VM status (running|paused)",
2685 c0e8520e Luiz Capitulino
        .user_print = do_info_status_print,
2686 c0e8520e Luiz Capitulino
        .mhandler.info_new = do_info_status,
2687 d7f9b689 Luiz Capitulino
    },
2688 d7f9b689 Luiz Capitulino
    {
2689 d7f9b689 Luiz Capitulino
        .name       = "pcmcia",
2690 d7f9b689 Luiz Capitulino
        .args_type  = "",
2691 d7f9b689 Luiz Capitulino
        .params     = "",
2692 d7f9b689 Luiz Capitulino
        .help       = "show guest PCMCIA status",
2693 910df89d Luiz Capitulino
        .mhandler.info = pcmcia_info,
2694 d7f9b689 Luiz Capitulino
    },
2695 d7f9b689 Luiz Capitulino
    {
2696 d7f9b689 Luiz Capitulino
        .name       = "mice",
2697 d7f9b689 Luiz Capitulino
        .args_type  = "",
2698 d7f9b689 Luiz Capitulino
        .params     = "",
2699 d7f9b689 Luiz Capitulino
        .help       = "show which guest mouse is receiving events",
2700 e78c48ec Luiz Capitulino
        .user_print = do_info_mice_print,
2701 e78c48ec Luiz Capitulino
        .mhandler.info_new = do_info_mice,
2702 d7f9b689 Luiz Capitulino
    },
2703 d7f9b689 Luiz Capitulino
    {
2704 d7f9b689 Luiz Capitulino
        .name       = "vnc",
2705 d7f9b689 Luiz Capitulino
        .args_type  = "",
2706 d7f9b689 Luiz Capitulino
        .params     = "",
2707 d7f9b689 Luiz Capitulino
        .help       = "show the vnc server status",
2708 d96fd29c Luiz Capitulino
        .user_print = do_info_vnc_print,
2709 d96fd29c Luiz Capitulino
        .mhandler.info_new = do_info_vnc,
2710 d7f9b689 Luiz Capitulino
    },
2711 d7f9b689 Luiz Capitulino
    {
2712 d7f9b689 Luiz Capitulino
        .name       = "name",
2713 d7f9b689 Luiz Capitulino
        .args_type  = "",
2714 d7f9b689 Luiz Capitulino
        .params     = "",
2715 d7f9b689 Luiz Capitulino
        .help       = "show the current VM name",
2716 e05486cb Luiz Capitulino
        .user_print = do_info_name_print,
2717 e05486cb Luiz Capitulino
        .mhandler.info_new = do_info_name,
2718 d7f9b689 Luiz Capitulino
    },
2719 d7f9b689 Luiz Capitulino
    {
2720 d7f9b689 Luiz Capitulino
        .name       = "uuid",
2721 d7f9b689 Luiz Capitulino
        .args_type  = "",
2722 d7f9b689 Luiz Capitulino
        .params     = "",
2723 d7f9b689 Luiz Capitulino
        .help       = "show the current VM UUID",
2724 9603ceba Luiz Capitulino
        .user_print = do_info_uuid_print,
2725 9603ceba Luiz Capitulino
        .mhandler.info_new = do_info_uuid,
2726 d7f9b689 Luiz Capitulino
    },
2727 76a66253 j_mayer
#if defined(TARGET_PPC)
2728 d7f9b689 Luiz Capitulino
    {
2729 d7f9b689 Luiz Capitulino
        .name       = "cpustats",
2730 d7f9b689 Luiz Capitulino
        .args_type  = "",
2731 d7f9b689 Luiz Capitulino
        .params     = "",
2732 d7f9b689 Luiz Capitulino
        .help       = "show CPU statistics",
2733 910df89d Luiz Capitulino
        .mhandler.info = do_info_cpu_stats,
2734 d7f9b689 Luiz Capitulino
    },
2735 76a66253 j_mayer
#endif
2736 31a60e22 blueswir1
#if defined(CONFIG_SLIRP)
2737 d7f9b689 Luiz Capitulino
    {
2738 d7f9b689 Luiz Capitulino
        .name       = "usernet",
2739 d7f9b689 Luiz Capitulino
        .args_type  = "",
2740 d7f9b689 Luiz Capitulino
        .params     = "",
2741 d7f9b689 Luiz Capitulino
        .help       = "show user network stack connection states",
2742 910df89d Luiz Capitulino
        .mhandler.info = do_info_usernet,
2743 d7f9b689 Luiz Capitulino
    },
2744 31a60e22 blueswir1
#endif
2745 d7f9b689 Luiz Capitulino
    {
2746 d7f9b689 Luiz Capitulino
        .name       = "migrate",
2747 d7f9b689 Luiz Capitulino
        .args_type  = "",
2748 d7f9b689 Luiz Capitulino
        .params     = "",
2749 d7f9b689 Luiz Capitulino
        .help       = "show migration status",
2750 c86a6683 Luiz Capitulino
        .user_print = do_info_migrate_print,
2751 c86a6683 Luiz Capitulino
        .mhandler.info_new = do_info_migrate,
2752 d7f9b689 Luiz Capitulino
    },
2753 d7f9b689 Luiz Capitulino
    {
2754 d7f9b689 Luiz Capitulino
        .name       = "balloon",
2755 d7f9b689 Luiz Capitulino
        .args_type  = "",
2756 d7f9b689 Luiz Capitulino
        .params     = "",
2757 d7f9b689 Luiz Capitulino
        .help       = "show balloon information",
2758 cc1d9c70 Luiz Capitulino
        .user_print = monitor_print_balloon,
2759 625a5bef Adam Litke
        .mhandler.info_async = do_info_balloon,
2760 625a5bef Adam Litke
        .async      = 1,
2761 d7f9b689 Luiz Capitulino
    },
2762 d7f9b689 Luiz Capitulino
    {
2763 d7f9b689 Luiz Capitulino
        .name       = "qtree",
2764 d7f9b689 Luiz Capitulino
        .args_type  = "",
2765 d7f9b689 Luiz Capitulino
        .params     = "",
2766 d7f9b689 Luiz Capitulino
        .help       = "show device tree",
2767 910df89d Luiz Capitulino
        .mhandler.info = do_info_qtree,
2768 d7f9b689 Luiz Capitulino
    },
2769 d7f9b689 Luiz Capitulino
    {
2770 d7f9b689 Luiz Capitulino
        .name       = "qdm",
2771 d7f9b689 Luiz Capitulino
        .args_type  = "",
2772 d7f9b689 Luiz Capitulino
        .params     = "",
2773 d7f9b689 Luiz Capitulino
        .help       = "show qdev device model list",
2774 910df89d Luiz Capitulino
        .mhandler.info = do_info_qdm,
2775 d7f9b689 Luiz Capitulino
    },
2776 d7f9b689 Luiz Capitulino
    {
2777 d7f9b689 Luiz Capitulino
        .name       = "roms",
2778 d7f9b689 Luiz Capitulino
        .args_type  = "",
2779 d7f9b689 Luiz Capitulino
        .params     = "",
2780 d7f9b689 Luiz Capitulino
        .help       = "show roms",
2781 910df89d Luiz Capitulino
        .mhandler.info = do_info_roms,
2782 d7f9b689 Luiz Capitulino
    },
2783 d7f9b689 Luiz Capitulino
    {
2784 d7f9b689 Luiz Capitulino
        .name       = NULL,
2785 d7f9b689 Luiz Capitulino
    },
2786 9dc39cba bellard
};
2787 9dc39cba bellard
2788 9307c4c1 bellard
/*******************************************************************/
2789 9307c4c1 bellard
2790 9307c4c1 bellard
static const char *pch;
2791 9307c4c1 bellard
static jmp_buf expr_env;
2792 9307c4c1 bellard
2793 92a31b1f bellard
#define MD_TLONG 0
2794 92a31b1f bellard
#define MD_I32   1
2795 92a31b1f bellard
2796 9307c4c1 bellard
typedef struct MonitorDef {
2797 9307c4c1 bellard
    const char *name;
2798 9307c4c1 bellard
    int offset;
2799 8662d656 blueswir1
    target_long (*get_value)(const struct MonitorDef *md, int val);
2800 92a31b1f bellard
    int type;
2801 9307c4c1 bellard
} MonitorDef;
2802 9307c4c1 bellard
2803 57206fd4 bellard
#if defined(TARGET_I386)
2804 8662d656 blueswir1
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2805 57206fd4 bellard
{
2806 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2807 6a00d601 bellard
    return env->eip + env->segs[R_CS].base;
2808 57206fd4 bellard
}
2809 57206fd4 bellard
#endif
2810 57206fd4 bellard
2811 a541f297 bellard
#if defined(TARGET_PPC)
2812 8662d656 blueswir1
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2813 a541f297 bellard
{
2814 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2815 a541f297 bellard
    unsigned int u;
2816 a541f297 bellard
    int i;
2817 a541f297 bellard
2818 a541f297 bellard
    u = 0;
2819 a541f297 bellard
    for (i = 0; i < 8; i++)
2820 28a76be8 aliguori
        u |= env->crf[i] << (32 - (4 * i));
2821 a541f297 bellard
2822 a541f297 bellard
    return u;
2823 a541f297 bellard
}
2824 a541f297 bellard
2825 8662d656 blueswir1
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2826 a541f297 bellard
{
2827 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2828 0411a972 j_mayer
    return env->msr;
2829 a541f297 bellard
}
2830 a541f297 bellard
2831 8662d656 blueswir1
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2832 a541f297 bellard
{
2833 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2834 3d7b417e aurel32
    return env->xer;
2835 a541f297 bellard
}
2836 9fddaa0c bellard
2837 8662d656 blueswir1
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2838 9fddaa0c bellard
{
2839 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2840 6a00d601 bellard
    return cpu_ppc_load_decr(env);
2841 9fddaa0c bellard
}
2842 9fddaa0c bellard
2843 8662d656 blueswir1
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2844 9fddaa0c bellard
{
2845 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2846 6a00d601 bellard
    return cpu_ppc_load_tbu(env);
2847 9fddaa0c bellard
}
2848 9fddaa0c bellard
2849 8662d656 blueswir1
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2850 9fddaa0c bellard
{
2851 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2852 6a00d601 bellard
    return cpu_ppc_load_tbl(env);
2853 9fddaa0c bellard
}
2854 a541f297 bellard
#endif
2855 a541f297 bellard
2856 e95c8d51 bellard
#if defined(TARGET_SPARC)
2857 7b936c0c bellard
#ifndef TARGET_SPARC64
2858 8662d656 blueswir1
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2859 e95c8d51 bellard
{
2860 6a00d601 bellard
    CPUState *env = mon_get_cpu();
2861 5a834bb4 Blue Swirl
2862 5a834bb4 Blue Swirl
    return cpu_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 88f7db84 Luiz Capitulino
        qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute", "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 04f8c053 Luiz Capitulino
    } else if (qobject_type(obj) != QTYPE_QDICT) {
4441 04f8c053 Luiz Capitulino
        qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments", "object");
4442 04f8c053 Luiz Capitulino
        goto err_input;
4443 5fa737a4 Luiz Capitulino
    } else {
4444 5fa737a4 Luiz Capitulino
        args = qobject_to_qdict(obj);
4445 5fa737a4 Luiz Capitulino
        QINCREF(args);
4446 5fa737a4 Luiz Capitulino
    }
4447 5fa737a4 Luiz Capitulino
4448 5fa737a4 Luiz Capitulino
    QDECREF(input);
4449 5fa737a4 Luiz Capitulino
4450 5fa737a4 Luiz Capitulino
    err = monitor_check_qmp_args(cmd, args);
4451 5fa737a4 Luiz Capitulino
    if (err < 0) {
4452 5fa737a4 Luiz Capitulino
        goto err_out;
4453 5fa737a4 Luiz Capitulino
    }
4454 5fa737a4 Luiz Capitulino
4455 940cc30d Adam Litke
    if (monitor_handler_is_async(cmd)) {
4456 940cc30d Adam Litke
        qmp_async_cmd_handler(mon, cmd, args);
4457 940cc30d Adam Litke
    } else {
4458 940cc30d Adam Litke
        monitor_call_handler(mon, cmd, args);
4459 940cc30d Adam Litke
    }
4460 5fa737a4 Luiz Capitulino
    goto out;
4461 5fa737a4 Luiz Capitulino
4462 5fa737a4 Luiz Capitulino
err_input:
4463 5fa737a4 Luiz Capitulino
    QDECREF(input);
4464 5fa737a4 Luiz Capitulino
err_out:
4465 5fa737a4 Luiz Capitulino
    monitor_protocol_emitter(mon, NULL);
4466 5fa737a4 Luiz Capitulino
out:
4467 5fa737a4 Luiz Capitulino
    QDECREF(args);
4468 5fa737a4 Luiz Capitulino
}
4469 5fa737a4 Luiz Capitulino
4470 9b57c02e Luiz Capitulino
/**
4471 9b57c02e Luiz Capitulino
 * monitor_control_read(): Read and handle QMP input
4472 9b57c02e Luiz Capitulino
 */
4473 9b57c02e Luiz Capitulino
static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4474 9b57c02e Luiz Capitulino
{
4475 9b57c02e Luiz Capitulino
    Monitor *old_mon = cur_mon;
4476 9b57c02e Luiz Capitulino
4477 9b57c02e Luiz Capitulino
    cur_mon = opaque;
4478 9b57c02e Luiz Capitulino
4479 5fa737a4 Luiz Capitulino
    json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
4480 9b57c02e Luiz Capitulino
4481 9b57c02e Luiz Capitulino
    cur_mon = old_mon;
4482 9b57c02e Luiz Capitulino
}
4483 9b57c02e Luiz Capitulino
4484 731b0364 aliguori
static void monitor_read(void *opaque, const uint8_t *buf, int size)
4485 9dc39cba bellard
{
4486 731b0364 aliguori
    Monitor *old_mon = cur_mon;
4487 7e2515e8 bellard
    int i;
4488 376253ec aliguori
4489 731b0364 aliguori
    cur_mon = opaque;
4490 731b0364 aliguori
4491 cde76ee1 aliguori
    if (cur_mon->rs) {
4492 cde76ee1 aliguori
        for (i = 0; i < size; i++)
4493 cde76ee1 aliguori
            readline_handle_byte(cur_mon->rs, buf[i]);
4494 cde76ee1 aliguori
    } else {
4495 cde76ee1 aliguori
        if (size == 0 || buf[size - 1] != 0)
4496 cde76ee1 aliguori
            monitor_printf(cur_mon, "corrupted command\n");
4497 cde76ee1 aliguori
        else
4498 f3c157c4 Luiz Capitulino
            handle_user_command(cur_mon, (char *)buf);
4499 cde76ee1 aliguori
    }
4500 9dc39cba bellard
4501 731b0364 aliguori
    cur_mon = old_mon;
4502 731b0364 aliguori
}
4503 d8f44609 aliguori
4504 376253ec aliguori
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
4505 aa455485 bellard
{
4506 731b0364 aliguori
    monitor_suspend(mon);
4507 f3c157c4 Luiz Capitulino
    handle_user_command(mon, cmdline);
4508 731b0364 aliguori
    monitor_resume(mon);
4509 d8f44609 aliguori
}
4510 d8f44609 aliguori
4511 cde76ee1 aliguori
int monitor_suspend(Monitor *mon)
4512 d8f44609 aliguori
{
4513 cde76ee1 aliguori
    if (!mon->rs)
4514 cde76ee1 aliguori
        return -ENOTTY;
4515 731b0364 aliguori
    mon->suspend_cnt++;
4516 cde76ee1 aliguori
    return 0;
4517 d8f44609 aliguori
}
4518 d8f44609 aliguori
4519 376253ec aliguori
void monitor_resume(Monitor *mon)
4520 d8f44609 aliguori
{
4521 cde76ee1 aliguori
    if (!mon->rs)
4522 cde76ee1 aliguori
        return;
4523 731b0364 aliguori
    if (--mon->suspend_cnt == 0)
4524 731b0364 aliguori
        readline_show_prompt(mon->rs);
4525 aa455485 bellard
}
4526 aa455485 bellard
4527 ca9567e2 Luiz Capitulino
static QObject *get_qmp_greeting(void)
4528 ca9567e2 Luiz Capitulino
{
4529 ca9567e2 Luiz Capitulino
    QObject *ver;
4530 ca9567e2 Luiz Capitulino
4531 ca9567e2 Luiz Capitulino
    do_info_version(NULL, &ver);
4532 ca9567e2 Luiz Capitulino
    return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4533 ca9567e2 Luiz Capitulino
}
4534 ca9567e2 Luiz Capitulino
4535 9b57c02e Luiz Capitulino
/**
4536 9b57c02e Luiz Capitulino
 * monitor_control_event(): Print QMP gretting
4537 9b57c02e Luiz Capitulino
 */
4538 9b57c02e Luiz Capitulino
static void monitor_control_event(void *opaque, int event)
4539 9b57c02e Luiz Capitulino
{
4540 47116d1c Luiz Capitulino
    QObject *data;
4541 47116d1c Luiz Capitulino
    Monitor *mon = opaque;
4542 9b57c02e Luiz Capitulino
4543 47116d1c Luiz Capitulino
    switch (event) {
4544 47116d1c Luiz Capitulino
    case CHR_EVENT_OPENED:
4545 4a7e1190 Luiz Capitulino
        mon->mc->command_mode = 0;
4546 5fa737a4 Luiz Capitulino
        json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4547 ca9567e2 Luiz Capitulino
        data = get_qmp_greeting();
4548 9b57c02e Luiz Capitulino
        monitor_json_emitter(mon, data);
4549 9b57c02e Luiz Capitulino
        qobject_decref(data);
4550 47116d1c Luiz Capitulino
        break;
4551 47116d1c Luiz Capitulino
    case CHR_EVENT_CLOSED:
4552 47116d1c Luiz Capitulino
        json_message_parser_destroy(&mon->mc->parser);
4553 47116d1c Luiz Capitulino
        break;
4554 9b57c02e Luiz Capitulino
    }
4555 9b57c02e Luiz Capitulino
}
4556 9b57c02e Luiz Capitulino
4557 731b0364 aliguori
static void monitor_event(void *opaque, int event)
4558 86e94dea ths
{
4559 376253ec aliguori
    Monitor *mon = opaque;
4560 376253ec aliguori
4561 2724b180 aliguori
    switch (event) {
4562 2724b180 aliguori
    case CHR_EVENT_MUX_IN:
4563 a7aec5da Gerd Hoffmann
        mon->mux_out = 0;
4564 a7aec5da Gerd Hoffmann
        if (mon->reset_seen) {
4565 a7aec5da Gerd Hoffmann
            readline_restart(mon->rs);
4566 a7aec5da Gerd Hoffmann
            monitor_resume(mon);
4567 a7aec5da Gerd Hoffmann
            monitor_flush(mon);
4568 a7aec5da Gerd Hoffmann
        } else {
4569 a7aec5da Gerd Hoffmann
            mon->suspend_cnt = 0;
4570 a7aec5da Gerd Hoffmann
        }
4571 2724b180 aliguori
        break;
4572 2724b180 aliguori
4573 2724b180 aliguori
    case CHR_EVENT_MUX_OUT:
4574 a7aec5da Gerd Hoffmann
        if (mon->reset_seen) {
4575 a7aec5da Gerd Hoffmann
            if (mon->suspend_cnt == 0) {
4576 a7aec5da Gerd Hoffmann
                monitor_printf(mon, "\n");
4577 a7aec5da Gerd Hoffmann
            }
4578 a7aec5da Gerd Hoffmann
            monitor_flush(mon);
4579 a7aec5da Gerd Hoffmann
            monitor_suspend(mon);
4580 a7aec5da Gerd Hoffmann
        } else {
4581 a7aec5da Gerd Hoffmann
            mon->suspend_cnt++;
4582 a7aec5da Gerd Hoffmann
        }
4583 a7aec5da Gerd Hoffmann
        mon->mux_out = 1;
4584 2724b180 aliguori
        break;
4585 86e94dea ths
4586 b6b8df56 Amit Shah
    case CHR_EVENT_OPENED:
4587 2724b180 aliguori
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4588 2724b180 aliguori
                       "information\n", QEMU_VERSION);
4589 a7aec5da Gerd Hoffmann
        if (!mon->mux_out) {
4590 2724b180 aliguori
            readline_show_prompt(mon->rs);
4591 a7aec5da Gerd Hoffmann
        }
4592 a7aec5da Gerd Hoffmann
        mon->reset_seen = 1;
4593 2724b180 aliguori
        break;
4594 2724b180 aliguori
    }
4595 86e94dea ths
}
4596 86e94dea ths
4597 76655d6d aliguori
4598 76655d6d aliguori
/*
4599 76655d6d aliguori
 * Local variables:
4600 76655d6d aliguori
 *  c-indent-level: 4
4601 76655d6d aliguori
 *  c-basic-offset: 4
4602 76655d6d aliguori
 *  tab-width: 8
4603 76655d6d aliguori
 * End:
4604 76655d6d aliguori
 */
4605 76655d6d aliguori
4606 731b0364 aliguori
void monitor_init(CharDriverState *chr, int flags)
4607 aa455485 bellard
{
4608 731b0364 aliguori
    static int is_first_init = 1;
4609 87127161 aliguori
    Monitor *mon;
4610 20d8a3ed ths
4611 20d8a3ed ths
    if (is_first_init) {
4612 c8256f9d balrog
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
4613 20d8a3ed ths
        is_first_init = 0;
4614 20d8a3ed ths
    }
4615 87127161 aliguori
4616 87127161 aliguori
    mon = qemu_mallocz(sizeof(*mon));
4617 20d8a3ed ths
4618 87127161 aliguori
    mon->chr = chr;
4619 731b0364 aliguori
    mon->flags = flags;
4620 cde76ee1 aliguori
    if (flags & MONITOR_USE_READLINE) {
4621 cde76ee1 aliguori
        mon->rs = readline_init(mon, monitor_find_completion);
4622 cde76ee1 aliguori
        monitor_read_command(mon, 0);
4623 cde76ee1 aliguori
    }
4624 87127161 aliguori
4625 9b57c02e Luiz Capitulino
    if (monitor_ctrl_mode(mon)) {
4626 5fa737a4 Luiz Capitulino
        mon->mc = qemu_mallocz(sizeof(MonitorControl));
4627 9b57c02e Luiz Capitulino
        /* Control mode requires special handlers */
4628 9b57c02e Luiz Capitulino
        qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4629 9b57c02e Luiz Capitulino
                              monitor_control_event, mon);
4630 9b57c02e Luiz Capitulino
    } else {
4631 9b57c02e Luiz Capitulino
        qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4632 9b57c02e Luiz Capitulino
                              monitor_event, mon);
4633 9b57c02e Luiz Capitulino
    }
4634 87127161 aliguori
4635 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&mon_list, mon, entry);
4636 8631b608 Markus Armbruster
    if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4637 8631b608 Markus Armbruster
        default_mon = mon;
4638 aa455485 bellard
}
4639 aa455485 bellard
4640 376253ec aliguori
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
4641 81d0912d bellard
{
4642 bb5fc20f aliguori
    BlockDriverState *bs = opaque;
4643 bb5fc20f aliguori
    int ret = 0;
4644 81d0912d bellard
4645 bb5fc20f aliguori
    if (bdrv_set_key(bs, password) != 0) {
4646 376253ec aliguori
        monitor_printf(mon, "invalid password\n");
4647 bb5fc20f aliguori
        ret = -EPERM;
4648 9dc39cba bellard
    }
4649 731b0364 aliguori
    if (mon->password_completion_cb)
4650 731b0364 aliguori
        mon->password_completion_cb(mon->password_opaque, ret);
4651 bb5fc20f aliguori
4652 731b0364 aliguori
    monitor_read_command(mon, 1);
4653 9dc39cba bellard
}
4654 c0f4ce77 aliguori
4655 0bbc47bb Luiz Capitulino
int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4656 0bbc47bb Luiz Capitulino
                                BlockDriverCompletionFunc *completion_cb,
4657 0bbc47bb Luiz Capitulino
                                void *opaque)
4658 c0f4ce77 aliguori
{
4659 cde76ee1 aliguori
    int err;
4660 cde76ee1 aliguori
4661 bb5fc20f aliguori
    if (!bdrv_key_required(bs)) {
4662 bb5fc20f aliguori
        if (completion_cb)
4663 bb5fc20f aliguori
            completion_cb(opaque, 0);
4664 0bbc47bb Luiz Capitulino
        return 0;
4665 bb5fc20f aliguori
    }
4666 c0f4ce77 aliguori
4667 94171e11 Luiz Capitulino
    if (monitor_ctrl_mode(mon)) {
4668 ab5b027e Markus Armbruster
        qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4669 0bbc47bb Luiz Capitulino
        return -1;
4670 94171e11 Luiz Capitulino
    }
4671 94171e11 Luiz Capitulino
4672 376253ec aliguori
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4673 376253ec aliguori
                   bdrv_get_encrypted_filename(bs));
4674 bb5fc20f aliguori
4675 731b0364 aliguori
    mon->password_completion_cb = completion_cb;
4676 731b0364 aliguori
    mon->password_opaque = opaque;
4677 bb5fc20f aliguori
4678 cde76ee1 aliguori
    err = monitor_read_password(mon, bdrv_password_cb, bs);
4679 cde76ee1 aliguori
4680 cde76ee1 aliguori
    if (err && completion_cb)
4681 cde76ee1 aliguori
        completion_cb(opaque, err);
4682 0bbc47bb Luiz Capitulino
4683 0bbc47bb Luiz Capitulino
    return err;
4684 c0f4ce77 aliguori
}