Statistics
| Branch: | Revision:

root / monitor.c @ 04088adb

History | View | Annotate | Download (124.8 kB)

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