Statistics
| Branch: | Revision:

root / monitor.c @ 2c17449b

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