Statistics
| Branch: | Revision:

root / monitor.c @ f36b4afb

History | View | Annotate | Download (122.3 kB)

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