Statistics
| Branch: | Revision:

root / monitor.c @ a8170e5e

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