Statistics
| Branch: | Revision:

root / monitor.c @ 361127df

History | View | Annotate | Download (124.6 kB)

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