blob: 1e09b91faf01fe519838e62a97d13e33aa37fb14 [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"
Gerd Hoffmann75721502010-10-07 12:22:54 +020037#include "ui/qemu-spice.h"
pbrook87ecb682007-11-17 17:14:51 +000038#include "sysemu.h"
aliguori376253e2009-03-05 23:01:23 +000039#include "monitor.h"
40#include "readline.h"
pbrook87ecb682007-11-17 17:14:51 +000041#include "console.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020042#include "blockdev.h"
pbrook87ecb682007-11-17 17:14:51 +000043#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000044#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000045#include "balloon.h"
balrogc8256f92008-06-08 22:45:01 +000046#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000047#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000048#include "kvm.h"
aliguori76655d62009-03-06 20:27:37 +000049#include "acl.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030050#include "qint.h"
Markus Armbruster3350a4d2010-01-25 14:23:03 +010051#include "qfloat.h"
Luiz Capitulino8f3cec02009-10-07 13:42:04 -030052#include "qlist.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020053#include "qbool.h"
Luiz Capitulinof7188bb2009-08-28 15:27:10 -030054#include "qstring.h"
Luiz Capitulino9b57c022009-11-26 22:58:58 -020055#include "qjson.h"
Luiz Capitulino5fa737a2009-11-26 22:59:01 -020056#include "json-streamer.h"
57#include "json-parser.h"
Blue Swirld08d6f02009-12-04 18:06:39 +000058#include "osdep.h"
Blue Swirl2b41f102011-06-19 20:38:22 +000059#include "cpu.h"
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +010060#include "trace.h"
Lluís31965ae2011-08-31 20:31:24 +020061#include "trace/control.h"
Lluís6d8a7642011-08-31 20:30:43 +020062#ifdef CONFIG_TRACE_SIMPLE
Lluís31965ae2011-08-31 20:31:24 +020063#include "trace/simple.h"
Prerna Saxena22890ab2010-06-24 17:04:53 +053064#endif
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020065#include "ui/qemu-spice.h"
Blue Swirl314e2982011-09-11 20:22:05 +000066#include "memory.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050067#include "qmp-commands.h"
68#include "hmp.h"
ths6a5bd302007-12-03 17:05:38 +000069
Jan Kiszka661f1922011-10-16 11:53:13 +020070/* for pic/irq_info */
71#if defined(TARGET_SPARC)
72#include "hw/sun4m.h"
73#endif
74#include "hw/lm32_pic.h"
75
bellard9dc39cb2004-03-14 21:38:27 +000076//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000077//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000078
bellard9307c4c2004-04-04 12:57:25 +000079/*
80 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000081 *
bellard9307c4c2004-04-04 12:57:25 +000082 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000083 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000084 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010085 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000090 * 'i' 32 bit integer
91 * 'l' target long (32 or 64 bit)
Markus Armbruster9fec5432010-01-25 14:23:01 +010092 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020094 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +010099 * 'T' double
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000102 * '/' optional gdb-like print format (like "/10x")
103 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100106 * 'b' boolean
107 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300108 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000109 *
110 */
111
Adam Litke940cc302010-01-25 12:18:44 -0600112typedef struct MonitorCompletionData MonitorCompletionData;
113struct MonitorCompletionData {
114 Monitor *mon;
115 void (*user_print)(Monitor *mon, const QObject *data);
116};
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000119 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000120 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000121 const char *params;
122 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300123 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300124 union {
125 void (*info)(Monitor *mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300126 void (*info_new)(Monitor *mon, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600127 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300128 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600130 int (*cmd_async)(Monitor *mon, const QDict *params,
131 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300132 } mhandler;
Anthony Liguorie3193602011-09-02 12:34:47 -0500133 bool qapi;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200134 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500135} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000136
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100137/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500138typedef struct mon_fd_t mon_fd_t;
139struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100140 char *name;
141 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500142 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100143};
144
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200145typedef struct MonitorControl {
146 QObject *id;
147 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200148 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200149} MonitorControl;
150
aliguori87127162009-03-05 23:01:29 +0000151struct Monitor {
152 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200153 int mux_out;
154 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000155 int flags;
156 int suspend_cnt;
157 uint8_t outbuf[1024];
158 int outbuf_index;
159 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200160 MonitorControl *mc;
aliguori731b0362009-03-05 23:01:42 +0000161 CPUState *mon_cpu;
162 BlockDriverCompletionFunc *password_completion_cb;
163 void *password_opaque;
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200164#ifdef CONFIG_DEBUG_MONITOR
165 int print_calls_nr;
166#endif
Luiz Capitulino8204a912009-11-18 23:05:31 -0200167 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500168 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000170};
171
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200172#ifdef CONFIG_DEBUG_MONITOR
173#define MON_DEBUG(fmt, ...) do { \
174 fprintf(stderr, "Monitor: "); \
175 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200176
177static inline void mon_print_count_inc(Monitor *mon)
178{
179 mon->print_calls_nr++;
180}
181
182static inline void mon_print_count_init(Monitor *mon)
183{
184 mon->print_calls_nr = 0;
185}
186
187static inline int mon_print_count_get(const Monitor *mon)
188{
189 return mon->print_calls_nr;
190}
191
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200192#else /* !CONFIG_DEBUG_MONITOR */
193#define MON_DEBUG(fmt, ...) do { } while (0)
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200194static inline void mon_print_count_inc(Monitor *mon) { }
195static inline void mon_print_count_init(Monitor *mon) { }
196static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulinob4475aa2010-02-10 23:50:03 -0200197#endif /* CONFIG_DEBUG_MONITOR */
198
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300199/* QMP checker flags */
200#define QMP_ACCEPT_UNKNOWNS 1
201
Blue Swirl72cf2d42009-09-12 07:36:22 +0000202static QLIST_HEAD(mon_list, Monitor) mon_list;
bellard7e2515e2004-08-01 21:52:19 +0000203
Anthony Liguoric227f092009-10-01 16:12:16 -0500204static const mon_cmd_t mon_cmds[];
205static const mon_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +0000206
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300207static const mon_cmd_t qmp_cmds[];
208
Markus Armbruster8631b602010-02-18 11:41:55 +0100209Monitor *cur_mon;
210Monitor *default_mon;
aliguori376253e2009-03-05 23:01:23 +0000211
aliguori731b0362009-03-05 23:01:42 +0000212static void monitor_command_cb(Monitor *mon, const char *cmdline,
213 void *opaque);
aliguori83ab7952008-08-19 14:44:22 +0000214
Luiz Capitulino09069b12010-02-04 18:10:06 -0200215static inline int qmp_cmd_mode(const Monitor *mon)
216{
217 return (mon->mc ? mon->mc->command_mode : 0);
218}
219
Luiz Capitulino418173c2009-11-26 22:58:51 -0200220/* Return true if in control mode, false otherwise */
221static inline int monitor_ctrl_mode(const Monitor *mon)
222{
223 return (mon->flags & MONITOR_USE_CONTROL);
224}
225
Markus Armbruster6620d3c2010-02-11 17:05:43 +0100226/* Return non-zero iff we have a current monitor, and it is in QMP mode. */
227int monitor_cur_is_qmp(void)
228{
229 return cur_mon && monitor_ctrl_mode(cur_mon);
230}
231
aliguori731b0362009-03-05 23:01:42 +0000232static void monitor_read_command(Monitor *mon, int show_prompt)
233{
Luiz Capitulino183e6e52009-12-14 18:53:23 -0200234 if (!mon->rs)
235 return;
236
aliguori731b0362009-03-05 23:01:42 +0000237 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
238 if (show_prompt)
239 readline_show_prompt(mon->rs);
240}
bellard6a00d602005-11-21 23:25:50 +0000241
aliguoricde76ee2009-03-05 23:01:51 +0000242static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
243 void *opaque)
aliguoribb5fc202009-03-05 23:01:15 +0000244{
Luiz Capitulino94171e12009-12-07 21:37:00 +0100245 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100246 qerror_report(QERR_MISSING_PARAMETER, "password");
Luiz Capitulino94171e12009-12-07 21:37:00 +0100247 return -EINVAL;
248 } else if (mon->rs) {
aliguoricde76ee2009-03-05 23:01:51 +0000249 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
250 /* prompt is printed on return from the command handler */
251 return 0;
252 } else {
253 monitor_printf(mon, "terminal does not support password prompting\n");
254 return -ENOTTY;
255 }
aliguoribb5fc202009-03-05 23:01:15 +0000256}
257
aliguori376253e2009-03-05 23:01:23 +0000258void monitor_flush(Monitor *mon)
bellard9dc39cb2004-03-14 21:38:27 +0000259{
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200260 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500261 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
aliguori731b0362009-03-05 23:01:42 +0000262 mon->outbuf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000263 }
264}
265
266/* flush at every end of line or if the buffer is full */
aliguori376253e2009-03-05 23:01:23 +0000267static void monitor_puts(Monitor *mon, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000268{
ths60fe76f2007-12-16 03:02:09 +0000269 char c;
aliguori731b0362009-03-05 23:01:42 +0000270
bellard7e2515e2004-08-01 21:52:19 +0000271 for(;;) {
272 c = *str++;
273 if (c == '\0')
274 break;
bellard7ba12602006-07-14 20:26:42 +0000275 if (c == '\n')
aliguori731b0362009-03-05 23:01:42 +0000276 mon->outbuf[mon->outbuf_index++] = '\r';
277 mon->outbuf[mon->outbuf_index++] = c;
278 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
279 || c == '\n')
aliguori376253e2009-03-05 23:01:23 +0000280 monitor_flush(mon);
bellard7e2515e2004-08-01 21:52:19 +0000281 }
282}
283
aliguori376253e2009-03-05 23:01:23 +0000284void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard7e2515e2004-08-01 21:52:19 +0000285{
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200286 char buf[4096];
287
Luiz Capitulino2daa1192009-12-14 18:53:24 -0200288 if (!mon)
289 return;
290
Luiz Capitulino10e4f602010-02-10 23:50:06 -0200291 mon_print_count_inc(mon);
292
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200293 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200295 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200296
297 vsnprintf(buf, sizeof(buf), fmt, ap);
298 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000299}
300
aliguori376253e2009-03-05 23:01:23 +0000301void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000302{
303 va_list ap;
304 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000305 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000306 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000307}
308
aliguori376253e2009-03-05 23:01:23 +0000309void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000310{
311 int i;
312
313 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000314 switch (filename[i]) {
315 case ' ':
316 case '"':
317 case '\\':
318 monitor_printf(mon, "\\%c", filename[i]);
319 break;
320 case '\t':
321 monitor_printf(mon, "\\t");
322 break;
323 case '\r':
324 monitor_printf(mon, "\\r");
325 break;
326 case '\n':
327 monitor_printf(mon, "\\n");
328 break;
329 default:
330 monitor_printf(mon, "%c", filename[i]);
331 break;
332 }
thsfef30742006-12-22 14:11:32 +0000333 }
334}
335
Stefan Weil8b7968f2010-09-23 21:28:05 +0200336static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
337 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000338{
339 va_list ap;
340 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000341 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000342 va_end(ap);
343 return 0;
344}
345
Luiz Capitulino13c74252009-10-07 13:41:55 -0300346static void monitor_user_noop(Monitor *mon, const QObject *data) { }
347
Luiz Capitulino9e807212010-09-16 10:58:59 -0300348static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300349{
350 return cmd->user_print != NULL;
351}
352
Luiz Capitulino4903de02010-09-16 11:01:32 -0300353static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600354{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200355 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600356}
357
Luiz Capitulino8204a912009-11-18 23:05:31 -0200358static inline int monitor_has_error(const Monitor *mon)
359{
360 return mon->error != NULL;
361}
362
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200363static void monitor_json_emitter(Monitor *mon, const QObject *data)
364{
365 QString *json;
366
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200367 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
368 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200369 assert(json != NULL);
370
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200371 qstring_append_chr(json, '\n');
372 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200373
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200374 QDECREF(json);
375}
376
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200377static void monitor_protocol_emitter(Monitor *mon, QObject *data)
378{
379 QDict *qmp;
380
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100381 trace_monitor_protocol_emitter(mon);
382
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200383 qmp = qdict_new();
384
385 if (!monitor_has_error(mon)) {
386 /* success response */
387 if (data) {
388 qobject_incref(data);
389 qdict_put_obj(qmp, "return", data);
390 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200391 /* return an empty QDict by default */
392 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200393 }
394 } else {
395 /* error response */
Markus Armbruster77e595e2009-12-07 21:37:16 +0100396 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200397 qdict_put(qmp, "error", mon->error->error);
398 QINCREF(mon->error->error);
399 QDECREF(mon->error);
400 mon->error = NULL;
401 }
402
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200403 if (mon->mc->id) {
404 qdict_put_obj(qmp, "id", mon->mc->id);
405 mon->mc->id = NULL;
406 }
407
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200408 monitor_json_emitter(mon, QOBJECT(qmp));
409 QDECREF(qmp);
410}
411
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200412static void timestamp_put(QDict *qdict)
413{
414 int err;
415 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000416 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200417
Blue Swirld08d6f02009-12-04 18:06:39 +0000418 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200419 if (err < 0)
420 return;
421
422 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
423 "'microseconds': %" PRId64 " }",
424 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425 qdict_put_obj(qdict, "timestamp", obj);
426}
427
428/**
429 * monitor_protocol_event(): Generate a Monitor event
430 *
431 * Event-specific data can be emitted through the (optional) 'data' parameter.
432 */
433void monitor_protocol_event(MonitorEvent event, QObject *data)
434{
435 QDict *qmp;
436 const char *event_name;
Adam Litkef039a562010-01-15 08:34:02 -0600437 Monitor *mon;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200438
Blue Swirl242cd002009-12-04 18:05:45 +0000439 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200440
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200441 switch (event) {
Blue Swirl242cd002009-12-04 18:05:45 +0000442 case QEVENT_SHUTDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200443 event_name = "SHUTDOWN";
444 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000445 case QEVENT_RESET:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200446 event_name = "RESET";
447 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000448 case QEVENT_POWERDOWN:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200449 event_name = "POWERDOWN";
450 break;
Blue Swirl242cd002009-12-04 18:05:45 +0000451 case QEVENT_STOP:
Luiz Capitulinob1a15e72009-11-26 22:59:04 -0200452 event_name = "STOP";
453 break;
Luiz Capitulino6ed2c482010-04-27 20:35:59 -0300454 case QEVENT_RESUME:
455 event_name = "RESUME";
456 break;
Luiz Capitulino586153d2010-01-14 14:50:57 -0200457 case QEVENT_VNC_CONNECTED:
458 event_name = "VNC_CONNECTED";
459 break;
Luiz Capitulino0d2ed462010-01-14 14:50:59 -0200460 case QEVENT_VNC_INITIALIZED:
461 event_name = "VNC_INITIALIZED";
462 break;
Luiz Capitulino0d72f3d2010-01-14 14:50:58 -0200463 case QEVENT_VNC_DISCONNECTED:
464 event_name = "VNC_DISCONNECTED";
465 break;
Luiz Capitulinoaa1db6e2010-02-03 12:41:00 -0200466 case QEVENT_BLOCK_IO_ERROR:
467 event_name = "BLOCK_IO_ERROR";
468 break;
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300469 case QEVENT_RTC_CHANGE:
470 event_name = "RTC_CHANGE";
471 break;
Luiz Capitulino9eedeb32010-02-25 12:13:04 -0300472 case QEVENT_WATCHDOG:
473 event_name = "WATCHDOG";
474 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200475 case QEVENT_SPICE_CONNECTED:
476 event_name = "SPICE_CONNECTED";
477 break;
478 case QEVENT_SPICE_INITIALIZED:
479 event_name = "SPICE_INITIALIZED";
480 break;
481 case QEVENT_SPICE_DISCONNECTED:
482 event_name = "SPICE_DISCONNECTED";
483 break;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200484 default:
485 abort();
486 break;
487 }
488
489 qmp = qdict_new();
490 timestamp_put(qmp);
491 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200492 if (data) {
493 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200494 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200495 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200496
Adam Litkef039a562010-01-15 08:34:02 -0600497 QLIST_FOREACH(mon, &mon_list, entry) {
Luiz Capitulino09069b12010-02-04 18:10:06 -0200498 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
Luiz Capitulino23fabed2010-01-20 10:37:59 -0200499 monitor_json_emitter(mon, QOBJECT(qmp));
500 }
Adam Litkef039a562010-01-15 08:34:02 -0600501 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200502 QDECREF(qmp);
503}
504
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200505static int do_qmp_capabilities(Monitor *mon, const QDict *params,
506 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200507{
508 /* Will setup QMP capabilities in the future */
509 if (monitor_ctrl_mode(mon)) {
510 mon->mc->command_mode = 1;
511 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200512
513 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200514}
515
Luiz Capitulino0268d972010-10-22 10:08:02 -0200516static void handle_user_command(Monitor *mon, const char *cmdline);
517
518static int do_hmp_passthrough(Monitor *mon, const QDict *params,
519 QObject **ret_data)
520{
521 int ret = 0;
522 Monitor *old_mon, hmp;
523 CharDriverState mchar;
524
525 memset(&hmp, 0, sizeof(hmp));
526 qemu_chr_init_mem(&mchar);
527 hmp.chr = &mchar;
528
529 old_mon = cur_mon;
530 cur_mon = &hmp;
531
532 if (qdict_haskey(params, "cpu-index")) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300533 ret = monitor_set_cpu(qdict_get_int(params, "cpu-index"));
Luiz Capitulino0268d972010-10-22 10:08:02 -0200534 if (ret < 0) {
535 cur_mon = old_mon;
536 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
537 goto out;
538 }
539 }
540
541 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
542 cur_mon = old_mon;
543
544 if (qemu_chr_mem_osize(hmp.chr) > 0) {
545 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
546 }
547
548out:
549 qemu_chr_close_mem(hmp.chr);
550 return ret;
551}
552
bellard9dc39cb2004-03-14 21:38:27 +0000553static int compare_cmd(const char *name, const char *list)
554{
555 const char *p, *pstart;
556 int len;
557 len = strlen(name);
558 p = list;
559 for(;;) {
560 pstart = p;
561 p = strchr(p, '|');
562 if (!p)
563 p = pstart + strlen(pstart);
564 if ((p - pstart) == len && !memcmp(pstart, name, len))
565 return 1;
566 if (*p == '\0')
567 break;
568 p++;
569 }
570 return 0;
571}
572
Anthony Liguoric227f092009-10-01 16:12:16 -0500573static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000574 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000575{
Anthony Liguoric227f092009-10-01 16:12:16 -0500576 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000577
578 for(cmd = cmds; cmd->name != NULL; cmd++) {
579 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000580 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
581 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000582 }
583}
584
aliguori376253e2009-03-05 23:01:23 +0000585static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000586{
587 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000588 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000589 } else {
aliguori376253e2009-03-05 23:01:23 +0000590 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000591 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000592 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000593 monitor_printf(mon, "Log items (comma separated):\n");
594 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000595 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000596 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000597 }
598 }
bellard9dc39cb2004-03-14 21:38:27 +0000599 }
600}
601
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300602static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300603{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300604 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300605}
606
Lluísfc764102011-08-31 20:31:18 +0200607static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530608{
609 const char *tp_name = qdict_get_str(qdict, "name");
610 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200611 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000612
613 if (!ret) {
614 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
615 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530616}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100617
Michael Rothc45a8162011-10-02 08:44:37 -0500618#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100619static void do_trace_file(Monitor *mon, const QDict *qdict)
620{
621 const char *op = qdict_get_try_str(qdict, "op");
622 const char *arg = qdict_get_try_str(qdict, "arg");
623
624 if (!op) {
625 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
626 } else if (!strcmp(op, "on")) {
627 st_set_trace_file_enabled(true);
628 } else if (!strcmp(op, "off")) {
629 st_set_trace_file_enabled(false);
630 } else if (!strcmp(op, "flush")) {
631 st_flush_trace_buffer();
632 } else if (!strcmp(op, "set")) {
633 if (arg) {
634 st_set_trace_file(arg);
635 }
636 } else {
637 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
638 help_cmd(mon, "trace-file");
639 }
640}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530641#endif
642
Adam Litke940cc302010-01-25 12:18:44 -0600643static void user_monitor_complete(void *opaque, QObject *ret_data)
644{
645 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
646
647 if (ret_data) {
648 data->user_print(data->mon, ret_data);
649 }
650 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500651 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600652}
653
654static void qmp_monitor_complete(void *opaque, QObject *ret_data)
655{
656 monitor_protocol_emitter(opaque, ret_data);
657}
658
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300659static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
660 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600661{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300662 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600663}
664
Adam Litke940cc302010-01-25 12:18:44 -0600665static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
666 const QDict *params)
667{
668 int ret;
669
Anthony Liguori7267c092011-08-20 22:09:37 -0500670 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600671 cb_data->mon = mon;
672 cb_data->user_print = cmd->user_print;
673 monitor_suspend(mon);
674 ret = cmd->mhandler.cmd_async(mon, params,
675 user_monitor_complete, cb_data);
676 if (ret < 0) {
677 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500678 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600679 }
680}
681
682static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
683{
684 int ret;
685
Anthony Liguori7267c092011-08-20 22:09:37 -0500686 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600687 cb_data->mon = mon;
688 cb_data->user_print = cmd->user_print;
689 monitor_suspend(mon);
690 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
691 if (ret < 0) {
692 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500693 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600694 }
695}
696
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300697static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000698{
Anthony Liguoric227f092009-10-01 16:12:16 -0500699 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300700 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000701
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200702 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000703 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200704 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300705
706 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000707 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300708 break;
bellard9dc39cb2004-03-14 21:38:27 +0000709 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300710
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200711 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300712 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200713 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300714
Luiz Capitulino4903de02010-09-16 11:01:32 -0300715 if (handler_is_async(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300716 user_async_info_handler(mon, cmd);
Luiz Capitulino9e807212010-09-16 10:58:59 -0300717 } else if (handler_is_qobject(cmd)) {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300718 QObject *info_data = NULL;
Jan Kiszkaa6c4d362010-06-28 18:27:47 +0200719
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300720 cmd->mhandler.info_new(mon, &info_data);
721 if (info_data) {
722 cmd->user_print(mon, info_data);
723 qobject_decref(info_data);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200724 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300725 } else {
Luiz Capitulino1dcbd6f2010-09-10 17:00:16 -0300726 cmd->mhandler.info(mon);
Luiz Capitulino13c74252009-10-07 13:41:55 -0300727 }
728
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300729 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300730
731help:
732 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000733}
734
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300735CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000736{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300737 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200738 const mon_cmd_t *cmd;
739
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300740 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200741 info = g_malloc0(sizeof(*info));
742 info->value = g_malloc0(sizeof(*info->value));
743 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200744
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300745 info->next = cmd_list;
746 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200747 }
748
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300749 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000750}
751
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300752/* set the current CPU defined by the user */
753int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000754{
755 CPUState *env;
756
757 for(env = first_cpu; env != NULL; env = env->next_cpu) {
758 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000759 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000760 return 0;
761 }
762 }
763 return -1;
764}
765
pbrook9596ebb2007-11-18 01:44:38 +0000766static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000767{
aliguori731b0362009-03-05 23:01:42 +0000768 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300769 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000770 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300771 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000772 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000773}
774
Luiz Capitulino99b77962011-10-24 10:53:44 -0200775int monitor_get_cpu_index(void)
776{
777 return mon_get_cpu()->cpu_index;
778}
779
aliguori376253e2009-03-05 23:01:23 +0000780static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000781{
bellard6a00d602005-11-21 23:25:50 +0000782 CPUState *env;
783 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000784#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000785 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000786 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000787#else
aliguori376253e2009-03-05 23:01:23 +0000788 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000789 0);
bellard9307c4c2004-04-04 12:57:25 +0000790#endif
791}
792
aliguori376253e2009-03-05 23:01:23 +0000793static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000794{
aliguori376253e2009-03-05 23:01:23 +0000795 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000796}
797
aliguori376253e2009-03-05 23:01:23 +0000798static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000799{
800 int i;
bellard7e2515e2004-08-01 21:52:19 +0000801 const char *str;
ths3b46e622007-09-17 08:09:54 +0000802
aliguoricde76ee2009-03-05 23:01:51 +0000803 if (!mon->rs)
804 return;
bellard7e2515e2004-08-01 21:52:19 +0000805 i = 0;
806 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000807 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000808 if (!str)
809 break;
aliguori376253e2009-03-05 23:01:23 +0000810 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000811 i++;
bellardaa455482004-04-04 13:07:25 +0000812 }
813}
814
j_mayer76a66252007-03-07 08:32:30 +0000815#if defined(TARGET_PPC)
816/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000817static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000818{
819 CPUState *env;
820
821 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000822 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000823}
824#endif
825
Lluís6d8a7642011-08-31 20:30:43 +0200826#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530827static void do_info_trace(Monitor *mon)
828{
829 st_print_trace((FILE *)mon, &monitor_fprintf);
830}
Lluís31965ae2011-08-31 20:31:24 +0200831#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +0530832
Lluísfc764102011-08-31 20:31:18 +0200833static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530834{
Lluísfc764102011-08-31 20:31:18 +0200835 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530836}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530837
Jes Sorensen821601e2011-03-16 13:33:36 +0100838#ifdef CONFIG_VNC
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200839static int change_vnc_password(const char *password)
aliguoribb5fc202009-03-05 23:01:15 +0000840{
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600841 if (!password || !password[0]) {
842 if (vnc_display_disable_login(NULL)) {
843 qerror_report(QERR_SET_PASSWD_FAILED);
844 return -1;
845 }
846 return 0;
847 }
848
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200849 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100850 qerror_report(QERR_SET_PASSWD_FAILED);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200851 return -1;
852 }
aliguoribb5fc202009-03-05 23:01:15 +0000853
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200854 return 0;
Markus Armbruster2895e072009-12-07 21:37:01 +0100855}
856
857static void change_vnc_password_cb(Monitor *mon, const char *password,
858 void *opaque)
859{
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100860 change_vnc_password(password);
aliguori731b0362009-03-05 23:01:42 +0000861 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +0000862}
863
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200864static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000865{
ths70848512007-08-25 01:37:05 +0000866 if (strcmp(target, "passwd") == 0 ||
aliguori28a76be2009-03-06 20:27:40 +0000867 strcmp(target, "password") == 0) {
868 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000869 char password[9];
aliguori28a76be2009-03-06 20:27:40 +0000870 strncpy(password, arg, sizeof(password));
871 password[sizeof(password) - 1] = '\0';
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200872 return change_vnc_password(password);
aliguoribb5fc202009-03-05 23:01:15 +0000873 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200874 return monitor_read_password(mon, change_vnc_password_cb, NULL);
aliguoribb5fc202009-03-05 23:01:15 +0000875 }
ths70848512007-08-25 01:37:05 +0000876 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200877 if (vnc_display_open(NULL, target) < 0) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +0100878 qerror_report(QERR_VNC_SERVER_FAILED, target);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200879 return -1;
880 }
ths70848512007-08-25 01:37:05 +0000881 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200882
883 return 0;
thse25a5822007-08-25 01:36:20 +0000884}
Jes Sorensen821601e2011-03-16 13:33:36 +0100885#else
886static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
887{
888 qerror_report(QERR_FEATURE_DISABLED, "vnc");
889 return -ENODEV;
890}
891#endif
thse25a5822007-08-25 01:36:20 +0000892
Markus Armbrusterec3b82a2009-12-07 21:37:09 +0100893/**
894 * do_change(): Change a removable medium, or VNC configuration
895 */
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200896static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
thse25a5822007-08-25 01:36:20 +0000897{
Luiz Capitulino1d4daa92009-08-28 15:27:15 -0300898 const char *device = qdict_get_str(qdict, "device");
899 const char *target = qdict_get_str(qdict, "target");
900 const char *arg = qdict_get_try_str(qdict, "arg");
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200901 int ret;
902
thse25a5822007-08-25 01:36:20 +0000903 if (strcmp(device, "vnc") == 0) {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200904 ret = do_change_vnc(mon, target, arg);
thse25a5822007-08-25 01:36:20 +0000905 } else {
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200906 ret = do_change_block(mon, device, target, arg);
thse25a5822007-08-25 01:36:20 +0000907 }
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -0200908
909 return ret;
thse25a5822007-08-25 01:36:20 +0000910}
911
Gerd Hoffmann75721502010-10-07 12:22:54 +0200912static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
913{
914 const char *protocol = qdict_get_str(qdict, "protocol");
915 const char *password = qdict_get_str(qdict, "password");
916 const char *connected = qdict_get_try_str(qdict, "connected");
917 int disconnect_if_connected = 0;
918 int fail_if_connected = 0;
919 int rc;
920
921 if (connected) {
922 if (strcmp(connected, "fail") == 0) {
923 fail_if_connected = 1;
924 } else if (strcmp(connected, "disconnect") == 0) {
925 disconnect_if_connected = 1;
926 } else if (strcmp(connected, "keep") == 0) {
927 /* nothing */
928 } else {
929 qerror_report(QERR_INVALID_PARAMETER, "connected");
930 return -1;
931 }
932 }
933
934 if (strcmp(protocol, "spice") == 0) {
935 if (!using_spice) {
936 /* correct one? spice isn't a device ,,, */
937 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
938 return -1;
939 }
940 rc = qemu_spice_set_passwd(password, fail_if_connected,
941 disconnect_if_connected);
942 if (rc != 0) {
943 qerror_report(QERR_SET_PASSWD_FAILED);
944 return -1;
945 }
946 return 0;
947 }
948
949 if (strcmp(protocol, "vnc") == 0) {
950 if (fail_if_connected || disconnect_if_connected) {
951 /* vnc supports "connected=keep" only */
952 qerror_report(QERR_INVALID_PARAMETER, "connected");
953 return -1;
954 }
Anthony Liguori1cd20f82011-01-31 14:27:36 -0600955 /* Note that setting an empty password will not disable login through
956 * this interface. */
Jes Sorensen821601e2011-03-16 13:33:36 +0100957 return vnc_display_password(NULL, password);
Gerd Hoffmann75721502010-10-07 12:22:54 +0200958 }
959
960 qerror_report(QERR_INVALID_PARAMETER, "protocol");
961 return -1;
962}
963
964static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
965{
966 const char *protocol = qdict_get_str(qdict, "protocol");
967 const char *whenstr = qdict_get_str(qdict, "time");
968 time_t when;
969 int rc;
970
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100971 if (strcmp(whenstr, "now") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200972 when = 0;
Marc-André Lureau8d86e2b2011-01-06 11:43:17 +0100973 } else if (strcmp(whenstr, "never") == 0) {
Gerd Hoffmann75721502010-10-07 12:22:54 +0200974 when = TIME_MAX;
975 } else if (whenstr[0] == '+') {
976 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
977 } else {
978 when = strtoull(whenstr, NULL, 10);
979 }
980
981 if (strcmp(protocol, "spice") == 0) {
982 if (!using_spice) {
983 /* correct one? spice isn't a device ,,, */
984 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
985 return -1;
986 }
987 rc = qemu_spice_set_pw_expire(when);
988 if (rc != 0) {
989 qerror_report(QERR_SET_PASSWD_FAILED);
990 return -1;
991 }
992 return 0;
993 }
994
995 if (strcmp(protocol, "vnc") == 0) {
Jes Sorensen821601e2011-03-16 13:33:36 +0100996 return vnc_display_pw_expire(NULL, when);
Gerd Hoffmann75721502010-10-07 12:22:54 +0200997 }
998
999 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1000 return -1;
1001}
1002
Daniel P. Berrange13661082011-06-23 13:31:42 +01001003static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1004{
1005 const char *protocol = qdict_get_str(qdict, "protocol");
1006 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +01001007 CharDriverState *s;
1008
1009 if (strcmp(protocol, "spice") == 0) {
1010 if (!using_spice) {
1011 /* correct one? spice isn't a device ,,, */
1012 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1013 return -1;
1014 }
1015 qerror_report(QERR_ADD_CLIENT_FAILED);
1016 return -1;
TeLeManc62f6d12011-07-25 16:29:14 +08001017#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +01001018 } else if (strcmp(protocol, "vnc") == 0) {
1019 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +01001020 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +01001021 vnc_display_add_client(NULL, fd, skipauth);
1022 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +08001023#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +01001024 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1025 int fd = monitor_get_fd(mon, fdname);
1026 if (qemu_chr_add_client(s, fd) < 0) {
1027 qerror_report(QERR_ADD_CLIENT_FAILED);
1028 return -1;
1029 }
1030 return 0;
1031 }
1032
1033 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1034 return -1;
1035}
1036
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001037static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1038{
1039 const char *protocol = qdict_get_str(qdict, "protocol");
1040 const char *hostname = qdict_get_str(qdict, "hostname");
1041 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1042 int port = qdict_get_try_int(qdict, "port", -1);
1043 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1044 int ret;
1045
1046 if (strcmp(protocol, "spice") == 0) {
1047 if (!using_spice) {
1048 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1049 return -1;
1050 }
1051
1052 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1053 if (ret != 0) {
1054 qerror_report(QERR_UNDEFINED_ERROR);
1055 return -1;
1056 }
1057 return 0;
1058 }
1059
1060 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1061 return -1;
1062}
1063
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001064static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001065{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001066 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001067 return 0;
bellard59a983b2004-03-17 23:17:16 +00001068}
1069
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001070static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001071{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001072 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001073}
1074
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001075static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001076{
1077 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001078 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001079
bellard9307c4c2004-04-04 12:57:25 +00001080 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001081 mask = 0;
1082 } else {
bellard9307c4c2004-04-04 12:57:25 +00001083 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001084 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001085 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001086 return;
1087 }
1088 }
1089 cpu_set_log(mask);
1090}
1091
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001092static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001093{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001094 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001095 if (!option || !strcmp(option, "on")) {
1096 singlestep = 1;
1097 } else if (!strcmp(option, "off")) {
1098 singlestep = 0;
1099 } else {
1100 monitor_printf(mon, "unexpected option %s\n", option);
1101 }
1102}
1103
aliguoribb5fc202009-03-05 23:01:15 +00001104static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +00001105
aliguori376253e2009-03-05 23:01:23 +00001106struct bdrv_iterate_context {
1107 Monitor *mon;
1108 int err;
1109};
aliguoric0f4ce72009-03-05 23:01:01 +00001110
Luiz Capitulino28a72822011-09-26 17:43:50 -03001111static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1112{
1113 bdrv_iostatus_reset(bs);
1114}
1115
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001116/**
1117 * do_cont(): Resume emulation.
1118 */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001119static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori376253e2009-03-05 23:01:23 +00001120{
1121 struct bdrv_iterate_context context = { mon, 0 };
1122
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001123 if (runstate_check(RUN_STATE_INMIGRATE)) {
Amit Shah8e848652010-07-27 15:49:19 +05301124 qerror_report(QERR_MIGRATION_EXPECTED);
1125 return -1;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001126 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1127 runstate_check(RUN_STATE_SHUTDOWN)) {
Luiz Capitulino6667b232011-07-29 15:57:54 -03001128 qerror_report(QERR_RESET_REQUIRED);
1129 return -1;
Amit Shah8e848652010-07-27 15:49:19 +05301130 }
Luiz Capitulino6667b232011-07-29 15:57:54 -03001131
Luiz Capitulino28a72822011-09-26 17:43:50 -03001132 bdrv_iterate(iostatus_bdrv_it, NULL);
aliguori376253e2009-03-05 23:01:23 +00001133 bdrv_iterate(encrypted_bdrv_it, &context);
aliguoric0f4ce72009-03-05 23:01:01 +00001134 /* only resume the vm if all keys are set and valid */
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001135 if (!context.err) {
aliguoric0f4ce72009-03-05 23:01:01 +00001136 vm_start();
Luiz Capitulinod5a7b382010-02-10 23:49:49 -02001137 return 0;
1138 } else {
1139 return -1;
1140 }
bellard8a7ddc32004-03-31 19:00:16 +00001141}
1142
aliguoribb5fc202009-03-05 23:01:15 +00001143static void bdrv_key_cb(void *opaque, int err)
1144{
aliguori376253e2009-03-05 23:01:23 +00001145 Monitor *mon = opaque;
1146
aliguoribb5fc202009-03-05 23:01:15 +00001147 /* another key was set successfully, retry to continue */
1148 if (!err)
Luiz Capitulinoa1f896a2009-10-07 13:42:00 -03001149 do_cont(mon, NULL, NULL);
aliguoribb5fc202009-03-05 23:01:15 +00001150}
1151
1152static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1153{
aliguori376253e2009-03-05 23:01:23 +00001154 struct bdrv_iterate_context *context = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00001155
aliguori376253e2009-03-05 23:01:23 +00001156 if (!context->err && bdrv_key_required(bs)) {
1157 context->err = -EBUSY;
1158 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1159 context->mon);
aliguoribb5fc202009-03-05 23:01:15 +00001160 }
1161}
1162
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001163static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001164{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001165 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001166 if (!device)
1167 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1168 if (gdbserver_start(device) < 0) {
1169 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1170 device);
1171 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001172 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001173 } else {
aliguori59030a82009-04-05 18:43:41 +00001174 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1175 device);
bellard8a7ddc32004-03-31 19:00:16 +00001176 }
1177}
1178
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001179static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001180{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001181 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001182 if (select_watchdog_action(action) == -1) {
1183 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1184 }
1185}
1186
aliguori376253e2009-03-05 23:01:23 +00001187static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001188{
aliguori376253e2009-03-05 23:01:23 +00001189 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001190 switch(c) {
1191 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001192 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001193 break;
1194 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001195 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001196 break;
1197 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001198 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001199 break;
1200 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001201 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001202 break;
1203 default:
1204 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001205 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001206 } else {
aliguori376253e2009-03-05 23:01:23 +00001207 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001208 }
1209 break;
1210 }
aliguori376253e2009-03-05 23:01:23 +00001211 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001212}
1213
aliguori376253e2009-03-05 23:01:23 +00001214static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001215 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001216{
bellard6a00d602005-11-21 23:25:50 +00001217 CPUState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001218 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001219 uint8_t buf[16];
1220 uint64_t v;
1221
1222 if (format == 'i') {
1223 int flags;
1224 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001225 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001226#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001227 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001228 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001229 } else if (wsize == 4) {
1230 flags = 0;
1231 } else {
bellard6a15fd12006-04-12 21:07:07 +00001232 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001233 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001234 if (env) {
1235#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001236 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001237 (env->segs[R_CS].flags & DESC_L_MASK))
1238 flags = 2;
1239 else
1240#endif
1241 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1242 flags = 1;
1243 }
bellard4c27ba22004-04-25 18:05:08 +00001244 }
1245#endif
aliguori376253e2009-03-05 23:01:23 +00001246 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001247 return;
1248 }
1249
1250 len = wsize * count;
1251 if (wsize == 1)
1252 line_size = 8;
1253 else
1254 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001255 max_digits = 0;
1256
1257 switch(format) {
1258 case 'o':
1259 max_digits = (wsize * 8 + 2) / 3;
1260 break;
1261 default:
1262 case 'x':
1263 max_digits = (wsize * 8) / 4;
1264 break;
1265 case 'u':
1266 case 'd':
1267 max_digits = (wsize * 8 * 10 + 32) / 33;
1268 break;
1269 case 'c':
1270 wsize = 1;
1271 break;
1272 }
1273
1274 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001275 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001276 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001277 else
aliguori376253e2009-03-05 23:01:23 +00001278 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001279 l = len;
1280 if (l > line_size)
1281 l = line_size;
1282 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001283 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001284 } else {
bellard6a00d602005-11-21 23:25:50 +00001285 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001286 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001287 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001288 break;
1289 }
bellard9307c4c2004-04-04 12:57:25 +00001290 }
ths5fafdf22007-09-16 21:08:06 +00001291 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001292 while (i < l) {
1293 switch(wsize) {
1294 default:
1295 case 1:
1296 v = ldub_raw(buf + i);
1297 break;
1298 case 2:
1299 v = lduw_raw(buf + i);
1300 break;
1301 case 4:
bellard92a31b12005-02-10 22:00:52 +00001302 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001303 break;
1304 case 8:
1305 v = ldq_raw(buf + i);
1306 break;
1307 }
aliguori376253e2009-03-05 23:01:23 +00001308 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001309 switch(format) {
1310 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001311 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001312 break;
1313 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001314 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001315 break;
1316 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001317 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001318 break;
1319 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001320 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001321 break;
1322 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001323 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001324 break;
1325 }
1326 i += wsize;
1327 }
aliguori376253e2009-03-05 23:01:23 +00001328 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001329 addr += l;
1330 len -= l;
1331 }
1332}
1333
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001334static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001335{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001336 int count = qdict_get_int(qdict, "count");
1337 int format = qdict_get_int(qdict, "format");
1338 int size = qdict_get_int(qdict, "size");
1339 target_long addr = qdict_get_int(qdict, "addr");
1340
aliguori376253e2009-03-05 23:01:23 +00001341 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001342}
1343
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001344static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001345{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001346 int count = qdict_get_int(qdict, "count");
1347 int format = qdict_get_int(qdict, "format");
1348 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001349 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001350
aliguori376253e2009-03-05 23:01:23 +00001351 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001352}
1353
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001354static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001355{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001356 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001357 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001358
blueswir17743e582007-09-24 18:39:04 +00001359#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +00001360 switch(format) {
1361 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001362 monitor_printf(mon, "%#o", val);
bellard9307c4c2004-04-04 12:57:25 +00001363 break;
1364 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001365 monitor_printf(mon, "%#x", val);
bellard9307c4c2004-04-04 12:57:25 +00001366 break;
1367 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001368 monitor_printf(mon, "%u", val);
bellard9307c4c2004-04-04 12:57:25 +00001369 break;
1370 default:
1371 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001372 monitor_printf(mon, "%d", val);
bellard9307c4c2004-04-04 12:57:25 +00001373 break;
1374 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001375 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001376 break;
1377 }
bellard92a31b12005-02-10 22:00:52 +00001378#else
1379 switch(format) {
1380 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001381 monitor_printf(mon, "%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +00001382 break;
1383 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001384 monitor_printf(mon, "%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +00001385 break;
1386 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001387 monitor_printf(mon, "%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +00001388 break;
1389 default:
1390 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001391 monitor_printf(mon, "%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +00001392 break;
1393 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001394 monitor_printc(mon, val);
bellard92a31b12005-02-10 22:00:52 +00001395 break;
1396 }
1397#endif
aliguori376253e2009-03-05 23:01:23 +00001398 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001399}
1400
Luiz Capitulino98696222010-02-10 23:49:58 -02001401static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellardb371dc52007-01-03 15:20:39 +00001402{
1403 FILE *f;
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001404 uint32_t size = qdict_get_int(qdict, "size");
1405 const char *filename = qdict_get_str(qdict, "filename");
1406 target_long addr = qdict_get_int(qdict, "val");
bellardb371dc52007-01-03 15:20:39 +00001407 uint32_t l;
1408 CPUState *env;
1409 uint8_t buf[1024];
Luiz Capitulino98696222010-02-10 23:49:58 -02001410 int ret = -1;
bellardb371dc52007-01-03 15:20:39 +00001411
1412 env = mon_get_cpu();
bellardb371dc52007-01-03 15:20:39 +00001413
1414 f = fopen(filename, "wb");
1415 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001416 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulino98696222010-02-10 23:49:58 -02001417 return -1;
bellardb371dc52007-01-03 15:20:39 +00001418 }
1419 while (size != 0) {
1420 l = sizeof(buf);
1421 if (l > size)
1422 l = size;
1423 cpu_memory_rw_debug(env, addr, buf, l, 0);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001424 if (fwrite(buf, 1, l, f) != l) {
1425 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1426 goto exit;
1427 }
bellardb371dc52007-01-03 15:20:39 +00001428 addr += l;
1429 size -= l;
1430 }
Luiz Capitulino98696222010-02-10 23:49:58 -02001431
1432 ret = 0;
1433
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001434exit:
bellardb371dc52007-01-03 15:20:39 +00001435 fclose(f);
Luiz Capitulino98696222010-02-10 23:49:58 -02001436 return ret;
bellardb371dc52007-01-03 15:20:39 +00001437}
1438
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001439static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
Luiz Capitulino18f5a8b2009-10-16 12:23:44 -03001440 QObject **ret_data)
aurel32a8bdf7a2008-04-11 21:36:14 +00001441{
1442 FILE *f;
1443 uint32_t l;
1444 uint8_t buf[1024];
Luiz Capitulinoafe67ef2009-08-28 15:27:16 -03001445 uint32_t size = qdict_get_int(qdict, "size");
1446 const char *filename = qdict_get_str(qdict, "filename");
Anthony Liguoric227f092009-10-01 16:12:16 -05001447 target_phys_addr_t addr = qdict_get_int(qdict, "val");
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001448 int ret = -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001449
1450 f = fopen(filename, "wb");
1451 if (!f) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01001452 qerror_report(QERR_OPEN_FILE_FAILED, filename);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001453 return -1;
aurel32a8bdf7a2008-04-11 21:36:14 +00001454 }
1455 while (size != 0) {
1456 l = sizeof(buf);
1457 if (l > size)
1458 l = size;
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001459 cpu_physical_memory_read(addr, buf, l);
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001460 if (fwrite(buf, 1, l, f) != l) {
1461 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1462 goto exit;
1463 }
aurel32a8bdf7a2008-04-11 21:36:14 +00001464 fflush(f);
1465 addr += l;
1466 size -= l;
1467 }
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001468
1469 ret = 0;
1470
Kirill A. Shutemov27e3ddd2010-01-20 00:56:19 +01001471exit:
aurel32a8bdf7a2008-04-11 21:36:14 +00001472 fclose(f);
Luiz Capitulinofe38a322010-02-10 23:49:59 -02001473 return ret;
aurel32a8bdf7a2008-04-11 21:36:14 +00001474}
1475
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001476static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001477{
1478 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001479 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001480 uint32_t start = qdict_get_int(qdict, "start");
1481 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001482
1483 sum = 0;
1484 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001485 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001486 /* BSD sum algorithm ('sum' Unix command) */
1487 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001488 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001489 }
aliguori376253e2009-03-05 23:01:23 +00001490 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001491}
1492
bellarda3a91a32004-06-04 11:06:21 +00001493typedef struct {
1494 int keycode;
1495 const char *name;
1496} KeyDef;
1497
1498static const KeyDef key_defs[] = {
1499 { 0x2a, "shift" },
1500 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001501
bellarda3a91a32004-06-04 11:06:21 +00001502 { 0x38, "alt" },
1503 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001504 { 0x64, "altgr" },
1505 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001506 { 0x1d, "ctrl" },
1507 { 0x9d, "ctrl_r" },
1508
1509 { 0xdd, "menu" },
1510
1511 { 0x01, "esc" },
1512
1513 { 0x02, "1" },
1514 { 0x03, "2" },
1515 { 0x04, "3" },
1516 { 0x05, "4" },
1517 { 0x06, "5" },
1518 { 0x07, "6" },
1519 { 0x08, "7" },
1520 { 0x09, "8" },
1521 { 0x0a, "9" },
1522 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001523 { 0x0c, "minus" },
1524 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001525 { 0x0e, "backspace" },
1526
1527 { 0x0f, "tab" },
1528 { 0x10, "q" },
1529 { 0x11, "w" },
1530 { 0x12, "e" },
1531 { 0x13, "r" },
1532 { 0x14, "t" },
1533 { 0x15, "y" },
1534 { 0x16, "u" },
1535 { 0x17, "i" },
1536 { 0x18, "o" },
1537 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001538 { 0x1a, "bracket_left" },
1539 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001540 { 0x1c, "ret" },
1541
1542 { 0x1e, "a" },
1543 { 0x1f, "s" },
1544 { 0x20, "d" },
1545 { 0x21, "f" },
1546 { 0x22, "g" },
1547 { 0x23, "h" },
1548 { 0x24, "j" },
1549 { 0x25, "k" },
1550 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001551 { 0x27, "semicolon" },
1552 { 0x28, "apostrophe" },
1553 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001554
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001555 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001556 { 0x2c, "z" },
1557 { 0x2d, "x" },
1558 { 0x2e, "c" },
1559 { 0x2f, "v" },
1560 { 0x30, "b" },
1561 { 0x31, "n" },
1562 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001563 { 0x33, "comma" },
1564 { 0x34, "dot" },
1565 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001566
balrog4d3b6f62008-02-10 16:33:14 +00001567 { 0x37, "asterisk" },
1568
bellarda3a91a32004-06-04 11:06:21 +00001569 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001570 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001571 { 0x3b, "f1" },
1572 { 0x3c, "f2" },
1573 { 0x3d, "f3" },
1574 { 0x3e, "f4" },
1575 { 0x3f, "f5" },
1576 { 0x40, "f6" },
1577 { 0x41, "f7" },
1578 { 0x42, "f8" },
1579 { 0x43, "f9" },
1580 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001581 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001582 { 0x46, "scroll_lock" },
1583
bellard64866c32006-05-07 18:03:31 +00001584 { 0xb5, "kp_divide" },
1585 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001586 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001587 { 0x4e, "kp_add" },
1588 { 0x9c, "kp_enter" },
1589 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001590 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001591
1592 { 0x52, "kp_0" },
1593 { 0x4f, "kp_1" },
1594 { 0x50, "kp_2" },
1595 { 0x51, "kp_3" },
1596 { 0x4b, "kp_4" },
1597 { 0x4c, "kp_5" },
1598 { 0x4d, "kp_6" },
1599 { 0x47, "kp_7" },
1600 { 0x48, "kp_8" },
1601 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001602
bellarda3a91a32004-06-04 11:06:21 +00001603 { 0x56, "<" },
1604
1605 { 0x57, "f11" },
1606 { 0x58, "f12" },
1607
1608 { 0xb7, "print" },
1609
1610 { 0xc7, "home" },
1611 { 0xc9, "pgup" },
1612 { 0xd1, "pgdn" },
1613 { 0xcf, "end" },
1614
1615 { 0xcb, "left" },
1616 { 0xc8, "up" },
1617 { 0xd0, "down" },
1618 { 0xcd, "right" },
1619
1620 { 0xd2, "insert" },
1621 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001622#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1623 { 0xf0, "stop" },
1624 { 0xf1, "again" },
1625 { 0xf2, "props" },
1626 { 0xf3, "undo" },
1627 { 0xf4, "front" },
1628 { 0xf5, "copy" },
1629 { 0xf6, "open" },
1630 { 0xf7, "paste" },
1631 { 0xf8, "find" },
1632 { 0xf9, "cut" },
1633 { 0xfa, "lf" },
1634 { 0xfb, "help" },
1635 { 0xfc, "meta_l" },
1636 { 0xfd, "meta_r" },
1637 { 0xfe, "compose" },
1638#endif
bellarda3a91a32004-06-04 11:06:21 +00001639 { 0, NULL },
1640};
1641
1642static int get_keycode(const char *key)
1643{
1644 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001645 char *endp;
1646 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001647
1648 for(p = key_defs; p->name != NULL; p++) {
1649 if (!strcmp(key, p->name))
1650 return p->keycode;
1651 }
bellard64866c32006-05-07 18:03:31 +00001652 if (strstart(key, "0x", NULL)) {
1653 ret = strtoul(key, &endp, 0);
1654 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1655 return ret;
1656 }
bellarda3a91a32004-06-04 11:06:21 +00001657 return -1;
1658}
1659
balrogc8256f92008-06-08 22:45:01 +00001660#define MAX_KEYCODES 16
1661static uint8_t keycodes[MAX_KEYCODES];
1662static int nb_pending_keycodes;
1663static QEMUTimer *key_timer;
1664
1665static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001666{
balrogc8256f92008-06-08 22:45:01 +00001667 int keycode;
1668
1669 while (nb_pending_keycodes > 0) {
1670 nb_pending_keycodes--;
1671 keycode = keycodes[nb_pending_keycodes];
1672 if (keycode & 0x80)
1673 kbd_put_keycode(0xe0);
1674 kbd_put_keycode(keycode | 0x80);
1675 }
1676}
1677
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001678static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001679{
balrog3401c0d2008-06-04 10:05:59 +00001680 char keyname_buf[16];
1681 char *separator;
1682 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001683 const char *string = qdict_get_str(qdict, "string");
1684 int has_hold_time = qdict_haskey(qdict, "hold_time");
1685 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001686
balrogc8256f92008-06-08 22:45:01 +00001687 if (nb_pending_keycodes > 0) {
1688 qemu_del_timer(key_timer);
1689 release_keys(NULL);
1690 }
1691 if (!has_hold_time)
1692 hold_time = 100;
1693 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001694 while (1) {
1695 separator = strchr(string, '-');
1696 keyname_len = separator ? separator - string : strlen(string);
1697 if (keyname_len > 0) {
1698 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1699 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001700 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001701 return;
bellarda3a91a32004-06-04 11:06:21 +00001702 }
balrogc8256f92008-06-08 22:45:01 +00001703 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001704 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001705 return;
1706 }
1707 keyname_buf[keyname_len] = 0;
1708 keycode = get_keycode(keyname_buf);
1709 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001710 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001711 return;
1712 }
balrogc8256f92008-06-08 22:45:01 +00001713 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001714 }
balrog3401c0d2008-06-04 10:05:59 +00001715 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001716 break;
balrog3401c0d2008-06-04 10:05:59 +00001717 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001718 }
balrogc8256f92008-06-08 22:45:01 +00001719 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001720 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001721 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001722 keycode = keycodes[i];
1723 if (keycode & 0x80)
1724 kbd_put_keycode(0xe0);
1725 kbd_put_keycode(keycode & 0x7f);
1726 }
balrogc8256f92008-06-08 22:45:01 +00001727 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001728 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001729 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001730}
1731
bellard13224a82006-07-14 22:03:35 +00001732static int mouse_button_state;
1733
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001734static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001735{
1736 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001737 const char *dx_str = qdict_get_str(qdict, "dx_str");
1738 const char *dy_str = qdict_get_str(qdict, "dy_str");
1739 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001740 dx = strtol(dx_str, NULL, 0);
1741 dy = strtol(dy_str, NULL, 0);
1742 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001743 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001744 dz = strtol(dz_str, NULL, 0);
1745 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1746}
1747
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001748static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001749{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001750 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001751 mouse_button_state = button_state;
1752 kbd_mouse_event(0, 0, 0, mouse_button_state);
1753}
1754
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001755static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001756{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001757 int size = qdict_get_int(qdict, "size");
1758 int addr = qdict_get_int(qdict, "addr");
1759 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001760 uint32_t val;
1761 int suffix;
1762
1763 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001764 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001765 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001766 addr++;
1767 }
1768 addr &= 0xffff;
1769
1770 switch(size) {
1771 default:
1772 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001773 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001774 suffix = 'b';
1775 break;
1776 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001777 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001778 suffix = 'w';
1779 break;
1780 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001781 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001782 suffix = 'l';
1783 break;
1784 }
aliguori376253e2009-03-05 23:01:23 +00001785 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1786 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001787}
bellarda3a91a32004-06-04 11:06:21 +00001788
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001789static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001790{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001791 int size = qdict_get_int(qdict, "size");
1792 int addr = qdict_get_int(qdict, "addr");
1793 int val = qdict_get_int(qdict, "val");
1794
Jan Kiszkaf1147842009-07-14 10:20:11 +02001795 addr &= IOPORTS_MASK;
1796
1797 switch (size) {
1798 default:
1799 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001800 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001801 break;
1802 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001803 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001804 break;
1805 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001806 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001807 break;
1808 }
1809}
1810
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001811static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001812{
1813 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001814 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001815
Jan Kiszka76e30d02009-07-02 00:19:02 +02001816 res = qemu_boot_set(bootdevice);
1817 if (res == 0) {
1818 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1819 } else if (res > 0) {
1820 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001821 } else {
aliguori376253e2009-03-05 23:01:23 +00001822 monitor_printf(mon, "no function defined to set boot device list for "
1823 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001824 }
1825}
1826
Luiz Capitulinoc80d2592009-10-07 13:41:58 -03001827/**
Luiz Capitulino43076662009-10-07 13:41:59 -03001828 * do_system_powerdown(): Issue a machine powerdown
1829 */
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001830static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1831 QObject **ret_data)
bellard34751872005-07-02 14:31:34 +00001832{
1833 qemu_system_powerdown_request();
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -02001834 return 0;
bellard34751872005-07-02 14:31:34 +00001835}
1836
bellardb86bda52004-09-18 19:32:46 +00001837#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001838static void print_pte(Monitor *mon, target_phys_addr_t addr,
1839 target_phys_addr_t pte,
1840 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001841{
Blue Swirld65aaf32010-12-11 18:56:24 +00001842#ifdef TARGET_X86_64
1843 if (addr & (1ULL << 47)) {
1844 addr |= -1LL << 48;
1845 }
1846#endif
1847 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1848 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001849 addr,
1850 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001851 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001852 pte & PG_GLOBAL_MASK ? 'G' : '-',
1853 pte & PG_PSE_MASK ? 'P' : '-',
1854 pte & PG_DIRTY_MASK ? 'D' : '-',
1855 pte & PG_ACCESSED_MASK ? 'A' : '-',
1856 pte & PG_PCD_MASK ? 'C' : '-',
1857 pte & PG_PWT_MASK ? 'T' : '-',
1858 pte & PG_USER_MASK ? 'U' : '-',
1859 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001860}
1861
Blue Swirld65aaf32010-12-11 18:56:24 +00001862static void tlb_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00001863{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001864 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001865 uint32_t pgd, pde, pte;
1866
bellardb86bda52004-09-18 19:32:46 +00001867 pgd = env->cr[3] & ~0xfff;
1868 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001869 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001870 pde = le32_to_cpu(pde);
1871 if (pde & PG_PRESENT_MASK) {
1872 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001873 /* 4M pages */
1874 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001875 } else {
1876 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001877 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001878 pte = le32_to_cpu(pte);
1879 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001880 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001881 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001882 ~0xfff);
1883 }
1884 }
1885 }
1886 }
1887 }
1888}
1889
Blue Swirld65aaf32010-12-11 18:56:24 +00001890static void tlb_info_pae32(Monitor *mon, CPUState *env)
1891{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001892 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001893 uint64_t pdpe, pde, pte;
1894 uint64_t pdp_addr, pd_addr, pt_addr;
1895
1896 pdp_addr = env->cr[3] & ~0x1f;
1897 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001898 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001899 pdpe = le64_to_cpu(pdpe);
1900 if (pdpe & PG_PRESENT_MASK) {
1901 pd_addr = pdpe & 0x3fffffffff000ULL;
1902 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001903 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001904 pde = le64_to_cpu(pde);
1905 if (pde & PG_PRESENT_MASK) {
1906 if (pde & PG_PSE_MASK) {
1907 /* 2M pages with PAE, CR4.PSE is ignored */
1908 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1909 ~((target_phys_addr_t)(1 << 20) - 1));
1910 } else {
1911 pt_addr = pde & 0x3fffffffff000ULL;
1912 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001913 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001914 pte = le64_to_cpu(pte);
1915 if (pte & PG_PRESENT_MASK) {
1916 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1917 + (l3 << 12),
1918 pte & ~PG_PSE_MASK,
1919 ~(target_phys_addr_t)0xfff);
1920 }
1921 }
1922 }
1923 }
1924 }
1925 }
1926 }
1927}
1928
1929#ifdef TARGET_X86_64
1930static void tlb_info_64(Monitor *mon, CPUState *env)
1931{
1932 uint64_t l1, l2, l3, l4;
1933 uint64_t pml4e, pdpe, pde, pte;
1934 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1935
1936 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1937 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001938 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001939 pml4e = le64_to_cpu(pml4e);
1940 if (pml4e & PG_PRESENT_MASK) {
1941 pdp_addr = pml4e & 0x3fffffffff000ULL;
1942 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001943 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001944 pdpe = le64_to_cpu(pdpe);
1945 if (pdpe & PG_PRESENT_MASK) {
1946 if (pdpe & PG_PSE_MASK) {
1947 /* 1G pages, CR4.PSE is ignored */
1948 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1949 0x3ffffc0000000ULL);
1950 } else {
1951 pd_addr = pdpe & 0x3fffffffff000ULL;
1952 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001953 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001954 pde = le64_to_cpu(pde);
1955 if (pde & PG_PRESENT_MASK) {
1956 if (pde & PG_PSE_MASK) {
1957 /* 2M pages, CR4.PSE is ignored */
1958 print_pte(mon, (l1 << 39) + (l2 << 30) +
1959 (l3 << 21), pde,
1960 0x3ffffffe00000ULL);
1961 } else {
1962 pt_addr = pde & 0x3fffffffff000ULL;
1963 for (l4 = 0; l4 < 512; l4++) {
1964 cpu_physical_memory_read(pt_addr
1965 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001966 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001967 pte = le64_to_cpu(pte);
1968 if (pte & PG_PRESENT_MASK) {
1969 print_pte(mon, (l1 << 39) +
1970 (l2 << 30) +
1971 (l3 << 21) + (l4 << 12),
1972 pte & ~PG_PSE_MASK,
1973 0x3fffffffff000ULL);
1974 }
1975 }
1976 }
1977 }
1978 }
1979 }
1980 }
1981 }
1982 }
1983 }
1984}
1985#endif
1986
1987static void tlb_info(Monitor *mon)
1988{
1989 CPUState *env;
1990
1991 env = mon_get_cpu();
1992
1993 if (!(env->cr[0] & CR0_PG_MASK)) {
1994 monitor_printf(mon, "PG disabled\n");
1995 return;
1996 }
1997 if (env->cr[4] & CR4_PAE_MASK) {
1998#ifdef TARGET_X86_64
1999 if (env->hflags & HF_LMA_MASK) {
2000 tlb_info_64(mon, env);
2001 } else
2002#endif
2003 {
2004 tlb_info_pae32(mon, env);
2005 }
2006 } else {
2007 tlb_info_32(mon, env);
2008 }
2009}
2010
Blue Swirl1b3cba62010-12-11 18:56:27 +00002011static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2012 int *plast_prot,
2013 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00002014{
bellard9746b152004-11-11 18:30:24 +00002015 int prot1;
2016 prot1 = *plast_prot;
2017 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00002018 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00002019 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2020 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00002021 *pstart, end, end - *pstart,
2022 prot1 & PG_USER_MASK ? 'u' : '-',
2023 'r',
2024 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00002025 }
2026 if (prot != 0)
2027 *pstart = end;
2028 else
2029 *pstart = -1;
2030 *plast_prot = prot;
2031 }
2032}
2033
Blue Swirl1b3cba62010-12-11 18:56:27 +00002034static void mem_info_32(Monitor *mon, CPUState *env)
bellardb86bda52004-09-18 19:32:46 +00002035{
Austin Clementsb49ca722011-08-14 23:19:21 -04002036 unsigned int l1, l2;
2037 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002038 uint32_t pgd, pde, pte;
2039 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00002040
bellardb86bda52004-09-18 19:32:46 +00002041 pgd = env->cr[3] & ~0xfff;
2042 last_prot = 0;
2043 start = -1;
2044 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002045 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00002046 pde = le32_to_cpu(pde);
2047 end = l1 << 22;
2048 if (pde & PG_PRESENT_MASK) {
2049 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2050 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00002051 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002052 } else {
2053 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002054 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00002055 pte = le32_to_cpu(pte);
2056 end = (l1 << 22) + (l2 << 12);
2057 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002058 prot = pte & pde &
2059 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00002060 } else {
2061 prot = 0;
2062 }
aliguori376253e2009-03-05 23:01:23 +00002063 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002064 }
2065 }
2066 } else {
2067 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00002068 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00002069 }
2070 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002071 /* Flush last range */
2072 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00002073}
Blue Swirl1b3cba62010-12-11 18:56:27 +00002074
2075static void mem_info_pae32(Monitor *mon, CPUState *env)
2076{
Austin Clementsb49ca722011-08-14 23:19:21 -04002077 unsigned int l1, l2, l3;
2078 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002079 uint64_t pdpe, pde, pte;
2080 uint64_t pdp_addr, pd_addr, pt_addr;
2081 target_phys_addr_t start, end;
2082
2083 pdp_addr = env->cr[3] & ~0x1f;
2084 last_prot = 0;
2085 start = -1;
2086 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002087 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002088 pdpe = le64_to_cpu(pdpe);
2089 end = l1 << 30;
2090 if (pdpe & PG_PRESENT_MASK) {
2091 pd_addr = pdpe & 0x3fffffffff000ULL;
2092 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002093 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002094 pde = le64_to_cpu(pde);
2095 end = (l1 << 30) + (l2 << 21);
2096 if (pde & PG_PRESENT_MASK) {
2097 if (pde & PG_PSE_MASK) {
2098 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2099 PG_PRESENT_MASK);
2100 mem_print(mon, &start, &last_prot, end, prot);
2101 } else {
2102 pt_addr = pde & 0x3fffffffff000ULL;
2103 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002104 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002105 pte = le64_to_cpu(pte);
2106 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2107 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04002108 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2109 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002110 } else {
2111 prot = 0;
2112 }
2113 mem_print(mon, &start, &last_prot, end, prot);
2114 }
2115 }
2116 } else {
2117 prot = 0;
2118 mem_print(mon, &start, &last_prot, end, prot);
2119 }
2120 }
2121 } else {
2122 prot = 0;
2123 mem_print(mon, &start, &last_prot, end, prot);
2124 }
2125 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002126 /* Flush last range */
2127 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002128}
2129
2130
2131#ifdef TARGET_X86_64
2132static void mem_info_64(Monitor *mon, CPUState *env)
2133{
2134 int prot, last_prot;
2135 uint64_t l1, l2, l3, l4;
2136 uint64_t pml4e, pdpe, pde, pte;
2137 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2138
2139 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2140 last_prot = 0;
2141 start = -1;
2142 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002143 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002144 pml4e = le64_to_cpu(pml4e);
2145 end = l1 << 39;
2146 if (pml4e & PG_PRESENT_MASK) {
2147 pdp_addr = pml4e & 0x3fffffffff000ULL;
2148 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002149 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002150 pdpe = le64_to_cpu(pdpe);
2151 end = (l1 << 39) + (l2 << 30);
2152 if (pdpe & PG_PRESENT_MASK) {
2153 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00002154 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2155 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002156 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002157 mem_print(mon, &start, &last_prot, end, prot);
2158 } else {
2159 pd_addr = pdpe & 0x3fffffffff000ULL;
2160 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01002161 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002162 pde = le64_to_cpu(pde);
2163 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2164 if (pde & PG_PRESENT_MASK) {
2165 if (pde & PG_PSE_MASK) {
2166 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2167 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002168 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002169 mem_print(mon, &start, &last_prot, end, prot);
2170 } else {
2171 pt_addr = pde & 0x3fffffffff000ULL;
2172 for (l4 = 0; l4 < 512; l4++) {
2173 cpu_physical_memory_read(pt_addr
2174 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01002175 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002176 pte = le64_to_cpu(pte);
2177 end = (l1 << 39) + (l2 << 30) +
2178 (l3 << 21) + (l4 << 12);
2179 if (pte & PG_PRESENT_MASK) {
2180 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2181 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04002182 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002183 } else {
2184 prot = 0;
2185 }
2186 mem_print(mon, &start, &last_prot, end, prot);
2187 }
2188 }
2189 } else {
2190 prot = 0;
2191 mem_print(mon, &start, &last_prot, end, prot);
2192 }
2193 }
2194 }
2195 } else {
2196 prot = 0;
2197 mem_print(mon, &start, &last_prot, end, prot);
2198 }
2199 }
2200 } else {
2201 prot = 0;
2202 mem_print(mon, &start, &last_prot, end, prot);
2203 }
2204 }
Austin Clements8a94b8ca2011-08-14 23:22:04 -04002205 /* Flush last range */
2206 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00002207}
2208#endif
2209
2210static void mem_info(Monitor *mon)
2211{
2212 CPUState *env;
2213
2214 env = mon_get_cpu();
2215
2216 if (!(env->cr[0] & CR0_PG_MASK)) {
2217 monitor_printf(mon, "PG disabled\n");
2218 return;
2219 }
2220 if (env->cr[4] & CR4_PAE_MASK) {
2221#ifdef TARGET_X86_64
2222 if (env->hflags & HF_LMA_MASK) {
2223 mem_info_64(mon, env);
2224 } else
2225#endif
2226 {
2227 mem_info_pae32(mon, env);
2228 }
2229 } else {
2230 mem_info_32(mon, env);
2231 }
2232}
bellardb86bda52004-09-18 19:32:46 +00002233#endif
2234
aurel327c664e22009-03-03 06:12:22 +00002235#if defined(TARGET_SH4)
2236
aliguori376253e2009-03-05 23:01:23 +00002237static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002238{
aliguori376253e2009-03-05 23:01:23 +00002239 monitor_printf(mon, " tlb%i:\t"
2240 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2241 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2242 "dirty=%hhu writethrough=%hhu\n",
2243 idx,
2244 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2245 tlb->v, tlb->sh, tlb->c, tlb->pr,
2246 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002247}
2248
aliguori376253e2009-03-05 23:01:23 +00002249static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002250{
2251 CPUState *env = mon_get_cpu();
2252 int i;
2253
aliguori376253e2009-03-05 23:01:23 +00002254 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002255 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002256 print_tlb (mon, i, &env->itlb[i]);
2257 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002258 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002259 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002260}
2261
2262#endif
2263
Scott Woodbebabbc2011-08-18 10:38:42 +00002264#if defined(TARGET_SPARC) || defined(TARGET_PPC)
Blue Swirld41160a2010-12-19 13:42:56 +00002265static void tlb_info(Monitor *mon)
2266{
2267 CPUState *env1 = mon_get_cpu();
2268
2269 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2270}
2271#endif
2272
Blue Swirl314e2982011-09-11 20:22:05 +00002273static void do_info_mtree(Monitor *mon)
2274{
2275 mtree_info((fprintf_function)monitor_printf, mon);
2276}
2277
aliguori030ea372009-04-21 22:30:47 +00002278static void do_info_numa(Monitor *mon)
2279{
aliguorib28b6232009-04-22 20:20:29 +00002280 int i;
aliguori030ea372009-04-21 22:30:47 +00002281 CPUState *env;
2282
2283 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2284 for (i = 0; i < nb_numa_nodes; i++) {
2285 monitor_printf(mon, "node %d cpus:", i);
2286 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2287 if (env->numa_node == i) {
2288 monitor_printf(mon, " %d", env->cpu_index);
2289 }
2290 }
2291 monitor_printf(mon, "\n");
2292 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2293 node_mem[i] >> 20);
2294 }
2295}
2296
bellard5f1ce942006-02-08 22:40:15 +00002297#ifdef CONFIG_PROFILER
2298
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002299int64_t qemu_time;
2300int64_t dev_time;
2301
aliguori376253e2009-03-05 23:01:23 +00002302static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002303{
2304 int64_t total;
2305 total = qemu_time;
2306 if (total == 0)
2307 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002308 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002309 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002310 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002311 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002312 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002313 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002314}
2315#else
aliguori376253e2009-03-05 23:01:23 +00002316static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002317{
aliguori376253e2009-03-05 23:01:23 +00002318 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002319}
2320#endif
2321
bellardec36b692006-07-16 18:57:03 +00002322/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002323static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002324
aliguori376253e2009-03-05 23:01:23 +00002325static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002326{
2327 int i;
2328 CaptureState *s;
2329
2330 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002331 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002332 s->ops.info (s->opaque);
2333 }
2334}
2335
Blue Swirl23130862009-06-06 08:22:04 +00002336#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002337static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002338{
2339 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002340 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002341 CaptureState *s;
2342
2343 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2344 if (i == n) {
2345 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002346 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002347 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002348 return;
2349 }
2350 }
2351}
2352
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002353static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002354{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002355 const char *path = qdict_get_str(qdict, "path");
2356 int has_freq = qdict_haskey(qdict, "freq");
2357 int freq = qdict_get_try_int(qdict, "freq", -1);
2358 int has_bits = qdict_haskey(qdict, "bits");
2359 int bits = qdict_get_try_int(qdict, "bits", -1);
2360 int has_channels = qdict_haskey(qdict, "nchannels");
2361 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002362 CaptureState *s;
2363
Anthony Liguori7267c092011-08-20 22:09:37 -05002364 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002365
2366 freq = has_freq ? freq : 44100;
2367 bits = has_bits ? bits : 16;
2368 nchannels = has_channels ? nchannels : 2;
2369
2370 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002371 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002372 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002373 return;
bellardec36b692006-07-16 18:57:03 +00002374 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002375 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002376}
2377#endif
2378
aurel32dc1c0b72008-04-27 23:52:12 +00002379#if defined(TARGET_I386)
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002380static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002381{
2382 CPUState *env;
2383
2384 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2385 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2386 }
2387
2388 return 0;
2389}
2390#else
Luiz Capitulinoe9b4b432011-04-29 12:11:50 -03002391static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
Lai Jiangshana4046662011-03-07 17:05:15 +08002392{
2393 qerror_report(QERR_UNSUPPORTED);
2394 return -1;
2395}
aurel32dc1c0b72008-04-27 23:52:12 +00002396#endif
2397
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002398static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002399{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002400 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002401
aliguori76655d62009-03-06 20:27:37 +00002402 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002403 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002404 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002405 return acl;
2406}
aliguori76655d62009-03-06 20:27:37 +00002407
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002408static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002409{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002410 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002411 qemu_acl *acl = find_acl(mon, aclname);
2412 qemu_acl_entry *entry;
2413 int i = 0;
2414
2415 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002416 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002417 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002418 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002419 i++;
2420 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002421 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002422 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002423 }
2424}
2425
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002426static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002427{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002428 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002429 qemu_acl *acl = find_acl(mon, aclname);
2430
2431 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002432 qemu_acl_reset(acl);
2433 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002434 }
2435}
aliguori76655d62009-03-06 20:27:37 +00002436
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002437static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002438{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002439 const char *aclname = qdict_get_str(qdict, "aclname");
2440 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002441 qemu_acl *acl = find_acl(mon, aclname);
2442
2443 if (acl) {
2444 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002445 acl->defaultDeny = 0;
2446 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002447 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002448 acl->defaultDeny = 1;
2449 monitor_printf(mon, "acl: policy set to 'deny'\n");
2450 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002451 monitor_printf(mon, "acl: unknown policy '%s', "
2452 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002453 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002454 }
2455}
aliguori76655d62009-03-06 20:27:37 +00002456
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002457static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002458{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002459 const char *aclname = qdict_get_str(qdict, "aclname");
2460 const char *match = qdict_get_str(qdict, "match");
2461 const char *policy = qdict_get_str(qdict, "policy");
2462 int has_index = qdict_haskey(qdict, "index");
2463 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002464 qemu_acl *acl = find_acl(mon, aclname);
2465 int deny, ret;
2466
2467 if (acl) {
2468 if (strcmp(policy, "allow") == 0) {
2469 deny = 0;
2470 } else if (strcmp(policy, "deny") == 0) {
2471 deny = 1;
2472 } else {
2473 monitor_printf(mon, "acl: unknown policy '%s', "
2474 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002475 return;
2476 }
aliguori28a76be2009-03-06 20:27:40 +00002477 if (has_index)
2478 ret = qemu_acl_insert(acl, deny, match, index);
2479 else
2480 ret = qemu_acl_append(acl, deny, match);
2481 if (ret < 0)
2482 monitor_printf(mon, "acl: unable to add acl entry\n");
2483 else
2484 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002485 }
2486}
aliguori76655d62009-03-06 20:27:37 +00002487
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002488static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002489{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002490 const char *aclname = qdict_get_str(qdict, "aclname");
2491 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002492 qemu_acl *acl = find_acl(mon, aclname);
2493 int ret;
aliguori76655d62009-03-06 20:27:37 +00002494
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002495 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002496 ret = qemu_acl_remove(acl, match);
2497 if (ret < 0)
2498 monitor_printf(mon, "acl: no matching acl entry\n");
2499 else
2500 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002501 }
2502}
2503
Huang Ying79c4f6b2009-06-23 10:05:14 +08002504#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002505static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002506{
2507 CPUState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002508 int cpu_index = qdict_get_int(qdict, "cpu_index");
2509 int bank = qdict_get_int(qdict, "bank");
2510 uint64_t status = qdict_get_int(qdict, "status");
2511 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2512 uint64_t addr = qdict_get_int(qdict, "addr");
2513 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002514 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002515
Jan Kiszka747461c2011-03-02 08:56:10 +01002516 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2517 flags |= MCE_INJECT_BROADCAST;
2518 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002519 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002520 if (cenv->cpu_index == cpu_index) {
2521 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002522 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002523 break;
2524 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002525 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002526}
2527#endif
2528
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002529static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002530{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002531 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002532 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002533 int fd;
2534
Anthony Liguori74c0d6f2011-08-15 11:17:39 -05002535 fd = qemu_chr_fe_get_msgfd(mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002536 if (fd == -1) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002537 qerror_report(QERR_FD_NOT_SUPPLIED);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002538 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002539 }
2540
2541 if (qemu_isdigit(fdname[0])) {
Markus Armbrustere17ba872010-03-25 17:22:36 +01002542 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2543 "a name not starting with a digit");
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002544 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002545 }
2546
Blue Swirl72cf2d42009-09-12 07:36:22 +00002547 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002548 if (strcmp(monfd->name, fdname) != 0) {
2549 continue;
2550 }
2551
2552 close(monfd->fd);
2553 monfd->fd = fd;
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002554 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002555 }
2556
Anthony Liguori7267c092011-08-20 22:09:37 -05002557 monfd = g_malloc0(sizeof(mon_fd_t));
2558 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002559 monfd->fd = fd;
2560
Blue Swirl72cf2d42009-09-12 07:36:22 +00002561 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
Luiz Capitulino6ad3ebd2010-02-10 23:49:53 -02002562 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002563}
2564
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002565static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002566{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002567 const char *fdname = qdict_get_str(qdict, "fdname");
Anthony Liguoric227f092009-10-01 16:12:16 -05002568 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002569
Blue Swirl72cf2d42009-09-12 07:36:22 +00002570 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002571 if (strcmp(monfd->name, fdname) != 0) {
2572 continue;
2573 }
2574
Blue Swirl72cf2d42009-09-12 07:36:22 +00002575 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002576 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002577 g_free(monfd->name);
2578 g_free(monfd);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002579 return 0;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002580 }
2581
Markus Armbrusterab5b0272010-03-02 18:15:09 +01002582 qerror_report(QERR_FD_NOT_FOUND, fdname);
Luiz Capitulinoaeb91c12010-02-10 23:49:54 -02002583 return -1;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002584}
2585
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002586static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002587{
Luiz Capitulino13548692011-07-29 15:36:43 -03002588 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002589 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002590
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002591 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002592
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002593 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002594 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002595 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002596}
2597
Mark McLoughlin7768e042009-07-22 09:11:41 +01002598int monitor_get_fd(Monitor *mon, const char *fdname)
2599{
Anthony Liguoric227f092009-10-01 16:12:16 -05002600 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002601
Blue Swirl72cf2d42009-09-12 07:36:22 +00002602 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002603 int fd;
2604
2605 if (strcmp(monfd->name, fdname) != 0) {
2606 continue;
2607 }
2608
2609 fd = monfd->fd;
2610
2611 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002612 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002613 g_free(monfd->name);
2614 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002615
2616 return fd;
2617 }
2618
2619 return -1;
2620}
2621
Anthony Liguoric227f092009-10-01 16:12:16 -05002622static const mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002623#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002624 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002625};
2626
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002627/* Please update hmp-commands.hx when adding or changing commands */
Anthony Liguoric227f092009-10-01 16:12:16 -05002628static const mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002629 {
2630 .name = "version",
2631 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002632 .params = "",
2633 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002634 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002635 },
2636 {
2637 .name = "network",
2638 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002639 .params = "",
2640 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002641 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002642 },
2643 {
2644 .name = "chardev",
2645 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002646 .params = "",
2647 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002648 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002649 },
2650 {
2651 .name = "block",
2652 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 .params = "",
2654 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002655 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 },
2657 {
2658 .name = "blockstats",
2659 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 .params = "",
2661 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002662 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002663 },
2664 {
2665 .name = "registers",
2666 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 .params = "",
2668 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002669 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002670 },
2671 {
2672 .name = "cpus",
2673 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 .params = "",
2675 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002676 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
2678 {
2679 .name = "history",
2680 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 .params = "",
2682 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002683 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002685#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2686 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002687 {
2688 .name = "irq",
2689 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002690 .params = "",
2691 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002692#ifdef TARGET_SPARC
2693 .mhandler.info = sun4m_irq_info,
2694#elif defined(TARGET_LM32)
2695 .mhandler.info = lm32_irq_info,
2696#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002697 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002698#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002699 },
2700 {
2701 .name = "pic",
2702 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002703 .params = "",
2704 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002705#ifdef TARGET_SPARC
2706 .mhandler.info = sun4m_pic_info,
2707#elif defined(TARGET_LM32)
2708 .mhandler.info = lm32_do_pic_info,
2709#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002710 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002711#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002713#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002714 {
2715 .name = "pci",
2716 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002717 .params = "",
2718 .help = "show PCI info",
Luiz Capitulino79627472011-10-21 14:15:33 -02002719 .mhandler.info = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002720 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002721#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2722 defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002723 {
2724 .name = "tlb",
2725 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002726 .params = "",
2727 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002728 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002729 },
aurel327c664e22009-03-03 06:12:22 +00002730#endif
2731#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002732 {
2733 .name = "mem",
2734 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002735 .params = "",
2736 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002737 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002738 },
bellardb86bda52004-09-18 19:32:46 +00002739#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 {
Blue Swirl314e2982011-09-11 20:22:05 +00002741 .name = "mtree",
2742 .args_type = "",
2743 .params = "",
2744 .help = "show memory tree",
2745 .mhandler.info = do_info_mtree,
2746 },
2747 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 .name = "jit",
2749 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002750 .params = "",
2751 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002752 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002753 },
2754 {
2755 .name = "kvm",
2756 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 .params = "",
2758 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002759 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 },
2761 {
2762 .name = "numa",
2763 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002764 .params = "",
2765 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002766 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002767 },
2768 {
2769 .name = "usb",
2770 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002771 .params = "",
2772 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002773 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002774 },
2775 {
2776 .name = "usbhost",
2777 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 .params = "",
2779 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002780 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 },
2782 {
2783 .name = "profile",
2784 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 .params = "",
2786 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002787 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 },
2789 {
2790 .name = "capture",
2791 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 .params = "",
2793 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002794 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 },
2796 {
2797 .name = "snapshots",
2798 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .params = "",
2800 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002801 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 },
2803 {
2804 .name = "status",
2805 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 .params = "",
2807 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002808 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 },
2810 {
2811 .name = "pcmcia",
2812 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 .params = "",
2814 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002815 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 },
2817 {
2818 .name = "mice",
2819 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 .params = "",
2821 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002822 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 },
2824 {
2825 .name = "vnc",
2826 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 .params = "",
2828 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002829 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002831#if defined(CONFIG_SPICE)
2832 {
2833 .name = "spice",
2834 .args_type = "",
2835 .params = "",
2836 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002837 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002838 },
2839#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002840 {
2841 .name = "name",
2842 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002843 .params = "",
2844 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002845 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002846 },
2847 {
2848 .name = "uuid",
2849 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002850 .params = "",
2851 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002852 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002853 },
j_mayer76a66252007-03-07 08:32:30 +00002854#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002855 {
2856 .name = "cpustats",
2857 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002858 .params = "",
2859 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002860 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002861 },
j_mayer76a66252007-03-07 08:32:30 +00002862#endif
blueswir131a60e22007-10-26 18:42:59 +00002863#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002864 {
2865 .name = "usernet",
2866 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002867 .params = "",
2868 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002869 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002870 },
blueswir131a60e22007-10-26 18:42:59 +00002871#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002872 {
2873 .name = "migrate",
2874 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002875 .params = "",
2876 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002877 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 },
2879 {
2880 .name = "balloon",
2881 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002882 .params = "",
2883 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002884 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002885 },
2886 {
2887 .name = "qtree",
2888 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 .params = "",
2890 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002891 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002892 },
2893 {
2894 .name = "qdm",
2895 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002896 .params = "",
2897 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002898 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002899 },
2900 {
2901 .name = "roms",
2902 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 .params = "",
2904 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002905 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002906 },
Lluís6d8a7642011-08-31 20:30:43 +02002907#if defined(CONFIG_TRACE_SIMPLE)
Prerna Saxena22890ab2010-06-24 17:04:53 +05302908 {
2909 .name = "trace",
2910 .args_type = "",
2911 .params = "",
2912 .help = "show current contents of trace buffer",
2913 .mhandler.info = do_info_trace,
2914 },
Lluís31965ae2011-08-31 20:31:24 +02002915#endif
Prerna Saxena22890ab2010-06-24 17:04:53 +05302916 {
2917 .name = "trace-events",
2918 .args_type = "",
2919 .params = "",
2920 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002921 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302922 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002923 {
2924 .name = NULL,
2925 },
bellard9dc39cb2004-03-14 21:38:27 +00002926};
2927
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002928static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002929#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002930 { /* NULL */ },
2931};
2932
bellard9307c4c2004-04-04 12:57:25 +00002933/*******************************************************************/
2934
2935static const char *pch;
2936static jmp_buf expr_env;
2937
bellard92a31b12005-02-10 22:00:52 +00002938#define MD_TLONG 0
2939#define MD_I32 1
2940
bellard9307c4c2004-04-04 12:57:25 +00002941typedef struct MonitorDef {
2942 const char *name;
2943 int offset;
blueswir18662d652008-10-02 18:32:44 +00002944 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002945 int type;
bellard9307c4c2004-04-04 12:57:25 +00002946} MonitorDef;
2947
bellard57206fd2004-04-25 18:54:52 +00002948#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002949static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002950{
bellard6a00d602005-11-21 23:25:50 +00002951 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002952 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002953}
2954#endif
2955
bellarda541f292004-04-12 20:39:29 +00002956#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002957static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002958{
bellard6a00d602005-11-21 23:25:50 +00002959 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002960 unsigned int u;
2961 int i;
2962
2963 u = 0;
2964 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002965 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002966
2967 return u;
2968}
2969
blueswir18662d652008-10-02 18:32:44 +00002970static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002971{
bellard6a00d602005-11-21 23:25:50 +00002972 CPUState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00002973 return env->msr;
bellarda541f292004-04-12 20:39:29 +00002974}
2975
blueswir18662d652008-10-02 18:32:44 +00002976static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002977{
bellard6a00d602005-11-21 23:25:50 +00002978 CPUState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00002979 return env->xer;
bellarda541f292004-04-12 20:39:29 +00002980}
bellard9fddaa02004-05-21 12:59:32 +00002981
blueswir18662d652008-10-02 18:32:44 +00002982static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002983{
bellard6a00d602005-11-21 23:25:50 +00002984 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002985 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00002986}
2987
blueswir18662d652008-10-02 18:32:44 +00002988static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002989{
bellard6a00d602005-11-21 23:25:50 +00002990 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002991 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00002992}
2993
blueswir18662d652008-10-02 18:32:44 +00002994static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00002995{
bellard6a00d602005-11-21 23:25:50 +00002996 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002997 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00002998}
bellarda541f292004-04-12 20:39:29 +00002999#endif
3000
bellarde95c8d52004-09-30 22:22:08 +00003001#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003002#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003003static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003004{
bellard6a00d602005-11-21 23:25:50 +00003005 CPUState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003006
3007 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003008}
bellard7b936c02005-10-30 17:05:13 +00003009#endif
bellarde95c8d52004-09-30 22:22:08 +00003010
blueswir18662d652008-10-02 18:32:44 +00003011static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003012{
bellard6a00d602005-11-21 23:25:50 +00003013 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003014 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003015}
3016#endif
3017
blueswir18662d652008-10-02 18:32:44 +00003018static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003019#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003020
3021#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00003022 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00003023 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00003024 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003025
bellard9307c4c2004-04-04 12:57:25 +00003026 { "eax", offsetof(CPUState, regs[0]) },
3027 { "ecx", offsetof(CPUState, regs[1]) },
3028 { "edx", offsetof(CPUState, regs[2]) },
3029 { "ebx", offsetof(CPUState, regs[3]) },
3030 { "esp|sp", offsetof(CPUState, regs[4]) },
3031 { "ebp|fp", offsetof(CPUState, regs[5]) },
3032 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00003033 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003034#ifdef TARGET_X86_64
3035 { "r8", offsetof(CPUState, regs[8]) },
3036 { "r9", offsetof(CPUState, regs[9]) },
3037 { "r10", offsetof(CPUState, regs[10]) },
3038 { "r11", offsetof(CPUState, regs[11]) },
3039 { "r12", offsetof(CPUState, regs[12]) },
3040 { "r13", offsetof(CPUState, regs[13]) },
3041 { "r14", offsetof(CPUState, regs[14]) },
3042 { "r15", offsetof(CPUState, regs[15]) },
3043#endif
bellard9307c4c2004-04-04 12:57:25 +00003044 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00003045 { "eip", offsetof(CPUState, eip) },
3046 SEG("cs", R_CS)
3047 SEG("ds", R_DS)
3048 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003049 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003050 SEG("fs", R_FS)
3051 SEG("gs", R_GS)
3052 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003053#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003054 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00003055 { "r0", offsetof(CPUState, gpr[0]) },
3056 { "r1", offsetof(CPUState, gpr[1]) },
3057 { "r2", offsetof(CPUState, gpr[2]) },
3058 { "r3", offsetof(CPUState, gpr[3]) },
3059 { "r4", offsetof(CPUState, gpr[4]) },
3060 { "r5", offsetof(CPUState, gpr[5]) },
3061 { "r6", offsetof(CPUState, gpr[6]) },
3062 { "r7", offsetof(CPUState, gpr[7]) },
3063 { "r8", offsetof(CPUState, gpr[8]) },
3064 { "r9", offsetof(CPUState, gpr[9]) },
3065 { "r10", offsetof(CPUState, gpr[10]) },
3066 { "r11", offsetof(CPUState, gpr[11]) },
3067 { "r12", offsetof(CPUState, gpr[12]) },
3068 { "r13", offsetof(CPUState, gpr[13]) },
3069 { "r14", offsetof(CPUState, gpr[14]) },
3070 { "r15", offsetof(CPUState, gpr[15]) },
3071 { "r16", offsetof(CPUState, gpr[16]) },
3072 { "r17", offsetof(CPUState, gpr[17]) },
3073 { "r18", offsetof(CPUState, gpr[18]) },
3074 { "r19", offsetof(CPUState, gpr[19]) },
3075 { "r20", offsetof(CPUState, gpr[20]) },
3076 { "r21", offsetof(CPUState, gpr[21]) },
3077 { "r22", offsetof(CPUState, gpr[22]) },
3078 { "r23", offsetof(CPUState, gpr[23]) },
3079 { "r24", offsetof(CPUState, gpr[24]) },
3080 { "r25", offsetof(CPUState, gpr[25]) },
3081 { "r26", offsetof(CPUState, gpr[26]) },
3082 { "r27", offsetof(CPUState, gpr[27]) },
3083 { "r28", offsetof(CPUState, gpr[28]) },
3084 { "r29", offsetof(CPUState, gpr[29]) },
3085 { "r30", offsetof(CPUState, gpr[30]) },
3086 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003087 /* Floating point registers */
3088 { "f0", offsetof(CPUState, fpr[0]) },
3089 { "f1", offsetof(CPUState, fpr[1]) },
3090 { "f2", offsetof(CPUState, fpr[2]) },
3091 { "f3", offsetof(CPUState, fpr[3]) },
3092 { "f4", offsetof(CPUState, fpr[4]) },
3093 { "f5", offsetof(CPUState, fpr[5]) },
3094 { "f6", offsetof(CPUState, fpr[6]) },
3095 { "f7", offsetof(CPUState, fpr[7]) },
3096 { "f8", offsetof(CPUState, fpr[8]) },
3097 { "f9", offsetof(CPUState, fpr[9]) },
3098 { "f10", offsetof(CPUState, fpr[10]) },
3099 { "f11", offsetof(CPUState, fpr[11]) },
3100 { "f12", offsetof(CPUState, fpr[12]) },
3101 { "f13", offsetof(CPUState, fpr[13]) },
3102 { "f14", offsetof(CPUState, fpr[14]) },
3103 { "f15", offsetof(CPUState, fpr[15]) },
3104 { "f16", offsetof(CPUState, fpr[16]) },
3105 { "f17", offsetof(CPUState, fpr[17]) },
3106 { "f18", offsetof(CPUState, fpr[18]) },
3107 { "f19", offsetof(CPUState, fpr[19]) },
3108 { "f20", offsetof(CPUState, fpr[20]) },
3109 { "f21", offsetof(CPUState, fpr[21]) },
3110 { "f22", offsetof(CPUState, fpr[22]) },
3111 { "f23", offsetof(CPUState, fpr[23]) },
3112 { "f24", offsetof(CPUState, fpr[24]) },
3113 { "f25", offsetof(CPUState, fpr[25]) },
3114 { "f26", offsetof(CPUState, fpr[26]) },
3115 { "f27", offsetof(CPUState, fpr[27]) },
3116 { "f28", offsetof(CPUState, fpr[28]) },
3117 { "f29", offsetof(CPUState, fpr[29]) },
3118 { "f30", offsetof(CPUState, fpr[30]) },
3119 { "f31", offsetof(CPUState, fpr[31]) },
3120 { "fpscr", offsetof(CPUState, fpscr) },
3121 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00003122 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00003123 { "lr", offsetof(CPUState, lr) },
3124 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003125 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003126 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003127 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003128 { "msr", 0, &monitor_get_msr, },
3129 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003130 { "tbu", 0, &monitor_get_tbu, },
3131 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003132#if defined(TARGET_PPC64)
3133 /* Address space register */
3134 { "asr", offsetof(CPUState, asr) },
3135#endif
3136 /* Segment registers */
David Gibsonbb593902011-04-01 15:15:15 +11003137 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
bellarda541f292004-04-12 20:39:29 +00003138 { "sr0", offsetof(CPUState, sr[0]) },
3139 { "sr1", offsetof(CPUState, sr[1]) },
3140 { "sr2", offsetof(CPUState, sr[2]) },
3141 { "sr3", offsetof(CPUState, sr[3]) },
3142 { "sr4", offsetof(CPUState, sr[4]) },
3143 { "sr5", offsetof(CPUState, sr[5]) },
3144 { "sr6", offsetof(CPUState, sr[6]) },
3145 { "sr7", offsetof(CPUState, sr[7]) },
3146 { "sr8", offsetof(CPUState, sr[8]) },
3147 { "sr9", offsetof(CPUState, sr[9]) },
3148 { "sr10", offsetof(CPUState, sr[10]) },
3149 { "sr11", offsetof(CPUState, sr[11]) },
3150 { "sr12", offsetof(CPUState, sr[12]) },
3151 { "sr13", offsetof(CPUState, sr[13]) },
3152 { "sr14", offsetof(CPUState, sr[14]) },
3153 { "sr15", offsetof(CPUState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003154 /* Too lazy to put BATs... */
3155 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3156
3157 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3158 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3159 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3160 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3161 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3162 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3163 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3164 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3165 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3166 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3167 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3168 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3169 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3170 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3171 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3172 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3173 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3174 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3175 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3176 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3177 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3178 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3179 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3180 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3181 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3182 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3183 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3184 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3185 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3186 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3187 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3188 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3189 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3190 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3191 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3192 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3193 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3194 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3195 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3196 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3197 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3198 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3199 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3200 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3201 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3202 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3203 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3204 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3205 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3206 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3207 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3208 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3209 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3210 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3211 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3212 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3213 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3214 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3215 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3216 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3217 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3218 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3219 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3220 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3221 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3222 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3223
bellarde95c8d52004-09-30 22:22:08 +00003224#elif defined(TARGET_SPARC)
3225 { "g0", offsetof(CPUState, gregs[0]) },
3226 { "g1", offsetof(CPUState, gregs[1]) },
3227 { "g2", offsetof(CPUState, gregs[2]) },
3228 { "g3", offsetof(CPUState, gregs[3]) },
3229 { "g4", offsetof(CPUState, gregs[4]) },
3230 { "g5", offsetof(CPUState, gregs[5]) },
3231 { "g6", offsetof(CPUState, gregs[6]) },
3232 { "g7", offsetof(CPUState, gregs[7]) },
3233 { "o0", 0, monitor_get_reg },
3234 { "o1", 1, monitor_get_reg },
3235 { "o2", 2, monitor_get_reg },
3236 { "o3", 3, monitor_get_reg },
3237 { "o4", 4, monitor_get_reg },
3238 { "o5", 5, monitor_get_reg },
3239 { "o6", 6, monitor_get_reg },
3240 { "o7", 7, monitor_get_reg },
3241 { "l0", 8, monitor_get_reg },
3242 { "l1", 9, monitor_get_reg },
3243 { "l2", 10, monitor_get_reg },
3244 { "l3", 11, monitor_get_reg },
3245 { "l4", 12, monitor_get_reg },
3246 { "l5", 13, monitor_get_reg },
3247 { "l6", 14, monitor_get_reg },
3248 { "l7", 15, monitor_get_reg },
3249 { "i0", 16, monitor_get_reg },
3250 { "i1", 17, monitor_get_reg },
3251 { "i2", 18, monitor_get_reg },
3252 { "i3", 19, monitor_get_reg },
3253 { "i4", 20, monitor_get_reg },
3254 { "i5", 21, monitor_get_reg },
3255 { "i6", 22, monitor_get_reg },
3256 { "i7", 23, monitor_get_reg },
3257 { "pc", offsetof(CPUState, pc) },
3258 { "npc", offsetof(CPUState, npc) },
3259 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00003260#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003261 { "psr", 0, &monitor_get_psr, },
3262 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003263#endif
bellarde95c8d52004-09-30 22:22:08 +00003264 { "tbr", offsetof(CPUState, tbr) },
3265 { "fsr", offsetof(CPUState, fsr) },
3266 { "f0", offsetof(CPUState, fpr[0]) },
3267 { "f1", offsetof(CPUState, fpr[1]) },
3268 { "f2", offsetof(CPUState, fpr[2]) },
3269 { "f3", offsetof(CPUState, fpr[3]) },
3270 { "f4", offsetof(CPUState, fpr[4]) },
3271 { "f5", offsetof(CPUState, fpr[5]) },
3272 { "f6", offsetof(CPUState, fpr[6]) },
3273 { "f7", offsetof(CPUState, fpr[7]) },
3274 { "f8", offsetof(CPUState, fpr[8]) },
3275 { "f9", offsetof(CPUState, fpr[9]) },
3276 { "f10", offsetof(CPUState, fpr[10]) },
3277 { "f11", offsetof(CPUState, fpr[11]) },
3278 { "f12", offsetof(CPUState, fpr[12]) },
3279 { "f13", offsetof(CPUState, fpr[13]) },
3280 { "f14", offsetof(CPUState, fpr[14]) },
3281 { "f15", offsetof(CPUState, fpr[15]) },
3282 { "f16", offsetof(CPUState, fpr[16]) },
3283 { "f17", offsetof(CPUState, fpr[17]) },
3284 { "f18", offsetof(CPUState, fpr[18]) },
3285 { "f19", offsetof(CPUState, fpr[19]) },
3286 { "f20", offsetof(CPUState, fpr[20]) },
3287 { "f21", offsetof(CPUState, fpr[21]) },
3288 { "f22", offsetof(CPUState, fpr[22]) },
3289 { "f23", offsetof(CPUState, fpr[23]) },
3290 { "f24", offsetof(CPUState, fpr[24]) },
3291 { "f25", offsetof(CPUState, fpr[25]) },
3292 { "f26", offsetof(CPUState, fpr[26]) },
3293 { "f27", offsetof(CPUState, fpr[27]) },
3294 { "f28", offsetof(CPUState, fpr[28]) },
3295 { "f29", offsetof(CPUState, fpr[29]) },
3296 { "f30", offsetof(CPUState, fpr[30]) },
3297 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00003298#ifdef TARGET_SPARC64
3299 { "f32", offsetof(CPUState, fpr[32]) },
3300 { "f34", offsetof(CPUState, fpr[34]) },
3301 { "f36", offsetof(CPUState, fpr[36]) },
3302 { "f38", offsetof(CPUState, fpr[38]) },
3303 { "f40", offsetof(CPUState, fpr[40]) },
3304 { "f42", offsetof(CPUState, fpr[42]) },
3305 { "f44", offsetof(CPUState, fpr[44]) },
3306 { "f46", offsetof(CPUState, fpr[46]) },
3307 { "f48", offsetof(CPUState, fpr[48]) },
3308 { "f50", offsetof(CPUState, fpr[50]) },
3309 { "f52", offsetof(CPUState, fpr[52]) },
3310 { "f54", offsetof(CPUState, fpr[54]) },
3311 { "f56", offsetof(CPUState, fpr[56]) },
3312 { "f58", offsetof(CPUState, fpr[58]) },
3313 { "f60", offsetof(CPUState, fpr[60]) },
3314 { "f62", offsetof(CPUState, fpr[62]) },
3315 { "asi", offsetof(CPUState, asi) },
3316 { "pstate", offsetof(CPUState, pstate) },
3317 { "cansave", offsetof(CPUState, cansave) },
3318 { "canrestore", offsetof(CPUState, canrestore) },
3319 { "otherwin", offsetof(CPUState, otherwin) },
3320 { "wstate", offsetof(CPUState, wstate) },
3321 { "cleanwin", offsetof(CPUState, cleanwin) },
3322 { "fprs", offsetof(CPUState, fprs) },
3323#endif
bellard9307c4c2004-04-04 12:57:25 +00003324#endif
3325 { NULL },
3326};
3327
aliguori376253e2009-03-05 23:01:23 +00003328static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003329{
aliguori376253e2009-03-05 23:01:23 +00003330 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003331 longjmp(expr_env, 1);
3332}
3333
Markus Armbruster09b94182010-01-20 13:07:30 +01003334/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003335static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003336{
blueswir18662d652008-10-02 18:32:44 +00003337 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003338 void *ptr;
3339
bellard9307c4c2004-04-04 12:57:25 +00003340 for(md = monitor_defs; md->name != NULL; md++) {
3341 if (compare_cmd(name, md->name)) {
3342 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003343 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003344 } else {
bellard6a00d602005-11-21 23:25:50 +00003345 CPUState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003346 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003347 switch(md->type) {
3348 case MD_I32:
3349 *pval = *(int32_t *)ptr;
3350 break;
3351 case MD_TLONG:
3352 *pval = *(target_long *)ptr;
3353 break;
3354 default:
3355 *pval = 0;
3356 break;
3357 }
bellard9307c4c2004-04-04 12:57:25 +00003358 }
3359 return 0;
3360 }
3361 }
3362 return -1;
3363}
3364
3365static void next(void)
3366{
Blue Swirl660f11b2009-07-31 21:16:51 +00003367 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003368 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003369 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003370 pch++;
3371 }
3372}
3373
aliguori376253e2009-03-05 23:01:23 +00003374static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003375
aliguori376253e2009-03-05 23:01:23 +00003376static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003377{
blueswir1c2efc952007-09-25 17:28:42 +00003378 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003379 char *p;
bellard6a00d602005-11-21 23:25:50 +00003380 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003381
3382 switch(*pch) {
3383 case '+':
3384 next();
aliguori376253e2009-03-05 23:01:23 +00003385 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003386 break;
3387 case '-':
3388 next();
aliguori376253e2009-03-05 23:01:23 +00003389 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003390 break;
3391 case '~':
3392 next();
aliguori376253e2009-03-05 23:01:23 +00003393 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003394 break;
3395 case '(':
3396 next();
aliguori376253e2009-03-05 23:01:23 +00003397 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003398 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003399 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003400 }
3401 next();
3402 break;
bellard81d09122004-07-14 17:21:37 +00003403 case '\'':
3404 pch++;
3405 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003406 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003407 n = *pch;
3408 pch++;
3409 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003410 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003411 next();
3412 break;
bellard9307c4c2004-04-04 12:57:25 +00003413 case '$':
3414 {
3415 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003416 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003417
bellard9307c4c2004-04-04 12:57:25 +00003418 pch++;
3419 q = buf;
3420 while ((*pch >= 'a' && *pch <= 'z') ||
3421 (*pch >= 'A' && *pch <= 'Z') ||
3422 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003423 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003424 if ((q - buf) < sizeof(buf) - 1)
3425 *q++ = *pch;
3426 pch++;
3427 }
blueswir1cd390082008-11-16 13:53:32 +00003428 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003429 pch++;
3430 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003431 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003432 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003433 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003434 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003435 }
3436 break;
3437 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003438 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003439 n = 0;
3440 break;
3441 default:
blueswir17743e582007-09-24 18:39:04 +00003442#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003443 n = strtoull(pch, &p, 0);
3444#else
bellard9307c4c2004-04-04 12:57:25 +00003445 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003446#endif
bellard9307c4c2004-04-04 12:57:25 +00003447 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003448 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003449 }
3450 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003451 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003452 pch++;
3453 break;
3454 }
3455 return n;
3456}
3457
3458
aliguori376253e2009-03-05 23:01:23 +00003459static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003460{
blueswir1c2efc952007-09-25 17:28:42 +00003461 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003462 int op;
ths3b46e622007-09-17 08:09:54 +00003463
aliguori376253e2009-03-05 23:01:23 +00003464 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003465 for(;;) {
3466 op = *pch;
3467 if (op != '*' && op != '/' && op != '%')
3468 break;
3469 next();
aliguori376253e2009-03-05 23:01:23 +00003470 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003471 switch(op) {
3472 default:
3473 case '*':
3474 val *= val2;
3475 break;
3476 case '/':
3477 case '%':
ths5fafdf22007-09-16 21:08:06 +00003478 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003479 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003480 if (op == '/')
3481 val /= val2;
3482 else
3483 val %= val2;
3484 break;
3485 }
3486 }
3487 return val;
3488}
3489
aliguori376253e2009-03-05 23:01:23 +00003490static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003491{
blueswir1c2efc952007-09-25 17:28:42 +00003492 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003493 int op;
bellard9307c4c2004-04-04 12:57:25 +00003494
aliguori376253e2009-03-05 23:01:23 +00003495 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003496 for(;;) {
3497 op = *pch;
3498 if (op != '&' && op != '|' && op != '^')
3499 break;
3500 next();
aliguori376253e2009-03-05 23:01:23 +00003501 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003502 switch(op) {
3503 default:
3504 case '&':
3505 val &= val2;
3506 break;
3507 case '|':
3508 val |= val2;
3509 break;
3510 case '^':
3511 val ^= val2;
3512 break;
3513 }
3514 }
3515 return val;
3516}
3517
aliguori376253e2009-03-05 23:01:23 +00003518static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003519{
blueswir1c2efc952007-09-25 17:28:42 +00003520 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003521 int op;
bellard9307c4c2004-04-04 12:57:25 +00003522
aliguori376253e2009-03-05 23:01:23 +00003523 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003524 for(;;) {
3525 op = *pch;
3526 if (op != '+' && op != '-')
3527 break;
3528 next();
aliguori376253e2009-03-05 23:01:23 +00003529 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003530 if (op == '+')
3531 val += val2;
3532 else
3533 val -= val2;
3534 }
3535 return val;
3536}
3537
aliguori376253e2009-03-05 23:01:23 +00003538static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003539{
3540 pch = *pp;
3541 if (setjmp(expr_env)) {
3542 *pp = pch;
3543 return -1;
3544 }
blueswir1cd390082008-11-16 13:53:32 +00003545 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003546 pch++;
aliguori376253e2009-03-05 23:01:23 +00003547 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003548 *pp = pch;
3549 return 0;
3550}
3551
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003552static int get_double(Monitor *mon, double *pval, const char **pp)
3553{
3554 const char *p = *pp;
3555 char *tailp;
3556 double d;
3557
3558 d = strtod(p, &tailp);
3559 if (tailp == p) {
3560 monitor_printf(mon, "Number expected\n");
3561 return -1;
3562 }
3563 if (d != d || d - d != 0) {
3564 /* NaN or infinity */
3565 monitor_printf(mon, "Bad number\n");
3566 return -1;
3567 }
3568 *pval = d;
3569 *pp = tailp;
3570 return 0;
3571}
3572
bellard9307c4c2004-04-04 12:57:25 +00003573static int get_str(char *buf, int buf_size, const char **pp)
3574{
3575 const char *p;
3576 char *q;
3577 int c;
3578
bellard81d09122004-07-14 17:21:37 +00003579 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003580 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003581 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003582 p++;
3583 if (*p == '\0') {
3584 fail:
bellard81d09122004-07-14 17:21:37 +00003585 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003586 *pp = p;
3587 return -1;
3588 }
bellard9307c4c2004-04-04 12:57:25 +00003589 if (*p == '\"') {
3590 p++;
3591 while (*p != '\0' && *p != '\"') {
3592 if (*p == '\\') {
3593 p++;
3594 c = *p++;
3595 switch(c) {
3596 case 'n':
3597 c = '\n';
3598 break;
3599 case 'r':
3600 c = '\r';
3601 break;
3602 case '\\':
3603 case '\'':
3604 case '\"':
3605 break;
3606 default:
3607 qemu_printf("unsupported escape code: '\\%c'\n", c);
3608 goto fail;
3609 }
3610 if ((q - buf) < buf_size - 1) {
3611 *q++ = c;
3612 }
3613 } else {
3614 if ((q - buf) < buf_size - 1) {
3615 *q++ = *p;
3616 }
3617 p++;
3618 }
3619 }
3620 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003621 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003622 goto fail;
3623 }
3624 p++;
3625 } else {
blueswir1cd390082008-11-16 13:53:32 +00003626 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003627 if ((q - buf) < buf_size - 1) {
3628 *q++ = *p;
3629 }
3630 p++;
3631 }
bellard9307c4c2004-04-04 12:57:25 +00003632 }
bellard81d09122004-07-14 17:21:37 +00003633 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003634 *pp = p;
3635 return 0;
3636}
3637
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003638/*
3639 * Store the command-name in cmdname, and return a pointer to
3640 * the remaining of the command string.
3641 */
3642static const char *get_command_name(const char *cmdline,
3643 char *cmdname, size_t nlen)
3644{
3645 size_t len;
3646 const char *p, *pstart;
3647
3648 p = cmdline;
3649 while (qemu_isspace(*p))
3650 p++;
3651 if (*p == '\0')
3652 return NULL;
3653 pstart = p;
3654 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3655 p++;
3656 len = p - pstart;
3657 if (len > nlen - 1)
3658 len = nlen - 1;
3659 memcpy(cmdname, pstart, len);
3660 cmdname[len] = '\0';
3661 return p;
3662}
3663
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003664/**
3665 * Read key of 'type' into 'key' and return the current
3666 * 'type' pointer.
3667 */
3668static char *key_get_info(const char *type, char **key)
3669{
3670 size_t len;
3671 char *p, *str;
3672
3673 if (*type == ',')
3674 type++;
3675
3676 p = strchr(type, ':');
3677 if (!p) {
3678 *key = NULL;
3679 return NULL;
3680 }
3681 len = p - type;
3682
Anthony Liguori7267c092011-08-20 22:09:37 -05003683 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003684 memcpy(str, type, len);
3685 str[len] = '\0';
3686
3687 *key = str;
3688 return ++p;
3689}
3690
bellard9307c4c2004-04-04 12:57:25 +00003691static int default_fmt_format = 'x';
3692static int default_fmt_size = 4;
3693
3694#define MAX_ARGS 16
3695
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003696static int is_valid_option(const char *c, const char *typestr)
3697{
3698 char option[3];
3699
3700 option[0] = '-';
3701 option[1] = *c;
3702 option[2] = '\0';
3703
3704 typestr = strstr(typestr, option);
3705 return (typestr != NULL);
3706}
3707
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003708static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3709 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003710{
3711 const mon_cmd_t *cmd;
3712
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003713 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003714 if (compare_cmd(cmdname, cmd->name)) {
3715 return cmd;
3716 }
3717 }
3718
3719 return NULL;
3720}
3721
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003722static const mon_cmd_t *monitor_find_command(const char *cmdname)
3723{
3724 return search_dispatch_table(mon_cmds, cmdname);
3725}
3726
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003727static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3728{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003729 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003730}
3731
Anthony Liguoric227f092009-10-01 16:12:16 -05003732static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003733 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003734 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003735{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003736 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003737 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003738 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003739 char cmdname[256];
3740 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003741 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003742
3743#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003744 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003745#endif
ths3b46e622007-09-17 08:09:54 +00003746
bellard9307c4c2004-04-04 12:57:25 +00003747 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003748 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3749 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003750 return NULL;
ths3b46e622007-09-17 08:09:54 +00003751
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003752 cmd = monitor_find_command(cmdname);
3753 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003754 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003755 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003756 }
bellard9307c4c2004-04-04 12:57:25 +00003757
bellard9307c4c2004-04-04 12:57:25 +00003758 /* parse the parameters */
3759 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003760 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003761 typestr = key_get_info(typestr, &key);
3762 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003763 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003764 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003765 typestr++;
3766 switch(c) {
3767 case 'F':
bellard81d09122004-07-14 17:21:37 +00003768 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003769 case 's':
3770 {
3771 int ret;
ths3b46e622007-09-17 08:09:54 +00003772
blueswir1cd390082008-11-16 13:53:32 +00003773 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003774 p++;
3775 if (*typestr == '?') {
3776 typestr++;
3777 if (*p == '\0') {
3778 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003779 break;
bellard9307c4c2004-04-04 12:57:25 +00003780 }
3781 }
3782 ret = get_str(buf, sizeof(buf), &p);
3783 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003784 switch(c) {
3785 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003786 monitor_printf(mon, "%s: filename expected\n",
3787 cmdname);
bellard81d09122004-07-14 17:21:37 +00003788 break;
3789 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003790 monitor_printf(mon, "%s: block device name expected\n",
3791 cmdname);
bellard81d09122004-07-14 17:21:37 +00003792 break;
3793 default:
aliguori376253e2009-03-05 23:01:23 +00003794 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003795 break;
3796 }
bellard9307c4c2004-04-04 12:57:25 +00003797 goto fail;
3798 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003799 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003800 }
3801 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003802 case 'O':
3803 {
3804 QemuOptsList *opts_list;
3805 QemuOpts *opts;
3806
3807 opts_list = qemu_find_opts(key);
3808 if (!opts_list || opts_list->desc->name) {
3809 goto bad_type;
3810 }
3811 while (qemu_isspace(*p)) {
3812 p++;
3813 }
3814 if (!*p)
3815 break;
3816 if (get_str(buf, sizeof(buf), &p) < 0) {
3817 goto fail;
3818 }
3819 opts = qemu_opts_parse(opts_list, buf, 1);
3820 if (!opts) {
3821 goto fail;
3822 }
3823 qemu_opts_to_qdict(opts, qdict);
3824 qemu_opts_del(opts);
3825 }
3826 break;
bellard9307c4c2004-04-04 12:57:25 +00003827 case '/':
3828 {
3829 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003830
blueswir1cd390082008-11-16 13:53:32 +00003831 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003832 p++;
3833 if (*p == '/') {
3834 /* format found */
3835 p++;
3836 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003837 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003838 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003839 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003840 count = count * 10 + (*p - '0');
3841 p++;
3842 }
3843 }
3844 size = -1;
3845 format = -1;
3846 for(;;) {
3847 switch(*p) {
3848 case 'o':
3849 case 'd':
3850 case 'u':
3851 case 'x':
3852 case 'i':
3853 case 'c':
3854 format = *p++;
3855 break;
3856 case 'b':
3857 size = 1;
3858 p++;
3859 break;
3860 case 'h':
3861 size = 2;
3862 p++;
3863 break;
3864 case 'w':
3865 size = 4;
3866 p++;
3867 break;
3868 case 'g':
3869 case 'L':
3870 size = 8;
3871 p++;
3872 break;
3873 default:
3874 goto next;
3875 }
3876 }
3877 next:
blueswir1cd390082008-11-16 13:53:32 +00003878 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003879 monitor_printf(mon, "invalid char in format: '%c'\n",
3880 *p);
bellard9307c4c2004-04-04 12:57:25 +00003881 goto fail;
3882 }
bellard9307c4c2004-04-04 12:57:25 +00003883 if (format < 0)
3884 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003885 if (format != 'i') {
3886 /* for 'i', not specifying a size gives -1 as size */
3887 if (size < 0)
3888 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003889 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003890 }
bellard9307c4c2004-04-04 12:57:25 +00003891 default_fmt_format = format;
3892 } else {
3893 count = 1;
3894 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003895 if (format != 'i') {
3896 size = default_fmt_size;
3897 } else {
3898 size = -1;
3899 }
bellard9307c4c2004-04-04 12:57:25 +00003900 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003901 qdict_put(qdict, "count", qint_from_int(count));
3902 qdict_put(qdict, "format", qint_from_int(format));
3903 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003904 }
3905 break;
3906 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003907 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003908 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003909 {
blueswir1c2efc952007-09-25 17:28:42 +00003910 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003911
blueswir1cd390082008-11-16 13:53:32 +00003912 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003913 p++;
bellard34405572004-06-08 00:55:58 +00003914 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003915 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003916 if (*p == '\0') {
3917 typestr++;
3918 break;
3919 }
bellard34405572004-06-08 00:55:58 +00003920 } else {
3921 if (*p == '.') {
3922 p++;
blueswir1cd390082008-11-16 13:53:32 +00003923 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003924 p++;
bellard34405572004-06-08 00:55:58 +00003925 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003926 typestr++;
3927 break;
bellard34405572004-06-08 00:55:58 +00003928 }
3929 }
bellard13224a82006-07-14 22:03:35 +00003930 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003931 }
aliguori376253e2009-03-05 23:01:23 +00003932 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003933 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003934 /* Check if 'i' is greater than 32-bit */
3935 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3936 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3937 monitor_printf(mon, "integer is for 32-bit values\n");
3938 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003939 } else if (c == 'M') {
3940 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003941 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003942 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003943 }
3944 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003945 case 'o':
3946 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003947 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003948 char *end;
3949
3950 while (qemu_isspace(*p)) {
3951 p++;
3952 }
3953 if (*typestr == '?') {
3954 typestr++;
3955 if (*p == '\0') {
3956 break;
3957 }
3958 }
3959 val = strtosz(p, &end);
3960 if (val < 0) {
3961 monitor_printf(mon, "invalid size\n");
3962 goto fail;
3963 }
3964 qdict_put(qdict, key, qint_from_int(val));
3965 p = end;
3966 }
3967 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003968 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003969 {
3970 double val;
3971
3972 while (qemu_isspace(*p))
3973 p++;
3974 if (*typestr == '?') {
3975 typestr++;
3976 if (*p == '\0') {
3977 break;
3978 }
3979 }
3980 if (get_double(mon, &val, &p) < 0) {
3981 goto fail;
3982 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02003983 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01003984 switch (*p) {
3985 case 'm':
3986 val /= 1e3; p += 2; break;
3987 case 'u':
3988 val /= 1e6; p += 2; break;
3989 case 'n':
3990 val /= 1e9; p += 2; break;
3991 }
3992 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003993 if (*p && !qemu_isspace(*p)) {
3994 monitor_printf(mon, "Unknown unit suffix\n");
3995 goto fail;
3996 }
3997 qdict_put(qdict, key, qfloat_from_double(val));
3998 }
3999 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004000 case 'b':
4001 {
4002 const char *beg;
4003 int val;
4004
4005 while (qemu_isspace(*p)) {
4006 p++;
4007 }
4008 beg = p;
4009 while (qemu_isgraph(*p)) {
4010 p++;
4011 }
4012 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4013 val = 1;
4014 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4015 val = 0;
4016 } else {
4017 monitor_printf(mon, "Expected 'on' or 'off'\n");
4018 goto fail;
4019 }
4020 qdict_put(qdict, key, qbool_from_int(val));
4021 }
4022 break;
bellard9307c4c2004-04-04 12:57:25 +00004023 case '-':
4024 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004025 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004026 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004027 /* option */
ths3b46e622007-09-17 08:09:54 +00004028
bellard9307c4c2004-04-04 12:57:25 +00004029 c = *typestr++;
4030 if (c == '\0')
4031 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004032 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004033 p++;
bellard9307c4c2004-04-04 12:57:25 +00004034 if (*p == '-') {
4035 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004036 if(c != *p) {
4037 if(!is_valid_option(p, typestr)) {
4038
4039 monitor_printf(mon, "%s: unsupported option -%c\n",
4040 cmdname, *p);
4041 goto fail;
4042 } else {
4043 skip_key = 1;
4044 }
bellard9307c4c2004-04-04 12:57:25 +00004045 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004046 if(skip_key) {
4047 p = tmp;
4048 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004049 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004050 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004051 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004052 }
bellard9307c4c2004-04-04 12:57:25 +00004053 }
bellard9307c4c2004-04-04 12:57:25 +00004054 }
4055 break;
4056 default:
4057 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004058 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004059 goto fail;
4060 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004061 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004062 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004063 }
4064 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004065 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004066 p++;
4067 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004068 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4069 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004070 goto fail;
4071 }
4072
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004073 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004074
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004075fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004076 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004077 return NULL;
4078}
4079
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004080void monitor_set_error(Monitor *mon, QError *qerror)
4081{
4082 /* report only the first error */
4083 if (!mon->error) {
4084 mon->error = qerror;
4085 } else {
4086 MON_DEBUG("Additional error report at %s:%d\n",
4087 qerror->file, qerror->linenr);
4088 QDECREF(qerror);
4089 }
4090}
4091
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004092static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4093{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004094 if (ret && !monitor_has_error(mon)) {
4095 /*
4096 * If it returns failure, it must have passed on error.
4097 *
4098 * Action: Report an internal error to the client if in QMP.
4099 */
4100 qerror_report(QERR_UNDEFINED_ERROR);
4101 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4102 cmd->name);
4103 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004104
4105#ifdef CONFIG_DEBUG_MONITOR
Luiz Capitulino6d441432010-11-22 16:35:09 -02004106 if (!ret && monitor_has_error(mon)) {
4107 /*
4108 * If it returns success, it must not have passed an error.
4109 *
4110 * Action: Report the passed error to the client.
4111 */
4112 MON_DEBUG("command '%s' returned success but passed an error\n",
4113 cmd->name);
Markus Armbrustercde0fc72010-03-02 14:56:34 +01004114 }
Luiz Capitulino6d441432010-11-22 16:35:09 -02004115
4116 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4117 /*
4118 * Handlers should not call Monitor print functions.
4119 *
4120 * Action: Ignore them in QMP.
4121 *
4122 * (XXX: we don't check any 'info' or 'query' command here
4123 * because the user print function _is_ called by do_info(), hence
4124 * we will trigger this check. This problem will go away when we
4125 * make 'query' commands real and kill do_info())
4126 */
4127 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4128 cmd->name, mon_print_count_get(mon));
4129 }
4130#endif
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004131}
4132
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004133static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004134{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004135 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004136 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004137
4138 qdict = qdict_new();
4139
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004140 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004141 if (!cmd)
4142 goto out;
4143
Luiz Capitulino4903de02010-09-16 11:01:32 -03004144 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004145 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004146 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004147 QObject *data = NULL;
4148
4149 /* XXX: ignores the error code */
4150 cmd->mhandler.cmd_new(mon, qdict, &data);
4151 assert(!monitor_has_error(mon));
4152 if (data) {
4153 cmd->user_print(mon, data);
4154 qobject_decref(data);
4155 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004156 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004157 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004158 }
4159
Luiz Capitulino13917be2009-10-07 13:41:54 -03004160out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004161 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004162}
4163
bellard81d09122004-07-14 17:21:37 +00004164static void cmd_completion(const char *name, const char *list)
4165{
4166 const char *p, *pstart;
4167 char cmd[128];
4168 int len;
4169
4170 p = list;
4171 for(;;) {
4172 pstart = p;
4173 p = strchr(p, '|');
4174 if (!p)
4175 p = pstart + strlen(pstart);
4176 len = p - pstart;
4177 if (len > sizeof(cmd) - 2)
4178 len = sizeof(cmd) - 2;
4179 memcpy(cmd, pstart, len);
4180 cmd[len] = '\0';
4181 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004182 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004183 }
4184 if (*p == '\0')
4185 break;
4186 p++;
4187 }
4188}
4189
4190static void file_completion(const char *input)
4191{
4192 DIR *ffs;
4193 struct dirent *d;
4194 char path[1024];
4195 char file[1024], file_prefix[1024];
4196 int input_path_len;
4197 const char *p;
4198
ths5fafdf22007-09-16 21:08:06 +00004199 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004200 if (!p) {
4201 input_path_len = 0;
4202 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004203 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004204 } else {
4205 input_path_len = p - input + 1;
4206 memcpy(path, input, input_path_len);
4207 if (input_path_len > sizeof(path) - 1)
4208 input_path_len = sizeof(path) - 1;
4209 path[input_path_len] = '\0';
4210 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4211 }
4212#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004213 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4214 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004215#endif
4216 ffs = opendir(path);
4217 if (!ffs)
4218 return;
4219 for(;;) {
4220 struct stat sb;
4221 d = readdir(ffs);
4222 if (!d)
4223 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004224
4225 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4226 continue;
4227 }
4228
bellard81d09122004-07-14 17:21:37 +00004229 if (strstart(d->d_name, file_prefix, NULL)) {
4230 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004231 if (input_path_len < sizeof(file))
4232 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4233 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004234 /* stat the file to find out if it's a directory.
4235 * In that case add a slash to speed up typing long paths
4236 */
4237 stat(file, &sb);
4238 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00004239 pstrcat(file, sizeof(file), "/");
aliguori731b0362009-03-05 23:01:42 +00004240 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004241 }
4242 }
4243 closedir(ffs);
4244}
4245
aliguori51de9762009-03-05 23:00:43 +00004246static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004247{
aliguori51de9762009-03-05 23:00:43 +00004248 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004249 const char *input = opaque;
4250
4251 if (input[0] == '\0' ||
4252 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004253 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004254 }
4255}
4256
4257/* NOTE: this parser is an approximate form of the real command parser */
4258static void parse_cmdline(const char *cmdline,
4259 int *pnb_args, char **args)
4260{
4261 const char *p;
4262 int nb_args, ret;
4263 char buf[1024];
4264
4265 p = cmdline;
4266 nb_args = 0;
4267 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004268 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004269 p++;
4270 if (*p == '\0')
4271 break;
4272 if (nb_args >= MAX_ARGS)
4273 break;
4274 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004275 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004276 nb_args++;
4277 if (ret < 0)
4278 break;
4279 }
4280 *pnb_args = nb_args;
4281}
4282
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004283static const char *next_arg_type(const char *typestr)
4284{
4285 const char *p = strchr(typestr, ':');
4286 return (p != NULL ? ++p : typestr);
4287}
4288
aliguori4c36ba32009-03-05 23:01:37 +00004289static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004290{
4291 const char *cmdname;
4292 char *args[MAX_ARGS];
4293 int nb_args, i, len;
4294 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004295 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004296 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004297
4298 parse_cmdline(cmdline, &nb_args, args);
4299#ifdef DEBUG_COMPLETION
4300 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004301 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004302 }
4303#endif
4304
4305 /* if the line ends with a space, it means we want to complete the
4306 next arg */
4307 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004308 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004309 if (nb_args >= MAX_ARGS) {
4310 goto cleanup;
4311 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004312 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004313 }
4314 if (nb_args <= 1) {
4315 /* command completion */
4316 if (nb_args == 0)
4317 cmdname = "";
4318 else
4319 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004320 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004321 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004322 cmd_completion(cmdname, cmd->name);
4323 }
4324 } else {
4325 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004326 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4327 if (compare_cmd(args[0], cmd->name)) {
4328 break;
4329 }
bellard81d09122004-07-14 17:21:37 +00004330 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004331 if (!cmd->name) {
4332 goto cleanup;
4333 }
4334
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004335 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004336 for(i = 0; i < nb_args - 2; i++) {
4337 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004338 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004339 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004340 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004341 }
4342 }
4343 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004344 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004345 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004346 }
bellard81d09122004-07-14 17:21:37 +00004347 switch(*ptype) {
4348 case 'F':
4349 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004350 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004351 file_completion(str);
4352 break;
4353 case 'B':
4354 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004355 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004356 bdrv_iterate(block_completion_it, (void *)str);
4357 break;
bellard7fe48482004-10-09 18:08:01 +00004358 case 's':
4359 /* XXX: more generic ? */
4360 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004361 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004362 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4363 cmd_completion(str, cmd->name);
4364 }
bellard64866c32006-05-07 18:03:31 +00004365 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004366 char *sep = strrchr(str, '-');
4367 if (sep)
4368 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004369 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004370 for(key = key_defs; key->name != NULL; key++) {
4371 cmd_completion(str, key->name);
4372 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004373 } else if (!strcmp(cmd->name, "help|?")) {
4374 readline_set_completion_index(cur_mon->rs, strlen(str));
4375 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4376 cmd_completion(str, cmd->name);
4377 }
bellard7fe48482004-10-09 18:08:01 +00004378 }
4379 break;
bellard81d09122004-07-14 17:21:37 +00004380 default:
4381 break;
4382 }
4383 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004384
4385cleanup:
4386 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004387 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004388 }
bellard81d09122004-07-14 17:21:37 +00004389}
4390
aliguori731b0362009-03-05 23:01:42 +00004391static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004392{
aliguori731b0362009-03-05 23:01:42 +00004393 Monitor *mon = opaque;
4394
Jan Kiszkac62313b2009-12-04 14:05:29 +01004395 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004396}
4397
Luiz Capitulino09069b12010-02-04 18:10:06 -02004398static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4399{
4400 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4401 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4402}
4403
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004404/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004405 * Argument validation rules:
4406 *
4407 * 1. The argument must exist in cmd_args qdict
4408 * 2. The argument type must be the expected one
4409 *
4410 * Special case: If the argument doesn't exist in cmd_args and
4411 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4412 * checking is skipped for it.
4413 */
4414static int check_client_args_type(const QDict *client_args,
4415 const QDict *cmd_args, int flags)
4416{
4417 const QDictEntry *ent;
4418
4419 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4420 QObject *obj;
4421 QString *arg_type;
4422 const QObject *client_arg = qdict_entry_value(ent);
4423 const char *client_arg_name = qdict_entry_key(ent);
4424
4425 obj = qdict_get(cmd_args, client_arg_name);
4426 if (!obj) {
4427 if (flags & QMP_ACCEPT_UNKNOWNS) {
4428 /* handler accepts unknowns */
4429 continue;
4430 }
4431 /* client arg doesn't exist */
4432 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4433 return -1;
4434 }
4435
4436 arg_type = qobject_to_qstring(obj);
4437 assert(arg_type != NULL);
4438
4439 /* check if argument's type is correct */
4440 switch (qstring_get_str(arg_type)[0]) {
4441 case 'F':
4442 case 'B':
4443 case 's':
4444 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4445 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4446 "string");
4447 return -1;
4448 }
4449 break;
4450 case 'i':
4451 case 'l':
4452 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004453 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004454 if (qobject_type(client_arg) != QTYPE_QINT) {
4455 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4456 "int");
4457 return -1;
4458 }
4459 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004460 case 'T':
4461 if (qobject_type(client_arg) != QTYPE_QINT &&
4462 qobject_type(client_arg) != QTYPE_QFLOAT) {
4463 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4464 "number");
4465 return -1;
4466 }
4467 break;
4468 case 'b':
4469 case '-':
4470 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4471 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4472 "bool");
4473 return -1;
4474 }
4475 break;
4476 case 'O':
4477 assert(flags & QMP_ACCEPT_UNKNOWNS);
4478 break;
4479 case '/':
4480 case '.':
4481 /*
4482 * These types are not supported by QMP and thus are not
4483 * handled here. Fall through.
4484 */
4485 default:
4486 abort();
4487 }
4488 }
4489
4490 return 0;
4491}
4492
4493/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004494 * - Check if the client has passed all mandatory args
4495 * - Set special flags for argument validation
4496 */
4497static int check_mandatory_args(const QDict *cmd_args,
4498 const QDict *client_args, int *flags)
4499{
4500 const QDictEntry *ent;
4501
4502 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4503 const char *cmd_arg_name = qdict_entry_key(ent);
4504 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4505 assert(type != NULL);
4506
4507 if (qstring_get_str(type)[0] == 'O') {
4508 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4509 *flags |= QMP_ACCEPT_UNKNOWNS;
4510 } else if (qstring_get_str(type)[0] != '-' &&
4511 qstring_get_str(type)[1] != '?' &&
4512 !qdict_haskey(client_args, cmd_arg_name)) {
4513 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4514 return -1;
4515 }
4516 }
4517
4518 return 0;
4519}
4520
4521static QDict *qdict_from_args_type(const char *args_type)
4522{
4523 int i;
4524 QDict *qdict;
4525 QString *key, *type, *cur_qs;
4526
4527 assert(args_type != NULL);
4528
4529 qdict = qdict_new();
4530
4531 if (args_type == NULL || args_type[0] == '\0') {
4532 /* no args, empty qdict */
4533 goto out;
4534 }
4535
4536 key = qstring_new();
4537 type = qstring_new();
4538
4539 cur_qs = key;
4540
4541 for (i = 0;; i++) {
4542 switch (args_type[i]) {
4543 case ',':
4544 case '\0':
4545 qdict_put(qdict, qstring_get_str(key), type);
4546 QDECREF(key);
4547 if (args_type[i] == '\0') {
4548 goto out;
4549 }
4550 type = qstring_new(); /* qdict has ref */
4551 cur_qs = key = qstring_new();
4552 break;
4553 case ':':
4554 cur_qs = type;
4555 break;
4556 default:
4557 qstring_append_chr(cur_qs, args_type[i]);
4558 break;
4559 }
4560 }
4561
4562out:
4563 return qdict;
4564}
4565
4566/*
4567 * Client argument checking rules:
4568 *
4569 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004570 * 2. Each argument provided by the client must be expected
4571 * 3. Each argument provided by the client must have the type expected
4572 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004573 */
4574static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4575{
4576 int flags, err;
4577 QDict *cmd_args;
4578
4579 cmd_args = qdict_from_args_type(cmd->args_type);
4580
4581 flags = 0;
4582 err = check_mandatory_args(cmd_args, client_args, &flags);
4583 if (err) {
4584 goto out;
4585 }
4586
Luiz Capitulino4af91932010-06-22 11:44:05 -03004587 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004588
4589out:
4590 QDECREF(cmd_args);
4591 return err;
4592}
4593
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004594/*
4595 * Input object checking rules
4596 *
4597 * 1. Input object must be a dict
4598 * 2. The "execute" key must exist
4599 * 3. The "execute" key must be a string
4600 * 4. If the "arguments" key exists, it must be a dict
4601 * 5. If the "id" key exists, it can be anything (ie. json-value)
4602 * 6. Any argument not listed above is considered invalid
4603 */
4604static QDict *qmp_check_input_obj(QObject *input_obj)
4605{
4606 const QDictEntry *ent;
4607 int has_exec_key = 0;
4608 QDict *input_dict;
4609
4610 if (qobject_type(input_obj) != QTYPE_QDICT) {
4611 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4612 return NULL;
4613 }
4614
4615 input_dict = qobject_to_qdict(input_obj);
4616
4617 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4618 const char *arg_name = qdict_entry_key(ent);
4619 const QObject *arg_obj = qdict_entry_value(ent);
4620
4621 if (!strcmp(arg_name, "execute")) {
4622 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4623 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4624 "string");
4625 return NULL;
4626 }
4627 has_exec_key = 1;
4628 } else if (!strcmp(arg_name, "arguments")) {
4629 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4630 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4631 "object");
4632 return NULL;
4633 }
4634 } else if (!strcmp(arg_name, "id")) {
4635 /* FIXME: check duplicated IDs for async commands */
4636 } else {
4637 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4638 return NULL;
4639 }
4640 }
4641
4642 if (!has_exec_key) {
4643 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4644 return NULL;
4645 }
4646
4647 return input_dict;
4648}
4649
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004650static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4651 const QDict *params)
4652{
4653 int ret;
4654 QObject *data = NULL;
4655
4656 mon_print_count_init(mon);
4657
4658 ret = cmd->mhandler.cmd_new(mon, params, &data);
4659 handler_audit(mon, cmd, ret);
4660 monitor_protocol_emitter(mon, data);
4661 qobject_decref(data);
4662}
4663
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004664static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4665{
4666 int err;
4667 QObject *obj;
4668 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004669 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004670 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004671 Monitor *mon = cur_mon;
4672
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004673 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004674
4675 obj = json_parser_parse(tokens, NULL);
4676 if (!obj) {
4677 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004678 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004679 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004680 }
4681
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004682 input = qmp_check_input_obj(obj);
4683 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004684 qobject_decref(obj);
4685 goto err_out;
4686 }
4687
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004688 mon->mc->id = qdict_get(input, "id");
4689 qobject_incref(mon->mc->id);
4690
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004691 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004692 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004693 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004694 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004695 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004696 }
4697
Anthony Liguorie3193602011-09-02 12:34:47 -05004698 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004699 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004700 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004701 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004702 }
4703
4704 obj = qdict_get(input, "arguments");
4705 if (!obj) {
4706 args = qdict_new();
4707 } else {
4708 args = qobject_to_qdict(obj);
4709 QINCREF(args);
4710 }
4711
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004712 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004713 if (err < 0) {
4714 goto err_out;
4715 }
4716
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004717 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004718 err = qmp_async_cmd_handler(mon, cmd, args);
4719 if (err) {
4720 /* emit the error response */
4721 goto err_out;
4722 }
Adam Litke940cc302010-01-25 12:18:44 -06004723 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004724 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004725 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004726
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004727 goto out;
4728
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004729err_out:
4730 monitor_protocol_emitter(mon, NULL);
4731out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004732 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004733 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004734}
4735
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004736/**
4737 * monitor_control_read(): Read and handle QMP input
4738 */
4739static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4740{
4741 Monitor *old_mon = cur_mon;
4742
4743 cur_mon = opaque;
4744
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004745 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004746
4747 cur_mon = old_mon;
4748}
4749
aliguori731b0362009-03-05 23:01:42 +00004750static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004751{
aliguori731b0362009-03-05 23:01:42 +00004752 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004753 int i;
aliguori376253e2009-03-05 23:01:23 +00004754
aliguori731b0362009-03-05 23:01:42 +00004755 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004756
aliguoricde76ee2009-03-05 23:01:51 +00004757 if (cur_mon->rs) {
4758 for (i = 0; i < size; i++)
4759 readline_handle_byte(cur_mon->rs, buf[i]);
4760 } else {
4761 if (size == 0 || buf[size - 1] != 0)
4762 monitor_printf(cur_mon, "corrupted command\n");
4763 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004764 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004765 }
aliguori731b0362009-03-05 23:01:42 +00004766
4767 cur_mon = old_mon;
4768}
aliguorid8f44602008-10-06 13:52:44 +00004769
aliguori376253e2009-03-05 23:01:23 +00004770static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004771{
aliguori731b0362009-03-05 23:01:42 +00004772 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004773 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004774 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004775}
4776
aliguoricde76ee2009-03-05 23:01:51 +00004777int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004778{
aliguoricde76ee2009-03-05 23:01:51 +00004779 if (!mon->rs)
4780 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004781 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004782 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004783}
4784
aliguori376253e2009-03-05 23:01:23 +00004785void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004786{
aliguoricde76ee2009-03-05 23:01:51 +00004787 if (!mon->rs)
4788 return;
aliguori731b0362009-03-05 23:01:42 +00004789 if (--mon->suspend_cnt == 0)
4790 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004791}
4792
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004793static QObject *get_qmp_greeting(void)
4794{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004795 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004796
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004797 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004798 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4799}
4800
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004801/**
4802 * monitor_control_event(): Print QMP gretting
4803 */
4804static void monitor_control_event(void *opaque, int event)
4805{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004806 QObject *data;
4807 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004808
Luiz Capitulino47116d12010-02-08 17:01:30 -02004809 switch (event) {
4810 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004811 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004812 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004813 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004814 monitor_json_emitter(mon, data);
4815 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004816 break;
4817 case CHR_EVENT_CLOSED:
4818 json_message_parser_destroy(&mon->mc->parser);
4819 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004820 }
4821}
4822
aliguori731b0362009-03-05 23:01:42 +00004823static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004824{
aliguori376253e2009-03-05 23:01:23 +00004825 Monitor *mon = opaque;
4826
aliguori2724b182009-03-05 23:01:47 +00004827 switch (event) {
4828 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004829 mon->mux_out = 0;
4830 if (mon->reset_seen) {
4831 readline_restart(mon->rs);
4832 monitor_resume(mon);
4833 monitor_flush(mon);
4834 } else {
4835 mon->suspend_cnt = 0;
4836 }
aliguori2724b182009-03-05 23:01:47 +00004837 break;
ths86e94de2007-01-05 22:01:59 +00004838
aliguori2724b182009-03-05 23:01:47 +00004839 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004840 if (mon->reset_seen) {
4841 if (mon->suspend_cnt == 0) {
4842 monitor_printf(mon, "\n");
4843 }
4844 monitor_flush(mon);
4845 monitor_suspend(mon);
4846 } else {
4847 mon->suspend_cnt++;
4848 }
4849 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004850 break;
4851
Amit Shahb6b8df52009-10-07 18:31:16 +05304852 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004853 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4854 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004855 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004856 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004857 }
4858 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004859 break;
4860 }
ths86e94de2007-01-05 22:01:59 +00004861}
4862
aliguori76655d62009-03-06 20:27:37 +00004863
4864/*
4865 * Local variables:
4866 * c-indent-level: 4
4867 * c-basic-offset: 4
4868 * tab-width: 8
4869 * End:
4870 */
4871
aliguori731b0362009-03-05 23:01:42 +00004872void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004873{
aliguori731b0362009-03-05 23:01:42 +00004874 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004875 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004876
4877 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004878 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
ths20d8a3e2007-02-18 17:04:49 +00004879 is_first_init = 0;
4880 }
aliguori87127162009-03-05 23:01:29 +00004881
Anthony Liguori7267c092011-08-20 22:09:37 -05004882 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004883
aliguori87127162009-03-05 23:01:29 +00004884 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004885 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004886 if (flags & MONITOR_USE_READLINE) {
4887 mon->rs = readline_init(mon, monitor_find_completion);
4888 monitor_read_command(mon, 0);
4889 }
aliguori87127162009-03-05 23:01:29 +00004890
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004891 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004892 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004893 /* Control mode requires special handlers */
4894 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4895 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004896 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004897 } else {
4898 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4899 monitor_event, mon);
4900 }
aliguori87127162009-03-05 23:01:29 +00004901
Blue Swirl72cf2d42009-09-12 07:36:22 +00004902 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004903 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4904 default_mon = mon;
bellard7e2515e2004-08-01 21:52:19 +00004905}
4906
aliguori376253e2009-03-05 23:01:23 +00004907static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004908{
aliguoribb5fc202009-03-05 23:01:15 +00004909 BlockDriverState *bs = opaque;
4910 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004911
aliguoribb5fc202009-03-05 23:01:15 +00004912 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004913 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004914 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004915 }
aliguori731b0362009-03-05 23:01:42 +00004916 if (mon->password_completion_cb)
4917 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004918
aliguori731b0362009-03-05 23:01:42 +00004919 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004920}
aliguoric0f4ce72009-03-05 23:01:01 +00004921
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004922int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4923 BlockDriverCompletionFunc *completion_cb,
4924 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004925{
aliguoricde76ee2009-03-05 23:01:51 +00004926 int err;
4927
aliguoribb5fc202009-03-05 23:01:15 +00004928 if (!bdrv_key_required(bs)) {
4929 if (completion_cb)
4930 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004931 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004932 }
aliguoric0f4ce72009-03-05 23:01:01 +00004933
Luiz Capitulino94171e12009-12-07 21:37:00 +01004934 if (monitor_ctrl_mode(mon)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004935 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004936 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004937 }
4938
aliguori376253e2009-03-05 23:01:23 +00004939 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4940 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004941
aliguori731b0362009-03-05 23:01:42 +00004942 mon->password_completion_cb = completion_cb;
4943 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004944
aliguoricde76ee2009-03-05 23:01:51 +00004945 err = monitor_read_password(mon, bdrv_password_cb, bs);
4946
4947 if (err && completion_cb)
4948 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004949
4950 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004951}