Statistics
| Branch: | Revision:

root / monitor.c @ acd0a093

History | View | Annotate | Download (125 kB)

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