blob: f319fee79190a15cfe31fc6c3b06b60863692aad [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
blueswir1511d2b12009-03-07 15:32:56 +000024#include <dirent.h>
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
Gerd Hoffmanncae49562009-06-05 15:53:17 +010026#include "hw/qdev.h"
pbrook87ecb682007-11-17 17:14:51 +000027#include "hw/usb.h"
28#include "hw/pcmcia.h"
29#include "hw/pc.h"
30#include "hw/pci.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010031#include "hw/watchdog.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020032#include "hw/loader.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "gdbstub.h"
34#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000035#include "net/slirp.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "qemu-char.h"
37#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000038#include "monitor.h"
39#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020041#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000042#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000043#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000045#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000046#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000047#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000048#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030049#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010050#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030051#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020052#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030053#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020054#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020055#include "json-streamer.h"
56#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000057#include "osdep.h"
ths6a5bd302007-12-03 17:05:38 +000058
bellard9dc39cb2004-03-14 21:38:27 +000059//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000060//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000061
bellard9307c4c2004-04-04 12:57:25 +000062/*
63 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000064 *
bellard9307c4c2004-04-04 12:57:25 +000065 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000066 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000067 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010068 * 'O' option string of the form NAME=VALUE,...
69 * parsed according to QemuOptsList given by its name
70 * Example: 'device:O' uses qemu_device_opts.
71 * Restriction: only lists with empty desc are supported
72 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000073 * 'i' 32 bit integer
74 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010075 * 'M' just like 'l', except in user mode the value is
76 * multiplied by 2^20 (think Mebibyte)
Markus Armbrusteree9545d2010-03-26 09:07:08 +010077 * 'f' double
Markus Armbruster3350a4d2010-01-25 14:23:03 +010078 * user mode accepts an optional G, g, M, m, K, k suffix,
79 * which multiplies the value by 2^30 for suffixes G and
80 * g, 2^20 for M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010081 * 'T' double
82 * user mode accepts an optional ms, us, ns suffix,
83 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +000084 * '/' optional gdb-like print format (like "/10x")
85 *
Luiz Capitulinofb466602009-08-28 15:27:27 -030086 * '?' optional type (for all types, except '/')
87 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +010088 * 'b' boolean
89 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -030090 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +000091 *
92 */
93
Adam Litke940cc302010-01-25 12:18:44 -060094typedef struct MonitorCompletionData MonitorCompletionData;
95struct MonitorCompletionData {
96 Monitor *mon;
97 void (*user_print)(Monitor *mon, const QObject *data);
98};
99
Anthony Liguoric227f092009-10-01 16:12:16 -0500100typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000101 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000102 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000103 const char *params;
104 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300105 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300106 union {
107 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300108 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600109 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300110 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200111 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600112 int (*cmd_async)(Monitor *mon, const QDict *params,
113 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300114 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200115 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500116} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000117
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100118/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500119typedef struct mon_fd_t mon_fd_t;
120struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100121 char *name;
122 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500123 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100124};
125
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200126typedef struct MonitorControl {
127 QObject *id;
128 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200129 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200130} MonitorControl;
131
aliguori87127162009-03-05 23:01:29 +0000132struct Monitor {
133 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200134 int mux_out;
135 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000136 int flags;
137 int suspend_cnt;
138 uint8_t outbuf[1024];
139 int outbuf_index;
140 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200141 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000142 CPUState *mon_cpu;
143 BlockDriverCompletionFunc *password_completion_cb;
144 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200145#ifdef CONFIG_DEBUG_MONITOR
146 int print_calls_nr;
147#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200148 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500149 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000150 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000151};
152
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200153#ifdef CONFIG_DEBUG_MONITOR
154#define MON_DEBUG(fmt, ...) do { \
155 fprintf(stderr, "Monitor: "); \
156 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200157
158static inline void mon_print_count_inc(Monitor *mon)
159{
160 mon->print_calls_nr++;
161}
162
163static inline void mon_print_count_init(Monitor *mon)
164{
165 mon->print_calls_nr = 0;
166}
167
168static inline int mon_print_count_get(const Monitor *mon)
169{
170 return mon->print_calls_nr;
171}
172
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200173#else /* !CONFIG_DEBUG_MONITOR */
174#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200175static inline void mon_print_count_inc(Monitor *mon) { }
176static inline void mon_print_count_init(Monitor *mon) { }
177static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200178#endif /* CONFIG_DEBUG_MONITOR */
179
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300180/* QMP checker flags */
181#define QMP_ACCEPT_UNKNOWNS 1
182
Blue Swirl72cf2d42009-09-12 07:36:22 +0000183static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000184
Anthony Liguoric227f092009-10-01 16:12:16 -0500185static const mon_cmd_t mon_cmds[];
186static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000187
Markus Armbruster8631b602010-02-18 11:41:55 +0100188Monitor *cur_mon;
189Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000190
aliguori731b0362009-03-05 23:01:42 +0000191static void monitor_command_cb(Monitor *mon, const char *cmdline,
192 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000193
Luiz Capitulino09069b12010-02-04 18:10:06 -0200194static inline int qmp_cmd_mode(const Monitor *mon)
195{
196 return (mon->mc ? mon->mc->command_mode : 0);
197}
198
Luiz Capitulino418173c2009-11-26 22:58:51 -0200199/* Return true if in control mode, false otherwise */
200static inline int monitor_ctrl_mode(const Monitor *mon)
201{
202 return (mon->flags & MONITOR_USE_CONTROL);
203}
204
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100205/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
206int monitor_cur_is_qmp(void)
207{
208 return cur_mon && monitor_ctrl_mode(cur_mon);
209}
210
aliguori731b0362009-03-05 23:01:42 +0000211static void monitor_read_command(Monitor *mon, int show_prompt)
212{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200213 if (!mon->rs)
214 return;
215
aliguori731b0362009-03-05 23:01:42 +0000216 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
217 if (show_prompt)
218 readline_show_prompt(mon->rs);
219}
bellard6a00d602005-11-21 23:25:50 +0000220
aliguoricde76ee2009-03-05 23:01:51 +0000221static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
222 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000223{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100224 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100225 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100226 return -EINVAL;
227 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000228 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
229 /* prompt is printed on return from the command handler */
230 return 0;
231 } else {
232 monitor_printf(mon, "terminal does not support password prompting\n");
233 return -ENOTTY;
234 }
aliguoribb5fc202009-03-05 23:01:15 +0000235}
236
aliguori376253e2009-03-05 23:01:23 +0000237void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000238{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200239 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
aliguori731b0362009-03-05 23:01:42 +0000240 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
241 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000242 }
243}
244
245/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000246static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000247{
ths60fe76f2007-12-16 03:02:09 +0000248 char c;
aliguori731b0362009-03-05 23:01:42 +0000249
bellard7e2515e2004-08-01 21:52:19 +0000250 for(;;) {
251 c = *str++;
252 if (c == '\0')
253 break;
bellard7ba12602006-07-14 20:26:42 +0000254 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000255 mon->outbuf[mon->outbuf_index++] = '\r';
256 mon->outbuf[mon->outbuf_index++] = c;
257 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
258 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000259 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000260 }
261}
262
aliguori376253e2009-03-05 23:01:23 +0000263void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000264{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200265 char buf[4096];
266
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200267 if (!mon)
268 return;
269
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200270 mon_print_count_inc(mon);
271
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200272 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200273 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200274 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200275
276 vsnprintf(buf, sizeof(buf), fmt, ap);
277 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000278}
279
aliguori376253e2009-03-05 23:01:23 +0000280void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000281{
282 va_list ap;
283 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000284 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000285 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000286}
287
aliguori376253e2009-03-05 23:01:23 +0000288void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000289{
290 int i;
291
292 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000293 switch (filename[i]) {
294 case ' ':
295 case '"':
296 case '\\':
297 monitor_printf(mon, "\\%c", filename[i]);
298 break;
299 case '\t':
300 monitor_printf(mon, "\\t");
301 break;
302 case '\r':
303 monitor_printf(mon, "\\r");
304 break;
305 case '\n':
306 monitor_printf(mon, "\\n");
307 break;
308 default:
309 monitor_printf(mon, "%c", filename[i]);
310 break;
311 }
thsfef30742006-12-22 14:11:32 +0000312 }
313}
314
bellard7fe48482004-10-09 18:08:01 +0000315static int monitor_fprintf(FILE *stream, const char *fmt, ...)
316{
317 va_list ap;
318 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000319 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000320 va_end(ap);
321 return 0;
322}
323
Luiz Capitulino13c74252009-10-07 13:41:55 -0300324static void monitor_user_noop(Monitor *mon, const QObject *data) { }
325
Luiz Capitulino13917be2009-10-07 13:41:54 -0300326static inline int monitor_handler_ported(const mon_cmd_t *cmd)
327{
328 return cmd->user_print != NULL;
329}
330
Adam Litke940cc302010-01-25 12:18:44 -0600331static inline bool monitor_handler_is_async(const mon_cmd_t *cmd)
332{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200333 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600334}
335
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200336static inline bool monitor_cmd_user_only(const mon_cmd_t *cmd)
337{
338 return (cmd->flags & MONITOR_CMD_USER_ONLY);
339}
340
Luiz Capitulino8204a912009-11-18 23:05:31 -0200341static inline int monitor_has_error(const Monitor *mon)
342{
343 return mon->error != NULL;
344}
345
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200346static void monitor_json_emitter(Monitor *mon, const QObject *data)
347{
348 QString *json;
349
350 json = qobject_to_json(data);
351 assert(json != NULL);
352
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200353 qstring_append_chr(json, '\n');
354 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200355
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200356 QDECREF(json);
357}
358
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200359static void monitor_protocol_emitter(Monitor *mon, QObject *data)
360{
361 QDict *qmp;
362
363 qmp = qdict_new();
364
365 if (!monitor_has_error(mon)) {
366 /* success response */
367 if (data) {
368 qobject_incref(data);
369 qdict_put_obj(qmp, "return", data);
370 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200371 /* return an empty QDict by default */
372 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200373 }
374 } else {
375 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100376 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200377 qdict_put(qmp, "error", mon->error->error);
378 QINCREF(mon->error->error);
379 QDECREF(mon->error);
380 mon->error = NULL;
381 }
382
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200383 if (mon->mc->id) {
384 qdict_put_obj(qmp, "id", mon->mc->id);
385 mon->mc->id = NULL;
386 }
387
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200388 monitor_json_emitter(mon, QOBJECT(qmp));
389 QDECREF(qmp);
390}
391
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200392static void timestamp_put(QDict *qdict)
393{
394 int err;
395 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000396 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200397
Blue Swirld08d6f02009-12-04 18:06:39 +0000398 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200399 if (err < 0)
400 return;
401
402 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
403 "'microseconds': %" PRId64 " }",
404 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200405 qdict_put_obj(qdict, "timestamp", obj);
406}
407
408/**
409 * monitor_protocol_event(): Generate a Monitor event
410 *
411 * Event-specific data can be emitted through the (optional) 'data' parameter.
412 */
413void monitor_protocol_event(MonitorEvent event, QObject *data)
414{
415 QDict *qmp;
416 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600417 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200418
Blue Swirl242cd002009-12-04 18:05:45 +0000419 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200420
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200421 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000422 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200423 event_name = "SHUTDOWN";
424 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000425 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200426 event_name = "RESET";
427 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000428 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200429 event_name = "POWERDOWN";
430 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000431 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200432 event_name = "STOP";
433 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300434 case QEVENT_RESUME:
435 event_name = "RESUME";
436 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200437 case QEVENT_VNC_CONNECTED:
438 event_name = "VNC_CONNECTED";
439 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200440 case QEVENT_VNC_INITIALIZED:
441 event_name = "VNC_INITIALIZED";
442 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200443 case QEVENT_VNC_DISCONNECTED:
444 event_name = "VNC_DISCONNECTED";
445 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200446 case QEVENT_BLOCK_IO_ERROR:
447 event_name = "BLOCK_IO_ERROR";
448 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300449 case QEVENT_RTC_CHANGE:
450 event_name = "RTC_CHANGE";
451 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300452 case QEVENT_WATCHDOG:
453 event_name = "WATCHDOG";
454 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200455 default:
456 abort();
457 break;
458 }
459
460 qmp = qdict_new();
461 timestamp_put(qmp);
462 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200463 if (data) {
464 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200465 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200466 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200467
Adam Litkef039a562010-01-15 08:34:02 -0600468 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200469 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200470 monitor_json_emitter(mon, QOBJECT(qmp));
471 }
Adam Litkef039a562010-01-15 08:34:02 -0600472 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200473 QDECREF(qmp);
474}
475
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200476static int do_qmp_capabilities(Monitor *mon, const QDict *params,
477 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200478{
479 /* Will setup QMP capabilities in the future */
480 if (monitor_ctrl_mode(mon)) {
481 mon->mc->command_mode = 1;
482 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200483
484 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200485}
486
bellard9dc39cb2004-03-14 21:38:27 +0000487static int compare_cmd(const char *name, const char *list)
488{
489 const char *p, *pstart;
490 int len;
491 len = strlen(name);
492 p = list;
493 for(;;) {
494 pstart = p;
495 p = strchr(p, '|');
496 if (!p)
497 p = pstart + strlen(pstart);
498 if ((p - pstart) == len && !memcmp(pstart, name, len))
499 return 1;
500 if (*p == '\0')
501 break;
502 p++;
503 }
504 return 0;
505}
506
Anthony Liguoric227f092009-10-01 16:12:16 -0500507static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000508 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000509{
Anthony Liguoric227f092009-10-01 16:12:16 -0500510 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000511
512 for(cmd = cmds; cmd->name != NULL; cmd++) {
513 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000514 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
515 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000516 }
517}
518
aliguori376253e2009-03-05 23:01:23 +0000519static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000520{
521 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000522 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000523 } else {
aliguori376253e2009-03-05 23:01:23 +0000524 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000525 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000526 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000527 monitor_printf(mon, "Log items (comma separated):\n");
528 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000529 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000530 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000531 }
532 }
bellard9dc39cb2004-03-14 21:38:27 +0000533 }
534}
535
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300536static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300537{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300538 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300539}
540
Adam Litke940cc302010-01-25 12:18:44 -0600541static void user_monitor_complete(void *opaque, QObject *ret_data)
542{
543 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
544
545 if (ret_data) {
546 data->user_print(data->mon, ret_data);
547 }
548 monitor_resume(data->mon);
549 qemu_free(data);
550}
551
552static void qmp_monitor_complete(void *opaque, QObject *ret_data)
553{
554 monitor_protocol_emitter(opaque, ret_data);
555}
556
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300557static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
558 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600559{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300560 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600561}
562
563static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
564{
565 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
566}
567
568static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
569 const QDict *params)
570{
571 int ret;
572
573 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
574 cb_data->mon = mon;
575 cb_data->user_print = cmd->user_print;
576 monitor_suspend(mon);
577 ret = cmd->mhandler.cmd_async(mon, params,
578 user_monitor_complete, cb_data);
579 if (ret < 0) {
580 monitor_resume(mon);
581 qemu_free(cb_data);
582 }
583}
584
585static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
586{
587 int ret;
588
589 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
590 cb_data->mon = mon;
591 cb_data->user_print = cmd->user_print;
592 monitor_suspend(mon);
593 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
594 if (ret < 0) {
595 monitor_resume(mon);
596 qemu_free(cb_data);
597 }
598}
599
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200600static int do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000601{
Anthony Liguoric227f092009-10-01 16:12:16 -0500602 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300603 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000604
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200605 if (!item) {
606 assert(monitor_ctrl_mode(mon) == 0);
bellard9dc39cb2004-03-14 21:38:27 +0000607 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200608 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300609
610 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000611 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300612 break;
bellard9dc39cb2004-03-14 21:38:27 +0000613 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300614
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200615 if (cmd->name == NULL) {
616 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100617 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200618 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200619 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300620 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200621 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300622
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200623 if (monitor_ctrl_mode(mon) && monitor_cmd_user_only(cmd)) {
624 qerror_report(QERR_COMMAND_NOT_FOUND, item);
625 return -1;
626 }
627
Adam Litke940cc302010-01-25 12:18:44 -0600628 if (monitor_handler_is_async(cmd)) {
629 if (monitor_ctrl_mode(mon)) {
630 qmp_async_info_handler(mon, cmd);
631 } else {
632 user_async_info_handler(mon, cmd);
633 }
634 /*
635 * Indicate that this command is asynchronous and will not return any
636 * data (not even empty). Instead, the data will be returned via a
637 * completion callback.
638 */
639 *ret_data = qobject_from_jsonf("{ '__mon_async': 'return' }");
640 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300641 cmd->mhandler.info_new(mon, ret_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200642
643 if (!monitor_ctrl_mode(mon)) {
644 /*
645 * User Protocol function is called here, Monitor Protocol is
646 * handled by monitor_call_handler()
647 */
648 if (*ret_data)
649 cmd->user_print(mon, *ret_data);
650 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300651 } else {
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200652 if (monitor_ctrl_mode(mon)) {
653 /* handler not converted yet */
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100654 qerror_report(QERR_COMMAND_NOT_FOUND, item);
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200655 return -1;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200656 } else {
657 cmd->mhandler.info(mon);
658 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300659 }
660
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200661 return 0;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300662
663help:
664 help_cmd(mon, "info");
Luiz Capitulino4fdc94b2010-02-10 23:50:00 -0200665 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000666}
667
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200668static void do_info_version_print(Monitor *mon, const QObject *data)
669{
670 QDict *qdict;
671
672 qdict = qobject_to_qdict(data);
673
674 monitor_printf(mon, "%s%s\n", qdict_get_str(qdict, "qemu"),
675 qdict_get_str(qdict, "package"));
676}
677
Luiz Capitulinoab2d3182009-10-07 13:42:02 -0300678static void do_info_version(Monitor *mon, QObject **ret_data)
bellard9bc9d1c2004-10-10 15:15:51 +0000679{
Luiz Capitulino45e914c2009-12-10 17:15:58 -0200680 *ret_data = qobject_from_jsonf("{ 'qemu': %s, 'package': %s }",
681 QEMU_VERSION, QEMU_PKGVERSION);
bellard9bc9d1c2004-10-10 15:15:51 +0000682}
683
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200684static void do_info_name_print(Monitor *mon, const QObject *data)
thsc35734b2007-03-19 15:17:08 +0000685{
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200686 QDict *qdict;
687
688 qdict = qobject_to_qdict(data);
689 if (qdict_size(qdict) == 0) {
690 return;
691 }
692
693 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
694}
695
Luiz Capitulinoe05486c2009-12-10 17:16:01 -0200696static void do_info_name(Monitor *mon, QObject **ret_data)
697{
698 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
699 qobject_from_jsonf("{}");
thsc35734b2007-03-19 15:17:08 +0000700}
701
Luiz Capitulino1a728672009-12-10 17:15:56 -0200702static QObject *get_cmd_dict(const char *name)
703{
704 const char *p;
705
706 /* Remove '|' from some commands */
707 p = strchr(name, '|');
708 if (p) {
709 p++;
710 } else {
711 p = name;
712 }
713
714 return qobject_from_jsonf("{ 'name': %s }", p);
715}
716
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200717static void do_info_commands(Monitor *mon, QObject **ret_data)
718{
719 QList *cmd_list;
720 const mon_cmd_t *cmd;
721
722 cmd_list = qlist_new();
723
724 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200725 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd) &&
726 !compare_cmd(cmd->name, "info")) {
Luiz Capitulino1a728672009-12-10 17:15:56 -0200727 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200728 }
729 }
730
731 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200732 if (monitor_handler_ported(cmd) && !monitor_cmd_user_only(cmd)) {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200733 char buf[128];
734 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
Luiz Capitulino1a728672009-12-10 17:15:56 -0200735 qlist_append_obj(cmd_list, get_cmd_dict(buf));
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200736 }
737 }
738
739 *ret_data = QOBJECT(cmd_list);
740}
741
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200742static void do_info_uuid_print(Monitor *mon, const QObject *data)
blueswir1f1f23ad2008-09-18 18:30:20 +0000743{
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200744 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
745}
746
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200747static void do_info_uuid(Monitor *mon, QObject **ret_data)
748{
749 char uuid[64];
750
751 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
aliguori376253e2009-03-05 23:01:23 +0000752 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
753 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
754 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
755 qemu_uuid[14], qemu_uuid[15]);
Luiz Capitulino9603ceb2009-12-10 17:16:03 -0200756 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
thsa36e69d2007-12-02 05:18:19 +0000757}
758
bellard6a00d602005-11-21 23:25:50 +0000759/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000760static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000761{
762 CPUState *env;
763
764 for(env = first_cpu; env != NULL; env = env->next_cpu) {
765 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000766 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000767 return 0;
768 }
769 }
770 return -1;
771}
772
pbrook9596ebb2007-11-18 01:44:38 +0000773static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000774{
aliguori731b0362009-03-05 23:01:42 +0000775 if (!cur_mon->mon_cpu) {
bellard6a00d602005-11-21 23:25:50 +0000776 mon_set_cpu(0);
777 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300778 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000779 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000780}
781
aliguori376253e2009-03-05 23:01:23 +0000782static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000783{
bellard6a00d602005-11-21 23:25:50 +0000784 CPUState *env;
785 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000786#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000787 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000788 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000789#else
aliguori376253e2009-03-05 23:01:23 +0000790 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000791 0);
bellard9307c4c2004-04-04 12:57:25 +0000792#endif
793}
794
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300795static void print_cpu_iter(QObject *obj, void *opaque)
796{
797 QDict *cpu;
798 int active = ' ';
799 Monitor *mon = opaque;
800
801 assert(qobject_type(obj) == QTYPE_QDICT);
802 cpu = qobject_to_qdict(obj);
803
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200804 if (qdict_get_bool(cpu, "current")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300805 active = '*';
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200806 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300807
808 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
809
810#if defined(TARGET_I386)
811 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
812 (target_ulong) qdict_get_int(cpu, "pc"));
813#elif defined(TARGET_PPC)
814 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
815 (target_long) qdict_get_int(cpu, "nip"));
816#elif defined(TARGET_SPARC)
817 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
818 (target_long) qdict_get_int(cpu, "pc"));
819 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
820 (target_long) qdict_get_int(cpu, "npc"));
821#elif defined(TARGET_MIPS)
822 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
823 (target_long) qdict_get_int(cpu, "PC"));
824#endif
825
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200826 if (qdict_get_bool(cpu, "halted")) {
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300827 monitor_printf(mon, " (halted)");
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200828 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300829
830 monitor_printf(mon, "\n");
831}
832
833static void monitor_print_cpus(Monitor *mon, const QObject *data)
834{
835 QList *cpu_list;
836
837 assert(qobject_type(data) == QTYPE_QLIST);
838 cpu_list = qobject_to_qlist(data);
839 qlist_iter(cpu_list, print_cpu_iter, mon);
840}
841
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300842static void do_info_cpus(Monitor *mon, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000843{
844 CPUState *env;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300845 QList *cpu_list;
846
847 cpu_list = qlist_new();
bellard6a00d602005-11-21 23:25:50 +0000848
849 /* just to set the default cpu if not already done */
850 mon_get_cpu();
851
852 for(env = first_cpu; env != NULL; env = env->next_cpu) {
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200853 QDict *cpu;
854 QObject *obj;
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300855
Avi Kivity4c0960c2009-08-17 23:19:53 +0300856 cpu_synchronize_state(env);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300857
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200858 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
859 env->cpu_index, env == mon->mon_cpu,
860 env->halted);
Luiz Capitulino55483ad2009-12-10 17:15:57 -0200861
862 cpu = qobject_to_qdict(obj);
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300863
bellard6a00d602005-11-21 23:25:50 +0000864#if defined(TARGET_I386)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300865 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
bellarde80e1cc2005-11-23 22:05:28 +0000866#elif defined(TARGET_PPC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300867 qdict_put(cpu, "nip", qint_from_int(env->nip));
bellardba3c64f2005-12-05 20:31:52 +0000868#elif defined(TARGET_SPARC)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300869 qdict_put(cpu, "pc", qint_from_int(env->pc));
870 qdict_put(cpu, "npc", qint_from_int(env->npc));
thsead93602007-09-06 00:18:15 +0000871#elif defined(TARGET_MIPS)
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300872 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
bellardce5232c2008-05-28 17:14:10 +0000873#endif
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300874
875 qlist_append(cpu_list, cpu);
bellard6a00d602005-11-21 23:25:50 +0000876 }
Luiz Capitulino8f3cec02009-10-07 13:42:04 -0300877
878 *ret_data = QOBJECT(cpu_list);
bellard6a00d602005-11-21 23:25:50 +0000879}
880
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200881static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard6a00d602005-11-21 23:25:50 +0000882{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300883 int index = qdict_get_int(qdict, "index");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200884 if (mon_set_cpu(index) < 0) {
Markus Armbrustere17ba872010-03-25 17:22:36 +0100885 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
886 "a CPU number");
Luiz Capitulino584cbdb2010-02-10 23:49:51 -0200887 return -1;
888 }
889 return 0;
bellard6a00d602005-11-21 23:25:50 +0000890}
891
aliguori376253e2009-03-05 23:01:23 +0000892static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000893{
aliguori376253e2009-03-05 23:01:23 +0000894 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000895}
896
aliguori376253e2009-03-05 23:01:23 +0000897static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000898{
899 int i;
bellard7e2515e2004-08-01 21:52:19 +0000900 const char *str;
ths3b46e622007-09-17 08:09:54 +0000901
aliguoricde76ee2009-03-05 23:01:51 +0000902 if (!mon->rs)
903 return;
bellard7e2515e2004-08-01 21:52:19 +0000904 i = 0;
905 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000906 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000907 if (!str)
908 break;
aliguori376253e2009-03-05 23:01:23 +0000909 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000910 i++;
bellardaa455482004-04-04 13:07:25 +0000911 }
912}
913
j_mayer76a66252007-03-07 08:32:30 +0000914#if defined(TARGET_PPC)
915/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000916static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000917{
918 CPUState *env;
919
920 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000921 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000922}
923#endif
924
Luiz Capitulinob223f352009-10-07 13:41:56 -0300925/**
926 * do_quit(): Quit QEMU execution
927 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200928static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard9dc39cb2004-03-14 21:38:27 +0000929{
Luiz Capitulino39b59d22010-05-11 18:08:20 -0300930 monitor_suspend(mon);
931 no_shutdown = 0;
932 qemu_system_shutdown_request();
933
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200934 return 0;
bellard9dc39cb2004-03-14 21:38:27 +0000935}
936
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200937static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000938{
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200939 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100940 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200941 return -1;
942 }
aliguoribb5fc202009-03-05 23:01:15 +0000943
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200944 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100945}
946
947static void change_vnc_password_cb(Monitor *mon, const char *password,
948 void *opaque)
949{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100950 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000951 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000952}
953
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200954static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000955{
ths70848512007-08-25 01:37:05 +0000956 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000957 strcmp(target, "password") == 0) {
958 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000959 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000960 strncpy(password, arg, sizeof(password));
961 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200962 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000963 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200964 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000965 }
ths70848512007-08-25 01:37:05 +0000966 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200967 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100968 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200969 return -1;
970 }
ths70848512007-08-25 01:37:05 +0000971 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200972
973 return 0;
thse25a5822007-08-25 01:36:20 +0000974}
975
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100976/**
977 * do_change(): Change a removable medium, or VNC configuration
978 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200979static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000980{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300981 const char *device = qdict_get_str(qdict, "device");
982 const char *target = qdict_get_str(qdict, "target");
983 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200984 int ret;
985
thse25a5822007-08-25 01:36:20 +0000986 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200987 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000988 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200989 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000990 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200991
992 return ret;
thse25a5822007-08-25 01:36:20 +0000993}
994
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300995static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +0000996{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300997 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -0300998 return 0;
bellard59a983b2004-03-17 23:17:16 +0000999}
1000
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001001static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001002{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001003 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001004}
1005
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001006static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001007{
1008 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001009 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001010
bellard9307c4c2004-04-04 12:57:25 +00001011 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001012 mask = 0;
1013 } else {
bellard9307c4c2004-04-04 12:57:25 +00001014 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001015 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001016 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001017 return;
1018 }
1019 }
1020 cpu_set_log(mask);
1021}
1022
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001023static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001024{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001025 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001026 if (!option || !strcmp(option, "on")) {
1027 singlestep = 1;
1028 } else if (!strcmp(option, "off")) {
1029 singlestep = 0;
1030 } else {
1031 monitor_printf(mon, "unexpected option %s\n", option);
1032 }
1033}
1034
Luiz Capitulinoe0c97bd2009-10-07 13:41:57 -03001035/**
1036 * do_stop(): Stop VM execution
1037 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001038static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard8a7ddc32004-03-31 19:00:16 +00001039{
1040 vm_stop(EXCP_INTERRUPT);
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001041 return 0;
bellard8a7ddc32004-03-31 19:00:16 +00001042}
1043
aliguoribb5fc202009-03-05 23:01:15 +00001044static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001045
aliguori376253e2009-03-05 23:01:23 +00001046struct bdrv_iterate_context {
1047 Monitor *mon;
1048 int err;
1049};
aliguoric0f4ce72009-03-05 23:01:01 +00001050
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001051/**
1052 * do_cont(): Resume emulation.
1053 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001054static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001055{
1056 struct bdrv_iterate_context context = { mon, 0 };
1057
1058 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001059 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001060 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001061 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001062 return 0;
1063 } else {
1064 return -1;
1065 }
bellard8a7ddc32004-03-31 19:00:16 +00001066}
1067
aliguoribb5fc202009-03-05 23:01:15 +00001068static void bdrv_key_cb(void *opaque, int err)
1069{
aliguori376253e2009-03-05 23:01:23 +00001070 Monitor *mon = opaque;
1071
aliguoribb5fc202009-03-05 23:01:15 +00001072 /* another key was set successfully, retry to continue */
1073 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001074 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001075}
1076
1077static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1078{
aliguori376253e2009-03-05 23:01:23 +00001079 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001080
aliguori376253e2009-03-05 23:01:23 +00001081 if (!context->err && bdrv_key_required(bs)) {
1082 context->err = -EBUSY;
1083 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1084 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001085 }
1086}
1087
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001088static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001089{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001090 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001091 if (!device)
1092 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1093 if (gdbserver_start(device) < 0) {
1094 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1095 device);
1096 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001097 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001098 } else {
aliguori59030a82009-04-05 18:43:41 +00001099 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1100 device);
bellard8a7ddc32004-03-31 19:00:16 +00001101 }
1102}
1103
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001104static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001105{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001106 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001107 if (select_watchdog_action(action) == -1) {
1108 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1109 }
1110}
1111
aliguori376253e2009-03-05 23:01:23 +00001112static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001113{
aliguori376253e2009-03-05 23:01:23 +00001114 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001115 switch(c) {
1116 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001117 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001118 break;
1119 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001120 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001121 break;
1122 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001123 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001124 break;
1125 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001126 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001127 break;
1128 default:
1129 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001130 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001131 } else {
aliguori376253e2009-03-05 23:01:23 +00001132 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001133 }
1134 break;
1135 }
aliguori376253e2009-03-05 23:01:23 +00001136 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001137}
1138
aliguori376253e2009-03-05 23:01:23 +00001139static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001140 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001141{
bellard6a00d602005-11-21 23:25:50 +00001142 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001143 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001144 uint8_t buf[16];
1145 uint64_t v;
1146
1147 if (format == 'i') {
1148 int flags;
1149 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001150 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001151#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001152 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001153 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001154 } else if (wsize == 4) {
1155 flags = 0;
1156 } else {
bellard6a15fd12006-04-12 21:07:07 +00001157 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001158 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001159 if (env) {
1160#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001161 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001162 (env->segs[R_CS].flags & DESC_L_MASK))
1163 flags = 2;
1164 else
1165#endif
1166 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1167 flags = 1;
1168 }
bellard4c27ba22004-04-25 18:05:08 +00001169 }
1170#endif
aliguori376253e2009-03-05 23:01:23 +00001171 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001172 return;
1173 }
1174
1175 len = wsize * count;
1176 if (wsize == 1)
1177 line_size = 8;
1178 else
1179 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001180 max_digits = 0;
1181
1182 switch(format) {
1183 case 'o':
1184 max_digits = (wsize * 8 + 2) / 3;
1185 break;
1186 default:
1187 case 'x':
1188 max_digits = (wsize * 8) / 4;
1189 break;
1190 case 'u':
1191 case 'd':
1192 max_digits = (wsize * 8 * 10 + 32) / 33;
1193 break;
1194 case 'c':
1195 wsize = 1;
1196 break;
1197 }
1198
1199 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001200 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001201 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001202 else
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001204 l = len;
1205 if (l > line_size)
1206 l = line_size;
1207 if (is_physical) {
1208 cpu_physical_memory_rw(addr, buf, l, 0);
1209 } else {
bellard6a00d602005-11-21 23:25:50 +00001210 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001211 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001212 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001213 break;
1214 }
bellard9307c4c2004-04-04 12:57:25 +00001215 }
ths5fafdf22007-09-16 21:08:06 +00001216 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001217 while (i < l) {
1218 switch(wsize) {
1219 default:
1220 case 1:
1221 v = ldub_raw(buf + i);
1222 break;
1223 case 2:
1224 v = lduw_raw(buf + i);
1225 break;
1226 case 4:
bellard92a31b12005-02-10 22:00:52 +00001227 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001228 break;
1229 case 8:
1230 v = ldq_raw(buf + i);
1231 break;
1232 }
aliguori376253e2009-03-05 23:01:23 +00001233 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001234 switch(format) {
1235 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001236 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001237 break;
1238 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001239 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001240 break;
1241 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001242 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001243 break;
1244 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001245 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001246 break;
1247 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001248 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001249 break;
1250 }
1251 i += wsize;
1252 }
aliguori376253e2009-03-05 23:01:23 +00001253 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001254 addr += l;
1255 len -= l;
1256 }
1257}
1258
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001259static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001260{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001261 int count = qdict_get_int(qdict, "count");
1262 int format = qdict_get_int(qdict, "format");
1263 int size = qdict_get_int(qdict, "size");
1264 target_long addr = qdict_get_int(qdict, "addr");
1265
aliguori376253e2009-03-05 23:01:23 +00001266 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001267}
1268
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001269static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001270{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001271 int count = qdict_get_int(qdict, "count");
1272 int format = qdict_get_int(qdict, "format");
1273 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001274 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001275
aliguori376253e2009-03-05 23:01:23 +00001276 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001277}
1278
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001279static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001280{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001281 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001282 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001283
blueswir17743e582007-09-24 18:39:04 +00001284#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001285 switch(format) {
1286 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001288 break;
1289 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001290 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001291 break;
1292 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001293 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001294 break;
1295 default:
1296 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001297 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001298 break;
1299 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001300 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001301 break;
1302 }
bellard92a31b12005-02-10 22:00:52 +00001303#else
1304 switch(format) {
1305 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001306 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001307 break;
1308 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001309 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001310 break;
1311 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001312 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001313 break;
1314 default:
1315 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001316 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001317 break;
1318 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001319 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001320 break;
1321 }
1322#endif
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001324}
1325
Luiz Capitulino98696222010-02-10 23:49:58 -02001326static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001327{
1328 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001329 uint32_t size = qdict_get_int(qdict, "size");
1330 const char *filename = qdict_get_str(qdict, "filename");
1331 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001332 uint32_t l;
1333 CPUState *env;
1334 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001335 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001336
1337 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001338
1339 f = fopen(filename, "wb");
1340 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001341 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001342 return -1;
bellardb371dc52007-01-03 15:20:39 +00001343 }
1344 while (size != 0) {
1345 l = sizeof(buf);
1346 if (l > size)
1347 l = size;
1348 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001349 if (fwrite(buf, 1, l, f) != l) {
1350 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1351 goto exit;
1352 }
bellardb371dc52007-01-03 15:20:39 +00001353 addr += l;
1354 size -= l;
1355 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001356
1357 ret = 0;
1358
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001359exit:
bellardb371dc52007-01-03 15:20:39 +00001360 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001361 return ret;
bellardb371dc52007-01-03 15:20:39 +00001362}
1363
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001364static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001365 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001366{
1367 FILE *f;
1368 uint32_t l;
1369 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001370 uint32_t size = qdict_get_int(qdict, "size");
1371 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001372 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001373 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001374
1375 f = fopen(filename, "wb");
1376 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001377 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001378 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001379 }
1380 while (size != 0) {
1381 l = sizeof(buf);
1382 if (l > size)
1383 l = size;
1384 cpu_physical_memory_rw(addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001385 if (fwrite(buf, 1, l, f) != l) {
1386 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1387 goto exit;
1388 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001389 fflush(f);
1390 addr += l;
1391 size -= l;
1392 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001393
1394 ret = 0;
1395
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001396exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001397 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001398 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001399}
1400
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001401static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001402{
1403 uint32_t addr;
1404 uint8_t buf[1];
1405 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001406 uint32_t start = qdict_get_int(qdict, "start");
1407 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001408
1409 sum = 0;
1410 for(addr = start; addr < (start + size); addr++) {
1411 cpu_physical_memory_rw(addr, buf, 1, 0);
1412 /* BSD sum algorithm ('sum' Unix command) */
1413 sum = (sum >> 1) | (sum << 15);
1414 sum += buf[0];
1415 }
aliguori376253e2009-03-05 23:01:23 +00001416 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001417}
1418
bellarda3a91a32004-06-04 11:06:21 +00001419typedef struct {
1420 int keycode;
1421 const char *name;
1422} KeyDef;
1423
1424static const KeyDef key_defs[] = {
1425 { 0x2a, "shift" },
1426 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001427
bellarda3a91a32004-06-04 11:06:21 +00001428 { 0x38, "alt" },
1429 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001430 { 0x64, "altgr" },
1431 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001432 { 0x1d, "ctrl" },
1433 { 0x9d, "ctrl_r" },
1434
1435 { 0xdd, "menu" },
1436
1437 { 0x01, "esc" },
1438
1439 { 0x02, "1" },
1440 { 0x03, "2" },
1441 { 0x04, "3" },
1442 { 0x05, "4" },
1443 { 0x06, "5" },
1444 { 0x07, "6" },
1445 { 0x08, "7" },
1446 { 0x09, "8" },
1447 { 0x0a, "9" },
1448 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001449 { 0x0c, "minus" },
1450 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001451 { 0x0e, "backspace" },
1452
1453 { 0x0f, "tab" },
1454 { 0x10, "q" },
1455 { 0x11, "w" },
1456 { 0x12, "e" },
1457 { 0x13, "r" },
1458 { 0x14, "t" },
1459 { 0x15, "y" },
1460 { 0x16, "u" },
1461 { 0x17, "i" },
1462 { 0x18, "o" },
1463 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001464 { 0x1a, "bracket_left" },
1465 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001466 { 0x1c, "ret" },
1467
1468 { 0x1e, "a" },
1469 { 0x1f, "s" },
1470 { 0x20, "d" },
1471 { 0x21, "f" },
1472 { 0x22, "g" },
1473 { 0x23, "h" },
1474 { 0x24, "j" },
1475 { 0x25, "k" },
1476 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001477 { 0x27, "semicolon" },
1478 { 0x28, "apostrophe" },
1479 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001480
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001481 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001482 { 0x2c, "z" },
1483 { 0x2d, "x" },
1484 { 0x2e, "c" },
1485 { 0x2f, "v" },
1486 { 0x30, "b" },
1487 { 0x31, "n" },
1488 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001489 { 0x33, "comma" },
1490 { 0x34, "dot" },
1491 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001492
balrog4d3b6f62008-02-10 16:33:14 +00001493 { 0x37, "asterisk" },
1494
bellarda3a91a32004-06-04 11:06:21 +00001495 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001496 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001497 { 0x3b, "f1" },
1498 { 0x3c, "f2" },
1499 { 0x3d, "f3" },
1500 { 0x3e, "f4" },
1501 { 0x3f, "f5" },
1502 { 0x40, "f6" },
1503 { 0x41, "f7" },
1504 { 0x42, "f8" },
1505 { 0x43, "f9" },
1506 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001507 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001508 { 0x46, "scroll_lock" },
1509
bellard64866c32006-05-07 18:03:31 +00001510 { 0xb5, "kp_divide" },
1511 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001512 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001513 { 0x4e, "kp_add" },
1514 { 0x9c, "kp_enter" },
1515 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001516 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001517
1518 { 0x52, "kp_0" },
1519 { 0x4f, "kp_1" },
1520 { 0x50, "kp_2" },
1521 { 0x51, "kp_3" },
1522 { 0x4b, "kp_4" },
1523 { 0x4c, "kp_5" },
1524 { 0x4d, "kp_6" },
1525 { 0x47, "kp_7" },
1526 { 0x48, "kp_8" },
1527 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001528
bellarda3a91a32004-06-04 11:06:21 +00001529 { 0x56, "<" },
1530
1531 { 0x57, "f11" },
1532 { 0x58, "f12" },
1533
1534 { 0xb7, "print" },
1535
1536 { 0xc7, "home" },
1537 { 0xc9, "pgup" },
1538 { 0xd1, "pgdn" },
1539 { 0xcf, "end" },
1540
1541 { 0xcb, "left" },
1542 { 0xc8, "up" },
1543 { 0xd0, "down" },
1544 { 0xcd, "right" },
1545
1546 { 0xd2, "insert" },
1547 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001548#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1549 { 0xf0, "stop" },
1550 { 0xf1, "again" },
1551 { 0xf2, "props" },
1552 { 0xf3, "undo" },
1553 { 0xf4, "front" },
1554 { 0xf5, "copy" },
1555 { 0xf6, "open" },
1556 { 0xf7, "paste" },
1557 { 0xf8, "find" },
1558 { 0xf9, "cut" },
1559 { 0xfa, "lf" },
1560 { 0xfb, "help" },
1561 { 0xfc, "meta_l" },
1562 { 0xfd, "meta_r" },
1563 { 0xfe, "compose" },
1564#endif
bellarda3a91a32004-06-04 11:06:21 +00001565 { 0, NULL },
1566};
1567
1568static int get_keycode(const char *key)
1569{
1570 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001571 char *endp;
1572 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001573
1574 for(p = key_defs; p->name != NULL; p++) {
1575 if (!strcmp(key, p->name))
1576 return p->keycode;
1577 }
bellard64866c32006-05-07 18:03:31 +00001578 if (strstart(key, "0x", NULL)) {
1579 ret = strtoul(key, &endp, 0);
1580 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1581 return ret;
1582 }
bellarda3a91a32004-06-04 11:06:21 +00001583 return -1;
1584}
1585
balrogc8256f92008-06-08 22:45:01 +00001586#define MAX_KEYCODES 16
1587static uint8_t keycodes[MAX_KEYCODES];
1588static int nb_pending_keycodes;
1589static QEMUTimer *key_timer;
1590
1591static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001592{
balrogc8256f92008-06-08 22:45:01 +00001593 int keycode;
1594
1595 while (nb_pending_keycodes > 0) {
1596 nb_pending_keycodes--;
1597 keycode = keycodes[nb_pending_keycodes];
1598 if (keycode & 0x80)
1599 kbd_put_keycode(0xe0);
1600 kbd_put_keycode(keycode | 0x80);
1601 }
1602}
1603
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001604static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001605{
balrog3401c0d2008-06-04 10:05:59 +00001606 char keyname_buf[16];
1607 char *separator;
1608 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001609 const char *string = qdict_get_str(qdict, "string");
1610 int has_hold_time = qdict_haskey(qdict, "hold_time");
1611 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001612
balrogc8256f92008-06-08 22:45:01 +00001613 if (nb_pending_keycodes > 0) {
1614 qemu_del_timer(key_timer);
1615 release_keys(NULL);
1616 }
1617 if (!has_hold_time)
1618 hold_time = 100;
1619 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001620 while (1) {
1621 separator = strchr(string, '-');
1622 keyname_len = separator ? separator - string : strlen(string);
1623 if (keyname_len > 0) {
1624 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1625 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001626 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001627 return;
bellarda3a91a32004-06-04 11:06:21 +00001628 }
balrogc8256f92008-06-08 22:45:01 +00001629 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001630 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001631 return;
1632 }
1633 keyname_buf[keyname_len] = 0;
1634 keycode = get_keycode(keyname_buf);
1635 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001636 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001637 return;
1638 }
balrogc8256f92008-06-08 22:45:01 +00001639 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001640 }
balrog3401c0d2008-06-04 10:05:59 +00001641 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001642 break;
balrog3401c0d2008-06-04 10:05:59 +00001643 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001644 }
balrogc8256f92008-06-08 22:45:01 +00001645 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001646 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001647 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001648 keycode = keycodes[i];
1649 if (keycode & 0x80)
1650 kbd_put_keycode(0xe0);
1651 kbd_put_keycode(keycode & 0x7f);
1652 }
balrogc8256f92008-06-08 22:45:01 +00001653 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001654 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001655 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001656}
1657
bellard13224a82006-07-14 22:03:35 +00001658static int mouse_button_state;
1659
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001660static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001661{
1662 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001663 const char *dx_str = qdict_get_str(qdict, "dx_str");
1664 const char *dy_str = qdict_get_str(qdict, "dy_str");
1665 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001666 dx = strtol(dx_str, NULL, 0);
1667 dy = strtol(dy_str, NULL, 0);
1668 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001669 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001670 dz = strtol(dz_str, NULL, 0);
1671 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1672}
1673
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001674static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001675{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001676 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001677 mouse_button_state = button_state;
1678 kbd_mouse_event(0, 0, 0, mouse_button_state);
1679}
1680
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001681static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001682{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001683 int size = qdict_get_int(qdict, "size");
1684 int addr = qdict_get_int(qdict, "addr");
1685 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001686 uint32_t val;
1687 int suffix;
1688
1689 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001690 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001691 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001692 addr++;
1693 }
1694 addr &= 0xffff;
1695
1696 switch(size) {
1697 default:
1698 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001699 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001700 suffix = 'b';
1701 break;
1702 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001703 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001704 suffix = 'w';
1705 break;
1706 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001707 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001708 suffix = 'l';
1709 break;
1710 }
aliguori376253e2009-03-05 23:01:23 +00001711 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1712 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001713}
bellarda3a91a32004-06-04 11:06:21 +00001714
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001715static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001716{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001717 int size = qdict_get_int(qdict, "size");
1718 int addr = qdict_get_int(qdict, "addr");
1719 int val = qdict_get_int(qdict, "val");
1720
Jan Kiszkaf1147842009-07-14 10:20:11 +02001721 addr &= IOPORTS_MASK;
1722
1723 switch (size) {
1724 default:
1725 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001726 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001727 break;
1728 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001729 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001730 break;
1731 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001732 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001733 break;
1734 }
1735}
1736
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001737static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001738{
1739 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001740 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001741
Jan Kiszka76e30d02009-07-02 00:19:02 +02001742 res = qemu_boot_set(bootdevice);
1743 if (res == 0) {
1744 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1745 } else if (res > 0) {
1746 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001747 } else {
aliguori376253e2009-03-05 23:01:23 +00001748 monitor_printf(mon, "no function defined to set boot device list for "
1749 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001750 }
1751}
1752
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001753/**
1754 * do_system_reset(): Issue a machine reset
1755 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001756static int do_system_reset(Monitor *mon, const QDict *qdict,
1757 QObject **ret_data)
bellarde4f90822004-06-20 12:35:44 +00001758{
1759 qemu_system_reset_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001760 return 0;
bellarde4f90822004-06-20 12:35:44 +00001761}
1762
Luiz Capitulino43076662009-10-07 13:41:59 -03001763/**
1764 * do_system_powerdown(): Issue a machine powerdown
1765 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001766static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1767 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001768{
1769 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001770 return 0;
bellard34751872005-07-02 14:31:34 +00001771}
1772
bellardb86bda52004-09-18 19:32:46 +00001773#if defined(TARGET_I386)
aliguori376253e2009-03-05 23:01:23 +00001774static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
bellardb86bda52004-09-18 19:32:46 +00001775{
aliguori376253e2009-03-05 23:01:23 +00001776 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1777 addr,
1778 pte & mask,
1779 pte & PG_GLOBAL_MASK ? 'G' : '-',
1780 pte & PG_PSE_MASK ? 'P' : '-',
1781 pte & PG_DIRTY_MASK ? 'D' : '-',
1782 pte & PG_ACCESSED_MASK ? 'A' : '-',
1783 pte & PG_PCD_MASK ? 'C' : '-',
1784 pte & PG_PWT_MASK ? 'T' : '-',
1785 pte & PG_USER_MASK ? 'U' : '-',
1786 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001787}
1788
aliguori376253e2009-03-05 23:01:23 +00001789static void tlb_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001790{
bellard6a00d602005-11-21 23:25:50 +00001791 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001792 int l1, l2;
1793 uint32_t pgd, pde, pte;
1794
bellard6a00d602005-11-21 23:25:50 +00001795 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001796
bellardb86bda52004-09-18 19:32:46 +00001797 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001798 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001799 return;
1800 }
1801 pgd = env->cr[3] & ~0xfff;
1802 for(l1 = 0; l1 < 1024; l1++) {
1803 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1804 pde = le32_to_cpu(pde);
1805 if (pde & PG_PRESENT_MASK) {
1806 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001807 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
bellardb86bda52004-09-18 19:32:46 +00001808 } else {
1809 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001810 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001811 (uint8_t *)&pte, 4);
1812 pte = le32_to_cpu(pte);
1813 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001814 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001815 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001816 ~0xfff);
1817 }
1818 }
1819 }
1820 }
1821 }
1822}
1823
aliguori376253e2009-03-05 23:01:23 +00001824static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001825 uint32_t end, int prot)
1826{
bellard9746b152004-11-11 18:30:24 +00001827 int prot1;
1828 prot1 = *plast_prot;
1829 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001830 if (*pstart != -1) {
aliguori376253e2009-03-05 23:01:23 +00001831 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1832 *pstart, end, end - *pstart,
1833 prot1 & PG_USER_MASK ? 'u' : '-',
1834 'r',
1835 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001836 }
1837 if (prot != 0)
1838 *pstart = end;
1839 else
1840 *pstart = -1;
1841 *plast_prot = prot;
1842 }
1843}
1844
aliguori376253e2009-03-05 23:01:23 +00001845static void mem_info(Monitor *mon)
bellardb86bda52004-09-18 19:32:46 +00001846{
bellard6a00d602005-11-21 23:25:50 +00001847 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001848 int l1, l2, prot, last_prot;
1849 uint32_t pgd, pde, pte, start, end;
1850
bellard6a00d602005-11-21 23:25:50 +00001851 env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00001852
bellardb86bda52004-09-18 19:32:46 +00001853 if (!(env->cr[0] & CR0_PG_MASK)) {
aliguori376253e2009-03-05 23:01:23 +00001854 monitor_printf(mon, "PG disabled\n");
bellardb86bda52004-09-18 19:32:46 +00001855 return;
1856 }
1857 pgd = env->cr[3] & ~0xfff;
1858 last_prot = 0;
1859 start = -1;
1860 for(l1 = 0; l1 < 1024; l1++) {
1861 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1862 pde = le32_to_cpu(pde);
1863 end = l1 << 22;
1864 if (pde & PG_PRESENT_MASK) {
1865 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1866 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001867 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001868 } else {
1869 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001870 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001871 (uint8_t *)&pte, 4);
1872 pte = le32_to_cpu(pte);
1873 end = (l1 << 22) + (l2 << 12);
1874 if (pte & PG_PRESENT_MASK) {
1875 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1876 } else {
1877 prot = 0;
1878 }
aliguori376253e2009-03-05 23:01:23 +00001879 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001880 }
1881 }
1882 } else {
1883 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001884 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001885 }
1886 }
1887}
1888#endif
1889
aurel327c664e22009-03-03 06:12:22 +00001890#if defined(TARGET_SH4)
1891
aliguori376253e2009-03-05 23:01:23 +00001892static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00001893{
aliguori376253e2009-03-05 23:01:23 +00001894 monitor_printf(mon, " tlb%i:\t"
1895 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1896 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1897 "dirty=%hhu writethrough=%hhu\n",
1898 idx,
1899 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1900 tlb->v, tlb->sh, tlb->c, tlb->pr,
1901 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00001902}
1903
aliguori376253e2009-03-05 23:01:23 +00001904static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00001905{
1906 CPUState *env = mon_get_cpu();
1907 int i;
1908
aliguori376253e2009-03-05 23:01:23 +00001909 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001910 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001911 print_tlb (mon, i, &env->itlb[i]);
1912 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00001913 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00001914 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00001915}
1916
1917#endif
1918
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001919static void do_info_kvm_print(Monitor *mon, const QObject *data)
1920{
1921 QDict *qdict;
1922
1923 qdict = qobject_to_qdict(data);
1924
1925 monitor_printf(mon, "kvm support: ");
1926 if (qdict_get_bool(qdict, "present")) {
1927 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1928 "enabled" : "disabled");
1929 } else {
1930 monitor_printf(mon, "not compiled\n");
1931 }
1932}
1933
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001934static void do_info_kvm(Monitor *mon, QObject **ret_data)
aliguori7ba1e612008-11-05 16:04:33 +00001935{
1936#ifdef CONFIG_KVM
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001937 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1938 kvm_enabled());
aliguori7ba1e612008-11-05 16:04:33 +00001939#else
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02001940 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
aliguori7ba1e612008-11-05 16:04:33 +00001941#endif
1942}
1943
aliguori030ea372009-04-21 22:30:47 +00001944static void do_info_numa(Monitor *mon)
1945{
aliguorib28b6232009-04-22 20:20:29 +00001946 int i;
aliguori030ea372009-04-21 22:30:47 +00001947 CPUState *env;
1948
1949 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1950 for (i = 0; i < nb_numa_nodes; i++) {
1951 monitor_printf(mon, "node %d cpus:", i);
1952 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1953 if (env->numa_node == i) {
1954 monitor_printf(mon, " %d", env->cpu_index);
1955 }
1956 }
1957 monitor_printf(mon, "\n");
1958 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1959 node_mem[i] >> 20);
1960 }
1961}
1962
bellard5f1ce942006-02-08 22:40:15 +00001963#ifdef CONFIG_PROFILER
1964
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02001965int64_t qemu_time;
1966int64_t dev_time;
1967
aliguori376253e2009-03-05 23:01:23 +00001968static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001969{
1970 int64_t total;
1971 total = qemu_time;
1972 if (total == 0)
1973 total = 1;
aliguori376253e2009-03-05 23:01:23 +00001974 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001975 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00001976 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001977 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00001978 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001979 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00001980}
1981#else
aliguori376253e2009-03-05 23:01:23 +00001982static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00001983{
aliguori376253e2009-03-05 23:01:23 +00001984 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00001985}
1986#endif
1987
bellardec36b692006-07-16 18:57:03 +00001988/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001989static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00001990
aliguori376253e2009-03-05 23:01:23 +00001991static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00001992{
1993 int i;
1994 CaptureState *s;
1995
1996 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00001997 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00001998 s->ops.info (s->opaque);
1999 }
2000}
2001
Blue Swirl23130862009-06-06 08:22:04 +00002002#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002003static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002004{
2005 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002006 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002007 CaptureState *s;
2008
2009 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2010 if (i == n) {
2011 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002012 QLIST_REMOVE (s, entries);
bellardec36b692006-07-16 18:57:03 +00002013 qemu_free (s);
2014 return;
2015 }
2016 }
2017}
2018
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002019static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002020{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002021 const char *path = qdict_get_str(qdict, "path");
2022 int has_freq = qdict_haskey(qdict, "freq");
2023 int freq = qdict_get_try_int(qdict, "freq", -1);
2024 int has_bits = qdict_haskey(qdict, "bits");
2025 int bits = qdict_get_try_int(qdict, "bits", -1);
2026 int has_channels = qdict_haskey(qdict, "nchannels");
2027 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002028 CaptureState *s;
2029
2030 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002031
2032 freq = has_freq ? freq : 44100;
2033 bits = has_bits ? bits : 16;
2034 nchannels = has_channels ? nchannels : 2;
2035
2036 if (wav_start_capture (s, path, freq, bits, nchannels)) {
aliguori376253e2009-03-05 23:01:23 +00002037 monitor_printf(mon, "Faied to add wave capture\n");
bellardec36b692006-07-16 18:57:03 +00002038 qemu_free (s);
2039 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002040 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002041}
2042#endif
2043
aurel32dc1c0b72008-04-27 23:52:12 +00002044#if defined(TARGET_I386)
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002045static void do_inject_nmi(Monitor *mon, const QDict *qdict)
aurel32dc1c0b72008-04-27 23:52:12 +00002046{
2047 CPUState *env;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002048 int cpu_index = qdict_get_int(qdict, "cpu_index");
aurel32dc1c0b72008-04-27 23:52:12 +00002049
2050 for (env = first_cpu; env != NULL; env = env->next_cpu)
2051 if (env->cpu_index == cpu_index) {
2052 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2053 break;
2054 }
2055}
2056#endif
2057
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002058static void do_info_status_print(Monitor *mon, const QObject *data)
aurel326f9c5ee2008-12-18 22:43:56 +00002059{
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002060 QDict *qdict;
2061
2062 qdict = qobject_to_qdict(data);
2063
2064 monitor_printf(mon, "VM status: ");
2065 if (qdict_get_bool(qdict, "running")) {
2066 monitor_printf(mon, "running");
2067 if (qdict_get_bool(qdict, "singlestep")) {
2068 monitor_printf(mon, " (single step mode)");
aurel321b530a62009-04-05 20:08:59 +00002069 }
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002070 } else {
2071 monitor_printf(mon, "paused");
2072 }
2073
2074 monitor_printf(mon, "\n");
2075}
2076
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002077static void do_info_status(Monitor *mon, QObject **ret_data)
2078{
2079 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2080 vm_running, singlestep);
aurel326f9c5ee2008-12-18 22:43:56 +00002081}
2082
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002083static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002084{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002085 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002086
aliguori76655d62009-03-06 20:27:37 +00002087 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002088 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002089 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002090 return acl;
2091}
aliguori76655d62009-03-06 20:27:37 +00002092
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002093static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002094{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002095 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002096 qemu_acl *acl = find_acl(mon, aclname);
2097 qemu_acl_entry *entry;
2098 int i = 0;
2099
2100 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002101 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002102 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002103 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002104 i++;
2105 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002106 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002107 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002108 }
2109}
2110
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002111static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002112{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002113 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002114 qemu_acl *acl = find_acl(mon, aclname);
2115
2116 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002117 qemu_acl_reset(acl);
2118 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002119 }
2120}
aliguori76655d62009-03-06 20:27:37 +00002121
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002122static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002123{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002124 const char *aclname = qdict_get_str(qdict, "aclname");
2125 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002126 qemu_acl *acl = find_acl(mon, aclname);
2127
2128 if (acl) {
2129 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002130 acl->defaultDeny = 0;
2131 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002132 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002133 acl->defaultDeny = 1;
2134 monitor_printf(mon, "acl: policy set to 'deny'\n");
2135 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002136 monitor_printf(mon, "acl: unknown policy '%s', "
2137 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002138 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002139 }
2140}
aliguori76655d62009-03-06 20:27:37 +00002141
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002142static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002143{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002144 const char *aclname = qdict_get_str(qdict, "aclname");
2145 const char *match = qdict_get_str(qdict, "match");
2146 const char *policy = qdict_get_str(qdict, "policy");
2147 int has_index = qdict_haskey(qdict, "index");
2148 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002149 qemu_acl *acl = find_acl(mon, aclname);
2150 int deny, ret;
2151
2152 if (acl) {
2153 if (strcmp(policy, "allow") == 0) {
2154 deny = 0;
2155 } else if (strcmp(policy, "deny") == 0) {
2156 deny = 1;
2157 } else {
2158 monitor_printf(mon, "acl: unknown policy '%s', "
2159 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002160 return;
2161 }
aliguori28a76be2009-03-06 20:27:40 +00002162 if (has_index)
2163 ret = qemu_acl_insert(acl, deny, match, index);
2164 else
2165 ret = qemu_acl_append(acl, deny, match);
2166 if (ret < 0)
2167 monitor_printf(mon, "acl: unable to add acl entry\n");
2168 else
2169 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002170 }
2171}
aliguori76655d62009-03-06 20:27:37 +00002172
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002173static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002174{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002175 const char *aclname = qdict_get_str(qdict, "aclname");
2176 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002177 qemu_acl *acl = find_acl(mon, aclname);
2178 int ret;
aliguori76655d62009-03-06 20:27:37 +00002179
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002180 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002181 ret = qemu_acl_remove(acl, match);
2182 if (ret < 0)
2183 monitor_printf(mon, "acl: no matching acl entry\n");
2184 else
2185 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002186 }
2187}
2188
Huang Ying79c4f6b2009-06-23 10:05:14 +08002189#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002190static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002191{
2192 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002193 int cpu_index = qdict_get_int(qdict, "cpu_index");
2194 int bank = qdict_get_int(qdict, "bank");
2195 uint64_t status = qdict_get_int(qdict, "status");
2196 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2197 uint64_t addr = qdict_get_int(qdict, "addr");
2198 uint64_t misc = qdict_get_int(qdict, "misc");
Huang Ying79c4f6b2009-06-23 10:05:14 +08002199
2200 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2201 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2202 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2203 break;
2204 }
2205}
2206#endif
2207
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002208static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002209{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002210 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002211 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002212 int fd;
2213
2214 fd = qemu_chr_get_msgfd(mon->chr);
2215 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002216 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002217 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002218 }
2219
2220 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002221 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2222 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002223 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002224 }
2225
Blue Swirl72cf2d42009-09-12 07:36:22 +00002226 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002227 if (strcmp(monfd->name, fdname) != 0) {
2228 continue;
2229 }
2230
2231 close(monfd->fd);
2232 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002233 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002234 }
2235
Anthony Liguoric227f092009-10-01 16:12:16 -05002236 monfd = qemu_mallocz(sizeof(mon_fd_t));
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002237 monfd->name = qemu_strdup(fdname);
2238 monfd->fd = fd;
2239
Blue Swirl72cf2d42009-09-12 07:36:22 +00002240 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002241 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002242}
2243
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002244static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002245{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002246 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002247 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002248
Blue Swirl72cf2d42009-09-12 07:36:22 +00002249 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002250 if (strcmp(monfd->name, fdname) != 0) {
2251 continue;
2252 }
2253
Blue Swirl72cf2d42009-09-12 07:36:22 +00002254 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002255 close(monfd->fd);
2256 qemu_free(monfd->name);
2257 qemu_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002258 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002259 }
2260
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002261 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002262 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002263}
2264
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002265static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002266{
2267 int saved_vm_running = vm_running;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002268 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002269
2270 vm_stop(0);
2271
Markus Armbruster03cd4652010-02-17 16:24:10 +01002272 if (load_vmstate(name) >= 0 && saved_vm_running)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002273 vm_start();
2274}
2275
Mark McLoughlin7768e042009-07-22 09:11:41 +01002276int monitor_get_fd(Monitor *mon, const char *fdname)
2277{
Anthony Liguoric227f092009-10-01 16:12:16 -05002278 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002279
Blue Swirl72cf2d42009-09-12 07:36:22 +00002280 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002281 int fd;
2282
2283 if (strcmp(monfd->name, fdname) != 0) {
2284 continue;
2285 }
2286
2287 fd = monfd->fd;
2288
2289 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002290 QLIST_REMOVE(monfd, next);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002291 qemu_free(monfd->name);
2292 qemu_free(monfd);
2293
2294 return fd;
2295 }
2296
2297 return -1;
2298}
2299
Anthony Liguoric227f092009-10-01 16:12:16 -05002300static const mon_cmd_t mon_cmds[] = {
Blue Swirl23130862009-06-06 08:22:04 +00002301#include "qemu-monitor.h"
ths5fafdf22007-09-16 21:08:06 +00002302 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002303};
2304
Blue Swirl23130862009-06-06 08:22:04 +00002305/* Please update qemu-monitor.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002306static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002307 {
2308 .name = "version",
2309 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002310 .params = "",
2311 .help = "show the version of QEMU",
Luiz Capitulino45e914c2009-12-10 17:15:58 -02002312 .user_print = do_info_version_print,
Luiz Capitulinoab2d3182009-10-07 13:42:02 -03002313 .mhandler.info_new = do_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002314 },
2315 {
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -02002316 .name = "commands",
2317 .args_type = "",
2318 .params = "",
2319 .help = "list QMP available commands",
2320 .user_print = monitor_user_noop,
2321 .mhandler.info_new = do_info_commands,
2322 },
2323 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002324 .name = "network",
2325 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002326 .params = "",
2327 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002328 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002329 },
2330 {
2331 .name = "chardev",
2332 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002333 .params = "",
2334 .help = "show the character devices",
Luiz Capitulino588b3832009-12-10 17:16:08 -02002335 .user_print = qemu_chr_info_print,
2336 .mhandler.info_new = qemu_chr_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002337 },
2338 {
2339 .name = "block",
2340 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002341 .params = "",
2342 .help = "show the block devices",
Luiz Capitulinod15e5462009-12-10 17:16:06 -02002343 .user_print = bdrv_info_print,
2344 .mhandler.info_new = bdrv_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002345 },
2346 {
2347 .name = "blockstats",
2348 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002349 .params = "",
2350 .help = "show block device statistics",
Luiz Capitulino218a5362009-12-10 17:16:07 -02002351 .user_print = bdrv_stats_print,
2352 .mhandler.info_new = bdrv_info_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002353 },
2354 {
2355 .name = "registers",
2356 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002357 .params = "",
2358 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002359 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002360 },
2361 {
2362 .name = "cpus",
2363 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002364 .params = "",
2365 .help = "show infos for each CPU",
Luiz Capitulino8f3cec02009-10-07 13:42:04 -03002366 .user_print = monitor_print_cpus,
2367 .mhandler.info_new = do_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002368 },
2369 {
2370 .name = "history",
2371 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002372 .params = "",
2373 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002374 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002375 },
2376 {
2377 .name = "irq",
2378 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002379 .params = "",
2380 .help = "show the interrupts statistics (if available)",
Luiz Capitulino910df892009-10-07 13:41:51 -03002381 .mhandler.info = irq_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002382 },
2383 {
2384 .name = "pic",
2385 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002386 .params = "",
2387 .help = "show i8259 (PIC) state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002388 .mhandler.info = pic_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002389 },
2390 {
2391 .name = "pci",
2392 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002393 .params = "",
2394 .help = "show PCI info",
Luiz Capitulino163c8a52010-01-21 19:15:40 -02002395 .user_print = do_pci_info_print,
2396 .mhandler.info_new = do_pci_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002397 },
aurel327c664e22009-03-03 06:12:22 +00002398#if defined(TARGET_I386) || defined(TARGET_SH4)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002399 {
2400 .name = "tlb",
2401 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002402 .params = "",
2403 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002404 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002405 },
aurel327c664e22009-03-03 06:12:22 +00002406#endif
2407#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002408 {
2409 .name = "mem",
2410 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002411 .params = "",
2412 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002413 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002414 },
bellardb86bda52004-09-18 19:32:46 +00002415#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002416 {
2417 .name = "jit",
2418 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002419 .params = "",
2420 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002421 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002422 },
2423 {
2424 .name = "kvm",
2425 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002426 .params = "",
2427 .help = "show KVM information",
Luiz Capitulino2af5ba72009-12-10 17:16:00 -02002428 .user_print = do_info_kvm_print,
2429 .mhandler.info_new = do_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002430 },
2431 {
2432 .name = "numa",
2433 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002434 .params = "",
2435 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002436 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002437 },
2438 {
2439 .name = "usb",
2440 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002441 .params = "",
2442 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002443 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002444 },
2445 {
2446 .name = "usbhost",
2447 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002448 .params = "",
2449 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002450 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002451 },
2452 {
2453 .name = "profile",
2454 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002455 .params = "",
2456 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002457 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002458 },
2459 {
2460 .name = "capture",
2461 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002462 .params = "",
2463 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002464 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002465 },
2466 {
2467 .name = "snapshots",
2468 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002469 .params = "",
2470 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002471 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002472 },
2473 {
2474 .name = "status",
2475 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002476 .params = "",
2477 .help = "show the current VM status (running|paused)",
Luiz Capitulinoc0e85202009-12-10 17:15:59 -02002478 .user_print = do_info_status_print,
2479 .mhandler.info_new = do_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002480 },
2481 {
2482 .name = "pcmcia",
2483 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002484 .params = "",
2485 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002486 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002487 },
2488 {
2489 .name = "mice",
2490 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002491 .params = "",
2492 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -02002493 .user_print = do_info_mice_print,
2494 .mhandler.info_new = do_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002495 },
2496 {
2497 .name = "vnc",
2498 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002499 .params = "",
2500 .help = "show the vnc server status",
Luiz Capitulinod96fd292009-12-10 17:16:10 -02002501 .user_print = do_info_vnc_print,
2502 .mhandler.info_new = do_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002503 },
2504 {
2505 .name = "name",
2506 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002507 .params = "",
2508 .help = "show the current VM name",
Luiz Capitulinoe05486c2009-12-10 17:16:01 -02002509 .user_print = do_info_name_print,
2510 .mhandler.info_new = do_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002511 },
2512 {
2513 .name = "uuid",
2514 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002515 .params = "",
2516 .help = "show the current VM UUID",
Luiz Capitulino9603ceb2009-12-10 17:16:03 -02002517 .user_print = do_info_uuid_print,
2518 .mhandler.info_new = do_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002519 },
j_mayer76a66252007-03-07 08:32:30 +00002520#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002521 {
2522 .name = "cpustats",
2523 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002524 .params = "",
2525 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002526 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002527 },
j_mayer76a66252007-03-07 08:32:30 +00002528#endif
blueswir131a60e22007-10-26 18:42:59 +00002529#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002530 {
2531 .name = "usernet",
2532 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002533 .params = "",
2534 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002535 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002536 },
blueswir131a60e22007-10-26 18:42:59 +00002537#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002538 {
2539 .name = "migrate",
2540 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002541 .params = "",
2542 .help = "show migration status",
Luiz Capitulinoc86a6682009-12-10 17:16:05 -02002543 .user_print = do_info_migrate_print,
2544 .mhandler.info_new = do_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002545 },
2546 {
2547 .name = "balloon",
2548 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002549 .params = "",
2550 .help = "show balloon information",
Luiz Capitulinocc1d9c72009-10-07 13:42:03 -03002551 .user_print = monitor_print_balloon,
Adam Litke625a5be2010-01-26 14:17:35 -06002552 .mhandler.info_async = do_info_balloon,
Jan Kiszka8ac470c2010-06-16 00:38:39 +02002553 .flags = MONITOR_CMD_ASYNC,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002554 },
2555 {
2556 .name = "qtree",
2557 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002558 .params = "",
2559 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002560 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002561 },
2562 {
2563 .name = "qdm",
2564 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002565 .params = "",
2566 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002567 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002568 },
2569 {
2570 .name = "roms",
2571 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002572 .params = "",
2573 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002574 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002575 },
2576 {
2577 .name = NULL,
2578 },
bellard9dc39cb2004-03-14 21:38:27 +00002579};
2580
bellard9307c4c2004-04-04 12:57:25 +00002581/*******************************************************************/
2582
2583static const char *pch;
2584static jmp_buf expr_env;
2585
bellard92a31b12005-02-10 22:00:52 +00002586#define MD_TLONG 0
2587#define MD_I32 1
2588
bellard9307c4c2004-04-04 12:57:25 +00002589typedef struct MonitorDef {
2590 const char *name;
2591 int offset;
blueswir18662d652008-10-02 18:32:44 +00002592 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002593 int type;
bellard9307c4c2004-04-04 12:57:25 +00002594} MonitorDef;
2595
bellard57206fd2004-04-25 18:54:52 +00002596#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002597static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002598{
bellard6a00d602005-11-21 23:25:50 +00002599 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002600 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002601}
2602#endif
2603
bellarda541f292004-04-12 20:39:29 +00002604#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002605static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002606{
bellard6a00d602005-11-21 23:25:50 +00002607 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002608 unsigned int u;
2609 int i;
2610
2611 u = 0;
2612 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002613 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002614
2615 return u;
2616}
2617
blueswir18662d652008-10-02 18:32:44 +00002618static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002619{
bellard6a00d602005-11-21 23:25:50 +00002620 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002621 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002622}
2623
blueswir18662d652008-10-02 18:32:44 +00002624static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002625{
bellard6a00d602005-11-21 23:25:50 +00002626 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002627 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002628}
bellard9fddaa02004-05-21 12:59:32 +00002629
blueswir18662d652008-10-02 18:32:44 +00002630static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002631{
bellard6a00d602005-11-21 23:25:50 +00002632 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002633 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002634}
2635
blueswir18662d652008-10-02 18:32:44 +00002636static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002637{
bellard6a00d602005-11-21 23:25:50 +00002638 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002639 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002640}
2641
blueswir18662d652008-10-02 18:32:44 +00002642static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002643{
bellard6a00d602005-11-21 23:25:50 +00002644 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002645 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002646}
bellarda541f292004-04-12 20:39:29 +00002647#endif
2648
bellarde95c8d52004-09-30 22:22:08 +00002649#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00002650#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00002651static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002652{
bellard6a00d602005-11-21 23:25:50 +00002653 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00002654
2655 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00002656}
bellard7b936c02005-10-30 17:05:13 +00002657#endif
bellarde95c8d52004-09-30 22:22:08 +00002658
blueswir18662d652008-10-02 18:32:44 +00002659static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00002660{
bellard6a00d602005-11-21 23:25:50 +00002661 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002662 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00002663}
2664#endif
2665
blueswir18662d652008-10-02 18:32:44 +00002666static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00002667#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00002668
2669#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00002670 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00002671 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00002672 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00002673
bellard9307c4c2004-04-04 12:57:25 +00002674 { "eax", offsetof(CPUState, regs[0]) },
2675 { "ecx", offsetof(CPUState, regs[1]) },
2676 { "edx", offsetof(CPUState, regs[2]) },
2677 { "ebx", offsetof(CPUState, regs[3]) },
2678 { "esp|sp", offsetof(CPUState, regs[4]) },
2679 { "ebp|fp", offsetof(CPUState, regs[5]) },
2680 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00002681 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00002682#ifdef TARGET_X86_64
2683 { "r8", offsetof(CPUState, regs[8]) },
2684 { "r9", offsetof(CPUState, regs[9]) },
2685 { "r10", offsetof(CPUState, regs[10]) },
2686 { "r11", offsetof(CPUState, regs[11]) },
2687 { "r12", offsetof(CPUState, regs[12]) },
2688 { "r13", offsetof(CPUState, regs[13]) },
2689 { "r14", offsetof(CPUState, regs[14]) },
2690 { "r15", offsetof(CPUState, regs[15]) },
2691#endif
bellard9307c4c2004-04-04 12:57:25 +00002692 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00002693 { "eip", offsetof(CPUState, eip) },
2694 SEG("cs", R_CS)
2695 SEG("ds", R_DS)
2696 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00002697 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00002698 SEG("fs", R_FS)
2699 SEG("gs", R_GS)
2700 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00002701#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00002702 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00002703 { "r0", offsetof(CPUState, gpr[0]) },
2704 { "r1", offsetof(CPUState, gpr[1]) },
2705 { "r2", offsetof(CPUState, gpr[2]) },
2706 { "r3", offsetof(CPUState, gpr[3]) },
2707 { "r4", offsetof(CPUState, gpr[4]) },
2708 { "r5", offsetof(CPUState, gpr[5]) },
2709 { "r6", offsetof(CPUState, gpr[6]) },
2710 { "r7", offsetof(CPUState, gpr[7]) },
2711 { "r8", offsetof(CPUState, gpr[8]) },
2712 { "r9", offsetof(CPUState, gpr[9]) },
2713 { "r10", offsetof(CPUState, gpr[10]) },
2714 { "r11", offsetof(CPUState, gpr[11]) },
2715 { "r12", offsetof(CPUState, gpr[12]) },
2716 { "r13", offsetof(CPUState, gpr[13]) },
2717 { "r14", offsetof(CPUState, gpr[14]) },
2718 { "r15", offsetof(CPUState, gpr[15]) },
2719 { "r16", offsetof(CPUState, gpr[16]) },
2720 { "r17", offsetof(CPUState, gpr[17]) },
2721 { "r18", offsetof(CPUState, gpr[18]) },
2722 { "r19", offsetof(CPUState, gpr[19]) },
2723 { "r20", offsetof(CPUState, gpr[20]) },
2724 { "r21", offsetof(CPUState, gpr[21]) },
2725 { "r22", offsetof(CPUState, gpr[22]) },
2726 { "r23", offsetof(CPUState, gpr[23]) },
2727 { "r24", offsetof(CPUState, gpr[24]) },
2728 { "r25", offsetof(CPUState, gpr[25]) },
2729 { "r26", offsetof(CPUState, gpr[26]) },
2730 { "r27", offsetof(CPUState, gpr[27]) },
2731 { "r28", offsetof(CPUState, gpr[28]) },
2732 { "r29", offsetof(CPUState, gpr[29]) },
2733 { "r30", offsetof(CPUState, gpr[30]) },
2734 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00002735 /* Floating point registers */
2736 { "f0", offsetof(CPUState, fpr[0]) },
2737 { "f1", offsetof(CPUState, fpr[1]) },
2738 { "f2", offsetof(CPUState, fpr[2]) },
2739 { "f3", offsetof(CPUState, fpr[3]) },
2740 { "f4", offsetof(CPUState, fpr[4]) },
2741 { "f5", offsetof(CPUState, fpr[5]) },
2742 { "f6", offsetof(CPUState, fpr[6]) },
2743 { "f7", offsetof(CPUState, fpr[7]) },
2744 { "f8", offsetof(CPUState, fpr[8]) },
2745 { "f9", offsetof(CPUState, fpr[9]) },
2746 { "f10", offsetof(CPUState, fpr[10]) },
2747 { "f11", offsetof(CPUState, fpr[11]) },
2748 { "f12", offsetof(CPUState, fpr[12]) },
2749 { "f13", offsetof(CPUState, fpr[13]) },
2750 { "f14", offsetof(CPUState, fpr[14]) },
2751 { "f15", offsetof(CPUState, fpr[15]) },
2752 { "f16", offsetof(CPUState, fpr[16]) },
2753 { "f17", offsetof(CPUState, fpr[17]) },
2754 { "f18", offsetof(CPUState, fpr[18]) },
2755 { "f19", offsetof(CPUState, fpr[19]) },
2756 { "f20", offsetof(CPUState, fpr[20]) },
2757 { "f21", offsetof(CPUState, fpr[21]) },
2758 { "f22", offsetof(CPUState, fpr[22]) },
2759 { "f23", offsetof(CPUState, fpr[23]) },
2760 { "f24", offsetof(CPUState, fpr[24]) },
2761 { "f25", offsetof(CPUState, fpr[25]) },
2762 { "f26", offsetof(CPUState, fpr[26]) },
2763 { "f27", offsetof(CPUState, fpr[27]) },
2764 { "f28", offsetof(CPUState, fpr[28]) },
2765 { "f29", offsetof(CPUState, fpr[29]) },
2766 { "f30", offsetof(CPUState, fpr[30]) },
2767 { "f31", offsetof(CPUState, fpr[31]) },
2768 { "fpscr", offsetof(CPUState, fpscr) },
2769 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00002770 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00002771 { "lr", offsetof(CPUState, lr) },
2772 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00002773 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00002774 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00002775 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00002776 { "msr", 0, &monitor_get_msr, },
2777 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00002778 { "tbu", 0, &monitor_get_tbu, },
2779 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00002780#if defined(TARGET_PPC64)
2781 /* Address space register */
2782 { "asr", offsetof(CPUState, asr) },
2783#endif
2784 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00002785 { "sdr1", offsetof(CPUState, sdr1) },
2786 { "sr0", offsetof(CPUState, sr[0]) },
2787 { "sr1", offsetof(CPUState, sr[1]) },
2788 { "sr2", offsetof(CPUState, sr[2]) },
2789 { "sr3", offsetof(CPUState, sr[3]) },
2790 { "sr4", offsetof(CPUState, sr[4]) },
2791 { "sr5", offsetof(CPUState, sr[5]) },
2792 { "sr6", offsetof(CPUState, sr[6]) },
2793 { "sr7", offsetof(CPUState, sr[7]) },
2794 { "sr8", offsetof(CPUState, sr[8]) },
2795 { "sr9", offsetof(CPUState, sr[9]) },
2796 { "sr10", offsetof(CPUState, sr[10]) },
2797 { "sr11", offsetof(CPUState, sr[11]) },
2798 { "sr12", offsetof(CPUState, sr[12]) },
2799 { "sr13", offsetof(CPUState, sr[13]) },
2800 { "sr14", offsetof(CPUState, sr[14]) },
2801 { "sr15", offsetof(CPUState, sr[15]) },
2802 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00002803#elif defined(TARGET_SPARC)
2804 { "g0", offsetof(CPUState, gregs[0]) },
2805 { "g1", offsetof(CPUState, gregs[1]) },
2806 { "g2", offsetof(CPUState, gregs[2]) },
2807 { "g3", offsetof(CPUState, gregs[3]) },
2808 { "g4", offsetof(CPUState, gregs[4]) },
2809 { "g5", offsetof(CPUState, gregs[5]) },
2810 { "g6", offsetof(CPUState, gregs[6]) },
2811 { "g7", offsetof(CPUState, gregs[7]) },
2812 { "o0", 0, monitor_get_reg },
2813 { "o1", 1, monitor_get_reg },
2814 { "o2", 2, monitor_get_reg },
2815 { "o3", 3, monitor_get_reg },
2816 { "o4", 4, monitor_get_reg },
2817 { "o5", 5, monitor_get_reg },
2818 { "o6", 6, monitor_get_reg },
2819 { "o7", 7, monitor_get_reg },
2820 { "l0", 8, monitor_get_reg },
2821 { "l1", 9, monitor_get_reg },
2822 { "l2", 10, monitor_get_reg },
2823 { "l3", 11, monitor_get_reg },
2824 { "l4", 12, monitor_get_reg },
2825 { "l5", 13, monitor_get_reg },
2826 { "l6", 14, monitor_get_reg },
2827 { "l7", 15, monitor_get_reg },
2828 { "i0", 16, monitor_get_reg },
2829 { "i1", 17, monitor_get_reg },
2830 { "i2", 18, monitor_get_reg },
2831 { "i3", 19, monitor_get_reg },
2832 { "i4", 20, monitor_get_reg },
2833 { "i5", 21, monitor_get_reg },
2834 { "i6", 22, monitor_get_reg },
2835 { "i7", 23, monitor_get_reg },
2836 { "pc", offsetof(CPUState, pc) },
2837 { "npc", offsetof(CPUState, npc) },
2838 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00002839#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00002840 { "psr", 0, &monitor_get_psr, },
2841 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00002842#endif
bellarde95c8d52004-09-30 22:22:08 +00002843 { "tbr", offsetof(CPUState, tbr) },
2844 { "fsr", offsetof(CPUState, fsr) },
2845 { "f0", offsetof(CPUState, fpr[0]) },
2846 { "f1", offsetof(CPUState, fpr[1]) },
2847 { "f2", offsetof(CPUState, fpr[2]) },
2848 { "f3", offsetof(CPUState, fpr[3]) },
2849 { "f4", offsetof(CPUState, fpr[4]) },
2850 { "f5", offsetof(CPUState, fpr[5]) },
2851 { "f6", offsetof(CPUState, fpr[6]) },
2852 { "f7", offsetof(CPUState, fpr[7]) },
2853 { "f8", offsetof(CPUState, fpr[8]) },
2854 { "f9", offsetof(CPUState, fpr[9]) },
2855 { "f10", offsetof(CPUState, fpr[10]) },
2856 { "f11", offsetof(CPUState, fpr[11]) },
2857 { "f12", offsetof(CPUState, fpr[12]) },
2858 { "f13", offsetof(CPUState, fpr[13]) },
2859 { "f14", offsetof(CPUState, fpr[14]) },
2860 { "f15", offsetof(CPUState, fpr[15]) },
2861 { "f16", offsetof(CPUState, fpr[16]) },
2862 { "f17", offsetof(CPUState, fpr[17]) },
2863 { "f18", offsetof(CPUState, fpr[18]) },
2864 { "f19", offsetof(CPUState, fpr[19]) },
2865 { "f20", offsetof(CPUState, fpr[20]) },
2866 { "f21", offsetof(CPUState, fpr[21]) },
2867 { "f22", offsetof(CPUState, fpr[22]) },
2868 { "f23", offsetof(CPUState, fpr[23]) },
2869 { "f24", offsetof(CPUState, fpr[24]) },
2870 { "f25", offsetof(CPUState, fpr[25]) },
2871 { "f26", offsetof(CPUState, fpr[26]) },
2872 { "f27", offsetof(CPUState, fpr[27]) },
2873 { "f28", offsetof(CPUState, fpr[28]) },
2874 { "f29", offsetof(CPUState, fpr[29]) },
2875 { "f30", offsetof(CPUState, fpr[30]) },
2876 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00002877#ifdef TARGET_SPARC64
2878 { "f32", offsetof(CPUState, fpr[32]) },
2879 { "f34", offsetof(CPUState, fpr[34]) },
2880 { "f36", offsetof(CPUState, fpr[36]) },
2881 { "f38", offsetof(CPUState, fpr[38]) },
2882 { "f40", offsetof(CPUState, fpr[40]) },
2883 { "f42", offsetof(CPUState, fpr[42]) },
2884 { "f44", offsetof(CPUState, fpr[44]) },
2885 { "f46", offsetof(CPUState, fpr[46]) },
2886 { "f48", offsetof(CPUState, fpr[48]) },
2887 { "f50", offsetof(CPUState, fpr[50]) },
2888 { "f52", offsetof(CPUState, fpr[52]) },
2889 { "f54", offsetof(CPUState, fpr[54]) },
2890 { "f56", offsetof(CPUState, fpr[56]) },
2891 { "f58", offsetof(CPUState, fpr[58]) },
2892 { "f60", offsetof(CPUState, fpr[60]) },
2893 { "f62", offsetof(CPUState, fpr[62]) },
2894 { "asi", offsetof(CPUState, asi) },
2895 { "pstate", offsetof(CPUState, pstate) },
2896 { "cansave", offsetof(CPUState, cansave) },
2897 { "canrestore", offsetof(CPUState, canrestore) },
2898 { "otherwin", offsetof(CPUState, otherwin) },
2899 { "wstate", offsetof(CPUState, wstate) },
2900 { "cleanwin", offsetof(CPUState, cleanwin) },
2901 { "fprs", offsetof(CPUState, fprs) },
2902#endif
bellard9307c4c2004-04-04 12:57:25 +00002903#endif
2904 { NULL },
2905};
2906
aliguori376253e2009-03-05 23:01:23 +00002907static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002908{
aliguori376253e2009-03-05 23:01:23 +00002909 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002910 longjmp(expr_env, 1);
2911}
2912
Markus Armbruster09b94182010-01-20 13:07:30 +01002913/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00002914static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002915{
blueswir18662d652008-10-02 18:32:44 +00002916 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002917 void *ptr;
2918
bellard9307c4c2004-04-04 12:57:25 +00002919 for(md = monitor_defs; md->name != NULL; md++) {
2920 if (compare_cmd(name, md->name)) {
2921 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002922 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002923 } else {
bellard6a00d602005-11-21 23:25:50 +00002924 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002925 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002926 switch(md->type) {
2927 case MD_I32:
2928 *pval = *(int32_t *)ptr;
2929 break;
2930 case MD_TLONG:
2931 *pval = *(target_long *)ptr;
2932 break;
2933 default:
2934 *pval = 0;
2935 break;
2936 }
bellard9307c4c2004-04-04 12:57:25 +00002937 }
2938 return 0;
2939 }
2940 }
2941 return -1;
2942}
2943
2944static void next(void)
2945{
Blue Swirl660f11b2009-07-31 21:16:51 +00002946 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00002947 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002948 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002949 pch++;
2950 }
2951}
2952
aliguori376253e2009-03-05 23:01:23 +00002953static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00002954
aliguori376253e2009-03-05 23:01:23 +00002955static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00002956{
blueswir1c2efc952007-09-25 17:28:42 +00002957 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002958 char *p;
bellard6a00d602005-11-21 23:25:50 +00002959 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002960
2961 switch(*pch) {
2962 case '+':
2963 next();
aliguori376253e2009-03-05 23:01:23 +00002964 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002965 break;
2966 case '-':
2967 next();
aliguori376253e2009-03-05 23:01:23 +00002968 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002969 break;
2970 case '~':
2971 next();
aliguori376253e2009-03-05 23:01:23 +00002972 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00002973 break;
2974 case '(':
2975 next();
aliguori376253e2009-03-05 23:01:23 +00002976 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00002977 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00002978 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00002979 }
2980 next();
2981 break;
bellard81d09122004-07-14 17:21:37 +00002982 case '\'':
2983 pch++;
2984 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00002985 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00002986 n = *pch;
2987 pch++;
2988 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00002989 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00002990 next();
2991 break;
bellard9307c4c2004-04-04 12:57:25 +00002992 case '$':
2993 {
2994 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002995 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002996
bellard9307c4c2004-04-04 12:57:25 +00002997 pch++;
2998 q = buf;
2999 while ((*pch >= 'a' && *pch <= 'z') ||
3000 (*pch >= 'A' && *pch <= 'Z') ||
3001 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003002 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003003 if ((q - buf) < sizeof(buf) - 1)
3004 *q++ = *pch;
3005 pch++;
3006 }
blueswir1cd390082008-11-16 13:53:32 +00003007 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003008 pch++;
3009 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003010 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003011 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003012 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003013 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003014 }
3015 break;
3016 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003017 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003018 n = 0;
3019 break;
3020 default:
blueswir17743e582007-09-24 18:39:04 +00003021#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003022 n = strtoull(pch, &p, 0);
3023#else
bellard9307c4c2004-04-04 12:57:25 +00003024 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003025#endif
bellard9307c4c2004-04-04 12:57:25 +00003026 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003027 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003028 }
3029 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003030 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003031 pch++;
3032 break;
3033 }
3034 return n;
3035}
3036
3037
aliguori376253e2009-03-05 23:01:23 +00003038static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003039{
blueswir1c2efc952007-09-25 17:28:42 +00003040 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003041 int op;
ths3b46e622007-09-17 08:09:54 +00003042
aliguori376253e2009-03-05 23:01:23 +00003043 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003044 for(;;) {
3045 op = *pch;
3046 if (op != '*' && op != '/' && op != '%')
3047 break;
3048 next();
aliguori376253e2009-03-05 23:01:23 +00003049 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003050 switch(op) {
3051 default:
3052 case '*':
3053 val *= val2;
3054 break;
3055 case '/':
3056 case '%':
ths5fafdf22007-09-16 21:08:06 +00003057 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003058 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003059 if (op == '/')
3060 val /= val2;
3061 else
3062 val %= val2;
3063 break;
3064 }
3065 }
3066 return val;
3067}
3068
aliguori376253e2009-03-05 23:01:23 +00003069static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003070{
blueswir1c2efc952007-09-25 17:28:42 +00003071 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003072 int op;
bellard9307c4c2004-04-04 12:57:25 +00003073
aliguori376253e2009-03-05 23:01:23 +00003074 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003075 for(;;) {
3076 op = *pch;
3077 if (op != '&' && op != '|' && op != '^')
3078 break;
3079 next();
aliguori376253e2009-03-05 23:01:23 +00003080 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003081 switch(op) {
3082 default:
3083 case '&':
3084 val &= val2;
3085 break;
3086 case '|':
3087 val |= val2;
3088 break;
3089 case '^':
3090 val ^= val2;
3091 break;
3092 }
3093 }
3094 return val;
3095}
3096
aliguori376253e2009-03-05 23:01:23 +00003097static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003098{
blueswir1c2efc952007-09-25 17:28:42 +00003099 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003100 int op;
bellard9307c4c2004-04-04 12:57:25 +00003101
aliguori376253e2009-03-05 23:01:23 +00003102 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003103 for(;;) {
3104 op = *pch;
3105 if (op != '+' && op != '-')
3106 break;
3107 next();
aliguori376253e2009-03-05 23:01:23 +00003108 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003109 if (op == '+')
3110 val += val2;
3111 else
3112 val -= val2;
3113 }
3114 return val;
3115}
3116
aliguori376253e2009-03-05 23:01:23 +00003117static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003118{
3119 pch = *pp;
3120 if (setjmp(expr_env)) {
3121 *pp = pch;
3122 return -1;
3123 }
blueswir1cd390082008-11-16 13:53:32 +00003124 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003125 pch++;
aliguori376253e2009-03-05 23:01:23 +00003126 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003127 *pp = pch;
3128 return 0;
3129}
3130
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003131static int get_double(Monitor *mon, double *pval, const char **pp)
3132{
3133 const char *p = *pp;
3134 char *tailp;
3135 double d;
3136
3137 d = strtod(p, &tailp);
3138 if (tailp == p) {
3139 monitor_printf(mon, "Number expected\n");
3140 return -1;
3141 }
3142 if (d != d || d - d != 0) {
3143 /* NaN or infinity */
3144 monitor_printf(mon, "Bad number\n");
3145 return -1;
3146 }
3147 *pval = d;
3148 *pp = tailp;
3149 return 0;
3150}
3151
bellard9307c4c2004-04-04 12:57:25 +00003152static int get_str(char *buf, int buf_size, const char **pp)
3153{
3154 const char *p;
3155 char *q;
3156 int c;
3157
bellard81d09122004-07-14 17:21:37 +00003158 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003159 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003160 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003161 p++;
3162 if (*p == '\0') {
3163 fail:
bellard81d09122004-07-14 17:21:37 +00003164 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003165 *pp = p;
3166 return -1;
3167 }
bellard9307c4c2004-04-04 12:57:25 +00003168 if (*p == '\"') {
3169 p++;
3170 while (*p != '\0' && *p != '\"') {
3171 if (*p == '\\') {
3172 p++;
3173 c = *p++;
3174 switch(c) {
3175 case 'n':
3176 c = '\n';
3177 break;
3178 case 'r':
3179 c = '\r';
3180 break;
3181 case '\\':
3182 case '\'':
3183 case '\"':
3184 break;
3185 default:
3186 qemu_printf("unsupported escape code: '\\%c'\n", c);
3187 goto fail;
3188 }
3189 if ((q - buf) < buf_size - 1) {
3190 *q++ = c;
3191 }
3192 } else {
3193 if ((q - buf) < buf_size - 1) {
3194 *q++ = *p;
3195 }
3196 p++;
3197 }
3198 }
3199 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003200 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003201 goto fail;
3202 }
3203 p++;
3204 } else {
blueswir1cd390082008-11-16 13:53:32 +00003205 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003206 if ((q - buf) < buf_size - 1) {
3207 *q++ = *p;
3208 }
3209 p++;
3210 }
bellard9307c4c2004-04-04 12:57:25 +00003211 }
bellard81d09122004-07-14 17:21:37 +00003212 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003213 *pp = p;
3214 return 0;
3215}
3216
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003217/*
3218 * Store the command-name in cmdname, and return a pointer to
3219 * the remaining of the command string.
3220 */
3221static const char *get_command_name(const char *cmdline,
3222 char *cmdname, size_t nlen)
3223{
3224 size_t len;
3225 const char *p, *pstart;
3226
3227 p = cmdline;
3228 while (qemu_isspace(*p))
3229 p++;
3230 if (*p == '\0')
3231 return NULL;
3232 pstart = p;
3233 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3234 p++;
3235 len = p - pstart;
3236 if (len > nlen - 1)
3237 len = nlen - 1;
3238 memcpy(cmdname, pstart, len);
3239 cmdname[len] = '\0';
3240 return p;
3241}
3242
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003243/**
3244 * Read key of 'type' into 'key' and return the current
3245 * 'type' pointer.
3246 */
3247static char *key_get_info(const char *type, char **key)
3248{
3249 size_t len;
3250 char *p, *str;
3251
3252 if (*type == ',')
3253 type++;
3254
3255 p = strchr(type, ':');
3256 if (!p) {
3257 *key = NULL;
3258 return NULL;
3259 }
3260 len = p - type;
3261
3262 str = qemu_malloc(len + 1);
3263 memcpy(str, type, len);
3264 str[len] = '\0';
3265
3266 *key = str;
3267 return ++p;
3268}
3269
bellard9307c4c2004-04-04 12:57:25 +00003270static int default_fmt_format = 'x';
3271static int default_fmt_size = 4;
3272
3273#define MAX_ARGS 16
3274
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003275static int is_valid_option(const char *c, const char *typestr)
3276{
3277 char option[3];
3278
3279 option[0] = '-';
3280 option[1] = *c;
3281 option[2] = '\0';
3282
3283 typestr = strstr(typestr, option);
3284 return (typestr != NULL);
3285}
3286
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003287static const mon_cmd_t *monitor_find_command(const char *cmdname)
3288{
3289 const mon_cmd_t *cmd;
3290
3291 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3292 if (compare_cmd(cmdname, cmd->name)) {
3293 return cmd;
3294 }
3295 }
3296
3297 return NULL;
3298}
3299
Anthony Liguoric227f092009-10-01 16:12:16 -05003300static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003301 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003302 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003303{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003304 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003305 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003306 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003307 char cmdname[256];
3308 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003309 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003310
3311#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003312 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003313#endif
ths3b46e622007-09-17 08:09:54 +00003314
bellard9307c4c2004-04-04 12:57:25 +00003315 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003316 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3317 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003318 return NULL;
ths3b46e622007-09-17 08:09:54 +00003319
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003320 cmd = monitor_find_command(cmdname);
3321 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003322 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003323 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003324 }
bellard9307c4c2004-04-04 12:57:25 +00003325
bellard9307c4c2004-04-04 12:57:25 +00003326 /* parse the parameters */
3327 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003328 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003329 typestr = key_get_info(typestr, &key);
3330 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003331 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003332 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003333 typestr++;
3334 switch(c) {
3335 case 'F':
bellard81d09122004-07-14 17:21:37 +00003336 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003337 case 's':
3338 {
3339 int ret;
ths3b46e622007-09-17 08:09:54 +00003340
blueswir1cd390082008-11-16 13:53:32 +00003341 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003342 p++;
3343 if (*typestr == '?') {
3344 typestr++;
3345 if (*p == '\0') {
3346 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003347 break;
bellard9307c4c2004-04-04 12:57:25 +00003348 }
3349 }
3350 ret = get_str(buf, sizeof(buf), &p);
3351 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003352 switch(c) {
3353 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003354 monitor_printf(mon, "%s: filename expected\n",
3355 cmdname);
bellard81d09122004-07-14 17:21:37 +00003356 break;
3357 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003358 monitor_printf(mon, "%s: block device name expected\n",
3359 cmdname);
bellard81d09122004-07-14 17:21:37 +00003360 break;
3361 default:
aliguori376253e2009-03-05 23:01:23 +00003362 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003363 break;
3364 }
bellard9307c4c2004-04-04 12:57:25 +00003365 goto fail;
3366 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003367 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003368 }
3369 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003370 case 'O':
3371 {
3372 QemuOptsList *opts_list;
3373 QemuOpts *opts;
3374
3375 opts_list = qemu_find_opts(key);
3376 if (!opts_list || opts_list->desc->name) {
3377 goto bad_type;
3378 }
3379 while (qemu_isspace(*p)) {
3380 p++;
3381 }
3382 if (!*p)
3383 break;
3384 if (get_str(buf, sizeof(buf), &p) < 0) {
3385 goto fail;
3386 }
3387 opts = qemu_opts_parse(opts_list, buf, 1);
3388 if (!opts) {
3389 goto fail;
3390 }
3391 qemu_opts_to_qdict(opts, qdict);
3392 qemu_opts_del(opts);
3393 }
3394 break;
bellard9307c4c2004-04-04 12:57:25 +00003395 case '/':
3396 {
3397 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003398
blueswir1cd390082008-11-16 13:53:32 +00003399 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003400 p++;
3401 if (*p == '/') {
3402 /* format found */
3403 p++;
3404 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003405 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003406 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003407 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003408 count = count * 10 + (*p - '0');
3409 p++;
3410 }
3411 }
3412 size = -1;
3413 format = -1;
3414 for(;;) {
3415 switch(*p) {
3416 case 'o':
3417 case 'd':
3418 case 'u':
3419 case 'x':
3420 case 'i':
3421 case 'c':
3422 format = *p++;
3423 break;
3424 case 'b':
3425 size = 1;
3426 p++;
3427 break;
3428 case 'h':
3429 size = 2;
3430 p++;
3431 break;
3432 case 'w':
3433 size = 4;
3434 p++;
3435 break;
3436 case 'g':
3437 case 'L':
3438 size = 8;
3439 p++;
3440 break;
3441 default:
3442 goto next;
3443 }
3444 }
3445 next:
blueswir1cd390082008-11-16 13:53:32 +00003446 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003447 monitor_printf(mon, "invalid char in format: '%c'\n",
3448 *p);
bellard9307c4c2004-04-04 12:57:25 +00003449 goto fail;
3450 }
bellard9307c4c2004-04-04 12:57:25 +00003451 if (format < 0)
3452 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003453 if (format != 'i') {
3454 /* for 'i', not specifying a size gives -1 as size */
3455 if (size < 0)
3456 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003457 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003458 }
bellard9307c4c2004-04-04 12:57:25 +00003459 default_fmt_format = format;
3460 } else {
3461 count = 1;
3462 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003463 if (format != 'i') {
3464 size = default_fmt_size;
3465 } else {
3466 size = -1;
3467 }
bellard9307c4c2004-04-04 12:57:25 +00003468 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003469 qdict_put(qdict, "count", qint_from_int(count));
3470 qdict_put(qdict, "format", qint_from_int(format));
3471 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003472 }
3473 break;
3474 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003475 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003476 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003477 {
blueswir1c2efc952007-09-25 17:28:42 +00003478 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003479
blueswir1cd390082008-11-16 13:53:32 +00003480 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003481 p++;
bellard34405572004-06-08 00:55:58 +00003482 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003483 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003484 if (*p == '\0') {
3485 typestr++;
3486 break;
3487 }
bellard34405572004-06-08 00:55:58 +00003488 } else {
3489 if (*p == '.') {
3490 p++;
blueswir1cd390082008-11-16 13:53:32 +00003491 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003492 p++;
bellard34405572004-06-08 00:55:58 +00003493 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003494 typestr++;
3495 break;
bellard34405572004-06-08 00:55:58 +00003496 }
3497 }
bellard13224a82006-07-14 22:03:35 +00003498 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003499 }
aliguori376253e2009-03-05 23:01:23 +00003500 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003501 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003502 /* Check if 'i' is greater than 32-bit */
3503 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3504 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3505 monitor_printf(mon, "integer is for 32-bit values\n");
3506 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003507 } else if (c == 'M') {
3508 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003509 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003510 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003511 }
3512 break;
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003513 case 'f':
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003514 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003515 {
3516 double val;
3517
3518 while (qemu_isspace(*p))
3519 p++;
3520 if (*typestr == '?') {
3521 typestr++;
3522 if (*p == '\0') {
3523 break;
3524 }
3525 }
3526 if (get_double(mon, &val, &p) < 0) {
3527 goto fail;
3528 }
Markus Armbrusteree9545d2010-03-26 09:07:08 +01003529 if (c == 'f' && *p) {
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003530 switch (*p) {
3531 case 'K': case 'k':
3532 val *= 1 << 10; p++; break;
3533 case 'M': case 'm':
3534 val *= 1 << 20; p++; break;
3535 case 'G': case 'g':
3536 val *= 1 << 30; p++; break;
3537 }
3538 }
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003539 if (c == 'T' && p[0] && p[1] == 's') {
3540 switch (*p) {
3541 case 'm':
3542 val /= 1e3; p += 2; break;
3543 case 'u':
3544 val /= 1e6; p += 2; break;
3545 case 'n':
3546 val /= 1e9; p += 2; break;
3547 }
3548 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003549 if (*p && !qemu_isspace(*p)) {
3550 monitor_printf(mon, "Unknown unit suffix\n");
3551 goto fail;
3552 }
3553 qdict_put(qdict, key, qfloat_from_double(val));
3554 }
3555 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01003556 case 'b':
3557 {
3558 const char *beg;
3559 int val;
3560
3561 while (qemu_isspace(*p)) {
3562 p++;
3563 }
3564 beg = p;
3565 while (qemu_isgraph(*p)) {
3566 p++;
3567 }
3568 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3569 val = 1;
3570 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3571 val = 0;
3572 } else {
3573 monitor_printf(mon, "Expected 'on' or 'off'\n");
3574 goto fail;
3575 }
3576 qdict_put(qdict, key, qbool_from_int(val));
3577 }
3578 break;
bellard9307c4c2004-04-04 12:57:25 +00003579 case '-':
3580 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003581 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003582 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00003583 /* option */
ths3b46e622007-09-17 08:09:54 +00003584
bellard9307c4c2004-04-04 12:57:25 +00003585 c = *typestr++;
3586 if (c == '\0')
3587 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00003588 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003589 p++;
bellard9307c4c2004-04-04 12:57:25 +00003590 if (*p == '-') {
3591 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003592 if(c != *p) {
3593 if(!is_valid_option(p, typestr)) {
3594
3595 monitor_printf(mon, "%s: unsupported option -%c\n",
3596 cmdname, *p);
3597 goto fail;
3598 } else {
3599 skip_key = 1;
3600 }
bellard9307c4c2004-04-04 12:57:25 +00003601 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003602 if(skip_key) {
3603 p = tmp;
3604 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003605 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003606 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03003607 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003608 }
bellard9307c4c2004-04-04 12:57:25 +00003609 }
bellard9307c4c2004-04-04 12:57:25 +00003610 }
3611 break;
3612 default:
3613 bad_type:
aliguori376253e2009-03-05 23:01:23 +00003614 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00003615 goto fail;
3616 }
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003617 qemu_free(key);
3618 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00003619 }
3620 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00003621 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003622 p++;
3623 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00003624 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3625 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00003626 goto fail;
3627 }
3628
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003629 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02003630
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003631fail:
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003632 qemu_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003633 return NULL;
3634}
3635
Markus Armbrusterd6f46832010-02-17 10:52:26 +01003636void monitor_set_error(Monitor *mon, QError *qerror)
3637{
3638 /* report only the first error */
3639 if (!mon->error) {
3640 mon->error = qerror;
3641 } else {
3642 MON_DEBUG("Additional error report at %s:%d\n",
3643 qerror->file, qerror->linenr);
3644 QDECREF(qerror);
3645 }
3646}
3647
Adam Litke940cc302010-01-25 12:18:44 -06003648static int is_async_return(const QObject *data)
3649{
Luiz Capitulino82617d72010-01-27 18:01:17 -02003650 if (data && qobject_type(data) == QTYPE_QDICT) {
3651 return qdict_haskey(qobject_to_qdict(data), "__mon_async");
3652 }
3653
3654 return 0;
Adam Litke940cc302010-01-25 12:18:44 -06003655}
3656
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003657static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3658{
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003659 if (monitor_ctrl_mode(mon)) {
3660 if (ret && !monitor_has_error(mon)) {
3661 /*
3662 * If it returns failure, it must have passed on error.
3663 *
3664 * Action: Report an internal error to the client if in QMP.
3665 */
Markus Armbrusterab5b0272010-03-02 18:15:09 +01003666 qerror_report(QERR_UNDEFINED_ERROR);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003667 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3668 cmd->name);
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003669 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003670
3671#ifdef CONFIG_DEBUG_MONITOR
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003672 if (!ret && monitor_has_error(mon)) {
3673 /*
3674 * If it returns success, it must not have passed an error.
3675 *
3676 * Action: Report the passed error to the client.
3677 */
3678 MON_DEBUG("command '%s' returned success but passed an error\n",
3679 cmd->name);
3680 }
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003681
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003682 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3683 /*
3684 * Handlers should not call Monitor print functions.
3685 *
3686 * Action: Ignore them in QMP.
3687 *
3688 * (XXX: we don't check any 'info' or 'query' command here
3689 * because the user print function _is_ called by do_info(), hence
3690 * we will trigger this check. This problem will go away when we
3691 * make 'query' commands real and kill do_info())
3692 */
3693 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3694 cmd->name, mon_print_count_get(mon));
3695 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003696#endif
Markus Armbrustercde0fc72010-03-02 14:56:34 +01003697 } else {
3698 assert(!monitor_has_error(mon));
3699 QDECREF(mon->error);
3700 mon->error = NULL;
3701 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003702}
3703
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003704static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3705 const QDict *params)
3706{
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003707 int ret;
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003708 QObject *data = NULL;
3709
Luiz Capitulino10e4f602010-02-10 23:50:06 -02003710 mon_print_count_init(mon);
3711
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02003712 ret = cmd->mhandler.cmd_new(mon, params, &data);
3713 handler_audit(mon, cmd, ret);
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003714
Adam Litke940cc302010-01-25 12:18:44 -06003715 if (is_async_return(data)) {
3716 /*
3717 * Asynchronous commands have no initial return data but they can
3718 * generate errors. Data is returned via the async completion handler.
3719 */
3720 if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) {
3721 monitor_protocol_emitter(mon, NULL);
3722 }
3723 } else if (monitor_ctrl_mode(mon)) {
Luiz Capitulino25b422e2009-11-26 22:58:59 -02003724 /* Monitor Protocol */
3725 monitor_protocol_emitter(mon, data);
3726 } else {
3727 /* User Protocol */
3728 if (data)
3729 cmd->user_print(mon, data);
3730 }
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003731
3732 qobject_decref(data);
3733}
3734
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02003735static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003736{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003737 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05003738 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003739
3740 qdict = qdict_new();
3741
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03003742 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003743 if (!cmd)
3744 goto out;
3745
Adam Litke940cc302010-01-25 12:18:44 -06003746 if (monitor_handler_is_async(cmd)) {
3747 user_async_cmd_handler(mon, cmd, qdict);
3748 } else if (monitor_handler_ported(cmd)) {
Luiz Capitulino99e2fc12009-11-26 22:58:53 -02003749 monitor_call_handler(mon, cmd, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03003750 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03003751 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003752 }
3753
Luiz Capitulino13917be2009-10-07 13:41:54 -03003754out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003755 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00003756}
3757
bellard81d09122004-07-14 17:21:37 +00003758static void cmd_completion(const char *name, const char *list)
3759{
3760 const char *p, *pstart;
3761 char cmd[128];
3762 int len;
3763
3764 p = list;
3765 for(;;) {
3766 pstart = p;
3767 p = strchr(p, '|');
3768 if (!p)
3769 p = pstart + strlen(pstart);
3770 len = p - pstart;
3771 if (len > sizeof(cmd) - 2)
3772 len = sizeof(cmd) - 2;
3773 memcpy(cmd, pstart, len);
3774 cmd[len] = '\0';
3775 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00003776 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00003777 }
3778 if (*p == '\0')
3779 break;
3780 p++;
3781 }
3782}
3783
3784static void file_completion(const char *input)
3785{
3786 DIR *ffs;
3787 struct dirent *d;
3788 char path[1024];
3789 char file[1024], file_prefix[1024];
3790 int input_path_len;
3791 const char *p;
3792
ths5fafdf22007-09-16 21:08:06 +00003793 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00003794 if (!p) {
3795 input_path_len = 0;
3796 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00003797 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00003798 } else {
3799 input_path_len = p - input + 1;
3800 memcpy(path, input, input_path_len);
3801 if (input_path_len > sizeof(path) - 1)
3802 input_path_len = sizeof(path) - 1;
3803 path[input_path_len] = '\0';
3804 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3805 }
3806#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00003807 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3808 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00003809#endif
3810 ffs = opendir(path);
3811 if (!ffs)
3812 return;
3813 for(;;) {
3814 struct stat sb;
3815 d = readdir(ffs);
3816 if (!d)
3817 break;
3818 if (strstart(d->d_name, file_prefix, NULL)) {
3819 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00003820 if (input_path_len < sizeof(file))
3821 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3822 d->d_name);
bellard81d09122004-07-14 17:21:37 +00003823 /* stat the file to find out if it's a directory.
3824 * In that case add a slash to speed up typing long paths
3825 */
3826 stat(file, &sb);
3827 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00003828 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00003829 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00003830 }
3831 }
3832 closedir(ffs);
3833}
3834
aliguori51de9762009-03-05 23:00:43 +00003835static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00003836{
aliguori51de9762009-03-05 23:00:43 +00003837 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00003838 const char *input = opaque;
3839
3840 if (input[0] == '\0' ||
3841 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00003842 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00003843 }
3844}
3845
3846/* NOTE: this parser is an approximate form of the real command parser */
3847static void parse_cmdline(const char *cmdline,
3848 int *pnb_args, char **args)
3849{
3850 const char *p;
3851 int nb_args, ret;
3852 char buf[1024];
3853
3854 p = cmdline;
3855 nb_args = 0;
3856 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00003857 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00003858 p++;
3859 if (*p == '\0')
3860 break;
3861 if (nb_args >= MAX_ARGS)
3862 break;
3863 ret = get_str(buf, sizeof(buf), &p);
3864 args[nb_args] = qemu_strdup(buf);
3865 nb_args++;
3866 if (ret < 0)
3867 break;
3868 }
3869 *pnb_args = nb_args;
3870}
3871
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003872static const char *next_arg_type(const char *typestr)
3873{
3874 const char *p = strchr(typestr, ':');
3875 return (p != NULL ? ++p : typestr);
3876}
3877
aliguori4c36ba32009-03-05 23:01:37 +00003878static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00003879{
3880 const char *cmdname;
3881 char *args[MAX_ARGS];
3882 int nb_args, i, len;
3883 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05003884 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00003885 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00003886
3887 parse_cmdline(cmdline, &nb_args, args);
3888#ifdef DEBUG_COMPLETION
3889 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00003890 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00003891 }
3892#endif
3893
3894 /* if the line ends with a space, it means we want to complete the
3895 next arg */
3896 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00003897 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02003898 if (nb_args >= MAX_ARGS) {
3899 goto cleanup;
3900 }
bellard81d09122004-07-14 17:21:37 +00003901 args[nb_args++] = qemu_strdup("");
3902 }
3903 if (nb_args <= 1) {
3904 /* command completion */
3905 if (nb_args == 0)
3906 cmdname = "";
3907 else
3908 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00003909 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00003910 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00003911 cmd_completion(cmdname, cmd->name);
3912 }
3913 } else {
3914 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02003915 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3916 if (compare_cmd(args[0], cmd->name)) {
3917 break;
3918 }
bellard81d09122004-07-14 17:21:37 +00003919 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003920 if (!cmd->name) {
3921 goto cleanup;
3922 }
3923
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003924 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00003925 for(i = 0; i < nb_args - 2; i++) {
3926 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003927 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003928 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003929 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00003930 }
3931 }
3932 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00003933 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02003934 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00003935 }
bellard81d09122004-07-14 17:21:37 +00003936 switch(*ptype) {
3937 case 'F':
3938 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00003939 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003940 file_completion(str);
3941 break;
3942 case 'B':
3943 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00003944 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00003945 bdrv_iterate(block_completion_it, (void *)str);
3946 break;
bellard7fe48482004-10-09 18:08:01 +00003947 case 's':
3948 /* XXX: more generic ? */
3949 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00003950 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00003951 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3952 cmd_completion(str, cmd->name);
3953 }
bellard64866c32006-05-07 18:03:31 +00003954 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00003955 char *sep = strrchr(str, '-');
3956 if (sep)
3957 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00003958 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00003959 for(key = key_defs; key->name != NULL; key++) {
3960 cmd_completion(str, key->name);
3961 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02003962 } else if (!strcmp(cmd->name, "help|?")) {
3963 readline_set_completion_index(cur_mon->rs, strlen(str));
3964 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3965 cmd_completion(str, cmd->name);
3966 }
bellard7fe48482004-10-09 18:08:01 +00003967 }
3968 break;
bellard81d09122004-07-14 17:21:37 +00003969 default:
3970 break;
3971 }
3972 }
Jan Kiszka03a63482010-06-16 00:38:33 +02003973
3974cleanup:
3975 for (i = 0; i < nb_args; i++) {
bellard81d09122004-07-14 17:21:37 +00003976 qemu_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02003977 }
bellard81d09122004-07-14 17:21:37 +00003978}
3979
aliguori731b0362009-03-05 23:01:42 +00003980static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00003981{
aliguori731b0362009-03-05 23:01:42 +00003982 Monitor *mon = opaque;
3983
Jan Kiszkac62313b2009-12-04 14:05:29 +01003984 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00003985}
3986
Luiz Capitulino09069b12010-02-04 18:10:06 -02003987static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
3988{
3989 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
3990 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
3991}
3992
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03003993/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03003994 * Argument validation rules:
3995 *
3996 * 1. The argument must exist in cmd_args qdict
3997 * 2. The argument type must be the expected one
3998 *
3999 * Special case: If the argument doesn't exist in cmd_args and
4000 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4001 * checking is skipped for it.
4002 */
4003static int check_client_args_type(const QDict *client_args,
4004 const QDict *cmd_args, int flags)
4005{
4006 const QDictEntry *ent;
4007
4008 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4009 QObject *obj;
4010 QString *arg_type;
4011 const QObject *client_arg = qdict_entry_value(ent);
4012 const char *client_arg_name = qdict_entry_key(ent);
4013
4014 obj = qdict_get(cmd_args, client_arg_name);
4015 if (!obj) {
4016 if (flags & QMP_ACCEPT_UNKNOWNS) {
4017 /* handler accepts unknowns */
4018 continue;
4019 }
4020 /* client arg doesn't exist */
4021 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4022 return -1;
4023 }
4024
4025 arg_type = qobject_to_qstring(obj);
4026 assert(arg_type != NULL);
4027
4028 /* check if argument's type is correct */
4029 switch (qstring_get_str(arg_type)[0]) {
4030 case 'F':
4031 case 'B':
4032 case 's':
4033 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4034 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4035 "string");
4036 return -1;
4037 }
4038 break;
4039 case 'i':
4040 case 'l':
4041 case 'M':
4042 if (qobject_type(client_arg) != QTYPE_QINT) {
4043 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4044 "int");
4045 return -1;
4046 }
4047 break;
4048 case 'f':
4049 case 'T':
4050 if (qobject_type(client_arg) != QTYPE_QINT &&
4051 qobject_type(client_arg) != QTYPE_QFLOAT) {
4052 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4053 "number");
4054 return -1;
4055 }
4056 break;
4057 case 'b':
4058 case '-':
4059 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4060 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4061 "bool");
4062 return -1;
4063 }
4064 break;
4065 case 'O':
4066 assert(flags & QMP_ACCEPT_UNKNOWNS);
4067 break;
4068 case '/':
4069 case '.':
4070 /*
4071 * These types are not supported by QMP and thus are not
4072 * handled here. Fall through.
4073 */
4074 default:
4075 abort();
4076 }
4077 }
4078
4079 return 0;
4080}
4081
4082/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004083 * - Check if the client has passed all mandatory args
4084 * - Set special flags for argument validation
4085 */
4086static int check_mandatory_args(const QDict *cmd_args,
4087 const QDict *client_args, int *flags)
4088{
4089 const QDictEntry *ent;
4090
4091 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4092 const char *cmd_arg_name = qdict_entry_key(ent);
4093 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4094 assert(type != NULL);
4095
4096 if (qstring_get_str(type)[0] == 'O') {
4097 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4098 *flags |= QMP_ACCEPT_UNKNOWNS;
4099 } else if (qstring_get_str(type)[0] != '-' &&
4100 qstring_get_str(type)[1] != '?' &&
4101 !qdict_haskey(client_args, cmd_arg_name)) {
4102 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4103 return -1;
4104 }
4105 }
4106
4107 return 0;
4108}
4109
4110static QDict *qdict_from_args_type(const char *args_type)
4111{
4112 int i;
4113 QDict *qdict;
4114 QString *key, *type, *cur_qs;
4115
4116 assert(args_type != NULL);
4117
4118 qdict = qdict_new();
4119
4120 if (args_type == NULL || args_type[0] == '\0') {
4121 /* no args, empty qdict */
4122 goto out;
4123 }
4124
4125 key = qstring_new();
4126 type = qstring_new();
4127
4128 cur_qs = key;
4129
4130 for (i = 0;; i++) {
4131 switch (args_type[i]) {
4132 case ',':
4133 case '\0':
4134 qdict_put(qdict, qstring_get_str(key), type);
4135 QDECREF(key);
4136 if (args_type[i] == '\0') {
4137 goto out;
4138 }
4139 type = qstring_new(); /* qdict has ref */
4140 cur_qs = key = qstring_new();
4141 break;
4142 case ':':
4143 cur_qs = type;
4144 break;
4145 default:
4146 qstring_append_chr(cur_qs, args_type[i]);
4147 break;
4148 }
4149 }
4150
4151out:
4152 return qdict;
4153}
4154
4155/*
4156 * Client argument checking rules:
4157 *
4158 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004159 * 2. Each argument provided by the client must be expected
4160 * 3. Each argument provided by the client must have the type expected
4161 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004162 */
4163static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4164{
4165 int flags, err;
4166 QDict *cmd_args;
4167
4168 cmd_args = qdict_from_args_type(cmd->args_type);
4169
4170 flags = 0;
4171 err = check_mandatory_args(cmd_args, client_args, &flags);
4172 if (err) {
4173 goto out;
4174 }
4175
Luiz Capitulino4af91932010-06-22 11:44:05 -03004176 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004177
4178out:
4179 QDECREF(cmd_args);
4180 return err;
4181}
4182
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004183/*
4184 * Input object checking rules
4185 *
4186 * 1. Input object must be a dict
4187 * 2. The "execute" key must exist
4188 * 3. The "execute" key must be a string
4189 * 4. If the "arguments" key exists, it must be a dict
4190 * 5. If the "id" key exists, it can be anything (ie. json-value)
4191 * 6. Any argument not listed above is considered invalid
4192 */
4193static QDict *qmp_check_input_obj(QObject *input_obj)
4194{
4195 const QDictEntry *ent;
4196 int has_exec_key = 0;
4197 QDict *input_dict;
4198
4199 if (qobject_type(input_obj) != QTYPE_QDICT) {
4200 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4201 return NULL;
4202 }
4203
4204 input_dict = qobject_to_qdict(input_obj);
4205
4206 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4207 const char *arg_name = qdict_entry_key(ent);
4208 const QObject *arg_obj = qdict_entry_value(ent);
4209
4210 if (!strcmp(arg_name, "execute")) {
4211 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4212 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4213 "string");
4214 return NULL;
4215 }
4216 has_exec_key = 1;
4217 } else if (!strcmp(arg_name, "arguments")) {
4218 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4219 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4220 "object");
4221 return NULL;
4222 }
4223 } else if (!strcmp(arg_name, "id")) {
4224 /* FIXME: check duplicated IDs for async commands */
4225 } else {
4226 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4227 return NULL;
4228 }
4229 }
4230
4231 if (!has_exec_key) {
4232 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4233 return NULL;
4234 }
4235
4236 return input_dict;
4237}
4238
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004239static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4240{
4241 int err;
4242 QObject *obj;
4243 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004244 const mon_cmd_t *cmd;
4245 Monitor *mon = cur_mon;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004246 const char *cmd_name, *info_item;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004247
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004248 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004249
4250 obj = json_parser_parse(tokens, NULL);
4251 if (!obj) {
4252 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004253 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004254 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004255 }
4256
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004257 input = qmp_check_input_obj(obj);
4258 if (!input) {
4259 qobject_decref(obj);
4260 goto err_out;
4261 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004262
4263 mon->mc->id = qdict_get(input, "id");
4264 qobject_incref(mon->mc->id);
4265
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004266 cmd_name = qdict_get_str(input, "execute");
Luiz Capitulino09069b12010-02-04 18:10:06 -02004267 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004268 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004269 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004270 }
4271
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004272 /*
4273 * XXX: We need this special case until we get info handlers
4274 * converted into 'query-' commands
4275 */
4276 if (compare_cmd(cmd_name, "info")) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004277 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004278 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004279 } else if (strstart(cmd_name, "query-", &info_item)) {
4280 cmd = monitor_find_command("info");
4281 qdict_put_obj(input, "arguments",
4282 qobject_from_jsonf("{ 'item': %s }", info_item));
4283 } else {
4284 cmd = monitor_find_command(cmd_name);
Jan Kiszkaa6c4d362010-06-28 18:27:47 +02004285 if (!cmd || !monitor_handler_ported(cmd)
4286 || monitor_cmd_user_only(cmd)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004287 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004288 goto err_out;
Luiz Capitulino5e23f482009-11-26 22:59:02 -02004289 }
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004290 }
4291
4292 obj = qdict_get(input, "arguments");
4293 if (!obj) {
4294 args = qdict_new();
4295 } else {
4296 args = qobject_to_qdict(obj);
4297 QINCREF(args);
4298 }
4299
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004300 err = qmp_check_client_args(cmd, args);
4301 if (err < 0) {
4302 goto err_out;
4303 }
4304
Adam Litke940cc302010-01-25 12:18:44 -06004305 if (monitor_handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004306 err = qmp_async_cmd_handler(mon, cmd, args);
4307 if (err) {
4308 /* emit the error response */
4309 goto err_out;
4310 }
Adam Litke940cc302010-01-25 12:18:44 -06004311 } else {
4312 monitor_call_handler(mon, cmd, args);
4313 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004314
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004315 goto out;
4316
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004317err_out:
4318 monitor_protocol_emitter(mon, NULL);
4319out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004320 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004321 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004322}
4323
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004324/**
4325 * monitor_control_read(): Read and handle QMP input
4326 */
4327static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4328{
4329 Monitor *old_mon = cur_mon;
4330
4331 cur_mon = opaque;
4332
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004333 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004334
4335 cur_mon = old_mon;
4336}
4337
aliguori731b0362009-03-05 23:01:42 +00004338static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004339{
aliguori731b0362009-03-05 23:01:42 +00004340 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004341 int i;
aliguori376253e2009-03-05 23:01:23 +00004342
aliguori731b0362009-03-05 23:01:42 +00004343 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004344
aliguoricde76ee2009-03-05 23:01:51 +00004345 if (cur_mon->rs) {
4346 for (i = 0; i < size; i++)
4347 readline_handle_byte(cur_mon->rs, buf[i]);
4348 } else {
4349 if (size == 0 || buf[size - 1] != 0)
4350 monitor_printf(cur_mon, "corrupted command\n");
4351 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004352 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004353 }
aliguori731b0362009-03-05 23:01:42 +00004354
4355 cur_mon = old_mon;
4356}
aliguorid8f44602008-10-06 13:52:44 +00004357
aliguori376253e2009-03-05 23:01:23 +00004358static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004359{
aliguori731b0362009-03-05 23:01:42 +00004360 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004361 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004362 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004363}
4364
aliguoricde76ee2009-03-05 23:01:51 +00004365int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004366{
aliguoricde76ee2009-03-05 23:01:51 +00004367 if (!mon->rs)
4368 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004369 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004370 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004371}
4372
aliguori376253e2009-03-05 23:01:23 +00004373void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004374{
aliguoricde76ee2009-03-05 23:01:51 +00004375 if (!mon->rs)
4376 return;
aliguori731b0362009-03-05 23:01:42 +00004377 if (--mon->suspend_cnt == 0)
4378 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004379}
4380
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004381static QObject *get_qmp_greeting(void)
4382{
4383 QObject *ver;
4384
4385 do_info_version(NULL, &ver);
4386 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4387}
4388
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004389/**
4390 * monitor_control_event(): Print QMP gretting
4391 */
4392static void monitor_control_event(void *opaque, int event)
4393{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004394 QObject *data;
4395 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004396
Luiz Capitulino47116d12010-02-08 17:01:30 -02004397 switch (event) {
4398 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004399 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004400 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004401 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004402 monitor_json_emitter(mon, data);
4403 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004404 break;
4405 case CHR_EVENT_CLOSED:
4406 json_message_parser_destroy(&mon->mc->parser);
4407 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004408 }
4409}
4410
aliguori731b0362009-03-05 23:01:42 +00004411static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004412{
aliguori376253e2009-03-05 23:01:23 +00004413 Monitor *mon = opaque;
4414
aliguori2724b182009-03-05 23:01:47 +00004415 switch (event) {
4416 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004417 mon->mux_out = 0;
4418 if (mon->reset_seen) {
4419 readline_restart(mon->rs);
4420 monitor_resume(mon);
4421 monitor_flush(mon);
4422 } else {
4423 mon->suspend_cnt = 0;
4424 }
aliguori2724b182009-03-05 23:01:47 +00004425 break;
ths86e94de2007-01-05 22:01:59 +00004426
aliguori2724b182009-03-05 23:01:47 +00004427 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004428 if (mon->reset_seen) {
4429 if (mon->suspend_cnt == 0) {
4430 monitor_printf(mon, "\n");
4431 }
4432 monitor_flush(mon);
4433 monitor_suspend(mon);
4434 } else {
4435 mon->suspend_cnt++;
4436 }
4437 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004438 break;
4439
Amit Shahb6b8df52009-10-07 18:31:16 +05304440 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004441 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4442 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004443 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004444 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004445 }
4446 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004447 break;
4448 }
ths86e94de2007-01-05 22:01:59 +00004449}
4450
aliguori76655d62009-03-06 20:27:37 +00004451
4452/*
4453 * Local variables:
4454 * c-indent-level: 4
4455 * c-basic-offset: 4
4456 * tab-width: 8
4457 * End:
4458 */
4459
aliguori731b0362009-03-05 23:01:42 +00004460void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004461{
aliguori731b0362009-03-05 23:01:42 +00004462 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004463 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004464
4465 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00004466 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004467 is_first_init = 0;
4468 }
aliguori87127162009-03-05 23:01:29 +00004469
4470 mon = qemu_mallocz(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004471
aliguori87127162009-03-05 23:01:29 +00004472 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004473 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004474 if (flags & MONITOR_USE_READLINE) {
4475 mon->rs = readline_init(mon, monitor_find_completion);
4476 monitor_read_command(mon, 0);
4477 }
aliguori87127162009-03-05 23:01:29 +00004478
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004479 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004480 mon->mc = qemu_mallocz(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004481 /* Control mode requires special handlers */
4482 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4483 monitor_control_event, mon);
4484 } else {
4485 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4486 monitor_event, mon);
4487 }
aliguori87127162009-03-05 23:01:29 +00004488
Blue Swirl72cf2d42009-09-12 07:36:22 +00004489 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004490 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4491 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004492}
4493
aliguori376253e2009-03-05 23:01:23 +00004494static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004495{
aliguoribb5fc202009-03-05 23:01:15 +00004496 BlockDriverState *bs = opaque;
4497 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004498
aliguoribb5fc202009-03-05 23:01:15 +00004499 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004500 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004501 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004502 }
aliguori731b0362009-03-05 23:01:42 +00004503 if (mon->password_completion_cb)
4504 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004505
aliguori731b0362009-03-05 23:01:42 +00004506 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004507}
aliguoric0f4ce72009-03-05 23:01:01 +00004508
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004509int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4510 BlockDriverCompletionFunc *completion_cb,
4511 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004512{
aliguoricde76ee2009-03-05 23:01:51 +00004513 int err;
4514
aliguoribb5fc202009-03-05 23:01:15 +00004515 if (!bdrv_key_required(bs)) {
4516 if (completion_cb)
4517 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004518 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004519 }
aliguoric0f4ce72009-03-05 23:01:01 +00004520
Luiz Capitulino94171e12009-12-07 21:37:00 +01004521 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004522 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004523 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004524 }
4525
aliguori376253e2009-03-05 23:01:23 +00004526 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4527 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004528
aliguori731b0362009-03-05 23:01:42 +00004529 mon->password_completion_cb = completion_cb;
4530 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004531
aliguoricde76ee2009-03-05 23:01:51 +00004532 err = monitor_read_password(mon, bdrv_password_cb, bs);
4533
4534 if (err && completion_cb)
4535 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004536
4537 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004538}