Statistics
| Branch: | Revision:

root / monitor.c @ fb5590f7

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