Statistics
| Branch: | Revision:

root / monitor.c @ 0389ced4

History | View | Annotate | Download (125.6 kB)

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