Statistics
| Branch: | Revision:

root / monitor.c @ a88790a1

History | View | Annotate | Download (118.4 kB)

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