Statistics
| Branch: | Revision:

root / monitor.c @ 0056c093

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