blob: a4a29b9cd7ff7552e30c3142f972f234df4a0670 [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"
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +010069#include "qemu-thread.h"
ths6a5bd302007-12-03 17:05:38 +000070
Jan Kiszka661f1922011-10-16 11:53:13 +020071/* for pic/irq_info */
72#if defined(TARGET_SPARC)
73#include "hw/sun4m.h"
74#endif
75#include "hw/lm32_pic.h"
76
bellard9dc39cb2004-03-14 21:38:27 +000077//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000078//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000079
bellard9307c4c2004-04-04 12:57:25 +000080/*
81 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000082 *
bellard9307c4c2004-04-04 12:57:25 +000083 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000084 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000085 * 's' string (accept optional quote)
Markus Armbruster361127d2010-02-10 20:24:35 +010086 * 'O' option string of the form NAME=VALUE,...
87 * parsed according to QemuOptsList given by its name
88 * Example: 'device:O' uses qemu_device_opts.
89 * Restriction: only lists with empty desc are supported
90 * TODO lift the restriction
bellard92a31b12005-02-10 22:00:52 +000091 * 'i' 32 bit integer
92 * 'l' target long (32 or 64 bit)
Luiz Capitulino91162842012-04-26 17:34:30 -030093 * 'M' Non-negative target long (32 or 64 bit), in user mode the
94 * value is multiplied by 2^20 (think Mebibyte)
Jes Sorensendbc0c672010-10-21 17:15:47 +020095 * 'o' octets (aka bytes)
96 * user mode accepts an optional T, t, G, g, M, m, K, k
97 * suffix, which multiplies the value by 2^40 for
98 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
99 * M and m, 2^10 for K and k
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +0100100 * 'T' double
101 * user mode accepts an optional ms, us, ns suffix,
102 * which divides the value by 1e3, 1e6, 1e9, respectively
bellard9307c4c2004-04-04 12:57:25 +0000103 * '/' optional gdb-like print format (like "/10x")
104 *
Luiz Capitulinofb466602009-08-28 15:27:27 -0300105 * '?' optional type (for all types, except '/')
106 * '.' other form of optional type (for 'i' and 'l')
Markus Armbruster942cd1f2010-03-26 09:07:09 +0100107 * 'b' boolean
108 * user mode accepts "on" or "off"
Luiz Capitulinofb466602009-08-28 15:27:27 -0300109 * '-' optional parameter (eg. '-f')
bellard9307c4c2004-04-04 12:57:25 +0000110 *
111 */
112
Adam Litke940cc302010-01-25 12:18:44 -0600113typedef struct MonitorCompletionData MonitorCompletionData;
114struct MonitorCompletionData {
115 Monitor *mon;
116 void (*user_print)(Monitor *mon, const QObject *data);
117};
118
Anthony Liguoric227f092009-10-01 16:12:16 -0500119typedef struct mon_cmd_t {
bellard9dc39cb2004-03-14 21:38:27 +0000120 const char *name;
bellard9307c4c2004-04-04 12:57:25 +0000121 const char *args_type;
bellard9dc39cb2004-03-14 21:38:27 +0000122 const char *params;
123 const char *help;
Luiz Capitulinoa2876f52009-10-07 13:41:53 -0300124 void (*user_print)(Monitor *mon, const QObject *data);
Luiz Capitulino910df892009-10-07 13:41:51 -0300125 union {
126 void (*info)(Monitor *mon);
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -0300127 void (*cmd)(Monitor *mon, const QDict *qdict);
Luiz Capitulino261394d2010-02-10 23:50:02 -0200128 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
Adam Litke940cc302010-01-25 12:18:44 -0600129 int (*cmd_async)(Monitor *mon, const QDict *params,
130 MonitorCompletion *cb, void *opaque);
Luiz Capitulino910df892009-10-07 13:41:51 -0300131 } mhandler;
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200132 int flags;
Anthony Liguoric227f092009-10-01 16:12:16 -0500133} mon_cmd_t;
bellard9dc39cb2004-03-14 21:38:27 +0000134
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100135/* file descriptors passed via SCM_RIGHTS */
Anthony Liguoric227f092009-10-01 16:12:16 -0500136typedef struct mon_fd_t mon_fd_t;
137struct mon_fd_t {
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100138 char *name;
139 int fd;
Anthony Liguoric227f092009-10-01 16:12:16 -0500140 QLIST_ENTRY(mon_fd_t) next;
Mark McLoughlinf07918f2009-07-22 09:11:40 +0100141};
142
Corey Bryantba1c0482012-08-14 16:43:43 -0400143/* file descriptor associated with a file descriptor set */
144typedef struct MonFdsetFd MonFdsetFd;
145struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
150};
151
152/* file descriptor set containing fds passed via SCM_RIGHTS */
153typedef struct MonFdset MonFdset;
154struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
Corey Bryantadb696f2012-08-14 16:43:47 -0400157 QLIST_HEAD(, MonFdsetFd) dup_fds;
Corey Bryantba1c0482012-08-14 16:43:43 -0400158 QLIST_ENTRY(MonFdset) next;
159};
160
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200161typedef struct MonitorControl {
162 QObject *id;
163 JSONMessageParser parser;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200164 int command_mode;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200165} MonitorControl;
166
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100167/*
168 * To prevent flooding clients, events can be throttled. The
169 * throttling is calculated globally, rather than per-Monitor
170 * instance.
171 */
172typedef struct MonitorEventState {
173 MonitorEvent event; /* Event being tracked */
174 int64_t rate; /* Period over which to throttle. 0 to disable */
175 int64_t last; /* Time at which event was last emitted */
176 QEMUTimer *timer; /* Timer for handling delayed events */
177 QObject *data; /* Event pending delayed dispatch */
178} MonitorEventState;
179
aliguori87127162009-03-05 23:01:29 +0000180struct Monitor {
181 CharDriverState *chr;
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +0200182 int mux_out;
183 int reset_seen;
aliguori731b0362009-03-05 23:01:42 +0000184 int flags;
185 int suspend_cnt;
186 uint8_t outbuf[1024];
187 int outbuf_index;
188 ReadLineState *rs;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200189 MonitorControl *mc;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100190 CPUArchState *mon_cpu;
aliguori731b0362009-03-05 23:01:42 +0000191 BlockDriverCompletionFunc *password_completion_cb;
192 void *password_opaque;
Luiz Capitulino8204a912009-11-18 23:05:31 -0200193 QError *error;
Anthony Liguoric227f092009-10-01 16:12:16 -0500194 QLIST_HEAD(,mon_fd_t) fds;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000195 QLIST_ENTRY(Monitor) entry;
aliguori87127162009-03-05 23:01:29 +0000196};
197
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -0300198/* QMP checker flags */
199#define QMP_ACCEPT_UNKNOWNS 1
200
Blue Swirl72cf2d42009-09-12 07:36:22 +0000201static QLIST_HEAD(mon_list, Monitor) mon_list;
Corey Bryantba1c0482012-08-14 16:43:43 -0400202static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
bellard7e2515e2004-08-01 21:52:19 +0000203
Wayne Xia816f8922011-10-12 11:32:41 +0800204static mon_cmd_t mon_cmds[];
205static 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
Anthony Liguori7060b472011-09-02 12:34:50 -0500232void monitor_read_command(Monitor *mon, int show_prompt)
aliguori731b0362009-03-05 23:01:42 +0000233{
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
Anthony Liguori7060b472011-09-02 12:34:50 -0500242int 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 Capitulinob8b08262010-02-10 23:50:04 -0200291 if (monitor_ctrl_mode(mon)) {
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200292 return;
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200293 }
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200294
295 vsnprintf(buf, sizeof(buf), fmt, ap);
296 monitor_puts(mon, buf);
bellard7e2515e2004-08-01 21:52:19 +0000297}
298
aliguori376253e2009-03-05 23:01:23 +0000299void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard7e2515e2004-08-01 21:52:19 +0000300{
301 va_list ap;
302 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000303 monitor_vprintf(mon, fmt, ap);
bellard7e2515e2004-08-01 21:52:19 +0000304 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000305}
306
aliguori376253e2009-03-05 23:01:23 +0000307void monitor_print_filename(Monitor *mon, const char *filename)
thsfef30742006-12-22 14:11:32 +0000308{
309 int i;
310
311 for (i = 0; filename[i]; i++) {
aliguori28a76be2009-03-06 20:27:40 +0000312 switch (filename[i]) {
313 case ' ':
314 case '"':
315 case '\\':
316 monitor_printf(mon, "\\%c", filename[i]);
317 break;
318 case '\t':
319 monitor_printf(mon, "\\t");
320 break;
321 case '\r':
322 monitor_printf(mon, "\\r");
323 break;
324 case '\n':
325 monitor_printf(mon, "\\n");
326 break;
327 default:
328 monitor_printf(mon, "%c", filename[i]);
329 break;
330 }
thsfef30742006-12-22 14:11:32 +0000331 }
332}
333
Stefan Weil8b7968f2010-09-23 21:28:05 +0200334static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
335 const char *fmt, ...)
bellard7fe48482004-10-09 18:08:01 +0000336{
337 va_list ap;
338 va_start(ap, fmt);
aliguori376253e2009-03-05 23:01:23 +0000339 monitor_vprintf((Monitor *)stream, fmt, ap);
bellard7fe48482004-10-09 18:08:01 +0000340 va_end(ap);
341 return 0;
342}
343
Luiz Capitulino13c74252009-10-07 13:41:55 -0300344static void monitor_user_noop(Monitor *mon, const QObject *data) { }
345
Luiz Capitulino9e807212010-09-16 10:58:59 -0300346static inline int handler_is_qobject(const mon_cmd_t *cmd)
Luiz Capitulino13917be2009-10-07 13:41:54 -0300347{
348 return cmd->user_print != NULL;
349}
350
Luiz Capitulino4903de02010-09-16 11:01:32 -0300351static inline bool handler_is_async(const mon_cmd_t *cmd)
Adam Litke940cc302010-01-25 12:18:44 -0600352{
Jan Kiszka8ac470c2010-06-16 00:38:39 +0200353 return cmd->flags & MONITOR_CMD_ASYNC;
Adam Litke940cc302010-01-25 12:18:44 -0600354}
355
Luiz Capitulino8204a912009-11-18 23:05:31 -0200356static inline int monitor_has_error(const Monitor *mon)
357{
358 return mon->error != NULL;
359}
360
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200361static void monitor_json_emitter(Monitor *mon, const QObject *data)
362{
363 QString *json;
364
Luiz Capitulino83a27d42010-11-22 17:10:37 -0200365 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
366 qobject_to_json(data);
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200367 assert(json != NULL);
368
Luiz Capitulinob8b08262010-02-10 23:50:04 -0200369 qstring_append_chr(json, '\n');
370 monitor_puts(mon, qstring_get_str(json));
Luiz Capitulino4a29a852009-11-26 22:59:05 -0200371
Luiz Capitulino9b57c022009-11-26 22:58:58 -0200372 QDECREF(json);
373}
374
Luiz Capitulinode253f12012-07-27 16:18:16 -0300375static QDict *build_qmp_error_dict(const QError *err)
376{
377 QObject *obj;
378
379 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
380 ErrorClass_lookup[err->err_class],
381 qerror_human(err));
382
383 return qobject_to_qdict(obj);
384}
385
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200386static void monitor_protocol_emitter(Monitor *mon, QObject *data)
387{
388 QDict *qmp;
389
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +0100390 trace_monitor_protocol_emitter(mon);
391
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200392 if (!monitor_has_error(mon)) {
393 /* success response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300394 qmp = qdict_new();
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200395 if (data) {
396 qobject_incref(data);
397 qdict_put_obj(qmp, "return", data);
398 } else {
Luiz Capitulino0abc6572009-12-18 13:25:00 -0200399 /* return an empty QDict by default */
400 qdict_put(qmp, "return", qdict_new());
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200401 }
402 } else {
403 /* error response */
Luiz Capitulinode253f12012-07-27 16:18:16 -0300404 qmp = build_qmp_error_dict(mon->error);
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200405 QDECREF(mon->error);
406 mon->error = NULL;
407 }
408
Luiz Capitulino5fa737a2009-11-26 22:59:01 -0200409 if (mon->mc->id) {
410 qdict_put_obj(qmp, "id", mon->mc->id);
411 mon->mc->id = NULL;
412 }
413
Luiz Capitulino25b422e2009-11-26 22:58:59 -0200414 monitor_json_emitter(mon, QOBJECT(qmp));
415 QDECREF(qmp);
416}
417
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200418static void timestamp_put(QDict *qdict)
419{
420 int err;
421 QObject *obj;
Blue Swirld08d6f02009-12-04 18:06:39 +0000422 qemu_timeval tv;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200423
Blue Swirld08d6f02009-12-04 18:06:39 +0000424 err = qemu_gettimeofday(&tv);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200425 if (err < 0)
426 return;
427
428 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
429 "'microseconds': %" PRId64 " }",
430 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200431 qdict_put_obj(qdict, "timestamp", obj);
432}
433
Daniel P. Berrange48608532012-05-21 17:59:51 +0100434
435static const char *monitor_event_names[] = {
436 [QEVENT_SHUTDOWN] = "SHUTDOWN",
437 [QEVENT_RESET] = "RESET",
438 [QEVENT_POWERDOWN] = "POWERDOWN",
439 [QEVENT_STOP] = "STOP",
440 [QEVENT_RESUME] = "RESUME",
441 [QEVENT_VNC_CONNECTED] = "VNC_CONNECTED",
442 [QEVENT_VNC_INITIALIZED] = "VNC_INITIALIZED",
443 [QEVENT_VNC_DISCONNECTED] = "VNC_DISCONNECTED",
444 [QEVENT_BLOCK_IO_ERROR] = "BLOCK_IO_ERROR",
445 [QEVENT_RTC_CHANGE] = "RTC_CHANGE",
446 [QEVENT_WATCHDOG] = "WATCHDOG",
447 [QEVENT_SPICE_CONNECTED] = "SPICE_CONNECTED",
448 [QEVENT_SPICE_INITIALIZED] = "SPICE_INITIALIZED",
449 [QEVENT_SPICE_DISCONNECTED] = "SPICE_DISCONNECTED",
450 [QEVENT_BLOCK_JOB_COMPLETED] = "BLOCK_JOB_COMPLETED",
451 [QEVENT_BLOCK_JOB_CANCELLED] = "BLOCK_JOB_CANCELLED",
452 [QEVENT_DEVICE_TRAY_MOVED] = "DEVICE_TRAY_MOVED",
453 [QEVENT_SUSPEND] = "SUSPEND",
Luiz Capitulino25df49f2012-08-08 17:03:01 -0300454 [QEVENT_SUSPEND_DISK] = "SUSPEND_DISK",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100455 [QEVENT_WAKEUP] = "WAKEUP",
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100456 [QEVENT_BALLOON_CHANGE] = "BALLOON_CHANGE",
Daniel P. Berrange48608532012-05-21 17:59:51 +0100457};
458QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names) != QEVENT_MAX)
459
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100460MonitorEventState monitor_event_state[QEVENT_MAX];
461QemuMutex monitor_event_state_lock;
462
463/*
464 * Emits the event to every monitor instance
465 */
466static void
467monitor_protocol_event_emit(MonitorEvent event,
468 QObject *data)
469{
470 Monitor *mon;
471
472 trace_monitor_protocol_event_emit(event, data);
473 QLIST_FOREACH(mon, &mon_list, entry) {
474 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
475 monitor_json_emitter(mon, data);
476 }
477 }
478}
479
480
481/*
482 * Queue a new event for emission to Monitor instances,
483 * applying any rate limiting if required.
484 */
485static void
486monitor_protocol_event_queue(MonitorEvent event,
487 QObject *data)
488{
489 MonitorEventState *evstate;
490 int64_t now = qemu_get_clock_ns(rt_clock);
491 assert(event < QEVENT_MAX);
492
493 qemu_mutex_lock(&monitor_event_state_lock);
494 evstate = &(monitor_event_state[event]);
495 trace_monitor_protocol_event_queue(event,
496 data,
497 evstate->rate,
498 evstate->last,
499 now);
500
501 /* Rate limit of 0 indicates no throttling */
502 if (!evstate->rate) {
503 monitor_protocol_event_emit(event, data);
504 evstate->last = now;
505 } else {
506 int64_t delta = now - evstate->last;
507 if (evstate->data ||
508 delta < evstate->rate) {
509 /* If there's an existing event pending, replace
510 * it with the new event, otherwise schedule a
511 * timer for delayed emission
512 */
513 if (evstate->data) {
514 qobject_decref(evstate->data);
515 } else {
516 int64_t then = evstate->last + evstate->rate;
517 qemu_mod_timer_ns(evstate->timer, then);
518 }
519 evstate->data = data;
520 qobject_incref(evstate->data);
521 } else {
522 monitor_protocol_event_emit(event, data);
523 evstate->last = now;
524 }
525 }
526 qemu_mutex_unlock(&monitor_event_state_lock);
527}
528
529
530/*
531 * The callback invoked by QemuTimer when a delayed
532 * event is ready to be emitted
533 */
534static void monitor_protocol_event_handler(void *opaque)
535{
536 MonitorEventState *evstate = opaque;
537 int64_t now = qemu_get_clock_ns(rt_clock);
538
539 qemu_mutex_lock(&monitor_event_state_lock);
540
541 trace_monitor_protocol_event_handler(evstate->event,
542 evstate->data,
543 evstate->last,
544 now);
545 if (evstate->data) {
546 monitor_protocol_event_emit(evstate->event, evstate->data);
547 qobject_decref(evstate->data);
548 evstate->data = NULL;
549 }
550 evstate->last = now;
551 qemu_mutex_unlock(&monitor_event_state_lock);
552}
553
554
555/*
556 * @event: the event ID to be limited
557 * @rate: the rate limit in milliseconds
558 *
559 * Sets a rate limit on a particular event, so no
560 * more than 1 event will be emitted within @rate
561 * milliseconds
562 */
563static void
564monitor_protocol_event_throttle(MonitorEvent event,
565 int64_t rate)
566{
567 MonitorEventState *evstate;
568 assert(event < QEVENT_MAX);
569
570 evstate = &(monitor_event_state[event]);
571
572 trace_monitor_protocol_event_throttle(event, rate);
573 evstate->event = event;
574 evstate->rate = rate * SCALE_MS;
575 evstate->timer = qemu_new_timer(rt_clock,
576 SCALE_MS,
577 monitor_protocol_event_handler,
578 evstate);
579 evstate->last = 0;
580 evstate->data = NULL;
581}
582
583
584/* Global, one-time initializer to configure the rate limiting
585 * and initialize state */
586static void monitor_protocol_event_init(void)
587{
588 qemu_mutex_init(&monitor_event_state_lock);
589 /* Limit RTC & BALLOON events to 1 per second */
590 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE, 1000);
591 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE, 1000);
592 monitor_protocol_event_throttle(QEVENT_WATCHDOG, 1000);
593}
594
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200595/**
596 * monitor_protocol_event(): Generate a Monitor event
597 *
598 * Event-specific data can be emitted through the (optional) 'data' parameter.
599 */
600void monitor_protocol_event(MonitorEvent event, QObject *data)
601{
602 QDict *qmp;
603 const char *event_name;
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200604
Blue Swirl242cd002009-12-04 18:05:45 +0000605 assert(event < QEVENT_MAX);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200606
Daniel P. Berrange48608532012-05-21 17:59:51 +0100607 event_name = monitor_event_names[event];
608 assert(event_name != NULL);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200609
610 qmp = qdict_new();
611 timestamp_put(qmp);
612 qdict_put(qmp, "event", qstring_from_str(event_name));
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200613 if (data) {
614 qobject_incref(data);
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200615 qdict_put_obj(qmp, "data", data);
Luiz Capitulino3d72f9a2010-01-08 16:45:53 -0200616 }
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200617
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +0100618 trace_monitor_protocol_event(event, event_name, qmp);
619 monitor_protocol_event_queue(event, QOBJECT(qmp));
Luiz Capitulino0d1ea872009-11-26 22:59:03 -0200620 QDECREF(qmp);
621}
622
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200623static int do_qmp_capabilities(Monitor *mon, const QDict *params,
624 QObject **ret_data)
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200625{
626 /* Will setup QMP capabilities in the future */
627 if (monitor_ctrl_mode(mon)) {
628 mon->mc->command_mode = 1;
629 }
Luiz Capitulinoef4b7ee2010-02-10 23:49:48 -0200630
631 return 0;
Luiz Capitulino4a7e1192010-02-04 18:10:05 -0200632}
633
Luiz Capitulino0268d972010-10-22 10:08:02 -0200634static void handle_user_command(Monitor *mon, const char *cmdline);
635
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200636char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
637 int64_t cpu_index, Error **errp)
Luiz Capitulino0268d972010-10-22 10:08:02 -0200638{
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200639 char *output = NULL;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200640 Monitor *old_mon, hmp;
641 CharDriverState mchar;
642
643 memset(&hmp, 0, sizeof(hmp));
644 qemu_chr_init_mem(&mchar);
645 hmp.chr = &mchar;
646
647 old_mon = cur_mon;
648 cur_mon = &hmp;
649
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200650 if (has_cpu_index) {
651 int ret = monitor_set_cpu(cpu_index);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200652 if (ret < 0) {
653 cur_mon = old_mon;
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200654 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
655 "a CPU number");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200656 goto out;
657 }
658 }
659
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200660 handle_user_command(&hmp, command_line);
Luiz Capitulino0268d972010-10-22 10:08:02 -0200661 cur_mon = old_mon;
662
663 if (qemu_chr_mem_osize(hmp.chr) > 0) {
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200664 QString *str = qemu_chr_mem_to_qs(hmp.chr);
665 output = g_strdup(qstring_get_str(str));
666 QDECREF(str);
667 } else {
668 output = g_strdup("");
Luiz Capitulino0268d972010-10-22 10:08:02 -0200669 }
670
671out:
672 qemu_chr_close_mem(hmp.chr);
Luiz Capitulinod51a67b2011-11-25 17:52:45 -0200673 return output;
Luiz Capitulino0268d972010-10-22 10:08:02 -0200674}
675
bellard9dc39cb2004-03-14 21:38:27 +0000676static int compare_cmd(const char *name, const char *list)
677{
678 const char *p, *pstart;
679 int len;
680 len = strlen(name);
681 p = list;
682 for(;;) {
683 pstart = p;
684 p = strchr(p, '|');
685 if (!p)
686 p = pstart + strlen(pstart);
687 if ((p - pstart) == len && !memcmp(pstart, name, len))
688 return 1;
689 if (*p == '\0')
690 break;
691 p++;
692 }
693 return 0;
694}
695
Anthony Liguoric227f092009-10-01 16:12:16 -0500696static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori376253e2009-03-05 23:01:23 +0000697 const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000698{
Anthony Liguoric227f092009-10-01 16:12:16 -0500699 const mon_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000700
701 for(cmd = cmds; cmd->name != NULL; cmd++) {
702 if (!name || !strcmp(name, cmd->name))
aliguori376253e2009-03-05 23:01:23 +0000703 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
704 cmd->params, cmd->help);
bellard9dc39cb2004-03-14 21:38:27 +0000705 }
706}
707
aliguori376253e2009-03-05 23:01:23 +0000708static void help_cmd(Monitor *mon, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000709{
710 if (name && !strcmp(name, "info")) {
aliguori376253e2009-03-05 23:01:23 +0000711 help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard9dc39cb2004-03-14 21:38:27 +0000712 } else {
aliguori376253e2009-03-05 23:01:23 +0000713 help_cmd_dump(mon, mon_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000714 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000715 const CPULogItem *item;
aliguori376253e2009-03-05 23:01:23 +0000716 monitor_printf(mon, "Log items (comma separated):\n");
717 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
bellardf193c792004-03-21 17:06:25 +0000718 for(item = cpu_log_items; item->mask != 0; item++) {
aliguori376253e2009-03-05 23:01:23 +0000719 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
bellardf193c792004-03-21 17:06:25 +0000720 }
721 }
bellard9dc39cb2004-03-14 21:38:27 +0000722 }
723}
724
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300725static void do_help_cmd(Monitor *mon, const QDict *qdict)
Luiz Capitulino38183182009-08-28 15:27:08 -0300726{
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300727 help_cmd(mon, qdict_get_try_str(qdict, "name"));
Luiz Capitulino38183182009-08-28 15:27:08 -0300728}
729
Lluísfc764102011-08-31 20:31:18 +0200730static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530731{
732 const char *tp_name = qdict_get_str(qdict, "name");
733 bool new_state = qdict_get_bool(qdict, "option");
Lluísfc764102011-08-31 20:31:18 +0200734 int ret = trace_event_set_state(tp_name, new_state);
Blue Swirlf871d682010-10-13 19:14:29 +0000735
736 if (!ret) {
737 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
738 }
Prerna Saxena22890ab2010-06-24 17:04:53 +0530739}
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100740
Michael Rothc45a8162011-10-02 08:44:37 -0500741#ifdef CONFIG_TRACE_SIMPLE
Stefan Hajnoczic5ceb522010-07-13 09:26:33 +0100742static void do_trace_file(Monitor *mon, const QDict *qdict)
743{
744 const char *op = qdict_get_try_str(qdict, "op");
745 const char *arg = qdict_get_try_str(qdict, "arg");
746
747 if (!op) {
748 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
749 } else if (!strcmp(op, "on")) {
750 st_set_trace_file_enabled(true);
751 } else if (!strcmp(op, "off")) {
752 st_set_trace_file_enabled(false);
753 } else if (!strcmp(op, "flush")) {
754 st_flush_trace_buffer();
755 } else if (!strcmp(op, "set")) {
756 if (arg) {
757 st_set_trace_file(arg);
758 }
759 } else {
760 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
761 help_cmd(mon, "trace-file");
762 }
763}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530764#endif
765
Adam Litke940cc302010-01-25 12:18:44 -0600766static void user_monitor_complete(void *opaque, QObject *ret_data)
767{
768 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
769
770 if (ret_data) {
771 data->user_print(data->mon, ret_data);
772 }
773 monitor_resume(data->mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500774 g_free(data);
Adam Litke940cc302010-01-25 12:18:44 -0600775}
776
777static void qmp_monitor_complete(void *opaque, QObject *ret_data)
778{
779 monitor_protocol_emitter(opaque, ret_data);
780}
781
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300782static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
783 const QDict *params)
Adam Litke940cc302010-01-25 12:18:44 -0600784{
Luiz Capitulino5af7bba2010-06-22 19:10:46 -0300785 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
Adam Litke940cc302010-01-25 12:18:44 -0600786}
787
Adam Litke940cc302010-01-25 12:18:44 -0600788static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
789 const QDict *params)
790{
791 int ret;
792
Anthony Liguori7267c092011-08-20 22:09:37 -0500793 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
Adam Litke940cc302010-01-25 12:18:44 -0600794 cb_data->mon = mon;
795 cb_data->user_print = cmd->user_print;
796 monitor_suspend(mon);
797 ret = cmd->mhandler.cmd_async(mon, params,
798 user_monitor_complete, cb_data);
799 if (ret < 0) {
800 monitor_resume(mon);
Anthony Liguori7267c092011-08-20 22:09:37 -0500801 g_free(cb_data);
Adam Litke940cc302010-01-25 12:18:44 -0600802 }
803}
804
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300805static void do_info(Monitor *mon, const QDict *qdict)
bellard9dc39cb2004-03-14 21:38:27 +0000806{
Anthony Liguoric227f092009-10-01 16:12:16 -0500807 const mon_cmd_t *cmd;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300808 const char *item = qdict_get_try_str(qdict, "item");
bellard9dc39cb2004-03-14 21:38:27 +0000809
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200810 if (!item) {
bellard9dc39cb2004-03-14 21:38:27 +0000811 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200812 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300813
814 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000815 if (compare_cmd(item, cmd->name))
Luiz Capitulino13c74252009-10-07 13:41:55 -0300816 break;
bellard9dc39cb2004-03-14 21:38:27 +0000817 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300818
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200819 if (cmd->name == NULL) {
Luiz Capitulino13c74252009-10-07 13:41:55 -0300820 goto help;
Luiz Capitulino956f1a02009-11-26 22:59:00 -0200821 }
Luiz Capitulino13c74252009-10-07 13:41:55 -0300822
Luiz Capitulinob5c30582011-10-21 16:24:28 -0200823 cmd->mhandler.info(mon);
Luiz Capitulino1162daa2010-09-13 12:15:26 -0300824 return;
Luiz Capitulino13c74252009-10-07 13:41:55 -0300825
826help:
827 help_cmd(mon, "info");
bellard9dc39cb2004-03-14 21:38:27 +0000828}
829
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300830CommandInfoList *qmp_query_commands(Error **errp)
bellard9bc9d1c2004-10-10 15:15:51 +0000831{
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300832 CommandInfoList *info, *cmd_list = NULL;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200833 const mon_cmd_t *cmd;
834
Luiz Capitulinof36b4af2010-09-15 17:17:45 -0300835 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
Luiz Capitulino40e5a012011-10-21 16:15:31 -0200836 info = g_malloc0(sizeof(*info));
837 info->value = g_malloc0(sizeof(*info->value));
838 info->value->name = g_strdup(cmd->name);
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200839
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300840 info->next = cmd_list;
841 cmd_list = info;
Luiz Capitulinoe3bba9d2009-11-26 22:58:56 -0200842 }
843
Luiz Capitulinoaa9b79b2011-09-21 14:31:51 -0300844 return cmd_list;
thsa36e69d2007-12-02 05:18:19 +0000845}
846
Daniel P. Berrange48608532012-05-21 17:59:51 +0100847EventInfoList *qmp_query_events(Error **errp)
848{
849 EventInfoList *info, *ev_list = NULL;
850 MonitorEvent e;
851
852 for (e = 0 ; e < QEVENT_MAX ; e++) {
853 const char *event_name = monitor_event_names[e];
854 assert(event_name != NULL);
855 info = g_malloc0(sizeof(*info));
856 info->value = g_malloc0(sizeof(*info->value));
857 info->value->name = g_strdup(event_name);
858
859 info->next = ev_list;
860 ev_list = info;
861 }
862
863 return ev_list;
864}
865
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300866/* set the current CPU defined by the user */
867int monitor_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000868{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100869 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000870
871 for(env = first_cpu; env != NULL; env = env->next_cpu) {
872 if (env->cpu_index == cpu_index) {
aliguori731b0362009-03-05 23:01:42 +0000873 cur_mon->mon_cpu = env;
bellard6a00d602005-11-21 23:25:50 +0000874 return 0;
875 }
876 }
877 return -1;
878}
879
Andreas Färber9349b4f2012-03-14 01:38:32 +0100880static CPUArchState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000881{
aliguori731b0362009-03-05 23:01:42 +0000882 if (!cur_mon->mon_cpu) {
Luiz Capitulinob025c8b2011-10-06 14:02:57 -0300883 monitor_set_cpu(0);
bellard6a00d602005-11-21 23:25:50 +0000884 }
Avi Kivity4c0960c2009-08-17 23:19:53 +0300885 cpu_synchronize_state(cur_mon->mon_cpu);
aliguori731b0362009-03-05 23:01:42 +0000886 return cur_mon->mon_cpu;
bellard6a00d602005-11-21 23:25:50 +0000887}
888
Luiz Capitulino99b77962011-10-24 10:53:44 -0200889int monitor_get_cpu_index(void)
890{
891 return mon_get_cpu()->cpu_index;
892}
893
aliguori376253e2009-03-05 23:01:23 +0000894static void do_info_registers(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +0000895{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100896 CPUArchState *env;
bellard6a00d602005-11-21 23:25:50 +0000897 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +0000898#ifdef TARGET_I386
aliguori376253e2009-03-05 23:01:23 +0000899 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000900 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000901#else
aliguori376253e2009-03-05 23:01:23 +0000902 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000903 0);
bellard9307c4c2004-04-04 12:57:25 +0000904#endif
905}
906
aliguori376253e2009-03-05 23:01:23 +0000907static void do_info_jit(Monitor *mon)
bellarde3db7222005-01-26 22:00:47 +0000908{
aliguori376253e2009-03-05 23:01:23 +0000909 dump_exec_info((FILE *)mon, monitor_fprintf);
bellarde3db7222005-01-26 22:00:47 +0000910}
911
aliguori376253e2009-03-05 23:01:23 +0000912static void do_info_history(Monitor *mon)
bellardaa455482004-04-04 13:07:25 +0000913{
914 int i;
bellard7e2515e2004-08-01 21:52:19 +0000915 const char *str;
ths3b46e622007-09-17 08:09:54 +0000916
aliguoricde76ee2009-03-05 23:01:51 +0000917 if (!mon->rs)
918 return;
bellard7e2515e2004-08-01 21:52:19 +0000919 i = 0;
920 for(;;) {
aliguori731b0362009-03-05 23:01:42 +0000921 str = readline_get_history(mon->rs, i);
bellard7e2515e2004-08-01 21:52:19 +0000922 if (!str)
923 break;
aliguori376253e2009-03-05 23:01:23 +0000924 monitor_printf(mon, "%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000925 i++;
bellardaa455482004-04-04 13:07:25 +0000926 }
927}
928
j_mayer76a66252007-03-07 08:32:30 +0000929#if defined(TARGET_PPC)
930/* XXX: not implemented in other targets */
aliguori376253e2009-03-05 23:01:23 +0000931static void do_info_cpu_stats(Monitor *mon)
j_mayer76a66252007-03-07 08:32:30 +0000932{
Andreas Färber9349b4f2012-03-14 01:38:32 +0100933 CPUArchState *env;
j_mayer76a66252007-03-07 08:32:30 +0000934
935 env = mon_get_cpu();
aliguori376253e2009-03-05 23:01:23 +0000936 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
j_mayer76a66252007-03-07 08:32:30 +0000937}
938#endif
939
Lluísfc764102011-08-31 20:31:18 +0200940static void do_trace_print_events(Monitor *mon)
Prerna Saxena22890ab2010-06-24 17:04:53 +0530941{
Lluísfc764102011-08-31 20:31:18 +0200942 trace_print_events((FILE *)mon, &monitor_fprintf);
Prerna Saxena22890ab2010-06-24 17:04:53 +0530943}
Prerna Saxena22890ab2010-06-24 17:04:53 +0530944
Daniel P. Berrange13661082011-06-23 13:31:42 +0100945static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
946{
947 const char *protocol = qdict_get_str(qdict, "protocol");
948 const char *fdname = qdict_get_str(qdict, "fdname");
Daniel P. Berrange13661082011-06-23 13:31:42 +0100949 CharDriverState *s;
950
951 if (strcmp(protocol, "spice") == 0) {
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000952 int fd = monitor_get_fd(mon, fdname);
953 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
954 int tls = qdict_get_try_bool(qdict, "tls", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100955 if (!using_spice) {
956 /* correct one? spice isn't a device ,,, */
957 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
958 return -1;
959 }
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000960 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
961 close(fd);
962 }
963 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800964#ifdef CONFIG_VNC
Daniel P. Berrange13661082011-06-23 13:31:42 +0100965 } else if (strcmp(protocol, "vnc") == 0) {
966 int fd = monitor_get_fd(mon, fdname);
Jamie Iles860341f2011-08-10 15:18:42 +0100967 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
Daniel P. Berrange13661082011-06-23 13:31:42 +0100968 vnc_display_add_client(NULL, fd, skipauth);
969 return 0;
TeLeManc62f6d12011-07-25 16:29:14 +0800970#endif
Daniel P. Berrange13661082011-06-23 13:31:42 +0100971 } else if ((s = qemu_chr_find(protocol)) != NULL) {
972 int fd = monitor_get_fd(mon, fdname);
973 if (qemu_chr_add_client(s, fd) < 0) {
974 qerror_report(QERR_ADD_CLIENT_FAILED);
975 return -1;
976 }
977 return 0;
978 }
979
980 qerror_report(QERR_INVALID_PARAMETER, "protocol");
981 return -1;
982}
983
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200984static int client_migrate_info(Monitor *mon, const QDict *qdict,
985 MonitorCompletion cb, void *opaque)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200986{
987 const char *protocol = qdict_get_str(qdict, "protocol");
988 const char *hostname = qdict_get_str(qdict, "hostname");
989 const char *subject = qdict_get_try_str(qdict, "cert-subject");
990 int port = qdict_get_try_int(qdict, "port", -1);
991 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
992 int ret;
993
994 if (strcmp(protocol, "spice") == 0) {
995 if (!using_spice) {
996 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
997 return -1;
998 }
999
Yonit Halperin6ec5dae2012-03-18 09:42:39 +02001000 if (port == -1 && tls_port == -1) {
1001 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1002 return -1;
1003 }
1004
Yonit Halperinedc5cb12011-10-17 10:03:18 +02001005 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1006 cb, opaque);
Gerd Hoffmanne866e232010-04-23 13:28:21 +02001007 if (ret != 0) {
1008 qerror_report(QERR_UNDEFINED_ERROR);
1009 return -1;
1010 }
1011 return 0;
1012 }
1013
1014 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1015 return -1;
1016}
1017
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001018static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard59a983b2004-03-17 23:17:16 +00001019{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001020 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
Luiz Capitulinof1dc58e2010-03-31 15:21:49 -03001021 return 0;
bellard59a983b2004-03-17 23:17:16 +00001022}
1023
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001024static void do_logfile(Monitor *mon, const QDict *qdict)
pbrooke735b912007-06-30 13:53:24 +00001025{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001026 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrooke735b912007-06-30 13:53:24 +00001027}
1028
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001029static void do_log(Monitor *mon, const QDict *qdict)
bellardf193c792004-03-21 17:06:25 +00001030{
1031 int mask;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001032 const char *items = qdict_get_str(qdict, "items");
ths3b46e622007-09-17 08:09:54 +00001033
bellard9307c4c2004-04-04 12:57:25 +00001034 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +00001035 mask = 0;
1036 } else {
bellard9307c4c2004-04-04 12:57:25 +00001037 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +00001038 if (!mask) {
aliguori376253e2009-03-05 23:01:23 +00001039 help_cmd(mon, "log");
bellardf193c792004-03-21 17:06:25 +00001040 return;
1041 }
1042 }
1043 cpu_set_log(mask);
1044}
1045
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001046static void do_singlestep(Monitor *mon, const QDict *qdict)
aurel321b530a62009-04-05 20:08:59 +00001047{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001048 const char *option = qdict_get_try_str(qdict, "option");
aurel321b530a62009-04-05 20:08:59 +00001049 if (!option || !strcmp(option, "on")) {
1050 singlestep = 1;
1051 } else if (!strcmp(option, "off")) {
1052 singlestep = 0;
1053 } else {
1054 monitor_printf(mon, "unexpected option %s\n", option);
1055 }
1056}
1057
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001058static void do_gdbserver(Monitor *mon, const QDict *qdict)
bellard8a7ddc32004-03-31 19:00:16 +00001059{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001060 const char *device = qdict_get_try_str(qdict, "device");
aliguori59030a82009-04-05 18:43:41 +00001061 if (!device)
1062 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1063 if (gdbserver_start(device) < 0) {
1064 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1065 device);
1066 } else if (strcmp(device, "none") == 0) {
aliguori36556b22009-03-28 18:05:53 +00001067 monitor_printf(mon, "Disabled gdbserver\n");
bellard8a7ddc32004-03-31 19:00:16 +00001068 } else {
aliguori59030a82009-04-05 18:43:41 +00001069 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1070 device);
bellard8a7ddc32004-03-31 19:00:16 +00001071 }
1072}
1073
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001074static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001075{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001076 const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001077 if (select_watchdog_action(action) == -1) {
1078 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1079 }
1080}
1081
aliguori376253e2009-03-05 23:01:23 +00001082static void monitor_printc(Monitor *mon, int c)
bellard9307c4c2004-04-04 12:57:25 +00001083{
aliguori376253e2009-03-05 23:01:23 +00001084 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001085 switch(c) {
1086 case '\'':
aliguori376253e2009-03-05 23:01:23 +00001087 monitor_printf(mon, "\\'");
bellard9307c4c2004-04-04 12:57:25 +00001088 break;
1089 case '\\':
aliguori376253e2009-03-05 23:01:23 +00001090 monitor_printf(mon, "\\\\");
bellard9307c4c2004-04-04 12:57:25 +00001091 break;
1092 case '\n':
aliguori376253e2009-03-05 23:01:23 +00001093 monitor_printf(mon, "\\n");
bellard9307c4c2004-04-04 12:57:25 +00001094 break;
1095 case '\r':
aliguori376253e2009-03-05 23:01:23 +00001096 monitor_printf(mon, "\\r");
bellard9307c4c2004-04-04 12:57:25 +00001097 break;
1098 default:
1099 if (c >= 32 && c <= 126) {
aliguori376253e2009-03-05 23:01:23 +00001100 monitor_printf(mon, "%c", c);
bellard9307c4c2004-04-04 12:57:25 +00001101 } else {
aliguori376253e2009-03-05 23:01:23 +00001102 monitor_printf(mon, "\\x%02x", c);
bellard9307c4c2004-04-04 12:57:25 +00001103 }
1104 break;
1105 }
aliguori376253e2009-03-05 23:01:23 +00001106 monitor_printf(mon, "'");
bellard9307c4c2004-04-04 12:57:25 +00001107}
1108
aliguori376253e2009-03-05 23:01:23 +00001109static void memory_dump(Monitor *mon, int count, int format, int wsize,
Anthony Liguoric227f092009-10-01 16:12:16 -05001110 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +00001111{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001112 CPUArchState *env;
Blue Swirl23842aa2010-01-12 20:27:43 +00001113 int l, line_size, i, max_digits, len;
bellard9307c4c2004-04-04 12:57:25 +00001114 uint8_t buf[16];
1115 uint64_t v;
1116
1117 if (format == 'i') {
1118 int flags;
1119 flags = 0;
bellard6a00d602005-11-21 23:25:50 +00001120 env = mon_get_cpu();
bellard9307c4c2004-04-04 12:57:25 +00001121#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +00001122 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +00001123 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +00001124 } else if (wsize == 4) {
1125 flags = 0;
1126 } else {
bellard6a15fd12006-04-12 21:07:07 +00001127 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +00001128 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +00001129 if (env) {
1130#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +00001131 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +00001132 (env->segs[R_CS].flags & DESC_L_MASK))
1133 flags = 2;
1134 else
1135#endif
1136 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1137 flags = 1;
1138 }
bellard4c27ba22004-04-25 18:05:08 +00001139 }
1140#endif
aliguori376253e2009-03-05 23:01:23 +00001141 monitor_disas(mon, env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +00001142 return;
1143 }
1144
1145 len = wsize * count;
1146 if (wsize == 1)
1147 line_size = 8;
1148 else
1149 line_size = 16;
bellard9307c4c2004-04-04 12:57:25 +00001150 max_digits = 0;
1151
1152 switch(format) {
1153 case 'o':
1154 max_digits = (wsize * 8 + 2) / 3;
1155 break;
1156 default:
1157 case 'x':
1158 max_digits = (wsize * 8) / 4;
1159 break;
1160 case 'u':
1161 case 'd':
1162 max_digits = (wsize * 8 * 10 + 32) / 33;
1163 break;
1164 case 'c':
1165 wsize = 1;
1166 break;
1167 }
1168
1169 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +00001170 if (is_physical)
aliguori376253e2009-03-05 23:01:23 +00001171 monitor_printf(mon, TARGET_FMT_plx ":", addr);
blueswir17743e582007-09-24 18:39:04 +00001172 else
aliguori376253e2009-03-05 23:01:23 +00001173 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +00001174 l = len;
1175 if (l > line_size)
1176 l = line_size;
1177 if (is_physical) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001178 cpu_physical_memory_read(addr, buf, l);
bellard9307c4c2004-04-04 12:57:25 +00001179 } else {
bellard6a00d602005-11-21 23:25:50 +00001180 env = mon_get_cpu();
aliguoric8f79b62008-08-18 14:00:20 +00001181 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
aliguori376253e2009-03-05 23:01:23 +00001182 monitor_printf(mon, " Cannot access memory\n");
aliguoric8f79b62008-08-18 14:00:20 +00001183 break;
1184 }
bellard9307c4c2004-04-04 12:57:25 +00001185 }
ths5fafdf22007-09-16 21:08:06 +00001186 i = 0;
bellard9307c4c2004-04-04 12:57:25 +00001187 while (i < l) {
1188 switch(wsize) {
1189 default:
1190 case 1:
1191 v = ldub_raw(buf + i);
1192 break;
1193 case 2:
1194 v = lduw_raw(buf + i);
1195 break;
1196 case 4:
bellard92a31b12005-02-10 22:00:52 +00001197 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +00001198 break;
1199 case 8:
1200 v = ldq_raw(buf + i);
1201 break;
1202 }
aliguori376253e2009-03-05 23:01:23 +00001203 monitor_printf(mon, " ");
bellard9307c4c2004-04-04 12:57:25 +00001204 switch(format) {
1205 case 'o':
aliguori376253e2009-03-05 23:01:23 +00001206 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001207 break;
1208 case 'x':
aliguori376253e2009-03-05 23:01:23 +00001209 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001210 break;
1211 case 'u':
aliguori376253e2009-03-05 23:01:23 +00001212 monitor_printf(mon, "%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001213 break;
1214 case 'd':
aliguori376253e2009-03-05 23:01:23 +00001215 monitor_printf(mon, "%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +00001216 break;
1217 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001218 monitor_printc(mon, v);
bellard9307c4c2004-04-04 12:57:25 +00001219 break;
1220 }
1221 i += wsize;
1222 }
aliguori376253e2009-03-05 23:01:23 +00001223 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001224 addr += l;
1225 len -= l;
1226 }
1227}
1228
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001229static void do_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001230{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001231 int count = qdict_get_int(qdict, "count");
1232 int format = qdict_get_int(qdict, "format");
1233 int size = qdict_get_int(qdict, "size");
1234 target_long addr = qdict_get_int(qdict, "addr");
1235
aliguori376253e2009-03-05 23:01:23 +00001236 memory_dump(mon, count, format, size, addr, 0);
bellard9307c4c2004-04-04 12:57:25 +00001237}
1238
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001239static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001240{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001241 int count = qdict_get_int(qdict, "count");
1242 int format = qdict_get_int(qdict, "format");
1243 int size = qdict_get_int(qdict, "size");
Anthony Liguoric227f092009-10-01 16:12:16 -05001244 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001245
aliguori376253e2009-03-05 23:01:23 +00001246 memory_dump(mon, count, format, size, addr, 1);
bellard9307c4c2004-04-04 12:57:25 +00001247}
1248
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001249static void do_print(Monitor *mon, const QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00001250{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001251 int format = qdict_get_int(qdict, "format");
Anthony Liguoric227f092009-10-01 16:12:16 -05001252 target_phys_addr_t val = qdict_get_int(qdict, "val");
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001253
bellard9307c4c2004-04-04 12:57:25 +00001254 switch(format) {
1255 case 'o':
Peter Maydell66f27e62012-06-25 16:52:24 +01001256 monitor_printf(mon, "%#" TARGET_PRIoPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001257 break;
1258 case 'x':
Peter Maydell66f27e62012-06-25 16:52:24 +01001259 monitor_printf(mon, "%#" TARGET_PRIxPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001260 break;
1261 case 'u':
Peter Maydell66f27e62012-06-25 16:52:24 +01001262 monitor_printf(mon, "%" TARGET_PRIuPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001263 break;
1264 default:
1265 case 'd':
Peter Maydell66f27e62012-06-25 16:52:24 +01001266 monitor_printf(mon, "%" TARGET_PRIdPHYS, val);
bellard9307c4c2004-04-04 12:57:25 +00001267 break;
1268 case 'c':
aliguori376253e2009-03-05 23:01:23 +00001269 monitor_printc(mon, val);
bellard9307c4c2004-04-04 12:57:25 +00001270 break;
1271 }
aliguori376253e2009-03-05 23:01:23 +00001272 monitor_printf(mon, "\n");
bellard9307c4c2004-04-04 12:57:25 +00001273}
1274
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001275static void do_sum(Monitor *mon, const QDict *qdict)
bellarde4cf1ad2005-06-04 20:15:57 +00001276{
1277 uint32_t addr;
bellarde4cf1ad2005-06-04 20:15:57 +00001278 uint16_t sum;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001279 uint32_t start = qdict_get_int(qdict, "start");
1280 uint32_t size = qdict_get_int(qdict, "size");
bellarde4cf1ad2005-06-04 20:15:57 +00001281
1282 sum = 0;
1283 for(addr = start; addr < (start + size); addr++) {
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001284 uint8_t val = ldub_phys(addr);
bellarde4cf1ad2005-06-04 20:15:57 +00001285 /* BSD sum algorithm ('sum' Unix command) */
1286 sum = (sum >> 1) | (sum << 15);
Stefan Weil54f7b4a2011-04-10 18:23:39 +02001287 sum += val;
bellarde4cf1ad2005-06-04 20:15:57 +00001288 }
aliguori376253e2009-03-05 23:01:23 +00001289 monitor_printf(mon, "%05d\n", sum);
bellarde4cf1ad2005-06-04 20:15:57 +00001290}
1291
bellarda3a91a32004-06-04 11:06:21 +00001292typedef struct {
1293 int keycode;
1294 const char *name;
1295} KeyDef;
1296
1297static const KeyDef key_defs[] = {
1298 { 0x2a, "shift" },
1299 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +00001300
bellarda3a91a32004-06-04 11:06:21 +00001301 { 0x38, "alt" },
1302 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +00001303 { 0x64, "altgr" },
1304 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +00001305 { 0x1d, "ctrl" },
1306 { 0x9d, "ctrl_r" },
1307
1308 { 0xdd, "menu" },
1309
1310 { 0x01, "esc" },
1311
1312 { 0x02, "1" },
1313 { 0x03, "2" },
1314 { 0x04, "3" },
1315 { 0x05, "4" },
1316 { 0x06, "5" },
1317 { 0x07, "6" },
1318 { 0x08, "7" },
1319 { 0x09, "8" },
1320 { 0x0a, "9" },
1321 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +00001322 { 0x0c, "minus" },
1323 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +00001324 { 0x0e, "backspace" },
1325
1326 { 0x0f, "tab" },
1327 { 0x10, "q" },
1328 { 0x11, "w" },
1329 { 0x12, "e" },
1330 { 0x13, "r" },
1331 { 0x14, "t" },
1332 { 0x15, "y" },
1333 { 0x16, "u" },
1334 { 0x17, "i" },
1335 { 0x18, "o" },
1336 { 0x19, "p" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001337 { 0x1a, "bracket_left" },
1338 { 0x1b, "bracket_right" },
bellarda3a91a32004-06-04 11:06:21 +00001339 { 0x1c, "ret" },
1340
1341 { 0x1e, "a" },
1342 { 0x1f, "s" },
1343 { 0x20, "d" },
1344 { 0x21, "f" },
1345 { 0x22, "g" },
1346 { 0x23, "h" },
1347 { 0x24, "j" },
1348 { 0x25, "k" },
1349 { 0x26, "l" },
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001350 { 0x27, "semicolon" },
1351 { 0x28, "apostrophe" },
1352 { 0x29, "grave_accent" },
bellarda3a91a32004-06-04 11:06:21 +00001353
Bernhard M. Wiedemann49b586a2010-06-02 05:32:30 +02001354 { 0x2b, "backslash" },
bellarda3a91a32004-06-04 11:06:21 +00001355 { 0x2c, "z" },
1356 { 0x2d, "x" },
1357 { 0x2e, "c" },
1358 { 0x2f, "v" },
1359 { 0x30, "b" },
1360 { 0x31, "n" },
1361 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +00001362 { 0x33, "comma" },
1363 { 0x34, "dot" },
1364 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +00001365
balrog4d3b6f62008-02-10 16:33:14 +00001366 { 0x37, "asterisk" },
1367
bellarda3a91a32004-06-04 11:06:21 +00001368 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +00001369 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001370 { 0x3b, "f1" },
1371 { 0x3c, "f2" },
1372 { 0x3d, "f3" },
1373 { 0x3e, "f4" },
1374 { 0x3f, "f5" },
1375 { 0x40, "f6" },
1376 { 0x41, "f7" },
1377 { 0x42, "f8" },
1378 { 0x43, "f9" },
1379 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +00001380 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +00001381 { 0x46, "scroll_lock" },
1382
bellard64866c32006-05-07 18:03:31 +00001383 { 0xb5, "kp_divide" },
1384 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +00001385 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +00001386 { 0x4e, "kp_add" },
1387 { 0x9c, "kp_enter" },
1388 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +00001389 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +00001390
1391 { 0x52, "kp_0" },
1392 { 0x4f, "kp_1" },
1393 { 0x50, "kp_2" },
1394 { 0x51, "kp_3" },
1395 { 0x4b, "kp_4" },
1396 { 0x4c, "kp_5" },
1397 { 0x4d, "kp_6" },
1398 { 0x47, "kp_7" },
1399 { 0x48, "kp_8" },
1400 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +00001401
bellarda3a91a32004-06-04 11:06:21 +00001402 { 0x56, "<" },
1403
1404 { 0x57, "f11" },
1405 { 0x58, "f12" },
1406
1407 { 0xb7, "print" },
1408
1409 { 0xc7, "home" },
1410 { 0xc9, "pgup" },
1411 { 0xd1, "pgdn" },
1412 { 0xcf, "end" },
1413
1414 { 0xcb, "left" },
1415 { 0xc8, "up" },
1416 { 0xd0, "down" },
1417 { 0xcd, "right" },
1418
1419 { 0xd2, "insert" },
1420 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +00001421#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1422 { 0xf0, "stop" },
1423 { 0xf1, "again" },
1424 { 0xf2, "props" },
1425 { 0xf3, "undo" },
1426 { 0xf4, "front" },
1427 { 0xf5, "copy" },
1428 { 0xf6, "open" },
1429 { 0xf7, "paste" },
1430 { 0xf8, "find" },
1431 { 0xf9, "cut" },
1432 { 0xfa, "lf" },
1433 { 0xfb, "help" },
1434 { 0xfc, "meta_l" },
1435 { 0xfd, "meta_r" },
1436 { 0xfe, "compose" },
1437#endif
bellarda3a91a32004-06-04 11:06:21 +00001438 { 0, NULL },
1439};
1440
1441static int get_keycode(const char *key)
1442{
1443 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +00001444 char *endp;
1445 int ret;
bellarda3a91a32004-06-04 11:06:21 +00001446
1447 for(p = key_defs; p->name != NULL; p++) {
1448 if (!strcmp(key, p->name))
1449 return p->keycode;
1450 }
bellard64866c32006-05-07 18:03:31 +00001451 if (strstart(key, "0x", NULL)) {
1452 ret = strtoul(key, &endp, 0);
1453 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1454 return ret;
1455 }
bellarda3a91a32004-06-04 11:06:21 +00001456 return -1;
1457}
1458
balrogc8256f92008-06-08 22:45:01 +00001459#define MAX_KEYCODES 16
1460static uint8_t keycodes[MAX_KEYCODES];
1461static int nb_pending_keycodes;
1462static QEMUTimer *key_timer;
1463
1464static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001465{
balrogc8256f92008-06-08 22:45:01 +00001466 int keycode;
1467
1468 while (nb_pending_keycodes > 0) {
1469 nb_pending_keycodes--;
1470 keycode = keycodes[nb_pending_keycodes];
1471 if (keycode & 0x80)
1472 kbd_put_keycode(0xe0);
1473 kbd_put_keycode(keycode | 0x80);
1474 }
1475}
1476
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001477static void do_sendkey(Monitor *mon, const QDict *qdict)
balrogc8256f92008-06-08 22:45:01 +00001478{
balrog3401c0d2008-06-04 10:05:59 +00001479 char keyname_buf[16];
1480 char *separator;
1481 int keyname_len, keycode, i;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001482 const char *string = qdict_get_str(qdict, "string");
1483 int has_hold_time = qdict_haskey(qdict, "hold_time");
1484 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
ths3b46e622007-09-17 08:09:54 +00001485
balrogc8256f92008-06-08 22:45:01 +00001486 if (nb_pending_keycodes > 0) {
1487 qemu_del_timer(key_timer);
1488 release_keys(NULL);
1489 }
1490 if (!has_hold_time)
1491 hold_time = 100;
1492 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001493 while (1) {
1494 separator = strchr(string, '-');
1495 keyname_len = separator ? separator - string : strlen(string);
1496 if (keyname_len > 0) {
1497 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1498 if (keyname_len > sizeof(keyname_buf) - 1) {
aliguori376253e2009-03-05 23:01:23 +00001499 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001500 return;
bellarda3a91a32004-06-04 11:06:21 +00001501 }
balrogc8256f92008-06-08 22:45:01 +00001502 if (i == MAX_KEYCODES) {
aliguori376253e2009-03-05 23:01:23 +00001503 monitor_printf(mon, "too many keys\n");
balrog3401c0d2008-06-04 10:05:59 +00001504 return;
1505 }
1506 keyname_buf[keyname_len] = 0;
1507 keycode = get_keycode(keyname_buf);
1508 if (keycode < 0) {
aliguori376253e2009-03-05 23:01:23 +00001509 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
balrog3401c0d2008-06-04 10:05:59 +00001510 return;
1511 }
balrogc8256f92008-06-08 22:45:01 +00001512 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001513 }
balrog3401c0d2008-06-04 10:05:59 +00001514 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001515 break;
balrog3401c0d2008-06-04 10:05:59 +00001516 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001517 }
balrogc8256f92008-06-08 22:45:01 +00001518 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001519 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001520 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001521 keycode = keycodes[i];
1522 if (keycode & 0x80)
1523 kbd_put_keycode(0xe0);
1524 kbd_put_keycode(keycode & 0x7f);
1525 }
balrogc8256f92008-06-08 22:45:01 +00001526 /* delayed key up events */
Paolo Bonzini74475452011-03-11 16:47:48 +01001527 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
Juan Quintela6ee093c2009-09-10 03:04:26 +02001528 muldiv64(get_ticks_per_sec(), hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001529}
1530
bellard13224a82006-07-14 22:03:35 +00001531static int mouse_button_state;
1532
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001533static void do_mouse_move(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001534{
1535 int dx, dy, dz;
Luiz Capitulino1d4daa92009-08-28 15:27:15 -03001536 const char *dx_str = qdict_get_str(qdict, "dx_str");
1537 const char *dy_str = qdict_get_str(qdict, "dy_str");
1538 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
bellard13224a82006-07-14 22:03:35 +00001539 dx = strtol(dx_str, NULL, 0);
1540 dy = strtol(dy_str, NULL, 0);
1541 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001542 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001543 dz = strtol(dz_str, NULL, 0);
1544 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1545}
1546
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001547static void do_mouse_button(Monitor *mon, const QDict *qdict)
bellard13224a82006-07-14 22:03:35 +00001548{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001549 int button_state = qdict_get_int(qdict, "button_state");
bellard13224a82006-07-14 22:03:35 +00001550 mouse_button_state = button_state;
1551 kbd_mouse_event(0, 0, 0, mouse_button_state);
1552}
1553
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001554static void do_ioport_read(Monitor *mon, const QDict *qdict)
bellard34405572004-06-08 00:55:58 +00001555{
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001556 int size = qdict_get_int(qdict, "size");
1557 int addr = qdict_get_int(qdict, "addr");
1558 int has_index = qdict_haskey(qdict, "index");
bellard34405572004-06-08 00:55:58 +00001559 uint32_t val;
1560 int suffix;
1561
1562 if (has_index) {
Luiz Capitulinoaa93e392009-08-28 15:27:18 -03001563 int index = qdict_get_int(qdict, "index");
Blue Swirlafcea8c2009-09-20 16:05:47 +00001564 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
bellard34405572004-06-08 00:55:58 +00001565 addr++;
1566 }
1567 addr &= 0xffff;
1568
1569 switch(size) {
1570 default:
1571 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001572 val = cpu_inb(addr);
bellard34405572004-06-08 00:55:58 +00001573 suffix = 'b';
1574 break;
1575 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001576 val = cpu_inw(addr);
bellard34405572004-06-08 00:55:58 +00001577 suffix = 'w';
1578 break;
1579 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001580 val = cpu_inl(addr);
bellard34405572004-06-08 00:55:58 +00001581 suffix = 'l';
1582 break;
1583 }
aliguori376253e2009-03-05 23:01:23 +00001584 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1585 suffix, addr, size * 2, val);
bellard34405572004-06-08 00:55:58 +00001586}
bellarda3a91a32004-06-04 11:06:21 +00001587
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001588static void do_ioport_write(Monitor *mon, const QDict *qdict)
Jan Kiszkaf1147842009-07-14 10:20:11 +02001589{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03001590 int size = qdict_get_int(qdict, "size");
1591 int addr = qdict_get_int(qdict, "addr");
1592 int val = qdict_get_int(qdict, "val");
1593
Jan Kiszkaf1147842009-07-14 10:20:11 +02001594 addr &= IOPORTS_MASK;
1595
1596 switch (size) {
1597 default:
1598 case 1:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001599 cpu_outb(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001600 break;
1601 case 2:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001602 cpu_outw(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001603 break;
1604 case 4:
Blue Swirlafcea8c2009-09-20 16:05:47 +00001605 cpu_outl(addr, val);
Jan Kiszkaf1147842009-07-14 10:20:11 +02001606 break;
1607 }
1608}
1609
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001610static void do_boot_set(Monitor *mon, const QDict *qdict)
aurel320ecdffb2008-05-04 20:11:34 +00001611{
1612 int res;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001613 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
aurel320ecdffb2008-05-04 20:11:34 +00001614
Jan Kiszka76e30d02009-07-02 00:19:02 +02001615 res = qemu_boot_set(bootdevice);
1616 if (res == 0) {
1617 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1618 } else if (res > 0) {
1619 monitor_printf(mon, "setting boot device list failed\n");
aurel320ecdffb2008-05-04 20:11:34 +00001620 } else {
aliguori376253e2009-03-05 23:01:23 +00001621 monitor_printf(mon, "no function defined to set boot device list for "
1622 "this architecture\n");
aurel320ecdffb2008-05-04 20:11:34 +00001623 }
1624}
1625
bellardb86bda52004-09-18 19:32:46 +00001626#if defined(TARGET_I386)
Blue Swirld65aaf32010-12-11 18:56:24 +00001627static void print_pte(Monitor *mon, target_phys_addr_t addr,
1628 target_phys_addr_t pte,
1629 target_phys_addr_t mask)
bellardb86bda52004-09-18 19:32:46 +00001630{
Blue Swirld65aaf32010-12-11 18:56:24 +00001631#ifdef TARGET_X86_64
1632 if (addr & (1ULL << 47)) {
1633 addr |= -1LL << 48;
1634 }
1635#endif
1636 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1637 " %c%c%c%c%c%c%c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001638 addr,
1639 pte & mask,
Blue Swirld65aaf32010-12-11 18:56:24 +00001640 pte & PG_NX_MASK ? 'X' : '-',
aliguori376253e2009-03-05 23:01:23 +00001641 pte & PG_GLOBAL_MASK ? 'G' : '-',
1642 pte & PG_PSE_MASK ? 'P' : '-',
1643 pte & PG_DIRTY_MASK ? 'D' : '-',
1644 pte & PG_ACCESSED_MASK ? 'A' : '-',
1645 pte & PG_PCD_MASK ? 'C' : '-',
1646 pte & PG_PWT_MASK ? 'T' : '-',
1647 pte & PG_USER_MASK ? 'U' : '-',
1648 pte & PG_RW_MASK ? 'W' : '-');
bellardb86bda52004-09-18 19:32:46 +00001649}
1650
Andreas Färber9349b4f2012-03-14 01:38:32 +01001651static void tlb_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001652{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001653 unsigned int l1, l2;
bellardb86bda52004-09-18 19:32:46 +00001654 uint32_t pgd, pde, pte;
1655
bellardb86bda52004-09-18 19:32:46 +00001656 pgd = env->cr[3] & ~0xfff;
1657 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001658 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001659 pde = le32_to_cpu(pde);
1660 if (pde & PG_PRESENT_MASK) {
1661 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
Blue Swirld65aaf32010-12-11 18:56:24 +00001662 /* 4M pages */
1663 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
bellardb86bda52004-09-18 19:32:46 +00001664 } else {
1665 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001666 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001667 pte = le32_to_cpu(pte);
1668 if (pte & PG_PRESENT_MASK) {
aliguori376253e2009-03-05 23:01:23 +00001669 print_pte(mon, (l1 << 22) + (l2 << 12),
ths5fafdf22007-09-16 21:08:06 +00001670 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001671 ~0xfff);
1672 }
1673 }
1674 }
1675 }
1676 }
1677}
1678
Andreas Färber9349b4f2012-03-14 01:38:32 +01001679static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001680{
Austin Clements94ac5cd2011-08-21 14:49:45 -04001681 unsigned int l1, l2, l3;
Blue Swirld65aaf32010-12-11 18:56:24 +00001682 uint64_t pdpe, pde, pte;
1683 uint64_t pdp_addr, pd_addr, pt_addr;
1684
1685 pdp_addr = env->cr[3] & ~0x1f;
1686 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001687 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001688 pdpe = le64_to_cpu(pdpe);
1689 if (pdpe & PG_PRESENT_MASK) {
1690 pd_addr = pdpe & 0x3fffffffff000ULL;
1691 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001692 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001693 pde = le64_to_cpu(pde);
1694 if (pde & PG_PRESENT_MASK) {
1695 if (pde & PG_PSE_MASK) {
1696 /* 2M pages with PAE, CR4.PSE is ignored */
1697 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1698 ~((target_phys_addr_t)(1 << 20) - 1));
1699 } else {
1700 pt_addr = pde & 0x3fffffffff000ULL;
1701 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001702 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001703 pte = le64_to_cpu(pte);
1704 if (pte & PG_PRESENT_MASK) {
1705 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1706 + (l3 << 12),
1707 pte & ~PG_PSE_MASK,
1708 ~(target_phys_addr_t)0xfff);
1709 }
1710 }
1711 }
1712 }
1713 }
1714 }
1715 }
1716}
1717
1718#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001719static void tlb_info_64(Monitor *mon, CPUArchState *env)
Blue Swirld65aaf32010-12-11 18:56:24 +00001720{
1721 uint64_t l1, l2, l3, l4;
1722 uint64_t pml4e, pdpe, pde, pte;
1723 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1724
1725 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1726 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001727 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001728 pml4e = le64_to_cpu(pml4e);
1729 if (pml4e & PG_PRESENT_MASK) {
1730 pdp_addr = pml4e & 0x3fffffffff000ULL;
1731 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001732 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001733 pdpe = le64_to_cpu(pdpe);
1734 if (pdpe & PG_PRESENT_MASK) {
1735 if (pdpe & PG_PSE_MASK) {
1736 /* 1G pages, CR4.PSE is ignored */
1737 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1738 0x3ffffc0000000ULL);
1739 } else {
1740 pd_addr = pdpe & 0x3fffffffff000ULL;
1741 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001742 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001743 pde = le64_to_cpu(pde);
1744 if (pde & PG_PRESENT_MASK) {
1745 if (pde & PG_PSE_MASK) {
1746 /* 2M pages, CR4.PSE is ignored */
1747 print_pte(mon, (l1 << 39) + (l2 << 30) +
1748 (l3 << 21), pde,
1749 0x3ffffffe00000ULL);
1750 } else {
1751 pt_addr = pde & 0x3fffffffff000ULL;
1752 for (l4 = 0; l4 < 512; l4++) {
1753 cpu_physical_memory_read(pt_addr
1754 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001755 &pte, 8);
Blue Swirld65aaf32010-12-11 18:56:24 +00001756 pte = le64_to_cpu(pte);
1757 if (pte & PG_PRESENT_MASK) {
1758 print_pte(mon, (l1 << 39) +
1759 (l2 << 30) +
1760 (l3 << 21) + (l4 << 12),
1761 pte & ~PG_PSE_MASK,
1762 0x3fffffffff000ULL);
1763 }
1764 }
1765 }
1766 }
1767 }
1768 }
1769 }
1770 }
1771 }
1772 }
1773}
1774#endif
1775
1776static void tlb_info(Monitor *mon)
1777{
Andreas Färber9349b4f2012-03-14 01:38:32 +01001778 CPUArchState *env;
Blue Swirld65aaf32010-12-11 18:56:24 +00001779
1780 env = mon_get_cpu();
1781
1782 if (!(env->cr[0] & CR0_PG_MASK)) {
1783 monitor_printf(mon, "PG disabled\n");
1784 return;
1785 }
1786 if (env->cr[4] & CR4_PAE_MASK) {
1787#ifdef TARGET_X86_64
1788 if (env->hflags & HF_LMA_MASK) {
1789 tlb_info_64(mon, env);
1790 } else
1791#endif
1792 {
1793 tlb_info_pae32(mon, env);
1794 }
1795 } else {
1796 tlb_info_32(mon, env);
1797 }
1798}
1799
Blue Swirl1b3cba62010-12-11 18:56:27 +00001800static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
1801 int *plast_prot,
1802 target_phys_addr_t end, int prot)
bellardb86bda52004-09-18 19:32:46 +00001803{
bellard9746b152004-11-11 18:30:24 +00001804 int prot1;
1805 prot1 = *plast_prot;
1806 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001807 if (*pstart != -1) {
Blue Swirl1b3cba62010-12-11 18:56:27 +00001808 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1809 TARGET_FMT_plx " %c%c%c\n",
aliguori376253e2009-03-05 23:01:23 +00001810 *pstart, end, end - *pstart,
1811 prot1 & PG_USER_MASK ? 'u' : '-',
1812 'r',
1813 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001814 }
1815 if (prot != 0)
1816 *pstart = end;
1817 else
1818 *pstart = -1;
1819 *plast_prot = prot;
1820 }
1821}
1822
Andreas Färber9349b4f2012-03-14 01:38:32 +01001823static void mem_info_32(Monitor *mon, CPUArchState *env)
bellardb86bda52004-09-18 19:32:46 +00001824{
Austin Clementsb49ca722011-08-14 23:19:21 -04001825 unsigned int l1, l2;
1826 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001827 uint32_t pgd, pde, pte;
1828 target_phys_addr_t start, end;
bellardb86bda52004-09-18 19:32:46 +00001829
bellardb86bda52004-09-18 19:32:46 +00001830 pgd = env->cr[3] & ~0xfff;
1831 last_prot = 0;
1832 start = -1;
1833 for(l1 = 0; l1 < 1024; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001834 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
bellardb86bda52004-09-18 19:32:46 +00001835 pde = le32_to_cpu(pde);
1836 end = l1 << 22;
1837 if (pde & PG_PRESENT_MASK) {
1838 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1839 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
aliguori376253e2009-03-05 23:01:23 +00001840 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001841 } else {
1842 for(l2 = 0; l2 < 1024; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001843 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
bellardb86bda52004-09-18 19:32:46 +00001844 pte = le32_to_cpu(pte);
1845 end = (l1 << 22) + (l2 << 12);
1846 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001847 prot = pte & pde &
1848 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
bellardb86bda52004-09-18 19:32:46 +00001849 } else {
1850 prot = 0;
1851 }
aliguori376253e2009-03-05 23:01:23 +00001852 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001853 }
1854 }
1855 } else {
1856 prot = 0;
aliguori376253e2009-03-05 23:01:23 +00001857 mem_print(mon, &start, &last_prot, end, prot);
bellardb86bda52004-09-18 19:32:46 +00001858 }
1859 }
Austin Clements8a94b8c2011-08-14 23:22:04 -04001860 /* Flush last range */
1861 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
bellardb86bda52004-09-18 19:32:46 +00001862}
Blue Swirl1b3cba62010-12-11 18:56:27 +00001863
Andreas Färber9349b4f2012-03-14 01:38:32 +01001864static void mem_info_pae32(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001865{
Austin Clementsb49ca722011-08-14 23:19:21 -04001866 unsigned int l1, l2, l3;
1867 int prot, last_prot;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001868 uint64_t pdpe, pde, pte;
1869 uint64_t pdp_addr, pd_addr, pt_addr;
1870 target_phys_addr_t start, end;
1871
1872 pdp_addr = env->cr[3] & ~0x1f;
1873 last_prot = 0;
1874 start = -1;
1875 for (l1 = 0; l1 < 4; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001876 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001877 pdpe = le64_to_cpu(pdpe);
1878 end = l1 << 30;
1879 if (pdpe & PG_PRESENT_MASK) {
1880 pd_addr = pdpe & 0x3fffffffff000ULL;
1881 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001882 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001883 pde = le64_to_cpu(pde);
1884 end = (l1 << 30) + (l2 << 21);
1885 if (pde & PG_PRESENT_MASK) {
1886 if (pde & PG_PSE_MASK) {
1887 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1888 PG_PRESENT_MASK);
1889 mem_print(mon, &start, &last_prot, end, prot);
1890 } else {
1891 pt_addr = pde & 0x3fffffffff000ULL;
1892 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001893 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001894 pte = le64_to_cpu(pte);
1895 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1896 if (pte & PG_PRESENT_MASK) {
Austin Clementsc76c8412011-08-14 23:22:28 -04001897 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1898 PG_PRESENT_MASK);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001899 } else {
1900 prot = 0;
1901 }
1902 mem_print(mon, &start, &last_prot, end, prot);
1903 }
1904 }
1905 } else {
1906 prot = 0;
1907 mem_print(mon, &start, &last_prot, end, prot);
1908 }
1909 }
1910 } else {
1911 prot = 0;
1912 mem_print(mon, &start, &last_prot, end, prot);
1913 }
1914 }
Austin Clements8a94b8c2011-08-14 23:22:04 -04001915 /* Flush last range */
1916 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001917}
1918
1919
1920#ifdef TARGET_X86_64
Andreas Färber9349b4f2012-03-14 01:38:32 +01001921static void mem_info_64(Monitor *mon, CPUArchState *env)
Blue Swirl1b3cba62010-12-11 18:56:27 +00001922{
1923 int prot, last_prot;
1924 uint64_t l1, l2, l3, l4;
1925 uint64_t pml4e, pdpe, pde, pte;
1926 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1927
1928 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1929 last_prot = 0;
1930 start = -1;
1931 for (l1 = 0; l1 < 512; l1++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001932 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001933 pml4e = le64_to_cpu(pml4e);
1934 end = l1 << 39;
1935 if (pml4e & PG_PRESENT_MASK) {
1936 pdp_addr = pml4e & 0x3fffffffff000ULL;
1937 for (l2 = 0; l2 < 512; l2++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001938 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001939 pdpe = le64_to_cpu(pdpe);
1940 end = (l1 << 39) + (l2 << 30);
1941 if (pdpe & PG_PRESENT_MASK) {
1942 if (pdpe & PG_PSE_MASK) {
Blue Swirl2d5b5072011-01-15 08:31:00 +00001943 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1944 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001945 prot &= pml4e;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001946 mem_print(mon, &start, &last_prot, end, prot);
1947 } else {
1948 pd_addr = pdpe & 0x3fffffffff000ULL;
1949 for (l3 = 0; l3 < 512; l3++) {
Stefan Weilb8b79322011-03-26 21:11:05 +01001950 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001951 pde = le64_to_cpu(pde);
1952 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1953 if (pde & PG_PRESENT_MASK) {
1954 if (pde & PG_PSE_MASK) {
1955 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1956 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001957 prot &= pml4e & pdpe;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001958 mem_print(mon, &start, &last_prot, end, prot);
1959 } else {
1960 pt_addr = pde & 0x3fffffffff000ULL;
1961 for (l4 = 0; l4 < 512; l4++) {
1962 cpu_physical_memory_read(pt_addr
1963 + l4 * 8,
Stefan Weilb8b79322011-03-26 21:11:05 +01001964 &pte, 8);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001965 pte = le64_to_cpu(pte);
1966 end = (l1 << 39) + (l2 << 30) +
1967 (l3 << 21) + (l4 << 12);
1968 if (pte & PG_PRESENT_MASK) {
1969 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1970 PG_PRESENT_MASK);
Austin Clementsc76c8412011-08-14 23:22:28 -04001971 prot &= pml4e & pdpe & pde;
Blue Swirl1b3cba62010-12-11 18:56:27 +00001972 } else {
1973 prot = 0;
1974 }
1975 mem_print(mon, &start, &last_prot, end, prot);
1976 }
1977 }
1978 } else {
1979 prot = 0;
1980 mem_print(mon, &start, &last_prot, end, prot);
1981 }
1982 }
1983 }
1984 } else {
1985 prot = 0;
1986 mem_print(mon, &start, &last_prot, end, prot);
1987 }
1988 }
1989 } else {
1990 prot = 0;
1991 mem_print(mon, &start, &last_prot, end, prot);
1992 }
1993 }
Austin Clements8a94b8c2011-08-14 23:22:04 -04001994 /* Flush last range */
1995 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
Blue Swirl1b3cba62010-12-11 18:56:27 +00001996}
1997#endif
1998
1999static void mem_info(Monitor *mon)
2000{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002001 CPUArchState *env;
Blue Swirl1b3cba62010-12-11 18:56:27 +00002002
2003 env = mon_get_cpu();
2004
2005 if (!(env->cr[0] & CR0_PG_MASK)) {
2006 monitor_printf(mon, "PG disabled\n");
2007 return;
2008 }
2009 if (env->cr[4] & CR4_PAE_MASK) {
2010#ifdef TARGET_X86_64
2011 if (env->hflags & HF_LMA_MASK) {
2012 mem_info_64(mon, env);
2013 } else
2014#endif
2015 {
2016 mem_info_pae32(mon, env);
2017 }
2018 } else {
2019 mem_info_32(mon, env);
2020 }
2021}
bellardb86bda52004-09-18 19:32:46 +00002022#endif
2023
aurel327c664e22009-03-03 06:12:22 +00002024#if defined(TARGET_SH4)
2025
aliguori376253e2009-03-05 23:01:23 +00002026static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
aurel327c664e22009-03-03 06:12:22 +00002027{
aliguori376253e2009-03-05 23:01:23 +00002028 monitor_printf(mon, " tlb%i:\t"
2029 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2030 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2031 "dirty=%hhu writethrough=%hhu\n",
2032 idx,
2033 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2034 tlb->v, tlb->sh, tlb->c, tlb->pr,
2035 tlb->d, tlb->wt);
aurel327c664e22009-03-03 06:12:22 +00002036}
2037
aliguori376253e2009-03-05 23:01:23 +00002038static void tlb_info(Monitor *mon)
aurel327c664e22009-03-03 06:12:22 +00002039{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002040 CPUArchState *env = mon_get_cpu();
aurel327c664e22009-03-03 06:12:22 +00002041 int i;
2042
aliguori376253e2009-03-05 23:01:23 +00002043 monitor_printf (mon, "ITLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002044 for (i = 0 ; i < ITLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002045 print_tlb (mon, i, &env->itlb[i]);
2046 monitor_printf (mon, "UTLB:\n");
aurel327c664e22009-03-03 06:12:22 +00002047 for (i = 0 ; i < UTLB_SIZE ; i++)
aliguori376253e2009-03-05 23:01:23 +00002048 print_tlb (mon, i, &env->utlb[i]);
aurel327c664e22009-03-03 06:12:22 +00002049}
2050
2051#endif
2052
Max Filippov692f7372012-01-07 20:02:40 +04002053#if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
Blue Swirld41160a2010-12-19 13:42:56 +00002054static void tlb_info(Monitor *mon)
2055{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002056 CPUArchState *env1 = mon_get_cpu();
Blue Swirld41160a2010-12-19 13:42:56 +00002057
2058 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2059}
2060#endif
2061
Blue Swirl314e2982011-09-11 20:22:05 +00002062static void do_info_mtree(Monitor *mon)
2063{
2064 mtree_info((fprintf_function)monitor_printf, mon);
2065}
2066
aliguori030ea372009-04-21 22:30:47 +00002067static void do_info_numa(Monitor *mon)
2068{
aliguorib28b6232009-04-22 20:20:29 +00002069 int i;
Andreas Färber9349b4f2012-03-14 01:38:32 +01002070 CPUArchState *env;
aliguori030ea372009-04-21 22:30:47 +00002071
2072 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2073 for (i = 0; i < nb_numa_nodes; i++) {
2074 monitor_printf(mon, "node %d cpus:", i);
2075 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2076 if (env->numa_node == i) {
2077 monitor_printf(mon, " %d", env->cpu_index);
2078 }
2079 }
2080 monitor_printf(mon, "\n");
2081 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2082 node_mem[i] >> 20);
2083 }
2084}
2085
bellard5f1ce942006-02-08 22:40:15 +00002086#ifdef CONFIG_PROFILER
2087
Aurelien Jarnoe9a66252009-09-30 14:09:52 +02002088int64_t qemu_time;
2089int64_t dev_time;
2090
aliguori376253e2009-03-05 23:01:23 +00002091static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002092{
2093 int64_t total;
2094 total = qemu_time;
2095 if (total == 0)
2096 total = 1;
aliguori376253e2009-03-05 23:01:23 +00002097 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002098 dev_time, dev_time / (double)get_ticks_per_sec());
aliguori376253e2009-03-05 23:01:23 +00002099 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02002100 qemu_time, qemu_time / (double)get_ticks_per_sec());
bellard5f1ce942006-02-08 22:40:15 +00002101 qemu_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002102 dev_time = 0;
bellard5f1ce942006-02-08 22:40:15 +00002103}
2104#else
aliguori376253e2009-03-05 23:01:23 +00002105static void do_info_profile(Monitor *mon)
bellard5f1ce942006-02-08 22:40:15 +00002106{
aliguori376253e2009-03-05 23:01:23 +00002107 monitor_printf(mon, "Internal profiler not compiled\n");
bellard5f1ce942006-02-08 22:40:15 +00002108}
2109#endif
2110
bellardec36b692006-07-16 18:57:03 +00002111/* Capture support */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002112static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
bellardec36b692006-07-16 18:57:03 +00002113
aliguori376253e2009-03-05 23:01:23 +00002114static void do_info_capture(Monitor *mon)
bellardec36b692006-07-16 18:57:03 +00002115{
2116 int i;
2117 CaptureState *s;
2118
2119 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
aliguori376253e2009-03-05 23:01:23 +00002120 monitor_printf(mon, "[%d]: ", i);
bellardec36b692006-07-16 18:57:03 +00002121 s->ops.info (s->opaque);
2122 }
2123}
2124
Blue Swirl23130862009-06-06 08:22:04 +00002125#ifdef HAS_AUDIO
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002126static void do_stop_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002127{
2128 int i;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002129 int n = qdict_get_int(qdict, "n");
bellardec36b692006-07-16 18:57:03 +00002130 CaptureState *s;
2131
2132 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2133 if (i == n) {
2134 s->ops.destroy (s->opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002135 QLIST_REMOVE (s, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002136 g_free (s);
bellardec36b692006-07-16 18:57:03 +00002137 return;
2138 }
2139 }
2140}
2141
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002142static void do_wav_capture(Monitor *mon, const QDict *qdict)
bellardec36b692006-07-16 18:57:03 +00002143{
Luiz Capitulinoc1925482009-08-28 15:27:19 -03002144 const char *path = qdict_get_str(qdict, "path");
2145 int has_freq = qdict_haskey(qdict, "freq");
2146 int freq = qdict_get_try_int(qdict, "freq", -1);
2147 int has_bits = qdict_haskey(qdict, "bits");
2148 int bits = qdict_get_try_int(qdict, "bits", -1);
2149 int has_channels = qdict_haskey(qdict, "nchannels");
2150 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
bellardec36b692006-07-16 18:57:03 +00002151 CaptureState *s;
2152
Anthony Liguori7267c092011-08-20 22:09:37 -05002153 s = g_malloc0 (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00002154
2155 freq = has_freq ? freq : 44100;
2156 bits = has_bits ? bits : 16;
2157 nchannels = has_channels ? nchannels : 2;
2158
2159 if (wav_start_capture (s, path, freq, bits, nchannels)) {
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002160 monitor_printf(mon, "Failed to add wave capture\n");
Anthony Liguori7267c092011-08-20 22:09:37 -05002161 g_free (s);
Isaku Yamahatad00b2612011-01-21 19:53:55 +09002162 return;
bellardec36b692006-07-16 18:57:03 +00002163 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002164 QLIST_INSERT_HEAD (&capture_head, s, entries);
bellardec36b692006-07-16 18:57:03 +00002165}
2166#endif
2167
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002168static qemu_acl *find_acl(Monitor *mon, const char *name)
aliguori76655d62009-03-06 20:27:37 +00002169{
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002170 qemu_acl *acl = qemu_acl_find(name);
aliguori76655d62009-03-06 20:27:37 +00002171
aliguori76655d62009-03-06 20:27:37 +00002172 if (!acl) {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002173 monitor_printf(mon, "acl: unknown list '%s'\n", name);
aliguori76655d62009-03-06 20:27:37 +00002174 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002175 return acl;
2176}
aliguori76655d62009-03-06 20:27:37 +00002177
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002178static void do_acl_show(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002179{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002180 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002181 qemu_acl *acl = find_acl(mon, aclname);
2182 qemu_acl_entry *entry;
2183 int i = 0;
2184
2185 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002186 monitor_printf(mon, "policy: %s\n",
aliguori76655d62009-03-06 20:27:37 +00002187 acl->defaultDeny ? "deny" : "allow");
Blue Swirl72cf2d42009-09-12 07:36:22 +00002188 QTAILQ_FOREACH(entry, &acl->entries, next) {
aliguori28a76be2009-03-06 20:27:40 +00002189 i++;
2190 monitor_printf(mon, "%d: %s %s\n", i,
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002191 entry->deny ? "deny" : "allow", entry->match);
aliguori28a76be2009-03-06 20:27:40 +00002192 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002193 }
2194}
2195
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002196static void do_acl_reset(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002197{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002198 const char *aclname = qdict_get_str(qdict, "aclname");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002199 qemu_acl *acl = find_acl(mon, aclname);
2200
2201 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002202 qemu_acl_reset(acl);
2203 monitor_printf(mon, "acl: removed all rules\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002204 }
2205}
aliguori76655d62009-03-06 20:27:37 +00002206
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002207static void do_acl_policy(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002208{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002209 const char *aclname = qdict_get_str(qdict, "aclname");
2210 const char *policy = qdict_get_str(qdict, "policy");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002211 qemu_acl *acl = find_acl(mon, aclname);
2212
2213 if (acl) {
2214 if (strcmp(policy, "allow") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002215 acl->defaultDeny = 0;
2216 monitor_printf(mon, "acl: policy set to 'allow'\n");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002217 } else if (strcmp(policy, "deny") == 0) {
aliguori28a76be2009-03-06 20:27:40 +00002218 acl->defaultDeny = 1;
2219 monitor_printf(mon, "acl: policy set to 'deny'\n");
2220 } else {
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002221 monitor_printf(mon, "acl: unknown policy '%s', "
2222 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002223 }
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002224 }
2225}
aliguori76655d62009-03-06 20:27:37 +00002226
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002227static void do_acl_add(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002228{
Luiz Capitulino1bd14422009-08-28 15:27:17 -03002229 const char *aclname = qdict_get_str(qdict, "aclname");
2230 const char *match = qdict_get_str(qdict, "match");
2231 const char *policy = qdict_get_str(qdict, "policy");
2232 int has_index = qdict_haskey(qdict, "index");
2233 int index = qdict_get_try_int(qdict, "index", -1);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002234 qemu_acl *acl = find_acl(mon, aclname);
2235 int deny, ret;
2236
2237 if (acl) {
2238 if (strcmp(policy, "allow") == 0) {
2239 deny = 0;
2240 } else if (strcmp(policy, "deny") == 0) {
2241 deny = 1;
2242 } else {
2243 monitor_printf(mon, "acl: unknown policy '%s', "
2244 "expected 'deny' or 'allow'\n", policy);
aliguori28a76be2009-03-06 20:27:40 +00002245 return;
2246 }
aliguori28a76be2009-03-06 20:27:40 +00002247 if (has_index)
2248 ret = qemu_acl_insert(acl, deny, match, index);
2249 else
2250 ret = qemu_acl_append(acl, deny, match);
2251 if (ret < 0)
2252 monitor_printf(mon, "acl: unable to add acl entry\n");
2253 else
2254 monitor_printf(mon, "acl: added rule at position %d\n", ret);
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002255 }
2256}
aliguori76655d62009-03-06 20:27:37 +00002257
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002258static void do_acl_remove(Monitor *mon, const QDict *qdict)
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002259{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03002260 const char *aclname = qdict_get_str(qdict, "aclname");
2261 const char *match = qdict_get_str(qdict, "match");
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002262 qemu_acl *acl = find_acl(mon, aclname);
2263 int ret;
aliguori76655d62009-03-06 20:27:37 +00002264
Jan Kiszka15dfcd42009-06-25 08:22:08 +02002265 if (acl) {
aliguori28a76be2009-03-06 20:27:40 +00002266 ret = qemu_acl_remove(acl, match);
2267 if (ret < 0)
2268 monitor_printf(mon, "acl: no matching acl entry\n");
2269 else
2270 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
aliguori76655d62009-03-06 20:27:37 +00002271 }
2272}
2273
Huang Ying79c4f6b2009-06-23 10:05:14 +08002274#if defined(TARGET_I386)
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002275static void do_inject_mce(Monitor *mon, const QDict *qdict)
Huang Ying79c4f6b2009-06-23 10:05:14 +08002276{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002277 CPUArchState *cenv;
Luiz Capitulino37b7ad42009-08-28 15:27:21 -03002278 int cpu_index = qdict_get_int(qdict, "cpu_index");
2279 int bank = qdict_get_int(qdict, "bank");
2280 uint64_t status = qdict_get_int(qdict, "status");
2281 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2282 uint64_t addr = qdict_get_int(qdict, "addr");
2283 uint64_t misc = qdict_get_int(qdict, "misc");
Jan Kiszka747461c2011-03-02 08:56:10 +01002284 int flags = MCE_INJECT_UNCOND_AO;
Huang Ying79c4f6b2009-06-23 10:05:14 +08002285
Jan Kiszka747461c2011-03-02 08:56:10 +01002286 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2287 flags |= MCE_INJECT_BROADCAST;
2288 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002289 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
Jan Kiszka316378e2011-03-02 08:56:09 +01002290 if (cenv->cpu_index == cpu_index) {
2291 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
Jan Kiszka747461c2011-03-02 08:56:10 +01002292 flags);
Huang Ying79c4f6b2009-06-23 10:05:14 +08002293 break;
2294 }
Jin Dongming31ce5e02010-12-10 17:21:02 +09002295 }
Huang Ying79c4f6b2009-06-23 10:05:14 +08002296}
2297#endif
2298
Corey Bryant208c9d12012-06-22 14:36:09 -04002299void qmp_getfd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002300{
Anthony Liguoric227f092009-10-01 16:12:16 -05002301 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002302 int fd;
2303
Corey Bryant208c9d12012-06-22 14:36:09 -04002304 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002305 if (fd == -1) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002306 error_set(errp, QERR_FD_NOT_SUPPLIED);
2307 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002308 }
2309
2310 if (qemu_isdigit(fdname[0])) {
Corey Bryant208c9d12012-06-22 14:36:09 -04002311 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2312 "a name not starting with a digit");
2313 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002314 }
2315
Corey Bryant208c9d12012-06-22 14:36:09 -04002316 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002317 if (strcmp(monfd->name, fdname) != 0) {
2318 continue;
2319 }
2320
2321 close(monfd->fd);
2322 monfd->fd = fd;
Corey Bryant208c9d12012-06-22 14:36:09 -04002323 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002324 }
2325
Anthony Liguori7267c092011-08-20 22:09:37 -05002326 monfd = g_malloc0(sizeof(mon_fd_t));
2327 monfd->name = g_strdup(fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002328 monfd->fd = fd;
2329
Corey Bryant208c9d12012-06-22 14:36:09 -04002330 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002331}
2332
Corey Bryant208c9d12012-06-22 14:36:09 -04002333void qmp_closefd(const char *fdname, Error **errp)
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002334{
Anthony Liguoric227f092009-10-01 16:12:16 -05002335 mon_fd_t *monfd;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002336
Corey Bryant208c9d12012-06-22 14:36:09 -04002337 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002338 if (strcmp(monfd->name, fdname) != 0) {
2339 continue;
2340 }
2341
Blue Swirl72cf2d42009-09-12 07:36:22 +00002342 QLIST_REMOVE(monfd, next);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002343 close(monfd->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -05002344 g_free(monfd->name);
2345 g_free(monfd);
Corey Bryant208c9d12012-06-22 14:36:09 -04002346 return;
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002347 }
2348
Corey Bryant208c9d12012-06-22 14:36:09 -04002349 error_set(errp, QERR_FD_NOT_FOUND, fdname);
Mark McLoughlinf07918f2009-07-22 09:11:40 +01002350}
2351
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002352static void do_loadvm(Monitor *mon, const QDict *qdict)
Juan Quintelac8d41b22009-08-20 19:42:21 +02002353{
Luiz Capitulino13548692011-07-29 15:36:43 -03002354 int saved_vm_running = runstate_is_running();
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002355 const char *name = qdict_get_str(qdict, "name");
Juan Quintelac8d41b22009-08-20 19:42:21 +02002356
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002357 vm_stop(RUN_STATE_RESTORE_VM);
Juan Quintelac8d41b22009-08-20 19:42:21 +02002358
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002359 if (load_vmstate(name) == 0 && saved_vm_running) {
Juan Quintelac8d41b22009-08-20 19:42:21 +02002360 vm_start();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002361 }
Juan Quintelac8d41b22009-08-20 19:42:21 +02002362}
2363
Mark McLoughlin7768e042009-07-22 09:11:41 +01002364int monitor_get_fd(Monitor *mon, const char *fdname)
2365{
Anthony Liguoric227f092009-10-01 16:12:16 -05002366 mon_fd_t *monfd;
Mark McLoughlin7768e042009-07-22 09:11:41 +01002367
Blue Swirl72cf2d42009-09-12 07:36:22 +00002368 QLIST_FOREACH(monfd, &mon->fds, next) {
Mark McLoughlin7768e042009-07-22 09:11:41 +01002369 int fd;
2370
2371 if (strcmp(monfd->name, fdname) != 0) {
2372 continue;
2373 }
2374
2375 fd = monfd->fd;
2376
2377 /* caller takes ownership of fd */
Blue Swirl72cf2d42009-09-12 07:36:22 +00002378 QLIST_REMOVE(monfd, next);
Anthony Liguori7267c092011-08-20 22:09:37 -05002379 g_free(monfd->name);
2380 g_free(monfd);
Mark McLoughlin7768e042009-07-22 09:11:41 +01002381
2382 return fd;
2383 }
2384
2385 return -1;
2386}
2387
Corey Bryantba1c0482012-08-14 16:43:43 -04002388static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2389{
2390 MonFdsetFd *mon_fdset_fd;
2391 MonFdsetFd *mon_fdset_fd_next;
2392
2393 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
2394 if (mon_fdset_fd->removed) {
2395 close(mon_fdset_fd->fd);
2396 g_free(mon_fdset_fd->opaque);
2397 QLIST_REMOVE(mon_fdset_fd, next);
2398 g_free(mon_fdset_fd);
2399 }
2400 }
2401
Corey Bryantadb696f2012-08-14 16:43:47 -04002402 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
Corey Bryantba1c0482012-08-14 16:43:43 -04002403 QLIST_REMOVE(mon_fdset, next);
2404 g_free(mon_fdset);
2405 }
2406}
2407
2408AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2409 const char *opaque, Error **errp)
2410{
2411 int fd;
2412 Monitor *mon = cur_mon;
2413 MonFdset *mon_fdset;
2414 MonFdsetFd *mon_fdset_fd;
2415 AddfdInfo *fdinfo;
2416
2417 fd = qemu_chr_fe_get_msgfd(mon->chr);
2418 if (fd == -1) {
2419 error_set(errp, QERR_FD_NOT_SUPPLIED);
2420 goto error;
2421 }
2422
2423 if (has_fdset_id) {
2424 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2425 if (mon_fdset->id == fdset_id) {
2426 break;
2427 }
2428 }
2429 if (mon_fdset == NULL) {
2430 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2431 "an existing fdset-id");
2432 goto error;
2433 }
2434 } else {
2435 int64_t fdset_id_prev = -1;
2436 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2437
2438 /* Use first available fdset ID */
2439 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2440 mon_fdset_cur = mon_fdset;
2441 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2442 fdset_id_prev = mon_fdset_cur->id;
2443 continue;
2444 }
2445 break;
2446 }
2447
2448 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2449 mon_fdset->id = fdset_id_prev + 1;
2450
2451 /* The fdset list is ordered by fdset ID */
2452 if (mon_fdset->id == 0) {
2453 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2454 } else if (mon_fdset->id < mon_fdset_cur->id) {
2455 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2456 } else {
2457 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2458 }
2459 }
2460
2461 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2462 mon_fdset_fd->fd = fd;
2463 mon_fdset_fd->removed = false;
2464 if (has_opaque) {
2465 mon_fdset_fd->opaque = g_strdup(opaque);
2466 }
2467 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2468
2469 fdinfo = g_malloc0(sizeof(*fdinfo));
2470 fdinfo->fdset_id = mon_fdset->id;
2471 fdinfo->fd = mon_fdset_fd->fd;
2472
2473 return fdinfo;
2474
2475error:
2476 if (fd != -1) {
2477 close(fd);
2478 }
2479 return NULL;
2480}
2481
2482void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2483{
2484 MonFdset *mon_fdset;
2485 MonFdsetFd *mon_fdset_fd;
2486 char fd_str[60];
2487
2488 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2489 if (mon_fdset->id != fdset_id) {
2490 continue;
2491 }
2492 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2493 if (has_fd) {
2494 if (mon_fdset_fd->fd != fd) {
2495 continue;
2496 }
2497 mon_fdset_fd->removed = true;
2498 break;
2499 } else {
2500 mon_fdset_fd->removed = true;
2501 }
2502 }
2503 if (has_fd && !mon_fdset_fd) {
2504 goto error;
2505 }
2506 monitor_fdset_cleanup(mon_fdset);
2507 return;
2508 }
2509
2510error:
2511 if (has_fd) {
2512 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2513 fdset_id, fd);
2514 } else {
2515 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2516 }
2517 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2518}
2519
2520FdsetInfoList *qmp_query_fdsets(Error **errp)
2521{
2522 MonFdset *mon_fdset;
2523 MonFdsetFd *mon_fdset_fd;
2524 FdsetInfoList *fdset_list = NULL;
2525
2526 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2527 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2528 FdsetFdInfoList *fdsetfd_list = NULL;
2529
2530 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2531 fdset_info->value->fdset_id = mon_fdset->id;
2532
2533 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2534 FdsetFdInfoList *fdsetfd_info;
2535
2536 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2537 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2538 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2539 if (mon_fdset_fd->opaque) {
2540 fdsetfd_info->value->has_opaque = true;
2541 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2542 } else {
2543 fdsetfd_info->value->has_opaque = false;
2544 }
2545
2546 fdsetfd_info->next = fdsetfd_list;
2547 fdsetfd_list = fdsetfd_info;
2548 }
2549
2550 fdset_info->value->fds = fdsetfd_list;
2551
2552 fdset_info->next = fdset_list;
2553 fdset_list = fdset_info;
2554 }
2555
2556 return fdset_list;
2557}
2558
Corey Bryantadb696f2012-08-14 16:43:47 -04002559int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2560{
2561 MonFdset *mon_fdset;
2562 MonFdsetFd *mon_fdset_fd;
2563 int mon_fd_flags;
2564
2565#ifndef _WIN32
2566 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2567 if (mon_fdset->id != fdset_id) {
2568 continue;
2569 }
2570 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2571 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2572 if (mon_fd_flags == -1) {
2573 return -1;
2574 }
2575
2576 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2577 return mon_fdset_fd->fd;
2578 }
2579 }
2580 errno = EACCES;
2581 return -1;
2582 }
2583#endif
2584
2585 errno = ENOENT;
2586 return -1;
2587}
2588
2589int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2590{
2591 MonFdset *mon_fdset;
2592 MonFdsetFd *mon_fdset_fd_dup;
2593
2594 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2595 if (mon_fdset->id != fdset_id) {
2596 continue;
2597 }
2598 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2599 if (mon_fdset_fd_dup->fd == dup_fd) {
2600 return -1;
2601 }
2602 }
2603 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2604 mon_fdset_fd_dup->fd = dup_fd;
2605 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2606 return 0;
2607 }
2608 return -1;
2609}
2610
2611static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2612{
2613 MonFdset *mon_fdset;
2614 MonFdsetFd *mon_fdset_fd_dup;
2615
2616 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2617 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2618 if (mon_fdset_fd_dup->fd == dup_fd) {
2619 if (remove) {
2620 QLIST_REMOVE(mon_fdset_fd_dup, next);
2621 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2622 monitor_fdset_cleanup(mon_fdset);
2623 }
2624 }
2625 return mon_fdset->id;
2626 }
2627 }
2628 }
2629 return -1;
2630}
2631
2632int monitor_fdset_dup_fd_find(int dup_fd)
2633{
2634 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2635}
2636
2637int monitor_fdset_dup_fd_remove(int dup_fd)
2638{
2639 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2640}
2641
Wayne Xia816f8922011-10-12 11:32:41 +08002642/* mon_cmds and info_cmds would be sorted at runtime */
2643static mon_cmd_t mon_cmds[] = {
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002644#include "hmp-commands.h"
ths5fafdf22007-09-16 21:08:06 +00002645 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00002646};
2647
Luiz Capitulinoacd0a092010-09-30 16:00:22 -03002648/* Please update hmp-commands.hx when adding or changing commands */
Wayne Xia816f8922011-10-12 11:32:41 +08002649static mon_cmd_t info_cmds[] = {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002650 {
2651 .name = "version",
2652 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002653 .params = "",
2654 .help = "show the version of QEMU",
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03002655 .mhandler.info = hmp_info_version,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002656 },
2657 {
2658 .name = "network",
2659 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002660 .params = "",
2661 .help = "show the network state",
Luiz Capitulino910df892009-10-07 13:41:51 -03002662 .mhandler.info = do_info_network,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002663 },
2664 {
2665 .name = "chardev",
2666 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002667 .params = "",
2668 .help = "show the character devices",
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -03002669 .mhandler.info = hmp_info_chardev,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002670 },
2671 {
2672 .name = "block",
2673 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002674 .params = "",
2675 .help = "show the block devices",
Luiz Capitulinob2023812011-09-21 17:16:47 -03002676 .mhandler.info = hmp_info_block,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002677 },
2678 {
2679 .name = "blockstats",
2680 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002681 .params = "",
2682 .help = "show block device statistics",
Luiz Capitulinof11f57e2011-09-22 15:56:36 -03002683 .mhandler.info = hmp_info_blockstats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002684 },
2685 {
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +00002686 .name = "block-jobs",
2687 .args_type = "",
2688 .params = "",
2689 .help = "show progress of ongoing block device operations",
2690 .mhandler.info = hmp_info_block_jobs,
2691 },
2692 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002693 .name = "registers",
2694 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002695 .params = "",
2696 .help = "show the cpu registers",
Luiz Capitulino910df892009-10-07 13:41:51 -03002697 .mhandler.info = do_info_registers,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002698 },
2699 {
2700 .name = "cpus",
2701 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002702 .params = "",
2703 .help = "show infos for each CPU",
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03002704 .mhandler.info = hmp_info_cpus,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002705 },
2706 {
2707 .name = "history",
2708 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002709 .params = "",
2710 .help = "show the command line history",
Luiz Capitulino910df892009-10-07 13:41:51 -03002711 .mhandler.info = do_info_history,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002712 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002713#if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2714 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002715 {
2716 .name = "irq",
2717 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002718 .params = "",
2719 .help = "show the interrupts statistics (if available)",
Jan Kiszka661f1922011-10-16 11:53:13 +02002720#ifdef TARGET_SPARC
2721 .mhandler.info = sun4m_irq_info,
2722#elif defined(TARGET_LM32)
2723 .mhandler.info = lm32_irq_info,
2724#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002725 .mhandler.info = irq_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002726#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002727 },
2728 {
2729 .name = "pic",
2730 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002731 .params = "",
2732 .help = "show i8259 (PIC) state",
Jan Kiszka661f1922011-10-16 11:53:13 +02002733#ifdef TARGET_SPARC
2734 .mhandler.info = sun4m_pic_info,
2735#elif defined(TARGET_LM32)
2736 .mhandler.info = lm32_do_pic_info,
2737#else
Luiz Capitulino910df892009-10-07 13:41:51 -03002738 .mhandler.info = pic_info,
Jan Kiszka661f1922011-10-16 11:53:13 +02002739#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002740 },
Jan Kiszka661f1922011-10-16 11:53:13 +02002741#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002742 {
2743 .name = "pci",
2744 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002745 .params = "",
2746 .help = "show PCI info",
Luiz Capitulino79627472011-10-21 14:15:33 -02002747 .mhandler.info = hmp_info_pci,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002748 },
Scott Woodbebabbc2011-08-18 10:38:42 +00002749#if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
Max Filippov692f7372012-01-07 20:02:40 +04002750 defined(TARGET_PPC) || defined(TARGET_XTENSA)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002751 {
2752 .name = "tlb",
2753 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002754 .params = "",
2755 .help = "show virtual to physical memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002756 .mhandler.info = tlb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002757 },
aurel327c664e22009-03-03 06:12:22 +00002758#endif
2759#if defined(TARGET_I386)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002760 {
2761 .name = "mem",
2762 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002763 .params = "",
2764 .help = "show the active virtual memory mappings",
Luiz Capitulino910df892009-10-07 13:41:51 -03002765 .mhandler.info = mem_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002766 },
bellardb86bda52004-09-18 19:32:46 +00002767#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002768 {
Blue Swirl314e2982011-09-11 20:22:05 +00002769 .name = "mtree",
2770 .args_type = "",
2771 .params = "",
2772 .help = "show memory tree",
2773 .mhandler.info = do_info_mtree,
2774 },
2775 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002776 .name = "jit",
2777 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002778 .params = "",
2779 .help = "show dynamic compiler info",
Luiz Capitulino910df892009-10-07 13:41:51 -03002780 .mhandler.info = do_info_jit,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002781 },
2782 {
2783 .name = "kvm",
2784 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002785 .params = "",
2786 .help = "show KVM information",
Luiz Capitulino292a2602011-09-12 15:10:53 -03002787 .mhandler.info = hmp_info_kvm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002788 },
2789 {
2790 .name = "numa",
2791 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002792 .params = "",
2793 .help = "show NUMA information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002794 .mhandler.info = do_info_numa,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002795 },
2796 {
2797 .name = "usb",
2798 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002799 .params = "",
2800 .help = "show guest USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002801 .mhandler.info = usb_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002802 },
2803 {
2804 .name = "usbhost",
2805 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002806 .params = "",
2807 .help = "show host USB devices",
Luiz Capitulino910df892009-10-07 13:41:51 -03002808 .mhandler.info = usb_host_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002809 },
2810 {
2811 .name = "profile",
2812 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002813 .params = "",
2814 .help = "show profiling information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002815 .mhandler.info = do_info_profile,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002816 },
2817 {
2818 .name = "capture",
2819 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002820 .params = "",
2821 .help = "show capture information",
Luiz Capitulino910df892009-10-07 13:41:51 -03002822 .mhandler.info = do_info_capture,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002823 },
2824 {
2825 .name = "snapshots",
2826 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002827 .params = "",
2828 .help = "show the currently saved VM snapshots",
Luiz Capitulino910df892009-10-07 13:41:51 -03002829 .mhandler.info = do_info_snapshots,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002830 },
2831 {
2832 .name = "status",
2833 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002834 .params = "",
2835 .help = "show the current VM status (running|paused)",
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -03002836 .mhandler.info = hmp_info_status,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002837 },
2838 {
2839 .name = "pcmcia",
2840 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002841 .params = "",
2842 .help = "show guest PCMCIA status",
Luiz Capitulino910df892009-10-07 13:41:51 -03002843 .mhandler.info = pcmcia_info,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002844 },
2845 {
2846 .name = "mice",
2847 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002848 .params = "",
2849 .help = "show which guest mouse is receiving events",
Luiz Capitulinoe235cec2011-09-21 15:29:55 -03002850 .mhandler.info = hmp_info_mice,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002851 },
2852 {
2853 .name = "vnc",
2854 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002855 .params = "",
2856 .help = "show the vnc server status",
Luiz Capitulino2b54aa82011-10-17 16:41:22 -02002857 .mhandler.info = hmp_info_vnc,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002858 },
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002859#if defined(CONFIG_SPICE)
2860 {
2861 .name = "spice",
2862 .args_type = "",
2863 .params = "",
2864 .help = "show the spice server status",
Luiz Capitulinod1f29642011-10-20 17:01:33 -02002865 .mhandler.info = hmp_info_spice,
Gerd Hoffmanncb42a872010-11-30 11:02:51 +01002866 },
2867#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002868 {
2869 .name = "name",
2870 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002871 .params = "",
2872 .help = "show the current VM name",
Anthony Liguori48a32be2011-09-02 12:34:48 -05002873 .mhandler.info = hmp_info_name,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002874 },
2875 {
2876 .name = "uuid",
2877 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002878 .params = "",
2879 .help = "show the current VM UUID",
Luiz Capitulinoefab7672011-09-13 17:16:25 -03002880 .mhandler.info = hmp_info_uuid,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002881 },
j_mayer76a66252007-03-07 08:32:30 +00002882#if defined(TARGET_PPC)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002883 {
2884 .name = "cpustats",
2885 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002886 .params = "",
2887 .help = "show CPU statistics",
Luiz Capitulino910df892009-10-07 13:41:51 -03002888 .mhandler.info = do_info_cpu_stats,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002889 },
j_mayer76a66252007-03-07 08:32:30 +00002890#endif
blueswir131a60e22007-10-26 18:42:59 +00002891#if defined(CONFIG_SLIRP)
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002892 {
2893 .name = "usernet",
2894 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002895 .params = "",
2896 .help = "show user network stack connection states",
Luiz Capitulino910df892009-10-07 13:41:51 -03002897 .mhandler.info = do_info_usernet,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002898 },
blueswir131a60e22007-10-26 18:42:59 +00002899#endif
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002900 {
2901 .name = "migrate",
2902 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002903 .params = "",
2904 .help = "show migration status",
Luiz Capitulino791e7c82011-09-13 17:37:16 -03002905 .mhandler.info = hmp_info_migrate,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002906 },
2907 {
Orit Wassermanbbf6da32012-08-06 21:42:47 +03002908 .name = "migrate_capabilities",
2909 .args_type = "",
2910 .params = "",
2911 .help = "show current migration capabilities",
2912 .mhandler.info = hmp_info_migrate_capabilities,
2913 },
2914 {
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03002915 .name = "migrate_cache_size",
2916 .args_type = "",
2917 .params = "",
2918 .help = "show current migration xbzrle cache size",
2919 .mhandler.info = hmp_info_migrate_cache_size,
2920 },
2921 {
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002922 .name = "balloon",
2923 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002924 .params = "",
2925 .help = "show balloon information",
Luiz Capitulino96637bc2011-10-21 11:41:37 -02002926 .mhandler.info = hmp_info_balloon,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002927 },
2928 {
2929 .name = "qtree",
2930 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002931 .params = "",
2932 .help = "show device tree",
Luiz Capitulino910df892009-10-07 13:41:51 -03002933 .mhandler.info = do_info_qtree,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002934 },
2935 {
2936 .name = "qdm",
2937 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002938 .params = "",
2939 .help = "show qdev device model list",
Luiz Capitulino910df892009-10-07 13:41:51 -03002940 .mhandler.info = do_info_qdm,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002941 },
2942 {
2943 .name = "roms",
2944 .args_type = "",
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002945 .params = "",
2946 .help = "show roms",
Luiz Capitulino910df892009-10-07 13:41:51 -03002947 .mhandler.info = do_info_roms,
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002948 },
Prerna Saxena22890ab2010-06-24 17:04:53 +05302949 {
2950 .name = "trace-events",
2951 .args_type = "",
2952 .params = "",
2953 .help = "show available trace-events & their state",
Lluísfc764102011-08-31 20:31:18 +02002954 .mhandler.info = do_trace_print_events,
Prerna Saxena22890ab2010-06-24 17:04:53 +05302955 },
Luiz Capitulinod7f9b682009-10-07 13:41:50 -03002956 {
2957 .name = NULL,
2958 },
bellard9dc39cb2004-03-14 21:38:27 +00002959};
2960
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002961static const mon_cmd_t qmp_cmds[] = {
Anthony Liguorie3193602011-09-02 12:34:47 -05002962#include "qmp-commands-old.h"
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03002963 { /* NULL */ },
2964};
2965
bellard9307c4c2004-04-04 12:57:25 +00002966/*******************************************************************/
2967
2968static const char *pch;
2969static jmp_buf expr_env;
2970
bellard92a31b12005-02-10 22:00:52 +00002971#define MD_TLONG 0
2972#define MD_I32 1
2973
bellard9307c4c2004-04-04 12:57:25 +00002974typedef struct MonitorDef {
2975 const char *name;
2976 int offset;
blueswir18662d652008-10-02 18:32:44 +00002977 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00002978 int type;
bellard9307c4c2004-04-04 12:57:25 +00002979} MonitorDef;
2980
bellard57206fd2004-04-25 18:54:52 +00002981#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00002982static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00002983{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002984 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00002985 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00002986}
2987#endif
2988
bellarda541f292004-04-12 20:39:29 +00002989#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00002990static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00002991{
Andreas Färber9349b4f2012-03-14 01:38:32 +01002992 CPUArchState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00002993 unsigned int u;
2994 int i;
2995
2996 u = 0;
2997 for (i = 0; i < 8; i++)
aliguori28a76be2009-03-06 20:27:40 +00002998 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00002999
3000 return u;
3001}
3002
blueswir18662d652008-10-02 18:32:44 +00003003static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003004{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003005 CPUArchState *env = mon_get_cpu();
j_mayer0411a972007-10-25 21:35:50 +00003006 return env->msr;
bellarda541f292004-04-12 20:39:29 +00003007}
3008
blueswir18662d652008-10-02 18:32:44 +00003009static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00003010{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003011 CPUArchState *env = mon_get_cpu();
aurel323d7b4172008-10-21 11:28:46 +00003012 return env->xer;
bellarda541f292004-04-12 20:39:29 +00003013}
bellard9fddaa02004-05-21 12:59:32 +00003014
blueswir18662d652008-10-02 18:32:44 +00003015static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003016{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003017 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003018 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00003019}
3020
blueswir18662d652008-10-02 18:32:44 +00003021static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003022{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003023 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003024 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00003025}
3026
blueswir18662d652008-10-02 18:32:44 +00003027static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00003028{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003029 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003030 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00003031}
bellarda541f292004-04-12 20:39:29 +00003032#endif
3033
bellarde95c8d52004-09-30 22:22:08 +00003034#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00003035#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00003036static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003037{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003038 CPUArchState *env = mon_get_cpu();
Blue Swirl5a834bb2010-05-09 20:19:04 +00003039
3040 return cpu_get_psr(env);
bellarde95c8d52004-09-30 22:22:08 +00003041}
bellard7b936c02005-10-30 17:05:13 +00003042#endif
bellarde95c8d52004-09-30 22:22:08 +00003043
blueswir18662d652008-10-02 18:32:44 +00003044static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00003045{
Andreas Färber9349b4f2012-03-14 01:38:32 +01003046 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003047 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00003048}
3049#endif
3050
blueswir18662d652008-10-02 18:32:44 +00003051static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00003052#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00003053
3054#define SEG(name, seg) \
Andreas Färbere59d1672012-02-16 00:40:47 +01003055 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3056 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3057 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00003058
Andreas Färbere59d1672012-02-16 00:40:47 +01003059 { "eax", offsetof(CPUX86State, regs[0]) },
3060 { "ecx", offsetof(CPUX86State, regs[1]) },
3061 { "edx", offsetof(CPUX86State, regs[2]) },
3062 { "ebx", offsetof(CPUX86State, regs[3]) },
3063 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3064 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3065 { "esi", offsetof(CPUX86State, regs[6]) },
3066 { "edi", offsetof(CPUX86State, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00003067#ifdef TARGET_X86_64
Andreas Färbere59d1672012-02-16 00:40:47 +01003068 { "r8", offsetof(CPUX86State, regs[8]) },
3069 { "r9", offsetof(CPUX86State, regs[9]) },
3070 { "r10", offsetof(CPUX86State, regs[10]) },
3071 { "r11", offsetof(CPUX86State, regs[11]) },
3072 { "r12", offsetof(CPUX86State, regs[12]) },
3073 { "r13", offsetof(CPUX86State, regs[13]) },
3074 { "r14", offsetof(CPUX86State, regs[14]) },
3075 { "r15", offsetof(CPUX86State, regs[15]) },
bellard92a31b12005-02-10 22:00:52 +00003076#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003077 { "eflags", offsetof(CPUX86State, eflags) },
3078 { "eip", offsetof(CPUX86State, eip) },
bellard57206fd2004-04-25 18:54:52 +00003079 SEG("cs", R_CS)
3080 SEG("ds", R_DS)
3081 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00003082 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00003083 SEG("fs", R_FS)
3084 SEG("gs", R_GS)
3085 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00003086#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00003087 /* General purpose registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003088 { "r0", offsetof(CPUPPCState, gpr[0]) },
3089 { "r1", offsetof(CPUPPCState, gpr[1]) },
3090 { "r2", offsetof(CPUPPCState, gpr[2]) },
3091 { "r3", offsetof(CPUPPCState, gpr[3]) },
3092 { "r4", offsetof(CPUPPCState, gpr[4]) },
3093 { "r5", offsetof(CPUPPCState, gpr[5]) },
3094 { "r6", offsetof(CPUPPCState, gpr[6]) },
3095 { "r7", offsetof(CPUPPCState, gpr[7]) },
3096 { "r8", offsetof(CPUPPCState, gpr[8]) },
3097 { "r9", offsetof(CPUPPCState, gpr[9]) },
3098 { "r10", offsetof(CPUPPCState, gpr[10]) },
3099 { "r11", offsetof(CPUPPCState, gpr[11]) },
3100 { "r12", offsetof(CPUPPCState, gpr[12]) },
3101 { "r13", offsetof(CPUPPCState, gpr[13]) },
3102 { "r14", offsetof(CPUPPCState, gpr[14]) },
3103 { "r15", offsetof(CPUPPCState, gpr[15]) },
3104 { "r16", offsetof(CPUPPCState, gpr[16]) },
3105 { "r17", offsetof(CPUPPCState, gpr[17]) },
3106 { "r18", offsetof(CPUPPCState, gpr[18]) },
3107 { "r19", offsetof(CPUPPCState, gpr[19]) },
3108 { "r20", offsetof(CPUPPCState, gpr[20]) },
3109 { "r21", offsetof(CPUPPCState, gpr[21]) },
3110 { "r22", offsetof(CPUPPCState, gpr[22]) },
3111 { "r23", offsetof(CPUPPCState, gpr[23]) },
3112 { "r24", offsetof(CPUPPCState, gpr[24]) },
3113 { "r25", offsetof(CPUPPCState, gpr[25]) },
3114 { "r26", offsetof(CPUPPCState, gpr[26]) },
3115 { "r27", offsetof(CPUPPCState, gpr[27]) },
3116 { "r28", offsetof(CPUPPCState, gpr[28]) },
3117 { "r29", offsetof(CPUPPCState, gpr[29]) },
3118 { "r30", offsetof(CPUPPCState, gpr[30]) },
3119 { "r31", offsetof(CPUPPCState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00003120 /* Floating point registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003121 { "f0", offsetof(CPUPPCState, fpr[0]) },
3122 { "f1", offsetof(CPUPPCState, fpr[1]) },
3123 { "f2", offsetof(CPUPPCState, fpr[2]) },
3124 { "f3", offsetof(CPUPPCState, fpr[3]) },
3125 { "f4", offsetof(CPUPPCState, fpr[4]) },
3126 { "f5", offsetof(CPUPPCState, fpr[5]) },
3127 { "f6", offsetof(CPUPPCState, fpr[6]) },
3128 { "f7", offsetof(CPUPPCState, fpr[7]) },
3129 { "f8", offsetof(CPUPPCState, fpr[8]) },
3130 { "f9", offsetof(CPUPPCState, fpr[9]) },
3131 { "f10", offsetof(CPUPPCState, fpr[10]) },
3132 { "f11", offsetof(CPUPPCState, fpr[11]) },
3133 { "f12", offsetof(CPUPPCState, fpr[12]) },
3134 { "f13", offsetof(CPUPPCState, fpr[13]) },
3135 { "f14", offsetof(CPUPPCState, fpr[14]) },
3136 { "f15", offsetof(CPUPPCState, fpr[15]) },
3137 { "f16", offsetof(CPUPPCState, fpr[16]) },
3138 { "f17", offsetof(CPUPPCState, fpr[17]) },
3139 { "f18", offsetof(CPUPPCState, fpr[18]) },
3140 { "f19", offsetof(CPUPPCState, fpr[19]) },
3141 { "f20", offsetof(CPUPPCState, fpr[20]) },
3142 { "f21", offsetof(CPUPPCState, fpr[21]) },
3143 { "f22", offsetof(CPUPPCState, fpr[22]) },
3144 { "f23", offsetof(CPUPPCState, fpr[23]) },
3145 { "f24", offsetof(CPUPPCState, fpr[24]) },
3146 { "f25", offsetof(CPUPPCState, fpr[25]) },
3147 { "f26", offsetof(CPUPPCState, fpr[26]) },
3148 { "f27", offsetof(CPUPPCState, fpr[27]) },
3149 { "f28", offsetof(CPUPPCState, fpr[28]) },
3150 { "f29", offsetof(CPUPPCState, fpr[29]) },
3151 { "f30", offsetof(CPUPPCState, fpr[30]) },
3152 { "f31", offsetof(CPUPPCState, fpr[31]) },
3153 { "fpscr", offsetof(CPUPPCState, fpscr) },
j_mayerff937db2007-09-19 05:49:13 +00003154 /* Next instruction pointer */
Andreas Färbere59d1672012-02-16 00:40:47 +01003155 { "nip|pc", offsetof(CPUPPCState, nip) },
3156 { "lr", offsetof(CPUPPCState, lr) },
3157 { "ctr", offsetof(CPUPPCState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00003158 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00003159 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00003160 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00003161 { "msr", 0, &monitor_get_msr, },
3162 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00003163 { "tbu", 0, &monitor_get_tbu, },
3164 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00003165#if defined(TARGET_PPC64)
3166 /* Address space register */
Andreas Färbere59d1672012-02-16 00:40:47 +01003167 { "asr", offsetof(CPUPPCState, asr) },
j_mayerff937db2007-09-19 05:49:13 +00003168#endif
3169 /* Segment registers */
Andreas Färbere59d1672012-02-16 00:40:47 +01003170 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3171 { "sr0", offsetof(CPUPPCState, sr[0]) },
3172 { "sr1", offsetof(CPUPPCState, sr[1]) },
3173 { "sr2", offsetof(CPUPPCState, sr[2]) },
3174 { "sr3", offsetof(CPUPPCState, sr[3]) },
3175 { "sr4", offsetof(CPUPPCState, sr[4]) },
3176 { "sr5", offsetof(CPUPPCState, sr[5]) },
3177 { "sr6", offsetof(CPUPPCState, sr[6]) },
3178 { "sr7", offsetof(CPUPPCState, sr[7]) },
3179 { "sr8", offsetof(CPUPPCState, sr[8]) },
3180 { "sr9", offsetof(CPUPPCState, sr[9]) },
3181 { "sr10", offsetof(CPUPPCState, sr[10]) },
3182 { "sr11", offsetof(CPUPPCState, sr[11]) },
3183 { "sr12", offsetof(CPUPPCState, sr[12]) },
3184 { "sr13", offsetof(CPUPPCState, sr[13]) },
3185 { "sr14", offsetof(CPUPPCState, sr[14]) },
3186 { "sr15", offsetof(CPUPPCState, sr[15]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003187 /* Too lazy to put BATs... */
Andreas Färbere59d1672012-02-16 00:40:47 +01003188 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003189
Andreas Färbere59d1672012-02-16 00:40:47 +01003190 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3191 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3192 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3193 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3194 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3195 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3196 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3197 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3198 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3199 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3200 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3201 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3202 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3203 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3204 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3205 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3206 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3207 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3208 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3209 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3210 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3211 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3212 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3213 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3214 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3215 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3216 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3217 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3218 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3219 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3220 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3221 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3222 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3223 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3224 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3225 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3226 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3227 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3228 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3229 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3230 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3231 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3232 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3233 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3234 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3235 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3236 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3237 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3238 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3239 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3240 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3241 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3242 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3243 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3244 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3245 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3246 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3247 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3248 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3249 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3250 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3251 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3252 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3253 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3254 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3255 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
Scott Wood90dc8812011-04-29 17:10:23 -05003256
bellarde95c8d52004-09-30 22:22:08 +00003257#elif defined(TARGET_SPARC)
Andreas Färbere59d1672012-02-16 00:40:47 +01003258 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3259 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3260 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3261 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3262 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3263 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3264 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3265 { "g7", offsetof(CPUSPARCState, gregs[7]) },
bellarde95c8d52004-09-30 22:22:08 +00003266 { "o0", 0, monitor_get_reg },
3267 { "o1", 1, monitor_get_reg },
3268 { "o2", 2, monitor_get_reg },
3269 { "o3", 3, monitor_get_reg },
3270 { "o4", 4, monitor_get_reg },
3271 { "o5", 5, monitor_get_reg },
3272 { "o6", 6, monitor_get_reg },
3273 { "o7", 7, monitor_get_reg },
3274 { "l0", 8, monitor_get_reg },
3275 { "l1", 9, monitor_get_reg },
3276 { "l2", 10, monitor_get_reg },
3277 { "l3", 11, monitor_get_reg },
3278 { "l4", 12, monitor_get_reg },
3279 { "l5", 13, monitor_get_reg },
3280 { "l6", 14, monitor_get_reg },
3281 { "l7", 15, monitor_get_reg },
3282 { "i0", 16, monitor_get_reg },
3283 { "i1", 17, monitor_get_reg },
3284 { "i2", 18, monitor_get_reg },
3285 { "i3", 19, monitor_get_reg },
3286 { "i4", 20, monitor_get_reg },
3287 { "i5", 21, monitor_get_reg },
3288 { "i6", 22, monitor_get_reg },
3289 { "i7", 23, monitor_get_reg },
Andreas Färbere59d1672012-02-16 00:40:47 +01003290 { "pc", offsetof(CPUSPARCState, pc) },
3291 { "npc", offsetof(CPUSPARCState, npc) },
3292 { "y", offsetof(CPUSPARCState, y) },
bellard7b936c02005-10-30 17:05:13 +00003293#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00003294 { "psr", 0, &monitor_get_psr, },
Andreas Färbere59d1672012-02-16 00:40:47 +01003295 { "wim", offsetof(CPUSPARCState, wim) },
bellard7b936c02005-10-30 17:05:13 +00003296#endif
Andreas Färbere59d1672012-02-16 00:40:47 +01003297 { "tbr", offsetof(CPUSPARCState, tbr) },
3298 { "fsr", offsetof(CPUSPARCState, fsr) },
3299 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3300 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3301 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3302 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3303 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3304 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3305 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3306 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3307 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3308 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3309 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3310 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3311 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3312 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3313 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3314 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3315 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3316 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3317 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3318 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3319 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3320 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3321 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3322 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3323 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3324 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3325 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3326 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3327 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3328 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3329 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3330 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
bellard7b936c02005-10-30 17:05:13 +00003331#ifdef TARGET_SPARC64
Andreas Färbere59d1672012-02-16 00:40:47 +01003332 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3333 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3334 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3335 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3336 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3337 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3338 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3339 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3340 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3341 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3342 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3343 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3344 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3345 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3346 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3347 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3348 { "asi", offsetof(CPUSPARCState, asi) },
3349 { "pstate", offsetof(CPUSPARCState, pstate) },
3350 { "cansave", offsetof(CPUSPARCState, cansave) },
3351 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3352 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3353 { "wstate", offsetof(CPUSPARCState, wstate) },
3354 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3355 { "fprs", offsetof(CPUSPARCState, fprs) },
bellard7b936c02005-10-30 17:05:13 +00003356#endif
bellard9307c4c2004-04-04 12:57:25 +00003357#endif
3358 { NULL },
3359};
3360
aliguori376253e2009-03-05 23:01:23 +00003361static void expr_error(Monitor *mon, const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00003362{
aliguori376253e2009-03-05 23:01:23 +00003363 monitor_printf(mon, "%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00003364 longjmp(expr_env, 1);
3365}
3366
Markus Armbruster09b94182010-01-20 13:07:30 +01003367/* return 0 if OK, -1 if not found */
bellard92a31b12005-02-10 22:00:52 +00003368static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00003369{
blueswir18662d652008-10-02 18:32:44 +00003370 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00003371 void *ptr;
3372
bellard9307c4c2004-04-04 12:57:25 +00003373 for(md = monitor_defs; md->name != NULL; md++) {
3374 if (compare_cmd(name, md->name)) {
3375 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00003376 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00003377 } else {
Andreas Färber9349b4f2012-03-14 01:38:32 +01003378 CPUArchState *env = mon_get_cpu();
bellard6a00d602005-11-21 23:25:50 +00003379 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00003380 switch(md->type) {
3381 case MD_I32:
3382 *pval = *(int32_t *)ptr;
3383 break;
3384 case MD_TLONG:
3385 *pval = *(target_long *)ptr;
3386 break;
3387 default:
3388 *pval = 0;
3389 break;
3390 }
bellard9307c4c2004-04-04 12:57:25 +00003391 }
3392 return 0;
3393 }
3394 }
3395 return -1;
3396}
3397
3398static void next(void)
3399{
Blue Swirl660f11b2009-07-31 21:16:51 +00003400 if (*pch != '\0') {
bellard9307c4c2004-04-04 12:57:25 +00003401 pch++;
blueswir1cd390082008-11-16 13:53:32 +00003402 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003403 pch++;
3404 }
3405}
3406
aliguori376253e2009-03-05 23:01:23 +00003407static int64_t expr_sum(Monitor *mon);
bellard9307c4c2004-04-04 12:57:25 +00003408
aliguori376253e2009-03-05 23:01:23 +00003409static int64_t expr_unary(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003410{
blueswir1c2efc952007-09-25 17:28:42 +00003411 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00003412 char *p;
bellard6a00d602005-11-21 23:25:50 +00003413 int ret;
bellard9307c4c2004-04-04 12:57:25 +00003414
3415 switch(*pch) {
3416 case '+':
3417 next();
aliguori376253e2009-03-05 23:01:23 +00003418 n = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003419 break;
3420 case '-':
3421 next();
aliguori376253e2009-03-05 23:01:23 +00003422 n = -expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003423 break;
3424 case '~':
3425 next();
aliguori376253e2009-03-05 23:01:23 +00003426 n = ~expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003427 break;
3428 case '(':
3429 next();
aliguori376253e2009-03-05 23:01:23 +00003430 n = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003431 if (*pch != ')') {
aliguori376253e2009-03-05 23:01:23 +00003432 expr_error(mon, "')' expected");
bellard9307c4c2004-04-04 12:57:25 +00003433 }
3434 next();
3435 break;
bellard81d09122004-07-14 17:21:37 +00003436 case '\'':
3437 pch++;
3438 if (*pch == '\0')
aliguori376253e2009-03-05 23:01:23 +00003439 expr_error(mon, "character constant expected");
bellard81d09122004-07-14 17:21:37 +00003440 n = *pch;
3441 pch++;
3442 if (*pch != '\'')
aliguori376253e2009-03-05 23:01:23 +00003443 expr_error(mon, "missing terminating \' character");
bellard81d09122004-07-14 17:21:37 +00003444 next();
3445 break;
bellard9307c4c2004-04-04 12:57:25 +00003446 case '$':
3447 {
3448 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00003449 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00003450
bellard9307c4c2004-04-04 12:57:25 +00003451 pch++;
3452 q = buf;
3453 while ((*pch >= 'a' && *pch <= 'z') ||
3454 (*pch >= 'A' && *pch <= 'Z') ||
3455 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00003456 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00003457 if ((q - buf) < sizeof(buf) - 1)
3458 *q++ = *pch;
3459 pch++;
3460 }
blueswir1cd390082008-11-16 13:53:32 +00003461 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003462 pch++;
3463 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00003464 ret = get_monitor_def(&reg, buf);
Markus Armbruster09b94182010-01-20 13:07:30 +01003465 if (ret < 0)
aliguori376253e2009-03-05 23:01:23 +00003466 expr_error(mon, "unknown register");
blueswir17743e582007-09-24 18:39:04 +00003467 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00003468 }
3469 break;
3470 case '\0':
aliguori376253e2009-03-05 23:01:23 +00003471 expr_error(mon, "unexpected end of expression");
bellard9307c4c2004-04-04 12:57:25 +00003472 n = 0;
3473 break;
3474 default:
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003475 errno = 0;
blueswir17743e582007-09-24 18:39:04 +00003476#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00003477 n = strtoull(pch, &p, 0);
3478#else
bellard9307c4c2004-04-04 12:57:25 +00003479 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00003480#endif
Luiz Capitulino6b0e33b2012-04-26 16:48:41 -03003481 if (errno == ERANGE) {
3482 expr_error(mon, "number too large");
3483 }
bellard9307c4c2004-04-04 12:57:25 +00003484 if (pch == p) {
aliguori376253e2009-03-05 23:01:23 +00003485 expr_error(mon, "invalid char in expression");
bellard9307c4c2004-04-04 12:57:25 +00003486 }
3487 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00003488 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003489 pch++;
3490 break;
3491 }
3492 return n;
3493}
3494
3495
aliguori376253e2009-03-05 23:01:23 +00003496static int64_t expr_prod(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003497{
blueswir1c2efc952007-09-25 17:28:42 +00003498 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003499 int op;
ths3b46e622007-09-17 08:09:54 +00003500
aliguori376253e2009-03-05 23:01:23 +00003501 val = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003502 for(;;) {
3503 op = *pch;
3504 if (op != '*' && op != '/' && op != '%')
3505 break;
3506 next();
aliguori376253e2009-03-05 23:01:23 +00003507 val2 = expr_unary(mon);
bellard9307c4c2004-04-04 12:57:25 +00003508 switch(op) {
3509 default:
3510 case '*':
3511 val *= val2;
3512 break;
3513 case '/':
3514 case '%':
ths5fafdf22007-09-16 21:08:06 +00003515 if (val2 == 0)
aliguori376253e2009-03-05 23:01:23 +00003516 expr_error(mon, "division by zero");
bellard9307c4c2004-04-04 12:57:25 +00003517 if (op == '/')
3518 val /= val2;
3519 else
3520 val %= val2;
3521 break;
3522 }
3523 }
3524 return val;
3525}
3526
aliguori376253e2009-03-05 23:01:23 +00003527static int64_t expr_logic(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003528{
blueswir1c2efc952007-09-25 17:28:42 +00003529 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003530 int op;
bellard9307c4c2004-04-04 12:57:25 +00003531
aliguori376253e2009-03-05 23:01:23 +00003532 val = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003533 for(;;) {
3534 op = *pch;
3535 if (op != '&' && op != '|' && op != '^')
3536 break;
3537 next();
aliguori376253e2009-03-05 23:01:23 +00003538 val2 = expr_prod(mon);
bellard9307c4c2004-04-04 12:57:25 +00003539 switch(op) {
3540 default:
3541 case '&':
3542 val &= val2;
3543 break;
3544 case '|':
3545 val |= val2;
3546 break;
3547 case '^':
3548 val ^= val2;
3549 break;
3550 }
3551 }
3552 return val;
3553}
3554
aliguori376253e2009-03-05 23:01:23 +00003555static int64_t expr_sum(Monitor *mon)
bellard9307c4c2004-04-04 12:57:25 +00003556{
blueswir1c2efc952007-09-25 17:28:42 +00003557 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00003558 int op;
bellard9307c4c2004-04-04 12:57:25 +00003559
aliguori376253e2009-03-05 23:01:23 +00003560 val = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003561 for(;;) {
3562 op = *pch;
3563 if (op != '+' && op != '-')
3564 break;
3565 next();
aliguori376253e2009-03-05 23:01:23 +00003566 val2 = expr_logic(mon);
bellard9307c4c2004-04-04 12:57:25 +00003567 if (op == '+')
3568 val += val2;
3569 else
3570 val -= val2;
3571 }
3572 return val;
3573}
3574
aliguori376253e2009-03-05 23:01:23 +00003575static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00003576{
3577 pch = *pp;
3578 if (setjmp(expr_env)) {
3579 *pp = pch;
3580 return -1;
3581 }
blueswir1cd390082008-11-16 13:53:32 +00003582 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00003583 pch++;
aliguori376253e2009-03-05 23:01:23 +00003584 *pval = expr_sum(mon);
bellard9307c4c2004-04-04 12:57:25 +00003585 *pp = pch;
3586 return 0;
3587}
3588
Markus Armbruster3350a4d2010-01-25 14:23:03 +01003589static int get_double(Monitor *mon, double *pval, const char **pp)
3590{
3591 const char *p = *pp;
3592 char *tailp;
3593 double d;
3594
3595 d = strtod(p, &tailp);
3596 if (tailp == p) {
3597 monitor_printf(mon, "Number expected\n");
3598 return -1;
3599 }
3600 if (d != d || d - d != 0) {
3601 /* NaN or infinity */
3602 monitor_printf(mon, "Bad number\n");
3603 return -1;
3604 }
3605 *pval = d;
3606 *pp = tailp;
3607 return 0;
3608}
3609
bellard9307c4c2004-04-04 12:57:25 +00003610static int get_str(char *buf, int buf_size, const char **pp)
3611{
3612 const char *p;
3613 char *q;
3614 int c;
3615
bellard81d09122004-07-14 17:21:37 +00003616 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00003617 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00003618 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003619 p++;
3620 if (*p == '\0') {
3621 fail:
bellard81d09122004-07-14 17:21:37 +00003622 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003623 *pp = p;
3624 return -1;
3625 }
bellard9307c4c2004-04-04 12:57:25 +00003626 if (*p == '\"') {
3627 p++;
3628 while (*p != '\0' && *p != '\"') {
3629 if (*p == '\\') {
3630 p++;
3631 c = *p++;
3632 switch(c) {
3633 case 'n':
3634 c = '\n';
3635 break;
3636 case 'r':
3637 c = '\r';
3638 break;
3639 case '\\':
3640 case '\'':
3641 case '\"':
3642 break;
3643 default:
3644 qemu_printf("unsupported escape code: '\\%c'\n", c);
3645 goto fail;
3646 }
3647 if ((q - buf) < buf_size - 1) {
3648 *q++ = c;
3649 }
3650 } else {
3651 if ((q - buf) < buf_size - 1) {
3652 *q++ = *p;
3653 }
3654 p++;
3655 }
3656 }
3657 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00003658 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00003659 goto fail;
3660 }
3661 p++;
3662 } else {
blueswir1cd390082008-11-16 13:53:32 +00003663 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003664 if ((q - buf) < buf_size - 1) {
3665 *q++ = *p;
3666 }
3667 p++;
3668 }
bellard9307c4c2004-04-04 12:57:25 +00003669 }
bellard81d09122004-07-14 17:21:37 +00003670 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00003671 *pp = p;
3672 return 0;
3673}
3674
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003675/*
3676 * Store the command-name in cmdname, and return a pointer to
3677 * the remaining of the command string.
3678 */
3679static const char *get_command_name(const char *cmdline,
3680 char *cmdname, size_t nlen)
3681{
3682 size_t len;
3683 const char *p, *pstart;
3684
3685 p = cmdline;
3686 while (qemu_isspace(*p))
3687 p++;
3688 if (*p == '\0')
3689 return NULL;
3690 pstart = p;
3691 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3692 p++;
3693 len = p - pstart;
3694 if (len > nlen - 1)
3695 len = nlen - 1;
3696 memcpy(cmdname, pstart, len);
3697 cmdname[len] = '\0';
3698 return p;
3699}
3700
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003701/**
3702 * Read key of 'type' into 'key' and return the current
3703 * 'type' pointer.
3704 */
3705static char *key_get_info(const char *type, char **key)
3706{
3707 size_t len;
3708 char *p, *str;
3709
3710 if (*type == ',')
3711 type++;
3712
3713 p = strchr(type, ':');
3714 if (!p) {
3715 *key = NULL;
3716 return NULL;
3717 }
3718 len = p - type;
3719
Anthony Liguori7267c092011-08-20 22:09:37 -05003720 str = g_malloc(len + 1);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003721 memcpy(str, type, len);
3722 str[len] = '\0';
3723
3724 *key = str;
3725 return ++p;
3726}
3727
bellard9307c4c2004-04-04 12:57:25 +00003728static int default_fmt_format = 'x';
3729static int default_fmt_size = 4;
3730
3731#define MAX_ARGS 16
3732
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02003733static int is_valid_option(const char *c, const char *typestr)
3734{
3735 char option[3];
3736
3737 option[0] = '-';
3738 option[1] = *c;
3739 option[2] = '\0';
3740
3741 typestr = strstr(typestr, option);
3742 return (typestr != NULL);
3743}
3744
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003745static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3746 const char *cmdname)
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003747{
3748 const mon_cmd_t *cmd;
3749
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003750 for (cmd = disp_table; cmd->name != NULL; cmd++) {
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003751 if (compare_cmd(cmdname, cmd->name)) {
3752 return cmd;
3753 }
3754 }
3755
3756 return NULL;
3757}
3758
Luiz Capitulino945c5ac2010-09-13 13:17:58 -03003759static const mon_cmd_t *monitor_find_command(const char *cmdname)
3760{
3761 return search_dispatch_table(mon_cmds, cmdname);
3762}
3763
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003764static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3765{
Luiz Capitulinof36b4af2010-09-15 17:17:45 -03003766 return search_dispatch_table(qmp_cmds, cmdname);
Luiz Capitulinobead3ce2010-09-15 17:08:39 -03003767}
3768
Anthony Liguoric227f092009-10-01 16:12:16 -05003769static const mon_cmd_t *monitor_parse_command(Monitor *mon,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003770 const char *cmdline,
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003771 QDict *qdict)
bellard9307c4c2004-04-04 12:57:25 +00003772{
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003773 const char *p, *typestr;
Luiz Capitulino53773582009-08-28 15:27:25 -03003774 int c;
Anthony Liguoric227f092009-10-01 16:12:16 -05003775 const mon_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00003776 char cmdname[256];
3777 char buf[1024];
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003778 char *key;
bellard9dc39cb2004-03-14 21:38:27 +00003779
3780#ifdef DEBUG
aliguori376253e2009-03-05 23:01:23 +00003781 monitor_printf(mon, "command='%s'\n", cmdline);
bellard9dc39cb2004-03-14 21:38:27 +00003782#endif
ths3b46e622007-09-17 08:09:54 +00003783
bellard9307c4c2004-04-04 12:57:25 +00003784 /* extract the command name */
Luiz Capitulino4590fd82009-06-09 18:21:30 -03003785 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3786 if (!p)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003787 return NULL;
ths3b46e622007-09-17 08:09:54 +00003788
Luiz Capitulino7fd669a2009-11-26 22:58:54 -02003789 cmd = monitor_find_command(cmdname);
3790 if (!cmd) {
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003791 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03003792 return NULL;
Luiz Capitulinod91d9bf2009-06-09 18:21:54 -03003793 }
bellard9307c4c2004-04-04 12:57:25 +00003794
bellard9307c4c2004-04-04 12:57:25 +00003795 /* parse the parameters */
3796 typestr = cmd->args_type;
bellard9307c4c2004-04-04 12:57:25 +00003797 for(;;) {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003798 typestr = key_get_info(typestr, &key);
3799 if (!typestr)
bellard9307c4c2004-04-04 12:57:25 +00003800 break;
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03003801 c = *typestr;
bellard9307c4c2004-04-04 12:57:25 +00003802 typestr++;
3803 switch(c) {
3804 case 'F':
bellard81d09122004-07-14 17:21:37 +00003805 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00003806 case 's':
3807 {
3808 int ret;
ths3b46e622007-09-17 08:09:54 +00003809
blueswir1cd390082008-11-16 13:53:32 +00003810 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003811 p++;
3812 if (*typestr == '?') {
3813 typestr++;
3814 if (*p == '\0') {
3815 /* no optional string: NULL argument */
Luiz Capitulino53773582009-08-28 15:27:25 -03003816 break;
bellard9307c4c2004-04-04 12:57:25 +00003817 }
3818 }
3819 ret = get_str(buf, sizeof(buf), &p);
3820 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00003821 switch(c) {
3822 case 'F':
aliguori376253e2009-03-05 23:01:23 +00003823 monitor_printf(mon, "%s: filename expected\n",
3824 cmdname);
bellard81d09122004-07-14 17:21:37 +00003825 break;
3826 case 'B':
aliguori376253e2009-03-05 23:01:23 +00003827 monitor_printf(mon, "%s: block device name expected\n",
3828 cmdname);
bellard81d09122004-07-14 17:21:37 +00003829 break;
3830 default:
aliguori376253e2009-03-05 23:01:23 +00003831 monitor_printf(mon, "%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00003832 break;
3833 }
bellard9307c4c2004-04-04 12:57:25 +00003834 goto fail;
3835 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003836 qdict_put(qdict, key, qstring_from_str(buf));
bellard9307c4c2004-04-04 12:57:25 +00003837 }
3838 break;
Markus Armbruster361127d2010-02-10 20:24:35 +01003839 case 'O':
3840 {
3841 QemuOptsList *opts_list;
3842 QemuOpts *opts;
3843
3844 opts_list = qemu_find_opts(key);
3845 if (!opts_list || opts_list->desc->name) {
3846 goto bad_type;
3847 }
3848 while (qemu_isspace(*p)) {
3849 p++;
3850 }
3851 if (!*p)
3852 break;
3853 if (get_str(buf, sizeof(buf), &p) < 0) {
3854 goto fail;
3855 }
3856 opts = qemu_opts_parse(opts_list, buf, 1);
3857 if (!opts) {
3858 goto fail;
3859 }
3860 qemu_opts_to_qdict(opts, qdict);
3861 qemu_opts_del(opts);
3862 }
3863 break;
bellard9307c4c2004-04-04 12:57:25 +00003864 case '/':
3865 {
3866 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00003867
blueswir1cd390082008-11-16 13:53:32 +00003868 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003869 p++;
3870 if (*p == '/') {
3871 /* format found */
3872 p++;
3873 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00003874 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003875 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00003876 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00003877 count = count * 10 + (*p - '0');
3878 p++;
3879 }
3880 }
3881 size = -1;
3882 format = -1;
3883 for(;;) {
3884 switch(*p) {
3885 case 'o':
3886 case 'd':
3887 case 'u':
3888 case 'x':
3889 case 'i':
3890 case 'c':
3891 format = *p++;
3892 break;
3893 case 'b':
3894 size = 1;
3895 p++;
3896 break;
3897 case 'h':
3898 size = 2;
3899 p++;
3900 break;
3901 case 'w':
3902 size = 4;
3903 p++;
3904 break;
3905 case 'g':
3906 case 'L':
3907 size = 8;
3908 p++;
3909 break;
3910 default:
3911 goto next;
3912 }
3913 }
3914 next:
blueswir1cd390082008-11-16 13:53:32 +00003915 if (*p != '\0' && !qemu_isspace(*p)) {
aliguori376253e2009-03-05 23:01:23 +00003916 monitor_printf(mon, "invalid char in format: '%c'\n",
3917 *p);
bellard9307c4c2004-04-04 12:57:25 +00003918 goto fail;
3919 }
bellard9307c4c2004-04-04 12:57:25 +00003920 if (format < 0)
3921 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003922 if (format != 'i') {
3923 /* for 'i', not specifying a size gives -1 as size */
3924 if (size < 0)
3925 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00003926 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00003927 }
bellard9307c4c2004-04-04 12:57:25 +00003928 default_fmt_format = format;
3929 } else {
3930 count = 1;
3931 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00003932 if (format != 'i') {
3933 size = default_fmt_size;
3934 } else {
3935 size = -1;
3936 }
bellard9307c4c2004-04-04 12:57:25 +00003937 }
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03003938 qdict_put(qdict, "count", qint_from_int(count));
3939 qdict_put(qdict, "format", qint_from_int(format));
3940 qdict_put(qdict, "size", qint_from_int(size));
bellard9307c4c2004-04-04 12:57:25 +00003941 }
3942 break;
3943 case 'i':
bellard92a31b12005-02-10 22:00:52 +00003944 case 'l':
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003945 case 'M':
bellard9307c4c2004-04-04 12:57:25 +00003946 {
blueswir1c2efc952007-09-25 17:28:42 +00003947 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00003948
blueswir1cd390082008-11-16 13:53:32 +00003949 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00003950 p++;
bellard34405572004-06-08 00:55:58 +00003951 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00003952 if (*typestr == '?') {
Luiz Capitulino53773582009-08-28 15:27:25 -03003953 if (*p == '\0') {
3954 typestr++;
3955 break;
3956 }
bellard34405572004-06-08 00:55:58 +00003957 } else {
3958 if (*p == '.') {
3959 p++;
blueswir1cd390082008-11-16 13:53:32 +00003960 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00003961 p++;
bellard34405572004-06-08 00:55:58 +00003962 } else {
Luiz Capitulino53773582009-08-28 15:27:25 -03003963 typestr++;
3964 break;
bellard34405572004-06-08 00:55:58 +00003965 }
3966 }
bellard13224a82006-07-14 22:03:35 +00003967 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00003968 }
aliguori376253e2009-03-05 23:01:23 +00003969 if (get_expr(mon, &val, &p))
bellard9307c4c2004-04-04 12:57:25 +00003970 goto fail;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003971 /* Check if 'i' is greater than 32-bit */
3972 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3973 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3974 monitor_printf(mon, "integer is for 32-bit values\n");
3975 goto fail;
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003976 } else if (c == 'M') {
Luiz Capitulino91162842012-04-26 17:34:30 -03003977 if (val < 0) {
3978 monitor_printf(mon, "enter a positive value\n");
3979 goto fail;
3980 }
Luiz Capitulinob6e098d2009-12-18 13:25:04 -02003981 val <<= 20;
Luiz Capitulino675ebef2009-08-28 15:27:26 -03003982 }
Luiz Capitulino53773582009-08-28 15:27:25 -03003983 qdict_put(qdict, key, qint_from_int(val));
bellard9307c4c2004-04-04 12:57:25 +00003984 }
3985 break;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003986 case 'o':
3987 {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01003988 int64_t val;
Jes Sorensendbc0c672010-10-21 17:15:47 +02003989 char *end;
3990
3991 while (qemu_isspace(*p)) {
3992 p++;
3993 }
3994 if (*typestr == '?') {
3995 typestr++;
3996 if (*p == '\0') {
3997 break;
3998 }
3999 }
4000 val = strtosz(p, &end);
4001 if (val < 0) {
4002 monitor_printf(mon, "invalid size\n");
4003 goto fail;
4004 }
4005 qdict_put(qdict, key, qint_from_int(val));
4006 p = end;
4007 }
4008 break;
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004009 case 'T':
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004010 {
4011 double val;
4012
4013 while (qemu_isspace(*p))
4014 p++;
4015 if (*typestr == '?') {
4016 typestr++;
4017 if (*p == '\0') {
4018 break;
4019 }
4020 }
4021 if (get_double(mon, &val, &p) < 0) {
4022 goto fail;
4023 }
Jes Sorensen07de3e62010-10-21 17:15:49 +02004024 if (p[0] && p[1] == 's') {
Markus Armbrusterfccfb11e2010-01-25 14:23:06 +01004025 switch (*p) {
4026 case 'm':
4027 val /= 1e3; p += 2; break;
4028 case 'u':
4029 val /= 1e6; p += 2; break;
4030 case 'n':
4031 val /= 1e9; p += 2; break;
4032 }
4033 }
Markus Armbruster3350a4d2010-01-25 14:23:03 +01004034 if (*p && !qemu_isspace(*p)) {
4035 monitor_printf(mon, "Unknown unit suffix\n");
4036 goto fail;
4037 }
4038 qdict_put(qdict, key, qfloat_from_double(val));
4039 }
4040 break;
Markus Armbruster942cd1f2010-03-26 09:07:09 +01004041 case 'b':
4042 {
4043 const char *beg;
4044 int val;
4045
4046 while (qemu_isspace(*p)) {
4047 p++;
4048 }
4049 beg = p;
4050 while (qemu_isgraph(*p)) {
4051 p++;
4052 }
4053 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4054 val = 1;
4055 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4056 val = 0;
4057 } else {
4058 monitor_printf(mon, "Expected 'on' or 'off'\n");
4059 goto fail;
4060 }
4061 qdict_put(qdict, key, qbool_from_int(val));
4062 }
4063 break;
bellard9307c4c2004-04-04 12:57:25 +00004064 case '-':
4065 {
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004066 const char *tmp = p;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004067 int skip_key = 0;
bellard9307c4c2004-04-04 12:57:25 +00004068 /* option */
ths3b46e622007-09-17 08:09:54 +00004069
bellard9307c4c2004-04-04 12:57:25 +00004070 c = *typestr++;
4071 if (c == '\0')
4072 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00004073 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004074 p++;
bellard9307c4c2004-04-04 12:57:25 +00004075 if (*p == '-') {
4076 p++;
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004077 if(c != *p) {
4078 if(!is_valid_option(p, typestr)) {
4079
4080 monitor_printf(mon, "%s: unsupported option -%c\n",
4081 cmdname, *p);
4082 goto fail;
4083 } else {
4084 skip_key = 1;
4085 }
bellard9307c4c2004-04-04 12:57:25 +00004086 }
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004087 if(skip_key) {
4088 p = tmp;
4089 } else {
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004090 /* has option */
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004091 p++;
Luiz Capitulinoeb159d12010-05-28 15:25:24 -03004092 qdict_put(qdict, key, qbool_from_int(1));
lirans@il.ibm.comfbc3d962009-11-02 15:41:13 +02004093 }
bellard9307c4c2004-04-04 12:57:25 +00004094 }
bellard9307c4c2004-04-04 12:57:25 +00004095 }
4096 break;
4097 default:
4098 bad_type:
aliguori376253e2009-03-05 23:01:23 +00004099 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
bellard9307c4c2004-04-04 12:57:25 +00004100 goto fail;
4101 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004102 g_free(key);
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004103 key = NULL;
bellard9307c4c2004-04-04 12:57:25 +00004104 }
4105 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00004106 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00004107 p++;
4108 if (*p != '\0') {
aliguori376253e2009-03-05 23:01:23 +00004109 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4110 cmdname);
bellard9307c4c2004-04-04 12:57:25 +00004111 goto fail;
4112 }
4113
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004114 return cmd;
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004115
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004116fail:
Anthony Liguori7267c092011-08-20 22:09:37 -05004117 g_free(key);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004118 return NULL;
4119}
4120
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004121void monitor_set_error(Monitor *mon, QError *qerror)
4122{
4123 /* report only the first error */
4124 if (!mon->error) {
4125 mon->error = qerror;
4126 } else {
Markus Armbrusterd6f46832010-02-17 10:52:26 +01004127 QDECREF(qerror);
4128 }
4129}
4130
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004131static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4132{
Luiz Capitulino6d441432010-11-22 16:35:09 -02004133 if (ret && !monitor_has_error(mon)) {
4134 /*
4135 * If it returns failure, it must have passed on error.
4136 *
4137 * Action: Report an internal error to the client if in QMP.
4138 */
4139 qerror_report(QERR_UNDEFINED_ERROR);
Luiz Capitulino6d441432010-11-22 16:35:09 -02004140 }
Luiz Capitulinobb89c2e2010-02-10 23:50:05 -02004141}
4142
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004143static void handle_user_command(Monitor *mon, const char *cmdline)
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004144{
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004145 QDict *qdict;
Anthony Liguoric227f092009-10-01 16:12:16 -05004146 const mon_cmd_t *cmd;
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004147
4148 qdict = qdict_new();
4149
Luiz Capitulino590fb3b2009-08-28 15:27:24 -03004150 cmd = monitor_parse_command(mon, cmdline, qdict);
Luiz Capitulino13917be2009-10-07 13:41:54 -03004151 if (!cmd)
4152 goto out;
4153
Luiz Capitulino4903de02010-09-16 11:01:32 -03004154 if (handler_is_async(cmd)) {
Adam Litke940cc302010-01-25 12:18:44 -06004155 user_async_cmd_handler(mon, cmd, qdict);
Luiz Capitulino9e807212010-09-16 10:58:59 -03004156 } else if (handler_is_qobject(cmd)) {
Luiz Capitulinode79ba62010-09-16 11:06:11 -03004157 QObject *data = NULL;
4158
4159 /* XXX: ignores the error code */
4160 cmd->mhandler.cmd_new(mon, qdict, &data);
4161 assert(!monitor_has_error(mon));
4162 if (data) {
4163 cmd->user_print(mon, data);
4164 qobject_decref(data);
4165 }
Luiz Capitulino13917be2009-10-07 13:41:54 -03004166 } else {
Luiz Capitulinoaf4ce882009-10-07 13:41:52 -03004167 cmd->mhandler.cmd(mon, qdict);
Luiz Capitulino55f81d92009-08-28 15:27:22 -03004168 }
4169
Luiz Capitulino13917be2009-10-07 13:41:54 -03004170out:
Luiz Capitulinof7188bb2009-08-28 15:27:10 -03004171 QDECREF(qdict);
bellard9dc39cb2004-03-14 21:38:27 +00004172}
4173
bellard81d09122004-07-14 17:21:37 +00004174static void cmd_completion(const char *name, const char *list)
4175{
4176 const char *p, *pstart;
4177 char cmd[128];
4178 int len;
4179
4180 p = list;
4181 for(;;) {
4182 pstart = p;
4183 p = strchr(p, '|');
4184 if (!p)
4185 p = pstart + strlen(pstart);
4186 len = p - pstart;
4187 if (len > sizeof(cmd) - 2)
4188 len = sizeof(cmd) - 2;
4189 memcpy(cmd, pstart, len);
4190 cmd[len] = '\0';
4191 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
aliguori731b0362009-03-05 23:01:42 +00004192 readline_add_completion(cur_mon->rs, cmd);
bellard81d09122004-07-14 17:21:37 +00004193 }
4194 if (*p == '\0')
4195 break;
4196 p++;
4197 }
4198}
4199
4200static void file_completion(const char *input)
4201{
4202 DIR *ffs;
4203 struct dirent *d;
4204 char path[1024];
4205 char file[1024], file_prefix[1024];
4206 int input_path_len;
4207 const char *p;
4208
ths5fafdf22007-09-16 21:08:06 +00004209 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00004210 if (!p) {
4211 input_path_len = 0;
4212 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00004213 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00004214 } else {
4215 input_path_len = p - input + 1;
4216 memcpy(path, input, input_path_len);
4217 if (input_path_len > sizeof(path) - 1)
4218 input_path_len = sizeof(path) - 1;
4219 path[input_path_len] = '\0';
4220 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4221 }
4222#ifdef DEBUG_COMPLETION
aliguori376253e2009-03-05 23:01:23 +00004223 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4224 input, path, file_prefix);
bellard81d09122004-07-14 17:21:37 +00004225#endif
4226 ffs = opendir(path);
4227 if (!ffs)
4228 return;
4229 for(;;) {
4230 struct stat sb;
4231 d = readdir(ffs);
4232 if (!d)
4233 break;
Kusanagi Kouichi46c7fc12010-10-20 18:00:01 +09004234
4235 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4236 continue;
4237 }
4238
bellard81d09122004-07-14 17:21:37 +00004239 if (strstart(d->d_name, file_prefix, NULL)) {
4240 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00004241 if (input_path_len < sizeof(file))
4242 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4243 d->d_name);
bellard81d09122004-07-14 17:21:37 +00004244 /* stat the file to find out if it's a directory.
4245 * In that case add a slash to speed up typing long paths
4246 */
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004247 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
blueswir1363a37d2008-08-21 17:58:08 +00004248 pstrcat(file, sizeof(file), "/");
Markus Armbrusterc951d9a2011-11-16 15:43:47 +01004249 }
aliguori731b0362009-03-05 23:01:42 +00004250 readline_add_completion(cur_mon->rs, file);
bellard81d09122004-07-14 17:21:37 +00004251 }
4252 }
4253 closedir(ffs);
4254}
4255
aliguori51de9762009-03-05 23:00:43 +00004256static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00004257{
aliguori51de9762009-03-05 23:00:43 +00004258 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00004259 const char *input = opaque;
4260
4261 if (input[0] == '\0' ||
4262 !strncmp(name, (char *)input, strlen(input))) {
aliguori731b0362009-03-05 23:01:42 +00004263 readline_add_completion(cur_mon->rs, name);
bellard81d09122004-07-14 17:21:37 +00004264 }
4265}
4266
4267/* NOTE: this parser is an approximate form of the real command parser */
4268static void parse_cmdline(const char *cmdline,
4269 int *pnb_args, char **args)
4270{
4271 const char *p;
4272 int nb_args, ret;
4273 char buf[1024];
4274
4275 p = cmdline;
4276 nb_args = 0;
4277 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00004278 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00004279 p++;
4280 if (*p == '\0')
4281 break;
4282 if (nb_args >= MAX_ARGS)
4283 break;
4284 ret = get_str(buf, sizeof(buf), &p);
Anthony Liguori7267c092011-08-20 22:09:37 -05004285 args[nb_args] = g_strdup(buf);
bellard81d09122004-07-14 17:21:37 +00004286 nb_args++;
4287 if (ret < 0)
4288 break;
4289 }
4290 *pnb_args = nb_args;
4291}
4292
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004293static const char *next_arg_type(const char *typestr)
4294{
4295 const char *p = strchr(typestr, ':');
4296 return (p != NULL ? ++p : typestr);
4297}
4298
aliguori4c36ba32009-03-05 23:01:37 +00004299static void monitor_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00004300{
4301 const char *cmdname;
4302 char *args[MAX_ARGS];
4303 int nb_args, i, len;
4304 const char *ptype, *str;
Anthony Liguoric227f092009-10-01 16:12:16 -05004305 const mon_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00004306 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00004307
4308 parse_cmdline(cmdline, &nb_args, args);
4309#ifdef DEBUG_COMPLETION
4310 for(i = 0; i < nb_args; i++) {
aliguori376253e2009-03-05 23:01:23 +00004311 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
bellard81d09122004-07-14 17:21:37 +00004312 }
4313#endif
4314
4315 /* if the line ends with a space, it means we want to complete the
4316 next arg */
4317 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00004318 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
Jan Kiszka03a63482010-06-16 00:38:33 +02004319 if (nb_args >= MAX_ARGS) {
4320 goto cleanup;
4321 }
Anthony Liguori7267c092011-08-20 22:09:37 -05004322 args[nb_args++] = g_strdup("");
bellard81d09122004-07-14 17:21:37 +00004323 }
4324 if (nb_args <= 1) {
4325 /* command completion */
4326 if (nb_args == 0)
4327 cmdname = "";
4328 else
4329 cmdname = args[0];
aliguori731b0362009-03-05 23:01:42 +00004330 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
aliguori376253e2009-03-05 23:01:23 +00004331 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
bellard81d09122004-07-14 17:21:37 +00004332 cmd_completion(cmdname, cmd->name);
4333 }
4334 } else {
4335 /* find the command */
Jan Kiszka03a63482010-06-16 00:38:33 +02004336 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4337 if (compare_cmd(args[0], cmd->name)) {
4338 break;
4339 }
bellard81d09122004-07-14 17:21:37 +00004340 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004341 if (!cmd->name) {
4342 goto cleanup;
4343 }
4344
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004345 ptype = next_arg_type(cmd->args_type);
bellard81d09122004-07-14 17:21:37 +00004346 for(i = 0; i < nb_args - 2; i++) {
4347 if (*ptype != '\0') {
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004348 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004349 while (*ptype == '?')
Luiz Capitulino4d76d2b2009-08-28 15:27:09 -03004350 ptype = next_arg_type(ptype);
bellard81d09122004-07-14 17:21:37 +00004351 }
4352 }
4353 str = args[nb_args - 1];
Blue Swirl2a1704a2009-08-23 20:10:28 +00004354 if (*ptype == '-' && ptype[1] != '\0') {
Jan Kiszka3b6dbf22010-06-16 00:38:34 +02004355 ptype = next_arg_type(ptype);
Blue Swirl2a1704a2009-08-23 20:10:28 +00004356 }
bellard81d09122004-07-14 17:21:37 +00004357 switch(*ptype) {
4358 case 'F':
4359 /* file completion */
aliguori731b0362009-03-05 23:01:42 +00004360 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004361 file_completion(str);
4362 break;
4363 case 'B':
4364 /* block device name completion */
aliguori731b0362009-03-05 23:01:42 +00004365 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard81d09122004-07-14 17:21:37 +00004366 bdrv_iterate(block_completion_it, (void *)str);
4367 break;
bellard7fe48482004-10-09 18:08:01 +00004368 case 's':
4369 /* XXX: more generic ? */
4370 if (!strcmp(cmd->name, "info")) {
aliguori731b0362009-03-05 23:01:42 +00004371 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard7fe48482004-10-09 18:08:01 +00004372 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4373 cmd_completion(str, cmd->name);
4374 }
bellard64866c32006-05-07 18:03:31 +00004375 } else if (!strcmp(cmd->name, "sendkey")) {
blueswir1e600d1e2009-03-08 17:42:02 +00004376 char *sep = strrchr(str, '-');
4377 if (sep)
4378 str = sep + 1;
aliguori731b0362009-03-05 23:01:42 +00004379 readline_set_completion_index(cur_mon->rs, strlen(str));
bellard64866c32006-05-07 18:03:31 +00004380 for(key = key_defs; key->name != NULL; key++) {
4381 cmd_completion(str, key->name);
4382 }
Jan Kiszkaf3353c62009-06-25 08:22:02 +02004383 } else if (!strcmp(cmd->name, "help|?")) {
4384 readline_set_completion_index(cur_mon->rs, strlen(str));
4385 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4386 cmd_completion(str, cmd->name);
4387 }
bellard7fe48482004-10-09 18:08:01 +00004388 }
4389 break;
bellard81d09122004-07-14 17:21:37 +00004390 default:
4391 break;
4392 }
4393 }
Jan Kiszka03a63482010-06-16 00:38:33 +02004394
4395cleanup:
4396 for (i = 0; i < nb_args; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004397 g_free(args[i]);
Jan Kiszka03a63482010-06-16 00:38:33 +02004398 }
bellard81d09122004-07-14 17:21:37 +00004399}
4400
aliguori731b0362009-03-05 23:01:42 +00004401static int monitor_can_read(void *opaque)
bellard9dc39cb2004-03-14 21:38:27 +00004402{
aliguori731b0362009-03-05 23:01:42 +00004403 Monitor *mon = opaque;
4404
Jan Kiszkac62313b2009-12-04 14:05:29 +01004405 return (mon->suspend_cnt == 0) ? 1 : 0;
bellard9dc39cb2004-03-14 21:38:27 +00004406}
4407
Luiz Capitulino09069b12010-02-04 18:10:06 -02004408static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4409{
4410 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4411 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4412}
4413
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004414/*
Luiz Capitulino4af91932010-06-22 11:44:05 -03004415 * Argument validation rules:
4416 *
4417 * 1. The argument must exist in cmd_args qdict
4418 * 2. The argument type must be the expected one
4419 *
4420 * Special case: If the argument doesn't exist in cmd_args and
4421 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4422 * checking is skipped for it.
4423 */
4424static int check_client_args_type(const QDict *client_args,
4425 const QDict *cmd_args, int flags)
4426{
4427 const QDictEntry *ent;
4428
4429 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4430 QObject *obj;
4431 QString *arg_type;
4432 const QObject *client_arg = qdict_entry_value(ent);
4433 const char *client_arg_name = qdict_entry_key(ent);
4434
4435 obj = qdict_get(cmd_args, client_arg_name);
4436 if (!obj) {
4437 if (flags & QMP_ACCEPT_UNKNOWNS) {
4438 /* handler accepts unknowns */
4439 continue;
4440 }
4441 /* client arg doesn't exist */
4442 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4443 return -1;
4444 }
4445
4446 arg_type = qobject_to_qstring(obj);
4447 assert(arg_type != NULL);
4448
4449 /* check if argument's type is correct */
4450 switch (qstring_get_str(arg_type)[0]) {
4451 case 'F':
4452 case 'B':
4453 case 's':
4454 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4455 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4456 "string");
4457 return -1;
4458 }
4459 break;
4460 case 'i':
4461 case 'l':
4462 case 'M':
Jes Sorensendbc0c672010-10-21 17:15:47 +02004463 case 'o':
Luiz Capitulino4af91932010-06-22 11:44:05 -03004464 if (qobject_type(client_arg) != QTYPE_QINT) {
4465 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4466 "int");
4467 return -1;
4468 }
4469 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004470 case 'T':
4471 if (qobject_type(client_arg) != QTYPE_QINT &&
4472 qobject_type(client_arg) != QTYPE_QFLOAT) {
4473 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4474 "number");
4475 return -1;
4476 }
4477 break;
4478 case 'b':
4479 case '-':
4480 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4481 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4482 "bool");
4483 return -1;
4484 }
4485 break;
4486 case 'O':
4487 assert(flags & QMP_ACCEPT_UNKNOWNS);
4488 break;
Paolo Bonzinib9f89782012-03-22 12:51:11 +01004489 case 'q':
4490 /* Any QObject can be passed. */
4491 break;
Luiz Capitulino4af91932010-06-22 11:44:05 -03004492 case '/':
4493 case '.':
4494 /*
4495 * These types are not supported by QMP and thus are not
4496 * handled here. Fall through.
4497 */
4498 default:
4499 abort();
4500 }
4501 }
4502
4503 return 0;
4504}
4505
4506/*
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004507 * - Check if the client has passed all mandatory args
4508 * - Set special flags for argument validation
4509 */
4510static int check_mandatory_args(const QDict *cmd_args,
4511 const QDict *client_args, int *flags)
4512{
4513 const QDictEntry *ent;
4514
4515 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4516 const char *cmd_arg_name = qdict_entry_key(ent);
4517 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4518 assert(type != NULL);
4519
4520 if (qstring_get_str(type)[0] == 'O') {
4521 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4522 *flags |= QMP_ACCEPT_UNKNOWNS;
4523 } else if (qstring_get_str(type)[0] != '-' &&
4524 qstring_get_str(type)[1] != '?' &&
4525 !qdict_haskey(client_args, cmd_arg_name)) {
4526 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4527 return -1;
4528 }
4529 }
4530
4531 return 0;
4532}
4533
4534static QDict *qdict_from_args_type(const char *args_type)
4535{
4536 int i;
4537 QDict *qdict;
4538 QString *key, *type, *cur_qs;
4539
4540 assert(args_type != NULL);
4541
4542 qdict = qdict_new();
4543
4544 if (args_type == NULL || args_type[0] == '\0') {
4545 /* no args, empty qdict */
4546 goto out;
4547 }
4548
4549 key = qstring_new();
4550 type = qstring_new();
4551
4552 cur_qs = key;
4553
4554 for (i = 0;; i++) {
4555 switch (args_type[i]) {
4556 case ',':
4557 case '\0':
4558 qdict_put(qdict, qstring_get_str(key), type);
4559 QDECREF(key);
4560 if (args_type[i] == '\0') {
4561 goto out;
4562 }
4563 type = qstring_new(); /* qdict has ref */
4564 cur_qs = key = qstring_new();
4565 break;
4566 case ':':
4567 cur_qs = type;
4568 break;
4569 default:
4570 qstring_append_chr(cur_qs, args_type[i]);
4571 break;
4572 }
4573 }
4574
4575out:
4576 return qdict;
4577}
4578
4579/*
4580 * Client argument checking rules:
4581 *
4582 * 1. Client must provide all mandatory arguments
Luiz Capitulino4af91932010-06-22 11:44:05 -03004583 * 2. Each argument provided by the client must be expected
4584 * 3. Each argument provided by the client must have the type expected
4585 * by the command
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004586 */
4587static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4588{
4589 int flags, err;
4590 QDict *cmd_args;
4591
4592 cmd_args = qdict_from_args_type(cmd->args_type);
4593
4594 flags = 0;
4595 err = check_mandatory_args(cmd_args, client_args, &flags);
4596 if (err) {
4597 goto out;
4598 }
4599
Luiz Capitulino4af91932010-06-22 11:44:05 -03004600 err = check_client_args_type(client_args, cmd_args, flags);
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004601
4602out:
4603 QDECREF(cmd_args);
4604 return err;
4605}
4606
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004607/*
4608 * Input object checking rules
4609 *
4610 * 1. Input object must be a dict
4611 * 2. The "execute" key must exist
4612 * 3. The "execute" key must be a string
4613 * 4. If the "arguments" key exists, it must be a dict
4614 * 5. If the "id" key exists, it can be anything (ie. json-value)
4615 * 6. Any argument not listed above is considered invalid
4616 */
4617static QDict *qmp_check_input_obj(QObject *input_obj)
4618{
4619 const QDictEntry *ent;
4620 int has_exec_key = 0;
4621 QDict *input_dict;
4622
4623 if (qobject_type(input_obj) != QTYPE_QDICT) {
4624 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4625 return NULL;
4626 }
4627
4628 input_dict = qobject_to_qdict(input_obj);
4629
4630 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4631 const char *arg_name = qdict_entry_key(ent);
4632 const QObject *arg_obj = qdict_entry_value(ent);
4633
4634 if (!strcmp(arg_name, "execute")) {
4635 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4636 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4637 "string");
4638 return NULL;
4639 }
4640 has_exec_key = 1;
4641 } else if (!strcmp(arg_name, "arguments")) {
4642 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4643 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4644 "object");
4645 return NULL;
4646 }
4647 } else if (!strcmp(arg_name, "id")) {
4648 /* FIXME: check duplicated IDs for async commands */
4649 } else {
4650 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4651 return NULL;
4652 }
4653 }
4654
4655 if (!has_exec_key) {
4656 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4657 return NULL;
4658 }
4659
4660 return input_dict;
4661}
4662
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004663static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4664 const QDict *params)
4665{
4666 int ret;
4667 QObject *data = NULL;
4668
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004669 ret = cmd->mhandler.cmd_new(mon, params, &data);
4670 handler_audit(mon, cmd, ret);
4671 monitor_protocol_emitter(mon, data);
4672 qobject_decref(data);
4673}
4674
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004675static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4676{
4677 int err;
4678 QObject *obj;
4679 QDict *input, *args;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004680 const mon_cmd_t *cmd;
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004681 const char *cmd_name;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004682 Monitor *mon = cur_mon;
4683
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004684 args = input = NULL;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004685
4686 obj = json_parser_parse(tokens, NULL);
4687 if (!obj) {
4688 // FIXME: should be triggered in json_parser_parse()
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004689 qerror_report(QERR_JSON_PARSING);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004690 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004691 }
4692
Luiz Capitulinoc917c8f2010-05-31 17:28:01 -03004693 input = qmp_check_input_obj(obj);
4694 if (!input) {
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004695 qobject_decref(obj);
4696 goto err_out;
4697 }
4698
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004699 mon->mc->id = qdict_get(input, "id");
4700 qobject_incref(mon->mc->id);
4701
Luiz Capitulino0bbab462010-05-31 17:32:50 -03004702 cmd_name = qdict_get_str(input, "execute");
Stefan Hajnoczi89bd8202011-09-23 08:23:06 +01004703 trace_handle_qmp_command(mon, cmd_name);
Luiz Capitulino09069b12010-02-04 18:10:06 -02004704 if (invalid_qmp_mode(mon, cmd_name)) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004705 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004706 goto err_out;
Luiz Capitulino09069b12010-02-04 18:10:06 -02004707 }
4708
Anthony Liguorie3193602011-09-02 12:34:47 -05004709 cmd = qmp_find_cmd(cmd_name);
Luiz Capitulinod1249ea2010-09-13 14:44:27 -03004710 if (!cmd) {
Markus Armbrusterab5b0272010-03-02 18:15:09 +01004711 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004712 goto err_out;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004713 }
4714
4715 obj = qdict_get(input, "arguments");
4716 if (!obj) {
4717 args = qdict_new();
4718 } else {
4719 args = qobject_to_qdict(obj);
4720 QINCREF(args);
4721 }
4722
Luiz Capitulino2dbc8db2010-05-26 16:13:09 -03004723 err = qmp_check_client_args(cmd, args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004724 if (err < 0) {
4725 goto err_out;
4726 }
4727
Luiz Capitulino40e5a012011-10-21 16:15:31 -02004728 if (handler_is_async(cmd)) {
Luiz Capitulino5af7bba2010-06-22 19:10:46 -03004729 err = qmp_async_cmd_handler(mon, cmd, args);
4730 if (err) {
4731 /* emit the error response */
4732 goto err_out;
4733 }
Adam Litke940cc302010-01-25 12:18:44 -06004734 } else {
Luiz Capitulinofc29df72010-09-16 11:11:18 -03004735 qmp_call_cmd(mon, cmd, args);
Adam Litke940cc302010-01-25 12:18:44 -06004736 }
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004737
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004738 goto out;
4739
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004740err_out:
4741 monitor_protocol_emitter(mon, NULL);
4742out:
Luiz Capitulinoe4940c62010-06-24 17:58:20 -03004743 QDECREF(input);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004744 QDECREF(args);
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004745}
4746
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004747/**
4748 * monitor_control_read(): Read and handle QMP input
4749 */
4750static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4751{
4752 Monitor *old_mon = cur_mon;
4753
4754 cur_mon = opaque;
4755
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004756 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004757
4758 cur_mon = old_mon;
4759}
4760
aliguori731b0362009-03-05 23:01:42 +00004761static void monitor_read(void *opaque, const uint8_t *buf, int size)
bellard9dc39cb2004-03-14 21:38:27 +00004762{
aliguori731b0362009-03-05 23:01:42 +00004763 Monitor *old_mon = cur_mon;
bellard9dc39cb2004-03-14 21:38:27 +00004764 int i;
aliguori376253e2009-03-05 23:01:23 +00004765
aliguori731b0362009-03-05 23:01:42 +00004766 cur_mon = opaque;
bellard7e2515e2004-08-01 21:52:19 +00004767
aliguoricde76ee2009-03-05 23:01:51 +00004768 if (cur_mon->rs) {
4769 for (i = 0; i < size; i++)
4770 readline_handle_byte(cur_mon->rs, buf[i]);
4771 } else {
4772 if (size == 0 || buf[size - 1] != 0)
4773 monitor_printf(cur_mon, "corrupted command\n");
4774 else
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004775 handle_user_command(cur_mon, (char *)buf);
aliguoricde76ee2009-03-05 23:01:51 +00004776 }
aliguori731b0362009-03-05 23:01:42 +00004777
4778 cur_mon = old_mon;
4779}
aliguorid8f44602008-10-06 13:52:44 +00004780
aliguori376253e2009-03-05 23:01:23 +00004781static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004782{
aliguori731b0362009-03-05 23:01:42 +00004783 monitor_suspend(mon);
Luiz Capitulinof3c157c2009-11-26 22:58:55 -02004784 handle_user_command(mon, cmdline);
aliguori731b0362009-03-05 23:01:42 +00004785 monitor_resume(mon);
aliguorid8f44602008-10-06 13:52:44 +00004786}
4787
aliguoricde76ee2009-03-05 23:01:51 +00004788int monitor_suspend(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004789{
aliguoricde76ee2009-03-05 23:01:51 +00004790 if (!mon->rs)
4791 return -ENOTTY;
aliguori731b0362009-03-05 23:01:42 +00004792 mon->suspend_cnt++;
aliguoricde76ee2009-03-05 23:01:51 +00004793 return 0;
aliguorid8f44602008-10-06 13:52:44 +00004794}
4795
aliguori376253e2009-03-05 23:01:23 +00004796void monitor_resume(Monitor *mon)
aliguorid8f44602008-10-06 13:52:44 +00004797{
aliguoricde76ee2009-03-05 23:01:51 +00004798 if (!mon->rs)
4799 return;
aliguori731b0362009-03-05 23:01:42 +00004800 if (--mon->suspend_cnt == 0)
4801 readline_show_prompt(mon->rs);
bellard7e2515e2004-08-01 21:52:19 +00004802}
4803
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004804static QObject *get_qmp_greeting(void)
4805{
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004806 QObject *ver = NULL;
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004807
Luiz Capitulinob9c15f12011-08-26 17:38:13 -03004808 qmp_marshal_input_query_version(NULL, NULL, &ver);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004809 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4810}
4811
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004812/**
4813 * monitor_control_event(): Print QMP gretting
4814 */
4815static void monitor_control_event(void *opaque, int event)
4816{
Luiz Capitulino47116d12010-02-08 17:01:30 -02004817 QObject *data;
4818 Monitor *mon = opaque;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004819
Luiz Capitulino47116d12010-02-08 17:01:30 -02004820 switch (event) {
4821 case CHR_EVENT_OPENED:
Luiz Capitulino4a7e1192010-02-04 18:10:05 -02004822 mon->mc->command_mode = 0;
Luiz Capitulino5fa737a2009-11-26 22:59:01 -02004823 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
Luiz Capitulinoca9567e2010-02-04 18:10:04 -02004824 data = get_qmp_greeting();
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004825 monitor_json_emitter(mon, data);
4826 qobject_decref(data);
Luiz Capitulino47116d12010-02-08 17:01:30 -02004827 break;
4828 case CHR_EVENT_CLOSED:
4829 json_message_parser_destroy(&mon->mc->parser);
4830 break;
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004831 }
4832}
4833
aliguori731b0362009-03-05 23:01:42 +00004834static void monitor_event(void *opaque, int event)
ths86e94de2007-01-05 22:01:59 +00004835{
aliguori376253e2009-03-05 23:01:23 +00004836 Monitor *mon = opaque;
4837
aliguori2724b182009-03-05 23:01:47 +00004838 switch (event) {
4839 case CHR_EVENT_MUX_IN:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004840 mon->mux_out = 0;
4841 if (mon->reset_seen) {
4842 readline_restart(mon->rs);
4843 monitor_resume(mon);
4844 monitor_flush(mon);
4845 } else {
4846 mon->suspend_cnt = 0;
4847 }
aliguori2724b182009-03-05 23:01:47 +00004848 break;
ths86e94de2007-01-05 22:01:59 +00004849
aliguori2724b182009-03-05 23:01:47 +00004850 case CHR_EVENT_MUX_OUT:
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004851 if (mon->reset_seen) {
4852 if (mon->suspend_cnt == 0) {
4853 monitor_printf(mon, "\n");
4854 }
4855 monitor_flush(mon);
4856 monitor_suspend(mon);
4857 } else {
4858 mon->suspend_cnt++;
4859 }
4860 mon->mux_out = 1;
aliguori2724b182009-03-05 23:01:47 +00004861 break;
4862
Amit Shahb6b8df52009-10-07 18:31:16 +05304863 case CHR_EVENT_OPENED:
aliguori2724b182009-03-05 23:01:47 +00004864 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4865 "information\n", QEMU_VERSION);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004866 if (!mon->mux_out) {
aliguori2724b182009-03-05 23:01:47 +00004867 readline_show_prompt(mon->rs);
Gerd Hoffmanna7aec5d2009-09-10 10:58:54 +02004868 }
4869 mon->reset_seen = 1;
aliguori2724b182009-03-05 23:01:47 +00004870 break;
4871 }
ths86e94de2007-01-05 22:01:59 +00004872}
4873
Wayne Xia816f8922011-10-12 11:32:41 +08004874static int
4875compare_mon_cmd(const void *a, const void *b)
4876{
4877 return strcmp(((const mon_cmd_t *)a)->name,
4878 ((const mon_cmd_t *)b)->name);
4879}
4880
4881static void sortcmdlist(void)
4882{
4883 int array_num;
4884 int elem_size = sizeof(mon_cmd_t);
4885
4886 array_num = sizeof(mon_cmds)/elem_size-1;
4887 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4888
4889 array_num = sizeof(info_cmds)/elem_size-1;
4890 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4891}
4892
aliguori76655d62009-03-06 20:27:37 +00004893
4894/*
4895 * Local variables:
4896 * c-indent-level: 4
4897 * c-basic-offset: 4
4898 * tab-width: 8
4899 * End:
4900 */
4901
aliguori731b0362009-03-05 23:01:42 +00004902void monitor_init(CharDriverState *chr, int flags)
bellard9dc39cb2004-03-14 21:38:27 +00004903{
aliguori731b0362009-03-05 23:01:42 +00004904 static int is_first_init = 1;
aliguori87127162009-03-05 23:01:29 +00004905 Monitor *mon;
ths20d8a3e2007-02-18 17:04:49 +00004906
4907 if (is_first_init) {
Paolo Bonzini74475452011-03-11 16:47:48 +01004908 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
Daniel P. Berrangeafeecec2012-06-14 18:12:57 +01004909 monitor_protocol_event_init();
ths20d8a3e2007-02-18 17:04:49 +00004910 is_first_init = 0;
4911 }
aliguori87127162009-03-05 23:01:29 +00004912
Anthony Liguori7267c092011-08-20 22:09:37 -05004913 mon = g_malloc0(sizeof(*mon));
ths20d8a3e2007-02-18 17:04:49 +00004914
aliguori87127162009-03-05 23:01:29 +00004915 mon->chr = chr;
aliguori731b0362009-03-05 23:01:42 +00004916 mon->flags = flags;
aliguoricde76ee2009-03-05 23:01:51 +00004917 if (flags & MONITOR_USE_READLINE) {
4918 mon->rs = readline_init(mon, monitor_find_completion);
4919 monitor_read_command(mon, 0);
4920 }
aliguori87127162009-03-05 23:01:29 +00004921
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004922 if (monitor_ctrl_mode(mon)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05004923 mon->mc = g_malloc0(sizeof(MonitorControl));
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004924 /* Control mode requires special handlers */
4925 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4926 monitor_control_event, mon);
Anthony Liguori15f31512011-08-15 11:17:35 -05004927 qemu_chr_fe_set_echo(chr, true);
Luiz Capitulino9b57c022009-11-26 22:58:58 -02004928 } else {
4929 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4930 monitor_event, mon);
4931 }
aliguori87127162009-03-05 23:01:29 +00004932
Blue Swirl72cf2d42009-09-12 07:36:22 +00004933 QLIST_INSERT_HEAD(&mon_list, mon, entry);
Markus Armbruster8631b602010-02-18 11:41:55 +01004934 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4935 default_mon = mon;
Wayne Xia816f8922011-10-12 11:32:41 +08004936
4937 sortcmdlist();
bellard7e2515e2004-08-01 21:52:19 +00004938}
4939
aliguori376253e2009-03-05 23:01:23 +00004940static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
bellard7e2515e2004-08-01 21:52:19 +00004941{
aliguoribb5fc202009-03-05 23:01:15 +00004942 BlockDriverState *bs = opaque;
4943 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00004944
aliguoribb5fc202009-03-05 23:01:15 +00004945 if (bdrv_set_key(bs, password) != 0) {
aliguori376253e2009-03-05 23:01:23 +00004946 monitor_printf(mon, "invalid password\n");
aliguoribb5fc202009-03-05 23:01:15 +00004947 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00004948 }
aliguori731b0362009-03-05 23:01:42 +00004949 if (mon->password_completion_cb)
4950 mon->password_completion_cb(mon->password_opaque, ret);
aliguoribb5fc202009-03-05 23:01:15 +00004951
aliguori731b0362009-03-05 23:01:42 +00004952 monitor_read_command(mon, 1);
bellard9dc39cb2004-03-14 21:38:27 +00004953}
aliguoric0f4ce72009-03-05 23:01:01 +00004954
Anthony Liguori7060b472011-09-02 12:34:50 -05004955ReadLineState *monitor_get_rs(Monitor *mon)
4956{
4957 return mon->rs;
4958}
4959
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004960int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4961 BlockDriverCompletionFunc *completion_cb,
4962 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00004963{
aliguoricde76ee2009-03-05 23:01:51 +00004964 int err;
4965
aliguoribb5fc202009-03-05 23:01:15 +00004966 if (!bdrv_key_required(bs)) {
4967 if (completion_cb)
4968 completion_cb(opaque, 0);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004969 return 0;
aliguoribb5fc202009-03-05 23:01:15 +00004970 }
aliguoric0f4ce72009-03-05 23:01:01 +00004971
Luiz Capitulino94171e12009-12-07 21:37:00 +01004972 if (monitor_ctrl_mode(mon)) {
Luiz Capitulino903a8812011-12-13 17:18:30 -02004973 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4974 bdrv_get_encrypted_filename(bs));
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004975 return -1;
Luiz Capitulino94171e12009-12-07 21:37:00 +01004976 }
4977
aliguori376253e2009-03-05 23:01:23 +00004978 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4979 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00004980
aliguori731b0362009-03-05 23:01:42 +00004981 mon->password_completion_cb = completion_cb;
4982 mon->password_opaque = opaque;
aliguoribb5fc202009-03-05 23:01:15 +00004983
aliguoricde76ee2009-03-05 23:01:51 +00004984 err = monitor_read_password(mon, bdrv_password_cb, bs);
4985
4986 if (err && completion_cb)
4987 completion_cb(opaque, err);
Luiz Capitulino0bbc47b2010-02-10 23:50:01 -02004988
4989 return err;
aliguoric0f4ce72009-03-05 23:01:01 +00004990}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -02004991
4992int monitor_read_block_device_key(Monitor *mon, const char *device,
4993 BlockDriverCompletionFunc *completion_cb,
4994 void *opaque)
4995{
4996 BlockDriverState *bs;
4997
4998 bs = bdrv_find(device);
4999 if (!bs) {
5000 monitor_printf(mon, "Device not found %s\n", device);
5001 return -1;
5002 }
5003
5004 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);
5005}