Statistics
| Branch: | Revision:

root / monitor.c @ e3e87df4

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