Statistics
| Branch: | Revision:

root / monitor.c @ 6d8a764e

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