Statistics
| Branch: | Revision:

root / monitor.c @ 46c7fc18

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