Statistics
| Branch: | Revision:

root / monitor.c @ 030db6e8

History | View | Annotate | Download (123.6 kB)

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