Statistics
| Branch: | Revision:

root / monitor.c @ df2dbb4a

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