Statistics
| Branch: | Revision:

root / monitor.c @ 6ab7e546

History | View | Annotate | Download (137.6 kB)

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