Statistics
| Branch: | Revision:

root / monitor.c @ 6f06f178

History | View | Annotate | Download (130.6 kB)

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