Statistics
| Branch: | Revision:

root / monitor.c @ d91a68a7

History | View | Annotate | Download (146.4 kB)

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