Statistics
| Branch: | Revision:

root / monitor.c @ 3e12a751

History | View | Annotate | Download (125.9 kB)

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