Statistics
| Branch: | Revision:

root / monitor.c @ 10e4f606

History | View | Annotate | Download (124.7 kB)

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