Statistics
| Branch: | Revision:

root / monitor.c @ 0534163f

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