Statistics
| Branch: | Revision:

root / monitor.c @ e3193601

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