Statistics
| Branch: | Revision:

root / monitor.c @ 82b11662

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